rk3288:bq24296:add dc_det_pin func,support power on when no usb
[firefly-linux-kernel-4.4.55.git] / drivers / power / bq24296_charger.c
1 /*
2  * BQ24296 battery driver
3  *
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.
7  *
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.
11  *
12  */
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>
31 #include <linux/of.h>
32 #include <linux/of_device.h>
33
34 struct bq24296_device_info *bq24296_di;
35 struct bq24296_board *bq24296_pdata;
36 static int bq24296_int = 0;
37 int bq24296_mode = 0;
38 int bq24296_chag_down ;
39 #if 0
40 #define DBG(x...) printk(KERN_INFO x)
41 #else
42 #define DBG(x...) do { } while (0)
43 #endif
44
45 /*
46  * Common code for BQ24296 devices read
47  */
48 static int bq24296_i2c_reg8_read(const struct i2c_client *client, const char reg, char *buf, int count, int scl_rate)
49 {
50         struct i2c_adapter *adap=client->adapter;
51         struct i2c_msg msgs[2];
52         int ret;
53         char reg_buf = reg;
54         
55         msgs[0].addr = client->addr;
56         msgs[0].flags = client->flags;
57         msgs[0].len = 1;
58         msgs[0].buf = &reg_buf;
59         msgs[0].scl_rate = scl_rate;
60 //      msgs[0].udelay = client->udelay;
61
62         msgs[1].addr = client->addr;
63         msgs[1].flags = client->flags | I2C_M_RD;
64         msgs[1].len = count;
65         msgs[1].buf = (char *)buf;
66         msgs[1].scl_rate = scl_rate;
67 //      msgs[1].udelay = client->udelay;
68
69         ret = i2c_transfer(adap, msgs, 2);
70
71         return (ret == 2)? count : ret;
72 }
73 EXPORT_SYMBOL(bq24296_i2c_reg8_read);
74
75 static int bq24296_i2c_reg8_write(const struct i2c_client *client, const char reg, const char *buf, int count, int scl_rate)
76 {
77         struct i2c_adapter *adap=client->adapter;
78         struct i2c_msg msg;
79         int ret;
80         char *tx_buf = (char *)kmalloc(count + 1, GFP_KERNEL);
81         if(!tx_buf)
82                 return -ENOMEM;
83         tx_buf[0] = reg;
84         memcpy(tx_buf+1, buf, count); 
85
86         msg.addr = client->addr;
87         msg.flags = client->flags;
88         msg.len = count + 1;
89         msg.buf = (char *)tx_buf;
90         msg.scl_rate = scl_rate;
91 //      msg.udelay = client->udelay;
92
93         ret = i2c_transfer(adap, &msg, 1);
94         kfree(tx_buf);
95         return (ret == 1) ? count : ret;
96
97 }
98 EXPORT_SYMBOL(bq24296_i2c_reg8_write);
99
100 static int bq24296_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
101 {
102         int ret;
103         ret = bq24296_i2c_reg8_read(client, reg, buf, len, BQ24296_SPEED);
104         return ret; 
105 }
106
107 static int bq24296_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
108 {
109         int ret; 
110         ret = bq24296_i2c_reg8_write(client, reg, buf, (int)len, BQ24296_SPEED);
111         return ret;
112 }
113
114 static ssize_t bat_param_read(struct device *dev,struct device_attribute *attr, char *buf)
115 {
116         int i;
117         u8 buffer;
118         struct bq24296_device_info *di=bq24296_di;
119
120         for(i=0;i<11;i++)
121         {
122                 bq24296_read(di->client,i,&buffer,1);
123                 DBG("reg %d value %x\n",i,buffer);              
124         }
125         return 0;
126 }
127 DEVICE_ATTR(battparam, 0664, bat_param_read,NULL);
128
129 static int bq24296_update_reg(struct i2c_client *client, int reg, u8 value, u8 mask )
130 {
131         int ret =0;
132         u8 retval = 0;
133
134         ret = bq24296_read(client, reg, &retval, 1);
135         if (ret < 0) {
136                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
137                 return ret;
138         }
139
140         if ((retval & mask) != value) {
141                 retval = ((retval & ~mask) | value) | value;
142                 ret = bq24296_write(client, reg, &retval, 1);
143                 if (ret < 0) {
144                         dev_err(&client->dev, "%s: err %d\n", __func__, ret);
145                         return ret;
146                 }
147         }
148
149         return ret;
150 }
151
152 static int bq24296_init_registers(void)
153 {
154         int ret = 0;
155
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);
161         if (ret < 0) {
162                 dev_err(&bq24296_di->client->dev, "%s(): Failed to reset the register \n",
163                                 __func__);
164                 goto final;
165         }
166
167         mdelay(5);
168
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);
174         if (ret < 0) {
175                 dev_err(&bq24296_di->client->dev, "%s(): Failed to disable the watchdog \n",
176                                 __func__);
177                 goto final;
178         }
179
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);
185         if (ret < 0) {
186                 dev_err(&bq24296_di->client->dev, "%s(): Failed to set pre-charge limit 128mA \n",
187                                 __func__);
188                 goto final;
189         }
190
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);
196         if (ret < 0) {
197                 dev_err(&bq24296_di->client->dev, "%s(): Failed to set termination limit 128mA \n",
198                                 __func__);
199                 goto final;
200         }
201
202 final:
203         return ret;
204 }
205
206 static int bq24296_get_limit_current(int value)
207 {
208         u8 data;
209         if (value < 120)
210                 data = 0;
211         else if(value < 400)
212                 data = 1;
213         else if(value < 700)
214                 data = 2;
215         else if(value < 1000)
216                 data = 3;
217         else if(value < 1200)
218                 data = 4;
219         else if(value < 1800)
220                 data = 6;
221         else
222                 data = 7;
223         data &= 0xff;
224         return data;
225         
226 }
227
228 static int bq24296_get_chg_current(int value)
229 {
230         u8 data;
231                 
232         data = (value)/64;
233         data &= 0xff;
234         return data;    
235 }
236 static int bq24296_update_input_current_limit(u8 value)
237 {
238         int ret = 0;
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)));
243         if (ret < 0) {
244                 dev_err(&bq24296_di->client->dev, "%s(): Failed to set input current limit (0x%x) \n",
245                                 __func__, value);
246         }
247         
248         return ret;
249 }
250  static int bq24296_set_charge_current(u8 value)
251 {
252         int ret = 0;
253
254         ret = bq24296_update_reg(bq24296_di->client,
255                                 CHARGE_CURRENT_CONTROL_REGISTER,
256                                 (value << CHARGE_CURRENT_OFFSET) ,(CHARGE_CURRENT_MASK <<CHARGE_CURRENT_OFFSET ));
257         if (ret < 0) {
258                 dev_err(&bq24296_di->client->dev, "%s(): Failed to set charge current limit (0x%x) \n",
259                                 __func__, value);
260         }
261         return ret;
262 }
263         
264 static int bq24296_update_en_hiz_disable(void)
265 {
266         int ret = 0;
267
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);
272         if (ret < 0) {
273                 dev_err(&bq24296_di->client->dev, "%s(): Failed to set en_hiz_disable\n",
274                                 __func__);
275         }
276         return ret;
277 }
278
279 int bq24296_set_input_current(int on)
280 {
281         if(!bq24296_int)
282                 return 0;
283
284         if(1 == on){
285 #ifdef CONFIG_BATTERY_RK30_USB_AND_CHARGE
286                 bq24296_update_input_current_limit(IINLIM_3000MA);
287 #else
288                 bq24296_update_input_current_limit(IINLIM_3000MA);
289 #endif
290         }else{
291                 bq24296_update_input_current_limit(IINLIM_500MA);
292         }
293         DBG("bq24296_set_input_current %s\n", on ? "3000mA" : "500mA");
294
295         return 0;
296 }
297 EXPORT_SYMBOL_GPL(bq24296_set_input_current);
298
299 static int bq24296_update_charge_mode(u8 value)
300 {
301         int ret = 0;
302
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);
307         if (ret < 0) {
308                 dev_err(&bq24296_di->client->dev, "%s(): Failed to set charge mode(0x%x) \n",
309                                 __func__, value);
310         }
311
312         return ret;
313 }
314
315 static int bq24296_update_otg_mode_current(u8 value)
316 {
317         int ret = 0;
318
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);
323         if (ret < 0) {
324                 dev_err(&bq24296_di->client->dev, "%s(): Failed to set otg current mode(0x%x) \n",
325                                 __func__, value);
326         }
327         return ret;
328 }
329
330 static int bq24296_charge_mode_config(int on)
331 {
332
333         if(!bq24296_int)
334                 return 0;
335
336         if(1 == on)
337         {
338                 bq24296_update_en_hiz_disable();
339                 mdelay(5);
340                 bq24296_update_charge_mode(CHARGE_MODE_CONFIG_OTG_OUTPUT);
341                 mdelay(10);
342                 bq24296_update_otg_mode_current(OTG_MODE_CURRENT_CONFIG_1300MA);
343         }else{
344                 bq24296_update_charge_mode(CHARGE_MODE_CONFIG_CHARGE_BATTERY);
345         }
346
347         DBG("bq24296_charge_mode_config is %s\n", on ? "OTG Mode" : "Charge Mode");
348
349         return 0;
350 }
351  int bq24296_charge_otg_en(int chg_en,int otg_en)
352 {
353         int ret = 0;
354
355         if ((chg_en ==0) && (otg_en ==0)){
356                 ret = bq24296_update_reg(bq24296_di->client,POWE_ON_CONFIGURATION_REGISTER,0x00 << 4,0x03 << 4);
357         }
358         else if ((chg_en ==0) && (otg_en ==1))
359                 bq24296_charge_mode_config(1);
360         else 
361                 bq24296_charge_mode_config(0);
362         return ret;
363 }
364
365 extern int dwc_otg_check_dpdm(bool wait);
366 //extern int get_gadget_connect_flag(void);
367
368 static void usb_detect_work_func(struct work_struct *work)
369 {
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);
372         u8 retval = 0;
373         int ret ;
374
375         ret = bq24296_read(bq24296_di->client, 0x08, &retval, 1);
376         if (ret < 0) {
377                 dev_err(&bq24296_di->client->dev, "%s: err %d\n", __func__, ret);
378         }
379         if ((retval & 0x30) ==0x30){
380                 bq24296_chag_down =1;
381         }else
382                 bq24296_chag_down =0;
383
384         DBG("%s: retval = %08x bq24296_chag_down = %d\n", __func__,retval,bq24296_chag_down);
385         
386         #ifdef CONFIG_OF
387         if (gpio_is_valid(bq24296_pdata->dc_det_pin)){
388                         ret = gpio_request(bq24296_pdata->dc_det_pin, "bq24296_dc_det");
389                         if (ret < 0) {
390                                 DBG("Failed to request gpio %d with ret:""%d\n",bq24296_pdata->dc_det_pin, ret);
391                         }
392                         gpio_direction_input(bq24296_pdata->dc_det_pin);
393                         ret = gpio_get_value(bq24296_pdata->dc_det_pin);
394                         if (ret ==0){
395                                 bq24296_update_input_current_limit(bq24296_di->adp_input_current);
396                                 bq24296_set_charge_current(bq24296_di->chg_current);
397                                 bq24296_charge_mode_config(0);
398                         }
399                         gpio_free(bq24296_pdata->dc_det_pin);
400                         DBG("%s: bq24296_di->dc_det_pin=%x\n", __func__, ret);
401         }       
402         #endif
403         
404         mutex_lock(&pi->var_lock);
405         DBG("%s: dwc_otg_check_dpdm %d\n", __func__, dwc_otg_check_dpdm(0));
406         switch(dwc_otg_check_dpdm(0))
407                 {
408                         case 2: // USB Wall charger
409                                 bq24296_update_input_current_limit(bq24296_di->adp_input_current);
410                                 bq24296_set_charge_current(bq24296_di->chg_current);
411                                 bq24296_charge_mode_config(0);
412                                 DBG("bq24296: detect usb wall charger\n");
413                         break;
414                         case 1: //normal USB
415                                 #if 0
416                                 if (0 == get_gadget_connect_flag()){  // non-standard AC charger
417                                 bq24296_update_input_current_limit(IINLIM_2000MA);
418                                 bq24296_set_charge_current(CHARGE_CURRENT_1024MA);
419                                 bq24296_charge_mode_config(0);;
420                                 }else{
421                                 #endif
422                                 // connect to pc        
423                                 bq24296_update_input_current_limit(bq24296_di->adp_input_current);
424                                 bq24296_set_charge_current(CHARGE_CURRENT_512MA);
425                                 bq24296_charge_mode_config(0);
426                                 DBG("bq24296: detect normal usb charger\n");
427                         //      }
428                         break;
429                         default:
430                                 DBG("bq24296: detect no usb \n");                       
431                         break;
432                 }
433         mutex_unlock(&pi->var_lock);
434         
435         schedule_delayed_work(&pi->usb_detect_work, 1*HZ);
436 }
437
438
439 static void irq_work_func(struct work_struct *work)
440 {
441 //      struct bq24296_device_info *info= container_of(work, struct bq24296_device_info, irq_work);
442 }
443
444 static irqreturn_t chg_irq_func(int irq, void *dev_id)
445 {
446 //      struct bq24296_device_info *info = dev_id;
447         DBG("%s\n", __func__);
448
449 //      queue_work(info->workqueue, &info->irq_work);
450
451         return IRQ_HANDLED;
452 }
453
454 #ifdef CONFIG_OF
455 static struct bq24296_board *bq24296_parse_dt(struct bq24296_device_info *di)
456 {
457         struct bq24296_board *pdata;
458         struct device_node *bq24296_np;
459         
460         DBG("%s,line=%d\n", __func__,__LINE__);
461         bq24296_np = of_node_get(di->dev->of_node);
462         if (!bq24296_np) {
463                 printk("could not find bq24296-node\n");
464                 return NULL;
465         }
466         pdata = devm_kzalloc(di->dev, sizeof(*pdata), GFP_KERNEL);
467         if (!pdata)
468                 return NULL;
469         if (of_property_read_u32_array(bq24296_np,"bq24296,chg_current",pdata->chg_current, 3)) {
470                 printk("dcdc sleep voltages not specified\n");
471                 return NULL;
472         }
473         
474         pdata->chg_irq_pin = of_get_named_gpio(bq24296_np,"gpios",0);
475         if (!gpio_is_valid(pdata->chg_irq_pin)) {
476                 printk("invalid gpio: %d\n",  pdata->chg_irq_pin);
477         }
478
479         pdata->dc_det_pin = of_get_named_gpio(bq24296_np,"gpios",1);
480         if (!gpio_is_valid(pdata->dc_det_pin)) {
481                 printk("invalid gpio: %d\n",  pdata->dc_det_pin);
482         }
483         
484         return pdata;
485 }
486
487 #else
488 static struct rk808_board *bq24296_parse_dt(struct bq24296_device_info *di)
489 {
490         return NULL;
491 }
492 #endif
493
494 #ifdef CONFIG_OF
495 static struct of_device_id bq24296_battery_of_match[] = {
496         { .compatible = "ti,bq24296"},
497         { },
498 };
499 MODULE_DEVICE_TABLE(of, bq24296_battery_of_match);
500 #endif
501
502 static int bq24296_battery_probe(struct i2c_client *client,const struct i2c_device_id *id)
503 {
504         struct bq24296_device_info *di;
505         u8 retval = 0;
506         struct bq24296_board *pdev;
507         struct device_node *bq24296_node;
508         int ret=0,irq=0;
509         
510          DBG("%s,line=%d\n", __func__,__LINE__);
511          
512          bq24296_node = of_node_get(client->dev.of_node);
513         if (!bq24296_node) {
514                 printk("could not find bq24296-node\n");
515         }
516          
517         di = devm_kzalloc( &client->dev,sizeof(*di), GFP_KERNEL);
518         if (!di) {
519                 dev_err(&client->dev, "failed to allocate device info data\n");
520                 retval = -ENOMEM;
521                 goto batt_failed_2;
522         }
523         i2c_set_clientdata(client, di);
524         di->dev = &client->dev;
525         di->client = client;    
526         if (bq24296_node)
527                 pdev = bq24296_parse_dt(di);
528         
529         bq24296_pdata = pdev;
530         
531          DBG("%s,line=%d chg_current =%d usb_input_current = %d adp_input_current =%d \n", __func__,__LINE__,
532                 pdev->chg_current[0],pdev->chg_current[1],pdev->chg_current[2]);
533          
534          /******************get set current******/
535         if (pdev->chg_current[0] && pdev->chg_current[1] && pdev->chg_current[2]){
536                 di->chg_current = bq24296_get_chg_current(pdev->chg_current[0] );
537                 di->usb_input_current  = bq24296_get_limit_current(pdev->chg_current[1]);
538                 di->adp_input_current  = bq24296_get_limit_current(pdev->chg_current[2]);
539         }
540         else {
541                 di->chg_current = bq24296_get_chg_current(1000);
542                 di->usb_input_current  = bq24296_get_limit_current(500);
543                 di->adp_input_current  = bq24296_get_limit_current(2000);
544         }
545         /****************************************/
546         bq24296_di = di;
547         /* get the vendor id */
548         ret = bq24296_read(di->client, VENDOR_STATS_REGISTER, &retval, 1);
549         if (ret < 0) {
550                 dev_err(&di->client->dev, "%s(): Failed in reading register"
551                                 "0x%02x\n", __func__, VENDOR_STATS_REGISTER);
552                 goto batt_failed_4;
553         }
554         di->workqueue = create_singlethread_workqueue("bq24296_irq");
555         INIT_WORK(&di->irq_work, irq_work_func);
556         mutex_init(&di->var_lock);
557         INIT_DELAYED_WORK(&di->usb_detect_work, usb_detect_work_func);
558         schedule_delayed_work(&di->usb_detect_work, 0);
559         bq24296_init_registers();
560
561
562         if (gpio_is_valid(pdev->chg_irq_pin)){
563                 irq = gpio_to_irq(pdev->chg_irq_pin);
564                 ret = request_threaded_irq(irq, NULL,chg_irq_func, IRQF_TRIGGER_FALLING| IRQF_ONESHOT, "bq24296_chg_irq", di);
565                 if (ret) {
566                         ret = -EINVAL;
567                         printk("failed to request bq24296_chg_irq\n");
568                         goto err_chgirq_failed;
569                 }
570         }
571
572         bq24296_int =1;
573
574         DBG("bq24296_battery_probe ok");
575         return 0;
576
577 batt_failed_4:
578         kfree(di);
579 batt_failed_2:
580         
581 err_chgirq_failed:
582         free_irq(gpio_to_irq(pdev->chg_irq_pin), NULL);
583         return retval;
584 }
585
586 static void bq24296_battery_shutdown(struct i2c_client *client)
587 {
588
589         if (gpio_is_valid(bq24296_pdata->chg_irq_pin)){
590         free_irq(gpio_to_irq(bq24296_pdata->chg_irq_pin), NULL);
591         }
592         
593 }
594 static int bq24296_battery_remove(struct i2c_client *client)
595 {
596         struct bq24296_device_info *di = i2c_get_clientdata(client);
597         kfree(di);
598         return 0;
599 }
600
601 static const struct i2c_device_id bq24296_id[] = {
602         { "bq24296", 0 },
603 };
604
605 static struct i2c_driver bq24296_battery_driver = {
606         .driver = {
607                 .name = "bq24296",
608                 .owner = THIS_MODULE,
609                 .of_match_table =of_match_ptr(bq24296_battery_of_match),
610         },
611         .probe = bq24296_battery_probe,
612         .remove = bq24296_battery_remove,
613         .shutdown = bq24296_battery_shutdown,
614         .id_table = bq24296_id,
615 };
616
617 static int __init bq24296_battery_init(void)
618 {
619         int ret;
620         
621         ret = i2c_add_driver(&bq24296_battery_driver);
622         if (ret)
623                 printk(KERN_ERR "Unable to register BQ24296 driver\n");
624         
625         return ret;
626 }
627 subsys_initcall(bq24296_battery_init);
628
629 static void __exit bq24296_battery_exit(void)
630 {
631         i2c_del_driver(&bq24296_battery_driver);
632 }
633 module_exit(bq24296_battery_exit);
634
635 MODULE_AUTHOR("Rockchip");
636 MODULE_DESCRIPTION("BQ24296 battery monitor driver");
637 MODULE_LICENSE("GPL");