V4L/DVB (12514): sh_mobile_ceu_camera: add a control for the camera low-pass filter
[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>
0d3244d6
MD
34
35#include <media/v4l2-common.h>
36#include <media/v4l2-dev.h>
37#include <media/soc_camera.h>
38#include <media/sh_mobile_ceu.h>
39#include <media/videobuf-dma-contig.h>
40
41/* register offsets for sh7722 / sh7723 */
42
dd54203b
MD
43#define CAPSR 0x00 /* Capture start register */
44#define CAPCR 0x04 /* Capture control register */
45#define CAMCR 0x08 /* Capture interface control register */
46#define CMCYR 0x0c /* Capture interface cycle register */
47#define CAMOR 0x10 /* Capture interface offset register */
48#define CAPWR 0x14 /* Capture interface width register */
49#define CAIFR 0x18 /* Capture interface input format register */
50#define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
51#define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
52#define CRCNTR 0x28 /* CEU register control register */
53#define CRCMPR 0x2c /* CEU register forcible control register */
54#define CFLCR 0x30 /* Capture filter control register */
55#define CFSZR 0x34 /* Capture filter size clip register */
56#define CDWDR 0x38 /* Capture destination width register */
57#define CDAYR 0x3c /* Capture data address Y register */
58#define CDACR 0x40 /* Capture data address C register */
59#define CDBYR 0x44 /* Capture data bottom-field address Y register */
60#define CDBCR 0x48 /* Capture data bottom-field address C register */
61#define CBDSR 0x4c /* Capture bundle destination size register */
62#define CFWCR 0x5c /* Firewall operation control register */
63#define CLFCR 0x60 /* Capture low-pass filter control register */
64#define CDOCR 0x64 /* Capture data output control register */
65#define CDDCR 0x68 /* Capture data complexity level register */
66#define CDDAR 0x6c /* Capture data complexity level address register */
67#define CEIER 0x70 /* Capture event interrupt enable register */
68#define CETCR 0x74 /* Capture event flag clear register */
69#define CSTSR 0x7c /* Capture status register */
70#define CSRTR 0x80 /* Capture software reset register */
71#define CDSSR 0x84 /* Capture data size register */
72#define CDAYR2 0x90 /* Capture data address Y register 2 */
73#define CDACR2 0x94 /* Capture data address C register 2 */
74#define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
75#define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
0d3244d6 76
0d3244d6
MD
77/* per video frame buffer */
78struct sh_mobile_ceu_buffer {
79 struct videobuf_buffer vb; /* v4l buffer must be first */
80 const struct soc_camera_data_format *fmt;
81};
82
83struct sh_mobile_ceu_dev {
0d3244d6
MD
84 struct soc_camera_host ici;
85 struct soc_camera_device *icd;
86
87 unsigned int irq;
88 void __iomem *base;
89 unsigned long video_limit;
90
c60f2b5c 91 /* lock used to protect videobuf */
0d3244d6
MD
92 spinlock_t lock;
93 struct list_head capture;
94 struct videobuf_buffer *active;
e802967c 95 int is_interlaced;
0d3244d6
MD
96
97 struct sh_mobile_ceu_info *pdata;
8be65dc5
MD
98
99 const struct soc_camera_data_format *camera_fmt;
0d3244d6
MD
100};
101
c354b400
KM
102static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
103{
104 unsigned long flags;
105
106 flags = SOCAM_MASTER |
107 SOCAM_PCLK_SAMPLE_RISING |
108 SOCAM_HSYNC_ACTIVE_HIGH |
109 SOCAM_HSYNC_ACTIVE_LOW |
110 SOCAM_VSYNC_ACTIVE_HIGH |
111 SOCAM_VSYNC_ACTIVE_LOW |
112 SOCAM_DATA_ACTIVE_HIGH;
113
114 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
115 flags |= SOCAM_DATAWIDTH_8;
116
117 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
118 flags |= SOCAM_DATAWIDTH_16;
119
120 if (flags & SOCAM_DATAWIDTH_MASK)
121 return flags;
122
123 return 0;
124}
125
0d3244d6 126static void ceu_write(struct sh_mobile_ceu_dev *priv,
7a1a8164 127 unsigned long reg_offs, u32 data)
0d3244d6
MD
128{
129 iowrite32(data, priv->base + reg_offs);
130}
131
7a1a8164 132static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
0d3244d6
MD
133{
134 return ioread32(priv->base + reg_offs);
135}
136
137/*
138 * Videobuf operations
139 */
140static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
141 unsigned int *count,
142 unsigned int *size)
143{
144 struct soc_camera_device *icd = vq->priv_data;
145 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
146 struct sh_mobile_ceu_dev *pcdev = ici->priv;
147 int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
148
149 *size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel);
150
151 if (0 == *count)
152 *count = 2;
153
154 if (pcdev->video_limit) {
155 while (*size * *count > pcdev->video_limit)
156 (*count)--;
157 }
158
159 dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
160
161 return 0;
162}
163
164static void free_buffer(struct videobuf_queue *vq,
165 struct sh_mobile_ceu_buffer *buf)
166{
167 struct soc_camera_device *icd = vq->priv_data;
168
86751b01 169 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
170 &buf->vb, buf->vb.baddr, buf->vb.bsize);
171
172 if (in_interrupt())
173 BUG();
174
97215cbd 175 videobuf_waiton(&buf->vb, 0, 0);
0d3244d6
MD
176 videobuf_dma_contig_free(vq, &buf->vb);
177 dev_dbg(&icd->dev, "%s freed\n", __func__);
178 buf->vb.state = VIDEOBUF_NEEDS_INIT;
179}
180
7a1a8164
MD
181#define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
182#define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
183#define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
184#define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
185
186
0d3244d6
MD
187static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
188{
8be65dc5 189 struct soc_camera_device *icd = pcdev->icd;
ccab8a29 190 dma_addr_t phys_addr_top, phys_addr_bottom;
7a1a8164
MD
191
192 /* The hardware is _very_ picky about this sequence. Especially
193 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
194 * several not-so-well documented interrupt sources in CETCR.
195 */
196 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
197 ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
198 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
199 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
200 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
0d3244d6 201
8be65dc5
MD
202 if (!pcdev->active)
203 return;
204
ccab8a29
KM
205 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
206 ceu_write(pcdev, CDAYR, phys_addr_top);
e802967c 207 if (pcdev->is_interlaced) {
ccab8a29
KM
208 phys_addr_bottom = phys_addr_top + icd->width;
209 ceu_write(pcdev, CDBYR, phys_addr_bottom);
210 }
8be65dc5
MD
211
212 switch (icd->current_fmt->fourcc) {
213 case V4L2_PIX_FMT_NV12:
214 case V4L2_PIX_FMT_NV21:
9e4a56d2
MD
215 case V4L2_PIX_FMT_NV16:
216 case V4L2_PIX_FMT_NV61:
ccab8a29
KM
217 phys_addr_top += icd->width * icd->height;
218 ceu_write(pcdev, CDACR, phys_addr_top);
e802967c 219 if (pcdev->is_interlaced) {
ccab8a29
KM
220 phys_addr_bottom = phys_addr_top + icd->width;
221 ceu_write(pcdev, CDBCR, phys_addr_bottom);
222 }
0d3244d6 223 }
8be65dc5
MD
224
225 pcdev->active->state = VIDEOBUF_ACTIVE;
226 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
0d3244d6
MD
227}
228
229static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
230 struct videobuf_buffer *vb,
231 enum v4l2_field field)
232{
233 struct soc_camera_device *icd = vq->priv_data;
234 struct sh_mobile_ceu_buffer *buf;
235 int ret;
236
237 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
238
86751b01 239 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
240 vb, vb->baddr, vb->bsize);
241
242 /* Added list head initialization on alloc */
243 WARN_ON(!list_empty(&vb->queue));
244
245#ifdef DEBUG
246 /* This can be useful if you want to see if we actually fill
247 * the buffer with something */
248 memset((void *)vb->baddr, 0xaa, vb->bsize);
249#endif
250
251 BUG_ON(NULL == icd->current_fmt);
252
253 if (buf->fmt != icd->current_fmt ||
254 vb->width != icd->width ||
255 vb->height != icd->height ||
256 vb->field != field) {
257 buf->fmt = icd->current_fmt;
258 vb->width = icd->width;
259 vb->height = icd->height;
260 vb->field = field;
261 vb->state = VIDEOBUF_NEEDS_INIT;
262 }
263
264 vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
265 if (0 != vb->baddr && vb->bsize < vb->size) {
266 ret = -EINVAL;
267 goto out;
268 }
269
270 if (vb->state == VIDEOBUF_NEEDS_INIT) {
271 ret = videobuf_iolock(vq, vb, NULL);
272 if (ret)
273 goto fail;
274 vb->state = VIDEOBUF_PREPARED;
275 }
276
277 return 0;
278fail:
279 free_buffer(vq, buf);
280out:
281 return ret;
282}
283
2dd54a54 284/* Called under spinlock_irqsave(&pcdev->lock, ...) */
0d3244d6
MD
285static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
286 struct videobuf_buffer *vb)
287{
288 struct soc_camera_device *icd = vq->priv_data;
289 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
290 struct sh_mobile_ceu_dev *pcdev = ici->priv;
0d3244d6 291
86751b01 292 dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
0d3244d6
MD
293 vb, vb->baddr, vb->bsize);
294
51354cc3 295 vb->state = VIDEOBUF_QUEUED;
0d3244d6
MD
296 list_add_tail(&vb->queue, &pcdev->capture);
297
298 if (!pcdev->active) {
299 pcdev->active = vb;
300 sh_mobile_ceu_capture(pcdev);
301 }
0d3244d6
MD
302}
303
304static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
305 struct videobuf_buffer *vb)
306{
307 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
308}
309
310static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
311 .buf_setup = sh_mobile_ceu_videobuf_setup,
312 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
313 .buf_queue = sh_mobile_ceu_videobuf_queue,
314 .buf_release = sh_mobile_ceu_videobuf_release,
315};
316
317static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
318{
319 struct sh_mobile_ceu_dev *pcdev = data;
320 struct videobuf_buffer *vb;
321 unsigned long flags;
322
323 spin_lock_irqsave(&pcdev->lock, flags);
324
325 vb = pcdev->active;
326 list_del_init(&vb->queue);
327
328 if (!list_empty(&pcdev->capture))
329 pcdev->active = list_entry(pcdev->capture.next,
330 struct videobuf_buffer, queue);
331 else
332 pcdev->active = NULL;
333
334 sh_mobile_ceu_capture(pcdev);
335
336 vb->state = VIDEOBUF_DONE;
337 do_gettimeofday(&vb->ts);
338 vb->field_count++;
339 wake_up(&vb->done);
340 spin_unlock_irqrestore(&pcdev->lock, flags);
341
342 return IRQ_HANDLED;
343}
344
1c3bb743 345/* Called with .video_lock held */
0d3244d6
MD
346static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
347{
348 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
349 struct sh_mobile_ceu_dev *pcdev = ici->priv;
0d3244d6 350
0d3244d6 351 if (pcdev->icd)
979ea1dd 352 return -EBUSY;
0d3244d6
MD
353
354 dev_info(&icd->dev,
355 "SuperH Mobile CEU driver attached to camera %d\n",
356 icd->devnum);
357
40e2e092
GL
358 clk_enable(pcdev->clk);
359
0d3244d6
MD
360 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
361 while (ceu_read(pcdev, CSTSR) & 1)
362 msleep(1);
363
364 pcdev->icd = icd;
979ea1dd
GL
365
366 return 0;
0d3244d6
MD
367}
368
1c3bb743 369/* Called with .video_lock held */
0d3244d6
MD
370static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
371{
372 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
373 struct sh_mobile_ceu_dev *pcdev = ici->priv;
51354cc3 374 unsigned long flags;
0d3244d6
MD
375
376 BUG_ON(icd != pcdev->icd);
377
378 /* disable capture, disable interrupts */
379 ceu_write(pcdev, CEIER, 0);
380 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
51354cc3
MD
381
382 /* make sure active buffer is canceled */
383 spin_lock_irqsave(&pcdev->lock, flags);
384 if (pcdev->active) {
385 list_del(&pcdev->active->queue);
386 pcdev->active->state = VIDEOBUF_ERROR;
387 wake_up_all(&pcdev->active->done);
388 pcdev->active = NULL;
389 }
390 spin_unlock_irqrestore(&pcdev->lock, flags);
391
40e2e092
GL
392 clk_disable(pcdev->clk);
393
0d3244d6
MD
394 dev_info(&icd->dev,
395 "SuperH Mobile CEU driver detached from camera %d\n",
396 icd->devnum);
397
398 pcdev->icd = NULL;
399}
400
401static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
402 __u32 pixfmt)
403{
404 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
405 struct sh_mobile_ceu_dev *pcdev = ici->priv;
ccab8a29 406 int ret, buswidth, width, height, cfszr_width, cdwdr_width;
0d3244d6 407 unsigned long camera_flags, common_flags, value;
8be65dc5 408 int yuv_mode, yuv_lineskip;
0d3244d6
MD
409
410 camera_flags = icd->ops->query_bus_param(icd);
411 common_flags = soc_camera_bus_param_compatible(camera_flags,
c354b400 412 make_bus_param(pcdev));
0d3244d6
MD
413 if (!common_flags)
414 return -EINVAL;
415
416 ret = icd->ops->set_bus_param(icd, common_flags);
417 if (ret < 0)
418 return ret;
419
420 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
421 case SOCAM_DATAWIDTH_8:
422 buswidth = 8;
423 break;
424 case SOCAM_DATAWIDTH_16:
425 buswidth = 16;
426 break;
427 default:
428 return -EINVAL;
429 }
430
431 ceu_write(pcdev, CRCNTR, 0);
432 ceu_write(pcdev, CRCMPR, 0);
433
8be65dc5
MD
434 value = 0x00000010; /* data fetch by default */
435 yuv_mode = yuv_lineskip = 0;
436
437 switch (icd->current_fmt->fourcc) {
438 case V4L2_PIX_FMT_NV12:
439 case V4L2_PIX_FMT_NV21:
440 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
9e4a56d2
MD
441 /* fall-through */
442 case V4L2_PIX_FMT_NV16:
443 case V4L2_PIX_FMT_NV61:
8be65dc5
MD
444 yuv_mode = 1;
445 switch (pcdev->camera_fmt->fourcc) {
446 case V4L2_PIX_FMT_UYVY:
447 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
448 break;
449 case V4L2_PIX_FMT_VYUY:
450 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
451 break;
452 case V4L2_PIX_FMT_YUYV:
453 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
454 break;
455 case V4L2_PIX_FMT_YVYU:
456 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
457 break;
458 default:
459 BUG();
460 }
461 }
462
7a1a8164
MD
463 if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
464 icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
9e4a56d2 465 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
8be65dc5 466
7a1a8164
MD
467 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
468 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
469 value |= buswidth == 16 ? 1 << 12 : 0;
0d3244d6
MD
470 ceu_write(pcdev, CAMCR, value);
471
472 ceu_write(pcdev, CAPCR, 0x00300000);
e802967c 473 ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
0d3244d6
MD
474
475 mdelay(1);
476
8be65dc5
MD
477 if (yuv_mode) {
478 width = icd->width * 2;
7a1a8164 479 width = buswidth == 16 ? width / 2 : width;
8be65dc5
MD
480 cfszr_width = cdwdr_width = icd->width;
481 } else {
482 width = icd->width * ((icd->current_fmt->depth + 7) >> 3);
7a1a8164
MD
483 width = buswidth == 16 ? width / 2 : width;
484 cfszr_width = buswidth == 8 ? width / 2 : width;
485 cdwdr_width = buswidth == 16 ? width * 2 : width;
8be65dc5 486 }
0d3244d6 487
ccab8a29 488 height = icd->height;
e802967c 489 if (pcdev->is_interlaced) {
ccab8a29
KM
490 height /= 2;
491 cdwdr_width *= 2;
492 }
493
0d3244d6 494 ceu_write(pcdev, CAMOR, 0);
ccab8a29 495 ceu_write(pcdev, CAPWR, (height << 16) | width);
8be65dc5 496 ceu_write(pcdev, CFLCR, 0); /* no scaling */
ccab8a29 497 ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width);
dd54203b
MD
498
499 /* A few words about byte order (observed in Big Endian mode)
500 *
501 * In data fetch mode bytes are received in chunks of 8 bytes.
502 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
503 *
504 * The data is however by default written to memory in reverse order:
505 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
506 *
507 * The lowest three bits of CDOCR allows us to do swapping,
2c0a072e
MD
508 * using 7 we swap the data bytes to match the incoming order:
509 * D0, D1, D2, D3, D4, D5, D6, D7
dd54203b 510 */
8be65dc5
MD
511 value = 0x00000017;
512 if (yuv_lineskip)
513 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
514
515 ceu_write(pcdev, CDOCR, value);
0d3244d6
MD
516
517 ceu_write(pcdev, CDWDR, cdwdr_width);
518 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
519
520 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
0d3244d6
MD
521 return 0;
522}
523
9414de39 524static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
0d3244d6
MD
525{
526 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
527 struct sh_mobile_ceu_dev *pcdev = ici->priv;
528 unsigned long camera_flags, common_flags;
529
530 camera_flags = icd->ops->query_bus_param(icd);
531 common_flags = soc_camera_bus_param_compatible(camera_flags,
c354b400 532 make_bus_param(pcdev));
0d3244d6
MD
533 if (!common_flags)
534 return -EINVAL;
535
536 return 0;
537}
538
8be65dc5
MD
539static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
540 {
541 .name = "NV12",
542 .depth = 12,
543 .fourcc = V4L2_PIX_FMT_NV12,
544 .colorspace = V4L2_COLORSPACE_JPEG,
545 },
546 {
547 .name = "NV21",
548 .depth = 12,
549 .fourcc = V4L2_PIX_FMT_NV21,
550 .colorspace = V4L2_COLORSPACE_JPEG,
551 },
9e4a56d2
MD
552 {
553 .name = "NV16",
554 .depth = 16,
555 .fourcc = V4L2_PIX_FMT_NV16,
556 .colorspace = V4L2_COLORSPACE_JPEG,
557 },
558 {
559 .name = "NV61",
560 .depth = 16,
561 .fourcc = V4L2_PIX_FMT_NV61,
562 .colorspace = V4L2_COLORSPACE_JPEG,
563 },
8be65dc5
MD
564};
565
9414de39
MD
566static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
567 struct soc_camera_format_xlate *xlate)
568{
569 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
8be65dc5 570 int ret, k, n;
9414de39
MD
571 int formats = 0;
572
573 ret = sh_mobile_ceu_try_bus_param(icd);
574 if (ret < 0)
575 return 0;
576
c8329acc
GL
577 /* Beginning of a pass */
578 if (!idx)
579 icd->host_priv = NULL;
580
9414de39 581 switch (icd->formats[idx].fourcc) {
8be65dc5
MD
582 case V4L2_PIX_FMT_UYVY:
583 case V4L2_PIX_FMT_VYUY:
584 case V4L2_PIX_FMT_YUYV:
585 case V4L2_PIX_FMT_YVYU:
c8329acc
GL
586 if (icd->host_priv)
587 goto add_single_format;
588
589 /*
590 * Our case is simple so far: for any of the above four camera
591 * formats we add all our four synthesized NV* formats, so,
592 * just marking the device with a single flag suffices. If
593 * the format generation rules are more complex, you would have
594 * to actually hang your already added / counted formats onto
595 * the host_priv pointer and check whether the format you're
596 * going to add now is already there.
597 */
598 icd->host_priv = (void *)sh_mobile_ceu_formats;
599
8be65dc5
MD
600 n = ARRAY_SIZE(sh_mobile_ceu_formats);
601 formats += n;
602 for (k = 0; xlate && k < n; k++) {
603 xlate->host_fmt = &sh_mobile_ceu_formats[k];
604 xlate->cam_fmt = icd->formats + idx;
605 xlate->buswidth = icd->formats[idx].depth;
606 xlate++;
979ea1dd 607 dev_dbg(ici->v4l2_dev.dev, "Providing format %s using %s\n",
8be65dc5
MD
608 sh_mobile_ceu_formats[k].name,
609 icd->formats[idx].name);
610 }
9414de39 611 default:
c8329acc 612add_single_format:
9414de39
MD
613 /* Generic pass-through */
614 formats++;
615 if (xlate) {
616 xlate->host_fmt = icd->formats + idx;
617 xlate->cam_fmt = icd->formats + idx;
618 xlate->buswidth = icd->formats[idx].depth;
619 xlate++;
979ea1dd 620 dev_dbg(ici->v4l2_dev.dev,
9414de39
MD
621 "Providing format %s in pass-through mode\n",
622 icd->formats[idx].name);
623 }
624 }
625
626 return formats;
627}
628
09e231b3
GL
629static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
630 struct v4l2_rect *rect)
631{
632 return icd->ops->set_crop(icd, rect);
633}
634
d8fac217 635static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
09e231b3 636 struct v4l2_format *f)
0d3244d6 637{
9414de39 638 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
8be65dc5 639 struct sh_mobile_ceu_dev *pcdev = ici->priv;
09e231b3 640 __u32 pixfmt = f->fmt.pix.pixelformat;
9414de39 641 const struct soc_camera_format_xlate *xlate;
25c4d74e
GL
642 int ret;
643
9414de39
MD
644 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
645 if (!xlate) {
979ea1dd 646 dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
9414de39 647 return -EINVAL;
25c4d74e
GL
648 }
649
979ea1dd
GL
650 f->fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
651 ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, s_fmt, f);
652 f->fmt.pix.pixelformat = pixfmt;
0ad675eb 653 if (!ret) {
9414de39
MD
654 icd->buswidth = xlate->buswidth;
655 icd->current_fmt = xlate->host_fmt;
8be65dc5 656 pcdev->camera_fmt = xlate->cam_fmt;
9414de39 657 }
25c4d74e
GL
658
659 return ret;
0d3244d6
MD
660}
661
d8fac217
GL
662static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
663 struct v4l2_format *f)
0d3244d6 664{
9414de39 665 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
ccab8a29 666 struct sh_mobile_ceu_dev *pcdev = ici->priv;
9414de39
MD
667 const struct soc_camera_format_xlate *xlate;
668 __u32 pixfmt = f->fmt.pix.pixelformat;
ccab8a29 669 int ret;
a2c8c68c 670
9414de39
MD
671 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
672 if (!xlate) {
979ea1dd 673 dev_warn(ici->v4l2_dev.dev, "Format %x not found\n", pixfmt);
25c4d74e 674 return -EINVAL;
9414de39 675 }
25c4d74e 676
0d3244d6
MD
677 /* FIXME: calculate using depth and bus width */
678
bc44fc06
TP
679 v4l_bound_align_image(&f->fmt.pix.width, 2, 2560, 1,
680 &f->fmt.pix.height, 4, 1920, 2, 0);
0d3244d6 681
25c4d74e 682 f->fmt.pix.bytesperline = f->fmt.pix.width *
9414de39 683 DIV_ROUND_UP(xlate->host_fmt->depth, 8);
25c4d74e
GL
684 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
685
979ea1dd
GL
686 f->fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
687
0d3244d6 688 /* limit to sensor capabilities */
979ea1dd
GL
689 ret = v4l2_device_call_until_err(&ici->v4l2_dev, (__u32)icd, video, try_fmt, f);
690 f->fmt.pix.pixelformat = pixfmt;
ccab8a29
KM
691 if (ret < 0)
692 return ret;
693
694 switch (f->fmt.pix.field) {
695 case V4L2_FIELD_INTERLACED:
e802967c 696 pcdev->is_interlaced = 1;
ccab8a29
KM
697 break;
698 case V4L2_FIELD_ANY:
699 f->fmt.pix.field = V4L2_FIELD_NONE;
700 /* fall-through */
701 case V4L2_FIELD_NONE:
e802967c 702 pcdev->is_interlaced = 0;
ccab8a29
KM
703 break;
704 default:
705 ret = -EINVAL;
706 break;
707 }
708
709 return ret;
0d3244d6
MD
710}
711
712static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
713 struct v4l2_requestbuffers *p)
714{
715 int i;
716
717 /* This is for locking debugging only. I removed spinlocks and now I
718 * check whether .prepare is ever called on a linked buffer, or whether
719 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
720 * it hadn't triggered */
721 for (i = 0; i < p->count; i++) {
722 struct sh_mobile_ceu_buffer *buf;
723
724 buf = container_of(icf->vb_vidq.bufs[i],
725 struct sh_mobile_ceu_buffer, vb);
726 INIT_LIST_HEAD(&buf->vb.queue);
727 }
728
729 return 0;
730}
731
732static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
733{
734 struct soc_camera_file *icf = file->private_data;
735 struct sh_mobile_ceu_buffer *buf;
736
737 buf = list_entry(icf->vb_vidq.stream.next,
738 struct sh_mobile_ceu_buffer, vb.stream);
739
740 poll_wait(file, &buf->vb.done, pt);
741
742 if (buf->vb.state == VIDEOBUF_DONE ||
743 buf->vb.state == VIDEOBUF_ERROR)
744 return POLLIN|POLLRDNORM;
745
746 return 0;
747}
748
749static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
750 struct v4l2_capability *cap)
751{
752 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
753 cap->version = KERNEL_VERSION(0, 0, 5);
754 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
755 return 0;
756}
757
758static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
759 struct soc_camera_device *icd)
760{
761 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
762 struct sh_mobile_ceu_dev *pcdev = ici->priv;
763
764 videobuf_queue_dma_contig_init(q,
765 &sh_mobile_ceu_videobuf_ops,
979ea1dd 766 ici->v4l2_dev.dev, &pcdev->lock,
0d3244d6 767 V4L2_BUF_TYPE_VIDEO_CAPTURE,
e802967c
GL
768 pcdev->is_interlaced ?
769 V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
0d3244d6
MD
770 sizeof(struct sh_mobile_ceu_buffer),
771 icd);
772}
773
4a6110bc
GL
774static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
775 struct v4l2_control *ctrl)
776{
777 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
778 struct sh_mobile_ceu_dev *pcdev = ici->priv;
779 u32 val;
780
781 switch (ctrl->id) {
782 case V4L2_CID_SHARPNESS:
783 val = ceu_read(pcdev, CLFCR);
784 ctrl->value = val ^ 1;
785 return 0;
786 }
787 return -ENOIOCTLCMD;
788}
789
790static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
791 struct v4l2_control *ctrl)
792{
793 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
794 struct sh_mobile_ceu_dev *pcdev = ici->priv;
795
796 switch (ctrl->id) {
797 case V4L2_CID_SHARPNESS:
798 switch (icd->current_fmt->fourcc) {
799 case V4L2_PIX_FMT_NV12:
800 case V4L2_PIX_FMT_NV21:
801 case V4L2_PIX_FMT_NV16:
802 case V4L2_PIX_FMT_NV61:
803 ceu_write(pcdev, CLFCR, !ctrl->value);
804 return 0;
805 }
806 return -EINVAL;
807 }
808 return -ENOIOCTLCMD;
809}
810
811static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
812 {
813 .id = V4L2_CID_SHARPNESS,
814 .type = V4L2_CTRL_TYPE_BOOLEAN,
815 .name = "Low-pass filter",
816 .minimum = 0,
817 .maximum = 1,
818 .step = 1,
819 .default_value = 0,
820 },
821};
822
0d3244d6
MD
823static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
824 .owner = THIS_MODULE,
825 .add = sh_mobile_ceu_add_device,
826 .remove = sh_mobile_ceu_remove_device,
9414de39 827 .get_formats = sh_mobile_ceu_get_formats,
09e231b3 828 .set_crop = sh_mobile_ceu_set_crop,
d8fac217
GL
829 .set_fmt = sh_mobile_ceu_set_fmt,
830 .try_fmt = sh_mobile_ceu_try_fmt,
4a6110bc
GL
831 .set_ctrl = sh_mobile_ceu_set_ctrl,
832 .get_ctrl = sh_mobile_ceu_get_ctrl,
0d3244d6
MD
833 .reqbufs = sh_mobile_ceu_reqbufs,
834 .poll = sh_mobile_ceu_poll,
835 .querycap = sh_mobile_ceu_querycap,
0d3244d6
MD
836 .set_bus_param = sh_mobile_ceu_set_bus_param,
837 .init_videobuf = sh_mobile_ceu_init_videobuf,
4a6110bc
GL
838 .controls = sh_mobile_ceu_controls,
839 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
0d3244d6
MD
840};
841
979ea1dd 842static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
0d3244d6
MD
843{
844 struct sh_mobile_ceu_dev *pcdev;
845 struct resource *res;
846 void __iomem *base;
847 unsigned int irq;
848 int err = 0;
849
850 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
851 irq = platform_get_irq(pdev, 0);
852 if (!res || !irq) {
853 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
854 err = -ENODEV;
855 goto exit;
856 }
857
858 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
859 if (!pcdev) {
860 dev_err(&pdev->dev, "Could not allocate pcdev\n");
861 err = -ENOMEM;
862 goto exit;
863 }
864
0d3244d6
MD
865 INIT_LIST_HEAD(&pcdev->capture);
866 spin_lock_init(&pcdev->lock);
867
868 pcdev->pdata = pdev->dev.platform_data;
869 if (!pcdev->pdata) {
870 err = -EINVAL;
871 dev_err(&pdev->dev, "CEU platform data not set.\n");
872 goto exit_kfree;
873 }
874
eb6c8558 875 base = ioremap_nocache(res->start, resource_size(res));
0d3244d6
MD
876 if (!base) {
877 err = -ENXIO;
878 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
879 goto exit_kfree;
880 }
881
882 pcdev->irq = irq;
883 pcdev->base = base;
884 pcdev->video_limit = 0; /* only enabled if second resource exists */
0d3244d6
MD
885
886 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
887 if (res) {
888 err = dma_declare_coherent_memory(&pdev->dev, res->start,
889 res->start,
eb6c8558 890 resource_size(res),
0d3244d6
MD
891 DMA_MEMORY_MAP |
892 DMA_MEMORY_EXCLUSIVE);
893 if (!err) {
894 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
895 err = -ENXIO;
896 goto exit_iounmap;
897 }
898
eb6c8558 899 pcdev->video_limit = resource_size(res);
0d3244d6
MD
900 }
901
902 /* request irq */
903 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
af128a10 904 dev_name(&pdev->dev), pcdev);
0d3244d6
MD
905 if (err) {
906 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
907 goto exit_release_mem;
908 }
909
6d1386c6
MD
910 pm_suspend_ignore_children(&pdev->dev, true);
911 pm_runtime_enable(&pdev->dev);
912 pm_runtime_resume(&pdev->dev);
a42b6dd6 913
0d3244d6 914 pcdev->ici.priv = pcdev;
979ea1dd 915 pcdev->ici.v4l2_dev.dev = &pdev->dev;
0d3244d6 916 pcdev->ici.nr = pdev->id;
af128a10
KS
917 pcdev->ici.drv_name = dev_name(&pdev->dev);
918 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
0d3244d6
MD
919
920 err = soc_camera_host_register(&pcdev->ici);
921 if (err)
6d1386c6 922 goto exit_free_irq;
0d3244d6
MD
923
924 return 0;
925
926exit_free_irq:
927 free_irq(pcdev->irq, pcdev);
928exit_release_mem:
929 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
930 dma_release_declared_memory(&pdev->dev);
931exit_iounmap:
932 iounmap(base);
933exit_kfree:
934 kfree(pcdev);
935exit:
936 return err;
937}
938
979ea1dd 939static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
0d3244d6 940{
eff505fa
GL
941 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
942 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
943 struct sh_mobile_ceu_dev, ici);
0d3244d6 944
eff505fa 945 soc_camera_host_unregister(soc_host);
0d3244d6
MD
946 free_irq(pcdev->irq, pcdev);
947 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
948 dma_release_declared_memory(&pdev->dev);
949 iounmap(pcdev->base);
950 kfree(pcdev);
951 return 0;
952}
953
6d1386c6
MD
954static int sh_mobile_ceu_runtime_nop(struct device *dev)
955{
956 /* Runtime PM callback shared between ->runtime_suspend()
957 * and ->runtime_resume(). Simply returns success.
958 *
959 * This driver re-initializes all registers after
960 * pm_runtime_get_sync() anyway so there is no need
961 * to save and restore registers here.
962 */
963 return 0;
964}
965
966static struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
967 .runtime_suspend = sh_mobile_ceu_runtime_nop,
968 .runtime_resume = sh_mobile_ceu_runtime_nop,
969};
970
0d3244d6
MD
971static struct platform_driver sh_mobile_ceu_driver = {
972 .driver = {
973 .name = "sh_mobile_ceu",
6d1386c6 974 .pm = &sh_mobile_ceu_dev_pm_ops,
0d3244d6
MD
975 },
976 .probe = sh_mobile_ceu_probe,
979ea1dd 977 .remove = __exit_p(sh_mobile_ceu_remove),
0d3244d6
MD
978};
979
980static int __init sh_mobile_ceu_init(void)
981{
982 return platform_driver_register(&sh_mobile_ceu_driver);
983}
984
985static void __exit sh_mobile_ceu_exit(void)
986{
01c1e4ca 987 platform_driver_unregister(&sh_mobile_ceu_driver);
0d3244d6
MD
988}
989
990module_init(sh_mobile_ceu_init);
991module_exit(sh_mobile_ceu_exit);
992
993MODULE_DESCRIPTION("SuperH Mobile CEU driver");
994MODULE_AUTHOR("Magnus Damm");
995MODULE_LICENSE("GPL");
40e2e092 996MODULE_ALIAS("platform:sh_mobile_ceu");
This page took 0.361935 seconds and 5 git commands to generate.