power_supply_core.c 25.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
#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
#define POWER_SUPPLY_DEFERRED_REGISTER_TIME	msecs_to_jiffies(10)

35 36 37 38 39 40 41 42 43 44
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) {
45
		if (!supplier->desc->name)
46 47
			return false;
		for (i = 0; i < supply->num_supplies; i++)
48
			if (!strcmp(supplier->desc->name, supply->supplied_from[i]))
49 50
				return true;
	} else {
51
		if (!supply->desc->name)
52 53
			return false;
		for (i = 0; i < supplier->num_supplicants; i++)
54
			if (!strcmp(supplier->supplied_to[i], supply->desc->name))
55 56 57 58 59 60
				return true;
	}

	return false;
}

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

66
	if (__power_supply_is_supplied_by(psy, pst)) {
67 68
		if (pst->desc->external_power_changed)
			pst->desc->external_power_changed(pst);
69 70
	}

71 72 73
	return 0;
}

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

80
	dev_dbg(&psy->dev, "%s\n", __func__);
81

82
	spin_lock_irqsave(&psy->changed_lock, flags);
83 84 85 86 87 88 89 90
	/*
	 * 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)) {
91 92 93 94 95
		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);
96 97
		atomic_notifier_call_chain(&power_supply_notifier,
				PSY_EVENT_PROP_CHANGED, psy);
98
		kobject_uevent(&psy->dev.kobj, KOBJ_CHANGE);
99 100
		spin_lock_irqsave(&psy->changed_lock, flags);
	}
101

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

void power_supply_changed(struct power_supply *psy)
{
114 115
	unsigned long flags;

116
	dev_dbg(&psy->dev, "%s\n", __func__);
117

118 119
	spin_lock_irqsave(&psy->changed_lock, flags);
	psy->changed = true;
120
	pm_stay_awake(&psy->dev);
121
	spin_unlock_irqrestore(&psy->changed_lock, flags);
122 123
	schedule_work(&psy->changed_work);
}
124
EXPORT_SYMBOL_GPL(power_supply_changed);
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
/*
 * Notify that power supply was registered after parent finished the probing.
 *
 * Often power supply is registered from driver's probe function. However
 * calling power_supply_changed() directly from power_supply_register()
 * would lead to execution of get_property() function provided by the driver
 * too early - before the probe ends.
 *
 * Avoid that by waiting on parent's mutex.
 */
static void power_supply_deferred_register_work(struct work_struct *work)
{
	struct power_supply *psy = container_of(work, struct power_supply,
						deferred_register_work.work);

	if (psy->dev.parent)
		mutex_lock(&psy->dev.parent->mutex);

	power_supply_changed(psy);

	if (psy->dev.parent)
		mutex_unlock(&psy->dev.parent->mutex);
}

150 151 152 153 154 155
#ifdef CONFIG_OF
#include <linux/of.h>

static int __power_supply_populate_supplied_from(struct device *dev,
						 void *data)
{
156
	struct power_supply *psy = data;
157 158 159 160 161 162 163
	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)
164
			break;
165 166

		if (np == epsy->of_node) {
167 168 169
			dev_info(&psy->dev, "%s: Found supply : %s\n",
				psy->desc->name, epsy->desc->name);
			psy->supplied_from[i-1] = (char *)epsy->desc->name;
170
			psy->num_supplies++;
171
			of_node_put(np);
172 173
			break;
		}
174
		of_node_put(np);
175 176 177 178 179 180 181 182 183 184 185 186
	} 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);

187
	dev_dbg(&psy->dev, "%s %d\n", __func__, error);
188 189 190 191 192 193 194

	return error;
}

static int  __power_supply_find_supply_from_node(struct device *dev,
						 void *data)
{
195
	struct device_node *np = data;
196 197
	struct power_supply *epsy = dev_get_drvdata(dev);

198
	/* returning non-zero breaks out of class_for_each_device loop */
199
	if (epsy->of_node == np)
200
		return 1;
201 202 203 204 205 206 207 208 209

	return 0;
}

static int power_supply_find_supply_from_node(struct device_node *supply_node)
{
	int error;

	/*
210 211 212 213 214 215 216 217
	 * 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.
218 219 220 221
	 */
	error = class_for_each_device(power_supply_class, NULL, supply_node,
				       __power_supply_find_supply_from_node);

222
	return error ? (error == 1 ? 0 : error) : -EPROBE_DEFER;
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
}

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)
243
			break;
