axp288_charger.c 26 KB

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