blob: 455ed1d15e4d2d6b83168383acaf1dc87c20a63d [file] [log] [blame]
Pratik Patel74929432011-12-26 12:03:41 -08001/* Copyright (c) 2012, Code Aurora Forum. 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/platform_device.h>
19#include <linux/io.h>
20#include <linux/err.h>
Pratik Patelbf3e77442012-03-18 18:30:43 -070021#include <linux/export.h>
Pratik Patelb84ef9d2012-05-24 14:01:43 -070022#include <linux/slab.h>
Pratik Patel0a7edd32012-06-08 09:31:55 -070023#include <linux/semaphore.h>
Pratik Patel27086d92012-05-17 23:21:43 -070024#include <linux/clk.h>
Pratik Patel1746b8f2012-06-02 21:11:41 -070025#include <linux/coresight.h>
Pratik Patel74929432011-12-26 12:03:41 -080026
Pratik Patel1746b8f2012-06-02 21:11:41 -070027#include "coresight-priv.h"
Pratik Patel74929432011-12-26 12:03:41 -080028
Pratik Patel0a7edd32012-06-08 09:31:55 -070029
30#define NO_SINK (-1)
Pratik Patel1403f2a2012-03-21 10:10:00 -070031
Pratik Patel74929432011-12-26 12:03:41 -080032
Pratik Patel0a7edd32012-06-08 09:31:55 -070033static int curr_sink = NO_SINK;
Pratik Patel6fb38342012-06-03 14:51:38 -070034static LIST_HEAD(coresight_orph_conns);
Pratik Patel6fb38342012-06-03 14:51:38 -070035static LIST_HEAD(coresight_devs);
Pratik Patel0a7edd32012-06-08 09:31:55 -070036static DEFINE_SEMAPHORE(coresight_mutex);
Pratik Patelb84ef9d2012-05-24 14:01:43 -070037
38
Pratik Patel0a7edd32012-06-08 09:31:55 -070039static int coresight_find_link_inport(struct coresight_device *csdev)
Pratik Patelb84ef9d2012-05-24 14:01:43 -070040{
41 int i;
Pratik Patel0a7edd32012-06-08 09:31:55 -070042 struct coresight_device *parent;
Pratik Patel6fb38342012-06-03 14:51:38 -070043 struct coresight_connection *conn;
Pratik Patelb84ef9d2012-05-24 14:01:43 -070044
Pratik Patel0a7edd32012-06-08 09:31:55 -070045 parent = container_of(csdev->path_link.next, struct coresight_device,
46 path_link);
47 for (i = 0; i < parent->nr_conns; i++) {
48 conn = &parent->conns[i];
49 if (conn->child_dev == csdev)
50 return conn->child_port;
Pratik Patelb84ef9d2012-05-24 14:01:43 -070051 }
Pratik Patel0a7edd32012-06-08 09:31:55 -070052
53 pr_err("coresight: couldn't find inport, parent: %d, child: %d\n",
54 parent->id, csdev->id);
Pratik Patelb84ef9d2012-05-24 14:01:43 -070055 return 0;
Pratik Patel0a7edd32012-06-08 09:31:55 -070056}
57
58static int coresight_find_link_outport(struct coresight_device *csdev)
59{
60 int i;
61 struct coresight_device *child;
62 struct coresight_connection *conn;
63
64 child = container_of(csdev->path_link.prev, struct coresight_device,
65 path_link);
66 for (i = 0; i < csdev->nr_conns; i++) {
67 conn = &csdev->conns[i];
68 if (conn->child_dev == child)
69 return conn->outport;
Pratik Patelb84ef9d2012-05-24 14:01:43 -070070 }
Pratik Patel0a7edd32012-06-08 09:31:55 -070071
72 pr_err("coresight: couldn't find outport, parent: %d, child: %d\n",
73 csdev->id, child->id);
74 return 0;
75}
76
77static int coresight_enable_sink(struct coresight_device *csdev)
78{
79 int ret;
80
81 if (csdev->refcnt.sink_refcnt == 0) {
82 if (csdev->ops->sink_ops->enable) {
83 ret = csdev->ops->sink_ops->enable(csdev);
84 if (ret)
85 goto err;
86 csdev->enable = true;
87 }
88 }
89 csdev->refcnt.sink_refcnt++;
90
91 return 0;
92err:
93 return ret;
94}
95
96static void coresight_disable_sink(struct coresight_device *csdev)
97{
98 if (csdev->refcnt.sink_refcnt == 1) {
99 if (csdev->ops->sink_ops->disable) {
100 csdev->ops->sink_ops->disable(csdev);
101 csdev->enable = false;
102 }
103 }
104 csdev->refcnt.sink_refcnt--;
105}
106
107static int coresight_enable_link(struct coresight_device *csdev)
108{
109 int ret;
110 int link_subtype;
111 int refport, inport, outport;
112
113 inport = coresight_find_link_inport(csdev);
114 outport = coresight_find_link_outport(csdev);
115
116 link_subtype = csdev->subtype.link_subtype;
117 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
118 refport = inport;
119 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
120 refport = outport;
121 else
122 refport = 0;
123
124 if (csdev->refcnt.link_refcnts[refport] == 0) {
125 if (csdev->ops->link_ops->enable) {
126 ret = csdev->ops->link_ops->enable(csdev, inport,
127 outport);
128 if (ret)
129 goto err;
130 csdev->enable = true;
131 }
132 }
133 csdev->refcnt.link_refcnts[refport]++;
134
135 return 0;
136err:
137 return ret;
138}
139
140static void coresight_disable_link(struct coresight_device *csdev)
141{
142 int link_subtype;
143 int refport, inport, outport;
144
145 inport = coresight_find_link_inport(csdev);
146 outport = coresight_find_link_outport(csdev);
147
148 link_subtype = csdev->subtype.link_subtype;
149 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
150 refport = inport;
151 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
152 refport = outport;
153 else
154 refport = 0;
155
156 if (csdev->refcnt.link_refcnts[refport] == 1) {
157 if (csdev->ops->link_ops->disable) {
158 csdev->ops->link_ops->disable(csdev, inport, outport);
159 csdev->enable = false;
160 }
161 }
162 csdev->refcnt.link_refcnts[refport]--;
163}
164
165static int coresight_enable_source(struct coresight_device *csdev)
166{
167 int ret;
168
169 if (csdev->refcnt.source_refcnt == 0) {
170 if (csdev->ops->source_ops->enable) {
171 ret = csdev->ops->source_ops->enable(csdev);
172 if (ret)
173 goto err;
174 csdev->enable = true;
175 }
176 }
177 csdev->refcnt.source_refcnt++;
178
179 return 0;
180err:
181 return ret;
182}
183
184static void coresight_disable_source(struct coresight_device *csdev)
185{
186 if (csdev->refcnt.source_refcnt == 1) {
187 if (csdev->ops->source_ops->disable) {
188 csdev->ops->source_ops->disable(csdev);
189 csdev->enable = false;
190 }
191 }
192 csdev->refcnt.source_refcnt--;
193}
194
195static struct list_head *coresight_build_path(struct coresight_device *csdev,
196 struct list_head *path)
197{
198 int i;
199 struct list_head *p;
200 struct coresight_connection *conn;
201
202 if (csdev->id == curr_sink) {
203 list_add_tail(&csdev->path_link, path);
204 return path;
205 }
206
207 for (i = 0; i < csdev->nr_conns; i++) {
208 conn = &csdev->conns[i];
209 p = coresight_build_path(conn->child_dev, path);
210 if (p) {
211 list_add_tail(&csdev->path_link, p);
212 return p;
213 }
214 }
215 return NULL;
216}
217
218static void coresight_release_path(struct list_head *path)
219{
220 struct coresight_device *cd, *temp;
221
222 list_for_each_entry_safe(cd, temp, path, path_link)
223 list_del(&cd->path_link);
224}
225
226static int coresight_enable_path(struct list_head *path, bool incl_source)
227{
228 int ret = 0;
229 struct coresight_device *cd;
230
231 list_for_each_entry(cd, path, path_link) {
232 if (cd == list_first_entry(path, struct coresight_device,
233 path_link)) {
234 ret = coresight_enable_sink(cd);
235 } else if (list_is_last(&cd->path_link, path)) {
236 if (incl_source)
237 ret = coresight_enable_source(cd);
238 } else {
239 ret = coresight_enable_link(cd);
240 }
241 if (ret)
242 goto err;
243 }
244 return 0;
245err:
246 list_for_each_entry_continue_reverse(cd, path, path_link) {
247 if (cd == list_first_entry(path, struct coresight_device,
248 path_link)) {
249 coresight_disable_sink(cd);
250 } else if (list_is_last(&cd->path_link, path)) {
251 if (incl_source)
252 coresight_disable_source(cd);
253 } else {
254 coresight_disable_link(cd);
255 }
256 }
257 return ret;
258}
259
260static void coresight_disable_path(struct list_head *path, bool incl_source)
261{
262 struct coresight_device *cd;
263
264 list_for_each_entry(cd, path, path_link) {
265 if (cd == list_first_entry(path, struct coresight_device,
266 path_link)) {
267 coresight_disable_sink(cd);
268 } else if (list_is_last(&cd->path_link, path)) {
269 if (incl_source)
270 coresight_disable_source(cd);
271 } else {
272 coresight_disable_link(cd);
273 }
274 }
275}
276
277static int coresight_switch_sink(struct coresight_device *csdev)
278{
279 int ret = 0;
280 LIST_HEAD(path);
281 struct coresight_device *cd;
282
283 if (IS_ERR_OR_NULL(csdev))
284 return -EINVAL;
285
286 down(&coresight_mutex);
287 if (csdev->id == curr_sink)
288 goto out;
289
290 list_for_each_entry(cd, &coresight_devs, dev_link) {
291 if (cd->type == CORESIGHT_DEV_TYPE_SOURCE && cd->enable) {
292 coresight_build_path(cd, &path);
293 coresight_disable_path(&path, false);
294 coresight_release_path(&path);
295 }
296 }
297 curr_sink = csdev->id;
298 list_for_each_entry(cd, &coresight_devs, dev_link) {
299 if (cd->type == CORESIGHT_DEV_TYPE_SOURCE && cd->enable) {
300 coresight_build_path(cd, &path);
301 ret = coresight_enable_path(&path, false);
302 coresight_release_path(&path);
303 if (ret)
304 goto err;
305 }
306 }
307out:
308 up(&coresight_mutex);
309 return 0;
310err:
311 list_for_each_entry(cd, &coresight_devs, dev_link) {
312 if (cd->type == CORESIGHT_DEV_TYPE_SOURCE && cd->enable)
313 coresight_disable_source(cd);
314 }
315 pr_err("coresight: sink switch failed, sources disabled; try again\n");
316 return ret;
317}
318
319int coresight_enable(struct coresight_device *csdev)
320{
321 int ret = 0;
322 LIST_HEAD(path);
323
324 if (IS_ERR_OR_NULL(csdev))
325 return -EINVAL;
326
327 down(&coresight_mutex);
328 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
329 ret = -EINVAL;
330 pr_err("coresight: wrong device type in %s\n", __func__);
331 goto out;
332 }
333 if (csdev->enable)
334 goto out;
335
336 coresight_build_path(csdev, &path);
337 ret = coresight_enable_path(&path, true);
338 coresight_release_path(&path);
339 if (ret)
340 pr_err("coresight: enable failed\n");
341out:
342 up(&coresight_mutex);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700343 return ret;
344}
Pratik Patel3b0ca882012-06-01 16:54:14 -0700345EXPORT_SYMBOL_GPL(coresight_enable);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700346
Pratik Patel0a7edd32012-06-08 09:31:55 -0700347void coresight_disable(struct coresight_device *csdev)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700348{
Pratik Patel0a7edd32012-06-08 09:31:55 -0700349 LIST_HEAD(path);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700350
Pratik Patel0a7edd32012-06-08 09:31:55 -0700351 if (IS_ERR_OR_NULL(csdev))
352 return;
353
354 down(&coresight_mutex);
355 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
356 pr_err("coresight: wrong device type in %s\n", __func__);
357 goto out;
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700358 }
Pratik Patel0a7edd32012-06-08 09:31:55 -0700359 if (!csdev->enable)
360 goto out;
361
362 coresight_build_path(csdev, &path);
363 coresight_disable_path(&path, true);
364 coresight_release_path(&path);
365out:
366 up(&coresight_mutex);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700367}
Pratik Patel3b0ca882012-06-01 16:54:14 -0700368EXPORT_SYMBOL_GPL(coresight_disable);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700369
Pratik Patelcf7d0452012-07-02 13:57:20 -0700370void coresight_abort(void)
371{
372 struct coresight_device *cd;
373
374 if (down_trylock(&coresight_mutex)) {
375 pr_err("coresight: abort could not be processed\n");
376 return;
377 }
378 if (curr_sink == NO_SINK)
379 goto out;
380
381 list_for_each_entry(cd, &coresight_devs, dev_link) {
382 if (cd->id == curr_sink) {
383 if (cd->enable && cd->ops->sink_ops->abort)
384 cd->ops->sink_ops->abort(cd);
385 }
386 }
387out:
388 up(&coresight_mutex);
389}
390EXPORT_SYMBOL_GPL(coresight_abort);
391
Pratik Patel6fb38342012-06-03 14:51:38 -0700392static ssize_t coresight_show_type(struct device *dev,
393 struct device_attribute *attr, char *buf)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700394{
395 return snprintf(buf, PAGE_SIZE, "%s\n", dev->type->name);
396}
397
Pratik Patel6fb38342012-06-03 14:51:38 -0700398static struct device_attribute coresight_dev_attrs[] = {
399 __ATTR(type, S_IRUGO, coresight_show_type, NULL),
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700400 { },
401};
402
Pratik Patel6fb38342012-06-03 14:51:38 -0700403struct bus_type coresight_bus_type = {
404 .name = "coresight",
405 .dev_attrs = coresight_dev_attrs,
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700406};
407
Pratik Patel0a7edd32012-06-08 09:31:55 -0700408static ssize_t coresight_show_curr_sink(struct device *dev,
409 struct device_attribute *attr,
410 char *buf)
411{
412 struct coresight_device *csdev = to_coresight_device(dev);
413
414 return scnprintf(buf, PAGE_SIZE, "%u\n",
415 csdev->id == curr_sink ? 1 : 0);
416}
417
418static ssize_t coresight_store_curr_sink(struct device *dev,
419 struct device_attribute *attr,
420 const char *buf, size_t size)
421{
422 int ret = 0;
423 unsigned long val;
424 struct coresight_device *csdev = to_coresight_device(dev);
425
426 if (sscanf(buf, "%lx", &val) != 1)
427 return -EINVAL;
428
429 if (val)
430 ret = coresight_switch_sink(csdev);
431 else
432 ret = -EINVAL;
433
434 if (ret)
435 return ret;
436 return size;
437}
438static DEVICE_ATTR(curr_sink, S_IRUGO | S_IWUSR, coresight_show_curr_sink,
439 coresight_store_curr_sink);
440
Pratik Patel6fb38342012-06-03 14:51:38 -0700441static ssize_t coresight_show_enable(struct device *dev,
442 struct device_attribute *attr, char *buf)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700443{
Pratik Patel6fb38342012-06-03 14:51:38 -0700444 struct coresight_device *csdev = to_coresight_device(dev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700445
446 return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable);
447}
448
Pratik Patel6fb38342012-06-03 14:51:38 -0700449static ssize_t coresight_store_enable(struct device *dev,
450 struct device_attribute *attr,
451 const char *buf, size_t size)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700452{
453 int ret = 0;
454 unsigned long val;
Pratik Patel6fb38342012-06-03 14:51:38 -0700455 struct coresight_device *csdev = to_coresight_device(dev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700456
457 if (sscanf(buf, "%lx", &val) != 1)
458 return -EINVAL;
459
460 if (val)
Pratik Patel0a7edd32012-06-08 09:31:55 -0700461 ret = coresight_enable(csdev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700462 else
Pratik Patel0a7edd32012-06-08 09:31:55 -0700463 coresight_disable(csdev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700464
465 if (ret)
466 return ret;
467 return size;
468}
Pratik Patel6fb38342012-06-03 14:51:38 -0700469static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, coresight_show_enable,
470 coresight_store_enable);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700471
Pratik Patel0a7edd32012-06-08 09:31:55 -0700472static struct attribute *coresight_attrs_sink[] = {
473 &dev_attr_curr_sink.attr,
474 NULL,
475};
476
477static struct attribute_group coresight_attr_grp_sink = {
478 .attrs = coresight_attrs_sink,
479};
480
481static const struct attribute_group *coresight_attr_grps_sink[] = {
482 &coresight_attr_grp_sink,
483 NULL,
484};
485
486static struct attribute *coresight_attrs_source[] = {
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700487 &dev_attr_enable.attr,
488 NULL,
489};
490
Pratik Patel0a7edd32012-06-08 09:31:55 -0700491static struct attribute_group coresight_attr_grp_source = {
492 .attrs = coresight_attrs_source,
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700493};
494
Pratik Patel0a7edd32012-06-08 09:31:55 -0700495static const struct attribute_group *coresight_attr_grps_source[] = {
496 &coresight_attr_grp_source,
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700497 NULL,
498};
499
Pratik Patel0a7edd32012-06-08 09:31:55 -0700500static struct device_type coresight_dev_type[] = {
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700501 {
Pratik Patel0a7edd32012-06-08 09:31:55 -0700502 .name = "sink",
503 .groups = coresight_attr_grps_sink,
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700504 },
505 {
506 .name = "link",
507 },
508 {
Pratik Patel0a7edd32012-06-08 09:31:55 -0700509 .name = "linksink",
510 .groups = coresight_attr_grps_sink,
511 },
512 {
513 .name = "source",
514 .groups = coresight_attr_grps_source,
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700515 },
516};
517
Pratik Patel6fb38342012-06-03 14:51:38 -0700518static void coresight_device_release(struct device *dev)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700519{
Pratik Patel6fb38342012-06-03 14:51:38 -0700520 struct coresight_device *csdev = to_coresight_device(dev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700521 kfree(csdev);
522}
523
Pratik Patel6fb38342012-06-03 14:51:38 -0700524static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700525{
Pratik Patel6fb38342012-06-03 14:51:38 -0700526 struct coresight_connection *conn, *temp;
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700527
Pratik Patel6fb38342012-06-03 14:51:38 -0700528 list_for_each_entry_safe(conn, temp, &coresight_orph_conns, link) {
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700529 if (conn->child_id == csdev->id) {
530 conn->child_dev = csdev;
531 list_del(&conn->link);
532 }
533 }
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700534}
535
Pratik Patel6fb38342012-06-03 14:51:38 -0700536static void coresight_fixup_device_conns(struct coresight_device *csdev)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700537{
538 int i;
Pratik Patel6fb38342012-06-03 14:51:38 -0700539 struct coresight_device *cd;
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700540 bool found;
541
542 for (i = 0; i < csdev->nr_conns; i++) {
543 found = false;
Pratik Patel0a7edd32012-06-08 09:31:55 -0700544 list_for_each_entry(cd, &coresight_devs, dev_link) {
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700545 if (csdev->conns[i].child_id == cd->id) {
546 csdev->conns[i].child_dev = cd;
547 found = true;
548 break;
549 }
550 }
Pratik Patel0a7edd32012-06-08 09:31:55 -0700551 if (!found)
Pratik Patel6fb38342012-06-03 14:51:38 -0700552 list_add_tail(&csdev->conns[i].link,
553 &coresight_orph_conns);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700554 }
555}
556
Pratik Patel6fb38342012-06-03 14:51:38 -0700557struct coresight_device *coresight_register(struct coresight_desc *desc)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700558{
559 int i;
560 int ret;
Pratik Patel0a7edd32012-06-08 09:31:55 -0700561 int link_subtype;
562 int nr_refcnts;
563 int *refcnts = NULL;
Pratik Patel6fb38342012-06-03 14:51:38 -0700564 struct coresight_device *csdev;
565 struct coresight_connection *conns;
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700566
567 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
568 if (!csdev) {
569 ret = -ENOMEM;
570 goto err_kzalloc_csdev;
571 }
572
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700573 csdev->id = desc->pdata->id;
574
Pratik Patel0a7edd32012-06-08 09:31:55 -0700575 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
576 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
577 link_subtype = desc->subtype.link_subtype;
578 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
579 nr_refcnts = desc->pdata->nr_inports;
580 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
581 nr_refcnts = desc->pdata->nr_outports;
582 else
583 nr_refcnts = 1;
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700584
Pratik Patel0a7edd32012-06-08 09:31:55 -0700585 refcnts = kzalloc(sizeof(*refcnts) * nr_refcnts, GFP_KERNEL);
586 if (!refcnts) {
587 ret = -ENOMEM;
588 goto err_kzalloc_refcnts;
589 }
590 csdev->refcnt.link_refcnts = refcnts;
591 }
592
593 csdev->nr_conns = desc->pdata->nr_outports;
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700594 conns = kzalloc(sizeof(*conns) * csdev->nr_conns, GFP_KERNEL);
595 if (!conns) {
596 ret = -ENOMEM;
597 goto err_kzalloc_conns;
598 }
599 for (i = 0; i < csdev->nr_conns; i++) {
Pratik Patel0a7edd32012-06-08 09:31:55 -0700600 conns[i].outport = desc->pdata->outports[i];
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700601 conns[i].child_id = desc->pdata->child_ids[i];
602 conns[i].child_port = desc->pdata->child_ports[i];
603 }
604 csdev->conns = conns;
605
Pratik Patel0a7edd32012-06-08 09:31:55 -0700606 csdev->type = desc->type;
607 csdev->subtype = desc->subtype;
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700608 csdev->ops = desc->ops;
609 csdev->owner = desc->owner;
610
Pratik Patel6fb38342012-06-03 14:51:38 -0700611 csdev->dev.type = &coresight_dev_type[desc->type];
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700612 csdev->dev.groups = desc->groups;
613 csdev->dev.parent = desc->dev;
Pratik Patel6fb38342012-06-03 14:51:38 -0700614 csdev->dev.bus = &coresight_bus_type;
615 csdev->dev.release = coresight_device_release;
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700616 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
617
Pratik Patel0a7edd32012-06-08 09:31:55 -0700618 down(&coresight_mutex);
619 if (desc->pdata->default_sink) {
620 if (curr_sink == NO_SINK) {
621 curr_sink = csdev->id;
622 } else {
623 ret = -EINVAL;
624 goto err_default_sink;
625 }
626 }
627
Pratik Patel6fb38342012-06-03 14:51:38 -0700628 coresight_fixup_device_conns(csdev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700629 ret = device_register(&csdev->dev);
630 if (ret)
631 goto err_dev_reg;
Pratik Patel6fb38342012-06-03 14:51:38 -0700632 coresight_fixup_orphan_conns(csdev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700633
Pratik Patel0a7edd32012-06-08 09:31:55 -0700634 list_add_tail(&csdev->dev_link, &coresight_devs);
635 up(&coresight_mutex);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700636
637 return csdev;
638err_dev_reg:
639 put_device(&csdev->dev);
Pratik Patel0a7edd32012-06-08 09:31:55 -0700640err_default_sink:
641 up(&coresight_mutex);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700642 kfree(conns);
643err_kzalloc_conns:
Pratik Patel0a7edd32012-06-08 09:31:55 -0700644 kfree(refcnts);
645err_kzalloc_refcnts:
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700646 kfree(csdev);
647err_kzalloc_csdev:
648 return ERR_PTR(ret);
649}
Pratik Patel3b0ca882012-06-01 16:54:14 -0700650EXPORT_SYMBOL_GPL(coresight_register);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700651
Pratik Patel6fb38342012-06-03 14:51:38 -0700652void coresight_unregister(struct coresight_device *csdev)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700653{
654 if (IS_ERR_OR_NULL(csdev))
655 return;
656
657 if (get_device(&csdev->dev)) {
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700658 device_unregister(&csdev->dev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700659 put_device(&csdev->dev);
660 }
661}
Pratik Patel3b0ca882012-06-01 16:54:14 -0700662EXPORT_SYMBOL_GPL(coresight_unregister);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700663
Pratik Patel6fb38342012-06-03 14:51:38 -0700664static int __init coresight_init(void)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700665{
Pratik Patel6fb38342012-06-03 14:51:38 -0700666 return bus_register(&coresight_bus_type);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700667}
Pratik Patel6fb38342012-06-03 14:51:38 -0700668subsys_initcall(coresight_init);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700669
Pratik Patel6fb38342012-06-03 14:51:38 -0700670static void __exit coresight_exit(void)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700671{
Pratik Patel6fb38342012-06-03 14:51:38 -0700672 bus_unregister(&coresight_bus_type);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700673}
Pratik Patel6fb38342012-06-03 14:51:38 -0700674module_exit(coresight_exit);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700675
676MODULE_LICENSE("GPL v2");