rgb.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. .reset = drm_atomic_helper_connector_reset,
  80. .detect = tegra_output_connector_detect,
  81. .fill_modes = drm_helper_probe_single_connector_modes,
  82. .destroy = tegra_output_connector_destroy,
  83. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  84. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  85. };
  86. static enum drm_mode_status
  87. tegra_rgb_connector_mode_valid(struct drm_connector *connector,
  88. struct drm_display_mode *mode)
  89. {
  90. /*
  91. * FIXME: For now, always assume that the mode is okay. There are
  92. * unresolved issues with clk_round_rate(), which doesn't always
  93. * reliably report whether a frequency can be set or not.
  94. */
  95. return MODE_OK;
  96. }
  97. static const struct drm_connector_helper_funcs tegra_rgb_connector_helper_funcs = {
  98. .get_modes = tegra_output_connector_get_modes,
  99. .mode_valid = tegra_rgb_connector_mode_valid,
  100. };
  101. static const struct drm_encoder_funcs tegra_rgb_encoder_funcs = {
  102. .destroy = tegra_output_encoder_destroy,
  103. };
  104. static void tegra_rgb_encoder_disable(struct drm_encoder *encoder)
  105. {
  106. struct tegra_output *output = encoder_to_output(encoder);
  107. struct tegra_rgb *rgb = to_rgb(output);
  108. if (output->panel)
  109. drm_panel_disable(output->panel);
  110. tegra_dc_write_regs(rgb->dc, rgb_disable, ARRAY_SIZE(rgb_disable));
  111. tegra_dc_commit(rgb->dc);
  112. if (output->panel)
  113. drm_panel_unprepare(output->panel);
  114. }
  115. static void tegra_rgb_encoder_enable(struct drm_encoder *encoder)
  116. {
  117. struct tegra_output *output = encoder_to_output(encoder);
  118. struct tegra_rgb *rgb = to_rgb(output);
  119. u32 value;
  120. if (output->panel)
  121. drm_panel_prepare(output->panel);
  122. tegra_dc_write_regs(rgb->dc, rgb_enable, ARRAY_SIZE(rgb_enable));
  123. value = DE_SELECT_ACTIVE | DE_CONTROL_NORMAL;
  124. tegra_dc_writel(rgb->dc, value, DC_DISP_DATA_ENABLE_OPTIONS);
  125. /* XXX: parameterize? */
  126. value = tegra_dc_readl(rgb->dc, DC_COM_PIN_OUTPUT_POLARITY(1));
  127. value &= ~LVS_OUTPUT_POLARITY_LOW;
  128. value &= ~LHS_OUTPUT_POLARITY_LOW;
  129. tegra_dc_writel(rgb->dc, value, DC_COM_PIN_OUTPUT_POLARITY(1));
  130. /* XXX: parameterize? */
  131. value = DISP_DATA_FORMAT_DF1P1C | DISP_ALIGNMENT_MSB |
  132. DISP_ORDER_RED_BLUE;
  133. tegra_dc_writel(rgb->dc, value, DC_DISP_DISP_INTERFACE_CONTROL);
  134. /* XXX: parameterize? */
  135. value = SC0_H_QUALIFIER_NONE | SC1_H_QUALIFIER_NONE;
  136. tegra_dc_writel(rgb->dc, value, DC_DISP_SHIFT_CLOCK_OPTIONS);
  137. tegra_dc_commit(rgb->dc);
  138. if (output->panel)
  139. drm_panel_enable(output->panel);
  140. }
  141. static int
  142. tegra_rgb_encoder_atomic_check(struct drm_encoder *encoder,
  143. struct drm_crtc_state *crtc_state,
  144. struct drm_connector_state *conn_state)
  145. {
  146. struct tegra_output *output = encoder_to_output(encoder);
  147. struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
  148. unsigned long pclk = crtc_state->mode.clock * 1000;
  149. struct tegra_rgb *rgb = to_rgb(output);
  150. unsigned int div;
  151. int err;
  152. /*
  153. * We may not want to change the frequency of the parent clock, since
  154. * it may be a parent for other peripherals. This is due to the fact
  155. * that on Tegra20 there's only a single clock dedicated to display
  156. * (pll_d_out0), whereas later generations have a second one that can
  157. * be used to independently drive a second output (pll_d2_out0).
  158. *
  159. * As a way to support multiple outputs on Tegra20 as well, pll_p is
  160. * typically used as the parent clock for the display controllers.
  161. * But this comes at a cost: pll_p is the parent of several other
  162. * peripherals, so its frequency shouldn't change out of the blue.
  163. *
  164. * The best we can do at this point is to use the shift clock divider
  165. * and hope that the desired frequency can be matched (or at least
  166. * matched sufficiently close that the panel will still work).
  167. */
  168. div = ((clk_get_rate(rgb->clk) * 2) / pclk) - 2;
  169. pclk = 0;
  170. err = tegra_dc_state_setup_clock(dc, crtc_state, rgb->clk_parent,
  171. pclk, div);
  172. if (err < 0) {
  173. dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
  174. return err;
  175. }
  176. return err;
  177. }
  178. static const struct drm_encoder_helper_funcs tegra_rgb_encoder_helper_funcs = {
  179. .disable = tegra_rgb_encoder_disable,
  180. .enable = tegra_rgb_encoder_enable,
  181. .atomic_check = tegra_rgb_encoder_atomic_check,
  182. };
  183. int tegra_dc_rgb_probe(struct tegra_dc *dc)
  184. {
  185. struct device_node *np;
  186. struct tegra_rgb *rgb;
  187. int err;
  188. np = of_get_child_by_name(dc->dev->of_node, "rgb");
  189. if (!np || !of_device_is_available(np))
  190. return -ENODEV;
  191. rgb = devm_kzalloc(dc->dev, sizeof(*rgb), GFP_KERNEL);
  192. if (!rgb)
  193. return -ENOMEM;
  194. rgb->output.dev = dc->dev;
  195. rgb->output.of_node = np;
  196. rgb->dc = dc;
  197. err = tegra_output_probe(&rgb->output);
  198. if (err < 0)
  199. return err;
  200. rgb->clk = devm_clk_get(dc->dev, NULL);
  201. if (IS_ERR(rgb->clk)) {
  202. dev_err(dc->dev, "failed to get clock\n");
  203. return PTR_ERR(rgb->clk);
  204. }
  205. rgb->clk_parent = devm_clk_get(dc->dev, "parent");
  206. if (IS_ERR(rgb->clk_parent)) {
  207. dev_err(dc->dev, "failed to get parent clock\n");
  208. return PTR_ERR(rgb->clk_parent);
  209. }
  210. err = clk_set_parent(rgb->clk, rgb->clk_parent);
  211. if (err < 0) {
  212. dev_err(dc->dev, "failed to set parent clock: %d\n", err);
  213. return err;
  214. }
  215. dc->rgb = &rgb->output;
  216. return 0;
  217. }
  218. int tegra_dc_rgb_remove(struct tegra_dc *dc)
  219. {
  220. if (!dc->rgb)
  221. return 0;
  222. tegra_output_remove(dc->rgb);
  223. dc->rgb = NULL;
  224. return 0;
  225. }
  226. int tegra_dc_rgb_init(struct drm_device *drm, struct tegra_dc *dc)
  227. {
  228. struct tegra_output *output = dc->rgb;
  229. int err;
  230. if (!dc->rgb)
  231. return -ENODEV;
  232. drm_connector_init(drm, &output->connector, &tegra_rgb_connector_funcs,
  233. DRM_MODE_CONNECTOR_LVDS);
  234. drm_connector_helper_add(&output->connector,
  235. &tegra_rgb_connector_helper_funcs);
  236. output->connector.dpms = DRM_MODE_DPMS_OFF;
  237. drm_encoder_init(drm, &output->encoder, &tegra_rgb_encoder_funcs,
  238. DRM_MODE_ENCODER_LVDS, NULL);
  239. drm_encoder_helper_add(&output->encoder,
  240. &tegra_rgb_encoder_helper_funcs);
  241. drm_mode_connector_attach_encoder(&output->connector,
  242. &output->encoder);
  243. drm_connector_register(&output->connector);
  244. err = tegra_output_init(drm, output);
  245. if (err < 0) {
  246. dev_err(output->dev, "failed to initialize output: %d\n", err);
  247. return err;
  248. }
  249. /*
  250. * Other outputs can be attached to either display controller. The RGB
  251. * outputs are an exception and work only with their parent display
  252. * controller.
  253. */
  254. output->encoder.possible_crtcs = drm_crtc_mask(&dc->base);
  255. return 0;
  256. }
  257. int tegra_dc_rgb_exit(struct tegra_dc *dc)
  258. {
  259. if (dc->rgb)
  260. tegra_output_exit(dc->rgb);
  261. return 0;
  262. }