treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / power / supply / ds2781_battery.c
blob130cbdfc14eb2020c20f555d02b32900b0fea2b7
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * 1-wire client/driver for the Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC
5 * Author: Renata Sayakhova <renata@oktetlabs.ru>
7 * Based on ds2780_battery drivers
8 */
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/param.h>
13 #include <linux/pm.h>
14 #include <linux/platform_device.h>
15 #include <linux/power_supply.h>
16 #include <linux/idr.h>
18 #include <linux/w1.h>
19 #include "../../w1/slaves/w1_ds2781.h"
21 /* Current unit measurement in uA for a 1 milli-ohm sense resistor */
22 #define DS2781_CURRENT_UNITS 1563
23 /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */
24 #define DS2781_CHARGE_UNITS 6250
25 /* Number of bytes in user EEPROM space */
26 #define DS2781_USER_EEPROM_SIZE (DS2781_EEPROM_BLOCK0_END - \
27 DS2781_EEPROM_BLOCK0_START + 1)
28 /* Number of bytes in parameter EEPROM space */
29 #define DS2781_PARAM_EEPROM_SIZE (DS2781_EEPROM_BLOCK1_END - \
30 DS2781_EEPROM_BLOCK1_START + 1)
32 struct ds2781_device_info {
33 struct device *dev;
34 struct power_supply *bat;
35 struct power_supply_desc bat_desc;
36 struct device *w1_dev;
39 enum current_types {
40 CURRENT_NOW,
41 CURRENT_AVG,
44 static const char model[] = "DS2781";
45 static const char manufacturer[] = "Maxim/Dallas";
47 static inline struct ds2781_device_info *
48 to_ds2781_device_info(struct power_supply *psy)
50 return power_supply_get_drvdata(psy);
53 static inline int ds2781_battery_io(struct ds2781_device_info *dev_info,
54 char *buf, int addr, size_t count, int io)
56 return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io);
59 static int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf,
60 int addr, size_t count)
62 return ds2781_battery_io(dev_info, buf, addr, count, 0);
65 static inline int ds2781_read8(struct ds2781_device_info *dev_info, u8 *val,
66 int addr)
68 return ds2781_battery_io(dev_info, val, addr, sizeof(u8), 0);
71 static int ds2781_read16(struct ds2781_device_info *dev_info, s16 *val,
72 int addr)
74 int ret;
75 u8 raw[2];
77 ret = ds2781_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 ds2781_read_block(struct ds2781_device_info *dev_info,
87 u8 *val, int addr, size_t count)
89 return ds2781_battery_io(dev_info, val, addr, count, 0);
92 static inline int ds2781_write(struct ds2781_device_info *dev_info, u8 *val,
93 int addr, size_t count)
95 return ds2781_battery_io(dev_info, val, addr, count, 1);
98 static inline int ds2781_store_eeprom(struct device *dev, int addr)
100 return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_COPY_DATA);
103 static inline int ds2781_recall_eeprom(struct device *dev, int addr)
105 return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_RECALL_DATA);
108 static int ds2781_save_eeprom(struct ds2781_device_info *dev_info, int reg)
110 int ret;
112 ret = ds2781_store_eeprom(dev_info->w1_dev, reg);
113 if (ret < 0)
114 return ret;
116 ret = ds2781_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 ds2781_set_sense_register(struct ds2781_device_info *dev_info,
125 u8 conductance)
127 int ret;
129 ret = ds2781_write(dev_info, &conductance,
130 DS2781_RSNSP, sizeof(u8));
131 if (ret < 0)
132 return ret;
134 return ds2781_save_eeprom(dev_info, DS2781_RSNSP);
137 /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
138 static int ds2781_get_rsgain_register(struct ds2781_device_info *dev_info,
139 u16 *rsgain)
141 return ds2781_read16(dev_info, rsgain, DS2781_RSGAIN_MSB);
144 /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
145 static int ds2781_set_rsgain_register(struct ds2781_device_info *dev_info,
146 u16 rsgain)
148 int ret;
149 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
151 ret = ds2781_write(dev_info, raw,
152 DS2781_RSGAIN_MSB, sizeof(raw));
153 if (ret < 0)
154 return ret;
156 return ds2781_save_eeprom(dev_info, DS2781_RSGAIN_MSB);
159 static int ds2781_get_voltage(struct ds2781_device_info *dev_info,
160 int *voltage_uV)
162 int ret;
163 char val[2];
164 int voltage_raw;
166 ret = w1_ds2781_read(dev_info, val, DS2781_VOLT_MSB, 2 * sizeof(u8));
167 if (ret < 0)
168 return ret;
170 * The voltage value is located in 10 bits across the voltage MSB
171 * and LSB registers in two's compliment form
172 * Sign bit of the voltage value is in bit 7 of the voltage MSB register
173 * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the
174 * voltage MSB register
175 * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the
176 * voltage LSB register
178 voltage_raw = (val[0] << 3) |
179 (val[1] >> 5);
181 /* DS2781 reports voltage in units of 9.76mV, but the battery class
182 * reports in units of uV, so convert by multiplying by 9760. */
183 *voltage_uV = voltage_raw * 9760;
185 return 0;
188 static int ds2781_get_temperature(struct ds2781_device_info *dev_info,
189 int *temp)
191 int ret;
192 char val[2];
193 int temp_raw;
195 ret = w1_ds2781_read(dev_info, val, DS2781_TEMP_MSB, 2 * sizeof(u8));
196 if (ret < 0)
197 return ret;
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 temp_raw = ((val[0]) << 3) |
209 (val[1] >> 5);
210 *temp = temp_raw + (temp_raw / 4);
212 return 0;
215 static int ds2781_get_current(struct ds2781_device_info *dev_info,
216 enum current_types type, int *current_uA)
218 int ret, sense_res;
219 s16 current_raw;
220 u8 sense_res_raw, reg_msb;
223 * The units of measurement for current are dependent on the value of
224 * the sense resistor.
226 ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
227 if (ret < 0)
228 return ret;
230 if (sense_res_raw == 0) {
231 dev_err(dev_info->dev, "sense resistor value is 0\n");
232 return -EINVAL;
234 sense_res = 1000 / sense_res_raw;
236 if (type == CURRENT_NOW)
237 reg_msb = DS2781_CURRENT_MSB;
238 else if (type == CURRENT_AVG)
239 reg_msb = DS2781_IAVG_MSB;
240 else
241 return -EINVAL;
244 * The current value is located in 16 bits across the current MSB
245 * and LSB registers in two's compliment form
246 * Sign bit of the current value is in bit 7 of the current MSB register
247 * Bits 14 - 8 of the current value are in bits 6 - 0 of the current
248 * MSB register
249 * Bits 7 - 0 of the current value are in bits 7 - 0 of the current
250 * LSB register
252 ret = ds2781_read16(dev_info, &current_raw, reg_msb);
253 if (ret < 0)
254 return ret;
256 *current_uA = current_raw * (DS2781_CURRENT_UNITS / sense_res);
257 return 0;
260 static int ds2781_get_accumulated_current(struct ds2781_device_info *dev_info,
261 int *accumulated_current)
263 int ret, sense_res;
264 s16 current_raw;
265 u8 sense_res_raw;
268 * The units of measurement for accumulated current are dependent on
269 * the value of the sense resistor.
271 ret = ds2781_read8(dev_info, &sense_res_raw, DS2781_RSNSP);
272 if (ret < 0)
273 return ret;
275 if (sense_res_raw == 0) {
276 dev_err(dev_info->dev, "sense resistor value is 0\n");
277 return -EINVAL;
279 sense_res = 1000 / sense_res_raw;
282 * The ACR value is located in 16 bits across the ACR MSB and
283 * LSB registers
284 * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR
285 * MSB register
286 * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR
287 * LSB register
289 ret = ds2781_read16(dev_info, &current_raw, DS2781_ACR_MSB);
290 if (ret < 0)
291 return ret;
293 *accumulated_current = current_raw * (DS2781_CHARGE_UNITS / sense_res);
294 return 0;
297 static int ds2781_get_capacity(struct ds2781_device_info *dev_info,
298 int *capacity)
300 int ret;
301 u8 raw;
303 ret = ds2781_read8(dev_info, &raw, DS2781_RARC);
304 if (ret < 0)
305 return ret;
307 *capacity = raw;
308 return 0;
311 static int ds2781_get_status(struct ds2781_device_info *dev_info, int *status)
313 int ret, current_uA, capacity;
315 ret = ds2781_get_current(dev_info, CURRENT_NOW, &current_uA);
316 if (ret < 0)
317 return ret;
319 ret = ds2781_get_capacity(dev_info, &capacity);
320 if (ret < 0)
321 return ret;
323 if (power_supply_am_i_supplied(dev_info->bat)) {
324 if (capacity == 100)
325 *status = POWER_SUPPLY_STATUS_FULL;
326 else if (current_uA > 50000)
327 *status = POWER_SUPPLY_STATUS_CHARGING;
328 else
329 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
330 } else {
331 *status = POWER_SUPPLY_STATUS_DISCHARGING;
333 return 0;
336 static int ds2781_get_charge_now(struct ds2781_device_info *dev_info,
337 int *charge_now)
339 int ret;
340 u16 charge_raw;
343 * The RAAC value is located in 16 bits across the RAAC MSB and
344 * LSB registers
345 * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC
346 * MSB register
347 * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC
348 * LSB register
350 ret = ds2781_read16(dev_info, &charge_raw, DS2781_RAAC_MSB);
351 if (ret < 0)
352 return ret;
354 *charge_now = charge_raw * 1600;
355 return 0;
358 static int ds2781_get_control_register(struct ds2781_device_info *dev_info,
359 u8 *control_reg)
361 return ds2781_read8(dev_info, control_reg, DS2781_CONTROL);
364 static int ds2781_set_control_register(struct ds2781_device_info *dev_info,
365 u8 control_reg)
367 int ret;
369 ret = ds2781_write(dev_info, &control_reg,
370 DS2781_CONTROL, sizeof(u8));
371 if (ret < 0)
372 return ret;
374 return ds2781_save_eeprom(dev_info, DS2781_CONTROL);
377 static int ds2781_battery_get_property(struct power_supply *psy,
378 enum power_supply_property psp,
379 union power_supply_propval *val)
381 int ret = 0;
382 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
384 switch (psp) {
385 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
386 ret = ds2781_get_voltage(dev_info, &val->intval);
387 break;
389 case POWER_SUPPLY_PROP_TEMP:
390 ret = ds2781_get_temperature(dev_info, &val->intval);
391 break;
393 case POWER_SUPPLY_PROP_MODEL_NAME:
394 val->strval = model;
395 break;
397 case POWER_SUPPLY_PROP_MANUFACTURER:
398 val->strval = manufacturer;
399 break;
401 case POWER_SUPPLY_PROP_CURRENT_NOW:
402 ret = ds2781_get_current(dev_info, CURRENT_NOW, &val->intval);
403 break;
405 case POWER_SUPPLY_PROP_CURRENT_AVG:
406 ret = ds2781_get_current(dev_info, CURRENT_AVG, &val->intval);
407 break;
409 case POWER_SUPPLY_PROP_STATUS:
410 ret = ds2781_get_status(dev_info, &val->intval);
411 break;
413 case POWER_SUPPLY_PROP_CAPACITY:
414 ret = ds2781_get_capacity(dev_info, &val->intval);
415 break;
417 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
418 ret = ds2781_get_accumulated_current(dev_info, &val->intval);
419 break;
421 case POWER_SUPPLY_PROP_CHARGE_NOW:
422 ret = ds2781_get_charge_now(dev_info, &val->intval);
423 break;
425 default:
426 ret = -EINVAL;
429 return ret;
432 static enum power_supply_property ds2781_battery_props[] = {
433 POWER_SUPPLY_PROP_STATUS,
434 POWER_SUPPLY_PROP_VOLTAGE_NOW,
435 POWER_SUPPLY_PROP_TEMP,
436 POWER_SUPPLY_PROP_MODEL_NAME,
437 POWER_SUPPLY_PROP_MANUFACTURER,
438 POWER_SUPPLY_PROP_CURRENT_NOW,
439 POWER_SUPPLY_PROP_CURRENT_AVG,
440 POWER_SUPPLY_PROP_CAPACITY,
441 POWER_SUPPLY_PROP_CHARGE_COUNTER,
442 POWER_SUPPLY_PROP_CHARGE_NOW,
445 static ssize_t ds2781_get_pmod_enabled(struct device *dev,
446 struct device_attribute *attr,
447 char *buf)
449 int ret;
450 u8 control_reg;
451 struct power_supply *psy = to_power_supply(dev);
452 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
454 /* Get power mode */
455 ret = ds2781_get_control_register(dev_info, &control_reg);
456 if (ret < 0)
457 return ret;
459 return sprintf(buf, "%d\n",
460 !!(control_reg & DS2781_CONTROL_PMOD));
463 static ssize_t ds2781_set_pmod_enabled(struct device *dev,
464 struct device_attribute *attr,
465 const char *buf,
466 size_t count)
468 int ret;
469 u8 control_reg, new_setting;
470 struct power_supply *psy = to_power_supply(dev);
471 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
473 /* Set power mode */
474 ret = ds2781_get_control_register(dev_info, &control_reg);
475 if (ret < 0)
476 return ret;
478 ret = kstrtou8(buf, 0, &new_setting);
479 if (ret < 0)
480 return ret;
482 if ((new_setting != 0) && (new_setting != 1)) {
483 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
484 return -EINVAL;
487 if (new_setting)
488 control_reg |= DS2781_CONTROL_PMOD;
489 else
490 control_reg &= ~DS2781_CONTROL_PMOD;
492 ret = ds2781_set_control_register(dev_info, control_reg);
493 if (ret < 0)
494 return ret;
496 return count;
499 static ssize_t ds2781_get_sense_resistor_value(struct device *dev,
500 struct device_attribute *attr,
501 char *buf)
503 int ret;
504 u8 sense_resistor;
505 struct power_supply *psy = to_power_supply(dev);
506 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
508 ret = ds2781_read8(dev_info, &sense_resistor, DS2781_RSNSP);
509 if (ret < 0)
510 return ret;
512 ret = sprintf(buf, "%d\n", sense_resistor);
513 return ret;
516 static ssize_t ds2781_set_sense_resistor_value(struct device *dev,
517 struct device_attribute *attr,
518 const char *buf,
519 size_t count)
521 int ret;
522 u8 new_setting;
523 struct power_supply *psy = to_power_supply(dev);
524 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
526 ret = kstrtou8(buf, 0, &new_setting);
527 if (ret < 0)
528 return ret;
530 ret = ds2781_set_sense_register(dev_info, new_setting);
531 if (ret < 0)
532 return ret;
534 return count;
537 static ssize_t ds2781_get_rsgain_setting(struct device *dev,
538 struct device_attribute *attr,
539 char *buf)
541 int ret;
542 u16 rsgain;
543 struct power_supply *psy = to_power_supply(dev);
544 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
546 ret = ds2781_get_rsgain_register(dev_info, &rsgain);
547 if (ret < 0)
548 return ret;
550 return sprintf(buf, "%d\n", rsgain);
553 static ssize_t ds2781_set_rsgain_setting(struct device *dev,
554 struct device_attribute *attr,
555 const char *buf,
556 size_t count)
558 int ret;
559 u16 new_setting;
560 struct power_supply *psy = to_power_supply(dev);
561 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
563 ret = kstrtou16(buf, 0, &new_setting);
564 if (ret < 0)
565 return ret;
567 /* Gain can only be from 0 to 1.999 in steps of .001 */
568 if (new_setting > 1999) {
569 dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n");
570 return -EINVAL;
573 ret = ds2781_set_rsgain_register(dev_info, new_setting);
574 if (ret < 0)
575 return ret;
577 return count;
580 static ssize_t ds2781_get_pio_pin(struct device *dev,
581 struct device_attribute *attr,
582 char *buf)
584 int ret;
585 u8 sfr;
586 struct power_supply *psy = to_power_supply(dev);
587 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
589 ret = ds2781_read8(dev_info, &sfr, DS2781_SFR);
590 if (ret < 0)
591 return ret;
593 ret = sprintf(buf, "%d\n", sfr & DS2781_SFR_PIOSC);
594 return ret;
597 static ssize_t ds2781_set_pio_pin(struct device *dev,
598 struct device_attribute *attr,
599 const char *buf,
600 size_t count)
602 int ret;
603 u8 new_setting;
604 struct power_supply *psy = to_power_supply(dev);
605 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
607 ret = kstrtou8(buf, 0, &new_setting);
608 if (ret < 0)
609 return ret;
611 if ((new_setting != 0) && (new_setting != 1)) {
612 dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n");
613 return -EINVAL;
616 ret = ds2781_write(dev_info, &new_setting,
617 DS2781_SFR, sizeof(u8));
618 if (ret < 0)
619 return ret;
621 return count;
624 static ssize_t ds2781_read_param_eeprom_bin(struct file *filp,
625 struct kobject *kobj,
626 struct bin_attribute *bin_attr,
627 char *buf, loff_t off, size_t count)
629 struct device *dev = container_of(kobj, struct device, kobj);
630 struct power_supply *psy = to_power_supply(dev);
631 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
633 return ds2781_read_block(dev_info, buf,
634 DS2781_EEPROM_BLOCK1_START + off, count);
637 static ssize_t ds2781_write_param_eeprom_bin(struct file *filp,
638 struct kobject *kobj,
639 struct bin_attribute *bin_attr,
640 char *buf, loff_t off, size_t count)
642 struct device *dev = container_of(kobj, struct device, kobj);
643 struct power_supply *psy = to_power_supply(dev);
644 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
645 int ret;
647 ret = ds2781_write(dev_info, buf,
648 DS2781_EEPROM_BLOCK1_START + off, count);
649 if (ret < 0)
650 return ret;
652 ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK1_START);
653 if (ret < 0)
654 return ret;
656 return count;
659 static struct bin_attribute ds2781_param_eeprom_bin_attr = {
660 .attr = {
661 .name = "param_eeprom",
662 .mode = S_IRUGO | S_IWUSR,
664 .size = DS2781_PARAM_EEPROM_SIZE,
665 .read = ds2781_read_param_eeprom_bin,
666 .write = ds2781_write_param_eeprom_bin,
669 static ssize_t ds2781_read_user_eeprom_bin(struct file *filp,
670 struct kobject *kobj,
671 struct bin_attribute *bin_attr,
672 char *buf, loff_t off, size_t count)
674 struct device *dev = container_of(kobj, struct device, kobj);
675 struct power_supply *psy = to_power_supply(dev);
676 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
678 return ds2781_read_block(dev_info, buf,
679 DS2781_EEPROM_BLOCK0_START + off, count);
683 static ssize_t ds2781_write_user_eeprom_bin(struct file *filp,
684 struct kobject *kobj,
685 struct bin_attribute *bin_attr,
686 char *buf, loff_t off, size_t count)
688 struct device *dev = container_of(kobj, struct device, kobj);
689 struct power_supply *psy = to_power_supply(dev);
690 struct ds2781_device_info *dev_info = to_ds2781_device_info(psy);
691 int ret;
693 ret = ds2781_write(dev_info, buf,
694 DS2781_EEPROM_BLOCK0_START + off, count);
695 if (ret < 0)
696 return ret;
698 ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK0_START);
699 if (ret < 0)
700 return ret;
702 return count;
705 static struct bin_attribute ds2781_user_eeprom_bin_attr = {
706 .attr = {
707 .name = "user_eeprom",
708 .mode = S_IRUGO | S_IWUSR,
710 .size = DS2781_USER_EEPROM_SIZE,
711 .read = ds2781_read_user_eeprom_bin,
712 .write = ds2781_write_user_eeprom_bin,
715 static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2781_get_pmod_enabled,
716 ds2781_set_pmod_enabled);
717 static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
718 ds2781_get_sense_resistor_value, ds2781_set_sense_resistor_value);
719 static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2781_get_rsgain_setting,
720 ds2781_set_rsgain_setting);
721 static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2781_get_pio_pin,
722 ds2781_set_pio_pin);
724 static struct attribute *ds2781_sysfs_attrs[] = {
725 &dev_attr_pmod_enabled.attr,
726 &dev_attr_sense_resistor_value.attr,
727 &dev_attr_rsgain_setting.attr,
728 &dev_attr_pio_pin.attr,
729 NULL
732 static struct bin_attribute *ds2781_sysfs_bin_attrs[] = {
733 &ds2781_param_eeprom_bin_attr,
734 &ds2781_user_eeprom_bin_attr,
735 NULL,
738 static const struct attribute_group ds2781_sysfs_group = {
739 .attrs = ds2781_sysfs_attrs,
740 .bin_attrs = ds2781_sysfs_bin_attrs,
744 static const struct attribute_group *ds2781_sysfs_groups[] = {
745 &ds2781_sysfs_group,
746 NULL,
749 static int ds2781_battery_probe(struct platform_device *pdev)
751 struct power_supply_config psy_cfg = {};
752 struct ds2781_device_info *dev_info;
754 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
755 if (!dev_info)
756 return -ENOMEM;
758 platform_set_drvdata(pdev, dev_info);
760 dev_info->dev = &pdev->dev;
761 dev_info->w1_dev = pdev->dev.parent;
762 dev_info->bat_desc.name = dev_name(&pdev->dev);
763 dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
764 dev_info->bat_desc.properties = ds2781_battery_props;
765 dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2781_battery_props);
766 dev_info->bat_desc.get_property = ds2781_battery_get_property;
768 psy_cfg.drv_data = dev_info;
769 psy_cfg.attr_grp = ds2781_sysfs_groups;
771 dev_info->bat = devm_power_supply_register(&pdev->dev,
772 &dev_info->bat_desc,
773 &psy_cfg);
774 if (IS_ERR(dev_info->bat)) {
775 dev_err(dev_info->dev, "failed to register battery\n");
776 return PTR_ERR(dev_info->bat);
779 return 0;
782 static struct platform_driver ds2781_battery_driver = {
783 .driver = {
784 .name = "ds2781-battery",
786 .probe = ds2781_battery_probe,
788 module_platform_driver(ds2781_battery_driver);
790 MODULE_LICENSE("GPL");
791 MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>");
792 MODULE_DESCRIPTION("Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC driver");
793 MODULE_ALIAS("platform:ds2781-battery");