1 /*
2 * TI VIP capture driver
3 *
4 * Copyright (C) 2013 - 2014 Texas Instruments, Inc.
5 * David Griego, <dagriego@biglakesoftware.com>
6 * Dale Farnsworth, <dale@farnsworth.org>
7 * Nikhil Devshatwar, <nikhil.nd@ti.com>
8 * Benoit Parrot, <bparrot@ti.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
16 #include <linux/clk.h>
17 #include <linux/delay.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
20 #include <linux/fs.h>
21 #include <linux/interrupt.h>
22 #include <linux/io.h>
23 #include <linux/ioctl.h>
24 #include <linux/i2c.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/mfd/syscon.h>
31 #include <linux/regmap.h>
33 #include <linux/pinctrl/consumer.h>
34 #include <linux/of_device.h>
35 #include <linux/of_graph.h>
37 #include <media/v4l2-of.h>
38 #include <media/v4l2-async.h>
40 #include "vip.h"
42 #define VIP_MODULE_NAME "vip"
43 #define VIP_INPUT_NAME "Vin0"
45 static int debug;
46 module_param(debug, int, 0644);
47 MODULE_PARM_DESC(debug, "debug level (0-8)");
49 /*
50 * Minimum and maximum frame sizes
51 */
52 #define MIN_W 128
53 #define MIN_H 128
54 #define MAX_W 1920
55 #define MAX_H 1080
57 /*
58 * Required alignments
59 */
60 #define S_ALIGN 0 /* multiple of 1 */
61 #define H_ALIGN 1 /* multiple of 2 */
62 #define W_ALIGN 1 /* multiple of 2 */
63 #define L_ALIGN 7 /* multiple of 128, line stride, 16 bytes */
65 /*
66 * Need a descriptor entry for each of up to 15 outputs,
67 * and up to 2 control transfers.
68 */
69 #define VIP_DESC_LIST_SIZE (17 * sizeof(struct vpdma_dtd))
71 #define vip_dbg(level, dev, fmt, arg...) \
72 v4l2_dbg(level, debug, &dev->v4l2_dev, fmt, ##arg)
73 #define vip_err(dev, fmt, arg...) \
74 v4l2_err(&dev->v4l2_dev, fmt, ##arg)
75 #define vip_info(dev, fmt, arg...) \
76 v4l2_info(&dev->v4l2_dev, fmt, ##arg)
78 #define CTRL_CORE_SMA_SW_1 0x534
79 /*
80 * The srce_info structure contains per-srce data.
81 */
82 struct vip_srce_info {
83 u8 base_channel; /* the VPDMA channel nummber */
84 u8 vb_index; /* input frame f, f-1, f-2 index */
85 u8 vb_part; /* identifies section of co-planar formats */
86 };
88 #define VIP_VPDMA_FIFO_SIZE 2
89 #define VIP_DROPQ_SIZE 3
91 /*
92 * Define indices into the srce_info tables
93 */
95 #define VIP_SRCE_MULT_PORT 0
96 #define VIP_SRCE_MULT_ANC 1
97 #define VIP_SRCE_LUMA 2
98 #define VIP_SRCE_CHROMA 3
99 #define VIP_SRCE_RGB 4
101 static struct vip_srce_info srce_info[5] = {
102 [VIP_SRCE_MULT_PORT] = {
103 .base_channel = VIP1_CHAN_NUM_MULT_PORT_A_SRC0,
104 .vb_index = 0,
105 .vb_part = VIP_CHROMA,
106 },
107 [VIP_SRCE_MULT_ANC] = {
108 .base_channel = VIP1_CHAN_NUM_MULT_ANC_A_SRC0,
109 .vb_index = 0,
110 .vb_part = VIP_LUMA,
111 },
112 [VIP_SRCE_LUMA] = {
113 .base_channel = VIP1_CHAN_NUM_PORT_B_LUMA,
114 .vb_index = 1,
115 .vb_part = VIP_LUMA,
116 },
117 [VIP_SRCE_CHROMA] = {
118 .base_channel = VIP1_CHAN_NUM_PORT_B_CHROMA,
119 .vb_index = 1,
120 .vb_part = VIP_CHROMA,
121 },
122 [VIP_SRCE_RGB] = {
123 .base_channel = VIP1_CHAN_NUM_PORT_B_RGB,
124 .vb_part = VIP_LUMA,
125 },
126 };
128 static struct vip_fmt vip_formats[] = {
129 {
130 .name = "YUV 444 co-planar",
131 .fourcc = V4L2_PIX_FMT_NV24,
132 .code = V4L2_MBUS_FMT_YDYUYDYV8_1X16,
133 .colorspace = V4L2_COLORSPACE_SMPTE170M,
134 .coplanar = 1,
135 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y444],
136 &vpdma_yuv_fmts[VPDMA_DATA_FMT_C444],
137 },
138 },
139 {
140 .name = "YUV 422 co-planar",
141 .fourcc = V4L2_PIX_FMT_NV16,
142 .code = V4L2_MBUS_FMT_YDYUYDYV8_1X16,
143 .colorspace = V4L2_COLORSPACE_SMPTE170M,
144 .coplanar = 1,
145 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y422],
146 &vpdma_yuv_fmts[VPDMA_DATA_FMT_C422],
147 },
148 },
149 {
150 .name = "YUV 420 co-planar",
151 .fourcc = V4L2_PIX_FMT_NV12,
152 .code = V4L2_MBUS_FMT_YDYUYDYV8_1X16,
153 .colorspace = V4L2_COLORSPACE_SMPTE170M,
154 .coplanar = 1,
155 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y420],
156 &vpdma_yuv_fmts[VPDMA_DATA_FMT_C420],
157 },
158 },
159 {
160 .name = "UYVY 422 packed",
161 .fourcc = V4L2_PIX_FMT_UYVY,
162 .code = V4L2_MBUS_FMT_UYVY8_2X8,
163 .colorspace = V4L2_COLORSPACE_SMPTE170M,
164 .coplanar = 0,
165 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CY422],
166 },
167 },
168 {
169 .name = "YUYV 422 packed",
170 .fourcc = V4L2_PIX_FMT_YUYV,
171 .code = V4L2_MBUS_FMT_YUYV8_2X8,
172 .colorspace = V4L2_COLORSPACE_SMPTE170M,
173 .coplanar = 0,
174 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YC422],
175 },
176 },
177 {
178 .name = "VYUY 422 packed",
179 .fourcc = V4L2_PIX_FMT_VYUY,
180 .code = V4L2_MBUS_FMT_VYUY8_2X8,
181 .colorspace = V4L2_COLORSPACE_SMPTE170M,
182 .coplanar = 0,
183 .vpdma_fmt = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YC422],
184 },
185 },
186 {
187 .name = "RGB888 packed",
188 .fourcc = V4L2_PIX_FMT_RGB24,
189 .code = V4L2_MBUS_FMT_RGB888_1X24,
190 .colorspace = V4L2_COLORSPACE_SRGB,
191 .coplanar = 0,
192 .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
193 },
194 },
195 {
196 .name = "ARGB888 packed",
197 .fourcc = V4L2_PIX_FMT_RGB32,
198 .code = V4L2_MBUS_FMT_ARGB8888_1X32,
199 .colorspace = V4L2_COLORSPACE_SRGB,
200 .coplanar = 0,
201 .vpdma_fmt = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
202 },
203 },
204 };
206 /* Print Four-character-code (FOURCC) */
207 static char *fourcc_to_str(u32 fmt)
208 {
209 static char code[5];
210 code[0] = (unsigned char)(fmt & 0xff);
211 code[1] = (unsigned char)((fmt>>8) & 0xff);
212 code[2] = (unsigned char)((fmt>>16) & 0xff);
213 code[3] = (unsigned char)((fmt>>24) & 0xff);
214 code[4] = '\0';
216 return code;
217 }
219 /*
220 * Find our format description corresponding to the passed v4l2_format
221 */
222 #ifdef DISABLED_FOR_NOW
223 static struct vip_fmt *find_format_by_pix(u32 pixelformat)
224 {
225 struct vip_fmt *fmt;
226 unsigned int k;
228 for (k = 0; k < ARRAY_SIZE(vip_formats); k++) {
229 fmt = &vip_formats[k];
230 if (fmt->fourcc == pixelformat)
231 return fmt;
232 }
234 return NULL;
235 }
236 #endif
238 static struct vip_fmt *find_format_by_code(u32 code)
239 {
240 struct vip_fmt *fmt;
241 unsigned int k;
243 for (k = 0; k < ARRAY_SIZE(vip_formats); k++) {
244 fmt = &vip_formats[k];
245 if (fmt->code == code)
246 return fmt;
247 }
249 return NULL;
250 }
252 static struct vip_fmt *find_active_format_by_pix(struct vip_dev *dev,
253 u32 pixelformat)
254 {
255 struct vip_fmt *fmt;
256 unsigned int k;
258 for (k = 0; k < dev->num_active_fmt; k++) {
259 fmt = dev->active_fmt[k];
260 if (fmt->fourcc == pixelformat)
261 return fmt;
262 }
264 return NULL;
265 }
267 static struct vip_fmt *find_active_format_by_code(struct vip_dev *dev,
268 u32 code)
269 {
270 struct vip_fmt *fmt;
271 unsigned int k;
273 for (k = 0; k < dev->num_active_fmt; k++) {
274 fmt = dev->active_fmt[k];
275 if (fmt->code == code)
276 return fmt;
277 }
279 return NULL;
280 }
282 static LIST_HEAD(vip_shared_list);
284 static inline struct vip_dev *notifier_to_vip_dev(struct v4l2_async_notifier *n)
285 {
286 return container_of(n, struct vip_dev, notifier);
287 }
289 /*
290 * port flag bits
291 */
292 #define FLAG_FRAME_1D (1 << 0)
293 #define FLAG_EVEN_LINE_SKIP (1 << 1)
294 #define FLAG_ODD_LINE_SKIP (1 << 2)
295 #define FLAG_MODE_TILED (1 << 3)
296 #define FLAG_INTERLACED (1 << 4)
297 #define FLAG_MULTIPLEXED (1 << 5)
298 #define FLAG_MULT_PORT (1 << 6)
299 #define FLAG_MULT_ANC (1 << 7)
301 /*
302 * Function prototype declarations
303 */
304 static int alloc_port(struct vip_dev *, int);
305 static void free_port(struct vip_port *);
306 static int vip_setup_parser(struct vip_port *port);
308 static inline u32 read_sreg(struct vip_shared *shared, int offset)
309 {
310 return ioread32(shared->base + offset);
311 }
313 static inline void write_sreg(struct vip_shared *shared, int offset, u32 value)
314 {
315 iowrite32(value, shared->base + offset);
316 }
318 static inline u32 read_vreg(struct vip_dev *dev, int offset)
319 {
320 return ioread32(dev->base + offset);
321 }
323 static inline void write_vreg(struct vip_dev *dev, int offset, u32 value)
324 {
325 iowrite32(value, dev->base + offset);
326 }
328 /*
329 * Insert a masked field into a 32-bit field
330 */
331 static void insert_field(u32 *valp, u32 field, u32 mask, int shift)
332 {
333 u32 val = *valp;
335 val &= ~(mask << shift);
336 val |= (field & mask) << shift;
337 *valp = val;
338 }
340 /*
341 * Set the system idle mode
342 */
343 static void vip_set_idle_mode(struct vip_shared *shared, int mode)
344 {
345 u32 reg = read_sreg(shared, VIP_SYSCONFIG);
346 insert_field(®, mode, VIP_SYSCONFIG_IDLE_MASK,
347 VIP_SYSCONFIG_IDLE_SHIFT);
348 write_sreg(shared, VIP_SYSCONFIG, reg);
349 }
351 /*
352 * Set the VIP standby mode
353 */
354 static void vip_set_standby_mode(struct vip_shared *shared, int mode)
355 {
356 u32 reg = read_sreg(shared, VIP_SYSCONFIG);
357 insert_field(®, mode, VIP_SYSCONFIG_STANDBY_MASK,
358 VIP_SYSCONFIG_STANDBY_SHIFT);
359 write_sreg(shared, VIP_SYSCONFIG, reg);
360 }
362 /*
363 * Enable or disable the VIP clocks
364 */
365 static void vip_set_clock_enable(struct vip_dev *dev, bool on)
366 {
367 u32 val = 0;
369 val = read_vreg(dev, VIP_CLK_ENABLE);
370 if (on) {
371 val |= VIP_VPDMA_CLK_ENABLE;
372 if (dev->slice_id == VIP_SLICE1)
373 val |= VIP_VIP1_DATA_PATH_CLK_ENABLE;
374 else
375 val |= VIP_VIP2_DATA_PATH_CLK_ENABLE;
376 } else {
377 if (dev->slice_id == VIP_SLICE1)
378 val &= ~VIP_VIP1_DATA_PATH_CLK_ENABLE;
379 else
380 val &= ~VIP_VIP2_DATA_PATH_CLK_ENABLE;
382 /* Both VIP are disabled then shutdown VPDMA also */
383 if (!(val & (VIP_VIP1_DATA_PATH_CLK_ENABLE|
384 VIP_VIP2_DATA_PATH_CLK_ENABLE)))
385 val = 0;
386 }
388 write_vreg(dev, VIP_CLK_ENABLE, val);
389 }
391 /* This helper function is used to enable the clock early on to
392 * enable vpdma firmware loading before the slice device are created
393 */
394 static void vip_shared_set_clock_enable(struct vip_shared *shared, bool on)
395 {
396 u32 val = 0;
398 if (on)
399 val = VIP_VIP1_DATA_PATH_CLK_ENABLE | VIP_VPDMA_CLK_ENABLE;
401 write_sreg(shared, VIP_CLK_ENABLE, val);
402 }
404 static void vip_top_reset(struct vip_dev *dev)
405 {
406 u32 val = 0;
408 val = read_vreg(dev, VIP_CLK_RESET);
410 if (dev->slice_id == VIP_SLICE1)
411 insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
412 VIP_VIP1_DATA_PATH_RESET_SHIFT);
413 else
414 insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
415 VIP_VIP2_DATA_PATH_RESET_SHIFT);
417 write_vreg(dev, VIP_CLK_RESET, val);
419 usleep_range(200, 250);
421 val = read_vreg(dev, VIP_CLK_RESET);
423 if (dev->slice_id == VIP_SLICE1)
424 insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
425 VIP_VIP1_DATA_PATH_RESET_SHIFT);
426 else
427 insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
428 VIP_VIP2_DATA_PATH_RESET_SHIFT);
429 write_vreg(dev, VIP_CLK_RESET, val);
430 }
432 static void vip_top_vpdma_reset(struct vip_shared *shared)
433 {
434 u32 val;
436 val = read_sreg(shared, VIP_CLK_RESET);
437 insert_field(&val, 1, VIP_VPDMA_CLK_RESET_MASK,
438 VIP_VPDMA_CLK_RESET_SHIFT);
439 write_sreg(shared, VIP_CLK_RESET, val);
441 usleep_range(200, 250);
443 val = read_sreg(shared, VIP_CLK_RESET);
444 insert_field(&val, 0, VIP_VPDMA_CLK_RESET_MASK,
445 VIP_VPDMA_CLK_RESET_SHIFT);
446 write_sreg(shared, VIP_CLK_RESET, val);
447 }
449 static void vip_xtra_set_repack_sel(struct vip_port *port, int repack_mode)
450 {
451 u32 val;
453 if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
454 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
455 VIP_PARSER_PORTA_1);
456 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
457 VIP_REPACK_SEL_SHFT);
459 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
460 VIP_PARSER_PORTA_1, val);
461 } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
462 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
463 VIP_PARSER_PORTA_1);
464 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
465 VIP_REPACK_SEL_SHFT);
467 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
468 VIP_PARSER_PORTA_1, val);
469 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
470 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
471 VIP_PARSER_PORTB_1);
472 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
473 VIP_REPACK_SEL_SHFT);
475 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
476 VIP_PARSER_PORTB_1, val);
477 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
478 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
479 VIP_PARSER_PORTB_1);
480 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
481 VIP_REPACK_SEL_SHFT);
483 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
484 VIP_PARSER_PORTB_1, val);
485 }
486 }
488 static void vip_set_discrete_basic_mode(struct vip_port *port)
489 {
490 u32 val;
492 if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
493 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
494 VIP_PARSER_PORTA_0);
495 val |= VIP_DISCRETE_BASIC_MODE;
497 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
498 VIP_PARSER_PORTA_0, val);
499 } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
500 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
501 VIP_PARSER_PORTA_0);
502 val |= VIP_DISCRETE_BASIC_MODE;
504 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
505 VIP_PARSER_PORTA_0, val);
506 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
507 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
508 VIP_PARSER_PORTB_0);
509 val |= VIP_DISCRETE_BASIC_MODE;
511 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
512 VIP_PARSER_PORTB_0, val);
513 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
514 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
515 VIP_PARSER_PORTB_0);
516 val |= VIP_DISCRETE_BASIC_MODE;
518 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
519 VIP_PARSER_PORTB_0, val);
520 }
521 }
523 static void vip_set_pclk_polarity(struct vip_port *port, int polarity)
524 {
525 u32 val, ret, offset;
527 if (polarity == 0 && port->dev->syscon) {
529 /*
530 * When the VIP parser is configured to so that the pixel clock
531 * is to be sampled at falling edge, the pixel clock needs to be
532 * inverted before it is given to the VIP module. This is done
533 * by setting a bit in the CTRL_CORE_SMA_SW1 register.
534 */
536 if (port->dev->instance_id == VIP_INSTANCE1)
537 offset = 0 + 2 * port->port_id + port->dev->slice_id;
538 else if (port->dev->instance_id == VIP_INSTANCE2)
539 offset = 4 + 2 * port->port_id + port->dev->slice_id;
540 else if (port->dev->instance_id == VIP_INSTANCE3)
541 offset = 10 - port->dev->slice_id;
542 else
543 BUG();
545 ret = regmap_update_bits(port->dev->syscon,
546 CTRL_CORE_SMA_SW_1, 1 << offset, 1 << offset);
547 }
549 if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
550 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
551 VIP_PARSER_PORTA_0);
552 if (polarity)
553 val |= VIP_PIXCLK_EDGE_POLARITY;
554 else
555 val &= ~VIP_PIXCLK_EDGE_POLARITY;
557 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
558 VIP_PARSER_PORTA_0, val);
559 } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
560 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET
561 + VIP_PARSER_PORTA_0);
562 if (polarity)
563 val |= VIP_PIXCLK_EDGE_POLARITY;
564 else
565 val &= ~VIP_PIXCLK_EDGE_POLARITY;
566 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
567 VIP_PARSER_PORTA_0, val);
568 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
569 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
570 VIP_PARSER_PORTB_0);
571 if (polarity)
572 val |= VIP_PIXCLK_EDGE_POLARITY;
573 else
574 val &= ~VIP_PIXCLK_EDGE_POLARITY;
575 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
576 VIP_PARSER_PORTB_0, val);
577 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
578 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
579 VIP_PARSER_PORTB_0);
580 if (polarity)
581 val |= VIP_PIXCLK_EDGE_POLARITY;
582 else
583 val &= ~VIP_PIXCLK_EDGE_POLARITY;
585 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
586 VIP_PARSER_PORTB_0, val);
587 }
588 }
590 static void vip_set_vsync_polarity(struct vip_port *port, int polarity)
591 {
592 u32 val;
594 if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
595 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
596 VIP_PARSER_PORTA_0);
597 if (polarity)
598 val |= VIP_VSYNC_POLARITY;
599 else
600 val &= ~VIP_VSYNC_POLARITY;
602 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
603 VIP_PARSER_PORTA_0, val);
604 } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
605 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
606 VIP_PARSER_PORTA_0);
607 if (polarity)
608 val |= VIP_VSYNC_POLARITY;
609 else
610 val &= ~VIP_VSYNC_POLARITY;
611 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
612 VIP_PARSER_PORTA_0, val);
613 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
614 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
615 VIP_PARSER_PORTB_0);
616 if (polarity)
617 val |= VIP_VSYNC_POLARITY;
618 else
619 val &= ~VIP_VSYNC_POLARITY;
620 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
621 VIP_PARSER_PORTB_0, val);
622 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
623 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
624 VIP_PARSER_PORTB_0);
625 if (polarity)
626 val |= VIP_VSYNC_POLARITY;
627 else
628 val &= ~VIP_VSYNC_POLARITY;
630 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
631 VIP_PARSER_PORTB_0, val);
632 }
633 }
635 static void vip_set_hsync_polarity(struct vip_port *port, int polarity)
636 {
637 u32 val;
639 if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
640 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
641 VIP_PARSER_PORTA_0);
642 if (polarity)
643 val |= VIP_HSYNC_POLARITY;
644 else
645 val &= ~VIP_HSYNC_POLARITY;
647 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
648 VIP_PARSER_PORTA_0, val);
649 } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
650 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
651 VIP_PARSER_PORTA_0);
652 if (polarity)
653 val |= VIP_HSYNC_POLARITY;
654 else
655 val &= ~VIP_HSYNC_POLARITY;
656 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
657 VIP_PARSER_PORTA_0, val);
658 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
659 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
660 VIP_PARSER_PORTB_0);
661 if (polarity)
662 val |= VIP_HSYNC_POLARITY;
663 else
664 val &= ~VIP_HSYNC_POLARITY;
665 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
666 VIP_PARSER_PORTB_0, val);
667 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
668 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
669 VIP_PARSER_PORTB_0);
670 if (polarity)
671 val |= VIP_HSYNC_POLARITY;
672 else
673 val &= ~VIP_HSYNC_POLARITY;
674 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
675 VIP_PARSER_PORTB_0, val);
676 }
677 }
679 static void vip_set_actvid_polarity(struct vip_port *port, int polarity)
680 {
681 u32 val;
683 if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
684 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
685 VIP_PARSER_PORTA_0);
686 if (polarity)
687 val |= VIP_ACTVID_POLARITY;
688 else
689 val &= ~VIP_ACTVID_POLARITY;
691 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
692 VIP_PARSER_PORTA_0, val);
693 } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
694 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
695 VIP_PARSER_PORTA_0);
696 if (polarity)
697 val |= VIP_ACTVID_POLARITY;
698 else
699 val &= ~VIP_ACTVID_POLARITY;
700 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
701 VIP_PARSER_PORTA_0, val);
702 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
703 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
704 VIP_PARSER_PORTB_0);
705 if (polarity)
706 val |= VIP_ACTVID_POLARITY;
707 else
708 val &= ~VIP_ACTVID_POLARITY;
709 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
710 VIP_PARSER_PORTB_0, val);
711 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
712 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
713 VIP_PARSER_PORTB_0);
714 if (polarity)
715 val |= VIP_ACTVID_POLARITY;
716 else
717 val &= ~VIP_ACTVID_POLARITY;
719 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
720 VIP_PARSER_PORTB_0, val);
721 }
722 }
724 static void vip_set_actvid_hsync_n(struct vip_port *port, int enable)
725 {
726 u32 val;
728 if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
729 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
730 VIP_PARSER_PORTA_0);
731 if (enable)
732 val |= VIP_USE_ACTVID_HSYNC_ONLY;
733 else
734 val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
736 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
737 VIP_PARSER_PORTA_0, val);
738 } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
739 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
740 VIP_PARSER_PORTA_0);
741 if (enable)
742 val |= VIP_USE_ACTVID_HSYNC_ONLY;
743 else
744 val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
745 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
746 VIP_PARSER_PORTA_0, val);
747 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
748 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
749 VIP_PARSER_PORTB_0);
750 if (enable)
751 val |= VIP_USE_ACTVID_HSYNC_ONLY;
752 else
753 val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
754 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
755 VIP_PARSER_PORTB_0, val);
756 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
757 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
758 VIP_PARSER_PORTB_0);
759 if (enable)
760 val |= VIP_USE_ACTVID_HSYNC_ONLY;
761 else
762 val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
764 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
765 VIP_PARSER_PORTB_0, val);
766 }
767 }
769 static void vip_sync_type(struct vip_port *port, enum sync_types sync)
770 {
771 u32 val;
773 if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
774 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
775 VIP_PARSER_PORTA_0);
776 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
777 VIP_SYNC_TYPE_SHFT);
779 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
780 VIP_PARSER_PORTA_0, val);
781 } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
782 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
783 VIP_PARSER_PORTA_0);
784 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
785 VIP_SYNC_TYPE_SHFT);
787 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
788 VIP_PARSER_PORTA_0, val);
789 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
790 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
791 VIP_PARSER_PORTB_0);
792 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
793 VIP_SYNC_TYPE_SHFT);
795 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
796 VIP_PARSER_PORTB_0, val);
797 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
798 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
799 VIP_PARSER_PORTB_0);
800 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
801 VIP_SYNC_TYPE_SHFT);
803 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
804 VIP_PARSER_PORTB_0, val);
805 }
806 }
808 static void vip_set_data_interface(struct vip_port *port,
809 enum data_interface_modes mode)
810 {
811 u32 val = 0;
813 if (port->dev->slice_id == VIP_SLICE1) {
814 insert_field(&val, mode, VIP_DATA_INTERFACE_MODE_MASK,
815 VIP_DATA_INTERFACE_MODE_SHFT);
817 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET, val);
818 } else if (port->dev->slice_id == VIP_SLICE2) {
819 insert_field(&val, mode, VIP_DATA_INTERFACE_MODE_MASK,
820 VIP_DATA_INTERFACE_MODE_SHFT);
822 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET, val);
823 }
824 }
826 static void vip_reset_port(struct vip_port *port)
827 {
828 u32 val = 0;
830 if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
831 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
832 VIP_PARSER_PORTA_0, VIP_SW_RESET);
834 usleep_range(200, 250);
836 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
837 VIP_PARSER_PORTA_0);
839 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
840 VIP_PARSER_PORTA_0, 0);
841 } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
842 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
843 VIP_PARSER_PORTA_0, VIP_SW_RESET);
845 usleep_range(200, 250);
847 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
848 VIP_PARSER_PORTA_0);
850 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
851 VIP_PARSER_PORTA_0, 0);
853 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
854 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
855 VIP_PARSER_PORTB_0, VIP_SW_RESET);
857 usleep_range(200, 250);
859 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
860 VIP_PARSER_PORTB_0);
862 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
863 VIP_PARSER_PORTB_0, 0);
864 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
865 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
866 VIP_PARSER_PORTB_0, VIP_SW_RESET);
868 usleep_range(200, 250);
870 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
871 VIP_PARSER_PORTB_0);
873 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
874 VIP_PARSER_PORTB_0, 0);
875 }
876 }
878 static void vip_set_port_enable(struct vip_port *port, bool on)
879 {
880 u32 val = 0;
882 if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
883 if (on)
884 val |= VIP_PORT_ENABLE;
885 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
886 VIP_PARSER_PORTA_0, val);
887 } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
888 if (on)
889 val |= VIP_PORT_ENABLE;
890 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
891 VIP_PARSER_PORTA_0, val);
892 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
893 if (on)
894 val |= VIP_PORT_ENABLE;
895 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
896 VIP_PARSER_PORTB_0, val);
897 } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
898 if (on)
899 val |= VIP_PORT_ENABLE;
900 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
901 VIP_PARSER_PORTB_0, val);
902 }
903 }
905 static void vip_set_slice_path(struct vip_dev *dev,
906 enum data_path_select data_path)
907 {
908 u32 val = 0;
910 switch (data_path) {
911 case VIP_MULTI_CHANNEL_DATA_SELECT:
912 if (dev->slice_id == VIP_SLICE1) {
913 val |= VIP_MULTI_CHANNEL_SELECT;
914 insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
915 VIP_DATAPATH_SELECT_SHFT);
917 write_vreg(dev, VIP_VIP1_DATA_PATH_SELECT, val);
918 } else if (dev->slice_id == VIP_SLICE2) {
919 val |= VIP_MULTI_CHANNEL_SELECT;
920 insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
921 VIP_DATAPATH_SELECT_SHFT);
923 write_vreg(dev, VIP_VIP2_DATA_PATH_SELECT, val);
924 }
925 break;
926 case VIP_RGB_OUT_LO_DATA_SELECT:
927 if (dev->slice_id == VIP_SLICE1) {
928 val |= VIP_RGB_OUT_LO_SRC_SELECT;
929 insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
930 VIP_DATAPATH_SELECT_SHFT);
932 write_vreg(dev, VIP_VIP1_DATA_PATH_SELECT, val);
933 } else if (dev->slice_id == VIP_SLICE2) {
934 val |= VIP_RGB_OUT_LO_SRC_SELECT;
935 insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
936 VIP_DATAPATH_SELECT_SHFT);
938 write_vreg(dev, VIP_VIP2_DATA_PATH_SELECT, val);
939 }
940 break;
941 default:
942 BUG();
943 }
944 }
946 /*
947 * Return the vip_stream structure for a given struct file
948 */
949 static inline struct vip_stream *file2stream(struct file *file)
950 {
951 return video_drvdata(file);
953 }
955 /*
956 * Append a destination descriptor to the current descriptor list,
957 * setting up dma to the given srce.
958 */
959 static int add_out_dtd(struct vip_stream *stream, int srce_type)
960 {
961 struct vip_port *port = stream->port;
962 struct vip_dev *dev = port->dev;
963 struct vip_srce_info *sinfo = &srce_info[srce_type];
964 struct v4l2_rect *c_rect = &port->c_rect;
965 struct vip_fmt *fmt = port->fmt;
966 int channel, plane = 0;
967 int max_width, max_height;
968 dma_addr_t dma_addr;
969 u32 flags;
971 channel = sinfo->base_channel;
973 switch (srce_type) {
974 case VIP_SRCE_MULT_PORT:
975 case VIP_SRCE_MULT_ANC:
976 if (port->port_id == VIP_PORTB)
977 channel += VIP_CHAN_MULT_PORTB_OFFSET;
978 channel += stream->stream_id;
979 flags = 0;
980 break;
981 case VIP_SRCE_CHROMA:
982 plane = 1;
983 case VIP_SRCE_LUMA:
984 if (port->port_id == VIP_PORTB)
985 channel += VIP_CHAN_YUV_PORTB_OFFSET;
986 flags = port->flags;
987 break;
988 case VIP_SRCE_RGB:
989 if (port->port_id == VIP_PORTB)
990 channel += VIP_CHAN_RGB_PORTB_OFFSET;
991 flags = port->flags;
992 break;
993 default:
994 BUG();
995 }
997 if (dev->slice_id == VIP_SLICE2)
998 channel += VIP_CHAN_VIP2_OFFSET;
1000 /* This is just for initialization purposes.
1001 * The actual dma_addr will be configured in vpdma_update_dma_addr
1002 */
1003 dma_addr = (dma_addr_t)NULL;
1005 /*
1006 * Use VPDMA_MAX_SIZE1 or VPDMA_MAX_SIZE2 register for slice0/1
1007 */
1009 if (dev->slice_id == VIP_SLICE1) {
1010 vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE1,
1011 stream->width, stream->height);
1013 max_width = MAX_OUT_WIDTH_REG1;
1014 max_height = MAX_OUT_HEIGHT_REG1;
1015 } else {
1016 vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE2,
1017 stream->width, stream->height);
1019 max_width = MAX_OUT_WIDTH_REG2;
1020 max_height = MAX_OUT_HEIGHT_REG2;
1021 }
1023 /* Mark this channel to be cleared while cleaning up resources
1024 * This will make sure that an abort descriptor for this channel
1025 * would be submitted to VPDMA causing any ongoing transaction to be
1026 * aborted and cleanup the VPDMA FSM for this channel */
1027 dev->vpdma_channels[channel] = 1;
1029 vpdma_rawchan_add_out_dtd(&dev->desc_list, c_rect->width, c_rect,
1030 fmt->vpdma_fmt[plane], dma_addr, max_width, max_height,
1031 channel, flags);
1033 return 0;
1034 }
1036 /*
1037 * add_stream_dtds - prepares and starts DMA for pending transfers
1038 */
1039 static void add_stream_dtds(struct vip_stream *stream)
1040 {
1041 struct vip_port *port = stream->port;
1042 int srce_type;
1044 if (port->flags & FLAG_MULT_PORT)
1045 srce_type = VIP_SRCE_MULT_PORT;
1046 else if (port->flags & FLAG_MULT_ANC)
1047 srce_type = VIP_SRCE_MULT_ANC;
1048 else if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
1049 srce_type = VIP_SRCE_RGB;
1050 else
1051 srce_type = VIP_SRCE_LUMA;
1053 add_out_dtd(stream, srce_type);
1055 if (srce_type == VIP_SRCE_LUMA && port->fmt->coplanar)
1056 add_out_dtd(stream, VIP_SRCE_CHROMA);
1057 }
1059 static void enable_irqs(struct vip_dev *dev, int irq_num)
1060 {
1061 u32 reg_addr = VIP_INT0_ENABLE0_SET +
1062 VIP_INTC_INTX_OFFSET * irq_num;
1063 int list_num = dev->slice_id;
1065 write_sreg(dev->shared, reg_addr, 1 << (list_num * 2));
1067 vpdma_enable_list_complete_irq(dev->shared->vpdma,
1068 irq_num, list_num, true);
1069 }
1071 static void disable_irqs(struct vip_dev *dev, int irq_num)
1072 {
1073 u32 reg_addr = VIP_INT0_ENABLE0_CLR +
1074 VIP_INTC_INTX_OFFSET * irq_num;
1075 int list_num = dev->slice_id;
1077 write_sreg(dev->shared, reg_addr, 0xffffffff);
1079 vpdma_enable_list_complete_irq(dev->shared->vpdma,
1080 irq_num, list_num, false);
1081 }
1083 static void clear_irqs(struct vip_dev *dev, int irq_num)
1084 {
1085 u32 reg_addr = VIP_INT0_STATUS0_CLR +
1086 VIP_INTC_INTX_OFFSET * irq_num;
1088 write_sreg(dev->shared, reg_addr, 0xffffffff);
1090 vpdma_clear_list_stat(dev->shared->vpdma, irq_num, dev->slice_id);
1091 }
1093 static void populate_desc_list(struct vip_stream *stream)
1094 {
1095 struct vip_port *port = stream->port;
1096 struct vip_dev *dev = port->dev;
1097 unsigned int list_length;
1099 dev->desc_next = dev->desc_list.buf.addr;
1100 add_stream_dtds(stream);
1102 list_length = dev->desc_next - dev->desc_list.buf.addr;
1103 vpdma_map_desc_buf(dev->shared->vpdma, &dev->desc_list.buf);
1104 }
1106 /*
1107 * start_dma - adds descriptors to the dma list and submits them.
1108 * Should be called after a new vb is queued and on a vpdma list
1109 * completion interrupt.
1110 */
1111 static void start_dma(struct vip_dev *dev, struct vip_buffer *buf)
1112 {
1113 struct vpdma_data *vpdma = dev->shared->vpdma;
1114 dma_addr_t dma_addr;
1115 int drop_data;
1117 if (vpdma_list_busy(vpdma, dev->slice_id)) {
1118 vip_err(dev, "vpdma list busy, cannot post");
1119 return; /* nothing to do */
1120 }
1122 if (buf) {
1123 dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vb, 0);
1124 drop_data = 0;
1125 vip_dbg(4, dev, "start_dma: buf:0x%08x, vb:0x%08x, dma_addr:0x%08x\n",
1126 (unsigned int)buf, (unsigned int)&buf->vb, dma_addr);
1127 } else {
1128 dma_addr = (dma_addr_t)NULL;
1129 drop_data = 1;
1130 vip_dbg(4, dev, "start_dma: dropped\n");
1131 }
1133 vpdma_update_dma_addr(dev->shared->vpdma, &dev->desc_list,
1134 dma_addr, dev->write_desc, drop_data);
1135 vpdma_submit_descs(dev->shared->vpdma, &dev->desc_list, dev->slice_id);
1136 }
1138 static void vip_schedule_next_buffer(struct vip_stream *stream)
1139 {
1140 struct vip_dev *dev = stream->port->dev;
1141 struct vip_buffer *buf;
1142 unsigned long flags;
1144 spin_lock_irqsave(&dev->slock, flags);
1145 if (list_empty(&stream->vidq)) {
1146 vip_dbg(4, dev, "Dropping frame\n");
1147 if (list_empty(&stream->dropq)) {
1148 vip_err(dev, "No dropq buffer left!");
1149 spin_unlock_irqrestore(&dev->slock, flags);
1150 return;
1151 }
1152 buf = list_entry(stream->dropq.next,
1153 struct vip_buffer, list);
1155 buf->drop = true;
1156 list_move_tail(&buf->list, &dev->vip_bufs);
1157 buf = NULL;
1158 } else if (vb2_is_streaming(&stream->vb_vidq)) {
1159 buf = list_entry(stream->vidq.next,
1160 struct vip_buffer, list);
1161 buf->drop = false;
1162 list_move_tail(&buf->list, &dev->vip_bufs);
1163 vip_dbg(4, dev, "added next buffer\n");
1164 } else {
1165 vip_err(dev, "IRQ occurred when not streaming\n");
1166 if (list_empty(&stream->dropq)) {
1167 vip_err(dev, "No dropq buffer left!");
1168 spin_unlock_irqrestore(&dev->slock, flags);
1169 return;
1170 }
1171 buf = list_entry(stream->dropq.next,
1172 struct vip_buffer, list);
1173 buf->drop = true;
1174 list_move_tail(&buf->list, &dev->vip_bufs);
1175 buf = NULL;
1176 }
1178 spin_unlock_irqrestore(&dev->slock, flags);
1179 start_dma(dev, buf);
1180 }
1182 static void vip_process_buffer_complete(struct vip_stream *stream)
1183 {
1184 struct vip_dev *dev = stream->port->dev;
1185 struct vip_buffer *buf;
1186 struct vb2_buffer *vb = NULL;
1187 unsigned long flags, fld;
1189 buf = list_first_entry(&dev->vip_bufs, struct vip_buffer, list);
1191 if (stream->port->flags & FLAG_INTERLACED) {
1192 vpdma_unmap_desc_buf(dev->shared->vpdma, &dev->desc_list.buf);
1194 fld = dtd_get_field(dev->write_desc);
1195 stream->field = fld ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
1197 vpdma_map_desc_buf(dev->shared->vpdma, &dev->desc_list.buf);
1198 }
1200 if (buf) {
1201 vip_dbg(4, dev, "vip buffer complete 0x%x, 0x%x\n",
1202 (unsigned int)buf, buf->drop);
1204 vb = &buf->vb;
1205 vb->v4l2_buf.field = stream->field;
1206 vb->v4l2_buf.sequence = stream->sequence;
1207 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
1209 if (buf->drop) {
1210 spin_lock_irqsave(&dev->slock, flags);
1211 list_move_tail(&buf->list, &stream->dropq);
1212 spin_unlock_irqrestore(&dev->slock, flags);
1213 } else {
1214 spin_lock_irqsave(&dev->slock, flags);
1215 list_del(&buf->list);
1216 spin_unlock_irqrestore(&dev->slock, flags);
1217 vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1218 }
1219 } else {
1220 BUG();
1221 }
1223 stream->sequence++;
1224 }
1226 static irqreturn_t vip_irq(int irq_vip, void *data)
1227 {
1228 struct vip_dev *dev = (struct vip_dev *)data;
1229 struct vip_stream *stream;
1230 int list_num = dev->slice_id;
1231 int irq_num = dev->slice_id;
1232 u32 irqst, reg_addr;
1234 if (!dev->shared)
1235 return IRQ_HANDLED;
1237 reg_addr = VIP_INT0_STATUS0 +
1238 VIP_INTC_INTX_OFFSET * irq_num;
1239 irqst = read_sreg(dev->shared, reg_addr);
1241 vip_dbg(8, dev, "IRQ %d VIP_INT%d_STATUS0 0x%x\n",
1242 irq_vip, irq_num, irqst);
1243 if (irqst) {
1244 vpdma_clear_list_stat(dev->shared->vpdma, irq_num, list_num);
1246 reg_addr = VIP_INT0_STATUS0_CLR +
1247 VIP_INTC_INTX_OFFSET * irq_num;
1248 write_sreg(dev->shared, reg_addr, irqst);
1249 }
1251 stream = dev->ports[0]->cap_streams[0];
1252 if (dev->num_skip_irq)
1253 dev->num_skip_irq--;
1254 else
1255 vip_process_buffer_complete(stream);
1257 vip_schedule_next_buffer(stream);
1259 return IRQ_HANDLED;
1260 }
1262 /*
1263 * video ioctls
1264 */
1265 static struct v4l2_mbus_framefmt *
1266 vip_video_pix_to_mbus(const struct v4l2_pix_format *pix,
1267 struct v4l2_mbus_framefmt *mbus)
1268 {
1269 unsigned int i;
1271 memset(mbus, 0, sizeof(*mbus));
1272 mbus->width = pix->width;
1273 mbus->height = pix->height;
1275 mbus->code = V4L2_MBUS_FMT_YUYV8_2X8;
1276 for (i = 0; i < ARRAY_SIZE(vip_formats) - 1; ++i) {
1277 if (vip_formats[i].fourcc == pix->pixelformat) {
1278 mbus->code = vip_formats[i].code;
1279 break;
1280 }
1281 }
1283 mbus->colorspace = pix->colorspace;
1284 mbus->field = pix->field;
1286 return mbus;
1287 }
1289 static int vip_querycap(struct file *file, void *priv,
1290 struct v4l2_capability *cap)
1291 {
1292 strncpy(cap->driver, VIP_MODULE_NAME, sizeof(cap->driver) - 1);
1293 strncpy(cap->card, VIP_MODULE_NAME, sizeof(cap->card) - 1);
1294 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1295 VIP_MODULE_NAME);
1296 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
1297 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1298 return 0;
1299 }
1301 static int vip_enuminput(struct file *file, void *priv,
1302 struct v4l2_input *inp)
1303 {
1304 struct vip_stream *stream = file2stream(file);
1306 if (inp->index)
1307 return -EINVAL;
1309 inp->type = V4L2_INPUT_TYPE_CAMERA;
1310 sprintf(inp->name, "camera %u", stream->vfd->num);
1312 return 0;
1313 }
1315 static int vip_g_input(struct file *file, void *priv, unsigned int *i)
1316 {
1317 *i = 0;
1318 return 0;
1319 }
1321 static int vip_s_input(struct file *file, void *priv, unsigned int i)
1322 {
1323 if (i != 0)
1324 return -EINVAL;
1325 return 0;
1326 }
1328 static int vip_querystd(struct file *file, void *fh, v4l2_std_id *std)
1329 {
1330 struct vip_stream *stream = file2stream(file);
1331 struct vip_dev *dev = stream->port->dev;
1333 v4l2_subdev_call(dev->sensor, video, querystd, std);
1334 return 0;
1335 }
1337 static int vip_g_std(struct file *file, void *fh, v4l2_std_id *std)
1338 {
1339 struct vip_stream *stream = file2stream(file);
1340 struct vip_dev *dev = stream->port->dev;
1342 *std = 0;
1343 v4l2_subdev_call(dev->sensor, video, g_std_output, std);
1344 return 0;
1345 }
1347 static int vip_s_std(struct file *file, void *fh, v4l2_std_id std)
1348 {
1349 struct vip_stream *stream = file2stream(file);
1350 struct vip_dev *dev = stream->port->dev;
1352 v4l2_subdev_call(dev->sensor, video, s_std_output, std);
1353 return 0;
1354 }
1356 static int vip_enum_fmt_vid_cap(struct file *file, void *priv,
1357 struct v4l2_fmtdesc *f)
1358 {
1359 struct vip_stream *stream = file2stream(file);
1360 struct vip_dev *dev = stream->port->dev;
1361 struct vip_fmt *fmt;
1363 vip_dbg(3, dev, "enum_fmt index:%d\n", f->index);
1364 if (f->index >= dev->num_active_fmt)
1365 return -EINVAL;
1367 fmt = dev->active_fmt[f->index];
1369 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
1370 f->pixelformat = fmt->fourcc;
1372 vip_dbg(3, dev, "enum_fmt fourcc:%s description:%s\n",
1373 fourcc_to_str(f->pixelformat), f->description);
1375 return 0;
1376 }
1378 /*
1379 * TODO: Change from hard coding values to reading these through
1380 * IOCTLS directly from sensor
1381 */
1383 static int vip_enum_framesizes(struct file *file, void *priv,
1384 struct v4l2_frmsizeenum *f)
1385 {
1386 struct vip_stream *stream = file2stream(file);
1387 struct vip_dev *dev = stream->port->dev;
1388 struct vip_fmt *fmt;
1389 int ret;
1391 fmt = find_active_format_by_pix(dev, f->pixel_format);
1392 if (!fmt)
1393 return -EINVAL;
1395 ret = v4l2_subdev_call(dev->sensor, video, enum_framesizes, f);
1396 if (ret)
1397 vip_dbg(1, dev, "enum_framesizes failed in subdev\n");
1399 return ret;
1400 }
1402 static int vip_enum_frameintervals(struct file *file, void *priv,
1403 struct v4l2_frmivalenum *f)
1404 {
1405 struct vip_stream *stream = file2stream(file);
1406 struct vip_dev *dev = stream->port->dev;
1407 struct v4l2_frmsizeenum fsize;
1408 struct vip_fmt *fmt;
1409 int ret;
1411 if (f->index)
1412 return -EINVAL;
1414 fmt = find_active_format_by_pix(dev, f->pixel_format);
1415 if (!fmt)
1416 return -EINVAL;
1418 /* check for valid width/height */
1419 ret = 0;
1420 for (fsize.index = 0; ; fsize.index++) {
1421 ret = v4l2_subdev_call(dev->sensor, video,
1422 enum_framesizes, &fsize);
1423 if (ret) {
1424 if (fsize.index == 0)
1425 vip_dbg(1, dev, "enum_frameinterval failed on the first enum_framesize\n");
1426 return -EINVAL;
1427 }
1429 if (fsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1430 if ((f->width == fsize.discrete.width) &&
1431 (f->height == fsize.discrete.height))
1432 break;
1433 } else if ((fsize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) ||
1434 (fsize.type == V4L2_FRMSIZE_TYPE_STEPWISE)) {
1435 if ((f->width >= fsize.stepwise.min_width) &&
1436 (f->width <= fsize.stepwise.max_width) &&
1437 (f->height >= fsize.stepwise.min_height) &&
1438 (f->height <= fsize.stepwise.max_height))
1439 break;
1440 } else
1441 return -EINVAL;
1442 }
1444 f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1445 f->discrete.numerator = 1;
1446 f->discrete.denominator = 30;
1448 return 0;
1449 }
1451 static int vip_s_parm(struct file *file, void *priv,
1452 struct v4l2_streamparm *parm)
1453 {
1454 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1455 return -EINVAL;
1457 parm->parm.capture.timeperframe.numerator = 1;
1458 parm->parm.capture.timeperframe.denominator = 30;
1460 return 0;
1461 }
1463 static int vip_try_fmt_vid_cap(struct file *file, void *priv,
1464 struct v4l2_format *f)
1465 {
1466 struct vip_stream *stream = file2stream(file);
1467 struct vip_dev *dev = stream->port->dev;
1468 struct vip_fmt *fmt = find_active_format_by_pix(dev,
1469 f->fmt.pix.pixelformat);
1470 enum v4l2_field field;
1472 if (!fmt) {
1473 vip_err(dev,
1474 "Fourcc format (0x%08x) invalid.\n",
1475 f->fmt.pix.pixelformat);
1476 return -EINVAL;
1477 }
1479 field = f->fmt.pix.field;
1481 if (field == V4L2_FIELD_ANY)
1482 field = V4L2_FIELD_NONE;
1483 else if (V4L2_FIELD_NONE != field && V4L2_FIELD_ALTERNATE != field)
1484 return -EINVAL;
1486 f->fmt.pix.field = field;
1488 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W, W_ALIGN,
1489 &f->fmt.pix.height, MIN_H, MAX_H, H_ALIGN,
1490 S_ALIGN);
1492 f->fmt.pix.bytesperline = f->fmt.pix.width *
1493 (fmt->vpdma_fmt[0]->depth >> 3);
1494 f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
1495 VPDMA_STRIDE_ALIGN);
1496 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.width *
1497 (fmt->vpdma_fmt[0]->depth +
1498 (fmt->coplanar ? fmt->vpdma_fmt[1]->depth : 0)) >> 3;
1499 f->fmt.pix.colorspace = fmt->colorspace;
1500 f->fmt.pix.priv = 0;
1502 vip_dbg(3, dev, "try_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1503 fourcc_to_str(f->fmt.pix.pixelformat),
1504 f->fmt.pix.width, f->fmt.pix.height,
1505 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1507 return 0;
1508 }
1510 static int vip_g_fmt_vid_cap(struct file *file, void *priv,
1511 struct v4l2_format *f)
1512 {
1513 struct vip_stream *stream = file2stream(file);
1514 struct vip_port *port = stream->port;
1515 struct vip_dev *dev = stream->port->dev;
1516 struct v4l2_mbus_framefmt mbus_fmt;
1517 struct vip_fmt *fmt;
1518 struct v4l2_format try_f;
1519 int ret;
1521 /* Use last known values or defaults */
1522 f->fmt.pix.width = stream->width;
1523 f->fmt.pix.height = stream->height;
1524 f->fmt.pix.pixelformat = port->fmt->fourcc;
1525 f->fmt.pix.field = stream->sup_field;
1526 f->fmt.pix.colorspace = port->fmt->colorspace;
1527 f->fmt.pix.bytesperline = stream->bytesperline;
1528 f->fmt.pix.sizeimage = stream->sizeimage;
1530 /* Check with the subdevice */
1531 ret = v4l2_subdev_call(dev->sensor, video, g_mbus_fmt, &mbus_fmt);
1532 if (ret)
1533 vip_dbg(1, dev, "g_mbus_fmt failed in subdev\n");
1535 fmt = find_active_format_by_code(dev, mbus_fmt.code);
1536 if (!fmt) {
1537 vip_err(dev,
1538 "mbus_code (0x%08x) invalid.\n",
1539 mbus_fmt.code);
1540 return -EINVAL;
1541 }
1543 vip_dbg(3, dev, "g_fmt subdev mbus_code: %04X fourcc:%s size: %dx%d\n",
1544 fmt->code,
1545 fourcc_to_str(fmt->fourcc),
1546 mbus_fmt.width, mbus_fmt.height);
1548 /*
1549 * Run a try_fmt call to properly calculate
1550 * the sizeimage and bytesperline values
1551 * in case the defaults were not accurate.
1552 */
1553 try_f = *f;
1554 try_f.fmt.pix.pixelformat = fmt->fourcc;
1555 try_f.fmt.pix.width = mbus_fmt.width;
1556 try_f.fmt.pix.height = mbus_fmt.height;
1557 try_f.fmt.pix.field = mbus_fmt.field;
1558 try_f.fmt.pix.colorspace = mbus_fmt.colorspace;
1560 ret = vip_try_fmt_vid_cap(file, priv, &try_f);
1561 if (ret)
1562 return ret;
1564 if (port->fmt != fmt) {
1565 vip_dbg(1, dev, "g_fmt fmt mismatch port->fmt:%p fmt:%p\n",
1566 port->fmt, fmt);
1567 vip_dbg(1, dev, "g_fmt port->fmt->fourcc:%s\n",
1568 fourcc_to_str(port->fmt->fourcc));
1569 vip_dbg(1, dev, "fmt->fourcc:%s\n",
1570 fourcc_to_str(fmt->fourcc));
1571 vip_dbg(1, dev, "g_fmt port->fmt->name:%s fmt->name:%s\n",
1572 port->fmt->name, fmt->name);
1573 port->fmt = fmt;
1574 }
1575 /*
1576 * Since everything looks correct update
1577 * the local copy as well to make sure we are consistent
1578 */
1579 *f = try_f;
1580 stream->width = f->fmt.pix.width;
1581 stream->height = f->fmt.pix.height;
1582 stream->sup_field = f->fmt.pix.field;
1583 if (stream->sup_field == V4L2_FIELD_ALTERNATE)
1584 port->flags |= FLAG_INTERLACED;
1585 else
1586 port->flags &= ~FLAG_INTERLACED;
1587 stream->bytesperline = f->fmt.pix.bytesperline;
1588 stream->sizeimage = f->fmt.pix.sizeimage;
1589 port->c_rect.left = 0;
1590 port->c_rect.top = 0;
1591 port->c_rect.width = stream->width;
1592 port->c_rect.height = stream->height;
1594 vip_dbg(3, dev, "g_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1595 fourcc_to_str(f->fmt.pix.pixelformat),
1596 f->fmt.pix.width, f->fmt.pix.height,
1597 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1599 return 0;
1600 }
1602 /*
1603 * Set the registers that are modified when the video format changes.
1604 */
1605 static void set_fmt_params(struct vip_stream *stream)
1606 {
1607 struct vip_dev *dev = stream->port->dev;
1609 stream->sequence = 0;
1610 stream->field = V4L2_FIELD_TOP;
1612 if (stream->port->fmt->colorspace == V4L2_COLORSPACE_SRGB) {
1613 vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT);
1614 /* Set alpha component in background color */
1615 vpdma_set_bg_color(dev->shared->vpdma,
1616 (struct vpdma_data_format *)
1617 stream->port->fmt->vpdma_fmt[0],
1618 0xff);
1619 }
1620 }
1622 int vip_s_fmt_vid_cap(struct file *file, void *priv,
1623 struct v4l2_format *f)
1624 {
1625 struct vip_stream *stream = file2stream(file);
1626 struct vip_port *port = stream->port;
1627 struct vip_dev *dev = port->dev;
1628 struct v4l2_subdev_format sfmt;
1629 struct v4l2_mbus_framefmt *mf;
1630 int ret;
1632 vip_dbg(3, dev, "s_fmt input fourcc:%s size: %dx%d\n",
1633 fourcc_to_str(f->fmt.pix.pixelformat),
1634 f->fmt.pix.width, f->fmt.pix.height);
1636 ret = vip_try_fmt_vid_cap(file, priv, f);
1637 if (ret)
1638 return ret;
1640 vip_dbg(3, dev, "s_fmt try_fmt fourcc:%s size: %dx%d\n",
1641 fourcc_to_str(f->fmt.pix.pixelformat),
1642 f->fmt.pix.width, f->fmt.pix.height);
1644 if (vb2_is_busy(&stream->vb_vidq)) {
1645 vip_err(dev, "%s queue busy\n", __func__);
1646 return -EBUSY;
1647 }
1649 port->fmt = find_active_format_by_pix(dev,
1650 f->fmt.pix.pixelformat);
1651 stream->width = f->fmt.pix.width;
1652 stream->height = f->fmt.pix.height;
1653 if (port->fmt->colorspace != f->fmt.pix.colorspace)
1654 vip_dbg(1, dev, "s_fmt colorspace mismatch port->fmt %d f->fmt %d\n",
1655 port->fmt->colorspace, f->fmt.pix.colorspace);
1657 stream->bytesperline = f->fmt.pix.bytesperline;
1658 stream->sizeimage = f->fmt.pix.sizeimage;
1659 stream->sup_field = f->fmt.pix.field;
1661 port->c_rect.left = 0;
1662 port->c_rect.top = 0;
1663 port->c_rect.width = stream->width;
1664 port->c_rect.height = stream->height;
1666 if (stream->sup_field == V4L2_FIELD_ALTERNATE)
1667 port->flags |= FLAG_INTERLACED;
1668 else
1669 port->flags &= ~FLAG_INTERLACED;
1671 vip_dbg(1, dev,
1672 "Setting format for type %d, wxh: %dx%d, fourcc:%s\n",
1673 f->type, stream->width, stream->height,
1674 fourcc_to_str(port->fmt->fourcc));
1676 vip_dbg(3, dev, "s_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1677 fourcc_to_str(f->fmt.pix.pixelformat),
1678 f->fmt.pix.width, f->fmt.pix.height,
1679 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1681 mf = vip_video_pix_to_mbus(&f->fmt.pix, &sfmt.format);
1683 vip_dbg(3, dev, "s_fmt pix_to_mbus mbus_code: %04X size: %dx%d\n",
1684 mf->code,
1685 mf->width, mf->height);
1687 ret = v4l2_subdev_call(dev->sensor, video, try_mbus_fmt, mf);
1688 if (ret) {
1689 vip_dbg(1, dev, "try_mbus_fmt failed in subdev\n");
1690 return ret;
1691 }
1692 vip_dbg(3, dev, "s_fmt subdev try_fmt mbus_code: %04X size: %dx%d\n",
1693 mf->code,
1694 mf->width, mf->height);
1695 ret = v4l2_subdev_call(dev->sensor, video, s_mbus_fmt, mf);
1696 if (ret) {
1697 vip_dbg(1, dev, "s_mbus_fmt failed in subdev\n");
1698 return ret;
1699 }
1700 vip_dbg(3, dev, "s_fmt subdev s_fmt mbus_code: %04X size: %dx%d\n",
1701 mf->code,
1702 mf->width, mf->height);
1704 return 0;
1705 }
1706 EXPORT_SYMBOL(vip_s_fmt_vid_cap);
1708 static int vip_g_selection(struct file *file, void *fh,
1709 struct v4l2_selection *s)
1710 {
1711 struct vip_stream *stream = file2stream(file);
1713 switch (s->target) {
1714 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1715 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1716 case V4L2_SEL_TGT_CROP_BOUNDS:
1717 case V4L2_SEL_TGT_CROP_DEFAULT:
1718 s->r.left = 0;
1719 s->r.top = 0;
1720 s->r.width = stream->width;
1721 s->r.height = stream->height;
1722 return 0;
1724 case V4L2_SEL_TGT_COMPOSE:
1725 case V4L2_SEL_TGT_CROP:
1726 s->r = stream->port->c_rect;
1727 return 0;
1728 }
1730 return -EINVAL;
1731 }
1733 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1734 {
1735 if (a->left < b->left || a->top < b->top)
1736 return 0;
1737 if (a->left + a->width > b->left + b->width)
1738 return 0;
1739 if (a->top + a->height > b->top + b->height)
1740 return 0;
1742 return 1;
1743 }
1745 static int vip_s_selection(struct file *file, void *fh,
1746 struct v4l2_selection *s)
1747 {
1748 struct vip_stream *stream = file2stream(file);
1749 struct vip_port *port = stream->port;
1750 struct v4l2_rect r = s->r;
1752 v4l_bound_align_image(&r.width, 0, stream->width, 0,
1753 &r.height, 0, stream->height, 0, 0);
1755 r.left = clamp_t(unsigned int, r.left, 0, stream->width - r.width);
1756 r.top = clamp_t(unsigned int, r.top, 0, stream->height - r.height);
1758 if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
1759 return -ERANGE;
1761 if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
1762 return -ERANGE;
1764 s->r = stream->port->c_rect = r;
1766 set_fmt_params(stream);
1768 vip_dbg(1, port->dev, "cropped (%d,%d)/%dx%d of %dx%d\n",
1769 r.left, r.top, r.width, r.height,
1770 stream->width, stream->height);
1772 return 0;
1773 }
1775 static long vip_ioctl_default(struct file *file, void *fh, bool valid_prio,
1776 unsigned int cmd, void *arg)
1777 {
1778 struct vip_stream *stream = file2stream(file);
1779 struct vip_port *port = stream->port;
1781 if (!valid_prio) {
1782 vip_err(port->dev, "%s device busy\n", __func__);
1783 return -EBUSY;
1784 }
1786 switch (cmd) {
1787 default:
1788 return -ENOTTY;
1789 }
1790 }
1792 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
1793 .vidioc_querycap = vip_querycap,
1794 .vidioc_enum_input = vip_enuminput,
1795 .vidioc_g_input = vip_g_input,
1796 .vidioc_s_input = vip_s_input,
1798 .vidioc_querystd = vip_querystd,
1799 .vidioc_g_std = vip_g_std,
1800 .vidioc_s_std = vip_s_std,
1802 .vidioc_enum_fmt_vid_cap = vip_enum_fmt_vid_cap,
1803 .vidioc_g_fmt_vid_cap = vip_g_fmt_vid_cap,
1804 .vidioc_try_fmt_vid_cap = vip_try_fmt_vid_cap,
1805 .vidioc_s_fmt_vid_cap = vip_s_fmt_vid_cap,
1807 .vidioc_enum_frameintervals = vip_enum_frameintervals,
1808 .vidioc_enum_framesizes = vip_enum_framesizes,
1809 .vidioc_s_parm = vip_s_parm,
1811 .vidioc_g_selection = vip_g_selection,
1812 .vidioc_s_selection = vip_s_selection,
1813 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1814 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1815 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1816 .vidioc_querybuf = vb2_ioctl_querybuf,
1817 .vidioc_qbuf = vb2_ioctl_qbuf,
1818 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1820 .vidioc_streamon = vb2_ioctl_streamon,
1821 .vidioc_streamoff = vb2_ioctl_streamoff,
1822 .vidioc_log_status = v4l2_ctrl_log_status,
1823 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1824 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1825 .vidioc_default = vip_ioctl_default,
1826 };
1828 /*
1829 * Videobuf operations
1830 */
1831 static int vip_queue_setup(struct vb2_queue *vq,
1832 const struct v4l2_format *fmt,
1833 unsigned int *nbuffers, unsigned int *nplanes,
1834 unsigned int sizes[], void *alloc_ctxs[])
1835 {
1836 struct vip_stream *stream = vb2_get_drv_priv(vq);
1837 struct vip_dev *dev = stream->port->dev;
1839 *nplanes = 1;
1840 sizes[0] = stream->sizeimage;
1841 alloc_ctxs[0] = dev->alloc_ctx;
1842 vip_dbg(1, dev, "get %d buffer(s) of size %d each.\n",
1843 *nbuffers, sizes[0]);
1845 return 0;
1846 }
1848 static int vip_buf_prepare(struct vb2_buffer *vb)
1849 {
1850 struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
1851 struct vip_dev *dev = stream->port->dev;
1853 if (vb2_plane_size(vb, 0) < stream->sizeimage) {
1854 vip_dbg(1, dev,
1855 "%s data will not fit into plane (%lu < %lu)\n",
1856 __func__, vb2_plane_size(vb, 0),
1857 (long)stream->sizeimage);
1858 return -EINVAL;
1859 }
1861 vb2_set_plane_payload(vb, 0, stream->sizeimage);
1863 return 0;
1864 }
1866 static void vip_buf_queue(struct vb2_buffer *vb)
1867 {
1868 struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
1869 struct vip_dev *dev = stream->port->dev;
1870 struct vip_buffer *buf = container_of(vb, struct vip_buffer, vb);
1871 unsigned long flags;
1873 spin_lock_irqsave(&dev->slock, flags);
1874 list_add_tail(&buf->list, &stream->vidq);
1875 spin_unlock_irqrestore(&dev->slock, flags);
1876 }
1878 static int vip_start_streaming(struct vb2_queue *vq, unsigned int count)
1879 {
1880 struct vip_stream *stream = vb2_get_drv_priv(vq);
1881 struct vip_port *port = stream->port;
1882 struct vip_dev *dev = port->dev;
1883 struct vip_buffer *buf;
1884 unsigned long flags;
1885 int ret;
1887 set_fmt_params(stream);
1888 vip_setup_parser(dev->ports[0]);
1890 buf = list_entry(stream->vidq.next,
1891 struct vip_buffer, list);
1893 vip_dbg(2, dev, "start_streaming: buf 0x%x %d\n",
1894 (unsigned int)buf, count);
1895 buf->drop = false;
1896 stream->sequence = 0;
1897 stream->field = V4L2_FIELD_TOP;
1899 if (dev->sensor) {
1900 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1901 if (ret) {
1902 vip_dbg(1, dev, "stream on failed in subdev\n");
1903 return ret;
1904 }
1905 }
1907 populate_desc_list(stream);
1909 /* The first few VPDMA ListComplete interrupts fire pretty quiclky
1910 * until the internal VPDMA descriptor fifo is full.
1911 * The subsequent ListComplete interrupts will fire at the actual
1912 * capture frame rate. The first few interrupts are therefore used
1913 * only to queue up descriptors, and then they will also be used
1914 * as End of Frame (EOF) event
1915 */
1916 dev->num_skip_irq = VIP_VPDMA_FIFO_SIZE;
1918 spin_lock_irqsave(&dev->slock, flags);
1919 if (vpdma_list_busy(dev->shared->vpdma, dev->slice_id)) {
1920 spin_unlock_irqrestore(&dev->slock, flags);
1921 vpdma_unmap_desc_buf(dev->shared->vpdma, &dev->desc_list.buf);
1922 vpdma_reset_desc_list(&dev->desc_list);
1923 return -EBUSY;
1924 }
1926 list_move_tail(&buf->list, &dev->vip_bufs);
1927 spin_unlock_irqrestore(&dev->slock, flags);
1929 vip_dbg(2, dev, "start_streaming: start_dma buf 0x%x\n",
1930 (unsigned int)buf);
1931 start_dma(dev, buf);
1933 /* We enable the irq after posting the vpdma descriptor
1934 * to prevent sprurious interrupt coming in before the
1935 * vb2 layer is completely ready to handle them
1936 * otherwise the vb2_streaming test would fail early on
1937 */
1938 enable_irqs(dev, dev->slice_id);
1939 return 0;
1940 }
1942 /*
1943 * Abort streaming and wait for last buffer
1944 */
1945 static int vip_stop_streaming(struct vb2_queue *vq)
1946 {
1947 struct vip_stream *stream = vb2_get_drv_priv(vq);
1948 struct vip_port *port = stream->port;
1949 struct vip_dev *dev = port->dev;
1950 struct vip_buffer *buf;
1951 int ret;
1953 if (dev->sensor) {
1954 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 0);
1955 if (ret)
1956 vip_dbg(1, dev, "stream on failed in subdev\n");
1957 }
1959 disable_irqs(dev, dev->slice_id);
1960 clear_irqs(dev, dev->slice_id);
1962 /* release all active buffers */
1963 while (!list_empty(&dev->vip_bufs)) {
1964 buf = list_entry(dev->vip_bufs.next, struct vip_buffer, list);
1965 list_del(&buf->list);
1966 if (buf->drop == 1)
1967 list_add_tail(&buf->list, &stream->dropq);
1968 else
1969 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1970 }
1971 while (!list_empty(&stream->vidq)) {
1972 buf = list_entry(stream->vidq.next, struct vip_buffer, list);
1973 list_del(&buf->list);
1974 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1975 }
1977 if (!vb2_is_streaming(vq))
1978 return 0;
1980 vpdma_unmap_desc_buf(dev->shared->vpdma, &dev->desc_list.buf);
1981 vpdma_reset_desc_list(&dev->desc_list);
1983 return 0;
1984 }
1986 /*
1987 * Lock access to the device
1988 */
1989 static void vip_lock(struct vb2_queue *vq)
1990 {
1991 struct vip_stream *stream = vb2_get_drv_priv(vq);
1993 mutex_lock(&stream->port->dev->mutex);
1994 }
1996 static void vip_unlock(struct vb2_queue *vq)
1997 {
1998 struct vip_stream *stream = vb2_get_drv_priv(vq);
1999 mutex_unlock(&stream->port->dev->mutex);
2000 }
2002 static struct vb2_ops vip_video_qops = {
2003 .queue_setup = vip_queue_setup,
2004 .buf_prepare = vip_buf_prepare,
2005 .buf_queue = vip_buf_queue,
2006 .start_streaming = vip_start_streaming,
2007 .stop_streaming = vip_stop_streaming,
2008 .wait_prepare = vip_unlock,
2009 .wait_finish = vip_lock,
2010 };
2012 /*
2013 * File operations
2014 */
2016 static int vip_init_dev(struct vip_dev *dev)
2017 {
2018 int ret;
2020 if (dev->num_ports != 0)
2021 goto done;
2023 ret = vpdma_create_desc_list(&dev->desc_list, VIP_DESC_LIST_SIZE,
2024 VPDMA_LIST_TYPE_NORMAL);
2026 if (ret != 0)
2027 return ret;
2029 dev->write_desc = (struct vpdma_dtd *)dev->desc_list.buf.addr
2030 + 15;
2031 vip_set_clock_enable(dev, 1);
2032 done:
2033 dev->num_ports++;
2035 return 0;
2036 }
2038 static void vip_release_dev(struct vip_dev *dev)
2039 {
2040 vpdma_unmap_desc_buf(dev->shared->vpdma, &dev->desc_list.buf);
2041 vpdma_free_desc_buf(&dev->desc_list.buf);
2042 vpdma_free_desc_list(&dev->desc_list);
2044 /*
2045 * On last close, disable clocks to conserve power
2046 */
2048 if (--dev->num_ports == 0)
2049 vip_set_clock_enable(dev, 0);
2050 }
2052 static int vip_setup_parser(struct vip_port *port)
2053 {
2054 struct vip_dev *dev = port->dev;
2055 struct v4l2_of_endpoint *endpoint = dev->endpoint;
2056 int iface = DUAL_8B_INTERFACE;
2057 int sync_type;
2058 unsigned int flags;
2060 vip_reset_port(port);
2061 vip_set_port_enable(port, 1);
2063 if (endpoint->bus_type == V4L2_MBUS_BT656) {
2064 iface = DUAL_8B_INTERFACE;
2066 /* Ideally, this should come from sensor
2067 port->fmt can be anything once CSC is enabled */
2068 if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
2069 sync_type = EMBEDDED_SYNC_SINGLE_RGB_OR_YUV444;
2070 else {
2071 switch (endpoint->bus.parallel.num_channels) {
2072 case 4:
2073 sync_type = EMBEDDED_SYNC_4X_MULTIPLEXED_YUV422;
2074 break;
2075 case 2:
2076 sync_type = EMBEDDED_SYNC_2X_MULTIPLEXED_YUV422;
2077 break;
2078 case 1:
2079 default:
2080 sync_type = EMBEDDED_SYNC_SINGLE_YUV422;
2081 }
2082 }
2084 } else if (endpoint->bus_type == V4L2_MBUS_PARALLEL) {
2085 switch (endpoint->bus.parallel.bus_width) {
2086 case 24:
2087 iface = SINGLE_24B_INTERFACE;
2088 break;
2089 case 16:
2090 iface = SINGLE_16B_INTERFACE;
2091 break;
2092 case 8:
2093 default:
2094 iface = DUAL_8B_INTERFACE;
2095 }
2097 if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
2098 sync_type = DISCRETE_SYNC_SINGLE_RGB_24B;
2099 else
2100 sync_type = DISCRETE_SYNC_SINGLE_YUV422;
2102 flags = endpoint->bus.parallel.flags;
2103 if (flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH |
2104 V4L2_MBUS_HSYNC_ACTIVE_LOW))
2105 vip_set_vsync_polarity(port,
2106 flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH ? 1 : 0);
2108 if (flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH |
2109 V4L2_MBUS_VSYNC_ACTIVE_LOW))
2110 vip_set_hsync_polarity(port,
2111 flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH ? 1 : 0);
2113 if (flags & (V4L2_MBUS_PCLK_SAMPLE_RISING |
2114 V4L2_MBUS_PCLK_SAMPLE_FALLING))
2115 vip_set_pclk_polarity(port,
2116 flags & V4L2_MBUS_PCLK_SAMPLE_RISING ? 1 : 0);
2118 vip_xtra_set_repack_sel(port, 0);
2119 vip_set_actvid_hsync_n(port, 0);
2120 vip_set_actvid_polarity(port, 1);
2121 vip_set_discrete_basic_mode(port);
2123 } else {
2124 vip_err(dev, "Device doesn't support CSI2");
2125 return -EINVAL;
2126 }
2128 vip_set_data_interface(port, iface);
2129 vip_sync_type(port, sync_type);
2130 return 0;
2131 }
2133 static int vip_init_port(struct vip_port *port)
2134 {
2135 int ret;
2137 if (port->num_streams != 0)
2138 goto done;
2140 ret = vip_init_dev(port->dev);
2141 if (ret)
2142 goto done;
2144 port->fmt = port->dev->active_fmt[0];
2145 port->src_colorspace = port->fmt->colorspace;
2146 port->c_rect.left = 0;
2147 port->c_rect.top = 0;
2149 done:
2150 port->num_streams++;
2151 return 0;
2152 }
2154 static void vip_release_port(struct vip_port *port)
2155 {
2156 struct vip_dev *dev = port->dev;
2157 int ch, size = 0;
2159 /* Create a list of channels to be cleared */
2160 for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++) {
2161 if (dev->vpdma_channels[ch] == 1) {
2162 dev->vpdma_channels[size++] = ch;
2163 vip_dbg(2, dev, "Clear channel no: %d\n", ch);
2164 }
2165 }
2167 /* Clear all the used channels for the list */
2168 vpdma_list_cleanup(dev->shared->vpdma, dev->slice_id,
2169 dev->vpdma_channels, size);
2171 for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++)
2172 dev->vpdma_channels[ch] = 0;
2174 if (--port->num_streams == 0)
2175 vip_release_dev(port->dev);
2176 }
2178 int vip_open(struct file *file)
2179 {
2180 struct vip_stream *stream = video_drvdata(file);
2181 struct vip_port *port = stream->port;
2182 struct vip_dev *dev = port->dev;
2183 struct v4l2_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
2184 int ret = 0;
2186 vip_dbg(2, dev, "vip_open\n");
2188 file->private_data = fh;
2189 if (fh == NULL)
2190 return -ENOMEM;
2192 mutex_lock(&dev->mutex);
2194 v4l2_fh_init(fh, video_devdata(file));
2195 v4l2_fh_add(fh);
2197 /*
2198 * If this is the first open file.
2199 * Then initialize hw module.
2200 */
2201 if (v4l2_fh_is_singular_file(file)) {
2202 if (vip_init_port(port)) {
2203 goto free_fh;
2204 ret = -ENODEV;
2205 }
2206 stream->width = 1280;
2207 stream->height = 720;
2208 stream->sizeimage = stream->width * stream->height *
2209 (port->fmt->vpdma_fmt[0]->depth +
2210 (port->fmt->coplanar ?
2211 port->fmt->vpdma_fmt[1]->depth : 0)) >> 3;
2212 stream->bytesperline = round_up((stream->width *
2213 port->fmt->vpdma_fmt[0]->depth) >> 3,
2214 1 << L_ALIGN);
2215 stream->sup_field = V4L2_FIELD_NONE;
2216 port->c_rect.width = stream->width;
2217 port->c_rect.height = stream->height;
2218 vip_dbg(1, dev, "Created stream instance %p\n", stream);
2219 }
2221 mutex_unlock(&dev->mutex);
2222 return 0;
2224 free_fh:
2225 mutex_unlock(&dev->mutex);
2226 if (fh) {
2227 v4l2_fh_del(fh);
2228 v4l2_fh_exit(fh);
2229 kfree(fh);
2230 }
2231 return ret;
2232 }
2233 EXPORT_SYMBOL(vip_open);
2235 int vip_release(struct file *file)
2236 {
2237 struct vip_stream *stream = video_drvdata(file);
2238 struct vip_port *port = stream->port;
2239 struct vip_dev *dev = port->dev;
2240 struct vb2_queue *q = &stream->vb_vidq;
2242 vip_dbg(2, dev, "vip_release\n");
2244 /*
2245 * If this is the last open file.
2246 * Then de-initialize hw module.
2247 */
2248 if (v4l2_fh_is_singular_file(file)) {
2249 mutex_lock(&dev->mutex);
2251 vip_stop_streaming(q);
2252 vip_release_port(stream->port);
2254 mutex_unlock(&dev->mutex);
2255 vip_dbg(1, dev, "Releasing stream instance %p\n", stream);
2256 }
2258 return vb2_fop_release(file);
2259 }
2260 EXPORT_SYMBOL(vip_release);
2262 static const struct v4l2_file_operations vip_fops = {
2263 .owner = THIS_MODULE,
2264 .open = vip_open,
2265 .release = vip_release,
2266 .poll = vb2_fop_poll,
2267 .unlocked_ioctl = video_ioctl2,
2268 .mmap = vb2_fop_mmap,
2269 };
2271 static struct video_device vip_videodev = {
2272 .name = VIP_MODULE_NAME,
2273 .fops = &vip_fops,
2274 .ioctl_ops = &vip_ioctl_ops,
2275 .minor = -1,
2276 .release = video_device_release,
2277 };
2279 static int alloc_stream(struct vip_port *port, int stream_id, int vfl_type)
2280 {
2281 struct vip_stream *stream;
2282 struct vip_dev *dev = port->dev;
2283 struct vb2_queue *q;
2284 struct video_device *vfd;
2285 struct vip_buffer *buf;
2286 int ret, i;
2288 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
2289 if (!stream)
2290 return -ENOMEM;
2292 stream->port = port;
2293 stream->stream_id = stream_id;
2294 stream->vfl_type = vfl_type;
2296 if (vfl_type == VFL_TYPE_GRABBER)
2297 port->cap_streams[stream_id] = stream;
2298 else
2299 port->vbi_streams[stream_id] = stream;
2301 /*
2302 * Initialize queue
2303 */
2304 q = &stream->vb_vidq;
2305 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2306 q->io_modes = VB2_MMAP | VB2_DMABUF;
2307 q->drv_priv = stream;
2308 q->buf_struct_size = sizeof(struct vip_buffer);
2309 q->ops = &vip_video_qops;
2310 q->mem_ops = &vb2_dma_contig_memops;
2311 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2313 /* Feature not back-ported yet. Enable when available */
2314 /* q->min_buffers_needed = 3; */
2316 ret = vb2_queue_init(q);
2317 if (ret)
2318 goto do_free_stream;
2320 INIT_LIST_HEAD(&stream->vidq);
2322 /* Allocate/populate Drop queue entries */
2323 INIT_LIST_HEAD(&stream->dropq);
2324 for (i = 0; i < VIP_DROPQ_SIZE; i++) {
2326 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
2327 if (!buf) {
2328 vip_err(dev, "No memory!!");
2329 ret = -ENOMEM;
2330 goto do_free_stream;
2331 }
2332 buf->drop = true;
2333 list_add(&buf->list, &stream->dropq);
2334 }
2336 vfd = &stream->vdev;
2337 *vfd = vip_videodev;
2338 vfd->v4l2_dev = &dev->v4l2_dev;
2339 vfd->queue = q;
2340 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
2342 vfd->lock = &dev->mutex;
2343 video_set_drvdata(vfd, stream);
2345 ret = video_register_device(vfd, vfl_type, -1);
2346 if (ret) {
2347 vip_err(dev, "Failed to register video device\n");
2348 goto do_free_stream;
2349 }
2351 snprintf(vfd->name, sizeof(vfd->name), "%s", vip_videodev.name);
2352 stream->vfd = vfd;
2354 vip_info(dev, VIP_MODULE_NAME
2355 " Device registered as /dev/video%d\n", vfd->num);
2356 return 0;
2358 do_free_stream:
2359 kfree(stream);
2360 return ret;
2361 }
2363 static void free_stream(struct vip_stream *stream)
2364 {
2365 struct vip_dev *dev = stream->port->dev;
2366 struct vip_buffer *buf;
2367 struct list_head *pos, *q;
2369 if (!stream)
2370 return;
2372 /* Free up the Drop queue */
2373 list_for_each_safe(pos, q, &stream->dropq) {
2374 buf = list_entry(stream->dropq.next,
2375 struct vip_buffer, list);
2376 vip_dbg(1, dev, "dropq buffer\n");
2377 list_del(pos);
2378 kfree(buf);
2379 }
2381 video_unregister_device(stream->vfd);
2382 video_device_release(stream->vfd);
2383 kfree(stream);
2384 }
2386 static int alloc_port(struct vip_dev *dev, int id)
2387 {
2388 struct vip_port *port;
2389 int ret;
2391 port = kzalloc(sizeof(*port), GFP_KERNEL);
2392 if (!port)
2393 return -ENOMEM;
2395 dev->ports[id] = port;
2396 port->dev = dev;
2397 port->port_id = id;
2398 port->num_streams = 0;
2400 ret = alloc_stream(port, 0, VFL_TYPE_GRABBER);
2402 return 0;
2403 }
2405 static void free_port(struct vip_port *port)
2406 {
2407 if (!port)
2408 return;
2410 free_stream(port->cap_streams[0]);
2412 kfree(port);
2413 }
2415 static int get_field(u32 value, u32 mask, int shift)
2416 {
2417 return (value & (mask << shift)) >> shift;
2418 }
2420 static int vip_of_probe(struct platform_device *pdev, struct vip_dev *dev);
2421 static void vip_vpdma_fw_cb(struct platform_device *pdev)
2422 {
2423 struct vip_shared *shared = platform_get_drvdata(pdev);
2424 struct vip_dev *dev;
2425 int slice, ret;
2427 dev_info(&pdev->dev, "VPDMA firmware loaded\n");
2429 for (slice = 0; slice < atomic_read(&shared->devs_allocated); slice++) {
2430 dev = shared->devs[slice];
2432 if (pdev->dev.of_node) {
2433 ret = vip_of_probe(pdev, dev);
2434 if (ret)
2435 goto free_port;
2436 }
2437 }
2439 return;
2441 free_port:
2442 free_port(dev->ports[0]);
2443 }
2445 static void remove_shared(struct vip_shared *shared)
2446 {
2447 if (atomic_dec_return(&shared->devs_allocated) != 0)
2448 return;
2450 iounmap(shared->base);
2451 release_mem_region(shared->res->start, resource_size(shared->res));
2452 kfree(shared);
2453 }
2455 static int vip_runtime_get(struct platform_device *pdev)
2456 {
2457 int r;
2459 r = pm_runtime_get_sync(&pdev->dev);
2460 WARN_ON(r < 0);
2461 return r < 0 ? r : 0;
2462 }
2464 static int get_subdev_active_format(struct vip_dev *dev,
2465 struct v4l2_subdev *subdev)
2466 {
2467 struct vip_fmt *fmt;
2468 enum v4l2_mbus_pixelcode code;
2469 int ret = 0;
2470 unsigned int k;
2472 /* first find how many formats to allocate the correct size */
2473 dev->num_active_fmt = 0;
2474 for (k = 0;
2475 (ret != -EINVAL) && (dev->num_active_fmt < VIP_MAX_ACTIVE_FMT);
2476 k++) {
2477 ret = v4l2_subdev_call(subdev, video, enum_mbus_fmt, k, &code);
2478 if (ret == 0) {
2479 fmt = find_format_by_code(code);
2480 if (fmt) {
2481 dev->active_fmt[dev->num_active_fmt] = fmt;
2482 dev->num_active_fmt++;
2483 }
2484 }
2485 }
2487 if (dev->num_active_fmt == 0) {
2489 vip_err(dev, "No suitable format reported by subdev %s\n",
2490 subdev->name);
2491 return -EINVAL;
2492 }
2494 return 0;
2495 }
2497 static int vip_async_bound(struct v4l2_async_notifier *notifier,
2498 struct v4l2_subdev *subdev,
2499 struct v4l2_async_subdev *asd)
2500 {
2501 struct vip_dev *dev = notifier_to_vip_dev(notifier);
2502 unsigned int idx = asd - &dev->config->asd[0];
2504 vip_dbg(1, dev, "vip_async_bound\n");
2505 if (idx > dev->config->asd_sizes)
2506 return -EINVAL;
2508 if (get_subdev_active_format(dev, subdev))
2509 return 0;
2511 if (dev->sensor) {
2512 if (asd < dev->sensor->asd) {
2513 /* Notified of a subdev earlier in the array */
2514 vip_info(dev, "Switching to subdev %s (High priority)",
2515 subdev->name);
2516 } else {
2517 vip_info(dev, "Rejecting subdev %s (Low priority)",
2518 subdev->name);
2519 return 0;
2520 }
2521 } else
2522 alloc_port(dev, 0);
2524 dev->sensor = subdev;
2525 dev->endpoint = &dev->config->endpoints[idx];
2526 vip_info(dev, "Using sensor %s for capture\n",
2527 subdev->name);
2529 return 0;
2530 }
2532 static int vip_async_complete(struct v4l2_async_notifier *notifier)
2533 {
2534 struct vip_dev *dev = notifier_to_vip_dev(notifier);
2535 vip_dbg(1, dev, "vip_async_complete\n");
2536 return 0;
2537 }
2539 static struct device_node *
2540 of_get_next_port(const struct device_node *parent,
2541 struct device_node *prev)
2542 {
2543 struct device_node *port = NULL;
2545 if (!parent)
2546 return NULL;
2548 if (!prev) {
2549 struct device_node *ports;
2550 /*
2551 * It's the first call, we have to find a port subnode
2552 * within this node or within an optional 'ports' node.
2553 */
2554 ports = of_get_child_by_name(parent, "ports");
2555 if (ports)
2556 parent = ports;
2558 port = of_get_child_by_name(parent, "port");
2560 /* release the 'ports' node */
2561 of_node_put(ports);
2562 } else {
2563 struct device_node *ports;
2565 ports = of_get_parent(prev);
2566 if (!ports)
2567 return NULL;
2569 do {
2570 port = of_get_next_child(ports, prev);
2571 if (!port) {
2572 of_node_put(ports);
2573 return NULL;
2574 }
2575 prev = port;
2576 } while (of_node_cmp(port->name, "port") != 0);
2577 }
2579 return port;
2580 }
2582 static struct device_node *
2583 of_get_next_endpoint(const struct device_node *parent,
2584 struct device_node *prev)
2585 {
2586 struct device_node *ep = NULL;
2588 if (!parent)
2589 return NULL;
2591 do {
2592 ep = of_get_next_child(parent, prev);
2593 if (!ep)
2594 return NULL;
2595 prev = ep;
2596 } while (of_node_cmp(ep->name, "endpoint") != 0);
2598 return ep;
2599 }
2601 static int vip_of_probe(struct platform_device *pdev, struct vip_dev *dev)
2602 {
2603 struct device_node *ep_node = NULL, *port, *remote_ep,
2604 *sensor_node, *parent;
2605 struct device_node *syscon_np;
2606 struct v4l2_of_endpoint *endpoint;
2607 struct v4l2_async_subdev *asd;
2608 u32 regval = 0;
2609 int ret, slice, i = 0, found_port = 0;
2611 parent = pdev->dev.of_node;
2613 syscon_np = of_parse_phandle(pdev->dev.of_node, "syscon", 0);
2614 dev->syscon = syscon_node_to_regmap(syscon_np);
2615 of_node_put(syscon_np);
2617 dev->config = kzalloc(sizeof(struct vip_config), GFP_KERNEL);
2618 if (!dev->config)
2619 return -ENOMEM;
2621 dev->config->card_name = "VIP Driver";
2623 port = NULL;
2624 vip_dbg(3, dev, "Scanning Port node for slice id: %d\n", dev->slice_id);
2625 for (slice = 0; slice < VIP_NUM_SLICES; slice++) {
2626 port = of_get_next_port(parent, port);
2627 if (!port) {
2628 vip_dbg(1, dev, "No port node found for slice_id:%d\n",
2629 slice);
2630 ret = -EINVAL;
2631 goto free_config;
2632 }
2634 /* Match the slice number with <REG> */
2635 of_property_read_u32(port, "reg", ®val);
2636 vip_dbg(3, dev, "slice:%d dev->slice_id:%d <reg>:%d\n",
2637 slice, dev->slice_id, regval);
2638 if ((regval == dev->slice_id) && (slice == dev->slice_id)) {
2639 found_port = 1;
2640 break;
2641 }
2642 }
2644 if (!found_port) {
2645 if (!port)
2646 of_node_put(port);
2647 vip_dbg(1, dev, "No port node matches slice_id:%d\n",
2648 dev->slice_id);
2649 ret = -EINVAL;
2650 goto free_config;
2651 }
2653 vip_dbg(3, dev, "Scanning sub-device(s) for slice id: %d\n",
2654 dev->slice_id);
2655 while (i < VIP_MAX_SUBDEV) {
2657 asd = &dev->config->asd[i];
2658 endpoint = &dev->config->endpoints[i];
2660 remote_ep = NULL;
2661 sensor_node = NULL;
2663 ep_node = of_get_next_endpoint(port, ep_node);
2664 if (!ep_node) {
2665 vip_dbg(3, dev, "can't get next endpoint: loop: %d\n",
2666 i);
2667 break;
2668 }
2670 sensor_node = of_graph_get_remote_port_parent(ep_node);
2671 if (!sensor_node) {
2672 vip_dbg(3, dev, "can't get remote parent: loop: %d\n",
2673 i);
2674 goto of_node_cleanup;
2675 }
2676 asd->match_type = V4L2_ASYNC_MATCH_OF;
2677 asd->match.of.node = sensor_node;
2679 remote_ep = of_parse_phandle(ep_node, "remote-endpoint", 0);
2680 if (!remote_ep) {
2681 vip_dbg(3, dev, "can't get remote-endpoint: loop: %d\n",
2682 i);
2683 goto of_node_cleanup;
2684 }
2685 v4l2_of_parse_endpoint(remote_ep, endpoint);
2687 dev->config->asd_list[i++] = asd;
2689 of_node_cleanup:
2690 if (!remote_ep)
2691 of_node_put(remote_ep);
2692 if (!sensor_node)
2693 of_node_put(sensor_node);
2694 }
2696 if (!ep_node)
2697 of_node_put(ep_node);
2698 if (!port)
2699 of_node_put(port);
2701 vip_dbg(1, dev, "Found %d sub-device(s) for slice id: %d\n",
2702 i, dev->slice_id);
2703 if (i > 0) {
2704 dev->config->asd_sizes = i;
2705 dev->notifier.subdevs = dev->config->asd_list;
2706 dev->notifier.num_subdevs = dev->config->asd_sizes;
2707 dev->notifier.bound = vip_async_bound;
2708 dev->notifier.complete = vip_async_complete;
2710 vip_dbg(1, dev, "registering the sync notifier for sensors %d",
2711 i);
2712 ret = v4l2_async_notifier_register(&dev->v4l2_dev,
2713 &dev->notifier);
2714 if (ret) {
2715 vip_dbg(1, dev, "Error registering async notifier\n");
2716 ret = -EINVAL;
2717 goto free_config;
2718 }
2719 }
2721 return 0;
2722 free_config:
2723 kfree(dev->config);
2724 return ret;
2725 }
2727 static const struct of_device_id vip_of_match[];
2728 static int vip_probe(struct platform_device *pdev)
2729 {
2730 struct vip_dev *dev;
2731 struct vip_shared *shared;
2732 const struct of_device_id *of_dev_id;
2733 struct pinctrl *pinctrl;
2734 int ret, slice = VIP_SLICE1;
2735 u32 tmp, pid;
2736 struct v4l2_ctrl_handler *hdl;
2738 pm_runtime_enable(&pdev->dev);
2740 ret = vip_runtime_get(pdev);
2741 if (ret)
2742 goto err_runtime_get;
2744 of_dev_id = of_match_device(vip_of_match, &pdev->dev);
2745 if (!of_dev_id) {
2746 dev_err(&pdev->dev, "%s: Unable to match device\n", __func__);
2747 return -ENODEV;
2748 }
2750 shared = kzalloc(sizeof(*shared), GFP_KERNEL);
2751 if (!shared)
2752 return -ENOMEM;
2754 shared->res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vip");
2755 if (shared->res == NULL) {
2756 dev_err(&pdev->dev, "Missing platform resources data\n");
2757 ret = -ENODEV;
2758 goto free_shared;
2759 }
2761 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2762 if (IS_ERR(pinctrl)) {
2763 dev_warn(&pdev->dev, "No explicit pinctrl resources data.\n");
2764 }
2766 if (devm_request_mem_region(&pdev->dev, shared->res->start,
2767 resource_size(shared->res), VIP_MODULE_NAME) == NULL) {
2768 ret = -ENOMEM;
2769 goto free_shared;
2770 }
2772 shared->base = devm_ioremap(&pdev->dev, shared->res->start,
2773 resource_size(shared->res));
2774 if (!shared->base) {
2775 dev_err(&pdev->dev, "failed to ioremap\n");
2776 ret = -ENOMEM;
2777 goto rel_mem_region;
2778 }
2780 /* Make sure H/W module has the right functionality */
2781 pid = read_sreg(shared, VIP_PID);
2782 tmp = get_field(pid, VIP_PID_FUNC_MASK, VIP_PID_FUNC_SHIFT);
2784 if (tmp != VIP_PID_FUNC) {
2785 dev_info(&pdev->dev, "vip: unexpected PID function: 0x%x\n",
2786 tmp);
2787 ret = -ENODEV;
2788 goto do_iounmap;
2789 }
2791 /* enable clocks, so the firmware will load properly */
2792 vip_shared_set_clock_enable(shared, 1);
2793 vip_top_vpdma_reset(shared);
2795 shared->vpdma = vpdma_create(pdev, vip_vpdma_fw_cb);
2796 if (!shared->vpdma) {
2797 dev_err(&pdev->dev, "Creating VPDMA failed");
2798 goto do_iounmap;
2799 }
2801 list_add_tail(&shared->list, &vip_shared_list);
2802 platform_set_drvdata(pdev, shared);
2803 atomic_set(&shared->devs_allocated, 0);
2805 vip_set_idle_mode(shared, VIP_SMART_IDLE_MODE);
2806 vip_set_standby_mode(shared, VIP_SMART_STANDBY_MODE);
2808 for (slice = VIP_SLICE1; slice < VIP_NUM_SLICES; slice++) {
2809 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2810 if (!dev)
2811 return -ENOMEM;
2813 dev->irq = platform_get_irq(pdev, slice);
2814 if (!dev->irq) {
2815 dev_err(&pdev->dev, "Could not get IRQ");
2816 goto err_runtime_get;
2817 }
2819 if (devm_request_irq(&pdev->dev, dev->irq, vip_irq,
2820 0, VIP_MODULE_NAME, dev) < 0) {
2821 ret = -ENOMEM;
2822 goto dev_unreg;
2823 }
2825 spin_lock_init(&dev->slock);
2826 spin_lock_init(&dev->lock);
2828 INIT_LIST_HEAD(&dev->vip_bufs);
2830 dev->instance_id = (int)of_dev_id->data;
2832 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
2833 "%s%d-%d", VIP_MODULE_NAME, dev->instance_id, slice);
2834 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2835 if (ret)
2836 goto err_runtime_get;
2838 mutex_init(&dev->mutex);
2840 hdl = &dev->ctrl_handler;
2841 v4l2_ctrl_handler_init(hdl, 11);
2842 dev->v4l2_dev.ctrl_handler = hdl;
2844 dev->slice_id = slice;
2845 dev->pdev = pdev;
2846 dev->res = shared->res;
2847 dev->base = shared->base;
2849 dev->shared = shared;
2850 shared->devs[slice] = dev;
2852 atomic_inc(&shared->devs_allocated);
2854 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2855 if (IS_ERR(dev->alloc_ctx)) {
2856 vip_err(dev, "Failed to alloc vb2 context\n");
2857 ret = PTR_ERR(dev->alloc_ctx);
2858 goto dev_unreg;
2859 }
2861 vip_top_reset(dev);
2862 vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT);
2863 }
2865 return 0;
2867 dev_unreg:
2868 v4l2_device_unregister(&dev->v4l2_dev);
2869 do_iounmap:
2870 iounmap(shared->base);
2871 rel_mem_region:
2872 release_mem_region(shared->res->start, resource_size(shared->res));
2873 free_shared:
2874 kfree(shared);
2875 err_runtime_get:
2876 if (slice == VIP_SLICE1) {
2877 pm_runtime_disable(&pdev->dev);
2878 return ret;
2879 } else
2880 return 0;
2881 }
2883 static int vip_remove(struct platform_device *pdev)
2884 {
2885 struct vip_shared *shared = platform_get_drvdata(pdev);
2886 struct vip_dev *dev;
2887 int slice;
2889 for (slice = 0; slice < atomic_read(&shared->devs_allocated); slice++) {
2890 dev = shared->devs[slice];
2891 if (!dev)
2892 continue;
2893 vip_info(dev, "Removing " VIP_MODULE_NAME);
2894 free_port(dev->ports[0]);
2895 v4l2_async_notifier_unregister(&dev->notifier);
2896 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2897 free_irq(dev->irq, dev);
2898 kfree(dev);
2899 }
2900 remove_shared(shared);
2902 return 0;
2903 }
2905 #if defined(CONFIG_OF)
2906 static const struct of_device_id vip_of_match[] = {
2907 {
2908 .compatible = "ti,vip1", .data = (void *) VIP_INSTANCE1,
2909 },
2911 {
2912 .compatible = "ti,vip2", .data = (void *) VIP_INSTANCE2,
2913 },
2915 {
2916 .compatible = "ti,vip3", .data = (void *) VIP_INSTANCE3,
2917 },
2918 {},
2919 };
2920 #else
2921 #define vip_of_match NULL
2922 #endif
2924 static struct platform_driver vip_pdrv = {
2925 .probe = vip_probe,
2926 .remove = vip_remove,
2927 .driver = {
2928 .name = VIP_MODULE_NAME,
2929 .owner = THIS_MODULE,
2930 .of_match_table = vip_of_match,
2931 },
2932 };
2934 module_platform_driver(vip_pdrv);
2936 MODULE_DESCRIPTION("TI VIP driver");
2937 MODULE_AUTHOR("Texas Instruments");
2938 MODULE_LICENSE("GPL v2");