UPSTREAM: drm/rockchip: Convert to support atomic API
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_vop.c
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 #include <drm/drm.h>
16 #include <drm/drmP.h>
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_crtc.h>
19 #include <drm/drm_crtc_helper.h>
20 #include <drm/drm_plane_helper.h>
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/clk.h>
26 #include <linux/of.h>
27 #include <linux/of_device.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/component.h>
30
31 #include <linux/reset.h>
32 #include <linux/delay.h>
33
34 #include "rockchip_drm_drv.h"
35 #include "rockchip_drm_gem.h"
36 #include "rockchip_drm_fb.h"
37 #include "rockchip_drm_vop.h"
38
39 #define VOP_REG(off, _mask, s) \
40                 {.offset = off, \
41                  .mask = _mask, \
42                  .shift = s,}
43
44 #define __REG_SET_RELAXED(x, off, mask, shift, v) \
45                 vop_mask_write_relaxed(x, off, (mask) << shift, (v) << shift)
46 #define __REG_SET_NORMAL(x, off, mask, shift, v) \
47                 vop_mask_write(x, off, (mask) << shift, (v) << shift)
48
49 #define REG_SET(x, base, reg, v, mode) \
50                 __REG_SET_##mode(x, base + reg.offset, reg.mask, reg.shift, v)
51
52 #define VOP_WIN_SET(x, win, name, v) \
53                 REG_SET(x, win->base, win->phy->name, v, RELAXED)
54 #define VOP_SCL_SET(x, win, name, v) \
55                 REG_SET(x, win->base, win->phy->scl->name, v, RELAXED)
56 #define VOP_CTRL_SET(x, name, v) \
57                 REG_SET(x, 0, (x)->data->ctrl->name, v, NORMAL)
58
59 #define VOP_WIN_GET(x, win, name) \
60                 vop_read_reg(x, win->base, &win->phy->name)
61
62 #define VOP_WIN_GET_YRGBADDR(vop, win) \
63                 vop_readl(vop, win->base + win->phy->yrgb_mst.offset)
64
65 #define to_vop(x) container_of(x, struct vop, crtc)
66 #define to_vop_win(x) container_of(x, struct vop_win, base)
67 #define to_vop_plane_state(x) container_of(x, struct vop_plane_state, base)
68
69 struct vop_plane_state {
70         struct drm_plane_state base;
71         int format;
72         struct drm_rect src;
73         struct drm_rect dest;
74         dma_addr_t yrgb_mst;
75         bool enable;
76 };
77
78 struct vop_win {
79         struct drm_plane base;
80         const struct vop_win_data *data;
81         struct vop *vop;
82
83         struct vop_plane_state state;
84 };
85
86 struct vop {
87         struct drm_crtc crtc;
88         struct device *dev;
89         struct drm_device *drm_dev;
90         bool is_enabled;
91
92         int connector_type;
93         int connector_out_mode;
94
95         /* mutex vsync_ work */
96         struct mutex vsync_mutex;
97         bool vsync_work_pending;
98         struct completion dsp_hold_completion;
99         struct completion wait_update_complete;
100         struct drm_pending_vblank_event *event;
101
102         const struct vop_data *data;
103
104         uint32_t *regsbak;
105         void __iomem *regs;
106
107         /* physical map length of vop register */
108         uint32_t len;
109
110         /* one time only one process allowed to config the register */
111         spinlock_t reg_lock;
112         /* lock vop irq reg */
113         spinlock_t irq_lock;
114
115         unsigned int irq;
116
117         /* vop AHP clk */
118         struct clk *hclk;
119         /* vop dclk */
120         struct clk *dclk;
121         /* vop share memory frequency */
122         struct clk *aclk;
123
124         /* vop dclk reset */
125         struct reset_control *dclk_rst;
126
127         struct vop_win win[];
128 };
129
130 enum vop_data_format {
131         VOP_FMT_ARGB8888 = 0,
132         VOP_FMT_RGB888,
133         VOP_FMT_RGB565,
134         VOP_FMT_YUV420SP = 4,
135         VOP_FMT_YUV422SP,
136         VOP_FMT_YUV444SP,
137 };
138
139 struct vop_reg_data {
140         uint32_t offset;
141         uint32_t value;
142 };
143
144 struct vop_reg {
145         uint32_t offset;
146         uint32_t shift;
147         uint32_t mask;
148 };
149
150 struct vop_ctrl {
151         struct vop_reg standby;
152         struct vop_reg data_blank;
153         struct vop_reg gate_en;
154         struct vop_reg mmu_en;
155         struct vop_reg rgb_en;
156         struct vop_reg edp_en;
157         struct vop_reg hdmi_en;
158         struct vop_reg mipi_en;
159         struct vop_reg out_mode;
160         struct vop_reg dither_down;
161         struct vop_reg dither_up;
162         struct vop_reg pin_pol;
163
164         struct vop_reg htotal_pw;
165         struct vop_reg hact_st_end;
166         struct vop_reg vtotal_pw;
167         struct vop_reg vact_st_end;
168         struct vop_reg hpost_st_end;
169         struct vop_reg vpost_st_end;
170 };
171
172 struct vop_scl_regs {
173         struct vop_reg cbcr_vsd_mode;
174         struct vop_reg cbcr_vsu_mode;
175         struct vop_reg cbcr_hsd_mode;
176         struct vop_reg cbcr_ver_scl_mode;
177         struct vop_reg cbcr_hor_scl_mode;
178         struct vop_reg yrgb_vsd_mode;
179         struct vop_reg yrgb_vsu_mode;
180         struct vop_reg yrgb_hsd_mode;
181         struct vop_reg yrgb_ver_scl_mode;
182         struct vop_reg yrgb_hor_scl_mode;
183         struct vop_reg line_load_mode;
184         struct vop_reg cbcr_axi_gather_num;
185         struct vop_reg yrgb_axi_gather_num;
186         struct vop_reg vsd_cbcr_gt2;
187         struct vop_reg vsd_cbcr_gt4;
188         struct vop_reg vsd_yrgb_gt2;
189         struct vop_reg vsd_yrgb_gt4;
190         struct vop_reg bic_coe_sel;
191         struct vop_reg cbcr_axi_gather_en;
192         struct vop_reg yrgb_axi_gather_en;
193
194         struct vop_reg lb_mode;
195         struct vop_reg scale_yrgb_x;
196         struct vop_reg scale_yrgb_y;
197         struct vop_reg scale_cbcr_x;
198         struct vop_reg scale_cbcr_y;
199 };
200
201 struct vop_win_phy {
202         const struct vop_scl_regs *scl;
203         const uint32_t *data_formats;
204         uint32_t nformats;
205
206         struct vop_reg enable;
207         struct vop_reg format;
208         struct vop_reg rb_swap;
209         struct vop_reg act_info;
210         struct vop_reg dsp_info;
211         struct vop_reg dsp_st;
212         struct vop_reg yrgb_mst;
213         struct vop_reg uv_mst;
214         struct vop_reg yrgb_vir;
215         struct vop_reg uv_vir;
216
217         struct vop_reg dst_alpha_ctl;
218         struct vop_reg src_alpha_ctl;
219 };
220
221 struct vop_win_data {
222         uint32_t base;
223         const struct vop_win_phy *phy;
224         enum drm_plane_type type;
225 };
226
227 struct vop_data {
228         const struct vop_reg_data *init_table;
229         unsigned int table_size;
230         const struct vop_ctrl *ctrl;
231         const struct vop_win_data *win;
232         unsigned int win_size;
233 };
234
235 static const uint32_t formats_01[] = {
236         DRM_FORMAT_XRGB8888,
237         DRM_FORMAT_ARGB8888,
238         DRM_FORMAT_XBGR8888,
239         DRM_FORMAT_ABGR8888,
240         DRM_FORMAT_RGB888,
241         DRM_FORMAT_BGR888,
242         DRM_FORMAT_RGB565,
243         DRM_FORMAT_BGR565,
244         DRM_FORMAT_NV12,
245         DRM_FORMAT_NV16,
246         DRM_FORMAT_NV24,
247 };
248
249 static const uint32_t formats_234[] = {
250         DRM_FORMAT_XRGB8888,
251         DRM_FORMAT_ARGB8888,
252         DRM_FORMAT_XBGR8888,
253         DRM_FORMAT_ABGR8888,
254         DRM_FORMAT_RGB888,
255         DRM_FORMAT_BGR888,
256         DRM_FORMAT_RGB565,
257         DRM_FORMAT_BGR565,
258 };
259
260 static const struct vop_scl_regs win_full_scl = {
261         .cbcr_vsd_mode = VOP_REG(WIN0_CTRL1, 0x1, 31),
262         .cbcr_vsu_mode = VOP_REG(WIN0_CTRL1, 0x1, 30),
263         .cbcr_hsd_mode = VOP_REG(WIN0_CTRL1, 0x3, 28),
264         .cbcr_ver_scl_mode = VOP_REG(WIN0_CTRL1, 0x3, 26),
265         .cbcr_hor_scl_mode = VOP_REG(WIN0_CTRL1, 0x3, 24),
266         .yrgb_vsd_mode = VOP_REG(WIN0_CTRL1, 0x1, 23),
267         .yrgb_vsu_mode = VOP_REG(WIN0_CTRL1, 0x1, 22),
268         .yrgb_hsd_mode = VOP_REG(WIN0_CTRL1, 0x3, 20),
269         .yrgb_ver_scl_mode = VOP_REG(WIN0_CTRL1, 0x3, 18),
270         .yrgb_hor_scl_mode = VOP_REG(WIN0_CTRL1, 0x3, 16),
271         .line_load_mode = VOP_REG(WIN0_CTRL1, 0x1, 15),
272         .cbcr_axi_gather_num = VOP_REG(WIN0_CTRL1, 0x7, 12),
273         .yrgb_axi_gather_num = VOP_REG(WIN0_CTRL1, 0xf, 8),
274         .vsd_cbcr_gt2 = VOP_REG(WIN0_CTRL1, 0x1, 7),
275         .vsd_cbcr_gt4 = VOP_REG(WIN0_CTRL1, 0x1, 6),
276         .vsd_yrgb_gt2 = VOP_REG(WIN0_CTRL1, 0x1, 5),
277         .vsd_yrgb_gt4 = VOP_REG(WIN0_CTRL1, 0x1, 4),
278         .bic_coe_sel = VOP_REG(WIN0_CTRL1, 0x3, 2),
279         .cbcr_axi_gather_en = VOP_REG(WIN0_CTRL1, 0x1, 1),
280         .yrgb_axi_gather_en = VOP_REG(WIN0_CTRL1, 0x1, 0),
281         .lb_mode = VOP_REG(WIN0_CTRL0, 0x7, 5),
282         .scale_yrgb_x = VOP_REG(WIN0_SCL_FACTOR_YRGB, 0xffff, 0x0),
283         .scale_yrgb_y = VOP_REG(WIN0_SCL_FACTOR_YRGB, 0xffff, 16),
284         .scale_cbcr_x = VOP_REG(WIN0_SCL_FACTOR_CBR, 0xffff, 0x0),
285         .scale_cbcr_y = VOP_REG(WIN0_SCL_FACTOR_CBR, 0xffff, 16),
286 };
287
288 static const struct vop_win_phy win01_data = {
289         .scl = &win_full_scl,
290         .data_formats = formats_01,
291         .nformats = ARRAY_SIZE(formats_01),
292         .enable = VOP_REG(WIN0_CTRL0, 0x1, 0),
293         .format = VOP_REG(WIN0_CTRL0, 0x7, 1),
294         .rb_swap = VOP_REG(WIN0_CTRL0, 0x1, 12),
295         .act_info = VOP_REG(WIN0_ACT_INFO, 0x1fff1fff, 0),
296         .dsp_info = VOP_REG(WIN0_DSP_INFO, 0x0fff0fff, 0),
297         .dsp_st = VOP_REG(WIN0_DSP_ST, 0x1fff1fff, 0),
298         .yrgb_mst = VOP_REG(WIN0_YRGB_MST, 0xffffffff, 0),
299         .uv_mst = VOP_REG(WIN0_CBR_MST, 0xffffffff, 0),
300         .yrgb_vir = VOP_REG(WIN0_VIR, 0x3fff, 0),
301         .uv_vir = VOP_REG(WIN0_VIR, 0x3fff, 16),
302         .src_alpha_ctl = VOP_REG(WIN0_SRC_ALPHA_CTRL, 0xff, 0),
303         .dst_alpha_ctl = VOP_REG(WIN0_DST_ALPHA_CTRL, 0xff, 0),
304 };
305
306 static const struct vop_win_phy win23_data = {
307         .data_formats = formats_234,
308         .nformats = ARRAY_SIZE(formats_234),
309         .enable = VOP_REG(WIN2_CTRL0, 0x1, 0),
310         .format = VOP_REG(WIN2_CTRL0, 0x7, 1),
311         .rb_swap = VOP_REG(WIN2_CTRL0, 0x1, 12),
312         .dsp_info = VOP_REG(WIN2_DSP_INFO0, 0x0fff0fff, 0),
313         .dsp_st = VOP_REG(WIN2_DSP_ST0, 0x1fff1fff, 0),
314         .yrgb_mst = VOP_REG(WIN2_MST0, 0xffffffff, 0),
315         .yrgb_vir = VOP_REG(WIN2_VIR0_1, 0x1fff, 0),
316         .src_alpha_ctl = VOP_REG(WIN2_SRC_ALPHA_CTRL, 0xff, 0),
317         .dst_alpha_ctl = VOP_REG(WIN2_DST_ALPHA_CTRL, 0xff, 0),
318 };
319
320 static const struct vop_ctrl ctrl_data = {
321         .standby = VOP_REG(SYS_CTRL, 0x1, 22),
322         .gate_en = VOP_REG(SYS_CTRL, 0x1, 23),
323         .mmu_en = VOP_REG(SYS_CTRL, 0x1, 20),
324         .rgb_en = VOP_REG(SYS_CTRL, 0x1, 12),
325         .hdmi_en = VOP_REG(SYS_CTRL, 0x1, 13),
326         .edp_en = VOP_REG(SYS_CTRL, 0x1, 14),
327         .mipi_en = VOP_REG(SYS_CTRL, 0x1, 15),
328         .dither_down = VOP_REG(DSP_CTRL1, 0xf, 1),
329         .dither_up = VOP_REG(DSP_CTRL1, 0x1, 6),
330         .data_blank = VOP_REG(DSP_CTRL0, 0x1, 19),
331         .out_mode = VOP_REG(DSP_CTRL0, 0xf, 0),
332         .pin_pol = VOP_REG(DSP_CTRL0, 0xf, 4),
333         .htotal_pw = VOP_REG(DSP_HTOTAL_HS_END, 0x1fff1fff, 0),
334         .hact_st_end = VOP_REG(DSP_HACT_ST_END, 0x1fff1fff, 0),
335         .vtotal_pw = VOP_REG(DSP_VTOTAL_VS_END, 0x1fff1fff, 0),
336         .vact_st_end = VOP_REG(DSP_VACT_ST_END, 0x1fff1fff, 0),
337         .hpost_st_end = VOP_REG(POST_DSP_HACT_INFO, 0x1fff1fff, 0),
338         .vpost_st_end = VOP_REG(POST_DSP_VACT_INFO, 0x1fff1fff, 0),
339 };
340
341 static const struct vop_reg_data vop_init_reg_table[] = {
342         {SYS_CTRL, 0x00c00000},
343         {DSP_CTRL0, 0x00000000},
344         {WIN0_CTRL0, 0x00000080},
345         {WIN1_CTRL0, 0x00000080},
346         /* TODO: Win2/3 support multiple area function, but we haven't found
347          * a suitable way to use it yet, so let's just use them as other windows
348          * with only area 0 enabled.
349          */
350         {WIN2_CTRL0, 0x00000010},
351         {WIN3_CTRL0, 0x00000010},
352 };
353
354 /*
355  * Note: rk3288 has a dedicated 'cursor' window, however, that window requires
356  * special support to get alpha blending working.  For now, just use overlay
357  * window 3 for the drm cursor.
358  *
359  */
360 static const struct vop_win_data rk3288_vop_win_data[] = {
361         { .base = 0x00, .phy = &win01_data, .type = DRM_PLANE_TYPE_PRIMARY },
362         { .base = 0x40, .phy = &win01_data, .type = DRM_PLANE_TYPE_OVERLAY },
363         { .base = 0x00, .phy = &win23_data, .type = DRM_PLANE_TYPE_OVERLAY },
364         { .base = 0x50, .phy = &win23_data, .type = DRM_PLANE_TYPE_CURSOR },
365 };
366
367 static const struct vop_data rk3288_vop = {
368         .init_table = vop_init_reg_table,
369         .table_size = ARRAY_SIZE(vop_init_reg_table),
370         .ctrl = &ctrl_data,
371         .win = rk3288_vop_win_data,
372         .win_size = ARRAY_SIZE(rk3288_vop_win_data),
373 };
374
375 static const struct of_device_id vop_driver_dt_match[] = {
376         { .compatible = "rockchip,rk3288-vop",
377           .data = &rk3288_vop },
378         {},
379 };
380 MODULE_DEVICE_TABLE(of, vop_driver_dt_match);
381
382 static inline void vop_writel(struct vop *vop, uint32_t offset, uint32_t v)
383 {
384         writel(v, vop->regs + offset);
385         vop->regsbak[offset >> 2] = v;
386 }
387
388 static inline uint32_t vop_readl(struct vop *vop, uint32_t offset)
389 {
390         return readl(vop->regs + offset);
391 }
392
393 static inline uint32_t vop_read_reg(struct vop *vop, uint32_t base,
394                                     const struct vop_reg *reg)
395 {
396         return (vop_readl(vop, base + reg->offset) >> reg->shift) & reg->mask;
397 }
398
399 static inline void vop_cfg_done(struct vop *vop)
400 {
401         writel(0x01, vop->regs + REG_CFG_DONE);
402 }
403
404 static inline void vop_mask_write(struct vop *vop, uint32_t offset,
405                                   uint32_t mask, uint32_t v)
406 {
407         if (mask) {
408                 uint32_t cached_val = vop->regsbak[offset >> 2];
409
410                 cached_val = (cached_val & ~mask) | v;
411                 writel(cached_val, vop->regs + offset);
412                 vop->regsbak[offset >> 2] = cached_val;
413         }
414 }
415
416 static inline void vop_mask_write_relaxed(struct vop *vop, uint32_t offset,
417                                           uint32_t mask, uint32_t v)
418 {
419         if (mask) {
420                 uint32_t cached_val = vop->regsbak[offset >> 2];
421
422                 cached_val = (cached_val & ~mask) | v;
423                 writel_relaxed(cached_val, vop->regs + offset);
424                 vop->regsbak[offset >> 2] = cached_val;
425         }
426 }
427
428 static bool has_rb_swapped(uint32_t format)
429 {
430         switch (format) {
431         case DRM_FORMAT_XBGR8888:
432         case DRM_FORMAT_ABGR8888:
433         case DRM_FORMAT_BGR888:
434         case DRM_FORMAT_BGR565:
435                 return true;
436         default:
437                 return false;
438         }
439 }
440
441 static enum vop_data_format vop_convert_format(uint32_t format)
442 {
443         switch (format) {
444         case DRM_FORMAT_XRGB8888:
445         case DRM_FORMAT_ARGB8888:
446         case DRM_FORMAT_XBGR8888:
447         case DRM_FORMAT_ABGR8888:
448                 return VOP_FMT_ARGB8888;
449         case DRM_FORMAT_RGB888:
450         case DRM_FORMAT_BGR888:
451                 return VOP_FMT_RGB888;
452         case DRM_FORMAT_RGB565:
453         case DRM_FORMAT_BGR565:
454                 return VOP_FMT_RGB565;
455         case DRM_FORMAT_NV12:
456                 return VOP_FMT_YUV420SP;
457         case DRM_FORMAT_NV16:
458                 return VOP_FMT_YUV422SP;
459         case DRM_FORMAT_NV24:
460                 return VOP_FMT_YUV444SP;
461         default:
462                 DRM_ERROR("unsupport format[%08x]\n", format);
463                 return -EINVAL;
464         }
465 }
466
467 static bool is_yuv_support(uint32_t format)
468 {
469         switch (format) {
470         case DRM_FORMAT_NV12:
471         case DRM_FORMAT_NV16:
472         case DRM_FORMAT_NV24:
473                 return true;
474         default:
475                 return false;
476         }
477 }
478
479 static bool is_alpha_support(uint32_t format)
480 {
481         switch (format) {
482         case DRM_FORMAT_ARGB8888:
483         case DRM_FORMAT_ABGR8888:
484                 return true;
485         default:
486                 return false;
487         }
488 }
489
490 static uint16_t scl_vop_cal_scale(enum scale_mode mode, uint32_t src,
491                                   uint32_t dst, bool is_horizontal,
492                                   int vsu_mode, int *vskiplines)
493 {
494         uint16_t val = 1 << SCL_FT_DEFAULT_FIXPOINT_SHIFT;
495
496         if (is_horizontal) {
497                 if (mode == SCALE_UP)
498                         val = GET_SCL_FT_BIC(src, dst);
499                 else if (mode == SCALE_DOWN)
500                         val = GET_SCL_FT_BILI_DN(src, dst);
501         } else {
502                 if (mode == SCALE_UP) {
503                         if (vsu_mode == SCALE_UP_BIL)
504                                 val = GET_SCL_FT_BILI_UP(src, dst);
505                         else
506                                 val = GET_SCL_FT_BIC(src, dst);
507                 } else if (mode == SCALE_DOWN) {
508                         if (vskiplines) {
509                                 *vskiplines = scl_get_vskiplines(src, dst);
510                                 val = scl_get_bili_dn_vskip(src, dst,
511                                                             *vskiplines);
512                         } else {
513                                 val = GET_SCL_FT_BILI_DN(src, dst);
514                         }
515                 }
516         }
517
518         return val;
519 }
520
521 static void scl_vop_cal_scl_fac(struct vop *vop, const struct vop_win_data *win,
522                              uint32_t src_w, uint32_t src_h, uint32_t dst_w,
523                              uint32_t dst_h, uint32_t pixel_format)
524 {
525         uint16_t yrgb_hor_scl_mode, yrgb_ver_scl_mode;
526         uint16_t cbcr_hor_scl_mode = SCALE_NONE;
527         uint16_t cbcr_ver_scl_mode = SCALE_NONE;
528         int hsub = drm_format_horz_chroma_subsampling(pixel_format);
529         int vsub = drm_format_vert_chroma_subsampling(pixel_format);
530         bool is_yuv = is_yuv_support(pixel_format);
531         uint16_t cbcr_src_w = src_w / hsub;
532         uint16_t cbcr_src_h = src_h / vsub;
533         uint16_t vsu_mode;
534         uint16_t lb_mode;
535         uint32_t val;
536         int vskiplines;
537
538         if (dst_w > 3840) {
539                 DRM_ERROR("Maximum destination width (3840) exceeded\n");
540                 return;
541         }
542
543         yrgb_hor_scl_mode = scl_get_scl_mode(src_w, dst_w);
544         yrgb_ver_scl_mode = scl_get_scl_mode(src_h, dst_h);
545
546         if (is_yuv) {
547                 cbcr_hor_scl_mode = scl_get_scl_mode(cbcr_src_w, dst_w);
548                 cbcr_ver_scl_mode = scl_get_scl_mode(cbcr_src_h, dst_h);
549                 if (cbcr_hor_scl_mode == SCALE_DOWN)
550                         lb_mode = scl_vop_cal_lb_mode(dst_w, true);
551                 else
552                         lb_mode = scl_vop_cal_lb_mode(cbcr_src_w, true);
553         } else {
554                 if (yrgb_hor_scl_mode == SCALE_DOWN)
555                         lb_mode = scl_vop_cal_lb_mode(dst_w, false);
556                 else
557                         lb_mode = scl_vop_cal_lb_mode(src_w, false);
558         }
559
560         VOP_SCL_SET(vop, win, lb_mode, lb_mode);
561         if (lb_mode == LB_RGB_3840X2) {
562                 if (yrgb_ver_scl_mode != SCALE_NONE) {
563                         DRM_ERROR("ERROR : not allow yrgb ver scale\n");
564                         return;
565                 }
566                 if (cbcr_ver_scl_mode != SCALE_NONE) {
567                         DRM_ERROR("ERROR : not allow cbcr ver scale\n");
568                         return;
569                 }
570                 vsu_mode = SCALE_UP_BIL;
571         } else if (lb_mode == LB_RGB_2560X4) {
572                 vsu_mode = SCALE_UP_BIL;
573         } else {
574                 vsu_mode = SCALE_UP_BIC;
575         }
576
577         val = scl_vop_cal_scale(yrgb_hor_scl_mode, src_w, dst_w,
578                                 true, 0, NULL);
579         VOP_SCL_SET(vop, win, scale_yrgb_x, val);
580         val = scl_vop_cal_scale(yrgb_ver_scl_mode, src_h, dst_h,
581                                 false, vsu_mode, &vskiplines);
582         VOP_SCL_SET(vop, win, scale_yrgb_y, val);
583
584         VOP_SCL_SET(vop, win, vsd_yrgb_gt4, vskiplines == 4);
585         VOP_SCL_SET(vop, win, vsd_yrgb_gt2, vskiplines == 2);
586
587         VOP_SCL_SET(vop, win, yrgb_hor_scl_mode, yrgb_hor_scl_mode);
588         VOP_SCL_SET(vop, win, yrgb_ver_scl_mode, yrgb_ver_scl_mode);
589         VOP_SCL_SET(vop, win, yrgb_hsd_mode, SCALE_DOWN_BIL);
590         VOP_SCL_SET(vop, win, yrgb_vsd_mode, SCALE_DOWN_BIL);
591         VOP_SCL_SET(vop, win, yrgb_vsu_mode, vsu_mode);
592         if (is_yuv) {
593                 val = scl_vop_cal_scale(cbcr_hor_scl_mode, cbcr_src_w,
594                                         dst_w, true, 0, NULL);
595                 VOP_SCL_SET(vop, win, scale_cbcr_x, val);
596                 val = scl_vop_cal_scale(cbcr_ver_scl_mode, cbcr_src_h,
597                                         dst_h, false, vsu_mode, &vskiplines);
598                 VOP_SCL_SET(vop, win, scale_cbcr_y, val);
599
600                 VOP_SCL_SET(vop, win, vsd_cbcr_gt4, vskiplines == 4);
601                 VOP_SCL_SET(vop, win, vsd_cbcr_gt2, vskiplines == 2);
602                 VOP_SCL_SET(vop, win, cbcr_hor_scl_mode, cbcr_hor_scl_mode);
603                 VOP_SCL_SET(vop, win, cbcr_ver_scl_mode, cbcr_ver_scl_mode);
604                 VOP_SCL_SET(vop, win, cbcr_hsd_mode, SCALE_DOWN_BIL);
605                 VOP_SCL_SET(vop, win, cbcr_vsd_mode, SCALE_DOWN_BIL);
606                 VOP_SCL_SET(vop, win, cbcr_vsu_mode, vsu_mode);
607         }
608 }
609
610 static void vop_dsp_hold_valid_irq_enable(struct vop *vop)
611 {
612         unsigned long flags;
613
614         if (WARN_ON(!vop->is_enabled))
615                 return;
616
617         spin_lock_irqsave(&vop->irq_lock, flags);
618
619         vop_mask_write(vop, INTR_CTRL0, DSP_HOLD_VALID_INTR_MASK,
620                        DSP_HOLD_VALID_INTR_EN(1));
621
622         spin_unlock_irqrestore(&vop->irq_lock, flags);
623 }
624
625 static void vop_dsp_hold_valid_irq_disable(struct vop *vop)
626 {
627         unsigned long flags;
628
629         if (WARN_ON(!vop->is_enabled))
630                 return;
631
632         spin_lock_irqsave(&vop->irq_lock, flags);
633
634         vop_mask_write(vop, INTR_CTRL0, DSP_HOLD_VALID_INTR_MASK,
635                        DSP_HOLD_VALID_INTR_EN(0));
636
637         spin_unlock_irqrestore(&vop->irq_lock, flags);
638 }
639
640 static void vop_enable(struct drm_crtc *crtc)
641 {
642         struct vop *vop = to_vop(crtc);
643         int ret;
644
645         if (vop->is_enabled)
646                 return;
647
648         ret = pm_runtime_get_sync(vop->dev);
649         if (ret < 0) {
650                 dev_err(vop->dev, "failed to get pm runtime: %d\n", ret);
651                 return;
652         }
653
654         ret = clk_enable(vop->hclk);
655         if (ret < 0) {
656                 dev_err(vop->dev, "failed to enable hclk - %d\n", ret);
657                 return;
658         }
659
660         ret = clk_enable(vop->dclk);
661         if (ret < 0) {
662                 dev_err(vop->dev, "failed to enable dclk - %d\n", ret);
663                 goto err_disable_hclk;
664         }
665
666         ret = clk_enable(vop->aclk);
667         if (ret < 0) {
668                 dev_err(vop->dev, "failed to enable aclk - %d\n", ret);
669                 goto err_disable_dclk;
670         }
671
672         /*
673          * Slave iommu shares power, irq and clock with vop.  It was associated
674          * automatically with this master device via common driver code.
675          * Now that we have enabled the clock we attach it to the shared drm
676          * mapping.
677          */
678         ret = rockchip_drm_dma_attach_device(vop->drm_dev, vop->dev);
679         if (ret) {
680                 dev_err(vop->dev, "failed to attach dma mapping, %d\n", ret);
681                 goto err_disable_aclk;
682         }
683
684         memcpy(vop->regs, vop->regsbak, vop->len);
685         /*
686          * At here, vop clock & iommu is enable, R/W vop regs would be safe.
687          */
688         vop->is_enabled = true;
689
690         spin_lock(&vop->reg_lock);
691
692         VOP_CTRL_SET(vop, standby, 0);
693
694         spin_unlock(&vop->reg_lock);
695
696         enable_irq(vop->irq);
697
698         drm_crtc_vblank_on(crtc);
699
700         return;
701
702 err_disable_aclk:
703         clk_disable(vop->aclk);
704 err_disable_dclk:
705         clk_disable(vop->dclk);
706 err_disable_hclk:
707         clk_disable(vop->hclk);
708 }
709
710 static void vop_crtc_disable(struct drm_crtc *crtc)
711 {
712         struct vop *vop = to_vop(crtc);
713
714         if (!vop->is_enabled)
715                 return;
716
717         drm_crtc_vblank_off(crtc);
718
719         /*
720          * Vop standby will take effect at end of current frame,
721          * if dsp hold valid irq happen, it means standby complete.
722          *
723          * we must wait standby complete when we want to disable aclk,
724          * if not, memory bus maybe dead.
725          */
726         reinit_completion(&vop->dsp_hold_completion);
727         vop_dsp_hold_valid_irq_enable(vop);
728
729         spin_lock(&vop->reg_lock);
730
731         VOP_CTRL_SET(vop, standby, 1);
732
733         spin_unlock(&vop->reg_lock);
734
735         wait_for_completion(&vop->dsp_hold_completion);
736
737         vop_dsp_hold_valid_irq_disable(vop);
738
739         disable_irq(vop->irq);
740
741         vop->is_enabled = false;
742
743         /*
744          * vop standby complete, so iommu detach is safe.
745          */
746         rockchip_drm_dma_detach_device(vop->drm_dev, vop->dev);
747
748         clk_disable(vop->dclk);
749         clk_disable(vop->aclk);
750         clk_disable(vop->hclk);
751         pm_runtime_put(vop->dev);
752 }
753
754 static void vop_plane_destroy(struct drm_plane *plane)
755 {
756         drm_plane_cleanup(plane);
757 }
758
759 static int vop_plane_atomic_check(struct drm_plane *plane,
760                            struct drm_plane_state *state)
761 {
762         struct drm_crtc *crtc = state->crtc;
763         struct drm_framebuffer *fb = state->fb;
764         struct vop_win *vop_win = to_vop_win(plane);
765         struct vop_plane_state *vop_plane_state = to_vop_plane_state(state);
766         const struct vop_win_data *win = vop_win->data;
767         bool visible;
768         int ret;
769         struct drm_rect *dest = &vop_plane_state->dest;
770         struct drm_rect *src = &vop_plane_state->src;
771         struct drm_rect clip;
772         int min_scale = win->phy->scl ? FRAC_16_16(1, 8) :
773                                         DRM_PLANE_HELPER_NO_SCALING;
774         int max_scale = win->phy->scl ? FRAC_16_16(8, 1) :
775                                         DRM_PLANE_HELPER_NO_SCALING;
776
777         crtc = crtc ? crtc : plane->state->crtc;
778         /*
779          * Both crtc or plane->state->crtc can be null.
780          */
781         if (!crtc || !fb)
782                 goto out_disable;
783         src->x1 = state->src_x;
784         src->y1 = state->src_y;
785         src->x2 = state->src_x + state->src_w;
786         src->y2 = state->src_y + state->src_h;
787         dest->x1 = state->crtc_x;
788         dest->y1 = state->crtc_y;
789         dest->x2 = state->crtc_x + state->crtc_w;
790         dest->y2 = state->crtc_y + state->crtc_h;
791
792         clip.x1 = 0;
793         clip.y1 = 0;
794         clip.x2 = crtc->mode.hdisplay;
795         clip.y2 = crtc->mode.vdisplay;
796
797         ret = drm_plane_helper_check_update(plane, crtc, state->fb,
798                                             src, dest, &clip,
799                                             min_scale,
800                                             max_scale,
801                                             true, true, &visible);
802         if (ret)
803                 return ret;
804
805         if (!visible)
806                 goto out_disable;
807
808         vop_plane_state->format = vop_convert_format(fb->pixel_format);
809         if (vop_plane_state->format < 0)
810                 return vop_plane_state->format;
811
812         /*
813          * Src.x1 can be odd when do clip, but yuv plane start point
814          * need align with 2 pixel.
815          */
816         if (is_yuv_support(fb->pixel_format) && ((src->x1 >> 16) % 2))
817                 return -EINVAL;
818
819         vop_plane_state->enable = true;
820
821         return 0;
822
823 out_disable:
824         vop_plane_state->enable = false;
825         return 0;
826 }
827
828 static void vop_plane_atomic_disable(struct drm_plane *plane,
829                                      struct drm_plane_state *old_state)
830 {
831         struct vop_plane_state *vop_plane_state = to_vop_plane_state(old_state);
832         struct vop_win *vop_win = to_vop_win(plane);
833         const struct vop_win_data *win = vop_win->data;
834         struct vop *vop = to_vop(old_state->crtc);
835
836         if (!old_state->crtc)
837                 return;
838
839         spin_lock(&vop->reg_lock);
840
841         VOP_WIN_SET(vop, win, enable, 0);
842
843         spin_unlock(&vop->reg_lock);
844
845         vop_plane_state->enable = false;
846 }
847
848 static void vop_plane_atomic_update(struct drm_plane *plane,
849                 struct drm_plane_state *old_state)
850 {
851         struct drm_plane_state *state = plane->state;
852         struct drm_crtc *crtc = state->crtc;
853         struct vop_win *vop_win = to_vop_win(plane);
854         struct vop_plane_state *vop_plane_state = to_vop_plane_state(state);
855         const struct vop_win_data *win = vop_win->data;
856         struct vop *vop = to_vop(state->crtc);
857         struct drm_framebuffer *fb = state->fb;
858         unsigned int actual_w, actual_h;
859         unsigned int dsp_stx, dsp_sty;
860         uint32_t act_info, dsp_info, dsp_st;
861         struct drm_rect *src = &vop_plane_state->src;
862         struct drm_rect *dest = &vop_plane_state->dest;
863         struct drm_gem_object *obj, *uv_obj;
864         struct rockchip_gem_object *rk_obj, *rk_uv_obj;
865         unsigned long offset;
866         dma_addr_t dma_addr;
867         uint32_t val;
868         bool rb_swap;
869
870         /*
871          * can't update plane when vop is disabled.
872          */
873         if (!crtc)
874                 return;
875
876         if (WARN_ON(!vop->is_enabled))
877                 return;
878
879         if (!vop_plane_state->enable) {
880                 vop_plane_atomic_disable(plane, old_state);
881                 return;
882         }
883
884         obj = rockchip_fb_get_gem_obj(fb, 0);
885         rk_obj = to_rockchip_obj(obj);
886
887         actual_w = drm_rect_width(src) >> 16;
888         actual_h = drm_rect_height(src) >> 16;
889         act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff);
890
891         dsp_info = (drm_rect_height(dest) - 1) << 16;
892         dsp_info |= (drm_rect_width(dest) - 1) & 0xffff;
893
894         dsp_stx = dest->x1 + crtc->mode.htotal - crtc->mode.hsync_start;
895         dsp_sty = dest->y1 + crtc->mode.vtotal - crtc->mode.vsync_start;
896         dsp_st = dsp_sty << 16 | (dsp_stx & 0xffff);
897
898         offset = (src->x1 >> 16) * drm_format_plane_cpp(fb->pixel_format, 0);
899         offset += (src->y1 >> 16) * fb->pitches[0];
900         vop_plane_state->yrgb_mst = rk_obj->dma_addr + offset + fb->offsets[0];
901
902         spin_lock(&vop->reg_lock);
903
904         VOP_WIN_SET(vop, win, format, vop_plane_state->format);
905         VOP_WIN_SET(vop, win, yrgb_vir, fb->pitches[0] >> 2);
906         VOP_WIN_SET(vop, win, yrgb_mst, vop_plane_state->yrgb_mst);
907         if (is_yuv_support(fb->pixel_format)) {
908                 int hsub = drm_format_horz_chroma_subsampling(fb->pixel_format);
909                 int vsub = drm_format_vert_chroma_subsampling(fb->pixel_format);
910                 int bpp = drm_format_plane_cpp(fb->pixel_format, 1);
911
912                 uv_obj = rockchip_fb_get_gem_obj(fb, 1);
913                 rk_uv_obj = to_rockchip_obj(uv_obj);
914
915                 offset = (src->x1 >> 16) * bpp / hsub;
916                 offset += (src->y1 >> 16) * fb->pitches[1] / vsub;
917
918                 dma_addr = rk_uv_obj->dma_addr + offset + fb->offsets[1];
919                 VOP_WIN_SET(vop, win, uv_vir, fb->pitches[1] >> 2);
920                 VOP_WIN_SET(vop, win, uv_mst, dma_addr);
921         }
922
923         if (win->phy->scl)
924                 scl_vop_cal_scl_fac(vop, win, actual_w, actual_h,
925                                     drm_rect_width(dest), drm_rect_height(dest),
926                                     fb->pixel_format);
927
928         VOP_WIN_SET(vop, win, act_info, act_info);
929         VOP_WIN_SET(vop, win, dsp_info, dsp_info);
930         VOP_WIN_SET(vop, win, dsp_st, dsp_st);
931
932         rb_swap = has_rb_swapped(fb->pixel_format);
933         VOP_WIN_SET(vop, win, rb_swap, rb_swap);
934
935         if (is_alpha_support(fb->pixel_format)) {
936                 VOP_WIN_SET(vop, win, dst_alpha_ctl,
937                             DST_FACTOR_M0(ALPHA_SRC_INVERSE));
938                 val = SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL) |
939                         SRC_ALPHA_M0(ALPHA_STRAIGHT) |
940                         SRC_BLEND_M0(ALPHA_PER_PIX) |
941                         SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION) |
942                         SRC_FACTOR_M0(ALPHA_ONE);
943                 VOP_WIN_SET(vop, win, src_alpha_ctl, val);
944         } else {
945                 VOP_WIN_SET(vop, win, src_alpha_ctl, SRC_ALPHA_EN(0));
946         }
947
948         VOP_WIN_SET(vop, win, enable, 1);
949         spin_unlock(&vop->reg_lock);
950 }
951
952 static const struct drm_plane_helper_funcs plane_helper_funcs = {
953         .atomic_check = vop_plane_atomic_check,
954         .atomic_update = vop_plane_atomic_update,
955         .atomic_disable = vop_plane_atomic_disable,
956 };
957
958 void vop_atomic_plane_reset(struct drm_plane *plane)
959 {
960         struct vop_plane_state *vop_plane_state =
961                                         to_vop_plane_state(plane->state);
962
963         if (plane->state && plane->state->fb)
964                 drm_framebuffer_unreference(plane->state->fb);
965
966         kfree(vop_plane_state);
967         vop_plane_state = kzalloc(sizeof(*vop_plane_state), GFP_KERNEL);
968         if (!vop_plane_state)
969                 return;
970
971         plane->state = &vop_plane_state->base;
972         plane->state->plane = plane;
973 }
974
975 struct drm_plane_state *
976 vop_atomic_plane_duplicate_state(struct drm_plane *plane)
977 {
978         struct vop_plane_state *old_vop_plane_state;
979         struct vop_plane_state *vop_plane_state;
980
981         if (WARN_ON(!plane->state))
982                 return NULL;
983
984         old_vop_plane_state = to_vop_plane_state(plane->state);
985         vop_plane_state = kmemdup(old_vop_plane_state,
986                                   sizeof(*vop_plane_state), GFP_KERNEL);
987         if (!vop_plane_state)
988                 return NULL;
989
990         __drm_atomic_helper_plane_duplicate_state(plane,
991                                                   &vop_plane_state->base);
992
993         return &vop_plane_state->base;
994 }
995
996 static void vop_atomic_plane_destroy_state(struct drm_plane *plane,
997                                            struct drm_plane_state *state)
998 {
999         struct vop_plane_state *vop_state = to_vop_plane_state(state);
1000
1001         __drm_atomic_helper_plane_destroy_state(plane, state);
1002
1003         kfree(vop_state);
1004 }
1005
1006 static const struct drm_plane_funcs vop_plane_funcs = {
1007         .update_plane   = drm_atomic_helper_update_plane,
1008         .disable_plane  = drm_atomic_helper_disable_plane,
1009         .destroy = vop_plane_destroy,
1010         .reset = vop_atomic_plane_reset,
1011         .atomic_duplicate_state = vop_atomic_plane_duplicate_state,
1012         .atomic_destroy_state = vop_atomic_plane_destroy_state,
1013 };
1014
1015 int rockchip_drm_crtc_mode_config(struct drm_crtc *crtc,
1016                                   int connector_type,
1017                                   int out_mode)
1018 {
1019         struct vop *vop = to_vop(crtc);
1020
1021         vop->connector_type = connector_type;
1022         vop->connector_out_mode = out_mode;
1023
1024         return 0;
1025 }
1026 EXPORT_SYMBOL_GPL(rockchip_drm_crtc_mode_config);
1027
1028 static int vop_crtc_enable_vblank(struct drm_crtc *crtc)
1029 {
1030         struct vop *vop = to_vop(crtc);
1031         unsigned long flags;
1032
1033         if (WARN_ON(!vop->is_enabled))
1034                 return -EPERM;
1035
1036         spin_lock_irqsave(&vop->irq_lock, flags);
1037
1038         vop_mask_write(vop, INTR_CTRL0, FS_INTR_MASK, FS_INTR_EN(1));
1039
1040         spin_unlock_irqrestore(&vop->irq_lock, flags);
1041
1042         return 0;
1043 }
1044
1045 static void vop_crtc_disable_vblank(struct drm_crtc *crtc)
1046 {
1047         struct vop *vop = to_vop(crtc);
1048         unsigned long flags;
1049
1050         if (WARN_ON(!vop->is_enabled))
1051                 return;
1052
1053         spin_lock_irqsave(&vop->irq_lock, flags);
1054         vop_mask_write(vop, INTR_CTRL0, FS_INTR_MASK, FS_INTR_EN(0));
1055         spin_unlock_irqrestore(&vop->irq_lock, flags);
1056 }
1057
1058 static void vop_crtc_wait_for_update(struct drm_crtc *crtc)
1059 {
1060         struct vop *vop = to_vop(crtc);
1061
1062         reinit_completion(&vop->wait_update_complete);
1063         WARN_ON(!wait_for_completion_timeout(&vop->wait_update_complete, 100));
1064 }
1065
1066 static const struct rockchip_crtc_funcs private_crtc_funcs = {
1067         .enable_vblank = vop_crtc_enable_vblank,
1068         .disable_vblank = vop_crtc_disable_vblank,
1069         .wait_for_update = vop_crtc_wait_for_update,
1070 };
1071
1072 static bool vop_crtc_mode_fixup(struct drm_crtc *crtc,
1073                                 const struct drm_display_mode *mode,
1074                                 struct drm_display_mode *adjusted_mode)
1075 {
1076         if (adjusted_mode->htotal == 0 || adjusted_mode->vtotal == 0)
1077                 return false;
1078
1079         return true;
1080 }
1081
1082 static void vop_crtc_enable(struct drm_crtc *crtc)
1083 {
1084         struct vop *vop = to_vop(crtc);
1085         struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
1086         u16 hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
1087         u16 hdisplay = adjusted_mode->hdisplay;
1088         u16 htotal = adjusted_mode->htotal;
1089         u16 hact_st = adjusted_mode->htotal - adjusted_mode->hsync_start;
1090         u16 hact_end = hact_st + hdisplay;
1091         u16 vdisplay = adjusted_mode->vdisplay;
1092         u16 vtotal = adjusted_mode->vtotal;
1093         u16 vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
1094         u16 vact_st = adjusted_mode->vtotal - adjusted_mode->vsync_start;
1095         u16 vact_end = vact_st + vdisplay;
1096         uint32_t val;
1097
1098         vop_enable(crtc);
1099         /*
1100          * disable dclk to stop frame scan, so that we can safe config mode and
1101          * enable iommu.
1102          */
1103         clk_disable(vop->dclk);
1104
1105         switch (vop->connector_type) {
1106         case DRM_MODE_CONNECTOR_LVDS:
1107                 VOP_CTRL_SET(vop, rgb_en, 1);
1108                 break;
1109         case DRM_MODE_CONNECTOR_eDP:
1110                 VOP_CTRL_SET(vop, edp_en, 1);
1111                 break;
1112         case DRM_MODE_CONNECTOR_HDMIA:
1113                 VOP_CTRL_SET(vop, hdmi_en, 1);
1114                 break;
1115         default:
1116                 DRM_ERROR("unsupport connector_type[%d]\n",
1117                           vop->connector_type);
1118                 goto out;
1119         };
1120         VOP_CTRL_SET(vop, out_mode, vop->connector_out_mode);
1121
1122         val = 0x8;
1123         val |= (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC) ? 0 : 1;
1124         val |= (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC) ? 0 : (1 << 1);
1125         VOP_CTRL_SET(vop, pin_pol, val);
1126
1127         VOP_CTRL_SET(vop, htotal_pw, (htotal << 16) | hsync_len);
1128         val = hact_st << 16;
1129         val |= hact_end;
1130         VOP_CTRL_SET(vop, hact_st_end, val);
1131         VOP_CTRL_SET(vop, hpost_st_end, val);
1132
1133         VOP_CTRL_SET(vop, vtotal_pw, (vtotal << 16) | vsync_len);
1134         val = vact_st << 16;
1135         val |= vact_end;
1136         VOP_CTRL_SET(vop, vact_st_end, val);
1137         VOP_CTRL_SET(vop, vpost_st_end, val);
1138
1139
1140         /*
1141          * reset dclk, take all mode config affect, so the clk would run in
1142          * correct frame.
1143          */
1144         reset_control_assert(vop->dclk_rst);
1145         usleep_range(10, 20);
1146         reset_control_deassert(vop->dclk_rst);
1147
1148         clk_set_rate(vop->dclk, adjusted_mode->clock * 1000);
1149 out:
1150         if (clk_enable(vop->dclk) < 0)
1151                 dev_err(vop->dev, "failed to enable dclk\n");
1152 }
1153
1154 static void vop_crtc_atomic_flush(struct drm_crtc *crtc,
1155                                   struct drm_crtc_state *old_crtc_state)
1156 {
1157         struct vop *vop = to_vop(crtc);
1158
1159         if (WARN_ON(!vop->is_enabled))
1160                 return;
1161
1162         spin_lock(&vop->reg_lock);
1163
1164         vop_cfg_done(vop);
1165
1166         spin_unlock(&vop->reg_lock);
1167 }
1168
1169 static void vop_crtc_atomic_begin(struct drm_crtc *crtc,
1170                                   struct drm_crtc_state *old_crtc_state)
1171 {
1172         struct vop *vop = to_vop(crtc);
1173
1174         if (crtc->state->event) {
1175                 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
1176
1177                 vop->event = crtc->state->event;
1178                 crtc->state->event = NULL;
1179         }
1180 }
1181
1182 static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = {
1183         .enable = vop_crtc_enable,
1184         .disable = vop_crtc_disable,
1185         .mode_fixup = vop_crtc_mode_fixup,
1186         .atomic_flush = vop_crtc_atomic_flush,
1187         .atomic_begin = vop_crtc_atomic_begin,
1188 };
1189
1190 static void vop_crtc_destroy(struct drm_crtc *crtc)
1191 {
1192         drm_crtc_cleanup(crtc);
1193 }
1194
1195 static const struct drm_crtc_funcs vop_crtc_funcs = {
1196         .set_config = drm_atomic_helper_set_config,
1197         .page_flip = drm_atomic_helper_page_flip,
1198         .destroy = vop_crtc_destroy,
1199         .reset = drm_atomic_helper_crtc_reset,
1200         .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
1201         .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
1202 };
1203
1204 static bool vop_win_pending_is_complete(struct vop_win *vop_win)
1205 {
1206         struct drm_plane *plane = &vop_win->base;
1207         struct vop_plane_state *state = to_vop_plane_state(plane->state);
1208         dma_addr_t yrgb_mst;
1209
1210         if (!state->enable)
1211                 return VOP_WIN_GET(vop_win->vop, vop_win->data, enable) == 0;
1212
1213         yrgb_mst = VOP_WIN_GET_YRGBADDR(vop_win->vop, vop_win->data);
1214
1215         return yrgb_mst == state->yrgb_mst;
1216 }
1217
1218 static void vop_handle_vblank(struct vop *vop)
1219 {
1220         struct drm_device *drm = vop->drm_dev;
1221         struct drm_crtc *crtc = &vop->crtc;
1222         unsigned long flags;
1223         int i;
1224
1225         for (i = 0; i < vop->data->win_size; i++) {
1226                 if (!vop_win_pending_is_complete(&vop->win[i]))
1227                         return;
1228         }
1229
1230         if (vop->event) {
1231                 spin_lock_irqsave(&drm->event_lock, flags);
1232
1233                 drm_crtc_send_vblank_event(crtc, vop->event);
1234                 drm_crtc_vblank_put(crtc);
1235                 vop->event = NULL;
1236
1237                 spin_unlock_irqrestore(&drm->event_lock, flags);
1238         }
1239         if (!completion_done(&vop->wait_update_complete))
1240                 complete(&vop->wait_update_complete);
1241 }
1242
1243 static irqreturn_t vop_isr(int irq, void *data)
1244 {
1245         struct vop *vop = data;
1246         struct drm_crtc *crtc = &vop->crtc;
1247         uint32_t intr0_reg, active_irqs;
1248         unsigned long flags;
1249         int ret = IRQ_NONE;
1250
1251         /*
1252          * INTR_CTRL0 register has interrupt status, enable and clear bits, we
1253          * must hold irq_lock to avoid a race with enable/disable_vblank().
1254         */
1255         spin_lock_irqsave(&vop->irq_lock, flags);
1256         intr0_reg = vop_readl(vop, INTR_CTRL0);
1257         active_irqs = intr0_reg & INTR_MASK;
1258         /* Clear all active interrupt sources */
1259         if (active_irqs)
1260                 vop_writel(vop, INTR_CTRL0,
1261                            intr0_reg | (active_irqs << INTR_CLR_SHIFT));
1262         spin_unlock_irqrestore(&vop->irq_lock, flags);
1263
1264         /* This is expected for vop iommu irqs, since the irq is shared */
1265         if (!active_irqs)
1266                 return IRQ_NONE;
1267
1268         if (active_irqs & DSP_HOLD_VALID_INTR) {
1269                 complete(&vop->dsp_hold_completion);
1270                 active_irqs &= ~DSP_HOLD_VALID_INTR;
1271                 ret = IRQ_HANDLED;
1272         }
1273
1274         if (active_irqs & FS_INTR) {
1275                 drm_crtc_handle_vblank(crtc);
1276                 vop_handle_vblank(vop);
1277                 active_irqs &= ~FS_INTR;
1278                 ret = IRQ_HANDLED;
1279         }
1280
1281         /* Unhandled irqs are spurious. */
1282         if (active_irqs)
1283                 DRM_ERROR("Unknown VOP IRQs: %#02x\n", active_irqs);
1284
1285         return ret;
1286 }
1287
1288 static int vop_create_crtc(struct vop *vop)
1289 {
1290         const struct vop_data *vop_data = vop->data;
1291         struct device *dev = vop->dev;
1292         struct drm_device *drm_dev = vop->drm_dev;
1293         struct drm_plane *primary = NULL, *cursor = NULL, *plane;
1294         struct drm_crtc *crtc = &vop->crtc;
1295         struct device_node *port;
1296         int ret;
1297         int i;
1298
1299         /*
1300          * Create drm_plane for primary and cursor planes first, since we need
1301          * to pass them to drm_crtc_init_with_planes, which sets the
1302          * "possible_crtcs" to the newly initialized crtc.
1303          */
1304         for (i = 0; i < vop_data->win_size; i++) {
1305                 struct vop_win *vop_win = &vop->win[i];
1306                 const struct vop_win_data *win_data = vop_win->data;
1307
1308                 if (win_data->type != DRM_PLANE_TYPE_PRIMARY &&
1309                     win_data->type != DRM_PLANE_TYPE_CURSOR)
1310                         continue;
1311
1312                 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1313                                                0, &vop_plane_funcs,
1314                                                win_data->phy->data_formats,
1315                                                win_data->phy->nformats,
1316                                                win_data->type, NULL);
1317                 if (ret) {
1318                         DRM_ERROR("failed to initialize plane\n");
1319                         goto err_cleanup_planes;
1320                 }
1321
1322                 plane = &vop_win->base;
1323                 drm_plane_helper_add(plane, &plane_helper_funcs);
1324                 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1325                         primary = plane;
1326                 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
1327                         cursor = plane;
1328         }
1329
1330         ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
1331                                         &vop_crtc_funcs, NULL);
1332         if (ret)
1333                 return ret;
1334
1335         drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs);
1336
1337         /*
1338          * Create drm_planes for overlay windows with possible_crtcs restricted
1339          * to the newly created crtc.
1340          */
1341         for (i = 0; i < vop_data->win_size; i++) {
1342                 struct vop_win *vop_win = &vop->win[i];
1343                 const struct vop_win_data *win_data = vop_win->data;
1344                 unsigned long possible_crtcs = 1 << drm_crtc_index(crtc);
1345
1346                 if (win_data->type != DRM_PLANE_TYPE_OVERLAY)
1347                         continue;
1348
1349                 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1350                                                possible_crtcs,
1351                                                &vop_plane_funcs,
1352                                                win_data->phy->data_formats,
1353                                                win_data->phy->nformats,
1354                                                win_data->type, NULL);
1355                 if (ret) {
1356                         DRM_ERROR("failed to initialize overlay plane\n");
1357                         goto err_cleanup_crtc;
1358                 }
1359                 drm_plane_helper_add(&vop_win->base, &plane_helper_funcs);
1360         }
1361
1362         port = of_get_child_by_name(dev->of_node, "port");
1363         if (!port) {
1364                 DRM_ERROR("no port node found in %s\n",
1365                           dev->of_node->full_name);
1366                 goto err_cleanup_crtc;
1367         }
1368
1369         init_completion(&vop->dsp_hold_completion);
1370         init_completion(&vop->wait_update_complete);
1371         crtc->port = port;
1372         rockchip_register_crtc_funcs(crtc, &private_crtc_funcs);
1373
1374         return 0;
1375
1376 err_cleanup_crtc:
1377         drm_crtc_cleanup(crtc);
1378 err_cleanup_planes:
1379         list_for_each_entry(plane, &drm_dev->mode_config.plane_list, head)
1380                 drm_plane_cleanup(plane);
1381         return ret;
1382 }
1383
1384 static void vop_destroy_crtc(struct vop *vop)
1385 {
1386         struct drm_crtc *crtc = &vop->crtc;
1387
1388         rockchip_unregister_crtc_funcs(crtc);
1389         of_node_put(crtc->port);
1390         drm_crtc_cleanup(crtc);
1391 }
1392
1393 static int vop_initial(struct vop *vop)
1394 {
1395         const struct vop_data *vop_data = vop->data;
1396         const struct vop_reg_data *init_table = vop_data->init_table;
1397         struct reset_control *ahb_rst;
1398         int i, ret;
1399
1400         vop->hclk = devm_clk_get(vop->dev, "hclk_vop");
1401         if (IS_ERR(vop->hclk)) {
1402                 dev_err(vop->dev, "failed to get hclk source\n");
1403                 return PTR_ERR(vop->hclk);
1404         }
1405         vop->aclk = devm_clk_get(vop->dev, "aclk_vop");
1406         if (IS_ERR(vop->aclk)) {
1407                 dev_err(vop->dev, "failed to get aclk source\n");
1408                 return PTR_ERR(vop->aclk);
1409         }
1410         vop->dclk = devm_clk_get(vop->dev, "dclk_vop");
1411         if (IS_ERR(vop->dclk)) {
1412                 dev_err(vop->dev, "failed to get dclk source\n");
1413                 return PTR_ERR(vop->dclk);
1414         }
1415
1416         ret = clk_prepare(vop->dclk);
1417         if (ret < 0) {
1418                 dev_err(vop->dev, "failed to prepare dclk\n");
1419                 return ret;
1420         }
1421
1422         /* Enable both the hclk and aclk to setup the vop */
1423         ret = clk_prepare_enable(vop->hclk);
1424         if (ret < 0) {
1425                 dev_err(vop->dev, "failed to prepare/enable hclk\n");
1426                 goto err_unprepare_dclk;
1427         }
1428
1429         ret = clk_prepare_enable(vop->aclk);
1430         if (ret < 0) {
1431                 dev_err(vop->dev, "failed to prepare/enable aclk\n");
1432                 goto err_disable_hclk;
1433         }
1434
1435         /*
1436          * do hclk_reset, reset all vop registers.
1437          */
1438         ahb_rst = devm_reset_control_get(vop->dev, "ahb");
1439         if (IS_ERR(ahb_rst)) {
1440                 dev_err(vop->dev, "failed to get ahb reset\n");
1441                 ret = PTR_ERR(ahb_rst);
1442                 goto err_disable_aclk;
1443         }
1444         reset_control_assert(ahb_rst);
1445         usleep_range(10, 20);
1446         reset_control_deassert(ahb_rst);
1447
1448         memcpy(vop->regsbak, vop->regs, vop->len);
1449
1450         for (i = 0; i < vop_data->table_size; i++)
1451                 vop_writel(vop, init_table[i].offset, init_table[i].value);
1452
1453         for (i = 0; i < vop_data->win_size; i++) {
1454                 const struct vop_win_data *win = &vop_data->win[i];
1455
1456                 VOP_WIN_SET(vop, win, enable, 0);
1457         }
1458
1459         vop_cfg_done(vop);
1460
1461         /*
1462          * do dclk_reset, let all config take affect.
1463          */
1464         vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk");
1465         if (IS_ERR(vop->dclk_rst)) {
1466                 dev_err(vop->dev, "failed to get dclk reset\n");
1467                 ret = PTR_ERR(vop->dclk_rst);
1468                 goto err_disable_aclk;
1469         }
1470         reset_control_assert(vop->dclk_rst);
1471         usleep_range(10, 20);
1472         reset_control_deassert(vop->dclk_rst);
1473
1474         clk_disable(vop->hclk);
1475         clk_disable(vop->aclk);
1476
1477         vop->is_enabled = false;
1478
1479         return 0;
1480
1481 err_disable_aclk:
1482         clk_disable_unprepare(vop->aclk);
1483 err_disable_hclk:
1484         clk_disable_unprepare(vop->hclk);
1485 err_unprepare_dclk:
1486         clk_unprepare(vop->dclk);
1487         return ret;
1488 }
1489
1490 /*
1491  * Initialize the vop->win array elements.
1492  */
1493 static void vop_win_init(struct vop *vop)
1494 {
1495         const struct vop_data *vop_data = vop->data;
1496         unsigned int i;
1497
1498         for (i = 0; i < vop_data->win_size; i++) {
1499                 struct vop_win *vop_win = &vop->win[i];
1500                 const struct vop_win_data *win_data = &vop_data->win[i];
1501
1502                 vop_win->data = win_data;
1503                 vop_win->vop = vop;
1504         }
1505 }
1506
1507 static int vop_bind(struct device *dev, struct device *master, void *data)
1508 {
1509         struct platform_device *pdev = to_platform_device(dev);
1510         const struct of_device_id *of_id;
1511         const struct vop_data *vop_data;
1512         struct drm_device *drm_dev = data;
1513         struct vop *vop;
1514         struct resource *res;
1515         size_t alloc_size;
1516         int ret, irq;
1517
1518         of_id = of_match_device(vop_driver_dt_match, dev);
1519         vop_data = of_id->data;
1520         if (!vop_data)
1521                 return -ENODEV;
1522
1523         /* Allocate vop struct and its vop_win array */
1524         alloc_size = sizeof(*vop) + sizeof(*vop->win) * vop_data->win_size;
1525         vop = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
1526         if (!vop)
1527                 return -ENOMEM;
1528
1529         vop->dev = dev;
1530         vop->data = vop_data;
1531         vop->drm_dev = drm_dev;
1532         dev_set_drvdata(dev, vop);
1533
1534         vop_win_init(vop);
1535
1536         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1537         vop->len = resource_size(res);
1538         vop->regs = devm_ioremap_resource(dev, res);
1539         if (IS_ERR(vop->regs))
1540                 return PTR_ERR(vop->regs);
1541
1542         vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL);
1543         if (!vop->regsbak)
1544                 return -ENOMEM;
1545
1546         ret = vop_initial(vop);
1547         if (ret < 0) {
1548                 dev_err(&pdev->dev, "cannot initial vop dev - err %d\n", ret);
1549                 return ret;
1550         }
1551
1552         irq = platform_get_irq(pdev, 0);
1553         if (irq < 0) {
1554                 dev_err(dev, "cannot find irq for vop\n");
1555                 return irq;
1556         }
1557         vop->irq = (unsigned int)irq;
1558
1559         spin_lock_init(&vop->reg_lock);
1560         spin_lock_init(&vop->irq_lock);
1561
1562         mutex_init(&vop->vsync_mutex);
1563
1564         ret = devm_request_irq(dev, vop->irq, vop_isr,
1565                                IRQF_SHARED, dev_name(dev), vop);
1566         if (ret)
1567                 return ret;
1568
1569         /* IRQ is initially disabled; it gets enabled in power_on */
1570         disable_irq(vop->irq);
1571
1572         ret = vop_create_crtc(vop);
1573         if (ret)
1574                 return ret;
1575
1576         pm_runtime_enable(&pdev->dev);
1577         return 0;
1578 }
1579
1580 static void vop_unbind(struct device *dev, struct device *master, void *data)
1581 {
1582         struct vop *vop = dev_get_drvdata(dev);
1583
1584         pm_runtime_disable(dev);
1585         vop_destroy_crtc(vop);
1586 }
1587
1588 static const struct component_ops vop_component_ops = {
1589         .bind = vop_bind,
1590         .unbind = vop_unbind,
1591 };
1592
1593 static int vop_probe(struct platform_device *pdev)
1594 {
1595         struct device *dev = &pdev->dev;
1596
1597         if (!dev->of_node) {
1598                 dev_err(dev, "can't find vop devices\n");
1599                 return -ENODEV;
1600         }
1601
1602         return component_add(dev, &vop_component_ops);
1603 }
1604
1605 static int vop_remove(struct platform_device *pdev)
1606 {
1607         component_del(&pdev->dev, &vop_component_ops);
1608
1609         return 0;
1610 }
1611
1612 struct platform_driver vop_platform_driver = {
1613         .probe = vop_probe,
1614         .remove = vop_remove,
1615         .driver = {
1616                 .name = "rockchip-vop",
1617                 .owner = THIS_MODULE,
1618                 .of_match_table = of_match_ptr(vop_driver_dt_match),
1619         },
1620 };
1621
1622 module_platform_driver(vop_platform_driver);
1623
1624 MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
1625 MODULE_DESCRIPTION("ROCKCHIP VOP Driver");
1626 MODULE_LICENSE("GPL v2");