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 */
157 ret = bq24296_update_reg(bq24296_di->client,
158 POWE_ON_CONFIGURATION_REGISTER,
159 REGISTER_RESET_ENABLE << REGISTER_RESET_OFFSET,
160 REGISTER_RESET_MASK << REGISTER_RESET_OFFSET);
162 dev_err(&bq24296_di->client->dev, "%s(): Failed to reset the register \n",
169 /* Disable the watchdog */
170 ret = bq24296_update_reg(bq24296_di->client,
171 TERMINATION_TIMER_CONTROL_REGISTER,
172 WATCHDOG_DISABLE << WATCHDOG_OFFSET,
173 WATCHDOG_MASK << WATCHDOG_OFFSET);
175 dev_err(&bq24296_di->client->dev, "%s(): Failed to disable the watchdog \n",
180 /* Set Pre-Charge Current Limit as 128mA */
181 ret = bq24296_update_reg(bq24296_di->client,
182 PRE_CHARGE_TERMINATION_CURRENT_CONTROL_REGISTER,
183 PRE_CHARGE_CURRENT_LIMIT_128MA << PRE_CHARGE_CURRENT_LIMIT_OFFSET,
184 PRE_CHARGE_CURRENT_LIMIT_MASK << PRE_CHARGE_CURRENT_LIMIT_OFFSET);
186 dev_err(&bq24296_di->client->dev, "%s(): Failed to set pre-charge limit 128mA \n",
191 /* Set Termination Current Limit as 128mA */
192 ret = bq24296_update_reg(bq24296_di->client,
193 PRE_CHARGE_TERMINATION_CURRENT_CONTROL_REGISTER,
194 TERMINATION_CURRENT_LIMIT_128MA << TERMINATION_CURRENT_LIMIT_OFFSET,
195 TERMINATION_CURRENT_LIMIT_MASK << TERMINATION_CURRENT_LIMIT_OFFSET);
197 dev_err(&bq24296_di->client->dev, "%s(): Failed to set termination limit 128mA \n",
206 static int bq24296_get_limit_current(int value)
215 else if(value < 1000)
217 else if(value < 1200)
219 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);
387 DBG("%s: dwc_otg_check_dpdm %d\n", __func__, dwc_otg_check_dpdm(0));
388 switch(dwc_otg_check_dpdm(0))
390 case 2: // USB Wall charger
391 bq24296_update_input_current_limit(bq24296_di->adp_input_current);
392 bq24296_set_charge_current(bq24296_di->chg_current);
393 bq24296_charge_mode_config(0);
394 DBG("bq24296: detect usb wall charger\n");
398 if (0 == get_gadget_connect_flag()){ // non-standard AC charger
399 bq24296_update_input_current_limit(IINLIM_2000MA);
400 bq24296_set_charge_current(CHARGE_CURRENT_1024MA);
401 bq24296_charge_mode_config(0);;
405 bq24296_update_input_current_limit(bq24296_di->adp_input_current);
406 bq24296_set_charge_current(CHARGE_CURRENT_512MA);
407 bq24296_charge_mode_config(0);
408 DBG("bq24296: detect normal usb charger\n");
412 DBG("bq24296: detect no usb \n");
415 mutex_unlock(&pi->var_lock);
417 schedule_delayed_work(&pi->usb_detect_work, 1*HZ);
421 static void irq_work_func(struct work_struct *work)
423 // struct bq24296_device_info *info= container_of(work, struct bq24296_device_info, irq_work);
426 static irqreturn_t chg_irq_func(int irq, void *dev_id)
428 // struct bq24296_device_info *info = dev_id;
429 DBG("%s\n", __func__);
431 // queue_work(info->workqueue, &info->irq_work);
437 static struct bq24296_board *bq24296_parse_dt(struct bq24296_device_info *di)
439 struct bq24296_board *pdata;
440 struct device_node *bq24296_np;
442 DBG("%s,line=%d\n", __func__,__LINE__);
443 bq24296_np = of_node_get(di->dev->of_node);
445 printk("could not find bq24296-node\n");
448 pdata = devm_kzalloc(di->dev, sizeof(*pdata), GFP_KERNEL);
451 if (of_property_read_u32_array(bq24296_np,"bq24296,chg_current",pdata->chg_current, 3)) {
452 printk("dcdc sleep voltages not specified\n");
456 pdata->chg_irq_pin = of_get_named_gpio(bq24296_np,"gpios",0);
457 if (!gpio_is_valid(pdata->chg_irq_pin)) {
458 printk("invalid gpio: %d\n", pdata->chg_irq_pin);
465 static struct rk808_board *bq24296_parse_dt(struct bq24296_device_info *di)
472 static struct of_device_id bq24296_battery_of_match[] = {
473 { .compatible = "ti,bq24296"},
476 MODULE_DEVICE_TABLE(of, bq24296_battery_of_match);
479 static int bq24296_battery_probe(struct i2c_client *client,const struct i2c_device_id *id)
481 struct bq24296_device_info *di;
483 struct bq24296_board *pdev;
484 struct device_node *bq24296_node;
487 DBG("%s,line=%d\n", __func__,__LINE__);
489 bq24296_node = of_node_get(client->dev.of_node);
491 printk("could not find bq24296-node\n");
494 di = devm_kzalloc( &client->dev,sizeof(*di), GFP_KERNEL);
496 dev_err(&client->dev, "failed to allocate device info data\n");
500 i2c_set_clientdata(client, di);
501 di->dev = &client->dev;
504 pdev = bq24296_parse_dt(di);
506 bq24296_pdata = pdev;
508 DBG("%s,line=%d chg_current =%d usb_input_current = %d adp_input_current =%d \n", __func__,__LINE__,
509 pdev->chg_current[0],pdev->chg_current[1],pdev->chg_current[2]);
511 /******************get set current******/
512 if (pdev->chg_current[0] && pdev->chg_current[1] && pdev->chg_current[2]){
513 di->chg_current = bq24296_get_chg_current(pdev->chg_current[0] );
514 di->usb_input_current = bq24296_get_limit_current(pdev->chg_current[1]);
515 di->adp_input_current = bq24296_get_limit_current(pdev->chg_current[2]);
518 di->chg_current = bq24296_get_chg_current(1000);
519 di->usb_input_current = bq24296_get_limit_current(500);
520 di->adp_input_current = bq24296_get_limit_current(2000);
522 /****************************************/
524 /* get the vendor id */
525 ret = bq24296_read(di->client, VENDOR_STATS_REGISTER, &retval, 1);
527 dev_err(&di->client->dev, "%s(): Failed in reading register"
528 "0x%02x\n", __func__, VENDOR_STATS_REGISTER);
531 di->workqueue = create_singlethread_workqueue("bq24296_irq");
532 INIT_WORK(&di->irq_work, irq_work_func);
533 mutex_init(&di->var_lock);
534 INIT_DELAYED_WORK(&di->usb_detect_work, usb_detect_work_func);
535 schedule_delayed_work(&di->usb_detect_work, 0);
536 bq24296_init_registers();
539 if (gpio_is_valid(pdev->chg_irq_pin)){
540 irq = gpio_to_irq(pdev->chg_irq_pin);
541 ret = request_threaded_irq(irq, NULL,chg_irq_func, IRQF_TRIGGER_FALLING| IRQF_ONESHOT, "bq24296_chg_irq", di);
544 printk("failed to request bq24296_chg_irq\n");
545 goto err_chgirq_failed;
551 DBG("bq24296_battery_probe ok");
559 free_irq(gpio_to_irq(pdev->chg_irq_pin), NULL);
563 static void bq24296_battery_shutdown(struct i2c_client *client)
566 if (gpio_is_valid(bq24296_pdata->chg_irq_pin)){
567 free_irq(gpio_to_irq(bq24296_pdata->chg_irq_pin), NULL);
571 static int bq24296_battery_remove(struct i2c_client *client)
573 struct bq24296_device_info *di = i2c_get_clientdata(client);
578 static const struct i2c_device_id bq24296_id[] = {
582 static struct i2c_driver bq24296_battery_driver = {
585 .owner = THIS_MODULE,
586 .of_match_table =of_match_ptr(bq24296_battery_of_match),
588 .probe = bq24296_battery_probe,
589 .remove = bq24296_battery_remove,
590 .shutdown = bq24296_battery_shutdown,
591 .id_table = bq24296_id,
594 static int __init bq24296_battery_init(void)
598 ret = i2c_add_driver(&bq24296_battery_driver);
600 printk(KERN_ERR "Unable to register BQ24296 driver\n");
604 subsys_initcall(bq24296_battery_init);
606 static void __exit bq24296_battery_exit(void)
608 i2c_del_driver(&bq24296_battery_driver);
610 module_exit(bq24296_battery_exit);
612 MODULE_AUTHOR("Rockchip");
613 MODULE_DESCRIPTION("BQ24296 battery monitor driver");
614 MODULE_LICENSE("GPL");