Merge branch 'for-paul' of git://gitorious.org/linux-omap-dss2/linux
[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/completion.h>
21 #include <linux/delay.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/errno.h>
24 #include <linux/fs.h>
25 #include <linux/interrupt.h>
26 #include <linux/kernel.h>
27 #include <linux/mm.h>
28 #include <linux/moduleparam.h>
29 #include <linux/time.h>
30 #include <linux/version.h>
31 #include <linux/slab.h>
32 #include <linux/device.h>
33 #include <linux/platform_device.h>
34 #include <linux/videodev2.h>
35 #include <linux/pm_runtime.h>
36 #include <linux/sched.h>
37
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-dev.h>
40 #include <media/soc_camera.h>
41 #include <media/sh_mobile_ceu.h>
42 #include <media/videobuf2-dma-contig.h>
43 #include <media/v4l2-mediabus.h>
44 #include <media/soc_mediabus.h>
45
46 /* register offsets for sh7722 / sh7723 */
47
48 #define CAPSR 0x00 /* Capture start register */
49 #define CAPCR 0x04 /* Capture control register */
50 #define CAMCR 0x08 /* Capture interface control register */
51 #define CMCYR 0x0c /* Capture interface cycle register */
52 #define CAMOR 0x10 /* Capture interface offset register */
53 #define CAPWR 0x14 /* Capture interface width register */
54 #define CAIFR 0x18 /* Capture interface input format register */
55 #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
56 #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
57 #define CRCNTR 0x28 /* CEU register control register */
58 #define CRCMPR 0x2c /* CEU register forcible control register */
59 #define CFLCR 0x30 /* Capture filter control register */
60 #define CFSZR 0x34 /* Capture filter size clip register */
61 #define CDWDR 0x38 /* Capture destination width register */
62 #define CDAYR 0x3c /* Capture data address Y register */
63 #define CDACR 0x40 /* Capture data address C register */
64 #define CDBYR 0x44 /* Capture data bottom-field address Y register */
65 #define CDBCR 0x48 /* Capture data bottom-field address C register */
66 #define CBDSR 0x4c /* Capture bundle destination size register */
67 #define CFWCR 0x5c /* Firewall operation control register */
68 #define CLFCR 0x60 /* Capture low-pass filter control register */
69 #define CDOCR 0x64 /* Capture data output control register */
70 #define CDDCR 0x68 /* Capture data complexity level register */
71 #define CDDAR 0x6c /* Capture data complexity level address register */
72 #define CEIER 0x70 /* Capture event interrupt enable register */
73 #define CETCR 0x74 /* Capture event flag clear register */
74 #define CSTSR 0x7c /* Capture status register */
75 #define CSRTR 0x80 /* Capture software reset register */
76 #define CDSSR 0x84 /* Capture data size register */
77 #define CDAYR2 0x90 /* Capture data address Y register 2 */
78 #define CDACR2 0x94 /* Capture data address C register 2 */
79 #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
80 #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
81
82 #undef DEBUG_GEOMETRY
83 #ifdef DEBUG_GEOMETRY
84 #define dev_geo dev_info
85 #else
86 #define dev_geo dev_dbg
87 #endif
88
89 /* per video frame buffer */
90 struct sh_mobile_ceu_buffer {
91 struct vb2_buffer vb; /* v4l buffer must be first */
92 struct list_head queue;
93 enum v4l2_mbus_pixelcode code;
94 };
95
96 struct sh_mobile_ceu_dev {
97 struct soc_camera_host ici;
98 struct soc_camera_device *icd;
99
100 unsigned int irq;
101 void __iomem *base;
102 unsigned long video_limit;
103
104 spinlock_t lock; /* Protects video buffer lists */
105 struct list_head capture;
106 struct vb2_buffer *active;
107 struct vb2_alloc_ctx *alloc_ctx;
108
109 struct sh_mobile_ceu_info *pdata;
110 struct completion complete;
111
112 u32 cflcr;
113
114 enum v4l2_field field;
115 int sequence;
116
117 unsigned int image_mode:1;
118 unsigned int is_16bit:1;
119 unsigned int frozen:1;
120 };
121
122 struct sh_mobile_ceu_cam {
123 /* CEU offsets within scaled by the CEU camera output */
124 unsigned int ceu_left;
125 unsigned int ceu_top;
126 /* Client output, as seen by the CEU */
127 unsigned int width;
128 unsigned int height;
129 /*
130 * User window from S_CROP / G_CROP, produced by client cropping and
131 * scaling, CEU scaling and CEU cropping, mapped back onto the client
132 * input window
133 */
134 struct v4l2_rect subrect;
135 /* Camera cropping rectangle */
136 struct v4l2_rect rect;
137 const struct soc_mbus_pixelfmt *extra_fmt;
138 enum v4l2_mbus_pixelcode code;
139 };
140
141 static struct sh_mobile_ceu_buffer *to_ceu_vb(struct vb2_buffer *vb)
142 {
143 return container_of(vb, struct sh_mobile_ceu_buffer, vb);
144 }
145
146 static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
147 {
148 unsigned long flags;
149
150 flags = SOCAM_MASTER |
151 SOCAM_PCLK_SAMPLE_RISING |
152 SOCAM_HSYNC_ACTIVE_HIGH |
153 SOCAM_HSYNC_ACTIVE_LOW |
154 SOCAM_VSYNC_ACTIVE_HIGH |
155 SOCAM_VSYNC_ACTIVE_LOW |
156 SOCAM_DATA_ACTIVE_HIGH;
157
158 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
159 flags |= SOCAM_DATAWIDTH_8;
160
161 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
162 flags |= SOCAM_DATAWIDTH_16;
163
164 if (flags & SOCAM_DATAWIDTH_MASK)
165 return flags;
166
167 return 0;
168 }
169
170 static void ceu_write(struct sh_mobile_ceu_dev *priv,
171 unsigned long reg_offs, u32 data)
172 {
173 iowrite32(data, priv->base + reg_offs);
174 }
175
176 static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
177 {
178 return ioread32(priv->base + reg_offs);
179 }
180
181 static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
182 {
183 int i, success = 0;
184 struct soc_camera_device *icd = pcdev->icd;
185
186 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
187
188 /* wait CSTSR.CPTON bit */
189 for (i = 0; i < 1000; i++) {
190 if (!(ceu_read(pcdev, CSTSR) & 1)) {
191 success++;
192 break;
193 }
194 udelay(1);
195 }
196
197 /* wait CAPSR.CPKIL bit */
198 for (i = 0; i < 1000; i++) {
199 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
200 success++;
201 break;
202 }
203 udelay(1);
204 }
205
206
207 if (2 != success) {
208 dev_warn(&icd->dev, "soft reset time out\n");
209 return -EIO;
210 }
211
212 return 0;
213 }
214
215 /*
216 * Videobuf operations
217 */
218 static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq,
219 unsigned int *count, unsigned int *num_planes,
220 unsigned long sizes[], void *alloc_ctxs[])
221 {
222 struct soc_camera_device *icd = container_of(vq, struct soc_camera_device, vb2_vidq);
223 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
224 struct sh_mobile_ceu_dev *pcdev = ici->priv;
225 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
226 icd->current_fmt->host_fmt);
227
228 if (bytes_per_line < 0)
229 return bytes_per_line;
230
231 *num_planes = 1;
232
233 pcdev->sequence = 0;
234 sizes[0] = bytes_per_line * icd->user_height;
235 alloc_ctxs[0] = pcdev->alloc_ctx;
236
237 if (!*count)
238 *count = 2;
239
240 if (pcdev->video_limit) {
241 if (PAGE_ALIGN(sizes[0]) * *count > pcdev->video_limit)
242 *count = pcdev->video_limit / PAGE_ALIGN(sizes[0]);
243 }
244
245 dev_dbg(icd->dev.parent, "count=%d, size=%lu\n", *count, sizes[0]);
246
247 return 0;
248 }
249
250 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
251 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
252 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
253 #define CEU_CEIER_VBP (1 << 20) /* vbp error */
254 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
255 #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
256
257
258 /*
259 * return value doesn't reflex the success/failure to queue the new buffer,
260 * but rather the status of the previous buffer.
261 */
262 static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
263 {
264 struct soc_camera_device *icd = pcdev->icd;
265 dma_addr_t phys_addr_top, phys_addr_bottom;
266 unsigned long top1, top2;
267 unsigned long bottom1, bottom2;
268 u32 status;
269 int ret = 0;
270
271 /*
272 * The hardware is _very_ picky about this sequence. Especially
273 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
274 * several not-so-well documented interrupt sources in CETCR.
275 */
276 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
277 status = ceu_read(pcdev, CETCR);
278 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
279 if (!pcdev->frozen)
280 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
281 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
282 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
283
284 /*
285 * When a VBP interrupt occurs, a capture end interrupt does not occur
286 * and the image of that frame is not captured correctly. So, soft reset
287 * is needed here.
288 */
289 if (status & CEU_CEIER_VBP) {
290 sh_mobile_ceu_soft_reset(pcdev);
291 ret = -EIO;
292 }
293
294 if (pcdev->frozen) {
295 complete(&pcdev->complete);
296 return ret;
297 }
298
299 if (!pcdev->active)
300 return ret;
301
302 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
303 top1 = CDBYR;
304 top2 = CDBCR;
305 bottom1 = CDAYR;
306 bottom2 = CDACR;
307 } else {
308 top1 = CDAYR;
309 top2 = CDACR;
310 bottom1 = CDBYR;
311 bottom2 = CDBCR;
312 }
313
314 phys_addr_top = vb2_dma_contig_plane_paddr(pcdev->active, 0);
315
316 ceu_write(pcdev, top1, phys_addr_top);
317 if (V4L2_FIELD_NONE != pcdev->field) {
318 phys_addr_bottom = phys_addr_top + icd->user_width;
319 ceu_write(pcdev, bottom1, phys_addr_bottom);
320 }
321
322 switch (icd->current_fmt->host_fmt->fourcc) {
323 case V4L2_PIX_FMT_NV12:
324 case V4L2_PIX_FMT_NV21:
325 case V4L2_PIX_FMT_NV16:
326 case V4L2_PIX_FMT_NV61:
327 phys_addr_top += icd->user_width *
328 icd->user_height;
329 ceu_write(pcdev, top2, phys_addr_top);
330 if (V4L2_FIELD_NONE != pcdev->field) {
331 phys_addr_bottom = phys_addr_top + icd->user_width;
332 ceu_write(pcdev, bottom2, phys_addr_bottom);
333 }
334 }
335
336 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
337
338 return ret;
339 }
340
341 static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer *vb)
342 {
343 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
344 struct sh_mobile_ceu_buffer *buf;
345 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
346 icd->current_fmt->host_fmt);
347 unsigned long size;
348
349 if (bytes_per_line < 0)
350 return bytes_per_line;
351
352 buf = to_ceu_vb(vb);
353
354 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
355 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
356
357 /* Added list head initialization on alloc */
358 WARN(!list_empty(&buf->queue), "Buffer %p on queue!\n", vb);
359
360 #ifdef DEBUG
361 /*
362 * This can be useful if you want to see if we actually fill
363 * the buffer with something
364 */
365 if (vb2_plane_vaddr(vb, 0))
366 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
367 #endif
368
369 BUG_ON(NULL == icd->current_fmt);
370
371 size = icd->user_height * bytes_per_line;
372
373 if (vb2_plane_size(vb, 0) < size) {
374 dev_err(icd->dev.parent, "Buffer too small (%lu < %lu)\n",
375 vb2_plane_size(vb, 0), size);
376 return -ENOBUFS;
377 }
378
379 vb2_set_plane_payload(vb, 0, size);
380
381 return 0;
382 }
383
384 static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer *vb)
385 {
386 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
387 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
388 struct sh_mobile_ceu_dev *pcdev = ici->priv;
389 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
390
391 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
392 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
393
394 spin_lock_irq(&pcdev->lock);
395 list_add_tail(&buf->queue, &pcdev->capture);
396
397 if (!pcdev->active) {
398 /*
399 * Because there were no active buffer at this moment,
400 * we are not interested in the return value of
401 * sh_mobile_ceu_capture here.
402 */
403 pcdev->active = vb;
404 sh_mobile_ceu_capture(pcdev);
405 }
406 spin_unlock_irq(&pcdev->lock);
407 }
408
409 static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb)
410 {
411 struct soc_camera_device *icd = container_of(vb->vb2_queue, struct soc_camera_device, vb2_vidq);
412 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
413 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vb);
414 struct sh_mobile_ceu_dev *pcdev = ici->priv;
415
416 spin_lock_irq(&pcdev->lock);
417
418 if (pcdev->active == vb) {
419 /* disable capture (release DMA buffer), reset */
420 ceu_write(pcdev, CAPSR, 1 << 16);
421 pcdev->active = NULL;
422 }
423
424 /* Doesn't hurt also if the list is empty */
425 list_del_init(&buf->queue);
426
427 spin_unlock_irq(&pcdev->lock);
428 }
429
430 static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb)
431 {
432 /* This is for locking debugging only */
433 INIT_LIST_HEAD(&to_ceu_vb(vb)->queue);
434 return 0;
435 }
436
437 static int sh_mobile_ceu_stop_streaming(struct vb2_queue *q)
438 {
439 struct soc_camera_device *icd = container_of(q, struct soc_camera_device, vb2_vidq);
440 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
441 struct sh_mobile_ceu_dev *pcdev = ici->priv;
442 struct list_head *buf_head, *tmp;
443
444 spin_lock_irq(&pcdev->lock);
445
446 pcdev->active = NULL;
447
448 list_for_each_safe(buf_head, tmp, &pcdev->capture)
449 list_del_init(buf_head);
450
451 spin_unlock_irq(&pcdev->lock);
452
453 return sh_mobile_ceu_soft_reset(pcdev);
454 }
455
456 static struct vb2_ops sh_mobile_ceu_videobuf_ops = {
457 .queue_setup = sh_mobile_ceu_videobuf_setup,
458 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
459 .buf_queue = sh_mobile_ceu_videobuf_queue,
460 .buf_cleanup = sh_mobile_ceu_videobuf_release,
461 .buf_init = sh_mobile_ceu_videobuf_init,
462 .wait_prepare = soc_camera_unlock,
463 .wait_finish = soc_camera_lock,
464 .stop_streaming = sh_mobile_ceu_stop_streaming,
465 };
466
467 static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
468 {
469 struct sh_mobile_ceu_dev *pcdev = data;
470 struct vb2_buffer *vb;
471 int ret;
472
473 spin_lock(&pcdev->lock);
474
475 vb = pcdev->active;
476 if (!vb)
477 /* Stale interrupt from a released buffer */
478 goto out;
479
480 list_del_init(&to_ceu_vb(vb)->queue);
481
482 if (!list_empty(&pcdev->capture))
483 pcdev->active = &list_entry(pcdev->capture.next,
484 struct sh_mobile_ceu_buffer, queue)->vb;
485 else
486 pcdev->active = NULL;
487
488 ret = sh_mobile_ceu_capture(pcdev);
489 do_gettimeofday(&vb->v4l2_buf.timestamp);
490 if (!ret) {
491 vb->v4l2_buf.field = pcdev->field;
492 vb->v4l2_buf.sequence = pcdev->sequence++;
493 }
494 vb2_buffer_done(vb, ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
495
496 out:
497 spin_unlock(&pcdev->lock);
498
499 return IRQ_HANDLED;
500 }
501
502 /* Called with .video_lock held */
503 static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
504 {
505 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
506 struct sh_mobile_ceu_dev *pcdev = ici->priv;
507 int ret;
508
509 if (pcdev->icd)
510 return -EBUSY;
511
512 dev_info(icd->dev.parent,
513 "SuperH Mobile CEU driver attached to camera %d\n",
514 icd->devnum);
515
516 pm_runtime_get_sync(ici->v4l2_dev.dev);
517
518 ret = sh_mobile_ceu_soft_reset(pcdev);
519 if (!ret)
520 pcdev->icd = icd;
521
522 return ret;
523 }
524
525 /* Called with .video_lock held */
526 static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
527 {
528 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
529 struct sh_mobile_ceu_dev *pcdev = ici->priv;
530
531 BUG_ON(icd != pcdev->icd);
532
533 /* disable capture, disable interrupts */
534 ceu_write(pcdev, CEIER, 0);
535 sh_mobile_ceu_soft_reset(pcdev);
536
537 /* make sure active buffer is canceled */
538 spin_lock_irq(&pcdev->lock);
539 if (pcdev->active) {
540 list_del_init(&to_ceu_vb(pcdev->active)->queue);
541 vb2_buffer_done(pcdev->active, VB2_BUF_STATE_ERROR);
542 pcdev->active = NULL;
543 }
544 spin_unlock_irq(&pcdev->lock);
545
546 pm_runtime_put_sync(ici->v4l2_dev.dev);
547
548 dev_info(icd->dev.parent,
549 "SuperH Mobile CEU driver detached from camera %d\n",
550 icd->devnum);
551
552 pcdev->icd = NULL;
553 }
554
555 /*
556 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
557 * in SH7722 Hardware Manual
558 */
559 static unsigned int size_dst(unsigned int src, unsigned int scale)
560 {
561 unsigned int mant_pre = scale >> 12;
562 if (!src || !scale)
563 return src;
564 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
565 mant_pre * 4096 / scale + 1;
566 }
567
568 static u16 calc_scale(unsigned int src, unsigned int *dst)
569 {
570 u16 scale;
571
572 if (src == *dst)
573 return 0;
574
575 scale = (src * 4096 / *dst) & ~7;
576
577 while (scale > 4096 && size_dst(src, scale) < *dst)
578 scale -= 8;
579
580 *dst = size_dst(src, scale);
581
582 return scale;
583 }
584
585 /* rect is guaranteed to not exceed the scaled camera rectangle */
586 static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
587 {
588 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
589 struct sh_mobile_ceu_cam *cam = icd->host_priv;
590 struct sh_mobile_ceu_dev *pcdev = ici->priv;
591 unsigned int height, width, cdwdr_width, in_width, in_height;
592 unsigned int left_offset, top_offset;
593 u32 camor;
594
595 dev_geo(icd->dev.parent, "Crop %ux%u@%u:%u\n",
596 icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
597
598 left_offset = cam->ceu_left;
599 top_offset = cam->ceu_top;
600
601 /* CEU cropping (CFSZR) is applied _after_ the scaling filter (CFLCR) */
602 if (pcdev->image_mode) {
603 in_width = cam->width;
604 if (!pcdev->is_16bit) {
605 in_width *= 2;
606 left_offset *= 2;
607 }
608 width = icd->user_width;
609 cdwdr_width = icd->user_width;
610 } else {
611 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
612 icd->current_fmt->host_fmt);
613 unsigned int w_factor;
614
615 width = icd->user_width;
616
617 switch (icd->current_fmt->host_fmt->packing) {
618 case SOC_MBUS_PACKING_2X8_PADHI:
619 w_factor = 2;
620 break;
621 default:
622 w_factor = 1;
623 }
624
625 in_width = cam->width * w_factor;
626 left_offset = left_offset * w_factor;
627
628 if (bytes_per_line < 0)
629 cdwdr_width = icd->user_width;
630 else
631 cdwdr_width = bytes_per_line;
632 }
633
634 height = icd->user_height;
635 in_height = cam->height;
636 if (V4L2_FIELD_NONE != pcdev->field) {
637 height /= 2;
638 in_height /= 2;
639 top_offset /= 2;
640 cdwdr_width *= 2;
641 }
642
643 /* CSI2 special configuration */
644 if (pcdev->pdata->csi2_dev) {
645 in_width = ((in_width - 2) * 2);
646 left_offset *= 2;
647 }
648
649 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
650 camor = left_offset | (top_offset << 16);
651
652 dev_geo(icd->dev.parent,
653 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
654 (in_height << 16) | in_width, (height << 16) | width,
655 cdwdr_width);
656
657 ceu_write(pcdev, CAMOR, camor);
658 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
659 ceu_write(pcdev, CFSZR, (height << 16) | width);
660 ceu_write(pcdev, CDWDR, cdwdr_width);
661 }
662
663 static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
664 {
665 u32 capsr = ceu_read(pcdev, CAPSR);
666 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
667 return capsr;
668 }
669
670 static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
671 {
672 unsigned long timeout = jiffies + 10 * HZ;
673
674 /*
675 * Wait until the end of the current frame. It can take a long time,
676 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
677 */
678 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
679 msleep(1);
680
681 if (time_after(jiffies, timeout)) {
682 dev_err(pcdev->ici.v4l2_dev.dev,
683 "Timeout waiting for frame end! Interface problem?\n");
684 return;
685 }
686
687 /* Wait until reset clears, this shall not hang... */
688 while (ceu_read(pcdev, CAPSR) & (1 << 16))
689 udelay(10);
690
691 /* Anything to restore? */
692 if (capsr & ~(1 << 16))
693 ceu_write(pcdev, CAPSR, capsr);
694 }
695
696 /* Capture is not running, no interrupts, no locking needed */
697 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
698 __u32 pixfmt)
699 {
700 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
701 struct sh_mobile_ceu_dev *pcdev = ici->priv;
702 int ret;
703 unsigned long camera_flags, common_flags, value;
704 int yuv_lineskip;
705 struct sh_mobile_ceu_cam *cam = icd->host_priv;
706 u32 capsr = capture_save_reset(pcdev);
707
708 camera_flags = icd->ops->query_bus_param(icd);
709 common_flags = soc_camera_bus_param_compatible(camera_flags,
710 make_bus_param(pcdev));
711 if (!common_flags)
712 return -EINVAL;
713
714 /* Make choises, based on platform preferences */
715 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
716 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
717 if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
718 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
719 else
720 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
721 }
722
723 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
724 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
725 if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
726 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
727 else
728 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
729 }
730
731 ret = icd->ops->set_bus_param(icd, common_flags);
732 if (ret < 0)
733 return ret;
734
735 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
736 case SOCAM_DATAWIDTH_8:
737 pcdev->is_16bit = 0;
738 break;
739 case SOCAM_DATAWIDTH_16:
740 pcdev->is_16bit = 1;
741 break;
742 default:
743 return -EINVAL;
744 }
745
746 ceu_write(pcdev, CRCNTR, 0);
747 ceu_write(pcdev, CRCMPR, 0);
748
749 value = 0x00000010; /* data fetch by default */
750 yuv_lineskip = 0;
751
752 switch (icd->current_fmt->host_fmt->fourcc) {
753 case V4L2_PIX_FMT_NV12:
754 case V4L2_PIX_FMT_NV21:
755 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
756 /* fall-through */
757 case V4L2_PIX_FMT_NV16:
758 case V4L2_PIX_FMT_NV61:
759 switch (cam->code) {
760 case V4L2_MBUS_FMT_UYVY8_2X8:
761 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
762 break;
763 case V4L2_MBUS_FMT_VYUY8_2X8:
764 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
765 break;
766 case V4L2_MBUS_FMT_YUYV8_2X8:
767 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
768 break;
769 case V4L2_MBUS_FMT_YVYU8_2X8:
770 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
771 break;
772 default:
773 BUG();
774 }
775 }
776
777 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
778 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
779 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
780
781 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
782 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
783 value |= pcdev->is_16bit ? 1 << 12 : 0;
784
785 /* CSI2 mode */
786 if (pcdev->pdata->csi2_dev)
787 value |= 3 << 12;
788
789 ceu_write(pcdev, CAMCR, value);
790
791 ceu_write(pcdev, CAPCR, 0x00300000);
792
793 switch (pcdev->field) {
794 case V4L2_FIELD_INTERLACED_TB:
795 value = 0x101;
796 break;
797 case V4L2_FIELD_INTERLACED_BT:
798 value = 0x102;
799 break;
800 default:
801 value = 0;
802 break;
803 }
804 ceu_write(pcdev, CAIFR, value);
805
806 sh_mobile_ceu_set_rect(icd);
807 mdelay(1);
808
809 dev_geo(icd->dev.parent, "CFLCR 0x%x\n", pcdev->cflcr);
810 ceu_write(pcdev, CFLCR, pcdev->cflcr);
811
812 /*
813 * A few words about byte order (observed in Big Endian mode)
814 *
815 * In data fetch mode bytes are received in chunks of 8 bytes.
816 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
817 *
818 * The data is however by default written to memory in reverse order:
819 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
820 *
821 * The lowest three bits of CDOCR allows us to do swapping,
822 * using 7 we swap the data bytes to match the incoming order:
823 * D0, D1, D2, D3, D4, D5, D6, D7
824 */
825 value = 0x00000017;
826 if (yuv_lineskip)
827 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
828
829 ceu_write(pcdev, CDOCR, value);
830 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
831
832 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
833 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
834 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
835 icd->user_width, icd->user_height);
836
837 capture_restore(pcdev, capsr);
838
839 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
840 return 0;
841 }
842
843 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
844 unsigned char buswidth)
845 {
846 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
847 struct sh_mobile_ceu_dev *pcdev = ici->priv;
848 unsigned long camera_flags, common_flags;
849
850 camera_flags = icd->ops->query_bus_param(icd);
851 common_flags = soc_camera_bus_param_compatible(camera_flags,
852 make_bus_param(pcdev));
853 if (!common_flags || buswidth > 16 ||
854 (buswidth > 8 && !(common_flags & SOCAM_DATAWIDTH_16)))
855 return -EINVAL;
856
857 return 0;
858 }
859
860 static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
861 {
862 .fourcc = V4L2_PIX_FMT_NV12,
863 .name = "NV12",
864 .bits_per_sample = 12,
865 .packing = SOC_MBUS_PACKING_NONE,
866 .order = SOC_MBUS_ORDER_LE,
867 }, {
868 .fourcc = V4L2_PIX_FMT_NV21,
869 .name = "NV21",
870 .bits_per_sample = 12,
871 .packing = SOC_MBUS_PACKING_NONE,
872 .order = SOC_MBUS_ORDER_LE,
873 }, {
874 .fourcc = V4L2_PIX_FMT_NV16,
875 .name = "NV16",
876 .bits_per_sample = 16,
877 .packing = SOC_MBUS_PACKING_NONE,
878 .order = SOC_MBUS_ORDER_LE,
879 }, {
880 .fourcc = V4L2_PIX_FMT_NV61,
881 .name = "NV61",
882 .bits_per_sample = 16,
883 .packing = SOC_MBUS_PACKING_NONE,
884 .order = SOC_MBUS_ORDER_LE,
885 },
886 };
887
888 /* This will be corrected as we get more formats */
889 static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
890 {
891 return fmt->packing == SOC_MBUS_PACKING_NONE ||
892 (fmt->bits_per_sample == 8 &&
893 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
894 (fmt->bits_per_sample > 8 &&
895 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
896 }
897
898 static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect);
899
900 static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx,
901 struct soc_camera_format_xlate *xlate)
902 {
903 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
904 struct device *dev = icd->dev.parent;
905 struct soc_camera_host *ici = to_soc_camera_host(dev);
906 struct sh_mobile_ceu_dev *pcdev = ici->priv;
907 int ret, k, n;
908 int formats = 0;
909 struct sh_mobile_ceu_cam *cam;
910 enum v4l2_mbus_pixelcode code;
911 const struct soc_mbus_pixelfmt *fmt;
912
913 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
914 if (ret < 0)
915 /* No more formats */
916 return 0;
917
918 fmt = soc_mbus_get_fmtdesc(code);
919 if (!fmt) {
920 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code);
921 return 0;
922 }
923
924 if (!pcdev->pdata->csi2_dev) {
925 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
926 if (ret < 0)
927 return 0;
928 }
929
930 if (!icd->host_priv) {
931 struct v4l2_mbus_framefmt mf;
932 struct v4l2_rect rect;
933 int shift = 0;
934
935 /* FIXME: subwindow is lost between close / open */
936
937 /* Cache current client geometry */
938 ret = client_g_rect(sd, &rect);
939 if (ret < 0)
940 return ret;
941
942 /* First time */
943 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
944 if (ret < 0)
945 return ret;
946
947 while ((mf.width > 2560 || mf.height > 1920) && shift < 4) {
948 /* Try 2560x1920, 1280x960, 640x480, 320x240 */
949 mf.width = 2560 >> shift;
950 mf.height = 1920 >> shift;
951 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
952 s_mbus_fmt, &mf);
953 if (ret < 0)
954 return ret;
955 shift++;
956 }
957
958 if (shift == 4) {
959 dev_err(dev, "Failed to configure the client below %ux%x\n",
960 mf.width, mf.height);
961 return -EIO;
962 }
963
964 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
965
966 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
967 if (!cam)
968 return -ENOMEM;
969
970 /* We are called with current camera crop, initialise subrect with it */
971 cam->rect = rect;
972 cam->subrect = rect;
973
974 cam->width = mf.width;
975 cam->height = mf.height;
976
977 cam->width = mf.width;
978 cam->height = mf.height;
979
980 icd->host_priv = cam;
981 } else {
982 cam = icd->host_priv;
983 }
984
985 /* Beginning of a pass */
986 if (!idx)
987 cam->extra_fmt = NULL;
988
989 switch (code) {
990 case V4L2_MBUS_FMT_UYVY8_2X8:
991 case V4L2_MBUS_FMT_VYUY8_2X8:
992 case V4L2_MBUS_FMT_YUYV8_2X8:
993 case V4L2_MBUS_FMT_YVYU8_2X8:
994 if (cam->extra_fmt)
995 break;
996
997 /*
998 * Our case is simple so far: for any of the above four camera
999 * formats we add all our four synthesized NV* formats, so,
1000 * just marking the device with a single flag suffices. If
1001 * the format generation rules are more complex, you would have
1002 * to actually hang your already added / counted formats onto
1003 * the host_priv pointer and check whether the format you're
1004 * going to add now is already there.
1005 */
1006 cam->extra_fmt = sh_mobile_ceu_formats;
1007
1008 n = ARRAY_SIZE(sh_mobile_ceu_formats);
1009 formats += n;
1010 for (k = 0; xlate && k < n; k++) {
1011 xlate->host_fmt = &sh_mobile_ceu_formats[k];
1012 xlate->code = code;
1013 xlate++;
1014 dev_dbg(dev, "Providing format %s using code %d\n",
1015 sh_mobile_ceu_formats[k].name, code);
1016 }
1017 break;
1018 default:
1019 if (!sh_mobile_ceu_packing_supported(fmt))
1020 return 0;
1021 }
1022
1023 /* Generic pass-through */
1024 formats++;
1025 if (xlate) {
1026 xlate->host_fmt = fmt;
1027 xlate->code = code;
1028 xlate++;
1029 dev_dbg(dev, "Providing format %s in pass-through mode\n",
1030 fmt->name);
1031 }
1032
1033 return formats;
1034 }
1035
1036 static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
1037 {
1038 kfree(icd->host_priv);
1039 icd->host_priv = NULL;
1040 }
1041
1042 /* Check if any dimension of r1 is smaller than respective one of r2 */
1043 static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
1044 {
1045 return r1->width < r2->width || r1->height < r2->height;
1046 }
1047
1048 /* Check if r1 fails to cover r2 */
1049 static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
1050 {
1051 return r1->left > r2->left || r1->top > r2->top ||
1052 r1->left + r1->width < r2->left + r2->width ||
1053 r1->top + r1->height < r2->top + r2->height;
1054 }
1055
1056 static unsigned int scale_down(unsigned int size, unsigned int scale)
1057 {
1058 return (size * 4096 + scale / 2) / scale;
1059 }
1060
1061 static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
1062 {
1063 return (input * 4096 + output / 2) / output;
1064 }
1065
1066 /* Get and store current client crop */
1067 static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
1068 {
1069 struct v4l2_crop crop;
1070 struct v4l2_cropcap cap;
1071 int ret;
1072
1073 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1074
1075 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
1076 if (!ret) {
1077 *rect = crop.c;
1078 return ret;
1079 }
1080
1081 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1082 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1083
1084 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1085 if (!ret)
1086 *rect = cap.defrect;
1087
1088 return ret;
1089 }
1090
1091 /* Client crop has changed, update our sub-rectangle to remain within the area */
1092 static void update_subrect(struct sh_mobile_ceu_cam *cam)
1093 {
1094 struct v4l2_rect *rect = &cam->rect, *subrect = &cam->subrect;
1095
1096 if (rect->width < subrect->width)
1097 subrect->width = rect->width;
1098
1099 if (rect->height < subrect->height)
1100 subrect->height = rect->height;
1101
1102 if (rect->left > subrect->left)
1103 subrect->left = rect->left;
1104 else if (rect->left + rect->width >
1105 subrect->left + subrect->width)
1106 subrect->left = rect->left + rect->width -
1107 subrect->width;
1108
1109 if (rect->top > subrect->top)
1110 subrect->top = rect->top;
1111 else if (rect->top + rect->height >
1112 subrect->top + subrect->height)
1113 subrect->top = rect->top + rect->height -
1114 subrect->height;
1115 }
1116
1117 /*
1118 * The common for both scaling and cropping iterative approach is:
1119 * 1. try if the client can produce exactly what requested by the user
1120 * 2. if (1) failed, try to double the client image until we get one big enough
1121 * 3. if (2) failed, try to request the maximum image
1122 */
1123 static int client_s_crop(struct soc_camera_device *icd, struct v4l2_crop *crop,
1124 struct v4l2_crop *cam_crop)
1125 {
1126 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1127 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
1128 struct device *dev = sd->v4l2_dev->dev;
1129 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1130 struct v4l2_cropcap cap;
1131 int ret;
1132 unsigned int width, height;
1133
1134 v4l2_subdev_call(sd, video, s_crop, crop);
1135 ret = client_g_rect(sd, cam_rect);
1136 if (ret < 0)
1137 return ret;
1138
1139 /*
1140 * Now cam_crop contains the current camera input rectangle, and it must
1141 * be within camera cropcap bounds
1142 */
1143 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
1144 /* Even if camera S_CROP failed, but camera rectangle matches */
1145 dev_dbg(dev, "Camera S_CROP successful for %dx%d@%d:%d\n",
1146 rect->width, rect->height, rect->left, rect->top);
1147 cam->rect = *cam_rect;
1148 return 0;
1149 }
1150
1151 /* Try to fix cropping, that camera hasn't managed to set */
1152 dev_geo(dev, "Fix camera S_CROP for %dx%d@%d:%d to %dx%d@%d:%d\n",
1153 cam_rect->width, cam_rect->height,
1154 cam_rect->left, cam_rect->top,
1155 rect->width, rect->height, rect->left, rect->top);
1156
1157 /* We need sensor maximum rectangle */
1158 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1159 if (ret < 0)
1160 return ret;
1161
1162 /* Put user requested rectangle within sensor bounds */
1163 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
1164 cap.bounds.width);
1165 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
1166 cap.bounds.height);
1167
1168 /*
1169 * Popular special case - some cameras can only handle fixed sizes like
1170 * QVGA, VGA,... Take care to avoid infinite loop.
1171 */
1172 width = max(cam_rect->width, 2);
1173 height = max(cam_rect->height, 2);
1174
1175 /*
1176 * Loop as long as sensor is not covering the requested rectangle and
1177 * is still within its bounds
1178 */
1179 while (!ret && (is_smaller(cam_rect, rect) ||
1180 is_inside(cam_rect, rect)) &&
1181 (cap.bounds.width > width || cap.bounds.height > height)) {
1182
1183 width *= 2;
1184 height *= 2;
1185
1186 cam_rect->width = width;
1187 cam_rect->height = height;
1188
1189 /*
1190 * We do not know what capabilities the camera has to set up
1191 * left and top borders. We could try to be smarter in iterating
1192 * them, e.g., if camera current left is to the right of the
1193 * target left, set it to the middle point between the current
1194 * left and minimum left. But that would add too much
1195 * complexity: we would have to iterate each border separately.
1196 * Instead we just drop to the left and top bounds.
1197 */
1198 if (cam_rect->left > rect->left)
1199 cam_rect->left = cap.bounds.left;
1200
1201 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
1202 cam_rect->width = rect->left + rect->width -
1203 cam_rect->left;
1204
1205 if (cam_rect->top > rect->top)
1206 cam_rect->top = cap.bounds.top;
1207
1208 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1209 cam_rect->height = rect->top + rect->height -
1210 cam_rect->top;
1211
1212 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1213 ret = client_g_rect(sd, cam_rect);
1214 dev_geo(dev, "Camera S_CROP %d for %dx%d@%d:%d\n", ret,
1215 cam_rect->width, cam_rect->height,
1216 cam_rect->left, cam_rect->top);
1217 }
1218
1219 /* S_CROP must not modify the rectangle */
1220 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
1221 /*
1222 * The camera failed to configure a suitable cropping,
1223 * we cannot use the current rectangle, set to max
1224 */
1225 *cam_rect = cap.bounds;
1226 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1227 ret = client_g_rect(sd, cam_rect);
1228 dev_geo(dev, "Camera S_CROP %d for max %dx%d@%d:%d\n", ret,
1229 cam_rect->width, cam_rect->height,
1230 cam_rect->left, cam_rect->top);
1231 }
1232
1233 if (!ret) {
1234 cam->rect = *cam_rect;
1235 update_subrect(cam);
1236 }
1237
1238 return ret;
1239 }
1240
1241 /* Iterative s_mbus_fmt, also updates cached client crop on success */
1242 static int client_s_fmt(struct soc_camera_device *icd,
1243 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
1244 {
1245 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1246 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1247 struct device *dev = icd->dev.parent;
1248 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
1249 unsigned int max_width, max_height;
1250 struct v4l2_cropcap cap;
1251 int ret;
1252
1253 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
1254 s_mbus_fmt, mf);
1255 if (ret < 0)
1256 return ret;
1257
1258 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
1259
1260 if ((width == mf->width && height == mf->height) || !ceu_can_scale)
1261 goto update_cache;
1262
1263 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1264
1265 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1266 if (ret < 0)
1267 return ret;
1268
1269 max_width = min(cap.bounds.width, 2560);
1270 max_height = min(cap.bounds.height, 1920);
1271
1272 /* Camera set a format, but geometry is not precise, try to improve */
1273 tmp_w = mf->width;
1274 tmp_h = mf->height;
1275
1276 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1277 while ((width > tmp_w || height > tmp_h) &&
1278 tmp_w < max_width && tmp_h < max_height) {
1279 tmp_w = min(2 * tmp_w, max_width);
1280 tmp_h = min(2 * tmp_h, max_height);
1281 mf->width = tmp_w;
1282 mf->height = tmp_h;
1283 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
1284 s_mbus_fmt, mf);
1285 dev_geo(dev, "Camera scaled to %ux%u\n",
1286 mf->width, mf->height);
1287 if (ret < 0) {
1288 /* This shouldn't happen */
1289 dev_err(dev, "Client failed to set format: %d\n", ret);
1290 return ret;
1291 }
1292 }
1293
1294 update_cache:
1295 /* Update cache */
1296 ret = client_g_rect(sd, &cam->rect);
1297 if (ret < 0)
1298 return ret;
1299
1300 update_subrect(cam);
1301
1302 return 0;
1303 }
1304
1305 /**
1306 * @width - on output: user width, mapped back to input
1307 * @height - on output: user height, mapped back to input
1308 * @mf - in- / output camera output window
1309 */
1310 static int client_scale(struct soc_camera_device *icd,
1311 struct v4l2_mbus_framefmt *mf,
1312 unsigned int *width, unsigned int *height,
1313 bool ceu_can_scale)
1314 {
1315 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1316 struct device *dev = icd->dev.parent;
1317 struct v4l2_mbus_framefmt mf_tmp = *mf;
1318 unsigned int scale_h, scale_v;
1319 int ret;
1320
1321 /*
1322 * 5. Apply iterative camera S_FMT for camera user window (also updates
1323 * client crop cache and the imaginary sub-rectangle).
1324 */
1325 ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
1326 if (ret < 0)
1327 return ret;
1328
1329 dev_geo(dev, "5: camera scaled to %ux%u\n",
1330 mf_tmp.width, mf_tmp.height);
1331
1332 /* 6. Retrieve camera output window (g_fmt) */
1333
1334 /* unneeded - it is already in "mf_tmp" */
1335
1336 /* 7. Calculate new client scales. */
1337 scale_h = calc_generic_scale(cam->rect.width, mf_tmp.width);
1338 scale_v = calc_generic_scale(cam->rect.height, mf_tmp.height);
1339
1340 mf->width = mf_tmp.width;
1341 mf->height = mf_tmp.height;
1342 mf->colorspace = mf_tmp.colorspace;
1343
1344 /*
1345 * 8. Calculate new CEU crop - apply camera scales to previously
1346 * updated "effective" crop.
1347 */
1348 *width = scale_down(cam->subrect.width, scale_h);
1349 *height = scale_down(cam->subrect.height, scale_v);
1350
1351 dev_geo(dev, "8: new client sub-window %ux%u\n", *width, *height);
1352
1353 return 0;
1354 }
1355
1356 /*
1357 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1358 * framerate by always requesting the maximum image from the client. See
1359 * Documentation/video4linux/sh_mobile_ceu_camera.txt for a description of
1360 * scaling and cropping algorithms and for the meaning of referenced here steps.
1361 */
1362 static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
1363 struct v4l2_crop *a)
1364 {
1365 struct v4l2_rect *rect = &a->c;
1366 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1367 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1368 struct v4l2_crop cam_crop;
1369 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1370 struct v4l2_rect *cam_rect = &cam_crop.c;
1371 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1372 struct device *dev = icd->dev.parent;
1373 struct v4l2_mbus_framefmt mf;
1374 unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
1375 out_width, out_height;
1376 int interm_width, interm_height;
1377 u32 capsr, cflcr;
1378 int ret;
1379
1380 dev_geo(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height,
1381 rect->left, rect->top);
1382
1383 /* During camera cropping its output window can change too, stop CEU */
1384 capsr = capture_save_reset(pcdev);
1385 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1386
1387 /* 1. - 2. Apply iterative camera S_CROP for new input window. */
1388 ret = client_s_crop(icd, a, &cam_crop);
1389 if (ret < 0)
1390 return ret;
1391
1392 dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
1393 cam_rect->width, cam_rect->height,
1394 cam_rect->left, cam_rect->top);
1395
1396 /* On success cam_crop contains current camera crop */
1397
1398 /* 3. Retrieve camera output window */
1399 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1400 if (ret < 0)
1401 return ret;
1402
1403 if (mf.width > 2560 || mf.height > 1920)
1404 return -EINVAL;
1405
1406 /* 4. Calculate camera scales */
1407 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1408 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
1409
1410 /* Calculate intermediate window */
1411 interm_width = scale_down(rect->width, scale_cam_h);
1412 interm_height = scale_down(rect->height, scale_cam_v);
1413
1414 if (interm_width < icd->user_width) {
1415 u32 new_scale_h;
1416
1417 new_scale_h = calc_generic_scale(rect->width, icd->user_width);
1418
1419 mf.width = scale_down(cam_rect->width, new_scale_h);
1420 }
1421
1422 if (interm_height < icd->user_height) {
1423 u32 new_scale_v;
1424
1425 new_scale_v = calc_generic_scale(rect->height, icd->user_height);
1426
1427 mf.height = scale_down(cam_rect->height, new_scale_v);
1428 }
1429
1430 if (interm_width < icd->user_width || interm_height < icd->user_height) {
1431 ret = v4l2_device_call_until_err(sd->v4l2_dev, (int)icd, video,
1432 s_mbus_fmt, &mf);
1433 if (ret < 0)
1434 return ret;
1435
1436 dev_geo(dev, "New camera output %ux%u\n", mf.width, mf.height);
1437 scale_cam_h = calc_generic_scale(cam_rect->width, mf.width);
1438 scale_cam_v = calc_generic_scale(cam_rect->height, mf.height);
1439 interm_width = scale_down(rect->width, scale_cam_h);
1440 interm_height = scale_down(rect->height, scale_cam_v);
1441 }
1442
1443 /* Cache camera output window */
1444 cam->width = mf.width;
1445 cam->height = mf.height;
1446
1447 if (pcdev->image_mode) {
1448 out_width = min(interm_width, icd->user_width);
1449 out_height = min(interm_height, icd->user_height);
1450 } else {
1451 out_width = interm_width;
1452 out_height = interm_height;
1453 }
1454
1455 /*
1456 * 5. Calculate CEU scales from camera scales from results of (5) and
1457 * the user window
1458 */
1459 scale_ceu_h = calc_scale(interm_width, &out_width);
1460 scale_ceu_v = calc_scale(interm_height, &out_height);
1461
1462 dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
1463
1464 /* Apply CEU scales. */
1465 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1466 if (cflcr != pcdev->cflcr) {
1467 pcdev->cflcr = cflcr;
1468 ceu_write(pcdev, CFLCR, cflcr);
1469 }
1470
1471 icd->user_width = out_width;
1472 icd->user_height = out_height;
1473 cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1;
1474 cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1;
1475
1476 /* 6. Use CEU cropping to crop to the new window. */
1477 sh_mobile_ceu_set_rect(icd);
1478
1479 cam->subrect = *rect;
1480
1481 dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
1482 icd->user_width, icd->user_height,
1483 cam->ceu_left, cam->ceu_top);
1484
1485 /* Restore capture. The CE bit can be cleared by the hardware */
1486 if (pcdev->active)
1487 capsr |= 1;
1488 capture_restore(pcdev, capsr);
1489
1490 /* Even if only camera cropping succeeded */
1491 return ret;
1492 }
1493
1494 static int sh_mobile_ceu_get_crop(struct soc_camera_device *icd,
1495 struct v4l2_crop *a)
1496 {
1497 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1498
1499 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1500 a->c = cam->subrect;
1501
1502 return 0;
1503 }
1504
1505 /*
1506 * Calculate real client output window by applying new scales to the current
1507 * client crop. New scales are calculated from the requested output format and
1508 * CEU crop, mapped backed onto the client input (subrect).
1509 */
1510 static void calculate_client_output(struct soc_camera_device *icd,
1511 struct v4l2_pix_format *pix, struct v4l2_mbus_framefmt *mf)
1512 {
1513 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1514 struct device *dev = icd->dev.parent;
1515 struct v4l2_rect *cam_subrect = &cam->subrect;
1516 unsigned int scale_v, scale_h;
1517
1518 if (cam_subrect->width == cam->rect.width &&
1519 cam_subrect->height == cam->rect.height) {
1520 /* No sub-cropping */
1521 mf->width = pix->width;
1522 mf->height = pix->height;
1523 return;
1524 }
1525
1526 /* 1.-2. Current camera scales and subwin - cached. */
1527
1528 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1529 cam_subrect->width, cam_subrect->height,
1530 cam_subrect->left, cam_subrect->top);
1531
1532 /*
1533 * 3. Calculate new combined scales from input sub-window to requested
1534 * user window.
1535 */
1536
1537 /*
1538 * TODO: CEU cannot scale images larger than VGA to smaller than SubQCIF
1539 * (128x96) or larger than VGA
1540 */
1541 scale_h = calc_generic_scale(cam_subrect->width, pix->width);
1542 scale_v = calc_generic_scale(cam_subrect->height, pix->height);
1543
1544 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1545
1546 /*
1547 * 4. Calculate client output window by applying combined scales to real
1548 * input window.
1549 */
1550 mf->width = scale_down(cam->rect.width, scale_h);
1551 mf->height = scale_down(cam->rect.height, scale_v);
1552 }
1553
1554 /* Similar to set_crop multistage iterative algorithm */
1555 static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
1556 struct v4l2_format *f)
1557 {
1558 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1559 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1560 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1561 struct v4l2_pix_format *pix = &f->fmt.pix;
1562 struct v4l2_mbus_framefmt mf;
1563 struct device *dev = icd->dev.parent;
1564 __u32 pixfmt = pix->pixelformat;
1565 const struct soc_camera_format_xlate *xlate;
1566 /* Keep Compiler Happy */
1567 unsigned int ceu_sub_width = 0, ceu_sub_height = 0;
1568 u16 scale_v, scale_h;
1569 int ret;
1570 bool image_mode;
1571 enum v4l2_field field;
1572
1573 dev_geo(dev, "S_FMT(pix=0x%x, %ux%u)\n", pixfmt, pix->width, pix->height);
1574
1575 switch (pix->field) {
1576 default:
1577 pix->field = V4L2_FIELD_NONE;
1578 /* fall-through */
1579 case V4L2_FIELD_INTERLACED_TB:
1580 case V4L2_FIELD_INTERLACED_BT:
1581 case V4L2_FIELD_NONE:
1582 field = pix->field;
1583 break;
1584 case V4L2_FIELD_INTERLACED:
1585 field = V4L2_FIELD_INTERLACED_TB;
1586 break;
1587 }
1588
1589 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1590 if (!xlate) {
1591 dev_warn(dev, "Format %x not found\n", pixfmt);
1592 return -EINVAL;
1593 }
1594
1595 /* 1.-4. Calculate client output geometry */
1596 calculate_client_output(icd, &f->fmt.pix, &mf);
1597 mf.field = pix->field;
1598 mf.colorspace = pix->colorspace;
1599 mf.code = xlate->code;
1600
1601 switch (pixfmt) {
1602 case V4L2_PIX_FMT_NV12:
1603 case V4L2_PIX_FMT_NV21:
1604 case V4L2_PIX_FMT_NV16:
1605 case V4L2_PIX_FMT_NV61:
1606 image_mode = true;
1607 break;
1608 default:
1609 image_mode = false;
1610 }
1611
1612 dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
1613
1614 /* 5. - 9. */
1615 ret = client_scale(icd, &mf, &ceu_sub_width, &ceu_sub_height,
1616 image_mode && V4L2_FIELD_NONE == field);
1617
1618 dev_geo(dev, "5-9: client scale return %d\n", ret);
1619
1620 /* Done with the camera. Now see if we can improve the result */
1621
1622 dev_geo(dev, "fmt %ux%u, requested %ux%u\n",
1623 mf.width, mf.height, pix->width, pix->height);
1624 if (ret < 0)
1625 return ret;
1626
1627 if (mf.code != xlate->code)
1628 return -EINVAL;
1629
1630 /* 9. Prepare CEU crop */
1631 cam->width = mf.width;
1632 cam->height = mf.height;
1633
1634 /* 10. Use CEU scaling to scale to the requested user window. */
1635
1636 /* We cannot scale up */
1637 if (pix->width > ceu_sub_width)
1638 ceu_sub_width = pix->width;
1639
1640 if (pix->height > ceu_sub_height)
1641 ceu_sub_height = pix->height;
1642
1643 pix->colorspace = mf.colorspace;
1644
1645 if (image_mode) {
1646 /* Scale pix->{width x height} down to width x height */
1647 scale_h = calc_scale(ceu_sub_width, &pix->width);
1648 scale_v = calc_scale(ceu_sub_height, &pix->height);
1649 } else {
1650 pix->width = ceu_sub_width;
1651 pix->height = ceu_sub_height;
1652 scale_h = 0;
1653 scale_v = 0;
1654 }
1655
1656 pcdev->cflcr = scale_h | (scale_v << 16);
1657
1658 /*
1659 * We have calculated CFLCR, the actual configuration will be performed
1660 * in sh_mobile_ceu_set_bus_param()
1661 */
1662
1663 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1664 ceu_sub_width, scale_h, pix->width,
1665 ceu_sub_height, scale_v, pix->height);
1666
1667 cam->code = xlate->code;
1668 icd->current_fmt = xlate;
1669
1670 pcdev->field = field;
1671 pcdev->image_mode = image_mode;
1672
1673 return 0;
1674 }
1675
1676 static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1677 struct v4l2_format *f)
1678 {
1679 const struct soc_camera_format_xlate *xlate;
1680 struct v4l2_pix_format *pix = &f->fmt.pix;
1681 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1682 struct v4l2_mbus_framefmt mf;
1683 __u32 pixfmt = pix->pixelformat;
1684 int width, height;
1685 int ret;
1686
1687 dev_geo(icd->dev.parent, "TRY_FMT(pix=0x%x, %ux%u)\n",
1688 pixfmt, pix->width, pix->height);
1689
1690 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1691 if (!xlate) {
1692 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
1693 return -EINVAL;
1694 }
1695
1696 /* FIXME: calculate using depth and bus width */
1697
1698 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1699 &pix->height, 4, 1920, 2, 0);
1700
1701 width = pix->width;
1702 height = pix->height;
1703
1704 pix->bytesperline = soc_mbus_bytes_per_line(width, xlate->host_fmt);
1705 if ((int)pix->bytesperline < 0)
1706 return pix->bytesperline;
1707 pix->sizeimage = height * pix->bytesperline;
1708
1709 /* limit to sensor capabilities */
1710 mf.width = pix->width;
1711 mf.height = pix->height;
1712 mf.field = pix->field;
1713 mf.code = xlate->code;
1714 mf.colorspace = pix->colorspace;
1715
1716 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video, try_mbus_fmt, &mf);
1717 if (ret < 0)
1718 return ret;
1719
1720 pix->width = mf.width;
1721 pix->height = mf.height;
1722 pix->field = mf.field;
1723 pix->colorspace = mf.colorspace;
1724
1725 switch (pixfmt) {
1726 case V4L2_PIX_FMT_NV12:
1727 case V4L2_PIX_FMT_NV21:
1728 case V4L2_PIX_FMT_NV16:
1729 case V4L2_PIX_FMT_NV61:
1730 /* FIXME: check against rect_max after converting soc-camera */
1731 /* We can scale precisely, need a bigger image from camera */
1732 if (pix->width < width || pix->height < height) {
1733 /*
1734 * We presume, the sensor behaves sanely, i.e., if
1735 * requested a bigger rectangle, it will not return a
1736 * smaller one.
1737 */
1738 mf.width = 2560;
1739 mf.height = 1920;
1740 ret = v4l2_device_call_until_err(sd->v4l2_dev, (long)icd, video,
1741 try_mbus_fmt, &mf);
1742 if (ret < 0) {
1743 /* Shouldn't actually happen... */
1744 dev_err(icd->dev.parent,
1745 "FIXME: client try_fmt() = %d\n", ret);
1746 return ret;
1747 }
1748 }
1749 /* We will scale exactly */
1750 if (mf.width > width)
1751 pix->width = width;
1752 if (mf.height > height)
1753 pix->height = height;
1754 }
1755
1756 dev_geo(icd->dev.parent, "%s(): return %d, fmt 0x%x, %ux%u\n",
1757 __func__, ret, pix->pixelformat, pix->width, pix->height);
1758
1759 return ret;
1760 }
1761
1762 static int sh_mobile_ceu_set_livecrop(struct soc_camera_device *icd,
1763 struct v4l2_crop *a)
1764 {
1765 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1766 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1767 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1768 u32 out_width = icd->user_width, out_height = icd->user_height;
1769 int ret;
1770
1771 /* Freeze queue */
1772 pcdev->frozen = 1;
1773 /* Wait for frame */
1774 ret = wait_for_completion_interruptible(&pcdev->complete);
1775 /* Stop the client */
1776 ret = v4l2_subdev_call(sd, video, s_stream, 0);
1777 if (ret < 0)
1778 dev_warn(icd->dev.parent,
1779 "Client failed to stop the stream: %d\n", ret);
1780 else
1781 /* Do the crop, if it fails, there's nothing more we can do */
1782 sh_mobile_ceu_set_crop(icd, a);
1783
1784 dev_geo(icd->dev.parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height);
1785
1786 if (icd->user_width != out_width || icd->user_height != out_height) {
1787 struct v4l2_format f = {
1788 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1789 .fmt.pix = {
1790 .width = out_width,
1791 .height = out_height,
1792 .pixelformat = icd->current_fmt->host_fmt->fourcc,
1793 .field = pcdev->field,
1794 .colorspace = icd->colorspace,
1795 },
1796 };
1797 ret = sh_mobile_ceu_set_fmt(icd, &f);
1798 if (!ret && (out_width != f.fmt.pix.width ||
1799 out_height != f.fmt.pix.height))
1800 ret = -EINVAL;
1801 if (!ret) {
1802 icd->user_width = out_width;
1803 icd->user_height = out_height;
1804 ret = sh_mobile_ceu_set_bus_param(icd,
1805 icd->current_fmt->host_fmt->fourcc);
1806 }
1807 }
1808
1809 /* Thaw the queue */
1810 pcdev->frozen = 0;
1811 spin_lock_irq(&pcdev->lock);
1812 sh_mobile_ceu_capture(pcdev);
1813 spin_unlock_irq(&pcdev->lock);
1814 /* Start the client */
1815 ret = v4l2_subdev_call(sd, video, s_stream, 1);
1816 return ret;
1817 }
1818
1819 static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1820 {
1821 struct soc_camera_device *icd = file->private_data;
1822
1823 return vb2_poll(&icd->vb2_vidq, file, pt);
1824 }
1825
1826 static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1827 struct v4l2_capability *cap)
1828 {
1829 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1830 cap->version = KERNEL_VERSION(0, 0, 5);
1831 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1832 return 0;
1833 }
1834
1835 static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q,
1836 struct soc_camera_device *icd)
1837 {
1838 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1839 q->io_modes = VB2_MMAP | VB2_USERPTR;
1840 q->drv_priv = icd;
1841 q->ops = &sh_mobile_ceu_videobuf_ops;
1842 q->mem_ops = &vb2_dma_contig_memops;
1843 q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer);
1844
1845 return vb2_queue_init(q);
1846 }
1847
1848 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1849 struct v4l2_control *ctrl)
1850 {
1851 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1852 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1853 u32 val;
1854
1855 switch (ctrl->id) {
1856 case V4L2_CID_SHARPNESS:
1857 val = ceu_read(pcdev, CLFCR);
1858 ctrl->value = val ^ 1;
1859 return 0;
1860 }
1861 return -ENOIOCTLCMD;
1862 }
1863
1864 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1865 struct v4l2_control *ctrl)
1866 {
1867 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1868 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1869
1870 switch (ctrl->id) {
1871 case V4L2_CID_SHARPNESS:
1872 switch (icd->current_fmt->host_fmt->fourcc) {
1873 case V4L2_PIX_FMT_NV12:
1874 case V4L2_PIX_FMT_NV21:
1875 case V4L2_PIX_FMT_NV16:
1876 case V4L2_PIX_FMT_NV61:
1877 ceu_write(pcdev, CLFCR, !ctrl->value);
1878 return 0;
1879 }
1880 return -EINVAL;
1881 }
1882 return -ENOIOCTLCMD;
1883 }
1884
1885 static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1886 {
1887 .id = V4L2_CID_SHARPNESS,
1888 .type = V4L2_CTRL_TYPE_BOOLEAN,
1889 .name = "Low-pass filter",
1890 .minimum = 0,
1891 .maximum = 1,
1892 .step = 1,
1893 .default_value = 0,
1894 },
1895 };
1896
1897 static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1898 .owner = THIS_MODULE,
1899 .add = sh_mobile_ceu_add_device,
1900 .remove = sh_mobile_ceu_remove_device,
1901 .get_formats = sh_mobile_ceu_get_formats,
1902 .put_formats = sh_mobile_ceu_put_formats,
1903 .get_crop = sh_mobile_ceu_get_crop,
1904 .set_crop = sh_mobile_ceu_set_crop,
1905 .set_livecrop = sh_mobile_ceu_set_livecrop,
1906 .set_fmt = sh_mobile_ceu_set_fmt,
1907 .try_fmt = sh_mobile_ceu_try_fmt,
1908 .set_ctrl = sh_mobile_ceu_set_ctrl,
1909 .get_ctrl = sh_mobile_ceu_get_ctrl,
1910 .poll = sh_mobile_ceu_poll,
1911 .querycap = sh_mobile_ceu_querycap,
1912 .set_bus_param = sh_mobile_ceu_set_bus_param,
1913 .init_videobuf2 = sh_mobile_ceu_init_videobuf,
1914 .controls = sh_mobile_ceu_controls,
1915 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
1916 };
1917
1918 struct bus_wait {
1919 struct notifier_block notifier;
1920 struct completion completion;
1921 struct device *dev;
1922 };
1923
1924 static int bus_notify(struct notifier_block *nb,
1925 unsigned long action, void *data)
1926 {
1927 struct device *dev = data;
1928 struct bus_wait *wait = container_of(nb, struct bus_wait, notifier);
1929
1930 if (wait->dev != dev)
1931 return NOTIFY_DONE;
1932
1933 switch (action) {
1934 case BUS_NOTIFY_UNBOUND_DRIVER:
1935 /* Protect from module unloading */
1936 wait_for_completion(&wait->completion);
1937 return NOTIFY_OK;
1938 }
1939 return NOTIFY_DONE;
1940 }
1941
1942 static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
1943 {
1944 struct sh_mobile_ceu_dev *pcdev;
1945 struct resource *res;
1946 void __iomem *base;
1947 unsigned int irq;
1948 int err = 0;
1949 struct bus_wait wait = {
1950 .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion),
1951 .notifier.notifier_call = bus_notify,
1952 };
1953 struct device *csi2;
1954
1955 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1956 irq = platform_get_irq(pdev, 0);
1957 if (!res || (int)irq <= 0) {
1958 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1959 err = -ENODEV;
1960 goto exit;
1961 }
1962
1963 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1964 if (!pcdev) {
1965 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1966 err = -ENOMEM;
1967 goto exit;
1968 }
1969
1970 INIT_LIST_HEAD(&pcdev->capture);
1971 spin_lock_init(&pcdev->lock);
1972 init_completion(&pcdev->complete);
1973
1974 pcdev->pdata = pdev->dev.platform_data;
1975 if (!pcdev->pdata) {
1976 err = -EINVAL;
1977 dev_err(&pdev->dev, "CEU platform data not set.\n");
1978 goto exit_kfree;
1979 }
1980
1981 base = ioremap_nocache(res->start, resource_size(res));
1982 if (!base) {
1983 err = -ENXIO;
1984 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1985 goto exit_kfree;
1986 }
1987
1988 pcdev->irq = irq;
1989 pcdev->base = base;
1990 pcdev->video_limit = 0; /* only enabled if second resource exists */
1991
1992 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1993 if (res) {
1994 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1995 res->start,
1996 resource_size(res),
1997 DMA_MEMORY_MAP |
1998 DMA_MEMORY_EXCLUSIVE);
1999 if (!err) {
2000 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
2001 err = -ENXIO;
2002 goto exit_iounmap;
2003 }
2004
2005 pcdev->video_limit = resource_size(res);
2006 }
2007
2008 /* request irq */
2009 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
2010 dev_name(&pdev->dev), pcdev);
2011 if (err) {
2012 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
2013 goto exit_release_mem;
2014 }
2015
2016 pm_suspend_ignore_children(&pdev->dev, true);
2017 pm_runtime_enable(&pdev->dev);
2018 pm_runtime_resume(&pdev->dev);
2019
2020 pcdev->ici.priv = pcdev;
2021 pcdev->ici.v4l2_dev.dev = &pdev->dev;
2022 pcdev->ici.nr = pdev->id;
2023 pcdev->ici.drv_name = dev_name(&pdev->dev);
2024 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
2025
2026 /* CSI2 interfacing */
2027 csi2 = pcdev->pdata->csi2_dev;
2028 if (csi2) {
2029 wait.dev = csi2;
2030
2031 err = bus_register_notifier(&platform_bus_type, &wait.notifier);
2032 if (err < 0)
2033 goto exit_free_clk;
2034
2035 /*
2036 * From this point the driver module will not unload, until
2037 * we complete the completion.
2038 */
2039
2040 if (!csi2->driver) {
2041 complete(&wait.completion);
2042 /* Either too late, or probing failed */
2043 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2044 err = -ENXIO;
2045 goto exit_free_clk;
2046 }
2047
2048 /*
2049 * The module is still loaded, in the worst case it is hanging
2050 * in device release on our completion. So, _now_ dereferencing
2051 * the "owner" is safe!
2052 */
2053
2054 err = try_module_get(csi2->driver->owner);
2055
2056 /* Let notifier complete, if it has been locked */
2057 complete(&wait.completion);
2058 bus_unregister_notifier(&platform_bus_type, &wait.notifier);
2059 if (!err) {
2060 err = -ENODEV;
2061 goto exit_free_clk;
2062 }
2063 }
2064
2065 pcdev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2066 if (IS_ERR(pcdev->alloc_ctx)) {
2067 err = PTR_ERR(pcdev->alloc_ctx);
2068 goto exit_module_put;
2069 }
2070
2071 err = soc_camera_host_register(&pcdev->ici);
2072 if (err)
2073 goto exit_free_ctx;
2074
2075 return 0;
2076
2077 exit_free_ctx:
2078 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
2079 exit_module_put:
2080 if (csi2 && csi2->driver)
2081 module_put(csi2->driver->owner);
2082 exit_free_clk:
2083 pm_runtime_disable(&pdev->dev);
2084 free_irq(pcdev->irq, pcdev);
2085 exit_release_mem:
2086 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2087 dma_release_declared_memory(&pdev->dev);
2088 exit_iounmap:
2089 iounmap(base);
2090 exit_kfree:
2091 kfree(pcdev);
2092 exit:
2093 return err;
2094 }
2095
2096 static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
2097 {
2098 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
2099 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
2100 struct sh_mobile_ceu_dev, ici);
2101 struct device *csi2 = pcdev->pdata->csi2_dev;
2102
2103 soc_camera_host_unregister(soc_host);
2104 pm_runtime_disable(&pdev->dev);
2105 free_irq(pcdev->irq, pcdev);
2106 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
2107 dma_release_declared_memory(&pdev->dev);
2108 iounmap(pcdev->base);
2109 vb2_dma_contig_cleanup_ctx(pcdev->alloc_ctx);
2110 if (csi2 && csi2->driver)
2111 module_put(csi2->driver->owner);
2112 kfree(pcdev);
2113
2114 return 0;
2115 }
2116
2117 static int sh_mobile_ceu_runtime_nop(struct device *dev)
2118 {
2119 /* Runtime PM callback shared between ->runtime_suspend()
2120 * and ->runtime_resume(). Simply returns success.
2121 *
2122 * This driver re-initializes all registers after
2123 * pm_runtime_get_sync() anyway so there is no need
2124 * to save and restore registers here.
2125 */
2126 return 0;
2127 }
2128
2129 static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
2130 .runtime_suspend = sh_mobile_ceu_runtime_nop,
2131 .runtime_resume = sh_mobile_ceu_runtime_nop,
2132 };
2133
2134 static struct platform_driver sh_mobile_ceu_driver = {
2135 .driver = {
2136 .name = "sh_mobile_ceu",
2137 .pm = &sh_mobile_ceu_dev_pm_ops,
2138 },
2139 .probe = sh_mobile_ceu_probe,
2140 .remove = __devexit_p(sh_mobile_ceu_remove),
2141 };
2142
2143 static int __init sh_mobile_ceu_init(void)
2144 {
2145 /* Whatever return code */
2146 request_module("sh_mobile_csi2");
2147 return platform_driver_register(&sh_mobile_ceu_driver);
2148 }
2149
2150 static void __exit sh_mobile_ceu_exit(void)
2151 {
2152 platform_driver_unregister(&sh_mobile_ceu_driver);
2153 }
2154
2155 module_init(sh_mobile_ceu_init);
2156 module_exit(sh_mobile_ceu_exit);
2157
2158 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
2159 MODULE_AUTHOR("Magnus Damm");
2160 MODULE_LICENSE("GPL");
2161 MODULE_ALIAS("platform:sh_mobile_ceu");