rgb.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (C) 2012 Avionic Design GmbH
  3. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <drm/drm_atomic_helper.h>
  11. #include <drm/drm_panel.h>
  12. #include "drm.h"
  13. #include "dc.h"
  14. struct tegra_rgb {
  15. struct tegra_output output;
  16. struct tegra_dc *dc;
  17. struct clk *clk_parent;
  18. struct clk *clk;
  19. };
  20. static inline struct tegra_rgb *to_rgb(struct tegra_output *output)
  21. {
  22. return container_of(output, struct tegra_rgb, output);
  23. }
  24. struct reg_entry {
  25. unsigned long offset;
  26. unsigned long value;
  27. };
  28. static const struct reg_entry rgb_enable[] = {
  29. { DC_COM_PIN_OUTPUT_ENABLE(0), 0x00000000 },
  30. { DC_COM_PIN_OUTPUT_ENABLE(1), 0x00000000 },
  31. { DC_COM_PIN_OUTPUT_ENABLE(2), 0x00000000 },
  32. { DC_COM_PIN_OUTPUT_ENABLE(3), 0x00000000 },
  33. { DC_COM_PIN_OUTPUT_POLARITY(0), 0x00000000 },
  34. { DC_COM_PIN_OUTPUT_POLARITY(1), 0x01000000 },
  35. { DC_COM_PIN_OUTPUT_POLARITY(2), 0x00000000 },
  36. { DC_COM_PIN_OUTPUT_POLARITY(3), 0x00000000 },
  37. { DC_COM_PIN_OUTPUT_DATA(0), 0x00000000 },
  38. { DC_COM_PIN_OUTPUT_DATA(1), 0x00000000 },
  39. { DC_COM_PIN_OUTPUT_DATA(2), 0x00000000 },
  40. { DC_COM_PIN_OUTPUT_DATA(3), 0x00000000 },
  41. { DC_COM_PIN_OUTPUT_SELECT(0), 0x00000000 },
  42. { DC_COM_PIN_OUTPUT_SELECT(1), 0x00000000 },
  43. { DC_COM_PIN_OUTPUT_SELECT(2), 0x00000000 },
  44. { DC_COM_PIN_OUTPUT_SELECT(3), 0x00000000 },
  45. { DC_COM_PIN_OUTPUT_SELECT(4), 0x00210222 },
  46. { DC_COM_PIN_OUTPUT_SELECT(5), 0x00002200 },
  47. { DC_COM_PIN_OUTPUT_SELECT(6), 0x00020000 },
  48. };
  49. static const struct reg_entry rgb_disable[] = {
  50. { DC_COM_PIN_OUTPUT_SELECT(6), 0x00000000 },
  51. { DC_COM_PIN_OUTPUT_SELECT(5), 0x00000000 },
  52. { DC_COM_PIN_OUTPUT_SELECT(4), 0x00000000 },
  53. { DC_COM_PIN_OUTPUT_SELECT(3), 0x00000000 },
  54. { DC_COM_PIN_OUTPUT_SELECT(2), 0x00000000 },
  55. { DC_COM_PIN_OUTPUT_SELECT(1), 0x00000000 },
  56. { DC_COM_PIN_OUTPUT_SELECT(0), 0x00000000 },
  57. { DC_COM_PIN_OUTPUT_DATA(3), 0xaaaaaaaa },
  58. { DC_COM_PIN_OUTPUT_DATA(2), 0xaaaaaaaa },
  59. { DC_COM_PIN_OUTPUT_DATA(1), 0xaaaaaaaa },
  60. { DC_COM_PIN_OUTPUT_DATA(0), 0xaaaaaaaa },
  61. { DC_COM_PIN_OUTPUT_POLARITY(3), 0x00000000 },
  62. { DC_COM_PIN_OUTPUT_POLARITY(2), 0x00000000 },
  63. { DC_COM_PIN_OUTPUT_POLARITY(1), 0x00000000 },
  64. { DC_COM_PIN_OUTPUT_POLARITY(0), 0x00000000 },
  65. { DC_COM_PIN_OUTPUT_ENABLE(3), 0x55555555 },
  66. { DC_COM_PIN_OUTPUT_ENABLE(2), 0x55555555 },
  67. { DC_COM_PIN_OUTPUT_ENABLE(1), 0x55150005 },
  68. { DC_COM_PIN_OUTPUT_ENABLE(0), 0x55555555 },
  69. };
  70. static void tegra_dc_write_regs(struct tegra_dc *dc,
  71. const struct reg_entry *table,
  72. unsigned int num)
  73. {
  74. unsigned int i;
  75. for (i = 0; i < num; i++)
  76. tegra_dc_writel(dc, table[i].value, table[i].offset);
  77. }
  78. static const struct drm_connector_funcs tegra_rgb_connector_funcs = {
  79. .dpms = drm_atomic_helper_connector_dpms,
  80. .reset = drm_atomic_helper_connector_reset,
  81. .detect = tegra_output_connector_detect,
  82. .fill_modes = drm_helper_probe_single_connector_modes,
  83. .destroy = tegra_output_connector_destroy,
  84. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  85. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  86. };
  87. static enum drm_mode_status
  88. tegra_rgb_connector_mode_valid(struct drm_connector *connector,
  89. struct drm_display_mode *mode)
  90. {
  91. /*
  92. * FIXME: For now, always assume that the mode is okay. There are
  93. * unresolved issues with clk_round_rate(), which doesn't always
  94. * reliably report whether a frequency can be set or not.
  95. */
  96. return MODE_OK;
  97. }
  98. static const struct drm_connector_helper_funcs tegra_rgb_connector_helper_funcs = {
  99. .get_modes = tegra_output_connector_get_modes,
  100. .mode_valid = tegra_rgb_connector_mode_valid,
  101. .best_encoder = tegra_output_connector_best_encoder,
  102. };
  103. static const struct drm_encoder_funcs tegra_rgb_encoder_funcs = {
  104. .destroy = tegra_output_encoder_destroy,
  105. };
  106. static void tegra_rgb_encoder_disable(struct drm_encoder *encoder)
  107. {
  108. struct tegra_output *output = encoder_to_output(encoder);
  109. struct tegra_rgb *rgb = to_rgb(output);
  110. if (output->panel)
  111. drm_panel_disable(output->panel);
  112. tegra_dc_write_regs(rgb->dc, rgb_disable, ARRAY_SIZE(rgb_disable));
  113. tegra_dc_commit(rgb->dc);
  114. if (output->panel)
  115. drm_panel_unprepare(output->panel);
  116. }
  117. static void tegra_rgb_encoder_enable(struct drm_encoder *encoder)
  118. {
  119. struct tegra_output *output = encoder_to_output(encoder);
  120. struct tegra_rgb *rgb = to_rgb(output);
  121. u32 value;
  122. if (output->panel)
  123. drm_panel_prepare(output->panel);
  124. tegra_dc_write_regs(rgb->dc, rgb_enable, ARRAY_SIZE(rgb_enable));
  125. value = DE_SELECT_ACTIVE | DE_CONTROL_NORMAL;
  126. tegra_dc_writel(rgb->dc, value, DC_DISP_DATA_ENABLE_OPTIONS);
  127. /* XXX: parameterize? */
  128. value = tegra_dc_readl(rgb->dc, DC_COM_PIN_OUTPUT_POLARITY(1));
  129. value &= ~LVS_OUTPUT_POLARITY_LOW;
  130. value &= ~LHS_OUTPUT_POLARITY_LOW;
  131. tegra_dc_writel(rgb->dc, value, DC_COM_PIN_OUTPUT_POLARITY(1));
  132. /* XXX: parameterize? */
  133. value = DISP_DATA_FORMAT_DF1P1C | DISP_ALIGNMENT_MSB |
  134. DISP_ORDER_RED_BLUE;
  135. tegra_dc_writel(rgb->dc, value, DC_DISP_DISP_INTERFACE_CONTROL);
  136. /* XXX: parameterize? */
  137. value = SC0_H_QUALIFIER_NONE | SC1_H_QUALIFIER_NONE;
  138. tegra_dc_writel(rgb->dc, value, DC_DISP_SHIFT_CLOCK_OPTIONS);
  139. tegra_dc_commit(rgb->dc);
  140. if (output->panel)
  141. drm_panel_enable(output->panel);
  142. }
  143. static int
  144. tegra_rgb_encoder_atomic_check(struct drm_encoder *encoder,
  145. struct drm_crtc_state *crtc_state,
  146. struct drm_connector_state *conn_state)
  147. {
  148. struct tegra_output *output = encoder_to_output(encoder);
  149. struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
  150. unsigned long pclk = crtc_state->mode.clock * 1000;
  151. struct tegra_rgb *rgb = to_rgb(output);
  152. unsigned int div;
  153. int err;
  154. /*
  155. * We may not want to change the frequency of the parent clock, since
  156. * it may be a parent for other peripherals. This is due to the fact
  157. * that on Tegra20 there's only a single clock dedicated to display
  158. * (pll_d_out0), whereas later generations have a second one that can
  159. * be used to independently drive a second output (pll_d2_out0).
  160. *
  161. * As a way to support multiple outputs on Tegra20 as well, pll_p is
  162. * typically used as the parent clock for the display controllers.
  163. * But this comes at a cost: pll_p is the parent of several other
  164. * peripherals, so its frequency shouldn't change out of the blue.
  165. *
  166. * The best we can do at this point is to use the shift clock divider
  167. * and hope that the desired frequency can be matched (or at least
  168. * matched sufficiently close that the panel will still work).
  169. */
  170. div = ((clk_get_rate(rgb->clk) * 2) / pclk) - 2;
  171. pclk = 0;
  172. err = tegra_dc_state_setup_clock(dc, crtc_state, rgb->clk_parent,
  173. pclk, div);
  174. if (err < 0) {
  175. dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
  176. return err;
  177. }
  178. return err;
  179. }
  180. static const struct drm_encoder_helper_funcs tegra_rgb_encoder_helper_funcs = {
  181. .disable = tegra_rgb_encoder_disable,
  182. .enable = tegra_rgb_encoder_enable,
  183. .atomic_check = tegra_rgb_encoder_atomic_check,
  184. };
  185. int tegra_dc_rgb_probe(struct tegra_dc *dc)
  186. {
  187. struct device_node *np;
  188. struct tegra_rgb *rgb;
  189. int err;
  190. np = of_get_child_by_name(dc->dev->of_node, "rgb");
  191. if (!np || !of_device_is_available(np))
  192. return -ENODEV;
  193. rgb = devm_kzalloc(dc->dev, sizeof(*rgb), GFP_KERNEL);
  194. if (!rgb)
  195. return -ENOMEM;
  196. rgb->output.dev = dc->dev;
  197. rgb->output.of_node = np;
  198. rgb->dc = dc;
  199. err = tegra_output_probe(&rgb->output);
  200. if (err < 0)
  201. return err;
  202. rgb->clk = devm_clk_get(dc->dev, NULL);
  203. if (IS_ERR(rgb->clk)) {
  204. dev_err(dc->dev, "failed to get clock\n");
  205. return PTR_ERR(rgb->clk);
  206. }
  207. rgb->clk_parent = devm_clk_get(dc->dev, "parent");
  208. if (IS_ERR(rgb->clk_parent)) {
  209. dev_err(dc->dev, "failed to get parent clock\n");
  210. return PTR_ERR(rgb->clk_parent);
  211. }
  212. err = clk_set_parent(rgb->clk, rgb->clk_parent);
  213. if (err < 0) {
  214. dev_err(dc->dev, "failed to set parent clock: %d\n", err);
  215. return err;
  216. }
  217. dc->rgb = &rgb->output;
  218. return 0;
  219. }
  220. int tegra_dc_rgb_remove(struct tegra_dc *dc)
  221. {
  222. if (!dc->rgb)
  223. return 0;
  224. tegra_output_remove(dc->rgb);
  225. dc->rgb = NULL;
  226. return 0;
  227. }
  228. int tegra_dc_rgb_init(struct drm_device *drm, struct tegra_dc *dc)
  229. {
  230. struct tegra_output *output = dc->rgb;
  231. int err;
  232. if (!dc->rgb)
  233. return -ENODEV;
  234. drm_connector_init(drm, &output->connector, &tegra_rgb_connector_funcs,
  235. DRM_MODE_CONNECTOR_LVDS);
  236. drm_connector_helper_add(&output->connector,
  237. &tegra_rgb_connector_helper_funcs);
  238. output->connector.dpms = DRM_MODE_DPMS_OFF;
  239. drm_encoder_init(drm, &output->encoder, &tegra_rgb_encoder_funcs,
  240. DRM_MODE_ENCODER_LVDS);
  241. drm_encoder_helper_add(&output->encoder,
  242. &tegra_rgb_encoder_helper_funcs);
  243. drm_mode_connector_attach_encoder(&output->connector,
  244. &output->encoder);
  245. drm_connector_register(&output->connector);
  246. err = tegra_output_init(drm, output);
  247. if (err < 0) {
  248. dev_err(output->dev, "failed to initialize output: %d\n", err);
  249. return err;
  250. }
  251. /*
  252. * Other outputs can be attached to either display controller. The RGB
  253. * outputs are an exception and work only with their parent display
  254. * controller.
  255. */
  256. output->encoder.possible_crtcs = drm_crtc_mask(&dc->base);
  257. return 0;
  258. }
  259. int tegra_dc_rgb_exit(struct tegra_dc *dc)
  260. {
  261. if (dc->rgb)
  262. tegra_output_exit(dc->rgb);
  263. return 0;
  264. }