2 * BQ24296 battery driver
4 * This package is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 #include <linux/module.h>
14 #include <linux/param.h>
15 #include <linux/jiffies.h>
16 #include <linux/workqueue.h>
17 #include <linux/delay.h>
18 #include <linux/platform_device.h>
19 #include <linux/power_supply.h>
20 #include <linux/idr.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
23 #include <asm/unaligned.h>
24 #include <linux/proc_fs.h>
25 #include <asm/uaccess.h>
26 #include <linux/power/bq24296_charger.h>
27 #include <linux/interrupt.h>
28 #include <linux/module.h>
29 #include <linux/of_irq.h>
30 #include <linux/of_gpio.h>
32 #include <linux/of_device.h>
34 struct bq24296_device_info *bq24296_di;
35 struct bq24296_board *bq24296_pdata;
36 static int bq24296_int = 0;
38 int bq24296_chag_down ;
40 #define DBG(x...) printk(KERN_INFO x)
42 #define DBG(x...) do { } while (0)
46 * Common code for BQ24296 devices read
48 static int bq24296_i2c_reg8_read(const struct i2c_client *client, const char reg, char *buf, int count, int scl_rate)
50 struct i2c_adapter *adap=client->adapter;
51 struct i2c_msg msgs[2];
55 msgs[0].addr = client->addr;
56 msgs[0].flags = client->flags;
58 msgs[0].buf = ®_buf;
59 msgs[0].scl_rate = scl_rate;
60 // msgs[0].udelay = client->udelay;
62 msgs[1].addr = client->addr;
63 msgs[1].flags = client->flags | I2C_M_RD;
65 msgs[1].buf = (char *)buf;
66 msgs[1].scl_rate = scl_rate;
67 // msgs[1].udelay = client->udelay;
69 ret = i2c_transfer(adap, msgs, 2);
71 return (ret == 2)? count : ret;
73 EXPORT_SYMBOL(bq24296_i2c_reg8_read);
75 static int bq24296_i2c_reg8_write(const struct i2c_client *client, const char reg, const char *buf, int count, int scl_rate)
77 struct i2c_adapter *adap=client->adapter;
80 char *tx_buf = (char *)kmalloc(count + 1, GFP_KERNEL);
84 memcpy(tx_buf+1, buf, count);
86 msg.addr = client->addr;
87 msg.flags = client->flags;
89 msg.buf = (char *)tx_buf;
90 msg.scl_rate = scl_rate;
91 // msg.udelay = client->udelay;
93 ret = i2c_transfer(adap, &msg, 1);
95 return (ret == 1) ? count : ret;
98 EXPORT_SYMBOL(bq24296_i2c_reg8_write);
100 static int bq24296_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
103 ret = bq24296_i2c_reg8_read(client, reg, buf, len, BQ24296_SPEED);
107 static int bq24296_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
110 ret = bq24296_i2c_reg8_write(client, reg, buf, (int)len, BQ24296_SPEED);
114 static ssize_t bat_param_read(struct device *dev,struct device_attribute *attr, char *buf)
118 struct bq24296_device_info *di=bq24296_di;
122 bq24296_read(di->client,i,&buffer,1);
123 DBG("reg %d value %x\n",i,buffer);
127 DEVICE_ATTR(battparam, 0664, bat_param_read,NULL);
129 static int bq24296_update_reg(struct i2c_client *client, int reg, u8 value, u8 mask )
134 ret = bq24296_read(client, reg, &retval, 1);
136 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
140 if ((retval & mask) != value) {
141 retval = ((retval & ~mask) | value) | value;
142 ret = bq24296_write(client, reg, &retval, 1);
144 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
152 static int bq24296_init_registers(void)
156 /* reset the register */
158 ret = bq24296_update_reg(bq24296_di->client,
159 POWE_ON_CONFIGURATION_REGISTER,
160 REGISTER_RESET_ENABLE << REGISTER_RESET_OFFSET,
161 REGISTER_RESET_MASK << REGISTER_RESET_OFFSET);
163 dev_err(&bq24296_di->client->dev, "%s(): Failed to reset the register \n",
170 /* Disable the watchdog */
171 ret = bq24296_update_reg(bq24296_di->client,
172 TERMINATION_TIMER_CONTROL_REGISTER,
173 WATCHDOG_DISABLE << WATCHDOG_OFFSET,
174 WATCHDOG_MASK << WATCHDOG_OFFSET);
176 dev_err(&bq24296_di->client->dev, "%s(): Failed to disable the watchdog \n",
181 /* Set Pre-Charge Current Limit as 128mA */
182 ret = bq24296_update_reg(bq24296_di->client,
183 PRE_CHARGE_TERMINATION_CURRENT_CONTROL_REGISTER,
184 PRE_CHARGE_CURRENT_LIMIT_128MA << PRE_CHARGE_CURRENT_LIMIT_OFFSET,
185 PRE_CHARGE_CURRENT_LIMIT_MASK << PRE_CHARGE_CURRENT_LIMIT_OFFSET);
187 dev_err(&bq24296_di->client->dev, "%s(): Failed to set pre-charge limit 128mA \n",
192 /* Set Termination Current Limit as 128mA */
193 ret = bq24296_update_reg(bq24296_di->client,
194 PRE_CHARGE_TERMINATION_CURRENT_CONTROL_REGISTER,
195 TERMINATION_CURRENT_LIMIT_128MA << TERMINATION_CURRENT_LIMIT_OFFSET,
196 TERMINATION_CURRENT_LIMIT_MASK << TERMINATION_CURRENT_LIMIT_OFFSET);
198 dev_err(&bq24296_di->client->dev, "%s(): Failed to set termination limit 128mA \n",
207 static int bq24296_get_limit_current(int value)
216 else if(value < 1000)
218 else if(value < 1200)
220 else if(value < 1800)
228 static int bq24296_get_chg_current(int value)
236 static int bq24296_update_input_current_limit(u8 value)
239 ret = bq24296_update_reg(bq24296_di->client,
240 INPUT_SOURCE_CONTROL_REGISTER,
241 ((value << IINLIM_OFFSET) | (EN_HIZ_DISABLE << EN_HIZ_OFFSET)),
242 ((IINLIM_MASK << IINLIM_OFFSET) | (EN_HIZ_MASK << EN_HIZ_OFFSET)));
244 dev_err(&bq24296_di->client->dev, "%s(): Failed to set input current limit (0x%x) \n",
250 static int bq24296_set_charge_current(u8 value)
254 ret = bq24296_update_reg(bq24296_di->client,
255 CHARGE_CURRENT_CONTROL_REGISTER,
256 (value << CHARGE_CURRENT_OFFSET) ,(CHARGE_CURRENT_MASK <<CHARGE_CURRENT_OFFSET ));
258 dev_err(&bq24296_di->client->dev, "%s(): Failed to set charge current limit (0x%x) \n",
264 static int bq24296_update_en_hiz_disable(void)
268 ret = bq24296_update_reg(bq24296_di->client,
269 INPUT_SOURCE_CONTROL_REGISTER,
270 EN_HIZ_DISABLE << EN_HIZ_OFFSET,
271 EN_HIZ_MASK << EN_HIZ_OFFSET);
273 dev_err(&bq24296_di->client->dev, "%s(): Failed to set en_hiz_disable\n",
279 int bq24296_set_input_current(int on)
285 #ifdef CONFIG_BATTERY_RK30_USB_AND_CHARGE
286 bq24296_update_input_current_limit(IINLIM_3000MA);
288 bq24296_update_input_current_limit(IINLIM_3000MA);
291 bq24296_update_input_current_limit(IINLIM_500MA);
293 DBG("bq24296_set_input_current %s\n", on ? "3000mA" : "500mA");
297 EXPORT_SYMBOL_GPL(bq24296_set_input_current);
299 static int bq24296_update_charge_mode(u8 value)
303 ret = bq24296_update_reg(bq24296_di->client,
304 POWE_ON_CONFIGURATION_REGISTER,
305 value << CHARGE_MODE_CONFIG_OFFSET,
306 CHARGE_MODE_CONFIG_MASK << CHARGE_MODE_CONFIG_OFFSET);
308 dev_err(&bq24296_di->client->dev, "%s(): Failed to set charge mode(0x%x) \n",
315 static int bq24296_update_otg_mode_current(u8 value)
319 ret = bq24296_update_reg(bq24296_di->client,
320 POWE_ON_CONFIGURATION_REGISTER,
321 value << OTG_MODE_CURRENT_CONFIG_OFFSET,
322 OTG_MODE_CURRENT_CONFIG_MASK << OTG_MODE_CURRENT_CONFIG_OFFSET);
324 dev_err(&bq24296_di->client->dev, "%s(): Failed to set otg current mode(0x%x) \n",
330 static int bq24296_charge_mode_config(int on)
338 bq24296_update_en_hiz_disable();
340 bq24296_update_charge_mode(CHARGE_MODE_CONFIG_OTG_OUTPUT);
342 bq24296_update_otg_mode_current(OTG_MODE_CURRENT_CONFIG_1300MA);
344 bq24296_update_charge_mode(CHARGE_MODE_CONFIG_CHARGE_BATTERY);
347 DBG("bq24296_charge_mode_config is %s\n", on ? "OTG Mode" : "Charge Mode");
351 int bq24296_charge_otg_en(int chg_en,int otg_en)
355 if ((chg_en ==0) && (otg_en ==0)){
356 ret = bq24296_update_reg(bq24296_di->client,POWE_ON_CONFIGURATION_REGISTER,0x00 << 4,0x03 << 4);
358 else if ((chg_en ==0) && (otg_en ==1))
359 bq24296_charge_mode_config(1);
361 bq24296_charge_mode_config(0);
365 extern int dwc_otg_check_dpdm(bool wait);
366 //extern int get_gadget_connect_flag(void);
368 static void usb_detect_work_func(struct work_struct *work)
370 struct delayed_work *delayed_work = (struct delayed_work *)container_of(work, struct delayed_work, work);
371 struct bq24296_device_info *pi = (struct bq24296_device_info *)container_of(delayed_work, struct bq24296_device_info, usb_detect_work);
375 ret = bq24296_read(bq24296_di->client, 0x08, &retval, 1);
377 dev_err(&bq24296_di->client->dev, "%s: err %d\n", __func__, ret);
379 if ((retval & 0x30) ==0x30){
380 bq24296_chag_down =1;
382 bq24296_chag_down =0;
384 DBG("%s: retval = %08x bq24296_chag_down = %d\n", __func__,retval,bq24296_chag_down);
386 mutex_lock(&pi->var_lock);
388 if (gpio_is_valid(bq24296_pdata->dc_det_pin)){
389 ret = gpio_request(bq24296_pdata->dc_det_pin, "bq24296_dc_det");
391 DBG("Failed to request gpio %d with ret:""%d\n",bq24296_pdata->dc_det_pin, ret);
393 gpio_direction_input(bq24296_pdata->dc_det_pin);
394 ret = gpio_get_value(bq24296_pdata->dc_det_pin);
396 bq24296_update_input_current_limit(bq24296_di->adp_input_current);
397 bq24296_set_charge_current(CHARGE_CURRENT_2048MA);
398 bq24296_charge_mode_config(0);
401 bq24296_update_input_current_limit(IINLIM_500MA);
402 bq24296_set_charge_current(CHARGE_CURRENT_512MA);
404 gpio_free(bq24296_pdata->dc_det_pin);
405 DBG("%s: bq24296_di->dc_det_pin=%x\n", __func__, ret);
408 DBG("%s: dwc_otg_check_dpdm %d\n", __func__, dwc_otg_check_dpdm(0));
409 switch(dwc_otg_check_dpdm(0))
411 case 2: // USB Wall charger
412 bq24296_update_input_current_limit(bq24296_di->usb_input_current);
413 bq24296_set_charge_current(CHARGE_CURRENT_2048MA);
414 bq24296_charge_mode_config(0);
415 DBG("bq24296: detect usb wall charger\n");
419 if (0 == get_gadget_connect_flag()){ // non-standard AC charger
420 bq24296_update_input_current_limit((bq24296_di->usb_input_current);
421 bq24296_set_charge_current(CHARGE_CURRENT_2048MA);
422 bq24296_charge_mode_config(0);;
426 bq24296_update_input_current_limit(bq24296_di->usb_input_current);
427 bq24296_set_charge_current(CHARGE_CURRENT_512MA);
428 bq24296_charge_mode_config(0);
429 DBG("bq24296: detect normal usb charger\n");
433 bq24296_update_input_current_limit(IINLIM_500MA);
434 bq24296_set_charge_current(CHARGE_CURRENT_512MA);
435 DBG("bq24296: detect no usb \n");
440 mutex_unlock(&pi->var_lock);
442 schedule_delayed_work(&pi->usb_detect_work, 1*HZ);
445 static void irq_work_func(struct work_struct *work)
447 // struct bq24296_device_info *info= container_of(work, struct bq24296_device_info, irq_work);
450 static irqreturn_t chg_irq_func(int irq, void *dev_id)
452 // struct bq24296_device_info *info = dev_id;
453 DBG("%s\n", __func__);
455 // queue_work(info->workqueue, &info->irq_work);
461 static struct bq24296_board *bq24296_parse_dt(struct bq24296_device_info *di)
463 struct bq24296_board *pdata;
464 struct device_node *bq24296_np;
466 DBG("%s,line=%d\n", __func__,__LINE__);
467 bq24296_np = of_node_get(di->dev->of_node);
469 printk("could not find bq24296-node\n");
472 pdata = devm_kzalloc(di->dev, sizeof(*pdata), GFP_KERNEL);
475 if (of_property_read_u32_array(bq24296_np,"bq24296,chg_current",pdata->chg_current, 3)) {
476 printk("dcdc sleep voltages not specified\n");
480 pdata->chg_irq_pin = of_get_named_gpio(bq24296_np,"gpios",0);
481 if (!gpio_is_valid(pdata->chg_irq_pin)) {
482 printk("invalid gpio: %d\n", pdata->chg_irq_pin);
485 pdata->dc_det_pin = of_get_named_gpio(bq24296_np,"gpios",1);
486 if (!gpio_is_valid(pdata->dc_det_pin)) {
487 printk("invalid gpio: %d\n", pdata->dc_det_pin);
494 static struct rk808_board *bq24296_parse_dt(struct bq24296_device_info *di)
501 static struct of_device_id bq24296_battery_of_match[] = {
502 { .compatible = "ti,bq24296"},
505 MODULE_DEVICE_TABLE(of, bq24296_battery_of_match);
508 static int bq24296_battery_suspend(struct i2c_client *client, pm_message_t mesg)
510 cancel_delayed_work_sync(&bq24296_di->usb_detect_work);
514 static int bq24296_battery_resume(struct i2c_client *client)
516 schedule_delayed_work(&bq24296_di->usb_detect_work, msecs_to_jiffies(50));
520 static int bq24296_battery_probe(struct i2c_client *client,const struct i2c_device_id *id)
522 struct bq24296_device_info *di;
524 struct bq24296_board *pdev;
525 struct device_node *bq24296_node;
528 DBG("%s,line=%d\n", __func__,__LINE__);
530 bq24296_node = of_node_get(client->dev.of_node);
532 printk("could not find bq24296-node\n");
535 di = devm_kzalloc(&client->dev,sizeof(*di), GFP_KERNEL);
537 dev_err(&client->dev, "failed to allocate device info data\n");
541 i2c_set_clientdata(client, di);
542 di->dev = &client->dev;
545 pdev = bq24296_parse_dt(di);
547 bq24296_pdata = pdev;
549 DBG("%s,line=%d chg_current =%d usb_input_current = %d adp_input_current =%d \n", __func__,__LINE__,
550 pdev->chg_current[0],pdev->chg_current[1],pdev->chg_current[2]);
552 /******************get set current******/
553 if (pdev->chg_current[0] && pdev->chg_current[1] && pdev->chg_current[2]){
554 di->chg_current = bq24296_get_chg_current(pdev->chg_current[0] );
555 di->usb_input_current = bq24296_get_limit_current(pdev->chg_current[1]);
556 di->adp_input_current = bq24296_get_limit_current(pdev->chg_current[2]);
559 di->chg_current = bq24296_get_chg_current(1000);
560 di->usb_input_current = bq24296_get_limit_current(500);
561 di->adp_input_current = bq24296_get_limit_current(2000);
563 /****************************************/
565 /* get the vendor id */
566 ret = bq24296_read(di->client, VENDOR_STATS_REGISTER, &retval, 1);
568 dev_err(&di->client->dev, "%s(): Failed in reading register"
569 "0x%02x\n", __func__, VENDOR_STATS_REGISTER);
572 di->workqueue = create_singlethread_workqueue("bq24296_irq");
573 INIT_WORK(&di->irq_work, irq_work_func);
574 mutex_init(&di->var_lock);
575 INIT_DELAYED_WORK(&di->usb_detect_work, usb_detect_work_func);
576 schedule_delayed_work(&di->usb_detect_work, 0);
577 bq24296_init_registers();
580 if (gpio_is_valid(pdev->chg_irq_pin)){
581 pdev->chg_irq = gpio_to_irq(pdev->chg_irq_pin);
582 ret = request_threaded_irq(pdev->chg_irq, NULL, chg_irq_func, IRQF_TRIGGER_FALLING| IRQF_ONESHOT, "bq24296_chg_irq", di);
585 printk("failed to request bq24296_chg_irq\n");
586 goto err_chgirq_failed;
592 DBG("bq24296_battery_probe ok");
596 free_irq(gpio_to_irq(pdev->chg_irq_pin), NULL);
601 static void bq24296_battery_shutdown(struct i2c_client *client)
603 struct bq24296_device_info *di = i2c_get_clientdata(client);
605 if (bq24296_pdata->chg_irq)
606 free_irq(bq24296_pdata->chg_irq, di);
609 static int bq24296_battery_remove(struct i2c_client *client)
611 struct bq24296_device_info *di = i2c_get_clientdata(client);
616 static const struct i2c_device_id bq24296_id[] = {
620 static struct i2c_driver bq24296_battery_driver = {
623 .owner = THIS_MODULE,
624 .of_match_table =of_match_ptr(bq24296_battery_of_match),
626 .probe = bq24296_battery_probe,
627 .remove = bq24296_battery_remove,
628 .shutdown = bq24296_battery_shutdown,
629 .suspend = bq24296_battery_suspend,
630 .resume = bq24296_battery_resume,
631 .id_table = bq24296_id,
634 static int __init bq24296_battery_init(void)
638 ret = i2c_add_driver(&bq24296_battery_driver);
640 printk(KERN_ERR "Unable to register BQ24296 driver\n");
644 subsys_initcall(bq24296_battery_init);
646 static void __exit bq24296_battery_exit(void)
648 i2c_del_driver(&bq24296_battery_driver);
650 module_exit(bq24296_battery_exit);
652 MODULE_AUTHOR("Rockchip");
653 MODULE_DESCRIPTION("BQ24296 battery monitor driver");
654 MODULE_LICENSE("GPL");