1 // SPDX-License-Identifier: GPL-2.0-only
3 * Power supply driver for testing.
5 * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com>
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
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>
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 static enum power_supply_charge_behaviour battery_charge_behaviour
=
39 POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO
;
41 static bool module_initialized
;
43 static int test_power_get_ac_property(struct power_supply
*psy
,
44 enum power_supply_property psp
,
45 union power_supply_propval
*val
)
48 case POWER_SUPPLY_PROP_ONLINE
:
49 val
->intval
= ac_online
;
57 static int test_power_get_usb_property(struct power_supply
*psy
,
58 enum power_supply_property psp
,
59 union power_supply_propval
*val
)
62 case POWER_SUPPLY_PROP_ONLINE
:
63 val
->intval
= usb_online
;
71 static int test_power_get_battery_property(struct power_supply
*psy
,
72 enum power_supply_property psp
,
73 union power_supply_propval
*val
)
76 case POWER_SUPPLY_PROP_MODEL_NAME
:
77 val
->strval
= "Test battery";
79 case POWER_SUPPLY_PROP_MANUFACTURER
:
80 val
->strval
= "Linux";
82 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
83 val
->strval
= UTS_RELEASE
;
85 case POWER_SUPPLY_PROP_STATUS
:
86 val
->intval
= battery_status
;
88 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
89 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
91 case POWER_SUPPLY_PROP_HEALTH
:
92 val
->intval
= battery_health
;
94 case POWER_SUPPLY_PROP_PRESENT
:
95 val
->intval
= battery_present
;
97 case POWER_SUPPLY_PROP_TECHNOLOGY
:
98 val
->intval
= battery_technology
;
100 case POWER_SUPPLY_PROP_CAPACITY_LEVEL
:
101 val
->intval
= POWER_SUPPLY_CAPACITY_LEVEL_NORMAL
;
103 case POWER_SUPPLY_PROP_CAPACITY
:
104 case POWER_SUPPLY_PROP_CHARGE_NOW
:
105 val
->intval
= battery_capacity
;
107 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
108 val
->intval
= battery_charge_counter
;
110 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
111 case POWER_SUPPLY_PROP_CHARGE_FULL
:
114 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
:
115 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
:
118 case POWER_SUPPLY_PROP_TEMP
:
121 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
122 val
->intval
= battery_voltage
;
124 case POWER_SUPPLY_PROP_CURRENT_AVG
:
125 case POWER_SUPPLY_PROP_CURRENT_NOW
:
126 val
->intval
= battery_current
;
128 case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR
:
129 val
->intval
= battery_charge_behaviour
;
132 pr_info("%s: some properties deliberately report errors.\n",
139 static int test_power_battery_property_is_writeable(struct power_supply
*psy
,
140 enum power_supply_property psp
)
142 return psp
== POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR
;
145 static int test_power_set_battery_property(struct power_supply
*psy
,
146 enum power_supply_property psp
,
147 const union power_supply_propval
*val
)
150 case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR
:
151 if (val
->intval
< 0 ||
152 val
->intval
>= BITS_PER_TYPE(typeof(psy
->desc
->charge_behaviours
)) ||
153 !(BIT(val
->intval
) & psy
->desc
->charge_behaviours
)) {
156 battery_charge_behaviour
= val
->intval
;
164 static enum power_supply_property test_power_ac_props
[] = {
165 POWER_SUPPLY_PROP_ONLINE
,
168 static enum power_supply_property test_power_battery_props
[] = {
169 POWER_SUPPLY_PROP_STATUS
,
170 POWER_SUPPLY_PROP_CHARGE_TYPE
,
171 POWER_SUPPLY_PROP_HEALTH
,
172 POWER_SUPPLY_PROP_PRESENT
,
173 POWER_SUPPLY_PROP_TECHNOLOGY
,
174 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
175 POWER_SUPPLY_PROP_CHARGE_FULL
,
176 POWER_SUPPLY_PROP_CHARGE_NOW
,
177 POWER_SUPPLY_PROP_CHARGE_COUNTER
,
178 POWER_SUPPLY_PROP_CAPACITY
,
179 POWER_SUPPLY_PROP_CAPACITY_LEVEL
,
180 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG
,
181 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW
,
182 POWER_SUPPLY_PROP_MODEL_NAME
,
183 POWER_SUPPLY_PROP_MANUFACTURER
,
184 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
185 POWER_SUPPLY_PROP_TEMP
,
186 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
187 POWER_SUPPLY_PROP_CURRENT_AVG
,
188 POWER_SUPPLY_PROP_CURRENT_NOW
,
189 POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR
,
192 static char *test_power_ac_supplied_to
[] = {
196 static struct power_supply
*test_power_supplies
[TEST_POWER_NUM
];
198 static const struct power_supply_desc test_power_desc
[] = {
201 .type
= POWER_SUPPLY_TYPE_MAINS
,
202 .properties
= test_power_ac_props
,
203 .num_properties
= ARRAY_SIZE(test_power_ac_props
),
204 .get_property
= test_power_get_ac_property
,
207 .name
= "test_battery",
208 .type
= POWER_SUPPLY_TYPE_BATTERY
,
209 .properties
= test_power_battery_props
,
210 .num_properties
= ARRAY_SIZE(test_power_battery_props
),
211 .get_property
= test_power_get_battery_property
,
212 .set_property
= test_power_set_battery_property
,
213 .property_is_writeable
= test_power_battery_property_is_writeable
,
214 .charge_behaviours
= BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO
)
215 | BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE
)
216 | BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE
),
220 .type
= POWER_SUPPLY_TYPE_USB
,
221 .properties
= test_power_ac_props
,
222 .num_properties
= ARRAY_SIZE(test_power_ac_props
),
223 .get_property
= test_power_get_usb_property
,
227 static const struct power_supply_config test_power_configs
[] = {
230 .supplied_to
= test_power_ac_supplied_to
,
231 .num_supplicants
= ARRAY_SIZE(test_power_ac_supplied_to
),
236 .supplied_to
= test_power_ac_supplied_to
,
237 .num_supplicants
= ARRAY_SIZE(test_power_ac_supplied_to
),
241 static int __init
test_power_init(void)
246 BUILD_BUG_ON(TEST_POWER_NUM
!= ARRAY_SIZE(test_power_supplies
));
247 BUILD_BUG_ON(TEST_POWER_NUM
!= ARRAY_SIZE(test_power_configs
));
249 for (i
= 0; i
< ARRAY_SIZE(test_power_supplies
); i
++) {
250 test_power_supplies
[i
] = power_supply_register(NULL
,
252 &test_power_configs
[i
]);
253 if (IS_ERR(test_power_supplies
[i
])) {
254 pr_err("%s: failed to register %s\n", __func__
,
255 test_power_desc
[i
].name
);
256 ret
= PTR_ERR(test_power_supplies
[i
]);
261 module_initialized
= true;
265 power_supply_unregister(test_power_supplies
[i
]);
268 module_init(test_power_init
);
270 static void __exit
test_power_exit(void)
274 /* Let's see how we handle changes... */
277 battery_status
= POWER_SUPPLY_STATUS_DISCHARGING
;
278 for (i
= 0; i
< ARRAY_SIZE(test_power_supplies
); i
++)
279 power_supply_changed(test_power_supplies
[i
]);
280 pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
284 for (i
= 0; i
< ARRAY_SIZE(test_power_supplies
); i
++)
285 power_supply_unregister(test_power_supplies
[i
]);
287 module_initialized
= false;
289 module_exit(test_power_exit
);
293 #define MAX_KEYLENGTH 256
294 struct battery_property_map
{
299 static struct battery_property_map map_ac_online
[] = {
305 static struct battery_property_map map_status
[] = {
306 { POWER_SUPPLY_STATUS_CHARGING
, "charging" },
307 { POWER_SUPPLY_STATUS_DISCHARGING
, "discharging" },
308 { POWER_SUPPLY_STATUS_NOT_CHARGING
, "not-charging" },
309 { POWER_SUPPLY_STATUS_FULL
, "full" },
313 static struct battery_property_map map_health
[] = {
314 { POWER_SUPPLY_HEALTH_GOOD
, "good" },
315 { POWER_SUPPLY_HEALTH_OVERHEAT
, "overheat" },
316 { POWER_SUPPLY_HEALTH_DEAD
, "dead" },
317 { POWER_SUPPLY_HEALTH_OVERVOLTAGE
, "overvoltage" },
318 { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
, "failure" },
322 static struct battery_property_map map_present
[] = {
328 static struct battery_property_map map_technology
[] = {
329 { POWER_SUPPLY_TECHNOLOGY_NiMH
, "NiMH" },
330 { POWER_SUPPLY_TECHNOLOGY_LION
, "LION" },
331 { POWER_SUPPLY_TECHNOLOGY_LIPO
, "LIPO" },
332 { POWER_SUPPLY_TECHNOLOGY_LiFe
, "LiFe" },
333 { POWER_SUPPLY_TECHNOLOGY_NiCd
, "NiCd" },
334 { POWER_SUPPLY_TECHNOLOGY_LiMn
, "LiMn" },
339 static int map_get_value(struct battery_property_map
*map
, const char *key
,
342 char buf
[MAX_KEYLENGTH
];
345 strscpy(buf
, key
, MAX_KEYLENGTH
);
347 cr
= strnlen(buf
, MAX_KEYLENGTH
) - 1;
354 if (strncasecmp(map
->key
, buf
, MAX_KEYLENGTH
) == 0)
363 static const char *map_get_key(struct battery_property_map
*map
, int value
,
367 if (map
->value
== value
)
375 static inline void signal_power_supply_changed(struct power_supply
*psy
)
377 if (module_initialized
)
378 power_supply_changed(psy
);
381 static int param_set_ac_online(const char *key
, const struct kernel_param
*kp
)
383 ac_online
= map_get_value(map_ac_online
, key
, ac_online
);
384 signal_power_supply_changed(test_power_supplies
[TEST_AC
]);
388 static int param_get_ac_online(char *buffer
, const struct kernel_param
*kp
)
390 return sprintf(buffer
, "%s\n",
391 map_get_key(map_ac_online
, ac_online
, "unknown"));
394 static int param_set_usb_online(const char *key
, const struct kernel_param
*kp
)
396 usb_online
= map_get_value(map_ac_online
, key
, usb_online
);
397 signal_power_supply_changed(test_power_supplies
[TEST_USB
]);
401 static int param_get_usb_online(char *buffer
, const struct kernel_param
*kp
)
403 return sprintf(buffer
, "%s\n",
404 map_get_key(map_ac_online
, usb_online
, "unknown"));
407 static int param_set_battery_status(const char *key
,
408 const struct kernel_param
*kp
)
410 battery_status
= map_get_value(map_status
, key
, battery_status
);
411 signal_power_supply_changed(test_power_supplies
[TEST_BATTERY
]);
415 static int param_get_battery_status(char *buffer
, const struct kernel_param
*kp
)
417 return sprintf(buffer
, "%s\n",
418 map_get_key(map_ac_online
, battery_status
, "unknown"));
421 static int param_set_battery_health(const char *key
,
422 const struct kernel_param
*kp
)
424 battery_health
= map_get_value(map_health
, key
, battery_health
);
425 signal_power_supply_changed(test_power_supplies
[TEST_BATTERY
]);
429 static int param_get_battery_health(char *buffer
, const struct kernel_param
*kp
)
431 return sprintf(buffer
, "%s\n",
432 map_get_key(map_ac_online
, battery_health
, "unknown"));
435 static int param_set_battery_present(const char *key
,
436 const struct kernel_param
*kp
)
438 battery_present
= map_get_value(map_present
, key
, battery_present
);
439 signal_power_supply_changed(test_power_supplies
[TEST_AC
]);
443 static int param_get_battery_present(char *buffer
,
444 const struct kernel_param
*kp
)
446 return sprintf(buffer
, "%s\n",
447 map_get_key(map_ac_online
, battery_present
, "unknown"));
450 static int param_set_battery_technology(const char *key
,
451 const struct kernel_param
*kp
)
453 battery_technology
= map_get_value(map_technology
, key
,
455 signal_power_supply_changed(test_power_supplies
[TEST_BATTERY
]);
459 static int param_get_battery_technology(char *buffer
,
460 const struct kernel_param
*kp
)
462 return sprintf(buffer
, "%s\n",
463 map_get_key(map_ac_online
, battery_technology
,
467 static int param_set_battery_capacity(const char *key
,
468 const struct kernel_param
*kp
)
472 if (1 != sscanf(key
, "%d", &tmp
))
475 battery_capacity
= tmp
;
476 signal_power_supply_changed(test_power_supplies
[TEST_BATTERY
]);
480 #define param_get_battery_capacity param_get_int
482 static int param_set_battery_voltage(const char *key
,
483 const struct kernel_param
*kp
)
487 if (1 != sscanf(key
, "%d", &tmp
))
490 battery_voltage
= tmp
;
491 signal_power_supply_changed(test_power_supplies
[TEST_BATTERY
]);
495 #define param_get_battery_voltage param_get_int
497 static int param_set_battery_charge_counter(const char *key
,
498 const struct kernel_param
*kp
)
502 if (1 != sscanf(key
, "%d", &tmp
))
505 battery_charge_counter
= tmp
;
506 signal_power_supply_changed(test_power_supplies
[TEST_BATTERY
]);
510 #define param_get_battery_charge_counter param_get_int
512 static int param_set_battery_current(const char *key
,
513 const struct kernel_param
*kp
)
517 if (1 != sscanf(key
, "%d", &tmp
))
520 battery_current
= tmp
;
521 signal_power_supply_changed(test_power_supplies
[TEST_BATTERY
]);
525 #define param_get_battery_current param_get_int
527 static const struct kernel_param_ops param_ops_ac_online
= {
528 .set
= param_set_ac_online
,
529 .get
= param_get_ac_online
,
532 static const struct kernel_param_ops param_ops_usb_online
= {
533 .set
= param_set_usb_online
,
534 .get
= param_get_usb_online
,
537 static const struct kernel_param_ops param_ops_battery_status
= {
538 .set
= param_set_battery_status
,
539 .get
= param_get_battery_status
,
542 static const struct kernel_param_ops param_ops_battery_present
= {
543 .set
= param_set_battery_present
,
544 .get
= param_get_battery_present
,
547 static const struct kernel_param_ops param_ops_battery_technology
= {
548 .set
= param_set_battery_technology
,
549 .get
= param_get_battery_technology
,
552 static const struct kernel_param_ops param_ops_battery_health
= {
553 .set
= param_set_battery_health
,
554 .get
= param_get_battery_health
,
557 static const struct kernel_param_ops param_ops_battery_capacity
= {
558 .set
= param_set_battery_capacity
,
559 .get
= param_get_battery_capacity
,
562 static const struct kernel_param_ops param_ops_battery_voltage
= {
563 .set
= param_set_battery_voltage
,
564 .get
= param_get_battery_voltage
,
567 static const struct kernel_param_ops param_ops_battery_charge_counter
= {
568 .set
= param_set_battery_charge_counter
,
569 .get
= param_get_battery_charge_counter
,
572 static const struct kernel_param_ops param_ops_battery_current
= {
573 .set
= param_set_battery_current
,
574 .get
= param_get_battery_current
,
577 #define param_check_ac_online(name, p) __param_check(name, p, void);
578 #define param_check_usb_online(name, p) __param_check(name, p, void);
579 #define param_check_battery_status(name, p) __param_check(name, p, void);
580 #define param_check_battery_present(name, p) __param_check(name, p, void);
581 #define param_check_battery_technology(name, p) __param_check(name, p, void);
582 #define param_check_battery_health(name, p) __param_check(name, p, void);
583 #define param_check_battery_capacity(name, p) __param_check(name, p, void);
584 #define param_check_battery_voltage(name, p) __param_check(name, p, void);
585 #define param_check_battery_charge_counter(name, p) __param_check(name, p, void);
586 #define param_check_battery_current(name, p) __param_check(name, p, void);
589 module_param(ac_online
, ac_online
, 0644);
590 MODULE_PARM_DESC(ac_online
, "AC charging state <on|off>");
592 module_param(usb_online
, usb_online
, 0644);
593 MODULE_PARM_DESC(usb_online
, "USB charging state <on|off>");
595 module_param(battery_status
, battery_status
, 0644);
596 MODULE_PARM_DESC(battery_status
,
597 "battery status <charging|discharging|not-charging|full>");
599 module_param(battery_present
, battery_present
, 0644);
600 MODULE_PARM_DESC(battery_present
,
601 "battery presence state <good|overheat|dead|overvoltage|failure>");
603 module_param(battery_technology
, battery_technology
, 0644);
604 MODULE_PARM_DESC(battery_technology
,
605 "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
607 module_param(battery_health
, battery_health
, 0644);
608 MODULE_PARM_DESC(battery_health
,
609 "battery health state <good|overheat|dead|overvoltage|failure>");
611 module_param(battery_capacity
, battery_capacity
, 0644);
612 MODULE_PARM_DESC(battery_capacity
, "battery capacity (percentage)");
614 module_param(battery_voltage
, battery_voltage
, 0644);
615 MODULE_PARM_DESC(battery_voltage
, "battery voltage (millivolts)");
617 module_param(battery_charge_counter
, battery_charge_counter
, 0644);
618 MODULE_PARM_DESC(battery_charge_counter
,
619 "battery charge counter (microampere-hours)");
621 module_param(battery_current
, battery_current
, 0644);
622 MODULE_PARM_DESC(battery_current
, "battery current (milliampere)");
624 MODULE_DESCRIPTION("Power supply driver for testing");
625 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
626 MODULE_LICENSE("GPL");