blob: f10e1d45b5a321d84707c1b26dfb6c62c377e01f [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
Willie Ruane6bee112011-11-06 15:52:52 -080028/**
David Keitel3ff54472014-03-31 15:00:36 -070029 * power_supply_set_voltage_limit - set current limit
30 * @psy: the power supply to control
31 * @limit: current limit in uV from the power supply.
32 * 0 will disable the power supply.
33 *
34 * This function will set a maximum supply current from a source
35 * and it will disable the charger when limit is 0.
36 */
37int power_supply_set_voltage_limit(struct power_supply *psy, int limit)
38{
39 const union power_supply_propval ret = {limit,};
40
41 if (psy->set_property)
42 return psy->set_property(psy, POWER_SUPPLY_PROP_VOLTAGE_MAX,
43 &ret);
44
45 return -ENXIO;
46}
47EXPORT_SYMBOL(power_supply_set_voltage_limit);
48
49
50/**
Willie Ruane6bee112011-11-06 15:52:52 -080051 * power_supply_set_current_limit - set current limit
52 * @psy: the power supply to control
53 * @limit: current limit in uA from the power supply.
54 * 0 will disable the power supply.
55 *
56 * This function will set a maximum supply current from a source
57 * and it will disable the charger when limit is 0.
58 */
59int power_supply_set_current_limit(struct power_supply *psy, int limit)
60{
61 const union power_supply_propval ret = {limit,};
62
63 if (psy->set_property)
64 return psy->set_property(psy, POWER_SUPPLY_PROP_CURRENT_MAX,
65 &ret);
66
67 return -ENXIO;
68}
69EXPORT_SYMBOL_GPL(power_supply_set_current_limit);
70
71/**
David Keitel50187782012-10-15 10:41:16 -070072 * power_supply_set_charging_enabled - enable or disable charging
73 * @psy: the power supply to control
74 * @enable: sets enable property of power supply
75 */
76int power_supply_set_charging_enabled(struct power_supply *psy, bool enable)
77{
78 const union power_supply_propval ret = {enable,};
79
80 if (psy->set_property)
81 return psy->set_property(psy,
82 POWER_SUPPLY_PROP_CHARGING_ENABLED,
83 &ret);
84
85 return -ENXIO;
86}
87EXPORT_SYMBOL_GPL(power_supply_set_charging_enabled);
88
89/**
David Keitel5cec16d2012-10-16 16:55:24 -070090 * power_supply_set_present - set present state of the power supply
Willie Ruane6bee112011-11-06 15:52:52 -080091 * @psy: the power supply to control
David Keitel5cec16d2012-10-16 16:55:24 -070092 * @enable: sets present property of power supply
Willie Ruane6bee112011-11-06 15:52:52 -080093 */
David Keitel272ce522012-08-17 16:25:24 -070094int power_supply_set_present(struct power_supply *psy, bool enable)
95{
96 const union power_supply_propval ret = {enable,};
97
98 if (psy->set_property)
99 return psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT,
100 &ret);
101
102 return -ENXIO;
103}
104EXPORT_SYMBOL_GPL(power_supply_set_present);
105
106/**
107 * power_supply_set_online - set online state of the power supply
108 * @psy: the power supply to control
109 * @enable: sets online property of power supply
110 */
David Keitel65ddd842012-01-11 11:59:41 -0800111int power_supply_set_online(struct power_supply *psy, bool enable)
Willie Ruane6bee112011-11-06 15:52:52 -0800112{
113 const union power_supply_propval ret = {enable,};
114
115 if (psy->set_property)
116 return psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE,
117 &ret);
118
119 return -ENXIO;
120}
David Keitel65ddd842012-01-11 11:59:41 -0800121EXPORT_SYMBOL_GPL(power_supply_set_online);
122
Wu Fenglin04ae6de2013-09-09 19:15:06 +0800123
124/** power_supply_set_health_state - set health state of the power supply
125 * @psy: the power supply to control
126 * @health: sets health property of power supply
127 */
128int power_supply_set_health_state(struct power_supply *psy, int health)
129{
130 const union power_supply_propval ret = {health,};
131
132 if (psy->set_property)
133 return psy->set_property(psy, POWER_SUPPLY_PROP_HEALTH,
134 &ret);
135 return -ENXIO;
136}
137EXPORT_SYMBOL(power_supply_set_health_state);
138
139
David Keitel65ddd842012-01-11 11:59:41 -0800140/**
Abhijeet Dharmapurikar59d890f2012-06-26 11:19:17 -0700141 * power_supply_set_scope - set scope of the power supply
142 * @psy: the power supply to control
143 * @scope: value to set the scope property to, should be from
144 * the SCOPE enum in power_supply.h
145 */
146int power_supply_set_scope(struct power_supply *psy, int scope)
147{
148 const union power_supply_propval ret = {scope, };
149
150 if (psy->set_property)
151 return psy->set_property(psy, POWER_SUPPLY_PROP_SCOPE,
152 &ret);
Abhijeet Dharmapurikar59d890f2012-06-26 11:19:17 -0700153 return -ENXIO;
154}
155EXPORT_SYMBOL_GPL(power_supply_set_scope);
156
157/**
David Keitel01a5ff02012-06-01 18:14:50 -0700158 * power_supply_set_supply_type - set type of the power supply
159 * @psy: the power supply to control
160 * @supply_type: sets type property of power supply
161 */
162int power_supply_set_supply_type(struct power_supply *psy,
163 enum power_supply_type supply_type)
164{
165 const union power_supply_propval ret = {supply_type,};
166
167 if (psy->set_property)
168 return psy->set_property(psy, POWER_SUPPLY_PROP_TYPE,
169 &ret);
170
171 return -ENXIO;
172}
173EXPORT_SYMBOL_GPL(power_supply_set_supply_type);
174
175/**
David Keitel65ddd842012-01-11 11:59:41 -0800176 * power_supply_set_charge_type - set charge type of the power supply
177 * @psy: the power supply to control
178 * @enable: sets charge type property of power supply
179 */
180int power_supply_set_charge_type(struct power_supply *psy, int charge_type)
181{
182 const union power_supply_propval ret = {charge_type,};
183
184 if (psy->set_property)
185 return psy->set_property(psy, POWER_SUPPLY_PROP_CHARGE_TYPE,
186 &ret);
187
188 return -ENXIO;
189}
190EXPORT_SYMBOL_GPL(power_supply_set_charge_type);
Willie Ruane6bee112011-11-06 15:52:52 -0800191
Dave Young443cad92008-01-22 13:58:22 +0800192static int __power_supply_changed_work(struct device *dev, void *data)
193{
194 struct power_supply *psy = (struct power_supply *)data;
195 struct power_supply *pst = dev_get_drvdata(dev);
196 int i;
197
198 for (i = 0; i < psy->num_supplicants; i++)
199 if (!strcmp(psy->supplied_to[i], pst->name)) {
200 if (pst->external_power_changed)
201 pst->external_power_changed(pst);
202 }
203 return 0;
204}
205
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400206static void power_supply_changed_work(struct work_struct *work)
207{
Arve Hjønnevåg836ad482009-09-21 17:26:47 -0700208 unsigned long flags;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400209 struct power_supply *psy = container_of(work, struct power_supply,
210 changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400211
Harvey Harrison0cddc0a2008-04-29 00:58:29 -0700212 dev_dbg(psy->dev, "%s\n", __func__);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400213
Arve Hjønnevåg836ad482009-09-21 17:26:47 -0700214 spin_lock_irqsave(&psy->changed_lock, flags);
215 if (psy->changed) {
216 psy->changed = false;
217 spin_unlock_irqrestore(&psy->changed_lock, flags);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400218
Arve Hjønnevåg836ad482009-09-21 17:26:47 -0700219 class_for_each_device(power_supply_class, NULL, psy,
220 __power_supply_changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400221
Arve Hjønnevåg836ad482009-09-21 17:26:47 -0700222 power_supply_update_leds(psy);
223
224 kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
225 spin_lock_irqsave(&psy->changed_lock, flags);
226 }
227 if (!psy->changed)
228 wake_unlock(&psy->work_wake_lock);
229 spin_unlock_irqrestore(&psy->changed_lock, flags);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400230}
231
232void power_supply_changed(struct power_supply *psy)
233{
Arve Hjønnevåg836ad482009-09-21 17:26:47 -0700234 unsigned long flags;
235
Harvey Harrison0cddc0a2008-04-29 00:58:29 -0700236 dev_dbg(psy->dev, "%s\n", __func__);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400237
Arve Hjønnevåg836ad482009-09-21 17:26:47 -0700238 spin_lock_irqsave(&psy->changed_lock, flags);
239 psy->changed = true;
240 wake_lock(&psy->work_wake_lock);
241 spin_unlock_irqrestore(&psy->changed_lock, flags);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400242 schedule_work(&psy->changed_work);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400243}
Daniel Mackff3417e2009-07-30 17:42:31 +0400244EXPORT_SYMBOL_GPL(power_supply_changed);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400245
Dave Young443cad92008-01-22 13:58:22 +0800246static int __power_supply_am_i_supplied(struct device *dev, void *data)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400247{
248 union power_supply_propval ret = {0,};
Dave Young443cad92008-01-22 13:58:22 +0800249 struct power_supply *psy = (struct power_supply *)data;
250 struct power_supply *epsy = dev_get_drvdata(dev);
251 int i;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400252
Dave Young443cad92008-01-22 13:58:22 +0800253 for (i = 0; i < epsy->num_supplicants; i++) {
254 if (!strcmp(epsy->supplied_to[i], psy->name)) {
255 if (epsy->get_property(epsy,
256 POWER_SUPPLY_PROP_ONLINE, &ret))
257 continue;
258 if (ret.intval)
259 return ret.intval;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400260 }
261 }
Dave Young443cad92008-01-22 13:58:22 +0800262 return 0;
263}
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400264
Dave Young443cad92008-01-22 13:58:22 +0800265int power_supply_am_i_supplied(struct power_supply *psy)
266{
267 int error;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400268
Greg Kroah-Hartman93562b52008-05-22 17:21:08 -0400269 error = class_for_each_device(power_supply_class, NULL, psy,
Dave Young443cad92008-01-22 13:58:22 +0800270 __power_supply_am_i_supplied);
271
Harvey Harrison0cddc0a2008-04-29 00:58:29 -0700272 dev_dbg(psy->dev, "%s %d\n", __func__, error);
Dave Young443cad92008-01-22 13:58:22 +0800273
274 return error;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400275}
Daniel Mackff3417e2009-07-30 17:42:31 +0400276EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400277
Matthew Garrett942ed162008-08-26 21:09:59 +0100278static int __power_supply_is_system_supplied(struct device *dev, void *data)
279{
280 union power_supply_propval ret = {0,};
281 struct power_supply *psy = dev_get_drvdata(dev);
Jean Delvare2530daa2011-12-10 22:53:36 +0100282 unsigned int *count = data;
Matthew Garrett942ed162008-08-26 21:09:59 +0100283
Jean Delvare2530daa2011-12-10 22:53:36 +0100284 (*count)++;
Matthew Garrett942ed162008-08-26 21:09:59 +0100285 if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
286 if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
287 return 0;
288 if (ret.intval)
289 return ret.intval;
290 }
291 return 0;
292}
293
294int power_supply_is_system_supplied(void)
295{
296 int error;
Jean Delvare2530daa2011-12-10 22:53:36 +0100297 unsigned int count = 0;
Matthew Garrett942ed162008-08-26 21:09:59 +0100298
Jean Delvare2530daa2011-12-10 22:53:36 +0100299 error = class_for_each_device(power_supply_class, NULL, &count,
Matthew Garrett942ed162008-08-26 21:09:59 +0100300 __power_supply_is_system_supplied);
301
Jean Delvare2530daa2011-12-10 22:53:36 +0100302 /*
303 * If no power class device was found at all, most probably we are
304 * running on a desktop system, so assume we are on mains power.
305 */
306 if (count == 0)
307 return 1;
308
Matthew Garrett942ed162008-08-26 21:09:59 +0100309 return error;
310}
Daniel Mackff3417e2009-07-30 17:42:31 +0400311EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
Matthew Garrett942ed162008-08-26 21:09:59 +0100312
Daniel Macke5f5ccb2009-07-23 20:35:53 +0200313int power_supply_set_battery_charged(struct power_supply *psy)
314{
315 if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
316 psy->set_charged(psy);
317 return 0;
318 }
319
320 return -EINVAL;
321}
322EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);
323
324static int power_supply_match_device_by_name(struct device *dev, void *data)
325{
326 const char *name = data;
327 struct power_supply *psy = dev_get_drvdata(dev);
328
329 return strcmp(psy->name, name) == 0;
330}
331
332struct power_supply *power_supply_get_by_name(char *name)
333{
334 struct device *dev = class_find_device(power_supply_class, NULL, name,
335 power_supply_match_device_by_name);
336
337 return dev ? dev_get_drvdata(dev) : NULL;
338}
339EXPORT_SYMBOL_GPL(power_supply_get_by_name);
340
Jeremy Fitzhardinge83516652011-12-07 09:15:45 -0800341int power_supply_powers(struct power_supply *psy, struct device *dev)
342{
Anton Vorontsov93278d12012-01-05 19:17:25 +0400343 return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
Jeremy Fitzhardinge83516652011-12-07 09:15:45 -0800344}
345EXPORT_SYMBOL_GPL(power_supply_powers);
346
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200347static void power_supply_dev_release(struct device *dev)
348{
349 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
350 kfree(dev);
351}
352
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400353int power_supply_register(struct device *parent, struct power_supply *psy)
354{
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200355 struct device *dev;
356 int rc;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400357
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200358 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
359 if (!dev)
360 return -ENOMEM;
361
362 device_initialize(dev);
363
364 dev->class = power_supply_class;
365 dev->type = &power_supply_dev_type;
366 dev->parent = parent;
367 dev->release = power_supply_dev_release;
368 dev_set_drvdata(dev, psy);
369 psy->dev = dev;
370
Lars-Peter Clausen97774672011-02-21 15:34:19 +0100371 INIT_WORK(&psy->changed_work, power_supply_changed_work);
372
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200373 rc = kobject_set_name(&dev->kobj, "%s", psy->name);
374 if (rc)
375 goto kobject_set_name_failed;
376
377 rc = device_add(dev);
378 if (rc)
379 goto device_add_failed;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400380
Arve Hjønnevåg836ad482009-09-21 17:26:47 -0700381 spin_lock_init(&psy->changed_lock);
382 wake_lock_init(&psy->work_wake_lock, WAKE_LOCK_SUSPEND, "power-supply");
383
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400384 rc = power_supply_create_triggers(psy);
385 if (rc)
386 goto create_triggers_failed;
387
388 power_supply_changed(psy);
389
390 goto success;
391
392create_triggers_failed:
Arve Hjønnevåg836ad482009-09-21 17:26:47 -0700393 wake_lock_destroy(&psy->work_wake_lock);
Vasiliy Kulikov3a2dbd62010-11-19 21:41:58 +0300394 device_del(dev);
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200395kobject_set_name_failed:
396device_add_failed:
Vasiliy Kulikov3a2dbd62010-11-19 21:41:58 +0300397 put_device(dev);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400398success:
399 return rc;
400}
Daniel Mackff3417e2009-07-30 17:42:31 +0400401EXPORT_SYMBOL_GPL(power_supply_register);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400402
403void power_supply_unregister(struct power_supply *psy)
404{
Tejun Heobc51e7f2010-12-11 17:51:45 +0100405 cancel_work_sync(&psy->changed_work);
Jeremy Fitzhardinge83516652011-12-07 09:15:45 -0800406 sysfs_remove_link(&psy->dev->kobj, "powers");
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400407 power_supply_remove_triggers(psy);
Arve Hjønnevåg836ad482009-09-21 17:26:47 -0700408 wake_lock_destroy(&psy->work_wake_lock);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400409 device_unregister(psy->dev);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400410}
Daniel Mackff3417e2009-07-30 17:42:31 +0400411EXPORT_SYMBOL_GPL(power_supply_unregister);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400412
413static int __init power_supply_class_init(void)
414{
415 power_supply_class = class_create(THIS_MODULE, "power_supply");
416
417 if (IS_ERR(power_supply_class))
418 return PTR_ERR(power_supply_class);
419
420 power_supply_class->dev_uevent = power_supply_uevent;
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200421 power_supply_init_attrs(&power_supply_dev_type);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400422
423 return 0;
424}
425
426static void __exit power_supply_class_exit(void)
427{
428 class_destroy(power_supply_class);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400429}
430
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400431subsys_initcall(power_supply_class_init);
432module_exit(power_supply_class_exit);
433
434MODULE_DESCRIPTION("Universal power supply monitor class");
435MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
436 "Szabolcs Gyurko, "
437 "Anton Vorontsov <cbou@mail.ru>");
438MODULE_LICENSE("GPL");