power: tps80031-battery: Exit driver if battery is not detected
[linux-2.6.git] / drivers / power / tps80031_battery_gauge.c
1 /*
2  * drivers/power/tps80031_battery_gauge.c
3  *
4  * Gas Gauge driver for TI's tps80031
5  *
6  * Copyright (c) 2011, NVIDIA Corporation.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22 #include <linux/kernel.h>
23 #include <linux/platform_device.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/err.h>
26 #include <linux/regulator/machine.h>
27 #include <linux/mutex.h>
28
29 #include <linux/module.h>
30 #include <linux/param.h>
31 #include <linux/jiffies.h>
32 #include <linux/workqueue.h>
33 #include <linux/pm.h>
34 #include <linux/i2c.h>
35 #include <linux/slab.h>
36 #include <linux/platform_device.h>
37 #include <linux/power_supply.h>
38
39 #include <linux/mfd/core.h>
40 #include <linux/mfd/tps80031.h>
41 #include <linux/tps80031-charger.h>
42
43 #define CONTROLLER_STAT1        0xe3
44 #define LINEAR_CHARGE_STS       0xde
45 #define STS_HW_CONDITIONS       0x21
46 #define TOGGLE1                 0x90
47 #define TOGGLE1_FGS             BIT(5)
48 #define TOGGLE1_GPADCR          BIT(1)
49 #define GPCH0_LSB               0x3b
50 #define GPCH0_MSB               0x3c
51 #define GPCH0_MSB_COLLISION_GP  BIT(4)
52 #define GPSELECT_ISB            0x35
53 #define GPADC_CTRL              0x2e
54 #define MISC1                   0xe4
55 #define CTRL_P1                 0x36
56 #define CTRL_P1_SP1             BIT(3)
57 #define CTRL_P1_EOCRT           BIT(2)
58 #define CTRL_P1_EOCP1           BIT(1)
59 #define CTRL_P1_BUSY            BIT(0)
60 #define FG_REG_00               0xc0
61 #define FG_REG_00_CC_CAL_EN     BIT(1)
62 #define FG_REG_00_CC_AUTOCLEAR  BIT(2)
63 #define FG_REG_01               0xc1    /* CONV_NR (unsigned) 0 - 7 */
64 #define FG_REG_02               0xc2    /* CONV_NR (unsigned) 8 - 15 */
65 #define FG_REG_03               0xc3    /* CONV_NR (unsigned) 16 - 23 */
66 #define FG_REG_04               0xc4    /* ACCM (signed) 0 - 7 */
67 #define FG_REG_05               0xc5    /* ACCM (signed) 8 - 15 */
68 #define FG_REG_06               0xc6    /* ACCM (signed) 16 - 23 */
69 #define FG_REG_07               0xc7    /* ACCM (signed) 24 - 31 */
70 #define FG_REG_08               0xc8    /* OFFSET (signed) 0 - 7 */
71 #define FG_REG_09               0xc9    /* OFFSET (signed) 8 - 9 */
72 #define FG_REG_10               0xca    /* LAST_READ (signed) 0 - 7 */
73 #define FG_REG_11               0xcb    /* LAST_READ (signed) 8 - 13 */
74
75 #define TPS80031_VBUS_DET       BIT(2)
76 #define TPS80031_VAC_DET        BIT(3)
77 #define TPS80031_STS_VYSMIN_HI  BIT(4)
78 #define END_OF_CHARGE           BIT(5)
79
80 #define DRIVER_VERSION          "1.1.0"
81 #define BATTERY_POLL_PERIOD     30000
82
83 static int tps80031_temp_table[] = {
84         /* adc code for temperature in degree C */
85         929, 925, /* -2, -1 */
86         920, 917, 912, 908, 904, 899, 895, 890, 885, 880, /* 00 - 09 */
87         875, 869, 864, 858, 853, 847, 841, 835, 829, 823, /* 10 - 19 */
88         816, 810, 804, 797, 790, 783, 776, 769, 762, 755, /* 20 - 29 */
89         748, 740, 732, 725, 718, 710, 703, 695, 687, 679, /* 30 - 39 */
90         671, 663, 655, 647, 639, 631, 623, 615, 607, 599, /* 40 - 49 */
91         591, 583, 575, 567, 559, 551, 543, 535, 527, 519, /* 50 - 59 */
92         511, 504, 496 /* 60 - 62 */
93 };
94
95 struct tps80031_device_info {
96         struct device           *dev;
97         struct i2c_client       *client;
98         struct power_supply     bat;
99         struct power_supply     usb;
100         struct timer_list       battery_poll_timer;
101         uint32_t vsys;
102         uint8_t usb_online;
103         uint8_t usb_status;
104         uint8_t capacity_sts;
105         uint8_t health;
106         uint8_t sys_vlow_intr;
107         uint8_t fg_calib_intr;
108         int16_t fg_offset;
109         struct mutex adc_lock;
110 };
111
112 static enum power_supply_property tps80031_bat_props[] = {
113         POWER_SUPPLY_PROP_TECHNOLOGY,
114         POWER_SUPPLY_PROP_VOLTAGE_NOW,
115         POWER_SUPPLY_PROP_TEMP,
116         POWER_SUPPLY_PROP_CAPACITY,
117         POWER_SUPPLY_PROP_HEALTH,
118         POWER_SUPPLY_PROP_STATUS,
119         POWER_SUPPLY_PROP_CHARGE_NOW,
120         POWER_SUPPLY_PROP_CHARGE_COUNTER,
121 };
122
123 static enum power_supply_property tps80031_usb_props[] = {
124         POWER_SUPPLY_PROP_ONLINE,
125 };
126
127 static int tps80031_reg_read(struct tps80031_device_info *di, int sid, int reg,
128                                         uint8_t *val)
129 {
130         int ret;
131
132         ret = tps80031_read(di->dev->parent, sid, reg, val);
133         if (ret < 0)
134                 dev_err(di->dev, "Failed read register 0x%02x\n",
135                                         reg);
136         return ret;
137 }
138
139 static int tps80031_reg_write(struct tps80031_device_info *di, int sid, int reg,
140                                         uint8_t val)
141 {
142         int ret;
143
144         ret = tps80031_write(di->dev->parent, sid, reg, val);
145         if (ret < 0)
146                 dev_err(di->dev, "Failed write register 0x%02x\n",
147                                         reg);
148         return ret;
149 }
150
151 static int tps80031_battery_capacity(struct tps80031_device_info *di,
152                         union power_supply_propval *val)
153 {
154         uint8_t hwsts;
155         int ret;
156
157         ret = tps80031_reg_read(di, SLAVE_ID2, LINEAR_CHARGE_STS, &hwsts);
158         if (ret < 0)
159                 return ret;
160
161         di->capacity_sts = di->vsys;
162         if (hwsts & END_OF_CHARGE)
163                 di->capacity_sts = 100;
164
165         if (di->sys_vlow_intr) {
166                 di->capacity_sts = 10;
167                 di->sys_vlow_intr = 0;
168         }
169
170         if (di->capacity_sts <= 10)
171                 di->health = POWER_SUPPLY_HEALTH_DEAD;
172         else
173                 di->health = POWER_SUPPLY_HEALTH_GOOD;
174
175         return  di->capacity_sts;
176 }
177
178 static int tps80031_battery_voltage(struct tps80031_device_info *di,
179                         union power_supply_propval *val)
180 {
181         int voltage;
182
183         voltage = tps80031_gpadc_conversion(SYSTEM_SUPPLY);
184         if (voltage < 0)
185                 return voltage;
186         voltage = ((voltage * 1000) / 4) * 5;
187
188         if (voltage < 3700000)
189                 di->vsys = 10;
190         else if (voltage > 3700000 && voltage <= 3800000)
191                 di->vsys = 20;
192         else if (voltage > 3800000 && voltage <= 3900000)
193                 di->vsys = 50;
194         else if (voltage > 3900000 && voltage <= 4000000)
195                 di->vsys = 75;
196         else if (voltage >= 4000000)
197                 di->vsys = 90;
198
199         return voltage;
200 }
201
202 static int tps80031_battery_charge_now(struct tps80031_device_info *di,
203                         union power_supply_propval *val)
204 {
205         int charge;
206
207         charge = tps80031_gpadc_conversion(BATTERY_CHARGING_CURRENT);
208         if (charge < 0)
209                 return charge;
210         charge = charge * 78125 / 40;
211
212         return charge;
213 }
214
215 static int tps80031_battery_charge_counter(struct tps80031_device_info *di,
216                         union power_supply_propval *val)
217 {
218         int retval, ret;
219         uint32_t cnt_byte;
220         uint32_t acc_byte;
221
222         /* check if calibrated */
223         if (di->fg_calib_intr == 0)
224                 return 0;
225
226         /* get current accumlator */
227         ret = tps80031_reads(di->dev->parent, SLAVE_ID2, FG_REG_04, 4,
228                                                         (uint8_t *) &acc_byte);
229         if (ret < 0)
230                 return ret;
231         /* counter value is mAs, need report uAh */
232         retval = (int32_t) acc_byte / 18 * 5;
233
234         /* get counter */
235         ret = tps80031_reads(di->dev->parent, SLAVE_ID2, FG_REG_01, 3,
236                                                         (uint8_t *) &cnt_byte);
237         if (ret < 0)
238                 return ret;
239         /* we need calibrate the offset current in uAh*/
240         retval = retval - (di->fg_offset / 4 * cnt_byte);
241
242         /* @todo, counter value will overflow if battery get continuously
243          * charged discharged for more than 108Ah using 250mS integration
244          * period althrough it is hightly impossible.
245          */
246
247         return retval;
248 }
249
250 static int tps80031_battery_temp(struct tps80031_device_info *di,
251                                         union power_supply_propval *val)
252 {
253         int adc_code, temp;
254
255         adc_code = tps80031_gpadc_conversion(BATTERY_TEMPERATURE);
256         if (adc_code < 0)
257                 return adc_code;
258
259         for (temp = 0; temp < ARRAY_SIZE(tps80031_temp_table); temp++) {
260                 if (adc_code >= tps80031_temp_table[temp])
261                         break;
262         }
263         /* first 2 values are for negative temperature */
264         val->intval = (temp - 2) * 10; /* in tenths of degree Celsius */
265
266         return  val->intval;
267 }
268
269 #define to_tps80031_device_info_bat(x) container_of((x), \
270                                 struct tps80031_device_info, bat);
271
272 static int tps80031_bat_get_property(struct power_supply *psy,
273         enum power_supply_property psp, union power_supply_propval *val)
274 {
275         struct tps80031_device_info *di = to_tps80031_device_info_bat(psy);
276
277         switch (psp) {
278
279         case POWER_SUPPLY_PROP_TECHNOLOGY:
280                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
281                 break;
282
283         case POWER_SUPPLY_PROP_HEALTH:
284                 val->intval = di->health;
285                 break;
286
287         case POWER_SUPPLY_PROP_CAPACITY:
288                 val->intval =  tps80031_battery_capacity(di, val);
289                 break;
290
291         case POWER_SUPPLY_PROP_CHARGE_NOW:
292                 val->intval =  tps80031_battery_charge_now(di, val);
293                 break;
294
295         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
296                 val->intval =  tps80031_battery_voltage(di, val);
297                 break;
298
299         case POWER_SUPPLY_PROP_CHARGE_COUNTER:
300                 val->intval =  tps80031_battery_charge_counter(di, val);
301                 break;
302
303         case POWER_SUPPLY_PROP_STATUS:
304                 val->intval = di->usb_status;
305                 break;
306
307         case POWER_SUPPLY_PROP_TEMP:
308                 val->intval = tps80031_battery_temp(di, val);
309                 break;
310
311         default:
312                 return -EINVAL;
313         }
314         return 0;
315 }
316
317 #define to_tps80031_device_info_usb(x) container_of((x), \
318                                 struct tps80031_device_info, usb);
319
320 static int tps80031_usb_get_property(struct power_supply *psy,
321                 enum power_supply_property psp, union power_supply_propval *val)
322 {
323         struct tps80031_device_info *di = to_tps80031_device_info_usb(psy);
324
325         switch (psp) {
326         case POWER_SUPPLY_PROP_ONLINE:
327                 val->intval = di->usb_online;
328                 break;
329         default:
330                 return -EINVAL;
331         }
332         return 0;
333 }
334
335 static irqreturn_t tps80031_sys_vlow(int irq, void *data)
336 {
337         struct tps80031_device_info *di = data;
338
339         di->sys_vlow_intr = 1;
340         power_supply_changed(&di->bat);
341         return IRQ_HANDLED;
342 }
343
344 static irqreturn_t tps80031_fg_calibrated(int irq, void *data)
345 {
346         struct tps80031_device_info *di = data;
347         uint8_t acc_byte0;
348         uint8_t acc_byte1;
349         int ret;
350
351         ret = tps80031_reg_read(di, SLAVE_ID2, FG_REG_08, &acc_byte0);
352         if (ret < 0)
353                 return IRQ_HANDLED;
354         ret = tps80031_reg_read(di, SLAVE_ID2, FG_REG_09, &acc_byte1);
355         if (ret < 0)
356                 return IRQ_HANDLED;
357         /* sign extension */
358         if (acc_byte1 & 0x02)
359                 acc_byte1 = acc_byte1 | 0xFC;
360         else
361                 acc_byte1 = acc_byte1 & 0x03;
362
363         di->fg_offset = (int16_t) ((acc_byte1 << 8) | acc_byte0);
364         /* fuel gauge auto calibration finished */
365         di->fg_calib_intr = 1;
366         return IRQ_HANDLED;
367 }
368
369 static int tps80031_fg_start_gas_gauge(struct tps80031_device_info *di)
370 {
371         int ret = 0;
372         di->fg_calib_intr = 0;
373
374         /* start gas gauge */
375         ret = tps80031_reg_write(di, SLAVE_ID2, TOGGLE1, 0x20);
376         if (ret < 0)
377                 return ret;
378         /* set ADC update time to 3.9ms and start calibration */
379         ret = tps80031_reg_write(di, SLAVE_ID2, FG_REG_00, FG_REG_00_CC_CAL_EN);
380         if (ret < 0)
381                 return ret;
382         return ret;
383 }
384
385 void tps80031_battery_status(enum charging_states status, void *data)
386 {
387         struct tps80031_device_info *di = data;
388
389         if ((status == charging_state_charging_in_progress)) {
390                 di->usb_status  = POWER_SUPPLY_STATUS_CHARGING;
391                 di->health      = POWER_SUPPLY_HEALTH_GOOD;
392                 di->usb_online = 1;
393         } else if (status == charging_state_charging_stopped) {
394                 di->usb_status = POWER_SUPPLY_STATUS_DISCHARGING;
395                 di->usb_online = 0;
396         }
397         power_supply_changed(&di->usb);
398         power_supply_changed(&di->bat);
399 }
400
401 static void battery_poll_timer_func(unsigned long pdi)
402 {
403         struct tps80031_device_info *di = (void *)pdi;
404         power_supply_changed(&di->bat);
405         mod_timer(&di->battery_poll_timer,
406                 jiffies + msecs_to_jiffies(BATTERY_POLL_PERIOD));
407 }
408
409 static int tps80031_battery_probe(struct platform_device *pdev)
410 {
411         int ret;
412         uint8_t retval;
413         struct device *dev = &pdev->dev;
414         struct tps80031_device_info *di;
415         struct tps80031_bg_platform_data *pdata = pdev->dev.platform_data;
416
417         di = devm_kzalloc(&pdev->dev, sizeof *di, GFP_KERNEL);
418         if (!di) {
419                 dev_err(dev->parent, "failed to allocate device info data\n");
420                 return -ENOMEM;
421         }
422
423         if (!pdata->battery_present) {
424                 dev_err(dev, "%s() No battery detected, exiting..\n",
425                                 __func__);
426                 return -ENODEV;
427         }
428
429         di->dev =  &pdev->dev;
430
431         ret = tps80031_reg_read(di, SLAVE_ID2, CONTROLLER_STAT1, &retval);
432         if (ret < 0)
433                 return ret;
434
435         if ((retval & TPS80031_VAC_DET) | (retval & TPS80031_VBUS_DET)) {
436                 di->usb_status = POWER_SUPPLY_STATUS_CHARGING;
437                 di->usb_online = 1;
438         } else {
439                 di->usb_status = POWER_SUPPLY_STATUS_DISCHARGING;
440                 di->usb_online = 0;
441         }
442
443         di->capacity_sts = 50;
444         di->health = POWER_SUPPLY_HEALTH_GOOD;
445
446         di->bat.name            = "tps80031-bat";
447         di->bat.type            = POWER_SUPPLY_TYPE_BATTERY;
448         di->bat.properties      = tps80031_bat_props;
449         di->bat.num_properties  = ARRAY_SIZE(tps80031_bat_props);
450         di->bat.get_property    = tps80031_bat_get_property;
451
452         ret = power_supply_register(dev->parent, &di->bat);
453         if (ret) {
454                 dev_err(dev->parent, "failed to register bat power supply\n");
455                 return ret;
456         }
457
458         di->usb.name            = "tps80031-usb";
459         di->usb.type            = POWER_SUPPLY_TYPE_USB;
460         di->usb.properties      = tps80031_usb_props;
461         di->usb.num_properties  = ARRAY_SIZE(tps80031_usb_props);
462         di->usb.get_property    = tps80031_usb_get_property;
463
464         ret = power_supply_register(dev->parent, &di->usb);
465         if (ret) {
466                 dev_err(dev->parent, "failed to register ac power supply\n");
467                 goto power_supply_fail2;
468         }
469
470         dev_set_drvdata(&pdev->dev, di);
471
472         ret = register_charging_state_callback(tps80031_battery_status, di);
473         if (ret < 0)
474                 goto power_supply_fail1;
475
476         ret = request_threaded_irq(pdata->irq_base + TPS80031_INT_SYS_VLOW,
477                                 NULL, tps80031_sys_vlow,
478                                         IRQF_ONESHOT, "tps80031_sys_vlow", di);
479         if (ret < 0) {
480                 dev_err(dev->parent, "request IRQ %d fail\n", pdata->irq_base);
481                 goto power_supply_fail1;
482         }
483
484         ret = request_threaded_irq(pdata->irq_base + TPS80031_INT_CC_AUTOCAL,
485                                 NULL, tps80031_fg_calibrated, IRQF_ONESHOT,
486                                 "tps80031_fuel_gauge_calibration", di);
487         if (ret < 0) {
488                 dev_err(dev->parent, "request IRQ %d fail\n", pdata->irq_base);
489                 goto irq_fail2;
490         }
491         setup_timer(&di->battery_poll_timer,
492                 battery_poll_timer_func, (unsigned long) di);
493         mod_timer(&di->battery_poll_timer,
494                 jiffies + msecs_to_jiffies(BATTERY_POLL_PERIOD));
495
496         ret = tps80031_fg_start_gas_gauge(di);
497         if (ret < 0) {
498                 dev_err(dev->parent, "failed to start fuel-gauge\n");
499                 goto irq_fail1;
500         }
501         dev_info(dev->parent, "support ver. %s enabled\n", DRIVER_VERSION);
502
503         return ret;
504
505 irq_fail1:
506         free_irq(pdata->irq_base + TPS80031_INT_CC_AUTOCAL, di);
507 irq_fail2:
508         free_irq(pdata->irq_base + TPS80031_INT_SYS_VLOW, di);
509 power_supply_fail1:
510         power_supply_unregister(&di->usb);
511 power_supply_fail2:
512         power_supply_unregister(&di->bat);
513         return ret;
514 }
515
516 static int tps80031_battery_remove(struct platform_device *pdev)
517 {
518         struct tps80031_device_info *di = dev_get_drvdata(&pdev->dev);
519
520         power_supply_unregister(&di->bat);
521         power_supply_unregister(&di->usb);
522
523         return 0;
524 }
525
526 static struct platform_driver tps80031_battery_driver = {
527         .driver = {
528                 .name   = "tps80031-battery-gauge",
529                 .owner  = THIS_MODULE,
530         },
531         .probe  = tps80031_battery_probe,
532         .remove = tps80031_battery_remove,
533 };
534
535 static int __init tps80031_battery_init(void)
536 {
537         return platform_driver_register(&tps80031_battery_driver);
538 }
539
540 static void __exit tps80031_battery_exit(void)
541 {
542         platform_driver_unregister(&tps80031_battery_driver);
543 }
544
545 module_init(tps80031_battery_init);
546 module_exit(tps80031_battery_exit);
547
548 MODULE_LICENSE("GPL");
549 MODULE_AUTHOR("Syed Rafiuddin <srafiuddin@nvidia.com> ");
550 MODULE_DESCRIPTION("tps80031 battery gauge driver");