• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Universal power supply monitor class
3  *
4  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
5  *  Copyright © 2004  Szabolcs Gyurko
6  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
7  *
8  *  Modified: 2004, Oct     Szabolcs Gyurko
9  *
10  *  You may use this code as per GPL version 2
11  */
12 
13 #ifndef __LINUX_POWER_SUPPLY_H__
14 #define __LINUX_POWER_SUPPLY_H__
15 
16 #include <linux/device.h>
17 #include <linux/workqueue.h>
18 #include <linux/leds.h>
19 #include <linux/spinlock.h>
20 #include <linux/notifier.h>
21 #include <linux/types.h>
22 
23 /*
24  * All voltages, currents, charges, energies, time and temperatures in uV,
25  * µA, µAh, µWh, seconds and tenths of degree Celsius unless otherwise
26  * stated. It's driver's job to convert its raw values to units in which
27  * this class operates.
28  */
29 
30 /*
31  * For systems where the charger determines the maximum battery capacity
32  * the min and max fields should be used to present these values to user
33  * space. Unused/unknown fields will not appear in sysfs.
34  */
35 
36 enum {
37 	POWER_SUPPLY_STATUS_UNKNOWN = 0,
38 	POWER_SUPPLY_STATUS_CHARGING,
39 	POWER_SUPPLY_STATUS_DISCHARGING,
40 	POWER_SUPPLY_STATUS_NOT_CHARGING,
41 	POWER_SUPPLY_STATUS_FULL,
42 };
43 
44 enum {
45 	POWER_SUPPLY_CHARGE_TYPE_UNKNOWN = 0,
46 	POWER_SUPPLY_CHARGE_TYPE_NONE,
47 	POWER_SUPPLY_CHARGE_TYPE_TRICKLE,
48 	POWER_SUPPLY_CHARGE_TYPE_FAST,
49 };
50 
51 enum {
52 	POWER_SUPPLY_HEALTH_UNKNOWN = 0,
53 	POWER_SUPPLY_HEALTH_GOOD,
54 	POWER_SUPPLY_HEALTH_OVERHEAT,
55 	POWER_SUPPLY_HEALTH_DEAD,
56 	POWER_SUPPLY_HEALTH_OVERVOLTAGE,
57 	POWER_SUPPLY_HEALTH_UNSPEC_FAILURE,
58 	POWER_SUPPLY_HEALTH_COLD,
59 	POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE,
60 	POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE,
61 };
62 
63 enum {
64 	POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
65 	POWER_SUPPLY_TECHNOLOGY_NiMH,
66 	POWER_SUPPLY_TECHNOLOGY_LION,
67 	POWER_SUPPLY_TECHNOLOGY_LIPO,
68 	POWER_SUPPLY_TECHNOLOGY_LiFe,
69 	POWER_SUPPLY_TECHNOLOGY_NiCd,
70 	POWER_SUPPLY_TECHNOLOGY_LiMn,
71 };
72 
73 enum {
74 	POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
75 	POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
76 	POWER_SUPPLY_CAPACITY_LEVEL_LOW,
77 	POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
78 	POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
79 	POWER_SUPPLY_CAPACITY_LEVEL_FULL,
80 };
81 
82 enum {
83 	POWER_SUPPLY_SCOPE_UNKNOWN = 0,
84 	POWER_SUPPLY_SCOPE_SYSTEM,
85 	POWER_SUPPLY_SCOPE_DEVICE,
86 };
87 
88 enum power_supply_property {
89 	/* Properties of type `int' */
90 	POWER_SUPPLY_PROP_STATUS = 0,
91 	POWER_SUPPLY_PROP_CHARGE_TYPE,
92 	POWER_SUPPLY_PROP_HEALTH,
93 	POWER_SUPPLY_PROP_PRESENT,
94 	POWER_SUPPLY_PROP_ONLINE,
95 	POWER_SUPPLY_PROP_AUTHENTIC,
96 	POWER_SUPPLY_PROP_TECHNOLOGY,
97 	POWER_SUPPLY_PROP_CYCLE_COUNT,
98 	POWER_SUPPLY_PROP_VOLTAGE_MAX,
99 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
100 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
101 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
102 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
103 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
104 	POWER_SUPPLY_PROP_VOLTAGE_OCV,
105 	POWER_SUPPLY_PROP_VOLTAGE_BOOT,
106 	POWER_SUPPLY_PROP_CURRENT_MAX,
107 	POWER_SUPPLY_PROP_CURRENT_NOW,
108 	POWER_SUPPLY_PROP_CURRENT_AVG,
109 	POWER_SUPPLY_PROP_CURRENT_BOOT,
110 	POWER_SUPPLY_PROP_POWER_NOW,
111 	POWER_SUPPLY_PROP_POWER_AVG,
112 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
113 	POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
114 	POWER_SUPPLY_PROP_CHARGE_FULL,
115 	POWER_SUPPLY_PROP_CHARGE_EMPTY,
116 	POWER_SUPPLY_PROP_CHARGE_NOW,
117 	POWER_SUPPLY_PROP_CHARGE_AVG,
118 	POWER_SUPPLY_PROP_CHARGE_COUNTER,
119 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
120 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
121 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
122 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
123 	POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
124 	POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
125 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
126 	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
127 	POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
128 	POWER_SUPPLY_PROP_ENERGY_FULL,
129 	POWER_SUPPLY_PROP_ENERGY_EMPTY,
130 	POWER_SUPPLY_PROP_ENERGY_NOW,
131 	POWER_SUPPLY_PROP_ENERGY_AVG,
132 	POWER_SUPPLY_PROP_CAPACITY, /* in percents! */
133 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, /* in percents! */
134 	POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, /* in percents! */
135 	POWER_SUPPLY_PROP_CAPACITY_LEVEL,
136 	POWER_SUPPLY_PROP_TEMP,
137 	POWER_SUPPLY_PROP_TEMP_MAX,
138 	POWER_SUPPLY_PROP_TEMP_MIN,
139 	POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
140 	POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
141 	POWER_SUPPLY_PROP_TEMP_AMBIENT,
142 	POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
143 	POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
144 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
145 	POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
146 	POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
147 	POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
148 	POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
149 	POWER_SUPPLY_PROP_SCOPE,
150 	POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
151 	POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
152 	POWER_SUPPLY_PROP_CALIBRATE,
153 	/* Local extensions */
154 	POWER_SUPPLY_PROP_USB_HC,
155 	POWER_SUPPLY_PROP_USB_OTG,
156 	POWER_SUPPLY_PROP_CHARGE_ENABLED,
157 	/* Local extensions of type int64_t */
158 	POWER_SUPPLY_PROP_CHARGE_COUNTER_EXT,
159 	/* Properties of type `const char *' */
160 	POWER_SUPPLY_PROP_MODEL_NAME,
161 	POWER_SUPPLY_PROP_MANUFACTURER,
162 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
163 };
164 
165 enum power_supply_type {
166 	POWER_SUPPLY_TYPE_UNKNOWN = 0,
167 	POWER_SUPPLY_TYPE_BATTERY,
168 	POWER_SUPPLY_TYPE_UPS,
169 	POWER_SUPPLY_TYPE_MAINS,
170 	POWER_SUPPLY_TYPE_USB,			/* Standard Downstream Port */
171 	POWER_SUPPLY_TYPE_USB_DCP,		/* Dedicated Charging Port */
172 	POWER_SUPPLY_TYPE_USB_CDP,		/* Charging Downstream Port */
173 	POWER_SUPPLY_TYPE_USB_ACA,		/* Accessory Charger Adapters */
174 	POWER_SUPPLY_TYPE_USB_TYPE_C,		/* Type C Port */
175 	POWER_SUPPLY_TYPE_USB_PD,		/* Power Delivery Port */
176 	POWER_SUPPLY_TYPE_USB_PD_DRP,		/* PD Dual Role Port */
177 	POWER_SUPPLY_TYPE_APPLE_BRICK_ID,	/* Apple Charging Method */
178 };
179 
180 enum power_supply_notifier_events {
181 	PSY_EVENT_PROP_CHANGED,
182 };
183 
184 union power_supply_propval {
185 	int intval;
186 	const char *strval;
187 	int64_t int64val;
188 };
189 
190 struct device_node;
191 struct power_supply;
192 
193 /* Run-time specific power supply configuration */
194 struct power_supply_config {
195 	struct device_node *of_node;
196 	/* Driver private data */
197 	void *drv_data;
198 
199 	char **supplied_to;
200 	size_t num_supplicants;
201 };
202 
203 /* Description of power supply */
204 struct power_supply_desc {
205 	const char *name;
206 	enum power_supply_type type;
207 	enum power_supply_property *properties;
208 	size_t num_properties;
209 
210 	/*
211 	 * Functions for drivers implementing power supply class.
212 	 * These shouldn't be called directly by other drivers for accessing
213 	 * this power supply. Instead use power_supply_*() functions (for
214 	 * example power_supply_get_property()).
215 	 */
216 	int (*get_property)(struct power_supply *psy,
217 			    enum power_supply_property psp,
218 			    union power_supply_propval *val);
219 	int (*set_property)(struct power_supply *psy,
220 			    enum power_supply_property psp,
221 			    const union power_supply_propval *val);
222 	/*
223 	 * property_is_writeable() will be called during registration
224 	 * of power supply. If this happens during device probe then it must
225 	 * not access internal data of device (because probe did not end).
226 	 */
227 	int (*property_is_writeable)(struct power_supply *psy,
228 				     enum power_supply_property psp);
229 	void (*external_power_changed)(struct power_supply *psy);
230 	void (*set_charged)(struct power_supply *psy);
231 
232 	/*
233 	 * Set if thermal zone should not be created for this power supply.
234 	 * For example for virtual supplies forwarding calls to actual
235 	 * sensors or other supplies.
236 	 */
237 	bool no_thermal;
238 	/* For APM emulation, think legacy userspace. */
239 	int use_for_apm;
240 };
241 
242 struct power_supply {
243 	const struct power_supply_desc *desc;
244 
245 	char **supplied_to;
246 	size_t num_supplicants;
247 
248 	char **supplied_from;
249 	size_t num_supplies;
250 	struct device_node *of_node;
251 
252 	/* Driver private data */
253 	void *drv_data;
254 
255 	/* private */
256 	struct device dev;
257 	struct work_struct changed_work;
258 	struct delayed_work deferred_register_work;
259 	spinlock_t changed_lock;
260 	bool changed;
261 	bool initialized;
262 	bool removing;
263 	atomic_t use_cnt;
264 #ifdef CONFIG_THERMAL
265 	struct thermal_zone_device *tzd;
266 	struct thermal_cooling_device *tcd;
267 #endif
268 
269 #ifdef CONFIG_LEDS_TRIGGERS
270 	struct led_trigger *charging_full_trig;
271 	char *charging_full_trig_name;
272 	struct led_trigger *charging_trig;
273 	char *charging_trig_name;
274 	struct led_trigger *full_trig;
275 	char *full_trig_name;
276 	struct led_trigger *online_trig;
277 	char *online_trig_name;
278 	struct led_trigger *charging_blink_full_solid_trig;
279 	char *charging_blink_full_solid_trig_name;
280 #endif
281 };
282 
283 /*
284  * This is recommended structure to specify static power supply parameters.
285  * Generic one, parametrizable for different power supplies. Power supply
286  * class itself does not use it, but that's what implementing most platform
287  * drivers, should try reuse for consistency.
288  */
289 
290 struct power_supply_info {
291 	const char *name;
292 	int technology;
293 	int voltage_max_design;
294 	int voltage_min_design;
295 	int charge_full_design;
296 	int charge_empty_design;
297 	int energy_full_design;
298 	int energy_empty_design;
299 	int use_for_apm;
300 };
301 
302 /*
303  * This is the recommended struct to manage static battery parameters,
304  * populated by power_supply_get_battery_info(). Most platform drivers should
305  * use these for consistency.
306  * Its field names must correspond to elements in enum power_supply_property.
307  * The default field value is -EINVAL.
308  * Power supply class itself doesn't use this.
309  */
310 
311 struct power_supply_battery_info {
312 	int energy_full_design_uwh;	    /* microWatt-hours */
313 	int charge_full_design_uah;	    /* microAmp-hours */
314 	int voltage_min_design_uv;	    /* microVolts */
315 	int precharge_current_ua;	    /* microAmps */
316 	int charge_term_current_ua;	    /* microAmps */
317 	int constant_charge_current_max_ua; /* microAmps */
318 	int constant_charge_voltage_max_uv; /* microVolts */
319 };
320 
321 extern struct atomic_notifier_head power_supply_notifier;
322 extern int power_supply_reg_notifier(struct notifier_block *nb);
323 extern void power_supply_unreg_notifier(struct notifier_block *nb);
324 extern struct power_supply *power_supply_get_by_name(const char *name);
325 extern void power_supply_put(struct power_supply *psy);
326 #ifdef CONFIG_OF
327 extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
328 							const char *property);
329 extern struct power_supply *devm_power_supply_get_by_phandle(
330 				    struct device *dev, const char *property);
331 #else /* !CONFIG_OF */
332 static inline struct power_supply *
power_supply_get_by_phandle(struct device_node * np,const char * property)333 power_supply_get_by_phandle(struct device_node *np, const char *property)
334 { return NULL; }
335 static inline struct power_supply *
devm_power_supply_get_by_phandle(struct device * dev,const char * property)336 devm_power_supply_get_by_phandle(struct device *dev, const char *property)
337 { return NULL; }
338 #endif /* CONFIG_OF */
339 
340 extern int power_supply_get_battery_info(struct power_supply *psy,
341 					 struct power_supply_battery_info *info);
342 extern void power_supply_changed(struct power_supply *psy);
343 extern int power_supply_am_i_supplied(struct power_supply *psy);
344 extern int power_supply_set_input_current_limit_from_supplier(
345 					 struct power_supply *psy);
346 extern int power_supply_set_battery_charged(struct power_supply *psy);
347 
348 #ifdef CONFIG_POWER_SUPPLY
349 extern int power_supply_is_system_supplied(void);
350 #else
power_supply_is_system_supplied(void)351 static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
352 #endif
353 
354 extern int power_supply_get_property(struct power_supply *psy,
355 			    enum power_supply_property psp,
356 			    union power_supply_propval *val);
357 extern int power_supply_set_property(struct power_supply *psy,
358 			    enum power_supply_property psp,
359 			    const union power_supply_propval *val);
360 extern int power_supply_property_is_writeable(struct power_supply *psy,
361 					enum power_supply_property psp);
362 extern void power_supply_external_power_changed(struct power_supply *psy);
363 
364 extern struct power_supply *__must_check
365 power_supply_register(struct device *parent,
366 				 const struct power_supply_desc *desc,
367 				 const struct power_supply_config *cfg);
368 extern struct power_supply *__must_check
369 power_supply_register_no_ws(struct device *parent,
370 				 const struct power_supply_desc *desc,
371 				 const struct power_supply_config *cfg);
372 extern struct power_supply *__must_check
373 devm_power_supply_register(struct device *parent,
374 				 const struct power_supply_desc *desc,
375 				 const struct power_supply_config *cfg);
376 extern struct power_supply *__must_check
377 devm_power_supply_register_no_ws(struct device *parent,
378 				 const struct power_supply_desc *desc,
379 				 const struct power_supply_config *cfg);
380 extern void power_supply_unregister(struct power_supply *psy);
381 extern int power_supply_powers(struct power_supply *psy, struct device *dev);
382 
383 extern void *power_supply_get_drvdata(struct power_supply *psy);
384 /* For APM emulation, think legacy userspace. */
385 extern struct class *power_supply_class;
386 
power_supply_is_amp_property(enum power_supply_property psp)387 static inline bool power_supply_is_amp_property(enum power_supply_property psp)
388 {
389 	switch (psp) {
390 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
391 	case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
392 	case POWER_SUPPLY_PROP_CHARGE_FULL:
393 	case POWER_SUPPLY_PROP_CHARGE_EMPTY:
394 	case POWER_SUPPLY_PROP_CHARGE_NOW:
395 	case POWER_SUPPLY_PROP_CHARGE_AVG:
396 	case POWER_SUPPLY_PROP_CHARGE_COUNTER:
397 	case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
398 	case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
399 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
400 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
401 	case POWER_SUPPLY_PROP_CURRENT_MAX:
402 	case POWER_SUPPLY_PROP_CURRENT_NOW:
403 	case POWER_SUPPLY_PROP_CURRENT_AVG:
404 	case POWER_SUPPLY_PROP_CURRENT_BOOT:
405 		return 1;
406 	default:
407 		break;
408 	}
409 
410 	return 0;
411 }
412 
power_supply_is_watt_property(enum power_supply_property psp)413 static inline bool power_supply_is_watt_property(enum power_supply_property psp)
414 {
415 	switch (psp) {
416 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
417 	case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
418 	case POWER_SUPPLY_PROP_ENERGY_FULL:
419 	case POWER_SUPPLY_PROP_ENERGY_EMPTY:
420 	case POWER_SUPPLY_PROP_ENERGY_NOW:
421 	case POWER_SUPPLY_PROP_ENERGY_AVG:
422 	case POWER_SUPPLY_PROP_VOLTAGE_MAX:
423 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
424 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
425 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
426 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
427 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
428 	case POWER_SUPPLY_PROP_VOLTAGE_OCV:
429 	case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
430 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
431 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
432 	case POWER_SUPPLY_PROP_POWER_NOW:
433 		return 1;
434 	default:
435 		break;
436 	}
437 
438 	return 0;
439 }
440 
441 #endif /* __LINUX_POWER_SUPPLY_H__ */
442