axp20x_battery.c 16 KB

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