1 /*
2 * Universal power supply monitor class
3 *
4 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
5 * Copyright © 2004 Szabolcs Gyurko
6 * Copyright © 2003 Ian Molton <spyro@f2s.com>
7 *
8 * Modified: 2004, Oct Szabolcs Gyurko
9 *
10 * You may use this code as per GPL version 2
11 */
12
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/notifier.h>
20 #include <linux/err.h>
21 #include <linux/of.h>
22 #include <linux/power_supply.h>
23 #include <linux/property.h>
24 #include <linux/thermal.h>
25 #include "power_supply.h"
26
27 /* exported for the APM Power driver, APM emulation */
28 struct class *power_supply_class;
29 EXPORT_SYMBOL_GPL(power_supply_class);
30
31 ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
32 EXPORT_SYMBOL_GPL(power_supply_notifier);
33
34 static struct device_type power_supply_dev_type;
35
36 #define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
37
__power_supply_is_supplied_by(struct power_supply * supplier,struct power_supply * supply)38 static bool __power_supply_is_supplied_by(struct power_supply *supplier,
39 struct power_supply *supply)
40 {
41 int i;
42
43 if (!supply->supplied_from && !supplier->supplied_to)
44 return false;
45
46 /* Support both supplied_to and supplied_from modes */
47 if (supply->supplied_from) {
48 if (!supplier->desc->name)
49 return false;
50 for (i = 0; i < supply->num_supplies; i++)
51 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
52 return true;
53 } else {
54 if (!supply->desc->name)
55 return false;
56 for (i = 0; i < supplier->num_supplicants; i++)
57 if (!strcmp(supplier->supplied_to[i], supply->desc->name))
58 return true;
59 }
60
61 return false;
62 }
63
__power_supply_changed_work(struct device * dev,void * data)64 static int __power_supply_changed_work(struct device *dev, void *data)
65 {
66 struct power_supply *psy = data;
67 struct power_supply *pst = dev_get_drvdata(dev);
68
69 if (__power_supply_is_supplied_by(psy, pst)) {
70 if (pst->desc->external_power_changed)
71 pst->desc->external_power_changed(pst);
72 }
73
74 return 0;
75 }
76
power_supply_changed_work(struct work_struct * work)77 static void power_supply_changed_work(struct work_struct *work)
78 {
79 unsigned long flags;
80 struct power_supply *psy = container_of(work, struct power_supply,
81 changed_work);
82
83 dev_dbg(&psy->dev, "%s\n", __func__);
84
85 spin_lock_irqsave(&psy->changed_lock, flags);
86 /*
87 * Check 'changed' here to avoid issues due to race between
88 * power_supply_changed() and this routine. In worst case
89 * power_supply_changed() can be called again just before we take above
90 * lock. During the first call of this routine we will mark 'changed' as
91 * false and it will stay false for the next call as well.
92 */
93 if (likely(psy->changed)) {
94 psy->changed = false;
95 spin_unlock_irqrestore(&psy->changed_lock, flags);
96 class_for_each_device(power_supply_class, NULL, psy,
97 __power_supply_changed_work);
98 power_supply_update_leds(psy);
99 atomic_notifier_call_chain(&power_supply_notifier,
100 PSY_EVENT_PROP_CHANGED, psy);
101 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
102 spin_lock_irqsave(&psy->changed_lock, flags);
103 }
104
105 /*
106 * Hold the wakeup_source until all events are processed.
107 * power_supply_changed() might have called again and have set 'changed'
108 * to true.
109 */
110 if (likely(!psy->changed))
111 pm_relax(&psy->dev);
112 spin_unlock_irqrestore(&psy->changed_lock, flags);
113 }
114
power_supply_changed(struct power_supply * psy)115 void power_supply_changed(struct power_supply *psy)
116 {
117 unsigned long flags;
118
119 dev_dbg(&psy->dev, "%s\n", __func__);
120
121 spin_lock_irqsave(&psy->changed_lock, flags);
122 psy->changed = true;
123 pm_stay_awake(&psy->dev);
124 spin_unlock_irqrestore(&psy->changed_lock, flags);
125 schedule_work(&psy->changed_work);
126 }
127 EXPORT_SYMBOL_GPL(power_supply_changed);
128
129 /*
130 * Notify that power supply was registered after parent finished the probing.
131 *
132 * Often power supply is registered from driver's probe function. However
133 * calling power_supply_changed() directly from power_supply_register()
134 * would lead to execution of get_property() function provided by the driver
135 * too early - before the probe ends.
136 *
137 * Avoid that by waiting on parent's mutex.
138 */
power_supply_deferred_register_work(struct work_struct * work)139 static void power_supply_deferred_register_work(struct work_struct *work)
140 {
141 struct power_supply *psy = container_of(work, struct power_supply,
142 deferred_register_work.work);
143
144 if (psy->dev.parent) {
145 while (!mutex_trylock(&psy->dev.parent->mutex)) {
146 if (psy->removing)
147 return;
148 msleep(10);
149 }
150 }
151
152 power_supply_changed(psy);
153
154 if (psy->dev.parent)
155 mutex_unlock(&psy->dev.parent->mutex);
156 }
157
158 #ifdef CONFIG_OF
159 #include <linux/of.h>
160
__power_supply_populate_supplied_from(struct device * dev,void * data)161 static int __power_supply_populate_supplied_from(struct device *dev,
162 void *data)
163 {
164 struct power_supply *psy = data;
165 struct power_supply *epsy = dev_get_drvdata(dev);
166 struct device_node *np;
167 int i = 0;
168
169 do {
170 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
171 if (!np)
172 break;
173
174 if (np == epsy->of_node) {
175 dev_info(&psy->dev, "%s: Found supply : %s\n",
176 psy->desc->name, epsy->desc->name);
177 psy->supplied_from[i-1] = (char *)epsy->desc->name;
178 psy->num_supplies++;
179 of_node_put(np);
180 break;
181 }
182 of_node_put(np);
183 } while (np);
184
185 return 0;
186 }
187
power_supply_populate_supplied_from(struct power_supply * psy)188 static int power_supply_populate_supplied_from(struct power_supply *psy)
189 {
190 int error;
191
192 error = class_for_each_device(power_supply_class, NULL, psy,
193 __power_supply_populate_supplied_from);
194
195 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
196
197 return error;
198 }
199
__power_supply_find_supply_from_node(struct device * dev,void * data)200 static int __power_supply_find_supply_from_node(struct device *dev,
201 void *data)
202 {
203 struct device_node *np = data;
204 struct power_supply *epsy = dev_get_drvdata(dev);
205
206 /* returning non-zero breaks out of class_for_each_device loop */
207 if (epsy->of_node == np)
208 return 1;
209
210 return 0;
211 }
212
power_supply_find_supply_from_node(struct device_node * supply_node)213 static int power_supply_find_supply_from_node(struct device_node *supply_node)
214 {
215 int error;
216
217 /*
218 * class_for_each_device() either returns its own errors or values
219 * returned by __power_supply_find_supply_from_node().
220 *
221 * __power_supply_find_supply_from_node() will return 0 (no match)
222 * or 1 (match).
223 *
224 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
225 * it returned 0, or error as returned by it.
226 */
227 error = class_for_each_device(power_supply_class, NULL, supply_node,
228 __power_supply_find_supply_from_node);
229
230 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
231 }
232
power_supply_check_supplies(struct power_supply * psy)233 static int power_supply_check_supplies(struct power_supply *psy)
234 {
235 struct device_node *np;
236 int cnt = 0;
237
238 /* If there is already a list honor it */
239 if (psy->supplied_from && psy->num_supplies > 0)
240 return 0;
241
242 /* No device node found, nothing to do */
243 if (!psy->of_node)
244 return 0;
245
246 do {
247 int ret;
248
249 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
250 if (!np)
251 break;
252
253 ret = power_supply_find_supply_from_node(np);
254 of_node_put(np);
255
256 if (ret) {
257 dev_dbg(&psy->dev, "Failed to find supply!\n");
258 return ret;
259 }
260 } while (np);
261
262 /* Missing valid "power-supplies" entries */
263 if (cnt == 1)
264 return 0;
265
266 /* All supplies found, allocate char ** array for filling */
267 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
268 GFP_KERNEL);
269 if (!psy->supplied_from)
270 return -ENOMEM;
271
272 *psy->supplied_from = devm_kcalloc(&psy->dev,
273 cnt - 1, sizeof(char *),
274 GFP_KERNEL);
275 if (!*psy->supplied_from)
276 return -ENOMEM;
277
278 return power_supply_populate_supplied_from(psy);
279 }
280 #else
power_supply_check_supplies(struct power_supply * psy)281 static int power_supply_check_supplies(struct power_supply *psy)
282 {
283 int nval, ret;
284
285 if (!psy->dev.parent)
286 return 0;
287
288 nval = device_property_read_string_array(psy->dev.parent,
289 "supplied-from", NULL, 0);
290 if (nval <= 0)
291 return 0;
292
293 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
294 sizeof(char *), GFP_KERNEL);
295 if (!psy->supplied_from)
296 return -ENOMEM;
297
298 ret = device_property_read_string_array(psy->dev.parent,
299 "supplied-from", (const char **)psy->supplied_from, nval);
300 if (ret < 0)
301 return ret;
302
303 psy->num_supplies = nval;
304
305 return 0;
306 }
307 #endif
308
309 struct psy_am_i_supplied_data {
310 struct power_supply *psy;
311 unsigned int count;
312 };
313
__power_supply_am_i_supplied(struct device * dev,void * _data)314 static int __power_supply_am_i_supplied(struct device *dev, void *_data)
315 {
316 union power_supply_propval ret = {0,};
317 struct power_supply *epsy = dev_get_drvdata(dev);
318 struct psy_am_i_supplied_data *data = _data;
319
320 if (__power_supply_is_supplied_by(epsy, data->psy)) {
321 data->count++;
322 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
323 &ret))
324 return ret.intval;
325 }
326
327 return 0;
328 }
329
power_supply_am_i_supplied(struct power_supply * psy)330 int power_supply_am_i_supplied(struct power_supply *psy)
331 {
332 struct psy_am_i_supplied_data data = { psy, 0 };
333 int error;
334
335 error = class_for_each_device(power_supply_class, NULL, &data,
336 __power_supply_am_i_supplied);
337
338 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
339
340 if (data.count == 0)
341 return -ENODEV;
342
343 return error;
344 }
345 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
346
__power_supply_is_system_supplied(struct device * dev,void * data)347 static int __power_supply_is_system_supplied(struct device *dev, void *data)
348 {
349 union power_supply_propval ret = {0,};
350 struct power_supply *psy = dev_get_drvdata(dev);
351 unsigned int *count = data;
352
353 (*count)++;
354 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
355 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
356 &ret))
357 return ret.intval;
358
359 return 0;
360 }
361
power_supply_is_system_supplied(void)362 int power_supply_is_system_supplied(void)
363 {
364 int error;
365 unsigned int count = 0;
366
367 error = class_for_each_device(power_supply_class, NULL, &count,
368 __power_supply_is_system_supplied);
369
370 /*
371 * If no power class device was found at all, most probably we are
372 * running on a desktop system, so assume we are on mains power.
373 */
374 if (count == 0)
375 return 1;
376
377 return error;
378 }
379 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
380
__power_supply_get_supplier_max_current(struct device * dev,void * data)381 static int __power_supply_get_supplier_max_current(struct device *dev,
382 void *data)
383 {
384 union power_supply_propval ret = {0,};
385 struct power_supply *epsy = dev_get_drvdata(dev);
386 struct power_supply *psy = data;
387
388 if (__power_supply_is_supplied_by(epsy, psy))
389 if (!epsy->desc->get_property(epsy,
390 POWER_SUPPLY_PROP_CURRENT_MAX,
391 &ret))
392 return ret.intval;
393
394 return 0;
395 }
396
power_supply_set_input_current_limit_from_supplier(struct power_supply * psy)397 int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
398 {
399 union power_supply_propval val = {0,};
400 int curr;
401
402 if (!psy->desc->set_property)
403 return -EINVAL;
404
405 /*
406 * This function is not intended for use with a supply with multiple
407 * suppliers, we simply pick the first supply to report a non 0
408 * max-current.
409 */
410 curr = class_for_each_device(power_supply_class, NULL, psy,
411 __power_supply_get_supplier_max_current);
412 if (curr <= 0)
413 return (curr == 0) ? -ENODEV : curr;
414
415 val.intval = curr;
416
417 return psy->desc->set_property(psy,
418 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
419 }
420 EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
421
power_supply_set_battery_charged(struct power_supply * psy)422 int power_supply_set_battery_charged(struct power_supply *psy)
423 {
424 if (atomic_read(&psy->use_cnt) >= 0 &&
425 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
426 psy->desc->set_charged) {
427 psy->desc->set_charged(psy);
428 return 0;
429 }
430
431 return -EINVAL;
432 }
433 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
434
power_supply_match_device_by_name(struct device * dev,const void * data)435 static int power_supply_match_device_by_name(struct device *dev, const void *data)
436 {
437 const char *name = data;
438 struct power_supply *psy = dev_get_drvdata(dev);
439
440 return strcmp(psy->desc->name, name) == 0;
441 }
442
443 /**
444 * power_supply_get_by_name() - Search for a power supply and returns its ref
445 * @name: Power supply name to fetch
446 *
447 * If power supply was found, it increases reference count for the
448 * internal power supply's device. The user should power_supply_put()
449 * after usage.
450 *
451 * Return: On success returns a reference to a power supply with
452 * matching name equals to @name, a NULL otherwise.
453 */
power_supply_get_by_name(const char * name)454 struct power_supply *power_supply_get_by_name(const char *name)
455 {
456 struct power_supply *psy = NULL;
457 struct device *dev = class_find_device(power_supply_class, NULL, name,
458 power_supply_match_device_by_name);
459
460 if (dev) {
461 psy = dev_get_drvdata(dev);
462 atomic_inc(&psy->use_cnt);
463 }
464
465 return psy;
466 }
467 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
468
469 /**
470 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
471 * @psy: Reference to put
472 *
473 * The reference to power supply should be put before unregistering
474 * the power supply.
475 */
power_supply_put(struct power_supply * psy)476 void power_supply_put(struct power_supply *psy)
477 {
478 might_sleep();
479
480 atomic_dec(&psy->use_cnt);
481 put_device(&psy->dev);
482 }
483 EXPORT_SYMBOL_GPL(power_supply_put);
484
485 #ifdef CONFIG_OF
power_supply_match_device_node(struct device * dev,const void * data)486 static int power_supply_match_device_node(struct device *dev, const void *data)
487 {
488 return dev->parent && dev->parent->of_node == data;
489 }
490
491 /**
492 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
493 * @np: Pointer to device node holding phandle property
494 * @property: Name of property holding a power supply name
495 *
496 * If power supply was found, it increases reference count for the
497 * internal power supply's device. The user should power_supply_put()
498 * after usage.
499 *
500 * Return: On success returns a reference to a power supply with
501 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
502 */
power_supply_get_by_phandle(struct device_node * np,const char * property)503 struct power_supply *power_supply_get_by_phandle(struct device_node *np,
504 const char *property)
505 {
506 struct device_node *power_supply_np;
507 struct power_supply *psy = NULL;
508 struct device *dev;
509
510 power_supply_np = of_parse_phandle(np, property, 0);
511 if (!power_supply_np)
512 return ERR_PTR(-ENODEV);
513
514 dev = class_find_device(power_supply_class, NULL, power_supply_np,
515 power_supply_match_device_node);
516
517 of_node_put(power_supply_np);
518
519 if (dev) {
520 psy = dev_get_drvdata(dev);
521 atomic_inc(&psy->use_cnt);
522 }
523
524 return psy;
525 }
526 EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
527
devm_power_supply_put(struct device * dev,void * res)528 static void devm_power_supply_put(struct device *dev, void *res)
529 {
530 struct power_supply **psy = res;
531
532 power_supply_put(*psy);
533 }
534
535 /**
536 * devm_power_supply_get_by_phandle() - Resource managed version of
537 * power_supply_get_by_phandle()
538 * @dev: Pointer to device holding phandle property
539 * @property: Name of property holding a power supply phandle
540 *
541 * Return: On success returns a reference to a power supply with
542 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
543 */
devm_power_supply_get_by_phandle(struct device * dev,const char * property)544 struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
545 const char *property)
546 {
547 struct power_supply **ptr, *psy;
548
549 if (!dev->of_node)
550 return ERR_PTR(-ENODEV);
551
552 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
553 if (!ptr)
554 return ERR_PTR(-ENOMEM);
555
556 psy = power_supply_get_by_phandle(dev->of_node, property);
557 if (IS_ERR_OR_NULL(psy)) {
558 devres_free(ptr);
559 } else {
560 *ptr = psy;
561 devres_add(dev, ptr);
562 }
563 return psy;
564 }
565 EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
566 #endif /* CONFIG_OF */
567
power_supply_get_battery_info(struct power_supply * psy,struct power_supply_battery_info * info)568 int power_supply_get_battery_info(struct power_supply *psy,
569 struct power_supply_battery_info *info)
570 {
571 struct device_node *battery_np;
572 const char *value;
573 int err;
574
575 info->energy_full_design_uwh = -EINVAL;
576 info->charge_full_design_uah = -EINVAL;
577 info->voltage_min_design_uv = -EINVAL;
578 info->precharge_current_ua = -EINVAL;
579 info->charge_term_current_ua = -EINVAL;
580 info->constant_charge_current_max_ua = -EINVAL;
581 info->constant_charge_voltage_max_uv = -EINVAL;
582
583 if (!psy->of_node) {
584 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
585 __func__);
586 return -ENXIO;
587 }
588
589 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
590 if (!battery_np)
591 return -ENODEV;
592
593 err = of_property_read_string(battery_np, "compatible", &value);
594 if (err)
595 return err;
596
597 if (strcmp("simple-battery", value))
598 return -ENODEV;
599
600 /* The property and field names below must correspond to elements
601 * in enum power_supply_property. For reasoning, see
602 * Documentation/power/power_supply_class.txt.
603 */
604
605 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
606 &info->energy_full_design_uwh);
607 of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
608 &info->charge_full_design_uah);
609 of_property_read_u32(battery_np, "voltage-min-design-microvolt",
610 &info->voltage_min_design_uv);
611 of_property_read_u32(battery_np, "precharge-current-microamp",
612 &info->precharge_current_ua);
613 of_property_read_u32(battery_np, "charge-term-current-microamp",
614 &info->charge_term_current_ua);
615 of_property_read_u32(battery_np, "constant_charge_current_max_microamp",
616 &info->constant_charge_current_max_ua);
617 of_property_read_u32(battery_np, "constant_charge_voltage_max_microvolt",
618 &info->constant_charge_voltage_max_uv);
619
620 return 0;
621 }
622 EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
623
power_supply_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)624 int power_supply_get_property(struct power_supply *psy,
625 enum power_supply_property psp,
626 union power_supply_propval *val)
627 {
628 if (atomic_read(&psy->use_cnt) <= 0) {
629 if (!psy->initialized)
630 return -EAGAIN;
631 return -ENODEV;
632 }
633
634 return psy->desc->get_property(psy, psp, val);
635 }
636 EXPORT_SYMBOL_GPL(power_supply_get_property);
637
power_supply_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)638 int power_supply_set_property(struct power_supply *psy,
639 enum power_supply_property psp,
640 const union power_supply_propval *val)
641 {
642 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
643 return -ENODEV;
644
645 return psy->desc->set_property(psy, psp, val);
646 }
647 EXPORT_SYMBOL_GPL(power_supply_set_property);
648
power_supply_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)649 int power_supply_property_is_writeable(struct power_supply *psy,
650 enum power_supply_property psp)
651 {
652 if (atomic_read(&psy->use_cnt) <= 0 ||
653 !psy->desc->property_is_writeable)
654 return -ENODEV;
655
656 return psy->desc->property_is_writeable(psy, psp);
657 }
658 EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
659
power_supply_external_power_changed(struct power_supply * psy)660 void power_supply_external_power_changed(struct power_supply *psy)
661 {
662 if (atomic_read(&psy->use_cnt) <= 0 ||
663 !psy->desc->external_power_changed)
664 return;
665
666 psy->desc->external_power_changed(psy);
667 }
668 EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
669
power_supply_powers(struct power_supply * psy,struct device * dev)670 int power_supply_powers(struct power_supply *psy, struct device *dev)
671 {
672 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
673 }
674 EXPORT_SYMBOL_GPL(power_supply_powers);
675
power_supply_dev_release(struct device * dev)676 static void power_supply_dev_release(struct device *dev)
677 {
678 struct power_supply *psy = to_power_supply(dev);
679 dev_dbg(dev, "%s\n", __func__);
680 kfree(psy);
681 }
682
power_supply_reg_notifier(struct notifier_block * nb)683 int power_supply_reg_notifier(struct notifier_block *nb)
684 {
685 return atomic_notifier_chain_register(&power_supply_notifier, nb);
686 }
687 EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
688
power_supply_unreg_notifier(struct notifier_block * nb)689 void power_supply_unreg_notifier(struct notifier_block *nb)
690 {
691 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
692 }
693 EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
694
695 #ifdef CONFIG_THERMAL
power_supply_read_temp(struct thermal_zone_device * tzd,int * temp)696 static int power_supply_read_temp(struct thermal_zone_device *tzd,
697 int *temp)
698 {
699 struct power_supply *psy;
700 union power_supply_propval val;
701 int ret;
702
703 WARN_ON(tzd == NULL);
704 psy = tzd->devdata;
705 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
706 if (ret)
707 return ret;
708
709 /* Convert tenths of degree Celsius to milli degree Celsius. */
710 *temp = val.intval * 100;
711
712 return ret;
713 }
714
715 static struct thermal_zone_device_ops psy_tzd_ops = {
716 .get_temp = power_supply_read_temp,
717 };
718
psy_register_thermal(struct power_supply * psy)719 static int psy_register_thermal(struct power_supply *psy)
720 {
721 int i;
722
723 if (psy->desc->no_thermal)
724 return 0;
725
726 /* Register battery zone device psy reports temperature */
727 for (i = 0; i < psy->desc->num_properties; i++) {
728 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
729 psy->tzd = thermal_zone_device_register(psy->desc->name,
730 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
731 return PTR_ERR_OR_ZERO(psy->tzd);
732 }
733 }
734 return 0;
735 }
736
psy_unregister_thermal(struct power_supply * psy)737 static void psy_unregister_thermal(struct power_supply *psy)
738 {
739 if (IS_ERR_OR_NULL(psy->tzd))
740 return;
741 thermal_zone_device_unregister(psy->tzd);
742 }
743
744 /* thermal cooling device callbacks */
ps_get_max_charge_cntl_limit(struct thermal_cooling_device * tcd,unsigned long * state)745 static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
746 unsigned long *state)
747 {
748 struct power_supply *psy;
749 union power_supply_propval val;
750 int ret;
751
752 psy = tcd->devdata;
753 ret = power_supply_get_property(psy,
754 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
755 if (ret)
756 return ret;
757
758 *state = val.intval;
759
760 return ret;
761 }
762
ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device * tcd,unsigned long * state)763 static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
764 unsigned long *state)
765 {
766 struct power_supply *psy;
767 union power_supply_propval val;
768 int ret;
769
770 psy = tcd->devdata;
771 ret = power_supply_get_property(psy,
772 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
773 if (ret)
774 return ret;
775
776 *state = val.intval;
777
778 return ret;
779 }
780
ps_set_cur_charge_cntl_limit(struct thermal_cooling_device * tcd,unsigned long state)781 static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
782 unsigned long state)
783 {
784 struct power_supply *psy;
785 union power_supply_propval val;
786 int ret;
787
788 psy = tcd->devdata;
789 val.intval = state;
790 ret = psy->desc->set_property(psy,
791 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
792
793 return ret;
794 }
795
796 static const struct thermal_cooling_device_ops psy_tcd_ops = {
797 .get_max_state = ps_get_max_charge_cntl_limit,
798 .get_cur_state = ps_get_cur_chrage_cntl_limit,
799 .set_cur_state = ps_set_cur_charge_cntl_limit,
800 };
801
psy_register_cooler(struct power_supply * psy)802 static int psy_register_cooler(struct power_supply *psy)
803 {
804 int i;
805
806 /* Register for cooling device if psy can control charging */
807 for (i = 0; i < psy->desc->num_properties; i++) {
808 if (psy->desc->properties[i] ==
809 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
810 psy->tcd = thermal_cooling_device_register(
811 (char *)psy->desc->name,
812 psy, &psy_tcd_ops);
813 return PTR_ERR_OR_ZERO(psy->tcd);
814 }
815 }
816 return 0;
817 }
818
psy_unregister_cooler(struct power_supply * psy)819 static void psy_unregister_cooler(struct power_supply *psy)
820 {
821 if (IS_ERR_OR_NULL(psy->tcd))
822 return;
823 thermal_cooling_device_unregister(psy->tcd);
824 }
825 #else
psy_register_thermal(struct power_supply * psy)826 static int psy_register_thermal(struct power_supply *psy)
827 {
828 return 0;
829 }
830
psy_unregister_thermal(struct power_supply * psy)831 static void psy_unregister_thermal(struct power_supply *psy)
832 {
833 }
834
psy_register_cooler(struct power_supply * psy)835 static int psy_register_cooler(struct power_supply *psy)
836 {
837 return 0;
838 }
839
psy_unregister_cooler(struct power_supply * psy)840 static void psy_unregister_cooler(struct power_supply *psy)
841 {
842 }
843 #endif
844
845 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)846 __power_supply_register(struct device *parent,
847 const struct power_supply_desc *desc,
848 const struct power_supply_config *cfg,
849 bool ws)
850 {
851 struct device *dev;
852 struct power_supply *psy;
853 int i, rc;
854
855 if (!parent)
856 pr_warn("%s: Expected proper parent device for '%s'\n",
857 __func__, desc->name);
858
859 if (!desc || !desc->name || !desc->properties || !desc->num_properties)
860 return ERR_PTR(-EINVAL);
861
862 for (i = 0; i < desc->num_properties; ++i) {
863 if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
864 (!desc->usb_types || !desc->num_usb_types))
865 return ERR_PTR(-EINVAL);
866 }
867
868 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
869 if (!psy)
870 return ERR_PTR(-ENOMEM);
871
872 dev = &psy->dev;
873
874 device_initialize(dev);
875
876 dev->class = power_supply_class;
877 dev->type = &power_supply_dev_type;
878 dev->parent = parent;
879 dev->release = power_supply_dev_release;
880 dev_set_drvdata(dev, psy);
881 psy->desc = desc;
882 if (cfg) {
883 psy->drv_data = cfg->drv_data;
884 psy->of_node =
885 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
886 psy->supplied_to = cfg->supplied_to;
887 psy->num_supplicants = cfg->num_supplicants;
888 }
889
890 rc = dev_set_name(dev, "%s", desc->name);
891 if (rc)
892 goto dev_set_name_failed;
893
894 INIT_WORK(&psy->changed_work, power_supply_changed_work);
895 INIT_DELAYED_WORK(&psy->deferred_register_work,
896 power_supply_deferred_register_work);
897
898 rc = power_supply_check_supplies(psy);
899 if (rc) {
900 dev_info(dev, "Not all required supplies found, defer probe\n");
901 goto check_supplies_failed;
902 }
903
904 spin_lock_init(&psy->changed_lock);
905 rc = device_init_wakeup(dev, ws);
906 if (rc)
907 goto wakeup_init_failed;
908
909 rc = device_add(dev);
910 if (rc)
911 goto device_add_failed;
912
913 rc = psy_register_thermal(psy);
914 if (rc)
915 goto register_thermal_failed;
916
917 rc = psy_register_cooler(psy);
918 if (rc)
919 goto register_cooler_failed;
920
921 rc = power_supply_create_triggers(psy);
922 if (rc)
923 goto create_triggers_failed;
924
925 /*
926 * Update use_cnt after any uevents (most notably from device_add()).
927 * We are here still during driver's probe but
928 * the power_supply_uevent() calls back driver's get_property
929 * method so:
930 * 1. Driver did not assigned the returned struct power_supply,
931 * 2. Driver could not finish initialization (anything in its probe
932 * after calling power_supply_register()).
933 */
934 atomic_inc(&psy->use_cnt);
935 psy->initialized = true;
936
937 queue_delayed_work(system_power_efficient_wq,
938 &psy->deferred_register_work,
939 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
940
941 return psy;
942
943 create_triggers_failed:
944 psy_unregister_cooler(psy);
945 register_cooler_failed:
946 psy_unregister_thermal(psy);
947 register_thermal_failed:
948 device_del(dev);
949 device_add_failed:
950 wakeup_init_failed:
951 check_supplies_failed:
952 dev_set_name_failed:
953 put_device(dev);
954 return ERR_PTR(rc);
955 }
956
957 /**
958 * power_supply_register() - Register new power supply
959 * @parent: Device to be a parent of power supply's device, usually
960 * the device which probe function calls this
961 * @desc: Description of power supply, must be valid through whole
962 * lifetime of this power supply
963 * @cfg: Run-time specific configuration accessed during registering,
964 * may be NULL
965 *
966 * Return: A pointer to newly allocated power_supply on success
967 * or ERR_PTR otherwise.
968 * Use power_supply_unregister() on returned power_supply pointer to release
969 * resources.
970 */
power_supply_register(struct device * parent,const struct power_supply_desc * desc,const struct power_supply_config * cfg)971 struct power_supply *__must_check power_supply_register(struct device *parent,
972 const struct power_supply_desc *desc,
973 const struct power_supply_config *cfg)
974 {
975 return __power_supply_register(parent, desc, cfg, true);
976 }
977 EXPORT_SYMBOL_GPL(power_supply_register);
978
979 /**
980 * power_supply_register_no_ws() - Register new non-waking-source power supply
981 * @parent: Device to be a parent of power supply's device, usually
982 * the device which probe function calls this
983 * @desc: Description of power supply, must be valid through whole
984 * lifetime of this power supply
985 * @cfg: Run-time specific configuration accessed during registering,
986 * may be NULL
987 *
988 * Return: A pointer to newly allocated power_supply on success
989 * or ERR_PTR otherwise.
990 * Use power_supply_unregister() on returned power_supply pointer to release
991 * resources.
992 */
993 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)994 power_supply_register_no_ws(struct device *parent,
995 const struct power_supply_desc *desc,
996 const struct power_supply_config *cfg)
997 {
998 return __power_supply_register(parent, desc, cfg, false);
999 }
1000 EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1001
devm_power_supply_release(struct device * dev,void * res)1002 static void devm_power_supply_release(struct device *dev, void *res)
1003 {
1004 struct power_supply **psy = res;
1005
1006 power_supply_unregister(*psy);
1007 }
1008
1009 /**
1010 * devm_power_supply_register() - Register managed power supply
1011 * @parent: Device to be a parent of power supply's device, usually
1012 * the device which probe function calls this
1013 * @desc: Description of power supply, must be valid through whole
1014 * lifetime of this power supply
1015 * @cfg: Run-time specific configuration accessed during registering,
1016 * may be NULL
1017 *
1018 * Return: A pointer to newly allocated power_supply on success
1019 * or ERR_PTR otherwise.
1020 * The returned power_supply pointer will be automatically unregistered
1021 * on driver detach.
1022 */
1023 struct power_supply *__must_check
devm_power_supply_register(struct device * parent,const struct power_supply_desc * desc,const struct power_supply_config * cfg)1024 devm_power_supply_register(struct device *parent,
1025 const struct power_supply_desc *desc,
1026 const struct power_supply_config *cfg)
1027 {
1028 struct power_supply **ptr, *psy;
1029
1030 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1031
1032 if (!ptr)
1033 return ERR_PTR(-ENOMEM);
1034 psy = __power_supply_register(parent, desc, cfg, true);
1035 if (IS_ERR(psy)) {
1036 devres_free(ptr);
1037 } else {
1038 *ptr = psy;
1039 devres_add(parent, ptr);
1040 }
1041 return psy;
1042 }
1043 EXPORT_SYMBOL_GPL(devm_power_supply_register);
1044
1045 /**
1046 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
1047 * @parent: Device to be a parent of power supply's device, usually
1048 * the device which probe function calls this
1049 * @desc: Description of power supply, must be valid through whole
1050 * lifetime of this power supply
1051 * @cfg: Run-time specific configuration accessed during registering,
1052 * may be NULL
1053 *
1054 * Return: A pointer to newly allocated power_supply on success
1055 * or ERR_PTR otherwise.
1056 * The returned power_supply pointer will be automatically unregistered
1057 * on driver detach.
1058 */
1059 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)1060 devm_power_supply_register_no_ws(struct device *parent,
1061 const struct power_supply_desc *desc,
1062 const struct power_supply_config *cfg)
1063 {
1064 struct power_supply **ptr, *psy;
1065
1066 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1067
1068 if (!ptr)
1069 return ERR_PTR(-ENOMEM);
1070 psy = __power_supply_register(parent, desc, cfg, false);
1071 if (IS_ERR(psy)) {
1072 devres_free(ptr);
1073 } else {
1074 *ptr = psy;
1075 devres_add(parent, ptr);
1076 }
1077 return psy;
1078 }
1079 EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1080
1081 /**
1082 * power_supply_unregister() - Remove this power supply from system
1083 * @psy: Pointer to power supply to unregister
1084 *
1085 * Remove this power supply from the system. The resources of power supply
1086 * will be freed here or on last power_supply_put() call.
1087 */
power_supply_unregister(struct power_supply * psy)1088 void power_supply_unregister(struct power_supply *psy)
1089 {
1090 WARN_ON(atomic_dec_return(&psy->use_cnt));
1091 psy->removing = true;
1092 cancel_work_sync(&psy->changed_work);
1093 cancel_delayed_work_sync(&psy->deferred_register_work);
1094 sysfs_remove_link(&psy->dev.kobj, "powers");
1095 power_supply_remove_triggers(psy);
1096 psy_unregister_cooler(psy);
1097 psy_unregister_thermal(psy);
1098 device_init_wakeup(&psy->dev, false);
1099 device_unregister(&psy->dev);
1100 }
1101 EXPORT_SYMBOL_GPL(power_supply_unregister);
1102
power_supply_get_drvdata(struct power_supply * psy)1103 void *power_supply_get_drvdata(struct power_supply *psy)
1104 {
1105 return psy->drv_data;
1106 }
1107 EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1108
power_supply_class_init(void)1109 static int __init power_supply_class_init(void)
1110 {
1111 power_supply_class = class_create(THIS_MODULE, "power_supply");
1112
1113 if (IS_ERR(power_supply_class))
1114 return PTR_ERR(power_supply_class);
1115
1116 power_supply_class->dev_uevent = power_supply_uevent;
1117 power_supply_init_attrs(&power_supply_dev_type);
1118
1119 return 0;
1120 }
1121
power_supply_class_exit(void)1122 static void __exit power_supply_class_exit(void)
1123 {
1124 class_destroy(power_supply_class);
1125 }
1126
1127 subsys_initcall(power_supply_class_init);
1128 module_exit(power_supply_class_exit);
1129
1130 MODULE_DESCRIPTION("Universal power supply monitor class");
1131 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1132 "Szabolcs Gyurko, "
1133 "Anton Vorontsov <cbou@mail.ru>");
1134 MODULE_LICENSE("GPL");
1135