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