sti_mixer.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <linux/seq_file.h>
  9. #include "sti_compositor.h"
  10. #include "sti_mixer.h"
  11. #include "sti_vtg.h"
  12. /* Module parameter to set the background color of the mixer */
  13. static unsigned int bkg_color = 0x000000;
  14. MODULE_PARM_DESC(bkgcolor, "Value of the background color 0xRRGGBB");
  15. module_param_named(bkgcolor, bkg_color, int, 0644);
  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. static inline u32 sti_mixer_reg_read(struct sti_mixer *mixer, u32 reg_id)
  56. {
  57. return readl(mixer->regs + reg_id);
  58. }
  59. static inline void sti_mixer_reg_write(struct sti_mixer *mixer,
  60. u32 reg_id, u32 val)
  61. {
  62. writel(val, mixer->regs + reg_id);
  63. }
  64. #define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
  65. sti_mixer_reg_read(mixer, reg))
  66. static void mixer_dbg_ctl(struct seq_file *s, int val)
  67. {
  68. unsigned int i;
  69. int count = 0;
  70. char *const disp_layer[] = {"BKG", "VID0", "VID1", "GDP0",
  71. "GDP1", "GDP2", "GDP3"};
  72. seq_puts(s, "\tEnabled: ");
  73. for (i = 0; i < 7; i++) {
  74. if (val & 1) {
  75. seq_printf(s, "%s ", disp_layer[i]);
  76. count++;
  77. }
  78. val = val >> 1;
  79. }
  80. val = val >> 2;
  81. if (val & 1) {
  82. seq_puts(s, "CURS ");
  83. count++;
  84. }
  85. if (!count)
  86. seq_puts(s, "Nothing");
  87. }
  88. static void mixer_dbg_crb(struct seq_file *s, int val)
  89. {
  90. int i;
  91. seq_puts(s, "\tDepth: ");
  92. for (i = 0; i < GAM_MIXER_NB_DEPTH_LEVEL; i++) {
  93. switch (val & GAM_DEPTH_MASK_ID) {
  94. case GAM_DEPTH_VID0_ID:
  95. seq_puts(s, "VID0");
  96. break;
  97. case GAM_DEPTH_VID1_ID:
  98. seq_puts(s, "VID1");
  99. break;
  100. case GAM_DEPTH_GDP0_ID:
  101. seq_puts(s, "GDP0");
  102. break;
  103. case GAM_DEPTH_GDP1_ID:
  104. seq_puts(s, "GDP1");
  105. break;
  106. case GAM_DEPTH_GDP2_ID:
  107. seq_puts(s, "GDP2");
  108. break;
  109. case GAM_DEPTH_GDP3_ID:
  110. seq_puts(s, "GDP3");
  111. break;
  112. default:
  113. seq_puts(s, "---");
  114. }
  115. if (i < GAM_MIXER_NB_DEPTH_LEVEL - 1)
  116. seq_puts(s, " < ");
  117. val = val >> 3;
  118. }
  119. }
  120. static void mixer_dbg_mxn(struct seq_file *s, void *addr)
  121. {
  122. int i;
  123. for (i = 1; i < 8; i++)
  124. seq_printf(s, "-0x%08X", (int)readl(addr + i * 4));
  125. }
  126. static int mixer_dbg_show(struct seq_file *s, void *arg)
  127. {
  128. struct drm_info_node *node = s->private;
  129. struct sti_mixer *mixer = (struct sti_mixer *)node->info_ent->data;
  130. seq_printf(s, "%s: (vaddr = 0x%p)",
  131. sti_mixer_to_str(mixer), mixer->regs);
  132. DBGFS_DUMP(GAM_MIXER_CTL);
  133. mixer_dbg_ctl(s, sti_mixer_reg_read(mixer, GAM_MIXER_CTL));
  134. DBGFS_DUMP(GAM_MIXER_BKC);
  135. DBGFS_DUMP(GAM_MIXER_BCO);
  136. DBGFS_DUMP(GAM_MIXER_BCS);
  137. DBGFS_DUMP(GAM_MIXER_AVO);
  138. DBGFS_DUMP(GAM_MIXER_AVS);
  139. DBGFS_DUMP(GAM_MIXER_CRB);
  140. mixer_dbg_crb(s, sti_mixer_reg_read(mixer, GAM_MIXER_CRB));
  141. DBGFS_DUMP(GAM_MIXER_ACT);
  142. DBGFS_DUMP(GAM_MIXER_MBP);
  143. DBGFS_DUMP(GAM_MIXER_MX0);
  144. mixer_dbg_mxn(s, mixer->regs + GAM_MIXER_MX0);
  145. seq_puts(s, "\n");
  146. return 0;
  147. }
  148. static struct drm_info_list mixer0_debugfs_files[] = {
  149. { "mixer_main", mixer_dbg_show, 0, NULL },
  150. };
  151. static struct drm_info_list mixer1_debugfs_files[] = {
  152. { "mixer_aux", mixer_dbg_show, 0, NULL },
  153. };
  154. int sti_mixer_debugfs_init(struct sti_mixer *mixer, struct drm_minor *minor)
  155. {
  156. unsigned int i;
  157. struct drm_info_list *mixer_debugfs_files;
  158. int nb_files;
  159. switch (mixer->id) {
  160. case STI_MIXER_MAIN:
  161. mixer_debugfs_files = mixer0_debugfs_files;
  162. nb_files = ARRAY_SIZE(mixer0_debugfs_files);
  163. break;
  164. case STI_MIXER_AUX:
  165. mixer_debugfs_files = mixer1_debugfs_files;
  166. nb_files = ARRAY_SIZE(mixer1_debugfs_files);
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. for (i = 0; i < nb_files; i++)
  172. mixer_debugfs_files[i].data = mixer;
  173. return drm_debugfs_create_files(mixer_debugfs_files,
  174. nb_files,
  175. minor->debugfs_root, minor);
  176. }
  177. void sti_mixer_set_background_status(struct sti_mixer *mixer, bool enable)
  178. {
  179. u32 val = sti_mixer_reg_read(mixer, GAM_MIXER_CTL);
  180. val &= ~GAM_CTL_BACK_MASK;
  181. val |= enable;
  182. sti_mixer_reg_write(mixer, GAM_MIXER_CTL, val);
  183. }
  184. static void sti_mixer_set_background_color(struct sti_mixer *mixer,
  185. unsigned int rgb)
  186. {
  187. sti_mixer_reg_write(mixer, GAM_MIXER_BKC, rgb);
  188. }
  189. static void sti_mixer_set_background_area(struct sti_mixer *mixer,
  190. struct drm_display_mode *mode)
  191. {
  192. u32 ydo, xdo, yds, xds;
  193. ydo = sti_vtg_get_line_number(*mode, 0);
  194. yds = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
  195. xdo = sti_vtg_get_pixel_number(*mode, 0);
  196. xds = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
  197. sti_mixer_reg_write(mixer, GAM_MIXER_BCO, ydo << 16 | xdo);
  198. sti_mixer_reg_write(mixer, GAM_MIXER_BCS, yds << 16 | xds);
  199. }
  200. int sti_mixer_set_plane_depth(struct sti_mixer *mixer, struct sti_plane *plane)
  201. {
  202. int plane_id, depth = plane->drm_plane.state->normalized_zpos;
  203. unsigned int i;
  204. u32 mask, val;
  205. switch (plane->desc) {
  206. case STI_GDP_0:
  207. plane_id = GAM_DEPTH_GDP0_ID;
  208. break;
  209. case STI_GDP_1:
  210. plane_id = GAM_DEPTH_GDP1_ID;
  211. break;
  212. case STI_GDP_2:
  213. plane_id = GAM_DEPTH_GDP2_ID;
  214. break;
  215. case STI_GDP_3:
  216. plane_id = GAM_DEPTH_GDP3_ID;
  217. break;
  218. case STI_HQVDP_0:
  219. plane_id = GAM_DEPTH_VID0_ID;
  220. break;
  221. case STI_CURSOR:
  222. /* no need to set depth for cursor */
  223. return 0;
  224. default:
  225. DRM_ERROR("Unknown plane %d\n", plane->desc);
  226. return 1;
  227. }
  228. /* Search if a previous depth was already assigned to the plane */
  229. val = sti_mixer_reg_read(mixer, GAM_MIXER_CRB);
  230. for (i = 0; i < GAM_MIXER_NB_DEPTH_LEVEL; i++) {
  231. mask = GAM_DEPTH_MASK_ID << (3 * i);
  232. if ((val & mask) == plane_id << (3 * i))
  233. break;
  234. }
  235. mask |= GAM_DEPTH_MASK_ID << (3 * depth);
  236. plane_id = plane_id << (3 * depth);
  237. DRM_DEBUG_DRIVER("%s %s depth=%d\n", sti_mixer_to_str(mixer),
  238. sti_plane_to_str(plane), depth);
  239. dev_dbg(mixer->dev, "GAM_MIXER_CRB val 0x%x mask 0x%x\n",
  240. plane_id, mask);
  241. val &= ~mask;
  242. val |= plane_id;
  243. sti_mixer_reg_write(mixer, GAM_MIXER_CRB, val);
  244. dev_dbg(mixer->dev, "Read GAM_MIXER_CRB 0x%x\n",
  245. sti_mixer_reg_read(mixer, GAM_MIXER_CRB));
  246. return 0;
  247. }
  248. int sti_mixer_active_video_area(struct sti_mixer *mixer,
  249. struct drm_display_mode *mode)
  250. {
  251. u32 ydo, xdo, yds, xds;
  252. ydo = sti_vtg_get_line_number(*mode, 0);
  253. yds = sti_vtg_get_line_number(*mode, mode->vdisplay - 1);
  254. xdo = sti_vtg_get_pixel_number(*mode, 0);
  255. xds = sti_vtg_get_pixel_number(*mode, mode->hdisplay - 1);
  256. DRM_DEBUG_DRIVER("%s active video area xdo:%d ydo:%d xds:%d yds:%d\n",
  257. sti_mixer_to_str(mixer), xdo, ydo, xds, yds);
  258. sti_mixer_reg_write(mixer, GAM_MIXER_AVO, ydo << 16 | xdo);
  259. sti_mixer_reg_write(mixer, GAM_MIXER_AVS, yds << 16 | xds);
  260. sti_mixer_set_background_color(mixer, bkg_color);
  261. sti_mixer_set_background_area(mixer, mode);
  262. sti_mixer_set_background_status(mixer, true);
  263. return 0;
  264. }
  265. static u32 sti_mixer_get_plane_mask(struct sti_plane *plane)
  266. {
  267. switch (plane->desc) {
  268. case STI_BACK:
  269. return GAM_CTL_BACK_MASK;
  270. case STI_GDP_0:
  271. return GAM_CTL_GDP0_MASK;
  272. case STI_GDP_1:
  273. return GAM_CTL_GDP1_MASK;
  274. case STI_GDP_2:
  275. return GAM_CTL_GDP2_MASK;
  276. case STI_GDP_3:
  277. return GAM_CTL_GDP3_MASK;
  278. case STI_HQVDP_0:
  279. return GAM_CTL_VID0_MASK;
  280. case STI_CURSOR:
  281. return GAM_CTL_CURSOR_MASK;
  282. default:
  283. return 0;
  284. }
  285. }
  286. int sti_mixer_set_plane_status(struct sti_mixer *mixer,
  287. struct sti_plane *plane, bool status)
  288. {
  289. u32 mask, val;
  290. DRM_DEBUG_DRIVER("%s %s %s\n", status ? "enable" : "disable",
  291. sti_mixer_to_str(mixer), sti_plane_to_str(plane));
  292. mask = sti_mixer_get_plane_mask(plane);
  293. if (!mask) {
  294. DRM_ERROR("Can't find layer mask\n");
  295. return -EINVAL;
  296. }
  297. val = sti_mixer_reg_read(mixer, GAM_MIXER_CTL);
  298. val &= ~mask;
  299. val |= status ? mask : 0;
  300. sti_mixer_reg_write(mixer, GAM_MIXER_CTL, val);
  301. return 0;
  302. }
  303. struct sti_mixer *sti_mixer_create(struct device *dev,
  304. struct drm_device *drm_dev,
  305. int id,
  306. void __iomem *baseaddr)
  307. {
  308. struct sti_mixer *mixer = devm_kzalloc(dev, sizeof(*mixer), GFP_KERNEL);
  309. dev_dbg(dev, "%s\n", __func__);
  310. if (!mixer) {
  311. DRM_ERROR("Failed to allocated memory for mixer\n");
  312. return NULL;
  313. }
  314. mixer->regs = baseaddr;
  315. mixer->dev = dev;
  316. mixer->id = id;
  317. DRM_DEBUG_DRIVER("%s created. Regs=%p\n",
  318. sti_mixer_to_str(mixer), mixer->regs);
  319. return mixer;
  320. }