CHROMIUM: sbs-battery: probe should try talking to the device
[cascardo/linux.git] / drivers / power / sbs-battery.c
1 /*
2  * Gas Gauge driver for SBS Compliant Batteries
3  *
4  * Copyright (c) 2010, NVIDIA Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/err.h>
25 #include <linux/power_supply.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/gpio.h>
30
31 #include <linux/power/sbs-battery.h>
32
33 enum {
34         REG_MANUFACTURER_DATA,
35         REG_TEMPERATURE,
36         REG_VOLTAGE,
37         REG_CURRENT,
38         REG_CAPACITY,
39         REG_TIME_TO_EMPTY,
40         REG_TIME_TO_FULL,
41         REG_STATUS,
42         REG_CYCLE_COUNT,
43         REG_SERIAL_NUMBER,
44         REG_REMAINING_CAPACITY,
45         REG_REMAINING_CAPACITY_CHARGE,
46         REG_FULL_CHARGE_CAPACITY,
47         REG_FULL_CHARGE_CAPACITY_CHARGE,
48         REG_DESIGN_CAPACITY,
49         REG_DESIGN_CAPACITY_CHARGE,
50         REG_DESIGN_VOLTAGE,
51         REG_MANUFACTURER,
52         REG_MODEL_NAME,
53 };
54
55 /* Battery Mode defines */
56 #define BATTERY_MODE_OFFSET             0x03
57 #define BATTERY_MODE_MASK               0x8000
58 enum sbs_battery_mode {
59         BATTERY_MODE_AMPS,
60         BATTERY_MODE_WATTS
61 };
62
63 /* manufacturer access defines */
64 #define MANUFACTURER_ACCESS_STATUS      0x0006
65 #define MANUFACTURER_ACCESS_SLEEP       0x0011
66
67 /* battery status value bits */
68 #define BATTERY_DISCHARGING             0x40
69 #define BATTERY_FULL_CHARGED            0x20
70 #define BATTERY_FULL_DISCHARGED         0x10
71
72 /* min_value and max_value are only valid for numerical data */
73 #define SBS_DATA(_psp, _addr, _min_value, _max_value) { \
74         .psp = _psp, \
75         .addr = _addr, \
76         .min_value = _min_value, \
77         .max_value = _max_value, \
78 }
79
80 static const struct chip_data {
81         enum power_supply_property psp;
82         u8 addr;
83         int min_value;
84         int max_value;
85 } sbs_data[] = {
86         [REG_MANUFACTURER_DATA] =
87                 SBS_DATA(POWER_SUPPLY_PROP_PRESENT, 0x00, 0, 65535),
88         [REG_TEMPERATURE] =
89                 SBS_DATA(POWER_SUPPLY_PROP_TEMP, 0x08, 0, 65535),
90         [REG_VOLTAGE] =
91                 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_NOW, 0x09, 0, 20000),
92         [REG_CURRENT] =
93                 SBS_DATA(POWER_SUPPLY_PROP_CURRENT_NOW, 0x0A, -32768, 32767),
94         [REG_CAPACITY] =
95                 SBS_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0E, 0, 100),
96         [REG_REMAINING_CAPACITY] =
97                 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
98         [REG_REMAINING_CAPACITY_CHARGE] =
99                 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
100         [REG_FULL_CHARGE_CAPACITY] =
101                 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
102         [REG_FULL_CHARGE_CAPACITY_CHARGE] =
103                 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
104         [REG_TIME_TO_EMPTY] =
105                 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0, 65535),
106         [REG_TIME_TO_FULL] =
107                 SBS_DATA(POWER_SUPPLY_PROP_TIME_TO_FULL_AVG, 0x13, 0, 65535),
108         [REG_STATUS] =
109                 SBS_DATA(POWER_SUPPLY_PROP_STATUS, 0x16, 0, 65535),
110         [REG_CYCLE_COUNT] =
111                 SBS_DATA(POWER_SUPPLY_PROP_CYCLE_COUNT, 0x17, 0, 65535),
112         [REG_DESIGN_CAPACITY] =
113                 SBS_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0, 65535),
114         [REG_DESIGN_CAPACITY_CHARGE] =
115                 SBS_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0, 65535),
116         [REG_DESIGN_VOLTAGE] =
117                 SBS_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0, 65535),
118         [REG_SERIAL_NUMBER] =
119                 SBS_DATA(POWER_SUPPLY_PROP_SERIAL_NUMBER, 0x1C, 0, 65535),
120         /* Properties of type `const char *' */
121         [REG_MANUFACTURER] =
122                 SBS_DATA(POWER_SUPPLY_PROP_MANUFACTURER, 0x20, 0, 65535),
123         [REG_MODEL_NAME] =
124                 SBS_DATA(POWER_SUPPLY_PROP_MODEL_NAME, 0x21, 0, 65535)
125 };
126
127 static enum power_supply_property sbs_properties[] = {
128         POWER_SUPPLY_PROP_STATUS,
129         POWER_SUPPLY_PROP_HEALTH,
130         POWER_SUPPLY_PROP_PRESENT,
131         POWER_SUPPLY_PROP_TECHNOLOGY,
132         POWER_SUPPLY_PROP_CYCLE_COUNT,
133         POWER_SUPPLY_PROP_VOLTAGE_NOW,
134         POWER_SUPPLY_PROP_CURRENT_NOW,
135         POWER_SUPPLY_PROP_CAPACITY,
136         POWER_SUPPLY_PROP_TEMP,
137         POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
138         POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
139         POWER_SUPPLY_PROP_SERIAL_NUMBER,
140         POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
141         POWER_SUPPLY_PROP_ENERGY_NOW,
142         POWER_SUPPLY_PROP_ENERGY_FULL,
143         POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
144         POWER_SUPPLY_PROP_CHARGE_NOW,
145         POWER_SUPPLY_PROP_CHARGE_FULL,
146         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
147         /* Properties of type `const char *' */
148         POWER_SUPPLY_PROP_MANUFACTURER,
149         POWER_SUPPLY_PROP_MODEL_NAME
150 };
151
152 struct sbs_info {
153         struct i2c_client               *client;
154         struct power_supply             power_supply;
155         struct sbs_platform_data        *pdata;
156         bool                            is_present;
157         bool                            gpio_detect;
158         bool                            enable_detection;
159         int                             irq;
160         int                             last_state;
161         int                             poll_time;
162         struct delayed_work             work;
163         int                             ignore_changes;
164 };
165
166 static char model_name[I2C_SMBUS_BLOCK_MAX + 1];
167 static char manufacturer[I2C_SMBUS_BLOCK_MAX + 1];
168
169 static int sbs_read_word_data(struct i2c_client *client, u8 address)
170 {
171         struct sbs_info *chip = i2c_get_clientdata(client);
172         s32 ret = 0;
173         int retries = 1;
174
175         if (chip->pdata)
176                 retries = max(chip->pdata->i2c_retry_count + 1, 1);
177
178         while (retries > 0) {
179                 ret = i2c_smbus_read_word_data(client, address);
180                 if (ret >= 0)
181                         break;
182                 retries--;
183         }
184
185         if (ret < 0) {
186                 dev_dbg(&client->dev,
187                         "%s: i2c read at address 0x%x failed\n",
188                         __func__, address);
189                 return ret;
190         }
191
192         return le16_to_cpu(ret);
193 }
194
195 static int sbs_read_string_data(struct i2c_client *client, u8 address,
196                                 char *values)
197 {
198         struct sbs_info *chip = i2c_get_clientdata(client);
199         s32 ret = 0, block_length = 0;
200         int retries_length = 1, retries_block = 1;
201         u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
202
203         if (chip->pdata) {
204                 retries_length = max(chip->pdata->i2c_retry_count + 1, 1);
205                 retries_block = max(chip->pdata->i2c_retry_count + 1, 1);
206         }
207
208         /* Adapter needs to support these two functions */
209         if (!i2c_check_functionality(client->adapter,
210                                      I2C_FUNC_SMBUS_BYTE_DATA |
211                                      I2C_FUNC_SMBUS_I2C_BLOCK)){
212                 return -ENODEV;
213         }
214
215         /* Get the length of block data */
216         while (retries_length > 0) {
217                 ret = i2c_smbus_read_byte_data(client, address);
218                 if (ret >= 0)
219                         break;
220                 retries_length--;
221         }
222
223         if (ret < 0) {
224                 dev_dbg(&client->dev,
225                         "%s: i2c read at address 0x%x failed\n",
226                         __func__, address);
227                 return ret;
228         }
229
230         /* block_length does not include NULL terminator */
231         block_length = ret;
232         if (block_length > I2C_SMBUS_BLOCK_MAX) {
233                 dev_err("%s: Returned block_length is longer than 0x%x\n",
234                         __func__, I2C_SMBUS_BLOCK_MAX);
235                 return -EINVAL;
236         }
237
238         /* Get the block data */
239         while (retries_block > 0) {
240                 ret = i2c_smbus_read_i2c_block_data(
241                                 client, address,
242                                 block_length + 1, block_buffer);
243                 if (ret >= 0)
244                         break;
245                 retries_block--;
246         }
247
248         if (ret < 0) {
249                 dev_dbg(&client->dev,
250                         "%s: i2c read at address 0x%x failed\n",
251                         __func__, address);
252                 return ret;
253         }
254
255         /* block_buffer[0] == block_length */
256         memcpy(values, block_buffer + 1, block_length);
257         values[block_length] = '\0';
258
259         return le16_to_cpu(ret);
260 }
261
262 static int sbs_write_word_data(struct i2c_client *client, u8 address,
263         u16 value)
264 {
265         struct sbs_info *chip = i2c_get_clientdata(client);
266         s32 ret = 0;
267         int retries = 1;
268
269         if (chip->pdata)
270                 retries = max(chip->pdata->i2c_retry_count + 1, 1);
271
272         while (retries > 0) {
273                 ret = i2c_smbus_write_word_data(client, address,
274                         le16_to_cpu(value));
275                 if (ret >= 0)
276                         break;
277                 retries--;
278         }
279
280         if (ret < 0) {
281                 dev_dbg(&client->dev,
282                         "%s: i2c write to address 0x%x failed\n",
283                         __func__, address);
284                 return ret;
285         }
286
287         return 0;
288 }
289
290 static int sbs_get_battery_presence_and_health(
291         struct i2c_client *client, enum power_supply_property psp,
292         union power_supply_propval *val)
293 {
294         s32 ret;
295         struct sbs_info *chip = i2c_get_clientdata(client);
296
297         if (psp == POWER_SUPPLY_PROP_PRESENT &&
298                 chip->gpio_detect) {
299                 ret = gpio_get_value(chip->pdata->battery_detect);
300                 if (ret == chip->pdata->battery_detect_present)
301                         val->intval = 1;
302                 else
303                         val->intval = 0;
304                 chip->is_present = val->intval;
305                 return ret;
306         }
307
308         /* Write to ManufacturerAccess with
309          * ManufacturerAccess command and then
310          * read the status */
311         ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
312                                         MANUFACTURER_ACCESS_STATUS);
313         if (ret < 0) {
314                 if (psp == POWER_SUPPLY_PROP_PRESENT)
315                         val->intval = 0; /* battery removed */
316                 return ret;
317         }
318
319         ret = sbs_read_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr);
320         if (ret < 0)
321                 return ret;
322
323         if (ret < sbs_data[REG_MANUFACTURER_DATA].min_value ||
324             ret > sbs_data[REG_MANUFACTURER_DATA].max_value) {
325                 val->intval = 0;
326                 return 0;
327         }
328
329         /* Mask the upper nibble of 2nd byte and
330          * lower byte of response then
331          * shift the result by 8 to get status*/
332         ret &= 0x0F00;
333         ret >>= 8;
334         if (psp == POWER_SUPPLY_PROP_PRESENT) {
335                 if (ret == 0x0F)
336                         /* battery removed */
337                         val->intval = 0;
338                 else
339                         val->intval = 1;
340         } else if (psp == POWER_SUPPLY_PROP_HEALTH) {
341                 if (ret == 0x09)
342                         val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
343                 else if (ret == 0x0B)
344                         val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
345                 else if (ret == 0x0C)
346                         val->intval = POWER_SUPPLY_HEALTH_DEAD;
347                 else
348                         val->intval = POWER_SUPPLY_HEALTH_GOOD;
349         }
350
351         return 0;
352 }
353
354 static int sbs_get_battery_property(struct i2c_client *client,
355         int reg_offset, enum power_supply_property psp,
356         union power_supply_propval *val)
357 {
358         struct sbs_info *chip = i2c_get_clientdata(client);
359         s32 ret;
360
361         ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
362         if (ret < 0)
363                 return ret;
364
365         /* returned values are 16 bit */
366         if (sbs_data[reg_offset].min_value < 0)
367                 ret = (s16)ret;
368
369         if (ret >= sbs_data[reg_offset].min_value &&
370             ret <= sbs_data[reg_offset].max_value) {
371                 val->intval = ret;
372                 if (psp != POWER_SUPPLY_PROP_STATUS)
373                         return 0;
374
375                 if (ret & BATTERY_FULL_CHARGED)
376                         val->intval = POWER_SUPPLY_STATUS_FULL;
377                 else if (ret & BATTERY_FULL_DISCHARGED)
378                         val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
379                 else if (ret & BATTERY_DISCHARGING)
380                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
381                 else
382                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
383
384                 if (chip->poll_time == 0)
385                         chip->last_state = val->intval;
386                 else if (chip->last_state != val->intval) {
387                         cancel_delayed_work_sync(&chip->work);
388                         power_supply_changed(&chip->power_supply);
389                         chip->poll_time = 0;
390                 }
391         } else {
392                 if (psp == POWER_SUPPLY_PROP_STATUS)
393                         val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
394                 else
395                         val->intval = 0;
396         }
397
398         return 0;
399 }
400
401 static int sbs_get_battery_string_property(struct i2c_client *client,
402         int reg_offset, enum power_supply_property psp, char *val)
403 {
404         s32 ret;
405
406         ret = sbs_read_string_data(client, sbs_data[reg_offset].addr, val);
407
408         if (ret < 0)
409                 return ret;
410
411         return 0;
412 }
413
414 static void  sbs_unit_adjustment(struct i2c_client *client,
415         enum power_supply_property psp, union power_supply_propval *val)
416 {
417 #define BASE_UNIT_CONVERSION            1000
418 #define BATTERY_MODE_CAP_MULT_WATT      (10 * BASE_UNIT_CONVERSION)
419 #define TIME_UNIT_CONVERSION            60
420 #define TEMP_KELVIN_TO_CELSIUS          2731
421         switch (psp) {
422         case POWER_SUPPLY_PROP_ENERGY_NOW:
423         case POWER_SUPPLY_PROP_ENERGY_FULL:
424         case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
425                 /* sbs provides energy in units of 10mWh.
426                  * Convert to ÂµWh
427                  */
428                 val->intval *= BATTERY_MODE_CAP_MULT_WATT;
429                 break;
430
431         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
432         case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
433         case POWER_SUPPLY_PROP_CURRENT_NOW:
434         case POWER_SUPPLY_PROP_CHARGE_NOW:
435         case POWER_SUPPLY_PROP_CHARGE_FULL:
436         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
437                 val->intval *= BASE_UNIT_CONVERSION;
438                 break;
439
440         case POWER_SUPPLY_PROP_TEMP:
441                 /* sbs provides battery temperature in 0.1K
442                  * so convert it to 0.1°C
443                  */
444                 val->intval -= TEMP_KELVIN_TO_CELSIUS;
445                 break;
446
447         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
448         case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
449                 /* sbs provides time to empty and time to full in minutes.
450                  * Convert to seconds
451                  */
452                 val->intval *= TIME_UNIT_CONVERSION;
453                 break;
454
455         default:
456                 dev_dbg(&client->dev,
457                         "%s: no need for unit conversion %d\n", __func__, psp);
458         }
459 }
460
461 static enum sbs_battery_mode sbs_set_battery_mode(struct i2c_client *client,
462         enum sbs_battery_mode mode)
463 {
464         int ret, original_val;
465
466         original_val = sbs_read_word_data(client, BATTERY_MODE_OFFSET);
467         if (original_val < 0)
468                 return original_val;
469
470         if ((original_val & BATTERY_MODE_MASK) == mode)
471                 return mode;
472
473         if (mode == BATTERY_MODE_AMPS)
474                 ret = original_val & ~BATTERY_MODE_MASK;
475         else
476                 ret = original_val | BATTERY_MODE_MASK;
477
478         ret = sbs_write_word_data(client, BATTERY_MODE_OFFSET, ret);
479         if (ret < 0)
480                 return ret;
481
482         return original_val & BATTERY_MODE_MASK;
483 }
484
485 static int sbs_get_battery_capacity(struct i2c_client *client,
486         int reg_offset, enum power_supply_property psp,
487         union power_supply_propval *val)
488 {
489         s32 ret;
490         enum sbs_battery_mode mode = BATTERY_MODE_WATTS;
491
492         if (power_supply_is_amp_property(psp))
493                 mode = BATTERY_MODE_AMPS;
494
495         mode = sbs_set_battery_mode(client, mode);
496         if (mode < 0)
497                 return mode;
498
499         ret = sbs_read_word_data(client, sbs_data[reg_offset].addr);
500         if (ret < 0)
501                 return ret;
502
503         if (psp == POWER_SUPPLY_PROP_CAPACITY) {
504                 /* sbs spec says that this can be >100 %
505                 * even if max value is 100 % */
506                 val->intval = min(ret, 100);
507         } else
508                 val->intval = ret;
509
510         ret = sbs_set_battery_mode(client, mode);
511         if (ret < 0)
512                 return ret;
513
514         return 0;
515 }
516
517 static char sbs_serial[5];
518 static int sbs_get_battery_serial_number(struct i2c_client *client,
519         union power_supply_propval *val)
520 {
521         int ret;
522
523         ret = sbs_read_word_data(client, sbs_data[REG_SERIAL_NUMBER].addr);
524         if (ret < 0)
525                 return ret;
526
527         ret = sprintf(sbs_serial, "%04x", ret);
528         val->strval = sbs_serial;
529
530         return 0;
531 }
532
533 static int sbs_get_property_index(struct i2c_client *client,
534         enum power_supply_property psp)
535 {
536         int count;
537         for (count = 0; count < ARRAY_SIZE(sbs_data); count++)
538                 if (psp == sbs_data[count].psp)
539                         return count;
540
541         dev_warn(&client->dev,
542                 "%s: Invalid Property - %d\n", __func__, psp);
543
544         return -EINVAL;
545 }
546
547 static int sbs_get_property(struct power_supply *psy,
548         enum power_supply_property psp,
549         union power_supply_propval *val)
550 {
551         int ret = 0;
552         struct sbs_info *chip = container_of(psy,
553                                 struct sbs_info, power_supply);
554         struct i2c_client *client = chip->client;
555
556         switch (psp) {
557         case POWER_SUPPLY_PROP_PRESENT:
558         case POWER_SUPPLY_PROP_HEALTH:
559                 ret = sbs_get_battery_presence_and_health(client, psp, val);
560                 if (psp == POWER_SUPPLY_PROP_PRESENT)
561                         return 0;
562                 break;
563
564         case POWER_SUPPLY_PROP_TECHNOLOGY:
565                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
566                 break;
567
568         case POWER_SUPPLY_PROP_ENERGY_NOW:
569         case POWER_SUPPLY_PROP_ENERGY_FULL:
570         case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
571         case POWER_SUPPLY_PROP_CHARGE_NOW:
572         case POWER_SUPPLY_PROP_CHARGE_FULL:
573         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
574         case POWER_SUPPLY_PROP_CAPACITY:
575                 ret = sbs_get_property_index(client, psp);
576                 if (ret < 0)
577                         break;
578
579                 ret = sbs_get_battery_capacity(client, ret, psp, val);
580                 break;
581
582         case POWER_SUPPLY_PROP_SERIAL_NUMBER:
583                 ret = sbs_get_battery_serial_number(client, val);
584                 break;
585
586         case POWER_SUPPLY_PROP_STATUS:
587         case POWER_SUPPLY_PROP_CYCLE_COUNT:
588         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
589         case POWER_SUPPLY_PROP_CURRENT_NOW:
590         case POWER_SUPPLY_PROP_TEMP:
591         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
592         case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
593         case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
594                 ret = sbs_get_property_index(client, psp);
595                 if (ret < 0)
596                         break;
597
598                 ret = sbs_get_battery_property(client, ret, psp, val);
599                 break;
600
601         case POWER_SUPPLY_PROP_MODEL_NAME:
602                 ret = sbs_get_property_index(client, psp);
603                 if (ret < 0)
604                         break;
605
606                 ret = sbs_get_battery_string_property(client, ret, psp,
607                                                       model_name);
608                 val->strval = model_name;
609                 break;
610
611         case POWER_SUPPLY_PROP_MANUFACTURER:
612                 ret = sbs_get_property_index(client, psp);
613                 if (ret < 0)
614                         break;
615
616                 ret = sbs_get_battery_string_property(client, ret, psp,
617                                                       manufacturer);
618                 val->strval = manufacturer;
619                 break;
620
621         default:
622                 dev_err(&client->dev,
623                         "%s: INVALID property\n", __func__);
624                 return -EINVAL;
625         }
626
627         if (!chip->enable_detection)
628                 goto done;
629
630         if (!chip->gpio_detect &&
631                 chip->is_present != (ret >= 0)) {
632                 chip->is_present = (ret >= 0);
633                 power_supply_changed(&chip->power_supply);
634         }
635
636 done:
637         if (!ret) {
638                 /* Convert units to match requirements for power supply class */
639                 sbs_unit_adjustment(client, psp, val);
640         }
641
642         dev_dbg(&client->dev,
643                 "%s: property = %d, value = %x\n", __func__, psp, val->intval);
644
645         if (ret && chip->is_present)
646                 return ret;
647
648         /* battery not present, so return NODATA for properties */
649         if (ret)
650                 return -ENODATA;
651
652         return 0;
653 }
654
655 static irqreturn_t sbs_irq(int irq, void *devid)
656 {
657         struct power_supply *battery = devid;
658
659         power_supply_changed(battery);
660
661         return IRQ_HANDLED;
662 }
663
664 static void sbs_external_power_changed(struct power_supply *psy)
665 {
666         struct sbs_info *chip;
667
668         chip = container_of(psy, struct sbs_info, power_supply);
669
670         if (chip->ignore_changes > 0) {
671                 chip->ignore_changes--;
672                 return;
673         }
674
675         /* cancel outstanding work */
676         cancel_delayed_work_sync(&chip->work);
677
678         schedule_delayed_work(&chip->work, HZ);
679         chip->poll_time = chip->pdata->poll_retry_count;
680 }
681
682 static void sbs_delayed_work(struct work_struct *work)
683 {
684         struct sbs_info *chip;
685         s32 ret;
686
687         chip = container_of(work, struct sbs_info, work.work);
688
689         ret = sbs_read_word_data(chip->client, sbs_data[REG_STATUS].addr);
690         /* if the read failed, give up on this work */
691         if (ret < 0) {
692                 chip->poll_time = 0;
693                 return;
694         }
695
696         if (ret & BATTERY_FULL_CHARGED)
697                 ret = POWER_SUPPLY_STATUS_FULL;
698         else if (ret & BATTERY_FULL_DISCHARGED)
699                 ret = POWER_SUPPLY_STATUS_NOT_CHARGING;
700         else if (ret & BATTERY_DISCHARGING)
701                 ret = POWER_SUPPLY_STATUS_DISCHARGING;
702         else
703                 ret = POWER_SUPPLY_STATUS_CHARGING;
704
705         if (chip->last_state != ret) {
706                 chip->poll_time = 0;
707                 power_supply_changed(&chip->power_supply);
708                 return;
709         }
710         if (chip->poll_time > 0) {
711                 schedule_delayed_work(&chip->work, HZ);
712                 chip->poll_time--;
713                 return;
714         }
715 }
716
717 #if defined(CONFIG_OF)
718
719 #include <linux/of_device.h>
720 #include <linux/of_gpio.h>
721
722 static const struct of_device_id sbs_dt_ids[] = {
723         { .compatible = "sbs,sbs-battery" },
724         { .compatible = "ti,bq20z75" },
725         { }
726 };
727 MODULE_DEVICE_TABLE(of, sbs_dt_ids);
728
729 static struct sbs_platform_data *sbs_of_populate_pdata(
730                 struct i2c_client *client)
731 {
732         struct device_node *of_node = client->dev.of_node;
733         struct sbs_platform_data *pdata = client->dev.platform_data;
734         enum of_gpio_flags gpio_flags;
735         int rc;
736         u32 prop;
737
738         /* verify this driver matches this device */
739         if (!of_node)
740                 return NULL;
741
742         /* if platform data is set, honor it */
743         if (pdata)
744                 return pdata;
745
746         /* first make sure at least one property is set, otherwise
747          * it won't change behavior from running without pdata.
748          */
749         if (!of_get_property(of_node, "sbs,i2c-retry-count", NULL) &&
750                 !of_get_property(of_node, "sbs,poll-retry-count", NULL) &&
751                 !of_get_property(of_node, "sbs,battery-detect-gpios", NULL))
752                 goto of_out;
753
754         pdata = devm_kzalloc(&client->dev, sizeof(struct sbs_platform_data),
755                                 GFP_KERNEL);
756         if (!pdata)
757                 goto of_out;
758
759         rc = of_property_read_u32(of_node, "sbs,i2c-retry-count", &prop);
760         if (!rc)
761                 pdata->i2c_retry_count = prop;
762
763         rc = of_property_read_u32(of_node, "sbs,poll-retry-count", &prop);
764         if (!rc)
765                 pdata->poll_retry_count = prop;
766
767         if (!of_get_property(of_node, "sbs,battery-detect-gpios", NULL)) {
768                 pdata->battery_detect = -1;
769                 goto of_out;
770         }
771
772         pdata->battery_detect = of_get_named_gpio_flags(of_node,
773                         "sbs,battery-detect-gpios", 0, &gpio_flags);
774
775         if (gpio_flags & OF_GPIO_ACTIVE_LOW)
776                 pdata->battery_detect_present = 0;
777         else
778                 pdata->battery_detect_present = 1;
779
780 of_out:
781         return pdata;
782 }
783 #else
784 #define sbs_dt_ids NULL
785 static struct sbs_platform_data *sbs_of_populate_pdata(
786         struct i2c_client *client)
787 {
788         return client->dev.platform_data;
789 }
790 #endif
791
792 static int __devinit sbs_probe(struct i2c_client *client,
793         const struct i2c_device_id *id)
794 {
795         struct sbs_info *chip;
796         struct sbs_platform_data *pdata = client->dev.platform_data;
797         int rc;
798         int irq;
799         char *name;
800
801         name = kasprintf(GFP_KERNEL, "sbs-%s", dev_name(&client->dev));
802         if (!name) {
803                 dev_err(&client->dev, "Failed to allocate device name\n");
804                 return -ENOMEM;
805         }
806
807         chip = kzalloc(sizeof(struct sbs_info), GFP_KERNEL);
808         if (!chip) {
809                 rc = -ENOMEM;
810                 goto exit_free_name;
811         }
812
813         chip->client = client;
814         chip->enable_detection = false;
815         chip->gpio_detect = false;
816         chip->power_supply.name = name;
817         chip->power_supply.type = POWER_SUPPLY_TYPE_BATTERY;
818         chip->power_supply.properties = sbs_properties;
819         chip->power_supply.num_properties = ARRAY_SIZE(sbs_properties);
820         chip->power_supply.get_property = sbs_get_property;
821         /* ignore first notification of external change, it is generated
822          * from the power_supply_register call back
823          */
824         chip->ignore_changes = 1;
825         chip->last_state = POWER_SUPPLY_STATUS_UNKNOWN;
826         chip->power_supply.external_power_changed = sbs_external_power_changed;
827
828         pdata = sbs_of_populate_pdata(client);
829
830         if (pdata) {
831                 chip->gpio_detect = gpio_is_valid(pdata->battery_detect);
832                 chip->pdata = pdata;
833         }
834
835         i2c_set_clientdata(client, chip);
836
837         if (!chip->gpio_detect)
838                 goto skip_gpio;
839
840         rc = gpio_request(pdata->battery_detect, dev_name(&client->dev));
841         if (rc) {
842                 dev_warn(&client->dev, "Failed to request gpio: %d\n", rc);
843                 chip->gpio_detect = false;
844                 goto skip_gpio;
845         }
846
847         rc = gpio_direction_input(pdata->battery_detect);
848         if (rc) {
849                 dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc);
850                 gpio_free(pdata->battery_detect);
851                 chip->gpio_detect = false;
852                 goto skip_gpio;
853         }
854
855         irq = gpio_to_irq(pdata->battery_detect);
856         if (irq <= 0) {
857                 dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
858                 gpio_free(pdata->battery_detect);
859                 chip->gpio_detect = false;
860                 goto skip_gpio;
861         }
862
863         rc = request_irq(irq, sbs_irq,
864                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
865                 dev_name(&client->dev), &chip->power_supply);
866         if (rc) {
867                 dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
868                 gpio_free(pdata->battery_detect);
869                 chip->gpio_detect = false;
870                 goto skip_gpio;
871         }
872
873         chip->irq = irq;
874
875 skip_gpio:
876
877         /* Before we register, we need to make sure we can actually talk
878          * to the battery.
879          */
880         rc = sbs_read_word_data(client, sbs_data[REG_STATUS].addr);
881         if (rc < 0) {
882                 dev_err(&client->dev, "%s: Failed to get device status\n",
883                         __func__);
884                 goto exit_psupply;
885         }
886
887         rc = power_supply_register(&client->dev, &chip->power_supply);
888         if (rc) {
889                 dev_err(&client->dev,
890                         "%s: Failed to register power supply\n", __func__);
891                 goto exit_psupply;
892         }
893
894         dev_info(&client->dev,
895                 "%s: battery gas gauge device registered\n", client->name);
896
897         INIT_DELAYED_WORK(&chip->work, sbs_delayed_work);
898
899         chip->enable_detection = true;
900
901         return 0;
902
903 exit_psupply:
904         if (chip->irq)
905                 free_irq(chip->irq, &chip->power_supply);
906         if (chip->gpio_detect)
907                 gpio_free(pdata->battery_detect);
908
909         kfree(chip);
910
911 exit_free_name:
912         kfree(name);
913
914         return rc;
915 }
916
917 static int __devexit sbs_remove(struct i2c_client *client)
918 {
919         struct sbs_info *chip = i2c_get_clientdata(client);
920
921         if (chip->irq)
922                 free_irq(chip->irq, &chip->power_supply);
923         if (chip->gpio_detect)
924                 gpio_free(chip->pdata->battery_detect);
925
926         power_supply_unregister(&chip->power_supply);
927
928         cancel_delayed_work_sync(&chip->work);
929
930         kfree(chip->power_supply.name);
931         kfree(chip);
932         chip = NULL;
933
934         return 0;
935 }
936
937 #if defined CONFIG_PM
938 static int sbs_suspend(struct i2c_client *client,
939         pm_message_t state)
940 {
941         struct sbs_info *chip = i2c_get_clientdata(client);
942         s32 ret;
943
944         if (chip->poll_time > 0)
945                 cancel_delayed_work_sync(&chip->work);
946
947         /* write to manufacturer access with sleep command */
948         ret = sbs_write_word_data(client, sbs_data[REG_MANUFACTURER_DATA].addr,
949                 MANUFACTURER_ACCESS_SLEEP);
950         if (chip->is_present && ret < 0)
951                 return ret;
952
953         return 0;
954 }
955 #else
956 #define sbs_suspend             NULL
957 #endif
958 /* any smbus transaction will wake up sbs */
959 #define sbs_resume              NULL
960
961 static const struct i2c_device_id sbs_id[] = {
962         { "bq20z75", 0 },
963         { "sbs-battery", 1 },
964         {}
965 };
966 MODULE_DEVICE_TABLE(i2c, sbs_id);
967
968 static struct i2c_driver sbs_battery_driver = {
969         .probe          = sbs_probe,
970         .remove         = __devexit_p(sbs_remove),
971         .suspend        = sbs_suspend,
972         .resume         = sbs_resume,
973         .id_table       = sbs_id,
974         .driver = {
975                 .name   = "sbs-battery",
976                 .of_match_table = sbs_dt_ids,
977         },
978 };
979 module_i2c_driver(sbs_battery_driver);
980
981 MODULE_DESCRIPTION("SBS battery monitor driver");
982 MODULE_LICENSE("GPL");