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