omap_device.c 24.3 KB
Newer Older
1 2 3
/*
 * omap_device implementation
 *
4
 * Copyright (C) 2009-2010 Nokia Corporation
5
 * Paul Walmsley, Kevin Hilman
6 7
 *
 * Developed in collaboration with (alphabetical order): Benoit
8
 * Cousson, Thara Gopinath, Tony Lindgren, Rajendra Nayak, Vikram
9 10 11 12 13 14 15 16 17 18 19
 * Pandita, Sakari Poussa, Anand Sawant, Santosh Shilimkar, Richard
 * Woodruff
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This code provides a consistent interface for OMAP device drivers
 * to control power management and interconnect properties of their
 * devices.
 *
20 21 22
 * In the medium- to long-term, this code should be implemented as a
 * proper omap_bus/omap_device in Linux, no more platform_data func
 * pointers
23 24 25 26 27 28 29
 *
 *
 */
#undef DEBUG

#include <linux/kernel.h>
#include <linux/platform_device.h>
30
#include <linux/slab.h>
31 32
#include <linux/err.h>
#include <linux/io.h>
33
#include <linux/clk.h>
34
#include <linux/clkdev.h>
35
#include <linux/pm_runtime.h>
36 37
#include <linux/of.h>
#include <linux/notifier.h>
38

39
#include "common.h"
T
Tony Lindgren 已提交
40
#include "soc.h"
41
#include "omap_device.h"
42
#include "omap_hwmod.h"
43 44 45

/* Private functions */

46 47 48 49
static void _add_clkdev(struct omap_device *od, const char *clk_alias,
		       const char *clk_name)
{
	struct clk *r;
50
	int rc;
51 52 53 54

	if (!clk_alias || !clk_name)
		return;

55
	dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
56

57
	r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
58
	if (!IS_ERR(r)) {
59
		dev_dbg(&od->pdev->dev,
60
			 "alias %s already exists\n", clk_alias);
61 62 63 64
		clk_put(r);
		return;
	}

65 66 67 68 69 70 71 72
	rc = clk_add_alias(clk_alias, dev_name(&od->pdev->dev), clk_name, NULL);
	if (rc) {
		if (rc == -ENODEV || rc == -ENOMEM)
			dev_err(&od->pdev->dev,
				"clkdev_alloc for %s failed\n", clk_alias);
		else
			dev_err(&od->pdev->dev,
				"clk_get for %s failed\n", clk_name);
73 74 75
	}
}

76
/**
77 78
 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
 * and main clock
79
 * @od: struct omap_device *od
80
 * @oh: struct omap_hwmod *oh
81
 *
82 83 84
 * For the main clock and every optional clock present per hwmod per
 * omap_device, this function adds an entry in the clkdev table of the
 * form <dev-id=dev_name, con-id=role> if it does not exist already.
85 86 87 88 89 90
 *
 * The function is called from inside omap_device_build_ss(), after
 * omap_device_register.
 *
 * This allows drivers to get a pointer to its optional clocks based on its role
 * by calling clk_get(<dev*>, <role>).
91
 * In the case of the main clock, a "fck" alias is used.
92 93 94
 *
 * No return value.
 */
95 96
static void _add_hwmod_clocks_clkdev(struct omap_device *od,
				     struct omap_hwmod *oh)
97 98 99
{
	int i;

100
	_add_clkdev(od, "fck", oh->main_clk);
101

102 103
	for (i = 0; i < oh->opt_clks_cnt; i++)
		_add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
104 105
}

106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/**
 * omap_device_build_from_dt - build an omap_device with multiple hwmods
 * @pdev_name: name of the platform_device driver to use
 * @pdev_id: this platform_device's connection ID
 * @oh: ptr to the single omap_hwmod that backs this omap_device
 * @pdata: platform_data ptr to associate with the platform_device
 * @pdata_len: amount of memory pointed to by @pdata
 *
 * Function for building an omap_device already registered from device-tree
 *
 * Returns 0 or PTR_ERR() on error.
 */
