lp8727_charger.c 12 KB

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