1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* |
3 | * Universal power supply monitor class |
4 | * |
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 | |
12 | #include <linux/module.h> |
13 | #include <linux/types.h> |
14 | #include <linux/init.h> |
15 | #include <linux/slab.h> |
16 | #include <linux/delay.h> |
17 | #include <linux/device.h> |
18 | #include <linux/notifier.h> |
19 | #include <linux/err.h> |
20 | #include <linux/of.h> |
21 | #include <linux/power_supply.h> |
22 | #include <linux/property.h> |
23 | #include <linux/thermal.h> |
24 | #include <linux/fixp-arith.h> |
25 | #include "power_supply.h" |
26 | #include "samsung-sdi-battery.h" |
27 | |
28 | /* exported for the APM Power driver, APM emulation */ |
29 | struct class *power_supply_class; |
30 | EXPORT_SYMBOL_GPL(power_supply_class); |
31 | |
32 | static BLOCKING_NOTIFIER_HEAD(power_supply_notifier); |
33 | |
34 | static struct device_type power_supply_dev_type; |
35 | |
36 | #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10) |
37 | |
38 | static bool __power_supply_is_supplied_by(struct power_supply *supplier, |
39 | struct power_supply *supply) |
40 | { |
41 | int i; |
42 | |
43 | if (!supply->supplied_from && !supplier->supplied_to) |
44 | return false; |
45 | |
46 | /* Support both supplied_to and supplied_from modes */ |
47 | if (supply->supplied_from) { |
48 | if (!supplier->desc->name) |
49 | return false; |
50 | for (i = 0; i < supply->num_supplies; i++) |
51 | if (!strcmp(supplier->desc->name, supply->supplied_from[i])) |
52 | return true; |
53 | } else { |
54 | if (!supply->desc->name) |
55 | return false; |
56 | for (i = 0; i < supplier->num_supplicants; i++) |
57 | if (!strcmp(supplier->supplied_to[i], supply->desc->name)) |
58 | return true; |
59 | } |
60 | |
61 | return false; |
62 | } |
63 | |
64 | static int __power_supply_changed_work(struct device *dev, void *data) |
65 | { |
66 | struct power_supply *psy = data; |
67 | struct power_supply *pst = dev_get_drvdata(dev); |
68 | |
69 | if (__power_supply_is_supplied_by(supplier: psy, supply: pst)) { |
70 | if (pst->desc->external_power_changed) |
71 | pst->desc->external_power_changed(pst); |
72 | } |
73 | |
74 | return 0; |
75 | } |
76 | |
77 | static void power_supply_changed_work(struct work_struct *work) |
78 | { |
79 | unsigned long flags; |
80 | struct power_supply *psy = container_of(work, struct power_supply, |
81 | changed_work); |
82 | |
83 | dev_dbg(&psy->dev, "%s\n" , __func__); |
84 | |
85 | spin_lock_irqsave(&psy->changed_lock, flags); |
86 | /* |
87 | * Check 'changed' here to avoid issues due to race between |
88 | * power_supply_changed() and this routine. In worst case |
89 | * power_supply_changed() can be called again just before we take above |
90 | * lock. During the first call of this routine we will mark 'changed' as |
91 | * false and it will stay false for the next call as well. |
92 | */ |
93 | if (likely(psy->changed)) { |
94 | psy->changed = false; |
95 | spin_unlock_irqrestore(lock: &psy->changed_lock, flags); |
96 | class_for_each_device(class: power_supply_class, NULL, data: psy, |
97 | fn: __power_supply_changed_work); |
98 | power_supply_update_leds(psy); |
99 | blocking_notifier_call_chain(nh: &power_supply_notifier, |
100 | val: PSY_EVENT_PROP_CHANGED, v: psy); |
101 | kobject_uevent(kobj: &psy->dev.kobj, action: KOBJ_CHANGE); |
102 | spin_lock_irqsave(&psy->changed_lock, flags); |
103 | } |
104 | |
105 | /* |
106 | * Hold the wakeup_source until all events are processed. |
107 | * power_supply_changed() might have called again and have set 'changed' |
108 | * to true. |
109 | */ |
110 | if (likely(!psy->changed)) |
111 | pm_relax(dev: &psy->dev); |
112 | spin_unlock_irqrestore(lock: &psy->changed_lock, flags); |
113 | } |
114 | |
115 | void power_supply_changed(struct power_supply *psy) |
116 | { |
117 | unsigned long flags; |
118 | |
119 | dev_dbg(&psy->dev, "%s\n" , __func__); |
120 | |
121 | spin_lock_irqsave(&psy->changed_lock, flags); |
122 | psy->changed = true; |
123 | pm_stay_awake(dev: &psy->dev); |
124 | spin_unlock_irqrestore(lock: &psy->changed_lock, flags); |
125 | schedule_work(work: &psy->changed_work); |
126 | } |
127 | EXPORT_SYMBOL_GPL(power_supply_changed); |
128 | |
129 | /* |
130 | * Notify that power supply was registered after parent finished the probing. |
131 | * |
132 | * Often power supply is registered from driver's probe function. However |
133 | * calling power_supply_changed() directly from power_supply_register() |
134 | * would lead to execution of get_property() function provided by the driver |
135 | * too early - before the probe ends. |
136 | * |
137 | * Avoid that by waiting on parent's mutex. |
138 | */ |
139 | static void power_supply_deferred_register_work(struct work_struct *work) |
140 | { |
141 | struct power_supply *psy = container_of(work, struct power_supply, |
142 | deferred_register_work.work); |
143 | |
144 | if (psy->dev.parent) { |
145 | while (!mutex_trylock(lock: &psy->dev.parent->mutex)) { |
146 | if (psy->removing) |
147 | return; |
148 | msleep(msecs: 10); |
149 | } |
150 | } |
151 | |
152 | power_supply_changed(psy); |
153 | |
154 | if (psy->dev.parent) |
155 | mutex_unlock(lock: &psy->dev.parent->mutex); |
156 | } |
157 | |
158 | #ifdef CONFIG_OF |
159 | static int __power_supply_populate_supplied_from(struct device *dev, |
160 | void *data) |
161 | { |
162 | struct power_supply *psy = data; |
163 | struct power_supply *epsy = dev_get_drvdata(dev); |
164 | struct device_node *np; |
165 | int i = 0; |
166 | |
167 | do { |
168 | np = of_parse_phandle(np: psy->of_node, phandle_name: "power-supplies" , index: i++); |
169 | if (!np) |
170 | break; |
171 | |
172 | if (np == epsy->of_node) { |
173 | dev_dbg(&psy->dev, "%s: Found supply : %s\n" , |
174 | psy->desc->name, epsy->desc->name); |
175 | psy->supplied_from[i-1] = (char *)epsy->desc->name; |
176 | psy->num_supplies++; |
177 | of_node_put(node: np); |
178 | break; |
179 | } |
180 | of_node_put(node: np); |
181 | } while (np); |
182 | |
183 | return 0; |
184 | } |
185 | |
186 | static int power_supply_populate_supplied_from(struct power_supply *psy) |
187 | { |
188 | int error; |
189 | |
190 | error = class_for_each_device(class: power_supply_class, NULL, data: psy, |
191 | fn: __power_supply_populate_supplied_from); |
192 | |
193 | dev_dbg(&psy->dev, "%s %d\n" , __func__, error); |
194 | |
195 | return error; |
196 | } |
197 | |
198 | static int __power_supply_find_supply_from_node(struct device *dev, |
199 | void *data) |
200 | { |
201 | struct device_node *np = data; |
202 | struct power_supply *epsy = dev_get_drvdata(dev); |
203 | |
204 | /* returning non-zero breaks out of class_for_each_device loop */ |
205 | if (epsy->of_node == np) |
206 | return 1; |
207 | |
208 | return 0; |
209 | } |
210 | |
211 | static int power_supply_find_supply_from_node(struct device_node *supply_node) |
212 | { |
213 | int error; |
214 | |
215 | /* |
216 | * class_for_each_device() either returns its own errors or values |
217 | * returned by __power_supply_find_supply_from_node(). |
218 | * |
219 | * __power_supply_find_supply_from_node() will return 0 (no match) |
220 | * or 1 (match). |
221 | * |
222 | * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if |
223 | * it returned 0, or error as returned by it. |
224 | */ |
225 | error = class_for_each_device(class: power_supply_class, NULL, data: supply_node, |
226 | fn: __power_supply_find_supply_from_node); |
227 | |
228 | return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER; |
229 | } |
230 | |
231 | static int power_supply_check_supplies(struct power_supply *psy) |
232 | { |
233 | struct device_node *np; |
234 | int cnt = 0; |
235 | |
236 | /* If there is already a list honor it */ |
237 | if (psy->supplied_from && psy->num_supplies > 0) |
238 | return 0; |
239 | |
240 | /* No device node found, nothing to do */ |
241 | if (!psy->of_node) |
242 | return 0; |
243 | |
244 | do { |
245 | int ret; |
246 | |
247 | np = of_parse_phandle(np: psy->of_node, phandle_name: "power-supplies" , index: cnt++); |
248 | if (!np) |
249 | break; |
250 | |
251 | ret = power_supply_find_supply_from_node(supply_node: np); |
252 | of_node_put(node: np); |
253 | |
254 | if (ret) { |
255 | dev_dbg(&psy->dev, "Failed to find supply!\n" ); |
256 | return ret; |
257 | } |
258 | } while (np); |
259 | |
260 | /* Missing valid "power-supplies" entries */ |
261 | if (cnt == 1) |
262 | return 0; |
263 | |
264 | /* All supplies found, allocate char ** array for filling */ |
265 | psy->supplied_from = devm_kzalloc(dev: &psy->dev, size: sizeof(*psy->supplied_from), |
266 | GFP_KERNEL); |
267 | if (!psy->supplied_from) |
268 | return -ENOMEM; |
269 | |
270 | *psy->supplied_from = devm_kcalloc(dev: &psy->dev, |
271 | n: cnt - 1, size: sizeof(**psy->supplied_from), |
272 | GFP_KERNEL); |
273 | if (!*psy->supplied_from) |
274 | return -ENOMEM; |
275 | |
276 | return power_supply_populate_supplied_from(psy); |
277 | } |
278 | #else |
279 | static int power_supply_check_supplies(struct power_supply *psy) |
280 | { |
281 | int nval, ret; |
282 | |
283 | if (!psy->dev.parent) |
284 | return 0; |
285 | |
286 | nval = device_property_string_array_count(psy->dev.parent, "supplied-from" ); |
287 | if (nval <= 0) |
288 | return 0; |
289 | |
290 | psy->supplied_from = devm_kmalloc_array(&psy->dev, nval, |
291 | sizeof(char *), GFP_KERNEL); |
292 | if (!psy->supplied_from) |
293 | return -ENOMEM; |
294 | |
295 | ret = device_property_read_string_array(psy->dev.parent, |
296 | "supplied-from" , (const char **)psy->supplied_from, nval); |
297 | if (ret < 0) |
298 | return ret; |
299 | |
300 | psy->num_supplies = nval; |
301 | |
302 | return 0; |
303 | } |
304 | #endif |
305 | |
306 | struct psy_am_i_supplied_data { |
307 | struct power_supply *psy; |
308 | unsigned int count; |
309 | }; |
310 | |
311 | static int __power_supply_am_i_supplied(struct device *dev, void *_data) |
312 | { |
313 | union power_supply_propval ret = {0,}; |
314 | struct power_supply *epsy = dev_get_drvdata(dev); |
315 | struct psy_am_i_supplied_data *data = _data; |
316 | |
317 | if (__power_supply_is_supplied_by(supplier: epsy, supply: data->psy)) { |
318 | data->count++; |
319 | if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, |
320 | &ret)) |
321 | return ret.intval; |
322 | } |
323 | |
324 | return 0; |
325 | } |
326 | |
327 | int power_supply_am_i_supplied(struct power_supply *psy) |
328 | { |
329 | struct psy_am_i_supplied_data data = { psy, 0 }; |
330 | int error; |
331 | |
332 | error = class_for_each_device(class: power_supply_class, NULL, data: &data, |
333 | fn: __power_supply_am_i_supplied); |
334 | |
335 | dev_dbg(&psy->dev, "%s count %u err %d\n" , __func__, data.count, error); |
336 | |
337 | if (data.count == 0) |
338 | return -ENODEV; |
339 | |
340 | return error; |
341 | } |
342 | EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); |
343 | |
344 | static int __power_supply_is_system_supplied(struct device *dev, void *data) |
345 | { |
346 | union power_supply_propval ret = {0,}; |
347 | struct power_supply *psy = dev_get_drvdata(dev); |
348 | unsigned int *count = data; |
349 | |
350 | if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_SCOPE, &ret)) |
351 | if (ret.intval == POWER_SUPPLY_SCOPE_DEVICE) |
352 | return 0; |
353 | |
354 | (*count)++; |
355 | if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY) |
356 | if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE, |
357 | &ret)) |
358 | return ret.intval; |
359 | |
360 | return 0; |
361 | } |
362 | |
363 | int power_supply_is_system_supplied(void) |
364 | { |
365 | int error; |
366 | unsigned int count = 0; |
367 | |
368 | error = class_for_each_device(class: power_supply_class, NULL, data: &count, |
369 | fn: __power_supply_is_system_supplied); |
370 | |
371 | /* |
372 | * If no system scope power class device was found at all, most probably we |
373 | * are running on a desktop system, so assume we are on mains power. |
374 | */ |
375 | if (count == 0) |
376 | return 1; |
377 | |
378 | return error; |
379 | } |
380 | EXPORT_SYMBOL_GPL(power_supply_is_system_supplied); |
381 | |
382 | struct psy_get_supplier_prop_data { |
383 | struct power_supply *psy; |
384 | enum power_supply_property psp; |
385 | union power_supply_propval *val; |
386 | }; |
387 | |
388 | static int __power_supply_get_supplier_property(struct device *dev, void *_data) |
389 | { |
390 | struct power_supply *epsy = dev_get_drvdata(dev); |
391 | struct psy_get_supplier_prop_data *data = _data; |
392 | |
393 | if (__power_supply_is_supplied_by(supplier: epsy, supply: data->psy)) |
394 | if (!power_supply_get_property(psy: epsy, psp: data->psp, val: data->val)) |
395 | return 1; /* Success */ |
396 | |
397 | return 0; /* Continue iterating */ |
398 | } |
399 | |
400 | int power_supply_get_property_from_supplier(struct power_supply *psy, |
401 | enum power_supply_property psp, |
402 | union power_supply_propval *val) |
403 | { |
404 | struct psy_get_supplier_prop_data data = { |
405 | .psy = psy, |
406 | .psp = psp, |
407 | .val = val, |
408 | }; |
409 | int ret; |
410 | |
411 | /* |
412 | * This function is not intended for use with a supply with multiple |
413 | * suppliers, we simply pick the first supply to report the psp. |
414 | */ |
415 | ret = class_for_each_device(class: power_supply_class, NULL, data: &data, |
416 | fn: __power_supply_get_supplier_property); |
417 | if (ret < 0) |
418 | return ret; |
419 | if (ret == 0) |
420 | return -ENODEV; |
421 | |
422 | return 0; |
423 | } |
424 | EXPORT_SYMBOL_GPL(power_supply_get_property_from_supplier); |
425 | |
426 | int power_supply_set_battery_charged(struct power_supply *psy) |
427 | { |
428 | if (atomic_read(v: &psy->use_cnt) >= 0 && |
429 | psy->desc->type == POWER_SUPPLY_TYPE_BATTERY && |
430 | psy->desc->set_charged) { |
431 | psy->desc->set_charged(psy); |
432 | return 0; |
433 | } |
434 | |
435 | return -EINVAL; |
436 | } |
437 | EXPORT_SYMBOL_GPL(power_supply_set_battery_charged); |
438 | |
439 | static int power_supply_match_device_by_name(struct device *dev, const void *data) |
440 | { |
441 | const char *name = data; |
442 | struct power_supply *psy = dev_get_drvdata(dev); |
443 | |
444 | return strcmp(psy->desc->name, name) == 0; |
445 | } |
446 | |
447 | /** |
448 | * power_supply_get_by_name() - Search for a power supply and returns its ref |
449 | * @name: Power supply name to fetch |
450 | * |
451 | * If power supply was found, it increases reference count for the |
452 | * internal power supply's device. The user should power_supply_put() |
453 | * after usage. |
454 | * |
455 | * Return: On success returns a reference to a power supply with |
456 | * matching name equals to @name, a NULL otherwise. |
457 | */ |
458 | struct power_supply *power_supply_get_by_name(const char *name) |
459 | { |
460 | struct power_supply *psy = NULL; |
461 | struct device *dev = class_find_device(class: power_supply_class, NULL, data: name, |
462 | match: power_supply_match_device_by_name); |
463 | |
464 | if (dev) { |
465 | psy = dev_get_drvdata(dev); |
466 | atomic_inc(v: &psy->use_cnt); |
467 | } |
468 | |
469 | return psy; |
470 | } |
471 | EXPORT_SYMBOL_GPL(power_supply_get_by_name); |
472 | |
473 | /** |
474 | * power_supply_put() - Drop reference obtained with power_supply_get_by_name |
475 | * @psy: Reference to put |
476 | * |
477 | * The reference to power supply should be put before unregistering |
478 | * the power supply. |
479 | */ |
480 | void power_supply_put(struct power_supply *psy) |
481 | { |
482 | might_sleep(); |
483 | |
484 | atomic_dec(v: &psy->use_cnt); |
485 | put_device(dev: &psy->dev); |
486 | } |
487 | EXPORT_SYMBOL_GPL(power_supply_put); |
488 | |
489 | #ifdef CONFIG_OF |
490 | static int power_supply_match_device_node(struct device *dev, const void *data) |
491 | { |
492 | return dev->parent && dev->parent->of_node == data; |
493 | } |
494 | |
495 | /** |
496 | * power_supply_get_by_phandle() - Search for a power supply and returns its ref |
497 | * @np: Pointer to device node holding phandle property |
498 | * @property: Name of property holding a power supply name |
499 | * |
500 | * If power supply was found, it increases reference count for the |
501 | * internal power supply's device. The user should power_supply_put() |
502 | * after usage. |
503 | * |
504 | * Return: On success returns a reference to a power supply with |
505 | * matching name equals to value under @property, NULL or ERR_PTR otherwise. |
506 | */ |
507 | struct power_supply *power_supply_get_by_phandle(struct device_node *np, |
508 | const char *property) |
509 | { |
510 | struct device_node *power_supply_np; |
511 | struct power_supply *psy = NULL; |
512 | struct device *dev; |
513 | |
514 | power_supply_np = of_parse_phandle(np, phandle_name: property, index: 0); |
515 | if (!power_supply_np) |
516 | return ERR_PTR(error: -ENODEV); |
517 | |
518 | dev = class_find_device(class: power_supply_class, NULL, data: power_supply_np, |
519 | match: power_supply_match_device_node); |
520 | |
521 | of_node_put(node: power_supply_np); |
522 | |
523 | if (dev) { |
524 | psy = dev_get_drvdata(dev); |
525 | atomic_inc(v: &psy->use_cnt); |
526 | } |
527 | |
528 | return psy; |
529 | } |
530 | EXPORT_SYMBOL_GPL(power_supply_get_by_phandle); |
531 | |
532 | static void devm_power_supply_put(struct device *dev, void *res) |
533 | { |
534 | struct power_supply **psy = res; |
535 | |
536 | power_supply_put(*psy); |
537 | } |
538 | |
539 | /** |
540 | * devm_power_supply_get_by_phandle() - Resource managed version of |
541 | * power_supply_get_by_phandle() |
542 | * @dev: Pointer to device holding phandle property |
543 | * @property: Name of property holding a power supply phandle |
544 | * |
545 | * Return: On success returns a reference to a power supply with |
546 | * matching name equals to value under @property, NULL or ERR_PTR otherwise. |
547 | */ |
548 | struct power_supply *devm_power_supply_get_by_phandle(struct device *dev, |
549 | const char *property) |
550 | { |
551 | struct power_supply **ptr, *psy; |
552 | |
553 | if (!dev->of_node) |
554 | return ERR_PTR(error: -ENODEV); |
555 | |
556 | ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL); |
557 | if (!ptr) |
558 | return ERR_PTR(error: -ENOMEM); |
559 | |
560 | psy = power_supply_get_by_phandle(dev->of_node, property); |
561 | if (IS_ERR_OR_NULL(ptr: psy)) { |
562 | devres_free(res: ptr); |
563 | } else { |
564 | *ptr = psy; |
565 | devres_add(dev, res: ptr); |
566 | } |
567 | return psy; |
568 | } |
569 | EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle); |
570 | #endif /* CONFIG_OF */ |
571 | |
572 | int power_supply_get_battery_info(struct power_supply *psy, |
573 | struct power_supply_battery_info **info_out) |
574 | { |
575 | struct power_supply_resistance_temp_table *resist_table; |
576 | struct power_supply_battery_info *info; |
577 | struct device_node *battery_np = NULL; |
578 | struct fwnode_reference_args args; |
579 | struct fwnode_handle *fwnode = NULL; |
580 | const char *value; |
581 | int err, len, index; |
582 | const __be32 *list; |
583 | u32 min_max[2]; |
584 | |
585 | if (psy->of_node) { |
586 | battery_np = of_parse_phandle(np: psy->of_node, phandle_name: "monitored-battery" , index: 0); |
587 | if (!battery_np) |
588 | return -ENODEV; |
589 | |
590 | fwnode = fwnode_handle_get(of_fwnode_handle(battery_np)); |
591 | } else if (psy->dev.parent) { |
592 | err = fwnode_property_get_reference_args( |
593 | dev_fwnode(psy->dev.parent), |
594 | prop: "monitored-battery" , NULL, nargs: 0, index: 0, args: &args); |
595 | if (err) |
596 | return err; |
597 | |
598 | fwnode = args.fwnode; |
599 | } |
600 | |
601 | if (!fwnode) |
602 | return -ENOENT; |
603 | |
604 | err = fwnode_property_read_string(fwnode, propname: "compatible" , val: &value); |
605 | if (err) |
606 | goto out_put_node; |
607 | |
608 | |
609 | /* Try static batteries first */ |
610 | err = samsung_sdi_battery_get_info(dev: &psy->dev, compatible: value, info: &info); |
611 | if (!err) |
612 | goto out_ret_pointer; |
613 | else if (err == -ENODEV) |
614 | /* |
615 | * Device does not have a static battery. |
616 | * Proceed to look for a simple battery. |
617 | */ |
618 | err = 0; |
619 | |
620 | if (strcmp("simple-battery" , value)) { |
621 | err = -ENODEV; |
622 | goto out_put_node; |
623 | } |
624 | |
625 | info = devm_kzalloc(dev: &psy->dev, size: sizeof(*info), GFP_KERNEL); |
626 | if (!info) { |
627 | err = -ENOMEM; |
628 | goto out_put_node; |
629 | } |
630 | |
631 | info->technology = POWER_SUPPLY_TECHNOLOGY_UNKNOWN; |
632 | info->energy_full_design_uwh = -EINVAL; |
633 | info->charge_full_design_uah = -EINVAL; |
634 | info->voltage_min_design_uv = -EINVAL; |
635 | info->voltage_max_design_uv = -EINVAL; |
636 | info->precharge_current_ua = -EINVAL; |
637 | info->charge_term_current_ua = -EINVAL; |
638 | info->constant_charge_current_max_ua = -EINVAL; |
639 | info->constant_charge_voltage_max_uv = -EINVAL; |
640 | info->tricklecharge_current_ua = -EINVAL; |
641 | info->precharge_voltage_max_uv = -EINVAL; |
642 | info->charge_restart_voltage_uv = -EINVAL; |
643 | info->overvoltage_limit_uv = -EINVAL; |
644 | info->maintenance_charge = NULL; |
645 | info->alert_low_temp_charge_current_ua = -EINVAL; |
646 | info->alert_low_temp_charge_voltage_uv = -EINVAL; |
647 | info->alert_high_temp_charge_current_ua = -EINVAL; |
648 | info->alert_high_temp_charge_voltage_uv = -EINVAL; |
649 | info->temp_ambient_alert_min = INT_MIN; |
650 | info->temp_ambient_alert_max = INT_MAX; |
651 | info->temp_alert_min = INT_MIN; |
652 | info->temp_alert_max = INT_MAX; |
653 | info->temp_min = INT_MIN; |
654 | info->temp_max = INT_MAX; |
655 | info->factory_internal_resistance_uohm = -EINVAL; |
656 | info->resist_table = NULL; |
657 | info->bti_resistance_ohm = -EINVAL; |
658 | info->bti_resistance_tolerance = -EINVAL; |
659 | |
660 | for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) { |
661 | info->ocv_table[index] = NULL; |
662 | info->ocv_temp[index] = -EINVAL; |
663 | info->ocv_table_size[index] = -EINVAL; |
664 | } |
665 | |
666 | /* The property and field names below must correspond to elements |
667 | * in enum power_supply_property. For reasoning, see |
668 | * Documentation/power/power_supply_class.rst. |
669 | */ |
670 | |
671 | if (!fwnode_property_read_string(fwnode, propname: "device-chemistry" , val: &value)) { |
672 | if (!strcmp("nickel-cadmium" , value)) |
673 | info->technology = POWER_SUPPLY_TECHNOLOGY_NiCd; |
674 | else if (!strcmp("nickel-metal-hydride" , value)) |
675 | info->technology = POWER_SUPPLY_TECHNOLOGY_NiMH; |
676 | else if (!strcmp("lithium-ion" , value)) |
677 | /* Imprecise lithium-ion type */ |
678 | info->technology = POWER_SUPPLY_TECHNOLOGY_LION; |
679 | else if (!strcmp("lithium-ion-polymer" , value)) |
680 | info->technology = POWER_SUPPLY_TECHNOLOGY_LIPO; |
681 | else if (!strcmp("lithium-ion-iron-phosphate" , value)) |
682 | info->technology = POWER_SUPPLY_TECHNOLOGY_LiFe; |
683 | else if (!strcmp("lithium-ion-manganese-oxide" , value)) |
684 | info->technology = POWER_SUPPLY_TECHNOLOGY_LiMn; |
685 | else |
686 | dev_warn(&psy->dev, "%s unknown battery type\n" , value); |
687 | } |
688 | |
689 | fwnode_property_read_u32(fwnode, propname: "energy-full-design-microwatt-hours" , |
690 | val: &info->energy_full_design_uwh); |
691 | fwnode_property_read_u32(fwnode, propname: "charge-full-design-microamp-hours" , |
692 | val: &info->charge_full_design_uah); |
693 | fwnode_property_read_u32(fwnode, propname: "voltage-min-design-microvolt" , |
694 | val: &info->voltage_min_design_uv); |
695 | fwnode_property_read_u32(fwnode, propname: "voltage-max-design-microvolt" , |
696 | val: &info->voltage_max_design_uv); |
697 | fwnode_property_read_u32(fwnode, propname: "trickle-charge-current-microamp" , |
698 | val: &info->tricklecharge_current_ua); |
699 | fwnode_property_read_u32(fwnode, propname: "precharge-current-microamp" , |
700 | val: &info->precharge_current_ua); |
701 | fwnode_property_read_u32(fwnode, propname: "precharge-upper-limit-microvolt" , |
702 | val: &info->precharge_voltage_max_uv); |
703 | fwnode_property_read_u32(fwnode, propname: "charge-term-current-microamp" , |
704 | val: &info->charge_term_current_ua); |
705 | fwnode_property_read_u32(fwnode, propname: "re-charge-voltage-microvolt" , |
706 | val: &info->charge_restart_voltage_uv); |
707 | fwnode_property_read_u32(fwnode, propname: "over-voltage-threshold-microvolt" , |
708 | val: &info->overvoltage_limit_uv); |
709 | fwnode_property_read_u32(fwnode, propname: "constant-charge-current-max-microamp" , |
710 | val: &info->constant_charge_current_max_ua); |
711 | fwnode_property_read_u32(fwnode, propname: "constant-charge-voltage-max-microvolt" , |
712 | val: &info->constant_charge_voltage_max_uv); |
713 | fwnode_property_read_u32(fwnode, propname: "factory-internal-resistance-micro-ohms" , |
714 | val: &info->factory_internal_resistance_uohm); |
715 | |
716 | if (!fwnode_property_read_u32_array(fwnode, propname: "ambient-celsius" , |
717 | val: min_max, ARRAY_SIZE(min_max))) { |
718 | info->temp_ambient_alert_min = min_max[0]; |
719 | info->temp_ambient_alert_max = min_max[1]; |
720 | } |
721 | if (!fwnode_property_read_u32_array(fwnode, propname: "alert-celsius" , |
722 | val: min_max, ARRAY_SIZE(min_max))) { |
723 | info->temp_alert_min = min_max[0]; |
724 | info->temp_alert_max = min_max[1]; |
725 | } |
726 | if (!fwnode_property_read_u32_array(fwnode, propname: "operating-range-celsius" , |
727 | val: min_max, ARRAY_SIZE(min_max))) { |
728 | info->temp_min = min_max[0]; |
729 | info->temp_max = min_max[1]; |
730 | } |
731 | |
732 | /* |
733 | * The below code uses raw of-data parsing to parse |
734 | * /schemas/types.yaml#/definitions/uint32-matrix |
735 | * data, so for now this is only support with of. |
736 | */ |
737 | if (!battery_np) |
738 | goto out_ret_pointer; |
739 | |
740 | len = of_property_count_u32_elems(np: battery_np, propname: "ocv-capacity-celsius" ); |
741 | if (len < 0 && len != -EINVAL) { |
742 | err = len; |
743 | goto out_put_node; |
744 | } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) { |
745 | dev_err(&psy->dev, "Too many temperature values\n" ); |
746 | err = -EINVAL; |
747 | goto out_put_node; |
748 | } else if (len > 0) { |
749 | of_property_read_u32_array(np: battery_np, propname: "ocv-capacity-celsius" , |
750 | out_values: info->ocv_temp, sz: len); |
751 | } |
752 | |
753 | for (index = 0; index < len; index++) { |
754 | struct power_supply_battery_ocv_table *table; |
755 | char *propname; |
756 | int i, tab_len, size; |
757 | |
758 | propname = kasprintf(GFP_KERNEL, fmt: "ocv-capacity-table-%d" , index); |
759 | if (!propname) { |
760 | power_supply_put_battery_info(psy, info); |
761 | err = -ENOMEM; |
762 | goto out_put_node; |
763 | } |
764 | list = of_get_property(node: battery_np, name: propname, lenp: &size); |
765 | if (!list || !size) { |
766 | dev_err(&psy->dev, "failed to get %s\n" , propname); |
767 | kfree(objp: propname); |
768 | power_supply_put_battery_info(psy, info); |
769 | err = -EINVAL; |
770 | goto out_put_node; |
771 | } |
772 | |
773 | kfree(objp: propname); |
774 | tab_len = size / (2 * sizeof(__be32)); |
775 | info->ocv_table_size[index] = tab_len; |
776 | |
777 | table = info->ocv_table[index] = |
778 | devm_kcalloc(dev: &psy->dev, n: tab_len, size: sizeof(*table), GFP_KERNEL); |
779 | if (!info->ocv_table[index]) { |
780 | power_supply_put_battery_info(psy, info); |
781 | err = -ENOMEM; |
782 | goto out_put_node; |
783 | } |
784 | |
785 | for (i = 0; i < tab_len; i++) { |
786 | table[i].ocv = be32_to_cpu(*list); |
787 | list++; |
788 | table[i].capacity = be32_to_cpu(*list); |
789 | list++; |
790 | } |
791 | } |
792 | |
793 | list = of_get_property(node: battery_np, name: "resistance-temp-table" , lenp: &len); |
794 | if (!list || !len) |
795 | goto out_ret_pointer; |
796 | |
797 | info->resist_table_size = len / (2 * sizeof(__be32)); |
798 | resist_table = info->resist_table = devm_kcalloc(dev: &psy->dev, |
799 | n: info->resist_table_size, |
800 | size: sizeof(*resist_table), |
801 | GFP_KERNEL); |
802 | if (!info->resist_table) { |
803 | power_supply_put_battery_info(psy, info); |
804 | err = -ENOMEM; |
805 | goto out_put_node; |
806 | } |
807 | |
808 | for (index = 0; index < info->resist_table_size; index++) { |
809 | resist_table[index].temp = be32_to_cpu(*list++); |
810 | resist_table[index].resistance = be32_to_cpu(*list++); |
811 | } |
812 | |
813 | out_ret_pointer: |
814 | /* Finally return the whole thing */ |
815 | *info_out = info; |
816 | |
817 | out_put_node: |
818 | fwnode_handle_put(fwnode); |
819 | of_node_put(node: battery_np); |
820 | return err; |
821 | } |
822 | EXPORT_SYMBOL_GPL(power_supply_get_battery_info); |
823 | |
824 | void power_supply_put_battery_info(struct power_supply *psy, |
825 | struct power_supply_battery_info *info) |
826 | { |
827 | int i; |
828 | |
829 | for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) { |
830 | if (info->ocv_table[i]) |
831 | devm_kfree(dev: &psy->dev, p: info->ocv_table[i]); |
832 | } |
833 | |
834 | if (info->resist_table) |
835 | devm_kfree(dev: &psy->dev, p: info->resist_table); |
836 | |
837 | devm_kfree(dev: &psy->dev, p: info); |
838 | } |
839 | EXPORT_SYMBOL_GPL(power_supply_put_battery_info); |
840 | |
841 | const enum power_supply_property power_supply_battery_info_properties[] = { |
842 | POWER_SUPPLY_PROP_TECHNOLOGY, |
843 | POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, |
844 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
845 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, |
846 | POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, |
847 | POWER_SUPPLY_PROP_PRECHARGE_CURRENT, |
848 | POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, |
849 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, |
850 | POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, |
851 | POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN, |
852 | POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX, |
853 | POWER_SUPPLY_PROP_TEMP_ALERT_MIN, |
854 | POWER_SUPPLY_PROP_TEMP_ALERT_MAX, |
855 | POWER_SUPPLY_PROP_TEMP_MIN, |
856 | POWER_SUPPLY_PROP_TEMP_MAX, |
857 | }; |
858 | EXPORT_SYMBOL_GPL(power_supply_battery_info_properties); |
859 | |
860 | const size_t power_supply_battery_info_properties_size = ARRAY_SIZE(power_supply_battery_info_properties); |
861 | EXPORT_SYMBOL_GPL(power_supply_battery_info_properties_size); |
862 | |
863 | bool power_supply_battery_info_has_prop(struct power_supply_battery_info *info, |
864 | enum power_supply_property psp) |
865 | { |
866 | if (!info) |
867 | return false; |
868 | |
869 | switch (psp) { |
870 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
871 | return info->technology != POWER_SUPPLY_TECHNOLOGY_UNKNOWN; |
872 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
873 | return info->energy_full_design_uwh >= 0; |
874 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
875 | return info->charge_full_design_uah >= 0; |
876 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
877 | return info->voltage_min_design_uv >= 0; |
878 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
879 | return info->voltage_max_design_uv >= 0; |
880 | case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: |
881 | return info->precharge_current_ua >= 0; |
882 | case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: |
883 | return info->charge_term_current_ua >= 0; |
884 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
885 | return info->constant_charge_current_max_ua >= 0; |
886 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: |
887 | return info->constant_charge_voltage_max_uv >= 0; |
888 | case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN: |
889 | return info->temp_ambient_alert_min > INT_MIN; |
890 | case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX: |
891 | return info->temp_ambient_alert_max < INT_MAX; |
892 | case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
893 | return info->temp_alert_min > INT_MIN; |
894 | case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
895 | return info->temp_alert_max < INT_MAX; |
896 | case POWER_SUPPLY_PROP_TEMP_MIN: |
897 | return info->temp_min > INT_MIN; |
898 | case POWER_SUPPLY_PROP_TEMP_MAX: |
899 | return info->temp_max < INT_MAX; |
900 | default: |
901 | return false; |
902 | } |
903 | } |
904 | EXPORT_SYMBOL_GPL(power_supply_battery_info_has_prop); |
905 | |
906 | int power_supply_battery_info_get_prop(struct power_supply_battery_info *info, |
907 | enum power_supply_property psp, |
908 | union power_supply_propval *val) |
909 | { |
910 | if (!info) |
911 | return -EINVAL; |
912 | |
913 | if (!power_supply_battery_info_has_prop(info, psp)) |
914 | return -EINVAL; |
915 | |
916 | switch (psp) { |
917 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
918 | val->intval = info->technology; |
919 | return 0; |
920 | case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN: |
921 | val->intval = info->energy_full_design_uwh; |
922 | return 0; |
923 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
924 | val->intval = info->charge_full_design_uah; |
925 | return 0; |
926 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: |
927 | val->intval = info->voltage_min_design_uv; |
928 | return 0; |
929 | case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN: |
930 | val->intval = info->voltage_max_design_uv; |
931 | return 0; |
932 | case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: |
933 | val->intval = info->precharge_current_ua; |
934 | return 0; |
935 | case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: |
936 | val->intval = info->charge_term_current_ua; |
937 | return 0; |
938 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: |
939 | val->intval = info->constant_charge_current_max_ua; |
940 | return 0; |
941 | case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: |
942 | val->intval = info->constant_charge_voltage_max_uv; |
943 | return 0; |
944 | case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN: |
945 | val->intval = info->temp_ambient_alert_min; |
946 | return 0; |
947 | case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX: |
948 | val->intval = info->temp_ambient_alert_max; |
949 | return 0; |
950 | case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: |
951 | val->intval = info->temp_alert_min; |
952 | return 0; |
953 | case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: |
954 | val->intval = info->temp_alert_max; |
955 | return 0; |
956 | case POWER_SUPPLY_PROP_TEMP_MIN: |
957 | val->intval = info->temp_min; |
958 | return 0; |
959 | case POWER_SUPPLY_PROP_TEMP_MAX: |
960 | val->intval = info->temp_max; |
961 | return 0; |
962 | default: |
963 | return -EINVAL; |
964 | } |
965 | } |
966 | EXPORT_SYMBOL_GPL(power_supply_battery_info_get_prop); |
967 | |
968 | /** |
969 | * power_supply_temp2resist_simple() - find the battery internal resistance |
970 | * percent from temperature |
971 | * @table: Pointer to battery resistance temperature table |
972 | * @table_len: The table length |
973 | * @temp: Current temperature |
974 | * |
975 | * This helper function is used to look up battery internal resistance percent |
976 | * according to current temperature value from the resistance temperature table, |
977 | * and the table must be ordered descending. Then the actual battery internal |
978 | * resistance = the ideal battery internal resistance * percent / 100. |
979 | * |
980 | * Return: the battery internal resistance percent |
981 | */ |
982 | int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table, |
983 | int table_len, int temp) |
984 | { |
985 | int i, high, low; |
986 | |
987 | for (i = 0; i < table_len; i++) |
988 | if (temp > table[i].temp) |
989 | break; |
990 | |
991 | /* The library function will deal with high == low */ |
992 | if (i == 0) |
993 | high = low = i; |
994 | else if (i == table_len) |
995 | high = low = i - 1; |
996 | else |
997 | high = (low = i) - 1; |
998 | |
999 | return fixp_linear_interpolate(x0: table[low].temp, |
1000 | y0: table[low].resistance, |
1001 | x1: table[high].temp, |
1002 | y1: table[high].resistance, |
1003 | x: temp); |
1004 | } |
1005 | EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple); |
1006 | |
1007 | /** |
1008 | * power_supply_vbat2ri() - find the battery internal resistance |
1009 | * from the battery voltage |
1010 | * @info: The battery information container |
1011 | * @vbat_uv: The battery voltage in microvolt |
1012 | * @charging: If we are charging (true) or not (false) |
1013 | * |
1014 | * This helper function is used to look up battery internal resistance |
1015 | * according to current battery voltage. Depending on whether the battery |
1016 | * is currently charging or not, different resistance will be returned. |
1017 | * |
1018 | * Returns the internal resistance in microohm or negative error code. |
1019 | */ |
1020 | int power_supply_vbat2ri(struct power_supply_battery_info *info, |
1021 | int vbat_uv, bool charging) |
1022 | { |
1023 | struct power_supply_vbat_ri_table *vbat2ri; |
1024 | int table_len; |
1025 | int i, high, low; |
1026 | |
1027 | /* |
1028 | * If we are charging, and the battery supplies a separate table |
1029 | * for this state, we use that in order to compensate for the |
1030 | * charging voltage. Otherwise we use the main table. |
1031 | */ |
1032 | if (charging && info->vbat2ri_charging) { |
1033 | vbat2ri = info->vbat2ri_charging; |
1034 | table_len = info->vbat2ri_charging_size; |
1035 | } else { |
1036 | vbat2ri = info->vbat2ri_discharging; |
1037 | table_len = info->vbat2ri_discharging_size; |
1038 | } |
1039 | |
1040 | /* |
1041 | * If no tables are specified, or if we are above the highest voltage in |
1042 | * the voltage table, just return the factory specified internal resistance. |
1043 | */ |
1044 | if (!vbat2ri || (table_len <= 0) || (vbat_uv > vbat2ri[0].vbat_uv)) { |
1045 | if (charging && (info->factory_internal_resistance_charging_uohm > 0)) |
1046 | return info->factory_internal_resistance_charging_uohm; |
1047 | else |
1048 | return info->factory_internal_resistance_uohm; |
1049 | } |
1050 | |
1051 | /* Break loop at table_len - 1 because that is the highest index */ |
1052 | for (i = 0; i < table_len - 1; i++) |
1053 | if (vbat_uv > vbat2ri[i].vbat_uv) |
1054 | break; |
1055 | |
1056 | /* The library function will deal with high == low */ |
1057 | if ((i == 0) || (i == (table_len - 1))) |
1058 | high = i; |
1059 | else |
1060 | high = i - 1; |
1061 | low = i; |
1062 | |
1063 | return fixp_linear_interpolate(x0: vbat2ri[low].vbat_uv, |
1064 | y0: vbat2ri[low].ri_uohm, |
1065 | x1: vbat2ri[high].vbat_uv, |
1066 | y1: vbat2ri[high].ri_uohm, |
1067 | x: vbat_uv); |
1068 | } |
1069 | EXPORT_SYMBOL_GPL(power_supply_vbat2ri); |
1070 | |
1071 | struct power_supply_maintenance_charge_table * |
1072 | power_supply_get_maintenance_charging_setting(struct power_supply_battery_info *info, |
1073 | int index) |
1074 | { |
1075 | if (index >= info->maintenance_charge_size) |
1076 | return NULL; |
1077 | return &info->maintenance_charge[index]; |
1078 | } |
1079 | EXPORT_SYMBOL_GPL(power_supply_get_maintenance_charging_setting); |
1080 | |
1081 | /** |
1082 | * power_supply_ocv2cap_simple() - find the battery capacity |
1083 | * @table: Pointer to battery OCV lookup table |
1084 | * @table_len: OCV table length |
1085 | * @ocv: Current OCV value |
1086 | * |
1087 | * This helper function is used to look up battery capacity according to |
1088 | * current OCV value from one OCV table, and the OCV table must be ordered |
1089 | * descending. |
1090 | * |
1091 | * Return: the battery capacity. |
1092 | */ |
1093 | int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table, |
1094 | int table_len, int ocv) |
1095 | { |
1096 | int i, high, low; |
1097 | |
1098 | for (i = 0; i < table_len; i++) |
1099 | if (ocv > table[i].ocv) |
1100 | break; |
1101 | |
1102 | /* The library function will deal with high == low */ |
1103 | if (i == 0) |
1104 | high = low = i; |
1105 | else if (i == table_len) |
1106 | high = low = i - 1; |
1107 | else |
1108 | high = (low = i) - 1; |
1109 | |
1110 | return fixp_linear_interpolate(x0: table[low].ocv, |
1111 | y0: table[low].capacity, |
1112 | x1: table[high].ocv, |
1113 | y1: table[high].capacity, |
1114 | x: ocv); |
1115 | } |
1116 | EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple); |
1117 | |
1118 | struct power_supply_battery_ocv_table * |
1119 | power_supply_find_ocv2cap_table(struct power_supply_battery_info *info, |
1120 | int temp, int *table_len) |
1121 | { |
1122 | int best_temp_diff = INT_MAX, temp_diff; |
1123 | u8 i, best_index = 0; |
1124 | |
1125 | if (!info->ocv_table[0]) |
1126 | return NULL; |
1127 | |
1128 | for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) { |
1129 | /* Out of capacity tables */ |
1130 | if (!info->ocv_table[i]) |
1131 | break; |
1132 | |
1133 | temp_diff = abs(info->ocv_temp[i] - temp); |
1134 | |
1135 | if (temp_diff < best_temp_diff) { |
1136 | best_temp_diff = temp_diff; |
1137 | best_index = i; |
1138 | } |
1139 | } |
1140 | |
1141 | *table_len = info->ocv_table_size[best_index]; |
1142 | return info->ocv_table[best_index]; |
1143 | } |
1144 | EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table); |
1145 | |
1146 | int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info, |
1147 | int ocv, int temp) |
1148 | { |
1149 | struct power_supply_battery_ocv_table *table; |
1150 | int table_len; |
1151 | |
1152 | table = power_supply_find_ocv2cap_table(info, temp, &table_len); |
1153 | if (!table) |
1154 | return -EINVAL; |
1155 | |
1156 | return power_supply_ocv2cap_simple(table, table_len, ocv); |
1157 | } |
1158 | EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap); |
1159 | |
1160 | bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info, |
1161 | int resistance) |
1162 | { |
1163 | int low, high; |
1164 | |
1165 | /* Nothing like this can be checked */ |
1166 | if (info->bti_resistance_ohm <= 0) |
1167 | return false; |
1168 | |
1169 | /* This will be extremely strict and unlikely to work */ |
1170 | if (info->bti_resistance_tolerance <= 0) |
1171 | return (info->bti_resistance_ohm == resistance); |
1172 | |
1173 | low = info->bti_resistance_ohm - |
1174 | (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100; |
1175 | high = info->bti_resistance_ohm + |
1176 | (info->bti_resistance_ohm * info->bti_resistance_tolerance) / 100; |
1177 | |
1178 | return ((resistance >= low) && (resistance <= high)); |
1179 | } |
1180 | EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range); |
1181 | |
1182 | static bool psy_has_property(const struct power_supply_desc *psy_desc, |
1183 | enum power_supply_property psp) |
1184 | { |
1185 | bool found = false; |
1186 | int i; |
1187 | |
1188 | for (i = 0; i < psy_desc->num_properties; i++) { |
1189 | if (psy_desc->properties[i] == psp) { |
1190 | found = true; |
1191 | break; |
1192 | } |
1193 | } |
1194 | |
1195 | return found; |
1196 | } |
1197 | |
1198 | int power_supply_get_property(struct power_supply *psy, |
1199 | enum power_supply_property psp, |
1200 | union power_supply_propval *val) |
1201 | { |
1202 | if (atomic_read(v: &psy->use_cnt) <= 0) { |
1203 | if (!psy->initialized) |
1204 | return -EAGAIN; |
1205 | return -ENODEV; |
1206 | } |
1207 | |
1208 | if (psy_has_property(psy_desc: psy->desc, psp)) |
1209 | return psy->desc->get_property(psy, psp, val); |
1210 | else if (power_supply_battery_info_has_prop(psy->battery_info, psp)) |
1211 | return power_supply_battery_info_get_prop(psy->battery_info, psp, val); |
1212 | else |
1213 | return -EINVAL; |
1214 | } |
1215 | EXPORT_SYMBOL_GPL(power_supply_get_property); |
1216 | |
1217 | int power_supply_set_property(struct power_supply *psy, |
1218 | enum power_supply_property psp, |
1219 | const union power_supply_propval *val) |
1220 | { |
1221 | if (atomic_read(v: &psy->use_cnt) <= 0 || !psy->desc->set_property) |
1222 | return -ENODEV; |
1223 | |
1224 | return psy->desc->set_property(psy, psp, val); |
1225 | } |
1226 | EXPORT_SYMBOL_GPL(power_supply_set_property); |
1227 | |
1228 | int power_supply_property_is_writeable(struct power_supply *psy, |
1229 | enum power_supply_property psp) |
1230 | { |
1231 | if (atomic_read(v: &psy->use_cnt) <= 0 || |
1232 | !psy->desc->property_is_writeable) |
1233 | return -ENODEV; |
1234 | |
1235 | return psy->desc->property_is_writeable(psy, psp); |
1236 | } |
1237 | EXPORT_SYMBOL_GPL(power_supply_property_is_writeable); |
1238 | |
1239 | void power_supply_external_power_changed(struct power_supply *psy) |
1240 | { |
1241 | if (atomic_read(v: &psy->use_cnt) <= 0 || |
1242 | !psy->desc->external_power_changed) |
1243 | return; |
1244 | |
1245 | psy->desc->external_power_changed(psy); |
1246 | } |
1247 | EXPORT_SYMBOL_GPL(power_supply_external_power_changed); |
1248 | |
1249 | int power_supply_powers(struct power_supply *psy, struct device *dev) |
1250 | { |
1251 | return sysfs_create_link(kobj: &psy->dev.kobj, target: &dev->kobj, name: "powers" ); |
1252 | } |
1253 | EXPORT_SYMBOL_GPL(power_supply_powers); |
1254 | |
1255 | static void power_supply_dev_release(struct device *dev) |
1256 | { |
1257 | struct power_supply *psy = to_power_supply(dev); |
1258 | dev_dbg(dev, "%s\n" , __func__); |
1259 | kfree(objp: psy); |
1260 | } |
1261 | |
1262 | int power_supply_reg_notifier(struct notifier_block *nb) |
1263 | { |
1264 | return blocking_notifier_chain_register(nh: &power_supply_notifier, nb); |
1265 | } |
1266 | EXPORT_SYMBOL_GPL(power_supply_reg_notifier); |
1267 | |
1268 | void power_supply_unreg_notifier(struct notifier_block *nb) |
1269 | { |
1270 | blocking_notifier_chain_unregister(nh: &power_supply_notifier, nb); |
1271 | } |
1272 | EXPORT_SYMBOL_GPL(power_supply_unreg_notifier); |
1273 | |
1274 | #ifdef CONFIG_THERMAL |
1275 | static int power_supply_read_temp(struct thermal_zone_device *tzd, |
1276 | int *temp) |
1277 | { |
1278 | struct power_supply *psy; |
1279 | union power_supply_propval val; |
1280 | int ret; |
1281 | |
1282 | WARN_ON(tzd == NULL); |
1283 | psy = thermal_zone_device_priv(tzd); |
1284 | ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val); |
1285 | if (ret) |
1286 | return ret; |
1287 | |
1288 | /* Convert tenths of degree Celsius to milli degree Celsius. */ |
1289 | *temp = val.intval * 100; |
1290 | |
1291 | return ret; |
1292 | } |
1293 | |
1294 | static struct thermal_zone_device_ops psy_tzd_ops = { |
1295 | .get_temp = power_supply_read_temp, |
1296 | }; |
1297 | |
1298 | static int psy_register_thermal(struct power_supply *psy) |
1299 | { |
1300 | int ret; |
1301 | |
1302 | if (psy->desc->no_thermal) |
1303 | return 0; |
1304 | |
1305 | /* Register battery zone device psy reports temperature */ |
1306 | if (psy_has_property(psy_desc: psy->desc, psp: POWER_SUPPLY_PROP_TEMP)) { |
1307 | /* Prefer our hwmon device and avoid duplicates */ |
1308 | struct thermal_zone_params tzp = { |
1309 | .no_hwmon = IS_ENABLED(CONFIG_POWER_SUPPLY_HWMON) |
1310 | }; |
1311 | psy->tzd = thermal_tripless_zone_device_register(type: psy->desc->name, |
1312 | devdata: psy, ops: &psy_tzd_ops, tzp: &tzp); |
1313 | if (IS_ERR(ptr: psy->tzd)) |
1314 | return PTR_ERR(ptr: psy->tzd); |
1315 | ret = thermal_zone_device_enable(tz: psy->tzd); |
1316 | if (ret) |
1317 | thermal_zone_device_unregister(tz: psy->tzd); |
1318 | return ret; |
1319 | } |
1320 | |
1321 | return 0; |
1322 | } |
1323 | |
1324 | static void psy_unregister_thermal(struct power_supply *psy) |
1325 | { |
1326 | if (IS_ERR_OR_NULL(ptr: psy->tzd)) |
1327 | return; |
1328 | thermal_zone_device_unregister(tz: psy->tzd); |
1329 | } |
1330 | |
1331 | #else |
1332 | static int psy_register_thermal(struct power_supply *psy) |
1333 | { |
1334 | return 0; |
1335 | } |
1336 | |
1337 | static void psy_unregister_thermal(struct power_supply *psy) |
1338 | { |
1339 | } |
1340 | #endif |
1341 | |
1342 | static struct power_supply *__must_check |
1343 | __power_supply_register(struct device *parent, |
1344 | const struct power_supply_desc *desc, |
1345 | const struct power_supply_config *cfg, |
1346 | bool ws) |
1347 | { |
1348 | struct device *dev; |
1349 | struct power_supply *psy; |
1350 | int rc; |
1351 | |
1352 | if (!desc || !desc->name || !desc->properties || !desc->num_properties) |
1353 | return ERR_PTR(error: -EINVAL); |
1354 | |
1355 | if (!parent) |
1356 | pr_warn("%s: Expected proper parent device for '%s'\n" , |
1357 | __func__, desc->name); |
1358 | |
1359 | if (psy_has_property(psy_desc: desc, psp: POWER_SUPPLY_PROP_USB_TYPE) && |
1360 | (!desc->usb_types || !desc->num_usb_types)) |
1361 | return ERR_PTR(error: -EINVAL); |
1362 | |
1363 | psy = kzalloc(size: sizeof(*psy), GFP_KERNEL); |
1364 | if (!psy) |
1365 | return ERR_PTR(error: -ENOMEM); |
1366 | |
1367 | dev = &psy->dev; |
1368 | |
1369 | device_initialize(dev); |
1370 | |
1371 | dev->class = power_supply_class; |
1372 | dev->type = &power_supply_dev_type; |
1373 | dev->parent = parent; |
1374 | dev->release = power_supply_dev_release; |
1375 | dev_set_drvdata(dev, data: psy); |
1376 | psy->desc = desc; |
1377 | if (cfg) { |
1378 | dev->groups = cfg->attr_grp; |
1379 | psy->drv_data = cfg->drv_data; |
1380 | psy->of_node = |
1381 | cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node; |
1382 | dev->of_node = psy->of_node; |
1383 | psy->supplied_to = cfg->supplied_to; |
1384 | psy->num_supplicants = cfg->num_supplicants; |
1385 | } |
1386 | |
1387 | rc = dev_set_name(dev, name: "%s" , desc->name); |
1388 | if (rc) |
1389 | goto dev_set_name_failed; |
1390 | |
1391 | INIT_WORK(&psy->changed_work, power_supply_changed_work); |
1392 | INIT_DELAYED_WORK(&psy->deferred_register_work, |
1393 | power_supply_deferred_register_work); |
1394 | |
1395 | rc = power_supply_check_supplies(psy); |
1396 | if (rc) { |
1397 | dev_dbg(dev, "Not all required supplies found, defer probe\n" ); |
1398 | goto check_supplies_failed; |
1399 | } |
1400 | |
1401 | /* |
1402 | * Expose constant battery info, if it is available. While there are |
1403 | * some chargers accessing constant battery data, we only want to |
1404 | * expose battery data to userspace for battery devices. |
1405 | */ |
1406 | if (desc->type == POWER_SUPPLY_TYPE_BATTERY) { |
1407 | rc = power_supply_get_battery_info(psy, &psy->battery_info); |
1408 | if (rc && rc != -ENODEV && rc != -ENOENT) |
1409 | goto check_supplies_failed; |
1410 | } |
1411 | |
1412 | spin_lock_init(&psy->changed_lock); |
1413 | rc = device_add(dev); |
1414 | if (rc) |
1415 | goto device_add_failed; |
1416 | |
1417 | rc = device_init_wakeup(dev, enable: ws); |
1418 | if (rc) |
1419 | goto wakeup_init_failed; |
1420 | |
1421 | rc = psy_register_thermal(psy); |
1422 | if (rc) |
1423 | goto register_thermal_failed; |
1424 | |
1425 | rc = power_supply_create_triggers(psy); |
1426 | if (rc) |
1427 | goto create_triggers_failed; |
1428 | |
1429 | rc = power_supply_add_hwmon_sysfs(psy); |
1430 | if (rc) |
1431 | goto add_hwmon_sysfs_failed; |
1432 | |
1433 | /* |
1434 | * Update use_cnt after any uevents (most notably from device_add()). |
1435 | * We are here still during driver's probe but |
1436 | * the power_supply_uevent() calls back driver's get_property |
1437 | * method so: |
1438 | * 1. Driver did not assigned the returned struct power_supply, |
1439 | * 2. Driver could not finish initialization (anything in its probe |
1440 | * after calling power_supply_register()). |
1441 | */ |
1442 | atomic_inc(v: &psy->use_cnt); |
1443 | psy->initialized = true; |
1444 | |
1445 | queue_delayed_work(wq: system_power_efficient_wq, |
1446 | dwork: &psy->deferred_register_work, |
1447 | POWER_SUPPLY_DEFERRED_REGISTER_TIME); |
1448 | |
1449 | return psy; |
1450 | |
1451 | add_hwmon_sysfs_failed: |
1452 | power_supply_remove_triggers(psy); |
1453 | create_triggers_failed: |
1454 | psy_unregister_thermal(psy); |
1455 | register_thermal_failed: |
1456 | wakeup_init_failed: |
1457 | device_del(dev); |
1458 | device_add_failed: |
1459 | check_supplies_failed: |
1460 | dev_set_name_failed: |
1461 | put_device(dev); |
1462 | return ERR_PTR(error: rc); |
1463 | } |
1464 | |
1465 | /** |
1466 | * power_supply_register() - Register new power supply |
1467 | * @parent: Device to be a parent of power supply's device, usually |
1468 | * the device which probe function calls this |
1469 | * @desc: Description of power supply, must be valid through whole |
1470 | * lifetime of this power supply |
1471 | * @cfg: Run-time specific configuration accessed during registering, |
1472 | * may be NULL |
1473 | * |
1474 | * Return: A pointer to newly allocated power_supply on success |
1475 | * or ERR_PTR otherwise. |
1476 | * Use power_supply_unregister() on returned power_supply pointer to release |
1477 | * resources. |
1478 | */ |
1479 | struct power_supply *__must_check power_supply_register(struct device *parent, |
1480 | const struct power_supply_desc *desc, |
1481 | const struct power_supply_config *cfg) |
1482 | { |
1483 | return __power_supply_register(parent, desc, cfg, ws: true); |
1484 | } |
1485 | EXPORT_SYMBOL_GPL(power_supply_register); |
1486 | |
1487 | /** |
1488 | * power_supply_register_no_ws() - Register new non-waking-source power supply |
1489 | * @parent: Device to be a parent of power supply's device, usually |
1490 | * the device which probe function calls this |
1491 | * @desc: Description of power supply, must be valid through whole |
1492 | * lifetime of this power supply |
1493 | * @cfg: Run-time specific configuration accessed during registering, |
1494 | * may be NULL |
1495 | * |
1496 | * Return: A pointer to newly allocated power_supply on success |
1497 | * or ERR_PTR otherwise. |
1498 | * Use power_supply_unregister() on returned power_supply pointer to release |
1499 | * resources. |
1500 | */ |
1501 | struct power_supply *__must_check |
1502 | power_supply_register_no_ws(struct device *parent, |
1503 | const struct power_supply_desc *desc, |
1504 | const struct power_supply_config *cfg) |
1505 | { |
1506 | return __power_supply_register(parent, desc, cfg, ws: false); |
1507 | } |
1508 | EXPORT_SYMBOL_GPL(power_supply_register_no_ws); |
1509 | |
1510 | static void devm_power_supply_release(struct device *dev, void *res) |
1511 | { |
1512 | struct power_supply **psy = res; |
1513 | |
1514 | power_supply_unregister(psy: *psy); |
1515 | } |
1516 | |
1517 | /** |
1518 | * devm_power_supply_register() - Register managed power supply |
1519 | * @parent: Device to be a parent of power supply's device, usually |
1520 | * the device which probe function calls this |
1521 | * @desc: Description of power supply, must be valid through whole |
1522 | * lifetime of this power supply |
1523 | * @cfg: Run-time specific configuration accessed during registering, |
1524 | * may be NULL |
1525 | * |
1526 | * Return: A pointer to newly allocated power_supply on success |
1527 | * or ERR_PTR otherwise. |
1528 | * The returned power_supply pointer will be automatically unregistered |
1529 | * on driver detach. |
1530 | */ |
1531 | struct power_supply *__must_check |
1532 | devm_power_supply_register(struct device *parent, |
1533 | const struct power_supply_desc *desc, |
1534 | const struct power_supply_config *cfg) |
1535 | { |
1536 | struct power_supply **ptr, *psy; |
1537 | |
1538 | ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL); |
1539 | |
1540 | if (!ptr) |
1541 | return ERR_PTR(error: -ENOMEM); |
1542 | psy = __power_supply_register(parent, desc, cfg, ws: true); |
1543 | if (IS_ERR(ptr: psy)) { |
1544 | devres_free(res: ptr); |
1545 | } else { |
1546 | *ptr = psy; |
1547 | devres_add(dev: parent, res: ptr); |
1548 | } |
1549 | return psy; |
1550 | } |
1551 | EXPORT_SYMBOL_GPL(devm_power_supply_register); |
1552 | |
1553 | /** |
1554 | * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply |
1555 | * @parent: Device to be a parent of power supply's device, usually |
1556 | * the device which probe function calls this |
1557 | * @desc: Description of power supply, must be valid through whole |
1558 | * lifetime of this power supply |
1559 | * @cfg: Run-time specific configuration accessed during registering, |
1560 | * may be NULL |
1561 | * |
1562 | * Return: A pointer to newly allocated power_supply on success |
1563 | * or ERR_PTR otherwise. |
1564 | * The returned power_supply pointer will be automatically unregistered |
1565 | * on driver detach. |
1566 | */ |
1567 | struct power_supply *__must_check |
1568 | devm_power_supply_register_no_ws(struct device *parent, |
1569 | const struct power_supply_desc *desc, |
1570 | const struct power_supply_config *cfg) |
1571 | { |
1572 | struct power_supply **ptr, *psy; |
1573 | |
1574 | ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL); |
1575 | |
1576 | if (!ptr) |
1577 | return ERR_PTR(error: -ENOMEM); |
1578 | psy = __power_supply_register(parent, desc, cfg, ws: false); |
1579 | if (IS_ERR(ptr: psy)) { |
1580 | devres_free(res: ptr); |
1581 | } else { |
1582 | *ptr = psy; |
1583 | devres_add(dev: parent, res: ptr); |
1584 | } |
1585 | return psy; |
1586 | } |
1587 | EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws); |
1588 | |
1589 | /** |
1590 | * power_supply_unregister() - Remove this power supply from system |
1591 | * @psy: Pointer to power supply to unregister |
1592 | * |
1593 | * Remove this power supply from the system. The resources of power supply |
1594 | * will be freed here or on last power_supply_put() call. |
1595 | */ |
1596 | void power_supply_unregister(struct power_supply *psy) |
1597 | { |
1598 | WARN_ON(atomic_dec_return(&psy->use_cnt)); |
1599 | psy->removing = true; |
1600 | cancel_work_sync(work: &psy->changed_work); |
1601 | cancel_delayed_work_sync(dwork: &psy->deferred_register_work); |
1602 | sysfs_remove_link(kobj: &psy->dev.kobj, name: "powers" ); |
1603 | power_supply_remove_hwmon_sysfs(psy); |
1604 | power_supply_remove_triggers(psy); |
1605 | psy_unregister_thermal(psy); |
1606 | device_init_wakeup(dev: &psy->dev, enable: false); |
1607 | device_unregister(dev: &psy->dev); |
1608 | } |
1609 | EXPORT_SYMBOL_GPL(power_supply_unregister); |
1610 | |
1611 | void *power_supply_get_drvdata(struct power_supply *psy) |
1612 | { |
1613 | return psy->drv_data; |
1614 | } |
1615 | EXPORT_SYMBOL_GPL(power_supply_get_drvdata); |
1616 | |
1617 | static int __init power_supply_class_init(void) |
1618 | { |
1619 | power_supply_class = class_create(name: "power_supply" ); |
1620 | |
1621 | if (IS_ERR(ptr: power_supply_class)) |
1622 | return PTR_ERR(ptr: power_supply_class); |
1623 | |
1624 | power_supply_class->dev_uevent = power_supply_uevent; |
1625 | power_supply_init_attrs(dev_type: &power_supply_dev_type); |
1626 | |
1627 | return 0; |
1628 | } |
1629 | |
1630 | static void __exit power_supply_class_exit(void) |
1631 | { |
1632 | class_destroy(cls: power_supply_class); |
1633 | } |
1634 | |
1635 | subsys_initcall(power_supply_class_init); |
1636 | module_exit(power_supply_class_exit); |
1637 | |
1638 | MODULE_DESCRIPTION("Universal power supply monitor class" ); |
1639 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " |
1640 | "Szabolcs Gyurko, " |
1641 | "Anton Vorontsov <cbou@mail.ru>" ); |
1642 | |