sh_mobile_ceu_camera: fix compile breakage, caused by a bad merge
[linux-block.git] / drivers / media / video / sh_mobile_ceu_camera.c
CommitLineData
0d3244d6
MD
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/delay.h>
21#include <linux/dma-mapping.h>
22#include <linux/errno.h>
23#include <linux/fs.h>
24#include <linux/interrupt.h>
25#include <linux/kernel.h>
26#include <linux/mm.h>
27#include <linux/moduleparam.h>
28#include <linux/time.h>
29#include <linux/version.h>
30#include <linux/device.h>
31#include <linux/platform_device.h>
0d3244d6 32#include <linux/videodev2.h>
6d1386c6 33#include <linux/pm_runtime.h>
0d3244d6
MD
34
35#include <media/v4l2-common.h>
36#include <media/v4l2-dev.h>
37#include <media/soc_camera.h>
38#include <media/sh_mobile_ceu.h>
39#include <media/videobuf-dma-contig.h>
40
41/* register offsets for sh7722 / sh7723 */
42
dd54203b
MD
43#define CAPSR 0x00 /* Capture start register */
44#define CAPCR 0x04 /* Capture control register */
45#define CAMCR 0x08 /* Capture interface control register */
46#define CMCYR 0x0c /* Capture interface cycle register */
47#define CAMOR 0x10 /* Capture interface offset register */
48#define CAPWR 0x14 /* Capture interface width register */
49#define CAIFR 0x18 /* Capture interface input format register */
50#define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
51#define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
52#define CRCNTR 0x28 /* CEU register control register */
53#define CRCMPR 0x2c /* CEU register forcible control register */
54#define CFLCR 0x30 /* Capture filter control register */
55#define CFSZR 0x34 /* Capture filter size clip register */
56#define CDWDR 0x38 /* Capture destination width register */
57#define CDAYR 0x3c /* Capture data address Y register */
58#define CDACR 0x40 /* Capture data address C register */
59#define CDBYR 0x44 /* Capture data bottom-field address Y register */
60#define CDBCR 0x48 /* Capture data bottom-field address C register */
61#define CBDSR 0x4c /* Capture bundle destination size register */
62#define CFWCR 0x5c /* Firewall operation control register */
63#define CLFCR 0x60 /* Capture low-pass filter control register */
64#define CDOCR 0x64 /* Capture data output control register */
65#define CDDCR 0x68 /* Capture data complexity level register */
66#define CDDAR 0x6c /* Capture data complexity level address register */
67#define CEIER 0x70 /* Capture event interrupt enable register */
68#define CETCR 0x74 /* Capture event flag clear register */
69#define CSTSR 0x7c /* Capture status register */
70#define CSRTR 0x80 /* Capture software reset register */
71#define CDSSR 0x84 /* Capture data size register */
72#define CDAYR2 0x90 /* Capture data address Y register 2 */
73#define CDACR2 0x94 /* Capture data address C register 2 */
74#define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
75#define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
0d3244d6 76
6a6c8786
GL
77#undef DEBUG_GEOMETRY
78#ifdef DEBUG_GEOMETRY
79#define dev_geo dev_info
80#else
81#define dev_geo dev_dbg
82#endif
83
0d3244d6
MD
84/* per video frame buffer */
85struct sh_mobile_ceu_buffer {
86 struct videobuf_buffer vb; /* v4l buffer must be first */
87 const struct soc_camera_data_format *fmt;
88};
89
90struct sh_mobile_ceu_dev {
0d3244d6
MD
91 struct soc_camera_host ici;
92 struct soc_camera_device *icd;
93
94 unsigned int irq;
95 void __iomem *base;
96 unsigned long video_limit;
97
c60f2b5c 98 /* lock used to protect videobuf */
0d3244d6
MD
99 spinlock_t lock;
100 struct list_head capture;
101 struct videobuf_buffer *active;
102
103 struct sh_mobile_ceu_info *pdata;
8be65dc5 104
961801bb
GL
105 u32 cflcr;
106
904078f1
GL
107 unsigned int is_interlaced:1;
108 unsigned int image_mode:1;
109 unsigned int is_16bit:1;
110};
111
112struct sh_mobile_ceu_cam {
6a6c8786
GL
113 struct v4l2_rect ceu_rect;
114 unsigned int cam_width;
115 unsigned int cam_height;
904078f1 116 const struct soc_camera_data_format *extra_fmt;
8be65dc5 117 const struct soc_camera_data_format *camera_fmt;
0d3244d6
MD
118};
119
c354b400
KM
120static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
121{
122 unsigned long flags;
123
124 flags = SOCAM_MASTER |
125 SOCAM_PCLK_SAMPLE_RISING |
126 SOCAM_HSYNC_ACTIVE_HIGH |
127 SOCAM_HSYNC_ACTIVE_LOW |
128 SOCAM_VSYNC_ACTIVE_HIGH |
129 SOCAM_VSYNC_ACTIVE_LOW |
130 SOCAM_DATA_ACTIVE_HIGH;
131
132 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
133 flags |= SOCAM_DATAWIDTH_8;
134
135 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
136 flags |= SOCAM_DATAWIDTH_16;
137
138 if (flags & SOCAM_DATAWIDTH_MASK)
139 return flags;
140
141 return 0;
142}
143
0d3244d6 144static void ceu_write(struct sh_mobile_ceu_dev *priv,
7a1a8164 145 unsigned long reg_offs, u32 data)
0d3244d6
MD
146{
147 iowrite32(data, priv->base + reg_offs);
148}
149
7a1a8164 150static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
0d3244d6
MD
151{
152 return ioread32(priv->base + reg_offs);
153}
154
155/*
156 * Videobuf operations
157 */
158static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
159 unsigned int *count,
160 unsigned int *size)
161{
162 struct soc_camera_device *icd = vq->priv_data;
163 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
164 struct sh_mobile_ceu_dev *pcdev = ici->priv;
165 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
166
6a6c8786 167 *size = PAGE_ALIGN(icd->user_width * icd->user_height *
a0705b07 168 bytes_per_pixel);
0d3244d6
MD
169
170 if (0 == *count)
171 *count = 2;
172
173 if (pcdev->video_limit) {
174 while (*size * *count > pcdev->video_limit)
175 (*count)--;
176 }
177
0166b743 178 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
0d3244d6
MD
179
180 return 0;
181}
182
183static void free_buffer(struct videobuf_queue *vq,
184 struct sh_mobile_ceu_buffer *buf)
185{
186 struct soc_camera_device *icd = vq->priv_data;
6a6c8786 187 struct device *dev = icd->dev.parent;
0d3244d6 188
6a6c8786 189 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
190 &buf->vb, buf->vb.baddr, buf->vb.bsize);
191
192 if (in_interrupt())
193 BUG();
194
97215cbd 195 videobuf_waiton(&buf->vb, 0, 0);
0d3244d6 196 videobuf_dma_contig_free(vq, &buf->vb);
6a6c8786 197 dev_dbg(dev, "%s freed\n", __func__);
0d3244d6
MD
198 buf->vb.state = VIDEOBUF_NEEDS_INIT;
199}
200
7a1a8164
MD
201#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
202#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
203#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
204#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
205
206
0d3244d6
MD
207static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
208{
8be65dc5 209 struct soc_camera_device *icd = pcdev->icd;
ccab8a29 210 dma_addr_t phys_addr_top, phys_addr_bottom;
7a1a8164
MD
211
212 /* The hardware is _very_ picky about this sequence. Especially
213 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
214 * several not-so-well documented interrupt sources in CETCR.
215 */
216 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
217 ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
218 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
219 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
220 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
0d3244d6 221
8be65dc5
MD
222 if (!pcdev->active)
223 return;
224
ccab8a29
KM
225 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
226 ceu_write(pcdev, CDAYR, phys_addr_top);
e802967c 227 if (pcdev->is_interlaced) {
6a6c8786 228 phys_addr_bottom = phys_addr_top + icd->user_width;
ccab8a29
KM
229 ceu_write(pcdev, CDBYR, phys_addr_bottom);
230 }
8be65dc5
MD
231
232 switch (icd->current_fmt->fourcc) {
233 case V4L2_PIX_FMT_NV12:
234 case V4L2_PIX_FMT_NV21:
9e4a56d2
MD
235 case V4L2_PIX_FMT_NV16:
236 case V4L2_PIX_FMT_NV61:
6a6c8786
GL
237 phys_addr_top += icd->user_width *
238 icd->user_height;
ccab8a29 239 ceu_write(pcdev, CDACR, phys_addr_top);
e802967c 240 if (pcdev->is_interlaced) {
a0705b07 241 phys_addr_bottom = phys_addr_top +
6a6c8786 242 icd->user_width;
ccab8a29
KM
243 ceu_write(pcdev, CDBCR, phys_addr_bottom);
244 }
0d3244d6 245 }
8be65dc5
MD
246
247 pcdev->active->state = VIDEOBUF_ACTIVE;
248 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
0d3244d6
MD
249}
250
251static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
252 struct videobuf_buffer *vb,
253 enum v4l2_field field)
254{
255 struct soc_camera_device *icd = vq->priv_data;
256 struct sh_mobile_ceu_buffer *buf;
257 int ret;
258
259 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
260
0166b743 261 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
262 vb, vb->baddr, vb->bsize);
263
264 /* Added list head initialization on alloc */
265 WARN_ON(!list_empty(&vb->queue));
266
267#ifdef DEBUG
268 /* This can be useful if you want to see if we actually fill
269 * the buffer with something */
270 memset((void *)vb->baddr, 0xaa, vb->bsize);
271#endif
272
273 BUG_ON(NULL == icd->current_fmt);
274
275 if (buf->fmt != icd->current_fmt ||
6a6c8786
GL
276 vb->width != icd->user_width ||
277 vb->height != icd->user_height ||
0d3244d6
MD
278 vb->field != field) {
279 buf->fmt = icd->current_fmt;
6a6c8786
GL
280 vb->width = icd->user_width;
281 vb->height = icd->user_height;
0d3244d6
MD
282 vb->field = field;
283 vb->state = VIDEOBUF_NEEDS_INIT;
284 }
285
286 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
287 if (0 != vb->baddr && vb->bsize < vb->size) {
288 ret = -EINVAL;
289 goto out;
290 }
291
292 if (vb->state == VIDEOBUF_NEEDS_INIT) {
293 ret = videobuf_iolock(vq, vb, NULL);
294 if (ret)
295 goto fail;
296 vb->state = VIDEOBUF_PREPARED;
297 }
298
299 return 0;
300fail:
301 free_buffer(vq, buf);
302out:
303 return ret;
304}
305
2dd54a54 306/* Called under spinlock_irqsave(&pcdev->lock, ...) */
0d3244d6
MD
307static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
308 struct videobuf_buffer *vb)
309{
310 struct soc_camera_device *icd = vq->priv_data;
311 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
312 struct sh_mobile_ceu_dev *pcdev = ici->priv;
0d3244d6 313
0166b743 314 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
315 vb, vb->baddr, vb->bsize);
316
51354cc3 317 vb->state = VIDEOBUF_QUEUED;
0d3244d6
MD
318 list_add_tail(&vb->queue, &pcdev->capture);
319
320 if (!pcdev->active) {
321 pcdev->active = vb;
322 sh_mobile_ceu_capture(pcdev);
323 }
0d3244d6
MD
324}
325
326static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
327 struct videobuf_buffer *vb)
328{
cca0e549
GL
329 struct soc_camera_device *icd = vq->priv_data;
330 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
331 struct sh_mobile_ceu_dev *pcdev = ici->priv;
332 unsigned long flags;
333
334 spin_lock_irqsave(&pcdev->lock, flags);
335
336 if (pcdev->active == vb) {
337 /* disable capture (release DMA buffer), reset */
338 ceu_write(pcdev, CAPSR, 1 << 16);
339 pcdev->active = NULL;
340 }
341
342 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
343 !list_empty(&vb->queue)) {
344 vb->state = VIDEOBUF_ERROR;
345 list_del_init(&vb->queue);
346 }
347
348 spin_unlock_irqrestore(&pcdev->lock, flags);
349
0d3244d6
MD
350 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
351}
352
353static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
354 .buf_setup = sh_mobile_ceu_videobuf_setup,
355 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
356 .buf_queue = sh_mobile_ceu_videobuf_queue,
357 .buf_release = sh_mobile_ceu_videobuf_release,
358};
359
360static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
361{
362 struct sh_mobile_ceu_dev *pcdev = data;
363 struct videobuf_buffer *vb;
364 unsigned long flags;
365
366 spin_lock_irqsave(&pcdev->lock, flags);
367
368 vb = pcdev->active;
cca0e549
GL
369 if (!vb)
370 /* Stale interrupt from a released buffer */
371 goto out;
372
0d3244d6
MD
373 list_del_init(&vb->queue);
374
375 if (!list_empty(&pcdev->capture))
376 pcdev->active = list_entry(pcdev->capture.next,
377 struct videobuf_buffer, queue);
378 else
379 pcdev->active = NULL;
380
381 sh_mobile_ceu_capture(pcdev);
382
383 vb->state = VIDEOBUF_DONE;
384 do_gettimeofday(&vb->ts);
385 vb->field_count++;
386 wake_up(&vb->done);
cca0e549
GL
387
388out:
0d3244d6
MD
389 spin_unlock_irqrestore(&pcdev->lock, flags);
390
391 return IRQ_HANDLED;
392}
393
1c3bb743 394/* Called with .video_lock held */
0d3244d6
MD
395static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
396{
397 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
398 struct sh_mobile_ceu_dev *pcdev = ici->priv;
0d3244d6 399
0d3244d6 400 if (pcdev->icd)
979ea1dd 401 return -EBUSY;
0d3244d6 402
0166b743 403 dev_info(icd->dev.parent,
0d3244d6
MD
404 "SuperH Mobile CEU driver attached to camera %d\n",
405 icd->devnum);
406
a79aebfc 407 pm_runtime_get_sync(ici->v4l2_dev.dev);
40e2e092 408
0d3244d6
MD
409 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
410 while (ceu_read(pcdev, CSTSR) & 1)
411 msleep(1);
412
413 pcdev->icd = icd;
979ea1dd
GL
414
415 return 0;
0d3244d6
MD
416}
417
1c3bb743 418/* Called with .video_lock held */
0d3244d6
MD
419static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
420{
421 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
422 struct sh_mobile_ceu_dev *pcdev = ici->priv;
51354cc3 423 unsigned long flags;
0d3244d6
MD
424
425 BUG_ON(icd != pcdev->icd);
426
427 /* disable capture, disable interrupts */
428 ceu_write(pcdev, CEIER, 0);
429 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
51354cc3
MD
430
431 /* make sure active buffer is canceled */
432 spin_lock_irqsave(&pcdev->lock, flags);
433 if (pcdev->active) {
434 list_del(&pcdev->active->queue);
435 pcdev->active->state = VIDEOBUF_ERROR;
436 wake_up_all(&pcdev->active->done);
437 pcdev->active = NULL;
438 }
439 spin_unlock_irqrestore(&pcdev->lock, flags);
440
a79aebfc 441 pm_runtime_put_sync(ici->v4l2_dev.dev);
40e2e092 442
0166b743 443 dev_info(icd->dev.parent,
0d3244d6
MD
444 "SuperH Mobile CEU driver detached from camera %d\n",
445 icd->devnum);
446
447 pcdev->icd = NULL;
448}
449
961801bb
GL
450/*
451 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
452 * in SH7722 Hardware Manual
453 */
454static unsigned int size_dst(unsigned int src, unsigned int scale)
455{
456 unsigned int mant_pre = scale >> 12;
457 if (!src || !scale)
458 return src;
459 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
460 mant_pre * 4096 / scale + 1;
461}
462
961801bb
GL
463static u16 calc_scale(unsigned int src, unsigned int *dst)
464{
465 u16 scale;
466
467 if (src == *dst)
468 return 0;
469
470 scale = (src * 4096 / *dst) & ~7;
471
472 while (scale > 4096 && size_dst(src, scale) < *dst)
473 scale -= 8;
474
475 *dst = size_dst(src, scale);
476
477 return scale;
478}
479
480/* rect is guaranteed to not exceed the scaled camera rectangle */
904078f1 481static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
6a6c8786
GL
482 unsigned int out_width,
483 unsigned int out_height)
904078f1
GL
484{
485 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
486 struct sh_mobile_ceu_cam *cam = icd->host_priv;
6a6c8786 487 struct v4l2_rect *rect = &cam->ceu_rect;
904078f1 488 struct sh_mobile_ceu_dev *pcdev = ici->priv;
6a6c8786
GL
489 unsigned int height, width, cdwdr_width, in_width, in_height;
490 unsigned int left_offset, top_offset;
904078f1
GL
491 u32 camor;
492
6a6c8786
GL
493 dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
494 rect->width, rect->height, rect->left, rect->top);
904078f1 495
6a6c8786
GL
496 left_offset = rect->left;
497 top_offset = rect->top;
904078f1
GL
498
499 if (pcdev->image_mode) {
6a6c8786 500 in_width = rect->width;
961801bb 501 if (!pcdev->is_16bit) {
961801bb
GL
502 in_width *= 2;
503 left_offset *= 2;
504 }
6a6c8786 505 width = cdwdr_width = out_width;
904078f1 506 } else {
961801bb 507 unsigned int w_factor = (icd->current_fmt->depth + 7) >> 3;
6a6c8786
GL
508
509 width = out_width * w_factor / 2;
510
961801bb
GL
511 if (!pcdev->is_16bit)
512 w_factor *= 2;
513
6a6c8786 514 in_width = rect->width * w_factor / 2;
961801bb
GL
515 left_offset = left_offset * w_factor / 2;
516
6a6c8786 517 cdwdr_width = width * 2;
904078f1
GL
518 }
519
6a6c8786
GL
520 height = out_height;
521 in_height = rect->height;
904078f1
GL
522 if (pcdev->is_interlaced) {
523 height /= 2;
961801bb
GL
524 in_height /= 2;
525 top_offset /= 2;
904078f1
GL
526 cdwdr_width *= 2;
527 }
528
6a6c8786 529 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
904078f1 530 camor = left_offset | (top_offset << 16);
6a6c8786
GL
531
532 dev_geo(icd->dev.parent,
533 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
534 (in_height << 16) | in_width, (height << 16) | width,
535 cdwdr_width);
536
904078f1 537 ceu_write(pcdev, CAMOR, camor);
961801bb 538 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
6a6c8786 539 ceu_write(pcdev, CFSZR, (height << 16) | width);
904078f1
GL
540 ceu_write(pcdev, CDWDR, cdwdr_width);
541}
542
543static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
544{
545 u32 capsr = ceu_read(pcdev, CAPSR);
546 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
547 return capsr;
548}
549
550static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
551{
552 unsigned long timeout = jiffies + 10 * HZ;
553
554 /*
555 * Wait until the end of the current frame. It can take a long time,
556 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
557 */
558 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
559 msleep(1);
560
561 if (time_after(jiffies, timeout)) {
562 dev_err(pcdev->ici.v4l2_dev.dev,
563 "Timeout waiting for frame end! Interface problem?\n");
564 return;
565 }
566
567 /* Wait until reset clears, this shall not hang... */
568 while (ceu_read(pcdev, CAPSR) & (1 << 16))
569 udelay(10);
570
571 /* Anything to restore? */
572 if (capsr & ~(1 << 16))
573 ceu_write(pcdev, CAPSR, capsr);
574}
575
0d3244d6
MD
576static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
577 __u32 pixfmt)
578{
579 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
580 struct sh_mobile_ceu_dev *pcdev = ici->priv;
904078f1 581 int ret;
0d3244d6 582 unsigned long camera_flags, common_flags, value;
904078f1
GL
583 int yuv_lineskip;
584 struct sh_mobile_ceu_cam *cam = icd->host_priv;
585 u32 capsr = capture_save_reset(pcdev);
0d3244d6
MD
586
587 camera_flags = icd->ops->query_bus_param(icd);
588 common_flags = soc_camera_bus_param_compatible(camera_flags,
c354b400 589 make_bus_param(pcdev));
0d3244d6
MD
590 if (!common_flags)
591 return -EINVAL;
592
593 ret = icd->ops->set_bus_param(icd, common_flags);
594 if (ret < 0)
595 return ret;
596
597 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
598 case SOCAM_DATAWIDTH_8:
904078f1 599 pcdev->is_16bit = 0;
0d3244d6
MD
600 break;
601 case SOCAM_DATAWIDTH_16:
904078f1 602 pcdev->is_16bit = 1;
0d3244d6
MD
603 break;
604 default:
605 return -EINVAL;
606 }
607
608 ceu_write(pcdev, CRCNTR, 0);
609 ceu_write(pcdev, CRCMPR, 0);
610
8be65dc5 611 value = 0x00000010; /* data fetch by default */
961801bb 612 yuv_lineskip = 0;
8be65dc5
MD
613
614 switch (icd->current_fmt->fourcc) {
615 case V4L2_PIX_FMT_NV12:
616 case V4L2_PIX_FMT_NV21:
617 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
9e4a56d2
MD
618 /* fall-through */
619 case V4L2_PIX_FMT_NV16:
620 case V4L2_PIX_FMT_NV61:
904078f1 621 switch (cam->camera_fmt->fourcc) {
8be65dc5
MD
622 case V4L2_PIX_FMT_UYVY:
623 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
624 break;
625 case V4L2_PIX_FMT_VYUY:
626 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
627 break;
628 case V4L2_PIX_FMT_YUYV:
629 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
630 break;
631 case V4L2_PIX_FMT_YVYU:
632 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
633 break;
634 default:
635 BUG();
636 }
637 }
638
7a1a8164
MD
639 if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
640 icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
9e4a56d2 641 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
8be65dc5 642
7a1a8164
MD
643 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
644 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
904078f1 645 value |= pcdev->is_16bit ? 1 << 12 : 0;
0d3244d6
MD
646 ceu_write(pcdev, CAMCR, value);
647
648 ceu_write(pcdev, CAPCR, 0x00300000);
e802967c 649 ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
0d3244d6 650
6a6c8786 651 sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
0d3244d6
MD
652 mdelay(1);
653
961801bb 654 ceu_write(pcdev, CFLCR, pcdev->cflcr);
dd54203b
MD
655
656 /* A few words about byte order (observed in Big Endian mode)
657 *
658 * In data fetch mode bytes are received in chunks of 8 bytes.
659 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
660 *
661 * The data is however by default written to memory in reverse order:
662 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
663 *
664 * The lowest three bits of CDOCR allows us to do swapping,
2c0a072e
MD
665 * using 7 we swap the data bytes to match the incoming order:
666 * D0, D1, D2, D3, D4, D5, D6, D7
dd54203b 667 */
8be65dc5
MD
668 value = 0x00000017;
669 if (yuv_lineskip)
670 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
671
672 ceu_write(pcdev, CDOCR, value);
0d3244d6
MD
673 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
674
6a6c8786 675 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
904078f1
GL
676 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
677 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
6a6c8786 678 icd->user_width, icd->user_height);
904078f1
GL
679
680 capture_restore(pcdev, capsr);
681
0d3244d6 682 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
0d3244d6
MD
683 return 0;
684}
685
9414de39 686static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
0d3244d6
MD
687{
688 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
689 struct sh_mobile_ceu_dev *pcdev = ici->priv;
690 unsigned long camera_flags, common_flags;
691
692 camera_flags = icd->ops->query_bus_param(icd);
693 common_flags = soc_camera_bus_param_compatible(camera_flags,
c354b400 694 make_bus_param(pcdev));
0d3244d6
MD
695 if (!common_flags)
696 return -EINVAL;
697
698 return 0;
699}
700
8be65dc5
MD
701static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
702 {
703 .name = "NV12",
704 .depth = 12,
705 .fourcc = V4L2_PIX_FMT_NV12,
706 .colorspace = V4L2_COLORSPACE_JPEG,
707 },
708 {
709 .name = "NV21",
710 .depth = 12,
711 .fourcc = V4L2_PIX_FMT_NV21,
712 .colorspace = V4L2_COLORSPACE_JPEG,
713 },
9e4a56d2
MD
714 {
715 .name = "NV16",
716 .depth = 16,
717 .fourcc = V4L2_PIX_FMT_NV16,
718 .colorspace = V4L2_COLORSPACE_JPEG,
719 },
720 {
721 .name = "NV61",
722 .depth = 16,
723 .fourcc = V4L2_PIX_FMT_NV61,
724 .colorspace = V4L2_COLORSPACE_JPEG,
725 },
8be65dc5
MD
726};
727
9414de39
MD
728static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
729 struct soc_camera_format_xlate *xlate)
730{
6a6c8786 731 struct device *dev = icd->dev.parent;
8be65dc5 732 int ret, k, n;
9414de39 733 int formats = 0;
904078f1 734 struct sh_mobile_ceu_cam *cam;
9414de39
MD
735
736 ret = sh_mobile_ceu_try_bus_param(icd);
737 if (ret < 0)
738 return 0;
739
904078f1
GL
740 if (!icd->host_priv) {
741 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
742 if (!cam)
743 return -ENOMEM;
744
745 icd->host_priv = cam;
746 } else {
747 cam = icd->host_priv;
748 }
749
c8329acc
GL
750 /* Beginning of a pass */
751 if (!idx)
904078f1 752 cam->extra_fmt = NULL;
c8329acc 753
9414de39 754 switch (icd->formats[idx].fourcc) {
8be65dc5
MD
755 case V4L2_PIX_FMT_UYVY:
756 case V4L2_PIX_FMT_VYUY:
757 case V4L2_PIX_FMT_YUYV:
758 case V4L2_PIX_FMT_YVYU:
904078f1 759 if (cam->extra_fmt)
c8329acc
GL
760 goto add_single_format;
761
762 /*
763 * Our case is simple so far: for any of the above four camera
764 * formats we add all our four synthesized NV* formats, so,
765 * just marking the device with a single flag suffices. If
766 * the format generation rules are more complex, you would have
767 * to actually hang your already added / counted formats onto
768 * the host_priv pointer and check whether the format you're
769 * going to add now is already there.
770 */
904078f1 771 cam->extra_fmt = (void *)sh_mobile_ceu_formats;
c8329acc 772
8be65dc5
MD
773 n = ARRAY_SIZE(sh_mobile_ceu_formats);
774 formats += n;
775 for (k = 0; xlate && k < n; k++) {
776 xlate->host_fmt = &sh_mobile_ceu_formats[k];
777 xlate->cam_fmt = icd->formats + idx;
778 xlate->buswidth = icd->formats[idx].depth;
779 xlate++;
6a6c8786 780 dev_dbg(dev, "Providing format %s using %s\n",
8be65dc5
MD
781 sh_mobile_ceu_formats[k].name,
782 icd->formats[idx].name);
783 }
9414de39 784 default:
c8329acc 785add_single_format:
9414de39
MD
786 /* Generic pass-through */
787 formats++;
788 if (xlate) {
789 xlate->host_fmt = icd->formats + idx;
790 xlate->cam_fmt = icd->formats + idx;
791 xlate->buswidth = icd->formats[idx].depth;
792 xlate++;
6a6c8786 793 dev_dbg(dev,
9414de39
MD
794 "Providing format %s in pass-through mode\n",
795 icd->formats[idx].name);
796 }
797 }
798
799 return formats;
800}
801
904078f1
GL
802static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
803{
804 kfree(icd->host_priv);
805 icd->host_priv = NULL;
806}
807
961801bb 808/* Check if any dimension of r1 is smaller than respective one of r2 */
904078f1
GL
809static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
810{
811 return r1->width < r2->width || r1->height < r2->height;
812}
813
961801bb
GL
814/* Check if r1 fails to cover r2 */
815static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
816{
817 return r1->left > r2->left || r1->top > r2->top ||
818 r1->left + r1->width < r2->left + r2->width ||
819 r1->top + r1->height < r2->top + r2->height;
820}
821
6a6c8786
GL
822static unsigned int scale_down(unsigned int size, unsigned int scale)
823{
824 return (size * 4096 + scale / 2) / scale;
825}
826
827static unsigned int scale_up(unsigned int size, unsigned int scale)
828{
829 return (size * scale + 2048) / 4096;
830}
831
832static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
833{
834 return (input * 4096 + output / 2) / output;
835}
836
837static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
838{
839 struct v4l2_crop crop;
840 struct v4l2_cropcap cap;
841 int ret;
842
843 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
844
845 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
846 if (!ret) {
847 *rect = crop.c;
848 return ret;
849 }
850
851 /* Camera driver doesn't support .g_crop(), assume default rectangle */
852 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
853
854 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
855 if (ret < 0)
856 return ret;
857
858 *rect = cap.defrect;
859
860 return ret;
861}
862
961801bb 863/*
6a6c8786 864 * The common for both scaling and cropping iterative approach is:
961801bb
GL
865 * 1. try if the client can produce exactly what requested by the user
866 * 2. if (1) failed, try to double the client image until we get one big enough
867 * 3. if (2) failed, try to request the maximum image
868 */
6a6c8786
GL
869static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
870 struct v4l2_crop *cam_crop)
09e231b3 871{
6a6c8786
GL
872 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
873 struct device *dev = sd->v4l2_dev->dev;
874 struct v4l2_cropcap cap;
904078f1 875 int ret;
6a6c8786 876 unsigned int width, height;
904078f1 877
6a6c8786
GL
878 v4l2_subdev_call(sd, video, s_crop, crop);
879 ret = client_g_rect(sd, cam_rect);
880 if (ret < 0)
881 return ret;
961801bb 882
6a6c8786
GL
883 /*
884 * Now cam_crop contains the current camera input rectangle, and it must
885 * be within camera cropcap bounds
886 */
887 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
888 /* Even if camera S_CROP failed, but camera rectangle matches */
889 dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n",
890 rect->width, rect->height, rect->left, rect->top);
891 return 0;
904078f1
GL
892 }
893
6a6c8786
GL
894 /* Try to fix cropping, that camera hasn't managed to set */
895 dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
896 cam_rect->width, cam_rect->height,
08590b96 897 cam_rect->left, cam_rect->top,
6a6c8786
GL
898 rect->width, rect->height, rect->left, rect->top);
899
900 /* We need sensor maximum rectangle */
901 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
902 if (ret < 0)
903 return ret;
904
905 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
906 cap.bounds.width);
907 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
908 cap.bounds.height);
904078f1
GL
909
910 /*
911 * Popular special case - some cameras can only handle fixed sizes like
912 * QVGA, VGA,... Take care to avoid infinite loop.
913 */
6a6c8786
GL
914 width = max(cam_rect->width, 2);
915 height = max(cam_rect->height, 2);
916
917 while (!ret && (is_smaller(cam_rect, rect) ||
918 is_inside(cam_rect, rect)) &&
919 (cap.bounds.width > width || cap.bounds.height > height)) {
961801bb
GL
920
921 width *= 2;
922 height *= 2;
6a6c8786 923
08590b96
GL
924 cam_rect->width = width;
925 cam_rect->height = height;
904078f1 926
6a6c8786
GL
927 /*
928 * We do not know what capabilities the camera has to set up
929 * left and top borders. We could try to be smarter in iterating
930 * them, e.g., if camera current left is to the right of the
931 * target left, set it to the middle point between the current
932 * left and minimum left. But that would add too much
933 * complexity: we would have to iterate each border separately.
934 */
935 if (cam_rect->left > rect->left)
936 cam_rect->left = cap.bounds.left;
961801bb 937
6a6c8786
GL
938 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
939 cam_rect->width = rect->left + rect->width -
08590b96 940 cam_rect->left;
961801bb 941
6a6c8786
GL
942 if (cam_rect->top > rect->top)
943 cam_rect->top = cap.bounds.top;
961801bb 944
6a6c8786
GL
945 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
946 cam_rect->height = rect->top + rect->height -
08590b96 947 cam_rect->top;
961801bb 948
6a6c8786
GL
949 v4l2_subdev_call(sd, video, s_crop, cam_crop);
950 ret = client_g_rect(sd, cam_rect);
951 dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret,
952 cam_rect->width, cam_rect->height,
08590b96 953 cam_rect->left, cam_rect->top);
904078f1
GL
954 }
955
6a6c8786
GL
956 /* S_CROP must not modify the rectangle */
957 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
904078f1
GL
958 /*
959 * The camera failed to configure a suitable cropping,
960 * we cannot use the current rectangle, set to max
961 */
6a6c8786
GL
962 *cam_rect = cap.bounds;
963 v4l2_subdev_call(sd, video, s_crop, cam_crop);
964 ret = client_g_rect(sd, cam_rect);
965 dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret,
966 cam_rect->width, cam_rect->height,
08590b96 967 cam_rect->left, cam_rect->top);
904078f1
GL
968 }
969
6a6c8786
GL
970 return ret;
971}
972
973static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
974 unsigned int *scale_h, unsigned int *scale_v)
975{
976 struct v4l2_format f;
977 int ret;
978
979 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
980
981 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
982 if (ret < 0)
983 return ret;
984
985 *scale_h = calc_generic_scale(rect->width, f.fmt.pix.width);
986 *scale_v = calc_generic_scale(rect->height, f.fmt.pix.height);
987
988 return 0;
989}
990
991static int get_camera_subwin(struct soc_camera_device *icd,
992 struct v4l2_rect *cam_subrect,
993 unsigned int cam_hscale, unsigned int cam_vscale)
994{
995 struct sh_mobile_ceu_cam *cam = icd->host_priv;
996 struct v4l2_rect *ceu_rect = &cam->ceu_rect;
997
998 if (!ceu_rect->width) {
999 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1000 struct device *dev = icd->dev.parent;
1001 struct v4l2_format f;
1002 struct v4l2_pix_format *pix = &f.fmt.pix;
1003 int ret;
1004 /* First time */
1005
1006 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1007
1008 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
1009 if (ret < 0)
1010 return ret;
1011
1012 dev_geo(dev, "camera fmt %ux%u\n", pix->width, pix->height);
1013
1014 if (pix->width > 2560) {
1015 ceu_rect->width = 2560;
1016 ceu_rect->left = (pix->width - 2560) / 2;
1017 } else {
1018 ceu_rect->width = pix->width;
1019 ceu_rect->left = 0;
1020 }
1021
1022 if (pix->height > 1920) {
1023 ceu_rect->height = 1920;
1024 ceu_rect->top = (pix->height - 1920) / 2;
1025 } else {
1026 ceu_rect->height = pix->height;
1027 ceu_rect->top = 0;
1028 }
1029
1030 dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
1031 ceu_rect->width, ceu_rect->height,
1032 ceu_rect->left, ceu_rect->top);
1033 }
1034
1035 cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
1036 cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
1037 cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
1038 cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
1039
1040 return 0;
1041}
1042
1043static int client_s_fmt(struct soc_camera_device *icd, struct v4l2_format *f,
1044 bool ceu_can_scale)
1045{
1046 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1047 struct device *dev = icd->dev.parent;
1048 struct v4l2_pix_format *pix = &f->fmt.pix;
1049 unsigned int width = pix->width, height = pix->height, tmp_w, tmp_h;
1050 unsigned int max_width, max_height;
1051 struct v4l2_cropcap cap;
1052 int ret;
1053
1054 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1055
1056 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1057 if (ret < 0)
1058 return ret;
1059
1060 max_width = min(cap.bounds.width, 2560);
1061 max_height = min(cap.bounds.height, 1920);
1062
1063 ret = v4l2_subdev_call(sd, video, s_fmt, f);
1064 if (ret < 0)
1065 return ret;
1066
1067 dev_geo(dev, "camera scaled to %ux%u\n", pix->width, pix->height);
1068
1069 if ((width == pix->width && height == pix->height) || !ceu_can_scale)
1070 return 0;
1071
1072 /* Camera set a format, but geometry is not precise, try to improve */
1073 tmp_w = pix->width;
1074 tmp_h = pix->height;
1075
1076 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1077 while ((width > tmp_w || height > tmp_h) &&
1078 tmp_w < max_width && tmp_h < max_height) {
1079 tmp_w = min(2 * tmp_w, max_width);
1080 tmp_h = min(2 * tmp_h, max_height);
1081 pix->width = tmp_w;
1082 pix->height = tmp_h;
1083 ret = v4l2_subdev_call(sd, video, s_fmt, f);
1084 dev_geo(dev, "Camera scaled to %ux%u\n",
1085 pix->width, pix->height);
1086 if (ret < 0) {
1087 /* This shouldn't happen */
1088 dev_err(dev, "Client failed to set format: %d\n", ret);
1089 return ret;
1090 }
1091 }
1092
1093 return 0;
1094}
1095
1096/**
1097 * @rect - camera cropped rectangle
1098 * @sub_rect - CEU cropped rectangle, mapped back to camera input area
1099 * @ceu_rect - on output calculated CEU crop rectangle
1100 */
1101static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
1102 struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect,
1103 struct v4l2_format *f, bool ceu_can_scale)
1104{
1105 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1106 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1107 struct device *dev = icd->dev.parent;
1108 struct v4l2_format f_tmp = *f;
1109 struct v4l2_pix_format *pix_tmp = &f_tmp.fmt.pix;
1110 unsigned int scale_h, scale_v;
1111 int ret;
1112
1113 /* 5. Apply iterative camera S_FMT for camera user window. */
1114 ret = client_s_fmt(icd, &f_tmp, ceu_can_scale);
1115 if (ret < 0)
1116 return ret;
1117
1118 dev_geo(dev, "5: camera scaled to %ux%u\n",
1119 pix_tmp->width, pix_tmp->height);
1120
1121 /* 6. Retrieve camera output window (g_fmt) */
1122
1123 /* unneeded - it is already in "f_tmp" */
1124
1125 /* 7. Calculate new camera scales. */
1126 ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
1127 if (ret < 0)
1128 return ret;
1129
1130 dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
1131
1132 cam->cam_width = pix_tmp->width;
1133 cam->cam_height = pix_tmp->height;
1134 f->fmt.pix.width = pix_tmp->width;
1135 f->fmt.pix.height = pix_tmp->height;
904078f1
GL
1136
1137 /*
6a6c8786
GL
1138 * 8. Calculate new CEU crop - apply camera scales to previously
1139 * calculated "effective" crop.
904078f1 1140 */
6a6c8786
GL
1141 ceu_rect->left = scale_down(sub_rect->left, scale_h);
1142 ceu_rect->width = scale_down(sub_rect->width, scale_h);
1143 ceu_rect->top = scale_down(sub_rect->top, scale_v);
1144 ceu_rect->height = scale_down(sub_rect->height, scale_v);
1145
1146 dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
1147 ceu_rect->width, ceu_rect->height,
1148 ceu_rect->left, ceu_rect->top);
1149
1150 return 0;
1151}
1152
1153/* Get combined scales */
1154static int get_scales(struct soc_camera_device *icd,
1155 unsigned int *scale_h, unsigned int *scale_v)
1156{
1157 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1158 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1159 struct v4l2_crop cam_crop;
1160 unsigned int width_in, height_in;
1161 int ret;
904078f1 1162
6a6c8786 1163 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
904078f1 1164
6a6c8786
GL
1165 ret = client_g_rect(sd, &cam_crop.c);
1166 if (ret < 0)
1167 return ret;
904078f1 1168
6a6c8786
GL
1169 ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
1170 if (ret < 0)
1171 return ret;
961801bb 1172
6a6c8786
GL
1173 width_in = scale_up(cam->ceu_rect.width, *scale_h);
1174 height_in = scale_up(cam->ceu_rect.height, *scale_v);
961801bb 1175
6a6c8786
GL
1176 *scale_h = calc_generic_scale(cam->ceu_rect.width, icd->user_width);
1177 *scale_v = calc_generic_scale(cam->ceu_rect.height, icd->user_height);
1178
1179 return 0;
1180}
1181
1182/*
1183 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1184 * framerate by always requesting the maximum image from the client. See
1185 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1186 * scaling and cropping algorithms and for the meaning of referenced here steps.
1187 */
1188static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
1189 struct v4l2_crop *a)
1190{
1191 struct v4l2_rect *rect = &a->c;
1192 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1193 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1194 struct v4l2_crop cam_crop;
1195 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1196 struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
1197 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1198 struct device *dev = icd->dev.parent;
1199 struct v4l2_format f;
1200 struct v4l2_pix_format *pix = &f.fmt.pix;
1201 unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v,
1202 out_width, out_height;
1203 u32 capsr, cflcr;
1204 int ret;
1205
1206 /* 1. Calculate current combined scales. */
1207 ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
1208 if (ret < 0)
1209 return ret;
1210
1211 dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
1212
1213 /* 2. Apply iterative camera S_CROP for new input window. */
1214 ret = client_s_crop(sd, a, &cam_crop);
1215 if (ret < 0)
1216 return ret;
1217
1218 dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n",
1219 cam_rect->width, cam_rect->height,
1220 cam_rect->left, cam_rect->top);
1221
1222 /* On success cam_crop contains current camera crop */
1223
1224 /*
1225 * 3. If old combined scales applied to new crop produce an impossible
1226 * user window, adjust scales to produce nearest possible window.
1227 */
1228 out_width = scale_down(rect->width, scale_comb_h);
1229 out_height = scale_down(rect->height, scale_comb_v);
1230
1231 if (out_width > 2560)
1232 out_width = 2560;
1233 else if (out_width < 2)
1234 out_width = 2;
1235
1236 if (out_height > 1920)
1237 out_height = 1920;
1238 else if (out_height < 4)
1239 out_height = 4;
1240
1241 dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
1242
1243 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1244
1245 /*
1246 * 5. Using actual input window and calculated combined scales calculate
1247 * camera target output window.
1248 */
1249 pix->width = scale_down(cam_rect->width, scale_comb_h);
1250 pix->height = scale_down(cam_rect->height, scale_comb_v);
1251
1252 dev_geo(dev, "5: camera target %ux%u\n", pix->width, pix->height);
1253
1254 /* 6. - 9. */
1255 pix->pixelformat = cam->camera_fmt->fourcc;
1256 pix->colorspace = cam->camera_fmt->colorspace;
1257
1258 capsr = capture_save_reset(pcdev);
1259 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1260
1261 /* Make relative to camera rectangle */
1262 rect->left -= cam_rect->left;
1263 rect->top -= cam_rect->top;
1264
1265 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1266
1267 ret = client_scale(icd, cam_rect, rect, ceu_rect, &f,
1268 pcdev->image_mode && !pcdev->is_interlaced);
1269
1270 dev_geo(dev, "6-9: %d\n", ret);
1271
1272 /* 10. Use CEU cropping to crop to the new window. */
1273 sh_mobile_ceu_set_rect(icd, out_width, out_height);
1274
1275 dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n",
1276 ceu_rect->width, ceu_rect->height,
1277 ceu_rect->left, ceu_rect->top);
1278
1279 /*
1280 * 11. Calculate CEU scales from camera scales from results of (10) and
1281 * user window from (3)
1282 */
1283 scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
1284 scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
1285
1286 dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
1287
1288 /* 12. Apply CEU scales. */
1289 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1290 if (cflcr != pcdev->cflcr) {
1291 pcdev->cflcr = cflcr;
1292 ceu_write(pcdev, CFLCR, cflcr);
1293 }
1294
1295 /* Restore capture */
904078f1
GL
1296 if (pcdev->active)
1297 capsr |= 1;
904078f1
GL
1298 capture_restore(pcdev, capsr);
1299
6a6c8786
GL
1300 icd->user_width = out_width;
1301 icd->user_height = out_height;
1302
904078f1
GL
1303 /* Even if only camera cropping succeeded */
1304 return ret;
09e231b3
GL
1305}
1306
961801bb 1307/* Similar to set_crop multistage iterative algorithm */
d8fac217 1308static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
09e231b3 1309 struct v4l2_format *f)
0d3244d6 1310{
9414de39 1311 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
961801bb 1312 struct sh_mobile_ceu_dev *pcdev = ici->priv;
904078f1
GL
1313 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1314 struct v4l2_pix_format *pix = &f->fmt.pix;
6a6c8786
GL
1315 struct v4l2_format cam_f = *f;
1316 struct v4l2_pix_format *cam_pix = &cam_f.fmt.pix;
c9c1f1c0 1317 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
6a6c8786 1318 struct device *dev = icd->dev.parent;
904078f1 1319 __u32 pixfmt = pix->pixelformat;
9414de39 1320 const struct soc_camera_format_xlate *xlate;
6a6c8786
GL
1321 struct v4l2_crop cam_crop;
1322 struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
1323 unsigned int scale_cam_h, scale_cam_v;
1324 u16 scale_v, scale_h;
1325 int ret;
1326 bool is_interlaced, image_mode;
961801bb
GL
1327
1328 switch (pix->field) {
1329 case V4L2_FIELD_INTERLACED:
6a6c8786 1330 is_interlaced = true;
961801bb
GL
1331 break;
1332 case V4L2_FIELD_ANY:
1333 default:
1334 pix->field = V4L2_FIELD_NONE;
1335 /* fall-through */
1336 case V4L2_FIELD_NONE:
6a6c8786 1337 is_interlaced = false;
961801bb
GL
1338 break;
1339 }
25c4d74e 1340
9414de39
MD
1341 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1342 if (!xlate) {
6a6c8786 1343 dev_warn(dev, "Format %x not found\n", pixfmt);
9414de39 1344 return -EINVAL;
25c4d74e
GL
1345 }
1346
6a6c8786
GL
1347 /* 1. Calculate current camera scales. */
1348 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1349
1350 ret = client_g_rect(sd, cam_rect);
1351 if (ret < 0)
1352 return ret;
1353
1354 ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
1355 if (ret < 0)
1356 return ret;
1357
1358 dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
1359
1360 /*
1361 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1362 * scaled back at current camera scales onto input window.
1363 */
1364 ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
961801bb
GL
1365 if (ret < 0)
1366 return ret;
1367
6a6c8786
GL
1368 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1369 cam_subrect.width, cam_subrect.height,
1370 cam_subrect.left, cam_subrect.top);
1371
1372 /*
1373 * 3. Calculate new combined scales from "effective" input window to
1374 * requested user window.
1375 */
1376 scale_h = calc_generic_scale(cam_subrect.width, pix->width);
1377 scale_v = calc_generic_scale(cam_subrect.height, pix->height);
1378
1379 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1380
1381 /*
1382 * 4. Calculate camera output window by applying combined scales to real
1383 * input window.
1384 */
1385 cam_pix->width = scale_down(cam_rect->width, scale_h);
1386 cam_pix->height = scale_down(cam_rect->height, scale_v);
1387 cam_pix->pixelformat = xlate->cam_fmt->fourcc;
1388
961801bb
GL
1389 switch (pixfmt) {
1390 case V4L2_PIX_FMT_NV12:
1391 case V4L2_PIX_FMT_NV21:
1392 case V4L2_PIX_FMT_NV16:
1393 case V4L2_PIX_FMT_NV61:
6a6c8786 1394 image_mode = true;
961801bb
GL
1395 break;
1396 default:
6a6c8786 1397 image_mode = false;
9414de39 1398 }
25c4d74e 1399
6a6c8786
GL
1400 dev_geo(dev, "4: camera output %ux%u\n",
1401 cam_pix->width, cam_pix->height);
961801bb 1402
6a6c8786
GL
1403 /* 5. - 9. */
1404 ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &cam_f,
1405 image_mode && !is_interlaced);
1406
1407 dev_geo(dev, "5-9: client scale %d\n", ret);
1408
1409 /* Done with the camera. Now see if we can improve the result */
1410
1411 dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
1412 ret, cam_pix->width, cam_pix->height, pix->width, pix->height);
1413 if (ret < 0)
1414 return ret;
1415
1416 /* 10. Use CEU scaling to scale to the requested user window. */
961801bb
GL
1417
1418 /* We cannot scale up */
6a6c8786
GL
1419 if (pix->width > cam_pix->width)
1420 pix->width = cam_pix->width;
1421 if (pix->width > ceu_rect.width)
1422 pix->width = ceu_rect.width;
961801bb 1423
6a6c8786
GL
1424 if (pix->height > cam_pix->height)
1425 pix->height = cam_pix->height;
1426 if (pix->height > ceu_rect.height)
1427 pix->height = ceu_rect.height;
961801bb
GL
1428
1429 /* Let's rock: scale pix->{width x height} down to width x height */
6a6c8786
GL
1430 scale_h = calc_scale(ceu_rect.width, &pix->width);
1431 scale_v = calc_scale(ceu_rect.height, &pix->height);
961801bb 1432
6a6c8786
GL
1433 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1434 ceu_rect.width, scale_h, pix->width,
1435 ceu_rect.height, scale_v, pix->height);
961801bb 1436
6a6c8786 1437 pcdev->cflcr = scale_h | (scale_v << 16);
961801bb
GL
1438
1439 icd->buswidth = xlate->buswidth;
1440 icd->current_fmt = xlate->host_fmt;
1441 cam->camera_fmt = xlate->cam_fmt;
6a6c8786 1442 cam->ceu_rect = ceu_rect;
961801bb
GL
1443
1444 pcdev->is_interlaced = is_interlaced;
6a6c8786 1445 pcdev->image_mode = image_mode;
961801bb
GL
1446
1447 return 0;
0d3244d6
MD
1448}
1449
d8fac217
GL
1450static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1451 struct v4l2_format *f)
0d3244d6 1452{
9414de39 1453 const struct soc_camera_format_xlate *xlate;
904078f1 1454 struct v4l2_pix_format *pix = &f->fmt.pix;
c9c1f1c0 1455 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
904078f1 1456 __u32 pixfmt = pix->pixelformat;
961801bb 1457 int width, height;
ccab8a29 1458 int ret;
a2c8c68c 1459
9414de39
MD
1460 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1461 if (!xlate) {
0166b743 1462 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
25c4d74e 1463 return -EINVAL;
9414de39 1464 }
25c4d74e 1465
0d3244d6
MD
1466 /* FIXME: calculate using depth and bus width */
1467
904078f1
GL
1468 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1469 &pix->height, 4, 1920, 2, 0);
0d3244d6 1470
961801bb
GL
1471 width = pix->width;
1472 height = pix->height;
1473
904078f1 1474 pix->bytesperline = pix->width *
9414de39 1475 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
904078f1 1476 pix->sizeimage = pix->height * pix->bytesperline;
25c4d74e 1477
904078f1 1478 pix->pixelformat = xlate->cam_fmt->fourcc;
979ea1dd 1479
0d3244d6 1480 /* limit to sensor capabilities */
c9c1f1c0 1481 ret = v4l2_subdev_call(sd, video, try_fmt, f);
904078f1 1482 pix->pixelformat = pixfmt;
ccab8a29
KM
1483 if (ret < 0)
1484 return ret;
1485
961801bb
GL
1486 switch (pixfmt) {
1487 case V4L2_PIX_FMT_NV12:
1488 case V4L2_PIX_FMT_NV21:
1489 case V4L2_PIX_FMT_NV16:
1490 case V4L2_PIX_FMT_NV61:
1491 /* FIXME: check against rect_max after converting soc-camera */
1492 /* We can scale precisely, need a bigger image from camera */
1493 if (pix->width < width || pix->height < height) {
1494 int tmp_w = pix->width, tmp_h = pix->height;
1495 pix->width = 2560;
1496 pix->height = 1920;
c9c1f1c0 1497 ret = v4l2_subdev_call(sd, video, try_fmt, f);
961801bb
GL
1498 if (ret < 0) {
1499 /* Shouldn't actually happen... */
0166b743 1500 dev_err(icd->dev.parent,
961801bb
GL
1501 "FIXME: try_fmt() returned %d\n", ret);
1502 pix->width = tmp_w;
1503 pix->height = tmp_h;
1504 }
1505 }
1506 if (pix->width > width)
1507 pix->width = width;
1508 if (pix->height > height)
1509 pix->height = height;
ccab8a29
KM
1510 }
1511
1512 return ret;
0d3244d6
MD
1513}
1514
1515static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
1516 struct v4l2_requestbuffers *p)
1517{
1518 int i;
1519
1520 /* This is for locking debugging only. I removed spinlocks and now I
1521 * check whether .prepare is ever called on a linked buffer, or whether
1522 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1523 * it hadn't triggered */
1524 for (i = 0; i < p->count; i++) {
1525 struct sh_mobile_ceu_buffer *buf;
1526
1527 buf = container_of(icf->vb_vidq.bufs[i],
1528 struct sh_mobile_ceu_buffer, vb);
1529 INIT_LIST_HEAD(&buf->vb.queue);
1530 }
1531
1532 return 0;
1533}
1534
1535static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1536{
1537 struct soc_camera_file *icf = file->private_data;
1538 struct sh_mobile_ceu_buffer *buf;
1539
1540 buf = list_entry(icf->vb_vidq.stream.next,
1541 struct sh_mobile_ceu_buffer, vb.stream);
1542
1543 poll_wait(file, &buf->vb.done, pt);
1544
1545 if (buf->vb.state == VIDEOBUF_DONE ||
1546 buf->vb.state == VIDEOBUF_ERROR)
1547 return POLLIN|POLLRDNORM;
1548
1549 return 0;
1550}
1551
1552static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1553 struct v4l2_capability *cap)
1554{
1555 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1556 cap->version = KERNEL_VERSION(0, 0, 5);
1557 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1558 return 0;
1559}
1560
1561static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
1562 struct soc_camera_device *icd)
1563{
1564 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1565 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1566
1567 videobuf_queue_dma_contig_init(q,
1568 &sh_mobile_ceu_videobuf_ops,
0166b743 1569 icd->dev.parent, &pcdev->lock,
0d3244d6 1570 V4L2_BUF_TYPE_VIDEO_CAPTURE,
e802967c
GL
1571 pcdev->is_interlaced ?
1572 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
0d3244d6
MD
1573 sizeof(struct sh_mobile_ceu_buffer),
1574 icd);
1575}
1576
4a6110bc
GL
1577static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1578 struct v4l2_control *ctrl)
1579{
1580 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1581 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1582 u32 val;
1583
1584 switch (ctrl->id) {
1585 case V4L2_CID_SHARPNESS:
1586 val = ceu_read(pcdev, CLFCR);
1587 ctrl->value = val ^ 1;
1588 return 0;
1589 }
1590 return -ENOIOCTLCMD;
1591}
1592
1593static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1594 struct v4l2_control *ctrl)
1595{
1596 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1597 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1598
1599 switch (ctrl->id) {
1600 case V4L2_CID_SHARPNESS:
1601 switch (icd->current_fmt->fourcc) {
1602 case V4L2_PIX_FMT_NV12:
1603 case V4L2_PIX_FMT_NV21:
1604 case V4L2_PIX_FMT_NV16:
1605 case V4L2_PIX_FMT_NV61:
1606 ceu_write(pcdev, CLFCR, !ctrl->value);
1607 return 0;
1608 }
1609 return -EINVAL;
1610 }
1611 return -ENOIOCTLCMD;
1612}
1613
1614static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1615 {
1616 .id = V4L2_CID_SHARPNESS,
1617 .type = V4L2_CTRL_TYPE_BOOLEAN,
1618 .name = "Low-pass filter",
1619 .minimum = 0,
1620 .maximum = 1,
1621 .step = 1,
1622 .default_value = 0,
1623 },
1624};
1625
0d3244d6
MD
1626static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1627 .owner = THIS_MODULE,
1628 .add = sh_mobile_ceu_add_device,
1629 .remove = sh_mobile_ceu_remove_device,
9414de39 1630 .get_formats = sh_mobile_ceu_get_formats,
904078f1 1631 .put_formats = sh_mobile_ceu_put_formats,
09e231b3 1632 .set_crop = sh_mobile_ceu_set_crop,
d8fac217
GL
1633 .set_fmt = sh_mobile_ceu_set_fmt,
1634 .try_fmt = sh_mobile_ceu_try_fmt,
4a6110bc
GL
1635 .set_ctrl = sh_mobile_ceu_set_ctrl,
1636 .get_ctrl = sh_mobile_ceu_get_ctrl,
0d3244d6
MD
1637 .reqbufs = sh_mobile_ceu_reqbufs,
1638 .poll = sh_mobile_ceu_poll,
1639 .querycap = sh_mobile_ceu_querycap,
0d3244d6
MD
1640 .set_bus_param = sh_mobile_ceu_set_bus_param,
1641 .init_videobuf = sh_mobile_ceu_init_videobuf,
4a6110bc
GL
1642 .controls = sh_mobile_ceu_controls,
1643 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
0d3244d6
MD
1644};
1645
979ea1dd 1646static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
0d3244d6
MD
1647{
1648 struct sh_mobile_ceu_dev *pcdev;
1649 struct resource *res;
1650 void __iomem *base;
1651 unsigned int irq;
1652 int err = 0;
1653
1654 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1655 irq = platform_get_irq(pdev, 0);
1656 if (!res || !irq) {
1657 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1658 err = -ENODEV;
1659 goto exit;
1660 }
1661
1662 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1663 if (!pcdev) {
1664 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1665 err = -ENOMEM;
1666 goto exit;
1667 }
1668
0d3244d6
MD
1669 INIT_LIST_HEAD(&pcdev->capture);
1670 spin_lock_init(&pcdev->lock);
1671
1672 pcdev->pdata = pdev->dev.platform_data;
1673 if (!pcdev->pdata) {
1674 err = -EINVAL;
1675 dev_err(&pdev->dev, "CEU platform data not set.\n");
1676 goto exit_kfree;
1677 }
1678
eb6c8558 1679 base = ioremap_nocache(res->start, resource_size(res));
0d3244d6
MD
1680 if (!base) {
1681 err = -ENXIO;
1682 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1683 goto exit_kfree;
1684 }
1685
1686 pcdev->irq = irq;
1687 pcdev->base = base;
1688 pcdev->video_limit = 0; /* only enabled if second resource exists */
0d3244d6
MD
1689
1690 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1691 if (res) {
1692 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1693 res->start,
eb6c8558 1694 resource_size(res),
0d3244d6
MD
1695 DMA_MEMORY_MAP |
1696 DMA_MEMORY_EXCLUSIVE);
1697 if (!err) {
1698 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1699 err = -ENXIO;
1700 goto exit_iounmap;
1701 }
1702
eb6c8558 1703 pcdev->video_limit = resource_size(res);
0d3244d6
MD
1704 }
1705
1706 /* request irq */
1707 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
af128a10 1708 dev_name(&pdev->dev), pcdev);
0d3244d6
MD
1709 if (err) {
1710 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1711 goto exit_release_mem;
1712 }
1713
6d1386c6
MD
1714 pm_suspend_ignore_children(&pdev->dev, true);
1715 pm_runtime_enable(&pdev->dev);
1716 pm_runtime_resume(&pdev->dev);
a42b6dd6 1717
0d3244d6 1718 pcdev->ici.priv = pcdev;
979ea1dd 1719 pcdev->ici.v4l2_dev.dev = &pdev->dev;
0d3244d6 1720 pcdev->ici.nr = pdev->id;
af128a10
KS
1721 pcdev->ici.drv_name = dev_name(&pdev->dev);
1722 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
0d3244d6
MD
1723
1724 err = soc_camera_host_register(&pcdev->ici);
1725 if (err)
6d1386c6 1726 goto exit_free_irq;
0d3244d6
MD
1727
1728 return 0;
1729
1730exit_free_irq:
1731 free_irq(pcdev->irq, pcdev);
1732exit_release_mem:
1733 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1734 dma_release_declared_memory(&pdev->dev);
1735exit_iounmap:
1736 iounmap(base);
1737exit_kfree:
1738 kfree(pcdev);
1739exit:
1740 return err;
1741}
1742
979ea1dd 1743static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
0d3244d6 1744{
eff505fa
GL
1745 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1746 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
1747 struct sh_mobile_ceu_dev, ici);
0d3244d6 1748
eff505fa 1749 soc_camera_host_unregister(soc_host);
0d3244d6
MD
1750 free_irq(pcdev->irq, pcdev);
1751 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1752 dma_release_declared_memory(&pdev->dev);
1753 iounmap(pcdev->base);
1754 kfree(pcdev);
1755 return 0;
1756}
1757
6d1386c6
MD
1758static int sh_mobile_ceu_runtime_nop(struct device *dev)
1759{
1760 /* Runtime PM callback shared between ->runtime_suspend()
1761 * and ->runtime_resume(). Simply returns success.
1762 *
1763 * This driver re-initializes all registers after
1764 * pm_runtime_get_sync() anyway so there is no need
1765 * to save and restore registers here.
1766 */
1767 return 0;
1768}
1769
1770static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
1771 .runtime_suspend = sh_mobile_ceu_runtime_nop,
1772 .runtime_resume = sh_mobile_ceu_runtime_nop,
1773};
1774
0d3244d6
MD
1775static struct platform_driver sh_mobile_ceu_driver = {
1776 .driver = {
1777 .name = "sh_mobile_ceu",
6d1386c6 1778 .pm = &sh_mobile_ceu_dev_pm_ops,
0d3244d6
MD
1779 },
1780 .probe = sh_mobile_ceu_probe,
979ea1dd 1781 .remove = __exit_p(sh_mobile_ceu_remove),
0d3244d6
MD
1782};
1783
1784static int __init sh_mobile_ceu_init(void)
1785{
1786 return platform_driver_register(&sh_mobile_ceu_driver);
1787}
1788
1789static void __exit sh_mobile_ceu_exit(void)
1790{
01c1e4ca 1791 platform_driver_unregister(&sh_mobile_ceu_driver);
0d3244d6
MD
1792}
1793
1794module_init(sh_mobile_ceu_init);
1795module_exit(sh_mobile_ceu_exit);
1796
1797MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1798MODULE_AUTHOR("Magnus Damm");
1799MODULE_LICENSE("GPL");
40e2e092 1800MODULE_ALIAS("platform:sh_mobile_ceu");