del ac property
[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
26 #define DRIVER_VERSION                  "1.1.0"
27
28 #define BQ27x00_REG_TEMP                0x06
29 #define BQ27x00_REG_VOLT                0x08
30 #define BQ27x00_REG_AI                  0x14
31 #define BQ27x00_REG_FLAGS               0x0A
32 #define BQ27x00_REG_TTE                 0x16
33 #define BQ27x00_REG_TTF                 0x18
34 #define BQ27x00_REG_TTECP               0x26
35
36 #define BQ27000_REG_RSOC                0x0B /* Relative State-of-Charge */
37 #define BQ27000_FLAG_CHGS               BIT(7)
38
39 #define BQ27500_REG_SOC                 0x2c
40 #define BQ27500_FLAG_DSC                BIT(0)
41 #define BQ27500_FLAG_FC                 BIT(9)
42
43 #define BQ27510_SPEED                   300 * 1000
44
45 #define DC_CHECK_PIN                    RK29_PIN4_PA1
46
47 #if 0
48 #define DBG(x...) printk(KERN_INFO x)
49 #else
50 #define DBG(x...) do { } while (0)
51 #endif
52
53 /* If the system has several batteries we need a different name for each
54  * of them...
55  */
56 static DEFINE_MUTEX(battery_mutex);
57
58 struct bq27510_device_info {
59         struct device           *dev;
60         struct power_supply     bat;
61         ///struct power_supply  ac;
62         struct delayed_work work;
63         unsigned int interval;
64         struct i2c_client       *client;
65 };
66
67 static enum power_supply_property bq27510_battery_props[] = {
68         POWER_SUPPLY_PROP_STATUS,
69         POWER_SUPPLY_PROP_PRESENT,
70         POWER_SUPPLY_PROP_VOLTAGE_NOW,
71         POWER_SUPPLY_PROP_CURRENT_NOW,
72         POWER_SUPPLY_PROP_CAPACITY,
73         POWER_SUPPLY_PROP_TEMP,
74         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
75         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
76         //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
77 };
78
79 static enum power_supply_property rk29_ac_props[] = {
80         POWER_SUPPLY_PROP_ONLINE,
81 };
82
83
84 /*
85  * Common code for BQ27510 devices read
86  */
87 static int bq27510_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
88 {
89         int ret; 
90         ret = i2c_master_reg8_recv(client, reg, buf, len, BQ27510_SPEED);
91         return ret; 
92 }
93 #ifdef CONFIG_CHECK_BATT_CAPACITY
94 static int bq27510_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
95 {
96         int ret; 
97         ret = i2c_master_reg8_send(client, reg, buf, (int)len, BQ27510_SPEED);
98         return ret;
99 }
100 #endif
101 /*
102  * Return the battery temperature in tenths of degree Celsius
103  * Or < 0 if something fails.
104  */
105 static int bq27510_battery_temperature(struct bq27510_device_info *di)
106 {
107         int ret;
108         int temp = 0;
109         u8 buf[2];
110         ret = bq27510_read(di->client,BQ27x00_REG_TEMP,buf,2);
111         if (ret<0) {
112                 dev_err(di->dev, "error reading temperature\n");
113                 return ret;
114         }
115         temp = get_unaligned_le16(buf);
116         temp = temp - 2731;
117         DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
118         return temp;
119 }
120
121 /*
122  * Return the battery Voltage in milivolts
123  * Or < 0 if something fails.
124  */
125 static int bq27510_battery_voltage(struct bq27510_device_info *di)
126 {
127         int ret;
128         u8 buf[2];
129         int volt = 0;
130
131         ret = bq27510_read(di->client,BQ27x00_REG_VOLT,buf,2); 
132         if (ret<0) {
133                 dev_err(di->dev, "error reading voltage\n");
134                 return ret;
135         }
136         volt = get_unaligned_le16(buf);
137         volt = volt * 1000;
138         DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
139         return volt;
140 }
141
142 /*
143  * Return the battery average current
144  * Note that current can be negative signed as well
145  * Or 0 if something fails.
146  */
147 static int bq27510_battery_current(struct bq27510_device_info *di)
148 {
149         int ret;
150         int curr = 0;
151         u8 buf[2];
152
153         ret = bq27510_read(di->client,BQ27x00_REG_AI,buf,2);
154         if (ret<0) {
155                 dev_err(di->dev, "error reading current\n");
156                 return 0;
157         }
158
159         curr = get_unaligned_le16(buf);
160         if(curr>0x8000){
161                 curr = 0xFFFF^(curr-1);
162         }
163         curr = curr * 1000;
164         DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
165         return curr;
166 }
167
168 /*
169  * Return the battery Relative State-of-Charge
170  * Or < 0 if something fails.
171  */
172 static int bq27510_battery_rsoc(struct bq27510_device_info *di)
173 {
174         int ret;
175         int rsoc = 0;
176         #if 0
177         int nvcap = 0,facap = 0,remcap=0,fccap=0,full=0,cnt=0;
178         #endif
179         u8 buf[2];
180         
181         ret = bq27510_read(di->client,BQ27500_REG_SOC,buf,2); 
182         if (ret<0) {
183                 dev_err(di->dev, "error reading relative State-of-Charge\n");
184                 return ret;
185         }
186         rsoc = get_unaligned_le16(buf);
187         DBG("Enter:%s %d--rsoc = %d\n",__FUNCTION__,__LINE__,rsoc);
188         #if CONFIG_NO_BATTERY_IC
189         rsoc = 100;
190         #endif
191         #if 0
192         ret = bq27510_read(di->client,0x0c,buf,2);
193         nvcap = get_unaligned_le16(buf);
194         DBG("Enter:%s %d--nvcap = %d\n",__FUNCTION__,__LINE__,nvcap);
195         ret = bq27510_read(di->client,0x0e,buf,2);
196         facap = get_unaligned_le16(buf);
197         DBG("Enter:%s %d--facap = %d\n",__FUNCTION__,__LINE__,facap);
198         ret = bq27510_read(di->client,0x10,buf,2);
199         remcap = get_unaligned_le16(buf);
200         DBG("Enter:%s %d--remcap = %d\n",__FUNCTION__,__LINE__,remcap);
201         ret = bq27510_read(di->client,0x12,buf,2);
202         fccap = get_unaligned_le16(buf);
203         DBG("Enter:%s %d--fccap = %d\n",__FUNCTION__,__LINE__,fccap);
204         ret = bq27510_read(di->client,0x3c,buf,2);
205         full = get_unaligned_le16(buf);
206         DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,full);
207         ret = bq27510_read(di->client,0x00,buf,2);
208         cnt = get_unaligned_le16(buf);
209         DBG("Enter:%s %d--full = %d\n",__FUNCTION__,__LINE__,cnt);
210         #endif
211         return rsoc;
212 }
213
214 static int bq27510_battery_status(struct bq27510_device_info *di,
215                                   union power_supply_propval *val)
216 {
217         u8 buf[2];
218         int flags = 0;
219         int status;
220         int ret;
221
222         ret = bq27510_read(di->client,BQ27x00_REG_FLAGS, buf, 2);
223         if (ret < 0) {
224                 dev_err(di->dev, "error reading flags\n");
225                 return ret;
226         }
227         flags = get_unaligned_le16(buf);
228         if (flags & BQ27500_FLAG_FC)
229                 status = POWER_SUPPLY_STATUS_FULL;
230         else if (flags & BQ27500_FLAG_DSC)
231                 status = POWER_SUPPLY_STATUS_DISCHARGING;
232         else
233                 status = POWER_SUPPLY_STATUS_CHARGING;
234
235         val->intval = status;
236         return 0;
237 }
238
239 /*
240  * Read a time register.
241  * Return < 0 if something fails.
242  */
243 static int bq27510_battery_time(struct bq27510_device_info *di, int reg,
244                                 union power_supply_propval *val)
245 {
246         u8 buf[2];
247         int tval = 0;
248         int ret;
249
250         ret = bq27510_read(di->client,reg,buf,2);
251         if (ret<0) {
252                 dev_err(di->dev, "error reading register %02x\n", reg);
253                 return ret;
254         }
255         tval = get_unaligned_le16(buf);
256         DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
257         if (tval == 65535)
258                 return -ENODATA;
259
260         val->intval = tval * 60;
261         DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
262         return 0;
263 }
264
265 #define to_bq27510_device_info(x) container_of((x), \
266                                 struct bq27510_device_info, bat);
267
268 static int bq27510_battery_get_property(struct power_supply *psy,
269                                         enum power_supply_property psp,
270                                         union power_supply_propval *val)
271 {
272         int ret = 0;
273         struct bq27510_device_info *di = to_bq27510_device_info(psy);
274
275         switch (psp) {
276         case POWER_SUPPLY_PROP_STATUS:
277                 ret = bq27510_battery_status(di, val);
278                 break;
279         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
280         case POWER_SUPPLY_PROP_PRESENT:
281                 val->intval = bq27510_battery_voltage(di);
282                 if (psp == POWER_SUPPLY_PROP_PRESENT)
283                         val->intval = val->intval <= 0 ? 0 : 1;
284                 break;
285         case POWER_SUPPLY_PROP_CURRENT_NOW:
286                 val->intval = bq27510_battery_current(di);
287                 break;
288         case POWER_SUPPLY_PROP_CAPACITY:
289                 val->intval = bq27510_battery_rsoc(di);
290                 break;
291         case POWER_SUPPLY_PROP_TEMP:
292                 val->intval = bq27510_battery_temperature(di);
293                 break;
294         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
295                 ret = bq27510_battery_time(di, BQ27x00_REG_TTE, val);
296                 break;
297         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
298                 ret = bq27510_battery_time(di, BQ27x00_REG_TTECP, val);
299                 break;
300         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
301                 ret = bq27510_battery_time(di, BQ27x00_REG_TTF, val);
302                 break;
303         default:
304                 return -EINVAL;
305         }
306
307         return ret;
308 }
309
310 static int rk29_ac_get_property(struct power_supply *psy,
311                         enum power_supply_property psp,
312                         union power_supply_propval *val)
313 {
314         int ret = 0;
315         switch (psp) {
316         case POWER_SUPPLY_PROP_ONLINE:
317                 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
318                         if(gpio_get_value(DC_CHECK_PIN))
319                                 val->intval = 0;
320                         else
321                                 val->intval = 1;        
322                 }
323                 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
324                 break;
325                 
326         default:
327                 ret = -EINVAL;
328                 break;
329         }
330         return ret;
331 }
332
333 static void bq27510_powersupply_init(struct bq27510_device_info *di)
334 {
335         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
336         di->bat.properties = bq27510_battery_props;
337         di->bat.num_properties = ARRAY_SIZE(bq27510_battery_props);
338         di->bat.get_property = bq27510_battery_get_property;
339         
340         /*di->ac.type = POWER_SUPPLY_TYPE_MAINS;
341         di->ac.properties = rk29_ac_props;
342         di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
343         di->ac.get_property = rk29_ac_get_property;*/
344 }
345
346
347 static void bq27510_battery_update_status(struct bq27510_device_info *di)
348 {
349         power_supply_changed(&di->bat);
350 }
351
352 static void bq27510_battery_work(struct work_struct *work)
353 {
354         struct bq27510_device_info *di = container_of(work, struct bq27510_device_info, work.work); 
355         bq27510_battery_update_status(di);
356         /* reschedule for the next time */
357         schedule_delayed_work(&di->work, di->interval);
358 }
359
360 static int bq27510_battery_probe(struct i2c_client *client,
361                                  const struct i2c_device_id *id)
362 {
363         struct bq27510_device_info *di;
364         int retval = 0;
365         
366         #ifdef CONFIG_CHECK_BATT_CAPACITY
367         u8 buf[2];
368         #endif
369          
370         di = kzalloc(sizeof(*di), GFP_KERNEL);
371         if (!di) {
372                 dev_err(&client->dev, "failed to allocate device info data\n");
373                 retval = -ENOMEM;
374                 goto batt_failed_2;
375         }
376         i2c_set_clientdata(client, di);
377         di->dev = &client->dev;
378         di->bat.name = "bq27510-battery";
379         di->client = client;
380         /* 4 seconds between monotor runs interval */
381         di->interval = msecs_to_jiffies(4 * 1000);
382         
383         gpio_request(DC_CHECK_PIN,"dc_check");
384         gpio_direction_input(DC_CHECK_PIN);
385         bq27510_powersupply_init(di);
386         #ifdef CONFIG_CHECK_BATT_CAPACITY
387         buf[0] = 0x41;
388         buf[1] = 0x00;
389         bq27510_write(di->client,0x00,buf,2);
390         buf[0] = 0x21;
391         buf[1] = 0x00;
392         bq27510_write(di->client,0x00,buf,2);
393         #endif
394         retval = power_supply_register(&client->dev, &di->bat);
395         if (retval) {
396                 dev_err(&client->dev, "failed to register battery\n");
397                 goto batt_failed_4;
398         }
399         INIT_DELAYED_WORK(&di->work, bq27510_battery_work);
400         schedule_delayed_work(&di->work, di->interval);
401         dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
402
403         return 0;
404
405 batt_failed_4:
406         kfree(di);
407 batt_failed_2:
408         return retval;
409 }
410
411 static int bq27510_battery_remove(struct i2c_client *client)
412 {
413         struct bq27510_device_info *di = i2c_get_clientdata(client);
414
415         power_supply_unregister(&di->bat);
416         kfree(di->bat.name);
417         kfree(di);
418         return 0;
419 }
420
421 /*
422  * Module stuff
423  */
424
425 static const struct i2c_device_id bq27510_id[] = {
426         { "bq27510", 0 },
427 };
428
429 static struct i2c_driver bq27510_battery_driver = {
430         .driver = {
431                 .name = "bq27510-battery",
432         },
433         .probe = bq27510_battery_probe,
434         .remove = bq27510_battery_remove,
435         .id_table = bq27510_id,
436 };
437
438 static int __init bq27510_battery_init(void)
439 {
440         int ret;
441
442         ret = i2c_add_driver(&bq27510_battery_driver);
443         if (ret)
444                 printk(KERN_ERR "Unable to register BQ27510 driver\n");
445         return ret;
446 }
447 module_init(bq27510_battery_init);
448
449 static void __exit bq27510_battery_exit(void)
450 {
451         i2c_del_driver(&bq27510_battery_driver);
452 }
453 module_exit(bq27510_battery_exit);
454
455 MODULE_AUTHOR("Rockchip");
456 MODULE_DESCRIPTION("BQ27510 battery monitor driver");
457 MODULE_LICENSE("GPL");