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