Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2016 Freescale Semiconductor, Inc.
4 * Copyright 2017-2018 NXP
5 * Dong Aisheng <aisheng.dong@nxp.com>
6 *
7 * Implementation of the SCU based Power Domains
8 *
9 * NOTE: a better implementation suggested by Ulf Hansson is using a
10 * single global power domain and implement the ->attach|detach_dev()
11 * callback for the genpd and use the regular of_genpd_add_provider_simple().
12 * From within the ->attach_dev(), we could get the OF node for
13 * the device that is being attached and then parse the power-domain
14 * cell containing the "resource id" and store that in the per device
15 * struct generic_pm_domain_data (we have void pointer there for
16 * storing these kind of things).
17 *
18 * Additionally, we need to implement the ->stop() and ->start()
19 * callbacks of genpd, which is where you "power on/off" devices,
20 * rather than using the above ->power_on|off() callbacks.
21 *
22 * However, there're two known issues:
23 * 1. The ->attach_dev() of power domain infrastructure still does
24 * not support multi domains case as the struct device *dev passed
25 * in is a virtual PD device, it does not help for parsing the real
26 * device resource id from device tree, so it's unware of which
27 * real sub power domain of device should be attached.
28 *
29 * The framework needs some proper extension to support multi power
30 * domain cases.
31 *
32 * 2. It also breaks most of current drivers as the driver probe sequence
33 * behavior changed if removing ->power_on|off() callback and use
34 * ->start() and ->stop() instead. genpd_dev_pm_attach will only power
35 * up the domain and attach device, but will not call .start() which
36 * relies on device runtime pm. That means the device power is still
37 * not up before running driver probe function. For SCU enabled
38 * platforms, all device drivers accessing registers/clock without power
39 * domain enabled will trigger a HW access error. That means we need fix
40 * most drivers probe sequence with proper runtime pm.
41 *
42 * In summary, we need fix above two issue before being able to switch to
43 * the "single global power domain" way.
44 *
45 */
46
47#include <dt-bindings/firmware/imx/rsrc.h>
48#include <linux/firmware/imx/sci.h>
49#include <linux/io.h>
50#include <linux/module.h>
51#include <linux/of.h>
52#include <linux/of_address.h>
53#include <linux/of_platform.h>
54#include <linux/platform_device.h>
55#include <linux/pm.h>
56#include <linux/pm_domain.h>
57#include <linux/slab.h>
58
59/* SCU Power Mode Protocol definition */
60struct imx_sc_msg_req_set_resource_power_mode {
61 struct imx_sc_rpc_msg hdr;
62 u16 resource;
63 u8 mode;
64} __packed;
65
66#define IMX_SCU_PD_NAME_SIZE 20
67struct imx_sc_pm_domain {
68 struct generic_pm_domain pd;
69 char name[IMX_SCU_PD_NAME_SIZE];
70 u32 rsrc;
71};
72
73struct imx_sc_pd_range {
74 char *name;
75 u32 rsrc;
76 u8 num;
77
78 /* add domain index */
79 bool postfix;
80 u8 start_from;
81};
82
83struct imx_sc_pd_soc {
84 const struct imx_sc_pd_range *pd_ranges;
85 u8 num_ranges;
86};
87
88static const struct imx_sc_pd_range imx8qxp_scu_pd_ranges[] = {
89 /* LSIO SS */
90 { "pwm", IMX_SC_R_PWM_0, 8, true, 0 },
91 { "gpio", IMX_SC_R_GPIO_0, 8, true, 0 },
92 { "gpt", IMX_SC_R_GPT_0, 5, true, 0 },
93 { "kpp", IMX_SC_R_KPP, 1, false, 0 },
94 { "fspi", IMX_SC_R_FSPI_0, 2, true, 0 },
95 { "mu", IMX_SC_R_MU_0A, 14, true, 0 },
96
97 /* CONN SS */
98 { "usb", IMX_SC_R_USB_0, 2, true, 0 },
99 { "usb0phy", IMX_SC_R_USB_0_PHY, 1, false, 0 },
100 { "usb2", IMX_SC_R_USB_2, 1, false, 0 },
101 { "usb2phy", IMX_SC_R_USB_2_PHY, 1, false, 0 },
102 { "sdhc", IMX_SC_R_SDHC_0, 3, true, 0 },
103 { "enet", IMX_SC_R_ENET_0, 2, true, 0 },
104 { "nand", IMX_SC_R_NAND, 1, false, 0 },
105 { "mlb", IMX_SC_R_MLB_0, 1, true, 0 },
106
107 /* AUDIO SS */
108 { "audio-pll0", IMX_SC_R_AUDIO_PLL_0, 1, false, 0 },
109 { "audio-pll1", IMX_SC_R_AUDIO_PLL_1, 1, false, 0 },
110 { "audio-clk-0", IMX_SC_R_AUDIO_CLK_0, 1, false, 0 },
111 { "dma0-ch", IMX_SC_R_DMA_0_CH0, 16, true, 0 },
112 { "dma1-ch", IMX_SC_R_DMA_1_CH0, 16, true, 0 },
113 { "dma2-ch", IMX_SC_R_DMA_2_CH0, 5, true, 0 },
114 { "asrc0", IMX_SC_R_ASRC_0, 1, false, 0 },
115 { "asrc1", IMX_SC_R_ASRC_1, 1, false, 0 },
116 { "esai0", IMX_SC_R_ESAI_0, 1, false, 0 },
117 { "spdif0", IMX_SC_R_SPDIF_0, 1, false, 0 },
118 { "sai", IMX_SC_R_SAI_0, 3, true, 0 },
119 { "amix", IMX_SC_R_AMIX, 1, false, 0 },
120 { "mqs0", IMX_SC_R_MQS_0, 1, false, 0 },
121 { "dsp", IMX_SC_R_DSP, 1, false, 0 },
122 { "dsp-ram", IMX_SC_R_DSP_RAM, 1, false, 0 },
123
124 /* DMA SS */
125 { "can", IMX_SC_R_CAN_0, 3, true, 0 },
126 { "ftm", IMX_SC_R_FTM_0, 2, true, 0 },
127 { "lpi2c", IMX_SC_R_I2C_0, 4, true, 0 },
128 { "adc", IMX_SC_R_ADC_0, 1, true, 0 },
129 { "lcd", IMX_SC_R_LCD_0, 1, true, 0 },
130 { "lcd0-pwm", IMX_SC_R_LCD_0_PWM_0, 1, true, 0 },
131 { "lpuart", IMX_SC_R_UART_0, 4, true, 0 },
132 { "lpspi", IMX_SC_R_SPI_0, 4, true, 0 },
133
134 /* VPU SS */
135 { "vpu", IMX_SC_R_VPU, 1, false, 0 },
136 { "vpu-pid", IMX_SC_R_VPU_PID0, 8, true, 0 },
137 { "vpu-dec0", IMX_SC_R_VPU_DEC_0, 1, false, 0 },
138 { "vpu-enc0", IMX_SC_R_VPU_ENC_0, 1, false, 0 },
139
140 /* GPU SS */
141 { "gpu0-pid", IMX_SC_R_GPU_0_PID0, 4, true, 0 },
142
143 /* HSIO SS */
144 { "pcie-b", IMX_SC_R_PCIE_B, 1, false, 0 },
145 { "serdes-1", IMX_SC_R_SERDES_1, 1, false, 0 },
146 { "hsio-gpio", IMX_SC_R_HSIO_GPIO, 1, false, 0 },
147
148 /* MIPI SS */
149 { "mipi0", IMX_SC_R_MIPI_0, 1, false, 0 },
150 { "mipi0-pwm0", IMX_SC_R_MIPI_0_PWM_0, 1, false, 0 },
151 { "mipi0-i2c", IMX_SC_R_MIPI_0_I2C_0, 2, true, 0 },
152
153 /* LVDS SS */
154 { "lvds0", IMX_SC_R_LVDS_0, 1, false, 0 },
155
156 /* DC SS */
157 { "dc0", IMX_SC_R_DC_0, 1, false, 0 },
158 { "dc0-pll", IMX_SC_R_DC_0_PLL_0, 2, true, 0 },
159};
160
161static const struct imx_sc_pd_soc imx8qxp_scu_pd = {
162 .pd_ranges = imx8qxp_scu_pd_ranges,
163 .num_ranges = ARRAY_SIZE(imx8qxp_scu_pd_ranges),
164};
165
166static struct imx_sc_ipc *pm_ipc_handle;
167
168static inline struct imx_sc_pm_domain *
169to_imx_sc_pd(struct generic_pm_domain *genpd)
170{
171 return container_of(genpd, struct imx_sc_pm_domain, pd);
172}
173
174static int imx_sc_pd_power(struct generic_pm_domain *domain, bool power_on)
175{
176 struct imx_sc_msg_req_set_resource_power_mode msg;
177 struct imx_sc_rpc_msg *hdr = &msg.hdr;
178 struct imx_sc_pm_domain *pd;
179 int ret;
180
181 pd = to_imx_sc_pd(domain);
182
183 hdr->ver = IMX_SC_RPC_VERSION;
184 hdr->svc = IMX_SC_RPC_SVC_PM;
185 hdr->func = IMX_SC_PM_FUNC_SET_RESOURCE_POWER_MODE;
186 hdr->size = 2;
187
188 msg.resource = pd->rsrc;
189 msg.mode = power_on ? IMX_SC_PM_PW_MODE_ON : IMX_SC_PM_PW_MODE_LP;
190
191 ret = imx_scu_call_rpc(pm_ipc_handle, &msg, true);
192 if (ret)
193 dev_err(&domain->dev, "failed to power %s resource %d ret %d\n",
194 power_on ? "up" : "off", pd->rsrc, ret);
195
196 return ret;
197}
198
199static int imx_sc_pd_power_on(struct generic_pm_domain *domain)
200{
201 return imx_sc_pd_power(domain, true);
202}
203
204static int imx_sc_pd_power_off(struct generic_pm_domain *domain)
205{
206 return imx_sc_pd_power(domain, false);
207}
208
209static struct generic_pm_domain *imx_scu_pd_xlate(struct of_phandle_args *spec,
210 void *data)
211{
212 struct generic_pm_domain *domain = ERR_PTR(-ENOENT);
213 struct genpd_onecell_data *pd_data = data;
214 unsigned int i;
215
216 for (i = 0; i < pd_data->num_domains; i++) {
217 struct imx_sc_pm_domain *sc_pd;
218
219 sc_pd = to_imx_sc_pd(pd_data->domains[i]);
220 if (sc_pd->rsrc == spec->args[0]) {
221 domain = &sc_pd->pd;
222 break;
223 }
224 }
225
226 return domain;
227}
228
229static struct imx_sc_pm_domain *
230imx_scu_add_pm_domain(struct device *dev, int idx,
231 const struct imx_sc_pd_range *pd_ranges)
232{
233 struct imx_sc_pm_domain *sc_pd;
234 int ret;
235
236 sc_pd = devm_kzalloc(dev, sizeof(*sc_pd), GFP_KERNEL);
237 if (!sc_pd)
238 return ERR_PTR(-ENOMEM);
239
240 sc_pd->rsrc = pd_ranges->rsrc + idx;
241 sc_pd->pd.power_off = imx_sc_pd_power_off;
242 sc_pd->pd.power_on = imx_sc_pd_power_on;
243
244 if (pd_ranges->postfix)
245 snprintf(sc_pd->name, sizeof(sc_pd->name),
246 "%s%i", pd_ranges->name, pd_ranges->start_from + idx);
247 else
248 snprintf(sc_pd->name, sizeof(sc_pd->name),
249 "%s", pd_ranges->name);
250
251 sc_pd->pd.name = sc_pd->name;
252
253 if (sc_pd->rsrc >= IMX_SC_R_LAST) {
254 dev_warn(dev, "invalid pd %s rsrc id %d found",
255 sc_pd->name, sc_pd->rsrc);
256
257 devm_kfree(dev, sc_pd);
258 return NULL;
259 }
260
261 ret = pm_genpd_init(&sc_pd->pd, NULL, true);
262 if (ret) {
263 dev_warn(dev, "failed to init pd %s rsrc id %d",
264 sc_pd->name, sc_pd->rsrc);
265 devm_kfree(dev, sc_pd);
266 return NULL;
267 }
268
269 return sc_pd;
270}
271
272static int imx_scu_init_pm_domains(struct device *dev,
273 const struct imx_sc_pd_soc *pd_soc)
274{
275 const struct imx_sc_pd_range *pd_ranges = pd_soc->pd_ranges;
276 struct generic_pm_domain **domains;
277 struct genpd_onecell_data *pd_data;
278 struct imx_sc_pm_domain *sc_pd;
279 u32 count = 0;
280 int i, j;
281
282 for (i = 0; i < pd_soc->num_ranges; i++)
283 count += pd_ranges[i].num;
284
285 domains = devm_kcalloc(dev, count, sizeof(*domains), GFP_KERNEL);
286 if (!domains)
287 return -ENOMEM;
288
289 pd_data = devm_kzalloc(dev, sizeof(*pd_data), GFP_KERNEL);
290 if (!pd_data)
291 return -ENOMEM;
292
293 count = 0;
294 for (i = 0; i < pd_soc->num_ranges; i++) {
295 for (j = 0; j < pd_ranges[i].num; j++) {
296 sc_pd = imx_scu_add_pm_domain(dev, j, &pd_ranges[i]);
297 if (IS_ERR_OR_NULL(sc_pd))
298 continue;
299
300 domains[count++] = &sc_pd->pd;
301 dev_dbg(dev, "added power domain %s\n", sc_pd->pd.name);
302 }
303 }
304
305 pd_data->domains = domains;
306 pd_data->num_domains = count;
307 pd_data->xlate = imx_scu_pd_xlate;
308
309 of_genpd_add_provider_onecell(dev->of_node, pd_data);
310
311 return 0;
312}
313
314static int imx_sc_pd_probe(struct platform_device *pdev)
315{
316 const struct imx_sc_pd_soc *pd_soc;
317 int ret;
318
319 ret = imx_scu_get_handle(&pm_ipc_handle);
320 if (ret)
321 return ret;
322
323 pd_soc = of_device_get_match_data(&pdev->dev);
324 if (!pd_soc)
325 return -ENODEV;
326
327 return imx_scu_init_pm_domains(&pdev->dev, pd_soc);
328}
329
330static const struct of_device_id imx_sc_pd_match[] = {
331 { .compatible = "fsl,imx8qxp-scu-pd", &imx8qxp_scu_pd},
332 { .compatible = "fsl,scu-pd", &imx8qxp_scu_pd},
333 { /* sentinel */ }
334};
335
336static struct platform_driver imx_sc_pd_driver = {
337 .driver = {
338 .name = "imx-scu-pd",
339 .of_match_table = imx_sc_pd_match,
340 },
341 .probe = imx_sc_pd_probe,
342};
343builtin_platform_driver(imx_sc_pd_driver);
344
345MODULE_AUTHOR("Dong Aisheng <aisheng.dong@nxp.com>");
346MODULE_DESCRIPTION("IMX SCU Power Domain driver");
347MODULE_LICENSE("GPL v2");