Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux-2.6-block.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>
16#include <linux/device.h>
17#include <linux/err.h>
18#include <linux/power_supply.h>
19#include "power_supply.h"
20
21struct class *power_supply_class;
22
443cad92
DY
23static int __power_supply_changed_work(struct device *dev, void *data)
24{
25 struct power_supply *psy = (struct power_supply *)data;
26 struct power_supply *pst = dev_get_drvdata(dev);
27 int i;
28
29 for (i = 0; i < psy->num_supplicants; i++)
30 if (!strcmp(psy->supplied_to[i], pst->name)) {
31 if (pst->external_power_changed)
32 pst->external_power_changed(pst);
33 }
34 return 0;
35}
36
4a11b59d
AV
37static void power_supply_changed_work(struct work_struct *work)
38{
39 struct power_supply *psy = container_of(work, struct power_supply,
40 changed_work);
4a11b59d 41
0cddc0a9 42 dev_dbg(psy->dev, "%s\n", __func__);
4a11b59d 43
93562b53 44 class_for_each_device(power_supply_class, NULL, psy,
443cad92 45 __power_supply_changed_work);
4a11b59d
AV
46
47 power_supply_update_leds(psy);
48
49 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
4a11b59d
AV
50}
51
52void power_supply_changed(struct power_supply *psy)
53{
0cddc0a9 54 dev_dbg(psy->dev, "%s\n", __func__);
4a11b59d
AV
55
56 schedule_work(&psy->changed_work);
4a11b59d
AV
57}
58
443cad92 59static int __power_supply_am_i_supplied(struct device *dev, void *data)
4a11b59d
AV
60{
61 union power_supply_propval ret = {0,};
443cad92
DY
62 struct power_supply *psy = (struct power_supply *)data;
63 struct power_supply *epsy = dev_get_drvdata(dev);
64 int i;
65
66 for (i = 0; i < epsy->num_supplicants; i++) {
67 if (!strcmp(epsy->supplied_to[i], psy->name)) {
68 if (epsy->get_property(epsy,
69 POWER_SUPPLY_PROP_ONLINE, &ret))
70 continue;
71 if (ret.intval)
72 return ret.intval;
4a11b59d
AV
73 }
74 }
443cad92
DY
75 return 0;
76}
77
78int power_supply_am_i_supplied(struct power_supply *psy)
79{
80 int error;
81
93562b53 82 error = class_for_each_device(power_supply_class, NULL, psy,
443cad92 83 __power_supply_am_i_supplied);
4a11b59d 84
0cddc0a9 85 dev_dbg(psy->dev, "%s %d\n", __func__, error);
4a11b59d 86
443cad92 87 return error;
4a11b59d
AV
88}
89
90int power_supply_register(struct device *parent, struct power_supply *psy)
91{
92 int rc = 0;
93
a9b12619
GKH
94 psy->dev = device_create(power_supply_class, parent, 0, psy,
95 "%s", psy->name);
4a11b59d
AV
96 if (IS_ERR(psy->dev)) {
97 rc = PTR_ERR(psy->dev);
98 goto dev_create_failed;
99 }
100
4a11b59d
AV
101 INIT_WORK(&psy->changed_work, power_supply_changed_work);
102
103 rc = power_supply_create_attrs(psy);
104 if (rc)
105 goto create_attrs_failed;
106
107 rc = power_supply_create_triggers(psy);
108 if (rc)
109 goto create_triggers_failed;
110
111 power_supply_changed(psy);
112
113 goto success;
114
115create_triggers_failed:
116 power_supply_remove_attrs(psy);
117create_attrs_failed:
118 device_unregister(psy->dev);
119dev_create_failed:
120success:
121 return rc;
122}
123
124void power_supply_unregister(struct power_supply *psy)
125{
126 flush_scheduled_work();
127 power_supply_remove_triggers(psy);
128 power_supply_remove_attrs(psy);
129 device_unregister(psy->dev);
4a11b59d
AV
130}
131
132static int __init power_supply_class_init(void)
133{
134 power_supply_class = class_create(THIS_MODULE, "power_supply");
135
136 if (IS_ERR(power_supply_class))
137 return PTR_ERR(power_supply_class);
138
139 power_supply_class->dev_uevent = power_supply_uevent;
140
141 return 0;
142}
143
144static void __exit power_supply_class_exit(void)
145{
146 class_destroy(power_supply_class);
4a11b59d
AV
147}
148
149EXPORT_SYMBOL_GPL(power_supply_changed);
150EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
151EXPORT_SYMBOL_GPL(power_supply_register);
152EXPORT_SYMBOL_GPL(power_supply_unregister);
153
154/* exported for the APM Power driver, APM emulation */
155EXPORT_SYMBOL_GPL(power_supply_class);
156
157subsys_initcall(power_supply_class_init);
158module_exit(power_supply_class_exit);
159
160MODULE_DESCRIPTION("Universal power supply monitor class");
161MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
162 "Szabolcs Gyurko, "
163 "Anton Vorontsov <cbou@mail.ru>");
164MODULE_LICENSE("GPL");