parallel-display.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * i.MX drm driver - parallel display implementation
  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/component.h>
  16. #include <linux/module.h>
  17. #include <drm/drmP.h>
  18. #include <drm/drm_fb_helper.h>
  19. #include <drm/drm_crtc_helper.h>
  20. #include <drm/drm_panel.h>
  21. #include <linux/videodev2.h>
  22. #include <video/of_display_timing.h>
  23. #include <linux/of_graph.h>
  24. #include "imx-drm.h"
  25. #define con_to_imxpd(x) container_of(x, struct imx_parallel_display, connector)
  26. #define enc_to_imxpd(x) container_of(x, struct imx_parallel_display, encoder)
  27. struct imx_parallel_display {
  28. struct drm_connector connector;
  29. struct drm_encoder encoder;
  30. struct device *dev;
  31. void *edid;
  32. int edid_len;
  33. u32 bus_format;
  34. int mode_valid;
  35. struct drm_display_mode mode;
  36. struct drm_panel *panel;
  37. };
  38. static enum drm_connector_status imx_pd_connector_detect(
  39. struct drm_connector *connector, bool force)
  40. {
  41. return connector_status_connected;
  42. }
  43. static int imx_pd_connector_get_modes(struct drm_connector *connector)
  44. {
  45. struct imx_parallel_display *imxpd = con_to_imxpd(connector);
  46. struct device_node *np = imxpd->dev->of_node;
  47. int num_modes = 0;
  48. if (imxpd->panel && imxpd->panel->funcs &&
  49. imxpd->panel->funcs->get_modes) {
  50. struct drm_display_info *di = &connector->display_info;
  51. num_modes = imxpd->panel->funcs->get_modes(imxpd->panel);
  52. if (!imxpd->bus_format && di->num_bus_formats)
  53. imxpd->bus_format = di->bus_formats[0];
  54. if (num_modes > 0)
  55. return num_modes;
  56. }
  57. if (imxpd->edid) {
  58. drm_mode_connector_update_edid_property(connector, imxpd->edid);
  59. num_modes = drm_add_edid_modes(connector, imxpd->edid);
  60. }
  61. if (imxpd->mode_valid) {
  62. struct drm_display_mode *mode = drm_mode_create(connector->dev);
  63. if (!mode)
  64. return -EINVAL;
  65. drm_mode_copy(mode, &imxpd->mode);
  66. mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  67. drm_mode_probed_add(connector, mode);
  68. num_modes++;
  69. }
  70. if (np) {
  71. struct drm_display_mode *mode = drm_mode_create(connector->dev);
  72. if (!mode)
  73. return -EINVAL;
  74. of_get_drm_display_mode(np, &imxpd->mode, OF_USE_NATIVE_MODE);
  75. drm_mode_copy(mode, &imxpd->mode);
  76. mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
  77. drm_mode_probed_add(connector, mode);
  78. num_modes++;
  79. }
  80. return num_modes;
  81. }
  82. static struct drm_encoder *imx_pd_connector_best_encoder(
  83. struct drm_connector *connector)
  84. {
  85. struct imx_parallel_display *imxpd = con_to_imxpd(connector);
  86. return &imxpd->encoder;
  87. }
  88. static void imx_pd_encoder_dpms(struct drm_encoder *encoder, int mode)
  89. {
  90. struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
  91. if (mode != DRM_MODE_DPMS_ON)
  92. drm_panel_disable(imxpd->panel);
  93. else
  94. drm_panel_enable(imxpd->panel);
  95. }
  96. static bool imx_pd_encoder_mode_fixup(struct drm_encoder *encoder,
  97. const struct drm_display_mode *mode,
  98. struct drm_display_mode *adjusted_mode)
  99. {
  100. return true;
  101. }
  102. static void imx_pd_encoder_prepare(struct drm_encoder *encoder)
  103. {
  104. struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
  105. imx_drm_set_bus_format(encoder, imxpd->bus_format);
  106. }
  107. static void imx_pd_encoder_commit(struct drm_encoder *encoder)
  108. {
  109. struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
  110. drm_panel_prepare(imxpd->panel);
  111. drm_panel_enable(imxpd->panel);
  112. }
  113. static void imx_pd_encoder_mode_set(struct drm_encoder *encoder,
  114. struct drm_display_mode *orig_mode,
  115. struct drm_display_mode *mode)
  116. {
  117. }
  118. static void imx_pd_encoder_disable(struct drm_encoder *encoder)
  119. {
  120. struct imx_parallel_display *imxpd = enc_to_imxpd(encoder);
  121. drm_panel_disable(imxpd->panel);
  122. drm_panel_unprepare(imxpd->panel);
  123. }
  124. static struct drm_connector_funcs imx_pd_connector_funcs = {
  125. .dpms = drm_helper_connector_dpms,
  126. .fill_modes = drm_helper_probe_single_connector_modes,
  127. .detect = imx_pd_connector_detect,
  128. .destroy = imx_drm_connector_destroy,
  129. };
  130. static struct drm_connector_helper_funcs imx_pd_connector_helper_funcs = {
  131. .get_modes = imx_pd_connector_get_modes,
  132. .best_encoder = imx_pd_connector_best_encoder,
  133. };
  134. static struct drm_encoder_funcs imx_pd_encoder_funcs = {
  135. .destroy = imx_drm_encoder_destroy,
  136. };
  137. static struct drm_encoder_helper_funcs imx_pd_encoder_helper_funcs = {
  138. .dpms = imx_pd_encoder_dpms,
  139. .mode_fixup = imx_pd_encoder_mode_fixup,
  140. .prepare = imx_pd_encoder_prepare,
  141. .commit = imx_pd_encoder_commit,
  142. .mode_set = imx_pd_encoder_mode_set,
  143. .disable = imx_pd_encoder_disable,
  144. };
  145. static int imx_pd_register(struct drm_device *drm,
  146. struct imx_parallel_display *imxpd)
  147. {
  148. int ret;
  149. ret = imx_drm_encoder_parse_of(drm, &imxpd->encoder,
  150. imxpd->dev->of_node);
  151. if (ret)
  152. return ret;
  153. /* set the connector's dpms to OFF so that
  154. * drm_helper_connector_dpms() won't return
  155. * immediately since the current state is ON
  156. * at this point.
  157. */
  158. imxpd->connector.dpms = DRM_MODE_DPMS_OFF;
  159. drm_encoder_helper_add(&imxpd->encoder, &imx_pd_encoder_helper_funcs);
  160. drm_encoder_init(drm, &imxpd->encoder, &imx_pd_encoder_funcs,
  161. DRM_MODE_ENCODER_NONE);
  162. drm_connector_helper_add(&imxpd->connector,
  163. &imx_pd_connector_helper_funcs);
  164. drm_connector_init(drm, &imxpd->connector, &imx_pd_connector_funcs,
  165. DRM_MODE_CONNECTOR_VGA);
  166. if (imxpd->panel)
  167. drm_panel_attach(imxpd->panel, &imxpd->connector);
  168. drm_mode_connector_attach_encoder(&imxpd->connector, &imxpd->encoder);
  169. imxpd->connector.encoder = &imxpd->encoder;
  170. return 0;
  171. }
  172. static int imx_pd_bind(struct device *dev, struct device *master, void *data)
  173. {
  174. struct drm_device *drm = data;
  175. struct device_node *np = dev->of_node;
  176. struct device_node *port;
  177. const u8 *edidp;
  178. struct imx_parallel_display *imxpd;
  179. int ret;
  180. const char *fmt;
  181. imxpd = devm_kzalloc(dev, sizeof(*imxpd), GFP_KERNEL);
  182. if (!imxpd)
  183. return -ENOMEM;
  184. edidp = of_get_property(np, "edid", &imxpd->edid_len);
  185. if (edidp)
  186. imxpd->edid = kmemdup(edidp, imxpd->edid_len, GFP_KERNEL);
  187. ret = of_property_read_string(np, "interface-pix-fmt", &fmt);
  188. if (!ret) {
  189. if (!strcmp(fmt, "rgb24"))
  190. imxpd->bus_format = MEDIA_BUS_FMT_RGB888_1X24;
  191. else if (!strcmp(fmt, "rgb565"))
  192. imxpd->bus_format = MEDIA_BUS_FMT_RGB565_1X16;
  193. else if (!strcmp(fmt, "bgr666"))
  194. imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X18;
  195. else if (!strcmp(fmt, "lvds666"))
  196. imxpd->bus_format = MEDIA_BUS_FMT_RGB666_1X24_CPADHI;
  197. }
  198. /* port@1 is the output port */
  199. port = of_graph_get_port_by_id(np, 1);
  200. if (port) {
  201. struct device_node *endpoint, *remote;
  202. endpoint = of_get_child_by_name(port, "endpoint");
  203. if (endpoint) {
  204. remote = of_graph_get_remote_port_parent(endpoint);
  205. if (remote)
  206. imxpd->panel = of_drm_find_panel(remote);
  207. if (!imxpd->panel)
  208. return -EPROBE_DEFER;
  209. }
  210. }
  211. imxpd->dev = dev;
  212. ret = imx_pd_register(drm, imxpd);
  213. if (ret)
  214. return ret;
  215. dev_set_drvdata(dev, imxpd);
  216. return 0;
  217. }
  218. static void imx_pd_unbind(struct device *dev, struct device *master,
  219. void *data)
  220. {
  221. struct imx_parallel_display *imxpd = dev_get_drvdata(dev);
  222. imxpd->encoder.funcs->destroy(&imxpd->encoder);
  223. imxpd->connector.funcs->destroy(&imxpd->connector);
  224. kfree(imxpd->edid);
  225. }
  226. static const struct component_ops imx_pd_ops = {
  227. .bind = imx_pd_bind,
  228. .unbind = imx_pd_unbind,
  229. };
  230. static int imx_pd_probe(struct platform_device *pdev)
  231. {
  232. return component_add(&pdev->dev, &imx_pd_ops);
  233. }
  234. static int imx_pd_remove(struct platform_device *pdev)
  235. {
  236. component_del(&pdev->dev, &imx_pd_ops);
  237. return 0;
  238. }
  239. static const struct of_device_id imx_pd_dt_ids[] = {
  240. { .compatible = "fsl,imx-parallel-display", },
  241. { /* sentinel */ }
  242. };
  243. MODULE_DEVICE_TABLE(of, imx_pd_dt_ids);
  244. static struct platform_driver imx_pd_driver = {
  245. .probe = imx_pd_probe,
  246. .remove = imx_pd_remove,
  247. .driver = {
  248. .of_match_table = imx_pd_dt_ids,
  249. .name = "imx-parallel-display",
  250. },
  251. };
  252. module_platform_driver(imx_pd_driver);
  253. MODULE_DESCRIPTION("i.MX parallel display driver");
  254. MODULE_AUTHOR("Sascha Hauer, Pengutronix");
  255. MODULE_LICENSE("GPL");
  256. MODULE_ALIAS("platform:imx-parallel-display");