Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[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 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 25struct class *power_supply_class;
ff3417e7 26EXPORT_SYMBOL_GPL(power_supply_class);
4a11b59d 27
d36240d2
PR
28ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
29EXPORT_SYMBOL_GPL(power_supply_notifier);
30
5f487cd3
AV
31static struct device_type power_supply_dev_type;
32
7f1a57fd
KK
33#define POWER_SUPPLY_DEFERRED_REGISTER_TIME msecs_to_jiffies(10)
34
5e0848c6
RK
35static bool __power_supply_is_supplied_by(struct power_supply *supplier,
36 struct power_supply *supply)
37{
38 int i;
39
40 if (!supply->supplied_from && !supplier->supplied_to)
41 return false;
42
43 /* Support both supplied_to and supplied_from modes */
44 if (supply->supplied_from) {
297d716f 45 if (!supplier->desc->name)
5e0848c6
RK
46 return false;
47 for (i = 0; i < supply->num_supplies; i++)
297d716f 48 if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
5e0848c6
RK
49 return true;
50 } else {
297d716f 51 if (!supply->desc->name)
5e0848c6
RK
52 return false;
53 for (i = 0; i < supplier->num_supplicants; i++)
297d716f 54 if (!strcmp(supplier->supplied_to[i], supply->desc->name))
5e0848c6
RK
55 return true;
56 }
57
58 return false;
59}
60
443cad92
DY
61static int __power_supply_changed_work(struct device *dev, void *data)
62{
e80cf421 63 struct power_supply *psy = data;
443cad92 64 struct power_supply *pst = dev_get_drvdata(dev);
443cad92 65
5e0848c6 66 if (__power_supply_is_supplied_by(psy, pst)) {
297d716f
KK
67 if (pst->desc->external_power_changed)
68 pst->desc->external_power_changed(pst);
5e0848c6
RK
69 }
70
443cad92
DY
71 return 0;
72}
73
4a11b59d
AV
74static void power_supply_changed_work(struct work_struct *work)
75{
948dcf96 76 unsigned long flags;
4a11b59d
AV
77 struct power_supply *psy = container_of(work, struct power_supply,
78 changed_work);
4a11b59d 79
297d716f 80 dev_dbg(&psy->dev, "%s\n", __func__);
4a11b59d 81
948dcf96 82 spin_lock_irqsave(&psy->changed_lock, flags);
061f3806
VK
83 /*
84 * Check 'changed' here to avoid issues due to race between
85 * power_supply_changed() and this routine. In worst case
86 * power_supply_changed() can be called again just before we take above
87 * lock. During the first call of this routine we will mark 'changed' as
88 * false and it will stay false for the next call as well.
89 */
90 if (likely(psy->changed)) {
948dcf96
ZM
91 psy->changed = false;
92 spin_unlock_irqrestore(&psy->changed_lock, flags);
93 class_for_each_device(power_supply_class, NULL, psy,
94 __power_supply_changed_work);
95 power_supply_update_leds(psy);
d36240d2
PR
96 atomic_notifier_call_chain(&power_supply_notifier,
97 PSY_EVENT_PROP_CHANGED, psy);
297d716f 98 kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
948dcf96
ZM
99 spin_lock_irqsave(&psy->changed_lock, flags);
100 }
061f3806 101
948dcf96 102 /*
061f3806
VK
103 * Hold the wakeup_source until all events are processed.
104 * power_supply_changed() might have called again and have set 'changed'
105 * to true.
948dcf96 106 */
061f3806 107 if (likely(!psy->changed))
297d716f 108 pm_relax(&psy->dev);
948dcf96 109 spin_unlock_irqrestore(&psy->changed_lock, flags);
4a11b59d
AV
110}
111
112void power_supply_changed(struct power_supply *psy)
113{
948dcf96
ZM
114 unsigned long flags;
115
297d716f 116 dev_dbg(&psy->dev, "%s\n", __func__);
4a11b59d 117
948dcf96
ZM
118 spin_lock_irqsave(&psy->changed_lock, flags);
119 psy->changed = true;
297d716f 120 pm_stay_awake(&psy->dev);
948dcf96 121 spin_unlock_irqrestore(&psy->changed_lock, flags);
4a11b59d 122 schedule_work(&psy->changed_work);
4a11b59d 123}
ff3417e7 124EXPORT_SYMBOL_GPL(power_supply_changed);
4a11b59d 125
7f1a57fd
KK
126/*
127 * Notify that power supply was registered after parent finished the probing.
128 *
129 * Often power supply is registered from driver's probe function. However
130 * calling power_supply_changed() directly from power_supply_register()
131 * would lead to execution of get_property() function provided by the driver
132 * too early - before the probe ends.
133 *
134 * Avoid that by waiting on parent's mutex.
135 */
136static void power_supply_deferred_register_work(struct work_struct *work)
137{
138 struct power_supply *psy = container_of(work, struct power_supply,
139 deferred_register_work.work);
140
141 if (psy->dev.parent)
142 mutex_lock(&psy->dev.parent->mutex);
143
144 power_supply_changed(psy);
145
146 if (psy->dev.parent)
147 mutex_unlock(&psy->dev.parent->mutex);
148}
149
f6e0b081
RK
150#ifdef CONFIG_OF
151#include <linux/of.h>
152
153static int __power_supply_populate_supplied_from(struct device *dev,
154 void *data)
155{
e80cf421 156 struct power_supply *psy = data;
f6e0b081
RK
157 struct power_supply *epsy = dev_get_drvdata(dev);
158 struct device_node *np;
159 int i = 0;
160
161 do {
162 np = of_parse_phandle(psy->of_node, "power-supplies", i++);
163 if (!np)
a0f93b42 164 break;
f6e0b081
RK
165
166 if (np == epsy->of_node) {
297d716f
KK
167 dev_info(&psy->dev, "%s: Found supply : %s\n",
168 psy->desc->name, epsy->desc->name);
169 psy->supplied_from[i-1] = (char *)epsy->desc->name;
f6e0b081 170 psy->num_supplies++;
2054d6e9 171 of_node_put(np);
f6e0b081
RK
172 break;
173 }
2054d6e9 174 of_node_put(np);
f6e0b081
RK
175 } while (np);
176
177 return 0;
178}
179
180static int power_supply_populate_supplied_from(struct power_supply *psy)
181{
182 int error;
183
184 error = class_for_each_device(power_supply_class, NULL, psy,
185 __power_supply_populate_supplied_from);
186
297d716f 187 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
f6e0b081
RK
188
189 return error;
190}
191
192static int __power_supply_find_supply_from_node(struct device *dev,
193 void *data)
194{
e80cf421 195 struct device_node *np = data;
f6e0b081
RK
196 struct power_supply *epsy = dev_get_drvdata(dev);
197
585b0087 198 /* returning non-zero breaks out of class_for_each_device loop */
f6e0b081 199 if (epsy->of_node == np)
585b0087 200 return 1;
f6e0b081
RK
201
202 return 0;
203}
204
205static int power_supply_find_supply_from_node(struct device_node *supply_node)
206{
207 int error;
f6e0b081
RK
208
209 /*
585b0087
VK
210 * class_for_each_device() either returns its own errors or values
211 * returned by __power_supply_find_supply_from_node().
212 *
213 * __power_supply_find_supply_from_node() will return 0 (no match)
214 * or 1 (match).
215 *
216 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
217 * it returned 0, or error as returned by it.
f6e0b081
RK
218 */
219 error = class_for_each_device(power_supply_class, NULL, supply_node,
220 __power_supply_find_supply_from_node);
221
585b0087 222 return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
f6e0b081
RK
223}
224
225static int power_supply_check_supplies(struct power_supply *psy)
226{
227 struct device_node *np;
228 int cnt = 0;
229
230 /* If there is already a list honor it */
231 if (psy->supplied_from && psy->num_supplies > 0)
232 return 0;
233
234 /* No device node found, nothing to do */
235 if (!psy->of_node)
236 return 0;
237
238 do {
239 int ret;
240
241 np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
242 if (!np)
a0f93b42 243 break;
f6e0b081
RK
244
245 ret = power_supply_find_supply_from_node(np);
8468b029
VK
246 of_node_put(np);
247
f6e0b081 248 if (ret) {
297d716f 249 dev_dbg(&psy->dev, "Failed to find supply!\n");
f5b89aff 250 return ret;
f6e0b081
RK
251 }
252 } while (np);
253
f9c85486
VK
254 /* Missing valid "power-supplies" entries */
255 if (cnt == 1)
256 return 0;
257
f6e0b081 258 /* All supplies found, allocate char ** array for filling */
297d716f 259 psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
f6e0b081
RK
260 GFP_KERNEL);
261 if (!psy->supplied_from) {
297d716f 262 dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
f6e0b081
RK
263 return -ENOMEM;
264 }
265
297d716f
KK
266 *psy->supplied_from = devm_kzalloc(&psy->dev,
267 sizeof(char *) * (cnt - 1),
f6e0b081
RK
268 GFP_KERNEL);
269 if (!*psy->supplied_from) {
297d716f 270 dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
f6e0b081
RK
271 return -ENOMEM;
272 }
273
274 return power_supply_populate_supplied_from(psy);
275}
276#else
277static inline int power_supply_check_supplies(struct power_supply *psy)
278{
279 return 0;
280}
281#endif
282
443cad92 283static int __power_supply_am_i_supplied(struct device *dev, void *data)
4a11b59d
AV
284{
285 union power_supply_propval ret = {0,};
e80cf421 286 struct power_supply *psy = data;
443cad92 287 struct power_supply *epsy = dev_get_drvdata(dev);
443cad92 288
5e0848c6 289 if (__power_supply_is_supplied_by(epsy, psy))
297d716f
KK
290 if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
291 &ret))
1c42a389 292 return ret.intval;
5e0848c6 293
443cad92
DY
294 return 0;
295}
296
297int power_supply_am_i_supplied(struct power_supply *psy)
298{
299 int error;
300
93562b53 301 error = class_for_each_device(power_supply_class, NULL, psy,
443cad92 302 __power_supply_am_i_supplied);
4a11b59d 303
297d716f 304 dev_dbg(&psy->dev, "%s %d\n", __func__, error);
4a11b59d 305
443cad92 306 return error;
4a11b59d 307}
ff3417e7 308EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
4a11b59d 309
942ed161
MG
310static int __power_supply_is_system_supplied(struct device *dev, void *data)
311{
312 union power_supply_propval ret = {0,};
313 struct power_supply *psy = dev_get_drvdata(dev);
2530daa1 314 unsigned int *count = data;
942ed161 315
2530daa1 316 (*count)++;
297d716f
KK
317 if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
318 if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
319 &ret))
942ed161 320 return ret.intval;
1c42a389 321
942ed161
MG
322 return 0;
323}
324
325int power_supply_is_system_supplied(void)
326{
327 int error;
2530daa1 328 unsigned int count = 0;
942ed161 329
2530daa1 330 error = class_for_each_device(power_supply_class, NULL, &count,
942ed161
MG
331 __power_supply_is_system_supplied);
332
2530daa1
JD
333 /*
334 * If no power class device was found at all, most probably we are
335 * running on a desktop system, so assume we are on mains power.
336 */
337 if (count == 0)
338 return 1;
339
942ed161
MG
340 return error;
341}
ff3417e7 342EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
942ed161 343
e5f5ccb6
DM
344int power_supply_set_battery_charged(struct power_supply *psy)
345{
bc154056 346 if (atomic_read(&psy->use_cnt) >= 0 &&
297d716f
KK
347 psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
348 psy->desc->set_charged) {
349 psy->desc->set_charged(psy);
e5f5ccb6
DM
350 return 0;
351 }
352
353 return -EINVAL;
354}
355EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
356
9f3b795a 357static int power_supply_match_device_by_name(struct device *dev, const void *data)
e5f5ccb6
DM
358{
359 const char *name = data;
360 struct power_supply *psy = dev_get_drvdata(dev);
361
297d716f 362 return strcmp(psy->desc->name, name) == 0;
e5f5ccb6
DM
363}
364
1a352462
KK
365/**
366 * power_supply_get_by_name() - Search for a power supply and returns its ref
367 * @name: Power supply name to fetch
368 *
369 * If power supply was found, it increases reference count for the
370 * internal power supply's device. The user should power_supply_put()
371 * after usage.
372 *
373 * Return: On success returns a reference to a power supply with
374 * matching name equals to @name, a NULL otherwise.
375 */
9f3b795a 376struct power_supply *power_supply_get_by_name(const char *name)
e5f5ccb6 377{
03e81acc 378 struct power_supply *psy = NULL;
e5f5ccb6
DM
379 struct device *dev = class_find_device(power_supply_class, NULL, name,
380 power_supply_match_device_by_name);
381
03e81acc
KK
382 if (dev) {
383 psy = dev_get_drvdata(dev);
384 atomic_inc(&psy->use_cnt);
385 }
386
387 return psy;
e5f5ccb6
DM
388}
389EXPORT_SYMBOL_GPL(power_supply_get_by_name);
390
1a352462
KK
391/**
392 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
393 * @psy: Reference to put
394 *
395 * The reference to power supply should be put before unregistering
396 * the power supply.
397 */
398void power_supply_put(struct power_supply *psy)
399{
400 might_sleep();
401
03e81acc 402 atomic_dec(&psy->use_cnt);
1a352462
KK
403 put_device(&psy->dev);
404}
405EXPORT_SYMBOL_GPL(power_supply_put);
406
abce9770
SR
407#ifdef CONFIG_OF
408static int power_supply_match_device_node(struct device *dev, const void *data)
409{
410 return dev->parent && dev->parent->of_node == data;
411}
412
1a352462
KK
413/**
414 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
415 * @np: Pointer to device node holding phandle property
416 * @phandle_name: Name of property holding a power supply name
417 *
418 * If power supply was found, it increases reference count for the
419 * internal power supply's device. The user should power_supply_put()
420 * after usage.
421 *
422 * Return: On success returns a reference to a power supply with
423 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
424 */
abce9770
SR
425struct power_supply *power_supply_get_by_phandle(struct device_node *np,
426 const char *property)
427{
428 struct device_node *power_supply_np;
03e81acc 429 struct power_supply *psy = NULL;
abce9770
SR
430 struct device *dev;
431
432 power_supply_np = of_parse_phandle(np, property, 0);
433 if (!power_supply_np)
434 return ERR_PTR(-ENODEV);
435
436 dev = class_find_device(power_supply_class, NULL, power_supply_np,
437 power_supply_match_device_node);
438
439 of_node_put(power_supply_np);
440
03e81acc
KK
441 if (dev) {
442 psy = dev_get_drvdata(dev);
443 atomic_inc(&psy->use_cnt);
444 }
445
446 return psy;
abce9770
SR
447}
448EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
fe27e1df
HG
449
450static void devm_power_supply_put(struct device *dev, void *res)
451{
452 struct power_supply **psy = res;
453
454 power_supply_put(*psy);
455}
456
457/**
458 * devm_power_supply_get_by_phandle() - Resource managed version of
459 * power_supply_get_by_phandle()
460 * @dev: Pointer to device holding phandle property
461 * @phandle_name: Name of property holding a power supply phandle
462 *
463 * Return: On success returns a reference to a power supply with
464 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
465 */
466struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
467 const char *property)
468{
469 struct power_supply **ptr, *psy;
470
471 if (!dev->of_node)
472 return ERR_PTR(-ENODEV);
473
474 ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
475 if (!ptr)
476 return ERR_PTR(-ENOMEM);
477
478 psy = power_supply_get_by_phandle(dev->of_node, property);
479 if (IS_ERR_OR_NULL(psy)) {
480 devres_free(ptr);
481 } else {
482 *ptr = psy;
483 devres_add(dev, ptr);
484 }
485 return psy;
486}
487EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
abce9770
SR
488#endif /* CONFIG_OF */
489
bc154056
KK
490int power_supply_get_property(struct power_supply *psy,
491 enum power_supply_property psp,
492 union power_supply_propval *val)
493{
494 if (atomic_read(&psy->use_cnt) <= 0)
495 return -ENODEV;
496
297d716f 497 return psy->desc->get_property(psy, psp, val);
bc154056
KK
498}
499EXPORT_SYMBOL_GPL(power_supply_get_property);
500
501int power_supply_set_property(struct power_supply *psy,
502 enum power_supply_property psp,
503 const union power_supply_propval *val)
504{
297d716f 505 if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
bc154056
KK
506 return -ENODEV;
507
297d716f 508 return psy->desc->set_property(psy, psp, val);
bc154056
KK
509}
510EXPORT_SYMBOL_GPL(power_supply_set_property);
511
512int power_supply_property_is_writeable(struct power_supply *psy,
513 enum power_supply_property psp)
514{
297d716f
KK
515 if (atomic_read(&psy->use_cnt) <= 0 ||
516 !psy->desc->property_is_writeable)
bc154056
KK
517 return -ENODEV;
518
297d716f 519 return psy->desc->property_is_writeable(psy, psp);
bc154056
KK
520}
521EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);
522
523void power_supply_external_power_changed(struct power_supply *psy)
524{
297d716f
KK
525 if (atomic_read(&psy->use_cnt) <= 0 ||
526 !psy->desc->external_power_changed)
bc154056
KK
527 return;
528
297d716f 529 psy->desc->external_power_changed(psy);
bc154056
KK
530}
531EXPORT_SYMBOL_GPL(power_supply_external_power_changed);
532
83516651
JF
533int power_supply_powers(struct power_supply *psy, struct device *dev)
534{
297d716f 535 return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
83516651
JF
536}
537EXPORT_SYMBOL_GPL(power_supply_powers);
538
5f487cd3
AV
539static void power_supply_dev_release(struct device *dev)
540{
297d716f 541 struct power_supply *psy = container_of(dev, struct power_supply, dev);
5f487cd3 542 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
297d716f 543 kfree(psy);
5f487cd3
AV
544}
545
d36240d2
PR
546int power_supply_reg_notifier(struct notifier_block *nb)
547{
548 return atomic_notifier_chain_register(&power_supply_notifier, nb);
549}
550EXPORT_SYMBOL_GPL(power_supply_reg_notifier);
551
552void power_supply_unreg_notifier(struct notifier_block *nb)
553{
554 atomic_notifier_chain_unregister(&power_supply_notifier, nb);
555}
556EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);
557
3be330bf
JT
558#ifdef CONFIG_THERMAL
559static int power_supply_read_temp(struct thermal_zone_device *tzd,
17e8351a 560 int *temp)
3be330bf
JT
561{
562 struct power_supply *psy;
563 union power_supply_propval val;
564 int ret;
565
566 WARN_ON(tzd == NULL);
567 psy = tzd->devdata;
5bc28b93
RK
568 ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
569 if (ret)
570 return ret;
3be330bf
JT
571
572 /* Convert tenths of degree Celsius to milli degree Celsius. */
5bc28b93 573 *temp = val.intval * 100;
3be330bf
JT
574
575 return ret;
576}
577
578static struct thermal_zone_device_ops psy_tzd_ops = {
579 .get_temp = power_supply_read_temp,
580};
581
582static int psy_register_thermal(struct power_supply *psy)
583{
584 int i;
585
297d716f 586 if (psy->desc->no_thermal)
a69d82b9
KK
587 return 0;
588
3be330bf 589 /* Register battery zone device psy reports temperature */
297d716f
KK
590 for (i = 0; i < psy->desc->num_properties; i++) {
591 if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
592 psy->tzd = thermal_zone_device_register(psy->desc->name,
593 0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
9d2410c7 594 return PTR_ERR_OR_ZERO(psy->tzd);
3be330bf
JT
595 }
596 }
597 return 0;
598}
599
600static void psy_unregister_thermal(struct power_supply *psy)
601{
602 if (IS_ERR_OR_NULL(psy->tzd))
603 return;
604 thermal_zone_device_unregister(psy->tzd);
605}
952aeeb3
RP
606
607/* thermal cooling device callbacks */
608static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
609 unsigned long *state)
610{
611 struct power_supply *psy;
612 union power_supply_propval val;
613 int ret;
614
615 psy = tcd->devdata;
5bc28b93
RK
616 ret = power_supply_get_property(psy,
617 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
618 if (ret)
619 return ret;
620
621 *state = val.intval;
952aeeb3
RP
622
623 return ret;
624}
625
626static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
627 unsigned long *state)
628{
629 struct power_supply *psy;
630 union power_supply_propval val;
631 int ret;
632
633 psy = tcd->devdata;
5bc28b93
RK
634 ret = power_supply_get_property(psy,
635 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
636 if (ret)
637 return ret;
638
639 *state = val.intval;
952aeeb3
RP
640
641 return ret;
642}
643
644static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
645 unsigned long state)
646{
647 struct power_supply *psy;
648 union power_supply_propval val;
649 int ret;
650
651 psy = tcd->devdata;
652 val.intval = state;
297d716f 653 ret = psy->desc->set_property(psy,
952aeeb3
RP
654 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
655
656 return ret;
657}
658
659static struct thermal_cooling_device_ops psy_tcd_ops = {
660 .get_max_state = ps_get_max_charge_cntl_limit,
661 .get_cur_state = ps_get_cur_chrage_cntl_limit,
662 .set_cur_state = ps_set_cur_charge_cntl_limit,
663};
664
665static int psy_register_cooler(struct power_supply *psy)
666{
667 int i;
668
669 /* Register for cooling device if psy can control charging */
297d716f
KK
670 for (i = 0; i < psy->desc->num_properties; i++) {
671 if (psy->desc->properties[i] ==
952aeeb3
RP
672 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
673 psy->tcd = thermal_cooling_device_register(
297d716f 674 (char *)psy->desc->name,
952aeeb3 675 psy, &psy_tcd_ops);
9d2410c7 676 return PTR_ERR_OR_ZERO(psy->tcd);
952aeeb3
RP
677 }
678 }
679 return 0;
680}
681
682static void psy_unregister_cooler(struct power_supply *psy)
683{
684 if (IS_ERR_OR_NULL(psy->tcd))
685 return;
686 thermal_cooling_device_unregister(psy->tcd);
687}
3be330bf
JT
688#else
689static int psy_register_thermal(struct power_supply *psy)
690{
691 return 0;
692}
693
694static void psy_unregister_thermal(struct power_supply *psy)
695{
696}
952aeeb3
RP
697
698static int psy_register_cooler(struct power_supply *psy)
699{
700 return 0;
701}
702
703static void psy_unregister_cooler(struct power_supply *psy)
704{
705}
3be330bf
JT
706#endif
707
297d716f
KK
708static struct power_supply *__must_check
709__power_supply_register(struct device *parent,
710 const struct power_supply_desc *desc,
2dc9215d
KK
711 const struct power_supply_config *cfg,
712 bool ws)
4a11b59d 713{
5f487cd3 714 struct device *dev;
297d716f 715 struct power_supply *psy;
5f487cd3 716 int rc;
4a11b59d 717
7f1a57fd
KK
718 if (!parent)
719 pr_warn("%s: Expected proper parent device for '%s'\n",
720 __func__, desc->name);
721
297d716f
KK
722 psy = kzalloc(sizeof(*psy), GFP_KERNEL);
723 if (!psy)
724 return ERR_PTR(-ENOMEM);
725
726 dev = &psy->dev;
4a11b59d 727
5f487cd3 728 device_initialize(dev);
4a11b59d 729
5f487cd3
AV
730 dev->class = power_supply_class;
731 dev->type = &power_supply_dev_type;
732 dev->parent = parent;
733 dev->release = power_supply_dev_release;
734 dev_set_drvdata(dev, psy);
297d716f 735 psy->desc = desc;
2dc9215d
KK
736 if (cfg) {
737 psy->drv_data = cfg->drv_data;
738 psy->of_node = cfg->of_node;
739 psy->supplied_to = cfg->supplied_to;
740 psy->num_supplicants = cfg->num_supplicants;
741 }
5f487cd3 742
297d716f 743 rc = dev_set_name(dev, "%s", desc->name);
80c6463e
SK
744 if (rc)
745 goto dev_set_name_failed;
746
97774672 747 INIT_WORK(&psy->changed_work, power_supply_changed_work);
7f1a57fd
KK
748 INIT_DELAYED_WORK(&psy->deferred_register_work,
749 power_supply_deferred_register_work);
97774672 750
f6e0b081
RK
751 rc = power_supply_check_supplies(psy);
752 if (rc) {
753 dev_info(dev, "Not all required supplies found, defer probe\n");
754 goto check_supplies_failed;
755 }
756
948dcf96 757 spin_lock_init(&psy->changed_lock);
9113e260 758 rc = device_init_wakeup(dev, ws);
948dcf96
ZM
759 if (rc)
760 goto wakeup_init_failed;
761
5f487cd3 762 rc = device_add(dev);
4a11b59d 763 if (rc)
5f487cd3
AV
764 goto device_add_failed;
765
3be330bf
JT
766 rc = psy_register_thermal(psy);
767 if (rc)
768 goto register_thermal_failed;
769
952aeeb3
RP
770 rc = psy_register_cooler(psy);
771 if (rc)
772 goto register_cooler_failed;
773
4a11b59d
AV
774 rc = power_supply_create_triggers(psy);
775 if (rc)
776 goto create_triggers_failed;
777
8e59c7f2
KK
778 /*
779 * Update use_cnt after any uevents (most notably from device_add()).
780 * We are here still during driver's probe but
781 * the power_supply_uevent() calls back driver's get_property
782 * method so:
783 * 1. Driver did not assigned the returned struct power_supply,
784 * 2. Driver could not finish initialization (anything in its probe
785 * after calling power_supply_register()).
786 */
787 atomic_inc(&psy->use_cnt);
7f1a57fd
KK
788
789 queue_delayed_work(system_power_efficient_wq,
790 &psy->deferred_register_work,
791 POWER_SUPPLY_DEFERRED_REGISTER_TIME);
4a11b59d 792
297d716f 793 return psy;
4a11b59d
AV
794
795create_triggers_failed:
952aeeb3
RP
796 psy_unregister_cooler(psy);
797register_cooler_failed:
3be330bf
JT
798 psy_unregister_thermal(psy);
799register_thermal_failed:
3a2dbd61 800 device_del(dev);
5f487cd3 801device_add_failed:
80c6463e 802wakeup_init_failed:
f6e0b081 803check_supplies_failed:
80c6463e 804dev_set_name_failed:
3a2dbd61 805 put_device(dev);
297d716f 806 return ERR_PTR(rc);
4a11b59d 807}
9113e260 808
297d716f
KK
809/**
810 * power_supply_register() - Register new power supply
7f1a57fd
KK
811 * @parent: Device to be a parent of power supply's device, usually
812 * the device which probe function calls this
297d716f
KK
813 * @desc: Description of power supply, must be valid through whole
814 * lifetime of this power supply
815 * @cfg: Run-time specific configuration accessed during registering,
816 * may be NULL
817 *
818 * Return: A pointer to newly allocated power_supply on success
819 * or ERR_PTR otherwise.
820 * Use power_supply_unregister() on returned power_supply pointer to release
821 * resources.
822 */
823struct power_supply *__must_check power_supply_register(struct device *parent,
824 const struct power_supply_desc *desc,
2dc9215d 825 const struct power_supply_config *cfg)
9113e260 826{
297d716f 827 return __power_supply_register(parent, desc, cfg, true);
9113e260 828}
ff3417e7 829EXPORT_SYMBOL_GPL(power_supply_register);
4a11b59d 830
297d716f 831/**
43df6105 832 * power_supply_register_no_ws() - Register new non-waking-source power supply
7f1a57fd
KK
833 * @parent: Device to be a parent of power supply's device, usually
834 * the device which probe function calls this
297d716f
KK
835 * @desc: Description of power supply, must be valid through whole
836 * lifetime of this power supply
837 * @cfg: Run-time specific configuration accessed during registering,
838 * may be NULL
839 *
840 * Return: A pointer to newly allocated power_supply on success
841 * or ERR_PTR otherwise.
842 * Use power_supply_unregister() on returned power_supply pointer to release
843 * resources.
844 */
845struct power_supply *__must_check
846power_supply_register_no_ws(struct device *parent,
847 const struct power_supply_desc *desc,
2dc9215d 848 const struct power_supply_config *cfg)
9113e260 849{
297d716f 850 return __power_supply_register(parent, desc, cfg, false);
9113e260
ZR
851}
852EXPORT_SYMBOL_GPL(power_supply_register_no_ws);
853
5d8a4219
N
854static void devm_power_supply_release(struct device *dev, void *res)
855{
856 struct power_supply **psy = res;
857
858 power_supply_unregister(*psy);
859}
860
297d716f 861/**
43df6105 862 * devm_power_supply_register() - Register managed power supply
7f1a57fd
KK
863 * @parent: Device to be a parent of power supply's device, usually
864 * the device which probe function calls this
297d716f
KK
865 * @desc: Description of power supply, must be valid through whole
866 * lifetime of this power supply
867 * @cfg: Run-time specific configuration accessed during registering,
868 * may be NULL
869 *
870 * Return: A pointer to newly allocated power_supply on success
871 * or ERR_PTR otherwise.
872 * The returned power_supply pointer will be automatically unregistered
873 * on driver detach.
874 */
875struct power_supply *__must_check
876devm_power_supply_register(struct device *parent,
877 const struct power_supply_desc *desc,
2dc9215d 878 const struct power_supply_config *cfg)
5d8a4219 879{
297d716f
KK
880 struct power_supply **ptr, *psy;
881
882 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
5d8a4219
N
883
884 if (!ptr)
297d716f
KK
885 return ERR_PTR(-ENOMEM);
886 psy = __power_supply_register(parent, desc, cfg, true);
887 if (IS_ERR(psy)) {
5d8a4219 888 devres_free(ptr);
297d716f 889 } else {
5d8a4219
N
890 *ptr = psy;
891 devres_add(parent, ptr);
892 }
297d716f 893 return psy;
5d8a4219
N
894}
895EXPORT_SYMBOL_GPL(devm_power_supply_register);
896
297d716f 897/**
43df6105 898 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
7f1a57fd
KK
899 * @parent: Device to be a parent of power supply's device, usually
900 * the device which probe function calls this
297d716f
KK
901 * @desc: Description of power supply, must be valid through whole
902 * lifetime of this power supply
903 * @cfg: Run-time specific configuration accessed during registering,
904 * may be NULL
905 *
906 * Return: A pointer to newly allocated power_supply on success
907 * or ERR_PTR otherwise.
908 * The returned power_supply pointer will be automatically unregistered
909 * on driver detach.
910 */
911struct power_supply *__must_check
912devm_power_supply_register_no_ws(struct device *parent,
913 const struct power_supply_desc *desc,
2dc9215d 914 const struct power_supply_config *cfg)
5d8a4219 915{
297d716f
KK
916 struct power_supply **ptr, *psy;
917
918 ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
5d8a4219
N
919
920 if (!ptr)
297d716f
KK
921 return ERR_PTR(-ENOMEM);
922 psy = __power_supply_register(parent, desc, cfg, false);
923 if (IS_ERR(psy)) {
5d8a4219 924 devres_free(ptr);
297d716f 925 } else {
5d8a4219
N
926 *ptr = psy;
927 devres_add(parent, ptr);
928 }
297d716f 929 return psy;
5d8a4219
N
930}
931EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);
932
297d716f
KK
933/**
934 * power_supply_unregister() - Remove this power supply from system
935 * @psy: Pointer to power supply to unregister
936 *
937 * Remove this power supply from the system. The resources of power supply
938 * will be freed here or on last power_supply_put() call.
939 */
4a11b59d
AV
940void power_supply_unregister(struct power_supply *psy)
941{
bc154056 942 WARN_ON(atomic_dec_return(&psy->use_cnt));
bc51e7ff 943 cancel_work_sync(&psy->changed_work);
7f1a57fd 944 cancel_delayed_work_sync(&psy->deferred_register_work);
297d716f 945 sysfs_remove_link(&psy->dev.kobj, "powers");
4a11b59d 946 power_supply_remove_triggers(psy);
952aeeb3 947 psy_unregister_cooler(psy);
3be330bf 948 psy_unregister_thermal(psy);
297d716f
KK
949 device_init_wakeup(&psy->dev, false);
950 device_unregister(&psy->dev);
4a11b59d 951}
ff3417e7 952EXPORT_SYMBOL_GPL(power_supply_unregister);
4a11b59d 953
e44ea364
KK
954void *power_supply_get_drvdata(struct power_supply *psy)
955{
956 return psy->drv_data;
957}
958EXPORT_SYMBOL_GPL(power_supply_get_drvdata);
959
4a11b59d
AV
960static int __init power_supply_class_init(void)
961{
962 power_supply_class = class_create(THIS_MODULE, "power_supply");
963
964 if (IS_ERR(power_supply_class))
965 return PTR_ERR(power_supply_class);
966
967 power_supply_class->dev_uevent = power_supply_uevent;
5f487cd3 968 power_supply_init_attrs(&power_supply_dev_type);
4a11b59d
AV
969
970 return 0;
971}
972
973static void __exit power_supply_class_exit(void)
974{
975 class_destroy(power_supply_class);
4a11b59d
AV
976}
977
4a11b59d
AV
978subsys_initcall(power_supply_class_init);
979module_exit(power_supply_class_exit);
980
981MODULE_DESCRIPTION("Universal power supply monitor class");
982MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
983 "Szabolcs Gyurko, "
984 "Anton Vorontsov <cbou@mail.ru>");
985MODULE_LICENSE("GPL");
This page took 0.569932 seconds and 5 git commands to generate.