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