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
29int 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
40void 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
50static 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
59static 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
73static 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
82static 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
91static __maybe_unused const struct dev_pm_ops tidss_pm_ops = {
92	SET_SYSTEM_SLEEP_PM_OPS(tidss_suspend, tidss_resume)
93	SET_RUNTIME_PM_OPS(tidss_pm_runtime_suspend, tidss_pm_runtime_resume, NULL)
94};
95
96/* DRM device Information */
97
98static void tidss_release(struct drm_device *ddev)
99{
100	drm_kms_helper_poll_fini(ddev);
101}
102
103DEFINE_DRM_GEM_DMA_FOPS(tidss_fops);
104
105static const struct drm_driver tidss_driver = {
106	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
107	.fops			= &tidss_fops,
108	.release		= tidss_release,
109	DRM_GEM_DMA_DRIVER_OPS_VMAP,
110	.name			= "tidss",
111	.desc			= "TI Keystone DSS",
112	.date			= "20180215",
113	.major			= 1,
114	.minor			= 0,
115};
116
117static int tidss_probe(struct platform_device *pdev)
118{
119	struct device *dev = &pdev->dev;
120	struct tidss_device *tidss;
121	struct drm_device *ddev;
122	int ret;
123	int irq;
124
125	dev_dbg(dev, "%s\n", __func__);
126
127	tidss = devm_drm_dev_alloc(&pdev->dev, &tidss_driver,
128				   struct tidss_device, ddev);
129	if (IS_ERR(tidss))
130		return PTR_ERR(tidss);
131
132	ddev = &tidss->ddev;
133
134	tidss->dev = dev;
135	tidss->feat = of_device_get_match_data(dev);
136
137	platform_set_drvdata(pdev, tidss);
138
139	ret = dispc_init(tidss);
140	if (ret) {
141		dev_err(dev, "failed to initialize dispc: %d\n", ret);
142		return ret;
143	}
144
145	pm_runtime_enable(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	drm_fbdev_dma_setup(ddev, 32);
183
184	dev_dbg(dev, "%s done\n", __func__);
185
186	return 0;
187
188err_irq_uninstall:
189	tidss_irq_uninstall(ddev);
190
191err_runtime_suspend:
192#ifndef CONFIG_PM
193	dispc_runtime_suspend(tidss->dispc);
194#endif
195	pm_runtime_disable(dev);
196
197	return ret;
198}
199
200static void tidss_remove(struct platform_device *pdev)
201{
202	struct device *dev = &pdev->dev;
203	struct tidss_device *tidss = platform_get_drvdata(pdev);
204	struct drm_device *ddev = &tidss->ddev;
205
206	dev_dbg(dev, "%s\n", __func__);
207
208	drm_dev_unregister(ddev);
209
210	drm_atomic_helper_shutdown(ddev);
211
212	tidss_irq_uninstall(ddev);
213
214#ifndef CONFIG_PM
215	/* If we don't have PM, we need to call suspend manually */
216	dispc_runtime_suspend(tidss->dispc);
217#endif
218	pm_runtime_disable(dev);
219
220	/* devm allocated dispc goes away with the dev so mark it NULL */
221	dispc_remove(tidss);
222
223	dev_dbg(dev, "%s done\n", __func__);
224}
225
226static void tidss_shutdown(struct platform_device *pdev)
227{
228	drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
229}
230
231static const struct of_device_id tidss_of_table[] = {
232	{ .compatible = "ti,k2g-dss", .data = &dispc_k2g_feats, },
233	{ .compatible = "ti,am625-dss", .data = &dispc_am625_feats, },
234	{ .compatible = "ti,am65x-dss", .data = &dispc_am65x_feats, },
235	{ .compatible = "ti,j721e-dss", .data = &dispc_j721e_feats, },
236	{ }
237};
238
239MODULE_DEVICE_TABLE(of, tidss_of_table);
240
241static struct platform_driver tidss_platform_driver = {
242	.probe		= tidss_probe,
243	.remove_new	= tidss_remove,
244	.shutdown	= tidss_shutdown,
245	.driver		= {
246		.name	= "tidss",
247		.pm	= pm_ptr(&tidss_pm_ops),
248		.of_match_table = tidss_of_table,
249		.suppress_bind_attrs = true,
250	},
251};
252
253drm_module_platform_driver(tidss_platform_driver);
254
255MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
256MODULE_DESCRIPTION("TI Keystone DSS Driver");
257MODULE_LICENSE("GPL v2");
258