rgb.c 9.7 KB

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