axp288_charger.c 23 KB

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