summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/imx/lcdif/lcdif-crtc.c
blob: df7618c308f09440f4457f7bade000bb0db9675e (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
/*
 * Copyright 2018 NXP
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/component.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <drm/drmP.h>
#include <drm/drm_atomic_helper.h>
#include <video/imx-lcdif.h>
#include <video/videomode.h>

#include "imx-drm.h"
#include "lcdif-plane.h"
#include "lcdif-kms.h"

struct lcdif_crtc {
	struct device *dev;

	struct drm_crtc base;
	struct imx_drm_crtc *imx_crtc;
	struct lcdif_plane *plane[2];

	int vbl_irq;
	u32 pix_fmt;		/* drm fourcc */
};

#define to_lcdif_crtc(crtc) container_of(crtc, struct lcdif_crtc, base)

static void lcdif_crtc_destroy(struct drm_crtc *crtc)
{
	struct lcdif_crtc *lcdif_crtc = to_lcdif_crtc(crtc);

	imx_drm_remove_crtc(lcdif_crtc->imx_crtc);
	lcdif_crtc->imx_crtc = NULL;
}

static void lcdif_crtc_reset(struct drm_crtc *crtc)
{
	struct imx_crtc_state *state;

	if (crtc->state) {
		__drm_atomic_helper_crtc_destroy_state(crtc->state);

		state = to_imx_crtc_state(crtc->state);
		kfree(state);
		crtc->state = NULL;
	}

	state = kzalloc(sizeof(*state), GFP_KERNEL);
	if (!state)
		return;

	crtc->state = &state->base;
	crtc->state->crtc = crtc;
}

static struct drm_crtc_state *lcdif_crtc_duplicate_state(struct drm_crtc *crtc)
{
	struct imx_crtc_state *state, *orig_state;

	if (WARN_ON(!crtc->state))
		return NULL;

	state = kzalloc(sizeof(*state), GFP_KERNEL);
	if (!state)
		return NULL;

	__drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);

	orig_state = to_imx_crtc_state(crtc->state);
	state->bus_format = orig_state->bus_format;
	state->bus_flags = orig_state->bus_flags;
	state->di_hsync_pin = orig_state->di_hsync_pin;
	state->di_vsync_pin = orig_state->di_vsync_pin;

	return &state->base;
}

static void lcdif_crtc_destroy_state(struct drm_crtc *crtc,
				     struct drm_crtc_state *state)
{
	__drm_atomic_helper_crtc_destroy_state(state);
	kfree(to_imx_crtc_state(state));
}

static const struct drm_crtc_funcs lcdif_crtc_funcs = {
	.set_config = drm_atomic_helper_set_config,
	.destroy    = lcdif_crtc_destroy,
	.page_flip  = drm_atomic_helper_page_flip,
	.reset      = lcdif_crtc_reset,
	.atomic_duplicate_state = lcdif_crtc_duplicate_state,
	.atomic_destroy_state	= lcdif_crtc_destroy_state,
};

static int lcdif_crtc_atomic_check(struct drm_crtc *crtc,
				   struct drm_crtc_state *state)
{
	struct lcdif_crtc *lcdif_crtc = to_lcdif_crtc(crtc);
	struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(state);

	/* return directly when no devices attached
	 * to LCDIF to avoid below error messsage to
	 * make noises in this case.
	 */
	if (!imx_crtc_state->bus_format)
		return -EINVAL;

	/* check the requested bus format can be
	 * supported by LCDIF CTRC or not
	 */
	switch (imx_crtc_state->bus_format) {
	case MEDIA_BUS_FMT_RGB565_1X16:
	case MEDIA_BUS_FMT_RGB666_1X18:
	case MEDIA_BUS_FMT_RGB888_1X24:
		break;
	default:
		dev_err(lcdif_crtc->dev,
			"unsupported bus format: %#x\n",
			imx_crtc_state->bus_format);
		return -EINVAL;
	}

	return 0;
}

static void lcdif_crtc_atomic_begin(struct drm_crtc *crtc,
				    struct drm_crtc_state *old_crtc_state)
{
	drm_crtc_vblank_on(crtc);

	spin_lock_irq(&crtc->dev->event_lock);
	if (crtc->state->event) {
		WARN_ON(drm_crtc_vblank_get(crtc));
		drm_crtc_arm_vblank_event(crtc, crtc->state->event);
		crtc->state->event = NULL;
	}
	spin_unlock_irq(&crtc->dev->event_lock);
}

