axp288_charger.c 24 KB

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