wm8350_power.c 15 KB

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