imx-ldb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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_atomic.h>
  20. #include <drm/drm_atomic_helper.h>
  21. #include <drm/drm_fb_helper.h>
  22. #include <drm/drm_crtc_helper.h>
  23. #include <drm/drm_of.h>
  24. #include <drm/drm_panel.h>
  25. #include <linux/mfd/syscon.h>
  26. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_graph.h>
  29. #include <video/of_display_timing.h>
  30. #include <video/of_videomode.h>
  31. #include <linux/regmap.h>
  32. #include <linux/videodev2.h>
  33. #include "imx-drm.h"
  34. #define DRIVER_NAME "imx-ldb"
  35. #define LDB_CH0_MODE_EN_TO_DI0 (1 << 0)
  36. #define LDB_CH0_MODE_EN_TO_DI1 (3 << 0)
  37. #define LDB_CH0_MODE_EN_MASK (3 << 0)
  38. #define LDB_CH1_MODE_EN_TO_DI0 (1 << 2)
  39. #define LDB_CH1_MODE_EN_TO_DI1 (3 << 2)
  40. #define LDB_CH1_MODE_EN_MASK (3 << 2)
  41. #define LDB_SPLIT_MODE_EN (1 << 4)
  42. #define LDB_DATA_WIDTH_CH0_24 (1 << 5)
  43. #define LDB_BIT_MAP_CH0_JEIDA (1 << 6)
  44. #define LDB_DATA_WIDTH_CH1_24 (1 << 7)
  45. #define LDB_BIT_MAP_CH1_JEIDA (1 << 8)
  46. #define LDB_DI0_VS_POL_ACT_LOW (1 << 9)
  47. #define LDB_DI1_VS_POL_ACT_LOW (1 << 10)
  48. #define LDB_BGREF_RMODE_INT (1 << 15)
  49. struct imx_ldb;
  50. struct imx_ldb_channel {
  51. struct imx_ldb *ldb;
  52. struct drm_connector connector;
  53. struct drm_encoder encoder;
  54. /* Defines what is connected to the ldb, only one at a time */
  55. struct drm_panel *panel;
  56. struct drm_bridge *bridge;
  57. struct device_node *child;
  58. struct i2c_adapter *ddc;
  59. int chno;
  60. void *edid;
  61. int edid_len;
  62. struct drm_display_mode mode;
  63. int mode_valid;
  64. u32 bus_format;
  65. u32 bus_flags;
  66. };
  67. static inline struct imx_ldb_channel *con_to_imx_ldb_ch(struct drm_connector *c)
  68. {
  69. return container_of(c, struct imx_ldb_channel, connector);
  70. }
  71. static inline struct imx_ldb_channel *enc_to_imx_ldb_ch(struct drm_encoder *e)
  72. {
  73. return container_of(e, struct imx_ldb_channel, encoder);
  74. }
  75. struct bus_mux {
  76. int reg;
  77. int shift;
  78. int mask;
  79. };
  80. struct imx_ldb {
  81. struct regmap *regmap;
  82. struct device *dev;
  83. struct imx_ldb_channel channel[2];
  84. struct clk *clk[2]; /* our own clock */
  85. struct clk *clk_sel[4]; /* parent of display clock */
  86. struct clk *clk_parent[4]; /* original parent of clk_sel */
  87. struct clk *clk_pll[2]; /* upstream clock we can adjust */
  88. u32 ldb_ctrl;
  89. const struct bus_mux *lvds_mux;
  90. };
  91. static enum drm_connector_status imx_ldb_connector_detect(
  92. struct drm_connector *connector, bool force)
  93. {
  94. return connector_status_connected;
  95. }
  96. static void imx_ldb_ch_set_bus_format(struct imx_ldb_channel *imx_ldb_ch,
  97. u32 bus_format)
  98. {
  99. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  100. int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
  101. switch (bus_format) {
  102. case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
  103. break;
  104. case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
  105. if (imx_ldb_ch->chno == 0 || dual)
  106. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24;
  107. if (imx_ldb_ch->chno == 1 || dual)
  108. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24;
  109. break;
  110. case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
  111. if (imx_ldb_ch->chno == 0 || dual)
  112. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24 |
  113. LDB_BIT_MAP_CH0_JEIDA;
  114. if (imx_ldb_ch->chno == 1 || dual)
  115. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24 |
  116. LDB_BIT_MAP_CH1_JEIDA;
  117. break;
  118. }
  119. }
  120. static int imx_ldb_connector_get_modes(struct drm_connector *connector)
  121. {
  122. struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
  123. int num_modes = 0;
  124. if (imx_ldb_ch->panel && imx_ldb_ch->panel->funcs &&
  125. imx_ldb_ch->panel->funcs->get_modes) {
  126. num_modes = imx_ldb_ch->panel->funcs->get_modes(imx_ldb_ch->panel);
  127. if (num_modes > 0)
  128. return num_modes;
  129. }
  130. if (!imx_ldb_ch->edid && imx_ldb_ch->ddc)
  131. imx_ldb_ch->edid = drm_get_edid(connector, imx_ldb_ch->ddc);
  132. if (imx_ldb_ch->edid) {
  133. drm_mode_connector_update_edid_property(connector,
  134. imx_ldb_ch->edid);
  135. num_modes = drm_add_edid_modes(connector, imx_ldb_ch->edid);
  136. }
  137. if (imx_ldb_ch->mode_valid) {
  138. struct drm_display_mode *mode;
  139. mode = drm_mode_create(connector->dev);
  140. if (!mode)
  141. return -EINVAL;
  142. drm_mode_copy(mode, &imx_ldb_ch->mode);
  143. mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  144. drm_mode_probed_add(connector, mode);
  145. num_modes++;
  146. }
  147. return num_modes;
  148. }
  149. static struct drm_encoder *imx_ldb_connector_best_encoder(
  150. struct drm_connector *connector)
  151. {
  152. struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
  153. return &imx_ldb_ch->encoder;
  154. }
  155. static void imx_ldb_set_clock(struct imx_ldb *ldb, int mux, int chno,
  156. unsigned long serial_clk, unsigned long di_clk)
  157. {
  158. int ret;
  159. dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
  160. clk_get_rate(ldb->clk_pll[chno]), serial_clk);
  161. clk_set_rate(ldb->clk_pll[chno], serial_clk);
  162. dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
  163. clk_get_rate(ldb->clk_pll[chno]));
  164. dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
  165. clk_get_rate(ldb->clk[chno]),
  166. (long int)di_clk);
  167. clk_set_rate(ldb->clk[chno], di_clk);
  168. dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
  169. clk_get_rate(ldb->clk[chno]));
  170. /* set display clock mux to LDB input clock */
  171. ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk[chno]);
  172. if (ret)
  173. dev_err(ldb->dev,
  174. "unable to set di%d parent clock to ldb_di%d\n", mux,
  175. chno);
  176. }
  177. static void imx_ldb_encoder_enable(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_set_parent(ldb->clk_sel[mux], ldb->clk[0]);
  186. clk_set_parent(ldb->clk_sel[mux], ldb->clk[1]);
  187. clk_prepare_enable(ldb->clk[0]);
  188. clk_prepare_enable(ldb->clk[1]);
  189. } else {
  190. clk_set_parent(ldb->clk_sel[mux], ldb->clk[imx_ldb_ch->chno]);
  191. }
  192. if (imx_ldb_ch == &ldb->channel[0] || dual) {
  193. ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
  194. if (mux == 0 || ldb->lvds_mux)
  195. ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI0;
  196. else if (mux == 1)
  197. ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI1;
  198. }
  199. if (imx_ldb_ch == &ldb->channel[1] || dual) {
  200. ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
  201. if (mux == 1 || ldb->lvds_mux)
  202. ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI1;
  203. else if (mux == 0)
  204. ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI0;
  205. }
  206. if (ldb->lvds_mux) {
  207. const struct bus_mux *lvds_mux = NULL;
  208. if (imx_ldb_ch == &ldb->channel[0])
  209. lvds_mux = &ldb->lvds_mux[0];
  210. else if (imx_ldb_ch == &ldb->channel[1])
  211. lvds_mux = &ldb->lvds_mux[1];
  212. regmap_update_bits(ldb->regmap, lvds_mux->reg, lvds_mux->mask,
  213. mux << lvds_mux->shift);
  214. }
  215. regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
  216. drm_panel_enable(imx_ldb_ch->panel);
  217. }
  218. static void
  219. imx_ldb_encoder_atomic_mode_set(struct drm_encoder *encoder,
  220. struct drm_crtc_state *crtc_state,
  221. struct drm_connector_state *connector_state)
  222. {
  223. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  224. struct drm_display_mode *mode = &crtc_state->adjusted_mode;
  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. u32 bus_format = imx_ldb_ch->bus_format;
  231. if (mode->clock > 170000) {
  232. dev_warn(ldb->dev,
  233. "%s: mode exceeds 170 MHz pixel clock\n", __func__);
  234. }
  235. if (mode->clock > 85000 && !dual) {
  236. dev_warn(ldb->dev,
  237. "%s: mode exceeds 85 MHz pixel clock\n", __func__);
  238. }
  239. if (dual) {
  240. serial_clk = 3500UL * mode->clock;
  241. imx_ldb_set_clock(ldb, mux, 0, serial_clk, di_clk);
  242. imx_ldb_set_clock(ldb, mux, 1, serial_clk, di_clk);
  243. } else {
  244. serial_clk = 7000UL * mode->clock;
  245. imx_ldb_set_clock(ldb, mux, imx_ldb_ch->chno, serial_clk,
  246. di_clk);
  247. }
  248. /* FIXME - assumes straight connections DI0 --> CH0, DI1 --> CH1 */
  249. if (imx_ldb_ch == &ldb->channel[0] || dual) {
  250. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  251. ldb->ldb_ctrl |= LDB_DI0_VS_POL_ACT_LOW;
  252. else if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  253. ldb->ldb_ctrl &= ~LDB_DI0_VS_POL_ACT_LOW;
  254. }
  255. if (imx_ldb_ch == &ldb->channel[1] || dual) {
  256. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  257. ldb->ldb_ctrl |= LDB_DI1_VS_POL_ACT_LOW;
  258. else if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  259. ldb->ldb_ctrl &= ~LDB_DI1_VS_POL_ACT_LOW;
  260. }
  261. if (!bus_format) {
  262. struct drm_connector *connector = connector_state->connector;
  263. struct drm_display_info *di = &connector->display_info;
  264. if (di->num_bus_formats)
  265. bus_format = di->bus_formats[0];
  266. }
  267. imx_ldb_ch_set_bus_format(imx_ldb_ch, bus_format);
  268. }
  269. static void imx_ldb_encoder_disable(struct drm_encoder *encoder)
  270. {
  271. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  272. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  273. int mux, ret;
  274. drm_panel_disable(imx_ldb_ch->panel);
  275. if (imx_ldb_ch == &ldb->channel[0])
  276. ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
  277. else if (imx_ldb_ch == &ldb->channel[1])
  278. ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
  279. regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
  280. if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
  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. .dpms = drm_atomic_helper_connector_dpms,
  337. .fill_modes = drm_helper_probe_single_connector_modes,
  338. .detect = imx_ldb_connector_detect,
  339. .destroy = imx_drm_connector_destroy,
  340. .reset = drm_atomic_helper_connector_reset,
  341. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  342. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  343. };
  344. static const struct drm_connector_helper_funcs imx_ldb_connector_helper_funcs = {
  345. .get_modes = imx_ldb_connector_get_modes,
  346. .best_encoder = imx_ldb_connector_best_encoder,
  347. };
  348. static const struct drm_encoder_funcs imx_ldb_encoder_funcs = {
  349. .destroy = imx_drm_encoder_destroy,
  350. };
  351. static const struct drm_encoder_helper_funcs imx_ldb_encoder_helper_funcs = {
  352. .atomic_mode_set = imx_ldb_encoder_atomic_mode_set,
  353. .enable = imx_ldb_encoder_enable,
  354. .disable = imx_ldb_encoder_disable,
  355. .atomic_check = imx_ldb_encoder_atomic_check,
  356. };
  357. static int imx_ldb_get_clk(struct imx_ldb *ldb, int chno)
  358. {
  359. char clkname[16];
  360. snprintf(clkname, sizeof(clkname), "di%d", chno);
  361. ldb->clk[chno] = devm_clk_get(ldb->dev, clkname);
  362. if (IS_ERR(ldb->clk[chno]))
  363. return PTR_ERR(ldb->clk[chno]);
  364. snprintf(clkname, sizeof(clkname), "di%d_pll", chno);
  365. ldb->clk_pll[chno] = devm_clk_get(ldb->dev, clkname);
  366. return PTR_ERR_OR_ZERO(ldb->clk_pll[chno]);
  367. }
  368. static int imx_ldb_register(struct drm_device *drm,
  369. struct imx_ldb_channel *imx_ldb_ch)
  370. {
  371. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  372. struct drm_encoder *encoder = &imx_ldb_ch->encoder;
  373. int ret;
  374. ret = imx_drm_encoder_parse_of(drm, encoder, imx_ldb_ch->child);
  375. if (ret)
  376. return ret;
  377. ret = imx_ldb_get_clk(ldb, imx_ldb_ch->chno);
  378. if (ret)
  379. return ret;
  380. if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
  381. ret = imx_ldb_get_clk(ldb, 1);
  382. if (ret)
  383. return ret;
  384. }
  385. drm_encoder_helper_add(encoder, &imx_ldb_encoder_helper_funcs);
  386. drm_encoder_init(drm, encoder, &imx_ldb_encoder_funcs,
  387. DRM_MODE_ENCODER_LVDS, NULL);
  388. if (imx_ldb_ch->bridge) {
  389. imx_ldb_ch->bridge->encoder = encoder;
  390. imx_ldb_ch->encoder.bridge = imx_ldb_ch->bridge;
  391. ret = drm_bridge_attach(drm, imx_ldb_ch->bridge);
  392. if (ret) {
  393. DRM_ERROR("Failed to initialize bridge with drm\n");
  394. return ret;
  395. }
  396. } else {
  397. /*
  398. * We want to add the connector whenever there is no bridge
  399. * that brings its own, not only when there is a panel. For
  400. * historical reasons, the ldb driver can also work without
  401. * a panel.
  402. */
  403. drm_connector_helper_add(&imx_ldb_ch->connector,
  404. &imx_ldb_connector_helper_funcs);
  405. drm_connector_init(drm, &imx_ldb_ch->connector,
  406. &imx_ldb_connector_funcs,
  407. DRM_MODE_CONNECTOR_LVDS);
  408. drm_mode_connector_attach_encoder(&imx_ldb_ch->connector,
  409. encoder);
  410. }
  411. if (imx_ldb_ch->panel) {
  412. ret = drm_panel_attach(imx_ldb_ch->panel,
  413. &imx_ldb_ch->connector);
  414. if (ret)
  415. return ret;
  416. }
  417. return 0;
  418. }
  419. enum {
  420. LVDS_BIT_MAP_SPWG,
  421. LVDS_BIT_MAP_JEIDA
  422. };
  423. struct imx_ldb_bit_mapping {
  424. u32 bus_format;
  425. u32 datawidth;
  426. const char * const mapping;
  427. };
  428. static const struct imx_ldb_bit_mapping imx_ldb_bit_mappings[] = {
  429. { MEDIA_BUS_FMT_RGB666_1X7X3_SPWG, 18, "spwg" },
  430. { MEDIA_BUS_FMT_RGB888_1X7X4_SPWG, 24, "spwg" },
  431. { MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA, 24, "jeida" },
  432. };
  433. static u32 of_get_bus_format(struct device *dev, struct device_node *np)
  434. {
  435. const char *bm;
  436. u32 datawidth = 0;
  437. int ret, i;
  438. ret = of_property_read_string(np, "fsl,data-mapping", &bm);
  439. if (ret < 0)
  440. return ret;
  441. of_property_read_u32(np, "fsl,data-width", &datawidth);
  442. for (i = 0; i < ARRAY_SIZE(imx_ldb_bit_mappings); i++) {
  443. if (!strcasecmp(bm, imx_ldb_bit_mappings[i].mapping) &&
  444. datawidth == imx_ldb_bit_mappings[i].datawidth)
  445. return imx_ldb_bit_mappings[i].bus_format;
  446. }
  447. dev_err(dev, "invalid data mapping: %d-bit \"%s\"\n", datawidth, bm);
  448. return -ENOENT;
  449. }
  450. static struct bus_mux imx6q_lvds_mux[2] = {
  451. {
  452. .reg = IOMUXC_GPR3,
  453. .shift = 6,
  454. .mask = IMX6Q_GPR3_LVDS0_MUX_CTL_MASK,
  455. }, {
  456. .reg = IOMUXC_GPR3,
  457. .shift = 8,
  458. .mask = IMX6Q_GPR3_LVDS1_MUX_CTL_MASK,
  459. }
  460. };
  461. /*
  462. * For a device declaring compatible = "fsl,imx6q-ldb", "fsl,imx53-ldb",
  463. * of_match_device will walk through this list and take the first entry
  464. * matching any of its compatible values. Therefore, the more generic
  465. * entries (in this case fsl,imx53-ldb) need to be ordered last.
  466. */
  467. static const struct of_device_id imx_ldb_dt_ids[] = {
  468. { .compatible = "fsl,imx6q-ldb", .data = imx6q_lvds_mux, },
  469. { .compatible = "fsl,imx53-ldb", .data = NULL, },
  470. { }
  471. };
  472. MODULE_DEVICE_TABLE(of, imx_ldb_dt_ids);
  473. static int imx_ldb_panel_ddc(struct device *dev,
  474. struct imx_ldb_channel *channel, struct device_node *child)
  475. {
  476. struct device_node *ddc_node;
  477. const u8 *edidp;
  478. int ret;
  479. ddc_node = of_parse_phandle(child, "ddc-i2c-bus", 0);
  480. if (ddc_node) {
  481. channel->ddc = of_find_i2c_adapter_by_node(ddc_node);
  482. of_node_put(ddc_node);
  483. if (!channel->ddc) {
  484. dev_warn(dev, "failed to get ddc i2c adapter\n");
  485. return -EPROBE_DEFER;
  486. }
  487. }
  488. if (!channel->ddc) {
  489. /* if no DDC available, fallback to hardcoded EDID */
  490. dev_dbg(dev, "no ddc available\n");
  491. edidp = of_get_property(child, "edid",
  492. &channel->edid_len);
  493. if (edidp) {
  494. channel->edid = kmemdup(edidp,
  495. channel->edid_len,
  496. GFP_KERNEL);
  497. } else if (!channel->panel) {
  498. /* fallback to display-timings node */
  499. ret = of_get_drm_display_mode(child,
  500. &channel->mode,
  501. &channel->bus_flags,
  502. OF_USE_NATIVE_MODE);
  503. if (!ret)
  504. channel->mode_valid = 1;
  505. }
  506. }
  507. return 0;
  508. }
  509. static int imx_ldb_bind(struct device *dev, struct device *master, void *data)
  510. {
  511. struct drm_device *drm = data;
  512. struct device_node *np = dev->of_node;
  513. const struct of_device_id *of_id =
  514. of_match_device(imx_ldb_dt_ids, dev);
  515. struct device_node *child;
  516. struct imx_ldb *imx_ldb;
  517. int dual;
  518. int ret;
  519. int i;
  520. imx_ldb = devm_kzalloc(dev, sizeof(*imx_ldb), GFP_KERNEL);
  521. if (!imx_ldb)
  522. return -ENOMEM;
  523. imx_ldb->regmap = syscon_regmap_lookup_by_phandle(np, "gpr");
  524. if (IS_ERR(imx_ldb->regmap)) {
  525. dev_err(dev, "failed to get parent regmap\n");
  526. return PTR_ERR(imx_ldb->regmap);
  527. }
  528. imx_ldb->dev = dev;
  529. if (of_id)
  530. imx_ldb->lvds_mux = of_id->data;
  531. dual = of_property_read_bool(np, "fsl,dual-channel");
  532. if (dual)
  533. imx_ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN;
  534. /*
  535. * There are three different possible clock mux configurations:
  536. * i.MX53: ipu1_di0_sel, ipu1_di1_sel
  537. * i.MX6q: ipu1_di0_sel, ipu1_di1_sel, ipu2_di0_sel, ipu2_di1_sel
  538. * i.MX6dl: ipu1_di0_sel, ipu1_di1_sel, lcdif_sel
  539. * Map them all to di0_sel...di3_sel.
  540. */
  541. for (i = 0; i < 4; i++) {
  542. char clkname[16];
  543. sprintf(clkname, "di%d_sel", i);
  544. imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
  545. if (IS_ERR(imx_ldb->clk_sel[i])) {
  546. ret = PTR_ERR(imx_ldb->clk_sel[i]);
  547. imx_ldb->clk_sel[i] = NULL;
  548. break;
  549. }
  550. imx_ldb->clk_parent[i] = clk_get_parent(imx_ldb->clk_sel[i]);
  551. }
  552. if (i == 0)
  553. return ret;
  554. for_each_child_of_node(np, child) {
  555. struct imx_ldb_channel *channel;
  556. struct device_node *ep;
  557. int bus_format;
  558. ret = of_property_read_u32(child, "reg", &i);
  559. if (ret || i < 0 || i > 1)
  560. return -EINVAL;
  561. if (dual && i > 0) {
  562. dev_warn(dev, "dual-channel mode, ignoring second output\n");
  563. continue;
  564. }
  565. if (!of_device_is_available(child))
  566. continue;
  567. channel = &imx_ldb->channel[i];
  568. channel->ldb = imx_ldb;
  569. channel->chno = i;
  570. channel->child = child;
  571. /*
  572. * The output port is port@4 with an external 4-port mux or
  573. * port@2 with the internal 2-port mux.
  574. */
  575. ep = of_graph_get_endpoint_by_regs(child,
  576. imx_ldb->lvds_mux ? 4 : 2,
  577. -1);
  578. if (ep) {
  579. struct device_node *remote;
  580. remote = of_graph_get_remote_port_parent(ep);
  581. of_node_put(ep);
  582. if (remote) {
  583. channel->panel = of_drm_find_panel(remote);
  584. channel->bridge = of_drm_find_bridge(remote);
  585. } else
  586. return -EPROBE_DEFER;
  587. of_node_put(remote);
  588. if (!channel->panel && !channel->bridge) {
  589. dev_err(dev, "panel/bridge not found: %s\n",
  590. remote->full_name);
  591. return -EPROBE_DEFER;
  592. }
  593. }
  594. /* panel ddc only if there is no bridge */
  595. if (!channel->bridge) {
  596. ret = imx_ldb_panel_ddc(dev, channel, child);
  597. if (ret)
  598. return ret;
  599. }
  600. bus_format = of_get_bus_format(dev, child);
  601. if (bus_format == -EINVAL) {
  602. /*
  603. * If no bus format was specified in the device tree,
  604. * we can still get it from the connected panel later.
  605. */
  606. if (channel->panel && channel->panel->funcs &&
  607. channel->panel->funcs->get_modes)
  608. bus_format = 0;
  609. }
  610. if (bus_format < 0) {
  611. dev_err(dev, "could not determine data mapping: %d\n",
  612. bus_format);
  613. return bus_format;
  614. }
  615. channel->bus_format = bus_format;
  616. ret = imx_ldb_register(drm, channel);
  617. if (ret)
  618. return ret;
  619. }
  620. dev_set_drvdata(dev, imx_ldb);
  621. return 0;
  622. }
  623. static void imx_ldb_unbind(struct device *dev, struct device *master,
  624. void *data)
  625. {
  626. struct imx_ldb *imx_ldb = dev_get_drvdata(dev);
  627. int i;
  628. for (i = 0; i < 2; i++) {
  629. struct imx_ldb_channel *channel = &imx_ldb->channel[i];
  630. if (channel->bridge)
  631. drm_bridge_detach(channel->bridge);
  632. if (channel->panel)
  633. drm_panel_detach(channel->panel);
  634. kfree(channel->edid);
  635. i2c_put_adapter(channel->ddc);
  636. }
  637. }
  638. static const struct component_ops imx_ldb_ops = {
  639. .bind = imx_ldb_bind,
  640. .unbind = imx_ldb_unbind,
  641. };
  642. static int imx_ldb_probe(struct platform_device *pdev)
  643. {
  644. return component_add(&pdev->dev, &imx_ldb_ops);
  645. }
  646. static int imx_ldb_remove(struct platform_device *pdev)
  647. {
  648. component_del(&pdev->dev, &imx_ldb_ops);
  649. return 0;
  650. }
  651. static struct platform_driver imx_ldb_driver = {
  652. .probe = imx_ldb_probe,
  653. .remove = imx_ldb_remove,
  654. .driver = {
  655. .of_match_table = imx_ldb_dt_ids,
  656. .name = DRIVER_NAME,
  657. },
  658. };
  659. module_platform_driver(imx_ldb_driver);
  660. MODULE_DESCRIPTION("i.MX LVDS driver");
  661. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  662. MODULE_LICENSE("GPL");
  663. MODULE_ALIAS("platform:" DRIVER_NAME);