twl4030_charger.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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/err.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/i2c/twl.h>
  21. #include <linux/power_supply.h>
  22. #include <linux/notifier.h>
  23. #include <linux/usb/otg.h>
  24. #include <linux/iio/consumer.h>
  25. #define TWL4030_BCIMDEN 0x00
  26. #define TWL4030_BCIMDKEY 0x01
  27. #define TWL4030_BCIMSTATEC 0x02
  28. #define TWL4030_BCIICHG 0x08
  29. #define TWL4030_BCIVAC 0x0a
  30. #define TWL4030_BCIVBUS 0x0c
  31. #define TWL4030_BCIMFSTS3 0x0F
  32. #define TWL4030_BCIMFSTS4 0x10
  33. #define TWL4030_BCICTL1 0x23
  34. #define TWL4030_BB_CFG 0x12
  35. #define TWL4030_BCIIREF1 0x27
  36. #define TWL4030_BCIIREF2 0x28
  37. #define TWL4030_BCIMFKEY 0x11
  38. #define TWL4030_BCIMFEN3 0x14
  39. #define TWL4030_BCIMFTH8 0x1d
  40. #define TWL4030_BCIMFTH9 0x1e
  41. #define TWL4030_BCIWDKEY 0x21
  42. #define TWL4030_BCIMFSTS1 0x01
  43. #define TWL4030_BCIAUTOWEN BIT(5)
  44. #define TWL4030_CONFIG_DONE BIT(4)
  45. #define TWL4030_CVENAC BIT(2)
  46. #define TWL4030_BCIAUTOUSB BIT(1)
  47. #define TWL4030_BCIAUTOAC BIT(0)
  48. #define TWL4030_CGAIN BIT(5)
  49. #define TWL4030_USBFASTMCHG BIT(2)
  50. #define TWL4030_STS_VBUS BIT(7)
  51. #define TWL4030_STS_USB_ID BIT(2)
  52. #define TWL4030_BBCHEN BIT(4)
  53. #define TWL4030_BBSEL_MASK 0x0c
  54. #define TWL4030_BBSEL_2V5 0x00
  55. #define TWL4030_BBSEL_3V0 0x04
  56. #define TWL4030_BBSEL_3V1 0x08
  57. #define TWL4030_BBSEL_3V2 0x0c
  58. #define TWL4030_BBISEL_MASK 0x03
  59. #define TWL4030_BBISEL_25uA 0x00
  60. #define TWL4030_BBISEL_150uA 0x01
  61. #define TWL4030_BBISEL_500uA 0x02
  62. #define TWL4030_BBISEL_1000uA 0x03
  63. #define TWL4030_BATSTSPCHG BIT(2)
  64. #define TWL4030_BATSTSMCHG BIT(6)
  65. /* BCI interrupts */
  66. #define TWL4030_WOVF BIT(0) /* Watchdog overflow */
  67. #define TWL4030_TMOVF BIT(1) /* Timer overflow */
  68. #define TWL4030_ICHGHIGH BIT(2) /* Battery charge current high */
  69. #define TWL4030_ICHGLOW BIT(3) /* Battery cc. low / FSM state change */
  70. #define TWL4030_ICHGEOC BIT(4) /* Battery current end-of-charge */
  71. #define TWL4030_TBATOR2 BIT(5) /* Battery temperature out of range 2 */
  72. #define TWL4030_TBATOR1 BIT(6) /* Battery temperature out of range 1 */
  73. #define TWL4030_BATSTS BIT(7) /* Battery status */
  74. #define TWL4030_VBATLVL BIT(0) /* VBAT level */
  75. #define TWL4030_VBATOV BIT(1) /* VBAT overvoltage */
  76. #define TWL4030_VBUSOV BIT(2) /* VBUS overvoltage */
  77. #define TWL4030_ACCHGOV BIT(3) /* Ac charger overvoltage */
  78. #define TWL4030_MSTATEC_USB BIT(4)
  79. #define TWL4030_MSTATEC_AC BIT(5)
  80. #define TWL4030_MSTATEC_MASK 0x0f
  81. #define TWL4030_MSTATEC_QUICK1 0x02
  82. #define TWL4030_MSTATEC_QUICK7 0x07
  83. #define TWL4030_MSTATEC_COMPLETE1 0x0b
  84. #define TWL4030_MSTATEC_COMPLETE4 0x0e
  85. /*
  86. * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
  87. * then AC is available.
  88. */
  89. static inline int ac_available(struct iio_channel *channel_vac)
  90. {
  91. int val, err;
  92. if (!channel_vac)
  93. return 0;
  94. err = iio_read_channel_processed(channel_vac, &val);
  95. if (err < 0)
  96. return 0;
  97. return val > 4500;
  98. }
  99. static bool allow_usb;
  100. module_param(allow_usb, bool, 0644);
  101. MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current");
  102. struct twl4030_bci {
  103. struct device *dev;
  104. struct power_supply *ac;
  105. struct power_supply *usb;
  106. struct usb_phy *transceiver;
  107. struct notifier_block usb_nb;
  108. struct work_struct work;
  109. int irq_chg;
  110. int irq_bci;
  111. int usb_enabled;
  112. /*
  113. * ichg_* and *_cur values in uA. If any are 'large', we set
  114. * CGAIN to '1' which doubles the range for half the
  115. * precision.
  116. */
  117. unsigned int ichg_eoc, ichg_lo, ichg_hi;
  118. unsigned int usb_cur, ac_cur;
  119. struct iio_channel *channel_vac;
  120. bool ac_is_active;
  121. int usb_mode, ac_mode; /* charging mode requested */
  122. #define CHARGE_OFF 0
  123. #define CHARGE_AUTO 1
  124. #define CHARGE_LINEAR 2
  125. /* When setting the USB current we slowly increase the
  126. * requested current until target is reached or the voltage
  127. * drops below 4.75V. In the latter case we step back one
  128. * step.
  129. */
  130. unsigned int usb_cur_target;
  131. struct delayed_work current_worker;
  132. #define USB_CUR_STEP 20000 /* 20mA at a time */
  133. #define USB_MIN_VOLT 4750000 /* 4.75V */
  134. #define USB_CUR_DELAY msecs_to_jiffies(100)
  135. #define USB_MAX_CURRENT 1700000 /* TWL4030 caps at 1.7A */
  136. unsigned long event;
  137. };
  138. /* strings for 'usb_mode' values */
  139. static char *modes[] = { "off", "auto", "continuous" };
  140. /*
  141. * clear and set bits on an given register on a given module
  142. */
  143. static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg)
  144. {
  145. u8 val = 0;
  146. int ret;
  147. ret = twl_i2c_read_u8(mod_no, &val, reg);
  148. if (ret)
  149. return ret;
  150. val &= ~clear;
  151. val |= set;
  152. return twl_i2c_write_u8(mod_no, val, reg);
  153. }
  154. static int twl4030_bci_read(u8 reg, u8 *val)
  155. {
  156. return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, val, reg);
  157. }
  158. static int twl4030_clear_set_boot_bci(u8 clear, u8 set)
  159. {
  160. return twl4030_clear_set(TWL_MODULE_PM_MASTER, clear,
  161. TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set,
  162. TWL4030_PM_MASTER_BOOT_BCI);
  163. }
  164. static int twl4030bci_read_adc_val(u8 reg)
  165. {
  166. int ret, temp;
  167. u8 val;
  168. /* read MSB */
  169. ret = twl4030_bci_read(reg + 1, &val);
  170. if (ret)
  171. return ret;
  172. temp = (int)(val & 0x03) << 8;
  173. /* read LSB */
  174. ret = twl4030_bci_read(reg, &val);
  175. if (ret)
  176. return ret;
  177. return temp | val;
  178. }
  179. /*
  180. * Check if Battery Pack was present
  181. */
  182. static int twl4030_is_battery_present(struct twl4030_bci *bci)
  183. {
  184. int ret;
  185. u8 val = 0;
  186. /* Battery presence in Main charge? */
  187. ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val, TWL4030_BCIMFSTS3);
  188. if (ret)
  189. return ret;
  190. if (val & TWL4030_BATSTSMCHG)
  191. return 0;
  192. /*
  193. * OK, It could be that bootloader did not enable main charger,
  194. * pre-charge is h/w auto. So, Battery presence in Pre-charge?
  195. */
  196. ret = twl_i2c_read_u8(TWL4030_MODULE_PRECHARGE, &val,
  197. TWL4030_BCIMFSTS1);
  198. if (ret)
  199. return ret;
  200. if (val & TWL4030_BATSTSPCHG)
  201. return 0;
  202. return -ENODEV;
  203. }
  204. /*
  205. * TI provided formulas:
  206. * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
  207. * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
  208. * Here we use integer approximation of:
  209. * CGAIN == 0: val * 1.6618 - 0.85 * 1000
  210. * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2
  211. */
  212. /*
  213. * convert twl register value for currents into uA
  214. */
  215. static int regval2ua(int regval, bool cgain)
  216. {
  217. if (cgain)
  218. return (regval * 16618 - 8500 * 1000) / 5;
  219. else
  220. return (regval * 16618 - 8500 * 1000) / 10;
  221. }
  222. /*
  223. * convert uA currents into twl register value
  224. */
  225. static int ua2regval(int ua, bool cgain)
  226. {
  227. int ret;
  228. if (cgain)
  229. ua /= 2;
  230. ret = (ua * 10 + 8500 * 1000) / 16618;
  231. /* rounding problems */
  232. if (ret < 512)
  233. ret = 512;
  234. return ret;
  235. }
  236. static int twl4030_charger_update_current(struct twl4030_bci *bci)
  237. {
  238. int status;
  239. int cur;
  240. unsigned reg, cur_reg;
  241. u8 bcictl1, oldreg, fullreg;
  242. bool cgain = false;
  243. u8 boot_bci;
  244. /*
  245. * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
  246. * and AC is enabled, set current for 'ac'
  247. */
  248. if (ac_available(bci->channel_vac)) {
  249. cur = bci->ac_cur;
  250. bci->ac_is_active = true;
  251. } else {
  252. cur = bci->usb_cur;
  253. bci->ac_is_active = false;
  254. if (cur > bci->usb_cur_target) {
  255. cur = bci->usb_cur_target;
  256. bci->usb_cur = cur;
  257. }
  258. if (cur < bci->usb_cur_target)
  259. schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
  260. }
  261. /* First, check thresholds and see if cgain is needed */
  262. if (bci->ichg_eoc >= 200000)
  263. cgain = true;
  264. if (bci->ichg_lo >= 400000)
  265. cgain = true;
  266. if (bci->ichg_hi >= 820000)
  267. cgain = true;
  268. if (cur > 852000)
  269. cgain = true;
  270. status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
  271. if (status < 0)
  272. return status;
  273. if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &boot_bci,
  274. TWL4030_PM_MASTER_BOOT_BCI) < 0)
  275. boot_bci = 0;
  276. boot_bci &= 7;
  277. if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN))
  278. /* Need to turn for charging while we change the
  279. * CGAIN bit. Leave it off while everything is
  280. * updated.
  281. */
  282. twl4030_clear_set_boot_bci(boot_bci, 0);
  283. /*
  284. * For ichg_eoc, the hardware only supports reg values matching
  285. * 100XXXX000, and requires the XXXX be stored in the high nibble
  286. * of TWL4030_BCIMFTH8.
  287. */
  288. reg = ua2regval(bci->ichg_eoc, cgain);
  289. if (reg > 0x278)
  290. reg = 0x278;
  291. if (reg < 0x200)
  292. reg = 0x200;
  293. reg = (reg >> 3) & 0xf;
  294. fullreg = reg << 4;
  295. /*
  296. * For ichg_lo, reg value must match 10XXXX0000.
  297. * XXXX is stored in low nibble of TWL4030_BCIMFTH8.
  298. */
  299. reg = ua2regval(bci->ichg_lo, cgain);
  300. if (reg > 0x2F0)
  301. reg = 0x2F0;
  302. if (reg < 0x200)
  303. reg = 0x200;
  304. reg = (reg >> 4) & 0xf;
  305. fullreg |= reg;
  306. /* ichg_eoc and ichg_lo live in same register */
  307. status = twl4030_bci_read(TWL4030_BCIMFTH8, &oldreg);
  308. if (status < 0)
  309. return status;
  310. if (oldreg != fullreg) {
  311. status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xF4,
  312. TWL4030_BCIMFKEY);
  313. if (status < 0)
  314. return status;
  315. twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
  316. fullreg, TWL4030_BCIMFTH8);
  317. }
  318. /* ichg_hi threshold must be 1XXXX01100 (I think) */
  319. reg = ua2regval(bci->ichg_hi, cgain);
  320. if (reg > 0x3E0)
  321. reg = 0x3E0;
  322. if (reg < 0x200)
  323. reg = 0x200;
  324. fullreg = (reg >> 5) & 0xF;
  325. fullreg <<= 4;
  326. status = twl4030_bci_read(TWL4030_BCIMFTH9, &oldreg);
  327. if (status < 0)
  328. return status;
  329. if ((oldreg & 0xF0) != fullreg) {
  330. fullreg |= (oldreg & 0x0F);
  331. status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
  332. TWL4030_BCIMFKEY);
  333. if (status < 0)
  334. return status;
  335. twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
  336. fullreg, TWL4030_BCIMFTH9);
  337. }
  338. /*
  339. * And finally, set the current. This is stored in
  340. * two registers.
  341. */
  342. reg = ua2regval(cur, cgain);
  343. /* we have only 10 bits */
  344. if (reg > 0x3ff)
  345. reg = 0x3ff;
  346. status = twl4030_bci_read(TWL4030_BCIIREF1, &oldreg);
  347. if (status < 0)
  348. return status;
  349. cur_reg = oldreg;
  350. status = twl4030_bci_read(TWL4030_BCIIREF2, &oldreg);
  351. if (status < 0)
  352. return status;
  353. cur_reg |= oldreg << 8;
  354. if (reg != oldreg) {
  355. /* disable write protection for one write access for
  356. * BCIIREF */
  357. status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
  358. TWL4030_BCIMFKEY);
  359. if (status < 0)
  360. return status;
  361. status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
  362. (reg & 0x100) ? 3 : 2,
  363. TWL4030_BCIIREF2);
  364. if (status < 0)
  365. return status;
  366. /* disable write protection for one write access for
  367. * BCIIREF */
  368. status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
  369. TWL4030_BCIMFKEY);
  370. if (status < 0)
  371. return status;
  372. status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
  373. reg & 0xff,
  374. TWL4030_BCIIREF1);
  375. }
  376. if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN)) {
  377. /* Flip CGAIN and re-enable charging */
  378. bcictl1 ^= TWL4030_CGAIN;
  379. twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
  380. bcictl1, TWL4030_BCICTL1);
  381. twl4030_clear_set_boot_bci(0, boot_bci);
  382. }
  383. return 0;
  384. }
  385. static int twl4030_charger_get_current(void);
  386. static void twl4030_current_worker(struct work_struct *data)
  387. {
  388. int v, curr;
  389. int res;
  390. struct twl4030_bci *bci = container_of(data, struct twl4030_bci,
  391. current_worker.work);
  392. res = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
  393. if (res < 0)
  394. v = 0;
  395. else
  396. /* BCIVBUS uses ADCIN8, 7/1023 V/step */
  397. v = res * 6843;
  398. curr = twl4030_charger_get_current();
  399. dev_dbg(bci->dev, "v=%d cur=%d limit=%d target=%d\n", v, curr,
  400. bci->usb_cur, bci->usb_cur_target);
  401. if (v < USB_MIN_VOLT) {
  402. /* Back up and stop adjusting. */
  403. bci->usb_cur -= USB_CUR_STEP;
  404. bci->usb_cur_target = bci->usb_cur;
  405. } else if (bci->usb_cur >= bci->usb_cur_target ||
  406. bci->usb_cur + USB_CUR_STEP > USB_MAX_CURRENT) {
  407. /* Reached target and voltage is OK - stop */
  408. return;
  409. } else {
  410. bci->usb_cur += USB_CUR_STEP;
  411. schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
  412. }
  413. twl4030_charger_update_current(bci);
  414. }
  415. /*
  416. * Enable/Disable USB Charge functionality.
  417. */
  418. static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
  419. {
  420. int ret;
  421. if (bci->usb_mode == CHARGE_OFF)
  422. enable = false;
  423. if (enable && !IS_ERR_OR_NULL(bci->transceiver)) {
  424. twl4030_charger_update_current(bci);
  425. /* Need to keep phy powered */
  426. if (!bci->usb_enabled) {
  427. pm_runtime_get_sync(bci->transceiver->dev);
  428. bci->usb_enabled = 1;
  429. }
  430. if (bci->usb_mode == CHARGE_AUTO)
  431. /* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
  432. ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB);
  433. /* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
  434. ret = twl4030_clear_set(TWL_MODULE_MAIN_CHARGE, 0,
  435. TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4);
  436. if (bci->usb_mode == CHARGE_LINEAR) {
  437. twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC|TWL4030_CVENAC, 0);
  438. /* Watch dog key: WOVF acknowledge */
  439. ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x33,
  440. TWL4030_BCIWDKEY);
  441. /* 0x24 + EKEY6: off mode */
  442. ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
  443. TWL4030_BCIMDKEY);
  444. /* EKEY2: Linear charge: USB path */
  445. ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x26,
  446. TWL4030_BCIMDKEY);
  447. /* WDKEY5: stop watchdog count */
  448. ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf3,
  449. TWL4030_BCIWDKEY);
  450. /* enable MFEN3 access */
  451. ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x9c,
  452. TWL4030_BCIMFKEY);
  453. /* ICHGEOCEN - end-of-charge monitor (current < 80mA)
  454. * (charging continues)
  455. * ICHGLOWEN - current level monitor (charge continues)
  456. * don't monitor over-current or heat save
  457. */
  458. ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf0,
  459. TWL4030_BCIMFEN3);
  460. }
  461. } else {
  462. ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0);
  463. ret |= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
  464. TWL4030_BCIMDKEY);
  465. if (bci->usb_enabled) {
  466. pm_runtime_mark_last_busy(bci->transceiver->dev);
  467. pm_runtime_put_autosuspend(bci->transceiver->dev);
  468. bci->usb_enabled = 0;
  469. }
  470. bci->usb_cur = 0;
  471. }
  472. return ret;
  473. }
  474. /*
  475. * Enable/Disable AC Charge funtionality.
  476. */
  477. static int twl4030_charger_enable_ac(struct twl4030_bci *bci, bool enable)
  478. {
  479. int ret;
  480. if (bci->ac_mode == CHARGE_OFF)
  481. enable = false;
  482. if (enable)
  483. ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC);
  484. else
  485. ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0);
  486. return ret;
  487. }
  488. /*
  489. * Enable/Disable charging of Backup Battery.
  490. */
  491. static int twl4030_charger_enable_backup(int uvolt, int uamp)
  492. {
  493. int ret;
  494. u8 flags;
  495. if (uvolt < 2500000 ||
  496. uamp < 25) {
  497. /* disable charging of backup battery */
  498. ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
  499. TWL4030_BBCHEN, 0, TWL4030_BB_CFG);
  500. return ret;
  501. }
  502. flags = TWL4030_BBCHEN;
  503. if (uvolt >= 3200000)
  504. flags |= TWL4030_BBSEL_3V2;
  505. else if (uvolt >= 3100000)
  506. flags |= TWL4030_BBSEL_3V1;
  507. else if (uvolt >= 3000000)
  508. flags |= TWL4030_BBSEL_3V0;
  509. else
  510. flags |= TWL4030_BBSEL_2V5;
  511. if (uamp >= 1000)
  512. flags |= TWL4030_BBISEL_1000uA;
  513. else if (uamp >= 500)
  514. flags |= TWL4030_BBISEL_500uA;
  515. else if (uamp >= 150)
  516. flags |= TWL4030_BBISEL_150uA;
  517. else
  518. flags |= TWL4030_BBISEL_25uA;
  519. ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
  520. TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK,
  521. flags,
  522. TWL4030_BB_CFG);
  523. return ret;
  524. }
  525. /*
  526. * TWL4030 CHG_PRES (AC charger presence) events
  527. */
  528. static irqreturn_t twl4030_charger_interrupt(int irq, void *arg)
  529. {
  530. struct twl4030_bci *bci = arg;
  531. dev_dbg(bci->dev, "CHG_PRES irq\n");
  532. /* reset current on each 'plug' event */
  533. bci->ac_cur = 500000;
  534. twl4030_charger_update_current(bci);
  535. power_supply_changed(bci->ac);
  536. power_supply_changed(bci->usb);
  537. return IRQ_HANDLED;
  538. }
  539. /*
  540. * TWL4030 BCI monitoring events
  541. */
  542. static irqreturn_t twl4030_bci_interrupt(int irq, void *arg)
  543. {
  544. struct twl4030_bci *bci = arg;
  545. u8 irqs1, irqs2;
  546. int ret;
  547. ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1,
  548. TWL4030_INTERRUPTS_BCIISR1A);
  549. if (ret < 0)
  550. return IRQ_HANDLED;
  551. ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2,
  552. TWL4030_INTERRUPTS_BCIISR2A);
  553. if (ret < 0)
  554. return IRQ_HANDLED;
  555. dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1);
  556. if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) {
  557. /* charger state change, inform the core */
  558. power_supply_changed(bci->ac);
  559. power_supply_changed(bci->usb);
  560. }
  561. twl4030_charger_update_current(bci);
  562. /* various monitoring events, for now we just log them here */
  563. if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1))
  564. dev_warn(bci->dev, "battery temperature out of range\n");
  565. if (irqs1 & TWL4030_BATSTS)
  566. dev_crit(bci->dev, "battery disconnected\n");
  567. if (irqs2 & TWL4030_VBATOV)
  568. dev_crit(bci->dev, "VBAT overvoltage\n");
  569. if (irqs2 & TWL4030_VBUSOV)
  570. dev_crit(bci->dev, "VBUS overvoltage\n");
  571. if (irqs2 & TWL4030_ACCHGOV)
  572. dev_crit(bci->dev, "Ac charger overvoltage\n");
  573. return IRQ_HANDLED;
  574. }
  575. /*
  576. * Provide "max_current" attribute in sysfs.
  577. */
  578. static ssize_t
  579. twl4030_bci_max_current_store(struct device *dev, struct device_attribute *attr,
  580. const char *buf, size_t n)
  581. {
  582. struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
  583. int cur = 0;
  584. int status = 0;
  585. status = kstrtoint(buf, 10, &cur);
  586. if (status)
  587. return status;
  588. if (cur < 0)
  589. return -EINVAL;
  590. if (dev == &bci->ac->dev)
  591. bci->ac_cur = cur;
  592. else
  593. bci->usb_cur_target = cur;
  594. twl4030_charger_update_current(bci);
  595. return n;
  596. }
  597. /*
  598. * sysfs max_current show
  599. */
  600. static ssize_t twl4030_bci_max_current_show(struct device *dev,
  601. struct device_attribute *attr, char *buf)
  602. {
  603. int status = 0;
  604. int cur = -1;
  605. u8 bcictl1;
  606. struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
  607. if (dev == &bci->ac->dev) {
  608. if (!bci->ac_is_active)
  609. cur = bci->ac_cur;
  610. } else {
  611. if (bci->ac_is_active)
  612. cur = bci->usb_cur_target;
  613. }
  614. if (cur < 0) {
  615. cur = twl4030bci_read_adc_val(TWL4030_BCIIREF1);
  616. if (cur < 0)
  617. return cur;
  618. status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
  619. if (status < 0)
  620. return status;
  621. cur = regval2ua(cur, bcictl1 & TWL4030_CGAIN);
  622. }
  623. return scnprintf(buf, PAGE_SIZE, "%u\n", cur);
  624. }
  625. static DEVICE_ATTR(max_current, 0644, twl4030_bci_max_current_show,
  626. twl4030_bci_max_current_store);
  627. static void twl4030_bci_usb_work(struct work_struct *data)
  628. {
  629. struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work);
  630. switch (bci->event) {
  631. case USB_EVENT_VBUS:
  632. case USB_EVENT_CHARGER:
  633. twl4030_charger_enable_usb(bci, true);
  634. break;
  635. case USB_EVENT_NONE:
  636. twl4030_charger_enable_usb(bci, false);
  637. break;
  638. }
  639. }
  640. static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
  641. void *priv)
  642. {
  643. struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, usb_nb);
  644. dev_dbg(bci->dev, "OTG notify %lu\n", val);
  645. /* reset current on each 'plug' event */
  646. if (allow_usb)
  647. bci->usb_cur_target = 500000;
  648. else
  649. bci->usb_cur_target = 100000;
  650. bci->event = val;
  651. schedule_work(&bci->work);
  652. return NOTIFY_OK;
  653. }
  654. /*
  655. * sysfs charger enabled store
  656. */
  657. static ssize_t
  658. twl4030_bci_mode_store(struct device *dev, struct device_attribute *attr,
  659. const char *buf, size_t n)
  660. {
  661. struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
  662. int mode;
  663. int status;
  664. if (sysfs_streq(buf, modes[0]))
  665. mode = 0;
  666. else if (sysfs_streq(buf, modes[1]))
  667. mode = 1;
  668. else if (sysfs_streq(buf, modes[2]))
  669. mode = 2;
  670. else
  671. return -EINVAL;
  672. if (dev == &bci->ac->dev) {
  673. if (mode == 2)
  674. return -EINVAL;
  675. twl4030_charger_enable_ac(bci, false);
  676. bci->ac_mode = mode;
  677. status = twl4030_charger_enable_ac(bci, true);
  678. } else {
  679. twl4030_charger_enable_usb(bci, false);
  680. bci->usb_mode = mode;
  681. status = twl4030_charger_enable_usb(bci, true);
  682. }
  683. return (status == 0) ? n : status;
  684. }
  685. /*
  686. * sysfs charger enabled show
  687. */
  688. static ssize_t
  689. twl4030_bci_mode_show(struct device *dev,
  690. struct device_attribute *attr, char *buf)
  691. {
  692. struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
  693. int len = 0;
  694. int i;
  695. int mode = bci->usb_mode;
  696. if (dev == &bci->ac->dev)
  697. mode = bci->ac_mode;
  698. for (i = 0; i < ARRAY_SIZE(modes); i++)
  699. if (mode == i)
  700. len += snprintf(buf+len, PAGE_SIZE-len,
  701. "[%s] ", modes[i]);
  702. else
  703. len += snprintf(buf+len, PAGE_SIZE-len,
  704. "%s ", modes[i]);
  705. buf[len-1] = '\n';
  706. return len;
  707. }
  708. static DEVICE_ATTR(mode, 0644, twl4030_bci_mode_show,
  709. twl4030_bci_mode_store);
  710. static int twl4030_charger_get_current(void)
  711. {
  712. int curr;
  713. int ret;
  714. u8 bcictl1;
  715. curr = twl4030bci_read_adc_val(TWL4030_BCIICHG);
  716. if (curr < 0)
  717. return curr;
  718. ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
  719. if (ret)
  720. return ret;
  721. return regval2ua(curr, bcictl1 & TWL4030_CGAIN);
  722. }
  723. /*
  724. * Returns the main charge FSM state
  725. * Or < 0 on failure.
  726. */
  727. static int twl4030bci_state(struct twl4030_bci *bci)
  728. {
  729. int ret;
  730. u8 state;
  731. ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state);
  732. if (ret) {
  733. pr_err("twl4030_bci: error reading BCIMSTATEC\n");
  734. return ret;
  735. }
  736. dev_dbg(bci->dev, "state: %02x\n", state);
  737. return state;
  738. }
  739. static int twl4030_bci_state_to_status(int state)
  740. {
  741. state &= TWL4030_MSTATEC_MASK;
  742. if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7)
  743. return POWER_SUPPLY_STATUS_CHARGING;
  744. else if (TWL4030_MSTATEC_COMPLETE1 <= state &&
  745. state <= TWL4030_MSTATEC_COMPLETE4)
  746. return POWER_SUPPLY_STATUS_FULL;
  747. else
  748. return POWER_SUPPLY_STATUS_NOT_CHARGING;
  749. }
  750. static int twl4030_bci_get_property(struct power_supply *psy,
  751. enum power_supply_property psp,
  752. union power_supply_propval *val)
  753. {
  754. struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
  755. int is_charging;
  756. int state;
  757. int ret;
  758. state = twl4030bci_state(bci);
  759. if (state < 0)
  760. return state;
  761. if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
  762. is_charging = state & TWL4030_MSTATEC_USB;
  763. else
  764. is_charging = state & TWL4030_MSTATEC_AC;
  765. if (!is_charging) {
  766. u8 s;
  767. twl4030_bci_read(TWL4030_BCIMDEN, &s);
  768. if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
  769. is_charging = s & 1;
  770. else
  771. is_charging = s & 2;
  772. if (is_charging)
  773. /* A little white lie */
  774. state = TWL4030_MSTATEC_QUICK1;
  775. }
  776. switch (psp) {
  777. case POWER_SUPPLY_PROP_STATUS:
  778. if (is_charging)
  779. val->intval = twl4030_bci_state_to_status(state);
  780. else
  781. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  782. break;
  783. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  784. /* charging must be active for meaningful result */
  785. if (!is_charging)
  786. return -ENODATA;
  787. if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
  788. ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
  789. if (ret < 0)
  790. return ret;
  791. /* BCIVBUS uses ADCIN8, 7/1023 V/step */
  792. val->intval = ret * 6843;
  793. } else {
  794. ret = twl4030bci_read_adc_val(TWL4030_BCIVAC);
  795. if (ret < 0)
  796. return ret;
  797. /* BCIVAC uses ADCIN11, 10/1023 V/step */
  798. val->intval = ret * 9775;
  799. }
  800. break;
  801. case POWER_SUPPLY_PROP_CURRENT_NOW:
  802. if (!is_charging)
  803. return -ENODATA;
  804. /* current measurement is shared between AC and USB */
  805. ret = twl4030_charger_get_current();
  806. if (ret < 0)
  807. return ret;
  808. val->intval = ret;
  809. break;
  810. case POWER_SUPPLY_PROP_ONLINE:
  811. val->intval = is_charging &&
  812. twl4030_bci_state_to_status(state) !=
  813. POWER_SUPPLY_STATUS_NOT_CHARGING;
  814. break;
  815. default:
  816. return -EINVAL;
  817. }
  818. return 0;
  819. }
  820. static enum power_supply_property twl4030_charger_props[] = {
  821. POWER_SUPPLY_PROP_STATUS,
  822. POWER_SUPPLY_PROP_ONLINE,
  823. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  824. POWER_SUPPLY_PROP_CURRENT_NOW,
  825. };
  826. #ifdef CONFIG_OF
  827. static const struct twl4030_bci_platform_data *
  828. twl4030_bci_parse_dt(struct device *dev)
  829. {
  830. struct device_node *np = dev->of_node;
  831. struct twl4030_bci_platform_data *pdata;
  832. u32 num;
  833. if (!np)
  834. return NULL;
  835. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  836. if (!pdata)
  837. return pdata;
  838. if (of_property_read_u32(np, "ti,bb-uvolt", &num) == 0)
  839. pdata->bb_uvolt = num;
  840. if (of_property_read_u32(np, "ti,bb-uamp", &num) == 0)
  841. pdata->bb_uamp = num;
  842. return pdata;
  843. }
  844. #else
  845. static inline const struct twl4030_bci_platform_data *
  846. twl4030_bci_parse_dt(struct device *dev)
  847. {
  848. return NULL;
  849. }
  850. #endif
  851. static const struct power_supply_desc twl4030_bci_ac_desc = {
  852. .name = "twl4030_ac",
  853. .type = POWER_SUPPLY_TYPE_MAINS,
  854. .properties = twl4030_charger_props,
  855. .num_properties = ARRAY_SIZE(twl4030_charger_props),
  856. .get_property = twl4030_bci_get_property,
  857. };
  858. static const struct power_supply_desc twl4030_bci_usb_desc = {
  859. .name = "twl4030_usb",
  860. .type = POWER_SUPPLY_TYPE_USB,
  861. .properties = twl4030_charger_props,
  862. .num_properties = ARRAY_SIZE(twl4030_charger_props),
  863. .get_property = twl4030_bci_get_property,
  864. };
  865. static int twl4030_bci_probe(struct platform_device *pdev)
  866. {
  867. struct twl4030_bci *bci;
  868. const struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
  869. int ret;
  870. u32 reg;
  871. bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL);
  872. if (bci == NULL)
  873. return -ENOMEM;
  874. if (!pdata)
  875. pdata = twl4030_bci_parse_dt(&pdev->dev);
  876. bci->ichg_eoc = 80100; /* Stop charging when current drops to here */
  877. bci->ichg_lo = 241000; /* Low threshold */
  878. bci->ichg_hi = 500000; /* High threshold */
  879. bci->ac_cur = 500000; /* 500mA */
  880. if (allow_usb)
  881. bci->usb_cur_target = 500000; /* 500mA */
  882. else
  883. bci->usb_cur_target = 100000; /* 100mA */
  884. bci->usb_mode = CHARGE_AUTO;
  885. bci->ac_mode = CHARGE_AUTO;
  886. bci->dev = &pdev->dev;
  887. bci->irq_chg = platform_get_irq(pdev, 0);
  888. bci->irq_bci = platform_get_irq(pdev, 1);
  889. /* Only proceed further *IF* battery is physically present */
  890. ret = twl4030_is_battery_present(bci);
  891. if (ret) {
  892. dev_crit(&pdev->dev, "Battery was not detected:%d\n", ret);
  893. return ret;
  894. }
  895. platform_set_drvdata(pdev, bci);
  896. bci->ac = devm_power_supply_register(&pdev->dev, &twl4030_bci_ac_desc,
  897. NULL);
  898. if (IS_ERR(bci->ac)) {
  899. ret = PTR_ERR(bci->ac);
  900. dev_err(&pdev->dev, "failed to register ac: %d\n", ret);
  901. return ret;
  902. }
  903. bci->usb = devm_power_supply_register(&pdev->dev, &twl4030_bci_usb_desc,
  904. NULL);
  905. if (IS_ERR(bci->usb)) {
  906. ret = PTR_ERR(bci->usb);
  907. dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
  908. return ret;
  909. }
  910. ret = devm_request_threaded_irq(&pdev->dev, bci->irq_chg, NULL,
  911. twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name,
  912. bci);
  913. if (ret < 0) {
  914. dev_err(&pdev->dev, "could not request irq %d, status %d\n",
  915. bci->irq_chg, ret);
  916. return ret;
  917. }
  918. ret = devm_request_threaded_irq(&pdev->dev, bci->irq_bci, NULL,
  919. twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci);
  920. if (ret < 0) {
  921. dev_err(&pdev->dev, "could not request irq %d, status %d\n",
  922. bci->irq_bci, ret);
  923. return ret;
  924. }
  925. bci->channel_vac = iio_channel_get(&pdev->dev, "vac");
  926. if (IS_ERR(bci->channel_vac)) {
  927. bci->channel_vac = NULL;
  928. dev_warn(&pdev->dev, "could not request vac iio channel");
  929. }
  930. INIT_WORK(&bci->work, twl4030_bci_usb_work);
  931. INIT_DELAYED_WORK(&bci->current_worker, twl4030_current_worker);
  932. bci->usb_nb.notifier_call = twl4030_bci_usb_ncb;
  933. if (bci->dev->of_node) {
  934. struct device_node *phynode;
  935. phynode = of_find_compatible_node(bci->dev->of_node->parent,
  936. NULL, "ti,twl4030-usb");
  937. if (phynode)
  938. bci->transceiver = devm_usb_get_phy_by_node(
  939. bci->dev, phynode, &bci->usb_nb);
  940. }
  941. /* Enable interrupts now. */
  942. reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 |
  943. TWL4030_TBATOR1 | TWL4030_BATSTS);
  944. ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
  945. TWL4030_INTERRUPTS_BCIIMR1A);
  946. if (ret < 0) {
  947. dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
  948. goto fail;
  949. }
  950. reg = ~(u32)(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV);
  951. ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
  952. TWL4030_INTERRUPTS_BCIIMR2A);
  953. if (ret < 0)
  954. dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
  955. twl4030_charger_update_current(bci);
  956. if (device_create_file(&bci->usb->dev, &dev_attr_max_current))
  957. dev_warn(&pdev->dev, "could not create sysfs file\n");
  958. if (device_create_file(&bci->usb->dev, &dev_attr_mode))
  959. dev_warn(&pdev->dev, "could not create sysfs file\n");
  960. if (device_create_file(&bci->ac->dev, &dev_attr_mode))
  961. dev_warn(&pdev->dev, "could not create sysfs file\n");
  962. if (device_create_file(&bci->ac->dev, &dev_attr_max_current))
  963. dev_warn(&pdev->dev, "could not create sysfs file\n");
  964. twl4030_charger_enable_ac(bci, true);
  965. if (!IS_ERR_OR_NULL(bci->transceiver))
  966. twl4030_bci_usb_ncb(&bci->usb_nb,
  967. bci->transceiver->last_event,
  968. NULL);
  969. else
  970. twl4030_charger_enable_usb(bci, false);
  971. if (pdata)
  972. twl4030_charger_enable_backup(pdata->bb_uvolt,
  973. pdata->bb_uamp);
  974. else
  975. twl4030_charger_enable_backup(0, 0);
  976. return 0;
  977. fail:
  978. iio_channel_release(bci->channel_vac);
  979. return ret;
  980. }
  981. static int __exit twl4030_bci_remove(struct platform_device *pdev)
  982. {
  983. struct twl4030_bci *bci = platform_get_drvdata(pdev);
  984. twl4030_charger_enable_ac(bci, false);
  985. twl4030_charger_enable_usb(bci, false);
  986. twl4030_charger_enable_backup(0, 0);
  987. iio_channel_release(bci->channel_vac);
  988. device_remove_file(&bci->usb->dev, &dev_attr_max_current);
  989. device_remove_file(&bci->usb->dev, &dev_attr_mode);
  990. device_remove_file(&bci->ac->dev, &dev_attr_max_current);
  991. device_remove_file(&bci->ac->dev, &dev_attr_mode);
  992. /* mask interrupts */
  993. twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
  994. TWL4030_INTERRUPTS_BCIIMR1A);
  995. twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
  996. TWL4030_INTERRUPTS_BCIIMR2A);
  997. return 0;
  998. }
  999. static const struct of_device_id twl_bci_of_match[] = {
  1000. {.compatible = "ti,twl4030-bci", },
  1001. { }
  1002. };
  1003. MODULE_DEVICE_TABLE(of, twl_bci_of_match);
  1004. static struct platform_driver twl4030_bci_driver = {
  1005. .probe = twl4030_bci_probe,
  1006. .driver = {
  1007. .name = "twl4030_bci",
  1008. .of_match_table = of_match_ptr(twl_bci_of_match),
  1009. },
  1010. .remove = __exit_p(twl4030_bci_remove),
  1011. };
  1012. module_platform_driver(twl4030_bci_driver);
  1013. MODULE_AUTHOR("Gražvydas Ignotas");
  1014. MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
  1015. MODULE_LICENSE("GPL");
  1016. MODULE_ALIAS("platform:twl4030_bci");