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