rgb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include "drm.h"
  14. #include "dc.h"
  15. struct tegra_rgb {
  16. struct tegra_output output;
  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 int tegra_output_rgb_enable(struct tegra_output *output)
  79. {
  80. struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
  81. tegra_dc_write_regs(dc, rgb_enable, ARRAY_SIZE(rgb_enable));
  82. return 0;
  83. }
  84. static int tegra_output_rgb_disable(struct tegra_output *output)
  85. {
  86. struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
  87. tegra_dc_write_regs(dc, rgb_disable, ARRAY_SIZE(rgb_disable));
  88. return 0;
  89. }
  90. static int tegra_output_rgb_setup_clock(struct tegra_output *output,
  91. struct clk *clk, unsigned long pclk)
  92. {
  93. struct tegra_rgb *rgb = to_rgb(output);
  94. return clk_set_parent(clk, rgb->clk_parent);
  95. }
  96. static int tegra_output_rgb_check_mode(struct tegra_output *output,
  97. struct drm_display_mode *mode,
  98. enum drm_mode_status *status)
  99. {
  100. /*
  101. * FIXME: For now, always assume that the mode is okay. There are
  102. * unresolved issues with clk_round_rate(), which doesn't always
  103. * reliably report whether a frequency can be set or not.
  104. */
  105. *status = MODE_OK;
  106. return 0;
  107. }
  108. static const struct tegra_output_ops rgb_ops = {
  109. .enable = tegra_output_rgb_enable,
  110. .disable = tegra_output_rgb_disable,
  111. .setup_clock = tegra_output_rgb_setup_clock,
  112. .check_mode = tegra_output_rgb_check_mode,
  113. };
  114. int tegra_dc_rgb_probe(struct tegra_dc *dc)
  115. {
  116. struct device_node *np;
  117. struct tegra_rgb *rgb;
  118. int err;
  119. np = of_get_child_by_name(dc->dev->of_node, "rgb");
  120. if (!np || !of_device_is_available(np))
  121. return -ENODEV;
  122. rgb = devm_kzalloc(dc->dev, sizeof(*rgb), GFP_KERNEL);
  123. if (!rgb)
  124. return -ENOMEM;
  125. rgb->clk = devm_clk_get(dc->dev, NULL);
  126. if (IS_ERR(rgb->clk)) {
  127. dev_err(dc->dev, "failed to get clock\n");
  128. return PTR_ERR(rgb->clk);
  129. }
  130. rgb->clk_parent = devm_clk_get(dc->dev, "parent");
  131. if (IS_ERR(rgb->clk_parent)) {
  132. dev_err(dc->dev, "failed to get parent clock\n");
  133. return PTR_ERR(rgb->clk_parent);
  134. }
  135. err = clk_set_parent(rgb->clk, rgb->clk_parent);
  136. if (err < 0) {
  137. dev_err(dc->dev, "failed to set parent clock: %d\n", err);
  138. return err;
  139. }
  140. rgb->output.dev = dc->dev;
  141. rgb->output.of_node = np;
  142. err = tegra_output_parse_dt(&rgb->output);
  143. if (err < 0)
  144. return err;
  145. dc->rgb = &rgb->output;
  146. return 0;
  147. }
  148. int tegra_dc_rgb_init(struct drm_device *drm, struct tegra_dc *dc)
  149. {
  150. struct tegra_rgb *rgb = to_rgb(dc->rgb);
  151. int err;
  152. if (!dc->rgb)
  153. return -ENODEV;
  154. rgb->output.type = TEGRA_OUTPUT_RGB;
  155. rgb->output.ops = &rgb_ops;
  156. err = tegra_output_init(dc->base.dev, &rgb->output);
  157. if (err < 0) {
  158. dev_err(dc->dev, "output setup failed: %d\n", err);
  159. return err;
  160. }
  161. /*
  162. * By default, outputs can be associated with each display controller.
  163. * RGB outputs are an exception, so we make sure they can be attached
  164. * to only their parent display controller.
  165. */
  166. rgb->output.encoder.possible_crtcs = 1 << dc->pipe;
  167. return 0;
  168. }
  169. int tegra_dc_rgb_exit(struct tegra_dc *dc)
  170. {
  171. if (dc->rgb) {
  172. int err;
  173. err = tegra_output_disable(dc->rgb);
  174. if (err < 0) {
  175. dev_err(dc->dev, "output failed to disable: %d\n", err);
  176. return err;
  177. }
  178. err = tegra_output_exit(dc->rgb);
  179. if (err < 0) {
  180. dev_err(dc->dev, "output cleanup failed: %d\n", err);
  181. return err;
  182. }
  183. dc->rgb = NULL;
  184. }
  185. return 0;
  186. }