lp8727_charger.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/lp8727.h>
  18. #define DEBOUNCE_MSEC 270
  19. /* Registers */
  20. #define CTRL1 0x1
  21. #define CTRL2 0x2
  22. #define SWCTRL 0x3
  23. #define INT1 0x4
  24. #define INT2 0x5
  25. #define STATUS1 0x6
  26. #define STATUS2 0x7
  27. #define CHGCTRL2 0x9
  28. /* CTRL1 register */
  29. #define CP_EN (1 << 0)
  30. #define ADC_EN (1 << 1)
  31. #define ID200_EN (1 << 4)
  32. /* CTRL2 register */
  33. #define CHGDET_EN (1 << 1)
  34. #define INT_EN (1 << 6)
  35. /* SWCTRL register */
  36. #define SW_DM1_DM (0x0 << 0)
  37. #define SW_DM1_U1 (0x1 << 0)
  38. #define SW_DM1_HiZ (0x7 << 0)
  39. #define SW_DP2_DP (0x0 << 3)
  40. #define SW_DP2_U2 (0x1 << 3)
  41. #define SW_DP2_HiZ (0x7 << 3)
  42. /* INT1 register */
  43. #define IDNO (0xF << 0)
  44. #define VBUS (1 << 4)
  45. /* STATUS1 register */
  46. #define CHGSTAT (3 << 4)
  47. #define CHPORT (1 << 6)
  48. #define DCPORT (1 << 7)
  49. /* STATUS2 register */
  50. #define TEMP_STAT (3 << 5)
  51. enum lp8727_dev_id {
  52. ID_NONE,
  53. ID_TA,
  54. ID_DEDICATED_CHG,
  55. ID_USB_CHG,
  56. ID_USB_DS,
  57. ID_MAX,
  58. };
  59. enum lp8727_chg_stat {
  60. PRECHG,
  61. CC,
  62. CV,
  63. EOC,
  64. };
  65. struct lp8727_psy {
  66. struct power_supply ac;
  67. struct power_supply usb;
  68. struct power_supply batt;
  69. };
  70. struct lp8727_chg {
  71. struct device *dev;
  72. struct i2c_client *client;
  73. struct mutex xfer_lock;
  74. struct delayed_work work;
  75. struct workqueue_struct *irqthread;
  76. struct lp8727_platform_data *pdata;
  77. struct lp8727_psy *psy;
  78. struct lp8727_chg_param *chg_parm;
  79. enum lp8727_dev_id devid;
  80. };
  81. static int lp8727_read_bytes(struct lp8727_chg *pchg, u8 reg, u8 *data, u8 len)
  82. {
  83. s32 ret;
  84. mutex_lock(&pchg->xfer_lock);
  85. ret = i2c_smbus_read_i2c_block_data(pchg->client, reg, len, data);
  86. mutex_unlock(&pchg->xfer_lock);
  87. return (ret != len) ? -EIO : 0;
  88. }
  89. static inline int lp8727_read_byte(struct lp8727_chg *pchg, u8 reg, u8 *data)
  90. {
  91. return lp8727_read_bytes(pchg, reg, data, 1);
  92. }
  93. static int lp8727_write_byte(struct lp8727_chg *pchg, u8 reg, u8 data)
  94. {
  95. int ret;
  96. mutex_lock(&pchg->xfer_lock);
  97. ret = i2c_smbus_write_byte_data(pchg->client, reg, data);
  98. mutex_unlock(&pchg->xfer_lock);
  99. return ret;
  100. }
  101. static int lp8727_is_charger_attached(const char *name, int id)
  102. {
  103. if (name) {
  104. if (!strcmp(name, "ac"))
  105. return (id == ID_TA || id == ID_DEDICATED_CHG) ? 1 : 0;
  106. else if (!strcmp(name, "usb"))
  107. return (id == ID_USB_CHG) ? 1 : 0;
  108. }
  109. return (id >= ID_TA && id <= ID_USB_CHG) ? 1 : 0;
  110. }
  111. static int lp8727_init_device(struct lp8727_chg *pchg)
  112. {
  113. u8 val;
  114. int ret;
  115. val = ID200_EN | ADC_EN | CP_EN;
  116. ret = lp8727_write_byte(pchg, CTRL1, val);
  117. if (ret)
  118. return ret;
  119. val = INT_EN | CHGDET_EN;
  120. ret = lp8727_write_byte(pchg, CTRL2, val);
  121. if (ret)
  122. return ret;
  123. return 0;
  124. }
  125. static int lp8727_is_dedicated_charger(struct lp8727_chg *pchg)
  126. {
  127. u8 val;
  128. lp8727_read_byte(pchg, STATUS1, &val);
  129. return val & DCPORT;
  130. }
  131. static int lp8727_is_usb_charger(struct lp8727_chg *pchg)
  132. {
  133. u8 val;
  134. lp8727_read_byte(pchg, STATUS1, &val);
  135. return val & CHPORT;
  136. }
  137. static void lp8727_ctrl_switch(struct lp8727_chg *pchg, u8 sw)
  138. {
  139. lp8727_write_byte(pchg, SWCTRL, sw);
  140. }
  141. static void lp8727_id_detection(struct lp8727_chg *pchg, u8 id, int vbusin)
  142. {
  143. u8 devid = ID_NONE;
  144. u8 swctrl = SW_DM1_HiZ | SW_DP2_HiZ;
  145. switch (id) {
  146. case 0x5:
  147. devid = ID_TA;
  148. pchg->chg_parm = &pchg->pdata->ac;
  149. break;
  150. case 0xB:
  151. if (lp8727_is_dedicated_charger(pchg)) {
  152. pchg->chg_parm = &pchg->pdata->ac;
  153. devid = ID_DEDICATED_CHG;
  154. } else if (lp8727_is_usb_charger(pchg)) {
  155. pchg->chg_parm = &pchg->pdata->usb;
  156. devid = ID_USB_CHG;
  157. swctrl = SW_DM1_DM | SW_DP2_DP;
  158. } else if (vbusin) {
  159. devid = ID_USB_DS;
  160. swctrl = SW_DM1_DM | SW_DP2_DP;
  161. }
  162. break;
  163. default:
  164. devid = ID_NONE;
  165. pchg->chg_parm = NULL;
  166. break;
  167. }
  168. pchg->devid = devid;
  169. lp8727_ctrl_switch(pchg, swctrl);
  170. }
  171. static void lp8727_enable_chgdet(struct lp8727_chg *pchg)
  172. {
  173. u8 val;
  174. lp8727_read_byte(pchg, CTRL2, &val);
  175. val |= CHGDET_EN;
  176. lp8727_write_byte(pchg, CTRL2, val);
  177. }
  178. static void lp8727_delayed_func(struct work_struct *_work)
  179. {
  180. u8 intstat[2], idno, vbus;
  181. struct lp8727_chg *pchg =
  182. container_of(_work, struct lp8727_chg, work.work);
  183. if (lp8727_read_bytes(pchg, INT1, intstat, 2)) {
  184. dev_err(pchg->dev, "can not read INT registers\n");
  185. return;
  186. }
  187. idno = intstat[0] & IDNO;
  188. vbus = intstat[0] & VBUS;
  189. lp8727_id_detection(pchg, idno, vbus);
  190. lp8727_enable_chgdet(pchg);
  191. power_supply_changed(&pchg->psy->ac);
  192. power_supply_changed(&pchg->psy->usb);
  193. power_supply_changed(&pchg->psy->batt);
  194. }
  195. static irqreturn_t lp8727_isr_func(int irq, void *ptr)
  196. {
  197. struct lp8727_chg *pchg = ptr;
  198. unsigned long delay = msecs_to_jiffies(DEBOUNCE_MSEC);
  199. queue_delayed_work(pchg->irqthread, &pchg->work, delay);
  200. return IRQ_HANDLED;
  201. }
  202. static int lp8727_intr_config(struct lp8727_chg *pchg)
  203. {
  204. INIT_DELAYED_WORK(&pchg->work, lp8727_delayed_func);
  205. pchg->irqthread = create_singlethread_workqueue("lp8727-irqthd");
  206. if (!pchg->irqthread) {
  207. dev_err(pchg->dev, "can not create thread for lp8727\n");
  208. return -ENOMEM;
  209. }
  210. return request_threaded_irq(pchg->client->irq,
  211. NULL,
  212. lp8727_isr_func,
  213. IRQF_TRIGGER_FALLING,
  214. "lp8727_irq",
  215. pchg);
  216. }
  217. static enum power_supply_property lp8727_charger_prop[] = {
  218. POWER_SUPPLY_PROP_ONLINE,
  219. };
  220. static enum power_supply_property lp8727_battery_prop[] = {
  221. POWER_SUPPLY_PROP_STATUS,
  222. POWER_SUPPLY_PROP_HEALTH,
  223. POWER_SUPPLY_PROP_PRESENT,
  224. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  225. POWER_SUPPLY_PROP_CAPACITY,
  226. POWER_SUPPLY_PROP_TEMP,
  227. };
  228. static char *battery_supplied_to[] = {
  229. "main_batt",
  230. };
  231. static int lp8727_charger_get_property(struct power_supply *psy,
  232. enum power_supply_property psp,
  233. union power_supply_propval *val)
  234. {
  235. struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
  236. if (psp == POWER_SUPPLY_PROP_ONLINE)
  237. val->intval = lp8727_is_charger_attached(psy->name,
  238. pchg->devid);
  239. return 0;
  240. }
  241. static int lp8727_battery_get_property(struct power_supply *psy,
  242. enum power_supply_property psp,
  243. union power_supply_propval *val)
  244. {
  245. struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
  246. u8 read;
  247. switch (psp) {
  248. case POWER_SUPPLY_PROP_STATUS:
  249. if (lp8727_is_charger_attached(psy->name, pchg->devid)) {
  250. lp8727_read_byte(pchg, STATUS1, &read);
  251. if (((read & CHGSTAT) >> 4) == EOC)
  252. val->intval = POWER_SUPPLY_STATUS_FULL;
  253. else
  254. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  255. } else {
  256. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  257. }
  258. break;
  259. case POWER_SUPPLY_PROP_HEALTH:
  260. lp8727_read_byte(pchg, STATUS2, &read);
  261. read = (read & TEMP_STAT) >> 5;
  262. if (read >= 0x1 && read <= 0x3)
  263. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  264. else
  265. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  266. break;
  267. case POWER_SUPPLY_PROP_PRESENT:
  268. if (pchg->pdata->get_batt_present)
  269. val->intval = pchg->pdata->get_batt_present();
  270. break;
  271. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  272. if (pchg->pdata->get_batt_level)
  273. val->intval = pchg->pdata->get_batt_level();
  274. break;
  275. case POWER_SUPPLY_PROP_CAPACITY:
  276. if (pchg->pdata->get_batt_capacity)
  277. val->intval = pchg->pdata->get_batt_capacity();
  278. break;
  279. case POWER_SUPPLY_PROP_TEMP:
  280. if (pchg->pdata->get_batt_temp)
  281. val->intval = pchg->pdata->get_batt_temp();
  282. break;
  283. default:
  284. break;
  285. }
  286. return 0;
  287. }
  288. static void lp8727_charger_changed(struct power_supply *psy)
  289. {
  290. struct lp8727_chg *pchg = dev_get_drvdata(psy->dev->parent);
  291. u8 val;
  292. u8 eoc_level, ichg;
  293. if (lp8727_is_charger_attached(psy->name, pchg->devid)) {
  294. if (pchg->chg_parm) {
  295. eoc_level = pchg->chg_parm->eoc_level;
  296. ichg = pchg->chg_parm->ichg;
  297. val = (ichg << 4) | eoc_level;
  298. lp8727_write_byte(pchg, CHGCTRL2, val);
  299. }
  300. }
  301. }
  302. static int lp8727_register_psy(struct lp8727_chg *pchg)
  303. {
  304. struct lp8727_psy *psy;
  305. psy = kzalloc(sizeof(*psy), GFP_KERNEL);
  306. if (!psy)
  307. goto err_mem;
  308. pchg->psy = psy;
  309. psy->ac.name = "ac";
  310. psy->ac.type = POWER_SUPPLY_TYPE_MAINS;
  311. psy->ac.properties = lp8727_charger_prop;
  312. psy->ac.num_properties = ARRAY_SIZE(lp8727_charger_prop);
  313. psy->ac.get_property = lp8727_charger_get_property;
  314. psy->ac.supplied_to = battery_supplied_to;
  315. psy->ac.num_supplicants = ARRAY_SIZE(battery_supplied_to);
  316. if (power_supply_register(pchg->dev, &psy->ac))
  317. goto err_psy;
  318. psy->usb.name = "usb";
  319. psy->usb.type = POWER_SUPPLY_TYPE_USB;
  320. psy->usb.properties = lp8727_charger_prop;
  321. psy->usb.num_properties = ARRAY_SIZE(lp8727_charger_prop);
  322. psy->usb.get_property = lp8727_charger_get_property;
  323. psy->usb.supplied_to = battery_supplied_to;
  324. psy->usb.num_supplicants = ARRAY_SIZE(battery_supplied_to);
  325. if (power_supply_register(pchg->dev, &psy->usb))
  326. goto err_psy;
  327. psy->batt.name = "main_batt";
  328. psy->batt.type = POWER_SUPPLY_TYPE_BATTERY;
  329. psy->batt.properties = lp8727_battery_prop;
  330. psy->batt.num_properties = ARRAY_SIZE(lp8727_battery_prop);
  331. psy->batt.get_property = lp8727_battery_get_property;
  332. psy->batt.external_power_changed = lp8727_charger_changed;
  333. if (power_supply_register(pchg->dev, &psy->batt))
  334. goto err_psy;
  335. return 0;
  336. err_mem:
  337. return -ENOMEM;
  338. err_psy:
  339. kfree(psy);
  340. return -EPERM;
  341. }
  342. static void lp8727_unregister_psy(struct lp8727_chg *pchg)
  343. {
  344. struct lp8727_psy *psy = pchg->psy;
  345. if (!psy)
  346. return;
  347. power_supply_unregister(&psy->ac);
  348. power_supply_unregister(&psy->usb);
  349. power_supply_unregister(&psy->batt);
  350. kfree(psy);
  351. }
  352. static int lp8727_probe(struct i2c_client *cl, const struct i2c_device_id *id)
  353. {
  354. struct lp8727_chg *pchg;
  355. int ret;
  356. if (!i2c_check_functionality(cl->adapter, I2C_FUNC_SMBUS_I2C_BLOCK))
  357. return -EIO;
  358. pchg = kzalloc(sizeof(*pchg), GFP_KERNEL);
  359. if (!pchg)
  360. return -ENOMEM;
  361. pchg->client = cl;
  362. pchg->dev = &cl->dev;
  363. pchg->pdata = cl->dev.platform_data;
  364. i2c_set_clientdata(cl, pchg);
  365. mutex_init(&pchg->xfer_lock);
  366. ret = lp8727_init_device(pchg);
  367. if (ret) {
  368. dev_err(pchg->dev, "i2c communication err: %d", ret);
  369. goto error;
  370. }
  371. ret = lp8727_intr_config(pchg);
  372. if (ret) {
  373. dev_err(pchg->dev, "irq handler err: %d", ret);
  374. goto error;
  375. }
  376. ret = lp8727_register_psy(pchg);
  377. if (ret) {
  378. dev_err(pchg->dev, "power supplies register err: %d", ret);
  379. goto error;
  380. }
  381. return 0;
  382. error:
  383. kfree(pchg);
  384. return ret;
  385. }
  386. static int __devexit lp8727_remove(struct i2c_client *cl)
  387. {
  388. struct lp8727_chg *pchg = i2c_get_clientdata(cl);
  389. lp8727_unregister_psy(pchg);
  390. free_irq(pchg->client->irq, pchg);
  391. flush_workqueue(pchg->irqthread);
  392. destroy_workqueue(pchg->irqthread);
  393. kfree(pchg);
  394. return 0;
  395. }
  396. static const struct i2c_device_id lp8727_ids[] = {
  397. {"lp8727", 0},
  398. { }
  399. };
  400. MODULE_DEVICE_TABLE(i2c, lp8727_ids);
  401. static struct i2c_driver lp8727_driver = {
  402. .driver = {
  403. .name = "lp8727",
  404. },
  405. .probe = lp8727_probe,
  406. .remove = __devexit_p(lp8727_remove),
  407. .id_table = lp8727_ids,
  408. };
  409. module_i2c_driver(lp8727_driver);
  410. MODULE_DESCRIPTION("TI/National Semiconductor LP8727 charger driver");
  411. MODULE_AUTHOR("Woogyom Kim <milo.kim@ti.com>, "
  412. "Daniel Jeong <daniel.jeong@ti.com>");
  413. MODULE_LICENSE("GPL");