2 * Power supply driver for testing.
4 * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com>
6 * Dynamic module parameter code from the Virtual Battery Driver
7 * Copyright (C) 2008 Pylone, Inc.
8 * By: Masashi YOKOTA <yokota@pylone.jp>
9 * Originally found here:
10 * http://downloads.pylone.jp/src/virtual_battery/virtual_battery-0.0.1.tar.bz2
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/power_supply.h>
20 #include <linux/errno.h>
21 #include <linux/delay.h>
22 #include <linux/vermagic.h>
32 static int ac_online = 1;
33 static int usb_online = 1;
34 static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
35 static int battery_health = POWER_SUPPLY_HEALTH_GOOD;
36 static int battery_present = 1; /* true */
37 static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
38 static int battery_capacity = 50;
39 static int battery_voltage = 3300;
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_FULL_DESIGN:
108 case POWER_SUPPLY_PROP_CHARGE_FULL:
111 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
112 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
115 case POWER_SUPPLY_PROP_TEMP:
118 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
119 val->intval = battery_voltage;
122 pr_info("%s: some properties deliberately report errors.\n",
129 static enum power_supply_property test_power_ac_props[] = {
130 POWER_SUPPLY_PROP_ONLINE,
133 static enum power_supply_property test_power_battery_props[] = {
134 POWER_SUPPLY_PROP_STATUS,
135 POWER_SUPPLY_PROP_CHARGE_TYPE,
136 POWER_SUPPLY_PROP_HEALTH,
137 POWER_SUPPLY_PROP_PRESENT,
138 POWER_SUPPLY_PROP_TECHNOLOGY,
139 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
140 POWER_SUPPLY_PROP_CHARGE_FULL,
141 POWER_SUPPLY_PROP_CHARGE_NOW,
142 POWER_SUPPLY_PROP_CAPACITY,
143 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
144 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
145 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
146 POWER_SUPPLY_PROP_MODEL_NAME,
147 POWER_SUPPLY_PROP_MANUFACTURER,
148 POWER_SUPPLY_PROP_SERIAL_NUMBER,
149 POWER_SUPPLY_PROP_TEMP,
150 POWER_SUPPLY_PROP_VOLTAGE_NOW,
153 static char *test_power_ac_supplied_to[] = {
157 static struct power_supply *test_power_supplies[TEST_POWER_NUM];
159 static const struct power_supply_desc test_power_desc[] = {
162 .type = POWER_SUPPLY_TYPE_MAINS,
163 .properties = test_power_ac_props,
164 .num_properties = ARRAY_SIZE(test_power_ac_props),
165 .get_property = test_power_get_ac_property,
168 .name = "test_battery",
169 .type = POWER_SUPPLY_TYPE_BATTERY,
170 .properties = test_power_battery_props,
171 .num_properties = ARRAY_SIZE(test_power_battery_props),
172 .get_property = test_power_get_battery_property,
176 .type = POWER_SUPPLY_TYPE_USB,
177 .properties = test_power_ac_props,
178 .num_properties = ARRAY_SIZE(test_power_ac_props),
179 .get_property = test_power_get_usb_property,
183 static const struct power_supply_config test_power_configs[] = {
186 .supplied_to = test_power_ac_supplied_to,
187 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
192 .supplied_to = test_power_ac_supplied_to,
193 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
197 static int __init test_power_init(void)
201 struct device_node *dev_node;
203 dev_node = of_find_node_by_name(NULL, "test-power");
206 pr_info("%s: could not find dev node\n", __func__);
209 if (!of_device_is_available(dev_node)) {
210 pr_info("%s: test power disabled\n", __func__);
213 of_node_put(dev_node);
215 BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies));
216 BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_configs));
218 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
219 test_power_supplies[i] = power_supply_register(NULL,
221 &test_power_configs[i]);
222 if (IS_ERR(test_power_supplies[i])) {
223 pr_err("%s: failed to register %s\n", __func__,
224 test_power_desc[i].name);
225 ret = PTR_ERR(test_power_supplies[i]);
230 module_initialized = true;
234 power_supply_unregister(test_power_supplies[i]);
237 module_init(test_power_init);
239 static void __exit test_power_exit(void)
243 /* Let's see how we handle changes... */
246 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
247 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
248 power_supply_changed(test_power_supplies[i]);
249 pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
253 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
254 power_supply_unregister(test_power_supplies[i]);
256 module_initialized = false;
258 module_exit(test_power_exit);
262 #define MAX_KEYLENGTH 256
263 struct battery_property_map {
268 static struct battery_property_map map_ac_online[] = {
274 static struct battery_property_map map_status[] = {
275 { POWER_SUPPLY_STATUS_CHARGING, "charging" },
276 { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
277 { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
278 { POWER_SUPPLY_STATUS_FULL, "full" },
282 static struct battery_property_map map_health[] = {
283 { POWER_SUPPLY_HEALTH_GOOD, "good" },
284 { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
285 { POWER_SUPPLY_HEALTH_DEAD, "dead" },
286 { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
287 { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
291 static struct battery_property_map map_present[] = {
297 static struct battery_property_map map_technology[] = {
298 { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
299 { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
300 { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
301 { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
302 { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
303 { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
308 static int map_get_value(struct battery_property_map *map, const char *key,
311 char buf[MAX_KEYLENGTH];
314 strncpy(buf, key, MAX_KEYLENGTH);
315 buf[MAX_KEYLENGTH-1] = '\0';
317 cr = strnlen(buf, MAX_KEYLENGTH) - 1;
322 if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
331 static const char *map_get_key(struct battery_property_map *map, int value,
335 if (map->value == value)
343 static inline void signal_power_supply_changed(struct power_supply *psy)
345 if (module_initialized)
346 power_supply_changed(psy);
349 static int param_set_ac_online(const char *key, const struct kernel_param *kp)
351 ac_online = map_get_value(map_ac_online, key, ac_online);
352 signal_power_supply_changed(test_power_supplies[TEST_AC]);
356 static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
358 strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
359 return strlen(buffer);
362 static int param_set_usb_online(const char *key, const struct kernel_param *kp)
364 usb_online = map_get_value(map_ac_online, key, usb_online);
365 signal_power_supply_changed(test_power_supplies[TEST_USB]);
369 static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
371 strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown"));
372 return strlen(buffer);
375 static int param_set_battery_status(const char *key,
376 const struct kernel_param *kp)
378 battery_status = map_get_value(map_status, key, battery_status);
379 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
383 static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
385 strcpy(buffer, map_get_key(map_status, battery_status, "unknown"));
386 return strlen(buffer);
389 static int param_set_battery_health(const char *key,
390 const struct kernel_param *kp)
392 battery_health = map_get_value(map_health, key, battery_health);
393 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
397 static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
399 strcpy(buffer, map_get_key(map_health, battery_health, "unknown"));
400 return strlen(buffer);
403 static int param_set_battery_present(const char *key,
404 const struct kernel_param *kp)
406 battery_present = map_get_value(map_present, key, battery_present);
407 signal_power_supply_changed(test_power_supplies[TEST_AC]);
411 static int param_get_battery_present(char *buffer,
412 const struct kernel_param *kp)
414 strcpy(buffer, map_get_key(map_present, battery_present, "unknown"));
415 return strlen(buffer);
418 static int param_set_battery_technology(const char *key,
419 const struct kernel_param *kp)
421 battery_technology = map_get_value(map_technology, key,
423 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
427 static int param_get_battery_technology(char *buffer,
428 const struct kernel_param *kp)
431 map_get_key(map_technology, battery_technology, "unknown"));
432 return strlen(buffer);
435 static int param_set_battery_capacity(const char *key,
436 const struct kernel_param *kp)
440 if (1 != sscanf(key, "%d", &tmp))
443 battery_capacity = tmp;
444 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
448 #define param_get_battery_capacity param_get_int
450 static int param_set_battery_voltage(const char *key,
451 const struct kernel_param *kp)
455 if (1 != sscanf(key, "%d", &tmp))
458 battery_voltage = tmp;
459 signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
463 #define param_get_battery_voltage param_get_int
465 static const struct kernel_param_ops param_ops_ac_online = {
466 .set = param_set_ac_online,
467 .get = param_get_ac_online,
470 static const struct kernel_param_ops param_ops_usb_online = {
471 .set = param_set_usb_online,
472 .get = param_get_usb_online,
475 static const struct kernel_param_ops param_ops_battery_status = {
476 .set = param_set_battery_status,
477 .get = param_get_battery_status,
480 static const struct kernel_param_ops param_ops_battery_present = {
481 .set = param_set_battery_present,
482 .get = param_get_battery_present,
485 static const struct kernel_param_ops param_ops_battery_technology = {
486 .set = param_set_battery_technology,
487 .get = param_get_battery_technology,
490 static const struct kernel_param_ops param_ops_battery_health = {
491 .set = param_set_battery_health,
492 .get = param_get_battery_health,
495 static const struct kernel_param_ops param_ops_battery_capacity = {
496 .set = param_set_battery_capacity,
497 .get = param_get_battery_capacity,
500 static const struct kernel_param_ops param_ops_battery_voltage = {
501 .set = param_set_battery_voltage,
502 .get = param_get_battery_voltage,
505 #define param_check_ac_online(name, p) __param_check(name, p, void);
506 #define param_check_usb_online(name, p) __param_check(name, p, void);
507 #define param_check_battery_status(name, p) __param_check(name, p, void);
508 #define param_check_battery_present(name, p) __param_check(name, p, void);
509 #define param_check_battery_technology(name, p) __param_check(name, p, void);
510 #define param_check_battery_health(name, p) __param_check(name, p, void);
511 #define param_check_battery_capacity(name, p) __param_check(name, p, void);
512 #define param_check_battery_voltage(name, p) __param_check(name, p, void);
515 module_param(ac_online, ac_online, 0644);
516 MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
518 module_param(usb_online, usb_online, 0644);
519 MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
521 module_param(battery_status, battery_status, 0644);
522 MODULE_PARM_DESC(battery_status,
523 "battery status <charging|discharging|not-charging|full>");
525 module_param(battery_present, battery_present, 0644);
526 MODULE_PARM_DESC(battery_present,
527 "battery presence state <good|overheat|dead|overvoltage|failure>");
529 module_param(battery_technology, battery_technology, 0644);
530 MODULE_PARM_DESC(battery_technology,
531 "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
533 module_param(battery_health, battery_health, 0644);
534 MODULE_PARM_DESC(battery_health,
535 "battery health state <good|overheat|dead|overvoltage|failure>");
537 module_param(battery_capacity, battery_capacity, 0644);
538 MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
540 module_param(battery_voltage, battery_voltage, 0644);
541 MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
543 MODULE_DESCRIPTION("Power supply driver for testing");
544 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
545 MODULE_LICENSE("GPL");