axp288_charger.c 24 KB

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