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