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