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