drm: sti: remove sti_gem_prime_export hack
[deliverable/linux.git] / drivers / gpu / drm / sti / sti_hqvdp.c
CommitLineData
4fdbc678
BG
1/*
2 * Copyright (C) STMicroelectronics SA 2014
3 * Authors: Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics.
4 * License terms: GNU General Public License (GPL), version 2
5 */
6
4fdbc678
BG
7#include <linux/component.h>
8#include <linux/firmware.h>
4fdbc678
BG
9#include <linux/reset.h>
10
dd86dc2f 11#include <drm/drm_atomic.h>
29d1dc62
VA
12#include <drm/drm_fb_cma_helper.h>
13#include <drm/drm_gem_cma_helper.h>
4fdbc678 14
29d1dc62 15#include "sti_compositor.h"
4fdbc678 16#include "sti_hqvdp_lut.h"
9e1f05b2 17#include "sti_plane.h"
4fdbc678
BG
18#include "sti_vtg.h"
19
20/* Firmware name */
21#define HQVDP_FMW_NAME "hqvdp-stih407.bin"
22
23/* Regs address */
24#define HQVDP_DMEM 0x00000000 /* 0x00000000 */
25#define HQVDP_PMEM 0x00040000 /* 0x00040000 */
26#define HQVDP_RD_PLUG 0x000E0000 /* 0x000E0000 */
27#define HQVDP_RD_PLUG_CONTROL (HQVDP_RD_PLUG + 0x1000) /* 0x000E1000 */
28#define HQVDP_RD_PLUG_PAGE_SIZE (HQVDP_RD_PLUG + 0x1004) /* 0x000E1004 */
29#define HQVDP_RD_PLUG_MIN_OPC (HQVDP_RD_PLUG + 0x1008) /* 0x000E1008 */
30#define HQVDP_RD_PLUG_MAX_OPC (HQVDP_RD_PLUG + 0x100C) /* 0x000E100C */
31#define HQVDP_RD_PLUG_MAX_CHK (HQVDP_RD_PLUG + 0x1010) /* 0x000E1010 */
32#define HQVDP_RD_PLUG_MAX_MSG (HQVDP_RD_PLUG + 0x1014) /* 0x000E1014 */
33#define HQVDP_RD_PLUG_MIN_SPACE (HQVDP_RD_PLUG + 0x1018) /* 0x000E1018 */
34#define HQVDP_WR_PLUG 0x000E2000 /* 0x000E2000 */
35#define HQVDP_WR_PLUG_CONTROL (HQVDP_WR_PLUG + 0x1000) /* 0x000E3000 */
36#define HQVDP_WR_PLUG_PAGE_SIZE (HQVDP_WR_PLUG + 0x1004) /* 0x000E3004 */
37#define HQVDP_WR_PLUG_MIN_OPC (HQVDP_WR_PLUG + 0x1008) /* 0x000E3008 */
38#define HQVDP_WR_PLUG_MAX_OPC (HQVDP_WR_PLUG + 0x100C) /* 0x000E300C */
39#define HQVDP_WR_PLUG_MAX_CHK (HQVDP_WR_PLUG + 0x1010) /* 0x000E3010 */
40#define HQVDP_WR_PLUG_MAX_MSG (HQVDP_WR_PLUG + 0x1014) /* 0x000E3014 */
41#define HQVDP_WR_PLUG_MIN_SPACE (HQVDP_WR_PLUG + 0x1018) /* 0x000E3018 */
42#define HQVDP_MBX 0x000E4000 /* 0x000E4000 */
43#define HQVDP_MBX_IRQ_TO_XP70 (HQVDP_MBX + 0x0000) /* 0x000E4000 */
44#define HQVDP_MBX_INFO_HOST (HQVDP_MBX + 0x0004) /* 0x000E4004 */
45#define HQVDP_MBX_IRQ_TO_HOST (HQVDP_MBX + 0x0008) /* 0x000E4008 */
46#define HQVDP_MBX_INFO_XP70 (HQVDP_MBX + 0x000C) /* 0x000E400C */
47#define HQVDP_MBX_SW_RESET_CTRL (HQVDP_MBX + 0x0010) /* 0x000E4010 */
48#define HQVDP_MBX_STARTUP_CTRL1 (HQVDP_MBX + 0x0014) /* 0x000E4014 */
49#define HQVDP_MBX_STARTUP_CTRL2 (HQVDP_MBX + 0x0018) /* 0x000E4018 */
50#define HQVDP_MBX_GP_STATUS (HQVDP_MBX + 0x001C) /* 0x000E401C */
51#define HQVDP_MBX_NEXT_CMD (HQVDP_MBX + 0x0020) /* 0x000E4020 */
52#define HQVDP_MBX_CURRENT_CMD (HQVDP_MBX + 0x0024) /* 0x000E4024 */
53#define HQVDP_MBX_SOFT_VSYNC (HQVDP_MBX + 0x0028) /* 0x000E4028 */
54
55/* Plugs config */
56#define PLUG_CONTROL_ENABLE 0x00000001
57#define PLUG_PAGE_SIZE_256 0x00000002
58#define PLUG_MIN_OPC_8 0x00000003
59#define PLUG_MAX_OPC_64 0x00000006
60#define PLUG_MAX_CHK_2X 0x00000001
61#define PLUG_MAX_MSG_1X 0x00000000
62#define PLUG_MIN_SPACE_1 0x00000000
63
64/* SW reset CTRL */
65#define SW_RESET_CTRL_FULL BIT(0)
66#define SW_RESET_CTRL_CORE BIT(1)
67
68/* Startup ctrl 1 */
69#define STARTUP_CTRL1_RST_DONE BIT(0)
70#define STARTUP_CTRL1_AUTH_IDLE BIT(2)
71
72/* Startup ctrl 2 */
73#define STARTUP_CTRL2_FETCH_EN BIT(1)
74
75/* Info xP70 */
76#define INFO_XP70_FW_READY BIT(15)
77#define INFO_XP70_FW_PROCESSING BIT(14)
78#define INFO_XP70_FW_INITQUEUES BIT(13)
79
80/* SOFT_VSYNC */
81#define SOFT_VSYNC_HW 0x00000000
82#define SOFT_VSYNC_SW_CMD 0x00000001
83#define SOFT_VSYNC_SW_CTRL_IRQ 0x00000003
84
85/* Reset & boot poll config */
86#define POLL_MAX_ATTEMPT 50
87#define POLL_DELAY_MS 20
88
89#define SCALE_FACTOR 8192
90#define SCALE_MAX_FOR_LEG_LUT_F 4096
91#define SCALE_MAX_FOR_LEG_LUT_E 4915
92#define SCALE_MAX_FOR_LEG_LUT_D 6654
93#define SCALE_MAX_FOR_LEG_LUT_C 8192
94
95enum sti_hvsrc_orient {
96 HVSRC_HORI,
97 HVSRC_VERT
98};
99
100/* Command structures */
101struct sti_hqvdp_top {
102 u32 config;
103 u32 mem_format;
104 u32 current_luma;
105 u32 current_enh_luma;
106 u32 current_right_luma;
107 u32 current_enh_right_luma;
108 u32 current_chroma;
109 u32 current_enh_chroma;
110 u32 current_right_chroma;
111 u32 current_enh_right_chroma;
112 u32 output_luma;
113 u32 output_chroma;
114 u32 luma_src_pitch;
115 u32 luma_enh_src_pitch;
116 u32 luma_right_src_pitch;
117 u32 luma_enh_right_src_pitch;
118 u32 chroma_src_pitch;
119 u32 chroma_enh_src_pitch;
120 u32 chroma_right_src_pitch;
121 u32 chroma_enh_right_src_pitch;
122 u32 luma_processed_pitch;
123 u32 chroma_processed_pitch;
124 u32 input_frame_size;
125 u32 input_viewport_ori;
126 u32 input_viewport_ori_right;
127 u32 input_viewport_size;
128 u32 left_view_border_width;
129 u32 right_view_border_width;
130 u32 left_view_3d_offset_width;
131 u32 right_view_3d_offset_width;
132 u32 side_stripe_color;
133 u32 crc_reset_ctrl;
134};
135
136/* Configs for interlaced : no IT, no pass thru, 3 fields */
137#define TOP_CONFIG_INTER_BTM 0x00000000
138#define TOP_CONFIG_INTER_TOP 0x00000002
139
140/* Config for progressive : no IT, no pass thru, 3 fields */
141#define TOP_CONFIG_PROGRESSIVE 0x00000001
142
143/* Default MemFormat: in=420_raster_dual out=444_raster;opaque Mem2Tv mode */
144#define TOP_MEM_FORMAT_DFLT 0x00018060
145
146/* Min/Max size */
147#define MAX_WIDTH 0x1FFF
148#define MAX_HEIGHT 0x0FFF
149#define MIN_WIDTH 0x0030
150#define MIN_HEIGHT 0x0010
151
152struct sti_hqvdp_vc1re {
153 u32 ctrl_prv_csdi;
154 u32 ctrl_cur_csdi;
155 u32 ctrl_nxt_csdi;
156 u32 ctrl_cur_fmd;
157 u32 ctrl_nxt_fmd;
158};
159
160struct sti_hqvdp_fmd {
161 u32 config;
162 u32 viewport_ori;
163 u32 viewport_size;
164 u32 next_next_luma;
165 u32 next_next_right_luma;
166 u32 next_next_next_luma;
167 u32 next_next_next_right_luma;
168 u32 threshold_scd;
169 u32 threshold_rfd;
170 u32 threshold_move;
171 u32 threshold_cfd;
172};
173
174struct sti_hqvdp_csdi {
175 u32 config;
176 u32 config2;
177 u32 dcdi_config;
178 u32 prev_luma;
179 u32 prev_enh_luma;
180 u32 prev_right_luma;
181 u32 prev_enh_right_luma;
182 u32 next_luma;
183 u32 next_enh_luma;
184 u32 next_right_luma;
185 u32 next_enh_right_luma;
186 u32 prev_chroma;
187 u32 prev_enh_chroma;
188 u32 prev_right_chroma;
189 u32 prev_enh_right_chroma;
190 u32 next_chroma;
191 u32 next_enh_chroma;
192 u32 next_right_chroma;
193 u32 next_enh_right_chroma;
194 u32 prev_motion;
195 u32 prev_right_motion;
196 u32 cur_motion;
197 u32 cur_right_motion;
198 u32 next_motion;
199 u32 next_right_motion;
200};
201
202/* Config for progressive: by pass */
203#define CSDI_CONFIG_PROG 0x00000000
204/* Config for directional deinterlacing without motion */
205#define CSDI_CONFIG_INTER_DIR 0x00000016
206/* Additional configs for fader, blender, motion,... deinterlace algorithms */
207#define CSDI_CONFIG2_DFLT 0x000001B3
208#define CSDI_DCDI_CONFIG_DFLT 0x00203803
209
210struct sti_hqvdp_hvsrc {
211 u32 hor_panoramic_ctrl;
212 u32 output_picture_size;
213 u32 init_horizontal;
214 u32 init_vertical;
215 u32 param_ctrl;
216 u32 yh_coef[NB_COEF];
217 u32 ch_coef[NB_COEF];
218 u32 yv_coef[NB_COEF];
219 u32 cv_coef[NB_COEF];
220 u32 hori_shift;
221 u32 vert_shift;
222};
223
224/* Default ParamCtrl: all controls enabled */
225#define HVSRC_PARAM_CTRL_DFLT 0xFFFFFFFF
226
227struct sti_hqvdp_iqi {
228 u32 config;
229 u32 demo_wind_size;
230 u32 pk_config;
231 u32 coeff0_coeff1;
232 u32 coeff2_coeff3;
233 u32 coeff4;
234 u32 pk_lut;
235 u32 pk_gain;
236 u32 pk_coring_level;
237 u32 cti_config;
238 u32 le_config;
239 u32 le_lut[64];
240 u32 con_bri;
241 u32 sat_gain;
242 u32 pxf_conf;
243 u32 default_color;
244};
245
246/* Default Config : IQI bypassed */
247#define IQI_CONFIG_DFLT 0x00000001
248/* Default Contrast & Brightness gain = 256 */
249#define IQI_CON_BRI_DFLT 0x00000100
250/* Default Saturation gain = 256 */
251#define IQI_SAT_GAIN_DFLT 0x00000100
252/* Default PxfConf : P2I bypassed */
253#define IQI_PXF_CONF_DFLT 0x00000001
254
255struct sti_hqvdp_top_status {
256 u32 processing_time;
257 u32 input_y_crc;
258 u32 input_uv_crc;
259};
260
261struct sti_hqvdp_fmd_status {
262 u32 fmd_repeat_move_status;
263 u32 fmd_scene_count_status;
264 u32 cfd_sum;
265 u32 field_sum;
266 u32 next_y_fmd_crc;
267 u32 next_next_y_fmd_crc;
268 u32 next_next_next_y_fmd_crc;
269};
270
271struct sti_hqvdp_csdi_status {
272 u32 prev_y_csdi_crc;
273 u32 cur_y_csdi_crc;
274 u32 next_y_csdi_crc;
275 u32 prev_uv_csdi_crc;
276 u32 cur_uv_csdi_crc;
277 u32 next_uv_csdi_crc;
278 u32 y_csdi_crc;
279 u32 uv_csdi_crc;
280 u32 uv_cup_crc;
281 u32 mot_csdi_crc;
282 u32 mot_cur_csdi_crc;
283 u32 mot_prev_csdi_crc;
284};
285
286struct sti_hqvdp_hvsrc_status {
287 u32 y_hvsrc_crc;
288 u32 u_hvsrc_crc;
289 u32 v_hvsrc_crc;
290};
291
292struct sti_hqvdp_iqi_status {
293 u32 pxf_it_status;
294 u32 y_iqi_crc;
295 u32 u_iqi_crc;
296 u32 v_iqi_crc;
297};
298
299/* Main commands. We use 2 commands one being processed by the firmware, one
300 * ready to be fetched upon next Vsync*/
301#define NB_VDP_CMD 2
302
303struct sti_hqvdp_cmd {
304 struct sti_hqvdp_top top;
305 struct sti_hqvdp_vc1re vc1re;
306 struct sti_hqvdp_fmd fmd;
307 struct sti_hqvdp_csdi csdi;
308 struct sti_hqvdp_hvsrc hvsrc;
309 struct sti_hqvdp_iqi iqi;
310 struct sti_hqvdp_top_status top_status;
311 struct sti_hqvdp_fmd_status fmd_status;
312 struct sti_hqvdp_csdi_status csdi_status;
313 struct sti_hqvdp_hvsrc_status hvsrc_status;
314 struct sti_hqvdp_iqi_status iqi_status;
315};
316
317/*
318 * STI HQVDP structure
319 *
320 * @dev: driver device
321 * @drm_dev: the drm device
322 * @regs: registers
871bcdfe 323 * @plane: plane structure for hqvdp it self
4fdbc678
BG
324 * @clk: IP clock
325 * @clk_pix_main: pix main clock
326 * @reset: reset control
327 * @vtg_nb: notifier to handle VTG Vsync
328 * @btm_field_pending: is there any bottom field (interlaced frame) to display
4fdbc678
BG
329 * @hqvdp_cmd: buffer of commands
330 * @hqvdp_cmd_paddr: physical address of hqvdp_cmd
331 * @vtg: vtg for main data path
871bcdfe 332 * @xp70_initialized: true if xp70 is already initialized
4fdbc678
BG
333 */
334struct sti_hqvdp {
335 struct device *dev;
336 struct drm_device *drm_dev;
337 void __iomem *regs;
871bcdfe 338 struct sti_plane plane;
4fdbc678
BG
339 struct clk *clk;
340 struct clk *clk_pix_main;
341 struct reset_control *reset;
342 struct notifier_block vtg_nb;
343 bool btm_field_pending;
4fdbc678
BG
344 void *hqvdp_cmd;
345 dma_addr_t hqvdp_cmd_paddr;
346 struct sti_vtg *vtg;
871bcdfe 347 bool xp70_initialized;
4fdbc678
BG
348};
349
871bcdfe 350#define to_sti_hqvdp(x) container_of(x, struct sti_hqvdp, plane)
4fdbc678
BG
351
352static const uint32_t hqvdp_supported_formats[] = {
353 DRM_FORMAT_NV12,
354};
355
4fdbc678
BG
356/**
357 * sti_hqvdp_get_free_cmd
358 * @hqvdp: hqvdp structure
359 *
360 * Look for a hqvdp_cmd that is not being used (or about to be used) by the FW.
361 *
362 * RETURNS:
363 * the offset of the command to be used.
364 * -1 in error cases
365 */
366static int sti_hqvdp_get_free_cmd(struct sti_hqvdp *hqvdp)
367{
368 int curr_cmd, next_cmd;
369 dma_addr_t cmd = hqvdp->hqvdp_cmd_paddr;
370 int i;
371
372 curr_cmd = readl(hqvdp->regs + HQVDP_MBX_CURRENT_CMD);
373 next_cmd = readl(hqvdp->regs + HQVDP_MBX_NEXT_CMD);
374
375 for (i = 0; i < NB_VDP_CMD; i++) {
376 if ((cmd != curr_cmd) && (cmd != next_cmd))
377 return i * sizeof(struct sti_hqvdp_cmd);
378 cmd += sizeof(struct sti_hqvdp_cmd);
379 }
380
381 return -1;
382}
383
384/**
385 * sti_hqvdp_get_curr_cmd
386 * @hqvdp: hqvdp structure
387 *
388 * Look for the hqvdp_cmd that is being used by the FW.
389 *
390 * RETURNS:
391 * the offset of the command to be used.
392 * -1 in error cases
393 */
394static int sti_hqvdp_get_curr_cmd(struct sti_hqvdp *hqvdp)
395{
396 int curr_cmd;
397 dma_addr_t cmd = hqvdp->hqvdp_cmd_paddr;
398 unsigned int i;
399
400 curr_cmd = readl(hqvdp->regs + HQVDP_MBX_CURRENT_CMD);
401
402 for (i = 0; i < NB_VDP_CMD; i++) {
403 if (cmd == curr_cmd)
404 return i * sizeof(struct sti_hqvdp_cmd);
405
406 cmd += sizeof(struct sti_hqvdp_cmd);
407 }
408
409 return -1;
410}
411
670454bb
VA
412/**
413 * sti_hqvdp_get_next_cmd
414 * @hqvdp: hqvdp structure
415 *
416 * Look for the next hqvdp_cmd that will be used by the FW.
417 *
418 * RETURNS:
419 * the offset of the next command that will be used.
420 * -1 in error cases
421 */
422static int sti_hqvdp_get_next_cmd(struct sti_hqvdp *hqvdp)
423{
424 int next_cmd;
425 dma_addr_t cmd = hqvdp->hqvdp_cmd_paddr;
426 unsigned int i;
427
428 next_cmd = readl(hqvdp->regs + HQVDP_MBX_NEXT_CMD);
429
430 for (i = 0; i < NB_VDP_CMD; i++) {
431 if (cmd == next_cmd)
432 return i * sizeof(struct sti_hqvdp_cmd);
433
434 cmd += sizeof(struct sti_hqvdp_cmd);
435 }
436
437 return -1;
438}
439
440#define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
441 readl(hqvdp->regs + reg))
442
443static const char *hqvdp_dbg_get_lut(u32 *coef)
444{
445 if (!memcmp(coef, coef_lut_a_legacy, 16))
446 return "LUT A";
447 if (!memcmp(coef, coef_lut_b, 16))
448 return "LUT B";
449 if (!memcmp(coef, coef_lut_c_y_legacy, 16))
450 return "LUT C Y";
451 if (!memcmp(coef, coef_lut_c_c_legacy, 16))
452 return "LUT C C";
453 if (!memcmp(coef, coef_lut_d_y_legacy, 16))
454 return "LUT D Y";
455 if (!memcmp(coef, coef_lut_d_c_legacy, 16))
456 return "LUT D C";
457 if (!memcmp(coef, coef_lut_e_y_legacy, 16))
458 return "LUT E Y";
459 if (!memcmp(coef, coef_lut_e_c_legacy, 16))
460 return "LUT E C";
461 if (!memcmp(coef, coef_lut_f_y_legacy, 16))
462 return "LUT F Y";
463 if (!memcmp(coef, coef_lut_f_c_legacy, 16))
464 return "LUT F C";
465 return "<UNKNOWN>";
466}
467
468static void hqvdp_dbg_dump_cmd(struct seq_file *s, struct sti_hqvdp_cmd *c)
469{
470 int src_w, src_h, dst_w, dst_h;
471
472 seq_puts(s, "\n\tTOP:");
473 seq_printf(s, "\n\t %-20s 0x%08X", "Config", c->top.config);
474 switch (c->top.config) {
475 case TOP_CONFIG_PROGRESSIVE:
476 seq_puts(s, "\tProgressive");
477 break;
478 case TOP_CONFIG_INTER_TOP:
479 seq_puts(s, "\tInterlaced, top field");
480 break;
481 case TOP_CONFIG_INTER_BTM:
482 seq_puts(s, "\tInterlaced, bottom field");
483 break;
484 default:
485 seq_puts(s, "\t<UNKNOWN>");
486 break;
487 }
488
489 seq_printf(s, "\n\t %-20s 0x%08X", "MemFormat", c->top.mem_format);
490 seq_printf(s, "\n\t %-20s 0x%08X", "CurrentY", c->top.current_luma);
491 seq_printf(s, "\n\t %-20s 0x%08X", "CurrentC", c->top.current_chroma);
492 seq_printf(s, "\n\t %-20s 0x%08X", "YSrcPitch", c->top.luma_src_pitch);
493 seq_printf(s, "\n\t %-20s 0x%08X", "CSrcPitch",
494 c->top.chroma_src_pitch);
495 seq_printf(s, "\n\t %-20s 0x%08X", "InputFrameSize",
496 c->top.input_frame_size);
497 seq_printf(s, "\t%dx%d",
498 c->top.input_frame_size & 0x0000FFFF,
499 c->top.input_frame_size >> 16);
500 seq_printf(s, "\n\t %-20s 0x%08X", "InputViewportSize",
501 c->top.input_viewport_size);
502 src_w = c->top.input_viewport_size & 0x0000FFFF;
503 src_h = c->top.input_viewport_size >> 16;
504 seq_printf(s, "\t%dx%d", src_w, src_h);
505
506 seq_puts(s, "\n\tHVSRC:");
507 seq_printf(s, "\n\t %-20s 0x%08X", "OutputPictureSize",
508 c->hvsrc.output_picture_size);
509 dst_w = c->hvsrc.output_picture_size & 0x0000FFFF;
510 dst_h = c->hvsrc.output_picture_size >> 16;
511 seq_printf(s, "\t%dx%d", dst_w, dst_h);
512 seq_printf(s, "\n\t %-20s 0x%08X", "ParamCtrl", c->hvsrc.param_ctrl);
513
514 seq_printf(s, "\n\t %-20s %s", "yh_coef",
515 hqvdp_dbg_get_lut(c->hvsrc.yh_coef));
516 seq_printf(s, "\n\t %-20s %s", "ch_coef",
517 hqvdp_dbg_get_lut(c->hvsrc.ch_coef));
518 seq_printf(s, "\n\t %-20s %s", "yv_coef",
519 hqvdp_dbg_get_lut(c->hvsrc.yv_coef));
520 seq_printf(s, "\n\t %-20s %s", "cv_coef",
521 hqvdp_dbg_get_lut(c->hvsrc.cv_coef));
522
523 seq_printf(s, "\n\t %-20s", "ScaleH");
524 if (dst_w > src_w)
525 seq_printf(s, " %d/1", dst_w / src_w);
526 else
527 seq_printf(s, " 1/%d", src_w / dst_w);
528
529 seq_printf(s, "\n\t %-20s", "tScaleV");
530 if (dst_h > src_h)
531 seq_printf(s, " %d/1", dst_h / src_h);
532 else
533 seq_printf(s, " 1/%d", src_h / dst_h);
534
535 seq_puts(s, "\n\tCSDI:");
536 seq_printf(s, "\n\t %-20s 0x%08X\t", "Config", c->csdi.config);
537 switch (c->csdi.config) {
538 case CSDI_CONFIG_PROG:
539 seq_puts(s, "Bypass");
540 break;
541 case CSDI_CONFIG_INTER_DIR:
542 seq_puts(s, "Deinterlace, directional");
543 break;
544 default:
545 seq_puts(s, "<UNKNOWN>");
546 break;
547 }
548
549 seq_printf(s, "\n\t %-20s 0x%08X", "Config2", c->csdi.config2);
550 seq_printf(s, "\n\t %-20s 0x%08X", "DcdiConfig", c->csdi.dcdi_config);
551}
552
553static int hqvdp_dbg_show(struct seq_file *s, void *data)
554{
555 struct drm_info_node *node = s->private;
556 struct sti_hqvdp *hqvdp = (struct sti_hqvdp *)node->info_ent->data;
557 struct drm_device *dev = node->minor->dev;
558 int cmd, cmd_offset, infoxp70;
559 void *virt;
560 int ret;
561
562 ret = mutex_lock_interruptible(&dev->struct_mutex);
563 if (ret)
564 return ret;
565
566 seq_printf(s, "%s: (vaddr = 0x%p)",
567 sti_plane_to_str(&hqvdp->plane), hqvdp->regs);
568
569 DBGFS_DUMP(HQVDP_MBX_IRQ_TO_XP70);
570 DBGFS_DUMP(HQVDP_MBX_INFO_HOST);
571 DBGFS_DUMP(HQVDP_MBX_IRQ_TO_HOST);
572 DBGFS_DUMP(HQVDP_MBX_INFO_XP70);
573 infoxp70 = readl(hqvdp->regs + HQVDP_MBX_INFO_XP70);
574 seq_puts(s, "\tFirmware state: ");
575 if (infoxp70 & INFO_XP70_FW_READY)
576 seq_puts(s, "idle and ready");
577 else if (infoxp70 & INFO_XP70_FW_PROCESSING)
578 seq_puts(s, "processing a picture");
579 else if (infoxp70 & INFO_XP70_FW_INITQUEUES)
580 seq_puts(s, "programming queues");
581 else
582 seq_puts(s, "NOT READY");
583
584 DBGFS_DUMP(HQVDP_MBX_SW_RESET_CTRL);
585 DBGFS_DUMP(HQVDP_MBX_STARTUP_CTRL1);
586 if (readl(hqvdp->regs + HQVDP_MBX_STARTUP_CTRL1)
587 & STARTUP_CTRL1_RST_DONE)
588 seq_puts(s, "\tReset is done");
589 else
590 seq_puts(s, "\tReset is NOT done");
591 DBGFS_DUMP(HQVDP_MBX_STARTUP_CTRL2);
592 if (readl(hqvdp->regs + HQVDP_MBX_STARTUP_CTRL2)
593 & STARTUP_CTRL2_FETCH_EN)
594 seq_puts(s, "\tFetch is enabled");
595 else
596 seq_puts(s, "\tFetch is NOT enabled");
597 DBGFS_DUMP(HQVDP_MBX_GP_STATUS);
598 DBGFS_DUMP(HQVDP_MBX_NEXT_CMD);
599 DBGFS_DUMP(HQVDP_MBX_CURRENT_CMD);
600 DBGFS_DUMP(HQVDP_MBX_SOFT_VSYNC);
601 if (!(readl(hqvdp->regs + HQVDP_MBX_SOFT_VSYNC) & 3))
602 seq_puts(s, "\tHW Vsync");
603 else
604 seq_puts(s, "\tSW Vsync ?!?!");
605
606 /* Last command */
607 cmd = readl(hqvdp->regs + HQVDP_MBX_CURRENT_CMD);
608 cmd_offset = sti_hqvdp_get_curr_cmd(hqvdp);
609 if (cmd_offset == -1) {
610 seq_puts(s, "\n\n Last command: unknown");
611 } else {
612 virt = hqvdp->hqvdp_cmd + cmd_offset;
613 seq_printf(s, "\n\n Last command: address @ 0x%x (0x%p)",
614 cmd, virt);
615 hqvdp_dbg_dump_cmd(s, (struct sti_hqvdp_cmd *)virt);
616 }
617
618 /* Next command */
619 cmd = readl(hqvdp->regs + HQVDP_MBX_NEXT_CMD);
620 cmd_offset = sti_hqvdp_get_next_cmd(hqvdp);
621 if (cmd_offset == -1) {
622 seq_puts(s, "\n\n Next command: unknown");
623 } else {
624 virt = hqvdp->hqvdp_cmd + cmd_offset;
625 seq_printf(s, "\n\n Next command address: @ 0x%x (0x%p)",
626 cmd, virt);
627 hqvdp_dbg_dump_cmd(s, (struct sti_hqvdp_cmd *)virt);
628 }
629
630 seq_puts(s, "\n");
631
632 mutex_unlock(&dev->struct_mutex);
633 return 0;
634}
635
636static struct drm_info_list hqvdp_debugfs_files[] = {
637 { "hqvdp", hqvdp_dbg_show, 0, NULL },
638};
639
640static int hqvdp_debugfs_init(struct sti_hqvdp *hqvdp, struct drm_minor *minor)
641{
642 unsigned int i;
643
644 for (i = 0; i < ARRAY_SIZE(hqvdp_debugfs_files); i++)
645 hqvdp_debugfs_files[i].data = hqvdp;
646
647 return drm_debugfs_create_files(hqvdp_debugfs_files,
648 ARRAY_SIZE(hqvdp_debugfs_files),
649 minor->debugfs_root, minor);
650}
651
4fdbc678
BG
652/**
653 * sti_hqvdp_update_hvsrc
654 * @orient: horizontal or vertical
655 * @scale: scaling/zoom factor
656 * @hvsrc: the structure containing the LUT coef
657 *
658 * Update the Y and C Lut coef, as well as the shift param
659 *
660 * RETURNS:
661 * None.
662 */
663static void sti_hqvdp_update_hvsrc(enum sti_hvsrc_orient orient, int scale,
664 struct sti_hqvdp_hvsrc *hvsrc)
665{
666 const int *coef_c, *coef_y;
667 int shift_c, shift_y;
668
669 /* Get the appropriate coef tables */
670 if (scale < SCALE_MAX_FOR_LEG_LUT_F) {
671 coef_y = coef_lut_f_y_legacy;
672 coef_c = coef_lut_f_c_legacy;
673 shift_y = SHIFT_LUT_F_Y_LEGACY;
674 shift_c = SHIFT_LUT_F_C_LEGACY;
675 } else if (scale < SCALE_MAX_FOR_LEG_LUT_E) {
676 coef_y = coef_lut_e_y_legacy;
677 coef_c = coef_lut_e_c_legacy;
678 shift_y = SHIFT_LUT_E_Y_LEGACY;
679 shift_c = SHIFT_LUT_E_C_LEGACY;
680 } else if (scale < SCALE_MAX_FOR_LEG_LUT_D) {
681 coef_y = coef_lut_d_y_legacy;
682 coef_c = coef_lut_d_c_legacy;
683 shift_y = SHIFT_LUT_D_Y_LEGACY;
684 shift_c = SHIFT_LUT_D_C_LEGACY;
685 } else if (scale < SCALE_MAX_FOR_LEG_LUT_C) {
686 coef_y = coef_lut_c_y_legacy;
687 coef_c = coef_lut_c_c_legacy;
688 shift_y = SHIFT_LUT_C_Y_LEGACY;
689 shift_c = SHIFT_LUT_C_C_LEGACY;
690 } else if (scale == SCALE_MAX_FOR_LEG_LUT_C) {
691 coef_y = coef_c = coef_lut_b;
692 shift_y = shift_c = SHIFT_LUT_B;
693 } else {
694 coef_y = coef_c = coef_lut_a_legacy;
695 shift_y = shift_c = SHIFT_LUT_A_LEGACY;
696 }
697
698 if (orient == HVSRC_HORI) {
699 hvsrc->hori_shift = (shift_c << 16) | shift_y;
700 memcpy(hvsrc->yh_coef, coef_y, sizeof(hvsrc->yh_coef));
701 memcpy(hvsrc->ch_coef, coef_c, sizeof(hvsrc->ch_coef));
702 } else {
703 hvsrc->vert_shift = (shift_c << 16) | shift_y;
704 memcpy(hvsrc->yv_coef, coef_y, sizeof(hvsrc->yv_coef));
705 memcpy(hvsrc->cv_coef, coef_c, sizeof(hvsrc->cv_coef));
706 }
707}
708
709/**
710 * sti_hqvdp_check_hw_scaling
29d1dc62
VA
711 * @hqvdp: hqvdp pointer
712 * @mode: display mode with timing constraints
713 * @src_w: source width
714 * @src_h: source height
715 * @dst_w: destination width
716 * @dst_h: destination height
4fdbc678
BG
717 *
718 * Check if the HW is able to perform the scaling request
719 * The firmware scaling limitation is "CEIL(1/Zy) <= FLOOR(LFW)" where:
720 * Zy = OutputHeight / InputHeight
721 * LFW = (Tx * IPClock) / (MaxNbCycles * Cp)
722 * Tx : Total video mode horizontal resolution
723 * IPClock : HQVDP IP clock (Mhz)
724 * MaxNbCycles: max(InputWidth, OutputWidth)
725 * Cp: Video mode pixel clock (Mhz)
726 *
727 * RETURNS:
728 * True if the HW can scale.
729 */
29d1dc62
VA
730static bool sti_hqvdp_check_hw_scaling(struct sti_hqvdp *hqvdp,
731 struct drm_display_mode *mode,
732 int src_w, int src_h,
733 int dst_w, int dst_h)
4fdbc678 734{
4fdbc678
BG
735 unsigned long lfw;
736 unsigned int inv_zy;
737
29d1dc62
VA
738 lfw = mode->htotal * (clk_get_rate(hqvdp->clk) / 1000000);
739 lfw /= max(src_w, dst_w) * mode->clock / 1000;
4fdbc678 740
29d1dc62 741 inv_zy = DIV_ROUND_UP(src_h, dst_h);
4fdbc678
BG
742
743 return (inv_zy <= lfw) ? true : false;
744}
745
871bcdfe
VA
746/**
747 * sti_hqvdp_disable
29d1dc62 748 * @hqvdp: hqvdp pointer
871bcdfe
VA
749 *
750 * Disables the HQVDP plane
871bcdfe 751 */
29d1dc62 752static void sti_hqvdp_disable(struct sti_hqvdp *hqvdp)
4fdbc678 753{
4fdbc678
BG
754 int i;
755
29d1dc62 756 DRM_DEBUG_DRIVER("%s\n", sti_plane_to_str(&hqvdp->plane));
4fdbc678
BG
757
758 /* Unregister VTG Vsync callback */
29d1dc62 759 if (sti_vtg_unregister_client(hqvdp->vtg, &hqvdp->vtg_nb))
4fdbc678
BG
760 DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
761
762 /* Set next cmd to NULL */
763 writel(0, hqvdp->regs + HQVDP_MBX_NEXT_CMD);
764
765 for (i = 0; i < POLL_MAX_ATTEMPT; i++) {
766 if (readl(hqvdp->regs + HQVDP_MBX_INFO_XP70)
767 & INFO_XP70_FW_READY)
768 break;
769 msleep(POLL_DELAY_MS);
770 }
771
772 /* VTG can stop now */
773 clk_disable_unprepare(hqvdp->clk_pix_main);
774
29d1dc62 775 if (i == POLL_MAX_ATTEMPT)
4fdbc678 776 DRM_ERROR("XP70 could not revert to idle\n");
4fdbc678 777
29d1dc62 778 hqvdp->plane.status = STI_PLANE_DISABLED;
4fdbc678
BG
779}
780
781/**
782 * sti_vdp_vtg_cb
783 * @nb: notifier block
784 * @evt: event message
785 * @data: private data
786 *
787 * Handle VTG Vsync event, display pending bottom field
788 *
789 * RETURNS:
790 * 0 on success.
791 */
792int sti_hqvdp_vtg_cb(struct notifier_block *nb, unsigned long evt, void *data)
793{
794 struct sti_hqvdp *hqvdp = container_of(nb, struct sti_hqvdp, vtg_nb);
795 int btm_cmd_offset, top_cmd_offest;
796 struct sti_hqvdp_cmd *btm_cmd, *top_cmd;
797
798 if ((evt != VTG_TOP_FIELD_EVENT) && (evt != VTG_BOTTOM_FIELD_EVENT)) {
799 DRM_DEBUG_DRIVER("Unknown event\n");
800 return 0;
801 }
802
29d1dc62
VA
803 if (hqvdp->plane.status == STI_PLANE_FLUSHING) {
804 /* disable need to be synchronize on vsync event */
805 DRM_DEBUG_DRIVER("Vsync event received => disable %s\n",
806 sti_plane_to_str(&hqvdp->plane));
807
808 sti_hqvdp_disable(hqvdp);
809 }
810
4fdbc678
BG
811 if (hqvdp->btm_field_pending) {
812 /* Create the btm field command from the current one */
813 btm_cmd_offset = sti_hqvdp_get_free_cmd(hqvdp);
814 top_cmd_offest = sti_hqvdp_get_curr_cmd(hqvdp);
815 if ((btm_cmd_offset == -1) || (top_cmd_offest == -1)) {
e4250b3e 816 DRM_DEBUG_DRIVER("Warning: no cmd, will skip field\n");
4fdbc678
BG
817 return -EBUSY;
818 }
819
820 btm_cmd = hqvdp->hqvdp_cmd + btm_cmd_offset;
821 top_cmd = hqvdp->hqvdp_cmd + top_cmd_offest;
822
823 memcpy(btm_cmd, top_cmd, sizeof(*btm_cmd));
824
825 btm_cmd->top.config = TOP_CONFIG_INTER_BTM;
826 btm_cmd->top.current_luma +=
827 btm_cmd->top.luma_src_pitch / 2;
828 btm_cmd->top.current_chroma +=
829 btm_cmd->top.chroma_src_pitch / 2;
830
831 /* Post the command to mailbox */
832 writel(hqvdp->hqvdp_cmd_paddr + btm_cmd_offset,
833 hqvdp->regs + HQVDP_MBX_NEXT_CMD);
834
4fdbc678
BG
835 hqvdp->btm_field_pending = false;
836
837 dev_dbg(hqvdp->dev, "%s Posted command:0x%x\n",
838 __func__, hqvdp->hqvdp_cmd_paddr);
bf8f9e4a
VA
839
840 sti_plane_update_fps(&hqvdp->plane, false, true);
4fdbc678
BG
841 }
842
843 return 0;
844}
845
871bcdfe 846static void sti_hqvdp_init(struct sti_hqvdp *hqvdp)
4fdbc678 847{
4fdbc678
BG
848 int size;
849
4fdbc678
BG
850 hqvdp->vtg_nb.notifier_call = sti_hqvdp_vtg_cb;
851
852 /* Allocate memory for the VDP commands */
853 size = NB_VDP_CMD * sizeof(struct sti_hqvdp_cmd);
854 hqvdp->hqvdp_cmd = dma_alloc_writecombine(hqvdp->dev, size,
855 &hqvdp->hqvdp_cmd_paddr,
856 GFP_KERNEL | GFP_DMA);
857 if (!hqvdp->hqvdp_cmd) {
858 DRM_ERROR("Failed to allocate memory for VDP cmd\n");
859 return;
860 }
861
862 memset(hqvdp->hqvdp_cmd, 0, size);
863}
864
e00fe64a
VA
865static void sti_hqvdp_init_plugs(struct sti_hqvdp *hqvdp)
866{
867 /* Configure Plugs (same for RD & WR) */
868 writel(PLUG_PAGE_SIZE_256, hqvdp->regs + HQVDP_RD_PLUG_PAGE_SIZE);
869 writel(PLUG_MIN_OPC_8, hqvdp->regs + HQVDP_RD_PLUG_MIN_OPC);
870 writel(PLUG_MAX_OPC_64, hqvdp->regs + HQVDP_RD_PLUG_MAX_OPC);
871 writel(PLUG_MAX_CHK_2X, hqvdp->regs + HQVDP_RD_PLUG_MAX_CHK);
872 writel(PLUG_MAX_MSG_1X, hqvdp->regs + HQVDP_RD_PLUG_MAX_MSG);
873 writel(PLUG_MIN_SPACE_1, hqvdp->regs + HQVDP_RD_PLUG_MIN_SPACE);
874 writel(PLUG_CONTROL_ENABLE, hqvdp->regs + HQVDP_RD_PLUG_CONTROL);
875
876 writel(PLUG_PAGE_SIZE_256, hqvdp->regs + HQVDP_WR_PLUG_PAGE_SIZE);
877 writel(PLUG_MIN_OPC_8, hqvdp->regs + HQVDP_WR_PLUG_MIN_OPC);
878 writel(PLUG_MAX_OPC_64, hqvdp->regs + HQVDP_WR_PLUG_MAX_OPC);
879 writel(PLUG_MAX_CHK_2X, hqvdp->regs + HQVDP_WR_PLUG_MAX_CHK);
880 writel(PLUG_MAX_MSG_1X, hqvdp->regs + HQVDP_WR_PLUG_MAX_MSG);
881 writel(PLUG_MIN_SPACE_1, hqvdp->regs + HQVDP_WR_PLUG_MIN_SPACE);
882 writel(PLUG_CONTROL_ENABLE, hqvdp->regs + HQVDP_WR_PLUG_CONTROL);
883}
884
885/**
886 * sti_hqvdp_start_xp70
887 * @hqvdp: hqvdp pointer
888 *
889 * Run the xP70 initialization sequence
890 */
891static void sti_hqvdp_start_xp70(struct sti_hqvdp *hqvdp)
892{
893 const struct firmware *firmware;
894 u32 *fw_rd_plug, *fw_wr_plug, *fw_pmem, *fw_dmem;
895 u8 *data;
896 int i;
897 struct fw_header {
898 int rd_size;
899 int wr_size;
900 int pmem_size;
901 int dmem_size;
902 } *header;
903
904 DRM_DEBUG_DRIVER("\n");
905
906 if (hqvdp->xp70_initialized) {
dd86dc2f 907 DRM_DEBUG_DRIVER("HQVDP XP70 already initialized\n");
e00fe64a
VA
908 return;
909 }
910
911 /* Request firmware */
912 if (request_firmware(&firmware, HQVDP_FMW_NAME, hqvdp->dev)) {
913 DRM_ERROR("Can't get HQVDP firmware\n");
914 return;
915 }
916
917 /* Check firmware parts */
918 if (!firmware) {
919 DRM_ERROR("Firmware not available\n");
920 return;
921 }
922
923 header = (struct fw_header *)firmware->data;
924 if (firmware->size < sizeof(*header)) {
925 DRM_ERROR("Invalid firmware size (%d)\n", firmware->size);
926 goto out;
927 }
928 if ((sizeof(*header) + header->rd_size + header->wr_size +
929 header->pmem_size + header->dmem_size) != firmware->size) {
930 DRM_ERROR("Invalid fmw structure (%d+%d+%d+%d+%d != %d)\n",
931 sizeof(*header), header->rd_size, header->wr_size,
932 header->pmem_size, header->dmem_size,
933 firmware->size);
934 goto out;
935 }
936
937 data = (u8 *)firmware->data;
938 data += sizeof(*header);
939 fw_rd_plug = (void *)data;
940 data += header->rd_size;
941 fw_wr_plug = (void *)data;
942 data += header->wr_size;
943 fw_pmem = (void *)data;
944 data += header->pmem_size;
945 fw_dmem = (void *)data;
946
947 /* Enable clock */
948 if (clk_prepare_enable(hqvdp->clk))
949 DRM_ERROR("Failed to prepare/enable HQVDP clk\n");
950
951 /* Reset */
952 writel(SW_RESET_CTRL_FULL, hqvdp->regs + HQVDP_MBX_SW_RESET_CTRL);
953
954 for (i = 0; i < POLL_MAX_ATTEMPT; i++) {
955 if (readl(hqvdp->regs + HQVDP_MBX_STARTUP_CTRL1)
956 & STARTUP_CTRL1_RST_DONE)
957 break;
958 msleep(POLL_DELAY_MS);
959 }
960 if (i == POLL_MAX_ATTEMPT) {
961 DRM_ERROR("Could not reset\n");
962 goto out;
963 }
964
965 /* Init Read & Write plugs */
966 for (i = 0; i < header->rd_size / 4; i++)
967 writel(fw_rd_plug[i], hqvdp->regs + HQVDP_RD_PLUG + i * 4);
968 for (i = 0; i < header->wr_size / 4; i++)
969 writel(fw_wr_plug[i], hqvdp->regs + HQVDP_WR_PLUG + i * 4);
970
971 sti_hqvdp_init_plugs(hqvdp);
972
973 /* Authorize Idle Mode */
974 writel(STARTUP_CTRL1_AUTH_IDLE, hqvdp->regs + HQVDP_MBX_STARTUP_CTRL1);
975
976 /* Prevent VTG interruption during the boot */
977 writel(SOFT_VSYNC_SW_CTRL_IRQ, hqvdp->regs + HQVDP_MBX_SOFT_VSYNC);
978 writel(0, hqvdp->regs + HQVDP_MBX_NEXT_CMD);
979
980 /* Download PMEM & DMEM */
981 for (i = 0; i < header->pmem_size / 4; i++)
982 writel(fw_pmem[i], hqvdp->regs + HQVDP_PMEM + i * 4);
983 for (i = 0; i < header->dmem_size / 4; i++)
984 writel(fw_dmem[i], hqvdp->regs + HQVDP_DMEM + i * 4);
985
986 /* Enable fetch */
987 writel(STARTUP_CTRL2_FETCH_EN, hqvdp->regs + HQVDP_MBX_STARTUP_CTRL2);
988
989 /* Wait end of boot */
990 for (i = 0; i < POLL_MAX_ATTEMPT; i++) {
991 if (readl(hqvdp->regs + HQVDP_MBX_INFO_XP70)
992 & INFO_XP70_FW_READY)
993 break;
994 msleep(POLL_DELAY_MS);
995 }
996 if (i == POLL_MAX_ATTEMPT) {
997 DRM_ERROR("Could not boot\n");
998 goto out;
999 }
1000
1001 /* Launch Vsync */
1002 writel(SOFT_VSYNC_HW, hqvdp->regs + HQVDP_MBX_SOFT_VSYNC);
1003
1004 DRM_INFO("HQVDP XP70 initialized\n");
1005
1006 hqvdp->xp70_initialized = true;
1007
1008out:
1009 release_firmware(firmware);
1010}
1011
dd86dc2f
VA
1012static int sti_hqvdp_atomic_check(struct drm_plane *drm_plane,
1013 struct drm_plane_state *state)
29d1dc62 1014{
29d1dc62
VA
1015 struct sti_plane *plane = to_sti_plane(drm_plane);
1016 struct sti_hqvdp *hqvdp = to_sti_hqvdp(plane);
1017 struct drm_crtc *crtc = state->crtc;
29d1dc62 1018 struct drm_framebuffer *fb = state->fb;
29d1dc62 1019 bool first_prepare = plane->status == STI_PLANE_DISABLED ? true : false;
dd86dc2f
VA
1020 struct drm_crtc_state *crtc_state;
1021 struct drm_display_mode *mode;
1022 int dst_x, dst_y, dst_w, dst_h;
1023 int src_x, src_y, src_w, src_h;
1024
1025 /* no need for further checks if the plane is being disabled */
1026 if (!crtc || !fb)
1027 return 0;
1028
1029 crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
1030 mode = &crtc_state->mode;
1031 dst_x = state->crtc_x;
1032 dst_y = state->crtc_y;
1033 dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x);
1034 dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y);
1035 /* src_x are in 16.16 format */
1036 src_x = state->src_x >> 16;
1037 src_y = state->src_y >> 16;
1038 src_w = state->src_w >> 16;
1039 src_h = state->src_h >> 16;
1040
1041 if (!sti_hqvdp_check_hw_scaling(hqvdp, mode,
1042 src_w, src_h,
1043 dst_w, dst_h)) {
1044 DRM_ERROR("Scaling beyond HW capabilities\n");
1045 return -EINVAL;
1046 }
1047
1048 if (!drm_fb_cma_get_gem_obj(fb, 0)) {
1049 DRM_ERROR("Can't get CMA GEM object for fb\n");
1050 return -EINVAL;
1051 }
1052
1053 /*
1054 * Input / output size
1055 * Align to upper even value
1056 */
1057 dst_w = ALIGN(dst_w, 2);
1058 dst_h = ALIGN(dst_h, 2);
1059
1060 if ((src_w > MAX_WIDTH) || (src_w < MIN_WIDTH) ||
1061 (src_h > MAX_HEIGHT) || (src_h < MIN_HEIGHT) ||
1062 (dst_w > MAX_WIDTH) || (dst_w < MIN_WIDTH) ||
1063 (dst_h > MAX_HEIGHT) || (dst_h < MIN_HEIGHT)) {
1064 DRM_ERROR("Invalid in/out size %dx%d -> %dx%d\n",
1065 src_w, src_h,
1066 dst_w, dst_h);
1067 return -EINVAL;
1068 }
1069
1070 if (first_prepare) {
1071 /* Start HQVDP XP70 coprocessor */
1072 sti_hqvdp_start_xp70(hqvdp);
1073
1074 /* Prevent VTG shutdown */
1075 if (clk_prepare_enable(hqvdp->clk_pix_main)) {
1076 DRM_ERROR("Failed to prepare/enable pix main clk\n");
1077 return -EINVAL;
1078 }
1079
1080 /* Register VTG Vsync callback to handle bottom fields */
1081 if (sti_vtg_register_client(hqvdp->vtg,
1082 &hqvdp->vtg_nb,
1083 crtc)) {
1084 DRM_ERROR("Cannot register VTG notifier\n");
1085 return -EINVAL;
1086 }
1087 }
29d1dc62
VA
1088
1089 DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n",
dd86dc2f 1090 crtc->base.id, sti_mixer_to_str(to_sti_mixer(crtc)),
29d1dc62
VA
1091 drm_plane->base.id, sti_plane_to_str(plane));
1092 DRM_DEBUG_KMS("%s dst=(%dx%d)@(%d,%d) - src=(%dx%d)@(%d,%d)\n",
1093 sti_plane_to_str(plane),
1094 dst_w, dst_h, dst_x, dst_y,
1095 src_w, src_h, src_x, src_y);
1096
dd86dc2f
VA
1097 return 0;
1098}
1099
1100static void sti_hqvdp_atomic_update(struct drm_plane *drm_plane,
1101 struct drm_plane_state *oldstate)
1102{
1103 struct drm_plane_state *state = drm_plane->state;
1104 struct sti_plane *plane = to_sti_plane(drm_plane);
1105 struct sti_hqvdp *hqvdp = to_sti_hqvdp(plane);
1106 struct drm_crtc *crtc = state->crtc;
1107 struct drm_framebuffer *fb = state->fb;
1108 struct drm_display_mode *mode;
1109 int dst_x, dst_y, dst_w, dst_h;
1110 int src_x, src_y, src_w, src_h;
1111 struct drm_gem_cma_object *cma_obj;
1112 struct sti_hqvdp_cmd *cmd;
1113 int scale_h, scale_v;
1114 int cmd_offset;
1115
1116 if (!crtc || !fb)
1117 return;
1118
1119 mode = &crtc->mode;
1120 dst_x = state->crtc_x;
1121 dst_y = state->crtc_y;
1122 dst_w = clamp_val(state->crtc_w, 0, mode->crtc_hdisplay - dst_x);
1123 dst_h = clamp_val(state->crtc_h, 0, mode->crtc_vdisplay - dst_y);
1124 /* src_x are in 16.16 format */
1125 src_x = state->src_x >> 16;
1126 src_y = state->src_y >> 16;
1127 src_w = state->src_w >> 16;
1128 src_h = state->src_h >> 16;
1129
29d1dc62
VA
1130 cmd_offset = sti_hqvdp_get_free_cmd(hqvdp);
1131 if (cmd_offset == -1) {
e4250b3e 1132 DRM_DEBUG_DRIVER("Warning: no cmd, will skip frame\n");
29d1dc62
VA
1133 return;
1134 }
1135 cmd = hqvdp->hqvdp_cmd + cmd_offset;
1136
29d1dc62
VA
1137 /* Static parameters, defaulting to progressive mode */
1138 cmd->top.config = TOP_CONFIG_PROGRESSIVE;
1139 cmd->top.mem_format = TOP_MEM_FORMAT_DFLT;
1140 cmd->hvsrc.param_ctrl = HVSRC_PARAM_CTRL_DFLT;
1141 cmd->csdi.config = CSDI_CONFIG_PROG;
1142
1143 /* VC1RE, FMD bypassed : keep everything set to 0
1144 * IQI/P2I bypassed */
1145 cmd->iqi.config = IQI_CONFIG_DFLT;
1146 cmd->iqi.con_bri = IQI_CON_BRI_DFLT;
1147 cmd->iqi.sat_gain = IQI_SAT_GAIN_DFLT;
1148 cmd->iqi.pxf_conf = IQI_PXF_CONF_DFLT;
1149
1150 cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
29d1dc62
VA
1151
1152 DRM_DEBUG_DRIVER("drm FB:%d format:%.4s phys@:0x%lx\n", fb->base.id,
1153 (char *)&fb->pixel_format,
1154 (unsigned long)cma_obj->paddr);
1155
1156 /* Buffer planes address */
1157 cmd->top.current_luma = (u32)cma_obj->paddr + fb->offsets[0];
1158 cmd->top.current_chroma = (u32)cma_obj->paddr + fb->offsets[1];
1159
1160 /* Pitches */
1161 cmd->top.luma_processed_pitch = fb->pitches[0];
1162 cmd->top.luma_src_pitch = fb->pitches[0];
1163 cmd->top.chroma_processed_pitch = fb->pitches[1];
1164 cmd->top.chroma_src_pitch = fb->pitches[1];
1165
1166 /* Input / output size
1167 * Align to upper even value */
1168 dst_w = ALIGN(dst_w, 2);
1169 dst_h = ALIGN(dst_h, 2);
1170
29d1dc62
VA
1171 cmd->top.input_viewport_size = src_h << 16 | src_w;
1172 cmd->top.input_frame_size = src_h << 16 | src_w;
1173 cmd->hvsrc.output_picture_size = dst_h << 16 | dst_w;
1174 cmd->top.input_viewport_ori = src_y << 16 | src_x;
1175
1176 /* Handle interlaced */
1177 if (fb->flags & DRM_MODE_FB_INTERLACED) {
1178 /* Top field to display */
1179 cmd->top.config = TOP_CONFIG_INTER_TOP;
1180
1181 /* Update pitches and vert size */
1182 cmd->top.input_frame_size = (src_h / 2) << 16 | src_w;
1183 cmd->top.luma_processed_pitch *= 2;
1184 cmd->top.luma_src_pitch *= 2;
1185 cmd->top.chroma_processed_pitch *= 2;
1186 cmd->top.chroma_src_pitch *= 2;
1187
1188 /* Enable directional deinterlacing processing */
1189 cmd->csdi.config = CSDI_CONFIG_INTER_DIR;
1190 cmd->csdi.config2 = CSDI_CONFIG2_DFLT;
1191 cmd->csdi.dcdi_config = CSDI_DCDI_CONFIG_DFLT;
1192 }
1193
1194 /* Update hvsrc lut coef */
1195 scale_h = SCALE_FACTOR * dst_w / src_w;
1196 sti_hqvdp_update_hvsrc(HVSRC_HORI, scale_h, &cmd->hvsrc);
1197
1198 scale_v = SCALE_FACTOR * dst_h / src_h;
1199 sti_hqvdp_update_hvsrc(HVSRC_VERT, scale_v, &cmd->hvsrc);
1200
29d1dc62
VA
1201 writel(hqvdp->hqvdp_cmd_paddr + cmd_offset,
1202 hqvdp->regs + HQVDP_MBX_NEXT_CMD);
1203
29d1dc62
VA
1204 /* Interlaced : get ready to display the bottom field at next Vsync */
1205 if (fb->flags & DRM_MODE_FB_INTERLACED)
1206 hqvdp->btm_field_pending = true;
1207
1208 dev_dbg(hqvdp->dev, "%s Posted command:0x%x\n",
1209 __func__, hqvdp->hqvdp_cmd_paddr + cmd_offset);
1210
bf8f9e4a
VA
1211 sti_plane_update_fps(plane, true, true);
1212
29d1dc62
VA
1213 plane->status = STI_PLANE_UPDATED;
1214}
1215
1216static void sti_hqvdp_atomic_disable(struct drm_plane *drm_plane,
1217 struct drm_plane_state *oldstate)
1218{
1219 struct sti_plane *plane = to_sti_plane(drm_plane);
29d1dc62
VA
1220
1221 if (!drm_plane->crtc) {
1222 DRM_DEBUG_DRIVER("drm plane:%d not enabled\n",
1223 drm_plane->base.id);
1224 return;
1225 }
1226
1227 DRM_DEBUG_DRIVER("CRTC:%d (%s) drm plane:%d (%s)\n",
dd86dc2f
VA
1228 drm_plane->crtc->base.id,
1229 sti_mixer_to_str(to_sti_mixer(drm_plane->crtc)),
29d1dc62
VA
1230 drm_plane->base.id, sti_plane_to_str(plane));
1231
1232 plane->status = STI_PLANE_DISABLING;
1233}
1234
1235static const struct drm_plane_helper_funcs sti_hqvdp_helpers_funcs = {
dd86dc2f 1236 .atomic_check = sti_hqvdp_atomic_check,
29d1dc62
VA
1237 .atomic_update = sti_hqvdp_atomic_update,
1238 .atomic_disable = sti_hqvdp_atomic_disable,
4fdbc678
BG
1239};
1240
29d1dc62
VA
1241static struct drm_plane *sti_hqvdp_create(struct drm_device *drm_dev,
1242 struct device *dev, int desc)
4fdbc678
BG
1243{
1244 struct sti_hqvdp *hqvdp = dev_get_drvdata(dev);
29d1dc62 1245 int res;
4fdbc678 1246
871bcdfe 1247 hqvdp->plane.desc = desc;
29d1dc62 1248 hqvdp->plane.status = STI_PLANE_DISABLED;
871bcdfe
VA
1249
1250 sti_hqvdp_init(hqvdp);
4fdbc678 1251
29d1dc62
VA
1252 res = drm_universal_plane_init(drm_dev, &hqvdp->plane.drm_plane, 1,
1253 &sti_plane_helpers_funcs,
1254 hqvdp_supported_formats,
1255 ARRAY_SIZE(hqvdp_supported_formats),
b0b3b795 1256 DRM_PLANE_TYPE_OVERLAY, NULL);
29d1dc62
VA
1257 if (res) {
1258 DRM_ERROR("Failed to initialize universal plane\n");
1259 return NULL;
1260 }
1261
1262 drm_plane_helper_add(&hqvdp->plane.drm_plane, &sti_hqvdp_helpers_funcs);
1263
1264 sti_plane_init_property(&hqvdp->plane, DRM_PLANE_TYPE_OVERLAY);
1265
670454bb
VA
1266 if (hqvdp_debugfs_init(hqvdp, drm_dev->primary))
1267 DRM_ERROR("HQVDP debugfs setup failed\n");
1268
29d1dc62 1269 return &hqvdp->plane.drm_plane;
4fdbc678
BG
1270}
1271
4fdbc678
BG
1272int sti_hqvdp_bind(struct device *dev, struct device *master, void *data)
1273{
1274 struct sti_hqvdp *hqvdp = dev_get_drvdata(dev);
1275 struct drm_device *drm_dev = data;
29d1dc62 1276 struct drm_plane *plane;
4fdbc678
BG
1277
1278 DRM_DEBUG_DRIVER("\n");
1279
1280 hqvdp->drm_dev = drm_dev;
1281
871bcdfe 1282 /* Create HQVDP plane once xp70 is initialized */
29d1dc62
VA
1283 plane = sti_hqvdp_create(drm_dev, hqvdp->dev, STI_HQVDP_0);
1284 if (!plane)
4fdbc678 1285 DRM_ERROR("Can't create HQVDP plane\n");
4fdbc678
BG
1286
1287 return 0;
1288}
1289
1290static void sti_hqvdp_unbind(struct device *dev,
1291 struct device *master, void *data)
1292{
1293 /* do nothing */
1294}
1295
1296static const struct component_ops sti_hqvdp_ops = {
1297 .bind = sti_hqvdp_bind,
1298 .unbind = sti_hqvdp_unbind,
1299};
1300
1301static int sti_hqvdp_probe(struct platform_device *pdev)
1302{
1303 struct device *dev = &pdev->dev;
1304 struct device_node *vtg_np;
1305 struct sti_hqvdp *hqvdp;
1306 struct resource *res;
1307
1308 DRM_DEBUG_DRIVER("\n");
1309
1310 hqvdp = devm_kzalloc(dev, sizeof(*hqvdp), GFP_KERNEL);
1311 if (!hqvdp) {
1312 DRM_ERROR("Failed to allocate HQVDP context\n");
1313 return -ENOMEM;
1314 }
1315
1316 hqvdp->dev = dev;
1317
1318 /* Get Memory resources */
1319 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1320 if (res == NULL) {
1321 DRM_ERROR("Get memory resource failed\n");
1322 return -ENXIO;
1323 }
1324 hqvdp->regs = devm_ioremap(dev, res->start, resource_size(res));
1325 if (hqvdp->regs == NULL) {
1326 DRM_ERROR("Register mapping failed\n");
1327 return -ENXIO;
1328 }
1329
1330 /* Get clock resources */
1331 hqvdp->clk = devm_clk_get(dev, "hqvdp");
1332 hqvdp->clk_pix_main = devm_clk_get(dev, "pix_main");
6dfca6b3 1333 if (IS_ERR(hqvdp->clk) || IS_ERR(hqvdp->clk_pix_main)) {
4fdbc678
BG
1334 DRM_ERROR("Cannot get clocks\n");
1335 return -ENXIO;
1336 }
1337
1338 /* Get reset resources */
1339 hqvdp->reset = devm_reset_control_get(dev, "hqvdp");
1340 if (!IS_ERR(hqvdp->reset))
1341 reset_control_deassert(hqvdp->reset);
1342
1343 vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 0);
1344 if (vtg_np)
1345 hqvdp->vtg = of_vtg_find(vtg_np);
1346
1347 platform_set_drvdata(pdev, hqvdp);
1348
1349 return component_add(&pdev->dev, &sti_hqvdp_ops);
1350}
1351
1352static int sti_hqvdp_remove(struct platform_device *pdev)
1353{
1354 component_del(&pdev->dev, &sti_hqvdp_ops);
1355 return 0;
1356}
1357
1358static struct of_device_id hqvdp_of_match[] = {
1359 { .compatible = "st,stih407-hqvdp", },
1360 { /* end node */ }
1361};
1362MODULE_DEVICE_TABLE(of, hqvdp_of_match);
1363
1364struct platform_driver sti_hqvdp_driver = {
1365 .driver = {
1366 .name = "sti-hqvdp",
1367 .owner = THIS_MODULE,
1368 .of_match_table = hqvdp_of_match,
1369 },
1370 .probe = sti_hqvdp_probe,
1371 .remove = sti_hqvdp_remove,
1372};
1373
4fdbc678
BG
1374MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
1375MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
1376MODULE_LICENSE("GPL");
This page took 0.173107 seconds and 5 git commands to generate.