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