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