imx-ldb.c 19 KB

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