Linux 4.2.1
[linux/fpc-iii.git] / drivers / hwtracing / coresight / coresight-replicator.c
blob7974b7c3da6b6c790b2c94b98159880c32b59fd3
1 /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/platform_device.h>
18 #include <linux/io.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/clk.h>
23 #include <linux/of.h>
24 #include <linux/coresight.h>
26 #include "coresight-priv.h"
28 /**
29 * struct replicator_drvdata - specifics associated to a replicator component
30 * @dev: the device entity associated with this component
31 * @atclk: optional clock for the core parts of the replicator.
32 * @csdev: component vitals needed by the framework
34 struct replicator_drvdata {
35 struct device *dev;
36 struct clk *atclk;
37 struct coresight_device *csdev;
40 static int replicator_enable(struct coresight_device *csdev, int inport,
41 int outport)
43 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
45 pm_runtime_get_sync(drvdata->dev);
46 dev_info(drvdata->dev, "REPLICATOR enabled\n");
47 return 0;
50 static void replicator_disable(struct coresight_device *csdev, int inport,
51 int outport)
53 struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
55 pm_runtime_put(drvdata->dev);
56 dev_info(drvdata->dev, "REPLICATOR disabled\n");
59 static const struct coresight_ops_link replicator_link_ops = {
60 .enable = replicator_enable,
61 .disable = replicator_disable,
64 static const struct coresight_ops replicator_cs_ops = {
65 .link_ops = &replicator_link_ops,
68 static int replicator_probe(struct platform_device *pdev)
70 int ret;
71 struct device *dev = &pdev->dev;
72 struct coresight_platform_data *pdata = NULL;
73 struct replicator_drvdata *drvdata;
74 struct coresight_desc *desc;
75 struct device_node *np = pdev->dev.of_node;
77 if (np) {
78 pdata = of_get_coresight_platform_data(dev, np);
79 if (IS_ERR(pdata))
80 return PTR_ERR(pdata);
81 pdev->dev.platform_data = pdata;
84 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
85 if (!drvdata)
86 return -ENOMEM;
88 drvdata->dev = &pdev->dev;
89 drvdata->atclk = devm_clk_get(&pdev->dev, "atclk"); /* optional */
90 if (!IS_ERR(drvdata->atclk)) {
91 ret = clk_prepare_enable(drvdata->atclk);
92 if (ret)
93 return ret;
95 pm_runtime_get_noresume(&pdev->dev);
96 pm_runtime_set_active(&pdev->dev);
97 pm_runtime_enable(&pdev->dev);
98 platform_set_drvdata(pdev, drvdata);
100 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
101 if (!desc) {
102 ret = -ENOMEM;
103 goto out_disable_pm;
106 desc->type = CORESIGHT_DEV_TYPE_LINK;
107 desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
108 desc->ops = &replicator_cs_ops;
109 desc->pdata = pdev->dev.platform_data;
110 desc->dev = &pdev->dev;
111 drvdata->csdev = coresight_register(desc);
112 if (IS_ERR(drvdata->csdev)) {
113 ret = PTR_ERR(drvdata->csdev);
114 goto out_disable_pm;
117 pm_runtime_put(&pdev->dev);
119 dev_info(dev, "REPLICATOR initialized\n");
120 return 0;
122 out_disable_pm:
123 if (!IS_ERR(drvdata->atclk))
124 clk_disable_unprepare(drvdata->atclk);
125 pm_runtime_put_noidle(&pdev->dev);
126 pm_runtime_disable(&pdev->dev);
128 return ret;
131 static int replicator_remove(struct platform_device *pdev)
133 struct replicator_drvdata *drvdata = platform_get_drvdata(pdev);
135 coresight_unregister(drvdata->csdev);
136 pm_runtime_get_sync(&pdev->dev);
137 if (!IS_ERR(drvdata->atclk))
138 clk_disable_unprepare(drvdata->atclk);
139 pm_runtime_put_noidle(&pdev->dev);
140 pm_runtime_disable(&pdev->dev);
142 return 0;
145 #ifdef CONFIG_PM
146 static int replicator_runtime_suspend(struct device *dev)
148 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
150 if (drvdata && !IS_ERR(drvdata->atclk))
151 clk_disable_unprepare(drvdata->atclk);
153 return 0;
156 static int replicator_runtime_resume(struct device *dev)
158 struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
160 if (drvdata && !IS_ERR(drvdata->atclk))
161 clk_prepare_enable(drvdata->atclk);
163 return 0;
165 #endif
167 static const struct dev_pm_ops replicator_dev_pm_ops = {
168 SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
169 replicator_runtime_resume, NULL)
172 static const struct of_device_id replicator_match[] = {
173 {.compatible = "arm,coresight-replicator"},
177 static struct platform_driver replicator_driver = {
178 .probe = replicator_probe,
179 .remove = replicator_remove,
180 .driver = {
181 .name = "coresight-replicator",
182 .of_match_table = replicator_match,
183 .pm = &replicator_dev_pm_ops,
187 static int __init replicator_init(void)
189 return platform_driver_register(&replicator_driver);
191 module_init(replicator_init);
193 static void __exit replicator_exit(void)
195 platform_driver_unregister(&replicator_driver);
197 module_exit(replicator_exit);
199 MODULE_LICENSE("GPL v2");
200 MODULE_DESCRIPTION("CoreSight Replicator driver");