1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
4  *
5  * Description: CoreSight Replicator driver
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/device.h>
10 #include <linux/platform_device.h>
11 #include <linux/io.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/clk.h>
16 #include <linux/of.h>
17 #include <linux/coresight.h>
18 
19 #include "coresight-priv.h"
20 
21 /**
22  * struct replicator_drvdata - specifics associated to a replicator component
23  * @dev:	the device entity associated with this component
24  * @atclk:	optional clock for the core parts of the replicator.
25  * @csdev:	component vitals needed by the framework
26  */
27 struct replicator_drvdata {
28 	struct device		*dev;
29 	struct clk		*atclk;
30 	struct coresight_device	*csdev;
31 };
32 
33 static int replicator_enable(struct coresight_device *csdev, int inport,
34 			     int outport)
35 {
36 	struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
37 
38 	dev_dbg(drvdata->dev, "REPLICATOR enabled\n");
39 	return 0;
40 }
41 
42 static void replicator_disable(struct coresight_device *csdev, int inport,
43 			       int outport)
44 {
45 	struct replicator_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
46 
47 	dev_dbg(drvdata->dev, "REPLICATOR disabled\n");
48 }
49 
50 static const struct coresight_ops_link replicator_link_ops = {
51 	.enable		= replicator_enable,
52 	.disable	= replicator_disable,
53 };
54 
55 static const struct coresight_ops replicator_cs_ops = {
56 	.link_ops	= &replicator_link_ops,
57 };
58 
59 static int replicator_probe(struct platform_device *pdev)
60 {
61 	int ret;
62 	struct device *dev = &pdev->dev;
63 	struct coresight_platform_data *pdata = NULL;
64 	struct replicator_drvdata *drvdata;
65 	struct coresight_desc desc = { 0 };
66 	struct device_node *np = pdev->dev.of_node;
67 
68 	if (np) {
69 		pdata = of_get_coresight_platform_data(dev, np);
70 		if (IS_ERR(pdata))
71 			return PTR_ERR(pdata);
72 		pdev->dev.platform_data = pdata;
73 	}
74 
75 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
76 	if (!drvdata)
77 		return -ENOMEM;
78 
79 	drvdata->dev = &pdev->dev;
80 	drvdata->atclk = devm_clk_get(&pdev->dev, "atclk"); /* optional */
81 	if (!IS_ERR(drvdata->atclk)) {
82 		ret = clk_prepare_enable(drvdata->atclk);
83 		if (ret)
84 			return ret;
85 	}
86 	pm_runtime_get_noresume(&pdev->dev);
87 	pm_runtime_set_active(&pdev->dev);
88 	pm_runtime_enable(&pdev->dev);
89 	platform_set_drvdata(pdev, drvdata);
90 
91 	desc.type = CORESIGHT_DEV_TYPE_LINK;
92 	desc.subtype.link_subtype = CORESIGHT_DEV_SUBTYPE_LINK_SPLIT;
93 	desc.ops = &replicator_cs_ops;
94 	desc.pdata = pdev->dev.platform_data;
95 	desc.dev = &pdev->dev;
96 	drvdata->csdev = coresight_register(&desc);
97 	if (IS_ERR(drvdata->csdev)) {
98 		ret = PTR_ERR(drvdata->csdev);
99 		goto out_disable_pm;
100 	}
101 
102 	pm_runtime_put(&pdev->dev);
103 
104 	return 0;
105 
106 out_disable_pm:
107 	if (!IS_ERR(drvdata->atclk))
108 		clk_disable_unprepare(drvdata->atclk);
109 	pm_runtime_put_noidle(&pdev->dev);
110 	pm_runtime_disable(&pdev->dev);
111 
112 	return ret;
113 }
114 
115 #ifdef CONFIG_PM
116 static int replicator_runtime_suspend(struct device *dev)
117 {
118 	struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
119 
120 	if (drvdata && !IS_ERR(drvdata->atclk))
121 		clk_disable_unprepare(drvdata->atclk);
122 
123 	return 0;
124 }
125 
126 static int replicator_runtime_resume(struct device *dev)
127 {
128 	struct replicator_drvdata *drvdata = dev_get_drvdata(dev);
129 
130 	if (drvdata && !IS_ERR(drvdata->atclk))
131 		clk_prepare_enable(drvdata->atclk);
132 
133 	return 0;
134 }
135 #endif
136 
137 static const struct dev_pm_ops replicator_dev_pm_ops = {
138 	SET_RUNTIME_PM_OPS(replicator_runtime_suspend,
139 			   replicator_runtime_resume, NULL)
140 };
141 
142 static const struct of_device_id replicator_match[] = {
143 	{.compatible = "arm,coresight-replicator"},
144 	{}
145 };
146 
147 static struct platform_driver replicator_driver = {
148 	.probe          = replicator_probe,
149 	.driver         = {
150 		.name   = "coresight-replicator",
151 		.of_match_table = replicator_match,
152 		.pm	= &replicator_dev_pm_ops,
153 		.suppress_bind_attrs = true,
154 	},
155 };
156 builtin_platform_driver(replicator_driver);
157