V4L/DVB (12533): soc-camera: Use video device object for output in host drivers
[deliverable/linux.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
0d3244d6
MD
77/* per video frame buffer */
78struct sh_mobile_ceu_buffer {
79 struct videobuf_buffer vb; /* v4l buffer must be first */
80 const struct soc_camera_data_format *fmt;
81};
82
83struct sh_mobile_ceu_dev {
0d3244d6
MD
84 struct soc_camera_host ici;
85 struct soc_camera_device *icd;
86
87 unsigned int irq;
88 void __iomem *base;
89 unsigned long video_limit;
90
c60f2b5c 91 /* lock used to protect videobuf */
0d3244d6
MD
92 spinlock_t lock;
93 struct list_head capture;
94 struct videobuf_buffer *active;
95
96 struct sh_mobile_ceu_info *pdata;
8be65dc5 97
961801bb
GL
98 u32 cflcr;
99
904078f1
GL
100 unsigned int is_interlaced:1;
101 unsigned int image_mode:1;
102 unsigned int is_16bit:1;
103};
104
105struct sh_mobile_ceu_cam {
106 struct v4l2_rect camera_rect;
961801bb 107 struct v4l2_rect camera_max;
904078f1 108 const struct soc_camera_data_format *extra_fmt;
8be65dc5 109 const struct soc_camera_data_format *camera_fmt;
0d3244d6
MD
110};
111
c354b400
KM
112static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
113{
114 unsigned long flags;
115
116 flags = SOCAM_MASTER |
117 SOCAM_PCLK_SAMPLE_RISING |
118 SOCAM_HSYNC_ACTIVE_HIGH |
119 SOCAM_HSYNC_ACTIVE_LOW |
120 SOCAM_VSYNC_ACTIVE_HIGH |
121 SOCAM_VSYNC_ACTIVE_LOW |
122 SOCAM_DATA_ACTIVE_HIGH;
123
124 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
125 flags |= SOCAM_DATAWIDTH_8;
126
127 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
128 flags |= SOCAM_DATAWIDTH_16;
129
130 if (flags & SOCAM_DATAWIDTH_MASK)
131 return flags;
132
133 return 0;
134}
135
0d3244d6 136static void ceu_write(struct sh_mobile_ceu_dev *priv,
7a1a8164 137 unsigned long reg_offs, u32 data)
0d3244d6
MD
138{
139 iowrite32(data, priv->base + reg_offs);
140}
141
7a1a8164 142static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
0d3244d6
MD
143{
144 return ioread32(priv->base + reg_offs);
145}
146
147/*
148 * Videobuf operations
149 */
150static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
151 unsigned int *count,
152 unsigned int *size)
153{
154 struct soc_camera_device *icd = vq->priv_data;
155 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
156 struct sh_mobile_ceu_dev *pcdev = ici->priv;
157 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
158
a0705b07
GL
159 *size = PAGE_ALIGN(icd->rect_current.width * icd->rect_current.height *
160 bytes_per_pixel);
0d3244d6
MD
161
162 if (0 == *count)
163 *count = 2;
164
165 if (pcdev->video_limit) {
166 while (*size * *count > pcdev->video_limit)
167 (*count)--;
168 }
169
0166b743 170 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
0d3244d6
MD
171
172 return 0;
173}
174
175static void free_buffer(struct videobuf_queue *vq,
176 struct sh_mobile_ceu_buffer *buf)
177{
178 struct soc_camera_device *icd = vq->priv_data;
179
0166b743 180 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
181 &buf->vb, buf->vb.baddr, buf->vb.bsize);
182
183 if (in_interrupt())
184 BUG();
185
97215cbd 186 videobuf_waiton(&buf->vb, 0, 0);
0d3244d6 187 videobuf_dma_contig_free(vq, &buf->vb);
0166b743 188 dev_dbg(icd->dev.parent, "%s freed\n", __func__);
0d3244d6
MD
189 buf->vb.state = VIDEOBUF_NEEDS_INIT;
190}
191
7a1a8164
MD
192#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
193#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
194#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
195#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
196
197
0d3244d6
MD
198static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
199{
8be65dc5 200 struct soc_camera_device *icd = pcdev->icd;
ccab8a29 201 dma_addr_t phys_addr_top, phys_addr_bottom;
7a1a8164
MD
202
203 /* The hardware is _very_ picky about this sequence. Especially
204 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
205 * several not-so-well documented interrupt sources in CETCR.
206 */
207 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
208 ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
209 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
210 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
211 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
0d3244d6 212
8be65dc5
MD
213 if (!pcdev->active)
214 return;
215
ccab8a29
KM
216 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
217 ceu_write(pcdev, CDAYR, phys_addr_top);
e802967c 218 if (pcdev->is_interlaced) {
a0705b07 219 phys_addr_bottom = phys_addr_top + icd->rect_current.width;
ccab8a29
KM
220 ceu_write(pcdev, CDBYR, phys_addr_bottom);
221 }
8be65dc5
MD
222
223 switch (icd->current_fmt->fourcc) {
224 case V4L2_PIX_FMT_NV12:
225 case V4L2_PIX_FMT_NV21:
9e4a56d2
MD
226 case V4L2_PIX_FMT_NV16:
227 case V4L2_PIX_FMT_NV61:
a0705b07
GL
228 phys_addr_top += icd->rect_current.width *
229 icd->rect_current.height;
ccab8a29 230 ceu_write(pcdev, CDACR, phys_addr_top);
e802967c 231 if (pcdev->is_interlaced) {
a0705b07
GL
232 phys_addr_bottom = phys_addr_top +
233 icd->rect_current.width;
ccab8a29
KM
234 ceu_write(pcdev, CDBCR, phys_addr_bottom);
235 }
0d3244d6 236 }
8be65dc5
MD
237
238 pcdev->active->state = VIDEOBUF_ACTIVE;
239 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
0d3244d6
MD
240}
241
242static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
243 struct videobuf_buffer *vb,
244 enum v4l2_field field)
245{
246 struct soc_camera_device *icd = vq->priv_data;
247 struct sh_mobile_ceu_buffer *buf;
248 int ret;
249
250 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
251
0166b743 252 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
253 vb, vb->baddr, vb->bsize);
254
255 /* Added list head initialization on alloc */
256 WARN_ON(!list_empty(&vb->queue));
257
258#ifdef DEBUG
259 /* This can be useful if you want to see if we actually fill
260 * the buffer with something */
261 memset((void *)vb->baddr, 0xaa, vb->bsize);
262#endif
263
264 BUG_ON(NULL == icd->current_fmt);
265
266 if (buf->fmt != icd->current_fmt ||
a0705b07
GL
267 vb->width != icd->rect_current.width ||
268 vb->height != icd->rect_current.height ||
0d3244d6
MD
269 vb->field != field) {
270 buf->fmt = icd->current_fmt;
a0705b07
GL
271 vb->width = icd->rect_current.width;
272 vb->height = icd->rect_current.height;
0d3244d6
MD
273 vb->field = field;
274 vb->state = VIDEOBUF_NEEDS_INIT;
275 }
276
277 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
278 if (0 != vb->baddr && vb->bsize < vb->size) {
279 ret = -EINVAL;
280 goto out;
281 }
282
283 if (vb->state == VIDEOBUF_NEEDS_INIT) {
284 ret = videobuf_iolock(vq, vb, NULL);
285 if (ret)
286 goto fail;
287 vb->state = VIDEOBUF_PREPARED;
288 }
289
290 return 0;
291fail:
292 free_buffer(vq, buf);
293out:
294 return ret;
295}
296
2dd54a54 297/* Called under spinlock_irqsave(&pcdev->lock, ...) */
0d3244d6
MD
298static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
299 struct videobuf_buffer *vb)
300{
301 struct soc_camera_device *icd = vq->priv_data;
302 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
303 struct sh_mobile_ceu_dev *pcdev = ici->priv;
0d3244d6 304
0166b743 305 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
306 vb, vb->baddr, vb->bsize);
307
51354cc3 308 vb->state = VIDEOBUF_QUEUED;
0d3244d6
MD
309 list_add_tail(&vb->queue, &pcdev->capture);
310
311 if (!pcdev->active) {
312 pcdev->active = vb;
313 sh_mobile_ceu_capture(pcdev);
314 }
0d3244d6
MD
315}
316
317static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
318 struct videobuf_buffer *vb)
319{
cca0e549
GL
320 struct soc_camera_device *icd = vq->priv_data;
321 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
322 struct sh_mobile_ceu_dev *pcdev = ici->priv;
323 unsigned long flags;
324
325 spin_lock_irqsave(&pcdev->lock, flags);
326
327 if (pcdev->active == vb) {
328 /* disable capture (release DMA buffer), reset */
329 ceu_write(pcdev, CAPSR, 1 << 16);
330 pcdev->active = NULL;
331 }
332
333 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
334 !list_empty(&vb->queue)) {
335 vb->state = VIDEOBUF_ERROR;
336 list_del_init(&vb->queue);
337 }
338
339 spin_unlock_irqrestore(&pcdev->lock, flags);
340
0d3244d6
MD
341 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
342}
343
344static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
345 .buf_setup = sh_mobile_ceu_videobuf_setup,
346 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
347 .buf_queue = sh_mobile_ceu_videobuf_queue,
348 .buf_release = sh_mobile_ceu_videobuf_release,
349};
350
351static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
352{
353 struct sh_mobile_ceu_dev *pcdev = data;
354 struct videobuf_buffer *vb;
355 unsigned long flags;
356
357 spin_lock_irqsave(&pcdev->lock, flags);
358
359 vb = pcdev->active;
cca0e549
GL
360 if (!vb)
361 /* Stale interrupt from a released buffer */
362 goto out;
363
0d3244d6
MD
364 list_del_init(&vb->queue);
365
366 if (!list_empty(&pcdev->capture))
367 pcdev->active = list_entry(pcdev->capture.next,
368 struct videobuf_buffer, queue);
369 else
370 pcdev->active = NULL;
371
372 sh_mobile_ceu_capture(pcdev);
373
374 vb->state = VIDEOBUF_DONE;
375 do_gettimeofday(&vb->ts);
376 vb->field_count++;
377 wake_up(&vb->done);
cca0e549
GL
378
379out:
0d3244d6
MD
380 spin_unlock_irqrestore(&pcdev->lock, flags);
381
382 return IRQ_HANDLED;
383}
384
1c3bb743 385/* Called with .video_lock held */
0d3244d6
MD
386static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
387{
388 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
389 struct sh_mobile_ceu_dev *pcdev = ici->priv;
0d3244d6 390
0d3244d6 391 if (pcdev->icd)
979ea1dd 392 return -EBUSY;
0d3244d6 393
0166b743 394 dev_info(icd->dev.parent,
0d3244d6
MD
395 "SuperH Mobile CEU driver attached to camera %d\n",
396 icd->devnum);
397
40e2e092
GL
398 clk_enable(pcdev->clk);
399
0d3244d6
MD
400 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
401 while (ceu_read(pcdev, CSTSR) & 1)
402 msleep(1);
403
404 pcdev->icd = icd;
979ea1dd
GL
405
406 return 0;
0d3244d6
MD
407}
408
1c3bb743 409/* Called with .video_lock held */
0d3244d6
MD
410static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
411{
412 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
413 struct sh_mobile_ceu_dev *pcdev = ici->priv;
51354cc3 414 unsigned long flags;
0d3244d6
MD
415
416 BUG_ON(icd != pcdev->icd);
417
418 /* disable capture, disable interrupts */
419 ceu_write(pcdev, CEIER, 0);
420 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
51354cc3
MD
421
422 /* make sure active buffer is canceled */
423 spin_lock_irqsave(&pcdev->lock, flags);
424 if (pcdev->active) {
425 list_del(&pcdev->active->queue);
426 pcdev->active->state = VIDEOBUF_ERROR;
427 wake_up_all(&pcdev->active->done);
428 pcdev->active = NULL;
429 }
430 spin_unlock_irqrestore(&pcdev->lock, flags);
431
40e2e092
GL
432 clk_disable(pcdev->clk);
433
0166b743 434 dev_info(icd->dev.parent,
0d3244d6
MD
435 "SuperH Mobile CEU driver detached from camera %d\n",
436 icd->devnum);
437
438 pcdev->icd = NULL;
439}
440
961801bb
GL
441/*
442 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
443 * in SH7722 Hardware Manual
444 */
445static unsigned int size_dst(unsigned int src, unsigned int scale)
446{
447 unsigned int mant_pre = scale >> 12;
448 if (!src || !scale)
449 return src;
450 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
451 mant_pre * 4096 / scale + 1;
452}
453
454static unsigned int size_src(unsigned int dst, unsigned int scale)
455{
456 unsigned int mant_pre = scale >> 12, tmp;
457 if (!dst || !scale)
458 return dst;
459 for (tmp = ((dst - 1) * scale + 2048 * mant_pre) / 4096 + 1;
460 size_dst(tmp, scale) < dst;
461 tmp++)
462 ;
463 return tmp;
464}
465
466static u16 calc_scale(unsigned int src, unsigned int *dst)
467{
468 u16 scale;
469
470 if (src == *dst)
471 return 0;
472
473 scale = (src * 4096 / *dst) & ~7;
474
475 while (scale > 4096 && size_dst(src, scale) < *dst)
476 scale -= 8;
477
478 *dst = size_dst(src, scale);
479
480 return scale;
481}
482
483/* rect is guaranteed to not exceed the scaled camera rectangle */
904078f1
GL
484static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
485 struct v4l2_rect *rect)
486{
487 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
488 struct sh_mobile_ceu_cam *cam = icd->host_priv;
489 struct sh_mobile_ceu_dev *pcdev = ici->priv;
961801bb
GL
490 int width, height, cfszr_width, cdwdr_width, in_width, in_height;
491 unsigned int left_offset, top_offset, left, top;
492 unsigned int hscale = pcdev->cflcr & 0xffff;
493 unsigned int vscale = (pcdev->cflcr >> 16) & 0xffff;
904078f1
GL
494 u32 camor;
495
961801bb
GL
496 /* Switch to the camera scale */
497 left = size_src(rect->left, hscale);
498 top = size_src(rect->top, vscale);
499
0166b743 500 dev_dbg(icd->dev.parent, "Left %u * 0x%x = %u, top %u * 0x%x = %u\n",
961801bb
GL
501 rect->left, hscale, left, rect->top, vscale, top);
502
503 if (left > cam->camera_rect.left) {
504 left_offset = left - cam->camera_rect.left;
904078f1
GL
505 } else {
506 left_offset = 0;
961801bb 507 left = cam->camera_rect.left;
904078f1
GL
508 }
509
961801bb
GL
510 if (top > cam->camera_rect.top) {
511 top_offset = top - cam->camera_rect.top;
904078f1
GL
512 } else {
513 top_offset = 0;
961801bb 514 top = cam->camera_rect.top;
904078f1
GL
515 }
516
0166b743 517 dev_dbg(icd->dev.parent, "New left %u, top %u, offsets %u:%u\n",
961801bb 518 rect->left, rect->top, left_offset, top_offset);
904078f1
GL
519
520 if (pcdev->image_mode) {
521 width = rect->width;
961801bb
GL
522 in_width = cam->camera_rect.width;
523 if (!pcdev->is_16bit) {
904078f1 524 width *= 2;
961801bb
GL
525 in_width *= 2;
526 left_offset *= 2;
527 }
904078f1
GL
528 cfszr_width = cdwdr_width = rect->width;
529 } else {
961801bb
GL
530 unsigned int w_factor = (icd->current_fmt->depth + 7) >> 3;
531 if (!pcdev->is_16bit)
532 w_factor *= 2;
533
534 width = rect->width * w_factor / 2;
535 in_width = cam->camera_rect.width * w_factor / 2;
536 left_offset = left_offset * w_factor / 2;
537
904078f1
GL
538 cfszr_width = pcdev->is_16bit ? width : width / 2;
539 cdwdr_width = pcdev->is_16bit ? width * 2 : width;
540 }
541
542 height = rect->height;
961801bb 543 in_height = cam->camera_rect.height;
904078f1
GL
544 if (pcdev->is_interlaced) {
545 height /= 2;
961801bb
GL
546 in_height /= 2;
547 top_offset /= 2;
904078f1
GL
548 cdwdr_width *= 2;
549 }
550
551 camor = left_offset | (top_offset << 16);
552 ceu_write(pcdev, CAMOR, camor);
961801bb 553 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
904078f1
GL
554 ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width);
555 ceu_write(pcdev, CDWDR, cdwdr_width);
556}
557
558static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
559{
560 u32 capsr = ceu_read(pcdev, CAPSR);
561 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
562 return capsr;
563}
564
565static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
566{
567 unsigned long timeout = jiffies + 10 * HZ;
568
569 /*
570 * Wait until the end of the current frame. It can take a long time,
571 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
572 */
573 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
574 msleep(1);
575
576 if (time_after(jiffies, timeout)) {
577 dev_err(pcdev->ici.v4l2_dev.dev,
578 "Timeout waiting for frame end! Interface problem?\n");
579 return;
580 }
581
582 /* Wait until reset clears, this shall not hang... */
583 while (ceu_read(pcdev, CAPSR) & (1 << 16))
584 udelay(10);
585
586 /* Anything to restore? */
587 if (capsr & ~(1 << 16))
588 ceu_write(pcdev, CAPSR, capsr);
589}
590
0d3244d6
MD
591static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
592 __u32 pixfmt)
593{
594 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
595 struct sh_mobile_ceu_dev *pcdev = ici->priv;
904078f1 596 int ret;
0d3244d6 597 unsigned long camera_flags, common_flags, value;
904078f1
GL
598 int yuv_lineskip;
599 struct sh_mobile_ceu_cam *cam = icd->host_priv;
600 u32 capsr = capture_save_reset(pcdev);
0d3244d6
MD
601
602 camera_flags = icd->ops->query_bus_param(icd);
603 common_flags = soc_camera_bus_param_compatible(camera_flags,
c354b400 604 make_bus_param(pcdev));
0d3244d6
MD
605 if (!common_flags)
606 return -EINVAL;
607
608 ret = icd->ops->set_bus_param(icd, common_flags);
609 if (ret < 0)
610 return ret;
611
612 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
613 case SOCAM_DATAWIDTH_8:
904078f1 614 pcdev->is_16bit = 0;
0d3244d6
MD
615 break;
616 case SOCAM_DATAWIDTH_16:
904078f1 617 pcdev->is_16bit = 1;
0d3244d6
MD
618 break;
619 default:
620 return -EINVAL;
621 }
622
623 ceu_write(pcdev, CRCNTR, 0);
624 ceu_write(pcdev, CRCMPR, 0);
625
8be65dc5 626 value = 0x00000010; /* data fetch by default */
961801bb 627 yuv_lineskip = 0;
8be65dc5
MD
628
629 switch (icd->current_fmt->fourcc) {
630 case V4L2_PIX_FMT_NV12:
631 case V4L2_PIX_FMT_NV21:
632 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
9e4a56d2
MD
633 /* fall-through */
634 case V4L2_PIX_FMT_NV16:
635 case V4L2_PIX_FMT_NV61:
904078f1 636 switch (cam->camera_fmt->fourcc) {
8be65dc5
MD
637 case V4L2_PIX_FMT_UYVY:
638 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
639 break;
640 case V4L2_PIX_FMT_VYUY:
641 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
642 break;
643 case V4L2_PIX_FMT_YUYV:
644 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
645 break;
646 case V4L2_PIX_FMT_YVYU:
647 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
648 break;
649 default:
650 BUG();
651 }
652 }
653
7a1a8164
MD
654 if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
655 icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
9e4a56d2 656 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
8be65dc5 657
7a1a8164
MD
658 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
659 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
904078f1 660 value |= pcdev->is_16bit ? 1 << 12 : 0;
0d3244d6
MD
661 ceu_write(pcdev, CAMCR, value);
662
663 ceu_write(pcdev, CAPCR, 0x00300000);
e802967c 664 ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
0d3244d6
MD
665
666 mdelay(1);
904078f1 667 sh_mobile_ceu_set_rect(icd, &icd->rect_current);
0d3244d6 668
961801bb 669 ceu_write(pcdev, CFLCR, pcdev->cflcr);
dd54203b
MD
670
671 /* A few words about byte order (observed in Big Endian mode)
672 *
673 * In data fetch mode bytes are received in chunks of 8 bytes.
674 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
675 *
676 * The data is however by default written to memory in reverse order:
677 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
678 *
679 * The lowest three bits of CDOCR allows us to do swapping,
2c0a072e
MD
680 * using 7 we swap the data bytes to match the incoming order:
681 * D0, D1, D2, D3, D4, D5, D6, D7
dd54203b 682 */
8be65dc5
MD
683 value = 0x00000017;
684 if (yuv_lineskip)
685 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
686
687 ceu_write(pcdev, CDOCR, value);
0d3244d6
MD
688 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
689
0166b743 690 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u@%u:%u\n",
904078f1
GL
691 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
692 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
693 icd->rect_current.width, icd->rect_current.height,
694 icd->rect_current.left, icd->rect_current.top);
695
696 capture_restore(pcdev, capsr);
697
0d3244d6 698 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
0d3244d6
MD
699 return 0;
700}
701
9414de39 702static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
0d3244d6
MD
703{
704 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
705 struct sh_mobile_ceu_dev *pcdev = ici->priv;
706 unsigned long camera_flags, common_flags;
707
708 camera_flags = icd->ops->query_bus_param(icd);
709 common_flags = soc_camera_bus_param_compatible(camera_flags,
c354b400 710 make_bus_param(pcdev));
0d3244d6
MD
711 if (!common_flags)
712 return -EINVAL;
713
714 return 0;
715}
716
8be65dc5
MD
717static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
718 {
719 .name = "NV12",
720 .depth = 12,
721 .fourcc = V4L2_PIX_FMT_NV12,
722 .colorspace = V4L2_COLORSPACE_JPEG,
723 },
724 {
725 .name = "NV21",
726 .depth = 12,
727 .fourcc = V4L2_PIX_FMT_NV21,
728 .colorspace = V4L2_COLORSPACE_JPEG,
729 },
9e4a56d2
MD
730 {
731 .name = "NV16",
732 .depth = 16,
733 .fourcc = V4L2_PIX_FMT_NV16,
734 .colorspace = V4L2_COLORSPACE_JPEG,
735 },
736 {
737 .name = "NV61",
738 .depth = 16,
739 .fourcc = V4L2_PIX_FMT_NV61,
740 .colorspace = V4L2_COLORSPACE_JPEG,
741 },
8be65dc5
MD
742};
743
9414de39
MD
744static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
745 struct soc_camera_format_xlate *xlate)
746{
8be65dc5 747 int ret, k, n;
9414de39 748 int formats = 0;
904078f1 749 struct sh_mobile_ceu_cam *cam;
9414de39
MD
750
751 ret = sh_mobile_ceu_try_bus_param(icd);
752 if (ret < 0)
753 return 0;
754
904078f1
GL
755 if (!icd->host_priv) {
756 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
757 if (!cam)
758 return -ENOMEM;
759
760 icd->host_priv = cam;
961801bb 761 cam->camera_max = icd->rect_max;
904078f1
GL
762 } else {
763 cam = icd->host_priv;
764 }
765
c8329acc
GL
766 /* Beginning of a pass */
767 if (!idx)
904078f1 768 cam->extra_fmt = NULL;
c8329acc 769
9414de39 770 switch (icd->formats[idx].fourcc) {
8be65dc5
MD
771 case V4L2_PIX_FMT_UYVY:
772 case V4L2_PIX_FMT_VYUY:
773 case V4L2_PIX_FMT_YUYV:
774 case V4L2_PIX_FMT_YVYU:
904078f1 775 if (cam->extra_fmt)
c8329acc
GL
776 goto add_single_format;
777
778 /*
779 * Our case is simple so far: for any of the above four camera
780 * formats we add all our four synthesized NV* formats, so,
781 * just marking the device with a single flag suffices. If
782 * the format generation rules are more complex, you would have
783 * to actually hang your already added / counted formats onto
784 * the host_priv pointer and check whether the format you're
785 * going to add now is already there.
786 */
904078f1 787 cam->extra_fmt = (void *)sh_mobile_ceu_formats;
c8329acc 788
8be65dc5
MD
789 n = ARRAY_SIZE(sh_mobile_ceu_formats);
790 formats += n;
791 for (k = 0; xlate && k < n; k++) {
792 xlate->host_fmt = &sh_mobile_ceu_formats[k];
793 xlate->cam_fmt = icd->formats + idx;
794 xlate->buswidth = icd->formats[idx].depth;
795 xlate++;
0166b743
GL
796 dev_dbg(icd->dev.parent,
797 "Providing format %s using %s\n",
8be65dc5
MD
798 sh_mobile_ceu_formats[k].name,
799 icd->formats[idx].name);
800 }
9414de39 801 default:
c8329acc 802add_single_format:
9414de39
MD
803 /* Generic pass-through */
804 formats++;
805 if (xlate) {
806 xlate->host_fmt = icd->formats + idx;
807 xlate->cam_fmt = icd->formats + idx;
808 xlate->buswidth = icd->formats[idx].depth;
809 xlate++;
0166b743 810 dev_dbg(icd->dev.parent,
9414de39
MD
811 "Providing format %s in pass-through mode\n",
812 icd->formats[idx].name);
813 }
814 }
815
816 return formats;
817}
818
904078f1
GL
819static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
820{
821 kfree(icd->host_priv);
822 icd->host_priv = NULL;
823}
824
961801bb 825/* Check if any dimension of r1 is smaller than respective one of r2 */
904078f1
GL
826static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
827{
828 return r1->width < r2->width || r1->height < r2->height;
829}
830
961801bb
GL
831/* Check if r1 fails to cover r2 */
832static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
833{
834 return r1->left > r2->left || r1->top > r2->top ||
835 r1->left + r1->width < r2->left + r2->width ||
836 r1->top + r1->height < r2->top + r2->height;
837}
838
839/*
840 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
841 * framerate by always requesting the maximum image from the client. For
842 * cropping we also have to take care of the current scale. The common for both
843 * scaling and cropping approach is:
844 * 1. try if the client can produce exactly what requested by the user
845 * 2. if (1) failed, try to double the client image until we get one big enough
846 * 3. if (2) failed, try to request the maximum image
847 */
09e231b3 848static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
08590b96 849 struct v4l2_crop *a)
09e231b3 850{
08590b96 851 struct v4l2_rect *rect = &a->c;
904078f1
GL
852 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
853 struct sh_mobile_ceu_dev *pcdev = ici->priv;
08590b96
GL
854 struct v4l2_crop cam_crop;
855 struct v4l2_rect *cam_rect = &cam_crop.c, target, cam_max;
904078f1 856 struct sh_mobile_ceu_cam *cam = icd->host_priv;
c9c1f1c0 857 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
961801bb
GL
858 unsigned int hscale = pcdev->cflcr & 0xffff;
859 unsigned int vscale = (pcdev->cflcr >> 16) & 0xffff;
904078f1
GL
860 unsigned short width, height;
861 u32 capsr;
862 int ret;
863
961801bb 864 /* Scale back up into client units */
08590b96
GL
865 cam_rect->left = size_src(rect->left, hscale);
866 cam_rect->width = size_src(rect->width, hscale);
867 cam_rect->top = size_src(rect->top, vscale);
868 cam_rect->height = size_src(rect->height, vscale);
961801bb 869
08590b96 870 target = *cam_rect;
961801bb 871
904078f1 872 capsr = capture_save_reset(pcdev);
0166b743
GL
873 dev_dbg(icd->dev.parent, "CAPSR 0x%x, CFLCR 0x%x\n",
874 capsr, pcdev->cflcr);
904078f1 875
961801bb 876 /* First attempt - see if the client can deliver a perfect result */
08590b96 877 ret = v4l2_subdev_call(sd, video, s_crop, &cam_crop);
961801bb 878 if (!ret && !memcmp(&target, &cam_rect, sizeof(target))) {
0166b743
GL
879 dev_dbg(icd->dev.parent,
880 "Camera S_CROP successful for %ux%u@%u:%u\n",
08590b96
GL
881 cam_rect->width, cam_rect->height,
882 cam_rect->left, cam_rect->top);
904078f1
GL
883 goto ceu_set_rect;
884 }
885
886 /* Try to fix cropping, that camera hasn't managed to do */
0166b743 887 dev_dbg(icd->dev.parent, "Fix camera S_CROP %d for %ux%u@%u:%u"
961801bb 888 " to %ux%u@%u:%u\n",
08590b96
GL
889 ret, cam_rect->width, cam_rect->height,
890 cam_rect->left, cam_rect->top,
961801bb 891 target.width, target.height, target.left, target.top);
904078f1
GL
892
893 /*
894 * Popular special case - some cameras can only handle fixed sizes like
895 * QVGA, VGA,... Take care to avoid infinite loop.
896 */
08590b96
GL
897 width = max(cam_rect->width, 1);
898 height = max(cam_rect->height, 1);
961801bb
GL
899 cam_max.width = size_src(icd->rect_max.width, hscale);
900 cam_max.left = size_src(icd->rect_max.left, hscale);
901 cam_max.height = size_src(icd->rect_max.height, vscale);
902 cam_max.top = size_src(icd->rect_max.top, vscale);
08590b96
GL
903 while (!ret && (is_smaller(cam_rect, &target) ||
904 is_inside(cam_rect, &target)) &&
961801bb
GL
905 cam_max.width >= width && cam_max.height >= height) {
906
907 width *= 2;
908 height *= 2;
08590b96
GL
909 cam_rect->width = width;
910 cam_rect->height = height;
904078f1 911
961801bb 912 /* We do not know what the camera is capable of, play safe */
08590b96
GL
913 if (cam_rect->left > target.left)
914 cam_rect->left = cam_max.left;
961801bb 915
08590b96
GL
916 if (cam_rect->left + cam_rect->width < target.left + target.width)
917 cam_rect->width = target.left + target.width -
918 cam_rect->left;
961801bb 919
08590b96
GL
920 if (cam_rect->top > target.top)
921 cam_rect->top = cam_max.top;
961801bb 922
08590b96
GL
923 if (cam_rect->top + cam_rect->height < target.top + target.height)
924 cam_rect->height = target.top + target.height -
925 cam_rect->top;
961801bb 926
08590b96 927 if (cam_rect->width + cam_rect->left >
961801bb 928 cam_max.width + cam_max.left)
08590b96
GL
929 cam_rect->left = max(cam_max.width + cam_max.left -
930 cam_rect->width, cam_max.left);
904078f1 931
08590b96 932 if (cam_rect->height + cam_rect->top >
961801bb 933 cam_max.height + cam_max.top)
08590b96
GL
934 cam_rect->top = max(cam_max.height + cam_max.top -
935 cam_rect->height, cam_max.top);
904078f1 936
08590b96 937 ret = v4l2_subdev_call(sd, video, s_crop, &cam_crop);
0166b743 938 dev_dbg(icd->dev.parent, "Camera S_CROP %d for %ux%u@%u:%u\n",
08590b96
GL
939 ret, cam_rect->width, cam_rect->height,
940 cam_rect->left, cam_rect->top);
904078f1
GL
941 }
942
943 /*
944 * If the camera failed to configure cropping, it should not modify the
945 * rectangle
946 */
961801bb
GL
947 if ((ret < 0 && (is_smaller(&icd->rect_current, rect) ||
948 is_inside(&icd->rect_current, rect))) ||
08590b96 949 is_smaller(cam_rect, &target) || is_inside(cam_rect, &target)) {
904078f1
GL
950 /*
951 * The camera failed to configure a suitable cropping,
952 * we cannot use the current rectangle, set to max
953 */
08590b96
GL
954 *cam_rect = cam_max;
955 ret = v4l2_subdev_call(sd, video, s_crop, &cam_crop);
0166b743
GL
956 dev_dbg(icd->dev.parent,
957 "Camera S_CROP %d for max %ux%u@%u:%u\n",
08590b96
GL
958 ret, cam_rect->width, cam_rect->height,
959 cam_rect->left, cam_rect->top);
960 if (ret < 0 && ret != -ENOIOCTLCMD)
904078f1
GL
961 /* All failed, hopefully resume current capture */
962 goto resume_capture;
961801bb
GL
963
964 /* Finally, adjust the target rectangle */
08590b96
GL
965 if (target.width > cam_rect->width)
966 target.width = cam_rect->width;
967 if (target.height > cam_rect->height)
968 target.height = cam_rect->height;
969 if (target.left + target.width > cam_rect->left + cam_rect->width)
970 target.left = cam_rect->left + cam_rect->width -
961801bb 971 target.width;
08590b96
GL
972 if (target.top + target.height > cam_rect->top + cam_rect->height)
973 target.top = cam_rect->top + cam_rect->height -
961801bb 974 target.height;
904078f1
GL
975 }
976
977 /* We now have a rectangle, larger than requested, let's crop */
978
979 /*
980 * We have to preserve camera rectangle between close() / open(),
981 * because soc-camera core calls .set_fmt() on each first open() with
982 * last before last close() _user_ rectangle, which can be different
983 * from camera rectangle.
984 */
0166b743 985 dev_dbg(icd->dev.parent,
961801bb 986 "SH S_CROP from %ux%u@%u:%u to %ux%u@%u:%u, scale to %ux%u@%u:%u\n",
08590b96 987 cam_rect->width, cam_rect->height, cam_rect->left, cam_rect->top,
961801bb 988 target.width, target.height, target.left, target.top,
904078f1
GL
989 rect->width, rect->height, rect->left, rect->top);
990
991 ret = 0;
992
993ceu_set_rect:
08590b96 994 cam->camera_rect = *cam_rect;
904078f1 995
961801bb
GL
996 rect->width = size_dst(target.width, hscale);
997 rect->left = size_dst(target.left, hscale);
998 rect->height = size_dst(target.height, vscale);
999 rect->top = size_dst(target.top, vscale);
1000
1001 sh_mobile_ceu_set_rect(icd, rect);
1002
1003resume_capture:
904078f1
GL
1004 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
1005 if (pcdev->active)
1006 capsr |= 1;
904078f1
GL
1007 capture_restore(pcdev, capsr);
1008
904078f1
GL
1009 /* Even if only camera cropping succeeded */
1010 return ret;
09e231b3
GL
1011}
1012
961801bb 1013/* Similar to set_crop multistage iterative algorithm */
d8fac217 1014static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
09e231b3 1015 struct v4l2_format *f)
0d3244d6 1016{
9414de39 1017 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
961801bb 1018 struct sh_mobile_ceu_dev *pcdev = ici->priv;
904078f1
GL
1019 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1020 struct v4l2_pix_format *pix = &f->fmt.pix;
c9c1f1c0 1021 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
904078f1 1022 __u32 pixfmt = pix->pixelformat;
9414de39 1023 const struct soc_camera_format_xlate *xlate;
961801bb
GL
1024 unsigned int width = pix->width, height = pix->height, tmp_w, tmp_h;
1025 u16 vscale, hscale;
1026 int ret, is_interlaced;
1027
1028 switch (pix->field) {
1029 case V4L2_FIELD_INTERLACED:
1030 is_interlaced = 1;
1031 break;
1032 case V4L2_FIELD_ANY:
1033 default:
1034 pix->field = V4L2_FIELD_NONE;
1035 /* fall-through */
1036 case V4L2_FIELD_NONE:
1037 is_interlaced = 0;
1038 break;
1039 }
25c4d74e 1040
9414de39
MD
1041 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1042 if (!xlate) {
0166b743 1043 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
9414de39 1044 return -EINVAL;
25c4d74e
GL
1045 }
1046
904078f1 1047 pix->pixelformat = xlate->cam_fmt->fourcc;
c9c1f1c0 1048 ret = v4l2_subdev_call(sd, video, s_fmt, f);
904078f1 1049 pix->pixelformat = pixfmt;
0166b743
GL
1050 dev_dbg(icd->dev.parent,
1051 "Camera %d fmt %ux%u, requested %ux%u, max %ux%u\n",
961801bb
GL
1052 ret, pix->width, pix->height, width, height,
1053 icd->rect_max.width, icd->rect_max.height);
1054 if (ret < 0)
1055 return ret;
1056
1057 switch (pixfmt) {
1058 case V4L2_PIX_FMT_NV12:
1059 case V4L2_PIX_FMT_NV21:
1060 case V4L2_PIX_FMT_NV16:
1061 case V4L2_PIX_FMT_NV61:
1062 pcdev->image_mode = 1;
1063 break;
1064 default:
1065 pcdev->image_mode = 0;
9414de39 1066 }
25c4d74e 1067
961801bb
GL
1068 if ((abs(width - pix->width) < 4 && abs(height - pix->height) < 4) ||
1069 !pcdev->image_mode || is_interlaced) {
1070 hscale = 0;
1071 vscale = 0;
1072 goto out;
1073 }
1074
1075 /* Camera set a format, but geometry is not precise, try to improve */
1076 /*
1077 * FIXME: when soc-camera is converted to implement traditional S_FMT
1078 * and S_CROP semantics, replace CEU limits with camera maxima
1079 */
1080 tmp_w = pix->width;
1081 tmp_h = pix->height;
1082 while ((width > tmp_w || height > tmp_h) &&
1083 tmp_w < 2560 && tmp_h < 1920) {
1084 tmp_w = min(2 * tmp_w, (__u32)2560);
1085 tmp_h = min(2 * tmp_h, (__u32)1920);
1086 pix->width = tmp_w;
1087 pix->height = tmp_h;
1088 pix->pixelformat = xlate->cam_fmt->fourcc;
c9c1f1c0 1089 ret = v4l2_subdev_call(sd, video, s_fmt, f);
961801bb 1090 pix->pixelformat = pixfmt;
0166b743 1091 dev_dbg(icd->dev.parent, "Camera scaled to %ux%u\n",
961801bb
GL
1092 pix->width, pix->height);
1093 if (ret < 0) {
1094 /* This shouldn't happen */
0166b743
GL
1095 dev_err(icd->dev.parent,
1096 "Client failed to set format: %d\n", ret);
961801bb
GL
1097 return ret;
1098 }
1099 }
1100
1101 /* We cannot scale up */
1102 if (width > pix->width)
1103 width = pix->width;
1104
1105 if (height > pix->height)
1106 height = pix->height;
1107
1108 /* Let's rock: scale pix->{width x height} down to width x height */
1109 hscale = calc_scale(pix->width, &width);
1110 vscale = calc_scale(pix->height, &height);
1111
0166b743 1112 dev_dbg(icd->dev.parent, "W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
961801bb
GL
1113 pix->width, hscale, width, pix->height, vscale, height);
1114
1115out:
1116 pcdev->cflcr = hscale | (vscale << 16);
1117
1118 icd->buswidth = xlate->buswidth;
1119 icd->current_fmt = xlate->host_fmt;
1120 cam->camera_fmt = xlate->cam_fmt;
1121 cam->camera_rect.width = pix->width;
1122 cam->camera_rect.height = pix->height;
1123
1124 icd->rect_max.left = size_dst(cam->camera_max.left, hscale);
1125 icd->rect_max.width = size_dst(cam->camera_max.width, hscale);
1126 icd->rect_max.top = size_dst(cam->camera_max.top, vscale);
1127 icd->rect_max.height = size_dst(cam->camera_max.height, vscale);
1128
1129 icd->rect_current.left = icd->rect_max.left;
1130 icd->rect_current.top = icd->rect_max.top;
1131
1132 pcdev->is_interlaced = is_interlaced;
1133
1134 pix->width = width;
1135 pix->height = height;
1136
1137 return 0;
0d3244d6
MD
1138}
1139
d8fac217
GL
1140static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1141 struct v4l2_format *f)
0d3244d6 1142{
9414de39 1143 const struct soc_camera_format_xlate *xlate;
904078f1 1144 struct v4l2_pix_format *pix = &f->fmt.pix;
c9c1f1c0 1145 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
904078f1 1146 __u32 pixfmt = pix->pixelformat;
961801bb 1147 int width, height;
ccab8a29 1148 int ret;
a2c8c68c 1149
9414de39
MD
1150 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1151 if (!xlate) {
0166b743 1152 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
25c4d74e 1153 return -EINVAL;
9414de39 1154 }
25c4d74e 1155
0d3244d6
MD
1156 /* FIXME: calculate using depth and bus width */
1157
904078f1
GL
1158 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1159 &pix->height, 4, 1920, 2, 0);
0d3244d6 1160
961801bb
GL
1161 width = pix->width;
1162 height = pix->height;
1163
904078f1 1164 pix->bytesperline = pix->width *
9414de39 1165 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
904078f1 1166 pix->sizeimage = pix->height * pix->bytesperline;
25c4d74e 1167
904078f1 1168 pix->pixelformat = xlate->cam_fmt->fourcc;
979ea1dd 1169
0d3244d6 1170 /* limit to sensor capabilities */
c9c1f1c0 1171 ret = v4l2_subdev_call(sd, video, try_fmt, f);
904078f1 1172 pix->pixelformat = pixfmt;
ccab8a29
KM
1173 if (ret < 0)
1174 return ret;
1175
961801bb
GL
1176 switch (pixfmt) {
1177 case V4L2_PIX_FMT_NV12:
1178 case V4L2_PIX_FMT_NV21:
1179 case V4L2_PIX_FMT_NV16:
1180 case V4L2_PIX_FMT_NV61:
1181 /* FIXME: check against rect_max after converting soc-camera */
1182 /* We can scale precisely, need a bigger image from camera */
1183 if (pix->width < width || pix->height < height) {
1184 int tmp_w = pix->width, tmp_h = pix->height;
1185 pix->width = 2560;
1186 pix->height = 1920;
c9c1f1c0 1187 ret = v4l2_subdev_call(sd, video, try_fmt, f);
961801bb
GL
1188 if (ret < 0) {
1189 /* Shouldn't actually happen... */
0166b743 1190 dev_err(icd->dev.parent,
961801bb
GL
1191 "FIXME: try_fmt() returned %d\n", ret);
1192 pix->width = tmp_w;
1193 pix->height = tmp_h;
1194 }
1195 }
1196 if (pix->width > width)
1197 pix->width = width;
1198 if (pix->height > height)
1199 pix->height = height;
ccab8a29
KM
1200 }
1201
1202 return ret;
0d3244d6
MD
1203}
1204
1205static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
1206 struct v4l2_requestbuffers *p)
1207{
1208 int i;
1209
1210 /* This is for locking debugging only. I removed spinlocks and now I
1211 * check whether .prepare is ever called on a linked buffer, or whether
1212 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1213 * it hadn't triggered */
1214 for (i = 0; i < p->count; i++) {
1215 struct sh_mobile_ceu_buffer *buf;
1216
1217 buf = container_of(icf->vb_vidq.bufs[i],
1218 struct sh_mobile_ceu_buffer, vb);
1219 INIT_LIST_HEAD(&buf->vb.queue);
1220 }
1221
1222 return 0;
1223}
1224
1225static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1226{
1227 struct soc_camera_file *icf = file->private_data;
1228 struct sh_mobile_ceu_buffer *buf;
1229
1230 buf = list_entry(icf->vb_vidq.stream.next,
1231 struct sh_mobile_ceu_buffer, vb.stream);
1232
1233 poll_wait(file, &buf->vb.done, pt);
1234
1235 if (buf->vb.state == VIDEOBUF_DONE ||
1236 buf->vb.state == VIDEOBUF_ERROR)
1237 return POLLIN|POLLRDNORM;
1238
1239 return 0;
1240}
1241
1242static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1243 struct v4l2_capability *cap)
1244{
1245 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1246 cap->version = KERNEL_VERSION(0, 0, 5);
1247 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1248 return 0;
1249}
1250
1251static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
1252 struct soc_camera_device *icd)
1253{
1254 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1255 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1256
1257 videobuf_queue_dma_contig_init(q,
1258 &sh_mobile_ceu_videobuf_ops,
0166b743 1259 icd->dev.parent, &pcdev->lock,
0d3244d6 1260 V4L2_BUF_TYPE_VIDEO_CAPTURE,
e802967c
GL
1261 pcdev->is_interlaced ?
1262 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
0d3244d6
MD
1263 sizeof(struct sh_mobile_ceu_buffer),
1264 icd);
1265}
1266
4a6110bc
GL
1267static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1268 struct v4l2_control *ctrl)
1269{
1270 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1271 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1272 u32 val;
1273
1274 switch (ctrl->id) {
1275 case V4L2_CID_SHARPNESS:
1276 val = ceu_read(pcdev, CLFCR);
1277 ctrl->value = val ^ 1;
1278 return 0;
1279 }
1280 return -ENOIOCTLCMD;
1281}
1282
1283static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1284 struct v4l2_control *ctrl)
1285{
1286 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1287 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1288
1289 switch (ctrl->id) {
1290 case V4L2_CID_SHARPNESS:
1291 switch (icd->current_fmt->fourcc) {
1292 case V4L2_PIX_FMT_NV12:
1293 case V4L2_PIX_FMT_NV21:
1294 case V4L2_PIX_FMT_NV16:
1295 case V4L2_PIX_FMT_NV61:
1296 ceu_write(pcdev, CLFCR, !ctrl->value);
1297 return 0;
1298 }
1299 return -EINVAL;
1300 }
1301 return -ENOIOCTLCMD;
1302}
1303
1304static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1305 {
1306 .id = V4L2_CID_SHARPNESS,
1307 .type = V4L2_CTRL_TYPE_BOOLEAN,
1308 .name = "Low-pass filter",
1309 .minimum = 0,
1310 .maximum = 1,
1311 .step = 1,
1312 .default_value = 0,
1313 },
1314};
1315
0d3244d6
MD
1316static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1317 .owner = THIS_MODULE,
1318 .add = sh_mobile_ceu_add_device,
1319 .remove = sh_mobile_ceu_remove_device,
9414de39 1320 .get_formats = sh_mobile_ceu_get_formats,
904078f1 1321 .put_formats = sh_mobile_ceu_put_formats,
09e231b3 1322 .set_crop = sh_mobile_ceu_set_crop,
d8fac217
GL
1323 .set_fmt = sh_mobile_ceu_set_fmt,
1324 .try_fmt = sh_mobile_ceu_try_fmt,
4a6110bc
GL
1325 .set_ctrl = sh_mobile_ceu_set_ctrl,
1326 .get_ctrl = sh_mobile_ceu_get_ctrl,
0d3244d6
MD
1327 .reqbufs = sh_mobile_ceu_reqbufs,
1328 .poll = sh_mobile_ceu_poll,
1329 .querycap = sh_mobile_ceu_querycap,
0d3244d6
MD
1330 .set_bus_param = sh_mobile_ceu_set_bus_param,
1331 .init_videobuf = sh_mobile_ceu_init_videobuf,
4a6110bc
GL
1332 .controls = sh_mobile_ceu_controls,
1333 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
0d3244d6
MD
1334};
1335
979ea1dd 1336static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
0d3244d6
MD
1337{
1338 struct sh_mobile_ceu_dev *pcdev;
1339 struct resource *res;
1340 void __iomem *base;
1341 unsigned int irq;
1342 int err = 0;
1343
1344 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1345 irq = platform_get_irq(pdev, 0);
1346 if (!res || !irq) {
1347 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1348 err = -ENODEV;
1349 goto exit;
1350 }
1351
1352 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1353 if (!pcdev) {
1354 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1355 err = -ENOMEM;
1356 goto exit;
1357 }
1358
0d3244d6
MD
1359 INIT_LIST_HEAD(&pcdev->capture);
1360 spin_lock_init(&pcdev->lock);
1361
1362 pcdev->pdata = pdev->dev.platform_data;
1363 if (!pcdev->pdata) {
1364 err = -EINVAL;
1365 dev_err(&pdev->dev, "CEU platform data not set.\n");
1366 goto exit_kfree;
1367 }
1368
eb6c8558 1369 base = ioremap_nocache(res->start, resource_size(res));
0d3244d6
MD
1370 if (!base) {
1371 err = -ENXIO;
1372 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1373 goto exit_kfree;
1374 }
1375
1376 pcdev->irq = irq;
1377 pcdev->base = base;
1378 pcdev->video_limit = 0; /* only enabled if second resource exists */
0d3244d6
MD
1379
1380 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1381 if (res) {
1382 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1383 res->start,
eb6c8558 1384 resource_size(res),
0d3244d6
MD
1385 DMA_MEMORY_MAP |
1386 DMA_MEMORY_EXCLUSIVE);
1387 if (!err) {
1388 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1389 err = -ENXIO;
1390 goto exit_iounmap;
1391 }
1392
eb6c8558 1393 pcdev->video_limit = resource_size(res);
0d3244d6
MD
1394 }
1395
1396 /* request irq */
1397 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
af128a10 1398 dev_name(&pdev->dev), pcdev);
0d3244d6
MD
1399 if (err) {
1400 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1401 goto exit_release_mem;
1402 }
1403
6d1386c6
MD
1404 pm_suspend_ignore_children(&pdev->dev, true);
1405 pm_runtime_enable(&pdev->dev);
1406 pm_runtime_resume(&pdev->dev);
a42b6dd6 1407
0d3244d6 1408 pcdev->ici.priv = pcdev;
979ea1dd 1409 pcdev->ici.v4l2_dev.dev = &pdev->dev;
0d3244d6 1410 pcdev->ici.nr = pdev->id;
af128a10
KS
1411 pcdev->ici.drv_name = dev_name(&pdev->dev);
1412 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
0d3244d6
MD
1413
1414 err = soc_camera_host_register(&pcdev->ici);
1415 if (err)
6d1386c6 1416 goto exit_free_irq;
0d3244d6
MD
1417
1418 return 0;
1419
1420exit_free_irq:
1421 free_irq(pcdev->irq, pcdev);
1422exit_release_mem:
1423 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1424 dma_release_declared_memory(&pdev->dev);
1425exit_iounmap:
1426 iounmap(base);
1427exit_kfree:
1428 kfree(pcdev);
1429exit:
1430 return err;
1431}
1432
979ea1dd 1433static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
0d3244d6 1434{
eff505fa
GL
1435 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1436 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
1437 struct sh_mobile_ceu_dev, ici);
0d3244d6 1438
eff505fa 1439 soc_camera_host_unregister(soc_host);
0d3244d6
MD
1440 free_irq(pcdev->irq, pcdev);
1441 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1442 dma_release_declared_memory(&pdev->dev);
1443 iounmap(pcdev->base);
1444 kfree(pcdev);
1445 return 0;
1446}
1447
6d1386c6
MD
1448static int sh_mobile_ceu_runtime_nop(struct device *dev)
1449{
1450 /* Runtime PM callback shared between ->runtime_suspend()
1451 * and ->runtime_resume(). Simply returns success.
1452 *
1453 * This driver re-initializes all registers after
1454 * pm_runtime_get_sync() anyway so there is no need
1455 * to save and restore registers here.
1456 */
1457 return 0;
1458}
1459
1460static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
1461 .runtime_suspend = sh_mobile_ceu_runtime_nop,
1462 .runtime_resume = sh_mobile_ceu_runtime_nop,
1463};
1464
0d3244d6
MD
1465static struct platform_driver sh_mobile_ceu_driver = {
1466 .driver = {
1467 .name = "sh_mobile_ceu",
6d1386c6 1468 .pm = &sh_mobile_ceu_dev_pm_ops,
0d3244d6
MD
1469 },
1470 .probe = sh_mobile_ceu_probe,
979ea1dd 1471 .remove = __exit_p(sh_mobile_ceu_remove),
0d3244d6
MD
1472};
1473
1474static int __init sh_mobile_ceu_init(void)
1475{
1476 return platform_driver_register(&sh_mobile_ceu_driver);
1477}
1478
1479static void __exit sh_mobile_ceu_exit(void)
1480{
01c1e4ca 1481 platform_driver_unregister(&sh_mobile_ceu_driver);
0d3244d6
MD
1482}
1483
1484module_init(sh_mobile_ceu_init);
1485module_exit(sh_mobile_ceu_exit);
1486
1487MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1488MODULE_AUTHOR("Magnus Damm");
1489MODULE_LICENSE("GPL");
40e2e092 1490MODULE_ALIAS("platform:sh_mobile_ceu");
This page took 0.312015 seconds and 5 git commands to generate.