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