1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
/*
* Battery driver for wm8350 PMIC
*
* Copyright 2007, 2008 Wolfson Microelectronics PLC.
*
* Based on OLPC Battery Driver
*
* Copyright 2006 David Woodhouse <dwmw2@infradead.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/mfd/wm8350/supply.h>
#include <linux/mfd/wm8350/core.h>
#include <linux/mfd/wm8350/comparator.h>
static int wm8350_read_battery_uvolts(struct wm8350 *wm8350)
{
return wm8350_read_auxadc(wm8350, WM8350_AUXADC_BATT, 0, 0)
* WM8350_AUX_COEFF;
}
static int wm8350_read_line_uvolts(struct wm8350 *wm8350)
{
return wm8350_read_auxadc(wm8350, WM8350_AUXADC_LINE, 0, 0)
* WM8350_AUX_COEFF;
}
static int wm8350_read_usb_uvolts(struct wm8350 *wm8350)
{
return wm8350_read_auxadc(wm8350, WM8350_AUXADC_USB, 0, 0)
* WM8350_AUX_COEFF;
}
#define WM8350_BATT_SUPPLY 1
#define WM8350_USB_SUPPLY 2
#define WM8350_LINE_SUPPLY 4
static inline int wm8350_charge_time_min(struct wm8350 *wm8350, int min)
{
if (!wm8350->power.rev_g_coeff)
return (((min - 30) / 15) & 0xf) << 8;
else
return (((min - 30) / 30) & 0xf) << 8;
}
static int wm8350_get_supplies(struct wm8350 *wm8350)
{
u16 sm, ov, co, chrg;
int supplies = 0;
sm = wm8350_reg_read(wm8350, WM8350_STATE_MACHINE_STATUS);
ov = wm8350_reg_read(wm8350, WM8350_MISC_OVERRIDES);
co = wm8350_reg_read(wm8350, WM8350_COMPARATOR_OVERRIDES);
chrg = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2);
/* USB_SM */
sm = (sm & WM8350_USB_SM_MASK) >> WM8350_USB_SM_SHIFT;
/* CHG_ISEL */
chrg &= WM8350_CHG_ISEL_MASK;
/* If the USB state machine is active then we're using that with or
* without battery, otherwise check for wall supply */
if (((sm == WM8350_USB_SM_100_SLV) ||
(sm == WM8350_USB_SM_500_SLV) ||
(sm == WM8350_USB_SM_STDBY_SLV))
&& !(ov & WM8350_USB_LIMIT_OVRDE))
supplies = WM8350_USB_SUPPLY;
else if (((sm == WM8350_USB_SM_100_SLV) ||
(sm == WM8350_USB_SM_500_SLV) ||
(sm == WM8350_USB_SM_STDBY_SLV))
&& (ov & WM8350_USB_LIMIT_OVRDE) && (chrg == 0))
supplies = WM8350_USB_SUPPLY | WM8350_BATT_SUPPLY;
else if (co & WM8350_WALL_FB_OVRDE)
supplies = WM8350_LINE_SUPPLY;
else
supplies = WM8350_BATT_SUPPLY;
return supplies;
}
static int wm8350_charger_config(struct wm8350 *wm8350,
struct wm8350_charger_policy *policy)
{
u16 reg, eoc_mA, fast_limit_mA;
if (!policy) {
dev_warn(wm8350->dev,
"No charger policy, charger not configured.\n");
return -EINVAL;
}
/* make sure USB fast charge current is not > 500mA */
if (policy->fast_limit_USB_mA > 500) {
dev_err(wm8350->dev, "USB fast charge > 500mA\n");
return -EINVAL;
}
eoc_mA = WM8350_CHG_EOC_mA(policy->eoc_mA);
wm8350_reg_unlock(wm8350);
reg = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_1)
& WM8350_CHG_ENA_R168;
wm8350_reg_write(wm8350, WM8350_BATTERY_CHARGER_CONTROL_1,
reg | eoc_mA | policy->trickle_start_mV |
WM8350_CHG_TRICKLE_TEMP_CHOKE |
WM8350_CHG_TRICKLE_USB_CHOKE |
WM8350_CHG_FAST_USB_THROTTLE);
if (wm8350_get_supplies(wm8350) & WM8350_USB_SUPPLY) {
fast_limit_mA =
WM8350_CHG_FAST_LIMIT_mA(policy->fast_limit_USB_mA);
wm8350_reg_write(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2,
policy->charge_mV | policy->trickle_charge_USB_mA |
fast_limit_mA | wm8350_charge_time_min(wm8350,
policy->charge_timeout));
} else {
fast_limit_mA =
WM8350_CHG_FAST_LIMIT_mA(policy->fast_limit_mA);
wm8350_reg_write(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2,
policy->charge_mV | policy->trickle_charge_mA |
fast_limit_mA | wm8350_charge_time_min(wm8350,
policy->charge_timeout));
}
wm8350_reg_lock(wm8350);
return 0;
}
static int wm8350_batt_status(struct wm8350 *wm8350)
{
u16 state;
state = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2);
state &= WM8350_CHG_STS_MASK;
switch (state) {
case WM8350_CHG_STS_OFF:
return POWER_SUPPLY_STATUS_DISCHARGING;
case WM8350_CHG_STS_TRICKLE:
case WM8350_CHG_STS_FAST:
return POWER_SUPPLY_STATUS_CHARGING;
default:
return POWER_SUPPLY_STATUS_UNKNOWN;
}
}
static ssize_t charger_state_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct wm8350 *wm8350 = dev_get_drvdata(dev);
char *charge;
int state;
state = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2) &
WM8350_CHG_STS_MASK;
switch (state) {
case WM8350_CHG_STS_OFF:
charge = "Charger Off";
break;
case WM8350_CHG_STS_TRICKLE:
charge = "Trickle Charging";
break;
case WM8350_CHG_STS_FAST:
charge = "Fast Charging";
break;
default:
return 0;
}
return sprintf(buf, "%s\n", charge);
}
static DEVICE_ATTR(charger_state, 0444, charger_state_show, NULL);
static irqreturn_t wm8350_charger_handler(int irq, void *data)
{
struct wm8350 *wm8350 = data;
struct wm8350_power *power = &wm8350->power;
struct wm8350_charger_policy *policy = power->policy;
switch (irq - wm8350->irq_base) {
case WM8350_IRQ_CHG_BAT_FAIL:
dev_err(wm8350->dev, "battery failed\n");
break;
case WM8350_IRQ_CHG_TO:
dev_err(wm8350->dev, "charger timeout\n");
power_supply_changed(&power->battery);
break;
case WM8350_IRQ_CHG_BAT_HOT:
case WM8350_IRQ_CHG_BAT_COLD:
case WM8350_IRQ_CHG_START:
case WM8350_IRQ_CHG_END:
power_supply_changed(&power->battery);
break;
case WM8350_IRQ_CHG_FAST_RDY:
dev_dbg(wm8350->dev, "fast charger ready\n");
wm8350_charger_config(wm8350, policy);
wm8350_reg_unlock(wm8350);
wm8350_set_bits(wm8350, WM8350_BATTERY_CHARGER_CONTROL_1,
WM8350_CHG_FAST);
wm8350_reg_lock(wm8350);
break;
case WM8350_IRQ_CHG_VBATT_LT_3P9:
dev_warn(wm8350->dev, "battery < 3.9V\n");
break;
case WM8350_IRQ_CHG_VBATT_LT_3P1:
dev_warn(wm8350->dev, "battery < 3.1V\n");
break;
case WM8350_IRQ_CHG_VBATT_LT_2P85:
dev_warn(wm8350->dev, "battery < 2.85V\n");
break;
/* Supply change. We will overnotify but it should do
* no harm. */
case WM8350_IRQ_EXT_USB_FB:
case WM8350_IRQ_EXT_WALL_FB:
wm8350_charger_config(wm8350, policy);
case WM8350_IRQ_EXT_BAT_FB: /* Fall through */
power_supply_changed(&power->battery);
power_supply_changed(&power->usb);
power_supply_changed(&power->ac);
break;
default:
dev_err(wm8350->dev, "Unknown interrupt %d\n", irq);
}
return IRQ_HANDLED;
}
/*********************************************************************
* AC Power
*********************************************************************/
static int wm8350_ac_get_prop(struct power_supply *psy,
enum power_supply_property psp,
union
|