power_supply_core.c 9.3 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
#include <linux/device.h>
#include <linux/err.h>
#include <linux/power_supply.h>
20
#include <linux/thermal.h>
21 22
#include "power_supply.h"

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

27 28
static struct device_type power_supply_dev_type;

29 30 31 32 33 34 35 36 37 38 39 40 41 42
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;
}

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

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

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

	power_supply_update_leds(psy);

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

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

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

66
static int __power_supply_am_i_supplied(struct device *dev, void *data)
67 68
{
	union power_supply_propval ret = {0,};
69 70 71 72 73 74 75 76 77 78 79
	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;
80 81
		}
	}
82 83 84 85 86 87 88
	return 0;
}

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

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

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

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

98 99 100 101
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);
102
	unsigned int *count = data;
103

104
	(*count)++;
105 106 107 108 109 110 111 112 113 114 115 116
	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;
117
	unsigned int count = 0;
118

119
	error = class_for_each_device(power_supply_class, NULL, &count,
120 121
				      __power_supply_is_system_supplied);

122 123 124 125 126 127 128
	/*
	 * If no power class device was found at all, most probably we are
	 * running on a desktop system, so assume we are on mains power.
	 */
	if (count == 0)
		return 1;

129 130
	return error;
}
131
EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
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);

161 162
int power_supply_powers(struct power_supply *psy, struct device *dev)
{
163
	return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
164 165 166
}
EXPORT_SYMBOL_GPL(power_supply_powers);

167 168 169 170 171 172
static void power_supply_dev_release(struct device *dev)
{
	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
	kfree(dev);
}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
#ifdef CONFIG_THERMAL
static int power_supply_read_temp(struct thermal_zone_device *tzd,
		unsigned long *temp)
{
	struct power_supply *psy;
	union power_supply_propval val;
	int ret;

	WARN_ON(tzd == NULL);
	psy = tzd->devdata;
	ret = psy->get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);

	/* Convert tenths of degree Celsius to milli degree Celsius. */
	if (!ret)
		*temp = val.intval * 100;

	return ret;
}

static struct thermal_zone_device_ops psy_tzd_ops = {
	.get_temp = power_supply_read_temp,
};

static int psy_register_thermal(struct power_supply *psy)
{
	int i;

	/* Register battery zone device psy reports temperature */
	for (i = 0; i < psy->num_properties; i++) {
		if (psy->properties[i] == POWER_SUPPLY_PROP_TEMP) {
203
			psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
204
					psy, &psy_tzd_ops, NULL, 0, 0);
205 206 207 208 209 210 211 212 213 214 215 216 217 218
			if (IS_ERR(psy->tzd))
				return PTR_ERR(psy->tzd);
			break;
		}
	}
	return 0;
}

static void psy_unregister_thermal(struct power_supply *psy)
{
	if (IS_ERR_OR_NULL(psy->tzd))
		return;
	thermal_zone_device_unregister(psy->tzd);
}
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

/* thermal cooling device callbacks */
static int ps_get_max_charge_cntl_limit(struct thermal_cooling_device *tcd,
					unsigned long *state)
{
	struct power_supply *psy;
	union power_supply_propval val;
	int ret;

	psy = tcd->devdata;
	ret = psy->get_property(psy,
		POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
	if (!ret)
		*state = val.intval;

	return ret;
}

static int ps_get_cur_chrage_cntl_limit(struct thermal_cooling_device *tcd,
					unsigned long *state)
{
	struct power_supply *psy;
	union power_supply_propval val;
	int ret;

	psy = tcd->devdata;
	ret = psy->get_property(psy,
		POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
	if (!ret)
		*state = val.intval;

	return ret;
}

static int ps_set_cur_charge_cntl_limit(struct thermal_cooling_device *tcd,
					unsigned long state)
{
	struct power_supply *psy;
	union power_supply_propval val;
	int ret;

	psy = tcd->devdata;
	val.intval = state;
	ret = psy->set_property(psy,
		POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);

	return ret;
}

static struct thermal_cooling_device_ops psy_tcd_ops = {
	.get_max_state = ps_get_max_charge_cntl_limit,
	.get_cur_state = ps_get_cur_chrage_cntl_limit,
	.set_cur_state = ps_set_cur_charge_cntl_limit,
};

static int psy_register_cooler(struct power_supply *psy)
{
	int i;

	/* Register for cooling device if psy can control charging */
	for (i = 0; i < psy->num_properties; i++) {
		if (psy->properties[i] ==
				POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
			psy->tcd = thermal_cooling_device_register(
							(char *)psy->name,
							psy, &psy_tcd_ops);
			if (IS_ERR(psy->tcd))
				return PTR_ERR(psy->tcd);
			break;
		}
	}
	return 0;
}

static void psy_unregister_cooler(struct power_supply *psy)
{
	if (IS_ERR_OR_NULL(psy->tcd))
		return;
	thermal_cooling_device_unregister(psy->tcd);
}
299 300 301 302 303 304 305 306 307
#else
static int psy_register_thermal(struct power_supply *psy)
{
	return 0;
}

static void psy_unregister_thermal(struct power_supply *psy)
{
}
308 309 310 311 312 313 314 315 316

static int psy_register_cooler(struct power_supply *psy)
{
	return 0;
}

static void psy_unregister_cooler(struct power_supply *psy)
{
}
317 318
#endif

319 320
int power_supply_register(struct device *parent, struct power_supply *psy)
{
321 322
	struct device *dev;
	int rc;
323

324 325 326
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return -ENOMEM;
327

328
	device_initialize(dev);
329

330 331 332 333 334 335 336
	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;

337 338
	INIT_WORK(&psy->changed_work, power_supply_changed_work);

339 340 341 342 343
	rc = kobject_set_name(&dev->kobj, "%s", psy->name);
	if (rc)
		goto kobject_set_name_failed;

	rc = device_add(dev);
344
	if (rc)
345 346
		goto device_add_failed;

347 348 349 350
	rc = psy_register_thermal(psy);
	if (rc)
		goto register_thermal_failed;

351 352 353 354
	rc = psy_register_cooler(psy);
	if (rc)
		goto register_cooler_failed;

355 356 357 358 359 360 361 362 363
	rc = power_supply_create_triggers(psy);
	if (rc)
		goto create_triggers_failed;

	power_supply_changed(psy);

	goto success;

create_triggers_failed:
364 365
	psy_unregister_cooler(psy);
register_cooler_failed:
366 367
	psy_unregister_thermal(psy);
register_thermal_failed:
368
	device_del(dev);
369 370
kobject_set_name_failed:
device_add_failed:
371
	put_device(dev);
372 373 374
success:
	return rc;
}
375
EXPORT_SYMBOL_GPL(power_supply_register);
376 377 378

void power_supply_unregister(struct power_supply *psy)
{
379
	cancel_work_sync(&psy->changed_work);
380
	sysfs_remove_link(&psy->dev->kobj, "powers");
381
	power_supply_remove_triggers(psy);
382
	psy_unregister_cooler(psy);
383
	psy_unregister_thermal(psy);
384 385
	device_unregister(psy->dev);
}
386
EXPORT_SYMBOL_GPL(power_supply_unregister);
387 388 389 390 391 392 393 394 395

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;
396
	power_supply_init_attrs(&power_supply_dev_type);
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

	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");