Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 1 | /* |
| 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 Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/clk.h> |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 18 | #include <linux/component.h> |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 19 | #include <drm/drmP.h> |
| 20 | #include <drm/drm_fb_helper.h> |
| 21 | #include <drm/drm_crtc_helper.h> |
| 22 | #include <linux/mfd/syscon.h> |
| 23 | #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h> |
| 24 | #include <linux/of_address.h> |
| 25 | #include <linux/of_device.h> |
| 26 | #include <video/of_videomode.h> |
| 27 | #include <linux/regmap.h> |
| 28 | #include <linux/videodev2.h> |
| 29 | |
| 30 | #include "imx-drm.h" |
| 31 | |
| 32 | #define DRIVER_NAME "imx-ldb" |
| 33 | |
| 34 | #define LDB_CH0_MODE_EN_TO_DI0 (1 << 0) |
| 35 | #define LDB_CH0_MODE_EN_TO_DI1 (3 << 0) |
| 36 | #define LDB_CH0_MODE_EN_MASK (3 << 0) |
| 37 | #define LDB_CH1_MODE_EN_TO_DI0 (1 << 2) |
| 38 | #define LDB_CH1_MODE_EN_TO_DI1 (3 << 2) |
| 39 | #define LDB_CH1_MODE_EN_MASK (3 << 2) |
| 40 | #define LDB_SPLIT_MODE_EN (1 << 4) |
| 41 | #define LDB_DATA_WIDTH_CH0_24 (1 << 5) |
| 42 | #define LDB_BIT_MAP_CH0_JEIDA (1 << 6) |
| 43 | #define LDB_DATA_WIDTH_CH1_24 (1 << 7) |
| 44 | #define LDB_BIT_MAP_CH1_JEIDA (1 << 8) |
| 45 | #define LDB_DI0_VS_POL_ACT_LOW (1 << 9) |
| 46 | #define LDB_DI1_VS_POL_ACT_LOW (1 << 10) |
| 47 | #define LDB_BGREF_RMODE_INT (1 << 15) |
| 48 | |
| 49 | #define con_to_imx_ldb_ch(x) container_of(x, struct imx_ldb_channel, connector) |
| 50 | #define enc_to_imx_ldb_ch(x) container_of(x, struct imx_ldb_channel, encoder) |
| 51 | |
| 52 | struct imx_ldb; |
| 53 | |
| 54 | struct imx_ldb_channel { |
| 55 | struct imx_ldb *ldb; |
| 56 | struct drm_connector connector; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 57 | struct drm_encoder encoder; |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 58 | struct device_node *child; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 59 | int chno; |
| 60 | void *edid; |
| 61 | int edid_len; |
| 62 | struct drm_display_mode mode; |
| 63 | int mode_valid; |
| 64 | }; |
| 65 | |
| 66 | struct bus_mux { |
| 67 | int reg; |
| 68 | int shift; |
| 69 | int mask; |
| 70 | }; |
| 71 | |
| 72 | struct imx_ldb { |
| 73 | struct regmap *regmap; |
| 74 | struct device *dev; |
| 75 | struct imx_ldb_channel channel[2]; |
| 76 | struct clk *clk[2]; /* our own clock */ |
| 77 | struct clk *clk_sel[4]; /* parent of display clock */ |
| 78 | struct clk *clk_pll[2]; /* upstream clock we can adjust */ |
| 79 | u32 ldb_ctrl; |
| 80 | const struct bus_mux *lvds_mux; |
| 81 | }; |
| 82 | |
| 83 | static enum drm_connector_status imx_ldb_connector_detect( |
| 84 | struct drm_connector *connector, bool force) |
| 85 | { |
| 86 | return connector_status_connected; |
| 87 | } |
| 88 | |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 89 | static int imx_ldb_connector_get_modes(struct drm_connector *connector) |
| 90 | { |
| 91 | struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector); |
| 92 | int num_modes = 0; |
| 93 | |
| 94 | if (imx_ldb_ch->edid) { |
| 95 | drm_mode_connector_update_edid_property(connector, |
| 96 | imx_ldb_ch->edid); |
| 97 | num_modes = drm_add_edid_modes(connector, imx_ldb_ch->edid); |
| 98 | } |
| 99 | |
| 100 | if (imx_ldb_ch->mode_valid) { |
| 101 | struct drm_display_mode *mode; |
| 102 | |
| 103 | mode = drm_mode_create(connector->dev); |
Fabio Estevam | 9f9b036 | 2014-02-26 20:53:40 -0300 | [diff] [blame] | 104 | if (!mode) |
| 105 | return -EINVAL; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 106 | drm_mode_copy(mode, &imx_ldb_ch->mode); |
| 107 | mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; |
| 108 | drm_mode_probed_add(connector, mode); |
| 109 | num_modes++; |
| 110 | } |
| 111 | |
| 112 | return num_modes; |
| 113 | } |
| 114 | |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 115 | static struct drm_encoder *imx_ldb_connector_best_encoder( |
| 116 | struct drm_connector *connector) |
| 117 | { |
| 118 | struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector); |
| 119 | |
| 120 | return &imx_ldb_ch->encoder; |
| 121 | } |
| 122 | |
| 123 | static void imx_ldb_encoder_dpms(struct drm_encoder *encoder, int mode) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | static bool imx_ldb_encoder_mode_fixup(struct drm_encoder *encoder, |
| 128 | const struct drm_display_mode *mode, |
| 129 | struct drm_display_mode *adjusted_mode) |
| 130 | { |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | static void imx_ldb_set_clock(struct imx_ldb *ldb, int mux, int chno, |
| 135 | unsigned long serial_clk, unsigned long di_clk) |
| 136 | { |
| 137 | int ret; |
| 138 | |
| 139 | dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__, |
| 140 | clk_get_rate(ldb->clk_pll[chno]), serial_clk); |
| 141 | clk_set_rate(ldb->clk_pll[chno], serial_clk); |
| 142 | |
| 143 | dev_dbg(ldb->dev, "%s after: %ld\n", __func__, |
| 144 | clk_get_rate(ldb->clk_pll[chno])); |
| 145 | |
| 146 | dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__, |
| 147 | clk_get_rate(ldb->clk[chno]), |
| 148 | (long int)di_clk); |
| 149 | clk_set_rate(ldb->clk[chno], di_clk); |
| 150 | |
| 151 | dev_dbg(ldb->dev, "%s after: %ld\n", __func__, |
| 152 | clk_get_rate(ldb->clk[chno])); |
| 153 | |
| 154 | /* set display clock mux to LDB input clock */ |
| 155 | ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk[chno]); |
Sima Baymani | 49f4a9c | 2013-11-03 11:18:57 +0100 | [diff] [blame] | 156 | if (ret) |
Aybuke Ozdemir | e5e1b16 | 2014-03-17 23:55:53 +0200 | [diff] [blame] | 157 | dev_err(ldb->dev, |
| 158 | "unable to set di%d parent clock to ldb_di%d\n", mux, |
| 159 | chno); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void imx_ldb_encoder_prepare(struct drm_encoder *encoder) |
| 163 | { |
| 164 | struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder); |
| 165 | struct imx_ldb *ldb = imx_ldb_ch->ldb; |
Markus Niebel | 000d73f | 2013-06-18 09:23:58 +0800 | [diff] [blame] | 166 | u32 pixel_fmt; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 167 | |
Markus Niebel | 000d73f | 2013-06-18 09:23:58 +0800 | [diff] [blame] | 168 | switch (imx_ldb_ch->chno) { |
| 169 | case 0: |
| 170 | pixel_fmt = (ldb->ldb_ctrl & LDB_DATA_WIDTH_CH0_24) ? |
| 171 | V4L2_PIX_FMT_RGB24 : V4L2_PIX_FMT_BGR666; |
| 172 | break; |
| 173 | case 1: |
| 174 | pixel_fmt = (ldb->ldb_ctrl & LDB_DATA_WIDTH_CH1_24) ? |
| 175 | V4L2_PIX_FMT_RGB24 : V4L2_PIX_FMT_BGR666; |
| 176 | break; |
| 177 | default: |
| 178 | dev_err(ldb->dev, "unable to config di%d panel format\n", |
| 179 | imx_ldb_ch->chno); |
| 180 | pixel_fmt = V4L2_PIX_FMT_RGB24; |
| 181 | } |
| 182 | |
Russell King | f2d66aa | 2013-11-03 15:52:16 +0000 | [diff] [blame] | 183 | imx_drm_panel_format(encoder, pixel_fmt); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void imx_ldb_encoder_commit(struct drm_encoder *encoder) |
| 187 | { |
| 188 | struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder); |
| 189 | struct imx_ldb *ldb = imx_ldb_ch->ldb; |
| 190 | int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN; |
Philipp Zabel | 655b43c | 2014-03-05 10:20:52 +0100 | [diff] [blame] | 191 | int mux = imx_drm_encoder_get_mux_id(imx_ldb_ch->child, encoder); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 192 | |
| 193 | if (dual) { |
| 194 | clk_prepare_enable(ldb->clk[0]); |
| 195 | clk_prepare_enable(ldb->clk[1]); |
| 196 | } |
| 197 | |
| 198 | if (imx_ldb_ch == &ldb->channel[0] || dual) { |
| 199 | ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK; |
| 200 | if (mux == 0 || ldb->lvds_mux) |
| 201 | ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI0; |
| 202 | else if (mux == 1) |
| 203 | ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI1; |
| 204 | } |
| 205 | if (imx_ldb_ch == &ldb->channel[1] || dual) { |
| 206 | ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK; |
| 207 | if (mux == 1 || ldb->lvds_mux) |
| 208 | ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI1; |
| 209 | else if (mux == 0) |
| 210 | ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI0; |
| 211 | } |
| 212 | |
| 213 | if (ldb->lvds_mux) { |
| 214 | const struct bus_mux *lvds_mux = NULL; |
| 215 | |
| 216 | if (imx_ldb_ch == &ldb->channel[0]) |
| 217 | lvds_mux = &ldb->lvds_mux[0]; |
| 218 | else if (imx_ldb_ch == &ldb->channel[1]) |
| 219 | lvds_mux = &ldb->lvds_mux[1]; |
| 220 | |
| 221 | regmap_update_bits(ldb->regmap, lvds_mux->reg, lvds_mux->mask, |
| 222 | mux << lvds_mux->shift); |
| 223 | } |
| 224 | |
| 225 | regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl); |
| 226 | } |
| 227 | |
| 228 | static void imx_ldb_encoder_mode_set(struct drm_encoder *encoder, |
Steve Longerbeam | eb10d63 | 2014-12-18 18:00:24 -0800 | [diff] [blame] | 229 | struct drm_display_mode *orig_mode, |
| 230 | struct drm_display_mode *mode) |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 231 | { |
| 232 | struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder); |
| 233 | struct imx_ldb *ldb = imx_ldb_ch->ldb; |
| 234 | int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN; |
Philipp Zabel | 51dac94 | 2015-01-23 17:10:01 +0100 | [diff] [blame^] | 235 | unsigned long serial_clk; |
| 236 | unsigned long di_clk = mode->clock * 1000; |
| 237 | int mux = imx_drm_encoder_get_mux_id(imx_ldb_ch->child, encoder); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 238 | |
| 239 | if (mode->clock > 170000) { |
| 240 | dev_warn(ldb->dev, |
| 241 | "%s: mode exceeds 170 MHz pixel clock\n", __func__); |
| 242 | } |
| 243 | if (mode->clock > 85000 && !dual) { |
| 244 | dev_warn(ldb->dev, |
| 245 | "%s: mode exceeds 85 MHz pixel clock\n", __func__); |
| 246 | } |
| 247 | |
Philipp Zabel | 51dac94 | 2015-01-23 17:10:01 +0100 | [diff] [blame^] | 248 | if (dual) { |
| 249 | serial_clk = 3500UL * mode->clock; |
| 250 | imx_ldb_set_clock(ldb, mux, 0, serial_clk, di_clk); |
| 251 | imx_ldb_set_clock(ldb, mux, 1, serial_clk, di_clk); |
| 252 | } else { |
| 253 | serial_clk = 7000UL * mode->clock; |
| 254 | imx_ldb_set_clock(ldb, mux, imx_ldb_ch->chno, serial_clk, |
| 255 | di_clk); |
| 256 | } |
| 257 | |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 258 | /* FIXME - assumes straight connections DI0 --> CH0, DI1 --> CH1 */ |
| 259 | if (imx_ldb_ch == &ldb->channel[0]) { |
| 260 | if (mode->flags & DRM_MODE_FLAG_NVSYNC) |
| 261 | ldb->ldb_ctrl |= LDB_DI0_VS_POL_ACT_LOW; |
| 262 | else if (mode->flags & DRM_MODE_FLAG_PVSYNC) |
| 263 | ldb->ldb_ctrl &= ~LDB_DI0_VS_POL_ACT_LOW; |
| 264 | } |
| 265 | if (imx_ldb_ch == &ldb->channel[1]) { |
| 266 | if (mode->flags & DRM_MODE_FLAG_NVSYNC) |
| 267 | ldb->ldb_ctrl |= LDB_DI1_VS_POL_ACT_LOW; |
| 268 | else if (mode->flags & DRM_MODE_FLAG_PVSYNC) |
| 269 | ldb->ldb_ctrl &= ~LDB_DI1_VS_POL_ACT_LOW; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static void imx_ldb_encoder_disable(struct drm_encoder *encoder) |
| 274 | { |
| 275 | struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder); |
| 276 | struct imx_ldb *ldb = imx_ldb_ch->ldb; |
| 277 | |
| 278 | /* |
| 279 | * imx_ldb_encoder_disable is called by |
| 280 | * drm_helper_disable_unused_functions without |
| 281 | * the encoder being enabled before. |
| 282 | */ |
| 283 | if (imx_ldb_ch == &ldb->channel[0] && |
| 284 | (ldb->ldb_ctrl & LDB_CH0_MODE_EN_MASK) == 0) |
| 285 | return; |
| 286 | else if (imx_ldb_ch == &ldb->channel[1] && |
| 287 | (ldb->ldb_ctrl & LDB_CH1_MODE_EN_MASK) == 0) |
| 288 | return; |
| 289 | |
| 290 | if (imx_ldb_ch == &ldb->channel[0]) |
| 291 | ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK; |
| 292 | else if (imx_ldb_ch == &ldb->channel[1]) |
| 293 | ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK; |
| 294 | |
| 295 | regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl); |
| 296 | |
| 297 | if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) { |
| 298 | clk_disable_unprepare(ldb->clk[0]); |
| 299 | clk_disable_unprepare(ldb->clk[1]); |
| 300 | } |
| 301 | } |
| 302 | |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 303 | static struct drm_connector_funcs imx_ldb_connector_funcs = { |
| 304 | .dpms = drm_helper_connector_dpms, |
| 305 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 306 | .detect = imx_ldb_connector_detect, |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 307 | .destroy = imx_drm_connector_destroy, |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | static struct drm_connector_helper_funcs imx_ldb_connector_helper_funcs = { |
| 311 | .get_modes = imx_ldb_connector_get_modes, |
| 312 | .best_encoder = imx_ldb_connector_best_encoder, |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | static struct drm_encoder_funcs imx_ldb_encoder_funcs = { |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 316 | .destroy = imx_drm_encoder_destroy, |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 317 | }; |
| 318 | |
| 319 | static struct drm_encoder_helper_funcs imx_ldb_encoder_helper_funcs = { |
| 320 | .dpms = imx_ldb_encoder_dpms, |
| 321 | .mode_fixup = imx_ldb_encoder_mode_fixup, |
| 322 | .prepare = imx_ldb_encoder_prepare, |
| 323 | .commit = imx_ldb_encoder_commit, |
| 324 | .mode_set = imx_ldb_encoder_mode_set, |
| 325 | .disable = imx_ldb_encoder_disable, |
| 326 | }; |
| 327 | |
| 328 | static int imx_ldb_get_clk(struct imx_ldb *ldb, int chno) |
| 329 | { |
| 330 | char clkname[16]; |
| 331 | |
Fabio Estevam | 98dd3b2 | 2014-02-28 11:39:42 -0300 | [diff] [blame] | 332 | snprintf(clkname, sizeof(clkname), "di%d", chno); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 333 | ldb->clk[chno] = devm_clk_get(ldb->dev, clkname); |
| 334 | if (IS_ERR(ldb->clk[chno])) |
| 335 | return PTR_ERR(ldb->clk[chno]); |
| 336 | |
Fabio Estevam | 98dd3b2 | 2014-02-28 11:39:42 -0300 | [diff] [blame] | 337 | snprintf(clkname, sizeof(clkname), "di%d_pll", chno); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 338 | ldb->clk_pll[chno] = devm_clk_get(ldb->dev, clkname); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 339 | |
Valentina Manea | 1f933fa | 2013-10-23 10:29:55 +0300 | [diff] [blame] | 340 | return PTR_ERR_OR_ZERO(ldb->clk_pll[chno]); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 341 | } |
| 342 | |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 343 | static int imx_ldb_register(struct drm_device *drm, |
| 344 | struct imx_ldb_channel *imx_ldb_ch) |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 345 | { |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 346 | struct imx_ldb *ldb = imx_ldb_ch->ldb; |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 347 | int ret; |
| 348 | |
| 349 | ret = imx_drm_encoder_parse_of(drm, &imx_ldb_ch->encoder, |
| 350 | imx_ldb_ch->child); |
| 351 | if (ret) |
| 352 | return ret; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 353 | |
| 354 | ret = imx_ldb_get_clk(ldb, imx_ldb_ch->chno); |
| 355 | if (ret) |
| 356 | return ret; |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 357 | |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 358 | if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) { |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 359 | ret = imx_ldb_get_clk(ldb, 1); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 360 | if (ret) |
| 361 | return ret; |
| 362 | } |
| 363 | |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 364 | drm_encoder_helper_add(&imx_ldb_ch->encoder, |
| 365 | &imx_ldb_encoder_helper_funcs); |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 366 | drm_encoder_init(drm, &imx_ldb_ch->encoder, &imx_ldb_encoder_funcs, |
| 367 | DRM_MODE_ENCODER_LVDS); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 368 | |
| 369 | drm_connector_helper_add(&imx_ldb_ch->connector, |
| 370 | &imx_ldb_connector_helper_funcs); |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 371 | drm_connector_init(drm, &imx_ldb_ch->connector, |
| 372 | &imx_ldb_connector_funcs, DRM_MODE_CONNECTOR_LVDS); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 373 | |
| 374 | drm_mode_connector_attach_encoder(&imx_ldb_ch->connector, |
| 375 | &imx_ldb_ch->encoder); |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | enum { |
| 381 | LVDS_BIT_MAP_SPWG, |
| 382 | LVDS_BIT_MAP_JEIDA |
| 383 | }; |
| 384 | |
Archana kumari | 5354cb6 | 2013-10-31 00:34:08 +0530 | [diff] [blame] | 385 | static const char * const imx_ldb_bit_mappings[] = { |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 386 | [LVDS_BIT_MAP_SPWG] = "spwg", |
| 387 | [LVDS_BIT_MAP_JEIDA] = "jeida", |
| 388 | }; |
| 389 | |
Fabio Estevam | d78b140 | 2013-09-01 22:11:55 -0300 | [diff] [blame] | 390 | static const int of_get_data_mapping(struct device_node *np) |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 391 | { |
| 392 | const char *bm; |
| 393 | int ret, i; |
| 394 | |
| 395 | ret = of_property_read_string(np, "fsl,data-mapping", &bm); |
| 396 | if (ret < 0) |
| 397 | return ret; |
| 398 | |
| 399 | for (i = 0; i < ARRAY_SIZE(imx_ldb_bit_mappings); i++) |
| 400 | if (!strcasecmp(bm, imx_ldb_bit_mappings[i])) |
| 401 | return i; |
| 402 | |
| 403 | return -EINVAL; |
| 404 | } |
| 405 | |
| 406 | static struct bus_mux imx6q_lvds_mux[2] = { |
| 407 | { |
| 408 | .reg = IOMUXC_GPR3, |
| 409 | .shift = 6, |
| 410 | .mask = IMX6Q_GPR3_LVDS0_MUX_CTL_MASK, |
| 411 | }, { |
| 412 | .reg = IOMUXC_GPR3, |
| 413 | .shift = 8, |
| 414 | .mask = IMX6Q_GPR3_LVDS1_MUX_CTL_MASK, |
| 415 | } |
| 416 | }; |
| 417 | |
| 418 | /* |
| 419 | * For a device declaring compatible = "fsl,imx6q-ldb", "fsl,imx53-ldb", |
| 420 | * of_match_device will walk through this list and take the first entry |
| 421 | * matching any of its compatible values. Therefore, the more generic |
| 422 | * entries (in this case fsl,imx53-ldb) need to be ordered last. |
| 423 | */ |
| 424 | static const struct of_device_id imx_ldb_dt_ids[] = { |
| 425 | { .compatible = "fsl,imx6q-ldb", .data = imx6q_lvds_mux, }, |
| 426 | { .compatible = "fsl,imx53-ldb", .data = NULL, }, |
| 427 | { } |
| 428 | }; |
| 429 | MODULE_DEVICE_TABLE(of, imx_ldb_dt_ids); |
| 430 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 431 | static int imx_ldb_bind(struct device *dev, struct device *master, void *data) |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 432 | { |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 433 | struct drm_device *drm = data; |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 434 | struct device_node *np = dev->of_node; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 435 | const struct of_device_id *of_id = |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 436 | of_match_device(imx_ldb_dt_ids, dev); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 437 | struct device_node *child; |
| 438 | const u8 *edidp; |
| 439 | struct imx_ldb *imx_ldb; |
| 440 | int datawidth; |
| 441 | int mapping; |
| 442 | int dual; |
| 443 | int ret; |
| 444 | int i; |
| 445 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 446 | imx_ldb = devm_kzalloc(dev, sizeof(*imx_ldb), GFP_KERNEL); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 447 | if (!imx_ldb) |
| 448 | return -ENOMEM; |
| 449 | |
| 450 | imx_ldb->regmap = syscon_regmap_lookup_by_phandle(np, "gpr"); |
| 451 | if (IS_ERR(imx_ldb->regmap)) { |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 452 | dev_err(dev, "failed to get parent regmap\n"); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 453 | return PTR_ERR(imx_ldb->regmap); |
| 454 | } |
| 455 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 456 | imx_ldb->dev = dev; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 457 | |
| 458 | if (of_id) |
| 459 | imx_ldb->lvds_mux = of_id->data; |
| 460 | |
| 461 | dual = of_property_read_bool(np, "fsl,dual-channel"); |
| 462 | if (dual) |
| 463 | imx_ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN; |
| 464 | |
| 465 | /* |
Masanari Iida | 4599934 | 2013-07-24 01:05:07 +0900 | [diff] [blame] | 466 | * There are three different possible clock mux configurations: |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 467 | * i.MX53: ipu1_di0_sel, ipu1_di1_sel |
| 468 | * i.MX6q: ipu1_di0_sel, ipu1_di1_sel, ipu2_di0_sel, ipu2_di1_sel |
| 469 | * i.MX6dl: ipu1_di0_sel, ipu1_di1_sel, lcdif_sel |
| 470 | * Map them all to di0_sel...di3_sel. |
| 471 | */ |
| 472 | for (i = 0; i < 4; i++) { |
| 473 | char clkname[16]; |
| 474 | |
| 475 | sprintf(clkname, "di%d_sel", i); |
| 476 | imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname); |
| 477 | if (IS_ERR(imx_ldb->clk_sel[i])) { |
| 478 | ret = PTR_ERR(imx_ldb->clk_sel[i]); |
| 479 | imx_ldb->clk_sel[i] = NULL; |
| 480 | break; |
| 481 | } |
| 482 | } |
| 483 | if (i == 0) |
| 484 | return ret; |
| 485 | |
| 486 | for_each_child_of_node(np, child) { |
| 487 | struct imx_ldb_channel *channel; |
| 488 | |
| 489 | ret = of_property_read_u32(child, "reg", &i); |
| 490 | if (ret || i < 0 || i > 1) |
| 491 | return -EINVAL; |
| 492 | |
| 493 | if (dual && i > 0) { |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 494 | dev_warn(dev, "dual-channel mode, ignoring second output\n"); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 495 | continue; |
| 496 | } |
| 497 | |
| 498 | if (!of_device_is_available(child)) |
| 499 | continue; |
| 500 | |
| 501 | channel = &imx_ldb->channel[i]; |
| 502 | channel->ldb = imx_ldb; |
| 503 | channel->chno = i; |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 504 | channel->child = child; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 505 | |
| 506 | edidp = of_get_property(child, "edid", &channel->edid_len); |
| 507 | if (edidp) { |
| 508 | channel->edid = kmemdup(edidp, channel->edid_len, |
| 509 | GFP_KERNEL); |
| 510 | } else { |
| 511 | ret = of_get_drm_display_mode(child, &channel->mode, 0); |
| 512 | if (!ret) |
| 513 | channel->mode_valid = 1; |
| 514 | } |
| 515 | |
| 516 | ret = of_property_read_u32(child, "fsl,data-width", &datawidth); |
| 517 | if (ret) |
| 518 | datawidth = 0; |
| 519 | else if (datawidth != 18 && datawidth != 24) |
| 520 | return -EINVAL; |
| 521 | |
| 522 | mapping = of_get_data_mapping(child); |
| 523 | switch (mapping) { |
| 524 | case LVDS_BIT_MAP_SPWG: |
| 525 | if (datawidth == 24) { |
| 526 | if (i == 0 || dual) |
Aybuke Ozdemir | e5e1b16 | 2014-03-17 23:55:53 +0200 | [diff] [blame] | 527 | imx_ldb->ldb_ctrl |= |
| 528 | LDB_DATA_WIDTH_CH0_24; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 529 | if (i == 1 || dual) |
Aybuke Ozdemir | e5e1b16 | 2014-03-17 23:55:53 +0200 | [diff] [blame] | 530 | imx_ldb->ldb_ctrl |= |
| 531 | LDB_DATA_WIDTH_CH1_24; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 532 | } |
| 533 | break; |
| 534 | case LVDS_BIT_MAP_JEIDA: |
| 535 | if (datawidth == 18) { |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 536 | dev_err(dev, "JEIDA standard only supported in 24 bit\n"); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 537 | return -EINVAL; |
| 538 | } |
| 539 | if (i == 0 || dual) |
Aybuke Ozdemir | e5e1b16 | 2014-03-17 23:55:53 +0200 | [diff] [blame] | 540 | imx_ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24 | |
| 541 | LDB_BIT_MAP_CH0_JEIDA; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 542 | if (i == 1 || dual) |
Aybuke Ozdemir | e5e1b16 | 2014-03-17 23:55:53 +0200 | [diff] [blame] | 543 | imx_ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24 | |
| 544 | LDB_BIT_MAP_CH1_JEIDA; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 545 | break; |
| 546 | default: |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 547 | dev_err(dev, "data mapping not specified or invalid\n"); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 548 | return -EINVAL; |
| 549 | } |
| 550 | |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 551 | ret = imx_ldb_register(drm, channel); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 552 | if (ret) |
| 553 | return ret; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 554 | } |
| 555 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 556 | dev_set_drvdata(dev, imx_ldb); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 561 | static void imx_ldb_unbind(struct device *dev, struct device *master, |
| 562 | void *data) |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 563 | { |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 564 | struct imx_ldb *imx_ldb = dev_get_drvdata(dev); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 565 | int i; |
| 566 | |
| 567 | for (i = 0; i < 2; i++) { |
| 568 | struct imx_ldb_channel *channel = &imx_ldb->channel[i]; |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 569 | |
Russell King | d9fdb9f | 2014-09-01 18:07:38 +0100 | [diff] [blame] | 570 | if (!channel->connector.funcs) |
| 571 | continue; |
| 572 | |
Russell King | 1b3f767 | 2013-11-03 13:30:48 +0000 | [diff] [blame] | 573 | channel->connector.funcs->destroy(&channel->connector); |
| 574 | channel->encoder.funcs->destroy(&channel->encoder); |
Peter Seiderer | f4876ff | 2013-12-08 22:03:57 +0100 | [diff] [blame] | 575 | |
| 576 | kfree(channel->edid); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 577 | } |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 578 | } |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 579 | |
Russell King | 17b5001 | 2013-11-03 11:23:34 +0000 | [diff] [blame] | 580 | static const struct component_ops imx_ldb_ops = { |
| 581 | .bind = imx_ldb_bind, |
| 582 | .unbind = imx_ldb_unbind, |
| 583 | }; |
| 584 | |
| 585 | static int imx_ldb_probe(struct platform_device *pdev) |
| 586 | { |
| 587 | return component_add(&pdev->dev, &imx_ldb_ops); |
| 588 | } |
| 589 | |
| 590 | static int imx_ldb_remove(struct platform_device *pdev) |
| 591 | { |
| 592 | component_del(&pdev->dev, &imx_ldb_ops); |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | static struct platform_driver imx_ldb_driver = { |
| 597 | .probe = imx_ldb_probe, |
| 598 | .remove = imx_ldb_remove, |
| 599 | .driver = { |
| 600 | .of_match_table = imx_ldb_dt_ids, |
| 601 | .name = DRIVER_NAME, |
Sascha Hauer | ac4c1a9 | 2013-06-18 09:23:57 +0800 | [diff] [blame] | 602 | }, |
| 603 | }; |
| 604 | |
| 605 | module_platform_driver(imx_ldb_driver); |
| 606 | |
| 607 | MODULE_DESCRIPTION("i.MX LVDS driver"); |
| 608 | MODULE_AUTHOR("Sascha Hauer, Pengutronix"); |
| 609 | MODULE_LICENSE("GPL"); |
Fabio Estevam | bc62738 | 2013-08-18 21:40:03 -0300 | [diff] [blame] | 610 | MODULE_ALIAS("platform:" DRIVER_NAME); |