axp20x_battery.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * Battery power supply driver for X-Powers AXP20X and AXP22X PMICs
  3. *
  4. * Copyright 2016 Free Electrons NextThing Co.
  5. * Quentin Schulz <[email protected]>
  6. *
  7. * This driver is based on a previous upstreaming attempt by:
  8. * Bruno Prémont <[email protected]>
  9. *
  10. * This file is subject to the terms and conditions of the GNU General
  11. * Public License. See the file "COPYING" in the main directory of this
  12. * archive for more details.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/err.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/power_supply.h>
  27. #include <linux/regmap.h>
  28. #include <linux/slab.h>
  29. #include <linux/time.h>
  30. #include <linux/iio/iio.h>
  31. #include <linux/iio/consumer.h>
  32. #include <linux/mfd/axp20x.h>
  33. #define AXP20X_PWR_STATUS_BAT_CHARGING BIT(2)
  34. #define AXP20X_PWR_OP_BATT_PRESENT BIT(5)
  35. #define AXP20X_PWR_OP_BATT_ACTIVATED BIT(3)
  36. #define AXP209_FG_PERCENT GENMASK(6, 0)
  37. #define AXP22X_FG_VALID BIT(7)
  38. #define AXP20X_CHRG_CTRL1_ENABLE BIT(7)
  39. #define AXP20X_CHRG_CTRL1_TGT_VOLT GENMASK(6, 5)
  40. #define AXP20X_CHRG_CTRL1_TGT_4_1V (0 << 5)
  41. #define AXP20X_CHRG_CTRL1_TGT_4_15V (1 << 5)
  42. #define AXP20X_CHRG_CTRL1_TGT_4_2V (2 << 5)
  43. #define AXP20X_CHRG_CTRL1_TGT_4_36V (3 << 5)
  44. #define AXP22X_CHRG_CTRL1_TGT_4_22V (1 << 5)
  45. #define AXP22X_CHRG_CTRL1_TGT_4_24V (3 << 5)
  46. #define AXP813_CHRG_CTRL1_TGT_4_35V (3 << 5)
  47. #define AXP20X_CHRG_CTRL1_TGT_CURR GENMASK(3, 0)
  48. #define AXP20X_V_OFF_MASK GENMASK(2, 0)
  49. struct axp20x_batt_ps;
  50. struct axp_data {
  51. int ccc_scale;
  52. int ccc_offset;
  53. bool has_fg_valid;
  54. int (*get_max_voltage)(struct axp20x_batt_ps *batt, int *val);
  55. int (*set_max_voltage)(struct axp20x_batt_ps *batt, int val);
  56. };
  57. struct axp20x_batt_ps {
  58. struct regmap *regmap;
  59. struct power_supply *batt;
  60. struct device *dev;
  61. struct iio_channel *batt_chrg_i;
  62. struct iio_channel *batt_dischrg_i;
  63. struct iio_channel *batt_v;
  64. /* Maximum constant charge current */
  65. unsigned int max_ccc;
  66. const struct axp_data *data;
  67. };
  68. static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
  69. int *val)
  70. {
  71. int ret, reg;
  72. ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
  73. if (ret)
  74. return ret;
  75. switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
  76. case AXP20X_CHRG_CTRL1_TGT_4_1V:
  77. *val = 4100000;
  78. break;
  79. case AXP20X_CHRG_CTRL1_TGT_4_15V:
  80. *val = 4150000;
  81. break;
  82. case AXP20X_CHRG_CTRL1_TGT_4_2V:
  83. *val = 4200000;
  84. break;
  85. case AXP20X_CHRG_CTRL1_TGT_4_36V:
  86. *val = 4360000;
  87. break;
  88. default:
  89. return -EINVAL;
  90. }
  91. return 0;
  92. }
  93. static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
  94. int *val)
  95. {
  96. int ret, reg;
  97. ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
  98. if (ret)
  99. return ret;
  100. switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
  101. case AXP20X_CHRG_CTRL1_TGT_4_1V:
  102. *val = 4100000;
  103. break;
  104. case AXP20X_CHRG_CTRL1_TGT_4_2V:
  105. *val = 4200000;
  106. break;
  107. case AXP22X_CHRG_CTRL1_TGT_4_22V:
  108. *val = 4220000;
  109. break;
  110. case AXP22X_CHRG_CTRL1_TGT_4_24V:
  111. *val = 4240000;
  112. break;
  113. default:
  114. return -EINVAL;
  115. }
  116. return 0;
  117. }
  118. static int axp813_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
  119. int *val)
  120. {
  121. int ret, reg;
  122. ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
  123. if (ret)
  124. return ret;
  125. switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
  126. case AXP20X_CHRG_CTRL1_TGT_4_1V:
  127. *val = 4100000;
  128. break;
  129. case AXP20X_CHRG_CTRL1_TGT_4_15V:
  130. *val = 4150000;
  131. break;
  132. case AXP20X_CHRG_CTRL1_TGT_4_2V:
  133. *val = 4200000;
  134. break;
  135. case AXP813_CHRG_CTRL1_TGT_4_35V:
  136. *val = 4350000;
  137. break;
  138. default:
  139. return -EINVAL;
  140. }
  141. return 0;
  142. }
  143. static int axp20x_get_constant_charge_current(struct axp20x_batt_ps *axp,
  144. int *val)
  145. {
  146. int ret;
  147. ret = regmap_read(axp->regmap, AXP20X_CHRG_CTRL1, val);
  148. if (ret)
  149. return ret;
  150. *val &= AXP20X_CHRG_CTRL1_TGT_CURR;
  151. *val = *val * axp->data->ccc_scale + axp->data->ccc_offset;
  152. return 0;
  153. }
  154. static int axp20x_battery_get_prop(struct power_supply *psy,
  155. enum power_supply_property psp,
  156. union power_supply_propval *val)
  157. {
  158. struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
  159. int ret = 0, reg, val1;
  160. switch (psp) {
  161. case POWER_SUPPLY_PROP_PRESENT:
  162. case POWER_SUPPLY_PROP_ONLINE:
  163. ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
  164. &reg);
  165. if (ret)
  166. return ret;
  167. val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
  168. break;
  169. case POWER_SUPPLY_PROP_STATUS:
  170. ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
  171. &reg);
  172. if (ret)
  173. return ret;
  174. if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
  175. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  176. return 0;
  177. }
  178. ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
  179. &val1);
  180. if (ret)
  181. return ret;
  182. if (val1) {
  183. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  184. return 0;
  185. }
  186. ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
  187. if (ret)
  188. return ret;
  189. /*
  190. * Fuel Gauge data takes 7 bits but the stored value seems to be
  191. * directly the raw percentage without any scaling to 7 bits.
  192. */
  193. if ((val1 & AXP209_FG_PERCENT) == 100)
  194. val->intval = POWER_SUPPLY_STATUS_FULL;
  195. else
  196. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  197. break;
  198. case POWER_SUPPLY_PROP_HEALTH:
  199. ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
  200. &val1);
  201. if (ret)
  202. return ret;
  203. if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
  204. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  205. return 0;
  206. }
  207. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  208. break;
  209. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  210. ret = axp20x_get_constant_charge_current(axp20x_batt,
  211. &val->intval);
  212. if (ret)
  213. return ret;
  214. break;
  215. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  216. val->intval = axp20x_batt->max_ccc;
  217. break;
  218. case POWER_SUPPLY_PROP_CURRENT_NOW:
  219. ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
  220. &reg);
  221. if (ret)
  222. return ret;
  223. if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
  224. ret = iio_read_channel_processed(axp20x_batt->batt_chrg_i, &val->intval);
  225. } else {
  226. ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i, &val1);
  227. val->intval = -val1;
  228. }
  229. if (ret)
  230. return ret;
  231. /* IIO framework gives mA but Power Supply framework gives uA */
  232. val->intval *= 1000;
  233. break;
  234. case POWER_SUPPLY_PROP_CAPACITY:
  235. /* When no battery is present, return capacity is 100% */
  236. ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
  237. &reg);
  238. if (ret)
  239. return ret;
  240. if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
  241. val->intval = 100;
  242. return 0;
  243. }
  244. ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &reg);
  245. if (ret)
  246. return ret;
  247. if (axp20x_batt->data->has_fg_valid && !(reg & AXP22X_FG_VALID))
  248. return -EINVAL;
  249. /*
  250. * Fuel Gauge data takes 7 bits but the stored value seems to be
  251. * directly the raw percentage without any scaling to 7 bits.
  252. */
  253. val->intval = reg & AXP209_FG_PERCENT;
  254. break;
  255. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  256. return axp20x_batt->data->get_max_voltage(axp20x_batt,
  257. &val->intval);
  258. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  259. ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, &reg);
  260. if (ret)
  261. return ret;
  262. val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
  263. break;
  264. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  265. ret = iio_read_channel_processed(axp20x_batt->batt_v,
  266. &val->intval);
  267. if (ret)
  268. return ret;
  269. /* IIO framework gives mV but Power Supply framework gives uV */
  270. val->intval *= 1000;
  271. break;
  272. default:
  273. return -EINVAL;
  274. }
  275. return 0;
  276. }
  277. static int axp22x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
  278. int val)
  279. {
  280. switch (val) {
  281. case 4100000:
  282. val = AXP20X_CHRG_CTRL1_TGT_4_1V;
  283. break;
  284. case 4200000:
  285. val = AXP20X_CHRG_CTRL1_TGT_4_2V;
  286. break;
  287. default:
  288. /*
  289. * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
  290. * can be set to 4.22V and 4.24V, but these voltages are too
  291. * high for Lithium based batteries (AXP PMICs are supposed to
  292. * be used with these kinds of battery).
  293. */
  294. return -EINVAL;
  295. }
  296. return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
  297. AXP20X_CHRG_CTRL1_TGT_VOLT, val);
  298. }
  299. static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
  300. int val)
  301. {
  302. switch (val) {
  303. case 4100000:
  304. val = AXP20X_CHRG_CTRL1_TGT_4_1V;
  305. break;
  306. case 4150000:
  307. val = AXP20X_CHRG_CTRL1_TGT_4_15V;
  308. break;
  309. case 4200000:
  310. val = AXP20X_CHRG_CTRL1_TGT_4_2V;
  311. break;
  312. default:
  313. /*
  314. * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
  315. * can be set to 4.22V and 4.24V, but these voltages are too
  316. * high for Lithium based batteries (AXP PMICs are supposed to
  317. * be used with these kinds of battery).
  318. */
  319. return -EINVAL;
  320. }
  321. return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
  322. AXP20X_CHRG_CTRL1_TGT_VOLT, val);
  323. }
  324. static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
  325. int charge_current)
  326. {
  327. if (charge_current > axp_batt->max_ccc)
  328. return -EINVAL;
  329. charge_current = (charge_current - axp_batt->data->ccc_offset) /
  330. axp_batt->data->ccc_scale;
  331. if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
  332. return -EINVAL;
  333. return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
  334. AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
  335. }
  336. static int axp20x_set_max_constant_charge_current(struct axp20x_batt_ps *axp,
  337. int charge_current)
  338. {
  339. bool lower_max = false;
  340. charge_current = (charge_current - axp->data->ccc_offset) /
  341. axp->data->ccc_scale;
  342. if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
  343. return -EINVAL;
  344. charge_current = charge_current * axp->data->ccc_scale +
  345. axp->data->ccc_offset;
  346. if (charge_current > axp->max_ccc)
  347. dev_warn(axp->dev,
  348. "Setting max constant charge current higher than previously defined. Note that increasing the constant charge current may damage your battery.\n");
  349. else
  350. lower_max = true;
  351. axp->max_ccc = charge_current;
  352. if (lower_max) {
  353. int current_cc;
  354. axp20x_get_constant_charge_current(axp, &current_cc);
  355. if (current_cc > charge_current)
  356. axp20x_set_constant_charge_current(axp, charge_current);
  357. }
  358. return 0;
  359. }
  360. static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
  361. int min_voltage)
  362. {
  363. int val1 = (min_voltage - 2600000) / 100000;
  364. if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
  365. return -EINVAL;
  366. return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
  367. AXP20X_V_OFF_MASK, val1);
  368. }
  369. static int axp20x_battery_set_prop(struct power_supply *psy,
  370. enum power_supply_property psp,
  371. const union power_supply_propval *val)
  372. {
  373. struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
  374. switch (psp) {
  375. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  376. return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
  377. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  378. return axp20x_batt->data->set_max_voltage(axp20x_batt, val->intval);
  379. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  380. return axp20x_set_constant_charge_current(axp20x_batt,
  381. val->intval);
  382. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  383. return axp20x_set_max_constant_charge_current(axp20x_batt,
  384. val->intval);
  385. case POWER_SUPPLY_PROP_STATUS:
  386. switch (val->intval) {
  387. case POWER_SUPPLY_STATUS_CHARGING:
  388. return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
  389. AXP20X_CHRG_CTRL1_ENABLE, AXP20X_CHRG_CTRL1_ENABLE);
  390. case POWER_SUPPLY_STATUS_DISCHARGING:
  391. case POWER_SUPPLY_STATUS_NOT_CHARGING:
  392. return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
  393. AXP20X_CHRG_CTRL1_ENABLE, 0);
  394. }
  395. fallthrough;
  396. default:
  397. return -EINVAL;
  398. }
  399. }
  400. static enum power_supply_property axp20x_battery_props[] = {
  401. POWER_SUPPLY_PROP_PRESENT,
  402. POWER_SUPPLY_PROP_ONLINE,
  403. POWER_SUPPLY_PROP_STATUS,
  404. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  405. POWER_SUPPLY_PROP_CURRENT_NOW,
  406. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  407. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  408. POWER_SUPPLY_PROP_HEALTH,
  409. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  410. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  411. POWER_SUPPLY_PROP_CAPACITY,
  412. };
  413. static int axp20x_battery_prop_writeable(struct power_supply *psy,
  414. enum power_supply_property psp)
  415. {
  416. return psp == POWER_SUPPLY_PROP_STATUS ||
  417. psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
  418. psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
  419. psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT ||
  420. psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX;
  421. }
  422. static const struct power_supply_desc axp20x_batt_ps_desc = {
  423. .name = "axp20x-battery",
  424. .type = POWER_SUPPLY_TYPE_BATTERY,
  425. .properties = axp20x_battery_props,
  426. .num_properties = ARRAY_SIZE(axp20x_battery_props),
  427. .property_is_writeable = axp20x_battery_prop_writeable,
  428. .get_property = axp20x_battery_get_prop,
  429. .set_property = axp20x_battery_set_prop,
  430. };
  431. static const struct axp_data axp209_data = {
  432. .ccc_scale = 100000,
  433. .ccc_offset = 300000,
  434. .get_max_voltage = axp20x_battery_get_max_voltage,
  435. .set_max_voltage = axp20x_battery_set_max_voltage,
  436. };
  437. static const struct axp_data axp221_data = {
  438. .ccc_scale = 150000,
  439. .ccc_offset = 300000,
  440. .has_fg_valid = true,
  441. .get_max_voltage = axp22x_battery_get_max_voltage,
  442. .set_max_voltage = axp22x_battery_set_max_voltage,
  443. };
  444. static const struct axp_data axp813_data = {
  445. .ccc_scale = 200000,
  446. .ccc_offset = 200000,
  447. .has_fg_valid = true,
  448. .get_max_voltage = axp813_battery_get_max_voltage,
  449. .set_max_voltage = axp20x_battery_set_max_voltage,
  450. };
  451. static const struct of_device_id axp20x_battery_ps_id[] = {
  452. {
  453. .compatible = "x-powers,axp209-battery-power-supply",
  454. .data = (void *)&axp209_data,
  455. }, {
  456. .compatible = "x-powers,axp221-battery-power-supply",
  457. .data = (void *)&axp221_data,
  458. }, {
  459. .compatible = "x-powers,axp813-battery-power-supply",
  460. .data = (void *)&axp813_data,
  461. }, { /* sentinel */ },
  462. };
  463. MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
  464. static int axp20x_power_probe(struct platform_device *pdev)
  465. {
  466. struct axp20x_batt_ps *axp20x_batt;
  467. struct power_supply_config psy_cfg = {};
  468. struct power_supply_battery_info *info;
  469. struct device *dev = &pdev->dev;
  470. if (!of_device_is_available(pdev->dev.of_node))
  471. return -ENODEV;
  472. axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
  473. GFP_KERNEL);
  474. if (!axp20x_batt)
  475. return -ENOMEM;
  476. axp20x_batt->dev = &pdev->dev;
  477. axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
  478. if (IS_ERR(axp20x_batt->batt_v)) {
  479. if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
  480. return -EPROBE_DEFER;
  481. return PTR_ERR(axp20x_batt->batt_v);
  482. }
  483. axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
  484. "batt_chrg_i");
  485. if (IS_ERR(axp20x_batt->batt_chrg_i)) {
  486. if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
  487. return -EPROBE_DEFER;
  488. return PTR_ERR(axp20x_batt->batt_chrg_i);
  489. }
  490. axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
  491. "batt_dischrg_i");
  492. if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
  493. if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
  494. return -EPROBE_DEFER;
  495. return PTR_ERR(axp20x_batt->batt_dischrg_i);
  496. }
  497. axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  498. platform_set_drvdata(pdev, axp20x_batt);
  499. psy_cfg.drv_data = axp20x_batt;
  500. psy_cfg.of_node = pdev->dev.of_node;
  501. axp20x_batt->data = (struct axp_data *)of_device_get_match_data(dev);
  502. axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
  503. &axp20x_batt_ps_desc,
  504. &psy_cfg);
  505. if (IS_ERR(axp20x_batt->batt)) {
  506. dev_err(&pdev->dev, "failed to register power supply: %ld\n",
  507. PTR_ERR(axp20x_batt->batt));
  508. return PTR_ERR(axp20x_batt->batt);
  509. }
  510. if (!power_supply_get_battery_info(axp20x_batt->batt, &info)) {
  511. int vmin = info->voltage_min_design_uv;
  512. int ccc = info->constant_charge_current_max_ua;
  513. if (vmin > 0 && axp20x_set_voltage_min_design(axp20x_batt,
  514. vmin))
  515. dev_err(&pdev->dev,
  516. "couldn't set voltage_min_design\n");
  517. /* Set max to unverified value to be able to set CCC */
  518. axp20x_batt->max_ccc = ccc;
  519. if (ccc <= 0 || axp20x_set_constant_charge_current(axp20x_batt,
  520. ccc)) {
  521. dev_err(&pdev->dev,
  522. "couldn't set constant charge current from DT: fallback to minimum value\n");
  523. ccc = 300000;
  524. axp20x_batt->max_ccc = ccc;
  525. axp20x_set_constant_charge_current(axp20x_batt, ccc);
  526. }
  527. }
  528. /*
  529. * Update max CCC to a valid value if battery info is present or set it
  530. * to current register value by default.
  531. */
  532. axp20x_get_constant_charge_current(axp20x_batt,
  533. &axp20x_batt->max_ccc);
  534. return 0;
  535. }
  536. static struct platform_driver axp20x_batt_driver = {
  537. .probe = axp20x_power_probe,
  538. .driver = {
  539. .name = "axp20x-battery-power-supply",
  540. .of_match_table = axp20x_battery_ps_id,
  541. },
  542. };
  543. module_platform_driver(axp20x_batt_driver);
  544. MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
  545. MODULE_AUTHOR("Quentin Schulz <[email protected]>");
  546. MODULE_LICENSE("GPL");