88pm860x_charger.c 19 KB

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