close log print in bq27510
[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         if(virtual_battery_enable == 1)
147                 return 125/*258*/;
148         ret = bq27510_read(di->client,BQ27x00_REG_TEMP,buf,2);
149         if (ret<0) {
150                 dev_err(di->dev, "error reading temperature\n");
151                 return ret;
152         }
153         temp = get_unaligned_le16(buf);
154         temp = temp - 2731;
155         DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
156         return temp;
157 }
158
159 /*
160  * Return the battery Voltage in milivolts
161  * Or < 0 if something fails.
162  */
163 static int bq27510_battery_voltage(struct bq27510_device_info *di)
164 {
165         int ret;
166         u8 buf[2];
167         int volt = 0;
168         if(virtual_battery_enable == 1)
169                 return 2000000/*4000000*/;
170
171         ret = bq27510_read(di->client,BQ27x00_REG_VOLT,buf,2); 
172         if (ret<0) {
173                 dev_err(di->dev, "error reading voltage\n");
174                 return ret;
175         }
176         volt = get_unaligned_le16(buf);
177
178         //bp27510 can only measure one li-lion bat
179         if(di->bat_num == 2){
180                 volt = volt * 1000 * 2;
181         }else{
182                 volt = volt * 1000;
183         }
184
185         DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
186         return volt;
187 }
188
189 /*
190  * Return the battery average current
191  * Note that current can be negative signed as well
192  * Or 0 if something fails.
193  */
194 static int bq27510_battery_current(struct bq27510_device_info *di)
195 {
196         int ret;
197         int curr = 0;
198         u8 buf[2];
199         if(virtual_battery_enable == 1)
200         return 11000/*22000*/;
201         ret = bq27510_read(di->client,BQ27x00_REG_AI,buf,2);
202         if (ret<0) {
203                 dev_err(di->dev, "error reading current\n");
204                 return 0;
205         }
206
207         curr = get_unaligned_le16(buf);
208         if(curr>0x8000){
209                 curr = 0xFFFF^(curr-1);
210         }
211         curr = curr * 1000;
212         DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
213         return curr;
214 }
215
216 /*
217  * Return the battery Relative State-of-Charge
218  * Or < 0 if something fails.
219  */
220 static int bq27510_battery_rsoc(struct bq27510_device_info *di)
221 {
222         int ret;
223         int rsoc = 0;
224         #if 0
225         int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
226         #endif
227         u8 buf[2];
228         if(virtual_battery_enable == 1)
229         return 50/*100*/;
230         
231         ret = bq27510_read(di->client,BQ27500_REG_SOC,buf,2); 
232         if (ret<0) {
233                 dev_err(di->dev, "error reading relative State-of-Charge\n");
234                 return ret;
235         }
236         rsoc = get_unaligned_le16(buf);
237         DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
238         #if 0
239         ret = bq27510_read(di->client,0x0c,buf,2);
240         nvcap = get_unaligned_le16(buf);
241         DBG("Enter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
242         ret = bq27510_read(di->client,0x0e,buf,2);
243         facap = get_unaligned_le16(buf);
244         DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
245         ret = bq27510_read(di->client,0x10,buf,2);
246         remcap = get_unaligned_le16(buf);
247         DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
248         ret = bq27510_read(di->client,0x12,buf,2);
249         fccap = get_unaligned_le16(buf);
250         DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
251         ret = bq27510_read(di->client,0x3c,buf,2);
252         full = get_unaligned_le16(buf);
253         DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,full);
254         ret = bq27510_read(di->client,0x00,buf,2);
255         cnt = get_unaligned_le16(buf);
256         DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,cnt);
257         #endif
258         return rsoc;
259 }
260
261 static int bq27510_battery_status(struct bq27510_device_info *di,
262                                   union power_supply_propval *val)
263 {
264         u8 buf[2];
265         int flags = 0;
266         int status;
267         int ret;
268         if(virtual_battery_enable == 1)
269         {
270                 val->intval = POWER_SUPPLY_STATUS_FULL;
271                 return 0;
272         }
273         ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
274         if (ret < 0) {
275                 dev_err(di->dev, "error reading flags\n");
276                 return ret;
277         }
278         flags = get_unaligned_le16(buf);
279         DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
280         if (flags & BQ27500_FLAG_FC)
281                 status = POWER_SUPPLY_STATUS_FULL;
282         else if (flags & BQ27500_FLAG_DSC)
283                 status = POWER_SUPPLY_STATUS_DISCHARGING;
284         else
285                 status = POWER_SUPPLY_STATUS_CHARGING;
286
287         val->intval = status;
288         return 0;
289 }
290
291 static int bq27510_health_status(struct bq27510_device_info *di,
292                                   union power_supply_propval *val)
293 {
294         u8 buf[2];
295         int flags = 0;
296         int status;
297         int ret;
298         if(virtual_battery_enable == 1)
299         {
300                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
301                 return 0;
302         }
303         ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
304         if (ret < 0) {
305                 dev_err(di->dev, "error reading flags\n");
306                 return ret;
307         }
308         flags = get_unaligned_le16(buf);
309         DBG("Enter:%s %d--status = %x\n",__FUNCTION__,__LINE__,flags);
310         if ((flags & BQ27500_FLAG_OTD)||(flags & BQ27500_FLAG_OTC))
311                 status = POWER_SUPPLY_HEALTH_OVERHEAT;
312         else
313                 status = POWER_SUPPLY_HEALTH_GOOD;
314
315         val->intval = status;
316         return 0;
317 }
318
319
320 /*
321  * Read a time register.
322  * Return < 0 if something fails.
323  */
324 static int bq27510_battery_time(struct bq27510_device_info *di, int reg,
325                                 union power_supply_propval *val)
326 {
327         u8 buf[2];
328         int tval = 0;
329         int ret;
330
331         ret = bq27510_read(di->client,reg,buf,2);
332         if (ret<0) {
333                 dev_err(di->dev, "error reading register %02x\n", reg);
334                 return ret;
335         }
336         tval = get_unaligned_le16(buf);
337         DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
338         if (tval == 65535)
339                 return -ENODATA;
340
341         val->intval = tval * 60;
342         DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
343         return 0;
344 }
345
346 #define to_bq27510_device_info(x) container_of((x), \
347                                 struct bq27510_device_info, bat);
348
349 static int bq27510_battery_get_property(struct power_supply *psy,
350                                         enum power_supply_property psp,
351                                         union power_supply_propval *val)
352 {
353         int ret = 0;
354         struct bq27510_device_info *di = to_bq27510_device_info(psy);
355         DBG("Enter:%s %d psp= %d\n",__FUNCTION__,__LINE__,psp);
356         
357         switch (psp) {
358         
359         case POWER_SUPPLY_PROP_STATUS:
360                 ret = bq27510_battery_status(di, val);
361                 break;
362         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
363         case POWER_SUPPLY_PROP_PRESENT:
364                 val->intval = bq27510_battery_voltage(di);
365                 if (psp == POWER_SUPPLY_PROP_PRESENT)
366                         val->intval = val->intval <= 0 ? 0 : 1;
367                 break;
368         case POWER_SUPPLY_PROP_CURRENT_NOW:
369                 val->intval = bq27510_battery_current(di);
370                 break;
371         case POWER_SUPPLY_PROP_CAPACITY:
372                 val->intval = bq27510_battery_rsoc(di);
373                 break;
374         case POWER_SUPPLY_PROP_TEMP:
375                 val->intval = bq27510_battery_temperature(di);
376                 break;
377         case POWER_SUPPLY_PROP_TECHNOLOGY:
378                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;     
379                 break;
380         case POWER_SUPPLY_PROP_HEALTH:
381                 ret = bq27510_health_status(di, val);
382                 break;
383         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
384                 ret = bq27510_battery_time(di, BQ27x00_REG_TTE, val);
385                 break;
386         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
387                 ret = bq27510_battery_time(di, BQ27x00_REG_TTECP, val);
388                 break;
389         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
390                 ret = bq27510_battery_time(di, BQ27x00_REG_TTF, val);
391                 break;
392         default:
393                 return -EINVAL;
394         }
395
396         return ret;
397 }
398
399 static int rk29_ac_get_property(struct power_supply *psy,
400                         enum power_supply_property psp,
401                         union power_supply_propval *val)
402 {
403         int ret = 0;
404         struct bq27510_device_info *di = container_of(psy, struct bq27510_device_info, ac);
405         
406         switch (psp) {
407         case POWER_SUPPLY_PROP_ONLINE:
408                 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
409                         if(gpio_get_value(di->dc_check_pin))
410                                 val->intval = 0;        /*discharging*/
411                         else
412                                 val->intval = 1;        /*charging*/
413                 }
414                 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
415                 break;
416                 
417         default:
418                 ret = -EINVAL;
419                 break;
420         }
421         return ret;
422 }
423
424 static void bq27510_powersupply_init(struct bq27510_device_info *di)
425 {
426         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
427         di->bat.properties = bq27510_battery_props;
428         di->bat.num_properties = ARRAY_SIZE(bq27510_battery_props);
429         di->bat.get_property = bq27510_battery_get_property;
430         
431         di->ac.name = "ac";
432         di->ac.type = POWER_SUPPLY_TYPE_MAINS;
433         di->ac.properties = rk29_ac_props;
434         di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
435         di->ac.get_property = rk29_ac_get_property;
436 }
437
438
439 static void bq27510_battery_update_status(struct bq27510_device_info *di)
440 {
441         power_supply_changed(&di->bat);
442 }
443
444 static void bq27510_battery_work(struct work_struct *work)
445 {
446         struct bq27510_device_info *di = container_of(work, struct bq27510_device_info, work.work); 
447         bq27510_battery_update_status(di);
448         /* reschedule for the next time */
449         schedule_delayed_work(&di->work, di->interval);
450 }
451
452 static int bq27510_battery_probe(struct i2c_client *client,
453                                  const struct i2c_device_id *id)
454 {
455         struct bq27510_device_info *di;
456         int retval = 0;
457         struct bq27510_platform_data *pdata;
458         
459         #ifdef CONFIG_CHECK_BATT_CAPACITY
460         u8 buf[2];
461         #endif
462
463         pdata = client->dev.platform_data;
464         
465         di = kzalloc(sizeof(*di), GFP_KERNEL);
466         if (!di) {
467                 dev_err(&client->dev, "failed to allocate device info data\n");
468                 retval = -ENOMEM;
469                 goto batt_failed_2;
470         }
471         i2c_set_clientdata(client, di);
472         di->dev = &client->dev;
473         di->bat.name = "bq27510-battery";
474         di->client = client;
475         /* 4 seconds between monotor runs interval */
476         di->interval = msecs_to_jiffies(4 * 1000);
477         
478         di->bat_num = pdata->bat_num;
479         di->dc_check_pin = pdata->dc_check_pin;
480         
481         if (pdata->init_dc_check_pin)
482                 pdata->init_dc_check_pin( );
483         
484         bq27510_powersupply_init(di);
485         #ifdef CONFIG_CHECK_BATT_CAPACITY
486         buf[0] = 0x41;
487         buf[1] = 0x00;
488         bq27510_write(di->client,0x00,buf,2);
489         buf[0] = 0x21;
490         buf[1] = 0x00;
491         bq27510_write(di->client,0x00,buf,2);
492         #endif
493         retval = power_supply_register(&client->dev, &di->bat);
494         if (retval) {
495                 dev_err(&client->dev, "failed to register battery\n");
496                 goto batt_failed_4;
497         }
498         retval = power_supply_register(&client->dev, &di->ac);
499         if (retval) {
500                 dev_err(&client->dev, "failed to register ac\n");
501                 goto batt_failed_4;
502         }
503         
504         INIT_DELAYED_WORK(&di->work, bq27510_battery_work);
505         schedule_delayed_work(&di->work, di->interval);
506         dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
507         return 0;
508
509 batt_failed_4:
510         kfree(di);
511 batt_failed_2:
512         return retval;
513 }
514
515 static int bq27510_battery_remove(struct i2c_client *client)
516 {
517         struct bq27510_device_info *di = i2c_get_clientdata(client);
518
519         power_supply_unregister(&di->bat);
520         kfree(di->bat.name);
521         kfree(di);
522         return 0;
523 }
524
525 static const struct i2c_device_id bq27510_id[] = {
526         { "bq27510", 0 },
527 };
528
529 static struct i2c_driver bq27510_battery_driver = {
530         .driver = {
531                 .name = "bq27510-battery",
532         },
533         .probe = bq27510_battery_probe,
534         .remove = bq27510_battery_remove,
535         .id_table = bq27510_id,
536 };
537
538 static int __init bq27510_battery_init(void)
539 {
540         int ret;
541         struct proc_dir_entry * battery_proc_entry;
542         
543         ret = i2c_add_driver(&bq27510_battery_driver);
544         if (ret)
545                 printk(KERN_ERR "Unable to register BQ27510 driver\n");
546         
547         battery_proc_entry = proc_create("driver/power",0777,NULL,&battery_proc_fops);
548         return ret;
549 }
550 module_init(bq27510_battery_init);
551
552 static void __exit bq27510_battery_exit(void)
553 {
554         i2c_del_driver(&bq27510_battery_driver);
555 }
556 module_exit(bq27510_battery_exit);
557
558 MODULE_AUTHOR("Rockchip");
559 MODULE_DESCRIPTION("BQ27510 battery monitor driver");
560 MODULE_LICENSE("GPL");