1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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/platform_device.h>
28#include <linux/power_supply.h>
29
30#include "../w1/w1.h"
31#include "../w1/slaves/w1_ds2760.h"
32
33struct ds2760_device_info {
34 struct device *dev;
35
36
37 unsigned long update_time;
38 char raw[DS2760_DATA_SIZE];
39 int voltage_raw;
40 int voltage_uV;
41 int current_raw;
42 int current_uA;
43 int accum_current_raw;
44 int accum_current_uAh;
45 int temp_raw;
46 int temp_C;
47 int rated_capacity;
48 int rem_capacity;
49 int full_active_uAh;
50 int empty_uAh;
51 int life_sec;
52 int charge_status;
53
54 int full_counter;
55 struct power_supply bat;
56 struct device *w1_dev;
57 struct workqueue_struct *monitor_wqueue;
58 struct delayed_work monitor_work;
59};
60
61static unsigned int cache_time = 1000;
62module_param(cache_time, uint, 0644);
63MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
64
65
66
67static int rated_capacities[] = {
68 0,
69 920,
70 920,
71 920,
72 920,
73 1440,
74 1440,
75 1440,
76 1440,
77 2880,
78 2880,
79 2880,
80 2880
81};
82
83
84
85static int battery_interpolate(int array[], int temp)
86{
87 int index, dt;
88
89 if (temp <= 0)
90 return array[0];
91 if (temp >= 40)
92 return array[4];
93
94 index = temp / 10;
95 dt = temp % 10;
96
97 return array[index] + (((array[index + 1] - array[index]) * dt) / 10);
98}
99
100static int ds2760_battery_read_status(struct ds2760_device_info *di)
101{
102 int ret, i, start, count, scale[5];
103
104 if (di->update_time && time_before(jiffies, di->update_time +
105 msecs_to_jiffies(cache_time)))
106 return 0;
107
108
109
110 if (di->update_time == 0) {
111 start = 0;
112 count = DS2760_DATA_SIZE;
113 } else {
114 start = DS2760_VOLTAGE_MSB;
115 count = DS2760_TEMP_LSB - start + 1;
116 }
117
118 ret = w1_ds2760_read(di->w1_dev, di->raw + start, start, count);
119 if (ret != count) {
120 dev_warn(di->dev, "call to w1_ds2760_read failed (0x%p)\n",
121 di->w1_dev);
122 return 1;
123 }
124
125 di->update_time = jiffies;
126
127
128
129 di->voltage_raw = (di->raw[DS2760_VOLTAGE_MSB] << 3) |
130 (di->raw[DS2760_VOLTAGE_LSB] >> 5);
131 di->voltage_uV = di->voltage_raw * 4880;
132
133
134
135 di->current_raw =
136 (((signed char)di->raw[DS2760_CURRENT_MSB]) << 5) |
137 (di->raw[DS2760_CURRENT_LSB] >> 3);
138 di->current_uA = di->current_raw * 625;
139
140
141 di->accum_current_raw =
142 (((signed char)di->raw[DS2760_CURRENT_ACCUM_MSB]) << 8) |
143 di->raw[DS2760_CURRENT_ACCUM_LSB];
144 di->accum_current_uAh = di->accum_current_raw * 250;
145
146
147
148
149 di->temp_raw = (((signed char)di->raw[DS2760_TEMP_MSB]) << 3) |
150 (di->raw[DS2760_TEMP_LSB] >> 5);
151 di->temp_C = di->temp_raw + (di->temp_raw / 4);
152
153
154
155 if (di->raw[DS2760_RATED_CAPACITY] < ARRAY_SIZE(rated_capacities))
156 di->rated_capacity = rated_capacities[
157 (unsigned int)di->raw[DS2760_RATED_CAPACITY]];
158 else
159 di->rated_capacity = di->raw[DS2760_RATED_CAPACITY] * 10;
160
161 di->rated_capacity *= 1000;
162
163
164 di->full_active_uAh = di->raw[DS2760_ACTIVE_FULL] << 8 |
165 di->raw[DS2760_ACTIVE_FULL + 1];
166
167 scale[0] = di->raw[DS2760_ACTIVE_FULL] << 8 |
168 di->raw[DS2760_ACTIVE_FULL + 1];
169 for (i = 1; i < 5; i++)
170 scale[i] = scale[i - 1] + di->raw[DS2760_ACTIVE_FULL + 2 + i];
171
172 di->full_active_uAh = battery_interpolate(scale, di->temp_C / 10);
173 di->full_active_uAh *= 1000;
174
175
176 scale[4] = di->raw[DS2760_ACTIVE_EMPTY + 4];
177 for (i = 3; i >= 0; i--)
178 scale[i] = scale[i + 1] + di->raw[DS2760_ACTIVE_EMPTY + i];
179
180 di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
181 di->empty_uAh *= 1000;
182
183 if (di->full_active_uAh == di->empty_uAh)
184 di->rem_capacity = 0;
185 else
186
187
188 di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
189 (di->full_active_uAh - di->empty_uAh);
190
191 if (di->rem_capacity < 0)
192 di->rem_capacity = 0;
193 if (di->rem_capacity > 100)
194 di->rem_capacity = 100;
195
196 if (di->current_uA)
197 di->life_sec = -((di->accum_current_uAh - di->empty_uAh) *
198 3600L) / di->current_uA;
199 else
200 di->life_sec = 0;
201
202 return 0;
203}
204
205static void ds2760_battery_update_status(struct ds2760_device_info *di)
206{
207 int old_charge_status = di->charge_status;
208
209 ds2760_battery_read_status(di);
210
211 if (di->charge_status == POWER_SUPPLY_STATUS_UNKNOWN)
212 di->full_counter = 0;
213
214 if (power_supply_am_i_supplied(&di->bat)) {
215 if (di->current_uA > 10000) {
216 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
217 di->full_counter = 0;
218 } else if (di->current_uA < -5000) {
219 if (di->charge_status != POWER_SUPPLY_STATUS_NOT_CHARGING)
220 dev_notice(di->dev, "not enough power to "
221 "charge\n");
222 di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
223 di->full_counter = 0;
224 } else if (di->current_uA < 10000 &&
225 di->charge_status != POWER_SUPPLY_STATUS_FULL) {
226
227
228
229
230
231 di->full_counter++;
232
233 if (di->full_counter < 2) {
234 di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
235 } else {
236 unsigned char acr[2];
237 int acr_val;
238
239
240 acr_val = di->full_active_uAh * 4L / 1000;
241
242 acr[0] = acr_val >> 8;
243 acr[1] = acr_val & 0xff;
244
245 if (w1_ds2760_write(di->w1_dev, acr,
246 DS2760_CURRENT_ACCUM_MSB, 2) < 2)
247 dev_warn(di->dev,
248 "ACR reset failed\n");
249
250 di->charge_status = POWER_SUPPLY_STATUS_FULL;
251 }
252 }
253 } else {
254 di->charge_status = POWER_SUPPLY_STATUS_DISCHARGING;
255 di->full_counter = 0;
256 }
257
258 if (di->charge_status != old_charge_status)
259 power_supply_changed(&di->bat);
260}
261
262static void ds2760_battery_work(struct work_struct *work)
263{
264 struct ds2760_device_info *di = container_of(work,
265 struct ds2760_device_info, monitor_work.work);
266 const int interval = HZ * 60;
267
268 dev_dbg(di->dev, "%s\n", __func__);
269
270 ds2760_battery_update_status(di);
271 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, interval);
272}
273
274#define to_ds2760_device_info(x) container_of((x), struct ds2760_device_info, \
275 bat);
276
277static void ds2760_battery_external_power_changed(struct power_supply *psy)
278{
279 struct ds2760_device_info *di = to_ds2760_device_info(psy);
280
281 dev_dbg(di->dev, "%s\n", __func__);
282
283 cancel_delayed_work(&di->monitor_work);
284 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ/10);
285}
286
287static int ds2760_battery_get_property(struct power_supply *psy,
288 enum power_supply_property psp,
289 union power_supply_propval *val)
290{
291 struct ds2760_device_info *di = to_ds2760_device_info(psy);
292
293 switch (psp) {
294 case POWER_SUPPLY_PROP_STATUS:
295 val->intval = di->charge_status;
296 return 0;
297 default:
298 break;
299 }
300
301 ds2760_battery_read_status(di);
302
303 switch (psp) {
304 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
305 val->intval = di->voltage_uV;
306 break;
307 case POWER_SUPPLY_PROP_CURRENT_NOW:
308 val->intval = di->current_uA;
309 break;
310 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
311 val->intval = di->rated_capacity;
312 break;
313 case POWER_SUPPLY_PROP_CHARGE_FULL:
314 val->intval = di->full_active_uAh;
315 break;
316 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
317 val->intval = di->empty_uAh;
318 break;
319 case POWER_SUPPLY_PROP_CHARGE_NOW:
320 val->intval = di->accum_current_uAh;
321 break;
322 case POWER_SUPPLY_PROP_TEMP:
323 val->intval = di->temp_C;
324 break;
325 default:
326 return -EINVAL;
327 }
328
329 return 0;
330}
331
332static enum power_supply_property ds2760_battery_props[] = {
333 POWER_SUPPLY_PROP_STATUS,
334 POWER_SUPPLY_PROP_VOLTAGE_NOW,
335 POWER_SUPPLY_PROP_CURRENT_NOW,
336 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
337 POWER_SUPPLY_PROP_CHARGE_FULL,
338 POWER_SUPPLY_PROP_CHARGE_EMPTY,
339 POWER_SUPPLY_PROP_CHARGE_NOW,
340 POWER_SUPPLY_PROP_TEMP,
341};
342
343static int ds2760_battery_probe(struct platform_device *pdev)
344{
345 int retval = 0;
346 struct ds2760_device_info *di;
347 struct ds2760_platform_data *pdata;
348
349 di = kzalloc(sizeof(*di), GFP_KERNEL);
350 if (!di) {
351 retval = -ENOMEM;
352 goto di_alloc_failed;
353 }
354
355 platform_set_drvdata(pdev, di);
356
357 pdata = pdev->dev.platform_data;
358 di->dev = &pdev->dev;
359 di->w1_dev = pdev->dev.parent;
360 di->bat.name = dev_name(&pdev->dev);
361 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
362 di->bat.properties = ds2760_battery_props;
363 di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
364 di->bat.get_property = ds2760_battery_get_property;
365 di->bat.external_power_changed =
366 ds2760_battery_external_power_changed;
367
368 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
369
370 retval = power_supply_register(&pdev->dev, &di->bat);
371 if (retval) {
372 dev_err(di->dev, "failed to register battery\n");
373 goto batt_failed;
374 }
375
376 INIT_DELAYED_WORK(&di->monitor_work, ds2760_battery_work);
377 di->monitor_wqueue = create_singlethread_workqueue(dev_name(&pdev->dev));
378 if (!di->monitor_wqueue) {
379 retval = -ESRCH;
380 goto workqueue_failed;
381 }
382 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ * 1);
383
384 goto success;
385
386workqueue_failed:
387 power_supply_unregister(&di->bat);
388batt_failed:
389 kfree(di);
390di_alloc_failed:
391success:
392 return retval;
393}
394
395static int ds2760_battery_remove(struct platform_device *pdev)
396{
397 struct ds2760_device_info *di = platform_get_drvdata(pdev);
398
399 cancel_rearming_delayed_workqueue(di->monitor_wqueue,
400 &di->monitor_work);
401 destroy_workqueue(di->monitor_wqueue);
402 power_supply_unregister(&di->bat);
403
404 return 0;
405}
406
407#ifdef CONFIG_PM
408
409static int ds2760_battery_suspend(struct platform_device *pdev,
410 pm_message_t state)
411{
412 struct ds2760_device_info *di = platform_get_drvdata(pdev);
413
414 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
415
416 return 0;
417}
418
419static int ds2760_battery_resume(struct platform_device *pdev)
420{
421 struct ds2760_device_info *di = platform_get_drvdata(pdev);
422
423 di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
424 power_supply_changed(&di->bat);
425
426 cancel_delayed_work(&di->monitor_work);
427 queue_delayed_work(di->monitor_wqueue, &di->monitor_work, HZ);
428
429 return 0;
430}
431
432#else
433
434#define ds2760_battery_suspend NULL
435#define ds2760_battery_resume NULL
436
437#endif
438
439MODULE_ALIAS("platform:ds2760-battery");
440
441static struct platform_driver ds2760_battery_driver = {
442 .driver = {
443 .name = "ds2760-battery",
444 },
445 .probe = ds2760_battery_probe,
446 .remove = ds2760_battery_remove,
447 .suspend = ds2760_battery_suspend,
448 .resume = ds2760_battery_resume,
449};
450
451static int __init ds2760_battery_init(void)
452{
453 return platform_driver_register(&ds2760_battery_driver);
454}
455
456static void __exit ds2760_battery_exit(void)
457{
458 platform_driver_unregister(&ds2760_battery_driver);
459}
460
461module_init(ds2760_battery_init);
462module_exit(ds2760_battery_exit);
463
464MODULE_LICENSE("GPL");
465MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
466 "Matt Reimer <mreimer@vpop.net>, "
467 "Anton Vorontsov <cbou@mail.ru>");
468MODULE_DESCRIPTION("ds2760 battery driver");
469