1 /*
2 * Driver for batteries with DS2760 chips inside.
3 *
4 * Copyright © 2007 Anton Vorontsov
5 * 2004-2007 Matt Reimer
6 * 2004 Szabolcs Gyurko
7 *
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.
11 *
12 * Author: Anton Vorontsov <cbou@mail.ru>
13 * February 2007
14 *
15 * Matt Reimer <mreimer@vpop.net>
16 * April 2004, 2005, 2007
17 *
18 * Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
19 * September 2004
20 */
21
22 #include <linux/module.h>
23 #include <linux/param.h>
24 #include <linux/jiffies.h>
25 #include <linux/workqueue.h>
26 #include <linux/pm.h>
27 #include <linux/slab.h>
28 #include <linux/platform_device.h>
29 #include <linux/power_supply.h>
30 #include <linux/suspend.h>
31 #include <linux/w1.h>
32 #include <linux/of.h>
33
34 static unsigned int cache_time = 1000;
35 module_param(cache_time, uint, 0644);
36 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
37
38 static bool pmod_enabled;
39 module_param(pmod_enabled, bool, 0644);
40 MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
41
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");
45
46 static unsigned int current_accum;
47 module_param(current_accum, uint, 0644);
48 MODULE_PARM_DESC(current_accum, "current accumulator value");
49
50 #define W1_FAMILY_DS2760 0x30
51
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
59
60 /* Number of valid register addresses */
61 #define DS2760_DATA_SIZE 0x40
62
63 #define DS2760_PROTECTION_REG 0x00
64
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)
70
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
88
89 struct ds2760_device_info {
90 struct device *dev;
91
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_* */
109
110 int full_counter;
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;
117 };
118
w1_ds2760_io(struct device * dev,char * buf,int addr,size_t count,int io)119 static int w1_ds2760_io(struct device *dev, char *buf, int addr, size_t count,
120 int io)
121 {
122 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
123
124 if (!dev)
125 return 0;
126
127 mutex_lock(&sl->master->bus_mutex);
128
129 if (addr > DS2760_DATA_SIZE || addr < 0) {
130 count = 0;
131 goto out;
132 }
133 if (addr + count > DS2760_DATA_SIZE)
134 count = DS2760_DATA_SIZE - addr;
135
136 if (!w1_reset_select_slave(sl)) {
137 if (!io) {
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);
141 } else {
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 */
146 }
147 }
148
149 out:
150 mutex_unlock(&sl->master->bus_mutex);
151
152 return count;
153 }
154
w1_ds2760_read(struct device * dev,char * buf,int addr,size_t count)155 static int w1_ds2760_read(struct device *dev,
156 char *buf, int addr,
157 size_t count)
158 {
159 return w1_ds2760_io(dev, buf, addr, count, 0);
160 }
161
w1_ds2760_write(struct device * dev,char * buf,int addr,size_t count)162 static int w1_ds2760_write(struct device *dev,
163 char *buf,
164 int addr, size_t count)
165 {
166 return w1_ds2760_io(dev, buf, addr, count, 1);
167 }
168
w1_ds2760_eeprom_cmd(struct device * dev,int addr,int cmd)169 static int w1_ds2760_eeprom_cmd(struct device *dev, int addr, int cmd)
170 {
171 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
172
173 if (!dev)
174 return -EINVAL;
175
176 mutex_lock(&sl->master->bus_mutex);
177
178 if (w1_reset_select_slave(sl) == 0) {
179 w1_write_8(sl->master, cmd);
180 w1_write_8(sl->master, addr);
181 }
182
183 mutex_unlock(&sl->master->bus_mutex);
184 return 0;
185 }
186
w1_ds2760_store_eeprom(struct device * dev,int addr)187 static int w1_ds2760_store_eeprom(struct device *dev, int addr)
188 {
189 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_COPY_DATA);
190 }
191
w1_ds2760_recall_eeprom(struct device * dev,int addr)192 static int w1_ds2760_recall_eeprom(struct device *dev, int addr)
193 {
194 return w1_ds2760_eeprom_cmd(dev, addr, W1_DS2760_RECALL_DATA);
195 }
196
w1_slave_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)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)
200 {
201 struct device *dev = container_of(kobj, struct device, kobj);
202 return w1_ds2760_read(dev, buf, off, count);
203 }
204
205 static BIN_ATTR_RO(w1_slave, DS2760_DATA_SIZE);
206
207 static struct bin_attribute *w1_ds2760_bin_attrs[] = {
208 &bin_attr_w1_slave,
209 NULL,
210 };
211
212 static const struct attribute_group w1_ds2760_group = {
213 .bin_attrs = w1_ds2760_bin_attrs,
214 };
215
216 static const struct attribute_group *w1_ds2760_groups[] = {
217 &w1_ds2760_group,
218 NULL,
219 };
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[] = {
223 0,
224 920, /* Samsung */
225 920, /* BYD */
226 920, /* Lishen */
227 920, /* NEC */
228 1440, /* Samsung */
229 1440, /* BYD */
230 #ifdef CONFIG_MACH_H4700
231 1800, /* HP iPAQ hx4700 3.7V 1800mAh (359113-001) */
232 #else
233 1440, /* Lishen */
234 #endif
235 1440, /* NEC */
236 2880, /* Samsung */
237 2880, /* BYD */
238 2880, /* Lishen */
239 2880, /* NEC */
240 #ifdef CONFIG_MACH_H4700
241 0,
242 3600, /* HP iPAQ hx4700 3.7V 3600mAh (359114-001) */
243 #endif
244 };
245
246 /* array is level at temps 0°C, 10°C, 20°C, 30°C, 40°C
247 * temp is in Celsius */
battery_interpolate(int array[],int temp)248 static int battery_interpolate(int array[], int temp)
249 {
250 int index, dt;
251
252 if (temp <= 0)
253 return array[0];
254 if (temp >= 40)
255 return array[4];
256
257 index = temp / 10;
258 dt = temp % 10;
259
260 return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
261 }
262
ds2760_battery_read_status(struct ds2760_device_info * di)263 static int ds2760_battery_read_status(struct ds2760_device_info *di)
264 {
265 int ret, i, start, count, scale[5];
266
267 if (di->update_time && time_before(jiffies, di->update_time +
268 msecs_to_jiffies(cache_time)))
269 return 0;
270
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) {
274 start = 0;
275 count = DS2760_DATA_SIZE;
276 } else {
277 start = DS2760_VOLTAGE_MSB;
278 count = DS2760_TEMP_LSB - start + 1;
279 }
280
281 ret = w1_ds2760_read(di->dev, di->raw + start, start, count);
282 if (ret != count) {
283 dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
284 di->dev);
285 return 1;
286 }
287
288 di->update_time = jiffies;
289
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;
295
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. */
298 di->current_raw =
299 (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
300 (di->raw[DS2760_CURRENT_LSB] >> 3);
301 di->current_uA = di->current_raw * 625;
302
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;
308
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);
315
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]];
321 else
322 di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
323
324 di->rated_capacity *= 1000; /* convert to µAh */
325
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];
329
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;
335
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];
339
340 di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
341 di->full_active_uAh *= 1000; /* convert to µAh */
342
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];
347
348 di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
349 di->empty_uAh *= 1000; /* convert to µAh */
350
351 if (di->full_active_uAh == di->empty_uAh)
352 di->rem_capacity = 0;
353 else
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);
358
359 if (di->rem_capacity < 0)
360 di->rem_capacity = 0;
361 if (di->rem_capacity > 100)
362 di->rem_capacity = 100;
363
364 if (di->current_uA < -100L)
365 di->life_sec = -((di->accum_current_uAh - di->empty_uAh) * 36L)
366 / (di->current_uA / 100L);
367 else
368 di->life_sec = 0;
369
370 return 0;
371 }
372
ds2760_battery_set_current_accum(struct ds2760_device_info * di,unsigned int acr_val)373 static void ds2760_battery_set_current_accum(struct ds2760_device_info *di,
374 unsigned int acr_val)
375 {
376 unsigned char acr[2];
377
378 /* acr is in units of 0.25 mAh */
379 acr_val *= 4L;
380 acr_val /= 1000;
381
382 acr[0] = acr_val >> 8;
383 acr[1] = acr_val & 0xff;
384
385 if (w1_ds2760_write(di->dev, acr, DS2760_CURRENT_ACCUM_MSB, 2) < 2)
386 dev_warn(di->dev, "ACR write failed\n");
387 }
388
ds2760_battery_update_status(struct ds2760_device_info * di)389 static void ds2760_battery_update_status(struct ds2760_device_info *di)
390 {
391 int old_charge_status = di->charge_status;
392
393 ds2760_battery_read_status(di);
394
395 if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
396 di->full_counter = 0;
397
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 "
405 "charge\n");
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) {
410
411 /* Don't consider the battery to be full unless
412 * we've seen the current < 10 mA at least two
413 * consecutive times. */
414
415 di->full_counter++;
416
417 if (di->full_counter < 2) {
418 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
419 } else {
420 di->charge_status = POWER_SUPPLY_STATUS_FULL;
421 ds2760_battery_set_current_accum(di,
422 di->full_active_uAh);
423 }
424 }
425 } else {
426 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
427 di->full_counter = 0;
428 }
429
430 if (di->charge_status != old_charge_status)
431 power_supply_changed(di->bat);
432 }
433
ds2760_battery_write_status(struct ds2760_device_info * di,char status)434 static void ds2760_battery_write_status(struct ds2760_device_info *di,
435 char status)
436 {
437 if (status == di->raw[DS2760_STATUS_REG])
438 return;
439
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);
443 }
444
ds2760_battery_write_rated_capacity(struct ds2760_device_info * di,unsigned char rated_capacity)445 static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
446 unsigned char rated_capacity)
447 {
448 if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
449 return;
450
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);
454 }
455
ds2760_battery_write_active_full(struct ds2760_device_info * di,int active_full)456 static void ds2760_battery_write_active_full(struct ds2760_device_info *di,
457 int active_full)
458 {
459 unsigned char tmp[2] = {
460 active_full >> 8,
461 active_full & 0xff
462 };
463
464 if (tmp[0] == di->raw[DS2760_ACTIVE_FULL] &&
465 tmp[1] == di->raw[DS2760_ACTIVE_FULL + 1])
466 return;
467
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);
471
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];
476 }
477
ds2760_battery_work(struct work_struct * work)478 static void ds2760_battery_work(struct work_struct *work)
479 {
480 struct ds2760_device_info *di = container_of(work,
481 struct ds2760_device_info, monitor_work.work);
482 const int interval = HZ * 60;
483
484 dev_dbg(di->dev, "%s\n", __func__);
485
486 ds2760_battery_update_status(di);
487 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
488 }
489
ds2760_battery_external_power_changed(struct power_supply * psy)490 static void ds2760_battery_external_power_changed(struct power_supply *psy)
491 {
492 struct ds2760_device_info *di = power_supply_get_drvdata(psy);
493
494 dev_dbg(di->dev, "%s\n", __func__);
495
496 mod_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
497 }
498
499
ds2760_battery_set_charged_work(struct work_struct * work)500 static void ds2760_battery_set_charged_work(struct work_struct *work)
501 {
502 char bias;
503 struct ds2760_device_info *di = container_of(work,
504 struct ds2760_device_info, set_charged_work.work);
505
506 dev_dbg(di->dev, "%s\n", __func__);
507
508 ds2760_battery_read_status(di);
509
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
515 * that error.
516 */
517
518 if (!power_supply_am_i_supplied(di->bat))
519 return;
520
521 bias = (signed char) di->current_raw +
522 (signed char) di->raw[DS2760_CURRENT_OFFSET_BIAS];
523
524 dev_dbg(di->dev, "%s: bias = %d\n", __func__, bias);
525
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);
529
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;
533 }
534
ds2760_battery_set_charged(struct power_supply * psy)535 static void ds2760_battery_set_charged(struct power_supply *psy)
536 {
537 struct ds2760_device_info *di = power_supply_get_drvdata(psy);
538
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);
542 }
543
ds2760_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)544 static int ds2760_battery_get_property(struct power_supply *psy,
545 enum power_supply_property psp,
546 union power_supply_propval *val)
547 {
548 struct ds2760_device_info *di = power_supply_get_drvdata(psy);
549
550 switch (psp) {
551 case POWER_SUPPLY_PROP_STATUS:
552 val->intval = di->charge_status;
553 return 0;
554 default:
555 break;
556 }
557
558 ds2760_battery_read_status(di);
559
560 switch (psp) {
561 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
562 val->intval = di->voltage_uV;
563 break;
564 case POWER_SUPPLY_PROP_CURRENT_NOW:
565 val->intval = di->current_uA;
566 break;
567 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
568 val->intval = di->rated_capacity;
569 break;
570 case POWER_SUPPLY_PROP_CHARGE_FULL:
571 val->intval = di->full_active_uAh;
572 break;
573 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
574 val->intval = di->empty_uAh;
575 break;
576 case POWER_SUPPLY_PROP_CHARGE_NOW:
577 val->intval = di->accum_current_uAh;
578 break;
579 case POWER_SUPPLY_PROP_TEMP:
580 val->intval = di->temp_C;
581 break;
582 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
583 val->intval = di->life_sec;
584 break;
585 case POWER_SUPPLY_PROP_CAPACITY:
586 val->intval = di->rem_capacity;
587 break;
588 default:
589 return -EINVAL;
590 }
591
592 return 0;
593 }
594
ds2760_battery_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)595 static int ds2760_battery_set_property(struct power_supply *psy,
596 enum power_supply_property psp,
597 const union power_supply_propval *val)
598 {
599 struct ds2760_device_info *di = power_supply_get_drvdata(psy);
600
601 switch (psp) {
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);
605 break;
606
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);
610 break;
611
612 default:
613 return -EPERM;
614 }
615
616 return 0;
617 }
618
ds2760_battery_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)619 static int ds2760_battery_property_is_writeable(struct power_supply *psy,
620 enum power_supply_property psp)
621 {
622 switch (psp) {
623 case POWER_SUPPLY_PROP_CHARGE_FULL:
624 case POWER_SUPPLY_PROP_CHARGE_NOW:
625 return 1;
626
627 default:
628 break;
629 }
630
631 return 0;
632 }
633
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,
645 };
646
ds2760_pm_notifier(struct notifier_block * notifier,unsigned long pm_event,void * unused)647 static int ds2760_pm_notifier(struct notifier_block *notifier,
648 unsigned long pm_event,
649 void *unused)
650 {
651 struct ds2760_device_info *di =
652 container_of(notifier, struct ds2760_device_info, pm_notifier);
653
654 switch (pm_event) {
655 case PM_HIBERNATION_PREPARE:
656 case PM_SUSPEND_PREPARE:
657 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
658 break;
659
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);
666
667 break;
668
669 case PM_RESTORE_PREPARE:
670 default:
671 break;
672 }
673
674 return NOTIFY_DONE;
675 }
676
w1_ds2760_add_slave(struct w1_slave * sl)677 static int w1_ds2760_add_slave(struct w1_slave *sl)
678 {
679 struct power_supply_config psy_cfg = {};
680 struct ds2760_device_info *di;
681 struct device *dev = &sl->dev;
682 int retval = 0;
683 char name[32];
684 char status;
685
686 di = devm_kzalloc(dev, sizeof(*di), GFP_KERNEL);
687 if (!di) {
688 retval = -ENOMEM;
689 goto di_alloc_failed;
690 }
691
692 snprintf(name, sizeof(name), "ds2760-battery.%d", dev->id);
693
694 di->dev = dev;
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;
706
707 psy_cfg.drv_data = di;
708
709 if (dev->of_node) {
710 u32 tmp;
711
712 psy_cfg.of_node = dev->of_node;
713
714 if (!of_property_read_bool(dev->of_node, "maxim,pmod-enabled"))
715 pmod_enabled = true;
716
717 if (!of_property_read_u32(dev->of_node,
718 "maxim,cache-time-ms", &tmp))
719 cache_time = tmp;
720
721 if (!of_property_read_u32(dev->of_node,
722 "rated-capacity-microamp-hours",
723 &tmp))
724 rated_capacity = tmp / 10; /* property is in mAh */
725 }
726
727 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
728
729 sl->family_data = di;
730
731 /* enable sleep mode feature */
732 ds2760_battery_read_status(di);
733 status = di->raw[DS2760_STATUS_REG];
734 if (pmod_enabled)
735 status |= DS2760_STATUS_PMOD;
736 else
737 status &= ~DS2760_STATUS_PMOD;
738
739 ds2760_battery_write_status(di, status);
740
741 /* set rated capacity from module param or device tree */
742 if (rated_capacity)
743 ds2760_battery_write_rated_capacity(di, rated_capacity);
744
745 /* set current accumulator if given as parameter.
746 * this should only be done for bootstrapping the value */
747 if (current_accum)
748 ds2760_battery_set_current_accum(di, current_accum);
749
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);
754 goto batt_failed;
755 }
756
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) {
762 retval = -ESRCH;
763 goto workqueue_failed;
764 }
765 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
766
767 di->pm_notifier.notifier_call = ds2760_pm_notifier;
768 register_pm_notifier(&di->pm_notifier);
769
770 goto success;
771
772 workqueue_failed:
773 power_supply_unregister(di->bat);
774 batt_failed:
775 di_alloc_failed:
776 success:
777 return retval;
778 }
779
w1_ds2760_remove_slave(struct w1_slave * sl)780 static void w1_ds2760_remove_slave(struct w1_slave *sl)
781 {
782 struct ds2760_device_info *di = sl->family_data;
783
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);
789 }
790
791 #ifdef CONFIG_OF
792 static const struct of_device_id w1_ds2760_of_ids[] = {
793 { .compatible = "maxim,ds2760" },
794 {}
795 };
796 #endif
797
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,
802 };
803
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),
808 };
809 module_w1_family(w1_ds2760_family);
810
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));
817