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