^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Power supply driver for testing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Dynamic module parameter code from the Virtual Battery Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2008 Pylone, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * By: Masashi YOKOTA <yokota@pylone.jp>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Originally found here:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * http://downloads.pylone.jp/src/virtual_battery/virtual_battery-0.0.1.tar.bz2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <generated/utsrelease.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) enum test_power_id {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) TEST_AC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) TEST_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) TEST_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) TEST_POWER_NUM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int ac_online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int usb_online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int battery_health = POWER_SUPPLY_HEALTH_GOOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int battery_present = 1; /* true */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int battery_capacity = 50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int battery_voltage = 3300;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int battery_charge_counter = -1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static int battery_current = -1600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static bool module_initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int test_power_get_ac_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) val->intval = ac_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int test_power_get_usb_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) val->intval = usb_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static int test_power_get_battery_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) case POWER_SUPPLY_PROP_MODEL_NAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) val->strval = "Test battery";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) case POWER_SUPPLY_PROP_MANUFACTURER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) val->strval = "Linux";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) case POWER_SUPPLY_PROP_SERIAL_NUMBER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) val->strval = UTS_RELEASE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) val->intval = battery_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) case POWER_SUPPLY_PROP_CHARGE_TYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) case POWER_SUPPLY_PROP_HEALTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) val->intval = battery_health;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) val->intval = battery_present;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) case POWER_SUPPLY_PROP_TECHNOLOGY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) val->intval = battery_technology;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) case POWER_SUPPLY_PROP_CAPACITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) case POWER_SUPPLY_PROP_CHARGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) val->intval = battery_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) case POWER_SUPPLY_PROP_CHARGE_COUNTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) val->intval = battery_charge_counter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) case POWER_SUPPLY_PROP_CHARGE_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) val->intval = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) val->intval = 3600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) case POWER_SUPPLY_PROP_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) val->intval = 26;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) val->intval = battery_voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) case POWER_SUPPLY_PROP_CURRENT_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) val->intval = battery_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) pr_info("%s: some properties deliberately report errors.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static enum power_supply_property test_power_ac_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static enum power_supply_property test_power_battery_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) POWER_SUPPLY_PROP_CHARGE_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) POWER_SUPPLY_PROP_HEALTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) POWER_SUPPLY_PROP_TECHNOLOGY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) POWER_SUPPLY_PROP_CHARGE_FULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) POWER_SUPPLY_PROP_CHARGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) POWER_SUPPLY_PROP_CHARGE_COUNTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) POWER_SUPPLY_PROP_CAPACITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) POWER_SUPPLY_PROP_CAPACITY_LEVEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) POWER_SUPPLY_PROP_MODEL_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) POWER_SUPPLY_PROP_MANUFACTURER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) POWER_SUPPLY_PROP_SERIAL_NUMBER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) POWER_SUPPLY_PROP_TEMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) POWER_SUPPLY_PROP_CURRENT_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static char *test_power_ac_supplied_to[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) "test_battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static struct power_supply *test_power_supplies[TEST_POWER_NUM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static const struct power_supply_desc test_power_desc[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) [TEST_AC] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) .name = "test_ac",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) .type = POWER_SUPPLY_TYPE_MAINS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) .properties = test_power_ac_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .num_properties = ARRAY_SIZE(test_power_ac_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .get_property = test_power_get_ac_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) [TEST_BATTERY] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .name = "test_battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .type = POWER_SUPPLY_TYPE_BATTERY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .properties = test_power_battery_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .num_properties = ARRAY_SIZE(test_power_battery_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .get_property = test_power_get_battery_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) [TEST_USB] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) .name = "test_usb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) .type = POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) .properties = test_power_ac_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) .num_properties = ARRAY_SIZE(test_power_ac_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) .get_property = test_power_get_usb_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static const struct power_supply_config test_power_configs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* test_ac */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) .supplied_to = test_power_ac_supplied_to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /* test_battery */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* test_usb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) .supplied_to = test_power_ac_supplied_to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int __init test_power_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct device_node *dev_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) dev_node = of_find_node_by_name(NULL, "test-power");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (!dev_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) pr_info("%s: could not find dev node\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (!of_device_is_available(dev_node)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) pr_info("%s: test power disabled\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) of_node_put(dev_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_configs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) test_power_supplies[i] = power_supply_register(NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) &test_power_desc[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) &test_power_configs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (IS_ERR(test_power_supplies[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) pr_err("%s: failed to register %s\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) test_power_desc[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ret = PTR_ERR(test_power_supplies[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) module_initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) while (--i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) power_supply_unregister(test_power_supplies[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) module_init(test_power_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static void __exit test_power_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /* Let's see how we handle changes... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ac_online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) usb_online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) power_supply_changed(test_power_supplies[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) ssleep(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) power_supply_unregister(test_power_supplies[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) module_initialized = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) module_exit(test_power_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) #define MAX_KEYLENGTH 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct battery_property_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) char const *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static struct battery_property_map map_ac_online[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) { 0, "off" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) { 1, "on" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) { -1, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static struct battery_property_map map_status[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) { POWER_SUPPLY_STATUS_CHARGING, "charging" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) { POWER_SUPPLY_STATUS_FULL, "full" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) { -1, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static struct battery_property_map map_health[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) { POWER_SUPPLY_HEALTH_GOOD, "good" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) { POWER_SUPPLY_HEALTH_DEAD, "dead" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) { -1, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static struct battery_property_map map_present[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) { 0, "false" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) { 1, "true" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) { -1, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) static struct battery_property_map map_technology[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) { -1, NULL },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static int map_get_value(struct battery_property_map *map, const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int def_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) char buf[MAX_KEYLENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int cr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) strncpy(buf, key, MAX_KEYLENGTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) buf[MAX_KEYLENGTH-1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) cr = strnlen(buf, MAX_KEYLENGTH) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (cr < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return def_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (buf[cr] == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) buf[cr] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) while (map->key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return map->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) map++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return def_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static const char *map_get_key(struct battery_property_map *map, int value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) const char *def_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) while (map->key) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (map->value == value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return map->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) map++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return def_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static inline void signal_power_supply_changed(struct power_supply *psy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (module_initialized)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) power_supply_changed(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) static int param_set_ac_online(const char *key, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ac_online = map_get_value(map_ac_online, key, ac_online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) signal_power_supply_changed(test_power_supplies[TEST_AC]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return sprintf(buffer, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) map_get_key(map_ac_online, ac_online, "unknown"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static int param_set_usb_online(const char *key, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) usb_online = map_get_value(map_ac_online, key, usb_online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) signal_power_supply_changed(test_power_supplies[TEST_USB]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return sprintf(buffer, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) map_get_key(map_ac_online, usb_online, "unknown"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static int param_set_battery_status(const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) battery_status = map_get_value(map_status, key, battery_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) return sprintf(buffer, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) map_get_key(map_ac_online, battery_status, "unknown"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) static int param_set_battery_health(const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) battery_health = map_get_value(map_health, key, battery_health);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return sprintf(buffer, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) map_get_key(map_ac_online, battery_health, "unknown"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) static int param_set_battery_present(const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) battery_present = map_get_value(map_present, key, battery_present);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) signal_power_supply_changed(test_power_supplies[TEST_AC]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static int param_get_battery_present(char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return sprintf(buffer, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) map_get_key(map_ac_online, battery_present, "unknown"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static int param_set_battery_technology(const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) battery_technology = map_get_value(map_technology, key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) battery_technology);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static int param_get_battery_technology(char *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return sprintf(buffer, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) map_get_key(map_ac_online, battery_technology,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) "unknown"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static int param_set_battery_capacity(const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (1 != sscanf(key, "%d", &tmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) battery_capacity = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) #define param_get_battery_capacity param_get_int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) static int param_set_battery_voltage(const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) if (1 != sscanf(key, "%d", &tmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) battery_voltage = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) #define param_get_battery_voltage param_get_int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static int param_set_battery_charge_counter(const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (1 != sscanf(key, "%d", &tmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) battery_charge_counter = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) #define param_get_battery_charge_counter param_get_int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static int param_set_battery_current(const char *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (1 != sscanf(key, "%d", &tmp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) battery_current = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) signal_power_supply_changed(test_power_supplies[TEST_BATTERY]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) #define param_get_battery_current param_get_int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static const struct kernel_param_ops param_ops_ac_online = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) .set = param_set_ac_online,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) .get = param_get_ac_online,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static const struct kernel_param_ops param_ops_usb_online = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) .set = param_set_usb_online,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) .get = param_get_usb_online,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) static const struct kernel_param_ops param_ops_battery_status = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) .set = param_set_battery_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) .get = param_get_battery_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) static const struct kernel_param_ops param_ops_battery_present = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) .set = param_set_battery_present,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) .get = param_get_battery_present,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) static const struct kernel_param_ops param_ops_battery_technology = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) .set = param_set_battery_technology,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) .get = param_get_battery_technology,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static const struct kernel_param_ops param_ops_battery_health = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) .set = param_set_battery_health,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) .get = param_get_battery_health,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) static const struct kernel_param_ops param_ops_battery_capacity = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) .set = param_set_battery_capacity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) .get = param_get_battery_capacity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) static const struct kernel_param_ops param_ops_battery_voltage = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) .set = param_set_battery_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) .get = param_get_battery_voltage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) static const struct kernel_param_ops param_ops_battery_charge_counter = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) .set = param_set_battery_charge_counter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) .get = param_get_battery_charge_counter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) static const struct kernel_param_ops param_ops_battery_current = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) .set = param_set_battery_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) .get = param_get_battery_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) #define param_check_ac_online(name, p) __param_check(name, p, void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) #define param_check_usb_online(name, p) __param_check(name, p, void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) #define param_check_battery_status(name, p) __param_check(name, p, void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) #define param_check_battery_present(name, p) __param_check(name, p, void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) #define param_check_battery_technology(name, p) __param_check(name, p, void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) #define param_check_battery_health(name, p) __param_check(name, p, void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) #define param_check_battery_capacity(name, p) __param_check(name, p, void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) #define param_check_battery_voltage(name, p) __param_check(name, p, void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) #define param_check_battery_charge_counter(name, p) __param_check(name, p, void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) #define param_check_battery_current(name, p) __param_check(name, p, void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) module_param(ac_online, ac_online, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) module_param(usb_online, usb_online, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) module_param(battery_status, battery_status, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) MODULE_PARM_DESC(battery_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) "battery status <charging|discharging|not-charging|full>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) module_param(battery_present, battery_present, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) MODULE_PARM_DESC(battery_present,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) "battery presence state <good|overheat|dead|overvoltage|failure>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) module_param(battery_technology, battery_technology, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) MODULE_PARM_DESC(battery_technology,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) module_param(battery_health, battery_health, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) MODULE_PARM_DESC(battery_health,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) "battery health state <good|overheat|dead|overvoltage|failure>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) module_param(battery_capacity, battery_capacity, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) module_param(battery_voltage, battery_voltage, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) module_param(battery_charge_counter, battery_charge_counter, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) MODULE_PARM_DESC(battery_charge_counter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) "battery charge counter (microampere-hours)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) module_param(battery_current, battery_current, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) MODULE_PARM_DESC(battery_current, "battery current (milliampere)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) MODULE_DESCRIPTION("Power supply driver for testing");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) MODULE_LICENSE("GPL");