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