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

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

28 29 30
ATOMIC_NOTIFIER_HEAD(power_supply_notifier);
EXPORT_SYMBOL_GPL(power_supply_notifier);

31 32
static struct device_type power_supply_dev_type;

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
static bool __power_supply_is_supplied_by(struct power_supply *supplier,
					 struct power_supply *supply)
{
	int i;

	if (!supply->supplied_from && !supplier->supplied_to)
		return false;

	/* Support both supplied_to and supplied_from modes */
	if (supply->supplied_from) {
		if (!supplier->name)
			return false;
		for (i = 0; i < supply->num_supplies; i++)
			if (!strcmp(supplier->name, supply->supplied_from[i]))
				return true;
	} else {
		if (!supply->name)
			return false;
		for (i = 0; i < supplier->num_supplicants; i++)
			if (!strcmp(supplier->supplied_to[i], supply->name))
				return true;
	}

	return false;
}

59 60
static int __power_supply_changed_work(struct device *dev, void *data)
{
61
	struct power_supply *psy = data;
62 63
	struct power_supply *pst = dev_get_drvdata(dev);

64 65 66 67 68
	if (__power_supply_is_supplied_by(psy, pst)) {
		if (pst->external_power_changed)
			pst->external_power_changed(pst);
	}

69 70 71
	return 0;
}

72 73
static void power_supply_changed_work(struct work_struct *work)
{
74
	unsigned long flags;
75 76 77
	struct power_supply *psy = container_of(work, struct power_supply,
						changed_work);

78
	dev_dbg(psy->dev, "%s\n", __func__);
79

80
	spin_lock_irqsave(&psy->changed_lock, flags);
81 82 83 84 85 86 87 88
	/*
	 * Check 'changed' here to avoid issues due to race between
	 * power_supply_changed() and this routine. In worst case
	 * power_supply_changed() can be called again just before we take above
	 * lock. During the first call of this routine we will mark 'changed' as
	 * false and it will stay false for the next call as well.
	 */
	if (likely(psy->changed)) {
89 90 91 92 93
		psy->changed = false;
		spin_unlock_irqrestore(&psy->changed_lock, flags);
		class_for_each_device(power_supply_class, NULL, psy,
				      __power_supply_changed_work);
		power_supply_update_leds(psy);
94 95
		atomic_notifier_call_chain(&power_supply_notifier,
				PSY_EVENT_PROP_CHANGED, psy);
96 97 98
		kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE);
		spin_lock_irqsave(&psy->changed_lock, flags);
	}
99

100
	/*
101 102 103
	 * Hold the wakeup_source until all events are processed.
	 * power_supply_changed() might have called again and have set 'changed'
	 * to true.
104
	 */
105
	if (likely(!psy->changed))
106 107
		pm_relax(psy->dev);
	spin_unlock_irqrestore(&psy->changed_lock, flags);
108 109 110 111
}

void power_supply_changed(struct power_supply *psy)
{
112 113
	unsigned long flags;

114
	dev_dbg(psy->dev, "%s\n", __func__);
115

116 117 118 119
	spin_lock_irqsave(&psy->changed_lock, flags);
	psy->changed = true;
	pm_stay_awake(psy->dev);
	spin_unlock_irqrestore(&psy->changed_lock, flags);
120 121
	schedule_work(&psy->changed_work);
}
122
EXPORT_SYMBOL_GPL(power_supply_changed);
123

124 125 126 127 128 129
#ifdef CONFIG_OF
#include <linux/of.h>

static int __power_supply_populate_supplied_from(struct device *dev,
						 void *data)
{
130
	struct power_supply *psy = data;
131 132 133 134 135 136 137
	struct power_supply *epsy = dev_get_drvdata(dev);
	struct device_node *np;
	int i = 0;

	do {
		np = of_parse_phandle(psy->of_node, "power-supplies", i++);
		if (!np)
138
			break;
139 140 141 142 143 144

		if (np == epsy->of_node) {
			dev_info(psy->dev, "%s: Found supply : %s\n",
				psy->name, epsy->name);
			psy->supplied_from[i-1] = (char *)epsy->name;
			psy->num_supplies++;
145
			of_node_put(np);
146 147
			break;
		}
148
		of_node_put(np);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	} while (np);

	return 0;
}

static int power_supply_populate_supplied_from(struct power_supply *psy)
{
	int error;

	error = class_for_each_device(power_supply_class, NULL, psy,
				      __power_supply_populate_supplied_from);

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

	return error;
}

static int  __power_supply_find_supply_from_node(struct device *dev,
						 void *data)
{
169
	struct device_node *np = data;
170 171
	struct power_supply *epsy = dev_get_drvdata(dev);

172
	/* returning non-zero breaks out of class_for_each_device loop */
173
	if (epsy->of_node == np)
174
		return 1;
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

	return 0;
}

