2 * Driver for batteries with DS2760 chips inside.
4 * Copyright © 2007 Anton Vorontsov
5 * 2004-2007 Matt Reimer
8 * Use consistent with the GNU GPL is permitted,
9 * provided that this copyright notice is
10 * preserved in its entirety in all copies and derived works.
12 * Author: Anton Vorontsov <cbou@mail.ru>
15 * Matt Reimer <mreimer@vpop.net>
16 * April 2004, 2005, 2007
18 * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
22 #include <linux/module.h>
23 #include <linux/param.h>
24 #include <linux/jiffies.h>
25 #include <linux/workqueue.h>
27 #include <linux/slab.h>
28 #include <linux/platform_device.h>
29 #include <linux/power_supply.h>
30 #include <linux/suspend.h>
34 static unsigned int cache_time
= 1000;
35 module_param(cache_time
, uint
, 0644);
36 MODULE_PARM_DESC(cache_time
, "cache time in milliseconds");
38 static bool pmod_enabled
;
39 module_param(pmod_enabled
, bool, 0644);
40 MODULE_PARM_DESC(pmod_enabled
, "PMOD enable bit");
42 static unsigned int rated_capacity
;
43 module_param(rated_capacity
, uint
, 0644);
44 MODULE_PARM_DESC(rated_capacity
, "rated battery capacity, 10*mAh or index");
46 static unsigned int current_accum
;
47 module_param(current_accum
, uint
, 0644);
48 MODULE_PARM_DESC(current_accum
, "current accumulator value");
50 #define W1_FAMILY_DS2760 0x30
52 /* Known commands to the DS2760 chip */
53 #define W1_DS2760_SWAP 0xAA
54 #define W1_DS2760_READ_DATA 0x69
55 #define W1_DS2760_WRITE_DATA 0x6C
56 #define W1_DS2760_COPY_DATA 0x48
57 #define W1_DS2760_RECALL_DATA 0xB8
58 #define W1_DS2760_LOCK 0x6A
60 /* Number of valid register addresses */
61 #define DS2760_DATA_SIZE 0x40
63 #define DS2760_PROTECTION_REG 0x00
65 #define DS2760_STATUS_REG 0x01
66 #define DS2760_STATUS_IE (1 << 2)
67 #define DS2760_STATUS_SWEN (1 << 3)
68 #define DS2760_STATUS_RNAOP (1 << 4)
69 #define DS2760_STATUS_PMOD (1 << 5)
71 #define DS2760_EEPROM_REG 0x07
72 #define DS2760_SPECIAL_FEATURE_REG 0x08
73 #define DS2760_VOLTAGE_MSB 0x0c
74 #define DS2760_VOLTAGE_LSB 0x0d
75 #define DS2760_CURRENT_MSB 0x0e
76 #define DS2760_CURRENT_LSB 0x0f
77 #define DS2760_CURRENT_ACCUM_MSB 0x10
78 #define DS2760_CURRENT_ACCUM_LSB 0x11
79 #define DS2760_TEMP_MSB 0x18
80 #define DS2760_TEMP_LSB 0x19
81 #define DS2760_EEPROM_BLOCK0 0x20
82 #define DS2760_ACTIVE_FULL 0x20
83 #define DS2760_EEPROM_BLOCK1 0x30
84 #define DS2760_STATUS_WRITE_REG 0x31
85 #define DS2760_RATED_CAPACITY 0x32
86 #define DS2760_CURRENT_OFFSET_BIAS 0x33
87 #define DS2760_ACTIVE_EMPTY 0x3b
89 struct ds2760_device_info
{
92 /* DS2760 data, valid after calling ds2760_battery_read_status() */
93 unsigned long update_time
; /* jiffies when data read */
94 char raw
[DS2760_DATA_SIZE
]; /* raw DS2760 data */
95 int voltage_raw
; /* units of 4.88 mV */
96 int voltage_uV
; /* units of µV */
97 int current_raw
; /* units of 0.625 mA */
98 int current_uA
; /* units of µA */
99 int accum_current_raw
; /* units of 0.25 mAh */
100 int accum_current_uAh
; /* units of µAh */
101 int temp_raw
; /* units of 0.125 °C */
102 int temp_C
; /* units of 0.1 °C */
103 int rated_capacity
; /* units of µAh */
104 int rem_capacity
; /* percentage */
105 int full_active_uAh
; /* units of µAh */
106 int empty_uAh
; /* units of µAh */
107 int life_sec
; /* units of seconds */
108 int charge_status
; /* POWER_SUPPLY_STATUS_* */
111 struct power_supply
*bat
;
112 struct power_supply_desc bat_desc
;
113 struct workqueue_struct
*monitor_wqueue
;
114 struct delayed_work monitor_work
;
115 struct delayed_work set_charged_work
;
116 struct notifier_block pm_notifier
;
119 static int w1_ds2760_io(struct device
*dev
, char *buf
, int addr
, size_t count
,
122 struct w1_slave
*sl
= container_of(dev
, struct w1_slave
, dev
);
127 mutex_lock(&sl
->master
->bus_mutex
);
129 if (addr
> DS2760_DATA_SIZE
|| addr
< 0) {
133 if (addr
+ count
> DS2760_DATA_SIZE
)
134 count
= DS2760_DATA_SIZE
- addr
;
136 if (!w1_reset_select_slave(sl
)) {
138 w1_write_8(sl
->master
, W1_DS2760_READ_DATA
);
139 w1_write_8(sl
->master
, addr
);
140 count
= w1_read_block(sl
->master
, buf
, count
);
142 w1_write_8(sl
->master
, W1_DS2760_WRITE_DATA
);
143 w1_write_8(sl
->master
, addr
);
144 w1_write_block(sl
->master
, buf
, count
);
145 /* XXX w1_write_block returns void, not n_written */
150 mutex_unlock(&sl
->master
->bus_mutex
);
155 static int w1_ds2760_read(struct device
*dev
,
159 return w1_ds2760_io(dev
, buf
, addr
, count
, 0);
162 static int w1_ds2760_write(struct device
*dev
,
164 int addr
, size_t count
)
166 return w1_ds2760_io(dev
, buf
, addr
, count
, 1);
169 static int w1_ds2760_eeprom_cmd(struct device
*dev
, int addr
, int cmd
)
171 struct w1_slave
*sl
= container_of(dev
, struct w1_slave
, dev
);
176 mutex_lock(&sl
->master
->bus_mutex
);
178 if (w1_reset_select_slave(sl
) == 0) {
179 w1_write_8(sl
->master
, cmd
);
180 w1_write_8(sl
->master
, addr
);
183 mutex_unlock(&sl
->master
->bus_mutex
);
187 static int w1_ds2760_store_eeprom(struct device
*dev
, int addr
)
189 return w1_ds2760_eeprom_cmd(dev
, addr
, W1_DS2760_COPY_DATA
);
192 static int w1_ds2760_recall_eeprom(struct device
*dev
, int addr
)
194 return w1_ds2760_eeprom_cmd(dev
, addr
, W1_DS2760_RECALL_DATA
);
197 static ssize_t
w1_slave_read(struct file
*filp
, struct kobject
*kobj
,
198 struct bin_attribute
*bin_attr
, char *buf
,
199 loff_t off
, size_t count
)
201 struct device
*dev
= kobj_to_dev(kobj
);
202 return w1_ds2760_read(dev
, buf
, off
, count
);
205 static BIN_ATTR_RO(w1_slave
, DS2760_DATA_SIZE
);
207 static struct bin_attribute
*w1_ds2760_bin_attrs
[] = {
212 static const struct attribute_group w1_ds2760_group
= {
213 .bin_attrs
= w1_ds2760_bin_attrs
,
216 static const struct attribute_group
*w1_ds2760_groups
[] = {
220 /* Some batteries have their rated capacity stored a N * 10 mAh, while
221 * others use an index into this table. */
222 static int rated_capacities
[] = {
238 /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
239 * temp is in Celsius */
240 static int battery_interpolate(int array
[], int temp
)
252 return array
[index
] + (((array
[index
+ 1] - array
[index
]) * dt
) / 10);
255 static int ds2760_battery_read_status(struct ds2760_device_info
*di
)
257 int ret
, i
, start
, count
, scale
[5];
259 if (di
->update_time
&& time_before(jiffies
, di
->update_time
+
260 msecs_to_jiffies(cache_time
)))
263 /* The first time we read the entire contents of SRAM/EEPROM,
264 * but after that we just read the interesting bits that change. */
265 if (di
->update_time
== 0) {
267 count
= DS2760_DATA_SIZE
;
269 start
= DS2760_VOLTAGE_MSB
;
270 count
= DS2760_TEMP_LSB
- start
+ 1;
273 ret
= w1_ds2760_read(di
->dev
, di
->raw
+ start
, start
, count
);
275 dev_warn(di
->dev
, "call to w1_ds2760_read failed (0x%p)\n",
280 di
->update_time
= jiffies
;
282 /* DS2760 reports voltage in units of 4.88mV, but the battery class
283 * reports in units of uV, so convert by multiplying by 4880. */
284 di
->voltage_raw
= (di
->raw
[DS2760_VOLTAGE_MSB
] << 3) |
285 (di
->raw
[DS2760_VOLTAGE_LSB
] >> 5);
286 di
->voltage_uV
= di
->voltage_raw
* 4880;
288 /* DS2760 reports current in signed units of 0.625mA, but the battery
289 * class reports in units of µA, so convert by multiplying by 625. */
291 (((signed char)di
->raw
[DS2760_CURRENT_MSB
]) << 5) |
292 (di
->raw
[DS2760_CURRENT_LSB
] >> 3);
293 di
->current_uA
= di
->current_raw
* 625;
295 /* DS2760 reports accumulated current in signed units of 0.25mAh. */
296 di
->accum_current_raw
=
297 (((signed char)di
->raw
[DS2760_CURRENT_ACCUM_MSB
]) << 8) |
298 di
->raw
[DS2760_CURRENT_ACCUM_LSB
];
299 di
->accum_current_uAh
= di
->accum_current_raw
* 250;
301 /* DS2760 reports temperature in signed units of 0.125°C, but the
302 * battery class reports in units of 1/10 °C, so we convert by
303 * multiplying by .125 * 10 = 1.25. */
304 di
->temp_raw
= (((signed char)di
->raw
[DS2760_TEMP_MSB
]) << 3) |
305 (di
->raw
[DS2760_TEMP_LSB
] >> 5);
306 di
->temp_C
= di
->temp_raw
+ (di
->temp_raw
/ 4);
308 /* At least some battery monitors (e.g. HP iPAQ) store the battery's
309 * maximum rated capacity. */
310 if (di
->raw
[DS2760_RATED_CAPACITY
] < ARRAY_SIZE(rated_capacities
))
311 di
->rated_capacity
= rated_capacities
[
312 (unsigned int)di
->raw
[DS2760_RATED_CAPACITY
]];
314 di
->rated_capacity
= di
->raw
[DS2760_RATED_CAPACITY
] * 10;
316 di
->rated_capacity
*= 1000; /* convert to µAh */
318 /* Calculate the full level at the present temperature. */
319 di
->full_active_uAh
= di
->raw
[DS2760_ACTIVE_FULL
] << 8 |
320 di
->raw
[DS2760_ACTIVE_FULL
+ 1];
322 /* If the full_active_uAh value is not given, fall back to the rated
323 * capacity. This is likely to happen when chips are not part of the
324 * battery pack and is therefore not bootstrapped. */
325 if (di
->full_active_uAh
== 0)
326 di
->full_active_uAh
= di
->rated_capacity
/ 1000L;
328 scale
[0] = di
->full_active_uAh
;
329 for (i
= 1; i
< 5; i
++)
330 scale
[i
] = scale
[i
- 1] + di
->raw
[DS2760_ACTIVE_FULL
+ 1 + i
];
332 di
->full_active_uAh
= battery_interpolate(scale
, di
->temp_C
/ 10);
333 di
->full_active_uAh
*= 1000; /* convert to µAh */
335 /* Calculate the empty level at the present temperature. */
336 scale
[4] = di
->raw
[DS2760_ACTIVE_EMPTY
+ 4];
337 for (i
= 3; i
>= 0; i
--)
338 scale
[i
] = scale
[i
+ 1] + di
->raw
[DS2760_ACTIVE_EMPTY
+ i
];
340 di
->empty_uAh
= battery_interpolate(scale
, di
->temp_C
/ 10);
341 di
->empty_uAh
*= 1000; /* convert to µAh */
343 if (di
->full_active_uAh
== di
->empty_uAh
)
344 di
->rem_capacity
= 0;
346 /* From Maxim Application Note 131: remaining capacity =
347 * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
348 di
->rem_capacity
= ((di
->accum_current_uAh
- di
->empty_uAh
) * 100L) /
349 (di
->full_active_uAh
- di
->empty_uAh
);
351 if (di
->rem_capacity
< 0)
352 di
->rem_capacity
= 0;
353 if (di
->rem_capacity
> 100)
354 di
->rem_capacity
= 100;
356 if (di
->current_uA
< -100L)
357 di
->life_sec
= -((di
->accum_current_uAh
- di
->empty_uAh
) * 36L)
358 / (di
->current_uA
/ 100L);
365 static void ds2760_battery_set_current_accum(struct ds2760_device_info
*di
,
366 unsigned int acr_val
)
368 unsigned char acr
[2];
370 /* acr is in units of 0.25 mAh */
374 acr
[0] = acr_val
>> 8;
375 acr
[1] = acr_val
& 0xff;
377 if (w1_ds2760_write(di
->dev
, acr
, DS2760_CURRENT_ACCUM_MSB
, 2) < 2)
378 dev_warn(di
->dev
, "ACR write failed\n");
381 static void ds2760_battery_update_status(struct ds2760_device_info
*di
)
383 int old_charge_status
= di
->charge_status
;
385 ds2760_battery_read_status(di
);
387 if (di
->charge_status
== POWER_SUPPLY_STATUS_UNKNOWN
)
388 di
->full_counter
= 0;
390 if (power_supply_am_i_supplied(di
->bat
)) {
391 if (di
->current_uA
> 10000) {
392 di
->charge_status
= POWER_SUPPLY_STATUS_CHARGING
;
393 di
->full_counter
= 0;
394 } else if (di
->current_uA
< -5000) {
395 if (di
->charge_status
!= POWER_SUPPLY_STATUS_NOT_CHARGING
)
396 dev_notice(di
->dev
, "not enough power to "
398 di
->charge_status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
399 di
->full_counter
= 0;
400 } else if (di
->current_uA
< 10000 &&
401 di
->charge_status
!= POWER_SUPPLY_STATUS_FULL
) {
403 /* Don't consider the battery to be full unless
404 * we've seen the current < 10 mA at least two
405 * consecutive times. */
409 if (di
->full_counter
< 2) {
410 di
->charge_status
= POWER_SUPPLY_STATUS_CHARGING
;
412 di
->charge_status
= POWER_SUPPLY_STATUS_FULL
;
413 ds2760_battery_set_current_accum(di
,
414 di
->full_active_uAh
);
418 di
->charge_status
= POWER_SUPPLY_STATUS_DISCHARGING
;
419 di
->full_counter
= 0;
422 if (di
->charge_status
!= old_charge_status
)
423 power_supply_changed(di
->bat
);
426 static void ds2760_battery_write_status(struct ds2760_device_info
*di
,
429 if (status
== di
->raw
[DS2760_STATUS_REG
])
432 w1_ds2760_write(di
->dev
, &status
, DS2760_STATUS_WRITE_REG
, 1);
433 w1_ds2760_store_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
434 w1_ds2760_recall_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
437 static void ds2760_battery_write_rated_capacity(struct ds2760_device_info
*di
,
438 unsigned char rated_capacity
)
440 if (rated_capacity
== di
->raw
[DS2760_RATED_CAPACITY
])
443 w1_ds2760_write(di
->dev
, &rated_capacity
, DS2760_RATED_CAPACITY
, 1);
444 w1_ds2760_store_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
445 w1_ds2760_recall_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
448 static void ds2760_battery_write_active_full(struct ds2760_device_info
*di
,
451 unsigned char tmp
[2] = {
456 if (tmp
[0] == di
->raw
[DS2760_ACTIVE_FULL
] &&
457 tmp
[1] == di
->raw
[DS2760_ACTIVE_FULL
+ 1])
460 w1_ds2760_write(di
->dev
, tmp
, DS2760_ACTIVE_FULL
, sizeof(tmp
));
461 w1_ds2760_store_eeprom(di
->dev
, DS2760_EEPROM_BLOCK0
);
462 w1_ds2760_recall_eeprom(di
->dev
, DS2760_EEPROM_BLOCK0
);
464 /* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL
465 * values won't be read back by ds2760_battery_read_status() */
466 di
->raw
[DS2760_ACTIVE_FULL
] = tmp
[0];
467 di
->raw
[DS2760_ACTIVE_FULL
+ 1] = tmp
[1];
470 static void ds2760_battery_work(struct work_struct
*work
)
472 struct ds2760_device_info
*di
= container_of(work
,
473 struct ds2760_device_info
, monitor_work
.work
);
474 const int interval
= HZ
* 60;
476 dev_dbg(di
->dev
, "%s\n", __func__
);
478 ds2760_battery_update_status(di
);
479 queue_delayed_work(di
->monitor_wqueue
, &di
->monitor_work
, interval
);
482 static void ds2760_battery_external_power_changed(struct power_supply
*psy
)
484 struct ds2760_device_info
*di
= power_supply_get_drvdata(psy
);
486 dev_dbg(di
->dev
, "%s\n", __func__
);
488 mod_delayed_work(di
->monitor_wqueue
, &di
->monitor_work
, HZ
/10);
492 static void ds2760_battery_set_charged_work(struct work_struct
*work
)
495 struct ds2760_device_info
*di
= container_of(work
,
496 struct ds2760_device_info
, set_charged_work
.work
);
498 dev_dbg(di
->dev
, "%s\n", __func__
);
500 ds2760_battery_read_status(di
);
502 /* When we get notified by external circuitry that the battery is
503 * considered fully charged now, we know that there is no current
504 * flow any more. However, the ds2760's internal current meter is
505 * too inaccurate to rely on - spec say something ~15% failure.
506 * Hence, we use the current offset bias register to compensate
510 if (!power_supply_am_i_supplied(di
->bat
))
513 bias
= (signed char) di
->current_raw
+
514 (signed char) di
->raw
[DS2760_CURRENT_OFFSET_BIAS
];
516 dev_dbg(di
->dev
, "%s: bias = %d\n", __func__
, bias
);
518 w1_ds2760_write(di
->dev
, &bias
, DS2760_CURRENT_OFFSET_BIAS
, 1);
519 w1_ds2760_store_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
520 w1_ds2760_recall_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
522 /* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS
523 * value won't be read back by ds2760_battery_read_status() */
524 di
->raw
[DS2760_CURRENT_OFFSET_BIAS
] = bias
;
527 static void ds2760_battery_set_charged(struct power_supply
*psy
)
529 struct ds2760_device_info
*di
= power_supply_get_drvdata(psy
);
531 /* postpone the actual work by 20 secs. This is for debouncing GPIO
532 * signals and to let the current value settle. See AN4188. */
533 mod_delayed_work(di
->monitor_wqueue
, &di
->set_charged_work
, HZ
* 20);
536 static int ds2760_battery_get_property(struct power_supply
*psy
,
537 enum power_supply_property psp
,
538 union power_supply_propval
*val
)
540 struct ds2760_device_info
*di
= power_supply_get_drvdata(psy
);
543 case POWER_SUPPLY_PROP_STATUS
:
544 val
->intval
= di
->charge_status
;
550 ds2760_battery_read_status(di
);
553 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
554 val
->intval
= di
->voltage_uV
;
556 case POWER_SUPPLY_PROP_CURRENT_NOW
:
557 val
->intval
= di
->current_uA
;
559 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
560 val
->intval
= di
->rated_capacity
;
562 case POWER_SUPPLY_PROP_CHARGE_FULL
:
563 val
->intval
= di
->full_active_uAh
;
565 case POWER_SUPPLY_PROP_CHARGE_EMPTY
:
566 val
->intval
= di
->empty_uAh
;
568 case POWER_SUPPLY_PROP_CHARGE_NOW
:
569 val
->intval
= di
->accum_current_uAh
;
571 case POWER_SUPPLY_PROP_TEMP
:
572 val
->intval
= di
->temp_C
;
574 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
575 val
->intval
= di
->life_sec
;
577 case POWER_SUPPLY_PROP_CAPACITY
:
578 val
->intval
= di
->rem_capacity
;
587 static int ds2760_battery_set_property(struct power_supply
*psy
,
588 enum power_supply_property psp
,
589 const union power_supply_propval
*val
)
591 struct ds2760_device_info
*di
= power_supply_get_drvdata(psy
);
594 case POWER_SUPPLY_PROP_CHARGE_FULL
:
595 /* the interface counts in uAh, convert the value */
596 ds2760_battery_write_active_full(di
, val
->intval
/ 1000L);
599 case POWER_SUPPLY_PROP_CHARGE_NOW
:
600 /* ds2760_battery_set_current_accum() does the conversion */
601 ds2760_battery_set_current_accum(di
, val
->intval
);
611 static int ds2760_battery_property_is_writeable(struct power_supply
*psy
,
612 enum power_supply_property psp
)
615 case POWER_SUPPLY_PROP_CHARGE_FULL
:
616 case POWER_SUPPLY_PROP_CHARGE_NOW
:
626 static enum power_supply_property ds2760_battery_props
[] = {
627 POWER_SUPPLY_PROP_STATUS
,
628 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
629 POWER_SUPPLY_PROP_CURRENT_NOW
,
630 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
631 POWER_SUPPLY_PROP_CHARGE_FULL
,
632 POWER_SUPPLY_PROP_CHARGE_EMPTY
,
633 POWER_SUPPLY_PROP_CHARGE_NOW
,
634 POWER_SUPPLY_PROP_TEMP
,
635 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
636 POWER_SUPPLY_PROP_CAPACITY
,
639 static int ds2760_pm_notifier(struct notifier_block
*notifier
,
640 unsigned long pm_event
,
643 struct ds2760_device_info
*di
=
644 container_of(notifier
, struct ds2760_device_info
, pm_notifier
);
647 case PM_HIBERNATION_PREPARE
:
648 case PM_SUSPEND_PREPARE
:
649 di
->charge_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
652 case PM_POST_RESTORE
:
653 case PM_POST_HIBERNATION
:
654 case PM_POST_SUSPEND
:
655 di
->charge_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
656 power_supply_changed(di
->bat
);
657 mod_delayed_work(di
->monitor_wqueue
, &di
->monitor_work
, HZ
);
661 case PM_RESTORE_PREPARE
:
669 static int w1_ds2760_add_slave(struct w1_slave
*sl
)
671 struct power_supply_config psy_cfg
= {};
672 struct ds2760_device_info
*di
;
673 struct device
*dev
= &sl
->dev
;
678 di
= devm_kzalloc(dev
, sizeof(*di
), GFP_KERNEL
);
681 goto di_alloc_failed
;
684 snprintf(name
, sizeof(name
), "ds2760-battery.%d", dev
->id
);
687 di
->bat_desc
.name
= name
;
688 di
->bat_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
689 di
->bat_desc
.properties
= ds2760_battery_props
;
690 di
->bat_desc
.num_properties
= ARRAY_SIZE(ds2760_battery_props
);
691 di
->bat_desc
.get_property
= ds2760_battery_get_property
;
692 di
->bat_desc
.set_property
= ds2760_battery_set_property
;
693 di
->bat_desc
.property_is_writeable
=
694 ds2760_battery_property_is_writeable
;
695 di
->bat_desc
.set_charged
= ds2760_battery_set_charged
;
696 di
->bat_desc
.external_power_changed
=
697 ds2760_battery_external_power_changed
;
699 psy_cfg
.drv_data
= di
;
704 psy_cfg
.of_node
= dev
->of_node
;
706 if (!of_property_read_bool(dev
->of_node
, "maxim,pmod-enabled"))
709 if (!of_property_read_u32(dev
->of_node
,
710 "maxim,cache-time-ms", &tmp
))
713 if (!of_property_read_u32(dev
->of_node
,
714 "rated-capacity-microamp-hours",
716 rated_capacity
= tmp
/ 10; /* property is in mAh */
719 di
->charge_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
721 sl
->family_data
= di
;
723 /* enable sleep mode feature */
724 ds2760_battery_read_status(di
);
725 status
= di
->raw
[DS2760_STATUS_REG
];
727 status
|= DS2760_STATUS_PMOD
;
729 status
&= ~DS2760_STATUS_PMOD
;
731 ds2760_battery_write_status(di
, status
);
733 /* set rated capacity from module param or device tree */
735 ds2760_battery_write_rated_capacity(di
, rated_capacity
);
737 /* set current accumulator if given as parameter.
738 * this should only be done for bootstrapping the value */
740 ds2760_battery_set_current_accum(di
, current_accum
);
742 di
->bat
= devm_power_supply_register(dev
, &di
->bat_desc
, &psy_cfg
);
743 if (IS_ERR(di
->bat
)) {
744 dev_err(di
->dev
, "failed to register battery\n");
745 retval
= PTR_ERR(di
->bat
);
749 INIT_DELAYED_WORK(&di
->monitor_work
, ds2760_battery_work
);
750 INIT_DELAYED_WORK(&di
->set_charged_work
,
751 ds2760_battery_set_charged_work
);
752 di
->monitor_wqueue
= alloc_ordered_workqueue(name
, WQ_MEM_RECLAIM
);
753 if (!di
->monitor_wqueue
) {
755 goto workqueue_failed
;
757 queue_delayed_work(di
->monitor_wqueue
, &di
->monitor_work
, HZ
* 1);
759 di
->pm_notifier
.notifier_call
= ds2760_pm_notifier
;
760 register_pm_notifier(&di
->pm_notifier
);
771 static void w1_ds2760_remove_slave(struct w1_slave
*sl
)
773 struct ds2760_device_info
*di
= sl
->family_data
;
775 unregister_pm_notifier(&di
->pm_notifier
);
776 cancel_delayed_work_sync(&di
->monitor_work
);
777 cancel_delayed_work_sync(&di
->set_charged_work
);
778 destroy_workqueue(di
->monitor_wqueue
);
782 static const struct of_device_id w1_ds2760_of_ids
[] = {
783 { .compatible
= "maxim,ds2760" },
788 static const struct w1_family_ops w1_ds2760_fops
= {
789 .add_slave
= w1_ds2760_add_slave
,
790 .remove_slave
= w1_ds2760_remove_slave
,
791 .groups
= w1_ds2760_groups
,
794 static struct w1_family w1_ds2760_family
= {
795 .fid
= W1_FAMILY_DS2760
,
796 .fops
= &w1_ds2760_fops
,
797 .of_match_table
= of_match_ptr(w1_ds2760_of_ids
),
799 module_w1_family(w1_ds2760_family
);
801 MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
802 "Matt Reimer <mreimer@vpop.net>, "
803 "Anton Vorontsov <cbou@mail.ru>");
804 MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
805 MODULE_LICENSE("GPL");
806 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760
));