rgb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Avionic Design GmbH
  4. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  5. */
  6. #include <linux/clk.h>
  7. #include <drm/drm_atomic_helper.h>
  8. #include <drm/drm_bridge_connector.h>
  9. #include <drm/drm_simple_kms_helper.h>
  10. #include "drm.h"
  11. #include "dc.h"
  12. struct tegra_rgb {
  13. struct tegra_output output;
  14. struct tegra_dc *dc;
  15. struct clk *pll_d_out0;
  16. struct clk *pll_d2_out0;
  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 void tegra_rgb_encoder_disable(struct drm_encoder *encoder)
  79. {
  80. struct tegra_output *output = encoder_to_output(encoder);
  81. struct tegra_rgb *rgb = to_rgb(output);
  82. tegra_dc_write_regs(rgb->dc, rgb_disable, ARRAY_SIZE(rgb_disable));
  83. tegra_dc_commit(rgb->dc);
  84. }
  85. static void tegra_rgb_encoder_enable(struct drm_encoder *encoder)
  86. {
  87. struct tegra_output *output = encoder_to_output(encoder);
  88. struct tegra_rgb *rgb = to_rgb(output);
  89. u32 value;
  90. tegra_dc_write_regs(rgb->dc, rgb_enable, ARRAY_SIZE(rgb_enable));
  91. value = DE_SELECT_ACTIVE | DE_CONTROL_NORMAL;
  92. tegra_dc_writel(rgb->dc, value, DC_DISP_DATA_ENABLE_OPTIONS);
  93. /* XXX: parameterize? */
  94. value = tegra_dc_readl(rgb->dc, DC_COM_PIN_OUTPUT_POLARITY(1));
  95. value &= ~LVS_OUTPUT_POLARITY_LOW;
  96. value &= ~LHS_OUTPUT_POLARITY_LOW;
  97. tegra_dc_writel(rgb->dc, value, DC_COM_PIN_OUTPUT_POLARITY(1));
  98. /* XXX: parameterize? */
  99. value = DISP_DATA_FORMAT_DF1P1C | DISP_ALIGNMENT_MSB |
  100. DISP_ORDER_RED_BLUE;
  101. tegra_dc_writel(rgb->dc, value, DC_DISP_DISP_INTERFACE_CONTROL);
  102. tegra_dc_commit(rgb->dc);
  103. }
  104. static bool tegra_rgb_pll_rate_change_allowed(struct tegra_rgb *rgb)
  105. {
  106. if (!rgb->pll_d2_out0)
  107. return false;
  108. if (!clk_is_match(rgb->clk_parent, rgb->pll_d_out0) &&
  109. !clk_is_match(rgb->clk_parent, rgb->pll_d2_out0))
  110. return false;
  111. return true;
  112. }
  113. static int
  114. tegra_rgb_encoder_atomic_check(struct drm_encoder *encoder,
  115. struct drm_crtc_state *crtc_state,
  116. struct drm_connector_state *conn_state)
  117. {
  118. struct tegra_output *output = encoder_to_output(encoder);
  119. struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
  120. unsigned long pclk = crtc_state->mode.clock * 1000;
  121. struct tegra_rgb *rgb = to_rgb(output);
  122. unsigned int div;
  123. int err;
  124. /*
  125. * We may not want to change the frequency of the parent clock, since
  126. * it may be a parent for other peripherals. This is due to the fact
  127. * that on Tegra20 there's only a single clock dedicated to display
  128. * (pll_d_out0), whereas later generations have a second one that can
  129. * be used to independently drive a second output (pll_d2_out0).
  130. *
  131. * As a way to support multiple outputs on Tegra20 as well, pll_p is
  132. * typically used as the parent clock for the display controllers.
  133. * But this comes at a cost: pll_p is the parent of several other
  134. * peripherals, so its frequency shouldn't change out of the blue.
  135. *
  136. * The best we can do at this point is to use the shift clock divider
  137. * and hope that the desired frequency can be matched (or at least
  138. * matched sufficiently close that the panel will still work).
  139. */
  140. if (tegra_rgb_pll_rate_change_allowed(rgb)) {
  141. /*
  142. * Set display controller clock to x2 of PCLK in order to
  143. * produce higher resolution pulse positions.
  144. */
  145. div = 2;
  146. pclk *= 2;
  147. } else {
  148. div = ((clk_get_rate(rgb->clk) * 2) / pclk) - 2;
  149. pclk = 0;
  150. }
  151. err = tegra_dc_state_setup_clock(dc, crtc_state, rgb->clk_parent,
  152. pclk, div);
  153. if (err < 0) {
  154. dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
  155. return err;
  156. }
  157. return err;
  158. }
  159. static const struct drm_encoder_helper_funcs tegra_rgb_encoder_helper_funcs = {
  160. .disable = tegra_rgb_encoder_disable,
  161. .enable = tegra_rgb_encoder_enable,
  162. .atomic_check = tegra_rgb_encoder_atomic_check,
  163. };
  164. int tegra_dc_rgb_probe(struct tegra_dc *dc)
  165. {
  166. struct device_node *np;
  167. struct tegra_rgb *rgb;
  168. int err;
  169. np = of_get_child_by_name(dc->dev->of_node, "rgb");
  170. if (!np || !of_device_is_available(np))
  171. return -ENODEV;
  172. rgb = devm_kzalloc(dc->dev, sizeof(*rgb), GFP_KERNEL);
  173. if (!rgb)
  174. return -ENOMEM;
  175. rgb->output.dev = dc->dev;
  176. rgb->output.of_node = np;
  177. rgb->dc = dc;
  178. err = tegra_output_probe(&rgb->output);
  179. if (err < 0)
  180. return err;
  181. rgb->clk = devm_clk_get(dc->dev, NULL);
  182. if (IS_ERR(rgb->clk)) {
  183. dev_err(dc->dev, "failed to get clock\n");
  184. return PTR_ERR(rgb->clk);
  185. }
  186. rgb->clk_parent = devm_clk_get(dc->dev, "parent");
  187. if (IS_ERR(rgb->clk_parent)) {
  188. dev_err(dc->dev, "failed to get parent clock\n");
  189. return PTR_ERR(rgb->clk_parent);
  190. }
  191. err = clk_set_parent(rgb->clk, rgb->clk_parent);
  192. if (err < 0) {
  193. dev_err(dc->dev, "failed to set parent clock: %d\n", err);
  194. return err;
  195. }
  196. rgb->pll_d_out0 = clk_get_sys(NULL, "pll_d_out0");
  197. if (IS_ERR(rgb->pll_d_out0)) {
  198. err = PTR_ERR(rgb->pll_d_out0);
  199. dev_err(dc->dev, "failed to get pll_d_out0: %d\n", err);
  200. return err;
  201. }
  202. if (dc->soc->has_pll_d2_out0) {
  203. rgb->pll_d2_out0 = clk_get_sys(NULL, "pll_d2_out0");
  204. if (IS_ERR(rgb->pll_d2_out0)) {
  205. err = PTR_ERR(rgb->pll_d2_out0);
  206. dev_err(dc->dev, "failed to get pll_d2_out0: %d\n", err);
  207. return err;
  208. }
  209. }
  210. dc->rgb = &rgb->output;
  211. return 0;
  212. }
  213. int tegra_dc_rgb_remove(struct tegra_dc *dc)
  214. {
  215. struct tegra_rgb *rgb;
  216. if (!dc->rgb)
  217. return 0;
  218. rgb = to_rgb(dc->rgb);
  219. clk_put(rgb->pll_d2_out0);
  220. clk_put(rgb->pll_d_out0);
  221. tegra_output_remove(dc->rgb);
  222. dc->rgb = NULL;
  223. return 0;
  224. }
  225. int tegra_dc_rgb_init(struct drm_device *drm, struct tegra_dc *dc)
  226. {
  227. struct tegra_output *output = dc->rgb;
  228. struct drm_connector *connector;
  229. int err;
  230. if (!dc->rgb)
  231. return -ENODEV;
  232. drm_simple_encoder_init(drm, &output->encoder, DRM_MODE_ENCODER_LVDS);
  233. drm_encoder_helper_add(&output->encoder,
  234. &tegra_rgb_encoder_helper_funcs);
  235. /*
  236. * Wrap directly-connected panel into DRM bridge in order to let
  237. * DRM core to handle panel for us.
  238. */
  239. if (output->panel) {
  240. output->bridge = devm_drm_panel_bridge_add(output->dev,
  241. output->panel);
  242. if (IS_ERR(output->bridge)) {
  243. dev_err(output->dev,
  244. "failed to wrap panel into bridge: %pe\n",
  245. output->bridge);
  246. return PTR_ERR(output->bridge);
  247. }
  248. output->panel = NULL;
  249. }
  250. /*
  251. * Tegra devices that have LVDS panel utilize LVDS encoder bridge
  252. * for converting up to 28 LCD LVTTL lanes into 5/4 LVDS lanes that
  253. * go to display panel's receiver.
  254. *
  255. * Encoder usually have a power-down control which needs to be enabled
  256. * in order to transmit data to the panel. Historically devices that
  257. * use an older device-tree version didn't model the bridge, assuming
  258. * that encoder is turned ON by default, while today's DRM allows us
  259. * to model LVDS encoder properly.
  260. *
  261. * Newer device-trees utilize LVDS encoder bridge, which provides
  262. * us with a connector and handles the display panel.
  263. *
  264. * For older device-trees we wrapped panel into the panel-bridge.
  265. */
  266. if (output->bridge) {
  267. err = drm_bridge_attach(&output->encoder, output->bridge,
  268. NULL, DRM_BRIDGE_ATTACH_NO_CONNECTOR);
  269. if (err)
  270. return err;
  271. connector = drm_bridge_connector_init(drm, &output->encoder);
  272. if (IS_ERR(connector)) {
  273. dev_err(output->dev,
  274. "failed to initialize bridge connector: %pe\n",
  275. connector);
  276. return PTR_ERR(connector);
  277. }
  278. drm_connector_attach_encoder(connector, &output->encoder);
  279. }
  280. err = tegra_output_init(drm, output);
  281. if (err < 0) {
  282. dev_err(output->dev, "failed to initialize output: %d\n", err);
  283. return err;
  284. }
  285. /*
  286. * Other outputs can be attached to either display controller. The RGB
  287. * outputs are an exception and work only with their parent display
  288. * controller.
  289. */
  290. output->encoder.possible_crtcs = drm_crtc_mask(&dc->base);
  291. return 0;
  292. }
  293. int tegra_dc_rgb_exit(struct tegra_dc *dc)
  294. {
  295. if (dc->rgb)
  296. tegra_output_exit(dc->rgb);
  297. return 0;
  298. }