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