1
2
3
4
5
6
7
8
9
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
27struct class *power_supply_class;
28EXPORT_SYMBOL_GPL(power_supply_class);
29
30ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
31EXPORT_SYMBOL_GPL(power_supply_notifier);
32
33static struct device_type power_supply_dev_type;
34
35#define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
36
37static 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
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
63static 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
76static 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
87
88
89
90
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
106
107
108
109 if (likely(!psy->changed))
110 pm_relax(&psy->dev);
111 spin_unlock_irqrestore(&psy->changed_lock, flags);
112}
113
114void 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}
126EXPORT_SYMBOL_GPL(power_supply_changed);
127
128
129
130
131
132
133
134
135
136
137
138static 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
158static int __power_supply_populate_supplied_from(struct device *dev,
159 void *data)
160{
161 struct power_supply *psy = data;
162 struct power_supply *epsy = dev_get_drvdata(dev);
163 struct device_node *np;
164 int i = 0;
165
166 do {
167 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
168 if (!np)
169 break;
170
171 if (np == epsy->of_node) {
172 dev_info(&psy->dev, "%s: Found supply : %s\n",
173 psy->desc->name, epsy->desc->name);
174 psy->supplied_from[i-1] = (char *)epsy->desc->name;
175 psy->num_supplies++;
176 of_node_put(np);
177 break;
178 }
179 of_node_put(np);
180 } while (np);
181
182 return 0;
183}
184
185static int power_supply_populate_supplied_from(struct power_supply *psy)
186{
187 int error;
188
189 error = class_for_each_device(power_supply_class, NULL, psy,
190 __power_supply_populate_supplied_from);
191
192 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
193
194 return error;
195}
196
197static int __power_supply_find_supply_from_node(struct device *dev,
198 void *data)
199{
200 struct device_node *np = data;
201 struct power_supply *epsy = dev_get_drvdata(dev);
202
203
204 if (epsy->of_node == np)
205 return 1;
206
207 return 0;
208}
209
210static int power_supply_find_supply_from_node(struct device_node *supply_node)
211{
212 int error;
213
214
215
216
217
218
219
220
221
222
223
224 error = class_for_each_device(power_supply_class, NULL, supply_node,
225 __power_supply_find_supply_from_node);
226
227 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
228}
229
230static int power_supply_check_supplies(struct power_supply *psy)
231{
232 struct device_node *np;
233 int cnt = 0;
234
235
236 if (psy->supplied_from && psy->num_supplies > 0)
237 return 0;
238
239
240 if (!psy->of_node)
241 return 0;
242
243 do {
244 int ret;
245
246 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
247 if (!np)
248 break;
249
250 ret = power_supply_find_supply_from_node(np);
251 of_node_put(np);
252
253 if (ret) {
254 dev_dbg(&psy->dev, "Failed to find supply!\n");
255 return ret;
256 }
257 } while (np);
258
259
260 if (cnt == 1)
261 return 0;
262
263
264 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
265 GFP_KERNEL);
266 if (!psy->supplied_from)
267 return -ENOMEM;
268
269 *psy->supplied_from = devm_kcalloc(&psy->dev,
270 cnt - 1, sizeof(char *),
271 GFP_KERNEL);
272 if (!*psy->supplied_from)
273 return -ENOMEM;
274
275 return power_supply_populate_supplied_from(psy);
276}
277#else
278static int power_supply_check_supplies(struct power_supply *psy)
279{
280 int nval, ret;
281
282 if (!psy->dev.parent)
283 return 0;
284
285 nval = device_property_read_string_array(psy->dev.parent,
286 "supplied-from", NULL, 0);
287 if (nval <= 0)
288 return 0;
289
290 psy->supplied_from = devm_kmalloc_array(&psy->dev, nval,
291 sizeof(char *), GFP_KERNEL);
292 if (!psy->supplied_from)
293 return -ENOMEM;
294
295 ret = device_property_read_string_array(psy->dev.parent,
296 "supplied-from", (const char **)psy->supplied_from, nval);
297 if (ret < 0)
298 return ret;
299
300 psy->num_supplies = nval;
301
302 return 0;
303}
304#endif
305
306struct psy_am_i_supplied_data {
307 struct power_supply *psy;
308 unsigned int count;
309};
310
311static int __power_supply_am_i_supplied(struct device *dev, void *_data)
312{
313 union power_supply_propval ret = {0,};
314 struct power_supply *epsy = dev_get_drvdata(dev);
315 struct psy_am_i_supplied_data *data = _data;
316
317 if (__power_supply_is_supplied_by(epsy, data->psy)) {
318 data->count++;
319 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
320 &ret))
321 return ret.intval;
322 }
323
324 return 0;
325}
326
327int power_supply_am_i_supplied(struct power_supply *psy)
328{
329 struct psy_am_i_supplied_data data = { psy, 0 };
330 int error;
331
332 error = class_for_each_device(power_supply_class, NULL, &data,
333 __power_supply_am_i_supplied);
334
335 dev_dbg(&psy->dev, "%s count %u err %d\n", __func__, data.count, error);
336
337 if (data.count == 0)
338 return -ENODEV;
339
340 return error;
341}
342EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
343
344static int __power_supply_is_system_supplied(struct device *dev, void *data)
345{
346 union power_supply_propval ret = {0,};
347 struct power_supply *psy = dev_get_drvdata(dev);
348 unsigned int *count = data;
349
350 (*count)++;
351 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
352 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
353 &ret))
354 return ret.intval;
355
356 return 0;
357}
358
359int power_supply_is_system_supplied(void)
360{
361 int error;
362 unsigned int count = 0;
363
364 error = class_for_each_device(power_supply_class, NULL, &count,
365 __power_supply_is_system_supplied);
366
367
368
369
370
371 if (count == 0)
372 return 1;
373
374 return error;
375}
376EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
377
378static int __power_supply_get_supplier_max_current(struct device *dev,
379 void *data)
380{
381 union power_supply_propval ret = {0,};
382 struct power_supply *epsy = dev_get_drvdata(dev);
383 struct power_supply *psy = data;
384
385 if (__power_supply_is_supplied_by(epsy, psy))
386 if (!epsy->desc->get_property(epsy,
387 POWER_SUPPLY_PROP_CURRENT_MAX,
388 &ret))
389 return ret.intval;
390
391 return 0;
392}
393
394int power_supply_set_input_current_limit_from_supplier(struct power_supply *psy)
395{
396 union power_supply_propval val = {0,};
397 int curr;
398
399 if (!psy->desc->set_property)
400 return -EINVAL;
401
402
403
404
405
406
407 curr = class_for_each_device(power_supply_class, NULL, psy,
408 __power_supply_get_supplier_max_current);
409 if (curr <= 0)
410 return (curr == 0) ? -ENODEV : curr;
411
412 val.intval = curr;
413
414 return psy->desc->set_property(psy,
415 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val);
416}
417EXPORT_SYMBOL_GPL(power_supply_set_input_current_limit_from_supplier);
418
419int power_supply_set_battery_charged(struct power_supply *psy)
420{
421 if (atomic_read(&psy->use_cnt) >= 0 &&
422 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
423 psy->desc->set_charged) {
424 psy->desc->set_charged(psy);
425 return 0;
426 }
427
428 return -EINVAL;
429}
430EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
431
432static int power_supply_match_device_by_name(struct device *dev, const void *data)
433{
434 const char *name = data;
435 struct power_supply *psy = dev_get_drvdata(dev);
436
437 return strcmp(psy->desc->name, name) == 0;
438}
439
440
441
442
443
444
445
446
447
448
449
450
451struct power_supply *power_supply_get_by_name(const char *name)
452{
453 struct power_supply *psy = NULL;
454 struct device *dev = class_find_device(power_supply_class, NULL, name,
455 power_supply_match_device_by_name);
456
457 if (dev) {
458 psy = dev_get_drvdata(dev);
459 atomic_inc(&psy->use_cnt);
460 }
461
462 return psy;
463}
464EXPORT_SYMBOL_GPL(power_supply_get_by_name);
465
466
467
468
469
470
471
472
473void power_supply_put(struct power_supply *psy)
474{
475 might_sleep();
476
477 atomic_dec(&psy->use_cnt);
478 put_device(&psy->dev);
479}
480EXPORT_SYMBOL_GPL(power_supply_put);
481
482#ifdef CONFIG_OF
483static int power_supply_match_device_node(struct device *dev, const void *data)
484{
485 return dev->parent && dev->parent->of_node == data;
486}
487
488
489
490
491
492
493
494
495
496
497
498
499
500struct power_supply *power_supply_get_by_phandle(struct device_node *np,
501 const char *property)
502{
503 struct device_node *power_supply_np;
504 struct power_supply *psy = NULL;
505 struct device *dev;
506
507 power_supply_np = of_parse_phandle(np, property, 0);
508 if (!power_supply_np)
509 return ERR_PTR(-ENODEV);
510
511 dev = class_find_device(power_supply_class, NULL, power_supply_np,
512 power_supply_match_device_node);
513
514 of_node_put(power_supply_np);
515
516 if (dev) {
517 psy = dev_get_drvdata(dev);
518 atomic_inc(&psy->use_cnt);
519 }
520
521 return psy;
522}
523EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
524
525static void devm_power_supply_put(struct device *dev, void *res)
526{
527 struct power_supply **psy = res;
528
529 power_supply_put(*psy);
530}
531
532
533
534
535
536
537
538
539
540
541struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
542 const char *property)
543{
544 struct power_supply **ptr, *psy;
545
546 if (!dev->of_node)
547 return ERR_PTR(-ENODEV);
548
549 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
550 if (!ptr)
551 return ERR_PTR(-ENOMEM);
552
553 psy = power_supply_get_by_phandle(dev->of_node, property);
554 if (IS_ERR_OR_NULL(psy)) {
555 devres_free(ptr);
556 } else {
557 *ptr = psy;
558 devres_add(dev, ptr);
559 }
560 return psy;
561}
562EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
563#endif
564
565int power_supply_get_battery_info(struct power_supply *psy,
566 struct power_supply_battery_info *info)
567{
568 struct power_supply_resistance_temp_table *resist_table;
569 struct device_node *battery_np;
570 const char *value;
571 int err, len, index;
572 const __be32 *list;
573
574 info->energy_full_design_uwh = -EINVAL;
575 info->charge_full_design_uah = -EINVAL;
576 info->voltage_min_design_uv = -EINVAL;
577 info->voltage_max_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 info->factory_internal_resistance_uohm = -EINVAL;
583 info->resist_table = NULL;
584
585 for (index = 0; index < POWER_SUPPLY_OCV_TEMP_MAX; index++) {
586 info->ocv_table[index] = NULL;
587 info->ocv_temp[index] = -EINVAL;
588 info->ocv_table_size[index] = -EINVAL;
589 }
590
591 if (!psy->of_node) {
592 dev_warn(&psy->dev, "%s currently only supports devicetree\n",
593 __func__);
594 return -ENXIO;
595 }
596
597 battery_np = of_parse_phandle(psy->of_node, "monitored-battery", 0);
598 if (!battery_np)
599 return -ENODEV;
600
601 err = of_property_read_string(battery_np, "compatible", &value);
602 if (err)
603 goto out_put_node;
604
605 if (strcmp("simple-battery", value)) {
606 err = -ENODEV;
607 goto out_put_node;
608 }
609
610
611
612
613
614
615 of_property_read_u32(battery_np, "energy-full-design-microwatt-hours",
616 &info->energy_full_design_uwh);
617 of_property_read_u32(battery_np, "charge-full-design-microamp-hours",
618 &info->charge_full_design_uah);
619 of_property_read_u32(battery_np, "voltage-min-design-microvolt",
620 &info->voltage_min_design_uv);
621 of_property_read_u32(battery_np, "voltage-max-design-microvolt",
622 &info->voltage_max_design_uv);
623 of_property_read_u32(battery_np, "trickle-charge-current-microamp",
624 &info->tricklecharge_current_ua);
625 of_property_read_u32(battery_np, "precharge-current-microamp",
626 &info->precharge_current_ua);
627 of_property_read_u32(battery_np, "precharge-upper-limit-microvolt",
628 &info->precharge_voltage_max_uv);
629 of_property_read_u32(battery_np, "charge-term-current-microamp",
630 &info->charge_term_current_ua);
631 of_property_read_u32(battery_np, "re-charge-voltage-microvolt",
632 &info->charge_restart_voltage_uv);
633 of_property_read_u32(battery_np, "over-voltage-threshold-microvolt",
634 &info->overvoltage_limit_uv);
635 of_property_read_u32(battery_np, "constant-charge-current-max-microamp",
636 &info->constant_charge_current_max_ua);
637 of_property_read_u32(battery_np, "constant-charge-voltage-max-microvolt",
638 &info->constant_charge_voltage_max_uv);
639 of_property_read_u32(battery_np, "factory-internal-resistance-micro-ohms",
640 &info->factory_internal_resistance_uohm);
641
642 len = of_property_count_u32_elems(battery_np, "ocv-capacity-celsius");
643 if (len < 0 && len != -EINVAL) {
644 err = len;
645 goto out_put_node;
646 } else if (len > POWER_SUPPLY_OCV_TEMP_MAX) {
647 dev_err(&psy->dev, "Too many temperature values\n");
648 err = -EINVAL;
649 goto out_put_node;
650 } else if (len > 0) {
651 of_property_read_u32_array(battery_np, "ocv-capacity-celsius",
652 info->ocv_temp, len);
653 }
654
655 for (index = 0; index < len; index++) {
656 struct power_supply_battery_ocv_table *table;
657 char *propname;
658 int i, tab_len, size;
659
660 propname = kasprintf(GFP_KERNEL, "ocv-capacity-table-%d", index);
661 list = of_get_property(battery_np, propname, &size);
662 if (!list || !size) {
663 dev_err(&psy->dev, "failed to get %s\n", propname);
664 kfree(propname);
665 power_supply_put_battery_info(psy, info);
666 err = -EINVAL;
667 goto out_put_node;
668 }
669
670 kfree(propname);
671 tab_len = size / (2 * sizeof(__be32));
672 info->ocv_table_size[index] = tab_len;
673
674 table = info->ocv_table[index] =
675 devm_kcalloc(&psy->dev, tab_len, sizeof(*table), GFP_KERNEL);
676 if (!info->ocv_table[index]) {
677 power_supply_put_battery_info(psy, info);
678 err = -ENOMEM;
679 goto out_put_node;
680 }
681
682 for (i = 0; i < tab_len; i++) {
683 table[i].ocv = be32_to_cpu(*list);
684 list++;
685 table[i].capacity = be32_to_cpu(*list);
686 list++;
687 }
688 }
689
690 list = of_get_property(battery_np, "resistance-temp-table", &len);
691 if (!list || !len)
692 goto out_put_node;
693
694 info->resist_table_size = len / (2 * sizeof(__be32));
695 resist_table = info->resist_table = devm_kcalloc(&psy->dev,
696 info->resist_table_size,
697 sizeof(*resist_table),
698 GFP_KERNEL);
699 if (!info->resist_table) {
700 power_supply_put_battery_info(psy, info);
701 err = -ENOMEM;
702 goto out_put_node;
703 }
704
705 for (index = 0; index < info->resist_table_size; index++) {
706 resist_table[index].temp = be32_to_cpu(*list++);
707 resist_table[index].resistance = be32_to_cpu(*list++);
708 }
709
710out_put_node:
711 of_node_put(battery_np);
712 return err;
713}
714EXPORT_SYMBOL_GPL(power_supply_get_battery_info);
715
716void power_supply_put_battery_info(struct power_supply *psy,
717 struct power_supply_battery_info *info)
718{
719 int i;
720
721 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
722 if (info->ocv_table[i])
723 devm_kfree(&psy->dev, info->ocv_table[i]);
724 }
725
726 if (info->resist_table)
727 devm_kfree(&psy->dev, info->resist_table);
728}
729EXPORT_SYMBOL_GPL(power_supply_put_battery_info);
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745int power_supply_temp2resist_simple(struct power_supply_resistance_temp_table *table,
746 int table_len, int temp)
747{
748 int i, resist;
749
750 for (i = 0; i < table_len; i++)
751 if (temp > table[i].temp)
752 break;
753
754 if (i > 0 && i < table_len) {
755 int tmp;
756
757 tmp = (table[i - 1].resistance - table[i].resistance) *
758 (temp - table[i].temp);
759 tmp /= table[i - 1].temp - table[i].temp;
760 resist = tmp + table[i].resistance;
761 } else if (i == 0) {
762 resist = table[0].resistance;
763 } else {
764 resist = table[table_len - 1].resistance;
765 }
766
767 return resist;
768}
769EXPORT_SYMBOL_GPL(power_supply_temp2resist_simple);
770
771
772
773
774
775
776
777
778
779
780
781
782
783int power_supply_ocv2cap_simple(struct power_supply_battery_ocv_table *table,
784 int table_len, int ocv)
785{
786 int i, cap, tmp;
787
788 for (i = 0; i < table_len; i++)
789 if (ocv > table[i].ocv)
790 break;
791
792 if (i > 0 && i < table_len) {
793 tmp = (table[i - 1].capacity - table[i].capacity) *
794 (ocv - table[i].ocv);
795 tmp /= table[i - 1].ocv - table[i].ocv;
796 cap = tmp + table[i].capacity;
797 } else if (i == 0) {
798 cap = table[0].capacity;
799 } else {
800 cap = table[table_len - 1].capacity;
801 }
802
803 return cap;
804}
805EXPORT_SYMBOL_GPL(power_supply_ocv2cap_simple);
806
807struct power_supply_battery_ocv_table *
808power_supply_find_ocv2cap_table(struct power_supply_battery_info *info,
809 int temp, int *table_len)
810{
811 int best_temp_diff = INT_MAX, temp_diff;
812 u8 i, best_index = 0;
813
814 if (!info->ocv_table[0])
815 return NULL;
816
817 for (i = 0; i < POWER_SUPPLY_OCV_TEMP_MAX; i++) {
818 temp_diff = abs(info->ocv_temp[i] - temp);
819
820 if (temp_diff < best_temp_diff) {
821 best_temp_diff = temp_diff;
822 best_index = i;
823 }
824 }
825
826 *table_len = info->ocv_table_size[best_index];
827 return info->ocv_table[best_index];
828}
829EXPORT_SYMBOL_GPL(power_supply_find_ocv2cap_table);
830
831int power_supply_batinfo_ocv2cap(struct power_supply_battery_info *info,
832 int ocv, int temp)
833{
834 struct power_supply_battery_ocv_table *table;
835 int table_len;
836
837 table = power_supply_find_ocv2cap_table(info, temp, &table_len);
838 if (!table)
839 return -EINVAL;
840
841 return power_supply_ocv2cap_simple(table, table_len, ocv);
842}
843EXPORT_SYMBOL_GPL(power_supply_batinfo_ocv2cap);
844
845int power_supply_get_property(struct power_supply *psy,
846 enum power_supply_property psp,
847 union power_supply_propval *val)
848{
849 if (atomic_read(&psy->use_cnt) <= 0) {
850 if (!psy->initialized)
851 return -EAGAIN;
852 return -ENODEV;
853 }
854
855 return psy->desc->get_property(psy, psp, val);
856}
857EXPORT_SYMBOL_GPL(power_supply_get_property);
858
859int power_supply_set_property(struct power_supply *psy,
860 enum power_supply_property psp,
861 const union power_supply_propval *val)
862{
863 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
864 return -ENODEV;
865
866 return psy->desc->set_property(psy, psp, val);
867}
868EXPORT_SYMBOL_GPL(power_supply_set_property);
869
870int power_supply_property_is_writeable(struct power_supply *psy,
871 enum power_supply_property psp)
872{
873 if (atomic_read(&psy->use_cnt) <= 0 ||
874 !psy->desc->property_is_writeable)
875 return -ENODEV;
876
877 return psy->desc->property_is_writeable(psy, psp);
878}
879EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
880
881void power_supply_external_power_changed(struct power_supply *psy)
882{
883 if (atomic_read(&psy->use_cnt) <= 0 ||
884 !psy->desc->external_power_changed)
885 return;
886
887 psy->desc->external_power_changed(psy);
888}
889EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
890
891int power_supply_powers(struct power_supply *psy, struct device *dev)
892{
893 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
894}
895EXPORT_SYMBOL_GPL(power_supply_powers);
896
897static void power_supply_dev_release(struct device *dev)
898{
899 struct power_supply *psy = to_power_supply(dev);
900 dev_dbg(dev, "%s\n", __func__);
901 kfree(psy);
902}
903
904int power_supply_reg_notifier(struct notifier_block *nb)
905{
906 return atomic_notifier_chain_register(&power_supply_notifier, nb);
907}
908EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
909
910void power_supply_unreg_notifier(struct notifier_block *nb)
911{
912 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
913}
914EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
915
916#ifdef CONFIG_THERMAL
917static int power_supply_read_temp(struct thermal_zone_device *tzd,
918 int *temp)
919{
920 struct power_supply *psy;
921 union power_supply_propval val;
922 int ret;
923
924 WARN_ON(tzd == NULL);
925 psy = tzd->devdata;
926 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
927 if (ret)
928 return ret;
929
930
931 *temp = val.intval * 100;
932
933 return ret;
934}
935
936static struct thermal_zone_device_ops psy_tzd_ops = {
937 .get_temp = power_supply_read_temp,
938};
939
940static int psy_register_thermal(struct power_supply *psy)
941{
942 int i, ret;
943
944 if (psy->desc->no_thermal)
945 return 0;
946
947
948 for (i = 0; i < psy->desc->num_properties; i++) {
949 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
950 psy->tzd = thermal_zone_device_register(psy->desc->name,
951 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
952 if (IS_ERR(psy->tzd))
953 return PTR_ERR(psy->tzd);
954 ret = thermal_zone_device_enable(psy->tzd);
955 if (ret)
956 thermal_zone_device_unregister(psy->tzd);
957 return ret;
958 }
959 }
960 return 0;
961}
962
963static void psy_unregister_thermal(struct power_supply *psy)
964{
965 if (IS_ERR_OR_NULL(psy->tzd))
966 return;
967 thermal_zone_device_unregister(psy->tzd);
968}
969
970
971static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
972 unsigned long *state)
973{
974 struct power_supply *psy;
975 union power_supply_propval val;
976 int ret;
977
978 psy = tcd->devdata;
979 ret = power_supply_get_property(psy,
980 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
981 if (ret)
982 return ret;
983
984 *state = val.intval;
985
986 return ret;
987}
988
989static int ps_get_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
990 unsigned long *state)
991{
992 struct power_supply *psy;
993 union power_supply_propval val;
994 int ret;
995
996 psy = tcd->devdata;
997 ret = power_supply_get_property(psy,
998 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
999 if (ret)
1000 return ret;
1001
1002 *state = val.intval;
1003
1004 return ret;
1005}
1006
1007static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
1008 unsigned long state)
1009{
1010 struct power_supply *psy;
1011 union power_supply_propval val;
1012 int ret;
1013
1014 psy = tcd->devdata;
1015 val.intval = state;
1016 ret = psy->desc->set_property(psy,
1017 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
1018
1019 return ret;
1020}
1021
1022static const struct thermal_cooling_device_ops psy_tcd_ops = {
1023 .get_max_state = ps_get_max_charge_cntl_limit,
1024 .get_cur_state = ps_get_cur_charge_cntl_limit,
1025 .set_cur_state = ps_set_cur_charge_cntl_limit,
1026};
1027
1028static int psy_register_cooler(struct power_supply *psy)
1029{
1030 int i;
1031
1032
1033 for (i = 0; i < psy->desc->num_properties; i++) {
1034 if (psy->desc->properties[i] ==
1035 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
1036 psy->tcd = thermal_cooling_device_register(
1037 (char *)psy->desc->name,
1038 psy, &psy_tcd_ops);
1039 return PTR_ERR_OR_ZERO(psy->tcd);
1040 }
1041 }
1042 return 0;
1043}
1044
1045static void psy_unregister_cooler(struct power_supply *psy)
1046{
1047 if (IS_ERR_OR_NULL(psy->tcd))
1048 return;
1049 thermal_cooling_device_unregister(psy->tcd);
1050}
1051#else
1052static int psy_register_thermal(struct power_supply *psy)
1053{
1054 return 0;
1055}
1056
1057static void psy_unregister_thermal(struct power_supply *psy)
1058{
1059}
1060
1061static int psy_register_cooler(struct power_supply *psy)
1062{
1063 return 0;
1064}
1065
1066static void psy_unregister_cooler(struct power_supply *psy)
1067{
1068}
1069#endif
1070
1071static struct power_supply *__must_check
1072__power_supply_register(struct device *parent,
1073 const struct power_supply_desc *desc,
1074 const struct power_supply_config *cfg,
1075 bool ws)
1076{
1077 struct device *dev;
1078 struct power_supply *psy;
1079 int i, rc;
1080
1081 if (!parent)
1082 pr_warn("%s: Expected proper parent device for '%s'\n",
1083 __func__, desc->name);
1084
1085 if (!desc || !desc->name || !desc->properties || !desc->num_properties)
1086 return ERR_PTR(-EINVAL);
1087
1088 for (i = 0; i < desc->num_properties; ++i) {
1089 if ((desc->properties[i] == POWER_SUPPLY_PROP_USB_TYPE) &&
1090 (!desc->usb_types || !desc->num_usb_types))
1091 return ERR_PTR(-EINVAL);
1092 }
1093
1094 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
1095 if (!psy)
1096 return ERR_PTR(-ENOMEM);
1097
1098 dev = &psy->dev;
1099
1100 device_initialize(dev);
1101
1102 dev->class = power_supply_class;
1103 dev->type = &power_supply_dev_type;
1104 dev->parent = parent;
1105 dev->release = power_supply_dev_release;
1106 dev_set_drvdata(dev, psy);
1107 psy->desc = desc;
1108 if (cfg) {
1109 dev->groups = cfg->attr_grp;
1110 psy->drv_data = cfg->drv_data;
1111 psy->of_node =
1112 cfg->fwnode ? to_of_node(cfg->fwnode) : cfg->of_node;
1113 psy->supplied_to = cfg->supplied_to;
1114 psy->num_supplicants = cfg->num_supplicants;
1115 }
1116
1117 rc = dev_set_name(dev, "%s", desc->name);
1118 if (rc)
1119 goto dev_set_name_failed;
1120
1121 INIT_WORK(&psy->changed_work, power_supply_changed_work);
1122 INIT_DELAYED_WORK(&psy->deferred_register_work,
1123 power_supply_deferred_register_work);
1124
1125 rc = power_supply_check_supplies(psy);
1126 if (rc) {
1127 dev_info(dev, "Not all required supplies found, defer probe\n");
1128 goto check_supplies_failed;
1129 }
1130
1131 spin_lock_init(&psy->changed_lock);
1132 rc = device_add(dev);
1133 if (rc)
1134 goto device_add_failed;
1135
1136 rc = device_init_wakeup(dev, ws);
1137 if (rc)
1138 goto wakeup_init_failed;
1139
1140 rc = psy_register_thermal(psy);
1141 if (rc)
1142 goto register_thermal_failed;
1143
1144 rc = psy_register_cooler(psy);
1145 if (rc)
1146 goto register_cooler_failed;
1147
1148 rc = power_supply_create_triggers(psy);
1149 if (rc)
1150 goto create_triggers_failed;
1151
1152 rc = power_supply_add_hwmon_sysfs(psy);
1153 if (rc)
1154 goto add_hwmon_sysfs_failed;
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165 atomic_inc(&psy->use_cnt);
1166 psy->initialized = true;
1167
1168 queue_delayed_work(system_power_efficient_wq,
1169 &psy->deferred_register_work,
1170 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
1171
1172 return psy;
1173
1174add_hwmon_sysfs_failed:
1175 power_supply_remove_triggers(psy);
1176create_triggers_failed:
1177 psy_unregister_cooler(psy);
1178register_cooler_failed:
1179 psy_unregister_thermal(psy);
1180register_thermal_failed:
1181 device_del(dev);
1182wakeup_init_failed:
1183device_add_failed:
1184check_supplies_failed:
1185dev_set_name_failed:
1186 put_device(dev);
1187 return ERR_PTR(rc);
1188}
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204struct power_supply *__must_check power_supply_register(struct device *parent,
1205 const struct power_supply_desc *desc,
1206 const struct power_supply_config *cfg)
1207{
1208 return __power_supply_register(parent, desc, cfg, true);
1209}
1210EXPORT_SYMBOL_GPL(power_supply_register);
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226struct power_supply *__must_check
1227power_supply_register_no_ws(struct device *parent,
1228 const struct power_supply_desc *desc,
1229 const struct power_supply_config *cfg)
1230{
1231 return __power_supply_register(parent, desc, cfg, false);
1232}
1233EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
1234
1235static void devm_power_supply_release(struct device *dev, void *res)
1236{
1237 struct power_supply **psy = res;
1238
1239 power_supply_unregister(*psy);
1240}
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256struct power_supply *__must_check
1257devm_power_supply_register(struct device *parent,
1258 const struct power_supply_desc *desc,
1259 const struct power_supply_config *cfg)
1260{
1261 struct power_supply **ptr, *psy;
1262
1263 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1264
1265 if (!ptr)
1266 return ERR_PTR(-ENOMEM);
1267 psy = __power_supply_register(parent, desc, cfg, true);
1268 if (IS_ERR(psy)) {
1269 devres_free(ptr);
1270 } else {
1271 *ptr = psy;
1272 devres_add(parent, ptr);
1273 }
1274 return psy;
1275}
1276EXPORT_SYMBOL_GPL(devm_power_supply_register);
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292struct power_supply *__must_check
1293devm_power_supply_register_no_ws(struct device *parent,
1294 const struct power_supply_desc *desc,
1295 const struct power_supply_config *cfg)
1296{
1297 struct power_supply **ptr, *psy;
1298
1299 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
1300
1301 if (!ptr)
1302 return ERR_PTR(-ENOMEM);
1303 psy = __power_supply_register(parent, desc, cfg, false);
1304 if (IS_ERR(psy)) {
1305 devres_free(ptr);
1306 } else {
1307 *ptr = psy;
1308 devres_add(parent, ptr);
1309 }
1310 return psy;
1311}
1312EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
1313
1314
1315
1316
1317
1318
1319
1320
1321void power_supply_unregister(struct power_supply *psy)
1322{
1323 WARN_ON(atomic_dec_return(&psy->use_cnt));
1324 psy->removing = true;
1325 cancel_work_sync(&psy->changed_work);
1326 cancel_delayed_work_sync(&psy->deferred_register_work);
1327 sysfs_remove_link(&psy->dev.kobj, "powers");
1328 power_supply_remove_hwmon_sysfs(psy);
1329 power_supply_remove_triggers(psy);
1330 psy_unregister_cooler(psy);
1331 psy_unregister_thermal(psy);
1332 device_init_wakeup(&psy->dev, false);
1333 device_unregister(&psy->dev);
1334}
1335EXPORT_SYMBOL_GPL(power_supply_unregister);
1336
1337void *power_supply_get_drvdata(struct power_supply *psy)
1338{
1339 return psy->drv_data;
1340}
1341EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
1342
1343static int __init power_supply_class_init(void)
1344{
1345 power_supply_class = class_create(THIS_MODULE, "power_supply");
1346
1347 if (IS_ERR(power_supply_class))
1348 return PTR_ERR(power_supply_class);
1349
1350 power_supply_class->dev_uevent = power_supply_uevent;
1351 power_supply_init_attrs(&power_supply_dev_type);
1352
1353 return 0;
1354}
1355
1356static void __exit power_supply_class_exit(void)
1357{
1358 class_destroy(power_supply_class);
1359}
1360
1361subsys_initcall(power_supply_class_init);
1362module_exit(power_supply_class_exit);
1363
1364MODULE_DESCRIPTION("Universal power supply monitor class");
1365MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
1366 "Szabolcs Gyurko, "
1367 "Anton Vorontsov <cbou@mail.ru>");
1368MODULE_LICENSE("GPL");
1369