axp288_charger.c 27 KB

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