Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / media / video / sh_mobile_ceu_camera.c
blob6e16b3979326fd2e39b6bfc910df9fa66ef23e1e
1 /*
2 * V4L2 Driver for SuperH Mobile CEU interface
4 * Copyright (C) 2008 Magnus Damm
6 * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
8 * Copyright (C) 2006, Sascha Hauer, Pengutronix
9 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
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.
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/slab.h>
31 #include <linux/device.h>
32 #include <linux/platform_device.h>
33 #include <linux/videodev2.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/sched.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-dev.h>
39 #include <media/soc_camera.h>
40 #include <media/sh_mobile_ceu.h>
41 #include <media/videobuf-dma-contig.h>
42 #include <media/v4l2-mediabus.h>
43 #include <media/soc_mediabus.h>
45 /* register offsets for sh7722 / sh7723 */
47 #define CAPSR 0x00 /* Capture start register */
48 #define CAPCR 0x04 /* Capture control register */
49 #define CAMCR 0x08 /* Capture interface control register */
50 #define CMCYR 0x0c /* Capture interface cycle register */
51 #define CAMOR 0x10 /* Capture interface offset register */
52 #define CAPWR 0x14 /* Capture interface width register */
53 #define CAIFR 0x18 /* Capture interface input format register */
54 #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
55 #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
56 #define CRCNTR 0x28 /* CEU register control register */
57 #define CRCMPR 0x2c /* CEU register forcible control register */
58 #define CFLCR 0x30 /* Capture filter control register */
59 #define CFSZR 0x34 /* Capture filter size clip register */
60 #define CDWDR 0x38 /* Capture destination width register */
61 #define CDAYR 0x3c /* Capture data address Y register */
62 #define CDACR 0x40 /* Capture data address C register */
63 #define CDBYR 0x44 /* Capture data bottom-field address Y register */
64 #define CDBCR 0x48 /* Capture data bottom-field address C register */
65 #define CBDSR 0x4c /* Capture bundle destination size register */
66 #define CFWCR 0x5c /* Firewall operation control register */
67 #define CLFCR 0x60 /* Capture low-pass filter control register */
68 #define CDOCR 0x64 /* Capture data output control register */
69 #define CDDCR 0x68 /* Capture data complexity level register */
70 #define CDDAR 0x6c /* Capture data complexity level address register */
71 #define CEIER 0x70 /* Capture event interrupt enable register */
72 #define CETCR 0x74 /* Capture event flag clear register */
73 #define CSTSR 0x7c /* Capture status register */
74 #define CSRTR 0x80 /* Capture software reset register */
75 #define CDSSR 0x84 /* Capture data size register */
76 #define CDAYR2 0x90 /* Capture data address Y register 2 */
77 #define CDACR2 0x94 /* Capture data address C register 2 */
78 #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
79 #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
81 #undef DEBUG_GEOMETRY
82 #ifdef DEBUG_GEOMETRY
83 #define dev_geo dev_info
84 #else
85 #define dev_geo dev_dbg
86 #endif
88 /* per video frame buffer */
89 struct sh_mobile_ceu_buffer {
90 struct videobuf_buffer vb; /* v4l buffer must be first */
91 enum v4l2_mbus_pixelcode code;
94 struct sh_mobile_ceu_dev {
95 struct soc_camera_host ici;
96 struct soc_camera_device *icd;
98 unsigned int irq;
99 void __iomem *base;
100 unsigned long video_limit;
102 /* lock used to protect videobuf */
103 spinlock_t lock;
104 struct list_head capture;
105 struct videobuf_buffer *active;
107 struct sh_mobile_ceu_info *pdata;
109 u32 cflcr;
111 enum v4l2_field field;
113 unsigned int image_mode:1;
114 unsigned int is_16bit:1;
117 struct sh_mobile_ceu_cam {
118 struct v4l2_rect ceu_rect;
119 unsigned int cam_width;
120 unsigned int cam_height;
121 const struct soc_mbus_pixelfmt *extra_fmt;
122 enum v4l2_mbus_pixelcode code;
125 static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
127 unsigned long flags;
129 flags = SOCAM_MASTER |
130 SOCAM_PCLK_SAMPLE_RISING |
131 SOCAM_HSYNC_ACTIVE_HIGH |
132 SOCAM_HSYNC_ACTIVE_LOW |
133 SOCAM_VSYNC_ACTIVE_HIGH |
134 SOCAM_VSYNC_ACTIVE_LOW |
135 SOCAM_DATA_ACTIVE_HIGH;
137 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
138 flags |= SOCAM_DATAWIDTH_8;
140 if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
141 flags |= SOCAM_DATAWIDTH_16;
143 if (flags & SOCAM_DATAWIDTH_MASK)
144 return flags;
146 return 0;
149 static void ceu_write(struct sh_mobile_ceu_dev *priv,
150 unsigned long reg_offs, u32 data)
152 iowrite32(data, priv->base + reg_offs);
155 static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
157 return ioread32(priv->base + reg_offs);
160 static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
162 int i, success = 0;
163 struct soc_camera_device *icd = pcdev->icd;
165 ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
167 /* wait CSTSR.CPTON bit */
168 for (i = 0; i < 1000; i++) {
169 if (!(ceu_read(pcdev, CSTSR) & 1)) {
170 success++;
171 break;
173 udelay(1);
176 /* wait CAPSR.CPKIL bit */
177 for (i = 0; i < 1000; i++) {
178 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
179 success++;
180 break;
182 udelay(1);
186 if (2 != success) {
187 dev_warn(&icd->dev, "soft reset time out\n");
188 return -EIO;
191 return 0;
195 * Videobuf operations
197 static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
198 unsigned int *count,
199 unsigned int *size)
201 struct soc_camera_device *icd = vq->priv_data;
202 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
203 struct sh_mobile_ceu_dev *pcdev = ici->priv;
204 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
205 icd->current_fmt->host_fmt);
207 if (bytes_per_line < 0)
208 return bytes_per_line;
210 *size = bytes_per_line * icd->user_height;
212 if (0 == *count)
213 *count = 2;
215 if (pcdev->video_limit) {
216 while (PAGE_ALIGN(*size) * *count > pcdev->video_limit)
217 (*count)--;
220 dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
222 return 0;
225 static void free_buffer(struct videobuf_queue *vq,
226 struct sh_mobile_ceu_buffer *buf)
228 struct soc_camera_device *icd = vq->priv_data;
229 struct device *dev = icd->dev.parent;
231 dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
232 &buf->vb, buf->vb.baddr, buf->vb.bsize);
234 if (in_interrupt())
235 BUG();
237 videobuf_waiton(&buf->vb, 0, 0);
238 videobuf_dma_contig_free(vq, &buf->vb);
239 dev_dbg(dev, "%s freed\n", __func__);
240 buf->vb.state = VIDEOBUF_NEEDS_INIT;
243 #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
244 #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
245 #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
246 #define CEU_CEIER_VBP (1 << 20) /* vbp error */
247 #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
248 #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
252 * return value doesn't reflex the success/failure to queue the new buffer,
253 * but rather the status of the previous buffer.
255 static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
257 struct soc_camera_device *icd = pcdev->icd;
258 dma_addr_t phys_addr_top, phys_addr_bottom;
259 unsigned long top1, top2;
260 unsigned long bottom1, bottom2;
261 u32 status;
262 int ret = 0;
265 * The hardware is _very_ picky about this sequence. Especially
266 * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
267 * several not-so-well documented interrupt sources in CETCR.
269 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
270 status = ceu_read(pcdev, CETCR);
271 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
272 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
273 ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
274 ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
277 * When a VBP interrupt occurs, a capture end interrupt does not occur
278 * and the image of that frame is not captured correctly. So, soft reset
279 * is needed here.
281 if (status & CEU_CEIER_VBP) {
282 sh_mobile_ceu_soft_reset(pcdev);
283 ret = -EIO;
286 if (!pcdev->active)
287 return ret;
289 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
290 top1 = CDBYR;
291 top2 = CDBCR;
292 bottom1 = CDAYR;
293 bottom2 = CDACR;
294 } else {
295 top1 = CDAYR;
296 top2 = CDACR;
297 bottom1 = CDBYR;
298 bottom2 = CDBCR;
301 phys_addr_top = videobuf_to_dma_contig(pcdev->active);
302 ceu_write(pcdev, top1, phys_addr_top);
303 if (V4L2_FIELD_NONE != pcdev->field) {
304 phys_addr_bottom = phys_addr_top + icd->user_width;
305 ceu_write(pcdev, bottom1, phys_addr_bottom);
308 switch (icd->current_fmt->host_fmt->fourcc) {
309 case V4L2_PIX_FMT_NV12:
310 case V4L2_PIX_FMT_NV21:
311 case V4L2_PIX_FMT_NV16:
312 case V4L2_PIX_FMT_NV61:
313 phys_addr_top += icd->user_width *
314 icd->user_height;
315 ceu_write(pcdev, top2, phys_addr_top);
316 if (V4L2_FIELD_NONE != pcdev->field) {
317 phys_addr_bottom = phys_addr_top + icd->user_width;
318 ceu_write(pcdev, bottom2, phys_addr_bottom);
322 pcdev->active->state = VIDEOBUF_ACTIVE;
323 ceu_write(pcdev, CAPSR, 0x1); /* start capture */
325 return ret;
328 static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
329 struct videobuf_buffer *vb,
330 enum v4l2_field field)
332 struct soc_camera_device *icd = vq->priv_data;
333 struct sh_mobile_ceu_buffer *buf;
334 int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
335 icd->current_fmt->host_fmt);
336 int ret;
338 if (bytes_per_line < 0)
339 return bytes_per_line;
341 buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
343 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
344 vb, vb->baddr, vb->bsize);
346 /* Added list head initialization on alloc */
347 WARN_ON(!list_empty(&vb->queue));
349 #ifdef DEBUG
351 * This can be useful if you want to see if we actually fill
352 * the buffer with something
354 memset((void *)vb->baddr, 0xaa, vb->bsize);
355 #endif
357 BUG_ON(NULL == icd->current_fmt);
359 if (buf->code != icd->current_fmt->code ||
360 vb->width != icd->user_width ||
361 vb->height != icd->user_height ||
362 vb->field != field) {
363 buf->code = icd->current_fmt->code;
364 vb->width = icd->user_width;
365 vb->height = icd->user_height;
366 vb->field = field;
367 vb->state = VIDEOBUF_NEEDS_INIT;
370 vb->size = vb->height * bytes_per_line;
371 if (0 != vb->baddr && vb->bsize < vb->size) {
372 ret = -EINVAL;
373 goto out;
376 if (vb->state == VIDEOBUF_NEEDS_INIT) {
377 ret = videobuf_iolock(vq, vb, NULL);
378 if (ret)
379 goto fail;
380 vb->state = VIDEOBUF_PREPARED;
383 return 0;
384 fail:
385 free_buffer(vq, buf);
386 out:
387 return ret;
390 /* Called under spinlock_irqsave(&pcdev->lock, ...) */
391 static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
392 struct videobuf_buffer *vb)
394 struct soc_camera_device *icd = vq->priv_data;
395 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
396 struct sh_mobile_ceu_dev *pcdev = ici->priv;
398 dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
399 vb, vb->baddr, vb->bsize);
401 vb->state = VIDEOBUF_QUEUED;
402 list_add_tail(&vb->queue, &pcdev->capture);
404 if (!pcdev->active) {
406 * Because there were no active buffer at this moment,
407 * we are not interested in the return value of
408 * sh_mobile_ceu_capture here.
410 pcdev->active = vb;
411 sh_mobile_ceu_capture(pcdev);
415 static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
416 struct videobuf_buffer *vb)
418 struct soc_camera_device *icd = vq->priv_data;
419 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
420 struct sh_mobile_ceu_dev *pcdev = ici->priv;
421 unsigned long flags;
423 spin_lock_irqsave(&pcdev->lock, flags);
425 if (pcdev->active == vb) {
426 /* disable capture (release DMA buffer), reset */
427 ceu_write(pcdev, CAPSR, 1 << 16);
428 pcdev->active = NULL;
431 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
432 !list_empty(&vb->queue)) {
433 vb->state = VIDEOBUF_ERROR;
434 list_del_init(&vb->queue);
437 spin_unlock_irqrestore(&pcdev->lock, flags);
439 free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
442 static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
443 .buf_setup = sh_mobile_ceu_videobuf_setup,
444 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
445 .buf_queue = sh_mobile_ceu_videobuf_queue,
446 .buf_release = sh_mobile_ceu_videobuf_release,
449 static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
451 struct sh_mobile_ceu_dev *pcdev = data;
452 struct videobuf_buffer *vb;
453 unsigned long flags;
455 spin_lock_irqsave(&pcdev->lock, flags);
457 vb = pcdev->active;
458 if (!vb)
459 /* Stale interrupt from a released buffer */
460 goto out;
462 list_del_init(&vb->queue);
464 if (!list_empty(&pcdev->capture))
465 pcdev->active = list_entry(pcdev->capture.next,
466 struct videobuf_buffer, queue);
467 else
468 pcdev->active = NULL;
470 vb->state = (sh_mobile_ceu_capture(pcdev) < 0) ?
471 VIDEOBUF_ERROR : VIDEOBUF_DONE;
472 do_gettimeofday(&vb->ts);
473 vb->field_count++;
474 wake_up(&vb->done);
476 out:
477 spin_unlock_irqrestore(&pcdev->lock, flags);
479 return IRQ_HANDLED;
482 /* Called with .video_lock held */
483 static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
485 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
486 struct sh_mobile_ceu_dev *pcdev = ici->priv;
487 int ret;
489 if (pcdev->icd)
490 return -EBUSY;
492 dev_info(icd->dev.parent,
493 "SuperH Mobile CEU driver attached to camera %d\n",
494 icd->devnum);
496 pm_runtime_get_sync(ici->v4l2_dev.dev);
498 ret = sh_mobile_ceu_soft_reset(pcdev);
499 if (!ret)
500 pcdev->icd = icd;
502 return ret;
505 /* Called with .video_lock held */
506 static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
508 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
509 struct sh_mobile_ceu_dev *pcdev = ici->priv;
510 unsigned long flags;
512 BUG_ON(icd != pcdev->icd);
514 /* disable capture, disable interrupts */
515 ceu_write(pcdev, CEIER, 0);
516 sh_mobile_ceu_soft_reset(pcdev);
518 /* make sure active buffer is canceled */
519 spin_lock_irqsave(&pcdev->lock, flags);
520 if (pcdev->active) {
521 list_del(&pcdev->active->queue);
522 pcdev->active->state = VIDEOBUF_ERROR;
523 wake_up_all(&pcdev->active->done);
524 pcdev->active = NULL;
526 spin_unlock_irqrestore(&pcdev->lock, flags);
528 pm_runtime_put_sync(ici->v4l2_dev.dev);
530 dev_info(icd->dev.parent,
531 "SuperH Mobile CEU driver detached from camera %d\n",
532 icd->devnum);
534 pcdev->icd = NULL;
538 * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
539 * in SH7722 Hardware Manual
541 static unsigned int size_dst(unsigned int src, unsigned int scale)
543 unsigned int mant_pre = scale >> 12;
544 if (!src || !scale)
545 return src;
546 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
547 mant_pre * 4096 / scale + 1;
550 static u16 calc_scale(unsigned int src, unsigned int *dst)
552 u16 scale;
554 if (src == *dst)
555 return 0;
557 scale = (src * 4096 / *dst) & ~7;
559 while (scale > 4096 && size_dst(src, scale) < *dst)
560 scale -= 8;
562 *dst = size_dst(src, scale);
564 return scale;
567 /* rect is guaranteed to not exceed the scaled camera rectangle */
568 static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
569 unsigned int out_width,
570 unsigned int out_height)
572 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
573 struct sh_mobile_ceu_cam *cam = icd->host_priv;
574 struct v4l2_rect *rect = &cam->ceu_rect;
575 struct sh_mobile_ceu_dev *pcdev = ici->priv;
576 unsigned int height, width, cdwdr_width, in_width, in_height;
577 unsigned int left_offset, top_offset;
578 u32 camor;
580 dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
581 rect->width, rect->height, rect->left, rect->top);
583 left_offset = rect->left;
584 top_offset = rect->top;
586 if (pcdev->image_mode) {
587 in_width = rect->width;
588 if (!pcdev->is_16bit) {
589 in_width *= 2;
590 left_offset *= 2;
592 width = out_width;
593 cdwdr_width = out_width;
594 } else {
595 int bytes_per_line = soc_mbus_bytes_per_line(out_width,
596 icd->current_fmt->host_fmt);
597 unsigned int w_factor;
599 width = out_width;
601 switch (icd->current_fmt->host_fmt->packing) {
602 case SOC_MBUS_PACKING_2X8_PADHI:
603 w_factor = 2;
604 break;
605 default:
606 w_factor = 1;
609 in_width = rect->width * w_factor;
610 left_offset = left_offset * w_factor;
612 if (bytes_per_line < 0)
613 cdwdr_width = out_width;
614 else
615 cdwdr_width = bytes_per_line;
618 height = out_height;
619 in_height = rect->height;
620 if (V4L2_FIELD_NONE != pcdev->field) {
621 height /= 2;
622 in_height /= 2;
623 top_offset /= 2;
624 cdwdr_width *= 2;
627 /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
628 camor = left_offset | (top_offset << 16);
630 dev_geo(icd->dev.parent,
631 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
632 (in_height << 16) | in_width, (height << 16) | width,
633 cdwdr_width);
635 ceu_write(pcdev, CAMOR, camor);
636 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
637 ceu_write(pcdev, CFSZR, (height << 16) | width);
638 ceu_write(pcdev, CDWDR, cdwdr_width);
641 static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
643 u32 capsr = ceu_read(pcdev, CAPSR);
644 ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
645 return capsr;
648 static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
650 unsigned long timeout = jiffies + 10 * HZ;
653 * Wait until the end of the current frame. It can take a long time,
654 * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
656 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
657 msleep(1);
659 if (time_after(jiffies, timeout)) {
660 dev_err(pcdev->ici.v4l2_dev.dev,
661 "Timeout waiting for frame end! Interface problem?\n");
662 return;
665 /* Wait until reset clears, this shall not hang... */
666 while (ceu_read(pcdev, CAPSR) & (1 << 16))
667 udelay(10);
669 /* Anything to restore? */
670 if (capsr & ~(1 << 16))
671 ceu_write(pcdev, CAPSR, capsr);
674 static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
675 __u32 pixfmt)
677 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
678 struct sh_mobile_ceu_dev *pcdev = ici->priv;
679 int ret;
680 unsigned long camera_flags, common_flags, value;
681 int yuv_lineskip;
682 struct sh_mobile_ceu_cam *cam = icd->host_priv;
683 u32 capsr = capture_save_reset(pcdev);
685 camera_flags = icd->ops->query_bus_param(icd);
686 common_flags = soc_camera_bus_param_compatible(camera_flags,
687 make_bus_param(pcdev));
688 if (!common_flags)
689 return -EINVAL;
691 /* Make choises, based on platform preferences */
692 if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
693 (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
694 if (pcdev->pdata->flags & SH_CEU_FLAG_HSYNC_LOW)
695 common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
696 else
697 common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
700 if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
701 (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
702 if (pcdev->pdata->flags & SH_CEU_FLAG_VSYNC_LOW)
703 common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
704 else
705 common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
708 ret = icd->ops->set_bus_param(icd, common_flags);
709 if (ret < 0)
710 return ret;
712 switch (common_flags & SOCAM_DATAWIDTH_MASK) {
713 case SOCAM_DATAWIDTH_8:
714 pcdev->is_16bit = 0;
715 break;
716 case SOCAM_DATAWIDTH_16:
717 pcdev->is_16bit = 1;
718 break;
719 default:
720 return -EINVAL;
723 ceu_write(pcdev, CRCNTR, 0);
724 ceu_write(pcdev, CRCMPR, 0);
726 value = 0x00000010; /* data fetch by default */
727 yuv_lineskip = 0;
729 switch (icd->current_fmt->host_fmt->fourcc) {
730 case V4L2_PIX_FMT_NV12:
731 case V4L2_PIX_FMT_NV21:
732 yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
733 /* fall-through */
734 case V4L2_PIX_FMT_NV16:
735 case V4L2_PIX_FMT_NV61:
736 switch (cam->code) {
737 case V4L2_MBUS_FMT_YUYV8_2X8_BE:
738 value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
739 break;
740 case V4L2_MBUS_FMT_YVYU8_2X8_BE:
741 value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
742 break;
743 case V4L2_MBUS_FMT_YUYV8_2X8_LE:
744 value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
745 break;
746 case V4L2_MBUS_FMT_YVYU8_2X8_LE:
747 value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
748 break;
749 default:
750 BUG();
754 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
755 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
756 value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
758 value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
759 value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
760 value |= pcdev->is_16bit ? 1 << 12 : 0;
761 ceu_write(pcdev, CAMCR, value);
763 ceu_write(pcdev, CAPCR, 0x00300000);
765 switch (pcdev->field) {
766 case V4L2_FIELD_INTERLACED_TB:
767 value = 0x101;
768 break;
769 case V4L2_FIELD_INTERLACED_BT:
770 value = 0x102;
771 break;
772 default:
773 value = 0;
774 break;
776 ceu_write(pcdev, CAIFR, value);
778 sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
779 mdelay(1);
781 ceu_write(pcdev, CFLCR, pcdev->cflcr);
784 * A few words about byte order (observed in Big Endian mode)
786 * In data fetch mode bytes are received in chunks of 8 bytes.
787 * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
789 * The data is however by default written to memory in reverse order:
790 * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
792 * The lowest three bits of CDOCR allows us to do swapping,
793 * using 7 we swap the data bytes to match the incoming order:
794 * D0, D1, D2, D3, D4, D5, D6, D7
796 value = 0x00000017;
797 if (yuv_lineskip)
798 value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
800 ceu_write(pcdev, CDOCR, value);
801 ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
803 dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
804 pixfmt & 0xff, (pixfmt >> 8) & 0xff,
805 (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
806 icd->user_width, icd->user_height);
808 capture_restore(pcdev, capsr);
810 /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
811 return 0;
814 static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
815 unsigned char buswidth)
817 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
818 struct sh_mobile_ceu_dev *pcdev = ici->priv;
819 unsigned long camera_flags, common_flags;
821 camera_flags = icd->ops->query_bus_param(icd);
822 common_flags = soc_camera_bus_param_compatible(camera_flags,
823 make_bus_param(pcdev));
824 if (!common_flags || buswidth > 16 ||
825 (buswidth > 8 && !(common_flags & SOCAM_DATAWIDTH_16)))
826 return -EINVAL;
828 return 0;
831 static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
833 .fourcc = V4L2_PIX_FMT_NV12,
834 .name = "NV12",
835 .bits_per_sample = 12,
836 .packing = SOC_MBUS_PACKING_NONE,
837 .order = SOC_MBUS_ORDER_LE,
838 }, {
839 .fourcc = V4L2_PIX_FMT_NV21,
840 .name = "NV21",
841 .bits_per_sample = 12,
842 .packing = SOC_MBUS_PACKING_NONE,
843 .order = SOC_MBUS_ORDER_LE,
844 }, {
845 .fourcc = V4L2_PIX_FMT_NV16,
846 .name = "NV16",
847 .bits_per_sample = 16,
848 .packing = SOC_MBUS_PACKING_NONE,
849 .order = SOC_MBUS_ORDER_LE,
850 }, {
851 .fourcc = V4L2_PIX_FMT_NV61,
852 .name = "NV61",
853 .bits_per_sample = 16,
854 .packing = SOC_MBUS_PACKING_NONE,
855 .order = SOC_MBUS_ORDER_LE,
859 /* This will be corrected as we get more formats */
860 static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
862 return fmt->packing == SOC_MBUS_PACKING_NONE ||
863 (fmt->bits_per_sample == 8 &&
864 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
865 (fmt->bits_per_sample > 8 &&
866 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
869 static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
870 struct soc_camera_format_xlate *xlate)
872 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
873 struct device *dev = icd->dev.parent;
874 int ret, k, n;
875 int formats = 0;
876 struct sh_mobile_ceu_cam *cam;
877 enum v4l2_mbus_pixelcode code;
878 const struct soc_mbus_pixelfmt *fmt;
880 ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
881 if (ret < 0)
882 /* No more formats */
883 return 0;
885 fmt = soc_mbus_get_fmtdesc(code);
886 if (!fmt) {
887 dev_err(icd->dev.parent,
888 "Invalid format code #%d: %d\n", idx, code);
889 return -EINVAL;
892 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
893 if (ret < 0)
894 return 0;
896 if (!icd->host_priv) {
897 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
898 if (!cam)
899 return -ENOMEM;
901 icd->host_priv = cam;
902 } else {
903 cam = icd->host_priv;
906 /* Beginning of a pass */
907 if (!idx)
908 cam->extra_fmt = NULL;
910 switch (code) {
911 case V4L2_MBUS_FMT_YUYV8_2X8_BE:
912 case V4L2_MBUS_FMT_YVYU8_2X8_BE:
913 case V4L2_MBUS_FMT_YUYV8_2X8_LE:
914 case V4L2_MBUS_FMT_YVYU8_2X8_LE:
915 if (cam->extra_fmt)
916 break;
919 * Our case is simple so far: for any of the above four camera
920 * formats we add all our four synthesized NV* formats, so,
921 * just marking the device with a single flag suffices. If
922 * the format generation rules are more complex, you would have
923 * to actually hang your already added / counted formats onto
924 * the host_priv pointer and check whether the format you're
925 * going to add now is already there.
927 cam->extra_fmt = sh_mobile_ceu_formats;
929 n = ARRAY_SIZE(sh_mobile_ceu_formats);
930 formats += n;
931 for (k = 0; xlate && k < n; k++) {
932 xlate->host_fmt = &sh_mobile_ceu_formats[k];
933 xlate->code = code;
934 xlate++;
935 dev_dbg(dev, "Providing format %s using code %d\n",
936 sh_mobile_ceu_formats[k].name, code);
938 break;
939 default:
940 if (!sh_mobile_ceu_packing_supported(fmt))
941 return 0;
944 /* Generic pass-through */
945 formats++;
946 if (xlate) {
947 xlate->host_fmt = fmt;
948 xlate->code = code;
949 xlate++;
950 dev_dbg(dev, "Providing format %s in pass-through mode\n",
951 xlate->host_fmt->name);
954 return formats;
957 static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
959 kfree(icd->host_priv);
960 icd->host_priv = NULL;
963 /* Check if any dimension of r1 is smaller than respective one of r2 */
964 static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
966 return r1->width < r2->width || r1->height < r2->height;
969 /* Check if r1 fails to cover r2 */
970 static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
972 return r1->left > r2->left || r1->top > r2->top ||
973 r1->left + r1->width < r2->left + r2->width ||
974 r1->top + r1->height < r2->top + r2->height;
977 static unsigned int scale_down(unsigned int size, unsigned int scale)
979 return (size * 4096 + scale / 2) / scale;
982 static unsigned int scale_up(unsigned int size, unsigned int scale)
984 return (size * scale + 2048) / 4096;
987 static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
989 return (input * 4096 + output / 2) / output;
992 static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
994 struct v4l2_crop crop;
995 struct v4l2_cropcap cap;
996 int ret;
998 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1000 ret = v4l2_subdev_call(sd, video, g_crop, &crop);
1001 if (!ret) {
1002 *rect = crop.c;
1003 return ret;
1006 /* Camera driver doesn't support .g_crop(), assume default rectangle */
1007 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1009 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1010 if (ret < 0)
1011 return ret;
1013 *rect = cap.defrect;
1015 return ret;
1019 * The common for both scaling and cropping iterative approach is:
1020 * 1. try if the client can produce exactly what requested by the user
1021 * 2. if (1) failed, try to double the client image until we get one big enough
1022 * 3. if (2) failed, try to request the maximum image
1024 static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
1025 struct v4l2_crop *cam_crop)
1027 struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
1028 struct device *dev = sd->v4l2_dev->dev;
1029 struct v4l2_cropcap cap;
1030 int ret;
1031 unsigned int width, height;
1033 v4l2_subdev_call(sd, video, s_crop, crop);
1034 ret = client_g_rect(sd, cam_rect);
1035 if (ret < 0)
1036 return ret;
1039 * Now cam_crop contains the current camera input rectangle, and it must
1040 * be within camera cropcap bounds
1042 if (!memcmp(rect, cam_rect, sizeof(*rect))) {
1043 /* Even if camera S_CROP failed, but camera rectangle matches */
1044 dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n",
1045 rect->width, rect->height, rect->left, rect->top);
1046 return 0;
1049 /* Try to fix cropping, that camera hasn't managed to set */
1050 dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
1051 cam_rect->width, cam_rect->height,
1052 cam_rect->left, cam_rect->top,
1053 rect->width, rect->height, rect->left, rect->top);
1055 /* We need sensor maximum rectangle */
1056 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1057 if (ret < 0)
1058 return ret;
1060 soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
1061 cap.bounds.width);
1062 soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
1063 cap.bounds.height);
1066 * Popular special case - some cameras can only handle fixed sizes like
1067 * QVGA, VGA,... Take care to avoid infinite loop.
1069 width = max(cam_rect->width, 2);
1070 height = max(cam_rect->height, 2);
1072 while (!ret && (is_smaller(cam_rect, rect) ||
1073 is_inside(cam_rect, rect)) &&
1074 (cap.bounds.width > width || cap.bounds.height > height)) {
1076 width *= 2;
1077 height *= 2;
1079 cam_rect->width = width;
1080 cam_rect->height = height;
1083 * We do not know what capabilities the camera has to set up
1084 * left and top borders. We could try to be smarter in iterating
1085 * them, e.g., if camera current left is to the right of the
1086 * target left, set it to the middle point between the current
1087 * left and minimum left. But that would add too much
1088 * complexity: we would have to iterate each border separately.
1090 if (cam_rect->left > rect->left)
1091 cam_rect->left = cap.bounds.left;
1093 if (cam_rect->left + cam_rect->width < rect->left + rect->width)
1094 cam_rect->width = rect->left + rect->width -
1095 cam_rect->left;
1097 if (cam_rect->top > rect->top)
1098 cam_rect->top = cap.bounds.top;
1100 if (cam_rect->top + cam_rect->height < rect->top + rect->height)
1101 cam_rect->height = rect->top + rect->height -
1102 cam_rect->top;
1104 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1105 ret = client_g_rect(sd, cam_rect);
1106 dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret,
1107 cam_rect->width, cam_rect->height,
1108 cam_rect->left, cam_rect->top);
1111 /* S_CROP must not modify the rectangle */
1112 if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
1114 * The camera failed to configure a suitable cropping,
1115 * we cannot use the current rectangle, set to max
1117 *cam_rect = cap.bounds;
1118 v4l2_subdev_call(sd, video, s_crop, cam_crop);
1119 ret = client_g_rect(sd, cam_rect);
1120 dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret,
1121 cam_rect->width, cam_rect->height,
1122 cam_rect->left, cam_rect->top);
1125 return ret;
1128 static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
1129 unsigned int *scale_h, unsigned int *scale_v)
1131 struct v4l2_mbus_framefmt mf;
1132 int ret;
1134 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1135 if (ret < 0)
1136 return ret;
1138 *scale_h = calc_generic_scale(rect->width, mf.width);
1139 *scale_v = calc_generic_scale(rect->height, mf.height);
1141 return 0;
1144 static int get_camera_subwin(struct soc_camera_device *icd,
1145 struct v4l2_rect *cam_subrect,
1146 unsigned int cam_hscale, unsigned int cam_vscale)
1148 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1149 struct v4l2_rect *ceu_rect = &cam->ceu_rect;
1151 if (!ceu_rect->width) {
1152 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1153 struct device *dev = icd->dev.parent;
1154 struct v4l2_mbus_framefmt mf;
1155 int ret;
1156 /* First time */
1158 ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
1159 if (ret < 0)
1160 return ret;
1162 dev_geo(dev, "camera fmt %ux%u\n", mf.width, mf.height);
1164 if (mf.width > 2560) {
1165 ceu_rect->width = 2560;
1166 ceu_rect->left = (mf.width - 2560) / 2;
1167 } else {
1168 ceu_rect->width = mf.width;
1169 ceu_rect->left = 0;
1172 if (mf.height > 1920) {
1173 ceu_rect->height = 1920;
1174 ceu_rect->top = (mf.height - 1920) / 2;
1175 } else {
1176 ceu_rect->height = mf.height;
1177 ceu_rect->top = 0;
1180 dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
1181 ceu_rect->width, ceu_rect->height,
1182 ceu_rect->left, ceu_rect->top);
1185 cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
1186 cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
1187 cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
1188 cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
1190 return 0;
1193 static int client_s_fmt(struct soc_camera_device *icd,
1194 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
1196 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1197 struct device *dev = icd->dev.parent;
1198 unsigned int width = mf->width, height = mf->height, tmp_w, tmp_h;
1199 unsigned int max_width, max_height;
1200 struct v4l2_cropcap cap;
1201 int ret;
1203 cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1205 ret = v4l2_subdev_call(sd, video, cropcap, &cap);
1206 if (ret < 0)
1207 return ret;
1209 max_width = min(cap.bounds.width, 2560);
1210 max_height = min(cap.bounds.height, 1920);
1212 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf);
1213 if (ret < 0)
1214 return ret;
1216 dev_geo(dev, "camera scaled to %ux%u\n", mf->width, mf->height);
1218 if ((width == mf->width && height == mf->height) || !ceu_can_scale)
1219 return 0;
1221 /* Camera set a format, but geometry is not precise, try to improve */
1222 tmp_w = mf->width;
1223 tmp_h = mf->height;
1225 /* width <= max_width && height <= max_height - guaranteed by try_fmt */
1226 while ((width > tmp_w || height > tmp_h) &&
1227 tmp_w < max_width && tmp_h < max_height) {
1228 tmp_w = min(2 * tmp_w, max_width);
1229 tmp_h = min(2 * tmp_h, max_height);
1230 mf->width = tmp_w;
1231 mf->height = tmp_h;
1232 ret = v4l2_subdev_call(sd, video, s_mbus_fmt, mf);
1233 dev_geo(dev, "Camera scaled to %ux%u\n",
1234 mf->width, mf->height);
1235 if (ret < 0) {
1236 /* This shouldn't happen */
1237 dev_err(dev, "Client failed to set format: %d\n", ret);
1238 return ret;
1242 return 0;
1246 * @rect - camera cropped rectangle
1247 * @sub_rect - CEU cropped rectangle, mapped back to camera input area
1248 * @ceu_rect - on output calculated CEU crop rectangle
1250 static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
1251 struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect,
1252 struct v4l2_mbus_framefmt *mf, bool ceu_can_scale)
1254 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1255 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1256 struct device *dev = icd->dev.parent;
1257 struct v4l2_mbus_framefmt mf_tmp = *mf;
1258 unsigned int scale_h, scale_v;
1259 int ret;
1261 /* 5. Apply iterative camera S_FMT for camera user window. */
1262 ret = client_s_fmt(icd, &mf_tmp, ceu_can_scale);
1263 if (ret < 0)
1264 return ret;
1266 dev_geo(dev, "5: camera scaled to %ux%u\n",
1267 mf_tmp.width, mf_tmp.height);
1269 /* 6. Retrieve camera output window (g_fmt) */
1271 /* unneeded - it is already in "mf_tmp" */
1273 /* 7. Calculate new camera scales. */
1274 ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
1275 if (ret < 0)
1276 return ret;
1278 dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
1280 cam->cam_width = mf_tmp.width;
1281 cam->cam_height = mf_tmp.height;
1282 mf->width = mf_tmp.width;
1283 mf->height = mf_tmp.height;
1284 mf->colorspace = mf_tmp.colorspace;
1287 * 8. Calculate new CEU crop - apply camera scales to previously
1288 * calculated "effective" crop.
1290 ceu_rect->left = scale_down(sub_rect->left, scale_h);
1291 ceu_rect->width = scale_down(sub_rect->width, scale_h);
1292 ceu_rect->top = scale_down(sub_rect->top, scale_v);
1293 ceu_rect->height = scale_down(sub_rect->height, scale_v);
1295 dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
1296 ceu_rect->width, ceu_rect->height,
1297 ceu_rect->left, ceu_rect->top);
1299 return 0;
1302 /* Get combined scales */
1303 static int get_scales(struct soc_camera_device *icd,
1304 unsigned int *scale_h, unsigned int *scale_v)
1306 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1307 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1308 struct v4l2_crop cam_crop;
1309 unsigned int width_in, height_in;
1310 int ret;
1312 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1314 ret = client_g_rect(sd, &cam_crop.c);
1315 if (ret < 0)
1316 return ret;
1318 ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
1319 if (ret < 0)
1320 return ret;
1322 width_in = scale_up(cam->ceu_rect.width, *scale_h);
1323 height_in = scale_up(cam->ceu_rect.height, *scale_v);
1325 *scale_h = calc_generic_scale(width_in, icd->user_width);
1326 *scale_v = calc_generic_scale(height_in, icd->user_height);
1328 return 0;
1332 * CEU can scale and crop, but we don't want to waste bandwidth and kill the
1333 * framerate by always requesting the maximum image from the client. See
1334 * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
1335 * scaling and cropping algorithms and for the meaning of referenced here steps.
1337 static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
1338 struct v4l2_crop *a)
1340 struct v4l2_rect *rect = &a->c;
1341 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1342 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1343 struct v4l2_crop cam_crop;
1344 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1345 struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
1346 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1347 struct device *dev = icd->dev.parent;
1348 struct v4l2_mbus_framefmt mf;
1349 unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v,
1350 out_width, out_height;
1351 u32 capsr, cflcr;
1352 int ret;
1354 /* 1. Calculate current combined scales. */
1355 ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
1356 if (ret < 0)
1357 return ret;
1359 dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
1361 /* 2. Apply iterative camera S_CROP for new input window. */
1362 ret = client_s_crop(sd, a, &cam_crop);
1363 if (ret < 0)
1364 return ret;
1366 dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n",
1367 cam_rect->width, cam_rect->height,
1368 cam_rect->left, cam_rect->top);
1370 /* On success cam_crop contains current camera crop */
1373 * 3. If old combined scales applied to new crop produce an impossible
1374 * user window, adjust scales to produce nearest possible window.
1376 out_width = scale_down(rect->width, scale_comb_h);
1377 out_height = scale_down(rect->height, scale_comb_v);
1379 if (out_width > 2560)
1380 out_width = 2560;
1381 else if (out_width < 2)
1382 out_width = 2;
1384 if (out_height > 1920)
1385 out_height = 1920;
1386 else if (out_height < 4)
1387 out_height = 4;
1389 dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
1391 /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
1394 * 5. Using actual input window and calculated combined scales calculate
1395 * camera target output window.
1397 mf.width = scale_down(cam_rect->width, scale_comb_h);
1398 mf.height = scale_down(cam_rect->height, scale_comb_v);
1400 dev_geo(dev, "5: camera target %ux%u\n", mf.width, mf.height);
1402 /* 6. - 9. */
1403 mf.code = cam->code;
1404 mf.field = pcdev->field;
1406 capsr = capture_save_reset(pcdev);
1407 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1409 /* Make relative to camera rectangle */
1410 rect->left -= cam_rect->left;
1411 rect->top -= cam_rect->top;
1413 ret = client_scale(icd, cam_rect, rect, ceu_rect, &mf,
1414 pcdev->image_mode &&
1415 V4L2_FIELD_NONE == pcdev->field);
1417 dev_geo(dev, "6-9: %d\n", ret);
1419 /* 10. Use CEU cropping to crop to the new window. */
1420 sh_mobile_ceu_set_rect(icd, out_width, out_height);
1422 dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n",
1423 ceu_rect->width, ceu_rect->height,
1424 ceu_rect->left, ceu_rect->top);
1427 * 11. Calculate CEU scales from camera scales from results of (10) and
1428 * user window from (3)
1430 scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
1431 scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
1433 dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
1435 /* 12. Apply CEU scales. */
1436 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1437 if (cflcr != pcdev->cflcr) {
1438 pcdev->cflcr = cflcr;
1439 ceu_write(pcdev, CFLCR, cflcr);
1442 /* Restore capture */
1443 if (pcdev->active)
1444 capsr |= 1;
1445 capture_restore(pcdev, capsr);
1447 icd->user_width = out_width;
1448 icd->user_height = out_height;
1450 /* Even if only camera cropping succeeded */
1451 return ret;
1454 /* Similar to set_crop multistage iterative algorithm */
1455 static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
1456 struct v4l2_format *f)
1458 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1459 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1460 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1461 struct v4l2_pix_format *pix = &f->fmt.pix;
1462 struct v4l2_mbus_framefmt mf;
1463 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1464 struct device *dev = icd->dev.parent;
1465 __u32 pixfmt = pix->pixelformat;
1466 const struct soc_camera_format_xlate *xlate;
1467 struct v4l2_crop cam_crop;
1468 struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
1469 unsigned int scale_cam_h, scale_cam_v;
1470 u16 scale_v, scale_h;
1471 int ret;
1472 bool image_mode;
1473 enum v4l2_field field;
1475 switch (pix->field) {
1476 default:
1477 pix->field = V4L2_FIELD_NONE;
1478 /* fall-through */
1479 case V4L2_FIELD_INTERLACED_TB:
1480 case V4L2_FIELD_INTERLACED_BT:
1481 case V4L2_FIELD_NONE:
1482 field = pix->field;
1483 break;
1484 case V4L2_FIELD_INTERLACED:
1485 field = V4L2_FIELD_INTERLACED_TB;
1486 break;
1489 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1490 if (!xlate) {
1491 dev_warn(dev, "Format %x not found\n", pixfmt);
1492 return -EINVAL;
1495 /* 1. Calculate current camera scales. */
1496 cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1498 ret = client_g_rect(sd, cam_rect);
1499 if (ret < 0)
1500 return ret;
1502 ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
1503 if (ret < 0)
1504 return ret;
1506 dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
1509 * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
1510 * scaled back at current camera scales onto input window.
1512 ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
1513 if (ret < 0)
1514 return ret;
1516 dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
1517 cam_subrect.width, cam_subrect.height,
1518 cam_subrect.left, cam_subrect.top);
1521 * 3. Calculate new combined scales from "effective" input window to
1522 * requested user window.
1524 scale_h = calc_generic_scale(cam_subrect.width, pix->width);
1525 scale_v = calc_generic_scale(cam_subrect.height, pix->height);
1527 dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
1530 * 4. Calculate camera output window by applying combined scales to real
1531 * input window.
1533 mf.width = scale_down(cam_rect->width, scale_h);
1534 mf.height = scale_down(cam_rect->height, scale_v);
1535 mf.field = pix->field;
1536 mf.colorspace = pix->colorspace;
1537 mf.code = xlate->code;
1539 switch (pixfmt) {
1540 case V4L2_PIX_FMT_NV12:
1541 case V4L2_PIX_FMT_NV21:
1542 case V4L2_PIX_FMT_NV16:
1543 case V4L2_PIX_FMT_NV61:
1544 image_mode = true;
1545 break;
1546 default:
1547 image_mode = false;
1550 dev_geo(dev, "4: camera output %ux%u\n", mf.width, mf.height);
1552 /* 5. - 9. */
1553 ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &mf,
1554 image_mode && V4L2_FIELD_NONE == field);
1556 dev_geo(dev, "5-9: client scale %d\n", ret);
1558 /* Done with the camera. Now see if we can improve the result */
1560 dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
1561 ret, mf.width, mf.height, pix->width, pix->height);
1562 if (ret < 0)
1563 return ret;
1565 if (mf.code != xlate->code)
1566 return -EINVAL;
1568 /* 10. Use CEU scaling to scale to the requested user window. */
1570 /* We cannot scale up */
1571 if (pix->width > mf.width)
1572 pix->width = mf.width;
1573 if (pix->width > ceu_rect.width)
1574 pix->width = ceu_rect.width;
1576 if (pix->height > mf.height)
1577 pix->height = mf.height;
1578 if (pix->height > ceu_rect.height)
1579 pix->height = ceu_rect.height;
1581 pix->colorspace = mf.colorspace;
1583 if (image_mode) {
1584 /* Scale pix->{width x height} down to width x height */
1585 scale_h = calc_scale(ceu_rect.width, &pix->width);
1586 scale_v = calc_scale(ceu_rect.height, &pix->height);
1588 pcdev->cflcr = scale_h | (scale_v << 16);
1589 } else {
1590 pix->width = ceu_rect.width;
1591 pix->height = ceu_rect.height;
1592 scale_h = scale_v = 0;
1593 pcdev->cflcr = 0;
1596 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1597 ceu_rect.width, scale_h, pix->width,
1598 ceu_rect.height, scale_v, pix->height);
1600 cam->code = xlate->code;
1601 cam->ceu_rect = ceu_rect;
1602 icd->current_fmt = xlate;
1604 pcdev->field = field;
1605 pcdev->image_mode = image_mode;
1607 return 0;
1610 static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1611 struct v4l2_format *f)
1613 const struct soc_camera_format_xlate *xlate;
1614 struct v4l2_pix_format *pix = &f->fmt.pix;
1615 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1616 struct v4l2_mbus_framefmt mf;
1617 __u32 pixfmt = pix->pixelformat;
1618 int width, height;
1619 int ret;
1621 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1622 if (!xlate) {
1623 dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
1624 return -EINVAL;
1627 /* FIXME: calculate using depth and bus width */
1629 v4l_bound_align_image(&pix->width, 2, 2560, 1,
1630 &pix->height, 4, 1920, 2, 0);
1632 width = pix->width;
1633 height = pix->height;
1635 pix->bytesperline = soc_mbus_bytes_per_line(width, xlate->host_fmt);
1636 if (pix->bytesperline < 0)
1637 return pix->bytesperline;
1638 pix->sizeimage = height * pix->bytesperline;
1640 /* limit to sensor capabilities */
1641 mf.width = pix->width;
1642 mf.height = pix->height;
1643 mf.field = pix->field;
1644 mf.code = xlate->code;
1645 mf.colorspace = pix->colorspace;
1647 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
1648 if (ret < 0)
1649 return ret;
1651 pix->width = mf.width;
1652 pix->height = mf.height;
1653 pix->field = mf.field;
1654 pix->colorspace = mf.colorspace;
1656 switch (pixfmt) {
1657 case V4L2_PIX_FMT_NV12:
1658 case V4L2_PIX_FMT_NV21:
1659 case V4L2_PIX_FMT_NV16:
1660 case V4L2_PIX_FMT_NV61:
1661 /* FIXME: check against rect_max after converting soc-camera */
1662 /* We can scale precisely, need a bigger image from camera */
1663 if (pix->width < width || pix->height < height) {
1665 * We presume, the sensor behaves sanely, i.e., if
1666 * requested a bigger rectangle, it will not return a
1667 * smaller one.
1669 mf.width = 2560;
1670 mf.height = 1920;
1671 ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
1672 if (ret < 0) {
1673 /* Shouldn't actually happen... */
1674 dev_err(icd->dev.parent,
1675 "FIXME: client try_fmt() = %d\n", ret);
1676 return ret;
1679 /* We will scale exactly */
1680 if (mf.width > width)
1681 pix->width = width;
1682 if (mf.height > height)
1683 pix->height = height;
1686 return ret;
1689 static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
1690 struct v4l2_requestbuffers *p)
1692 int i;
1695 * This is for locking debugging only. I removed spinlocks and now I
1696 * check whether .prepare is ever called on a linked buffer, or whether
1697 * a dma IRQ can occur for an in-work or unlinked buffer. Until now
1698 * it hadn't triggered
1700 for (i = 0; i < p->count; i++) {
1701 struct sh_mobile_ceu_buffer *buf;
1703 buf = container_of(icf->vb_vidq.bufs[i],
1704 struct sh_mobile_ceu_buffer, vb);
1705 INIT_LIST_HEAD(&buf->vb.queue);
1708 return 0;
1711 static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1713 struct soc_camera_file *icf = file->private_data;
1714 struct sh_mobile_ceu_buffer *buf;
1716 buf = list_entry(icf->vb_vidq.stream.next,
1717 struct sh_mobile_ceu_buffer, vb.stream);
1719 poll_wait(file, &buf->vb.done, pt);
1721 if (buf->vb.state == VIDEOBUF_DONE ||
1722 buf->vb.state == VIDEOBUF_ERROR)
1723 return POLLIN|POLLRDNORM;
1725 return 0;
1728 static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1729 struct v4l2_capability *cap)
1731 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1732 cap->version = KERNEL_VERSION(0, 0, 5);
1733 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1734 return 0;
1737 static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
1738 struct soc_camera_device *icd)
1740 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1741 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1743 videobuf_queue_dma_contig_init(q,
1744 &sh_mobile_ceu_videobuf_ops,
1745 icd->dev.parent, &pcdev->lock,
1746 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1747 pcdev->field,
1748 sizeof(struct sh_mobile_ceu_buffer),
1749 icd);
1752 static int sh_mobile_ceu_get_parm(struct soc_camera_device *icd,
1753 struct v4l2_streamparm *parm)
1755 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1757 return v4l2_subdev_call(sd, video, g_parm, parm);
1760 static int sh_mobile_ceu_set_parm(struct soc_camera_device *icd,
1761 struct v4l2_streamparm *parm)
1763 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1765 return v4l2_subdev_call(sd, video, s_parm, parm);
1768 static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
1769 struct v4l2_control *ctrl)
1771 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1772 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1773 u32 val;
1775 switch (ctrl->id) {
1776 case V4L2_CID_SHARPNESS:
1777 val = ceu_read(pcdev, CLFCR);
1778 ctrl->value = val ^ 1;
1779 return 0;
1781 return -ENOIOCTLCMD;
1784 static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
1785 struct v4l2_control *ctrl)
1787 struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
1788 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1790 switch (ctrl->id) {
1791 case V4L2_CID_SHARPNESS:
1792 switch (icd->current_fmt->host_fmt->fourcc) {
1793 case V4L2_PIX_FMT_NV12:
1794 case V4L2_PIX_FMT_NV21:
1795 case V4L2_PIX_FMT_NV16:
1796 case V4L2_PIX_FMT_NV61:
1797 ceu_write(pcdev, CLFCR, !ctrl->value);
1798 return 0;
1800 return -EINVAL;
1802 return -ENOIOCTLCMD;
1805 static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
1807 .id = V4L2_CID_SHARPNESS,
1808 .type = V4L2_CTRL_TYPE_BOOLEAN,
1809 .name = "Low-pass filter",
1810 .minimum = 0,
1811 .maximum = 1,
1812 .step = 1,
1813 .default_value = 0,
1817 static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1818 .owner = THIS_MODULE,
1819 .add = sh_mobile_ceu_add_device,
1820 .remove = sh_mobile_ceu_remove_device,
1821 .get_formats = sh_mobile_ceu_get_formats,
1822 .put_formats = sh_mobile_ceu_put_formats,
1823 .set_crop = sh_mobile_ceu_set_crop,
1824 .set_fmt = sh_mobile_ceu_set_fmt,
1825 .try_fmt = sh_mobile_ceu_try_fmt,
1826 .set_ctrl = sh_mobile_ceu_set_ctrl,
1827 .get_ctrl = sh_mobile_ceu_get_ctrl,
1828 .set_parm = sh_mobile_ceu_set_parm,
1829 .get_parm = sh_mobile_ceu_get_parm,
1830 .reqbufs = sh_mobile_ceu_reqbufs,
1831 .poll = sh_mobile_ceu_poll,
1832 .querycap = sh_mobile_ceu_querycap,
1833 .set_bus_param = sh_mobile_ceu_set_bus_param,
1834 .init_videobuf = sh_mobile_ceu_init_videobuf,
1835 .controls = sh_mobile_ceu_controls,
1836 .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
1839 static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
1841 struct sh_mobile_ceu_dev *pcdev;
1842 struct resource *res;
1843 void __iomem *base;
1844 unsigned int irq;
1845 int err = 0;
1847 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1848 irq = platform_get_irq(pdev, 0);
1849 if (!res || (int)irq <= 0) {
1850 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1851 err = -ENODEV;
1852 goto exit;
1855 pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
1856 if (!pcdev) {
1857 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1858 err = -ENOMEM;
1859 goto exit;
1862 INIT_LIST_HEAD(&pcdev->capture);
1863 spin_lock_init(&pcdev->lock);
1865 pcdev->pdata = pdev->dev.platform_data;
1866 if (!pcdev->pdata) {
1867 err = -EINVAL;
1868 dev_err(&pdev->dev, "CEU platform data not set.\n");
1869 goto exit_kfree;
1872 base = ioremap_nocache(res->start, resource_size(res));
1873 if (!base) {
1874 err = -ENXIO;
1875 dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
1876 goto exit_kfree;
1879 pcdev->irq = irq;
1880 pcdev->base = base;
1881 pcdev->video_limit = 0; /* only enabled if second resource exists */
1883 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1884 if (res) {
1885 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1886 res->start,
1887 resource_size(res),
1888 DMA_MEMORY_MAP |
1889 DMA_MEMORY_EXCLUSIVE);
1890 if (!err) {
1891 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1892 err = -ENXIO;
1893 goto exit_iounmap;
1896 pcdev->video_limit = resource_size(res);
1899 /* request irq */
1900 err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
1901 dev_name(&pdev->dev), pcdev);
1902 if (err) {
1903 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1904 goto exit_release_mem;
1907 pm_suspend_ignore_children(&pdev->dev, true);
1908 pm_runtime_enable(&pdev->dev);
1909 pm_runtime_resume(&pdev->dev);
1911 pcdev->ici.priv = pcdev;
1912 pcdev->ici.v4l2_dev.dev = &pdev->dev;
1913 pcdev->ici.nr = pdev->id;
1914 pcdev->ici.drv_name = dev_name(&pdev->dev);
1915 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
1917 err = soc_camera_host_register(&pcdev->ici);
1918 if (err)
1919 goto exit_free_clk;
1921 return 0;
1923 exit_free_clk:
1924 pm_runtime_disable(&pdev->dev);
1925 free_irq(pcdev->irq, pcdev);
1926 exit_release_mem:
1927 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1928 dma_release_declared_memory(&pdev->dev);
1929 exit_iounmap:
1930 iounmap(base);
1931 exit_kfree:
1932 kfree(pcdev);
1933 exit:
1934 return err;
1937 static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
1939 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1940 struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
1941 struct sh_mobile_ceu_dev, ici);
1943 soc_camera_host_unregister(soc_host);
1944 pm_runtime_disable(&pdev->dev);
1945 free_irq(pcdev->irq, pcdev);
1946 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1947 dma_release_declared_memory(&pdev->dev);
1948 iounmap(pcdev->base);
1949 kfree(pcdev);
1950 return 0;
1953 static int sh_mobile_ceu_runtime_nop(struct device *dev)
1955 /* Runtime PM callback shared between ->runtime_suspend()
1956 * and ->runtime_resume(). Simply returns success.
1958 * This driver re-initializes all registers after
1959 * pm_runtime_get_sync() anyway so there is no need
1960 * to save and restore registers here.
1962 return 0;
1965 static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
1966 .runtime_suspend = sh_mobile_ceu_runtime_nop,
1967 .runtime_resume = sh_mobile_ceu_runtime_nop,
1970 static struct platform_driver sh_mobile_ceu_driver = {
1971 .driver = {
1972 .name = "sh_mobile_ceu",
1973 .pm = &sh_mobile_ceu_dev_pm_ops,
1975 .probe = sh_mobile_ceu_probe,
1976 .remove = __devexit_p(sh_mobile_ceu_remove),
1979 static int __init sh_mobile_ceu_init(void)
1981 return platform_driver_register(&sh_mobile_ceu_driver);
1984 static void __exit sh_mobile_ceu_exit(void)
1986 platform_driver_unregister(&sh_mobile_ceu_driver);
1989 module_init(sh_mobile_ceu_init);
1990 module_exit(sh_mobile_ceu_exit);
1992 MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1993 MODULE_AUTHOR("Magnus Damm");
1994 MODULE_LICENSE("GPL");
1995 MODULE_ALIAS("platform:sh_mobile_ceu");