1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Battery driver for the Ingenic JZ47xx SoCs
4  * Copyright (c) 2019 Artur Rojek <contact@artur-rojek.eu>
5  *
6  * based on drivers/power/supply/jz4740-battery.c
7  */
8 
9 #include <linux/iio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/power_supply.h>
14 #include <linux/property.h>
15 
16 struct ingenic_battery {
17 	struct device *dev;
18 	struct iio_channel *channel;
19 	struct power_supply_desc desc;
20 	struct power_supply *battery;
21 	struct power_supply_battery_info info;
22 };
23 
24 static int ingenic_battery_get_property(struct power_supply *psy,
25 					enum power_supply_property psp,
26 					union power_supply_propval *val)
27 {
28 	struct ingenic_battery *bat = power_supply_get_drvdata(psy);
29 	struct power_supply_battery_info *info = &bat->info;
30 	int ret;
31 
32 	switch (psp) {
33 	case POWER_SUPPLY_PROP_HEALTH:
34 		ret = iio_read_channel_processed(bat->channel, &val->intval);
35 		val->intval *= 1000;
36 		if (val->intval < info->voltage_min_design_uv)
37 			val->intval = POWER_SUPPLY_HEALTH_DEAD;
38 		else if (val->intval > info->voltage_max_design_uv)
39 			val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
40 		else
41 			val->intval = POWER_SUPPLY_HEALTH_GOOD;
42 		return ret;
43 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
44 		ret = iio_read_channel_processed(bat->channel, &val->intval);
45 		val->intval *= 1000;
46 		return ret;
47 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
48 		val->intval = info->voltage_min_design_uv;
49 		return 0;
50 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
51 		val->intval = info->voltage_max_design_uv;
52 		return 0;
53 	default:
54 		return -EINVAL;
55 	};
56 }
57 
58 /* Set the most appropriate IIO channel voltage reference scale
59  * based on the battery's max voltage.
60  */
61 static int ingenic_battery_set_scale(struct ingenic_battery *bat)
62 {
63 	const int *scale_raw;
64 	int scale_len, scale_type, best_idx = -1, best_mV, max_raw, i, ret;
65 	u64 max_mV;
66 
67 	ret = iio_read_max_channel_raw(bat->channel, &max_raw);
68 	if (ret) {
69 		dev_err(bat->dev, "Unable to read max raw channel value\n");
70 		return ret;
71 	}
72 
73 	ret = iio_read_avail_channel_attribute(bat->channel, &scale_raw,
74 					       &scale_type, &scale_len,
75 					       IIO_CHAN_INFO_SCALE);
76 	if (ret < 0) {
77 		dev_err(bat->dev, "Unable to read channel avail scale\n");
78 		return ret;
79 	}
80 	if (ret != IIO_AVAIL_LIST || scale_type != IIO_VAL_FRACTIONAL_LOG2)
81 		return -EINVAL;
82 
83 	max_mV = bat->info.voltage_max_design_uv / 1000;
84 
85 	for (i = 0; i < scale_len; i += 2) {
86 		u64 scale_mV = (max_raw * scale_raw[i]) >> scale_raw[i + 1];
87 
88 		if (scale_mV < max_mV)
89 			continue;
90 
91 		if (best_idx >= 0 && scale_mV > best_mV)
92 			continue;
93 
94 		best_mV = scale_mV;
95 		best_idx = i;
96 	}
97 
98 	if (best_idx < 0) {
99 		dev_err(bat->dev, "Unable to find matching voltage scale\n");
100 		return -EINVAL;
101 	}
102 
103 	return iio_write_channel_attribute(bat->channel,
104 					   scale_raw[best_idx],
105 					   scale_raw[best_idx + 1],
106 					   IIO_CHAN_INFO_SCALE);
107 }
108 
109 static enum power_supply_property ingenic_battery_properties[] = {
110 	POWER_SUPPLY_PROP_HEALTH,
111 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
112 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
113 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
114 };
115 
116 static int ingenic_battery_probe(struct platform_device *pdev)
117 {
118 	struct device *dev = &pdev->dev;
119 	struct ingenic_battery *bat;
120 	struct power_supply_config psy_cfg = {};
121 	struct power_supply_desc *desc;
122 	int ret;
123 
124 	bat = devm_kzalloc(dev, sizeof(*bat), GFP_KERNEL);
125 	if (!bat)
126 		return -ENOMEM;
127 
128 	bat->dev = dev;
129 	bat->channel = devm_iio_channel_get(dev, "battery");
130 	if (IS_ERR(bat->channel))
131 		return PTR_ERR(bat->channel);
132 
133 	desc = &bat->desc;
134 	desc->name = "jz-battery";
135 	desc->type = POWER_SUPPLY_TYPE_BATTERY;
136 	desc->properties = ingenic_battery_properties;
137 	desc->num_properties = ARRAY_SIZE(ingenic_battery_properties);
138 	desc->get_property = ingenic_battery_get_property;
139 	psy_cfg.drv_data = bat;
140 	psy_cfg.of_node = dev->of_node;
141 
142 	bat->battery = devm_power_supply_register(dev, desc, &psy_cfg);
143 	if (IS_ERR(bat->battery)) {
144 		dev_err(dev, "Unable to register battery\n");
145 		return PTR_ERR(bat->battery);
146 	}
147 
148 	ret = power_supply_get_battery_info(bat->battery, &bat->info);
149 	if (ret) {
150 		dev_err(dev, "Unable to get battery info: %d\n", ret);
151 		return ret;
152 	}
153 	if (bat->info.voltage_min_design_uv < 0) {
154 		dev_err(dev, "Unable to get voltage min design\n");
155 		return bat->info.voltage_min_design_uv;
156 	}
157 	if (bat->info.voltage_max_design_uv < 0) {
158 		dev_err(dev, "Unable to get voltage max design\n");
159 		return bat->info.voltage_max_design_uv;
160 	}
161 
162 	return ingenic_battery_set_scale(bat);
163 }
164 
165 #ifdef CONFIG_OF
166 static const struct of_device_id ingenic_battery_of_match[] = {
167 	{ .compatible = "ingenic,jz4740-battery", },
168 	{ },
169 };
170 MODULE_DEVICE_TABLE(of, ingenic_battery_of_match);
171 #endif
172 
173 static struct platform_driver ingenic_battery_driver = {
174 	.driver = {
175 		.name = "ingenic-battery",
176 		.of_match_table = of_match_ptr(ingenic_battery_of_match),
177 	},
178 	.probe = ingenic_battery_probe,
179 };
180 module_platform_driver(ingenic_battery_driver);
181 
182 MODULE_DESCRIPTION("Battery driver for Ingenic JZ47xx SoCs");
183 MODULE_AUTHOR("Artur Rojek <contact@artur-rojek.eu>");
184 MODULE_LICENSE("GPL");
185