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