intel_pstate: fix PCT_TO_HWP macro
[linux-2.6-block.git] / drivers / hwtracing / coresight / coresight-replicator.c
1 /* Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
2  *
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.
6  *
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.
11  */
12
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>
25
26 #include "coresight-priv.h"
27
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
33  */
34 struct replicator_drvdata {
35         struct device           *dev;
36         struct clk              *atclk;
37         struct coresight_device *csdev;
38 };
39
40 static int replicator_enable(struct coresight_device *csdev, int inport,
41                              int outport)
42 {
43         struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
44
45         pm_runtime_get_sync(drvdata->dev);
46         dev_info(drvdata->dev, "REPLICATOR enabled\n");
47         return 0;
48 }
49
50 static void replicator_disable(struct coresight_device *csdev, int inport,
51                                int outport)
52 {
53         struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
54
55         pm_runtime_put(drvdata->dev);
56         dev_info(drvdata->dev, "REPLICATOR disabled\n");
57 }
58
59 static const struct coresight_ops_link replicator_link_ops = {
60         .enable         = replicator_enable,
61         .disable        = replicator_disable,
62 };
63
64 static const struct coresight_ops replicator_cs_ops = {
65         .link_ops       = &replicator_link_ops,
66 };
67
68 static int replicator_probe(struct platform_device *pdev)
69 {
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;
76
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;
82         }
83
84         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
85         if (!drvdata)
86                 return -ENOMEM;
87
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;
94         }
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);
99
100         desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
101         if (!desc) {
102                 ret = -ENOMEM;
103                 goto out_disable_pm;
104         }
105
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;
115         }
116
117         pm_runtime_put(&pdev->dev);
118
119         dev_info(dev, "REPLICATOR initialized\n");
120         return 0;
121
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);
127
128         return ret;
129 }
130
131 static int replicator_remove(struct platform_device *pdev)
132 {
133         struct replicator_drvdata *drvdata = platform_get_drvdata(pdev);
134
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);
141
142         return 0;
143 }
144
145 #ifdef CONFIG_PM
146 static int replicator_runtime_suspend(struct device *dev)
147 {
148         struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
149
150         if (drvdata && !IS_ERR(drvdata->atclk))
151                 clk_disable_unprepare(drvdata->atclk);
152
153         return 0;
154 }
155
156 static int replicator_runtime_resume(struct device *dev)
157 {
158         struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
159
160         if (drvdata && !IS_ERR(drvdata->atclk))
161                 clk_prepare_enable(drvdata->atclk);
162
163         return 0;
164 }
165 #endif
166
167 static const struct dev_pm_ops replicator_dev_pm_ops = {
168         SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
169                            replicator_runtime_resume, NULL)
170 };
171
172 static const struct of_device_id replicator_match[] = {
173         {.compatible = "arm,coresight-replicator"},
174         {}
175 };
176
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,
184         },
185 };
186
187 static int __init replicator_init(void)
188 {
189         return platform_driver_register(&replicator_driver);
190 }
191 module_init(replicator_init);
192
193 static void __exit replicator_exit(void)
194 {
195         platform_driver_unregister(&replicator_driver);
196 }
197 module_exit(replicator_exit);
198
199 MODULE_LICENSE("GPL v2");
200 MODULE_DESCRIPTION("CoreSight Replicator driver");