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(const struct of_phandle_args * spec,void * data)85 static struct icc_node *exynos_generic_icc_xlate(const 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 void 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
exynos_generic_icc_probe(struct platform_device * pdev)104 static int exynos_generic_icc_probe(struct platform_device *pdev)
105 {
106 struct device *bus_dev = pdev->dev.parent;
107 struct exynos_icc_priv *priv;
108 struct icc_provider *provider;
109 struct icc_node *icc_node, *icc_parent_node;
110 int ret;
111
112 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
113 if (!priv)
114 return -ENOMEM;
115
116 priv->dev = &pdev->dev;
117 platform_set_drvdata(pdev, priv);
118
119 provider = &priv->provider;
120
121 provider->set = exynos_generic_icc_set;
122 provider->aggregate = icc_std_aggregate;
123 provider->xlate = exynos_generic_icc_xlate;
124 provider->dev = bus_dev;
125 provider->inter_set = true;
126 provider->data = priv;
127
128 icc_provider_init(provider);
129
130 icc_node = icc_node_create(pdev->id);
131 if (IS_ERR(icc_node))
132 return PTR_ERR(icc_node);
133
134 priv->node = icc_node;
135 icc_node->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%pOFn",
136 bus_dev->of_node);
137 if (of_property_read_u32(bus_dev->of_node, "samsung,data-clock-ratio",
138 &priv->bus_clk_ratio))
139 priv->bus_clk_ratio = EXYNOS_ICC_DEFAULT_BUS_CLK_RATIO;
140
141 icc_node->data = priv;
142 icc_node_add(icc_node, provider);
143
144 /*
145 * Register a PM QoS request for the parent (devfreq) device.
146 */
147 ret = dev_pm_qos_add_request(bus_dev, &priv->qos_req,
148 DEV_PM_QOS_MIN_FREQUENCY, 0);
149 if (ret < 0)
150 goto err_node_del;
151
152 icc_parent_node = exynos_icc_get_parent(bus_dev->of_node);
153 if (IS_ERR(icc_parent_node)) {
154 ret = PTR_ERR(icc_parent_node);
155 goto err_pmqos_del;
156 }
157 if (icc_parent_node) {
158 ret = icc_link_create(icc_node, icc_parent_node->id);
159 if (ret < 0)
160 goto err_pmqos_del;
161 }
162
163 ret = icc_provider_register(provider);
164 if (ret < 0)
165 goto err_pmqos_del;
166
167 return 0;
168
169 err_pmqos_del:
170 dev_pm_qos_remove_request(&priv->qos_req);
171 err_node_del:
172 icc_nodes_remove(provider);
173
174 return ret;
175 }
176
177 static struct platform_driver exynos_generic_icc_driver = {
178 .driver = {
179 .name = "exynos-generic-icc",
180 .sync_state = icc_sync_state,
181 },
182 .probe = exynos_generic_icc_probe,
183 .remove_new = exynos_generic_icc_remove,
184 };
185 module_platform_driver(exynos_generic_icc_driver);
186
187 MODULE_DESCRIPTION("Exynos generic interconnect driver");
188 MODULE_AUTHOR("Artur Świgoń <a.swigon@samsung.com>");
189 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
190 MODULE_LICENSE("GPL v2");
191 MODULE_ALIAS("platform:exynos-generic-icc");
192