imx-ldb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 void imx_ldb_ch_set_bus_format(struct imx_ldb_channel *imx_ldb_ch,
  92. u32 bus_format)
  93. {
  94. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  95. int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
  96. switch (bus_format) {
  97. case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
  98. break;
  99. case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
  100. if (imx_ldb_ch->chno == 0 || dual)
  101. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24;
  102. if (imx_ldb_ch->chno == 1 || dual)
  103. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24;
  104. break;
  105. case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
  106. if (imx_ldb_ch->chno == 0 || dual)
  107. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH0_24 |
  108. LDB_BIT_MAP_CH0_JEIDA;
  109. if (imx_ldb_ch->chno == 1 || dual)
  110. ldb->ldb_ctrl |= LDB_DATA_WIDTH_CH1_24 |
  111. LDB_BIT_MAP_CH1_JEIDA;
  112. break;
  113. }
  114. }
  115. static int imx_ldb_connector_get_modes(struct drm_connector *connector)
  116. {
  117. struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
  118. int num_modes = 0;
  119. if (imx_ldb_ch->panel && imx_ldb_ch->panel->funcs &&
  120. imx_ldb_ch->panel->funcs->get_modes) {
  121. num_modes = imx_ldb_ch->panel->funcs->get_modes(imx_ldb_ch->panel);
  122. if (num_modes > 0)
  123. return num_modes;
  124. }
  125. if (!imx_ldb_ch->edid && imx_ldb_ch->ddc)
  126. imx_ldb_ch->edid = drm_get_edid(connector, imx_ldb_ch->ddc);
  127. if (imx_ldb_ch->edid) {
  128. drm_connector_update_edid_property(connector,
  129. imx_ldb_ch->edid);
  130. num_modes = drm_add_edid_modes(connector, imx_ldb_ch->edid);
  131. }
  132. if (imx_ldb_ch->mode_valid) {
  133. struct drm_display_mode *mode;
  134. mode = drm_mode_create(connector->dev);
  135. if (!mode)
  136. return -EINVAL;
  137. drm_mode_copy(mode, &imx_ldb_ch->mode);
  138. mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  139. drm_mode_probed_add(connector, mode);
  140. num_modes++;
  141. }
  142. return num_modes;
  143. }
  144. static struct drm_encoder *imx_ldb_connector_best_encoder(
  145. struct drm_connector *connector)
  146. {
  147. struct imx_ldb_channel *imx_ldb_ch = con_to_imx_ldb_ch(connector);
  148. return &imx_ldb_ch->encoder;
  149. }
  150. static void imx_ldb_set_clock(struct imx_ldb *ldb, int mux, int chno,
  151. unsigned long serial_clk, unsigned long di_clk)
  152. {
  153. int ret;
  154. dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
  155. clk_get_rate(ldb->clk_pll[chno]), serial_clk);
  156. clk_set_rate(ldb->clk_pll[chno], serial_clk);
  157. dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
  158. clk_get_rate(ldb->clk_pll[chno]));
  159. dev_dbg(ldb->dev, "%s: now: %ld want: %ld\n", __func__,
  160. clk_get_rate(ldb->clk[chno]),
  161. (long int)di_clk);
  162. clk_set_rate(ldb->clk[chno], di_clk);
  163. dev_dbg(ldb->dev, "%s after: %ld\n", __func__,
  164. clk_get_rate(ldb->clk[chno]));
  165. /* set display clock mux to LDB input clock */
  166. ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk[chno]);
  167. if (ret)
  168. dev_err(ldb->dev,
  169. "unable to set di%d parent clock to ldb_di%d\n", mux,
  170. chno);
  171. }
  172. static void imx_ldb_encoder_enable(struct drm_encoder *encoder)
  173. {
  174. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  175. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  176. int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
  177. int mux = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
  178. if (mux < 0 || mux >= ARRAY_SIZE(ldb->clk_sel)) {
  179. dev_warn(ldb->dev, "%s: invalid mux %d\n", __func__, mux);
  180. return;
  181. }
  182. drm_panel_prepare(imx_ldb_ch->panel);
  183. if (dual) {
  184. clk_set_parent(ldb->clk_sel[mux], ldb->clk[0]);
  185. clk_set_parent(ldb->clk_sel[mux], ldb->clk[1]);
  186. clk_prepare_enable(ldb->clk[0]);
  187. clk_prepare_enable(ldb->clk[1]);
  188. } else {
  189. clk_set_parent(ldb->clk_sel[mux], ldb->clk[imx_ldb_ch->chno]);
  190. }
  191. if (imx_ldb_ch == &ldb->channel[0] || dual) {
  192. ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
  193. if (mux == 0 || ldb->lvds_mux)
  194. ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI0;
  195. else if (mux == 1)
  196. ldb->ldb_ctrl |= LDB_CH0_MODE_EN_TO_DI1;
  197. }
  198. if (imx_ldb_ch == &ldb->channel[1] || dual) {
  199. ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
  200. if (mux == 1 || ldb->lvds_mux)
  201. ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI1;
  202. else if (mux == 0)
  203. ldb->ldb_ctrl |= LDB_CH1_MODE_EN_TO_DI0;
  204. }
  205. if (ldb->lvds_mux) {
  206. const struct bus_mux *lvds_mux = NULL;
  207. if (imx_ldb_ch == &ldb->channel[0])
  208. lvds_mux = &ldb->lvds_mux[0];
  209. else if (imx_ldb_ch == &ldb->channel[1])
  210. lvds_mux = &ldb->lvds_mux[1];
  211. regmap_update_bits(ldb->regmap, lvds_mux->reg, lvds_mux->mask,
  212. mux << lvds_mux->shift);
  213. }
  214. regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
  215. drm_panel_enable(imx_ldb_ch->panel);
  216. }
  217. static void
  218. imx_ldb_encoder_atomic_mode_set(struct drm_encoder *encoder,
  219. struct drm_crtc_state *crtc_state,
  220. struct drm_connector_state *connector_state)
  221. {
  222. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  223. struct drm_display_mode *mode = &crtc_state->adjusted_mode;
  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 = drm_of_encoder_active_port_id(imx_ldb_ch->child, encoder);
  229. u32 bus_format = imx_ldb_ch->bus_format;
  230. if (mux < 0 || mux >= ARRAY_SIZE(ldb->clk_sel)) {
  231. dev_warn(ldb->dev, "%s: invalid mux %d\n", __func__, mux);
  232. return;
  233. }
  234. if (mode->clock > 170000) {
  235. dev_warn(ldb->dev,
  236. "%s: mode exceeds 170 MHz pixel clock\n", __func__);
  237. }
  238. if (mode->clock > 85000 && !dual) {
  239. dev_warn(ldb->dev,
  240. "%s: mode exceeds 85 MHz pixel clock\n", __func__);
  241. }
  242. if (dual) {
  243. serial_clk = 3500UL * mode->clock;
  244. imx_ldb_set_clock(ldb, mux, 0, serial_clk, di_clk);
  245. imx_ldb_set_clock(ldb, mux, 1, serial_clk, di_clk);
  246. } else {
  247. serial_clk = 7000UL * mode->clock;
  248. imx_ldb_set_clock(ldb, mux, imx_ldb_ch->chno, serial_clk,
  249. di_clk);
  250. }
  251. /* FIXME - assumes straight connections DI0 --> CH0, DI1 --> CH1 */
  252. if (imx_ldb_ch == &ldb->channel[0] || dual) {
  253. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  254. ldb->ldb_ctrl |= LDB_DI0_VS_POL_ACT_LOW;
  255. else if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  256. ldb->ldb_ctrl &= ~LDB_DI0_VS_POL_ACT_LOW;
  257. }
  258. if (imx_ldb_ch == &ldb->channel[1] || dual) {
  259. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  260. ldb->ldb_ctrl |= LDB_DI1_VS_POL_ACT_LOW;
  261. else if (mode->flags & DRM_MODE_FLAG_PVSYNC)
  262. ldb->ldb_ctrl &= ~LDB_DI1_VS_POL_ACT_LOW;
  263. }
  264. if (!bus_format) {
  265. struct drm_connector *connector = connector_state->connector;
  266. struct drm_display_info *di = &connector->display_info;
  267. if (di->num_bus_formats)
  268. bus_format = di->bus_formats[0];
  269. }
  270. imx_ldb_ch_set_bus_format(imx_ldb_ch, bus_format);
  271. }
  272. static void imx_ldb_encoder_disable(struct drm_encoder *encoder)
  273. {
  274. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  275. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  276. int dual = ldb->ldb_ctrl & LDB_SPLIT_MODE_EN;
  277. int mux, ret;
  278. drm_panel_disable(imx_ldb_ch->panel);
  279. if (imx_ldb_ch == &ldb->channel[0] || dual)
  280. ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
  281. if (imx_ldb_ch == &ldb->channel[1] || dual)
  282. ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
  283. regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
  284. if (dual) {
  285. clk_disable_unprepare(ldb->clk[0]);
  286. clk_disable_unprepare(ldb->clk[1]);
  287. }
  288. if (ldb->lvds_mux) {
  289. const struct bus_mux *lvds_mux = NULL;
  290. if (imx_ldb_ch == &ldb->channel[0])
  291. lvds_mux = &ldb->lvds_mux[0];
  292. else if (imx_ldb_ch == &ldb->channel[1])
  293. lvds_mux = &ldb->lvds_mux[1];
  294. regmap_read(ldb->regmap, lvds_mux->reg, &mux);
  295. mux &= lvds_mux->mask;
  296. mux >>= lvds_mux->shift;
  297. } else {
  298. mux = (imx_ldb_ch == &ldb->channel[0]) ? 0 : 1;
  299. }
  300. /* set display clock mux back to original input clock */
  301. ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk_parent[mux]);
  302. if (ret)
  303. dev_err(ldb->dev,
  304. "unable to set di%d parent clock to original parent\n",
  305. mux);
  306. drm_panel_unprepare(imx_ldb_ch->panel);
  307. }
  308. static int imx_ldb_encoder_atomic_check(struct drm_encoder *encoder,
  309. struct drm_crtc_state *crtc_state,
  310. struct drm_connector_state *conn_state)
  311. {
  312. struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
  313. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  314. struct drm_display_info *di = &conn_state->connector->display_info;
  315. u32 bus_format = imx_ldb_ch->bus_format;
  316. /* Bus format description in DT overrides connector display info. */
  317. if (!bus_format && di->num_bus_formats) {
  318. bus_format = di->bus_formats[0];
  319. imx_crtc_state->bus_flags = di->bus_flags;
  320. } else {
  321. bus_format = imx_ldb_ch->bus_format;
  322. imx_crtc_state->bus_flags = imx_ldb_ch->bus_flags;
  323. }
  324. switch (bus_format) {
  325. case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
  326. imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
  327. break;
  328. case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
  329. case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
  330. imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  331. break;
  332. default:
  333. return -EINVAL;
  334. }
  335. imx_crtc_state->di_hsync_pin = 2;
  336. imx_crtc_state->di_vsync_pin = 3;
  337. return 0;
  338. }
  339. static const struct drm_connector_funcs imx_ldb_connector_funcs = {
  340. .fill_modes = drm_helper_probe_single_connector_modes,
  341. .destroy = imx_drm_connector_destroy,
  342. .reset = drm_atomic_helper_connector_reset,
  343. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  344. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  345. };
  346. static const struct drm_connector_helper_funcs imx_ldb_connector_helper_funcs = {
  347. .get_modes = imx_ldb_connector_get_modes,
  348. .best_encoder = imx_ldb_connector_best_encoder,
  349. };
  350. static const struct drm_encoder_funcs imx_ldb_encoder_funcs = {
  351. .destroy = imx_drm_encoder_destroy,
  352. };
  353. static const struct drm_encoder_helper_funcs imx_ldb_encoder_helper_funcs = {
  354. .atomic_mode_set = imx_ldb_encoder_atomic_mode_set,
  355. .enable = imx_ldb_encoder_enable,
  356. .disable = imx_ldb_encoder_disable,
  357. .atomic_check = imx_ldb_encoder_atomic_check,
  358. };
  359. static int imx_ldb_get_clk(struct imx_ldb *ldb, int chno)
  360. {
  361. char clkname[16];
  362. snprintf(clkname, sizeof(clkname), "di%d", chno);
  363. ldb->clk[chno] = devm_clk_get(ldb->dev, clkname);
  364. if (IS_ERR(ldb->clk[chno]))
  365. return PTR_ERR(ldb->clk[chno]);
  366. snprintf(clkname, sizeof(clkname), "di%d_pll", chno);
  367. ldb->clk_pll[chno] = devm_clk_get(ldb->dev, clkname);
  368. return PTR_ERR_OR_ZERO(ldb->clk_pll[chno]);
  369. }
  370. static int imx_ldb_register(struct drm_device *drm,
  371. struct imx_ldb_channel *imx_ldb_ch)
  372. {
  373. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  374. struct drm_encoder *encoder = &imx_ldb_ch->encoder;
  375. int ret;
  376. ret = imx_drm_encoder_parse_of(drm, encoder, imx_ldb_ch->child);
  377. if (ret)
  378. return ret;
  379. ret = imx_ldb_get_clk(ldb, imx_ldb_ch->chno);
  380. if (ret)
  381. return ret;
  382. if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
  383. ret = imx_ldb_get_clk(ldb, 1);
  384. if (ret)
  385. return ret;
  386. }
  387. drm_encoder_helper_add(encoder, &imx_ldb_encoder_helper_funcs);
  388. drm_encoder_init(drm, encoder, &imx_ldb_encoder_funcs,
  389. DRM_MODE_ENCODER_LVDS, NULL);
  390. if (imx_ldb_ch->bridge) {
  391. ret = drm_bridge_attach(&imx_ldb_ch->encoder,
  392. imx_ldb_ch->bridge, NULL);
  393. if (ret) {
  394. DRM_ERROR("Failed to initialize bridge with drm\n");
  395. return ret;
  396. }
  397. } else {
  398. /*
  399. * We want to add the connector whenever there is no bridge
  400. * that brings its own, not only when there is a panel. For
  401. * historical reasons, the ldb driver can also work without
  402. * a panel.
  403. */
  404. drm_connector_helper_add(&imx_ldb_ch->connector,
  405. &imx_ldb_connector_helper_funcs);
  406. drm_connector_init(drm, &imx_ldb_ch->connector,
  407. &imx_ldb_connector_funcs,
  408. DRM_MODE_CONNECTOR_LVDS);
  409. drm_connector_attach_encoder(&imx_ldb_ch->connector, 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. /* disable LDB by resetting the control register to POR default */
  529. regmap_write(imx_ldb->regmap, IOMUXC_GPR2, 0);
  530. imx_ldb->dev = dev;
  531. if (of_id)
  532. imx_ldb->lvds_mux = of_id->data;
  533. dual = of_property_read_bool(np, "fsl,dual-channel");
  534. if (dual)
  535. imx_ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN;
  536. /*
  537. * There are three different possible clock mux configurations:
  538. * i.MX53: ipu1_di0_sel, ipu1_di1_sel
  539. * i.MX6q: ipu1_di0_sel, ipu1_di1_sel, ipu2_di0_sel, ipu2_di1_sel
  540. * i.MX6dl: ipu1_di0_sel, ipu1_di1_sel, lcdif_sel
  541. * Map them all to di0_sel...di3_sel.
  542. */
  543. for (i = 0; i < 4; i++) {
  544. char clkname[16];
  545. sprintf(clkname, "di%d_sel", i);
  546. imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
  547. if (IS_ERR(imx_ldb->clk_sel[i])) {
  548. ret = PTR_ERR(imx_ldb->clk_sel[i]);
  549. imx_ldb->clk_sel[i] = NULL;
  550. break;
  551. }
  552. imx_ldb->clk_parent[i] = clk_get_parent(imx_ldb->clk_sel[i]);
  553. }
  554. if (i == 0)
  555. return ret;
  556. for_each_child_of_node(np, child) {
  557. struct imx_ldb_channel *channel;
  558. int bus_format;
  559. ret = of_property_read_u32(child, "reg", &i);
  560. if (ret || i < 0 || i > 1) {
  561. ret = -EINVAL;
  562. goto free_child;
  563. }
  564. if (!of_device_is_available(child))
  565. continue;
  566. if (dual && i > 0) {
  567. dev_warn(dev, "dual-channel mode, ignoring second output\n");
  568. continue;
  569. }
  570. channel = &imx_ldb->channel[i];
  571. channel->ldb = imx_ldb;
  572. channel->chno = i;
  573. /*
  574. * The output port is port@4 with an external 4-port mux or
  575. * port@2 with the internal 2-port mux.
  576. */
  577. ret = drm_of_find_panel_or_bridge(child,
  578. imx_ldb->lvds_mux ? 4 : 2, 0,
  579. &channel->panel, &channel->bridge);
  580. if (ret && ret != -ENODEV)
  581. goto free_child;
  582. /* panel ddc only if there is no bridge */
  583. if (!channel->bridge) {
  584. ret = imx_ldb_panel_ddc(dev, channel, child);
  585. if (ret)
  586. goto free_child;
  587. }
  588. bus_format = of_get_bus_format(dev, child);
  589. if (bus_format == -EINVAL) {
  590. /*
  591. * If no bus format was specified in the device tree,
  592. * we can still get it from the connected panel later.
  593. */
  594. if (channel->panel && channel->panel->funcs &&
  595. channel->panel->funcs->get_modes)
  596. bus_format = 0;
  597. }
  598. if (bus_format < 0) {
  599. dev_err(dev, "could not determine data mapping: %d\n",
  600. bus_format);
  601. ret = bus_format;
  602. goto free_child;
  603. }
  604. channel->bus_format = bus_format;
  605. channel->child = child;
  606. ret = imx_ldb_register(drm, channel);
  607. if (ret) {
  608. channel->child = NULL;
  609. goto free_child;
  610. }
  611. }
  612. dev_set_drvdata(dev, imx_ldb);
  613. return 0;
  614. free_child:
  615. of_node_put(child);
  616. return ret;
  617. }
  618. static void imx_ldb_unbind(struct device *dev, struct device *master,
  619. void *data)
  620. {
  621. struct imx_ldb *imx_ldb = dev_get_drvdata(dev);
  622. int i;
  623. for (i = 0; i < 2; i++) {
  624. struct imx_ldb_channel *channel = &imx_ldb->channel[i];
  625. if (channel->panel)
  626. drm_panel_detach(channel->panel);
  627. kfree(channel->edid);
  628. i2c_put_adapter(channel->ddc);
  629. }
  630. }
  631. static const struct component_ops imx_ldb_ops = {
  632. .bind = imx_ldb_bind,
  633. .unbind = imx_ldb_unbind,
  634. };
  635. static int imx_ldb_probe(struct platform_device *pdev)
  636. {
  637. return component_add(&pdev->dev, &imx_ldb_ops);
  638. }
  639. static int imx_ldb_remove(struct platform_device *pdev)
  640. {
  641. component_del(&pdev->dev, &imx_ldb_ops);
  642. return 0;
  643. }
  644. static struct platform_driver imx_ldb_driver = {
  645. .probe = imx_ldb_probe,
  646. .remove = imx_ldb_remove,
  647. .driver = {
  648. .of_match_table = imx_ldb_dt_ids,
  649. .name = DRIVER_NAME,
  650. },
  651. };
  652. module_platform_driver(imx_ldb_driver);
  653. MODULE_DESCRIPTION("i.MX LVDS driver");
  654. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  655. MODULE_LICENSE("GPL");
  656. MODULE_ALIAS("platform:" DRIVER_NAME);