blob: fe7191663bbde4bdafdb3d2097af30de450059b9 [file] [log] [blame]
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +05301/*
2 * omap-ocp2scp.c - transform ocp interface protocol to scp protocol
3 *
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * Author: Kishon Vijay Abraham I <kishon@ti.com>
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/err.h>
22#include <linux/pm_runtime.h>
23#include <linux/of.h>
24#include <linux/of_platform.h>
Kishon Vijay Abraham I01333702012-11-02 13:44:46 +053025#include <linux/platform_data/omap_ocp2scp.h>
26
27/**
28 * _count_resources - count for the number of resources
29 * @res: struct resource *
30 *
31 * Count and return the number of resources populated for the device that is
32 * connected to ocp2scp.
33 */
34static unsigned _count_resources(struct resource *res)
35{
36 int cnt = 0;
37
38 while (res->start != res->end) {
39 cnt++;
40 res++;
41 }
42
43 return cnt;
44}
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +053045
46static int ocp2scp_remove_devices(struct device *dev, void *c)
47{
48 struct platform_device *pdev = to_platform_device(dev);
49
50 platform_device_unregister(pdev);
51
52 return 0;
53}
54
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -080055static int omap_ocp2scp_probe(struct platform_device *pdev)
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +053056{
Kishon Vijay Abraham I01333702012-11-02 13:44:46 +053057 int ret;
58 unsigned res_cnt, i;
59 struct device_node *np = pdev->dev.of_node;
60 struct platform_device *pdev_child;
61 struct omap_ocp2scp_platform_data *pdata = pdev->dev.platform_data;
62 struct omap_ocp2scp_dev *dev;
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +053063
64 if (np) {
65 ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
66 if (ret) {
Kishon Vijay Abraham I01333702012-11-02 13:44:46 +053067 dev_err(&pdev->dev,
68 "failed to add resources for ocp2scp child\n");
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +053069 goto err0;
70 }
Kishon Vijay Abraham I01333702012-11-02 13:44:46 +053071 } else if (pdata) {
72 for (i = 0, dev = *pdata->devices; i < pdata->dev_cnt; i++,
73 dev++) {
74 res_cnt = _count_resources(dev->res);
75
76 pdev_child = platform_device_alloc(dev->drv_name,
77 PLATFORM_DEVID_AUTO);
78 if (!pdev_child) {
79 dev_err(&pdev->dev,
80 "failed to allocate mem for ocp2scp child\n");
81 goto err0;
82 }
83
84 ret = platform_device_add_resources(pdev_child,
85 dev->res, res_cnt);
86 if (ret) {
87 dev_err(&pdev->dev,
88 "failed to add resources for ocp2scp child\n");
89 goto err1;
90 }
91
92 pdev_child->dev.parent = &pdev->dev;
93
94 ret = platform_device_add(pdev_child);
95 if (ret) {
96 dev_err(&pdev->dev,
97 "failed to register ocp2scp child device\n");
98 goto err1;
99 }
100 }
101 } else {
102 dev_err(&pdev->dev, "OCP2SCP initialized without plat data\n");
103 return -EINVAL;
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +0530104 }
Kishon Vijay Abraham I01333702012-11-02 13:44:46 +0530105
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +0530106 pm_runtime_enable(&pdev->dev);
107
108 return 0;
109
Kishon Vijay Abraham I01333702012-11-02 13:44:46 +0530110err1:
111 platform_device_put(pdev_child);
112
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +0530113err0:
114 device_for_each_child(&pdev->dev, NULL, ocp2scp_remove_devices);
115
116 return ret;
117}
118
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800119static int omap_ocp2scp_remove(struct platform_device *pdev)
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +0530120{
121 pm_runtime_disable(&pdev->dev);
122 device_for_each_child(&pdev->dev, NULL, ocp2scp_remove_devices);
123
124 return 0;
125}
126
127#ifdef CONFIG_OF
128static const struct of_device_id omap_ocp2scp_id_table[] = {
129 { .compatible = "ti,omap-ocp2scp" },
130 {}
131};
Kishon Vijay Abraham I46ca6812012-08-23 23:10:26 +0530132MODULE_DEVICE_TABLE(of, omap_ocp2scp_id_table);
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +0530133#endif
134
135static struct platform_driver omap_ocp2scp_driver = {
136 .probe = omap_ocp2scp_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800137 .remove = omap_ocp2scp_remove,
Kishon Vijay Abraham I26a84b32012-08-22 14:10:02 +0530138 .driver = {
139 .name = "omap-ocp2scp",
140 .owner = THIS_MODULE,
141 .of_match_table = of_match_ptr(omap_ocp2scp_id_table),
142 },
143};
144
145module_platform_driver(omap_ocp2scp_driver);
146
147MODULE_ALIAS("platform: omap-ocp2scp");
148MODULE_AUTHOR("Texas Instruments Inc.");
149MODULE_DESCRIPTION("OMAP OCP2SCP driver");
150MODULE_LICENSE("GPL v2");