blob: b929d8bf30fdf30e089af50934631937dbe8b0b8 [file] [log] [blame]
Anton Vorontsov4a11b592007-05-04 00:27:45 +04001/*
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 Gortmaker51990e82012-01-22 11:23:42 -050015#include <linux/device.h>
Anton Vorontsov4a11b592007-05-04 00:27:45 +040016#include <linux/power_supply.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Paul Gortmaker778f5232011-05-27 10:22:46 -040018#include <linux/stat.h>
Anton Vorontsov4a11b592007-05-04 00:27:45 +040019
Adrian Bunk25f121412007-11-26 00:25:45 +030020#include "power_supply.h"
21
Anton Vorontsov4a11b592007-05-04 00:27:45 +040022/*
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 Vorontsov5f487cd2010-05-18 21:49:51 +020036 .attr = { .name = #_name }, \
Anton Vorontsov4a11b592007-05-04 00:27:45 +040037 .show = power_supply_show_property, \
Daniel Mack0011d2d2010-05-18 21:49:52 +020038 .store = power_supply_store_property, \
Anton Vorontsov4a11b592007-05-04 00:27:45 +040039}
40
41static struct device_attribute power_supply_attrs[];
42
43static ssize_t power_supply_show_property(struct device *dev,
44 struct device_attribute *attr,
45 char *buf) {
Anton Vorontsov5f487cd2010-05-18 21:49:51 +020046 static char *type_text[] = {
Abhijeet Dharmapurikar9a3a6c32017-01-06 18:27:29 -080047 "Unknown", "Battery", "UPS", "Mains", "USB", "USB_DCP",
48 "USB_CDP", "USB_ACA", "USB_HVDCP", "USB_HVDCP_3", "USB_PD",
Abhijeet Dharmapurikar4e56cab2017-07-05 14:00:52 -070049 "Wireless", "USB_FLOAT", "BMS", "Parallel", "Main", "Wipower",
Abhijeet Dharmapurikar9a3a6c32017-01-06 18:27:29 -080050 "TYPEC", "TYPEC_UFP", "TYPEC_DFP"
Anton Vorontsov5f487cd2010-05-18 21:49:51 +020051 };
Anton Vorontsov4a11b592007-05-04 00:27:45 +040052 static char *status_text[] = {
53 "Unknown", "Charging", "Discharging", "Not charging", "Full"
54 };
Andres Salomonee8076e2009-07-02 09:45:18 -040055 static char *charge_type[] = {
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -070056 "Unknown", "N/A", "Trickle", "Fast",
57 "Taper"
Andres Salomonee8076e2009-07-02 09:45:18 -040058 };
Anton Vorontsov4a11b592007-05-04 00:27:45 +040059 static char *health_text[] = {
60 "Unknown", "Good", "Overheat", "Dead", "Over voltage",
Ramakrishna Pallalaa05be992012-11-30 13:57:46 +053061 "Unspecified failure", "Cold", "Watchdog timer expire",
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -070062 "Safety timer expire",
Nicholas Troast86cc4f672017-02-08 10:41:06 -080063 "Warm", "Cool", "Hot"
Anton Vorontsov4a11b592007-05-04 00:27:45 +040064 };
65 static char *technology_text[] = {
Dmitry Baryshkovc7cc9302008-01-07 04:12:41 +030066 "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
67 "LiMn"
Anton Vorontsov4a11b592007-05-04 00:27:45 +040068 };
Andres Salomonb294a292009-06-30 02:13:01 -040069 static char *capacity_level_text[] = {
70 "Unknown", "Critical", "Low", "Normal", "High", "Full"
71 };
Jeremy Fitzhardinge25a0bc22011-12-07 11:24:20 -080072 static char *scope_text[] = {
73 "Unknown", "System", "Device"
74 };
Jack Phamd8c9b782016-02-29 11:41:58 -080075 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 Vorontsov5f487cd2010-05-18 21:49:51 +020086 ssize_t ret = 0;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040087 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 Kumar73b4a082014-09-04 17:31:34 +053091 if (off == POWER_SUPPLY_PROP_TYPE) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010092 value.intval = psy->desc->type;
Viresh Kumar73b4a082014-09-04 17:31:34 +053093 } else {
Krzysztof Kozlowskiee8f3342015-03-12 08:44:04 +010094 ret = power_supply_get_property(psy, off, &value);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040095
Viresh Kumar73b4a082014-09-04 17:31:34 +053096 if (ret < 0) {
97 if (ret == -ENODATA)
98 dev_dbg(dev, "driver has no data for `%s' property\n",
99 attr->attr.name);
Rhyland Kleine3805382016-06-22 11:45:52 -0400100 else if (ret != -ENODEV && ret != -EAGAIN)
Viresh Kumar73b4a082014-09-04 17:31:34 +0530101 dev_err(dev, "driver failed to report `%s' property: %zd\n",
102 attr->attr.name, ret);
103 return ret;
104 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400105 }
106
107 if (off == POWER_SUPPLY_PROP_STATUS)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700108 return scnprintf(buf, PAGE_SIZE, "%s\n",
109 status_text[value.intval]);
Andres Salomonee8076e2009-07-02 09:45:18 -0400110 else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700111 return scnprintf(buf, PAGE_SIZE, "%s\n",
112 charge_type[value.intval]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400113 else if (off == POWER_SUPPLY_PROP_HEALTH)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700114 return scnprintf(buf, PAGE_SIZE, "%s\n",
115 health_text[value.intval]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400116 else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700117 return scnprintf(buf, PAGE_SIZE, "%s\n",
118 technology_text[value.intval]);
Andres Salomonb294a292009-06-30 02:13:01 -0400119 else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700120 return scnprintf(buf, PAGE_SIZE, "%s\n",
121 capacity_level_text[value.intval]);
Fenglin Wue8615ac2017-04-25 20:51:06 +0800122 else if (off == POWER_SUPPLY_PROP_TYPE ||
123 off == POWER_SUPPLY_PROP_REAL_TYPE)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700124 return scnprintf(buf, PAGE_SIZE, "%s\n",
125 type_text[value.intval]);
Jeremy Fitzhardinge25a0bc22011-12-07 11:24:20 -0800126 else if (off == POWER_SUPPLY_PROP_SCOPE)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700127 return scnprintf(buf, PAGE_SIZE, "%s\n",
128 scope_text[value.intval]);
Jack Phamd8c9b782016-02-29 11:41:58 -0800129 else if (off == POWER_SUPPLY_PROP_TYPEC_MODE)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700130 return scnprintf(buf, PAGE_SIZE, "%s\n",
131 typec_text[value.intval]);
Jack Phamd8c9b782016-02-29 11:41:58 -0800132 else if (off == POWER_SUPPLY_PROP_TYPEC_POWER_ROLE)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700133 return scnprintf(buf, PAGE_SIZE, "%s\n",
134 typec_pr_text[value.intval]);
Nicholas Troast86cc4f672017-02-08 10:41:06 -0800135 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 Vorontsov4a11b592007-05-04 00:27:45 +0400141 else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700142 return scnprintf(buf, PAGE_SIZE, "%s\n",
143 value.strval);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400144
Todd Poynorfd276922013-12-12 15:59:09 -0800145 if (off == POWER_SUPPLY_PROP_CHARGE_COUNTER_EXT)
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700146 return scnprintf(buf, PAGE_SIZE, "%lld\n",
147 value.int64val);
Todd Poynorfd276922013-12-12 15:59:09 -0800148 else
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700149 return scnprintf(buf, PAGE_SIZE, "%d\n",
150 value.intval);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400151}
152
Daniel Mack0011d2d2010-05-18 21:49:52 +0200153static 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 Hanf107ae12013-07-19 16:06:16 +0900163 ret = kstrtol(buf, 10, &long_val);
Daniel Mack0011d2d2010-05-18 21:49:52 +0200164 if (ret < 0)
165 return ret;
166
167 value.intval = long_val;
168
Krzysztof Kozlowskia9f6a192015-05-19 16:16:29 +0900169 ret = power_supply_set_property(psy, off, &value);
Daniel Mack0011d2d2010-05-18 21:49:52 +0200170 if (ret < 0)
171 return ret;
172
173 return count;
174}
175
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400176/* Must be in the same order as POWER_SUPPLY_PROP_* */
177static struct device_attribute power_supply_attrs[] = {
178 /* Properties of type `int' */
179 POWER_SUPPLY_ATTR(status),
Andres Salomonee8076e2009-07-02 09:45:18 -0400180 POWER_SUPPLY_ATTR(charge_type),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400181 POWER_SUPPLY_ATTR(health),
182 POWER_SUPPLY_ATTR(present),
183 POWER_SUPPLY_ATTR(online),
Ramakrishna Pallalab1b56872012-08-23 06:50:21 +0530184 POWER_SUPPLY_ATTR(authentic),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400185 POWER_SUPPLY_ATTR(technology),
Alexey Starikovskiyc955fe82009-10-15 14:31:30 +0400186 POWER_SUPPLY_ATTR(cycle_count),
Dmitry Baryshkovc7cc9302008-01-07 04:12:41 +0300187 POWER_SUPPLY_ATTR(voltage_max),
188 POWER_SUPPLY_ATTR(voltage_min),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400189 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 Pallalaa2ebfe22012-04-10 16:21:20 +0530193 POWER_SUPPLY_ATTR(voltage_ocv),
Ramakrishna Pallalaa8adcc902014-08-27 23:44:08 +0530194 POWER_SUPPLY_ATTR(voltage_boot),
Heikki Krogerusfe3f6d02010-10-04 10:51:38 +0300195 POWER_SUPPLY_ATTR(current_max),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400196 POWER_SUPPLY_ATTR(current_now),
197 POWER_SUPPLY_ATTR(current_avg),
Ramakrishna Pallalaa8adcc902014-08-27 23:44:08 +0530198 POWER_SUPPLY_ATTR(current_boot),
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400199 POWER_SUPPLY_ATTR(power_now),
200 POWER_SUPPLY_ATTR(power_avg),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400201 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 Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700206 POWER_SUPPLY_ATTR(charge_now_raw),
207 POWER_SUPPLY_ATTR(charge_now_error),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400208 POWER_SUPPLY_ATTR(charge_avg),
Andres Salomon8e552c32008-05-12 21:46:29 -0400209 POWER_SUPPLY_ATTR(charge_counter),
Ramakrishna Pallala3824c472012-05-06 18:16:44 +0530210 POWER_SUPPLY_ATTR(constant_charge_current),
Ramakrishna Pallala2815b782012-07-30 12:49:21 +0530211 POWER_SUPPLY_ATTR(constant_charge_current_max),
Ramakrishna Pallala3824c472012-05-06 18:16:44 +0530212 POWER_SUPPLY_ATTR(constant_charge_voltage),
Ramakrishna Pallala2815b782012-07-30 12:49:21 +0530213 POWER_SUPPLY_ATTR(constant_charge_voltage_max),
Ramakrishna Pallalaea2ce922012-10-09 22:25:29 +0530214 POWER_SUPPLY_ATTR(charge_control_limit),
215 POWER_SUPPLY_ATTR(charge_control_limit_max),
Jenny TC6bb1d272014-07-08 11:34:18 +0530216 POWER_SUPPLY_ATTR(input_current_limit),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400217 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 Pallalae908c412012-07-05 16:59:12 +0530224 POWER_SUPPLY_ATTR(capacity_alert_min),
225 POWER_SUPPLY_ATTR(capacity_alert_max),
Andres Salomonb294a292009-06-30 02:13:01 -0400226 POWER_SUPPLY_ATTR(capacity_level),
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700227 POWER_SUPPLY_ATTR(capacity_raw),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400228 POWER_SUPPLY_ATTR(temp),
Jenny TC6bb1d272014-07-08 11:34:18 +0530229 POWER_SUPPLY_ATTR(temp_max),
230 POWER_SUPPLY_ATTR(temp_min),
Ramakrishna Pallalae908c412012-07-05 16:59:12 +0530231 POWER_SUPPLY_ATTR(temp_alert_min),
232 POWER_SUPPLY_ATTR(temp_alert_max),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400233 POWER_SUPPLY_ATTR(temp_ambient),
Ramakrishna Pallalae908c412012-07-05 16:59:12 +0530234 POWER_SUPPLY_ATTR(temp_ambient_alert_min),
235 POWER_SUPPLY_ATTR(temp_ambient_alert_max),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400236 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 Vorontsov5f487cd2010-05-18 21:49:51 +0200240 POWER_SUPPLY_ATTR(type),
Jeremy Fitzhardinge25a0bc22011-12-07 11:24:20 -0800241 POWER_SUPPLY_ATTR(scope),
Jenny TC6bb1d272014-07-08 11:34:18 +0530242 POWER_SUPPLY_ATTR(charge_term_current),
Ramakrishna Pallalaa8adcc902014-08-27 23:44:08 +0530243 POWER_SUPPLY_ATTR(calibrate),
Todd Poynor10433d52012-07-12 20:27:16 -0700244 /* Local extensions */
245 POWER_SUPPLY_ATTR(usb_hc),
Todd Poynor46e6ae22012-07-13 13:30:04 -0700246 POWER_SUPPLY_ATTR(usb_otg),
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700247 POWER_SUPPLY_ATTR(battery_charging_enabled),
248 POWER_SUPPLY_ATTR(charging_enabled),
Harry Yang878ba2b2016-08-31 16:07:30 -0700249 POWER_SUPPLY_ATTR(step_charging_enabled),
250 POWER_SUPPLY_ATTR(step_charging_step),
Nicholas Troastcf50b0c2016-08-02 13:43:58 -0700251 POWER_SUPPLY_ATTR(pin_enabled),
Nicholas Troast24163112016-04-01 10:48:01 -0700252 POWER_SUPPLY_ATTR(input_suspend),
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700253 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 Troast5bb8e7f2017-01-28 09:32:17 -0800257 POWER_SUPPLY_ATTR(input_voltage_settled),
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700258 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 Narayanamurthy7b8a3ee2017-08-11 18:37:26 -0700264 POWER_SUPPLY_ATTR(temp_cold),
265 POWER_SUPPLY_ATTR(temp_hot),
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700266 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 Yang71a8d3e2017-02-25 21:25:51 -0800284 POWER_SUPPLY_ATTR(charge_qnovo_enable),
Harry Yanga22224c2017-02-07 16:53:28 -0800285 POWER_SUPPLY_ATTR(current_qnovo),
286 POWER_SUPPLY_ATTR(voltage_qnovo),
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700287 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 Phamd8c9b782016-02-29 11:41:58 -0800292 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 Pham2f3c8252016-09-30 17:32:01 -0700297 POWER_SUPPLY_ATTR(pd_in_hard_reset),
298 POWER_SUPPLY_ATTR(pd_current_max),
299 POWER_SUPPLY_ATTR(pd_usb_suspend_supported),
Nicholas Troast0fa31852016-07-28 13:42:49 -0700300 POWER_SUPPLY_ATTR(charger_temp),
301 POWER_SUPPLY_ATTR(charger_temp_max),
Nicholas Troast549fcde2016-10-06 11:48:31 -0700302 POWER_SUPPLY_ATTR(parallel_disable),
Abhijeet Dharmapurikar6d3ad962016-10-17 16:51:36 -0700303 POWER_SUPPLY_ATTR(pe_start),
Fenglin Wu53758762016-11-22 12:50:25 +0800304 POWER_SUPPLY_ATTR(set_ship_mode),
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700305 POWER_SUPPLY_ATTR(soc_reporting_ready),
306 POWER_SUPPLY_ATTR(debug_battery),
Nicholas Troast86cc4f672017-02-08 10:41:06 -0800307 POWER_SUPPLY_ATTR(fcc_delta),
308 POWER_SUPPLY_ATTR(icl_reduction),
309 POWER_SUPPLY_ATTR(parallel_mode),
Nicholas Troast86cc4f672017-02-08 10:41:06 -0800310 POWER_SUPPLY_ATTR(die_health),
311 POWER_SUPPLY_ATTR(connector_health),
Nicholas Troast6b859142017-02-05 19:44:33 -0800312 POWER_SUPPLY_ATTR(ctm_current_max),
Ashay Jaiswal104edd82017-03-23 10:34:18 +0530313 POWER_SUPPLY_ATTR(hw_current_max),
Fenglin Wue8615ac2017-04-25 20:51:06 +0800314 POWER_SUPPLY_ATTR(real_type),
Abhijeet Dharmapurikare43bacd2017-05-24 17:11:24 -0700315 POWER_SUPPLY_ATTR(pr_swap),
Nicholas Troast0d8716a2017-06-20 16:25:57 -0700316 POWER_SUPPLY_ATTR(cc_step),
317 POWER_SUPPLY_ATTR(cc_step_sel),
Ashay Jaiswal8ae23a52017-08-09 09:46:31 +0530318 POWER_SUPPLY_ATTR(sw_jeita_enabled),
Nicholas Troaste552a1d2017-07-25 13:10:26 -0700319 POWER_SUPPLY_ATTR(pd_voltage_max),
320 POWER_SUPPLY_ATTR(pd_voltage_min),
321 POWER_SUPPLY_ATTR(sdp_current_max),
Todd Poynorfd276922013-12-12 15:59:09 -0800322 /* Local extensions of type int64_t */
Ashay Jaiswal8b6a3a52017-01-05 19:54:08 +0530323 POWER_SUPPLY_ATTR(charge_counter_ext),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400324 /* Properties of type `const char *' */
325 POWER_SUPPLY_ATTR(model_name),
326 POWER_SUPPLY_ATTR(manufacturer),
maximilian attems7c2670b2008-01-22 18:46:50 +0100327 POWER_SUPPLY_ATTR(serial_number),
Abhijeet Dharmapurikardcdf2fc2017-03-23 00:53:46 -0700328 POWER_SUPPLY_ATTR(battery_type),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400329};
330
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200331static struct attribute *
332__power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400333
Al Viro587a1f12011-07-23 23:11:19 -0400334static umode_t power_supply_attr_is_visible(struct kobject *kobj,
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200335 struct attribute *attr,
336 int attrno)
337{
338 struct device *dev = container_of(kobj, struct device, kobj);
339 struct power_supply *psy = dev_get_drvdata(dev);
Al Viro587a1f12011-07-23 23:11:19 -0400340 umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200341 int i;
342
Daniel Mackbbabb152010-05-25 02:39:45 +0200343 if (attrno == POWER_SUPPLY_PROP_TYPE)
344 return mode;
345
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100346 for (i = 0; i < psy->desc->num_properties; i++) {
347 int property = psy->desc->properties[i];
Daniel Mack0011d2d2010-05-18 21:49:52 +0200348
349 if (property == attrno) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100350 if (psy->desc->property_is_writeable &&
Krzysztof Kozlowski5c6e3a92015-06-08 10:09:48 +0900351 psy->desc->property_is_writeable(psy, property) > 0)
Daniel Mack0011d2d2010-05-18 21:49:52 +0200352 mode |= S_IWUSR;
353
354 return mode;
355 }
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200356 }
357
358 return 0;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400359}
360
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200361static struct attribute_group power_supply_attr_group = {
362 .attrs = __power_supply_attrs,
363 .is_visible = power_supply_attr_is_visible,
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400364};
365
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200366static const struct attribute_group *power_supply_attr_groups[] = {
367 &power_supply_attr_group,
368 NULL,
369};
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400370
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200371void power_supply_init_attrs(struct device_type *dev_type)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400372{
373 int i;
374
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200375 dev_type->groups = power_supply_attr_groups;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400376
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200377 for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++)
378 __power_supply_attrs[i] = &power_supply_attrs[i].attr;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400379}
380
381static char *kstruprdup(const char *str, gfp_t gfp)
382{
383 char *ret, *ustr;
384
385 ustr = ret = kmalloc(strlen(str) + 1, gfp);
386
387 if (!ret)
388 return NULL;
389
390 while (*str)
391 *ustr++ = toupper(*str++);
392
393 *ustr = 0;
394
395 return ret;
396}
397
Kay Sievers7eff2e72007-08-14 15:15:12 +0200398int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400399{
400 struct power_supply *psy = dev_get_drvdata(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200401 int ret = 0, j;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400402 char *prop_buf;
403 char *attrname;
404
405 dev_dbg(dev, "uevent\n");
406
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100407 if (!psy || !psy->desc) {
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400408 dev_dbg(dev, "No power supply yet\n");
409 return ret;
410 }
411
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100412 dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->desc->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400413
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100414 ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400415 if (ret)
416 return ret;
417
418 prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
419 if (!prop_buf)
420 return -ENOMEM;
421
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100422 for (j = 0; j < psy->desc->num_properties; j++) {
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400423 struct device_attribute *attr;
424 char *line;
425
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100426 attr = &power_supply_attrs[psy->desc->properties[j]];
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400427
428 ret = power_supply_show_property(dev, attr, prop_buf);
Lars-Peter Clausenf722e172011-01-08 19:12:26 +0100429 if (ret == -ENODEV || ret == -ENODATA) {
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400430 /* When a battery is absent, we expect -ENODEV. Don't abort;
431 send the uevent with at least the the PRESENT=0 property */
432 ret = 0;
433 continue;
434 }
435
436 if (ret < 0)
437 goto out;
438
439 line = strchr(prop_buf, '\n');
440 if (line)
441 *line = 0;
442
443 attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
444 if (!attrname) {
445 ret = -ENOMEM;
446 goto out;
447 }
448
449 dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
450
Kay Sievers7eff2e72007-08-14 15:15:12 +0200451 ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400452 kfree(attrname);
453 if (ret)
454 goto out;
455 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400456
457out:
458 free_page((unsigned long)prop_buf);
459
460 return ret;
461}