xref: /linux/drivers/gpu/drm/tidss/tidss_drv.c (revision 52e6b198833411564e0b9ce6e96bbd3d72f961e7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
4  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
5  */
6 
7 #include <linux/console.h>
8 #include <linux/of.h>
9 #include <linux/module.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/aperture.h>
12 
13 #include <drm/clients/drm_client_setup.h>
14 #include <drm/drm_atomic.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_drv.h>
18 #include <drm/drm_fbdev_dma.h>
19 #include <drm/drm_gem_dma_helper.h>
20 #include <drm/drm_managed.h>
21 #include <drm/drm_module.h>
22 #include <drm/drm_probe_helper.h>
23 
24 #include "tidss_dispc.h"
25 #include "tidss_drv.h"
26 #include "tidss_kms.h"
27 #include "tidss_irq.h"
28 #include "tidss_oldi.h"
29 
30 /* Power management */
31 
32 int tidss_runtime_get(struct tidss_device *tidss)
33 {
34 	int r;
35 
36 	r = pm_runtime_resume_and_get(tidss->dev);
37 	WARN_ON(r < 0);
38 	return r;
39 }
40 
41 void tidss_runtime_put(struct tidss_device *tidss)
42 {
43 	int r;
44 
45 	pm_runtime_mark_last_busy(tidss->dev);
46 
47 	r = pm_runtime_put_autosuspend(tidss->dev);
48 	WARN_ON(r < 0);
49 }
50 
51 static int __maybe_unused tidss_pm_runtime_suspend(struct device *dev)
52 {
53 	struct tidss_device *tidss = dev_get_drvdata(dev);
54 
55 	return dispc_runtime_suspend(tidss->dispc);
56 }
57 
58 static int __maybe_unused tidss_pm_runtime_resume(struct device *dev)
59 {
60 	struct tidss_device *tidss = dev_get_drvdata(dev);
61 	int r;
62 
63 	r = dispc_runtime_resume(tidss->dispc);
64 	if (r)
65 		return r;
66 
67 	return 0;
68 }
69 
70 static int __maybe_unused tidss_suspend(struct device *dev)
71 {
72 	struct tidss_device *tidss = dev_get_drvdata(dev);
73 
74 	return drm_mode_config_helper_suspend(&tidss->ddev);
75 }
76 
77 static int __maybe_unused tidss_resume(struct device *dev)
78 {
79 	struct tidss_device *tidss = dev_get_drvdata(dev);
80 
81 	return drm_mode_config_helper_resume(&tidss->ddev);
82 }
83 
84 static __maybe_unused const struct dev_pm_ops tidss_pm_ops = {
85 	SET_SYSTEM_SLEEP_PM_OPS(tidss_suspend, tidss_resume)
86 	SET_RUNTIME_PM_OPS(tidss_pm_runtime_suspend, tidss_pm_runtime_resume, NULL)
87 };
88 
89 /* DRM device Information */
90 
91 static void tidss_release(struct drm_device *ddev)
92 {
93 	drm_kms_helper_poll_fini(ddev);
94 }
95 
96 DEFINE_DRM_GEM_DMA_FOPS(tidss_fops);
97 
98 static const struct drm_driver tidss_driver = {
99 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
100 	.fops			= &tidss_fops,
101 	.release		= tidss_release,
102 	DRM_GEM_DMA_DRIVER_OPS_VMAP,
103 	DRM_FBDEV_DMA_DRIVER_OPS,
104 	.name			= "tidss",
105 	.desc			= "TI Keystone DSS",
106 	.major			= 1,
107 	.minor			= 0,
108 };
109 
110 static int tidss_probe(struct platform_device *pdev)
111 {
112 	struct device *dev = &pdev->dev;
113 	struct tidss_device *tidss;
114 	struct drm_device *ddev;
115 	int ret;
116 	int irq;
117 
118 	tidss = devm_drm_dev_alloc(&pdev->dev, &tidss_driver,
119 				   struct tidss_device, ddev);
120 	if (IS_ERR(tidss))
121 		return PTR_ERR(tidss);
122 
123 	ddev = &tidss->ddev;
124 
125 	tidss->dev = dev;
126 	tidss->feat = of_device_get_match_data(dev);
127 
128 	platform_set_drvdata(pdev, tidss);
129 
130 	spin_lock_init(&tidss->irq_lock);
131 
132 	ret = dispc_init(tidss);
133 	if (ret) {
134 		dev_err(dev, "failed to initialize dispc: %d\n", ret);
135 		return ret;
136 	}
137 
138 	ret = tidss_oldi_init(tidss);
139 	if (ret)
140 		return dev_err_probe(dev, ret, "failed to init OLDI\n");
141 
142 	pm_runtime_enable(dev);
143 
144 	pm_runtime_set_autosuspend_delay(dev, 1000);
145 	pm_runtime_use_autosuspend(dev);
146 
147 #ifndef CONFIG_PM
148 	/* If we don't have PM, we need to call resume manually */
149 	dispc_runtime_resume(tidss->dispc);
150 #endif
151 
152 	ret = tidss_modeset_init(tidss);
153 	if (ret < 0) {
154 		if (ret != -EPROBE_DEFER)
155 			dev_err(dev, "failed to init DRM/KMS (%d)\n", ret);
156 		goto err_runtime_suspend;
157 	}
158 
159 	irq = platform_get_irq(pdev, 0);
160 	if (irq < 0) {
161 		ret = irq;
162 		goto err_runtime_suspend;
163 	}
164 	tidss->irq = irq;
165 
166 	ret = tidss_irq_install(ddev, irq);
167 	if (ret) {
168 		dev_err(dev, "tidss_irq_install failed: %d\n", ret);
169 		goto err_runtime_suspend;
170 	}
171 
172 	drm_kms_helper_poll_init(ddev);
173 
174 	drm_mode_config_reset(ddev);
175 
176 	ret = drm_dev_register(ddev, 0);
177 	if (ret) {
178 		dev_err(dev, "failed to register DRM device\n");
179 		goto err_irq_uninstall;
180 	}
181 
182 	/* Remove possible early fb before setting up the fbdev */
183 	ret = aperture_remove_all_conflicting_devices(tidss_driver.name);
184 	if (ret)
185 		goto err_drm_dev_unreg;
186 
187 	drm_client_setup(ddev, NULL);
188 
189 	dev_dbg(dev, "%s done\n", __func__);
190 
191 	return 0;
192 
193 err_drm_dev_unreg:
194 	drm_dev_unregister(ddev);
195 
196 err_irq_uninstall:
197 	tidss_irq_uninstall(ddev);
198 
199 err_runtime_suspend:
200 #ifndef CONFIG_PM
201 	dispc_runtime_suspend(tidss->dispc);
202 #endif
203 	pm_runtime_dont_use_autosuspend(dev);
204 	pm_runtime_disable(dev);
205 
206 	tidss_oldi_deinit(tidss);
207 
208 	return ret;
209 }
210 
211 static void tidss_remove(struct platform_device *pdev)
212 {
213 	struct device *dev = &pdev->dev;
214 	struct tidss_device *tidss = platform_get_drvdata(pdev);
215 	struct drm_device *ddev = &tidss->ddev;
216 
217 	drm_dev_unregister(ddev);
218 
219 	drm_atomic_helper_shutdown(ddev);
220 
221 	tidss_irq_uninstall(ddev);
222 
223 #ifndef CONFIG_PM
224 	/* If we don't have PM, we need to call suspend manually */
225 	dispc_runtime_suspend(tidss->dispc);
226 #endif
227 	pm_runtime_dont_use_autosuspend(dev);
228 	pm_runtime_disable(dev);
229 
230 	tidss_oldi_deinit(tidss);
231 
232 	/* devm allocated dispc goes away with the dev so mark it NULL */
233 	dispc_remove(tidss);
234 
235 	dev_dbg(dev, "%s done\n", __func__);
236 }
237 
238 static void tidss_shutdown(struct platform_device *pdev)
239 {
240 	drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
241 }
242 
243 static const struct of_device_id tidss_of_table[] = {
244 	{ .compatible = "ti,k2g-dss", .data = &dispc_k2g_feats, },
245 	{ .compatible = "ti,am625-dss", .data = &dispc_am625_feats, },
246 	{ .compatible = "ti,am62a7-dss", .data = &dispc_am62a7_feats, },
247 	{ .compatible = "ti,am62l-dss", .data = &dispc_am62l_feats, },
248 	{ .compatible = "ti,am65x-dss", .data = &dispc_am65x_feats, },
249 	{ .compatible = "ti,j721e-dss", .data = &dispc_j721e_feats, },
250 	{ }
251 };
252 
253 MODULE_DEVICE_TABLE(of, tidss_of_table);
254 
255 static struct platform_driver tidss_platform_driver = {
256 	.probe		= tidss_probe,
257 	.remove		= tidss_remove,
258 	.shutdown	= tidss_shutdown,
259 	.driver		= {
260 		.name	= "tidss",
261 		.pm	= pm_ptr(&tidss_pm_ops),
262 		.of_match_table = tidss_of_table,
263 		.suppress_bind_attrs = true,
264 	},
265 };
266 
267 drm_module_platform_driver(tidss_platform_driver);
268 
269 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
270 MODULE_DESCRIPTION("TI Keystone DSS Driver");
271 MODULE_LICENSE("GPL v2");
272