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