Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * 1-wire client/driver for the Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC
3 *
4 * Copyright (C) 2010 Indesign, LLC
5 *
6 * Author: Clifton Barnes <cabarnes@indesign-llc.com>
7 *
8 * Based on ds2760_battery and ds2782_battery drivers
9 *
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.
13 *
14 */
15
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>
23
24#include <linux/w1.h>
25#include "../../w1/slaves/w1_ds2780.h"
26
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)
37
38struct ds2780_device_info {
39 struct device *dev;
40 struct power_supply *bat;
41 struct power_supply_desc bat_desc;
42 struct device *w1_dev;
43};
44
45enum current_types {
46 CURRENT_NOW,
47 CURRENT_AVG,
48};
49
50static const char model[] = "DS2780";
51static const char manufacturer[] = "Maxim/Dallas";
52
53static inline struct ds2780_device_info *
54to_ds2780_device_info(struct power_supply *psy)
55{
56 return power_supply_get_drvdata(psy);
57}
58
59static inline int ds2780_battery_io(struct ds2780_device_info *dev_info,
60 char *buf, int addr, size_t count, int io)
61{
62 return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
63}
64
65static inline int ds2780_read8(struct ds2780_device_info *dev_info, u8 *val,
66 int addr)
67{
68 return ds2780_battery_io(dev_info, val, addr, sizeof(u8), 0);
69}
70
71static int ds2780_read16(struct ds2780_device_info *dev_info, s16 *val,
72 int addr)
73{
74 int ret;
75 u8 raw[2];
76
77 ret = ds2780_battery_io(dev_info, raw, addr, sizeof(raw), 0);
78 if (ret < 0)
79 return ret;
80
81 *val = (raw[0] << 8) | raw[1];
82
83 return 0;
84}
85
86static inline int ds2780_read_block(struct ds2780_device_info *dev_info,
87 u8 *val, int addr, size_t count)
88{
89 return ds2780_battery_io(dev_info, val, addr, count, 0);
90}
91
92static inline int ds2780_write(struct ds2780_device_info *dev_info, u8 *val,
93 int addr, size_t count)
94{
95 return ds2780_battery_io(dev_info, val, addr, count, 1);
96}
97
98static inline int ds2780_store_eeprom(struct device *dev, int addr)
99{
100 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_COPY_DATA);
101}
102
103static inline int ds2780_recall_eeprom(struct device *dev, int addr)
104{
105 return w1_ds2780_eeprom_cmd(dev, addr, W1_DS2780_RECALL_DATA);
106}
107
108static int ds2780_save_eeprom(struct ds2780_device_info *dev_info, int reg)
109{
110 int ret;
111
112 ret = ds2780_store_eeprom(dev_info->w1_dev, reg);
113 if (ret < 0)
114 return ret;
115
116 ret = ds2780_recall_eeprom(dev_info->w1_dev, reg);
117 if (ret < 0)
118 return ret;
119
120 return 0;
121}
122
123/* Set sense resistor value in mhos */
124static int ds2780_set_sense_register(struct ds2780_device_info *dev_info,
125 u8 conductance)
126{
127 int ret;
128
129 ret = ds2780_write(dev_info, &conductance,
130 DS2780_RSNSP_REG, sizeof(u8));
131 if (ret < 0)
132 return ret;
133
134 return ds2780_save_eeprom(dev_info, DS2780_RSNSP_REG);
135}
136
137/* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */
138static int ds2780_get_rsgain_register(struct ds2780_device_info *dev_info,
139 u16 *rsgain)
140{
141 return ds2780_read16(dev_info, rsgain, DS2780_RSGAIN_MSB_REG);
142}
143
144/* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */
145static int ds2780_set_rsgain_register(struct ds2780_device_info *dev_info,
146 u16 rsgain)
147{
148 int ret;
149 u8 raw[] = {rsgain >> 8, rsgain & 0xFF};
150
151 ret = ds2780_write(dev_info, raw,
152 DS2780_RSGAIN_MSB_REG, sizeof(raw));
153 if (ret < 0)
154 return ret;
155
156 return ds2780_save_eeprom(dev_info, DS2780_RSGAIN_MSB_REG);
157}
158
159static int ds2780_get_voltage(struct ds2780_device_info *dev_info,
160 int *voltage_uV)
161{
162 int ret;
163 s16 voltage_raw;
164
165 /*
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
173 */
174 ret = ds2780_read16(dev_info, &voltage_raw,
175 DS2780_VOLT_MSB_REG);
176 if (ret < 0)
177 return ret;
178
179 /*
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.
182 */
183 *voltage_uV = (voltage_raw / 32) * 4880;
184 return 0;
185}
186
187static int ds2780_get_temperature(struct ds2780_device_info *dev_info,
188 int *temperature)
189{
190 int ret;
191 s16 temperature_raw;
192
193 /*
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
202 */
203 ret = ds2780_read16(dev_info, &temperature_raw,
204 DS2780_TEMP_MSB_REG);
205 if (ret < 0)
206 return ret;
207
208 /*
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.
213 */
214 *temperature = ((temperature_raw / 32) * 125) / 100;
215 return 0;
216}
217
218static int ds2780_get_current(struct ds2780_device_info *dev_info,
219 enum current_types type, int *current_uA)
220{
221 int ret, sense_res;
222 s16 current_raw;
223 u8 sense_res_raw, reg_msb;
224
225 /*
226 * The units of measurement for current are dependent on the value of
227 * the sense resistor.
228 */
229 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
230 if (ret < 0)
231 return ret;
232
233 if (sense_res_raw == 0) {
234 dev_err(dev_info->dev, "sense resistor value is 0\n");
235 return -EINVAL;
236 }
237 sense_res = 1000 / sense_res_raw;
238
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;
245
246 /*
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
254 */
255 ret = ds2780_read16(dev_info, ¤t_raw, reg_msb);
256 if (ret < 0)
257 return ret;
258
259 *current_uA = current_raw * (DS2780_CURRENT_UNITS / sense_res);
260 return 0;
261}
262
263static int ds2780_get_accumulated_current(struct ds2780_device_info *dev_info,
264 int *accumulated_current)
265{
266 int ret, sense_res;
267 s16 current_raw;
268 u8 sense_res_raw;
269
270 /*
271 * The units of measurement for accumulated current are dependent on
272 * the value of the sense resistor.
273 */
274 ret = ds2780_read8(dev_info, &sense_res_raw, DS2780_RSNSP_REG);
275 if (ret < 0)
276 return ret;
277
278 if (sense_res_raw == 0) {
279 dev_err(dev_info->dev, "sense resistor value is 0\n");
280 return -ENXIO;
281 }
282 sense_res = 1000 / sense_res_raw;
283
284 /*
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
291 */
292 ret = ds2780_read16(dev_info, ¤t_raw, DS2780_ACR_MSB_REG);
293 if (ret < 0)
294 return ret;
295
296 *accumulated_current = current_raw * (DS2780_CHARGE_UNITS / sense_res);
297 return 0;
298}
299
300static int ds2780_get_capacity(struct ds2780_device_info *dev_info,
301 int *capacity)
302{
303 int ret;
304 u8 raw;
305
306 ret = ds2780_read8(dev_info, &raw, DS2780_RARC_REG);
307 if (ret < 0)
308 return ret;
309
310 *capacity = raw;
311 return raw;
312}
313
314static int ds2780_get_status(struct ds2780_device_info *dev_info, int *status)
315{
316 int ret, current_uA, capacity;
317
318 ret = ds2780_get_current(dev_info, CURRENT_NOW, ¤t_uA);
319 if (ret < 0)
320 return ret;
321
322 ret = ds2780_get_capacity(dev_info, &capacity);
323 if (ret < 0)
324 return ret;
325
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;
334
335 return 0;
336}
337
338static int ds2780_get_charge_now(struct ds2780_device_info *dev_info,
339 int *charge_now)
340{
341 int ret;
342 u16 charge_raw;
343
344 /*
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
351 */
352 ret = ds2780_read16(dev_info, &charge_raw, DS2780_RAAC_MSB_REG);
353 if (ret < 0)
354 return ret;
355
356 *charge_now = charge_raw * 1600;
357 return 0;
358}
359
360static int ds2780_get_control_register(struct ds2780_device_info *dev_info,
361 u8 *control_reg)
362{
363 return ds2780_read8(dev_info, control_reg, DS2780_CONTROL_REG);
364}
365
366static int ds2780_set_control_register(struct ds2780_device_info *dev_info,
367 u8 control_reg)
368{
369 int ret;
370
371 ret = ds2780_write(dev_info, &control_reg,
372 DS2780_CONTROL_REG, sizeof(u8));
373 if (ret < 0)
374 return ret;
375
376 return ds2780_save_eeprom(dev_info, DS2780_CONTROL_REG);
377}
378
379static int ds2780_battery_get_property(struct power_supply *psy,
380 enum power_supply_property psp,
381 union power_supply_propval *val)
382{
383 int ret = 0;
384 struct ds2780_device_info *dev_info = to_ds2780_device_info(psy);
385
386 switch (psp) {
387 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
388 ret = ds2780_get_voltage(dev_info, &val->intval);
389 break;
390
391 case POWER_SUPPLY_PROP_TEMP:
392 ret = ds2780_get_temperature(dev_info, &val->intval);
393 break;
394
395 case POWER_SUPPLY_PROP_MODEL_NAME:
396 val->strval = model;
397 break;
398
399 case POWER_SUPPLY_PROP_MANUFACTURER:
400 val->strval = manufacturer;
401 break;
402
403 case POWER_SUPPLY_PROP_CURRENT_NOW:
404 ret = ds2780_get_current(dev_info, CURRENT_NOW, &val->intval);
405 break;
406
407 case POWER_SUPPLY_PROP_CURRENT_AVG:
408 ret = ds2780_get_current(dev_info, CURRENT_AVG, &val->intval);
409 break;
410
411 case POWER_SUPPLY_PROP_STATUS:
412 ret = ds2780_get_status(dev_info, &val->intval);
413 break;
414
415 case POWER_SUPPLY_PROP_CAPACITY:
416 ret = ds2780_get_capacity(dev_info, &val->intval);
417 break;
418
419 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
420 ret = ds2780_get_accumulated_current(dev_info, &val->intval);
421 break;
422
423 case POWER_SUPPLY_PROP_CHARGE_NOW:
424 ret = ds2780_get_charge_now(dev_info, &val->intval);
425 break;
426
427 default:
428 ret = -EINVAL;
429 }
430
431 return ret;
432}
433
434static 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,
445};
446
447static ssize_t ds2780_get_pmod_enabled(struct device *dev,
448 struct device_attribute *attr,
449 char *buf)
450{
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);
455
456 /* Get power mode */
457 ret = ds2780_get_control_register(dev_info, &control_reg);
458 if (ret < 0)
459 return ret;
460
461 return sprintf(buf, "%d\n",
462 !!(control_reg & DS2780_CONTROL_REG_PMOD));
463}
464
465static ssize_t ds2780_set_pmod_enabled(struct device *dev,
466 struct device_attribute *attr,
467 const char *buf,
468 size_t count)
469{
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);
474
475 /* Set power mode */
476 ret = ds2780_get_control_register(dev_info, &control_reg);
477 if (ret < 0)
478 return ret;
479
480 ret = kstrtou8(buf, 0, &new_setting);
481 if (ret < 0)
482 return ret;
483
484 if ((new_setting != 0) && (new_setting != 1)) {
485 dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n");
486 return -EINVAL;
487 }
488
489 if (new_setting)
490 control_reg |= DS2780_CONTROL_REG_PMOD;
491 else
492 control_reg &= ~DS2780_CONTROL_REG_PMOD;
493
494 ret = ds2780_set_control_register(dev_info, control_reg);
495 if (ret < 0)
496 return ret;
497
498 return count;
499}
500
501static ssize_t ds2780_get_sense_resistor_value(struct device *dev,
502 struct device_attribute *attr,
503 char *buf)
504{
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);
509
510 ret = ds2780_read8(dev_info, &sense_resistor, DS2780_RSNSP_REG);
511 if (ret < 0)
512 return ret;
513
514 ret = sprintf(buf, "%d\n", sense_resistor);
515 return ret;
516}
517
518static ssize_t ds2780_set_sense_resistor_value(struct device *dev,
519 struct device_attribute *attr,
520 const char *buf,
521 size_t count)
522{
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);
527
528 ret = kstrtou8(buf, 0, &new_setting);
529 if (ret < 0)
530 return ret;
531
532 ret = ds2780_set_sense_register(dev_info, new_setting);
533 if (ret < 0)
534 return ret;
535
536 return count;
537}
538
539static ssize_t ds2780_get_rsgain_setting(struct device *dev,
540 struct device_attribute *attr,
541 char *buf)
542{
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);
547
548 ret = ds2780_get_rsgain_register(dev_info, &rsgain);
549 if (ret < 0)
550 return ret;
551
552 return sprintf(buf, "%d\n", rsgain);
553}
554
555static ssize_t ds2780_set_rsgain_setting(struct device *dev,
556 struct device_attribute *attr,
557 const char *buf,
558 size_t count)
559{
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);
564
565 ret = kstrtou16(buf, 0, &new_setting);
566 if (ret < 0)
567 return ret;
568
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;
573 }
574
575 ret = ds2780_set_rsgain_register(dev_info, new_setting);
576 if (ret < 0)
577 return ret;
578
579 return count;
580}
581
582static ssize_t ds2780_get_pio_pin(struct device *dev,
583 struct device_attribute *attr,
584 char *buf)
585{
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);
590
591 ret = ds2780_read8(dev_info, &sfr, DS2780_SFR_REG);
592 if (ret < 0)
593 return ret;
594
595 ret = sprintf(buf, "%d\n", sfr & DS2780_SFR_REG_PIOSC);
596 return ret;
597}
598
599static ssize_t ds2780_set_pio_pin(struct device *dev,
600 struct device_attribute *attr,
601 const char *buf,
602 size_t count)
603{
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);
608
609 ret = kstrtou8(buf, 0, &new_setting);
610 if (ret < 0)
611 return ret;
612
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;
616 }
617
618 ret = ds2780_write(dev_info, &new_setting,
619 DS2780_SFR_REG, sizeof(u8));
620 if (ret < 0)
621 return ret;
622
623 return count;
624}
625
626static 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)
630{
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);
634
635 return ds2780_read_block(dev_info, buf,
636 DS2780_EEPROM_BLOCK1_START + off, count);
637}
638
639static 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)
643{
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;
648
649 ret = ds2780_write(dev_info, buf,
650 DS2780_EEPROM_BLOCK1_START + off, count);
651 if (ret < 0)
652 return ret;
653
654 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK1_START);
655 if (ret < 0)
656 return ret;
657
658 return count;
659}
660
661static struct bin_attribute ds2780_param_eeprom_bin_attr = {
662 .attr = {
663 .name = "param_eeprom",
664 .mode = S_IRUGO | S_IWUSR,
665 },
666 .size = DS2780_PARAM_EEPROM_SIZE,
667 .read = ds2780_read_param_eeprom_bin,
668 .write = ds2780_write_param_eeprom_bin,
669};
670
671static 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)
675{
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);
679
680 return ds2780_read_block(dev_info, buf,
681 DS2780_EEPROM_BLOCK0_START + off, count);
682}
683
684static 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)
688{
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;
693
694 ret = ds2780_write(dev_info, buf,
695 DS2780_EEPROM_BLOCK0_START + off, count);
696 if (ret < 0)
697 return ret;
698
699 ret = ds2780_save_eeprom(dev_info, DS2780_EEPROM_BLOCK0_START);
700 if (ret < 0)
701 return ret;
702
703 return count;
704}
705
706static struct bin_attribute ds2780_user_eeprom_bin_attr = {
707 .attr = {
708 .name = "user_eeprom",
709 .mode = S_IRUGO | S_IWUSR,
710 },
711 .size = DS2780_USER_EEPROM_SIZE,
712 .read = ds2780_read_user_eeprom_bin,
713 .write = ds2780_write_user_eeprom_bin,
714};
715
716static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2780_get_pmod_enabled,
717 ds2780_set_pmod_enabled);
718static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR,
719 ds2780_get_sense_resistor_value, ds2780_set_sense_resistor_value);
720static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2780_get_rsgain_setting,
721 ds2780_set_rsgain_setting);
722static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2780_get_pio_pin,
723 ds2780_set_pio_pin);
724
725static struct attribute *ds2780_sysfs_attrs[] = {
726 &dev_attr_pmod_enabled.attr,
727 &dev_attr_sense_resistor_value.attr,
728 &dev_attr_rsgain_setting.attr,
729 &dev_attr_pio_pin.attr,
730 NULL
731};
732
733static struct bin_attribute *ds2780_sysfs_bin_attrs[] = {
734 &ds2780_param_eeprom_bin_attr,
735 &ds2780_user_eeprom_bin_attr,
736 NULL
737};
738
739static const struct attribute_group ds2780_sysfs_group = {
740 .attrs = ds2780_sysfs_attrs,
741 .bin_attrs = ds2780_sysfs_bin_attrs,
742};
743
744static const struct attribute_group *ds2780_sysfs_groups[] = {
745 &ds2780_sysfs_group,
746 NULL,
747};
748
749static int ds2780_battery_probe(struct platform_device *pdev)
750{
751 struct power_supply_config psy_cfg = {};
752 struct ds2780_device_info *dev_info;
753
754 dev_info = devm_kzalloc(&pdev->dev, sizeof(*dev_info), GFP_KERNEL);
755 if (!dev_info)
756 return -ENOMEM;
757
758 platform_set_drvdata(pdev, dev_info);
759
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 = ds2780_battery_props;
765 dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2780_battery_props);
766 dev_info->bat_desc.get_property = ds2780_battery_get_property;
767
768 psy_cfg.drv_data = dev_info;
769 psy_cfg.attr_grp = ds2780_sysfs_groups;
770
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);
777 }
778
779 return 0;
780}
781
782static struct platform_driver ds2780_battery_driver = {
783 .driver = {
784 .name = "ds2780-battery",
785 },
786 .probe = ds2780_battery_probe,
787};
788
789module_platform_driver(ds2780_battery_driver);
790
791MODULE_LICENSE("GPL");
792MODULE_AUTHOR("Clifton Barnes <cabarnes@indesign-llc.com>");
793MODULE_DESCRIPTION("Maxim/Dallas DS2780 Stand-Alone Fuel Gauge IC driver");
794MODULE_ALIAS("platform:ds2780-battery");