imx-ldb.c 18 KB

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