drm/rockchip: vop: add line_flag 0/1 for ddr freq change
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_vop.h
1 /*
2  * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3  * Author:Mark Yao <mark.yao@rock-chips.com>
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #ifndef _ROCKCHIP_DRM_VOP_H
16 #define _ROCKCHIP_DRM_VOP_H
17
18 /*
19  * major: IP major vertion, used for IP structure
20  * minor: big feature change under same structure
21  */
22 #define VOP_VERSION(major, minor)       ((major) << 8 | (minor))
23 #define VOP_MAJOR(version)      ((version) >> 8)
24 #define VOP_MINOR(version)      ((version) & 0xff)
25
26 #define AFBDC_FMT_RGB565        0x0
27 #define AFBDC_FMT_U8U8U8U8      0x5
28 #define AFBDC_FMT_U8U8U8        0x4
29
30 enum vop_csc_format {
31         CSC_BT601,
32         CSC_BT709,
33         CSC_BT2020,
34 };
35
36 enum vop_csc_mode {
37         CSC_RGB,
38         CSC_YUV,
39 };
40
41 enum vop_data_format {
42         VOP_FMT_ARGB8888 = 0,
43         VOP_FMT_RGB888,
44         VOP_FMT_RGB565,
45         VOP_FMT_YUV420SP = 4,
46         VOP_FMT_YUV422SP,
47         VOP_FMT_YUV444SP,
48 };
49
50 struct vop_reg_data {
51         uint32_t offset;
52         uint32_t value;
53 };
54
55 struct vop_reg {
56         uint32_t mask;
57         uint32_t offset:12;
58         uint32_t shift:5;
59         uint32_t begin_minor:4;
60         uint32_t end_minor:4;
61         uint32_t major:3;
62         uint32_t write_mask:1;
63 };
64
65 struct vop_csc {
66         struct vop_reg y2r_en;
67         struct vop_reg r2r_en;
68         struct vop_reg r2y_en;
69
70         uint32_t y2r_offset;
71         uint32_t r2r_offset;
72         uint32_t r2y_offset;
73 };
74
75 struct vop_ctrl {
76         struct vop_reg standby;
77         struct vop_reg htotal_pw;
78         struct vop_reg hact_st_end;
79         struct vop_reg vtotal_pw;
80         struct vop_reg vact_st_end;
81         struct vop_reg vact_st_end_f1;
82         struct vop_reg vs_st_end_f1;
83         struct vop_reg hpost_st_end;
84         struct vop_reg vpost_st_end;
85         struct vop_reg vpost_st_end_f1;
86         struct vop_reg post_scl_factor;
87         struct vop_reg post_scl_ctrl;
88         struct vop_reg dsp_interlace;
89         struct vop_reg global_regdone_en;
90         struct vop_reg auto_gate_en;
91         struct vop_reg post_lb_mode;
92         struct vop_reg dsp_layer_sel;
93         struct vop_reg overlay_mode;
94         struct vop_reg core_dclk_div;
95         struct vop_reg dclk_ddr;
96         struct vop_reg p2i_en;
97         struct vop_reg rgb_en;
98         struct vop_reg edp_en;
99         struct vop_reg hdmi_en;
100         struct vop_reg mipi_en;
101         struct vop_reg dp_en;
102         struct vop_reg pin_pol;
103         struct vop_reg rgb_pin_pol;
104         struct vop_reg hdmi_pin_pol;
105         struct vop_reg edp_pin_pol;
106         struct vop_reg mipi_pin_pol;
107         struct vop_reg dp_pin_pol;
108
109         struct vop_reg dither_up;
110         struct vop_reg dither_down;
111
112         struct vop_reg dsp_out_yuv;
113         struct vop_reg dsp_data_swap;
114         struct vop_reg dsp_ccir656_avg;
115         struct vop_reg dsp_black;
116         struct vop_reg dsp_blank;
117         struct vop_reg dsp_outzero;
118         struct vop_reg update_gamma_lut;
119         struct vop_reg lut_buffer_index;
120         struct vop_reg dsp_lut_en;
121
122         struct vop_reg out_mode;
123
124         struct vop_reg xmirror;
125         struct vop_reg ymirror;
126         struct vop_reg dsp_background;
127
128         /* AFBDC */
129         struct vop_reg afbdc_en;
130         struct vop_reg afbdc_sel;
131         struct vop_reg afbdc_format;
132         struct vop_reg afbdc_hreg_block_split;
133         struct vop_reg afbdc_pic_size;
134         struct vop_reg afbdc_hdr_ptr;
135         struct vop_reg afbdc_rstn;
136
137         struct vop_reg cfg_done;
138 };
139
140 struct vop_intr {
141         const int *intrs;
142         uint32_t nintrs;
143         struct vop_reg line_flag_num[2];
144         struct vop_reg enable;
145         struct vop_reg clear;
146         struct vop_reg status;
147 };
148
149 struct vop_scl_extension {
150         struct vop_reg cbcr_vsd_mode;
151         struct vop_reg cbcr_vsu_mode;
152         struct vop_reg cbcr_hsd_mode;
153         struct vop_reg cbcr_ver_scl_mode;
154         struct vop_reg cbcr_hor_scl_mode;
155         struct vop_reg yrgb_vsd_mode;
156         struct vop_reg yrgb_vsu_mode;
157         struct vop_reg yrgb_hsd_mode;
158         struct vop_reg yrgb_ver_scl_mode;
159         struct vop_reg yrgb_hor_scl_mode;
160         struct vop_reg line_load_mode;
161         struct vop_reg cbcr_axi_gather_num;
162         struct vop_reg yrgb_axi_gather_num;
163         struct vop_reg vsd_cbcr_gt2;
164         struct vop_reg vsd_cbcr_gt4;
165         struct vop_reg vsd_yrgb_gt2;
166         struct vop_reg vsd_yrgb_gt4;
167         struct vop_reg bic_coe_sel;
168         struct vop_reg cbcr_axi_gather_en;
169         struct vop_reg yrgb_axi_gather_en;
170         struct vop_reg lb_mode;
171 };
172
173 struct vop_scl_regs {
174         const struct vop_scl_extension *ext;
175
176         struct vop_reg scale_yrgb_x;
177         struct vop_reg scale_yrgb_y;
178         struct vop_reg scale_cbcr_x;
179         struct vop_reg scale_cbcr_y;
180 };
181
182 struct vop_csc_table {
183         const uint32_t *y2r_bt601;
184         const uint32_t *y2r_bt601_12_235;
185         const uint32_t *y2r_bt601_10bit;
186         const uint32_t *y2r_bt601_10bit_12_235;
187         const uint32_t *r2y_bt601;
188         const uint32_t *r2y_bt601_12_235;
189         const uint32_t *r2y_bt601_10bit;
190         const uint32_t *r2y_bt601_10bit_12_235;
191
192         const uint32_t *y2r_bt709;
193         const uint32_t *y2r_bt709_10bit;
194         const uint32_t *r2y_bt709;
195         const uint32_t *r2y_bt709_10bit;
196
197         const uint32_t *y2r_bt2020;
198         const uint32_t *r2y_bt2020;
199
200         const uint32_t *r2r_bt709_to_bt2020;
201         const uint32_t *r2r_bt2020_to_bt709;
202 };
203
204 enum {
205         VOP_CSC_Y2R_BT601,
206         VOP_CSC_Y2R_BT709,
207         VOP_CSC_Y2R_BT2020,
208         VOP_CSC_R2Y_BT601,
209         VOP_CSC_R2Y_BT709,
210         VOP_CSC_R2Y_BT2020,
211         VOP_CSC_R2R_BT2020_TO_BT709,
212         VOP_CSC_R2R_BT709_TO_2020,
213 };
214
215 enum _vop_overlay_mode {
216         VOP_RGB_DOMAIN,
217         VOP_YUV_DOMAIN
218 };
219
220 struct vop_win_phy {
221         const struct vop_scl_regs *scl;
222         const uint32_t *data_formats;
223         uint32_t nformats;
224
225         struct vop_reg gate;
226         struct vop_reg enable;
227         struct vop_reg format;
228         struct vop_reg fmt_10;
229         struct vop_reg xmirror;
230         struct vop_reg ymirror;
231         struct vop_reg rb_swap;
232         struct vop_reg act_info;
233         struct vop_reg dsp_info;
234         struct vop_reg dsp_st;
235         struct vop_reg yrgb_mst;
236         struct vop_reg uv_mst;
237         struct vop_reg yrgb_vir;
238         struct vop_reg uv_vir;
239
240         struct vop_reg channel;
241         struct vop_reg dst_alpha_ctl;
242         struct vop_reg src_alpha_ctl;
243         struct vop_reg alpha_mode;
244         struct vop_reg alpha_en;
245         struct vop_reg key_color;
246         struct vop_reg key_en;
247 };
248
249 struct vop_win_data {
250         uint32_t base;
251         enum drm_plane_type type;
252         const struct vop_win_phy *phy;
253         const struct vop_win_phy **area;
254         const struct vop_csc *csc;
255         unsigned int area_size;
256 };
257
258 #define VOP_FEATURE_OUTPUT_10BIT        BIT(0)
259 #define VOP_FEATURE_AFBDC               BIT(1)
260
261 struct vop_rect {
262         int width;
263         int height;
264 };
265
266 struct vop_data {
267         const struct vop_reg_data *init_table;
268         unsigned int table_size;
269         const struct vop_ctrl *ctrl;
270         const struct vop_intr *intr;
271         const struct vop_win_data *win;
272         const struct vop_csc_table *csc_table;
273         unsigned int win_size;
274         uint32_t version;
275         struct vop_rect max_input;
276         struct vop_rect max_output;
277         u64 feature;
278 };
279
280 /* interrupt define */
281 #define DSP_HOLD_VALID_INTR             (1 << 0)
282 #define FS_INTR                         (1 << 1)
283 #define LINE_FLAG_INTR                  (1 << 2)
284 #define BUS_ERROR_INTR                  (1 << 3)
285 #define FS_NEW_INTR                     (1 << 4)
286 #define ADDR_SAME_INTR                  (1 << 5)
287 #define LINE_FLAG1_INTR                 (1 << 6)
288 #define WIN0_EMPTY_INTR                 (1 << 7)
289 #define WIN1_EMPTY_INTR                 (1 << 8)
290 #define WIN2_EMPTY_INTR                 (1 << 9)
291 #define WIN3_EMPTY_INTR                 (1 << 10)
292 #define HWC_EMPTY_INTR                  (1 << 11)
293 #define POST_BUF_EMPTY_INTR             (1 << 12)
294 #define PWM_GEN_INTR                    (1 << 13)
295
296 #define INTR_MASK                       (DSP_HOLD_VALID_INTR | FS_INTR | \
297                                          LINE_FLAG_INTR | BUS_ERROR_INTR | \
298                                          FS_NEW_INTR | LINE_FLAG1_INTR | \
299                                          WIN0_EMPTY_INTR | WIN1_EMPTY_INTR | \
300                                          WIN2_EMPTY_INTR | WIN3_EMPTY_INTR | \
301                                          HWC_EMPTY_INTR | POST_BUF_EMPTY_INTR)
302
303 #define DSP_HOLD_VALID_INTR_EN(x)       ((x) << 4)
304 #define FS_INTR_EN(x)                   ((x) << 5)
305 #define LINE_FLAG_INTR_EN(x)            ((x) << 6)
306 #define BUS_ERROR_INTR_EN(x)            ((x) << 7)
307 #define DSP_HOLD_VALID_INTR_MASK        (1 << 4)
308 #define FS_INTR_MASK                    (1 << 5)
309 #define LINE_FLAG_INTR_MASK             (1 << 6)
310 #define BUS_ERROR_INTR_MASK             (1 << 7)
311
312 #define INTR_CLR_SHIFT                  8
313 #define DSP_HOLD_VALID_INTR_CLR         (1 << (INTR_CLR_SHIFT + 0))
314 #define FS_INTR_CLR                     (1 << (INTR_CLR_SHIFT + 1))
315 #define LINE_FLAG_INTR_CLR              (1 << (INTR_CLR_SHIFT + 2))
316 #define BUS_ERROR_INTR_CLR              (1 << (INTR_CLR_SHIFT + 3))
317
318 #define DSP_LINE_NUM(x)                 (((x) & 0x1fff) << 12)
319 #define DSP_LINE_NUM_MASK               (0x1fff << 12)
320
321 /* src alpha ctrl define */
322 #define SRC_FADING_VALUE(x)             (((x) & 0xff) << 24)
323 #define SRC_GLOBAL_ALPHA(x)             (((x) & 0xff) << 16)
324 #define SRC_FACTOR_M0(x)                (((x) & 0x7) << 6)
325 #define SRC_ALPHA_CAL_M0(x)             (((x) & 0x1) << 5)
326 #define SRC_BLEND_M0(x)                 (((x) & 0x3) << 3)
327 #define SRC_ALPHA_M0(x)                 (((x) & 0x1) << 2)
328 #define SRC_COLOR_M0(x)                 (((x) & 0x1) << 1)
329 #define SRC_ALPHA_EN(x)                 (((x) & 0x1) << 0)
330 /* dst alpha ctrl define */
331 #define DST_FACTOR_M0(x)                (((x) & 0x7) << 6)
332
333 /*
334  * display output interface supported by rockchip lcdc
335  */
336 #define ROCKCHIP_OUT_MODE_P888          0
337 #define ROCKCHIP_OUT_MODE_P666          1
338 #define ROCKCHIP_OUT_MODE_P565          2
339 #define ROCKCHIP_OUT_MODE_YUV420        14
340 /* for use special outface */
341 #define ROCKCHIP_OUT_MODE_AAAA          15
342
343 #define ROCKCHIP_OUT_MODE_TYPE(x)       ((x) >> 16)
344 #define ROCKCHIP_OUT_MODE(x)            ((x) & 0xffff)
345 #define ROCKCHIP_DSP_MODE(type, mode) \
346                 (DRM_MODE_CONNECTOR_##type << 16) | \
347                 (ROCKCHIP_OUT_MODE_##mode & 0xffff)
348
349 enum alpha_mode {
350         ALPHA_STRAIGHT,
351         ALPHA_INVERSE,
352 };
353
354 enum global_blend_mode {
355         ALPHA_GLOBAL,
356         ALPHA_PER_PIX,
357         ALPHA_PER_PIX_GLOBAL,
358 };
359
360 enum alpha_cal_mode {
361         ALPHA_SATURATION,
362         ALPHA_NO_SATURATION,
363 };
364
365 enum color_mode {
366         ALPHA_SRC_PRE_MUL,
367         ALPHA_SRC_NO_PRE_MUL,
368 };
369
370 enum factor_mode {
371         ALPHA_ZERO,
372         ALPHA_ONE,
373         ALPHA_SRC,
374         ALPHA_SRC_INVERSE,
375         ALPHA_SRC_GLOBAL,
376 };
377
378 enum scale_mode {
379         SCALE_NONE = 0x0,
380         SCALE_UP   = 0x1,
381         SCALE_DOWN = 0x2
382 };
383
384 enum lb_mode {
385         LB_YUV_3840X5 = 0x0,
386         LB_YUV_2560X8 = 0x1,
387         LB_RGB_3840X2 = 0x2,
388         LB_RGB_2560X4 = 0x3,
389         LB_RGB_1920X5 = 0x4,
390         LB_RGB_1280X8 = 0x5
391 };
392
393 enum sacle_up_mode {
394         SCALE_UP_BIL = 0x0,
395         SCALE_UP_BIC = 0x1
396 };
397
398 enum scale_down_mode {
399         SCALE_DOWN_BIL = 0x0,
400         SCALE_DOWN_AVG = 0x1
401 };
402
403 enum dither_down_mode {
404         RGB888_TO_RGB565 = 0x0,
405         RGB888_TO_RGB666 = 0x1
406 };
407
408 enum dither_down_mode_sel {
409         DITHER_DOWN_ALLEGRO = 0x0,
410         DITHER_DOWN_FRC = 0x1
411 };
412
413 #define PRE_DITHER_DOWN_EN(x)   ((x) << 0)
414 #define DITHER_DOWN_EN(x)       ((x) << 1)
415 #define DITHER_DOWN_MODE(x)     ((x) << 2)
416 #define DITHER_DOWN_MODE_SEL(x) ((x) << 3)
417
418 enum vop_pol {
419         HSYNC_POSITIVE = 0,
420         VSYNC_POSITIVE = 1,
421         DEN_NEGATIVE   = 2,
422         DCLK_INVERT    = 3
423 };
424
425 #define FRAC_16_16(mult, div)    (((mult) << 16) / (div))
426 #define SCL_FT_DEFAULT_FIXPOINT_SHIFT   12
427 #define SCL_MAX_VSKIPLINES              4
428 #define MIN_SCL_FT_AFTER_VSKIP          1
429
430 static inline uint16_t scl_cal_scale(int src, int dst, int shift)
431 {
432         return ((src * 2 - 3) << (shift - 1)) / (dst - 1);
433 }
434
435 static inline uint16_t scl_cal_scale2(int src, int dst)
436 {
437         return ((src - 1) << 12) / (dst - 1);
438 }
439
440 #define GET_SCL_FT_BILI_DN(src, dst)    scl_cal_scale(src, dst, 12)
441 #define GET_SCL_FT_BILI_UP(src, dst)    scl_cal_scale(src, dst, 16)
442 #define GET_SCL_FT_BIC(src, dst)        scl_cal_scale(src, dst, 16)
443
444 static inline uint16_t scl_get_bili_dn_vskip(int src_h, int dst_h,
445                                              int vskiplines)
446 {
447         int act_height;
448
449         act_height = (src_h + vskiplines - 1) / vskiplines;
450
451         if (act_height == dst_h)
452                 return GET_SCL_FT_BILI_DN(src_h, dst_h) / vskiplines;
453
454         return GET_SCL_FT_BILI_DN(act_height, dst_h);
455 }
456
457 static inline enum scale_mode scl_get_scl_mode(int src, int dst)
458 {
459         if (src < dst)
460                 return SCALE_UP;
461         else if (src > dst)
462                 return SCALE_DOWN;
463
464         return SCALE_NONE;
465 }
466
467 static inline int scl_get_vskiplines(uint32_t srch, uint32_t dsth)
468 {
469         uint32_t vskiplines;
470
471         for (vskiplines = SCL_MAX_VSKIPLINES; vskiplines > 1; vskiplines /= 2)
472                 if (srch >= vskiplines * dsth * MIN_SCL_FT_AFTER_VSKIP)
473                         break;
474
475         return vskiplines;
476 }
477
478 static inline int scl_vop_cal_lb_mode(int width, bool is_yuv)
479 {
480         int lb_mode;
481
482         if (width > 2560)
483                 lb_mode = LB_RGB_3840X2;
484         else if (width > 1920)
485                 lb_mode = LB_RGB_2560X4;
486         else if (!is_yuv)
487                 lb_mode = LB_RGB_1920X5;
488         else if (width > 1280)
489                 lb_mode = LB_YUV_3840X5;
490         else
491                 lb_mode = LB_YUV_2560X8;
492
493         return lb_mode;
494 }
495
496 static inline int us_to_vertical_line(struct drm_display_mode *mode, int us)
497 {
498         return us * mode->clock / mode->htotal / 1000;
499 }
500
501 extern const struct component_ops vop_component_ops;
502 #endif /* _ROCKCHIP_DRM_VOP_H */