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