axp288_charger.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * axp288_charger.c - X-power AXP288 PMIC Charger driver
  3. *
  4. * Copyright (C) 2016-2017 Hans de Goede <hdegoede@redhat.com>
  5. * Copyright (C) 2014 Intel Corporation
  6. * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.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. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/acpi.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/usb/otg.h>
  25. #include <linux/notifier.h>
  26. #include <linux/power_supply.h>
  27. #include <linux/property.h>
  28. #include <linux/mfd/axp20x.h>
  29. #include <linux/extcon.h>
  30. #include <linux/dmi.h>
  31. #define PS_STAT_VBUS_TRIGGER (1 << 0)
  32. #define PS_STAT_BAT_CHRG_DIR (1 << 2)
  33. #define PS_STAT_VBAT_ABOVE_VHOLD (1 << 3)
  34. #define PS_STAT_VBUS_VALID (1 << 4)
  35. #define PS_STAT_VBUS_PRESENT (1 << 5)
  36. #define CHRG_STAT_BAT_SAFE_MODE (1 << 3)
  37. #define CHRG_STAT_BAT_VALID (1 << 4)
  38. #define CHRG_STAT_BAT_PRESENT (1 << 5)
  39. #define CHRG_STAT_CHARGING (1 << 6)
  40. #define CHRG_STAT_PMIC_OTP (1 << 7)
  41. #define VBUS_ISPOUT_CUR_LIM_MASK 0x03
  42. #define VBUS_ISPOUT_CUR_LIM_BIT_POS 0
  43. #define VBUS_ISPOUT_CUR_LIM_900MA 0x0 /* 900mA */
  44. #define VBUS_ISPOUT_CUR_LIM_1500MA 0x1 /* 1500mA */
  45. #define VBUS_ISPOUT_CUR_LIM_2000MA 0x2 /* 2000mA */
  46. #define VBUS_ISPOUT_CUR_NO_LIM 0x3 /* 2500mA */
  47. #define VBUS_ISPOUT_VHOLD_SET_MASK 0x31
  48. #define VBUS_ISPOUT_VHOLD_SET_BIT_POS 0x3
  49. #define VBUS_ISPOUT_VHOLD_SET_OFFSET 4000 /* 4000mV */
  50. #define VBUS_ISPOUT_VHOLD_SET_LSB_RES 100 /* 100mV */
  51. #define VBUS_ISPOUT_VHOLD_SET_4300MV 0x3 /* 4300mV */
  52. #define VBUS_ISPOUT_VBUS_PATH_DIS (1 << 7)
  53. #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
  54. #define CHRG_CCCV_CC_BIT_POS 0
  55. #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
  56. #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
  57. #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */
  58. #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
  59. #define CHRG_CCCV_CV_BIT_POS 5
  60. #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */
  61. #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */
  62. #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */
  63. #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */
  64. #define CHRG_CCCV_CHG_EN (1 << 7)
  65. #define CNTL2_CC_TIMEOUT_MASK 0x3 /* 2 bits */
  66. #define CNTL2_CC_TIMEOUT_OFFSET 6 /* 6 Hrs */
  67. #define CNTL2_CC_TIMEOUT_LSB_RES 2 /* 2 Hrs */
  68. #define CNTL2_CC_TIMEOUT_12HRS 0x3 /* 12 Hrs */
  69. #define CNTL2_CHGLED_TYPEB (1 << 4)
  70. #define CNTL2_CHG_OUT_TURNON (1 << 5)
  71. #define CNTL2_PC_TIMEOUT_MASK 0xC0
  72. #define CNTL2_PC_TIMEOUT_OFFSET 40 /* 40 mins */
  73. #define CNTL2_PC_TIMEOUT_LSB_RES 10 /* 10 mins */
  74. #define CNTL2_PC_TIMEOUT_70MINS 0x3
  75. #define CHRG_ILIM_TEMP_LOOP_EN (1 << 3)
  76. #define CHRG_VBUS_ILIM_MASK 0xf0
  77. #define CHRG_VBUS_ILIM_BIT_POS 4
  78. #define CHRG_VBUS_ILIM_100MA 0x0 /* 100mA */
  79. #define CHRG_VBUS_ILIM_500MA 0x1 /* 500mA */
  80. #define CHRG_VBUS_ILIM_900MA 0x2 /* 900mA */
  81. #define CHRG_VBUS_ILIM_1500MA 0x3 /* 1500mA */
  82. #define CHRG_VBUS_ILIM_2000MA 0x4 /* 2000mA */
  83. #define CHRG_VBUS_ILIM_2500MA 0x5 /* 2500mA */
  84. #define CHRG_VBUS_ILIM_3000MA 0x6 /* 3000mA */
  85. #define CHRG_VBUS_ILIM_3500MA 0x7 /* 3500mA */
  86. #define CHRG_VBUS_ILIM_4000MA 0x8 /* 4000mA */
  87. #define CHRG_VLTFC_0C 0xA5 /* 0 DegC */
  88. #define CHRG_VHTFC_45C 0x1F /* 45 DegC */
  89. #define FG_CNTL_OCV_ADJ_EN (1 << 3)
  90. #define CV_4100MV 4100 /* 4100mV */
  91. #define CV_4150MV 4150 /* 4150mV */
  92. #define CV_4200MV 4200 /* 4200mV */
  93. #define CV_4350MV 4350 /* 4350mV */
  94. #define AXP288_EXTCON_DEV_NAME "axp288_extcon"
  95. #define USB_HOST_EXTCON_HID "INT3496"
  96. #define USB_HOST_EXTCON_NAME "INT3496:00"
  97. enum {
  98. VBUS_OV_IRQ = 0,
  99. CHARGE_DONE_IRQ,
  100. CHARGE_CHARGING_IRQ,
  101. BAT_SAFE_QUIT_IRQ,
  102. BAT_SAFE_ENTER_IRQ,
  103. QCBTU_IRQ,
  104. CBTU_IRQ,
  105. QCBTO_IRQ,
  106. CBTO_IRQ,
  107. CHRG_INTR_END,
  108. };
  109. struct axp288_chrg_info {
  110. struct platform_device *pdev;
  111. struct regmap *regmap;
  112. struct regmap_irq_chip_data *regmap_irqc;
  113. int irq[CHRG_INTR_END];
  114. struct power_supply *psy_usb;
  115. /* OTG/Host mode */
  116. struct {
  117. struct work_struct work;
  118. struct extcon_dev *cable;
  119. struct notifier_block id_nb;
  120. bool id_short;
  121. } otg;
  122. /* SDP/CDP/DCP USB charging cable notifications */
  123. struct {
  124. struct extcon_dev *edev;
  125. struct notifier_block nb;
  126. struct work_struct work;
  127. } cable;
  128. int cc;
  129. int cv;
  130. int max_cc;
  131. int max_cv;
  132. };
  133. static inline int axp288_charger_set_cc(struct axp288_chrg_info *info, int cc)
  134. {
  135. u8 reg_val;
  136. int ret;
  137. if (cc < CHRG_CCCV_CC_OFFSET)
  138. cc = CHRG_CCCV_CC_OFFSET;
  139. else if (cc > info->max_cc)
  140. cc = info->max_cc;
  141. reg_val = (cc - CHRG_CCCV_CC_OFFSET) / CHRG_CCCV_CC_LSB_RES;
  142. cc = (reg_val * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
  143. reg_val = reg_val << CHRG_CCCV_CC_BIT_POS;
  144. ret = regmap_update_bits(info->regmap,
  145. AXP20X_CHRG_CTRL1,
  146. CHRG_CCCV_CC_MASK, reg_val);
  147. if (ret >= 0)
  148. info->cc = cc;
  149. return ret;
  150. }
  151. static inline int axp288_charger_set_cv(struct axp288_chrg_info *info, int cv)
  152. {
  153. u8 reg_val;
  154. int ret;
  155. if (cv <= CV_4100MV) {
  156. reg_val = CHRG_CCCV_CV_4100MV;
  157. cv = CV_4100MV;
  158. } else if (cv <= CV_4150MV) {
  159. reg_val = CHRG_CCCV_CV_4150MV;
  160. cv = CV_4150MV;
  161. } else if (cv <= CV_4200MV) {
  162. reg_val = CHRG_CCCV_CV_4200MV;
  163. cv = CV_4200MV;
  164. } else {
  165. reg_val = CHRG_CCCV_CV_4350MV;
  166. cv = CV_4350MV;
  167. }
  168. reg_val = reg_val << CHRG_CCCV_CV_BIT_POS;
  169. ret = regmap_update_bits(info->regmap,
  170. AXP20X_CHRG_CTRL1,
  171. CHRG_CCCV_CV_MASK, reg_val);
  172. if (ret >= 0)
  173. info->cv = cv;
  174. return ret;
  175. }
  176. static int axp288_charger_get_vbus_inlmt(struct axp288_chrg_info *info)
  177. {
  178. unsigned int val;
  179. int ret;
  180. ret = regmap_read(info->regmap, AXP20X_CHRG_BAK_CTRL, &val);
  181. if (ret < 0)
  182. return ret;
  183. val >>= CHRG_VBUS_ILIM_BIT_POS;
  184. switch (val) {
  185. case CHRG_VBUS_ILIM_100MA:
  186. return 100000;
  187. case CHRG_VBUS_ILIM_500MA:
  188. return 500000;
  189. case CHRG_VBUS_ILIM_900MA:
  190. return 900000;
  191. case CHRG_VBUS_ILIM_1500MA:
  192. return 1500000;
  193. case CHRG_VBUS_ILIM_2000MA:
  194. return 2000000;
  195. case CHRG_VBUS_ILIM_2500MA:
  196. return 2500000;
  197. case CHRG_VBUS_ILIM_3000MA:
  198. return 3000000;
  199. case CHRG_VBUS_ILIM_3500MA:
  200. return 3500000;
  201. default:
  202. /* All b1xxx values map to 4000 mA */
  203. return 4000000;
  204. }
  205. }
  206. static inline int axp288_charger_set_vbus_inlmt(struct axp288_chrg_info *info,
  207. int inlmt)
  208. {
  209. int ret;
  210. u8 reg_val;
  211. if (inlmt >= 4000000)
  212. reg_val = CHRG_VBUS_ILIM_4000MA << CHRG_VBUS_ILIM_BIT_POS;
  213. else if (inlmt >= 3500000)
  214. reg_val = CHRG_VBUS_ILIM_3500MA << CHRG_VBUS_ILIM_BIT_POS;
  215. else if (inlmt >= 3000000)
  216. reg_val = CHRG_VBUS_ILIM_3000MA << CHRG_VBUS_ILIM_BIT_POS;
  217. else if (inlmt >= 2500000)
  218. reg_val = CHRG_VBUS_ILIM_2500MA << CHRG_VBUS_ILIM_BIT_POS;
  219. else if (inlmt >= 2000000)
  220. reg_val = CHRG_VBUS_ILIM_2000MA << CHRG_VBUS_ILIM_BIT_POS;
  221. else if (inlmt >= 1500000)
  222. reg_val = CHRG_VBUS_ILIM_1500MA << CHRG_VBUS_ILIM_BIT_POS;
  223. else if (inlmt >= 900000)
  224. reg_val = CHRG_VBUS_ILIM_900MA << CHRG_VBUS_ILIM_BIT_POS;
  225. else if (inlmt >= 500000)
  226. reg_val = CHRG_VBUS_ILIM_500MA << CHRG_VBUS_ILIM_BIT_POS;
  227. else
  228. reg_val = CHRG_VBUS_ILIM_100MA << CHRG_VBUS_ILIM_BIT_POS;
  229. ret = regmap_update_bits(info->regmap, AXP20X_CHRG_BAK_CTRL,
  230. CHRG_VBUS_ILIM_MASK, reg_val);
  231. if (ret < 0)
  232. dev_err(&info->pdev->dev, "charger BAK control %d\n", ret);
  233. return ret;
  234. }
  235. static int axp288_charger_vbus_path_select(struct axp288_chrg_info *info,
  236. bool enable)
  237. {
  238. int ret;
  239. if (enable)
  240. ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
  241. VBUS_ISPOUT_VBUS_PATH_DIS, 0);
  242. else
  243. ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
  244. VBUS_ISPOUT_VBUS_PATH_DIS, VBUS_ISPOUT_VBUS_PATH_DIS);
  245. if (ret < 0)
  246. dev_err(&info->pdev->dev, "axp288 vbus path select %d\n", ret);
  247. return ret;
  248. }
  249. static int axp288_charger_enable_charger(struct axp288_chrg_info *info,
  250. bool enable)
  251. {
  252. int ret;
  253. if (enable)
  254. ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
  255. CHRG_CCCV_CHG_EN, CHRG_CCCV_CHG_EN);
  256. else
  257. ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
  258. CHRG_CCCV_CHG_EN, 0);
  259. if (ret < 0)
  260. dev_err(&info->pdev->dev, "axp288 enable charger %d\n", ret);
  261. return ret;
  262. }
  263. static int axp288_charger_is_present(struct axp288_chrg_info *info)
  264. {
  265. int ret, present = 0;
  266. unsigned int val;
  267. ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
  268. if (ret < 0)
  269. return ret;
  270. if (val & PS_STAT_VBUS_PRESENT)
  271. present = 1;
  272. return present;
  273. }
  274. static int axp288_charger_is_online(struct axp288_chrg_info *info)
  275. {
  276. int ret, online = 0;
  277. unsigned int val;
  278. ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
  279. if (ret < 0)
  280. return ret;
  281. if (val & PS_STAT_VBUS_VALID)
  282. online = 1;
  283. return online;
  284. }
  285. static int axp288_get_charger_health(struct axp288_chrg_info *info)
  286. {
  287. int ret, pwr_stat, chrg_stat;
  288. int health = POWER_SUPPLY_HEALTH_UNKNOWN;
  289. unsigned int val;
  290. ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
  291. if ((ret < 0) || !(val & PS_STAT_VBUS_PRESENT))
  292. goto health_read_fail;
  293. else
  294. pwr_stat = val;
  295. ret = regmap_read(info->regmap, AXP20X_PWR_OP_MODE, &val);
  296. if (ret < 0)
  297. goto health_read_fail;
  298. else
  299. chrg_stat = val;
  300. if (!(pwr_stat & PS_STAT_VBUS_VALID))
  301. health = POWER_SUPPLY_HEALTH_DEAD;
  302. else if (chrg_stat & CHRG_STAT_PMIC_OTP)
  303. health = POWER_SUPPLY_HEALTH_OVERHEAT;
  304. else if (chrg_stat & CHRG_STAT_BAT_SAFE_MODE)
  305. health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
  306. else
  307. health = POWER_SUPPLY_HEALTH_GOOD;
  308. health_read_fail:
  309. return health;
  310. }
  311. static int axp288_charger_usb_set_property(struct power_supply *psy,
  312. enum power_supply_property psp,
  313. const union power_supply_propval *val)
  314. {
  315. struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
  316. int ret = 0;
  317. int scaled_val;
  318. switch (psp) {
  319. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  320. scaled_val = min(val->intval, info->max_cc);
  321. scaled_val = DIV_ROUND_CLOSEST(scaled_val, 1000);
  322. ret = axp288_charger_set_cc(info, scaled_val);
  323. if (ret < 0)
  324. dev_warn(&info->pdev->dev, "set charge current failed\n");
  325. break;
  326. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  327. scaled_val = min(val->intval, info->max_cv);
  328. scaled_val = DIV_ROUND_CLOSEST(scaled_val, 1000);
  329. ret = axp288_charger_set_cv(info, scaled_val);
  330. if (ret < 0)
  331. dev_warn(&info->pdev->dev, "set charge voltage failed\n");
  332. break;
  333. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  334. ret = axp288_charger_set_vbus_inlmt(info, val->intval);
  335. if (ret < 0)
  336. dev_warn(&info->pdev->dev, "set input current limit failed\n");
  337. break;
  338. default:
  339. ret = -EINVAL;
  340. }
  341. return ret;
  342. }
  343. static int axp288_charger_usb_get_property(struct power_supply *psy,
  344. enum power_supply_property psp,
  345. union power_supply_propval *val)
  346. {
  347. struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
  348. int ret;
  349. switch (psp) {
  350. case POWER_SUPPLY_PROP_PRESENT:
  351. /* Check for OTG case first */
  352. if (info->otg.id_short) {
  353. val->intval = 0;
  354. break;
  355. }
  356. ret = axp288_charger_is_present(info);
  357. if (ret < 0)
  358. return ret;
  359. val->intval = ret;
  360. break;
  361. case POWER_SUPPLY_PROP_ONLINE:
  362. /* Check for OTG case first */
  363. if (info->otg.id_short) {
  364. val->intval = 0;
  365. break;
  366. }
  367. ret = axp288_charger_is_online(info);
  368. if (ret < 0)
  369. return ret;
  370. val->intval = ret;
  371. break;
  372. case POWER_SUPPLY_PROP_HEALTH:
  373. val->intval = axp288_get_charger_health(info);
  374. break;
  375. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  376. val->intval = info->cc * 1000;
  377. break;
  378. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  379. val->intval = info->max_cc * 1000;
  380. break;
  381. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  382. val->intval = info->cv * 1000;
  383. break;
  384. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  385. val->intval = info->max_cv * 1000;
  386. break;
  387. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  388. ret = axp288_charger_get_vbus_inlmt(info);
  389. if (ret < 0)
  390. return ret;
  391. val->intval = ret;
  392. break;
  393. default:
  394. return -EINVAL;
  395. }
  396. return 0;
  397. }
  398. static int axp288_charger_property_is_writeable(struct power_supply *psy,
  399. enum power_supply_property psp)
  400. {
  401. int ret;
  402. switch (psp) {
  403. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  404. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  405. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  406. ret = 1;
  407. break;
  408. default:
  409. ret = 0;
  410. }
  411. return ret;
  412. }
  413. static enum power_supply_property axp288_usb_props[] = {
  414. POWER_SUPPLY_PROP_PRESENT,
  415. POWER_SUPPLY_PROP_ONLINE,
  416. POWER_SUPPLY_PROP_TYPE,
  417. POWER_SUPPLY_PROP_HEALTH,
  418. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  419. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  420. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  421. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  422. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  423. };
  424. static const struct power_supply_desc axp288_charger_desc = {
  425. .name = "axp288_charger",
  426. .type = POWER_SUPPLY_TYPE_USB,
  427. .properties = axp288_usb_props,
  428. .num_properties = ARRAY_SIZE(axp288_usb_props),
  429. .get_property = axp288_charger_usb_get_property,
  430. .set_property = axp288_charger_usb_set_property,
  431. .property_is_writeable = axp288_charger_property_is_writeable,
  432. };
  433. static irqreturn_t axp288_charger_irq_thread_handler(int irq, void *dev)
  434. {
  435. struct axp288_chrg_info *info = dev;
  436. int i;
  437. for (i = 0; i < CHRG_INTR_END; i++) {
  438. if (info->irq[i] == irq)
  439. break;
  440. }
  441. if (i >= CHRG_INTR_END) {
  442. dev_warn(&info->pdev->dev, "spurious interrupt!!\n");
  443. return IRQ_NONE;
  444. }
  445. switch (i) {
  446. case VBUS_OV_IRQ:
  447. dev_dbg(&info->pdev->dev, "VBUS Over Voltage INTR\n");
  448. break;
  449. case CHARGE_DONE_IRQ:
  450. dev_dbg(&info->pdev->dev, "Charging Done INTR\n");
  451. break;
  452. case CHARGE_CHARGING_IRQ:
  453. dev_dbg(&info->pdev->dev, "Start Charging IRQ\n");
  454. break;
  455. case BAT_SAFE_QUIT_IRQ:
  456. dev_dbg(&info->pdev->dev,
  457. "Quit Safe Mode(restart timer) Charging IRQ\n");
  458. break;
  459. case BAT_SAFE_ENTER_IRQ:
  460. dev_dbg(&info->pdev->dev,
  461. "Enter Safe Mode(timer expire) Charging IRQ\n");
  462. break;
  463. case QCBTU_IRQ:
  464. dev_dbg(&info->pdev->dev,
  465. "Quit Battery Under Temperature(CHRG) INTR\n");
  466. break;
  467. case CBTU_IRQ:
  468. dev_dbg(&info->pdev->dev,
  469. "Hit Battery Under Temperature(CHRG) INTR\n");
  470. break;
  471. case QCBTO_IRQ:
  472. dev_dbg(&info->pdev->dev,
  473. "Quit Battery Over Temperature(CHRG) INTR\n");
  474. break;
  475. case CBTO_IRQ:
  476. dev_dbg(&info->pdev->dev,
  477. "Hit Battery Over Temperature(CHRG) INTR\n");
  478. break;
  479. default:
  480. dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n");
  481. goto out;
  482. }
  483. power_supply_changed(info->psy_usb);
  484. out:
  485. return IRQ_HANDLED;
  486. }
  487. /*
  488. * The HP Pavilion x2 10 series comes in a number of variants:
  489. * Bay Trail SoC + AXP288 PMIC, Micro-USB, DMI_BOARD_NAME: "8021"
  490. * Bay Trail SoC + AXP288 PMIC, Type-C, DMI_BOARD_NAME: "815D"
  491. * Cherry Trail SoC + AXP288 PMIC, Type-C, DMI_BOARD_NAME: "813E"
  492. * Cherry Trail SoC + TI PMIC, Type-C, DMI_BOARD_NAME: "827C" or "82F4"
  493. *
  494. * The variants with the AXP288 + Type-C connector are all kinds of special:
  495. *
  496. * 1. They use a Type-C connector which the AXP288 does not support, so when
  497. * using a Type-C charger it is not recognized. Unlike most AXP288 devices,
  498. * this model actually has mostly working ACPI AC / Battery code, the ACPI code
  499. * "solves" this by simply setting the input_current_limit to 3A.
  500. * There are still some issues with the ACPI code, so we use this native driver,
  501. * and to solve the charging not working (500mA is not enough) issue we hardcode
  502. * the 3A input_current_limit like the ACPI code does.
  503. *
  504. * 2. If no charger is connected the machine boots with the vbus-path disabled.
  505. * Normally this is done when a 5V boost converter is active to avoid the PMIC
  506. * trying to charge from the 5V boost converter's output. This is done when
  507. * an OTG host cable is inserted and the ID pin on the micro-B receptacle is
  508. * pulled low and the ID pin has an ACPI event handler associated with it
  509. * which re-enables the vbus-path when the ID pin is pulled high when the
  510. * OTG host cable is removed. The Type-C connector has no ID pin, there is
  511. * no ID pin handler and there appears to be no 5V boost converter, so we
  512. * end up not charging because the vbus-path is disabled, until we unplug
  513. * the charger which automatically clears the vbus-path disable bit and then
  514. * on the second plug-in of the adapter we start charging. To solve the not
  515. * charging on first charger plugin we unconditionally enable the vbus-path at
  516. * probe on this model, which is safe since there is no 5V boost converter.
  517. */
  518. static const struct dmi_system_id axp288_hp_x2_dmi_ids[] = {
  519. {
  520. .matches = {
  521. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  522. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
  523. DMI_EXACT_MATCH(DMI_BOARD_NAME, "815D"),
  524. },
  525. },
  526. {
  527. .matches = {
  528. DMI_EXACT_MATCH(DMI_SYS_VENDOR, "HP"),
  529. DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
  530. DMI_EXACT_MATCH(DMI_BOARD_NAME, "813E"),
  531. },
  532. },
  533. {} /* Terminating entry */
  534. };
  535. static void axp288_charger_extcon_evt_worker(struct work_struct *work)
  536. {
  537. struct axp288_chrg_info *info =
  538. container_of(work, struct axp288_chrg_info, cable.work);
  539. int ret, current_limit;
  540. struct extcon_dev *edev = info->cable.edev;
  541. unsigned int val;
  542. ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
  543. if (ret < 0) {
  544. dev_err(&info->pdev->dev, "Error reading status (%d)\n", ret);
  545. return;
  546. }
  547. /* Offline? Disable charging and bail */
  548. if (!(val & PS_STAT_VBUS_VALID)) {
  549. dev_dbg(&info->pdev->dev, "USB charger disconnected\n");
  550. axp288_charger_enable_charger(info, false);
  551. power_supply_changed(info->psy_usb);
  552. return;
  553. }
  554. /* Determine cable/charger type */
  555. if (dmi_check_system(axp288_hp_x2_dmi_ids)) {
  556. /* See comment above axp288_hp_x2_dmi_ids declaration */
  557. dev_dbg(&info->pdev->dev, "HP X2 with Type-C, setting inlmt to 3A\n");
  558. current_limit = 3000000;
  559. } else if (extcon_get_state(edev, EXTCON_CHG_USB_SDP) > 0) {
  560. dev_dbg(&info->pdev->dev, "USB SDP charger is connected\n");
  561. current_limit = 500000;
  562. } else if (extcon_get_state(edev, EXTCON_CHG_USB_CDP) > 0) {
  563. dev_dbg(&info->pdev->dev, "USB CDP charger is connected\n");
  564. current_limit = 1500000;
  565. } else if (extcon_get_state(edev, EXTCON_CHG_USB_DCP) > 0) {
  566. dev_dbg(&info->pdev->dev, "USB DCP charger is connected\n");
  567. current_limit = 2000000;
  568. } else {
  569. /* Charger type detection still in progress, bail. */
  570. return;
  571. }
  572. /* Set vbus current limit first, then enable charger */
  573. ret = axp288_charger_set_vbus_inlmt(info, current_limit);
  574. if (ret == 0)
  575. axp288_charger_enable_charger(info, true);
  576. else
  577. dev_err(&info->pdev->dev,
  578. "error setting current limit (%d)\n", ret);
  579. power_supply_changed(info->psy_usb);
  580. }
  581. static int axp288_charger_handle_cable_evt(struct notifier_block *nb,
  582. unsigned long event, void *param)
  583. {
  584. struct axp288_chrg_info *info =
  585. container_of(nb, struct axp288_chrg_info, cable.nb);
  586. schedule_work(&info->cable.work);
  587. return NOTIFY_OK;
  588. }
  589. static void axp288_charger_otg_evt_worker(struct work_struct *work)
  590. {
  591. struct axp288_chrg_info *info =
  592. container_of(work, struct axp288_chrg_info, otg.work);
  593. struct extcon_dev *edev = info->otg.cable;
  594. int ret, usb_host = extcon_get_state(edev, EXTCON_USB_HOST);
  595. dev_dbg(&info->pdev->dev, "external connector USB-Host is %s\n",
  596. usb_host ? "attached" : "detached");
  597. /*
  598. * Set usb_id_short flag to avoid running charger detection logic
  599. * in case usb host.
  600. */
  601. info->otg.id_short = usb_host;
  602. /* Disable VBUS path before enabling the 5V boost */
  603. ret = axp288_charger_vbus_path_select(info, !info->otg.id_short);
  604. if (ret < 0)
  605. dev_warn(&info->pdev->dev, "vbus path disable failed\n");
  606. }
  607. static int axp288_charger_handle_otg_evt(struct notifier_block *nb,
  608. unsigned long event, void *param)
  609. {
  610. struct axp288_chrg_info *info =
  611. container_of(nb, struct axp288_chrg_info, otg.id_nb);
  612. schedule_work(&info->otg.work);
  613. return NOTIFY_OK;
  614. }
  615. static int 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_err(&info->pdev->dev, "register(%x) write error(%d)\n",
  623. AXP20X_V_LTF_CHRG, ret);
  624. return ret;
  625. }
  626. ret = regmap_write(info->regmap, AXP20X_V_HTF_CHRG, CHRG_VHTFC_45C);
  627. if (ret < 0) {
  628. dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
  629. AXP20X_V_HTF_CHRG, ret);
  630. return ret;
  631. }
  632. /* Do not turn-off charger o/p after charge cycle ends */
  633. ret = regmap_update_bits(info->regmap,
  634. AXP20X_CHRG_CTRL2,
  635. CNTL2_CHG_OUT_TURNON, CNTL2_CHG_OUT_TURNON);
  636. if (ret < 0) {
  637. dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
  638. AXP20X_CHRG_CTRL2, ret);
  639. return ret;
  640. }
  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_err(&info->pdev->dev, "register(%x) write error(%d)\n",
  647. AXP20X_CHRG_CTRL1, ret);
  648. return ret;
  649. }
  650. /* Disable OCV-SOC curve calibration */
  651. ret = regmap_update_bits(info->regmap,
  652. AXP20X_CC_CTRL,
  653. FG_CNTL_OCV_ADJ_EN, 0);
  654. if (ret < 0) {
  655. dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
  656. AXP20X_CC_CTRL, ret);
  657. return ret;
  658. }
  659. if (dmi_check_system(axp288_hp_x2_dmi_ids)) {
  660. /* See comment above axp288_hp_x2_dmi_ids declaration */
  661. ret = axp288_charger_vbus_path_select(info, true);
  662. if (ret < 0)
  663. return ret;
  664. }
  665. /* Read current charge voltage and current limit */
  666. ret = regmap_read(info->regmap, AXP20X_CHRG_CTRL1, &val);
  667. if (ret < 0) {
  668. dev_err(&info->pdev->dev, "register(%x) read error(%d)\n",
  669. AXP20X_CHRG_CTRL1, ret);
  670. return ret;
  671. }
  672. /* Determine charge voltage */
  673. cv = (val & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS;
  674. switch (cv) {
  675. case CHRG_CCCV_CV_4100MV:
  676. info->cv = CV_4100MV;
  677. break;
  678. case CHRG_CCCV_CV_4150MV:
  679. info->cv = CV_4150MV;
  680. break;
  681. case CHRG_CCCV_CV_4200MV:
  682. info->cv = CV_4200MV;
  683. break;
  684. case CHRG_CCCV_CV_4350MV:
  685. info->cv = CV_4350MV;
  686. break;
  687. }
  688. /* Determine charge current limit */
  689. cc = (val & CHRG_CCCV_CC_MASK) >> CHRG_CCCV_CC_BIT_POS;
  690. cc = (cc * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
  691. info->cc = cc;
  692. /*
  693. * Do not allow the user to configure higher settings then those
  694. * set by the firmware
  695. */
  696. info->max_cv = info->cv;
  697. info->max_cc = info->cc;
  698. return 0;
  699. }
  700. static void axp288_charger_cancel_work(void *data)
  701. {
  702. struct axp288_chrg_info *info = data;
  703. cancel_work_sync(&info->otg.work);
  704. cancel_work_sync(&info->cable.work);
  705. }
  706. static int axp288_charger_probe(struct platform_device *pdev)
  707. {
  708. int ret, i, pirq;
  709. struct axp288_chrg_info *info;
  710. struct device *dev = &pdev->dev;
  711. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  712. struct power_supply_config charger_cfg = {};
  713. unsigned int val;
  714. /*
  715. * On some devices the fuelgauge and charger parts of the axp288 are
  716. * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
  717. */
  718. ret = regmap_read(axp20x->regmap, AXP20X_CC_CTRL, &val);
  719. if (ret < 0)
  720. return ret;
  721. if (val == 0)
  722. return -ENODEV;
  723. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  724. if (!info)
  725. return -ENOMEM;
  726. info->pdev = pdev;
  727. info->regmap = axp20x->regmap;
  728. info->regmap_irqc = axp20x->regmap_irqc;
  729. info->cable.edev = extcon_get_extcon_dev(AXP288_EXTCON_DEV_NAME);
  730. if (info->cable.edev == NULL) {
  731. dev_dbg(&pdev->dev, "%s is not ready, probe deferred\n",
  732. AXP288_EXTCON_DEV_NAME);
  733. return -EPROBE_DEFER;
  734. }
  735. if (acpi_dev_present(USB_HOST_EXTCON_HID, NULL, -1)) {
  736. info->otg.cable = extcon_get_extcon_dev(USB_HOST_EXTCON_NAME);
  737. if (info->otg.cable == NULL) {
  738. dev_dbg(dev, "EXTCON_USB_HOST is not ready, probe deferred\n");
  739. return -EPROBE_DEFER;
  740. }
  741. dev_info(&pdev->dev,
  742. "Using " USB_HOST_EXTCON_HID " extcon for usb-id\n");
  743. }
  744. platform_set_drvdata(pdev, info);
  745. ret = charger_init_hw_regs(info);
  746. if (ret)
  747. return ret;
  748. /* Register with power supply class */
  749. charger_cfg.drv_data = info;
  750. info->psy_usb = devm_power_supply_register(dev, &axp288_charger_desc,
  751. &charger_cfg);
  752. if (IS_ERR(info->psy_usb)) {
  753. ret = PTR_ERR(info->psy_usb);
  754. dev_err(dev, "failed to register power supply: %d\n", ret);
  755. return ret;
  756. }
  757. /* Cancel our work on cleanup, register this before the notifiers */
  758. ret = devm_add_action(dev, axp288_charger_cancel_work, info);
  759. if (ret)
  760. return ret;
  761. /* Register for extcon notification */
  762. INIT_WORK(&info->cable.work, axp288_charger_extcon_evt_worker);
  763. info->cable.nb.notifier_call = axp288_charger_handle_cable_evt;
  764. ret = devm_extcon_register_notifier_all(dev, info->cable.edev,
  765. &info->cable.nb);
  766. if (ret) {
  767. dev_err(dev, "failed to register cable extcon notifier\n");
  768. return ret;
  769. }
  770. schedule_work(&info->cable.work);
  771. /* Register for OTG notification */
  772. INIT_WORK(&info->otg.work, axp288_charger_otg_evt_worker);
  773. info->otg.id_nb.notifier_call = axp288_charger_handle_otg_evt;
  774. if (info->otg.cable) {
  775. ret = devm_extcon_register_notifier(&pdev->dev, info->otg.cable,
  776. EXTCON_USB_HOST, &info->otg.id_nb);
  777. if (ret) {
  778. dev_err(dev, "failed to register EXTCON_USB_HOST notifier\n");
  779. return ret;
  780. }
  781. schedule_work(&info->otg.work);
  782. }
  783. /* Register charger interrupts */
  784. for (i = 0; i < CHRG_INTR_END; i++) {
  785. pirq = platform_get_irq(info->pdev, i);
  786. if (pirq < 0) {
  787. dev_err(&pdev->dev, "Failed to get IRQ: %d\n", pirq);
  788. return pirq;
  789. }
  790. info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
  791. if (info->irq[i] < 0) {
  792. dev_warn(&info->pdev->dev,
  793. "failed to get virtual interrupt=%d\n", pirq);
  794. return info->irq[i];
  795. }
  796. ret = devm_request_threaded_irq(&info->pdev->dev, info->irq[i],
  797. NULL, axp288_charger_irq_thread_handler,
  798. IRQF_ONESHOT, info->pdev->name, info);
  799. if (ret) {
  800. dev_err(&pdev->dev, "failed to request interrupt=%d\n",
  801. info->irq[i]);
  802. return ret;
  803. }
  804. }
  805. return 0;
  806. }
  807. static const struct platform_device_id axp288_charger_id_table[] = {
  808. { .name = "axp288_charger" },
  809. {},
  810. };
  811. MODULE_DEVICE_TABLE(platform, axp288_charger_id_table);
  812. static struct platform_driver axp288_charger_driver = {
  813. .probe = axp288_charger_probe,
  814. .id_table = axp288_charger_id_table,
  815. .driver = {
  816. .name = "axp288_charger",
  817. },
  818. };
  819. module_platform_driver(axp288_charger_driver);
  820. MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
  821. MODULE_DESCRIPTION("X-power AXP288 Charger Driver");
  822. MODULE_LICENSE("GPL v2");