sti_mixer.c 6.7 KB

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