gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / power / ds2780_battery.c
bloba7a0427343f3ef563fd9d6264165e27edf1e0ef2
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 "../w1/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 struct power_supply *to_power_supply(struct device *dev)
61 return dev_get_drvdata(dev);
64 static inline int ds2780_battery_io(struct ds2780_device_info *dev_info,
65 char *buf, int addr, size_t count, int io)
67 return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
70 static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val,
71 int addr)
73 return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0);
76 static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val,
77 int addr)
79 int ret;
80 u8 raw[2];
82 ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0);
83 if (ret < 0)
84 return ret;
86 *val = (raw[0] << 8) | raw[1];
88 return 0;
91 static inline int ds2780_read_block(struct ds2780_device_info *dev_info,
92 u8 *val, int addr, size_t count)
94 return ds2780_battery_io(dev_info, val, addr, count, 0);
97 static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val,
98 int addr, size_t count)
100 return ds2780_battery_io(dev_info, val, addr, count, 1);
103 static inline int ds2780_store_eeprom(struct device *dev, int addr)
105 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
108 static inline int ds2780_recall_eeprom(struct device *dev, int addr)
110 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
113 static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
115 int ret;
117 ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
118 if (ret < 0)
119 return ret;
121 ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
122 if (ret < 0)
123 return ret;
125 return 0;
128 /* Set sense resistor value in mhos */
129 static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
130 u8 conductance)
132 int ret;
134 ret = ds2780_write(dev_info, &conductance,
135 DS2780_RSNSP_REG, sizeof(u8));
136 if (ret < 0)
137 return ret;
139 return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
142 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
143 static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
144 u16 *rsgain)
146 return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG);
149 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
150 static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
151 u16 rsgain)
153 int ret;
154 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
156 ret = ds2780_write(dev_info, raw,
157 DS2780_RSGAIN_MSB_REG, sizeof(raw));
158 if (ret < 0)
159 return ret;
161 return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
164 static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
165 int *voltage_uV)
167 int ret;
168 s16 voltage_raw;
171 * The voltage value is located in 10 bits across the voltage MSB
172 * and LSB registers in two's compliment form
173 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
174 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
175 * voltage MSB register
176 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
177 * voltage LSB register
179 ret = ds2780_read16(dev_info, &voltage_raw,
180 DS2780_VOLT_MSB_REG);
181 if (ret < 0)
182 return ret;
185 * DS2780 reports voltage in units of 4.88mV, but the battery class
186 * reports in units of uV, so convert by multiplying by 4880.
188 *voltage_uV = (voltage_raw / 32) * 4880;
189 return 0;
192 static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
193 int *temperature)
195 int ret;
196 s16 temperature_raw;
199 * The temperature value is located in 10 bits across the temperature
200 * MSB and LSB registers in two's compliment form
201 * Sign bit of the temperature value is in bit 7 of the temperature
202 * MSB register
203 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
204 * temperature MSB register
205 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
206 * temperature LSB register
208 ret = ds2780_read16(dev_info, &temperature_raw,
209 DS2780_TEMP_MSB_REG);
210 if (ret < 0)
211 return ret;
214 * Temperature is measured in units of 0.125 degrees celcius, the
215 * power_supply class measures temperature in tenths of degrees
216 * celsius. The temperature value is stored as a 10 bit number, plus
217 * sign in the upper bits of a 16 bit register.
219 *temperature = ((temperature_raw / 32) * 125) / 100;
220 return 0;
223 static int ds2780_get_current(struct ds2780_device_info *dev_info,
224 enum current_types type, int *current_uA)
226 int ret, sense_res;
227 s16 current_raw;
228 u8 sense_res_raw, reg_msb;
231 * The units of measurement for current are dependent on the value of
232 * the sense resistor.
234 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
235 if (ret < 0)
236 return ret;
238 if (sense_res_raw == 0) {
239 dev_err(dev_info->dev, "sense resistor value is 0\n");
240 return -EINVAL;
242 sense_res = 1000 / sense_res_raw;
244 if (type == CURRENT_NOW)
245 reg_msb = DS2780_CURRENT_MSB_REG;
246 else if (type == CURRENT_AVG)
247 reg_msb = DS2780_IAVG_MSB_REG;
248 else
249 return -EINVAL;
252 * The current value is located in 16 bits across the current MSB
253 * and LSB registers in two's compliment form
254 * Sign bit of the current value is in bit 7 of the current MSB register
255 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
256 * MSB register
257 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
258 * LSB register
260 ret = ds2780_read16(dev_info, &current_raw, reg_msb);
261 if (ret < 0)
262 return ret;
264 *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
265 return 0;
268 static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info,
269 int *accumulated_current)
271 int ret, sense_res;
272 s16 current_raw;
273 u8 sense_res_raw;
276 * The units of measurement for accumulated current are dependent on
277 * the value of the sense resistor.
279 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
280 if (ret < 0)
281 return ret;
283 if (sense_res_raw == 0) {
284 dev_err(dev_info->dev, "sense resistor value is 0\n");
285 return -ENXIO;
287 sense_res = 1000 / sense_res_raw;
290 * The ACR value is located in 16 bits across the ACR MSB and
291 * LSB registers
292 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
293 * MSB register
294 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
295 * LSB register
297 ret = ds2780_read16(dev_info, &current_raw, DS2780_ACR_MSB_REG);
298 if (ret < 0)
299 return ret;
301 *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
302 return 0;
305 static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
306 int *capacity)
308 int ret;
309 u8 raw;
311 ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG);
312 if (ret < 0)
313 return ret;
315 *capacity = raw;
316 return raw;
319 static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
321 int ret, current_uA, capacity;
323 ret = ds2780_get_current(dev_info, CURRENT_NOW, &current_uA);
324 if (ret < 0)
325 return ret;
327 ret = ds2780_get_capacity(dev_info, &capacity);
328 if (ret < 0)
329 return ret;
331 if (capacity == 100)
332 *status = POWER_SUPPLY_STATUS_FULL;
333 else if (current_uA == 0)
334 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
335 else if (current_uA < 0)
336 *status = POWER_SUPPLY_STATUS_DISCHARGING;
337 else
338 *status = POWER_SUPPLY_STATUS_CHARGING;
340 return 0;
343 static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
344 int *charge_now)
346 int ret;
347 u16 charge_raw;
350 * The RAAC value is located in 16 bits across the RAAC MSB and
351 * LSB registers
352 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
353 * MSB register
354 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
355 * LSB register
357 ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG);
358 if (ret < 0)
359 return ret;
361 *charge_now = charge_raw * 1600;
362 return 0;
365 static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
366 u8 *control_reg)
368 return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG);
371 static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
372 u8 control_reg)
374 int ret;
376 ret = ds2780_write(dev_info, &control_reg,
377 DS2780_CONTROL_REG, sizeof(u8));
378 if (ret < 0)
379 return ret;
381 return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
384 static int ds2780_battery_get_property(struct power_supply *psy,
385 enum power_supply_property psp,
386 union power_supply_propval *val)
388 int ret = 0;
389 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
391 switch (psp) {
392 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
393 ret = ds2780_get_voltage(dev_info, &val->intval);
394 break;
396 case POWER_SUPPLY_PROP_TEMP:
397 ret = ds2780_get_temperature(dev_info, &val->intval);
398 break;
400 case POWER_SUPPLY_PROP_MODEL_NAME:
401 val->strval = model;
402 break;
404 case POWER_SUPPLY_PROP_MANUFACTURER:
405 val->strval = manufacturer;
406 break;
408 case POWER_SUPPLY_PROP_CURRENT_NOW:
409 ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
410 break;
412 case POWER_SUPPLY_PROP_CURRENT_AVG:
413 ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
414 break;
416 case POWER_SUPPLY_PROP_STATUS:
417 ret = ds2780_get_status(dev_info, &val->intval);
418 break;
420 case POWER_SUPPLY_PROP_CAPACITY:
421 ret = ds2780_get_capacity(dev_info, &val->intval);
422 break;
424 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
425 ret = ds2780_get_accumulated_current(dev_info, &val->intval);
426 break;
428 case POWER_SUPPLY_PROP_CHARGE_NOW:
429 ret = ds2780_get_charge_now(dev_info, &val->intval);
430 break;
432 default:
433 ret = -EINVAL;
436 return ret;
439 static enum power_supply_property ds2780_battery_props[] = {
440 POWER_SUPPLY_PROP_STATUS,
441 POWER_SUPPLY_PROP_VOLTAGE_NOW,
442 POWER_SUPPLY_PROP_TEMP,
443 POWER_SUPPLY_PROP_MODEL_NAME,
444 POWER_SUPPLY_PROP_MANUFACTURER,
445 POWER_SUPPLY_PROP_CURRENT_NOW,
446 POWER_SUPPLY_PROP_CURRENT_AVG,
447 POWER_SUPPLY_PROP_CAPACITY,
448 POWER_SUPPLY_PROP_CHARGE_COUNTER,
449 POWER_SUPPLY_PROP_CHARGE_NOW,
452 static ssize_t ds2780_get_pmod_enabled(struct device *dev,
453 struct device_attribute *attr,
454 char *buf)
456 int ret;
457 u8 control_reg;
458 struct power_supply *psy = to_power_supply(dev);
459 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
461 /* Get power mode */
462 ret = ds2780_get_control_register(dev_info, &control_reg);
463 if (ret < 0)
464 return ret;
466 return sprintf(buf, "%d\n",
467 !!(control_reg & DS2780_CONTROL_REG_PMOD));
470 static ssize_t ds2780_set_pmod_enabled(struct device *dev,
471 struct device_attribute *attr,
472 const char *buf,
473 size_t count)
475 int ret;
476 u8 control_reg, new_setting;
477 struct power_supply *psy = to_power_supply(dev);
478 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
480 /* Set power mode */
481 ret = ds2780_get_control_register(dev_info, &control_reg);
482 if (ret < 0)
483 return ret;
485 ret = kstrtou8(buf, 0, &new_setting);
486 if (ret < 0)
487 return ret;
489 if ((new_setting != 0) && (new_setting != 1)) {
490 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
491 return -EINVAL;
494 if (new_setting)
495 control_reg |= DS2780_CONTROL_REG_PMOD;
496 else
497 control_reg &= ~DS2780_CONTROL_REG_PMOD;
499 ret = ds2780_set_control_register(dev_info, control_reg);
500 if (ret < 0)
501 return ret;
503 return count;
506 static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
507 struct device_attribute *attr,
508 char *buf)
510 int ret;
511 u8 sense_resistor;
512 struct power_supply *psy = to_power_supply(dev);
513 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
515 ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG);
516 if (ret < 0)
517 return ret;
519 ret = sprintf(buf, "%d\n", sense_resistor);
520 return ret;
523 static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
524 struct device_attribute *attr,
525 const char *buf,
526 size_t count)
528 int ret;
529 u8 new_setting;
530 struct power_supply *psy = to_power_supply(dev);
531 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
533 ret = kstrtou8(buf, 0, &new_setting);
534 if (ret < 0)
535 return ret;
537 ret = ds2780_set_sense_register(dev_info, new_setting);
538 if (ret < 0)
539 return ret;
541 return count;
544 static ssize_t ds2780_get_rsgain_setting(struct device *dev,
545 struct device_attribute *attr,
546 char *buf)
548 int ret;
549 u16 rsgain;
550 struct power_supply *psy = to_power_supply(dev);
551 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
553 ret = ds2780_get_rsgain_register(dev_info, &rsgain);
554 if (ret < 0)
555 return ret;
557 return sprintf(buf, "%d\n", rsgain);
560 static ssize_t ds2780_set_rsgain_setting(struct device *dev,
561 struct device_attribute *attr,
562 const char *buf,
563 size_t count)
565 int ret;
566 u16 new_setting;
567 struct power_supply *psy = to_power_supply(dev);
568 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
570 ret = kstrtou16(buf, 0, &new_setting);
571 if (ret < 0)
572 return ret;
574 /* Gain can only be from 0 to 1.999 in steps of .001 */
575 if (new_setting > 1999) {
576 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
577 return -EINVAL;
580 ret = ds2780_set_rsgain_register(dev_info, new_setting);
581 if (ret < 0)
582 return ret;
584 return count;
587 static ssize_t ds2780_get_pio_pin(struct device *dev,
588 struct device_attribute *attr,
589 char *buf)
591 int ret;
592 u8 sfr;
593 struct power_supply *psy = to_power_supply(dev);
594 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
596 ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG);
597 if (ret < 0)
598 return ret;
600 ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
601 return ret;
604 static ssize_t ds2780_set_pio_pin(struct device *dev,
605 struct device_attribute *attr,
606 const char *buf,
607 size_t count)
609 int ret;
610 u8 new_setting;
611 struct power_supply *psy = to_power_supply(dev);
612 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
614 ret = kstrtou8(buf, 0, &new_setting);
615 if (ret < 0)
616 return ret;
618 if ((new_setting != 0) && (new_setting != 1)) {
619 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
620 return -EINVAL;
623 ret = ds2780_write(dev_info, &new_setting,
624 DS2780_SFR_REG, sizeof(u8));
625 if (ret < 0)
626 return ret;
628 return count;
631 static ssize_t ds2780_read_param_eeprom_bin(struct file *filp,
632 struct kobject *kobj,
633 struct bin_attribute *bin_attr,
634 char *buf, loff_t off, size_t count)
636 struct device *dev = container_of(kobj, struct device, kobj);
637 struct power_supply *psy = to_power_supply(dev);
638 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
640 count = min_t(loff_t, count,
641 DS2780_EEPROM_BLOCK1_END -
642 DS2780_EEPROM_BLOCK1_START + 1 - off);
644 return ds2780_read_block(dev_info, buf,
645 DS2780_EEPROM_BLOCK1_START + off, count);
648 static ssize_t ds2780_write_param_eeprom_bin(struct file *filp,
649 struct kobject *kobj,
650 struct bin_attribute *bin_attr,
651 char *buf, loff_t off, size_t count)
653 struct device *dev = container_of(kobj, struct device, kobj);
654 struct power_supply *psy = to_power_supply(dev);
655 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
656 int ret;
658 count = min_t(loff_t, count,
659 DS2780_EEPROM_BLOCK1_END -
660 DS2780_EEPROM_BLOCK1_START + 1 - off);
662 ret = ds2780_write(dev_info, buf,
663 DS2780_EEPROM_BLOCK1_START + off, count);
664 if (ret < 0)
665 return ret;
667 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
668 if (ret < 0)
669 return ret;
671 return count;
674 static struct bin_attribute ds2780_param_eeprom_bin_attr = {
675 .attr = {
676 .name = "param_eeprom",
677 .mode = S_IRUGO | S_IWUSR,
679 .size = DS2780_EEPROM_BLOCK1_END - DS2780_EEPROM_BLOCK1_START + 1,
680 .read = ds2780_read_param_eeprom_bin,
681 .write = ds2780_write_param_eeprom_bin,
684 static ssize_t ds2780_read_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);
693 count = min_t(loff_t, count,
694 DS2780_EEPROM_BLOCK0_END -
695 DS2780_EEPROM_BLOCK0_START + 1 - off);
697 return ds2780_read_block(dev_info, buf,
698 DS2780_EEPROM_BLOCK0_START + off, count);
701 static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
702 struct kobject *kobj,
703 struct bin_attribute *bin_attr,
704 char *buf, loff_t off, size_t count)
706 struct device *dev = container_of(kobj, struct device, kobj);
707 struct power_supply *psy = to_power_supply(dev);
708 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
709 int ret;
711 count = min_t(loff_t, count,
712 DS2780_EEPROM_BLOCK0_END -
713 DS2780_EEPROM_BLOCK0_START + 1 - off);
715 ret = ds2780_write(dev_info, buf,
716 DS2780_EEPROM_BLOCK0_START + off, count);
717 if (ret < 0)
718 return ret;
720 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
721 if (ret < 0)
722 return ret;
724 return count;
727 static struct bin_attribute ds2780_user_eeprom_bin_attr = {
728 .attr = {
729 .name = "user_eeprom",
730 .mode = S_IRUGO | S_IWUSR,
732 .size = DS2780_EEPROM_BLOCK0_END - DS2780_EEPROM_BLOCK0_START + 1,
733 .read = ds2780_read_user_eeprom_bin,
734 .write = ds2780_write_user_eeprom_bin,
737 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
738 ds2780_set_pmod_enabled);
739 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
740 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
741 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
742 ds2780_set_rsgain_setting);
743 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
744 ds2780_set_pio_pin);
747 static struct attribute *ds2780_attributes[] = {
748 &dev_attr_pmod_enabled.attr,
749 &dev_attr_sense_resistor_value.attr,
750 &dev_attr_rsgain_setting.attr,
751 &dev_attr_pio_pin.attr,
752 NULL
755 static const struct attribute_group ds2780_attr_group = {
756 .attrs = ds2780_attributes,
759 static int ds2780_battery_probe(struct platform_device *pdev)
761 struct power_supply_config psy_cfg = {};
762 int ret = 0;
763 struct ds2780_device_info *dev_info;
765 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
766 if (!dev_info) {
767 ret = -ENOMEM;
768 goto fail;
771 platform_set_drvdata(pdev, dev_info);
773 dev_info->dev = &pdev->dev;
774 dev_info->w1_dev = pdev->dev.parent;
775 dev_info->bat_desc.name = dev_name(&pdev->dev);
776 dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
777 dev_info->bat_desc.properties = ds2780_battery_props;
778 dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2780_battery_props);
779 dev_info->bat_desc.get_property = ds2780_battery_get_property;
781 psy_cfg.drv_data = dev_info;
783 dev_info->bat = power_supply_register(&pdev->dev, &dev_info->bat_desc,
784 &psy_cfg);
785 if (IS_ERR(dev_info->bat)) {
786 dev_err(dev_info->dev, "failed to register battery\n");
787 ret = PTR_ERR(dev_info->bat);
788 goto fail;
791 ret = sysfs_create_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
792 if (ret) {
793 dev_err(dev_info->dev, "failed to create sysfs group\n");
794 goto fail_unregister;
797 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
798 &ds2780_param_eeprom_bin_attr);
799 if (ret) {
800 dev_err(dev_info->dev,
801 "failed to create param eeprom bin file");
802 goto fail_remove_group;
805 ret = sysfs_create_bin_file(&dev_info->bat->dev.kobj,
806 &ds2780_user_eeprom_bin_attr);
807 if (ret) {
808 dev_err(dev_info->dev,
809 "failed to create user eeprom bin file");
810 goto fail_remove_bin_file;
813 return 0;
815 fail_remove_bin_file:
816 sysfs_remove_bin_file(&dev_info->bat->dev.kobj,
817 &ds2780_param_eeprom_bin_attr);
818 fail_remove_group:
819 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
820 fail_unregister:
821 power_supply_unregister(dev_info->bat);
822 fail:
823 return ret;
826 static int ds2780_battery_remove(struct platform_device *pdev)
828 struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);
831 * Remove attributes before unregistering power supply
832 * because 'bat' will be freed on power_supply_unregister() call.
834 sysfs_remove_group(&dev_info->bat->dev.kobj, &ds2780_attr_group);
836 power_supply_unregister(dev_info->bat);
838 return 0;
841 static struct platform_driver ds2780_battery_driver = {
842 .driver = {
843 .name = "ds2780-battery",
845 .probe = ds2780_battery_probe,
846 .remove = ds2780_battery_remove,
849 module_platform_driver(ds2780_battery_driver);
851 MODULE_LICENSE("GPL");
852 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
853 MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauage IC driver");
854 MODULE_ALIAS("platform:ds2780-battery");