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