static void lcdif_crtc_atomic_flush(struct drm_crtc *crtc,
				    struct drm_crtc_state *old_crtc_state)
{
	/* LCDIF doesn't have command buffer */
	return;
}

static void lcdif_crtc_enable(struct drm_crtc *crtc)
{
	struct lcdif_crtc *lcdif_crtc = to_lcdif_crtc(crtc);
	struct lcdif_soc *lcdif = dev_get_drvdata(lcdif_crtc->dev->parent);
	struct drm_display_mode *mode = &crtc->state->adjusted_mode;
	struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc->state);
	struct videomode vm;

	drm_display_mode_to_videomode(mode, &vm);

	if (imx_crtc_state->bus_flags & DRM_BUS_FLAG_DE_HIGH)
		vm.flags |= DISPLAY_FLAGS_DE_HIGH;
	else
		vm.flags |= DISPLAY_FLAGS_DE_LOW;

	if (imx_crtc_state->bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE)
		vm.flags |= DISPLAY_FLAGS_PIXDATA_POSEDGE;
	else
		vm.flags |= DISPLAY_FLAGS_PIXDATA_NEGEDGE;

	pm_runtime_get_sync(lcdif_crtc->dev->parent);

	lcdif_set_mode(lcdif, &vm);

	/* config LCDIF output bus format */
	lcdif_set_bus_fmt(lcdif, imx_crtc_state->bus_format);

	/* defer the lcdif controller enable to plane update,
	 * since until then the lcdif config is complete to
	 * enable the controller to run actually.
	 */
}

static void lcdif_crtc_atomic_disable(struct drm_crtc *crtc,
				      struct drm_crtc_state *old_crtc_state)
{
	struct lcdif_crtc *lcdif_crtc = to_lcdif_crtc(crtc);
	struct lcdif_soc *lcdif = dev_get_drvdata(lcdif_crtc->dev->parent);

	spin_lock_irq(&crtc->dev->event_lock);
	if (crtc->state->event) {
		drm_crtc_send_vblank_event(crtc, crtc->state->event);
		crtc->state->event = NULL;
	}
	spin_unlock_irq(&crtc->dev->event_lock);

	drm_crtc_vblank_off(crtc);

	lcdif_disable_controller(lcdif);

	pm_runtime_put(lcdif_crtc->dev->parent);
}

static const struct drm_crtc_helper_funcs lcdif_helper_funcs = {
	.atomic_check	= lcdif_crtc_atomic_check,
	.atomic_begin	= lcdif_crtc_atomic_begin,
	.atomic_flush	= lcdif_crtc_atomic_flush,
	.enable		= lcdif_crtc_enable,
	.atomic_disable	= lcdif_crtc_atomic_disable,
};

static int lcdif_enable_vblank(struct drm_crtc *crtc)
{
	struct lcdif_crtc *lcdif_crtc = to_lcdif_crtc(crtc);
	struct lcdif_soc *lcdif = dev_get_drvdata(lcdif_crtc->dev->parent);

	lcdif_vblank_irq_enable(lcdif);
	enable_irq(lcdif_crtc->vbl_irq);

	return 0;
}

static void lcdif_disable_vblank(struct drm_crtc *crtc)
{
	struct lcdif_crtc *lcdif_crtc = to_lcdif_crtc(crtc);
	struct lcdif_soc *lcdif = dev_get_drvdata(lcdif_crtc->dev->parent);

	disable_irq_nosync(lcdif_crtc->vbl_irq);
	lcdif_vblank_irq_disable(lcdif);
}

static const struct imx_drm_crtc_helper_funcs lcdif_crtc_helper_funcs = {
	.enable_vblank     = lcdif_enable_vblank,
	.disable_vblank    = lcdif_disable_vblank,
	.crtc_funcs        = &lcdif_crtc_funcs,
	.crtc_helper_funcs = &lcdif_helper_funcs,
};

static irqreturn_t lcdif_crtc_vblank_irq_handler(int irq, void *dev_id)
{
	struct lcdif_crtc *lcdif_crtc = dev_id;
	struct lcdif_soc *lcdif = dev_get_drvdata(lcdif_crtc->dev->parent);

	drm_crtc_handle_vblank(&lcdif_crtc->base);

	lcdif_vblank_irq_clear(lcdif);

	return IRQ_HANDLED;
}

static int lcdif_crtc_init(struct lcdif_crtc *lcdif_crtc,
			   struct lcdif_client_platformdata *pdata,
			   struct drm_device *drm)
{
	int ret;
	struct lcdif_plane *primary = lcdif_crtc->plane[0];
	struct lcdif_soc *lcdif = dev_get_drvdata(lcdif_crtc->dev->parent);