static int omap_device_build_from_dt(struct platform_device *pdev)
{
	struct omap_hwmod **hwmods;
	struct omap_device *od;
	struct omap_hwmod *oh;
	struct device_node *node = pdev->dev.of_node;
	const char *oh_name;
	int oh_cnt, i, ret = 0;
127
	bool device_active = false;
128 129

	oh_cnt = of_property_count_strings(node, "ti,hwmods");
130
	if (oh_cnt <= 0) {
131
		dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
		return -ENODEV;
	}

	hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
	if (!hwmods) {
		ret = -ENOMEM;
		goto odbfd_exit;
	}

	for (i = 0; i < oh_cnt; i++) {
		of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
		oh = omap_hwmod_lookup(oh_name);
		if (!oh) {
			dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
				oh_name);
			ret = -EINVAL;
			goto odbfd_exit1;
		}
		hwmods[i] = oh;
151 152
		if (oh->flags & HWMOD_INIT_NO_IDLE)
			device_active = true;
153 154
	}

155
	od = omap_device_alloc(pdev, hwmods, oh_cnt);
156
	if (IS_ERR(od)) {
157 158 159 160 161 162
		dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
			oh_name);
		ret = PTR_ERR(od);
		goto odbfd_exit1;
	}

163 164 165 166 167 168 169 170
	/* Fix up missing resource names */
	for (i = 0; i < pdev->num_resources; i++) {
		struct resource *r = &pdev->resource[i];

		if (r->name == NULL)
			r->name = dev_name(&pdev->dev);
	}

171 172
	pdev->dev.pm_domain = &omap_device_pm_domain;

173 174 175 176 177
	if (device_active) {
		omap_device_enable(pdev);
		pm_runtime_set_active(&pdev->dev);
	}

178 179 180
odbfd_exit1:
	kfree(hwmods);
odbfd_exit:
181 182 183 184
	/* if data/we are at fault.. load up a fail handler */
	if (ret)
		pdev->dev.pm_domain = &omap_device_fail_pm_domain;

185 186 187 188 189 190 191
	return ret;
}

static int _omap_device_notifier_call(struct notifier_block *nb,
				      unsigned long event, void *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
192
	struct omap_device *od;
193 194 195 196 197 198

	switch (event) {
	case BUS_NOTIFY_DEL_DEVICE:
		if (pdev->archdata.od)
			omap_device_delete(pdev->archdata.od);
		break;
199 200 201
	case BUS_NOTIFY_ADD_DEVICE:
		if (pdev->dev.of_node)
			omap_device_build_from_dt(pdev);
202
		omap_auxdata_legacy_init(dev);
203 204 205 206 207
		/* fall through */
	default:
		od = to_omap_device(pdev);
		if (od)
			od->_driver_status = event;
208 209 210 211 212
	}

	return NOTIFY_DONE;
}

213 214 215 216 217 218 219 220
/**
 * _omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
 * @od: struct omap_device *od
 *
 * Enable all underlying hwmods.  Returns 0.
 */
static int _omap_device_enable_hwmods(struct omap_device *od)
{
221
	int ret = 0;
222 223 224
	int i;

	for (i = 0; i < od->hwmods_cnt; i++)
225
		ret |= omap_hwmod_enable(od->hwmods[i]);
226

227
	return ret;
228 229 230 231 232 233 234 235 236 237
}

/**
 * _omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
 * @od: struct omap_device *od
 *
 * Idle all underlying hwmods.  Returns 0.
 */
static int _omap_device_idle_hwmods(struct omap_device *od)
{
238
	int ret = 0;
239 240 241
	int i;

	for (i = 0; i < od->hwmods_cnt; i++)
242
		ret |= omap_hwmod_idle(od->hwmods[i]);
243

244
	return ret;
245
}
246

247 248
/* Public functions for use by core code */

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
/**
 * omap_device_get_context_loss_count - get lost context count
 * @od: struct omap_device *
 *
 * Using the primary hwmod, query the context loss count for this
 * device.
 *
 * Callers should consider context for this device lost any time this
 * function returns a value different than the value the caller got
 * the last time it called this function.
 *
 * If any hwmods exist for the omap_device assoiated with @pdev,
 * return the context loss counter for that hwmod, otherwise return
 * zero.
 */
264
int omap_device_get_context_loss_count(struct platform_device *pdev)
265 266 267 268
{
	struct omap_device *od;
	u32 ret = 0;

269
	od = to_omap_device(pdev);
270 271 272 273 274 275 276

	if (od->hwmods_cnt)
		ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);

	return ret;
}

