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