imx-ldb.c 18 KB

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