summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/tidss/tidss_drv.c
blob: fff3883a355a39686afff911c411bdfc57c6625b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2018 Texas Instruments Incorporated - https://www.ti.com/
 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
 */

#include <linux/console.h>
#include <linux/of_device.h>
#include <linux/module.h>
#include <linux/pm_runtime.h>
#include <linux/pm_domain.h>

#include <drm/drm_aperture.h>
#include <drm/drm_atomic.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_crtc.h>
#include <drm/drm_crtc_helper.h>
#include <drm/drm_drv.h>
#include <drm/drm_fb_helper.h>
#include <drm/drm_gem_dma_helper.h>
#include <drm/drm_managed.h>
#include <drm/drm_module.h>
#include <drm/drm_probe_helper.h>

#include "tidss_dispc.h"
#include "tidss_drv.h"
#include "tidss_kms.h"
#include "tidss_irq.h"

/* Power management */

int tidss_runtime_get(struct tidss_device *tidss)
{
	int r;

	dev_dbg(tidss->dev, "%s\n", __func__);

	/* No PM in display sharing mode */
	if (tidss->shared_mode)
		return 0;

	r = pm_runtime_resume_and_get(tidss->dev);
	if (WARN_ON(r < 0))
		return r;

	if (tidss->boot_enabled_vp_mask) {
		/*
		 * If 'boot_enabled_vp_mask' is set, it means that the DSS is
		 * enabled and bootloader splash-screen is still on the screen,
		 * using bootloader's DSS HW config.
		 *
		 * This is the first time the driver is about to use the HW, and
		 * we need to do some cleanup and initial setup.
		 */
		dispc_splash_fini(tidss->dispc);
	}

	return 0;
}

void tidss_runtime_put(struct tidss_device *tidss)
{
	int r;

	dev_dbg(tidss->dev, "%s\n", __func__);

	if (tidss->shared_mode)
		return;

	pm_runtime_mark_last_busy(tidss->dev);

	r = pm_runtime_put_autosuspend(tidss->dev);
	WARN_ON(r < 0);
}

static int __maybe_unused tidss_pm_runtime_suspend(struct device *dev)
{
	struct tidss_device *tidss = dev_get_drvdata(dev);

	dev_dbg(dev, "%s\n", __func__);

	return dispc_runtime_suspend(tidss->dispc);
}

static int __maybe_unused tidss_pm_runtime_resume(struct device *dev)
{
	struct tidss_device *tidss = dev_get_drvdata(dev);
	int r;

	dev_dbg(dev, "%s\n", __func__);

	r = dispc_runtime_resume(tidss->dispc);
	if (r)
		return r;

	return 0;
}

static int __maybe_unused tidss_suspend(struct device *dev)
{
	struct tidss_device *tidss = dev_get_drvdata(dev);

	dev_dbg(dev, "%s\n", __func__);

	return drm_mode_config_helper_suspend(&tidss->ddev);
}

static int __maybe_unused tidss_resume(struct device *dev)
{
	struct tidss_device *tidss = dev_get_drvdata(dev);

	dev_dbg(dev, "%s\n", __func__);

	return drm_mode_config_helper_resume(&tidss->ddev);
}

static __maybe_unused const struct dev_pm_ops tidss_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(tidss_suspend, tidss_resume)
	SET_RUNTIME_PM_OPS(tidss_pm_runtime_suspend, tidss_pm_runtime_resume, NULL)
};

/* DRM device Information */

static void tidss_release(struct drm_device *ddev)
{
	drm_kms_helper_poll_fini(ddev);
}

DEFINE_DRM_GEM_DMA_FOPS(tidss_fops);

static const struct drm_driver tidss_driver = {
	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
	.fops			= &tidss_fops,
	.release		= tidss_release,
	DRM_GEM_DMA_DRIVER_OPS_VMAP,
	.name			= "tidss",
	.desc			= "TI Keystone DSS",
	.date			= "20180215",
	.major			= 1,
	.minor			= 0,
};

