Merge tag 'lsk-v4.4-17.06-android' of git://git.linaro.org/kernel/linux-linaro-stable.git
[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         struct vop_reg dither_up;
109         struct vop_reg dither_down;
110
111         struct vop_reg sw_dac_sel;
112         struct vop_reg tve_sw_mode;
113         struct vop_reg tve_dclk_pol;
114         struct vop_reg tve_dclk_en;
115         struct vop_reg sw_genlock;
116         struct vop_reg sw_uv_offset_en;
117         struct vop_reg dsp_out_yuv;
118         struct vop_reg dsp_data_swap;
119         struct vop_reg dsp_ccir656_avg;
120         struct vop_reg dsp_black;
121         struct vop_reg dsp_blank;
122         struct vop_reg dsp_outzero;
123         struct vop_reg update_gamma_lut;
124         struct vop_reg lut_buffer_index;
125         struct vop_reg dsp_lut_en;
126
127         struct vop_reg out_mode;
128
129         struct vop_reg xmirror;
130         struct vop_reg ymirror;
131         struct vop_reg dsp_background;
132
133         /* AFBDC */
134         struct vop_reg afbdc_en;
135         struct vop_reg afbdc_sel;
136         struct vop_reg afbdc_format;
137         struct vop_reg afbdc_hreg_block_split;
138         struct vop_reg afbdc_pic_size;
139         struct vop_reg afbdc_hdr_ptr;
140         struct vop_reg afbdc_rstn;
141
142         struct vop_reg cfg_done;
143 };
144
145 struct vop_intr {
146         const int *intrs;
147         uint32_t nintrs;
148         struct vop_reg line_flag_num[2];
149         struct vop_reg enable;
150         struct vop_reg clear;
151         struct vop_reg status;
152 };
153
154 struct vop_scl_extension {
155         struct vop_reg cbcr_vsd_mode;
156         struct vop_reg cbcr_vsu_mode;
157         struct vop_reg cbcr_hsd_mode;
158         struct vop_reg cbcr_ver_scl_mode;
159         struct vop_reg cbcr_hor_scl_mode;
160         struct vop_reg yrgb_vsd_mode;
161         struct vop_reg yrgb_vsu_mode;
162         struct vop_reg yrgb_hsd_mode;
163         struct vop_reg yrgb_ver_scl_mode;
164         struct vop_reg yrgb_hor_scl_mode;
165         struct vop_reg line_load_mode;
166         struct vop_reg cbcr_axi_gather_num;
167         struct vop_reg yrgb_axi_gather_num;
168         struct vop_reg vsd_cbcr_gt2;
169         struct vop_reg vsd_cbcr_gt4;
170         struct vop_reg vsd_yrgb_gt2;
171         struct vop_reg vsd_yrgb_gt4;
172         struct vop_reg bic_coe_sel;
173         struct vop_reg cbcr_axi_gather_en;
174         struct vop_reg yrgb_axi_gather_en;
175         struct vop_reg lb_mode;
176 };
177
178 struct vop_scl_regs {
179         const struct vop_scl_extension *ext;
180
181         struct vop_reg scale_yrgb_x;
182         struct vop_reg scale_yrgb_y;
183         struct vop_reg scale_cbcr_x;
184         struct vop_reg scale_cbcr_y;
185 };
186
187 struct vop_csc_table {
188         const uint32_t *y2r_bt601;
189         const uint32_t *y2r_bt601_12_235;
190         const uint32_t *y2r_bt601_10bit;
191         const uint32_t *y2r_bt601_10bit_12_235;
192         const uint32_t *r2y_bt601;
193         const uint32_t *r2y_bt601_12_235;
194         const uint32_t *r2y_bt601_10bit;
195         const uint32_t *r2y_bt601_10bit_12_235;
196
197         const uint32_t *y2r_bt709;
198         const uint32_t *y2r_bt709_10bit;
199         const uint32_t *r2y_bt709;
200         const uint32_t *r2y_bt709_10bit;
201
202         const uint32_t *y2r_bt2020;
203         const uint32_t *r2y_bt2020;
204
205         const uint32_t *r2r_bt709_to_bt2020;
206         const uint32_t *r2r_bt2020_to_bt709;
207 };
208
209 enum {
210         VOP_CSC_Y2R_BT601,
211         VOP_CSC_Y2R_BT709,
212         VOP_CSC_Y2R_BT2020,
213         VOP_CSC_R2Y_BT601,
214         VOP_CSC_R2Y_BT709,
215         VOP_CSC_R2Y_BT2020,
216         VOP_CSC_R2R_BT2020_TO_BT709,
217         VOP_CSC_R2R_BT709_TO_2020,
218 };
219
220 enum _vop_overlay_mode {
221         VOP_RGB_DOMAIN,
222         VOP_YUV_DOMAIN
223 };
224
225 struct vop_win_phy {
226         const struct vop_scl_regs *scl;
227         const uint32_t *data_formats;
228         uint32_t nformats;
229
230         struct vop_reg gate;
231         struct vop_reg enable;
232         struct vop_reg format;
233         struct vop_reg fmt_10;
234         struct vop_reg xmirror;
235         struct vop_reg ymirror;
236         struct vop_reg rb_swap;
237         struct vop_reg act_info;
238         struct vop_reg dsp_info;
239         struct vop_reg dsp_st;
240         struct vop_reg yrgb_mst;
241         struct vop_reg uv_mst;
242         struct vop_reg yrgb_vir;
243         struct vop_reg uv_vir;
244
245         struct vop_reg channel;
246         struct vop_reg dst_alpha_ctl;
247         struct vop_reg src_alpha_ctl;
248         struct vop_reg alpha_mode;
249         struct vop_reg alpha_en;
250         struct vop_reg key_color;
251         struct vop_reg key_en;
252 };
253
254 struct vop_win_data {
255         uint32_t base;
256         enum drm_plane_type type;
257         const struct vop_win_phy *phy;
258         const struct vop_win_phy **area;
259         const struct vop_csc *csc;
260         unsigned int area_size;
261 };
262
263 #define VOP_FEATURE_OUTPUT_10BIT        BIT(0)
264 #define VOP_FEATURE_AFBDC               BIT(1)
265
266 struct vop_rect {
267         int width;
268         int height;
269 };
270
271 struct vop_data {
272         const struct vop_reg_data *init_table;
273         unsigned int table_size;
274         const struct vop_ctrl *ctrl;
275         const struct vop_intr *intr;
276         const struct vop_win_data *win;
277         const struct vop_csc_table *csc_table;
278         unsigned int win_size;
279         uint32_t version;
280         struct vop_rect max_input;
281         struct vop_rect max_output;
282         u64 feature;
283 };
284
285 #define CVBS_PAL_VDISPLAY               288
286
287 /* interrupt define */
288 #define DSP_HOLD_VALID_INTR             (1 << 0)
289 #define FS_INTR                         (1 << 1)
290 #define LINE_FLAG_INTR                  (1 << 2)
291 #define BUS_ERROR_INTR                  (1 << 3)
292 #define FS_NEW_INTR                     (1 << 4)
293 #define ADDR_SAME_INTR                  (1 << 5)
294 #define LINE_FLAG1_INTR                 (1 << 6)
295 #define WIN0_EMPTY_INTR                 (1 << 7)
296 #define WIN1_EMPTY_INTR                 (1 << 8)
297 #define WIN2_EMPTY_INTR                 (1 << 9)
298 #define WIN3_EMPTY_INTR                 (1 << 10)
299 #define HWC_EMPTY_INTR                  (1 << 11)
300 #define POST_BUF_EMPTY_INTR             (1 << 12)
301 #define PWM_GEN_INTR                    (1 << 13)
302
303 #define INTR_MASK                       (DSP_HOLD_VALID_INTR | FS_INTR | \
304                                          LINE_FLAG_INTR | BUS_ERROR_INTR | \
305                                          FS_NEW_INTR | LINE_FLAG1_INTR | \
306                                          WIN0_EMPTY_INTR | WIN1_EMPTY_INTR | \
307                                          WIN2_EMPTY_INTR | WIN3_EMPTY_INTR | \
308                                          HWC_EMPTY_INTR | POST_BUF_EMPTY_INTR)
309
310 #define DSP_HOLD_VALID_INTR_EN(x)       ((x) << 4)
311 #define FS_INTR_EN(x)                   ((x) << 5)
312 #define LINE_FLAG_INTR_EN(x)            ((x) << 6)
313 #define BUS_ERROR_INTR_EN(x)            ((x) << 7)
314 #define DSP_HOLD_VALID_INTR_MASK        (1 << 4)
315 #define FS_INTR_MASK                    (1 << 5)
316 #define LINE_FLAG_INTR_MASK             (1 << 6)
317 #define BUS_ERROR_INTR_MASK             (1 << 7)
318
319 #define INTR_CLR_SHIFT                  8
320 #define DSP_HOLD_VALID_INTR_CLR         (1 << (INTR_CLR_SHIFT + 0))
321 #define FS_INTR_CLR                     (1 << (INTR_CLR_SHIFT + 1))
322 #define LINE_FLAG_INTR_CLR              (1 << (INTR_CLR_SHIFT + 2))
323 #define BUS_ERROR_INTR_CLR              (1 << (INTR_CLR_SHIFT + 3))
324
325 #define DSP_LINE_NUM(x)                 (((x) & 0x1fff) << 12)
326 #define DSP_LINE_NUM_MASK               (0x1fff << 12)
327
328 /* src alpha ctrl define */
329 #define SRC_FADING_VALUE(x)             (((x) & 0xff) << 24)
330 #define SRC_GLOBAL_ALPHA(x)             (((x) & 0xff) << 16)
331 #define SRC_FACTOR_M0(x)                (((x) & 0x7) << 6)
332 #define SRC_ALPHA_CAL_M0(x)             (((x) & 0x1) << 5)
333 #define SRC_BLEND_M0(x)                 (((x) & 0x3) << 3)
334 #define SRC_ALPHA_M0(x)                 (((x) & 0x1) << 2)
335 #define SRC_COLOR_M0(x)                 (((x) & 0x1) << 1)
336 #define SRC_ALPHA_EN(x)                 (((x) & 0x1) << 0)
337 /* dst alpha ctrl define */
338 #define DST_FACTOR_M0(x)                (((x) & 0x7) << 6)
339
340 /*
341  * display output interface supported by rockchip lcdc
342  */
343 #define ROCKCHIP_OUT_MODE_P888          0
344 #define ROCKCHIP_OUT_MODE_P666          1
345 #define ROCKCHIP_OUT_MODE_P565          2
346 #define ROCKCHIP_OUT_MODE_YUV420        14
347 /* for use special outface */
348 #define ROCKCHIP_OUT_MODE_AAAA          15
349
350 #define ROCKCHIP_OUT_MODE_TYPE(x)       ((x) >> 16)
351 #define ROCKCHIP_OUT_MODE(x)            ((x) & 0xffff)
352 #define ROCKCHIP_DSP_MODE(type, mode) \
353                 (DRM_MODE_CONNECTOR_##type << 16) | \
354                 (ROCKCHIP_OUT_MODE_##mode & 0xffff)
355
356 enum alpha_mode {
357         ALPHA_STRAIGHT,
358         ALPHA_INVERSE,
359 };
360
361 enum global_blend_mode {
362         ALPHA_GLOBAL,
363         ALPHA_PER_PIX,
364         ALPHA_PER_PIX_GLOBAL,
365 };
366
367 enum alpha_cal_mode {
368         ALPHA_SATURATION,
369         ALPHA_NO_SATURATION,
370 };
371
372 enum color_mode {
373         ALPHA_SRC_PRE_MUL,
374         ALPHA_SRC_NO_PRE_MUL,
375 };
376
377 enum factor_mode {
378         ALPHA_ZERO,
379         ALPHA_ONE,
380         ALPHA_SRC,
381         ALPHA_SRC_INVERSE,
382         ALPHA_SRC_GLOBAL,
383 };
384
385 enum scale_mode {
386         SCALE_NONE = 0x0,
387         SCALE_UP   = 0x1,
388         SCALE_DOWN = 0x2
389 };
390
391 enum lb_mode {
392         LB_YUV_3840X5 = 0x0,
393         LB_YUV_2560X8 = 0x1,
394         LB_RGB_3840X2 = 0x2,
395         LB_RGB_2560X4 = 0x3,
396         LB_RGB_1920X5 = 0x4,
397         LB_RGB_1280X8 = 0x5
398 };
399
400 enum sacle_up_mode {
401         SCALE_UP_BIL = 0x0,
402         SCALE_UP_BIC = 0x1
403 };
404
405 enum scale_down_mode {
406         SCALE_DOWN_BIL = 0x0,
407         SCALE_DOWN_AVG = 0x1
408 };
409
410 enum dither_down_mode {
411         RGB888_TO_RGB565 = 0x0,
412         RGB888_TO_RGB666 = 0x1
413 };
414
415 enum dither_down_mode_sel {
416         DITHER_DOWN_ALLEGRO = 0x0,
417         DITHER_DOWN_FRC = 0x1
418 };
419
420 #define PRE_DITHER_DOWN_EN(x)   ((x) << 0)
421 #define DITHER_DOWN_EN(x)       ((x) << 1)
422 #define DITHER_DOWN_MODE(x)     ((x) << 2)
423 #define DITHER_DOWN_MODE_SEL(x) ((x) << 3)
424
425 enum vop_pol {
426         HSYNC_POSITIVE = 0,
427         VSYNC_POSITIVE = 1,
428         DEN_NEGATIVE   = 2,
429         DCLK_INVERT    = 3
430 };
431
432 #define FRAC_16_16(mult, div)    (((mult) << 16) / (div))
433 #define SCL_FT_DEFAULT_FIXPOINT_SHIFT   12
434 #define SCL_MAX_VSKIPLINES              4
435 #define MIN_SCL_FT_AFTER_VSKIP          1
436
437 static inline uint16_t scl_cal_scale(int src, int dst, int shift)
438 {
439         return ((src * 2 - 3) << (shift - 1)) / (dst - 1);
440 }
441
442 static inline uint16_t scl_cal_scale2(int src, int dst)
443 {
444         return ((src - 1) << 12) / (dst - 1);
445 }
446
447 #define GET_SCL_FT_BILI_DN(src, dst)    scl_cal_scale(src, dst, 12)
448 #define GET_SCL_FT_BILI_UP(src, dst)    scl_cal_scale(src, dst, 16)
449 #define GET_SCL_FT_BIC(src, dst)        scl_cal_scale(src, dst, 16)
450
451 static inline uint16_t scl_get_bili_dn_vskip(int src_h, int dst_h,
452                                              int vskiplines)
453 {
454         int act_height;
455
456         act_height = (src_h + vskiplines - 1) / vskiplines;
457
458         if (act_height == dst_h)
459                 return GET_SCL_FT_BILI_DN(src_h, dst_h) / vskiplines;
460
461         return GET_SCL_FT_BILI_DN(act_height, dst_h);
462 }
463
464 static inline enum scale_mode scl_get_scl_mode(int src, int dst)
465 {
466         if (src < dst)
467                 return SCALE_UP;
468         else if (src > dst)
469                 return SCALE_DOWN;
470
471         return SCALE_NONE;
472 }
473
474 static inline int scl_get_vskiplines(uint32_t srch, uint32_t dsth)
475 {
476         uint32_t vskiplines;
477
478         for (vskiplines = SCL_MAX_VSKIPLINES; vskiplines > 1; vskiplines /= 2)
479                 if (srch >= vskiplines * dsth * MIN_SCL_FT_AFTER_VSKIP)
480                         break;
481
482         return vskiplines;
483 }
484
485 static inline int scl_vop_cal_lb_mode(int width, bool is_yuv)
486 {
487         int lb_mode;
488
489         if (width > 2560)
490                 lb_mode = LB_RGB_3840X2;
491         else if (width > 1920)
492                 lb_mode = LB_RGB_2560X4;
493         else if (!is_yuv)
494                 lb_mode = LB_RGB_1920X5;
495         else if (width > 1280)
496                 lb_mode = LB_YUV_3840X5;
497         else
498                 lb_mode = LB_YUV_2560X8;
499
500         return lb_mode;
501 }
502
503 static inline int us_to_vertical_line(struct drm_display_mode *mode, int us)
504 {
505         return us * mode->clock / mode->htotal / 1000;
506 }
507
508 extern const struct component_ops vop_component_ops;
509 #endif /* _ROCKCHIP_DRM_VOP_H */