244 245

		ret = power_supply_find_supply_from_node(np);
246 247
		of_node_put(np);

248
		if (ret) {
249
			dev_dbg(&psy->dev, "Failed to find supply!\n");
250
			return ret;
251 252 253
		}
	} while (np);

254 255 256 257
	/* Missing valid "power-supplies" entries */
	if (cnt == 1)
		return 0;

258
	/* All supplies found, allocate char ** array for filling */
259
	psy->supplied_from = devm_kzalloc(&psy->dev, sizeof(psy->supplied_from),
260 261
					  GFP_KERNEL);
	if (!psy->supplied_from) {
262
		dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
263 264 265
		return -ENOMEM;
	}

266 267
	*psy->supplied_from = devm_kzalloc(&psy->dev,
					   sizeof(char *) * (cnt - 1),
268 269
					   GFP_KERNEL);
	if (!*psy->supplied_from) {
270
		dev_err(&psy->dev, "Couldn't allocate memory for supply list\n");
271 272 273 274 275 276 277 278 279 280 281 282
		return -ENOMEM;
	}

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

283
static int __power_supply_am_i_supplied(struct device *dev, void *data)
284 285
{
	union power_supply_propval ret = {0,};
286
	struct power_supply *psy = data;
287 288
	struct power_supply *epsy = dev_get_drvdata(dev);

289
	if (__power_supply_is_supplied_by(epsy, psy))
290 291
		if (!epsy->desc->get_property(epsy, POWER_SUPPLY_PROP_ONLINE,
					&ret))
292
			return ret.intval;
293

294 295 296 297 298 299 300
	return 0;
}

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

301
	error = class_for_each_device(power_supply_class, NULL, psy,
302
				      __power_supply_am_i_supplied);
303

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

306
	return error;
307
}
308
EXPORT_SYMBOL_GPL(power_supply_am_i_supplied);
309

310 311 312 313
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);
314
	unsigned int *count = data;
315

316
	(*count)++;
317 318 319
	if (psy->desc->type != POWER_SUPPLY_TYPE_BATTERY)
		if (!psy->desc->get_property(psy, POWER_SUPPLY_PROP_ONLINE,
					&ret))
320
			return ret.intval;
321

322 323 324 325 326 327
	return 0;
}

int power_supply_is_system_supplied(void)
{
	int error;
328
	unsigned int count = 0;
329

330
	error = class_for_each_device(power_supply_class, NULL, &count,
331 332
				      __power_supply_is_system_supplied);

333 334 335 336 337 338 339
	/*
	 * 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;

340 341
	return error;
}
342
EXPORT_SYMBOL_GPL(power_supply_is_system_supplied);
343

344 345
int power_supply_set_battery_charged(struct power_supply *psy)
{
346
	if (atomic_read(&psy->use_cnt) >= 0 &&
347 348 349
			psy->desc->type == POWER_SUPPLY_TYPE_BATTERY &&
			psy->desc->set_charged) {
		psy->desc->set_charged(psy);
350 351 352 353 354 355 356
		return 0;
	}

	return -EINVAL;
}
EXPORT_SYMBOL_GPL(power_supply_set_battery_charged);

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

362
	return strcmp(psy->desc->name, name) == 0;
363 364
}

365 366 367 368 369 370 371 372 373 374 375
/**
 * power_supply_get_by_name() - Search for a power supply and returns its ref
 * @name: Power supply name to fetch
 *
 * If power supply was found, it increases reference count for the
 * internal power supply's device. The user should power_supply_put()
 * after usage.
 *
 * Return: On success returns a reference to a power supply with
 * matching name equals to @name, a NULL otherwise.
 */
376
struct power_supply *power_supply_get_by_name(const char *name)
377
{
378
	struct power_supply *psy = NULL;
379 380 381
	struct device *dev = class_find_device(power_supply_class, NULL, name,
					power_supply_match_device_by_name);

382 383 384 385 386 387
	if (dev) {
		psy = dev_get_drvdata(dev);
		atomic_inc(&psy->use_cnt);
	}

	return psy;
388 389 390
}
EXPORT_SYMBOL_GPL(power_supply_get_by_name);

391 392 393 394 395 396 397 398 399 400 401
/**
 * power_supply_put() - Drop reference obtained with power_supply_get_by_name
 * @psy: Reference to put
 *
 * The reference to power supply should be put before unregistering
 * the power supply.
 */
void power_supply_put(struct power_supply *psy)
{
	might_sleep();

402
	atomic_dec(&psy->use_cnt);
403 404 405 406
	put_device(&psy->dev);
}
EXPORT_SYMBOL_GPL(power_supply_put);

407 408 409 410 411 412
#ifdef CONFIG_OF
static int power_supply_match_device_node(struct device *dev, const void *data)
{
	return dev->parent && dev->parent->of_node == data;
}

413 414 415
/**
 * power_supply_get_by_phandle() - Search for a power supply and returns its ref
 * @np: Pointer to device node holding phandle property
416
 * @property: Name of property holding a power supply name
417 418 419 420 421 422 423 424
 *
 * If power supply was found, it increases reference count for the
 * internal power supply's device. The user should power_supply_put()
 * after usage.
 *
 * Return: On success returns a reference to a power supply with
 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
 */
425 426 427 428
struct power_supply *power_supply_get_by_phandle(struct device_node *np,
							const char *property)
{
	struct device_node *power_supply_np;
429
	struct power_supply *psy = NULL;
430 431 432 433 434 435 436 437 438 439 440
	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);

441 442 443 444 445 446
	if (dev) {
		psy = dev_get_drvdata(dev);
		atomic_inc(&psy->use_cnt);
	}

	return psy;
447 448
}
EXPORT_SYMBOL_GPL(power_supply_get_by_phandle);
449 450 451 452 453 454 455 456 457 458 459 460

