test_power: Add support for USB AC source
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / power / test_power.c
CommitLineData
a1e50fd4
AV
1/*
2 * Power supply driver for testing.
3 *
4 * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com>
5 *
f17ef9b2
JS
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
11 *
a1e50fd4
AV
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.
15 */
16
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>
23
f17ef9b2 24static int ac_online = 1;
73847375 25static int usb_online = 1;
f17ef9b2
JS
26static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
27static int battery_health = POWER_SUPPLY_HEALTH_GOOD;
28static int battery_present = 1; /* true */
29static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
30static int battery_capacity = 50;
a1e50fd4
AV
31
32static int test_power_get_ac_property(struct power_supply *psy,
33 enum power_supply_property psp,
34 union power_supply_propval *val)
35{
36 switch (psp) {
37 case POWER_SUPPLY_PROP_ONLINE:
f17ef9b2 38 val->intval = ac_online;
a1e50fd4
AV
39 break;
40 default:
41 return -EINVAL;
42 }
43 return 0;
44}
45
73847375
DES
46static int test_power_get_usb_property(struct power_supply *psy,
47 enum power_supply_property psp,
48 union power_supply_propval *val)
49{
50 switch (psp) {
51 case POWER_SUPPLY_PROP_ONLINE:
52 val->intval = usb_online;
53 break;
54 default:
55 return -EINVAL;
56 }
57 return 0;
58}
59
a1e50fd4
AV
60static int test_power_get_battery_property(struct power_supply *psy,
61 enum power_supply_property psp,
62 union power_supply_propval *val)
63{
64 switch (psp) {
65 case POWER_SUPPLY_PROP_MODEL_NAME:
66 val->strval = "Test battery";
67 break;
68 case POWER_SUPPLY_PROP_MANUFACTURER:
69 val->strval = "Linux";
70 break;
71 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
72 val->strval = UTS_RELEASE;
73 break;
74 case POWER_SUPPLY_PROP_STATUS:
f17ef9b2 75 val->intval = battery_status;
a1e50fd4
AV
76 break;
77 case POWER_SUPPLY_PROP_CHARGE_TYPE:
78 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
79 break;
80 case POWER_SUPPLY_PROP_HEALTH:
f17ef9b2
JS
81 val->intval = battery_health;
82 break;
83 case POWER_SUPPLY_PROP_PRESENT:
84 val->intval = battery_present;
a1e50fd4
AV
85 break;
86 case POWER_SUPPLY_PROP_TECHNOLOGY:
f17ef9b2 87 val->intval = battery_technology;
a1e50fd4
AV
88 break;
89 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
90 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
91 break;
92 case POWER_SUPPLY_PROP_CAPACITY:
f17ef9b2
JS
93 case POWER_SUPPLY_PROP_CHARGE_NOW:
94 val->intval = battery_capacity;
95 break;
96 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
97 case POWER_SUPPLY_PROP_CHARGE_FULL:
98 val->intval = 100;
a1e50fd4
AV
99 break;
100 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
101 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
102 val->intval = 3600;
103 break;
104 default:
105 pr_info("%s: some properties deliberately report errors.\n",
106 __func__);
107 return -EINVAL;
108 }
109 return 0;
110}
111
112static enum power_supply_property test_power_ac_props[] = {
113 POWER_SUPPLY_PROP_ONLINE,
114};
115
116static enum power_supply_property test_power_battery_props[] = {
117 POWER_SUPPLY_PROP_STATUS,
118 POWER_SUPPLY_PROP_CHARGE_TYPE,
119 POWER_SUPPLY_PROP_HEALTH,
f17ef9b2 120 POWER_SUPPLY_PROP_PRESENT,
a1e50fd4 121 POWER_SUPPLY_PROP_TECHNOLOGY,
f17ef9b2 122 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
a1e50fd4 123 POWER_SUPPLY_PROP_CHARGE_FULL,
f17ef9b2 124 POWER_SUPPLY_PROP_CHARGE_NOW,
a1e50fd4
AV
125 POWER_SUPPLY_PROP_CAPACITY,
126 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
127 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
128 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
129 POWER_SUPPLY_PROP_MODEL_NAME,
130 POWER_SUPPLY_PROP_MANUFACTURER,
131 POWER_SUPPLY_PROP_SERIAL_NUMBER,
132};
133
134static char *test_power_ac_supplied_to[] = {
135 "test_battery",
136};
137
138static struct power_supply test_power_supplies[] = {
139 {
140 .name = "test_ac",
141 .type = POWER_SUPPLY_TYPE_MAINS,
142 .supplied_to = test_power_ac_supplied_to,
143 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
144 .properties = test_power_ac_props,
145 .num_properties = ARRAY_SIZE(test_power_ac_props),
146 .get_property = test_power_get_ac_property,
147 }, {
148 .name = "test_battery",
149 .type = POWER_SUPPLY_TYPE_BATTERY,
150 .properties = test_power_battery_props,
151 .num_properties = ARRAY_SIZE(test_power_battery_props),
152 .get_property = test_power_get_battery_property,
73847375
DES
153 }, {
154 .name = "test_usb",
155 .type = POWER_SUPPLY_TYPE_USB,
156 .supplied_to = test_power_ac_supplied_to,
157 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
158 .properties = test_power_ac_props,
159 .num_properties = ARRAY_SIZE(test_power_ac_props),
160 .get_property = test_power_get_usb_property,
a1e50fd4
AV
161 },
162};
163
f17ef9b2 164
a1e50fd4
AV
165static int __init test_power_init(void)
166{
167 int i;
168 int ret;
169
170 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
171 ret = power_supply_register(NULL, &test_power_supplies[i]);
172 if (ret) {
173 pr_err("%s: failed to register %s\n", __func__,
174 test_power_supplies[i].name);
175 goto failed;
176 }
177 }
178
179 return 0;
180failed:
181 while (--i >= 0)
182 power_supply_unregister(&test_power_supplies[i]);
183 return ret;
184}
185module_init(test_power_init);
186
187static void __exit test_power_exit(void)
188{
189 int i;
190
191 /* Let's see how we handle changes... */
f17ef9b2 192 ac_online = 0;
73847375 193 usb_online = 0;
f17ef9b2 194 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
a1e50fd4
AV
195 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
196 power_supply_changed(&test_power_supplies[i]);
197 pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
198 __func__);
199 ssleep(10);
200
201 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
202 power_supply_unregister(&test_power_supplies[i]);
203}
204module_exit(test_power_exit);
205
f17ef9b2
JS
206
207
208#define MAX_KEYLENGTH 256
209struct battery_property_map {
210 int value;
211 char const *key;
212};
213
214static struct battery_property_map map_ac_online[] = {
215 { 0, "on" },
216 { 1, "off" },
217 { -1, NULL },
218};
219
220static struct battery_property_map map_status[] = {
221 { POWER_SUPPLY_STATUS_CHARGING, "charging" },
222 { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
223 { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
224 { POWER_SUPPLY_STATUS_FULL, "full" },
225 { -1, NULL },
226};
227
228static struct battery_property_map map_health[] = {
229 { POWER_SUPPLY_HEALTH_GOOD, "good" },
230 { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
231 { POWER_SUPPLY_HEALTH_DEAD, "dead" },
232 { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
233 { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
234 { -1, NULL },
235};
236
237static struct battery_property_map map_present[] = {
238 { 0, "false" },
239 { 1, "true" },
240 { -1, NULL },
241};
242
243static struct battery_property_map map_technology[] = {
244 { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
245 { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
246 { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
247 { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
248 { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
249 { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
250 { -1, NULL },
251};
252
253
254static int map_get_value(struct battery_property_map *map, const char *key,
255 int def_val)
256{
257 char buf[MAX_KEYLENGTH];
258 int cr;
259
260 strncpy(buf, key, MAX_KEYLENGTH);
261 buf[MAX_KEYLENGTH-1] = '\0';
262
263 cr = strnlen(buf, MAX_KEYLENGTH) - 1;
264 if (buf[cr] == '\n')
265 buf[cr] = '\0';
266
267 while (map->key) {
268 if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
269 return map->value;
270 map++;
271 }
272
273 return def_val;
274}
275
276
277static const char *map_get_key(struct battery_property_map *map, int value,
278 const char *def_key)
279{
280 while (map->key) {
281 if (map->value == value)
282 return map->key;
283 map++;
284 }
285
286 return def_key;
287}
288
289static int param_set_ac_online(const char *key, const struct kernel_param *kp)
290{
291 ac_online = map_get_value(map_ac_online, key, ac_online);
292 power_supply_changed(&test_power_supplies[0]);
293 return 0;
294}
295
296static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
297{
298 strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
299 return strlen(buffer);
300}
301
73847375
DES
302static int param_set_usb_online(const char *key, const struct kernel_param *kp)
303{
304 usb_online = map_get_value(map_ac_online, key, usb_online);
305 power_supply_changed(&test_power_supplies[2]);
306 return 0;
307}
308
309static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
310{
311 strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown"));
312 return strlen(buffer);
313}
314
f17ef9b2
JS
315static int param_set_battery_status(const char *key,
316 const struct kernel_param *kp)
317{
318 battery_status = map_get_value(map_status, key, battery_status);
319 power_supply_changed(&test_power_supplies[1]);
320 return 0;
321}
322
323static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
324{
325 strcpy(buffer, map_get_key(map_status, battery_status, "unknown"));
326 return strlen(buffer);
327}
328
329static int param_set_battery_health(const char *key,
330 const struct kernel_param *kp)
331{
332 battery_health = map_get_value(map_health, key, battery_health);
333 power_supply_changed(&test_power_supplies[1]);
334 return 0;
335}
336
337static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
338{
339 strcpy(buffer, map_get_key(map_health, battery_health, "unknown"));
340 return strlen(buffer);
341}
342
343static int param_set_battery_present(const char *key,
344 const struct kernel_param *kp)
345{
346 battery_present = map_get_value(map_present, key, battery_present);
347 power_supply_changed(&test_power_supplies[0]);
348 return 0;
349}
350
351static int param_get_battery_present(char *buffer,
352 const struct kernel_param *kp)
353{
354 strcpy(buffer, map_get_key(map_present, battery_present, "unknown"));
355 return strlen(buffer);
356}
357
358static int param_set_battery_technology(const char *key,
359 const struct kernel_param *kp)
360{
361 battery_technology = map_get_value(map_technology, key,
362 battery_technology);
363 power_supply_changed(&test_power_supplies[1]);
364 return 0;
365}
366
367static int param_get_battery_technology(char *buffer,
368 const struct kernel_param *kp)
369{
370 strcpy(buffer,
371 map_get_key(map_technology, battery_technology, "unknown"));
372 return strlen(buffer);
373}
374
375static int param_set_battery_capacity(const char *key,
376 const struct kernel_param *kp)
377{
378 int tmp;
379
380 if (1 != sscanf(key, "%d", &tmp))
381 return -EINVAL;
382
383 battery_capacity = tmp;
384 power_supply_changed(&test_power_supplies[1]);
385 return 0;
386}
387
388#define param_get_battery_capacity param_get_int
389
f17ef9b2
JS
390static struct kernel_param_ops param_ops_ac_online = {
391 .set = param_set_ac_online,
392 .get = param_get_ac_online,
393};
394
73847375
DES
395static struct kernel_param_ops param_ops_usb_online = {
396 .set = param_set_usb_online,
397 .get = param_get_usb_online,
398};
399
f17ef9b2
JS
400static struct kernel_param_ops param_ops_battery_status = {
401 .set = param_set_battery_status,
402 .get = param_get_battery_status,
403};
404
405static struct kernel_param_ops param_ops_battery_present = {
406 .set = param_set_battery_present,
407 .get = param_get_battery_present,
408};
409
410static struct kernel_param_ops param_ops_battery_technology = {
411 .set = param_set_battery_technology,
412 .get = param_get_battery_technology,
413};
414
415static struct kernel_param_ops param_ops_battery_health = {
416 .set = param_set_battery_health,
417 .get = param_get_battery_health,
418};
419
420static struct kernel_param_ops param_ops_battery_capacity = {
421 .set = param_set_battery_capacity,
422 .get = param_get_battery_capacity,
423};
424
425
426#define param_check_ac_online(name, p) __param_check(name, p, void);
73847375 427#define param_check_usb_online(name, p) __param_check(name, p, void);
f17ef9b2
JS
428#define param_check_battery_status(name, p) __param_check(name, p, void);
429#define param_check_battery_present(name, p) __param_check(name, p, void);
430#define param_check_battery_technology(name, p) __param_check(name, p, void);
431#define param_check_battery_health(name, p) __param_check(name, p, void);
432#define param_check_battery_capacity(name, p) __param_check(name, p, void);
433
434
435module_param(ac_online, ac_online, 0644);
436MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
437
73847375
DES
438module_param(usb_online, usb_online, 0644);
439MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
440
f17ef9b2
JS
441module_param(battery_status, battery_status, 0644);
442MODULE_PARM_DESC(battery_status,
443 "battery status <charging|discharging|not-charging|full>");
444
445module_param(battery_present, battery_present, 0644);
446MODULE_PARM_DESC(battery_present,
447 "battery presence state <good|overheat|dead|overvoltage|failure>");
448
449module_param(battery_technology, battery_technology, 0644);
450MODULE_PARM_DESC(battery_technology,
451 "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
452
453module_param(battery_health, battery_health, 0644);
454MODULE_PARM_DESC(battery_health,
455 "battery health state <good|overheat|dead|overvoltage|failure>");
456
457module_param(battery_capacity, battery_capacity, 0644);
458MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
459
460
a1e50fd4
AV
461MODULE_DESCRIPTION("Power supply driver for testing");
462MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
463MODULE_LICENSE("GPL");