277 278 279
/**
 * omap_device_count_resources - count number of struct resource entries needed
 * @od: struct omap_device *
280
 * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
281 282 283 284 285 286
 *
 * Count the number of struct resource entries needed for this
 * omap_device @od.  Used by omap_device_build_ss() to determine how
 * much memory to allocate before calling
 * omap_device_fill_resources().  Returns the count.
 */
287 288
static int omap_device_count_resources(struct omap_device *od,
				       unsigned long flags)
289 290 291 292
{
	int c = 0;
	int i;

293
	for (i = 0; i < od->hwmods_cnt; i++)
294
		c += omap_hwmod_count_resources(od->hwmods[i], flags);
295

P
Paul Walmsley 已提交
296 297
	pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n",
		 od->pdev->name, c, od->hwmods_cnt);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

	return c;
}

/**
 * omap_device_fill_resources - fill in array of struct resource
 * @od: struct omap_device *
 * @res: pointer to an array of struct resource to be filled in
 *
 * Populate one or more empty struct resource pointed to by @res with
 * the resource data for this omap_device @od.  Used by
 * omap_device_build_ss() after calling omap_device_count_resources().
 * Ideally this function would not be needed at all.  If omap_device
 * replaces platform_device, then we can specify our own
 * get_resource()/ get_irq()/etc functions that use the underlying
 * omap_hwmod information.  Or if platform_device is extended to use
 * subarchitecture-specific function pointers, the various
 * platform_device functions can simply call omap_device internal
 * functions to get device resources.  Hacking around the existing
 * platform_device code wastes memory.  Returns 0.
 */
319 320
static int omap_device_fill_resources(struct omap_device *od,
				      struct resource *res)
321 322 323
{
	int i, r;

324 325
	for (i = 0; i < od->hwmods_cnt; i++) {
		r = omap_hwmod_fill_resources(od->hwmods[i], res);
326 327 328 329 330 331
		res += r;
	}

	return 0;
}

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
/**
 * _od_fill_dma_resources - fill in array of struct resource with dma resources
 * @od: struct omap_device *
 * @res: pointer to an array of struct resource to be filled in
 *
 * Populate one or more empty struct resource pointed to by @res with
 * the dma resource data for this omap_device @od.  Used by
 * omap_device_alloc() after calling omap_device_count_resources().
 *
 * Ideally this function would not be needed at all.  If we have
 * mechanism to get dma resources from DT.
 *
 * Returns 0.
 */
static int _od_fill_dma_resources(struct omap_device *od,
				      struct resource *res)
{
	int i, r;

	for (i = 0; i < od->hwmods_cnt; i++) {
		r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
		res += r;
	}

	return 0;
}

359 360 361 362 363 364 365 366
/**
 * omap_device_alloc - allocate an omap_device
 * @pdev: platform_device that will be included in this omap_device
 * @oh: ptr to the single omap_hwmod that backs this omap_device
 * @pdata: platform_data ptr to associate with the platform_device
 * @pdata_len: amount of memory pointed to by @pdata
 *
 * Convenience function for allocating an omap_device structure and filling
367
 * hwmods, and resources.
368 369 370
 *
 * Returns an struct omap_device pointer or ERR_PTR() on error;
 */
