1/* 2 * V4L2 Driver for SuperH Mobile CEU interface 3 * 4 * Copyright (C) 2008 Magnus Damm 5 * 6 * Based on V4L2 Driver for PXA camera host - "pxa_camera.c", 7 * 8 * Copyright (C) 2006, Sascha Hauer, Pengutronix 9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 */ 16 17#include <linux/init.h> 18#include <linux/module.h> 19#include <linux/io.h> 20#include <linux/completion.h> 21#include <linux/delay.h> 22#include <linux/dma-mapping.h> 23#include <linux/err.h> 24#include <linux/errno.h> 25#include <linux/fs.h> 26#include <linux/interrupt.h> 27#include <linux/kernel.h> 28#include <linux/mm.h> 29#include <linux/moduleparam.h> 30#include <linux/of.h> 31#include <linux/time.h> 32#include <linux/slab.h> 33#include <linux/device.h> 34#include <linux/platform_device.h> 35#include <linux/videodev2.h> 36#include <linux/pm_runtime.h> 37#include <linux/sched.h> 38 39#include <media/v4l2-async.h> 40#include <media/v4l2-common.h> 41#include <media/v4l2-dev.h> 42#include <media/soc_camera.h> 43#include <media/sh_mobile_ceu.h> 44#include <media/sh_mobile_csi2.h> 45#include <media/videobuf2-dma-contig.h> 46#include <media/v4l2-mediabus.h> 47#include <media/soc_mediabus.h> 48 49#include "soc_scale_crop.h" 50 51/* register offsets for sh7722 / sh7723 */ 52 53#define CAPSR 0x00 /* Capture start register */ 54#define CAPCR 0x04 /* Capture control register */ 55#define CAMCR 0x08 /* Capture interface control register */ 56#define CMCYR 0x0c /* Capture interface cycle register */ 57#define CAMOR 0x10 /* Capture interface offset register */ 58#define CAPWR 0x14 /* Capture interface width register */ 59#define CAIFR 0x18 /* Capture interface input format register */ 60#define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */ 61#define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */ 62#define CRCNTR 0x28 /* CEU register control register */ 63#define CRCMPR 0x2c /* CEU register forcible control register */ 64#define CFLCR 0x30 /* Capture filter control register */ 65#define CFSZR 0x34 /* Capture filter size clip register */ 66#define CDWDR 0x38 /* Capture destination width register */ 67#define CDAYR 0x3c /* Capture data address Y register */ 68#define CDACR 0x40 /* Capture data address C register */ 69#define CDBYR 0x44 /* Capture data bottom-field address Y register */ 70#define CDBCR 0x48 /* Capture data bottom-field address C register */ 71#define CBDSR 0x4c /* Capture bundle destination size register */ 72#define CFWCR 0x5c /* Firewall operation control register */ 73#define CLFCR 0x60 /* Capture low-pass filter control register */ 74#define CDOCR 0x64 /* Capture data output control register */ 75#define CDDCR 0x68 /* Capture data complexity level register */ 76#define CDDAR 0x6c /* Capture data complexity level address register */ 77#define CEIER 0x70 /* Capture event interrupt enable register */ 78#define CETCR 0x74 /* Capture event flag clear register */ 79#define CSTSR 0x7c /* Capture status register */ 80#define CSRTR 0x80 /* Capture software reset register */ 81#define CDSSR 0x84 /* Capture data size register */ 82#define CDAYR2 0x90 /* Capture data address Y register 2 */ 83#define CDACR2 0x94 /* Capture data address C register 2 */ 84#define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */ 85#define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */ 86 87#undef DEBUG_GEOMETRY 88#ifdef DEBUG_GEOMETRY 89#define dev_geo dev_info 90#else 91#define dev_geo dev_dbg 92#endif 93 94/* per video frame buffer */ 95struct sh_mobile_ceu_buffer { 96 struct vb2_buffer vb; /* v4l buffer must be first */ 97 struct list_head queue; 98}; 99 100struct sh_mobile_ceu_dev { 101 struct soc_camera_host ici; 102 /* Asynchronous CSI2 linking */ 103 struct v4l2_async_subdev *csi2_asd; 104 struct v4l2_subdev *csi2_sd; 105 /* Synchronous probing compatibility */ 106 struct platform_device *csi2_pdev; 107 108 unsigned int irq; 109 void __iomem *base; 110 size_t video_limit; 111 size_t buf_total; 112 113 spinlock_t lock; /* Protects video buffer lists */ 114 struct list_head capture; 115 struct vb2_buffer *active; 116 struct vb2_alloc_ctx *alloc_ctx; 117 118 struct sh_mobile_ceu_info *pdata; 119 struct completion complete; 120 121 u32 cflcr; 122 123 /* static max sizes either from platform data or default */ 124 int max_width; 125 int max_height; 126 127 enum v4l2_field field; 128 int sequence; 129 unsigned long flags; 130 131 unsigned int image_mode:1; 132 unsigned int is_16bit:1; 133 unsigned int frozen:1; 134}; 135 136struct sh_mobile_ceu_cam { 137 /* CEU offsets within the camera output, before the CEU scaler */ 138 unsigned int ceu_left; 139 unsigned int ceu_top; 140 /* Client output, as seen by the CEU */ 141 unsigned int width; 142 unsigned int height; 143 /* 144 * User window from S_CROP / G_CROP, produced by client cropping and 145 * scaling, CEU scaling and CEU cropping, mapped back onto the client 146 * input window 147 */ 148 struct v4l2_rect subrect; 149 /* Camera cropping rectangle */ 150 struct v4l2_rect rect; 151 const struct soc_mbus_pixelfmt *extra_fmt; 152 u32 code; 153}; 154 155static struct sh_mobile_ceu_buffer *to_ceu_vb(struct vb2_buffer *vb) 156{ 157 return container_of(vb, struct sh_mobile_ceu_buffer, vb); 158} 159 160static void ceu_write(struct sh_mobile_ceu_dev *priv, 161 unsigned long reg_offs, u32 data) 162{ 163 iowrite32(data, priv->base + reg_offs); 164} 165 166static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs) 167{ 168 return ioread32(priv->base + reg_offs); 169} 170 171static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev) 172{ 173 int i, success = 0; 174 175 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */ 176 177 /* wait CSTSR.CPTON bit */ 178 for (i = 0; i < 1000; i++) { 179 if (!(ceu_read(pcdev, CSTSR) & 1)) { 180 success++; 181 break; 182 } 183 udelay(1); 184 } 185 186 /* wait CAPSR.CPKIL bit */ 187 for (i = 0; i < 1000; i++) { 188 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) { 189 success++; 190 break; 191 } 192 udelay(1); 193 } 194 195 if (2 != success) { 196 dev_warn(pcdev->ici.v4l2_dev.dev, "soft reset time out\n"); 197 return -EIO; 198 } 199 200 return 0; 201} 202 203/* 204 * Videobuf operations 205 */ 206 207/* 208 * .queue_setup() is called to check, whether the driver can accept the 209 * requested number of buffers and to fill in plane sizes 210 * for the current frame format if required 211 */ 212static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq, 213 const struct v4l2_format *fmt, 214 unsigned int *count, unsigned int *num_planes, 215 unsigned int sizes[], void *alloc_ctxs[]) 216{ 217 struct soc_camera_device *icd = container_of(vq, struct soc_camera_device, vb2_vidq); 218 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 219 struct sh_mobile_ceu_dev *pcdev = ici->priv; 220 221 if (fmt) { 222 const struct soc_camera_format_xlate *xlate = soc_camera_xlate_by_fourcc(icd, 223 fmt->fmt.pix.pixelformat); 224 unsigned int bytes_per_line; 225 int ret; 226 227 if (!xlate) 228 return -EINVAL; 229 230 ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width, 231 xlate->host_fmt); 232 if (ret < 0) 233 return ret; 234 235 bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret); 236 237 ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line, 238 fmt->fmt.pix.height); 239 if (ret < 0) 240 return ret; 241 242 sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret); 243 } else { 244 /* Called from VIDIOC_REQBUFS or in compatibility mode */ 245 sizes[0] = icd->sizeimage; 246 } 247 248 alloc_ctxs[0] = pcdev->alloc_ctx; 249 250 if (!vq->num_buffers) 251 pcdev->sequence = 0; 252 253 if (!*count) 254 *count = 2; 255 256 /* If *num_planes != 0, we have already verified *count. */ 257 if (pcdev->video_limit && !*num_planes) { 258 size_t size = PAGE_ALIGN(sizes[0]) * *count; 259 260 if (size + pcdev->buf_total > pcdev->video_limit) 261 *count = (pcdev->video_limit - pcdev->buf_total) / 262 PAGE_ALIGN(sizes[0]); 263 } 264 265 *num_planes = 1; 266 267 dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]); 268 269 return 0; 270} 271 272#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */ 273#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */ 274#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */ 275#define CEU_CEIER_VBP (1 << 20) /* vbp error */ 276#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */ 277#define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP) 278 279 280/* 281 * return value doesn't reflex the success/failure to queue the new buffer, 282 * but rather the status of the previous buffer. 283 */ 284static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev) 285{ 286 struct soc_camera_device *icd = pcdev->ici.icd; 287 dma_addr_t phys_addr_top, phys_addr_bottom; 288 unsigned long top1, top2; 289 unsigned long bottom1, bottom2; 290 u32 status; 291 bool planar; 292 int ret = 0; 293 294 /* 295 * The hardware is _very_ picky about this sequence. Especially 296 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge 297 * several not-so-well documented interrupt sources in CETCR. 298 */ 299 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK); 300 status = ceu_read(pcdev, CETCR); 301 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC); 302 if (!pcdev->frozen) 303 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK); 304 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP); 305 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW); 306 307 /* 308 * When a VBP interrupt occurs, a capture end interrupt does not occur 309 * and the image of that frame is not captured correctly. So, soft reset 310 * is needed here. 311 */ 312 if (status & CEU_CEIER_VBP) { 313 sh_mobile_ceu_soft_reset(pcdev); 314 ret = -EIO; 315 } 316 317 if (pcdev->frozen) { 318 complete(&pcdev->complete); 319 return ret; 320 } 321 322 if (!pcdev->active) 323 return ret; 324 325 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) { 326 top1 = CDBYR; 327 top2 = CDBCR; 328 bottom1 = CDAYR; 329 bottom2 = CDACR; 330 } else { 331 top1 = CDAYR; 332 top2 = CDACR; 333 bottom1 = CDBYR; 334 bottom2 = CDBCR; 335 } 336 337 phys_addr_top = vb2_dma_contig_plane_dma_addr(pcdev->active, 0); 338 339 switch (icd->current_fmt->host_fmt->fourcc) { 340 case V4L2_PIX_FMT_NV12: 341 case V4L2_PIX_FMT_NV21: 342 case V4L2_PIX_FMT_NV16: 343 case V4L2_PIX_FMT_NV61: 344 planar = true; 345 break; 346 default: 347 planar = false; 348 } 349 350 ceu_write(pcdev, top1, phys_addr_top); 351 if (V4L2_FIELD_NONE != pcdev->field) { 352 phys_addr_bottom = phys_addr_top + icd->bytesperline; 353 ceu_write(pcdev, bottom1, phys_addr_bottom); 354 } 355 356 if (planar) { 357 phys_addr_top += icd->bytesperline * icd->user_height; 358 ceu_write(pcdev, top2, phys_addr_top); 359 if (V4L2_FIELD_NONE != pcdev->field) { 360 phys_addr_bottom = phys_addr_top + icd->bytesperline; 361 ceu_write(pcdev, bottom2, phys_addr_bottom); 362 } 363 } 364 365 ceu_write(pcdev, CAPSR, 0x1); /* start capture */ 366 367 return ret; 368} 369 370static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer *vb) 371{ 372 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb); 373 374 /* Added list head initialization on alloc */ 375 WARN(!list_empty(&buf->queue), "Buffer %p on queue!\n", vb); 376 377 return 0; 378} 379 380static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer *vb) 381{ 382 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq); 383 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 384 struct sh_mobile_ceu_dev *pcdev = ici->priv; 385 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb); 386 unsigned long size; 387 388 size = icd->sizeimage; 389 390 if (vb2_plane_size(vb, 0) < size) { 391 dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n", 392 vb->v4l2_buf.index, vb2_plane_size(vb, 0), size); 393 goto error; 394 } 395 396 vb2_set_plane_payload(vb, 0, size); 397 398 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__, 399 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0)); 400 401#ifdef DEBUG 402 /* 403 * This can be useful if you want to see if we actually fill 404 * the buffer with something 405 */ 406 if (vb2_plane_vaddr(vb, 0)) 407 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0)); 408#endif 409 410 spin_lock_irq(&pcdev->lock); 411 list_add_tail(&buf->queue, &pcdev->capture); 412 413 if (!pcdev->active) { 414 /* 415 * Because there were no active buffer at this moment, 416 * we are not interested in the return value of 417 * sh_mobile_ceu_capture here. 418 */ 419 pcdev->active = vb; 420 sh_mobile_ceu_capture(pcdev); 421 } 422 spin_unlock_irq(&pcdev->lock); 423 424 return; 425 426error: 427 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); 428} 429 430static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb) 431{ 432 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq); 433 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 434 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb); 435 struct sh_mobile_ceu_dev *pcdev = ici->priv; 436 437 spin_lock_irq(&pcdev->lock); 438 439 if (pcdev->active == vb) { 440 /* disable capture (release DMA buffer), reset */ 441 ceu_write(pcdev, CAPSR, 1 << 16); 442 pcdev->active = NULL; 443 } 444 445 /* 446 * Doesn't hurt also if the list is empty, but it hurts, if queuing the 447 * buffer failed, and .buf_init() hasn't been called 448 */ 449 if (buf->queue.next) 450 list_del_init(&buf->queue); 451 452 pcdev->buf_total -= PAGE_ALIGN(vb2_plane_size(vb, 0)); 453 dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__, 454 pcdev->buf_total); 455 456 spin_unlock_irq(&pcdev->lock); 457} 458 459static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb) 460{ 461 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq); 462 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 463 struct sh_mobile_ceu_dev *pcdev = ici->priv; 464 465 pcdev->buf_total += PAGE_ALIGN(vb2_plane_size(vb, 0)); 466 dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__, 467 pcdev->buf_total); 468 469 /* This is for locking debugging only */ 470 INIT_LIST_HEAD(&to_ceu_vb(vb)->queue); 471 return 0; 472} 473 474static void sh_mobile_ceu_stop_streaming(struct vb2_queue *q) 475{ 476 struct soc_camera_device *icd = container_of(q, struct soc_camera_device, vb2_vidq); 477 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 478 struct sh_mobile_ceu_dev *pcdev = ici->priv; 479 struct list_head *buf_head, *tmp; 480 481 spin_lock_irq(&pcdev->lock); 482 483 pcdev->active = NULL; 484 485 list_for_each_safe(buf_head, tmp, &pcdev->capture) 486 list_del_init(buf_head); 487 488 spin_unlock_irq(&pcdev->lock); 489 490 sh_mobile_ceu_soft_reset(pcdev); 491} 492 493static struct vb2_ops sh_mobile_ceu_videobuf_ops = { 494 .queue_setup = sh_mobile_ceu_videobuf_setup, 495 .buf_prepare = sh_mobile_ceu_videobuf_prepare, 496 .buf_queue = sh_mobile_ceu_videobuf_queue, 497 .buf_cleanup = sh_mobile_ceu_videobuf_release, 498 .buf_init = sh_mobile_ceu_videobuf_init, 499 .wait_prepare = vb2_ops_wait_prepare, 500 .wait_finish = vb2_ops_wait_finish, 501 .stop_streaming = sh_mobile_ceu_stop_streaming, 502}; 503 504static irqreturn_t sh_mobile_ceu_irq(int irq, void *data) 505{ 506 struct sh_mobile_ceu_dev *pcdev = data; 507 struct vb2_buffer *vb; 508 int ret; 509 510 spin_lock(&pcdev->lock); 511 512 vb = pcdev->active; 513 if (!vb) 514 /* Stale interrupt from a released buffer */ 515 goto out; 516 517 list_del_init(&to_ceu_vb(vb)->queue); 518 519 if (!list_empty(&pcdev->capture)) 520 pcdev->active = &list_entry(pcdev->capture.next, 521 struct sh_mobile_ceu_buffer, queue)->vb; 522 else 523 pcdev->active = NULL; 524 525 ret = sh_mobile_ceu_capture(pcdev); 526 v4l2_get_timestamp(&vb->v4l2_buf.timestamp); 527 if (!ret) { 528 vb->v4l2_buf.field = pcdev->field; 529 vb->v4l2_buf.sequence = pcdev->sequence++; 530 } 531 vb2_buffer_done(vb, ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE); 532 533out: 534 spin_unlock(&pcdev->lock); 535 536 return IRQ_HANDLED; 537} 538 539static struct v4l2_subdev *find_csi2(struct sh_mobile_ceu_dev *pcdev) 540{ 541 struct v4l2_subdev *sd; 542 543 if (pcdev->csi2_sd) 544 return pcdev->csi2_sd; 545 546 if (pcdev->csi2_asd) { 547 char name[] = "sh-mobile-csi2"; 548 v4l2_device_for_each_subdev(sd, &pcdev->ici.v4l2_dev) 549 if (!strncmp(name, sd->name, sizeof(name) - 1)) { 550 pcdev->csi2_sd = sd; 551 return sd; 552 } 553 } 554 555 return NULL; 556} 557 558static struct v4l2_subdev *csi2_subdev(struct sh_mobile_ceu_dev *pcdev, 559 struct soc_camera_device *icd) 560{ 561 struct v4l2_subdev *sd = pcdev->csi2_sd; 562 563 return sd && sd->grp_id == soc_camera_grp_id(icd) ? sd : NULL; 564} 565 566static int sh_mobile_ceu_add_device(struct soc_camera_device *icd) 567{ 568 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 569 struct sh_mobile_ceu_dev *pcdev = ici->priv; 570 struct v4l2_subdev *csi2_sd = find_csi2(pcdev); 571 int ret; 572 573 if (csi2_sd) { 574 csi2_sd->grp_id = soc_camera_grp_id(icd); 575 v4l2_set_subdev_hostdata(csi2_sd, icd); 576 } 577 578 ret = v4l2_subdev_call(csi2_sd, core, s_power, 1); 579 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) 580 return ret; 581 582 /* 583 * -ENODEV is special: either csi2_sd == NULL or the CSI-2 driver 584 * has not found this soc-camera device among its clients 585 */ 586 if (csi2_sd && ret == -ENODEV) 587 csi2_sd->grp_id = 0; 588 589 dev_info(icd->parent, 590 "SuperH Mobile CEU%s driver attached to camera %d\n", 591 csi2_sd && csi2_sd->grp_id ? "/CSI-2" : "", icd->devnum); 592 593 return 0; 594} 595 596static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd) 597{ 598 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 599 struct sh_mobile_ceu_dev *pcdev = ici->priv; 600 struct v4l2_subdev *csi2_sd = find_csi2(pcdev); 601 602 dev_info(icd->parent, 603 "SuperH Mobile CEU driver detached from camera %d\n", 604 icd->devnum); 605 606 v4l2_subdev_call(csi2_sd, core, s_power, 0); 607} 608 609/* Called with .host_lock held */ 610static int sh_mobile_ceu_clock_start(struct soc_camera_host *ici) 611{ 612 struct sh_mobile_ceu_dev *pcdev = ici->priv; 613 614 pm_runtime_get_sync(ici->v4l2_dev.dev); 615 616 pcdev->buf_total = 0; 617 618 sh_mobile_ceu_soft_reset(pcdev); 619 620 return 0; 621} 622 623/* Called with .host_lock held */ 624static void sh_mobile_ceu_clock_stop(struct soc_camera_host *ici) 625{ 626 struct sh_mobile_ceu_dev *pcdev = ici->priv; 627 628 /* disable capture, disable interrupts */ 629 ceu_write(pcdev, CEIER, 0); 630 sh_mobile_ceu_soft_reset(pcdev); 631 632 /* make sure active buffer is canceled */ 633 spin_lock_irq(&pcdev->lock); 634 if (pcdev->active) { 635 list_del_init(&to_ceu_vb(pcdev->active)->queue); 636 vb2_buffer_done(pcdev->active, VB2_BUF_STATE_ERROR); 637 pcdev->active = NULL; 638 } 639 spin_unlock_irq(&pcdev->lock); 640 641 pm_runtime_put(ici->v4l2_dev.dev); 642} 643 644/* 645 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)" 646 * in SH7722 Hardware Manual 647 */ 648static unsigned int size_dst(unsigned int src, unsigned int scale) 649{ 650 unsigned int mant_pre = scale >> 12; 651 if (!src || !scale) 652 return src; 653 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) * 654 mant_pre * 4096 / scale + 1; 655} 656 657static u16 calc_scale(unsigned int src, unsigned int *dst) 658{ 659 u16 scale; 660 661 if (src == *dst) 662 return 0; 663 664 scale = (src * 4096 / *dst) & ~7; 665 666 while (scale > 4096 && size_dst(src, scale) < *dst) 667 scale -= 8; 668 669 *dst = size_dst(src, scale); 670 671 return scale; 672} 673 674/* rect is guaranteed to not exceed the scaled camera rectangle */ 675static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd) 676{ 677 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 678 struct sh_mobile_ceu_cam *cam = icd->host_priv; 679 struct sh_mobile_ceu_dev *pcdev = ici->priv; 680 unsigned int height, width, cdwdr_width, in_width, in_height; 681 unsigned int left_offset, top_offset; 682 u32 camor; 683 684 dev_geo(icd->parent, "Crop %ux%u@%u:%u\n", 685 icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top); 686 687 left_offset = cam->ceu_left; 688 top_offset = cam->ceu_top; 689 690 WARN_ON(icd->user_width & 3 || icd->user_height & 3); 691 692 width = icd->user_width; 693 694 if (pcdev->image_mode) { 695 in_width = cam->width; 696 if (!pcdev->is_16bit) { 697 in_width *= 2; 698 left_offset *= 2; 699 } 700 } else { 701 unsigned int w_factor; 702 703 switch (icd->current_fmt->host_fmt->packing) { 704 case SOC_MBUS_PACKING_2X8_PADHI: 705 w_factor = 2; 706 break; 707 default: 708 w_factor = 1; 709 } 710 711 in_width = cam->width * w_factor; 712 left_offset *= w_factor; 713 } 714 715 cdwdr_width = icd->bytesperline; 716 717 height = icd->user_height; 718 in_height = cam->height; 719 if (V4L2_FIELD_NONE != pcdev->field) { 720 height = (height / 2) & ~3; 721 in_height /= 2; 722 top_offset /= 2; 723 cdwdr_width *= 2; 724 } 725 726 /* CSI2 special configuration */ 727 if (csi2_subdev(pcdev, icd)) { 728 in_width = ((in_width - 2) * 2); 729 left_offset *= 2; 730 } 731 732 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */ 733 camor = left_offset | (top_offset << 16); 734 735 dev_geo(icd->parent, 736 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor, 737 (in_height << 16) | in_width, (height << 16) | width, 738 cdwdr_width); 739 740 ceu_write(pcdev, CAMOR, camor); 741 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width); 742 /* CFSZR clipping is applied _after_ the scaling filter (CFLCR) */ 743 ceu_write(pcdev, CFSZR, (height << 16) | width); 744 ceu_write(pcdev, CDWDR, cdwdr_width); 745} 746 747static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev) 748{ 749 u32 capsr = ceu_read(pcdev, CAPSR); 750 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */ 751 return capsr; 752} 753 754static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr) 755{ 756 unsigned long timeout = jiffies + 10 * HZ; 757 758 /* 759 * Wait until the end of the current frame. It can take a long time, 760 * but if it has been aborted by a CAPSR reset, it shoule exit sooner. 761 */ 762 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout)) 763 msleep(1); 764 765 if (time_after(jiffies, timeout)) { 766 dev_err(pcdev->ici.v4l2_dev.dev, 767 "Timeout waiting for frame end! Interface problem?\n"); 768 return; 769 } 770 771 /* Wait until reset clears, this shall not hang... */ 772 while (ceu_read(pcdev, CAPSR) & (1 << 16)) 773 udelay(10); 774 775 /* Anything to restore? */ 776 if (capsr & ~(1 << 16)) 777 ceu_write(pcdev, CAPSR, capsr); 778} 779 780/* Find the bus subdevice driver, e.g., CSI2 */ 781static struct v4l2_subdev *find_bus_subdev(struct sh_mobile_ceu_dev *pcdev, 782 struct soc_camera_device *icd) 783{ 784 return csi2_subdev(pcdev, icd) ? : soc_camera_to_subdev(icd); 785} 786 787#define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \ 788 V4L2_MBUS_PCLK_SAMPLE_RISING | \ 789 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \ 790 V4L2_MBUS_HSYNC_ACTIVE_LOW | \ 791 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \ 792 V4L2_MBUS_VSYNC_ACTIVE_LOW | \ 793 V4L2_MBUS_DATA_ACTIVE_HIGH) 794 795/* Capture is not running, no interrupts, no locking needed */ 796static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd) 797{ 798 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 799 struct sh_mobile_ceu_dev *pcdev = ici->priv; 800 struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd); 801 struct sh_mobile_ceu_cam *cam = icd->host_priv; 802 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; 803 unsigned long value, common_flags = CEU_BUS_FLAGS; 804 u32 capsr = capture_save_reset(pcdev); 805 unsigned int yuv_lineskip; 806 int ret; 807 808 /* 809 * If the client doesn't implement g_mbus_config, we just use our 810 * platform data 811 */ 812 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); 813 if (!ret) { 814 common_flags = soc_mbus_config_compatible(&cfg, 815 common_flags); 816 if (!common_flags) 817 return -EINVAL; 818 } else if (ret != -ENOIOCTLCMD) { 819 return ret; 820 } 821 822 /* Make choises, based on platform preferences */ 823 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) && 824 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) { 825 if (pcdev->flags & SH_CEU_FLAG_HSYNC_LOW) 826 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH; 827 else 828 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW; 829 } 830 831 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) && 832 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) { 833 if (pcdev->flags & SH_CEU_FLAG_VSYNC_LOW) 834 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH; 835 else 836 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW; 837 } 838 839 cfg.flags = common_flags; 840 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg); 841 if (ret < 0 && ret != -ENOIOCTLCMD) 842 return ret; 843 844 if (icd->current_fmt->host_fmt->bits_per_sample > 8) 845 pcdev->is_16bit = 1; 846 else 847 pcdev->is_16bit = 0; 848 849 ceu_write(pcdev, CRCNTR, 0); 850 ceu_write(pcdev, CRCMPR, 0); 851 852 value = 0x00000010; /* data fetch by default */ 853 yuv_lineskip = 0x10; 854 855 switch (icd->current_fmt->host_fmt->fourcc) { 856 case V4L2_PIX_FMT_NV12: 857 case V4L2_PIX_FMT_NV21: 858 /* convert 4:2:2 -> 4:2:0 */ 859 yuv_lineskip = 0; /* skip for NV12/21, no skip for NV16/61 */ 860 /* fall-through */ 861 case V4L2_PIX_FMT_NV16: 862 case V4L2_PIX_FMT_NV61: 863 switch (cam->code) { 864 case MEDIA_BUS_FMT_UYVY8_2X8: 865 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */ 866 break; 867 case MEDIA_BUS_FMT_VYUY8_2X8: 868 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */ 869 break; 870 case MEDIA_BUS_FMT_YUYV8_2X8: 871 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */ 872 break; 873 case MEDIA_BUS_FMT_YVYU8_2X8: 874 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */ 875 break; 876 default: 877 BUG(); 878 } 879 } 880 881 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 || 882 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61) 883 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */ 884 885 value |= common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? 1 << 1 : 0; 886 value |= common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? 1 << 0 : 0; 887 888 if (csi2_subdev(pcdev, icd)) /* CSI2 mode */ 889 value |= 3 << 12; 890 else if (pcdev->is_16bit) 891 value |= 1 << 12; 892 else if (pcdev->flags & SH_CEU_FLAG_LOWER_8BIT) 893 value |= 2 << 12; 894 895 ceu_write(pcdev, CAMCR, value); 896 897 ceu_write(pcdev, CAPCR, 0x00300000); 898 899 switch (pcdev->field) { 900 case V4L2_FIELD_INTERLACED_TB: 901 value = 0x101; 902 break; 903 case V4L2_FIELD_INTERLACED_BT: 904 value = 0x102; 905 break; 906 default: 907 value = 0; 908 break; 909 } 910 ceu_write(pcdev, CAIFR, value); 911 912 sh_mobile_ceu_set_rect(icd); 913 mdelay(1); 914 915 dev_geo(icd->parent, "CFLCR 0x%x\n", pcdev->cflcr); 916 ceu_write(pcdev, CFLCR, pcdev->cflcr); 917 918 /* 919 * A few words about byte order (observed in Big Endian mode) 920 * 921 * In data fetch mode bytes are received in chunks of 8 bytes. 922 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first) 923 * 924 * The data is however by default written to memory in reverse order: 925 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte) 926 * 927 * The lowest three bits of CDOCR allows us to do swapping, 928 * using 7 we swap the data bytes to match the incoming order: 929 * D0, D1, D2, D3, D4, D5, D6, D7 930 */ 931 value = 0x00000007 | yuv_lineskip; 932 933 ceu_write(pcdev, CDOCR, value); 934 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */ 935 936 capture_restore(pcdev, capsr); 937 938 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */ 939 return 0; 940} 941 942static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd, 943 unsigned char buswidth) 944{ 945 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 946 struct sh_mobile_ceu_dev *pcdev = ici->priv; 947 struct v4l2_subdev *sd = find_bus_subdev(pcdev, icd); 948 unsigned long common_flags = CEU_BUS_FLAGS; 949 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,}; 950 int ret; 951 952 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); 953 if (!ret) 954 common_flags = soc_mbus_config_compatible(&cfg, 955 common_flags); 956 else if (ret != -ENOIOCTLCMD) 957 return ret; 958 959 if (!common_flags || buswidth > 16) 960 return -EINVAL; 961 962 return 0; 963} 964 965static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = { 966 { 967 .fourcc = V4L2_PIX_FMT_NV12, 968 .name = "NV12", 969 .bits_per_sample = 8, 970 .packing = SOC_MBUS_PACKING_1_5X8, 971 .order = SOC_MBUS_ORDER_LE, 972 .layout = SOC_MBUS_LAYOUT_PLANAR_2Y_C, 973 }, { 974 .fourcc = V4L2_PIX_FMT_NV21, 975 .name = "NV21", 976 .bits_per_sample = 8, 977 .packing = SOC_MBUS_PACKING_1_5X8, 978 .order = SOC_MBUS_ORDER_LE, 979 .layout = SOC_MBUS_LAYOUT_PLANAR_2Y_C, 980 }, { 981 .fourcc = V4L2_PIX_FMT_NV16, 982 .name = "NV16", 983 .bits_per_sample = 8, 984 .packing = SOC_MBUS_PACKING_2X8_PADHI, 985 .order = SOC_MBUS_ORDER_LE, 986 .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C, 987 }, { 988 .fourcc = V4L2_PIX_FMT_NV61, 989 .name = "NV61", 990 .bits_per_sample = 8, 991 .packing = SOC_MBUS_PACKING_2X8_PADHI, 992 .order = SOC_MBUS_ORDER_LE, 993 .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C, 994 }, 995}; 996 997/* This will be corrected as we get more formats */ 998static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt) 999{ 1000 return fmt->packing == SOC_MBUS_PACKING_NONE || 1001 (fmt->bits_per_sample == 8 && 1002 fmt->packing == SOC_MBUS_PACKING_1_5X8) || 1003 (fmt->bits_per_sample == 8 && 1004 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) || 1005 (fmt->bits_per_sample > 8 && 1006 fmt->packing == SOC_MBUS_PACKING_EXTEND16); 1007} 1008 1009static struct soc_camera_device *ctrl_to_icd(struct v4l2_ctrl *ctrl) 1010{ 1011 return container_of(ctrl->handler, struct soc_camera_device, 1012 ctrl_handler); 1013} 1014 1015static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl *ctrl) 1016{ 1017 struct soc_camera_device *icd = ctrl_to_icd(ctrl); 1018 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1019 struct sh_mobile_ceu_dev *pcdev = ici->priv; 1020 1021 switch (ctrl->id) { 1022 case V4L2_CID_SHARPNESS: 1023 switch (icd->current_fmt->host_fmt->fourcc) { 1024 case V4L2_PIX_FMT_NV12: 1025 case V4L2_PIX_FMT_NV21: 1026 case V4L2_PIX_FMT_NV16: 1027 case V4L2_PIX_FMT_NV61: 1028 ceu_write(pcdev, CLFCR, !ctrl->val); 1029 return 0; 1030 } 1031 break; 1032 } 1033 1034 return -EINVAL; 1035} 1036 1037static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops = { 1038 .s_ctrl = sh_mobile_ceu_s_ctrl, 1039}; 1040 1041static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx, 1042 struct soc_camera_format_xlate *xlate) 1043{ 1044 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1045 struct device *dev = icd->parent; 1046 struct soc_camera_host *ici = to_soc_camera_host(dev); 1047 struct sh_mobile_ceu_dev *pcdev = ici->priv; 1048 int ret, k, n; 1049 int formats = 0; 1050 struct sh_mobile_ceu_cam *cam; 1051 u32 code; 1052 const struct soc_mbus_pixelfmt *fmt; 1053 1054 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code); 1055 if (ret < 0) 1056 /* No more formats */ 1057 return 0; 1058 1059 fmt = soc_mbus_get_fmtdesc(code); 1060 if (!fmt) { 1061 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code); 1062 return 0; 1063 } 1064 1065 if (!csi2_subdev(pcdev, icd)) { 1066 /* Are there any restrictions in the CSI-2 case? */ 1067 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample); 1068 if (ret < 0) 1069 return 0; 1070 } 1071 1072 if (!icd->host_priv) { 1073 struct v4l2_mbus_framefmt mf; 1074 struct v4l2_rect rect; 1075 int shift = 0; 1076 1077 /* Add our control */ 1078 v4l2_ctrl_new_std(&icd->ctrl_handler, &sh_mobile_ceu_ctrl_ops, 1079 V4L2_CID_SHARPNESS, 0, 1, 1, 1); 1080 if (icd->ctrl_handler.error) 1081 return icd->ctrl_handler.error; 1082 1083 /* FIXME: subwindow is lost between close / open */ 1084 1085 /* Cache current client geometry */ 1086 ret = soc_camera_client_g_rect(sd, &rect); 1087 if (ret < 0) 1088 return ret; 1089 1090 /* First time */ 1091 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); 1092 if (ret < 0) 1093 return ret; 1094 1095 /* 1096 * All currently existing CEU implementations support 2560x1920 1097 * or larger frames. If the sensor is proposing too big a frame, 1098 * don't bother with possibly supportred by the CEU larger 1099 * sizes, just try VGA multiples. If needed, this can be 1100 * adjusted in the future. 1101 */ 1102 while ((mf.width > pcdev->max_width || 1103 mf.height > pcdev->max_height) && shift < 4) { 1104 /* Try 2560x1920, 1280x960, 640x480, 320x240 */ 1105 mf.width = 2560 >> shift; 1106 mf.height = 1920 >> shift; 1107 ret = v4l2_device_call_until_err(sd->v4l2_dev, 1108 soc_camera_grp_id(icd), video, 1109 s_mbus_fmt, &mf); 1110 if (ret < 0) 1111 return ret; 1112 shift++; 1113 } 1114 1115 if (shift == 4) { 1116 dev_err(dev, "Failed to configure the client below %ux%x\n", 1117 mf.width, mf.height); 1118 return -EIO; 1119 } 1120 1121 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height); 1122 1123 cam = kzalloc(sizeof(*cam), GFP_KERNEL); 1124 if (!cam) 1125 return -ENOMEM; 1126 1127 /* We are called with current camera crop, initialise subrect with it */ 1128 cam->rect = rect; 1129 cam->subrect = rect; 1130 1131 cam->width = mf.width; 1132 cam->height = mf.height; 1133 1134 icd->host_priv = cam; 1135 } else { 1136 cam = icd->host_priv; 1137 } 1138 1139 /* Beginning of a pass */ 1140 if (!idx) 1141 cam->extra_fmt = NULL; 1142 1143 switch (code) { 1144 case MEDIA_BUS_FMT_UYVY8_2X8: 1145 case MEDIA_BUS_FMT_VYUY8_2X8: 1146 case MEDIA_BUS_FMT_YUYV8_2X8: 1147 case MEDIA_BUS_FMT_YVYU8_2X8: 1148 if (cam->extra_fmt) 1149 break; 1150 1151 /* 1152 * Our case is simple so far: for any of the above four camera 1153 * formats we add all our four synthesized NV* formats, so, 1154 * just marking the device with a single flag suffices. If 1155 * the format generation rules are more complex, you would have 1156 * to actually hang your already added / counted formats onto 1157 * the host_priv pointer and check whether the format you're 1158 * going to add now is already there. 1159 */ 1160 cam->extra_fmt = sh_mobile_ceu_formats; 1161 1162 n = ARRAY_SIZE(sh_mobile_ceu_formats); 1163 formats += n; 1164 for (k = 0; xlate && k < n; k++) { 1165 xlate->host_fmt = &sh_mobile_ceu_formats[k]; 1166 xlate->code = code; 1167 xlate++; 1168 dev_dbg(dev, "Providing format %s using code %d\n", 1169 sh_mobile_ceu_formats[k].name, code); 1170 } 1171 break; 1172 default: 1173 if (!sh_mobile_ceu_packing_supported(fmt)) 1174 return 0; 1175 } 1176 1177 /* Generic pass-through */ 1178 formats++; 1179 if (xlate) { 1180 xlate->host_fmt = fmt; 1181 xlate->code = code; 1182 xlate++; 1183 dev_dbg(dev, "Providing format %s in pass-through mode\n", 1184 fmt->name); 1185 } 1186 1187 return formats; 1188} 1189 1190static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd) 1191{ 1192 kfree(icd->host_priv); 1193 icd->host_priv = NULL; 1194} 1195 1196#define scale_down(size, scale) soc_camera_shift_scale(size, 12, scale) 1197#define calc_generic_scale(in, out) soc_camera_calc_scale(in, 12, out) 1198 1199/* 1200 * CEU can scale and crop, but we don't want to waste bandwidth and kill the 1201 * framerate by always requesting the maximum image from the client. See 1202 * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of 1203 * scaling and cropping algorithms and for the meaning of referenced here steps. 1204 */ 1205static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd, 1206 const struct v4l2_crop *a) 1207{ 1208 struct v4l2_crop a_writable = *a; 1209 const struct v4l2_rect *rect = &a_writable.c; 1210 struct device *dev = icd->parent; 1211 struct soc_camera_host *ici = to_soc_camera_host(dev); 1212 struct sh_mobile_ceu_dev *pcdev = ici->priv; 1213 struct v4l2_crop cam_crop; 1214 struct sh_mobile_ceu_cam *cam = icd->host_priv; 1215 struct v4l2_rect *cam_rect = &cam_crop.c; 1216 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1217 struct v4l2_mbus_framefmt mf; 1218 unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v, 1219 out_width, out_height; 1220 int interm_width, interm_height; 1221 u32 capsr, cflcr; 1222 int ret; 1223 1224 dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height, 1225 rect->left, rect->top); 1226 1227 /* During camera cropping its output window can change too, stop CEU */ 1228 capsr = capture_save_reset(pcdev); 1229 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr); 1230 1231 /* 1232 * 1. - 2. Apply iterative camera S_CROP for new input window, read back 1233 * actual camera rectangle. 1234 */ 1235 ret = soc_camera_client_s_crop(sd, &a_writable, &cam_crop, 1236 &cam->rect, &cam->subrect); 1237 if (ret < 0) 1238 return ret; 1239 1240 dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n", 1241 cam_rect->width, cam_rect->height, 1242 cam_rect->left, cam_rect->top); 1243 1244 /* On success cam_crop contains current camera crop */ 1245 1246 /* 3. Retrieve camera output window */ 1247 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); 1248 if (ret < 0) 1249 return ret; 1250 1251 if (mf.width > pcdev->max_width || mf.height > pcdev->max_height) 1252 return -EINVAL; 1253 1254 /* 4. Calculate camera scales */ 1255 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width); 1256 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height); 1257 1258 /* Calculate intermediate window */ 1259 interm_width = scale_down(rect->width, scale_cam_h); 1260 interm_height = scale_down(rect->height, scale_cam_v); 1261 1262 if (interm_width < icd->user_width) { 1263 u32 new_scale_h; 1264 1265 new_scale_h = calc_generic_scale(rect->width, icd->user_width); 1266 1267 mf.width = scale_down(cam_rect->width, new_scale_h); 1268 } 1269 1270 if (interm_height < icd->user_height) { 1271 u32 new_scale_v; 1272 1273 new_scale_v = calc_generic_scale(rect->height, icd->user_height); 1274 1275 mf.height = scale_down(cam_rect->height, new_scale_v); 1276 } 1277 1278 if (interm_width < icd->user_width || interm_height < icd->user_height) { 1279 ret = v4l2_device_call_until_err(sd->v4l2_dev, 1280 soc_camera_grp_id(icd), video, 1281 s_mbus_fmt, &mf); 1282 if (ret < 0) 1283 return ret; 1284 1285 dev_geo(dev, "New camera output %ux%u\n", mf.width, mf.height); 1286 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width); 1287 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height); 1288 interm_width = scale_down(rect->width, scale_cam_h); 1289 interm_height = scale_down(rect->height, scale_cam_v); 1290 } 1291 1292 /* Cache camera output window */ 1293 cam->width = mf.width; 1294 cam->height = mf.height; 1295 1296 if (pcdev->image_mode) { 1297 out_width = min(interm_width, icd->user_width); 1298 out_height = min(interm_height, icd->user_height); 1299 } else { 1300 out_width = interm_width; 1301 out_height = interm_height; 1302 } 1303 1304 /* 1305 * 5. Calculate CEU scales from camera scales from results of (5) and 1306 * the user window 1307 */ 1308 scale_ceu_h = calc_scale(interm_width, &out_width); 1309 scale_ceu_v = calc_scale(interm_height, &out_height); 1310 1311 dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v); 1312 1313 /* Apply CEU scales. */ 1314 cflcr = scale_ceu_h | (scale_ceu_v << 16); 1315 if (cflcr != pcdev->cflcr) { 1316 pcdev->cflcr = cflcr; 1317 ceu_write(pcdev, CFLCR, cflcr); 1318 } 1319 1320 icd->user_width = out_width & ~3; 1321 icd->user_height = out_height & ~3; 1322 /* Offsets are applied at the CEU scaling filter input */ 1323 cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1; 1324 cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1; 1325 1326 /* 6. Use CEU cropping to crop to the new window. */ 1327 sh_mobile_ceu_set_rect(icd); 1328 1329 cam->subrect = *rect; 1330 1331 dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n", 1332 icd->user_width, icd->user_height, 1333 cam->ceu_left, cam->ceu_top); 1334 1335 /* Restore capture. The CE bit can be cleared by the hardware */ 1336 if (pcdev->active) 1337 capsr |= 1; 1338 capture_restore(pcdev, capsr); 1339 1340 /* Even if only camera cropping succeeded */ 1341 return ret; 1342} 1343 1344static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd, 1345 struct v4l2_crop *a) 1346{ 1347 struct sh_mobile_ceu_cam *cam = icd->host_priv; 1348 1349 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1350 a->c = cam->subrect; 1351 1352 return 0; 1353} 1354 1355/* Similar to set_crop multistage iterative algorithm */ 1356static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd, 1357 struct v4l2_format *f) 1358{ 1359 struct device *dev = icd->parent; 1360 struct soc_camera_host *ici = to_soc_camera_host(dev); 1361 struct sh_mobile_ceu_dev *pcdev = ici->priv; 1362 struct sh_mobile_ceu_cam *cam = icd->host_priv; 1363 struct v4l2_pix_format *pix = &f->fmt.pix; 1364 struct v4l2_mbus_framefmt mf; 1365 __u32 pixfmt = pix->pixelformat; 1366 const struct soc_camera_format_xlate *xlate; 1367 unsigned int ceu_sub_width = pcdev->max_width, 1368 ceu_sub_height = pcdev->max_height; 1369 u16 scale_v, scale_h; 1370 int ret; 1371 bool image_mode; 1372 enum v4l2_field field; 1373 1374 switch (pix->field) { 1375 default: 1376 pix->field = V4L2_FIELD_NONE; 1377 /* fall-through */ 1378 case V4L2_FIELD_INTERLACED_TB: 1379 case V4L2_FIELD_INTERLACED_BT: 1380 case V4L2_FIELD_NONE: 1381 field = pix->field; 1382 break; 1383 case V4L2_FIELD_INTERLACED: 1384 field = V4L2_FIELD_INTERLACED_TB; 1385 break; 1386 } 1387 1388 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); 1389 if (!xlate) { 1390 dev_warn(dev, "Format %x not found\n", pixfmt); 1391 return -EINVAL; 1392 } 1393 1394 /* 1.-4. Calculate desired client output geometry */ 1395 soc_camera_calc_client_output(icd, &cam->rect, &cam->subrect, pix, &mf, 12); 1396 mf.field = pix->field; 1397 mf.colorspace = pix->colorspace; 1398 mf.code = xlate->code; 1399 1400 switch (pixfmt) { 1401 case V4L2_PIX_FMT_NV12: 1402 case V4L2_PIX_FMT_NV21: 1403 case V4L2_PIX_FMT_NV16: 1404 case V4L2_PIX_FMT_NV61: 1405 image_mode = true; 1406 break; 1407 default: 1408 image_mode = false; 1409 } 1410 1411 dev_geo(dev, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt, mf.field, mf.code, 1412 pix->width, pix->height); 1413 1414 dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height); 1415 1416 /* 5. - 9. */ 1417 ret = soc_camera_client_scale(icd, &cam->rect, &cam->subrect, 1418 &mf, &ceu_sub_width, &ceu_sub_height, 1419 image_mode && V4L2_FIELD_NONE == field, 12); 1420 1421 dev_geo(dev, "5-9: client scale return %d\n", ret); 1422 1423 /* Done with the camera. Now see if we can improve the result */ 1424 1425 dev_geo(dev, "fmt %ux%u, requested %ux%u\n", 1426 mf.width, mf.height, pix->width, pix->height); 1427 if (ret < 0) 1428 return ret; 1429 1430 if (mf.code != xlate->code) 1431 return -EINVAL; 1432 1433 /* 9. Prepare CEU crop */ 1434 cam->width = mf.width; 1435 cam->height = mf.height; 1436 1437 /* 10. Use CEU scaling to scale to the requested user window. */ 1438 1439 /* We cannot scale up */ 1440 if (pix->width > ceu_sub_width) 1441 ceu_sub_width = pix->width; 1442 1443 if (pix->height > ceu_sub_height) 1444 ceu_sub_height = pix->height; 1445 1446 pix->colorspace = mf.colorspace; 1447 1448 if (image_mode) { 1449 /* Scale pix->{width x height} down to width x height */ 1450 scale_h = calc_scale(ceu_sub_width, &pix->width); 1451 scale_v = calc_scale(ceu_sub_height, &pix->height); 1452 } else { 1453 pix->width = ceu_sub_width; 1454 pix->height = ceu_sub_height; 1455 scale_h = 0; 1456 scale_v = 0; 1457 } 1458 1459 pcdev->cflcr = scale_h | (scale_v << 16); 1460 1461 /* 1462 * We have calculated CFLCR, the actual configuration will be performed 1463 * in sh_mobile_ceu_set_bus_param() 1464 */ 1465 1466 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n", 1467 ceu_sub_width, scale_h, pix->width, 1468 ceu_sub_height, scale_v, pix->height); 1469 1470 cam->code = xlate->code; 1471 icd->current_fmt = xlate; 1472 1473 pcdev->field = field; 1474 pcdev->image_mode = image_mode; 1475 1476 /* CFSZR requirement */ 1477 pix->width &= ~3; 1478 pix->height &= ~3; 1479 1480 return 0; 1481} 1482 1483#define CEU_CHDW_MAX 8188U /* Maximum line stride */ 1484 1485static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd, 1486 struct v4l2_format *f) 1487{ 1488 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1489 struct sh_mobile_ceu_dev *pcdev = ici->priv; 1490 const struct soc_camera_format_xlate *xlate; 1491 struct v4l2_pix_format *pix = &f->fmt.pix; 1492 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1493 struct v4l2_mbus_framefmt mf; 1494 __u32 pixfmt = pix->pixelformat; 1495 int width, height; 1496 int ret; 1497 1498 dev_geo(icd->parent, "TRY_FMT(pix=0x%x, %ux%u)\n", 1499 pixfmt, pix->width, pix->height); 1500 1501 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); 1502 if (!xlate) { 1503 xlate = icd->current_fmt; 1504 dev_dbg(icd->parent, "Format %x not found, keeping %x\n", 1505 pixfmt, xlate->host_fmt->fourcc); 1506 pixfmt = xlate->host_fmt->fourcc; 1507 pix->pixelformat = pixfmt; 1508 pix->colorspace = icd->colorspace; 1509 } 1510 1511 /* FIXME: calculate using depth and bus width */ 1512 1513 /* CFSZR requires height and width to be 4-pixel aligned */ 1514 v4l_bound_align_image(&pix->width, 2, pcdev->max_width, 2, 1515 &pix->height, 4, pcdev->max_height, 2, 0); 1516 1517 width = pix->width; 1518 height = pix->height; 1519 1520 /* limit to sensor capabilities */ 1521 mf.width = pix->width; 1522 mf.height = pix->height; 1523 mf.field = pix->field; 1524 mf.code = xlate->code; 1525 mf.colorspace = pix->colorspace; 1526 1527 ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd), 1528 video, try_mbus_fmt, &mf); 1529 if (ret < 0) 1530 return ret; 1531 1532 pix->width = mf.width; 1533 pix->height = mf.height; 1534 pix->field = mf.field; 1535 pix->colorspace = mf.colorspace; 1536 1537 switch (pixfmt) { 1538 case V4L2_PIX_FMT_NV12: 1539 case V4L2_PIX_FMT_NV21: 1540 case V4L2_PIX_FMT_NV16: 1541 case V4L2_PIX_FMT_NV61: 1542 /* FIXME: check against rect_max after converting soc-camera */ 1543 /* We can scale precisely, need a bigger image from camera */ 1544 if (pix->width < width || pix->height < height) { 1545 /* 1546 * We presume, the sensor behaves sanely, i.e., if 1547 * requested a bigger rectangle, it will not return a 1548 * smaller one. 1549 */ 1550 mf.width = pcdev->max_width; 1551 mf.height = pcdev->max_height; 1552 ret = v4l2_device_call_until_err(sd->v4l2_dev, 1553 soc_camera_grp_id(icd), video, 1554 try_mbus_fmt, &mf); 1555 if (ret < 0) { 1556 /* Shouldn't actually happen... */ 1557 dev_err(icd->parent, 1558 "FIXME: client try_fmt() = %d\n", ret); 1559 return ret; 1560 } 1561 } 1562 /* We will scale exactly */ 1563 if (mf.width > width) 1564 pix->width = width; 1565 if (mf.height > height) 1566 pix->height = height; 1567 1568 pix->bytesperline = max(pix->bytesperline, pix->width); 1569 pix->bytesperline = min(pix->bytesperline, CEU_CHDW_MAX); 1570 pix->bytesperline &= ~3; 1571 break; 1572 1573 default: 1574 /* Configurable stride isn't supported in pass-through mode. */ 1575 pix->bytesperline = 0; 1576 } 1577 1578 pix->width &= ~3; 1579 pix->height &= ~3; 1580 pix->sizeimage = 0; 1581 1582 dev_geo(icd->parent, "%s(): return %d, fmt 0x%x, %ux%u\n", 1583 __func__, ret, pix->pixelformat, pix->width, pix->height); 1584 1585 return ret; 1586} 1587 1588static int sh_mobile_ceu_set_livecrop(struct soc_camera_device *icd, 1589 const struct v4l2_crop *a) 1590{ 1591 struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1592 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1593 struct sh_mobile_ceu_dev *pcdev = ici->priv; 1594 u32 out_width = icd->user_width, out_height = icd->user_height; 1595 int ret; 1596 1597 /* Freeze queue */ 1598 pcdev->frozen = 1; 1599 /* Wait for frame */ 1600 ret = wait_for_completion_interruptible(&pcdev->complete); 1601 /* Stop the client */ 1602 ret = v4l2_subdev_call(sd, video, s_stream, 0); 1603 if (ret < 0) 1604 dev_warn(icd->parent, 1605 "Client failed to stop the stream: %d\n", ret); 1606 else 1607 /* Do the crop, if it fails, there's nothing more we can do */ 1608 sh_mobile_ceu_set_crop(icd, a); 1609 1610 dev_geo(icd->parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height); 1611 1612 if (icd->user_width != out_width || icd->user_height != out_height) { 1613 struct v4l2_format f = { 1614 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE, 1615 .fmt.pix = { 1616 .width = out_width, 1617 .height = out_height, 1618 .pixelformat = icd->current_fmt->host_fmt->fourcc, 1619 .field = pcdev->field, 1620 .colorspace = icd->colorspace, 1621 }, 1622 }; 1623 ret = sh_mobile_ceu_set_fmt(icd, &f); 1624 if (!ret && (out_width != f.fmt.pix.width || 1625 out_height != f.fmt.pix.height)) 1626 ret = -EINVAL; 1627 if (!ret) { 1628 icd->user_width = out_width & ~3; 1629 icd->user_height = out_height & ~3; 1630 ret = sh_mobile_ceu_set_bus_param(icd); 1631 } 1632 } 1633 1634 /* Thaw the queue */ 1635 pcdev->frozen = 0; 1636 spin_lock_irq(&pcdev->lock); 1637 sh_mobile_ceu_capture(pcdev); 1638 spin_unlock_irq(&pcdev->lock); 1639 /* Start the client */ 1640 ret = v4l2_subdev_call(sd, video, s_stream, 1); 1641 return ret; 1642} 1643 1644static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt) 1645{ 1646 struct soc_camera_device *icd = file->private_data; 1647 1648 return vb2_poll(&icd->vb2_vidq, file, pt); 1649} 1650 1651static int sh_mobile_ceu_querycap(struct soc_camera_host *ici, 1652 struct v4l2_capability *cap) 1653{ 1654 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card)); 1655 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; 1656 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 1657 1658 return 0; 1659} 1660 1661static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q, 1662 struct soc_camera_device *icd) 1663{ 1664 struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1665 1666 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1667 q->io_modes = VB2_MMAP | VB2_USERPTR; 1668 q->drv_priv = icd; 1669 q->ops = &sh_mobile_ceu_videobuf_ops; 1670 q->mem_ops = &vb2_dma_contig_memops; 1671 q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer); 1672 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1673 q->lock = &ici->host_lock; 1674 1675 return vb2_queue_init(q); 1676} 1677 1678static struct soc_camera_host_ops sh_mobile_ceu_host_ops = { 1679 .owner = THIS_MODULE, 1680 .add = sh_mobile_ceu_add_device, 1681 .remove = sh_mobile_ceu_remove_device, 1682 .clock_start = sh_mobile_ceu_clock_start, 1683 .clock_stop = sh_mobile_ceu_clock_stop, 1684 .get_formats = sh_mobile_ceu_get_formats, 1685 .put_formats = sh_mobile_ceu_put_formats, 1686 .get_crop = sh_mobile_ceu_get_crop, 1687 .set_crop = sh_mobile_ceu_set_crop, 1688 .set_livecrop = sh_mobile_ceu_set_livecrop, 1689 .set_fmt = sh_mobile_ceu_set_fmt, 1690 .try_fmt = sh_mobile_ceu_try_fmt, 1691 .poll = sh_mobile_ceu_poll, 1692 .querycap = sh_mobile_ceu_querycap, 1693 .set_bus_param = sh_mobile_ceu_set_bus_param, 1694 .init_videobuf2 = sh_mobile_ceu_init_videobuf, 1695}; 1696 1697struct bus_wait { 1698 struct notifier_block notifier; 1699 struct completion completion; 1700 struct device *dev; 1701}; 1702 1703static int bus_notify(struct notifier_block *nb, 1704 unsigned long action, void *data) 1705{ 1706 struct device *dev = data; 1707 struct bus_wait *wait = container_of(nb, struct bus_wait, notifier); 1708 1709 if (wait->dev != dev) 1710 return NOTIFY_DONE; 1711 1712 switch (action) { 1713 case BUS_NOTIFY_UNBOUND_DRIVER: 1714 /* Protect from module unloading */ 1715 wait_for_completion(&wait->completion); 1716 return NOTIFY_OK; 1717 } 1718 return NOTIFY_DONE; 1719} 1720 1721static int sh_mobile_ceu_probe(struct platform_device *pdev) 1722{ 1723 struct sh_mobile_ceu_dev *pcdev; 1724 struct resource *res; 1725 void __iomem *base; 1726 unsigned int irq; 1727 int err, i; 1728 struct bus_wait wait = { 1729 .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion), 1730 .notifier.notifier_call = bus_notify, 1731 }; 1732 struct sh_mobile_ceu_companion *csi2; 1733 1734 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1735 irq = platform_get_irq(pdev, 0); 1736 if (!res || (int)irq <= 0) { 1737 dev_err(&pdev->dev, "Not enough CEU platform resources.\n"); 1738 return -ENODEV; 1739 } 1740 1741 pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL); 1742 if (!pcdev) { 1743 dev_err(&pdev->dev, "Could not allocate pcdev\n"); 1744 return -ENOMEM; 1745 } 1746 1747 INIT_LIST_HEAD(&pcdev->capture); 1748 spin_lock_init(&pcdev->lock); 1749 init_completion(&pcdev->complete); 1750 1751 pcdev->pdata = pdev->dev.platform_data; 1752 if (!pcdev->pdata && !pdev->dev.of_node) { 1753 dev_err(&pdev->dev, "CEU platform data not set.\n"); 1754 return -EINVAL; 1755 } 1756 1757 /* TODO: implement per-device bus flags */ 1758 if (pcdev->pdata) { 1759 pcdev->max_width = pcdev->pdata->max_width; 1760 pcdev->max_height = pcdev->pdata->max_height; 1761 pcdev->flags = pcdev->pdata->flags; 1762 } 1763 1764 if (!pcdev->max_width) { 1765 unsigned int v; 1766 err = of_property_read_u32(pdev->dev.of_node, "renesas,max-width", &v); 1767 if (!err) 1768 pcdev->max_width = v; 1769 1770 if (!pcdev->max_width) 1771 pcdev->max_width = 2560; 1772 } 1773 if (!pcdev->max_height) { 1774 unsigned int v; 1775 err = of_property_read_u32(pdev->dev.of_node, "renesas,max-height", &v); 1776 if (!err) 1777 pcdev->max_height = v; 1778 1779 if (!pcdev->max_height) 1780 pcdev->max_height = 1920; 1781 } 1782 1783 base = devm_ioremap_resource(&pdev->dev, res); 1784 if (IS_ERR(base)) 1785 return PTR_ERR(base); 1786 1787 pcdev->irq = irq; 1788 pcdev->base = base; 1789 pcdev->video_limit = 0; /* only enabled if second resource exists */ 1790 1791 res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1792 if (res) { 1793 err = dma_declare_coherent_memory(&pdev->dev, res->start, 1794 res->start, 1795 resource_size(res), 1796 DMA_MEMORY_MAP | 1797 DMA_MEMORY_EXCLUSIVE); 1798 if (!err) { 1799 dev_err(&pdev->dev, "Unable to declare CEU memory.\n"); 1800 return -ENXIO; 1801 } 1802 1803 pcdev->video_limit = resource_size(res); 1804 } 1805 1806 /* request irq */ 1807 err = devm_request_irq(&pdev->dev, pcdev->irq, sh_mobile_ceu_irq, 1808 0, dev_name(&pdev->dev), pcdev); 1809 if (err) { 1810 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n"); 1811 goto exit_release_mem; 1812 } 1813 1814 pm_suspend_ignore_children(&pdev->dev, true); 1815 pm_runtime_enable(&pdev->dev); 1816 pm_runtime_resume(&pdev->dev); 1817 1818 pcdev->ici.priv = pcdev; 1819 pcdev->ici.v4l2_dev.dev = &pdev->dev; 1820 pcdev->ici.nr = pdev->id; 1821 pcdev->ici.drv_name = dev_name(&pdev->dev); 1822 pcdev->ici.ops = &sh_mobile_ceu_host_ops; 1823 pcdev->ici.capabilities = SOCAM_HOST_CAP_STRIDE; 1824 1825 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); 1826 if (IS_ERR(pcdev->alloc_ctx)) { 1827 err = PTR_ERR(pcdev->alloc_ctx); 1828 goto exit_free_clk; 1829 } 1830 1831 if (pcdev->pdata && pcdev->pdata->asd_sizes) { 1832 struct v4l2_async_subdev **asd; 1833 char name[] = "sh-mobile-csi2"; 1834 int j; 1835 1836 /* 1837 * CSI2 interfacing: several groups can use CSI2, pick up the 1838 * first one 1839 */ 1840 asd = pcdev->pdata->asd; 1841 for (j = 0; pcdev->pdata->asd_sizes[j]; j++) { 1842 for (i = 0; i < pcdev->pdata->asd_sizes[j]; i++, asd++) { 1843 dev_dbg(&pdev->dev, "%s(): subdev #%d, type %u\n", 1844 __func__, i, (*asd)->match_type); 1845 if ((*asd)->match_type == V4L2_ASYNC_MATCH_DEVNAME && 1846 !strncmp(name, (*asd)->match.device_name.name, 1847 sizeof(name) - 1)) { 1848 pcdev->csi2_asd = *asd; 1849 break; 1850 } 1851 } 1852 if (pcdev->csi2_asd) 1853 break; 1854 } 1855 1856 pcdev->ici.asd = pcdev->pdata->asd; 1857 pcdev->ici.asd_sizes = pcdev->pdata->asd_sizes; 1858 } 1859 1860 /* Legacy CSI2 interfacing */ 1861 csi2 = pcdev->pdata ? pcdev->pdata->csi2 : NULL; 1862 if (csi2) { 1863 /* 1864 * TODO: remove this once all users are converted to 1865 * asynchronous CSI2 probing. If it has to be kept, csi2 1866 * platform device resources have to be added, using 1867 * platform_device_add_resources() 1868 */ 1869 struct platform_device *csi2_pdev = 1870 platform_device_alloc("sh-mobile-csi2", csi2->id); 1871 struct sh_csi2_pdata *csi2_pdata = csi2->platform_data; 1872 1873 if (!csi2_pdev) { 1874 err = -ENOMEM; 1875 goto exit_free_ctx; 1876 } 1877 1878 pcdev->csi2_pdev = csi2_pdev; 1879 1880 err = platform_device_add_data(csi2_pdev, csi2_pdata, 1881 sizeof(*csi2_pdata)); 1882 if (err < 0) 1883 goto exit_pdev_put; 1884 1885 csi2_pdev->resource = csi2->resource; 1886 csi2_pdev->num_resources = csi2->num_resources; 1887 1888 err = platform_device_add(csi2_pdev); 1889 if (err < 0) 1890 goto exit_pdev_put; 1891 1892 wait.dev = &csi2_pdev->dev; 1893 1894 err = bus_register_notifier(&platform_bus_type, &wait.notifier); 1895 if (err < 0) 1896 goto exit_pdev_unregister; 1897 1898 /* 1899 * From this point the driver module will not unload, until 1900 * we complete the completion. 1901 */ 1902 1903 if (!csi2_pdev->dev.driver) { 1904 complete(&wait.completion); 1905 /* Either too late, or probing failed */ 1906 bus_unregister_notifier(&platform_bus_type, &wait.notifier); 1907 err = -ENXIO; 1908 goto exit_pdev_unregister; 1909 } 1910 1911 /* 1912 * The module is still loaded, in the worst case it is hanging 1913 * in device release on our completion. So, _now_ dereferencing 1914 * the "owner" is safe! 1915 */ 1916 1917 err = try_module_get(csi2_pdev->dev.driver->owner); 1918 1919 /* Let notifier complete, if it has been locked */ 1920 complete(&wait.completion); 1921 bus_unregister_notifier(&platform_bus_type, &wait.notifier); 1922 if (!err) { 1923 err = -ENODEV; 1924 goto exit_pdev_unregister; 1925 } 1926 1927 pcdev->csi2_sd = platform_get_drvdata(csi2_pdev); 1928 } 1929 1930 err = soc_camera_host_register(&pcdev->ici); 1931 if (err) 1932 goto exit_csi2_unregister; 1933 1934 if (csi2) { 1935 err = v4l2_device_register_subdev(&pcdev->ici.v4l2_dev, 1936 pcdev->csi2_sd); 1937 dev_dbg(&pdev->dev, "%s(): ret(register_subdev) = %d\n", 1938 __func__, err); 1939 if (err < 0) 1940 goto exit_host_unregister; 1941 /* v4l2_device_register_subdev() took a reference too */ 1942 module_put(pcdev->csi2_sd->owner); 1943 } 1944 1945 return 0; 1946 1947exit_host_unregister: 1948 soc_camera_host_unregister(&pcdev->ici); 1949exit_csi2_unregister: 1950 if (csi2) { 1951 module_put(pcdev->csi2_pdev->dev.driver->owner); 1952exit_pdev_unregister: 1953 platform_device_del(pcdev->csi2_pdev); 1954exit_pdev_put: 1955 pcdev->csi2_pdev->resource = NULL; 1956 platform_device_put(pcdev->csi2_pdev); 1957 } 1958exit_free_ctx: 1959 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx); 1960exit_free_clk: 1961 pm_runtime_disable(&pdev->dev); 1962exit_release_mem: 1963 if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) 1964 dma_release_declared_memory(&pdev->dev); 1965 return err; 1966} 1967 1968static int sh_mobile_ceu_remove(struct platform_device *pdev) 1969{ 1970 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); 1971 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host, 1972 struct sh_mobile_ceu_dev, ici); 1973 struct platform_device *csi2_pdev = pcdev->csi2_pdev; 1974 1975 soc_camera_host_unregister(soc_host); 1976 pm_runtime_disable(&pdev->dev); 1977 if (platform_get_resource(pdev, IORESOURCE_MEM, 1)) 1978 dma_release_declared_memory(&pdev->dev); 1979 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx); 1980 if (csi2_pdev && csi2_pdev->dev.driver) { 1981 struct module *csi2_drv = csi2_pdev->dev.driver->owner; 1982 platform_device_del(csi2_pdev); 1983 csi2_pdev->resource = NULL; 1984 platform_device_put(csi2_pdev); 1985 module_put(csi2_drv); 1986 } 1987 1988 return 0; 1989} 1990 1991static int sh_mobile_ceu_runtime_nop(struct device *dev) 1992{ 1993 /* Runtime PM callback shared between ->runtime_suspend() 1994 * and ->runtime_resume(). Simply returns success. 1995 * 1996 * This driver re-initializes all registers after 1997 * pm_runtime_get_sync() anyway so there is no need 1998 * to save and restore registers here. 1999 */ 2000 return 0; 2001} 2002 2003static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = { 2004 .runtime_suspend = sh_mobile_ceu_runtime_nop, 2005 .runtime_resume = sh_mobile_ceu_runtime_nop, 2006}; 2007 2008static const struct of_device_id sh_mobile_ceu_of_match[] = { 2009 { .compatible = "renesas,sh-mobile-ceu" }, 2010 { } 2011}; 2012MODULE_DEVICE_TABLE(of, sh_mobile_ceu_of_match); 2013 2014static struct platform_driver sh_mobile_ceu_driver = { 2015 .driver = { 2016 .name = "sh_mobile_ceu", 2017 .pm = &sh_mobile_ceu_dev_pm_ops, 2018 .of_match_table = sh_mobile_ceu_of_match, 2019 }, 2020 .probe = sh_mobile_ceu_probe, 2021 .remove = sh_mobile_ceu_remove, 2022}; 2023 2024static int __init sh_mobile_ceu_init(void) 2025{ 2026 /* Whatever return code */ 2027 request_module("sh_mobile_csi2"); 2028 return platform_driver_register(&sh_mobile_ceu_driver); 2029} 2030 2031static void __exit sh_mobile_ceu_exit(void) 2032{ 2033 platform_driver_unregister(&sh_mobile_ceu_driver); 2034} 2035 2036module_init(sh_mobile_ceu_init); 2037module_exit(sh_mobile_ceu_exit); 2038 2039MODULE_DESCRIPTION("SuperH Mobile CEU driver"); 2040MODULE_AUTHOR("Magnus Damm"); 2041MODULE_LICENSE("GPL"); 2042MODULE_VERSION("0.1.0"); 2043MODULE_ALIAS("platform:sh_mobile_ceu"); 2044