UPSTREAM: PCI: rockchip: remove the pointer to L1 substate cap
[firefly-linux-kernel-4.4.55.git] / drivers / power / test_power.c
1 /*
2  * Power supply driver for testing.
3  *
4  * Copyright 2010  Anton Vorontsov <cbouatmailru@gmail.com>
5  *
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  *
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 #include <linux/of.h>
24
25 enum test_power_id {
26         TEST_AC,
27         TEST_BATTERY,
28         TEST_USB,
29         TEST_POWER_NUM,
30 };
31
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;
40
41 static bool module_initialized;
42
43 static int test_power_get_ac_property(struct power_supply *psy,
44                                       enum power_supply_property psp,
45                                       union power_supply_propval *val)
46 {
47         switch (psp) {
48         case POWER_SUPPLY_PROP_ONLINE:
49                 val->intval = ac_online;
50                 break;
51         default:
52                 return -EINVAL;
53         }
54         return 0;
55 }
56
57 static int test_power_get_usb_property(struct power_supply *psy,
58                                       enum power_supply_property psp,
59                                       union power_supply_propval *val)
60 {
61         switch (psp) {
62         case POWER_SUPPLY_PROP_ONLINE:
63                 val->intval = usb_online;
64                 break;
65         default:
66                 return -EINVAL;
67         }
68         return 0;
69 }
70
71 static int test_power_get_battery_property(struct power_supply *psy,
72                                            enum power_supply_property psp,
73                                            union power_supply_propval *val)
74 {
75         switch (psp) {
76         case POWER_SUPPLY_PROP_MODEL_NAME:
77                 val->strval = "Test battery";
78                 break;
79         case POWER_SUPPLY_PROP_MANUFACTURER:
80                 val->strval = "Linux";
81                 break;
82         case POWER_SUPPLY_PROP_SERIAL_NUMBER:
83                 val->strval = UTS_RELEASE;
84                 break;
85         case POWER_SUPPLY_PROP_STATUS:
86                 val->intval = battery_status;
87                 break;
88         case POWER_SUPPLY_PROP_CHARGE_TYPE:
89                 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
90                 break;
91         case POWER_SUPPLY_PROP_HEALTH:
92                 val->intval = battery_health;
93                 break;
94         case POWER_SUPPLY_PROP_PRESENT:
95                 val->intval = battery_present;
96                 break;
97         case POWER_SUPPLY_PROP_TECHNOLOGY:
98                 val->intval = battery_technology;
99                 break;
100         case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
101                 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
102                 break;
103         case POWER_SUPPLY_PROP_CAPACITY:
104         case POWER_SUPPLY_PROP_CHARGE_NOW:
105                 val->intval = battery_capacity;
106                 break;
107         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
108         case POWER_SUPPLY_PROP_CHARGE_FULL:
109                 val->intval = 100;
110                 break;
111         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
112         case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
113                 val->intval = 3600;
114                 break;
115         case POWER_SUPPLY_PROP_TEMP:
116                 val->intval = 26;
117                 break;
118         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
119                 val->intval = battery_voltage;
120                 break;
121         default:
122                 pr_info("%s: some properties deliberately report errors.\n",
123                         __func__);
124                 return -EINVAL;
125         }
126         return 0;
127 }
128
129 static enum power_supply_property test_power_ac_props[] = {
130         POWER_SUPPLY_PROP_ONLINE,
131 };
132
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,
151 };
152
153 static char *test_power_ac_supplied_to[] = {
154         "test_battery",
155 };
156
157 static struct power_supply *test_power_supplies[TEST_POWER_NUM];
158
159 static const struct power_supply_desc test_power_desc[] = {
160         [TEST_AC] = {
161                 .name = "test_ac",
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,
166         },
167         [TEST_BATTERY] = {
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,
173         },
174         [TEST_USB] = {
175                 .name = "test_usb",
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,
180         },
181 };
182
183 static const struct power_supply_config test_power_configs[] = {
184         {
185                 /* test_ac */
186                 .supplied_to = test_power_ac_supplied_to,
187                 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
188         }, {
189                 /* test_battery */
190         }, {
191                 /* test_usb */
192                 .supplied_to = test_power_ac_supplied_to,
193                 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
194         },
195 };
196
197 static int __init test_power_init(void)
198 {
199         int i;
200         int ret;
201         struct device_node *dev_node;
202
203         dev_node = of_find_node_by_name(NULL, "test-power");
204
205         if (!dev_node) {
206                 pr_info("%s: could not find dev node\n", __func__);
207                 return 0;
208         }
209         if (!of_device_is_available(dev_node)) {
210                 pr_info("%s: test power disabled\n", __func__);
211                 return 0;
212         }
213         of_node_put(dev_node);
214
215         BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies));
216         BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_configs));
217
218         for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
219                 test_power_supplies[i] = power_supply_register(NULL,
220                                                 &test_power_desc[i],
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]);
226                         goto failed;
227                 }
228         }
229
230         module_initialized = true;
231         return 0;
232 failed:
233         while (--i >= 0)
234                 power_supply_unregister(test_power_supplies[i]);
235         return ret;
236 }
237 module_init(test_power_init);
238
239 static void __exit test_power_exit(void)
240 {
241         int i;
242
243         /* Let's see how we handle changes... */
244         ac_online = 0;
245         usb_online = 0;
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",
250                 __func__);
251         ssleep(10);
252
253         for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
254                 power_supply_unregister(test_power_supplies[i]);
255
256         module_initialized = false;
257 }
258 module_exit(test_power_exit);
259
260
261
262 #define MAX_KEYLENGTH 256
263 struct battery_property_map {
264         int value;
265         char const *key;
266 };
267
268 static struct battery_property_map map_ac_online[] = {
269         { 0,  "off"  },
270         { 1,  "on" },
271         { -1, NULL  },
272 };
273
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"         },
279         { -1,                               NULL           },
280 };
281
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"     },
288         { -1,                                 NULL          },
289 };
290
291 static struct battery_property_map map_present[] = {
292         { 0,  "false" },
293         { 1,  "true"  },
294         { -1, NULL    },
295 };
296
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" },
304         { -1,                           NULL   },
305 };
306
307
308 static int map_get_value(struct battery_property_map *map, const char *key,
309                                 int def_val)
310 {
311         char buf[MAX_KEYLENGTH];
312         int cr;
313
314         strncpy(buf, key, MAX_KEYLENGTH);
315         buf[MAX_KEYLENGTH-1] = '\0';
316
317         cr = strnlen(buf, MAX_KEYLENGTH) - 1;
318         if (buf[cr] == '\n')
319                 buf[cr] = '\0';
320
321         while (map->key) {
322                 if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
323                         return map->value;
324                 map++;
325         }
326
327         return def_val;
328 }
329
330
331 static const char *map_get_key(struct battery_property_map *map, int value,
332                                 const char *def_key)
333 {
334         while (map->key) {
335                 if (map->value == value)
336                         return map->key;
337                 map++;
338         }
339
340         return def_key;
341 }
342
343 static inline void signal_power_supply_changed(struct power_supply *psy)
344 {
345         if (module_initialized)
346                 power_supply_changed(psy);
347 }
348
349 static int param_set_ac_online(const char *key, const struct kernel_param *kp)
350 {
351         ac_online = map_get_value(map_ac_online, key, ac_online);
352         signal_power_supply_changed(test_power_supplies[TEST_AC]);
353         return 0;
354 }
355
356 static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
357 {
358         strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
359         return strlen(buffer);
360 }
361
362 static int param_set_usb_online(const char *key, const struct kernel_param *kp)
363 {
364         usb_online = map_get_value(map_ac_online, key, usb_online);
365         signal_power_supply_changed(test_power_supplies[TEST_USB]);
366         return 0;
367 }
368
369 static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
370 {
371         strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown"));
372         return strlen(buffer);
373 }
374
375 static int param_set_battery_status(const char *key,
376                                         const struct kernel_param *kp)
377 {
378         battery_status = map_get_value(map_status, key, battery_status);
379         signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
380         return 0;
381 }
382
383 static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
384 {
385         strcpy(buffer, map_get_key(map_status, battery_status, "unknown"));
386         return strlen(buffer);
387 }
388
389 static int param_set_battery_health(const char *key,
390                                         const struct kernel_param *kp)
391 {
392         battery_health = map_get_value(map_health, key, battery_health);
393         signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
394         return 0;
395 }
396
397 static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
398 {
399         strcpy(buffer, map_get_key(map_health, battery_health, "unknown"));
400         return strlen(buffer);
401 }
402
403 static int param_set_battery_present(const char *key,
404                                         const struct kernel_param *kp)
405 {
406         battery_present = map_get_value(map_present, key, battery_present);
407         signal_power_supply_changed(test_power_supplies[TEST_AC]);
408         return 0;
409 }
410
411 static int param_get_battery_present(char *buffer,
412                                         const struct kernel_param *kp)
413 {
414         strcpy(buffer, map_get_key(map_present, battery_present, "unknown"));
415         return strlen(buffer);
416 }
417
418 static int param_set_battery_technology(const char *key,
419                                         const struct kernel_param *kp)
420 {
421         battery_technology = map_get_value(map_technology, key,
422                                                 battery_technology);
423         signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
424         return 0;
425 }
426
427 static int param_get_battery_technology(char *buffer,
428                                         const struct kernel_param *kp)
429 {
430         strcpy(buffer,
431                 map_get_key(map_technology, battery_technology, "unknown"));
432         return strlen(buffer);
433 }
434
435 static int param_set_battery_capacity(const char *key,
436                                         const struct kernel_param *kp)
437 {
438         int tmp;
439
440         if (1 != sscanf(key, "%d", &tmp))
441                 return -EINVAL;
442
443         battery_capacity = tmp;
444         signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
445         return 0;
446 }
447
448 #define param_get_battery_capacity param_get_int
449
450 static int param_set_battery_voltage(const char *key,
451                                         const struct kernel_param *kp)
452 {
453         int tmp;
454
455         if (1 != sscanf(key, "%d", &tmp))
456                 return -EINVAL;
457
458         battery_voltage = tmp;
459         signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
460         return 0;
461 }
462
463 #define param_get_battery_voltage param_get_int
464
465 static const struct kernel_param_ops param_ops_ac_online = {
466         .set = param_set_ac_online,
467         .get = param_get_ac_online,
468 };
469
470 static const struct kernel_param_ops param_ops_usb_online = {
471         .set = param_set_usb_online,
472         .get = param_get_usb_online,
473 };
474
475 static const struct kernel_param_ops param_ops_battery_status = {
476         .set = param_set_battery_status,
477         .get = param_get_battery_status,
478 };
479
480 static const struct kernel_param_ops param_ops_battery_present = {
481         .set = param_set_battery_present,
482         .get = param_get_battery_present,
483 };
484
485 static const struct kernel_param_ops param_ops_battery_technology = {
486         .set = param_set_battery_technology,
487         .get = param_get_battery_technology,
488 };
489
490 static const struct kernel_param_ops param_ops_battery_health = {
491         .set = param_set_battery_health,
492         .get = param_get_battery_health,
493 };
494
495 static const struct kernel_param_ops param_ops_battery_capacity = {
496         .set = param_set_battery_capacity,
497         .get = param_get_battery_capacity,
498 };
499
500 static const struct kernel_param_ops param_ops_battery_voltage = {
501         .set = param_set_battery_voltage,
502         .get = param_get_battery_voltage,
503 };
504
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);
513
514
515 module_param(ac_online, ac_online, 0644);
516 MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
517
518 module_param(usb_online, usb_online, 0644);
519 MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
520
521 module_param(battery_status, battery_status, 0644);
522 MODULE_PARM_DESC(battery_status,
523         "battery status <charging|discharging|not-charging|full>");
524
525 module_param(battery_present, battery_present, 0644);
526 MODULE_PARM_DESC(battery_present,
527         "battery presence state <good|overheat|dead|overvoltage|failure>");
528
529 module_param(battery_technology, battery_technology, 0644);
530 MODULE_PARM_DESC(battery_technology,
531         "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
532
533 module_param(battery_health, battery_health, 0644);
534 MODULE_PARM_DESC(battery_health,
535         "battery health state <good|overheat|dead|overvoltage|failure>");
536
537 module_param(battery_capacity, battery_capacity, 0644);
538 MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
539
540 module_param(battery_voltage, battery_voltage, 0644);
541 MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
542
543 MODULE_DESCRIPTION("Power supply driver for testing");
544 MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
545 MODULE_LICENSE("GPL");