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