blob: 2436f13500132c7bf0e2f997ab8feb9c996767de [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>
Jenny TC3be330b2012-05-09 20:36:47 +053020#include <linux/thermal.h>
Anton Vorontsov4a11b592007-05-04 00:27:45 +040021#include "power_supply.h"
22
Daniel Mackff3417e2009-07-30 17:42:31 +040023/* exported for the APM Power driver, APM emulation */
Anton Vorontsov4a11b592007-05-04 00:27:45 +040024struct class *power_supply_class;
Daniel Mackff3417e2009-07-30 17:42:31 +040025EXPORT_SYMBOL_GPL(power_supply_class);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040026
Anton Vorontsov5f487cd2010-05-18 21:49:51 +020027static struct device_type power_supply_dev_type;
28
Dave Young443cad92008-01-22 13:58:22 +080029static int __power_supply_changed_work(struct device *dev, void *data)
30{
31 struct power_supply *psy = (struct power_supply *)data;
32 struct power_supply *pst = dev_get_drvdata(dev);
33 int i;
34
35 for (i = 0; i < psy->num_supplicants; i++)
36 if (!strcmp(psy->supplied_to[i], pst->name)) {
37 if (pst->external_power_changed)
38 pst->external_power_changed(pst);
39 }
40 return 0;
41}
42
Anton Vorontsov4a11b592007-05-04 00:27:45 +040043static void power_supply_changed_work(struct work_struct *work)
44{
45 struct power_supply *psy = container_of(work, struct power_supply,
46 changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040047
Harvey Harrison0cddc0a2008-04-29 00:58:29 -070048 dev_dbg(psy->dev, "%s\n", __func__);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040049
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -040050 class_for_each_device(power_supply_class, NULL, psy,
Dave Young443cad92008-01-22 13:58:22 +080051 __power_supply_changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040052
53 power_supply_update_leds(psy);
54
55 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040056}
57
58void power_supply_changed(struct power_supply *psy)
59{
Harvey Harrison0cddc0a2008-04-29 00:58:29 -070060 dev_dbg(psy->dev, "%s\n", __func__);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040061
62 schedule_work(&psy->changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040063}
Daniel Mackff3417e2009-07-30 17:42:31 +040064EXPORT_SYMBOL_GPL(power_supply_changed);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040065
Dave Young443cad92008-01-22 13:58:22 +080066static int __power_supply_am_i_supplied(struct device *dev, void *data)
Anton Vorontsov4a11b592007-05-04 00:27:45 +040067{
68 union power_supply_propval ret = {0,};
Dave Young443cad92008-01-22 13:58:22 +080069 struct power_supply *psy = (struct power_supply *)data;
70 struct power_supply *epsy = dev_get_drvdata(dev);
71 int i;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040072
Dave Young443cad92008-01-22 13:58:22 +080073 for (i = 0; i < epsy->num_supplicants; i++) {
74 if (!strcmp(epsy->supplied_to[i], psy->name)) {
75 if (epsy->get_property(epsy,
76 POWER_SUPPLY_PROP_ONLINE, &ret))
77 continue;
78 if (ret.intval)
79 return ret.intval;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040080 }
81 }
Dave Young443cad92008-01-22 13:58:22 +080082 return 0;
83}
Anton Vorontsov4a11b592007-05-04 00:27:45 +040084
Dave Young443cad92008-01-22 13:58:22 +080085int power_supply_am_i_supplied(struct power_supply *psy)
86{
87 int error;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040088
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -040089 error = class_for_each_device(power_supply_class, NULL, psy,
Dave Young443cad92008-01-22 13:58:22 +080090 __power_supply_am_i_supplied);
91
Harvey Harrison0cddc0a2008-04-29 00:58:29 -070092 dev_dbg(psy->dev, "%s %d\n", __func__, error);
Dave Young443cad92008-01-22 13:58:22 +080093
94 return error;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040095}
Daniel Mackff3417e2009-07-30 17:42:31 +040096EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040097
Matthew Garrett942ed162008-08-26 21:09:59 +010098static int __power_supply_is_system_supplied(struct device *dev, void *data)
99{
100 union power_supply_propval ret = {0,};
101 struct power_supply *psy = dev_get_drvdata(dev);
Jean Delvare2530daa2011-12-10 22:53:36 +0100102 unsigned int *count = data;
Matthew Garrett942ed162008-08-26 21:09:59 +0100103
Jean Delvare2530daa2011-12-10 22:53:36 +0100104 (*count)++;
Matthew Garrett942ed162008-08-26 21:09:59 +0100105 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
106 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
107 return 0;
108 if (ret.intval)
109 return ret.intval;
110 }
111 return 0;
112}
113
114int power_supply_is_system_supplied(void)
115{
116 int error;
Jean Delvare2530daa2011-12-10 22:53:36 +0100117 unsigned int count = 0;
Matthew Garrett942ed162008-08-26 21:09:59 +0100118
Jean Delvare2530daa2011-12-10 22:53:36 +0100119 error = class_for_each_device(power_supply_class, NULL, &count,
Matthew Garrett942ed162008-08-26 21:09:59 +0100120 __power_supply_is_system_supplied);
121
Jean Delvare2530daa2011-12-10 22:53:36 +0100122 /*
123 * If no power class device was found at all, most probably we are
124 * running on a desktop system, so assume we are on mains power.
125 */
126 if (count == 0)
127 return 1;
128
Matthew Garrett942ed162008-08-26 21:09:59 +0100129 return error;
130}
Daniel Mackff3417e2009-07-30 17:42:31 +0400131EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
Matthew Garrett942ed162008-08-26 21:09:59 +0100132
Daniel Macke5f5ccb2009-07-23 20:35:53 +0200133int power_supply_set_battery_charged(struct power_supply *psy)
134{
135 if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
136 psy->set_charged(psy);
137 return 0;
138 }
139
140 return -EINVAL;
141}
142EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
143
144static int power_supply_match_device_by_name(struct device *dev, void *data)
145{
146 const char *name = data;
147 struct power_supply *psy = dev_get_drvdata(dev);
148
149 return strcmp(psy->name, name) == 0;
150}
151
152struct power_supply *power_supply_get_by_name(char *name)
153{
154 struct device *dev = class_find_device(power_supply_class, NULL, name,
155 power_supply_match_device_by_name);
156
157 return dev ? dev_get_drvdata(dev) : NULL;
158}
159EXPORT_SYMBOL_GPL(power_supply_get_by_name);
160
Jeremy Fitzhardinge83516652011-12-07 09:15:45 -0800161int power_supply_powers(struct power_supply *psy, struct device *dev)
162{
Anton Vorontsov93278d12012-01-05 19:17:25 +0400163 return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
Jeremy Fitzhardinge83516652011-12-07 09:15:45 -0800164}
165EXPORT_SYMBOL_GPL(power_supply_powers);
166
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200167static void power_supply_dev_release(struct device *dev)
168{
169 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
170 kfree(dev);
171}
172
Jenny TC3be330b2012-05-09 20:36:47 +0530173#ifdef CONFIG_THERMAL
174static int power_supply_read_temp(struct thermal_zone_device *tzd,
175 unsigned long *temp)
176{
177 struct power_supply *psy;
178 union power_supply_propval val;
179 int ret;
180
181 WARN_ON(tzd == NULL);
182 psy = tzd->devdata;
183 ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
184
185 /* Convert tenths of degree Celsius to milli degree Celsius. */
186 if (!ret)
187 *temp = val.intval * 100;
188
189 return ret;
190}
191
192static struct thermal_zone_device_ops psy_tzd_ops = {
193 .get_temp = power_supply_read_temp,
194};
195
196static int psy_register_thermal(struct power_supply *psy)
197{
198 int i;
199
200 /* Register battery zone device psy reports temperature */
201 for (i = 0; i < psy->num_properties; i++) {
202 if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
Anton Vorontsove6db06a2012-07-31 04:59:42 -0700203 psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
Zhang Rui1b7ddb82012-06-27 09:51:12 +0800204 psy, &psy_tzd_ops, 0, 0);
Jenny TC3be330b2012-05-09 20:36:47 +0530205 if (IS_ERR(psy->tzd))
206 return PTR_ERR(psy->tzd);
207 break;
208 }
209 }
210 return 0;
211}
212
213static void psy_unregister_thermal(struct power_supply *psy)
214{
215 if (IS_ERR_OR_NULL(psy->tzd))
216 return;
217 thermal_zone_device_unregister(psy->tzd);
218}
219#else
220static int psy_register_thermal(struct power_supply *psy)
221{
222 return 0;
223}
224
225static void psy_unregister_thermal(struct power_supply *psy)
226{
227}
228#endif
229
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400230int power_supply_register(struct device *parent, struct power_supply *psy)
231{
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200232 struct device *dev;
233 int rc;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400234
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200235 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
236 if (!dev)
237 return -ENOMEM;
238
239 device_initialize(dev);
240
241 dev->class = power_supply_class;
242 dev->type = &power_supply_dev_type;
243 dev->parent = parent;
244 dev->release = power_supply_dev_release;
245 dev_set_drvdata(dev, psy);
246 psy->dev = dev;
247
Lars-Peter Clausen97774672011-02-21 15:34:19 +0100248 INIT_WORK(&psy->changed_work, power_supply_changed_work);
249
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200250 rc = kobject_set_name(&dev->kobj, "%s", psy->name);
251 if (rc)
252 goto kobject_set_name_failed;
253
254 rc = device_add(dev);
255 if (rc)
256 goto device_add_failed;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400257
Jenny TC3be330b2012-05-09 20:36:47 +0530258 rc = psy_register_thermal(psy);
259 if (rc)
260 goto register_thermal_failed;
261
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400262 rc = power_supply_create_triggers(psy);
263 if (rc)
264 goto create_triggers_failed;
265
266 power_supply_changed(psy);
267
268 goto success;
269
270create_triggers_failed:
Jenny TC3be330b2012-05-09 20:36:47 +0530271 psy_unregister_thermal(psy);
272register_thermal_failed:
Vasiliy Kulikov3a2dbd62010-11-19 21:41:58 +0300273 device_del(dev);
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200274kobject_set_name_failed:
275device_add_failed:
Vasiliy Kulikov3a2dbd62010-11-19 21:41:58 +0300276 put_device(dev);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400277success:
278 return rc;
279}
Daniel Mackff3417e2009-07-30 17:42:31 +0400280EXPORT_SYMBOL_GPL(power_supply_register);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400281
282void power_supply_unregister(struct power_supply *psy)
283{
Tejun Heobc51e7f2010-12-11 17:51:45 +0100284 cancel_work_sync(&psy->changed_work);
Jeremy Fitzhardinge83516652011-12-07 09:15:45 -0800285 sysfs_remove_link(&psy->dev->kobj, "powers");
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400286 power_supply_remove_triggers(psy);
Jenny TC3be330b2012-05-09 20:36:47 +0530287 psy_unregister_thermal(psy);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400288 device_unregister(psy->dev);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400289}
Daniel Mackff3417e2009-07-30 17:42:31 +0400290EXPORT_SYMBOL_GPL(power_supply_unregister);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400291
292static int __init power_supply_class_init(void)
293{
294 power_supply_class = class_create(THIS_MODULE, "power_supply");
295
296 if (IS_ERR(power_supply_class))
297 return PTR_ERR(power_supply_class);
298
299 power_supply_class->dev_uevent = power_supply_uevent;
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200300 power_supply_init_attrs(&power_supply_dev_type);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400301
302 return 0;
303}
304
305static void __exit power_supply_class_exit(void)
306{
307 class_destroy(power_supply_class);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400308}
309
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400310subsys_initcall(power_supply_class_init);
311module_exit(power_supply_class_exit);
312
313MODULE_DESCRIPTION("Universal power supply monitor class");
314MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
315 "Szabolcs Gyurko, "
316 "Anton Vorontsov <cbou@mail.ru>");
317MODULE_LICENSE("GPL");