const: constify remaining dev_pm_ops
[deliverable/linux.git] / drivers / media / video / sh_mobile_ceu_camera.c
CommitLineData
0d3244d6
MD
1/*
2 * V4L2 Driver for SuperH Mobile CEU interface
3 *
4 * Copyright (C) 2008 Magnus Damm
5 *
6 * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
7 *
8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 */
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/io.h>
20#include <linux/delay.h>
21#include <linux/dma-mapping.h>
22#include <linux/errno.h>
23#include <linux/fs.h>
24#include <linux/interrupt.h>
25#include <linux/kernel.h>
26#include <linux/mm.h>
27#include <linux/moduleparam.h>
28#include <linux/time.h>
29#include <linux/version.h>
30#include <linux/device.h>
31#include <linux/platform_device.h>
0d3244d6 32#include <linux/videodev2.h>
6d1386c6 33#include <linux/pm_runtime.h>
f39c1ab3 34#include <linux/sched.h>
0d3244d6
MD
35
36#include <media/v4l2-common.h>
37#include <media/v4l2-dev.h>
38#include <media/soc_camera.h>
39#include <media/sh_mobile_ceu.h>
40#include <media/videobuf-dma-contig.h>
41
42/* register offsets for sh7722 / sh7723 */
43
dd54203b
MD
44#define CAPSR 0x00 /* Capture start register */
45#define CAPCR 0x04 /* Capture control register */
46#define CAMCR 0x08 /* Capture interface control register */
47#define CMCYR 0x0c /* Capture interface cycle register */
48#define CAMOR 0x10 /* Capture interface offset register */
49#define CAPWR 0x14 /* Capture interface width register */
50#define CAIFR 0x18 /* Capture interface input format register */
51#define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
52#define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
53#define CRCNTR 0x28 /* CEU register control register */
54#define CRCMPR 0x2c /* CEU register forcible control register */
55#define CFLCR 0x30 /* Capture filter control register */
56#define CFSZR 0x34 /* Capture filter size clip register */
57#define CDWDR 0x38 /* Capture destination width register */
58#define CDAYR 0x3c /* Capture data address Y register */
59#define CDACR 0x40 /* Capture data address C register */
60#define CDBYR 0x44 /* Capture data bottom-field address Y register */
61#define CDBCR 0x48 /* Capture data bottom-field address C register */
62#define CBDSR 0x4c /* Capture bundle destination size register */
63#define CFWCR 0x5c /* Firewall operation control register */
64#define CLFCR 0x60 /* Capture low-pass filter control register */
65#define CDOCR 0x64 /* Capture data output control register */
66#define CDDCR 0x68 /* Capture data complexity level register */
67#define CDDAR 0x6c /* Capture data complexity level address register */
68#define CEIER 0x70 /* Capture event interrupt enable register */
69#define CETCR 0x74 /* Capture event flag clear register */
70#define CSTSR 0x7c /* Capture status register */
71#define CSRTR 0x80 /* Capture software reset register */
72#define CDSSR 0x84 /* Capture data size register */
73#define CDAYR2 0x90 /* Capture data address Y register 2 */
74#define CDACR2 0x94 /* Capture data address C register 2 */
75#define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
76#define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
0d3244d6 77
6a6c8786
GL
78#undef DEBUG_GEOMETRY
79#ifdef DEBUG_GEOMETRY
80#define dev_geo dev_info
81#else
82#define dev_geo dev_dbg
83#endif
84
0d3244d6
MD
85/* per video frame buffer */
86struct sh_mobile_ceu_buffer {
87 struct videobuf_buffer vb; /* v4l buffer must be first */
88 const struct soc_camera_data_format *fmt;
89};
90
91struct sh_mobile_ceu_dev {
0d3244d6
MD
92 struct soc_camera_host ici;
93 struct soc_camera_device *icd;
94
95 unsigned int irq;
96 void __iomem *base;
97 unsigned long video_limit;
98
c60f2b5c 99 /* lock used to protect videobuf */
0d3244d6
MD
100 spinlock_t lock;
101 struct list_head capture;
102 struct videobuf_buffer *active;
103
104 struct sh_mobile_ceu_info *pdata;
8be65dc5 105
961801bb
GL
106 u32 cflcr;
107
904078f1
GL
108 unsigned int is_interlaced:1;
109 unsigned int image_mode:1;
110 unsigned int is_16bit:1;
111};
112
113struct sh_mobile_ceu_cam {
6a6c8786
GL
114 struct v4l2_rect ceu_rect;
115 unsigned int cam_width;
116 unsigned int cam_height;
904078f1 117 const struct soc_camera_data_format *extra_fmt;
8be65dc5 118 const struct soc_camera_data_format *camera_fmt;
0d3244d6
MD
119};
120
c354b400
KM
121static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
122{
123 unsigned long flags;
124
125 flags = SOCAM_MASTER |
126 SOCAM_PCLK_SAMPLE_RISING |
127 SOCAM_HSYNC_ACTIVE_HIGH |
128 SOCAM_HSYNC_ACTIVE_LOW |
129 SOCAM_VSYNC_ACTIVE_HIGH |
130 SOCAM_VSYNC_ACTIVE_LOW |
131 SOCAM_DATA_ACTIVE_HIGH;
132
133 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
134 flags |= SOCAM_DATAWIDTH_8;
135
136 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
137 flags |= SOCAM_DATAWIDTH_16;
138
139 if (flags & SOCAM_DATAWIDTH_MASK)
140 return flags;
141
142 return 0;
143}
144
0d3244d6 145static void ceu_write(struct sh_mobile_ceu_dev *priv,
7a1a8164 146 unsigned long reg_offs, u32 data)
0d3244d6
MD
147{
148 iowrite32(data, priv->base + reg_offs);
149}
150
7a1a8164 151static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
0d3244d6
MD
152{
153 return ioread32(priv->base + reg_offs);
154}
155
b1de7aeb
KM
156static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
157{
158 int i, success = 0;
159 struct soc_camera_device *icd = pcdev->icd;
160
161 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
162
163 /* wait CSTSR.CPTON bit */
164 for (i = 0; i < 1000; i++) {
165 if (!(ceu_read(pcdev, CSTSR) & 1)) {
166 success++;
167 break;
168 }
169 udelay(1);
170 }
171
172 /* wait CAPSR.CPKIL bit */
173 for (i = 0; i < 1000; i++) {
174 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
175 success++;
176 break;
177 }
178 udelay(1);
179 }
180
181
182 if (2 != success) {
183 dev_warn(&icd->dev, "soft reset time out\n");
184 return -EIO;
185 }
186
187 return 0;
188}
189
0d3244d6
MD
190/*
191 * Videobuf operations
192 */
193static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
194 unsigned int *count,
195 unsigned int *size)
196{
197 struct soc_camera_device *icd = vq->priv_data;
198 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
199 struct sh_mobile_ceu_dev *pcdev = ici->priv;
200 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
201
6a6c8786 202 *size = PAGE_ALIGN(icd->user_width * icd->user_height *
a0705b07 203 bytes_per_pixel);
0d3244d6
MD
204
205 if (0 == *count)
206 *count = 2;
207
208 if (pcdev->video_limit) {
209 while (*size * *count > pcdev->video_limit)
210 (*count)--;
211 }
212
0166b743 213 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
0d3244d6
MD
214
215 return 0;
216}
217
218static void free_buffer(struct videobuf_queue *vq,
219 struct sh_mobile_ceu_buffer *buf)
220{
221 struct soc_camera_device *icd = vq->priv_data;
6a6c8786 222 struct device *dev = icd->dev.parent;
0d3244d6 223
6a6c8786 224 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
225 &buf->vb, buf->vb.baddr, buf->vb.bsize);
226
227 if (in_interrupt())
228 BUG();
229
97215cbd 230 videobuf_waiton(&buf->vb, 0, 0);
0d3244d6 231 videobuf_dma_contig_free(vq, &buf->vb);
6a6c8786 232 dev_dbg(dev, "%s freed\n", __func__);
0d3244d6
MD
233 buf->vb.state = VIDEOBUF_NEEDS_INIT;
234}
235
7a1a8164
MD
236#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
237#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
238#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
5cf93f1d 239#define CEU_CEIER_VBP (1 << 20) /* vbp error */
7a1a8164 240#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
5cf93f1d 241#define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
7a1a8164
MD
242
243
5cf93f1d
KM
244/*
245 * return value doesn't reflex the success/failure to queue the new buffer,
246 * but rather the status of the previous buffer.
247 */
248static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
0d3244d6 249{
8be65dc5 250 struct soc_camera_device *icd = pcdev->icd;
ccab8a29 251 dma_addr_t phys_addr_top, phys_addr_bottom;
5cf93f1d
KM
252 u32 status;
253 int ret = 0;
7a1a8164
MD
254
255 /* The hardware is _very_ picky about this sequence. Especially
256 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
257 * several not-so-well documented interrupt sources in CETCR.
258 */
5cf93f1d
KM
259 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
260 status = ceu_read(pcdev, CETCR);
261 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
262 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
7a1a8164
MD
263 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
264 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
0d3244d6 265
5cf93f1d
KM
266 /*
267 * When a VBP interrupt occurs, a capture end interrupt does not occur
268 * and the image of that frame is not captured correctly. So, soft reset
269 * is needed here.
270 */
271 if (status & CEU_CEIER_VBP) {
272 sh_mobile_ceu_soft_reset(pcdev);
273 ret = -EIO;
274 }
275
8be65dc5 276 if (!pcdev->active)
5cf93f1d 277 return ret;
8be65dc5 278
ccab8a29
KM
279 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
280 ceu_write(pcdev, CDAYR, phys_addr_top);
e802967c 281 if (pcdev->is_interlaced) {
6a6c8786 282 phys_addr_bottom = phys_addr_top + icd->user_width;
ccab8a29
KM
283 ceu_write(pcdev, CDBYR, phys_addr_bottom);
284 }
8be65dc5
MD
285
286 switch (icd->current_fmt->fourcc) {
287 case V4L2_PIX_FMT_NV12:
288 case V4L2_PIX_FMT_NV21:
9e4a56d2
MD
289 case V4L2_PIX_FMT_NV16:
290 case V4L2_PIX_FMT_NV61:
6a6c8786
GL
291 phys_addr_top += icd->user_width *
292 icd->user_height;
ccab8a29 293 ceu_write(pcdev, CDACR, phys_addr_top);
e802967c 294 if (pcdev->is_interlaced) {
a0705b07 295 phys_addr_bottom = phys_addr_top +
6a6c8786 296 icd->user_width;
ccab8a29
KM
297 ceu_write(pcdev, CDBCR, phys_addr_bottom);
298 }
0d3244d6 299 }
8be65dc5
MD
300
301 pcdev->active->state = VIDEOBUF_ACTIVE;
302 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
5cf93f1d
KM
303
304 return ret;
0d3244d6
MD
305}
306
307static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
308 struct videobuf_buffer *vb,
309 enum v4l2_field field)
310{
311 struct soc_camera_device *icd = vq->priv_data;
312 struct sh_mobile_ceu_buffer *buf;
313 int ret;
314
315 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
316
0166b743 317 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
318 vb, vb->baddr, vb->bsize);
319
320 /* Added list head initialization on alloc */
321 WARN_ON(!list_empty(&vb->queue));
322
323#ifdef DEBUG
324 /* This can be useful if you want to see if we actually fill
325 * the buffer with something */
326 memset((void *)vb->baddr, 0xaa, vb->bsize);
327#endif
328
329 BUG_ON(NULL == icd->current_fmt);
330
331 if (buf->fmt != icd->current_fmt ||
6a6c8786
GL
332 vb->width != icd->user_width ||
333 vb->height != icd->user_height ||
0d3244d6
MD
334 vb->field != field) {
335 buf->fmt = icd->current_fmt;
6a6c8786
GL
336 vb->width = icd->user_width;
337 vb->height = icd->user_height;
0d3244d6
MD
338 vb->field = field;
339 vb->state = VIDEOBUF_NEEDS_INIT;
340 }
341
342 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
343 if (0 != vb->baddr && vb->bsize < vb->size) {
344 ret = -EINVAL;
345 goto out;
346 }
347
348 if (vb->state == VIDEOBUF_NEEDS_INIT) {
349 ret = videobuf_iolock(vq, vb, NULL);
350 if (ret)
351 goto fail;
352 vb->state = VIDEOBUF_PREPARED;
353 }
354
355 return 0;
356fail:
357 free_buffer(vq, buf);
358out:
359 return ret;
360}
361
2dd54a54 362/* Called under spinlock_irqsave(&pcdev->lock, ...) */
0d3244d6
MD
363static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
364 struct videobuf_buffer *vb)
365{
366 struct soc_camera_device *icd = vq->priv_data;
367 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
368 struct sh_mobile_ceu_dev *pcdev = ici->priv;
0d3244d6 369
0166b743 370 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
371 vb, vb->baddr, vb->bsize);
372
51354cc3 373 vb->state = VIDEOBUF_QUEUED;
0d3244d6
MD
374 list_add_tail(&vb->queue, &pcdev->capture);
375
376 if (!pcdev->active) {
5cf93f1d
KM
377 /*
378 * Because there were no active buffer at this moment,
379 * we are not interested in the return value of
380 * sh_mobile_ceu_capture here.
381 */
0d3244d6
MD
382 pcdev->active = vb;
383 sh_mobile_ceu_capture(pcdev);
384 }
0d3244d6
MD
385}
386
387static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
388 struct videobuf_buffer *vb)
389{
cca0e549
GL
390 struct soc_camera_device *icd = vq->priv_data;
391 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
392 struct sh_mobile_ceu_dev *pcdev = ici->priv;
393 unsigned long flags;
394
395 spin_lock_irqsave(&pcdev->lock, flags);
396
397 if (pcdev->active == vb) {
398 /* disable capture (release DMA buffer), reset */
399 ceu_write(pcdev, CAPSR, 1 << 16);
400 pcdev->active = NULL;
401 }
402
403 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
404 !list_empty(&vb->queue)) {
405 vb->state = VIDEOBUF_ERROR;
406 list_del_init(&vb->queue);
407 }
408
409 spin_unlock_irqrestore(&pcdev->lock, flags);
410
0d3244d6
MD
411 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
412}
413
414static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
415 .buf_setup = sh_mobile_ceu_videobuf_setup,
416 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
417 .buf_queue = sh_mobile_ceu_videobuf_queue,
418 .buf_release = sh_mobile_ceu_videobuf_release,
419};
420
421static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
422{
423 struct sh_mobile_ceu_dev *pcdev = data;
424 struct videobuf_buffer *vb;
425 unsigned long flags;
426
427 spin_lock_irqsave(&pcdev->lock, flags);
428
429 vb = pcdev->active;
cca0e549
GL
430 if (!vb)
431 /* Stale interrupt from a released buffer */
432 goto out;
433
0d3244d6
MD
434 list_del_init(&vb->queue);
435
436 if (!list_empty(&pcdev->capture))
437 pcdev->active = list_entry(pcdev->capture.next,
438 struct videobuf_buffer, queue);
439 else
440 pcdev->active = NULL;
441
5cf93f1d
KM
442 vb->state = (sh_mobile_ceu_capture(pcdev) < 0) ?
443 VIDEOBUF_ERROR : VIDEOBUF_DONE;
0d3244d6
MD
444 do_gettimeofday(&vb->ts);
445 vb->field_count++;
446 wake_up(&vb->done);
cca0e549
GL
447
448out:
0d3244d6
MD
449 spin_unlock_irqrestore(&pcdev->lock, flags);
450
451 return IRQ_HANDLED;
452}
453
1c3bb743 454/* Called with .video_lock held */
0d3244d6
MD
455static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
456{
457 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
458 struct sh_mobile_ceu_dev *pcdev = ici->priv;
0d3244d6 459
0d3244d6 460 if (pcdev->icd)
979ea1dd 461 return -EBUSY;
0d3244d6 462
0166b743 463 dev_info(icd->dev.parent,
0d3244d6
MD
464 "SuperH Mobile CEU driver attached to camera %d\n",
465 icd->devnum);
466
a79aebfc 467 pm_runtime_get_sync(ici->v4l2_dev.dev);
40e2e092 468
0d3244d6 469 pcdev->icd = icd;
979ea1dd 470
b1de7aeb 471 return sh_mobile_ceu_soft_reset(pcdev);
0d3244d6
MD
472}
473
1c3bb743 474/* Called with .video_lock held */
0d3244d6
MD
475static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
476{
477 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
478 struct sh_mobile_ceu_dev *pcdev = ici->priv;
51354cc3 479 unsigned long flags;
0d3244d6
MD
480
481 BUG_ON(icd != pcdev->icd);
482
483 /* disable capture, disable interrupts */
484 ceu_write(pcdev, CEIER, 0);
b1de7aeb 485 sh_mobile_ceu_soft_reset(pcdev);
51354cc3
MD
486
487 /* make sure active buffer is canceled */
488 spin_lock_irqsave(&pcdev->lock, flags);
489 if (pcdev->active) {
490 list_del(&pcdev->active->queue);
491 pcdev->active->state = VIDEOBUF_ERROR;
492 wake_up_all(&pcdev->active->done);
493 pcdev->active = NULL;
494 }
495 spin_unlock_irqrestore(&pcdev->lock, flags);
496
a79aebfc 497 pm_runtime_put_sync(ici->v4l2_dev.dev);
40e2e092 498
0166b743 499 dev_info(icd->dev.parent,
0d3244d6
MD
500 "SuperH Mobile CEU driver detached from camera %d\n",
501 icd->devnum);
502
503 pcdev->icd = NULL;
504}
505
961801bb
GL
506/*
507 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
508 * in SH7722 Hardware Manual
509 */
510static unsigned int size_dst(unsigned int src, unsigned int scale)
511{
512 unsigned int mant_pre = scale >> 12;
513 if (!src || !scale)
514 return src;
515 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
516 mant_pre * 4096 / scale + 1;
517}
518
961801bb
GL
519static u16 calc_scale(unsigned int src, unsigned int *dst)
520{
521 u16 scale;
522
523 if (src == *dst)
524 return 0;
525
526 scale = (src * 4096 / *dst) & ~7;
527
528 while (scale > 4096 && size_dst(src, scale) < *dst)
529 scale -= 8;
530
531 *dst = size_dst(src, scale);
532
533 return scale;
534}
535
536/* rect is guaranteed to not exceed the scaled camera rectangle */
904078f1 537static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
6a6c8786
GL
538 unsigned int out_width,
539 unsigned int out_height)
904078f1
GL
540{
541 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
542 struct sh_mobile_ceu_cam *cam = icd->host_priv;
6a6c8786 543 struct v4l2_rect *rect = &cam->ceu_rect;
904078f1 544 struct sh_mobile_ceu_dev *pcdev = ici->priv;
6a6c8786
GL
545 unsigned int height, width, cdwdr_width, in_width, in_height;
546 unsigned int left_offset, top_offset;
904078f1
GL
547 u32 camor;
548
6a6c8786
GL
549 dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
550 rect->width, rect->height, rect->left, rect->top);
904078f1 551
6a6c8786
GL
552 left_offset = rect->left;
553 top_offset = rect->top;
904078f1
GL
554
555 if (pcdev->image_mode) {
6a6c8786 556 in_width = rect->width;
961801bb 557 if (!pcdev->is_16bit) {
961801bb
GL
558 in_width *= 2;
559 left_offset *= 2;
560 }
6a6c8786 561 width = cdwdr_width = out_width;
904078f1 562 } else {
961801bb 563 unsigned int w_factor = (icd->current_fmt->depth + 7) >> 3;
6a6c8786
GL
564
565 width = out_width * w_factor / 2;
566
961801bb
GL
567 if (!pcdev->is_16bit)
568 w_factor *= 2;
569
6a6c8786 570 in_width = rect->width * w_factor / 2;
961801bb
GL
571 left_offset = left_offset * w_factor / 2;
572
6a6c8786 573 cdwdr_width = width * 2;
904078f1
GL
574 }
575
6a6c8786
GL
576 height = out_height;
577 in_height = rect->height;
904078f1
GL
578 if (pcdev->is_interlaced) {
579 height /= 2;
961801bb
GL
580 in_height /= 2;
581 top_offset /= 2;
904078f1
GL
582 cdwdr_width *= 2;
583 }
584
6a6c8786 585 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
904078f1 586 camor = left_offset | (top_offset << 16);
6a6c8786
GL
587
588 dev_geo(icd->dev.parent,
589 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
590 (in_height << 16) | in_width, (height << 16) | width,
591 cdwdr_width);
592
904078f1 593 ceu_write(pcdev, CAMOR, camor);
961801bb 594 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
6a6c8786 595 ceu_write(pcdev, CFSZR, (height << 16) | width);
904078f1
GL
596 ceu_write(pcdev, CDWDR, cdwdr_width);
597}
598
599static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
600{
601 u32 capsr = ceu_read(pcdev, CAPSR);
602 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
603 return capsr;
604}
605
606static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
607{
608 unsigned long timeout = jiffies + 10 * HZ;
609
610 /*
611 * Wait until the end of the current frame. It can take a long time,
612 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
613 */
614 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
615 msleep(1);
616
617 if (time_after(jiffies, timeout)) {
618 dev_err(pcdev->ici.v4l2_dev.dev,
619 "Timeout waiting for frame end! Interface problem?\n");
620 return;
621 }
622
623 /* Wait until reset clears, this shall not hang... */
624 while (ceu_read(pcdev, CAPSR) & (1 << 16))
625 udelay(10);
626
627 /* Anything to restore? */
628 if (capsr & ~(1 << 16))
629 ceu_write(pcdev, CAPSR, capsr);
630}
631
0d3244d6
MD
632static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
633 __u32 pixfmt)
634{
635 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
636 struct sh_mobile_ceu_dev *pcdev = ici->priv;
904078f1 637 int ret;
0d3244d6 638 unsigned long camera_flags, common_flags, value;
904078f1
GL
639 int yuv_lineskip;
640 struct sh_mobile_ceu_cam *cam = icd->host_priv;
641 u32 capsr = capture_save_reset(pcdev);
0d3244d6
MD
642
643 camera_flags = icd->ops->query_bus_param(icd);
644 common_flags = soc_camera_bus_param_compatible(camera_flags,
c354b400 645 make_bus_param(pcdev));
0d3244d6
MD
646 if (!common_flags)
647 return -EINVAL;
648
649 ret = icd->ops->set_bus_param(icd, common_flags);
650 if (ret < 0)
651 return ret;
652
653 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
654 case SOCAM_DATAWIDTH_8:
904078f1 655 pcdev->is_16bit = 0;
0d3244d6
MD
656 break;
657 case SOCAM_DATAWIDTH_16:
904078f1 658 pcdev->is_16bit = 1;
0d3244d6
MD
659 break;
660 default:
661 return -EINVAL;
662 }
663
664 ceu_write(pcdev, CRCNTR, 0);
665 ceu_write(pcdev, CRCMPR, 0);
666
8be65dc5 667 value = 0x00000010; /* data fetch by default */
961801bb 668 yuv_lineskip = 0;
8be65dc5
MD
669
670 switch (icd->current_fmt->fourcc) {
671 case V4L2_PIX_FMT_NV12:
672 case V4L2_PIX_FMT_NV21:
673 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
9e4a56d2
MD
674 /* fall-through */
675 case V4L2_PIX_FMT_NV16:
676 case V4L2_PIX_FMT_NV61:
904078f1 677 switch (cam->camera_fmt->fourcc) {
8be65dc5
MD
678 case V4L2_PIX_FMT_UYVY:
679 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
680 break;
681 case V4L2_PIX_FMT_VYUY:
682 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
683 break;
684 case V4L2_PIX_FMT_YUYV:
685 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
686 break;
687 case V4L2_PIX_FMT_YVYU:
688 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
689 break;
690 default:
691 BUG();
692 }
693 }
694
7a1a8164
MD
695 if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
696 icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
9e4a56d2 697 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
8be65dc5 698
7a1a8164
MD
699 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
700 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
904078f1 701 value |= pcdev->is_16bit ? 1 << 12 : 0;
0d3244d6
MD
702 ceu_write(pcdev, CAMCR, value);
703
704 ceu_write(pcdev, CAPCR, 0x00300000);
e802967c 705 ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
0d3244d6 706
6a6c8786 707 sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
0d3244d6
MD
708 mdelay(1);
709
961801bb 710 ceu_write(pcdev, CFLCR, pcdev->cflcr);
dd54203b
MD
711
712 /* A few words about byte order (observed in Big Endian mode)
713 *
714 * In data fetch mode bytes are received in chunks of 8 bytes.
715 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
716 *
717 * The data is however by default written to memory in reverse order:
718 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
719 *
720 * The lowest three bits of CDOCR allows us to do swapping,
2c0a072e
MD
721 * using 7 we swap the data bytes to match the incoming order:
722 * D0, D1, D2, D3, D4, D5, D6, D7
dd54203b 723 */
8be65dc5
MD
724 value = 0x00000017;
725 if (yuv_lineskip)
726 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
727
728 ceu_write(pcdev, CDOCR, value);
0d3244d6
MD
729 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
730
6a6c8786 731 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
904078f1
GL
732 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
733 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
6a6c8786 734 icd->user_width, icd->user_height);
904078f1
GL
735
736 capture_restore(pcdev, capsr);
737
0d3244d6 738 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
0d3244d6
MD
739 return 0;
740}
741
9414de39 742static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
0d3244d6
MD
743{
744 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
745 struct sh_mobile_ceu_dev *pcdev = ici->priv;
746 unsigned long camera_flags, common_flags;
747
748 camera_flags = icd->ops->query_bus_param(icd);
749 common_flags = soc_camera_bus_param_compatible(camera_flags,
c354b400 750 make_bus_param(pcdev));
0d3244d6
MD
751 if (!common_flags)
752 return -EINVAL;
753
754 return 0;
755}
756
8be65dc5
MD
757static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
758 {
759 .name = "NV12",
760 .depth = 12,
761 .fourcc = V4L2_PIX_FMT_NV12,
762 .colorspace = V4L2_COLORSPACE_JPEG,
763 },
764 {
765 .name = "NV21",
766 .depth = 12,
767 .fourcc = V4L2_PIX_FMT_NV21,
768 .colorspace = V4L2_COLORSPACE_JPEG,
769 },
9e4a56d2
MD
770 {
771 .name = "NV16",
772 .depth = 16,
773 .fourcc = V4L2_PIX_FMT_NV16,
774 .colorspace = V4L2_COLORSPACE_JPEG,
775 },
776 {
777 .name = "NV61",
778 .depth = 16,
779 .fourcc = V4L2_PIX_FMT_NV61,
780 .colorspace = V4L2_COLORSPACE_JPEG,
781 },
8be65dc5
MD
782};
783
9414de39
MD
784static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
785 struct soc_camera_format_xlate *xlate)
786{
6a6c8786 787 struct device *dev = icd->dev.parent;
8be65dc5 788 int ret, k, n;
9414de39 789 int formats = 0;
904078f1 790 struct sh_mobile_ceu_cam *cam;
9414de39
MD
791
792 ret = sh_mobile_ceu_try_bus_param(icd);
793 if (ret < 0)
794 return 0;
795
904078f1
GL
796 if (!icd->host_priv) {
797 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
798 if (!cam)
799 return -ENOMEM;
800
801 icd->host_priv = cam;
802 } else {
803 cam = icd->host_priv;
804 }
805
c8329acc
GL
806 /* Beginning of a pass */
807 if (!idx)
904078f1 808 cam->extra_fmt = NULL;
c8329acc 809
9414de39 810 switch (icd->formats[idx].fourcc) {
8be65dc5
MD
811 case V4L2_PIX_FMT_UYVY:
812 case V4L2_PIX_FMT_VYUY:
813 case V4L2_PIX_FMT_YUYV:
814 case V4L2_PIX_FMT_YVYU:
904078f1 815 if (cam->extra_fmt)
c8329acc
GL
816 goto add_single_format;
817
818 /*
819 * Our case is simple so far: for any of the above four camera
820 * formats we add all our four synthesized NV* formats, so,
821 * just marking the device with a single flag suffices. If
822 * the format generation rules are more complex, you would have
823 * to actually hang your already added / counted formats onto
824 * the host_priv pointer and check whether the format you're
825 * going to add now is already there.
826 */
904078f1 827 cam->extra_fmt = (void *)sh_mobile_ceu_formats;
c8329acc 828
8be65dc5
MD
829 n = ARRAY_SIZE(sh_mobile_ceu_formats);
830 formats += n;
831 for (k = 0; xlate && k < n; k++) {
832 xlate->host_fmt = &sh_mobile_ceu_formats[k];
833 xlate->cam_fmt = icd->formats + idx;
834 xlate->buswidth = icd->formats[idx].depth;
835 xlate++;
6a6c8786 836 dev_dbg(dev, "Providing format %s using %s\n",
8be65dc5
MD
837 sh_mobile_ceu_formats[k].name,
838 icd->formats[idx].name);
839 }
9414de39 840 default:
c8329acc 841add_single_format:
9414de39
MD
842 /* Generic pass-through */
843 formats++;
844 if (xlate) {
845 xlate->host_fmt = icd->formats + idx;
846 xlate->cam_fmt = icd->formats + idx;
847 xlate->buswidth = icd->formats[idx].depth;
848 xlate++;
6a6c8786 849 dev_dbg(dev,
9414de39
MD
850 "Providing format %s in pass-through mode\n",
851 icd->formats[idx].name);
852 }
853 }
854
855 return formats;
856}
857
904078f1
GL
858static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
859{
860 kfree(icd->host_priv);
861 icd->host_priv = NULL;
862}
863
961801bb 864/* Check if any dimension of r1 is smaller than respective one of r2 */
904078f1
GL
865static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
866{
867 return r1->width < r2->width || r1->height < r2->height;
868}
869
961801bb
GL
870/* Check if r1 fails to cover r2 */
871static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
872{
873 return r1->left > r2->left || r1->top > r2->top ||
874 r1->left + r1->width < r2->left + r2->width ||
875 r1->top + r1->height < r2->top + r2->height;
876}
877
6a6c8786
GL
878static unsigned int scale_down(unsigned int size, unsigned int scale)
879{
880 return (size * 4096 + scale / 2) / scale;
881}
882
883static unsigned int scale_up(unsigned int size, unsigned int scale)
884{
885 return (size * scale + 2048) / 4096;
886}
887
888static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
889{
890 return (input * 4096 + output / 2) / output;
891}
892
893static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
894{
895 struct v4l2_crop crop;
896 struct v4l2_cropcap cap;
897 int ret;
898
899 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
900
901 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
902 if (!ret) {
903 *rect = crop.c;
904 return ret;
905 }
906
907 /* Camera driver doesn't support .g_crop(), assume default rectangle */
908 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
909
910 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
911 if (ret < 0)
912 return ret;
913
914 *rect = cap.defrect;
915
916 return ret;
917}
918
961801bb 919/*
6a6c8786 920 * The common for both scaling and cropping iterative approach is:
961801bb
GL
921 * 1. try if the client can produce exactly what requested by the user
922 * 2. if (1) failed, try to double the client image until we get one big enough
923 * 3. if (2) failed, try to request the maximum image
924 */
6a6c8786
GL
925static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
926 struct v4l2_crop *cam_crop)
09e231b3 927{
6a6c8786
GL
928 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
929 struct device *dev = sd->v4l2_dev->dev;
930 struct v4l2_cropcap cap;
904078f1 931 int ret;
6a6c8786 932 unsigned int width, height;
904078f1 933
6a6c8786
GL
934 v4l2_subdev_call(sd, video, s_crop, crop);
935 ret = client_g_rect(sd, cam_rect);
936 if (ret < 0)
937 return ret;
961801bb 938
6a6c8786
GL
939 /*
940 * Now cam_crop contains the current camera input rectangle, and it must
941 * be within camera cropcap bounds
942 */
943 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
944 /* Even if camera S_CROP failed, but camera rectangle matches */
945 dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n",
946 rect->width, rect->height, rect->left, rect->top);
947 return 0;
904078f1
GL
948 }
949
6a6c8786
GL
950 /* Try to fix cropping, that camera hasn't managed to set */
951 dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
952 cam_rect->width, cam_rect->height,
08590b96 953 cam_rect->left, cam_rect->top,
6a6c8786
GL
954 rect->width, rect->height, rect->left, rect->top);
955
956 /* We need sensor maximum rectangle */
957 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
958 if (ret < 0)
959 return ret;
960
961 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
962 cap.bounds.width);
963 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
964 cap.bounds.height);
904078f1
GL
965
966 /*
967 * Popular special case - some cameras can only handle fixed sizes like
968 * QVGA, VGA,... Take care to avoid infinite loop.
969 */
6a6c8786
GL
970 width = max(cam_rect->width, 2);
971 height = max(cam_rect->height, 2);
972
973 while (!ret && (is_smaller(cam_rect, rect) ||
974 is_inside(cam_rect, rect)) &&
975 (cap.bounds.width > width || cap.bounds.height > height)) {
961801bb
GL
976
977 width *= 2;
978 height *= 2;
6a6c8786 979
08590b96
GL
980 cam_rect->width = width;
981 cam_rect->height = height;
904078f1 982
6a6c8786
GL
983 /*
984 * We do not know what capabilities the camera has to set up
985 * left and top borders. We could try to be smarter in iterating
986 * them, e.g., if camera current left is to the right of the
987 * target left, set it to the middle point between the current
988 * left and minimum left. But that would add too much
989 * complexity: we would have to iterate each border separately.
990 */
991 if (cam_rect->left > rect->left)
992 cam_rect->left = cap.bounds.left;
961801bb 993
6a6c8786
GL
994 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
995 cam_rect->width = rect->left + rect->width -
08590b96 996 cam_rect->left;
961801bb 997
6a6c8786
GL
998 if (cam_rect->top > rect->top)
999 cam_rect->top = cap.bounds.top;
961801bb 1000
6a6c8786
GL
1001 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1002 cam_rect->height = rect->top + rect->height -
08590b96 1003 cam_rect->top;
961801bb 1004
6a6c8786
GL
1005 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1006 ret = client_g_rect(sd, cam_rect);
1007 dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret,
1008 cam_rect->width, cam_rect->height,
08590b96 1009 cam_rect->left, cam_rect->top);
904078f1
GL
1010 }
1011
6a6c8786
GL
1012 /* S_CROP must not modify the rectangle */
1013 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
904078f1
GL
1014 /*
1015 * The camera failed to configure a suitable cropping,
1016 * we cannot use the current rectangle, set to max
1017 */
6a6c8786
GL
1018 *cam_rect = cap.bounds;
1019 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1020 ret = client_g_rect(sd, cam_rect);
1021 dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret,
1022 cam_rect->width, cam_rect->height,
08590b96 1023 cam_rect->left, cam_rect->top);
904078f1
GL
1024 }
1025
6a6c8786
GL
1026 return ret;
1027}
1028
1029static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
1030 unsigned int *scale_h, unsigned int *scale_v)
1031{
1032 struct v4l2_format f;
1033 int ret;
1034
1035 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1036
1037 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
1038 if (ret < 0)
1039 return ret;
1040
1041 *scale_h = calc_generic_scale(rect->width, f.fmt.pix.width);
1042 *scale_v = calc_generic_scale(rect->height, f.fmt.pix.height);
1043
1044 return 0;
1045}
1046
1047static int get_camera_subwin(struct soc_camera_device *icd,
1048 struct v4l2_rect *cam_subrect,
1049 unsigned int cam_hscale, unsigned int cam_vscale)
1050{
1051 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1052 struct v4l2_rect *ceu_rect = &cam->ceu_rect;
1053
1054 if (!ceu_rect->width) {
1055 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1056 struct device *dev = icd->dev.parent;
1057 struct v4l2_format f;
1058 struct v4l2_pix_format *pix = &f.fmt.pix;
1059 int ret;
1060 /* First time */
1061
1062 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1063
1064 ret = v4l2_subdev_call(sd, video, g_fmt, &f);
1065 if (ret < 0)
1066 return ret;
1067
1068 dev_geo(dev, "camera fmt %ux%u\n", pix->width, pix->height);
1069
1070 if (pix->width > 2560) {
1071 ceu_rect->width = 2560;
1072 ceu_rect->left = (pix->width - 2560) / 2;
1073 } else {
1074 ceu_rect->width = pix->width;
1075 ceu_rect->left = 0;
1076 }
1077
1078 if (pix->height > 1920) {
1079 ceu_rect->height = 1920;
1080 ceu_rect->top = (pix->height - 1920) / 2;
1081 } else {
1082 ceu_rect->height = pix->height;
1083 ceu_rect->top = 0;
1084 }
1085
1086 dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
1087 ceu_rect->width, ceu_rect->height,
1088 ceu_rect->left, ceu_rect->top);
1089 }
1090
1091 cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
1092 cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
1093 cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
1094 cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
1095
1096 return 0;
1097}
1098
1099static int client_s_fmt(struct soc_camera_device *icd, struct v4l2_format *f,
1100 bool ceu_can_scale)
1101{
1102 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1103 struct device *dev = icd->dev.parent;
1104 struct v4l2_pix_format *pix = &f->fmt.pix;
1105 unsigned int width = pix->width, height = pix->height, tmp_w, tmp_h;
1106 unsigned int max_width, max_height;
1107 struct v4l2_cropcap cap;
1108 int ret;
1109
1110 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1111
1112 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1113 if (ret < 0)
1114 return ret;
1115
1116 max_width = min(cap.bounds.width, 2560);
1117 max_height = min(cap.bounds.height, 1920);
1118
1119 ret = v4l2_subdev_call(sd, video, s_fmt, f);
1120 if (ret < 0)
1121 return ret;
1122
1123 dev_geo(dev, "camera scaled to %ux%u\n", pix->width, pix->height);
1124
1125 if ((width == pix->width && height == pix->height) || !ceu_can_scale)
1126 return 0;
1127
1128 /* Camera set a format, but geometry is not precise, try to improve */
1129 tmp_w = pix->width;
1130 tmp_h = pix->height;
1131
1132 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1133 while ((width > tmp_w || height > tmp_h) &&
1134 tmp_w < max_width && tmp_h < max_height) {
1135 tmp_w = min(2 * tmp_w, max_width);
1136 tmp_h = min(2 * tmp_h, max_height);
1137 pix->width = tmp_w;
1138 pix->height = tmp_h;
1139 ret = v4l2_subdev_call(sd, video, s_fmt, f);
1140 dev_geo(dev, "Camera scaled to %ux%u\n",
1141 pix->width, pix->height);
1142 if (ret < 0) {
1143 /* This shouldn't happen */
1144 dev_err(dev, "Client failed to set format: %d\n", ret);
1145 return ret;
1146 }
1147 }
1148
1149 return 0;
1150}
1151
1152/**
1153 * @rect - camera cropped rectangle
1154 * @sub_rect - CEU cropped rectangle, mapped back to camera input area
1155 * @ceu_rect - on output calculated CEU crop rectangle
1156 */
1157static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
1158 struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect,
1159 struct v4l2_format *f, bool ceu_can_scale)
1160{
1161 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1162 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1163 struct device *dev = icd->dev.parent;
1164 struct v4l2_format f_tmp = *f;
1165 struct v4l2_pix_format *pix_tmp = &f_tmp.fmt.pix;
1166 unsigned int scale_h, scale_v;
1167 int ret;
1168
1169 /* 5. Apply iterative camera S_FMT for camera user window. */
1170 ret = client_s_fmt(icd, &f_tmp, ceu_can_scale);
1171 if (ret < 0)
1172 return ret;
1173
1174 dev_geo(dev, "5: camera scaled to %ux%u\n",
1175 pix_tmp->width, pix_tmp->height);
1176
1177 /* 6. Retrieve camera output window (g_fmt) */
1178
1179 /* unneeded - it is already in "f_tmp" */
1180
1181 /* 7. Calculate new camera scales. */
1182 ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
1183 if (ret < 0)
1184 return ret;
1185
1186 dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
1187
1188 cam->cam_width = pix_tmp->width;
1189 cam->cam_height = pix_tmp->height;
1190 f->fmt.pix.width = pix_tmp->width;
1191 f->fmt.pix.height = pix_tmp->height;
904078f1
GL
1192
1193 /*
6a6c8786
GL
1194 * 8. Calculate new CEU crop - apply camera scales to previously
1195 * calculated "effective" crop.
904078f1 1196 */
6a6c8786
GL
1197 ceu_rect->left = scale_down(sub_rect->left, scale_h);
1198 ceu_rect->width = scale_down(sub_rect->width, scale_h);
1199 ceu_rect->top = scale_down(sub_rect->top, scale_v);
1200 ceu_rect->height = scale_down(sub_rect->height, scale_v);
1201
1202 dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
1203 ceu_rect->width, ceu_rect->height,
1204 ceu_rect->left, ceu_rect->top);
1205
1206 return 0;
1207}
1208
1209/* Get combined scales */
1210static int get_scales(struct soc_camera_device *icd,
1211 unsigned int *scale_h, unsigned int *scale_v)
1212{
1213 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1214 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1215 struct v4l2_crop cam_crop;
1216 unsigned int width_in, height_in;
1217 int ret;
904078f1 1218
6a6c8786 1219 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
904078f1 1220
6a6c8786
GL
1221 ret = client_g_rect(sd, &cam_crop.c);
1222 if (ret < 0)
1223 return ret;
904078f1 1224
6a6c8786
GL
1225 ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
1226 if (ret < 0)
1227 return ret;
961801bb 1228
6a6c8786
GL
1229 width_in = scale_up(cam->ceu_rect.width, *scale_h);
1230 height_in = scale_up(cam->ceu_rect.height, *scale_v);
961801bb 1231
8cf1fec8
GL
1232 *scale_h = calc_generic_scale(width_in, icd->user_width);
1233 *scale_v = calc_generic_scale(height_in, icd->user_height);
6a6c8786
GL
1234
1235 return 0;
1236}
1237
1238/*
1239 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1240 * framerate by always requesting the maximum image from the client. See
1241 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1242 * scaling and cropping algorithms and for the meaning of referenced here steps.
1243 */
1244static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
1245 struct v4l2_crop *a)
1246{
1247 struct v4l2_rect *rect = &a->c;
1248 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1249 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1250 struct v4l2_crop cam_crop;
1251 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1252 struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
1253 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1254 struct device *dev = icd->dev.parent;
1255 struct v4l2_format f;
1256 struct v4l2_pix_format *pix = &f.fmt.pix;
1257 unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v,
1258 out_width, out_height;
1259 u32 capsr, cflcr;
1260 int ret;
1261
1262 /* 1. Calculate current combined scales. */
1263 ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
1264 if (ret < 0)
1265 return ret;
1266
1267 dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
1268
1269 /* 2. Apply iterative camera S_CROP for new input window. */
1270 ret = client_s_crop(sd, a, &cam_crop);
1271 if (ret < 0)
1272 return ret;
1273
1274 dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n",
1275 cam_rect->width, cam_rect->height,
1276 cam_rect->left, cam_rect->top);
1277
1278 /* On success cam_crop contains current camera crop */
1279
1280 /*
1281 * 3. If old combined scales applied to new crop produce an impossible
1282 * user window, adjust scales to produce nearest possible window.
1283 */
1284 out_width = scale_down(rect->width, scale_comb_h);
1285 out_height = scale_down(rect->height, scale_comb_v);
1286
1287 if (out_width > 2560)
1288 out_width = 2560;
1289 else if (out_width < 2)
1290 out_width = 2;
1291
1292 if (out_height > 1920)
1293 out_height = 1920;
1294 else if (out_height < 4)
1295 out_height = 4;
1296
1297 dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
1298
1299 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1300
1301 /*
1302 * 5. Using actual input window and calculated combined scales calculate
1303 * camera target output window.
1304 */
1305 pix->width = scale_down(cam_rect->width, scale_comb_h);
1306 pix->height = scale_down(cam_rect->height, scale_comb_v);
1307
1308 dev_geo(dev, "5: camera target %ux%u\n", pix->width, pix->height);
1309
1310 /* 6. - 9. */
1311 pix->pixelformat = cam->camera_fmt->fourcc;
1312 pix->colorspace = cam->camera_fmt->colorspace;
1313
1314 capsr = capture_save_reset(pcdev);
1315 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1316
1317 /* Make relative to camera rectangle */
1318 rect->left -= cam_rect->left;
1319 rect->top -= cam_rect->top;
1320
1321 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1322
1323 ret = client_scale(icd, cam_rect, rect, ceu_rect, &f,
1324 pcdev->image_mode && !pcdev->is_interlaced);
1325
1326 dev_geo(dev, "6-9: %d\n", ret);
1327
1328 /* 10. Use CEU cropping to crop to the new window. */
1329 sh_mobile_ceu_set_rect(icd, out_width, out_height);
1330
1331 dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n",
1332 ceu_rect->width, ceu_rect->height,
1333 ceu_rect->left, ceu_rect->top);
1334
1335 /*
1336 * 11. Calculate CEU scales from camera scales from results of (10) and
1337 * user window from (3)
1338 */
1339 scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
1340 scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
1341
1342 dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
1343
1344 /* 12. Apply CEU scales. */
1345 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1346 if (cflcr != pcdev->cflcr) {
1347 pcdev->cflcr = cflcr;
1348 ceu_write(pcdev, CFLCR, cflcr);
1349 }
1350
1351 /* Restore capture */
904078f1
GL
1352 if (pcdev->active)
1353 capsr |= 1;
904078f1
GL
1354 capture_restore(pcdev, capsr);
1355
6a6c8786
GL
1356 icd->user_width = out_width;
1357 icd->user_height = out_height;
1358
904078f1
GL
1359 /* Even if only camera cropping succeeded */
1360 return ret;
09e231b3
GL
1361}
1362
961801bb 1363/* Similar to set_crop multistage iterative algorithm */
d8fac217 1364static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
09e231b3 1365 struct v4l2_format *f)
0d3244d6 1366{
9414de39 1367 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
961801bb 1368 struct sh_mobile_ceu_dev *pcdev = ici->priv;
904078f1
GL
1369 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1370 struct v4l2_pix_format *pix = &f->fmt.pix;
6a6c8786
GL
1371 struct v4l2_format cam_f = *f;
1372 struct v4l2_pix_format *cam_pix = &cam_f.fmt.pix;
c9c1f1c0 1373 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
6a6c8786 1374 struct device *dev = icd->dev.parent;
904078f1 1375 __u32 pixfmt = pix->pixelformat;
9414de39 1376 const struct soc_camera_format_xlate *xlate;
6a6c8786
GL
1377 struct v4l2_crop cam_crop;
1378 struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
1379 unsigned int scale_cam_h, scale_cam_v;
1380 u16 scale_v, scale_h;
1381 int ret;
1382 bool is_interlaced, image_mode;
961801bb
GL
1383
1384 switch (pix->field) {
1385 case V4L2_FIELD_INTERLACED:
6a6c8786 1386 is_interlaced = true;
961801bb
GL
1387 break;
1388 case V4L2_FIELD_ANY:
1389 default:
1390 pix->field = V4L2_FIELD_NONE;
1391 /* fall-through */
1392 case V4L2_FIELD_NONE:
6a6c8786 1393 is_interlaced = false;
961801bb
GL
1394 break;
1395 }
25c4d74e 1396
9414de39
MD
1397 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1398 if (!xlate) {
6a6c8786 1399 dev_warn(dev, "Format %x not found\n", pixfmt);
9414de39 1400 return -EINVAL;
25c4d74e
GL
1401 }
1402
6a6c8786
GL
1403 /* 1. Calculate current camera scales. */
1404 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1405
1406 ret = client_g_rect(sd, cam_rect);
1407 if (ret < 0)
1408 return ret;
1409
1410 ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
1411 if (ret < 0)
1412 return ret;
1413
1414 dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
1415
1416 /*
1417 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1418 * scaled back at current camera scales onto input window.
1419 */
1420 ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
961801bb
GL
1421 if (ret < 0)
1422 return ret;
1423
6a6c8786
GL
1424 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1425 cam_subrect.width, cam_subrect.height,
1426 cam_subrect.left, cam_subrect.top);
1427
1428 /*
1429 * 3. Calculate new combined scales from "effective" input window to
1430 * requested user window.
1431 */
1432 scale_h = calc_generic_scale(cam_subrect.width, pix->width);
1433 scale_v = calc_generic_scale(cam_subrect.height, pix->height);
1434
1435 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1436
1437 /*
1438 * 4. Calculate camera output window by applying combined scales to real
1439 * input window.
1440 */
1441 cam_pix->width = scale_down(cam_rect->width, scale_h);
1442 cam_pix->height = scale_down(cam_rect->height, scale_v);
1443 cam_pix->pixelformat = xlate->cam_fmt->fourcc;
1444
961801bb
GL
1445 switch (pixfmt) {
1446 case V4L2_PIX_FMT_NV12:
1447 case V4L2_PIX_FMT_NV21:
1448 case V4L2_PIX_FMT_NV16:
1449 case V4L2_PIX_FMT_NV61:
6a6c8786 1450 image_mode = true;
961801bb
GL
1451 break;
1452 default:
6a6c8786 1453 image_mode = false;
9414de39 1454 }
25c4d74e 1455
6a6c8786
GL
1456 dev_geo(dev, "4: camera output %ux%u\n",
1457 cam_pix->width, cam_pix->height);
961801bb 1458
6a6c8786
GL
1459 /* 5. - 9. */
1460 ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &cam_f,
1461 image_mode && !is_interlaced);
1462
1463 dev_geo(dev, "5-9: client scale %d\n", ret);
1464
1465 /* Done with the camera. Now see if we can improve the result */
1466
1467 dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
1468 ret, cam_pix->width, cam_pix->height, pix->width, pix->height);
1469 if (ret < 0)
1470 return ret;
1471
1472 /* 10. Use CEU scaling to scale to the requested user window. */
961801bb
GL
1473
1474 /* We cannot scale up */
6a6c8786
GL
1475 if (pix->width > cam_pix->width)
1476 pix->width = cam_pix->width;
1477 if (pix->width > ceu_rect.width)
1478 pix->width = ceu_rect.width;
961801bb 1479
6a6c8786
GL
1480 if (pix->height > cam_pix->height)
1481 pix->height = cam_pix->height;
1482 if (pix->height > ceu_rect.height)
1483 pix->height = ceu_rect.height;
961801bb
GL
1484
1485 /* Let's rock: scale pix->{width x height} down to width x height */
6a6c8786
GL
1486 scale_h = calc_scale(ceu_rect.width, &pix->width);
1487 scale_v = calc_scale(ceu_rect.height, &pix->height);
961801bb 1488
6a6c8786
GL
1489 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1490 ceu_rect.width, scale_h, pix->width,
1491 ceu_rect.height, scale_v, pix->height);
961801bb 1492
6a6c8786 1493 pcdev->cflcr = scale_h | (scale_v << 16);
961801bb
GL
1494
1495 icd->buswidth = xlate->buswidth;
1496 icd->current_fmt = xlate->host_fmt;
1497 cam->camera_fmt = xlate->cam_fmt;
6a6c8786 1498 cam->ceu_rect = ceu_rect;
961801bb
GL
1499
1500 pcdev->is_interlaced = is_interlaced;
6a6c8786 1501 pcdev->image_mode = image_mode;
961801bb
GL
1502
1503 return 0;
0d3244d6
MD
1504}
1505
d8fac217
GL
1506static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1507 struct v4l2_format *f)
0d3244d6 1508{
9414de39 1509 const struct soc_camera_format_xlate *xlate;
904078f1 1510 struct v4l2_pix_format *pix = &f->fmt.pix;
c9c1f1c0 1511 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
904078f1 1512 __u32 pixfmt = pix->pixelformat;
961801bb 1513 int width, height;
ccab8a29 1514 int ret;
a2c8c68c 1515
9414de39
MD
1516 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1517 if (!xlate) {
0166b743 1518 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
25c4d74e 1519 return -EINVAL;
9414de39 1520 }
25c4d74e 1521
0d3244d6
MD
1522 /* FIXME: calculate using depth and bus width */
1523
904078f1
GL
1524 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1525 &pix->height, 4, 1920, 2, 0);
0d3244d6 1526
961801bb
GL
1527 width = pix->width;
1528 height = pix->height;
1529
904078f1 1530 pix->bytesperline = pix->width *
9414de39 1531 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
904078f1 1532 pix->sizeimage = pix->height * pix->bytesperline;
25c4d74e 1533
904078f1 1534 pix->pixelformat = xlate->cam_fmt->fourcc;
979ea1dd 1535
0d3244d6 1536 /* limit to sensor capabilities */
c9c1f1c0 1537 ret = v4l2_subdev_call(sd, video, try_fmt, f);
904078f1 1538 pix->pixelformat = pixfmt;
ccab8a29
KM
1539 if (ret < 0)
1540 return ret;
1541
961801bb
GL
1542 switch (pixfmt) {
1543 case V4L2_PIX_FMT_NV12:
1544 case V4L2_PIX_FMT_NV21:
1545 case V4L2_PIX_FMT_NV16:
1546 case V4L2_PIX_FMT_NV61:
1547 /* FIXME: check against rect_max after converting soc-camera */
1548 /* We can scale precisely, need a bigger image from camera */
1549 if (pix->width < width || pix->height < height) {
1550 int tmp_w = pix->width, tmp_h = pix->height;
1551 pix->width = 2560;
1552 pix->height = 1920;
c9c1f1c0 1553 ret = v4l2_subdev_call(sd, video, try_fmt, f);
961801bb
GL
1554 if (ret < 0) {
1555 /* Shouldn't actually happen... */
0166b743 1556 dev_err(icd->dev.parent,
961801bb
GL
1557 "FIXME: try_fmt() returned %d\n", ret);
1558 pix->width = tmp_w;
1559 pix->height = tmp_h;
1560 }
1561 }
1562 if (pix->width > width)
1563 pix->width = width;
1564 if (pix->height > height)
1565 pix->height = height;
ccab8a29
KM
1566 }
1567
1568 return ret;
0d3244d6
MD
1569}
1570
1571static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
1572 struct v4l2_requestbuffers *p)
1573{
1574 int i;
1575
1576 /* This is for locking debugging only. I removed spinlocks and now I
1577 * check whether .prepare is ever called on a linked buffer, or whether
1578 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1579 * it hadn't triggered */
1580 for (i = 0; i < p->count; i++) {
1581 struct sh_mobile_ceu_buffer *buf;
1582
1583 buf = container_of(icf->vb_vidq.bufs[i],
1584 struct sh_mobile_ceu_buffer, vb);
1585 INIT_LIST_HEAD(&buf->vb.queue);
1586 }
1587
1588 return 0;
1589}
1590
1591static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1592{
1593 struct soc_camera_file *icf = file->private_data;
1594 struct sh_mobile_ceu_buffer *buf;
1595
1596 buf = list_entry(icf->vb_vidq.stream.next,
1597 struct sh_mobile_ceu_buffer, vb.stream);
1598
1599 poll_wait(file, &buf->vb.done, pt);
1600
1601 if (buf->vb.state == VIDEOBUF_DONE ||
1602 buf->vb.state == VIDEOBUF_ERROR)
1603 return POLLIN|POLLRDNORM;
1604
1605 return 0;
1606}
1607
1608static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1609 struct v4l2_capability *cap)
1610{
1611 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1612 cap->version = KERNEL_VERSION(0, 0, 5);
1613 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1614 return 0;
1615}
1616
1617static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
1618 struct soc_camera_device *icd)
1619{
1620 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1621 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1622
1623 videobuf_queue_dma_contig_init(q,
1624 &sh_mobile_ceu_videobuf_ops,
0166b743 1625 icd->dev.parent, &pcdev->lock,
0d3244d6 1626 V4L2_BUF_TYPE_VIDEO_CAPTURE,
e802967c
GL
1627 pcdev->is_interlaced ?
1628 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
0d3244d6
MD
1629 sizeof(struct sh_mobile_ceu_buffer),
1630 icd);
1631}
1632
4a6110bc
GL
1633static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1634 struct v4l2_control *ctrl)
1635{
1636 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1637 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1638 u32 val;
1639
1640 switch (ctrl->id) {
1641 case V4L2_CID_SHARPNESS:
1642 val = ceu_read(pcdev, CLFCR);
1643 ctrl->value = val ^ 1;
1644 return 0;
1645 }
1646 return -ENOIOCTLCMD;
1647}
1648
1649static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1650 struct v4l2_control *ctrl)
1651{
1652 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1653 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1654
1655 switch (ctrl->id) {
1656 case V4L2_CID_SHARPNESS:
1657 switch (icd->current_fmt->fourcc) {
1658 case V4L2_PIX_FMT_NV12:
1659 case V4L2_PIX_FMT_NV21:
1660 case V4L2_PIX_FMT_NV16:
1661 case V4L2_PIX_FMT_NV61:
1662 ceu_write(pcdev, CLFCR, !ctrl->value);
1663 return 0;
1664 }
1665 return -EINVAL;
1666 }
1667 return -ENOIOCTLCMD;
1668}
1669
1670static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1671 {
1672 .id = V4L2_CID_SHARPNESS,
1673 .type = V4L2_CTRL_TYPE_BOOLEAN,
1674 .name = "Low-pass filter",
1675 .minimum = 0,
1676 .maximum = 1,
1677 .step = 1,
1678 .default_value = 0,
1679 },
1680};
1681
0d3244d6
MD
1682static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1683 .owner = THIS_MODULE,
1684 .add = sh_mobile_ceu_add_device,
1685 .remove = sh_mobile_ceu_remove_device,
9414de39 1686 .get_formats = sh_mobile_ceu_get_formats,
904078f1 1687 .put_formats = sh_mobile_ceu_put_formats,
09e231b3 1688 .set_crop = sh_mobile_ceu_set_crop,
d8fac217
GL
1689 .set_fmt = sh_mobile_ceu_set_fmt,
1690 .try_fmt = sh_mobile_ceu_try_fmt,
4a6110bc
GL
1691 .set_ctrl = sh_mobile_ceu_set_ctrl,
1692 .get_ctrl = sh_mobile_ceu_get_ctrl,
0d3244d6
MD
1693 .reqbufs = sh_mobile_ceu_reqbufs,
1694 .poll = sh_mobile_ceu_poll,
1695 .querycap = sh_mobile_ceu_querycap,
0d3244d6
MD
1696 .set_bus_param = sh_mobile_ceu_set_bus_param,
1697 .init_videobuf = sh_mobile_ceu_init_videobuf,
4a6110bc
GL
1698 .controls = sh_mobile_ceu_controls,
1699 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
0d3244d6
MD
1700};
1701
979ea1dd 1702static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
0d3244d6
MD
1703{
1704 struct sh_mobile_ceu_dev *pcdev;
1705 struct resource *res;
1706 void __iomem *base;
1707 unsigned int irq;
1708 int err = 0;
1709
1710 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1711 irq = platform_get_irq(pdev, 0);
1712 if (!res || !irq) {
1713 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1714 err = -ENODEV;
1715 goto exit;
1716 }
1717
1718 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1719 if (!pcdev) {
1720 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1721 err = -ENOMEM;
1722 goto exit;
1723 }
1724
0d3244d6
MD
1725 INIT_LIST_HEAD(&pcdev->capture);
1726 spin_lock_init(&pcdev->lock);
1727
1728 pcdev->pdata = pdev->dev.platform_data;
1729 if (!pcdev->pdata) {
1730 err = -EINVAL;
1731 dev_err(&pdev->dev, "CEU platform data not set.\n");
1732 goto exit_kfree;
1733 }
1734
eb6c8558 1735 base = ioremap_nocache(res->start, resource_size(res));
0d3244d6
MD
1736 if (!base) {
1737 err = -ENXIO;
1738 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1739 goto exit_kfree;
1740 }
1741
1742 pcdev->irq = irq;
1743 pcdev->base = base;
1744 pcdev->video_limit = 0; /* only enabled if second resource exists */
0d3244d6
MD
1745
1746 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1747 if (res) {
1748 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1749 res->start,
eb6c8558 1750 resource_size(res),
0d3244d6
MD
1751 DMA_MEMORY_MAP |
1752 DMA_MEMORY_EXCLUSIVE);
1753 if (!err) {
1754 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1755 err = -ENXIO;
1756 goto exit_iounmap;
1757 }
1758
eb6c8558 1759 pcdev->video_limit = resource_size(res);
0d3244d6
MD
1760 }
1761
1762 /* request irq */
1763 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
af128a10 1764 dev_name(&pdev->dev), pcdev);
0d3244d6
MD
1765 if (err) {
1766 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1767 goto exit_release_mem;
1768 }
1769
6d1386c6
MD
1770 pm_suspend_ignore_children(&pdev->dev, true);
1771 pm_runtime_enable(&pdev->dev);
1772 pm_runtime_resume(&pdev->dev);
a42b6dd6 1773
0d3244d6 1774 pcdev->ici.priv = pcdev;
979ea1dd 1775 pcdev->ici.v4l2_dev.dev = &pdev->dev;
0d3244d6 1776 pcdev->ici.nr = pdev->id;
af128a10
KS
1777 pcdev->ici.drv_name = dev_name(&pdev->dev);
1778 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
0d3244d6
MD
1779
1780 err = soc_camera_host_register(&pcdev->ici);
1781 if (err)
055e05a0 1782 goto exit_free_clk;
0d3244d6
MD
1783
1784 return 0;
1785
055e05a0
KM
1786exit_free_clk:
1787 pm_runtime_disable(&pdev->dev);
0d3244d6
MD
1788 free_irq(pcdev->irq, pcdev);
1789exit_release_mem:
1790 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1791 dma_release_declared_memory(&pdev->dev);
1792exit_iounmap:
1793 iounmap(base);
1794exit_kfree:
1795 kfree(pcdev);
1796exit:
1797 return err;
1798}
1799
979ea1dd 1800static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
0d3244d6 1801{
eff505fa
GL
1802 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1803 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
1804 struct sh_mobile_ceu_dev, ici);
0d3244d6 1805
eff505fa 1806 soc_camera_host_unregister(soc_host);
055e05a0 1807 pm_runtime_disable(&pdev->dev);
0d3244d6
MD
1808 free_irq(pcdev->irq, pcdev);
1809 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1810 dma_release_declared_memory(&pdev->dev);
1811 iounmap(pcdev->base);
1812 kfree(pcdev);
1813 return 0;
1814}
1815
6d1386c6
MD
1816static int sh_mobile_ceu_runtime_nop(struct device *dev)
1817{
1818 /* Runtime PM callback shared between ->runtime_suspend()
1819 * and ->runtime_resume(). Simply returns success.
1820 *
1821 * This driver re-initializes all registers after
1822 * pm_runtime_get_sync() anyway so there is no need
1823 * to save and restore registers here.
1824 */
1825 return 0;
1826}
1827
47145210 1828static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
6d1386c6
MD
1829 .runtime_suspend = sh_mobile_ceu_runtime_nop,
1830 .runtime_resume = sh_mobile_ceu_runtime_nop,
1831};
1832
0d3244d6
MD
1833static struct platform_driver sh_mobile_ceu_driver = {
1834 .driver = {
1835 .name = "sh_mobile_ceu",
6d1386c6 1836 .pm = &sh_mobile_ceu_dev_pm_ops,
0d3244d6
MD
1837 },
1838 .probe = sh_mobile_ceu_probe,
979ea1dd 1839 .remove = __exit_p(sh_mobile_ceu_remove),
0d3244d6
MD
1840};
1841
1842static int __init sh_mobile_ceu_init(void)
1843{
1844 return platform_driver_register(&sh_mobile_ceu_driver);
1845}
1846
1847static void __exit sh_mobile_ceu_exit(void)
1848{
01c1e4ca 1849 platform_driver_unregister(&sh_mobile_ceu_driver);
0d3244d6
MD
1850}
1851
1852module_init(sh_mobile_ceu_init);
1853module_exit(sh_mobile_ceu_exit);
1854
1855MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1856MODULE_AUTHOR("Magnus Damm");
1857MODULE_LICENSE("GPL");
40e2e092 1858MODULE_ALIAS("platform:sh_mobile_ceu");
This page took 0.298623 seconds and 5 git commands to generate.