From e98c3502bd334b79e88cac2b2747117ffe600d48 Mon Sep 17 00:00:00 2001
From: Nate Drude <nate.d@variscite.com>
Date: Mon, 3 Oct 2022 16:09:16 -0500
Subject: [PATCH] MLK-26052 drm/imx: sec_mipi_dsim-imx: release resources after
 deferral

The imx_sec_dsim_probe may be deferred (e.g. when uising ti,sn65dsi84).
After a deferral, of_reset_control_array_get will fail
because of the call in the previous imx_sec_dsim_probe.

To fix this, unwind by doing the following when component_add is deferred:
- Release reset controls by calling sec_dsim_of_put_resets
- Disable PM by calling pm_runtime_disable.

Tested using ti,sn65dsi84 on the following Variscite configurations:
- VAR-SOM-MX8M-NANO v1.1 + Symphony v1.6
- DART-MX8M-MINI v1.1 + DT8MCustomBoard v3.0

Fixes the following:

[    1.929043] imx_sec_dsim_drv 32e10000.dsi_controller: version number is 0x1060200
[    1.936603] [drm:drm_bridge_attach] *ERROR* failed to attach bridge /soc@0/bus@32c00000/dsi_controller@32e10000 to encoder DSI-34: -517
[    1.948815] imx_sec_dsim_drv 32e10000.dsi_controller: Failed to attach bridge: 32e10000.dsi_controller
[    1.958137] imx_sec_dsim_drv 32e10000.dsi_controller: failed to bind sec dsim bridge: -517
[    1.972737] pps pps0: new PPS source ptp0
[    2.083716] vddio: Bringing 1500000uV into 1800000-1800000uV
[    2.090807] fec 30be0000.ethernet eth0: registered PHC device 0
[    2.103846] imx-cpufreq-dt imx-cpufreq-dt: cpu speed grade 8 mkt segment 0 supported-hw 0x100 0x1
[    2.113964] Hot alarm is canceled. GPU3D clock will return to 64/64
[    2.123475] sdhci-esdhc-imx 30b50000.mmc: Got CD GPIO
[    2.128789] mxc-mipi-csi2-sam 32e30000.csi: supply mipi-phy not found, using dummy regulator
[    2.138023] mxc-mipi-csi2-sam 32e30000.csi: lanes: 2, hs_settle: 13, clk_settle: 2, wclk: 1, freq: 333000000
[    2.150900] ------------[ cut here ]------------
[    2.155536] WARNING: CPU: 2 PID: 8 at drivers/reset/core.c:765 __reset_control_get_internal+0x68/0x160
[    2.164859] Modules linked in:
[    2.165181] mmc1: SDHCI controller on 30b50000.mmc [30b50000.mmc] using ADMA
[    2.167919] CPU: 2 PID: 8 Comm: kworker/u8:0 Not tainted 5.15.60-05940-gd9b705ee8975 #28
[    2.167926] Hardware name: Variscite VAR-SOM-MX8M-NANO on Symphony-Board (DT)
[    2.167932] Workqueue: events_unbound deferred_probe_work_func
[    2.167944] pstate: 20000005 (nzCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    2.202981] pc : __reset_control_get_internal+0x68/0x160
[    2.208295] lr : __of_reset_control_get+0x16c/0x1d0
[    2.213176] sp : ffff800009df3990
[    2.216488] x29: ffff800009df3990 x28: ffff000017dd1e80 x27: ffff00000516fb38
[    2.223629] x26: 0000000000000001 x25: 0000000000000000 x24: 0000000000000001
[    2.230767] x23: 0000000000000000 x22: ffff000004982880 x21: 0000000000000001
[    2.237907] x20: ffff0000049828a0 x19: ffff00000545bd80 x18: ffffffffffffffff
[    2.245046] x17: 7465735f6b6c6320 x16: 2c3331203a656c74 x15: ffff00000516f78a
[    2.252186] x14: ffffffffffffffff x13: 0000000000000018 x12: 0101010101010101
[    2.259326] x11: 0000000000000030 x10: 0101010101010101 x9 : 0000000000000000
[    2.266465] x8 : 7f7f7f7f7f7f7f7f x7 : 70ff726b6b64622c x6 : 0000000000008072
[    2.273604] x5 : fffffbfffdc01818 x4 : 0000000000000000 x3 : 0000000000000001
[    2.280743] x2 : 0000000000000001 x1 : 0000000000000001 x0 : 0000000000000000
[    2.287884] Call trace:
[    2.290329]  __reset_control_get_internal+0x68/0x160
[    2.295296]  __of_reset_control_get+0x16c/0x1d0
[    2.299828]  of_reset_control_array_get+0xac/0x230
[    2.304622]  imx_sec_dsim_probe+0x21c/0x30c
[    2.308809]  platform_probe+0x68/0xe0
[    2.312475]  really_probe.part.0+0x9c/0x30c
[    2.316660]  __driver_probe_device+0x98/0x144
[    2.321018]  driver_probe_device+0xc8/0x15c
[    2.325202]  __device_attach_driver+0xb4/0x120
[    2.329645]  bus_for_each_drv+0x78/0xd0
[    2.333483]  __device_attach+0xa8/0x1c0
[    2.337320]  device_initial_probe+0x14/0x20
[    2.341505]  bus_probe_device+0x9c/0xa4
[    2.345342]  deferred_probe_work_func+0x80/0xc0
[    2.349874]  process_one_work+0x1d0/0x354
[    2.353890]  worker_thread+0x2c0/0x470
[    2.357641]  kthread+0x150/0x160
[    2.360871]  ret_from_fork+0x10/0x20
[    2.364451] ---[ end trace 60172b2d7c2580e5 ]---
[    2.369212] ------------[ cut here ]------------
[    2.373828] WARNING: CPU: 2 PID: 8 at drivers/reset/core.c:765 __reset_control_get_internal+0x68/0x160
[    2.383141] Modules linked in:
[    2.386198] CPU: 2 PID: 8 Comm: kworker/u8:0 Tainted: G        W         5.15.60-05940-gd9b705ee8975 #28
[    2.395677] Hardware name: Variscite VAR-SOM-MX8M-NANO on Symphony-Board (DT)
[    2.402811] Workqueue: events_unbound deferred_probe_work_func
[    2.408648] pstate: 20000005 (nzCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    2.415610] pc : __reset_control_get_internal+0x68/0x160
[    2.420924] lr : __of_reset_control_get+0x16c/0x1d0
[    2.425803] sp : ffff800009df3990
[    2.429114] x29: ffff800009df3990 x28: ffff000017dd20a8 x27: ffff00000516fb38
[    2.436255] x26: 0000000000000001 x25: 0000000000000000 x24: 0000000000000001
[    2.443393] x23: 0000000000000000 x22: ffff000004982f80 x21: 0000000000000001
[    2.450533] x20: ffff000004982fa0 x19: ffff00000545bf00 x18: ffffffffffffffff
[    2.457672] x17: 7465735f6b6c6320 x16: 2c3331203a656c74 x15: ffff00000516f78a
[    2.464812] x14: ffffffffffffffff x13: 0000000000000018 x12: 0101010101010101
[    2.471952] x11: 0000000000000030 x10: 0101010101010101 x9 : 0000000000000000
[    2.479091] x8 : 7f7f7f7f7f7f7f7f x7 : 70ff726b6b64622c x6 : 0000000000008072
[    2.486230] x5 : fffffbfffdc01868 x4 : 0000000000000000 x3 : 0000000000000001
[    2.493369] x2 : 0000000000000001 x1 : 0000000000000001 x0 : 0000000000000000
[    2.500509] Call trace:
[    2.502954]  __reset_control_get_internal+0x68/0x160
[    2.507920]  __of_reset_control_get+0x16c/0x1d0
[    2.512452]  of_reset_control_array_get+0xac/0x230
[    2.517245]  imx_sec_dsim_probe+0x21c/0x30c
[    2.521433]  platform_probe+0x68/0xe0
[    2.525097]  really_probe.part.0+0x9c/0x30c
[    2.529281]  __driver_probe_device+0x98/0x144
[    2.533640]  driver_probe_device+0xc8/0x15c
[    2.537824]  __device_attach_driver+0xb4/0x120
[    2.542269]  bus_for_each_drv+0x78/0xd0
[    2.546107]  __device_attach+0xa8/0x1c0
[    2.549945]  device_initial_probe+0x14/0x20
[    2.554129]  bus_probe_device+0x9c/0xa4
[    2.557965]  deferred_probe_work_func+0x80/0xc0
[    2.562497]  process_one_work+0x1d0/0x354
[    2.566509]  worker_thread+0x2c0/0x470
[    2.570260]  kthread+0x150/0x160
[    2.573490]  ret_from_fork+0x10/0x20
[    2.577067] ---[ end trace 60172b2d7c2580e6 ]---
[    2.581883] ------------[ cut here ]------------
[    2.586499] WARNING: CPU: 2 PID: 8 at drivers/reset/core.c:765 __reset_control_get_internal+0x68/0x160
[    2.595808] Modules linked in:
[    2.598862] CPU: 2 PID: 8 Comm: kworker/u8:0 Tainted: G        W         5.15.60-05940-gd9b705ee8975 #28
[    2.608341] Hardware name: Variscite VAR-SOM-MX8M-NANO on Symphony-Board (DT)
[    2.615474] Workqueue: events_unbound deferred_probe_work_func
[    2.621309] pstate: 20000005 (nzCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[    2.628271] pc : __reset_control_get_internal+0x68/0x160
[    2.633584] lr : __of_reset_control_get+0x16c/0x1d0
[    2.638464] sp : ffff800009df3990
[    2.641775] x29: ffff800009df3990 x28: ffff000017dd22d0 x27: ffff00000516fb38
[    2.648915] x26: 0000000000000001 x25: 0000000000000000 x24: 0000000000000001
[    2.656053] x23: 0000000000000000 x22: ffff000004983680 x21: 0000000000000001
[    2.663192] x20: ffff0000049836a0 x19: ffff000004909500 x18: ffffffffffffffff
[    2.670331] x17: 7465735f6b6c6320 x16: 2c3331203a656c74 x15: ffff00000516f78a
[    2.677469] x14: ffffffffffffffff x13: 0000000000000018 x12: 0101010101010101
[    2.684609] x11: 0000000000000030 x10: 0101010101010101 x9 : 0000000000000000
[    2.691747] x8 : 7f7f7f7f7f7f7f7f x7 : 70ff726b6b64622c x6 : 0000000000008072
[    2.698886] x5 : fffffbfffdc018b8 x4 : 0000000000000000 x3 : 0000000000000001
[    2.706024] x2 : 0000000000000001 x1 : 0000000000000001 x0 : 0000000000000000
[    2.713162] Call trace:
[    2.715606]  __reset_control_get_internal+0x68/0x160
[    2.720572]  __of_reset_control_get+0x16c/0x1d0
[    2.725103]  of_reset_control_array_get+0xac/0x230
[    2.729895]  imx_sec_dsim_probe+0x21c/0x30c
[    2.734080]  platform_probe+0x68/0xe0
[    2.737744]  really_probe.part.0+0x9c/0x30c
[    2.741928]  __driver_probe_device+0x98/0x144
[    2.746285]  driver_probe_device+0xc8/0x15c
[    2.750469]  __device_attach_driver+0xb4/0x120
[    2.754914]  bus_for_each_drv+0x78/0xd0
[    2.758750]  __device_attach+0xa8/0x1c0
[    2.762586]  device_initial_probe+0x14/0x20
[    2.766771]  bus_probe_device+0x9c/0xa4
[    2.770606]  deferred_probe_work_func+0x80/0xc0
[    2.775137]  process_one_work+0x1d0/0x354
[    2.779149]  worker_thread+0x2c0/0x470
[    2.782899]  kthread+0x150/0x160
[    2.786127]  ret_from_fork+0x10/0x20
[    2.789703] ---[ end trace 60172b2d7c2580e7 ]---
[    2.794359] imx_sec_dsim_drv 32e10000.dsi_controller: no invalid reset control exists
[    2.802553] imx_sec_dsim_drv: probe of 32e10000.dsi_controller failed with error -22

Signed-off-by: Nate Drude <nate.d@variscite.com>
Reviewed-by: Liu Ying <victor.liu@nxp.com>
[ Liu Ying: Add JIRA ticket number and use dev_err_probe() ]
Signed-off-by: Liu Ying <victor.liu@nxp.com>
---
 drivers/gpu/drm/imx/sec_mipi_dsim-imx.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/imx/sec_mipi_dsim-imx.c b/drivers/gpu/drm/imx/sec_mipi_dsim-imx.c
index cd80096477b0..4542b1ddc219 100644
--- a/drivers/gpu/drm/imx/sec_mipi_dsim-imx.c
+++ b/drivers/gpu/drm/imx/sec_mipi_dsim-imx.c
@@ -456,7 +456,14 @@ static int imx_sec_dsim_probe(struct platform_device *pdev)
 
 	pm_runtime_enable(dev);
 
-	return component_add(dev, &imx_sec_dsim_ops);
+	ret = component_add(dev, &imx_sec_dsim_ops);
+	if (ret) {
+		pm_runtime_disable(dev);
+		sec_dsim_of_put_resets(dsim_dev);
+		dev_err_probe(dev, ret, "Failed to add component\n");
+	}
+
+	return ret;
 }
 
 static int imx_sec_dsim_remove(struct platform_device *pdev)
-- 
2.25.1

