blob: f506e8dd76cb4de1bfc13aaeaaf34055d3dc0325 [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>
20#include <drm/drm_fb_helper.h>
21#include <drm/drm_crtc_helper.h>
Philipp Zabel53141e42015-02-24 11:41:28 +010022#include <drm/drm_of.h>
Philipp Zabel751e2672014-03-06 14:54:39 +010023#include <drm/drm_panel.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080024#include <linux/mfd/syscon.h>
25#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080026#include <linux/of_device.h>
Philipp Zabel751e2672014-03-06 14:54:39 +010027#include <linux/of_graph.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080028#include <video/of_videomode.h>
29#include <linux/regmap.h>
30#include <linux/videodev2.h>
31
32#include "imx-drm.h"
33
34#define DRIVER_NAME "imx-ldb"
35
36#define LDB_CH0_MODE_EN_TO_DI0 (1 << 0)
37#define LDB_CH0_MODE_EN_TO_DI1 (3 << 0)
38#define LDB_CH0_MODE_EN_MASK (3 << 0)
39#define LDB_CH1_MODE_EN_TO_DI0 (1 << 2)
40#define LDB_CH1_MODE_EN_TO_DI1 (3 << 2)
41#define LDB_CH1_MODE_EN_MASK (3 << 2)
42#define LDB_SPLIT_MODE_EN (1 << 4)
43#define LDB_DATA_WIDTH_CH0_24 (1 << 5)
44#define LDB_BIT_MAP_CH0_JEIDA (1 << 6)
45#define LDB_DATA_WIDTH_CH1_24 (1 << 7)
46#define LDB_BIT_MAP_CH1_JEIDA (1 << 8)
47#define LDB_DI0_VS_POL_ACT_LOW (1 << 9)
48#define LDB_DI1_VS_POL_ACT_LOW (1 << 10)
49#define LDB_BGREF_RMODE_INT (1 << 15)
50
51#define con_to_imx_ldb_ch(x) container_of(x, struct imx_ldb_channel, connector)
52#define enc_to_imx_ldb_ch(x) container_of(x, struct imx_ldb_channel, encoder)
53
54struct imx_ldb;
55
56struct imx_ldb_channel {
57 struct imx_ldb *ldb;
58 struct drm_connector connector;
Sascha Hauerac4c1a92013-06-18 09:23:57 +080059 struct drm_encoder encoder;
Philipp Zabel751e2672014-03-06 14:54:39 +010060 struct drm_panel *panel;
Russell King1b3f7672013-11-03 13:30:48 +000061 struct device_node *child;
Steve Longerbeama6d206e2016-04-27 16:23:33 -040062 struct i2c_adapter *ddc;
Sascha Hauerac4c1a92013-06-18 09:23:57 +080063 int chno;
64 void *edid;
65 int edid_len;
66 struct drm_display_mode mode;
67 int mode_valid;
Philipp Zabel5e501ed2014-12-02 20:22:49 +010068 int bus_format;
Sascha Hauerac4c1a92013-06-18 09:23:57 +080069};
70
71struct bus_mux {
72 int reg;
73 int shift;
74 int mask;
75};
76
77struct imx_ldb {
78 struct regmap *regmap;
79 struct device *dev;
80 struct imx_ldb_channel channel[2];
81 struct clk *clk[2]; /* our own clock */
82 struct clk *clk_sel[4]; /* parent of display clock */
Philipp Zabel3973aff2014-11-26 13:59:11 +010083 struct clk *clk_parent[4]; /* original parent of clk_sel */
Sascha Hauerac4c1a92013-06-18 09:23:57 +080084 struct clk *clk_pll[2]; /* upstream clock we can adjust */
85 u32 ldb_ctrl;
86 const struct bus_mux *lvds_mux;
87};
88
89static enum drm_connector_status imx_ldb_connector_detect(
90 struct drm_connector *connector, bool force)
91{
92 return connector_status_connected;
93}
94
Sascha Hauerac4c1a92013-06-18 09:23:57 +080095static int imx_ldb_connector_get_modes(struct drm_connector *connector)
96{
97 struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
98 int num_modes = 0;
99
Philipp Zabel751e2672014-03-06 14:54:39 +0100100 if (imx_ldb_ch->panel && imx_ldb_ch->panel->funcs &&
101 imx_ldb_ch->panel->funcs->get_modes) {
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100102 struct drm_display_info *di = &connector->display_info;
103
Philipp Zabel751e2672014-03-06 14:54:39 +0100104 num_modes = imx_ldb_ch->panel->funcs->get_modes(imx_ldb_ch->panel);
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100105 if (!imx_ldb_ch->bus_format && di->num_bus_formats)
106 imx_ldb_ch->bus_format = di->bus_formats[0];
Philipp Zabel751e2672014-03-06 14:54:39 +0100107 if (num_modes > 0)
108 return num_modes;
109 }
110
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400111 if (!imx_ldb_ch->edid && imx_ldb_ch->ddc)
112 imx_ldb_ch->edid = drm_get_edid(connector, imx_ldb_ch->ddc);
113
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800114 if (imx_ldb_ch->edid) {
115 drm_mode_connector_update_edid_property(connector,
116 imx_ldb_ch->edid);
117 num_modes = drm_add_edid_modes(connector, imx_ldb_ch->edid);
118 }
119
120 if (imx_ldb_ch->mode_valid) {
121 struct drm_display_mode *mode;
122
123 mode = drm_mode_create(connector->dev);
Fabio Estevam9f9b0362014-02-26 20:53:40 -0300124 if (!mode)
125 return -EINVAL;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800126 drm_mode_copy(mode, &imx_ldb_ch->mode);
127 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
128 drm_mode_probed_add(connector, mode);
129 num_modes++;
130 }
131
132 return num_modes;
133}
134
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800135static struct drm_encoder *imx_ldb_connector_best_encoder(
136 struct drm_connector *connector)
137{
138 struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
139
140 return &imx_ldb_ch->encoder;
141}
142
143static void imx_ldb_encoder_dpms(struct drm_encoder *encoder, int mode)
144{
145}
146
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800147static void imx_ldb_set_clock(struct imx_ldb *ldb, int mux, int chno,
148 unsigned long serial_clk, unsigned long di_clk)
149{
150 int ret;
151
152 dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
153 clk_get_rate(ldb->clk_pll[chno]), serial_clk);
154 clk_set_rate(ldb->clk_pll[chno], serial_clk);
155
156 dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
157 clk_get_rate(ldb->clk_pll[chno]));
158
159 dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
160 clk_get_rate(ldb->clk[chno]),
161 (long int)di_clk);
162 clk_set_rate(ldb->clk[chno], di_clk);
163
164 dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
165 clk_get_rate(ldb->clk[chno]));
166
167 /* set display clock mux to LDB input clock */
168 ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk[chno]);
Sima Baymani49f4a9c2013-11-03 11:18:57 +0100169 if (ret)
Aybuke Ozdemire5e1b162014-03-17 23:55:53 +0200170 dev_err(ldb->dev,
171 "unable to set di%d parent clock to ldb_di%d\n", mux,
172 chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800173}
174
175static void imx_ldb_encoder_prepare(struct drm_encoder *encoder)
176{
177 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
178 struct imx_ldb *ldb = imx_ldb_ch->ldb;
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100179 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
Philipp Zabel2872c802015-02-02 17:25:59 +0100180 u32 bus_format;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800181
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100182 switch (imx_ldb_ch->bus_format) {
Markus Niebel000d73f2013-06-18 09:23:58 +0800183 default:
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100184 dev_warn(ldb->dev,
185 "could not determine data mapping, default to 18-bit \"spwg\"\n");
186 /* fallthrough */
187 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
188 bus_format = MEDIA_BUS_FMT_RGB666_1X18;
189 break;
190 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
Philipp Zabel2872c802015-02-02 17:25:59 +0100191 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100192 if (imx_ldb_ch->chno == 0 || dual)
193 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24;
194 if (imx_ldb_ch->chno == 1 || dual)
195 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24;
196 break;
197 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
198 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
199 if (imx_ldb_ch->chno == 0 || dual)
200 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24 |
201 LDB_BIT_MAP_CH0_JEIDA;
202 if (imx_ldb_ch->chno == 1 || dual)
203 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24 |
204 LDB_BIT_MAP_CH1_JEIDA;
205 break;
Markus Niebel000d73f2013-06-18 09:23:58 +0800206 }
207
Philipp Zabel2872c802015-02-02 17:25:59 +0100208 imx_drm_set_bus_format(encoder, bus_format);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800209}
210
211static void imx_ldb_encoder_commit(struct drm_encoder *encoder)
212{
213 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
214 struct imx_ldb *ldb = imx_ldb_ch->ldb;
215 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
Philipp Zabel53141e42015-02-24 11:41:28 +0100216 int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800217
Philipp Zabel751e2672014-03-06 14:54:39 +0100218 drm_panel_prepare(imx_ldb_ch->panel);
219
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800220 if (dual) {
221 clk_prepare_enable(ldb->clk[0]);
222 clk_prepare_enable(ldb->clk[1]);
223 }
224
225 if (imx_ldb_ch == &ldb->channel[0] || dual) {
226 ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
227 if (mux == 0 || ldb->lvds_mux)
228 ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI0;
229 else if (mux == 1)
230 ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI1;
231 }
232 if (imx_ldb_ch == &ldb->channel[1] || dual) {
233 ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
234 if (mux == 1 || ldb->lvds_mux)
235 ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI1;
236 else if (mux == 0)
237 ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI0;
238 }
239
240 if (ldb->lvds_mux) {
241 const struct bus_mux *lvds_mux = NULL;
242
243 if (imx_ldb_ch == &ldb->channel[0])
244 lvds_mux = &ldb->lvds_mux[0];
245 else if (imx_ldb_ch == &ldb->channel[1])
246 lvds_mux = &ldb->lvds_mux[1];
247
248 regmap_update_bits(ldb->regmap, lvds_mux->reg, lvds_mux->mask,
249 mux << lvds_mux->shift);
250 }
251
252 regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
Philipp Zabel751e2672014-03-06 14:54:39 +0100253
254 drm_panel_enable(imx_ldb_ch->panel);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800255}
256
257static void imx_ldb_encoder_mode_set(struct drm_encoder *encoder,
Steve Longerbeameb10d632014-12-18 18:00:24 -0800258 struct drm_display_mode *orig_mode,
259 struct drm_display_mode *mode)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800260{
261 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
262 struct imx_ldb *ldb = imx_ldb_ch->ldb;
263 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
Philipp Zabel51dac942015-01-23 17:10:01 +0100264 unsigned long serial_clk;
265 unsigned long di_clk = mode->clock * 1000;
Philipp Zabel53141e42015-02-24 11:41:28 +0100266 int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800267
268 if (mode->clock > 170000) {
269 dev_warn(ldb->dev,
270 "%s: mode exceeds 170 MHz pixel clock\n", __func__);
271 }
272 if (mode->clock > 85000 && !dual) {
273 dev_warn(ldb->dev,
274 "%s: mode exceeds 85 MHz pixel clock\n", __func__);
275 }
276
Philipp Zabel51dac942015-01-23 17:10:01 +0100277 if (dual) {
278 serial_clk = 3500UL * mode->clock;
279 imx_ldb_set_clock(ldb, mux, 0, serial_clk, di_clk);
280 imx_ldb_set_clock(ldb, mux, 1, serial_clk, di_clk);
281 } else {
282 serial_clk = 7000UL * mode->clock;
283 imx_ldb_set_clock(ldb, mux, imx_ldb_ch->chno, serial_clk,
284 di_clk);
285 }
286
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800287 /* FIXME - assumes straight connections DI0 --> CH0, DI1 --> CH1 */
288 if (imx_ldb_ch == &ldb->channel[0]) {
289 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
290 ldb->ldb_ctrl |= LDB_DI0_VS_POL_ACT_LOW;
291 else if (mode->flags & DRM_MODE_FLAG_PVSYNC)
292 ldb->ldb_ctrl &= ~LDB_DI0_VS_POL_ACT_LOW;
293 }
294 if (imx_ldb_ch == &ldb->channel[1]) {
295 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
296 ldb->ldb_ctrl |= LDB_DI1_VS_POL_ACT_LOW;
297 else if (mode->flags & DRM_MODE_FLAG_PVSYNC)
298 ldb->ldb_ctrl &= ~LDB_DI1_VS_POL_ACT_LOW;
299 }
300}
301
302static void imx_ldb_encoder_disable(struct drm_encoder *encoder)
303{
304 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
305 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 = {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800360 .dpms = drm_helper_connector_dpms,
361 .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,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800364};
365
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100366static const struct drm_connector_helper_funcs imx_ldb_connector_helper_funcs = {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800367 .get_modes = imx_ldb_connector_get_modes,
368 .best_encoder = imx_ldb_connector_best_encoder,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800369};
370
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100371static const struct drm_encoder_funcs imx_ldb_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000372 .destroy = imx_drm_encoder_destroy,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800373};
374
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100375static const struct drm_encoder_helper_funcs imx_ldb_encoder_helper_funcs = {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800376 .dpms = imx_ldb_encoder_dpms,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800377 .prepare = imx_ldb_encoder_prepare,
378 .commit = imx_ldb_encoder_commit,
379 .mode_set = imx_ldb_encoder_mode_set,
380 .disable = imx_ldb_encoder_disable,
381};
382
383static int imx_ldb_get_clk(struct imx_ldb *ldb, int chno)
384{
385 char clkname[16];
386
Fabio Estevam98dd3b22014-02-28 11:39:42 -0300387 snprintf(clkname, sizeof(clkname), "di%d", chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800388 ldb->clk[chno] = devm_clk_get(ldb->dev, clkname);
389 if (IS_ERR(ldb->clk[chno]))
390 return PTR_ERR(ldb->clk[chno]);
391
Fabio Estevam98dd3b22014-02-28 11:39:42 -0300392 snprintf(clkname, sizeof(clkname), "di%d_pll", chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800393 ldb->clk_pll[chno] = devm_clk_get(ldb->dev, clkname);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800394
Valentina Manea1f933fa2013-10-23 10:29:55 +0300395 return PTR_ERR_OR_ZERO(ldb->clk_pll[chno]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800396}
397
Russell King1b3f7672013-11-03 13:30:48 +0000398static int imx_ldb_register(struct drm_device *drm,
399 struct imx_ldb_channel *imx_ldb_ch)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800400{
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800401 struct imx_ldb *ldb = imx_ldb_ch->ldb;
Russell King1b3f7672013-11-03 13:30:48 +0000402 int ret;
403
404 ret = imx_drm_encoder_parse_of(drm, &imx_ldb_ch->encoder,
405 imx_ldb_ch->child);
406 if (ret)
407 return ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800408
409 ret = imx_ldb_get_clk(ldb, imx_ldb_ch->chno);
410 if (ret)
411 return ret;
Russell King1b3f7672013-11-03 13:30:48 +0000412
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800413 if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
Russell King1b3f7672013-11-03 13:30:48 +0000414 ret = imx_ldb_get_clk(ldb, 1);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800415 if (ret)
416 return ret;
417 }
418
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800419 drm_encoder_helper_add(&imx_ldb_ch->encoder,
420 &imx_ldb_encoder_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000421 drm_encoder_init(drm, &imx_ldb_ch->encoder, &imx_ldb_encoder_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200422 DRM_MODE_ENCODER_LVDS, NULL);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800423
424 drm_connector_helper_add(&imx_ldb_ch->connector,
425 &imx_ldb_connector_helper_funcs);
Russell King1b3f7672013-11-03 13:30:48 +0000426 drm_connector_init(drm, &imx_ldb_ch->connector,
427 &imx_ldb_connector_funcs, DRM_MODE_CONNECTOR_LVDS);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800428
Philipp Zabel751e2672014-03-06 14:54:39 +0100429 if (imx_ldb_ch->panel)
430 drm_panel_attach(imx_ldb_ch->panel, &imx_ldb_ch->connector);
431
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800432 drm_mode_connector_attach_encoder(&imx_ldb_ch->connector,
433 &imx_ldb_ch->encoder);
434
435 return 0;
436}
437
438enum {
439 LVDS_BIT_MAP_SPWG,
440 LVDS_BIT_MAP_JEIDA
441};
442
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100443struct imx_ldb_bit_mapping {
444 u32 bus_format;
445 u32 datawidth;
446 const char * const mapping;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800447};
448
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100449static const struct imx_ldb_bit_mapping imx_ldb_bit_mappings[] = {
450 { MEDIA_BUS_FMT_RGB666_1X7X3_SPWG, 18, "spwg" },
451 { MEDIA_BUS_FMT_RGB888_1X7X4_SPWG, 24, "spwg" },
452 { MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA, 24, "jeida" },
453};
454
455static u32 of_get_bus_format(struct device *dev, struct device_node *np)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800456{
457 const char *bm;
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100458 u32 datawidth = 0;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800459 int ret, i;
460
461 ret = of_property_read_string(np, "fsl,data-mapping", &bm);
462 if (ret < 0)
463 return ret;
464
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100465 of_property_read_u32(np, "fsl,data-width", &datawidth);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800466
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100467 for (i = 0; i < ARRAY_SIZE(imx_ldb_bit_mappings); i++) {
468 if (!strcasecmp(bm, imx_ldb_bit_mappings[i].mapping) &&
469 datawidth == imx_ldb_bit_mappings[i].datawidth)
470 return imx_ldb_bit_mappings[i].bus_format;
471 }
472
473 dev_err(dev, "invalid data mapping: %d-bit \"%s\"\n", datawidth, bm);
474
475 return -ENOENT;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800476}
477
478static struct bus_mux imx6q_lvds_mux[2] = {
479 {
480 .reg = IOMUXC_GPR3,
481 .shift = 6,
482 .mask = IMX6Q_GPR3_LVDS0_MUX_CTL_MASK,
483 }, {
484 .reg = IOMUXC_GPR3,
485 .shift = 8,
486 .mask = IMX6Q_GPR3_LVDS1_MUX_CTL_MASK,
487 }
488};
489
490/*
491 * For a device declaring compatible = "fsl,imx6q-ldb", "fsl,imx53-ldb",
492 * of_match_device will walk through this list and take the first entry
493 * matching any of its compatible values. Therefore, the more generic
494 * entries (in this case fsl,imx53-ldb) need to be ordered last.
495 */
496static const struct of_device_id imx_ldb_dt_ids[] = {
497 { .compatible = "fsl,imx6q-ldb", .data = imx6q_lvds_mux, },
498 { .compatible = "fsl,imx53-ldb", .data = NULL, },
499 { }
500};
501MODULE_DEVICE_TABLE(of, imx_ldb_dt_ids);
502
Russell King17b50012013-11-03 11:23:34 +0000503static int imx_ldb_bind(struct device *dev, struct device *master, void *data)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800504{
Russell King1b3f7672013-11-03 13:30:48 +0000505 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000506 struct device_node *np = dev->of_node;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800507 const struct of_device_id *of_id =
Russell King17b50012013-11-03 11:23:34 +0000508 of_match_device(imx_ldb_dt_ids, dev);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800509 struct device_node *child;
510 const u8 *edidp;
511 struct imx_ldb *imx_ldb;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800512 int dual;
513 int ret;
514 int i;
515
Russell King17b50012013-11-03 11:23:34 +0000516 imx_ldb = devm_kzalloc(dev, sizeof(*imx_ldb), GFP_KERNEL);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800517 if (!imx_ldb)
518 return -ENOMEM;
519
520 imx_ldb->regmap = syscon_regmap_lookup_by_phandle(np, "gpr");
521 if (IS_ERR(imx_ldb->regmap)) {
Russell King17b50012013-11-03 11:23:34 +0000522 dev_err(dev, "failed to get parent regmap\n");
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800523 return PTR_ERR(imx_ldb->regmap);
524 }
525
Russell King17b50012013-11-03 11:23:34 +0000526 imx_ldb->dev = dev;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800527
528 if (of_id)
529 imx_ldb->lvds_mux = of_id->data;
530
531 dual = of_property_read_bool(np, "fsl,dual-channel");
532 if (dual)
533 imx_ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN;
534
535 /*
Masanari Iida45999342013-07-24 01:05:07 +0900536 * There are three different possible clock mux configurations:
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800537 * i.MX53: ipu1_di0_sel, ipu1_di1_sel
538 * i.MX6q: ipu1_di0_sel, ipu1_di1_sel, ipu2_di0_sel, ipu2_di1_sel
539 * i.MX6dl: ipu1_di0_sel, ipu1_di1_sel, lcdif_sel
540 * Map them all to di0_sel...di3_sel.
541 */
542 for (i = 0; i < 4; i++) {
543 char clkname[16];
544
545 sprintf(clkname, "di%d_sel", i);
546 imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
547 if (IS_ERR(imx_ldb->clk_sel[i])) {
548 ret = PTR_ERR(imx_ldb->clk_sel[i]);
549 imx_ldb->clk_sel[i] = NULL;
550 break;
551 }
Philipp Zabel3973aff2014-11-26 13:59:11 +0100552
553 imx_ldb->clk_parent[i] = clk_get_parent(imx_ldb->clk_sel[i]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800554 }
555 if (i == 0)
556 return ret;
557
558 for_each_child_of_node(np, child) {
559 struct imx_ldb_channel *channel;
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400560 struct device_node *ddc_node;
Philipp Zabel751e2672014-03-06 14:54:39 +0100561 struct device_node *port;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800562
563 ret = of_property_read_u32(child, "reg", &i);
564 if (ret || i < 0 || i > 1)
565 return -EINVAL;
566
567 if (dual && i > 0) {
Russell King17b50012013-11-03 11:23:34 +0000568 dev_warn(dev, "dual-channel mode, ignoring second output\n");
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800569 continue;
570 }
571
572 if (!of_device_is_available(child))
573 continue;
574
575 channel = &imx_ldb->channel[i];
576 channel->ldb = imx_ldb;
577 channel->chno = i;
Russell King1b3f7672013-11-03 13:30:48 +0000578 channel->child = child;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800579
Philipp Zabel751e2672014-03-06 14:54:39 +0100580 /*
581 * The output port is port@4 with an external 4-port mux or
582 * port@2 with the internal 2-port mux.
583 */
584 port = of_graph_get_port_by_id(child, imx_ldb->lvds_mux ? 4 : 2);
585 if (port) {
586 struct device_node *endpoint, *remote;
587
588 endpoint = of_get_child_by_name(port, "endpoint");
589 if (endpoint) {
590 remote = of_graph_get_remote_port_parent(endpoint);
591 if (remote)
592 channel->panel = of_drm_find_panel(remote);
593 else
594 return -EPROBE_DEFER;
595 if (!channel->panel) {
596 dev_err(dev, "panel not found: %s\n",
597 remote->full_name);
598 return -EPROBE_DEFER;
599 }
600 }
601 }
602
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400603 ddc_node = of_parse_phandle(child, "ddc-i2c-bus", 0);
604 if (ddc_node) {
605 channel->ddc = of_find_i2c_adapter_by_node(ddc_node);
606 of_node_put(ddc_node);
607 if (!channel->ddc) {
608 dev_warn(dev, "failed to get ddc i2c adapter\n");
609 return -EPROBE_DEFER;
610 }
611 }
612
613 if (!channel->ddc) {
614 /* if no DDC available, fallback to hardcoded EDID */
615 dev_dbg(dev, "no ddc available\n");
616
617 edidp = of_get_property(child, "edid",
618 &channel->edid_len);
619 if (edidp) {
620 channel->edid = kmemdup(edidp,
621 channel->edid_len,
622 GFP_KERNEL);
623 } else if (!channel->panel) {
624 /* fallback to display-timings node */
625 ret = of_get_drm_display_mode(child,
626 &channel->mode,
627 0);
628 if (!ret)
629 channel->mode_valid = 1;
630 }
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800631 }
632
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100633 channel->bus_format = of_get_bus_format(dev, child);
634 if (channel->bus_format == -EINVAL) {
635 /*
636 * If no bus format was specified in the device tree,
637 * we can still get it from the connected panel later.
638 */
639 if (channel->panel && channel->panel->funcs &&
640 channel->panel->funcs->get_modes)
641 channel->bus_format = 0;
642 }
643 if (channel->bus_format < 0) {
644 dev_err(dev, "could not determine data mapping: %d\n",
645 channel->bus_format);
646 return channel->bus_format;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800647 }
648
Russell King1b3f7672013-11-03 13:30:48 +0000649 ret = imx_ldb_register(drm, channel);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800650 if (ret)
651 return ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800652 }
653
Russell King17b50012013-11-03 11:23:34 +0000654 dev_set_drvdata(dev, imx_ldb);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800655
656 return 0;
657}
658
Russell King17b50012013-11-03 11:23:34 +0000659static void imx_ldb_unbind(struct device *dev, struct device *master,
660 void *data)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800661{
Russell King17b50012013-11-03 11:23:34 +0000662 struct imx_ldb *imx_ldb = dev_get_drvdata(dev);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800663 int i;
664
665 for (i = 0; i < 2; i++) {
666 struct imx_ldb_channel *channel = &imx_ldb->channel[i];
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800667
Russell Kingd9fdb9f2014-09-01 18:07:38 +0100668 if (!channel->connector.funcs)
669 continue;
670
Russell King1b3f7672013-11-03 13:30:48 +0000671 channel->connector.funcs->destroy(&channel->connector);
672 channel->encoder.funcs->destroy(&channel->encoder);
Peter Seidererf4876ff2013-12-08 22:03:57 +0100673
674 kfree(channel->edid);
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400675 i2c_put_adapter(channel->ddc);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800676 }
Russell King17b50012013-11-03 11:23:34 +0000677}
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800678
Russell King17b50012013-11-03 11:23:34 +0000679static const struct component_ops imx_ldb_ops = {
680 .bind = imx_ldb_bind,
681 .unbind = imx_ldb_unbind,
682};
683
684static int imx_ldb_probe(struct platform_device *pdev)
685{
686 return component_add(&pdev->dev, &imx_ldb_ops);
687}
688
689static int imx_ldb_remove(struct platform_device *pdev)
690{
691 component_del(&pdev->dev, &imx_ldb_ops);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800692 return 0;
693}
694
695static struct platform_driver imx_ldb_driver = {
696 .probe = imx_ldb_probe,
697 .remove = imx_ldb_remove,
698 .driver = {
699 .of_match_table = imx_ldb_dt_ids,
700 .name = DRIVER_NAME,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800701 },
702};
703
704module_platform_driver(imx_ldb_driver);
705
706MODULE_DESCRIPTION("i.MX LVDS driver");
707MODULE_AUTHOR("Sascha Hauer, Pengutronix");
708MODULE_LICENSE("GPL");
Fabio Estevambc627382013-08-18 21:40:03 -0300709MODULE_ALIAS("platform:" DRIVER_NAME);