EHCI: fix direction handling for interrupt data toggles
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / power / ds2780_battery.c
blob1fefe82e12e3b94f59add3abb7af9db44047f1a8
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 device *w1_dev;
44 enum current_types {
45 CURRENT_NOW,
46 CURRENT_AVG,
49 static const char model[] = "DS2780";
50 static const char manufacturer[] = "Maxim/Dallas";
52 static inline struct ds2780_device_info *to_ds2780_device_info(
53 struct power_supply *psy)
55 return container_of(psy, struct ds2780_device_info, bat);
58 static inline struct power_supply *to_power_supply(struct device *dev)
60 return dev_get_drvdata(dev);
63 static inline int ds2780_read8(struct device *dev, u8 *val, int addr)
65 return w1_ds2780_io(dev, val, addr, sizeof(u8), 0);
68 static int ds2780_read16(struct device *dev, s16 *val, int addr)
70 int ret;
71 u8 raw[2];
73 ret = w1_ds2780_io(dev, raw, addr, sizeof(u8) * 2, 0);
74 if (ret < 0)
75 return ret;
77 *val = (raw[0] << 8) | raw[1];
79 return 0;
82 static inline int ds2780_read_block(struct device *dev, u8 *val, int addr,
83 size_t count)
85 return w1_ds2780_io(dev, val, addr, count, 0);
88 static inline int ds2780_write(struct device *dev, u8 *val, int addr,
89 size_t count)
91 return w1_ds2780_io(dev, val, addr, count, 1);
94 static inline int ds2780_store_eeprom(struct device *dev, int addr)
96 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
99 static inline int ds2780_recall_eeprom(struct device *dev, int addr)
101 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
104 static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
106 int ret;
108 ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
109 if (ret < 0)
110 return ret;
112 ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
113 if (ret < 0)
114 return ret;
116 return 0;
119 /* Set sense resistor value in mhos */
120 static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
121 u8 conductance)
123 int ret;
125 ret = ds2780_write(dev_info->w1_dev, &conductance,
126 DS2780_RSNSP_REG, sizeof(u8));
127 if (ret < 0)
128 return ret;
130 return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
133 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
134 static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
135 u16 *rsgain)
137 return ds2780_read16(dev_info->w1_dev, rsgain, DS2780_RSGAIN_MSB_REG);
140 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
141 static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
142 u16 rsgain)
144 int ret;
145 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
147 ret = ds2780_write(dev_info->w1_dev, raw,
148 DS2780_RSGAIN_MSB_REG, sizeof(u8) * 2);
149 if (ret < 0)
150 return ret;
152 return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
155 static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
156 int *voltage_uV)
158 int ret;
159 s16 voltage_raw;
162 * The voltage value is located in 10 bits across the voltage MSB
163 * and LSB registers in two's compliment form
164 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
165 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
166 * voltage MSB register
167 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
168 * voltage LSB register
170 ret = ds2780_read16(dev_info->w1_dev, &voltage_raw,
171 DS2780_VOLT_MSB_REG);
172 if (ret < 0)
173 return ret;
176 * DS2780 reports voltage in units of 4.88mV, but the battery class
177 * reports in units of uV, so convert by multiplying by 4880.
179 *voltage_uV = (voltage_raw / 32) * 4880;
180 return 0;
183 static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
184 int *temperature)
186 int ret;
187 s16 temperature_raw;
190 * The temperature value is located in 10 bits across the temperature
191 * MSB and LSB registers in two's compliment form
192 * Sign bit of the temperature value is in bit 7 of the temperature
193 * MSB register
194 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
195 * temperature MSB register
196 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
197 * temperature LSB register
199 ret = ds2780_read16(dev_info->w1_dev, &temperature_raw,
200 DS2780_TEMP_MSB_REG);
201 if (ret < 0)
202 return ret;
205 * Temperature is measured in units of 0.125 degrees celcius, the
206 * power_supply class measures temperature in tenths of degrees
207 * celsius. The temperature value is stored as a 10 bit number, plus
208 * sign in the upper bits of a 16 bit register.
210 *temperature = ((temperature_raw / 32) * 125) / 100;
211 return 0;
214 static int ds2780_get_current(struct ds2780_device_info *dev_info,
215 enum current_types type, int *current_uA)
217 int ret, sense_res;
218 s16 current_raw;
219 u8 sense_res_raw, reg_msb;
222 * The units of measurement for current are dependent on the value of
223 * the sense resistor.
225 ret = ds2780_read8(dev_info->w1_dev, &sense_res_raw, DS2780_RSNSP_REG);
226 if (ret < 0)
227 return ret;
229 if (sense_res_raw == 0) {
230 dev_err(dev_info->dev, "sense resistor value is 0\n");
231 return -ENXIO;
233 sense_res = 1000 / sense_res_raw;
235 if (type == CURRENT_NOW)
236 reg_msb = DS2780_CURRENT_MSB_REG;
237 else if (type == CURRENT_AVG)
238 reg_msb = DS2780_IAVG_MSB_REG;
239 else
240 return -EINVAL;
243 * The current value is located in 16 bits across the current MSB
244 * and LSB registers in two's compliment form
245 * Sign bit of the current value is in bit 7 of the current MSB register
246 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
247 * MSB register
248 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
249 * LSB register
251 ret = ds2780_read16(dev_info->w1_dev, &current_raw, reg_msb);
252 if (ret < 0)
253 return ret;
255 *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
256 return 0;
259 static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info,
260 int *accumulated_current)
262 int ret, sense_res;
263 s16 current_raw;
264 u8 sense_res_raw;
267 * The units of measurement for accumulated current are dependent on
268 * the value of the sense resistor.
270 ret = ds2780_read8(dev_info->w1_dev, &sense_res_raw, DS2780_RSNSP_REG);
271 if (ret < 0)
272 return ret;
274 if (sense_res_raw == 0) {
275 dev_err(dev_info->dev, "sense resistor value is 0\n");
276 return -ENXIO;
278 sense_res = 1000 / sense_res_raw;
281 * The ACR value is located in 16 bits across the ACR MSB and
282 * LSB registers
283 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
284 * MSB register
285 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
286 * LSB register
288 ret = ds2780_read16(dev_info->w1_dev, &current_raw, DS2780_ACR_MSB_REG);
289 if (ret < 0)
290 return ret;
292 *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
293 return 0;
296 static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
297 int *capacity)
299 int ret;
300 u8 raw;
302 ret = ds2780_read8(dev_info->w1_dev, &raw, DS2780_RARC_REG);
303 if (ret < 0)
304 return ret;
306 *capacity = raw;
307 return raw;
310 static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
312 int ret, current_uA, capacity;
314 ret = ds2780_get_current(dev_info, CURRENT_NOW, &current_uA);
315 if (ret < 0)
316 return ret;
318 ret = ds2780_get_capacity(dev_info, &capacity);
319 if (ret < 0)
320 return ret;
322 if (capacity == 100)
323 *status = POWER_SUPPLY_STATUS_FULL;
324 else if (current_uA == 0)
325 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
326 else if (current_uA < 0)
327 *status = POWER_SUPPLY_STATUS_DISCHARGING;
328 else
329 *status = POWER_SUPPLY_STATUS_CHARGING;
331 return 0;
334 static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
335 int *charge_now)
337 int ret;
338 u16 charge_raw;
341 * The RAAC value is located in 16 bits across the RAAC MSB and
342 * LSB registers
343 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
344 * MSB register
345 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
346 * LSB register
348 ret = ds2780_read16(dev_info->w1_dev, &charge_raw, DS2780_RAAC_MSB_REG);
349 if (ret < 0)
350 return ret;
352 *charge_now = charge_raw * 1600;
353 return 0;
356 static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
357 u8 *control_reg)
359 return ds2780_read8(dev_info->w1_dev, control_reg, DS2780_CONTROL_REG);
362 static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
363 u8 control_reg)
365 int ret;
367 ret = ds2780_write(dev_info->w1_dev, &control_reg,
368 DS2780_CONTROL_REG, sizeof(u8));
369 if (ret < 0)
370 return ret;
372 return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
375 static int ds2780_battery_get_property(struct power_supply *psy,
376 enum power_supply_property psp,
377 union power_supply_propval *val)
379 int ret = 0;
380 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
382 switch (psp) {
383 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
384 ret = ds2780_get_voltage(dev_info, &val->intval);
385 break;
387 case POWER_SUPPLY_PROP_TEMP:
388 ret = ds2780_get_temperature(dev_info, &val->intval);
389 break;
391 case POWER_SUPPLY_PROP_MODEL_NAME:
392 val->strval = model;
393 break;
395 case POWER_SUPPLY_PROP_MANUFACTURER:
396 val->strval = manufacturer;
397 break;
399 case POWER_SUPPLY_PROP_CURRENT_NOW:
400 ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
401 break;
403 case POWER_SUPPLY_PROP_CURRENT_AVG:
404 ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
405 break;
407 case POWER_SUPPLY_PROP_STATUS:
408 ret = ds2780_get_status(dev_info, &val->intval);
409 break;
411 case POWER_SUPPLY_PROP_CAPACITY:
412 ret = ds2780_get_capacity(dev_info, &val->intval);
413 break;
415 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
416 ret = ds2780_get_accumulated_current(dev_info, &val->intval);
417 break;
419 case POWER_SUPPLY_PROP_CHARGE_NOW:
420 ret = ds2780_get_charge_now(dev_info, &val->intval);
421 break;
423 default:
424 ret = -EINVAL;
427 return ret;
430 static enum power_supply_property ds2780_battery_props[] = {
431 POWER_SUPPLY_PROP_STATUS,
432 POWER_SUPPLY_PROP_VOLTAGE_NOW,
433 POWER_SUPPLY_PROP_TEMP,
434 POWER_SUPPLY_PROP_MODEL_NAME,
435 POWER_SUPPLY_PROP_MANUFACTURER,
436 POWER_SUPPLY_PROP_CURRENT_NOW,
437 POWER_SUPPLY_PROP_CURRENT_AVG,
438 POWER_SUPPLY_PROP_CAPACITY,
439 POWER_SUPPLY_PROP_CHARGE_COUNTER,
440 POWER_SUPPLY_PROP_CHARGE_NOW,
443 static ssize_t ds2780_get_pmod_enabled(struct device *dev,
444 struct device_attribute *attr,
445 char *buf)
447 int ret;
448 u8 control_reg;
449 struct power_supply *psy = to_power_supply(dev);
450 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
452 /* Get power mode */
453 ret = ds2780_get_control_register(dev_info, &control_reg);
454 if (ret < 0)
455 return ret;
457 return sprintf(buf, "%d\n",
458 !!(control_reg & DS2780_CONTROL_REG_PMOD));
461 static ssize_t ds2780_set_pmod_enabled(struct device *dev,
462 struct device_attribute *attr,
463 const char *buf,
464 size_t count)
466 int ret;
467 u8 control_reg, new_setting;
468 struct power_supply *psy = to_power_supply(dev);
469 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
471 /* Set power mode */
472 ret = ds2780_get_control_register(dev_info, &control_reg);
473 if (ret < 0)
474 return ret;
476 ret = kstrtou8(buf, 0, &new_setting);
477 if (ret < 0)
478 return ret;
480 if ((new_setting != 0) && (new_setting != 1)) {
481 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
482 return -EINVAL;
485 if (new_setting)
486 control_reg |= DS2780_CONTROL_REG_PMOD;
487 else
488 control_reg &= ~DS2780_CONTROL_REG_PMOD;
490 ret = ds2780_set_control_register(dev_info, control_reg);
491 if (ret < 0)
492 return ret;
494 return count;
497 static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
498 struct device_attribute *attr,
499 char *buf)
501 int ret;
502 u8 sense_resistor;
503 struct power_supply *psy = to_power_supply(dev);
504 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
506 ret = ds2780_read8(dev_info->w1_dev, &sense_resistor, DS2780_RSNSP_REG);
507 if (ret < 0)
508 return ret;
510 ret = sprintf(buf, "%d\n", sense_resistor);
511 return ret;
514 static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
515 struct device_attribute *attr,
516 const char *buf,
517 size_t count)
519 int ret;
520 u8 new_setting;
521 struct power_supply *psy = to_power_supply(dev);
522 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
524 ret = kstrtou8(buf, 0, &new_setting);
525 if (ret < 0)
526 return ret;
528 ret = ds2780_set_sense_register(dev_info, new_setting);
529 if (ret < 0)
530 return ret;
532 return count;
535 static ssize_t ds2780_get_rsgain_setting(struct device *dev,
536 struct device_attribute *attr,
537 char *buf)
539 int ret;
540 u16 rsgain;
541 struct power_supply *psy = to_power_supply(dev);
542 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
544 ret = ds2780_get_rsgain_register(dev_info, &rsgain);
545 if (ret < 0)
546 return ret;
548 return sprintf(buf, "%d\n", rsgain);
551 static ssize_t ds2780_set_rsgain_setting(struct device *dev,
552 struct device_attribute *attr,
553 const char *buf,
554 size_t count)
556 int ret;
557 u16 new_setting;
558 struct power_supply *psy = to_power_supply(dev);
559 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
561 ret = kstrtou16(buf, 0, &new_setting);
562 if (ret < 0)
563 return ret;
565 /* Gain can only be from 0 to 1.999 in steps of .001 */
566 if (new_setting > 1999) {
567 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
568 return -EINVAL;
571 ret = ds2780_set_rsgain_register(dev_info, new_setting);
572 if (ret < 0)
573 return ret;
575 return count;
578 static ssize_t ds2780_get_pio_pin(struct device *dev,
579 struct device_attribute *attr,
580 char *buf)
582 int ret;
583 u8 sfr;
584 struct power_supply *psy = to_power_supply(dev);
585 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
587 ret = ds2780_read8(dev_info->w1_dev, &sfr, DS2780_SFR_REG);
588 if (ret < 0)
589 return ret;
591 ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
592 return ret;
595 static ssize_t ds2780_set_pio_pin(struct device *dev,
596 struct device_attribute *attr,
597 const char *buf,
598 size_t count)
600 int ret;
601 u8 new_setting;
602 struct power_supply *psy = to_power_supply(dev);
603 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
605 ret = kstrtou8(buf, 0, &new_setting);
606 if (ret < 0)
607 return ret;
609 if ((new_setting != 0) && (new_setting != 1)) {
610 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
611 return -EINVAL;
614 ret = ds2780_write(dev_info->w1_dev, &new_setting,
615 DS2780_SFR_REG, sizeof(u8));
616 if (ret < 0)
617 return ret;
619 return count;
622 static ssize_t ds2780_read_param_eeprom_bin(struct file *filp,
623 struct kobject *kobj,
624 struct bin_attribute *bin_attr,
625 char *buf, loff_t off, size_t count)
627 struct device *dev = container_of(kobj, struct device, kobj);
628 struct power_supply *psy = to_power_supply(dev);
629 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
631 count = min_t(loff_t, count,
632 DS2780_EEPROM_BLOCK1_END -
633 DS2780_EEPROM_BLOCK1_START + 1 - off);
635 return ds2780_read_block(dev_info->w1_dev, 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 count = min_t(loff_t, count,
650 DS2780_EEPROM_BLOCK1_END -
651 DS2780_EEPROM_BLOCK1_START + 1 - off);
653 ret = ds2780_write(dev_info->w1_dev, buf,
654 DS2780_EEPROM_BLOCK1_START + off, count);
655 if (ret < 0)
656 return ret;
658 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
659 if (ret < 0)
660 return ret;
662 return count;
665 static struct bin_attribute ds2780_param_eeprom_bin_attr = {
666 .attr = {
667 .name = "param_eeprom",
668 .mode = S_IRUGO | S_IWUSR,
670 .size = DS2780_EEPROM_BLOCK1_END - DS2780_EEPROM_BLOCK1_START + 1,
671 .read = ds2780_read_param_eeprom_bin,
672 .write = ds2780_write_param_eeprom_bin,
675 static ssize_t ds2780_read_user_eeprom_bin(struct file *filp,
676 struct kobject *kobj,
677 struct bin_attribute *bin_attr,
678 char *buf, loff_t off, size_t count)
680 struct device *dev = container_of(kobj, struct device, kobj);
681 struct power_supply *psy = to_power_supply(dev);
682 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
684 count = min_t(loff_t, count,
685 DS2780_EEPROM_BLOCK0_END -
686 DS2780_EEPROM_BLOCK0_START + 1 - off);
688 return ds2780_read_block(dev_info->w1_dev, buf,
689 DS2780_EEPROM_BLOCK0_START + off, count);
693 static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
694 struct kobject *kobj,
695 struct bin_attribute *bin_attr,
696 char *buf, loff_t off, size_t count)
698 struct device *dev = container_of(kobj, struct device, kobj);
699 struct power_supply *psy = to_power_supply(dev);
700 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
701 int ret;
703 count = min_t(loff_t, count,
704 DS2780_EEPROM_BLOCK0_END -
705 DS2780_EEPROM_BLOCK0_START + 1 - off);
707 ret = ds2780_write(dev_info->w1_dev, buf,
708 DS2780_EEPROM_BLOCK0_START + off, count);
709 if (ret < 0)
710 return ret;
712 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
713 if (ret < 0)
714 return ret;
716 return count;
719 static struct bin_attribute ds2780_user_eeprom_bin_attr = {
720 .attr = {
721 .name = "user_eeprom",
722 .mode = S_IRUGO | S_IWUSR,
724 .size = DS2780_EEPROM_BLOCK0_END - DS2780_EEPROM_BLOCK0_START + 1,
725 .read = ds2780_read_user_eeprom_bin,
726 .write = ds2780_write_user_eeprom_bin,
729 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
730 ds2780_set_pmod_enabled);
731 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
732 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
733 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
734 ds2780_set_rsgain_setting);
735 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
736 ds2780_set_pio_pin);
739 static struct attribute *ds2780_attributes[] = {
740 &dev_attr_pmod_enabled.attr,
741 &dev_attr_sense_resistor_value.attr,
742 &dev_attr_rsgain_setting.attr,
743 &dev_attr_pio_pin.attr,
744 NULL
747 static const struct attribute_group ds2780_attr_group = {
748 .attrs = ds2780_attributes,
751 static int __devinit ds2780_battery_probe(struct platform_device *pdev)
753 int ret = 0;
754 struct ds2780_device_info *dev_info;
756 dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
757 if (!dev_info) {
758 ret = -ENOMEM;
759 goto fail;
762 platform_set_drvdata(pdev, dev_info);
764 dev_info->dev = &pdev->dev;
765 dev_info->w1_dev = pdev->dev.parent;
766 dev_info->bat.name = dev_name(&pdev->dev);
767 dev_info->bat.type = POWER_SUPPLY_TYPE_BATTERY;
768 dev_info->bat.properties = ds2780_battery_props;
769 dev_info->bat.num_properties = ARRAY_SIZE(ds2780_battery_props);
770 dev_info->bat.get_property = ds2780_battery_get_property;
772 ret = power_supply_register(&pdev->dev, &dev_info->bat);
773 if (ret) {
774 dev_err(dev_info->dev, "failed to register battery\n");
775 goto fail_free_info;
778 ret = sysfs_create_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
779 if (ret) {
780 dev_err(dev_info->dev, "failed to create sysfs group\n");
781 goto fail_unregister;
784 ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
785 &ds2780_param_eeprom_bin_attr);
786 if (ret) {
787 dev_err(dev_info->dev,
788 "failed to create param eeprom bin file");
789 goto fail_remove_group;
792 ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
793 &ds2780_user_eeprom_bin_attr);
794 if (ret) {
795 dev_err(dev_info->dev,
796 "failed to create user eeprom bin file");
797 goto fail_remove_bin_file;
800 return 0;
802 fail_remove_bin_file:
803 sysfs_remove_bin_file(&dev_info->bat.dev->kobj,
804 &ds2780_param_eeprom_bin_attr);
805 fail_remove_group:
806 sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
807 fail_unregister:
808 power_supply_unregister(&dev_info->bat);
809 fail_free_info:
810 kfree(dev_info);
811 fail:
812 return ret;
815 static int __devexit ds2780_battery_remove(struct platform_device *pdev)
817 struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);
819 /* remove attributes */
820 sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
822 power_supply_unregister(&dev_info->bat);
824 kfree(dev_info);
825 return 0;
828 MODULE_ALIAS("platform:ds2780-battery");
830 static struct platform_driver ds2780_battery_driver = {
831 .driver = {
832 .name = "ds2780-battery",
834 .probe = ds2780_battery_probe,
835 .remove = ds2780_battery_remove,
838 static int __init ds2780_battery_init(void)
840 return platform_driver_register(&ds2780_battery_driver);
843 static void __exit ds2780_battery_exit(void)
845 platform_driver_unregister(&ds2780_battery_driver);
848 module_init(ds2780_battery_init);
849 module_exit(ds2780_battery_exit);
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");