sti_mixer.c 9.0 KB

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