close log print in bq27510
[firefly-linux-kernel-4.4.55.git] / drivers / power / power_supply_core.c
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/device.h>
17 #include <linux/err.h>
18 #include <linux/power_supply.h>
19 #include "power_supply.h"
20
21 /* exported for the APM Power driver, APM emulation */
22 struct class *power_supply_class;
23 EXPORT_SYMBOL_GPL(power_supply_class);
24
25 static int __power_supply_changed_work(struct device *dev, void *data)
26 {
27         struct power_supply *psy = (struct power_supply *)data;
28         struct power_supply *pst = dev_get_drvdata(dev);
29         int i;
30
31         for (i = 0; i < psy->num_supplicants; i++)
32                 if (!strcmp(psy->supplied_to[i], pst->name)) {
33                         if (pst->external_power_changed)
34                                 pst->external_power_changed(pst);
35                 }
36         return 0;
37 }
38
39 static void power_supply_changed_work(struct work_struct *work)
40 {
41         unsigned long flags;
42         struct power_supply *psy = container_of(work, struct power_supply,
43                                                 changed_work);
44
45         dev_dbg(psy->dev, "%s\n", __func__);
46
47         spin_lock_irqsave(&psy->changed_lock, flags);
48         if (psy->changed) {
49                 psy->changed = false;
50                 spin_unlock_irqrestore(&psy->changed_lock, flags);
51
52                 class_for_each_device(power_supply_class, NULL, psy,
53                                       __power_supply_changed_work);
54
55                 power_supply_update_leds(psy);
56
57                 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
58                 spin_lock_irqsave(&psy->changed_lock, flags);
59         }
60         if (!psy->changed)
61                 wake_unlock(&psy->work_wake_lock);
62         spin_unlock_irqrestore(&psy->changed_lock, flags);
63 }
64
65 void power_supply_changed(struct power_supply *psy)
66 {
67         unsigned long flags;
68
69         dev_dbg(psy->dev, "%s\n", __func__);
70
71         spin_lock_irqsave(&psy->changed_lock, flags);
72         psy->changed = true;
73         wake_lock(&psy->work_wake_lock);
74         spin_unlock_irqrestore(&psy->changed_lock, flags);
75         schedule_work(&psy->changed_work);
76 }
77 EXPORT_SYMBOL_GPL(power_supply_changed);
78
79 static int __power_supply_am_i_supplied(struct device *dev, void *data)
80 {
81         union power_supply_propval ret = {0,};
82         struct power_supply *psy = (struct power_supply *)data;
83         struct power_supply *epsy = dev_get_drvdata(dev);
84         int i;
85
86         for (i = 0; i < epsy->num_supplicants; i++) {
87                 if (!strcmp(epsy->supplied_to[i], psy->name)) {
88                         if (epsy->get_property(epsy,
89                                   POWER_SUPPLY_PROP_ONLINE, &ret))
90                                 continue;
91                         if (ret.intval)
92                                 return ret.intval;
93                 }
94         }
95         return 0;
96 }
97
98 int power_supply_am_i_supplied(struct power_supply *psy)
99 {
100         int error;
101
102         error = class_for_each_device(power_supply_class, NULL, psy,
103                                       __power_supply_am_i_supplied);
104
105         dev_dbg(psy->dev, "%s %d\n", __func__, error);
106
107         return error;
108 }
109 EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
110
111 static int __power_supply_is_system_supplied(struct device *dev, void *data)
112 {
113         union power_supply_propval ret = {0,};
114         struct power_supply *psy = dev_get_drvdata(dev);
115
116         if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
117                 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
118                         return 0;
119                 if (ret.intval)
120                         return ret.intval;
121         }
122         return 0;
123 }
124
125 int power_supply_is_system_supplied(void)
126 {
127         int error;
128
129         error = class_for_each_device(power_supply_class, NULL, NULL,
130                                       __power_supply_is_system_supplied);
131
132         return error;
133 }
134 EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
135
136 int power_supply_set_battery_charged(struct power_supply *psy)
137 {
138         if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
139                 psy->set_charged(psy);
140                 return 0;
141         }
142
143         return -EINVAL;
144 }
145 EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
146
147 static int power_supply_match_device_by_name(struct device *dev, void *data)
148 {
149         const char *name = data;
150         struct power_supply *psy = dev_get_drvdata(dev);
151
152         return strcmp(psy->name, name) == 0;
153 }
154
155 struct power_supply *power_supply_get_by_name(char *name)
156 {
157         struct device *dev = class_find_device(power_supply_class, NULL, name,
158                                         power_supply_match_device_by_name);
159
160         return dev ? dev_get_drvdata(dev) : NULL;
161 }
162 EXPORT_SYMBOL_GPL(power_supply_get_by_name);
163
164 int power_supply_register(struct device *parent, struct power_supply *psy)
165 {
166         int rc = 0;
167
168         psy->dev = device_create(power_supply_class, parent, 0, psy,
169                                  "%s", psy->name);
170         if (IS_ERR(psy->dev)) {
171                 rc = PTR_ERR(psy->dev);
172                 goto dev_create_failed;
173         }
174
175         INIT_WORK(&psy->changed_work, power_supply_changed_work);
176         spin_lock_init(&psy->changed_lock);
177         wake_lock_init(&psy->work_wake_lock, WAKE_LOCK_SUSPEND, "power-supply");
178
179         rc = power_supply_create_attrs(psy);
180         if (rc)
181                 goto create_attrs_failed;
182
183         rc = power_supply_create_triggers(psy);
184         if (rc)
185                 goto create_triggers_failed;
186
187         power_supply_changed(psy);
188
189         goto success;
190
191 create_triggers_failed:
192         power_supply_remove_attrs(psy);
193 create_attrs_failed:
194         wake_lock_destroy(&psy->work_wake_lock);
195         device_unregister(psy->dev);
196 dev_create_failed:
197 success:
198         return rc;
199 }
200 EXPORT_SYMBOL_GPL(power_supply_register);
201
202 void power_supply_unregister(struct power_supply *psy)
203 {
204         flush_scheduled_work();
205         power_supply_remove_triggers(psy);
206         power_supply_remove_attrs(psy);
207         wake_lock_destroy(&psy->work_wake_lock);
208         device_unregister(psy->dev);
209 }
210 EXPORT_SYMBOL_GPL(power_supply_unregister);
211
212 static int __init power_supply_class_init(void)
213 {
214         power_supply_class = class_create(THIS_MODULE, "power_supply");
215
216         if (IS_ERR(power_supply_class))
217                 return PTR_ERR(power_supply_class);
218
219         power_supply_class->dev_uevent = power_supply_uevent;
220
221         return 0;
222 }
223
224 static void __exit power_supply_class_exit(void)
225 {
226         class_destroy(power_supply_class);
227 }
228
229 subsys_initcall(power_supply_class_init);
230 module_exit(power_supply_class_exit);
231
232 MODULE_DESCRIPTION("Universal power supply monitor class");
233 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
234               "Szabolcs Gyurko, "
235               "Anton Vorontsov <cbou@mail.ru>");
236 MODULE_LICENSE("GPL");