at commit c03c1a14

1 // SPDX-License-Identifier: GPL-2.0+
2
3 /*
4 * Copyright 2021 Pengutronix, Lucas Stach <kernel@pengutronix.de>
5 */
6
7 #include <linux/bitfield.h>
8 #include <linux/device.h>
9 #include <linux/interconnect.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_domain.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regmap.h>
17 #include <linux/clk.h>
18 #include <linux/mutex.h>
19
20 #include <dt-bindings/power/imx8mm-power.h>
21 #include <dt-bindings/power/imx8mn-power.h>
22 #include <dt-bindings/power/imx8mp-power.h>
23 #include <dt-bindings/power/imx8mq-power.h>
24
25 #define BLK_SFT_RSTN 0x0
26 #define BLK_CLK_EN 0x4
27 #define BLK_MIPI_RESET_DIV 0x8 /* Mini/Nano/Plus DISPLAY_BLK_CTRL only */
28
29 struct imx8m_blk_ctrl_domain;
30
31 struct imx8m_blk_ctrl {
32 struct device *dev;
33 struct notifier_block power_nb;
34 struct device *bus_power_dev;
35 struct regmap *regmap;
36 struct imx8m_blk_ctrl_domain *domains;
37 struct genpd_onecell_data onecell_data;
38 /*
39 * Serializes the blk-ctrl reset/clock sequence across sibling domains;
40 * their transitions interact through the shared VPUMIX bus domain,
41 * VPU_NOC and the not-ack-verified ADB400 handshake (ERR050531).
42 */
43 struct mutex power_lock;
44 };
45
46 struct imx8m_blk_ctrl_domain_data {
47 const char *name;
48 const char * const *clk_names;
49 const char * const *path_names;
50 const char *gpc_name;
51 int num_clks;
52 int num_paths;
53 u32 rst_mask;
54 u32 clk_mask;
55
56 /*
57 * i.MX8M Mini, Nano and Plus have a third DISPLAY_BLK_CTRL register
58 * which is used to control the reset for the MIPI Phy.
59 * Since it's only present in certain circumstances,
60 * an if-statement should be used before setting and clearing this
61 * register.
62 */
63 u32 mipi_phy_rst_mask;
64
65 /*
66 * VC8000E reset de-assertion edge and AXI clock may have a timing issue.
67 * Workaround: Set bit2 (vc8000e_clk_en) of BLK_CLK_EN_CSR to 0 to gate off
68 * both AXI clock and VC8000E clock sent to VC8000E and AXI clock sent to
69 * VPU_NOC m_v_2 interface during VC8000E power up(VC8000E reset is
70 * de-asserted by HW)
71 */
72 bool is_errata_err050531;
73 };
74
75 #define DOMAIN_MAX_CLKS 4
76 #define DOMAIN_MAX_PATHS 4
77
78 struct imx8m_blk_ctrl_domain {
79 struct generic_pm_domain genpd;
80 const struct imx8m_blk_ctrl_domain_data *data;
81 struct clk_bulk_data clks[DOMAIN_MAX_CLKS];
82 struct icc_bulk_data paths[DOMAIN_MAX_PATHS];
83 struct device *power_dev;
84 struct imx8m_blk_ctrl *bc;
85 int num_paths;
86 };
87
88 struct imx8m_blk_ctrl_data {
89 int max_reg;
90 notifier_fn_t power_notifier_fn;
91 const struct imx8m_blk_ctrl_domain_data *domains;
92 int num_domains;
93 };
94
95 static inline struct imx8m_blk_ctrl_domain *
96 to_imx8m_blk_ctrl_domain(struct generic_pm_domain *genpd)
97 {
98 return container_of(genpd, struct imx8m_blk_ctrl_domain, genpd);
99 }
100
101 static int imx8m_blk_ctrl_power_on(struct generic_pm_domain *genpd)
102 {
103 struct imx8m_blk_ctrl_domain *domain = to_imx8m_blk_ctrl_domain(genpd);
104 const struct imx8m_blk_ctrl_domain_data *data = domain->data;
105 struct imx8m_blk_ctrl *bc = domain->bc;
106 int ret;
107
108 guard(mutex)(&bc->power_lock);
109
110 /* make sure bus domain is awake */
111 ret = pm_runtime_get_sync(bc->bus_power_dev);
112 if (ret < 0) {
113 pm_runtime_put_noidle(bc->bus_power_dev);
114 dev_err(bc->dev, "failed to power up bus domain\n");
115 return ret;
116 }
117
118 /* put devices into reset */
119 regmap_clear_bits(bc->regmap, BLK_SFT_RSTN, data->rst_mask);
120 if (data->mipi_phy_rst_mask)
121 regmap_clear_bits(bc->regmap, BLK_MIPI_RESET_DIV, data->mipi_phy_rst_mask);
122
123 /* enable upstream and blk-ctrl clocks to allow reset to propagate */
124 ret = clk_bulk_prepare_enable(data->num_clks, domain->clks);
125 if (ret) {
126 dev_err(bc->dev, "failed to enable clocks\n");
127 goto bus_put;
128 }
129
130 if (data->is_errata_err050531)
131 regmap_clear_bits(bc->regmap, BLK_CLK_EN, data->clk_mask);
132 else
133 regmap_set_bits(bc->regmap, BLK_CLK_EN, data->clk_mask);
134
135 /* power up upstream GPC domain */
136 ret = pm_runtime_get_sync(domain->power_dev);
137 if (ret < 0) {
138 dev_err(bc->dev, "failed to power up peripheral domain\n");
139 goto clk_disable;
140 }
141
142 if (data->is_errata_err050531)
143 regmap_set_bits(bc->regmap, BLK_CLK_EN, data->clk_mask);
144
145 /* wait for reset to propagate */
146 udelay(5);
147
148 /* release reset */
149 regmap_set_bits(bc->regmap, BLK_SFT_RSTN, data->rst_mask);
150 if (data->mipi_phy_rst_mask)
151 regmap_set_bits(bc->regmap, BLK_MIPI_RESET_DIV, data->mipi_phy_rst_mask);
152
153 ret = icc_bulk_set_bw(domain->num_paths, domain->paths);
154 if (ret)
155 dev_err(bc->dev, "failed to set icc bw\n");
156
157 /* disable upstream clocks */
158 clk_bulk_disable_unprepare(data->num_clks, domain->clks);
159
160 return 0;
161
162 clk_disable:
163 clk_bulk_disable_unprepare(data->num_clks, domain->clks);
164 bus_put:
165 pm_runtime_put(bc->bus_power_dev);
166
167 return ret;
168 }
169
170 static int imx8m_blk_ctrl_power_off(struct generic_pm_domain *genpd)
171 {
172 struct imx8m_blk_ctrl_domain *domain = to_imx8m_blk_ctrl_domain(genpd);
173 const struct imx8m_blk_ctrl_domain_data *data = domain->data;
174 struct imx8m_blk_ctrl *bc = domain->bc;
175
176 guard(mutex)(&bc->power_lock);
177
178 /* put devices into reset and disable clocks */
179 if (data->mipi_phy_rst_mask)
180 regmap_clear_bits(bc->regmap, BLK_MIPI_RESET_DIV, data->mipi_phy_rst_mask);
181
182 regmap_clear_bits(bc->regmap, BLK_SFT_RSTN, data->rst_mask);
183 regmap_clear_bits(bc->regmap, BLK_CLK_EN, data->clk_mask);
184
185 /* power down upstream GPC domain */
186 pm_runtime_put(domain->power_dev);
187
188 /* allow bus domain to suspend */
189 pm_runtime_put(bc->bus_power_dev);
190
191 return 0;
192 }
193
194 static struct lock_class_key blk_ctrl_genpd_lock_class;
195
196 static int imx8m_blk_ctrl_probe(struct platform_device *pdev)
197 {
198 const struct imx8m_blk_ctrl_data *bc_data;
199 struct device *dev = &pdev->dev;
200 struct imx8m_blk_ctrl *bc;
201 void __iomem *base;
202 int i, ret;
203
204 struct regmap_config regmap_config = {
205 .reg_bits = 32,
206 .val_bits = 32,
207 .reg_stride = 4,
208 };
209
210 bc = devm_kzalloc(dev, sizeof(*bc), GFP_KERNEL);
211 if (!bc)
212 return -ENOMEM;
213
214 bc->dev = dev;
215
216 ret = devm_mutex_init(dev, &bc->power_lock);
217 if (ret)
218 return ret;
219
220 bc_data = of_device_get_match_data(dev);
221
222 base = devm_platform_ioremap_resource(pdev, 0);
223 if (IS_ERR(base))
224 return PTR_ERR(base);
225
226 regmap_config.max_register = bc_data->max_reg;
227 bc->regmap = devm_regmap_init_mmio(dev, base, &regmap_config);
228 if (IS_ERR(bc->regmap))
229 return dev_err_probe(dev, PTR_ERR(bc->regmap),
230 "failed to init regmap\n");
231
232 bc->domains = devm_kcalloc(dev, bc_data->num_domains,
233 sizeof(struct imx8m_blk_ctrl_domain),
234 GFP_KERNEL);
235 if (!bc->domains)
236 return -ENOMEM;
237
238 bc->onecell_data.num_domains = bc_data->num_domains;
239 bc->onecell_data.domains =
240 devm_kcalloc(dev, bc_data->num_domains,
241 sizeof(struct generic_pm_domain *), GFP_KERNEL);
242 if (!bc->onecell_data.domains)
243 return -ENOMEM;
244
245 bc->bus_power_dev = dev_pm_domain_attach_by_name(dev, "bus");
246 if (IS_ERR(bc->bus_power_dev)) {
247 if (PTR_ERR(bc->bus_power_dev) == -ENODEV)
248 return dev_err_probe(dev, -EPROBE_DEFER,
249 "failed to attach power domain \"bus\"\n");
250 else
251 return dev_err_probe(dev, PTR_ERR(bc->bus_power_dev),
252 "failed to attach power domain \"bus\"\n");
253 }
254
255 for (i = 0; i < bc_data->num_domains; i++) {
256 const struct imx8m_blk_ctrl_domain_data *data = &bc_data->domains[i];
257 struct imx8m_blk_ctrl_domain *domain = &bc->domains[i];
258 int j;
259
260 domain->data = data;
261 domain->num_paths = data->num_paths;
262
263 for (j = 0; j < data->num_clks; j++)
264 domain->clks[j].id = data->clk_names[j];
265
266 for (j = 0; j < data->num_paths; j++) {
267 domain->paths[j].name = data->path_names[j];
268 /* Fake value for now, just let ICC could configure NoC mode/priority */
269 domain->paths[j].avg_bw = 1;
270 domain->paths[j].peak_bw = 1;
271 }
272
273 ret = devm_of_icc_bulk_get(dev, data->num_paths, domain->paths);
274 if (ret) {
275 if (ret != -EPROBE_DEFER) {
276 dev_warn_once(dev, "Could not get interconnect paths, NoC will stay unconfigured!\n");
277 domain->num_paths = 0;
278 } else {
279 dev_err_probe(dev, ret, "failed to get noc entries\n");
280 goto cleanup_pds;
281 }
282 }
283
284 ret = devm_clk_bulk_get(dev, data->num_clks, domain->clks);
285 if (ret) {
286 dev_err_probe(dev, ret, "failed to get clock\n");
287 goto cleanup_pds;
288 }
289
290 domain->power_dev =
291 dev_pm_domain_attach_by_name(dev, data->gpc_name);
292 if (IS_ERR_OR_NULL(domain->power_dev)) {
293 if (!domain->power_dev)
294 ret = -ENODEV;
295 else
296 ret = PTR_ERR(domain->power_dev);
297 dev_err_probe(dev, ret,
298 "failed to attach power domain \"%s\"\n",
299 data->gpc_name);
300 goto cleanup_pds;
301 }
302
303 domain->genpd.name = data->name;
304 domain->genpd.power_on = imx8m_blk_ctrl_power_on;
305 domain->genpd.power_off = imx8m_blk_ctrl_power_off;
306 domain->bc = bc;
307
308 ret = pm_genpd_init(&domain->genpd, NULL, true);
309 if (ret) {
310 dev_err_probe(dev, ret,
311 "failed to init power domain \"%s\"\n",
312 data->gpc_name);
313 dev_pm_domain_detach(domain->power_dev, true);
314 goto cleanup_pds;
315 }
316
317 /*
318 * We use runtime PM to trigger power on/off of the upstream GPC
319 * domain, as a strict hierarchical parent/child power domain
320 * setup doesn't allow us to meet the sequencing requirements.
321 * This means we have nested locking of genpd locks, without the
322 * nesting being visible at the genpd level, so we need a
323 * separate lock class to make lockdep aware of the fact that
324 * this are separate domain locks that can be nested without a
325 * self-deadlock.
326 */
327 lockdep_set_class(&domain->genpd.mlock,
328 &blk_ctrl_genpd_lock_class);
329
330 bc->onecell_data.domains[i] = &domain->genpd;
331 }
332
333 ret = of_genpd_add_provider_onecell(dev->of_node, &bc->onecell_data);
334 if (ret) {
335 dev_err_probe(dev, ret, "failed to add power domain provider\n");
336 goto cleanup_pds;
337 }
338
339 bc->power_nb.notifier_call = bc_data->power_notifier_fn;
340 ret = dev_pm_genpd_add_notifier(bc->bus_power_dev, &bc->power_nb);
341 if (ret) {
342 dev_err_probe(dev, ret, "failed to add power notifier\n");
343 goto cleanup_provider;
344 }
345
346 dev_set_drvdata(dev, bc);
347
348 ret = devm_of_platform_populate(dev);
349 if (ret)
350 goto cleanup_provider;
351
352 return 0;
353
354 cleanup_provider:
355 of_genpd_del_provider(dev->of_node);
356 cleanup_pds:
357 for (i--; i >= 0; i--) {
358 pm_genpd_remove(&bc->domains[i].genpd);
359 dev_pm_domain_detach(bc->domains[i].power_dev, true);
360 }
361
362 dev_pm_domain_detach(bc->bus_power_dev, true);
363
364 return ret;
365 }
366
367 static void imx8m_blk_ctrl_remove(struct platform_device *pdev)
368 {
369 struct imx8m_blk_ctrl *bc = dev_get_drvdata(&pdev->dev);
370 int i;
371
372 of_genpd_del_provider(pdev->dev.of_node);
373
374 for (i = 0; i < bc->onecell_data.num_domains; i++) {
375 struct imx8m_blk_ctrl_domain *domain = &bc->domains[i];
376
377 pm_genpd_remove(&domain->genpd);
378 dev_pm_domain_detach(domain->power_dev, true);
379 }
380
381 dev_pm_genpd_remove_notifier(bc->bus_power_dev);
382
383 dev_pm_domain_detach(bc->bus_power_dev, true);
384 }
385
386 #ifdef CONFIG_PM_SLEEP
387 static int imx8m_blk_ctrl_suspend(struct device *dev)
388 {
389 struct imx8m_blk_ctrl *bc = dev_get_drvdata(dev);
390 int ret, i;
391
392 /*
393 * This may look strange, but is done so the generic PM_SLEEP code
394 * can power down our domains and more importantly power them up again
395 * after resume, without tripping over our usage of runtime PM to
396 * control the upstream GPC domains. Things happen in the right order
397 * in the system suspend/resume paths due to the device parent/child
398 * hierarchy.
399 */
400 ret = pm_runtime_get_sync(bc->bus_power_dev);
401 if (ret < 0) {
402 pm_runtime_put_noidle(bc->bus_power_dev);
403 return ret;
404 }
405
406 for (i = 0; i < bc->onecell_data.num_domains; i++) {
407 struct imx8m_blk_ctrl_domain *domain = &bc->domains[i];
408
409 ret = pm_runtime_get_sync(domain->power_dev);
410 if (ret < 0) {
411 pm_runtime_put_noidle(domain->power_dev);
412 goto out_fail;
413 }
414 }
415
416 return 0;
417
418 out_fail:
419 for (i--; i >= 0; i--)
420 pm_runtime_put(bc->domains[i].power_dev);
421
422 pm_runtime_put(bc->bus_power_dev);
423
424 return ret;
425 }
426
427 static int imx8m_blk_ctrl_resume(struct device *dev)
428 {
429 struct imx8m_blk_ctrl *bc = dev_get_drvdata(dev);
430 int i;
431
432 for (i = 0; i < bc->onecell_data.num_domains; i++)
433 pm_runtime_put(bc->domains[i].power_dev);
434
435 pm_runtime_put(bc->bus_power_dev);
436
437 return 0;
438 }
439 #endif
440
441 static const struct dev_pm_ops imx8m_blk_ctrl_pm_ops = {
442 SET_SYSTEM_SLEEP_PM_OPS(imx8m_blk_ctrl_suspend, imx8m_blk_ctrl_resume)
443 };
444
445 static int imx8mm_vpu_power_notifier(struct notifier_block *nb,
446 unsigned long action, void *data)
447 {
448 struct imx8m_blk_ctrl *bc = container_of(nb, struct imx8m_blk_ctrl,
449 power_nb);
450
451 if (action != GENPD_NOTIFY_ON && action != GENPD_NOTIFY_PRE_OFF)
452 return NOTIFY_OK;
453
454 /*
455 * The ADB in the VPUMIX domain has no separate reset and clock
456 * enable bits, but is ungated together with the VPU clocks. To
457 * allow the handshake with the GPC to progress we put the VPUs
458 * in reset and ungate the clocks.
459 */
460 regmap_clear_bits(bc->regmap, BLK_SFT_RSTN, BIT(0) | BIT(1) | BIT(2));
461 regmap_set_bits(bc->regmap, BLK_CLK_EN, BIT(0) | BIT(1) | BIT(2));
462
463 if (action == GENPD_NOTIFY_ON) {
464 /*
465 * On power up we have no software backchannel to the GPC to
466 * wait for the ADB handshake to happen, so we just delay for a
467 * bit. On power down the GPC driver waits for the handshake.
468 */
469 udelay(5);
470
471 /* set "fuse" bits to enable the VPUs */
472 regmap_set_bits(bc->regmap, 0x8, 0xffffffff);
473 regmap_set_bits(bc->regmap, 0xc, 0xffffffff);
474 regmap_set_bits(bc->regmap, 0x10, 0xffffffff);
475 regmap_set_bits(bc->regmap, 0x14, 0xffffffff);
476 }
477
478 return NOTIFY_OK;
479 }
480
481 static const struct imx8m_blk_ctrl_domain_data imx8mm_vpu_blk_ctl_domain_data[] = {
482 [IMX8MM_VPUBLK_PD_G1] = {
483 .name = "vpublk-g1",
484 .clk_names = (const char *[]){ "g1", },
485 .num_clks = 1,
486 .gpc_name = "g1",
487 .rst_mask = BIT(1),
488 .clk_mask = BIT(1),
489 },
490 [IMX8MM_VPUBLK_PD_G2] = {
491 .name = "vpublk-g2",
492 .clk_names = (const char *[]){ "g2", },
493 .num_clks = 1,
494 .gpc_name = "g2",
495 .rst_mask = BIT(0),
496 .clk_mask = BIT(0),
497 },
498 [IMX8MM_VPUBLK_PD_H1] = {
499 .name = "vpublk-h1",
500 .clk_names = (const char *[]){ "h1", },
501 .num_clks = 1,
502 .gpc_name = "h1",
503 .rst_mask = BIT(2),
504 .clk_mask = BIT(2),
505 },
506 };
507
508 static const struct imx8m_blk_ctrl_data imx8mm_vpu_blk_ctl_dev_data = {
509 .max_reg = 0x18,
510 .power_notifier_fn = imx8mm_vpu_power_notifier,
511 .domains = imx8mm_vpu_blk_ctl_domain_data,
512 .num_domains = ARRAY_SIZE(imx8mm_vpu_blk_ctl_domain_data),
513 };
514
515 static const struct imx8m_blk_ctrl_domain_data imx8mp_vpu_blk_ctl_domain_data[] = {
516 [IMX8MP_VPUBLK_PD_G1] = {
517 .name = "vpublk-g1",
518 .clk_names = (const char *[]){ "g1", },
519 .num_clks = 1,
520 .gpc_name = "g1",
521 .rst_mask = BIT(1),
522 .clk_mask = BIT(1),
523 .path_names = (const char *[]){"g1"},
524 .num_paths = 1,
525 },
526 [IMX8MP_VPUBLK_PD_G2] = {
527 .name = "vpublk-g2",
528 .clk_names = (const char *[]){ "g2", },
529 .num_clks = 1,
530 .gpc_name = "g2",
531 .rst_mask = BIT(0),
532 .clk_mask = BIT(0),
533 .path_names = (const char *[]){"g2"},
534 .num_paths = 1,
535 },
536 [IMX8MP_VPUBLK_PD_VC8000E] = {
537 .name = "vpublk-vc8000e",
538 .clk_names = (const char *[]){ "vc8000e", },
539 .num_clks = 1,
540 .gpc_name = "vc8000e",
541 .rst_mask = BIT(2),
542 .clk_mask = BIT(2),
543 .path_names = (const char *[]){"vc8000e"},
544 .num_paths = 1,
545 .is_errata_err050531 = true,
546 },
547 };
548
549 static int imx8mp_vpu_power_notifier(struct notifier_block *nb,
550 unsigned long action, void *data)
551 {
552 struct imx8m_blk_ctrl *bc = container_of(nb, struct imx8m_blk_ctrl,
553 power_nb);
554
555 if (action == GENPD_NOTIFY_ON) {
556 /*
557 * On power up we have no software backchannel to the GPC to
558 * wait for the ADB handshake to happen, so we just delay for a
559 * bit. On power down the GPC driver waits for the handshake.
560 */
561
562 udelay(5);
563
564 /* set "fuse" bits to enable the VPUs */
565 regmap_set_bits(bc->regmap, 0x8, 0xffffffff);
566 regmap_set_bits(bc->regmap, 0xc, 0xffffffff);
567 regmap_set_bits(bc->regmap, 0x10, 0xffffffff);
568 regmap_set_bits(bc->regmap, 0x14, 0xffffffff);
569 }
570
571 return NOTIFY_OK;
572 }
573
574 static const struct imx8m_blk_ctrl_data imx8mp_vpu_blk_ctl_dev_data = {
575 .max_reg = 0x18,
576 .power_notifier_fn = imx8mp_vpu_power_notifier,
577 .domains = imx8mp_vpu_blk_ctl_domain_data,
578 .num_domains = ARRAY_SIZE(imx8mp_vpu_blk_ctl_domain_data),
579 };
580
581 static int imx8mm_disp_power_notifier(struct notifier_block *nb,
582 unsigned long action, void *data)
583 {
584 struct imx8m_blk_ctrl *bc = container_of(nb, struct imx8m_blk_ctrl,
585 power_nb);
586
587 if (action != GENPD_NOTIFY_ON && action != GENPD_NOTIFY_PRE_OFF)
588 return NOTIFY_OK;
589
590 /* Enable bus clock and deassert bus reset */
591 regmap_set_bits(bc->regmap, BLK_CLK_EN, BIT(12));
592 regmap_set_bits(bc->regmap, BLK_SFT_RSTN, BIT(6));
593
594 /*
595 * On power up we have no software backchannel to the GPC to
596 * wait for the ADB handshake to happen, so we just delay for a
597 * bit. On power down the GPC driver waits for the handshake.
598 */
599 if (action == GENPD_NOTIFY_ON)
600 udelay(5);
601
602
603 return NOTIFY_OK;
604 }
605
606 static const struct imx8m_blk_ctrl_domain_data imx8mm_disp_blk_ctl_domain_data[] = {
607 [IMX8MM_DISPBLK_PD_CSI_BRIDGE] = {
608 .name = "dispblk-csi-bridge",
609 .clk_names = (const char *[]){ "csi-bridge-axi", "csi-bridge-apb",
610 "csi-bridge-core", },
611 .num_clks = 3,
612 .gpc_name = "csi-bridge",
613 .rst_mask = BIT(0) | BIT(1) | BIT(2),
614 .clk_mask = BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5),
615 },
616 [IMX8MM_DISPBLK_PD_LCDIF] = {
617 .name = "dispblk-lcdif",
618 .clk_names = (const char *[]){ "lcdif-axi", "lcdif-apb", "lcdif-pix", },
619 .num_clks = 3,
620 .gpc_name = "lcdif",
621 .clk_mask = BIT(6) | BIT(7),
622 },
623 [IMX8MM_DISPBLK_PD_MIPI_DSI] = {
624 .name = "dispblk-mipi-dsi",
625 .clk_names = (const char *[]){ "dsi-pclk", "dsi-ref", },
626 .num_clks = 2,
627 .gpc_name = "mipi-dsi",
628 .rst_mask = BIT(5),
629 .clk_mask = BIT(8) | BIT(9),
630 .mipi_phy_rst_mask = BIT(17),
631 },
632 [IMX8MM_DISPBLK_PD_MIPI_CSI] = {
633 .name = "dispblk-mipi-csi",
634 .clk_names = (const char *[]){ "csi-aclk", "csi-pclk" },
635 .num_clks = 2,
636 .gpc_name = "mipi-csi",
637 .rst_mask = BIT(3) | BIT(4),
638 .clk_mask = BIT(10) | BIT(11),
639 .mipi_phy_rst_mask = BIT(16),
640 },
641 };
642
643 static const struct imx8m_blk_ctrl_data imx8mm_disp_blk_ctl_dev_data = {
644 .max_reg = 0x2c,
645 .power_notifier_fn = imx8mm_disp_power_notifier,
646 .domains = imx8mm_disp_blk_ctl_domain_data,
647 .num_domains = ARRAY_SIZE(imx8mm_disp_blk_ctl_domain_data),
648 };
649
650
651 static int imx8mn_disp_power_notifier(struct notifier_block *nb,
652 unsigned long action, void *data)
653 {
654 struct imx8m_blk_ctrl *bc = container_of(nb, struct imx8m_blk_ctrl,
655 power_nb);
656
657 if (action != GENPD_NOTIFY_ON && action != GENPD_NOTIFY_PRE_OFF)
658 return NOTIFY_OK;
659
660 /* Enable bus clock and deassert bus reset */
661 regmap_set_bits(bc->regmap, BLK_CLK_EN, BIT(8));
662 regmap_set_bits(bc->regmap, BLK_SFT_RSTN, BIT(8));
663
664 /*
665 * On power up we have no software backchannel to the GPC to
666 * wait for the ADB handshake to happen, so we just delay for a
667 * bit. On power down the GPC driver waits for the handshake.
668 */
669 if (action == GENPD_NOTIFY_ON)
670 udelay(5);
671
672
673 return NOTIFY_OK;
674 }
675
676 static const struct imx8m_blk_ctrl_domain_data imx8mn_disp_blk_ctl_domain_data[] = {
677 [IMX8MN_DISPBLK_PD_MIPI_DSI] = {
678 .name = "dispblk-mipi-dsi",
679 .clk_names = (const char *[]){ "dsi-pclk", "dsi-ref", },
680 .num_clks = 2,
681 .gpc_name = "mipi-dsi",
682 .rst_mask = BIT(0) | BIT(1),
683 .clk_mask = BIT(0) | BIT(1),
684 .mipi_phy_rst_mask = BIT(17),
685 },
686 [IMX8MN_DISPBLK_PD_MIPI_CSI] = {
687 .name = "dispblk-mipi-csi",
688 .clk_names = (const char *[]){ "csi-aclk", "csi-pclk" },
689 .num_clks = 2,
690 .gpc_name = "mipi-csi",
691 .rst_mask = BIT(2) | BIT(3),
692 .clk_mask = BIT(2) | BIT(3),
693 .mipi_phy_rst_mask = BIT(16),
694 },
695 [IMX8MN_DISPBLK_PD_LCDIF] = {
696 .name = "dispblk-lcdif",
697 .clk_names = (const char *[]){ "lcdif-axi", "lcdif-apb", "lcdif-pix", },
698 .num_clks = 3,
699 .gpc_name = "lcdif",
700 .rst_mask = BIT(4) | BIT(5),
701 .clk_mask = BIT(4) | BIT(5),
702 },
703 [IMX8MN_DISPBLK_PD_ISI] = {
704 .name = "dispblk-isi",
705 .clk_names = (const char *[]){ "disp_axi", "disp_apb", "disp_axi_root",
706 "disp_apb_root"},
707 .num_clks = 4,
708 .gpc_name = "isi",
709 .rst_mask = BIT(6) | BIT(7),
710 .clk_mask = BIT(6) | BIT(7),
711 },
712 };
713
714 static const struct imx8m_blk_ctrl_data imx8mn_disp_blk_ctl_dev_data = {
715 .max_reg = 0x84,
716 .power_notifier_fn = imx8mn_disp_power_notifier,
717 .domains = imx8mn_disp_blk_ctl_domain_data,
718 .num_domains = ARRAY_SIZE(imx8mn_disp_blk_ctl_domain_data),
719 };
720
721 #define LCDIF_ARCACHE_CTRL 0x4c
722 #define LCDIF_1_RD_HURRY GENMASK(15, 13)
723 #define LCDIF_0_RD_HURRY GENMASK(12, 10)
724
725 #define ISI_CACHE_CTRL 0x50
726 #define ISI_V_WR_HURRY GENMASK(28, 26)
727 #define ISI_U_WR_HURRY GENMASK(25, 23)
728 #define ISI_Y_WR_HURRY GENMASK(22, 20)
729
730 static int imx8mp_media_power_notifier(struct notifier_block *nb,
731 unsigned long action, void *data)
732 {
733 struct imx8m_blk_ctrl *bc = container_of(nb, struct imx8m_blk_ctrl,
734 power_nb);
735
736 if (action != GENPD_NOTIFY_ON && action != GENPD_NOTIFY_PRE_OFF)
737 return NOTIFY_OK;
738
739 /* Enable bus clock and deassert bus reset */
740 regmap_set_bits(bc->regmap, BLK_CLK_EN, BIT(8));
741 regmap_set_bits(bc->regmap, BLK_SFT_RSTN, BIT(8));
742
743 if (action == GENPD_NOTIFY_ON) {
744 /*
745 * On power up we have no software backchannel to the GPC to
746 * wait for the ADB handshake to happen, so we just delay for a
747 * bit. On power down the GPC driver waits for the handshake.
748 */
749 udelay(5);
750
751 /*
752 * Set panic read hurry level for both LCDIF interfaces to
753 * maximum priority to minimize chances of display FIFO
754 * underflow.
755 */
756 regmap_set_bits(bc->regmap, LCDIF_ARCACHE_CTRL,
757 FIELD_PREP(LCDIF_1_RD_HURRY, 7) |
758 FIELD_PREP(LCDIF_0_RD_HURRY, 7));
759 /* Same here for ISI */
760 regmap_set_bits(bc->regmap, ISI_CACHE_CTRL,
761 FIELD_PREP(ISI_V_WR_HURRY, 7) |
762 FIELD_PREP(ISI_U_WR_HURRY, 7) |
763 FIELD_PREP(ISI_Y_WR_HURRY, 7));
764 }
765
766 return NOTIFY_OK;
767 }
768
769 /*
770 * From i.MX 8M Plus Applications Processor Reference Manual, Rev. 1,
771 * section 13.2.2, 13.2.3
772 * isp-ahb and dwe are not in Figure 13-5. Media BLK_CTRL Clocks
773 */
774 static const struct imx8m_blk_ctrl_domain_data imx8mp_media_blk_ctl_domain_data[] = {
775 [IMX8MP_MEDIABLK_PD_MIPI_DSI_1] = {
776 .name = "mediablk-mipi-dsi-1",
777 .clk_names = (const char *[]){ "apb", "phy", },
778 .num_clks = 2,
779 .gpc_name = "mipi-dsi1",
780 .rst_mask = BIT(0) | BIT(1),
781 .clk_mask = BIT(0) | BIT(1),
782 .mipi_phy_rst_mask = BIT(17),
783 },
784 [IMX8MP_MEDIABLK_PD_MIPI_CSI2_1] = {
785 .name = "mediablk-mipi-csi2-1",
786 .clk_names = (const char *[]){ "apb", "cam1" },
787 .num_clks = 2,
788 .gpc_name = "mipi-csi1",
789 .rst_mask = BIT(2) | BIT(3),
790 .clk_mask = BIT(2) | BIT(3),
791 .mipi_phy_rst_mask = BIT(16),
792 },
793 [IMX8MP_MEDIABLK_PD_LCDIF_1] = {
794 .name = "mediablk-lcdif-1",
795 .clk_names = (const char *[]){ "disp1", "apb", "axi", },
796 .num_clks = 3,
797 .gpc_name = "lcdif1",
798 .rst_mask = BIT(4) | BIT(5) | BIT(23),
799 .clk_mask = BIT(4) | BIT(5) | BIT(23),
800 .path_names = (const char *[]){"lcdif-rd", "lcdif-wr"},
801 .num_paths = 2,
802 },
803 [IMX8MP_MEDIABLK_PD_ISI] = {
804 .name = "mediablk-isi",
805 .clk_names = (const char *[]){ "axi", "apb" },
806 .num_clks = 2,
807 .gpc_name = "isi",
808 .rst_mask = BIT(6) | BIT(7),
809 .clk_mask = BIT(6) | BIT(7),
810 .path_names = (const char *[]){"isi0", "isi1", "isi2"},
811 .num_paths = 3,
812 },
813 [IMX8MP_MEDIABLK_PD_MIPI_CSI2_2] = {
814 .name = "mediablk-mipi-csi2-2",
815 .clk_names = (const char *[]){ "apb", "cam2" },
816 .num_clks = 2,
817 .gpc_name = "mipi-csi2",
818 .rst_mask = BIT(9) | BIT(10),
819 .clk_mask = BIT(9) | BIT(10),
820 .mipi_phy_rst_mask = BIT(30),
821 },
822 [IMX8MP_MEDIABLK_PD_LCDIF_2] = {
823 .name = "mediablk-lcdif-2",
824 .clk_names = (const char *[]){ "disp2", "apb", "axi", },
825 .num_clks = 3,
826 .gpc_name = "lcdif2",
827 .rst_mask = BIT(11) | BIT(12) | BIT(24),
828 .clk_mask = BIT(11) | BIT(12) | BIT(24),
829 .path_names = (const char *[]){"lcdif-rd", "lcdif-wr"},
830 .num_paths = 2,
831 },
832 [IMX8MP_MEDIABLK_PD_ISP] = {
833 .name = "mediablk-isp",
834 .clk_names = (const char *[]){ "isp", "axi", "apb" },
835 .num_clks = 3,
836 .gpc_name = "isp",
837 .rst_mask = BIT(16) | BIT(17) | BIT(18),
838 .clk_mask = BIT(16) | BIT(17) | BIT(18),
839 .path_names = (const char *[]){"isp0", "isp1"},
840 .num_paths = 2,
841 },
842 [IMX8MP_MEDIABLK_PD_DWE] = {
843 .name = "mediablk-dwe",
844 .clk_names = (const char *[]){ "axi", "apb" },
845 .num_clks = 2,
846 .gpc_name = "dwe",
847 .rst_mask = BIT(19) | BIT(20) | BIT(21),
848 .clk_mask = BIT(19) | BIT(20) | BIT(21),
849 .path_names = (const char *[]){"dwe"},
850 .num_paths = 1,
851 },
852 [IMX8MP_MEDIABLK_PD_MIPI_DSI_2] = {
853 .name = "mediablk-mipi-dsi-2",
854 .clk_names = (const char *[]){ "phy", },
855 .num_clks = 1,
856 .gpc_name = "mipi-dsi2",
857 .rst_mask = BIT(22),
858 .clk_mask = BIT(22),
859 .mipi_phy_rst_mask = BIT(29),
860 },
861 };
862
863 static const struct imx8m_blk_ctrl_data imx8mp_media_blk_ctl_dev_data = {
864 .max_reg = 0x138,
865 .power_notifier_fn = imx8mp_media_power_notifier,
866 .domains = imx8mp_media_blk_ctl_domain_data,
867 .num_domains = ARRAY_SIZE(imx8mp_media_blk_ctl_domain_data),
868 };
869
870 static int imx8mq_vpu_power_notifier(struct notifier_block *nb,
871 unsigned long action, void *data)
872 {
873 struct imx8m_blk_ctrl *bc = container_of(nb, struct imx8m_blk_ctrl,
874 power_nb);
875
876 if (action != GENPD_NOTIFY_ON && action != GENPD_NOTIFY_PRE_OFF)
877 return NOTIFY_OK;
878
879 /*
880 * The ADB in the VPUMIX domain has no separate reset and clock
881 * enable bits, but is ungated and reset together with the VPUs. The
882 * reset and clock enable inputs to the ADB is a logical OR of the
883 * VPU bits. In order to set the G2 fuse bits, the G2 clock must
884 * also be enabled.
885 */
886 regmap_set_bits(bc->regmap, BLK_SFT_RSTN, BIT(0) | BIT(1));
887 regmap_set_bits(bc->regmap, BLK_CLK_EN, BIT(0) | BIT(1));
888
889 if (action == GENPD_NOTIFY_ON) {
890 /*
891 * On power up we have no software backchannel to the GPC to
892 * wait for the ADB handshake to happen, so we just delay for a
893 * bit. On power down the GPC driver waits for the handshake.
894 */
895 udelay(5);
896
897 /* set "fuse" bits to enable the VPUs */
898 regmap_set_bits(bc->regmap, 0x8, 0xffffffff);
899 regmap_set_bits(bc->regmap, 0xc, 0xffffffff);
900 regmap_set_bits(bc->regmap, 0x10, 0xffffffff);
901 }
902
903 return NOTIFY_OK;
904 }
905
906 /*
907 * For i.MX8MQ, the ADB in the VPUMIX domain has no separate reset and clock
908 * enable bits, but is ungated and reset together with the VPUs.
909 * Resetting G1 or G2 separately may led to system hang.
910 * Remove the rst_mask and clk_mask from the domain data of G1 and G2,
911 * Let imx8mq_vpu_power_notifier() do really vpu reset.
912 */
913 static const struct imx8m_blk_ctrl_domain_data imx8mq_vpu_blk_ctl_domain_data[] = {
914 [IMX8MQ_VPUBLK_PD_G1] = {
915 .name = "vpublk-g1",
916 .clk_names = (const char *[]){ "g1", },
917 .num_clks = 1,
918 .gpc_name = "g1",
919 },
920 [IMX8MQ_VPUBLK_PD_G2] = {
921 .name = "vpublk-g2",
922 .clk_names = (const char *[]){ "g2", },
923 .num_clks = 1,
924 .gpc_name = "g2",
925 },
926 };
927
928 static const struct imx8m_blk_ctrl_data imx8mq_vpu_blk_ctl_dev_data = {
929 .max_reg = 0x14,
930 .power_notifier_fn = imx8mq_vpu_power_notifier,
931 .domains = imx8mq_vpu_blk_ctl_domain_data,
932 .num_domains = ARRAY_SIZE(imx8mq_vpu_blk_ctl_domain_data),
933 };
934
935 static const struct of_device_id imx8m_blk_ctrl_of_match[] = {
936 {
937 .compatible = "fsl,imx8mm-vpu-blk-ctrl",
938 .data = &imx8mm_vpu_blk_ctl_dev_data
939 }, {
940 .compatible = "fsl,imx8mm-disp-blk-ctrl",
941 .data = &imx8mm_disp_blk_ctl_dev_data
942 }, {
943 .compatible = "fsl,imx8mn-disp-blk-ctrl",
944 .data = &imx8mn_disp_blk_ctl_dev_data
945 }, {
946 .compatible = "fsl,imx8mp-media-blk-ctrl",
947 .data = &imx8mp_media_blk_ctl_dev_data
948 }, {
949 .compatible = "fsl,imx8mq-vpu-blk-ctrl",
950 .data = &imx8mq_vpu_blk_ctl_dev_data
951 }, {
952 .compatible = "fsl,imx8mp-vpu-blk-ctrl",
953 .data = &imx8mp_vpu_blk_ctl_dev_data
954 }, {
955 /* Sentinel */
956 }
957 };
958 MODULE_DEVICE_TABLE(of, imx8m_blk_ctrl_of_match);
959
960 static struct platform_driver imx8m_blk_ctrl_driver = {
961 .probe = imx8m_blk_ctrl_probe,
962 .remove = imx8m_blk_ctrl_remove,
963 .driver = {
964 .name = "imx8m-blk-ctrl",
965 .pm = &imx8m_blk_ctrl_pm_ops,
966 .of_match_table = imx8m_blk_ctrl_of_match,
967 .suppress_bind_attrs = true,
968 },
969 };
970 module_platform_driver(imx8m_blk_ctrl_driver);
971 MODULE_DESCRIPTION("NXP i.MX8M power domain driver");
972 MODULE_LICENSE("GPL");