1 /*
2 * omap_device implementation
3 *
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 * Pandita, Sakari Poussa, Anand Sawant, Santosh Shilimkar, Richard
10 * Woodruff
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 * This code provides a consistent interface for OMAP device drivers
17 * to control power management and interconnect properties of their
18 * devices.
19 *
20 * In the medium- to long-term, this code should either be
21 * a) implemented via arch-specific pointers in platform_data
22 * or
23 * b) implemented as a proper omap_bus/omap_device in Linux, no more
24 * platform_data func pointers
25 *
26 *
27 * Guidelines for usage by driver authors:
28 *
29 * 1. These functions are intended to be used by device drivers via
30 * function pointers in struct platform_data. As an example,
31 * omap_device_enable() should be passed to the driver as
32 *
33 * struct foo_driver_platform_data {
34 * ...
35 * int (*device_enable)(struct platform_device *pdev);
36 * ...
37 * }
38 *
39 * Note that the generic "device_enable" name is used, rather than
40 * "omap_device_enable". This is so other architectures can pass in their
41 * own enable/disable functions here.
42 *
43 * This should be populated during device setup:
44 *
45 * ...
46 * pdata->device_enable = omap_device_enable;
47 * ...
48 *
49 * 2. Drivers should first check to ensure the function pointer is not null
50 * before calling it, as in:
51 *
52 * if (pdata->device_enable)
53 * pdata->device_enable(pdev);
54 *
55 * This allows other architectures that don't use similar device_enable()/
56 * device_shutdown() functions to execute normally.
57 *
58 * ...
59 *
60 * Suggested usage by device drivers:
61 *
62 * During device initialization:
63 * device_enable()
64 *
65 * During device idle:
66 * (save remaining device context if necessary)
67 * device_idle();
68 *
69 * During device resume:
70 * device_enable();
71 * (restore context if necessary)
72 *
73 * During device shutdown:
74 * device_shutdown()
75 * (device must be reinitialized at this point to use it again)
76 *
77 */
78 #undef DEBUG
80 #include <linux/kernel.h>
81 #include <linux/export.h>
82 #include <linux/platform_device.h>
83 #include <linux/slab.h>
84 #include <linux/err.h>
85 #include <linux/io.h>
86 #include <linux/clk.h>
87 #include <linux/clkdev.h>
88 #include <linux/pm_runtime.h>
89 #include <linux/of.h>
90 #include <linux/notifier.h>
92 #include "omap_device.h"
93 #include "omap_hwmod.h"
95 /* These parameters are passed to _omap_device_{de,}activate() */
96 #define USE_WAKEUP_LAT 0
97 #define IGNORE_WAKEUP_LAT 1
99 static int omap_early_device_register(struct platform_device *pdev);
101 static struct omap_device_pm_latency omap_default_latency[] = {
102 {
103 .deactivate_func = omap_device_idle_hwmods,
104 .activate_func = omap_device_enable_hwmods,
105 .flags = OMAP_DEVICE_LATENCY_AUTO_ADJUST,
106 }
107 };
109 /* Private functions */
111 /**
112 * _omap_device_activate - increase device readiness
113 * @od: struct omap_device *
114 * @ignore_lat: increase to latency target (0) or full readiness (1)?
115 *
116 * Increase readiness of omap_device @od (thus decreasing device
117 * wakeup latency, but consuming more power). If @ignore_lat is
118 * IGNORE_WAKEUP_LAT, make the omap_device fully active. Otherwise,
119 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
120 * latency is greater than the requested maximum wakeup latency, step
121 * backwards in the omap_device_pm_latency table to ensure the
122 * device's maximum wakeup latency is less than or equal to the
123 * requested maximum wakeup latency. Returns 0.
124 */
125 static int _omap_device_activate(struct omap_device *od, u8 ignore_lat)
126 {
127 struct timespec a, b, c;
129 dev_dbg(&od->pdev->dev, "omap_device: activating\n");
131 while (od->pm_lat_level > 0) {
132 struct omap_device_pm_latency *odpl;
133 unsigned long long act_lat = 0;
135 od->pm_lat_level--;
137 odpl = od->pm_lats + od->pm_lat_level;
139 if (!ignore_lat &&
140 (od->dev_wakeup_lat <= od->_dev_wakeup_lat_limit))
141 break;
143 read_persistent_clock(&a);
145 /* XXX check return code */
146 odpl->activate_func(od);
148 read_persistent_clock(&b);
150 c = timespec_sub(b, a);
151 act_lat = timespec_to_ns(&c);
153 dev_dbg(&od->pdev->dev,
154 "omap_device: pm_lat %d: activate: elapsed time %llu nsec\n",
155 od->pm_lat_level, act_lat);
157 if (act_lat > odpl->activate_lat) {
158 odpl->activate_lat_worst = act_lat;
159 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
160 odpl->activate_lat = act_lat;
161 dev_dbg(&od->pdev->dev,
162 "new worst case activate latency %d: %llu\n",
163 od->pm_lat_level, act_lat);
164 } else
165 dev_warn(&od->pdev->dev,
166 "activate latency %d higher than expected. (%llu > %d)\n",
167 od->pm_lat_level, act_lat,
168 odpl->activate_lat);
169 }
171 od->dev_wakeup_lat -= odpl->activate_lat;
172 }
174 return 0;
175 }
177 /**
178 * _omap_device_deactivate - decrease device readiness
179 * @od: struct omap_device *
180 * @ignore_lat: decrease to latency target (0) or full inactivity (1)?
181 *
182 * Decrease readiness of omap_device @od (thus increasing device
183 * wakeup latency, but conserving power). If @ignore_lat is
184 * IGNORE_WAKEUP_LAT, make the omap_device fully inactive. Otherwise,
185 * if @ignore_lat is USE_WAKEUP_LAT, and the device's maximum wakeup
186 * latency is less than the requested maximum wakeup latency, step
187 * forwards in the omap_device_pm_latency table to ensure the device's
188 * maximum wakeup latency is less than or equal to the requested
189 * maximum wakeup latency. Returns 0.
190 */
191 static int _omap_device_deactivate(struct omap_device *od, u8 ignore_lat)
192 {
193 struct timespec a, b, c;
195 dev_dbg(&od->pdev->dev, "omap_device: deactivating\n");
197 while (od->pm_lat_level < od->pm_lats_cnt) {
198 struct omap_device_pm_latency *odpl;
199 unsigned long long deact_lat = 0;
201 odpl = od->pm_lats + od->pm_lat_level;
203 if (!ignore_lat &&
204 ((od->dev_wakeup_lat + odpl->activate_lat) >
205 od->_dev_wakeup_lat_limit))
206 break;
208 read_persistent_clock(&a);
210 /* XXX check return code */
211 odpl->deactivate_func(od);
213 read_persistent_clock(&b);
215 c = timespec_sub(b, a);
216 deact_lat = timespec_to_ns(&c);
218 dev_dbg(&od->pdev->dev,
219 "omap_device: pm_lat %d: deactivate: elapsed time %llu nsec\n",
220 od->pm_lat_level, deact_lat);
222 if (deact_lat > odpl->deactivate_lat) {
223 odpl->deactivate_lat_worst = deact_lat;
224 if (odpl->flags & OMAP_DEVICE_LATENCY_AUTO_ADJUST) {
225 odpl->deactivate_lat = deact_lat;
226 dev_dbg(&od->pdev->dev,
227 "new worst case deactivate latency %d: %llu\n",
228 od->pm_lat_level, deact_lat);
229 } else
230 dev_warn(&od->pdev->dev,
231 "deactivate latency %d higher than expected. (%llu > %d)\n",
232 od->pm_lat_level, deact_lat,
233 odpl->deactivate_lat);
234 }
236 od->dev_wakeup_lat += odpl->activate_lat;
238 od->pm_lat_level++;
239 }
241 return 0;
242 }
244 static void _add_clkdev(struct omap_device *od, const char *clk_alias,
245 const char *clk_name)
246 {
247 struct clk *r;
248 struct clk_lookup *l;
250 if (!clk_alias || !clk_name)
251 return;
253 dev_dbg(&od->pdev->dev, "Creating %s -> %s\n", clk_alias, clk_name);
255 r = clk_get_sys(dev_name(&od->pdev->dev), clk_alias);
256 if (!IS_ERR(r)) {
257 dev_warn(&od->pdev->dev,
258 "alias %s already exists\n", clk_alias);
259 clk_put(r);
260 return;
261 }
263 r = clk_get(NULL, clk_name);
264 if (IS_ERR(r)) {
265 dev_err(&od->pdev->dev,
266 "clk_get for %s failed\n", clk_name);
267 return;
268 }
270 l = clkdev_alloc(r, clk_alias, dev_name(&od->pdev->dev));
271 if (!l) {
272 dev_err(&od->pdev->dev,
273 "clkdev_alloc for %s failed\n", clk_alias);
274 return;
275 }
277 clkdev_add(l);
278 }
280 /**
281 * _add_hwmod_clocks_clkdev - Add clkdev entry for hwmod optional clocks
282 * and main clock
283 * @od: struct omap_device *od
284 * @oh: struct omap_hwmod *oh
285 *
286 * For the main clock and every optional clock present per hwmod per
287 * omap_device, this function adds an entry in the clkdev table of the
288 * form <dev-id=dev_name, con-id=role> if it does not exist already.
289 *
290 * The function is called from inside omap_device_build_ss(), after
291 * omap_device_register.
292 *
293 * This allows drivers to get a pointer to its optional clocks based on its role
294 * by calling clk_get(<dev*>, <role>).
295 * In the case of the main clock, a "fck" alias is used.
296 *
297 * No return value.
298 */
299 static void _add_hwmod_clocks_clkdev(struct omap_device *od,
300 struct omap_hwmod *oh)
301 {
302 int i;
304 _add_clkdev(od, "fck", oh->main_clk);
306 for (i = 0; i < oh->opt_clks_cnt; i++)
307 _add_clkdev(od, oh->opt_clks[i].role, oh->opt_clks[i].clk);
308 }
311 /**
312 * omap_device_build_from_dt - build an omap_device with multiple hwmods
313 * @pdev_name: name of the platform_device driver to use
314 * @pdev_id: this platform_device's connection ID
315 * @oh: ptr to the single omap_hwmod that backs this omap_device
316 * @pdata: platform_data ptr to associate with the platform_device
317 * @pdata_len: amount of memory pointed to by @pdata
318 * @pm_lats: pointer to a omap_device_pm_latency array for this device
319 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
320 * @is_early_device: should the device be registered as an early device or not
321 *
322 * Function for building an omap_device already registered from device-tree
323 *
324 * Returns 0 or PTR_ERR() on error.
325 */
326 static int omap_device_build_from_dt(struct platform_device *pdev)
327 {
328 struct omap_hwmod **hwmods;
329 struct omap_device *od;
330 struct omap_hwmod *oh;
331 struct device_node *node = pdev->dev.of_node;
332 const char *oh_name;
333 int oh_cnt, i, ret = 0;
335 oh_cnt = of_property_count_strings(node, "ti,hwmods");
336 if (!oh_cnt || IS_ERR_VALUE(oh_cnt)) {
337 dev_dbg(&pdev->dev, "No 'hwmods' to build omap_device\n");
338 return -ENODEV;
339 }
341 hwmods = kzalloc(sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
342 if (!hwmods) {
343 ret = -ENOMEM;
344 goto odbfd_exit;
345 }
347 for (i = 0; i < oh_cnt; i++) {
348 of_property_read_string_index(node, "ti,hwmods", i, &oh_name);
349 oh = omap_hwmod_lookup(oh_name);
350 if (!oh) {
351 dev_err(&pdev->dev, "Cannot lookup hwmod '%s'\n",
352 oh_name);
353 ret = -EINVAL;
354 goto odbfd_exit1;
355 }
356 hwmods[i] = oh;
357 }
359 od = omap_device_alloc(pdev, hwmods, oh_cnt, NULL, 0);
360 if (!od) {
361 dev_err(&pdev->dev, "Cannot allocate omap_device for :%s\n",
362 oh_name);
363 ret = PTR_ERR(od);
364 goto odbfd_exit1;
365 }
367 /* Fix up missing resource names */
368 for (i = 0; i < pdev->num_resources; i++) {
369 struct resource *r = &pdev->resource[i];
371 if (r->name == NULL)
372 r->name = dev_name(&pdev->dev);
373 }
375 pdev->dev.pm_domain = &omap_device_pm_domain;
377 odbfd_exit1:
378 kfree(hwmods);
379 odbfd_exit:
380 return ret;
381 }
383 static int _omap_device_notifier_call(struct notifier_block *nb,
384 unsigned long event, void *dev)
385 {
386 struct platform_device *pdev = to_platform_device(dev);
387 struct omap_device *od;
389 switch (event) {
390 case BUS_NOTIFY_DEL_DEVICE:
391 if (pdev->archdata.od)
392 omap_device_delete(pdev->archdata.od);
393 break;
394 case BUS_NOTIFY_ADD_DEVICE:
395 if (pdev->dev.of_node)
396 omap_device_build_from_dt(pdev);
397 /* fall through */
398 default:
399 od = to_omap_device(pdev);
400 if (od)
401 od->_driver_status = event;
402 }
404 return NOTIFY_DONE;
405 }
408 /* Public functions for use by core code */
410 /**
411 * omap_device_get_context_loss_count - get lost context count
412 * @od: struct omap_device *
413 *
414 * Using the primary hwmod, query the context loss count for this
415 * device.
416 *
417 * Callers should consider context for this device lost any time this
418 * function returns a value different than the value the caller got
419 * the last time it called this function.
420 *
421 * If any hwmods exist for the omap_device assoiated with @pdev,
422 * return the context loss counter for that hwmod, otherwise return
423 * zero.
424 */
425 int omap_device_get_context_loss_count(struct platform_device *pdev)
426 {
427 struct omap_device *od;
428 u32 ret = 0;
430 od = to_omap_device(pdev);
432 if (od->hwmods_cnt)
433 ret = omap_hwmod_get_context_loss_count(od->hwmods[0]);
435 return ret;
436 }
438 /**
439 * omap_device_count_resources - count number of struct resource entries needed
440 * @od: struct omap_device *
441 * @flags: Type of resources to include when counting (IRQ/DMA/MEM)
442 *
443 * Count the number of struct resource entries needed for this
444 * omap_device @od. Used by omap_device_build_ss() to determine how
445 * much memory to allocate before calling
446 * omap_device_fill_resources(). Returns the count.
447 */
448 static int omap_device_count_resources(struct omap_device *od,
449 unsigned long flags)
450 {
451 int c = 0;
452 int i;
454 for (i = 0; i < od->hwmods_cnt; i++)
455 c += omap_hwmod_count_resources(od->hwmods[i], flags);
457 pr_debug("omap_device: %s: counted %d total resources across %d hwmods\n",
458 od->pdev->name, c, od->hwmods_cnt);
460 return c;
461 }
463 /**
464 * omap_device_fill_resources - fill in array of struct resource
465 * @od: struct omap_device *
466 * @res: pointer to an array of struct resource to be filled in
467 *
468 * Populate one or more empty struct resource pointed to by @res with
469 * the resource data for this omap_device @od. Used by
470 * omap_device_build_ss() after calling omap_device_count_resources().
471 * Ideally this function would not be needed at all. If omap_device
472 * replaces platform_device, then we can specify our own
473 * get_resource()/ get_irq()/etc functions that use the underlying
474 * omap_hwmod information. Or if platform_device is extended to use
475 * subarchitecture-specific function pointers, the various
476 * platform_device functions can simply call omap_device internal
477 * functions to get device resources. Hacking around the existing
478 * platform_device code wastes memory. Returns 0.
479 */
480 static int omap_device_fill_resources(struct omap_device *od,
481 struct resource *res)
482 {
483 int i, r;
485 for (i = 0; i < od->hwmods_cnt; i++) {
486 r = omap_hwmod_fill_resources(od->hwmods[i], res);
487 res += r;
488 }
490 return 0;
491 }
493 /**
494 * _od_fill_dma_resources - fill in array of struct resource with dma resources
495 * @od: struct omap_device *
496 * @res: pointer to an array of struct resource to be filled in
497 *
498 * Populate one or more empty struct resource pointed to by @res with
499 * the dma resource data for this omap_device @od. Used by
500 * omap_device_alloc() after calling omap_device_count_resources().
501 *
502 * Ideally this function would not be needed at all. If we have
503 * mechanism to get dma resources from DT.
504 *
505 * Returns 0.
506 */
507 static int _od_fill_dma_resources(struct omap_device *od,
508 struct resource *res)
509 {
510 int i, r;
512 for (i = 0; i < od->hwmods_cnt; i++) {
513 r = omap_hwmod_fill_dma_resources(od->hwmods[i], res);
514 res += r;
515 }
517 return 0;
518 }
520 /**
521 * omap_device_alloc - allocate an omap_device
522 * @pdev: platform_device that will be included in this omap_device
523 * @oh: ptr to the single omap_hwmod that backs this omap_device
524 * @pdata: platform_data ptr to associate with the platform_device
525 * @pdata_len: amount of memory pointed to by @pdata
526 * @pm_lats: pointer to a omap_device_pm_latency array for this device
527 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
528 *
529 * Convenience function for allocating an omap_device structure and filling
530 * hwmods, resources and pm_latency attributes.
531 *
532 * Returns an struct omap_device pointer or ERR_PTR() on error;
533 */
534 struct omap_device *omap_device_alloc(struct platform_device *pdev,
535 struct omap_hwmod **ohs, int oh_cnt,
536 struct omap_device_pm_latency *pm_lats,
537 int pm_lats_cnt)
538 {
539 int ret = -ENOMEM;
540 struct omap_device *od;
541 struct resource *res = NULL;
542 int i, res_count;
543 struct omap_hwmod **hwmods;
545 od = kzalloc(sizeof(struct omap_device), GFP_KERNEL);
546 if (!od) {
547 ret = -ENOMEM;
548 goto oda_exit1;
549 }
550 od->hwmods_cnt = oh_cnt;
552 hwmods = kmemdup(ohs, sizeof(struct omap_hwmod *) * oh_cnt, GFP_KERNEL);
553 if (!hwmods)
554 goto oda_exit2;
556 od->hwmods = hwmods;
557 od->pdev = pdev;
559 /*
560 * Non-DT Boot:
561 * Here, pdev->num_resources = 0, and we should get all the
562 * resources from hwmod.
563 *
564 * DT Boot:
565 * OF framework will construct the resource structure (currently
566 * does for MEM & IRQ resource) and we should respect/use these
567 * resources, killing hwmod dependency.
568 * If pdev->num_resources > 0, we assume that MEM & IRQ resources
569 * have been allocated by OF layer already (through DTB).
570 * As preparation for the future we examine the OF provided resources
571 * to see if we have DMA resources provided already. In this case
572 * there is no need to update the resources for the device, we use the
573 * OF provided ones.
574 *
575 * TODO: Once DMA resource is available from OF layer, we should
576 * kill filling any resources from hwmod.
577 */
578 if (!pdev->num_resources) {
579 /* Count all resources for the device */
580 res_count = omap_device_count_resources(od, IORESOURCE_IRQ |
581 IORESOURCE_DMA |
582 IORESOURCE_MEM);
583 } else {
584 /* Take a look if we already have DMA resource via DT */
585 for (i = 0; i < pdev->num_resources; i++) {
586 struct resource *r = &pdev->resource[i];
588 /* We have it, no need to touch the resources */
589 if (r->flags == IORESOURCE_DMA)
590 goto have_everything;
591 }
592 /* Count only DMA resources for the device */
593 res_count = omap_device_count_resources(od, IORESOURCE_DMA);
594 /* The device has no DMA resource, no need for update */
595 if (!res_count)
596 goto have_everything;
598 res_count += pdev->num_resources;
599 }
601 /* Allocate resources memory to account for new resources */
602 res = kzalloc(sizeof(struct resource) * res_count, GFP_KERNEL);
603 if (!res)
604 goto oda_exit3;
606 if (!pdev->num_resources) {
607 dev_dbg(&pdev->dev, "%s: using %d resources from hwmod\n",
608 __func__, res_count);
609 omap_device_fill_resources(od, res);
610 } else {
611 dev_dbg(&pdev->dev,
612 "%s: appending %d DMA resources from hwmod\n",
613 __func__, res_count - pdev->num_resources);
614 memcpy(res, pdev->resource,
615 sizeof(struct resource) * pdev->num_resources);
616 _od_fill_dma_resources(od, &res[pdev->num_resources]);
617 }
619 ret = platform_device_add_resources(pdev, res, res_count);
620 kfree(res);
622 if (ret)
623 goto oda_exit3;
625 have_everything:
626 if (!pm_lats) {
627 pm_lats = omap_default_latency;
628 pm_lats_cnt = ARRAY_SIZE(omap_default_latency);
629 }
631 od->pm_lats_cnt = pm_lats_cnt;
632 od->pm_lats = kmemdup(pm_lats,
633 sizeof(struct omap_device_pm_latency) * pm_lats_cnt,
634 GFP_KERNEL);
635 if (!od->pm_lats)
636 goto oda_exit3;
638 pdev->archdata.od = od;
640 for (i = 0; i < oh_cnt; i++) {
641 hwmods[i]->od = od;
642 _add_hwmod_clocks_clkdev(od, hwmods[i]);
643 }
645 return od;
647 oda_exit3:
648 kfree(hwmods);
649 oda_exit2:
650 kfree(od);
651 oda_exit1:
652 dev_err(&pdev->dev, "omap_device: build failed (%d)\n", ret);
654 return ERR_PTR(ret);
655 }
657 void omap_device_delete(struct omap_device *od)
658 {
659 if (!od)
660 return;
662 od->pdev->archdata.od = NULL;
663 kfree(od->pm_lats);
664 kfree(od->hwmods);
665 kfree(od);
666 }
668 /**
669 * omap_device_build - build and register an omap_device with one omap_hwmod
670 * @pdev_name: name of the platform_device driver to use
671 * @pdev_id: this platform_device's connection ID
672 * @oh: ptr to the single omap_hwmod that backs this omap_device
673 * @pdata: platform_data ptr to associate with the platform_device
674 * @pdata_len: amount of memory pointed to by @pdata
675 * @pm_lats: pointer to a omap_device_pm_latency array for this device
676 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
677 * @is_early_device: should the device be registered as an early device or not
678 *
679 * Convenience function for building and registering a single
680 * omap_device record, which in turn builds and registers a
681 * platform_device record. See omap_device_build_ss() for more
682 * information. Returns ERR_PTR(-EINVAL) if @oh is NULL; otherwise,
683 * passes along the return value of omap_device_build_ss().
684 */
685 struct platform_device __init *omap_device_build(const char *pdev_name, int pdev_id,
686 struct omap_hwmod *oh, void *pdata,
687 int pdata_len,
688 struct omap_device_pm_latency *pm_lats,
689 int pm_lats_cnt, int is_early_device)
690 {
691 struct omap_hwmod *ohs[] = { oh };
693 if (!oh)
694 return ERR_PTR(-EINVAL);
696 return omap_device_build_ss(pdev_name, pdev_id, ohs, 1, pdata,
697 pdata_len, pm_lats, pm_lats_cnt,
698 is_early_device);
699 }
701 /**
702 * omap_device_build_ss - build and register an omap_device with multiple hwmods
703 * @pdev_name: name of the platform_device driver to use
704 * @pdev_id: this platform_device's connection ID
705 * @oh: ptr to the single omap_hwmod that backs this omap_device
706 * @pdata: platform_data ptr to associate with the platform_device
707 * @pdata_len: amount of memory pointed to by @pdata
708 * @pm_lats: pointer to a omap_device_pm_latency array for this device
709 * @pm_lats_cnt: ARRAY_SIZE() of @pm_lats
710 * @is_early_device: should the device be registered as an early device or not
711 *
712 * Convenience function for building and registering an omap_device
713 * subsystem record. Subsystem records consist of multiple
714 * omap_hwmods. This function in turn builds and registers a
715 * platform_device record. Returns an ERR_PTR() on error, or passes
716 * along the return value of omap_device_register().
717 */
718 struct platform_device __init *omap_device_build_ss(const char *pdev_name, int pdev_id,
719 struct omap_hwmod **ohs, int oh_cnt,
720 void *pdata, int pdata_len,
721 struct omap_device_pm_latency *pm_lats,
722 int pm_lats_cnt, int is_early_device)
723 {
724 int ret = -ENOMEM;
725 struct platform_device *pdev;
726 struct omap_device *od;
728 if (!ohs || oh_cnt == 0 || !pdev_name)
729 return ERR_PTR(-EINVAL);
731 if (!pdata && pdata_len > 0)
732 return ERR_PTR(-EINVAL);
734 pdev = platform_device_alloc(pdev_name, pdev_id);
735 if (!pdev) {
736 ret = -ENOMEM;
737 goto odbs_exit;
738 }
740 /* Set the dev_name early to allow dev_xxx in omap_device_alloc */
741 if (pdev->id != -1)
742 dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
743 else
744 dev_set_name(&pdev->dev, "%s", pdev->name);
746 od = omap_device_alloc(pdev, ohs, oh_cnt, pm_lats, pm_lats_cnt);
747 if (IS_ERR(od))
748 goto odbs_exit1;
750 ret = platform_device_add_data(pdev, pdata, pdata_len);
751 if (ret)
752 goto odbs_exit2;
754 if (is_early_device)
755 ret = omap_early_device_register(pdev);
756 else
757 ret = omap_device_register(pdev);
758 if (ret)
759 goto odbs_exit2;
761 return pdev;
763 odbs_exit2:
764 omap_device_delete(od);
765 odbs_exit1:
766 platform_device_put(pdev);
767 odbs_exit:
769 pr_err("omap_device: %s: build failed (%d)\n", pdev_name, ret);
771 return ERR_PTR(ret);
772 }
774 /**
775 * omap_early_device_register - register an omap_device as an early platform
776 * device.
777 * @od: struct omap_device * to register
778 *
779 * Register the omap_device structure. This currently just calls
780 * platform_early_add_device() on the underlying platform_device.
781 * Returns 0 by default.
782 */
783 static int __init omap_early_device_register(struct platform_device *pdev)
784 {
785 struct platform_device *devices[1];
787 devices[0] = pdev;
788 early_platform_add_devices(devices, 1);
789 return 0;
790 }
792 #ifdef CONFIG_PM_RUNTIME
793 static int _od_runtime_suspend(struct device *dev)
794 {
795 struct platform_device *pdev = to_platform_device(dev);
796 int ret;
798 ret = pm_generic_runtime_suspend(dev);
800 if (!ret)
801 omap_device_idle(pdev);
803 return ret;
804 }
806 static int _od_runtime_idle(struct device *dev)
807 {
808 return pm_generic_runtime_idle(dev);
809 }
811 static int _od_runtime_resume(struct device *dev)
812 {
813 struct platform_device *pdev = to_platform_device(dev);
815 omap_device_enable(pdev);
817 return pm_generic_runtime_resume(dev);
818 }
819 #endif
821 #ifdef CONFIG_SUSPEND
822 static int _od_suspend_noirq(struct device *dev)
823 {
824 struct platform_device *pdev = to_platform_device(dev);
825 struct omap_device *od = to_omap_device(pdev);
826 int ret;
828 /* Don't attempt late suspend on a driver that is not bound */
829 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER)
830 return 0;
832 ret = pm_generic_suspend_noirq(dev);
834 if (!ret && !pm_runtime_status_suspended(dev)) {
835 if (pm_generic_runtime_suspend(dev) == 0) {
836 omap_device_idle(pdev);
837 od->flags |= OMAP_DEVICE_SUSPENDED;
838 }
839 }
841 return ret;
842 }
844 static int _od_resume_noirq(struct device *dev)
845 {
846 struct platform_device *pdev = to_platform_device(dev);
847 struct omap_device *od = to_omap_device(pdev);
849 if ((od->flags & OMAP_DEVICE_SUSPENDED) &&
850 !pm_runtime_status_suspended(dev)) {
851 od->flags &= ~OMAP_DEVICE_SUSPENDED;
852 omap_device_enable(pdev);
853 pm_generic_runtime_resume(dev);
854 }
856 return pm_generic_resume_noirq(dev);
857 }
858 #else
859 #define _od_suspend_noirq NULL
860 #define _od_resume_noirq NULL
861 #endif
863 struct dev_pm_domain omap_device_pm_domain = {
864 .ops = {
865 SET_RUNTIME_PM_OPS(_od_runtime_suspend, _od_runtime_resume,
866 _od_runtime_idle)
867 USE_PLATFORM_PM_SLEEP_OPS
868 .suspend_noirq = _od_suspend_noirq,
869 .resume_noirq = _od_resume_noirq,
870 }
871 };
873 /**
874 * omap_device_register - register an omap_device with one omap_hwmod
875 * @od: struct omap_device * to register
876 *
877 * Register the omap_device structure. This currently just calls
878 * platform_device_register() on the underlying platform_device.
879 * Returns the return value of platform_device_register().
880 */
881 int omap_device_register(struct platform_device *pdev)
882 {
883 pr_debug("omap_device: %s: registering\n", pdev->name);
885 pdev->dev.pm_domain = &omap_device_pm_domain;
886 return platform_device_add(pdev);
887 }
890 /* Public functions for use by device drivers through struct platform_data */
892 /**
893 * omap_device_enable - fully activate an omap_device
894 * @od: struct omap_device * to activate
895 *
896 * Do whatever is necessary for the hwmods underlying omap_device @od
897 * to be accessible and ready to operate. This generally involves
898 * enabling clocks, setting SYSCONFIG registers; and in the future may
899 * involve remuxing pins. Device drivers should call this function
900 * (through platform_data function pointers) where they would normally
901 * enable clocks, etc. Returns -EINVAL if called when the omap_device
902 * is already enabled, or passes along the return value of
903 * _omap_device_activate().
904 */
905 int omap_device_enable(struct platform_device *pdev)
906 {
907 int ret;
908 struct omap_device *od;
910 od = to_omap_device(pdev);
912 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
913 dev_warn(&pdev->dev,
914 "omap_device: %s() called from invalid state %d\n",
915 __func__, od->_state);
916 return -EINVAL;
917 }
919 /* Enable everything if we're enabling this device from scratch */
920 if (od->_state == OMAP_DEVICE_STATE_UNKNOWN)
921 od->pm_lat_level = od->pm_lats_cnt;
923 ret = _omap_device_activate(od, IGNORE_WAKEUP_LAT);
925 od->dev_wakeup_lat = 0;
926 od->_dev_wakeup_lat_limit = UINT_MAX;
927 od->_state = OMAP_DEVICE_STATE_ENABLED;
929 return ret;
930 }
932 /**
933 * omap_device_idle - idle an omap_device
934 * @od: struct omap_device * to idle
935 *
936 * Idle omap_device @od by calling as many .deactivate_func() entries
937 * in the omap_device's pm_lats table as is possible without exceeding
938 * the device's maximum wakeup latency limit, pm_lat_limit. Device
939 * drivers should call this function (through platform_data function
940 * pointers) where they would normally disable clocks after operations
941 * complete, etc.. Returns -EINVAL if the omap_device is not
942 * currently enabled, or passes along the return value of
943 * _omap_device_deactivate().
944 */
945 int omap_device_idle(struct platform_device *pdev)
946 {
947 int ret;
948 struct omap_device *od;
950 od = to_omap_device(pdev);
952 if (od->_state != OMAP_DEVICE_STATE_ENABLED) {
953 dev_warn(&pdev->dev,
954 "omap_device: %s() called from invalid state %d\n",
955 __func__, od->_state);
956 return -EINVAL;
957 }
959 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
961 od->_state = OMAP_DEVICE_STATE_IDLE;
963 return ret;
964 }
966 /**
967 * omap_device_shutdown - shut down an omap_device
968 * @od: struct omap_device * to shut down
969 *
970 * Shut down omap_device @od by calling all .deactivate_func() entries
971 * in the omap_device's pm_lats table and then shutting down all of
972 * the underlying omap_hwmods. Used when a device is being "removed"
973 * or a device driver is being unloaded. Returns -EINVAL if the
974 * omap_device is not currently enabled or idle, or passes along the
975 * return value of _omap_device_deactivate().
976 */
977 int omap_device_shutdown(struct platform_device *pdev)
978 {
979 int ret, i;
980 struct omap_device *od;
982 od = to_omap_device(pdev);
984 if (od->_state != OMAP_DEVICE_STATE_ENABLED &&
985 od->_state != OMAP_DEVICE_STATE_IDLE) {
986 dev_warn(&pdev->dev,
987 "omap_device: %s() called from invalid state %d\n",
988 __func__, od->_state);
989 return -EINVAL;
990 }
992 ret = _omap_device_deactivate(od, IGNORE_WAKEUP_LAT);
994 for (i = 0; i < od->hwmods_cnt; i++)
995 omap_hwmod_shutdown(od->hwmods[i]);
997 od->_state = OMAP_DEVICE_STATE_SHUTDOWN;
999 return ret;
1000 }
1002 /**
1003 * omap_device_assert_hardreset - set a device's hardreset line
1004 * @pdev: struct platform_device * to reset
1005 * @name: const char * name of the reset line
1006 *
1007 * Set the hardreset line identified by @name on the IP blocks
1008 * associated with the hwmods backing the platform_device @pdev. All
1009 * of the hwmods associated with @pdev must have the same hardreset
1010 * line linked to them for this to work. Passes along the return value
1011 * of omap_hwmod_assert_hardreset() in the event of any failure, or
1012 * returns 0 upon success.
1013 */
1014 int omap_device_assert_hardreset(struct platform_device *pdev, const char *name)
1015 {
1016 struct omap_device *od = to_omap_device(pdev);
1017 int ret = 0;
1018 int i;
1020 for (i = 0; i < od->hwmods_cnt; i++) {
1021 ret = omap_hwmod_assert_hardreset(od->hwmods[i], name);
1022 if (ret)
1023 break;
1024 }
1026 return ret;
1027 }
1029 /**
1030 * omap_device_deassert_hardreset - release a device's hardreset line
1031 * @pdev: struct platform_device * to reset
1032 * @name: const char * name of the reset line
1033 *
1034 * Release the hardreset line identified by @name on the IP blocks
1035 * associated with the hwmods backing the platform_device @pdev. All
1036 * of the hwmods associated with @pdev must have the same hardreset
1037 * line linked to them for this to work. Passes along the return
1038 * value of omap_hwmod_deassert_hardreset() in the event of any
1039 * failure, or returns 0 upon success.
1040 */
1041 int omap_device_deassert_hardreset(struct platform_device *pdev,
1042 const char *name)
1043 {
1044 struct omap_device *od = to_omap_device(pdev);
1045 int ret = 0;
1046 int i;
1048 for (i = 0; i < od->hwmods_cnt; i++) {
1049 ret = omap_hwmod_deassert_hardreset(od->hwmods[i], name);
1050 if (ret)
1051 break;
1052 }
1054 return ret;
1055 }
1057 /**
1058 * omap_device_align_pm_lat - activate/deactivate device to match wakeup lat lim
1059 * @od: struct omap_device *
1060 *
1061 * When a device's maximum wakeup latency limit changes, call some of
1062 * the .activate_func or .deactivate_func function pointers in the
1063 * omap_device's pm_lats array to ensure that the device's maximum
1064 * wakeup latency is less than or equal to the new latency limit.
1065 * Intended to be called by OMAP PM code whenever a device's maximum
1066 * wakeup latency limit changes (e.g., via
1067 * omap_pm_set_dev_wakeup_lat()). Returns 0 if nothing needs to be
1068 * done (e.g., if the omap_device is not currently idle, or if the
1069 * wakeup latency is already current with the new limit) or passes
1070 * along the return value of _omap_device_deactivate() or
1071 * _omap_device_activate().
1072 */
1073 int omap_device_align_pm_lat(struct platform_device *pdev,
1074 u32 new_wakeup_lat_limit)
1075 {
1076 int ret = -EINVAL;
1077 struct omap_device *od;
1079 od = to_omap_device(pdev);
1081 if (new_wakeup_lat_limit == od->dev_wakeup_lat)
1082 return 0;
1084 od->_dev_wakeup_lat_limit = new_wakeup_lat_limit;
1086 if (od->_state != OMAP_DEVICE_STATE_IDLE)
1087 return 0;
1088 else if (new_wakeup_lat_limit > od->dev_wakeup_lat)
1089 ret = _omap_device_deactivate(od, USE_WAKEUP_LAT);
1090 else if (new_wakeup_lat_limit < od->dev_wakeup_lat)
1091 ret = _omap_device_activate(od, USE_WAKEUP_LAT);
1093 return ret;
1094 }
1096 /**
1097 * omap_device_get_pwrdm - return the powerdomain * associated with @od
1098 * @od: struct omap_device *
1099 *
1100 * Return the powerdomain associated with the first underlying
1101 * omap_hwmod for this omap_device. Intended for use by core OMAP PM
1102 * code. Returns NULL on error or a struct powerdomain * upon
1103 * success.
1104 */
1105 struct powerdomain *omap_device_get_pwrdm(struct omap_device *od)
1106 {
1107 /*
1108 * XXX Assumes that all omap_hwmod powerdomains are identical.
1109 * This may not necessarily be true. There should be a sanity
1110 * check in here to WARN() if any difference appears.
1111 */
1112 if (!od->hwmods_cnt)
1113 return NULL;
1115 return omap_hwmod_get_pwrdm(od->hwmods[0]);
1116 }
1118 /**
1119 * omap_device_get_mpu_rt_va - return the MPU's virtual addr for the hwmod base
1120 * @od: struct omap_device *
1121 *
1122 * Return the MPU's virtual address for the base of the hwmod, from
1123 * the ioremap() that the hwmod code does. Only valid if there is one
1124 * hwmod associated with this device. Returns NULL if there are zero
1125 * or more than one hwmods associated with this omap_device;
1126 * otherwise, passes along the return value from
1127 * omap_hwmod_get_mpu_rt_va().
1128 */
1129 void __iomem *omap_device_get_rt_va(struct omap_device *od)
1130 {
1131 if (od->hwmods_cnt != 1)
1132 return NULL;
1134 return omap_hwmod_get_mpu_rt_va(od->hwmods[0]);
1135 }
1137 /**
1138 * omap_device_get_by_hwmod_name() - convert a hwmod name to
1139 * device pointer.
1140 * @oh_name: name of the hwmod device
1141 *
1142 * Returns back a struct device * pointer associated with a hwmod
1143 * device represented by a hwmod_name
1144 */
1145 struct device *omap_device_get_by_hwmod_name(const char *oh_name)
1146 {
1147 struct omap_hwmod *oh;
1149 if (!oh_name) {
1150 WARN(1, "%s: no hwmod name!\n", __func__);
1151 return ERR_PTR(-EINVAL);
1152 }
1154 oh = omap_hwmod_lookup(oh_name);
1155 if (IS_ERR_OR_NULL(oh)) {
1156 WARN(1, "%s: no hwmod for %s\n", __func__,
1157 oh_name);
1158 return ERR_PTR(oh ? PTR_ERR(oh) : -ENODEV);
1159 }
1160 if (IS_ERR_OR_NULL(oh->od)) {
1161 WARN(1, "%s: no omap_device for %s\n", __func__,
1162 oh_name);
1163 return ERR_PTR(oh->od ? PTR_ERR(oh->od) : -ENODEV);
1164 }
1166 if (IS_ERR_OR_NULL(oh->od->pdev))
1167 return ERR_PTR(oh->od->pdev ? PTR_ERR(oh->od->pdev) : -ENODEV);
1169 return &oh->od->pdev->dev;
1170 }
1171 EXPORT_SYMBOL(omap_device_get_by_hwmod_name);
1173 /*
1174 * Public functions intended for use in omap_device_pm_latency
1175 * .activate_func and .deactivate_func function pointers
1176 */
1178 /**
1179 * omap_device_enable_hwmods - call omap_hwmod_enable() on all hwmods
1180 * @od: struct omap_device *od
1181 *
1182 * Enable all underlying hwmods. Returns 0.
1183 */
1184 int omap_device_enable_hwmods(struct omap_device *od)
1185 {
1186 int i;
1188 for (i = 0; i < od->hwmods_cnt; i++)
1189 omap_hwmod_enable(od->hwmods[i]);
1191 /* XXX pass along return value here? */
1192 return 0;
1193 }
1195 /**
1196 * omap_device_idle_hwmods - call omap_hwmod_idle() on all hwmods
1197 * @od: struct omap_device *od
1198 *
1199 * Idle all underlying hwmods. Returns 0.
1200 */
1201 int omap_device_idle_hwmods(struct omap_device *od)
1202 {
1203 int i;
1205 for (i = 0; i < od->hwmods_cnt; i++)
1206 omap_hwmod_idle(od->hwmods[i]);
1208 /* XXX pass along return value here? */
1209 return 0;
1210 }
1212 /**
1213 * omap_device_disable_clocks - disable all main and interface clocks
1214 * @od: struct omap_device *od
1215 *
1216 * Disable the main functional clock and interface clock for all of the
1217 * omap_hwmods associated with the omap_device. Returns 0.
1218 */
1219 int omap_device_disable_clocks(struct omap_device *od)
1220 {
1221 int i;
1223 for (i = 0; i < od->hwmods_cnt; i++)
1224 omap_hwmod_disable_clocks(od->hwmods[i]);
1226 /* XXX pass along return value here? */
1227 return 0;
1228 }
1230 /**
1231 * omap_device_enable_clocks - enable all main and interface clocks
1232 * @od: struct omap_device *od
1233 *
1234 * Enable the main functional clock and interface clock for all of the
1235 * omap_hwmods associated with the omap_device. Returns 0.
1236 */
1237 int omap_device_enable_clocks(struct omap_device *od)
1238 {
1239 int i;
1241 for (i = 0; i < od->hwmods_cnt; i++)
1242 omap_hwmod_enable_clocks(od->hwmods[i]);
1244 /* XXX pass along return value here? */
1245 return 0;
1246 }
1248 static struct notifier_block platform_nb = {
1249 .notifier_call = _omap_device_notifier_call,
1250 };
1252 static int __init omap_device_init(void)
1253 {
1254 bus_register_notifier(&platform_bus_type, &platform_nb);
1255 return 0;
1256 }
1257 core_initcall(omap_device_init);
1259 /**
1260 * omap_device_late_idle - idle devices without drivers
1261 * @dev: struct device * associated with omap_device
1262 * @data: unused
1263 *
1264 * Check the driver bound status of this device, and idle it
1265 * if there is no driver attached.
1266 */
1267 static int __init omap_device_late_idle(struct device *dev, void *data)
1268 {
1269 struct platform_device *pdev = to_platform_device(dev);
1270 struct omap_device *od = to_omap_device(pdev);
1272 if (!od)
1273 return 0;
1275 /*
1276 * If omap_device state is enabled, but has no driver bound,
1277 * idle it.
1278 */
1279 if (od->_driver_status != BUS_NOTIFY_BOUND_DRIVER) {
1280 if (od->_state == OMAP_DEVICE_STATE_ENABLED) {
1281 dev_warn(dev, "%s: enabled but no driver. Idling\n",
1282 __func__);
1283 omap_device_idle(pdev);
1284 }
1285 }
1287 return 0;
1288 }
1290 static int __init omap_device_late_init(void)
1291 {
1292 bus_for_each_dev(&platform_bus_type, NULL, NULL, omap_device_late_idle);
1293 return 0;
1294 }
1295 late_initcall(omap_device_late_init);