static void devm_power_supply_put(struct device *dev, void *res)
{
	struct power_supply **psy = res;

	power_supply_put(*psy);
}

/**
 * devm_power_supply_get_by_phandle() - Resource managed version of
 *  power_supply_get_by_phandle()
 * @dev: Pointer to device holding phandle property
461
 * @property: Name of property holding a power supply phandle
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
 *
 * Return: On success returns a reference to a power supply with
 * matching name equals to value under @property, NULL or ERR_PTR otherwise.
 */
struct power_supply *devm_power_supply_get_by_phandle(struct device *dev,
						      const char *property)
{
	struct power_supply **ptr, *psy;

	if (!dev->of_node)
		return ERR_PTR(-ENODEV);

	ptr = devres_alloc(devm_power_supply_put, sizeof(*ptr), GFP_KERNEL);
	if (!ptr)
		return ERR_PTR(-ENOMEM);

	psy = power_supply_get_by_phandle(dev->of_node, property);
	if (IS_ERR_OR_NULL(psy)) {
		devres_free(ptr);
	} else {
		*ptr = psy;
		devres_add(dev, ptr);
	}
	return psy;
}
EXPORT_SYMBOL_GPL(devm_power_supply_get_by_phandle);
488 489
#endif /* CONFIG_OF */

490 491 492 493
int power_supply_get_property(struct power_supply *psy,
			    enum power_supply_property psp,
			    union power_supply_propval *val)
{
494 495 496
	if (atomic_read(&psy->use_cnt) <= 0) {
		if (!psy->initialized)
			return -EAGAIN;
497
		return -ENODEV;
498
	}
499

500
	return psy->desc->get_property(psy, psp, val);
501 502 503 504 505 506 507
}
EXPORT_SYMBOL_GPL(power_supply_get_property);

int power_supply_set_property(struct power_supply *psy,
			    enum power_supply_property psp,
			    const union power_supply_propval *val)
{
508
	if (atomic_read(&psy->use_cnt) <= 0 || !psy->desc->set_property)
509 510
		return -ENODEV;

511
	return psy->desc->set_property(psy, psp, val);
512 513 514 515 516 517
}
EXPORT_SYMBOL_GPL(power_supply_set_property);

int power_supply_property_is_writeable(struct power_supply *psy,
					enum power_supply_property psp)
{
518 519
	if (atomic_read(&psy->use_cnt) <= 0 ||
			!psy->desc->property_is_writeable)
520 521
		return -ENODEV;

522
	return psy->desc->property_is_writeable(psy, psp);
523 524 525 526 527
}
EXPORT_SYMBOL_GPL(power_supply_property_is_writeable);

