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