1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/init.h>
18#include <linux/module.h>
19#include <linux/io.h>
20#include <linux/completion.h>
21#include <linux/delay.h>
22#include <linux/dma-mapping.h>
23#include <linux/err.h>
24#include <linux/errno.h>
25#include <linux/fs.h>
26#include <linux/interrupt.h>
27#include <linux/kernel.h>
28#include <linux/mm.h>
29#include <linux/moduleparam.h>
30#include <linux/of.h>
31#include <linux/time.h>
32#include <linux/slab.h>
33#include <linux/device.h>
34#include <linux/platform_device.h>
35#include <linux/videodev2.h>
36#include <linux/pm_runtime.h>
37#include <linux/sched.h>
38
39#include <media/v4l2-async.h>
40#include <media/v4l2-common.h>
41#include <media/v4l2-dev.h>
42#include <media/soc_camera.h>
43#include <media/drv-intf/sh_mobile_ceu.h>
44#include <media/videobuf2-dma-contig.h>
45#include <media/v4l2-mediabus.h>
46#include <media/drv-intf/soc_mediabus.h>
47
48#include "soc_scale_crop.h"
49
50
51
52#define CAPSR 0x00
53#define CAPCR 0x04
54#define CAMCR 0x08
55#define CMCYR 0x0c
56#define CAMOR 0x10
57#define CAPWR 0x14
58#define CAIFR 0x18
59#define CSTCR 0x20
60#define CSECR 0x24
61#define CRCNTR 0x28
62#define CRCMPR 0x2c
63#define CFLCR 0x30
64#define CFSZR 0x34
65#define CDWDR 0x38
66#define CDAYR 0x3c
67#define CDACR 0x40
68#define CDBYR 0x44
69#define CDBCR 0x48
70#define CBDSR 0x4c
71#define CFWCR 0x5c
72#define CLFCR 0x60
73#define CDOCR 0x64
74#define CDDCR 0x68
75#define CDDAR 0x6c
76#define CEIER 0x70
77#define CETCR 0x74
78#define CSTSR 0x7c
79#define CSRTR 0x80
80#define CDSSR 0x84
81#define CDAYR2 0x90
82#define CDACR2 0x94
83#define CDBYR2 0x98
84#define CDBCR2 0x9c
85
86#undef DEBUG_GEOMETRY
87#ifdef DEBUG_GEOMETRY
88#define dev_geo dev_info
89#else
90#define dev_geo dev_dbg
91#endif
92
93
94struct sh_mobile_ceu_buffer {
95 struct vb2_v4l2_buffer vb;
96 struct list_head queue;
97};
98
99struct sh_mobile_ceu_dev {
100 struct soc_camera_host ici;
101
102 unsigned int irq;
103 void __iomem *base;
104 size_t video_limit;
105 size_t buf_total;
106
107 spinlock_t lock;
108 struct list_head capture;
109 struct vb2_v4l2_buffer *active;
110
111 struct sh_mobile_ceu_info *pdata;
112 struct completion complete;
113
114 u32 cflcr;
115
116
117 int max_width;
118 int max_height;
119
120 enum v4l2_field field;
121 int sequence;
122 unsigned long flags;
123
124 unsigned int image_mode:1;
125 unsigned int is_16bit:1;
126 unsigned int frozen:1;
127};
128
129struct sh_mobile_ceu_cam {
130
131 unsigned int ceu_left;
132 unsigned int ceu_top;
133
134 unsigned int width;
135 unsigned int height;
136
137
138
139
140
141 struct v4l2_rect subrect;
142
143 struct v4l2_rect rect;
144 const struct soc_mbus_pixelfmt *extra_fmt;
145 u32 code;
146};
147
148static struct sh_mobile_ceu_buffer *to_ceu_vb(struct vb2_v4l2_buffer *vbuf)
149{
150 return container_of(vbuf, struct sh_mobile_ceu_buffer, vb);
151}
152
153static void ceu_write(struct sh_mobile_ceu_dev *priv,
154 unsigned long reg_offs, u32 data)
155{
156 iowrite32(data, priv->base + reg_offs);
157}
158
159static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
160{
161 return ioread32(priv->base + reg_offs);
162}
163
164static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
165{
166 int i, success = 0;
167
168 ceu_write(pcdev, CAPSR, 1 << 16);
169
170
171 for (i = 0; i < 1000; i++) {
172 if (!(ceu_read(pcdev, CSTSR) & 1)) {
173 success++;
174 break;
175 }
176 udelay(1);
177 }
178
179
180 for (i = 0; i < 1000; i++) {
181 if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
182 success++;
183 break;
184 }
185 udelay(1);
186 }
187
188 if (2 != success) {
189 dev_warn(pcdev->ici.v4l2_dev.dev, "soft reset time out\n");
190 return -EIO;
191 }
192
193 return 0;
194}
195
196
197
198
199
200
201
202
203
204
205static int sh_mobile_ceu_videobuf_setup(struct vb2_queue *vq,
206 unsigned int *count, unsigned int *num_planes,
207 unsigned int sizes[], struct device *alloc_devs[])
208{
209 struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
210 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
211 struct sh_mobile_ceu_dev *pcdev = ici->priv;
212
213 if (!vq->num_buffers)
214 pcdev->sequence = 0;
215
216 if (!*count)
217 *count = 2;
218
219
220 if (!*num_planes)
221 sizes[0] = icd->sizeimage;
222 else if (sizes[0] < icd->sizeimage)
223 return -EINVAL;
224
225
226 if (pcdev->video_limit) {
227 size_t size = PAGE_ALIGN(sizes[0]) * *count;
228
229 if (size + pcdev->buf_total > pcdev->video_limit)
230 *count = (pcdev->video_limit - pcdev->buf_total) /
231 PAGE_ALIGN(sizes[0]);
232 }
233
234 *num_planes = 1;
235
236 dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]);
237
238 return 0;
239}
240
241#define CEU_CETCR_MAGIC 0x0317f313
242#define CEU_CETCR_IGRW (1 << 4)
243#define CEU_CEIER_CPEIE (1 << 0)
244#define CEU_CEIER_VBP (1 << 20)
245#define CEU_CAPCR_CTNCP (1 << 16)
246#define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
247
248
249
250
251
252
253static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
254{
255 struct soc_camera_device *icd = pcdev->ici.icd;
256 dma_addr_t phys_addr_top, phys_addr_bottom;
257 unsigned long top1, top2;
258 unsigned long bottom1, bottom2;
259 u32 status;
260 bool planar;
261 int ret = 0;
262
263
264
265
266
267
268 ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
269 status = ceu_read(pcdev, CETCR);
270 ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
271 if (!pcdev->frozen)
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);
275
276
277
278
279
280
281 if (status & CEU_CEIER_VBP) {
282 sh_mobile_ceu_soft_reset(pcdev);
283 ret = -EIO;
284 }
285
286 if (pcdev->frozen) {
287 complete(&pcdev->complete);
288 return ret;
289 }
290
291 if (!pcdev->active)
292 return ret;
293
294 if (V4L2_FIELD_INTERLACED_BT == pcdev->field) {
295 top1 = CDBYR;
296 top2 = CDBCR;
297 bottom1 = CDAYR;
298 bottom2 = CDACR;
299 } else {
300 top1 = CDAYR;
301 top2 = CDACR;
302 bottom1 = CDBYR;
303 bottom2 = CDBCR;
304 }
305
306 phys_addr_top =
307 vb2_dma_contig_plane_dma_addr(&pcdev->active->vb2_buf, 0);
308
309 switch (icd->current_fmt->host_fmt->fourcc) {
310 case V4L2_PIX_FMT_NV12:
311 case V4L2_PIX_FMT_NV21:
312 case V4L2_PIX_FMT_NV16:
313 case V4L2_PIX_FMT_NV61:
314 planar = true;
315 break;
316 default:
317 planar = false;
318 }
319
320 ceu_write(pcdev, top1, phys_addr_top);
321 if (V4L2_FIELD_NONE != pcdev->field) {
322 phys_addr_bottom = phys_addr_top + icd->bytesperline;
323 ceu_write(pcdev, bottom1, phys_addr_bottom);
324 }
325
326 if (planar) {
327 phys_addr_top += icd->bytesperline * icd->user_height;
328 ceu_write(pcdev, top2, phys_addr_top);
329 if (V4L2_FIELD_NONE != pcdev->field) {
330 phys_addr_bottom = phys_addr_top + icd->bytesperline;
331 ceu_write(pcdev, bottom2, phys_addr_bottom);
332 }
333 }
334
335 ceu_write(pcdev, CAPSR, 0x1);
336
337 return ret;
338}
339
340static int sh_mobile_ceu_videobuf_prepare(struct vb2_buffer *vb)
341{
342 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
343 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vbuf);
344
345
346 WARN(!list_empty(&buf->queue), "Buffer %p on queue!\n", vb);
347
348 return 0;
349}
350
351static void sh_mobile_ceu_videobuf_queue(struct vb2_buffer *vb)
352{
353 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
354 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
355 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
356 struct sh_mobile_ceu_dev *pcdev = ici->priv;
357 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vbuf);
358 unsigned long size;
359
360 size = icd->sizeimage;
361
362 if (vb2_plane_size(vb, 0) < size) {
363 dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n",
364 vb->index, vb2_plane_size(vb, 0), size);
365 goto error;
366 }
367
368 vb2_set_plane_payload(vb, 0, size);
369
370 dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__,
371 vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0));
372
373#ifdef DEBUG
374
375
376
377
378 if (vb2_plane_vaddr(vb, 0))
379 memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
380#endif
381
382 spin_lock_irq(&pcdev->lock);
383 list_add_tail(&buf->queue, &pcdev->capture);
384
385 if (!pcdev->active) {
386
387
388
389
390
391 pcdev->active = vbuf;
392 sh_mobile_ceu_capture(pcdev);
393 }
394 spin_unlock_irq(&pcdev->lock);
395
396 return;
397
398error:
399 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
400}
401
402static void sh_mobile_ceu_videobuf_release(struct vb2_buffer *vb)
403{
404 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
405 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
406 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
407 struct sh_mobile_ceu_buffer *buf = to_ceu_vb(vbuf);
408 struct sh_mobile_ceu_dev *pcdev = ici->priv;
409
410 spin_lock_irq(&pcdev->lock);
411
412 if (pcdev->active == vbuf) {
413
414 ceu_write(pcdev, CAPSR, 1 << 16);
415 pcdev->active = NULL;
416 }
417
418
419
420
421
422 if (buf->queue.next)
423 list_del_init(&buf->queue);
424
425 pcdev->buf_total -= PAGE_ALIGN(vb2_plane_size(vb, 0));
426 dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
427 pcdev->buf_total);
428
429 spin_unlock_irq(&pcdev->lock);
430}
431
432static int sh_mobile_ceu_videobuf_init(struct vb2_buffer *vb)
433{
434 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
435 struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
436 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
437 struct sh_mobile_ceu_dev *pcdev = ici->priv;
438
439 pcdev->buf_total += PAGE_ALIGN(vb2_plane_size(vb, 0));
440 dev_dbg(icd->parent, "%s() %zu bytes buffers\n", __func__,
441 pcdev->buf_total);
442
443
444 INIT_LIST_HEAD(&to_ceu_vb(vbuf)->queue);
445 return 0;
446}
447
448static void sh_mobile_ceu_stop_streaming(struct vb2_queue *q)
449{
450 struct soc_camera_device *icd = soc_camera_from_vb2q(q);
451 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
452 struct sh_mobile_ceu_dev *pcdev = ici->priv;
453 struct list_head *buf_head, *tmp;
454 struct vb2_v4l2_buffer *vbuf;
455
456 spin_lock_irq(&pcdev->lock);
457
458 pcdev->active = NULL;
459
460 list_for_each_safe(buf_head, tmp, &pcdev->capture) {
461 vbuf = &list_entry(buf_head, struct sh_mobile_ceu_buffer,
462 queue)->vb;
463 vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
464 list_del_init(buf_head);
465 }
466
467 spin_unlock_irq(&pcdev->lock);
468
469 sh_mobile_ceu_soft_reset(pcdev);
470}
471
472static const struct vb2_ops sh_mobile_ceu_videobuf_ops = {
473 .queue_setup = sh_mobile_ceu_videobuf_setup,
474 .buf_prepare = sh_mobile_ceu_videobuf_prepare,
475 .buf_queue = sh_mobile_ceu_videobuf_queue,
476 .buf_cleanup = sh_mobile_ceu_videobuf_release,
477 .buf_init = sh_mobile_ceu_videobuf_init,
478 .wait_prepare = vb2_ops_wait_prepare,
479 .wait_finish = vb2_ops_wait_finish,
480 .stop_streaming = sh_mobile_ceu_stop_streaming,
481};
482
483static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
484{
485 struct sh_mobile_ceu_dev *pcdev = data;
486 struct vb2_v4l2_buffer *vbuf;
487 int ret;
488
489 spin_lock(&pcdev->lock);
490
491 vbuf = pcdev->active;
492 if (!vbuf)
493
494 goto out;
495
496 list_del_init(&to_ceu_vb(vbuf)->queue);
497
498 if (!list_empty(&pcdev->capture))
499 pcdev->active = &list_entry(pcdev->capture.next,
500 struct sh_mobile_ceu_buffer, queue)->vb;
501 else
502 pcdev->active = NULL;
503
504 ret = sh_mobile_ceu_capture(pcdev);
505 vbuf->vb2_buf.timestamp = ktime_get_ns();
506 if (!ret) {
507 vbuf->field = pcdev->field;
508 vbuf->sequence = pcdev->sequence++;
509 }
510 vb2_buffer_done(&vbuf->vb2_buf,
511 ret < 0 ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
512
513out:
514 spin_unlock(&pcdev->lock);
515
516 return IRQ_HANDLED;
517}
518
519static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
520{
521 dev_info(icd->parent,
522 "SuperH Mobile CEU driver attached to camera %d\n",
523 icd->devnum);
524
525 return 0;
526}
527
528static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
529{
530 dev_info(icd->parent,
531 "SuperH Mobile CEU driver detached from camera %d\n",
532 icd->devnum);
533}
534
535
536static int sh_mobile_ceu_clock_start(struct soc_camera_host *ici)
537{
538 struct sh_mobile_ceu_dev *pcdev = ici->priv;
539
540 pm_runtime_get_sync(ici->v4l2_dev.dev);
541
542 pcdev->buf_total = 0;
543
544 sh_mobile_ceu_soft_reset(pcdev);
545
546 return 0;
547}
548
549
550static void sh_mobile_ceu_clock_stop(struct soc_camera_host *ici)
551{
552 struct sh_mobile_ceu_dev *pcdev = ici->priv;
553
554
555 ceu_write(pcdev, CEIER, 0);
556 sh_mobile_ceu_soft_reset(pcdev);
557
558
559 spin_lock_irq(&pcdev->lock);
560 if (pcdev->active) {
561 list_del_init(&to_ceu_vb(pcdev->active)->queue);
562 vb2_buffer_done(&pcdev->active->vb2_buf, VB2_BUF_STATE_ERROR);
563 pcdev->active = NULL;
564 }
565 spin_unlock_irq(&pcdev->lock);
566
567 pm_runtime_put(ici->v4l2_dev.dev);
568}
569
570
571
572
573
574static unsigned int size_dst(unsigned int src, unsigned int scale)
575{
576 unsigned int mant_pre = scale >> 12;
577 if (!src || !scale)
578 return src;
579 return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
580 mant_pre * 4096 / scale + 1;
581}
582
583static u16 calc_scale(unsigned int src, unsigned int *dst)
584{
585 u16 scale;
586
587 if (src == *dst)
588 return 0;
589
590 scale = (src * 4096 / *dst) & ~7;
591
592 while (scale > 4096 && size_dst(src, scale) < *dst)
593 scale -= 8;
594
595 *dst = size_dst(src, scale);
596
597 return scale;
598}
599
600
601static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd)
602{
603 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
604 struct sh_mobile_ceu_cam *cam = icd->host_priv;
605 struct sh_mobile_ceu_dev *pcdev = ici->priv;
606 unsigned int height, width, cdwdr_width, in_width, in_height;
607 unsigned int left_offset, top_offset;
608 u32 camor;
609
610 dev_geo(icd->parent, "Crop %ux%u@%u:%u\n",
611 icd->user_width, icd->user_height, cam->ceu_left, cam->ceu_top);
612
613 left_offset = cam->ceu_left;
614 top_offset = cam->ceu_top;
615
616 WARN_ON(icd->user_width & 3 || icd->user_height & 3);
617
618 width = icd->user_width;
619
620 if (pcdev->image_mode) {
621 in_width = cam->width;
622 if (!pcdev->is_16bit) {
623 in_width *= 2;
624 left_offset *= 2;
625 }
626 } else {
627 unsigned int w_factor;
628
629 switch (icd->current_fmt->host_fmt->packing) {
630 case SOC_MBUS_PACKING_2X8_PADHI:
631 w_factor = 2;
632 break;
633 default:
634 w_factor = 1;
635 }
636
637 in_width = cam->width * w_factor;
638 left_offset *= w_factor;
639 }
640
641 cdwdr_width = icd->bytesperline;
642
643 height = icd->user_height;
644 in_height = cam->height;
645 if (V4L2_FIELD_NONE != pcdev->field) {
646 height = (height / 2) & ~3;
647 in_height /= 2;
648 top_offset /= 2;
649 cdwdr_width *= 2;
650 }
651
652
653 camor = left_offset | (top_offset << 16);
654
655 dev_geo(icd->parent,
656 "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
657 (in_height << 16) | in_width, (height << 16) | width,
658 cdwdr_width);
659
660 ceu_write(pcdev, CAMOR, camor);
661 ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
662
663 ceu_write(pcdev, CFSZR, (height << 16) | width);
664 ceu_write(pcdev, CDWDR, cdwdr_width);
665}
666
667static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
668{
669 u32 capsr = ceu_read(pcdev, CAPSR);
670 ceu_write(pcdev, CAPSR, 1 << 16);
671 return capsr;
672}
673
674static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
675{
676 unsigned long timeout = jiffies + 10 * HZ;
677
678
679
680
681
682 while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
683 msleep(1);
684
685 if (time_after(jiffies, timeout)) {
686 dev_err(pcdev->ici.v4l2_dev.dev,
687 "Timeout waiting for frame end! Interface problem?\n");
688 return;
689 }
690
691
692 while (ceu_read(pcdev, CAPSR) & (1 << 16))
693 udelay(10);
694
695
696 if (capsr & ~(1 << 16))
697 ceu_write(pcdev, CAPSR, capsr);
698}
699
700#define CEU_BUS_FLAGS (V4L2_MBUS_MASTER | \
701 V4L2_MBUS_PCLK_SAMPLE_RISING | \
702 V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
703 V4L2_MBUS_HSYNC_ACTIVE_LOW | \
704 V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
705 V4L2_MBUS_VSYNC_ACTIVE_LOW | \
706 V4L2_MBUS_DATA_ACTIVE_HIGH)
707
708
709static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd)
710{
711 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
712 struct sh_mobile_ceu_dev *pcdev = ici->priv;
713 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
714 struct sh_mobile_ceu_cam *cam = icd->host_priv;
715 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
716 unsigned long value, common_flags = CEU_BUS_FLAGS;
717 u32 capsr = capture_save_reset(pcdev);
718 unsigned int yuv_lineskip;
719 int ret;
720
721
722
723
724
725 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
726 if (!ret) {
727 common_flags = soc_mbus_config_compatible(&cfg,
728 common_flags);
729 if (!common_flags)
730 return -EINVAL;
731 } else if (ret != -ENOIOCTLCMD) {
732 return ret;
733 }
734
735
736 if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
737 (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
738 if (pcdev->flags & SH_CEU_FLAG_HSYNC_LOW)
739 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
740 else
741 common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
742 }
743
744 if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
745 (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
746 if (pcdev->flags & SH_CEU_FLAG_VSYNC_LOW)
747 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
748 else
749 common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
750 }
751
752 cfg.flags = common_flags;
753 ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
754 if (ret < 0 && ret != -ENOIOCTLCMD)
755 return ret;
756
757 if (icd->current_fmt->host_fmt->bits_per_sample > 8)
758 pcdev->is_16bit = 1;
759 else
760 pcdev->is_16bit = 0;
761
762 ceu_write(pcdev, CRCNTR, 0);
763 ceu_write(pcdev, CRCMPR, 0);
764
765 value = 0x00000010;
766 yuv_lineskip = 0x10;
767
768 switch (icd->current_fmt->host_fmt->fourcc) {
769 case V4L2_PIX_FMT_NV12:
770 case V4L2_PIX_FMT_NV21:
771
772 yuv_lineskip = 0;
773
774 case V4L2_PIX_FMT_NV16:
775 case V4L2_PIX_FMT_NV61:
776 switch (cam->code) {
777 case MEDIA_BUS_FMT_UYVY8_2X8:
778 value = 0x00000000;
779 break;
780 case MEDIA_BUS_FMT_VYUY8_2X8:
781 value = 0x00000100;
782 break;
783 case MEDIA_BUS_FMT_YUYV8_2X8:
784 value = 0x00000200;
785 break;
786 case MEDIA_BUS_FMT_YVYU8_2X8:
787 value = 0x00000300;
788 break;
789 default:
790 BUG();
791 }
792 }
793
794 if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
795 icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_NV61)
796 value ^= 0x00000100;
797
798 value |= common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
799 value |= common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
800
801 if (pcdev->is_16bit)
802 value |= 1 << 12;
803 else if (pcdev->flags & SH_CEU_FLAG_LOWER_8BIT)
804 value |= 2 << 12;
805
806 ceu_write(pcdev, CAMCR, value);
807
808 ceu_write(pcdev, CAPCR, 0x00300000);
809
810 switch (pcdev->field) {
811 case V4L2_FIELD_INTERLACED_TB:
812 value = 0x101;
813 break;
814 case V4L2_FIELD_INTERLACED_BT:
815 value = 0x102;
816 break;
817 default:
818 value = 0;
819 break;
820 }
821 ceu_write(pcdev, CAIFR, value);
822
823 sh_mobile_ceu_set_rect(icd);
824 mdelay(1);
825
826 dev_geo(icd->parent, "CFLCR 0x%x\n", pcdev->cflcr);
827 ceu_write(pcdev, CFLCR, pcdev->cflcr);
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842 value = 0x00000007 | yuv_lineskip;
843
844 ceu_write(pcdev, CDOCR, value);
845 ceu_write(pcdev, CFWCR, 0);
846
847 capture_restore(pcdev, capsr);
848
849
850 return 0;
851}
852
853static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd,
854 unsigned char buswidth)
855{
856 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
857 unsigned long common_flags = CEU_BUS_FLAGS;
858 struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
859 int ret;
860
861 ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
862 if (!ret)
863 common_flags = soc_mbus_config_compatible(&cfg,
864 common_flags);
865 else if (ret != -ENOIOCTLCMD)
866 return ret;
867
868 if (!common_flags || buswidth > 16)
869 return -EINVAL;
870
871 return 0;
872}
873
874static const struct soc_mbus_pixelfmt sh_mobile_ceu_formats[] = {
875 {
876 .fourcc = V4L2_PIX_FMT_NV12,
877 .name = "NV12",
878 .bits_per_sample = 8,
879 .packing = SOC_MBUS_PACKING_1_5X8,
880 .order = SOC_MBUS_ORDER_LE,
881 .layout = SOC_MBUS_LAYOUT_PLANAR_2Y_C,
882 }, {
883 .fourcc = V4L2_PIX_FMT_NV21,
884 .name = "NV21",
885 .bits_per_sample = 8,
886 .packing = SOC_MBUS_PACKING_1_5X8,
887 .order = SOC_MBUS_ORDER_LE,
888 .layout = SOC_MBUS_LAYOUT_PLANAR_2Y_C,
889 }, {
890 .fourcc = V4L2_PIX_FMT_NV16,
891 .name = "NV16",
892 .bits_per_sample = 8,
893 .packing = SOC_MBUS_PACKING_2X8_PADHI,
894 .order = SOC_MBUS_ORDER_LE,
895 .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C,
896 }, {
897 .fourcc = V4L2_PIX_FMT_NV61,
898 .name = "NV61",
899 .bits_per_sample = 8,
900 .packing = SOC_MBUS_PACKING_2X8_PADHI,
901 .order = SOC_MBUS_ORDER_LE,
902 .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C,
903 },
904};
905
906
907static bool sh_mobile_ceu_packing_supported(const struct soc_mbus_pixelfmt *fmt)
908{
909 return fmt->packing == SOC_MBUS_PACKING_NONE ||
910 (fmt->bits_per_sample == 8 &&
911 fmt->packing == SOC_MBUS_PACKING_1_5X8) ||
912 (fmt->bits_per_sample == 8 &&
913 fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
914 (fmt->bits_per_sample > 8 &&
915 fmt->packing == SOC_MBUS_PACKING_EXTEND16);
916}
917
918static struct soc_camera_device *ctrl_to_icd(struct v4l2_ctrl *ctrl)
919{
920 return container_of(ctrl->handler, struct soc_camera_device,
921 ctrl_handler);
922}
923
924static int sh_mobile_ceu_s_ctrl(struct v4l2_ctrl *ctrl)
925{
926 struct soc_camera_device *icd = ctrl_to_icd(ctrl);
927 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
928 struct sh_mobile_ceu_dev *pcdev = ici->priv;
929
930 switch (ctrl->id) {
931 case V4L2_CID_SHARPNESS:
932 switch (icd->current_fmt->host_fmt->fourcc) {
933 case V4L2_PIX_FMT_NV12:
934 case V4L2_PIX_FMT_NV21:
935 case V4L2_PIX_FMT_NV16:
936 case V4L2_PIX_FMT_NV61:
937 ceu_write(pcdev, CLFCR, !ctrl->val);
938 return 0;
939 }
940 break;
941 }
942
943 return -EINVAL;
944}
945
946static const struct v4l2_ctrl_ops sh_mobile_ceu_ctrl_ops = {
947 .s_ctrl = sh_mobile_ceu_s_ctrl,
948};
949
950static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, unsigned int idx,
951 struct soc_camera_format_xlate *xlate)
952{
953 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
954 struct device *dev = icd->parent;
955 struct soc_camera_host *ici = to_soc_camera_host(dev);
956 struct sh_mobile_ceu_dev *pcdev = ici->priv;
957 int ret, k, n;
958 int formats = 0;
959 struct sh_mobile_ceu_cam *cam;
960 struct v4l2_subdev_mbus_code_enum code = {
961 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
962 .index = idx,
963 };
964 const struct soc_mbus_pixelfmt *fmt;
965
966 ret = v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
967 if (ret < 0)
968
969 return 0;
970
971 fmt = soc_mbus_get_fmtdesc(code.code);
972 if (!fmt) {
973 dev_warn(dev, "unsupported format code #%u: %d\n", idx, code.code);
974 return 0;
975 }
976
977 ret = sh_mobile_ceu_try_bus_param(icd, fmt->bits_per_sample);
978 if (ret < 0)
979 return 0;
980
981 if (!icd->host_priv) {
982 struct v4l2_subdev_format fmt = {
983 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
984 };
985 struct v4l2_mbus_framefmt *mf = &fmt.format;
986 struct v4l2_rect rect;
987 int shift = 0;
988
989
990 v4l2_ctrl_new_std(&icd->ctrl_handler, &sh_mobile_ceu_ctrl_ops,
991 V4L2_CID_SHARPNESS, 0, 1, 1, 1);
992 if (icd->ctrl_handler.error)
993 return icd->ctrl_handler.error;
994
995
996
997
998 ret = soc_camera_client_g_rect(sd, &rect);
999 if (ret < 0)
1000 return ret;
1001
1002
1003 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
1004 if (ret < 0)
1005 return ret;
1006
1007
1008
1009
1010
1011
1012
1013
1014 while ((mf->width > pcdev->max_width ||
1015 mf->height > pcdev->max_height) && shift < 4) {
1016
1017 mf->width = 2560 >> shift;
1018 mf->height = 1920 >> shift;
1019 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1020 soc_camera_grp_id(icd), pad,
1021 set_fmt, NULL, &fmt);
1022 if (ret < 0)
1023 return ret;
1024 shift++;
1025 }
1026
1027 if (shift == 4) {
1028 dev_err(dev, "Failed to configure the client below %ux%x\n",
1029 mf->width, mf->height);
1030 return -EIO;
1031 }
1032
1033 dev_geo(dev, "camera fmt %ux%u\n", mf->width, mf->height);
1034
1035 cam = kzalloc(sizeof(*cam), GFP_KERNEL);
1036 if (!cam)
1037 return -ENOMEM;
1038
1039
1040 cam->rect = rect;
1041 cam->subrect = rect;
1042
1043 cam->width = mf->width;
1044 cam->height = mf->height;
1045
1046 icd->host_priv = cam;
1047 } else {
1048 cam = icd->host_priv;
1049 }
1050
1051
1052 if (!idx)
1053 cam->extra_fmt = NULL;
1054
1055 switch (code.code) {
1056 case MEDIA_BUS_FMT_UYVY8_2X8:
1057 case MEDIA_BUS_FMT_VYUY8_2X8:
1058 case MEDIA_BUS_FMT_YUYV8_2X8:
1059 case MEDIA_BUS_FMT_YVYU8_2X8:
1060 if (cam->extra_fmt)
1061 break;
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072 cam->extra_fmt = sh_mobile_ceu_formats;
1073
1074 n = ARRAY_SIZE(sh_mobile_ceu_formats);
1075 formats += n;
1076 for (k = 0; xlate && k < n; k++) {
1077 xlate->host_fmt = &sh_mobile_ceu_formats[k];
1078 xlate->code = code.code;
1079 xlate++;
1080 dev_dbg(dev, "Providing format %s using code %d\n",
1081 sh_mobile_ceu_formats[k].name, code.code);
1082 }
1083 break;
1084 default:
1085 if (!sh_mobile_ceu_packing_supported(fmt))
1086 return 0;
1087 }
1088
1089
1090 formats++;
1091 if (xlate) {
1092 xlate->host_fmt = fmt;
1093 xlate->code = code.code;
1094 xlate++;
1095 dev_dbg(dev, "Providing format %s in pass-through mode\n",
1096 fmt->name);
1097 }
1098
1099 return formats;
1100}
1101
1102static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
1103{
1104 kfree(icd->host_priv);
1105 icd->host_priv = NULL;
1106}
1107
1108#define scale_down(size, scale) soc_camera_shift_scale(size, 12, scale)
1109#define calc_generic_scale(in, out) soc_camera_calc_scale(in, 12, out)
1110
1111
1112
1113
1114
1115
1116
1117static int sh_mobile_ceu_set_selection(struct soc_camera_device *icd,
1118 struct v4l2_selection *sel)
1119{
1120 struct v4l2_rect *rect = &sel->r;
1121 struct device *dev = icd->parent;
1122 struct soc_camera_host *ici = to_soc_camera_host(dev);
1123 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1124 struct v4l2_selection cam_sel;
1125 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1126 struct v4l2_rect *cam_rect = &cam_sel.r;
1127 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1128 struct v4l2_subdev_format fmt = {
1129 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1130 };
1131 struct v4l2_mbus_framefmt *mf = &fmt.format;
1132 unsigned int scale_cam_h, scale_cam_v, scale_ceu_h, scale_ceu_v,
1133 out_width, out_height;
1134 int interm_width, interm_height;
1135 u32 capsr, cflcr;
1136 int ret;
1137
1138 dev_geo(dev, "S_SELECTION(%ux%u@%u:%u)\n", rect->width, rect->height,
1139 rect->left, rect->top);
1140
1141
1142 capsr = capture_save_reset(pcdev);
1143 dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
1144
1145
1146
1147
1148
1149 ret = soc_camera_client_s_selection(sd, sel, &cam_sel,
1150 &cam->rect, &cam->subrect);
1151 if (ret < 0)
1152 return ret;
1153
1154 dev_geo(dev, "1-2: camera cropped to %ux%u@%u:%u\n",
1155 cam_rect->width, cam_rect->height,
1156 cam_rect->left, cam_rect->top);
1157
1158
1159
1160
1161 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
1162 if (ret < 0)
1163 return ret;
1164
1165 if (mf->width > pcdev->max_width || mf->height > pcdev->max_height)
1166 return -EINVAL;
1167
1168
1169 scale_cam_h = calc_generic_scale(cam_rect->width, mf->width);
1170 scale_cam_v = calc_generic_scale(cam_rect->height, mf->height);
1171
1172
1173 interm_width = scale_down(rect->width, scale_cam_h);
1174 interm_height = scale_down(rect->height, scale_cam_v);
1175
1176 if (interm_width < icd->user_width) {
1177 u32 new_scale_h;
1178
1179 new_scale_h = calc_generic_scale(rect->width, icd->user_width);
1180
1181 mf->width = scale_down(cam_rect->width, new_scale_h);
1182 }
1183
1184 if (interm_height < icd->user_height) {
1185 u32 new_scale_v;
1186
1187 new_scale_v = calc_generic_scale(rect->height, icd->user_height);
1188
1189 mf->height = scale_down(cam_rect->height, new_scale_v);
1190 }
1191
1192 if (interm_width < icd->user_width || interm_height < icd->user_height) {
1193 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1194 soc_camera_grp_id(icd), pad,
1195 set_fmt, NULL, &fmt);
1196 if (ret < 0)
1197 return ret;
1198
1199 dev_geo(dev, "New camera output %ux%u\n", mf->width, mf->height);
1200 scale_cam_h = calc_generic_scale(cam_rect->width, mf->width);
1201 scale_cam_v = calc_generic_scale(cam_rect->height, mf->height);
1202 interm_width = scale_down(rect->width, scale_cam_h);
1203 interm_height = scale_down(rect->height, scale_cam_v);
1204 }
1205
1206
1207 cam->width = mf->width;
1208 cam->height = mf->height;
1209
1210 if (pcdev->image_mode) {
1211 out_width = min(interm_width, icd->user_width);
1212 out_height = min(interm_height, icd->user_height);
1213 } else {
1214 out_width = interm_width;
1215 out_height = interm_height;
1216 }
1217
1218
1219
1220
1221
1222 scale_ceu_h = calc_scale(interm_width, &out_width);
1223 scale_ceu_v = calc_scale(interm_height, &out_height);
1224
1225 dev_geo(dev, "5: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
1226
1227
1228 cflcr = scale_ceu_h | (scale_ceu_v << 16);
1229 if (cflcr != pcdev->cflcr) {
1230 pcdev->cflcr = cflcr;
1231 ceu_write(pcdev, CFLCR, cflcr);
1232 }
1233
1234 icd->user_width = out_width & ~3;
1235 icd->user_height = out_height & ~3;
1236
1237 cam->ceu_left = scale_down(rect->left - cam_rect->left, scale_cam_h) & ~1;
1238 cam->ceu_top = scale_down(rect->top - cam_rect->top, scale_cam_v) & ~1;
1239
1240
1241 sh_mobile_ceu_set_rect(icd);
1242
1243 cam->subrect = *rect;
1244
1245 dev_geo(dev, "6: CEU cropped to %ux%u@%u:%u\n",
1246 icd->user_width, icd->user_height,
1247 cam->ceu_left, cam->ceu_top);
1248
1249
1250 if (pcdev->active)
1251 capsr |= 1;
1252 capture_restore(pcdev, capsr);
1253
1254
1255 return ret;
1256}
1257
1258static int sh_mobile_ceu_get_selection(struct soc_camera_device *icd,
1259 struct v4l2_selection *sel)
1260{
1261 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1262
1263 sel->r = cam->subrect;
1264
1265 return 0;
1266}
1267
1268
1269static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
1270 struct v4l2_format *f)
1271{
1272 struct device *dev = icd->parent;
1273 struct soc_camera_host *ici = to_soc_camera_host(dev);
1274 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1275 struct sh_mobile_ceu_cam *cam = icd->host_priv;
1276 struct v4l2_pix_format *pix = &f->fmt.pix;
1277 struct v4l2_mbus_framefmt mf;
1278 __u32 pixfmt = pix->pixelformat;
1279 const struct soc_camera_format_xlate *xlate;
1280 unsigned int ceu_sub_width = pcdev->max_width,
1281 ceu_sub_height = pcdev->max_height;
1282 u16 scale_v, scale_h;
1283 int ret;
1284 bool image_mode;
1285 enum v4l2_field field;
1286
1287 switch (pix->field) {
1288 default:
1289 pix->field = V4L2_FIELD_NONE;
1290
1291 case V4L2_FIELD_INTERLACED_TB:
1292 case V4L2_FIELD_INTERLACED_BT:
1293 case V4L2_FIELD_NONE:
1294 field = pix->field;
1295 break;
1296 case V4L2_FIELD_INTERLACED:
1297 field = V4L2_FIELD_INTERLACED_TB;
1298 break;
1299 }
1300
1301 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1302 if (!xlate) {
1303 dev_warn(dev, "Format %x not found\n", pixfmt);
1304 return -EINVAL;
1305 }
1306
1307
1308 soc_camera_calc_client_output(icd, &cam->rect, &cam->subrect, pix, &mf, 12);
1309 mf.field = pix->field;
1310 mf.colorspace = pix->colorspace;
1311 mf.code = xlate->code;
1312
1313 switch (pixfmt) {
1314 case V4L2_PIX_FMT_NV12:
1315 case V4L2_PIX_FMT_NV21:
1316 case V4L2_PIX_FMT_NV16:
1317 case V4L2_PIX_FMT_NV61:
1318 image_mode = true;
1319 break;
1320 default:
1321 image_mode = false;
1322 }
1323
1324 dev_geo(dev, "S_FMT(pix=0x%x, fld 0x%x, code 0x%x, %ux%u)\n", pixfmt, mf.field, mf.code,
1325 pix->width, pix->height);
1326
1327 dev_geo(dev, "4: request camera output %ux%u\n", mf.width, mf.height);
1328
1329
1330 ret = soc_camera_client_scale(icd, &cam->rect, &cam->subrect,
1331 &mf, &ceu_sub_width, &ceu_sub_height,
1332 image_mode && V4L2_FIELD_NONE == field, 12);
1333
1334 dev_geo(dev, "5-9: client scale return %d\n", ret);
1335
1336
1337
1338 dev_geo(dev, "fmt %ux%u, requested %ux%u\n",
1339 mf.width, mf.height, pix->width, pix->height);
1340 if (ret < 0)
1341 return ret;
1342
1343 if (mf.code != xlate->code)
1344 return -EINVAL;
1345
1346
1347 cam->width = mf.width;
1348 cam->height = mf.height;
1349
1350
1351
1352
1353 if (pix->width > ceu_sub_width)
1354 ceu_sub_width = pix->width;
1355
1356 if (pix->height > ceu_sub_height)
1357 ceu_sub_height = pix->height;
1358
1359 pix->colorspace = mf.colorspace;
1360
1361 if (image_mode) {
1362
1363 scale_h = calc_scale(ceu_sub_width, &pix->width);
1364 scale_v = calc_scale(ceu_sub_height, &pix->height);
1365 } else {
1366 pix->width = ceu_sub_width;
1367 pix->height = ceu_sub_height;
1368 scale_h = 0;
1369 scale_v = 0;
1370 }
1371
1372 pcdev->cflcr = scale_h | (scale_v << 16);
1373
1374
1375
1376
1377
1378
1379 dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
1380 ceu_sub_width, scale_h, pix->width,
1381 ceu_sub_height, scale_v, pix->height);
1382
1383 cam->code = xlate->code;
1384 icd->current_fmt = xlate;
1385
1386 pcdev->field = field;
1387 pcdev->image_mode = image_mode;
1388
1389
1390 pix->width &= ~3;
1391 pix->height &= ~3;
1392
1393 return 0;
1394}
1395
1396#define CEU_CHDW_MAX 8188U
1397
1398static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
1399 struct v4l2_format *f)
1400{
1401 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1402 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1403 const struct soc_camera_format_xlate *xlate;
1404 struct v4l2_pix_format *pix = &f->fmt.pix;
1405 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1406 struct v4l2_subdev_pad_config pad_cfg;
1407 struct v4l2_subdev_format format = {
1408 .which = V4L2_SUBDEV_FORMAT_TRY,
1409 };
1410 struct v4l2_mbus_framefmt *mf = &format.format;
1411 __u32 pixfmt = pix->pixelformat;
1412 int width, height;
1413 int ret;
1414
1415 dev_geo(icd->parent, "TRY_FMT(pix=0x%x, %ux%u)\n",
1416 pixfmt, pix->width, pix->height);
1417
1418 xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
1419 if (!xlate) {
1420 xlate = icd->current_fmt;
1421 dev_dbg(icd->parent, "Format %x not found, keeping %x\n",
1422 pixfmt, xlate->host_fmt->fourcc);
1423 pixfmt = xlate->host_fmt->fourcc;
1424 pix->pixelformat = pixfmt;
1425 pix->colorspace = icd->colorspace;
1426 }
1427
1428
1429
1430
1431 v4l_bound_align_image(&pix->width, 2, pcdev->max_width, 2,
1432 &pix->height, 4, pcdev->max_height, 2, 0);
1433
1434 width = pix->width;
1435 height = pix->height;
1436
1437
1438 mf->width = pix->width;
1439 mf->height = pix->height;
1440 mf->field = pix->field;
1441 mf->code = xlate->code;
1442 mf->colorspace = pix->colorspace;
1443
1444 ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd),
1445 pad, set_fmt, &pad_cfg, &format);
1446 if (ret < 0)
1447 return ret;
1448
1449 pix->width = mf->width;
1450 pix->height = mf->height;
1451 pix->field = mf->field;
1452 pix->colorspace = mf->colorspace;
1453
1454 switch (pixfmt) {
1455 case V4L2_PIX_FMT_NV12:
1456 case V4L2_PIX_FMT_NV21:
1457 case V4L2_PIX_FMT_NV16:
1458 case V4L2_PIX_FMT_NV61:
1459
1460
1461 if (pix->width < width || pix->height < height) {
1462
1463
1464
1465
1466
1467 mf->width = pcdev->max_width;
1468 mf->height = pcdev->max_height;
1469 ret = v4l2_device_call_until_err(sd->v4l2_dev,
1470 soc_camera_grp_id(icd), pad,
1471 set_fmt, &pad_cfg, &format);
1472 if (ret < 0) {
1473
1474 dev_err(icd->parent,
1475 "FIXME: client try_fmt() = %d\n", ret);
1476 return ret;
1477 }
1478 }
1479
1480 if (mf->width > width)
1481 pix->width = width;
1482 if (mf->height > height)
1483 pix->height = height;
1484
1485 pix->bytesperline = max(pix->bytesperline, pix->width);
1486 pix->bytesperline = min(pix->bytesperline, CEU_CHDW_MAX);
1487 pix->bytesperline &= ~3;
1488 break;
1489
1490 default:
1491
1492 pix->bytesperline = 0;
1493 }
1494
1495 pix->width &= ~3;
1496 pix->height &= ~3;
1497 pix->sizeimage = 0;
1498
1499 dev_geo(icd->parent, "%s(): return %d, fmt 0x%x, %ux%u\n",
1500 __func__, ret, pix->pixelformat, pix->width, pix->height);
1501
1502 return ret;
1503}
1504
1505static int sh_mobile_ceu_set_liveselection(struct soc_camera_device *icd,
1506 struct v4l2_selection *sel)
1507{
1508 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
1509 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1510 struct sh_mobile_ceu_dev *pcdev = ici->priv;
1511 u32 out_width = icd->user_width, out_height = icd->user_height;
1512 int ret;
1513
1514
1515 pcdev->frozen = 1;
1516
1517 ret = wait_for_completion_interruptible(&pcdev->complete);
1518
1519 ret = v4l2_subdev_call(sd, video, s_stream, 0);
1520 if (ret < 0)
1521 dev_warn(icd->parent,
1522 "Client failed to stop the stream: %d\n", ret);
1523 else
1524
1525 sh_mobile_ceu_set_selection(icd, sel);
1526
1527 dev_geo(icd->parent, "Output after crop: %ux%u\n", icd->user_width, icd->user_height);
1528
1529 if (icd->user_width != out_width || icd->user_height != out_height) {
1530 struct v4l2_format f = {
1531 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
1532 .fmt.pix = {
1533 .width = out_width,
1534 .height = out_height,
1535 .pixelformat = icd->current_fmt->host_fmt->fourcc,
1536 .field = pcdev->field,
1537 .colorspace = icd->colorspace,
1538 },
1539 };
1540 ret = sh_mobile_ceu_set_fmt(icd, &f);
1541 if (!ret && (out_width != f.fmt.pix.width ||
1542 out_height != f.fmt.pix.height))
1543 ret = -EINVAL;
1544 if (!ret) {
1545 icd->user_width = out_width & ~3;
1546 icd->user_height = out_height & ~3;
1547 ret = sh_mobile_ceu_set_bus_param(icd);
1548 }
1549 }
1550
1551
1552 pcdev->frozen = 0;
1553 spin_lock_irq(&pcdev->lock);
1554 sh_mobile_ceu_capture(pcdev);
1555 spin_unlock_irq(&pcdev->lock);
1556
1557 ret = v4l2_subdev_call(sd, video, s_stream, 1);
1558 return ret;
1559}
1560
1561static __poll_t sh_mobile_ceu_poll(struct file *file, poll_table *pt)
1562{
1563 struct soc_camera_device *icd = file->private_data;
1564
1565 return vb2_poll(&icd->vb2_vidq, file, pt);
1566}
1567
1568static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
1569 struct v4l2_capability *cap)
1570{
1571 strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
1572 strlcpy(cap->driver, "sh_mobile_ceu", sizeof(cap->driver));
1573 strlcpy(cap->bus_info, "platform:sh_mobile_ceu", sizeof(cap->bus_info));
1574 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1575 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1576
1577 return 0;
1578}
1579
1580static int sh_mobile_ceu_init_videobuf(struct vb2_queue *q,
1581 struct soc_camera_device *icd)
1582{
1583 struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
1584
1585 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1586 q->io_modes = VB2_MMAP | VB2_USERPTR;
1587 q->drv_priv = icd;
1588 q->ops = &sh_mobile_ceu_videobuf_ops;
1589 q->mem_ops = &vb2_dma_contig_memops;
1590 q->buf_struct_size = sizeof(struct sh_mobile_ceu_buffer);
1591 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1592 q->lock = &ici->host_lock;
1593 q->dev = ici->v4l2_dev.dev;
1594
1595 return vb2_queue_init(q);
1596}
1597
1598static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
1599 .owner = THIS_MODULE,
1600 .add = sh_mobile_ceu_add_device,
1601 .remove = sh_mobile_ceu_remove_device,
1602 .clock_start = sh_mobile_ceu_clock_start,
1603 .clock_stop = sh_mobile_ceu_clock_stop,
1604 .get_formats = sh_mobile_ceu_get_formats,
1605 .put_formats = sh_mobile_ceu_put_formats,
1606 .get_selection = sh_mobile_ceu_get_selection,
1607 .set_selection = sh_mobile_ceu_set_selection,
1608 .set_liveselection = sh_mobile_ceu_set_liveselection,
1609 .set_fmt = sh_mobile_ceu_set_fmt,
1610 .try_fmt = sh_mobile_ceu_try_fmt,
1611 .poll = sh_mobile_ceu_poll,
1612 .querycap = sh_mobile_ceu_querycap,
1613 .set_bus_param = sh_mobile_ceu_set_bus_param,
1614 .init_videobuf2 = sh_mobile_ceu_init_videobuf,
1615};
1616
1617struct bus_wait {
1618 struct notifier_block notifier;
1619 struct completion completion;
1620 struct device *dev;
1621};
1622
1623static int bus_notify(struct notifier_block *nb,
1624 unsigned long action, void *data)
1625{
1626 struct device *dev = data;
1627 struct bus_wait *wait = container_of(nb, struct bus_wait, notifier);
1628
1629 if (wait->dev != dev)
1630 return NOTIFY_DONE;
1631
1632 switch (action) {
1633 case BUS_NOTIFY_UNBOUND_DRIVER:
1634
1635 wait_for_completion(&wait->completion);
1636 return NOTIFY_OK;
1637 }
1638 return NOTIFY_DONE;
1639}
1640
1641static int sh_mobile_ceu_probe(struct platform_device *pdev)
1642{
1643 struct sh_mobile_ceu_dev *pcdev;
1644 struct resource *res;
1645 void __iomem *base;
1646 unsigned int irq;
1647 int err;
1648 struct bus_wait wait = {
1649 .completion = COMPLETION_INITIALIZER_ONSTACK(wait.completion),
1650 .notifier.notifier_call = bus_notify,
1651 };
1652
1653 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1654 irq = platform_get_irq(pdev, 0);
1655 if (!res || (int)irq <= 0) {
1656 dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
1657 return -ENODEV;
1658 }
1659
1660 pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
1661 if (!pcdev) {
1662 dev_err(&pdev->dev, "Could not allocate pcdev\n");
1663 return -ENOMEM;
1664 }
1665
1666 INIT_LIST_HEAD(&pcdev->capture);
1667 spin_lock_init(&pcdev->lock);
1668 init_completion(&pcdev->complete);
1669
1670 pcdev->pdata = pdev->dev.platform_data;
1671 if (!pcdev->pdata && !pdev->dev.of_node) {
1672 dev_err(&pdev->dev, "CEU platform data not set.\n");
1673 return -EINVAL;
1674 }
1675
1676
1677 if (pcdev->pdata) {
1678 pcdev->max_width = pcdev->pdata->max_width;
1679 pcdev->max_height = pcdev->pdata->max_height;
1680 pcdev->flags = pcdev->pdata->flags;
1681 }
1682 pcdev->field = V4L2_FIELD_NONE;
1683
1684 if (!pcdev->max_width) {
1685 unsigned int v;
1686 err = of_property_read_u32(pdev->dev.of_node, "renesas,max-width", &v);
1687 if (!err)
1688 pcdev->max_width = v;
1689
1690 if (!pcdev->max_width)
1691 pcdev->max_width = 2560;
1692 }
1693 if (!pcdev->max_height) {
1694 unsigned int v;
1695 err = of_property_read_u32(pdev->dev.of_node, "renesas,max-height", &v);
1696 if (!err)
1697 pcdev->max_height = v;
1698
1699 if (!pcdev->max_height)
1700 pcdev->max_height = 1920;
1701 }
1702
1703 base = devm_ioremap_resource(&pdev->dev, res);
1704 if (IS_ERR(base))
1705 return PTR_ERR(base);
1706
1707 pcdev->irq = irq;
1708 pcdev->base = base;
1709 pcdev->video_limit = 0;
1710
1711 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1712 if (res) {
1713 err = dma_declare_coherent_memory(&pdev->dev, res->start,
1714 res->start,
1715 resource_size(res),
1716 DMA_MEMORY_EXCLUSIVE);
1717 if (err) {
1718 dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
1719 return err;
1720 }
1721
1722 pcdev->video_limit = resource_size(res);
1723 }
1724
1725
1726 err = devm_request_irq(&pdev->dev, pcdev->irq, sh_mobile_ceu_irq,
1727 0, dev_name(&pdev->dev), pcdev);
1728 if (err) {
1729 dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
1730 goto exit_release_mem;
1731 }
1732
1733 pm_suspend_ignore_children(&pdev->dev, true);
1734 pm_runtime_enable(&pdev->dev);
1735 pm_runtime_resume(&pdev->dev);
1736
1737 pcdev->ici.priv = pcdev;
1738 pcdev->ici.v4l2_dev.dev = &pdev->dev;
1739 pcdev->ici.nr = pdev->id;
1740 pcdev->ici.drv_name = dev_name(&pdev->dev);
1741 pcdev->ici.ops = &sh_mobile_ceu_host_ops;
1742 pcdev->ici.capabilities = SOCAM_HOST_CAP_STRIDE;
1743
1744 if (pcdev->pdata && pcdev->pdata->asd_sizes) {
1745 pcdev->ici.asd = pcdev->pdata->asd;
1746 pcdev->ici.asd_sizes = pcdev->pdata->asd_sizes;
1747 }
1748
1749 err = soc_camera_host_register(&pcdev->ici);
1750 if (err)
1751 goto exit_free_clk;
1752
1753 return 0;
1754
1755exit_free_clk:
1756 pm_runtime_disable(&pdev->dev);
1757exit_release_mem:
1758 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1759 dma_release_declared_memory(&pdev->dev);
1760 return err;
1761}
1762
1763static int sh_mobile_ceu_remove(struct platform_device *pdev)
1764{
1765 struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
1766
1767 soc_camera_host_unregister(soc_host);
1768 pm_runtime_disable(&pdev->dev);
1769 if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
1770 dma_release_declared_memory(&pdev->dev);
1771
1772 return 0;
1773}
1774
1775static int sh_mobile_ceu_runtime_nop(struct device *dev)
1776{
1777
1778
1779
1780
1781
1782
1783
1784 return 0;
1785}
1786
1787static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
1788 .runtime_suspend = sh_mobile_ceu_runtime_nop,
1789 .runtime_resume = sh_mobile_ceu_runtime_nop,
1790};
1791
1792static const struct of_device_id sh_mobile_ceu_of_match[] = {
1793 { .compatible = "renesas,sh-mobile-ceu" },
1794 { }
1795};
1796MODULE_DEVICE_TABLE(of, sh_mobile_ceu_of_match);
1797
1798static struct platform_driver sh_mobile_ceu_driver = {
1799 .driver = {
1800 .name = "sh_mobile_ceu",
1801 .pm = &sh_mobile_ceu_dev_pm_ops,
1802 .of_match_table = sh_mobile_ceu_of_match,
1803 },
1804 .probe = sh_mobile_ceu_probe,
1805 .remove = sh_mobile_ceu_remove,
1806};
1807
1808module_platform_driver(sh_mobile_ceu_driver);
1809
1810MODULE_DESCRIPTION("SuperH Mobile CEU driver");
1811MODULE_AUTHOR("Magnus Damm");
1812MODULE_LICENSE("GPL");
1813MODULE_VERSION("0.1.0");
1814MODULE_ALIAS("platform:sh_mobile_ceu");
1815