exynos_drm_dpi.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Exynos DRM Parallel output support.
  3. *
  4. * Copyright (c) 2014 Samsung Electronics Co., Ltd
  5. *
  6. * Contacts: Andrzej Hajda <a.hajda@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <drm/drmP.h>
  13. #include <drm/drm_crtc_helper.h>
  14. #include <drm/drm_panel.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <video/of_videomode.h>
  18. #include <video/videomode.h>
  19. #include "exynos_drm_crtc.h"
  20. struct exynos_dpi {
  21. struct drm_encoder encoder;
  22. struct device *dev;
  23. struct device_node *panel_node;
  24. struct drm_panel *panel;
  25. struct drm_connector connector;
  26. struct videomode *vm;
  27. };
  28. #define connector_to_dpi(c) container_of(c, struct exynos_dpi, connector)
  29. static inline struct exynos_dpi *encoder_to_dpi(struct drm_encoder *e)
  30. {
  31. return container_of(e, struct exynos_dpi, encoder);
  32. }
  33. static enum drm_connector_status
  34. exynos_dpi_detect(struct drm_connector *connector, bool force)
  35. {
  36. struct exynos_dpi *ctx = connector_to_dpi(connector);
  37. if (ctx->panel && !ctx->panel->connector)
  38. drm_panel_attach(ctx->panel, &ctx->connector);
  39. return connector_status_connected;
  40. }
  41. static void exynos_dpi_connector_destroy(struct drm_connector *connector)
  42. {
  43. drm_connector_unregister(connector);
  44. drm_connector_cleanup(connector);
  45. }
  46. static struct drm_connector_funcs exynos_dpi_connector_funcs = {
  47. .dpms = drm_atomic_helper_connector_dpms,
  48. .detect = exynos_dpi_detect,
  49. .fill_modes = drm_helper_probe_single_connector_modes,
  50. .destroy = exynos_dpi_connector_destroy,
  51. .reset = drm_atomic_helper_connector_reset,
  52. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  53. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  54. };
  55. static int exynos_dpi_get_modes(struct drm_connector *connector)
  56. {
  57. struct exynos_dpi *ctx = connector_to_dpi(connector);
  58. /* fimd timings gets precedence over panel modes */
  59. if (ctx->vm) {
  60. struct drm_display_mode *mode;
  61. mode = drm_mode_create(connector->dev);
  62. if (!mode) {
  63. DRM_ERROR("failed to create a new display mode\n");
  64. return 0;
  65. }
  66. drm_display_mode_from_videomode(ctx->vm, mode);
  67. mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
  68. drm_mode_probed_add(connector, mode);
  69. return 1;
  70. }
  71. if (ctx->panel)
  72. return ctx->panel->funcs->get_modes(ctx->panel);
  73. return 0;
  74. }
  75. static struct drm_encoder *
  76. exynos_dpi_best_encoder(struct drm_connector *connector)
  77. {
  78. struct exynos_dpi *ctx = connector_to_dpi(connector);
  79. return &ctx->encoder;
  80. }
  81. static struct drm_connector_helper_funcs exynos_dpi_connector_helper_funcs = {
  82. .get_modes = exynos_dpi_get_modes,
  83. .best_encoder = exynos_dpi_best_encoder,
  84. };
  85. static int exynos_dpi_create_connector(struct drm_encoder *encoder)
  86. {
  87. struct exynos_dpi *ctx = encoder_to_dpi(encoder);
  88. struct drm_connector *connector = &ctx->connector;
  89. int ret;
  90. connector->polled = DRM_CONNECTOR_POLL_HPD;
  91. ret = drm_connector_init(encoder->dev, connector,
  92. &exynos_dpi_connector_funcs,
  93. DRM_MODE_CONNECTOR_VGA);
  94. if (ret) {
  95. DRM_ERROR("failed to initialize connector with drm\n");
  96. return ret;
  97. }
  98. drm_connector_helper_add(connector, &exynos_dpi_connector_helper_funcs);
  99. drm_connector_register(connector);
  100. drm_mode_connector_attach_encoder(connector, encoder);
  101. return 0;
  102. }
  103. static bool exynos_dpi_mode_fixup(struct drm_encoder *encoder,
  104. const struct drm_display_mode *mode,
  105. struct drm_display_mode *adjusted_mode)
  106. {
  107. return true;
  108. }
  109. static void exynos_dpi_mode_set(struct drm_encoder *encoder,
  110. struct drm_display_mode *mode,
  111. struct drm_display_mode *adjusted_mode)
  112. {
  113. }
  114. static void exynos_dpi_enable(struct drm_encoder *encoder)
  115. {
  116. struct exynos_dpi *ctx = encoder_to_dpi(encoder);
  117. if (ctx->panel) {
  118. drm_panel_prepare(ctx->panel);
  119. drm_panel_enable(ctx->panel);
  120. }
  121. }
  122. static void exynos_dpi_disable(struct drm_encoder *encoder)
  123. {
  124. struct exynos_dpi *ctx = encoder_to_dpi(encoder);
  125. if (ctx->panel) {
  126. drm_panel_disable(ctx->panel);
  127. drm_panel_unprepare(ctx->panel);
  128. }
  129. }
  130. static struct drm_encoder_helper_funcs exynos_dpi_encoder_helper_funcs = {
  131. .mode_fixup = exynos_dpi_mode_fixup,
  132. .mode_set = exynos_dpi_mode_set,
  133. .enable = exynos_dpi_enable,
  134. .disable = exynos_dpi_disable,
  135. };
  136. static struct drm_encoder_funcs exynos_dpi_encoder_funcs = {
  137. .destroy = drm_encoder_cleanup,
  138. };
  139. /* of_* functions will be removed after merge of of_graph patches */
  140. static struct device_node *
  141. of_get_child_by_name_reg(struct device_node *parent, const char *name, u32 reg)
  142. {
  143. struct device_node *np;
  144. for_each_child_of_node(parent, np) {
  145. u32 r;
  146. if (!np->name || of_node_cmp(np->name, name))
  147. continue;
  148. if (of_property_read_u32(np, "reg", &r) < 0)
  149. r = 0;
  150. if (reg == r)
  151. break;
  152. }
  153. return np;
  154. }
  155. static struct device_node *of_graph_get_port_by_reg(struct device_node *parent,
  156. u32 reg)
  157. {
  158. struct device_node *ports, *port;
  159. ports = of_get_child_by_name(parent, "ports");
  160. if (ports)
  161. parent = ports;
  162. port = of_get_child_by_name_reg(parent, "port", reg);
  163. of_node_put(ports);
  164. return port;
  165. }
  166. static struct device_node *
  167. of_graph_get_endpoint_by_reg(struct device_node *port, u32 reg)
  168. {
  169. return of_get_child_by_name_reg(port, "endpoint", reg);
  170. }
  171. static struct device_node *
  172. of_graph_get_remote_port_parent(const struct device_node *node)
  173. {
  174. struct device_node *np;
  175. unsigned int depth;
  176. np = of_parse_phandle(node, "remote-endpoint", 0);
  177. /* Walk 3 levels up only if there is 'ports' node. */
  178. for (depth = 3; depth && np; depth--) {
  179. np = of_get_next_parent(np);
  180. if (depth == 2 && of_node_cmp(np->name, "ports"))
  181. break;
  182. }
  183. return np;
  184. }
  185. enum {
  186. FIMD_PORT_IN0,
  187. FIMD_PORT_IN1,
  188. FIMD_PORT_IN2,
  189. FIMD_PORT_RGB,
  190. FIMD_PORT_WRB,
  191. };
  192. static struct device_node *exynos_dpi_of_find_panel_node(struct device *dev)
  193. {
  194. struct device_node *np, *ep;
  195. np = of_graph_get_port_by_reg(dev->of_node, FIMD_PORT_RGB);
  196. if (!np)
  197. return NULL;
  198. ep = of_graph_get_endpoint_by_reg(np, 0);
  199. of_node_put(np);
  200. if (!ep)
  201. return NULL;
  202. np = of_graph_get_remote_port_parent(ep);
  203. of_node_put(ep);
  204. return np;
  205. }
  206. static int exynos_dpi_parse_dt(struct exynos_dpi *ctx)
  207. {
  208. struct device *dev = ctx->dev;
  209. struct device_node *dn = dev->of_node;
  210. struct device_node *np;
  211. ctx->panel_node = exynos_dpi_of_find_panel_node(dev);
  212. np = of_get_child_by_name(dn, "display-timings");
  213. if (np) {
  214. struct videomode *vm;
  215. int ret;
  216. of_node_put(np);
  217. vm = devm_kzalloc(dev, sizeof(*ctx->vm), GFP_KERNEL);
  218. if (!vm)
  219. return -ENOMEM;
  220. ret = of_get_videomode(dn, vm, 0);
  221. if (ret < 0) {
  222. devm_kfree(dev, vm);
  223. return ret;
  224. }
  225. ctx->vm = vm;
  226. return 0;
  227. }
  228. if (!ctx->panel_node)
  229. return -EINVAL;
  230. return 0;
  231. }
  232. int exynos_dpi_bind(struct drm_device *dev, struct drm_encoder *encoder)
  233. {
  234. int ret;
  235. ret = exynos_drm_crtc_get_pipe_from_type(dev, EXYNOS_DISPLAY_TYPE_LCD);
  236. if (ret < 0)
  237. return ret;
  238. encoder->possible_crtcs = 1 << ret;
  239. DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
  240. drm_encoder_init(dev, encoder, &exynos_dpi_encoder_funcs,
  241. DRM_MODE_ENCODER_TMDS);
  242. drm_encoder_helper_add(encoder, &exynos_dpi_encoder_helper_funcs);
  243. ret = exynos_dpi_create_connector(encoder);
  244. if (ret) {
  245. DRM_ERROR("failed to create connector ret = %d\n", ret);
  246. drm_encoder_cleanup(encoder);
  247. return ret;
  248. }
  249. return 0;
  250. }
  251. struct drm_encoder *exynos_dpi_probe(struct device *dev)
  252. {
  253. struct exynos_dpi *ctx;
  254. int ret;
  255. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  256. if (!ctx)
  257. return ERR_PTR(-ENOMEM);
  258. ctx->dev = dev;
  259. ret = exynos_dpi_parse_dt(ctx);
  260. if (ret < 0) {
  261. devm_kfree(dev, ctx);
  262. return NULL;
  263. }
  264. if (ctx->panel_node) {
  265. ctx->panel = of_drm_find_panel(ctx->panel_node);
  266. if (!ctx->panel)
  267. return ERR_PTR(-EPROBE_DEFER);
  268. }
  269. return &ctx->encoder;
  270. }
  271. int exynos_dpi_remove(struct drm_encoder *encoder)
  272. {
  273. struct exynos_dpi *ctx = encoder_to_dpi(encoder);
  274. exynos_dpi_disable(&ctx->encoder);
  275. if (ctx->panel)
  276. drm_panel_detach(ctx->panel);
  277. return 0;
  278. }