blob: 9c48c4b23a8cd49a4f09d8c4417b06576a929657 [file] [log] [blame]
Sascha Hauerac4c1a92013-06-18 09:23:57 +08001/*
2 * i.MX drm driver - LVDS display bridge
3 *
4 * Copyright (C) 2012 Sascha Hauer, Pengutronix
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Sascha Hauerac4c1a92013-06-18 09:23:57 +080014 */
15
16#include <linux/module.h>
17#include <linux/clk.h>
Russell King17b50012013-11-03 11:23:34 +000018#include <linux/component.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080019#include <drm/drmP.h>
Liu Ying255c35f2016-07-08 17:40:56 +080020#include <drm/drm_atomic_helper.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080021#include <drm/drm_fb_helper.h>
22#include <drm/drm_crtc_helper.h>
Philipp Zabel53141e42015-02-24 11:41:28 +010023#include <drm/drm_of.h>
Philipp Zabel751e2672014-03-06 14:54:39 +010024#include <drm/drm_panel.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080025#include <linux/mfd/syscon.h>
26#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080027#include <linux/of_device.h>
Philipp Zabel751e2672014-03-06 14:54:39 +010028#include <linux/of_graph.h>
Lothar Waßmannc82b4d72016-05-24 08:31:49 +020029#include <video/of_display_timing.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080030#include <video/of_videomode.h>
31#include <linux/regmap.h>
32#include <linux/videodev2.h>
33
34#include "imx-drm.h"
35
36#define DRIVER_NAME "imx-ldb"
37
38#define LDB_CH0_MODE_EN_TO_DI0 (1 << 0)
39#define LDB_CH0_MODE_EN_TO_DI1 (3 << 0)
40#define LDB_CH0_MODE_EN_MASK (3 << 0)
41#define LDB_CH1_MODE_EN_TO_DI0 (1 << 2)
42#define LDB_CH1_MODE_EN_TO_DI1 (3 << 2)
43#define LDB_CH1_MODE_EN_MASK (3 << 2)
44#define LDB_SPLIT_MODE_EN (1 << 4)
45#define LDB_DATA_WIDTH_CH0_24 (1 << 5)
46#define LDB_BIT_MAP_CH0_JEIDA (1 << 6)
47#define LDB_DATA_WIDTH_CH1_24 (1 << 7)
48#define LDB_BIT_MAP_CH1_JEIDA (1 << 8)
49#define LDB_DI0_VS_POL_ACT_LOW (1 << 9)
50#define LDB_DI1_VS_POL_ACT_LOW (1 << 10)
51#define LDB_BGREF_RMODE_INT (1 << 15)
52
53#define con_to_imx_ldb_ch(x) container_of(x, struct imx_ldb_channel, connector)
Liu Ying032003c2016-07-08 17:40:58 +080054#define imx_enc_to_imx_ldb_ch(x) \
55 container_of(x, struct imx_ldb_channel, imx_encoder)
Sascha Hauerac4c1a92013-06-18 09:23:57 +080056
57struct imx_ldb;
58
59struct imx_ldb_channel {
60 struct imx_ldb *ldb;
61 struct drm_connector connector;
Liu Ying032003c2016-07-08 17:40:58 +080062 struct imx_drm_encoder imx_encoder;
Philipp Zabel751e2672014-03-06 14:54:39 +010063 struct drm_panel *panel;
Russell King1b3f7672013-11-03 13:30:48 +000064 struct device_node *child;
Steve Longerbeama6d206e2016-04-27 16:23:33 -040065 struct i2c_adapter *ddc;
Sascha Hauerac4c1a92013-06-18 09:23:57 +080066 int chno;
67 void *edid;
68 int edid_len;
69 struct drm_display_mode mode;
70 int mode_valid;
71};
72
73struct bus_mux {
74 int reg;
75 int shift;
76 int mask;
77};
78
79struct imx_ldb {
80 struct regmap *regmap;
81 struct device *dev;
82 struct imx_ldb_channel channel[2];
83 struct clk *clk[2]; /* our own clock */
84 struct clk *clk_sel[4]; /* parent of display clock */
Philipp Zabel3973aff2014-11-26 13:59:11 +010085 struct clk *clk_parent[4]; /* original parent of clk_sel */
Sascha Hauerac4c1a92013-06-18 09:23:57 +080086 struct clk *clk_pll[2]; /* upstream clock we can adjust */
87 u32 ldb_ctrl;
88 const struct bus_mux *lvds_mux;
89};
90
91static enum drm_connector_status imx_ldb_connector_detect(
92 struct drm_connector *connector, bool force)
93{
94 return connector_status_connected;
95}
96
Liu Ying032003c2016-07-08 17:40:58 +080097static void imx_ldb_bus_format_translation(struct imx_ldb_channel *imx_ldb_ch,
98 u32 bus_format)
99{
100 struct imx_ldb *ldb = imx_ldb_ch->ldb;
101 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
102
103 switch (bus_format) {
104 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
105 imx_ldb_ch->imx_encoder.bus_format = MEDIA_BUS_FMT_RGB666_1X18;
106 break;
107 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
108 imx_ldb_ch->imx_encoder.bus_format = MEDIA_BUS_FMT_RGB888_1X24;
109 if (imx_ldb_ch->chno == 0 || dual)
110 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24;
111 if (imx_ldb_ch->chno == 1 || dual)
112 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24;
113 break;
114 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
115 imx_ldb_ch->imx_encoder.bus_format = MEDIA_BUS_FMT_RGB888_1X24;
116 if (imx_ldb_ch->chno == 0 || dual)
117 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24 |
118 LDB_BIT_MAP_CH0_JEIDA;
119 if (imx_ldb_ch->chno == 1 || dual)
120 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24 |
121 LDB_BIT_MAP_CH1_JEIDA;
122 break;
123 }
124}
125
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800126static int imx_ldb_connector_get_modes(struct drm_connector *connector)
127{
128 struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
129 int num_modes = 0;
130
Philipp Zabel751e2672014-03-06 14:54:39 +0100131 if (imx_ldb_ch->panel && imx_ldb_ch->panel->funcs &&
132 imx_ldb_ch->panel->funcs->get_modes) {
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100133 struct drm_display_info *di = &connector->display_info;
134
Philipp Zabel751e2672014-03-06 14:54:39 +0100135 num_modes = imx_ldb_ch->panel->funcs->get_modes(imx_ldb_ch->panel);
Liu Ying032003c2016-07-08 17:40:58 +0800136 if (!imx_ldb_ch->imx_encoder.bus_format && di->num_bus_formats)
137 imx_ldb_bus_format_translation(imx_ldb_ch,
138 di->bus_formats[0]);
Philipp Zabel751e2672014-03-06 14:54:39 +0100139 if (num_modes > 0)
140 return num_modes;
141 }
142
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400143 if (!imx_ldb_ch->edid && imx_ldb_ch->ddc)
144 imx_ldb_ch->edid = drm_get_edid(connector, imx_ldb_ch->ddc);
145
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800146 if (imx_ldb_ch->edid) {
147 drm_mode_connector_update_edid_property(connector,
148 imx_ldb_ch->edid);
149 num_modes = drm_add_edid_modes(connector, imx_ldb_ch->edid);
150 }
151
152 if (imx_ldb_ch->mode_valid) {
153 struct drm_display_mode *mode;
154
155 mode = drm_mode_create(connector->dev);
Fabio Estevam9f9b0362014-02-26 20:53:40 -0300156 if (!mode)
157 return -EINVAL;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800158 drm_mode_copy(mode, &imx_ldb_ch->mode);
159 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
160 drm_mode_probed_add(connector, mode);
161 num_modes++;
162 }
163
164 return num_modes;
165}
166
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800167static struct drm_encoder *imx_ldb_connector_best_encoder(
168 struct drm_connector *connector)
169{
170 struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
171
Liu Ying032003c2016-07-08 17:40:58 +0800172 return &imx_ldb_ch->imx_encoder.encoder;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800173}
174
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800175static void imx_ldb_set_clock(struct imx_ldb *ldb, int mux, int chno,
176 unsigned long serial_clk, unsigned long di_clk)
177{
178 int ret;
179
180 dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
181 clk_get_rate(ldb->clk_pll[chno]), serial_clk);
182 clk_set_rate(ldb->clk_pll[chno], serial_clk);
183
184 dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
185 clk_get_rate(ldb->clk_pll[chno]));
186
187 dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
188 clk_get_rate(ldb->clk[chno]),
189 (long int)di_clk);
190 clk_set_rate(ldb->clk[chno], di_clk);
191
192 dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
193 clk_get_rate(ldb->clk[chno]));
194
195 /* set display clock mux to LDB input clock */
196 ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk[chno]);
Sima Baymani49f4a9c2013-11-03 11:18:57 +0100197 if (ret)
Aybuke Ozdemire5e1b162014-03-17 23:55:53 +0200198 dev_err(ldb->dev,
199 "unable to set di%d parent clock to ldb_di%d\n", mux,
200 chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800201}
202
Liu Yingf6e396e2016-07-08 17:41:01 +0800203static void imx_ldb_encoder_enable(struct drm_encoder *encoder)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800204{
Liu Ying032003c2016-07-08 17:40:58 +0800205 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
206 struct imx_ldb_channel *imx_ldb_ch = imx_enc_to_imx_ldb_ch(imx_encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800207 struct imx_ldb *ldb = imx_ldb_ch->ldb;
208 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
Philipp Zabel53141e42015-02-24 11:41:28 +0100209 int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800210
Philipp Zabel751e2672014-03-06 14:54:39 +0100211 drm_panel_prepare(imx_ldb_ch->panel);
212
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800213 if (dual) {
Liu Yingf6e396e2016-07-08 17:41:01 +0800214 clk_set_parent(ldb->clk_sel[mux], ldb->clk[0]);
215 clk_set_parent(ldb->clk_sel[mux], ldb->clk[1]);
216
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800217 clk_prepare_enable(ldb->clk[0]);
218 clk_prepare_enable(ldb->clk[1]);
Liu Yingf6e396e2016-07-08 17:41:01 +0800219 } else {
220 clk_set_parent(ldb->clk_sel[mux], ldb->clk[imx_ldb_ch->chno]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800221 }
222
223 if (imx_ldb_ch == &ldb->channel[0] || dual) {
224 ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
225 if (mux == 0 || ldb->lvds_mux)
226 ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI0;
227 else if (mux == 1)
228 ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI1;
229 }
230 if (imx_ldb_ch == &ldb->channel[1] || dual) {
231 ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
232 if (mux == 1 || ldb->lvds_mux)
233 ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI1;
234 else if (mux == 0)
235 ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI0;
236 }
237
238 if (ldb->lvds_mux) {
239 const struct bus_mux *lvds_mux = NULL;
240
241 if (imx_ldb_ch == &ldb->channel[0])
242 lvds_mux = &ldb->lvds_mux[0];
243 else if (imx_ldb_ch == &ldb->channel[1])
244 lvds_mux = &ldb->lvds_mux[1];
245
246 regmap_update_bits(ldb->regmap, lvds_mux->reg, lvds_mux->mask,
247 mux << lvds_mux->shift);
248 }
249
250 regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
Philipp Zabel751e2672014-03-06 14:54:39 +0100251
252 drm_panel_enable(imx_ldb_ch->panel);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800253}
254
255static void imx_ldb_encoder_mode_set(struct drm_encoder *encoder,
Steve Longerbeameb10d632014-12-18 18:00:24 -0800256 struct drm_display_mode *orig_mode,
257 struct drm_display_mode *mode)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800258{
Liu Ying032003c2016-07-08 17:40:58 +0800259 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
260 struct imx_ldb_channel *imx_ldb_ch = imx_enc_to_imx_ldb_ch(imx_encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800261 struct imx_ldb *ldb = imx_ldb_ch->ldb;
262 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
Philipp Zabel51dac942015-01-23 17:10:01 +0100263 unsigned long serial_clk;
264 unsigned long di_clk = mode->clock * 1000;
Philipp Zabel53141e42015-02-24 11:41:28 +0100265 int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800266
267 if (mode->clock > 170000) {
268 dev_warn(ldb->dev,
269 "%s: mode exceeds 170 MHz pixel clock\n", __func__);
270 }
271 if (mode->clock > 85000 && !dual) {
272 dev_warn(ldb->dev,
273 "%s: mode exceeds 85 MHz pixel clock\n", __func__);
274 }
275
Philipp Zabel51dac942015-01-23 17:10:01 +0100276 if (dual) {
277 serial_clk = 3500UL * mode->clock;
278 imx_ldb_set_clock(ldb, mux, 0, serial_clk, di_clk);
279 imx_ldb_set_clock(ldb, mux, 1, serial_clk, di_clk);
280 } else {
281 serial_clk = 7000UL * mode->clock;
282 imx_ldb_set_clock(ldb, mux, imx_ldb_ch->chno, serial_clk,
283 di_clk);
284 }
285
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800286 /* FIXME - assumes straight connections DI0 --> CH0, DI1 --> CH1 */
287 if (imx_ldb_ch == &ldb->channel[0]) {
288 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
289 ldb->ldb_ctrl |= LDB_DI0_VS_POL_ACT_LOW;
290 else if (mode->flags & DRM_MODE_FLAG_PVSYNC)
291 ldb->ldb_ctrl &= ~LDB_DI0_VS_POL_ACT_LOW;
292 }
293 if (imx_ldb_ch == &ldb->channel[1]) {
294 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
295 ldb->ldb_ctrl |= LDB_DI1_VS_POL_ACT_LOW;
296 else if (mode->flags & DRM_MODE_FLAG_PVSYNC)
297 ldb->ldb_ctrl &= ~LDB_DI1_VS_POL_ACT_LOW;
298 }
299}
300
301static void imx_ldb_encoder_disable(struct drm_encoder *encoder)
302{
Liu Ying032003c2016-07-08 17:40:58 +0800303 struct imx_drm_encoder *imx_encoder = enc_to_imx_enc(encoder);
304 struct imx_ldb_channel *imx_ldb_ch = imx_enc_to_imx_ldb_ch(imx_encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800305 struct imx_ldb *ldb = imx_ldb_ch->ldb;
Philipp Zabel3973aff2014-11-26 13:59:11 +0100306 int mux, ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800307
308 /*
309 * imx_ldb_encoder_disable is called by
310 * drm_helper_disable_unused_functions without
311 * the encoder being enabled before.
312 */
313 if (imx_ldb_ch == &ldb->channel[0] &&
314 (ldb->ldb_ctrl & LDB_CH0_MODE_EN_MASK) == 0)
315 return;
316 else if (imx_ldb_ch == &ldb->channel[1] &&
317 (ldb->ldb_ctrl & LDB_CH1_MODE_EN_MASK) == 0)
318 return;
319
Philipp Zabel751e2672014-03-06 14:54:39 +0100320 drm_panel_disable(imx_ldb_ch->panel);
321
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800322 if (imx_ldb_ch == &ldb->channel[0])
323 ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
324 else if (imx_ldb_ch == &ldb->channel[1])
325 ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
326
327 regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
328
329 if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
330 clk_disable_unprepare(ldb->clk[0]);
331 clk_disable_unprepare(ldb->clk[1]);
332 }
Philipp Zabel751e2672014-03-06 14:54:39 +0100333
Philipp Zabel3973aff2014-11-26 13:59:11 +0100334 if (ldb->lvds_mux) {
335 const struct bus_mux *lvds_mux = NULL;
336
337 if (imx_ldb_ch == &ldb->channel[0])
338 lvds_mux = &ldb->lvds_mux[0];
339 else if (imx_ldb_ch == &ldb->channel[1])
340 lvds_mux = &ldb->lvds_mux[1];
341
342 regmap_read(ldb->regmap, lvds_mux->reg, &mux);
343 mux &= lvds_mux->mask;
344 mux >>= lvds_mux->shift;
345 } else {
346 mux = (imx_ldb_ch == &ldb->channel[0]) ? 0 : 1;
347 }
348
349 /* set display clock mux back to original input clock */
350 ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk_parent[mux]);
351 if (ret)
352 dev_err(ldb->dev,
353 "unable to set di%d parent clock to original parent\n",
354 mux);
355
Philipp Zabel751e2672014-03-06 14:54:39 +0100356 drm_panel_unprepare(imx_ldb_ch->panel);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800357}
358
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100359static const struct drm_connector_funcs imx_ldb_connector_funcs = {
Liu Yingf6e396e2016-07-08 17:41:01 +0800360 .dpms = drm_atomic_helper_connector_dpms,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800361 .fill_modes = drm_helper_probe_single_connector_modes,
362 .detect = imx_ldb_connector_detect,
Russell King1b3f7672013-11-03 13:30:48 +0000363 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800364 .reset = drm_atomic_helper_connector_reset,
365 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
366 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800367};
368
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100369static const struct drm_connector_helper_funcs imx_ldb_connector_helper_funcs = {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800370 .get_modes = imx_ldb_connector_get_modes,
371 .best_encoder = imx_ldb_connector_best_encoder,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800372};
373
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100374static const struct drm_encoder_funcs imx_ldb_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000375 .destroy = imx_drm_encoder_destroy,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800376};
377
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100378static const struct drm_encoder_helper_funcs imx_ldb_encoder_helper_funcs = {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800379 .mode_set = imx_ldb_encoder_mode_set,
Liu Yingf6e396e2016-07-08 17:41:01 +0800380 .enable = imx_ldb_encoder_enable,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800381 .disable = imx_ldb_encoder_disable,
382};
383
384static int imx_ldb_get_clk(struct imx_ldb *ldb, int chno)
385{
386 char clkname[16];
387
Fabio Estevam98dd3b22014-02-28 11:39:42 -0300388 snprintf(clkname, sizeof(clkname), "di%d", chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800389 ldb->clk[chno] = devm_clk_get(ldb->dev, clkname);
390 if (IS_ERR(ldb->clk[chno]))
391 return PTR_ERR(ldb->clk[chno]);
392
Fabio Estevam98dd3b22014-02-28 11:39:42 -0300393 snprintf(clkname, sizeof(clkname), "di%d_pll", chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800394 ldb->clk_pll[chno] = devm_clk_get(ldb->dev, clkname);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800395
Valentina Manea1f933fa2013-10-23 10:29:55 +0300396 return PTR_ERR_OR_ZERO(ldb->clk_pll[chno]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800397}
398
Russell King1b3f7672013-11-03 13:30:48 +0000399static int imx_ldb_register(struct drm_device *drm,
400 struct imx_ldb_channel *imx_ldb_ch)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800401{
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800402 struct imx_ldb *ldb = imx_ldb_ch->ldb;
Russell King1b3f7672013-11-03 13:30:48 +0000403 int ret;
404
Liu Ying032003c2016-07-08 17:40:58 +0800405 ret = imx_drm_encoder_parse_of(drm, &imx_ldb_ch->imx_encoder.encoder,
Russell King1b3f7672013-11-03 13:30:48 +0000406 imx_ldb_ch->child);
407 if (ret)
408 return ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800409
410 ret = imx_ldb_get_clk(ldb, imx_ldb_ch->chno);
411 if (ret)
412 return ret;
Russell King1b3f7672013-11-03 13:30:48 +0000413
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800414 if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
Russell King1b3f7672013-11-03 13:30:48 +0000415 ret = imx_ldb_get_clk(ldb, 1);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800416 if (ret)
417 return ret;
418 }
419
Liu Ying032003c2016-07-08 17:40:58 +0800420 drm_encoder_helper_add(&imx_ldb_ch->imx_encoder.encoder,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800421 &imx_ldb_encoder_helper_funcs);
Liu Ying032003c2016-07-08 17:40:58 +0800422 drm_encoder_init(drm, &imx_ldb_ch->imx_encoder.encoder,
423 &imx_ldb_encoder_funcs, DRM_MODE_ENCODER_LVDS, NULL);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800424
425 drm_connector_helper_add(&imx_ldb_ch->connector,
426 &imx_ldb_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000427 drm_connector_init(drm, &imx_ldb_ch->connector,
428 &imx_ldb_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800429
Philipp Zabel751e2672014-03-06 14:54:39 +0100430 if (imx_ldb_ch->panel)
431 drm_panel_attach(imx_ldb_ch->panel, &imx_ldb_ch->connector);
432
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800433 drm_mode_connector_attach_encoder(&imx_ldb_ch->connector,
Liu Ying032003c2016-07-08 17:40:58 +0800434 &imx_ldb_ch->imx_encoder.encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800435
436 return 0;
437}
438
439enum {
440 LVDS_BIT_MAP_SPWG,
441 LVDS_BIT_MAP_JEIDA
442};
443
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100444struct imx_ldb_bit_mapping {
445 u32 bus_format;
446 u32 datawidth;
447 const char * const mapping;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800448};
449
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100450static const struct imx_ldb_bit_mapping imx_ldb_bit_mappings[] = {
451 { MEDIA_BUS_FMT_RGB666_1X7X3_SPWG, 18, "spwg" },
452 { MEDIA_BUS_FMT_RGB888_1X7X4_SPWG, 24, "spwg" },
453 { MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA, 24, "jeida" },
454};
455
456static u32 of_get_bus_format(struct device *dev, struct device_node *np)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800457{
458 const char *bm;
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100459 u32 datawidth = 0;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800460 int ret, i;
461
462 ret = of_property_read_string(np, "fsl,data-mapping", &bm);
463 if (ret < 0)
464 return ret;
465
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100466 of_property_read_u32(np, "fsl,data-width", &datawidth);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800467
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100468 for (i = 0; i < ARRAY_SIZE(imx_ldb_bit_mappings); i++) {
469 if (!strcasecmp(bm, imx_ldb_bit_mappings[i].mapping) &&
470 datawidth == imx_ldb_bit_mappings[i].datawidth)
471 return imx_ldb_bit_mappings[i].bus_format;
472 }
473
474 dev_err(dev, "invalid data mapping: %d-bit \"%s\"\n", datawidth, bm);
475
476 return -ENOENT;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800477}
478
479static struct bus_mux imx6q_lvds_mux[2] = {
480 {
481 .reg = IOMUXC_GPR3,
482 .shift = 6,
483 .mask = IMX6Q_GPR3_LVDS0_MUX_CTL_MASK,
484 }, {
485 .reg = IOMUXC_GPR3,
486 .shift = 8,
487 .mask = IMX6Q_GPR3_LVDS1_MUX_CTL_MASK,
488 }
489};
490
491/*
492 * For a device declaring compatible = "fsl,imx6q-ldb", "fsl,imx53-ldb",
493 * of_match_device will walk through this list and take the first entry
494 * matching any of its compatible values. Therefore, the more generic
495 * entries (in this case fsl,imx53-ldb) need to be ordered last.
496 */
497static const struct of_device_id imx_ldb_dt_ids[] = {
498 { .compatible = "fsl,imx6q-ldb", .data = imx6q_lvds_mux, },
499 { .compatible = "fsl,imx53-ldb", .data = NULL, },
500 { }
501};
502MODULE_DEVICE_TABLE(of, imx_ldb_dt_ids);
503
Russell King17b50012013-11-03 11:23:34 +0000504static int imx_ldb_bind(struct device *dev, struct device *master, void *data)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800505{
Russell King1b3f7672013-11-03 13:30:48 +0000506 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000507 struct device_node *np = dev->of_node;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800508 const struct of_device_id *of_id =
Russell King17b50012013-11-03 11:23:34 +0000509 of_match_device(imx_ldb_dt_ids, dev);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800510 struct device_node *child;
511 const u8 *edidp;
512 struct imx_ldb *imx_ldb;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800513 int dual;
514 int ret;
515 int i;
516
Russell King17b50012013-11-03 11:23:34 +0000517 imx_ldb = devm_kzalloc(dev, sizeof(*imx_ldb), GFP_KERNEL);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800518 if (!imx_ldb)
519 return -ENOMEM;
520
521 imx_ldb->regmap = syscon_regmap_lookup_by_phandle(np, "gpr");
522 if (IS_ERR(imx_ldb->regmap)) {
Russell King17b50012013-11-03 11:23:34 +0000523 dev_err(dev, "failed to get parent regmap\n");
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800524 return PTR_ERR(imx_ldb->regmap);
525 }
526
Russell King17b50012013-11-03 11:23:34 +0000527 imx_ldb->dev = dev;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800528
529 if (of_id)
530 imx_ldb->lvds_mux = of_id->data;
531
532 dual = of_property_read_bool(np, "fsl,dual-channel");
533 if (dual)
534 imx_ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN;
535
536 /*
Masanari Iida45999342013-07-24 01:05:07 +0900537 * There are three different possible clock mux configurations:
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800538 * i.MX53: ipu1_di0_sel, ipu1_di1_sel
539 * i.MX6q: ipu1_di0_sel, ipu1_di1_sel, ipu2_di0_sel, ipu2_di1_sel
540 * i.MX6dl: ipu1_di0_sel, ipu1_di1_sel, lcdif_sel
541 * Map them all to di0_sel...di3_sel.
542 */
543 for (i = 0; i < 4; i++) {
544 char clkname[16];
545
546 sprintf(clkname, "di%d_sel", i);
547 imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
548 if (IS_ERR(imx_ldb->clk_sel[i])) {
549 ret = PTR_ERR(imx_ldb->clk_sel[i]);
550 imx_ldb->clk_sel[i] = NULL;
551 break;
552 }
Philipp Zabel3973aff2014-11-26 13:59:11 +0100553
554 imx_ldb->clk_parent[i] = clk_get_parent(imx_ldb->clk_sel[i]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800555 }
556 if (i == 0)
557 return ret;
558
559 for_each_child_of_node(np, child) {
560 struct imx_ldb_channel *channel;
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400561 struct device_node *ddc_node;
Philipp Zabel620011e2016-05-03 14:37:13 +0200562 struct device_node *ep;
Liu Ying032003c2016-07-08 17:40:58 +0800563 int bus_format;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800564
565 ret = of_property_read_u32(child, "reg", &i);
566 if (ret || i < 0 || i > 1)
567 return -EINVAL;
568
569 if (dual && i > 0) {
Russell King17b50012013-11-03 11:23:34 +0000570 dev_warn(dev, "dual-channel mode, ignoring second output\n");
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800571 continue;
572 }
573
574 if (!of_device_is_available(child))
575 continue;
576
577 channel = &imx_ldb->channel[i];
578 channel->ldb = imx_ldb;
579 channel->chno = i;
Russell King1b3f7672013-11-03 13:30:48 +0000580 channel->child = child;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800581
Philipp Zabel751e2672014-03-06 14:54:39 +0100582 /*
583 * The output port is port@4 with an external 4-port mux or
584 * port@2 with the internal 2-port mux.
585 */
Philipp Zabel620011e2016-05-03 14:37:13 +0200586 ep = of_graph_get_endpoint_by_regs(child,
587 imx_ldb->lvds_mux ? 4 : 2,
588 -1);
589 if (ep) {
590 struct device_node *remote;
Philipp Zabel751e2672014-03-06 14:54:39 +0100591
Philipp Zabel620011e2016-05-03 14:37:13 +0200592 remote = of_graph_get_remote_port_parent(ep);
593 of_node_put(ep);
594 if (remote)
595 channel->panel = of_drm_find_panel(remote);
596 else
597 return -EPROBE_DEFER;
598 of_node_put(remote);
599 if (!channel->panel) {
600 dev_err(dev, "panel not found: %s\n",
601 remote->full_name);
602 return -EPROBE_DEFER;
Philipp Zabel751e2672014-03-06 14:54:39 +0100603 }
604 }
605
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400606 ddc_node = of_parse_phandle(child, "ddc-i2c-bus", 0);
607 if (ddc_node) {
608 channel->ddc = of_find_i2c_adapter_by_node(ddc_node);
609 of_node_put(ddc_node);
610 if (!channel->ddc) {
611 dev_warn(dev, "failed to get ddc i2c adapter\n");
612 return -EPROBE_DEFER;
613 }
614 }
615
616 if (!channel->ddc) {
617 /* if no DDC available, fallback to hardcoded EDID */
618 dev_dbg(dev, "no ddc available\n");
619
620 edidp = of_get_property(child, "edid",
621 &channel->edid_len);
622 if (edidp) {
623 channel->edid = kmemdup(edidp,
624 channel->edid_len,
625 GFP_KERNEL);
626 } else if (!channel->panel) {
627 /* fallback to display-timings node */
628 ret = of_get_drm_display_mode(child,
629 &channel->mode,
Lothar Waßmannc82b4d72016-05-24 08:31:49 +0200630 OF_USE_NATIVE_MODE);
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400631 if (!ret)
632 channel->mode_valid = 1;
633 }
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800634 }
635
Liu Ying032003c2016-07-08 17:40:58 +0800636 bus_format = of_get_bus_format(dev, child);
637 if (bus_format == -EINVAL) {
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100638 /*
639 * If no bus format was specified in the device tree,
640 * we can still get it from the connected panel later.
641 */
642 if (channel->panel && channel->panel->funcs &&
643 channel->panel->funcs->get_modes)
Liu Ying032003c2016-07-08 17:40:58 +0800644 bus_format = 0;
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100645 }
Liu Ying032003c2016-07-08 17:40:58 +0800646 if (bus_format < 0) {
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100647 dev_err(dev, "could not determine data mapping: %d\n",
Liu Ying032003c2016-07-08 17:40:58 +0800648 bus_format);
649 return bus_format;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800650 }
Liu Ying032003c2016-07-08 17:40:58 +0800651 imx_ldb_bus_format_translation(channel, bus_format);
652
653 channel->imx_encoder.di_hsync_pin = 2;
654 channel->imx_encoder.di_vsync_pin = 3;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800655
Russell King1b3f7672013-11-03 13:30:48 +0000656 ret = imx_ldb_register(drm, channel);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800657 if (ret)
658 return ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800659 }
660
Russell King17b50012013-11-03 11:23:34 +0000661 dev_set_drvdata(dev, imx_ldb);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800662
663 return 0;
664}
665
Russell King17b50012013-11-03 11:23:34 +0000666static void imx_ldb_unbind(struct device *dev, struct device *master,
667 void *data)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800668{
Russell King17b50012013-11-03 11:23:34 +0000669 struct imx_ldb *imx_ldb = dev_get_drvdata(dev);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800670 int i;
671
672 for (i = 0; i < 2; i++) {
673 struct imx_ldb_channel *channel = &imx_ldb->channel[i];
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800674
Russell Kingd9fdb9f2014-09-01 18:07:38 +0100675 if (!channel->connector.funcs)
676 continue;
677
Russell King1b3f7672013-11-03 13:30:48 +0000678 channel->connector.funcs->destroy(&channel->connector);
Liu Ying032003c2016-07-08 17:40:58 +0800679 channel->imx_encoder.encoder.funcs->destroy(
680 &channel->imx_encoder.encoder);
Peter Seidererf4876ff2013-12-08 22:03:57 +0100681
682 kfree(channel->edid);
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400683 i2c_put_adapter(channel->ddc);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800684 }
Russell King17b50012013-11-03 11:23:34 +0000685}
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800686
Russell King17b50012013-11-03 11:23:34 +0000687static const struct component_ops imx_ldb_ops = {
688 .bind = imx_ldb_bind,
689 .unbind = imx_ldb_unbind,
690};
691
692static int imx_ldb_probe(struct platform_device *pdev)
693{
694 return component_add(&pdev->dev, &imx_ldb_ops);
695}
696
697static int imx_ldb_remove(struct platform_device *pdev)
698{
699 component_del(&pdev->dev, &imx_ldb_ops);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800700 return 0;
701}
702
703static struct platform_driver imx_ldb_driver = {
704 .probe = imx_ldb_probe,
705 .remove = imx_ldb_remove,
706 .driver = {
707 .of_match_table = imx_ldb_dt_ids,
708 .name = DRIVER_NAME,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800709 },
710};
711
712module_platform_driver(imx_ldb_driver);
713
714MODULE_DESCRIPTION("i.MX LVDS driver");
715MODULE_AUTHOR("Sascha Hauer, Pengutronix");
716MODULE_LICENSE("GPL");
Fabio Estevambc627382013-08-18 21:40:03 -0300717MODULE_ALIAS("platform:" DRIVER_NAME);