Commit | Line | Data |
---|---|---|
73135e96 VB |
1 | /* |
2 | * SoC-camera host driver for Renesas R-Car VIN unit | |
3 | * | |
4 | * Copyright (C) 2011-2013 Renesas Solutions Corp. | |
5 | * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com> | |
6 | * | |
7 | * Based on V4L2 Driver for SuperH Mobile CEU interface "sh_mobile_ceu_camera.c" | |
8 | * | |
9 | * Copyright (C) 2008 Magnus Damm | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify it | |
12 | * under the terms of the GNU General Public License as published by the | |
13 | * Free Software Foundation; either version 2 of the License, or (at your | |
14 | * option) any later version. | |
15 | */ | |
16 | ||
17 | #include <linux/delay.h> | |
18 | #include <linux/interrupt.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/platform_data/camera-rcar.h> | |
22 | #include <linux/platform_device.h> | |
23 | #include <linux/pm_runtime.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/videodev2.h> | |
26 | ||
27 | #include <media/soc_camera.h> | |
28 | #include <media/soc_mediabus.h> | |
29 | #include <media/v4l2-common.h> | |
30 | #include <media/v4l2-dev.h> | |
31 | #include <media/v4l2-device.h> | |
32 | #include <media/v4l2-mediabus.h> | |
33 | #include <media/v4l2-subdev.h> | |
34 | #include <media/videobuf2-dma-contig.h> | |
35 | ||
36 | #include "soc_scale_crop.h" | |
37 | ||
38 | #define DRV_NAME "rcar_vin" | |
39 | ||
40 | /* Register offsets for R-Car VIN */ | |
41 | #define VNMC_REG 0x00 /* Video n Main Control Register */ | |
42 | #define VNMS_REG 0x04 /* Video n Module Status Register */ | |
43 | #define VNFC_REG 0x08 /* Video n Frame Capture Register */ | |
44 | #define VNSLPRC_REG 0x0C /* Video n Start Line Pre-Clip Register */ | |
45 | #define VNELPRC_REG 0x10 /* Video n End Line Pre-Clip Register */ | |
46 | #define VNSPPRC_REG 0x14 /* Video n Start Pixel Pre-Clip Register */ | |
47 | #define VNEPPRC_REG 0x18 /* Video n End Pixel Pre-Clip Register */ | |
48 | #define VNSLPOC_REG 0x1C /* Video n Start Line Post-Clip Register */ | |
49 | #define VNELPOC_REG 0x20 /* Video n End Line Post-Clip Register */ | |
50 | #define VNSPPOC_REG 0x24 /* Video n Start Pixel Post-Clip Register */ | |
51 | #define VNEPPOC_REG 0x28 /* Video n End Pixel Post-Clip Register */ | |
52 | #define VNIS_REG 0x2C /* Video n Image Stride Register */ | |
53 | #define VNMB_REG(m) (0x30 + ((m) << 2)) /* Video n Memory Base m Register */ | |
54 | #define VNIE_REG 0x40 /* Video n Interrupt Enable Register */ | |
55 | #define VNINTS_REG 0x44 /* Video n Interrupt Status Register */ | |
56 | #define VNSI_REG 0x48 /* Video n Scanline Interrupt Register */ | |
57 | #define VNMTC_REG 0x4C /* Video n Memory Transfer Control Register */ | |
58 | #define VNYS_REG 0x50 /* Video n Y Scale Register */ | |
59 | #define VNXS_REG 0x54 /* Video n X Scale Register */ | |
60 | #define VNDMR_REG 0x58 /* Video n Data Mode Register */ | |
61 | #define VNDMR2_REG 0x5C /* Video n Data Mode Register 2 */ | |
62 | #define VNUVAOF_REG 0x60 /* Video n UV Address Offset Register */ | |
63 | ||
64 | /* Register bit fields for R-Car VIN */ | |
65 | /* Video n Main Control Register bits */ | |
66 | #define VNMC_FOC (1 << 21) | |
67 | #define VNMC_YCAL (1 << 19) | |
68 | #define VNMC_INF_YUV8_BT656 (0 << 16) | |
69 | #define VNMC_INF_YUV8_BT601 (1 << 16) | |
70 | #define VNMC_INF_YUV16 (5 << 16) | |
71 | #define VNMC_VUP (1 << 10) | |
72 | #define VNMC_IM_ODD (0 << 3) | |
73 | #define VNMC_IM_ODD_EVEN (1 << 3) | |
74 | #define VNMC_IM_EVEN (2 << 3) | |
75 | #define VNMC_IM_FULL (3 << 3) | |
76 | #define VNMC_BPS (1 << 1) | |
77 | #define VNMC_ME (1 << 0) | |
78 | ||
79 | /* Video n Module Status Register bits */ | |
80 | #define VNMS_FBS_MASK (3 << 3) | |
81 | #define VNMS_FBS_SHIFT 3 | |
82 | #define VNMS_AV (1 << 1) | |
83 | #define VNMS_CA (1 << 0) | |
84 | ||
85 | /* Video n Frame Capture Register bits */ | |
86 | #define VNFC_C_FRAME (1 << 1) | |
87 | #define VNFC_S_FRAME (1 << 0) | |
88 | ||
89 | /* Video n Interrupt Enable Register bits */ | |
90 | #define VNIE_FIE (1 << 4) | |
91 | #define VNIE_EFE (1 << 1) | |
92 | ||
93 | /* Video n Data Mode Register bits */ | |
94 | #define VNDMR_EXRGB (1 << 8) | |
95 | #define VNDMR_BPSM (1 << 4) | |
96 | #define VNDMR_DTMD_YCSEP (1 << 1) | |
97 | #define VNDMR_DTMD_ARGB1555 (1 << 0) | |
98 | ||
99 | /* Video n Data Mode Register 2 bits */ | |
100 | #define VNDMR2_VPS (1 << 30) | |
101 | #define VNDMR2_HPS (1 << 29) | |
102 | #define VNDMR2_FTEV (1 << 17) | |
103 | ||
104 | #define VIN_MAX_WIDTH 2048 | |
105 | #define VIN_MAX_HEIGHT 2048 | |
106 | ||
107 | enum chip_id { | |
ca0c9f17 | 108 | RCAR_H2, |
73135e96 VB |
109 | RCAR_H1, |
110 | RCAR_M1, | |
111 | RCAR_E1, | |
112 | }; | |
113 | ||
114 | enum rcar_vin_state { | |
115 | STOPPED = 0, | |
116 | RUNNING, | |
117 | STOPPING, | |
118 | }; | |
119 | ||
120 | struct rcar_vin_priv { | |
121 | void __iomem *base; | |
122 | spinlock_t lock; | |
123 | int sequence; | |
124 | /* State of the VIN module in capturing mode */ | |
125 | enum rcar_vin_state state; | |
126 | struct rcar_vin_platform_data *pdata; | |
127 | struct soc_camera_host ici; | |
128 | struct list_head capture; | |
129 | #define MAX_BUFFER_NUM 3 | |
130 | struct vb2_buffer *queue_buf[MAX_BUFFER_NUM]; | |
131 | struct vb2_alloc_ctx *alloc_ctx; | |
132 | enum v4l2_field field; | |
133 | unsigned int vb_count; | |
134 | unsigned int nr_hw_slots; | |
135 | bool request_to_stop; | |
136 | struct completion capture_stop; | |
137 | enum chip_id chip; | |
138 | }; | |
139 | ||
140 | #define is_continuous_transfer(priv) (priv->vb_count > MAX_BUFFER_NUM) | |
141 | ||
142 | struct rcar_vin_buffer { | |
143 | struct vb2_buffer vb; | |
144 | struct list_head list; | |
145 | }; | |
146 | ||
147 | #define to_buf_list(vb2_buffer) (&container_of(vb2_buffer, \ | |
148 | struct rcar_vin_buffer, \ | |
149 | vb)->list) | |
150 | ||
151 | struct rcar_vin_cam { | |
152 | /* VIN offsets within the camera output, before the VIN scaler */ | |
153 | unsigned int vin_left; | |
154 | unsigned int vin_top; | |
155 | /* Client output, as seen by the VIN */ | |
156 | unsigned int width; | |
157 | unsigned int height; | |
158 | /* | |
159 | * User window from S_CROP / G_CROP, produced by client cropping and | |
160 | * scaling, VIN scaling and VIN cropping, mapped back onto the client | |
161 | * input window | |
162 | */ | |
163 | struct v4l2_rect subrect; | |
164 | /* Camera cropping rectangle */ | |
165 | struct v4l2_rect rect; | |
166 | const struct soc_mbus_pixelfmt *extra_fmt; | |
167 | }; | |
168 | ||
169 | /* | |
170 | * .queue_setup() is called to check whether the driver can accept the requested | |
171 | * number of buffers and to fill in plane sizes for the current frame format if | |
172 | * required | |
173 | */ | |
174 | static int rcar_vin_videobuf_setup(struct vb2_queue *vq, | |
175 | const struct v4l2_format *fmt, | |
176 | unsigned int *count, | |
177 | unsigned int *num_planes, | |
178 | unsigned int sizes[], void *alloc_ctxs[]) | |
179 | { | |
180 | struct soc_camera_device *icd = soc_camera_from_vb2q(vq); | |
181 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); | |
182 | struct rcar_vin_priv *priv = ici->priv; | |
183 | ||
184 | if (fmt) { | |
185 | const struct soc_camera_format_xlate *xlate; | |
186 | unsigned int bytes_per_line; | |
187 | int ret; | |
188 | ||
189 | xlate = soc_camera_xlate_by_fourcc(icd, | |
190 | fmt->fmt.pix.pixelformat); | |
191 | if (!xlate) | |
192 | return -EINVAL; | |
193 | ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width, | |
194 | xlate->host_fmt); | |
195 | if (ret < 0) | |
196 | return ret; | |
197 | ||
198 | bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret); | |
199 | ||
200 | ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line, | |
201 | fmt->fmt.pix.height); | |
202 | if (ret < 0) | |
203 | return ret; | |
204 | ||
205 | sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret); | |
206 | } else { | |
207 | /* Called from VIDIOC_REQBUFS or in compatibility mode */ | |
208 | sizes[0] = icd->sizeimage; | |
209 | } | |
210 | ||
211 | alloc_ctxs[0] = priv->alloc_ctx; | |
212 | ||
213 | if (!vq->num_buffers) | |
214 | priv->sequence = 0; | |
215 | ||
216 | if (!*count) | |
217 | *count = 2; | |
218 | priv->vb_count = *count; | |
219 | ||
220 | *num_planes = 1; | |
221 | ||
222 | /* Number of hardware slots */ | |
223 | if (is_continuous_transfer(priv)) | |
224 | priv->nr_hw_slots = MAX_BUFFER_NUM; | |
225 | else | |
226 | priv->nr_hw_slots = 1; | |
227 | ||
228 | dev_dbg(icd->parent, "count=%d, size=%u\n", *count, sizes[0]); | |
229 | ||
230 | return 0; | |
231 | } | |
232 | ||
233 | static int rcar_vin_setup(struct rcar_vin_priv *priv) | |
234 | { | |
235 | struct soc_camera_device *icd = priv->ici.icd; | |
236 | struct rcar_vin_cam *cam = icd->host_priv; | |
237 | u32 vnmc, dmr, interrupts; | |
238 | bool progressive = false, output_is_yuv = false; | |
239 | ||
240 | switch (priv->field) { | |
241 | case V4L2_FIELD_TOP: | |
242 | vnmc = VNMC_IM_ODD; | |
243 | break; | |
244 | case V4L2_FIELD_BOTTOM: | |
245 | vnmc = VNMC_IM_EVEN; | |
246 | break; | |
247 | case V4L2_FIELD_INTERLACED: | |
248 | case V4L2_FIELD_INTERLACED_TB: | |
249 | vnmc = VNMC_IM_FULL; | |
250 | break; | |
251 | case V4L2_FIELD_INTERLACED_BT: | |
252 | vnmc = VNMC_IM_FULL | VNMC_FOC; | |
253 | break; | |
254 | case V4L2_FIELD_NONE: | |
255 | if (is_continuous_transfer(priv)) { | |
256 | vnmc = VNMC_IM_ODD_EVEN; | |
257 | progressive = true; | |
258 | } else { | |
259 | vnmc = VNMC_IM_ODD; | |
260 | } | |
261 | break; | |
262 | default: | |
263 | vnmc = VNMC_IM_ODD; | |
264 | break; | |
265 | } | |
266 | ||
267 | /* input interface */ | |
268 | switch (icd->current_fmt->code) { | |
269 | case V4L2_MBUS_FMT_YUYV8_1X16: | |
270 | /* BT.601/BT.1358 16bit YCbCr422 */ | |
271 | vnmc |= VNMC_INF_YUV16; | |
272 | break; | |
273 | case V4L2_MBUS_FMT_YUYV8_2X8: | |
274 | /* BT.656 8bit YCbCr422 or BT.601 8bit YCbCr422 */ | |
275 | vnmc |= priv->pdata->flags & RCAR_VIN_BT656 ? | |
276 | VNMC_INF_YUV8_BT656 : VNMC_INF_YUV8_BT601; | |
277 | default: | |
278 | break; | |
279 | } | |
280 | ||
281 | /* output format */ | |
282 | switch (icd->current_fmt->host_fmt->fourcc) { | |
283 | case V4L2_PIX_FMT_NV16: | |
284 | iowrite32(ALIGN(cam->width * cam->height, 0x80), | |
285 | priv->base + VNUVAOF_REG); | |
286 | dmr = VNDMR_DTMD_YCSEP; | |
287 | output_is_yuv = true; | |
288 | break; | |
289 | case V4L2_PIX_FMT_YUYV: | |
290 | dmr = VNDMR_BPSM; | |
291 | output_is_yuv = true; | |
292 | break; | |
293 | case V4L2_PIX_FMT_UYVY: | |
294 | dmr = 0; | |
295 | output_is_yuv = true; | |
296 | break; | |
297 | case V4L2_PIX_FMT_RGB555X: | |
298 | dmr = VNDMR_DTMD_ARGB1555; | |
299 | break; | |
300 | case V4L2_PIX_FMT_RGB565: | |
301 | dmr = 0; | |
302 | break; | |
303 | case V4L2_PIX_FMT_RGB32: | |
ca0c9f17 VB |
304 | if (priv->chip == RCAR_H2 || priv->chip == RCAR_H1 || |
305 | priv->chip == RCAR_E1) { | |
73135e96 VB |
306 | dmr = VNDMR_EXRGB; |
307 | break; | |
308 | } | |
309 | default: | |
310 | dev_warn(icd->parent, "Invalid fourcc format (0x%x)\n", | |
311 | icd->current_fmt->host_fmt->fourcc); | |
312 | return -EINVAL; | |
313 | } | |
314 | ||
315 | /* Always update on field change */ | |
316 | vnmc |= VNMC_VUP; | |
317 | ||
318 | /* If input and output use the same colorspace, use bypass mode */ | |
319 | if (output_is_yuv) | |
320 | vnmc |= VNMC_BPS; | |
321 | ||
322 | /* progressive or interlaced mode */ | |
323 | interrupts = progressive ? VNIE_FIE | VNIE_EFE : VNIE_EFE; | |
324 | ||
325 | /* ack interrupts */ | |
326 | iowrite32(interrupts, priv->base + VNINTS_REG); | |
327 | /* enable interrupts */ | |
328 | iowrite32(interrupts, priv->base + VNIE_REG); | |
329 | /* start capturing */ | |
330 | iowrite32(dmr, priv->base + VNDMR_REG); | |
331 | iowrite32(vnmc | VNMC_ME, priv->base + VNMC_REG); | |
332 | ||
333 | return 0; | |
334 | } | |
335 | ||
336 | static void rcar_vin_capture(struct rcar_vin_priv *priv) | |
337 | { | |
338 | if (is_continuous_transfer(priv)) | |
339 | /* Continuous Frame Capture Mode */ | |
340 | iowrite32(VNFC_C_FRAME, priv->base + VNFC_REG); | |
341 | else | |
342 | /* Single Frame Capture Mode */ | |
343 | iowrite32(VNFC_S_FRAME, priv->base + VNFC_REG); | |
344 | } | |
345 | ||
346 | static void rcar_vin_request_capture_stop(struct rcar_vin_priv *priv) | |
347 | { | |
348 | priv->state = STOPPING; | |
349 | ||
350 | /* set continuous & single transfer off */ | |
351 | iowrite32(0, priv->base + VNFC_REG); | |
352 | /* disable capture (release DMA buffer), reset */ | |
353 | iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME, | |
354 | priv->base + VNMC_REG); | |
355 | ||
356 | /* update the status if stopped already */ | |
357 | if (!(ioread32(priv->base + VNMS_REG) & VNMS_CA)) | |
358 | priv->state = STOPPED; | |
359 | } | |
360 | ||
361 | static int rcar_vin_get_free_hw_slot(struct rcar_vin_priv *priv) | |
362 | { | |
363 | int slot; | |
364 | ||
365 | for (slot = 0; slot < priv->nr_hw_slots; slot++) | |
366 | if (priv->queue_buf[slot] == NULL) | |
367 | return slot; | |
368 | ||
369 | return -1; | |
370 | } | |
371 | ||
372 | static int rcar_vin_hw_ready(struct rcar_vin_priv *priv) | |
373 | { | |
374 | /* Ensure all HW slots are filled */ | |
375 | return rcar_vin_get_free_hw_slot(priv) < 0 ? 1 : 0; | |
376 | } | |
377 | ||
378 | /* Moves a buffer from the queue to the HW slots */ | |
379 | static int rcar_vin_fill_hw_slot(struct rcar_vin_priv *priv) | |
380 | { | |
381 | struct vb2_buffer *vb; | |
382 | dma_addr_t phys_addr_top; | |
383 | int slot; | |
384 | ||
385 | if (list_empty(&priv->capture)) | |
386 | return 0; | |
387 | ||
388 | /* Find a free HW slot */ | |
389 | slot = rcar_vin_get_free_hw_slot(priv); | |
390 | if (slot < 0) | |
391 | return 0; | |
392 | ||
393 | vb = &list_entry(priv->capture.next, struct rcar_vin_buffer, list)->vb; | |
394 | list_del_init(to_buf_list(vb)); | |
395 | priv->queue_buf[slot] = vb; | |
396 | phys_addr_top = vb2_dma_contig_plane_dma_addr(vb, 0); | |
397 | iowrite32(phys_addr_top, priv->base + VNMB_REG(slot)); | |
398 | ||
399 | return 1; | |
400 | } | |
401 | ||
402 | static void rcar_vin_videobuf_queue(struct vb2_buffer *vb) | |
403 | { | |
404 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); | |
405 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); | |
406 | struct rcar_vin_priv *priv = ici->priv; | |
407 | unsigned long size; | |
408 | ||
409 | size = icd->sizeimage; | |
410 | ||
411 | if (vb2_plane_size(vb, 0) < size) { | |
412 | dev_err(icd->parent, "Buffer #%d too small (%lu < %lu)\n", | |
413 | vb->v4l2_buf.index, vb2_plane_size(vb, 0), size); | |
414 | goto error; | |
415 | } | |
416 | ||
417 | vb2_set_plane_payload(vb, 0, size); | |
418 | ||
419 | dev_dbg(icd->parent, "%s (vb=0x%p) 0x%p %lu\n", __func__, | |
420 | vb, vb2_plane_vaddr(vb, 0), vb2_get_plane_payload(vb, 0)); | |
421 | ||
422 | spin_lock_irq(&priv->lock); | |
423 | ||
424 | list_add_tail(to_buf_list(vb), &priv->capture); | |
425 | rcar_vin_fill_hw_slot(priv); | |
426 | ||
427 | /* If we weren't running, and have enough buffers, start capturing! */ | |
428 | if (priv->state != RUNNING && rcar_vin_hw_ready(priv)) { | |
429 | if (rcar_vin_setup(priv)) { | |
430 | /* Submit error */ | |
431 | list_del_init(to_buf_list(vb)); | |
432 | spin_unlock_irq(&priv->lock); | |
433 | goto error; | |
434 | } | |
435 | priv->request_to_stop = false; | |
436 | init_completion(&priv->capture_stop); | |
437 | priv->state = RUNNING; | |
438 | rcar_vin_capture(priv); | |
439 | } | |
440 | ||
441 | spin_unlock_irq(&priv->lock); | |
442 | ||
443 | return; | |
444 | ||
445 | error: | |
446 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); | |
447 | } | |
448 | ||
449 | static void rcar_vin_videobuf_release(struct vb2_buffer *vb) | |
450 | { | |
451 | struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue); | |
452 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); | |
453 | struct rcar_vin_priv *priv = ici->priv; | |
454 | unsigned int i; | |
455 | int buf_in_use = 0; | |
456 | ||
457 | spin_lock_irq(&priv->lock); | |
458 | ||
459 | /* Is the buffer in use by the VIN hardware? */ | |
460 | for (i = 0; i < MAX_BUFFER_NUM; i++) { | |
461 | if (priv->queue_buf[i] == vb) { | |
462 | buf_in_use = 1; | |
463 | break; | |
464 | } | |
465 | } | |
466 | ||
467 | if (buf_in_use) { | |
468 | while (priv->state != STOPPED) { | |
469 | ||
470 | /* issue stop if running */ | |
471 | if (priv->state == RUNNING) | |
472 | rcar_vin_request_capture_stop(priv); | |
473 | ||
474 | /* wait until capturing has been stopped */ | |
475 | if (priv->state == STOPPING) { | |
476 | priv->request_to_stop = true; | |
477 | spin_unlock_irq(&priv->lock); | |
478 | wait_for_completion(&priv->capture_stop); | |
479 | spin_lock_irq(&priv->lock); | |
480 | } | |
481 | } | |
482 | /* | |
483 | * Capturing has now stopped. The buffer we have been asked | |
484 | * to release could be any of the current buffers in use, so | |
485 | * release all buffers that are in use by HW | |
486 | */ | |
487 | for (i = 0; i < MAX_BUFFER_NUM; i++) { | |
488 | if (priv->queue_buf[i]) { | |
489 | vb2_buffer_done(priv->queue_buf[i], | |
490 | VB2_BUF_STATE_ERROR); | |
491 | priv->queue_buf[i] = NULL; | |
492 | } | |
493 | } | |
494 | } else { | |
495 | list_del_init(to_buf_list(vb)); | |
496 | } | |
497 | ||
498 | spin_unlock_irq(&priv->lock); | |
499 | } | |
500 | ||
501 | static int rcar_vin_videobuf_init(struct vb2_buffer *vb) | |
502 | { | |
503 | INIT_LIST_HEAD(to_buf_list(vb)); | |
504 | return 0; | |
505 | } | |
506 | ||
507 | static int rcar_vin_stop_streaming(struct vb2_queue *vq) | |
508 | { | |
509 | struct soc_camera_device *icd = soc_camera_from_vb2q(vq); | |
510 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); | |
511 | struct rcar_vin_priv *priv = ici->priv; | |
512 | struct list_head *buf_head, *tmp; | |
513 | ||
514 | spin_lock_irq(&priv->lock); | |
515 | list_for_each_safe(buf_head, tmp, &priv->capture) | |
516 | list_del_init(buf_head); | |
517 | spin_unlock_irq(&priv->lock); | |
518 | ||
519 | return 0; | |
520 | } | |
521 | ||
522 | static struct vb2_ops rcar_vin_vb2_ops = { | |
523 | .queue_setup = rcar_vin_videobuf_setup, | |
524 | .buf_init = rcar_vin_videobuf_init, | |
525 | .buf_cleanup = rcar_vin_videobuf_release, | |
526 | .buf_queue = rcar_vin_videobuf_queue, | |
527 | .stop_streaming = rcar_vin_stop_streaming, | |
528 | .wait_prepare = soc_camera_unlock, | |
529 | .wait_finish = soc_camera_lock, | |
530 | }; | |
531 | ||
532 | static irqreturn_t rcar_vin_irq(int irq, void *data) | |
533 | { | |
534 | struct rcar_vin_priv *priv = data; | |
535 | u32 int_status; | |
536 | bool can_run = false, hw_stopped; | |
537 | int slot; | |
538 | unsigned int handled = 0; | |
539 | ||
540 | spin_lock(&priv->lock); | |
541 | ||
542 | int_status = ioread32(priv->base + VNINTS_REG); | |
543 | if (!int_status) | |
544 | goto done; | |
545 | /* ack interrupts */ | |
546 | iowrite32(int_status, priv->base + VNINTS_REG); | |
547 | handled = 1; | |
548 | ||
549 | /* nothing to do if capture status is 'STOPPED' */ | |
550 | if (priv->state == STOPPED) | |
551 | goto done; | |
552 | ||
553 | hw_stopped = !(ioread32(priv->base + VNMS_REG) & VNMS_CA); | |
554 | ||
555 | if (!priv->request_to_stop) { | |
556 | if (is_continuous_transfer(priv)) | |
557 | slot = (ioread32(priv->base + VNMS_REG) & | |
558 | VNMS_FBS_MASK) >> VNMS_FBS_SHIFT; | |
559 | else | |
560 | slot = 0; | |
561 | ||
562 | priv->queue_buf[slot]->v4l2_buf.field = priv->field; | |
563 | priv->queue_buf[slot]->v4l2_buf.sequence = priv->sequence++; | |
564 | do_gettimeofday(&priv->queue_buf[slot]->v4l2_buf.timestamp); | |
565 | vb2_buffer_done(priv->queue_buf[slot], VB2_BUF_STATE_DONE); | |
566 | priv->queue_buf[slot] = NULL; | |
567 | ||
568 | if (priv->state != STOPPING) | |
569 | can_run = rcar_vin_fill_hw_slot(priv); | |
570 | ||
571 | if (hw_stopped || !can_run) { | |
572 | priv->state = STOPPED; | |
573 | } else if (is_continuous_transfer(priv) && | |
574 | list_empty(&priv->capture) && | |
575 | priv->state == RUNNING) { | |
576 | /* | |
577 | * The continuous capturing requires an explicit stop | |
578 | * operation when there is no buffer to be set into | |
579 | * the VnMBm registers. | |
580 | */ | |
581 | rcar_vin_request_capture_stop(priv); | |
582 | } else { | |
583 | rcar_vin_capture(priv); | |
584 | } | |
585 | ||
586 | } else if (hw_stopped) { | |
587 | priv->state = STOPPED; | |
588 | priv->request_to_stop = false; | |
589 | complete(&priv->capture_stop); | |
590 | } | |
591 | ||
592 | done: | |
593 | spin_unlock(&priv->lock); | |
594 | ||
595 | return IRQ_RETVAL(handled); | |
596 | } | |
597 | ||
598 | static int rcar_vin_add_device(struct soc_camera_device *icd) | |
599 | { | |
600 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); | |
601 | struct rcar_vin_priv *priv = ici->priv; | |
602 | int i; | |
603 | ||
604 | for (i = 0; i < MAX_BUFFER_NUM; i++) | |
605 | priv->queue_buf[i] = NULL; | |
606 | ||
607 | pm_runtime_get_sync(ici->v4l2_dev.dev); | |
608 | ||
609 | dev_dbg(icd->parent, "R-Car VIN driver attached to camera %d\n", | |
610 | icd->devnum); | |
611 | ||
612 | return 0; | |
613 | } | |
614 | ||
615 | static void rcar_vin_remove_device(struct soc_camera_device *icd) | |
616 | { | |
617 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); | |
618 | struct rcar_vin_priv *priv = ici->priv; | |
619 | struct vb2_buffer *vb; | |
620 | int i; | |
621 | ||
622 | /* disable capture, disable interrupts */ | |
623 | iowrite32(ioread32(priv->base + VNMC_REG) & ~VNMC_ME, | |
624 | priv->base + VNMC_REG); | |
625 | iowrite32(0, priv->base + VNIE_REG); | |
626 | ||
627 | priv->state = STOPPED; | |
628 | priv->request_to_stop = false; | |
629 | ||
630 | /* make sure active buffer is cancelled */ | |
631 | spin_lock_irq(&priv->lock); | |
632 | for (i = 0; i < MAX_BUFFER_NUM; i++) { | |
633 | vb = priv->queue_buf[i]; | |
634 | if (vb) { | |
635 | list_del_init(to_buf_list(vb)); | |
636 | vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); | |
637 | } | |
638 | } | |
639 | spin_unlock_irq(&priv->lock); | |
640 | ||
641 | pm_runtime_put(ici->v4l2_dev.dev); | |
642 | ||
643 | dev_dbg(icd->parent, "R-Car VIN driver detached from camera %d\n", | |
644 | icd->devnum); | |
645 | } | |
646 | ||
647 | /* Called with .host_lock held */ | |
648 | static int rcar_vin_clock_start(struct soc_camera_host *ici) | |
649 | { | |
650 | /* VIN does not have "mclk" */ | |
651 | return 0; | |
652 | } | |
653 | ||
654 | /* Called with .host_lock held */ | |
655 | static void rcar_vin_clock_stop(struct soc_camera_host *ici) | |
656 | { | |
657 | /* VIN does not have "mclk" */ | |
658 | } | |
659 | ||
660 | /* rect is guaranteed to not exceed the scaled camera rectangle */ | |
661 | static int rcar_vin_set_rect(struct soc_camera_device *icd) | |
662 | { | |
663 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); | |
664 | struct rcar_vin_cam *cam = icd->host_priv; | |
665 | struct rcar_vin_priv *priv = ici->priv; | |
666 | unsigned int left_offset, top_offset; | |
667 | unsigned char dsize = 0; | |
668 | struct v4l2_rect *cam_subrect = &cam->subrect; | |
669 | ||
670 | dev_dbg(icd->parent, "Crop %ux%u@%u:%u\n", | |
671 | icd->user_width, icd->user_height, cam->vin_left, cam->vin_top); | |
672 | ||
673 | left_offset = cam->vin_left; | |
674 | top_offset = cam->vin_top; | |
675 | ||
676 | if (icd->current_fmt->host_fmt->fourcc == V4L2_PIX_FMT_RGB32 && | |
677 | priv->chip == RCAR_E1) | |
678 | dsize = 1; | |
679 | ||
680 | dev_dbg(icd->parent, "Cam %ux%u@%u:%u\n", | |
681 | cam->width, cam->height, cam->vin_left, cam->vin_top); | |
682 | dev_dbg(icd->parent, "Cam subrect %ux%u@%u:%u\n", | |
683 | cam_subrect->width, cam_subrect->height, | |
684 | cam_subrect->left, cam_subrect->top); | |
685 | ||
686 | /* Set Start/End Pixel/Line Pre-Clip */ | |
687 | iowrite32(left_offset << dsize, priv->base + VNSPPRC_REG); | |
688 | iowrite32((left_offset + cam->width - 1) << dsize, | |
689 | priv->base + VNEPPRC_REG); | |
690 | switch (priv->field) { | |
691 | case V4L2_FIELD_INTERLACED: | |
692 | case V4L2_FIELD_INTERLACED_TB: | |
693 | case V4L2_FIELD_INTERLACED_BT: | |
694 | iowrite32(top_offset / 2, priv->base + VNSLPRC_REG); | |
695 | iowrite32((top_offset + cam->height) / 2 - 1, | |
696 | priv->base + VNELPRC_REG); | |
697 | break; | |
698 | default: | |
699 | iowrite32(top_offset, priv->base + VNSLPRC_REG); | |
700 | iowrite32(top_offset + cam->height - 1, | |
701 | priv->base + VNELPRC_REG); | |
702 | break; | |
703 | } | |
704 | ||
705 | /* Set Start/End Pixel/Line Post-Clip */ | |
706 | iowrite32(0, priv->base + VNSPPOC_REG); | |
707 | iowrite32(0, priv->base + VNSLPOC_REG); | |
708 | iowrite32((cam_subrect->width - 1) << dsize, priv->base + VNEPPOC_REG); | |
709 | switch (priv->field) { | |
710 | case V4L2_FIELD_INTERLACED: | |
711 | case V4L2_FIELD_INTERLACED_TB: | |
712 | case V4L2_FIELD_INTERLACED_BT: | |
713 | iowrite32(cam_subrect->height / 2 - 1, | |
714 | priv->base + VNELPOC_REG); | |
715 | break; | |
716 | default: | |
717 | iowrite32(cam_subrect->height - 1, priv->base + VNELPOC_REG); | |
718 | break; | |
719 | } | |
720 | ||
721 | iowrite32(ALIGN(cam->width, 0x10), priv->base + VNIS_REG); | |
722 | ||
723 | return 0; | |
724 | } | |
725 | ||
726 | static void capture_stop_preserve(struct rcar_vin_priv *priv, u32 *vnmc) | |
727 | { | |
728 | *vnmc = ioread32(priv->base + VNMC_REG); | |
729 | /* module disable */ | |
730 | iowrite32(*vnmc & ~VNMC_ME, priv->base + VNMC_REG); | |
731 | } | |
732 | ||
733 | static void capture_restore(struct rcar_vin_priv *priv, u32 vnmc) | |
734 | { | |
735 | unsigned long timeout = jiffies + 10 * HZ; | |
736 | ||
737 | /* | |
738 | * Wait until the end of the current frame. It can take a long time, | |
739 | * but if it has been aborted by a MRST1 reset, it should exit sooner. | |
740 | */ | |
741 | while ((ioread32(priv->base + VNMS_REG) & VNMS_AV) && | |
742 | time_before(jiffies, timeout)) | |
743 | msleep(1); | |
744 | ||
745 | if (time_after(jiffies, timeout)) { | |
746 | dev_err(priv->ici.v4l2_dev.dev, | |
747 | "Timeout waiting for frame end! Interface problem?\n"); | |
748 | return; | |
749 | } | |
750 | ||
751 | iowrite32(vnmc, priv->base + VNMC_REG); | |
752 | } | |
753 | ||
754 | #define VIN_MBUS_FLAGS (V4L2_MBUS_MASTER | \ | |
755 | V4L2_MBUS_PCLK_SAMPLE_RISING | \ | |
756 | V4L2_MBUS_HSYNC_ACTIVE_HIGH | \ | |
757 | V4L2_MBUS_HSYNC_ACTIVE_LOW | \ | |
758 | V4L2_MBUS_VSYNC_ACTIVE_HIGH | \ | |
759 | V4L2_MBUS_VSYNC_ACTIVE_LOW | \ | |
760 | V4L2_MBUS_DATA_ACTIVE_HIGH) | |
761 | ||
762 | static int rcar_vin_set_bus_param(struct soc_camera_device *icd) | |
763 | { | |
764 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); | |
765 | struct rcar_vin_priv *priv = ici->priv; | |
766 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
767 | struct v4l2_mbus_config cfg; | |
768 | unsigned long common_flags; | |
769 | u32 vnmc; | |
770 | u32 val; | |
771 | int ret; | |
772 | ||
773 | capture_stop_preserve(priv, &vnmc); | |
774 | ||
775 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); | |
776 | if (!ret) { | |
777 | common_flags = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS); | |
778 | if (!common_flags) { | |
779 | dev_warn(icd->parent, | |
780 | "MBUS flags incompatible: camera 0x%x, host 0x%x\n", | |
781 | cfg.flags, VIN_MBUS_FLAGS); | |
782 | return -EINVAL; | |
783 | } | |
784 | } else if (ret != -ENOIOCTLCMD) { | |
785 | return ret; | |
786 | } else { | |
787 | common_flags = VIN_MBUS_FLAGS; | |
788 | } | |
789 | ||
790 | /* Make choises, based on platform preferences */ | |
791 | if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) && | |
792 | (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) { | |
793 | if (priv->pdata->flags & RCAR_VIN_HSYNC_ACTIVE_LOW) | |
794 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH; | |
795 | else | |
796 | common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW; | |
797 | } | |
798 | ||
799 | if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) && | |
800 | (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) { | |
801 | if (priv->pdata->flags & RCAR_VIN_VSYNC_ACTIVE_LOW) | |
802 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH; | |
803 | else | |
804 | common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW; | |
805 | } | |
806 | ||
807 | cfg.flags = common_flags; | |
808 | ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg); | |
809 | if (ret < 0 && ret != -ENOIOCTLCMD) | |
810 | return ret; | |
811 | ||
812 | val = priv->field == V4L2_FIELD_NONE ? VNDMR2_FTEV : 0; | |
813 | if (!(common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) | |
814 | val |= VNDMR2_VPS; | |
815 | if (!(common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) | |
816 | val |= VNDMR2_HPS; | |
817 | iowrite32(val, priv->base + VNDMR2_REG); | |
818 | ||
819 | ret = rcar_vin_set_rect(icd); | |
820 | if (ret < 0) | |
821 | return ret; | |
822 | ||
823 | capture_restore(priv, vnmc); | |
824 | ||
825 | return 0; | |
826 | } | |
827 | ||
828 | static int rcar_vin_try_bus_param(struct soc_camera_device *icd, | |
829 | unsigned char buswidth) | |
830 | { | |
831 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
832 | struct v4l2_mbus_config cfg; | |
833 | int ret; | |
834 | ||
835 | ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg); | |
836 | if (ret == -ENOIOCTLCMD) | |
837 | return 0; | |
838 | else if (ret) | |
839 | return ret; | |
840 | ||
841 | if (buswidth > 24) | |
842 | return -EINVAL; | |
843 | ||
844 | /* check is there common mbus flags */ | |
845 | ret = soc_mbus_config_compatible(&cfg, VIN_MBUS_FLAGS); | |
846 | if (ret) | |
847 | return 0; | |
848 | ||
849 | dev_warn(icd->parent, | |
850 | "MBUS flags incompatible: camera 0x%x, host 0x%x\n", | |
851 | cfg.flags, VIN_MBUS_FLAGS); | |
852 | ||
853 | return -EINVAL; | |
854 | } | |
855 | ||
856 | static bool rcar_vin_packing_supported(const struct soc_mbus_pixelfmt *fmt) | |
857 | { | |
858 | return fmt->packing == SOC_MBUS_PACKING_NONE || | |
859 | (fmt->bits_per_sample > 8 && | |
860 | fmt->packing == SOC_MBUS_PACKING_EXTEND16); | |
861 | } | |
862 | ||
863 | static const struct soc_mbus_pixelfmt rcar_vin_formats[] = { | |
864 | { | |
865 | .fourcc = V4L2_PIX_FMT_NV16, | |
866 | .name = "NV16", | |
867 | .bits_per_sample = 8, | |
868 | .packing = SOC_MBUS_PACKING_2X8_PADHI, | |
869 | .order = SOC_MBUS_ORDER_LE, | |
870 | .layout = SOC_MBUS_LAYOUT_PLANAR_Y_C, | |
871 | }, | |
872 | { | |
873 | .fourcc = V4L2_PIX_FMT_UYVY, | |
874 | .name = "UYVY", | |
875 | .bits_per_sample = 16, | |
876 | .packing = SOC_MBUS_PACKING_NONE, | |
877 | .order = SOC_MBUS_ORDER_LE, | |
878 | .layout = SOC_MBUS_LAYOUT_PACKED, | |
879 | }, | |
880 | { | |
881 | .fourcc = V4L2_PIX_FMT_RGB565, | |
882 | .name = "RGB565", | |
883 | .bits_per_sample = 16, | |
884 | .packing = SOC_MBUS_PACKING_NONE, | |
885 | .order = SOC_MBUS_ORDER_LE, | |
886 | .layout = SOC_MBUS_LAYOUT_PACKED, | |
887 | }, | |
888 | { | |
889 | .fourcc = V4L2_PIX_FMT_RGB555X, | |
890 | .name = "ARGB1555", | |
891 | .bits_per_sample = 16, | |
892 | .packing = SOC_MBUS_PACKING_NONE, | |
893 | .order = SOC_MBUS_ORDER_LE, | |
894 | .layout = SOC_MBUS_LAYOUT_PACKED, | |
895 | }, | |
896 | { | |
897 | .fourcc = V4L2_PIX_FMT_RGB32, | |
898 | .name = "RGB888", | |
899 | .bits_per_sample = 32, | |
900 | .packing = SOC_MBUS_PACKING_NONE, | |
901 | .order = SOC_MBUS_ORDER_LE, | |
902 | .layout = SOC_MBUS_LAYOUT_PACKED, | |
903 | }, | |
904 | }; | |
905 | ||
906 | static int rcar_vin_get_formats(struct soc_camera_device *icd, unsigned int idx, | |
907 | struct soc_camera_format_xlate *xlate) | |
908 | { | |
909 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
910 | struct device *dev = icd->parent; | |
911 | int ret, k, n; | |
912 | int formats = 0; | |
913 | struct rcar_vin_cam *cam; | |
914 | enum v4l2_mbus_pixelcode code; | |
915 | const struct soc_mbus_pixelfmt *fmt; | |
916 | ||
917 | ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code); | |
918 | if (ret < 0) | |
919 | return 0; | |
920 | ||
921 | fmt = soc_mbus_get_fmtdesc(code); | |
922 | if (!fmt) { | |
923 | dev_warn(dev, "unsupported format code #%u: %d\n", idx, code); | |
924 | return 0; | |
925 | } | |
926 | ||
927 | ret = rcar_vin_try_bus_param(icd, fmt->bits_per_sample); | |
928 | if (ret < 0) | |
929 | return 0; | |
930 | ||
931 | if (!icd->host_priv) { | |
932 | struct v4l2_mbus_framefmt mf; | |
933 | struct v4l2_rect rect; | |
934 | struct device *dev = icd->parent; | |
935 | int shift; | |
936 | ||
937 | ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); | |
938 | if (ret < 0) | |
939 | return ret; | |
940 | ||
941 | /* Cache current client geometry */ | |
942 | ret = soc_camera_client_g_rect(sd, &rect); | |
943 | if (ret == -ENOIOCTLCMD) { | |
944 | /* Sensor driver doesn't support cropping */ | |
945 | rect.left = 0; | |
946 | rect.top = 0; | |
947 | rect.width = mf.width; | |
948 | rect.height = mf.height; | |
949 | } else if (ret < 0) { | |
950 | return ret; | |
951 | } | |
952 | ||
953 | /* | |
954 | * If sensor proposes too large format then try smaller ones: | |
955 | * 1280x960, 640x480, 320x240 | |
956 | */ | |
957 | for (shift = 0; shift < 3; shift++) { | |
958 | if (mf.width <= VIN_MAX_WIDTH && | |
959 | mf.height <= VIN_MAX_HEIGHT) | |
960 | break; | |
961 | ||
962 | mf.width = 1280 >> shift; | |
963 | mf.height = 960 >> shift; | |
964 | ret = v4l2_device_call_until_err(sd->v4l2_dev, | |
965 | soc_camera_grp_id(icd), | |
966 | video, s_mbus_fmt, | |
967 | &mf); | |
968 | if (ret < 0) | |
969 | return ret; | |
970 | } | |
971 | ||
972 | if (shift == 3) { | |
973 | dev_err(dev, | |
974 | "Failed to configure the client below %ux%x\n", | |
975 | mf.width, mf.height); | |
976 | return -EIO; | |
977 | } | |
978 | ||
979 | dev_dbg(dev, "camera fmt %ux%u\n", mf.width, mf.height); | |
980 | ||
981 | cam = kzalloc(sizeof(*cam), GFP_KERNEL); | |
982 | if (!cam) | |
983 | return -ENOMEM; | |
984 | /* | |
985 | * We are called with current camera crop, | |
986 | * initialise subrect with it | |
987 | */ | |
988 | cam->rect = rect; | |
989 | cam->subrect = rect; | |
990 | cam->width = mf.width; | |
991 | cam->height = mf.height; | |
992 | ||
993 | icd->host_priv = cam; | |
994 | } else { | |
995 | cam = icd->host_priv; | |
996 | } | |
997 | ||
998 | /* Beginning of a pass */ | |
999 | if (!idx) | |
1000 | cam->extra_fmt = NULL; | |
1001 | ||
1002 | switch (code) { | |
1003 | case V4L2_MBUS_FMT_YUYV8_1X16: | |
1004 | case V4L2_MBUS_FMT_YUYV8_2X8: | |
1005 | if (cam->extra_fmt) | |
1006 | break; | |
1007 | ||
1008 | /* Add all our formats that can be generated by VIN */ | |
1009 | cam->extra_fmt = rcar_vin_formats; | |
1010 | ||
1011 | n = ARRAY_SIZE(rcar_vin_formats); | |
1012 | formats += n; | |
1013 | for (k = 0; xlate && k < n; k++, xlate++) { | |
1014 | xlate->host_fmt = &rcar_vin_formats[k]; | |
1015 | xlate->code = code; | |
1016 | dev_dbg(dev, "Providing format %s using code %d\n", | |
1017 | rcar_vin_formats[k].name, code); | |
1018 | } | |
1019 | break; | |
1020 | default: | |
1021 | if (!rcar_vin_packing_supported(fmt)) | |
1022 | return 0; | |
1023 | ||
1024 | dev_dbg(dev, "Providing format %s in pass-through mode\n", | |
1025 | fmt->name); | |
1026 | break; | |
1027 | } | |
1028 | ||
1029 | /* Generic pass-through */ | |
1030 | formats++; | |
1031 | if (xlate) { | |
1032 | xlate->host_fmt = fmt; | |
1033 | xlate->code = code; | |
1034 | xlate++; | |
1035 | } | |
1036 | ||
1037 | return formats; | |
1038 | } | |
1039 | ||
1040 | static void rcar_vin_put_formats(struct soc_camera_device *icd) | |
1041 | { | |
1042 | kfree(icd->host_priv); | |
1043 | icd->host_priv = NULL; | |
1044 | } | |
1045 | ||
1046 | static int rcar_vin_set_crop(struct soc_camera_device *icd, | |
1047 | const struct v4l2_crop *a) | |
1048 | { | |
1049 | struct v4l2_crop a_writable = *a; | |
1050 | const struct v4l2_rect *rect = &a_writable.c; | |
1051 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); | |
1052 | struct rcar_vin_priv *priv = ici->priv; | |
1053 | struct v4l2_crop cam_crop; | |
1054 | struct rcar_vin_cam *cam = icd->host_priv; | |
1055 | struct v4l2_rect *cam_rect = &cam_crop.c; | |
1056 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
1057 | struct device *dev = icd->parent; | |
1058 | struct v4l2_mbus_framefmt mf; | |
1059 | u32 vnmc; | |
1060 | int ret, i; | |
1061 | ||
1062 | dev_dbg(dev, "S_CROP(%ux%u@%u:%u)\n", rect->width, rect->height, | |
1063 | rect->left, rect->top); | |
1064 | ||
1065 | /* During camera cropping its output window can change too, stop VIN */ | |
1066 | capture_stop_preserve(priv, &vnmc); | |
1067 | dev_dbg(dev, "VNMC_REG 0x%x\n", vnmc); | |
1068 | ||
1069 | /* Apply iterative camera S_CROP for new input window. */ | |
1070 | ret = soc_camera_client_s_crop(sd, &a_writable, &cam_crop, | |
1071 | &cam->rect, &cam->subrect); | |
1072 | if (ret < 0) | |
1073 | return ret; | |
1074 | ||
1075 | dev_dbg(dev, "camera cropped to %ux%u@%u:%u\n", | |
1076 | cam_rect->width, cam_rect->height, | |
1077 | cam_rect->left, cam_rect->top); | |
1078 | ||
1079 | /* On success cam_crop contains current camera crop */ | |
1080 | ||
1081 | /* Retrieve camera output window */ | |
1082 | ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf); | |
1083 | if (ret < 0) | |
1084 | return ret; | |
1085 | ||
1086 | if (mf.width > VIN_MAX_WIDTH || mf.height > VIN_MAX_HEIGHT) | |
1087 | return -EINVAL; | |
1088 | ||
1089 | /* Cache camera output window */ | |
1090 | cam->width = mf.width; | |
1091 | cam->height = mf.height; | |
1092 | ||
1093 | icd->user_width = cam->width; | |
1094 | icd->user_height = cam->height; | |
1095 | ||
1096 | cam->vin_left = rect->left & ~1; | |
1097 | cam->vin_top = rect->top & ~1; | |
1098 | ||
1099 | /* Use VIN cropping to crop to the new window. */ | |
1100 | ret = rcar_vin_set_rect(icd); | |
1101 | if (ret < 0) | |
1102 | return ret; | |
1103 | ||
1104 | cam->subrect = *rect; | |
1105 | ||
1106 | dev_dbg(dev, "VIN cropped to %ux%u@%u:%u\n", | |
1107 | icd->user_width, icd->user_height, | |
1108 | cam->vin_left, cam->vin_top); | |
1109 | ||
1110 | /* Restore capture */ | |
1111 | for (i = 0; i < MAX_BUFFER_NUM; i++) { | |
1112 | if (priv->queue_buf[i] && priv->state == STOPPED) { | |
1113 | vnmc |= VNMC_ME; | |
1114 | break; | |
1115 | } | |
1116 | } | |
1117 | capture_restore(priv, vnmc); | |
1118 | ||
1119 | /* Even if only camera cropping succeeded */ | |
1120 | return ret; | |
1121 | } | |
1122 | ||
1123 | static int rcar_vin_get_crop(struct soc_camera_device *icd, | |
1124 | struct v4l2_crop *a) | |
1125 | { | |
1126 | struct rcar_vin_cam *cam = icd->host_priv; | |
1127 | ||
1128 | a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1129 | a->c = cam->subrect; | |
1130 | ||
1131 | return 0; | |
1132 | } | |
1133 | ||
1134 | /* Similar to set_crop multistage iterative algorithm */ | |
1135 | static int rcar_vin_set_fmt(struct soc_camera_device *icd, | |
1136 | struct v4l2_format *f) | |
1137 | { | |
1138 | struct soc_camera_host *ici = to_soc_camera_host(icd->parent); | |
1139 | struct rcar_vin_priv *priv = ici->priv; | |
1140 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
1141 | struct rcar_vin_cam *cam = icd->host_priv; | |
1142 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
1143 | struct v4l2_mbus_framefmt mf; | |
1144 | struct device *dev = icd->parent; | |
1145 | __u32 pixfmt = pix->pixelformat; | |
1146 | const struct soc_camera_format_xlate *xlate; | |
1147 | unsigned int vin_sub_width = 0, vin_sub_height = 0; | |
1148 | int ret; | |
1149 | bool can_scale; | |
1150 | enum v4l2_field field; | |
1151 | v4l2_std_id std; | |
1152 | ||
1153 | dev_dbg(dev, "S_FMT(pix=0x%x, %ux%u)\n", | |
1154 | pixfmt, pix->width, pix->height); | |
1155 | ||
1156 | switch (pix->field) { | |
1157 | default: | |
1158 | pix->field = V4L2_FIELD_NONE; | |
1159 | /* fall-through */ | |
1160 | case V4L2_FIELD_NONE: | |
1161 | case V4L2_FIELD_TOP: | |
1162 | case V4L2_FIELD_BOTTOM: | |
1163 | case V4L2_FIELD_INTERLACED_TB: | |
1164 | case V4L2_FIELD_INTERLACED_BT: | |
1165 | field = pix->field; | |
1166 | break; | |
1167 | case V4L2_FIELD_INTERLACED: | |
1168 | /* Query for standard if not explicitly mentioned _TB/_BT */ | |
1169 | ret = v4l2_subdev_call(sd, video, querystd, &std); | |
1170 | if (ret < 0) | |
1171 | std = V4L2_STD_625_50; | |
1172 | ||
1173 | field = std & V4L2_STD_625_50 ? V4L2_FIELD_INTERLACED_TB : | |
1174 | V4L2_FIELD_INTERLACED_BT; | |
1175 | break; | |
1176 | } | |
1177 | ||
1178 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); | |
1179 | if (!xlate) { | |
1180 | dev_warn(dev, "Format %x not found\n", pixfmt); | |
1181 | return -EINVAL; | |
1182 | } | |
1183 | /* Calculate client output geometry */ | |
1184 | soc_camera_calc_client_output(icd, &cam->rect, &cam->subrect, pix, &mf, | |
1185 | 12); | |
1186 | mf.field = pix->field; | |
1187 | mf.colorspace = pix->colorspace; | |
1188 | mf.code = xlate->code; | |
1189 | ||
1190 | switch (pixfmt) { | |
1191 | case V4L2_PIX_FMT_RGB32: | |
1192 | can_scale = priv->chip != RCAR_E1; | |
1193 | break; | |
1194 | case V4L2_PIX_FMT_UYVY: | |
1195 | case V4L2_PIX_FMT_YUYV: | |
1196 | case V4L2_PIX_FMT_RGB565: | |
1197 | case V4L2_PIX_FMT_RGB555X: | |
1198 | can_scale = true; | |
1199 | break; | |
1200 | default: | |
1201 | can_scale = false; | |
1202 | break; | |
1203 | } | |
1204 | ||
1205 | dev_dbg(dev, "request camera output %ux%u\n", mf.width, mf.height); | |
1206 | ||
1207 | ret = soc_camera_client_scale(icd, &cam->rect, &cam->subrect, | |
1208 | &mf, &vin_sub_width, &vin_sub_height, | |
1209 | can_scale, 12); | |
1210 | ||
1211 | /* Done with the camera. Now see if we can improve the result */ | |
1212 | dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n", | |
1213 | ret, mf.width, mf.height, pix->width, pix->height); | |
1214 | ||
1215 | if (ret == -ENOIOCTLCMD) | |
1216 | dev_dbg(dev, "Sensor doesn't support scaling\n"); | |
1217 | else if (ret < 0) | |
1218 | return ret; | |
1219 | ||
1220 | if (mf.code != xlate->code) | |
1221 | return -EINVAL; | |
1222 | ||
1223 | /* Prepare VIN crop */ | |
1224 | cam->width = mf.width; | |
1225 | cam->height = mf.height; | |
1226 | ||
1227 | /* Use VIN scaling to scale to the requested user window. */ | |
1228 | ||
1229 | /* We cannot scale up */ | |
1230 | if (pix->width > vin_sub_width) | |
1231 | vin_sub_width = pix->width; | |
1232 | ||
1233 | if (pix->height > vin_sub_height) | |
1234 | vin_sub_height = pix->height; | |
1235 | ||
1236 | pix->colorspace = mf.colorspace; | |
1237 | ||
1238 | if (!can_scale) { | |
1239 | pix->width = vin_sub_width; | |
1240 | pix->height = vin_sub_height; | |
1241 | } | |
1242 | ||
1243 | /* | |
1244 | * We have calculated CFLCR, the actual configuration will be performed | |
1245 | * in rcar_vin_set_bus_param() | |
1246 | */ | |
1247 | ||
1248 | dev_dbg(dev, "W: %u : %u, H: %u : %u\n", | |
1249 | vin_sub_width, pix->width, vin_sub_height, pix->height); | |
1250 | ||
1251 | icd->current_fmt = xlate; | |
1252 | ||
1253 | priv->field = field; | |
1254 | ||
1255 | return 0; | |
1256 | } | |
1257 | ||
1258 | static int rcar_vin_try_fmt(struct soc_camera_device *icd, | |
1259 | struct v4l2_format *f) | |
1260 | { | |
1261 | const struct soc_camera_format_xlate *xlate; | |
1262 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
1263 | struct v4l2_subdev *sd = soc_camera_to_subdev(icd); | |
1264 | struct v4l2_mbus_framefmt mf; | |
1265 | __u32 pixfmt = pix->pixelformat; | |
1266 | int width, height; | |
1267 | int ret; | |
1268 | ||
1269 | xlate = soc_camera_xlate_by_fourcc(icd, pixfmt); | |
1270 | if (!xlate) { | |
1271 | xlate = icd->current_fmt; | |
1272 | dev_dbg(icd->parent, "Format %x not found, keeping %x\n", | |
1273 | pixfmt, xlate->host_fmt->fourcc); | |
1274 | pixfmt = xlate->host_fmt->fourcc; | |
1275 | pix->pixelformat = pixfmt; | |
1276 | pix->colorspace = icd->colorspace; | |
1277 | } | |
1278 | ||
1279 | /* FIXME: calculate using depth and bus width */ | |
1280 | v4l_bound_align_image(&pix->width, 2, VIN_MAX_WIDTH, 1, | |
1281 | &pix->height, 4, VIN_MAX_HEIGHT, 2, 0); | |
1282 | ||
1283 | width = pix->width; | |
1284 | height = pix->height; | |
1285 | ||
1286 | /* let soc-camera calculate these values */ | |
1287 | pix->bytesperline = 0; | |
1288 | pix->sizeimage = 0; | |
1289 | ||
1290 | /* limit to sensor capabilities */ | |
1291 | mf.width = pix->width; | |
1292 | mf.height = pix->height; | |
1293 | mf.field = pix->field; | |
1294 | mf.code = xlate->code; | |
1295 | mf.colorspace = pix->colorspace; | |
1296 | ||
1297 | ret = v4l2_device_call_until_err(sd->v4l2_dev, soc_camera_grp_id(icd), | |
1298 | video, try_mbus_fmt, &mf); | |
1299 | if (ret < 0) | |
1300 | return ret; | |
1301 | ||
1302 | pix->width = mf.width; | |
1303 | pix->height = mf.height; | |
1304 | pix->field = mf.field; | |
1305 | pix->colorspace = mf.colorspace; | |
1306 | ||
1307 | if (pixfmt == V4L2_PIX_FMT_NV16) { | |
1308 | /* FIXME: check against rect_max after converting soc-camera */ | |
1309 | /* We can scale precisely, need a bigger image from camera */ | |
1310 | if (pix->width < width || pix->height < height) { | |
1311 | /* | |
1312 | * We presume, the sensor behaves sanely, i.e. if | |
1313 | * requested a bigger rectangle, it will not return a | |
1314 | * smaller one. | |
1315 | */ | |
1316 | mf.width = VIN_MAX_WIDTH; | |
1317 | mf.height = VIN_MAX_HEIGHT; | |
1318 | ret = v4l2_device_call_until_err(sd->v4l2_dev, | |
1319 | soc_camera_grp_id(icd), | |
1320 | video, try_mbus_fmt, | |
1321 | &mf); | |
1322 | if (ret < 0) { | |
1323 | dev_err(icd->parent, | |
1324 | "client try_fmt() = %d\n", ret); | |
1325 | return ret; | |
1326 | } | |
1327 | } | |
1328 | /* We will scale exactly */ | |
1329 | if (mf.width > width) | |
1330 | pix->width = width; | |
1331 | if (mf.height > height) | |
1332 | pix->height = height; | |
1333 | } | |
1334 | ||
1335 | return ret; | |
1336 | } | |
1337 | ||
1338 | static unsigned int rcar_vin_poll(struct file *file, poll_table *pt) | |
1339 | { | |
1340 | struct soc_camera_device *icd = file->private_data; | |
1341 | ||
1342 | return vb2_poll(&icd->vb2_vidq, file, pt); | |
1343 | } | |
1344 | ||
1345 | static int rcar_vin_querycap(struct soc_camera_host *ici, | |
1346 | struct v4l2_capability *cap) | |
1347 | { | |
1348 | strlcpy(cap->card, "R_Car_VIN", sizeof(cap->card)); | |
1349 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; | |
1350 | return 0; | |
1351 | } | |
1352 | ||
1353 | static int rcar_vin_init_videobuf2(struct vb2_queue *vq, | |
1354 | struct soc_camera_device *icd) | |
1355 | { | |
1356 | vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
1357 | vq->io_modes = VB2_MMAP | VB2_USERPTR; | |
1358 | vq->drv_priv = icd; | |
1359 | vq->ops = &rcar_vin_vb2_ops; | |
1360 | vq->mem_ops = &vb2_dma_contig_memops; | |
1361 | vq->buf_struct_size = sizeof(struct rcar_vin_buffer); | |
1362 | vq->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; | |
1363 | ||
1364 | return vb2_queue_init(vq); | |
1365 | } | |
1366 | ||
1367 | static struct soc_camera_host_ops rcar_vin_host_ops = { | |
1368 | .owner = THIS_MODULE, | |
1369 | .add = rcar_vin_add_device, | |
1370 | .remove = rcar_vin_remove_device, | |
1371 | .clock_start = rcar_vin_clock_start, | |
1372 | .clock_stop = rcar_vin_clock_stop, | |
1373 | .get_formats = rcar_vin_get_formats, | |
1374 | .put_formats = rcar_vin_put_formats, | |
1375 | .get_crop = rcar_vin_get_crop, | |
1376 | .set_crop = rcar_vin_set_crop, | |
1377 | .try_fmt = rcar_vin_try_fmt, | |
1378 | .set_fmt = rcar_vin_set_fmt, | |
1379 | .poll = rcar_vin_poll, | |
1380 | .querycap = rcar_vin_querycap, | |
1381 | .set_bus_param = rcar_vin_set_bus_param, | |
1382 | .init_videobuf2 = rcar_vin_init_videobuf2, | |
1383 | }; | |
1384 | ||
1385 | static struct platform_device_id rcar_vin_id_table[] = { | |
ca0c9f17 | 1386 | { "r8a7790-vin", RCAR_H2 }, |
73135e96 VB |
1387 | { "r8a7779-vin", RCAR_H1 }, |
1388 | { "r8a7778-vin", RCAR_M1 }, | |
1389 | { "uPD35004-vin", RCAR_E1 }, | |
1390 | {}, | |
1391 | }; | |
1392 | MODULE_DEVICE_TABLE(platform, rcar_vin_id_table); | |
1393 | ||
1394 | static int rcar_vin_probe(struct platform_device *pdev) | |
1395 | { | |
1396 | struct rcar_vin_priv *priv; | |
1397 | struct resource *mem; | |
1398 | struct rcar_vin_platform_data *pdata; | |
1399 | int irq, ret; | |
1400 | ||
1401 | pdata = pdev->dev.platform_data; | |
1402 | if (!pdata || !pdata->flags) { | |
1403 | dev_err(&pdev->dev, "platform data not set\n"); | |
1404 | return -EINVAL; | |
1405 | } | |
1406 | ||
1407 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1408 | if (mem == NULL) | |
1409 | return -EINVAL; | |
1410 | ||
1411 | irq = platform_get_irq(pdev, 0); | |
1412 | if (irq <= 0) | |
1413 | return -EINVAL; | |
1414 | ||
1415 | priv = devm_kzalloc(&pdev->dev, sizeof(struct rcar_vin_priv), | |
1416 | GFP_KERNEL); | |
1417 | if (!priv) | |
1418 | return -ENOMEM; | |
1419 | ||
1420 | priv->base = devm_ioremap_resource(&pdev->dev, mem); | |
1421 | if (IS_ERR(priv->base)) | |
1422 | return PTR_ERR(priv->base); | |
1423 | ||
1424 | ret = devm_request_irq(&pdev->dev, irq, rcar_vin_irq, IRQF_SHARED, | |
1425 | dev_name(&pdev->dev), priv); | |
1426 | if (ret) | |
1427 | return ret; | |
1428 | ||
1429 | priv->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); | |
1430 | if (IS_ERR(priv->alloc_ctx)) | |
1431 | return PTR_ERR(priv->alloc_ctx); | |
1432 | ||
1433 | priv->ici.priv = priv; | |
1434 | priv->ici.v4l2_dev.dev = &pdev->dev; | |
1435 | priv->ici.nr = pdev->id; | |
1436 | priv->ici.drv_name = dev_name(&pdev->dev); | |
1437 | priv->ici.ops = &rcar_vin_host_ops; | |
1438 | ||
1439 | priv->pdata = pdata; | |
1440 | priv->chip = pdev->id_entry->driver_data; | |
1441 | spin_lock_init(&priv->lock); | |
1442 | INIT_LIST_HEAD(&priv->capture); | |
1443 | ||
1444 | priv->state = STOPPED; | |
1445 | ||
1446 | pm_suspend_ignore_children(&pdev->dev, true); | |
1447 | pm_runtime_enable(&pdev->dev); | |
1448 | ||
1449 | ret = soc_camera_host_register(&priv->ici); | |
1450 | if (ret) | |
1451 | goto cleanup; | |
1452 | ||
1453 | return 0; | |
1454 | ||
1455 | cleanup: | |
1456 | pm_runtime_disable(&pdev->dev); | |
1457 | vb2_dma_contig_cleanup_ctx(priv->alloc_ctx); | |
1458 | ||
1459 | return ret; | |
1460 | } | |
1461 | ||
1462 | static int rcar_vin_remove(struct platform_device *pdev) | |
1463 | { | |
1464 | struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev); | |
1465 | struct rcar_vin_priv *priv = container_of(soc_host, | |
1466 | struct rcar_vin_priv, ici); | |
1467 | ||
1468 | soc_camera_host_unregister(soc_host); | |
1469 | pm_runtime_disable(&pdev->dev); | |
1470 | vb2_dma_contig_cleanup_ctx(priv->alloc_ctx); | |
1471 | ||
1472 | return 0; | |
1473 | } | |
1474 | ||
1475 | static struct platform_driver rcar_vin_driver = { | |
1476 | .probe = rcar_vin_probe, | |
1477 | .remove = rcar_vin_remove, | |
1478 | .driver = { | |
1479 | .name = DRV_NAME, | |
1480 | .owner = THIS_MODULE, | |
1481 | }, | |
1482 | .id_table = rcar_vin_id_table, | |
1483 | }; | |
1484 | ||
1485 | module_platform_driver(rcar_vin_driver); | |
1486 | ||
1487 | MODULE_LICENSE("GPL"); | |
1488 | MODULE_ALIAS("platform:rcar_vin"); | |
1489 | MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver"); |