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