lp8727_charger.c 13 KB

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