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