88pm860x_charger.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 = psy->get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, &data);
  258. if (ret)
  259. return ret;
  260. vbatt = data.intval / 1000;
  261. ret = psy->get_property(psy, POWER_SUPPLY_PROP_PRESENT, &data);
  262. if (ret)
  263. return ret;
  264. mutex_lock(&info->lock);
  265. info->present = data.intval;
  266. dev_dbg(info->dev, "Entering FSM:%s, Charger:%s, Battery:%s, "
  267. "Allowed:%d\n",
  268. &fsm_state[info->state][0],
  269. (info->online) ? "online" : "N/A",
  270. (info->present) ? "present" : "N/A", info->allowed);
  271. dev_dbg(info->dev, "set_charging_fsm:vbatt:%d(mV)\n", vbatt);
  272. switch (info->state) {
  273. case FSM_INIT:
  274. if (info->online && info->present && info->allowed) {
  275. if (vbatt < PRECHARGE_THRESHOLD) {
  276. info->state = FSM_PRECHARGE;
  277. start_precharge(info);
  278. } else if (vbatt > DISCHARGE_THRESHOLD) {
  279. info->state = FSM_DISCHARGE;
  280. stop_charge(info, vbatt);
  281. } else if (vbatt < DISCHARGE_THRESHOLD) {
  282. info->state = FSM_FASTCHARGE;
  283. start_fastcharge(info);
  284. }
  285. } else {
  286. if (vbatt < POWEROFF_THRESHOLD) {
  287. power_off_notification(info);
  288. } else {
  289. info->state = FSM_DISCHARGE;
  290. stop_charge(info, vbatt);
  291. }
  292. }
  293. break;
  294. case FSM_PRECHARGE:
  295. if (info->online && info->present && info->allowed) {
  296. if (vbatt > PRECHARGE_THRESHOLD) {
  297. info->state = FSM_FASTCHARGE;
  298. start_fastcharge(info);
  299. }
  300. } else {
  301. info->state = FSM_DISCHARGE;
  302. stop_charge(info, vbatt);
  303. }
  304. break;
  305. case FSM_FASTCHARGE:
  306. if (info->online && info->present && info->allowed) {
  307. if (vbatt < PRECHARGE_THRESHOLD) {
  308. info->state = FSM_PRECHARGE;
  309. start_precharge(info);
  310. }
  311. } else {
  312. info->state = FSM_DISCHARGE;
  313. stop_charge(info, vbatt);
  314. }
  315. break;
  316. case FSM_DISCHARGE:
  317. if (info->online && info->present && info->allowed) {
  318. if (vbatt < PRECHARGE_THRESHOLD) {
  319. info->state = FSM_PRECHARGE;
  320. start_precharge(info);
  321. } else if (vbatt < DISCHARGE_THRESHOLD) {
  322. info->state = FSM_FASTCHARGE;
  323. start_fastcharge(info);
  324. }
  325. } else {
  326. if (vbatt < POWEROFF_THRESHOLD)
  327. power_off_notification(info);
  328. else if (vbatt > CHARGE_THRESHOLD && info->online)
  329. set_vbatt_threshold(info, CHARGE_THRESHOLD, 0);
  330. }
  331. break;
  332. default:
  333. dev_warn(info->dev, "FSM meets wrong state:%d\n",
  334. info->state);
  335. break;
  336. }
  337. dev_dbg(info->dev,
  338. "Out FSM:%s, Charger:%s, Battery:%s, Allowed:%d\n",
  339. &fsm_state[info->state][0],
  340. (info->online) ? "online" : "N/A",
  341. (info->present) ? "present" : "N/A", info->allowed);
  342. mutex_unlock(&info->lock);
  343. return 0;
  344. }
  345. static irqreturn_t pm860x_charger_handler(int irq, void *data)
  346. {
  347. struct pm860x_charger_info *info = data;
  348. int ret;
  349. mutex_lock(&info->lock);
  350. ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
  351. if (ret < 0) {
  352. mutex_unlock(&info->lock);
  353. goto out;
  354. }
  355. if (ret & STATUS2_CHG) {
  356. info->online = 1;
  357. info->allowed = 1;
  358. } else {
  359. info->online = 0;
  360. info->allowed = 0;
  361. }
  362. mutex_unlock(&info->lock);
  363. dev_dbg(info->dev, "%s, Charger:%s, Allowed:%d\n", __func__,
  364. (info->online) ? "online" : "N/A", info->allowed);
  365. set_charging_fsm(info);
  366. power_supply_changed(&info->usb);
  367. out:
  368. return IRQ_HANDLED;
  369. }
  370. static irqreturn_t pm860x_temp_handler(int irq, void *data)
  371. {
  372. struct power_supply *psy;
  373. struct pm860x_charger_info *info = data;
  374. union power_supply_propval temp;
  375. int value;
  376. int ret;
  377. psy = power_supply_get_by_name(pm860x_supplied_to[0]);
  378. if (!psy)
  379. goto out;
  380. ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &temp);
  381. if (ret)
  382. goto out;
  383. value = temp.intval / 10;
  384. mutex_lock(&info->lock);
  385. /* Temperature < -10 C or >40 C, Will not allow charge */
  386. if (value < -10 || value > 40)
  387. info->allowed = 0;
  388. else
  389. info->allowed = 1;
  390. dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
  391. mutex_unlock(&info->lock);
  392. set_charging_fsm(info);
  393. out:
  394. return IRQ_HANDLED;
  395. }
  396. static irqreturn_t pm860x_exception_handler(int irq, void *data)
  397. {
  398. struct pm860x_charger_info *info = data;
  399. mutex_lock(&info->lock);
  400. info->allowed = 0;
  401. mutex_unlock(&info->lock);
  402. dev_dbg(info->dev, "%s, irq: %d\n", __func__, irq);
  403. set_charging_fsm(info);
  404. return IRQ_HANDLED;
  405. }
  406. static irqreturn_t pm860x_done_handler(int irq, void *data)
  407. {
  408. struct pm860x_charger_info *info = data;
  409. struct power_supply *psy;
  410. union power_supply_propval val;
  411. int ret;
  412. int vbatt;
  413. mutex_lock(&info->lock);
  414. /* pre-charge done, will transimit to fast-charge stage */
  415. if (info->state == FSM_PRECHARGE) {
  416. info->allowed = 1;
  417. goto out;
  418. }
  419. /*
  420. * Fast charge done, delay to read
  421. * the correct status of CHG_DET.
  422. */
  423. mdelay(5);
  424. info->allowed = 0;
  425. psy = power_supply_get_by_name(pm860x_supplied_to[0]);
  426. if (!psy)
  427. goto out;
  428. ret = psy->get_property(psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
  429. if (ret)
  430. goto out;
  431. vbatt = val.intval / 1000;
  432. /*
  433. * CHG_DONE interrupt is faster than CHG_DET interrupt when
  434. * plug in/out usb, So we can not rely on info->online, we
  435. * need check pm8607 status register to check usb is online
  436. * or not, then we can decide it is real charge done
  437. * automatically or it is triggered by usb plug out;
  438. */
  439. ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
  440. if (ret < 0)
  441. goto out;
  442. if (vbatt > CHARGE_THRESHOLD && ret & STATUS2_CHG)
  443. psy->set_property(psy, POWER_SUPPLY_PROP_CHARGE_FULL, &val);
  444. out:
  445. mutex_unlock(&info->lock);
  446. dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
  447. set_charging_fsm(info);
  448. return IRQ_HANDLED;
  449. }
  450. static irqreturn_t pm860x_vbattery_handler(int irq, void *data)
  451. {
  452. struct pm860x_charger_info *info = data;
  453. mutex_lock(&info->lock);
  454. set_vbatt_threshold(info, 0, 0);
  455. if (info->present && info->online)
  456. info->allowed = 1;
  457. else
  458. info->allowed = 0;
  459. mutex_unlock(&info->lock);
  460. dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
  461. set_charging_fsm(info);
  462. return IRQ_HANDLED;
  463. }
  464. static irqreturn_t pm860x_vchg_handler(int irq, void *data)
  465. {
  466. struct pm860x_charger_info *info = data;
  467. int vchg = 0;
  468. if (info->present)
  469. goto out;
  470. measure_vchg(info, &vchg);
  471. mutex_lock(&info->lock);
  472. if (!info->online) {
  473. int status;
  474. /* check if over-temp on pm8606 or not */
  475. status = pm860x_reg_read(info->i2c_8606, PM8606_FLAGS);
  476. if (status & OVER_TEMP_FLAG) {
  477. /* clear over temp flag and set auto recover */
  478. pm860x_set_bits(info->i2c_8606, PM8606_FLAGS,
  479. OVER_TEMP_FLAG, OVER_TEMP_FLAG);
  480. pm860x_set_bits(info->i2c_8606,
  481. PM8606_VSYS,
  482. OVTEMP_AUTORECOVER,
  483. OVTEMP_AUTORECOVER);
  484. dev_dbg(info->dev,
  485. "%s, pm8606 over-temp occure\n", __func__);
  486. }
  487. }
  488. if (vchg > VCHG_NORMAL_CHECK) {
  489. set_vchg_threshold(info, VCHG_OVP_LOW, 0);
  490. info->allowed = 0;
  491. dev_dbg(info->dev,
  492. "%s,pm8607 over-vchg occure,vchg = %dmv\n",
  493. __func__, vchg);
  494. } else if (vchg < VCHG_OVP_LOW) {
  495. set_vchg_threshold(info, VCHG_NORMAL_LOW,
  496. VCHG_NORMAL_HIGH);
  497. info->allowed = 1;
  498. dev_dbg(info->dev,
  499. "%s,pm8607 over-vchg recover,vchg = %dmv\n",
  500. __func__, vchg);
  501. }
  502. mutex_unlock(&info->lock);
  503. dev_dbg(info->dev, "%s, Allowed: %d\n", __func__, info->allowed);
  504. set_charging_fsm(info);
  505. out:
  506. return IRQ_HANDLED;
  507. }
  508. static int pm860x_usb_get_prop(struct power_supply *psy,
  509. enum power_supply_property psp,
  510. union power_supply_propval *val)
  511. {
  512. struct pm860x_charger_info *info =
  513. dev_get_drvdata(psy->dev->parent);
  514. switch (psp) {
  515. case POWER_SUPPLY_PROP_STATUS:
  516. if (info->state == FSM_FASTCHARGE ||
  517. info->state == FSM_PRECHARGE)
  518. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  519. else
  520. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  521. break;
  522. case POWER_SUPPLY_PROP_ONLINE:
  523. val->intval = info->online;
  524. break;
  525. default:
  526. return -ENODEV;
  527. }
  528. return 0;
  529. }
  530. static enum power_supply_property pm860x_usb_props[] = {
  531. POWER_SUPPLY_PROP_STATUS,
  532. POWER_SUPPLY_PROP_ONLINE,
  533. };
  534. static int pm860x_init_charger(struct pm860x_charger_info *info)
  535. {
  536. int ret;
  537. ret = pm860x_reg_read(info->i2c, PM8607_STATUS_2);
  538. if (ret < 0)
  539. return ret;
  540. mutex_lock(&info->lock);
  541. info->state = FSM_INIT;
  542. if (ret & STATUS2_CHG) {
  543. info->online = 1;
  544. info->allowed = 1;
  545. } else {
  546. info->online = 0;
  547. info->allowed = 0;
  548. }
  549. mutex_unlock(&info->lock);
  550. set_charging_fsm(info);
  551. return 0;
  552. }
  553. static struct pm860x_irq_desc {
  554. const char *name;
  555. irqreturn_t (*handler)(int irq, void *data);
  556. } pm860x_irq_descs[] = {
  557. { "usb supply detect", pm860x_charger_handler },
  558. { "charge done", pm860x_done_handler },
  559. { "charge timeout", pm860x_exception_handler },
  560. { "charge fault", pm860x_exception_handler },
  561. { "temperature", pm860x_temp_handler },
  562. { "vbatt", pm860x_vbattery_handler },
  563. { "vchg", pm860x_vchg_handler },
  564. };
  565. static int pm860x_charger_probe(struct platform_device *pdev)
  566. {
  567. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  568. struct pm860x_charger_info *info;
  569. int ret;
  570. int count;
  571. int i;
  572. int j;
  573. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  574. if (!info)
  575. return -ENOMEM;
  576. count = pdev->num_resources;
  577. for (i = 0, j = 0; i < count; i++) {
  578. info->irq[j] = platform_get_irq(pdev, i);
  579. if (info->irq[j] < 0)
  580. continue;
  581. j++;
  582. }
  583. info->irq_nums = j;
  584. info->chip = chip;
  585. info->i2c =
  586. (chip->id == CHIP_PM8607) ? chip->client : chip->companion;
  587. info->i2c_8606 =
  588. (chip->id == CHIP_PM8607) ? chip->companion : chip->client;
  589. if (!info->i2c_8606) {
  590. dev_err(&pdev->dev, "Missed I2C address of 88PM8606!\n");
  591. ret = -EINVAL;
  592. goto out;
  593. }
  594. info->dev = &pdev->dev;
  595. /* set init value for the case we are not using battery */
  596. set_vchg_threshold(info, VCHG_NORMAL_LOW, VCHG_OVP_LOW);
  597. mutex_init(&info->lock);
  598. platform_set_drvdata(pdev, info);
  599. info->usb.name = "usb";
  600. info->usb.type = POWER_SUPPLY_TYPE_USB;
  601. info->usb.supplied_to = pm860x_supplied_to;
  602. info->usb.num_supplicants = ARRAY_SIZE(pm860x_supplied_to);
  603. info->usb.properties = pm860x_usb_props;
  604. info->usb.num_properties = ARRAY_SIZE(pm860x_usb_props);
  605. info->usb.get_property = pm860x_usb_get_prop;
  606. ret = power_supply_register(&pdev->dev, &info->usb);
  607. if (ret)
  608. goto out;
  609. pm860x_init_charger(info);
  610. for (i = 0; i < ARRAY_SIZE(info->irq); i++) {
  611. ret = request_threaded_irq(info->irq[i], NULL,
  612. pm860x_irq_descs[i].handler,
  613. IRQF_ONESHOT, pm860x_irq_descs[i].name, info);
  614. if (ret < 0) {
  615. dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n",
  616. info->irq[i], ret);
  617. goto out_irq;
  618. }
  619. }
  620. return 0;
  621. out_irq:
  622. while (--i >= 0)
  623. free_irq(info->irq[i], info);
  624. out:
  625. kfree(info);
  626. return ret;
  627. }
  628. static int pm860x_charger_remove(struct platform_device *pdev)
  629. {
  630. struct pm860x_charger_info *info = platform_get_drvdata(pdev);
  631. int i;
  632. platform_set_drvdata(pdev, NULL);
  633. power_supply_unregister(&info->usb);
  634. free_irq(info->irq[0], info);
  635. for (i = 0; i < info->irq_nums; i++)
  636. free_irq(info->irq[i], info);
  637. kfree(info);
  638. return 0;
  639. }
  640. static struct platform_driver pm860x_charger_driver = {
  641. .driver = {
  642. .name = "88pm860x-charger",
  643. .owner = THIS_MODULE,
  644. },
  645. .probe = pm860x_charger_probe,
  646. .remove = pm860x_charger_remove,
  647. };
  648. module_platform_driver(pm860x_charger_driver);
  649. MODULE_DESCRIPTION("Marvell 88PM860x Charger driver");
  650. MODULE_LICENSE("GPL");