rgb.c 10 KB

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