1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  ChromeOS EC driver for charge control
4  *
5  *  Copyright (C) 2024 Thomas Weißschuh <linux@weissschuh.net>
6  */
7 #include <acpi/battery.h>
8 #include <linux/container_of.h>
9 #include <linux/dmi.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/platform_data/cros_ec_commands.h>
13 #include <linux/platform_data/cros_ec_proto.h>
14 #include <linux/platform_device.h>
15 #include <linux/types.h>
16 
17 #define EC_CHARGE_CONTROL_BEHAVIOURS	(BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO)             | \
18 					 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE)   | \
19 					 BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE))
20 
21 enum CROS_CHCTL_ATTR {
22 	CROS_CHCTL_ATTR_START_THRESHOLD,
23 	CROS_CHCTL_ATTR_END_THRESHOLD,
24 	CROS_CHCTL_ATTR_CHARGE_BEHAVIOUR,
25 	_CROS_CHCTL_ATTR_COUNT
26 };
27 
28 /*
29  * Semantics of data *returned* from the EC API and Linux sysfs differ
30  * slightly, also the v1 API can not return any data.
31  * To match the expected sysfs API, data is never read back from the EC but
32  * cached in the driver.
33  *
34  * Changes to the EC bypassing the driver will not be reflected in sysfs.
35  * Any change to "charge_behaviour" will synchronize the EC with the driver state.
36  */
37 
38 struct cros_chctl_priv {
39 	struct cros_ec_device *cros_ec;
40 	struct acpi_battery_hook battery_hook;
41 	struct power_supply *hooked_battery;
42 	u8 cmd_version;
43 
44 	/* The callbacks need to access this priv structure.
45 	 * As neither the struct device nor power_supply are under the drivers
46 	 * control, embed the attributes within priv to use with container_of().
47 	 */
48 	struct device_attribute device_attrs[_CROS_CHCTL_ATTR_COUNT];
49 	struct attribute *attributes[_CROS_CHCTL_ATTR_COUNT];
50 	struct attribute_group group;
51 
52 	enum power_supply_charge_behaviour current_behaviour;
53 	u8 current_start_threshold, current_end_threshold;
54 };
55 
cros_chctl_send_charge_control_cmd(struct cros_ec_device * cros_ec,u8 cmd_version,struct ec_params_charge_control * req)56 static int cros_chctl_send_charge_control_cmd(struct cros_ec_device *cros_ec,
57 					      u8 cmd_version, struct ec_params_charge_control *req)
58 {
59 	static const u8 outsizes[] = {
60 		[1] = offsetof(struct ec_params_charge_control, cmd),
61 		[2] = sizeof(struct ec_params_charge_control),
62 		[3] = sizeof(struct ec_params_charge_control),
63 	};
64 
65 	struct {
66 		struct cros_ec_command msg;
67 		union {
68 			struct ec_params_charge_control req;
69 			struct ec_response_charge_control resp;
70 		} __packed data;
71 	} __packed buf = {
72 		.msg = {
73 			.command = EC_CMD_CHARGE_CONTROL,
74 			.version = cmd_version,
75 			.insize  = 0,
76 			.outsize = outsizes[cmd_version],
77 		},
78 		.data.req = *req,
79 	};
80 
81 	return cros_ec_cmd_xfer_status(cros_ec, &buf.msg);
82 }
83 
cros_chctl_configure_ec(struct cros_chctl_priv * priv)84 static int cros_chctl_configure_ec(struct cros_chctl_priv *priv)
85 {
86 	struct ec_params_charge_control req = {};
87 
88 	req.cmd = EC_CHARGE_CONTROL_CMD_SET;
89 
90 	switch (priv->current_behaviour) {
91 	case POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO:
92 		req.mode = CHARGE_CONTROL_NORMAL;
93 		break;
94 	case POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE:
95 		req.mode = CHARGE_CONTROL_IDLE;
96 		break;
97 	case POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE:
98 		req.mode = CHARGE_CONTROL_DISCHARGE;
99 		break;
100 	default:
101 		return -EINVAL;
102 	}
103 
104 	if (priv->current_behaviour == POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO &&
105 	    !(priv->current_start_threshold == 0 && priv->current_end_threshold == 100)) {
106 		req.sustain_soc.lower = priv->current_start_threshold;
107 		req.sustain_soc.upper = priv->current_end_threshold;
108 	} else {
109 		/* Disable charging limits */
110 		req.sustain_soc.lower = -1;
111 		req.sustain_soc.upper = -1;
112 	}
113 
114 	return cros_chctl_send_charge_control_cmd(priv->cros_ec, priv->cmd_version, &req);
115 }
116 
cros_chctl_attr_to_priv(struct attribute * attr,enum CROS_CHCTL_ATTR idx)117 static struct cros_chctl_priv *cros_chctl_attr_to_priv(struct attribute *attr,
118 						       enum CROS_CHCTL_ATTR idx)
119 {
120 	struct device_attribute *dev_attr = container_of(attr, struct device_attribute, attr);
121 
122 	return container_of(dev_attr, struct cros_chctl_priv, device_attrs[idx]);
123 }
124 
cros_chctl_store_threshold(struct device * dev,struct cros_chctl_priv * priv,int is_end_threshold,const char * buf,size_t count)125 static ssize_t cros_chctl_store_threshold(struct device *dev, struct cros_chctl_priv *priv,
126 					  int is_end_threshold, const char *buf, size_t count)
127 {
128 	int ret, val;
129 
130 	ret = kstrtoint(buf, 10, &val);
131 	if (ret < 0)
132 		return ret;
133 	if (val < 0 || val > 100)
134 		return -EINVAL;
135 
136 	if (is_end_threshold) {
137 		if (val <= priv->current_start_threshold)
138 			return -EINVAL;
139 		priv->current_end_threshold = val;
140 	} else {
141 		if (val >= priv->current_end_threshold)
142 			return -EINVAL;
143 		priv->current_start_threshold = val;
144 	}
145 
146 	if (priv->current_behaviour == POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO) {
147 		ret = cros_chctl_configure_ec(priv);
148 		if (ret < 0)
149 			return ret;
150 	}
151 
152 	return count;
153 }
154 
charge_control_start_threshold_show(struct device * dev,struct device_attribute * attr,char * buf)155 static ssize_t charge_control_start_threshold_show(struct device *dev,
156 						   struct device_attribute *attr,
157 						   char *buf)
158 {
159 	struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr,
160 							       CROS_CHCTL_ATTR_START_THRESHOLD);
161 
162 	return sysfs_emit(buf, "%u\n", (unsigned int)priv->current_start_threshold);
163 }
164 
charge_control_start_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)165 static ssize_t charge_control_start_threshold_store(struct device *dev,
166 						    struct device_attribute *attr,
167 						    const char *buf, size_t count)
168 {
169 	struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr,
170 							       CROS_CHCTL_ATTR_START_THRESHOLD);
171 
172 	return cros_chctl_store_threshold(dev, priv, 0, buf, count);
173 }
174 
charge_control_end_threshold_show(struct device * dev,struct device_attribute * attr,char * buf)175 static ssize_t charge_control_end_threshold_show(struct device *dev, struct device_attribute *attr,
176 						 char *buf)
177 {
178 	struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr,
179 							       CROS_CHCTL_ATTR_END_THRESHOLD);
180 
181 	return sysfs_emit(buf, "%u\n", (unsigned int)priv->current_end_threshold);
182 }
183 
charge_control_end_threshold_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)184 static ssize_t charge_control_end_threshold_store(struct device *dev, struct device_attribute *attr,
185 						  const char *buf, size_t count)
186 {
187 	struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr,
188 							       CROS_CHCTL_ATTR_END_THRESHOLD);
189 
190 	return cros_chctl_store_threshold(dev, priv, 1, buf, count);
191 }
192 
charge_behaviour_show(struct device * dev,struct device_attribute * attr,char * buf)193 static ssize_t charge_behaviour_show(struct device *dev, struct device_attribute *attr, char *buf)
194 {
195 	struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr,
196 							       CROS_CHCTL_ATTR_CHARGE_BEHAVIOUR);
197 
198 	return power_supply_charge_behaviour_show(dev, EC_CHARGE_CONTROL_BEHAVIOURS,
199 						  priv->current_behaviour, buf);
200 }
201 
charge_behaviour_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)202 static ssize_t charge_behaviour_store(struct device *dev, struct device_attribute *attr,
203 				      const char *buf, size_t count)
204 {
205 	struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(&attr->attr,
206 							       CROS_CHCTL_ATTR_CHARGE_BEHAVIOUR);
207 	int ret;
208 
209 	ret = power_supply_charge_behaviour_parse(EC_CHARGE_CONTROL_BEHAVIOURS, buf);
210 	if (ret < 0)
211 		return ret;
212 
213 	priv->current_behaviour = ret;
214 
215 	ret = cros_chctl_configure_ec(priv);
216 	if (ret < 0)
217 		return ret;
218 
219 	return count;
220 }
221 
cros_chtl_attr_is_visible(struct kobject * kobj,struct attribute * attr,int n)222 static umode_t cros_chtl_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
223 {
224 	struct cros_chctl_priv *priv = cros_chctl_attr_to_priv(attr, n);
225 
226 	if (priv->cmd_version < 2) {
227 		if (n == CROS_CHCTL_ATTR_START_THRESHOLD)
228 			return 0;
229 		if (n == CROS_CHCTL_ATTR_END_THRESHOLD)
230 			return 0;
231 	}
232 
233 	return attr->mode;
234 }
235 
cros_chctl_add_battery(struct power_supply * battery,struct acpi_battery_hook * hook)236 static int cros_chctl_add_battery(struct power_supply *battery, struct acpi_battery_hook *hook)
237 {
238 	struct cros_chctl_priv *priv = container_of(hook, struct cros_chctl_priv, battery_hook);
239 
240 	if (priv->hooked_battery)
241 		return 0;
242 
243 	priv->hooked_battery = battery;
244 	return device_add_group(&battery->dev, &priv->group);
245 }
246 
cros_chctl_remove_battery(struct power_supply * battery,struct acpi_battery_hook * hook)247 static int cros_chctl_remove_battery(struct power_supply *battery, struct acpi_battery_hook *hook)
248 {
249 	struct cros_chctl_priv *priv = container_of(hook, struct cros_chctl_priv, battery_hook);
250 
251 	if (priv->hooked_battery == battery) {
252 		device_remove_group(&battery->dev, &priv->group);
253 		priv->hooked_battery = NULL;
254 	}
255 
256 	return 0;
257 }
258 
259 static bool probe_with_fwk_charge_control;
260 module_param(probe_with_fwk_charge_control, bool, 0644);
261 MODULE_PARM_DESC(probe_with_fwk_charge_control,
262 		 "Probe the driver in the presence of the custom Framework EC charge control");
263 
cros_chctl_fwk_charge_control_versions(struct cros_ec_device * cros_ec)264 static int cros_chctl_fwk_charge_control_versions(struct cros_ec_device *cros_ec)
265 {
266 	if (!dmi_match(DMI_SYS_VENDOR, "Framework"))
267 		return 0;
268 
269 	return cros_ec_get_cmd_versions(cros_ec, 0x3E03 /* FW_EC_CMD_CHARGE_LIMIT */);
270 }
271 
cros_chctl_probe(struct platform_device * pdev)272 static int cros_chctl_probe(struct platform_device *pdev)
273 {
274 	struct device *dev = &pdev->dev;
275 	struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
276 	struct cros_ec_device *cros_ec = ec_dev->ec_dev;
277 	struct cros_chctl_priv *priv;
278 	size_t i;
279 	int ret;
280 
281 	ret = cros_chctl_fwk_charge_control_versions(cros_ec);
282 	if (ret < 0)
283 		return ret;
284 	if (ret > 0 && !probe_with_fwk_charge_control) {
285 		dev_info(dev, "Framework charge control detected, preventing load\n");
286 		return -ENODEV;
287 	}
288 
289 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
290 	if (!priv)
291 		return -ENOMEM;
292 
293 	ret = cros_ec_get_cmd_versions(cros_ec, EC_CMD_CHARGE_CONTROL);
294 	if (ret < 0)
295 		return ret;
296 	else if (ret & EC_VER_MASK(3))
297 		priv->cmd_version = 3;
298 	else if (ret & EC_VER_MASK(2))
299 		priv->cmd_version = 2;
300 	else if (ret & EC_VER_MASK(1))
301 		priv->cmd_version = 1;
302 	else
303 		return -ENODEV;
304 
305 	dev_dbg(dev, "Command version: %u\n", (unsigned int)priv->cmd_version);
306 
307 	priv->cros_ec = cros_ec;
308 	priv->device_attrs[CROS_CHCTL_ATTR_START_THRESHOLD] =
309 		(struct device_attribute)__ATTR_RW(charge_control_start_threshold);
310 	priv->device_attrs[CROS_CHCTL_ATTR_END_THRESHOLD] =
311 		(struct device_attribute)__ATTR_RW(charge_control_end_threshold);
312 	priv->device_attrs[CROS_CHCTL_ATTR_CHARGE_BEHAVIOUR] =
313 		(struct device_attribute)__ATTR_RW(charge_behaviour);
314 	for (i = 0; i < _CROS_CHCTL_ATTR_COUNT; i++) {
315 		sysfs_attr_init(&priv->device_attrs[i].attr);
316 		priv->attributes[i] = &priv->device_attrs[i].attr;
317 	}
318 	priv->group.is_visible = cros_chtl_attr_is_visible;
319 	priv->group.attrs = priv->attributes;
320 
321 	priv->battery_hook.name = dev_name(dev);
322 	priv->battery_hook.add_battery = cros_chctl_add_battery;
323 	priv->battery_hook.remove_battery = cros_chctl_remove_battery;
324 
325 	priv->current_behaviour = POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO;
326 	priv->current_start_threshold = 0;
327 	priv->current_end_threshold = 100;
328 
329 	/* Bring EC into well-known state */
330 	ret = cros_chctl_configure_ec(priv);
331 	if (ret < 0)
332 		return ret;
333 
334 	return devm_battery_hook_register(dev, &priv->battery_hook);
335 }
336 
337 static const struct platform_device_id cros_chctl_id[] = {
338 	{ "cros-charge-control", 0 },
339 	{}
340 };
341 
342 static struct platform_driver cros_chctl_driver = {
343 	.driver.name	= "cros-charge-control",
344 	.probe		= cros_chctl_probe,
345 	.id_table	= cros_chctl_id,
346 };
347 module_platform_driver(cros_chctl_driver);
348 
349 MODULE_DEVICE_TABLE(platform, cros_chctl_id);
350 MODULE_DESCRIPTION("ChromeOS EC charge control");
351 MODULE_AUTHOR("Thomas Weißschuh <linux@weissschuh.net>");
352 MODULE_LICENSE("GPL");
353