371
struct omap_device *omap_device_alloc(struct platform_device *pdev,
372
					struct omap_hwmod **ohs, int oh_cnt)
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
{
	int ret = -ENOMEM;
	struct omap_device *od;
	struct resource *res = NULL;
	int i, res_count;
	struct omap_hwmod **hwmods;

	od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
	if (!od) {
		ret = -ENOMEM;
		goto oda_exit1;
	}
	od->hwmods_cnt = oh_cnt;

	hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
	if (!hwmods)
		goto oda_exit2;

	od->hwmods = hwmods;
	od->pdev = pdev;

	/*
395 396 397 398
	 * Non-DT Boot:
	 *   Here, pdev->num_resources = 0, and we should get all the
	 *   resources from hwmod.
	 *
399 400 401 402 403 404
	 * DT Boot:
	 *   OF framework will construct the resource structure (currently
	 *   does for MEM & IRQ resource) and we should respect/use these
	 *   resources, killing hwmod dependency.
	 *   If pdev->num_resources > 0, we assume that MEM & IRQ resources
	 *   have been allocated by OF layer already (through DTB).
405 406 407 408
	 *   As preparation for the future we examine the OF provided resources
	 *   to see if we have DMA resources provided already. In this case
	 *   there is no need to update the resources for the device, we use the
	 *   OF provided ones.
409 410 411
	 *
	 * TODO: Once DMA resource is available from OF layer, we should
	 *   kill filling any resources from hwmod.
412
	 */
413 414 415 416 417 418 419 420 421 422 423 424 425
	if (!pdev->num_resources) {
		/* Count all resources for the device */
		res_count = omap_device_count_resources(od, IORESOURCE_IRQ |
							    IORESOURCE_DMA |
							    IORESOURCE_MEM);
	} else {
		/* Take a look if we already have DMA resource via DT */
		for (i = 0; i < pdev->num_resources; i++) {
			struct resource *r = &pdev->resource[i];

			/* We have it, no need to touch the resources */
			if (r->flags == IORESOURCE_DMA)
				goto have_everything;
426
		}
427 428 429 430 431
		/* Count only DMA resources for the device */
		res_count = omap_device_count_resources(od, IORESOURCE_DMA);
		/* The device has no DMA resource, no need for update */
		if (!res_count)
			goto have_everything;
432

433 434
		res_count += pdev->num_resources;
	}
435

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	/* Allocate resources memory to account for new resources */
	res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
	if (!res)
		goto oda_exit3;

	if (!pdev->num_resources) {
		dev_dbg(&pdev->dev, "%s: using %d resources from hwmod\n",
			__func__, res_count);
		omap_device_fill_resources(od, res);
	} else {
		dev_dbg(&pdev->dev,
			"%s: appending %d DMA resources from hwmod\n",
			__func__, res_count - pdev->num_resources);
		memcpy(res, pdev->resource,
		       sizeof(struct resource) * pdev->num_resources);
		_od_fill_dma_resources(od, &res[pdev->num_resources]);
452 453
	}

454 455 456 457 458 459 460
	ret = platform_device_add_resources(pdev, res, res_count);
	kfree(res);

	if (ret)
		goto oda_exit3;

have_everything:
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
	pdev->archdata.od = od;

	for (i = 0; i < oh_cnt; i++) {
		hwmods[i]->od = od;
		_add_hwmod_clocks_clkdev(od, hwmods[i]);
	}

	return od;

oda_exit3:
	kfree(hwmods);
oda_exit2:
	kfree(od);
oda_exit1:
	dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);

	return ERR_PTR(ret);
}

480
void omap_device_delete(struct omap_device *od)
481
{
482 483 484
	if (!od)
		return;

485 486 487 488 489
	od->pdev->archdata.od = NULL;
	kfree(od->hwmods);
	kfree(od);
}

490 491 492 493 494 495 496 497 498 499 500 501 502 503
/**
 * omap_device_build - build and register an omap_device with one omap_hwmod
 * @pdev_name: name of the platform_device driver to use
 * @pdev_id: this platform_device's connection ID
 * @oh: ptr to the single omap_hwmod that backs this omap_device
 * @pdata: platform_data ptr to associate with the platform_device
 * @pdata_len: amount of memory pointed to by @pdata
 *
 * Convenience function for building and registering a single
 * omap_device record, which in turn builds and registers a
 * platform_device record.  See omap_device_build_ss() for more
 * information.  Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
 * passes along the return value of omap_device_build_ss().
 */
504 505 506 507
struct platform_device __init *omap_device_build(const char *pdev_name,
						 int pdev_id,
						 struct omap_hwmod *oh,
						 void *pdata, int pdata_len)
508 509 510 511 512 513 514
{
	struct omap_hwmod *ohs[] = { oh };

	if (!oh)
		return ERR_PTR(-EINVAL);

	return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
515
				    pdata_len);
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
}

