blob: 22e9d6f7baaaac089dcb0b8b7b82d59ff4b5b217 [file] [log] [blame]
Satyajit Desaiac8f7542017-05-04 17:20:59 -07001/* Copyright (c) 2013-2017, The Linux Foundation. All rights reserved.
Satyajit Desaic1a7d402016-11-01 12:11:18 -07002 *
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/mutex.h>
22#include <linux/clk.h>
23#include <linux/coresight.h>
24#include <linux/of.h>
25#include <linux/regulator/consumer.h>
26
27#include "coresight-priv.h"
28
29struct hwevent_mux {
30 phys_addr_t start;
31 phys_addr_t end;
32};
33
34struct hwevent_drvdata {
35 struct device *dev;
36 struct coresight_device *csdev;
37 struct clk *clk;
38 struct mutex mutex;
39 int nr_hclk;
40 struct clk **hclk;
41 int nr_hreg;
42 struct regulator **hreg;
43 int nr_hmux;
44 struct hwevent_mux *hmux;
45};
46
47static int hwevent_enable(struct hwevent_drvdata *drvdata)
48{
49 int ret, i, j;
50
51 ret = clk_prepare_enable(drvdata->clk);
52 if (ret)
53 return ret;
54
55 for (i = 0; i < drvdata->nr_hreg; i++) {
56 ret = regulator_enable(drvdata->hreg[i]);
57 if (ret)
58 goto err0;
59 }
60
61 for (j = 0; j < drvdata->nr_hclk; j++) {
62 ret = clk_prepare_enable(drvdata->hclk[j]);
63 if (ret)
64 goto err1;
65 }
66 return 0;
67err1:
68 for (j--; j >= 0; j--)
69 clk_disable_unprepare(drvdata->hclk[j]);
70err0:
71 for (i--; i >= 0; i--)
72 regulator_disable(drvdata->hreg[i]);
73
74 clk_disable_unprepare(drvdata->clk);
75 return ret;
76}
77
78static void hwevent_disable(struct hwevent_drvdata *drvdata)
79{
80 int i;
81
82 clk_disable_unprepare(drvdata->clk);
83 for (i = 0; i < drvdata->nr_hclk; i++)
84 clk_disable_unprepare(drvdata->hclk[i]);
85 for (i = 0; i < drvdata->nr_hreg; i++)
86 regulator_disable(drvdata->hreg[i]);
87}
88
89static ssize_t hwevent_store_setreg(struct device *dev,
90 struct device_attribute *attr,
91 const char *buf, size_t size)
92{
93 struct hwevent_drvdata *drvdata = dev_get_drvdata(dev->parent);
94 void *hwereg;
95 unsigned long long addr;
96 unsigned long val;
97 int ret, i;
98
99 if (sscanf(buf, "%llx %lx", &addr, &val) != 2)
100 return -EINVAL;
101
102 mutex_lock(&drvdata->mutex);
103 ret = hwevent_enable(drvdata);
104 if (ret) {
105 mutex_unlock(&drvdata->mutex);
106 return ret;
107 }
108
109 for (i = 0; i < drvdata->nr_hmux; i++) {
110 if ((addr >= drvdata->hmux[i].start) &&
111 (addr < drvdata->hmux[i].end)) {
112 hwereg = devm_ioremap(dev,
113 drvdata->hmux[i].start,
114 drvdata->hmux[i].end -
115 drvdata->hmux[i].start);
116 if (!hwereg) {
117 dev_err(dev, "unable to map address 0x%llx\n",
118 addr);
119 ret = -ENOMEM;
120 goto err;
121 }
122 writel_relaxed(val, hwereg + addr -
123 drvdata->hmux[i].start);
124 /*
125 * Ensure writes to hwevent control registers
126 * are completed before unmapping the address
127 */
128 mb();
129 devm_iounmap(dev, hwereg);
130 break;
131 }
132 }
133
134 if (i == drvdata->nr_hmux) {
135 ret = coresight_csr_hwctrl_set(addr, val);
136 if (ret) {
137 dev_err(dev, "invalid mux control register address\n");
138 ret = -EINVAL;
139 goto err;
140 }
141 }
142
143 hwevent_disable(drvdata);
144 mutex_unlock(&drvdata->mutex);
145 return size;
146err:
147 hwevent_disable(drvdata);
148 mutex_unlock(&drvdata->mutex);
149 return ret;
150}
151static DEVICE_ATTR(setreg, 0200, NULL, hwevent_store_setreg);
152
153static struct attribute *hwevent_attrs[] = {
154 &dev_attr_setreg.attr,
155 NULL,
156};
157
158static struct attribute_group hwevent_attr_grp = {
159 .attrs = hwevent_attrs,
160};
161
162static const struct attribute_group *hwevent_attr_grps[] = {
163 &hwevent_attr_grp,
164 NULL,
165};
166
167static int hwevent_probe(struct platform_device *pdev)
168{
169 struct device *dev = &pdev->dev;
170 struct hwevent_drvdata *drvdata;
171 struct coresight_desc *desc;
172 struct coresight_platform_data *pdata;
173 struct resource *res;
174 int ret, i;
175 const char *hmux_name, *hclk_name, *hreg_name;
176
177 pdata = of_get_coresight_platform_data(dev, pdev->dev.of_node);
178 if (IS_ERR(pdata))
179 return PTR_ERR(pdata);
180 pdev->dev.platform_data = pdata;
181
182 drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
183 if (!drvdata)
184 return -ENOMEM;
185 drvdata->dev = &pdev->dev;
186 platform_set_drvdata(pdev, drvdata);
187
188 drvdata->nr_hmux = of_property_count_strings(pdev->dev.of_node,
189 "reg-names");
190
191 if (!drvdata->nr_hmux)
192 return -ENODEV;
193
194 if (drvdata->nr_hmux > 0) {
195 drvdata->hmux = devm_kzalloc(dev, drvdata->nr_hmux *
196 sizeof(*drvdata->hmux),
197 GFP_KERNEL);
198 if (!drvdata->hmux)
199 return -ENOMEM;
200 for (i = 0; i < drvdata->nr_hmux; i++) {
201 ret = of_property_read_string_index(pdev->dev.of_node,
202 "reg-names", i,
203 &hmux_name);
204 if (ret)
205 return ret;
206 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
207 hmux_name);
208 if (!res)
209 return -ENODEV;
210 drvdata->hmux[i].start = res->start;
211 drvdata->hmux[i].end = res->end;
212 }
213 } else {
214 return drvdata->nr_hmux;
215 }
216
217 mutex_init(&drvdata->mutex);
218
Satyajit Desaiac8f7542017-05-04 17:20:59 -0700219 drvdata->clk = devm_clk_get(dev, "apb_pclk");
Satyajit Desaic1a7d402016-11-01 12:11:18 -0700220 if (IS_ERR(drvdata->clk))
221 return PTR_ERR(drvdata->clk);
222
Satyajit Desaic1a7d402016-11-01 12:11:18 -0700223 drvdata->nr_hclk = of_property_count_strings(pdev->dev.of_node,
224 "qcom,hwevent-clks");
225 drvdata->nr_hreg = of_property_count_strings(pdev->dev.of_node,
226 "qcom,hwevent-regs");
227
228 if (drvdata->nr_hclk > 0) {
229 drvdata->hclk = devm_kzalloc(dev, drvdata->nr_hclk *
230 sizeof(*drvdata->hclk),
231 GFP_KERNEL);
232 if (!drvdata->hclk)
233 return -ENOMEM;
234
235 for (i = 0; i < drvdata->nr_hclk; i++) {
236 ret = of_property_read_string_index(pdev->dev.of_node,
237 "qcom,hwevent-clks",
238 i, &hclk_name);
239 if (ret)
240 return ret;
241
242 drvdata->hclk[i] = devm_clk_get(dev, hclk_name);
243 if (IS_ERR(drvdata->hclk[i]))
244 return PTR_ERR(drvdata->hclk[i]);
245 }
246 }
247 if (drvdata->nr_hreg > 0) {
248 drvdata->hreg = devm_kzalloc(dev, drvdata->nr_hreg *
249 sizeof(*drvdata->hreg),
250 GFP_KERNEL);
251 if (!drvdata->hreg)
252 return -ENOMEM;
253
254 for (i = 0; i < drvdata->nr_hreg; i++) {
255 ret = of_property_read_string_index(pdev->dev.of_node,
256 "qcom,hwevent-regs",
257 i, &hreg_name);
258 if (ret)
259 return ret;
260
261 drvdata->hreg[i] = devm_regulator_get(dev, hreg_name);
262 if (IS_ERR(drvdata->hreg[i]))
263 return PTR_ERR(drvdata->hreg[i]);
264 }
265 }
266 desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
267 if (!desc)
268 return -ENOMEM;
269
270 desc->type = CORESIGHT_DEV_TYPE_NONE;
271 desc->pdata = pdev->dev.platform_data;
272 desc->dev = &pdev->dev;
273 desc->groups = hwevent_attr_grps;
274 drvdata->csdev = coresight_register(desc);
275 if (IS_ERR(drvdata->csdev))
276 return PTR_ERR(drvdata->csdev);
277
278 dev_info(dev, "Hardware Event driver initialized\n");
279 return 0;
280}
281
282static int hwevent_remove(struct platform_device *pdev)
283{
284 struct hwevent_drvdata *drvdata = platform_get_drvdata(pdev);
285
286 coresight_unregister(drvdata->csdev);
287 return 0;
288}
289
290static const struct of_device_id hwevent_match[] = {
291 {.compatible = "qcom,coresight-hwevent"},
292 {}
293};
294
295static struct platform_driver hwevent_driver = {
296 .probe = hwevent_probe,
297 .remove = hwevent_remove,
298 .driver = {
299 .name = "coresight-hwevent",
300 .owner = THIS_MODULE,
301 .of_match_table = hwevent_match,
302 },
303};
304
305static int __init hwevent_init(void)
306{
307 return platform_driver_register(&hwevent_driver);
308}
309module_init(hwevent_init);
310
311static void __exit hwevent_exit(void)
312{
313 platform_driver_unregister(&hwevent_driver);
314}
315module_exit(hwevent_exit);
316
317MODULE_LICENSE("GPL v2");
318MODULE_DESCRIPTION("CoreSight Hardware Event driver");