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