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