blob: db461b6a257fb31866f49803bdf5aea4932fa771 [file] [log] [blame]
Fabio Estevam946485d2018-09-28 16:13:25 -03001// SPDX-License-Identifier: GPL-2.0+
Sascha Hauerac4c1a92013-06-18 09:23:57 +08002/*
3 * i.MX drm driver - LVDS display bridge
4 *
5 * Copyright (C) 2012 Sascha Hauer, Pengutronix
Sascha Hauerac4c1a92013-06-18 09:23:57 +08006 */
7
Sascha Hauerac4c1a92013-06-18 09:23:57 +08008#include <linux/clk.h>
Russell King17b50012013-11-03 11:23:34 +00009#include <linux/component.h>
Sam Ravnborg05f09402019-07-16 08:42:18 +020010#include <linux/mfd/syscon.h>
11#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
12#include <linux/module.h>
13#include <linux/of_device.h>
14#include <linux/of_graph.h>
15#include <linux/regmap.h>
16#include <linux/videodev2.h>
17
18#include <video/of_display_timing.h>
19#include <video/of_videomode.h>
20
Philipp Zabel49f98bc2016-07-06 14:49:24 +020021#include <drm/drm_atomic.h>
Liu Ying255c35f2016-07-08 17:40:56 +080022#include <drm/drm_atomic_helper.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080023#include <drm/drm_fb_helper.h>
Philipp Zabel53141e42015-02-24 11:41:28 +010024#include <drm/drm_of.h>
Philipp Zabel751e2672014-03-06 14:54:39 +010025#include <drm/drm_panel.h>
Sam Ravnborg05f09402019-07-16 08:42:18 +020026#include <drm/drm_print.h>
Daniel Vetterfcd70cd2019-01-17 22:03:34 +010027#include <drm/drm_probe_helper.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080028
29#include "imx-drm.h"
30
31#define DRIVER_NAME "imx-ldb"
32
33#define LDB_CH0_MODE_EN_TO_DI0 (1 << 0)
34#define LDB_CH0_MODE_EN_TO_DI1 (3 << 0)
35#define LDB_CH0_MODE_EN_MASK (3 << 0)
36#define LDB_CH1_MODE_EN_TO_DI0 (1 << 2)
37#define LDB_CH1_MODE_EN_TO_DI1 (3 << 2)
38#define LDB_CH1_MODE_EN_MASK (3 << 2)
39#define LDB_SPLIT_MODE_EN (1 << 4)
40#define LDB_DATA_WIDTH_CH0_24 (1 << 5)
41#define LDB_BIT_MAP_CH0_JEIDA (1 << 6)
42#define LDB_DATA_WIDTH_CH1_24 (1 << 7)
43#define LDB_BIT_MAP_CH1_JEIDA (1 << 8)
44#define LDB_DI0_VS_POL_ACT_LOW (1 << 9)
45#define LDB_DI1_VS_POL_ACT_LOW (1 << 10)
46#define LDB_BGREF_RMODE_INT (1 << 15)
47
Sascha Hauerac4c1a92013-06-18 09:23:57 +080048struct imx_ldb;
49
50struct imx_ldb_channel {
51 struct imx_ldb *ldb;
52 struct drm_connector connector;
Philipp Zabel49f98bc2016-07-06 14:49:24 +020053 struct drm_encoder encoder;
Peter Senna Tschudindc80d702016-08-05 00:36:58 +020054
55 /* Defines what is connected to the ldb, only one at a time */
Philipp Zabel751e2672014-03-06 14:54:39 +010056 struct drm_panel *panel;
Peter Senna Tschudindc80d702016-08-05 00:36:58 +020057 struct drm_bridge *bridge;
58
Russell King1b3f7672013-11-03 13:30:48 +000059 struct device_node *child;
Steve Longerbeama6d206e2016-04-27 16:23:33 -040060 struct i2c_adapter *ddc;
Sascha Hauerac4c1a92013-06-18 09:23:57 +080061 int chno;
62 void *edid;
63 int edid_len;
64 struct drm_display_mode mode;
65 int mode_valid;
Philipp Zabel49f98bc2016-07-06 14:49:24 +020066 u32 bus_format;
Lothar Waßmannfafc79e2016-07-12 15:30:03 +020067 u32 bus_flags;
Sascha Hauerac4c1a92013-06-18 09:23:57 +080068};
69
Philipp Zabel3df07392016-07-06 15:47:11 +020070static inline struct imx_ldb_channel *con_to_imx_ldb_ch(struct drm_connector *c)
71{
72 return container_of(c, struct imx_ldb_channel, connector);
73}
74
Philipp Zabel49f98bc2016-07-06 14:49:24 +020075static inline struct imx_ldb_channel *enc_to_imx_ldb_ch(struct drm_encoder *e)
76{
77 return container_of(e, struct imx_ldb_channel, encoder);
78}
79
Sascha Hauerac4c1a92013-06-18 09:23:57 +080080struct bus_mux {
81 int reg;
82 int shift;
83 int mask;
84};
85
86struct imx_ldb {
87 struct regmap *regmap;
88 struct device *dev;
89 struct imx_ldb_channel channel[2];
90 struct clk *clk[2]; /* our own clock */
91 struct clk *clk_sel[4]; /* parent of display clock */
Philipp Zabel3973aff2014-11-26 13:59:11 +010092 struct clk *clk_parent[4]; /* original parent of clk_sel */
Sascha Hauerac4c1a92013-06-18 09:23:57 +080093 struct clk *clk_pll[2]; /* upstream clock we can adjust */
94 u32 ldb_ctrl;
95 const struct bus_mux *lvds_mux;
96};
97
Philipp Zabel49f98bc2016-07-06 14:49:24 +020098static void imx_ldb_ch_set_bus_format(struct imx_ldb_channel *imx_ldb_ch,
99 u32 bus_format)
Liu Ying032003c2016-07-08 17:40:58 +0800100{
101 struct imx_ldb *ldb = imx_ldb_ch->ldb;
102 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
103
104 switch (bus_format) {
105 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
Liu Ying032003c2016-07-08 17:40:58 +0800106 break;
107 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
Liu Ying032003c2016-07-08 17:40:58 +0800108 if (imx_ldb_ch->chno == 0 || dual)
109 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24;
110 if (imx_ldb_ch->chno == 1 || dual)
111 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24;
112 break;
113 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
Liu Ying032003c2016-07-08 17:40:58 +0800114 if (imx_ldb_ch->chno == 0 || dual)
115 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24 |
116 LDB_BIT_MAP_CH0_JEIDA;
117 if (imx_ldb_ch->chno == 1 || dual)
118 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24 |
119 LDB_BIT_MAP_CH1_JEIDA;
120 break;
121 }
122}
123
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800124static int imx_ldb_connector_get_modes(struct drm_connector *connector)
125{
126 struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
127 int num_modes = 0;
128
Philipp Zabel751e2672014-03-06 14:54:39 +0100129 if (imx_ldb_ch->panel && imx_ldb_ch->panel->funcs &&
130 imx_ldb_ch->panel->funcs->get_modes) {
131 num_modes = imx_ldb_ch->panel->funcs->get_modes(imx_ldb_ch->panel);
132 if (num_modes > 0)
133 return num_modes;
134 }
135
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400136 if (!imx_ldb_ch->edid && imx_ldb_ch->ddc)
137 imx_ldb_ch->edid = drm_get_edid(connector, imx_ldb_ch->ddc);
138
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800139 if (imx_ldb_ch->edid) {
Daniel Vetterc555f022018-07-09 10:40:06 +0200140 drm_connector_update_edid_property(connector,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800141 imx_ldb_ch->edid);
142 num_modes = drm_add_edid_modes(connector, imx_ldb_ch->edid);
143 }
144
145 if (imx_ldb_ch->mode_valid) {
146 struct drm_display_mode *mode;
147
148 mode = drm_mode_create(connector->dev);
Fabio Estevam9f9b0362014-02-26 20:53:40 -0300149 if (!mode)
150 return -EINVAL;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800151 drm_mode_copy(mode, &imx_ldb_ch->mode);
152 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
153 drm_mode_probed_add(connector, mode);
154 num_modes++;
155 }
156
157 return num_modes;
158}
159
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800160static struct drm_encoder *imx_ldb_connector_best_encoder(
161 struct drm_connector *connector)
162{
163 struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
164
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200165 return &imx_ldb_ch->encoder;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800166}
167
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800168static void imx_ldb_set_clock(struct imx_ldb *ldb, int mux, int chno,
169 unsigned long serial_clk, unsigned long di_clk)
170{
171 int ret;
172
173 dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
174 clk_get_rate(ldb->clk_pll[chno]), serial_clk);
175 clk_set_rate(ldb->clk_pll[chno], serial_clk);
176
177 dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
178 clk_get_rate(ldb->clk_pll[chno]));
179
180 dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
181 clk_get_rate(ldb->clk[chno]),
182 (long int)di_clk);
183 clk_set_rate(ldb->clk[chno], di_clk);
184
185 dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
186 clk_get_rate(ldb->clk[chno]));
187
188 /* set display clock mux to LDB input clock */
189 ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk[chno]);
Sima Baymani49f4a9c2013-11-03 11:18:57 +0100190 if (ret)
Aybuke Ozdemire5e1b162014-03-17 23:55:53 +0200191 dev_err(ldb->dev,
192 "unable to set di%d parent clock to ldb_di%d\n", mux,
193 chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800194}
195
Liu Yingf6e396e2016-07-08 17:41:01 +0800196static void imx_ldb_encoder_enable(struct drm_encoder *encoder)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800197{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200198 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800199 struct imx_ldb *ldb = imx_ldb_ch->ldb;
200 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
Philipp Zabel53141e42015-02-24 11:41:28 +0100201 int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800202
Philipp Zabel751e2672014-03-06 14:54:39 +0100203 drm_panel_prepare(imx_ldb_ch->panel);
204
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800205 if (dual) {
Liu Yingf6e396e2016-07-08 17:41:01 +0800206 clk_set_parent(ldb->clk_sel[mux], ldb->clk[0]);
207 clk_set_parent(ldb->clk_sel[mux], ldb->clk[1]);
208
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800209 clk_prepare_enable(ldb->clk[0]);
210 clk_prepare_enable(ldb->clk[1]);
Liu Yingf6e396e2016-07-08 17:41:01 +0800211 } else {
212 clk_set_parent(ldb->clk_sel[mux], ldb->clk[imx_ldb_ch->chno]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800213 }
214
215 if (imx_ldb_ch == &ldb->channel[0] || dual) {
216 ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
217 if (mux == 0 || ldb->lvds_mux)
218 ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI0;
219 else if (mux == 1)
220 ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI1;
221 }
222 if (imx_ldb_ch == &ldb->channel[1] || dual) {
223 ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
224 if (mux == 1 || ldb->lvds_mux)
225 ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI1;
226 else if (mux == 0)
227 ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI0;
228 }
229
230 if (ldb->lvds_mux) {
231 const struct bus_mux *lvds_mux = NULL;
232
233 if (imx_ldb_ch == &ldb->channel[0])
234 lvds_mux = &ldb->lvds_mux[0];
235 else if (imx_ldb_ch == &ldb->channel[1])
236 lvds_mux = &ldb->lvds_mux[1];
237
238 regmap_update_bits(ldb->regmap, lvds_mux->reg, lvds_mux->mask,
239 mux << lvds_mux->shift);
240 }
241
242 regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
Philipp Zabel751e2672014-03-06 14:54:39 +0100243
244 drm_panel_enable(imx_ldb_ch->panel);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800245}
246
Philipp Zabel3a2ad502016-07-22 12:43:04 +0200247static void
248imx_ldb_encoder_atomic_mode_set(struct drm_encoder *encoder,
249 struct drm_crtc_state *crtc_state,
250 struct drm_connector_state *connector_state)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800251{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200252 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
Philipp Zabel3a2ad502016-07-22 12:43:04 +0200253 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800254 struct imx_ldb *ldb = imx_ldb_ch->ldb;
255 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
Philipp Zabel51dac942015-01-23 17:10:01 +0100256 unsigned long serial_clk;
257 unsigned long di_clk = mode->clock * 1000;
Philipp Zabel53141e42015-02-24 11:41:28 +0100258 int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200259 u32 bus_format = imx_ldb_ch->bus_format;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800260
261 if (mode->clock > 170000) {
262 dev_warn(ldb->dev,
263 "%s: mode exceeds 170 MHz pixel clock\n", __func__);
264 }
265 if (mode->clock > 85000 && !dual) {
266 dev_warn(ldb->dev,
267 "%s: mode exceeds 85 MHz pixel clock\n", __func__);
268 }
269
Philipp Zabel51dac942015-01-23 17:10:01 +0100270 if (dual) {
271 serial_clk = 3500UL * mode->clock;
272 imx_ldb_set_clock(ldb, mux, 0, serial_clk, di_clk);
273 imx_ldb_set_clock(ldb, mux, 1, serial_clk, di_clk);
274 } else {
275 serial_clk = 7000UL * mode->clock;
276 imx_ldb_set_clock(ldb, mux, imx_ldb_ch->chno, serial_clk,
277 di_clk);
278 }
279
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800280 /* FIXME - assumes straight connections DI0 --> CH0, DI1 --> CH1 */
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200281 if (imx_ldb_ch == &ldb->channel[0] || dual) {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800282 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
283 ldb->ldb_ctrl |= LDB_DI0_VS_POL_ACT_LOW;
284 else if (mode->flags & DRM_MODE_FLAG_PVSYNC)
285 ldb->ldb_ctrl &= ~LDB_DI0_VS_POL_ACT_LOW;
286 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200287 if (imx_ldb_ch == &ldb->channel[1] || dual) {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800288 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
289 ldb->ldb_ctrl |= LDB_DI1_VS_POL_ACT_LOW;
290 else if (mode->flags & DRM_MODE_FLAG_PVSYNC)
291 ldb->ldb_ctrl &= ~LDB_DI1_VS_POL_ACT_LOW;
292 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200293
294 if (!bus_format) {
Philipp Zabel3a2ad502016-07-22 12:43:04 +0200295 struct drm_connector *connector = connector_state->connector;
296 struct drm_display_info *di = &connector->display_info;
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200297
Philipp Zabel3a2ad502016-07-22 12:43:04 +0200298 if (di->num_bus_formats)
299 bus_format = di->bus_formats[0];
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200300 }
301 imx_ldb_ch_set_bus_format(imx_ldb_ch, bus_format);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800302}
303
304static void imx_ldb_encoder_disable(struct drm_encoder *encoder)
305{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200306 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800307 struct imx_ldb *ldb = imx_ldb_ch->ldb;
Philipp Zabel3973aff2014-11-26 13:59:11 +0100308 int mux, ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800309
Philipp Zabel751e2672014-03-06 14:54:39 +0100310 drm_panel_disable(imx_ldb_ch->panel);
311
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800312 if (imx_ldb_ch == &ldb->channel[0])
313 ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
314 else if (imx_ldb_ch == &ldb->channel[1])
315 ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
316
317 regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
318
319 if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
320 clk_disable_unprepare(ldb->clk[0]);
321 clk_disable_unprepare(ldb->clk[1]);
322 }
Philipp Zabel751e2672014-03-06 14:54:39 +0100323
Philipp Zabel3973aff2014-11-26 13:59:11 +0100324 if (ldb->lvds_mux) {
325 const struct bus_mux *lvds_mux = NULL;
326
327 if (imx_ldb_ch == &ldb->channel[0])
328 lvds_mux = &ldb->lvds_mux[0];
329 else if (imx_ldb_ch == &ldb->channel[1])
330 lvds_mux = &ldb->lvds_mux[1];
331
332 regmap_read(ldb->regmap, lvds_mux->reg, &mux);
333 mux &= lvds_mux->mask;
334 mux >>= lvds_mux->shift;
335 } else {
336 mux = (imx_ldb_ch == &ldb->channel[0]) ? 0 : 1;
337 }
338
339 /* set display clock mux back to original input clock */
340 ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk_parent[mux]);
341 if (ret)
342 dev_err(ldb->dev,
343 "unable to set di%d parent clock to original parent\n",
344 mux);
345
Philipp Zabel751e2672014-03-06 14:54:39 +0100346 drm_panel_unprepare(imx_ldb_ch->panel);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800347}
348
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200349static int imx_ldb_encoder_atomic_check(struct drm_encoder *encoder,
350 struct drm_crtc_state *crtc_state,
351 struct drm_connector_state *conn_state)
352{
353 struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
354 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
355 struct drm_display_info *di = &conn_state->connector->display_info;
356 u32 bus_format = imx_ldb_ch->bus_format;
357
358 /* Bus format description in DT overrides connector display info. */
Lothar Waßmannfafc79e2016-07-12 15:30:03 +0200359 if (!bus_format && di->num_bus_formats) {
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200360 bus_format = di->bus_formats[0];
Lothar Waßmannfafc79e2016-07-12 15:30:03 +0200361 imx_crtc_state->bus_flags = di->bus_flags;
362 } else {
363 bus_format = imx_ldb_ch->bus_format;
364 imx_crtc_state->bus_flags = imx_ldb_ch->bus_flags;
365 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200366 switch (bus_format) {
367 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
368 imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
369 break;
370 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
371 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
372 imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
373 break;
374 default:
375 return -EINVAL;
376 }
377
378 imx_crtc_state->di_hsync_pin = 2;
379 imx_crtc_state->di_vsync_pin = 3;
380
381 return 0;
382}
383
384
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100385static const struct drm_connector_funcs imx_ldb_connector_funcs = {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800386 .fill_modes = drm_helper_probe_single_connector_modes,
Russell King1b3f7672013-11-03 13:30:48 +0000387 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800388 .reset = drm_atomic_helper_connector_reset,
389 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
390 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800391};
392
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100393static const struct drm_connector_helper_funcs imx_ldb_connector_helper_funcs = {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800394 .get_modes = imx_ldb_connector_get_modes,
395 .best_encoder = imx_ldb_connector_best_encoder,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800396};
397
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100398static const struct drm_encoder_funcs imx_ldb_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000399 .destroy = imx_drm_encoder_destroy,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800400};
401
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100402static const struct drm_encoder_helper_funcs imx_ldb_encoder_helper_funcs = {
Philipp Zabel3a2ad502016-07-22 12:43:04 +0200403 .atomic_mode_set = imx_ldb_encoder_atomic_mode_set,
Liu Yingf6e396e2016-07-08 17:41:01 +0800404 .enable = imx_ldb_encoder_enable,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800405 .disable = imx_ldb_encoder_disable,
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200406 .atomic_check = imx_ldb_encoder_atomic_check,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800407};
408
409static int imx_ldb_get_clk(struct imx_ldb *ldb, int chno)
410{
411 char clkname[16];
412
Fabio Estevam98dd3b22014-02-28 11:39:42 -0300413 snprintf(clkname, sizeof(clkname), "di%d", chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800414 ldb->clk[chno] = devm_clk_get(ldb->dev, clkname);
415 if (IS_ERR(ldb->clk[chno]))
416 return PTR_ERR(ldb->clk[chno]);
417
Fabio Estevam98dd3b22014-02-28 11:39:42 -0300418 snprintf(clkname, sizeof(clkname), "di%d_pll", chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800419 ldb->clk_pll[chno] = devm_clk_get(ldb->dev, clkname);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800420
Valentina Manea1f933fa2013-10-23 10:29:55 +0300421 return PTR_ERR_OR_ZERO(ldb->clk_pll[chno]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800422}
423
Russell King1b3f7672013-11-03 13:30:48 +0000424static int imx_ldb_register(struct drm_device *drm,
425 struct imx_ldb_channel *imx_ldb_ch)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800426{
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800427 struct imx_ldb *ldb = imx_ldb_ch->ldb;
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200428 struct drm_encoder *encoder = &imx_ldb_ch->encoder;
Russell King1b3f7672013-11-03 13:30:48 +0000429 int ret;
430
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200431 ret = imx_drm_encoder_parse_of(drm, encoder, imx_ldb_ch->child);
Russell King1b3f7672013-11-03 13:30:48 +0000432 if (ret)
433 return ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800434
435 ret = imx_ldb_get_clk(ldb, imx_ldb_ch->chno);
436 if (ret)
437 return ret;
Russell King1b3f7672013-11-03 13:30:48 +0000438
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800439 if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
Russell King1b3f7672013-11-03 13:30:48 +0000440 ret = imx_ldb_get_clk(ldb, 1);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800441 if (ret)
442 return ret;
443 }
444
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200445 drm_encoder_helper_add(encoder, &imx_ldb_encoder_helper_funcs);
446 drm_encoder_init(drm, encoder, &imx_ldb_encoder_funcs,
447 DRM_MODE_ENCODER_LVDS, NULL);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800448
Peter Senna Tschudindc80d702016-08-05 00:36:58 +0200449 if (imx_ldb_ch->bridge) {
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200450 ret = drm_bridge_attach(&imx_ldb_ch->encoder,
451 imx_ldb_ch->bridge, NULL);
Peter Senna Tschudindc80d702016-08-05 00:36:58 +0200452 if (ret) {
453 DRM_ERROR("Failed to initialize bridge with drm\n");
454 return ret;
455 }
456 } else {
457 /*
458 * We want to add the connector whenever there is no bridge
459 * that brings its own, not only when there is a panel. For
460 * historical reasons, the ldb driver can also work without
461 * a panel.
462 */
463 drm_connector_helper_add(&imx_ldb_ch->connector,
464 &imx_ldb_connector_helper_funcs);
Andrzej Pietrasiewiczbe0ec352019-07-26 19:23:04 +0200465 drm_connector_init_with_ddc(drm, &imx_ldb_ch->connector,
466 &imx_ldb_connector_funcs,
467 DRM_MODE_CONNECTOR_LVDS,
468 imx_ldb_ch->ddc);
Daniel Vettercde4c442018-07-09 10:40:07 +0200469 drm_connector_attach_encoder(&imx_ldb_ch->connector, encoder);
Peter Senna Tschudindc80d702016-08-05 00:36:58 +0200470 }
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800471
Lucas Stach7075ba72016-06-16 11:12:13 +0200472 if (imx_ldb_ch->panel) {
473 ret = drm_panel_attach(imx_ldb_ch->panel,
474 &imx_ldb_ch->connector);
475 if (ret)
476 return ret;
477 }
Philipp Zabel751e2672014-03-06 14:54:39 +0100478
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800479 return 0;
480}
481
482enum {
483 LVDS_BIT_MAP_SPWG,
484 LVDS_BIT_MAP_JEIDA
485};
486
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100487struct imx_ldb_bit_mapping {
488 u32 bus_format;
489 u32 datawidth;
490 const char * const mapping;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800491};
492
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100493static const struct imx_ldb_bit_mapping imx_ldb_bit_mappings[] = {
494 { MEDIA_BUS_FMT_RGB666_1X7X3_SPWG, 18, "spwg" },
495 { MEDIA_BUS_FMT_RGB888_1X7X4_SPWG, 24, "spwg" },
496 { MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA, 24, "jeida" },
497};
498
499static u32 of_get_bus_format(struct device *dev, struct device_node *np)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800500{
501 const char *bm;
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100502 u32 datawidth = 0;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800503 int ret, i;
504
505 ret = of_property_read_string(np, "fsl,data-mapping", &bm);
506 if (ret < 0)
507 return ret;
508
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100509 of_property_read_u32(np, "fsl,data-width", &datawidth);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800510
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100511 for (i = 0; i < ARRAY_SIZE(imx_ldb_bit_mappings); i++) {
512 if (!strcasecmp(bm, imx_ldb_bit_mappings[i].mapping) &&
513 datawidth == imx_ldb_bit_mappings[i].datawidth)
514 return imx_ldb_bit_mappings[i].bus_format;
515 }
516
517 dev_err(dev, "invalid data mapping: %d-bit \"%s\"\n", datawidth, bm);
518
519 return -ENOENT;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800520}
521
522static struct bus_mux imx6q_lvds_mux[2] = {
523 {
524 .reg = IOMUXC_GPR3,
525 .shift = 6,
526 .mask = IMX6Q_GPR3_LVDS0_MUX_CTL_MASK,
527 }, {
528 .reg = IOMUXC_GPR3,
529 .shift = 8,
530 .mask = IMX6Q_GPR3_LVDS1_MUX_CTL_MASK,
531 }
532};
533
534/*
535 * For a device declaring compatible = "fsl,imx6q-ldb", "fsl,imx53-ldb",
536 * of_match_device will walk through this list and take the first entry
537 * matching any of its compatible values. Therefore, the more generic
538 * entries (in this case fsl,imx53-ldb) need to be ordered last.
539 */
540static const struct of_device_id imx_ldb_dt_ids[] = {
541 { .compatible = "fsl,imx6q-ldb", .data = imx6q_lvds_mux, },
542 { .compatible = "fsl,imx53-ldb", .data = NULL, },
543 { }
544};
545MODULE_DEVICE_TABLE(of, imx_ldb_dt_ids);
546
Peter Senna Tschudindc80d702016-08-05 00:36:58 +0200547static int imx_ldb_panel_ddc(struct device *dev,
548 struct imx_ldb_channel *channel, struct device_node *child)
549{
550 struct device_node *ddc_node;
551 const u8 *edidp;
552 int ret;
553
554 ddc_node = of_parse_phandle(child, "ddc-i2c-bus", 0);
555 if (ddc_node) {
556 channel->ddc = of_find_i2c_adapter_by_node(ddc_node);
557 of_node_put(ddc_node);
558 if (!channel->ddc) {
559 dev_warn(dev, "failed to get ddc i2c adapter\n");
560 return -EPROBE_DEFER;
561 }
562 }
563
564 if (!channel->ddc) {
565 /* if no DDC available, fallback to hardcoded EDID */
566 dev_dbg(dev, "no ddc available\n");
567
568 edidp = of_get_property(child, "edid",
569 &channel->edid_len);
570 if (edidp) {
571 channel->edid = kmemdup(edidp,
572 channel->edid_len,
573 GFP_KERNEL);
574 } else if (!channel->panel) {
575 /* fallback to display-timings node */
576 ret = of_get_drm_display_mode(child,
577 &channel->mode,
578 &channel->bus_flags,
579 OF_USE_NATIVE_MODE);
580 if (!ret)
581 channel->mode_valid = 1;
582 }
583 }
584 return 0;
585}
586
Russell King17b50012013-11-03 11:23:34 +0000587static int imx_ldb_bind(struct device *dev, struct device *master, void *data)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800588{
Russell King1b3f7672013-11-03 13:30:48 +0000589 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000590 struct device_node *np = dev->of_node;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800591 const struct of_device_id *of_id =
Russell King17b50012013-11-03 11:23:34 +0000592 of_match_device(imx_ldb_dt_ids, dev);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800593 struct device_node *child;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800594 struct imx_ldb *imx_ldb;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800595 int dual;
596 int ret;
597 int i;
598
Russell King17b50012013-11-03 11:23:34 +0000599 imx_ldb = devm_kzalloc(dev, sizeof(*imx_ldb), GFP_KERNEL);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800600 if (!imx_ldb)
601 return -ENOMEM;
602
603 imx_ldb->regmap = syscon_regmap_lookup_by_phandle(np, "gpr");
604 if (IS_ERR(imx_ldb->regmap)) {
Russell King17b50012013-11-03 11:23:34 +0000605 dev_err(dev, "failed to get parent regmap\n");
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800606 return PTR_ERR(imx_ldb->regmap);
607 }
608
Lucas Stachb5826232018-04-11 17:31:35 +0200609 /* disable LDB by resetting the control register to POR default */
610 regmap_write(imx_ldb->regmap, IOMUXC_GPR2, 0);
611
Russell King17b50012013-11-03 11:23:34 +0000612 imx_ldb->dev = dev;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800613
614 if (of_id)
615 imx_ldb->lvds_mux = of_id->data;
616
617 dual = of_property_read_bool(np, "fsl,dual-channel");
618 if (dual)
619 imx_ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN;
620
621 /*
Masanari Iida45999342013-07-24 01:05:07 +0900622 * There are three different possible clock mux configurations:
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800623 * i.MX53: ipu1_di0_sel, ipu1_di1_sel
624 * i.MX6q: ipu1_di0_sel, ipu1_di1_sel, ipu2_di0_sel, ipu2_di1_sel
625 * i.MX6dl: ipu1_di0_sel, ipu1_di1_sel, lcdif_sel
626 * Map them all to di0_sel...di3_sel.
627 */
628 for (i = 0; i < 4; i++) {
629 char clkname[16];
630
631 sprintf(clkname, "di%d_sel", i);
632 imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
633 if (IS_ERR(imx_ldb->clk_sel[i])) {
634 ret = PTR_ERR(imx_ldb->clk_sel[i]);
635 imx_ldb->clk_sel[i] = NULL;
636 break;
637 }
Philipp Zabel3973aff2014-11-26 13:59:11 +0100638
639 imx_ldb->clk_parent[i] = clk_get_parent(imx_ldb->clk_sel[i]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800640 }
641 if (i == 0)
642 return ret;
643
644 for_each_child_of_node(np, child) {
645 struct imx_ldb_channel *channel;
Liu Ying032003c2016-07-08 17:40:58 +0800646 int bus_format;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800647
648 ret = of_property_read_u32(child, "reg", &i);
Julia Lawallaa3312012019-01-13 09:47:42 +0100649 if (ret || i < 0 || i > 1) {
650 ret = -EINVAL;
651 goto free_child;
652 }
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800653
Lucas Stachc80d6732018-04-11 17:31:36 +0200654 if (!of_device_is_available(child))
655 continue;
656
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800657 if (dual && i > 0) {
Russell King17b50012013-11-03 11:23:34 +0000658 dev_warn(dev, "dual-channel mode, ignoring second output\n");
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800659 continue;
660 }
661
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800662 channel = &imx_ldb->channel[i];
663 channel->ldb = imx_ldb;
664 channel->chno = i;
665
Philipp Zabel751e2672014-03-06 14:54:39 +0100666 /*
667 * The output port is port@4 with an external 4-port mux or
668 * port@2 with the internal 2-port mux.
669 */
Rob Herringebc94462017-03-29 13:55:46 -0500670 ret = drm_of_find_panel_or_bridge(child,
671 imx_ldb->lvds_mux ? 4 : 2, 0,
672 &channel->panel, &channel->bridge);
Leonard Cresteze36aecb2017-05-10 16:17:13 +0300673 if (ret && ret != -ENODEV)
Julia Lawallaa3312012019-01-13 09:47:42 +0100674 goto free_child;
Philipp Zabel751e2672014-03-06 14:54:39 +0100675
Peter Senna Tschudindc80d702016-08-05 00:36:58 +0200676 /* panel ddc only if there is no bridge */
677 if (!channel->bridge) {
678 ret = imx_ldb_panel_ddc(dev, channel, child);
679 if (ret)
Julia Lawallaa3312012019-01-13 09:47:42 +0100680 goto free_child;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800681 }
682
Liu Ying032003c2016-07-08 17:40:58 +0800683 bus_format = of_get_bus_format(dev, child);
684 if (bus_format == -EINVAL) {
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100685 /*
686 * If no bus format was specified in the device tree,
687 * we can still get it from the connected panel later.
688 */
689 if (channel->panel && channel->panel->funcs &&
690 channel->panel->funcs->get_modes)
Liu Ying032003c2016-07-08 17:40:58 +0800691 bus_format = 0;
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100692 }
Liu Ying032003c2016-07-08 17:40:58 +0800693 if (bus_format < 0) {
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100694 dev_err(dev, "could not determine data mapping: %d\n",
Liu Ying032003c2016-07-08 17:40:58 +0800695 bus_format);
Julia Lawallaa3312012019-01-13 09:47:42 +0100696 ret = bus_format;
697 goto free_child;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800698 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200699 channel->bus_format = bus_format;
Julia Lawallaa3312012019-01-13 09:47:42 +0100700 channel->child = child;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800701
Russell King1b3f7672013-11-03 13:30:48 +0000702 ret = imx_ldb_register(drm, channel);
Julia Lawallaa3312012019-01-13 09:47:42 +0100703 if (ret) {
704 channel->child = NULL;
705 goto free_child;
706 }
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800707 }
708
Russell King17b50012013-11-03 11:23:34 +0000709 dev_set_drvdata(dev, imx_ldb);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800710
711 return 0;
Julia Lawallaa3312012019-01-13 09:47:42 +0100712
713free_child:
714 of_node_put(child);
715 return ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800716}
717
Russell King17b50012013-11-03 11:23:34 +0000718static void imx_ldb_unbind(struct device *dev, struct device *master,
719 void *data)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800720{
Russell King17b50012013-11-03 11:23:34 +0000721 struct imx_ldb *imx_ldb = dev_get_drvdata(dev);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800722 int i;
723
724 for (i = 0; i < 2; i++) {
725 struct imx_ldb_channel *channel = &imx_ldb->channel[i];
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800726
Lucas Stachb1318d52016-06-16 11:19:41 +0200727 if (channel->panel)
728 drm_panel_detach(channel->panel);
729
Peter Seidererf4876ff2013-12-08 22:03:57 +0100730 kfree(channel->edid);
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400731 i2c_put_adapter(channel->ddc);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800732 }
Russell King17b50012013-11-03 11:23:34 +0000733}
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800734
Russell King17b50012013-11-03 11:23:34 +0000735static const struct component_ops imx_ldb_ops = {
736 .bind = imx_ldb_bind,
737 .unbind = imx_ldb_unbind,
738};
739
740static int imx_ldb_probe(struct platform_device *pdev)
741{
742 return component_add(&pdev->dev, &imx_ldb_ops);
743}
744
745static int imx_ldb_remove(struct platform_device *pdev)
746{
747 component_del(&pdev->dev, &imx_ldb_ops);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800748 return 0;
749}
750
751static struct platform_driver imx_ldb_driver = {
752 .probe = imx_ldb_probe,
753 .remove = imx_ldb_remove,
754 .driver = {
755 .of_match_table = imx_ldb_dt_ids,
756 .name = DRIVER_NAME,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800757 },
758};
759
760module_platform_driver(imx_ldb_driver);
761
762MODULE_DESCRIPTION("i.MX LVDS driver");
763MODULE_AUTHOR("Sascha Hauer, Pengutronix");
764MODULE_LICENSE("GPL");
Fabio Estevambc627382013-08-18 21:40:03 -0300765MODULE_ALIAS("platform:" DRIVER_NAME);