blob: cccb5a7233e0d1de993fff676126c378b302c099 [file] [log] [blame]
Pratik Patelb8bb4032012-07-22 23:09:11 -07001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Pratik Patel74929432011-12-26 12:03:41 -08002 *
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#define NO_SINK (-1)
Pratik Patel1403f2a2012-03-21 10:10:00 -070030
Pratik Patel0a7edd32012-06-08 09:31:55 -070031static int curr_sink = NO_SINK;
Pratik Patel6fb38342012-06-03 14:51:38 -070032static LIST_HEAD(coresight_orph_conns);
Pratik Patel6fb38342012-06-03 14:51:38 -070033static LIST_HEAD(coresight_devs);
Pratik Patel0a7edd32012-06-08 09:31:55 -070034static DEFINE_SEMAPHORE(coresight_mutex);
Pratik Patelb84ef9d2012-05-24 14:01:43 -070035
Pratik Patel0a7edd32012-06-08 09:31:55 -070036static int coresight_find_link_inport(struct coresight_device *csdev)
Pratik Patelb84ef9d2012-05-24 14:01:43 -070037{
38 int i;
Pratik Patel0a7edd32012-06-08 09:31:55 -070039 struct coresight_device *parent;
Pratik Patel6fb38342012-06-03 14:51:38 -070040 struct coresight_connection *conn;
Pratik Patelb84ef9d2012-05-24 14:01:43 -070041
Pratik Patel0a7edd32012-06-08 09:31:55 -070042 parent = container_of(csdev->path_link.next, struct coresight_device,
43 path_link);
44 for (i = 0; i < parent->nr_conns; i++) {
45 conn = &parent->conns[i];
46 if (conn->child_dev == csdev)
47 return conn->child_port;
Pratik Patelb84ef9d2012-05-24 14:01:43 -070048 }
Pratik Patel0a7edd32012-06-08 09:31:55 -070049
50 pr_err("coresight: couldn't find inport, parent: %d, child: %d\n",
51 parent->id, csdev->id);
Pratik Patelb84ef9d2012-05-24 14:01:43 -070052 return 0;
Pratik Patel0a7edd32012-06-08 09:31:55 -070053}
54
55static int coresight_find_link_outport(struct coresight_device *csdev)
56{
57 int i;
58 struct coresight_device *child;
59 struct coresight_connection *conn;
60
61 child = container_of(csdev->path_link.prev, struct coresight_device,
62 path_link);
63 for (i = 0; i < csdev->nr_conns; i++) {
64 conn = &csdev->conns[i];
65 if (conn->child_dev == child)
66 return conn->outport;
Pratik Patelb84ef9d2012-05-24 14:01:43 -070067 }
Pratik Patel0a7edd32012-06-08 09:31:55 -070068
69 pr_err("coresight: couldn't find outport, parent: %d, child: %d\n",
70 csdev->id, child->id);
71 return 0;
72}
73
74static int coresight_enable_sink(struct coresight_device *csdev)
75{
76 int ret;
77
78 if (csdev->refcnt.sink_refcnt == 0) {
79 if (csdev->ops->sink_ops->enable) {
80 ret = csdev->ops->sink_ops->enable(csdev);
81 if (ret)
82 goto err;
83 csdev->enable = true;
84 }
85 }
86 csdev->refcnt.sink_refcnt++;
87
88 return 0;
89err:
90 return ret;
91}
92
93static void coresight_disable_sink(struct coresight_device *csdev)
94{
95 if (csdev->refcnt.sink_refcnt == 1) {
96 if (csdev->ops->sink_ops->disable) {
97 csdev->ops->sink_ops->disable(csdev);
98 csdev->enable = false;
99 }
100 }
101 csdev->refcnt.sink_refcnt--;
102}
103
104static int coresight_enable_link(struct coresight_device *csdev)
105{
106 int ret;
107 int link_subtype;
108 int refport, inport, outport;
109
110 inport = coresight_find_link_inport(csdev);
111 outport = coresight_find_link_outport(csdev);
112
113 link_subtype = csdev->subtype.link_subtype;
114 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
115 refport = inport;
116 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
117 refport = outport;
118 else
119 refport = 0;
120
121 if (csdev->refcnt.link_refcnts[refport] == 0) {
122 if (csdev->ops->link_ops->enable) {
123 ret = csdev->ops->link_ops->enable(csdev, inport,
124 outport);
125 if (ret)
126 goto err;
127 csdev->enable = true;
128 }
129 }
130 csdev->refcnt.link_refcnts[refport]++;
131
132 return 0;
133err:
134 return ret;
135}
136
137static void coresight_disable_link(struct coresight_device *csdev)
138{
139 int link_subtype;
140 int refport, inport, outport;
141
142 inport = coresight_find_link_inport(csdev);
143 outport = coresight_find_link_outport(csdev);
144
145 link_subtype = csdev->subtype.link_subtype;
146 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
147 refport = inport;
148 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
149 refport = outport;
150 else
151 refport = 0;
152
153 if (csdev->refcnt.link_refcnts[refport] == 1) {
154 if (csdev->ops->link_ops->disable) {
155 csdev->ops->link_ops->disable(csdev, inport, outport);
156 csdev->enable = false;
157 }
158 }
159 csdev->refcnt.link_refcnts[refport]--;
160}
161
162static int coresight_enable_source(struct coresight_device *csdev)
163{
164 int ret;
165
166 if (csdev->refcnt.source_refcnt == 0) {
167 if (csdev->ops->source_ops->enable) {
168 ret = csdev->ops->source_ops->enable(csdev);
169 if (ret)
170 goto err;
171 csdev->enable = true;
172 }
173 }
174 csdev->refcnt.source_refcnt++;
175
176 return 0;
177err:
178 return ret;
179}
180
181static void coresight_disable_source(struct coresight_device *csdev)
182{
183 if (csdev->refcnt.source_refcnt == 1) {
184 if (csdev->ops->source_ops->disable) {
185 csdev->ops->source_ops->disable(csdev);
186 csdev->enable = false;
187 }
188 }
189 csdev->refcnt.source_refcnt--;
190}
191
192static struct list_head *coresight_build_path(struct coresight_device *csdev,
193 struct list_head *path)
194{
195 int i;
196 struct list_head *p;
197 struct coresight_connection *conn;
198
199 if (csdev->id == curr_sink) {
200 list_add_tail(&csdev->path_link, path);
201 return path;
202 }
203
204 for (i = 0; i < csdev->nr_conns; i++) {
205 conn = &csdev->conns[i];
206 p = coresight_build_path(conn->child_dev, path);
207 if (p) {
208 list_add_tail(&csdev->path_link, p);
209 return p;
210 }
211 }
212 return NULL;
213}
214
215static void coresight_release_path(struct list_head *path)
216{
217 struct coresight_device *cd, *temp;
218
219 list_for_each_entry_safe(cd, temp, path, path_link)
220 list_del(&cd->path_link);
221}
222
223static int coresight_enable_path(struct list_head *path, bool incl_source)
224{
225 int ret = 0;
226 struct coresight_device *cd;
227
228 list_for_each_entry(cd, path, path_link) {
229 if (cd == list_first_entry(path, struct coresight_device,
230 path_link)) {
231 ret = coresight_enable_sink(cd);
232 } else if (list_is_last(&cd->path_link, path)) {
233 if (incl_source)
234 ret = coresight_enable_source(cd);
235 } else {
236 ret = coresight_enable_link(cd);
237 }
238 if (ret)
239 goto err;
240 }
241 return 0;
242err:
243 list_for_each_entry_continue_reverse(cd, path, path_link) {
244 if (cd == list_first_entry(path, struct coresight_device,
245 path_link)) {
246 coresight_disable_sink(cd);
247 } else if (list_is_last(&cd->path_link, path)) {
248 if (incl_source)
249 coresight_disable_source(cd);
250 } else {
251 coresight_disable_link(cd);
252 }
253 }
254 return ret;
255}
256
257static void coresight_disable_path(struct list_head *path, bool incl_source)
258{
259 struct coresight_device *cd;
260
261 list_for_each_entry(cd, path, path_link) {
262 if (cd == list_first_entry(path, struct coresight_device,
263 path_link)) {
264 coresight_disable_sink(cd);
265 } else if (list_is_last(&cd->path_link, path)) {
266 if (incl_source)
267 coresight_disable_source(cd);
268 } else {
269 coresight_disable_link(cd);
270 }
271 }
272}
273
274static int coresight_switch_sink(struct coresight_device *csdev)
275{
276 int ret = 0;
277 LIST_HEAD(path);
278 struct coresight_device *cd;
279
280 if (IS_ERR_OR_NULL(csdev))
281 return -EINVAL;
282
283 down(&coresight_mutex);
284 if (csdev->id == curr_sink)
285 goto out;
286
287 list_for_each_entry(cd, &coresight_devs, dev_link) {
288 if (cd->type == CORESIGHT_DEV_TYPE_SOURCE && cd->enable) {
289 coresight_build_path(cd, &path);
290 coresight_disable_path(&path, false);
291 coresight_release_path(&path);
292 }
293 }
294 curr_sink = csdev->id;
295 list_for_each_entry(cd, &coresight_devs, dev_link) {
296 if (cd->type == CORESIGHT_DEV_TYPE_SOURCE && cd->enable) {
297 coresight_build_path(cd, &path);
298 ret = coresight_enable_path(&path, false);
299 coresight_release_path(&path);
300 if (ret)
301 goto err;
302 }
303 }
304out:
305 up(&coresight_mutex);
306 return 0;
307err:
308 list_for_each_entry(cd, &coresight_devs, dev_link) {
309 if (cd->type == CORESIGHT_DEV_TYPE_SOURCE && cd->enable)
310 coresight_disable_source(cd);
311 }
312 pr_err("coresight: sink switch failed, sources disabled; try again\n");
313 return ret;
314}
315
316int coresight_enable(struct coresight_device *csdev)
317{
318 int ret = 0;
319 LIST_HEAD(path);
320
321 if (IS_ERR_OR_NULL(csdev))
322 return -EINVAL;
323
324 down(&coresight_mutex);
325 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
326 ret = -EINVAL;
327 pr_err("coresight: wrong device type in %s\n", __func__);
328 goto out;
329 }
330 if (csdev->enable)
331 goto out;
332
333 coresight_build_path(csdev, &path);
334 ret = coresight_enable_path(&path, true);
335 coresight_release_path(&path);
336 if (ret)
337 pr_err("coresight: enable failed\n");
338out:
339 up(&coresight_mutex);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700340 return ret;
341}
Pratik Patel3b0ca882012-06-01 16:54:14 -0700342EXPORT_SYMBOL_GPL(coresight_enable);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700343
Pratik Patel0a7edd32012-06-08 09:31:55 -0700344void coresight_disable(struct coresight_device *csdev)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700345{
Pratik Patel0a7edd32012-06-08 09:31:55 -0700346 LIST_HEAD(path);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700347
Pratik Patel0a7edd32012-06-08 09:31:55 -0700348 if (IS_ERR_OR_NULL(csdev))
349 return;
350
351 down(&coresight_mutex);
352 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
353 pr_err("coresight: wrong device type in %s\n", __func__);
354 goto out;
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700355 }
Pratik Patel0a7edd32012-06-08 09:31:55 -0700356 if (!csdev->enable)
357 goto out;
358
359 coresight_build_path(csdev, &path);
360 coresight_disable_path(&path, true);
361 coresight_release_path(&path);
362out:
363 up(&coresight_mutex);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700364}
Pratik Patel3b0ca882012-06-01 16:54:14 -0700365EXPORT_SYMBOL_GPL(coresight_disable);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700366
Pratik Patelcf7d0452012-07-02 13:57:20 -0700367void coresight_abort(void)
368{
369 struct coresight_device *cd;
370
371 if (down_trylock(&coresight_mutex)) {
372 pr_err("coresight: abort could not be processed\n");
373 return;
374 }
375 if (curr_sink == NO_SINK)
376 goto out;
377
378 list_for_each_entry(cd, &coresight_devs, dev_link) {
379 if (cd->id == curr_sink) {
380 if (cd->enable && cd->ops->sink_ops->abort)
381 cd->ops->sink_ops->abort(cd);
382 }
383 }
384out:
385 up(&coresight_mutex);
386}
387EXPORT_SYMBOL_GPL(coresight_abort);
388
Pratik Patel6fb38342012-06-03 14:51:38 -0700389static ssize_t coresight_show_type(struct device *dev,
390 struct device_attribute *attr, char *buf)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700391{
392 return snprintf(buf, PAGE_SIZE, "%s\n", dev->type->name);
393}
394
Pratik Patel6fb38342012-06-03 14:51:38 -0700395static struct device_attribute coresight_dev_attrs[] = {
396 __ATTR(type, S_IRUGO, coresight_show_type, NULL),
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700397 { },
398};
399
Pratik Patel6fb38342012-06-03 14:51:38 -0700400struct bus_type coresight_bus_type = {
401 .name = "coresight",
402 .dev_attrs = coresight_dev_attrs,
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700403};
404
Pratik Patel0a7edd32012-06-08 09:31:55 -0700405static ssize_t coresight_show_curr_sink(struct device *dev,
406 struct device_attribute *attr,
407 char *buf)
408{
409 struct coresight_device *csdev = to_coresight_device(dev);
410
411 return scnprintf(buf, PAGE_SIZE, "%u\n",
412 csdev->id == curr_sink ? 1 : 0);
413}
414
415static ssize_t coresight_store_curr_sink(struct device *dev,
416 struct device_attribute *attr,
417 const char *buf, size_t size)
418{
419 int ret = 0;
420 unsigned long val;
421 struct coresight_device *csdev = to_coresight_device(dev);
422
423 if (sscanf(buf, "%lx", &val) != 1)
424 return -EINVAL;
425
426 if (val)
427 ret = coresight_switch_sink(csdev);
428 else
429 ret = -EINVAL;
430
431 if (ret)
432 return ret;
433 return size;
434}
435static DEVICE_ATTR(curr_sink, S_IRUGO | S_IWUSR, coresight_show_curr_sink,
436 coresight_store_curr_sink);
437
Pratik Patel6fb38342012-06-03 14:51:38 -0700438static ssize_t coresight_show_enable(struct device *dev,
439 struct device_attribute *attr, char *buf)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700440{
Pratik Patel6fb38342012-06-03 14:51:38 -0700441 struct coresight_device *csdev = to_coresight_device(dev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700442
443 return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable);
444}
445
Pratik Patel6fb38342012-06-03 14:51:38 -0700446static ssize_t coresight_store_enable(struct device *dev,
447 struct device_attribute *attr,
448 const char *buf, size_t size)
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700449{
450 int ret = 0;
451 unsigned long val;
Pratik Patel6fb38342012-06-03 14:51:38 -0700452 struct coresight_device *csdev = to_coresight_device(dev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700453
454 if (sscanf(buf, "%lx", &val) != 1)
455 return -EINVAL;
456
457 if (val)
Pratik Patel0a7edd32012-06-08 09:31:55 -0700458 ret = coresight_enable(csdev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700459 else
Pratik Patel0a7edd32012-06-08 09:31:55 -0700460 coresight_disable(csdev);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700461
462 if (ret)
463 return ret;
464 return size;
465}
Pratik Patel6fb38342012-06-03 14:51:38 -0700466static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, coresight_show_enable,
467 coresight_store_enable);
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700468
Pratik Patel0a7edd32012-06-08 09:31:55 -0700469static struct attribute *coresight_attrs_sink[] = {
470 &dev_attr_curr_sink.attr,
471 NULL,
472};
473
474static struct attribute_group coresight_attr_grp_sink = {
475 .attrs = coresight_attrs_sink,
476};
477
478static const struct attribute_group *coresight_attr_grps_sink[] = {
479 &coresight_attr_grp_sink,
480 NULL,
481};
482
483static struct attribute *coresight_attrs_source[] = {
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700484 &dev_attr_enable.attr,
485 NULL,
486};
487
Pratik Patel0a7edd32012-06-08 09:31:55 -0700488static struct attribute_group coresight_attr_grp_source = {
489 .attrs = coresight_attrs_source,
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700490};
491
Pratik Patel0a7edd32012-06-08 09:31:55 -0700492static const struct attribute_group *coresight_attr_grps_source[] = {
493 &coresight_attr_grp_source,
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700494 NULL,
495};
496
Pratik Patel0a7edd32012-06-08 09:31:55 -0700497static struct device_type coresight_dev_type[] = {
Pratik Patelb84ef9d2012-05-24 14:01:43 -0700498 {
Pratik Patelb8bb4032012-07-22 23:09:11 -0700499 .name = "none",
500 },
501 {
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");