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