sti_vid.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <linux/seq_file.h>
  7. #include <drm/drmP.h>
  8. #include "sti_plane.h"
  9. #include "sti_vid.h"
  10. #include "sti_vtg.h"
  11. /* Registers */
  12. #define VID_CTL 0x00
  13. #define VID_ALP 0x04
  14. #define VID_CLF 0x08
  15. #define VID_VPO 0x0C
  16. #define VID_VPS 0x10
  17. #define VID_KEY1 0x28
  18. #define VID_KEY2 0x2C
  19. #define VID_MPR0 0x30
  20. #define VID_MPR1 0x34
  21. #define VID_MPR2 0x38
  22. #define VID_MPR3 0x3C
  23. #define VID_MST 0x68
  24. #define VID_BC 0x70
  25. #define VID_TINT 0x74
  26. #define VID_CSAT 0x78
  27. /* Registers values */
  28. #define VID_CTL_IGNORE (BIT(31) | BIT(30))
  29. #define VID_CTL_PSI_ENABLE (BIT(2) | BIT(1) | BIT(0))
  30. #define VID_ALP_OPAQUE 0x00000080
  31. #define VID_BC_DFLT 0x00008000
  32. #define VID_TINT_DFLT 0x00000000
  33. #define VID_CSAT_DFLT 0x00000080
  34. /* YCbCr to RGB BT709:
  35. * R = Y+1.5391Cr
  36. * G = Y-0.4590Cr-0.1826Cb
  37. * B = Y+1.8125Cb */
  38. #define VID_MPR0_BT709 0x0A800000
  39. #define VID_MPR1_BT709 0x0AC50000
  40. #define VID_MPR2_BT709 0x07150545
  41. #define VID_MPR3_BT709 0x00000AE8
  42. /* YCbCr to RGB BT709:
  43. * R = Y+1.3711Cr
  44. * G = Y-0.6992Cr-0.3359Cb
  45. * B = Y+1.7344Cb
  46. */
  47. #define VID_MPR0_BT601 0x0A800000
  48. #define VID_MPR1_BT601 0x0AAF0000
  49. #define VID_MPR2_BT601 0x094E0754
  50. #define VID_MPR3_BT601 0x00000ADD
  51. #define VID_MIN_HD_HEIGHT 720
  52. #define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
  53. readl(vid->regs + reg))
  54. static void vid_dbg_ctl(struct seq_file *s, int val)
  55. {
  56. val = val >> 30;
  57. seq_puts(s, "\t");
  58. if (!(val & 1))
  59. seq_puts(s, "NOT ");
  60. seq_puts(s, "ignored on main mixer - ");
  61. if (!(val & 2))
  62. seq_puts(s, "NOT ");
  63. seq_puts(s, "ignored on aux mixer");
  64. }
  65. static void vid_dbg_vpo(struct seq_file *s, int val)
  66. {
  67. seq_printf(s, "\txdo:%4d\tydo:%4d", val & 0x0FFF, (val >> 16) & 0x0FFF);
  68. }
  69. static void vid_dbg_vps(struct seq_file *s, int val)
  70. {
  71. seq_printf(s, "\txds:%4d\tyds:%4d", val & 0x0FFF, (val >> 16) & 0x0FFF);
  72. }
  73. static void vid_dbg_mst(struct seq_file *s, int val)
  74. {
  75. if (val & 1)
  76. seq_puts(s, "\tBUFFER UNDERFLOW!");
  77. }
  78. static int vid_dbg_show(struct seq_file *s, void *arg)
  79. {
  80. struct drm_info_node *node = s->private;
  81. struct sti_vid *vid = (struct sti_vid *)node->info_ent->data;
  82. seq_printf(s, "VID: (vaddr= 0x%p)", vid->regs);
  83. DBGFS_DUMP(VID_CTL);
  84. vid_dbg_ctl(s, readl(vid->regs + VID_CTL));
  85. DBGFS_DUMP(VID_ALP);
  86. DBGFS_DUMP(VID_CLF);
  87. DBGFS_DUMP(VID_VPO);
  88. vid_dbg_vpo(s, readl(vid->regs + VID_VPO));
  89. DBGFS_DUMP(VID_VPS);
  90. vid_dbg_vps(s, readl(vid->regs + VID_VPS));
  91. DBGFS_DUMP(VID_KEY1);
  92. DBGFS_DUMP(VID_KEY2);
  93. DBGFS_DUMP(VID_MPR0);
  94. DBGFS_DUMP(VID_MPR1);
  95. DBGFS_DUMP(VID_MPR2);
  96. DBGFS_DUMP(VID_MPR3);
  97. DBGFS_DUMP(VID_MST);
  98. vid_dbg_mst(s, readl(vid->regs + VID_MST));
  99. DBGFS_DUMP(VID_BC);
  100. DBGFS_DUMP(VID_TINT);
  101. DBGFS_DUMP(VID_CSAT);
  102. seq_puts(s, "\n");
  103. return 0;
  104. }
  105. static struct drm_info_list vid_debugfs_files[] = {
  106. { "vid", vid_dbg_show, 0, NULL },
  107. };
  108. int vid_debugfs_init(struct sti_vid *vid, struct drm_minor *minor)
  109. {
  110. unsigned int i;
  111. for (i = 0; i < ARRAY_SIZE(vid_debugfs_files); i++)
  112. vid_debugfs_files[i].data = vid;
  113. return drm_debugfs_create_files(vid_debugfs_files,
  114. ARRAY_SIZE(vid_debugfs_files),
  115. minor->debugfs_root, minor);
  116. }
  117. void sti_vid_commit(struct sti_vid *vid,
  118. struct drm_plane_state *state)
  119. {
  120. struct drm_crtc *crtc = state->crtc;
  121. struct drm_display_mode *mode = &crtc->mode;
  122. int dst_x = state->crtc_x;
  123. int dst_y = state->crtc_y;
  124. int dst_w = clamp_val(state->crtc_w, 0, mode->hdisplay - dst_x);
  125. int dst_h = clamp_val(state->crtc_h, 0, mode->vdisplay - dst_y);
  126. int src_h = state->src_h >> 16;
  127. u32 val, ydo, xdo, yds, xds;
  128. /* Input / output size
  129. * Align to upper even value */
  130. dst_w = ALIGN(dst_w, 2);
  131. dst_h = ALIGN(dst_h, 2);
  132. /* Unmask */
  133. val = readl(vid->regs + VID_CTL);
  134. val &= ~VID_CTL_IGNORE;
  135. writel(val, vid->regs + VID_CTL);
  136. ydo = sti_vtg_get_line_number(*mode, dst_y);
  137. yds = sti_vtg_get_line_number(*mode, dst_y + dst_h - 1);
  138. xdo = sti_vtg_get_pixel_number(*mode, dst_x);
  139. xds = sti_vtg_get_pixel_number(*mode, dst_x + dst_w - 1);
  140. writel((ydo << 16) | xdo, vid->regs + VID_VPO);
  141. writel((yds << 16) | xds, vid->regs + VID_VPS);
  142. /* Color conversion parameters */
  143. if (src_h >= VID_MIN_HD_HEIGHT) {
  144. writel(VID_MPR0_BT709, vid->regs + VID_MPR0);
  145. writel(VID_MPR1_BT709, vid->regs + VID_MPR1);
  146. writel(VID_MPR2_BT709, vid->regs + VID_MPR2);
  147. writel(VID_MPR3_BT709, vid->regs + VID_MPR3);
  148. } else {
  149. writel(VID_MPR0_BT601, vid->regs + VID_MPR0);
  150. writel(VID_MPR1_BT601, vid->regs + VID_MPR1);
  151. writel(VID_MPR2_BT601, vid->regs + VID_MPR2);
  152. writel(VID_MPR3_BT601, vid->regs + VID_MPR3);
  153. }
  154. }
  155. void sti_vid_disable(struct sti_vid *vid)
  156. {
  157. u32 val;
  158. /* Mask */
  159. val = readl(vid->regs + VID_CTL);
  160. val |= VID_CTL_IGNORE;
  161. writel(val, vid->regs + VID_CTL);
  162. }
  163. static void sti_vid_init(struct sti_vid *vid)
  164. {
  165. /* Enable PSI, Mask layer */
  166. writel(VID_CTL_PSI_ENABLE | VID_CTL_IGNORE, vid->regs + VID_CTL);
  167. /* Opaque */
  168. writel(VID_ALP_OPAQUE, vid->regs + VID_ALP);
  169. /* Brightness, contrast, tint, saturation */
  170. writel(VID_BC_DFLT, vid->regs + VID_BC);
  171. writel(VID_TINT_DFLT, vid->regs + VID_TINT);
  172. writel(VID_CSAT_DFLT, vid->regs + VID_CSAT);
  173. }
  174. struct sti_vid *sti_vid_create(struct device *dev, struct drm_device *drm_dev,
  175. int id, void __iomem *baseaddr)
  176. {
  177. struct sti_vid *vid;
  178. vid = devm_kzalloc(dev, sizeof(*vid), GFP_KERNEL);
  179. if (!vid) {
  180. DRM_ERROR("Failed to allocate memory for VID\n");
  181. return NULL;
  182. }
  183. vid->dev = dev;
  184. vid->regs = baseaddr;
  185. vid->id = id;
  186. sti_vid_init(vid);
  187. return vid;
  188. }