imx-ldb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. /*
  275. * imx_ldb_encoder_disable is called by
  276. * drm_helper_disable_unused_functions without
  277. * the encoder being enabled before.
  278. */
  279. if (imx_ldb_ch == &ldb->channel[0] &&
  280. (ldb->ldb_ctrl & LDB_CH0_MODE_EN_MASK) == 0)
  281. return;
  282. else if (imx_ldb_ch == &ldb->channel[1] &&
  283. (ldb->ldb_ctrl & LDB_CH1_MODE_EN_MASK) == 0)
  284. return;
  285. drm_panel_disable(imx_ldb_ch->panel);
  286. if (imx_ldb_ch == &ldb->channel[0])
  287. ldb->ldb_ctrl &= ~LDB_CH0_MODE_EN_MASK;
  288. else if (imx_ldb_ch == &ldb->channel[1])
  289. ldb->ldb_ctrl &= ~LDB_CH1_MODE_EN_MASK;
  290. regmap_write(ldb->regmap, IOMUXC_GPR2, ldb->ldb_ctrl);
  291. if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
  292. clk_disable_unprepare(ldb->clk[0]);
  293. clk_disable_unprepare(ldb->clk[1]);
  294. }
  295. if (ldb->lvds_mux) {
  296. const struct bus_mux *lvds_mux = NULL;
  297. if (imx_ldb_ch == &ldb->channel[0])
  298. lvds_mux = &ldb->lvds_mux[0];
  299. else if (imx_ldb_ch == &ldb->channel[1])
  300. lvds_mux = &ldb->lvds_mux[1];
  301. regmap_read(ldb->regmap, lvds_mux->reg, &mux);
  302. mux &= lvds_mux->mask;
  303. mux >>= lvds_mux->shift;
  304. } else {
  305. mux = (imx_ldb_ch == &ldb->channel[0]) ? 0 : 1;
  306. }
  307. /* set display clock mux back to original input clock */
  308. ret = clk_set_parent(ldb->clk_sel[mux], ldb->clk_parent[mux]);
  309. if (ret)
  310. dev_err(ldb->dev,
  311. "unable to set di%d parent clock to original parent\n",
  312. mux);
  313. drm_panel_unprepare(imx_ldb_ch->panel);
  314. }
  315. static int imx_ldb_encoder_atomic_check(struct drm_encoder *encoder,
  316. struct drm_crtc_state *crtc_state,
  317. struct drm_connector_state *conn_state)
  318. {
  319. struct imx_crtc_state *imx_crtc_state = to_imx_crtc_state(crtc_state);
  320. struct imx_ldb_channel *imx_ldb_ch = enc_to_imx_ldb_ch(encoder);
  321. struct drm_display_info *di = &conn_state->connector->display_info;
  322. u32 bus_format = imx_ldb_ch->bus_format;
  323. /* Bus format description in DT overrides connector display info. */
  324. if (!bus_format && di->num_bus_formats) {
  325. bus_format = di->bus_formats[0];
  326. imx_crtc_state->bus_flags = di->bus_flags;
  327. } else {
  328. bus_format = imx_ldb_ch->bus_format;
  329. imx_crtc_state->bus_flags = imx_ldb_ch->bus_flags;
  330. }
  331. switch (bus_format) {
  332. case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:
  333. imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
  334. break;
  335. case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:
  336. case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:
  337. imx_crtc_state->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  338. break;
  339. default:
  340. return -EINVAL;
  341. }
  342. imx_crtc_state->di_hsync_pin = 2;
  343. imx_crtc_state->di_vsync_pin = 3;
  344. return 0;
  345. }
  346. static const struct drm_connector_funcs imx_ldb_connector_funcs = {
  347. .dpms = drm_atomic_helper_connector_dpms,
  348. .fill_modes = drm_helper_probe_single_connector_modes,
  349. .detect = imx_ldb_connector_detect,
  350. .destroy = imx_drm_connector_destroy,
  351. .reset = drm_atomic_helper_connector_reset,
  352. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  353. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  354. };
  355. static const struct drm_connector_helper_funcs imx_ldb_connector_helper_funcs = {
  356. .get_modes = imx_ldb_connector_get_modes,
  357. .best_encoder = imx_ldb_connector_best_encoder,
  358. };
  359. static const struct drm_encoder_funcs imx_ldb_encoder_funcs = {
  360. .destroy = imx_drm_encoder_destroy,
  361. };
  362. static const struct drm_encoder_helper_funcs imx_ldb_encoder_helper_funcs = {
  363. .atomic_mode_set = imx_ldb_encoder_atomic_mode_set,
  364. .enable = imx_ldb_encoder_enable,
  365. .disable = imx_ldb_encoder_disable,
  366. .atomic_check = imx_ldb_encoder_atomic_check,
  367. };
  368. static int imx_ldb_get_clk(struct imx_ldb *ldb, int chno)
  369. {
  370. char clkname[16];
  371. snprintf(clkname, sizeof(clkname), "di%d", chno);
  372. ldb->clk[chno] = devm_clk_get(ldb->dev, clkname);
  373. if (IS_ERR(ldb->clk[chno]))
  374. return PTR_ERR(ldb->clk[chno]);
  375. snprintf(clkname, sizeof(clkname), "di%d_pll", chno);
  376. ldb->clk_pll[chno] = devm_clk_get(ldb->dev, clkname);
  377. return PTR_ERR_OR_ZERO(ldb->clk_pll[chno]);
  378. }
  379. static int imx_ldb_register(struct drm_device *drm,
  380. struct imx_ldb_channel *imx_ldb_ch)
  381. {
  382. struct imx_ldb *ldb = imx_ldb_ch->ldb;
  383. struct drm_encoder *encoder = &imx_ldb_ch->encoder;
  384. int ret;
  385. ret = imx_drm_encoder_parse_of(drm, encoder, imx_ldb_ch->child);
  386. if (ret)
  387. return ret;
  388. ret = imx_ldb_get_clk(ldb, imx_ldb_ch->chno);
  389. if (ret)
  390. return ret;
  391. if (ldb->ldb_ctrl & LDB_SPLIT_MODE_EN) {
  392. ret = imx_ldb_get_clk(ldb, 1);
  393. if (ret)
  394. return ret;
  395. }
  396. drm_encoder_helper_add(encoder, &imx_ldb_encoder_helper_funcs);
  397. drm_encoder_init(drm, encoder, &imx_ldb_encoder_funcs,
  398. DRM_MODE_ENCODER_LVDS, NULL);
  399. if (imx_ldb_ch->bridge) {
  400. imx_ldb_ch->bridge->encoder = encoder;
  401. imx_ldb_ch->encoder.bridge = imx_ldb_ch->bridge;
  402. ret = drm_bridge_attach(drm, imx_ldb_ch->bridge);
  403. if (ret) {
  404. DRM_ERROR("Failed to initialize bridge with drm\n");
  405. return ret;
  406. }
  407. } else {
  408. /*
  409. * We want to add the connector whenever there is no bridge
  410. * that brings its own, not only when there is a panel. For
  411. * historical reasons, the ldb driver can also work without
  412. * a panel.
  413. */
  414. drm_connector_helper_add(&imx_ldb_ch->connector,
  415. &imx_ldb_connector_helper_funcs);
  416. drm_connector_init(drm, &imx_ldb_ch->connector,
  417. &imx_ldb_connector_funcs,
  418. DRM_MODE_CONNECTOR_LVDS);
  419. drm_mode_connector_attach_encoder(&imx_ldb_ch->connector,
  420. encoder);
  421. }
  422. if (imx_ldb_ch->panel) {
  423. ret = drm_panel_attach(imx_ldb_ch->panel,
  424. &imx_ldb_ch->connector);
  425. if (ret)
  426. return ret;
  427. }
  428. return 0;
  429. }
  430. enum {
  431. LVDS_BIT_MAP_SPWG,
  432. LVDS_BIT_MAP_JEIDA
  433. };
  434. struct imx_ldb_bit_mapping {
  435. u32 bus_format;
  436. u32 datawidth;
  437. const char * const mapping;
  438. };
  439. static const struct imx_ldb_bit_mapping imx_ldb_bit_mappings[] = {
  440. { MEDIA_BUS_FMT_RGB666_1X7X3_SPWG, 18, "spwg" },
  441. { MEDIA_BUS_FMT_RGB888_1X7X4_SPWG, 24, "spwg" },
  442. { MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA, 24, "jeida" },
  443. };
  444. static u32 of_get_bus_format(struct device *dev, struct device_node *np)
  445. {
  446. const char *bm;
  447. u32 datawidth = 0;
  448. int ret, i;
  449. ret = of_property_read_string(np, "fsl,data-mapping", &bm);
  450. if (ret < 0)
  451. return ret;
  452. of_property_read_u32(np, "fsl,data-width", &datawidth);
  453. for (i = 0; i < ARRAY_SIZE(imx_ldb_bit_mappings); i++) {
  454. if (!strcasecmp(bm, imx_ldb_bit_mappings[i].mapping) &&
  455. datawidth == imx_ldb_bit_mappings[i].datawidth)
  456. return imx_ldb_bit_mappings[i].bus_format;
  457. }
  458. dev_err(dev, "invalid data mapping: %d-bit \"%s\"\n", datawidth, bm);
  459. return -ENOENT;
  460. }
  461. static struct bus_mux imx6q_lvds_mux[2] = {
  462. {
  463. .reg = IOMUXC_GPR3,
  464. .shift = 6,
  465. .mask = IMX6Q_GPR3_LVDS0_MUX_CTL_MASK,
  466. }, {
  467. .reg = IOMUXC_GPR3,
  468. .shift = 8,
  469. .mask = IMX6Q_GPR3_LVDS1_MUX_CTL_MASK,
  470. }
  471. };
  472. /*
  473. * For a device declaring compatible = "fsl,imx6q-ldb", "fsl,imx53-ldb",
  474. * of_match_device will walk through this list and take the first entry
  475. * matching any of its compatible values. Therefore, the more generic
  476. * entries (in this case fsl,imx53-ldb) need to be ordered last.
  477. */
  478. static const struct of_device_id imx_ldb_dt_ids[] = {
  479. { .compatible = "fsl,imx6q-ldb", .data = imx6q_lvds_mux, },
  480. { .compatible = "fsl,imx53-ldb", .data = NULL, },
  481. { }
  482. };
  483. MODULE_DEVICE_TABLE(of, imx_ldb_dt_ids);
  484. static int imx_ldb_panel_ddc(struct device *dev,
  485. struct imx_ldb_channel *channel, struct device_node *child)
  486. {
  487. struct device_node *ddc_node;
  488. const u8 *edidp;
  489. int ret;
  490. ddc_node = of_parse_phandle(child, "ddc-i2c-bus", 0);
  491. if (ddc_node) {
  492. channel->ddc = of_find_i2c_adapter_by_node(ddc_node);
  493. of_node_put(ddc_node);
  494. if (!channel->ddc) {
  495. dev_warn(dev, "failed to get ddc i2c adapter\n");
  496. return -EPROBE_DEFER;
  497. }
  498. }
  499. if (!channel->ddc) {
  500. /* if no DDC available, fallback to hardcoded EDID */
  501. dev_dbg(dev, "no ddc available\n");
  502. edidp = of_get_property(child, "edid",
  503. &channel->edid_len);
  504. if (edidp) {
  505. channel->edid = kmemdup(edidp,
  506. channel->edid_len,
  507. GFP_KERNEL);
  508. } else if (!channel->panel) {
  509. /* fallback to display-timings node */
  510. ret = of_get_drm_display_mode(child,
  511. &channel->mode,
  512. &channel->bus_flags,
  513. OF_USE_NATIVE_MODE);
  514. if (!ret)
  515. channel->mode_valid = 1;
  516. }
  517. }
  518. return 0;
  519. }
  520. static int imx_ldb_bind(struct device *dev, struct device *master, void *data)
  521. {
  522. struct drm_device *drm = data;
  523. struct device_node *np = dev->of_node;
  524. const struct of_device_id *of_id =
  525. of_match_device(imx_ldb_dt_ids, dev);
  526. struct device_node *child;
  527. struct imx_ldb *imx_ldb;
  528. int dual;
  529. int ret;
  530. int i;
  531. imx_ldb = devm_kzalloc(dev, sizeof(*imx_ldb), GFP_KERNEL);
  532. if (!imx_ldb)
  533. return -ENOMEM;
  534. imx_ldb->regmap = syscon_regmap_lookup_by_phandle(np, "gpr");
  535. if (IS_ERR(imx_ldb->regmap)) {
  536. dev_err(dev, "failed to get parent regmap\n");
  537. return PTR_ERR(imx_ldb->regmap);
  538. }
  539. imx_ldb->dev = dev;
  540. if (of_id)
  541. imx_ldb->lvds_mux = of_id->data;
  542. dual = of_property_read_bool(np, "fsl,dual-channel");
  543. if (dual)
  544. imx_ldb->ldb_ctrl |= LDB_SPLIT_MODE_EN;
  545. /*
  546. * There are three different possible clock mux configurations:
  547. * i.MX53: ipu1_di0_sel, ipu1_di1_sel
  548. * i.MX6q: ipu1_di0_sel, ipu1_di1_sel, ipu2_di0_sel, ipu2_di1_sel
  549. * i.MX6dl: ipu1_di0_sel, ipu1_di1_sel, lcdif_sel
  550. * Map them all to di0_sel...di3_sel.
  551. */
  552. for (i = 0; i < 4; i++) {
  553. char clkname[16];
  554. sprintf(clkname, "di%d_sel", i);
  555. imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
  556. if (IS_ERR(imx_ldb->clk_sel[i])) {
  557. ret = PTR_ERR(imx_ldb->clk_sel[i]);
  558. imx_ldb->clk_sel[i] = NULL;
  559. break;
  560. }
  561. imx_ldb->clk_parent[i] = clk_get_parent(imx_ldb->clk_sel[i]);
  562. }
  563. if (i == 0)
  564. return ret;
  565. for_each_child_of_node(np, child) {
  566. struct imx_ldb_channel *channel;
  567. struct device_node *ep;
  568. int bus_format;
  569. ret = of_property_read_u32(child, "reg", &i);
  570. if (ret || i < 0 || i > 1)
  571. return -EINVAL;
  572. if (dual && i > 0) {
  573. dev_warn(dev, "dual-channel mode, ignoring second output\n");
  574. continue;
  575. }
  576. if (!of_device_is_available(child))
  577. continue;
  578. channel = &imx_ldb->channel[i];
  579. channel->ldb = imx_ldb;
  580. channel->chno = i;
  581. channel->child = child;
  582. /*
  583. * The output port is port@4 with an external 4-port mux or
  584. * port@2 with the internal 2-port mux.
  585. */
  586. ep = of_graph_get_endpoint_by_regs(child,
  587. imx_ldb->lvds_mux ? 4 : 2,
  588. -1);
  589. if (ep) {
  590. struct device_node *remote;
  591. remote = of_graph_get_remote_port_parent(ep);
  592. of_node_put(ep);
  593. if (remote) {
  594. channel->panel = of_drm_find_panel(remote);
  595. channel->bridge = of_drm_find_bridge(remote);
  596. } else
  597. return -EPROBE_DEFER;
  598. of_node_put(remote);
  599. if (!channel->panel && !channel->bridge) {
  600. dev_err(dev, "panel/bridge not found: %s\n",
  601. remote->full_name);
  602. return -EPROBE_DEFER;
  603. }
  604. }
  605. /* panel ddc only if there is no bridge */
  606. if (!channel->bridge) {
  607. ret = imx_ldb_panel_ddc(dev, channel, child);
  608. if (ret)
  609. return ret;
  610. }
  611. bus_format = of_get_bus_format(dev, child);
  612. if (bus_format == -EINVAL) {
  613. /*
  614. * If no bus format was specified in the device tree,
  615. * we can still get it from the connected panel later.
  616. */
  617. if (channel->panel && channel->panel->funcs &&
  618. channel->panel->funcs->get_modes)
  619. bus_format = 0;
  620. }
  621. if (bus_format < 0) {
  622. dev_err(dev, "could not determine data mapping: %d\n",
  623. bus_format);
  624. return bus_format;
  625. }
  626. channel->bus_format = bus_format;
  627. ret = imx_ldb_register(drm, channel);
  628. if (ret)
  629. return ret;
  630. }
  631. dev_set_drvdata(dev, imx_ldb);
  632. return 0;
  633. }
  634. static void imx_ldb_unbind(struct device *dev, struct device *master,
  635. void *data)
  636. {
  637. struct imx_ldb *imx_ldb = dev_get_drvdata(dev);
  638. int i;
  639. for (i = 0; i < 2; i++) {
  640. struct imx_ldb_channel *channel = &imx_ldb->channel[i];
  641. if (channel->bridge)
  642. drm_bridge_detach(channel->bridge);
  643. if (channel->panel)
  644. drm_panel_detach(channel->panel);
  645. kfree(channel->edid);
  646. i2c_put_adapter(channel->ddc);
  647. }
  648. }
  649. static const struct component_ops imx_ldb_ops = {
  650. .bind = imx_ldb_bind,
  651. .unbind = imx_ldb_unbind,
  652. };
  653. static int imx_ldb_probe(struct platform_device *pdev)
  654. {
  655. return component_add(&pdev->dev, &imx_ldb_ops);
  656. }
  657. static int imx_ldb_remove(struct platform_device *pdev)
  658. {
  659. component_del(&pdev->dev, &imx_ldb_ops);
  660. return 0;
  661. }
  662. static struct platform_driver imx_ldb_driver = {
  663. .probe = imx_ldb_probe,
  664. .remove = imx_ldb_remove,
  665. .driver = {
  666. .of_match_table = imx_ldb_dt_ids,
  667. .name = DRIVER_NAME,
  668. },
  669. };
  670. module_platform_driver(imx_ldb_driver);
  671. MODULE_DESCRIPTION("i.MX LVDS driver");
  672. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  673. MODULE_LICENSE("GPL");
  674. MODULE_ALIAS("platform:" DRIVER_NAME);