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