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