Merge branch 'android-4.14-p' into android-exynos-4.14-ww-9610-minor_up-dev
[GitHub/moto-9609/android_kernel_motorola_exynos9610.git] / include / linux / power_supply.h
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 POWER_SUPPLY_HEALTH_UNDERVOLTAGE,
62 };
63
64 enum {
65 POWER_SUPPLY_TECHNOLOGY_UNKNOWN = 0,
66 POWER_SUPPLY_TECHNOLOGY_NiMH,
67 POWER_SUPPLY_TECHNOLOGY_LION,
68 POWER_SUPPLY_TECHNOLOGY_LIPO,
69 POWER_SUPPLY_TECHNOLOGY_LiFe,
70 POWER_SUPPLY_TECHNOLOGY_NiCd,
71 POWER_SUPPLY_TECHNOLOGY_LiMn,
72 };
73
74 enum {
75 POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN = 0,
76 POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL,
77 POWER_SUPPLY_CAPACITY_LEVEL_LOW,
78 POWER_SUPPLY_CAPACITY_LEVEL_NORMAL,
79 POWER_SUPPLY_CAPACITY_LEVEL_HIGH,
80 POWER_SUPPLY_CAPACITY_LEVEL_FULL,
81 };
82
83 enum {
84 POWER_SUPPLY_SCOPE_UNKNOWN = 0,
85 POWER_SUPPLY_SCOPE_SYSTEM,
86 POWER_SUPPLY_SCOPE_DEVICE,
87 };
88
89 enum power_supply_property {
90 /* Properties of type `int' */
91 POWER_SUPPLY_PROP_STATUS = 0,
92 POWER_SUPPLY_PROP_CHARGE_TYPE,
93 POWER_SUPPLY_PROP_HEALTH,
94 POWER_SUPPLY_PROP_PRESENT,
95 POWER_SUPPLY_PROP_ONLINE,
96 POWER_SUPPLY_PROP_AUTHENTIC,
97 POWER_SUPPLY_PROP_CHARGING_ENABLED,
98 POWER_SUPPLY_PROP_TECHNOLOGY,
99 POWER_SUPPLY_PROP_CYCLE_COUNT,
100 POWER_SUPPLY_PROP_VOLTAGE_MAX,
101 POWER_SUPPLY_PROP_VOLTAGE_MIN,
102 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
103 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
104 POWER_SUPPLY_PROP_VOLTAGE_NOW,
105 POWER_SUPPLY_PROP_VOLTAGE_AVG,
106 POWER_SUPPLY_PROP_VOLTAGE_OCV,
107 POWER_SUPPLY_PROP_VOLTAGE_BOOT,
108 POWER_SUPPLY_PROP_INPUT_VOLTAGE_REGULATION,
109 POWER_SUPPLY_PROP_CURRENT_MAX,
110 POWER_SUPPLY_PROP_CURRENT_NOW,
111 POWER_SUPPLY_PROP_CURRENT_AVG,
112 POWER_SUPPLY_PROP_CURRENT_FULL,
113 POWER_SUPPLY_PROP_CURRENT_BOOT,
114 POWER_SUPPLY_PROP_POWER_NOW,
115 POWER_SUPPLY_PROP_POWER_AVG,
116 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
117 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
118 POWER_SUPPLY_PROP_CHARGE_FULL,
119 POWER_SUPPLY_PROP_CHARGE_EMPTY,
120 POWER_SUPPLY_PROP_CHARGE_NOW,
121 POWER_SUPPLY_PROP_CHARGE_AVG,
122 POWER_SUPPLY_PROP_CHARGE_COUNTER,
123 POWER_SUPPLY_PROP_CHARGE_OTG_CONTROL,
124 POWER_SUPPLY_PROP_CHARGE_POWERED_OTG_CONTROL,
125 POWER_SUPPLY_PROP_CHARGE_TEMP,
126 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
127 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
128 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
129 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
130 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
131 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
132 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
133 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
134 POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN,
135 POWER_SUPPLY_PROP_ENERGY_FULL,
136 POWER_SUPPLY_PROP_ENERGY_EMPTY,
137 POWER_SUPPLY_PROP_ENERGY_NOW,
138 POWER_SUPPLY_PROP_ENERGY_AVG,
139 POWER_SUPPLY_PROP_CAPACITY, /* in percents! */
140 POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN, /* in percents! */
141 POWER_SUPPLY_PROP_CAPACITY_ALERT_MAX, /* in percents! */
142 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
143 POWER_SUPPLY_PROP_TEMP,
144 POWER_SUPPLY_PROP_TEMP_MAX,
145 POWER_SUPPLY_PROP_TEMP_MIN,
146 POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
147 POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
148 POWER_SUPPLY_PROP_TEMP_AMBIENT,
149 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN,
150 POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX,
151 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
152 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
153 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
154 POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
155 POWER_SUPPLY_PROP_TYPE, /* use power_supply.type instead */
156 POWER_SUPPLY_PROP_SCOPE,
157 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
158 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
159 POWER_SUPPLY_PROP_CALIBRATE,
160 /* Local extensions */
161 POWER_SUPPLY_PROP_USB_HC,
162 POWER_SUPPLY_PROP_USB_OTG,
163 POWER_SUPPLY_PROP_CHARGE_ENABLED,
164 POWER_SUPPLY_PROP_FUELGAUGE_RESET,
165 POWER_SUPPLY_PROP_SOH,
166 /* Local extensions of type int64_t */
167 POWER_SUPPLY_PROP_CHARGE_COUNTER_EXT,
168 /* Properties of type `const char *' */
169 POWER_SUPPLY_PROP_MODEL_NAME,
170 POWER_SUPPLY_PROP_MANUFACTURER,
171 POWER_SUPPLY_PROP_SERIAL_NUMBER,
172
173 POWER_SUPPLY_PROP_VCHGIN,
174 POWER_SUPPLY_PROP_VWCIN,
175 POWER_SUPPLY_PROP_VBYP,
176 POWER_SUPPLY_PROP_VSYS,
177 POWER_SUPPLY_PROP_VBAT,
178 POWER_SUPPLY_PROP_VGPADC,
179 POWER_SUPPLY_PROP_VCC1,
180 POWER_SUPPLY_PROP_VCC2,
181 POWER_SUPPLY_PROP_ICHGIN,
182 POWER_SUPPLY_PROP_IWCIN,
183 POWER_SUPPLY_PROP_IOTG,
184 POWER_SUPPLY_PROP_ITX,
185 POWER_SUPPLY_PROP_CO_ENABLE,
186 POWER_SUPPLY_PROP_RR_ENABLE,
187 POWER_SUPPLY_PROP_USBPD_RESET,
188 POWER_SUPPLY_PROP_USBPD_TEST_READ,
189 };
190
191 enum power_supply_type {
192 POWER_SUPPLY_TYPE_UNKNOWN = 0,
193 POWER_SUPPLY_TYPE_BATTERY,
194 POWER_SUPPLY_TYPE_UPS,
195 POWER_SUPPLY_TYPE_MAINS,
196 POWER_SUPPLY_TYPE_USB, /* Standard Downstream Port */
197 POWER_SUPPLY_TYPE_USB_DCP, /* Dedicated Charging Port */
198 POWER_SUPPLY_TYPE_USB_CDP, /* Charging Downstream Port */
199 POWER_SUPPLY_TYPE_USB_ACA, /* Accessory Charger Adapters */
200 POWER_SUPPLY_TYPE_USB_TYPE_C, /* Type C Port */
201 POWER_SUPPLY_TYPE_USB_PD, /* Power Delivery Port */
202 POWER_SUPPLY_TYPE_USB_PD_DRP, /* PD Dual Role Port */
203 POWER_SUPPLY_TYPE_APPLE_BRICK_ID, /* Apple Charging Method */
204 POWER_SUPPLY_TYPE_OTG,
205 POWER_SUPPLY_TYPE_HV_MAINS,
206 POWER_SUPPLY_TYPE_PREPARE_TA,
207 POWER_SUPPLY_TYPE_SMART_NOTG,
208 POWER_SUPPLY_TYPE_END,
209 };
210
211 enum power_supply_notifier_events {
212 PSY_EVENT_PROP_CHANGED,
213 PSY_EVENT_PROP_ADDED,
214 };
215
216 union power_supply_propval {
217 int intval;
218 const char *strval;
219 int64_t int64val;
220 };
221
222 struct device_node;
223 struct power_supply;
224
225 /* Run-time specific power supply configuration */
226 struct power_supply_config {
227 struct device_node *of_node;
228 /* Driver private data */
229 void *drv_data;
230
231 char **supplied_to;
232 size_t num_supplicants;
233 };
234
235 /* Description of power supply */
236 struct power_supply_desc {
237 const char *name;
238 enum power_supply_type type;
239 enum power_supply_property *properties;
240 size_t num_properties;
241
242 /*
243 * Functions for drivers implementing power supply class.
244 * These shouldn't be called directly by other drivers for accessing
245 * this power supply. Instead use power_supply_*() functions (for
246 * example power_supply_get_property()).
247 */
248 int (*get_property)(struct power_supply *psy,
249 enum power_supply_property psp,
250 union power_supply_propval *val);
251 int (*set_property)(struct power_supply *psy,
252 enum power_supply_property psp,
253 const union power_supply_propval *val);
254 /*
255 * property_is_writeable() will be called during registration
256 * of power supply. If this happens during device probe then it must
257 * not access internal data of device (because probe did not end).
258 */
259 int (*property_is_writeable)(struct power_supply *psy,
260 enum power_supply_property psp);
261 void (*external_power_changed)(struct power_supply *psy);
262 void (*set_charged)(struct power_supply *psy);
263
264 /*
265 * Set if thermal zone should not be created for this power supply.
266 * For example for virtual supplies forwarding calls to actual
267 * sensors or other supplies.
268 */
269 bool no_thermal;
270 /* For APM emulation, think legacy userspace. */
271 int use_for_apm;
272 };
273
274 struct power_supply {
275 const struct power_supply_desc *desc;
276
277 char **supplied_to;
278 size_t num_supplicants;
279
280 char **supplied_from;
281 size_t num_supplies;
282 struct device_node *of_node;
283
284 /* Driver private data */
285 void *drv_data;
286
287 /* private */
288 struct device dev;
289 struct work_struct changed_work;
290 struct delayed_work deferred_register_work;
291 spinlock_t changed_lock;
292 bool changed;
293 bool initialized;
294 bool removing;
295 atomic_t use_cnt;
296 #ifdef CONFIG_THERMAL
297 struct thermal_zone_device *tzd;
298 struct thermal_cooling_device *tcd;
299 #endif
300
301 #ifdef CONFIG_LEDS_TRIGGERS
302 struct led_trigger *charging_full_trig;
303 char *charging_full_trig_name;
304 struct led_trigger *charging_trig;
305 char *charging_trig_name;
306 struct led_trigger *full_trig;
307 char *full_trig_name;
308 struct led_trigger *online_trig;
309 char *online_trig_name;
310 struct led_trigger *charging_blink_full_solid_trig;
311 char *charging_blink_full_solid_trig_name;
312 #endif
313 };
314
315 /*
316 * This is recommended structure to specify static power supply parameters.
317 * Generic one, parametrizable for different power supplies. Power supply
318 * class itself does not use it, but that's what implementing most platform
319 * drivers, should try reuse for consistency.
320 */
321
322 struct power_supply_info {
323 const char *name;
324 int technology;
325 int voltage_max_design;
326 int voltage_min_design;
327 int charge_full_design;
328 int charge_empty_design;
329 int energy_full_design;
330 int energy_empty_design;
331 int use_for_apm;
332 };
333
334 /*
335 * This is the recommended struct to manage static battery parameters,
336 * populated by power_supply_get_battery_info(). Most platform drivers should
337 * use these for consistency.
338 * Its field names must correspond to elements in enum power_supply_property.
339 * The default field value is -EINVAL.
340 * Power supply class itself doesn't use this.
341 */
342
343 struct power_supply_battery_info {
344 int energy_full_design_uwh; /* microWatt-hours */
345 int charge_full_design_uah; /* microAmp-hours */
346 int voltage_min_design_uv; /* microVolts */
347 int precharge_current_ua; /* microAmps */
348 int charge_term_current_ua; /* microAmps */
349 int constant_charge_current_max_ua; /* microAmps */
350 int constant_charge_voltage_max_uv; /* microVolts */
351 };
352
353 extern struct atomic_notifier_head power_supply_notifier;
354 extern int power_supply_reg_notifier(struct notifier_block *nb);
355 extern void power_supply_unreg_notifier(struct notifier_block *nb);
356 extern struct power_supply *power_supply_get_by_name(const char *name);
357 extern void power_supply_put(struct power_supply *psy);
358 #ifdef CONFIG_OF
359 extern struct power_supply *power_supply_get_by_phandle(struct device_node *np,
360 const char *property);
361 extern struct power_supply *devm_power_supply_get_by_phandle(
362 struct device *dev, const char *property);
363 #else /* !CONFIG_OF */
364 static inline struct power_supply *
365 power_supply_get_by_phandle(struct device_node *np, const char *property)
366 { return NULL; }
367 static inline struct power_supply *
368 devm_power_supply_get_by_phandle(struct device *dev, const char *property)
369 { return NULL; }
370 #endif /* CONFIG_OF */
371
372 extern int power_supply_get_battery_info(struct power_supply *psy,
373 struct power_supply_battery_info *info);
374 extern void power_supply_changed(struct power_supply *psy);
375 extern int power_supply_am_i_supplied(struct power_supply *psy);
376 extern int power_supply_set_input_current_limit_from_supplier(
377 struct power_supply *psy);
378 extern int power_supply_set_battery_charged(struct power_supply *psy);
379
380 #ifdef CONFIG_POWER_SUPPLY
381 extern int power_supply_is_system_supplied(void);
382 #else
383 static inline int power_supply_is_system_supplied(void) { return -ENOSYS; }
384 #endif
385
386 extern int power_supply_get_property(struct power_supply *psy,
387 enum power_supply_property psp,
388 union power_supply_propval *val);
389 extern int power_supply_set_property(struct power_supply *psy,
390 enum power_supply_property psp,
391 const union power_supply_propval *val);
392 extern int power_supply_property_is_writeable(struct power_supply *psy,
393 enum power_supply_property psp);
394 extern void power_supply_external_power_changed(struct power_supply *psy);
395
396 extern struct power_supply *__must_check
397 power_supply_register(struct device *parent,
398 const struct power_supply_desc *desc,
399 const struct power_supply_config *cfg);
400 extern struct power_supply *__must_check
401 power_supply_register_no_ws(struct device *parent,
402 const struct power_supply_desc *desc,
403 const struct power_supply_config *cfg);
404 extern struct power_supply *__must_check
405 devm_power_supply_register(struct device *parent,
406 const struct power_supply_desc *desc,
407 const struct power_supply_config *cfg);
408 extern struct power_supply *__must_check
409 devm_power_supply_register_no_ws(struct device *parent,
410 const struct power_supply_desc *desc,
411 const struct power_supply_config *cfg);
412 extern void power_supply_unregister(struct power_supply *psy);
413 extern int power_supply_powers(struct power_supply *psy, struct device *dev);
414
415 extern void *power_supply_get_drvdata(struct power_supply *psy);
416 /* For APM emulation, think legacy userspace. */
417 extern struct class *power_supply_class;
418
419 static inline bool power_supply_is_amp_property(enum power_supply_property psp)
420 {
421 switch (psp) {
422 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
423 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
424 case POWER_SUPPLY_PROP_CHARGE_FULL:
425 case POWER_SUPPLY_PROP_CHARGE_EMPTY:
426 case POWER_SUPPLY_PROP_CHARGE_NOW:
427 case POWER_SUPPLY_PROP_CHARGE_AVG:
428 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
429 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
430 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
431 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
432 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
433 case POWER_SUPPLY_PROP_CURRENT_MAX:
434 case POWER_SUPPLY_PROP_CURRENT_NOW:
435 case POWER_SUPPLY_PROP_CURRENT_AVG:
436 case POWER_SUPPLY_PROP_CURRENT_BOOT:
437 return 1;
438 default:
439 break;
440 }
441
442 return 0;
443 }
444
445 static inline bool power_supply_is_watt_property(enum power_supply_property psp)
446 {
447 switch (psp) {
448 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
449 case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
450 case POWER_SUPPLY_PROP_ENERGY_FULL:
451 case POWER_SUPPLY_PROP_ENERGY_EMPTY:
452 case POWER_SUPPLY_PROP_ENERGY_NOW:
453 case POWER_SUPPLY_PROP_ENERGY_AVG:
454 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
455 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
456 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
457 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
458 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
459 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
460 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
461 case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
462 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
463 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
464 case POWER_SUPPLY_PROP_POWER_NOW:
465 return 1;
466 default:
467 break;
468 }
469
470 return 0;
471 }
472
473 #endif /* __LINUX_POWER_SUPPLY_H__ */