cwz set default regulator status
[firefly-linux-kernel-4.4.55.git] / drivers / power / bq27510_battery.c
1 /*
2  * BQ27510 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 <mach/gpio.h>
25 #include <linux/proc_fs.h>
26 #include <asm/uaccess.h>
27 #include <mach/board.h>
28
29
30 #define DRIVER_VERSION                  "1.1.0"
31 #define BQ27x00_REG_TEMP                0x06
32 #define BQ27x00_REG_VOLT                0x08
33 #define BQ27x00_REG_AI                  0x14
34 #define BQ27x00_REG_FLAGS               0x0A
35 #define BQ27x00_REG_TTE                 0x16
36 #define BQ27x00_REG_TTF                 0x18
37 #define BQ27x00_REG_TTECP               0x26
38 #define BQ27000_REG_RSOC                0x0B /* Relative State-of-Charge */
39 #define BQ27500_REG_SOC                 0x2c
40
41 #define BQ27500_FLAG_DSC                BIT(0)
42 #define BQ27000_FLAG_CHGS               BIT(8)
43 #define BQ27500_FLAG_FC                 BIT(9)
44 #define BQ27500_FLAG_OTD                BIT(14)
45 #define BQ27500_FLAG_OTC                BIT(15)
46
47 #define BQ27510_SPEED                   300 * 1000
48 int  virtual_battery_enable = 0;
49 extern int dwc_vbus_status(void);
50
51 #if 0
52 #define DBG(x...) printk(KERN_INFO x)
53 #else
54 #define DBG(x...) do { } while (0)
55 #endif
56
57 /* If the system has several batteries we need a different name for each
58  * of them...
59  */
60 static DEFINE_MUTEX(battery_mutex);
61
62 struct bq27510_device_info {
63         struct device           *dev;
64         struct power_supply     bat;
65         struct power_supply     ac;
66         struct delayed_work work;
67         struct i2c_client       *client;
68         unsigned int interval;
69         unsigned int dc_check_pin;
70         unsigned int bat_num;
71 };
72
73 static enum power_supply_property bq27510_battery_props[] = {
74         POWER_SUPPLY_PROP_STATUS,
75         POWER_SUPPLY_PROP_PRESENT,
76         POWER_SUPPLY_PROP_VOLTAGE_NOW,
77         POWER_SUPPLY_PROP_CURRENT_NOW,
78         POWER_SUPPLY_PROP_CAPACITY,
79         POWER_SUPPLY_PROP_TEMP,
80         POWER_SUPPLY_PROP_TECHNOLOGY,
81         POWER_SUPPLY_PROP_HEALTH,
82         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
83         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
84         //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
85 };
86
87 static enum power_supply_property rk29_ac_props[] = {
88         POWER_SUPPLY_PROP_ONLINE,
89 };
90
91 static ssize_t battery_proc_write(struct file *file,const char __user *buffer,
92                          unsigned long count,void *data)
93 {
94         char c;
95         int rc;
96         printk("USER:\n");
97         printk("echo x >/proc/driver/power\n");
98         printk("x=1,means just print log ||x=2,means log and data ||x= other,means close log\n");
99
100         rc = get_user(c,buffer);
101         if(rc)
102                 return rc;
103         if(c == '1')
104                 virtual_battery_enable = 1;
105         else if(c == '2')
106                 virtual_battery_enable = 2;
107         else if(c == '3')
108                 virtual_battery_enable = 3;
109         else 
110                 virtual_battery_enable = 0;
111         printk("%s,count(%d),virtual_battery_enable(%d)\n",__FUNCTION__,(int)count,virtual_battery_enable);
112         return count;
113 }
114
115 static const struct file_operations battery_proc_fops = {
116         .owner          = THIS_MODULE, 
117         .write          = battery_proc_write,
118 }; 
119
120 /*
121  * Common code for BQ27510 devices read
122  */
123 static int bq27510_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
124 {
125         int ret;
126         ret = i2c_master_reg8_recv(client, reg, buf, len, BQ27510_SPEED);
127         return ret; 
128 }
129 #ifdef CONFIG_CHECK_BATT_CAPACITY
130 static int bq27510_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
131 {
132         int ret; 
133         ret = i2c_master_reg8_send(client, reg, buf, (int)len, BQ27510_SPEED);
134         return ret;
135 }
136 #endif
137 /*
138  * Return the battery temperature in tenths of degree Celsius
139  * Or < 0 if something fails.
140  */
141 static int bq27510_battery_temperature(struct bq27510_device_info *di)
142 {
143         int ret;
144         int temp = 0;
145         u8 buf[2];
146
147         #if CONFIG_NO_BATTERY_IC
148         return 258;
149         #endif
150
151         if(virtual_battery_enable == 1)
152                 return 125/*258*/;
153         ret = bq27510_read(di->client,BQ27x00_REG_TEMP,buf,2);
154         if (ret<0) {
155                 dev_err(di->dev, "error reading temperature\n");
156                 return ret;
157         }
158         temp = get_unaligned_le16(buf);
159         temp = temp - 2731;
160         DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
161         return temp;
162 }
163
164 /*
165  * Return the battery Voltage in milivolts
166  * Or < 0 if something fails.
167  */
168 static int bq27510_battery_voltage(struct bq27510_device_info *di)
169 {
170         int ret;
171         u8 buf[2];
172         int volt = 0;
173
174         #if CONFIG_NO_BATTERY_IC
175         return 4000000;
176         #endif
177         if(virtual_battery_enable == 1)
178                 return 2000000/*4000000*/;
179
180         ret = bq27510_read(di->client,BQ27x00_REG_VOLT,buf,2); 
181         if (ret<0) {
182                 dev_err(di->dev, "error reading voltage\n");
183                 return ret;
184         }
185         volt = get_unaligned_le16(buf);
186
187         //bp27510 can only measure one li-lion bat
188         if(di->bat_num == 2){
189                 volt = volt * 1000 * 2;
190         }else{
191                 volt = volt * 1000;
192         }
193
194         DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
195         return volt;
196 }
197
198 /*
199  * Return the battery average current
200  * Note that current can be negative signed as well
201  * Or 0 if something fails.
202  */
203 static int bq27510_battery_current(struct bq27510_device_info *di)
204 {
205         int ret;
206         int curr = 0;
207         u8 buf[2];
208
209         #if CONFIG_NO_BATTERY_IC
210         return 22000;
211         #endif
212         if(virtual_battery_enable == 1)
213         return 11000/*22000*/;
214         ret = bq27510_read(di->client,BQ27x00_REG_AI,buf,2);
215         if (ret<0) {
216                 dev_err(di->dev, "error reading current\n");
217                 return 0;
218         }
219
220         curr = get_unaligned_le16(buf);
221         if(curr>0x8000){
222                 curr = 0xFFFF^(curr-1);
223         }
224         curr = curr * 1000;
225         DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
226         return curr;
227 }
228
229 /*
230  * Return the battery Relative State-of-Charge
231  * Or < 0 if something fails.
232  */
233 static int bq27510_battery_rsoc(struct bq27510_device_info *di)
234 {
235         int ret;
236         int rsoc = 0;
237         #if 0
238         int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
239         #endif
240         u8 buf[2];
241
242         #if CONFIG_NO_BATTERY_IC
243         return 100;
244         #endif
245         if(virtual_battery_enable == 1)
246         return 50/*100*/;
247         
248         ret = bq27510_read(di->client,BQ27500_REG_SOC,buf,2); 
249         if (ret<0) {
250                 dev_err(di->dev, "error reading relative State-of-Charge\n");
251                 return ret;
252         }
253         rsoc = get_unaligned_le16(buf);
254         DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
255
256         #if CONFIG_NO_BATTERY_IC
257         rsoc = 100;
258         #endif
259         #if 0
260         ret = bq27510_read(di->client,0x0c,buf,2);
261         nvcap = get_unaligned_le16(buf);
262         DBG("Enter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
263         ret = bq27510_read(di->client,0x0e,buf,2);
264         facap = get_unaligned_le16(buf);
265         DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
266         ret = bq27510_read(di->client,0x10,buf,2);
267         remcap = get_unaligned_le16(buf);
268         DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
269         ret = bq27510_read(di->client,0x12,buf,2);
270         fccap = get_unaligned_le16(buf);
271         DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
272         ret = bq27510_read(di->client,0x3c,buf,2);
273         full = get_unaligned_le16(buf);
274         DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,full);
275         ret = bq27510_read(di->client,0x00,buf,2);
276         cnt = get_unaligned_le16(buf);
277         DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,cnt);
278         #endif
279         return rsoc;
280 }
281
282 static int bq27510_battery_status(struct bq27510_device_info *di,
283                                   union power_supply_propval *val)
284 {
285         u8 buf[2];
286         int flags = 0;
287         int status;
288         int ret;
289
290         #if CONFIG_NO_BATTERY_IC
291         val->intval = POWER_SUPPLY_STATUS_FULL;
292         return 0;
293         #endif
294
295         if(virtual_battery_enable == 1)
296         {
297                 val->intval = POWER_SUPPLY_STATUS_FULL;
298                 return 0;
299         }
300         ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
301         if (ret < 0) {
302                 dev_err(di->dev, "error reading flags\n");
303                 return ret;
304         }
305         flags = get_unaligned_le16(buf);
306         DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
307         if (flags & BQ27500_FLAG_FC)
308                 status = POWER_SUPPLY_STATUS_FULL;
309         else if (flags & BQ27500_FLAG_DSC)
310                 status = POWER_SUPPLY_STATUS_DISCHARGING;
311         else
312                 status = POWER_SUPPLY_STATUS_CHARGING;
313
314         val->intval = status;
315         return 0;
316 }
317
318 static int bq27510_health_status(struct bq27510_device_info *di,
319                                   union power_supply_propval *val)
320 {
321         u8 buf[2];
322         int flags = 0;
323         int status;
324         int ret;
325         
326         #if CONFIG_NO_BATTERY_IC
327         val->intval = POWER_SUPPLY_HEALTH_GOOD;
328         return 0;
329         #endif
330
331         if(virtual_battery_enable == 1)
332         {
333                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
334                 return 0;
335         }
336         ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
337         if (ret < 0) {
338                 dev_err(di->dev, "error reading flags\n");
339                 return ret;
340         }
341         flags = get_unaligned_le16(buf);
342         DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
343         if ((flags & BQ27500_FLAG_OTD)||(flags & BQ27500_FLAG_OTC))
344                 status = POWER_SUPPLY_HEALTH_OVERHEAT;
345         else
346                 status = POWER_SUPPLY_HEALTH_GOOD;
347
348         val->intval = status;
349         return 0;
350 }
351
352
353 /*
354  * Read a time register.
355  * Return < 0 if something fails.
356  */
357 static int bq27510_battery_time(struct bq27510_device_info *di, int reg,
358                                 union power_supply_propval *val)
359 {
360         u8 buf[2];
361         int tval = 0;
362         int ret;
363
364         ret = bq27510_read(di->client,reg,buf,2);
365         if (ret<0) {
366                 dev_err(di->dev, "error reading register %02x\n", reg);
367                 return ret;
368         }
369         tval = get_unaligned_le16(buf);
370         DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
371         if (tval == 65535)
372                 return -ENODATA;
373
374         val->intval = tval * 60;
375         DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
376         return 0;
377 }
378
379 #define to_bq27510_device_info(x) container_of((x), \
380                                 struct bq27510_device_info, bat);
381
382 static int bq27510_battery_get_property(struct power_supply *psy,
383                                         enum power_supply_property psp,
384                                         union power_supply_propval *val)
385 {
386         int ret = 0;
387         struct bq27510_device_info *di = to_bq27510_device_info(psy);
388         DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
389         
390         switch (psp) {
391         
392         case POWER_SUPPLY_PROP_STATUS:
393                 ret = bq27510_battery_status(di, val);
394                 break;
395         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
396         case POWER_SUPPLY_PROP_PRESENT:
397                 val->intval = bq27510_battery_voltage(di);
398                 if (psp == POWER_SUPPLY_PROP_PRESENT)
399                         val->intval = val->intval <= 0 ? 0 : 1;
400                 break;
401         case POWER_SUPPLY_PROP_CURRENT_NOW:
402                 val->intval = bq27510_battery_current(di);
403                 break;
404         case POWER_SUPPLY_PROP_CAPACITY:
405                 val->intval = bq27510_battery_rsoc(di);
406                 break;
407         case POWER_SUPPLY_PROP_TEMP:
408                 val->intval = bq27510_battery_temperature(di);
409                 break;
410         case POWER_SUPPLY_PROP_TECHNOLOGY:
411                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;     
412                 break;
413         case POWER_SUPPLY_PROP_HEALTH:
414                 ret = bq27510_health_status(di, val);
415                 break;
416         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
417                 ret = bq27510_battery_time(di, BQ27x00_REG_TTE, val);
418                 break;
419         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
420                 ret = bq27510_battery_time(di, BQ27x00_REG_TTECP, val);
421                 break;
422         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
423                 ret = bq27510_battery_time(di, BQ27x00_REG_TTF, val);
424                 break;
425         default:
426                 return -EINVAL;
427         }
428
429         return ret;
430 }
431
432 static int rk29_ac_get_property(struct power_supply *psy,
433                         enum power_supply_property psp,
434                         union power_supply_propval *val)
435 {
436         int ret = 0;
437         struct bq27510_device_info *di = container_of(psy, struct bq27510_device_info, ac);
438         
439         switch (psp) {
440         case POWER_SUPPLY_PROP_ONLINE:
441                 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
442                         if(gpio_get_value(di->dc_check_pin))
443                                 val->intval = 0;        /*discharging*/
444                         else
445                                 val->intval = 1;        /*charging*/
446                 }
447                 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
448                 break;
449                 
450         default:
451                 ret = -EINVAL;
452                 break;
453         }
454         return ret;
455 }
456
457 static void bq27510_powersupply_init(struct bq27510_device_info *di)
458 {
459         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
460         di->bat.properties = bq27510_battery_props;
461         di->bat.num_properties = ARRAY_SIZE(bq27510_battery_props);
462         di->bat.get_property = bq27510_battery_get_property;
463         
464         di->ac.name = "ac";
465         di->ac.type = POWER_SUPPLY_TYPE_MAINS;
466         di->ac.properties = rk29_ac_props;
467         di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
468         di->ac.get_property = rk29_ac_get_property;
469 }
470
471
472 static void bq27510_battery_update_status(struct bq27510_device_info *di)
473 {
474         power_supply_changed(&di->bat);
475 }
476
477 static void bq27510_battery_work(struct work_struct *work)
478 {
479         struct bq27510_device_info *di = container_of(work, struct bq27510_device_info, work.work); 
480         bq27510_battery_update_status(di);
481         /* reschedule for the next time */
482         schedule_delayed_work(&di->work, di->interval);
483 }
484
485 static int bq27510_battery_probe(struct i2c_client *client,
486                                  const struct i2c_device_id *id)
487 {
488         struct bq27510_device_info *di;
489         int retval = 0;
490         struct bq27510_platform_data *pdata;
491         
492         #ifdef CONFIG_CHECK_BATT_CAPACITY
493         u8 buf[2];
494         #endif
495
496         pdata = client->dev.platform_data;
497         
498         di = kzalloc(sizeof(*di), GFP_KERNEL);
499         if (!di) {
500                 dev_err(&client->dev, "failed to allocate device info data\n");
501                 retval = -ENOMEM;
502                 goto batt_failed_2;
503         }
504         i2c_set_clientdata(client, di);
505         di->dev = &client->dev;
506         di->bat.name = "bq27510-battery";
507         di->client = client;
508         /* 4 seconds between monotor runs interval */
509         di->interval = msecs_to_jiffies(4 * 1000);
510         
511         di->bat_num = pdata->bat_num;
512         di->dc_check_pin = pdata->dc_check_pin;
513         
514         if (pdata->init_dc_check_pin)
515                 pdata->init_dc_check_pin( );
516         
517         bq27510_powersupply_init(di);
518         #ifdef CONFIG_CHECK_BATT_CAPACITY
519         buf[0] = 0x41;
520         buf[1] = 0x00;
521         bq27510_write(di->client,0x00,buf,2);
522         buf[0] = 0x21;
523         buf[1] = 0x00;
524         bq27510_write(di->client,0x00,buf,2);
525         #endif
526         retval = power_supply_register(&client->dev, &di->bat);
527         if (retval) {
528                 dev_err(&client->dev, "failed to register battery\n");
529                 goto batt_failed_4;
530         }
531         retval = power_supply_register(&client->dev, &di->ac);
532         if (retval) {
533                 dev_err(&client->dev, "failed to register ac\n");
534                 goto batt_failed_4;
535         }
536         
537         INIT_DELAYED_WORK(&di->work, bq27510_battery_work);
538         schedule_delayed_work(&di->work, di->interval);
539         dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
540         return 0;
541
542 batt_failed_4:
543         kfree(di);
544 batt_failed_2:
545         return retval;
546 }
547
548 static int bq27510_battery_remove(struct i2c_client *client)
549 {
550         struct bq27510_device_info *di = i2c_get_clientdata(client);
551
552         power_supply_unregister(&di->bat);
553         kfree(di->bat.name);
554         kfree(di);
555         return 0;
556 }
557
558 static const struct i2c_device_id bq27510_id[] = {
559         { "bq27510", 0 },
560 };
561
562 static struct i2c_driver bq27510_battery_driver = {
563         .driver = {
564                 .name = "bq27510",
565         },
566         .probe = bq27510_battery_probe,
567         .remove = bq27510_battery_remove,
568         .id_table = bq27510_id,
569 };
570
571 static int __init bq27510_battery_init(void)
572 {
573         int ret;
574         struct proc_dir_entry * battery_proc_entry;
575         
576         ret = i2c_add_driver(&bq27510_battery_driver);
577         if (ret)
578                 printk(KERN_ERR "Unable to register BQ27510 driver\n");
579         
580         battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
581         return ret;
582 }
583 module_init(bq27510_battery_init);
584
585 static void __exit bq27510_battery_exit(void)
586 {
587         i2c_del_driver(&bq27510_battery_driver);
588 }
589 module_exit(bq27510_battery_exit);
590
591 MODULE_AUTHOR("Rockchip");
592 MODULE_DESCRIPTION("BQ27510 battery monitor driver");
593 MODULE_LICENSE("GPL");