Commit | Line | Data |
---|---|---|
4a11b59d AV |
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> | |
5f487cd3 | 16 | #include <linux/slab.h> |
4a11b59d | 17 | #include <linux/device.h> |
d36240d2 | 18 | #include <linux/notifier.h> |
4a11b59d AV |
19 | #include <linux/err.h> |
20 | #include <linux/power_supply.h> | |
3be330bf | 21 | #include <linux/thermal.h> |
4a11b59d AV |
22 | #include "power_supply.h" |
23 | ||
ff3417e7 | 24 | /* exported for the APM Power driver, APM emulation */ |
4a11b59d | 25 | struct class *power_supply_class; |
ff3417e7 | 26 | EXPORT_SYMBOL_GPL(power_supply_class); |
4a11b59d | 27 | |
d36240d2 PR |
28 | ATOMIC_NOTIFIER_HEAD(power_supply_notifier); |
29 | EXPORT_SYMBOL_GPL(power_supply_notifier); | |
30 | ||
5f487cd3 AV |
31 | static struct device_type power_supply_dev_type; |
32 | ||
5e0848c6 RK |
33 | static bool __power_supply_is_supplied_by(struct power_supply *supplier, |
34 | struct power_supply *supply) | |
35 | { | |
36 | int i; | |
37 | ||
38 | if (!supply->supplied_from && !supplier->supplied_to) | |
39 | return false; | |
40 | ||
41 | /* Support both supplied_to and supplied_from modes */ | |
42 | if (supply->supplied_from) { | |
43 | if (!supplier->name) | |
44 | return false; | |
45 | for (i = 0; i < supply->num_supplies; i++) | |
46 | if (!strcmp(supplier->name, supply->supplied_from[i])) | |
47 | return true; | |
48 | } else { | |
49 | if (!supply->name) | |
50 | return false; | |
51 | for (i = 0; i < supplier->num_supplicants; i++) | |
52 | if (!strcmp(supplier->supplied_to[i], supply->name)) | |
53 | return true; | |
54 | } | |
55 | ||
56 | return false; | |
57 | } | |
58 | ||
443cad92 DY |
59 | static int __power_supply_changed_work(struct device *dev, void *data) |
60 | { | |
e80cf421 | 61 | struct power_supply *psy = data; |
443cad92 | 62 | struct power_supply *pst = dev_get_drvdata(dev); |
443cad92 | 63 | |
5e0848c6 RK |
64 | if (__power_supply_is_supplied_by(psy, pst)) { |
65 | if (pst->external_power_changed) | |
66 | pst->external_power_changed(pst); | |
67 | } | |
68 | ||
443cad92 DY |
69 | return 0; |
70 | } | |
71 | ||
4a11b59d AV |
72 | static void power_supply_changed_work(struct work_struct *work) |
73 | { | |
948dcf96 | 74 | unsigned long flags; |
4a11b59d AV |
75 | struct power_supply *psy = container_of(work, struct power_supply, |
76 | changed_work); | |
4a11b59d | 77 | |
0cddc0a9 | 78 | dev_dbg(psy->dev, "%s\n", __func__); |
4a11b59d | 79 | |
948dcf96 ZM |
80 | spin_lock_irqsave(&psy->changed_lock, flags); |
81 | if (psy->changed) { | |
82 | psy->changed = false; | |
83 | spin_unlock_irqrestore(&psy->changed_lock, flags); | |
84 | class_for_each_device(power_supply_class, NULL, psy, | |
85 | __power_supply_changed_work); | |
86 | power_supply_update_leds(psy); | |
d36240d2 PR |
87 | atomic_notifier_call_chain(&power_supply_notifier, |
88 | PSY_EVENT_PROP_CHANGED, psy); | |
948dcf96 ZM |
89 | kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE); |
90 | spin_lock_irqsave(&psy->changed_lock, flags); | |
91 | } | |
92 | /* | |
93 | * Dependent power supplies (e.g. battery) may have changed state | |
94 | * as a result of this event, so poll again and hold the | |
95 | * wakeup_source until all events are processed. | |
96 | */ | |
97 | if (!psy->changed) | |
98 | pm_relax(psy->dev); | |
99 | spin_unlock_irqrestore(&psy->changed_lock, flags); | |
4a11b59d AV |
100 | } |
101 | ||
102 | void power_supply_changed(struct power_supply *psy) | |
103 | { | |
948dcf96 ZM |
104 | unsigned long flags; |
105 | ||
0cddc0a9 | 106 | dev_dbg(psy->dev, "%s\n", __func__); |
4a11b59d | 107 | |
948dcf96 ZM |
108 | spin_lock_irqsave(&psy->changed_lock, flags); |
109 | psy->changed = true; | |
110 | pm_stay_awake(psy->dev); | |
111 | spin_unlock_irqrestore(&psy->changed_lock, flags); | |
4a11b59d | 112 | schedule_work(&psy->changed_work); |
4a11b59d | 113 | } |
ff3417e7 | 114 | EXPORT_SYMBOL_GPL(power_supply_changed); |
4a11b59d | 115 | |
f6e0b081 RK |
116 | #ifdef CONFIG_OF |
117 | #include <linux/of.h> | |
118 | ||
119 | static int __power_supply_populate_supplied_from(struct device *dev, | |
120 | void *data) | |
121 | { | |
e80cf421 | 122 | struct power_supply *psy = data; |
f6e0b081 RK |
123 | struct power_supply *epsy = dev_get_drvdata(dev); |
124 | struct device_node *np; | |
125 | int i = 0; | |
126 | ||
127 | do { | |
128 | np = of_parse_phandle(psy->of_node, "power-supplies", i++); | |
129 | if (!np) | |
a0f93b42 | 130 | break; |
f6e0b081 RK |
131 | |
132 | if (np == epsy->of_node) { | |
133 | dev_info(psy->dev, "%s: Found supply : %s\n", | |
134 | psy->name, epsy->name); | |
135 | psy->supplied_from[i-1] = (char *)epsy->name; | |
136 | psy->num_supplies++; | |
2054d6e9 | 137 | of_node_put(np); |
f6e0b081 RK |
138 | break; |
139 | } | |
2054d6e9 | 140 | of_node_put(np); |
f6e0b081 RK |
141 | } while (np); |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | static int power_supply_populate_supplied_from(struct power_supply *psy) | |
147 | { | |
148 | int error; | |
149 | ||
150 | error = class_for_each_device(power_supply_class, NULL, psy, | |
151 | __power_supply_populate_supplied_from); | |
152 | ||
153 | dev_dbg(psy->dev, "%s %d\n", __func__, error); | |
154 | ||
155 | return error; | |
156 | } | |
157 | ||
158 | static int __power_supply_find_supply_from_node(struct device *dev, | |
159 | void *data) | |
160 | { | |
e80cf421 | 161 | struct device_node *np = data; |
f6e0b081 RK |
162 | struct power_supply *epsy = dev_get_drvdata(dev); |
163 | ||
585b0087 | 164 | /* returning non-zero breaks out of class_for_each_device loop */ |
f6e0b081 | 165 | if (epsy->of_node == np) |
585b0087 | 166 | return 1; |
f6e0b081 RK |
167 | |
168 | return 0; | |
169 | } | |
170 | ||
171 | static int power_supply_find_supply_from_node(struct device_node *supply_node) | |
172 | { | |
173 | int error; | |
174 | struct device *dev; | |
175 | struct class_dev_iter iter; | |
176 | ||
177 | /* | |
178 | * Use iterator to see if any other device is registered. | |
179 | * This is required since class_for_each_device returns 0 | |
180 | * if there are no devices registered. | |
181 | */ | |
182 | class_dev_iter_init(&iter, power_supply_class, NULL, NULL); | |
183 | dev = class_dev_iter_next(&iter); | |
184 | ||
185 | if (!dev) | |
186 | return -EPROBE_DEFER; | |
187 | ||
188 | /* | |
585b0087 VK |
189 | * class_for_each_device() either returns its own errors or values |
190 | * returned by __power_supply_find_supply_from_node(). | |
191 | * | |
192 | * __power_supply_find_supply_from_node() will return 0 (no match) | |
193 | * or 1 (match). | |
194 | * | |
195 | * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if | |
196 | * it returned 0, or error as returned by it. | |
f6e0b081 RK |
197 | */ |
198 | error = class_for_each_device(power_supply_class, NULL, supply_node, | |
199 | __power_supply_find_supply_from_node); | |
200 | ||
585b0087 | 201 | return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER; |
f6e0b081 RK |
202 | } |
203 | ||
204 | static int power_supply_check_supplies(struct power_supply *psy) | |
205 | { | |
206 | struct device_node *np; | |
207 | int cnt = 0; | |
208 | ||
209 | /* If there is already a list honor it */ | |
210 | if (psy->supplied_from && psy->num_supplies > 0) | |
211 | return 0; | |
212 | ||
213 | /* No device node found, nothing to do */ | |
214 | if (!psy->of_node) | |
215 | return 0; | |
216 | ||
217 | do { | |
218 | int ret; | |
219 | ||
220 | np = of_parse_phandle(psy->of_node, "power-supplies", cnt++); | |
221 | if (!np) | |
a0f93b42 | 222 | break; |
f6e0b081 RK |
223 | |
224 | ret = power_supply_find_supply_from_node(np); | |
8468b029 VK |
225 | of_node_put(np); |
226 | ||
f6e0b081 | 227 | if (ret) { |
f5b89aff VK |
228 | dev_dbg(psy->dev, "Failed to find supply!\n"); |
229 | return ret; | |
f6e0b081 RK |
230 | } |
231 | } while (np); | |
232 | ||
f9c85486 VK |
233 | /* Missing valid "power-supplies" entries */ |
234 | if (cnt == 1) | |
235 | return 0; | |
236 | ||
f6e0b081 RK |
237 | /* All supplies found, allocate char ** array for filling */ |
238 | psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from), | |
239 | GFP_KERNEL); | |
240 | if (!psy->supplied_from) { | |
241 | dev_err(psy->dev, "Couldn't allocate memory for supply list\n"); | |
242 | return -ENOMEM; | |
243 | } | |
244 | ||
8f5a37cb | 245 | *psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * (cnt - 1), |
f6e0b081 RK |
246 | GFP_KERNEL); |
247 | if (!*psy->supplied_from) { | |
248 | dev_err(psy->dev, "Couldn't allocate memory for supply list\n"); | |
249 | return -ENOMEM; | |
250 | } | |
251 | ||
252 | return power_supply_populate_supplied_from(psy); | |
253 | } | |
254 | #else | |
255 | static inline int power_supply_check_supplies(struct power_supply *psy) | |
256 | { | |
257 | return 0; | |
258 | } | |
259 | #endif | |
260 | ||
443cad92 | 261 | static int __power_supply_am_i_supplied(struct device *dev, void *data) |
4a11b59d AV |
262 | { |
263 | union power_supply_propval ret = {0,}; | |
e80cf421 | 264 | struct power_supply *psy = data; |
443cad92 | 265 | struct power_supply *epsy = dev_get_drvdata(dev); |
443cad92 | 266 | |
5e0848c6 RK |
267 | if (__power_supply_is_supplied_by(epsy, psy)) |
268 | if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) { | |
443cad92 DY |
269 | if (ret.intval) |
270 | return ret.intval; | |
4a11b59d | 271 | } |
5e0848c6 | 272 | |
443cad92 DY |
273 | return 0; |
274 | } | |
275 | ||
276 | int power_supply_am_i_supplied(struct power_supply *psy) | |
277 | { | |
278 | int error; | |
279 | ||
93562b53 | 280 | error = class_for_each_device(power_supply_class, NULL, psy, |
443cad92 | 281 | __power_supply_am_i_supplied); |
4a11b59d | 282 | |
0cddc0a9 | 283 | dev_dbg(psy->dev, "%s %d\n", __func__, error); |
4a11b59d | 284 | |
443cad92 | 285 | return error; |
4a11b59d | 286 | } |
ff3417e7 | 287 | EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); |
4a11b59d | 288 | |
942ed161 MG |
289 | static int __power_supply_is_system_supplied(struct device *dev, void *data) |
290 | { | |
291 | union power_supply_propval ret = {0,}; | |
292 | struct power_supply *psy = dev_get_drvdata(dev); | |
2530daa1 | 293 | unsigned int *count = data; |
942ed161 | 294 | |
2530daa1 | 295 | (*count)++; |
942ed161 MG |
296 | if (psy->type != POWER_SUPPLY_TYPE_BATTERY) { |
297 | if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret)) | |
298 | return 0; | |
299 | if (ret.intval) | |
300 | return ret.intval; | |
301 | } | |
302 | return 0; | |
303 | } | |
304 | ||
305 | int power_supply_is_system_supplied(void) | |
306 | { | |
307 | int error; | |
2530daa1 | 308 | unsigned int count = 0; |
942ed161 | 309 | |
2530daa1 | 310 | error = class_for_each_device(power_supply_class, NULL, &count, |
942ed161 MG |
311 | __power_supply_is_system_supplied); |
312 | ||
2530daa1 JD |
313 | /* |
314 | * If no power class device was found at all, most probably we are | |
315 | * running on a desktop system, so assume we are on mains power. | |
316 | */ | |
317 | if (count == 0) | |
318 | return 1; | |
319 | ||
942ed161 MG |
320 | return error; |
321 | } | |
ff3417e7 | 322 | EXPORT_SYMBOL_GPL(power_supply_is_system_supplied); |
942ed161 | 323 | |
e5f5ccb6 DM |
324 | int power_supply_set_battery_charged(struct power_supply *psy) |
325 | { | |
326 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) { | |
327 | psy->set_charged(psy); | |
328 | return 0; | |
329 | } | |
330 | ||
331 | return -EINVAL; | |
332 | } | |
333 | EXPORT_SYMBOL_GPL(power_supply_set_battery_charged); | |
334 | ||
9f3b795a | 335 | static int power_supply_match_device_by_name(struct device *dev, const void *data) |
e5f5ccb6 DM |
336 | { |
337 | const char *name = data; | |
338 | struct power_supply *psy = dev_get_drvdata(dev); | |
339 | ||
340 | return strcmp(psy->name, name) == 0; | |
341 | } | |
342 | ||
9f3b795a | 343 | struct power_supply *power_supply_get_by_name(const char *name) |
e5f5ccb6 DM |
344 | { |
345 | struct device *dev = class_find_device(power_supply_class, NULL, name, | |
346 | power_supply_match_device_by_name); | |
347 | ||
348 | return dev ? dev_get_drvdata(dev) : NULL; | |
349 | } | |
350 | EXPORT_SYMBOL_GPL(power_supply_get_by_name); | |
351 | ||
abce9770 SR |
352 | #ifdef CONFIG_OF |
353 | static int power_supply_match_device_node(struct device *dev, const void *data) | |
354 | { | |
355 | return dev->parent && dev->parent->of_node == data; | |
356 | } | |
357 | ||
358 | struct power_supply *power_supply_get_by_phandle(struct device_node *np, | |
359 | const char *property) | |
360 | { | |
361 | struct device_node *power_supply_np; | |
362 | struct device *dev; | |
363 | ||
364 | power_supply_np = of_parse_phandle(np, property, 0); | |
365 | if (!power_supply_np) | |
366 | return ERR_PTR(-ENODEV); | |
367 | ||
368 | dev = class_find_device(power_supply_class, NULL, power_supply_np, | |
369 | power_supply_match_device_node); | |
370 | ||
371 | of_node_put(power_supply_np); | |
372 | ||
373 | return dev ? dev_get_drvdata(dev) : NULL; | |
374 | } | |
375 | EXPORT_SYMBOL_GPL(power_supply_get_by_phandle); | |
376 | #endif /* CONFIG_OF */ | |
377 | ||
83516651 JF |
378 | int power_supply_powers(struct power_supply *psy, struct device *dev) |
379 | { | |
93278d15 | 380 | return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers"); |
83516651 JF |
381 | } |
382 | EXPORT_SYMBOL_GPL(power_supply_powers); | |
383 | ||
5f487cd3 AV |
384 | static void power_supply_dev_release(struct device *dev) |
385 | { | |
386 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); | |
387 | kfree(dev); | |
388 | } | |
389 | ||
d36240d2 PR |
390 | int power_supply_reg_notifier(struct notifier_block *nb) |
391 | { | |
392 | return atomic_notifier_chain_register(&power_supply_notifier, nb); | |
393 | } | |
394 | EXPORT_SYMBOL_GPL(power_supply_reg_notifier); | |
395 | ||
396 | void power_supply_unreg_notifier(struct notifier_block *nb) | |
397 | { | |
398 | atomic_notifier_chain_unregister(&power_supply_notifier, nb); | |
399 | } | |
400 | EXPORT_SYMBOL_GPL(power_supply_unreg_notifier); | |
401 | ||
3be330bf JT |
402 | #ifdef CONFIG_THERMAL |
403 | static int power_supply_read_temp(struct thermal_zone_device *tzd, | |
404 | unsigned long *temp) | |
405 | { | |
406 | struct power_supply *psy; | |
407 | union power_supply_propval val; | |
408 | int ret; | |
409 | ||
410 | WARN_ON(tzd == NULL); | |
411 | psy = tzd->devdata; | |
412 | ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val); | |
413 | ||
414 | /* Convert tenths of degree Celsius to milli degree Celsius. */ | |
415 | if (!ret) | |
416 | *temp = val.intval * 100; | |
417 | ||
418 | return ret; | |
419 | } | |
420 | ||
421 | static struct thermal_zone_device_ops psy_tzd_ops = { | |
422 | .get_temp = power_supply_read_temp, | |
423 | }; | |
424 | ||
425 | static int psy_register_thermal(struct power_supply *psy) | |
426 | { | |
427 | int i; | |
428 | ||
429 | /* Register battery zone device psy reports temperature */ | |
430 | for (i = 0; i < psy->num_properties; i++) { | |
431 | if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) { | |
e6db06a5 | 432 | psy->tzd = thermal_zone_device_register(psy->name, 0, 0, |
50125a9b | 433 | psy, &psy_tzd_ops, NULL, 0, 0); |
3be330bf JT |
434 | if (IS_ERR(psy->tzd)) |
435 | return PTR_ERR(psy->tzd); | |
436 | break; | |
437 | } | |
438 | } | |
439 | return 0; | |
440 | } | |
441 | ||
442 | static void psy_unregister_thermal(struct power_supply *psy) | |
443 | { | |
444 | if (IS_ERR_OR_NULL(psy->tzd)) | |
445 | return; | |
446 | thermal_zone_device_unregister(psy->tzd); | |
447 | } | |
952aeeb3 RP |
448 | |
449 | /* thermal cooling device callbacks */ | |
450 | static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd, | |
451 | unsigned long *state) | |
452 | { | |
453 | struct power_supply *psy; | |
454 | union power_supply_propval val; | |
455 | int ret; | |
456 | ||
457 | psy = tcd->devdata; | |
458 | ret = psy->get_property(psy, | |
459 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val); | |
460 | if (!ret) | |
461 | *state = val.intval; | |
462 | ||
463 | return ret; | |
464 | } | |
465 | ||
466 | static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd, | |
467 | unsigned long *state) | |
468 | { | |
469 | struct power_supply *psy; | |
470 | union power_supply_propval val; | |
471 | int ret; | |
472 | ||
473 | psy = tcd->devdata; | |
474 | ret = psy->get_property(psy, | |
475 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); | |
476 | if (!ret) | |
477 | *state = val.intval; | |
478 | ||
479 | return ret; | |
480 | } | |
481 | ||
482 | static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd, | |
483 | unsigned long state) | |
484 | { | |
485 | struct power_supply *psy; | |
486 | union power_supply_propval val; | |
487 | int ret; | |
488 | ||
489 | psy = tcd->devdata; | |
490 | val.intval = state; | |
491 | ret = psy->set_property(psy, | |
492 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); | |
493 | ||
494 | return ret; | |
495 | } | |
496 | ||
497 | static struct thermal_cooling_device_ops psy_tcd_ops = { | |
498 | .get_max_state = ps_get_max_charge_cntl_limit, | |
499 | .get_cur_state = ps_get_cur_chrage_cntl_limit, | |
500 | .set_cur_state = ps_set_cur_charge_cntl_limit, | |
501 | }; | |
502 | ||
503 | static int psy_register_cooler(struct power_supply *psy) | |
504 | { | |
505 | int i; | |
506 | ||
507 | /* Register for cooling device if psy can control charging */ | |
508 | for (i = 0; i < psy->num_properties; i++) { | |
509 | if (psy->properties[i] == | |
510 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) { | |
511 | psy->tcd = thermal_cooling_device_register( | |
512 | (char *)psy->name, | |
513 | psy, &psy_tcd_ops); | |
514 | if (IS_ERR(psy->tcd)) | |
515 | return PTR_ERR(psy->tcd); | |
516 | break; | |
517 | } | |
518 | } | |
519 | return 0; | |
520 | } | |
521 | ||
522 | static void psy_unregister_cooler(struct power_supply *psy) | |
523 | { | |
524 | if (IS_ERR_OR_NULL(psy->tcd)) | |
525 | return; | |
526 | thermal_cooling_device_unregister(psy->tcd); | |
527 | } | |
3be330bf JT |
528 | #else |
529 | static int psy_register_thermal(struct power_supply *psy) | |
530 | { | |
531 | return 0; | |
532 | } | |
533 | ||
534 | static void psy_unregister_thermal(struct power_supply *psy) | |
535 | { | |
536 | } | |
952aeeb3 RP |
537 | |
538 | static int psy_register_cooler(struct power_supply *psy) | |
539 | { | |
540 | return 0; | |
541 | } | |
542 | ||
543 | static void psy_unregister_cooler(struct power_supply *psy) | |
544 | { | |
545 | } | |
3be330bf JT |
546 | #endif |
547 | ||
c128d397 WY |
548 | static int __power_supply_register(struct device *parent, |
549 | struct power_supply *psy, bool ws) | |
4a11b59d | 550 | { |
5f487cd3 AV |
551 | struct device *dev; |
552 | int rc; | |
4a11b59d | 553 | |
5f487cd3 AV |
554 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
555 | if (!dev) | |
556 | return -ENOMEM; | |
4a11b59d | 557 | |
5f487cd3 | 558 | device_initialize(dev); |
4a11b59d | 559 | |
5f487cd3 AV |
560 | dev->class = power_supply_class; |
561 | dev->type = &power_supply_dev_type; | |
562 | dev->parent = parent; | |
563 | dev->release = power_supply_dev_release; | |
564 | dev_set_drvdata(dev, psy); | |
565 | psy->dev = dev; | |
566 | ||
80c6463e SK |
567 | rc = dev_set_name(dev, "%s", psy->name); |
568 | if (rc) | |
569 | goto dev_set_name_failed; | |
570 | ||
97774672 LPC |
571 | INIT_WORK(&psy->changed_work, power_supply_changed_work); |
572 | ||
f6e0b081 RK |
573 | rc = power_supply_check_supplies(psy); |
574 | if (rc) { | |
575 | dev_info(dev, "Not all required supplies found, defer probe\n"); | |
576 | goto check_supplies_failed; | |
577 | } | |
578 | ||
948dcf96 | 579 | spin_lock_init(&psy->changed_lock); |
9113e260 | 580 | rc = device_init_wakeup(dev, ws); |
948dcf96 ZM |
581 | if (rc) |
582 | goto wakeup_init_failed; | |
583 | ||
5f487cd3 | 584 | rc = device_add(dev); |
4a11b59d | 585 | if (rc) |
5f487cd3 AV |
586 | goto device_add_failed; |
587 | ||
3be330bf JT |
588 | rc = psy_register_thermal(psy); |
589 | if (rc) | |
590 | goto register_thermal_failed; | |
591 | ||
952aeeb3 RP |
592 | rc = psy_register_cooler(psy); |
593 | if (rc) | |
594 | goto register_cooler_failed; | |
595 | ||
4a11b59d AV |
596 | rc = power_supply_create_triggers(psy); |
597 | if (rc) | |
598 | goto create_triggers_failed; | |
599 | ||
600 | power_supply_changed(psy); | |
601 | ||
602 | goto success; | |
603 | ||
604 | create_triggers_failed: | |
952aeeb3 RP |
605 | psy_unregister_cooler(psy); |
606 | register_cooler_failed: | |
3be330bf JT |
607 | psy_unregister_thermal(psy); |
608 | register_thermal_failed: | |
3a2dbd61 | 609 | device_del(dev); |
5f487cd3 | 610 | device_add_failed: |
80c6463e | 611 | wakeup_init_failed: |
f6e0b081 | 612 | check_supplies_failed: |
80c6463e | 613 | dev_set_name_failed: |
3a2dbd61 | 614 | put_device(dev); |
4a11b59d AV |
615 | success: |
616 | return rc; | |
617 | } | |
9113e260 ZR |
618 | |
619 | int power_supply_register(struct device *parent, struct power_supply *psy) | |
620 | { | |
621 | return __power_supply_register(parent, psy, true); | |
622 | } | |
ff3417e7 | 623 | EXPORT_SYMBOL_GPL(power_supply_register); |
4a11b59d | 624 | |
9113e260 ZR |
625 | int power_supply_register_no_ws(struct device *parent, struct power_supply *psy) |
626 | { | |
627 | return __power_supply_register(parent, psy, false); | |
628 | } | |
629 | EXPORT_SYMBOL_GPL(power_supply_register_no_ws); | |
630 | ||
4a11b59d AV |
631 | void power_supply_unregister(struct power_supply *psy) |
632 | { | |
bc51e7ff | 633 | cancel_work_sync(&psy->changed_work); |
83516651 | 634 | sysfs_remove_link(&psy->dev->kobj, "powers"); |
4a11b59d | 635 | power_supply_remove_triggers(psy); |
952aeeb3 | 636 | psy_unregister_cooler(psy); |
3be330bf | 637 | psy_unregister_thermal(psy); |
948dcf96 | 638 | device_init_wakeup(psy->dev, false); |
4a11b59d | 639 | device_unregister(psy->dev); |
4a11b59d | 640 | } |
ff3417e7 | 641 | EXPORT_SYMBOL_GPL(power_supply_unregister); |
4a11b59d AV |
642 | |
643 | static int __init power_supply_class_init(void) | |
644 | { | |
645 | power_supply_class = class_create(THIS_MODULE, "power_supply"); | |
646 | ||
647 | if (IS_ERR(power_supply_class)) | |
648 | return PTR_ERR(power_supply_class); | |
649 | ||
650 | power_supply_class->dev_uevent = power_supply_uevent; | |
5f487cd3 | 651 | power_supply_init_attrs(&power_supply_dev_type); |
4a11b59d AV |
652 | |
653 | return 0; | |
654 | } | |
655 | ||
656 | static void __exit power_supply_class_exit(void) | |
657 | { | |
658 | class_destroy(power_supply_class); | |
4a11b59d AV |
659 | } |
660 | ||
4a11b59d AV |
661 | subsys_initcall(power_supply_class_init); |
662 | module_exit(power_supply_class_exit); | |
663 | ||
664 | MODULE_DESCRIPTION("Universal power supply monitor class"); | |
665 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " | |
666 | "Szabolcs Gyurko, " | |
667 | "Anton Vorontsov <cbou@mail.ru>"); | |
668 | MODULE_LICENSE("GPL"); |