sti_mixer.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Authors: Benjamin Gaignard <benjamin.gaignard@st.com>
  4. * Fabien Dessenne <fabien.dessenne@st.com>
  5. * for STMicroelectronics.
  6. * License terms: GNU General Public License (GPL), version 2
  7. */
  8. #include "sti_compositor.h"
  9. #include "sti_mixer.h"
  10. #include "sti_vtg.h"
  11. /* Identity: G=Y , B=Cb , R=Cr */
  12. static const u32 mixerColorSpaceMatIdentity[] = {
  13. 0x10000000, 0x00000000, 0x10000000, 0x00001000,
  14. 0x00000000, 0x00000000, 0x00000000, 0x00000000
  15. };
  16. /* regs offset */
  17. #define GAM_MIXER_CTL 0x00
  18. #define GAM_MIXER_BKC 0x04
  19. #define GAM_MIXER_BCO 0x0C
  20. #define GAM_MIXER_BCS 0x10
  21. #define GAM_MIXER_AVO 0x28
  22. #define GAM_MIXER_AVS 0x2C
  23. #define GAM_MIXER_CRB 0x34
  24. #define GAM_MIXER_ACT 0x38
  25. #define GAM_MIXER_MBP 0x3C
  26. #define GAM_MIXER_MX0 0x80
  27. /* id for depth of CRB reg */
  28. #define GAM_DEPTH_VID0_ID 1
  29. #define GAM_DEPTH_VID1_ID 2
  30. #define GAM_DEPTH_GDP0_ID 3
  31. #define GAM_DEPTH_GDP1_ID 4
  32. #define GAM_DEPTH_GDP2_ID 5
  33. #define GAM_DEPTH_GDP3_ID 6
  34. #define GAM_DEPTH_MASK_ID 7
  35. /* mask in CTL reg */
  36. #define GAM_CTL_BACK_MASK BIT(0)
  37. #define GAM_CTL_VID0_MASK BIT(1)
  38. #define GAM_CTL_VID1_MASK BIT(2)
  39. #define GAM_CTL_GDP0_MASK BIT(3)
  40. #define GAM_CTL_GDP1_MASK BIT(4)
  41. #define GAM_CTL_GDP2_MASK BIT(5)
  42. #define GAM_CTL_GDP3_MASK BIT(6)
  43. #define GAM_CTL_CURSOR_MASK BIT(9)
  44. const char *sti_mixer_to_str(struct sti_mixer *mixer)
  45. {
  46. switch (mixer->id) {
  47. case STI_MIXER_MAIN:
  48. return "MAIN_MIXER";
  49. case STI_MIXER_AUX:
  50. return "AUX_MIXER";
  51. default:
  52. return "<UNKNOWN MIXER>";
  53. }
  54. }
  55. EXPORT_SYMBOL(sti_mixer_to_str);
  56. static inline u32 sti_mixer_reg_read(struct sti_mixer *mixer, u32 reg_id)
  57. {
  58. return readl(mixer->regs + reg_id);
  59. }
  60. static inline void sti_mixer_reg_write(struct sti_mixer *mixer,
  61. u32 reg_id, u32 val)
  62. {
  63. writel(val, mixer->regs + reg_id);
  64. }
  65. void sti_mixer_set_background_status(struct sti_mixer *mixer, bool enable)
  66. {
  67. u32 val = sti_mixer_reg_read(mixer, GAM_MIXER_CTL);
  68. val &= ~GAM_CTL_BACK_MASK;
  69. val |= enable;
  70. sti_mixer_reg_write(mixer, GAM_MIXER_CTL, val);
  71. }
  72. static void sti_mixer_set_background_color(struct sti_mixer *mixer,
  73. u8 red, u8 green, u8 blue)
  74. {
  75. u32 val = (red << 16) | (green << 8) | blue;
  76. sti_mixer_reg_write(mixer, GAM_MIXER_BKC, val);
  77. }
  78. static void sti_mixer_set_background_area(struct sti_mixer *mixer,
  79. struct drm_display_mode *mode)
  80. {
  81. u32 ydo, xdo, yds, xds;
  82. ydo = sti_vtg_get_line_number(*mode, 0);
  83. yds = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
  84. xdo = sti_vtg_get_pixel_number(*mode, 0);
  85. xds = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
  86. sti_mixer_reg_write(mixer, GAM_MIXER_BCO, ydo << 16 | xdo);
  87. sti_mixer_reg_write(mixer, GAM_MIXER_BCS, yds << 16 | xds);
  88. }
  89. int sti_mixer_set_plane_depth(struct sti_mixer *mixer, struct sti_plane *plane)
  90. {
  91. int plane_id, depth = plane->zorder;
  92. unsigned int i;
  93. u32 mask, val;
  94. if ((depth < 1) || (depth > GAM_MIXER_NB_DEPTH_LEVEL))
  95. return 1;
  96. switch (plane->desc) {
  97. case STI_GDP_0:
  98. plane_id = GAM_DEPTH_GDP0_ID;
  99. break;
  100. case STI_GDP_1:
  101. plane_id = GAM_DEPTH_GDP1_ID;
  102. break;
  103. case STI_GDP_2:
  104. plane_id = GAM_DEPTH_GDP2_ID;
  105. break;
  106. case STI_GDP_3:
  107. plane_id = GAM_DEPTH_GDP3_ID;
  108. break;
  109. case STI_HQVDP_0:
  110. plane_id = GAM_DEPTH_VID0_ID;
  111. break;
  112. case STI_CURSOR:
  113. /* no need to set depth for cursor */
  114. return 0;
  115. default:
  116. DRM_ERROR("Unknown plane %d\n", plane->desc);
  117. return 1;
  118. }
  119. /* Search if a previous depth was already assigned to the plane */
  120. val = sti_mixer_reg_read(mixer, GAM_MIXER_CRB);
  121. for (i = 0; i < GAM_MIXER_NB_DEPTH_LEVEL; i++) {
  122. mask = GAM_DEPTH_MASK_ID << (3 * i);
  123. if ((val & mask) == plane_id << (3 * i))
  124. break;
  125. }
  126. mask |= GAM_DEPTH_MASK_ID << (3 * (depth - 1));
  127. plane_id = plane_id << (3 * (depth - 1));
  128. DRM_DEBUG_DRIVER("%s %s depth=%d\n", sti_mixer_to_str(mixer),
  129. sti_plane_to_str(plane), depth);
  130. dev_dbg(mixer->dev, "GAM_MIXER_CRB val 0x%x mask 0x%x\n",
  131. plane_id, mask);
  132. val &= ~mask;
  133. val |= plane_id;
  134. sti_mixer_reg_write(mixer, GAM_MIXER_CRB, val);
  135. dev_dbg(mixer->dev, "Read GAM_MIXER_CRB 0x%x\n",
  136. sti_mixer_reg_read(mixer, GAM_MIXER_CRB));
  137. return 0;
  138. }
  139. int sti_mixer_active_video_area(struct sti_mixer *mixer,
  140. struct drm_display_mode *mode)
  141. {
  142. u32 ydo, xdo, yds, xds;
  143. ydo = sti_vtg_get_line_number(*mode, 0);
  144. yds = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
  145. xdo = sti_vtg_get_pixel_number(*mode, 0);
  146. xds = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
  147. DRM_DEBUG_DRIVER("%s active video area xdo:%d ydo:%d xds:%d yds:%d\n",
  148. sti_mixer_to_str(mixer), xdo, ydo, xds, yds);
  149. sti_mixer_reg_write(mixer, GAM_MIXER_AVO, ydo << 16 | xdo);
  150. sti_mixer_reg_write(mixer, GAM_MIXER_AVS, yds << 16 | xds);
  151. sti_mixer_set_background_color(mixer, 0xFF, 0, 0);
  152. sti_mixer_set_background_area(mixer, mode);
  153. sti_mixer_set_background_status(mixer, true);
  154. return 0;
  155. }
  156. static u32 sti_mixer_get_plane_mask(struct sti_plane *plane)
  157. {
  158. switch (plane->desc) {
  159. case STI_BACK:
  160. return GAM_CTL_BACK_MASK;
  161. case STI_GDP_0:
  162. return GAM_CTL_GDP0_MASK;
  163. case STI_GDP_1:
  164. return GAM_CTL_GDP1_MASK;
  165. case STI_GDP_2:
  166. return GAM_CTL_GDP2_MASK;
  167. case STI_GDP_3:
  168. return GAM_CTL_GDP3_MASK;
  169. case STI_HQVDP_0:
  170. return GAM_CTL_VID0_MASK;
  171. case STI_CURSOR:
  172. return GAM_CTL_CURSOR_MASK;
  173. default:
  174. return 0;
  175. }
  176. }
  177. int sti_mixer_set_plane_status(struct sti_mixer *mixer,
  178. struct sti_plane *plane, bool status)
  179. {
  180. u32 mask, val;
  181. DRM_DEBUG_DRIVER("%s %s %s\n", status ? "enable" : "disable",
  182. sti_mixer_to_str(mixer), sti_plane_to_str(plane));
  183. mask = sti_mixer_get_plane_mask(plane);
  184. if (!mask) {
  185. DRM_ERROR("Can't find layer mask\n");
  186. return -EINVAL;
  187. }
  188. val = sti_mixer_reg_read(mixer, GAM_MIXER_CTL);
  189. val &= ~mask;
  190. val |= status ? mask : 0;
  191. sti_mixer_reg_write(mixer, GAM_MIXER_CTL, val);
  192. return 0;
  193. }
  194. void sti_mixer_set_matrix(struct sti_mixer *mixer)
  195. {
  196. unsigned int i;
  197. for (i = 0; i < ARRAY_SIZE(mixerColorSpaceMatIdentity); i++)
  198. sti_mixer_reg_write(mixer, GAM_MIXER_MX0 + (i * 4),
  199. mixerColorSpaceMatIdentity[i]);
  200. }
  201. struct sti_mixer *sti_mixer_create(struct device *dev, int id,
  202. void __iomem *baseaddr)
  203. {
  204. struct sti_mixer *mixer = devm_kzalloc(dev, sizeof(*mixer), GFP_KERNEL);
  205. struct device_node *np = dev->of_node;
  206. dev_dbg(dev, "%s\n", __func__);
  207. if (!mixer) {
  208. DRM_ERROR("Failed to allocated memory for mixer\n");
  209. return NULL;
  210. }
  211. mixer->regs = baseaddr;
  212. mixer->dev = dev;
  213. mixer->id = id;
  214. if (of_device_is_compatible(np, "st,stih416-compositor"))
  215. sti_mixer_set_matrix(mixer);
  216. DRM_DEBUG_DRIVER("%s created. Regs=%p\n",
  217. sti_mixer_to_str(mixer), mixer->regs);
  218. return mixer;
  219. }