blob: 5520040449c486e2cb6978cacdc0c4715a067974 [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>
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
Dave Young443cad92008-01-22 13:58:22 +080023static 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
Anton Vorontsov4a11b592007-05-04 00:27:45 +040037static void power_supply_changed_work(struct work_struct *work)
38{
39 struct power_supply *psy = container_of(work, struct power_supply,
40 changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040041
Harvey Harrison0cddc0a2008-04-29 00:58:29 -070042 dev_dbg(psy->dev, "%s\n", __func__);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040043
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -040044 class_for_each_device(power_supply_class, NULL, psy,
Dave Young443cad92008-01-22 13:58:22 +080045 __power_supply_changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040046
47 power_supply_update_leds(psy);
48
49 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040050}
51
52void power_supply_changed(struct power_supply *psy)
53{
Harvey Harrison0cddc0a2008-04-29 00:58:29 -070054 dev_dbg(psy->dev, "%s\n", __func__);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040055
56 schedule_work(&psy->changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040057}
58
Dave Young443cad92008-01-22 13:58:22 +080059static int __power_supply_am_i_supplied(struct device *dev, void *data)
Anton Vorontsov4a11b592007-05-04 00:27:45 +040060{
61 union power_supply_propval ret = {0,};
Dave Young443cad92008-01-22 13:58:22 +080062 struct power_supply *psy = (struct power_supply *)data;
63 struct power_supply *epsy = dev_get_drvdata(dev);
64 int i;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040065
Dave Young443cad92008-01-22 13:58:22 +080066 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;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040073 }
74 }
Dave Young443cad92008-01-22 13:58:22 +080075 return 0;
76}
Anton Vorontsov4a11b592007-05-04 00:27:45 +040077
Dave Young443cad92008-01-22 13:58:22 +080078int power_supply_am_i_supplied(struct power_supply *psy)
79{
80 int error;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040081
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -040082 error = class_for_each_device(power_supply_class, NULL, psy,
Dave Young443cad92008-01-22 13:58:22 +080083 __power_supply_am_i_supplied);
84
Harvey Harrison0cddc0a2008-04-29 00:58:29 -070085 dev_dbg(psy->dev, "%s %d\n", __func__, error);
Dave Young443cad92008-01-22 13:58:22 +080086
87 return error;
Anton Vorontsov4a11b592007-05-04 00:27:45 +040088}
89
Matthew Garrett942ed162008-08-26 21:09:59 +010090static int __power_supply_is_system_supplied(struct device *dev, void *data)
91{
92 union power_supply_propval ret = {0,};
93 struct power_supply *psy = dev_get_drvdata(dev);
94
95 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
96 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
97 return 0;
98 if (ret.intval)
99 return ret.intval;
100 }
101 return 0;
102}
103
104int power_supply_is_system_supplied(void)
105{
106 int error;
107
108 error = class_for_each_device(power_supply_class, NULL, NULL,
109 __power_supply_is_system_supplied);
110
111 return error;
112}
113
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400114int power_supply_register(struct device *parent, struct power_supply *psy)
115{
116 int rc = 0;
117
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700118 psy->dev = device_create(power_supply_class, parent, 0, psy,
119 "%s", psy->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400120 if (IS_ERR(psy->dev)) {
121 rc = PTR_ERR(psy->dev);
122 goto dev_create_failed;
123 }
124
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400125 INIT_WORK(&psy->changed_work, power_supply_changed_work);
126
127 rc = power_supply_create_attrs(psy);
128 if (rc)
129 goto create_attrs_failed;
130
131 rc = power_supply_create_triggers(psy);
132 if (rc)
133 goto create_triggers_failed;
134
135 power_supply_changed(psy);
136
137 goto success;
138
139create_triggers_failed:
140 power_supply_remove_attrs(psy);
141create_attrs_failed:
142 device_unregister(psy->dev);
143dev_create_failed:
144success:
145 return rc;
146}
147
148void power_supply_unregister(struct power_supply *psy)
149{
150 flush_scheduled_work();
151 power_supply_remove_triggers(psy);
152 power_supply_remove_attrs(psy);
153 device_unregister(psy->dev);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400154}
155
156static int __init power_supply_class_init(void)
157{
158 power_supply_class = class_create(THIS_MODULE, "power_supply");
159
160 if (IS_ERR(power_supply_class))
161 return PTR_ERR(power_supply_class);
162
163 power_supply_class->dev_uevent = power_supply_uevent;
164
165 return 0;
166}
167
168static void __exit power_supply_class_exit(void)
169{
170 class_destroy(power_supply_class);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400171}
172
173EXPORT_SYMBOL_GPL(power_supply_changed);
174EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
Matthew Garrett942ed162008-08-26 21:09:59 +0100175EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400176EXPORT_SYMBOL_GPL(power_supply_register);
177EXPORT_SYMBOL_GPL(power_supply_unregister);
178
179/* exported for the APM Power driver, APM emulation */
180EXPORT_SYMBOL_GPL(power_supply_class);
181
182subsys_initcall(power_supply_class_init);
183module_exit(power_supply_class_exit);
184
185MODULE_DESCRIPTION("Universal power supply monitor class");
186MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
187 "Szabolcs Gyurko, "
188 "Anton Vorontsov <cbou@mail.ru>");
189MODULE_LICENSE("GPL");