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