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