static int tidss_detach_pm_domains(struct tidss_device *tidss)
{
	int i;

	if (tidss->num_domains <= 1)
		return 0;

	for (i = 0; i < tidss->num_domains; i++) {
		if (tidss->pd_link[i] && !IS_ERR(tidss->pd_link[i]))
			device_link_del(tidss->pd_link[i]);
		if (tidss->pd_dev[i] && !IS_ERR(tidss->pd_dev[i]))
			dev_pm_domain_detach(tidss->pd_dev[i], true);
		tidss->pd_dev[i] = NULL;
		tidss->pd_link[i] = NULL;
	}

	return 0;
}

static int tidss_attach_pm_domains(struct tidss_device *tidss)
{
	struct device *dev = tidss->dev;
	int i;
	int ret;
	struct platform_device *pdev = to_platform_device(dev);
	struct device_node *np = pdev->dev.of_node;

	tidss->num_domains = of_count_phandle_with_args(np, "power-domains",
							"#power-domain-cells");
	if (tidss->num_domains <= 1) {
		dev_dbg(dev, "One or less power domains, no need to do attach domains\n");
		return 0;
	}

	tidss->pd_dev = devm_kmalloc_array(dev, tidss->num_domains,
					   sizeof(*tidss->pd_dev), GFP_KERNEL);
	if (!tidss->pd_dev)
		return -ENOMEM;

	tidss->pd_link = devm_kmalloc_array(dev, tidss->num_domains,
					    sizeof(*tidss->pd_link), GFP_KERNEL);
	if (!tidss->pd_link)
		return -ENOMEM;

	for (i = 0; i < tidss->num_domains; i++) {
		tidss->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i);
		if (IS_ERR(tidss->pd_dev[i])) {
			ret = PTR_ERR(tidss->pd_dev[i]);
			goto fail;
		}

		tidss->pd_link[i] = device_link_add(dev, tidss->pd_dev[i],
						    DL_FLAG_STATELESS |
						    DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE);
		if (!tidss->pd_link[i]) {
			ret = -EINVAL;
			goto fail;
		}
	}

	return 0;
fail:
	tidss_detach_pm_domains(tidss);
	return ret;
}

static void check_for_simplefb_device(struct tidss_device *tidss)
{
	if (IS_ENABLED(CONFIG_FB_SIMPLE)) {
		struct device *simplefb_dev;
		struct device_node *simplefb_node;

		simplefb_node = of_find_compatible_node(NULL, NULL, "simple-framebuffer");
		if (!simplefb_node)
			return;

		simplefb_dev = bus_find_device_by_of_node(&platform_bus_type, simplefb_node);
		if (!simplefb_dev) {
			of_node_put(simplefb_node);
			return;
		}

		tidss->simplefb_enabled = true;
		dev_dbg(tidss->dev, "simple-framebuffer detected\n");
		put_device(simplefb_dev);
		of_node_put(simplefb_node);
	}
}