	/* Primary plane
	 * The 'possible_crtcs' of primary plane will be
	 * recalculated during the 'crtc' initialization
	 * later.
	 */
	primary = lcdif_plane_init(drm, lcdif, 0, DRM_PLANE_TYPE_PRIMARY, 0);
	if (IS_ERR(primary))
		return PTR_ERR(primary);
	lcdif_crtc->plane[0] = primary;

	/* TODO: Overlay plane */

	ret = imx_drm_add_crtc(drm, &lcdif_crtc->base,
			       &lcdif_crtc->imx_crtc, &primary->base,
			       &lcdif_crtc_helper_funcs, pdata->of_node);
	if (ret) {
		dev_err(lcdif_crtc->dev, "failed to init crtc\n");
		goto primary_plane_deinit;
	}

	lcdif_crtc->vbl_irq = lcdif_vblank_irq_get(lcdif);
	WARN_ON(lcdif_crtc->vbl_irq < 0);

	ret = devm_request_irq(lcdif_crtc->dev, lcdif_crtc->vbl_irq,
			       lcdif_crtc_vblank_irq_handler, 0,
			       dev_name(lcdif_crtc->dev), lcdif_crtc);
	if (ret) {
		dev_err(lcdif_crtc->dev,
			"vblank irq request failed: %d\n", ret);
		goto primary_plane_deinit;
	}

	disable_irq(lcdif_crtc->vbl_irq);

	return 0;

primary_plane_deinit:
	lcdif_plane_deinit(drm, primary);

	return ret;
}

static int lcdif_crtc_bind(struct device *dev, struct device *master,
			   void *data)
{
	int ret;
	struct drm_device *drm = data;
	struct lcdif_crtc *lcdif_crtc;
	struct lcdif_client_platformdata *pdata = dev->platform_data;

	dev_dbg(dev, "%s: lcdif crtc bind begin\n", __func__);

	lcdif_crtc = devm_kzalloc(dev, sizeof(*lcdif_crtc), GFP_KERNEL);
	if (!lcdif_crtc)
		return -ENOMEM;

	lcdif_crtc->dev = dev;

	ret = lcdif_crtc_init(lcdif_crtc, pdata, drm);
	if (ret)
		return ret;

	if (!drm->mode_config.funcs)
		drm->mode_config.funcs = &lcdif_drm_mode_config_funcs;

	if (!drm->mode_config.helper_private)
		drm->mode_config.helper_private = &lcdif_drm_mode_config_helpers;

	/* limit the max width and height */
	drm->mode_config.max_width  = 1920;
	drm->mode_config.max_height = 1920;

	dev_set_drvdata(dev, lcdif_crtc);

	dev_dbg(dev, "%s: lcdif crtc bind end\n", __func__);

	return 0;
}

static void lcdif_crtc_unbind(struct device *dev, struct device *master,
			      void *data)
{
	struct drm_device *drm = data;
	struct lcdif_crtc *lcdif_crtc = dev_get_drvdata(dev);

	if (lcdif_crtc->imx_crtc) {
		imx_drm_remove_crtc(lcdif_crtc->imx_crtc);
		lcdif_crtc->imx_crtc = NULL;
	}

	lcdif_plane_deinit(drm, lcdif_crtc->plane[0]);
}

static const struct component_ops lcdif_crtc_ops = {
	.bind   = lcdif_crtc_bind,
	.unbind = lcdif_crtc_unbind,
};

static int lcdif_crtc_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;

	dev_dbg(&pdev->dev, "%s: lcdif crtc probe begin\n", __func__);

	if (!dev->platform_data) {
		dev_err(dev, "no platform data\n");
		return -EINVAL;
	}

	return component_add(dev, &lcdif_crtc_ops);
}

static int lcdif_crtc_remove(struct platform_device *pdev)
{
	component_del(&pdev->dev, &lcdif_crtc_ops);

	return 0;
}

static struct platform_driver lcdif_crtc_driver = {
	.probe  = lcdif_crtc_probe,
	.remove = lcdif_crtc_remove,
	.driver = {
		.name = "imx-lcdif-crtc",
	},
};
module_platform_driver(lcdif_crtc_driver);

MODULE_DESCRIPTION("NXP i.MX LCDIF DRM CRTC driver");
MODULE_AUTHOR("Fancy Fang <chen.fang@nxp.com>");
MODULE_LICENSE("GPL");