xref: /openbmc/linux/drivers/power/supply/power_supply_sysfs.c (revision 5ef12cb4a3a78ffb331c03a795a15eea4ae35155)
1 /*
2  *  Sysfs interface for the universal power supply monitor class
3  *
4  *  Copyright © 2007  David Woodhouse <dwmw2@infradead.org>
5  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
6  *  Copyright © 2004  Szabolcs Gyurko
7  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
8  *
9  *  Modified: 2004, Oct     Szabolcs Gyurko
10  *
11  *  You may use this code as per GPL version 2
12  */
13 
14 #include <linux/ctype.h>
15 #include <linux/device.h>
16 #include <linux/power_supply.h>
17 #include <linux/slab.h>
18 #include <linux/stat.h>
19 
20 #include "power_supply.h"
21 
22 /*
23  * This is because the name "current" breaks the device attr macro.
24  * The "current" word resolves to "(get_current())" so instead of
25  * "current" "(get_current())" appears in the sysfs.
26  *
27  * The source of this definition is the device.h which calls __ATTR
28  * macro in sysfs.h which calls the __stringify macro.
29  *
30  * Only modification that the name is not tried to be resolved
31  * (as a macro let's say).
32  */
33 
34 #define POWER_SUPPLY_ATTR(_name)					\
35 {									\
36 	.attr = { .name = #_name },					\
37 	.show = power_supply_show_property,				\
38 	.store = power_supply_store_property,				\
39 }
40 
41 static struct device_attribute power_supply_attrs[];
42 
43 static const char * const power_supply_type_text[] = {
44 	"Unknown", "Battery", "UPS", "Mains", "USB",
45 	"USB_DCP", "USB_CDP", "USB_ACA", "USB_C",
46 	"USB_PD", "USB_PD_DRP", "BrickID"
47 };
48 
49 static const char * const power_supply_usb_type_text[] = {
50 	"Unknown", "SDP", "DCP", "CDP", "ACA", "C",
51 	"PD", "PD_DRP", "PD_PPS", "BrickID"
52 };
53 
54 static const char * const power_supply_status_text[] = {
55 	"Unknown", "Charging", "Discharging", "Not charging", "Full"
56 };
57 
58 static const char * const power_supply_charge_type_text[] = {
59 	"Unknown", "N/A", "Trickle", "Fast"
60 };
61 
62 static const char * const power_supply_health_text[] = {
63 	"Unknown", "Good", "Overheat", "Dead", "Over voltage",
64 	"Unspecified failure", "Cold", "Watchdog timer expire",
65 	"Safety timer expire"
66 };
67 
68 static const char * const power_supply_technology_text[] = {
69 	"Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
70 	"LiMn"
71 };
72 
73 static const char * const power_supply_capacity_level_text[] = {
74 	"Unknown", "Critical", "Low", "Normal", "High", "Full"
75 };
76 
77 static const char * const power_supply_scope_text[] = {
78 	"Unknown", "System", "Device"
79 };
80 
81 static ssize_t power_supply_show_usb_type(struct device *dev,
82 					  enum power_supply_usb_type *usb_types,
83 					  ssize_t num_usb_types,
84 					  union power_supply_propval *value,
85 					  char *buf)
86 {
87 	enum power_supply_usb_type usb_type;
88 	ssize_t count = 0;
89 	bool match = false;
90 	int i;
91 
92 	for (i = 0; i < num_usb_types; ++i) {
93 		usb_type = usb_types[i];
94 
95 		if (value->intval == usb_type) {
96 			count += sprintf(buf + count, "[%s] ",
97 					 power_supply_usb_type_text[usb_type]);
98 			match = true;
99 		} else {
100 			count += sprintf(buf + count, "%s ",
101 					 power_supply_usb_type_text[usb_type]);
102 		}
103 	}
104 
105 	if (!match) {
106 		dev_warn(dev, "driver reporting unsupported connected type\n");
107 		return -EINVAL;
108 	}
109 
110 	if (count)
111 		buf[count - 1] = '\n';
112 
113 	return count;
114 }
115 
116 static ssize_t power_supply_show_property(struct device *dev,
117 					  struct device_attribute *attr,
118 					  char *buf) {
119 	ssize_t ret = 0;
120 	struct power_supply *psy = dev_get_drvdata(dev);
121 	const ptrdiff_t off = attr - power_supply_attrs;
122 	union power_supply_propval value;
123 
124 	if (off == POWER_SUPPLY_PROP_TYPE) {
125 		value.intval = psy->desc->type;
126 	} else {
127 		ret = power_supply_get_property(psy, off, &value);
128 
129 		if (ret < 0) {
130 			if (ret == -ENODATA)
131 				dev_dbg(dev, "driver has no data for `%s' property\n",
132 					attr->attr.name);
133 			else if (ret != -ENODEV && ret != -EAGAIN)
134 				dev_err(dev, "driver failed to report `%s' property: %zd\n",
135 					attr->attr.name, ret);
136 			return ret;
137 		}
138 	}
139 
140 	if (off == POWER_SUPPLY_PROP_STATUS)
141 		return sprintf(buf, "%s\n",
142 			       power_supply_status_text[value.intval]);
143 	else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE)
144 		return sprintf(buf, "%s\n",
145 			       power_supply_charge_type_text[value.intval]);
146 	else if (off == POWER_SUPPLY_PROP_HEALTH)
147 		return sprintf(buf, "%s\n",
148 			       power_supply_health_text[value.intval]);
149 	else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
150 		return sprintf(buf, "%s\n",
151 			       power_supply_technology_text[value.intval]);
152 	else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
153 		return sprintf(buf, "%s\n",
154 			       power_supply_capacity_level_text[value.intval]);
155 	else if (off == POWER_SUPPLY_PROP_TYPE)
156 		return sprintf(buf, "%s\n",
157 			       power_supply_type_text[value.intval]);
158 	else if (off == POWER_SUPPLY_PROP_USB_TYPE)
159 		return power_supply_show_usb_type(dev, psy->desc->usb_types,
160 						  psy->desc->num_usb_types,
161 						  &value, buf);
162 	else if (off == POWER_SUPPLY_PROP_SCOPE)
163 		return sprintf(buf, "%s\n",
164 			       power_supply_scope_text[value.intval]);
165 	else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
166 		return sprintf(buf, "%s\n", value.strval);
167 
168 	return sprintf(buf, "%d\n", value.intval);
169 }
170 
171 static ssize_t power_supply_store_property(struct device *dev,
172 					   struct device_attribute *attr,
173 					   const char *buf, size_t count) {
174 	ssize_t ret;
175 	struct power_supply *psy = dev_get_drvdata(dev);
176 	const ptrdiff_t off = attr - power_supply_attrs;
177 	union power_supply_propval value;
178 
179 	/* maybe it is a enum property? */
180 	switch (off) {
181 	case POWER_SUPPLY_PROP_STATUS:
182 		ret = sysfs_match_string(power_supply_status_text, buf);
183 		break;
184 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
185 		ret = sysfs_match_string(power_supply_charge_type_text, buf);
186 		break;
187 	case POWER_SUPPLY_PROP_HEALTH:
188 		ret = sysfs_match_string(power_supply_health_text, buf);
189 		break;
190 	case POWER_SUPPLY_PROP_TECHNOLOGY:
191 		ret = sysfs_match_string(power_supply_technology_text, buf);
192 		break;
193 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
194 		ret = sysfs_match_string(power_supply_capacity_level_text, buf);
195 		break;
196 	case POWER_SUPPLY_PROP_SCOPE:
197 		ret = sysfs_match_string(power_supply_scope_text, buf);
198 		break;
199 	default:
200 		ret = -EINVAL;
201 	}
202 
203 	/*
204 	 * If no match was found, then check to see if it is an integer.
205 	 * Integer values are valid for enums in addition to the text value.
206 	 */
207 	if (ret < 0) {
208 		long long_val;
209 
210 		ret = kstrtol(buf, 10, &long_val);
211 		if (ret < 0)
212 			return ret;
213 
214 		ret = long_val;
215 	}
216 
217 	value.intval = ret;
218 
219 	ret = power_supply_set_property(psy, off, &value);
220 	if (ret < 0)
221 		return ret;
222 
223 	return count;
224 }
225 
226 /* Must be in the same order as POWER_SUPPLY_PROP_* */
227 static struct device_attribute power_supply_attrs[] = {
228 	/* Properties of type `int' */
229 	POWER_SUPPLY_ATTR(status),
230 	POWER_SUPPLY_ATTR(charge_type),
231 	POWER_SUPPLY_ATTR(health),
232 	POWER_SUPPLY_ATTR(present),
233 	POWER_SUPPLY_ATTR(online),
234 	POWER_SUPPLY_ATTR(authentic),
235 	POWER_SUPPLY_ATTR(technology),
236 	POWER_SUPPLY_ATTR(cycle_count),
237 	POWER_SUPPLY_ATTR(voltage_max),
238 	POWER_SUPPLY_ATTR(voltage_min),
239 	POWER_SUPPLY_ATTR(voltage_max_design),
240 	POWER_SUPPLY_ATTR(voltage_min_design),
241 	POWER_SUPPLY_ATTR(voltage_now),
242 	POWER_SUPPLY_ATTR(voltage_avg),
243 	POWER_SUPPLY_ATTR(voltage_ocv),
244 	POWER_SUPPLY_ATTR(voltage_boot),
245 	POWER_SUPPLY_ATTR(current_max),
246 	POWER_SUPPLY_ATTR(current_now),
247 	POWER_SUPPLY_ATTR(current_avg),
248 	POWER_SUPPLY_ATTR(current_boot),
249 	POWER_SUPPLY_ATTR(power_now),
250 	POWER_SUPPLY_ATTR(power_avg),
251 	POWER_SUPPLY_ATTR(charge_full_design),
252 	POWER_SUPPLY_ATTR(charge_empty_design),
253 	POWER_SUPPLY_ATTR(charge_full),
254 	POWER_SUPPLY_ATTR(charge_empty),
255 	POWER_SUPPLY_ATTR(charge_now),
256 	POWER_SUPPLY_ATTR(charge_avg),
257 	POWER_SUPPLY_ATTR(charge_counter),
258 	POWER_SUPPLY_ATTR(constant_charge_current),
259 	POWER_SUPPLY_ATTR(constant_charge_current_max),
260 	POWER_SUPPLY_ATTR(constant_charge_voltage),
261 	POWER_SUPPLY_ATTR(constant_charge_voltage_max),
262 	POWER_SUPPLY_ATTR(charge_control_limit),
263 	POWER_SUPPLY_ATTR(charge_control_limit_max),
264 	POWER_SUPPLY_ATTR(input_current_limit),
265 	POWER_SUPPLY_ATTR(energy_full_design),
266 	POWER_SUPPLY_ATTR(energy_empty_design),
267 	POWER_SUPPLY_ATTR(energy_full),
268 	POWER_SUPPLY_ATTR(energy_empty),
269 	POWER_SUPPLY_ATTR(energy_now),
270 	POWER_SUPPLY_ATTR(energy_avg),
271 	POWER_SUPPLY_ATTR(capacity),
272 	POWER_SUPPLY_ATTR(capacity_alert_min),
273 	POWER_SUPPLY_ATTR(capacity_alert_max),
274 	POWER_SUPPLY_ATTR(capacity_level),
275 	POWER_SUPPLY_ATTR(temp),
276 	POWER_SUPPLY_ATTR(temp_max),
277 	POWER_SUPPLY_ATTR(temp_min),
278 	POWER_SUPPLY_ATTR(temp_alert_min),
279 	POWER_SUPPLY_ATTR(temp_alert_max),
280 	POWER_SUPPLY_ATTR(temp_ambient),
281 	POWER_SUPPLY_ATTR(temp_ambient_alert_min),
282 	POWER_SUPPLY_ATTR(temp_ambient_alert_max),
283 	POWER_SUPPLY_ATTR(time_to_empty_now),
284 	POWER_SUPPLY_ATTR(time_to_empty_avg),
285 	POWER_SUPPLY_ATTR(time_to_full_now),
286 	POWER_SUPPLY_ATTR(time_to_full_avg),
287 	POWER_SUPPLY_ATTR(type),
288 	POWER_SUPPLY_ATTR(usb_type),
289 	POWER_SUPPLY_ATTR(scope),
290 	POWER_SUPPLY_ATTR(precharge_current),
291 	POWER_SUPPLY_ATTR(charge_term_current),
292 	POWER_SUPPLY_ATTR(calibrate),
293 	/* Properties of type `const char *' */
294 	POWER_SUPPLY_ATTR(model_name),
295 	POWER_SUPPLY_ATTR(manufacturer),
296 	POWER_SUPPLY_ATTR(serial_number),
297 };
298 
299 static struct attribute *
300 __power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
301 
302 static umode_t power_supply_attr_is_visible(struct kobject *kobj,
303 					   struct attribute *attr,
304 					   int attrno)
305 {
306 	struct device *dev = container_of(kobj, struct device, kobj);
307 	struct power_supply *psy = dev_get_drvdata(dev);
308 	umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
309 	int i;
310 
311 	if (attrno == POWER_SUPPLY_PROP_TYPE)
312 		return mode;
313 
314 	for (i = 0; i < psy->desc->num_properties; i++) {
315 		int property = psy->desc->properties[i];
316 
317 		if (property == attrno) {
318 			if (psy->desc->property_is_writeable &&
319 			    psy->desc->property_is_writeable(psy, property) > 0)
320 				mode |= S_IWUSR;
321 
322 			return mode;
323 		}
324 	}
325 
326 	return 0;
327 }
328 
329 static struct attribute_group power_supply_attr_group = {
330 	.attrs = __power_supply_attrs,
331 	.is_visible = power_supply_attr_is_visible,
332 };
333 
334 static const struct attribute_group *power_supply_attr_groups[] = {
335 	&power_supply_attr_group,
336 	NULL,
337 };
338 
339 void power_supply_init_attrs(struct device_type *dev_type)
340 {
341 	int i;
342 
343 	dev_type->groups = power_supply_attr_groups;
344 
345 	for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++)
346 		__power_supply_attrs[i] = &power_supply_attrs[i].attr;
347 }
348 
349 static char *kstruprdup(const char *str, gfp_t gfp)
350 {
351 	char *ret, *ustr;
352 
353 	ustr = ret = kmalloc(strlen(str) + 1, gfp);
354 
355 	if (!ret)
356 		return NULL;
357 
358 	while (*str)
359 		*ustr++ = toupper(*str++);
360 
361 	*ustr = 0;
362 
363 	return ret;
364 }
365 
366 int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
367 {
368 	struct power_supply *psy = dev_get_drvdata(dev);
369 	int ret = 0, j;
370 	char *prop_buf;
371 	char *attrname;
372 
373 	dev_dbg(dev, "uevent\n");
374 
375 	if (!psy || !psy->desc) {
376 		dev_dbg(dev, "No power supply yet\n");
377 		return ret;
378 	}
379 
380 	dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->desc->name);
381 
382 	ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name);
383 	if (ret)
384 		return ret;
385 
386 	prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
387 	if (!prop_buf)
388 		return -ENOMEM;
389 
390 	for (j = 0; j < psy->desc->num_properties; j++) {
391 		struct device_attribute *attr;
392 		char *line;
393 
394 		attr = &power_supply_attrs[psy->desc->properties[j]];
395 
396 		ret = power_supply_show_property(dev, attr, prop_buf);
397 		if (ret == -ENODEV || ret == -ENODATA) {
398 			/* When a battery is absent, we expect -ENODEV. Don't abort;
399 			   send the uevent with at least the the PRESENT=0 property */
400 			ret = 0;
401 			continue;
402 		}
403 
404 		if (ret < 0)
405 			goto out;
406 
407 		line = strchr(prop_buf, '\n');
408 		if (line)
409 			*line = 0;
410 
411 		attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
412 		if (!attrname) {
413 			ret = -ENOMEM;
414 			goto out;
415 		}
416 
417 		dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
418 
419 		ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
420 		kfree(attrname);
421 		if (ret)
422 			goto out;
423 	}
424 
425 out:
426 	free_page((unsigned long)prop_buf);
427 
428 	return ret;
429 }
430