axp288_charger.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * axp288_charger.c - X-power AXP288 PMIC Charger driver
  3. *
  4. * Copyright (C) 2014 Intel Corporation
  5. * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
  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. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/delay.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/usb/otg.h>
  23. #include <linux/notifier.h>
  24. #include <linux/power_supply.h>
  25. #include <linux/notifier.h>
  26. #include <linux/property.h>
  27. #include <linux/mfd/axp20x.h>
  28. #include <linux/extcon.h>
  29. #define PS_STAT_VBUS_TRIGGER (1 << 0)
  30. #define PS_STAT_BAT_CHRG_DIR (1 << 2)
  31. #define PS_STAT_VBAT_ABOVE_VHOLD (1 << 3)
  32. #define PS_STAT_VBUS_VALID (1 << 4)
  33. #define PS_STAT_VBUS_PRESENT (1 << 5)
  34. #define CHRG_STAT_BAT_SAFE_MODE (1 << 3)
  35. #define CHRG_STAT_BAT_VALID (1 << 4)
  36. #define CHRG_STAT_BAT_PRESENT (1 << 5)
  37. #define CHRG_STAT_CHARGING (1 << 6)
  38. #define CHRG_STAT_PMIC_OTP (1 << 7)
  39. #define VBUS_ISPOUT_CUR_LIM_MASK 0x03
  40. #define VBUS_ISPOUT_CUR_LIM_BIT_POS 0
  41. #define VBUS_ISPOUT_CUR_LIM_900MA 0x0 /* 900mA */
  42. #define VBUS_ISPOUT_CUR_LIM_1500MA 0x1 /* 1500mA */
  43. #define VBUS_ISPOUT_CUR_LIM_2000MA 0x2 /* 2000mA */
  44. #define VBUS_ISPOUT_CUR_NO_LIM 0x3 /* 2500mA */
  45. #define VBUS_ISPOUT_VHOLD_SET_MASK 0x31
  46. #define VBUS_ISPOUT_VHOLD_SET_BIT_POS 0x3
  47. #define VBUS_ISPOUT_VHOLD_SET_OFFSET 4000 /* 4000mV */
  48. #define VBUS_ISPOUT_VHOLD_SET_LSB_RES 100 /* 100mV */
  49. #define VBUS_ISPOUT_VHOLD_SET_4300MV 0x3 /* 4300mV */
  50. #define VBUS_ISPOUT_VBUS_PATH_DIS (1 << 7)
  51. #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
  52. #define CHRG_CCCV_CC_BIT_POS 0
  53. #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
  54. #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
  55. #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */
  56. #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
  57. #define CHRG_CCCV_CV_BIT_POS 5
  58. #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */
  59. #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */
  60. #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */
  61. #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */
  62. #define CHRG_CCCV_CHG_EN (1 << 7)
  63. #define CNTL2_CC_TIMEOUT_MASK 0x3 /* 2 bits */
  64. #define CNTL2_CC_TIMEOUT_OFFSET 6 /* 6 Hrs */
  65. #define CNTL2_CC_TIMEOUT_LSB_RES 2 /* 2 Hrs */
  66. #define CNTL2_CC_TIMEOUT_12HRS 0x3 /* 12 Hrs */
  67. #define CNTL2_CHGLED_TYPEB (1 << 4)
  68. #define CNTL2_CHG_OUT_TURNON (1 << 5)
  69. #define CNTL2_PC_TIMEOUT_MASK 0xC0
  70. #define CNTL2_PC_TIMEOUT_OFFSET 40 /* 40 mins */
  71. #define CNTL2_PC_TIMEOUT_LSB_RES 10 /* 10 mins */
  72. #define CNTL2_PC_TIMEOUT_70MINS 0x3
  73. #define CHRG_ILIM_TEMP_LOOP_EN (1 << 3)
  74. #define CHRG_VBUS_ILIM_MASK 0xf0
  75. #define CHRG_VBUS_ILIM_BIT_POS 4
  76. #define CHRG_VBUS_ILIM_100MA 0x0 /* 100mA */
  77. #define CHRG_VBUS_ILIM_500MA 0x1 /* 500mA */
  78. #define CHRG_VBUS_ILIM_900MA 0x2 /* 900mA */
  79. #define CHRG_VBUS_ILIM_1500MA 0x3 /* 1500mA */
  80. #define CHRG_VBUS_ILIM_2000MA 0x4 /* 2000mA */
  81. #define CHRG_VBUS_ILIM_2500MA 0x5 /* 2500mA */
  82. #define CHRG_VBUS_ILIM_3000MA 0x6 /* 3000mA */
  83. #define CHRG_VLTFC_0C 0xA5 /* 0 DegC */
  84. #define CHRG_VHTFC_45C 0x1F /* 45 DegC */
  85. #define BAT_IRQ_CFG_CHRG_DONE (1 << 2)
  86. #define BAT_IRQ_CFG_CHRG_START (1 << 3)
  87. #define BAT_IRQ_CFG_BAT_SAFE_EXIT (1 << 4)
  88. #define BAT_IRQ_CFG_BAT_SAFE_ENTER (1 << 5)
  89. #define BAT_IRQ_CFG_BAT_DISCON (1 << 6)
  90. #define BAT_IRQ_CFG_BAT_CONN (1 << 7)
  91. #define BAT_IRQ_CFG_BAT_MASK 0xFC
  92. #define TEMP_IRQ_CFG_QCBTU (1 << 4)
  93. #define TEMP_IRQ_CFG_CBTU (1 << 5)
  94. #define TEMP_IRQ_CFG_QCBTO (1 << 6)
  95. #define TEMP_IRQ_CFG_CBTO (1 << 7)
  96. #define TEMP_IRQ_CFG_MASK 0xF0
  97. #define FG_CNTL_OCV_ADJ_EN (1 << 3)
  98. #define CV_4100MV 4100 /* 4100mV */
  99. #define CV_4150MV 4150 /* 4150mV */
  100. #define CV_4200MV 4200 /* 4200mV */
  101. #define CV_4350MV 4350 /* 4350mV */
  102. #define CC_200MA 200 /* 200mA */
  103. #define CC_600MA 600 /* 600mA */
  104. #define CC_800MA 800 /* 800mA */
  105. #define CC_1000MA 1000 /* 1000mA */
  106. #define CC_1600MA 1600 /* 1600mA */
  107. #define CC_2000MA 2000 /* 2000mA */
  108. #define ILIM_100MA 100 /* 100mA */
  109. #define ILIM_500MA 500 /* 500mA */
  110. #define ILIM_900MA 900 /* 900mA */
  111. #define ILIM_1500MA 1500 /* 1500mA */
  112. #define ILIM_2000MA 2000 /* 2000mA */
  113. #define ILIM_2500MA 2500 /* 2500mA */
  114. #define ILIM_3000MA 3000 /* 3000mA */
  115. #define AXP288_EXTCON_DEV_NAME "axp288_extcon"
  116. #define AXP288_EXTCON_SLOW_CHARGER "SLOW-CHARGER"
  117. #define AXP288_EXTCON_DOWNSTREAM_CHARGER "CHARGE-DOWNSTREAM"
  118. #define AXP288_EXTCON_FAST_CHARGER "FAST-CHARGER"
  119. enum {
  120. VBUS_OV_IRQ = 0,
  121. CHARGE_DONE_IRQ,
  122. CHARGE_CHARGING_IRQ,
  123. BAT_SAFE_QUIT_IRQ,
  124. BAT_SAFE_ENTER_IRQ,
  125. QCBTU_IRQ,
  126. CBTU_IRQ,
  127. QCBTO_IRQ,
  128. CBTO_IRQ,
  129. CHRG_INTR_END,
  130. };
  131. struct axp288_chrg_info {
  132. struct platform_device *pdev;
  133. struct axp20x_chrg_pdata *pdata;
  134. struct regmap *regmap;
  135. struct regmap_irq_chip_data *regmap_irqc;
  136. int irq[CHRG_INTR_END];
  137. struct power_supply *psy_usb;
  138. struct mutex lock;
  139. /* OTG/Host mode */
  140. struct {
  141. struct work_struct work;
  142. struct extcon_specific_cable_nb cable;
  143. struct notifier_block id_nb;
  144. bool id_short;
  145. } otg;
  146. /* SDP/CDP/DCP USB charging cable notifications */
  147. struct {
  148. struct extcon_dev *edev;
  149. bool connected;
  150. enum power_supply_type chg_type;
  151. struct notifier_block nb;
  152. struct work_struct work;
  153. } cable;
  154. int health;
  155. int inlmt;
  156. int cc;
  157. int cv;
  158. int max_cc;
  159. int max_cv;
  160. bool online;
  161. bool present;
  162. bool enable_charger;
  163. bool is_charger_enabled;
  164. };
  165. static inline int axp288_charger_set_cc(struct axp288_chrg_info *info, int cc)
  166. {
  167. u8 reg_val;
  168. int ret;
  169. if (cc < CHRG_CCCV_CC_OFFSET)
  170. cc = CHRG_CCCV_CC_OFFSET;
  171. else if (cc > info->max_cc)
  172. cc = info->max_cc;
  173. reg_val = (cc - CHRG_CCCV_CC_OFFSET) / CHRG_CCCV_CC_LSB_RES;
  174. cc = (reg_val * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
  175. reg_val = reg_val << CHRG_CCCV_CC_BIT_POS;
  176. ret = regmap_update_bits(info->regmap,
  177. AXP20X_CHRG_CTRL1,
  178. CHRG_CCCV_CC_MASK, reg_val);
  179. if (ret >= 0)
  180. info->cc = cc;
  181. return ret;
  182. }
  183. static inline int axp288_charger_set_cv(struct axp288_chrg_info *info, int cv)
  184. {
  185. u8 reg_val;
  186. int ret;
  187. if (cv <= CV_4100MV) {
  188. reg_val = CHRG_CCCV_CV_4100MV;
  189. cv = CV_4100MV;
  190. } else if (cv <= CV_4150MV) {
  191. reg_val = CHRG_CCCV_CV_4150MV;
  192. cv = CV_4150MV;
  193. } else if (cv <= CV_4200MV) {
  194. reg_val = CHRG_CCCV_CV_4200MV;
  195. cv = CV_4200MV;
  196. } else {
  197. reg_val = CHRG_CCCV_CV_4350MV;
  198. cv = CV_4350MV;
  199. }
  200. reg_val = reg_val << CHRG_CCCV_CV_BIT_POS;
  201. ret = regmap_update_bits(info->regmap,
  202. AXP20X_CHRG_CTRL1,
  203. CHRG_CCCV_CV_MASK, reg_val);
  204. if (ret >= 0)
  205. info->cv = cv;
  206. return ret;
  207. }
  208. static inline int axp288_charger_set_vbus_inlmt(struct axp288_chrg_info *info,
  209. int inlmt)
  210. {
  211. int ret;
  212. unsigned int val;
  213. u8 reg_val;
  214. /* Read in limit register */
  215. ret = regmap_read(info->regmap, AXP20X_CHRG_BAK_CTRL, &val);
  216. if (ret < 0)
  217. goto set_inlmt_fail;
  218. if (inlmt <= ILIM_100MA) {
  219. reg_val = CHRG_VBUS_ILIM_100MA;
  220. inlmt = ILIM_100MA;
  221. } else if (inlmt <= ILIM_500MA) {
  222. reg_val = CHRG_VBUS_ILIM_500MA;
  223. inlmt = ILIM_500MA;
  224. } else if (inlmt <= ILIM_900MA) {
  225. reg_val = CHRG_VBUS_ILIM_900MA;
  226. inlmt = ILIM_900MA;
  227. } else if (inlmt <= ILIM_1500MA) {
  228. reg_val = CHRG_VBUS_ILIM_1500MA;
  229. inlmt = ILIM_1500MA;
  230. } else if (inlmt <= ILIM_2000MA) {
  231. reg_val = CHRG_VBUS_ILIM_2000MA;
  232. inlmt = ILIM_2000MA;
  233. } else if (inlmt <= ILIM_2500MA) {
  234. reg_val = CHRG_VBUS_ILIM_2500MA;
  235. inlmt = ILIM_2500MA;
  236. } else {
  237. reg_val = CHRG_VBUS_ILIM_3000MA;
  238. inlmt = ILIM_3000MA;
  239. }
  240. reg_val = (val & ~CHRG_VBUS_ILIM_MASK)
  241. | (reg_val << CHRG_VBUS_ILIM_BIT_POS);
  242. ret = regmap_write(info->regmap, AXP20X_CHRG_BAK_CTRL, reg_val);
  243. if (ret >= 0)
  244. info->inlmt = inlmt;
  245. else
  246. dev_err(&info->pdev->dev, "charger BAK control %d\n", ret);
  247. set_inlmt_fail:
  248. return ret;
  249. }
  250. static int axp288_charger_vbus_path_select(struct axp288_chrg_info *info,
  251. bool enable)
  252. {
  253. int ret;
  254. if (enable)
  255. ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
  256. VBUS_ISPOUT_VBUS_PATH_DIS, 0);
  257. else
  258. ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
  259. VBUS_ISPOUT_VBUS_PATH_DIS, VBUS_ISPOUT_VBUS_PATH_DIS);
  260. if (ret < 0)
  261. dev_err(&info->pdev->dev, "axp288 vbus path select %d\n", ret);
  262. return ret;
  263. }
  264. static int axp288_charger_enable_charger(struct axp288_chrg_info *info,
  265. bool enable)
  266. {
  267. int ret;
  268. if (enable)
  269. ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
  270. CHRG_CCCV_CHG_EN, CHRG_CCCV_CHG_EN);
  271. else
  272. ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
  273. CHRG_CCCV_CHG_EN, 0);
  274. if (ret < 0)
  275. dev_err(&info->pdev->dev, "axp288 enable charger %d\n", ret);
  276. else
  277. info->is_charger_enabled = enable;
  278. return ret;
  279. }
  280. static int axp288_charger_is_present(struct axp288_chrg_info *info)
  281. {
  282. int ret, present = 0;
  283. unsigned int val;
  284. ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
  285. if (ret < 0)
  286. return ret;
  287. if (val & PS_STAT_VBUS_PRESENT)
  288. present = 1;
  289. return present;
  290. }
  291. static int axp288_charger_is_online(struct axp288_chrg_info *info)
  292. {
  293. int ret, online = 0;
  294. unsigned int val;
  295. ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
  296. if (ret < 0)
  297. return ret;
  298. if (val & PS_STAT_VBUS_VALID)
  299. online = 1;
  300. return online;
  301. }
  302. static int axp288_get_charger_health(struct axp288_chrg_info *info)
  303. {
  304. int ret, pwr_stat, chrg_stat;
  305. int health = POWER_SUPPLY_HEALTH_UNKNOWN;
  306. unsigned int val;
  307. ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
  308. if ((ret < 0) || !(val & PS_STAT_VBUS_PRESENT))
  309. goto health_read_fail;
  310. else
  311. pwr_stat = val;
  312. ret = regmap_read(info->regmap, AXP20X_PWR_OP_MODE, &val);
  313. if (ret < 0)
  314. goto health_read_fail;
  315. else
  316. chrg_stat = val;
  317. if (!(pwr_stat & PS_STAT_VBUS_VALID))
  318. health = POWER_SUPPLY_HEALTH_DEAD;
  319. else if (chrg_stat & CHRG_STAT_PMIC_OTP)
  320. health = POWER_SUPPLY_HEALTH_OVERHEAT;
  321. else if (chrg_stat & CHRG_STAT_BAT_SAFE_MODE)
  322. health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
  323. else
  324. health = POWER_SUPPLY_HEALTH_GOOD;
  325. health_read_fail:
  326. return health;
  327. }
  328. static int axp288_charger_usb_set_property(struct power_supply *psy,
  329. enum power_supply_property psp,
  330. const union power_supply_propval *val)
  331. {
  332. struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
  333. int ret = 0;
  334. int scaled_val;
  335. mutex_lock(&info->lock);
  336. switch (psp) {
  337. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  338. scaled_val = min(val->intval, info->max_cc);
  339. scaled_val = DIV_ROUND_CLOSEST(scaled_val, 1000);
  340. ret = axp288_charger_set_cc(info, scaled_val);
  341. if (ret < 0)
  342. dev_warn(&info->pdev->dev, "set charge current failed\n");
  343. break;
  344. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  345. scaled_val = min(val->intval, info->max_cv);
  346. scaled_val = DIV_ROUND_CLOSEST(scaled_val, 1000);
  347. ret = axp288_charger_set_cv(info, scaled_val);
  348. if (ret < 0)
  349. dev_warn(&info->pdev->dev, "set charge voltage failed\n");
  350. break;
  351. default:
  352. ret = -EINVAL;
  353. }
  354. mutex_unlock(&info->lock);
  355. return ret;
  356. }
  357. static int axp288_charger_usb_get_property(struct power_supply *psy,
  358. enum power_supply_property psp,
  359. union power_supply_propval *val)
  360. {
  361. struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
  362. int ret = 0;
  363. mutex_lock(&info->lock);
  364. switch (psp) {
  365. case POWER_SUPPLY_PROP_PRESENT:
  366. /* Check for OTG case first */
  367. if (info->otg.id_short) {
  368. val->intval = 0;
  369. break;
  370. }
  371. ret = axp288_charger_is_present(info);
  372. if (ret < 0)
  373. goto psy_get_prop_fail;
  374. info->present = ret;
  375. val->intval = info->present;
  376. break;
  377. case POWER_SUPPLY_PROP_ONLINE:
  378. /* Check for OTG case first */
  379. if (info->otg.id_short) {
  380. val->intval = 0;
  381. break;
  382. }
  383. ret = axp288_charger_is_online(info);
  384. if (ret < 0)
  385. goto psy_get_prop_fail;
  386. info->online = ret;
  387. val->intval = info->online;
  388. break;
  389. case POWER_SUPPLY_PROP_HEALTH:
  390. val->intval = axp288_get_charger_health(info);
  391. break;
  392. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  393. val->intval = info->cc * 1000;
  394. break;
  395. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  396. val->intval = info->max_cc * 1000;
  397. break;
  398. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  399. val->intval = info->cv * 1000;
  400. break;
  401. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  402. val->intval = info->max_cv * 1000;
  403. break;
  404. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
  405. val->intval = info->inlmt * 1000;
  406. break;
  407. default:
  408. ret = -EINVAL;
  409. goto psy_get_prop_fail;
  410. }
  411. psy_get_prop_fail:
  412. mutex_unlock(&info->lock);
  413. return ret;
  414. }
  415. static int axp288_charger_property_is_writeable(struct power_supply *psy,
  416. enum power_supply_property psp)
  417. {
  418. int ret;
  419. switch (psp) {
  420. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  421. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  422. ret = 1;
  423. break;
  424. default:
  425. ret = 0;
  426. }
  427. return ret;
  428. }
  429. static enum power_supply_property axp288_usb_props[] = {
  430. POWER_SUPPLY_PROP_PRESENT,
  431. POWER_SUPPLY_PROP_ONLINE,
  432. POWER_SUPPLY_PROP_TYPE,
  433. POWER_SUPPLY_PROP_HEALTH,
  434. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  435. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  436. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  437. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  438. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
  439. };
  440. static const struct power_supply_desc axp288_charger_desc = {
  441. .name = "axp288_charger",
  442. .type = POWER_SUPPLY_TYPE_USB,
  443. .properties = axp288_usb_props,
  444. .num_properties = ARRAY_SIZE(axp288_usb_props),
  445. .get_property = axp288_charger_usb_get_property,
  446. .set_property = axp288_charger_usb_set_property,
  447. .property_is_writeable = axp288_charger_property_is_writeable,
  448. };
  449. static irqreturn_t axp288_charger_irq_thread_handler(int irq, void *dev)
  450. {
  451. struct axp288_chrg_info *info = dev;
  452. int i;
  453. for (i = 0; i < CHRG_INTR_END; i++) {
  454. if (info->irq[i] == irq)
  455. break;
  456. }
  457. if (i >= CHRG_INTR_END) {
  458. dev_warn(&info->pdev->dev, "spurious interrupt!!\n");
  459. return IRQ_NONE;
  460. }
  461. switch (i) {
  462. case VBUS_OV_IRQ:
  463. dev_dbg(&info->pdev->dev, "VBUS Over Voltage INTR\n");
  464. break;
  465. case CHARGE_DONE_IRQ:
  466. dev_dbg(&info->pdev->dev, "Charging Done INTR\n");
  467. break;
  468. case CHARGE_CHARGING_IRQ:
  469. dev_dbg(&info->pdev->dev, "Start Charging IRQ\n");
  470. break;
  471. case BAT_SAFE_QUIT_IRQ:
  472. dev_dbg(&info->pdev->dev,
  473. "Quit Safe Mode(restart timer) Charging IRQ\n");
  474. break;
  475. case BAT_SAFE_ENTER_IRQ:
  476. dev_dbg(&info->pdev->dev,
  477. "Enter Safe Mode(timer expire) Charging IRQ\n");
  478. break;
  479. case QCBTU_IRQ:
  480. dev_dbg(&info->pdev->dev,
  481. "Quit Battery Under Temperature(CHRG) INTR\n");
  482. break;
  483. case CBTU_IRQ:
  484. dev_dbg(&info->pdev->dev,
  485. "Hit Battery Under Temperature(CHRG) INTR\n");
  486. break;
  487. case QCBTO_IRQ:
  488. dev_dbg(&info->pdev->dev,
  489. "Quit Battery Over Temperature(CHRG) INTR\n");
  490. break;
  491. case CBTO_IRQ:
  492. dev_dbg(&info->pdev->dev,
  493. "Hit Battery Over Temperature(CHRG) INTR\n");
  494. break;
  495. default:
  496. dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n");
  497. goto out;
  498. }
  499. power_supply_changed(info->psy_usb);
  500. out:
  501. return IRQ_HANDLED;
  502. }
  503. static void axp288_charger_extcon_evt_worker(struct work_struct *work)
  504. {
  505. struct axp288_chrg_info *info =
  506. container_of(work, struct axp288_chrg_info, cable.work);
  507. int ret, current_limit;
  508. bool changed = false;
  509. struct extcon_dev *edev = info->cable.edev;
  510. bool old_connected = info->cable.connected;
  511. /* Determine cable/charger type */
  512. if (extcon_get_cable_state(edev, AXP288_EXTCON_SLOW_CHARGER) > 0) {
  513. dev_dbg(&info->pdev->dev, "USB SDP charger is connected");
  514. info->cable.connected = true;
  515. info->cable.chg_type = POWER_SUPPLY_TYPE_USB;
  516. } else if (extcon_get_cable_state(edev,
  517. AXP288_EXTCON_DOWNSTREAM_CHARGER) > 0) {
  518. dev_dbg(&info->pdev->dev, "USB CDP charger is connected");
  519. info->cable.connected = true;
  520. info->cable.chg_type = POWER_SUPPLY_TYPE_USB_CDP;
  521. } else if (extcon_get_cable_state(edev,
  522. AXP288_EXTCON_FAST_CHARGER) > 0) {
  523. dev_dbg(&info->pdev->dev, "USB DCP charger is connected");
  524. info->cable.connected = true;
  525. info->cable.chg_type = POWER_SUPPLY_TYPE_USB_DCP;
  526. } else {
  527. if (old_connected)
  528. dev_dbg(&info->pdev->dev, "USB charger disconnected");
  529. info->cable.connected = false;
  530. info->cable.chg_type = POWER_SUPPLY_TYPE_USB;
  531. }
  532. /* Cable status changed */
  533. if (old_connected != info->cable.connected)
  534. changed = true;
  535. if (!changed)
  536. return;
  537. mutex_lock(&info->lock);
  538. if (info->is_charger_enabled && !info->cable.connected) {
  539. info->enable_charger = false;
  540. ret = axp288_charger_enable_charger(info, info->enable_charger);
  541. if (ret < 0)
  542. dev_err(&info->pdev->dev,
  543. "cannot disable charger (%d)", ret);
  544. } else if (!info->is_charger_enabled && info->cable.connected) {
  545. switch (info->cable.chg_type) {
  546. case POWER_SUPPLY_TYPE_USB:
  547. current_limit = ILIM_500MA;
  548. break;
  549. case POWER_SUPPLY_TYPE_USB_CDP:
  550. current_limit = ILIM_1500MA;
  551. break;
  552. case POWER_SUPPLY_TYPE_USB_DCP:
  553. current_limit = ILIM_2000MA;
  554. break;
  555. default:
  556. /* Unknown */
  557. current_limit = 0;
  558. break;
  559. }
  560. /* Set vbus current limit first, then enable charger */
  561. ret = axp288_charger_set_vbus_inlmt(info, current_limit);
  562. if (ret < 0) {
  563. dev_err(&info->pdev->dev,
  564. "error setting current limit (%d)", ret);
  565. } else {
  566. info->enable_charger = (current_limit > 0);
  567. ret = axp288_charger_enable_charger(info,
  568. info->enable_charger);
  569. if (ret < 0)
  570. dev_err(&info->pdev->dev,
  571. "cannot enable charger (%d)", ret);
  572. }
  573. }
  574. if (changed)
  575. info->health = axp288_get_charger_health(info);
  576. mutex_unlock(&info->lock);
  577. if (changed)
  578. power_supply_changed(info->psy_usb);
  579. }
  580. static int axp288_charger_handle_cable_evt(struct notifier_block *nb,
  581. unsigned long event, void *param)
  582. {
  583. struct axp288_chrg_info *info =
  584. container_of(nb, struct axp288_chrg_info, cable.nb);
  585. schedule_work(&info->cable.work);
  586. return NOTIFY_OK;
  587. }
  588. static void axp288_charger_otg_evt_worker(struct work_struct *work)
  589. {
  590. struct axp288_chrg_info *info =
  591. container_of(work, struct axp288_chrg_info, otg.work);
  592. int ret;
  593. /* Disable VBUS path before enabling the 5V boost */
  594. ret = axp288_charger_vbus_path_select(info, !info->otg.id_short);
  595. if (ret < 0)
  596. dev_warn(&info->pdev->dev, "vbus path disable failed\n");
  597. }
  598. static int axp288_charger_handle_otg_evt(struct notifier_block *nb,
  599. unsigned long event, void *param)
  600. {
  601. struct axp288_chrg_info *info =
  602. container_of(nb, struct axp288_chrg_info, otg.id_nb);
  603. struct extcon_dev *edev = param;
  604. int usb_host = extcon_get_cable_state(edev, "USB-Host");
  605. dev_dbg(&info->pdev->dev, "external connector USB-Host is %s\n",
  606. usb_host ? "attached" : "detached");
  607. /*
  608. * Set usb_id_short flag to avoid running charger detection logic
  609. * in case usb host.
  610. */
  611. info->otg.id_short = usb_host;
  612. schedule_work(&info->otg.work);
  613. return NOTIFY_OK;
  614. }
  615. static void charger_init_hw_regs(struct axp288_chrg_info *info)
  616. {
  617. int ret, cc, cv;
  618. unsigned int val;
  619. /* Program temperature thresholds */
  620. ret = regmap_write(info->regmap, AXP20X_V_LTF_CHRG, CHRG_VLTFC_0C);
  621. if (ret < 0)
  622. dev_warn(&info->pdev->dev, "register(%x) write error(%d)\n",
  623. AXP20X_V_LTF_CHRG, ret);
  624. ret = regmap_write(info->regmap, AXP20X_V_HTF_CHRG, CHRG_VHTFC_45C);
  625. if (ret < 0)
  626. dev_warn(&info->pdev->dev, "register(%x) write error(%d)\n",
  627. AXP20X_V_HTF_CHRG, ret);
  628. /* Do not turn-off charger o/p after charge cycle ends */
  629. ret = regmap_update_bits(info->regmap,
  630. AXP20X_CHRG_CTRL2,
  631. CNTL2_CHG_OUT_TURNON, 1);
  632. if (ret < 0)
  633. dev_warn(&info->pdev->dev, "register(%x) write error(%d)\n",
  634. AXP20X_CHRG_CTRL2, ret);
  635. /* Enable interrupts */
  636. ret = regmap_update_bits(info->regmap,
  637. AXP20X_IRQ2_EN,
  638. BAT_IRQ_CFG_BAT_MASK, 1);
  639. if (ret < 0)
  640. dev_warn(&info->pdev->dev, "register(%x) write error(%d)\n",
  641. AXP20X_IRQ2_EN, ret);
  642. ret = regmap_update_bits(info->regmap, AXP20X_IRQ3_EN,
  643. TEMP_IRQ_CFG_MASK, 1);
  644. if (ret < 0)
  645. dev_warn(&info->pdev->dev, "register(%x) write error(%d)\n",
  646. AXP20X_IRQ3_EN, ret);
  647. /* Setup ending condition for charging to be 10% of I(chrg) */
  648. ret = regmap_update_bits(info->regmap,
  649. AXP20X_CHRG_CTRL1,
  650. CHRG_CCCV_ITERM_20P, 0);
  651. if (ret < 0)
  652. dev_warn(&info->pdev->dev, "register(%x) write error(%d)\n",
  653. AXP20X_CHRG_CTRL1, ret);
  654. /* Disable OCV-SOC curve calibration */
  655. ret = regmap_update_bits(info->regmap,
  656. AXP20X_CC_CTRL,
  657. FG_CNTL_OCV_ADJ_EN, 0);
  658. if (ret < 0)
  659. dev_warn(&info->pdev->dev, "register(%x) write error(%d)\n",
  660. AXP20X_CC_CTRL, ret);
  661. /* Init charging current and voltage */
  662. info->max_cc = info->pdata->max_cc;
  663. info->max_cv = info->pdata->max_cv;
  664. /* Read current charge voltage and current limit */
  665. ret = regmap_read(info->regmap, AXP20X_CHRG_CTRL1, &val);
  666. if (ret < 0) {
  667. /* Assume default if cannot read */
  668. info->cc = info->pdata->def_cc;
  669. info->cv = info->pdata->def_cv;
  670. } else {
  671. /* Determine charge voltage */
  672. cv = (val & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS;
  673. switch (cv) {
  674. case CHRG_CCCV_CV_4100MV:
  675. info->cv = CV_4100MV;
  676. break;
  677. case CHRG_CCCV_CV_4150MV:
  678. info->cv = CV_4150MV;
  679. break;
  680. case CHRG_CCCV_CV_4200MV:
  681. info->cv = CV_4200MV;
  682. break;
  683. case CHRG_CCCV_CV_4350MV:
  684. info->cv = CV_4350MV;
  685. break;
  686. default:
  687. info->cv = INT_MAX;
  688. break;
  689. }
  690. /* Determine charge current limit */
  691. cc = (ret & CHRG_CCCV_CC_MASK) >> CHRG_CCCV_CC_BIT_POS;
  692. cc = (cc * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
  693. info->cc = cc;
  694. /* Program default charging voltage and current */
  695. cc = min(info->pdata->def_cc, info->max_cc);
  696. cv = min(info->pdata->def_cv, info->max_cv);
  697. ret = axp288_charger_set_cc(info, cc);
  698. if (ret < 0)
  699. dev_warn(&info->pdev->dev,
  700. "error(%d) in setting CC\n", ret);
  701. ret = axp288_charger_set_cv(info, cv);
  702. if (ret < 0)
  703. dev_warn(&info->pdev->dev,
  704. "error(%d) in setting CV\n", ret);
  705. }
  706. }
  707. static int axp288_charger_probe(struct platform_device *pdev)
  708. {
  709. int ret, i, pirq;
  710. struct axp288_chrg_info *info;
  711. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  712. struct power_supply_config charger_cfg = {};
  713. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  714. if (!info)
  715. return -ENOMEM;
  716. info->pdev = pdev;
  717. info->regmap = axp20x->regmap;
  718. info->regmap_irqc = axp20x->regmap_irqc;
  719. info->pdata = pdev->dev.platform_data;
  720. if (!info->pdata) {
  721. /* Try ACPI provided pdata via device properties */
  722. if (!device_property_present(&pdev->dev,
  723. "axp288_charger_data\n"))
  724. dev_err(&pdev->dev, "failed to get platform data\n");
  725. return -ENODEV;
  726. }
  727. info->cable.edev = extcon_get_extcon_dev(AXP288_EXTCON_DEV_NAME);
  728. if (info->cable.edev == NULL) {
  729. dev_dbg(&pdev->dev, "%s is not ready, probe deferred\n",
  730. AXP288_EXTCON_DEV_NAME);
  731. return -EPROBE_DEFER;
  732. }
  733. /* Register for extcon notification */
  734. INIT_WORK(&info->cable.work, axp288_charger_extcon_evt_worker);
  735. info->cable.nb.notifier_call = axp288_charger_handle_cable_evt;
  736. ret = extcon_register_notifier(info->cable.edev, EXTCON_NONE, &info->cable.nb);
  737. if (ret) {
  738. dev_err(&info->pdev->dev,
  739. "failed to register extcon notifier %d\n", ret);
  740. return ret;
  741. }
  742. platform_set_drvdata(pdev, info);
  743. mutex_init(&info->lock);
  744. /* Register with power supply class */
  745. charger_cfg.drv_data = info;
  746. info->psy_usb = power_supply_register(&pdev->dev, &axp288_charger_desc,
  747. &charger_cfg);
  748. if (IS_ERR(info->psy_usb)) {
  749. dev_err(&pdev->dev, "failed to register power supply charger\n");
  750. ret = PTR_ERR(info->psy_usb);
  751. goto psy_reg_failed;
  752. }
  753. /* Register for OTG notification */
  754. INIT_WORK(&info->otg.work, axp288_charger_otg_evt_worker);
  755. info->otg.id_nb.notifier_call = axp288_charger_handle_otg_evt;
  756. ret = extcon_register_interest(&info->otg.cable, NULL, "USB-Host",
  757. &info->otg.id_nb);
  758. if (ret)
  759. dev_warn(&pdev->dev, "failed to register otg notifier\n");
  760. if (info->otg.cable.edev)
  761. info->otg.id_short = extcon_get_cable_state(
  762. info->otg.cable.edev, "USB-Host");
  763. /* Register charger interrupts */
  764. for (i = 0; i < CHRG_INTR_END; i++) {
  765. pirq = platform_get_irq(info->pdev, i);
  766. info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
  767. if (info->irq[i] < 0) {
  768. dev_warn(&info->pdev->dev,
  769. "failed to get virtual interrupt=%d\n", pirq);
  770. ret = info->irq[i];
  771. goto intr_reg_failed;
  772. }
  773. ret = devm_request_threaded_irq(&info->pdev->dev, info->irq[i],
  774. NULL, axp288_charger_irq_thread_handler,
  775. IRQF_ONESHOT, info->pdev->name, info);
  776. if (ret) {
  777. dev_err(&pdev->dev, "failed to request interrupt=%d\n",
  778. info->irq[i]);
  779. goto intr_reg_failed;
  780. }
  781. }
  782. charger_init_hw_regs(info);
  783. return 0;
  784. intr_reg_failed:
  785. if (info->otg.cable.edev)
  786. extcon_unregister_interest(&info->otg.cable);
  787. power_supply_unregister(info->psy_usb);
  788. psy_reg_failed:
  789. extcon_unregister_notifier(info->cable.edev, EXTCON_NONE, &info->cable.nb);
  790. return ret;
  791. }
  792. static int axp288_charger_remove(struct platform_device *pdev)
  793. {
  794. struct axp288_chrg_info *info = dev_get_drvdata(&pdev->dev);
  795. if (info->otg.cable.edev)
  796. extcon_unregister_interest(&info->otg.cable);
  797. extcon_unregister_notifier(info->cable.edev, EXTCON_NONE, &info->cable.nb);
  798. power_supply_unregister(info->psy_usb);
  799. return 0;
  800. }
  801. static struct platform_driver axp288_charger_driver = {
  802. .probe = axp288_charger_probe,
  803. .remove = axp288_charger_remove,
  804. .driver = {
  805. .name = "axp288_charger",
  806. },
  807. };
  808. module_platform_driver(axp288_charger_driver);
  809. MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
  810. MODULE_DESCRIPTION("X-power AXP288 Charger Driver");
  811. MODULE_LICENSE("GPL v2");