lp8727_charger.c 13 KB

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