summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/imx/imx8mp-ldb.c
blob: 5eff812d8680ea5accc1ecedad450b713df74f8c (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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2020,2022 NXP
 */

#include <linux/clk.h>
#include <linux/component.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/phy/phy.h>

#include <drm/bridge/fsl_imx_ldb.h>
#include <drm/drm_atomic_helper.h>
#include <drm/drm_probe_helper.h>
#include <drm/drm_simple_kms_helper.h>

#include "imx-drm.h"

#define DRIVER_NAME "imx8mp-ldb"

#define LDB_CH0_MODE_EN_TO_DI0		(1 << 0)
#define LDB_CH0_MODE_EN_TO_DI1		(3 << 0)
#define LDB_CH0_MODE_EN_MASK		(3 << 0)
#define LDB_CH1_MODE_EN_TO_DI0		(1 << 2)
#define LDB_CH1_MODE_EN_TO_DI1		(3 << 2)
#define LDB_CH1_MODE_EN_MASK		(3 << 2)
#define LDB_REG_CH0_FIFO_RESET		(1 << 11)
#define LDB_REG_CH1_FIFO_RESET		(1 << 12)
#define LDB_REG_ASYNC_FIFO_EN		(1 << 24)
#define LDB_FIFO_THRESHOLD		(4 << 25)

struct imx8mp_ldb;

struct imx8mp_ldb_channel {
	struct ldb_channel base;
	struct imx8mp_ldb *imx8mp_ldb;

	struct drm_connector connector;
	struct drm_encoder encoder;

	struct phy *phy;
	bool phy_is_on;

	u32 bus_flags;
};

static inline struct imx8mp_ldb_channel *
con_to_imx8mp_ldb_ch(struct drm_connector *c)
{
	return container_of(c, struct imx8mp_ldb_channel, connector);
}

static inline struct imx8mp_ldb_channel *
enc_to_imx8mp_ldb_ch(struct drm_encoder *e)
{
	return container_of(e, struct imx8mp_ldb_channel, encoder);
}

struct imx8mp_ldb {
	struct ldb base;
	struct imx8mp_ldb_channel channel[LDB_CH_NUM];
	struct clk *clk_root;
};

static struct drm_encoder *imx8mp_ldb_connector_best_encoder(
		struct drm_connector *connector)
{
	struct imx8mp_ldb_channel *imx8mp_ldb_ch =
						con_to_imx8mp_ldb_ch(connector);

	return &imx8mp_ldb_ch->encoder;
}

static void imx8mp_ldb_encoder_enable(struct drm_encoder *encoder)
{
	struct imx8mp_ldb_channel *imx8mp_ldb_ch =
						enc_to_imx8mp_ldb_ch(encoder);
	struct imx8mp_ldb *imx8mp_ldb = imx8mp_ldb_ch->imx8mp_ldb;
	struct ldb *ldb = &imx8mp_ldb->base;

	clk_prepare_enable(imx8mp_ldb->clk_root);

	if (imx8mp_ldb_ch == &imx8mp_ldb->channel[0] || ldb->dual) {
		ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
		ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI0;
	}
	if (imx8mp_ldb_ch == &imx8mp_ldb->channel[1] || ldb->dual) {
		ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
		ldb->ldb_ctrl |= ldb->dual ?
				LDB_CH1_MODE_EN_TO_DI0 : LDB_CH1_MODE_EN_TO_DI1;
	}

	if (ldb->dual) {
		phy_power_on(imx8mp_ldb->channel[0].phy);
		phy_power_on(imx8mp_ldb->channel[1].phy);

		imx8mp_ldb->channel[0].phy_is_on = true;
		imx8mp_ldb->channel[1].phy_is_on = true;
	} else {
		phy_power_on(imx8mp_ldb_ch->phy);

		imx8mp_ldb_ch->phy_is_on = true;
	}
}

static void
imx8mp_ldb_encoder_atomic_mode_set(struct drm_encoder *encoder,
				   struct drm_crtc_state *crtc_state,
				   struct drm_connector_state *connector_state)
{
	struct imx8mp_ldb_channel *imx8mp_ldb_ch =
						enc_to_imx8mp_ldb_ch(encoder);
	struct imx8mp_ldb *imx8mp_ldb = imx8mp_ldb_ch->imx8mp_ldb;
	struct ldb_channel *ldb_ch = &imx8mp_ldb_ch->base;
	struct ldb *ldb = &imx8mp_ldb->base;
	struct drm_display_mode *mode = &crtc_state->adjusted_mode;
	unsigned long serial_clk;

	if (mode->clock > 160000) {
		dev_warn(ldb->dev,
			 "%s: mode exceeds 160 MHz pixel clock\n", __func__);
	}
	if (mode->clock > 80000 && !ldb->dual) {
		dev_warn(ldb->dev,
			 "%s: mode exceeds 80 MHz pixel clock\n", __func__);
	}

	serial_clk = mode->clock * (ldb->dual ? 3500UL : 7000UL);
	clk_set_rate(imx8mp_ldb->clk_root, serial_clk);

	if (!ldb_ch->bus_format) {
		struct drm_connector *connector = connector_state->connector;
		struct drm_display_info *di = &connector->display_info;

		if (di->num_bus_formats)
			ldb_ch->bus_format = di->bus_formats[0];
	}
}

static void imx8mp_ldb_encoder_disable(struct drm_encoder *encoder)
{
	struct imx8mp_ldb_channel *imx8mp_ldb_ch =
						enc_to_imx8mp_ldb_ch(encoder);
	struct imx8mp_ldb *imx8mp_ldb = imx8mp_ldb_ch->imx8mp_ldb;
	struct ldb *ldb = &imx8mp_ldb->base;

	if (ldb->dual) {
		phy_power_off(imx8mp_ldb->channel[0].phy);
		phy_power_off(imx8mp_ldb->channel[1].phy);

		imx8mp_ldb->channel[0].phy_is_on = false;
		imx8mp_ldb->channel[1].phy_is_on = false;
	} else {
		phy_power_off(imx8mp_ldb_ch->phy);

		imx8mp_ldb_ch->phy_is_on = false;
	}

	clk_disable_unprepare(imx8mp_ldb->clk_root);
}

static int
imx8mp_ldb_encoder_atomic_check(struct drm_encoder *encoder,
				struct drm_crtc_state *crtc_state,
				struct drm_connector_state *conn_state)
{
	struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
	struct imx8mp_ldb_channel *imx8mp_ldb_ch =
						enc_to_imx8mp_ldb_ch(encoder);
	struct ldb_channel *ldb_ch = &imx8mp_ldb_ch->base;
	struct drm_display_info *di = &conn_state->connector->display_info;
	u32 bus_format = ldb_ch->bus_format;

	/* Bus format description in DT overrides connector display info. */
	if (!bus_format && di->num_bus_formats) {
		bus_format = di->bus_formats[0];
		imx_crtc_state->bus_flags = di->bus_flags;
	} else {
		bus_format = ldb_ch->bus_format;
		imx_crtc_state->bus_flags = imx8mp_ldb_ch->bus_flags;
	}
	switch (bus_format) {
	case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
		imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
		break;
	case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
	case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
		imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static enum drm_mode_status
imx8mp_ldb_encoder_mode_valid(struct drm_encoder *encoder,
			      const struct drm_display_mode *mode)
{
	struct imx8mp_ldb_channel *imx8mp_ldb_ch =
						enc_to_imx8mp_ldb_ch(encoder);
	struct ldb_channel *ldb_ch = &imx8mp_ldb_ch->base;
	struct imx8mp_ldb *imx8mp_ldb = imx8mp_ldb_ch->imx8mp_ldb;
	struct ldb *ldb = &imx8mp_ldb->base;

	/* it should be okay with a panel */
	if (ldb_ch->panel)
		return MODE_OK;

	/*
	 * According to the iMX8MP Reference manual, the LDB peripheral supports:
	 * - Single channel (4 lanes) output at up to 80MHz pixel clock and LVDS clock.
	 *   This supports resolutions up to 1366x768p60.
	 * - Dual asynchronous channels (8 data, 2 clocks). This is intended for a single
	 *   panel with two interfaces, transferring across two channels (even pixel/odd
	 *   pixel). This is supported at up to 160MHz pixel clock, which is up to 80MHz
	 *   LVDS clock (due to 2 pixels per LVDS clock). This supports resolutions
	 *   above 1366x768p60, up to 1080p60.
	 *
	 * Check if the clock that's set in the mode is not outside the maximum range.
	 * The manual doesn't state a minimum pixel clock, so it's not checked here.
	 */
	if (ldb->dual && mode->clock > 160000)
		return MODE_NOCLOCK;

	if (!ldb->dual && mode->clock > 80000)
		return MODE_NOCLOCK;

	return MODE_OK;
}

static const struct drm_connector_funcs imx8mp_ldb_connector_funcs = {
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = imx_drm_connector_destroy,
	.reset = drm_atomic_helper_connector_reset,
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
};

static const struct drm_connector_helper_funcs
imx8mp_ldb_connector_helper_funcs = {
	.best_encoder = imx8mp_ldb_connector_best_encoder,
};

static const struct drm_encoder_helper_funcs imx8mp_ldb_encoder_helper_funcs = {
	.atomic_mode_set = imx8mp_ldb_encoder_atomic_mode_set,
	.enable = imx8mp_ldb_encoder_enable,
	.disable = imx8mp_ldb_encoder_disable,
	.atomic_check = imx8mp_ldb_encoder_atomic_check,
	.mode_valid = imx8mp_ldb_encoder_mode_valid,
};

static const struct of_device_id imx8mp_ldb_dt_ids[] = {
	{ .compatible = "fsl,imx8mp-ldb", },
	{ }
};
MODULE_DEVICE_TABLE(of, imx8mp_ldb_dt_ids);

static int
imx8mp_ldb_bind(struct device *dev, struct device *master, void *data)
{
	struct drm_device *drm = data;
	struct device_node *np = dev->of_node;
	struct device_node *child;
	struct imx8mp_ldb *imx8mp_ldb = dev_get_drvdata(dev);
	struct ldb *ldb;
	struct ldb_channel *ldb_ch;
	struct drm_encoder *encoder[LDB_CH_NUM];
	int ret;
	int i;

	ldb = &imx8mp_ldb->base;
	ldb->dev = dev;
	ldb->ctrl_reg = 0x5c,
	ldb->output_port = 1;

	for (i = 0; i < LDB_CH_NUM; i++) {
		imx8mp_ldb->channel[i].imx8mp_ldb = imx8mp_ldb;
		ldb->channel[i] = &imx8mp_ldb->channel[i].base;
	}

	imx8mp_ldb->clk_root = devm_clk_get(dev, "ldb");
	if (IS_ERR(imx8mp_ldb->clk_root))
		return PTR_ERR(imx8mp_ldb->clk_root);

	for_each_child_of_node(np, child) {
		ret = of_property_read_u32(child, "reg", &i);
		if (ret || i < 0 || i > 1)
			return -EINVAL;

		if (!of_device_is_available(child))
			continue;

		encoder[i] = &imx8mp_ldb->channel[i].encoder;

		drm_encoder_helper_add(encoder[i],
				      &imx8mp_ldb_encoder_helper_funcs);
		drm_simple_encoder_init(drm, encoder[i], DRM_MODE_ENCODER_LVDS);
	}

	pm_runtime_enable(dev);

	ret = ldb_bind(ldb, encoder);
	if (ret)
		goto disable_pm_runtime;

	for_each_child_of_node(np, child) {
		struct imx8mp_ldb_channel *imx8mp_ldb_ch;
		bool auxiliary_ch = false;

		ret = of_property_read_u32(child, "reg", &i);
		if (ret || i < 0 || i > 1) {
			ret = -EINVAL;
			goto free_child;
		}

		if (ldb->dual && i > 0) {
			auxiliary_ch = true;
			imx8mp_ldb_ch = &imx8mp_ldb->channel[i];
			goto get_phy;
		}

		if (!of_device_is_available(child))
			continue;

		imx8mp_ldb_ch = &imx8mp_ldb->channel[i];
get_phy:
		imx8mp_ldb_ch->phy = devm_of_phy_get(dev, child, "ldb_phy");
		if (IS_ERR(imx8mp_ldb_ch->phy)) {
			ret = PTR_ERR(imx8mp_ldb_ch->phy);
			if (ret != -EPROBE_DEFER)
				dev_err(dev, "can't get channel%d phy: %d\n",
					i, ret);
			goto free_child;
		}

		ret = phy_init(imx8mp_ldb_ch->phy);
		if (ret < 0) {
			dev_err(dev, "failed to initialize channel%d phy: %d\n",
				i, ret);
			goto free_child;
		}

		if (auxiliary_ch)
			continue;
	}

	for (i = 0; i < LDB_CH_NUM; i++) {
		ldb_ch = &imx8mp_ldb->channel[i].base;

		if (!ldb_ch->is_valid)
			continue;

		ret = imx_drm_encoder_parse_of(drm, encoder[i], ldb_ch->child);
		if (ret)
			goto disable_pm_runtime;
	}

	return 0;

free_child:
	of_node_put(child);
disable_pm_runtime:
	pm_runtime_disable(dev);

	return ret;
}

static void imx8mp_ldb_unbind(struct device *dev, struct device *master,
	void *data)
{
	struct imx8mp_ldb *imx8mp_ldb = dev_get_drvdata(dev);
	int i;

	for (i = 0; i < LDB_CH_NUM; i++) {
		struct imx8mp_ldb_channel *imx8mp_ldb_ch =
						&imx8mp_ldb->channel[i];

		if (imx8mp_ldb_ch->phy_is_on)
			phy_power_off(imx8mp_ldb_ch->phy);

		phy_exit(imx8mp_ldb_ch->phy);
	}

	pm_runtime_disable(dev);
}

static const struct component_ops imx8mp_ldb_ops = {
	.bind	= imx8mp_ldb_bind,
	.unbind	= imx8mp_ldb_unbind,
};

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

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

	dev_set_drvdata(dev, imx8mp_ldb);

	return component_add(dev, &imx8mp_ldb_ops);
}

static int imx8mp_ldb_remove(struct platform_device *pdev)
{
	component_del(&pdev->dev, &imx8mp_ldb_ops);
	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int imx8mp_ldb_suspend(struct device *dev)
{
	struct imx8mp_ldb *imx8mp_ldb = dev_get_drvdata(dev);
	int i;

	if (imx8mp_ldb == NULL)
		return 0;

	for (i = 0; i < LDB_CH_NUM; i++)
		phy_exit(imx8mp_ldb->channel[i].phy);

	return 0;
}

static int imx8mp_ldb_resume(struct device *dev)
{
	struct imx8mp_ldb *imx8mp_ldb = dev_get_drvdata(dev);
	int i;

	if (imx8mp_ldb == NULL)
		return 0;

	for (i = 0; i < LDB_CH_NUM; i++)
		phy_init(imx8mp_ldb->channel[i].phy);

	return 0;
}
#endif

static const struct dev_pm_ops imx8mp_ldb_pm_ops = {
	SET_LATE_SYSTEM_SLEEP_PM_OPS(imx8mp_ldb_suspend, imx8mp_ldb_resume)
};

static struct platform_driver imx8mp_ldb_driver = {
	.probe		= imx8mp_ldb_probe,
	.remove		= imx8mp_ldb_remove,
	.driver		= {
		.of_match_table = imx8mp_ldb_dt_ids,
		.name	= DRIVER_NAME,
		.pm	= &imx8mp_ldb_pm_ops,
	},
};

module_platform_driver(imx8mp_ldb_driver);

MODULE_DESCRIPTION("i.MX8MP LVDS driver");
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" DRIVER_NAME);