sti_vid.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Author: Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics.
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #include <drm/drmP.h>
  7. #include "sti_plane.h"
  8. #include "sti_vid.h"
  9. #include "sti_vtg.h"
  10. /* Registers */
  11. #define VID_CTL 0x00
  12. #define VID_ALP 0x04
  13. #define VID_CLF 0x08
  14. #define VID_VPO 0x0C
  15. #define VID_VPS 0x10
  16. #define VID_KEY1 0x28
  17. #define VID_KEY2 0x2C
  18. #define VID_MPR0 0x30
  19. #define VID_MPR1 0x34
  20. #define VID_MPR2 0x38
  21. #define VID_MPR3 0x3C
  22. #define VID_MST 0x68
  23. #define VID_BC 0x70
  24. #define VID_TINT 0x74
  25. #define VID_CSAT 0x78
  26. /* Registers values */
  27. #define VID_CTL_IGNORE (BIT(31) | BIT(30))
  28. #define VID_CTL_PSI_ENABLE (BIT(2) | BIT(1) | BIT(0))
  29. #define VID_ALP_OPAQUE 0x00000080
  30. #define VID_BC_DFLT 0x00008000
  31. #define VID_TINT_DFLT 0x00000000
  32. #define VID_CSAT_DFLT 0x00000080
  33. /* YCbCr to RGB BT709:
  34. * R = Y+1.5391Cr
  35. * G = Y-0.4590Cr-0.1826Cb
  36. * B = Y+1.8125Cb */
  37. #define VID_MPR0_BT709 0x0A800000
  38. #define VID_MPR1_BT709 0x0AC50000
  39. #define VID_MPR2_BT709 0x07150545
  40. #define VID_MPR3_BT709 0x00000AE8
  41. void sti_vid_commit(struct sti_vid *vid,
  42. struct drm_plane_state *state)
  43. {
  44. struct drm_crtc *crtc = state->crtc;
  45. struct drm_display_mode *mode = &crtc->mode;
  46. int dst_x = state->crtc_x;
  47. int dst_y = state->crtc_y;
  48. int dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x);
  49. int dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y);
  50. u32 val, ydo, xdo, yds, xds;
  51. /* Input / output size
  52. * Align to upper even value */
  53. dst_w = ALIGN(dst_w, 2);
  54. dst_h = ALIGN(dst_h, 2);
  55. /* Unmask */
  56. val = readl(vid->regs + VID_CTL);
  57. val &= ~VID_CTL_IGNORE;
  58. writel(val, vid->regs + VID_CTL);
  59. ydo = sti_vtg_get_line_number(*mode, dst_y);
  60. yds = sti_vtg_get_line_number(*mode, dst_y + dst_h - 1);
  61. xdo = sti_vtg_get_pixel_number(*mode, dst_x);
  62. xds = sti_vtg_get_pixel_number(*mode, dst_x + dst_w - 1);
  63. writel((ydo << 16) | xdo, vid->regs + VID_VPO);
  64. writel((yds << 16) | xds, vid->regs + VID_VPS);
  65. }
  66. void sti_vid_disable(struct sti_vid *vid)
  67. {
  68. u32 val;
  69. /* Mask */
  70. val = readl(vid->regs + VID_CTL);
  71. val |= VID_CTL_IGNORE;
  72. writel(val, vid->regs + VID_CTL);
  73. }
  74. static void sti_vid_init(struct sti_vid *vid)
  75. {
  76. /* Enable PSI, Mask layer */
  77. writel(VID_CTL_PSI_ENABLE | VID_CTL_IGNORE, vid->regs + VID_CTL);
  78. /* Opaque */
  79. writel(VID_ALP_OPAQUE, vid->regs + VID_ALP);
  80. /* Color conversion parameters */
  81. writel(VID_MPR0_BT709, vid->regs + VID_MPR0);
  82. writel(VID_MPR1_BT709, vid->regs + VID_MPR1);
  83. writel(VID_MPR2_BT709, vid->regs + VID_MPR2);
  84. writel(VID_MPR3_BT709, vid->regs + VID_MPR3);
  85. /* Brightness, contrast, tint, saturation */
  86. writel(VID_BC_DFLT, vid->regs + VID_BC);
  87. writel(VID_TINT_DFLT, vid->regs + VID_TINT);
  88. writel(VID_CSAT_DFLT, vid->regs + VID_CSAT);
  89. }
  90. struct sti_vid *sti_vid_create(struct device *dev, int id,
  91. void __iomem *baseaddr)
  92. {
  93. struct sti_vid *vid;
  94. vid = devm_kzalloc(dev, sizeof(*vid), GFP_KERNEL);
  95. if (!vid) {
  96. DRM_ERROR("Failed to allocate memory for VID\n");
  97. return NULL;
  98. }
  99. vid->dev = dev;
  100. vid->regs = baseaddr;
  101. vid->id = id;
  102. sti_vid_init(vid);
  103. return vid;
  104. }