blob: 91606bb55318110307e3997bcb61722cd247ee73 [file] [log] [blame]
Anton Vorontsov4a11b592007-05-04 00:27:45 +04001/*
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>
Anton Vorontsov5f487cd2010-05-18 21:49:51 +020016#include <linux/slab.h>
Anton Vorontsov4a11b592007-05-04 00:27:45 +040017#include <linux/device.h>
18#include <linux/err.h>
19#include <linux/power_supply.h>
20#include "power_supply.h"
21
Daniel Mackff3417e2009-07-30 17:42:31 +040022/* exported for the APM Power driver, APM emulation */
Anton Vorontsov4a11b592007-05-04 00:27:45 +040023struct class *power_supply_class;
Daniel Mackff3417e2009-07-30 17:42:31 +040024EXPORT_SYMBOL_GPL(power_supply_class);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040025
Anton Vorontsov5f487cd2010-05-18 21:49:51 +020026static struct device_type power_supply_dev_type;
27
Dave Young443cad92008-01-22 13:58:22 +080028static int __power_supply_changed_work(struct device *dev, void *data)
29{
30 struct power_supply *psy = (struct power_supply *)data;
31 struct power_supply *pst = dev_get_drvdata(dev);
32 int i;
33
34 for (i = 0; i < psy->num_supplicants; i++)
35 if (!strcmp(psy->supplied_to[i], pst->name)) {
36 if (pst->external_power_changed)
37 pst->external_power_changed(pst);
38 }
39 return 0;
40}
41
Anton Vorontsov4a11b592007-05-04 00:27:45 +040042static void power_supply_changed_work(struct work_struct *work)
43{
44 struct power_supply *psy = container_of(work, struct power_supply,
45 changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040046
Harvey Harrison0cddc0a2008-04-29 00:58:29 -070047 dev_dbg(psy->dev, "%s\n", __func__);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040048
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -040049 class_for_each_device(power_supply_class, NULL, psy,
Dave Young443cad92008-01-22 13:58:22 +080050 __power_supply_changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040051
52 power_supply_update_leds(psy);
53
54 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040055}
56
57void power_supply_changed(struct power_supply *psy)
58{
Harvey Harrison0cddc0a2008-04-29 00:58:29 -070059 dev_dbg(psy->dev, "%s\n", __func__);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040060
61 schedule_work(&psy->changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040062}
Daniel Mackff3417e2009-07-30 17:42:31 +040063EXPORT_SYMBOL_GPL(power_supply_changed);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040064
Dave Young443cad92008-01-22 13:58:22 +080065static int __power_supply_am_i_supplied(struct device *dev, void *data)
Anton Vorontsov4a11b592007-05-04 00:27:45 +040066{
67 union power_supply_propval ret = {0,};
Dave Young443cad92008-01-22 13:58:22 +080068 struct power_supply *psy = (struct power_supply *)data;
69 struct power_supply *epsy = dev_get_drvdata(dev);
70 int i;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040071
Dave Young443cad92008-01-22 13:58:22 +080072 for (i = 0; i < epsy->num_supplicants; i++) {
73 if (!strcmp(epsy->supplied_to[i], psy->name)) {
74 if (epsy->get_property(epsy,
75 POWER_SUPPLY_PROP_ONLINE, &ret))
76 continue;
77 if (ret.intval)
78 return ret.intval;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040079 }
80 }
Dave Young443cad92008-01-22 13:58:22 +080081 return 0;
82}
Anton Vorontsov4a11b592007-05-04 00:27:45 +040083
Dave Young443cad92008-01-22 13:58:22 +080084int power_supply_am_i_supplied(struct power_supply *psy)
85{
86 int error;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040087
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -040088 error = class_for_each_device(power_supply_class, NULL, psy,
Dave Young443cad92008-01-22 13:58:22 +080089 __power_supply_am_i_supplied);
90
Harvey Harrison0cddc0a2008-04-29 00:58:29 -070091 dev_dbg(psy->dev, "%s %d\n", __func__, error);
Dave Young443cad92008-01-22 13:58:22 +080092
93 return error;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040094}
Daniel Mackff3417e2009-07-30 17:42:31 +040095EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040096
Matthew Garrett942ed162008-08-26 21:09:59 +010097static int __power_supply_is_system_supplied(struct device *dev, void *data)
98{
99 union power_supply_propval ret = {0,};
100 struct power_supply *psy = dev_get_drvdata(dev);
101
102 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
103 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
104 return 0;
105 if (ret.intval)
106 return ret.intval;
107 }
108 return 0;
109}
110
111int power_supply_is_system_supplied(void)
112{
113 int error;
114
115 error = class_for_each_device(power_supply_class, NULL, NULL,
116 __power_supply_is_system_supplied);
117
118 return error;
119}
Daniel Mackff3417e2009-07-30 17:42:31 +0400120EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
Matthew Garrett942ed162008-08-26 21:09:59 +0100121
Daniel Macke5f5ccb2009-07-23 20:35:53 +0200122int power_supply_set_battery_charged(struct power_supply *psy)
123{
124 if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
125 psy->set_charged(psy);
126 return 0;
127 }
128
129 return -EINVAL;
130}
131EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
132
133static int power_supply_match_device_by_name(struct device *dev, void *data)
134{
135 const char *name = data;
136 struct power_supply *psy = dev_get_drvdata(dev);
137
138 return strcmp(psy->name, name) == 0;
139}
140
141struct power_supply *power_supply_get_by_name(char *name)
142{
143 struct device *dev = class_find_device(power_supply_class, NULL, name,
144 power_supply_match_device_by_name);
145
146 return dev ? dev_get_drvdata(dev) : NULL;
147}
148EXPORT_SYMBOL_GPL(power_supply_get_by_name);
149
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200150static void power_supply_dev_release(struct device *dev)
151{
152 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
153 kfree(dev);
154}
155
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400156int power_supply_register(struct device *parent, struct power_supply *psy)
157{
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200158 struct device *dev;
159 int rc;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400160
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200161 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
162 if (!dev)
163 return -ENOMEM;
164
165 device_initialize(dev);
166
167 dev->class = power_supply_class;
168 dev->type = &power_supply_dev_type;
169 dev->parent = parent;
170 dev->release = power_supply_dev_release;
171 dev_set_drvdata(dev, psy);
172 psy->dev = dev;
173
174 rc = kobject_set_name(&dev->kobj, "%s", psy->name);
175 if (rc)
176 goto kobject_set_name_failed;
177
178 rc = device_add(dev);
179 if (rc)
180 goto device_add_failed;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400181
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400182 INIT_WORK(&psy->changed_work, power_supply_changed_work);
183
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400184 rc = power_supply_create_triggers(psy);
185 if (rc)
186 goto create_triggers_failed;
187
188 power_supply_changed(psy);
189
190 goto success;
191
192create_triggers_failed:
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400193 device_unregister(psy->dev);
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200194kobject_set_name_failed:
195device_add_failed:
196 kfree(dev);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400197success:
198 return rc;
199}
Daniel Mackff3417e2009-07-30 17:42:31 +0400200EXPORT_SYMBOL_GPL(power_supply_register);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400201
202void power_supply_unregister(struct power_supply *psy)
203{
204 flush_scheduled_work();
205 power_supply_remove_triggers(psy);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400206 device_unregister(psy->dev);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400207}
Daniel Mackff3417e2009-07-30 17:42:31 +0400208EXPORT_SYMBOL_GPL(power_supply_unregister);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400209
210static int __init power_supply_class_init(void)
211{
212 power_supply_class = class_create(THIS_MODULE, "power_supply");
213
214 if (IS_ERR(power_supply_class))
215 return PTR_ERR(power_supply_class);
216
217 power_supply_class->dev_uevent = power_supply_uevent;
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200218 power_supply_init_attrs(&power_supply_dev_type);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400219
220 return 0;
221}
222
223static void __exit power_supply_class_exit(void)
224{
225 class_destroy(power_supply_class);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400226}
227
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400228subsys_initcall(power_supply_class_init);
229module_exit(power_supply_class_exit);
230
231MODULE_DESCRIPTION("Universal power supply monitor class");
232MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
233 "Szabolcs Gyurko, "
234 "Anton Vorontsov <cbou@mail.ru>");
235MODULE_LICENSE("GPL");