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