Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Sysfs interface for the universal power supply monitor class |
| 3 | * |
| 4 | * Copyright © 2007 David Woodhouse <dwmw2@infradead.org> |
| 5 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> |
| 6 | * Copyright © 2004 Szabolcs Gyurko |
| 7 | * Copyright © 2003 Ian Molton <spyro@f2s.com> |
| 8 | * |
| 9 | * Modified: 2004, Oct Szabolcs Gyurko |
| 10 | * |
| 11 | * You may use this code as per GPL version 2 |
| 12 | */ |
| 13 | |
| 14 | #include <linux/ctype.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 15 | #include <linux/device.h> |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 16 | #include <linux/power_supply.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Paul Gortmaker | 778f523 | 2011-05-27 10:22:46 -0400 | [diff] [blame] | 18 | #include <linux/stat.h> |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 19 | |
Adrian Bunk | 25f12141 | 2007-11-26 00:25:45 +0300 | [diff] [blame] | 20 | #include "power_supply.h" |
| 21 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 22 | /* |
| 23 | * This is because the name "current" breaks the device attr macro. |
| 24 | * The "current" word resolves to "(get_current())" so instead of |
| 25 | * "current" "(get_current())" appears in the sysfs. |
| 26 | * |
| 27 | * The source of this definition is the device.h which calls __ATTR |
| 28 | * macro in sysfs.h which calls the __stringify macro. |
| 29 | * |
| 30 | * Only modification that the name is not tried to be resolved |
| 31 | * (as a macro let's say). |
| 32 | */ |
| 33 | |
| 34 | #define POWER_SUPPLY_ATTR(_name) \ |
| 35 | { \ |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 36 | .attr = { .name = #_name }, \ |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 37 | .show = power_supply_show_property, \ |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 38 | .store = power_supply_store_property, \ |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static struct device_attribute power_supply_attrs[]; |
| 42 | |
| 43 | static ssize_t power_supply_show_property(struct device *dev, |
| 44 | struct device_attribute *attr, |
| 45 | char *buf) { |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 46 | static char *type_text[] = { |
Abhijeet Dharmapurikar | 9a3a6c3 | 2017-01-06 18:27:29 -0800 | [diff] [blame] | 47 | "Unknown", "Battery", "UPS", "Mains", "USB", "USB_DCP", |
| 48 | "USB_CDP", "USB_ACA", "USB_HVDCP", "USB_HVDCP_3", "USB_PD", |
Abhijeet Dharmapurikar | 4e56cab | 2017-07-05 14:00:52 -0700 | [diff] [blame] | 49 | "Wireless", "USB_FLOAT", "BMS", "Parallel", "Main", "Wipower", |
Abhijeet Dharmapurikar | 9a3a6c3 | 2017-01-06 18:27:29 -0800 | [diff] [blame] | 50 | "TYPEC", "TYPEC_UFP", "TYPEC_DFP" |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 51 | }; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 52 | static char *status_text[] = { |
| 53 | "Unknown", "Charging", "Discharging", "Not charging", "Full" |
| 54 | }; |
Andres Salomon | ee8076e | 2009-07-02 09:45:18 -0400 | [diff] [blame] | 55 | static char *charge_type[] = { |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 56 | "Unknown", "N/A", "Trickle", "Fast", |
| 57 | "Taper" |
Andres Salomon | ee8076e | 2009-07-02 09:45:18 -0400 | [diff] [blame] | 58 | }; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 59 | static char *health_text[] = { |
| 60 | "Unknown", "Good", "Overheat", "Dead", "Over voltage", |
Ramakrishna Pallala | a05be99 | 2012-11-30 13:57:46 +0530 | [diff] [blame] | 61 | "Unspecified failure", "Cold", "Watchdog timer expire", |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 62 | "Safety timer expire", |
Nicholas Troast | 86cc4f67 | 2017-02-08 10:41:06 -0800 | [diff] [blame] | 63 | "Warm", "Cool", "Hot" |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 64 | }; |
| 65 | static char *technology_text[] = { |
Dmitry Baryshkov | c7cc930 | 2008-01-07 04:12:41 +0300 | [diff] [blame] | 66 | "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd", |
| 67 | "LiMn" |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 68 | }; |
Andres Salomon | b294a29 | 2009-06-30 02:13:01 -0400 | [diff] [blame] | 69 | static char *capacity_level_text[] = { |
| 70 | "Unknown", "Critical", "Low", "Normal", "High", "Full" |
| 71 | }; |
Jeremy Fitzhardinge | 25a0bc2 | 2011-12-07 11:24:20 -0800 | [diff] [blame] | 72 | static char *scope_text[] = { |
| 73 | "Unknown", "System", "Device" |
| 74 | }; |
Jack Pham | d8c9b78 | 2016-02-29 11:41:58 -0800 | [diff] [blame] | 75 | static const char * const typec_text[] = { |
| 76 | "Nothing attached", "Sink attached", "Powered cable w/ sink", |
| 77 | "Debug Accessory", "Audio Adapter", "Powered cable w/o sink", |
| 78 | "Source attached (default current)", |
| 79 | "Source attached (medium current)", |
| 80 | "Source attached (high current)", |
| 81 | "Non compliant", |
| 82 | }; |
| 83 | static const char * const typec_pr_text[] = { |
| 84 | "none", "dual power role", "sink", "source" |
| 85 | }; |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 86 | ssize_t ret = 0; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 87 | struct power_supply *psy = dev_get_drvdata(dev); |
| 88 | const ptrdiff_t off = attr - power_supply_attrs; |
| 89 | union power_supply_propval value; |
| 90 | |
Viresh Kumar | 73b4a08 | 2014-09-04 17:31:34 +0530 | [diff] [blame] | 91 | if (off == POWER_SUPPLY_PROP_TYPE) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 92 | value.intval = psy->desc->type; |
Viresh Kumar | 73b4a08 | 2014-09-04 17:31:34 +0530 | [diff] [blame] | 93 | } else { |
Krzysztof Kozlowski | ee8f334 | 2015-03-12 08:44:04 +0100 | [diff] [blame] | 94 | ret = power_supply_get_property(psy, off, &value); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 95 | |
Viresh Kumar | 73b4a08 | 2014-09-04 17:31:34 +0530 | [diff] [blame] | 96 | if (ret < 0) { |
| 97 | if (ret == -ENODATA) |
| 98 | dev_dbg(dev, "driver has no data for `%s' property\n", |
| 99 | attr->attr.name); |
Rhyland Klein | e380538 | 2016-06-22 11:45:52 -0400 | [diff] [blame] | 100 | else if (ret != -ENODEV && ret != -EAGAIN) |
Viresh Kumar | 73b4a08 | 2014-09-04 17:31:34 +0530 | [diff] [blame] | 101 | dev_err(dev, "driver failed to report `%s' property: %zd\n", |
| 102 | attr->attr.name, ret); |
| 103 | return ret; |
| 104 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (off == POWER_SUPPLY_PROP_STATUS) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 108 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 109 | status_text[value.intval]); |
Andres Salomon | ee8076e | 2009-07-02 09:45:18 -0400 | [diff] [blame] | 110 | else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 111 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 112 | charge_type[value.intval]); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 113 | else if (off == POWER_SUPPLY_PROP_HEALTH) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 114 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 115 | health_text[value.intval]); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 116 | else if (off == POWER_SUPPLY_PROP_TECHNOLOGY) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 117 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 118 | technology_text[value.intval]); |
Andres Salomon | b294a29 | 2009-06-30 02:13:01 -0400 | [diff] [blame] | 119 | else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 120 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 121 | capacity_level_text[value.intval]); |
Fenglin Wu | e8615ac | 2017-04-25 20:51:06 +0800 | [diff] [blame] | 122 | else if (off == POWER_SUPPLY_PROP_TYPE || |
| 123 | off == POWER_SUPPLY_PROP_REAL_TYPE) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 124 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 125 | type_text[value.intval]); |
Jeremy Fitzhardinge | 25a0bc2 | 2011-12-07 11:24:20 -0800 | [diff] [blame] | 126 | else if (off == POWER_SUPPLY_PROP_SCOPE) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 127 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 128 | scope_text[value.intval]); |
Jack Pham | d8c9b78 | 2016-02-29 11:41:58 -0800 | [diff] [blame] | 129 | else if (off == POWER_SUPPLY_PROP_TYPEC_MODE) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 130 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 131 | typec_text[value.intval]); |
Jack Pham | d8c9b78 | 2016-02-29 11:41:58 -0800 | [diff] [blame] | 132 | else if (off == POWER_SUPPLY_PROP_TYPEC_POWER_ROLE) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 133 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 134 | typec_pr_text[value.intval]); |
Nicholas Troast | 86cc4f67 | 2017-02-08 10:41:06 -0800 | [diff] [blame] | 135 | else if (off == POWER_SUPPLY_PROP_DIE_HEALTH) |
| 136 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 137 | health_text[value.intval]); |
| 138 | else if (off == POWER_SUPPLY_PROP_CONNECTOR_HEALTH) |
| 139 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 140 | health_text[value.intval]); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 141 | else if (off >= POWER_SUPPLY_PROP_MODEL_NAME) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 142 | return scnprintf(buf, PAGE_SIZE, "%s\n", |
| 143 | value.strval); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 144 | |
Todd Poynor | fd27692 | 2013-12-12 15:59:09 -0800 | [diff] [blame] | 145 | if (off == POWER_SUPPLY_PROP_CHARGE_COUNTER_EXT) |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 146 | return scnprintf(buf, PAGE_SIZE, "%lld\n", |
| 147 | value.int64val); |
Todd Poynor | fd27692 | 2013-12-12 15:59:09 -0800 | [diff] [blame] | 148 | else |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 149 | return scnprintf(buf, PAGE_SIZE, "%d\n", |
| 150 | value.intval); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 151 | } |
| 152 | |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 153 | static ssize_t power_supply_store_property(struct device *dev, |
| 154 | struct device_attribute *attr, |
| 155 | const char *buf, size_t count) { |
| 156 | ssize_t ret; |
| 157 | struct power_supply *psy = dev_get_drvdata(dev); |
| 158 | const ptrdiff_t off = attr - power_supply_attrs; |
| 159 | union power_supply_propval value; |
| 160 | long long_val; |
| 161 | |
| 162 | /* TODO: support other types than int */ |
Jingoo Han | f107ae1 | 2013-07-19 16:06:16 +0900 | [diff] [blame] | 163 | ret = kstrtol(buf, 10, &long_val); |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 164 | if (ret < 0) |
| 165 | return ret; |
| 166 | |
| 167 | value.intval = long_val; |
| 168 | |
Krzysztof Kozlowski | a9f6a19 | 2015-05-19 16:16:29 +0900 | [diff] [blame] | 169 | ret = power_supply_set_property(psy, off, &value); |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 170 | if (ret < 0) |
| 171 | return ret; |
| 172 | |
| 173 | return count; |
| 174 | } |
| 175 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 176 | /* Must be in the same order as POWER_SUPPLY_PROP_* */ |
| 177 | static struct device_attribute power_supply_attrs[] = { |
| 178 | /* Properties of type `int' */ |
| 179 | POWER_SUPPLY_ATTR(status), |
Andres Salomon | ee8076e | 2009-07-02 09:45:18 -0400 | [diff] [blame] | 180 | POWER_SUPPLY_ATTR(charge_type), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 181 | POWER_SUPPLY_ATTR(health), |
| 182 | POWER_SUPPLY_ATTR(present), |
| 183 | POWER_SUPPLY_ATTR(online), |
Ramakrishna Pallala | b1b5687 | 2012-08-23 06:50:21 +0530 | [diff] [blame] | 184 | POWER_SUPPLY_ATTR(authentic), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 185 | POWER_SUPPLY_ATTR(technology), |
Alexey Starikovskiy | c955fe8 | 2009-10-15 14:31:30 +0400 | [diff] [blame] | 186 | POWER_SUPPLY_ATTR(cycle_count), |
Dmitry Baryshkov | c7cc930 | 2008-01-07 04:12:41 +0300 | [diff] [blame] | 187 | POWER_SUPPLY_ATTR(voltage_max), |
| 188 | POWER_SUPPLY_ATTR(voltage_min), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 189 | POWER_SUPPLY_ATTR(voltage_max_design), |
| 190 | POWER_SUPPLY_ATTR(voltage_min_design), |
| 191 | POWER_SUPPLY_ATTR(voltage_now), |
| 192 | POWER_SUPPLY_ATTR(voltage_avg), |
Ramakrishna Pallala | a2ebfe2 | 2012-04-10 16:21:20 +0530 | [diff] [blame] | 193 | POWER_SUPPLY_ATTR(voltage_ocv), |
Ramakrishna Pallala | a8adcc90 | 2014-08-27 23:44:08 +0530 | [diff] [blame] | 194 | POWER_SUPPLY_ATTR(voltage_boot), |
Heikki Krogerus | fe3f6d0 | 2010-10-04 10:51:38 +0300 | [diff] [blame] | 195 | POWER_SUPPLY_ATTR(current_max), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 196 | POWER_SUPPLY_ATTR(current_now), |
| 197 | POWER_SUPPLY_ATTR(current_avg), |
Ramakrishna Pallala | a8adcc90 | 2014-08-27 23:44:08 +0530 | [diff] [blame] | 198 | POWER_SUPPLY_ATTR(current_boot), |
Alexey Starikovskiy | 7faa144 | 2009-03-27 22:23:52 -0400 | [diff] [blame] | 199 | POWER_SUPPLY_ATTR(power_now), |
| 200 | POWER_SUPPLY_ATTR(power_avg), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 201 | POWER_SUPPLY_ATTR(charge_full_design), |
| 202 | POWER_SUPPLY_ATTR(charge_empty_design), |
| 203 | POWER_SUPPLY_ATTR(charge_full), |
| 204 | POWER_SUPPLY_ATTR(charge_empty), |
| 205 | POWER_SUPPLY_ATTR(charge_now), |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 206 | POWER_SUPPLY_ATTR(charge_now_raw), |
| 207 | POWER_SUPPLY_ATTR(charge_now_error), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 208 | POWER_SUPPLY_ATTR(charge_avg), |
Andres Salomon | 8e552c3 | 2008-05-12 21:46:29 -0400 | [diff] [blame] | 209 | POWER_SUPPLY_ATTR(charge_counter), |
Ramakrishna Pallala | 3824c47 | 2012-05-06 18:16:44 +0530 | [diff] [blame] | 210 | POWER_SUPPLY_ATTR(constant_charge_current), |
Ramakrishna Pallala | 2815b78 | 2012-07-30 12:49:21 +0530 | [diff] [blame] | 211 | POWER_SUPPLY_ATTR(constant_charge_current_max), |
Ramakrishna Pallala | 3824c47 | 2012-05-06 18:16:44 +0530 | [diff] [blame] | 212 | POWER_SUPPLY_ATTR(constant_charge_voltage), |
Ramakrishna Pallala | 2815b78 | 2012-07-30 12:49:21 +0530 | [diff] [blame] | 213 | POWER_SUPPLY_ATTR(constant_charge_voltage_max), |
Ramakrishna Pallala | ea2ce92 | 2012-10-09 22:25:29 +0530 | [diff] [blame] | 214 | POWER_SUPPLY_ATTR(charge_control_limit), |
| 215 | POWER_SUPPLY_ATTR(charge_control_limit_max), |
Jenny TC | 6bb1d27 | 2014-07-08 11:34:18 +0530 | [diff] [blame] | 216 | POWER_SUPPLY_ATTR(input_current_limit), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 217 | POWER_SUPPLY_ATTR(energy_full_design), |
| 218 | POWER_SUPPLY_ATTR(energy_empty_design), |
| 219 | POWER_SUPPLY_ATTR(energy_full), |
| 220 | POWER_SUPPLY_ATTR(energy_empty), |
| 221 | POWER_SUPPLY_ATTR(energy_now), |
| 222 | POWER_SUPPLY_ATTR(energy_avg), |
| 223 | POWER_SUPPLY_ATTR(capacity), |
Ramakrishna Pallala | e908c41 | 2012-07-05 16:59:12 +0530 | [diff] [blame] | 224 | POWER_SUPPLY_ATTR(capacity_alert_min), |
| 225 | POWER_SUPPLY_ATTR(capacity_alert_max), |
Andres Salomon | b294a29 | 2009-06-30 02:13:01 -0400 | [diff] [blame] | 226 | POWER_SUPPLY_ATTR(capacity_level), |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 227 | POWER_SUPPLY_ATTR(capacity_raw), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 228 | POWER_SUPPLY_ATTR(temp), |
Jenny TC | 6bb1d27 | 2014-07-08 11:34:18 +0530 | [diff] [blame] | 229 | POWER_SUPPLY_ATTR(temp_max), |
| 230 | POWER_SUPPLY_ATTR(temp_min), |
Ramakrishna Pallala | e908c41 | 2012-07-05 16:59:12 +0530 | [diff] [blame] | 231 | POWER_SUPPLY_ATTR(temp_alert_min), |
| 232 | POWER_SUPPLY_ATTR(temp_alert_max), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 233 | POWER_SUPPLY_ATTR(temp_ambient), |
Ramakrishna Pallala | e908c41 | 2012-07-05 16:59:12 +0530 | [diff] [blame] | 234 | POWER_SUPPLY_ATTR(temp_ambient_alert_min), |
| 235 | POWER_SUPPLY_ATTR(temp_ambient_alert_max), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 236 | POWER_SUPPLY_ATTR(time_to_empty_now), |
| 237 | POWER_SUPPLY_ATTR(time_to_empty_avg), |
| 238 | POWER_SUPPLY_ATTR(time_to_full_now), |
| 239 | POWER_SUPPLY_ATTR(time_to_full_avg), |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 240 | POWER_SUPPLY_ATTR(type), |
Jeremy Fitzhardinge | 25a0bc2 | 2011-12-07 11:24:20 -0800 | [diff] [blame] | 241 | POWER_SUPPLY_ATTR(scope), |
Jenny TC | 6bb1d27 | 2014-07-08 11:34:18 +0530 | [diff] [blame] | 242 | POWER_SUPPLY_ATTR(charge_term_current), |
Ramakrishna Pallala | a8adcc90 | 2014-08-27 23:44:08 +0530 | [diff] [blame] | 243 | POWER_SUPPLY_ATTR(calibrate), |
Todd Poynor | 10433d5 | 2012-07-12 20:27:16 -0700 | [diff] [blame] | 244 | /* Local extensions */ |
| 245 | POWER_SUPPLY_ATTR(usb_hc), |
Todd Poynor | 46e6ae2 | 2012-07-13 13:30:04 -0700 | [diff] [blame] | 246 | POWER_SUPPLY_ATTR(usb_otg), |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 247 | POWER_SUPPLY_ATTR(battery_charging_enabled), |
| 248 | POWER_SUPPLY_ATTR(charging_enabled), |
Harry Yang | 878ba2b | 2016-08-31 16:07:30 -0700 | [diff] [blame] | 249 | POWER_SUPPLY_ATTR(step_charging_enabled), |
| 250 | POWER_SUPPLY_ATTR(step_charging_step), |
Nicholas Troast | cf50b0c | 2016-08-02 13:43:58 -0700 | [diff] [blame] | 251 | POWER_SUPPLY_ATTR(pin_enabled), |
Nicholas Troast | 2416311 | 2016-04-01 10:48:01 -0700 | [diff] [blame] | 252 | POWER_SUPPLY_ATTR(input_suspend), |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 253 | POWER_SUPPLY_ATTR(input_voltage_regulation), |
| 254 | POWER_SUPPLY_ATTR(input_current_max), |
| 255 | POWER_SUPPLY_ATTR(input_current_trim), |
| 256 | POWER_SUPPLY_ATTR(input_current_settled), |
Nicholas Troast | 5bb8e7f | 2017-01-28 09:32:17 -0800 | [diff] [blame] | 257 | POWER_SUPPLY_ATTR(input_voltage_settled), |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 258 | POWER_SUPPLY_ATTR(bypass_vchg_loop_debouncer), |
| 259 | POWER_SUPPLY_ATTR(charge_counter_shadow), |
| 260 | POWER_SUPPLY_ATTR(hi_power), |
| 261 | POWER_SUPPLY_ATTR(low_power), |
| 262 | POWER_SUPPLY_ATTR(temp_cool), |
| 263 | POWER_SUPPLY_ATTR(temp_warm), |
Subbaraman Narayanamurthy | 7b8a3ee | 2017-08-11 18:37:26 -0700 | [diff] [blame] | 264 | POWER_SUPPLY_ATTR(temp_cold), |
| 265 | POWER_SUPPLY_ATTR(temp_hot), |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 266 | POWER_SUPPLY_ATTR(system_temp_level), |
| 267 | POWER_SUPPLY_ATTR(resistance), |
| 268 | POWER_SUPPLY_ATTR(resistance_capacitive), |
| 269 | POWER_SUPPLY_ATTR(resistance_id), |
| 270 | POWER_SUPPLY_ATTR(resistance_now), |
| 271 | POWER_SUPPLY_ATTR(flash_current_max), |
| 272 | POWER_SUPPLY_ATTR(update_now), |
| 273 | POWER_SUPPLY_ATTR(esr_count), |
| 274 | POWER_SUPPLY_ATTR(buck_freq), |
| 275 | POWER_SUPPLY_ATTR(boost_current), |
| 276 | POWER_SUPPLY_ATTR(safety_timer_enabled), |
| 277 | POWER_SUPPLY_ATTR(charge_done), |
| 278 | POWER_SUPPLY_ATTR(flash_active), |
| 279 | POWER_SUPPLY_ATTR(flash_trigger), |
| 280 | POWER_SUPPLY_ATTR(force_tlim), |
| 281 | POWER_SUPPLY_ATTR(dp_dm), |
| 282 | POWER_SUPPLY_ATTR(input_current_limited), |
| 283 | POWER_SUPPLY_ATTR(input_current_now), |
Harry Yang | 71a8d3e | 2017-02-25 21:25:51 -0800 | [diff] [blame] | 284 | POWER_SUPPLY_ATTR(charge_qnovo_enable), |
Harry Yang | a22224c | 2017-02-07 16:53:28 -0800 | [diff] [blame] | 285 | POWER_SUPPLY_ATTR(current_qnovo), |
| 286 | POWER_SUPPLY_ATTR(voltage_qnovo), |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 287 | POWER_SUPPLY_ATTR(rerun_aicl), |
| 288 | POWER_SUPPLY_ATTR(cycle_count_id), |
| 289 | POWER_SUPPLY_ATTR(safety_timer_expired), |
| 290 | POWER_SUPPLY_ATTR(restricted_charging), |
| 291 | POWER_SUPPLY_ATTR(current_capability), |
Jack Pham | d8c9b78 | 2016-02-29 11:41:58 -0800 | [diff] [blame] | 292 | POWER_SUPPLY_ATTR(typec_mode), |
| 293 | POWER_SUPPLY_ATTR(typec_cc_orientation), |
| 294 | POWER_SUPPLY_ATTR(typec_power_role), |
| 295 | POWER_SUPPLY_ATTR(pd_allowed), |
| 296 | POWER_SUPPLY_ATTR(pd_active), |
Jack Pham | 2f3c825 | 2016-09-30 17:32:01 -0700 | [diff] [blame] | 297 | POWER_SUPPLY_ATTR(pd_in_hard_reset), |
| 298 | POWER_SUPPLY_ATTR(pd_current_max), |
| 299 | POWER_SUPPLY_ATTR(pd_usb_suspend_supported), |
Nicholas Troast | 0fa3185 | 2016-07-28 13:42:49 -0700 | [diff] [blame] | 300 | POWER_SUPPLY_ATTR(charger_temp), |
| 301 | POWER_SUPPLY_ATTR(charger_temp_max), |
Nicholas Troast | 549fcde | 2016-10-06 11:48:31 -0700 | [diff] [blame] | 302 | POWER_SUPPLY_ATTR(parallel_disable), |
Abhijeet Dharmapurikar | 6d3ad96 | 2016-10-17 16:51:36 -0700 | [diff] [blame] | 303 | POWER_SUPPLY_ATTR(pe_start), |
Fenglin Wu | 5375876 | 2016-11-22 12:50:25 +0800 | [diff] [blame] | 304 | POWER_SUPPLY_ATTR(set_ship_mode), |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 305 | POWER_SUPPLY_ATTR(soc_reporting_ready), |
| 306 | POWER_SUPPLY_ATTR(debug_battery), |
Nicholas Troast | 86cc4f67 | 2017-02-08 10:41:06 -0800 | [diff] [blame] | 307 | POWER_SUPPLY_ATTR(fcc_delta), |
| 308 | POWER_SUPPLY_ATTR(icl_reduction), |
| 309 | POWER_SUPPLY_ATTR(parallel_mode), |
Nicholas Troast | 86cc4f67 | 2017-02-08 10:41:06 -0800 | [diff] [blame] | 310 | POWER_SUPPLY_ATTR(die_health), |
| 311 | POWER_SUPPLY_ATTR(connector_health), |
Nicholas Troast | 6b85914 | 2017-02-05 19:44:33 -0800 | [diff] [blame] | 312 | POWER_SUPPLY_ATTR(ctm_current_max), |
Ashay Jaiswal | 104edd8 | 2017-03-23 10:34:18 +0530 | [diff] [blame] | 313 | POWER_SUPPLY_ATTR(hw_current_max), |
Fenglin Wu | e8615ac | 2017-04-25 20:51:06 +0800 | [diff] [blame] | 314 | POWER_SUPPLY_ATTR(real_type), |
Abhijeet Dharmapurikar | e43bacd | 2017-05-24 17:11:24 -0700 | [diff] [blame] | 315 | POWER_SUPPLY_ATTR(pr_swap), |
Nicholas Troast | 0d8716a | 2017-06-20 16:25:57 -0700 | [diff] [blame] | 316 | POWER_SUPPLY_ATTR(cc_step), |
| 317 | POWER_SUPPLY_ATTR(cc_step_sel), |
Ashay Jaiswal | 8ae23a5 | 2017-08-09 09:46:31 +0530 | [diff] [blame] | 318 | POWER_SUPPLY_ATTR(sw_jeita_enabled), |
Nicholas Troast | e552a1d | 2017-07-25 13:10:26 -0700 | [diff] [blame] | 319 | POWER_SUPPLY_ATTR(pd_voltage_max), |
| 320 | POWER_SUPPLY_ATTR(pd_voltage_min), |
| 321 | POWER_SUPPLY_ATTR(sdp_current_max), |
Ashay Jaiswal | eb72684 | 2017-11-14 10:41:43 +0530 | [diff] [blame] | 322 | POWER_SUPPLY_ATTR(connector_type), |
Ashay Jaiswal | 45142d9 | 2017-11-02 09:57:18 +0530 | [diff] [blame] | 323 | POWER_SUPPLY_ATTR(parallel_batfet_mode), |
Guru Das Srinagesh | 2eb6768 | 2018-01-23 10:45:48 -0800 | [diff] [blame] | 324 | POWER_SUPPLY_ATTR(parallel_fcc_max), |
Ashay Jaiswal | 45142d9 | 2017-11-02 09:57:18 +0530 | [diff] [blame] | 325 | POWER_SUPPLY_ATTR(min_icl), |
Tirupathi Reddy | 4ba4b50 | 2017-09-27 12:04:31 +0530 | [diff] [blame] | 326 | POWER_SUPPLY_ATTR(moisture_detected), |
Anirudh Ghayal | 232897e | 2018-02-09 15:22:57 +0530 | [diff] [blame^] | 327 | POWER_SUPPLY_ATTR(batt_full_current), |
| 328 | POWER_SUPPLY_ATTR(recharge_soc), |
Todd Poynor | fd27692 | 2013-12-12 15:59:09 -0800 | [diff] [blame] | 329 | /* Local extensions of type int64_t */ |
Ashay Jaiswal | 8b6a3a5 | 2017-01-05 19:54:08 +0530 | [diff] [blame] | 330 | POWER_SUPPLY_ATTR(charge_counter_ext), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 331 | /* Properties of type `const char *' */ |
| 332 | POWER_SUPPLY_ATTR(model_name), |
| 333 | POWER_SUPPLY_ATTR(manufacturer), |
maximilian attems | 7c2670b | 2008-01-22 18:46:50 +0100 | [diff] [blame] | 334 | POWER_SUPPLY_ATTR(serial_number), |
Abhijeet Dharmapurikar | dcdf2fc | 2017-03-23 00:53:46 -0700 | [diff] [blame] | 335 | POWER_SUPPLY_ATTR(battery_type), |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 336 | }; |
| 337 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 338 | static struct attribute * |
| 339 | __power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1]; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 340 | |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 341 | static umode_t power_supply_attr_is_visible(struct kobject *kobj, |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 342 | struct attribute *attr, |
| 343 | int attrno) |
| 344 | { |
| 345 | struct device *dev = container_of(kobj, struct device, kobj); |
| 346 | struct power_supply *psy = dev_get_drvdata(dev); |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 347 | umode_t mode = S_IRUSR | S_IRGRP | S_IROTH; |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 348 | int i; |
| 349 | |
Daniel Mack | bbabb15 | 2010-05-25 02:39:45 +0200 | [diff] [blame] | 350 | if (attrno == POWER_SUPPLY_PROP_TYPE) |
| 351 | return mode; |
| 352 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 353 | for (i = 0; i < psy->desc->num_properties; i++) { |
| 354 | int property = psy->desc->properties[i]; |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 355 | |
| 356 | if (property == attrno) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 357 | if (psy->desc->property_is_writeable && |
Krzysztof Kozlowski | 5c6e3a9 | 2015-06-08 10:09:48 +0900 | [diff] [blame] | 358 | psy->desc->property_is_writeable(psy, property) > 0) |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 359 | mode |= S_IWUSR; |
| 360 | |
| 361 | return mode; |
| 362 | } |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | return 0; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 366 | } |
| 367 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 368 | static struct attribute_group power_supply_attr_group = { |
| 369 | .attrs = __power_supply_attrs, |
| 370 | .is_visible = power_supply_attr_is_visible, |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 371 | }; |
| 372 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 373 | static const struct attribute_group *power_supply_attr_groups[] = { |
| 374 | &power_supply_attr_group, |
| 375 | NULL, |
| 376 | }; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 377 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 378 | void power_supply_init_attrs(struct device_type *dev_type) |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 379 | { |
| 380 | int i; |
| 381 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 382 | dev_type->groups = power_supply_attr_groups; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 383 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 384 | for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++) |
| 385 | __power_supply_attrs[i] = &power_supply_attrs[i].attr; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | static char *kstruprdup(const char *str, gfp_t gfp) |
| 389 | { |
| 390 | char *ret, *ustr; |
| 391 | |
| 392 | ustr = ret = kmalloc(strlen(str) + 1, gfp); |
| 393 | |
| 394 | if (!ret) |
| 395 | return NULL; |
| 396 | |
| 397 | while (*str) |
| 398 | *ustr++ = toupper(*str++); |
| 399 | |
| 400 | *ustr = 0; |
| 401 | |
| 402 | return ret; |
| 403 | } |
| 404 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 405 | int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env) |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 406 | { |
| 407 | struct power_supply *psy = dev_get_drvdata(dev); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 408 | int ret = 0, j; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 409 | char *prop_buf; |
| 410 | char *attrname; |
| 411 | |
| 412 | dev_dbg(dev, "uevent\n"); |
| 413 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 414 | if (!psy || !psy->desc) { |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 415 | dev_dbg(dev, "No power supply yet\n"); |
| 416 | return ret; |
| 417 | } |
| 418 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 419 | dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->desc->name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 420 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 421 | ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 422 | if (ret) |
| 423 | return ret; |
| 424 | |
| 425 | prop_buf = (char *)get_zeroed_page(GFP_KERNEL); |
| 426 | if (!prop_buf) |
| 427 | return -ENOMEM; |
| 428 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 429 | for (j = 0; j < psy->desc->num_properties; j++) { |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 430 | struct device_attribute *attr; |
| 431 | char *line; |
| 432 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 433 | attr = &power_supply_attrs[psy->desc->properties[j]]; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 434 | |
| 435 | ret = power_supply_show_property(dev, attr, prop_buf); |
Lars-Peter Clausen | f722e17 | 2011-01-08 19:12:26 +0100 | [diff] [blame] | 436 | if (ret == -ENODEV || ret == -ENODATA) { |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 437 | /* When a battery is absent, we expect -ENODEV. Don't abort; |
| 438 | send the uevent with at least the the PRESENT=0 property */ |
| 439 | ret = 0; |
| 440 | continue; |
| 441 | } |
| 442 | |
| 443 | if (ret < 0) |
| 444 | goto out; |
| 445 | |
| 446 | line = strchr(prop_buf, '\n'); |
| 447 | if (line) |
| 448 | *line = 0; |
| 449 | |
| 450 | attrname = kstruprdup(attr->attr.name, GFP_KERNEL); |
| 451 | if (!attrname) { |
| 452 | ret = -ENOMEM; |
| 453 | goto out; |
| 454 | } |
| 455 | |
| 456 | dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf); |
| 457 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 458 | ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 459 | kfree(attrname); |
| 460 | if (ret) |
| 461 | goto out; |
| 462 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 463 | |
| 464 | out: |
| 465 | free_page((unsigned long)prop_buf); |
| 466 | |
| 467 | return ret; |
| 468 | } |