Linux 4.19.133
[linux/fpc-iii.git] / drivers / power / supply / ds2780_battery.c
blob370e9109342b8a3e9340f5c7a4eba7d6a0e69b54
1 /*
2 * 1-wire client/driver for the Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC
4 * Copyright (C) 2010 Indesign, LLC
6 * Author: Clifton Barnes <cabarnes@indesign-llc.com>
8 * Based on ds2760_battery and ds2782_battery drivers
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/param.h>
19 #include <linux/pm.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/idr.h>
24 #include <linux/w1.h>
25 #include "../../w1/slaves/w1_ds2780.h"
27 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
28 #define DS2780_CURRENT_UNITS 1563
29 /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
30 #define DS2780_CHARGE_UNITS 6250
31 /* Number of bytes in user EEPROM space */
32 #define DS2780_USER_EEPROM_SIZE (DS2780_EEPROM_BLOCK0_END - \
33 DS2780_EEPROM_BLOCK0_START + 1)
34 /* Number of bytes in parameter EEPROM space */
35 #define DS2780_PARAM_EEPROM_SIZE (DS2780_EEPROM_BLOCK1_END - \
36 DS2780_EEPROM_BLOCK1_START + 1)
38 struct ds2780_device_info {
39 struct device *dev;
40 struct power_supply *bat;
41 struct power_supply_desc bat_desc;
42 struct device *w1_dev;
45 enum current_types {
46 CURRENT_NOW,
47 CURRENT_AVG,
50 static const char model[] = "DS2780";
51 static const char manufacturer[] = "Maxim/Dallas";
53 static inline struct ds2780_device_info *
54 to_ds2780_device_info(struct power_supply *psy)
56 return power_supply_get_drvdata(psy);
59 static inline int ds2780_battery_io(struct ds2780_device_info *dev_info,
60 char *buf, int addr, size_t count, int io)
62 return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
65 static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val,
66 int addr)
68 return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0);
71 static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val,
72 int addr)
74 int ret;
75 u8 raw[2];
77 ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0);
78 if (ret < 0)
79 return ret;
81 *val = (raw[0] << 8) | raw[1];
83 return 0;
86 static inline int ds2780_read_block(struct ds2780_device_info *dev_info,
87 u8 *val, int addr, size_t count)
89 return ds2780_battery_io(dev_info, val, addr, count, 0);
92 static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val,
93 int addr, size_t count)
95 return ds2780_battery_io(dev_info, val, addr, count, 1);
98 static inline int ds2780_store_eeprom(struct device *dev, int addr)
100 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
103 static inline int ds2780_recall_eeprom(struct device *dev, int addr)
105 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
108 static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
110 int ret;
112 ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
113 if (ret < 0)
114 return ret;
116 ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
117 if (ret < 0)
118 return ret;
120 return 0;
123 /* Set sense resistor value in mhos */
124 static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
125 u8 conductance)
127 int ret;
129 ret = ds2780_write(dev_info, &conductance,
130 DS2780_RSNSP_REG, sizeof(u8));
131 if (ret < 0)
132 return ret;
134 return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
137 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
138 static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
139 u16 *rsgain)
141 return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG);
144 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
145 static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
146 u16 rsgain)
148 int ret;
149 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
151 ret = ds2780_write(dev_info, raw,
152 DS2780_RSGAIN_MSB_REG, sizeof(raw));
153 if (ret < 0)
154 return ret;
156 return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
159 static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
160 int *voltage_uV)
162 int ret;
163 s16 voltage_raw;
166 * The voltage value is located in 10 bits across the voltage MSB
167 * and LSB registers in two's compliment form
168 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
169 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
170 * voltage MSB register
171 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
172 * voltage LSB register
174 ret = ds2780_read16(dev_info, &voltage_raw,
175 DS2780_VOLT_MSB_REG);
176 if (ret < 0)
177 return ret;
180 * DS2780 reports voltage in units of 4.88mV, but the battery class
181 * reports in units of uV, so convert by multiplying by 4880.
183 *voltage_uV = (voltage_raw / 32) * 4880;
184 return 0;
187 static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
188 int *temperature)
190 int ret;
191 s16 temperature_raw;
194 * The temperature value is located in 10 bits across the temperature
195 * MSB and LSB registers in two's compliment form
196 * Sign bit of the temperature value is in bit 7 of the temperature
197 * MSB register
198 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
199 * temperature MSB register
200 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
201 * temperature LSB register
203 ret = ds2780_read16(dev_info, &temperature_raw,
204 DS2780_TEMP_MSB_REG);
205 if (ret < 0)
206 return ret;
209 * Temperature is measured in units of 0.125 degrees celcius, the
210 * power_supply class measures temperature in tenths of degrees
211 * celsius. The temperature value is stored as a 10 bit number, plus
212 * sign in the upper bits of a 16 bit register.
214 *temperature = ((temperature_raw / 32) * 125) / 100;
215 return 0;
218 static int ds2780_get_current(struct ds2780_device_info *dev_info,
219 enum current_types type, int *current_uA)
221 int ret, sense_res;
222 s16 current_raw;
223 u8 sense_res_raw, reg_msb;
226 * The units of measurement for current are dependent on the value of
227 * the sense resistor.
229 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
230 if (ret < 0)
231 return ret;
233 if (sense_res_raw == 0) {
234 dev_err(dev_info->dev, "sense resistor value is 0\n");
235 return -EINVAL;
237 sense_res = 1000 / sense_res_raw;
239 if (type == CURRENT_NOW)
240 reg_msb = DS2780_CURRENT_MSB_REG;
241 else if (type == CURRENT_AVG)
242 reg_msb = DS2780_IAVG_MSB_REG;
243 else
244 return -EINVAL;
247 * The current value is located in 16 bits across the current MSB
248 * and LSB registers in two's compliment form
249 * Sign bit of the current value is in bit 7 of the current MSB register
250 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
251 * MSB register
252 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
253 * LSB register
255 ret = ds2780_read16(dev_info, &current_raw, reg_msb);
256 if (ret < 0)
257 return ret;
259 *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
260 return 0;
263 static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info,
264 int *accumulated_current)
266 int ret, sense_res;
267 s16 current_raw;
268 u8 sense_res_raw;
271 * The units of measurement for accumulated current are dependent on
272 * the value of the sense resistor.
274 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
275 if (ret < 0)
276 return ret;
278 if (sense_res_raw == 0) {
279 dev_err(dev_info->dev, "sense resistor value is 0\n");
280 return -ENXIO;
282 sense_res = 1000 / sense_res_raw;
285 * The ACR value is located in 16 bits across the ACR MSB and
286 * LSB registers
287 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
288 * MSB register
289 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
290 * LSB register
292 ret = ds2780_read16(dev_info, &current_raw, DS2780_ACR_MSB_REG);
293 if (ret < 0)
294 return ret;
296 *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
297 return 0;
300 static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
301 int *capacity)
303 int ret;
304 u8 raw;
306 ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG);
307 if (ret < 0)
308 return ret;
310 *capacity = raw;
311 return raw;
314 static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
316 int ret, current_uA, capacity;
318 ret = ds2780_get_current(dev_info, CURRENT_NOW, &current_uA);
319 if (ret < 0)
320 return ret;
322 ret = ds2780_get_capacity(dev_info, &capacity);
323 if (ret < 0)
324 return ret;
326 if (capacity == 100)
327 *status = POWER_SUPPLY_STATUS_FULL;
328 else if (current_uA == 0)
329 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
330 else if (current_uA < 0)
331 *status = POWER_SUPPLY_STATUS_DISCHARGING;
332 else
333 *status = POWER_SUPPLY_STATUS_CHARGING;
335 return 0;
338 static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
339 int *charge_now)
341 int ret;
342 u16 charge_raw;
345 * The RAAC value is located in 16 bits across the RAAC MSB and
346 * LSB registers
347 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
348 * MSB register
349 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
350 * LSB register
352 ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG);
353 if (ret < 0)
354 return ret;
356 *charge_now = charge_raw * 1600;
357 return 0;
360 static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
361 u8 *control_reg)
363 return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG);
366 static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
367 u8 control_reg)
369 int ret;
371 ret = ds2780_write(dev_info, &control_reg,
372 DS2780_CONTROL_REG, sizeof(u8));
373 if (ret < 0)
374 return ret;
376 return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
379 static int ds2780_battery_get_property(struct power_supply *psy,
380 enum power_supply_property psp,
381 union power_supply_propval *val)
383 int ret = 0;
384 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
386 switch (psp) {
387 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
388 ret = ds2780_get_voltage(dev_info, &val->intval);
389 break;
391 case POWER_SUPPLY_PROP_TEMP:
392 ret = ds2780_get_temperature(dev_info, &val->intval);
393 break;
395 case POWER_SUPPLY_PROP_MODEL_NAME:
396 val->strval = model;
397 break;
399 case POWER_SUPPLY_PROP_MANUFACTURER:
400 val->strval = manufacturer;
401 break;
403 case POWER_SUPPLY_PROP_CURRENT_NOW:
404 ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
405 break;
407 case POWER_SUPPLY_PROP_CURRENT_AVG:
408 ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
409 break;
411 case POWER_SUPPLY_PROP_STATUS:
412 ret = ds2780_get_status(dev_info, &val->intval);
413 break;
415 case POWER_SUPPLY_PROP_CAPACITY:
416 ret = ds2780_get_capacity(dev_info, &val->intval);
417 break;
419 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
420 ret = ds2780_get_accumulated_current(dev_info, &val->intval);
421 break;
423 case POWER_SUPPLY_PROP_CHARGE_NOW:
424 ret = ds2780_get_charge_now(dev_info, &val->intval);
425 break;
427 default:
428 ret = -EINVAL;
431 return ret;
434 static enum power_supply_property ds2780_battery_props[] = {
435 POWER_SUPPLY_PROP_STATUS,
436 POWER_SUPPLY_PROP_VOLTAGE_NOW,
437 POWER_SUPPLY_PROP_TEMP,
438 POWER_SUPPLY_PROP_MODEL_NAME,
439 POWER_SUPPLY_PROP_MANUFACTURER,
440 POWER_SUPPLY_PROP_CURRENT_NOW,
441 POWER_SUPPLY_PROP_CURRENT_AVG,
442 POWER_SUPPLY_PROP_CAPACITY,
443 POWER_SUPPLY_PROP_CHARGE_COUNTER,
444 POWER_SUPPLY_PROP_CHARGE_NOW,
447 static ssize_t ds2780_get_pmod_enabled(struct device *dev,
448 struct device_attribute *attr,
449 char *buf)
451 int ret;
452 u8 control_reg;
453 struct power_supply *psy = to_power_supply(dev);
454 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
456 /* Get power mode */
457 ret = ds2780_get_control_register(dev_info, &control_reg);
458 if (ret < 0)
459 return ret;
461 return sprintf(buf, "%d\n",
462 !!(control_reg & DS2780_CONTROL_REG_PMOD));
465 static ssize_t ds2780_set_pmod_enabled(struct device *dev,
466 struct device_attribute *attr,
467 const char *buf,
468 size_t count)
470 int ret;
471 u8 control_reg, new_setting;
472 struct power_supply *psy = to_power_supply(dev);
473 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
475 /* Set power mode */
476 ret = ds2780_get_control_register(dev_info, &control_reg);
477 if (ret < 0)
478 return ret;
480 ret = kstrtou8(buf, 0, &new_setting);
481 if (ret < 0)
482 return ret;
484 if ((new_setting != 0) && (new_setting != 1)) {
485 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
486 return -EINVAL;
489 if (new_setting)
490 control_reg |= DS2780_CONTROL_REG_PMOD;
491 else
492 control_reg &= ~DS2780_CONTROL_REG_PMOD;
494 ret = ds2780_set_control_register(dev_info, control_reg);
495 if (ret < 0)
496 return ret;
498 return count;
501 static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
502 struct device_attribute *attr,
503 char *buf)
505 int ret;
506 u8 sense_resistor;
507 struct power_supply *psy = to_power_supply(dev);
508 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
510 ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG);
511 if (ret < 0)
512 return ret;
514 ret = sprintf(buf, "%d\n", sense_resistor);
515 return ret;
518 static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
519 struct device_attribute *attr,
520 const char *buf,
521 size_t count)
523 int ret;
524 u8 new_setting;
525 struct power_supply *psy = to_power_supply(dev);
526 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
528 ret = kstrtou8(buf, 0, &new_setting);
529 if (ret < 0)
530 return ret;
532 ret = ds2780_set_sense_register(dev_info, new_setting);
533 if (ret < 0)
534 return ret;
536 return count;
539 static ssize_t ds2780_get_rsgain_setting(struct device *dev,
540 struct device_attribute *attr,
541 char *buf)
543 int ret;
544 u16 rsgain;
545 struct power_supply *psy = to_power_supply(dev);
546 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
548 ret = ds2780_get_rsgain_register(dev_info, &rsgain);
549 if (ret < 0)
550 return ret;
552 return sprintf(buf, "%d\n", rsgain);
555 static ssize_t ds2780_set_rsgain_setting(struct device *dev,
556 struct device_attribute *attr,
557 const char *buf,
558 size_t count)
560 int ret;
561 u16 new_setting;
562 struct power_supply *psy = to_power_supply(dev);
563 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
565 ret = kstrtou16(buf, 0, &new_setting);
566 if (ret < 0)
567 return ret;
569 /* Gain can only be from 0 to 1.999 in steps of .001 */
570 if (new_setting > 1999) {
571 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
572 return -EINVAL;
575 ret = ds2780_set_rsgain_register(dev_info, new_setting);
576 if (ret < 0)
577 return ret;
579 return count;
582 static ssize_t ds2780_get_pio_pin(struct device *dev,
583 struct device_attribute *attr,
584 char *buf)
586 int ret;
587 u8 sfr;
588 struct power_supply *psy = to_power_supply(dev);
589 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
591 ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG);
592 if (ret < 0)
593 return ret;
595 ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
596 return ret;
599 static ssize_t ds2780_set_pio_pin(struct device *dev,
600 struct device_attribute *attr,
601 const char *buf,
602 size_t count)
604 int ret;
605 u8 new_setting;
606 struct power_supply *psy = to_power_supply(dev);
607 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
609 ret = kstrtou8(buf, 0, &new_setting);
610 if (ret < 0)
611 return ret;
613 if ((new_setting != 0) && (new_setting != 1)) {
614 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
615 return -EINVAL;
618 ret = ds2780_write(dev_info, &new_setting,
619 DS2780_SFR_REG, sizeof(u8));
620 if (ret < 0)
621 return ret;
623 return count;
626 static ssize_t ds2780_read_param_eeprom_bin(struct file *filp,
627 struct kobject *kobj,
628 struct bin_attribute *bin_attr,
629 char *buf, loff_t off, size_t count)
631 struct device *dev = container_of(kobj, struct device, kobj);
632 struct power_supply *psy = to_power_supply(dev);
633 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
635 return ds2780_read_block(dev_info, buf,
636 DS2780_EEPROM_BLOCK1_START + off, count);
639 static ssize_t ds2780_write_param_eeprom_bin(struct file *filp,
640 struct kobject *kobj,
641 struct bin_attribute *bin_attr,
642 char *buf, loff_t off, size_t count)
644 struct device *dev = container_of(kobj, struct device, kobj);
645 struct power_supply *psy = to_power_supply(dev);
646 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
647 int ret;
649 ret = ds2780_write(dev_info, buf,
650 DS2780_EEPROM_BLOCK1_START + off, count);
651 if (ret < 0)
652 return ret;
654 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
655 if (ret < 0)
656 return ret;
658 return count;
661 static const struct bin_attribute ds2780_param_eeprom_bin_attr = {
662 .attr = {
663 .name = "param_eeprom",
664 .mode = S_IRUGO | S_IWUSR,
666 .size = DS2780_PARAM_EEPROM_SIZE,
667 .read = ds2780_read_param_eeprom_bin,
668 .write = ds2780_write_param_eeprom_bin,
671 static ssize_t ds2780_read_user_eeprom_bin(struct file *filp,
672 struct kobject *kobj,
673 struct bin_attribute *bin_attr,
674 char *buf, loff_t off, size_t count)
676 struct device *dev = container_of(kobj, struct device, kobj);
677 struct power_supply *psy = to_power_supply(dev);
678 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
680 return ds2780_read_block(dev_info, buf,
681 DS2780_EEPROM_BLOCK0_START + off, count);
684 static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
685 struct kobject *kobj,
686 struct bin_attribute *bin_attr,
687 char *buf, loff_t off, size_t count)
689 struct device *dev = container_of(kobj, struct device, kobj);
690 struct power_supply *psy = to_power_supply(dev);
691 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
692 int ret;
694 ret = ds2780_write(dev_info, buf,
695 DS2780_EEPROM_BLOCK0_START + off, count);
696 if (ret < 0)
697 return ret;
699 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
700 if (ret < 0)
701 return ret;
703 return count;
706 static const struct bin_attribute ds2780_user_eeprom_bin_attr = {
707 .attr = {
708 .name = "user_eeprom",
709 .mode = S_IRUGO | S_IWUSR,
711 .size = DS2780_USER_EEPROM_SIZE,
712 .read = ds2780_read_user_eeprom_bin,
713 .write = ds2780_write_user_eeprom_bin,
716 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
717 ds2780_set_pmod_enabled);
718 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
719 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
720 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
721 ds2780_set_rsgain_setting);
722 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
723 ds2780_set_pio_pin);
726 static struct attribute *ds2780_attributes[] = {
727 &dev_attr_pmod_enabled.attr,
728 &dev_attr_sense_resistor_value.attr,
729 &dev_attr_rsgain_setting.attr,
730 &dev_attr_pio_pin.attr,
731 NULL
734 static const struct attribute_group ds2780_attr_group = {
735 .attrs = ds2780_attributes,
738 static int ds2780_battery_probe(struct platform_device *pdev)
740 struct power_supply_config psy_cfg = {};
741 int ret = 0;
742 struct ds2780_device_info *dev_info;
744 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
745 if (!dev_info) {
746 ret = -ENOMEM;
747 goto fail;
750 platform_set_drvdata(pdev, dev_info);
752 dev_info->dev = &pdev->dev;
753 dev_info->w1_dev = pdev->dev.parent;
754 dev_info->bat_desc.name = dev_name(&pdev->dev);
755 dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
756 dev_info->bat_desc.properties = ds2780_battery_props;
757 dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2780_battery_props);
758 dev_info->bat_desc.get_property = ds2780_battery_get_property;
760 psy_cfg.drv_data = dev_info;
762 dev_info->bat = power_supply_register(&pdev->dev, &dev_info->bat_desc,
763 &psy_cfg);
764 if (IS_ERR(dev_info->bat)) {
765 dev_err(dev_info->dev, "failed to register battery\n");
766 ret = PTR_ERR(dev_info->bat);
767 goto fail;
770 ret = sysfs_create_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
771 if (ret) {
772 dev_err(dev_info->dev, "failed to create sysfs group\n");
773 goto fail_unregister;
776 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
777 &ds2780_param_eeprom_bin_attr);
778 if (ret) {
779 dev_err(dev_info->dev,
780 "failed to create param eeprom bin file");
781 goto fail_remove_group;
784 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
785 &ds2780_user_eeprom_bin_attr);
786 if (ret) {
787 dev_err(dev_info->dev,
788 "failed to create user eeprom bin file");
789 goto fail_remove_bin_file;
792 return 0;
794 fail_remove_bin_file:
795 sysfs_remove_bin_file(&dev_info->bat->dev.kobj,
796 &ds2780_param_eeprom_bin_attr);
797 fail_remove_group:
798 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
799 fail_unregister:
800 power_supply_unregister(dev_info->bat);
801 fail:
802 return ret;
805 static int ds2780_battery_remove(struct platform_device *pdev)
807 struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);
810 * Remove attributes before unregistering power supply
811 * because 'bat' will be freed on power_supply_unregister() call.
813 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
815 power_supply_unregister(dev_info->bat);
817 return 0;
820 static struct platform_driver ds2780_battery_driver = {
821 .driver = {
822 .name = "ds2780-battery",
824 .probe = ds2780_battery_probe,
825 .remove = ds2780_battery_remove,
828 module_platform_driver(ds2780_battery_driver);
830 MODULE_LICENSE("GPL");
831 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
832 MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauage IC driver");
833 MODULE_ALIAS("platform:ds2780-battery");