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 | 267 | if (__power_supply_is_supplied_by(epsy, psy)) |
1c42a389 VK |
268 | if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret)) |
269 | return ret.intval; | |
5e0848c6 | 270 | |
443cad92 DY |
271 | return 0; |
272 | } | |
273 | ||
274 | int power_supply_am_i_supplied(struct power_supply *psy) | |
275 | { | |
276 | int error; | |
277 | ||
93562b53 | 278 | error = class_for_each_device(power_supply_class, NULL, psy, |
443cad92 | 279 | __power_supply_am_i_supplied); |
4a11b59d | 280 | |
0cddc0a9 | 281 | dev_dbg(psy->dev, "%s %d\n", __func__, error); |
4a11b59d | 282 | |
443cad92 | 283 | return error; |
4a11b59d | 284 | } |
ff3417e7 | 285 | EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); |
4a11b59d | 286 | |
942ed161 MG |
287 | static int __power_supply_is_system_supplied(struct device *dev, void *data) |
288 | { | |
289 | union power_supply_propval ret = {0,}; | |
290 | struct power_supply *psy = dev_get_drvdata(dev); | |
2530daa1 | 291 | unsigned int *count = data; |
942ed161 | 292 | |
2530daa1 | 293 | (*count)++; |
1c42a389 VK |
294 | if (psy->type != POWER_SUPPLY_TYPE_BATTERY) |
295 | if (!psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret)) | |
942ed161 | 296 | return ret.intval; |
1c42a389 | 297 | |
942ed161 MG |
298 | return 0; |
299 | } | |
300 | ||
301 | int power_supply_is_system_supplied(void) | |
302 | { | |
303 | int error; | |
2530daa1 | 304 | unsigned int count = 0; |
942ed161 | 305 | |
2530daa1 | 306 | error = class_for_each_device(power_supply_class, NULL, &count, |
942ed161 MG |
307 | __power_supply_is_system_supplied); |
308 | ||
2530daa1 JD |
309 | /* |
310 | * If no power class device was found at all, most probably we are | |
311 | * running on a desktop system, so assume we are on mains power. | |
312 | */ | |
313 | if (count == 0) | |
314 | return 1; | |
315 | ||
942ed161 MG |
316 | return error; |
317 | } | |
ff3417e7 | 318 | EXPORT_SYMBOL_GPL(power_supply_is_system_supplied); |
942ed161 | 319 | |
e5f5ccb6 DM |
320 | int power_supply_set_battery_charged(struct power_supply *psy) |
321 | { | |
322 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) { | |
323 | psy->set_charged(psy); | |
324 | return 0; | |
325 | } | |
326 | ||
327 | return -EINVAL; | |
328 | } | |
329 | EXPORT_SYMBOL_GPL(power_supply_set_battery_charged); | |
330 | ||
9f3b795a | 331 | static int power_supply_match_device_by_name(struct device *dev, const void *data) |
e5f5ccb6 DM |
332 | { |
333 | const char *name = data; | |
334 | struct power_supply *psy = dev_get_drvdata(dev); | |
335 | ||
336 | return strcmp(psy->name, name) == 0; | |
337 | } | |
338 | ||
9f3b795a | 339 | struct power_supply *power_supply_get_by_name(const char *name) |
e5f5ccb6 DM |
340 | { |
341 | struct device *dev = class_find_device(power_supply_class, NULL, name, | |
342 | power_supply_match_device_by_name); | |
343 | ||
344 | return dev ? dev_get_drvdata(dev) : NULL; | |
345 | } | |
346 | EXPORT_SYMBOL_GPL(power_supply_get_by_name); | |
347 | ||
abce9770 SR |
348 | #ifdef CONFIG_OF |
349 | static int power_supply_match_device_node(struct device *dev, const void *data) | |
350 | { | |
351 | return dev->parent && dev->parent->of_node == data; | |
352 | } | |
353 | ||
354 | struct power_supply *power_supply_get_by_phandle(struct device_node *np, | |
355 | const char *property) | |
356 | { | |
357 | struct device_node *power_supply_np; | |
358 | struct device *dev; | |
359 | ||
360 | power_supply_np = of_parse_phandle(np, property, 0); | |
361 | if (!power_supply_np) | |
362 | return ERR_PTR(-ENODEV); | |
363 | ||
364 | dev = class_find_device(power_supply_class, NULL, power_supply_np, | |
365 | power_supply_match_device_node); | |
366 | ||
367 | of_node_put(power_supply_np); | |
368 | ||
369 | return dev ? dev_get_drvdata(dev) : NULL; | |
370 | } | |
371 | EXPORT_SYMBOL_GPL(power_supply_get_by_phandle); | |
372 | #endif /* CONFIG_OF */ | |
373 | ||
83516651 JF |
374 | int power_supply_powers(struct power_supply *psy, struct device *dev) |
375 | { | |
93278d15 | 376 | return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers"); |
83516651 JF |
377 | } |
378 | EXPORT_SYMBOL_GPL(power_supply_powers); | |
379 | ||
5f487cd3 AV |
380 | static void power_supply_dev_release(struct device *dev) |
381 | { | |
382 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); | |
383 | kfree(dev); | |
384 | } | |
385 | ||
d36240d2 PR |
386 | int power_supply_reg_notifier(struct notifier_block *nb) |
387 | { | |
388 | return atomic_notifier_chain_register(&power_supply_notifier, nb); | |
389 | } | |
390 | EXPORT_SYMBOL_GPL(power_supply_reg_notifier); | |
391 | ||
392 | void power_supply_unreg_notifier(struct notifier_block *nb) | |
393 | { | |
394 | atomic_notifier_chain_unregister(&power_supply_notifier, nb); | |
395 | } | |
396 | EXPORT_SYMBOL_GPL(power_supply_unreg_notifier); | |
397 | ||
3be330bf JT |
398 | #ifdef CONFIG_THERMAL |
399 | static int power_supply_read_temp(struct thermal_zone_device *tzd, | |
400 | unsigned long *temp) | |
401 | { | |
402 | struct power_supply *psy; | |
403 | union power_supply_propval val; | |
404 | int ret; | |
405 | ||
406 | WARN_ON(tzd == NULL); | |
407 | psy = tzd->devdata; | |
408 | ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val); | |
409 | ||
410 | /* Convert tenths of degree Celsius to milli degree Celsius. */ | |
411 | if (!ret) | |
412 | *temp = val.intval * 100; | |
413 | ||
414 | return ret; | |
415 | } | |
416 | ||
417 | static struct thermal_zone_device_ops psy_tzd_ops = { | |
418 | .get_temp = power_supply_read_temp, | |
419 | }; | |
420 | ||
421 | static int psy_register_thermal(struct power_supply *psy) | |
422 | { | |
423 | int i; | |
424 | ||
425 | /* Register battery zone device psy reports temperature */ | |
426 | for (i = 0; i < psy->num_properties; i++) { | |
427 | if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) { | |
e6db06a5 | 428 | psy->tzd = thermal_zone_device_register(psy->name, 0, 0, |
50125a9b | 429 | psy, &psy_tzd_ops, NULL, 0, 0); |
3be330bf JT |
430 | if (IS_ERR(psy->tzd)) |
431 | return PTR_ERR(psy->tzd); | |
432 | break; | |
433 | } | |
434 | } | |
435 | return 0; | |
436 | } | |
437 | ||
438 | static void psy_unregister_thermal(struct power_supply *psy) | |
439 | { | |
440 | if (IS_ERR_OR_NULL(psy->tzd)) | |
441 | return; | |
442 | thermal_zone_device_unregister(psy->tzd); | |
443 | } | |
952aeeb3 RP |
444 | |
445 | /* thermal cooling device callbacks */ | |
446 | static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd, | |
447 | unsigned long *state) | |
448 | { | |
449 | struct power_supply *psy; | |
450 | union power_supply_propval val; | |
451 | int ret; | |
452 | ||
453 | psy = tcd->devdata; | |
454 | ret = psy->get_property(psy, | |
455 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val); | |
456 | if (!ret) | |
457 | *state = val.intval; | |
458 | ||
459 | return ret; | |
460 | } | |
461 | ||
462 | static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd, | |
463 | unsigned long *state) | |
464 | { | |
465 | struct power_supply *psy; | |
466 | union power_supply_propval val; | |
467 | int ret; | |
468 | ||
469 | psy = tcd->devdata; | |
470 | ret = psy->get_property(psy, | |
471 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); | |
472 | if (!ret) | |
473 | *state = val.intval; | |
474 | ||
475 | return ret; | |
476 | } | |
477 | ||
478 | static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd, | |
479 | unsigned long state) | |
480 | { | |
481 | struct power_supply *psy; | |
482 | union power_supply_propval val; | |
483 | int ret; | |
484 | ||
485 | psy = tcd->devdata; | |
486 | val.intval = state; | |
487 | ret = psy->set_property(psy, | |
488 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val); | |
489 | ||
490 | return ret; | |
491 | } | |
492 | ||
493 | static struct thermal_cooling_device_ops psy_tcd_ops = { | |
494 | .get_max_state = ps_get_max_charge_cntl_limit, | |
495 | .get_cur_state = ps_get_cur_chrage_cntl_limit, | |
496 | .set_cur_state = ps_set_cur_charge_cntl_limit, | |
497 | }; | |
498 | ||
499 | static int psy_register_cooler(struct power_supply *psy) | |
500 | { | |
501 | int i; | |
502 | ||
503 | /* Register for cooling device if psy can control charging */ | |
504 | for (i = 0; i < psy->num_properties; i++) { | |
505 | if (psy->properties[i] == | |
506 | POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) { | |
507 | psy->tcd = thermal_cooling_device_register( | |
508 | (char *)psy->name, | |
509 | psy, &psy_tcd_ops); | |
510 | if (IS_ERR(psy->tcd)) | |
511 | return PTR_ERR(psy->tcd); | |
512 | break; | |
513 | } | |
514 | } | |
515 | return 0; | |
516 | } | |
517 | ||
518 | static void psy_unregister_cooler(struct power_supply *psy) | |
519 | { | |
520 | if (IS_ERR_OR_NULL(psy->tcd)) | |
521 | return; | |
522 | thermal_cooling_device_unregister(psy->tcd); | |
523 | } | |
3be330bf JT |
524 | #else |
525 | static int psy_register_thermal(struct power_supply *psy) | |
526 | { | |
527 | return 0; | |
528 | } | |
529 | ||
530 | static void psy_unregister_thermal(struct power_supply *psy) | |
531 | { | |
532 | } | |
952aeeb3 RP |
533 | |
534 | static int psy_register_cooler(struct power_supply *psy) | |
535 | { | |
536 | return 0; | |
537 | } | |
538 | ||
539 | static void psy_unregister_cooler(struct power_supply *psy) | |
540 | { | |
541 | } | |
3be330bf JT |
542 | #endif |
543 | ||
c128d397 WY |
544 | static int __power_supply_register(struct device *parent, |
545 | struct power_supply *psy, bool ws) | |
4a11b59d | 546 | { |
5f487cd3 AV |
547 | struct device *dev; |
548 | int rc; | |
4a11b59d | 549 | |
5f487cd3 AV |
550 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
551 | if (!dev) | |
552 | return -ENOMEM; | |
4a11b59d | 553 | |
5f487cd3 | 554 | device_initialize(dev); |
4a11b59d | 555 | |
5f487cd3 AV |
556 | dev->class = power_supply_class; |
557 | dev->type = &power_supply_dev_type; | |
558 | dev->parent = parent; | |
559 | dev->release = power_supply_dev_release; | |
560 | dev_set_drvdata(dev, psy); | |
561 | psy->dev = dev; | |
562 | ||
80c6463e SK |
563 | rc = dev_set_name(dev, "%s", psy->name); |
564 | if (rc) | |
565 | goto dev_set_name_failed; | |
566 | ||
97774672 LPC |
567 | INIT_WORK(&psy->changed_work, power_supply_changed_work); |
568 | ||
f6e0b081 RK |
569 | rc = power_supply_check_supplies(psy); |
570 | if (rc) { | |
571 | dev_info(dev, "Not all required supplies found, defer probe\n"); | |
572 | goto check_supplies_failed; | |
573 | } | |
574 | ||
948dcf96 | 575 | spin_lock_init(&psy->changed_lock); |
9113e260 | 576 | rc = device_init_wakeup(dev, ws); |
948dcf96 ZM |
577 | if (rc) |
578 | goto wakeup_init_failed; | |
579 | ||
5f487cd3 | 580 | rc = device_add(dev); |
4a11b59d | 581 | if (rc) |
5f487cd3 AV |
582 | goto device_add_failed; |
583 | ||
3be330bf JT |
584 | rc = psy_register_thermal(psy); |
585 | if (rc) | |
586 | goto register_thermal_failed; | |
587 | ||
952aeeb3 RP |
588 | rc = psy_register_cooler(psy); |
589 | if (rc) | |
590 | goto register_cooler_failed; | |
591 | ||
4a11b59d AV |
592 | rc = power_supply_create_triggers(psy); |
593 | if (rc) | |
594 | goto create_triggers_failed; | |
595 | ||
596 | power_supply_changed(psy); | |
597 | ||
598 | goto success; | |
599 | ||
600 | create_triggers_failed: | |
952aeeb3 RP |
601 | psy_unregister_cooler(psy); |
602 | register_cooler_failed: | |
3be330bf JT |
603 | psy_unregister_thermal(psy); |
604 | register_thermal_failed: | |
3a2dbd61 | 605 | device_del(dev); |
5f487cd3 | 606 | device_add_failed: |
80c6463e | 607 | wakeup_init_failed: |
f6e0b081 | 608 | check_supplies_failed: |
80c6463e | 609 | dev_set_name_failed: |
3a2dbd61 | 610 | put_device(dev); |
4a11b59d AV |
611 | success: |
612 | return rc; | |
613 | } | |
9113e260 ZR |
614 | |
615 | int power_supply_register(struct device *parent, struct power_supply *psy) | |
616 | { | |
617 | return __power_supply_register(parent, psy, true); | |
618 | } | |
ff3417e7 | 619 | EXPORT_SYMBOL_GPL(power_supply_register); |
4a11b59d | 620 | |
9113e260 ZR |
621 | int power_supply_register_no_ws(struct device *parent, struct power_supply *psy) |
622 | { | |
623 | return __power_supply_register(parent, psy, false); | |
624 | } | |
625 | EXPORT_SYMBOL_GPL(power_supply_register_no_ws); | |
626 | ||
4a11b59d AV |
627 | void power_supply_unregister(struct power_supply *psy) |
628 | { | |
bc51e7ff | 629 | cancel_work_sync(&psy->changed_work); |
83516651 | 630 | sysfs_remove_link(&psy->dev->kobj, "powers"); |
4a11b59d | 631 | power_supply_remove_triggers(psy); |
952aeeb3 | 632 | psy_unregister_cooler(psy); |
3be330bf | 633 | psy_unregister_thermal(psy); |
948dcf96 | 634 | device_init_wakeup(psy->dev, false); |
4a11b59d | 635 | device_unregister(psy->dev); |
4a11b59d | 636 | } |
ff3417e7 | 637 | EXPORT_SYMBOL_GPL(power_supply_unregister); |
4a11b59d AV |
638 | |
639 | static int __init power_supply_class_init(void) | |
640 | { | |
641 | power_supply_class = class_create(THIS_MODULE, "power_supply"); | |
642 | ||
643 | if (IS_ERR(power_supply_class)) | |
644 | return PTR_ERR(power_supply_class); | |
645 | ||
646 | power_supply_class->dev_uevent = power_supply_uevent; | |
5f487cd3 | 647 | power_supply_init_attrs(&power_supply_dev_type); |
4a11b59d AV |
648 | |
649 | return 0; | |
650 | } | |
651 | ||
652 | static void __exit power_supply_class_exit(void) | |
653 | { | |
654 | class_destroy(power_supply_class); | |
4a11b59d AV |
655 | } |
656 | ||
4a11b59d AV |
657 | subsys_initcall(power_supply_class_init); |
658 | module_exit(power_supply_class_exit); | |
659 | ||
660 | MODULE_DESCRIPTION("Universal power supply monitor class"); | |
661 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " | |
662 | "Szabolcs Gyurko, " | |
663 | "Anton Vorontsov <cbou@mail.ru>"); | |
664 | MODULE_LICENSE("GPL"); |