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