xref: /linux/drivers/power/supply/axp20x_battery.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
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 
20 #include <linux/err.h>
21 #include <linux/interrupt.h>
22 #include <linux/irq.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/of_device.h>
26 #include <linux/platform_device.h>
27 #include <linux/power_supply.h>
28 #include <linux/regmap.h>
29 #include <linux/slab.h>
30 #include <linux/time.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/consumer.h>
33 #include <linux/mfd/axp20x.h>
34 
35 #define AXP20X_PWR_STATUS_BAT_CHARGING	BIT(2)
36 
37 #define AXP20X_PWR_OP_BATT_PRESENT	BIT(5)
38 #define AXP20X_PWR_OP_BATT_ACTIVATED	BIT(3)
39 
40 #define AXP209_FG_PERCENT		GENMASK(6, 0)
41 #define AXP22X_FG_VALID			BIT(7)
42 
43 #define AXP20X_CHRG_CTRL1_TGT_VOLT	GENMASK(6, 5)
44 #define AXP20X_CHRG_CTRL1_TGT_4_1V	(0 << 5)
45 #define AXP20X_CHRG_CTRL1_TGT_4_15V	(1 << 5)
46 #define AXP20X_CHRG_CTRL1_TGT_4_2V	(2 << 5)
47 #define AXP20X_CHRG_CTRL1_TGT_4_36V	(3 << 5)
48 
49 #define AXP22X_CHRG_CTRL1_TGT_4_22V	(1 << 5)
50 #define AXP22X_CHRG_CTRL1_TGT_4_24V	(3 << 5)
51 
52 #define AXP20X_CHRG_CTRL1_TGT_CURR	GENMASK(3, 0)
53 
54 #define AXP20X_V_OFF_MASK		GENMASK(2, 0)
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 	u8 axp_id;
64 };
65 
66 static int axp20x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
67 					  int *val)
68 {
69 	int ret, reg;
70 
71 	ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
72 	if (ret)
73 		return ret;
74 
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 
92 	return 0;
93 }
94 
95 static int axp22x_battery_get_max_voltage(struct axp20x_batt_ps *axp20x_batt,
96 					  int *val)
97 {
98 	int ret, reg;
99 
100 	ret = regmap_read(axp20x_batt->regmap, AXP20X_CHRG_CTRL1, &reg);
101 	if (ret)
102 		return ret;
103 
104 	switch (reg & AXP20X_CHRG_CTRL1_TGT_VOLT) {
105 	case AXP20X_CHRG_CTRL1_TGT_4_1V:
106 		*val = 4100000;
107 		break;
108 	case AXP20X_CHRG_CTRL1_TGT_4_2V:
109 		*val = 4200000;
110 		break;
111 	case AXP22X_CHRG_CTRL1_TGT_4_22V:
112 		*val = 4220000;
113 		break;
114 	case AXP22X_CHRG_CTRL1_TGT_4_24V:
115 		*val = 4240000;
116 		break;
117 	default:
118 		return -EINVAL;
119 	}
120 
121 	return 0;
122 }
123 
124 static void raw_to_constant_charge_current(struct axp20x_batt_ps *axp, int *val)
125 {
126 	if (axp->axp_id == AXP209_ID)
127 		*val = *val * 100000 + 300000;
128 	else
129 		*val = *val * 150000 + 300000;
130 }
131 
132 static int axp20x_get_constant_charge_current(struct axp20x_batt_ps *axp,
133 					      int *val)
134 {
135 	int ret;
136 
137 	ret = regmap_read(axp->regmap, AXP20X_CHRG_CTRL1, val);
138 	if (ret)
139 		return ret;
140 
141 	*val &= AXP20X_CHRG_CTRL1_TGT_CURR;
142 
143 	raw_to_constant_charge_current(axp, val);
144 
145 	return 0;
146 }
147 
148 static int axp20x_battery_get_prop(struct power_supply *psy,
149 				   enum power_supply_property psp,
150 				   union power_supply_propval *val)
151 {
152 	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
153 	struct iio_channel *chan;
154 	int ret = 0, reg, val1;
155 
156 	switch (psp) {
157 	case POWER_SUPPLY_PROP_PRESENT:
158 	case POWER_SUPPLY_PROP_ONLINE:
159 		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
160 				  &reg);
161 		if (ret)
162 			return ret;
163 
164 		val->intval = !!(reg & AXP20X_PWR_OP_BATT_PRESENT);
165 		break;
166 
167 	case POWER_SUPPLY_PROP_STATUS:
168 		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
169 				  &reg);
170 		if (ret)
171 			return ret;
172 
173 		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING) {
174 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
175 			return 0;
176 		}
177 
178 		ret = iio_read_channel_processed(axp20x_batt->batt_dischrg_i,
179 						 &val1);
180 		if (ret)
181 			return ret;
182 
183 		if (val1) {
184 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
185 			return 0;
186 		}
187 
188 		ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &val1);
189 		if (ret)
190 			return ret;
191 
192 		/*
193 		 * Fuel Gauge data takes 7 bits but the stored value seems to be
194 		 * directly the raw percentage without any scaling to 7 bits.
195 		 */
196 		if ((val1 & AXP209_FG_PERCENT) == 100)
197 			val->intval = POWER_SUPPLY_STATUS_FULL;
198 		else
199 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
200 		break;
201 
202 	case POWER_SUPPLY_PROP_HEALTH:
203 		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
204 				  &val1);
205 		if (ret)
206 			return ret;
207 
208 		if (val1 & AXP20X_PWR_OP_BATT_ACTIVATED) {
209 			val->intval = POWER_SUPPLY_HEALTH_DEAD;
210 			return 0;
211 		}
212 
213 		val->intval = POWER_SUPPLY_HEALTH_GOOD;
214 		break;
215 
216 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
217 		ret = axp20x_get_constant_charge_current(axp20x_batt,
218 							 &val->intval);
219 		if (ret)
220 			return ret;
221 		break;
222 
223 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
224 		val->intval = AXP20X_CHRG_CTRL1_TGT_CURR;
225 		raw_to_constant_charge_current(axp20x_batt, &val->intval);
226 
227 		break;
228 
229 	case POWER_SUPPLY_PROP_CURRENT_NOW:
230 		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_INPUT_STATUS,
231 				  &reg);
232 		if (ret)
233 			return ret;
234 
235 		if (reg & AXP20X_PWR_STATUS_BAT_CHARGING)
236 			chan = axp20x_batt->batt_chrg_i;
237 		else
238 			chan = axp20x_batt->batt_dischrg_i;
239 
240 		ret = iio_read_channel_processed(chan, &val->intval);
241 		if (ret)
242 			return ret;
243 
244 		/* IIO framework gives mA but Power Supply framework gives uA */
245 		val->intval *= 1000;
246 		break;
247 
248 	case POWER_SUPPLY_PROP_CAPACITY:
249 		/* When no battery is present, return capacity is 100% */
250 		ret = regmap_read(axp20x_batt->regmap, AXP20X_PWR_OP_MODE,
251 				  &reg);
252 		if (ret)
253 			return ret;
254 
255 		if (!(reg & AXP20X_PWR_OP_BATT_PRESENT)) {
256 			val->intval = 100;
257 			return 0;
258 		}
259 
260 		ret = regmap_read(axp20x_batt->regmap, AXP20X_FG_RES, &reg);
261 		if (ret)
262 			return ret;
263 
264 		if (axp20x_batt->axp_id == AXP221_ID &&
265 		    !(reg & AXP22X_FG_VALID))
266 			return -EINVAL;
267 
268 		/*
269 		 * Fuel Gauge data takes 7 bits but the stored value seems to be
270 		 * directly the raw percentage without any scaling to 7 bits.
271 		 */
272 		val->intval = reg & AXP209_FG_PERCENT;
273 		break;
274 
275 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
276 		if (axp20x_batt->axp_id == AXP209_ID)
277 			return axp20x_battery_get_max_voltage(axp20x_batt,
278 							      &val->intval);
279 		return axp22x_battery_get_max_voltage(axp20x_batt,
280 						      &val->intval);
281 
282 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
283 		ret = regmap_read(axp20x_batt->regmap, AXP20X_V_OFF, &reg);
284 		if (ret)
285 			return ret;
286 
287 		val->intval = 2600000 + 100000 * (reg & AXP20X_V_OFF_MASK);
288 		break;
289 
290 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
291 		ret = iio_read_channel_processed(axp20x_batt->batt_v,
292 						 &val->intval);
293 		if (ret)
294 			return ret;
295 
296 		/* IIO framework gives mV but Power Supply framework gives uV */
297 		val->intval *= 1000;
298 		break;
299 
300 	default:
301 		return -EINVAL;
302 	}
303 
304 	return 0;
305 }
306 
307 static int axp20x_battery_set_max_voltage(struct axp20x_batt_ps *axp20x_batt,
308 					  int val)
309 {
310 	switch (val) {
311 	case 4100000:
312 		val = AXP20X_CHRG_CTRL1_TGT_4_1V;
313 		break;
314 
315 	case 4150000:
316 		if (axp20x_batt->axp_id == AXP221_ID)
317 			return -EINVAL;
318 
319 		val = AXP20X_CHRG_CTRL1_TGT_4_15V;
320 		break;
321 
322 	case 4200000:
323 		val = AXP20X_CHRG_CTRL1_TGT_4_2V;
324 		break;
325 
326 	default:
327 		/*
328 		 * AXP20x max voltage can be set to 4.36V and AXP22X max voltage
329 		 * can be set to 4.22V and 4.24V, but these voltages are too
330 		 * high for Lithium based batteries (AXP PMICs are supposed to
331 		 * be used with these kinds of battery).
332 		 */
333 		return -EINVAL;
334 	}
335 
336 	return regmap_update_bits(axp20x_batt->regmap, AXP20X_CHRG_CTRL1,
337 				  AXP20X_CHRG_CTRL1_TGT_VOLT, val);
338 }
339 
340 static int axp20x_set_constant_charge_current(struct axp20x_batt_ps *axp_batt,
341 					      int charge_current)
342 {
343 	if (axp_batt->axp_id == AXP209_ID)
344 		charge_current = (charge_current - 300000) / 100000;
345 	else
346 		charge_current = (charge_current - 300000) / 150000;
347 
348 	if (charge_current > AXP20X_CHRG_CTRL1_TGT_CURR || charge_current < 0)
349 		return -EINVAL;
350 
351 	return regmap_update_bits(axp_batt->regmap, AXP20X_CHRG_CTRL1,
352 				  AXP20X_CHRG_CTRL1_TGT_CURR, charge_current);
353 }
354 
355 static int axp20x_set_voltage_min_design(struct axp20x_batt_ps *axp_batt,
356 					 int min_voltage)
357 {
358 	int val1 = (min_voltage - 2600000) / 100000;
359 
360 	if (val1 < 0 || val1 > AXP20X_V_OFF_MASK)
361 		return -EINVAL;
362 
363 	return regmap_update_bits(axp_batt->regmap, AXP20X_V_OFF,
364 				  AXP20X_V_OFF_MASK, val1);
365 }
366 
367 static int axp20x_battery_set_prop(struct power_supply *psy,
368 				   enum power_supply_property psp,
369 				   const union power_supply_propval *val)
370 {
371 	struct axp20x_batt_ps *axp20x_batt = power_supply_get_drvdata(psy);
372 
373 	switch (psp) {
374 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
375 		return axp20x_set_voltage_min_design(axp20x_batt, val->intval);
376 
377 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
378 		return axp20x_battery_set_max_voltage(axp20x_batt, val->intval);
379 
380 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
381 		return axp20x_set_constant_charge_current(axp20x_batt,
382 							  val->intval);
383 
384 	default:
385 		return -EINVAL;
386 	}
387 }
388 
389 static enum power_supply_property axp20x_battery_props[] = {
390 	POWER_SUPPLY_PROP_PRESENT,
391 	POWER_SUPPLY_PROP_ONLINE,
392 	POWER_SUPPLY_PROP_STATUS,
393 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
394 	POWER_SUPPLY_PROP_CURRENT_NOW,
395 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
396 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
397 	POWER_SUPPLY_PROP_HEALTH,
398 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
399 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
400 	POWER_SUPPLY_PROP_CAPACITY,
401 };
402 
403 static int axp20x_battery_prop_writeable(struct power_supply *psy,
404 					 enum power_supply_property psp)
405 {
406 	return psp == POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN ||
407 	       psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
408 	       psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT;
409 }
410 
411 static const struct power_supply_desc axp20x_batt_ps_desc = {
412 	.name = "axp20x-battery",
413 	.type = POWER_SUPPLY_TYPE_BATTERY,
414 	.properties = axp20x_battery_props,
415 	.num_properties = ARRAY_SIZE(axp20x_battery_props),
416 	.property_is_writeable = axp20x_battery_prop_writeable,
417 	.get_property = axp20x_battery_get_prop,
418 	.set_property = axp20x_battery_set_prop,
419 };
420 
421 static const struct of_device_id axp20x_battery_ps_id[] = {
422 	{
423 		.compatible = "x-powers,axp209-battery-power-supply",
424 		.data = (void *)AXP209_ID,
425 	}, {
426 		.compatible = "x-powers,axp221-battery-power-supply",
427 		.data = (void *)AXP221_ID,
428 	}, { /* sentinel */ },
429 };
430 MODULE_DEVICE_TABLE(of, axp20x_battery_ps_id);
431 
432 static int axp20x_power_probe(struct platform_device *pdev)
433 {
434 	struct axp20x_batt_ps *axp20x_batt;
435 	struct power_supply_config psy_cfg = {};
436 
437 	if (!of_device_is_available(pdev->dev.of_node))
438 		return -ENODEV;
439 
440 	axp20x_batt = devm_kzalloc(&pdev->dev, sizeof(*axp20x_batt),
441 				   GFP_KERNEL);
442 	if (!axp20x_batt)
443 		return -ENOMEM;
444 
445 	axp20x_batt->dev = &pdev->dev;
446 
447 	axp20x_batt->batt_v = devm_iio_channel_get(&pdev->dev, "batt_v");
448 	if (IS_ERR(axp20x_batt->batt_v)) {
449 		if (PTR_ERR(axp20x_batt->batt_v) == -ENODEV)
450 			return -EPROBE_DEFER;
451 		return PTR_ERR(axp20x_batt->batt_v);
452 	}
453 
454 	axp20x_batt->batt_chrg_i = devm_iio_channel_get(&pdev->dev,
455 							"batt_chrg_i");
456 	if (IS_ERR(axp20x_batt->batt_chrg_i)) {
457 		if (PTR_ERR(axp20x_batt->batt_chrg_i) == -ENODEV)
458 			return -EPROBE_DEFER;
459 		return PTR_ERR(axp20x_batt->batt_chrg_i);
460 	}
461 
462 	axp20x_batt->batt_dischrg_i = devm_iio_channel_get(&pdev->dev,
463 							   "batt_dischrg_i");
464 	if (IS_ERR(axp20x_batt->batt_dischrg_i)) {
465 		if (PTR_ERR(axp20x_batt->batt_dischrg_i) == -ENODEV)
466 			return -EPROBE_DEFER;
467 		return PTR_ERR(axp20x_batt->batt_dischrg_i);
468 	}
469 
470 	axp20x_batt->regmap = dev_get_regmap(pdev->dev.parent, NULL);
471 	platform_set_drvdata(pdev, axp20x_batt);
472 
473 	psy_cfg.drv_data = axp20x_batt;
474 	psy_cfg.of_node = pdev->dev.of_node;
475 
476 	axp20x_batt->axp_id = (uintptr_t)of_device_get_match_data(&pdev->dev);
477 
478 	axp20x_batt->batt = devm_power_supply_register(&pdev->dev,
479 						       &axp20x_batt_ps_desc,
480 						       &psy_cfg);
481 	if (IS_ERR(axp20x_batt->batt)) {
482 		dev_err(&pdev->dev, "failed to register power supply: %ld\n",
483 			PTR_ERR(axp20x_batt->batt));
484 		return PTR_ERR(axp20x_batt->batt);
485 	}
486 
487 	return 0;
488 }
489 
490 static struct platform_driver axp20x_batt_driver = {
491 	.probe    = axp20x_power_probe,
492 	.driver   = {
493 		.name  = "axp20x-battery-power-supply",
494 		.of_match_table = axp20x_battery_ps_id,
495 	},
496 };
497 
498 module_platform_driver(axp20x_batt_driver);
499 
500 MODULE_DESCRIPTION("Battery power supply driver for AXP20X and AXP22X PMICs");
501 MODULE_AUTHOR("Quentin Schulz <quentin.schulz@free-electrons.com>");
502 MODULE_LICENSE("GPL");
503