1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) STMicroelectronics SA 2014 4 * Authors: Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics. 5 */ 6 7 #include <linux/errno.h> 8 #include <linux/interrupt.h> 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/of.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/slab.h> 14 15 #include <media/v4l2-event.h> 16 #include <media/v4l2-ioctl.h> 17 18 #include "bdisp.h" 19 20 #define BDISP_MAX_CTRL_NUM 10 21 22 #define BDISP_WORK_TIMEOUT ((100 * HZ) / 1000) 23 24 /* User configuration change */ 25 #define BDISP_PARAMS BIT(0) /* Config updated */ 26 #define BDISP_SRC_FMT BIT(1) /* Source set */ 27 #define BDISP_DST_FMT BIT(2) /* Destination set */ 28 #define BDISP_CTX_STOP_REQ BIT(3) /* Stop request */ 29 #define BDISP_CTX_ABORT BIT(4) /* Abort while device run */ 30 31 #define BDISP_MIN_W 1 32 #define BDISP_MAX_W 8191 33 #define BDISP_MIN_H 1 34 #define BDISP_MAX_H 8191 35 36 #define fh_to_ctx(__fh) container_of(__fh, struct bdisp_ctx, fh) 37 38 enum bdisp_dev_flags { 39 ST_M2M_OPEN, /* Driver opened */ 40 ST_M2M_RUNNING, /* HW device running */ 41 ST_M2M_SUSPENDED, /* Driver suspended */ 42 ST_M2M_SUSPENDING, /* Driver being suspended */ 43 }; 44 45 static const struct bdisp_fmt bdisp_formats[] = { 46 /* ARGB888. [31:0] A:R:G:B 8:8:8:8 little endian */ 47 { 48 .pixelformat = V4L2_PIX_FMT_ABGR32, /* is actually ARGB */ 49 .nb_planes = 1, 50 .bpp = 32, 51 .bpp_plane0 = 32, 52 .w_align = 1, 53 .h_align = 1 54 }, 55 /* XRGB888. [31:0] x:R:G:B 8:8:8:8 little endian */ 56 { 57 .pixelformat = V4L2_PIX_FMT_XBGR32, /* is actually xRGB */ 58 .nb_planes = 1, 59 .bpp = 32, 60 .bpp_plane0 = 32, 61 .w_align = 1, 62 .h_align = 1 63 }, 64 /* RGB565. [15:0] R:G:B 5:6:5 little endian */ 65 { 66 .pixelformat = V4L2_PIX_FMT_RGB565, 67 .nb_planes = 1, 68 .bpp = 16, 69 .bpp_plane0 = 16, 70 .w_align = 1, 71 .h_align = 1 72 }, 73 /* NV12. YUV420SP - 1 plane for Y + 1 plane for (CbCr) */ 74 { 75 .pixelformat = V4L2_PIX_FMT_NV12, 76 .nb_planes = 2, 77 .bpp = 12, 78 .bpp_plane0 = 8, 79 .w_align = 2, 80 .h_align = 2 81 }, 82 /* RGB888. [23:0] B:G:R 8:8:8 little endian */ 83 { 84 .pixelformat = V4L2_PIX_FMT_RGB24, 85 .nb_planes = 1, 86 .bpp = 24, 87 .bpp_plane0 = 24, 88 .w_align = 1, 89 .h_align = 1 90 }, 91 /* YU12. YUV420P - 1 plane for Y + 1 plane for Cb + 1 plane for Cr 92 * To keep as the LAST element of this table (no support on capture) 93 */ 94 { 95 .pixelformat = V4L2_PIX_FMT_YUV420, 96 .nb_planes = 3, 97 .bpp = 12, 98 .bpp_plane0 = 8, 99 .w_align = 2, 100 .h_align = 2 101 } 102 }; 103 104 /* Default format : HD ARGB32*/ 105 #define BDISP_DEF_WIDTH 1920 106 #define BDISP_DEF_HEIGHT 1080 107 108 static const struct bdisp_frame bdisp_dflt_fmt = { 109 .width = BDISP_DEF_WIDTH, 110 .height = BDISP_DEF_HEIGHT, 111 .fmt = &bdisp_formats[0], 112 .field = V4L2_FIELD_NONE, 113 .bytesperline = BDISP_DEF_WIDTH * 4, 114 .sizeimage = BDISP_DEF_WIDTH * BDISP_DEF_HEIGHT * 4, 115 .colorspace = V4L2_COLORSPACE_REC709, 116 .crop = {0, 0, BDISP_DEF_WIDTH, BDISP_DEF_HEIGHT}, 117 .paddr = {0, 0, 0, 0} 118 }; 119 120 static inline void bdisp_ctx_state_lock_set(u32 state, struct bdisp_ctx *ctx) 121 { 122 unsigned long flags; 123 124 spin_lock_irqsave(&ctx->bdisp_dev->slock, flags); 125 ctx->state |= state; 126 spin_unlock_irqrestore(&ctx->bdisp_dev->slock, flags); 127 } 128 129 static inline void bdisp_ctx_state_lock_clear(u32 state, struct bdisp_ctx *ctx) 130 { 131 unsigned long flags; 132 133 spin_lock_irqsave(&ctx->bdisp_dev->slock, flags); 134 ctx->state &= ~state; 135 spin_unlock_irqrestore(&ctx->bdisp_dev->slock, flags); 136 } 137 138 static inline bool bdisp_ctx_state_is_set(u32 mask, struct bdisp_ctx *ctx) 139 { 140 unsigned long flags; 141 bool ret; 142 143 spin_lock_irqsave(&ctx->bdisp_dev->slock, flags); 144 ret = (ctx->state & mask) == mask; 145 spin_unlock_irqrestore(&ctx->bdisp_dev->slock, flags); 146 147 return ret; 148 } 149 150 static const struct bdisp_fmt *bdisp_find_fmt(u32 pixelformat) 151 { 152 const struct bdisp_fmt *fmt; 153 unsigned int i; 154 155 for (i = 0; i < ARRAY_SIZE(bdisp_formats); i++) { 156 fmt = &bdisp_formats[i]; 157 if (fmt->pixelformat == pixelformat) 158 return fmt; 159 } 160 161 return NULL; 162 } 163 164 static struct bdisp_frame *ctx_get_frame(struct bdisp_ctx *ctx, 165 enum v4l2_buf_type type) 166 { 167 switch (type) { 168 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 169 return &ctx->src; 170 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 171 return &ctx->dst; 172 default: 173 dev_err(ctx->bdisp_dev->dev, 174 "Wrong buffer/video queue type (%d)\n", type); 175 break; 176 } 177 178 return ERR_PTR(-EINVAL); 179 } 180 181 static void bdisp_job_finish(struct bdisp_ctx *ctx, int vb_state) 182 { 183 struct vb2_v4l2_buffer *src_vb, *dst_vb; 184 185 if (WARN(!ctx || !ctx->fh.m2m_ctx, "Null hardware context\n")) 186 return; 187 188 dev_dbg(ctx->bdisp_dev->dev, "%s\n", __func__); 189 190 src_vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 191 dst_vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 192 193 if (src_vb && dst_vb) { 194 dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp; 195 dst_vb->timecode = src_vb->timecode; 196 dst_vb->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK; 197 dst_vb->flags |= src_vb->flags & 198 V4L2_BUF_FLAG_TSTAMP_SRC_MASK; 199 200 v4l2_m2m_buf_done(src_vb, vb_state); 201 v4l2_m2m_buf_done(dst_vb, vb_state); 202 203 v4l2_m2m_job_finish(ctx->bdisp_dev->m2m.m2m_dev, 204 ctx->fh.m2m_ctx); 205 } 206 } 207 208 static int bdisp_ctx_stop_req(struct bdisp_ctx *ctx) 209 { 210 struct bdisp_ctx *curr_ctx; 211 struct bdisp_dev *bdisp = ctx->bdisp_dev; 212 int ret; 213 214 dev_dbg(ctx->bdisp_dev->dev, "%s\n", __func__); 215 216 cancel_delayed_work(&bdisp->timeout_work); 217 218 curr_ctx = v4l2_m2m_get_curr_priv(bdisp->m2m.m2m_dev); 219 if (!test_bit(ST_M2M_RUNNING, &bdisp->state) || (curr_ctx != ctx)) 220 return 0; 221 222 bdisp_ctx_state_lock_set(BDISP_CTX_STOP_REQ, ctx); 223 224 ret = wait_event_timeout(bdisp->irq_queue, 225 !bdisp_ctx_state_is_set(BDISP_CTX_STOP_REQ, ctx), 226 BDISP_WORK_TIMEOUT); 227 228 if (!ret) { 229 dev_err(ctx->bdisp_dev->dev, "%s IRQ timeout\n", __func__); 230 return -ETIMEDOUT; 231 } 232 233 return 0; 234 } 235 236 static void __bdisp_job_abort(struct bdisp_ctx *ctx) 237 { 238 int ret; 239 240 ret = bdisp_ctx_stop_req(ctx); 241 if ((ret == -ETIMEDOUT) || (ctx->state & BDISP_CTX_ABORT)) { 242 bdisp_ctx_state_lock_clear(BDISP_CTX_STOP_REQ | BDISP_CTX_ABORT, 243 ctx); 244 bdisp_job_finish(ctx, VB2_BUF_STATE_ERROR); 245 } 246 } 247 248 static void bdisp_job_abort(void *priv) 249 { 250 __bdisp_job_abort((struct bdisp_ctx *)priv); 251 } 252 253 static int bdisp_get_addr(struct bdisp_ctx *ctx, struct vb2_buffer *vb, 254 struct bdisp_frame *frame, dma_addr_t *paddr) 255 { 256 if (!vb || !frame) 257 return -EINVAL; 258 259 paddr[0] = vb2_dma_contig_plane_dma_addr(vb, 0); 260 261 if (frame->fmt->nb_planes > 1) 262 /* UV (NV12) or U (420P) */ 263 paddr[1] = (dma_addr_t)(paddr[0] + 264 frame->bytesperline * frame->height); 265 266 if (frame->fmt->nb_planes > 2) 267 /* V (420P) */ 268 paddr[2] = (dma_addr_t)(paddr[1] + 269 (frame->bytesperline * frame->height) / 4); 270 271 if (frame->fmt->nb_planes > 3) 272 dev_dbg(ctx->bdisp_dev->dev, "ignoring some planes\n"); 273 274 dev_dbg(ctx->bdisp_dev->dev, 275 "%s plane[0]=%pad plane[1]=%pad plane[2]=%pad\n", 276 __func__, &paddr[0], &paddr[1], &paddr[2]); 277 278 return 0; 279 } 280 281 static int bdisp_get_bufs(struct bdisp_ctx *ctx) 282 { 283 struct bdisp_frame *src, *dst; 284 struct vb2_v4l2_buffer *src_vb, *dst_vb; 285 int ret; 286 287 src = &ctx->src; 288 dst = &ctx->dst; 289 290 src_vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 291 ret = bdisp_get_addr(ctx, &src_vb->vb2_buf, src, src->paddr); 292 if (ret) 293 return ret; 294 295 dst_vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 296 ret = bdisp_get_addr(ctx, &dst_vb->vb2_buf, dst, dst->paddr); 297 if (ret) 298 return ret; 299 300 dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp; 301 302 return 0; 303 } 304 305 static void bdisp_device_run(void *priv) 306 { 307 struct bdisp_ctx *ctx = priv; 308 struct bdisp_dev *bdisp; 309 unsigned long flags; 310 int err = 0; 311 312 if (WARN(!ctx, "Null hardware context\n")) 313 return; 314 315 bdisp = ctx->bdisp_dev; 316 dev_dbg(bdisp->dev, "%s\n", __func__); 317 spin_lock_irqsave(&bdisp->slock, flags); 318 319 if (bdisp->m2m.ctx != ctx) { 320 dev_dbg(bdisp->dev, "ctx updated: %p -> %p\n", 321 bdisp->m2m.ctx, ctx); 322 ctx->state |= BDISP_PARAMS; 323 bdisp->m2m.ctx = ctx; 324 } 325 326 if (ctx->state & BDISP_CTX_STOP_REQ) { 327 ctx->state &= ~BDISP_CTX_STOP_REQ; 328 ctx->state |= BDISP_CTX_ABORT; 329 wake_up(&bdisp->irq_queue); 330 goto out; 331 } 332 333 err = bdisp_get_bufs(ctx); 334 if (err) { 335 dev_err(bdisp->dev, "cannot get address\n"); 336 goto out; 337 } 338 339 bdisp_dbg_perf_begin(bdisp); 340 341 err = bdisp_hw_reset(bdisp); 342 if (err) { 343 dev_err(bdisp->dev, "could not get HW ready\n"); 344 goto out; 345 } 346 347 err = bdisp_hw_update(ctx); 348 if (err) { 349 dev_err(bdisp->dev, "could not send HW request\n"); 350 goto out; 351 } 352 353 queue_delayed_work(bdisp->work_queue, &bdisp->timeout_work, 354 BDISP_WORK_TIMEOUT); 355 set_bit(ST_M2M_RUNNING, &bdisp->state); 356 out: 357 ctx->state &= ~BDISP_PARAMS; 358 spin_unlock_irqrestore(&bdisp->slock, flags); 359 if (err) 360 bdisp_job_finish(ctx, VB2_BUF_STATE_ERROR); 361 } 362 363 static const struct v4l2_m2m_ops bdisp_m2m_ops = { 364 .device_run = bdisp_device_run, 365 .job_abort = bdisp_job_abort, 366 }; 367 368 static int __bdisp_s_ctrl(struct bdisp_ctx *ctx, struct v4l2_ctrl *ctrl) 369 { 370 if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE) 371 return 0; 372 373 switch (ctrl->id) { 374 case V4L2_CID_HFLIP: 375 ctx->hflip = ctrl->val; 376 break; 377 case V4L2_CID_VFLIP: 378 ctx->vflip = ctrl->val; 379 break; 380 default: 381 dev_err(ctx->bdisp_dev->dev, "unknown control %d\n", ctrl->id); 382 return -EINVAL; 383 } 384 385 ctx->state |= BDISP_PARAMS; 386 387 return 0; 388 } 389 390 static int bdisp_s_ctrl(struct v4l2_ctrl *ctrl) 391 { 392 struct bdisp_ctx *ctx = container_of(ctrl->handler, struct bdisp_ctx, 393 ctrl_handler); 394 unsigned long flags; 395 int ret; 396 397 spin_lock_irqsave(&ctx->bdisp_dev->slock, flags); 398 ret = __bdisp_s_ctrl(ctx, ctrl); 399 spin_unlock_irqrestore(&ctx->bdisp_dev->slock, flags); 400 401 return ret; 402 } 403 404 static const struct v4l2_ctrl_ops bdisp_c_ops = { 405 .s_ctrl = bdisp_s_ctrl, 406 }; 407 408 static int bdisp_ctrls_create(struct bdisp_ctx *ctx) 409 { 410 if (ctx->ctrls_rdy) 411 return 0; 412 413 v4l2_ctrl_handler_init(&ctx->ctrl_handler, BDISP_MAX_CTRL_NUM); 414 415 ctx->bdisp_ctrls.hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, 416 &bdisp_c_ops, V4L2_CID_HFLIP, 0, 1, 1, 0); 417 ctx->bdisp_ctrls.vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, 418 &bdisp_c_ops, V4L2_CID_VFLIP, 0, 1, 1, 0); 419 420 if (ctx->ctrl_handler.error) { 421 int err = ctx->ctrl_handler.error; 422 423 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 424 return err; 425 } 426 427 ctx->ctrls_rdy = true; 428 429 return 0; 430 } 431 432 static void bdisp_ctrls_delete(struct bdisp_ctx *ctx) 433 { 434 if (ctx->ctrls_rdy) { 435 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 436 ctx->ctrls_rdy = false; 437 } 438 } 439 440 static int bdisp_queue_setup(struct vb2_queue *vq, 441 unsigned int *nb_buf, unsigned int *nb_planes, 442 unsigned int sizes[], struct device *alloc_devs[]) 443 { 444 struct bdisp_ctx *ctx = vb2_get_drv_priv(vq); 445 struct bdisp_frame *frame = ctx_get_frame(ctx, vq->type); 446 447 if (IS_ERR(frame)) { 448 dev_err(ctx->bdisp_dev->dev, "Invalid frame (%p)\n", frame); 449 return PTR_ERR(frame); 450 } 451 452 if (!frame->fmt) { 453 dev_err(ctx->bdisp_dev->dev, "Invalid format\n"); 454 return -EINVAL; 455 } 456 457 if (*nb_planes) 458 return sizes[0] < frame->sizeimage ? -EINVAL : 0; 459 460 *nb_planes = 1; 461 sizes[0] = frame->sizeimage; 462 463 return 0; 464 } 465 466 static int bdisp_buf_prepare(struct vb2_buffer *vb) 467 { 468 struct bdisp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 469 struct bdisp_frame *frame = ctx_get_frame(ctx, vb->vb2_queue->type); 470 471 if (IS_ERR(frame)) { 472 dev_err(ctx->bdisp_dev->dev, "Invalid frame (%p)\n", frame); 473 return PTR_ERR(frame); 474 } 475 476 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 477 vb2_set_plane_payload(vb, 0, frame->sizeimage); 478 479 return 0; 480 } 481 482 static void bdisp_buf_queue(struct vb2_buffer *vb) 483 { 484 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 485 struct bdisp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 486 487 /* return to V4L2 any 0-size buffer so it can be dequeued by user */ 488 if (!vb2_get_plane_payload(vb, 0)) { 489 dev_dbg(ctx->bdisp_dev->dev, "0 data buffer, skip it\n"); 490 vb2_buffer_done(vb, VB2_BUF_STATE_DONE); 491 return; 492 } 493 494 if (ctx->fh.m2m_ctx) 495 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 496 } 497 498 static int bdisp_start_streaming(struct vb2_queue *q, unsigned int count) 499 { 500 struct bdisp_ctx *ctx = q->drv_priv; 501 struct vb2_v4l2_buffer *buf; 502 int ret = pm_runtime_resume_and_get(ctx->bdisp_dev->dev); 503 504 if (ret < 0) { 505 dev_err(ctx->bdisp_dev->dev, "failed to set runtime PM\n"); 506 507 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 508 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) 509 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED); 510 } else { 511 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx))) 512 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED); 513 } 514 515 return ret; 516 } 517 518 return 0; 519 } 520 521 static void bdisp_stop_streaming(struct vb2_queue *q) 522 { 523 struct bdisp_ctx *ctx = q->drv_priv; 524 525 __bdisp_job_abort(ctx); 526 527 pm_runtime_put(ctx->bdisp_dev->dev); 528 } 529 530 static const struct vb2_ops bdisp_qops = { 531 .queue_setup = bdisp_queue_setup, 532 .buf_prepare = bdisp_buf_prepare, 533 .buf_queue = bdisp_buf_queue, 534 .stop_streaming = bdisp_stop_streaming, 535 .start_streaming = bdisp_start_streaming, 536 }; 537 538 static int queue_init(void *priv, 539 struct vb2_queue *src_vq, struct vb2_queue *dst_vq) 540 { 541 struct bdisp_ctx *ctx = priv; 542 int ret; 543 544 memset(src_vq, 0, sizeof(*src_vq)); 545 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 546 src_vq->io_modes = VB2_MMAP | VB2_DMABUF; 547 src_vq->drv_priv = ctx; 548 src_vq->ops = &bdisp_qops; 549 src_vq->mem_ops = &vb2_dma_contig_memops; 550 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 551 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 552 src_vq->lock = &ctx->bdisp_dev->lock; 553 src_vq->dev = ctx->bdisp_dev->v4l2_dev.dev; 554 555 ret = vb2_queue_init(src_vq); 556 if (ret) 557 return ret; 558 559 memset(dst_vq, 0, sizeof(*dst_vq)); 560 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 561 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF; 562 dst_vq->drv_priv = ctx; 563 dst_vq->ops = &bdisp_qops; 564 dst_vq->mem_ops = &vb2_dma_contig_memops; 565 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 566 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 567 dst_vq->lock = &ctx->bdisp_dev->lock; 568 dst_vq->dev = ctx->bdisp_dev->v4l2_dev.dev; 569 570 return vb2_queue_init(dst_vq); 571 } 572 573 static int bdisp_open(struct file *file) 574 { 575 struct bdisp_dev *bdisp = video_drvdata(file); 576 struct bdisp_ctx *ctx = NULL; 577 int ret; 578 579 if (mutex_lock_interruptible(&bdisp->lock)) 580 return -ERESTARTSYS; 581 582 /* Allocate memory for both context and node */ 583 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 584 if (!ctx) { 585 ret = -ENOMEM; 586 goto unlock; 587 } 588 ctx->bdisp_dev = bdisp; 589 590 if (bdisp_hw_alloc_nodes(ctx)) { 591 dev_err(bdisp->dev, "no memory for nodes\n"); 592 ret = -ENOMEM; 593 goto mem_ctx; 594 } 595 596 v4l2_fh_init(&ctx->fh, bdisp->m2m.vdev); 597 598 ret = bdisp_ctrls_create(ctx); 599 if (ret) { 600 dev_err(bdisp->dev, "Failed to create control\n"); 601 goto error_fh; 602 } 603 604 /* Use separate control handler per file handle */ 605 ctx->fh.ctrl_handler = &ctx->ctrl_handler; 606 file->private_data = &ctx->fh; 607 v4l2_fh_add(&ctx->fh); 608 609 /* Default format */ 610 ctx->src = bdisp_dflt_fmt; 611 ctx->dst = bdisp_dflt_fmt; 612 613 /* Setup the device context for mem2mem mode. */ 614 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(bdisp->m2m.m2m_dev, ctx, 615 queue_init); 616 if (IS_ERR(ctx->fh.m2m_ctx)) { 617 dev_err(bdisp->dev, "Failed to initialize m2m context\n"); 618 ret = PTR_ERR(ctx->fh.m2m_ctx); 619 goto error_ctrls; 620 } 621 622 bdisp->m2m.refcnt++; 623 set_bit(ST_M2M_OPEN, &bdisp->state); 624 625 dev_dbg(bdisp->dev, "driver opened, ctx = 0x%p\n", ctx); 626 627 mutex_unlock(&bdisp->lock); 628 629 return 0; 630 631 error_ctrls: 632 bdisp_ctrls_delete(ctx); 633 v4l2_fh_del(&ctx->fh); 634 error_fh: 635 v4l2_fh_exit(&ctx->fh); 636 bdisp_hw_free_nodes(ctx); 637 mem_ctx: 638 kfree(ctx); 639 unlock: 640 mutex_unlock(&bdisp->lock); 641 642 return ret; 643 } 644 645 static int bdisp_release(struct file *file) 646 { 647 struct bdisp_ctx *ctx = fh_to_ctx(file->private_data); 648 struct bdisp_dev *bdisp = ctx->bdisp_dev; 649 650 dev_dbg(bdisp->dev, "%s\n", __func__); 651 652 mutex_lock(&bdisp->lock); 653 654 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 655 656 bdisp_ctrls_delete(ctx); 657 658 v4l2_fh_del(&ctx->fh); 659 v4l2_fh_exit(&ctx->fh); 660 661 if (--bdisp->m2m.refcnt <= 0) 662 clear_bit(ST_M2M_OPEN, &bdisp->state); 663 664 bdisp_hw_free_nodes(ctx); 665 666 kfree(ctx); 667 668 mutex_unlock(&bdisp->lock); 669 670 return 0; 671 } 672 673 static const struct v4l2_file_operations bdisp_fops = { 674 .owner = THIS_MODULE, 675 .open = bdisp_open, 676 .release = bdisp_release, 677 .poll = v4l2_m2m_fop_poll, 678 .unlocked_ioctl = video_ioctl2, 679 .mmap = v4l2_m2m_fop_mmap, 680 }; 681 682 static int bdisp_querycap(struct file *file, void *fh, 683 struct v4l2_capability *cap) 684 { 685 struct bdisp_ctx *ctx = fh_to_ctx(fh); 686 struct bdisp_dev *bdisp = ctx->bdisp_dev; 687 688 strscpy(cap->driver, bdisp->pdev->name, sizeof(cap->driver)); 689 strscpy(cap->card, bdisp->pdev->name, sizeof(cap->card)); 690 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s%d", 691 BDISP_NAME, bdisp->id); 692 return 0; 693 } 694 695 static int bdisp_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f) 696 { 697 struct bdisp_ctx *ctx = fh_to_ctx(fh); 698 const struct bdisp_fmt *fmt; 699 700 if (f->index >= ARRAY_SIZE(bdisp_formats)) 701 return -EINVAL; 702 703 fmt = &bdisp_formats[f->index]; 704 705 if ((fmt->pixelformat == V4L2_PIX_FMT_YUV420) && 706 (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)) { 707 dev_dbg(ctx->bdisp_dev->dev, "No YU12 on capture\n"); 708 return -EINVAL; 709 } 710 f->pixelformat = fmt->pixelformat; 711 712 return 0; 713 } 714 715 static int bdisp_g_fmt(struct file *file, void *fh, struct v4l2_format *f) 716 { 717 struct bdisp_ctx *ctx = fh_to_ctx(fh); 718 struct v4l2_pix_format *pix; 719 struct bdisp_frame *frame = ctx_get_frame(ctx, f->type); 720 721 if (IS_ERR(frame)) { 722 dev_err(ctx->bdisp_dev->dev, "Invalid frame (%p)\n", frame); 723 return PTR_ERR(frame); 724 } 725 726 pix = &f->fmt.pix; 727 pix->width = frame->width; 728 pix->height = frame->height; 729 pix->pixelformat = frame->fmt->pixelformat; 730 pix->field = frame->field; 731 pix->bytesperline = frame->bytesperline; 732 pix->sizeimage = frame->sizeimage; 733 pix->colorspace = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) ? 734 frame->colorspace : bdisp_dflt_fmt.colorspace; 735 736 return 0; 737 } 738 739 static int bdisp_try_fmt(struct file *file, void *fh, struct v4l2_format *f) 740 { 741 struct bdisp_ctx *ctx = fh_to_ctx(fh); 742 struct v4l2_pix_format *pix = &f->fmt.pix; 743 const struct bdisp_fmt *format; 744 u32 in_w, in_h; 745 746 format = bdisp_find_fmt(pix->pixelformat); 747 if (!format) { 748 dev_dbg(ctx->bdisp_dev->dev, "Unknown format 0x%x\n", 749 pix->pixelformat); 750 return -EINVAL; 751 } 752 753 /* YUV420P only supported for VIDEO_OUTPUT */ 754 if ((format->pixelformat == V4L2_PIX_FMT_YUV420) && 755 (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)) { 756 dev_dbg(ctx->bdisp_dev->dev, "No YU12 on capture\n"); 757 return -EINVAL; 758 } 759 760 /* Field (interlaced only supported on OUTPUT) */ 761 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) || 762 (pix->field != V4L2_FIELD_INTERLACED)) 763 pix->field = V4L2_FIELD_NONE; 764 765 /* Adjust width & height */ 766 in_w = pix->width; 767 in_h = pix->height; 768 v4l_bound_align_image(&pix->width, 769 BDISP_MIN_W, BDISP_MAX_W, 770 ffs(format->w_align) - 1, 771 &pix->height, 772 BDISP_MIN_H, BDISP_MAX_H, 773 ffs(format->h_align) - 1, 774 0); 775 if ((pix->width != in_w) || (pix->height != in_h)) 776 dev_dbg(ctx->bdisp_dev->dev, 777 "%s size updated: %dx%d -> %dx%d\n", __func__, 778 in_w, in_h, pix->width, pix->height); 779 780 pix->bytesperline = (pix->width * format->bpp_plane0) / 8; 781 pix->sizeimage = (pix->width * pix->height * format->bpp) / 8; 782 783 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 784 pix->colorspace = bdisp_dflt_fmt.colorspace; 785 786 return 0; 787 } 788 789 static int bdisp_s_fmt(struct file *file, void *fh, struct v4l2_format *f) 790 { 791 struct bdisp_ctx *ctx = fh_to_ctx(fh); 792 struct vb2_queue *vq; 793 struct bdisp_frame *frame; 794 struct v4l2_pix_format *pix; 795 int ret; 796 u32 state; 797 798 ret = bdisp_try_fmt(file, fh, f); 799 if (ret) { 800 dev_err(ctx->bdisp_dev->dev, "Cannot set format\n"); 801 return ret; 802 } 803 804 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 805 if (vb2_is_streaming(vq)) { 806 dev_err(ctx->bdisp_dev->dev, "queue (%d) busy\n", f->type); 807 return -EBUSY; 808 } 809 810 frame = (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) ? 811 &ctx->src : &ctx->dst; 812 pix = &f->fmt.pix; 813 frame->fmt = bdisp_find_fmt(pix->pixelformat); 814 if (!frame->fmt) { 815 dev_err(ctx->bdisp_dev->dev, "Unknown format 0x%x\n", 816 pix->pixelformat); 817 return -EINVAL; 818 } 819 820 frame->width = pix->width; 821 frame->height = pix->height; 822 frame->bytesperline = pix->bytesperline; 823 frame->sizeimage = pix->sizeimage; 824 frame->field = pix->field; 825 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) 826 frame->colorspace = pix->colorspace; 827 828 frame->crop.width = frame->width; 829 frame->crop.height = frame->height; 830 frame->crop.left = 0; 831 frame->crop.top = 0; 832 833 state = BDISP_PARAMS; 834 state |= (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ? 835 BDISP_DST_FMT : BDISP_SRC_FMT; 836 bdisp_ctx_state_lock_set(state, ctx); 837 838 return 0; 839 } 840 841 static int bdisp_g_selection(struct file *file, void *fh, 842 struct v4l2_selection *s) 843 { 844 struct bdisp_frame *frame; 845 struct bdisp_ctx *ctx = fh_to_ctx(fh); 846 847 frame = ctx_get_frame(ctx, s->type); 848 if (IS_ERR(frame)) { 849 dev_err(ctx->bdisp_dev->dev, "Invalid frame (%p)\n", frame); 850 return PTR_ERR(frame); 851 } 852 853 switch (s->type) { 854 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 855 switch (s->target) { 856 case V4L2_SEL_TGT_CROP: 857 /* cropped frame */ 858 s->r = frame->crop; 859 break; 860 case V4L2_SEL_TGT_CROP_DEFAULT: 861 case V4L2_SEL_TGT_CROP_BOUNDS: 862 /* complete frame */ 863 s->r.left = 0; 864 s->r.top = 0; 865 s->r.width = frame->width; 866 s->r.height = frame->height; 867 break; 868 default: 869 dev_err(ctx->bdisp_dev->dev, "Invalid target\n"); 870 return -EINVAL; 871 } 872 break; 873 874 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 875 switch (s->target) { 876 case V4L2_SEL_TGT_COMPOSE: 877 case V4L2_SEL_TGT_COMPOSE_PADDED: 878 /* composed (cropped) frame */ 879 s->r = frame->crop; 880 break; 881 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 882 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 883 /* complete frame */ 884 s->r.left = 0; 885 s->r.top = 0; 886 s->r.width = frame->width; 887 s->r.height = frame->height; 888 break; 889 default: 890 dev_err(ctx->bdisp_dev->dev, "Invalid target\n"); 891 return -EINVAL; 892 } 893 break; 894 895 default: 896 dev_err(ctx->bdisp_dev->dev, "Invalid type\n"); 897 return -EINVAL; 898 } 899 900 return 0; 901 } 902 903 static int is_rect_enclosed(struct v4l2_rect *a, struct v4l2_rect *b) 904 { 905 /* Return 1 if a is enclosed in b, or 0 otherwise. */ 906 907 if (a->left < b->left || a->top < b->top) 908 return 0; 909 910 if (a->left + a->width > b->left + b->width) 911 return 0; 912 913 if (a->top + a->height > b->top + b->height) 914 return 0; 915 916 return 1; 917 } 918 919 static int bdisp_s_selection(struct file *file, void *fh, 920 struct v4l2_selection *s) 921 { 922 struct bdisp_frame *frame; 923 struct bdisp_ctx *ctx = fh_to_ctx(fh); 924 struct v4l2_rect *in, out; 925 bool valid = false; 926 927 if ((s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) && 928 (s->target == V4L2_SEL_TGT_CROP)) 929 valid = true; 930 931 if ((s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) && 932 (s->target == V4L2_SEL_TGT_COMPOSE)) 933 valid = true; 934 935 if (!valid) { 936 dev_err(ctx->bdisp_dev->dev, "Invalid type / target\n"); 937 return -EINVAL; 938 } 939 940 frame = ctx_get_frame(ctx, s->type); 941 if (IS_ERR(frame)) { 942 dev_err(ctx->bdisp_dev->dev, "Invalid frame (%p)\n", frame); 943 return PTR_ERR(frame); 944 } 945 946 in = &s->r; 947 out = *in; 948 949 /* Align and check origin */ 950 out.left = ALIGN(in->left, frame->fmt->w_align); 951 out.top = ALIGN(in->top, frame->fmt->h_align); 952 953 if ((out.left < 0) || (out.left >= frame->width) || 954 (out.top < 0) || (out.top >= frame->height)) { 955 dev_err(ctx->bdisp_dev->dev, 956 "Invalid crop: %dx%d@(%d,%d) vs frame: %dx%d\n", 957 out.width, out.height, out.left, out.top, 958 frame->width, frame->height); 959 return -EINVAL; 960 } 961 962 /* Align and check size */ 963 out.width = ALIGN(in->width, frame->fmt->w_align); 964 out.height = ALIGN(in->height, frame->fmt->w_align); 965 966 if (((out.left + out.width) > frame->width) || 967 ((out.top + out.height) > frame->height)) { 968 dev_err(ctx->bdisp_dev->dev, 969 "Invalid crop: %dx%d@(%d,%d) vs frame: %dx%d\n", 970 out.width, out.height, out.left, out.top, 971 frame->width, frame->height); 972 return -EINVAL; 973 } 974 975 /* Checks adjust constraints flags */ 976 if (s->flags & V4L2_SEL_FLAG_LE && !is_rect_enclosed(&out, in)) 977 return -ERANGE; 978 979 if (s->flags & V4L2_SEL_FLAG_GE && !is_rect_enclosed(in, &out)) 980 return -ERANGE; 981 982 if ((out.left != in->left) || (out.top != in->top) || 983 (out.width != in->width) || (out.height != in->height)) { 984 dev_dbg(ctx->bdisp_dev->dev, 985 "%s crop updated: %dx%d@(%d,%d) -> %dx%d@(%d,%d)\n", 986 __func__, in->width, in->height, in->left, in->top, 987 out.width, out.height, out.left, out.top); 988 *in = out; 989 } 990 991 frame->crop = out; 992 993 bdisp_ctx_state_lock_set(BDISP_PARAMS, ctx); 994 995 return 0; 996 } 997 998 static int bdisp_streamon(struct file *file, void *fh, enum v4l2_buf_type type) 999 { 1000 struct bdisp_ctx *ctx = fh_to_ctx(fh); 1001 1002 if ((type == V4L2_BUF_TYPE_VIDEO_OUTPUT) && 1003 !bdisp_ctx_state_is_set(BDISP_SRC_FMT, ctx)) { 1004 dev_err(ctx->bdisp_dev->dev, "src not defined\n"); 1005 return -EINVAL; 1006 } 1007 1008 if ((type == V4L2_BUF_TYPE_VIDEO_CAPTURE) && 1009 !bdisp_ctx_state_is_set(BDISP_DST_FMT, ctx)) { 1010 dev_err(ctx->bdisp_dev->dev, "dst not defined\n"); 1011 return -EINVAL; 1012 } 1013 1014 return v4l2_m2m_streamon(file, ctx->fh.m2m_ctx, type); 1015 } 1016 1017 static const struct v4l2_ioctl_ops bdisp_ioctl_ops = { 1018 .vidioc_querycap = bdisp_querycap, 1019 .vidioc_enum_fmt_vid_cap = bdisp_enum_fmt, 1020 .vidioc_enum_fmt_vid_out = bdisp_enum_fmt, 1021 .vidioc_g_fmt_vid_cap = bdisp_g_fmt, 1022 .vidioc_g_fmt_vid_out = bdisp_g_fmt, 1023 .vidioc_try_fmt_vid_cap = bdisp_try_fmt, 1024 .vidioc_try_fmt_vid_out = bdisp_try_fmt, 1025 .vidioc_s_fmt_vid_cap = bdisp_s_fmt, 1026 .vidioc_s_fmt_vid_out = bdisp_s_fmt, 1027 .vidioc_g_selection = bdisp_g_selection, 1028 .vidioc_s_selection = bdisp_s_selection, 1029 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 1030 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 1031 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 1032 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 1033 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 1034 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 1035 .vidioc_streamon = bdisp_streamon, 1036 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 1037 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1038 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1039 }; 1040 1041 static int bdisp_register_device(struct bdisp_dev *bdisp) 1042 { 1043 int ret; 1044 1045 if (!bdisp) 1046 return -ENODEV; 1047 1048 bdisp->vdev.fops = &bdisp_fops; 1049 bdisp->vdev.ioctl_ops = &bdisp_ioctl_ops; 1050 bdisp->vdev.release = video_device_release_empty; 1051 bdisp->vdev.lock = &bdisp->lock; 1052 bdisp->vdev.vfl_dir = VFL_DIR_M2M; 1053 bdisp->vdev.v4l2_dev = &bdisp->v4l2_dev; 1054 bdisp->vdev.device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M; 1055 snprintf(bdisp->vdev.name, sizeof(bdisp->vdev.name), "%s.%d", 1056 BDISP_NAME, bdisp->id); 1057 1058 video_set_drvdata(&bdisp->vdev, bdisp); 1059 1060 bdisp->m2m.vdev = &bdisp->vdev; 1061 bdisp->m2m.m2m_dev = v4l2_m2m_init(&bdisp_m2m_ops); 1062 if (IS_ERR(bdisp->m2m.m2m_dev)) { 1063 dev_err(bdisp->dev, "failed to initialize v4l2-m2m device\n"); 1064 return PTR_ERR(bdisp->m2m.m2m_dev); 1065 } 1066 1067 ret = video_register_device(&bdisp->vdev, VFL_TYPE_VIDEO, -1); 1068 if (ret) { 1069 dev_err(bdisp->dev, 1070 "%s(): failed to register video device\n", __func__); 1071 v4l2_m2m_release(bdisp->m2m.m2m_dev); 1072 return ret; 1073 } 1074 1075 return 0; 1076 } 1077 1078 static void bdisp_unregister_device(struct bdisp_dev *bdisp) 1079 { 1080 if (!bdisp) 1081 return; 1082 1083 if (bdisp->m2m.m2m_dev) 1084 v4l2_m2m_release(bdisp->m2m.m2m_dev); 1085 1086 video_unregister_device(bdisp->m2m.vdev); 1087 } 1088 1089 static irqreturn_t bdisp_irq_thread(int irq, void *priv) 1090 { 1091 struct bdisp_dev *bdisp = priv; 1092 struct bdisp_ctx *ctx; 1093 1094 spin_lock(&bdisp->slock); 1095 1096 bdisp_dbg_perf_end(bdisp); 1097 1098 cancel_delayed_work(&bdisp->timeout_work); 1099 1100 if (!test_and_clear_bit(ST_M2M_RUNNING, &bdisp->state)) 1101 goto isr_unlock; 1102 1103 if (test_and_clear_bit(ST_M2M_SUSPENDING, &bdisp->state)) { 1104 set_bit(ST_M2M_SUSPENDED, &bdisp->state); 1105 wake_up(&bdisp->irq_queue); 1106 goto isr_unlock; 1107 } 1108 1109 ctx = v4l2_m2m_get_curr_priv(bdisp->m2m.m2m_dev); 1110 if (!ctx || !ctx->fh.m2m_ctx) 1111 goto isr_unlock; 1112 1113 spin_unlock(&bdisp->slock); 1114 1115 bdisp_job_finish(ctx, VB2_BUF_STATE_DONE); 1116 1117 if (bdisp_ctx_state_is_set(BDISP_CTX_STOP_REQ, ctx)) { 1118 bdisp_ctx_state_lock_clear(BDISP_CTX_STOP_REQ, ctx); 1119 wake_up(&bdisp->irq_queue); 1120 } 1121 1122 return IRQ_HANDLED; 1123 1124 isr_unlock: 1125 spin_unlock(&bdisp->slock); 1126 1127 return IRQ_HANDLED; 1128 } 1129 1130 static irqreturn_t bdisp_irq_handler(int irq, void *priv) 1131 { 1132 if (bdisp_hw_get_and_clear_irq((struct bdisp_dev *)priv)) 1133 return IRQ_NONE; 1134 else 1135 return IRQ_WAKE_THREAD; 1136 } 1137 1138 static void bdisp_irq_timeout(struct work_struct *ptr) 1139 { 1140 struct delayed_work *twork = to_delayed_work(ptr); 1141 struct bdisp_dev *bdisp = container_of(twork, struct bdisp_dev, 1142 timeout_work); 1143 struct bdisp_ctx *ctx; 1144 1145 ctx = v4l2_m2m_get_curr_priv(bdisp->m2m.m2m_dev); 1146 1147 dev_err(ctx->bdisp_dev->dev, "Device work timeout\n"); 1148 1149 spin_lock(&bdisp->slock); 1150 clear_bit(ST_M2M_RUNNING, &bdisp->state); 1151 spin_unlock(&bdisp->slock); 1152 1153 bdisp_hw_reset(bdisp); 1154 1155 bdisp_job_finish(ctx, VB2_BUF_STATE_ERROR); 1156 } 1157 1158 static int bdisp_m2m_suspend(struct bdisp_dev *bdisp) 1159 { 1160 unsigned long flags; 1161 long time_left; 1162 1163 spin_lock_irqsave(&bdisp->slock, flags); 1164 if (!test_bit(ST_M2M_RUNNING, &bdisp->state)) { 1165 spin_unlock_irqrestore(&bdisp->slock, flags); 1166 return 0; 1167 } 1168 clear_bit(ST_M2M_SUSPENDED, &bdisp->state); 1169 set_bit(ST_M2M_SUSPENDING, &bdisp->state); 1170 spin_unlock_irqrestore(&bdisp->slock, flags); 1171 1172 time_left = wait_event_timeout(bdisp->irq_queue, 1173 test_bit(ST_M2M_SUSPENDED, &bdisp->state), 1174 BDISP_WORK_TIMEOUT); 1175 1176 clear_bit(ST_M2M_SUSPENDING, &bdisp->state); 1177 1178 if (!time_left) { 1179 dev_err(bdisp->dev, "%s IRQ timeout\n", __func__); 1180 return -EAGAIN; 1181 } 1182 1183 return 0; 1184 } 1185 1186 static int bdisp_m2m_resume(struct bdisp_dev *bdisp) 1187 { 1188 struct bdisp_ctx *ctx; 1189 unsigned long flags; 1190 1191 spin_lock_irqsave(&bdisp->slock, flags); 1192 ctx = bdisp->m2m.ctx; 1193 bdisp->m2m.ctx = NULL; 1194 spin_unlock_irqrestore(&bdisp->slock, flags); 1195 1196 if (test_and_clear_bit(ST_M2M_SUSPENDED, &bdisp->state)) 1197 bdisp_job_finish(ctx, VB2_BUF_STATE_ERROR); 1198 1199 return 0; 1200 } 1201 1202 static int bdisp_runtime_resume(struct device *dev) 1203 { 1204 struct bdisp_dev *bdisp = dev_get_drvdata(dev); 1205 int ret = clk_enable(bdisp->clock); 1206 1207 if (ret) 1208 return ret; 1209 1210 return bdisp_m2m_resume(bdisp); 1211 } 1212 1213 static int bdisp_runtime_suspend(struct device *dev) 1214 { 1215 struct bdisp_dev *bdisp = dev_get_drvdata(dev); 1216 int ret = bdisp_m2m_suspend(bdisp); 1217 1218 if (!ret) 1219 clk_disable(bdisp->clock); 1220 1221 return ret; 1222 } 1223 1224 static int bdisp_resume(struct device *dev) 1225 { 1226 struct bdisp_dev *bdisp = dev_get_drvdata(dev); 1227 unsigned long flags; 1228 int opened; 1229 1230 spin_lock_irqsave(&bdisp->slock, flags); 1231 opened = test_bit(ST_M2M_OPEN, &bdisp->state); 1232 spin_unlock_irqrestore(&bdisp->slock, flags); 1233 1234 if (!opened) 1235 return 0; 1236 1237 if (!pm_runtime_suspended(dev)) 1238 return bdisp_runtime_resume(dev); 1239 1240 return 0; 1241 } 1242 1243 static int bdisp_suspend(struct device *dev) 1244 { 1245 if (!pm_runtime_suspended(dev)) 1246 return bdisp_runtime_suspend(dev); 1247 1248 return 0; 1249 } 1250 1251 static const struct dev_pm_ops bdisp_pm_ops = { 1252 .suspend = bdisp_suspend, 1253 .resume = bdisp_resume, 1254 .runtime_suspend = bdisp_runtime_suspend, 1255 .runtime_resume = bdisp_runtime_resume, 1256 }; 1257 1258 static void bdisp_remove(struct platform_device *pdev) 1259 { 1260 struct bdisp_dev *bdisp = platform_get_drvdata(pdev); 1261 1262 bdisp_unregister_device(bdisp); 1263 1264 bdisp_hw_free_filters(bdisp->dev); 1265 1266 pm_runtime_disable(&pdev->dev); 1267 1268 bdisp_debugfs_remove(bdisp); 1269 1270 v4l2_device_unregister(&bdisp->v4l2_dev); 1271 1272 if (!IS_ERR(bdisp->clock)) 1273 clk_unprepare(bdisp->clock); 1274 1275 destroy_workqueue(bdisp->work_queue); 1276 1277 dev_dbg(&pdev->dev, "%s driver unloaded\n", pdev->name); 1278 } 1279 1280 static int bdisp_probe(struct platform_device *pdev) 1281 { 1282 struct bdisp_dev *bdisp; 1283 struct device *dev = &pdev->dev; 1284 int ret; 1285 1286 dev_dbg(dev, "%s\n", __func__); 1287 1288 bdisp = devm_kzalloc(dev, sizeof(struct bdisp_dev), GFP_KERNEL); 1289 if (!bdisp) 1290 return -ENOMEM; 1291 1292 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32)); 1293 if (ret) 1294 return ret; 1295 1296 bdisp->pdev = pdev; 1297 bdisp->dev = dev; 1298 platform_set_drvdata(pdev, bdisp); 1299 1300 if (dev->of_node) 1301 bdisp->id = of_alias_get_id(pdev->dev.of_node, BDISP_NAME); 1302 else 1303 bdisp->id = pdev->id; 1304 1305 init_waitqueue_head(&bdisp->irq_queue); 1306 INIT_DELAYED_WORK(&bdisp->timeout_work, bdisp_irq_timeout); 1307 bdisp->work_queue = create_workqueue(BDISP_NAME); 1308 if (!bdisp->work_queue) 1309 return -ENOMEM; 1310 1311 spin_lock_init(&bdisp->slock); 1312 mutex_init(&bdisp->lock); 1313 1314 /* get resources */ 1315 bdisp->regs = devm_platform_ioremap_resource(pdev, 0); 1316 if (IS_ERR(bdisp->regs)) { 1317 ret = PTR_ERR(bdisp->regs); 1318 goto err_wq; 1319 } 1320 1321 bdisp->clock = devm_clk_get(dev, BDISP_NAME); 1322 if (IS_ERR(bdisp->clock)) { 1323 dev_err(dev, "failed to get clock\n"); 1324 ret = PTR_ERR(bdisp->clock); 1325 goto err_wq; 1326 } 1327 1328 ret = clk_prepare(bdisp->clock); 1329 if (ret < 0) { 1330 dev_err(dev, "clock prepare failed\n"); 1331 bdisp->clock = ERR_PTR(-EINVAL); 1332 goto err_wq; 1333 } 1334 1335 ret = platform_get_irq(pdev, 0); 1336 if (ret < 0) 1337 goto err_clk; 1338 1339 ret = devm_request_threaded_irq(dev, ret, bdisp_irq_handler, 1340 bdisp_irq_thread, IRQF_ONESHOT, 1341 pdev->name, bdisp); 1342 if (ret) { 1343 dev_err(dev, "failed to install irq\n"); 1344 goto err_clk; 1345 } 1346 1347 /* v4l2 register */ 1348 ret = v4l2_device_register(dev, &bdisp->v4l2_dev); 1349 if (ret) { 1350 dev_err(dev, "failed to register\n"); 1351 goto err_clk; 1352 } 1353 1354 /* Debug */ 1355 bdisp_debugfs_create(bdisp); 1356 1357 /* Power management */ 1358 pm_runtime_enable(dev); 1359 ret = pm_runtime_resume_and_get(dev); 1360 if (ret < 0) { 1361 dev_err(dev, "failed to set PM\n"); 1362 goto err_remove; 1363 } 1364 1365 /* Filters */ 1366 if (bdisp_hw_alloc_filters(bdisp->dev)) { 1367 dev_err(bdisp->dev, "no memory for filters\n"); 1368 ret = -ENOMEM; 1369 goto err_pm; 1370 } 1371 1372 /* Register */ 1373 ret = bdisp_register_device(bdisp); 1374 if (ret) { 1375 dev_err(dev, "failed to register\n"); 1376 goto err_filter; 1377 } 1378 1379 dev_info(dev, "%s%d registered as /dev/video%d\n", BDISP_NAME, 1380 bdisp->id, bdisp->vdev.num); 1381 1382 pm_runtime_put(dev); 1383 1384 return 0; 1385 1386 err_filter: 1387 bdisp_hw_free_filters(bdisp->dev); 1388 err_pm: 1389 pm_runtime_put(dev); 1390 err_remove: 1391 pm_runtime_disable(dev); 1392 bdisp_debugfs_remove(bdisp); 1393 v4l2_device_unregister(&bdisp->v4l2_dev); 1394 err_clk: 1395 clk_unprepare(bdisp->clock); 1396 err_wq: 1397 destroy_workqueue(bdisp->work_queue); 1398 return ret; 1399 } 1400 1401 static const struct of_device_id bdisp_match_types[] = { 1402 { 1403 .compatible = "st,stih407-bdisp", 1404 }, 1405 { /* end node */ } 1406 }; 1407 1408 MODULE_DEVICE_TABLE(of, bdisp_match_types); 1409 1410 static struct platform_driver bdisp_driver = { 1411 .probe = bdisp_probe, 1412 .remove = bdisp_remove, 1413 .driver = { 1414 .name = BDISP_NAME, 1415 .of_match_table = bdisp_match_types, 1416 .pm = &bdisp_pm_ops, 1417 }, 1418 }; 1419 1420 module_platform_driver(bdisp_driver); 1421 1422 MODULE_DESCRIPTION("2D blitter for STMicroelectronics SoC"); 1423 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>"); 1424 MODULE_LICENSE("GPL"); 1425