88pm860x_charger.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * Battery driver for Marvell 88PM860x PMIC
  3. *
  4. * Copyright (c) 2012 Marvell International Ltd.
  5. * Author: Jett Zhou <jtzhou@marvell.com>
  6. * Haojian Zhuang <haojian.zhuang@marvell.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/slab.h>
  16. #include <linux/power_supply.h>
  17. #include <linux/mfd/88pm860x.h>
  18. #include <linux/delay.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/div64.h>
  21. /* bit definitions of Status Query Interface 2 */
  22. #define STATUS2_CHG (1 << 2)
  23. /* bit definitions of Reset Out Register */
  24. #define RESET_SW_PD (1 << 7)
  25. /* bit definitions of PreReg 1 */
  26. #define PREREG1_90MA (0x0)
  27. #define PREREG1_180MA (0x1)
  28. #define PREREG1_450MA (0x4)
  29. #define PREREG1_540MA (0x5)
  30. #define PREREG1_1350MA (0xE)
  31. #define PREREG1_VSYS_4_5V (3 << 4)
  32. /* bit definitions of Charger Control 1 Register */
  33. #define CC1_MODE_OFF (0)
  34. #define CC1_MODE_PRECHARGE (1)
  35. #define CC1_MODE_FASTCHARGE (2)
  36. #define CC1_MODE_PULSECHARGE (3)
  37. #define CC1_ITERM_20MA (0 << 2)
  38. #define CC1_ITERM_60MA (2 << 2)
  39. #define CC1_VFCHG_4_2V (9 << 4)
  40. /* bit definitions of Charger Control 2 Register */
  41. #define CC2_ICHG_100MA (0x1)
  42. #define CC2_ICHG_500MA (0x9)
  43. #define CC2_ICHG_1000MA (0x13)
  44. /* bit definitions of Charger Control 3 Register */
  45. #define CC3_180MIN_TIMEOUT (0x6 << 4)
  46. #define CC3_270MIN_TIMEOUT (0x7 << 4)
  47. #define CC3_360MIN_TIMEOUT (0xA << 4)
  48. #define CC3_DISABLE_TIMEOUT (0xF << 4)
  49. /* bit definitions of Charger Control 4 Register */
  50. #define CC4_IPRE_40MA (7)
  51. #define CC4_VPCHG_3_2V (3 << 4)
  52. #define CC4_IFCHG_MON_EN (1 << 6)
  53. #define CC4_BTEMP_MON_EN (1 << 7)
  54. /* bit definitions of Charger Control 6 Register */
  55. #define CC6_BAT_OV_EN (1 << 2)
  56. #define CC6_BAT_UV_EN (1 << 3)
  57. #define CC6_UV_VBAT_SET (0x3 << 6) /* 2.8v */
  58. /* bit definitions of Charger Control 7 Register */
  59. #define CC7_BAT_REM_EN (1 << 3)
  60. #define CC7_IFSM_EN (1 << 7)
  61. /* bit definitions of Measurement Enable 1 Register */
  62. #define MEAS1_VBAT (1 << 0)
  63. /* bit definitions of Measurement Enable 3 Register */
  64. #define MEAS3_IBAT_EN (1 << 0)
  65. #define MEAS3_CC_EN (1 << 2)
  66. #define FSM_INIT 0
  67. #define FSM_DISCHARGE 1
  68. #define FSM_PRECHARGE 2
  69. #define FSM_FASTCHARGE 3
  70. #define PRECHARGE_THRESHOLD 3100
  71. #define POWEROFF_THRESHOLD 3400
  72. #define CHARGE_THRESHOLD 4000
  73. #define DISCHARGE_THRESHOLD 4180
  74. /* over-temperature on PM8606 setting */
  75. #define OVER_TEMP_FLAG (1 << 6)
  76. #define OVTEMP_AUTORECOVER (1 << 3)
  77. /* over-voltage protect on vchg setting mv */
  78. #define VCHG_NORMAL_LOW 4200
  79. #define VCHG_NORMAL_CHECK 5800
  80. #define VCHG_NORMAL_HIGH 6000
  81. #define VCHG_OVP_LOW 5500
  82. struct pm860x_charger_info {
  83. struct pm860x_chip *chip;
  84. struct i2c_client *i2c;
  85. struct i2c_client *i2c_8606;
  86. struct device *dev;
  87. struct power_supply *usb;
  88. struct mutex lock;
  89. int irq_nums;
  90. int irq[7];
  91. unsigned state:3; /* fsm state */
  92. unsigned online:1; /* usb charger */
  93. unsigned present:1; /* battery present */
  94. unsigned allowed:1;
  95. };
  96. static char *pm860x_supplied_to[] = {
  97. "battery-monitor",
  98. };
  99. static int measure_vchg(struct pm860x_charger_info *info, int *data)
  100. {
  101. unsigned char buf[2];
  102. int ret = 0;
  103. ret = pm860x_bulk_read(info->i2c, PM8607_VCHG_MEAS1, 2, buf);
  104. if (ret < 0)
  105. return ret;
  106. *data = ((buf[0] & 0xff) << 4) | (buf[1] & 0x0f);
  107. /* V_BATT_MEAS(mV) = value * 5 * 1.8 * 1000 / (2^12) */
  108. *data = ((*data & 0xfff) * 9 * 125) >> 9;
  109. dev_dbg(info->dev, "%s, vchg: %d mv\n", __func__, *data);
  110. return ret;
  111. }
  112. static void set_vchg_threshold(struct pm860x_charger_info *info,
  113. int min, int max)
  114. {
  115. int data;
  116. /* (tmp << 8) * / 5 / 1800 */
  117. if (min <= 0)
  118. data = 0;
  119. else
  120. data = (min << 5) / 1125;
  121. pm860x_reg_write(info->i2c, PM8607_VCHG_LOWTH, data);
  122. dev_dbg(info->dev, "VCHG_LOWTH:%dmv, 0x%x\n", min, data);
  123. if (max <= 0)
  124. data = 0xff;
  125. else
  126. data = (max << 5) / 1125;
  127. pm860x_reg_write(info->i2c, PM8607_VCHG_HIGHTH, data);
  128. dev_dbg(info->dev, "VCHG_HIGHTH:%dmv, 0x%x\n", max, data);
  129. }
  130. static void set_vbatt_threshold(struct pm860x_charger_info *info,
  131. int min, int max)
  132. {
  133. int data;
  134. /* (tmp << 8) * 3 / 1800 */
  135. if (min <= 0)
  136. data = 0;
  137. else
  138. data = (min << 5) / 675;
  139. pm860x_reg_write(info->i2c, PM8607_VBAT_LOWTH, data);
  140. dev_dbg(info->dev, "VBAT Min:%dmv, LOWTH:0x%x\n", min, data);
  141. if (max <= 0)
  142. data = 0xff;
  143. else
  144. data = (max << 5) / 675;
  145. pm860x_reg_write(info->i2c, PM8607_VBAT_HIGHTH, data);
  146. dev_dbg(info->dev, "VBAT Max:%dmv, HIGHTH:0x%x\n", max, data);
  147. return;
  148. }
  149. static int start_precharge(struct pm860x_charger_info *info)
  150. {
  151. int ret;
  152. dev_dbg(info->dev, "Start Pre-charging!\n");
  153. set_vbatt_threshold(info, 0, 0);
  154. ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA,
  155. PREREG1_1350MA | PREREG1_VSYS_4_5V);
  156. if (ret < 0)
  157. goto out;
  158. /* stop charging */
  159. ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
  160. CC1_MODE_OFF);
  161. if (ret < 0)
  162. goto out;
  163. /* set 270 minutes timeout */
  164. ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4),
  165. CC3_270MIN_TIMEOUT);
  166. if (ret < 0)
  167. goto out;
  168. /* set precharge current, termination voltage, IBAT & TBAT monitor */
  169. ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL4,
  170. CC4_IPRE_40MA | CC4_VPCHG_3_2V |
  171. CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN);
  172. if (ret < 0)
  173. goto out;
  174. ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7,
  175. CC7_BAT_REM_EN | CC7_IFSM_EN,
  176. CC7_BAT_REM_EN | CC7_IFSM_EN);
  177. if (ret < 0)
  178. goto out;
  179. /* trigger precharge */
  180. ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
  181. CC1_MODE_PRECHARGE);
  182. out:
  183. return ret;
  184. }
  185. static int start_fastcharge(struct pm860x_charger_info *info)
  186. {
  187. int ret;
  188. dev_dbg(info->dev, "Start Fast-charging!\n");
  189. /* set fastcharge termination current & voltage, disable charging */
  190. ret = pm860x_reg_write(info->i2c, PM8607_CHG_CTRL1,
  191. CC1_MODE_OFF | CC1_ITERM_60MA |
  192. CC1_VFCHG_4_2V);
  193. if (ret < 0)
  194. goto out;
  195. ret = pm860x_reg_write(info->i2c_8606, PM8606_PREREGULATORA,
  196. PREREG1_540MA | PREREG1_VSYS_4_5V);
  197. if (ret < 0)
  198. goto out;
  199. ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL2, 0x1f,
  200. CC2_ICHG_500MA);
  201. if (ret < 0)
  202. goto out;
  203. /* set 270 minutes timeout */
  204. ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL3, (0xf << 4),
  205. CC3_270MIN_TIMEOUT);
  206. if (ret < 0)
  207. goto out;
  208. /* set IBAT & TBAT monitor */
  209. ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL4,
  210. CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN,
  211. CC4_IFCHG_MON_EN | CC4_BTEMP_MON_EN);
  212. if (ret < 0)
  213. goto out;
  214. ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL6,
  215. CC6_BAT_OV_EN | CC6_BAT_UV_EN |
  216. CC6_UV_VBAT_SET,
  217. CC6_BAT_OV_EN | CC6_BAT_UV_EN |
  218. CC6_UV_VBAT_SET);
  219. if (ret < 0)
  220. goto out;
  221. ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL7,
  222. CC7_BAT_REM_EN | CC7_IFSM_EN,
  223. CC7_BAT_REM_EN | CC7_IFSM_EN);
  224. if (ret < 0)
  225. goto out;
  226. /* launch fast-charge */
  227. ret = pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3,
  228. CC1_MODE_FASTCHARGE);
  229. /* vchg threshold setting */
  230. set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_NORMAL_HIGH);
  231. out:
  232. return ret;
  233. }
  234. static void stop_charge(struct pm860x_charger_info *info, int vbatt)
  235. {
  236. dev_dbg(info->dev, "Stop charging!\n");
  237. pm860x_set_bits(info->i2c, PM8607_CHG_CTRL1, 3, CC1_MODE_OFF);
  238. if (vbatt > CHARGE_THRESHOLD && info->online)
  239. set_vbatt_threshold(info, CHARGE_THRESHOLD, 0);
  240. }
  241. static void power_off_notification(struct pm860x_charger_info *info)
  242. {
  243. dev_dbg(info->dev, "Power-off notification!\n");
  244. }
  245. static int set_charging_fsm(struct pm860x_charger_info *info)
  246. {
  247. struct power_supply *psy;
  248. union power_supply_propval data;
  249. unsigned char fsm_state[][16] = { "init", "discharge", "precharge",
  250. "fastcharge",
  251. };
  252. int ret;
  253. int vbatt;
  254. psy = power_supply_get_by_name(pm860x_supplied_to[0]);
  255. if (!psy)
  256. return -EINVAL;
  257. ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW,
  258. &data);
  259. if (ret) {
  260. power_supply_put(psy);
  261. return ret;
  262. }
  263. vbatt = data.intval / 1000;
  264. ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_PRESENT, &data);
  265. if (ret) {
  266. power_supply_put(psy);
  267. return ret;
  268. }
  269. power_supply_put(psy);
  270. mutex_lock(&info->lock);
  271. info->present = data.intval;
  272. dev_dbg(info->dev, "Entering FSM:%s, Charger:%s, Battery:%s, "
  273. "Allowed:%d\n",
  274. &fsm_state[info->state][0],
  275. (info->online) ? "online" : "N/A",
  276. (info->present) ? "present" : "N/A", info->allowed);
  277. dev_dbg(info->dev, "set_charging_fsm:vbatt:%d(mV)\n", vbatt);
  278. switch (info->state) {
  279. case FSM_INIT:
  280. if (info->online && info->present && info->allowed) {
  281. if (vbatt < PRECHARGE_THRESHOLD) {
  282. info->state = FSM_PRECHARGE;
  283. start_precharge(info);
  284. } else if (vbatt > DISCHARGE_THRESHOLD) {
  285. info->state = FSM_DISCHARGE;
  286. stop_charge(info, vbatt);
  287. } else if (vbatt < DISCHARGE_THRESHOLD) {
  288. info->state = FSM_FASTCHARGE;
  289. start_fastcharge(info);
  290. }
  291. } else {
  292. if (vbatt < POWEROFF_THRESHOLD) {
  293. power_off_notification(info);
  294. } else {
  295. info->state = FSM_DISCHARGE;
  296. stop_charge(info, vbatt);
  297. }
  298. }
  299. break;
  300. case FSM_PRECHARGE:
  301. if (info->online && info->present && info->allowed) {
  302. if (vbatt > PRECHARGE_THRESHOLD) {
  303. info->state = FSM_FASTCHARGE;
  304. start_fastcharge(info);
  305. }
  306. } else {
  307. info->state = FSM_DISCHARGE;
  308. stop_charge(info, vbatt);
  309. }
  310. break;
  311. case FSM_FASTCHARGE:
  312. if (info->online && info->present && info->allowed) {
  313. if (vbatt < PRECHARGE_THRESHOLD) {
  314. info->state = FSM_PRECHARGE;
  315. start_precharge(info);
  316. }
  317. } else {
  318. info->state = FSM_DISCHARGE;
  319. stop_charge(info, vbatt);
  320. }
  321. break;
  322. case FSM_DISCHARGE:
  323. if (info->online && info->present && info->allowed) {
  324. if (vbatt < PRECHARGE_THRESHOLD) {
  325. info->state = FSM_PRECHARGE;
  326. start_precharge(info);
  327. } else if (vbatt < DISCHARGE_THRESHOLD) {
  328. info->state = FSM_FASTCHARGE;
  329. start_fastcharge(info);
  330. }
  331. } else {
  332. if (vbatt < POWEROFF_THRESHOLD)
  333. power_off_notification(info);
  334. else if (vbatt > CHARGE_THRESHOLD && info->online)
  335. set_vbatt_threshold(info, CHARGE_THRESHOLD, 0);
  336. }
  337. break;
  338. default:
  339. dev_warn(info->dev, "FSM meets wrong state:%d\n",
  340. info->state);
  341. break;
  342. }
  343. dev_dbg(info->dev,
  344. "Out FSM:%s, Charger:%s, Battery:%s, Allowed:%d\n",
  345. &fsm_state[info->state][0],
  346. (info->online) ? "online" : "N/A",
  347. (info->present) ? "present" : "N/A", info->allowed);
  348. mutex_unlock(&info->lock);
  349. return 0;
  350. }
  351. static irqreturn_t pm860x_charger_handler(int irq, void *data)
  352. {
  353. struct pm860x_charger_info *info = data;
  354. int ret;
  355. mutex_lock(&info->lock);
  356. ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
  357. if (ret < 0) {
  358. mutex_unlock(&info->lock);
  359. goto out;
  360. }
  361. if (ret & STATUS2_CHG) {
  362. info->online = 1;
  363. info->allowed = 1;
  364. } else {
  365. info->online = 0;
  366. info->allowed = 0;
  367. }
  368. mutex_unlock(&info->lock);
  369. dev_dbg(info->dev, "%s, Charger:%s, Allowed:%d\n", __func__,
  370. (info->online) ? "online" : "N/A", info->allowed);
  371. set_charging_fsm(info);
  372. power_supply_changed(info->usb);
  373. out:
  374. return IRQ_HANDLED;
  375. }
  376. static irqreturn_t pm860x_temp_handler(int irq, void *data)
  377. {
  378. struct power_supply *psy;
  379. struct pm860x_charger_info *info = data;
  380. union power_supply_propval temp;
  381. int value;
  382. int ret;
  383. psy = power_supply_get_by_name(pm860x_supplied_to[0]);
  384. if (!psy)
  385. goto out;
  386. ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &temp);
  387. if (ret)
  388. goto out;
  389. value = temp.intval / 10;
  390. mutex_lock(&info->lock);
  391. /* Temperature < -10 C or >40 C, Will not allow charge */
  392. if (value < -10 || value > 40)
  393. info->allowed = 0;
  394. else
  395. info->allowed = 1;
  396. dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
  397. mutex_unlock(&info->lock);
  398. set_charging_fsm(info);
  399. out:
  400. power_supply_put(psy);
  401. return IRQ_HANDLED;
  402. }
  403. static irqreturn_t pm860x_exception_handler(int irq, void *data)
  404. {
  405. struct pm860x_charger_info *info = data;
  406. mutex_lock(&info->lock);
  407. info->allowed = 0;
  408. mutex_unlock(&info->lock);
  409. dev_dbg(info->dev, "%s, irq: %d\n", __func__, irq);
  410. set_charging_fsm(info);
  411. return IRQ_HANDLED;
  412. }
  413. static irqreturn_t pm860x_done_handler(int irq, void *data)
  414. {
  415. struct pm860x_charger_info *info = data;
  416. struct power_supply *psy;
  417. union power_supply_propval val;
  418. int ret;
  419. int vbatt;
  420. mutex_lock(&info->lock);
  421. /* pre-charge done, will transimit to fast-charge stage */
  422. if (info->state == FSM_PRECHARGE) {
  423. info->allowed = 1;
  424. goto out;
  425. }
  426. /*
  427. * Fast charge done, delay to read
  428. * the correct status of CHG_DET.
  429. */
  430. mdelay(5);
  431. info->allowed = 0;
  432. psy = power_supply_get_by_name(pm860x_supplied_to[0]);
  433. if (!psy)
  434. goto out;
  435. ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW,
  436. &val);
  437. if (ret)
  438. goto out_psy_put;
  439. vbatt = val.intval / 1000;
  440. /*
  441. * CHG_DONE interrupt is faster than CHG_DET interrupt when
  442. * plug in/out usb, So we can not rely on info->online, we
  443. * need check pm8607 status register to check usb is online
  444. * or not, then we can decide it is real charge done
  445. * automatically or it is triggered by usb plug out;
  446. */
  447. ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
  448. if (ret < 0)
  449. goto out_psy_put;
  450. if (vbatt > CHARGE_THRESHOLD && ret & STATUS2_CHG)
  451. power_supply_set_property(psy, POWER_SUPPLY_PROP_CHARGE_FULL,
  452. &val);
  453. out_psy_put:
  454. power_supply_put(psy);
  455. out:
  456. mutex_unlock(&info->lock);
  457. dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
  458. set_charging_fsm(info);
  459. return IRQ_HANDLED;
  460. }
  461. static irqreturn_t pm860x_vbattery_handler(int irq, void *data)
  462. {
  463. struct pm860x_charger_info *info = data;
  464. mutex_lock(&info->lock);
  465. set_vbatt_threshold(info, 0, 0);
  466. if (info->present && info->online)
  467. info->allowed = 1;
  468. else
  469. info->allowed = 0;
  470. mutex_unlock(&info->lock);
  471. dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
  472. set_charging_fsm(info);
  473. return IRQ_HANDLED;
  474. }
  475. static irqreturn_t pm860x_vchg_handler(int irq, void *data)
  476. {
  477. struct pm860x_charger_info *info = data;
  478. int vchg = 0;
  479. if (info->present)
  480. goto out;
  481. measure_vchg(info, &vchg);
  482. mutex_lock(&info->lock);
  483. if (!info->online) {
  484. int status;
  485. /* check if over-temp on pm8606 or not */
  486. status = pm860x_reg_read(info->i2c_8606, PM8606_FLAGS);
  487. if (status & OVER_TEMP_FLAG) {
  488. /* clear over temp flag and set auto recover */
  489. pm860x_set_bits(info->i2c_8606, PM8606_FLAGS,
  490. OVER_TEMP_FLAG, OVER_TEMP_FLAG);
  491. pm860x_set_bits(info->i2c_8606,
  492. PM8606_VSYS,
  493. OVTEMP_AUTORECOVER,
  494. OVTEMP_AUTORECOVER);
  495. dev_dbg(info->dev,
  496. "%s, pm8606 over-temp occurred\n", __func__);
  497. }
  498. }
  499. if (vchg > VCHG_NORMAL_CHECK) {
  500. set_vchg_threshold(info, VCHG_OVP_LOW, 0);
  501. info->allowed = 0;
  502. dev_dbg(info->dev,
  503. "%s,pm8607 over-vchg occurred,vchg = %dmv\n",
  504. __func__, vchg);
  505. } else if (vchg < VCHG_OVP_LOW) {
  506. set_vchg_threshold(info, VCHG_NORMAL_LOW,
  507. VCHG_NORMAL_HIGH);
  508. info->allowed = 1;
  509. dev_dbg(info->dev,
  510. "%s,pm8607 over-vchg recover,vchg = %dmv\n",
  511. __func__, vchg);
  512. }
  513. mutex_unlock(&info->lock);
  514. dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
  515. set_charging_fsm(info);
  516. out:
  517. return IRQ_HANDLED;
  518. }
  519. static int pm860x_usb_get_prop(struct power_supply *psy,
  520. enum power_supply_property psp,
  521. union power_supply_propval *val)
  522. {
  523. struct pm860x_charger_info *info = power_supply_get_drvdata(psy);
  524. switch (psp) {
  525. case POWER_SUPPLY_PROP_STATUS:
  526. if (info->state == FSM_FASTCHARGE ||
  527. info->state == FSM_PRECHARGE)
  528. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  529. else
  530. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  531. break;
  532. case POWER_SUPPLY_PROP_ONLINE:
  533. val->intval = info->online;
  534. break;
  535. default:
  536. return -ENODEV;
  537. }
  538. return 0;
  539. }
  540. static enum power_supply_property pm860x_usb_props[] = {
  541. POWER_SUPPLY_PROP_STATUS,
  542. POWER_SUPPLY_PROP_ONLINE,
  543. };
  544. static int pm860x_init_charger(struct pm860x_charger_info *info)
  545. {
  546. int ret;
  547. ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
  548. if (ret < 0)
  549. return ret;
  550. mutex_lock(&info->lock);
  551. info->state = FSM_INIT;
  552. if (ret & STATUS2_CHG) {
  553. info->online = 1;
  554. info->allowed = 1;
  555. } else {
  556. info->online = 0;
  557. info->allowed = 0;
  558. }
  559. mutex_unlock(&info->lock);
  560. set_charging_fsm(info);
  561. return 0;
  562. }
  563. static struct pm860x_irq_desc {
  564. const char *name;
  565. irqreturn_t (*handler)(int irq, void *data);
  566. } pm860x_irq_descs[] = {
  567. { "usb supply detect", pm860x_charger_handler },
  568. { "charge done", pm860x_done_handler },
  569. { "charge timeout", pm860x_exception_handler },
  570. { "charge fault", pm860x_exception_handler },
  571. { "temperature", pm860x_temp_handler },
  572. { "vbatt", pm860x_vbattery_handler },
  573. { "vchg", pm860x_vchg_handler },
  574. };
  575. static const struct power_supply_desc pm860x_charger_desc = {
  576. .name = "usb",
  577. .type = POWER_SUPPLY_TYPE_USB,
  578. .properties = pm860x_usb_props,
  579. .num_properties = ARRAY_SIZE(pm860x_usb_props),
  580. .get_property = pm860x_usb_get_prop,
  581. };
  582. static int pm860x_charger_probe(struct platform_device *pdev)
  583. {
  584. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  585. struct power_supply_config psy_cfg = {};
  586. struct pm860x_charger_info *info;
  587. int ret;
  588. int count;
  589. int i;
  590. int j;
  591. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  592. if (!info)
  593. return -ENOMEM;
  594. count = pdev->num_resources;
  595. for (i = 0, j = 0; i < count; i++) {
  596. info->irq[j] = platform_get_irq(pdev, i);
  597. if (info->irq[j] < 0)
  598. continue;
  599. j++;
  600. }
  601. info->irq_nums = j;
  602. info->chip = chip;
  603. info->i2c =
  604. (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  605. info->i2c_8606 =
  606. (chip->id == CHIP_PM8607) ? chip->companion : chip->client;
  607. if (!info->i2c_8606) {
  608. dev_err(&pdev->dev, "Missed I2C address of 88PM8606!\n");
  609. ret = -EINVAL;
  610. goto out;
  611. }
  612. info->dev = &pdev->dev;
  613. /* set init value for the case we are not using battery */
  614. set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_OVP_LOW);
  615. mutex_init(&info->lock);
  616. platform_set_drvdata(pdev, info);
  617. psy_cfg.drv_data = info;
  618. psy_cfg.supplied_to = pm860x_supplied_to;
  619. psy_cfg.num_supplicants = ARRAY_SIZE(pm860x_supplied_to);
  620. info->usb = power_supply_register(&pdev->dev, &pm860x_charger_desc,
  621. &psy_cfg);
  622. if (IS_ERR(info->usb)) {
  623. ret = PTR_ERR(info->usb);
  624. goto out;
  625. }
  626. pm860x_init_charger(info);
  627. for (i = 0; i < ARRAY_SIZE(info->irq); i++) {
  628. ret = request_threaded_irq(info->irq[i], NULL,
  629. pm860x_irq_descs[i].handler,
  630. IRQF_ONESHOT, pm860x_irq_descs[i].name, info);
  631. if (ret < 0) {
  632. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  633. info->irq[i], ret);
  634. goto out_irq;
  635. }
  636. }
  637. return 0;
  638. out_irq:
  639. power_supply_unregister(info->usb);
  640. while (--i >= 0)
  641. free_irq(info->irq[i], info);
  642. out:
  643. return ret;
  644. }
  645. static int pm860x_charger_remove(struct platform_device *pdev)
  646. {
  647. struct pm860x_charger_info *info = platform_get_drvdata(pdev);
  648. int i;
  649. power_supply_unregister(info->usb);
  650. for (i = 0; i < info->irq_nums; i++)
  651. free_irq(info->irq[i], info);
  652. return 0;
  653. }
  654. static struct platform_driver pm860x_charger_driver = {
  655. .driver = {
  656. .name = "88pm860x-charger",
  657. },
  658. .probe = pm860x_charger_probe,
  659. .remove = pm860x_charger_remove,
  660. };
  661. module_platform_driver(pm860x_charger_driver);
  662. MODULE_DESCRIPTION("Marvell 88PM860x Charger driver");
  663. MODULE_LICENSE("GPL");