drm/radeon/kms: move panel mode setup into encoder mode set
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / power / ds2780_battery.c
blob91a783d72360b314094a134d5284a89e751b3ca9
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;
42 struct task_struct *mutex_holder;
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 container_of(psy, struct ds2780_device_info, bat);
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 if (dev_info->mutex_holder == current)
68 return w1_ds2780_io_nolock(dev_info->w1_dev, buf, addr, count, io);
69 else
70 return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
73 static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val,
74 int addr)
76 return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0);
79 static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val,
80 int addr)
82 int ret;
83 u8 raw[2];
85 ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0);
86 if (ret < 0)
87 return ret;
89 *val = (raw[0] << 8) | raw[1];
91 return 0;
94 static inline int ds2780_read_block(struct ds2780_device_info *dev_info,
95 u8 *val, int addr, size_t count)
97 return ds2780_battery_io(dev_info, val, addr, count, 0);
100 static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val,
101 int addr, size_t count)
103 return ds2780_battery_io(dev_info, val, addr, count, 1);
106 static inline int ds2780_store_eeprom(struct device *dev, int addr)
108 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
111 static inline int ds2780_recall_eeprom(struct device *dev, int addr)
113 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
116 static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
118 int ret;
120 ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
121 if (ret < 0)
122 return ret;
124 ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
125 if (ret < 0)
126 return ret;
128 return 0;
131 /* Set sense resistor value in mhos */
132 static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
133 u8 conductance)
135 int ret;
137 ret = ds2780_write(dev_info, &conductance,
138 DS2780_RSNSP_REG, sizeof(u8));
139 if (ret < 0)
140 return ret;
142 return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
145 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
146 static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
147 u16 *rsgain)
149 return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG);
152 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
153 static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
154 u16 rsgain)
156 int ret;
157 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
159 ret = ds2780_write(dev_info, raw,
160 DS2780_RSGAIN_MSB_REG, sizeof(raw));
161 if (ret < 0)
162 return ret;
164 return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
167 static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
168 int *voltage_uV)
170 int ret;
171 s16 voltage_raw;
174 * The voltage value is located in 10 bits across the voltage MSB
175 * and LSB registers in two's compliment form
176 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
177 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
178 * voltage MSB register
179 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
180 * voltage LSB register
182 ret = ds2780_read16(dev_info, &voltage_raw,
183 DS2780_VOLT_MSB_REG);
184 if (ret < 0)
185 return ret;
188 * DS2780 reports voltage in units of 4.88mV, but the battery class
189 * reports in units of uV, so convert by multiplying by 4880.
191 *voltage_uV = (voltage_raw / 32) * 4880;
192 return 0;
195 static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
196 int *temperature)
198 int ret;
199 s16 temperature_raw;
202 * The temperature value is located in 10 bits across the temperature
203 * MSB and LSB registers in two's compliment form
204 * Sign bit of the temperature value is in bit 7 of the temperature
205 * MSB register
206 * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the
207 * temperature MSB register
208 * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the
209 * temperature LSB register
211 ret = ds2780_read16(dev_info, &temperature_raw,
212 DS2780_TEMP_MSB_REG);
213 if (ret < 0)
214 return ret;
217 * Temperature is measured in units of 0.125 degrees celcius, the
218 * power_supply class measures temperature in tenths of degrees
219 * celsius. The temperature value is stored as a 10 bit number, plus
220 * sign in the upper bits of a 16 bit register.
222 *temperature = ((temperature_raw / 32) * 125) / 100;
223 return 0;
226 static int ds2780_get_current(struct ds2780_device_info *dev_info,
227 enum current_types type, int *current_uA)
229 int ret, sense_res;
230 s16 current_raw;
231 u8 sense_res_raw, reg_msb;
234 * The units of measurement for current are dependent on the value of
235 * the sense resistor.
237 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
238 if (ret < 0)
239 return ret;
241 if (sense_res_raw == 0) {
242 dev_err(dev_info->dev, "sense resistor value is 0\n");
243 return -EINVAL;
245 sense_res = 1000 / sense_res_raw;
247 if (type == CURRENT_NOW)
248 reg_msb = DS2780_CURRENT_MSB_REG;
249 else if (type == CURRENT_AVG)
250 reg_msb = DS2780_IAVG_MSB_REG;
251 else
252 return -EINVAL;
255 * The current value is located in 16 bits across the current MSB
256 * and LSB registers in two's compliment form
257 * Sign bit of the current value is in bit 7 of the current MSB register
258 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
259 * MSB register
260 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
261 * LSB register
263 ret = ds2780_read16(dev_info, &current_raw, reg_msb);
264 if (ret < 0)
265 return ret;
267 *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
268 return 0;
271 static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info,
272 int *accumulated_current)
274 int ret, sense_res;
275 s16 current_raw;
276 u8 sense_res_raw;
279 * The units of measurement for accumulated current are dependent on
280 * the value of the sense resistor.
282 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
283 if (ret < 0)
284 return ret;
286 if (sense_res_raw == 0) {
287 dev_err(dev_info->dev, "sense resistor value is 0\n");
288 return -ENXIO;
290 sense_res = 1000 / sense_res_raw;
293 * The ACR value is located in 16 bits across the ACR MSB and
294 * LSB registers
295 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
296 * MSB register
297 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
298 * LSB register
300 ret = ds2780_read16(dev_info, &current_raw, DS2780_ACR_MSB_REG);
301 if (ret < 0)
302 return ret;
304 *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
305 return 0;
308 static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
309 int *capacity)
311 int ret;
312 u8 raw;
314 ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG);
315 if (ret < 0)
316 return ret;
318 *capacity = raw;
319 return raw;
322 static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
324 int ret, current_uA, capacity;
326 ret = ds2780_get_current(dev_info, CURRENT_NOW, &current_uA);
327 if (ret < 0)
328 return ret;
330 ret = ds2780_get_capacity(dev_info, &capacity);
331 if (ret < 0)
332 return ret;
334 if (capacity == 100)
335 *status = POWER_SUPPLY_STATUS_FULL;
336 else if (current_uA == 0)
337 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
338 else if (current_uA < 0)
339 *status = POWER_SUPPLY_STATUS_DISCHARGING;
340 else
341 *status = POWER_SUPPLY_STATUS_CHARGING;
343 return 0;
346 static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
347 int *charge_now)
349 int ret;
350 u16 charge_raw;
353 * The RAAC value is located in 16 bits across the RAAC MSB and
354 * LSB registers
355 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
356 * MSB register
357 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
358 * LSB register
360 ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG);
361 if (ret < 0)
362 return ret;
364 *charge_now = charge_raw * 1600;
365 return 0;
368 static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
369 u8 *control_reg)
371 return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG);
374 static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
375 u8 control_reg)
377 int ret;
379 ret = ds2780_write(dev_info, &control_reg,
380 DS2780_CONTROL_REG, sizeof(u8));
381 if (ret < 0)
382 return ret;
384 return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
387 static int ds2780_battery_get_property(struct power_supply *psy,
388 enum power_supply_property psp,
389 union power_supply_propval *val)
391 int ret = 0;
392 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
394 switch (psp) {
395 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
396 ret = ds2780_get_voltage(dev_info, &val->intval);
397 break;
399 case POWER_SUPPLY_PROP_TEMP:
400 ret = ds2780_get_temperature(dev_info, &val->intval);
401 break;
403 case POWER_SUPPLY_PROP_MODEL_NAME:
404 val->strval = model;
405 break;
407 case POWER_SUPPLY_PROP_MANUFACTURER:
408 val->strval = manufacturer;
409 break;
411 case POWER_SUPPLY_PROP_CURRENT_NOW:
412 ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
413 break;
415 case POWER_SUPPLY_PROP_CURRENT_AVG:
416 ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
417 break;
419 case POWER_SUPPLY_PROP_STATUS:
420 ret = ds2780_get_status(dev_info, &val->intval);
421 break;
423 case POWER_SUPPLY_PROP_CAPACITY:
424 ret = ds2780_get_capacity(dev_info, &val->intval);
425 break;
427 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
428 ret = ds2780_get_accumulated_current(dev_info, &val->intval);
429 break;
431 case POWER_SUPPLY_PROP_CHARGE_NOW:
432 ret = ds2780_get_charge_now(dev_info, &val->intval);
433 break;
435 default:
436 ret = -EINVAL;
439 return ret;
442 static enum power_supply_property ds2780_battery_props[] = {
443 POWER_SUPPLY_PROP_STATUS,
444 POWER_SUPPLY_PROP_VOLTAGE_NOW,
445 POWER_SUPPLY_PROP_TEMP,
446 POWER_SUPPLY_PROP_MODEL_NAME,
447 POWER_SUPPLY_PROP_MANUFACTURER,
448 POWER_SUPPLY_PROP_CURRENT_NOW,
449 POWER_SUPPLY_PROP_CURRENT_AVG,
450 POWER_SUPPLY_PROP_CAPACITY,
451 POWER_SUPPLY_PROP_CHARGE_COUNTER,
452 POWER_SUPPLY_PROP_CHARGE_NOW,
455 static ssize_t ds2780_get_pmod_enabled(struct device *dev,
456 struct device_attribute *attr,
457 char *buf)
459 int ret;
460 u8 control_reg;
461 struct power_supply *psy = to_power_supply(dev);
462 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
464 /* Get power mode */
465 ret = ds2780_get_control_register(dev_info, &control_reg);
466 if (ret < 0)
467 return ret;
469 return sprintf(buf, "%d\n",
470 !!(control_reg & DS2780_CONTROL_REG_PMOD));
473 static ssize_t ds2780_set_pmod_enabled(struct device *dev,
474 struct device_attribute *attr,
475 const char *buf,
476 size_t count)
478 int ret;
479 u8 control_reg, new_setting;
480 struct power_supply *psy = to_power_supply(dev);
481 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
483 /* Set power mode */
484 ret = ds2780_get_control_register(dev_info, &control_reg);
485 if (ret < 0)
486 return ret;
488 ret = kstrtou8(buf, 0, &new_setting);
489 if (ret < 0)
490 return ret;
492 if ((new_setting != 0) && (new_setting != 1)) {
493 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
494 return -EINVAL;
497 if (new_setting)
498 control_reg |= DS2780_CONTROL_REG_PMOD;
499 else
500 control_reg &= ~DS2780_CONTROL_REG_PMOD;
502 ret = ds2780_set_control_register(dev_info, control_reg);
503 if (ret < 0)
504 return ret;
506 return count;
509 static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
510 struct device_attribute *attr,
511 char *buf)
513 int ret;
514 u8 sense_resistor;
515 struct power_supply *psy = to_power_supply(dev);
516 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
518 ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG);
519 if (ret < 0)
520 return ret;
522 ret = sprintf(buf, "%d\n", sense_resistor);
523 return ret;
526 static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
527 struct device_attribute *attr,
528 const char *buf,
529 size_t count)
531 int ret;
532 u8 new_setting;
533 struct power_supply *psy = to_power_supply(dev);
534 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
536 ret = kstrtou8(buf, 0, &new_setting);
537 if (ret < 0)
538 return ret;
540 ret = ds2780_set_sense_register(dev_info, new_setting);
541 if (ret < 0)
542 return ret;
544 return count;
547 static ssize_t ds2780_get_rsgain_setting(struct device *dev,
548 struct device_attribute *attr,
549 char *buf)
551 int ret;
552 u16 rsgain;
553 struct power_supply *psy = to_power_supply(dev);
554 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
556 ret = ds2780_get_rsgain_register(dev_info, &rsgain);
557 if (ret < 0)
558 return ret;
560 return sprintf(buf, "%d\n", rsgain);
563 static ssize_t ds2780_set_rsgain_setting(struct device *dev,
564 struct device_attribute *attr,
565 const char *buf,
566 size_t count)
568 int ret;
569 u16 new_setting;
570 struct power_supply *psy = to_power_supply(dev);
571 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
573 ret = kstrtou16(buf, 0, &new_setting);
574 if (ret < 0)
575 return ret;
577 /* Gain can only be from 0 to 1.999 in steps of .001 */
578 if (new_setting > 1999) {
579 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
580 return -EINVAL;
583 ret = ds2780_set_rsgain_register(dev_info, new_setting);
584 if (ret < 0)
585 return ret;
587 return count;
590 static ssize_t ds2780_get_pio_pin(struct device *dev,
591 struct device_attribute *attr,
592 char *buf)
594 int ret;
595 u8 sfr;
596 struct power_supply *psy = to_power_supply(dev);
597 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
599 ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG);
600 if (ret < 0)
601 return ret;
603 ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
604 return ret;
607 static ssize_t ds2780_set_pio_pin(struct device *dev,
608 struct device_attribute *attr,
609 const char *buf,
610 size_t count)
612 int ret;
613 u8 new_setting;
614 struct power_supply *psy = to_power_supply(dev);
615 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
617 ret = kstrtou8(buf, 0, &new_setting);
618 if (ret < 0)
619 return ret;
621 if ((new_setting != 0) && (new_setting != 1)) {
622 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
623 return -EINVAL;
626 ret = ds2780_write(dev_info, &new_setting,
627 DS2780_SFR_REG, sizeof(u8));
628 if (ret < 0)
629 return ret;
631 return count;
634 static ssize_t ds2780_read_param_eeprom_bin(struct file *filp,
635 struct kobject *kobj,
636 struct bin_attribute *bin_attr,
637 char *buf, loff_t off, size_t count)
639 struct device *dev = container_of(kobj, struct device, kobj);
640 struct power_supply *psy = to_power_supply(dev);
641 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
643 count = min_t(loff_t, count,
644 DS2780_EEPROM_BLOCK1_END -
645 DS2780_EEPROM_BLOCK1_START + 1 - off);
647 return ds2780_read_block(dev_info, buf,
648 DS2780_EEPROM_BLOCK1_START + off, count);
651 static ssize_t ds2780_write_param_eeprom_bin(struct file *filp,
652 struct kobject *kobj,
653 struct bin_attribute *bin_attr,
654 char *buf, loff_t off, size_t count)
656 struct device *dev = container_of(kobj, struct device, kobj);
657 struct power_supply *psy = to_power_supply(dev);
658 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
659 int ret;
661 count = min_t(loff_t, count,
662 DS2780_EEPROM_BLOCK1_END -
663 DS2780_EEPROM_BLOCK1_START + 1 - off);
665 ret = ds2780_write(dev_info, buf,
666 DS2780_EEPROM_BLOCK1_START + off, count);
667 if (ret < 0)
668 return ret;
670 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
671 if (ret < 0)
672 return ret;
674 return count;
677 static struct bin_attribute ds2780_param_eeprom_bin_attr = {
678 .attr = {
679 .name = "param_eeprom",
680 .mode = S_IRUGO | S_IWUSR,
682 .size = DS2780_EEPROM_BLOCK1_END - DS2780_EEPROM_BLOCK1_START + 1,
683 .read = ds2780_read_param_eeprom_bin,
684 .write = ds2780_write_param_eeprom_bin,
687 static ssize_t ds2780_read_user_eeprom_bin(struct file *filp,
688 struct kobject *kobj,
689 struct bin_attribute *bin_attr,
690 char *buf, loff_t off, size_t count)
692 struct device *dev = container_of(kobj, struct device, kobj);
693 struct power_supply *psy = to_power_supply(dev);
694 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
696 count = min_t(loff_t, count,
697 DS2780_EEPROM_BLOCK0_END -
698 DS2780_EEPROM_BLOCK0_START + 1 - off);
700 return ds2780_read_block(dev_info, buf,
701 DS2780_EEPROM_BLOCK0_START + off, count);
704 static ssize_t ds2780_write_user_eeprom_bin(struct file *filp,
705 struct kobject *kobj,
706 struct bin_attribute *bin_attr,
707 char *buf, loff_t off, size_t count)
709 struct device *dev = container_of(kobj, struct device, kobj);
710 struct power_supply *psy = to_power_supply(dev);
711 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
712 int ret;
714 count = min_t(loff_t, count,
715 DS2780_EEPROM_BLOCK0_END -
716 DS2780_EEPROM_BLOCK0_START + 1 - off);
718 ret = ds2780_write(dev_info, buf,
719 DS2780_EEPROM_BLOCK0_START + off, count);
720 if (ret < 0)
721 return ret;
723 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
724 if (ret < 0)
725 return ret;
727 return count;
730 static struct bin_attribute ds2780_user_eeprom_bin_attr = {
731 .attr = {
732 .name = "user_eeprom",
733 .mode = S_IRUGO | S_IWUSR,
735 .size = DS2780_EEPROM_BLOCK0_END - DS2780_EEPROM_BLOCK0_START + 1,
736 .read = ds2780_read_user_eeprom_bin,
737 .write = ds2780_write_user_eeprom_bin,
740 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
741 ds2780_set_pmod_enabled);
742 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
743 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
744 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
745 ds2780_set_rsgain_setting);
746 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
747 ds2780_set_pio_pin);
750 static struct attribute *ds2780_attributes[] = {
751 &dev_attr_pmod_enabled.attr,
752 &dev_attr_sense_resistor_value.attr,
753 &dev_attr_rsgain_setting.attr,
754 &dev_attr_pio_pin.attr,
755 NULL
758 static const struct attribute_group ds2780_attr_group = {
759 .attrs = ds2780_attributes,
762 static int __devinit ds2780_battery_probe(struct platform_device *pdev)
764 int ret = 0;
765 struct ds2780_device_info *dev_info;
767 dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
768 if (!dev_info) {
769 ret = -ENOMEM;
770 goto fail;
773 platform_set_drvdata(pdev, dev_info);
775 dev_info->dev = &pdev->dev;
776 dev_info->w1_dev = pdev->dev.parent;
777 dev_info->bat.name = dev_name(&pdev->dev);
778 dev_info->bat.type = POWER_SUPPLY_TYPE_BATTERY;
779 dev_info->bat.properties = ds2780_battery_props;
780 dev_info->bat.num_properties = ARRAY_SIZE(ds2780_battery_props);
781 dev_info->bat.get_property = ds2780_battery_get_property;
782 dev_info->mutex_holder = current;
784 ret = power_supply_register(&pdev->dev, &dev_info->bat);
785 if (ret) {
786 dev_err(dev_info->dev, "failed to register battery\n");
787 goto fail_free_info;
790 ret = sysfs_create_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
791 if (ret) {
792 dev_err(dev_info->dev, "failed to create sysfs group\n");
793 goto fail_unregister;
796 ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
797 &ds2780_param_eeprom_bin_attr);
798 if (ret) {
799 dev_err(dev_info->dev,
800 "failed to create param eeprom bin file");
801 goto fail_remove_group;
804 ret = sysfs_create_bin_file(&dev_info->bat.dev->kobj,
805 &ds2780_user_eeprom_bin_attr);
806 if (ret) {
807 dev_err(dev_info->dev,
808 "failed to create user eeprom bin file");
809 goto fail_remove_bin_file;
812 dev_info->mutex_holder = NULL;
814 return 0;
816 fail_remove_bin_file:
817 sysfs_remove_bin_file(&dev_info->bat.dev->kobj,
818 &ds2780_param_eeprom_bin_attr);
819 fail_remove_group:
820 sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
821 fail_unregister:
822 power_supply_unregister(&dev_info->bat);
823 fail_free_info:
824 kfree(dev_info);
825 fail:
826 return ret;
829 static int __devexit ds2780_battery_remove(struct platform_device *pdev)
831 struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);
833 dev_info->mutex_holder = current;
835 /* remove attributes */
836 sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);
838 power_supply_unregister(&dev_info->bat);
840 kfree(dev_info);
841 return 0;
844 MODULE_ALIAS("platform:ds2780-battery");
846 static struct platform_driver ds2780_battery_driver = {
847 .driver = {
848 .name = "ds2780-battery",
850 .probe = ds2780_battery_probe,
851 .remove = ds2780_battery_remove,
854 static int __init ds2780_battery_init(void)
856 return platform_driver_register(&ds2780_battery_driver);
859 static void __exit ds2780_battery_exit(void)
861 platform_driver_unregister(&ds2780_battery_driver);
864 module_init(ds2780_battery_init);
865 module_exit(ds2780_battery_exit);
867 MODULE_LICENSE("GPL");
868 MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
869 MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauage IC driver");