void power_supply_external_power_changed(struct power_supply *psy)
{
528 529
	if (atomic_read(&psy->use_cnt) <= 0 ||
			!psy->desc->external_power_changed)
530 531
		return;

532
	psy->desc->external_power_changed(psy);
533 534 535
}
EXPORT_SYMBOL_GPL(power_supply_external_power_changed);

536 537
int power_supply_powers(struct power_supply *psy, struct device *dev)
{
538
	return sysfs_create_link(&psy->dev.kobj, &dev->kobj, "powers");
539 540 541
}
EXPORT_SYMBOL_GPL(power_supply_powers);

542 543
static void power_supply_dev_release(struct device *dev)
{
544
	struct power_supply *psy = container_of(dev, struct power_supply, dev);
545
	pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
546
	kfree(psy);
547 548
}

549 550 551 552 553 554 555 556 557 558 559 560
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);

561 562
#ifdef CONFIG_THERMAL
static int power_supply_read_temp(struct thermal_zone_device *tzd,
563
		int *temp)
564 565 566 567 568 569 570
{
	struct power_supply *psy;
	union power_supply_propval val;
	int ret;

	WARN_ON(tzd == NULL);
	psy = tzd->devdata;
571 572 573
	ret = power_supply_get_property(psy, POWER_SUPPLY_PROP_TEMP, &val);
	if (ret)
		return ret;
574 575

	/* Convert tenths of degree Celsius to milli degree Celsius. */
576
	*temp = val.intval * 100;
577 578 579 580 581 582 583 584 585 586 587 588

	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;

589
	if (psy->desc->no_thermal)
590 591
		return 0;

592
	/* Register battery zone device psy reports temperature */
593 594 595 596
	for (i = 0; i < psy->desc->num_properties; i++) {
		if (psy->desc->properties[i] == POWER_SUPPLY_PROP_TEMP) {
			psy->tzd = thermal_zone_device_register(psy->desc->name,
					0, 0, psy, &psy_tzd_ops, NULL, 0, 0);
597
			return PTR_ERR_OR_ZERO(psy->tzd);
598 599 600 601 602 603 604 605 606 607 608
		}
	}
	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);
}
609 610 611 612 613 614 615 616 617 618

/* 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;
619 620 621 622 623 624
	ret = power_supply_get_property(psy,
			POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX, &val);
	if (ret)
		return ret;

	*state = val.intval;
625 626 627 628 629 630 631 632 633 634 635 636

	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;
637 638 639 640 641 642
	ret = power_supply_get_property(psy,
			POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT, &val);
	if (ret)
		return ret;

	*state = val.intval;
643 644 645 646 647 648 649 650 651 652 653 654 655

	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;
656
	ret = psy->desc->set_property(psy,
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
		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 */
673 674
	for (i = 0; i < psy->desc->num_properties; i++) {
		if (psy->desc->properties[i] ==
675 676
				POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT) {
			psy->tcd = thermal_cooling_device_register(
677
							(char *)psy->desc->name,
678
							psy, &psy_tcd_ops);
679
			return PTR_ERR_OR_ZERO(psy->tcd);
680 681 682 683 684 685 686 687 688 689 690
		}
	}
	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);
}
691 692 693 694 695 696 697 698 699
#else
static int psy_register_thermal(struct power_supply *psy)
{
	return 0;
}

static void psy_unregister_thermal(struct power_supply *psy)
{
}
700 701 702 703 704 705 706 707 708

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

static void psy_unregister_cooler(struct power_supply *psy)
{
}
709 710
#endif

711 712 713
static struct power_supply *__must_check
__power_supply_register(struct device *parent,
				   const struct power_supply_desc *desc,
714 715
				   const struct power_supply_config *cfg,
				   bool ws)
716
{
717
	struct device *dev;
718
	struct power_supply *psy;
719
	int rc;
720

721 722 723 724
	if (!parent)
		pr_warn("%s: Expected proper parent device for '%s'\n",
			__func__, desc->name);

725 726 727 728 729
	psy = kzalloc(sizeof(*psy), GFP_KERNEL);
	if (!psy)
		return ERR_PTR(-ENOMEM);

	dev = &psy->dev;
730

731
	device_initialize(dev);
732

733 734 735 736 737
	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);
