lp8788-charger.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /*
  2. * TI LP8788 MFD - battery charger driver
  3. *
  4. * Copyright 2012 Texas Instruments
  5. *
  6. * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/err.h>
  14. #include <linux/iio/consumer.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/mfd/lp8788.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/power_supply.h>
  21. #include <linux/slab.h>
  22. #include <linux/workqueue.h>
  23. /* register address */
  24. #define LP8788_CHG_STATUS 0x07
  25. #define LP8788_CHG_IDCIN 0x13
  26. #define LP8788_CHG_IBATT 0x14
  27. #define LP8788_CHG_VTERM 0x15
  28. #define LP8788_CHG_EOC 0x16
  29. /* mask/shift bits */
  30. #define LP8788_CHG_INPUT_STATE_M 0x03 /* Addr 07h */
  31. #define LP8788_CHG_STATE_M 0x3C
  32. #define LP8788_CHG_STATE_S 2
  33. #define LP8788_NO_BATT_M BIT(6)
  34. #define LP8788_BAD_BATT_M BIT(7)
  35. #define LP8788_CHG_IBATT_M 0x1F /* Addr 14h */
  36. #define LP8788_CHG_VTERM_M 0x0F /* Addr 15h */
  37. #define LP8788_CHG_EOC_LEVEL_M 0x30 /* Addr 16h */
  38. #define LP8788_CHG_EOC_LEVEL_S 4
  39. #define LP8788_CHG_EOC_TIME_M 0x0E
  40. #define LP8788_CHG_EOC_TIME_S 1
  41. #define LP8788_CHG_EOC_MODE_M BIT(0)
  42. #define LP8788_CHARGER_NAME "charger"
  43. #define LP8788_BATTERY_NAME "main_batt"
  44. #define LP8788_CHG_START 0x11
  45. #define LP8788_CHG_END 0x1C
  46. #define LP8788_ISEL_MAX 23
  47. #define LP8788_ISEL_STEP 50
  48. #define LP8788_VTERM_MIN 4100
  49. #define LP8788_VTERM_STEP 25
  50. #define LP8788_MAX_BATT_CAPACITY 100
  51. #define LP8788_MAX_CHG_IRQS 11
  52. enum lp8788_charging_state {
  53. LP8788_OFF,
  54. LP8788_WARM_UP,
  55. LP8788_LOW_INPUT = 0x3,
  56. LP8788_PRECHARGE,
  57. LP8788_CC,
  58. LP8788_CV,
  59. LP8788_MAINTENANCE,
  60. LP8788_BATTERY_FAULT,
  61. LP8788_SYSTEM_SUPPORT = 0xC,
  62. LP8788_HIGH_CURRENT = 0xF,
  63. LP8788_MAX_CHG_STATE,
  64. };
  65. enum lp8788_charger_adc_sel {
  66. LP8788_VBATT,
  67. LP8788_BATT_TEMP,
  68. LP8788_NUM_CHG_ADC,
  69. };
  70. enum lp8788_charger_input_state {
  71. LP8788_SYSTEM_SUPPLY = 1,
  72. LP8788_FULL_FUNCTION,
  73. };
  74. /*
  75. * struct lp8788_chg_irq
  76. * @which : lp8788 interrupt id
  77. * @virq : Linux IRQ number from irq_domain
  78. */
  79. struct lp8788_chg_irq {
  80. enum lp8788_int_id which;
  81. int virq;
  82. };
  83. /*
  84. * struct lp8788_charger
  85. * @lp : used for accessing the registers of mfd lp8788 device
  86. * @charger : power supply driver for the battery charger
  87. * @battery : power supply driver for the battery
  88. * @charger_work : work queue for charger input interrupts
  89. * @chan : iio channels for getting adc values
  90. * eg) battery voltage, capacity and temperature
  91. * @irqs : charger dedicated interrupts
  92. * @num_irqs : total numbers of charger interrupts
  93. * @pdata : charger platform specific data
  94. */
  95. struct lp8788_charger {
  96. struct lp8788 *lp;
  97. struct power_supply *charger;
  98. struct power_supply *battery;
  99. struct work_struct charger_work;
  100. struct iio_channel *chan[LP8788_NUM_CHG_ADC];
  101. struct lp8788_chg_irq irqs[LP8788_MAX_CHG_IRQS];
  102. int num_irqs;
  103. struct lp8788_charger_platform_data *pdata;
  104. };
  105. static char *battery_supplied_to[] = {
  106. LP8788_BATTERY_NAME,
  107. };
  108. static enum power_supply_property lp8788_charger_prop[] = {
  109. POWER_SUPPLY_PROP_ONLINE,
  110. POWER_SUPPLY_PROP_CURRENT_MAX,
  111. };
  112. static enum power_supply_property lp8788_battery_prop[] = {
  113. POWER_SUPPLY_PROP_STATUS,
  114. POWER_SUPPLY_PROP_HEALTH,
  115. POWER_SUPPLY_PROP_PRESENT,
  116. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  117. POWER_SUPPLY_PROP_CAPACITY,
  118. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  119. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  120. POWER_SUPPLY_PROP_TEMP,
  121. };
  122. static bool lp8788_is_charger_detected(struct lp8788_charger *pchg)
  123. {
  124. u8 data;
  125. lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data);
  126. data &= LP8788_CHG_INPUT_STATE_M;
  127. return data == LP8788_SYSTEM_SUPPLY || data == LP8788_FULL_FUNCTION;
  128. }
  129. static int lp8788_charger_get_property(struct power_supply *psy,
  130. enum power_supply_property psp,
  131. union power_supply_propval *val)
  132. {
  133. struct lp8788_charger *pchg = dev_get_drvdata(psy->dev.parent);
  134. u8 read;
  135. switch (psp) {
  136. case POWER_SUPPLY_PROP_ONLINE:
  137. val->intval = lp8788_is_charger_detected(pchg);
  138. break;
  139. case POWER_SUPPLY_PROP_CURRENT_MAX:
  140. lp8788_read_byte(pchg->lp, LP8788_CHG_IDCIN, &read);
  141. val->intval = LP8788_ISEL_STEP *
  142. (min_t(int, read, LP8788_ISEL_MAX) + 1);
  143. break;
  144. default:
  145. return -EINVAL;
  146. }
  147. return 0;
  148. }
  149. static int lp8788_get_battery_status(struct lp8788_charger *pchg,
  150. union power_supply_propval *val)
  151. {
  152. enum lp8788_charging_state state;
  153. u8 data;
  154. int ret;
  155. ret = lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data);
  156. if (ret)
  157. return ret;
  158. state = (data & LP8788_CHG_STATE_M) >> LP8788_CHG_STATE_S;
  159. switch (state) {
  160. case LP8788_OFF:
  161. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  162. break;
  163. case LP8788_PRECHARGE:
  164. case LP8788_CC:
  165. case LP8788_CV:
  166. case LP8788_HIGH_CURRENT:
  167. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  168. break;
  169. case LP8788_MAINTENANCE:
  170. val->intval = POWER_SUPPLY_STATUS_FULL;
  171. break;
  172. default:
  173. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  174. break;
  175. }
  176. return 0;
  177. }
  178. static int lp8788_get_battery_health(struct lp8788_charger *pchg,
  179. union power_supply_propval *val)
  180. {
  181. u8 data;
  182. int ret;
  183. ret = lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data);
  184. if (ret)
  185. return ret;
  186. if (data & LP8788_NO_BATT_M)
  187. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  188. else if (data & LP8788_BAD_BATT_M)
  189. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  190. else
  191. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  192. return 0;
  193. }
  194. static int lp8788_get_battery_present(struct lp8788_charger *pchg,
  195. union power_supply_propval *val)
  196. {
  197. u8 data;
  198. int ret;
  199. ret = lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data);
  200. if (ret)
  201. return ret;
  202. val->intval = !(data & LP8788_NO_BATT_M);
  203. return 0;
  204. }
  205. static int lp8788_get_vbatt_adc(struct lp8788_charger *pchg, int *result)
  206. {
  207. struct iio_channel *channel = pchg->chan[LP8788_VBATT];
  208. if (!channel)
  209. return -EINVAL;
  210. return iio_read_channel_processed(channel, result);
  211. }
  212. static int lp8788_get_battery_voltage(struct lp8788_charger *pchg,
  213. union power_supply_propval *val)
  214. {
  215. return lp8788_get_vbatt_adc(pchg, &val->intval);
  216. }
  217. static int lp8788_get_battery_capacity(struct lp8788_charger *pchg,
  218. union power_supply_propval *val)
  219. {
  220. struct lp8788 *lp = pchg->lp;
  221. struct lp8788_charger_platform_data *pdata = pchg->pdata;
  222. unsigned int max_vbatt;
  223. int vbatt;
  224. enum lp8788_charging_state state;
  225. u8 data;
  226. int ret;
  227. if (!pdata)
  228. return -EINVAL;
  229. max_vbatt = pdata->max_vbatt_mv;
  230. if (max_vbatt == 0)
  231. return -EINVAL;
  232. ret = lp8788_read_byte(lp, LP8788_CHG_STATUS, &data);
  233. if (ret)
  234. return ret;
  235. state = (data & LP8788_CHG_STATE_M) >> LP8788_CHG_STATE_S;
  236. if (state == LP8788_MAINTENANCE) {
  237. val->intval = LP8788_MAX_BATT_CAPACITY;
  238. } else {
  239. ret = lp8788_get_vbatt_adc(pchg, &vbatt);
  240. if (ret)
  241. return ret;
  242. val->intval = (vbatt * LP8788_MAX_BATT_CAPACITY) / max_vbatt;
  243. val->intval = min(val->intval, LP8788_MAX_BATT_CAPACITY);
  244. }
  245. return 0;
  246. }
  247. static int lp8788_get_battery_temperature(struct lp8788_charger *pchg,
  248. union power_supply_propval *val)
  249. {
  250. struct iio_channel *channel = pchg->chan[LP8788_BATT_TEMP];
  251. int result;
  252. int ret;
  253. if (!channel)
  254. return -EINVAL;
  255. ret = iio_read_channel_processed(channel, &result);
  256. if (ret < 0)
  257. return -EINVAL;
  258. /* unit: 0.1 'C */
  259. val->intval = result * 10;
  260. return 0;
  261. }
  262. static int lp8788_get_battery_charging_current(struct lp8788_charger *pchg,
  263. union power_supply_propval *val)
  264. {
  265. u8 read;
  266. lp8788_read_byte(pchg->lp, LP8788_CHG_IBATT, &read);
  267. read &= LP8788_CHG_IBATT_M;
  268. val->intval = LP8788_ISEL_STEP *
  269. (min_t(int, read, LP8788_ISEL_MAX) + 1);
  270. return 0;
  271. }
  272. static int lp8788_get_charging_termination_voltage(struct lp8788_charger *pchg,
  273. union power_supply_propval *val)
  274. {
  275. u8 read;
  276. lp8788_read_byte(pchg->lp, LP8788_CHG_VTERM, &read);
  277. read &= LP8788_CHG_VTERM_M;
  278. val->intval = LP8788_VTERM_MIN + LP8788_VTERM_STEP * read;
  279. return 0;
  280. }
  281. static int lp8788_battery_get_property(struct power_supply *psy,
  282. enum power_supply_property psp,
  283. union power_supply_propval *val)
  284. {
  285. struct lp8788_charger *pchg = dev_get_drvdata(psy->dev.parent);
  286. switch (psp) {
  287. case POWER_SUPPLY_PROP_STATUS:
  288. return lp8788_get_battery_status(pchg, val);
  289. case POWER_SUPPLY_PROP_HEALTH:
  290. return lp8788_get_battery_health(pchg, val);
  291. case POWER_SUPPLY_PROP_PRESENT:
  292. return lp8788_get_battery_present(pchg, val);
  293. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  294. return lp8788_get_battery_voltage(pchg, val);
  295. case POWER_SUPPLY_PROP_CAPACITY:
  296. return lp8788_get_battery_capacity(pchg, val);
  297. case POWER_SUPPLY_PROP_TEMP:
  298. return lp8788_get_battery_temperature(pchg, val);
  299. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  300. return lp8788_get_battery_charging_current(pchg, val);
  301. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  302. return lp8788_get_charging_termination_voltage(pchg, val);
  303. default:
  304. return -EINVAL;
  305. }
  306. }
  307. static inline bool lp8788_is_valid_charger_register(u8 addr)
  308. {
  309. return addr >= LP8788_CHG_START && addr <= LP8788_CHG_END;
  310. }
  311. static int lp8788_update_charger_params(struct platform_device *pdev,
  312. struct lp8788_charger *pchg)
  313. {
  314. struct lp8788 *lp = pchg->lp;
  315. struct lp8788_charger_platform_data *pdata = pchg->pdata;
  316. struct lp8788_chg_param *param;
  317. int i;
  318. int ret;
  319. if (!pdata || !pdata->chg_params) {
  320. dev_info(&pdev->dev, "skip updating charger parameters\n");
  321. return 0;
  322. }
  323. /* settting charging parameters */
  324. for (i = 0; i < pdata->num_chg_params; i++) {
  325. param = pdata->chg_params + i;
  326. if (!param)
  327. continue;
  328. if (lp8788_is_valid_charger_register(param->addr)) {
  329. ret = lp8788_write_byte(lp, param->addr, param->val);
  330. if (ret)
  331. return ret;
  332. }
  333. }
  334. return 0;
  335. }
  336. static const struct power_supply_desc lp8788_psy_charger_desc = {
  337. .name = LP8788_CHARGER_NAME,
  338. .type = POWER_SUPPLY_TYPE_MAINS,
  339. .properties = lp8788_charger_prop,
  340. .num_properties = ARRAY_SIZE(lp8788_charger_prop),
  341. .get_property = lp8788_charger_get_property,
  342. };
  343. static const struct power_supply_desc lp8788_psy_battery_desc = {
  344. .name = LP8788_BATTERY_NAME,
  345. .type = POWER_SUPPLY_TYPE_BATTERY,
  346. .properties = lp8788_battery_prop,
  347. .num_properties = ARRAY_SIZE(lp8788_battery_prop),
  348. .get_property = lp8788_battery_get_property,
  349. };
  350. static int lp8788_psy_register(struct platform_device *pdev,
  351. struct lp8788_charger *pchg)
  352. {
  353. struct power_supply_config charger_cfg = {};
  354. charger_cfg.supplied_to = battery_supplied_to;
  355. charger_cfg.num_supplicants = ARRAY_SIZE(battery_supplied_to);
  356. pchg->charger = power_supply_register(&pdev->dev,
  357. &lp8788_psy_charger_desc,
  358. &charger_cfg);
  359. if (IS_ERR(pchg->charger))
  360. return -EPERM;
  361. pchg->battery = power_supply_register(&pdev->dev,
  362. &lp8788_psy_battery_desc, NULL);
  363. if (IS_ERR(pchg->battery)) {
  364. power_supply_unregister(pchg->charger);
  365. return -EPERM;
  366. }
  367. return 0;
  368. }
  369. static void lp8788_psy_unregister(struct lp8788_charger *pchg)
  370. {
  371. power_supply_unregister(pchg->battery);
  372. power_supply_unregister(pchg->charger);
  373. }
  374. static void lp8788_charger_event(struct work_struct *work)
  375. {
  376. struct lp8788_charger *pchg =
  377. container_of(work, struct lp8788_charger, charger_work);
  378. struct lp8788_charger_platform_data *pdata = pchg->pdata;
  379. enum lp8788_charger_event event = lp8788_is_charger_detected(pchg);
  380. pdata->charger_event(pchg->lp, event);
  381. }
  382. static bool lp8788_find_irq_id(struct lp8788_charger *pchg, int virq, int *id)
  383. {
  384. bool found = false;
  385. int i;
  386. for (i = 0; i < pchg->num_irqs; i++) {
  387. if (pchg->irqs[i].virq == virq) {
  388. *id = pchg->irqs[i].which;
  389. found = true;
  390. break;
  391. }
  392. }
  393. return found;
  394. }
  395. static irqreturn_t lp8788_charger_irq_thread(int virq, void *ptr)
  396. {
  397. struct lp8788_charger *pchg = ptr;
  398. struct lp8788_charger_platform_data *pdata = pchg->pdata;
  399. int id = -1;
  400. if (!lp8788_find_irq_id(pchg, virq, &id))
  401. return IRQ_NONE;
  402. switch (id) {
  403. case LP8788_INT_CHG_INPUT_STATE:
  404. case LP8788_INT_CHG_STATE:
  405. case LP8788_INT_EOC:
  406. case LP8788_INT_BATT_LOW:
  407. case LP8788_INT_NO_BATT:
  408. power_supply_changed(pchg->charger);
  409. power_supply_changed(pchg->battery);
  410. break;
  411. default:
  412. break;
  413. }
  414. /* report charger dectection event if used */
  415. if (!pdata)
  416. goto irq_handled;
  417. if (pdata->charger_event && id == LP8788_INT_CHG_INPUT_STATE)
  418. schedule_work(&pchg->charger_work);
  419. irq_handled:
  420. return IRQ_HANDLED;
  421. }
  422. static int lp8788_set_irqs(struct platform_device *pdev,
  423. struct lp8788_charger *pchg, const char *name)
  424. {
  425. struct resource *r;
  426. struct irq_domain *irqdm = pchg->lp->irqdm;
  427. int irq_start;
  428. int irq_end;
  429. int virq;
  430. int nr_irq;
  431. int i;
  432. int ret;
  433. /* no error even if no irq resource */
  434. r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, name);
  435. if (!r)
  436. return 0;
  437. irq_start = r->start;
  438. irq_end = r->end;
  439. for (i = irq_start; i <= irq_end; i++) {
  440. nr_irq = pchg->num_irqs;
  441. virq = irq_create_mapping(irqdm, i);
  442. pchg->irqs[nr_irq].virq = virq;
  443. pchg->irqs[nr_irq].which = i;
  444. pchg->num_irqs++;
  445. ret = request_threaded_irq(virq, NULL,
  446. lp8788_charger_irq_thread,
  447. 0, name, pchg);
  448. if (ret)
  449. break;
  450. }
  451. if (i <= irq_end)
  452. goto err_free_irq;
  453. return 0;
  454. err_free_irq:
  455. for (i = 0; i < pchg->num_irqs; i++)
  456. free_irq(pchg->irqs[i].virq, pchg);
  457. return ret;
  458. }
  459. static int lp8788_irq_register(struct platform_device *pdev,
  460. struct lp8788_charger *pchg)
  461. {
  462. const char *name[] = {
  463. LP8788_CHG_IRQ, LP8788_PRSW_IRQ, LP8788_BATT_IRQ
  464. };
  465. int i;
  466. int ret;
  467. INIT_WORK(&pchg->charger_work, lp8788_charger_event);
  468. pchg->num_irqs = 0;
  469. for (i = 0; i < ARRAY_SIZE(name); i++) {
  470. ret = lp8788_set_irqs(pdev, pchg, name[i]);
  471. if (ret) {
  472. dev_warn(&pdev->dev, "irq setup failed: %s\n", name[i]);
  473. return ret;
  474. }
  475. }
  476. if (pchg->num_irqs > LP8788_MAX_CHG_IRQS) {
  477. dev_err(&pdev->dev, "invalid total number of irqs: %d\n",
  478. pchg->num_irqs);
  479. return -EINVAL;
  480. }
  481. return 0;
  482. }
  483. static void lp8788_irq_unregister(struct platform_device *pdev,
  484. struct lp8788_charger *pchg)
  485. {
  486. int i;
  487. int irq;
  488. for (i = 0; i < pchg->num_irqs; i++) {
  489. irq = pchg->irqs[i].virq;
  490. if (!irq)
  491. continue;
  492. free_irq(irq, pchg);
  493. }
  494. }
  495. static void lp8788_setup_adc_channel(struct device *dev,
  496. struct lp8788_charger *pchg)
  497. {
  498. struct lp8788_charger_platform_data *pdata = pchg->pdata;
  499. struct iio_channel *chan;
  500. if (!pdata)
  501. return;
  502. /* ADC channel for battery voltage */
  503. chan = iio_channel_get(dev, pdata->adc_vbatt);
  504. pchg->chan[LP8788_VBATT] = IS_ERR(chan) ? NULL : chan;
  505. /* ADC channel for battery temperature */
  506. chan = iio_channel_get(dev, pdata->adc_batt_temp);
  507. pchg->chan[LP8788_BATT_TEMP] = IS_ERR(chan) ? NULL : chan;
  508. }
  509. static void lp8788_release_adc_channel(struct lp8788_charger *pchg)
  510. {
  511. int i;
  512. for (i = 0; i < LP8788_NUM_CHG_ADC; i++) {
  513. if (!pchg->chan[i])
  514. continue;
  515. iio_channel_release(pchg->chan[i]);
  516. pchg->chan[i] = NULL;
  517. }
  518. }
  519. static ssize_t lp8788_show_charger_status(struct device *dev,
  520. struct device_attribute *attr, char *buf)
  521. {
  522. struct lp8788_charger *pchg = dev_get_drvdata(dev);
  523. enum lp8788_charging_state state;
  524. char *desc[LP8788_MAX_CHG_STATE] = {
  525. [LP8788_OFF] = "CHARGER OFF",
  526. [LP8788_WARM_UP] = "WARM UP",
  527. [LP8788_LOW_INPUT] = "LOW INPUT STATE",
  528. [LP8788_PRECHARGE] = "CHARGING - PRECHARGE",
  529. [LP8788_CC] = "CHARGING - CC",
  530. [LP8788_CV] = "CHARGING - CV",
  531. [LP8788_MAINTENANCE] = "NO CHARGING - MAINTENANCE",
  532. [LP8788_BATTERY_FAULT] = "BATTERY FAULT",
  533. [LP8788_SYSTEM_SUPPORT] = "SYSTEM SUPPORT",
  534. [LP8788_HIGH_CURRENT] = "HIGH CURRENT",
  535. };
  536. u8 data;
  537. lp8788_read_byte(pchg->lp, LP8788_CHG_STATUS, &data);
  538. state = (data & LP8788_CHG_STATE_M) >> LP8788_CHG_STATE_S;
  539. return scnprintf(buf, PAGE_SIZE, "%s\n", desc[state]);
  540. }
  541. static ssize_t lp8788_show_eoc_time(struct device *dev,
  542. struct device_attribute *attr, char *buf)
  543. {
  544. struct lp8788_charger *pchg = dev_get_drvdata(dev);
  545. char *stime[] = { "400ms", "5min", "10min", "15min",
  546. "20min", "25min", "30min", "No timeout" };
  547. u8 val;
  548. lp8788_read_byte(pchg->lp, LP8788_CHG_EOC, &val);
  549. val = (val & LP8788_CHG_EOC_TIME_M) >> LP8788_CHG_EOC_TIME_S;
  550. return scnprintf(buf, PAGE_SIZE, "End Of Charge Time: %s\n",
  551. stime[val]);
  552. }
  553. static ssize_t lp8788_show_eoc_level(struct device *dev,
  554. struct device_attribute *attr, char *buf)
  555. {
  556. struct lp8788_charger *pchg = dev_get_drvdata(dev);
  557. char *abs_level[] = { "25mA", "49mA", "75mA", "98mA" };
  558. char *relative_level[] = { "5%", "10%", "15%", "20%" };
  559. char *level;
  560. u8 val;
  561. u8 mode;
  562. lp8788_read_byte(pchg->lp, LP8788_CHG_EOC, &val);
  563. mode = val & LP8788_CHG_EOC_MODE_M;
  564. val = (val & LP8788_CHG_EOC_LEVEL_M) >> LP8788_CHG_EOC_LEVEL_S;
  565. level = mode ? abs_level[val] : relative_level[val];
  566. return scnprintf(buf, PAGE_SIZE, "End Of Charge Level: %s\n", level);
  567. }
  568. static DEVICE_ATTR(charger_status, S_IRUSR, lp8788_show_charger_status, NULL);
  569. static DEVICE_ATTR(eoc_time, S_IRUSR, lp8788_show_eoc_time, NULL);
  570. static DEVICE_ATTR(eoc_level, S_IRUSR, lp8788_show_eoc_level, NULL);
  571. static struct attribute *lp8788_charger_attr[] = {
  572. &dev_attr_charger_status.attr,
  573. &dev_attr_eoc_time.attr,
  574. &dev_attr_eoc_level.attr,
  575. NULL,
  576. };
  577. static const struct attribute_group lp8788_attr_group = {
  578. .attrs = lp8788_charger_attr,
  579. };
  580. static int lp8788_charger_probe(struct platform_device *pdev)
  581. {
  582. struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
  583. struct lp8788_charger *pchg;
  584. struct device *dev = &pdev->dev;
  585. int ret;
  586. pchg = devm_kzalloc(dev, sizeof(struct lp8788_charger), GFP_KERNEL);
  587. if (!pchg)
  588. return -ENOMEM;
  589. pchg->lp = lp;
  590. pchg->pdata = lp->pdata ? lp->pdata->chg_pdata : NULL;
  591. platform_set_drvdata(pdev, pchg);
  592. ret = lp8788_update_charger_params(pdev, pchg);
  593. if (ret)
  594. return ret;
  595. lp8788_setup_adc_channel(&pdev->dev, pchg);
  596. ret = lp8788_psy_register(pdev, pchg);
  597. if (ret)
  598. return ret;
  599. ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group);
  600. if (ret) {
  601. lp8788_psy_unregister(pchg);
  602. return ret;
  603. }
  604. ret = lp8788_irq_register(pdev, pchg);
  605. if (ret)
  606. dev_warn(dev, "failed to register charger irq: %d\n", ret);
  607. return 0;
  608. }
  609. static int lp8788_charger_remove(struct platform_device *pdev)
  610. {
  611. struct lp8788_charger *pchg = platform_get_drvdata(pdev);
  612. flush_work(&pchg->charger_work);
  613. lp8788_irq_unregister(pdev, pchg);
  614. sysfs_remove_group(&pdev->dev.kobj, &lp8788_attr_group);
  615. lp8788_psy_unregister(pchg);
  616. lp8788_release_adc_channel(pchg);
  617. return 0;
  618. }
  619. static struct platform_driver lp8788_charger_driver = {
  620. .probe = lp8788_charger_probe,
  621. .remove = lp8788_charger_remove,
  622. .driver = {
  623. .name = LP8788_DEV_CHARGER,
  624. },
  625. };
  626. module_platform_driver(lp8788_charger_driver);
  627. MODULE_DESCRIPTION("TI LP8788 Charger Driver");
  628. MODULE_AUTHOR("Milo Kim");
  629. MODULE_LICENSE("GPL");
  630. MODULE_ALIAS("platform:lp8788-charger");