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