imx-ldb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * i.MX drm driver - LVDS display bridge
  4. *
  5. * Copyright (C) 2012 Sascha Hauer, Pengutronix
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/component.h>
  9. #include <linux/i2c.h>
  10. #include <linux/media-bus-format.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_graph.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/property.h>
  18. #include <linux/regmap.h>
  19. #include <linux/videodev2.h>
  20. #include <video/of_display_timing.h>
  21. #include <video/of_videomode.h>
  22. #include <drm/drm_atomic.h>
  23. #include <drm/drm_atomic_helper.h>
  24. #include <drm/drm_bridge.h>
  25. #include <drm/drm_edid.h>
  26. #include <drm/drm_managed.h>
  27. #include <drm/drm_of.h>
  28. #include <drm/drm_panel.h>
  29. #include <drm/drm_print.h>
  30. #include <drm/drm_probe_helper.h>
  31. #include <drm/drm_simple_kms_helper.h>
  32. #include "imx-drm.h"
  33. #define DRIVER_NAME "imx-ldb"
  34. #define LDB_CH0_MODE_EN_TO_DI0 (1 << 0)
  35. #define LDB_CH0_MODE_EN_TO_DI1 (3 << 0)
  36. #define LDB_CH0_MODE_EN_MASK (3 << 0)
  37. #define LDB_CH1_MODE_EN_TO_DI0 (1 << 2)
  38. #define LDB_CH1_MODE_EN_TO_DI1 (3 << 2)
  39. #define LDB_CH1_MODE_EN_MASK (3 << 2)
  40. #define LDB_SPLIT_MODE_EN (1 << 4)
  41. #define LDB_DATA_WIDTH_CH0_24 (1 << 5)
  42. #define LDB_BIT_MAP_CH0_JEIDA (1 << 6)
  43. #define LDB_DATA_WIDTH_CH1_24 (1 << 7)
  44. #define LDB_BIT_MAP_CH1_JEIDA (1 << 8)
  45. #define LDB_DI0_VS_POL_ACT_LOW (1 << 9)
  46. #define LDB_DI1_VS_POL_ACT_LOW (1 << 10)
  47. #define LDB_BGREF_RMODE_INT (1 << 15)
  48. struct imx_ldb_channel;
  49. struct imx_ldb_encoder {
  50. struct drm_connector connector;
  51. struct drm_encoder encoder;
  52. struct imx_ldb_channel *channel;
  53. };
  54. struct imx_ldb;
  55. struct imx_ldb_channel {
  56. struct imx_ldb *ldb;
  57. /* Defines what is connected to the ldb, only one at a time */
  58. struct drm_panel *panel;
  59. struct drm_bridge *bridge;
  60. struct device_node *child;
  61. struct i2c_adapter *ddc;
  62. int chno;
  63. const struct drm_edid *drm_edid;
  64. struct drm_display_mode mode;
  65. int mode_valid;
  66. u32 bus_format;
  67. u32 bus_flags;
  68. };
  69. static inline struct imx_ldb_channel *con_to_imx_ldb_ch(struct drm_connector *c)
  70. {
  71. return container_of(c, struct imx_ldb_encoder, connector)->channel;
  72. }
  73. static inline struct imx_ldb_channel *enc_to_imx_ldb_ch(struct drm_encoder *e)
  74. {
  75. return container_of(e, struct imx_ldb_encoder, encoder)->channel;
  76. }
  77. struct bus_mux {
  78. int reg;
  79. int shift;
  80. int mask;
  81. };
  82. struct imx_ldb {
  83. struct regmap *regmap;
  84. struct device *dev;
  85. struct imx_ldb_channel channel[2];
  86. struct clk *clk[2]; /* our own clock */
  87. struct clk *clk_sel[4]; /* parent of display clock */
  88. struct clk *clk_parent[4]; /* original parent of clk_sel */
  89. struct clk *clk_pll[2]; /* upstream clock we can adjust */
  90. u32 ldb_ctrl;
  91. const struct bus_mux *lvds_mux;
  92. };
  93. static void imx_ldb_ch_set_bus_format(struct imx_ldb_channel *imx_ldb_ch,
  94. u32 bus_format)
  95. {
  96. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  97. int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
  98. switch (bus_format) {
  99. case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
  100. break;
  101. case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
  102. if (imx_ldb_ch->chno == 0 || dual)
  103. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24;
  104. if (imx_ldb_ch->chno == 1 || dual)
  105. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24;
  106. break;
  107. case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
  108. if (imx_ldb_ch->chno == 0 || dual)
  109. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24 |
  110. LDB_BIT_MAP_CH0_JEIDA;
  111. if (imx_ldb_ch->chno == 1 || dual)
  112. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24 |
  113. LDB_BIT_MAP_CH1_JEIDA;
  114. break;
  115. }
  116. }
  117. static int imx_ldb_connector_get_modes(struct drm_connector *connector)
  118. {
  119. struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
  120. int num_modes;
  121. num_modes = drm_panel_get_modes(imx_ldb_ch->panel, connector);
  122. if (num_modes > 0)
  123. return num_modes;
  124. if (!imx_ldb_ch->drm_edid && imx_ldb_ch->ddc) {
  125. imx_ldb_ch->drm_edid = drm_edid_read_ddc(connector,
  126. imx_ldb_ch->ddc);
  127. drm_edid_connector_update(connector, imx_ldb_ch->drm_edid);
  128. }
  129. if (imx_ldb_ch->drm_edid)
  130. num_modes = drm_edid_connector_add_modes(connector);
  131. if (imx_ldb_ch->mode_valid) {
  132. struct drm_display_mode *mode;
  133. mode = drm_mode_duplicate(connector->dev, &imx_ldb_ch->mode);
  134. if (!mode)
  135. return -EINVAL;
  136. mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  137. drm_mode_probed_add(connector, mode);
  138. num_modes++;
  139. }
  140. return num_modes;
  141. }
  142. static void imx_ldb_set_clock(struct imx_ldb *ldb, int mux, int chno,
  143. unsigned long serial_clk, unsigned long di_clk)
  144. {
  145. int ret;
  146. dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
  147. clk_get_rate(ldb->clk_pll[chno]), serial_clk);
  148. clk_set_rate(ldb->clk_pll[chno], serial_clk);
  149. dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
  150. clk_get_rate(ldb->clk_pll[chno]));
  151. dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
  152. clk_get_rate(ldb->clk[chno]),
  153. (long int)di_clk);
  154. clk_set_rate(ldb->clk[chno], di_clk);
  155. dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
  156. clk_get_rate(ldb->clk[chno]));
  157. /* set display clock mux to LDB input clock */
  158. ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk[chno]);
  159. if (ret)
  160. dev_err(ldb->dev,
  161. "unable to set di%d parent clock to ldb_di%d\n", mux,
  162. chno);
  163. }
  164. static void imx_ldb_encoder_enable(struct drm_encoder *encoder)
  165. {
  166. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  167. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  168. int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
  169. int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
  170. if (mux < 0 || mux >= ARRAY_SIZE(ldb->clk_sel)) {
  171. dev_warn(ldb->dev, "%s: invalid mux %d\n", __func__, mux);
  172. return;
  173. }
  174. drm_panel_prepare(imx_ldb_ch->panel);
  175. if (dual) {
  176. clk_set_parent(ldb->clk_sel[mux], ldb->clk[0]);
  177. clk_set_parent(ldb->clk_sel[mux], ldb->clk[1]);
  178. clk_prepare_enable(ldb->clk[0]);
  179. clk_prepare_enable(ldb->clk[1]);
  180. } else {
  181. clk_set_parent(ldb->clk_sel[mux], ldb->clk[imx_ldb_ch->chno]);
  182. }
  183. if (imx_ldb_ch == &ldb->channel[0] || dual) {
  184. ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
  185. if (mux == 0 || ldb->lvds_mux)
  186. ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI0;
  187. else if (mux == 1)
  188. ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI1;
  189. }
  190. if (imx_ldb_ch == &ldb->channel[1] || dual) {
  191. ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
  192. if (mux == 1 || ldb->lvds_mux)
  193. ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI1;
  194. else if (mux == 0)
  195. ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI0;
  196. }
  197. if (ldb->lvds_mux) {
  198. const struct bus_mux *lvds_mux = NULL;
  199. if (imx_ldb_ch == &ldb->channel[0])
  200. lvds_mux = &ldb->lvds_mux[0];
  201. else if (imx_ldb_ch == &ldb->channel[1])
  202. lvds_mux = &ldb->lvds_mux[1];
  203. regmap_update_bits(ldb->regmap, lvds_mux->reg, lvds_mux->mask,
  204. mux << lvds_mux->shift);
  205. }
  206. regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
  207. drm_panel_enable(imx_ldb_ch->panel);
  208. }
  209. static void
  210. imx_ldb_encoder_atomic_mode_set(struct drm_encoder *encoder,
  211. struct drm_crtc_state *crtc_state,
  212. struct drm_connector_state *connector_state)
  213. {
  214. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  215. struct drm_display_mode *mode = &crtc_state->adjusted_mode;
  216. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  217. int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
  218. unsigned long serial_clk;
  219. unsigned long di_clk = mode->clock * 1000;
  220. int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
  221. u32 bus_format = imx_ldb_ch->bus_format;
  222. if (mux < 0 || mux >= ARRAY_SIZE(ldb->clk_sel)) {
  223. dev_warn(ldb->dev, "%s: invalid mux %d\n", __func__, mux);
  224. return;
  225. }
  226. if (mode->clock > 170000) {
  227. dev_warn(ldb->dev,
  228. "%s: mode exceeds 170 MHz pixel clock\n", __func__);
  229. }
  230. if (mode->clock > 85000 && !dual) {
  231. dev_warn(ldb->dev,
  232. "%s: mode exceeds 85 MHz pixel clock\n", __func__);
  233. }
  234. if (!IS_ALIGNED(mode->hdisplay, 8)) {
  235. dev_warn(ldb->dev,
  236. "%s: hdisplay does not align to 8 byte\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] || dual) {
  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] || dual) {
  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. if (!bus_format) {
  261. struct drm_connector *connector = connector_state->connector;
  262. struct drm_display_info *di = &connector->display_info;
  263. if (di->num_bus_formats)
  264. bus_format = di->bus_formats[0];
  265. }
  266. imx_ldb_ch_set_bus_format(imx_ldb_ch, bus_format);
  267. }
  268. static void imx_ldb_encoder_disable(struct drm_encoder *encoder)
  269. {
  270. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  271. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  272. int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
  273. int mux, ret;
  274. drm_panel_disable(imx_ldb_ch->panel);
  275. if (imx_ldb_ch == &ldb->channel[0] || dual)
  276. ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
  277. if (imx_ldb_ch == &ldb->channel[1] || dual)
  278. ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
  279. regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
  280. if (dual) {
  281. clk_disable_unprepare(ldb->clk[0]);
  282. clk_disable_unprepare(ldb->clk[1]);
  283. }
  284. if (ldb->lvds_mux) {
  285. const struct bus_mux *lvds_mux = NULL;
  286. if (imx_ldb_ch == &ldb->channel[0])
  287. lvds_mux = &ldb->lvds_mux[0];
  288. else if (imx_ldb_ch == &ldb->channel[1])
  289. lvds_mux = &ldb->lvds_mux[1];
  290. regmap_read(ldb->regmap, lvds_mux->reg, &mux);
  291. mux &= lvds_mux->mask;
  292. mux >>= lvds_mux->shift;
  293. } else {
  294. mux = (imx_ldb_ch == &ldb->channel[0]) ? 0 : 1;
  295. }
  296. /* set display clock mux back to original input clock */
  297. ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk_parent[mux]);
  298. if (ret)
  299. dev_err(ldb->dev,
  300. "unable to set di%d parent clock to original parent\n",
  301. mux);
  302. drm_panel_unprepare(imx_ldb_ch->panel);
  303. }
  304. static int imx_ldb_encoder_atomic_check(struct drm_encoder *encoder,
  305. struct drm_crtc_state *crtc_state,
  306. struct drm_connector_state *conn_state)
  307. {
  308. struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
  309. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  310. struct drm_display_info *di = &conn_state->connector->display_info;
  311. u32 bus_format = imx_ldb_ch->bus_format;
  312. /* Bus format description in DT overrides connector display info. */
  313. if (!bus_format && di->num_bus_formats) {
  314. bus_format = di->bus_formats[0];
  315. imx_crtc_state->bus_flags = di->bus_flags;
  316. } else {
  317. bus_format = imx_ldb_ch->bus_format;
  318. imx_crtc_state->bus_flags = imx_ldb_ch->bus_flags;
  319. }
  320. switch (bus_format) {
  321. case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
  322. imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
  323. break;
  324. case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
  325. case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
  326. imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  327. break;
  328. default:
  329. return -EINVAL;
  330. }
  331. imx_crtc_state->di_hsync_pin = 2;
  332. imx_crtc_state->di_vsync_pin = 3;
  333. return 0;
  334. }
  335. static const struct drm_connector_funcs imx_ldb_connector_funcs = {
  336. .fill_modes = drm_helper_probe_single_connector_modes,
  337. .destroy = imx_drm_connector_destroy,
  338. .reset = drm_atomic_helper_connector_reset,
  339. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  340. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  341. };
  342. static const struct drm_connector_helper_funcs imx_ldb_connector_helper_funcs = {
  343. .get_modes = imx_ldb_connector_get_modes,
  344. };
  345. static const struct drm_encoder_helper_funcs imx_ldb_encoder_helper_funcs = {
  346. .atomic_mode_set = imx_ldb_encoder_atomic_mode_set,
  347. .enable = imx_ldb_encoder_enable,
  348. .disable = imx_ldb_encoder_disable,
  349. .atomic_check = imx_ldb_encoder_atomic_check,
  350. };
  351. static int imx_ldb_get_clk(struct imx_ldb *ldb, int chno)
  352. {
  353. char clkname[16];
  354. snprintf(clkname, sizeof(clkname), "di%d", chno);
  355. ldb->clk[chno] = devm_clk_get(ldb->dev, clkname);
  356. if (IS_ERR(ldb->clk[chno]))
  357. return PTR_ERR(ldb->clk[chno]);
  358. snprintf(clkname, sizeof(clkname), "di%d_pll", chno);
  359. ldb->clk_pll[chno] = devm_clk_get(ldb->dev, clkname);
  360. return PTR_ERR_OR_ZERO(ldb->clk_pll[chno]);
  361. }
  362. static int imx_ldb_register(struct drm_device *drm,
  363. struct imx_ldb_channel *imx_ldb_ch)
  364. {
  365. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  366. struct imx_ldb_encoder *ldb_encoder;
  367. struct drm_connector *connector;
  368. struct drm_encoder *encoder;
  369. int ret;
  370. ldb_encoder = drmm_simple_encoder_alloc(drm, struct imx_ldb_encoder,
  371. encoder, DRM_MODE_ENCODER_LVDS);
  372. if (IS_ERR(ldb_encoder))
  373. return PTR_ERR(ldb_encoder);
  374. ldb_encoder->channel = imx_ldb_ch;
  375. connector = &ldb_encoder->connector;
  376. encoder = &ldb_encoder->encoder;
  377. ret = imx_drm_encoder_parse_of(drm, encoder, imx_ldb_ch->child);
  378. if (ret)
  379. return ret;
  380. ret = imx_ldb_get_clk(ldb, imx_ldb_ch->chno);
  381. if (ret)
  382. return ret;
  383. if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
  384. ret = imx_ldb_get_clk(ldb, 1);
  385. if (ret)
  386. return ret;
  387. }
  388. drm_encoder_helper_add(encoder, &imx_ldb_encoder_helper_funcs);
  389. if (imx_ldb_ch->bridge) {
  390. ret = drm_bridge_attach(encoder, imx_ldb_ch->bridge, NULL, 0);
  391. if (ret)
  392. return ret;
  393. } else {
  394. /*
  395. * We want to add the connector whenever there is no bridge
  396. * that brings its own, not only when there is a panel. For
  397. * historical reasons, the ldb driver can also work without
  398. * a panel.
  399. */
  400. drm_connector_helper_add(connector,
  401. &imx_ldb_connector_helper_funcs);
  402. drm_connector_init_with_ddc(drm, connector,
  403. &imx_ldb_connector_funcs,
  404. DRM_MODE_CONNECTOR_LVDS,
  405. imx_ldb_ch->ddc);
  406. drm_connector_attach_encoder(connector, encoder);
  407. }
  408. return 0;
  409. }
  410. struct imx_ldb_bit_mapping {
  411. u32 bus_format;
  412. u32 datawidth;
  413. const char * const mapping;
  414. };
  415. static const struct imx_ldb_bit_mapping imx_ldb_bit_mappings[] = {
  416. { MEDIA_BUS_FMT_RGB666_1X7X3_SPWG, 18, "spwg" },
  417. { MEDIA_BUS_FMT_RGB888_1X7X4_SPWG, 24, "spwg" },
  418. { MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA, 24, "jeida" },
  419. };
  420. static u32 of_get_bus_format(struct device *dev, struct device_node *np)
  421. {
  422. const char *bm;
  423. u32 datawidth = 0;
  424. int ret, i;
  425. ret = of_property_read_string(np, "fsl,data-mapping", &bm);
  426. if (ret < 0)
  427. return ret;
  428. of_property_read_u32(np, "fsl,data-width", &datawidth);
  429. for (i = 0; i < ARRAY_SIZE(imx_ldb_bit_mappings); i++) {
  430. if (!strcasecmp(bm, imx_ldb_bit_mappings[i].mapping) &&
  431. datawidth == imx_ldb_bit_mappings[i].datawidth)
  432. return imx_ldb_bit_mappings[i].bus_format;
  433. }
  434. dev_err(dev, "invalid data mapping: %d-bit \"%s\"\n", datawidth, bm);
  435. return -ENOENT;
  436. }
  437. static struct bus_mux imx6q_lvds_mux[2] = {
  438. {
  439. .reg = IOMUXC_GPR3,
  440. .shift = 6,
  441. .mask = IMX6Q_GPR3_LVDS0_MUX_CTL_MASK,
  442. }, {
  443. .reg = IOMUXC_GPR3,
  444. .shift = 8,
  445. .mask = IMX6Q_GPR3_LVDS1_MUX_CTL_MASK,
  446. }
  447. };
  448. /*
  449. * For a device declaring compatible = "fsl,imx6q-ldb", "fsl,imx53-ldb",
  450. * of_match_device will walk through this list and take the first entry
  451. * matching any of its compatible values. Therefore, the more generic
  452. * entries (in this case fsl,imx53-ldb) need to be ordered last.
  453. */
  454. static const struct of_device_id imx_ldb_dt_ids[] = {
  455. { .compatible = "fsl,imx6q-ldb", .data = imx6q_lvds_mux, },
  456. { .compatible = "fsl,imx53-ldb", .data = NULL, },
  457. { }
  458. };
  459. MODULE_DEVICE_TABLE(of, imx_ldb_dt_ids);
  460. static int imx_ldb_panel_ddc(struct device *dev,
  461. struct imx_ldb_channel *channel, struct device_node *child)
  462. {
  463. struct device_node *ddc_node;
  464. int ret;
  465. ddc_node = of_parse_phandle(child, "ddc-i2c-bus", 0);
  466. if (ddc_node) {
  467. channel->ddc = of_find_i2c_adapter_by_node(ddc_node);
  468. of_node_put(ddc_node);
  469. if (!channel->ddc) {
  470. dev_warn(dev, "failed to get ddc i2c adapter\n");
  471. return -EPROBE_DEFER;
  472. }
  473. }
  474. if (!channel->ddc) {
  475. const void *edidp;
  476. int edid_len;
  477. /* if no DDC available, fallback to hardcoded EDID */
  478. dev_dbg(dev, "no ddc available\n");
  479. edidp = of_get_property(child, "edid", &edid_len);
  480. if (edidp) {
  481. channel->drm_edid = drm_edid_alloc(edidp, edid_len);
  482. if (!channel->drm_edid)
  483. return -ENOMEM;
  484. } else if (!channel->panel) {
  485. /* fallback to display-timings node */
  486. ret = of_get_drm_display_mode(child,
  487. &channel->mode,
  488. &channel->bus_flags,
  489. OF_USE_NATIVE_MODE);
  490. if (!ret)
  491. channel->mode_valid = 1;
  492. }
  493. }
  494. return 0;
  495. }
  496. static int imx_ldb_bind(struct device *dev, struct device *master, void *data)
  497. {
  498. struct drm_device *drm = data;
  499. struct imx_ldb *imx_ldb = dev_get_drvdata(dev);
  500. int ret;
  501. int i;
  502. for (i = 0; i < 2; i++) {
  503. struct imx_ldb_channel *channel = &imx_ldb->channel[i];
  504. if (!channel->ldb)
  505. continue;
  506. ret = imx_ldb_register(drm, channel);
  507. if (ret)
  508. return ret;
  509. }
  510. return 0;
  511. }
  512. static const struct component_ops imx_ldb_ops = {
  513. .bind = imx_ldb_bind,
  514. };
  515. static int imx_ldb_probe(struct platform_device *pdev)
  516. {
  517. struct device *dev = &pdev->dev;
  518. struct device_node *np = dev->of_node;
  519. struct device_node *child;
  520. struct imx_ldb *imx_ldb;
  521. int dual;
  522. int ret;
  523. int i;
  524. imx_ldb = devm_kzalloc(dev, sizeof(*imx_ldb), GFP_KERNEL);
  525. if (!imx_ldb)
  526. return -ENOMEM;
  527. imx_ldb->regmap = syscon_regmap_lookup_by_phandle(np, "gpr");
  528. if (IS_ERR(imx_ldb->regmap)) {
  529. dev_err(dev, "failed to get parent regmap\n");
  530. return PTR_ERR(imx_ldb->regmap);
  531. }
  532. /* disable LDB by resetting the control register to POR default */
  533. regmap_write(imx_ldb->regmap, IOMUXC_GPR2, 0);
  534. imx_ldb->dev = dev;
  535. imx_ldb->lvds_mux = device_get_match_data(dev);
  536. dual = of_property_read_bool(np, "fsl,dual-channel");
  537. if (dual)
  538. imx_ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN;
  539. /*
  540. * There are three different possible clock mux configurations:
  541. * i.MX53: ipu1_di0_sel, ipu1_di1_sel
  542. * i.MX6q: ipu1_di0_sel, ipu1_di1_sel, ipu2_di0_sel, ipu2_di1_sel
  543. * i.MX6dl: ipu1_di0_sel, ipu1_di1_sel, lcdif_sel
  544. * Map them all to di0_sel...di3_sel.
  545. */
  546. for (i = 0; i < 4; i++) {
  547. char clkname[16];
  548. snprintf(clkname, sizeof(clkname), "di%d_sel", i);
  549. imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
  550. if (IS_ERR(imx_ldb->clk_sel[i])) {
  551. ret = PTR_ERR(imx_ldb->clk_sel[i]);
  552. imx_ldb->clk_sel[i] = NULL;
  553. break;
  554. }
  555. imx_ldb->clk_parent[i] = clk_get_parent(imx_ldb->clk_sel[i]);
  556. }
  557. if (i == 0)
  558. return ret;
  559. for_each_child_of_node(np, child) {
  560. struct imx_ldb_channel *channel;
  561. int bus_format;
  562. ret = of_property_read_u32(child, "reg", &i);
  563. if (ret || i < 0 || i > 1) {
  564. ret = -EINVAL;
  565. goto free_child;
  566. }
  567. if (!of_device_is_available(child))
  568. continue;
  569. if (dual && i > 0) {
  570. dev_warn(dev, "dual-channel mode, ignoring second output\n");
  571. continue;
  572. }
  573. channel = &imx_ldb->channel[i];
  574. channel->ldb = imx_ldb;
  575. channel->chno = i;
  576. /*
  577. * The output port is port@4 with an external 4-port mux or
  578. * port@2 with the internal 2-port mux.
  579. */
  580. ret = drm_of_find_panel_or_bridge(child,
  581. imx_ldb->lvds_mux ? 4 : 2, 0,
  582. &channel->panel, &channel->bridge);
  583. if (ret && ret != -ENODEV)
  584. goto free_child;
  585. /* panel ddc only if there is no bridge */
  586. if (!channel->bridge) {
  587. ret = imx_ldb_panel_ddc(dev, channel, child);
  588. if (ret)
  589. goto free_child;
  590. }
  591. bus_format = of_get_bus_format(dev, child);
  592. if (bus_format == -EINVAL) {
  593. /*
  594. * If no bus format was specified in the device tree,
  595. * we can still get it from the connected panel later.
  596. */
  597. if (channel->panel && channel->panel->funcs &&
  598. channel->panel->funcs->get_modes)
  599. bus_format = 0;
  600. }
  601. if (bus_format < 0) {
  602. dev_err(dev, "could not determine data mapping: %d\n",
  603. bus_format);
  604. ret = bus_format;
  605. goto free_child;
  606. }
  607. channel->bus_format = bus_format;
  608. channel->child = child;
  609. }
  610. platform_set_drvdata(pdev, imx_ldb);
  611. return component_add(&pdev->dev, &imx_ldb_ops);
  612. free_child:
  613. of_node_put(child);
  614. return ret;
  615. }
  616. static void imx_ldb_remove(struct platform_device *pdev)
  617. {
  618. struct imx_ldb *imx_ldb = platform_get_drvdata(pdev);
  619. int i;
  620. for (i = 0; i < 2; i++) {
  621. struct imx_ldb_channel *channel = &imx_ldb->channel[i];
  622. drm_edid_free(channel->drm_edid);
  623. i2c_put_adapter(channel->ddc);
  624. }
  625. component_del(&pdev->dev, &imx_ldb_ops);
  626. }
  627. static struct platform_driver imx_ldb_driver = {
  628. .probe = imx_ldb_probe,
  629. .remove_new = imx_ldb_remove,
  630. .driver = {
  631. .of_match_table = imx_ldb_dt_ids,
  632. .name = DRIVER_NAME,
  633. },
  634. };
  635. module_platform_driver(imx_ldb_driver);
  636. MODULE_DESCRIPTION("i.MX LVDS driver");
  637. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  638. MODULE_LICENSE("GPL");
  639. MODULE_ALIAS("platform:" DRIVER_NAME);