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>
24 #include <linux/of_address.h>
25 #include <linux/err.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;
37 static bool module_initialized;
39 static int test_power_get_ac_property(struct power_supply *psy,
40 enum power_supply_property psp,
41 union power_supply_propval *val)
44 case POWER_SUPPLY_PROP_ONLINE:
45 val->intval = ac_online;
53 static int test_power_get_usb_property(struct power_supply *psy,
54 enum power_supply_property psp,
55 union power_supply_propval *val)
58 case POWER_SUPPLY_PROP_ONLINE:
59 val->intval = usb_online;
67 static int test_power_get_battery_property(struct power_supply *psy,
68 enum power_supply_property psp,
69 union power_supply_propval *val)
72 case POWER_SUPPLY_PROP_MODEL_NAME:
73 val->strval = "Test battery";
75 case POWER_SUPPLY_PROP_MANUFACTURER:
76 val->strval = "Linux";
78 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
79 val->strval = UTS_RELEASE;
81 case POWER_SUPPLY_PROP_STATUS:
82 val->intval = battery_status;
84 case POWER_SUPPLY_PROP_CHARGE_TYPE:
85 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
87 case POWER_SUPPLY_PROP_HEALTH:
88 val->intval = battery_health;
90 case POWER_SUPPLY_PROP_PRESENT:
91 val->intval = battery_present;
93 case POWER_SUPPLY_PROP_TECHNOLOGY:
94 val->intval = battery_technology;
96 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
97 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
99 case POWER_SUPPLY_PROP_CAPACITY:
100 case POWER_SUPPLY_PROP_CHARGE_NOW:
101 val->intval = battery_capacity;
103 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
104 case POWER_SUPPLY_PROP_CHARGE_FULL:
107 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
108 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
111 case POWER_SUPPLY_PROP_TEMP:
114 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
115 val->intval = battery_voltage;
118 pr_info("%s: some properties deliberately report errors.\n",
125 static enum power_supply_property test_power_ac_props[] = {
126 POWER_SUPPLY_PROP_ONLINE,
129 static enum power_supply_property test_power_battery_props[] = {
130 POWER_SUPPLY_PROP_STATUS,
131 POWER_SUPPLY_PROP_CHARGE_TYPE,
132 POWER_SUPPLY_PROP_HEALTH,
133 POWER_SUPPLY_PROP_PRESENT,
134 POWER_SUPPLY_PROP_TECHNOLOGY,
135 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
136 POWER_SUPPLY_PROP_CHARGE_FULL,
137 POWER_SUPPLY_PROP_CHARGE_NOW,
138 POWER_SUPPLY_PROP_CAPACITY,
139 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
140 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
141 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
142 POWER_SUPPLY_PROP_MODEL_NAME,
143 POWER_SUPPLY_PROP_MANUFACTURER,
144 POWER_SUPPLY_PROP_SERIAL_NUMBER,
145 POWER_SUPPLY_PROP_TEMP,
146 POWER_SUPPLY_PROP_VOLTAGE_NOW,
149 static char *test_power_ac_supplied_to[] = {
153 static struct power_supply test_power_supplies[] = {
156 .type = POWER_SUPPLY_TYPE_MAINS,
157 .supplied_to = test_power_ac_supplied_to,
158 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
159 .properties = test_power_ac_props,
160 .num_properties = ARRAY_SIZE(test_power_ac_props),
161 .get_property = test_power_get_ac_property,
163 .name = "test_battery",
164 .type = POWER_SUPPLY_TYPE_BATTERY,
165 .properties = test_power_battery_props,
166 .num_properties = ARRAY_SIZE(test_power_battery_props),
167 .get_property = test_power_get_battery_property,
170 .type = POWER_SUPPLY_TYPE_USB,
171 .supplied_to = test_power_ac_supplied_to,
172 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
173 .properties = test_power_ac_props,
174 .num_properties = ARRAY_SIZE(test_power_ac_props),
175 .get_property = test_power_get_usb_property,
180 static int __init test_power_init(void)
184 struct device_node *dev_node;
186 dev_node = of_find_node_by_name(NULL, "test-power");
188 if (IS_ERR_OR_NULL(dev_node)) {
189 pr_info("not find %s dev node\n", __func__);
192 if (!of_device_is_available(dev_node)) {
193 pr_info("test power disabled\n");
197 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
198 ret = power_supply_register(NULL, &test_power_supplies[i]);
200 pr_err("%s: failed to register %s\n", __func__,
201 test_power_supplies[i].name);
206 module_initialized = true;
210 power_supply_unregister(&test_power_supplies[i]);
213 module_init(test_power_init);
215 static void __exit test_power_exit(void)
219 /* Let's see how we handle changes... */
222 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
223 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
224 power_supply_changed(&test_power_supplies[i]);
225 pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
229 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
230 power_supply_unregister(&test_power_supplies[i]);
232 module_initialized = false;
234 module_exit(test_power_exit);
238 #define MAX_KEYLENGTH 256
239 struct battery_property_map {
244 static struct battery_property_map map_ac_online[] = {
250 static struct battery_property_map map_status[] = {
251 { POWER_SUPPLY_STATUS_CHARGING, "charging" },
252 { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
253 { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
254 { POWER_SUPPLY_STATUS_FULL, "full" },
258 static struct battery_property_map map_health[] = {
259 { POWER_SUPPLY_HEALTH_GOOD, "good" },
260 { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
261 { POWER_SUPPLY_HEALTH_DEAD, "dead" },
262 { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
263 { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
267 static struct battery_property_map map_present[] = {
273 static struct battery_property_map map_technology[] = {
274 { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
275 { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
276 { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
277 { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
278 { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
279 { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
284 static int map_get_value(struct battery_property_map *map, const char *key,
287 char buf[MAX_KEYLENGTH];
290 strncpy(buf, key, MAX_KEYLENGTH);
291 buf[MAX_KEYLENGTH-1] = '\0';
293 cr = strnlen(buf, MAX_KEYLENGTH) - 1;
298 if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
307 static const char *map_get_key(struct battery_property_map *map, int value,
311 if (map->value == value)
319 static inline void signal_power_supply_changed(struct power_supply *psy)
321 if (module_initialized)
322 power_supply_changed(psy);
325 static int param_set_ac_online(const char *key, const struct kernel_param *kp)
327 ac_online = map_get_value(map_ac_online, key, ac_online);
328 signal_power_supply_changed(&test_power_supplies[0]);
332 static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
334 strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
335 return strlen(buffer);
338 static int param_set_usb_online(const char *key, const struct kernel_param *kp)
340 usb_online = map_get_value(map_ac_online, key, usb_online);
341 signal_power_supply_changed(&test_power_supplies[2]);
345 static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
347 strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown"));
348 return strlen(buffer);
351 static int param_set_battery_status(const char *key,
352 const struct kernel_param *kp)
354 battery_status = map_get_value(map_status, key, battery_status);
355 signal_power_supply_changed(&test_power_supplies[1]);
359 static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
361 strcpy(buffer, map_get_key(map_status, battery_status, "unknown"));
362 return strlen(buffer);
365 static int param_set_battery_health(const char *key,
366 const struct kernel_param *kp)
368 battery_health = map_get_value(map_health, key, battery_health);
369 signal_power_supply_changed(&test_power_supplies[1]);
373 static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
375 strcpy(buffer, map_get_key(map_health, battery_health, "unknown"));
376 return strlen(buffer);
379 static int param_set_battery_present(const char *key,
380 const struct kernel_param *kp)
382 battery_present = map_get_value(map_present, key, battery_present);
383 signal_power_supply_changed(&test_power_supplies[0]);
387 static int param_get_battery_present(char *buffer,
388 const struct kernel_param *kp)
390 strcpy(buffer, map_get_key(map_present, battery_present, "unknown"));
391 return strlen(buffer);
394 static int param_set_battery_technology(const char *key,
395 const struct kernel_param *kp)
397 battery_technology = map_get_value(map_technology, key,
399 signal_power_supply_changed(&test_power_supplies[1]);
403 static int param_get_battery_technology(char *buffer,
404 const struct kernel_param *kp)
407 map_get_key(map_technology, battery_technology, "unknown"));
408 return strlen(buffer);
411 static int param_set_battery_capacity(const char *key,
412 const struct kernel_param *kp)
416 if (1 != sscanf(key, "%d", &tmp))
419 battery_capacity = tmp;
420 signal_power_supply_changed(&test_power_supplies[1]);
424 #define param_get_battery_capacity param_get_int
426 static int param_set_battery_voltage(const char *key,
427 const struct kernel_param *kp)
431 if (1 != sscanf(key, "%d", &tmp))
434 battery_voltage = tmp;
435 signal_power_supply_changed(&test_power_supplies[1]);
439 #define param_get_battery_voltage param_get_int
441 static struct kernel_param_ops param_ops_ac_online = {
442 .set = param_set_ac_online,
443 .get = param_get_ac_online,
446 static struct kernel_param_ops param_ops_usb_online = {
447 .set = param_set_usb_online,
448 .get = param_get_usb_online,
451 static struct kernel_param_ops param_ops_battery_status = {
452 .set = param_set_battery_status,
453 .get = param_get_battery_status,
456 static struct kernel_param_ops param_ops_battery_present = {
457 .set = param_set_battery_present,
458 .get = param_get_battery_present,
461 static struct kernel_param_ops param_ops_battery_technology = {
462 .set = param_set_battery_technology,
463 .get = param_get_battery_technology,
466 static struct kernel_param_ops param_ops_battery_health = {
467 .set = param_set_battery_health,
468 .get = param_get_battery_health,
471 static struct kernel_param_ops param_ops_battery_capacity = {
472 .set = param_set_battery_capacity,
473 .get = param_get_battery_capacity,
476 static struct kernel_param_ops param_ops_battery_voltage = {
477 .set = param_set_battery_voltage,
478 .get = param_get_battery_voltage,
481 #define param_check_ac_online(name, p) __param_check(name, p, void);
482 #define param_check_usb_online(name, p) __param_check(name, p, void);
483 #define param_check_battery_status(name, p) __param_check(name, p, void);
484 #define param_check_battery_present(name, p) __param_check(name, p, void);
485 #define param_check_battery_technology(name, p) __param_check(name, p, void);
486 #define param_check_battery_health(name, p) __param_check(name, p, void);
487 #define param_check_battery_capacity(name, p) __param_check(name, p, void);
488 #define param_check_battery_voltage(name, p) __param_check(name, p, void);
491 module_param(ac_online, ac_online, 0644);
492 MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
494 module_param(usb_online, usb_online, 0644);
495 MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
497 module_param(battery_status, battery_status, 0644);
498 MODULE_PARM_DESC(battery_status,
499 "battery status <charging|discharging|not-charging|full>");
501 module_param(battery_present, battery_present, 0644);
502 MODULE_PARM_DESC(battery_present,
503 "battery presence state <good|overheat|dead|overvoltage|failure>");
505 module_param(battery_technology, battery_technology, 0644);
506 MODULE_PARM_DESC(battery_technology,
507 "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
509 module_param(battery_health, battery_health, 0644);
510 MODULE_PARM_DESC(battery_health,
511 "battery health state <good|overheat|dead|overvoltage|failure>");
513 module_param(battery_capacity, battery_capacity, 0644);
514 MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
516 module_param(battery_voltage, battery_voltage, 0644);
517 MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
519 MODULE_DESCRIPTION("Power supply driver for testing");
520 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
521 MODULE_LICENSE("GPL");