blob: ab18659e2a6fc4285208a79694ff92813406cbe2 [file] [log] [blame]
Mark Yao2048e322014-08-22 18:36:26 +08001/*
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>
Mark Yao63ebb9f2015-11-30 18:22:42 +080017#include <drm/drm_atomic.h>
Mark Yao2048e322014-08-22 18:36:26 +080018#include <drm/drm_crtc.h>
19#include <drm/drm_crtc_helper.h>
Tomasz Figa47a7eb42016-09-14 21:54:57 +090020#include <drm/drm_flip_work.h>
Mark Yao2048e322014-08-22 18:36:26 +080021#include <drm/drm_plane_helper.h>
Sean Paul6cca3862017-03-06 15:02:26 -050022#ifdef CONFIG_DRM_ANALOGIX_DP
Tomeu Vizoso3190e582017-03-03 14:39:36 +010023#include <drm/bridge/analogix_dp.h>
Sean Paul6cca3862017-03-06 15:02:26 -050024#endif
Mark Yao2048e322014-08-22 18:36:26 +080025
26#include <linux/kernel.h>
Paul Gortmaker00fe6142015-05-01 20:02:30 -040027#include <linux/module.h>
Mark Yao2048e322014-08-22 18:36:26 +080028#include <linux/platform_device.h>
29#include <linux/clk.h>
Tomasz Figa7caecdb2016-09-14 21:54:56 +090030#include <linux/iopoll.h>
Mark Yao2048e322014-08-22 18:36:26 +080031#include <linux/of.h>
32#include <linux/of_device.h>
33#include <linux/pm_runtime.h>
34#include <linux/component.h>
35
36#include <linux/reset.h>
37#include <linux/delay.h>
38
39#include "rockchip_drm_drv.h"
40#include "rockchip_drm_gem.h"
41#include "rockchip_drm_fb.h"
Yakir Yang5182c1a2016-07-24 14:57:44 +080042#include "rockchip_drm_psr.h"
Mark Yao2048e322014-08-22 18:36:26 +080043#include "rockchip_drm_vop.h"
44
Mark Yao2048e322014-08-22 18:36:26 +080045#define VOP_WIN_SET(x, win, name, v) \
Mark yao9a61c542017-07-28 14:06:25 +080046 vop_reg_set(vop, &win->phy->name, win->base, ~0, v, #name)
Mark Yao4c156c22015-06-26 17:14:46 +080047#define VOP_SCL_SET(x, win, name, v) \
Mark yao9a61c542017-07-28 14:06:25 +080048 vop_reg_set(vop, &win->phy->scl->name, win->base, ~0, v, #name)
Mark Yao1194fff2015-12-15 09:08:43 +080049#define VOP_SCL_SET_EXT(x, win, name, v) \
Mark yao9a61c542017-07-28 14:06:25 +080050 vop_reg_set(vop, &win->phy->scl->ext->name, \
51 win->base, ~0, v, #name)
Mark yaoac6560d2017-07-26 14:19:19 +080052
53#define VOP_INTR_SET_MASK(vop, name, mask, v) \
Mark yao9a61c542017-07-28 14:06:25 +080054 vop_reg_set(vop, &vop->data->intr->name, 0, mask, v, #name)
55
56#define VOP_REG_SET(vop, group, name, v) \
57 vop_reg_set(vop, &vop->data->group->name, 0, ~0, v, #name)
Mark yaoac6560d2017-07-26 14:19:19 +080058
Mark Yaodbb3d942015-12-15 08:36:55 +080059#define VOP_INTR_SET_TYPE(vop, name, type, v) \
60 do { \
John Keepingc7647f82016-01-12 18:05:18 +000061 int i, reg = 0, mask = 0; \
Mark Yaodbb3d942015-12-15 08:36:55 +080062 for (i = 0; i < vop->data->intr->nintrs; i++) { \
John Keepingc7647f82016-01-12 18:05:18 +000063 if (vop->data->intr->intrs[i] & type) { \
Mark Yaodbb3d942015-12-15 08:36:55 +080064 reg |= (v) << i; \
John Keepingc7647f82016-01-12 18:05:18 +000065 mask |= 1 << i; \
66 } \
Mark Yaodbb3d942015-12-15 08:36:55 +080067 } \
Mark yaoac6560d2017-07-26 14:19:19 +080068 VOP_INTR_SET_MASK(vop, name, mask, reg); \
Mark Yaodbb3d942015-12-15 08:36:55 +080069 } while (0)
70#define VOP_INTR_GET_TYPE(vop, name, type) \
71 vop_get_intr_type(vop, &vop->data->intr->name, type)
72
Mark Yao2048e322014-08-22 18:36:26 +080073#define VOP_WIN_GET(x, win, name) \
Mark yao9a61c542017-07-28 14:06:25 +080074 vop_read_reg(x, win->offset, win->phy->name)
Mark Yao2048e322014-08-22 18:36:26 +080075
76#define VOP_WIN_GET_YRGBADDR(vop, win) \
77 vop_readl(vop, win->base + win->phy->yrgb_mst.offset)
78
79#define to_vop(x) container_of(x, struct vop, crtc)
80#define to_vop_win(x) container_of(x, struct vop_win, base)
81
Tomasz Figa47a7eb42016-09-14 21:54:57 +090082enum vop_pending {
83 VOP_PENDING_FB_UNREF,
84};
85
Mark Yao2048e322014-08-22 18:36:26 +080086struct vop_win {
87 struct drm_plane base;
88 const struct vop_win_data *data;
89 struct vop *vop;
Mark Yao2048e322014-08-22 18:36:26 +080090};
91
92struct vop {
93 struct drm_crtc crtc;
94 struct device *dev;
95 struct drm_device *drm_dev;
Mark Yao31e980c2015-01-22 14:37:56 +080096 bool is_enabled;
Mark Yao2048e322014-08-22 18:36:26 +080097
Mark Yao2048e322014-08-22 18:36:26 +080098 /* mutex vsync_ work */
99 struct mutex vsync_mutex;
100 bool vsync_work_pending;
Mark Yao10672192015-02-04 13:10:31 +0800101 struct completion dsp_hold_completion;
Daniel Vetter4f9d39a2016-06-08 14:19:11 +0200102
103 /* protected by dev->event_lock */
Mark Yao63ebb9f2015-11-30 18:22:42 +0800104 struct drm_pending_vblank_event *event;
Mark Yao2048e322014-08-22 18:36:26 +0800105
Tomasz Figa47a7eb42016-09-14 21:54:57 +0900106 struct drm_flip_work fb_unref_work;
107 unsigned long pending;
108
Yakir Yang69c34e42016-07-24 14:57:40 +0800109 struct completion line_flag_completion;
110
Mark Yao2048e322014-08-22 18:36:26 +0800111 const struct vop_data *data;
112
113 uint32_t *regsbak;
114 void __iomem *regs;
115
116 /* physical map length of vop register */
117 uint32_t len;
118
119 /* one time only one process allowed to config the register */
120 spinlock_t reg_lock;
121 /* lock vop irq reg */
122 spinlock_t irq_lock;
123
124 unsigned int irq;
125
126 /* vop AHP clk */
127 struct clk *hclk;
128 /* vop dclk */
129 struct clk *dclk;
130 /* vop share memory frequency */
131 struct clk *aclk;
132
133 /* vop dclk reset */
134 struct reset_control *dclk_rst;
135
Mark Yao2048e322014-08-22 18:36:26 +0800136 struct vop_win win[];
137};
138
Mark Yao2048e322014-08-22 18:36:26 +0800139static inline void vop_writel(struct vop *vop, uint32_t offset, uint32_t v)
140{
141 writel(v, vop->regs + offset);
142 vop->regsbak[offset >> 2] = v;
143}
144
145static inline uint32_t vop_readl(struct vop *vop, uint32_t offset)
146{
147 return readl(vop->regs + offset);
148}
149
150static inline uint32_t vop_read_reg(struct vop *vop, uint32_t base,
151 const struct vop_reg *reg)
152{
153 return (vop_readl(vop, base + reg->offset) >> reg->shift) & reg->mask;
154}
155
Mark yao9a61c542017-07-28 14:06:25 +0800156static void vop_reg_set(struct vop *vop, const struct vop_reg *reg,
157 uint32_t _offset, uint32_t _mask, uint32_t v,
158 const char *reg_name)
Mark Yao2048e322014-08-22 18:36:26 +0800159{
Mark yao9a61c542017-07-28 14:06:25 +0800160 int offset, mask, shift;
Mark Yaod49463e2016-04-20 14:18:15 +0800161
Mark yao9a61c542017-07-28 14:06:25 +0800162 if (!reg || !reg->mask) {
163 dev_dbg(vop->dev, "Warning: not support %s\n", reg_name);
164 return;
165 }
166
167 offset = reg->offset + _offset;
168 mask = reg->mask & _mask;
169 shift = reg->shift;
170
171 if (reg->write_mask) {
Mark Yaod49463e2016-04-20 14:18:15 +0800172 v = ((v << shift) & 0xffff) | (mask << (shift + 16));
173 } else {
Mark Yao2048e322014-08-22 18:36:26 +0800174 uint32_t cached_val = vop->regsbak[offset >> 2];
175
Mark Yaod49463e2016-04-20 14:18:15 +0800176 v = (cached_val & ~(mask << shift)) | ((v & mask) << shift);
177 vop->regsbak[offset >> 2] = v;
Mark Yao2048e322014-08-22 18:36:26 +0800178 }
Mark Yao2048e322014-08-22 18:36:26 +0800179
Mark yao9a61c542017-07-28 14:06:25 +0800180 if (reg->relaxed)
Mark Yaod49463e2016-04-20 14:18:15 +0800181 writel_relaxed(v, vop->regs + offset);
182 else
183 writel(v, vop->regs + offset);
Mark Yao2048e322014-08-22 18:36:26 +0800184}
185
Mark Yaodbb3d942015-12-15 08:36:55 +0800186static inline uint32_t vop_get_intr_type(struct vop *vop,
187 const struct vop_reg *reg, int type)
188{
189 uint32_t i, ret = 0;
190 uint32_t regs = vop_read_reg(vop, 0, reg);
191
192 for (i = 0; i < vop->data->intr->nintrs; i++) {
193 if ((type & vop->data->intr->intrs[i]) && (regs & 1 << i))
194 ret |= vop->data->intr->intrs[i];
195 }
196
197 return ret;
198}
199
Mark Yao0cf33fe2015-12-14 18:14:36 +0800200static inline void vop_cfg_done(struct vop *vop)
201{
Mark yao9a61c542017-07-28 14:06:25 +0800202 VOP_REG_SET(vop, common, cfg_done, 1);
Mark Yao0cf33fe2015-12-14 18:14:36 +0800203}
204
Tomasz Figa85a359f2015-05-11 19:55:39 +0900205static bool has_rb_swapped(uint32_t format)
206{
207 switch (format) {
208 case DRM_FORMAT_XBGR8888:
209 case DRM_FORMAT_ABGR8888:
210 case DRM_FORMAT_BGR888:
211 case DRM_FORMAT_BGR565:
212 return true;
213 default:
214 return false;
215 }
216}
217
Mark Yao2048e322014-08-22 18:36:26 +0800218static enum vop_data_format vop_convert_format(uint32_t format)
219{
220 switch (format) {
221 case DRM_FORMAT_XRGB8888:
222 case DRM_FORMAT_ARGB8888:
Tomasz Figa85a359f2015-05-11 19:55:39 +0900223 case DRM_FORMAT_XBGR8888:
224 case DRM_FORMAT_ABGR8888:
Mark Yao2048e322014-08-22 18:36:26 +0800225 return VOP_FMT_ARGB8888;
226 case DRM_FORMAT_RGB888:
Tomasz Figa85a359f2015-05-11 19:55:39 +0900227 case DRM_FORMAT_BGR888:
Mark Yao2048e322014-08-22 18:36:26 +0800228 return VOP_FMT_RGB888;
229 case DRM_FORMAT_RGB565:
Tomasz Figa85a359f2015-05-11 19:55:39 +0900230 case DRM_FORMAT_BGR565:
Mark Yao2048e322014-08-22 18:36:26 +0800231 return VOP_FMT_RGB565;
232 case DRM_FORMAT_NV12:
233 return VOP_FMT_YUV420SP;
234 case DRM_FORMAT_NV16:
235 return VOP_FMT_YUV422SP;
236 case DRM_FORMAT_NV24:
237 return VOP_FMT_YUV444SP;
238 default:
Sean Paulee4d7892016-08-12 13:00:54 -0400239 DRM_ERROR("unsupported format[%08x]\n", format);
Mark Yao2048e322014-08-22 18:36:26 +0800240 return -EINVAL;
241 }
242}
243
Mark Yao84c7f8c2015-07-20 16:16:49 +0800244static bool is_yuv_support(uint32_t format)
245{
246 switch (format) {
247 case DRM_FORMAT_NV12:
248 case DRM_FORMAT_NV16:
249 case DRM_FORMAT_NV24:
250 return true;
251 default:
252 return false;
253 }
254}
255
Mark Yao2048e322014-08-22 18:36:26 +0800256static bool is_alpha_support(uint32_t format)
257{
258 switch (format) {
259 case DRM_FORMAT_ARGB8888:
Tomasz Figa85a359f2015-05-11 19:55:39 +0900260 case DRM_FORMAT_ABGR8888:
Mark Yao2048e322014-08-22 18:36:26 +0800261 return true;
262 default:
263 return false;
264 }
265}
266
Mark Yao4c156c22015-06-26 17:14:46 +0800267static uint16_t scl_vop_cal_scale(enum scale_mode mode, uint32_t src,
268 uint32_t dst, bool is_horizontal,
269 int vsu_mode, int *vskiplines)
270{
271 uint16_t val = 1 << SCL_FT_DEFAULT_FIXPOINT_SHIFT;
272
273 if (is_horizontal) {
274 if (mode == SCALE_UP)
275 val = GET_SCL_FT_BIC(src, dst);
276 else if (mode == SCALE_DOWN)
277 val = GET_SCL_FT_BILI_DN(src, dst);
278 } else {
279 if (mode == SCALE_UP) {
280 if (vsu_mode == SCALE_UP_BIL)
281 val = GET_SCL_FT_BILI_UP(src, dst);
282 else
283 val = GET_SCL_FT_BIC(src, dst);
284 } else if (mode == SCALE_DOWN) {
285 if (vskiplines) {
286 *vskiplines = scl_get_vskiplines(src, dst);
287 val = scl_get_bili_dn_vskip(src, dst,
288 *vskiplines);
289 } else {
290 val = GET_SCL_FT_BILI_DN(src, dst);
291 }
292 }
293 }
294
295 return val;
296}
297
298static void scl_vop_cal_scl_fac(struct vop *vop, const struct vop_win_data *win,
299 uint32_t src_w, uint32_t src_h, uint32_t dst_w,
300 uint32_t dst_h, uint32_t pixel_format)
301{
302 uint16_t yrgb_hor_scl_mode, yrgb_ver_scl_mode;
303 uint16_t cbcr_hor_scl_mode = SCALE_NONE;
304 uint16_t cbcr_ver_scl_mode = SCALE_NONE;
305 int hsub = drm_format_horz_chroma_subsampling(pixel_format);
306 int vsub = drm_format_vert_chroma_subsampling(pixel_format);
307 bool is_yuv = is_yuv_support(pixel_format);
308 uint16_t cbcr_src_w = src_w / hsub;
309 uint16_t cbcr_src_h = src_h / vsub;
310 uint16_t vsu_mode;
311 uint16_t lb_mode;
312 uint32_t val;
Mark Yao2db00cf2016-04-29 15:39:53 +0800313 int vskiplines = 0;
Mark Yao4c156c22015-06-26 17:14:46 +0800314
315 if (dst_w > 3840) {
Sean Paulee4d7892016-08-12 13:00:54 -0400316 DRM_DEV_ERROR(vop->dev, "Maximum dst width (3840) exceeded\n");
Mark Yao4c156c22015-06-26 17:14:46 +0800317 return;
318 }
319
Mark Yao1194fff2015-12-15 09:08:43 +0800320 if (!win->phy->scl->ext) {
321 VOP_SCL_SET(vop, win, scale_yrgb_x,
322 scl_cal_scale2(src_w, dst_w));
323 VOP_SCL_SET(vop, win, scale_yrgb_y,
324 scl_cal_scale2(src_h, dst_h));
325 if (is_yuv) {
326 VOP_SCL_SET(vop, win, scale_cbcr_x,
Mark Yaoee8662f2016-06-06 15:58:46 +0800327 scl_cal_scale2(cbcr_src_w, dst_w));
Mark Yao1194fff2015-12-15 09:08:43 +0800328 VOP_SCL_SET(vop, win, scale_cbcr_y,
Mark Yaoee8662f2016-06-06 15:58:46 +0800329 scl_cal_scale2(cbcr_src_h, dst_h));
Mark Yao1194fff2015-12-15 09:08:43 +0800330 }
331 return;
332 }
333
Mark Yao4c156c22015-06-26 17:14:46 +0800334 yrgb_hor_scl_mode = scl_get_scl_mode(src_w, dst_w);
335 yrgb_ver_scl_mode = scl_get_scl_mode(src_h, dst_h);
336
337 if (is_yuv) {
338 cbcr_hor_scl_mode = scl_get_scl_mode(cbcr_src_w, dst_w);
339 cbcr_ver_scl_mode = scl_get_scl_mode(cbcr_src_h, dst_h);
340 if (cbcr_hor_scl_mode == SCALE_DOWN)
341 lb_mode = scl_vop_cal_lb_mode(dst_w, true);
342 else
343 lb_mode = scl_vop_cal_lb_mode(cbcr_src_w, true);
344 } else {
345 if (yrgb_hor_scl_mode == SCALE_DOWN)
346 lb_mode = scl_vop_cal_lb_mode(dst_w, false);
347 else
348 lb_mode = scl_vop_cal_lb_mode(src_w, false);
349 }
350
Mark Yao1194fff2015-12-15 09:08:43 +0800351 VOP_SCL_SET_EXT(vop, win, lb_mode, lb_mode);
Mark Yao4c156c22015-06-26 17:14:46 +0800352 if (lb_mode == LB_RGB_3840X2) {
353 if (yrgb_ver_scl_mode != SCALE_NONE) {
Sean Paulee4d7892016-08-12 13:00:54 -0400354 DRM_DEV_ERROR(vop->dev, "not allow yrgb ver scale\n");
Mark Yao4c156c22015-06-26 17:14:46 +0800355 return;
356 }
357 if (cbcr_ver_scl_mode != SCALE_NONE) {
Sean Paulee4d7892016-08-12 13:00:54 -0400358 DRM_DEV_ERROR(vop->dev, "not allow cbcr ver scale\n");
Mark Yao4c156c22015-06-26 17:14:46 +0800359 return;
360 }
361 vsu_mode = SCALE_UP_BIL;
362 } else if (lb_mode == LB_RGB_2560X4) {
363 vsu_mode = SCALE_UP_BIL;
364 } else {
365 vsu_mode = SCALE_UP_BIC;
366 }
367
368 val = scl_vop_cal_scale(yrgb_hor_scl_mode, src_w, dst_w,
369 true, 0, NULL);
370 VOP_SCL_SET(vop, win, scale_yrgb_x, val);
371 val = scl_vop_cal_scale(yrgb_ver_scl_mode, src_h, dst_h,
372 false, vsu_mode, &vskiplines);
373 VOP_SCL_SET(vop, win, scale_yrgb_y, val);
374
Mark Yao1194fff2015-12-15 09:08:43 +0800375 VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt4, vskiplines == 4);
376 VOP_SCL_SET_EXT(vop, win, vsd_yrgb_gt2, vskiplines == 2);
Mark Yao4c156c22015-06-26 17:14:46 +0800377
Mark Yao1194fff2015-12-15 09:08:43 +0800378 VOP_SCL_SET_EXT(vop, win, yrgb_hor_scl_mode, yrgb_hor_scl_mode);
379 VOP_SCL_SET_EXT(vop, win, yrgb_ver_scl_mode, yrgb_ver_scl_mode);
380 VOP_SCL_SET_EXT(vop, win, yrgb_hsd_mode, SCALE_DOWN_BIL);
381 VOP_SCL_SET_EXT(vop, win, yrgb_vsd_mode, SCALE_DOWN_BIL);
382 VOP_SCL_SET_EXT(vop, win, yrgb_vsu_mode, vsu_mode);
Mark Yao4c156c22015-06-26 17:14:46 +0800383 if (is_yuv) {
384 val = scl_vop_cal_scale(cbcr_hor_scl_mode, cbcr_src_w,
385 dst_w, true, 0, NULL);
386 VOP_SCL_SET(vop, win, scale_cbcr_x, val);
387 val = scl_vop_cal_scale(cbcr_ver_scl_mode, cbcr_src_h,
388 dst_h, false, vsu_mode, &vskiplines);
389 VOP_SCL_SET(vop, win, scale_cbcr_y, val);
390
Mark Yao1194fff2015-12-15 09:08:43 +0800391 VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt4, vskiplines == 4);
392 VOP_SCL_SET_EXT(vop, win, vsd_cbcr_gt2, vskiplines == 2);
393 VOP_SCL_SET_EXT(vop, win, cbcr_hor_scl_mode, cbcr_hor_scl_mode);
394 VOP_SCL_SET_EXT(vop, win, cbcr_ver_scl_mode, cbcr_ver_scl_mode);
395 VOP_SCL_SET_EXT(vop, win, cbcr_hsd_mode, SCALE_DOWN_BIL);
396 VOP_SCL_SET_EXT(vop, win, cbcr_vsd_mode, SCALE_DOWN_BIL);
397 VOP_SCL_SET_EXT(vop, win, cbcr_vsu_mode, vsu_mode);
Mark Yao4c156c22015-06-26 17:14:46 +0800398 }
399}
400
Mark Yao10672192015-02-04 13:10:31 +0800401static void vop_dsp_hold_valid_irq_enable(struct vop *vop)
402{
403 unsigned long flags;
404
405 if (WARN_ON(!vop->is_enabled))
406 return;
407
408 spin_lock_irqsave(&vop->irq_lock, flags);
409
Tomasz Figafa374102016-09-14 21:54:54 +0900410 VOP_INTR_SET_TYPE(vop, clear, DSP_HOLD_VALID_INTR, 1);
Mark Yaodbb3d942015-12-15 08:36:55 +0800411 VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 1);
Mark Yao10672192015-02-04 13:10:31 +0800412
413 spin_unlock_irqrestore(&vop->irq_lock, flags);
414}
415
416static void vop_dsp_hold_valid_irq_disable(struct vop *vop)
417{
418 unsigned long flags;
419
420 if (WARN_ON(!vop->is_enabled))
421 return;
422
423 spin_lock_irqsave(&vop->irq_lock, flags);
424
Mark Yaodbb3d942015-12-15 08:36:55 +0800425 VOP_INTR_SET_TYPE(vop, enable, DSP_HOLD_VALID_INTR, 0);
Mark Yao10672192015-02-04 13:10:31 +0800426
427 spin_unlock_irqrestore(&vop->irq_lock, flags);
428}
429
Yakir Yang69c34e42016-07-24 14:57:40 +0800430/*
431 * (1) each frame starts at the start of the Vsync pulse which is signaled by
432 * the "FRAME_SYNC" interrupt.
433 * (2) the active data region of each frame ends at dsp_vact_end
434 * (3) we should program this same number (dsp_vact_end) into dsp_line_frag_num,
435 * to get "LINE_FLAG" interrupt at the end of the active on screen data.
436 *
437 * VOP_INTR_CTRL0.dsp_line_frag_num = VOP_DSP_VACT_ST_END.dsp_vact_end
438 * Interrupts
439 * LINE_FLAG -------------------------------+
440 * FRAME_SYNC ----+ |
441 * | |
442 * v v
443 * | Vsync | Vbp | Vactive | Vfp |
444 * ^ ^ ^ ^
445 * | | | |
446 * | | | |
447 * dsp_vs_end ------------+ | | | VOP_DSP_VTOTAL_VS_END
448 * dsp_vact_start --------------+ | | VOP_DSP_VACT_ST_END
449 * dsp_vact_end ----------------------------+ | VOP_DSP_VACT_ST_END
450 * dsp_total -------------------------------------+ VOP_DSP_VTOTAL_VS_END
451 */
452static bool vop_line_flag_irq_is_enabled(struct vop *vop)
453{
454 uint32_t line_flag_irq;
455 unsigned long flags;
456
457 spin_lock_irqsave(&vop->irq_lock, flags);
458
459 line_flag_irq = VOP_INTR_GET_TYPE(vop, enable, LINE_FLAG_INTR);
460
461 spin_unlock_irqrestore(&vop->irq_lock, flags);
462
463 return !!line_flag_irq;
464}
465
Jeffy Chen459b0862017-04-27 14:54:17 +0800466static void vop_line_flag_irq_enable(struct vop *vop)
Yakir Yang69c34e42016-07-24 14:57:40 +0800467{
468 unsigned long flags;
469
470 if (WARN_ON(!vop->is_enabled))
471 return;
472
473 spin_lock_irqsave(&vop->irq_lock, flags);
474
Tomasz Figafa374102016-09-14 21:54:54 +0900475 VOP_INTR_SET_TYPE(vop, clear, LINE_FLAG_INTR, 1);
Yakir Yang69c34e42016-07-24 14:57:40 +0800476 VOP_INTR_SET_TYPE(vop, enable, LINE_FLAG_INTR, 1);
477
478 spin_unlock_irqrestore(&vop->irq_lock, flags);
479}
480
481static void vop_line_flag_irq_disable(struct vop *vop)
482{
483 unsigned long flags;
484
485 if (WARN_ON(!vop->is_enabled))
486 return;
487
488 spin_lock_irqsave(&vop->irq_lock, flags);
489
490 VOP_INTR_SET_TYPE(vop, enable, LINE_FLAG_INTR, 0);
491
492 spin_unlock_irqrestore(&vop->irq_lock, flags);
493}
494
Sean Paul39a9ad82016-08-15 16:12:29 -0700495static int vop_enable(struct drm_crtc *crtc)
Mark Yao2048e322014-08-22 18:36:26 +0800496{
497 struct vop *vop = to_vop(crtc);
498 int ret;
499
Mark Yao5d82d1a2015-04-01 13:48:53 +0800500 ret = pm_runtime_get_sync(vop->dev);
501 if (ret < 0) {
502 dev_err(vop->dev, "failed to get pm runtime: %d\n", ret);
Jeffy Chen5e570372017-04-06 20:31:20 +0800503 return ret;
Mark Yao5d82d1a2015-04-01 13:48:53 +0800504 }
505
Mark Yao2048e322014-08-22 18:36:26 +0800506 ret = clk_enable(vop->hclk);
Sean Paul39a9ad82016-08-15 16:12:29 -0700507 if (WARN_ON(ret < 0))
508 goto err_put_pm_runtime;
Mark Yao2048e322014-08-22 18:36:26 +0800509
510 ret = clk_enable(vop->dclk);
Sean Paul39a9ad82016-08-15 16:12:29 -0700511 if (WARN_ON(ret < 0))
Mark Yao2048e322014-08-22 18:36:26 +0800512 goto err_disable_hclk;
Mark Yao2048e322014-08-22 18:36:26 +0800513
514 ret = clk_enable(vop->aclk);
Sean Paul39a9ad82016-08-15 16:12:29 -0700515 if (WARN_ON(ret < 0))
Mark Yao2048e322014-08-22 18:36:26 +0800516 goto err_disable_dclk;
Mark Yao2048e322014-08-22 18:36:26 +0800517
518 /*
519 * Slave iommu shares power, irq and clock with vop. It was associated
520 * automatically with this master device via common driver code.
521 * Now that we have enabled the clock we attach it to the shared drm
522 * mapping.
523 */
524 ret = rockchip_drm_dma_attach_device(vop->drm_dev, vop->dev);
525 if (ret) {
526 dev_err(vop->dev, "failed to attach dma mapping, %d\n", ret);
527 goto err_disable_aclk;
528 }
529
Mark Yao77faa162015-07-20 16:25:20 +0800530 memcpy(vop->regs, vop->regsbak, vop->len);
Chris Zhong17a794d2016-08-26 20:39:38 -0700531 vop_cfg_done(vop);
532
Mark Yao52ab7892015-01-22 18:29:57 +0800533 /*
534 * At here, vop clock & iommu is enable, R/W vop regs would be safe.
535 */
536 vop->is_enabled = true;
537
Mark Yao2048e322014-08-22 18:36:26 +0800538 spin_lock(&vop->reg_lock);
539
Mark yao9a61c542017-07-28 14:06:25 +0800540 VOP_REG_SET(vop, common, standby, 1);
Mark Yao2048e322014-08-22 18:36:26 +0800541
542 spin_unlock(&vop->reg_lock);
543
544 enable_irq(vop->irq);
545
Mark Yaob5f7b752015-11-23 15:21:08 +0800546 drm_crtc_vblank_on(crtc);
Mark Yao2048e322014-08-22 18:36:26 +0800547
Sean Paul39a9ad82016-08-15 16:12:29 -0700548 return 0;
Mark Yao2048e322014-08-22 18:36:26 +0800549
550err_disable_aclk:
551 clk_disable(vop->aclk);
552err_disable_dclk:
553 clk_disable(vop->dclk);
554err_disable_hclk:
555 clk_disable(vop->hclk);
Sean Paul39a9ad82016-08-15 16:12:29 -0700556err_put_pm_runtime:
557 pm_runtime_put_sync(vop->dev);
558 return ret;
Mark Yao2048e322014-08-22 18:36:26 +0800559}
560
Laurent Pinchart64581712017-06-30 12:36:45 +0300561static void vop_crtc_atomic_disable(struct drm_crtc *crtc,
562 struct drm_crtc_state *old_state)
Mark Yao2048e322014-08-22 18:36:26 +0800563{
564 struct vop *vop = to_vop(crtc);
Tomeu Vizoso3ed6c642016-03-22 16:08:04 +0100565 int i;
Mark Yao2048e322014-08-22 18:36:26 +0800566
Daniel Vetter893b6ca2016-06-08 14:19:12 +0200567 WARN_ON(vop->event);
568
Sean Paulb883c9b2016-08-18 12:01:46 -0700569 rockchip_drm_psr_deactivate(&vop->crtc);
570
Tomeu Vizoso3ed6c642016-03-22 16:08:04 +0100571 /*
572 * We need to make sure that all windows are disabled before we
573 * disable that crtc. Otherwise we might try to scan from a destroyed
574 * buffer later.
575 */
576 for (i = 0; i < vop->data->win_size; i++) {
577 struct vop_win *vop_win = &vop->win[i];
578 const struct vop_win_data *win = vop_win->data;
579
580 spin_lock(&vop->reg_lock);
581 VOP_WIN_SET(vop, win, enable, 0);
582 spin_unlock(&vop->reg_lock);
583 }
584
Chris Zhong17a794d2016-08-26 20:39:38 -0700585 vop_cfg_done(vop);
586
Mark Yaob5f7b752015-11-23 15:21:08 +0800587 drm_crtc_vblank_off(crtc);
Mark Yao2048e322014-08-22 18:36:26 +0800588
Mark Yao2048e322014-08-22 18:36:26 +0800589 /*
Mark Yao10672192015-02-04 13:10:31 +0800590 * Vop standby will take effect at end of current frame,
591 * if dsp hold valid irq happen, it means standby complete.
592 *
593 * we must wait standby complete when we want to disable aclk,
594 * if not, memory bus maybe dead.
Mark Yao2048e322014-08-22 18:36:26 +0800595 */
Mark Yao10672192015-02-04 13:10:31 +0800596 reinit_completion(&vop->dsp_hold_completion);
597 vop_dsp_hold_valid_irq_enable(vop);
598
Mark Yao2048e322014-08-22 18:36:26 +0800599 spin_lock(&vop->reg_lock);
600
Mark yao9a61c542017-07-28 14:06:25 +0800601 VOP_REG_SET(vop, common, standby, 1);
Mark Yao2048e322014-08-22 18:36:26 +0800602
603 spin_unlock(&vop->reg_lock);
Mark Yao52ab7892015-01-22 18:29:57 +0800604
Mark Yao10672192015-02-04 13:10:31 +0800605 wait_for_completion(&vop->dsp_hold_completion);
Mark Yao2048e322014-08-22 18:36:26 +0800606
Mark Yao10672192015-02-04 13:10:31 +0800607 vop_dsp_hold_valid_irq_disable(vop);
608
609 disable_irq(vop->irq);
610
611 vop->is_enabled = false;
612
613 /*
614 * vop standby complete, so iommu detach is safe.
615 */
Mark Yao2048e322014-08-22 18:36:26 +0800616 rockchip_drm_dma_detach_device(vop->drm_dev, vop->dev);
617
Mark Yao10672192015-02-04 13:10:31 +0800618 clk_disable(vop->dclk);
Mark Yao2048e322014-08-22 18:36:26 +0800619 clk_disable(vop->aclk);
620 clk_disable(vop->hclk);
Mark Yao5d82d1a2015-04-01 13:48:53 +0800621 pm_runtime_put(vop->dev);
Daniel Vetter893b6ca2016-06-08 14:19:12 +0200622
623 if (crtc->state->event && !crtc->state->active) {
624 spin_lock_irq(&crtc->dev->event_lock);
625 drm_crtc_send_vblank_event(crtc, crtc->state->event);
626 spin_unlock_irq(&crtc->dev->event_lock);
627
628 crtc->state->event = NULL;
629 }
Mark Yao2048e322014-08-22 18:36:26 +0800630}
631
Mark Yao63ebb9f2015-11-30 18:22:42 +0800632static void vop_plane_destroy(struct drm_plane *plane)
Mark Yao2048e322014-08-22 18:36:26 +0800633{
Mark Yao63ebb9f2015-11-30 18:22:42 +0800634 drm_plane_cleanup(plane);
Mark Yao2048e322014-08-22 18:36:26 +0800635}
636
Mark Yao63ebb9f2015-11-30 18:22:42 +0800637static int vop_plane_atomic_check(struct drm_plane *plane,
638 struct drm_plane_state *state)
Mark Yao2048e322014-08-22 18:36:26 +0800639{
Mark Yao63ebb9f2015-11-30 18:22:42 +0800640 struct drm_crtc *crtc = state->crtc;
John Keeping92915da2016-03-04 11:04:03 +0000641 struct drm_crtc_state *crtc_state;
Mark Yao63ebb9f2015-11-30 18:22:42 +0800642 struct drm_framebuffer *fb = state->fb;
Mark Yao2048e322014-08-22 18:36:26 +0800643 struct vop_win *vop_win = to_vop_win(plane);
644 const struct vop_win_data *win = vop_win->data;
Mark Yao2048e322014-08-22 18:36:26 +0800645 int ret;
Mark Yao63ebb9f2015-11-30 18:22:42 +0800646 struct drm_rect clip;
Mark Yao4c156c22015-06-26 17:14:46 +0800647 int min_scale = win->phy->scl ? FRAC_16_16(1, 8) :
648 DRM_PLANE_HELPER_NO_SCALING;
649 int max_scale = win->phy->scl ? FRAC_16_16(8, 1) :
650 DRM_PLANE_HELPER_NO_SCALING;
Mark Yao2048e322014-08-22 18:36:26 +0800651
Mark Yao63ebb9f2015-11-30 18:22:42 +0800652 if (!crtc || !fb)
Tomasz Figad47a7242016-09-14 21:55:01 +0900653 return 0;
John Keeping92915da2016-03-04 11:04:03 +0000654
655 crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc);
656 if (WARN_ON(!crtc_state))
657 return -EINVAL;
658
Mark Yao63ebb9f2015-11-30 18:22:42 +0800659 clip.x1 = 0;
660 clip.y1 = 0;
John Keeping92915da2016-03-04 11:04:03 +0000661 clip.x2 = crtc_state->adjusted_mode.hdisplay;
662 clip.y2 = crtc_state->adjusted_mode.vdisplay;
Mark Yao63ebb9f2015-11-30 18:22:42 +0800663
Ville Syrjäläf9b96be2016-07-26 19:07:02 +0300664 ret = drm_plane_helper_check_state(state, &clip,
665 min_scale, max_scale,
666 true, true);
Mark Yao2048e322014-08-22 18:36:26 +0800667 if (ret)
668 return ret;
669
Ville Syrjäläf9b96be2016-07-26 19:07:02 +0300670 if (!state->visible)
Tomasz Figad47a7242016-09-14 21:55:01 +0900671 return 0;
Mark Yao2048e322014-08-22 18:36:26 +0800672
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200673 ret = vop_convert_format(fb->format->format);
Tomasz Figad47a7242016-09-14 21:55:01 +0900674 if (ret < 0)
675 return ret;
Mark Yao84c7f8c2015-07-20 16:16:49 +0800676
Mark Yao63ebb9f2015-11-30 18:22:42 +0800677 /*
678 * Src.x1 can be odd when do clip, but yuv plane start point
679 * need align with 2 pixel.
680 */
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200681 if (is_yuv_support(fb->format->format) && ((state->src.x1 >> 16) % 2))
Mark Yao63ebb9f2015-11-30 18:22:42 +0800682 return -EINVAL;
683
Mark Yao63ebb9f2015-11-30 18:22:42 +0800684 return 0;
685}
686
687static void vop_plane_atomic_disable(struct drm_plane *plane,
688 struct drm_plane_state *old_state)
689{
Mark Yao63ebb9f2015-11-30 18:22:42 +0800690 struct vop_win *vop_win = to_vop_win(plane);
691 const struct vop_win_data *win = vop_win->data;
692 struct vop *vop = to_vop(old_state->crtc);
693
694 if (!old_state->crtc)
695 return;
696
697 spin_lock(&vop->reg_lock);
698
699 VOP_WIN_SET(vop, win, enable, 0);
700
701 spin_unlock(&vop->reg_lock);
Mark Yao63ebb9f2015-11-30 18:22:42 +0800702}
703
704static void vop_plane_atomic_update(struct drm_plane *plane,
705 struct drm_plane_state *old_state)
706{
707 struct drm_plane_state *state = plane->state;
708 struct drm_crtc *crtc = state->crtc;
709 struct vop_win *vop_win = to_vop_win(plane);
Mark Yao63ebb9f2015-11-30 18:22:42 +0800710 const struct vop_win_data *win = vop_win->data;
711 struct vop *vop = to_vop(state->crtc);
712 struct drm_framebuffer *fb = state->fb;
713 unsigned int actual_w, actual_h;
714 unsigned int dsp_stx, dsp_sty;
715 uint32_t act_info, dsp_info, dsp_st;
Ville Syrjäläac920282016-07-26 19:07:01 +0300716 struct drm_rect *src = &state->src;
717 struct drm_rect *dest = &state->dst;
Mark Yao63ebb9f2015-11-30 18:22:42 +0800718 struct drm_gem_object *obj, *uv_obj;
719 struct rockchip_gem_object *rk_obj, *rk_uv_obj;
720 unsigned long offset;
721 dma_addr_t dma_addr;
722 uint32_t val;
723 bool rb_swap;
Tomasz Figad47a7242016-09-14 21:55:01 +0900724 int format;
Mark Yao63ebb9f2015-11-30 18:22:42 +0800725
726 /*
727 * can't update plane when vop is disabled.
728 */
Daniel Vetter4f9d39a2016-06-08 14:19:11 +0200729 if (WARN_ON(!crtc))
Mark Yao63ebb9f2015-11-30 18:22:42 +0800730 return;
731
732 if (WARN_ON(!vop->is_enabled))
733 return;
734
Tomasz Figad47a7242016-09-14 21:55:01 +0900735 if (!state->visible) {
Mark Yao63ebb9f2015-11-30 18:22:42 +0800736 vop_plane_atomic_disable(plane, old_state);
737 return;
738 }
Mark Yao2048e322014-08-22 18:36:26 +0800739
740 obj = rockchip_fb_get_gem_obj(fb, 0);
Mark Yao2048e322014-08-22 18:36:26 +0800741 rk_obj = to_rockchip_obj(obj);
742
Mark Yao63ebb9f2015-11-30 18:22:42 +0800743 actual_w = drm_rect_width(src) >> 16;
744 actual_h = drm_rect_height(src) >> 16;
745 act_info = (actual_h - 1) << 16 | ((actual_w - 1) & 0xffff);
Mark Yao84c7f8c2015-07-20 16:16:49 +0800746
Mark Yao63ebb9f2015-11-30 18:22:42 +0800747 dsp_info = (drm_rect_height(dest) - 1) << 16;
748 dsp_info |= (drm_rect_width(dest) - 1) & 0xffff;
Mark Yao2048e322014-08-22 18:36:26 +0800749
Mark Yao63ebb9f2015-11-30 18:22:42 +0800750 dsp_stx = dest->x1 + crtc->mode.htotal - crtc->mode.hsync_start;
751 dsp_sty = dest->y1 + crtc->mode.vtotal - crtc->mode.vsync_start;
752 dsp_st = dsp_sty << 16 | (dsp_stx & 0xffff);
Mark Yao2048e322014-08-22 18:36:26 +0800753
Ville Syrjälä353c8592016-12-14 23:30:57 +0200754 offset = (src->x1 >> 16) * fb->format->cpp[0];
Mark Yao63ebb9f2015-11-30 18:22:42 +0800755 offset += (src->y1 >> 16) * fb->pitches[0];
Tomasz Figad47a7242016-09-14 21:55:01 +0900756 dma_addr = rk_obj->dma_addr + offset + fb->offsets[0];
757
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200758 format = vop_convert_format(fb->format->format);
Mark Yao2048e322014-08-22 18:36:26 +0800759
Mark Yao63ebb9f2015-11-30 18:22:42 +0800760 spin_lock(&vop->reg_lock);
Mark Yao2048e322014-08-22 18:36:26 +0800761
Tomasz Figad47a7242016-09-14 21:55:01 +0900762 VOP_WIN_SET(vop, win, format, format);
Mark Yao63ebb9f2015-11-30 18:22:42 +0800763 VOP_WIN_SET(vop, win, yrgb_vir, fb->pitches[0] >> 2);
Tomasz Figad47a7242016-09-14 21:55:01 +0900764 VOP_WIN_SET(vop, win, yrgb_mst, dma_addr);
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200765 if (is_yuv_support(fb->format->format)) {
766 int hsub = drm_format_horz_chroma_subsampling(fb->format->format);
767 int vsub = drm_format_vert_chroma_subsampling(fb->format->format);
Ville Syrjälä353c8592016-12-14 23:30:57 +0200768 int bpp = fb->format->cpp[1];
Mark Yao84c7f8c2015-07-20 16:16:49 +0800769
770 uv_obj = rockchip_fb_get_gem_obj(fb, 1);
Mark Yao84c7f8c2015-07-20 16:16:49 +0800771 rk_uv_obj = to_rockchip_obj(uv_obj);
Mark Yao84c7f8c2015-07-20 16:16:49 +0800772
Mark Yao63ebb9f2015-11-30 18:22:42 +0800773 offset = (src->x1 >> 16) * bpp / hsub;
774 offset += (src->y1 >> 16) * fb->pitches[1] / vsub;
Mark Yao84c7f8c2015-07-20 16:16:49 +0800775
Mark Yao63ebb9f2015-11-30 18:22:42 +0800776 dma_addr = rk_uv_obj->dma_addr + offset + fb->offsets[1];
777 VOP_WIN_SET(vop, win, uv_vir, fb->pitches[1] >> 2);
778 VOP_WIN_SET(vop, win, uv_mst, dma_addr);
Mark Yao84c7f8c2015-07-20 16:16:49 +0800779 }
Mark Yao4c156c22015-06-26 17:14:46 +0800780
781 if (win->phy->scl)
782 scl_vop_cal_scl_fac(vop, win, actual_w, actual_h,
Mark Yao63ebb9f2015-11-30 18:22:42 +0800783 drm_rect_width(dest), drm_rect_height(dest),
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200784 fb->format->format);
Mark Yao4c156c22015-06-26 17:14:46 +0800785
Mark Yao63ebb9f2015-11-30 18:22:42 +0800786 VOP_WIN_SET(vop, win, act_info, act_info);
787 VOP_WIN_SET(vop, win, dsp_info, dsp_info);
788 VOP_WIN_SET(vop, win, dsp_st, dsp_st);
Mark Yao4c156c22015-06-26 17:14:46 +0800789
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200790 rb_swap = has_rb_swapped(fb->format->format);
Tomasz Figa85a359f2015-05-11 19:55:39 +0900791 VOP_WIN_SET(vop, win, rb_swap, rb_swap);
Mark Yao2048e322014-08-22 18:36:26 +0800792
Ville Syrjälä438b74a2016-12-14 23:32:55 +0200793 if (is_alpha_support(fb->format->format)) {
Mark Yao2048e322014-08-22 18:36:26 +0800794 VOP_WIN_SET(vop, win, dst_alpha_ctl,
795 DST_FACTOR_M0(ALPHA_SRC_INVERSE));
796 val = SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL) |
797 SRC_ALPHA_M0(ALPHA_STRAIGHT) |
798 SRC_BLEND_M0(ALPHA_PER_PIX) |
799 SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION) |
800 SRC_FACTOR_M0(ALPHA_ONE);
801 VOP_WIN_SET(vop, win, src_alpha_ctl, val);
802 } else {
803 VOP_WIN_SET(vop, win, src_alpha_ctl, SRC_ALPHA_EN(0));
804 }
805
806 VOP_WIN_SET(vop, win, enable, 1);
Mark Yao2048e322014-08-22 18:36:26 +0800807 spin_unlock(&vop->reg_lock);
Mark Yao2048e322014-08-22 18:36:26 +0800808}
809
Mark Yao63ebb9f2015-11-30 18:22:42 +0800810static const struct drm_plane_helper_funcs plane_helper_funcs = {
811 .atomic_check = vop_plane_atomic_check,
812 .atomic_update = vop_plane_atomic_update,
813 .atomic_disable = vop_plane_atomic_disable,
814};
815
Mark Yao2048e322014-08-22 18:36:26 +0800816static const struct drm_plane_funcs vop_plane_funcs = {
Mark Yao63ebb9f2015-11-30 18:22:42 +0800817 .update_plane = drm_atomic_helper_update_plane,
818 .disable_plane = drm_atomic_helper_disable_plane,
Mark Yao2048e322014-08-22 18:36:26 +0800819 .destroy = vop_plane_destroy,
Tomasz Figad47a7242016-09-14 21:55:01 +0900820 .reset = drm_atomic_helper_plane_reset,
821 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
822 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Mark Yao2048e322014-08-22 18:36:26 +0800823};
824
Mark Yao2048e322014-08-22 18:36:26 +0800825static int vop_crtc_enable_vblank(struct drm_crtc *crtc)
826{
827 struct vop *vop = to_vop(crtc);
828 unsigned long flags;
829
Mark Yao63ebb9f2015-11-30 18:22:42 +0800830 if (WARN_ON(!vop->is_enabled))
Mark Yao2048e322014-08-22 18:36:26 +0800831 return -EPERM;
832
833 spin_lock_irqsave(&vop->irq_lock, flags);
834
Tomasz Figafa374102016-09-14 21:54:54 +0900835 VOP_INTR_SET_TYPE(vop, clear, FS_INTR, 1);
Mark Yaodbb3d942015-12-15 08:36:55 +0800836 VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 1);
Mark Yao2048e322014-08-22 18:36:26 +0800837
838 spin_unlock_irqrestore(&vop->irq_lock, flags);
839
840 return 0;
841}
842
843static void vop_crtc_disable_vblank(struct drm_crtc *crtc)
844{
845 struct vop *vop = to_vop(crtc);
846 unsigned long flags;
847
Mark Yao63ebb9f2015-11-30 18:22:42 +0800848 if (WARN_ON(!vop->is_enabled))
Mark Yao2048e322014-08-22 18:36:26 +0800849 return;
Mark Yao31e980c2015-01-22 14:37:56 +0800850
Mark Yao2048e322014-08-22 18:36:26 +0800851 spin_lock_irqsave(&vop->irq_lock, flags);
Mark Yaodbb3d942015-12-15 08:36:55 +0800852
853 VOP_INTR_SET_TYPE(vop, enable, FS_INTR, 0);
854
Mark Yao2048e322014-08-22 18:36:26 +0800855 spin_unlock_irqrestore(&vop->irq_lock, flags);
856}
857
Mark Yao2048e322014-08-22 18:36:26 +0800858static bool vop_crtc_mode_fixup(struct drm_crtc *crtc,
859 const struct drm_display_mode *mode,
860 struct drm_display_mode *adjusted_mode)
861{
Chris Zhongb59b8de2016-01-06 12:03:53 +0800862 struct vop *vop = to_vop(crtc);
863
Chris Zhongb59b8de2016-01-06 12:03:53 +0800864 adjusted_mode->clock =
865 clk_round_rate(vop->dclk, mode->clock * 1000) / 1000;
866
Mark Yao2048e322014-08-22 18:36:26 +0800867 return true;
868}
869
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300870static void vop_crtc_atomic_enable(struct drm_crtc *crtc,
871 struct drm_crtc_state *old_state)
Mark Yao2048e322014-08-22 18:36:26 +0800872{
873 struct vop *vop = to_vop(crtc);
Mark yaoefd11cc2017-05-27 19:43:36 +0800874 const struct vop_data *vop_data = vop->data;
Mark Yao4e257d92016-04-20 10:41:42 +0800875 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc->state);
Mark Yao63ebb9f2015-11-30 18:22:42 +0800876 struct drm_display_mode *adjusted_mode = &crtc->state->adjusted_mode;
Mark Yao2048e322014-08-22 18:36:26 +0800877 u16 hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
878 u16 hdisplay = adjusted_mode->hdisplay;
879 u16 htotal = adjusted_mode->htotal;
880 u16 hact_st = adjusted_mode->htotal - adjusted_mode->hsync_start;
881 u16 hact_end = hact_st + hdisplay;
882 u16 vdisplay = adjusted_mode->vdisplay;
883 u16 vtotal = adjusted_mode->vtotal;
884 u16 vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
885 u16 vact_st = adjusted_mode->vtotal - adjusted_mode->vsync_start;
886 u16 vact_end = vact_st + vdisplay;
Mark Yao0a63bfd2016-04-20 14:18:16 +0800887 uint32_t pin_pol, val;
Sean Paul39a9ad82016-08-15 16:12:29 -0700888 int ret;
Mark Yao2048e322014-08-22 18:36:26 +0800889
Daniel Vetter893b6ca2016-06-08 14:19:12 +0200890 WARN_ON(vop->event);
891
Sean Paul39a9ad82016-08-15 16:12:29 -0700892 ret = vop_enable(crtc);
893 if (ret) {
894 DRM_DEV_ERROR(vop->dev, "Failed to enable vop (%d)\n", ret);
895 return;
896 }
897
Mark Yao2048e322014-08-22 18:36:26 +0800898 /*
Mark Yaoce3887e2015-12-16 18:08:17 +0800899 * If dclk rate is zero, mean that scanout is stop,
900 * we don't need wait any more.
Mark Yao2048e322014-08-22 18:36:26 +0800901 */
Mark Yaoce3887e2015-12-16 18:08:17 +0800902 if (clk_get_rate(vop->dclk)) {
903 /*
904 * Rk3288 vop timing register is immediately, when configure
905 * display timing on display time, may cause tearing.
906 *
907 * Vop standby will take effect at end of current frame,
908 * if dsp hold valid irq happen, it means standby complete.
909 *
910 * mode set:
911 * standby and wait complete --> |----
912 * | display time
913 * |----
914 * |---> dsp hold irq
915 * configure display timing --> |
916 * standby exit |
917 * | new frame start.
918 */
919
920 reinit_completion(&vop->dsp_hold_completion);
921 vop_dsp_hold_valid_irq_enable(vop);
922
923 spin_lock(&vop->reg_lock);
924
Mark yao9a61c542017-07-28 14:06:25 +0800925 VOP_REG_SET(vop, common, standby, 1);
Mark Yaoce3887e2015-12-16 18:08:17 +0800926
927 spin_unlock(&vop->reg_lock);
928
929 wait_for_completion(&vop->dsp_hold_completion);
930
931 vop_dsp_hold_valid_irq_disable(vop);
932 }
Mark Yao2048e322014-08-22 18:36:26 +0800933
Chris Zhong1a0f7ed2017-02-05 15:54:56 +0800934 pin_pol = BIT(DCLK_INVERT);
John Keepingd790ad02017-02-24 12:55:03 +0000935 pin_pol |= (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC) ?
936 BIT(HSYNC_POSITIVE) : 0;
937 pin_pol |= (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC) ?
938 BIT(VSYNC_POSITIVE) : 0;
Mark yao9a61c542017-07-28 14:06:25 +0800939 VOP_REG_SET(vop, output, pin_pol, pin_pol);
Mark Yao0a63bfd2016-04-20 14:18:16 +0800940
Mark Yao4e257d92016-04-20 10:41:42 +0800941 switch (s->output_type) {
942 case DRM_MODE_CONNECTOR_LVDS:
Mark yao9a61c542017-07-28 14:06:25 +0800943 VOP_REG_SET(vop, output, rgb_en, 1);
944 VOP_REG_SET(vop, output, rgb_pin_pol, pin_pol);
Mark Yao4e257d92016-04-20 10:41:42 +0800945 break;
946 case DRM_MODE_CONNECTOR_eDP:
Mark yao9a61c542017-07-28 14:06:25 +0800947 VOP_REG_SET(vop, output, edp_pin_pol, pin_pol);
948 VOP_REG_SET(vop, output, edp_en, 1);
Mark Yao4e257d92016-04-20 10:41:42 +0800949 break;
950 case DRM_MODE_CONNECTOR_HDMIA:
Mark yao9a61c542017-07-28 14:06:25 +0800951 VOP_REG_SET(vop, output, hdmi_pin_pol, pin_pol);
952 VOP_REG_SET(vop, output, hdmi_en, 1);
Mark Yao4e257d92016-04-20 10:41:42 +0800953 break;
954 case DRM_MODE_CONNECTOR_DSI:
Mark yao9a61c542017-07-28 14:06:25 +0800955 VOP_REG_SET(vop, output, mipi_pin_pol, pin_pol);
956 VOP_REG_SET(vop, output, mipi_en, 1);
Mark Yao4e257d92016-04-20 10:41:42 +0800957 break;
Chris Zhong1a0f7ed2017-02-05 15:54:56 +0800958 case DRM_MODE_CONNECTOR_DisplayPort:
959 pin_pol &= ~BIT(DCLK_INVERT);
Mark yao9a61c542017-07-28 14:06:25 +0800960 VOP_REG_SET(vop, output, dp_pin_pol, pin_pol);
961 VOP_REG_SET(vop, output, dp_en, 1);
Chris Zhong1a0f7ed2017-02-05 15:54:56 +0800962 break;
Mark Yao4e257d92016-04-20 10:41:42 +0800963 default:
Sean Paulee4d7892016-08-12 13:00:54 -0400964 DRM_DEV_ERROR(vop->dev, "unsupported connector_type [%d]\n",
965 s->output_type);
Mark Yao4e257d92016-04-20 10:41:42 +0800966 }
Mark yaoefd11cc2017-05-27 19:43:36 +0800967
968 /*
969 * if vop is not support RGB10 output, need force RGB10 to RGB888.
970 */
971 if (s->output_mode == ROCKCHIP_OUT_MODE_AAAA &&
972 !(vop_data->feature & VOP_FEATURE_OUTPUT_RGB10))
973 s->output_mode = ROCKCHIP_OUT_MODE_P888;
Mark yao9a61c542017-07-28 14:06:25 +0800974 VOP_REG_SET(vop, common, out_mode, s->output_mode);
Mark Yao2048e322014-08-22 18:36:26 +0800975
Mark yao9a61c542017-07-28 14:06:25 +0800976 VOP_REG_SET(vop, modeset, htotal_pw, (htotal << 16) | hsync_len);
Mark Yao2048e322014-08-22 18:36:26 +0800977 val = hact_st << 16;
978 val |= hact_end;
Mark yao9a61c542017-07-28 14:06:25 +0800979 VOP_REG_SET(vop, modeset, hact_st_end, val);
980 VOP_REG_SET(vop, modeset, hpost_st_end, val);
Mark Yao2048e322014-08-22 18:36:26 +0800981
Mark yao9a61c542017-07-28 14:06:25 +0800982 VOP_REG_SET(vop, modeset, vtotal_pw, (vtotal << 16) | vsync_len);
Mark Yao2048e322014-08-22 18:36:26 +0800983 val = vact_st << 16;
984 val |= vact_end;
Mark yao9a61c542017-07-28 14:06:25 +0800985 VOP_REG_SET(vop, modeset, vact_st_end, val);
986 VOP_REG_SET(vop, modeset, vpost_st_end, val);
Mark Yao2048e322014-08-22 18:36:26 +0800987
Mark yao9a61c542017-07-28 14:06:25 +0800988 VOP_REG_SET(vop, intr, line_flag_num[0], vact_end);
Jeffy Chen459b0862017-04-27 14:54:17 +0800989
Mark Yao2048e322014-08-22 18:36:26 +0800990 clk_set_rate(vop->dclk, adjusted_mode->clock * 1000);
Mark Yaoce3887e2015-12-16 18:08:17 +0800991
Mark yao9a61c542017-07-28 14:06:25 +0800992 VOP_REG_SET(vop, common, standby, 0);
Sean Paulb883c9b2016-08-18 12:01:46 -0700993
994 rockchip_drm_psr_activate(&vop->crtc);
Mark Yao63ebb9f2015-11-30 18:22:42 +0800995}
Mark Yao2048e322014-08-22 18:36:26 +0800996
Tomasz Figa7caecdb2016-09-14 21:54:56 +0900997static bool vop_fs_irq_is_pending(struct vop *vop)
998{
999 return VOP_INTR_GET_TYPE(vop, status, FS_INTR);
1000}
1001
1002static void vop_wait_for_irq_handler(struct vop *vop)
1003{
1004 bool pending;
1005 int ret;
1006
1007 /*
1008 * Spin until frame start interrupt status bit goes low, which means
1009 * that interrupt handler was invoked and cleared it. The timeout of
1010 * 10 msecs is really too long, but it is just a safety measure if
1011 * something goes really wrong. The wait will only happen in the very
1012 * unlikely case of a vblank happening exactly at the same time and
1013 * shouldn't exceed microseconds range.
1014 */
1015 ret = readx_poll_timeout_atomic(vop_fs_irq_is_pending, vop, pending,
1016 !pending, 0, 10 * 1000);
1017 if (ret)
1018 DRM_DEV_ERROR(vop->dev, "VOP vblank IRQ stuck for 10 ms\n");
1019
1020 synchronize_irq(vop->irq);
1021}
1022
Mark Yao63ebb9f2015-11-30 18:22:42 +08001023static void vop_crtc_atomic_flush(struct drm_crtc *crtc,
1024 struct drm_crtc_state *old_crtc_state)
1025{
Tomasz Figa47a7eb42016-09-14 21:54:57 +09001026 struct drm_atomic_state *old_state = old_crtc_state->state;
Maarten Lankhorste741f2b2017-07-12 10:13:37 +02001027 struct drm_plane_state *old_plane_state, *new_plane_state;
Mark Yao63ebb9f2015-11-30 18:22:42 +08001028 struct vop *vop = to_vop(crtc);
Tomasz Figa47a7eb42016-09-14 21:54:57 +09001029 struct drm_plane *plane;
1030 int i;
Mark Yao63ebb9f2015-11-30 18:22:42 +08001031
1032 if (WARN_ON(!vop->is_enabled))
1033 return;
1034
1035 spin_lock(&vop->reg_lock);
1036
1037 vop_cfg_done(vop);
1038
1039 spin_unlock(&vop->reg_lock);
Tomasz Figa7caecdb2016-09-14 21:54:56 +09001040
1041 /*
1042 * There is a (rather unlikely) possiblity that a vblank interrupt
1043 * fired before we set the cfg_done bit. To avoid spuriously
1044 * signalling flip completion we need to wait for it to finish.
1045 */
1046 vop_wait_for_irq_handler(vop);
Tomasz Figa47a7eb42016-09-14 21:54:57 +09001047
Tomasz Figa41ee4362016-09-14 21:55:00 +09001048 spin_lock_irq(&crtc->dev->event_lock);
1049 if (crtc->state->event) {
1050 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
1051 WARN_ON(vop->event);
1052
1053 vop->event = crtc->state->event;
1054 crtc->state->event = NULL;
1055 }
1056 spin_unlock_irq(&crtc->dev->event_lock);
1057
Maarten Lankhorste741f2b2017-07-12 10:13:37 +02001058 for_each_oldnew_plane_in_state(old_state, plane, old_plane_state,
1059 new_plane_state, i) {
Tomasz Figa47a7eb42016-09-14 21:54:57 +09001060 if (!old_plane_state->fb)
1061 continue;
1062
Maarten Lankhorste741f2b2017-07-12 10:13:37 +02001063 if (old_plane_state->fb == new_plane_state->fb)
Tomasz Figa47a7eb42016-09-14 21:54:57 +09001064 continue;
1065
1066 drm_framebuffer_reference(old_plane_state->fb);
1067 drm_flip_work_queue(&vop->fb_unref_work, old_plane_state->fb);
1068 set_bit(VOP_PENDING_FB_UNREF, &vop->pending);
1069 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
1070 }
Mark Yao63ebb9f2015-11-30 18:22:42 +08001071}
1072
1073static void vop_crtc_atomic_begin(struct drm_crtc *crtc,
1074 struct drm_crtc_state *old_crtc_state)
1075{
Sean Paulb883c9b2016-08-18 12:01:46 -07001076 rockchip_drm_psr_flush(crtc);
Mark Yao2048e322014-08-22 18:36:26 +08001077}
1078
Mark Yao2048e322014-08-22 18:36:26 +08001079static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = {
Mark Yao2048e322014-08-22 18:36:26 +08001080 .mode_fixup = vop_crtc_mode_fixup,
Mark Yao63ebb9f2015-11-30 18:22:42 +08001081 .atomic_flush = vop_crtc_atomic_flush,
1082 .atomic_begin = vop_crtc_atomic_begin,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +03001083 .atomic_enable = vop_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +03001084 .atomic_disable = vop_crtc_atomic_disable,
Mark Yao2048e322014-08-22 18:36:26 +08001085};
1086
Mark Yao2048e322014-08-22 18:36:26 +08001087static void vop_crtc_destroy(struct drm_crtc *crtc)
1088{
1089 drm_crtc_cleanup(crtc);
1090}
1091
John Keepingdc0b4082016-07-14 16:29:15 +01001092static void vop_crtc_reset(struct drm_crtc *crtc)
1093{
1094 if (crtc->state)
1095 __drm_atomic_helper_crtc_destroy_state(crtc->state);
1096 kfree(crtc->state);
1097
1098 crtc->state = kzalloc(sizeof(struct rockchip_crtc_state), GFP_KERNEL);
1099 if (crtc->state)
1100 crtc->state->crtc = crtc;
1101}
1102
Mark Yao4e257d92016-04-20 10:41:42 +08001103static struct drm_crtc_state *vop_crtc_duplicate_state(struct drm_crtc *crtc)
1104{
1105 struct rockchip_crtc_state *rockchip_state;
1106
1107 rockchip_state = kzalloc(sizeof(*rockchip_state), GFP_KERNEL);
1108 if (!rockchip_state)
1109 return NULL;
1110
1111 __drm_atomic_helper_crtc_duplicate_state(crtc, &rockchip_state->base);
1112 return &rockchip_state->base;
1113}
1114
1115static void vop_crtc_destroy_state(struct drm_crtc *crtc,
1116 struct drm_crtc_state *state)
1117{
1118 struct rockchip_crtc_state *s = to_rockchip_crtc_state(state);
1119
Daniel Vetterec2dc6a2016-05-09 16:34:09 +02001120 __drm_atomic_helper_crtc_destroy_state(&s->base);
Mark Yao4e257d92016-04-20 10:41:42 +08001121 kfree(s);
1122}
1123
Sean Paul6cca3862017-03-06 15:02:26 -05001124#ifdef CONFIG_DRM_ANALOGIX_DP
Tomeu Vizoso3190e582017-03-03 14:39:36 +01001125static struct drm_connector *vop_get_edp_connector(struct vop *vop)
1126{
Tomeu Vizoso3190e582017-03-03 14:39:36 +01001127 struct drm_connector *connector;
Gustavo Padovan2cbeb642017-05-15 10:43:30 -03001128 struct drm_connector_list_iter conn_iter;
Tomeu Vizoso3190e582017-03-03 14:39:36 +01001129
Gustavo Padovan2cbeb642017-05-15 10:43:30 -03001130 drm_connector_list_iter_begin(vop->drm_dev, &conn_iter);
1131 drm_for_each_connector_iter(connector, &conn_iter) {
Tomeu Vizoso3190e582017-03-03 14:39:36 +01001132 if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
Gustavo Padovan2cbeb642017-05-15 10:43:30 -03001133 drm_connector_list_iter_end(&conn_iter);
Tomeu Vizoso3190e582017-03-03 14:39:36 +01001134 return connector;
1135 }
Gustavo Padovan2cbeb642017-05-15 10:43:30 -03001136 }
1137 drm_connector_list_iter_end(&conn_iter);
Tomeu Vizoso3190e582017-03-03 14:39:36 +01001138
1139 return NULL;
1140}
1141
1142static int vop_crtc_set_crc_source(struct drm_crtc *crtc,
1143 const char *source_name, size_t *values_cnt)
1144{
1145 struct vop *vop = to_vop(crtc);
1146 struct drm_connector *connector;
1147 int ret;
1148
1149 connector = vop_get_edp_connector(vop);
1150 if (!connector)
1151 return -EINVAL;
1152
1153 *values_cnt = 3;
1154
1155 if (source_name && strcmp(source_name, "auto") == 0)
1156 ret = analogix_dp_start_crc(connector);
1157 else if (!source_name)
1158 ret = analogix_dp_stop_crc(connector);
1159 else
1160 ret = -EINVAL;
1161
1162 return ret;
1163}
Sean Paul6cca3862017-03-06 15:02:26 -05001164#else
1165static int vop_crtc_set_crc_source(struct drm_crtc *crtc,
1166 const char *source_name, size_t *values_cnt)
1167{
1168 return -ENODEV;
1169}
1170#endif
Tomeu Vizoso3190e582017-03-03 14:39:36 +01001171
Mark Yao2048e322014-08-22 18:36:26 +08001172static const struct drm_crtc_funcs vop_crtc_funcs = {
Mark Yao63ebb9f2015-11-30 18:22:42 +08001173 .set_config = drm_atomic_helper_set_config,
1174 .page_flip = drm_atomic_helper_page_flip,
Mark Yao2048e322014-08-22 18:36:26 +08001175 .destroy = vop_crtc_destroy,
John Keepingdc0b4082016-07-14 16:29:15 +01001176 .reset = vop_crtc_reset,
Mark Yao4e257d92016-04-20 10:41:42 +08001177 .atomic_duplicate_state = vop_crtc_duplicate_state,
1178 .atomic_destroy_state = vop_crtc_destroy_state,
Shawn Guoc3605df2017-02-07 17:16:29 +08001179 .enable_vblank = vop_crtc_enable_vblank,
1180 .disable_vblank = vop_crtc_disable_vblank,
Tomeu Vizoso3190e582017-03-03 14:39:36 +01001181 .set_crc_source = vop_crtc_set_crc_source,
Mark Yao2048e322014-08-22 18:36:26 +08001182};
1183
Tomasz Figa47a7eb42016-09-14 21:54:57 +09001184static void vop_fb_unref_worker(struct drm_flip_work *work, void *val)
1185{
1186 struct vop *vop = container_of(work, struct vop, fb_unref_work);
1187 struct drm_framebuffer *fb = val;
1188
1189 drm_crtc_vblank_put(&vop->crtc);
1190 drm_framebuffer_unreference(fb);
1191}
1192
Mark Yao63ebb9f2015-11-30 18:22:42 +08001193static void vop_handle_vblank(struct vop *vop)
1194{
1195 struct drm_device *drm = vop->drm_dev;
1196 struct drm_crtc *crtc = &vop->crtc;
1197 unsigned long flags;
Mark Yao2048e322014-08-22 18:36:26 +08001198
Daniel Vetter893b6ca2016-06-08 14:19:12 +02001199 spin_lock_irqsave(&drm->event_lock, flags);
Mark Yao63ebb9f2015-11-30 18:22:42 +08001200 if (vop->event) {
Mark Yao63ebb9f2015-11-30 18:22:42 +08001201 drm_crtc_send_vblank_event(crtc, vop->event);
Sean Paul5b680402016-08-10 16:24:39 -04001202 drm_crtc_vblank_put(crtc);
Tomasz Figa646ec682016-09-14 21:54:59 +09001203 vop->event = NULL;
Sean Paul5b680402016-08-10 16:24:39 -04001204 }
Daniel Vetter893b6ca2016-06-08 14:19:12 +02001205 spin_unlock_irqrestore(&drm->event_lock, flags);
1206
Tomasz Figa47a7eb42016-09-14 21:54:57 +09001207 if (test_and_clear_bit(VOP_PENDING_FB_UNREF, &vop->pending))
1208 drm_flip_work_commit(&vop->fb_unref_work, system_unbound_wq);
Mark Yao2048e322014-08-22 18:36:26 +08001209}
1210
1211static irqreturn_t vop_isr(int irq, void *data)
1212{
1213 struct vop *vop = data;
Mark Yaob5f7b752015-11-23 15:21:08 +08001214 struct drm_crtc *crtc = &vop->crtc;
Mark Yaodbb3d942015-12-15 08:36:55 +08001215 uint32_t active_irqs;
Mark Yao2048e322014-08-22 18:36:26 +08001216 unsigned long flags;
Mark Yao10672192015-02-04 13:10:31 +08001217 int ret = IRQ_NONE;
Mark Yao2048e322014-08-22 18:36:26 +08001218
1219 /*
Mark Yaodbb3d942015-12-15 08:36:55 +08001220 * interrupt register has interrupt status, enable and clear bits, we
Mark Yao2048e322014-08-22 18:36:26 +08001221 * must hold irq_lock to avoid a race with enable/disable_vblank().
1222 */
1223 spin_lock_irqsave(&vop->irq_lock, flags);
Mark Yaodbb3d942015-12-15 08:36:55 +08001224
1225 active_irqs = VOP_INTR_GET_TYPE(vop, status, INTR_MASK);
Mark Yao2048e322014-08-22 18:36:26 +08001226 /* Clear all active interrupt sources */
1227 if (active_irqs)
Mark Yaodbb3d942015-12-15 08:36:55 +08001228 VOP_INTR_SET_TYPE(vop, clear, active_irqs, 1);
1229
Mark Yao2048e322014-08-22 18:36:26 +08001230 spin_unlock_irqrestore(&vop->irq_lock, flags);
1231
1232 /* This is expected for vop iommu irqs, since the irq is shared */
1233 if (!active_irqs)
1234 return IRQ_NONE;
1235
Mark Yao10672192015-02-04 13:10:31 +08001236 if (active_irqs & DSP_HOLD_VALID_INTR) {
1237 complete(&vop->dsp_hold_completion);
1238 active_irqs &= ~DSP_HOLD_VALID_INTR;
1239 ret = IRQ_HANDLED;
Mark Yao2048e322014-08-22 18:36:26 +08001240 }
1241
Yakir Yang69c34e42016-07-24 14:57:40 +08001242 if (active_irqs & LINE_FLAG_INTR) {
1243 complete(&vop->line_flag_completion);
1244 active_irqs &= ~LINE_FLAG_INTR;
1245 ret = IRQ_HANDLED;
1246 }
1247
Mark Yao10672192015-02-04 13:10:31 +08001248 if (active_irqs & FS_INTR) {
Mark Yaob5f7b752015-11-23 15:21:08 +08001249 drm_crtc_handle_vblank(crtc);
Mark Yao63ebb9f2015-11-30 18:22:42 +08001250 vop_handle_vblank(vop);
Mark Yao10672192015-02-04 13:10:31 +08001251 active_irqs &= ~FS_INTR;
Mark Yao63ebb9f2015-11-30 18:22:42 +08001252 ret = IRQ_HANDLED;
Mark Yao10672192015-02-04 13:10:31 +08001253 }
Mark Yao2048e322014-08-22 18:36:26 +08001254
Mark Yao10672192015-02-04 13:10:31 +08001255 /* Unhandled irqs are spurious. */
1256 if (active_irqs)
Sean Paulee4d7892016-08-12 13:00:54 -04001257 DRM_DEV_ERROR(vop->dev, "Unknown VOP IRQs: %#02x\n",
1258 active_irqs);
Mark Yao10672192015-02-04 13:10:31 +08001259
1260 return ret;
Mark Yao2048e322014-08-22 18:36:26 +08001261}
1262
1263static int vop_create_crtc(struct vop *vop)
1264{
1265 const struct vop_data *vop_data = vop->data;
1266 struct device *dev = vop->dev;
1267 struct drm_device *drm_dev = vop->drm_dev;
Douglas Anderson328b51c2016-03-07 14:00:52 -08001268 struct drm_plane *primary = NULL, *cursor = NULL, *plane, *tmp;
Mark Yao2048e322014-08-22 18:36:26 +08001269 struct drm_crtc *crtc = &vop->crtc;
1270 struct device_node *port;
1271 int ret;
1272 int i;
1273
1274 /*
1275 * Create drm_plane for primary and cursor planes first, since we need
1276 * to pass them to drm_crtc_init_with_planes, which sets the
1277 * "possible_crtcs" to the newly initialized crtc.
1278 */
1279 for (i = 0; i < vop_data->win_size; i++) {
1280 struct vop_win *vop_win = &vop->win[i];
1281 const struct vop_win_data *win_data = vop_win->data;
1282
1283 if (win_data->type != DRM_PLANE_TYPE_PRIMARY &&
1284 win_data->type != DRM_PLANE_TYPE_CURSOR)
1285 continue;
1286
1287 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1288 0, &vop_plane_funcs,
1289 win_data->phy->data_formats,
1290 win_data->phy->nformats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -07001291 NULL, win_data->type, NULL);
Mark Yao2048e322014-08-22 18:36:26 +08001292 if (ret) {
Sean Paulee4d7892016-08-12 13:00:54 -04001293 DRM_DEV_ERROR(vop->dev, "failed to init plane %d\n",
1294 ret);
Mark Yao2048e322014-08-22 18:36:26 +08001295 goto err_cleanup_planes;
1296 }
1297
1298 plane = &vop_win->base;
Mark Yao63ebb9f2015-11-30 18:22:42 +08001299 drm_plane_helper_add(plane, &plane_helper_funcs);
Mark Yao2048e322014-08-22 18:36:26 +08001300 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1301 primary = plane;
1302 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
1303 cursor = plane;
1304 }
1305
1306 ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +02001307 &vop_crtc_funcs, NULL);
Mark Yao2048e322014-08-22 18:36:26 +08001308 if (ret)
Douglas Anderson328b51c2016-03-07 14:00:52 -08001309 goto err_cleanup_planes;
Mark Yao2048e322014-08-22 18:36:26 +08001310
1311 drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs);
1312
1313 /*
1314 * Create drm_planes for overlay windows with possible_crtcs restricted
1315 * to the newly created crtc.
1316 */
1317 for (i = 0; i < vop_data->win_size; i++) {
1318 struct vop_win *vop_win = &vop->win[i];
1319 const struct vop_win_data *win_data = vop_win->data;
1320 unsigned long possible_crtcs = 1 << drm_crtc_index(crtc);
1321
1322 if (win_data->type != DRM_PLANE_TYPE_OVERLAY)
1323 continue;
1324
1325 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1326 possible_crtcs,
1327 &vop_plane_funcs,
1328 win_data->phy->data_formats,
1329 win_data->phy->nformats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -07001330 NULL, win_data->type, NULL);
Mark Yao2048e322014-08-22 18:36:26 +08001331 if (ret) {
Sean Paulee4d7892016-08-12 13:00:54 -04001332 DRM_DEV_ERROR(vop->dev, "failed to init overlay %d\n",
1333 ret);
Mark Yao2048e322014-08-22 18:36:26 +08001334 goto err_cleanup_crtc;
1335 }
Mark Yao63ebb9f2015-11-30 18:22:42 +08001336 drm_plane_helper_add(&vop_win->base, &plane_helper_funcs);
Mark Yao2048e322014-08-22 18:36:26 +08001337 }
1338
1339 port = of_get_child_by_name(dev->of_node, "port");
1340 if (!port) {
Rob Herring4bf99142017-07-18 16:43:04 -05001341 DRM_DEV_ERROR(vop->dev, "no port node found in %pOF\n",
1342 dev->of_node);
Douglas Anderson328b51c2016-03-07 14:00:52 -08001343 ret = -ENOENT;
Mark Yao2048e322014-08-22 18:36:26 +08001344 goto err_cleanup_crtc;
1345 }
1346
Tomasz Figa47a7eb42016-09-14 21:54:57 +09001347 drm_flip_work_init(&vop->fb_unref_work, "fb_unref",
1348 vop_fb_unref_worker);
1349
Mark Yao10672192015-02-04 13:10:31 +08001350 init_completion(&vop->dsp_hold_completion);
Yakir Yang69c34e42016-07-24 14:57:40 +08001351 init_completion(&vop->line_flag_completion);
Mark Yao2048e322014-08-22 18:36:26 +08001352 crtc->port = port;
Mark Yao2048e322014-08-22 18:36:26 +08001353
1354 return 0;
1355
1356err_cleanup_crtc:
1357 drm_crtc_cleanup(crtc);
1358err_cleanup_planes:
Douglas Anderson328b51c2016-03-07 14:00:52 -08001359 list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list,
1360 head)
Mark Yao2048e322014-08-22 18:36:26 +08001361 drm_plane_cleanup(plane);
1362 return ret;
1363}
1364
1365static void vop_destroy_crtc(struct vop *vop)
1366{
1367 struct drm_crtc *crtc = &vop->crtc;
Douglas Anderson328b51c2016-03-07 14:00:52 -08001368 struct drm_device *drm_dev = vop->drm_dev;
1369 struct drm_plane *plane, *tmp;
Mark Yao2048e322014-08-22 18:36:26 +08001370
Mark Yao2048e322014-08-22 18:36:26 +08001371 of_node_put(crtc->port);
Douglas Anderson328b51c2016-03-07 14:00:52 -08001372
1373 /*
1374 * We need to cleanup the planes now. Why?
1375 *
1376 * The planes are "&vop->win[i].base". That means the memory is
1377 * all part of the big "struct vop" chunk of memory. That memory
1378 * was devm allocated and associated with this component. We need to
1379 * free it ourselves before vop_unbind() finishes.
1380 */
1381 list_for_each_entry_safe(plane, tmp, &drm_dev->mode_config.plane_list,
1382 head)
1383 vop_plane_destroy(plane);
1384
1385 /*
1386 * Destroy CRTC after vop_plane_destroy() since vop_disable_plane()
1387 * references the CRTC.
1388 */
Mark Yao2048e322014-08-22 18:36:26 +08001389 drm_crtc_cleanup(crtc);
Tomasz Figa47a7eb42016-09-14 21:54:57 +09001390 drm_flip_work_cleanup(&vop->fb_unref_work);
Mark Yao2048e322014-08-22 18:36:26 +08001391}
1392
1393static int vop_initial(struct vop *vop)
1394{
1395 const struct vop_data *vop_data = vop->data;
Mark Yao2048e322014-08-22 18:36:26 +08001396 struct reset_control *ahb_rst;
1397 int i, ret;
1398
1399 vop->hclk = devm_clk_get(vop->dev, "hclk_vop");
1400 if (IS_ERR(vop->hclk)) {
1401 dev_err(vop->dev, "failed to get hclk source\n");
1402 return PTR_ERR(vop->hclk);
1403 }
1404 vop->aclk = devm_clk_get(vop->dev, "aclk_vop");
1405 if (IS_ERR(vop->aclk)) {
1406 dev_err(vop->dev, "failed to get aclk source\n");
1407 return PTR_ERR(vop->aclk);
1408 }
1409 vop->dclk = devm_clk_get(vop->dev, "dclk_vop");
1410 if (IS_ERR(vop->dclk)) {
1411 dev_err(vop->dev, "failed to get dclk source\n");
1412 return PTR_ERR(vop->dclk);
1413 }
1414
Jeffy Chen5e570372017-04-06 20:31:20 +08001415 ret = pm_runtime_get_sync(vop->dev);
1416 if (ret < 0) {
1417 dev_err(vop->dev, "failed to get pm runtime: %d\n", ret);
1418 return ret;
1419 }
1420
Mark Yao2048e322014-08-22 18:36:26 +08001421 ret = clk_prepare(vop->dclk);
1422 if (ret < 0) {
1423 dev_err(vop->dev, "failed to prepare dclk\n");
Jeffy Chen5e570372017-04-06 20:31:20 +08001424 goto err_put_pm_runtime;
Mark Yao2048e322014-08-22 18:36:26 +08001425 }
1426
Sjoerd Simonsd7b53fd2015-11-06 13:22:24 +01001427 /* Enable both the hclk and aclk to setup the vop */
1428 ret = clk_prepare_enable(vop->hclk);
Mark Yao2048e322014-08-22 18:36:26 +08001429 if (ret < 0) {
Sjoerd Simonsd7b53fd2015-11-06 13:22:24 +01001430 dev_err(vop->dev, "failed to prepare/enable hclk\n");
Mark Yao2048e322014-08-22 18:36:26 +08001431 goto err_unprepare_dclk;
1432 }
1433
Sjoerd Simonsd7b53fd2015-11-06 13:22:24 +01001434 ret = clk_prepare_enable(vop->aclk);
Mark Yao2048e322014-08-22 18:36:26 +08001435 if (ret < 0) {
Sjoerd Simonsd7b53fd2015-11-06 13:22:24 +01001436 dev_err(vop->dev, "failed to prepare/enable aclk\n");
1437 goto err_disable_hclk;
Mark Yao2048e322014-08-22 18:36:26 +08001438 }
Sjoerd Simonsd7b53fd2015-11-06 13:22:24 +01001439
Mark Yao2048e322014-08-22 18:36:26 +08001440 /*
1441 * do hclk_reset, reset all vop registers.
1442 */
1443 ahb_rst = devm_reset_control_get(vop->dev, "ahb");
1444 if (IS_ERR(ahb_rst)) {
1445 dev_err(vop->dev, "failed to get ahb reset\n");
1446 ret = PTR_ERR(ahb_rst);
Sjoerd Simonsd7b53fd2015-11-06 13:22:24 +01001447 goto err_disable_aclk;
Mark Yao2048e322014-08-22 18:36:26 +08001448 }
1449 reset_control_assert(ahb_rst);
1450 usleep_range(10, 20);
1451 reset_control_deassert(ahb_rst);
1452
1453 memcpy(vop->regsbak, vop->regs, vop->len);
1454
Mark yao9a61c542017-07-28 14:06:25 +08001455 VOP_REG_SET(vop, misc, global_regdone_en, 1);
1456 VOP_REG_SET(vop, common, dsp_blank, 0);
Mark Yao2048e322014-08-22 18:36:26 +08001457
1458 for (i = 0; i < vop_data->win_size; i++) {
1459 const struct vop_win_data *win = &vop_data->win[i];
Mark yao9dd2aca2017-07-26 14:19:39 +08001460 int channel = i * 2 + 1;
Mark Yao2048e322014-08-22 18:36:26 +08001461
Mark yao9dd2aca2017-07-26 14:19:39 +08001462 VOP_WIN_SET(vop, win, channel, (channel + 1) << 4 | channel);
Mark Yao2048e322014-08-22 18:36:26 +08001463 VOP_WIN_SET(vop, win, enable, 0);
Mark yao60b7ae72017-07-26 14:19:05 +08001464 VOP_WIN_SET(vop, win, gate, 1);
Mark Yao2048e322014-08-22 18:36:26 +08001465 }
1466
1467 vop_cfg_done(vop);
1468
1469 /*
1470 * do dclk_reset, let all config take affect.
1471 */
1472 vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk");
1473 if (IS_ERR(vop->dclk_rst)) {
1474 dev_err(vop->dev, "failed to get dclk reset\n");
1475 ret = PTR_ERR(vop->dclk_rst);
Sjoerd Simonsd7b53fd2015-11-06 13:22:24 +01001476 goto err_disable_aclk;
Mark Yao2048e322014-08-22 18:36:26 +08001477 }
1478 reset_control_assert(vop->dclk_rst);
1479 usleep_range(10, 20);
1480 reset_control_deassert(vop->dclk_rst);
1481
1482 clk_disable(vop->hclk);
Sjoerd Simonsd7b53fd2015-11-06 13:22:24 +01001483 clk_disable(vop->aclk);
Mark Yao2048e322014-08-22 18:36:26 +08001484
Mark Yao31e980c2015-01-22 14:37:56 +08001485 vop->is_enabled = false;
Mark Yao2048e322014-08-22 18:36:26 +08001486
Jeffy Chen5e570372017-04-06 20:31:20 +08001487 pm_runtime_put_sync(vop->dev);
1488
Mark Yao2048e322014-08-22 18:36:26 +08001489 return 0;
1490
Sjoerd Simonsd7b53fd2015-11-06 13:22:24 +01001491err_disable_aclk:
1492 clk_disable_unprepare(vop->aclk);
Mark Yao2048e322014-08-22 18:36:26 +08001493err_disable_hclk:
Sjoerd Simonsd7b53fd2015-11-06 13:22:24 +01001494 clk_disable_unprepare(vop->hclk);
Mark Yao2048e322014-08-22 18:36:26 +08001495err_unprepare_dclk:
1496 clk_unprepare(vop->dclk);
Jeffy Chen5e570372017-04-06 20:31:20 +08001497err_put_pm_runtime:
1498 pm_runtime_put_sync(vop->dev);
Mark Yao2048e322014-08-22 18:36:26 +08001499 return ret;
1500}
1501
1502/*
1503 * Initialize the vop->win array elements.
1504 */
1505static void vop_win_init(struct vop *vop)
1506{
1507 const struct vop_data *vop_data = vop->data;
1508 unsigned int i;
1509
1510 for (i = 0; i < vop_data->win_size; i++) {
1511 struct vop_win *vop_win = &vop->win[i];
1512 const struct vop_win_data *win_data = &vop_data->win[i];
1513
1514 vop_win->data = win_data;
1515 vop_win->vop = vop;
Mark Yao2048e322014-08-22 18:36:26 +08001516 }
1517}
1518
Yakir Yang69c34e42016-07-24 14:57:40 +08001519/**
Jeffy Chen459b0862017-04-27 14:54:17 +08001520 * rockchip_drm_wait_vact_end
Yakir Yang69c34e42016-07-24 14:57:40 +08001521 * @crtc: CRTC to enable line flag
Yakir Yang69c34e42016-07-24 14:57:40 +08001522 * @mstimeout: millisecond for timeout
1523 *
Jeffy Chen459b0862017-04-27 14:54:17 +08001524 * Wait for vact_end line flag irq or timeout.
Yakir Yang69c34e42016-07-24 14:57:40 +08001525 *
1526 * Returns:
1527 * Zero on success, negative errno on failure.
1528 */
Jeffy Chen459b0862017-04-27 14:54:17 +08001529int rockchip_drm_wait_vact_end(struct drm_crtc *crtc, unsigned int mstimeout)
Yakir Yang69c34e42016-07-24 14:57:40 +08001530{
1531 struct vop *vop = to_vop(crtc);
1532 unsigned long jiffies_left;
1533
1534 if (!crtc || !vop->is_enabled)
1535 return -ENODEV;
1536
Jeffy Chen459b0862017-04-27 14:54:17 +08001537 if (mstimeout <= 0)
Yakir Yang69c34e42016-07-24 14:57:40 +08001538 return -EINVAL;
1539
1540 if (vop_line_flag_irq_is_enabled(vop))
1541 return -EBUSY;
1542
1543 reinit_completion(&vop->line_flag_completion);
Jeffy Chen459b0862017-04-27 14:54:17 +08001544 vop_line_flag_irq_enable(vop);
Yakir Yang69c34e42016-07-24 14:57:40 +08001545
1546 jiffies_left = wait_for_completion_timeout(&vop->line_flag_completion,
1547 msecs_to_jiffies(mstimeout));
1548 vop_line_flag_irq_disable(vop);
1549
1550 if (jiffies_left == 0) {
1551 dev_err(vop->dev, "Timeout waiting for IRQ\n");
1552 return -ETIMEDOUT;
1553 }
1554
1555 return 0;
1556}
Jeffy Chen459b0862017-04-27 14:54:17 +08001557EXPORT_SYMBOL(rockchip_drm_wait_vact_end);
Yakir Yang69c34e42016-07-24 14:57:40 +08001558
Mark Yao2048e322014-08-22 18:36:26 +08001559static int vop_bind(struct device *dev, struct device *master, void *data)
1560{
1561 struct platform_device *pdev = to_platform_device(dev);
Mark Yao2048e322014-08-22 18:36:26 +08001562 const struct vop_data *vop_data;
1563 struct drm_device *drm_dev = data;
1564 struct vop *vop;
1565 struct resource *res;
1566 size_t alloc_size;
Heiko Stuebner3ea68922015-04-20 01:00:53 +02001567 int ret, irq;
Mark Yao2048e322014-08-22 18:36:26 +08001568
Mark Yaoa67719d2015-12-15 08:58:26 +08001569 vop_data = of_device_get_match_data(dev);
Mark Yao2048e322014-08-22 18:36:26 +08001570 if (!vop_data)
1571 return -ENODEV;
1572
1573 /* Allocate vop struct and its vop_win array */
1574 alloc_size = sizeof(*vop) + sizeof(*vop->win) * vop_data->win_size;
1575 vop = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
1576 if (!vop)
1577 return -ENOMEM;
1578
1579 vop->dev = dev;
1580 vop->data = vop_data;
1581 vop->drm_dev = drm_dev;
1582 dev_set_drvdata(dev, vop);
1583
1584 vop_win_init(vop);
1585
1586 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1587 vop->len = resource_size(res);
1588 vop->regs = devm_ioremap_resource(dev, res);
1589 if (IS_ERR(vop->regs))
1590 return PTR_ERR(vop->regs);
1591
1592 vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL);
1593 if (!vop->regsbak)
1594 return -ENOMEM;
1595
Heiko Stuebner3ea68922015-04-20 01:00:53 +02001596 irq = platform_get_irq(pdev, 0);
1597 if (irq < 0) {
Mark Yao2048e322014-08-22 18:36:26 +08001598 dev_err(dev, "cannot find irq for vop\n");
Heiko Stuebner3ea68922015-04-20 01:00:53 +02001599 return irq;
Mark Yao2048e322014-08-22 18:36:26 +08001600 }
Heiko Stuebner3ea68922015-04-20 01:00:53 +02001601 vop->irq = (unsigned int)irq;
Mark Yao2048e322014-08-22 18:36:26 +08001602
1603 spin_lock_init(&vop->reg_lock);
1604 spin_lock_init(&vop->irq_lock);
1605
1606 mutex_init(&vop->vsync_mutex);
1607
Mark Yao63ebb9f2015-11-30 18:22:42 +08001608 ret = devm_request_irq(dev, vop->irq, vop_isr,
1609 IRQF_SHARED, dev_name(dev), vop);
Mark Yao2048e322014-08-22 18:36:26 +08001610 if (ret)
1611 return ret;
1612
1613 /* IRQ is initially disabled; it gets enabled in power_on */
1614 disable_irq(vop->irq);
1615
1616 ret = vop_create_crtc(vop);
1617 if (ret)
Sean Paul8c763c92016-09-16 14:22:03 -04001618 goto err_enable_irq;
Mark Yao2048e322014-08-22 18:36:26 +08001619
1620 pm_runtime_enable(&pdev->dev);
Yakir Yang5182c1a2016-07-24 14:57:44 +08001621
Jeffy Chen5e570372017-04-06 20:31:20 +08001622 ret = vop_initial(vop);
1623 if (ret < 0) {
1624 dev_err(&pdev->dev, "cannot initial vop dev - err %d\n", ret);
1625 goto err_disable_pm_runtime;
1626 }
1627
Mark Yao2048e322014-08-22 18:36:26 +08001628 return 0;
Sean Paul8c763c92016-09-16 14:22:03 -04001629
Jeffy Chen5e570372017-04-06 20:31:20 +08001630err_disable_pm_runtime:
1631 pm_runtime_disable(&pdev->dev);
1632 vop_destroy_crtc(vop);
Sean Paul8c763c92016-09-16 14:22:03 -04001633err_enable_irq:
1634 enable_irq(vop->irq); /* To balance out the disable_irq above */
1635 return ret;
Mark Yao2048e322014-08-22 18:36:26 +08001636}
1637
1638static void vop_unbind(struct device *dev, struct device *master, void *data)
1639{
1640 struct vop *vop = dev_get_drvdata(dev);
1641
1642 pm_runtime_disable(dev);
1643 vop_destroy_crtc(vop);
Jeffy Chenec6e7762017-04-06 20:31:21 +08001644
1645 clk_unprepare(vop->aclk);
1646 clk_unprepare(vop->hclk);
1647 clk_unprepare(vop->dclk);
Mark Yao2048e322014-08-22 18:36:26 +08001648}
1649
Mark Yaoa67719d2015-12-15 08:58:26 +08001650const struct component_ops vop_component_ops = {
Mark Yao2048e322014-08-22 18:36:26 +08001651 .bind = vop_bind,
1652 .unbind = vop_unbind,
1653};
Stephen Rothwell54255e82015-12-31 13:40:11 +11001654EXPORT_SYMBOL_GPL(vop_component_ops);