/**
 * omap_device_build_ss - build and register an omap_device with multiple hwmods
 * @pdev_name: name of the platform_device driver to use
 * @pdev_id: this platform_device's connection ID
 * @oh: ptr to the single omap_hwmod that backs this omap_device
 * @pdata: platform_data ptr to associate with the platform_device
 * @pdata_len: amount of memory pointed to by @pdata
 *
 * Convenience function for building and registering an omap_device
 * subsystem record.  Subsystem records consist of multiple
 * omap_hwmods.  This function in turn builds and registers a
 * platform_device record.  Returns an ERR_PTR() on error, or passes
 * along the return value of omap_device_register().
 */
532 533 534 535 536
struct platform_device __init *omap_device_build_ss(const char *pdev_name,
						    int pdev_id,
						    struct omap_hwmod **ohs,
						    int oh_cnt, void *pdata,
						    int pdata_len)
537 538
{
	int ret = -ENOMEM;
539
	struct platform_device *pdev;
540 541 542 543 544 545 546 547
	struct omap_device *od;

	if (!ohs || oh_cnt == 0 || !pdev_name)
		return ERR_PTR(-EINVAL);

	if (!pdata && pdata_len > 0)
		return ERR_PTR(-EINVAL);

548 549 550 551 552 553
	pdev = platform_device_alloc(pdev_name, pdev_id);
	if (!pdev) {
		ret = -ENOMEM;
		goto odbs_exit;
	}

554 555 556 557 558
	/* Set the dev_name early to allow dev_xxx in omap_device_alloc */
	if (pdev->id != -1)
		dev_set_name(&pdev->dev, "%s.%d", pdev->name,  pdev->id);
	else
		dev_set_name(&pdev->dev, "%s", pdev->name);
559

560
	od = omap_device_alloc(pdev, ohs, oh_cnt);
561
	if (IS_ERR(od))
562 563 564
		goto odbs_exit1;

	ret = platform_device_add_data(pdev, pdata, pdata_len);
565
	if (ret)
566
		goto odbs_exit2;
567

568
	ret = omap_device_register(pdev);
569
	if (ret)
570
		goto odbs_exit2;
571

572
	return pdev;
573

574
odbs_exit2:
575
	omap_device_delete(od);
576 577 578
odbs_exit1:
	platform_device_put(pdev);
odbs_exit:
579 580 581 582 583 584

	pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);

	return ERR_PTR(ret);
}

585
#ifdef CONFIG_PM
586 587 588
static int _od_runtime_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
589
	int ret;
590

591
	ret = pm_generic_runtime_suspend(dev);
592 593
	if (ret)
		return ret;
594

595
	return omap_device_idle(pdev);
596 597
}

598 599 600
static int _od_runtime_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
601
	int ret;
602

603 604 605
	ret = omap_device_enable(pdev);
	if (ret)
		return ret;
606 607

	return pm_generic_runtime_resume(dev);
608
}
609 610 611 612 613 614 615 616 617 618 619 620 621

static int _od_fail_runtime_suspend(struct device *dev)
{
	dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
	return -ENODEV;
}

static int _od_fail_runtime_resume(struct device *dev)
{
	dev_warn(dev, "%s: FIXME: missing hwmod/omap_dev info\n", __func__);
	return -ENODEV;
}

622
#endif
623

624 625 626 627 628 629 630
#ifdef CONFIG_SUSPEND
static int _od_suspend_noirq(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct omap_device *od = to_omap_device(pdev);
	int ret;

631 632 633 634
	/* Don't attempt late suspend on a driver that is not bound */
	if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
		return 0;

635 636 637 638
	ret = pm_generic_suspend_noirq(dev);

	if (!ret && !pm_runtime_status_suspended(dev)) {
		if (pm_generic_runtime_suspend(dev) == 0) {
639
			pm_runtime_set_suspended(dev);
640
			omap_device_idle(pdev);
641 642 643 644 645 646 647 648 649 650 651 652
			od->flags |= OMAP_DEVICE_SUSPENDED;
		}
	}

	return ret;
}

