1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Exynos generic interconnect provider driver
4 *
5 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6 *
7 * Authors: Artur Świgoń <a.swigon@samsung.com>
8 * Sylwester Nawrocki <s.nawrocki@samsung.com>
9 */
10 #include <linux/device.h>
11 #include <linux/interconnect-provider.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_qos.h>
16 #include <linux/slab.h>
17
18 #define EXYNOS_ICC_DEFAULT_BUS_CLK_RATIO 8
19
20 struct exynos_icc_priv {
21 struct device *dev;
22
23 /* One interconnect node per provider */
24 struct icc_provider provider;
25 struct icc_node *node;
26
27 struct dev_pm_qos_request qos_req;
28 u32 bus_clk_ratio;
29 };
30
exynos_icc_get_parent(struct device_node * np)31 static struct icc_node *exynos_icc_get_parent(struct device_node *np)
32 {
33 struct of_phandle_args args;
34 struct icc_node_data *icc_node_data;
35 struct icc_node *icc_node;
36 int num, ret;
37
38 num = of_count_phandle_with_args(np, "interconnects",
39 "#interconnect-cells");
40 if (num < 1)
41 return NULL; /* parent nodes are optional */
42
43 /* Get the interconnect target node */
44 ret = of_parse_phandle_with_args(np, "interconnects",
45 "#interconnect-cells", 0, &args);
46 if (ret < 0)
47 return ERR_PTR(ret);
48
49 icc_node_data = of_icc_get_from_provider(&args);
50 of_node_put(args.np);
51
52 if (IS_ERR(icc_node_data))
53 return ERR_CAST(icc_node_data);
54
55 icc_node = icc_node_data->node;
56 kfree(icc_node_data);
57
58 return icc_node;
59 }
60
exynos_generic_icc_set(struct icc_node * src,struct icc_node * dst)61 static int exynos_generic_icc_set(struct icc_node *src, struct icc_node *dst)
62 {
63 struct exynos_icc_priv *src_priv = src->data, *dst_priv = dst->data;
64 s32 src_freq = max(src->avg_bw, src->peak_bw) / src_priv->bus_clk_ratio;
65 s32 dst_freq = max(dst->avg_bw, dst->peak_bw) / dst_priv->bus_clk_ratio;
66 int ret;
67
68 ret = dev_pm_qos_update_request(&src_priv->qos_req, src_freq);
69 if (ret < 0) {
70 dev_err(src_priv->dev, "failed to update PM QoS of %s (src)\n",
71 src->name);
72 return ret;
73 }
74
75 ret = dev_pm_qos_update_request(&dst_priv->qos_req, dst_freq);
76 if (ret < 0) {
77 dev_err(dst_priv->dev, "failed to update PM QoS of %s (dst)\n",
78 dst->name);
79 return ret;
80 }
81
82 return 0;
83 }
84
exynos_generic_icc_xlate(struct of_phandle_args * spec,void * data)85 static struct icc_node *exynos_generic_icc_xlate(struct of_phandle_args *spec,
86 void *data)
87 {
88 struct exynos_icc_priv *priv = data;
89
90 if (spec->np != priv->dev->parent->of_node)
91 return ERR_PTR(-EINVAL);
92
93 return priv->node;
94 }
95
exynos_generic_icc_remove(struct platform_device * pdev)96 static int exynos_generic_icc_remove(struct platform_device *pdev)
97 {
98 struct exynos_icc_priv *priv = platform_get_drvdata(pdev);
99
100 icc_provider_deregister(&priv->provider);
101 icc_nodes_remove(&priv->provider);
102
103 return 0;
104 }
105
exynos_generic_icc_probe(struct platform_device * pdev)106 static int exynos_generic_icc_probe(struct platform_device *pdev)
107 {
108 struct device *bus_dev = pdev->dev.parent;
109 struct exynos_icc_priv *priv;
110 struct icc_provider *provider;
111 struct icc_node *icc_node, *icc_parent_node;
112 int ret;
113
114 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
115 if (!priv)
116 return -ENOMEM;
117
118 priv->dev = &pdev->dev;
119 platform_set_drvdata(pdev, priv);
120
121 provider = &priv->provider;
122
123 provider->set = exynos_generic_icc_set;
124 provider->aggregate = icc_std_aggregate;
125 provider->xlate = exynos_generic_icc_xlate;
126 provider->dev = bus_dev;
127 provider->inter_set = true;
128 provider->data = priv;
129
130 icc_provider_init(provider);
131
132 icc_node = icc_node_create(pdev->id);
133 if (IS_ERR(icc_node))
134 return PTR_ERR(icc_node);
135
136 priv->node = icc_node;
137 icc_node->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%pOFn",
138 bus_dev->of_node);
139 if (of_property_read_u32(bus_dev->of_node, "samsung,data-clock-ratio",
140 &priv->bus_clk_ratio))
141 priv->bus_clk_ratio = EXYNOS_ICC_DEFAULT_BUS_CLK_RATIO;
142
143 icc_node->data = priv;
144 icc_node_add(icc_node, provider);
145
146 /*
147 * Register a PM QoS request for the parent (devfreq) device.
148 */
149 ret = dev_pm_qos_add_request(bus_dev, &priv->qos_req,
150 DEV_PM_QOS_MIN_FREQUENCY, 0);
151 if (ret < 0)
152 goto err_node_del;
153
154 icc_parent_node = exynos_icc_get_parent(bus_dev->of_node);
155 if (IS_ERR(icc_parent_node)) {
156 ret = PTR_ERR(icc_parent_node);
157 goto err_pmqos_del;
158 }
159 if (icc_parent_node) {
160 ret = icc_link_create(icc_node, icc_parent_node->id);
161 if (ret < 0)
162 goto err_pmqos_del;
163 }
164
165 ret = icc_provider_register(provider);
166 if (ret < 0)
167 goto err_pmqos_del;
168
169 return 0;
170
171 err_pmqos_del:
172 dev_pm_qos_remove_request(&priv->qos_req);
173 err_node_del:
174 icc_nodes_remove(provider);
175
176 return ret;
177 }
178
179 static struct platform_driver exynos_generic_icc_driver = {
180 .driver = {
181 .name = "exynos-generic-icc",
182 .sync_state = icc_sync_state,
183 },
184 .probe = exynos_generic_icc_probe,
185 .remove = exynos_generic_icc_remove,
186 };
187 module_platform_driver(exynos_generic_icc_driver);
188
189 MODULE_DESCRIPTION("Exynos generic interconnect driver");
190 MODULE_AUTHOR("Artur Świgoń <a.swigon@samsung.com>");
191 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
192 MODULE_LICENSE("GPL v2");
193 MODULE_ALIAS("platform:exynos-generic-icc");
194