imx-ldb.c 18 KB

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