lp8727_charger.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /*
  2. * Driver for LP8727 Micro/Mini USB IC with integrated charger
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Copyright (C) 2011 National Semiconductor
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/i2c.h>
  16. #include <linux/power_supply.h>
  17. #include <linux/platform_data/lp8727.h>
  18. #include <linux/of.h>
  19. #define LP8788_NUM_INTREGS 2
  20. #define DEFAULT_DEBOUNCE_MSEC 270
  21. /* Registers */
  22. #define LP8727_CTRL1 0x1
  23. #define LP8727_CTRL2 0x2
  24. #define LP8727_SWCTRL 0x3
  25. #define LP8727_INT1 0x4
  26. #define LP8727_INT2 0x5
  27. #define LP8727_STATUS1 0x6
  28. #define LP8727_STATUS2 0x7
  29. #define LP8727_CHGCTRL2 0x9
  30. /* CTRL1 register */
  31. #define LP8727_CP_EN BIT(0)
  32. #define LP8727_ADC_EN BIT(1)
  33. #define LP8727_ID200_EN BIT(4)
  34. /* CTRL2 register */
  35. #define LP8727_CHGDET_EN BIT(1)
  36. #define LP8727_INT_EN BIT(6)
  37. /* SWCTRL register */
  38. #define LP8727_SW_DM1_DM (0x0 << 0)
  39. #define LP8727_SW_DM1_HiZ (0x7 << 0)
  40. #define LP8727_SW_DP2_DP (0x0 << 3)
  41. #define LP8727_SW_DP2_HiZ (0x7 << 3)
  42. /* INT1 register */
  43. #define LP8727_IDNO (0xF << 0)
  44. #define LP8727_VBUS BIT(4)
  45. /* STATUS1 register */
  46. #define LP8727_CHGSTAT (3 << 4)
  47. #define LP8727_CHPORT BIT(6)
  48. #define LP8727_DCPORT BIT(7)
  49. #define LP8727_STAT_EOC 0x30
  50. /* STATUS2 register */
  51. #define LP8727_TEMP_STAT (3 << 5)
  52. #define LP8727_TEMP_SHIFT 5
  53. /* CHGCTRL2 register */
  54. #define LP8727_ICHG_SHIFT 4
  55. enum lp8727_dev_id {
  56. LP8727_ID_NONE,
  57. LP8727_ID_TA,
  58. LP8727_ID_DEDICATED_CHG,
  59. LP8727_ID_USB_CHG,
  60. LP8727_ID_USB_DS,
  61. LP8727_ID_MAX,
  62. };
  63. enum lp8727_die_temp {
  64. LP8788_TEMP_75C,
  65. LP8788_TEMP_95C,
  66. LP8788_TEMP_115C,
  67. LP8788_TEMP_135C,
  68. };
  69. struct lp8727_psy {
  70. struct power_supply *ac;
  71. struct power_supply *usb;
  72. struct power_supply *batt;
  73. };
  74. struct lp8727_chg {
  75. struct device *dev;
  76. struct i2c_client *client;
  77. struct mutex xfer_lock;
  78. struct lp8727_psy *psy;
  79. struct lp8727_platform_data *pdata;
  80. /* Charger Data */
  81. enum lp8727_dev_id devid;
  82. struct lp8727_chg_param *chg_param;
  83. /* Interrupt Handling */
  84. int irq;
  85. struct delayed_work work;
  86. unsigned long debounce_jiffies;
  87. };
  88. static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
  89. {
  90. s32 ret;
  91. mutex_lock(&pchg->xfer_lock);
  92. ret = i2c_smbus_read_i2c_block_data(pchg->client, reg, len, data);
  93. mutex_unlock(&pchg->xfer_lock);
  94. return (ret != len) ? -EIO : 0;
  95. }
  96. static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data)
  97. {
  98. return lp8727_read_bytes(pchg, reg, data, 1);
  99. }
  100. static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data)
  101. {
  102. int ret;
  103. mutex_lock(&pchg->xfer_lock);
  104. ret = i2c_smbus_write_byte_data(pchg->client, reg, data);
  105. mutex_unlock(&pchg->xfer_lock);
  106. return ret;
  107. }
  108. static bool lp8727_is_charger_attached(const char *name, int id)
  109. {
  110. if (!strcmp(name, "ac"))
  111. return id == LP8727_ID_TA || id == LP8727_ID_DEDICATED_CHG;
  112. else if (!strcmp(name, "usb"))
  113. return id == LP8727_ID_USB_CHG;
  114. return id >= LP8727_ID_TA && id <= LP8727_ID_USB_CHG;
  115. }
  116. static int lp8727_init_device(struct lp8727_chg *pchg)
  117. {
  118. u8 val;
  119. int ret;
  120. u8 intstat[LP8788_NUM_INTREGS];
  121. /* clear interrupts */
  122. ret = lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS);
  123. if (ret)
  124. return ret;
  125. val = LP8727_ID200_EN | LP8727_ADC_EN | LP8727_CP_EN;
  126. ret = lp8727_write_byte(pchg, LP8727_CTRL1, val);
  127. if (ret)
  128. return ret;
  129. val = LP8727_INT_EN | LP8727_CHGDET_EN;
  130. return lp8727_write_byte(pchg, LP8727_CTRL2, val);
  131. }
  132. static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
  133. {
  134. u8 val;
  135. lp8727_read_byte(pchg, LP8727_STATUS1, &val);
  136. return val & LP8727_DCPORT;
  137. }
  138. static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
  139. {
  140. u8 val;
  141. lp8727_read_byte(pchg, LP8727_STATUS1, &val);
  142. return val & LP8727_CHPORT;
  143. }
  144. static inline void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
  145. {
  146. lp8727_write_byte(pchg, LP8727_SWCTRL, sw);
  147. }
  148. static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin)
  149. {
  150. struct lp8727_platform_data *pdata = pchg->pdata;
  151. u8 devid = LP8727_ID_NONE;
  152. u8 swctrl = LP8727_SW_DM1_HiZ | LP8727_SW_DP2_HiZ;
  153. switch (id) {
  154. case 0x5:
  155. devid = LP8727_ID_TA;
  156. pchg->chg_param = pdata ? pdata->ac : NULL;
  157. break;
  158. case 0xB:
  159. if (lp8727_is_dedicated_charger(pchg)) {
  160. pchg->chg_param = pdata ? pdata->ac : NULL;
  161. devid = LP8727_ID_DEDICATED_CHG;
  162. } else if (lp8727_is_usb_charger(pchg)) {
  163. pchg->chg_param = pdata ? pdata->usb : NULL;
  164. devid = LP8727_ID_USB_CHG;
  165. swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
  166. } else if (vbusin) {
  167. devid = LP8727_ID_USB_DS;
  168. swctrl = LP8727_SW_DM1_DM | LP8727_SW_DP2_DP;
  169. }
  170. break;
  171. default:
  172. devid = LP8727_ID_NONE;
  173. pchg->chg_param = NULL;
  174. break;
  175. }
  176. pchg->devid = devid;
  177. lp8727_ctrl_switch(pchg, swctrl);
  178. }
  179. static void lp8727_enable_chgdet(struct lp8727_chg *pchg)
  180. {
  181. u8 val;
  182. lp8727_read_byte(pchg, LP8727_CTRL2, &val);
  183. val |= LP8727_CHGDET_EN;
  184. lp8727_write_byte(pchg, LP8727_CTRL2, val);
  185. }
  186. static void lp8727_delayed_func(struct work_struct *_work)
  187. {
  188. struct lp8727_chg *pchg = container_of(_work, struct lp8727_chg,
  189. work.work);
  190. u8 intstat[LP8788_NUM_INTREGS];
  191. u8 idno;
  192. u8 vbus;
  193. if (lp8727_read_bytes(pchg, LP8727_INT1, intstat, LP8788_NUM_INTREGS)) {
  194. dev_err(pchg->dev, "can not read INT registers\n");
  195. return;
  196. }
  197. idno = intstat[0] & LP8727_IDNO;
  198. vbus = intstat[0] & LP8727_VBUS;
  199. lp8727_id_detection(pchg, idno, vbus);
  200. lp8727_enable_chgdet(pchg);
  201. power_supply_changed(pchg->psy->ac);
  202. power_supply_changed(pchg->psy->usb);
  203. power_supply_changed(pchg->psy->batt);
  204. }
  205. static irqreturn_t lp8727_isr_func(int irq, void *ptr)
  206. {
  207. struct lp8727_chg *pchg = ptr;
  208. schedule_delayed_work(&pchg->work, pchg->debounce_jiffies);
  209. return IRQ_HANDLED;
  210. }
  211. static int lp8727_setup_irq(struct lp8727_chg *pchg)
  212. {
  213. int ret;
  214. int irq = pchg->client->irq;
  215. unsigned delay_msec = pchg->pdata ? pchg->pdata->debounce_msec :
  216. DEFAULT_DEBOUNCE_MSEC;
  217. INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
  218. if (irq <= 0) {
  219. dev_warn(pchg->dev, "invalid irq number: %d\n", irq);
  220. return 0;
  221. }
  222. ret = request_threaded_irq(irq, NULL, lp8727_isr_func,
  223. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  224. "lp8727_irq", pchg);
  225. if (ret)
  226. return ret;
  227. pchg->irq = irq;
  228. pchg->debounce_jiffies = msecs_to_jiffies(delay_msec);
  229. return 0;
  230. }
  231. static void lp8727_release_irq(struct lp8727_chg *pchg)
  232. {
  233. cancel_delayed_work_sync(&pchg->work);
  234. if (pchg->irq)
  235. free_irq(pchg->irq, pchg);
  236. }
  237. static enum power_supply_property lp8727_charger_prop[] = {
  238. POWER_SUPPLY_PROP_ONLINE,
  239. };
  240. static enum power_supply_property lp8727_battery_prop[] = {
  241. POWER_SUPPLY_PROP_STATUS,
  242. POWER_SUPPLY_PROP_HEALTH,
  243. POWER_SUPPLY_PROP_PRESENT,
  244. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  245. POWER_SUPPLY_PROP_CAPACITY,
  246. POWER_SUPPLY_PROP_TEMP,
  247. };
  248. static char *battery_supplied_to[] = {
  249. "main_batt",
  250. };
  251. static int lp8727_charger_get_property(struct power_supply *psy,
  252. enum power_supply_property psp,
  253. union power_supply_propval *val)
  254. {
  255. struct lp8727_chg *pchg = dev_get_drvdata(psy->dev.parent);
  256. if (psp != POWER_SUPPLY_PROP_ONLINE)
  257. return -EINVAL;
  258. val->intval = lp8727_is_charger_attached(psy->desc->name, pchg->devid);
  259. return 0;
  260. }
  261. static bool lp8727_is_high_temperature(enum lp8727_die_temp temp)
  262. {
  263. switch (temp) {
  264. case LP8788_TEMP_95C:
  265. case LP8788_TEMP_115C:
  266. case LP8788_TEMP_135C:
  267. return true;
  268. default:
  269. return false;
  270. }
  271. }
  272. static int lp8727_battery_get_property(struct power_supply *psy,
  273. enum power_supply_property psp,
  274. union power_supply_propval *val)
  275. {
  276. struct lp8727_chg *pchg = dev_get_drvdata(psy->dev.parent);
  277. struct lp8727_platform_data *pdata = pchg->pdata;
  278. enum lp8727_die_temp temp;
  279. u8 read;
  280. switch (psp) {
  281. case POWER_SUPPLY_PROP_STATUS:
  282. if (!lp8727_is_charger_attached(psy->desc->name, pchg->devid)) {
  283. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  284. return 0;
  285. }
  286. lp8727_read_byte(pchg, LP8727_STATUS1, &read);
  287. val->intval = (read & LP8727_CHGSTAT) == LP8727_STAT_EOC ?
  288. POWER_SUPPLY_STATUS_FULL :
  289. POWER_SUPPLY_STATUS_CHARGING;
  290. break;
  291. case POWER_SUPPLY_PROP_HEALTH:
  292. lp8727_read_byte(pchg, LP8727_STATUS2, &read);
  293. temp = (read & LP8727_TEMP_STAT) >> LP8727_TEMP_SHIFT;
  294. val->intval = lp8727_is_high_temperature(temp) ?
  295. POWER_SUPPLY_HEALTH_OVERHEAT :
  296. POWER_SUPPLY_HEALTH_GOOD;
  297. break;
  298. case POWER_SUPPLY_PROP_PRESENT:
  299. if (!pdata)
  300. return -EINVAL;
  301. if (pdata->get_batt_present)
  302. val->intval = pdata->get_batt_present();
  303. break;
  304. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  305. if (!pdata)
  306. return -EINVAL;
  307. if (pdata->get_batt_level)
  308. val->intval = pdata->get_batt_level();
  309. break;
  310. case POWER_SUPPLY_PROP_CAPACITY:
  311. if (!pdata)
  312. return -EINVAL;
  313. if (pdata->get_batt_capacity)
  314. val->intval = pdata->get_batt_capacity();
  315. break;
  316. case POWER_SUPPLY_PROP_TEMP:
  317. if (!pdata)
  318. return -EINVAL;
  319. if (pdata->get_batt_temp)
  320. val->intval = pdata->get_batt_temp();
  321. break;
  322. default:
  323. break;
  324. }
  325. return 0;
  326. }
  327. static void lp8727_charger_changed(struct power_supply *psy)
  328. {
  329. struct lp8727_chg *pchg = dev_get_drvdata(psy->dev.parent);
  330. u8 eoc_level;
  331. u8 ichg;
  332. u8 val;
  333. /* skip if no charger exists */
  334. if (!lp8727_is_charger_attached(psy->desc->name, pchg->devid))
  335. return;
  336. /* update charging parameters */
  337. if (pchg->chg_param) {
  338. eoc_level = pchg->chg_param->eoc_level;
  339. ichg = pchg->chg_param->ichg;
  340. val = (ichg << LP8727_ICHG_SHIFT) | eoc_level;
  341. lp8727_write_byte(pchg, LP8727_CHGCTRL2, val);
  342. }
  343. }
  344. static const struct power_supply_desc lp8727_ac_desc = {
  345. .name = "ac",
  346. .type = POWER_SUPPLY_TYPE_MAINS,
  347. .properties = lp8727_charger_prop,
  348. .num_properties = ARRAY_SIZE(lp8727_charger_prop),
  349. .get_property = lp8727_charger_get_property,
  350. };
  351. static const struct power_supply_desc lp8727_usb_desc = {
  352. .name = "usb",
  353. .type = POWER_SUPPLY_TYPE_USB,
  354. .properties = lp8727_charger_prop,
  355. .num_properties = ARRAY_SIZE(lp8727_charger_prop),
  356. .get_property = lp8727_charger_get_property,
  357. };
  358. static const struct power_supply_desc lp8727_batt_desc = {
  359. .name = "main_batt",
  360. .type = POWER_SUPPLY_TYPE_BATTERY,
  361. .properties = lp8727_battery_prop,
  362. .num_properties = ARRAY_SIZE(lp8727_battery_prop),
  363. .get_property = lp8727_battery_get_property,
  364. .external_power_changed = lp8727_charger_changed,
  365. };
  366. static int lp8727_register_psy(struct lp8727_chg *pchg)
  367. {
  368. struct power_supply_config psy_cfg = {}; /* Only for ac and usb */
  369. struct lp8727_psy *psy;
  370. psy = devm_kzalloc(pchg->dev, sizeof(*psy), GFP_KERNEL);
  371. if (!psy)
  372. return -ENOMEM;
  373. pchg->psy = psy;
  374. psy_cfg.supplied_to = battery_supplied_to;
  375. psy_cfg.num_supplicants = ARRAY_SIZE(battery_supplied_to);
  376. psy->ac = power_supply_register(pchg->dev, &lp8727_ac_desc, &psy_cfg);
  377. if (IS_ERR(psy->ac))
  378. goto err_psy_ac;
  379. psy->usb = power_supply_register(pchg->dev, &lp8727_usb_desc,
  380. &psy_cfg);
  381. if (IS_ERR(psy->usb))
  382. goto err_psy_usb;
  383. psy->batt = power_supply_register(pchg->dev, &lp8727_batt_desc, NULL);
  384. if (IS_ERR(psy->batt))
  385. goto err_psy_batt;
  386. return 0;
  387. err_psy_batt:
  388. power_supply_unregister(psy->usb);
  389. err_psy_usb:
  390. power_supply_unregister(psy->ac);
  391. err_psy_ac:
  392. return -EPERM;
  393. }
  394. static void lp8727_unregister_psy(struct lp8727_chg *pchg)
  395. {
  396. struct lp8727_psy *psy = pchg->psy;
  397. if (!psy)
  398. return;
  399. power_supply_unregister(psy->ac);
  400. power_supply_unregister(psy->usb);
  401. power_supply_unregister(psy->batt);
  402. }
  403. #ifdef CONFIG_OF
  404. static struct lp8727_chg_param
  405. *lp8727_parse_charge_pdata(struct device *dev, struct device_node *np)
  406. {
  407. struct lp8727_chg_param *param;
  408. param = devm_kzalloc(dev, sizeof(*param), GFP_KERNEL);
  409. if (!param)
  410. goto out;
  411. of_property_read_u8(np, "eoc-level", (u8 *)&param->eoc_level);
  412. of_property_read_u8(np, "charging-current", (u8 *)&param->ichg);
  413. out:
  414. return param;
  415. }
  416. static struct lp8727_platform_data *lp8727_parse_dt(struct device *dev)
  417. {
  418. struct device_node *np = dev->of_node;
  419. struct device_node *child;
  420. struct lp8727_platform_data *pdata;
  421. const char *type;
  422. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  423. if (!pdata)
  424. return ERR_PTR(-ENOMEM);
  425. of_property_read_u32(np, "debounce-ms", &pdata->debounce_msec);
  426. /* If charging parameter is not defined, just skip parsing the dt */
  427. if (of_get_child_count(np) == 0)
  428. return pdata;
  429. for_each_child_of_node(np, child) {
  430. of_property_read_string(child, "charger-type", &type);
  431. if (!strcmp(type, "ac"))
  432. pdata->ac = lp8727_parse_charge_pdata(dev, child);
  433. if (!strcmp(type, "usb"))
  434. pdata->usb = lp8727_parse_charge_pdata(dev, child);
  435. }
  436. return pdata;
  437. }
  438. #else
  439. static struct lp8727_platform_data *lp8727_parse_dt(struct device *dev)
  440. {
  441. return NULL;
  442. }
  443. #endif
  444. static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
  445. {
  446. struct lp8727_chg *pchg;
  447. struct lp8727_platform_data *pdata;
  448. int ret;
  449. if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
  450. return -EIO;
  451. if (cl->dev.of_node) {
  452. pdata = lp8727_parse_dt(&cl->dev);
  453. if (IS_ERR(pdata))
  454. return PTR_ERR(pdata);
  455. } else {
  456. pdata = dev_get_platdata(&cl->dev);
  457. }
  458. pchg = devm_kzalloc(&cl->dev, sizeof(*pchg), GFP_KERNEL);
  459. if (!pchg)
  460. return -ENOMEM;
  461. pchg->client = cl;
  462. pchg->dev = &cl->dev;
  463. pchg->pdata = pdata;
  464. i2c_set_clientdata(cl, pchg);
  465. mutex_init(&pchg->xfer_lock);
  466. ret = lp8727_init_device(pchg);
  467. if (ret) {
  468. dev_err(pchg->dev, "i2c communication err: %d", ret);
  469. return ret;
  470. }
  471. ret = lp8727_register_psy(pchg);
  472. if (ret) {
  473. dev_err(pchg->dev, "power supplies register err: %d", ret);
  474. return ret;
  475. }
  476. ret = lp8727_setup_irq(pchg);
  477. if (ret) {
  478. dev_err(pchg->dev, "irq handler err: %d", ret);
  479. lp8727_unregister_psy(pchg);
  480. return ret;
  481. }
  482. return 0;
  483. }
  484. static int lp8727_remove(struct i2c_client *cl)
  485. {
  486. struct lp8727_chg *pchg = i2c_get_clientdata(cl);
  487. lp8727_release_irq(pchg);
  488. lp8727_unregister_psy(pchg);
  489. return 0;
  490. }
  491. static const struct of_device_id lp8727_dt_ids[] = {
  492. { .compatible = "ti,lp8727", },
  493. { }
  494. };
  495. MODULE_DEVICE_TABLE(of, lp8727_dt_ids);
  496. static const struct i2c_device_id lp8727_ids[] = {
  497. {"lp8727", 0},
  498. { }
  499. };
  500. MODULE_DEVICE_TABLE(i2c, lp8727_ids);
  501. static struct i2c_driver lp8727_driver = {
  502. .driver = {
  503. .name = "lp8727",
  504. .of_match_table = of_match_ptr(lp8727_dt_ids),
  505. },
  506. .probe = lp8727_probe,
  507. .remove = lp8727_remove,
  508. .id_table = lp8727_ids,
  509. };
  510. module_i2c_driver(lp8727_driver);
  511. MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
  512. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>, Daniel Jeong <daniel.jeong@ti.com>");
  513. MODULE_LICENSE("GPL");