738
	psy->desc = desc;
739 740 741 742 743 744
	if (cfg) {
		psy->drv_data = cfg->drv_data;
		psy->of_node = cfg->of_node;
		psy->supplied_to = cfg->supplied_to;
		psy->num_supplicants = cfg->num_supplicants;
	}
745

746
	rc = dev_set_name(dev, "%s", desc->name);
747 748 749
	if (rc)
		goto dev_set_name_failed;

750
	INIT_WORK(&psy->changed_work, power_supply_changed_work);
751 752
	INIT_DELAYED_WORK(&psy->deferred_register_work,
			  power_supply_deferred_register_work);
753

754 755 756 757 758 759
	rc = power_supply_check_supplies(psy);
	if (rc) {
		dev_info(dev, "Not all required supplies found, defer probe\n");
		goto check_supplies_failed;
	}

760
	spin_lock_init(&psy->changed_lock);
761
	rc = device_init_wakeup(dev, ws);
762 763 764
	if (rc)
		goto wakeup_init_failed;

765
	rc = device_add(dev);
766
	if (rc)
767 768
		goto device_add_failed;

769 770 771 772
	rc = psy_register_thermal(psy);
	if (rc)
		goto register_thermal_failed;

773 774 775 776
	rc = psy_register_cooler(psy);
	if (rc)
		goto register_cooler_failed;

777 778 779 780
	rc = power_supply_create_triggers(psy);
	if (rc)
		goto create_triggers_failed;

781 782 783 784 785 786 787 788 789 790
	/*
	 * Update use_cnt after any uevents (most notably from device_add()).
	 * We are here still during driver's probe but
	 * the power_supply_uevent() calls back driver's get_property
	 * method so:
	 * 1. Driver did not assigned the returned struct power_supply,
	 * 2. Driver could not finish initialization (anything in its probe
	 *    after calling power_supply_register()).
	 */
	atomic_inc(&psy->use_cnt);
791
	psy->initialized = true;
792 793 794 795

	queue_delayed_work(system_power_efficient_wq,
			   &psy->deferred_register_work,
			   POWER_SUPPLY_DEFERRED_REGISTER_TIME);
796

797
	return psy;
798 799

create_triggers_failed:
800 801
	psy_unregister_cooler(psy);
register_cooler_failed:
802 803
	psy_unregister_thermal(psy);
register_thermal_failed:
804
	device_del(dev);
805
device_add_failed:
806
wakeup_init_failed:
807
check_supplies_failed:
808
dev_set_name_failed:
809
	put_device(dev);
810
	return ERR_PTR(rc);
811
}
812

813 814
/**
 * power_supply_register() - Register new power supply
815 816
 * @parent:	Device to be a parent of power supply's device, usually
 *		the device which probe function calls this
817 818 819 820 821 822 823 824 825 826 827 828
 * @desc:	Description of power supply, must be valid through whole
 *		lifetime of this power supply
 * @cfg:	Run-time specific configuration accessed during registering,
 *		may be NULL
 *
 * Return: A pointer to newly allocated power_supply on success
 * or ERR_PTR otherwise.
 * Use power_supply_unregister() on returned power_supply pointer to release
 * resources.
 */
struct power_supply *__must_check power_supply_register(struct device *parent,
		const struct power_supply_desc *desc,
829
		const struct power_supply_config *cfg)
830
{
831
	return __power_supply_register(parent, desc, cfg, true);
832
}
833
EXPORT_SYMBOL_GPL(power_supply_register);
834

835
/**
836
 * power_supply_register_no_ws() - Register new non-waking-source power supply
837 838
 * @parent:	Device to be a parent of power supply's device, usually
 *		the device which probe function calls this
839 840 841 842 843 844 845 846 847 848 849 850 851
 * @desc:	Description of power supply, must be valid through whole
 *		lifetime of this power supply
 * @cfg:	Run-time specific configuration accessed during registering,
 *		may be NULL
 *
 * Return: A pointer to newly allocated power_supply on success
 * or ERR_PTR otherwise.
 * Use power_supply_unregister() on returned power_supply pointer to release
 * resources.
 */
struct power_supply *__must_check
power_supply_register_no_ws(struct device *parent,
		const struct power_supply_desc *desc,
852
		const struct power_supply_config *cfg)
