xref: /linux/drivers/power/supply/test_power.c (revision f9bff0e31881d03badf191d3b0005839391f5f2b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Power supply driver for testing.
4  *
5  * Copyright 2010  Anton Vorontsov <cbouatmailru@gmail.com>
6  *
7  * Dynamic module parameter code from the Virtual Battery Driver
8  * Copyright (C) 2008 Pylone, Inc.
9  * By: Masashi YOKOTA <yokota@pylone.jp>
10  * Originally found here:
11  * http://downloads.pylone.jp/src/virtual_battery/virtual_battery-0.0.1.tar.bz2
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/power_supply.h>
17 #include <linux/errno.h>
18 #include <linux/delay.h>
19 #include <generated/utsrelease.h>
20 
21 enum test_power_id {
22 	TEST_AC,
23 	TEST_BATTERY,
24 	TEST_USB,
25 	TEST_POWER_NUM,
26 };
27 
28 static int ac_online			= 1;
29 static int usb_online			= 1;
30 static int battery_status		= POWER_SUPPLY_STATUS_DISCHARGING;
31 static int battery_health		= POWER_SUPPLY_HEALTH_GOOD;
32 static int battery_present		= 1; /* true */
33 static int battery_technology		= POWER_SUPPLY_TECHNOLOGY_LION;
34 static int battery_capacity		= 50;
35 static int battery_voltage		= 3300;
36 static int battery_charge_counter	= -1000;
37 static int battery_current		= -1600;
38 
39 static bool module_initialized;
40 
41 static int test_power_get_ac_property(struct power_supply *psy,
42 				      enum power_supply_property psp,
43 				      union power_supply_propval *val)
44 {
45 	switch (psp) {
46 	case POWER_SUPPLY_PROP_ONLINE:
47 		val->intval = ac_online;
48 		break;
49 	default:
50 		return -EINVAL;
51 	}
52 	return 0;
53 }
54 
55 static int test_power_get_usb_property(struct power_supply *psy,
56 				      enum power_supply_property psp,
57 				      union power_supply_propval *val)
58 {
59 	switch (psp) {
60 	case POWER_SUPPLY_PROP_ONLINE:
61 		val->intval = usb_online;
62 		break;
63 	default:
64 		return -EINVAL;
65 	}
66 	return 0;
67 }
68 
69 static int test_power_get_battery_property(struct power_supply *psy,
70 					   enum power_supply_property psp,
71 					   union power_supply_propval *val)
72 {
73 	switch (psp) {
74 	case POWER_SUPPLY_PROP_MODEL_NAME:
75 		val->strval = "Test battery";
76 		break;
77 	case POWER_SUPPLY_PROP_MANUFACTURER:
78 		val->strval = "Linux";
79 		break;
80 	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
81 		val->strval = UTS_RELEASE;
82 		break;
83 	case POWER_SUPPLY_PROP_STATUS:
84 		val->intval = battery_status;
85 		break;
86 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
87 		val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
88 		break;
89 	case POWER_SUPPLY_PROP_HEALTH:
90 		val->intval = battery_health;
91 		break;
92 	case POWER_SUPPLY_PROP_PRESENT:
93 		val->intval = battery_present;
94 		break;
95 	case POWER_SUPPLY_PROP_TECHNOLOGY:
96 		val->intval = battery_technology;
97 		break;
98 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
99 		val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
100 		break;
101 	case POWER_SUPPLY_PROP_CAPACITY:
102 	case POWER_SUPPLY_PROP_CHARGE_NOW:
103 		val->intval = battery_capacity;
104 		break;
105 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
106 		val->intval = battery_charge_counter;
107 		break;
108 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
109 	case POWER_SUPPLY_PROP_CHARGE_FULL:
110 		val->intval = 100;
111 		break;
112 	case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
113 	case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
114 		val->intval = 3600;
115 		break;
116 	case POWER_SUPPLY_PROP_TEMP:
117 		val->intval = 26;
118 		break;
119 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
120 		val->intval = battery_voltage;
121 		break;
122 	case POWER_SUPPLY_PROP_CURRENT_AVG:
123 	case POWER_SUPPLY_PROP_CURRENT_NOW:
124 		val->intval = battery_current;
125 		break;
126 	default:
127 		pr_info("%s: some properties deliberately report errors.\n",
128 			__func__);
129 		return -EINVAL;
130 	}
131 	return 0;
132 }
133 
134 static enum power_supply_property test_power_ac_props[] = {
135 	POWER_SUPPLY_PROP_ONLINE,
136 };
137 
138 static enum power_supply_property test_power_battery_props[] = {
139 	POWER_SUPPLY_PROP_STATUS,
140 	POWER_SUPPLY_PROP_CHARGE_TYPE,
141 	POWER_SUPPLY_PROP_HEALTH,
142 	POWER_SUPPLY_PROP_PRESENT,
143 	POWER_SUPPLY_PROP_TECHNOLOGY,
144 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
145 	POWER_SUPPLY_PROP_CHARGE_FULL,
146 	POWER_SUPPLY_PROP_CHARGE_NOW,
147 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
148 	POWER_SUPPLY_PROP_CAPACITY,
149 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
150 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
151 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
152 	POWER_SUPPLY_PROP_MODEL_NAME,
153 	POWER_SUPPLY_PROP_MANUFACTURER,
154 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
155 	POWER_SUPPLY_PROP_TEMP,
156 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
157 	POWER_SUPPLY_PROP_CURRENT_AVG,
158 	POWER_SUPPLY_PROP_CURRENT_NOW,
159 };
160 
161 static char *test_power_ac_supplied_to[] = {
162 	"test_battery",
163 };
164 
165 static struct power_supply *test_power_supplies[TEST_POWER_NUM];
166 
167 static const struct power_supply_desc test_power_desc[] = {
168 	[TEST_AC] = {
169 		.name = "test_ac",
170 		.type = POWER_SUPPLY_TYPE_MAINS,
171 		.properties = test_power_ac_props,
172 		.num_properties = ARRAY_SIZE(test_power_ac_props),
173 		.get_property = test_power_get_ac_property,
174 	},
175 	[TEST_BATTERY] = {
176 		.name = "test_battery",
177 		.type = POWER_SUPPLY_TYPE_BATTERY,
178 		.properties = test_power_battery_props,
179 		.num_properties = ARRAY_SIZE(test_power_battery_props),
180 		.get_property = test_power_get_battery_property,
181 	},
182 	[TEST_USB] = {
183 		.name = "test_usb",
184 		.type = POWER_SUPPLY_TYPE_USB,
185 		.properties = test_power_ac_props,
186 		.num_properties = ARRAY_SIZE(test_power_ac_props),
187 		.get_property = test_power_get_usb_property,
188 	},
189 };
190 
191 static const struct power_supply_config test_power_configs[] = {
192 	{
193 		/* test_ac */
194 		.supplied_to = test_power_ac_supplied_to,
195 		.num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
196 	}, {
197 		/* test_battery */
198 	}, {
199 		/* test_usb */
200 		.supplied_to = test_power_ac_supplied_to,
201 		.num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
202 	},
203 };
204 
205 static int __init test_power_init(void)
206 {
207 	int i;
208 	int ret;
209 
210 	BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies));
211 	BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_configs));
212 
213 	for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
214 		test_power_supplies[i] = power_supply_register(NULL,
215 						&test_power_desc[i],
216 						&test_power_configs[i]);
217 		if (IS_ERR(test_power_supplies[i])) {
218 			pr_err("%s: failed to register %s\n", __func__,
219 				test_power_desc[i].name);
220 			ret = PTR_ERR(test_power_supplies[i]);
221 			goto failed;
222 		}
223 	}
224 
225 	module_initialized = true;
226 	return 0;
227 failed:
228 	while (--i >= 0)
229 		power_supply_unregister(test_power_supplies[i]);
230 	return ret;
231 }
232 module_init(test_power_init);
233 
234 static void __exit test_power_exit(void)
235 {
236 	int i;
237 
238 	/* Let's see how we handle changes... */
239 	ac_online = 0;
240 	usb_online = 0;
241 	battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
242 	for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
243 		power_supply_changed(test_power_supplies[i]);
244 	pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
245 		__func__);
246 	ssleep(10);
247 
248 	for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
249 		power_supply_unregister(test_power_supplies[i]);
250 
251 	module_initialized = false;
252 }
253 module_exit(test_power_exit);
254 
255 
256 
257 #define MAX_KEYLENGTH 256
258 struct battery_property_map {
259 	int value;
260 	char const *key;
261 };
262 
263 static struct battery_property_map map_ac_online[] = {
264 	{ 0,  "off"  },
265 	{ 1,  "on" },
266 	{ -1, NULL  },
267 };
268 
269 static struct battery_property_map map_status[] = {
270 	{ POWER_SUPPLY_STATUS_CHARGING,     "charging"     },
271 	{ POWER_SUPPLY_STATUS_DISCHARGING,  "discharging"  },
272 	{ POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
273 	{ POWER_SUPPLY_STATUS_FULL,         "full"         },
274 	{ -1,                               NULL           },
275 };
276 
277 static struct battery_property_map map_health[] = {
278 	{ POWER_SUPPLY_HEALTH_GOOD,           "good"        },
279 	{ POWER_SUPPLY_HEALTH_OVERHEAT,       "overheat"    },
280 	{ POWER_SUPPLY_HEALTH_DEAD,           "dead"        },
281 	{ POWER_SUPPLY_HEALTH_OVERVOLTAGE,    "overvoltage" },
282 	{ POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure"     },
283 	{ -1,                                 NULL          },
284 };
285 
286 static struct battery_property_map map_present[] = {
287 	{ 0,  "false" },
288 	{ 1,  "true"  },
289 	{ -1, NULL    },
290 };
291 
292 static struct battery_property_map map_technology[] = {
293 	{ POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
294 	{ POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
295 	{ POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
296 	{ POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
297 	{ POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
298 	{ POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
299 	{ -1,				NULL   },
300 };
301 
302 
303 static int map_get_value(struct battery_property_map *map, const char *key,
304 				int def_val)
305 {
306 	char buf[MAX_KEYLENGTH];
307 	int cr;
308 
309 	strscpy(buf, key, MAX_KEYLENGTH);
310 
311 	cr = strnlen(buf, MAX_KEYLENGTH) - 1;
312 	if (cr < 0)
313 		return def_val;
314 	if (buf[cr] == '\n')
315 		buf[cr] = '\0';
316 
317 	while (map->key) {
318 		if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
319 			return map->value;
320 		map++;
321 	}
322 
323 	return def_val;
324 }
325 
326 
327 static const char *map_get_key(struct battery_property_map *map, int value,
328 				const char *def_key)
329 {
330 	while (map->key) {
331 		if (map->value == value)
332 			return map->key;
333 		map++;
334 	}
335 
336 	return def_key;
337 }
338 
339 static inline void signal_power_supply_changed(struct power_supply *psy)
340 {
341 	if (module_initialized)
342 		power_supply_changed(psy);
343 }
344 
345 static int param_set_ac_online(const char *key, const struct kernel_param *kp)
346 {
347 	ac_online = map_get_value(map_ac_online, key, ac_online);
348 	signal_power_supply_changed(test_power_supplies[TEST_AC]);
349 	return 0;
350 }
351 
352 static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
353 {
354 	return sprintf(buffer, "%s\n",
355 			map_get_key(map_ac_online, ac_online, "unknown"));
356 }
357 
358 static int param_set_usb_online(const char *key, const struct kernel_param *kp)
359 {
360 	usb_online = map_get_value(map_ac_online, key, usb_online);
361 	signal_power_supply_changed(test_power_supplies[TEST_USB]);
362 	return 0;
363 }
364 
365 static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
366 {
367 	return sprintf(buffer, "%s\n",
368 			map_get_key(map_ac_online, usb_online, "unknown"));
369 }
370 
371 static int param_set_battery_status(const char *key,
372 					const struct kernel_param *kp)
373 {
374 	battery_status = map_get_value(map_status, key, battery_status);
375 	signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
376 	return 0;
377 }
378 
379 static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
380 {
381 	return sprintf(buffer, "%s\n",
382 			map_get_key(map_ac_online, battery_status, "unknown"));
383 }
384 
385 static int param_set_battery_health(const char *key,
386 					const struct kernel_param *kp)
387 {
388 	battery_health = map_get_value(map_health, key, battery_health);
389 	signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
390 	return 0;
391 }
392 
393 static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
394 {
395 	return sprintf(buffer, "%s\n",
396 			map_get_key(map_ac_online, battery_health, "unknown"));
397 }
398 
399 static int param_set_battery_present(const char *key,
400 					const struct kernel_param *kp)
401 {
402 	battery_present = map_get_value(map_present, key, battery_present);
403 	signal_power_supply_changed(test_power_supplies[TEST_AC]);
404 	return 0;
405 }
406 
407 static int param_get_battery_present(char *buffer,
408 					const struct kernel_param *kp)
409 {
410 	return sprintf(buffer, "%s\n",
411 			map_get_key(map_ac_online, battery_present, "unknown"));
412 }
413 
414 static int param_set_battery_technology(const char *key,
415 					const struct kernel_param *kp)
416 {
417 	battery_technology = map_get_value(map_technology, key,
418 						battery_technology);
419 	signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
420 	return 0;
421 }
422 
423 static int param_get_battery_technology(char *buffer,
424 					const struct kernel_param *kp)
425 {
426 	return sprintf(buffer, "%s\n",
427 			map_get_key(map_ac_online, battery_technology,
428 					"unknown"));
429 }
430 
431 static int param_set_battery_capacity(const char *key,
432 					const struct kernel_param *kp)
433 {
434 	int tmp;
435 
436 	if (1 != sscanf(key, "%d", &tmp))
437 		return -EINVAL;
438 
439 	battery_capacity = tmp;
440 	signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
441 	return 0;
442 }
443 
444 #define param_get_battery_capacity param_get_int
445 
446 static int param_set_battery_voltage(const char *key,
447 					const struct kernel_param *kp)
448 {
449 	int tmp;
450 
451 	if (1 != sscanf(key, "%d", &tmp))
452 		return -EINVAL;
453 
454 	battery_voltage = tmp;
455 	signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
456 	return 0;
457 }
458 
459 #define param_get_battery_voltage param_get_int
460 
461 static int param_set_battery_charge_counter(const char *key,
462 					const struct kernel_param *kp)
463 {
464 	int tmp;
465 
466 	if (1 != sscanf(key, "%d", &tmp))
467 		return -EINVAL;
468 
469 	battery_charge_counter = tmp;
470 	signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
471 	return 0;
472 }
473 
474 #define param_get_battery_charge_counter param_get_int
475 
476 static int param_set_battery_current(const char *key,
477 					const struct kernel_param *kp)
478 {
479 	int tmp;
480 
481 	if (1 != sscanf(key, "%d", &tmp))
482 		return -EINVAL;
483 
484 	battery_current = tmp;
485 	signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
486 	return 0;
487 }
488 
489 #define param_get_battery_current param_get_int
490 
491 static const struct kernel_param_ops param_ops_ac_online = {
492 	.set = param_set_ac_online,
493 	.get = param_get_ac_online,
494 };
495 
496 static const struct kernel_param_ops param_ops_usb_online = {
497 	.set = param_set_usb_online,
498 	.get = param_get_usb_online,
499 };
500 
501 static const struct kernel_param_ops param_ops_battery_status = {
502 	.set = param_set_battery_status,
503 	.get = param_get_battery_status,
504 };
505 
506 static const struct kernel_param_ops param_ops_battery_present = {
507 	.set = param_set_battery_present,
508 	.get = param_get_battery_present,
509 };
510 
511 static const struct kernel_param_ops param_ops_battery_technology = {
512 	.set = param_set_battery_technology,
513 	.get = param_get_battery_technology,
514 };
515 
516 static const struct kernel_param_ops param_ops_battery_health = {
517 	.set = param_set_battery_health,
518 	.get = param_get_battery_health,
519 };
520 
521 static const struct kernel_param_ops param_ops_battery_capacity = {
522 	.set = param_set_battery_capacity,
523 	.get = param_get_battery_capacity,
524 };
525 
526 static const struct kernel_param_ops param_ops_battery_voltage = {
527 	.set = param_set_battery_voltage,
528 	.get = param_get_battery_voltage,
529 };
530 
531 static const struct kernel_param_ops param_ops_battery_charge_counter = {
532 	.set = param_set_battery_charge_counter,
533 	.get = param_get_battery_charge_counter,
534 };
535 
536 static const struct kernel_param_ops param_ops_battery_current = {
537 	.set = param_set_battery_current,
538 	.get = param_get_battery_current,
539 };
540 
541 #define param_check_ac_online(name, p) __param_check(name, p, void);
542 #define param_check_usb_online(name, p) __param_check(name, p, void);
543 #define param_check_battery_status(name, p) __param_check(name, p, void);
544 #define param_check_battery_present(name, p) __param_check(name, p, void);
545 #define param_check_battery_technology(name, p) __param_check(name, p, void);
546 #define param_check_battery_health(name, p) __param_check(name, p, void);
547 #define param_check_battery_capacity(name, p) __param_check(name, p, void);
548 #define param_check_battery_voltage(name, p) __param_check(name, p, void);
549 #define param_check_battery_charge_counter(name, p) __param_check(name, p, void);
550 #define param_check_battery_current(name, p) __param_check(name, p, void);
551 
552 
553 module_param(ac_online, ac_online, 0644);
554 MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
555 
556 module_param(usb_online, usb_online, 0644);
557 MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
558 
559 module_param(battery_status, battery_status, 0644);
560 MODULE_PARM_DESC(battery_status,
561 	"battery status <charging|discharging|not-charging|full>");
562 
563 module_param(battery_present, battery_present, 0644);
564 MODULE_PARM_DESC(battery_present,
565 	"battery presence state <good|overheat|dead|overvoltage|failure>");
566 
567 module_param(battery_technology, battery_technology, 0644);
568 MODULE_PARM_DESC(battery_technology,
569 	"battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
570 
571 module_param(battery_health, battery_health, 0644);
572 MODULE_PARM_DESC(battery_health,
573 	"battery health state <good|overheat|dead|overvoltage|failure>");
574 
575 module_param(battery_capacity, battery_capacity, 0644);
576 MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
577 
578 module_param(battery_voltage, battery_voltage, 0644);
579 MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
580 
581 module_param(battery_charge_counter, battery_charge_counter, 0644);
582 MODULE_PARM_DESC(battery_charge_counter,
583 	"battery charge counter (microampere-hours)");
584 
585 module_param(battery_current, battery_current, 0644);
586 MODULE_PARM_DESC(battery_current, "battery current (milliampere)");
587 
588 MODULE_DESCRIPTION("Power supply driver for testing");
589 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
590 MODULE_LICENSE("GPL");
591