atmel_hlcdc_output.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (C) 2014 Traphandler
  3. * Copyright (C) 2014 Free Electrons
  4. * Copyright (C) 2014 Atmel
  5. *
  6. * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
  7. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/of_graph.h>
  22. #include <drm/drmP.h>
  23. #include <drm/drm_panel.h>
  24. #include "atmel_hlcdc_dc.h"
  25. /**
  26. * Atmel HLCDC RGB output mode
  27. */
  28. enum atmel_hlcdc_connector_rgb_mode {
  29. ATMEL_HLCDC_CONNECTOR_RGB444,
  30. ATMEL_HLCDC_CONNECTOR_RGB565,
  31. ATMEL_HLCDC_CONNECTOR_RGB666,
  32. ATMEL_HLCDC_CONNECTOR_RGB888,
  33. };
  34. /**
  35. * Atmel HLCDC RGB connector structure
  36. *
  37. * This structure stores RGB slave device information.
  38. *
  39. * @connector: DRM connector
  40. * @encoder: DRM encoder
  41. * @dc: pointer to the atmel_hlcdc_dc structure
  42. * @dpms: current DPMS mode
  43. */
  44. struct atmel_hlcdc_rgb_output {
  45. struct drm_connector connector;
  46. struct drm_encoder encoder;
  47. struct atmel_hlcdc_dc *dc;
  48. int dpms;
  49. };
  50. static inline struct atmel_hlcdc_rgb_output *
  51. drm_connector_to_atmel_hlcdc_rgb_output(struct drm_connector *connector)
  52. {
  53. return container_of(connector, struct atmel_hlcdc_rgb_output,
  54. connector);
  55. }
  56. static inline struct atmel_hlcdc_rgb_output *
  57. drm_encoder_to_atmel_hlcdc_rgb_output(struct drm_encoder *encoder)
  58. {
  59. return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
  60. }
  61. /**
  62. * Atmel HLCDC Panel device structure
  63. *
  64. * This structure is specialization of the slave device structure to
  65. * interface with drm panels.
  66. *
  67. * @base: base slave device fields
  68. * @panel: drm panel attached to this slave device
  69. */
  70. struct atmel_hlcdc_panel {
  71. struct atmel_hlcdc_rgb_output base;
  72. struct drm_panel *panel;
  73. };
  74. static inline struct atmel_hlcdc_panel *
  75. atmel_hlcdc_rgb_output_to_panel(struct atmel_hlcdc_rgb_output *output)
  76. {
  77. return container_of(output, struct atmel_hlcdc_panel, base);
  78. }
  79. static void atmel_hlcdc_panel_encoder_enable(struct drm_encoder *encoder)
  80. {
  81. struct atmel_hlcdc_rgb_output *rgb =
  82. drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
  83. struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
  84. drm_panel_enable(panel->panel);
  85. }
  86. static void atmel_hlcdc_panel_encoder_disable(struct drm_encoder *encoder)
  87. {
  88. struct atmel_hlcdc_rgb_output *rgb =
  89. drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
  90. struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
  91. drm_panel_disable(panel->panel);
  92. }
  93. static bool
  94. atmel_hlcdc_panel_encoder_mode_fixup(struct drm_encoder *encoder,
  95. const struct drm_display_mode *mode,
  96. struct drm_display_mode *adjusted)
  97. {
  98. return true;
  99. }
  100. static void
  101. atmel_hlcdc_rgb_encoder_mode_set(struct drm_encoder *encoder,
  102. struct drm_display_mode *mode,
  103. struct drm_display_mode *adjusted)
  104. {
  105. struct atmel_hlcdc_rgb_output *rgb =
  106. drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
  107. struct drm_display_info *info = &rgb->connector.display_info;
  108. unsigned int cfg;
  109. cfg = 0;
  110. if (info->num_bus_formats) {
  111. switch (info->bus_formats[0]) {
  112. case MEDIA_BUS_FMT_RGB565_1X16:
  113. cfg |= ATMEL_HLCDC_CONNECTOR_RGB565 << 8;
  114. break;
  115. case MEDIA_BUS_FMT_RGB666_1X18:
  116. cfg |= ATMEL_HLCDC_CONNECTOR_RGB666 << 8;
  117. break;
  118. case MEDIA_BUS_FMT_RGB888_1X24:
  119. cfg |= ATMEL_HLCDC_CONNECTOR_RGB888 << 8;
  120. break;
  121. case MEDIA_BUS_FMT_RGB444_1X12:
  122. default:
  123. break;
  124. }
  125. }
  126. regmap_update_bits(rgb->dc->hlcdc->regmap, ATMEL_HLCDC_CFG(5),
  127. ATMEL_HLCDC_MODE_MASK,
  128. cfg);
  129. }
  130. static struct drm_encoder_helper_funcs atmel_hlcdc_panel_encoder_helper_funcs = {
  131. .mode_fixup = atmel_hlcdc_panel_encoder_mode_fixup,
  132. .mode_set = atmel_hlcdc_rgb_encoder_mode_set,
  133. .disable = atmel_hlcdc_panel_encoder_disable,
  134. .enable = atmel_hlcdc_panel_encoder_enable,
  135. };
  136. static void atmel_hlcdc_rgb_encoder_destroy(struct drm_encoder *encoder)
  137. {
  138. drm_encoder_cleanup(encoder);
  139. memset(encoder, 0, sizeof(*encoder));
  140. }
  141. static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = {
  142. .destroy = atmel_hlcdc_rgb_encoder_destroy,
  143. };
  144. static int atmel_hlcdc_panel_get_modes(struct drm_connector *connector)
  145. {
  146. struct atmel_hlcdc_rgb_output *rgb =
  147. drm_connector_to_atmel_hlcdc_rgb_output(connector);
  148. struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
  149. return panel->panel->funcs->get_modes(panel->panel);
  150. }
  151. static int atmel_hlcdc_rgb_mode_valid(struct drm_connector *connector,
  152. struct drm_display_mode *mode)
  153. {
  154. struct atmel_hlcdc_rgb_output *rgb =
  155. drm_connector_to_atmel_hlcdc_rgb_output(connector);
  156. return atmel_hlcdc_dc_mode_valid(rgb->dc, mode);
  157. }
  158. static struct drm_encoder *
  159. atmel_hlcdc_rgb_best_encoder(struct drm_connector *connector)
  160. {
  161. struct atmel_hlcdc_rgb_output *rgb =
  162. drm_connector_to_atmel_hlcdc_rgb_output(connector);
  163. return &rgb->encoder;
  164. }
  165. static struct drm_connector_helper_funcs atmel_hlcdc_panel_connector_helper_funcs = {
  166. .get_modes = atmel_hlcdc_panel_get_modes,
  167. .mode_valid = atmel_hlcdc_rgb_mode_valid,
  168. .best_encoder = atmel_hlcdc_rgb_best_encoder,
  169. };
  170. static enum drm_connector_status
  171. atmel_hlcdc_panel_connector_detect(struct drm_connector *connector, bool force)
  172. {
  173. return connector_status_connected;
  174. }
  175. static void
  176. atmel_hlcdc_panel_connector_destroy(struct drm_connector *connector)
  177. {
  178. struct atmel_hlcdc_rgb_output *rgb =
  179. drm_connector_to_atmel_hlcdc_rgb_output(connector);
  180. struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
  181. drm_panel_detach(panel->panel);
  182. drm_connector_cleanup(connector);
  183. }
  184. static const struct drm_connector_funcs atmel_hlcdc_panel_connector_funcs = {
  185. .dpms = drm_atomic_helper_connector_dpms,
  186. .detect = atmel_hlcdc_panel_connector_detect,
  187. .fill_modes = drm_helper_probe_single_connector_modes,
  188. .destroy = atmel_hlcdc_panel_connector_destroy,
  189. .reset = drm_atomic_helper_connector_reset,
  190. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  191. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  192. };
  193. static int atmel_hlcdc_create_panel_output(struct drm_device *dev,
  194. struct of_endpoint *ep)
  195. {
  196. struct atmel_hlcdc_dc *dc = dev->dev_private;
  197. struct device_node *np;
  198. struct drm_panel *p = NULL;
  199. struct atmel_hlcdc_panel *panel;
  200. int ret;
  201. np = of_graph_get_remote_port_parent(ep->local_node);
  202. if (!np)
  203. return -EINVAL;
  204. p = of_drm_find_panel(np);
  205. of_node_put(np);
  206. if (!p)
  207. return -EPROBE_DEFER;
  208. panel = devm_kzalloc(dev->dev, sizeof(*panel), GFP_KERNEL);
  209. if (!panel)
  210. return -EINVAL;
  211. panel->base.dpms = DRM_MODE_DPMS_OFF;
  212. panel->base.dc = dc;
  213. drm_encoder_helper_add(&panel->base.encoder,
  214. &atmel_hlcdc_panel_encoder_helper_funcs);
  215. ret = drm_encoder_init(dev, &panel->base.encoder,
  216. &atmel_hlcdc_panel_encoder_funcs,
  217. DRM_MODE_ENCODER_LVDS);
  218. if (ret)
  219. return ret;
  220. panel->base.connector.dpms = DRM_MODE_DPMS_OFF;
  221. panel->base.connector.polled = DRM_CONNECTOR_POLL_CONNECT;
  222. drm_connector_helper_add(&panel->base.connector,
  223. &atmel_hlcdc_panel_connector_helper_funcs);
  224. ret = drm_connector_init(dev, &panel->base.connector,
  225. &atmel_hlcdc_panel_connector_funcs,
  226. DRM_MODE_CONNECTOR_LVDS);
  227. if (ret)
  228. goto err_encoder_cleanup;
  229. drm_mode_connector_attach_encoder(&panel->base.connector,
  230. &panel->base.encoder);
  231. panel->base.encoder.possible_crtcs = 0x1;
  232. drm_panel_attach(p, &panel->base.connector);
  233. panel->panel = p;
  234. return 0;
  235. err_encoder_cleanup:
  236. drm_encoder_cleanup(&panel->base.encoder);
  237. return ret;
  238. }
  239. int atmel_hlcdc_create_outputs(struct drm_device *dev)
  240. {
  241. struct device_node *port_np, *np;
  242. struct of_endpoint ep;
  243. int ret;
  244. port_np = of_get_child_by_name(dev->dev->of_node, "port");
  245. if (!port_np)
  246. return -EINVAL;
  247. np = of_get_child_by_name(port_np, "endpoint");
  248. of_node_put(port_np);
  249. if (!np)
  250. return -EINVAL;
  251. ret = of_graph_parse_endpoint(np, &ep);
  252. of_node_put(port_np);
  253. if (ret)
  254. return ret;
  255. /* We currently only support panel output */
  256. return atmel_hlcdc_create_panel_output(dev, &ep);
  257. }