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