static int power_supply_find_supply_from_node(struct device_node *supply_node)
{
	int error;
	struct device *dev;
	struct class_dev_iter iter;

	/*
	 * Use iterator to see if any other device is registered.
	 * This is required since class_for_each_device returns 0
	 * if there are no devices registered.
	 */
	class_dev_iter_init(&iter, power_supply_class, NULL, NULL);
	dev = class_dev_iter_next(&iter);

	if (!dev)
		return -EPROBE_DEFER;

	/*
197 198 199 200 201 202 203 204
	 * class_for_each_device() either returns its own errors or values
	 * returned by __power_supply_find_supply_from_node().
	 *
	 * __power_supply_find_supply_from_node() will return 0 (no match)
	 * or 1 (match).
	 *
	 * We return 0 if class_for_each_device() returned 1, -EPROBE_DEFER if
	 * it returned 0, or error as returned by it.
205 206 207 208
	 */
	error = class_for_each_device(power_supply_class, NULL, supply_node,
				       __power_supply_find_supply_from_node);

209
	return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
}

static int power_supply_check_supplies(struct power_supply *psy)
{
	struct device_node *np;
	int cnt = 0;

	/* If there is already a list honor it */
	if (psy->supplied_from && psy->num_supplies > 0)
		return 0;

	/* No device node found, nothing to do */
	if (!psy->of_node)
		return 0;

	do {
		int ret;

		np = of_parse_phandle(psy->of_node, "power-supplies", cnt++);
		if (!np)
230
			break;
231 232

		ret = power_supply_find_supply_from_node(np);
233 234
		of_node_put(np);

235
		if (ret) {
236 237
			dev_dbg(psy->dev, "Failed to find supply!\n");
			return ret;
238 239 240
		}
	} while (np);

241 242 243 244
	/* Missing valid "power-supplies" entries */
	if (cnt == 1)
		return 0;

245 246 247 248 249 250 251 252
	/* All supplies found, allocate char ** array for filling */
	psy->supplied_from = devm_kzalloc(psy->dev, sizeof(psy->supplied_from),
					  GFP_KERNEL);
	if (!psy->supplied_from) {
		dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
		return -ENOMEM;
	}

253
	*psy->supplied_from = devm_kzalloc(psy->dev, sizeof(char *) * (cnt - 1),
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
					   GFP_KERNEL);
	if (!*psy->supplied_from) {
		dev_err(psy->dev, "Couldn't allocate memory for supply list\n");
		return -ENOMEM;
	}

	return power_supply_populate_supplied_from(psy);
}
#else
static inline int power_supply_check_supplies(struct power_supply *psy)
{
	return 0;
}
#endif

269
static int __power_supply_am_i_supplied(struct device *dev, void *data)
270 271
{
	union power_supply_propval ret = {0,};
272
	struct power_supply *psy = data;
273 274
	struct power_supply *epsy = dev_get_drvdata(dev);

275
	if (__power_supply_is_supplied_by(epsy, psy))
276 277
		if (!epsy->get_property(epsy, POWER_SUPPLY_PROP_ONLINE, &ret))
			return ret.intval;
278

279 280 281 282 283 284 285
	return 0;
}

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

286
	error = class_for_each_device(power_supply_class, NULL, psy,
287
				      __power_supply_am_i_supplied);
288

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

291
	return error;
292
}
293
EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
294

295 296 297 298
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);
299
	unsigned int *count = data;
300

301
	(*count)++;
302 303
	if (psy->type != POWER_SUPPLY_TYPE_BATTERY)
		if (!psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &ret))
304
			return ret.intval;
305

306 307 308 309 310 311
	return 0;
}

