blob: d041921b3bb9b1f0fd67acf4f8ac43860542b6c3 [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>
17#include <drm/drm_crtc.h>
18#include <drm/drm_crtc_helper.h>
19#include <drm/drm_plane_helper.h>
20
21#include <linux/kernel.h>
22#include <linux/platform_device.h>
23#include <linux/clk.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/pm_runtime.h>
27#include <linux/component.h>
28
29#include <linux/reset.h>
30#include <linux/delay.h>
31
32#include "rockchip_drm_drv.h"
33#include "rockchip_drm_gem.h"
34#include "rockchip_drm_fb.h"
35#include "rockchip_drm_vop.h"
36
37#define VOP_REG(off, _mask, s) \
38 {.offset = off, \
39 .mask = _mask, \
40 .shift = s,}
41
42#define __REG_SET_RELAXED(x, off, mask, shift, v) \
43 vop_mask_write_relaxed(x, off, (mask) << shift, (v) << shift)
44#define __REG_SET_NORMAL(x, off, mask, shift, v) \
45 vop_mask_write(x, off, (mask) << shift, (v) << shift)
46
47#define REG_SET(x, base, reg, v, mode) \
48 __REG_SET_##mode(x, base + reg.offset, reg.mask, reg.shift, v)
49
50#define VOP_WIN_SET(x, win, name, v) \
51 REG_SET(x, win->base, win->phy->name, v, RELAXED)
52#define VOP_CTRL_SET(x, name, v) \
53 REG_SET(x, 0, (x)->data->ctrl->name, v, NORMAL)
54
55#define VOP_WIN_GET(x, win, name) \
56 vop_read_reg(x, win->base, &win->phy->name)
57
58#define VOP_WIN_GET_YRGBADDR(vop, win) \
59 vop_readl(vop, win->base + win->phy->yrgb_mst.offset)
60
61#define to_vop(x) container_of(x, struct vop, crtc)
62#define to_vop_win(x) container_of(x, struct vop_win, base)
63
64struct vop_win_state {
65 struct list_head head;
66 struct drm_framebuffer *fb;
67 dma_addr_t yrgb_mst;
68 struct drm_pending_vblank_event *event;
69};
70
71struct vop_win {
72 struct drm_plane base;
73 const struct vop_win_data *data;
74 struct vop *vop;
75
76 struct list_head pending;
77 struct vop_win_state *active;
78};
79
80struct vop {
81 struct drm_crtc crtc;
82 struct device *dev;
83 struct drm_device *drm_dev;
Mark Yao31e980c2015-01-22 14:37:56 +080084 bool is_enabled;
Mark Yao2048e322014-08-22 18:36:26 +080085
86 int connector_type;
87 int connector_out_mode;
88
89 /* mutex vsync_ work */
90 struct mutex vsync_mutex;
91 bool vsync_work_pending;
Mark Yao10672192015-02-04 13:10:31 +080092 struct completion dsp_hold_completion;
Mark Yao2048e322014-08-22 18:36:26 +080093
94 const struct vop_data *data;
95
96 uint32_t *regsbak;
97 void __iomem *regs;
98
99 /* physical map length of vop register */
100 uint32_t len;
101
102 /* one time only one process allowed to config the register */
103 spinlock_t reg_lock;
104 /* lock vop irq reg */
105 spinlock_t irq_lock;
106
107 unsigned int irq;
108
109 /* vop AHP clk */
110 struct clk *hclk;
111 /* vop dclk */
112 struct clk *dclk;
113 /* vop share memory frequency */
114 struct clk *aclk;
115
116 /* vop dclk reset */
117 struct reset_control *dclk_rst;
118
119 int pipe;
120
121 struct vop_win win[];
122};
123
124enum vop_data_format {
125 VOP_FMT_ARGB8888 = 0,
126 VOP_FMT_RGB888,
127 VOP_FMT_RGB565,
128 VOP_FMT_YUV420SP = 4,
129 VOP_FMT_YUV422SP,
130 VOP_FMT_YUV444SP,
131};
132
133struct vop_reg_data {
134 uint32_t offset;
135 uint32_t value;
136};
137
138struct vop_reg {
139 uint32_t offset;
140 uint32_t shift;
141 uint32_t mask;
142};
143
144struct vop_ctrl {
145 struct vop_reg standby;
146 struct vop_reg data_blank;
147 struct vop_reg gate_en;
148 struct vop_reg mmu_en;
149 struct vop_reg rgb_en;
150 struct vop_reg edp_en;
151 struct vop_reg hdmi_en;
152 struct vop_reg mipi_en;
153 struct vop_reg out_mode;
154 struct vop_reg dither_down;
155 struct vop_reg dither_up;
156 struct vop_reg pin_pol;
157
158 struct vop_reg htotal_pw;
159 struct vop_reg hact_st_end;
160 struct vop_reg vtotal_pw;
161 struct vop_reg vact_st_end;
162 struct vop_reg hpost_st_end;
163 struct vop_reg vpost_st_end;
164};
165
166struct vop_win_phy {
167 const uint32_t *data_formats;
168 uint32_t nformats;
169
170 struct vop_reg enable;
171 struct vop_reg format;
172 struct vop_reg act_info;
173 struct vop_reg dsp_info;
174 struct vop_reg dsp_st;
175 struct vop_reg yrgb_mst;
176 struct vop_reg uv_mst;
177 struct vop_reg yrgb_vir;
178 struct vop_reg uv_vir;
179
180 struct vop_reg dst_alpha_ctl;
181 struct vop_reg src_alpha_ctl;
182};
183
184struct vop_win_data {
185 uint32_t base;
186 const struct vop_win_phy *phy;
187 enum drm_plane_type type;
188};
189
190struct vop_data {
191 const struct vop_reg_data *init_table;
192 unsigned int table_size;
193 const struct vop_ctrl *ctrl;
194 const struct vop_win_data *win;
195 unsigned int win_size;
196};
197
198static const uint32_t formats_01[] = {
199 DRM_FORMAT_XRGB8888,
200 DRM_FORMAT_ARGB8888,
201 DRM_FORMAT_RGB888,
202 DRM_FORMAT_RGB565,
203 DRM_FORMAT_NV12,
204 DRM_FORMAT_NV16,
205 DRM_FORMAT_NV24,
206};
207
208static const uint32_t formats_234[] = {
209 DRM_FORMAT_XRGB8888,
210 DRM_FORMAT_ARGB8888,
211 DRM_FORMAT_RGB888,
212 DRM_FORMAT_RGB565,
213};
214
215static const struct vop_win_phy win01_data = {
216 .data_formats = formats_01,
217 .nformats = ARRAY_SIZE(formats_01),
218 .enable = VOP_REG(WIN0_CTRL0, 0x1, 0),
219 .format = VOP_REG(WIN0_CTRL0, 0x7, 1),
220 .act_info = VOP_REG(WIN0_ACT_INFO, 0x1fff1fff, 0),
221 .dsp_info = VOP_REG(WIN0_DSP_INFO, 0x0fff0fff, 0),
222 .dsp_st = VOP_REG(WIN0_DSP_ST, 0x1fff1fff, 0),
223 .yrgb_mst = VOP_REG(WIN0_YRGB_MST, 0xffffffff, 0),
224 .uv_mst = VOP_REG(WIN0_CBR_MST, 0xffffffff, 0),
225 .yrgb_vir = VOP_REG(WIN0_VIR, 0x3fff, 0),
226 .uv_vir = VOP_REG(WIN0_VIR, 0x3fff, 16),
227 .src_alpha_ctl = VOP_REG(WIN0_SRC_ALPHA_CTRL, 0xff, 0),
228 .dst_alpha_ctl = VOP_REG(WIN0_DST_ALPHA_CTRL, 0xff, 0),
229};
230
231static const struct vop_win_phy win23_data = {
232 .data_formats = formats_234,
233 .nformats = ARRAY_SIZE(formats_234),
234 .enable = VOP_REG(WIN2_CTRL0, 0x1, 0),
235 .format = VOP_REG(WIN2_CTRL0, 0x7, 1),
236 .dsp_info = VOP_REG(WIN2_DSP_INFO0, 0x0fff0fff, 0),
237 .dsp_st = VOP_REG(WIN2_DSP_ST0, 0x1fff1fff, 0),
238 .yrgb_mst = VOP_REG(WIN2_MST0, 0xffffffff, 0),
239 .yrgb_vir = VOP_REG(WIN2_VIR0_1, 0x1fff, 0),
240 .src_alpha_ctl = VOP_REG(WIN2_SRC_ALPHA_CTRL, 0xff, 0),
241 .dst_alpha_ctl = VOP_REG(WIN2_DST_ALPHA_CTRL, 0xff, 0),
242};
243
244static const struct vop_win_phy cursor_data = {
245 .data_formats = formats_234,
246 .nformats = ARRAY_SIZE(formats_234),
247 .enable = VOP_REG(HWC_CTRL0, 0x1, 0),
248 .format = VOP_REG(HWC_CTRL0, 0x7, 1),
249 .dsp_st = VOP_REG(HWC_DSP_ST, 0x1fff1fff, 0),
250 .yrgb_mst = VOP_REG(HWC_MST, 0xffffffff, 0),
251};
252
253static const struct vop_ctrl ctrl_data = {
254 .standby = VOP_REG(SYS_CTRL, 0x1, 22),
255 .gate_en = VOP_REG(SYS_CTRL, 0x1, 23),
256 .mmu_en = VOP_REG(SYS_CTRL, 0x1, 20),
257 .rgb_en = VOP_REG(SYS_CTRL, 0x1, 12),
258 .hdmi_en = VOP_REG(SYS_CTRL, 0x1, 13),
259 .edp_en = VOP_REG(SYS_CTRL, 0x1, 14),
260 .mipi_en = VOP_REG(SYS_CTRL, 0x1, 15),
261 .dither_down = VOP_REG(DSP_CTRL1, 0xf, 1),
262 .dither_up = VOP_REG(DSP_CTRL1, 0x1, 6),
263 .data_blank = VOP_REG(DSP_CTRL0, 0x1, 19),
264 .out_mode = VOP_REG(DSP_CTRL0, 0xf, 0),
265 .pin_pol = VOP_REG(DSP_CTRL0, 0xf, 4),
266 .htotal_pw = VOP_REG(DSP_HTOTAL_HS_END, 0x1fff1fff, 0),
267 .hact_st_end = VOP_REG(DSP_HACT_ST_END, 0x1fff1fff, 0),
268 .vtotal_pw = VOP_REG(DSP_VTOTAL_VS_END, 0x1fff1fff, 0),
269 .vact_st_end = VOP_REG(DSP_VACT_ST_END, 0x1fff1fff, 0),
270 .hpost_st_end = VOP_REG(POST_DSP_HACT_INFO, 0x1fff1fff, 0),
271 .vpost_st_end = VOP_REG(POST_DSP_VACT_INFO, 0x1fff1fff, 0),
272};
273
274static const struct vop_reg_data vop_init_reg_table[] = {
275 {SYS_CTRL, 0x00c00000},
276 {DSP_CTRL0, 0x00000000},
277 {WIN0_CTRL0, 0x00000080},
278 {WIN1_CTRL0, 0x00000080},
279};
280
281/*
282 * Note: rk3288 has a dedicated 'cursor' window, however, that window requires
283 * special support to get alpha blending working. For now, just use overlay
284 * window 1 for the drm cursor.
285 */
286static const struct vop_win_data rk3288_vop_win_data[] = {
287 { .base = 0x00, .phy = &win01_data, .type = DRM_PLANE_TYPE_PRIMARY },
288 { .base = 0x40, .phy = &win01_data, .type = DRM_PLANE_TYPE_CURSOR },
289 { .base = 0x00, .phy = &win23_data, .type = DRM_PLANE_TYPE_OVERLAY },
290 { .base = 0x50, .phy = &win23_data, .type = DRM_PLANE_TYPE_OVERLAY },
291 { .base = 0x00, .phy = &cursor_data, .type = DRM_PLANE_TYPE_OVERLAY },
292};
293
294static const struct vop_data rk3288_vop = {
295 .init_table = vop_init_reg_table,
296 .table_size = ARRAY_SIZE(vop_init_reg_table),
297 .ctrl = &ctrl_data,
298 .win = rk3288_vop_win_data,
299 .win_size = ARRAY_SIZE(rk3288_vop_win_data),
300};
301
302static const struct of_device_id vop_driver_dt_match[] = {
303 { .compatible = "rockchip,rk3288-vop",
304 .data = &rk3288_vop },
305 {},
306};
307
308static inline void vop_writel(struct vop *vop, uint32_t offset, uint32_t v)
309{
310 writel(v, vop->regs + offset);
311 vop->regsbak[offset >> 2] = v;
312}
313
314static inline uint32_t vop_readl(struct vop *vop, uint32_t offset)
315{
316 return readl(vop->regs + offset);
317}
318
319static inline uint32_t vop_read_reg(struct vop *vop, uint32_t base,
320 const struct vop_reg *reg)
321{
322 return (vop_readl(vop, base + reg->offset) >> reg->shift) & reg->mask;
323}
324
325static inline void vop_cfg_done(struct vop *vop)
326{
327 writel(0x01, vop->regs + REG_CFG_DONE);
328}
329
330static inline void vop_mask_write(struct vop *vop, uint32_t offset,
331 uint32_t mask, uint32_t v)
332{
333 if (mask) {
334 uint32_t cached_val = vop->regsbak[offset >> 2];
335
336 cached_val = (cached_val & ~mask) | v;
337 writel(cached_val, vop->regs + offset);
338 vop->regsbak[offset >> 2] = cached_val;
339 }
340}
341
342static inline void vop_mask_write_relaxed(struct vop *vop, uint32_t offset,
343 uint32_t mask, uint32_t v)
344{
345 if (mask) {
346 uint32_t cached_val = vop->regsbak[offset >> 2];
347
348 cached_val = (cached_val & ~mask) | v;
349 writel_relaxed(cached_val, vop->regs + offset);
350 vop->regsbak[offset >> 2] = cached_val;
351 }
352}
353
354static enum vop_data_format vop_convert_format(uint32_t format)
355{
356 switch (format) {
357 case DRM_FORMAT_XRGB8888:
358 case DRM_FORMAT_ARGB8888:
359 return VOP_FMT_ARGB8888;
360 case DRM_FORMAT_RGB888:
361 return VOP_FMT_RGB888;
362 case DRM_FORMAT_RGB565:
363 return VOP_FMT_RGB565;
364 case DRM_FORMAT_NV12:
365 return VOP_FMT_YUV420SP;
366 case DRM_FORMAT_NV16:
367 return VOP_FMT_YUV422SP;
368 case DRM_FORMAT_NV24:
369 return VOP_FMT_YUV444SP;
370 default:
371 DRM_ERROR("unsupport format[%08x]\n", format);
372 return -EINVAL;
373 }
374}
375
376static bool is_alpha_support(uint32_t format)
377{
378 switch (format) {
379 case DRM_FORMAT_ARGB8888:
380 return true;
381 default:
382 return false;
383 }
384}
385
Mark Yao10672192015-02-04 13:10:31 +0800386static void vop_dsp_hold_valid_irq_enable(struct vop *vop)
387{
388 unsigned long flags;
389
390 if (WARN_ON(!vop->is_enabled))
391 return;
392
393 spin_lock_irqsave(&vop->irq_lock, flags);
394
395 vop_mask_write(vop, INTR_CTRL0, DSP_HOLD_VALID_INTR_MASK,
396 DSP_HOLD_VALID_INTR_EN(1));
397
398 spin_unlock_irqrestore(&vop->irq_lock, flags);
399}
400
401static void vop_dsp_hold_valid_irq_disable(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
410 vop_mask_write(vop, INTR_CTRL0, DSP_HOLD_VALID_INTR_MASK,
411 DSP_HOLD_VALID_INTR_EN(0));
412
413 spin_unlock_irqrestore(&vop->irq_lock, flags);
414}
415
Mark Yao2048e322014-08-22 18:36:26 +0800416static void vop_enable(struct drm_crtc *crtc)
417{
418 struct vop *vop = to_vop(crtc);
419 int ret;
420
Mark Yao31e980c2015-01-22 14:37:56 +0800421 if (vop->is_enabled)
422 return;
423
Mark Yao2048e322014-08-22 18:36:26 +0800424 ret = clk_enable(vop->hclk);
425 if (ret < 0) {
426 dev_err(vop->dev, "failed to enable hclk - %d\n", ret);
427 return;
428 }
429
430 ret = clk_enable(vop->dclk);
431 if (ret < 0) {
432 dev_err(vop->dev, "failed to enable dclk - %d\n", ret);
433 goto err_disable_hclk;
434 }
435
436 ret = clk_enable(vop->aclk);
437 if (ret < 0) {
438 dev_err(vop->dev, "failed to enable aclk - %d\n", ret);
439 goto err_disable_dclk;
440 }
441
442 /*
443 * Slave iommu shares power, irq and clock with vop. It was associated
444 * automatically with this master device via common driver code.
445 * Now that we have enabled the clock we attach it to the shared drm
446 * mapping.
447 */
448 ret = rockchip_drm_dma_attach_device(vop->drm_dev, vop->dev);
449 if (ret) {
450 dev_err(vop->dev, "failed to attach dma mapping, %d\n", ret);
451 goto err_disable_aclk;
452 }
453
Mark Yao52ab7892015-01-22 18:29:57 +0800454 /*
455 * At here, vop clock & iommu is enable, R/W vop regs would be safe.
456 */
457 vop->is_enabled = true;
458
Mark Yao2048e322014-08-22 18:36:26 +0800459 spin_lock(&vop->reg_lock);
460
461 VOP_CTRL_SET(vop, standby, 0);
462
463 spin_unlock(&vop->reg_lock);
464
465 enable_irq(vop->irq);
466
467 drm_vblank_on(vop->drm_dev, vop->pipe);
468
469 return;
470
471err_disable_aclk:
472 clk_disable(vop->aclk);
473err_disable_dclk:
474 clk_disable(vop->dclk);
475err_disable_hclk:
476 clk_disable(vop->hclk);
477}
478
479static void vop_disable(struct drm_crtc *crtc)
480{
481 struct vop *vop = to_vop(crtc);
482
Mark Yao31e980c2015-01-22 14:37:56 +0800483 if (!vop->is_enabled)
484 return;
485
Mark Yao2048e322014-08-22 18:36:26 +0800486 drm_vblank_off(crtc->dev, vop->pipe);
487
Mark Yao2048e322014-08-22 18:36:26 +0800488 /*
Mark Yao10672192015-02-04 13:10:31 +0800489 * Vop standby will take effect at end of current frame,
490 * if dsp hold valid irq happen, it means standby complete.
491 *
492 * we must wait standby complete when we want to disable aclk,
493 * if not, memory bus maybe dead.
Mark Yao2048e322014-08-22 18:36:26 +0800494 */
Mark Yao10672192015-02-04 13:10:31 +0800495 reinit_completion(&vop->dsp_hold_completion);
496 vop_dsp_hold_valid_irq_enable(vop);
497
Mark Yao2048e322014-08-22 18:36:26 +0800498 spin_lock(&vop->reg_lock);
499
500 VOP_CTRL_SET(vop, standby, 1);
501
502 spin_unlock(&vop->reg_lock);
Mark Yao52ab7892015-01-22 18:29:57 +0800503
Mark Yao10672192015-02-04 13:10:31 +0800504 wait_for_completion(&vop->dsp_hold_completion);
Mark Yao2048e322014-08-22 18:36:26 +0800505
Mark Yao10672192015-02-04 13:10:31 +0800506 vop_dsp_hold_valid_irq_disable(vop);
507
508 disable_irq(vop->irq);
509
510 vop->is_enabled = false;
511
512 /*
513 * vop standby complete, so iommu detach is safe.
514 */
Mark Yao2048e322014-08-22 18:36:26 +0800515 rockchip_drm_dma_detach_device(vop->drm_dev, vop->dev);
516
Mark Yao10672192015-02-04 13:10:31 +0800517 clk_disable(vop->dclk);
Mark Yao2048e322014-08-22 18:36:26 +0800518 clk_disable(vop->aclk);
519 clk_disable(vop->hclk);
520}
521
522/*
523 * Caller must hold vsync_mutex.
524 */
525static struct drm_framebuffer *vop_win_last_pending_fb(struct vop_win *vop_win)
526{
527 struct vop_win_state *last;
528 struct vop_win_state *active = vop_win->active;
529
530 if (list_empty(&vop_win->pending))
531 return active ? active->fb : NULL;
532
533 last = list_last_entry(&vop_win->pending, struct vop_win_state, head);
534 return last ? last->fb : NULL;
535}
536
537/*
538 * Caller must hold vsync_mutex.
539 */
540static int vop_win_queue_fb(struct vop_win *vop_win,
541 struct drm_framebuffer *fb, dma_addr_t yrgb_mst,
542 struct drm_pending_vblank_event *event)
543{
544 struct vop_win_state *state;
545
546 state = kzalloc(sizeof(*state), GFP_KERNEL);
547 if (!state)
548 return -ENOMEM;
549
550 state->fb = fb;
551 state->yrgb_mst = yrgb_mst;
552 state->event = event;
553
554 list_add_tail(&state->head, &vop_win->pending);
555
556 return 0;
557}
558
559static int vop_update_plane_event(struct drm_plane *plane,
560 struct drm_crtc *crtc,
561 struct drm_framebuffer *fb, int crtc_x,
562 int crtc_y, unsigned int crtc_w,
563 unsigned int crtc_h, uint32_t src_x,
564 uint32_t src_y, uint32_t src_w,
565 uint32_t src_h,
566 struct drm_pending_vblank_event *event)
567{
568 struct vop_win *vop_win = to_vop_win(plane);
569 const struct vop_win_data *win = vop_win->data;
570 struct vop *vop = to_vop(crtc);
571 struct drm_gem_object *obj;
572 struct rockchip_gem_object *rk_obj;
573 unsigned long offset;
574 unsigned int actual_w;
575 unsigned int actual_h;
576 unsigned int dsp_stx;
577 unsigned int dsp_sty;
578 unsigned int y_vir_stride;
579 dma_addr_t yrgb_mst;
580 enum vop_data_format format;
581 uint32_t val;
582 bool is_alpha;
583 bool visible;
584 int ret;
585 struct drm_rect dest = {
586 .x1 = crtc_x,
587 .y1 = crtc_y,
588 .x2 = crtc_x + crtc_w,
589 .y2 = crtc_y + crtc_h,
590 };
591 struct drm_rect src = {
592 /* 16.16 fixed point */
593 .x1 = src_x,
594 .y1 = src_y,
595 .x2 = src_x + src_w,
596 .y2 = src_y + src_h,
597 };
598 const struct drm_rect clip = {
599 .x2 = crtc->mode.hdisplay,
600 .y2 = crtc->mode.vdisplay,
601 };
602 bool can_position = plane->type != DRM_PLANE_TYPE_PRIMARY;
603
604 ret = drm_plane_helper_check_update(plane, crtc, fb,
605 &src, &dest, &clip,
606 DRM_PLANE_HELPER_NO_SCALING,
607 DRM_PLANE_HELPER_NO_SCALING,
608 can_position, false, &visible);
609 if (ret)
610 return ret;
611
612 if (!visible)
613 return 0;
614
615 is_alpha = is_alpha_support(fb->pixel_format);
616 format = vop_convert_format(fb->pixel_format);
617 if (format < 0)
618 return format;
619
620 obj = rockchip_fb_get_gem_obj(fb, 0);
621 if (!obj) {
622 DRM_ERROR("fail to get rockchip gem object from framebuffer\n");
623 return -EINVAL;
624 }
625
626 rk_obj = to_rockchip_obj(obj);
627
628 actual_w = (src.x2 - src.x1) >> 16;
629 actual_h = (src.y2 - src.y1) >> 16;
630 crtc_x = max(0, crtc_x);
631 crtc_y = max(0, crtc_y);
632
633 dsp_stx = crtc_x + crtc->mode.htotal - crtc->mode.hsync_start;
634 dsp_sty = crtc_y + crtc->mode.vtotal - crtc->mode.vsync_start;
635
636 offset = (src.x1 >> 16) * (fb->bits_per_pixel >> 3);
637 offset += (src.y1 >> 16) * fb->pitches[0];
638 yrgb_mst = rk_obj->dma_addr + offset;
639
640 y_vir_stride = fb->pitches[0] / (fb->bits_per_pixel >> 3);
641
642 /*
643 * If this plane update changes the plane's framebuffer, (or more
644 * precisely, if this update has a different framebuffer than the last
645 * update), enqueue it so we can track when it completes.
646 *
647 * Only when we discover that this update has completed, can we
648 * unreference any previous framebuffers.
649 */
650 mutex_lock(&vop->vsync_mutex);
651 if (fb != vop_win_last_pending_fb(vop_win)) {
652 ret = drm_vblank_get(plane->dev, vop->pipe);
653 if (ret) {
654 DRM_ERROR("failed to get vblank, %d\n", ret);
655 mutex_unlock(&vop->vsync_mutex);
656 return ret;
657 }
658
659 drm_framebuffer_reference(fb);
660
661 ret = vop_win_queue_fb(vop_win, fb, yrgb_mst, event);
662 if (ret) {
663 drm_vblank_put(plane->dev, vop->pipe);
664 mutex_unlock(&vop->vsync_mutex);
665 return ret;
666 }
667
668 vop->vsync_work_pending = true;
669 }
670 mutex_unlock(&vop->vsync_mutex);
671
672 spin_lock(&vop->reg_lock);
673
674 VOP_WIN_SET(vop, win, format, format);
675 VOP_WIN_SET(vop, win, yrgb_vir, y_vir_stride);
676 VOP_WIN_SET(vop, win, yrgb_mst, yrgb_mst);
677 val = (actual_h - 1) << 16;
678 val |= (actual_w - 1) & 0xffff;
679 VOP_WIN_SET(vop, win, act_info, val);
680 VOP_WIN_SET(vop, win, dsp_info, val);
681 val = (dsp_sty - 1) << 16;
682 val |= (dsp_stx - 1) & 0xffff;
683 VOP_WIN_SET(vop, win, dsp_st, val);
684
685 if (is_alpha) {
686 VOP_WIN_SET(vop, win, dst_alpha_ctl,
687 DST_FACTOR_M0(ALPHA_SRC_INVERSE));
688 val = SRC_ALPHA_EN(1) | SRC_COLOR_M0(ALPHA_SRC_PRE_MUL) |
689 SRC_ALPHA_M0(ALPHA_STRAIGHT) |
690 SRC_BLEND_M0(ALPHA_PER_PIX) |
691 SRC_ALPHA_CAL_M0(ALPHA_NO_SATURATION) |
692 SRC_FACTOR_M0(ALPHA_ONE);
693 VOP_WIN_SET(vop, win, src_alpha_ctl, val);
694 } else {
695 VOP_WIN_SET(vop, win, src_alpha_ctl, SRC_ALPHA_EN(0));
696 }
697
698 VOP_WIN_SET(vop, win, enable, 1);
699
700 vop_cfg_done(vop);
701 spin_unlock(&vop->reg_lock);
702
703 return 0;
704}
705
706static int vop_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
707 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
708 unsigned int crtc_w, unsigned int crtc_h,
709 uint32_t src_x, uint32_t src_y, uint32_t src_w,
710 uint32_t src_h)
711{
712 return vop_update_plane_event(plane, crtc, fb, crtc_x, crtc_y, crtc_w,
713 crtc_h, src_x, src_y, src_w, src_h,
714 NULL);
715}
716
717static int vop_update_primary_plane(struct drm_crtc *crtc,
718 struct drm_pending_vblank_event *event)
719{
720 unsigned int crtc_w, crtc_h;
721
722 crtc_w = crtc->primary->fb->width - crtc->x;
723 crtc_h = crtc->primary->fb->height - crtc->y;
724
725 return vop_update_plane_event(crtc->primary, crtc, crtc->primary->fb,
726 0, 0, crtc_w, crtc_h, crtc->x << 16,
727 crtc->y << 16, crtc_w << 16,
728 crtc_h << 16, event);
729}
730
731static int vop_disable_plane(struct drm_plane *plane)
732{
733 struct vop_win *vop_win = to_vop_win(plane);
734 const struct vop_win_data *win = vop_win->data;
735 struct vop *vop;
736 int ret;
737
738 if (!plane->crtc)
739 return 0;
740
741 vop = to_vop(plane->crtc);
742
743 ret = drm_vblank_get(plane->dev, vop->pipe);
744 if (ret) {
745 DRM_ERROR("failed to get vblank, %d\n", ret);
746 return ret;
747 }
748
749 mutex_lock(&vop->vsync_mutex);
750
751 ret = vop_win_queue_fb(vop_win, NULL, 0, NULL);
752 if (ret) {
753 drm_vblank_put(plane->dev, vop->pipe);
754 mutex_unlock(&vop->vsync_mutex);
755 return ret;
756 }
757
758 vop->vsync_work_pending = true;
759 mutex_unlock(&vop->vsync_mutex);
760
761 spin_lock(&vop->reg_lock);
762 VOP_WIN_SET(vop, win, enable, 0);
763 vop_cfg_done(vop);
764 spin_unlock(&vop->reg_lock);
765
766 return 0;
767}
768
769static void vop_plane_destroy(struct drm_plane *plane)
770{
771 vop_disable_plane(plane);
772 drm_plane_cleanup(plane);
773}
774
775static const struct drm_plane_funcs vop_plane_funcs = {
776 .update_plane = vop_update_plane,
777 .disable_plane = vop_disable_plane,
778 .destroy = vop_plane_destroy,
779};
780
781int rockchip_drm_crtc_mode_config(struct drm_crtc *crtc,
782 int connector_type,
783 int out_mode)
784{
785 struct vop *vop = to_vop(crtc);
786
787 vop->connector_type = connector_type;
788 vop->connector_out_mode = out_mode;
789
790 return 0;
791}
Philipp Zabelf66a1622015-01-07 16:16:18 +0100792EXPORT_SYMBOL_GPL(rockchip_drm_crtc_mode_config);
Mark Yao2048e322014-08-22 18:36:26 +0800793
794static int vop_crtc_enable_vblank(struct drm_crtc *crtc)
795{
796 struct vop *vop = to_vop(crtc);
797 unsigned long flags;
798
Mark Yao31e980c2015-01-22 14:37:56 +0800799 if (!vop->is_enabled)
Mark Yao2048e322014-08-22 18:36:26 +0800800 return -EPERM;
801
802 spin_lock_irqsave(&vop->irq_lock, flags);
803
804 vop_mask_write(vop, INTR_CTRL0, FS_INTR_MASK, FS_INTR_EN(1));
805
806 spin_unlock_irqrestore(&vop->irq_lock, flags);
807
808 return 0;
809}
810
811static void vop_crtc_disable_vblank(struct drm_crtc *crtc)
812{
813 struct vop *vop = to_vop(crtc);
814 unsigned long flags;
815
Mark Yao31e980c2015-01-22 14:37:56 +0800816 if (!vop->is_enabled)
Mark Yao2048e322014-08-22 18:36:26 +0800817 return;
Mark Yao31e980c2015-01-22 14:37:56 +0800818
Mark Yao2048e322014-08-22 18:36:26 +0800819 spin_lock_irqsave(&vop->irq_lock, flags);
820 vop_mask_write(vop, INTR_CTRL0, FS_INTR_MASK, FS_INTR_EN(0));
821 spin_unlock_irqrestore(&vop->irq_lock, flags);
822}
823
824static const struct rockchip_crtc_funcs private_crtc_funcs = {
825 .enable_vblank = vop_crtc_enable_vblank,
826 .disable_vblank = vop_crtc_disable_vblank,
827};
828
829static void vop_crtc_dpms(struct drm_crtc *crtc, int mode)
830{
Mark Yao2048e322014-08-22 18:36:26 +0800831 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
832
Mark Yao2048e322014-08-22 18:36:26 +0800833 switch (mode) {
834 case DRM_MODE_DPMS_ON:
835 vop_enable(crtc);
836 break;
837 case DRM_MODE_DPMS_STANDBY:
838 case DRM_MODE_DPMS_SUSPEND:
839 case DRM_MODE_DPMS_OFF:
840 vop_disable(crtc);
841 break;
842 default:
843 DRM_DEBUG_KMS("unspecified mode %d\n", mode);
844 break;
845 }
Mark Yao2048e322014-08-22 18:36:26 +0800846}
847
848static void vop_crtc_prepare(struct drm_crtc *crtc)
849{
850 vop_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
851}
852
853static bool vop_crtc_mode_fixup(struct drm_crtc *crtc,
854 const struct drm_display_mode *mode,
855 struct drm_display_mode *adjusted_mode)
856{
857 if (adjusted_mode->htotal == 0 || adjusted_mode->vtotal == 0)
858 return false;
859
860 return true;
861}
862
863static int vop_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
864 struct drm_framebuffer *old_fb)
865{
866 int ret;
867
868 crtc->x = x;
869 crtc->y = y;
870
871 ret = vop_update_primary_plane(crtc, NULL);
872 if (ret < 0) {
873 DRM_ERROR("fail to update plane\n");
874 return ret;
875 }
876
877 return 0;
878}
879
880static int vop_crtc_mode_set(struct drm_crtc *crtc,
881 struct drm_display_mode *mode,
882 struct drm_display_mode *adjusted_mode,
883 int x, int y, struct drm_framebuffer *fb)
884{
885 struct vop *vop = to_vop(crtc);
886 u16 hsync_len = adjusted_mode->hsync_end - adjusted_mode->hsync_start;
887 u16 hdisplay = adjusted_mode->hdisplay;
888 u16 htotal = adjusted_mode->htotal;
889 u16 hact_st = adjusted_mode->htotal - adjusted_mode->hsync_start;
890 u16 hact_end = hact_st + hdisplay;
891 u16 vdisplay = adjusted_mode->vdisplay;
892 u16 vtotal = adjusted_mode->vtotal;
893 u16 vsync_len = adjusted_mode->vsync_end - adjusted_mode->vsync_start;
894 u16 vact_st = adjusted_mode->vtotal - adjusted_mode->vsync_start;
895 u16 vact_end = vact_st + vdisplay;
896 int ret;
897 uint32_t val;
898
899 /*
900 * disable dclk to stop frame scan, so that we can safe config mode and
901 * enable iommu.
902 */
903 clk_disable(vop->dclk);
904
905 switch (vop->connector_type) {
906 case DRM_MODE_CONNECTOR_LVDS:
907 VOP_CTRL_SET(vop, rgb_en, 1);
908 break;
909 case DRM_MODE_CONNECTOR_eDP:
910 VOP_CTRL_SET(vop, edp_en, 1);
911 break;
912 case DRM_MODE_CONNECTOR_HDMIA:
913 VOP_CTRL_SET(vop, hdmi_en, 1);
914 break;
915 default:
916 DRM_ERROR("unsupport connector_type[%d]\n",
917 vop->connector_type);
918 return -EINVAL;
919 };
920 VOP_CTRL_SET(vop, out_mode, vop->connector_out_mode);
921
922 val = 0x8;
Mark Yao44ddb7e2015-01-22 11:15:02 +0800923 val |= (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC) ? 0 : 1;
924 val |= (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC) ? 0 : (1 << 1);
Mark Yao2048e322014-08-22 18:36:26 +0800925 VOP_CTRL_SET(vop, pin_pol, val);
926
927 VOP_CTRL_SET(vop, htotal_pw, (htotal << 16) | hsync_len);
928 val = hact_st << 16;
929 val |= hact_end;
930 VOP_CTRL_SET(vop, hact_st_end, val);
931 VOP_CTRL_SET(vop, hpost_st_end, val);
932
933 VOP_CTRL_SET(vop, vtotal_pw, (vtotal << 16) | vsync_len);
934 val = vact_st << 16;
935 val |= vact_end;
936 VOP_CTRL_SET(vop, vact_st_end, val);
937 VOP_CTRL_SET(vop, vpost_st_end, val);
938
939 ret = vop_crtc_mode_set_base(crtc, x, y, fb);
940 if (ret)
941 return ret;
942
943 /*
944 * reset dclk, take all mode config affect, so the clk would run in
945 * correct frame.
946 */
947 reset_control_assert(vop->dclk_rst);
948 usleep_range(10, 20);
949 reset_control_deassert(vop->dclk_rst);
950
951 clk_set_rate(vop->dclk, adjusted_mode->clock * 1000);
952 ret = clk_enable(vop->dclk);
953 if (ret < 0) {
954 dev_err(vop->dev, "failed to enable dclk - %d\n", ret);
955 return ret;
956 }
957
958 return 0;
959}
960
961static void vop_crtc_commit(struct drm_crtc *crtc)
962{
963}
964
965static const struct drm_crtc_helper_funcs vop_crtc_helper_funcs = {
966 .dpms = vop_crtc_dpms,
967 .prepare = vop_crtc_prepare,
968 .mode_fixup = vop_crtc_mode_fixup,
969 .mode_set = vop_crtc_mode_set,
970 .mode_set_base = vop_crtc_mode_set_base,
971 .commit = vop_crtc_commit,
972};
973
974static int vop_crtc_page_flip(struct drm_crtc *crtc,
975 struct drm_framebuffer *fb,
976 struct drm_pending_vblank_event *event,
977 uint32_t page_flip_flags)
978{
979 struct vop *vop = to_vop(crtc);
980 struct drm_framebuffer *old_fb = crtc->primary->fb;
981 int ret;
982
Mark Yao31e980c2015-01-22 14:37:56 +0800983 /* when the page flip is requested, crtc should be on */
984 if (!vop->is_enabled) {
985 DRM_DEBUG("page flip request rejected because crtc is off.\n");
Mark Yao2048e322014-08-22 18:36:26 +0800986 return 0;
987 }
988
989 crtc->primary->fb = fb;
990
991 ret = vop_update_primary_plane(crtc, event);
992 if (ret)
993 crtc->primary->fb = old_fb;
994
995 return ret;
996}
997
998static void vop_win_state_complete(struct vop_win *vop_win,
999 struct vop_win_state *state)
1000{
1001 struct vop *vop = vop_win->vop;
1002 struct drm_crtc *crtc = &vop->crtc;
1003 struct drm_device *drm = crtc->dev;
1004 unsigned long flags;
1005
1006 if (state->event) {
1007 spin_lock_irqsave(&drm->event_lock, flags);
1008 drm_send_vblank_event(drm, -1, state->event);
1009 spin_unlock_irqrestore(&drm->event_lock, flags);
1010 }
1011
1012 list_del(&state->head);
1013 drm_vblank_put(crtc->dev, vop->pipe);
1014}
1015
1016static void vop_crtc_destroy(struct drm_crtc *crtc)
1017{
1018 drm_crtc_cleanup(crtc);
1019}
1020
1021static const struct drm_crtc_funcs vop_crtc_funcs = {
1022 .set_config = drm_crtc_helper_set_config,
1023 .page_flip = vop_crtc_page_flip,
1024 .destroy = vop_crtc_destroy,
1025};
1026
1027static bool vop_win_state_is_active(struct vop_win *vop_win,
1028 struct vop_win_state *state)
1029{
1030 bool active = false;
1031
1032 if (state->fb) {
1033 dma_addr_t yrgb_mst;
1034
1035 /* check yrgb_mst to tell if pending_fb is now front */
1036 yrgb_mst = VOP_WIN_GET_YRGBADDR(vop_win->vop, vop_win->data);
1037
1038 active = (yrgb_mst == state->yrgb_mst);
1039 } else {
1040 bool enabled;
1041
1042 /* if enable bit is clear, plane is now disabled */
1043 enabled = VOP_WIN_GET(vop_win->vop, vop_win->data, enable);
1044
1045 active = (enabled == 0);
1046 }
1047
1048 return active;
1049}
1050
1051static void vop_win_state_destroy(struct vop_win_state *state)
1052{
1053 struct drm_framebuffer *fb = state->fb;
1054
1055 if (fb)
1056 drm_framebuffer_unreference(fb);
1057
1058 kfree(state);
1059}
1060
1061static void vop_win_update_state(struct vop_win *vop_win)
1062{
1063 struct vop_win_state *state, *n, *new_active = NULL;
1064
1065 /* Check if any pending states are now active */
1066 list_for_each_entry(state, &vop_win->pending, head)
1067 if (vop_win_state_is_active(vop_win, state)) {
1068 new_active = state;
1069 break;
1070 }
1071
1072 if (!new_active)
1073 return;
1074
1075 /*
1076 * Destroy any 'skipped' pending states - states that were queued
1077 * before the newly active state.
1078 */
1079 list_for_each_entry_safe(state, n, &vop_win->pending, head) {
1080 if (state == new_active)
1081 break;
1082 vop_win_state_complete(vop_win, state);
1083 vop_win_state_destroy(state);
1084 }
1085
1086 vop_win_state_complete(vop_win, new_active);
1087
1088 if (vop_win->active)
1089 vop_win_state_destroy(vop_win->active);
1090 vop_win->active = new_active;
1091}
1092
1093static bool vop_win_has_pending_state(struct vop_win *vop_win)
1094{
1095 return !list_empty(&vop_win->pending);
1096}
1097
1098static irqreturn_t vop_isr_thread(int irq, void *data)
1099{
1100 struct vop *vop = data;
1101 const struct vop_data *vop_data = vop->data;
1102 unsigned int i;
1103
1104 mutex_lock(&vop->vsync_mutex);
1105
1106 if (!vop->vsync_work_pending)
1107 goto done;
1108
1109 vop->vsync_work_pending = false;
1110
1111 for (i = 0; i < vop_data->win_size; i++) {
1112 struct vop_win *vop_win = &vop->win[i];
1113
1114 vop_win_update_state(vop_win);
1115 if (vop_win_has_pending_state(vop_win))
1116 vop->vsync_work_pending = true;
1117 }
1118
1119done:
1120 mutex_unlock(&vop->vsync_mutex);
1121
1122 return IRQ_HANDLED;
1123}
1124
1125static irqreturn_t vop_isr(int irq, void *data)
1126{
1127 struct vop *vop = data;
1128 uint32_t intr0_reg, active_irqs;
1129 unsigned long flags;
Mark Yao10672192015-02-04 13:10:31 +08001130 int ret = IRQ_NONE;
Mark Yao2048e322014-08-22 18:36:26 +08001131
1132 /*
1133 * INTR_CTRL0 register has interrupt status, enable and clear bits, we
1134 * must hold irq_lock to avoid a race with enable/disable_vblank().
1135 */
1136 spin_lock_irqsave(&vop->irq_lock, flags);
1137 intr0_reg = vop_readl(vop, INTR_CTRL0);
1138 active_irqs = intr0_reg & INTR_MASK;
1139 /* Clear all active interrupt sources */
1140 if (active_irqs)
1141 vop_writel(vop, INTR_CTRL0,
1142 intr0_reg | (active_irqs << INTR_CLR_SHIFT));
1143 spin_unlock_irqrestore(&vop->irq_lock, flags);
1144
1145 /* This is expected for vop iommu irqs, since the irq is shared */
1146 if (!active_irqs)
1147 return IRQ_NONE;
1148
Mark Yao10672192015-02-04 13:10:31 +08001149 if (active_irqs & DSP_HOLD_VALID_INTR) {
1150 complete(&vop->dsp_hold_completion);
1151 active_irqs &= ~DSP_HOLD_VALID_INTR;
1152 ret = IRQ_HANDLED;
Mark Yao2048e322014-08-22 18:36:26 +08001153 }
1154
Mark Yao10672192015-02-04 13:10:31 +08001155 if (active_irqs & FS_INTR) {
1156 drm_handle_vblank(vop->drm_dev, vop->pipe);
1157 active_irqs &= ~FS_INTR;
1158 ret = (vop->vsync_work_pending) ? IRQ_WAKE_THREAD : IRQ_HANDLED;
1159 }
Mark Yao2048e322014-08-22 18:36:26 +08001160
Mark Yao10672192015-02-04 13:10:31 +08001161 /* Unhandled irqs are spurious. */
1162 if (active_irqs)
1163 DRM_ERROR("Unknown VOP IRQs: %#02x\n", active_irqs);
1164
1165 return ret;
Mark Yao2048e322014-08-22 18:36:26 +08001166}
1167
1168static int vop_create_crtc(struct vop *vop)
1169{
1170 const struct vop_data *vop_data = vop->data;
1171 struct device *dev = vop->dev;
1172 struct drm_device *drm_dev = vop->drm_dev;
1173 struct drm_plane *primary = NULL, *cursor = NULL, *plane;
1174 struct drm_crtc *crtc = &vop->crtc;
1175 struct device_node *port;
1176 int ret;
1177 int i;
1178
1179 /*
1180 * Create drm_plane for primary and cursor planes first, since we need
1181 * to pass them to drm_crtc_init_with_planes, which sets the
1182 * "possible_crtcs" to the newly initialized crtc.
1183 */
1184 for (i = 0; i < vop_data->win_size; i++) {
1185 struct vop_win *vop_win = &vop->win[i];
1186 const struct vop_win_data *win_data = vop_win->data;
1187
1188 if (win_data->type != DRM_PLANE_TYPE_PRIMARY &&
1189 win_data->type != DRM_PLANE_TYPE_CURSOR)
1190 continue;
1191
1192 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1193 0, &vop_plane_funcs,
1194 win_data->phy->data_formats,
1195 win_data->phy->nformats,
1196 win_data->type);
1197 if (ret) {
1198 DRM_ERROR("failed to initialize plane\n");
1199 goto err_cleanup_planes;
1200 }
1201
1202 plane = &vop_win->base;
1203 if (plane->type == DRM_PLANE_TYPE_PRIMARY)
1204 primary = plane;
1205 else if (plane->type == DRM_PLANE_TYPE_CURSOR)
1206 cursor = plane;
1207 }
1208
1209 ret = drm_crtc_init_with_planes(drm_dev, crtc, primary, cursor,
1210 &vop_crtc_funcs);
1211 if (ret)
1212 return ret;
1213
1214 drm_crtc_helper_add(crtc, &vop_crtc_helper_funcs);
1215
1216 /*
1217 * Create drm_planes for overlay windows with possible_crtcs restricted
1218 * to the newly created crtc.
1219 */
1220 for (i = 0; i < vop_data->win_size; i++) {
1221 struct vop_win *vop_win = &vop->win[i];
1222 const struct vop_win_data *win_data = vop_win->data;
1223 unsigned long possible_crtcs = 1 << drm_crtc_index(crtc);
1224
1225 if (win_data->type != DRM_PLANE_TYPE_OVERLAY)
1226 continue;
1227
1228 ret = drm_universal_plane_init(vop->drm_dev, &vop_win->base,
1229 possible_crtcs,
1230 &vop_plane_funcs,
1231 win_data->phy->data_formats,
1232 win_data->phy->nformats,
1233 win_data->type);
1234 if (ret) {
1235 DRM_ERROR("failed to initialize overlay plane\n");
1236 goto err_cleanup_crtc;
1237 }
1238 }
1239
1240 port = of_get_child_by_name(dev->of_node, "port");
1241 if (!port) {
1242 DRM_ERROR("no port node found in %s\n",
1243 dev->of_node->full_name);
1244 goto err_cleanup_crtc;
1245 }
1246
Mark Yao10672192015-02-04 13:10:31 +08001247 init_completion(&vop->dsp_hold_completion);
Mark Yao2048e322014-08-22 18:36:26 +08001248 crtc->port = port;
1249 vop->pipe = drm_crtc_index(crtc);
1250 rockchip_register_crtc_funcs(drm_dev, &private_crtc_funcs, vop->pipe);
1251
1252 return 0;
1253
1254err_cleanup_crtc:
1255 drm_crtc_cleanup(crtc);
1256err_cleanup_planes:
1257 list_for_each_entry(plane, &drm_dev->mode_config.plane_list, head)
1258 drm_plane_cleanup(plane);
1259 return ret;
1260}
1261
1262static void vop_destroy_crtc(struct vop *vop)
1263{
1264 struct drm_crtc *crtc = &vop->crtc;
1265
1266 rockchip_unregister_crtc_funcs(vop->drm_dev, vop->pipe);
1267 of_node_put(crtc->port);
1268 drm_crtc_cleanup(crtc);
1269}
1270
1271static int vop_initial(struct vop *vop)
1272{
1273 const struct vop_data *vop_data = vop->data;
1274 const struct vop_reg_data *init_table = vop_data->init_table;
1275 struct reset_control *ahb_rst;
1276 int i, ret;
1277
1278 vop->hclk = devm_clk_get(vop->dev, "hclk_vop");
1279 if (IS_ERR(vop->hclk)) {
1280 dev_err(vop->dev, "failed to get hclk source\n");
1281 return PTR_ERR(vop->hclk);
1282 }
1283 vop->aclk = devm_clk_get(vop->dev, "aclk_vop");
1284 if (IS_ERR(vop->aclk)) {
1285 dev_err(vop->dev, "failed to get aclk source\n");
1286 return PTR_ERR(vop->aclk);
1287 }
1288 vop->dclk = devm_clk_get(vop->dev, "dclk_vop");
1289 if (IS_ERR(vop->dclk)) {
1290 dev_err(vop->dev, "failed to get dclk source\n");
1291 return PTR_ERR(vop->dclk);
1292 }
1293
1294 ret = clk_prepare(vop->hclk);
1295 if (ret < 0) {
1296 dev_err(vop->dev, "failed to prepare hclk\n");
1297 return ret;
1298 }
1299
1300 ret = clk_prepare(vop->dclk);
1301 if (ret < 0) {
1302 dev_err(vop->dev, "failed to prepare dclk\n");
1303 goto err_unprepare_hclk;
1304 }
1305
1306 ret = clk_prepare(vop->aclk);
1307 if (ret < 0) {
1308 dev_err(vop->dev, "failed to prepare aclk\n");
1309 goto err_unprepare_dclk;
1310 }
1311
1312 /*
1313 * enable hclk, so that we can config vop register.
1314 */
1315 ret = clk_enable(vop->hclk);
1316 if (ret < 0) {
1317 dev_err(vop->dev, "failed to prepare aclk\n");
1318 goto err_unprepare_aclk;
1319 }
1320 /*
1321 * do hclk_reset, reset all vop registers.
1322 */
1323 ahb_rst = devm_reset_control_get(vop->dev, "ahb");
1324 if (IS_ERR(ahb_rst)) {
1325 dev_err(vop->dev, "failed to get ahb reset\n");
1326 ret = PTR_ERR(ahb_rst);
1327 goto err_disable_hclk;
1328 }
1329 reset_control_assert(ahb_rst);
1330 usleep_range(10, 20);
1331 reset_control_deassert(ahb_rst);
1332
1333 memcpy(vop->regsbak, vop->regs, vop->len);
1334
1335 for (i = 0; i < vop_data->table_size; i++)
1336 vop_writel(vop, init_table[i].offset, init_table[i].value);
1337
1338 for (i = 0; i < vop_data->win_size; i++) {
1339 const struct vop_win_data *win = &vop_data->win[i];
1340
1341 VOP_WIN_SET(vop, win, enable, 0);
1342 }
1343
1344 vop_cfg_done(vop);
1345
1346 /*
1347 * do dclk_reset, let all config take affect.
1348 */
1349 vop->dclk_rst = devm_reset_control_get(vop->dev, "dclk");
1350 if (IS_ERR(vop->dclk_rst)) {
1351 dev_err(vop->dev, "failed to get dclk reset\n");
1352 ret = PTR_ERR(vop->dclk_rst);
1353 goto err_unprepare_aclk;
1354 }
1355 reset_control_assert(vop->dclk_rst);
1356 usleep_range(10, 20);
1357 reset_control_deassert(vop->dclk_rst);
1358
1359 clk_disable(vop->hclk);
1360
Mark Yao31e980c2015-01-22 14:37:56 +08001361 vop->is_enabled = false;
Mark Yao2048e322014-08-22 18:36:26 +08001362
1363 return 0;
1364
1365err_disable_hclk:
1366 clk_disable(vop->hclk);
1367err_unprepare_aclk:
1368 clk_unprepare(vop->aclk);
1369err_unprepare_dclk:
1370 clk_unprepare(vop->dclk);
1371err_unprepare_hclk:
1372 clk_unprepare(vop->hclk);
1373 return ret;
1374}
1375
1376/*
1377 * Initialize the vop->win array elements.
1378 */
1379static void vop_win_init(struct vop *vop)
1380{
1381 const struct vop_data *vop_data = vop->data;
1382 unsigned int i;
1383
1384 for (i = 0; i < vop_data->win_size; i++) {
1385 struct vop_win *vop_win = &vop->win[i];
1386 const struct vop_win_data *win_data = &vop_data->win[i];
1387
1388 vop_win->data = win_data;
1389 vop_win->vop = vop;
1390 INIT_LIST_HEAD(&vop_win->pending);
1391 }
1392}
1393
1394static int vop_bind(struct device *dev, struct device *master, void *data)
1395{
1396 struct platform_device *pdev = to_platform_device(dev);
1397 const struct of_device_id *of_id;
1398 const struct vop_data *vop_data;
1399 struct drm_device *drm_dev = data;
1400 struct vop *vop;
1401 struct resource *res;
1402 size_t alloc_size;
1403 int ret;
1404
1405 of_id = of_match_device(vop_driver_dt_match, dev);
1406 vop_data = of_id->data;
1407 if (!vop_data)
1408 return -ENODEV;
1409
1410 /* Allocate vop struct and its vop_win array */
1411 alloc_size = sizeof(*vop) + sizeof(*vop->win) * vop_data->win_size;
1412 vop = devm_kzalloc(dev, alloc_size, GFP_KERNEL);
1413 if (!vop)
1414 return -ENOMEM;
1415
1416 vop->dev = dev;
1417 vop->data = vop_data;
1418 vop->drm_dev = drm_dev;
1419 dev_set_drvdata(dev, vop);
1420
1421 vop_win_init(vop);
1422
1423 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1424 vop->len = resource_size(res);
1425 vop->regs = devm_ioremap_resource(dev, res);
1426 if (IS_ERR(vop->regs))
1427 return PTR_ERR(vop->regs);
1428
1429 vop->regsbak = devm_kzalloc(dev, vop->len, GFP_KERNEL);
1430 if (!vop->regsbak)
1431 return -ENOMEM;
1432
1433 ret = vop_initial(vop);
1434 if (ret < 0) {
1435 dev_err(&pdev->dev, "cannot initial vop dev - err %d\n", ret);
1436 return ret;
1437 }
1438
1439 vop->irq = platform_get_irq(pdev, 0);
1440 if (vop->irq < 0) {
1441 dev_err(dev, "cannot find irq for vop\n");
1442 return vop->irq;
1443 }
1444
1445 spin_lock_init(&vop->reg_lock);
1446 spin_lock_init(&vop->irq_lock);
1447
1448 mutex_init(&vop->vsync_mutex);
1449
1450 ret = devm_request_threaded_irq(dev, vop->irq, vop_isr, vop_isr_thread,
1451 IRQF_SHARED, dev_name(dev), vop);
1452 if (ret)
1453 return ret;
1454
1455 /* IRQ is initially disabled; it gets enabled in power_on */
1456 disable_irq(vop->irq);
1457
1458 ret = vop_create_crtc(vop);
1459 if (ret)
1460 return ret;
1461
1462 pm_runtime_enable(&pdev->dev);
1463 return 0;
1464}
1465
1466static void vop_unbind(struct device *dev, struct device *master, void *data)
1467{
1468 struct vop *vop = dev_get_drvdata(dev);
1469
1470 pm_runtime_disable(dev);
1471 vop_destroy_crtc(vop);
1472}
1473
1474static const struct component_ops vop_component_ops = {
1475 .bind = vop_bind,
1476 .unbind = vop_unbind,
1477};
1478
1479static int vop_probe(struct platform_device *pdev)
1480{
1481 struct device *dev = &pdev->dev;
1482
1483 if (!dev->of_node) {
1484 dev_err(dev, "can't find vop devices\n");
1485 return -ENODEV;
1486 }
1487
1488 return component_add(dev, &vop_component_ops);
1489}
1490
1491static int vop_remove(struct platform_device *pdev)
1492{
1493 component_del(&pdev->dev, &vop_component_ops);
1494
1495 return 0;
1496}
1497
1498struct platform_driver vop_platform_driver = {
1499 .probe = vop_probe,
1500 .remove = vop_remove,
1501 .driver = {
1502 .name = "rockchip-vop",
1503 .owner = THIS_MODULE,
1504 .of_match_table = of_match_ptr(vop_driver_dt_match),
1505 },
1506};
1507
1508module_platform_driver(vop_platform_driver);
1509
1510MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
1511MODULE_DESCRIPTION("ROCKCHIP VOP Driver");
1512MODULE_LICENSE("GPL v2");