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