blob: 17974c0b4be84417afb227c0b5f9984c0f4e61d3 [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>
Philipp Zabel49f98bc2016-07-06 14:49:24 +020020#include <drm/drm_atomic.h>
Liu Ying255c35f2016-07-08 17:40:56 +080021#include <drm/drm_atomic_helper.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080022#include <drm/drm_fb_helper.h>
23#include <drm/drm_crtc_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>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080026#include <linux/mfd/syscon.h>
27#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080028#include <linux/of_device.h>
Philipp Zabel751e2672014-03-06 14:54:39 +010029#include <linux/of_graph.h>
Lothar Waßmannc82b4d72016-05-24 08:31:49 +020030#include <video/of_display_timing.h>
Sascha Hauerac4c1a92013-06-18 09:23:57 +080031#include <video/of_videomode.h>
32#include <linux/regmap.h>
33#include <linux/videodev2.h>
34
35#include "imx-drm.h"
36
37#define DRIVER_NAME "imx-ldb"
38
39#define LDB_CH0_MODE_EN_TO_DI0 (1 << 0)
40#define LDB_CH0_MODE_EN_TO_DI1 (3 << 0)
41#define LDB_CH0_MODE_EN_MASK (3 << 0)
42#define LDB_CH1_MODE_EN_TO_DI0 (1 << 2)
43#define LDB_CH1_MODE_EN_TO_DI1 (3 << 2)
44#define LDB_CH1_MODE_EN_MASK (3 << 2)
45#define LDB_SPLIT_MODE_EN (1 << 4)
46#define LDB_DATA_WIDTH_CH0_24 (1 << 5)
47#define LDB_BIT_MAP_CH0_JEIDA (1 << 6)
48#define LDB_DATA_WIDTH_CH1_24 (1 << 7)
49#define LDB_BIT_MAP_CH1_JEIDA (1 << 8)
50#define LDB_DI0_VS_POL_ACT_LOW (1 << 9)
51#define LDB_DI1_VS_POL_ACT_LOW (1 << 10)
52#define LDB_BGREF_RMODE_INT (1 << 15)
53
Sascha Hauerac4c1a92013-06-18 09:23:57 +080054struct imx_ldb;
55
56struct imx_ldb_channel {
57 struct imx_ldb *ldb;
58 struct drm_connector connector;
Philipp Zabel49f98bc2016-07-06 14:49:24 +020059 struct drm_encoder encoder;
Peter Senna Tschudindc80d702016-08-05 00:36:58 +020060
61 /* Defines what is connected to the ldb, only one at a time */
Philipp Zabel751e2672014-03-06 14:54:39 +010062 struct drm_panel *panel;
Peter Senna Tschudindc80d702016-08-05 00:36:58 +020063 struct drm_bridge *bridge;
64
Russell King1b3f7672013-11-03 13:30:48 +000065 struct device_node *child;
Steve Longerbeama6d206e2016-04-27 16:23:33 -040066 struct i2c_adapter *ddc;
Sascha Hauerac4c1a92013-06-18 09:23:57 +080067 int chno;
68 void *edid;
69 int edid_len;
70 struct drm_display_mode mode;
71 int mode_valid;
Philipp Zabel49f98bc2016-07-06 14:49:24 +020072 u32 bus_format;
Lothar Waßmannfafc79e2016-07-12 15:30:03 +020073 u32 bus_flags;
Sascha Hauerac4c1a92013-06-18 09:23:57 +080074};
75
Philipp Zabel3df07392016-07-06 15:47:11 +020076static inline struct imx_ldb_channel *con_to_imx_ldb_ch(struct drm_connector *c)
77{
78 return container_of(c, struct imx_ldb_channel, connector);
79}
80
Philipp Zabel49f98bc2016-07-06 14:49:24 +020081static inline struct imx_ldb_channel *enc_to_imx_ldb_ch(struct drm_encoder *e)
82{
83 return container_of(e, struct imx_ldb_channel, encoder);
84}
85
Sascha Hauerac4c1a92013-06-18 09:23:57 +080086struct bus_mux {
87 int reg;
88 int shift;
89 int mask;
90};
91
92struct imx_ldb {
93 struct regmap *regmap;
94 struct device *dev;
95 struct imx_ldb_channel channel[2];
96 struct clk *clk[2]; /* our own clock */
97 struct clk *clk_sel[4]; /* parent of display clock */
Philipp Zabel3973aff2014-11-26 13:59:11 +010098 struct clk *clk_parent[4]; /* original parent of clk_sel */
Sascha Hauerac4c1a92013-06-18 09:23:57 +080099 struct clk *clk_pll[2]; /* upstream clock we can adjust */
100 u32 ldb_ctrl;
101 const struct bus_mux *lvds_mux;
102};
103
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200104static void imx_ldb_ch_set_bus_format(struct imx_ldb_channel *imx_ldb_ch,
105 u32 bus_format)
Liu Ying032003c2016-07-08 17:40:58 +0800106{
107 struct imx_ldb *ldb = imx_ldb_ch->ldb;
108 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
109
110 switch (bus_format) {
111 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
Liu Ying032003c2016-07-08 17:40:58 +0800112 break;
113 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
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 if (imx_ldb_ch->chno == 1 || dual)
117 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24;
118 break;
119 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
Liu Ying032003c2016-07-08 17:40:58 +0800120 if (imx_ldb_ch->chno == 0 || dual)
121 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24 |
122 LDB_BIT_MAP_CH0_JEIDA;
123 if (imx_ldb_ch->chno == 1 || dual)
124 ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24 |
125 LDB_BIT_MAP_CH1_JEIDA;
126 break;
127 }
128}
129
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800130static int imx_ldb_connector_get_modes(struct drm_connector *connector)
131{
132 struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
133 int num_modes = 0;
134
Philipp Zabel751e2672014-03-06 14:54:39 +0100135 if (imx_ldb_ch->panel && imx_ldb_ch->panel->funcs &&
136 imx_ldb_ch->panel->funcs->get_modes) {
137 num_modes = imx_ldb_ch->panel->funcs->get_modes(imx_ldb_ch->panel);
138 if (num_modes > 0)
139 return num_modes;
140 }
141
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400142 if (!imx_ldb_ch->edid && imx_ldb_ch->ddc)
143 imx_ldb_ch->edid = drm_get_edid(connector, imx_ldb_ch->ddc);
144
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800145 if (imx_ldb_ch->edid) {
146 drm_mode_connector_update_edid_property(connector,
147 imx_ldb_ch->edid);
148 num_modes = drm_add_edid_modes(connector, imx_ldb_ch->edid);
149 }
150
151 if (imx_ldb_ch->mode_valid) {
152 struct drm_display_mode *mode;
153
154 mode = drm_mode_create(connector->dev);
Fabio Estevam9f9b0362014-02-26 20:53:40 -0300155 if (!mode)
156 return -EINVAL;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800157 drm_mode_copy(mode, &imx_ldb_ch->mode);
158 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
159 drm_mode_probed_add(connector, mode);
160 num_modes++;
161 }
162
163 return num_modes;
164}
165
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800166static struct drm_encoder *imx_ldb_connector_best_encoder(
167 struct drm_connector *connector)
168{
169 struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
170
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200171 return &imx_ldb_ch->encoder;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800172}
173
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800174static void imx_ldb_set_clock(struct imx_ldb *ldb, int mux, int chno,
175 unsigned long serial_clk, unsigned long di_clk)
176{
177 int ret;
178
179 dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
180 clk_get_rate(ldb->clk_pll[chno]), serial_clk);
181 clk_set_rate(ldb->clk_pll[chno], serial_clk);
182
183 dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
184 clk_get_rate(ldb->clk_pll[chno]));
185
186 dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
187 clk_get_rate(ldb->clk[chno]),
188 (long int)di_clk);
189 clk_set_rate(ldb->clk[chno], di_clk);
190
191 dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
192 clk_get_rate(ldb->clk[chno]));
193
194 /* set display clock mux to LDB input clock */
195 ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk[chno]);
Sima Baymani49f4a9c2013-11-03 11:18:57 +0100196 if (ret)
Aybuke Ozdemire5e1b162014-03-17 23:55:53 +0200197 dev_err(ldb->dev,
198 "unable to set di%d parent clock to ldb_di%d\n", mux,
199 chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800200}
201
Liu Yingf6e396e2016-07-08 17:41:01 +0800202static void imx_ldb_encoder_enable(struct drm_encoder *encoder)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800203{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200204 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800205 struct imx_ldb *ldb = imx_ldb_ch->ldb;
206 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
Philipp Zabel53141e42015-02-24 11:41:28 +0100207 int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800208
Philipp Zabel751e2672014-03-06 14:54:39 +0100209 drm_panel_prepare(imx_ldb_ch->panel);
210
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800211 if (dual) {
Liu Yingf6e396e2016-07-08 17:41:01 +0800212 clk_set_parent(ldb->clk_sel[mux], ldb->clk[0]);
213 clk_set_parent(ldb->clk_sel[mux], ldb->clk[1]);
214
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800215 clk_prepare_enable(ldb->clk[0]);
216 clk_prepare_enable(ldb->clk[1]);
Liu Yingf6e396e2016-07-08 17:41:01 +0800217 } else {
218 clk_set_parent(ldb->clk_sel[mux], ldb->clk[imx_ldb_ch->chno]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800219 }
220
221 if (imx_ldb_ch == &ldb->channel[0] || dual) {
222 ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
223 if (mux == 0 || ldb->lvds_mux)
224 ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI0;
225 else if (mux == 1)
226 ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI1;
227 }
228 if (imx_ldb_ch == &ldb->channel[1] || dual) {
229 ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
230 if (mux == 1 || ldb->lvds_mux)
231 ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI1;
232 else if (mux == 0)
233 ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI0;
234 }
235
236 if (ldb->lvds_mux) {
237 const struct bus_mux *lvds_mux = NULL;
238
239 if (imx_ldb_ch == &ldb->channel[0])
240 lvds_mux = &ldb->lvds_mux[0];
241 else if (imx_ldb_ch == &ldb->channel[1])
242 lvds_mux = &ldb->lvds_mux[1];
243
244 regmap_update_bits(ldb->regmap, lvds_mux->reg, lvds_mux->mask,
245 mux << lvds_mux->shift);
246 }
247
248 regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
Philipp Zabel751e2672014-03-06 14:54:39 +0100249
250 drm_panel_enable(imx_ldb_ch->panel);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800251}
252
Philipp Zabel3a2ad502016-07-22 12:43:04 +0200253static void
254imx_ldb_encoder_atomic_mode_set(struct drm_encoder *encoder,
255 struct drm_crtc_state *crtc_state,
256 struct drm_connector_state *connector_state)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800257{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200258 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
Philipp Zabel3a2ad502016-07-22 12:43:04 +0200259 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800260 struct imx_ldb *ldb = imx_ldb_ch->ldb;
261 int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
Philipp Zabel51dac942015-01-23 17:10:01 +0100262 unsigned long serial_clk;
263 unsigned long di_clk = mode->clock * 1000;
Philipp Zabel53141e42015-02-24 11:41:28 +0100264 int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200265 u32 bus_format = imx_ldb_ch->bus_format;
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 */
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200287 if (imx_ldb_ch == &ldb->channel[0] || dual) {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800288 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 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200293 if (imx_ldb_ch == &ldb->channel[1] || dual) {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800294 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 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200299
300 if (!bus_format) {
Philipp Zabel3a2ad502016-07-22 12:43:04 +0200301 struct drm_connector *connector = connector_state->connector;
302 struct drm_display_info *di = &connector->display_info;
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200303
Philipp Zabel3a2ad502016-07-22 12:43:04 +0200304 if (di->num_bus_formats)
305 bus_format = di->bus_formats[0];
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200306 }
307 imx_ldb_ch_set_bus_format(imx_ldb_ch, bus_format);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800308}
309
310static void imx_ldb_encoder_disable(struct drm_encoder *encoder)
311{
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200312 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800313 struct imx_ldb *ldb = imx_ldb_ch->ldb;
Philipp Zabel3973aff2014-11-26 13:59:11 +0100314 int mux, ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800315
Philipp Zabel751e2672014-03-06 14:54:39 +0100316 drm_panel_disable(imx_ldb_ch->panel);
317
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800318 if (imx_ldb_ch == &ldb->channel[0])
319 ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
320 else if (imx_ldb_ch == &ldb->channel[1])
321 ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
322
323 regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
324
325 if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
326 clk_disable_unprepare(ldb->clk[0]);
327 clk_disable_unprepare(ldb->clk[1]);
328 }
Philipp Zabel751e2672014-03-06 14:54:39 +0100329
Philipp Zabel3973aff2014-11-26 13:59:11 +0100330 if (ldb->lvds_mux) {
331 const struct bus_mux *lvds_mux = NULL;
332
333 if (imx_ldb_ch == &ldb->channel[0])
334 lvds_mux = &ldb->lvds_mux[0];
335 else if (imx_ldb_ch == &ldb->channel[1])
336 lvds_mux = &ldb->lvds_mux[1];
337
338 regmap_read(ldb->regmap, lvds_mux->reg, &mux);
339 mux &= lvds_mux->mask;
340 mux >>= lvds_mux->shift;
341 } else {
342 mux = (imx_ldb_ch == &ldb->channel[0]) ? 0 : 1;
343 }
344
345 /* set display clock mux back to original input clock */
346 ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk_parent[mux]);
347 if (ret)
348 dev_err(ldb->dev,
349 "unable to set di%d parent clock to original parent\n",
350 mux);
351
Philipp Zabel751e2672014-03-06 14:54:39 +0100352 drm_panel_unprepare(imx_ldb_ch->panel);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800353}
354
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200355static int imx_ldb_encoder_atomic_check(struct drm_encoder *encoder,
356 struct drm_crtc_state *crtc_state,
357 struct drm_connector_state *conn_state)
358{
359 struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
360 struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
361 struct drm_display_info *di = &conn_state->connector->display_info;
362 u32 bus_format = imx_ldb_ch->bus_format;
363
364 /* Bus format description in DT overrides connector display info. */
Lothar Waßmannfafc79e2016-07-12 15:30:03 +0200365 if (!bus_format && di->num_bus_formats) {
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200366 bus_format = di->bus_formats[0];
Lothar Waßmannfafc79e2016-07-12 15:30:03 +0200367 imx_crtc_state->bus_flags = di->bus_flags;
368 } else {
369 bus_format = imx_ldb_ch->bus_format;
370 imx_crtc_state->bus_flags = imx_ldb_ch->bus_flags;
371 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200372 switch (bus_format) {
373 case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
374 imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
375 break;
376 case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
377 case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
378 imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
379 break;
380 default:
381 return -EINVAL;
382 }
383
384 imx_crtc_state->di_hsync_pin = 2;
385 imx_crtc_state->di_vsync_pin = 3;
386
387 return 0;
388}
389
390
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100391static const struct drm_connector_funcs imx_ldb_connector_funcs = {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800392 .fill_modes = drm_helper_probe_single_connector_modes,
Russell King1b3f7672013-11-03 13:30:48 +0000393 .destroy = imx_drm_connector_destroy,
Liu Ying255c35f2016-07-08 17:40:56 +0800394 .reset = drm_atomic_helper_connector_reset,
395 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
396 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800397};
398
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100399static const struct drm_connector_helper_funcs imx_ldb_connector_helper_funcs = {
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800400 .get_modes = imx_ldb_connector_get_modes,
401 .best_encoder = imx_ldb_connector_best_encoder,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800402};
403
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100404static const struct drm_encoder_funcs imx_ldb_encoder_funcs = {
Russell King1b3f7672013-11-03 13:30:48 +0000405 .destroy = imx_drm_encoder_destroy,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800406};
407
Ville Syrjälä7ae847d2015-12-15 12:21:09 +0100408static const struct drm_encoder_helper_funcs imx_ldb_encoder_helper_funcs = {
Philipp Zabel3a2ad502016-07-22 12:43:04 +0200409 .atomic_mode_set = imx_ldb_encoder_atomic_mode_set,
Liu Yingf6e396e2016-07-08 17:41:01 +0800410 .enable = imx_ldb_encoder_enable,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800411 .disable = imx_ldb_encoder_disable,
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200412 .atomic_check = imx_ldb_encoder_atomic_check,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800413};
414
415static int imx_ldb_get_clk(struct imx_ldb *ldb, int chno)
416{
417 char clkname[16];
418
Fabio Estevam98dd3b22014-02-28 11:39:42 -0300419 snprintf(clkname, sizeof(clkname), "di%d", chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800420 ldb->clk[chno] = devm_clk_get(ldb->dev, clkname);
421 if (IS_ERR(ldb->clk[chno]))
422 return PTR_ERR(ldb->clk[chno]);
423
Fabio Estevam98dd3b22014-02-28 11:39:42 -0300424 snprintf(clkname, sizeof(clkname), "di%d_pll", chno);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800425 ldb->clk_pll[chno] = devm_clk_get(ldb->dev, clkname);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800426
Valentina Manea1f933fa2013-10-23 10:29:55 +0300427 return PTR_ERR_OR_ZERO(ldb->clk_pll[chno]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800428}
429
Russell King1b3f7672013-11-03 13:30:48 +0000430static int imx_ldb_register(struct drm_device *drm,
431 struct imx_ldb_channel *imx_ldb_ch)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800432{
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800433 struct imx_ldb *ldb = imx_ldb_ch->ldb;
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200434 struct drm_encoder *encoder = &imx_ldb_ch->encoder;
Russell King1b3f7672013-11-03 13:30:48 +0000435 int ret;
436
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200437 ret = imx_drm_encoder_parse_of(drm, encoder, imx_ldb_ch->child);
Russell King1b3f7672013-11-03 13:30:48 +0000438 if (ret)
439 return ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800440
441 ret = imx_ldb_get_clk(ldb, imx_ldb_ch->chno);
442 if (ret)
443 return ret;
Russell King1b3f7672013-11-03 13:30:48 +0000444
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800445 if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
Russell King1b3f7672013-11-03 13:30:48 +0000446 ret = imx_ldb_get_clk(ldb, 1);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800447 if (ret)
448 return ret;
449 }
450
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200451 drm_encoder_helper_add(encoder, &imx_ldb_encoder_helper_funcs);
452 drm_encoder_init(drm, encoder, &imx_ldb_encoder_funcs,
453 DRM_MODE_ENCODER_LVDS, NULL);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800454
Peter Senna Tschudindc80d702016-08-05 00:36:58 +0200455 if (imx_ldb_ch->bridge) {
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200456 ret = drm_bridge_attach(&imx_ldb_ch->encoder,
457 imx_ldb_ch->bridge, NULL);
Peter Senna Tschudindc80d702016-08-05 00:36:58 +0200458 if (ret) {
459 DRM_ERROR("Failed to initialize bridge with drm\n");
460 return ret;
461 }
462 } else {
463 /*
464 * We want to add the connector whenever there is no bridge
465 * that brings its own, not only when there is a panel. For
466 * historical reasons, the ldb driver can also work without
467 * a panel.
468 */
469 drm_connector_helper_add(&imx_ldb_ch->connector,
470 &imx_ldb_connector_helper_funcs);
471 drm_connector_init(drm, &imx_ldb_ch->connector,
472 &imx_ldb_connector_funcs,
473 DRM_MODE_CONNECTOR_LVDS);
474 drm_mode_connector_attach_encoder(&imx_ldb_ch->connector,
475 encoder);
476 }
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800477
Lucas Stach7075ba72016-06-16 11:12:13 +0200478 if (imx_ldb_ch->panel) {
479 ret = drm_panel_attach(imx_ldb_ch->panel,
480 &imx_ldb_ch->connector);
481 if (ret)
482 return ret;
483 }
Philipp Zabel751e2672014-03-06 14:54:39 +0100484
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800485 return 0;
486}
487
488enum {
489 LVDS_BIT_MAP_SPWG,
490 LVDS_BIT_MAP_JEIDA
491};
492
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100493struct imx_ldb_bit_mapping {
494 u32 bus_format;
495 u32 datawidth;
496 const char * const mapping;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800497};
498
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100499static const struct imx_ldb_bit_mapping imx_ldb_bit_mappings[] = {
500 { MEDIA_BUS_FMT_RGB666_1X7X3_SPWG, 18, "spwg" },
501 { MEDIA_BUS_FMT_RGB888_1X7X4_SPWG, 24, "spwg" },
502 { MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA, 24, "jeida" },
503};
504
505static u32 of_get_bus_format(struct device *dev, struct device_node *np)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800506{
507 const char *bm;
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100508 u32 datawidth = 0;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800509 int ret, i;
510
511 ret = of_property_read_string(np, "fsl,data-mapping", &bm);
512 if (ret < 0)
513 return ret;
514
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100515 of_property_read_u32(np, "fsl,data-width", &datawidth);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800516
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100517 for (i = 0; i < ARRAY_SIZE(imx_ldb_bit_mappings); i++) {
518 if (!strcasecmp(bm, imx_ldb_bit_mappings[i].mapping) &&
519 datawidth == imx_ldb_bit_mappings[i].datawidth)
520 return imx_ldb_bit_mappings[i].bus_format;
521 }
522
523 dev_err(dev, "invalid data mapping: %d-bit \"%s\"\n", datawidth, bm);
524
525 return -ENOENT;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800526}
527
528static struct bus_mux imx6q_lvds_mux[2] = {
529 {
530 .reg = IOMUXC_GPR3,
531 .shift = 6,
532 .mask = IMX6Q_GPR3_LVDS0_MUX_CTL_MASK,
533 }, {
534 .reg = IOMUXC_GPR3,
535 .shift = 8,
536 .mask = IMX6Q_GPR3_LVDS1_MUX_CTL_MASK,
537 }
538};
539
540/*
541 * For a device declaring compatible = "fsl,imx6q-ldb", "fsl,imx53-ldb",
542 * of_match_device will walk through this list and take the first entry
543 * matching any of its compatible values. Therefore, the more generic
544 * entries (in this case fsl,imx53-ldb) need to be ordered last.
545 */
546static const struct of_device_id imx_ldb_dt_ids[] = {
547 { .compatible = "fsl,imx6q-ldb", .data = imx6q_lvds_mux, },
548 { .compatible = "fsl,imx53-ldb", .data = NULL, },
549 { }
550};
551MODULE_DEVICE_TABLE(of, imx_ldb_dt_ids);
552
Peter Senna Tschudindc80d702016-08-05 00:36:58 +0200553static int imx_ldb_panel_ddc(struct device *dev,
554 struct imx_ldb_channel *channel, struct device_node *child)
555{
556 struct device_node *ddc_node;
557 const u8 *edidp;
558 int ret;
559
560 ddc_node = of_parse_phandle(child, "ddc-i2c-bus", 0);
561 if (ddc_node) {
562 channel->ddc = of_find_i2c_adapter_by_node(ddc_node);
563 of_node_put(ddc_node);
564 if (!channel->ddc) {
565 dev_warn(dev, "failed to get ddc i2c adapter\n");
566 return -EPROBE_DEFER;
567 }
568 }
569
570 if (!channel->ddc) {
571 /* if no DDC available, fallback to hardcoded EDID */
572 dev_dbg(dev, "no ddc available\n");
573
574 edidp = of_get_property(child, "edid",
575 &channel->edid_len);
576 if (edidp) {
577 channel->edid = kmemdup(edidp,
578 channel->edid_len,
579 GFP_KERNEL);
580 } else if (!channel->panel) {
581 /* fallback to display-timings node */
582 ret = of_get_drm_display_mode(child,
583 &channel->mode,
584 &channel->bus_flags,
585 OF_USE_NATIVE_MODE);
586 if (!ret)
587 channel->mode_valid = 1;
588 }
589 }
590 return 0;
591}
592
Russell King17b50012013-11-03 11:23:34 +0000593static int imx_ldb_bind(struct device *dev, struct device *master, void *data)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800594{
Russell King1b3f7672013-11-03 13:30:48 +0000595 struct drm_device *drm = data;
Russell King17b50012013-11-03 11:23:34 +0000596 struct device_node *np = dev->of_node;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800597 const struct of_device_id *of_id =
Russell King17b50012013-11-03 11:23:34 +0000598 of_match_device(imx_ldb_dt_ids, dev);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800599 struct device_node *child;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800600 struct imx_ldb *imx_ldb;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800601 int dual;
602 int ret;
603 int i;
604
Russell King17b50012013-11-03 11:23:34 +0000605 imx_ldb = devm_kzalloc(dev, sizeof(*imx_ldb), GFP_KERNEL);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800606 if (!imx_ldb)
607 return -ENOMEM;
608
609 imx_ldb->regmap = syscon_regmap_lookup_by_phandle(np, "gpr");
610 if (IS_ERR(imx_ldb->regmap)) {
Russell King17b50012013-11-03 11:23:34 +0000611 dev_err(dev, "failed to get parent regmap\n");
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800612 return PTR_ERR(imx_ldb->regmap);
613 }
614
Lucas Stachb5826232018-04-11 17:31:35 +0200615 /* disable LDB by resetting the control register to POR default */
616 regmap_write(imx_ldb->regmap, IOMUXC_GPR2, 0);
617
Russell King17b50012013-11-03 11:23:34 +0000618 imx_ldb->dev = dev;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800619
620 if (of_id)
621 imx_ldb->lvds_mux = of_id->data;
622
623 dual = of_property_read_bool(np, "fsl,dual-channel");
624 if (dual)
625 imx_ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN;
626
627 /*
Masanari Iida45999342013-07-24 01:05:07 +0900628 * There are three different possible clock mux configurations:
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800629 * i.MX53: ipu1_di0_sel, ipu1_di1_sel
630 * i.MX6q: ipu1_di0_sel, ipu1_di1_sel, ipu2_di0_sel, ipu2_di1_sel
631 * i.MX6dl: ipu1_di0_sel, ipu1_di1_sel, lcdif_sel
632 * Map them all to di0_sel...di3_sel.
633 */
634 for (i = 0; i < 4; i++) {
635 char clkname[16];
636
637 sprintf(clkname, "di%d_sel", i);
638 imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
639 if (IS_ERR(imx_ldb->clk_sel[i])) {
640 ret = PTR_ERR(imx_ldb->clk_sel[i]);
641 imx_ldb->clk_sel[i] = NULL;
642 break;
643 }
Philipp Zabel3973aff2014-11-26 13:59:11 +0100644
645 imx_ldb->clk_parent[i] = clk_get_parent(imx_ldb->clk_sel[i]);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800646 }
647 if (i == 0)
648 return ret;
649
650 for_each_child_of_node(np, child) {
651 struct imx_ldb_channel *channel;
Liu Ying032003c2016-07-08 17:40:58 +0800652 int bus_format;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800653
654 ret = of_property_read_u32(child, "reg", &i);
655 if (ret || i < 0 || i > 1)
656 return -EINVAL;
657
658 if (dual && i > 0) {
Russell King17b50012013-11-03 11:23:34 +0000659 dev_warn(dev, "dual-channel mode, ignoring second output\n");
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800660 continue;
661 }
662
663 if (!of_device_is_available(child))
664 continue;
665
666 channel = &imx_ldb->channel[i];
667 channel->ldb = imx_ldb;
668 channel->chno = i;
Russell King1b3f7672013-11-03 13:30:48 +0000669 channel->child = child;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800670
Philipp Zabel751e2672014-03-06 14:54:39 +0100671 /*
672 * The output port is port@4 with an external 4-port mux or
673 * port@2 with the internal 2-port mux.
674 */
Rob Herringebc94462017-03-29 13:55:46 -0500675 ret = drm_of_find_panel_or_bridge(child,
676 imx_ldb->lvds_mux ? 4 : 2, 0,
677 &channel->panel, &channel->bridge);
Leonard Cresteze36aecb2017-05-10 16:17:13 +0300678 if (ret && ret != -ENODEV)
Rob Herringebc94462017-03-29 13:55:46 -0500679 return ret;
Philipp Zabel751e2672014-03-06 14:54:39 +0100680
Peter Senna Tschudindc80d702016-08-05 00:36:58 +0200681 /* panel ddc only if there is no bridge */
682 if (!channel->bridge) {
683 ret = imx_ldb_panel_ddc(dev, channel, child);
684 if (ret)
685 return ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800686 }
687
Liu Ying032003c2016-07-08 17:40:58 +0800688 bus_format = of_get_bus_format(dev, child);
689 if (bus_format == -EINVAL) {
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100690 /*
691 * If no bus format was specified in the device tree,
692 * we can still get it from the connected panel later.
693 */
694 if (channel->panel && channel->panel->funcs &&
695 channel->panel->funcs->get_modes)
Liu Ying032003c2016-07-08 17:40:58 +0800696 bus_format = 0;
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100697 }
Liu Ying032003c2016-07-08 17:40:58 +0800698 if (bus_format < 0) {
Philipp Zabel5e501ed2014-12-02 20:22:49 +0100699 dev_err(dev, "could not determine data mapping: %d\n",
Liu Ying032003c2016-07-08 17:40:58 +0800700 bus_format);
701 return bus_format;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800702 }
Philipp Zabel49f98bc2016-07-06 14:49:24 +0200703 channel->bus_format = bus_format;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800704
Russell King1b3f7672013-11-03 13:30:48 +0000705 ret = imx_ldb_register(drm, channel);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800706 if (ret)
707 return ret;
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800708 }
709
Russell King17b50012013-11-03 11:23:34 +0000710 dev_set_drvdata(dev, imx_ldb);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800711
712 return 0;
713}
714
Russell King17b50012013-11-03 11:23:34 +0000715static void imx_ldb_unbind(struct device *dev, struct device *master,
716 void *data)
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800717{
Russell King17b50012013-11-03 11:23:34 +0000718 struct imx_ldb *imx_ldb = dev_get_drvdata(dev);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800719 int i;
720
721 for (i = 0; i < 2; i++) {
722 struct imx_ldb_channel *channel = &imx_ldb->channel[i];
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800723
Lucas Stachb1318d52016-06-16 11:19:41 +0200724 if (channel->panel)
725 drm_panel_detach(channel->panel);
726
Peter Seidererf4876ff2013-12-08 22:03:57 +0100727 kfree(channel->edid);
Steve Longerbeama6d206e2016-04-27 16:23:33 -0400728 i2c_put_adapter(channel->ddc);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800729 }
Russell King17b50012013-11-03 11:23:34 +0000730}
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800731
Russell King17b50012013-11-03 11:23:34 +0000732static const struct component_ops imx_ldb_ops = {
733 .bind = imx_ldb_bind,
734 .unbind = imx_ldb_unbind,
735};
736
737static int imx_ldb_probe(struct platform_device *pdev)
738{
739 return component_add(&pdev->dev, &imx_ldb_ops);
740}
741
742static int imx_ldb_remove(struct platform_device *pdev)
743{
744 component_del(&pdev->dev, &imx_ldb_ops);
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800745 return 0;
746}
747
748static struct platform_driver imx_ldb_driver = {
749 .probe = imx_ldb_probe,
750 .remove = imx_ldb_remove,
751 .driver = {
752 .of_match_table = imx_ldb_dt_ids,
753 .name = DRIVER_NAME,
Sascha Hauerac4c1a92013-06-18 09:23:57 +0800754 },
755};
756
757module_platform_driver(imx_ldb_driver);
758
759MODULE_DESCRIPTION("i.MX LVDS driver");
760MODULE_AUTHOR("Sascha Hauer, Pengutronix");
761MODULE_LICENSE("GPL");
Fabio Estevambc627382013-08-18 21:40:03 -0300762MODULE_ALIAS("platform:" DRIVER_NAME);