853
{
854
	return __power_supply_register(parent, desc, cfg, false);
855 856 857
}
EXPORT_SYMBOL_GPL(power_supply_register_no_ws);

858 859 860 861 862 863 864
static void devm_power_supply_release(struct device *dev, void *res)
{
	struct power_supply **psy = res;

	power_supply_unregister(*psy);
}

865
/**
866
 * devm_power_supply_register() - Register managed power supply
867 868
 * @parent:	Device to be a parent of power supply's device, usually
 *		the device which probe function calls this
869 870 871 872 873 874 875 876 877 878 879 880 881
 * @desc:	Description of power supply, must be valid through whole
 *		lifetime of this power supply
 * @cfg:	Run-time specific configuration accessed during registering,
 *		may be NULL
 *
 * Return: A pointer to newly allocated power_supply on success
 * or ERR_PTR otherwise.
 * The returned power_supply pointer will be automatically unregistered
 * on driver detach.
 */
struct power_supply *__must_check
devm_power_supply_register(struct device *parent,
		const struct power_supply_desc *desc,
882
		const struct power_supply_config *cfg)
883
{
884 885 886
	struct power_supply **ptr, *psy;

	ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
887 888

	if (!ptr)
889 890 891
		return ERR_PTR(-ENOMEM);
	psy = __power_supply_register(parent, desc, cfg, true);
	if (IS_ERR(psy)) {
892
		devres_free(ptr);
893
	} else {
894 895 896
		*ptr = psy;
		devres_add(parent, ptr);
	}
897
	return psy;
898 899 900
}
EXPORT_SYMBOL_GPL(devm_power_supply_register);

901
/**
902
 * devm_power_supply_register_no_ws() - Register managed non-waking-source power supply
903 904
 * @parent:	Device to be a parent of power supply's device, usually
 *		the device which probe function calls this
905 906 907 908 909 910 911 912 913 914 915 916 917
 * @desc:	Description of power supply, must be valid through whole
 *		lifetime of this power supply
 * @cfg:	Run-time specific configuration accessed during registering,
 *		may be NULL
 *
 * Return: A pointer to newly allocated power_supply on success
 * or ERR_PTR otherwise.
 * The returned power_supply pointer will be automatically unregistered
 * on driver detach.
 */
struct power_supply *__must_check
devm_power_supply_register_no_ws(struct device *parent,
		const struct power_supply_desc *desc,
918
		const struct power_supply_config *cfg)
919
{
920 921 922
	struct power_supply **ptr, *psy;

	ptr = devres_alloc(devm_power_supply_release, sizeof(*ptr), GFP_KERNEL);
923 924

	if (!ptr)
925 926 927
		return ERR_PTR(-ENOMEM);
	psy = __power_supply_register(parent, desc, cfg, false);
	if (IS_ERR(psy)) {
928
		devres_free(ptr);
929
	} else {
930 931 932
		*ptr = psy;
		devres_add(parent, ptr);
	}
933
	return psy;
934 935 936
}
EXPORT_SYMBOL_GPL(devm_power_supply_register_no_ws);

937 938 939 940 941 942 943
/**
 * power_supply_unregister() - Remove this power supply from system
 * @psy:	Pointer to power supply to unregister
 *
 * Remove this power supply from the system. The resources of power supply
 * will be freed here or on last power_supply_put() call.
 */
944 945
void power_supply_unregister(struct power_supply *psy)
{
946
	WARN_ON(atomic_dec_return(&psy->use_cnt));
947
	cancel_work_sync(&psy->changed_work);
948
	cancel_delayed_work_sync(&psy->deferred_register_work);
949
	sysfs_remove_link(&psy->dev.kobj, "powers");
950
	power_supply_remove_triggers(psy);
951
	psy_unregister_cooler(psy);
952
	psy_unregister_thermal(psy);
953 954
	device_init_wakeup(&psy->dev, false);
	device_unregister(&psy->dev);
955
}
956
EXPORT_SYMBOL_GPL(power_supply_unregister);
957

958 959 960 961 962 963
void *power_supply_get_drvdata(struct power_supply *psy)
{
	return psy->drv_data;
}
EXPORT_SYMBOL_GPL(power_supply_get_drvdata);

964 965 966 967 968 969 970 971
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;
972
	power_supply_init_attrs(&power_supply_dev_type);
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989

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