coresight: moving PM runtime operations to core framework
[cascardo/linux.git] / drivers / hwtracing / coresight / coresight-funnel.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/types.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/fs.h>
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/coresight.h>
23 #include <linux/amba/bus.h>
24 #include <linux/clk.h>
25
26 #include "coresight-priv.h"
27
28 #define FUNNEL_FUNCTL           0x000
29 #define FUNNEL_PRICTL           0x004
30
31 #define FUNNEL_HOLDTIME_MASK    0xf00
32 #define FUNNEL_HOLDTIME_SHFT    0x8
33 #define FUNNEL_HOLDTIME         (0x7 << FUNNEL_HOLDTIME_SHFT)
34
35 /**
36  * struct funnel_drvdata - specifics associated to a funnel component
37  * @base:       memory mapped base address for this component.
38  * @dev:        the device entity associated to this component.
39  * @atclk:      optional clock for the core parts of the funnel.
40  * @csdev:      component vitals needed by the framework.
41  * @priority:   port selection order.
42  */
43 struct funnel_drvdata {
44         void __iomem            *base;
45         struct device           *dev;
46         struct clk              *atclk;
47         struct coresight_device *csdev;
48         unsigned long           priority;
49 };
50
51 static void funnel_enable_hw(struct funnel_drvdata *drvdata, int port)
52 {
53         u32 functl;
54
55         CS_UNLOCK(drvdata->base);
56
57         functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
58         functl &= ~FUNNEL_HOLDTIME_MASK;
59         functl |= FUNNEL_HOLDTIME;
60         functl |= (1 << port);
61         writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
62         writel_relaxed(drvdata->priority, drvdata->base + FUNNEL_PRICTL);
63
64         CS_LOCK(drvdata->base);
65 }
66
67 static int funnel_enable(struct coresight_device *csdev, int inport,
68                          int outport)
69 {
70         struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
71
72         funnel_enable_hw(drvdata, inport);
73
74         dev_info(drvdata->dev, "FUNNEL inport %d enabled\n", inport);
75         return 0;
76 }
77
78 static void funnel_disable_hw(struct funnel_drvdata *drvdata, int inport)
79 {
80         u32 functl;
81
82         CS_UNLOCK(drvdata->base);
83
84         functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
85         functl &= ~(1 << inport);
86         writel_relaxed(functl, drvdata->base + FUNNEL_FUNCTL);
87
88         CS_LOCK(drvdata->base);
89 }
90
91 static void funnel_disable(struct coresight_device *csdev, int inport,
92                            int outport)
93 {
94         struct funnel_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
95
96         funnel_disable_hw(drvdata, inport);
97
98         dev_info(drvdata->dev, "FUNNEL inport %d disabled\n", inport);
99 }
100
101 static const struct coresight_ops_link funnel_link_ops = {
102         .enable         = funnel_enable,
103         .disable        = funnel_disable,
104 };
105
106 static const struct coresight_ops funnel_cs_ops = {
107         .link_ops       = &funnel_link_ops,
108 };
109
110 static ssize_t priority_show(struct device *dev,
111                              struct device_attribute *attr, char *buf)
112 {
113         struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
114         unsigned long val = drvdata->priority;
115
116         return sprintf(buf, "%#lx\n", val);
117 }
118
119 static ssize_t priority_store(struct device *dev,
120                               struct device_attribute *attr,
121                               const char *buf, size_t size)
122 {
123         int ret;
124         unsigned long val;
125         struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
126
127         ret = kstrtoul(buf, 16, &val);
128         if (ret)
129                 return ret;
130
131         drvdata->priority = val;
132         return size;
133 }
134 static DEVICE_ATTR_RW(priority);
135
136 static u32 get_funnel_ctrl_hw(struct funnel_drvdata *drvdata)
137 {
138         u32 functl;
139
140         CS_UNLOCK(drvdata->base);
141         functl = readl_relaxed(drvdata->base + FUNNEL_FUNCTL);
142         CS_LOCK(drvdata->base);
143
144         return functl;
145 }
146
147 static ssize_t funnel_ctrl_show(struct device *dev,
148                              struct device_attribute *attr, char *buf)
149 {
150         u32 val;
151         struct funnel_drvdata *drvdata = dev_get_drvdata(dev->parent);
152
153         pm_runtime_get_sync(drvdata->dev);
154
155         val = get_funnel_ctrl_hw(drvdata);
156
157         pm_runtime_put(drvdata->dev);
158
159         return sprintf(buf, "%#x\n", val);
160 }
161 static DEVICE_ATTR_RO(funnel_ctrl);
162
163 static struct attribute *coresight_funnel_attrs[] = {
164         &dev_attr_funnel_ctrl.attr,
165         &dev_attr_priority.attr,
166         NULL,
167 };
168 ATTRIBUTE_GROUPS(coresight_funnel);
169
170 static int funnel_probe(struct amba_device *adev, const struct amba_id *id)
171 {
172         int ret;
173         void __iomem *base;
174         struct device *dev = &adev->dev;
175         struct coresight_platform_data *pdata = NULL;
176         struct funnel_drvdata *drvdata;
177         struct resource *res = &adev->res;
178         struct coresight_desc *desc;
179         struct device_node *np = adev->dev.of_node;
180
181         if (np) {
182                 pdata = of_get_coresight_platform_data(dev, np);
183                 if (IS_ERR(pdata))
184                         return PTR_ERR(pdata);
185                 adev->dev.platform_data = pdata;
186         }
187
188         drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
189         if (!drvdata)
190                 return -ENOMEM;
191
192         drvdata->dev = &adev->dev;
193         drvdata->atclk = devm_clk_get(&adev->dev, "atclk"); /* optional */
194         if (!IS_ERR(drvdata->atclk)) {
195                 ret = clk_prepare_enable(drvdata->atclk);
196                 if (ret)
197                         return ret;
198         }
199         dev_set_drvdata(dev, drvdata);
200
201         /* Validity for the resource is already checked by the AMBA core */
202         base = devm_ioremap_resource(dev, res);
203         if (IS_ERR(base))
204                 return PTR_ERR(base);
205
206         drvdata->base = base;
207         pm_runtime_put(&adev->dev);
208
209         desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
210         if (!desc)
211                 return -ENOMEM;
212
213         desc->type = CORESIGHT_DEV_TYPE_LINK;
214         desc->subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_MERG;
215         desc->ops = &funnel_cs_ops;
216         desc->pdata = pdata;
217         desc->dev = dev;
218         desc->groups = coresight_funnel_groups;
219         drvdata->csdev = coresight_register(desc);
220         if (IS_ERR(drvdata->csdev))
221                 return PTR_ERR(drvdata->csdev);
222
223         dev_info(dev, "FUNNEL initialized\n");
224         return 0;
225 }
226
227 #ifdef CONFIG_PM
228 static int funnel_runtime_suspend(struct device *dev)
229 {
230         struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
231
232         if (drvdata && !IS_ERR(drvdata->atclk))
233                 clk_disable_unprepare(drvdata->atclk);
234
235         return 0;
236 }
237
238 static int funnel_runtime_resume(struct device *dev)
239 {
240         struct funnel_drvdata *drvdata = dev_get_drvdata(dev);
241
242         if (drvdata && !IS_ERR(drvdata->atclk))
243                 clk_prepare_enable(drvdata->atclk);
244
245         return 0;
246 }
247 #endif
248
249 static const struct dev_pm_ops funnel_dev_pm_ops = {
250         SET_RUNTIME_PM_OPS(funnel_runtime_suspend, funnel_runtime_resume, NULL)
251 };
252
253 static struct amba_id funnel_ids[] = {
254         {
255                 .id     = 0x0003b908,
256                 .mask   = 0x0003ffff,
257         },
258         { 0, 0},
259 };
260
261 static struct amba_driver funnel_driver = {
262         .drv = {
263                 .name   = "coresight-funnel",
264                 .owner  = THIS_MODULE,
265                 .pm     = &funnel_dev_pm_ops,
266                 .suppress_bind_attrs = true,
267         },
268         .probe          = funnel_probe,
269         .id_table       = funnel_ids,
270 };
271
272 module_amba_driver(funnel_driver);
273
274 MODULE_LICENSE("GPL v2");
275 MODULE_DESCRIPTION("CoreSight Funnel driver");