UPSTREAM: PCI: rockchip: remove the pointer to L1 substate cap
[firefly-linux-kernel-4.4.55.git] / drivers / power / bq3060_battery.c
1 /*
2  * bq3060 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 BQ3060_REG_TEMP         0x08
29 #define BQ3060_REG_VOLT         0x09
30 #define BQ3060_REG_AI                   0x0a   ///0x14
31 #define BQ3060_REG_STATUS               0x16
32 #define BQ3060_REG_TTE                  0x06 ///0x16
33 #define BQ3060_REG_TTF                  0x05 //0x18
34 #define BQ3060_REG_TTECP                0x12 //0x26
35 #define BQ3060_REG_DESIGNCAPACITY       0x18 //0x26
36
37 #define BQ3060_REG_RSOC         0x0B /* Relative State-of-Charge */
38 #define BQ3060_FLAG_CHGS                BIT(7)
39
40 #define BQ3060_REG_CAPACITY     0x0f  ///0x0E
41 #define BQ3060_FLAG_DSC         BIT(0)
42 #define BQ3060_FLAG_FC                  BIT(9)
43
44 #define bq3060_SPEED                    200 * 1000
45
46 #define DC_CHECK_PIN                    RK29_PIN4_PA1
47
48 /* manufacturer access defines */
49 #define MANUFACTURER_ACCESS_STATUS 0x0006
50 #define MANUFACTURER_ACCESS_SLEEP 0x0011
51
52 /* battery status value bits */
53 #define BATTERY_DISCHARGING             0x40
54 #define BATTERY_FULL_CHARGED            0x20
55 #define BATTERY_FULL_DISCHARGED         0x10
56
57
58 #if 0
59 #define DBG(x...) printk(KERN_INFO x)
60 #else
61 #define DBG(x...) do { } while (0)
62 #endif
63
64 /* If the system has several batteries we need a different name for each
65  * of them...
66  */
67 static DEFINE_MUTEX(battery_mutex);
68
69 struct bq3060_device_info {
70         struct device           *dev;
71         struct power_supply     bat;
72         struct power_supply     ac;
73         struct power_supply     usb;
74         struct delayed_work work;
75         unsigned int interval;
76         struct i2c_client       *client;
77 };
78
79 static enum power_supply_property bq3060_battery_props[] = {
80         POWER_SUPPLY_PROP_STATUS,
81         POWER_SUPPLY_PROP_PRESENT,
82         POWER_SUPPLY_PROP_VOLTAGE_NOW,
83         POWER_SUPPLY_PROP_CURRENT_NOW,
84         POWER_SUPPLY_PROP_CAPACITY,
85         POWER_SUPPLY_PROP_TEMP,
86         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
87         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
88         //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
89 };
90
91 static enum power_supply_property rk29_ac_props[] = {
92         POWER_SUPPLY_PROP_ONLINE,
93 };
94
95
96 /*
97  * Common code for bq3060 devices read
98  */
99 static int bq3060_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
100 {
101         struct i2c_adapter *adap=client->adapter;
102         struct i2c_msg msgs[2];
103         int ret;
104         char reg_buf = reg;
105         
106         msgs[0].addr = client->addr;
107         msgs[0].flags = client->flags;
108         msgs[0].len = 1;
109         msgs[0].buf = &reg_buf;
110         msgs[0].scl_rate = bq3060_SPEED;
111
112         msgs[1].addr = client->addr;
113         msgs[1].flags = client->flags | I2C_M_RD;
114         msgs[1].len = len;
115         msgs[1].buf = (char *)buf;
116         msgs[1].scl_rate = bq3060_SPEED;
117
118         ret = i2c_transfer(adap, msgs, 2);
119
120         return (ret == 2)? len : ret;
121 }
122 static int bq3060_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
123 {
124         int ret; 
125         ///return 0;
126         ret = i2c_master_reg8_send(client, reg, buf, (int)len, bq3060_SPEED);
127         return ret;
128 }
129 /*
130  * Return the battery temperature in tenths of degree Celsius
131  * Or < 0 if something fails.
132  */
133 static int bq3060_battery_temperature(struct bq3060_device_info *di)
134 {
135         int ret;
136         int temp = 0;
137         u8 buf[2];
138         ret = bq3060_read(di->client,BQ3060_REG_TEMP,buf,2);
139         if (ret<0) {
140                 dev_err(di->dev, "error reading temperature\n");
141                 return ret;
142         }
143         temp = get_unaligned_le16(buf);
144         temp = temp - 2731;
145         //#if CONFIG_NO_BATTERY_IC
146         temp = 258;
147         //#endif
148         DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
149         return temp;
150 }
151
152 /*
153  * Return the battery Voltage in milivolts
154  * Or < 0 if something fails.
155  */
156 static int bq3060_battery_voltage(struct bq3060_device_info *di)
157 {
158         int ret;
159         u8 buf[2];
160         int volt = 0;
161
162         ret = bq3060_read(di->client,BQ3060_REG_VOLT,buf,2); 
163         if (ret<0) {
164                 dev_err(di->dev, "error reading voltage\n");
165                 return ret;
166         }
167         volt = get_unaligned_le16(buf);
168         volt = volt;
169         DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
170         return volt;
171 }
172
173 /*
174  * Return the battery average current
175  * Note that current can be negative signed as well
176  * Or 0 if something fails.
177  */
178 static int bq3060_battery_current(struct bq3060_device_info *di)
179 {
180         int ret;
181         int curr = 0;
182         u8 buf[2];
183
184         ret = bq3060_read(di->client,BQ3060_REG_AI,buf,2);
185         if (ret<0) {
186                 dev_err(di->dev, "error reading current\n");
187                 return 0;
188         }
189
190         curr = get_unaligned_le16(buf);
191         if(curr>0x8000){
192                 curr = 0xFFFF^(curr-1);
193         }
194         curr = curr * 1000;
195         DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
196         return curr;
197 }
198
199 /*
200  * Return the battery Relative State-of-Charge
201  * Or < 0 if something fails.
202  */
203 static int bq3060_battery_capacity(struct bq3060_device_info *di)
204 {
205         int ret;
206         int rsoc = 0;
207         #if 1
208         int designcapacity=0;
209         #endif
210         u8 buf[2];
211         
212         ret = bq3060_read(di->client,BQ3060_REG_CAPACITY,buf,2); 
213         if (ret<0) {
214                 dev_err(di->dev, "error reading relative State-of-Charge\n");
215                 return ret;
216         }
217         rsoc = get_unaligned_le16(buf);
218         DBG("Enter:%s %d--capacity = %d\n",__FUNCTION__,__LINE__,rsoc);
219         #if CONFIG_NO_BATTERY_IC
220         rsoc = 100;
221         #endif
222         #if 1
223         ret = bq3060_read(di->client,BQ3060_REG_DESIGNCAPACITY,buf,2);
224         designcapacity = get_unaligned_le16(buf);
225         DBG("Enter:%s %d--designcapacity = %d\n",__FUNCTION__,__LINE__,designcapacity);
226         #endif
227         
228         if((rsoc<150)|(designcapacity<=200))
229                 return 0;
230         rsoc = ((rsoc - 100)*100) / (designcapacity -200);
231         if(rsoc>100)
232                 rsoc = 100;
233         DBG("Enter:%s %d--capacity = %d\n",__FUNCTION__,__LINE__,rsoc); 
234         return rsoc;
235 }
236
237 static int bq3060_battery_status(struct bq3060_device_info *di,
238                                   union power_supply_propval *val)
239 {
240         u8 buf[2];
241         int flags = 0;
242         int status;
243         int ret;
244
245         ret = bq3060_read(di->client,BQ3060_REG_STATUS, buf, 2);
246         if (ret < 0) {
247                 dev_err(di->dev, "error reading flags\n");
248                 return ret;
249         }
250         flags = get_unaligned_le16(buf);
251         DBG("Enter:%s %d--flags = %x\n",__FUNCTION__,__LINE__,flags);
252         if (flags & 0x20 )
253                 status = POWER_SUPPLY_STATUS_FULL;
254         else if (flags & 0x40 )
255                 status = POWER_SUPPLY_STATUS_DISCHARGING;
256         else
257                 status = POWER_SUPPLY_STATUS_CHARGING;
258
259         val->intval = status;
260         return status;
261 }
262
263 /*
264  * Read a time register.
265  * Return < 0 if something fails.
266  */
267 static int bq3060_battery_time(struct bq3060_device_info *di, int reg,
268                                 union power_supply_propval *val)
269 {
270         u8 buf[2];
271         int tval = 0;
272         int ret;
273
274         ret = bq3060_read(di->client,reg,buf,2);
275         if (ret<0) {
276                 dev_err(di->dev, "error reading register %02x\n", reg);
277                 return ret;
278         }
279         tval = get_unaligned_le16(buf);
280         DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
281         if (tval == 65535)
282                 return -ENODATA;
283
284         val->intval = tval * 60;
285         DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
286         return 0;
287 }
288
289 #define to_bq3060_device_info(x) container_of((x), \
290                                 struct bq3060_device_info, bat);
291
292 static int bq3060_battery_get_property(struct power_supply *psy,
293                                         enum power_supply_property psp,
294                                         union power_supply_propval *val)
295 {
296         int ret = 0;
297         struct bq3060_device_info *di = to_bq3060_device_info(psy);
298
299         switch (psp) {
300         case POWER_SUPPLY_PROP_ONLINE:
301                 val->intval = 1;
302                 break;
303         case POWER_SUPPLY_PROP_STATUS:
304                 val->intval = bq3060_battery_status(di, val);
305                 if(val->intval < 0)
306                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
307                 break;
308         case POWER_SUPPLY_PROP_HEALTH:          
309                 val->intval = POWER_SUPPLY_HEALTH_GOOD;         
310                 break;  
311         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
312         case POWER_SUPPLY_PROP_PRESENT:
313                 val->intval =1;// bq3060_battery_voltage(di);
314         
315                 break;
316         case POWER_SUPPLY_PROP_CURRENT_NOW:
317                 val->intval = bq3060_battery_current(di);
318                 break;
319         case POWER_SUPPLY_PROP_CAPACITY:
320                 val->intval = bq3060_battery_capacity(di);
321                 break;
322         case POWER_SUPPLY_PROP_TEMP:
323                 val->intval = bq3060_battery_temperature(di);
324                 break;
325         case POWER_SUPPLY_PROP_TECHNOLOGY:              
326                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;     
327                 break;          
328         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
329                 ret = bq3060_battery_time(di, BQ3060_REG_TTE, val);
330                 break;
331         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
332                 ret = bq3060_battery_time(di, BQ3060_REG_TTECP, val);
333                 break;
334         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
335                 ret = bq3060_battery_time(di, BQ3060_REG_TTF, val);
336                 break;
337         default:
338                 return -EINVAL;
339         }
340
341         return 0;
342 }
343
344 static int rk29_ac_get_property(struct power_supply *psy,
345                         enum power_supply_property psp,
346                         union power_supply_propval *val)
347 {
348         int ret = 0;
349         switch (psp) {
350         case POWER_SUPPLY_PROP_ONLINE:
351                 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
352                         if(gpio_get_value(DC_CHECK_PIN))
353                                 val->intval = 0;
354                         else
355                                 val->intval = 1;        
356                 }
357                 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
358                 break;
359                 
360         default:
361                 ret = -EINVAL;
362                 break;
363         }
364         return ret;
365 }
366
367 static void bq3060_powersupply_init(struct bq3060_device_info *di)
368 {
369         di->bat.name = "battery";
370         di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
371         di->bat.properties = bq3060_battery_props;
372         di->bat.num_properties = ARRAY_SIZE(bq3060_battery_props);
373         di->bat.get_property = bq3060_battery_get_property;
374
375         di->ac.name = "ac";
376         di->ac.type = POWER_SUPPLY_TYPE_MAINS;
377         di->ac.properties = rk29_ac_props;
378         di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
379         di->ac.get_property = rk29_ac_get_property;
380
381         di->usb.name = "usb";
382         di->usb.type = POWER_SUPPLY_TYPE_USB;
383         di->usb.properties = rk29_ac_props;
384         di->usb.num_properties = ARRAY_SIZE(rk29_ac_props);
385         di->usb.get_property = bq3060_battery_get_property;
386 }
387
388
389 static void bq3060_battery_update_status(struct bq3060_device_info *di)
390 {
391         power_supply_changed(&di->bat);
392 }
393
394 static void bq3060_battery_work(struct work_struct *work)
395 {
396         struct bq3060_device_info *di = container_of(work, struct bq3060_device_info, work.work); 
397         bq3060_battery_update_status(di);
398         /* reschedule for the next time */
399         schedule_delayed_work(&di->work, di->interval);
400 }
401
402 static int bq3060_battery_probe(struct i2c_client *client,
403                                  const struct i2c_device_id *id)
404 {
405         struct bq3060_device_info *di;
406         int retval = 0;
407         
408         u8 buf[2];
409          
410         di = kzalloc(sizeof(*di), GFP_KERNEL);
411         if (!di) {
412                 dev_err(&client->dev, "failed to allocate device info data\n");
413                 retval = -ENOMEM;
414                 goto batt_failed_2;
415         }
416         i2c_set_clientdata(client, di);
417         di->dev = &client->dev;
418         di->bat.name = "bq3060-battery";
419         di->client = client;
420         /* 4 seconds between monotor runs interval */
421         di->interval = msecs_to_jiffies(1 * 1000);
422         
423         gpio_request(DC_CHECK_PIN,"dc_check");
424         gpio_direction_input(DC_CHECK_PIN);
425         bq3060_powersupply_init(di);
426         buf[0] = 0x41;
427         buf[1] = 0x00;
428         bq3060_write(di->client,0x00,buf,2);
429         buf[0] = 0x21;
430         buf[1] = 0x00;
431         bq3060_write(di->client,0x00,buf,2);
432         retval = power_supply_register(&client->dev, &di->bat);
433         if (retval) {
434                 dev_err(&client->dev, "failed to register battery\n");
435                 goto batt_failed_4;
436         }
437         //retval = power_supply_register(&client->dev, &di->usb);
438         if (retval) {
439                 dev_err(&client->dev, "failed to register usb battery\n");
440                 goto batt_failed_4;
441         }
442         retval = power_supply_register(&client->dev, &di->ac);
443         if (retval) {
444                 dev_err(&client->dev, "failed to register ac adapter\n");
445                 goto batt_failed_4;
446         }
447         INIT_DELAYED_WORK(&di->work, bq3060_battery_work);
448         schedule_delayed_work(&di->work, di->interval);
449         dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
450
451         return 0;
452
453 batt_failed_4:
454         kfree(di);
455 batt_failed_2:
456         return retval;
457 }
458
459 static int bq3060_battery_remove(struct i2c_client *client)
460 {
461         struct bq3060_device_info *di = i2c_get_clientdata(client);
462
463         power_supply_unregister(&di->bat);
464         kfree(di->bat.name);
465         kfree(di);
466         return 0;
467 }
468
469 /*
470  * Module stuff
471  */
472
473 static const struct i2c_device_id bq3060_id[] = {
474         { "bq3060", 0 },
475 };
476
477 static struct i2c_driver bq3060_battery_driver = {
478         .driver = {
479                 .name = "bq3060-battery",
480         },
481         .probe = bq3060_battery_probe,
482         .remove = bq3060_battery_remove,
483         .id_table = bq3060_id,
484 };
485
486 static int __init bq3060_battery_init(void)
487 {
488         int ret;
489
490         ret = i2c_add_driver(&bq3060_battery_driver);
491         if (ret)
492                 printk(KERN_ERR "Unable to register bq3060 driver\n");
493         return ret;
494 }
495 module_init(bq3060_battery_init);
496
497 static void __exit bq3060_battery_exit(void)
498 {
499         i2c_del_driver(&bq3060_battery_driver);
500 }
501 module_exit(bq3060_battery_exit);
502
503 MODULE_AUTHOR("Rockchip");
504 MODULE_DESCRIPTION("bq3060 battery monitor driver");
505 MODULE_LICENSE("GPL");