twl4030_charger.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * TWL4030/TPS65950 BCI (Battery Charger Interface) driver
  3. *
  4. * Copyright (C) 2010 Gražvydas Ignotas <notasas@gmail.com>
  5. *
  6. * based on twl4030_bci_battery.c by TI
  7. * Copyright (C) 2008 Texas Instruments, Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/i2c/twl.h>
  20. #include <linux/power_supply.h>
  21. #include <linux/notifier.h>
  22. #include <linux/usb/otg.h>
  23. #define TWL4030_BCIMSTATEC 0x02
  24. #define TWL4030_BCIICHG 0x08
  25. #define TWL4030_BCIVAC 0x0a
  26. #define TWL4030_BCIVBUS 0x0c
  27. #define TWL4030_BCIMFSTS4 0x10
  28. #define TWL4030_BCICTL1 0x23
  29. #define TWL4030_BCIAUTOWEN BIT(5)
  30. #define TWL4030_CONFIG_DONE BIT(4)
  31. #define TWL4030_BCIAUTOUSB BIT(1)
  32. #define TWL4030_BCIAUTOAC BIT(0)
  33. #define TWL4030_CGAIN BIT(5)
  34. #define TWL4030_USBFASTMCHG BIT(2)
  35. #define TWL4030_STS_VBUS BIT(7)
  36. #define TWL4030_STS_USB_ID BIT(2)
  37. /* BCI interrupts */
  38. #define TWL4030_WOVF BIT(0) /* Watchdog overflow */
  39. #define TWL4030_TMOVF BIT(1) /* Timer overflow */
  40. #define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
  41. #define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
  42. #define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
  43. #define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
  44. #define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
  45. #define TWL4030_BATSTS BIT(7) /* Battery status */
  46. #define TWL4030_VBATLVL BIT(0) /* VBAT level */
  47. #define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */
  48. #define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */
  49. #define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */
  50. #define TWL4030_MSTATEC_USB BIT(4)
  51. #define TWL4030_MSTATEC_AC BIT(5)
  52. #define TWL4030_MSTATEC_MASK 0x0f
  53. #define TWL4030_MSTATEC_QUICK1 0x02
  54. #define TWL4030_MSTATEC_QUICK7 0x07
  55. #define TWL4030_MSTATEC_COMPLETE1 0x0b
  56. #define TWL4030_MSTATEC_COMPLETE4 0x0e
  57. static bool allow_usb;
  58. module_param(allow_usb, bool, 1);
  59. MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current");
  60. struct twl4030_bci {
  61. struct device *dev;
  62. struct power_supply ac;
  63. struct power_supply usb;
  64. struct otg_transceiver *transceiver;
  65. struct notifier_block otg_nb;
  66. struct work_struct work;
  67. int irq_chg;
  68. int irq_bci;
  69. unsigned long event;
  70. };
  71. /*
  72. * clear and set bits on an given register on a given module
  73. */
  74. static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg)
  75. {
  76. u8 val = 0;
  77. int ret;
  78. ret = twl_i2c_read_u8(mod_no, &val, reg);
  79. if (ret)
  80. return ret;
  81. val &= ~clear;
  82. val |= set;
  83. return twl_i2c_write_u8(mod_no, val, reg);
  84. }
  85. static int twl4030_bci_read(u8 reg, u8 *val)
  86. {
  87. return twl_i2c_read_u8(TWL4030_MODULE_MAIN_CHARGE, val, reg);
  88. }
  89. static int twl4030_clear_set_boot_bci(u8 clear, u8 set)
  90. {
  91. return twl4030_clear_set(TWL4030_MODULE_PM_MASTER, 0,
  92. TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set,
  93. TWL4030_PM_MASTER_BOOT_BCI);
  94. }
  95. static int twl4030bci_read_adc_val(u8 reg)
  96. {
  97. int ret, temp;
  98. u8 val;
  99. /* read MSB */
  100. ret = twl4030_bci_read(reg + 1, &val);
  101. if (ret)
  102. return ret;
  103. temp = (int)(val & 0x03) << 8;
  104. /* read LSB */
  105. ret = twl4030_bci_read(reg, &val);
  106. if (ret)
  107. return ret;
  108. return temp | val;
  109. }
  110. /*
  111. * Check if VBUS power is present
  112. */
  113. static int twl4030_bci_have_vbus(struct twl4030_bci *bci)
  114. {
  115. int ret;
  116. u8 hwsts;
  117. ret = twl_i2c_read_u8(TWL4030_MODULE_PM_MASTER, &hwsts,
  118. TWL4030_PM_MASTER_STS_HW_CONDITIONS);
  119. if (ret < 0)
  120. return 0;
  121. dev_dbg(bci->dev, "check_vbus: HW_CONDITIONS %02x\n", hwsts);
  122. /* in case we also have STS_USB_ID, VBUS is driven by TWL itself */
  123. if ((hwsts & TWL4030_STS_VBUS) && !(hwsts & TWL4030_STS_USB_ID))
  124. return 1;
  125. return 0;
  126. }
  127. /*
  128. * Enable/Disable USB Charge funtionality.
  129. */
  130. static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
  131. {
  132. int ret;
  133. if (enable) {
  134. /* Check for USB charger conneted */
  135. if (!twl4030_bci_have_vbus(bci))
  136. return -ENODEV;
  137. /*
  138. * Until we can find out what current the device can provide,
  139. * require a module param to enable USB charging.
  140. */
  141. if (!allow_usb) {
  142. dev_warn(bci->dev, "USB charging is disabled.\n");
  143. return -EACCES;
  144. }
  145. /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
  146. ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB);
  147. if (ret < 0)
  148. return ret;
  149. /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
  150. ret = twl4030_clear_set(TWL4030_MODULE_MAIN_CHARGE, 0,
  151. TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4);
  152. } else {
  153. ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0);
  154. }
  155. return ret;
  156. }
  157. /*
  158. * Enable/Disable AC Charge funtionality.
  159. */
  160. static int twl4030_charger_enable_ac(bool enable)
  161. {
  162. int ret;
  163. if (enable)
  164. ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC);
  165. else
  166. ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0);
  167. return ret;
  168. }
  169. /*
  170. * TWL4030 CHG_PRES (AC charger presence) events
  171. */
  172. static irqreturn_t twl4030_charger_interrupt(int irq, void *arg)
  173. {
  174. struct twl4030_bci *bci = arg;
  175. dev_dbg(bci->dev, "CHG_PRES irq\n");
  176. power_supply_changed(&bci->ac);
  177. power_supply_changed(&bci->usb);
  178. return IRQ_HANDLED;
  179. }
  180. /*
  181. * TWL4030 BCI monitoring events
  182. */
  183. static irqreturn_t twl4030_bci_interrupt(int irq, void *arg)
  184. {
  185. struct twl4030_bci *bci = arg;
  186. u8 irqs1, irqs2;
  187. int ret;
  188. ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1,
  189. TWL4030_INTERRUPTS_BCIISR1A);
  190. if (ret < 0)
  191. return IRQ_HANDLED;
  192. ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2,
  193. TWL4030_INTERRUPTS_BCIISR2A);
  194. if (ret < 0)
  195. return IRQ_HANDLED;
  196. dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1);
  197. if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) {
  198. /* charger state change, inform the core */
  199. power_supply_changed(&bci->ac);
  200. power_supply_changed(&bci->usb);
  201. }
  202. /* various monitoring events, for now we just log them here */
  203. if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1))
  204. dev_warn(bci->dev, "battery temperature out of range\n");
  205. if (irqs1 & TWL4030_BATSTS)
  206. dev_crit(bci->dev, "battery disconnected\n");
  207. if (irqs2 & TWL4030_VBATOV)
  208. dev_crit(bci->dev, "VBAT overvoltage\n");
  209. if (irqs2 & TWL4030_VBUSOV)
  210. dev_crit(bci->dev, "VBUS overvoltage\n");
  211. if (irqs2 & TWL4030_ACCHGOV)
  212. dev_crit(bci->dev, "Ac charger overvoltage\n");
  213. return IRQ_HANDLED;
  214. }
  215. static void twl4030_bci_usb_work(struct work_struct *data)
  216. {
  217. struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work);
  218. switch (bci->event) {
  219. case USB_EVENT_VBUS:
  220. case USB_EVENT_CHARGER:
  221. twl4030_charger_enable_usb(bci, true);
  222. break;
  223. case USB_EVENT_NONE:
  224. twl4030_charger_enable_usb(bci, false);
  225. break;
  226. }
  227. }
  228. static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
  229. void *priv)
  230. {
  231. struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, otg_nb);
  232. dev_dbg(bci->dev, "OTG notify %lu\n", val);
  233. bci->event = val;
  234. schedule_work(&bci->work);
  235. return NOTIFY_OK;
  236. }
  237. /*
  238. * TI provided formulas:
  239. * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
  240. * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
  241. * Here we use integer approximation of:
  242. * CGAIN == 0: val * 1.6618 - 0.85
  243. * CGAIN == 1: (val * 1.6618 - 0.85) * 2
  244. */
  245. static int twl4030_charger_get_current(void)
  246. {
  247. int curr;
  248. int ret;
  249. u8 bcictl1;
  250. curr = twl4030bci_read_adc_val(TWL4030_BCIICHG);
  251. if (curr < 0)
  252. return curr;
  253. ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
  254. if (ret)
  255. return ret;
  256. ret = (curr * 16618 - 850 * 10000) / 10;
  257. if (bcictl1 & TWL4030_CGAIN)
  258. ret *= 2;
  259. return ret;
  260. }
  261. /*
  262. * Returns the main charge FSM state
  263. * Or < 0 on failure.
  264. */
  265. static int twl4030bci_state(struct twl4030_bci *bci)
  266. {
  267. int ret;
  268. u8 state;
  269. ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state);
  270. if (ret) {
  271. pr_err("twl4030_bci: error reading BCIMSTATEC\n");
  272. return ret;
  273. }
  274. dev_dbg(bci->dev, "state: %02x\n", state);
  275. return state;
  276. }
  277. static int twl4030_bci_state_to_status(int state)
  278. {
  279. state &= TWL4030_MSTATEC_MASK;
  280. if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7)
  281. return POWER_SUPPLY_STATUS_CHARGING;
  282. else if (TWL4030_MSTATEC_COMPLETE1 <= state &&
  283. state <= TWL4030_MSTATEC_COMPLETE4)
  284. return POWER_SUPPLY_STATUS_FULL;
  285. else
  286. return POWER_SUPPLY_STATUS_NOT_CHARGING;
  287. }
  288. static int twl4030_bci_get_property(struct power_supply *psy,
  289. enum power_supply_property psp,
  290. union power_supply_propval *val)
  291. {
  292. struct twl4030_bci *bci = dev_get_drvdata(psy->dev->parent);
  293. int is_charging;
  294. int state;
  295. int ret;
  296. state = twl4030bci_state(bci);
  297. if (state < 0)
  298. return state;
  299. if (psy->type == POWER_SUPPLY_TYPE_USB)
  300. is_charging = state & TWL4030_MSTATEC_USB;
  301. else
  302. is_charging = state & TWL4030_MSTATEC_AC;
  303. switch (psp) {
  304. case POWER_SUPPLY_PROP_STATUS:
  305. if (is_charging)
  306. val->intval = twl4030_bci_state_to_status(state);
  307. else
  308. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  309. break;
  310. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  311. /* charging must be active for meaningful result */
  312. if (!is_charging)
  313. return -ENODATA;
  314. if (psy->type == POWER_SUPPLY_TYPE_USB) {
  315. ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
  316. if (ret < 0)
  317. return ret;
  318. /* BCIVBUS uses ADCIN8, 7/1023 V/step */
  319. val->intval = ret * 6843;
  320. } else {
  321. ret = twl4030bci_read_adc_val(TWL4030_BCIVAC);
  322. if (ret < 0)
  323. return ret;
  324. /* BCIVAC uses ADCIN11, 10/1023 V/step */
  325. val->intval = ret * 9775;
  326. }
  327. break;
  328. case POWER_SUPPLY_PROP_CURRENT_NOW:
  329. if (!is_charging)
  330. return -ENODATA;
  331. /* current measurement is shared between AC and USB */
  332. ret = twl4030_charger_get_current();
  333. if (ret < 0)
  334. return ret;
  335. val->intval = ret;
  336. break;
  337. case POWER_SUPPLY_PROP_ONLINE:
  338. val->intval = is_charging &&
  339. twl4030_bci_state_to_status(state) !=
  340. POWER_SUPPLY_STATUS_NOT_CHARGING;
  341. break;
  342. default:
  343. return -EINVAL;
  344. }
  345. return 0;
  346. }
  347. static enum power_supply_property twl4030_charger_props[] = {
  348. POWER_SUPPLY_PROP_STATUS,
  349. POWER_SUPPLY_PROP_ONLINE,
  350. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  351. POWER_SUPPLY_PROP_CURRENT_NOW,
  352. };
  353. static int __init twl4030_bci_probe(struct platform_device *pdev)
  354. {
  355. struct twl4030_bci *bci;
  356. int ret;
  357. int reg;
  358. bci = kzalloc(sizeof(*bci), GFP_KERNEL);
  359. if (bci == NULL)
  360. return -ENOMEM;
  361. bci->dev = &pdev->dev;
  362. bci->irq_chg = platform_get_irq(pdev, 0);
  363. bci->irq_bci = platform_get_irq(pdev, 1);
  364. platform_set_drvdata(pdev, bci);
  365. bci->ac.name = "twl4030_ac";
  366. bci->ac.type = POWER_SUPPLY_TYPE_MAINS;
  367. bci->ac.properties = twl4030_charger_props;
  368. bci->ac.num_properties = ARRAY_SIZE(twl4030_charger_props);
  369. bci->ac.get_property = twl4030_bci_get_property;
  370. ret = power_supply_register(&pdev->dev, &bci->ac);
  371. if (ret) {
  372. dev_err(&pdev->dev, "failed to register ac: %d\n", ret);
  373. goto fail_register_ac;
  374. }
  375. bci->usb.name = "twl4030_usb";
  376. bci->usb.type = POWER_SUPPLY_TYPE_USB;
  377. bci->usb.properties = twl4030_charger_props;
  378. bci->usb.num_properties = ARRAY_SIZE(twl4030_charger_props);
  379. bci->usb.get_property = twl4030_bci_get_property;
  380. ret = power_supply_register(&pdev->dev, &bci->usb);
  381. if (ret) {
  382. dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
  383. goto fail_register_usb;
  384. }
  385. ret = request_threaded_irq(bci->irq_chg, NULL,
  386. twl4030_charger_interrupt, 0, pdev->name, bci);
  387. if (ret < 0) {
  388. dev_err(&pdev->dev, "could not request irq %d, status %d\n",
  389. bci->irq_chg, ret);
  390. goto fail_chg_irq;
  391. }
  392. ret = request_threaded_irq(bci->irq_bci, NULL,
  393. twl4030_bci_interrupt, 0, pdev->name, bci);
  394. if (ret < 0) {
  395. dev_err(&pdev->dev, "could not request irq %d, status %d\n",
  396. bci->irq_bci, ret);
  397. goto fail_bci_irq;
  398. }
  399. INIT_WORK(&bci->work, twl4030_bci_usb_work);
  400. bci->transceiver = otg_get_transceiver();
  401. if (bci->transceiver != NULL) {
  402. bci->otg_nb.notifier_call = twl4030_bci_usb_ncb;
  403. otg_register_notifier(bci->transceiver, &bci->otg_nb);
  404. }
  405. /* Enable interrupts now. */
  406. reg = ~(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 |
  407. TWL4030_TBATOR1 | TWL4030_BATSTS);
  408. ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
  409. TWL4030_INTERRUPTS_BCIIMR1A);
  410. if (ret < 0) {
  411. dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
  412. goto fail_unmask_interrupts;
  413. }
  414. reg = ~(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV);
  415. ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
  416. TWL4030_INTERRUPTS_BCIIMR2A);
  417. if (ret < 0)
  418. dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
  419. twl4030_charger_enable_ac(true);
  420. twl4030_charger_enable_usb(bci, true);
  421. return 0;
  422. fail_unmask_interrupts:
  423. if (bci->transceiver != NULL) {
  424. otg_unregister_notifier(bci->transceiver, &bci->otg_nb);
  425. otg_put_transceiver(bci->transceiver);
  426. }
  427. free_irq(bci->irq_bci, bci);
  428. fail_bci_irq:
  429. free_irq(bci->irq_chg, bci);
  430. fail_chg_irq:
  431. power_supply_unregister(&bci->usb);
  432. fail_register_usb:
  433. power_supply_unregister(&bci->ac);
  434. fail_register_ac:
  435. platform_set_drvdata(pdev, NULL);
  436. kfree(bci);
  437. return ret;
  438. }
  439. static int __exit twl4030_bci_remove(struct platform_device *pdev)
  440. {
  441. struct twl4030_bci *bci = platform_get_drvdata(pdev);
  442. twl4030_charger_enable_ac(false);
  443. twl4030_charger_enable_usb(bci, false);
  444. /* mask interrupts */
  445. twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
  446. TWL4030_INTERRUPTS_BCIIMR1A);
  447. twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
  448. TWL4030_INTERRUPTS_BCIIMR2A);
  449. if (bci->transceiver != NULL) {
  450. otg_unregister_notifier(bci->transceiver, &bci->otg_nb);
  451. otg_put_transceiver(bci->transceiver);
  452. }
  453. free_irq(bci->irq_bci, bci);
  454. free_irq(bci->irq_chg, bci);
  455. power_supply_unregister(&bci->usb);
  456. power_supply_unregister(&bci->ac);
  457. platform_set_drvdata(pdev, NULL);
  458. kfree(bci);
  459. return 0;
  460. }
  461. static struct platform_driver twl4030_bci_driver = {
  462. .driver = {
  463. .name = "twl4030_bci",
  464. .owner = THIS_MODULE,
  465. },
  466. .remove = __exit_p(twl4030_bci_remove),
  467. };
  468. static int __init twl4030_bci_init(void)
  469. {
  470. return platform_driver_probe(&twl4030_bci_driver, twl4030_bci_probe);
  471. }
  472. module_init(twl4030_bci_init);
  473. static void __exit twl4030_bci_exit(void)
  474. {
  475. platform_driver_unregister(&twl4030_bci_driver);
  476. }
  477. module_exit(twl4030_bci_exit);
  478. MODULE_AUTHOR("Gražydas Ignotas");
  479. MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
  480. MODULE_LICENSE("GPL");
  481. MODULE_ALIAS("platform:twl4030_bci");