wm8350_power.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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_RO(charger_state);
  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. /* Fall through */
  198. case WM8350_IRQ_EXT_BAT_FB:
  199. power_supply_changed(power->battery);
  200. power_supply_changed(power->usb);
  201. power_supply_changed(power->ac);
  202. break;
  203. default:
  204. dev_err(wm8350->dev, "Unknown interrupt %d\n", irq);
  205. }
  206. return IRQ_HANDLED;
  207. }
  208. /*********************************************************************
  209. * AC Power
  210. *********************************************************************/
  211. static int wm8350_ac_get_prop(struct power_supply *psy,
  212. enum power_supply_property psp,
  213. union power_supply_propval *val)
  214. {
  215. struct wm8350 *wm8350 = dev_get_drvdata(psy->dev.parent);
  216. int ret = 0;
  217. switch (psp) {
  218. case POWER_SUPPLY_PROP_ONLINE:
  219. val->intval = !!(wm8350_get_supplies(wm8350) &
  220. WM8350_LINE_SUPPLY);
  221. break;
  222. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  223. val->intval = wm8350_read_line_uvolts(wm8350);
  224. break;
  225. default:
  226. ret = -EINVAL;
  227. break;
  228. }
  229. return ret;
  230. }
  231. static enum power_supply_property wm8350_ac_props[] = {
  232. POWER_SUPPLY_PROP_ONLINE,
  233. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  234. };
  235. /*********************************************************************
  236. * USB Power
  237. *********************************************************************/
  238. static int wm8350_usb_get_prop(struct power_supply *psy,
  239. enum power_supply_property psp,
  240. union power_supply_propval *val)
  241. {
  242. struct wm8350 *wm8350 = dev_get_drvdata(psy->dev.parent);
  243. int ret = 0;
  244. switch (psp) {
  245. case POWER_SUPPLY_PROP_ONLINE:
  246. val->intval = !!(wm8350_get_supplies(wm8350) &
  247. WM8350_USB_SUPPLY);
  248. break;
  249. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  250. val->intval = wm8350_read_usb_uvolts(wm8350);
  251. break;
  252. default:
  253. ret = -EINVAL;
  254. break;
  255. }
  256. return ret;
  257. }
  258. static enum power_supply_property wm8350_usb_props[] = {
  259. POWER_SUPPLY_PROP_ONLINE,
  260. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  261. };
  262. /*********************************************************************
  263. * Battery properties
  264. *********************************************************************/
  265. static int wm8350_bat_check_health(struct wm8350 *wm8350)
  266. {
  267. u16 reg;
  268. if (wm8350_read_battery_uvolts(wm8350) < 2850000)
  269. return POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  270. reg = wm8350_reg_read(wm8350, WM8350_CHARGER_OVERRIDES);
  271. if (reg & WM8350_CHG_BATT_HOT_OVRDE)
  272. return POWER_SUPPLY_HEALTH_OVERHEAT;
  273. if (reg & WM8350_CHG_BATT_COLD_OVRDE)
  274. return POWER_SUPPLY_HEALTH_COLD;
  275. return POWER_SUPPLY_HEALTH_GOOD;
  276. }
  277. static int wm8350_bat_get_charge_type(struct wm8350 *wm8350)
  278. {
  279. int state;
  280. state = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2) &
  281. WM8350_CHG_STS_MASK;
  282. switch (state) {
  283. case WM8350_CHG_STS_OFF:
  284. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  285. case WM8350_CHG_STS_TRICKLE:
  286. return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  287. case WM8350_CHG_STS_FAST:
  288. return POWER_SUPPLY_CHARGE_TYPE_FAST;
  289. default:
  290. return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
  291. }
  292. }
  293. static int wm8350_bat_get_property(struct power_supply *psy,
  294. enum power_supply_property psp,
  295. union power_supply_propval *val)
  296. {
  297. struct wm8350 *wm8350 = dev_get_drvdata(psy->dev.parent);
  298. int ret = 0;
  299. switch (psp) {
  300. case POWER_SUPPLY_PROP_STATUS:
  301. val->intval = wm8350_batt_status(wm8350);
  302. break;
  303. case POWER_SUPPLY_PROP_ONLINE:
  304. val->intval = !!(wm8350_get_supplies(wm8350) &
  305. WM8350_BATT_SUPPLY);
  306. break;
  307. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  308. val->intval = wm8350_read_battery_uvolts(wm8350);
  309. break;
  310. case POWER_SUPPLY_PROP_HEALTH:
  311. val->intval = wm8350_bat_check_health(wm8350);
  312. break;
  313. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  314. val->intval = wm8350_bat_get_charge_type(wm8350);
  315. break;
  316. default:
  317. ret = -EINVAL;
  318. break;
  319. }
  320. return ret;
  321. }
  322. static enum power_supply_property wm8350_bat_props[] = {
  323. POWER_SUPPLY_PROP_STATUS,
  324. POWER_SUPPLY_PROP_ONLINE,
  325. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  326. POWER_SUPPLY_PROP_HEALTH,
  327. POWER_SUPPLY_PROP_CHARGE_TYPE,
  328. };
  329. static const struct power_supply_desc wm8350_ac_desc = {
  330. .name = "wm8350-ac",
  331. .type = POWER_SUPPLY_TYPE_MAINS,
  332. .properties = wm8350_ac_props,
  333. .num_properties = ARRAY_SIZE(wm8350_ac_props),
  334. .get_property = wm8350_ac_get_prop,
  335. };
  336. static const struct power_supply_desc wm8350_battery_desc = {
  337. .name = "wm8350-battery",
  338. .properties = wm8350_bat_props,
  339. .num_properties = ARRAY_SIZE(wm8350_bat_props),
  340. .get_property = wm8350_bat_get_property,
  341. .use_for_apm = 1,
  342. };
  343. static const struct power_supply_desc wm8350_usb_desc = {
  344. .name = "wm8350-usb",
  345. .type = POWER_SUPPLY_TYPE_USB,
  346. .properties = wm8350_usb_props,
  347. .num_properties = ARRAY_SIZE(wm8350_usb_props),
  348. .get_property = wm8350_usb_get_prop,
  349. };
  350. /*********************************************************************
  351. * Initialisation
  352. *********************************************************************/
  353. static void wm8350_init_charger(struct wm8350 *wm8350)
  354. {
  355. /* register our interest in charger events */
  356. wm8350_register_irq(wm8350, WM8350_IRQ_CHG_BAT_HOT,
  357. wm8350_charger_handler, 0, "Battery hot", wm8350);
  358. wm8350_register_irq(wm8350, WM8350_IRQ_CHG_BAT_COLD,
  359. wm8350_charger_handler, 0, "Battery cold", wm8350);
  360. wm8350_register_irq(wm8350, WM8350_IRQ_CHG_BAT_FAIL,
  361. wm8350_charger_handler, 0, "Battery fail", wm8350);
  362. wm8350_register_irq(wm8350, WM8350_IRQ_CHG_TO,
  363. wm8350_charger_handler, 0,
  364. "Charger timeout", wm8350);
  365. wm8350_register_irq(wm8350, WM8350_IRQ_CHG_END,
  366. wm8350_charger_handler, 0,
  367. "Charge end", wm8350);
  368. wm8350_register_irq(wm8350, WM8350_IRQ_CHG_START,
  369. wm8350_charger_handler, 0,
  370. "Charge start", wm8350);
  371. wm8350_register_irq(wm8350, WM8350_IRQ_CHG_FAST_RDY,
  372. wm8350_charger_handler, 0,
  373. "Fast charge ready", wm8350);
  374. wm8350_register_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P9,
  375. wm8350_charger_handler, 0,
  376. "Battery <3.9V", wm8350);
  377. wm8350_register_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P1,
  378. wm8350_charger_handler, 0,
  379. "Battery <3.1V", wm8350);
  380. wm8350_register_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_2P85,
  381. wm8350_charger_handler, 0,
  382. "Battery <2.85V", wm8350);
  383. /* and supply change events */
  384. wm8350_register_irq(wm8350, WM8350_IRQ_EXT_USB_FB,
  385. wm8350_charger_handler, 0, "USB", wm8350);
  386. wm8350_register_irq(wm8350, WM8350_IRQ_EXT_WALL_FB,
  387. wm8350_charger_handler, 0, "Wall", wm8350);
  388. wm8350_register_irq(wm8350, WM8350_IRQ_EXT_BAT_FB,
  389. wm8350_charger_handler, 0, "Battery", wm8350);
  390. }
  391. static void free_charger_irq(struct wm8350 *wm8350)
  392. {
  393. wm8350_free_irq(wm8350, WM8350_IRQ_CHG_BAT_HOT, wm8350);
  394. wm8350_free_irq(wm8350, WM8350_IRQ_CHG_BAT_COLD, wm8350);
  395. wm8350_free_irq(wm8350, WM8350_IRQ_CHG_BAT_FAIL, wm8350);
  396. wm8350_free_irq(wm8350, WM8350_IRQ_CHG_TO, wm8350);
  397. wm8350_free_irq(wm8350, WM8350_IRQ_CHG_END, wm8350);
  398. wm8350_free_irq(wm8350, WM8350_IRQ_CHG_START, wm8350);
  399. wm8350_free_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P9, wm8350);
  400. wm8350_free_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P1, wm8350);
  401. wm8350_free_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_2P85, wm8350);
  402. wm8350_free_irq(wm8350, WM8350_IRQ_EXT_USB_FB, wm8350);
  403. wm8350_free_irq(wm8350, WM8350_IRQ_EXT_WALL_FB, wm8350);
  404. wm8350_free_irq(wm8350, WM8350_IRQ_EXT_BAT_FB, wm8350);
  405. }
  406. static int wm8350_power_probe(struct platform_device *pdev)
  407. {
  408. struct wm8350 *wm8350 = platform_get_drvdata(pdev);
  409. struct wm8350_power *power = &wm8350->power;
  410. struct wm8350_charger_policy *policy = power->policy;
  411. int ret;
  412. power->ac = power_supply_register(&pdev->dev, &wm8350_ac_desc, NULL);
  413. if (IS_ERR(power->ac))
  414. return PTR_ERR(power->ac);
  415. power->battery = power_supply_register(&pdev->dev, &wm8350_battery_desc,
  416. NULL);
  417. if (IS_ERR(power->battery)) {
  418. ret = PTR_ERR(power->battery);
  419. goto battery_failed;
  420. }
  421. power->usb = power_supply_register(&pdev->dev, &wm8350_usb_desc, NULL);
  422. if (IS_ERR(power->usb)) {
  423. ret = PTR_ERR(power->usb);
  424. goto usb_failed;
  425. }
  426. ret = device_create_file(&pdev->dev, &dev_attr_charger_state);
  427. if (ret < 0)
  428. dev_warn(wm8350->dev, "failed to add charge sysfs: %d\n", ret);
  429. ret = 0;
  430. wm8350_init_charger(wm8350);
  431. if (wm8350_charger_config(wm8350, policy) == 0) {
  432. wm8350_reg_unlock(wm8350);
  433. wm8350_set_bits(wm8350, WM8350_POWER_MGMT_5, WM8350_CHG_ENA);
  434. wm8350_reg_lock(wm8350);
  435. }
  436. return ret;
  437. usb_failed:
  438. power_supply_unregister(power->battery);
  439. battery_failed:
  440. power_supply_unregister(power->ac);
  441. return ret;
  442. }
  443. static int wm8350_power_remove(struct platform_device *pdev)
  444. {
  445. struct wm8350 *wm8350 = platform_get_drvdata(pdev);
  446. struct wm8350_power *power = &wm8350->power;
  447. free_charger_irq(wm8350);
  448. device_remove_file(&pdev->dev, &dev_attr_charger_state);
  449. power_supply_unregister(power->battery);
  450. power_supply_unregister(power->ac);
  451. power_supply_unregister(power->usb);
  452. return 0;
  453. }
  454. static struct platform_driver wm8350_power_driver = {
  455. .probe = wm8350_power_probe,
  456. .remove = wm8350_power_remove,
  457. .driver = {
  458. .name = "wm8350-power",
  459. },
  460. };
  461. module_platform_driver(wm8350_power_driver);
  462. MODULE_LICENSE("GPL");
  463. MODULE_DESCRIPTION("Power supply driver for WM8350");
  464. MODULE_ALIAS("platform:wm8350-power");