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