static int _od_resume_noirq(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct omap_device *od = to_omap_device(pdev);

653
	if (od->flags & OMAP_DEVICE_SUSPENDED) {
654
		od->flags &= ~OMAP_DEVICE_SUSPENDED;
655
		omap_device_enable(pdev);
656 657 658 659 660 661 662 663 664
		/*
		 * XXX: we run before core runtime pm has resumed itself. At
		 * this point in time, we just restore the runtime pm state and
		 * considering symmetric operations in resume, we donot expect
		 * to fail. If we failed, something changed in core runtime_pm
		 * framework OR some device driver messed things up, hence, WARN
		 */
		WARN(pm_runtime_set_active(dev),
		     "Could not set %s runtime state active\n", dev_name(dev));
665 666 667 668 669
		pm_generic_runtime_resume(dev);
	}

	return pm_generic_resume_noirq(dev);
}
670 671 672
#else
#define _od_suspend_noirq NULL
#define _od_resume_noirq NULL
673 674
#endif

675 676 677 678 679 680 681
struct dev_pm_domain omap_device_fail_pm_domain = {
	.ops = {
		SET_RUNTIME_PM_OPS(_od_fail_runtime_suspend,
				   _od_fail_runtime_resume, NULL)
	}
};

682
struct dev_pm_domain omap_device_pm_domain = {
683
	.ops = {
684
		SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
685
				   NULL)
686
		USE_PLATFORM_PM_SLEEP_OPS
687 688
		SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(_od_suspend_noirq,
					      _od_resume_noirq)
689 690 691
	}
};

692 693 694 695 696 697 698 699
/**
 * omap_device_register - register an omap_device with one omap_hwmod
 * @od: struct omap_device * to register
 *
 * Register the omap_device structure.  This currently just calls
 * platform_device_register() on the underlying platform_device.
 * Returns the return value of platform_device_register().
 */
700
int omap_device_register(struct platform_device *pdev)
701
{
702
	pr_debug("omap_device: %s: registering\n", pdev->name);
703

704
	pdev->dev.pm_domain = &omap_device_pm_domain;
705
	return platform_device_add(pdev);
706 707 708 709 710 711 712 713 714 715 716 717 718
}


/* Public functions for use by device drivers through struct platform_data */

/**
 * omap_device_enable - fully activate an omap_device
 * @od: struct omap_device * to activate
 *
 * Do whatever is necessary for the hwmods underlying omap_device @od
 * to be accessible and ready to operate.  This generally involves
 * enabling clocks, setting SYSCONFIG registers; and in the future may
 * involve remuxing pins.  Device drivers should call this function
719 720 721
 * indirectly via pm_runtime_get*().  Returns -EINVAL if called when
 * the omap_device is already enabled, or passes along the return
 * value of _omap_device_enable_hwmods().
722 723 724 725 726 727
 */
int omap_device_enable(struct platform_device *pdev)
{
	int ret;
	struct omap_device *od;

728
	od = to_omap_device(pdev);
729 730

	if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
731 732 733
		dev_warn(&pdev->dev,
			 "omap_device: %s() called from invalid state %d\n",
			 __func__, od->_state);
734 735 736
		return -EINVAL;
	}

737
	ret = _omap_device_enable_hwmods(od);
738

739 740
	if (ret == 0)
		od->_state = OMAP_DEVICE_STATE_ENABLED;
741 742 743 744 745 746 747 748

	return ret;
}

/**
 * omap_device_idle - idle an omap_device
 * @od: struct omap_device * to idle
 *
749 750
 * Idle omap_device @od.  Device drivers call this function indirectly
 * via pm_runtime_put*().  Returns -EINVAL if the omap_device is not
751
 * currently enabled, or passes along the return value of
752
 * _omap_device_idle_hwmods().
753 754 755 756 757 758
 */
int omap_device_idle(struct platform_device *pdev)
{
	int ret;
	struct omap_device *od;

759
	od = to_omap_device(pdev);
760 761

	if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
762 763 764
		dev_warn(&pdev->dev,
			 "omap_device: %s() called from invalid state %d\n",
			 __func__, od->_state);
765 766 767
		return -EINVAL;
	}

768
	ret = _omap_device_idle_hwmods(od);
769

770 771
	if (ret == 0)
		od->_state = OMAP_DEVICE_STATE_IDLE;
772 773 774 775

	return ret;
}

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
/**
 * omap_device_assert_hardreset - set a device's hardreset line
 * @pdev: struct platform_device * to reset
 * @name: const char * name of the reset line
 *
 * Set the hardreset line identified by @name on the IP blocks
 * associated with the hwmods backing the platform_device @pdev.  All
 * of the hwmods associated with @pdev must have the same hardreset
 * line linked to them for this to work.  Passes along the return value
 * of omap_hwmod_assert_hardreset() in the event of any failure, or
 * returns 0 upon success.
 */
