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