static int tidss_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct tidss_device *tidss;
	struct drm_device *ddev;
	int ret;
	int irq;

	dev_dbg(dev, "%s\n", __func__);

	tidss = devm_drm_dev_alloc(&pdev->dev, &tidss_driver,
				   struct tidss_device, ddev);
	if (IS_ERR(tidss))
		return PTR_ERR(tidss);

	ddev = &tidss->ddev;

	tidss->dev = dev;
	tidss->feat = of_device_get_match_data(dev);

	platform_set_drvdata(pdev, tidss);

	spin_lock_init(&tidss->wait_lock);

	check_for_simplefb_device(tidss);
	tidss->shared_mode = device_property_read_bool(dev, "ti,dss-shared-mode");

	/* powering up associated OLDI domains */
	if (!tidss->shared_mode) {
		ret = tidss_attach_pm_domains(tidss);
		if (ret < 0) {
			dev_err(dev, "failed to attach power domains %d\n", ret);
			return ret;
		}
	}

	ret = dispc_init(tidss);
	if (ret) {
		dev_err(dev, "failed to initialize dispc: %d\n", ret);
		return ret;
	}

	if (!tidss->shared_mode) {
		pm_runtime_enable(dev);
		pm_runtime_set_autosuspend_delay(dev, 1000);
		pm_runtime_use_autosuspend(dev);

#ifndef CONFIG_PM
		/* If we don't have PM, we need to call resume manually */
		dispc_runtime_resume(tidss->dispc);
#endif
	}

	ret = tidss_modeset_init(tidss);
	if (ret < 0) {
		if (ret != -EPROBE_DEFER)
			dev_err(dev, "failed to init DRM/KMS (%d)\n", ret);
		goto err_runtime_suspend;
	}

	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		ret = irq;
		goto err_runtime_suspend;
	}
	tidss->irq = irq;

	ret = tidss_irq_install(ddev, irq);
	if (ret) {
		dev_err(dev, "tidss_irq_install failed: %d\n", ret);
		goto err_runtime_suspend;
	}

	drm_kms_helper_poll_init(ddev);

	drm_mode_config_reset(ddev);

	ret = drm_dev_register(ddev, 0);
	if (ret) {
		dev_err(dev, "failed to register DRM device\n");
		goto err_irq_uninstall;
	}

	/* Remove possible early fb before setting up the fbdev */
	ret = drm_aperture_remove_framebuffers(&tidss_driver);
	if (ret)
		goto err_drm_dev_unreg;

	drm_fbdev_generic_setup(ddev, 32);

	dev_dbg(dev, "%s done\n", __func__);

	return 0;

err_drm_dev_unreg:
	drm_dev_unregister(ddev);

err_irq_uninstall:
	tidss_irq_uninstall(ddev);

err_runtime_suspend:
	if (tidss->shared_mode)
		return ret;
#ifndef CONFIG_PM
	dispc_runtime_suspend(tidss->dispc);
#endif
	pm_runtime_dont_use_autosuspend(dev);
	pm_runtime_disable(dev);
	tidss_detach_pm_domains(tidss);

	return ret;
}

static int tidss_remove(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct tidss_device *tidss = platform_get_drvdata(pdev);
	struct drm_device *ddev = &tidss->ddev;

	dev_dbg(dev, "%s\n", __func__);

	drm_dev_unregister(ddev);

	drm_atomic_helper_shutdown(ddev);

	tidss_irq_uninstall(ddev);

	if (!tidss->shared_mode) {
#ifndef CONFIG_PM
		/* If we don't have PM, we need to call suspend manually */
		dispc_runtime_suspend(tidss->dispc);
#endif
		pm_runtime_dont_use_autosuspend(dev);
		pm_runtime_disable(dev);
		tidss_detach_pm_domains(tidss);
	}

	/* devm allocated dispc goes away with the dev so mark it NULL */
	dispc_remove(tidss);

	dev_dbg(dev, "%s done\n", __func__);

	return 0;
}

static void tidss_shutdown(struct platform_device *pdev)
{
	drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
}

static const struct of_device_id tidss_of_table[] = {
	{ .compatible = "ti,k2g-dss", .data = &dispc_k2g_feats, },
	{ .compatible = "ti,am625-dss", .data = &dispc_am625_feats, },
	{ .compatible = "ti,am62a7-dss", .data = &dispc_am62a7_feats, },
	{ .compatible = "ti,am62p51-dss", .data = &dispc_am62p51_feats, },
	{ .compatible = "ti,am62p52-dss", .data = &dispc_am62p52_feats, },
	{ .compatible = "ti,am65x-dss", .data = &dispc_am65x_feats, },
	{ .compatible = "ti,j721e-dss", .data = &dispc_j721e_feats, },
	{ }
};

MODULE_DEVICE_TABLE(of, tidss_of_table);

static struct platform_driver tidss_platform_driver = {
	.probe		= tidss_probe,
	.remove		= tidss_remove,
	.shutdown	= tidss_shutdown,
	.driver		= {
		.name	= "tidss",
		.pm	= pm_ptr(&tidss_pm_ops),
		.of_match_table = tidss_of_table,
		.suppress_bind_attrs = true,
	},
};

drm_module_platform_driver(tidss_platform_driver);

MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
MODULE_DESCRIPTION("TI Keystone DSS Driver");
MODULE_LICENSE("GPL v2");