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