imx-ldb.c 18 KB

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