int power_supply_is_system_supplied(void)
{
	int error;
312
	unsigned int count = 0;
313

314
	error = class_for_each_device(power_supply_class, NULL, &count,
315 316
				      __power_supply_is_system_supplied);

317 318 319 320 321 322 323
	/*
	 * 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;

324 325
	return error;
}
326
EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
327

328 329 330 331 332 333 334 335 336 337 338
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);

339
static int power_supply_match_device_by_name(struct device *dev, const void *data)
340 341 342 343 344 345 346
{
	const char *name = data;
	struct power_supply *psy = dev_get_drvdata(dev);

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

347
struct power_supply *power_supply_get_by_name(const char *name)
348 349 350 351 352 353 354 355
{
	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);

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
#ifdef CONFIG_OF
static int power_supply_match_device_node(struct device *dev, const void *data)
{
	return dev->parent && dev->parent->of_node == data;
}

struct power_supply *power_supply_get_by_phandle(struct device_node *np,
							const char *property)
{
	struct device_node *power_supply_np;
	struct device *dev;

	power_supply_np = of_parse_phandle(np, property, 0);
	if (!power_supply_np)
		return ERR_PTR(-ENODEV);

	dev = class_find_device(power_supply_class, NULL, power_supply_np,
						power_supply_match_device_node);

	of_node_put(power_supply_np);

	return dev ? dev_get_drvdata(dev) : NULL;
}
EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
#endif /* CONFIG_OF */

382 383
int power_supply_powers(struct power_supply *psy, struct device *dev)
{
384
	return sysfs_create_link(&psy->dev->kobj, &dev->kobj, "powers");
385 386 387
}
EXPORT_SYMBOL_GPL(power_supply_powers);

388 389 390 391 392 393
static void power_supply_dev_release(struct device *dev)
{
	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
	kfree(dev);
}

394 395 396 397 398 399 400 401 402 403 404 405
int power_supply_reg_notifier(struct notifier_block *nb)
{
	return atomic_notifier_chain_register(&power_supply_notifier, nb);
}
EXPORT_SYMBOL_GPL(power_supply_reg_notifier);

void power_supply_unreg_notifier(struct notifier_block *nb)
{
	atomic_notifier_chain_unregister(&power_supply_notifier, nb);
}
EXPORT_SYMBOL_GPL(power_supply_unreg_notifier);

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
#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) {
436
			psy->tzd = thermal_zone_device_register(psy->name, 0, 0,
437
					psy, &psy_tzd_ops, NULL, 0, 0);
438 439 440 441 442 443 444 445 446 447 448 449 450 451
			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);
}
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

/* 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);
}
532 533 534 535 536 537 538 539 540
#else
static int psy_register_thermal(struct power_supply *psy)
{
	return 0;
}

static void psy_unregister_thermal(struct power_supply *psy)
{
}
541 542 543 544 545 546 547 548 549

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

static void psy_unregister_cooler(struct power_supply *psy)
{
}
550 551
#endif

552 553
static int __power_supply_register(struct device *parent,
				   struct power_supply *psy, bool ws)
554
{
555 556
	struct device *dev;
	int rc;
557

558 559 560
	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev)
		return -ENOMEM;
561

562
	device_initialize(dev);
563

564 565 566 567 568 569 570
	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;

571 572 573 574
	rc = dev_set_name(dev, "%s", psy->name);
	if (rc)
		goto dev_set_name_failed;

575 576
	INIT_WORK(&psy->changed_work, power_supply_changed_work);

577 578 579 580 581 582
	rc = power_supply_check_supplies(psy);
	if (rc) {
		dev_info(dev, "Not all required supplies found, defer probe\n");
		goto check_supplies_failed;
	}

583
	spin_lock_init(&psy->changed_lock);
584
	rc = device_init_wakeup(dev, ws);
585 586 587
	if (rc)
		goto wakeup_init_failed;

588
	rc = device_add(dev);
589
	if (rc)
590 591
		goto device_add_failed;

592 593 594 595
	rc = psy_register_thermal(psy);
	if (rc)
		goto register_thermal_failed;

596 597 598 599
	rc = psy_register_cooler(psy);
	if (rc)
		goto register_cooler_failed;

600 601 602 603 604 605 606 607 608
	rc = power_supply_create_triggers(psy);
	if (rc)
		goto create_triggers_failed;

	power_supply_changed(psy);

	goto success;

create_triggers_failed:
609 610
	psy_unregister_cooler(psy);
register_cooler_failed:
611 612
	psy_unregister_thermal(psy);
register_thermal_failed:
613
	device_del(dev);
614
device_add_failed:
615
wakeup_init_failed:
616
check_supplies_failed:
617
dev_set_name_failed:
618
	put_device(dev);
619 620 621
success:
	return rc;
}
622 623 624 625 626

int power_supply_register(struct device *parent, struct power_supply *psy)
{
	return __power_supply_register(parent, psy, true);
}
627
EXPORT_SYMBOL_GPL(power_supply_register);
628

629 630 631 632 633 634
int power_supply_register_no_ws(struct device *parent, struct power_supply *psy)
{
	return __power_supply_register(parent, psy, false);
}
EXPORT_SYMBOL_GPL(power_supply_register_no_ws);

635 636
void power_supply_unregister(struct power_supply *psy)
{
637
	cancel_work_sync(&psy->changed_work);
638
	sysfs_remove_link(&psy->dev->kobj, "powers");
639
	power_supply_remove_triggers(psy);
640
	psy_unregister_cooler(psy);
641
	psy_unregister_thermal(psy);
642
	device_init_wakeup(psy->dev, false);
643 644
	device_unregister(psy->dev);
}
645
EXPORT_SYMBOL_GPL(power_supply_unregister);
646 647 648 649 650 651 652 653 654

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;
655
	power_supply_init_attrs(&power_supply_dev_type);
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672

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