int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
{
	struct omap_device *od = to_omap_device(pdev);
	int ret = 0;
	int i;

	for (i = 0; i < od->hwmods_cnt; i++) {
		ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
		if (ret)
			break;
	}

	return ret;
}

/**
 * omap_device_deassert_hardreset - release a device's hardreset line
 * @pdev: struct platform_device * to reset
 * @name: const char * name of the reset line
 *
 * Release the hardreset line identified by @name on the IP blocks
 * associated with the hwmods backing the platform_device @pdev.  All
 * of the hwmods associated with @pdev must have the same hardreset
 * line linked to them for this to work.  Passes along the return
 * value of omap_hwmod_deassert_hardreset() in the event of any
 * failure, or returns 0 upon success.
 */
int omap_device_deassert_hardreset(struct platform_device *pdev,
				   const char *name)
{
	struct omap_device *od = to_omap_device(pdev);
	int ret = 0;
	int i;

	for (i = 0; i < od->hwmods_cnt; i++) {
		ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
		if (ret)
			break;
	}

	return ret;
}

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
/**
 * omap_device_get_by_hwmod_name() - convert a hwmod name to
 * device pointer.
 * @oh_name: name of the hwmod device
 *
 * Returns back a struct device * pointer associated with a hwmod
 * device represented by a hwmod_name
 */
struct device *omap_device_get_by_hwmod_name(const char *oh_name)
{
	struct omap_hwmod *oh;

	if (!oh_name) {
		WARN(1, "%s: no hwmod name!\n", __func__);
		return ERR_PTR(-EINVAL);
	}

	oh = omap_hwmod_lookup(oh_name);
849
	if (!oh) {
850 851
		WARN(1, "%s: no hwmod for %s\n", __func__,
			oh_name);
852
		return ERR_PTR(-ENODEV);
853
	}
854
	if (!oh->od) {
855 856
		WARN(1, "%s: no omap_device for %s\n", __func__,
			oh_name);
857
		return ERR_PTR(-ENODEV);
858 859 860 861
	}

	return &oh->od->pdev->dev;
}
862

863 864 865 866
static struct notifier_block platform_nb = {
	.notifier_call = _omap_device_notifier_call,
};

867 868
static int __init omap_device_init(void)
{
869
	bus_register_notifier(&platform_bus_type, &platform_nb);
870
	return 0;
871
}
T
Tony Lindgren 已提交
872
omap_core_initcall(omap_device_init);
873 874 875 876 877 878 879 880 881 882 883 884 885

/**
 * omap_device_late_idle - idle devices without drivers
 * @dev: struct device * associated with omap_device
 * @data: unused
 *
 * Check the driver bound status of this device, and idle it
 * if there is no driver attached.
 */
static int __init omap_device_late_idle(struct device *dev, void *data)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct omap_device *od = to_omap_device(pdev);
886
	int i;
887 888 889 890 891 892 893 894

	if (!od)
		return 0;

	/*
	 * If omap_device state is enabled, but has no driver bound,
	 * idle it.
	 */
895 896 897 898 899 900 901 902 903

	/*
	 * Some devices (like memory controllers) are always kept
	 * enabled, and should not be idled even with no drivers.
	 */
	for (i = 0; i < od->hwmods_cnt; i++)
		if (od->hwmods[i]->flags & HWMOD_INIT_NO_IDLE)
			return 0;

904 905
	if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER &&
	    od->_driver_status != BUS_NOTIFY_BIND_DRIVER) {
906 907 908 909 910 911 912 913 914 915 916 917 918
		if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
			dev_warn(dev, "%s: enabled but no driver.  Idling\n",
				 __func__);
			omap_device_idle(pdev);
		}
	}

	return 0;
}

static int __init omap_device_late_init(void)
{
	bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
919 920 921 922

	WARN(!of_have_populated_dt(),
		"legacy booting deprecated, please update to boot with .dts\n");

923 924
	return 0;
}
925
omap_late_initcall_sync(omap_device_late_init);