power_supply_core.c 5.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 *  Universal power supply monitor class
 *
 *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
 *  Copyright © 2004  Szabolcs Gyurko
 *  Copyright © 2003  Ian Molton <spyro@f2s.com>
 *
 *  Modified: 2004, Oct     Szabolcs Gyurko
 *
 *  You may use this code as per GPL version 2
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/init.h>
16
#include <linux/slab.h>
17 18 19 20 21
#include <linux/device.h>
#include <linux/err.h>
#include <linux/power_supply.h>
#include "power_supply.h"

22
/* exported for the APM Power driver, APM emulation */
23
struct class *power_supply_class;
24
EXPORT_SYMBOL_GPL(power_supply_class);
25

26 27
static struct device_type power_supply_dev_type;

28 29 30 31 32 33 34 35 36 37 38 39 40 41
static int __power_supply_changed_work(struct device *dev, void *data)
{
	struct power_supply *psy = (struct power_supply *)data;
	struct power_supply *pst = dev_get_drvdata(dev);
	int i;

	for (i = 0; i < psy->num_supplicants; i++)
		if (!strcmp(psy->supplied_to[i], pst->name)) {
			if (pst->external_power_changed)
				pst->external_power_changed(pst);
		}
	return 0;
}

42 43 44 45 46
static void power_supply_changed_work(struct work_struct *work)
{
	struct power_supply *psy = container_of(work, struct power_supply,
						changed_work);

47
	dev_dbg(psy->dev, "%s\n", __func__);
48

49
	class_for_each_device(power_supply_class, NULL, psy,
50
			      __power_supply_changed_work);
51 52 53 54 55 56 57 58

	power_supply_update_leds(psy);

	kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
}

void power_supply_changed(struct power_supply *psy)
{
59
	dev_dbg(psy->dev, "%s\n", __func__);
60 61 62

	schedule_work(&psy->changed_work);
}
63
EXPORT_SYMBOL_GPL(power_supply_changed);
64

65
static int __power_supply_am_i_supplied(struct device *dev, void *data)
66 67
{
	union power_supply_propval ret = {0,};
68 69 70 71 72 73 74 75 76 77 78
	struct power_supply *psy = (struct power_supply *)data;
	struct power_supply *epsy = dev_get_drvdata(dev);
	int i;

	for (i = 0; i < epsy->num_supplicants; i++) {
		if (!strcmp(epsy->supplied_to[i], psy->name)) {
			if (epsy->get_property(epsy,
				  POWER_SUPPLY_PROP_ONLINE, &ret))
				continue;
			if (ret.intval)
				return ret.intval;
79 80
		}
	}
81 82 83 84 85 86 87
	return 0;
}

int power_supply_am_i_supplied(struct power_supply *psy)
{
	int error;

88
	error = class_for_each_device(power_supply_class, NULL, psy,
89
				      __power_supply_am_i_supplied);
90

91
	dev_dbg(psy->dev, "%s %d\n", __func__, error);
92

93
	return error;
94
}
95
EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
static int __power_supply_is_system_supplied(struct device *dev, void *data)
{
	union power_supply_propval ret = {0,};
	struct power_supply *psy = dev_get_drvdata(dev);

	if (psy->type != POWER_SUPPLY_TYPE_BATTERY) {
		if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
			return 0;
		if (ret.intval)
			return ret.intval;
	}
	return 0;
}

int power_supply_is_system_supplied(void)
{
	int error;

	error = class_for_each_device(power_supply_class, NULL, NULL,
				      __power_supply_is_system_supplied);

	return error;
}
120
EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
int power_supply_set_battery_charged(struct power_supply *psy)
{
	if (psy->type == POWER_SUPPLY_TYPE_BATTERY && psy->set_charged) {
		psy->set_charged(psy);
		return 0;
	}

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);

static int power_supply_match_device_by_name(struct device *dev, void *data)
{
	const char *name = data;
	struct power_supply *psy = dev_get_drvdata(dev);

	return strcmp(psy->name, name) == 0;
}

struct power_supply *power_supply_get_by_name(char *name)
{
	struct device *dev = class_find_device(power_supply_class, NULL, name,
					power_supply_match_device_by_name);

	return dev ? dev_get_drvdata(dev) : NULL;
}
EXPORT_SYMBOL_GPL(power_supply_get_by_name);

150 151 152 153 154 155
int power_supply_powers(struct power_supply *psy, struct device *dev)
{
	return sysfs_create_link_nowarn(&psy->dev->kobj, &dev->kobj, "powers");
}
EXPORT_SYMBOL_GPL(power_supply_powers);

156 157 158 159 160 161
static void power_supply_dev_release(struct device *dev)
{
	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
	kfree(dev);
}

162 163
int power_supply_register(struct device *parent, struct power_supply *psy)
{
164 165
	struct device *dev;
	int rc;
166

167 168 169
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return -ENOMEM;
170

171
	device_initialize(dev);
172

173 174 175 176 177 178 179
	dev->class = power_supply_class;
	dev->type = &power_supply_dev_type;
	dev->parent = parent;
	dev->release = power_supply_dev_release;
	dev_set_drvdata(dev, psy);
	psy->dev = dev;

180 181
	INIT_WORK(&psy->changed_work, power_supply_changed_work);

182 183 184 185 186
	rc = kobject_set_name(&dev->kobj, "%s", psy->name);
	if (rc)
		goto kobject_set_name_failed;

	rc = device_add(dev);
187
	if (rc)
188 189
		goto device_add_failed;

190 191 192 193 194 195 196 197 198
	rc = power_supply_create_triggers(psy);
	if (rc)
		goto create_triggers_failed;

	power_supply_changed(psy);

	goto success;

create_triggers_failed:
199
	device_del(dev);
200 201
kobject_set_name_failed:
device_add_failed:
202
	put_device(dev);
203 204 205
success:
	return rc;
}
206
EXPORT_SYMBOL_GPL(power_supply_register);
207 208 209

void power_supply_unregister(struct power_supply *psy)
{
210
	cancel_work_sync(&psy->changed_work);
211
	sysfs_remove_link(&psy->dev->kobj, "powers");
212 213 214
	power_supply_remove_triggers(psy);
	device_unregister(psy->dev);
}
215
EXPORT_SYMBOL_GPL(power_supply_unregister);
216 217 218 219 220 221 222 223 224

static int __init power_supply_class_init(void)
{
	power_supply_class = class_create(THIS_MODULE, "power_supply");

	if (IS_ERR(power_supply_class))
		return PTR_ERR(power_supply_class);

	power_supply_class->dev_uevent = power_supply_uevent;
225
	power_supply_init_attrs(&power_supply_dev_type);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

	return 0;
}

static void __exit power_supply_class_exit(void)
{
	class_destroy(power_supply_class);
}

subsys_initcall(power_supply_class_init);
module_exit(power_supply_class_exit);

MODULE_DESCRIPTION("Universal power supply monitor class");
MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, "
	      "Szabolcs Gyurko, "
	      "Anton Vorontsov <cbou@mail.ru>");
MODULE_LICENSE("GPL");