rgb.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.h"
  11. #include "dc.h"
  12. struct tegra_rgb {
  13. struct tegra_output output;
  14. struct tegra_dc *dc;
  15. bool enabled;
  16. struct clk *clk_parent;
  17. struct clk *clk;
  18. };
  19. static inline struct tegra_rgb *to_rgb(struct tegra_output *output)
  20. {
  21. return container_of(output, struct tegra_rgb, output);
  22. }
  23. struct reg_entry {
  24. unsigned long offset;
  25. unsigned long value;
  26. };
  27. static const struct reg_entry rgb_enable[] = {
  28. { DC_COM_PIN_OUTPUT_ENABLE(0), 0x00000000 },
  29. { DC_COM_PIN_OUTPUT_ENABLE(1), 0x00000000 },
  30. { DC_COM_PIN_OUTPUT_ENABLE(2), 0x00000000 },
  31. { DC_COM_PIN_OUTPUT_ENABLE(3), 0x00000000 },
  32. { DC_COM_PIN_OUTPUT_POLARITY(0), 0x00000000 },
  33. { DC_COM_PIN_OUTPUT_POLARITY(1), 0x01000000 },
  34. { DC_COM_PIN_OUTPUT_POLARITY(2), 0x00000000 },
  35. { DC_COM_PIN_OUTPUT_POLARITY(3), 0x00000000 },
  36. { DC_COM_PIN_OUTPUT_DATA(0), 0x00000000 },
  37. { DC_COM_PIN_OUTPUT_DATA(1), 0x00000000 },
  38. { DC_COM_PIN_OUTPUT_DATA(2), 0x00000000 },
  39. { DC_COM_PIN_OUTPUT_DATA(3), 0x00000000 },
  40. { DC_COM_PIN_OUTPUT_SELECT(0), 0x00000000 },
  41. { DC_COM_PIN_OUTPUT_SELECT(1), 0x00000000 },
  42. { DC_COM_PIN_OUTPUT_SELECT(2), 0x00000000 },
  43. { DC_COM_PIN_OUTPUT_SELECT(3), 0x00000000 },
  44. { DC_COM_PIN_OUTPUT_SELECT(4), 0x00210222 },
  45. { DC_COM_PIN_OUTPUT_SELECT(5), 0x00002200 },
  46. { DC_COM_PIN_OUTPUT_SELECT(6), 0x00020000 },
  47. };
  48. static const struct reg_entry rgb_disable[] = {
  49. { DC_COM_PIN_OUTPUT_SELECT(6), 0x00000000 },
  50. { DC_COM_PIN_OUTPUT_SELECT(5), 0x00000000 },
  51. { DC_COM_PIN_OUTPUT_SELECT(4), 0x00000000 },
  52. { DC_COM_PIN_OUTPUT_SELECT(3), 0x00000000 },
  53. { DC_COM_PIN_OUTPUT_SELECT(2), 0x00000000 },
  54. { DC_COM_PIN_OUTPUT_SELECT(1), 0x00000000 },
  55. { DC_COM_PIN_OUTPUT_SELECT(0), 0x00000000 },
  56. { DC_COM_PIN_OUTPUT_DATA(3), 0xaaaaaaaa },
  57. { DC_COM_PIN_OUTPUT_DATA(2), 0xaaaaaaaa },
  58. { DC_COM_PIN_OUTPUT_DATA(1), 0xaaaaaaaa },
  59. { DC_COM_PIN_OUTPUT_DATA(0), 0xaaaaaaaa },
  60. { DC_COM_PIN_OUTPUT_POLARITY(3), 0x00000000 },
  61. { DC_COM_PIN_OUTPUT_POLARITY(2), 0x00000000 },
  62. { DC_COM_PIN_OUTPUT_POLARITY(1), 0x00000000 },
  63. { DC_COM_PIN_OUTPUT_POLARITY(0), 0x00000000 },
  64. { DC_COM_PIN_OUTPUT_ENABLE(3), 0x55555555 },
  65. { DC_COM_PIN_OUTPUT_ENABLE(2), 0x55555555 },
  66. { DC_COM_PIN_OUTPUT_ENABLE(1), 0x55150005 },
  67. { DC_COM_PIN_OUTPUT_ENABLE(0), 0x55555555 },
  68. };
  69. static void tegra_dc_write_regs(struct tegra_dc *dc,
  70. const struct reg_entry *table,
  71. unsigned int num)
  72. {
  73. unsigned int i;
  74. for (i = 0; i < num; i++)
  75. tegra_dc_writel(dc, table[i].value, table[i].offset);
  76. }
  77. static int tegra_output_rgb_enable(struct tegra_output *output)
  78. {
  79. struct tegra_rgb *rgb = to_rgb(output);
  80. unsigned long value;
  81. if (rgb->enabled)
  82. return 0;
  83. tegra_dc_write_regs(rgb->dc, rgb_enable, ARRAY_SIZE(rgb_enable));
  84. value = DE_SELECT_ACTIVE | DE_CONTROL_NORMAL;
  85. tegra_dc_writel(rgb->dc, value, DC_DISP_DATA_ENABLE_OPTIONS);
  86. /* XXX: parameterize? */
  87. value = tegra_dc_readl(rgb->dc, DC_COM_PIN_OUTPUT_POLARITY(1));
  88. value &= ~LVS_OUTPUT_POLARITY_LOW;
  89. value &= ~LHS_OUTPUT_POLARITY_LOW;
  90. tegra_dc_writel(rgb->dc, value, DC_COM_PIN_OUTPUT_POLARITY(1));
  91. /* XXX: parameterize? */
  92. value = DISP_DATA_FORMAT_DF1P1C | DISP_ALIGNMENT_MSB |
  93. DISP_ORDER_RED_BLUE;
  94. tegra_dc_writel(rgb->dc, value, DC_DISP_DISP_INTERFACE_CONTROL);
  95. /* XXX: parameterize? */
  96. value = SC0_H_QUALIFIER_NONE | SC1_H_QUALIFIER_NONE;
  97. tegra_dc_writel(rgb->dc, value, DC_DISP_SHIFT_CLOCK_OPTIONS);
  98. value = tegra_dc_readl(rgb->dc, DC_CMD_DISPLAY_COMMAND);
  99. value &= ~DISP_CTRL_MODE_MASK;
  100. value |= DISP_CTRL_MODE_C_DISPLAY;
  101. tegra_dc_writel(rgb->dc, value, DC_CMD_DISPLAY_COMMAND);
  102. value = tegra_dc_readl(rgb->dc, DC_CMD_DISPLAY_POWER_CONTROL);
  103. value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  104. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
  105. tegra_dc_writel(rgb->dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  106. tegra_dc_writel(rgb->dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  107. tegra_dc_writel(rgb->dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  108. rgb->enabled = true;
  109. return 0;
  110. }
  111. static int tegra_output_rgb_disable(struct tegra_output *output)
  112. {
  113. struct tegra_rgb *rgb = to_rgb(output);
  114. unsigned long value;
  115. if (!rgb->enabled)
  116. return 0;
  117. value = tegra_dc_readl(rgb->dc, DC_CMD_DISPLAY_POWER_CONTROL);
  118. value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  119. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
  120. tegra_dc_writel(rgb->dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  121. value = tegra_dc_readl(rgb->dc, DC_CMD_DISPLAY_COMMAND);
  122. value &= ~DISP_CTRL_MODE_MASK;
  123. tegra_dc_writel(rgb->dc, value, DC_CMD_DISPLAY_COMMAND);
  124. tegra_dc_writel(rgb->dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  125. tegra_dc_writel(rgb->dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  126. tegra_dc_write_regs(rgb->dc, rgb_disable, ARRAY_SIZE(rgb_disable));
  127. rgb->enabled = false;
  128. return 0;
  129. }
  130. static int tegra_output_rgb_setup_clock(struct tegra_output *output,
  131. struct clk *clk, unsigned long pclk,
  132. unsigned int *div)
  133. {
  134. struct tegra_rgb *rgb = to_rgb(output);
  135. int err;
  136. err = clk_set_parent(clk, rgb->clk_parent);
  137. if (err < 0) {
  138. dev_err(output->dev, "failed to set parent: %d\n", err);
  139. return err;
  140. }
  141. /*
  142. * We may not want to change the frequency of the parent clock, since
  143. * it may be a parent for other peripherals. This is due to the fact
  144. * that on Tegra20 there's only a single clock dedicated to display
  145. * (pll_d_out0), whereas later generations have a second one that can
  146. * be used to independently drive a second output (pll_d2_out0).
  147. *
  148. * As a way to support multiple outputs on Tegra20 as well, pll_p is
  149. * typically used as the parent clock for the display controllers.
  150. * But this comes at a cost: pll_p is the parent of several other
  151. * peripherals, so its frequency shouldn't change out of the blue.
  152. *
  153. * The best we can do at this point is to use the shift clock divider
  154. * and hope that the desired frequency can be matched (or at least
  155. * matched sufficiently close that the panel will still work).
  156. */
  157. *div = ((clk_get_rate(clk) * 2) / pclk) - 2;
  158. return 0;
  159. }
  160. static int tegra_output_rgb_check_mode(struct tegra_output *output,
  161. struct drm_display_mode *mode,
  162. enum drm_mode_status *status)
  163. {
  164. /*
  165. * FIXME: For now, always assume that the mode is okay. There are
  166. * unresolved issues with clk_round_rate(), which doesn't always
  167. * reliably report whether a frequency can be set or not.
  168. */
  169. *status = MODE_OK;
  170. return 0;
  171. }
  172. static const struct tegra_output_ops rgb_ops = {
  173. .enable = tegra_output_rgb_enable,
  174. .disable = tegra_output_rgb_disable,
  175. .setup_clock = tegra_output_rgb_setup_clock,
  176. .check_mode = tegra_output_rgb_check_mode,
  177. };
  178. int tegra_dc_rgb_probe(struct tegra_dc *dc)
  179. {
  180. struct device_node *np;
  181. struct tegra_rgb *rgb;
  182. int err;
  183. np = of_get_child_by_name(dc->dev->of_node, "rgb");
  184. if (!np || !of_device_is_available(np))
  185. return -ENODEV;
  186. rgb = devm_kzalloc(dc->dev, sizeof(*rgb), GFP_KERNEL);
  187. if (!rgb)
  188. return -ENOMEM;
  189. rgb->output.dev = dc->dev;
  190. rgb->output.of_node = np;
  191. rgb->dc = dc;
  192. err = tegra_output_probe(&rgb->output);
  193. if (err < 0)
  194. return err;
  195. rgb->clk = devm_clk_get(dc->dev, NULL);
  196. if (IS_ERR(rgb->clk)) {
  197. dev_err(dc->dev, "failed to get clock\n");
  198. return PTR_ERR(rgb->clk);
  199. }
  200. rgb->clk_parent = devm_clk_get(dc->dev, "parent");
  201. if (IS_ERR(rgb->clk_parent)) {
  202. dev_err(dc->dev, "failed to get parent clock\n");
  203. return PTR_ERR(rgb->clk_parent);
  204. }
  205. err = clk_set_parent(rgb->clk, rgb->clk_parent);
  206. if (err < 0) {
  207. dev_err(dc->dev, "failed to set parent clock: %d\n", err);
  208. return err;
  209. }
  210. dc->rgb = &rgb->output;
  211. return 0;
  212. }
  213. int tegra_dc_rgb_remove(struct tegra_dc *dc)
  214. {
  215. int err;
  216. if (!dc->rgb)
  217. return 0;
  218. err = tegra_output_remove(dc->rgb);
  219. if (err < 0)
  220. return err;
  221. return 0;
  222. }
  223. int tegra_dc_rgb_init(struct drm_device *drm, struct tegra_dc *dc)
  224. {
  225. struct tegra_rgb *rgb = to_rgb(dc->rgb);
  226. int err;
  227. if (!dc->rgb)
  228. return -ENODEV;
  229. rgb->output.type = TEGRA_OUTPUT_RGB;
  230. rgb->output.ops = &rgb_ops;
  231. err = tegra_output_init(dc->base.dev, &rgb->output);
  232. if (err < 0) {
  233. dev_err(dc->dev, "output setup failed: %d\n", err);
  234. return err;
  235. }
  236. /*
  237. * By default, outputs can be associated with each display controller.
  238. * RGB outputs are an exception, so we make sure they can be attached
  239. * to only their parent display controller.
  240. */
  241. rgb->output.encoder.possible_crtcs = drm_crtc_mask(&dc->base);
  242. return 0;
  243. }
  244. int tegra_dc_rgb_exit(struct tegra_dc *dc)
  245. {
  246. if (dc->rgb) {
  247. int err;
  248. err = tegra_output_disable(dc->rgb);
  249. if (err < 0) {
  250. dev_err(dc->dev, "output failed to disable: %d\n", err);
  251. return err;
  252. }
  253. err = tegra_output_exit(dc->rgb);
  254. if (err < 0) {
  255. dev_err(dc->dev, "output cleanup failed: %d\n", err);
  256. return err;
  257. }
  258. dc->rgb = NULL;
  259. }
  260. return 0;
  261. }