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
= container_of(kobj
, struct device
, 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
[] = {
230 #ifdef CONFIG_MACH_H4700
231 1800, /* HP iPAQ hx4700 3.7V 1800mAh (359113-001) */
240 #ifdef CONFIG_MACH_H4700
242 3600, /* HP iPAQ hx4700 3.7V 3600mAh (359114-001) */
246 /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
247 * temp is in Celsius */
248 static int battery_interpolate(int array
[], int temp
)
260 return array
[index
] + (((array
[index
+ 1] - array
[index
]) * dt
) / 10);
263 static int ds2760_battery_read_status(struct ds2760_device_info
*di
)
265 int ret
, i
, start
, count
, scale
[5];
267 if (di
->update_time
&& time_before(jiffies
, di
->update_time
+
268 msecs_to_jiffies(cache_time
)))
271 /* The first time we read the entire contents of SRAM/EEPROM,
272 * but after that we just read the interesting bits that change. */
273 if (di
->update_time
== 0) {
275 count
= DS2760_DATA_SIZE
;
277 start
= DS2760_VOLTAGE_MSB
;
278 count
= DS2760_TEMP_LSB
- start
+ 1;
281 ret
= w1_ds2760_read(di
->dev
, di
->raw
+ start
, start
, count
);
283 dev_warn(di
->dev
, "call to w1_ds2760_read failed (0x%p)\n",
288 di
->update_time
= jiffies
;
290 /* DS2760 reports voltage in units of 4.88mV, but the battery class
291 * reports in units of uV, so convert by multiplying by 4880. */
292 di
->voltage_raw
= (di
->raw
[DS2760_VOLTAGE_MSB
] << 3) |
293 (di
->raw
[DS2760_VOLTAGE_LSB
] >> 5);
294 di
->voltage_uV
= di
->voltage_raw
* 4880;
296 /* DS2760 reports current in signed units of 0.625mA, but the battery
297 * class reports in units of µA, so convert by multiplying by 625. */
299 (((signed char)di
->raw
[DS2760_CURRENT_MSB
]) << 5) |
300 (di
->raw
[DS2760_CURRENT_LSB
] >> 3);
301 di
->current_uA
= di
->current_raw
* 625;
303 /* DS2760 reports accumulated current in signed units of 0.25mAh. */
304 di
->accum_current_raw
=
305 (((signed char)di
->raw
[DS2760_CURRENT_ACCUM_MSB
]) << 8) |
306 di
->raw
[DS2760_CURRENT_ACCUM_LSB
];
307 di
->accum_current_uAh
= di
->accum_current_raw
* 250;
309 /* DS2760 reports temperature in signed units of 0.125°C, but the
310 * battery class reports in units of 1/10 °C, so we convert by
311 * multiplying by .125 * 10 = 1.25. */
312 di
->temp_raw
= (((signed char)di
->raw
[DS2760_TEMP_MSB
]) << 3) |
313 (di
->raw
[DS2760_TEMP_LSB
] >> 5);
314 di
->temp_C
= di
->temp_raw
+ (di
->temp_raw
/ 4);
316 /* At least some battery monitors (e.g. HP iPAQ) store the battery's
317 * maximum rated capacity. */
318 if (di
->raw
[DS2760_RATED_CAPACITY
] < ARRAY_SIZE(rated_capacities
))
319 di
->rated_capacity
= rated_capacities
[
320 (unsigned int)di
->raw
[DS2760_RATED_CAPACITY
]];
322 di
->rated_capacity
= di
->raw
[DS2760_RATED_CAPACITY
] * 10;
324 di
->rated_capacity
*= 1000; /* convert to µAh */
326 /* Calculate the full level at the present temperature. */
327 di
->full_active_uAh
= di
->raw
[DS2760_ACTIVE_FULL
] << 8 |
328 di
->raw
[DS2760_ACTIVE_FULL
+ 1];
330 /* If the full_active_uAh value is not given, fall back to the rated
331 * capacity. This is likely to happen when chips are not part of the
332 * battery pack and is therefore not bootstrapped. */
333 if (di
->full_active_uAh
== 0)
334 di
->full_active_uAh
= di
->rated_capacity
/ 1000L;
336 scale
[0] = di
->full_active_uAh
;
337 for (i
= 1; i
< 5; i
++)
338 scale
[i
] = scale
[i
- 1] + di
->raw
[DS2760_ACTIVE_FULL
+ 1 + i
];
340 di
->full_active_uAh
= battery_interpolate(scale
, di
->temp_C
/ 10);
341 di
->full_active_uAh
*= 1000; /* convert to µAh */
343 /* Calculate the empty level at the present temperature. */
344 scale
[4] = di
->raw
[DS2760_ACTIVE_EMPTY
+ 4];
345 for (i
= 3; i
>= 0; i
--)
346 scale
[i
] = scale
[i
+ 1] + di
->raw
[DS2760_ACTIVE_EMPTY
+ i
];
348 di
->empty_uAh
= battery_interpolate(scale
, di
->temp_C
/ 10);
349 di
->empty_uAh
*= 1000; /* convert to µAh */
351 if (di
->full_active_uAh
== di
->empty_uAh
)
352 di
->rem_capacity
= 0;
354 /* From Maxim Application Note 131: remaining capacity =
355 * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
356 di
->rem_capacity
= ((di
->accum_current_uAh
- di
->empty_uAh
) * 100L) /
357 (di
->full_active_uAh
- di
->empty_uAh
);
359 if (di
->rem_capacity
< 0)
360 di
->rem_capacity
= 0;
361 if (di
->rem_capacity
> 100)
362 di
->rem_capacity
= 100;
364 if (di
->current_uA
< -100L)
365 di
->life_sec
= -((di
->accum_current_uAh
- di
->empty_uAh
) * 36L)
366 / (di
->current_uA
/ 100L);
373 static void ds2760_battery_set_current_accum(struct ds2760_device_info
*di
,
374 unsigned int acr_val
)
376 unsigned char acr
[2];
378 /* acr is in units of 0.25 mAh */
382 acr
[0] = acr_val
>> 8;
383 acr
[1] = acr_val
& 0xff;
385 if (w1_ds2760_write(di
->dev
, acr
, DS2760_CURRENT_ACCUM_MSB
, 2) < 2)
386 dev_warn(di
->dev
, "ACR write failed\n");
389 static void ds2760_battery_update_status(struct ds2760_device_info
*di
)
391 int old_charge_status
= di
->charge_status
;
393 ds2760_battery_read_status(di
);
395 if (di
->charge_status
== POWER_SUPPLY_STATUS_UNKNOWN
)
396 di
->full_counter
= 0;
398 if (power_supply_am_i_supplied(di
->bat
)) {
399 if (di
->current_uA
> 10000) {
400 di
->charge_status
= POWER_SUPPLY_STATUS_CHARGING
;
401 di
->full_counter
= 0;
402 } else if (di
->current_uA
< -5000) {
403 if (di
->charge_status
!= POWER_SUPPLY_STATUS_NOT_CHARGING
)
404 dev_notice(di
->dev
, "not enough power to "
406 di
->charge_status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
407 di
->full_counter
= 0;
408 } else if (di
->current_uA
< 10000 &&
409 di
->charge_status
!= POWER_SUPPLY_STATUS_FULL
) {
411 /* Don't consider the battery to be full unless
412 * we've seen the current < 10 mA at least two
413 * consecutive times. */
417 if (di
->full_counter
< 2) {
418 di
->charge_status
= POWER_SUPPLY_STATUS_CHARGING
;
420 di
->charge_status
= POWER_SUPPLY_STATUS_FULL
;
421 ds2760_battery_set_current_accum(di
,
422 di
->full_active_uAh
);
426 di
->charge_status
= POWER_SUPPLY_STATUS_DISCHARGING
;
427 di
->full_counter
= 0;
430 if (di
->charge_status
!= old_charge_status
)
431 power_supply_changed(di
->bat
);
434 static void ds2760_battery_write_status(struct ds2760_device_info
*di
,
437 if (status
== di
->raw
[DS2760_STATUS_REG
])
440 w1_ds2760_write(di
->dev
, &status
, DS2760_STATUS_WRITE_REG
, 1);
441 w1_ds2760_store_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
442 w1_ds2760_recall_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
445 static void ds2760_battery_write_rated_capacity(struct ds2760_device_info
*di
,
446 unsigned char rated_capacity
)
448 if (rated_capacity
== di
->raw
[DS2760_RATED_CAPACITY
])
451 w1_ds2760_write(di
->dev
, &rated_capacity
, DS2760_RATED_CAPACITY
, 1);
452 w1_ds2760_store_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
453 w1_ds2760_recall_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
456 static void ds2760_battery_write_active_full(struct ds2760_device_info
*di
,
459 unsigned char tmp
[2] = {
464 if (tmp
[0] == di
->raw
[DS2760_ACTIVE_FULL
] &&
465 tmp
[1] == di
->raw
[DS2760_ACTIVE_FULL
+ 1])
468 w1_ds2760_write(di
->dev
, tmp
, DS2760_ACTIVE_FULL
, sizeof(tmp
));
469 w1_ds2760_store_eeprom(di
->dev
, DS2760_EEPROM_BLOCK0
);
470 w1_ds2760_recall_eeprom(di
->dev
, DS2760_EEPROM_BLOCK0
);
472 /* Write to the di->raw[] buffer directly - the DS2760_ACTIVE_FULL
473 * values won't be read back by ds2760_battery_read_status() */
474 di
->raw
[DS2760_ACTIVE_FULL
] = tmp
[0];
475 di
->raw
[DS2760_ACTIVE_FULL
+ 1] = tmp
[1];
478 static void ds2760_battery_work(struct work_struct
*work
)
480 struct ds2760_device_info
*di
= container_of(work
,
481 struct ds2760_device_info
, monitor_work
.work
);
482 const int interval
= HZ
* 60;
484 dev_dbg(di
->dev
, "%s\n", __func__
);
486 ds2760_battery_update_status(di
);
487 queue_delayed_work(di
->monitor_wqueue
, &di
->monitor_work
, interval
);
490 static void ds2760_battery_external_power_changed(struct power_supply
*psy
)
492 struct ds2760_device_info
*di
= power_supply_get_drvdata(psy
);
494 dev_dbg(di
->dev
, "%s\n", __func__
);
496 mod_delayed_work(di
->monitor_wqueue
, &di
->monitor_work
, HZ
/10);
500 static void ds2760_battery_set_charged_work(struct work_struct
*work
)
503 struct ds2760_device_info
*di
= container_of(work
,
504 struct ds2760_device_info
, set_charged_work
.work
);
506 dev_dbg(di
->dev
, "%s\n", __func__
);
508 ds2760_battery_read_status(di
);
510 /* When we get notified by external circuitry that the battery is
511 * considered fully charged now, we know that there is no current
512 * flow any more. However, the ds2760's internal current meter is
513 * too inaccurate to rely on - spec say something ~15% failure.
514 * Hence, we use the current offset bias register to compensate
518 if (!power_supply_am_i_supplied(di
->bat
))
521 bias
= (signed char) di
->current_raw
+
522 (signed char) di
->raw
[DS2760_CURRENT_OFFSET_BIAS
];
524 dev_dbg(di
->dev
, "%s: bias = %d\n", __func__
, bias
);
526 w1_ds2760_write(di
->dev
, &bias
, DS2760_CURRENT_OFFSET_BIAS
, 1);
527 w1_ds2760_store_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
528 w1_ds2760_recall_eeprom(di
->dev
, DS2760_EEPROM_BLOCK1
);
530 /* Write to the di->raw[] buffer directly - the CURRENT_OFFSET_BIAS
531 * value won't be read back by ds2760_battery_read_status() */
532 di
->raw
[DS2760_CURRENT_OFFSET_BIAS
] = bias
;
535 static void ds2760_battery_set_charged(struct power_supply
*psy
)
537 struct ds2760_device_info
*di
= power_supply_get_drvdata(psy
);
539 /* postpone the actual work by 20 secs. This is for debouncing GPIO
540 * signals and to let the current value settle. See AN4188. */
541 mod_delayed_work(di
->monitor_wqueue
, &di
->set_charged_work
, HZ
* 20);
544 static int ds2760_battery_get_property(struct power_supply
*psy
,
545 enum power_supply_property psp
,
546 union power_supply_propval
*val
)
548 struct ds2760_device_info
*di
= power_supply_get_drvdata(psy
);
551 case POWER_SUPPLY_PROP_STATUS
:
552 val
->intval
= di
->charge_status
;
558 ds2760_battery_read_status(di
);
561 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
562 val
->intval
= di
->voltage_uV
;
564 case POWER_SUPPLY_PROP_CURRENT_NOW
:
565 val
->intval
= di
->current_uA
;
567 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
568 val
->intval
= di
->rated_capacity
;
570 case POWER_SUPPLY_PROP_CHARGE_FULL
:
571 val
->intval
= di
->full_active_uAh
;
573 case POWER_SUPPLY_PROP_CHARGE_EMPTY
:
574 val
->intval
= di
->empty_uAh
;
576 case POWER_SUPPLY_PROP_CHARGE_NOW
:
577 val
->intval
= di
->accum_current_uAh
;
579 case POWER_SUPPLY_PROP_TEMP
:
580 val
->intval
= di
->temp_C
;
582 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
583 val
->intval
= di
->life_sec
;
585 case POWER_SUPPLY_PROP_CAPACITY
:
586 val
->intval
= di
->rem_capacity
;
595 static int ds2760_battery_set_property(struct power_supply
*psy
,
596 enum power_supply_property psp
,
597 const union power_supply_propval
*val
)
599 struct ds2760_device_info
*di
= power_supply_get_drvdata(psy
);
602 case POWER_SUPPLY_PROP_CHARGE_FULL
:
603 /* the interface counts in uAh, convert the value */
604 ds2760_battery_write_active_full(di
, val
->intval
/ 1000L);
607 case POWER_SUPPLY_PROP_CHARGE_NOW
:
608 /* ds2760_battery_set_current_accum() does the conversion */
609 ds2760_battery_set_current_accum(di
, val
->intval
);
619 static int ds2760_battery_property_is_writeable(struct power_supply
*psy
,
620 enum power_supply_property psp
)
623 case POWER_SUPPLY_PROP_CHARGE_FULL
:
624 case POWER_SUPPLY_PROP_CHARGE_NOW
:
634 static enum power_supply_property ds2760_battery_props
[] = {
635 POWER_SUPPLY_PROP_STATUS
,
636 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
637 POWER_SUPPLY_PROP_CURRENT_NOW
,
638 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
639 POWER_SUPPLY_PROP_CHARGE_FULL
,
640 POWER_SUPPLY_PROP_CHARGE_EMPTY
,
641 POWER_SUPPLY_PROP_CHARGE_NOW
,
642 POWER_SUPPLY_PROP_TEMP
,
643 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
644 POWER_SUPPLY_PROP_CAPACITY
,
647 static int ds2760_pm_notifier(struct notifier_block
*notifier
,
648 unsigned long pm_event
,
651 struct ds2760_device_info
*di
=
652 container_of(notifier
, struct ds2760_device_info
, pm_notifier
);
655 case PM_HIBERNATION_PREPARE
:
656 case PM_SUSPEND_PREPARE
:
657 di
->charge_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
660 case PM_POST_RESTORE
:
661 case PM_POST_HIBERNATION
:
662 case PM_POST_SUSPEND
:
663 di
->charge_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
664 power_supply_changed(di
->bat
);
665 mod_delayed_work(di
->monitor_wqueue
, &di
->monitor_work
, HZ
);
669 case PM_RESTORE_PREPARE
:
677 static int w1_ds2760_add_slave(struct w1_slave
*sl
)
679 struct power_supply_config psy_cfg
= {};
680 struct ds2760_device_info
*di
;
681 struct device
*dev
= &sl
->dev
;
686 di
= devm_kzalloc(dev
, sizeof(*di
), GFP_KERNEL
);
689 goto di_alloc_failed
;
692 snprintf(name
, sizeof(name
), "ds2760-battery.%d", dev
->id
);
695 di
->bat_desc
.name
= name
;
696 di
->bat_desc
.type
= POWER_SUPPLY_TYPE_BATTERY
;
697 di
->bat_desc
.properties
= ds2760_battery_props
;
698 di
->bat_desc
.num_properties
= ARRAY_SIZE(ds2760_battery_props
);
699 di
->bat_desc
.get_property
= ds2760_battery_get_property
;
700 di
->bat_desc
.set_property
= ds2760_battery_set_property
;
701 di
->bat_desc
.property_is_writeable
=
702 ds2760_battery_property_is_writeable
;
703 di
->bat_desc
.set_charged
= ds2760_battery_set_charged
;
704 di
->bat_desc
.external_power_changed
=
705 ds2760_battery_external_power_changed
;
707 psy_cfg
.drv_data
= di
;
712 psy_cfg
.of_node
= dev
->of_node
;
714 if (!of_property_read_bool(dev
->of_node
, "maxim,pmod-enabled"))
717 if (!of_property_read_u32(dev
->of_node
,
718 "maxim,cache-time-ms", &tmp
))
721 if (!of_property_read_u32(dev
->of_node
,
722 "rated-capacity-microamp-hours",
724 rated_capacity
= tmp
/ 10; /* property is in mAh */
727 di
->charge_status
= POWER_SUPPLY_STATUS_UNKNOWN
;
729 sl
->family_data
= di
;
731 /* enable sleep mode feature */
732 ds2760_battery_read_status(di
);
733 status
= di
->raw
[DS2760_STATUS_REG
];
735 status
|= DS2760_STATUS_PMOD
;
737 status
&= ~DS2760_STATUS_PMOD
;
739 ds2760_battery_write_status(di
, status
);
741 /* set rated capacity from module param or device tree */
743 ds2760_battery_write_rated_capacity(di
, rated_capacity
);
745 /* set current accumulator if given as parameter.
746 * this should only be done for bootstrapping the value */
748 ds2760_battery_set_current_accum(di
, current_accum
);
750 di
->bat
= power_supply_register(dev
, &di
->bat_desc
, &psy_cfg
);
751 if (IS_ERR(di
->bat
)) {
752 dev_err(di
->dev
, "failed to register battery\n");
753 retval
= PTR_ERR(di
->bat
);
757 INIT_DELAYED_WORK(&di
->monitor_work
, ds2760_battery_work
);
758 INIT_DELAYED_WORK(&di
->set_charged_work
,
759 ds2760_battery_set_charged_work
);
760 di
->monitor_wqueue
= alloc_ordered_workqueue(name
, WQ_MEM_RECLAIM
);
761 if (!di
->monitor_wqueue
) {
763 goto workqueue_failed
;
765 queue_delayed_work(di
->monitor_wqueue
, &di
->monitor_work
, HZ
* 1);
767 di
->pm_notifier
.notifier_call
= ds2760_pm_notifier
;
768 register_pm_notifier(&di
->pm_notifier
);
773 power_supply_unregister(di
->bat
);
780 static void w1_ds2760_remove_slave(struct w1_slave
*sl
)
782 struct ds2760_device_info
*di
= sl
->family_data
;
784 unregister_pm_notifier(&di
->pm_notifier
);
785 cancel_delayed_work_sync(&di
->monitor_work
);
786 cancel_delayed_work_sync(&di
->set_charged_work
);
787 destroy_workqueue(di
->monitor_wqueue
);
788 power_supply_unregister(di
->bat
);
792 static const struct of_device_id w1_ds2760_of_ids
[] = {
793 { .compatible
= "maxim,ds2760" },
798 static struct w1_family_ops w1_ds2760_fops
= {
799 .add_slave
= w1_ds2760_add_slave
,
800 .remove_slave
= w1_ds2760_remove_slave
,
801 .groups
= w1_ds2760_groups
,
804 static struct w1_family w1_ds2760_family
= {
805 .fid
= W1_FAMILY_DS2760
,
806 .fops
= &w1_ds2760_fops
,
807 .of_match_table
= of_match_ptr(w1_ds2760_of_ids
),
809 module_w1_family(w1_ds2760_family
);
811 MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
812 "Matt Reimer <mreimer@vpop.net>, "
813 "Anton Vorontsov <cbou@mail.ru>");
814 MODULE_DESCRIPTION("1-wire Driver Dallas 2760 battery monitor chip");
815 MODULE_LICENSE("GPL");
816 MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS2760
));