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