50f4b1bbddc624e96d6d41e6b13bdbf98089d34e
[android-sdk/kernel-video.git] / drivers / media / platform / ti-vpe / vip.c
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           = "NV24 YUV 444 co-planar",
131                 .fourcc         = V4L2_PIX_FMT_NV24,
132                 .code           = V4L2_MBUS_FMT_UYVY8_2X8,
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           = "NV16 YUV 422 co-planar",
141                 .fourcc         = V4L2_PIX_FMT_NV16,
142                 .code           = V4L2_MBUS_FMT_UYVY8_2X8,
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           = "NV12 YUV 420 co-planar",
151                 .fourcc         = V4L2_PIX_FMT_NV12,
152                 .code           = V4L2_MBUS_FMT_UYVY8_2X8,
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_CBY422],
166                                   },
167         },
168         {
169                 .name           = "YUYV 422 packed",
170                 .fourcc         = V4L2_PIX_FMT_YUYV,
171                 .code           = V4L2_MBUS_FMT_UYVY8_2X8,
172                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
173                 .coplanar       = 0,
174                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YCB422],
175                                   },
176         },
177         {
178                 .name           = "VYUY 422 packed",
179                 .fourcc         = V4L2_PIX_FMT_VYUY,
180                 .code           = V4L2_MBUS_FMT_UYVY8_2X8,
181                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
182                 .coplanar       = 0,
183                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CRY422],
184                                   },
185         },
186         {
187                 .name           = "YVYU 422 packed",
188                 .fourcc         = V4L2_PIX_FMT_YVYU,
189                 .code           = V4L2_MBUS_FMT_UYVY8_2X8,
190                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
191                 .coplanar       = 0,
192                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YCR422],
193                                   },
194         },
195         {
196                 .name           = "RGB888 packed",
197                 .fourcc         = V4L2_PIX_FMT_RGB24,
198                 .code           = V4L2_MBUS_FMT_RGB888_1X24,
199                 .colorspace     = V4L2_COLORSPACE_SRGB,
200                 .coplanar       = 0,
201                 .vpdma_fmt      = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
202                                   },
203         },
204         {
205                 .name           = "ARGB888 packed",
206                 .fourcc         = V4L2_PIX_FMT_RGB32,
207                 .code           = V4L2_MBUS_FMT_ARGB8888_1X32,
208                 .colorspace     = V4L2_COLORSPACE_SRGB,
209                 .coplanar       = 0,
210                 .vpdma_fmt      = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
211                                   },
212         },
213 };
215 /*  Print Four-character-code (FOURCC) */
216 static char *fourcc_to_str(u32 fmt)
218         static char code[5];
219         code[0] = (unsigned char)(fmt & 0xff);
220         code[1] = (unsigned char)((fmt>>8) & 0xff);
221         code[2] = (unsigned char)((fmt>>16) & 0xff);
222         code[3] = (unsigned char)((fmt>>24) & 0xff);
223         code[4] = '\0';
225         return code;
228 /*
229  * Find our format description corresponding to the passed v4l2_format
230  */
232 static struct vip_fmt *find_port_format_by_pix(struct vip_port *port,
233                                                u32 pixelformat)
235         struct vip_dev *dev = port->dev;
236         struct vip_fmt *fmt;
237         unsigned int k;
239         for (k = 0; k < dev->num_active_fmt; k++) {
240                 fmt = dev->active_fmt[k];
241                 if (fmt->fourcc == pixelformat)
242                         return fmt;
243         }
245         return NULL;
248 static LIST_HEAD(vip_shared_list);
250 static inline struct vip_dev *notifier_to_vip_dev(struct v4l2_async_notifier *n)
252         return container_of(n, struct vip_dev, notifier);
255 /*
256  * port flag bits
257  */
258 #define FLAG_FRAME_1D           (1 << 0)
259 #define FLAG_EVEN_LINE_SKIP     (1 << 1)
260 #define FLAG_ODD_LINE_SKIP      (1 << 2)
261 #define FLAG_MODE_TILED         (1 << 3)
262 #define FLAG_INTERLACED         (1 << 4)
263 #define FLAG_MULTIPLEXED        (1 << 5)
264 #define FLAG_MULT_PORT          (1 << 6)
265 #define FLAG_MULT_ANC           (1 << 7)
267 /*
268  * Function prototype declarations
269  */
270 static int alloc_port(struct vip_dev *, int);
271 static void free_port(struct vip_port *);
272 static int vip_setup_parser(struct vip_port *port);
273 static void stop_dma(struct vip_stream *stream);
275 static inline u32 read_sreg(struct vip_shared *shared, int offset)
277         return ioread32(shared->base + offset);
280 static inline void write_sreg(struct vip_shared *shared, int offset, u32 value)
282         iowrite32(value, shared->base + offset);
285 static inline u32 read_vreg(struct vip_dev *dev, int offset)
287         return ioread32(dev->base + offset);
290 static inline void write_vreg(struct vip_dev *dev, int offset, u32 value)
292         iowrite32(value, dev->base + offset);
295 /*
296  * Insert a masked field into a 32-bit field
297  */
298 static void insert_field(u32 *valp, u32 field, u32 mask, int shift)
300         u32 val = *valp;
302         val &= ~(mask << shift);
303         val |= (field & mask) << shift;
304         *valp = val;
307 /*
308  * Set the system idle mode
309  */
310 static void vip_set_idle_mode(struct vip_shared *shared, int mode)
312         u32 reg = read_sreg(shared, VIP_SYSCONFIG);
313         insert_field(&reg, mode, VIP_SYSCONFIG_IDLE_MASK,
314                      VIP_SYSCONFIG_IDLE_SHIFT);
315         write_sreg(shared, VIP_SYSCONFIG, reg);
318 /*
319  * Set the VIP standby mode
320  */
321 static void vip_set_standby_mode(struct vip_shared *shared, int mode)
323         u32 reg = read_sreg(shared, VIP_SYSCONFIG);
324         insert_field(&reg, mode, VIP_SYSCONFIG_STANDBY_MASK,
325                      VIP_SYSCONFIG_STANDBY_SHIFT);
326         write_sreg(shared, VIP_SYSCONFIG, reg);
329 /*
330  * Enable or disable the VIP clocks
331  */
332 static void vip_set_clock_enable(struct vip_dev *dev, bool on)
334         u32 val = 0;
336         val = read_vreg(dev, VIP_CLK_ENABLE);
337         if (on) {
338                 val |= VIP_VPDMA_CLK_ENABLE;
339                 if (dev->slice_id == VIP_SLICE1)
340                         val |= VIP_VIP1_DATA_PATH_CLK_ENABLE;
341                 else
342                         val |= VIP_VIP2_DATA_PATH_CLK_ENABLE;
343         } else {
344                 if (dev->slice_id == VIP_SLICE1)
345                         val &= ~VIP_VIP1_DATA_PATH_CLK_ENABLE;
346                 else
347                         val &= ~VIP_VIP2_DATA_PATH_CLK_ENABLE;
349                 /* Both VIP are disabled then shutdown VPDMA also */
350                 if (!(val & (VIP_VIP1_DATA_PATH_CLK_ENABLE|
351                              VIP_VIP2_DATA_PATH_CLK_ENABLE)))
352                         val = 0;
353         }
355         write_vreg(dev, VIP_CLK_ENABLE, val);
358 /* This helper function is used to enable the clock early on to
359  * enable vpdma firmware loading before the slice device are created
360  */
361 static void vip_shared_set_clock_enable(struct vip_shared *shared, bool on)
363         u32 val = 0;
365         if (on)
366                 val = VIP_VIP1_DATA_PATH_CLK_ENABLE | VIP_VPDMA_CLK_ENABLE;
368         write_sreg(shared, VIP_CLK_ENABLE, val);
371 static void vip_top_reset(struct vip_dev *dev)
373         u32 val = 0;
375         val = read_vreg(dev, VIP_CLK_RESET);
377         if (dev->slice_id == VIP_SLICE1)
378                 insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
379                         VIP_VIP1_DATA_PATH_RESET_SHIFT);
380         else
381                 insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
382                         VIP_VIP2_DATA_PATH_RESET_SHIFT);
384         write_vreg(dev, VIP_CLK_RESET, val);
386         usleep_range(200, 250);
388         val = read_vreg(dev, VIP_CLK_RESET);
390         if (dev->slice_id == VIP_SLICE1)
391                 insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
392                         VIP_VIP1_DATA_PATH_RESET_SHIFT);
393         else
394                 insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
395                         VIP_VIP2_DATA_PATH_RESET_SHIFT);
396         write_vreg(dev, VIP_CLK_RESET, val);
399 static void vip_top_vpdma_reset(struct vip_shared *shared)
401         u32 val;
403         val = read_sreg(shared, VIP_CLK_RESET);
404         insert_field(&val, 1, VIP_VPDMA_CLK_RESET_MASK,
405                 VIP_VPDMA_CLK_RESET_SHIFT);
406         write_sreg(shared, VIP_CLK_RESET, val);
408         usleep_range(200, 250);
410         val = read_sreg(shared, VIP_CLK_RESET);
411         insert_field(&val, 0, VIP_VPDMA_CLK_RESET_MASK,
412                 VIP_VPDMA_CLK_RESET_SHIFT);
413         write_sreg(shared, VIP_CLK_RESET, val);
416 static void vip_xtra_set_repack_sel(struct vip_port *port, int repack_mode)
418         u32 val;
420         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
421                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
422                                 VIP_PARSER_PORTA_1);
423                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
424                              VIP_REPACK_SEL_SHFT);
426                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
427                            VIP_PARSER_PORTA_1, val);
428         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
429                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
430                                 VIP_PARSER_PORTA_1);
431                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
432                              VIP_REPACK_SEL_SHFT);
434                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
435                            VIP_PARSER_PORTA_1, val);
436         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
437                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
438                                 VIP_PARSER_PORTB_1);
439                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
440                              VIP_REPACK_SEL_SHFT);
442                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
443                            VIP_PARSER_PORTB_1, val);
444         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
445                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
446                                 VIP_PARSER_PORTB_1);
447                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
448                              VIP_REPACK_SEL_SHFT);
450                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
451                            VIP_PARSER_PORTB_1, val);
452         }
455 static void vip_set_discrete_basic_mode(struct vip_port *port)
457         u32 val;
459         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
460                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
461                                 VIP_PARSER_PORTA_0);
462                 val |= VIP_DISCRETE_BASIC_MODE;
464                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
465                            VIP_PARSER_PORTA_0, val);
466         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
467                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
468                                 VIP_PARSER_PORTA_0);
469                 val |= VIP_DISCRETE_BASIC_MODE;
471                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
472                            VIP_PARSER_PORTA_0, val);
473         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
474                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
475                                 VIP_PARSER_PORTB_0);
476                 val |= VIP_DISCRETE_BASIC_MODE;
478                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
479                            VIP_PARSER_PORTB_0, val);
480         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
481                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
482                                 VIP_PARSER_PORTB_0);
483                 val |= VIP_DISCRETE_BASIC_MODE;
485                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
486                            VIP_PARSER_PORTB_0, val);
487         }
490 static void vip_set_pclk_polarity(struct vip_port *port, int polarity)
492         u32 val, ret, offset;
494         if (polarity == 1 && port->dev->syscon) {
496                 /*
497                  * When the VIP parser is configured to so that the pixel clock
498                  * is to be sampled at falling edge, the pixel clock needs to be
499                  * inverted before it is given to the VIP module. This is done
500                  * by setting a bit in the CTRL_CORE_SMA_SW1 register.
501                  */
503                 if (port->dev->instance_id == VIP_INSTANCE1)
504                         offset = 0 + 2 * port->port_id + port->dev->slice_id;
505                 else if (port->dev->instance_id == VIP_INSTANCE2)
506                         offset = 4 + 2 * port->port_id + port->dev->slice_id;
507                 else if (port->dev->instance_id == VIP_INSTANCE3)
508                         offset = 10 - port->dev->slice_id;
509                 else
510                         BUG();
512                 ret = regmap_update_bits(port->dev->syscon,
513                         CTRL_CORE_SMA_SW_1, 1 << offset, 1 << offset);
514         }
516         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
517                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
518                                 VIP_PARSER_PORTA_0);
519                 if (polarity)
520                         val |= VIP_PIXCLK_EDGE_POLARITY;
521                 else
522                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
524                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
525                            VIP_PARSER_PORTA_0, val);
526         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
527                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET
528                                 + VIP_PARSER_PORTA_0);
529                 if (polarity)
530                         val |= VIP_PIXCLK_EDGE_POLARITY;
531                 else
532                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
533                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
534                            VIP_PARSER_PORTA_0, val);
535         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
536                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
537                                 VIP_PARSER_PORTB_0);
538                 if (polarity)
539                         val |= VIP_PIXCLK_EDGE_POLARITY;
540                 else
541                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
542                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
543                            VIP_PARSER_PORTB_0, val);
544         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
545                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
546                                 VIP_PARSER_PORTB_0);
547                 if (polarity)
548                         val |= VIP_PIXCLK_EDGE_POLARITY;
549                 else
550                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
552                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
553                            VIP_PARSER_PORTB_0, val);
554         }
557 static void vip_set_vsync_polarity(struct vip_port *port, int polarity)
559         u32 val;
561         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
562                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
563                                 VIP_PARSER_PORTA_0);
564                 if (polarity)
565                         val |= VIP_VSYNC_POLARITY;
566                 else
567                         val &= ~VIP_VSYNC_POLARITY;
569                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
570                            VIP_PARSER_PORTA_0, val);
571         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
572                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
573                                 VIP_PARSER_PORTA_0);
574                 if (polarity)
575                         val |= VIP_VSYNC_POLARITY;
576                 else
577                         val &= ~VIP_VSYNC_POLARITY;
578                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
579                            VIP_PARSER_PORTA_0, val);
580         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
581                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
582                                 VIP_PARSER_PORTB_0);
583                 if (polarity)
584                         val |= VIP_VSYNC_POLARITY;
585                 else
586                         val &= ~VIP_VSYNC_POLARITY;
587                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
588                            VIP_PARSER_PORTB_0, val);
589         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
590                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
591                                 VIP_PARSER_PORTB_0);
592                 if (polarity)
593                         val |= VIP_VSYNC_POLARITY;
594                 else
595                         val &= ~VIP_VSYNC_POLARITY;
597                 write_vreg(port->dev,  VIP2_PARSER_REG_OFFSET +
598                            VIP_PARSER_PORTB_0, val);
599         }
602 static void vip_set_hsync_polarity(struct vip_port *port, int polarity)
604         u32 val;
606         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
607                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
608                                 VIP_PARSER_PORTA_0);
609                 if (polarity)
610                         val |= VIP_HSYNC_POLARITY;
611                 else
612                         val &= ~VIP_HSYNC_POLARITY;
614                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
615                            VIP_PARSER_PORTA_0, val);
616         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
617                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
618                                 VIP_PARSER_PORTA_0);
619                 if (polarity)
620                         val |= VIP_HSYNC_POLARITY;
621                 else
622                         val &= ~VIP_HSYNC_POLARITY;
623                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
624                            VIP_PARSER_PORTA_0, val);
625         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
626                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
627                                 VIP_PARSER_PORTB_0);
628                 if (polarity)
629                         val |= VIP_HSYNC_POLARITY;
630                 else
631                         val &= ~VIP_HSYNC_POLARITY;
632                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
633                            VIP_PARSER_PORTB_0, val);
634         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
635                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
636                                 VIP_PARSER_PORTB_0);
637                 if (polarity)
638                         val |= VIP_HSYNC_POLARITY;
639                 else
640                         val &= ~VIP_HSYNC_POLARITY;
641                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
642                            VIP_PARSER_PORTB_0, val);
643         }
646 static void vip_set_actvid_polarity(struct vip_port *port, int polarity)
648         u32 val;
650         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
651                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
652                                 VIP_PARSER_PORTA_0);
653                 if (polarity)
654                         val |= VIP_ACTVID_POLARITY;
655                 else
656                         val &= ~VIP_ACTVID_POLARITY;
658                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
659                            VIP_PARSER_PORTA_0, val);
660         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
661                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
662                                 VIP_PARSER_PORTA_0);
663                 if (polarity)
664                         val |= VIP_ACTVID_POLARITY;
665                 else
666                         val &= ~VIP_ACTVID_POLARITY;
667                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
668                            VIP_PARSER_PORTA_0, val);
669         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
670                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
671                                 VIP_PARSER_PORTB_0);
672                 if (polarity)
673                         val |= VIP_ACTVID_POLARITY;
674                 else
675                         val &= ~VIP_ACTVID_POLARITY;
676                 write_vreg(port->dev,  VIP1_PARSER_REG_OFFSET +
677                            VIP_PARSER_PORTB_0, val);
678         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
679                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
680                                 VIP_PARSER_PORTB_0);
681                 if (polarity)
682                         val |= VIP_ACTVID_POLARITY;
683                 else
684                         val &= ~VIP_ACTVID_POLARITY;
686                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
687                            VIP_PARSER_PORTB_0, val);
688         }
691 static void vip_set_actvid_hsync_n(struct vip_port *port, int enable)
693         u32 val;
695         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
696                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
697                                 VIP_PARSER_PORTA_0);
698                 if (enable)
699                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
700                 else
701                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
703                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
704                            VIP_PARSER_PORTA_0, val);
705         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
706                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
707                                 VIP_PARSER_PORTA_0);
708                 if (enable)
709                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
710                 else
711                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
712                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
713                            VIP_PARSER_PORTA_0, val);
714         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
715                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
716                                 VIP_PARSER_PORTB_0);
717                 if (enable)
718                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
719                 else
720                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
721                 write_vreg(port->dev,  VIP1_PARSER_REG_OFFSET +
722                            VIP_PARSER_PORTB_0, val);
723         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
724                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
725                                 VIP_PARSER_PORTB_0);
726                 if (enable)
727                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
728                 else
729                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
731                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
732                            VIP_PARSER_PORTB_0, val);
733         }
736 static void vip_sync_type(struct vip_port *port, enum sync_types sync)
738         u32 val;
740         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
741                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
742                                 VIP_PARSER_PORTA_0);
743                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
744                              VIP_SYNC_TYPE_SHFT);
746                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
747                            VIP_PARSER_PORTA_0, val);
748         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
749                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
750                                 VIP_PARSER_PORTA_0);
751                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
752                              VIP_SYNC_TYPE_SHFT);
754                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
755                            VIP_PARSER_PORTA_0, val);
756         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
757                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
758                                 VIP_PARSER_PORTB_0);
759                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
760                              VIP_SYNC_TYPE_SHFT);
762                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
763                            VIP_PARSER_PORTB_0, val);
764         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
765                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
766                                 VIP_PARSER_PORTB_0);
767                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
768                              VIP_SYNC_TYPE_SHFT);
770                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
771                            VIP_PARSER_PORTB_0, val);
772         }
775 static void vip_set_data_interface(struct vip_port *port,
776                                    enum data_interface_modes mode)
778         u32 val = 0;
780         if (port->dev->slice_id == VIP_SLICE1) {
781                 insert_field(&val, mode, VIP_DATA_INTERFACE_MODE_MASK,
782                              VIP_DATA_INTERFACE_MODE_SHFT);
784                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET, val);
785         } else if (port->dev->slice_id == VIP_SLICE2) {
786                 insert_field(&val, mode, VIP_DATA_INTERFACE_MODE_MASK,
787                              VIP_DATA_INTERFACE_MODE_SHFT);
789                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET, val);
790         }
793 static void vip_reset_port(struct vip_port *port)
795         u32 val = 0;
797         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
798                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
799                            VIP_PARSER_PORTA_0, VIP_SW_RESET);
801                 usleep_range(200, 250);
803                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
804                                 VIP_PARSER_PORTA_0);
806                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
807                            VIP_PARSER_PORTA_0, 0);
808         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
809                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
810                            VIP_PARSER_PORTA_0, VIP_SW_RESET);
812                 usleep_range(200, 250);
814                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
815                                 VIP_PARSER_PORTA_0);
817                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
818                            VIP_PARSER_PORTA_0, 0);
820         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
821                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
822                            VIP_PARSER_PORTB_0, VIP_SW_RESET);
824                 usleep_range(200, 250);
826                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
827                                 VIP_PARSER_PORTB_0);
829                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
830                            VIP_PARSER_PORTB_0, 0);
831         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
832                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
833                            VIP_PARSER_PORTB_0, VIP_SW_RESET);
835                 usleep_range(200, 250);
837                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
838                                 VIP_PARSER_PORTB_0);
840                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
841                            VIP_PARSER_PORTB_0, 0);
842         }
845 static void vip_set_port_enable(struct vip_port *port, bool on)
847         u32 val = 0;
849         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
850                 if (on)
851                         val |= VIP_PORT_ENABLE;
852                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
853                            VIP_PARSER_PORTA_0, val);
854         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
855                 if (on)
856                         val |= VIP_PORT_ENABLE;
857                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
858                            VIP_PARSER_PORTA_0, val);
859         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
860                 if (on)
861                         val |= VIP_PORT_ENABLE;
862                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
863                            VIP_PARSER_PORTB_0, val);
864         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
865                 if (on)
866                         val |= VIP_PORT_ENABLE;
867                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
868                            VIP_PARSER_PORTB_0, val);
869         }
872 static void vip_set_slice_path(struct vip_dev *dev,
873                                enum data_path_select data_path)
875         u32 val = 0;
877         switch (data_path) {
878         case VIP_MULTI_CHANNEL_DATA_SELECT:
879                 if (dev->slice_id == VIP_SLICE1) {
880                         val |= VIP_MULTI_CHANNEL_SELECT;
881                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
882                                      VIP_DATAPATH_SELECT_SHFT);
884                         write_vreg(dev, VIP_VIP1_DATA_PATH_SELECT, val);
885                 } else if (dev->slice_id == VIP_SLICE2) {
886                         val |= VIP_MULTI_CHANNEL_SELECT;
887                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
888                                      VIP_DATAPATH_SELECT_SHFT);
890                         write_vreg(dev, VIP_VIP2_DATA_PATH_SELECT, val);
891                 }
892                 break;
893         case VIP_RGB_OUT_LO_DATA_SELECT:
894                 if (dev->slice_id == VIP_SLICE1) {
895                         val |= VIP_RGB_OUT_LO_SRC_SELECT;
896                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
897                                      VIP_DATAPATH_SELECT_SHFT);
899                         write_vreg(dev, VIP_VIP1_DATA_PATH_SELECT, val);
900                 } else if (dev->slice_id == VIP_SLICE2) {
901                         val |= VIP_RGB_OUT_LO_SRC_SELECT;
902                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
903                                      VIP_DATAPATH_SELECT_SHFT);
905                         write_vreg(dev, VIP_VIP2_DATA_PATH_SELECT, val);
906                 }
907                 break;
908         default:
909                 BUG();
910         }
913 /*
914  * Return the vip_stream structure for a given struct file
915  */
916 static inline struct vip_stream *file2stream(struct file *file)
918         return video_drvdata(file);
922 /*
923  * Append a destination descriptor to the current descriptor list,
924  * setting up dma to the given srce.
925  */
926 static int add_out_dtd(struct vip_stream *stream, int srce_type)
928         struct vip_port *port = stream->port;
929         struct vip_dev *dev = port->dev;
930         struct vip_srce_info *sinfo = &srce_info[srce_type];
931         struct v4l2_rect *c_rect = &port->c_rect;
932         struct vip_fmt *fmt = port->fmt;
933         int channel, plane = 0;
934         int max_width, max_height;
935         dma_addr_t dma_addr;
936         u32 flags;
938         channel = sinfo->base_channel;
940         switch (srce_type) {
941         case VIP_SRCE_MULT_PORT:
942         case VIP_SRCE_MULT_ANC:
943                 if (port->port_id == VIP_PORTB)
944                         channel += VIP_CHAN_MULT_PORTB_OFFSET;
945                 channel += stream->stream_id;
946                 flags = 0;
947                 break;
948         case VIP_SRCE_CHROMA:
949                 plane = 1;
950         case VIP_SRCE_LUMA:
951                 if (port->port_id == VIP_PORTB)
952                         channel += VIP_CHAN_YUV_PORTB_OFFSET;
953                 flags = port->flags;
954                 break;
955         case VIP_SRCE_RGB:
956                 if (port->port_id == VIP_PORTB)
957                         channel += VIP_CHAN_RGB_PORTB_OFFSET;
958                 flags = port->flags;
959                 break;
960         default:
961                 BUG();
962         }
964         if (dev->slice_id == VIP_SLICE2)
965                 channel += VIP_CHAN_VIP2_OFFSET;
967         /* This is just for initialization purposes.
968          * The actual dma_addr will be configured in vpdma_update_dma_addr
969          */
970         dma_addr = (dma_addr_t)NULL;
972         /*
973          * Use VPDMA_MAX_SIZE1 or VPDMA_MAX_SIZE2 register for slice0/1
974          */
976         if (dev->slice_id == VIP_SLICE1) {
977                 vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE1,
978                         stream->width, stream->height);
980                 max_width = MAX_OUT_WIDTH_REG1;
981                 max_height = MAX_OUT_HEIGHT_REG1;
982         } else {
983                 vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE2,
984                         stream->width, stream->height);
986                 max_width = MAX_OUT_WIDTH_REG2;
987                 max_height = MAX_OUT_HEIGHT_REG2;
988         }
990         /* Mark this channel to be cleared while cleaning up resources
991          * This will make sure that an abort descriptor for this channel
992          * would be submitted to VPDMA causing any ongoing  transaction to be
993          * aborted and cleanup the VPDMA FSM for this channel */
994         stream->vpdma_channels[channel] = 1;
996         vpdma_rawchan_add_out_dtd(&stream->desc_list, c_rect->width, c_rect,
997                 fmt->vpdma_fmt[plane], dma_addr, max_width, max_height,
998                 channel, flags);
1000         return 0;
1003 /*
1004  * add_stream_dtds - prepares and starts DMA for pending transfers
1005  */
1006 static void add_stream_dtds(struct vip_stream *stream)
1008         struct vip_port *port = stream->port;
1009         int srce_type;
1011         if (port->flags & FLAG_MULT_PORT)
1012                 srce_type = VIP_SRCE_MULT_PORT;
1013         else if (port->flags & FLAG_MULT_ANC)
1014                 srce_type = VIP_SRCE_MULT_ANC;
1015         else if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
1016                 srce_type = VIP_SRCE_RGB;
1017         else
1018                 srce_type = VIP_SRCE_LUMA;
1020         add_out_dtd(stream, srce_type);
1022         if (srce_type == VIP_SRCE_LUMA && port->fmt->coplanar)
1023                 add_out_dtd(stream, VIP_SRCE_CHROMA);
1026 static void enable_irqs(struct vip_dev *dev, int irq_num, int list_num)
1028         u32 reg_addr = VIP_INT0_ENABLE0_SET +
1029                         VIP_INTC_INTX_OFFSET * irq_num;
1031         write_sreg(dev->shared, reg_addr, 1 << (list_num * 2));
1033         vpdma_enable_list_complete_irq(dev->shared->vpdma,
1034                 irq_num, list_num, true);
1037 static void disable_irqs(struct vip_dev *dev, int irq_num, int list_num)
1039         u32 reg_addr = VIP_INT0_ENABLE0_CLR +
1040                         VIP_INTC_INTX_OFFSET * irq_num;
1042         write_sreg(dev->shared, reg_addr, 1 << (list_num * 2));
1044         vpdma_enable_list_complete_irq(dev->shared->vpdma,
1045                 irq_num, list_num, false);
1048 static void clear_irqs(struct vip_dev *dev, int irq_num, int list_num)
1050         u32 reg_addr = VIP_INT0_STATUS0_CLR +
1051                         VIP_INTC_INTX_OFFSET * irq_num;
1053         write_sreg(dev->shared, reg_addr, 1 << (list_num * 2));
1055         vpdma_clear_list_stat(dev->shared->vpdma, irq_num, dev->slice_id);
1058 static void populate_desc_list(struct vip_stream *stream)
1060         struct vip_port *port = stream->port;
1061         struct vip_dev *dev = port->dev;
1062         unsigned int list_length;
1064         stream->desc_next = stream->desc_list.buf.addr;
1065         add_stream_dtds(stream);
1067         list_length = stream->desc_next - stream->desc_list.buf.addr;
1068         vpdma_map_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
1071 /*
1072  * start_dma - adds descriptors to the dma list and submits them.
1073  * Should be called after a new vb is queued and on a vpdma list
1074  * completion interrupt.
1075  */
1076 static void start_dma(struct vip_stream *stream, struct vip_buffer *buf)
1078         struct vip_dev *dev = stream->port->dev;
1079         struct vpdma_data *vpdma = dev->shared->vpdma;
1080         int list_num = stream->list_num;
1081         dma_addr_t dma_addr;
1082         int drop_data;
1084         if (vpdma_list_busy(vpdma, list_num)) {
1085                 vip_err(dev, "vpdma list busy, cannot post");
1086                 return;                         /* nothing to do */
1087         }
1089         if (buf) {
1090                 dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vb, 0);
1091                 drop_data = 0;
1092                 vip_dbg(4, dev, "start_dma: buf:0x%08x, vb:0x%08x, dma_addr:0x%08x\n",
1093                         (unsigned int)buf, (unsigned int)&buf->vb, dma_addr);
1094         } else {
1095                 dma_addr = (dma_addr_t)NULL;
1096                 drop_data = 1;
1097                 vip_dbg(4, dev, "start_dma: dropped\n");
1098         }
1100         vpdma_update_dma_addr(dev->shared->vpdma, &stream->desc_list,
1101                               dma_addr, stream->write_desc, drop_data, 0);
1103         if (stream->port->fmt->coplanar) {
1104                 dma_addr += stream->width * stream->height;
1105                 vpdma_update_dma_addr(dev->shared->vpdma, &stream->desc_list,
1106                               dma_addr, stream->write_desc + 1, drop_data, 1);
1107         }
1109         vpdma_submit_descs(dev->shared->vpdma,
1110                         &stream->desc_list, stream->list_num);
1113 static void vip_schedule_next_buffer(struct vip_stream *stream)
1115         struct vip_dev *dev = stream->port->dev;
1116         struct vip_buffer *buf;
1117         unsigned long flags;
1119         spin_lock_irqsave(&dev->slock, flags);
1120         if (list_empty(&stream->vidq)) {
1121                 vip_dbg(4, dev, "Dropping frame\n");
1122                 if (list_empty(&stream->dropq)) {
1123                         vip_err(dev, "No dropq buffer left!");
1124                         spin_unlock_irqrestore(&dev->slock, flags);
1125                         return;
1126                 }
1127                 buf = list_entry(stream->dropq.next,
1128                                  struct vip_buffer, list);
1130                 buf->drop = true;
1131                 list_move_tail(&buf->list, &stream->post_bufs);
1132                 buf = NULL;
1133         } else if (vb2_is_streaming(&stream->vb_vidq)) {
1134                 buf = list_entry(stream->vidq.next,
1135                                 struct vip_buffer, list);
1136                 buf->drop = false;
1137                 list_move_tail(&buf->list, &stream->post_bufs);
1138                 vip_dbg(4, dev, "added next buffer\n");
1139         } else {
1140                 vip_err(dev, "IRQ occurred when not streaming\n");
1141                 if (list_empty(&stream->dropq)) {
1142                         vip_err(dev, "No dropq buffer left!");
1143                         spin_unlock_irqrestore(&dev->slock, flags);
1144                         return;
1145                 }
1146                 buf = list_entry(stream->dropq.next,
1147                                  struct vip_buffer, list);
1148                 buf->drop = true;
1149                 list_move_tail(&buf->list, &stream->post_bufs);
1150                 buf = NULL;
1151         }
1153         spin_unlock_irqrestore(&dev->slock, flags);
1154         start_dma(stream, buf);
1157 static void vip_process_buffer_complete(struct vip_stream *stream)
1159         struct vip_dev *dev = stream->port->dev;
1160         struct vip_buffer *buf;
1161         struct vb2_buffer *vb = NULL;
1162         unsigned long flags, fld;
1164         buf = list_first_entry(&stream->post_bufs, struct vip_buffer, list);
1166         if (stream->port->flags & FLAG_INTERLACED) {
1167                 vpdma_unmap_desc_buf(dev->shared->vpdma,
1168                                         &stream->desc_list.buf);
1170                 fld = dtd_get_field(stream->write_desc);
1171                 stream->field = fld ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
1173                 vpdma_map_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
1174         }
1176         if (buf) {
1177                 vip_dbg(4, dev, "vip buffer complete 0x%x, 0x%x\n",
1178                         (unsigned int)buf, buf->drop);
1180                 vb = &buf->vb;
1181                 vb->v4l2_buf.field = stream->field;
1182                 vb->v4l2_buf.sequence = stream->sequence;
1183                 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
1185                 if (buf->drop) {
1186                         spin_lock_irqsave(&dev->slock, flags);
1187                         list_move_tail(&buf->list, &stream->dropq);
1188                         spin_unlock_irqrestore(&dev->slock, flags);
1189                 } else {
1190                         spin_lock_irqsave(&dev->slock, flags);
1191                         list_del(&buf->list);
1192                         spin_unlock_irqrestore(&dev->slock, flags);
1193                         vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1194                 }
1195         } else {
1196                 BUG();
1197         }
1199         stream->sequence++;
1202 static irqreturn_t vip_irq(int irq_vip, void *data)
1204         struct vip_dev *dev = (struct vip_dev *)data;
1205         struct vpdma_data *vpdma = dev->shared->vpdma;
1206         struct vip_stream *stream;
1207         int list_num;
1208         int irq_num = dev->slice_id;
1209         u32 irqst, reg_addr;
1211         if (!dev->shared)
1212                 return IRQ_HANDLED;
1214         reg_addr = VIP_INT0_STATUS0 +
1215                         VIP_INTC_INTX_OFFSET * irq_num;
1216         irqst = read_sreg(dev->shared, reg_addr);
1218         vip_dbg(8, dev, "IRQ %d VIP_INT%d_STATUS0 0x%x\n",
1219                 irq_vip, irq_num, irqst);
1220         if (irqst) {
1221                 reg_addr = VIP_INT0_STATUS0_CLR +
1222                         VIP_INTC_INTX_OFFSET * irq_num;
1223                 write_sreg(dev->shared, reg_addr, irqst);
1225                 for (list_num = 0; list_num < 8;  list_num++) {
1226                         /* Check for LIST_COMPLETE IRQ */
1227                         if (!(irqst & (1 << list_num * 2)))
1228                                 continue;
1230                         vip_dbg(8, dev, "IRQ %d: handling LIST%d_COMPLETE\n",
1231                                 irq_num, list_num);
1233                         stream = vpdma_hwlist_get_priv(vpdma, list_num);
1234                         if (!stream || stream->list_num != list_num) {
1235                                 vip_err(dev, "IRQ occured for unused list");
1236                                 continue;
1237                         }
1239                         vpdma_clear_list_stat(vpdma, irq_num, list_num);
1241                         if (dev->num_skip_irq)
1242                                 dev->num_skip_irq--;
1243                         else
1244                                 vip_process_buffer_complete(stream);
1246                         vip_schedule_next_buffer(stream);
1247                         irqst &= ~((1 << list_num * 2));
1248                 }
1249         }
1251         return IRQ_HANDLED;
1254 /*
1255  * video ioctls
1256  */
1257 static int vip_querycap(struct file *file, void *priv,
1258                         struct v4l2_capability *cap)
1260         strncpy(cap->driver, VIP_MODULE_NAME, sizeof(cap->driver) - 1);
1261         strncpy(cap->card, VIP_MODULE_NAME, sizeof(cap->card) - 1);
1262         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1263                 VIP_MODULE_NAME);
1264         cap->device_caps  = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE |
1265                             V4L2_CAP_READWRITE;
1266         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1267         return 0;
1270 static int vip_enuminput(struct file *file, void *priv,
1271                                 struct v4l2_input *inp)
1273         struct vip_stream *stream = file2stream(file);
1275         if (inp->index)
1276                 return -EINVAL;
1278         inp->type = V4L2_INPUT_TYPE_CAMERA;
1279         inp->std = stream->vfd->tvnorms;
1280         sprintf(inp->name, "camera %u", stream->vfd->num);
1282         return 0;
1285 static int vip_g_input(struct file *file, void *priv, unsigned int *i)
1287         *i = 0;
1288         return 0;
1291 static int vip_s_input(struct file *file, void *priv, unsigned int i)
1293         if (i != 0)
1294                 return -EINVAL;
1295         return 0;
1298 static int vip_querystd(struct file *file, void *fh, v4l2_std_id *std)
1300         struct vip_stream *stream = file2stream(file);
1301         struct vip_dev *dev = stream->port->dev;
1303         *std = stream->vfd->tvnorms;
1304         v4l2_subdev_call(dev->sensor, video, querystd, std);
1305         vip_dbg(1, dev, "querystd: 0x%lx\n", (unsigned long)*std);
1306         return 0;
1309 static int vip_g_std(struct file *file, void *fh, v4l2_std_id *std)
1311         struct vip_stream *stream = file2stream(file);
1312         struct vip_dev *dev = stream->port->dev;
1314         *std = stream->vfd->tvnorms;
1315         v4l2_subdev_call(dev->sensor, video, g_std_output, std);
1316         vip_dbg(1, dev, "g_std: 0x%lx\n", (unsigned long)*std);
1318         return 0;
1321 static int vip_s_std(struct file *file, void *fh, v4l2_std_id std)
1323         struct vip_stream *stream = file2stream(file);
1324         struct vip_dev *dev = stream->port->dev;
1326         vip_dbg(1, dev, "s_std: 0x%lx\n", (unsigned long)std);
1328         if (!(std & stream->vfd->tvnorms)) {
1329                 vip_dbg(1, dev, "s_std after check: 0x%lx\n",
1330                         (unsigned long)std);
1331                 return -EINVAL;
1332         }
1334         v4l2_subdev_call(dev->sensor, video, s_std_output, std);
1335         return 0;
1338 static int vip_enum_fmt_vid_cap(struct file *file, void *priv,
1339                                 struct v4l2_fmtdesc *f)
1341         struct vip_stream *stream = file2stream(file);
1342         struct vip_dev *dev = stream->port->dev;
1343         struct vip_fmt *fmt = NULL;
1345         vip_dbg(3, dev, "enum_fmt index:%d\n", f->index);
1347         if (f->index >= dev->num_active_fmt)
1348                 return -EINVAL;
1350         fmt = dev->active_fmt[f->index];
1352         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
1353         f->pixelformat = fmt->fourcc;
1355         vip_dbg(3, dev, "enum_fmt fourcc:%s description:%s\n",
1356                 fourcc_to_str(f->pixelformat), f->description);
1358         return 0;
1361 /*
1362   * TODO: Change from hard coding values to reading these through
1363   * IOCTLS directly from sensor
1364   */
1366 static int vip_enum_framesizes(struct file *file, void *priv,
1367                         struct v4l2_frmsizeenum *f)
1369         struct vip_stream *stream = file2stream(file);
1370         struct vip_dev *dev = stream->port->dev;
1371         struct vip_port *port = stream->port;
1372         struct vip_fmt *fmt;
1374         fmt = find_port_format_by_pix(port, f->pixel_format);
1375         if (!fmt)
1376                 return -EINVAL;
1378         return v4l2_subdev_call(dev->sensor, video, enum_framesizes, f);
1381 static int vip_enum_frameintervals(struct file *file, void *priv,
1382                         struct v4l2_frmivalenum *f)
1384         struct vip_stream *stream = file2stream(file);
1385         struct vip_dev *dev = stream->port->dev;
1386         struct vip_port *port = stream->port;
1387         struct v4l2_frmsizeenum fsize;
1388         struct vip_fmt *fmt;
1389         int ret;
1391         if (f->index)
1392                 return -EINVAL;
1394         fmt = find_port_format_by_pix(port, f->pixel_format);
1395         if (!fmt)
1396                 return -EINVAL;
1398         /* check for valid width/height */
1399         ret = 0;
1400         for (fsize.index = 0; ; fsize.index++) {
1401                 ret = v4l2_subdev_call(dev->sensor, video,
1402                                         enum_framesizes, &fsize);
1403                 if (ret) {
1404                         return -EINVAL;
1405                 }
1407                 if (fsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1408                         if ((f->width == fsize.discrete.width) &&
1409                             (f->height == fsize.discrete.height))
1410                                 break;
1411                 } else if ((fsize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) ||
1412                            (fsize.type == V4L2_FRMSIZE_TYPE_STEPWISE)) {
1413                         if ((f->width >= fsize.stepwise.min_width) &&
1414                             (f->width <= fsize.stepwise.max_width) &&
1415                             (f->height >= fsize.stepwise.min_height) &&
1416                             (f->height <= fsize.stepwise.max_height))
1417                                 break;
1418                 } else {
1419                         return -EINVAL;
1420                 }
1421         }
1423         f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1424         f->discrete.numerator = 1;
1425         f->discrete.denominator = 30;
1427         return 0;
1430 static int vip_g_parm(struct file *file, void *priv,
1431                       struct v4l2_streamparm *parm)
1433         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1434                 return -EINVAL;
1436         parm->parm.capture.capability   = V4L2_CAP_TIMEPERFRAME;
1437         parm->parm.capture.timeperframe.numerator = 1;
1438         parm->parm.capture.timeperframe.denominator = 30;
1439         parm->parm.capture.readbuffers  = 4;
1440         return 0;
1443 static int vip_s_parm(struct file *file, void *priv,
1444                         struct v4l2_streamparm *parm)
1446         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1447                 return -EINVAL;
1449         parm->parm.capture.timeperframe.numerator = 1;
1450         parm->parm.capture.timeperframe.denominator = 30;
1451         parm->parm.capture.readbuffers  = 4;
1453         return 0;
1456 static int vip_calc_format_size(struct vip_port *port,
1457                                 struct vip_fmt *fmt,
1458                                 struct v4l2_format *f)
1460         struct vip_dev *dev = port->dev;
1461         enum v4l2_field *field;
1463         if (!fmt) {
1464                 vip_dbg(2, dev,
1465                         "no vip_fmt format provided!\n");
1466                 return -EINVAL;
1467         }
1469         field = &f->fmt.pix.field;
1470         if (*field == V4L2_FIELD_ANY)
1471                 *field = V4L2_FIELD_NONE;
1472         else if (V4L2_FIELD_NONE != *field && V4L2_FIELD_ALTERNATE != *field)
1473                 return -EINVAL;
1475         v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W, W_ALIGN,
1476                               &f->fmt.pix.height, MIN_H, MAX_H, H_ALIGN,
1477                               S_ALIGN);
1479         f->fmt.pix.bytesperline = f->fmt.pix.width *
1480                                   (fmt->vpdma_fmt[0]->depth >> 3);
1481         f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
1482                                         VPDMA_STRIDE_ALIGN);
1483         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.width *
1484                 (fmt->vpdma_fmt[0]->depth +
1485                  (fmt->coplanar ? fmt->vpdma_fmt[1]->depth : 0)) >> 3;
1486         f->fmt.pix.colorspace = fmt->colorspace;
1487         f->fmt.pix.priv = 0;
1489         vip_dbg(3, dev, "calc_format_size: fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1490                 fourcc_to_str(f->fmt.pix.pixelformat),
1491                 f->fmt.pix.width, f->fmt.pix.height,
1492                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1494         return 0;
1497 static int vip_try_fmt_vid_cap(struct file *file, void *priv,
1498                                struct v4l2_format *f)
1500         struct vip_stream *stream = file2stream(file);
1501         struct vip_port *port = stream->port;
1502         struct vip_dev *dev = port->dev;
1503         struct v4l2_frmsizeenum fsize;
1504         struct vip_fmt *fmt;
1505         int ret, found;
1507         vip_dbg(3, dev, "try_fmt fourcc:%s size: %dx%d\n",
1508                 fourcc_to_str(f->fmt.pix.pixelformat),
1509                 f->fmt.pix.width, f->fmt.pix.height);
1511         fmt = find_port_format_by_pix(port, f->fmt.pix.pixelformat);
1512         if (!fmt) {
1513                 vip_dbg(2, dev,
1514                         "Fourcc format (0x%08x) not found.\n",
1515                         f->fmt.pix.pixelformat);
1517                 /* Just get the first one enumerated */
1518                 fmt = dev->active_fmt[0];
1519                 f->fmt.pix.pixelformat = fmt->fourcc;
1520         }
1522         /* check for/find a valid width/height */
1523         ret = 0;
1524         found = false;
1525         for (fsize.index = 0; ; fsize.index++) {
1526                 ret = v4l2_subdev_call(dev->sensor, video,
1527                                         enum_framesizes, &fsize);
1528                 if (ret)
1529                         break;
1531                 if (fsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1532                         if ((f->fmt.pix.width == fsize.discrete.width) &&
1533                             (f->fmt.pix.height == fsize.discrete.height)) {
1534                                 found = true;
1535                                 break;
1536                         }
1537                 } else if ((fsize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) ||
1538                            (fsize.type == V4L2_FRMSIZE_TYPE_STEPWISE)) {
1539                         if ((f->fmt.pix.width >= fsize.stepwise.min_width) &&
1540                             (f->fmt.pix.width <= fsize.stepwise.max_width) &&
1541                             (f->fmt.pix.height >= fsize.stepwise.min_height) &&
1542                             (f->fmt.pix.height <= fsize.stepwise.max_height)) {
1543                                 found = true;
1544                                 break;
1545                         }
1546                 }
1547         }
1549         if (!found) {
1550                 /* use existing values as default */
1551                 f->fmt.pix.width = port->mbus_framefmt.width;
1552                 f->fmt.pix.height =  port->mbus_framefmt.height;
1553         }
1555         /* That we have a fmt calculate imagesize and bytesperline */
1556         return vip_calc_format_size(port, fmt, f);
1559 static int vip_g_fmt_vid_cap(struct file *file, void *priv,
1560                              struct v4l2_format *f)
1562         struct vip_stream *stream = file2stream(file);
1563         struct vip_port *port = stream->port;
1564         struct vip_dev *dev = stream->port->dev;
1565         struct vip_fmt *fmt = port->fmt;
1567         /* Use last known values or defaults */
1568         f->fmt.pix.width        = stream->width;
1569         f->fmt.pix.height       = stream->height;
1570         f->fmt.pix.pixelformat  = port->fmt->fourcc;
1571         f->fmt.pix.field        = stream->sup_field;
1572         f->fmt.pix.colorspace   = port->fmt->colorspace;
1573         f->fmt.pix.bytesperline = stream->bytesperline;
1574         f->fmt.pix.sizeimage    = stream->sizeimage;
1576         vip_dbg(3, dev,
1577                 "g_fmt fourcc:%s code: %04x size: %dx%d bpl:%d img_size:%d\n",
1578                 fourcc_to_str(f->fmt.pix.pixelformat),
1579                 fmt->code,
1580                 f->fmt.pix.width, f->fmt.pix.height,
1581                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1582         vip_dbg(3, dev, "g_fmt vpdma data type: 0x%02X\n",
1583                 port->fmt->vpdma_fmt[0]->data_type);
1585         return 0;
1588 static int vip_s_fmt_vid_cap(struct file *file, void *priv,
1589                              struct v4l2_format *f)
1591         struct vip_stream *stream = file2stream(file);
1592         struct vip_port *port = stream->port;
1593         struct vip_dev *dev = port->dev;
1594         struct v4l2_subdev_format sfmt;
1595         struct v4l2_mbus_framefmt *mf;
1596         int ret;
1598         vip_dbg(3, dev, "s_fmt input fourcc:%s size: %dx%d\n",
1599                 fourcc_to_str(f->fmt.pix.pixelformat),
1600                 f->fmt.pix.width, f->fmt.pix.height);
1602         ret = vip_try_fmt_vid_cap(file, priv, f);
1603         if (ret)
1604                 return ret;
1606         vip_dbg(3, dev, "s_fmt try_fmt fourcc:%s size: %dx%d\n",
1607                 fourcc_to_str(f->fmt.pix.pixelformat),
1608                 f->fmt.pix.width, f->fmt.pix.height);
1610         if (vb2_is_busy(&stream->vb_vidq)) {
1611                 vip_err(dev, "%s queue busy\n", __func__);
1612                 return -EBUSY;
1613         }
1615         port->fmt = find_port_format_by_pix(port,
1616                                             f->fmt.pix.pixelformat);
1617         stream->width           = f->fmt.pix.width;
1618         stream->height          = f->fmt.pix.height;
1619         stream->bytesperline    = f->fmt.pix.bytesperline;
1620         stream->sizeimage       = f->fmt.pix.sizeimage;
1621         stream->sup_field       = f->fmt.pix.field;
1623         port->c_rect.left       = 0;
1624         port->c_rect.top        = 0;
1625         port->c_rect.width      = stream->width;
1626         port->c_rect.height     = stream->height;
1628         if (stream->sup_field == V4L2_FIELD_ALTERNATE)
1629                 port->flags |= FLAG_INTERLACED;
1630         else
1631                 port->flags &= ~FLAG_INTERLACED;
1633         vip_dbg(3, dev, "s_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1634                 fourcc_to_str(f->fmt.pix.pixelformat),
1635                 f->fmt.pix.width, f->fmt.pix.height,
1636                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1638         mf = &sfmt.format;
1639         v4l2_fill_mbus_format(mf, &f->fmt.pix, port->fmt->code);
1641         /* Save it */
1642         port->mbus_framefmt = *mf;
1644         vip_dbg(3, dev, "s_fmt pix_to_mbus mbus_code: %04X size: %dx%d\n",
1645                 mf->code,
1646                 mf->width, mf->height);
1648         ret = v4l2_subdev_call(dev->sensor, video, try_mbus_fmt, mf);
1649         if (ret) {
1650                 vip_dbg(1, dev, "try_mbus_fmt failed in subdev\n");
1651                 return ret;
1652         }
1653         vip_dbg(3, dev, "s_fmt subdev try_fmt mbus_code: %04X size: %dx%d\n",
1654                 mf->code,
1655                 mf->width, mf->height);
1656         ret = v4l2_subdev_call(dev->sensor, video, s_mbus_fmt, mf);
1657         if (ret) {
1658                 vip_dbg(1, dev, "s_mbus_fmt failed in subdev\n");
1659                 return ret;
1660         }
1661         vip_dbg(3, dev, "s_fmt subdev s_fmt mbus_code: %04X size: %dx%d\n",
1662                 mf->code,
1663                 mf->width, mf->height);
1664         vip_dbg(3, dev, "s_fmt vpdma data type: 0x%02X\n",
1665                 port->fmt->vpdma_fmt[0]->data_type);
1667         return 0;
1670 /*
1671  * Set the registers that are modified when the video format changes.
1672  */
1673 static void set_fmt_params(struct vip_stream *stream)
1675         struct vip_dev *dev = stream->port->dev;
1676         int data_path_reg;
1678         stream->sequence = 0;
1679         stream->field = V4L2_FIELD_TOP;
1681         if (stream->port->fmt->colorspace == V4L2_COLORSPACE_SRGB) {
1682                 vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT);
1683                 /* Set alpha component in background color */
1684                 vpdma_set_bg_color(dev->shared->vpdma,
1685                                    (struct vpdma_data_format *)
1686                                    stream->port->fmt->vpdma_fmt[0],
1687                                    0xff);
1688         }
1690         data_path_reg = VIP_VIP1_DATA_PATH_SELECT + 4 * dev->slice_id;
1691         if (stream->port->fmt->coplanar) {
1692                 stream->port->flags &= ~FLAG_MULT_PORT;
1693                 write_vreg(dev, data_path_reg, 0x600);
1694         } else {
1695                 stream->port->flags |= FLAG_MULT_PORT;
1696                 write_vreg(dev, data_path_reg, 0x8000);
1697         }
1700 static int vip_g_selection(struct file *file, void *fh,
1701                            struct v4l2_selection *s)
1703         struct vip_stream *stream = file2stream(file);
1705         switch (s->target) {
1706         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1707         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1708         case V4L2_SEL_TGT_CROP_BOUNDS:
1709         case V4L2_SEL_TGT_CROP_DEFAULT:
1710                 s->r.left = 0;
1711                 s->r.top = 0;
1712                 s->r.width = stream->width;
1713                 s->r.height = stream->height;
1714                 return 0;
1716         case V4L2_SEL_TGT_COMPOSE:
1717         case V4L2_SEL_TGT_CROP:
1718                 s->r = stream->port->c_rect;
1719                 return 0;
1720         }
1722         return -EINVAL;
1725 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1727         if (a->left < b->left || a->top < b->top)
1728                 return 0;
1729         if (a->left + a->width > b->left + b->width)
1730                 return 0;
1731         if (a->top + a->height > b->top + b->height)
1732                 return 0;
1734         return 1;
1737 static int vip_s_selection(struct file *file, void *fh,
1738                            struct v4l2_selection *s)
1740         struct vip_stream *stream = file2stream(file);
1741         struct vip_port *port = stream->port;
1742         struct v4l2_rect r = s->r;
1744         v4l_bound_align_image(&r.width, 0, stream->width, 0,
1745                               &r.height, 0, stream->height, 0, 0);
1747         r.left = clamp_t(unsigned int, r.left, 0, stream->width - r.width);
1748         r.top  = clamp_t(unsigned int, r.top, 0, stream->height - r.height);
1750         if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
1751                 return -ERANGE;
1753         if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
1754                 return -ERANGE;
1756         s->r = stream->port->c_rect = r;
1758         set_fmt_params(stream);
1760         vip_dbg(1, port->dev, "cropped (%d,%d)/%dx%d of %dx%d\n",
1761                 r.left, r.top, r.width, r.height,
1762                 stream->width, stream->height);
1764         return 0;
1767 static long vip_ioctl_default(struct file *file, void *fh, bool valid_prio,
1768                               unsigned int cmd, void *arg)
1770         struct vip_stream *stream = file2stream(file);
1771         struct vip_port *port = stream->port;
1773         if (!valid_prio) {
1774                 vip_err(port->dev, "%s device busy\n", __func__);
1775                 return -EBUSY;
1776         }
1778         switch (cmd) {
1779         default:
1780                 return -ENOTTY;
1781         }
1784 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
1785         .vidioc_querycap        = vip_querycap,
1786         .vidioc_enum_input      = vip_enuminput,
1787         .vidioc_g_input         = vip_g_input,
1788         .vidioc_s_input         = vip_s_input,
1790         .vidioc_querystd        = vip_querystd,
1791         .vidioc_g_std           = vip_g_std,
1792         .vidioc_s_std           = vip_s_std,
1794         .vidioc_enum_fmt_vid_cap = vip_enum_fmt_vid_cap,
1795         .vidioc_g_fmt_vid_cap   = vip_g_fmt_vid_cap,
1796         .vidioc_try_fmt_vid_cap = vip_try_fmt_vid_cap,
1797         .vidioc_s_fmt_vid_cap   = vip_s_fmt_vid_cap,
1799         .vidioc_enum_frameintervals     = vip_enum_frameintervals,
1800         .vidioc_enum_framesizes         = vip_enum_framesizes,
1801         .vidioc_s_parm                  = vip_s_parm,
1802         .vidioc_g_parm                  = vip_g_parm,
1803         .vidioc_g_selection     = vip_g_selection,
1804         .vidioc_s_selection     = vip_s_selection,
1805         .vidioc_reqbufs         = vb2_ioctl_reqbufs,
1806         .vidioc_create_bufs     = vb2_ioctl_create_bufs,
1807         .vidioc_prepare_buf     = vb2_ioctl_prepare_buf,
1808         .vidioc_querybuf        = vb2_ioctl_querybuf,
1809         .vidioc_qbuf            = vb2_ioctl_qbuf,
1810         .vidioc_dqbuf           = vb2_ioctl_dqbuf,
1812         .vidioc_streamon        = vb2_ioctl_streamon,
1813         .vidioc_streamoff       = vb2_ioctl_streamoff,
1814         .vidioc_log_status      = v4l2_ctrl_log_status,
1815         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1816         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1817         .vidioc_default         = vip_ioctl_default,
1818 };
1820 /*
1821  * Videobuf operations
1822  */
1823 static int vip_queue_setup(struct vb2_queue *vq,
1824                            const struct v4l2_format *fmt,
1825                            unsigned int *nbuffers, unsigned int *nplanes,
1826                            unsigned int sizes[], void *alloc_ctxs[])
1828         struct vip_stream *stream = vb2_get_drv_priv(vq);
1829         struct vip_dev *dev = stream->port->dev;
1831         *nplanes = 1;
1832         sizes[0] = stream->sizeimage;
1833         alloc_ctxs[0] = dev->alloc_ctx;
1834         vip_dbg(1, dev, "get %d buffer(s) of size %d each.\n",
1835                 *nbuffers, sizes[0]);
1837         return 0;
1840 static int vip_buf_prepare(struct vb2_buffer *vb)
1842         struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
1843         struct vip_dev *dev = stream->port->dev;
1845         if (vb2_plane_size(vb, 0) < stream->sizeimage) {
1846                 vip_dbg(1, dev,
1847                         "%s data will not fit into plane (%lu < %lu)\n",
1848                         __func__, vb2_plane_size(vb, 0),
1849                         (long)stream->sizeimage);
1850                 return -EINVAL;
1851         }
1853         vb2_set_plane_payload(vb, 0, stream->sizeimage);
1855         return 0;
1858 static void vip_buf_queue(struct vb2_buffer *vb)
1860         struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
1861         struct vip_dev *dev = stream->port->dev;
1862         struct vip_buffer *buf = container_of(vb, struct vip_buffer, vb);
1863         unsigned long flags;
1865         spin_lock_irqsave(&dev->slock, flags);
1866         list_add_tail(&buf->list, &stream->vidq);
1867         spin_unlock_irqrestore(&dev->slock, flags);
1870 static int vip_start_streaming(struct vb2_queue *vq, unsigned int count)
1872         struct vip_stream *stream = vb2_get_drv_priv(vq);
1873         struct vip_port *port = stream->port;
1874         struct vip_dev *dev = port->dev;
1875         struct vip_buffer *buf;
1876         unsigned long flags;
1877         int ret;
1879         set_fmt_params(stream);
1880         vip_setup_parser(dev->ports[0]);
1882         buf = list_entry(stream->vidq.next,
1883                          struct vip_buffer, list);
1885         vip_dbg(2, dev, "start_streaming: buf 0x%x %d\n",
1886                 (unsigned int)buf, count);
1887         buf->drop = false;
1888         stream->sequence = 0;
1889         stream->field = V4L2_FIELD_TOP;
1891         if (dev->sensor) {
1892                 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1893                 if (ret) {
1894                         vip_dbg(1, dev, "stream on failed in subdev\n");
1895                         return ret;
1896                 }
1897         }
1899         populate_desc_list(stream);
1901         /* The first few VPDMA ListComplete interrupts fire pretty quiclky
1902          * until the internal VPDMA descriptor fifo is full.
1903          * The subsequent ListComplete interrupts will fire at the actual
1904          * capture frame rate. The first few interrupts are therefore used
1905          * only to queue up descriptors, and then they will also be used
1906          * as End of Frame (EOF) event
1907          */
1908         dev->num_skip_irq = VIP_VPDMA_FIFO_SIZE;
1910         spin_lock_irqsave(&dev->slock, flags);
1911         if (vpdma_list_busy(dev->shared->vpdma, stream->list_num)) {
1912                 spin_unlock_irqrestore(&dev->slock, flags);
1913                 vpdma_unmap_desc_buf(dev->shared->vpdma,
1914                                 &stream->desc_list.buf);
1915                 vpdma_reset_desc_list(&stream->desc_list);
1916                 return -EBUSY;
1917         }
1919         list_move_tail(&buf->list, &stream->post_bufs);
1920         spin_unlock_irqrestore(&dev->slock, flags);
1922         vip_dbg(2, dev, "start_streaming: start_dma buf 0x%x\n",
1923                 (unsigned int)buf);
1924         start_dma(stream, buf);
1926         /* We enable the irq after posting the vpdma descriptor
1927          * to prevent sprurious interrupt coming in before the
1928          * vb2 layer is completely ready to handle them
1929          * otherwise the vb2_streaming test would fail early on
1930           */
1931         enable_irqs(dev, dev->slice_id, stream->list_num);
1933         return 0;
1936 /*
1937  * Abort streaming and wait for last buffer
1938  */
1939 static int vip_stop_streaming(struct vb2_queue *vq)
1941         struct vip_stream *stream = vb2_get_drv_priv(vq);
1942         struct vip_port *port = stream->port;
1943         struct vip_dev *dev = port->dev;
1944         struct vip_buffer *buf;
1945         int ret;
1947         if (dev->sensor) {
1948                 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 0);
1949                 if (ret)
1950                         vip_dbg(1, dev, "stream on failed in subdev\n");
1951         }
1953         disable_irqs(dev, dev->slice_id, stream->list_num);
1954         clear_irqs(dev, dev->slice_id, stream->list_num);
1955         stop_dma(stream);
1957         /* release all active buffers */
1958         while (!list_empty(&stream->post_bufs)) {
1959                 buf = list_entry(stream->post_bufs.next,
1960                                         struct vip_buffer, list);
1961                 list_del(&buf->list);
1962                 if (buf->drop == 1)
1963                         list_add_tail(&buf->list, &stream->dropq);
1964                 else
1965                         vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1966         }
1967         while (!list_empty(&stream->vidq)) {
1968                 buf = list_entry(stream->vidq.next, struct vip_buffer, list);
1969                 list_del(&buf->list);
1970                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1971         }
1973         if (!vb2_is_streaming(vq))
1974                 return 0;
1976         vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
1977         vpdma_reset_desc_list(&stream->desc_list);
1979         return 0;
1982 /*
1983  * Lock access to the device
1984  */
1985 static void vip_lock(struct vb2_queue *vq)
1987         struct vip_stream *stream = vb2_get_drv_priv(vq);
1989         mutex_lock(&stream->port->dev->mutex);
1992 static void vip_unlock(struct vb2_queue *vq)
1994         struct vip_stream *stream = vb2_get_drv_priv(vq);
1995         mutex_unlock(&stream->port->dev->mutex);
1998 static struct vb2_ops vip_video_qops = {
1999         .queue_setup            = vip_queue_setup,
2000         .buf_prepare            = vip_buf_prepare,
2001         .buf_queue              = vip_buf_queue,
2002         .start_streaming        = vip_start_streaming,
2003         .stop_streaming         = vip_stop_streaming,
2004         .wait_prepare           = vip_unlock,
2005         .wait_finish            = vip_lock,
2006 };
2008 /*
2009  * File operations
2010  */
2012 static int vip_init_dev(struct vip_dev *dev)
2014         if (dev->num_ports != 0)
2015                 goto done;
2017         vip_set_clock_enable(dev, 1);
2018 done:
2019         dev->num_ports++;
2021         return 0;
2024 static int vip_init_port(struct vip_port *port)
2026         int ret;
2027         struct vip_dev *dev = port->dev;
2028         struct vip_fmt *fmt;
2029         struct v4l2_mbus_framefmt *mbus_fmt = &port->mbus_framefmt;
2031         if (port->num_streams != 0)
2032                 goto done;
2034         ret = vip_init_dev(port->dev);
2035         if (ret)
2036                 goto done;
2038         /* Get subdevice current frame format */
2039         ret = v4l2_subdev_call(dev->sensor, video, g_mbus_fmt, mbus_fmt);
2040         if (ret)
2041                 vip_dbg(1, dev, "init_port g_mbus_fmt failed in subdev\n");
2043         /* try to find one that matches */
2044         fmt = find_port_format_by_pix(port, mbus_fmt->code);
2045         if (!fmt) {
2046                 vip_dbg(1, dev, "subdev default mbus_fmt %04x is not matched.\n",
2047                         mbus_fmt->code);
2048                 /* if all else fails just pick the first one */
2049                 fmt = dev->active_fmt[0];
2051                 mbus_fmt->code = fmt->code;
2052                 ret = v4l2_subdev_call(dev->sensor, video,
2053                                        s_mbus_fmt, mbus_fmt);
2054                 if (ret)
2055                         vip_dbg(1, dev, "init_port s_mbus_fmt failed in subdev\n");
2056         }
2058         /* Assign current format */
2059         port->fmt = fmt;
2061         vip_dbg(3, dev, "vip_init_port: g_mbus_fmt subdev mbus_code: %04X fourcc:%s size: %dx%d\n",
2062                 fmt->code,
2063                 fourcc_to_str(fmt->fourcc),
2064                 mbus_fmt->width, mbus_fmt->height);
2066         if (mbus_fmt->field == V4L2_FIELD_ALTERNATE)
2067                 port->flags |= FLAG_INTERLACED;
2068         else
2069                 port->flags &= ~FLAG_INTERLACED;
2071         port->c_rect.left       = 0;
2072         port->c_rect.top        = 0;
2073         port->c_rect.width      = mbus_fmt->width;
2074         port->c_rect.height     = mbus_fmt->height;
2076 done:
2077         port->num_streams++;
2078         return 0;
2081 static int vip_init_stream(struct vip_stream *stream)
2083         struct vip_port *port = stream->port;
2084         struct vip_dev *dev = port->dev;
2085         struct vip_fmt *fmt;
2086         struct v4l2_mbus_framefmt *mbus_fmt;
2087         struct v4l2_format f;
2088         int ret;
2090         ret = vip_init_port(port);
2091         if (ret != 0)
2092                 return ret;
2094         fmt = port->fmt;
2095         mbus_fmt = &port->mbus_framefmt;
2097         /* Properly calculate the sizeimage and bytesperline values. */
2098         v4l2_fill_pix_format(&f.fmt.pix, mbus_fmt);
2099         f.fmt.pix.pixelformat = fmt->fourcc;
2100         ret = vip_calc_format_size(port, fmt, &f);
2101         if (ret)
2102                 return ret;
2104         stream->width = f.fmt.pix.width;
2105         stream->height = f.fmt.pix.height;
2106         stream->sup_field = f.fmt.pix.field;
2107         stream->bytesperline = f.fmt.pix.bytesperline;
2108         stream->sizeimage = f.fmt.pix.sizeimage;
2110         vip_dbg(3, dev, "init_stream fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
2111                 fourcc_to_str(f.fmt.pix.pixelformat),
2112                 f.fmt.pix.width, f.fmt.pix.height,
2113                 f.fmt.pix.bytesperline, f.fmt.pix.sizeimage);
2114         vip_dbg(3, dev, "init_stream vpdma data type: 0x%02X\n",
2115                 port->fmt->vpdma_fmt[0]->data_type);
2117         ret = vpdma_create_desc_list(&stream->desc_list, VIP_DESC_LIST_SIZE,
2118                         VPDMA_LIST_TYPE_NORMAL);
2120         if (ret != 0)
2121                 return ret;
2123         stream->write_desc = (struct vpdma_dtd *)stream->desc_list.buf.addr
2124                                 + 15;
2125         return 0;
2128 static void vip_release_dev(struct vip_dev *dev)
2130         /*
2131          * On last close, disable clocks to conserve power
2132          */
2134         if (--dev->num_ports == 0)
2135                 vip_set_clock_enable(dev, 0);
2138 static int vip_setup_parser(struct vip_port *port)
2140         struct vip_dev *dev = port->dev;
2141         struct v4l2_of_endpoint *endpoint = dev->endpoint;
2142         int iface = DUAL_8B_INTERFACE;
2143         int sync_type, pclk_type;
2144         unsigned int flags;
2146         flags = endpoint->bus.parallel.flags;
2147         vip_reset_port(port);
2148         vip_set_port_enable(port, 1);
2150         if (endpoint->bus_type == V4L2_MBUS_BT656) {
2151                 iface = DUAL_8B_INTERFACE;
2153                 /* Ideally, this should come from sensor
2154                    port->fmt can be anything once CSC is enabled */
2155                 if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
2156                         sync_type = EMBEDDED_SYNC_SINGLE_RGB_OR_YUV444;
2157                 else {
2158                         switch (endpoint->bus.parallel.num_channels) {
2159                         case 4:
2160                                 sync_type = EMBEDDED_SYNC_4X_MULTIPLEXED_YUV422;
2161                         break;
2162                         case 2:
2163                                 sync_type = EMBEDDED_SYNC_2X_MULTIPLEXED_YUV422;
2164                         break;
2165                         case 1:
2166                         default:
2167                                 sync_type =
2168                                 EMBEDDED_SYNC_LINE_MULTIPLEXED_YUV422;
2169                         }
2170                 }
2172         } else if (endpoint->bus_type == V4L2_MBUS_PARALLEL) {
2173                 switch (endpoint->bus.parallel.bus_width) {
2174                 case 24:
2175                         iface = SINGLE_24B_INTERFACE;
2176                 break;
2177                 case 16:
2178                         iface = SINGLE_16B_INTERFACE;
2179                 break;
2180                 case 8:
2181                 default:
2182                         iface = DUAL_8B_INTERFACE;
2183                 }
2185                 if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
2186                         sync_type = DISCRETE_SYNC_SINGLE_RGB_24B;
2187                 else
2188                         sync_type = DISCRETE_SYNC_SINGLE_YUV422;
2190                 if (flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH |
2191                         V4L2_MBUS_HSYNC_ACTIVE_LOW))
2192                         vip_set_vsync_polarity(port,
2193                                 flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH ? 1 : 0);
2195                 if (flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH |
2196                         V4L2_MBUS_VSYNC_ACTIVE_LOW))
2197                         vip_set_hsync_polarity(port,
2198                                 flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH ? 1 : 0);
2200                 vip_xtra_set_repack_sel(port, 0);
2201                 vip_set_actvid_hsync_n(port, 0);
2202                 vip_set_actvid_polarity(port, 1);
2203                 vip_set_discrete_basic_mode(port);
2205         } else {
2206                 vip_err(dev, "Device doesn't support CSI2");
2207                 return -EINVAL;
2208         }
2210         pclk_type = flags & V4L2_MBUS_PCLK_SAMPLE_RISING ? 0 : 1;
2211         vip_set_pclk_polarity(port, pclk_type);
2212         vip_set_data_interface(port, iface);
2213         vip_sync_type(port, sync_type);
2214         return 0;
2217 static void vip_release_stream(struct vip_stream *stream)
2219         struct vip_dev *dev = stream->port->dev;
2221         vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
2222         vpdma_free_desc_buf(&stream->desc_list.buf);
2223         vpdma_free_desc_list(&stream->desc_list);
2226 static void stop_dma(struct vip_stream *stream)
2228         struct vip_dev *dev = stream->port->dev;
2229         int ch, size = 0;
2231         /* Create a list of channels to be cleared */
2232         for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++) {
2233                 if (stream->vpdma_channels[ch] == 1) {
2234                         stream->vpdma_channels[size++] = ch;
2235                         vip_dbg(2, dev, "Clear channel no: %d\n", ch);
2236                 }
2237         }
2239         /* Clear all the used channels for the list */
2240         vpdma_list_cleanup(dev->shared->vpdma, stream->list_num,
2241                 stream->vpdma_channels, size);
2243         for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++)
2244                 stream->vpdma_channels[ch] = 0;
2247 static int vip_open(struct file *file)
2249         struct vip_stream *stream = video_drvdata(file);
2250         struct vip_port *port = stream->port;
2251         struct vip_dev *dev = port->dev;
2252         struct v4l2_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
2253         int ret = 0;
2255         vip_dbg(2, dev, "vip_open\n");
2257         file->private_data = fh;
2258         if (fh == NULL)
2259                 return -ENOMEM;
2261         mutex_lock(&dev->mutex);
2263         v4l2_fh_init(fh, video_devdata(file));
2264         v4l2_fh_add(fh);
2266         /*
2267          * If this is the first open file.
2268          * Then initialize hw module.
2269          */
2270         if (v4l2_fh_is_singular_file(file)) {
2271                 if (vip_init_stream(stream)) {
2272                         ret = -ENODEV;
2273                         goto free_fh;
2274                 }
2275                 vip_dbg(1, dev, "Created stream instance %p\n", stream);
2276         }
2278         mutex_unlock(&dev->mutex);
2279         return 0;
2281 free_fh:
2282         mutex_unlock(&dev->mutex);
2283         if (fh) {
2284                 v4l2_fh_del(fh);
2285                 v4l2_fh_exit(fh);
2286                 kfree(fh);
2287         }
2288         return ret;
2291 static int vip_release(struct file *file)
2293         struct vip_stream *stream = video_drvdata(file);
2294         struct vip_port *port = stream->port;
2295         struct vip_dev *dev = port->dev;
2296         struct vb2_queue *q = &stream->vb_vidq;
2298         vip_dbg(2, dev, "vip_release\n");
2300         /*
2301          * If this is the last open file.
2302          * Then de-initialize hw module.
2303          */
2304         if (v4l2_fh_is_singular_file(file)) {
2305                 mutex_lock(&dev->mutex);
2307                 vip_stop_streaming(q);
2308                 vip_release_stream(stream);
2310                 if (--port->num_streams == 0)
2311                         vip_release_dev(port->dev);
2313                 mutex_unlock(&dev->mutex);
2314                 vip_dbg(1, dev, "Releasing stream instance %p\n", stream);
2315         }
2317         return vb2_fop_release(file);
2320 static const struct v4l2_file_operations vip_fops = {
2321         .owner          = THIS_MODULE,
2322         .open           = vip_open,
2323         .release        = vip_release,
2324         .read           = vb2_fop_read,
2325         .poll           = vb2_fop_poll,
2326         .unlocked_ioctl = video_ioctl2,
2327         .mmap           = vb2_fop_mmap,
2328 };
2330 static struct video_device vip_videodev = {
2331         .name           = VIP_MODULE_NAME,
2332         .fops           = &vip_fops,
2333         .ioctl_ops      = &vip_ioctl_ops,
2334         .minor          = -1,
2335         .release        = video_device_release,
2336         .tvnorms        = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
2337 };
2339 static int alloc_stream(struct vip_port *port, int stream_id, int vfl_type)
2341         struct vip_stream *stream;
2342         struct vip_dev *dev = port->dev;
2343         struct vb2_queue *q;
2344         struct video_device *vfd;
2345         struct vip_buffer *buf;
2346         int ret, i;
2348         stream = kzalloc(sizeof(*stream), GFP_KERNEL);
2349         if (!stream)
2350                 return -ENOMEM;
2352         stream->port = port;
2353         stream->stream_id = stream_id;
2354         stream->vfl_type = vfl_type;
2356         stream->list_num = vpdma_hwlist_alloc(dev->shared->vpdma, stream);
2357         if (stream->list_num < 0) {
2358                 vip_err(dev, "Could not get VPDMA hwlist");
2359                 ret = -ENODEV;
2360                 goto do_free_stream;
2361         }
2363         INIT_LIST_HEAD(&stream->post_bufs);
2365         if (vfl_type == VFL_TYPE_GRABBER)
2366                 port->cap_streams[stream_id] = stream;
2367         else
2368                 port->vbi_streams[stream_id] = stream;
2370         /*
2371          * Initialize queue
2372          */
2373         q = &stream->vb_vidq;
2374         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2375         q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2376         q->drv_priv = stream;
2377         q->buf_struct_size = sizeof(struct vip_buffer);
2378         q->ops = &vip_video_qops;
2379         q->mem_ops = &vb2_dma_contig_memops;
2380         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2382         /* Feature not back-ported yet. Enable when available */
2383         /* q->min_buffers_needed = 3; */
2385         ret = vb2_queue_init(q);
2386         if (ret)
2387                 goto do_free_stream;
2389         INIT_LIST_HEAD(&stream->vidq);
2391         /* Allocate/populate Drop queue entries */
2392         INIT_LIST_HEAD(&stream->dropq);
2393         for (i = 0; i < VIP_DROPQ_SIZE; i++) {
2395                 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
2396                 if (!buf) {
2397                         vip_err(dev, "No memory!!");
2398                         ret = -ENOMEM;
2399                         goto do_free_stream;
2400                 }
2401                 buf->drop = true;
2402                 list_add(&buf->list, &stream->dropq);
2403         }
2405         vfd = &stream->vdev;
2406         *vfd = vip_videodev;
2407         vfd->v4l2_dev = &dev->v4l2_dev;
2408         vfd->queue = q;
2409         set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
2411         vfd->lock = &dev->mutex;
2412         video_set_drvdata(vfd, stream);
2414         ret = video_register_device(vfd, vfl_type, -1);
2415         if (ret) {
2416                 vip_err(dev, "Failed to register video device\n");
2417                 goto do_free_stream;
2418         }
2420         snprintf(vfd->name, sizeof(vfd->name), "%s", vip_videodev.name);
2421         stream->vfd = vfd;
2423         vip_info(dev, VIP_MODULE_NAME
2424                  " Device registered as /dev/video%d\n", vfd->num);
2425         return 0;
2427 do_free_stream:
2428         kfree(stream);
2429         return ret;
2432 static void free_stream(struct vip_stream *stream)
2434         struct vip_dev *dev = stream->port->dev;
2435         struct vip_buffer *buf;
2436         struct list_head *pos, *q;
2438         if (!stream)
2439                 return;
2441         /* Free up the Drop queue */
2442         list_for_each_safe(pos, q, &stream->dropq) {
2443                 buf = list_entry(stream->dropq.next,
2444                                  struct vip_buffer, list);
2445                 vip_dbg(1, dev, "dropq buffer\n");
2446                 list_del(pos);
2447                 kfree(buf);
2448         }
2450         video_unregister_device(stream->vfd);
2451         video_device_release(stream->vfd);
2452         vpdma_hwlist_release(dev->shared->vpdma, stream->list_num);
2453         kfree(stream);
2456 static int get_subdev_active_format(struct vip_port *port,
2457                                     struct v4l2_subdev *subdev)
2459         struct vip_dev *dev = port->dev;
2460         struct vip_fmt *fmt;
2461         enum v4l2_mbus_pixelcode code;
2462         int ret = 0;
2463         unsigned int k, i, j;
2465         /* Enumerate sub device formats and enable all matching local formats */
2466         dev->num_active_fmt = 0;
2467         for (k = 0, i = 0;
2468              (ret != -EINVAL);
2469              k++) {
2470                 ret = v4l2_subdev_call(subdev, video, enum_mbus_fmt, k, &code);
2471                 if (ret == 0) {
2472                         vip_dbg(2, dev,
2473                                 "subdev %s: code: %04x idx: %d\n",
2474                                 subdev->name, code, k);
2476                         for (j = 0; j < ARRAY_SIZE(vip_formats); j++) {
2477                                 fmt = &vip_formats[j];
2478                                 if (code == fmt->code) {
2479                                         dev->active_fmt[i] = fmt;
2480                                         dev->num_active_fmt = i++;
2481                                         vip_dbg(2, dev,
2482                                                 "matched fourcc: %s: code: %04x idx: %d\n",
2483                                                 fourcc_to_str(fmt->fourcc),
2484                                                 fmt->code,
2485                                                 dev->num_active_fmt);
2486                                 }
2487                         }
2488                 }
2489         }
2491         if (i == 0) {
2492                 vip_err(dev, "No suitable format reported by subdev %s\n",
2493                         subdev->name);
2494                 return -EINVAL;
2495         }
2496         return 0;
2499 static int alloc_port(struct vip_dev *dev, int id)
2501         struct vip_port *port;
2502         int ret;
2504         port = kzalloc(sizeof(*port), GFP_KERNEL);
2505         if (!port)
2506                 return -ENOMEM;
2508         dev->ports[id] = port;
2509         port->dev = dev;
2510         port->port_id = id;
2511         port->num_streams = 0;
2512         port->flags |= FLAG_MULT_PORT;
2514         ret = alloc_stream(port, 0, VFL_TYPE_GRABBER);
2516         if (dev->endpoint->bus_type == V4L2_MBUS_BT656) {
2517                 /* Allocate streams for 4 channels */
2518                 ret = alloc_stream(port, 2, VFL_TYPE_GRABBER);
2519                 ret = alloc_stream(port, 4, VFL_TYPE_GRABBER);
2520                 ret = alloc_stream(port, 6, VFL_TYPE_GRABBER);
2521         }
2523         return get_subdev_active_format(port, dev->sensor);
2526 static void free_port(struct vip_port *port)
2528         if (!port)
2529                 return;
2531         free_stream(port->cap_streams[0]);
2533         kfree(port);
2536 static int get_field(u32 value, u32 mask, int shift)
2538         return (value & (mask << shift)) >> shift;
2541 static int vip_of_probe(struct platform_device *pdev, struct vip_dev *dev);
2542 static void vip_vpdma_fw_cb(struct platform_device *pdev)
2544         struct vip_shared *shared = platform_get_drvdata(pdev);
2545         struct vip_dev *dev;
2546         int slice, ret;
2548         dev_info(&pdev->dev, "VPDMA firmware loaded\n");
2550         for (slice = 0; slice < atomic_read(&shared->devs_allocated); slice++) {
2551                 dev = shared->devs[slice];
2553                 if (pdev->dev.of_node) {
2554                         ret = vip_of_probe(pdev, dev);
2555                         if (ret)
2556                                 goto free_port;
2557                 }
2558         }
2560         return;
2562 free_port:
2563         free_port(dev->ports[0]);
2566 static void remove_shared(struct vip_shared *shared)
2568         if (atomic_dec_return(&shared->devs_allocated) != 0)
2569                 return;
2571         iounmap(shared->base);
2572         release_mem_region(shared->res->start, resource_size(shared->res));
2573         kfree(shared);
2576 static int vip_runtime_get(struct platform_device *pdev)
2578         int r;
2580         r = pm_runtime_get_sync(&pdev->dev);
2581         WARN_ON(r < 0);
2582         return r < 0 ? r : 0;
2586 static int vip_async_bound(struct v4l2_async_notifier *notifier,
2587                         struct v4l2_subdev *subdev,
2588                         struct v4l2_async_subdev *asd)
2590         struct vip_dev *dev = notifier_to_vip_dev(notifier);
2591         unsigned int idx = asd - &dev->config->asd[0];
2592         int ret;
2594         vip_dbg(1, dev, "vip_async_bound\n");
2595         if (idx > dev->config->asd_sizes)
2596                 return -EINVAL;
2598         if (dev->sensor) {
2599                 if (asd < dev->sensor->asd) {
2600                         /* Notified of a subdev earlier in the array */
2601                         dev->sensor = subdev;
2602                         dev->endpoint = &dev->config->endpoints[idx];
2603                         vip_info(dev, "Switching to subdev %s (High priority)",
2604                                  subdev->name);
2606                 } else
2607                         vip_info(dev, "Rejecting subdev %s (Low priority)",
2608                                  subdev->name);
2609                 return 0;
2610         }
2612         dev->sensor = subdev;
2613         dev->endpoint = &dev->config->endpoints[idx];
2614         ret = alloc_port(dev, 0);
2615         if (!ret)
2616                 vip_info(dev, "Using sensor %s for capture\n", subdev->name);
2618         return ret;
2621 static int vip_async_complete(struct v4l2_async_notifier *notifier)
2623         struct vip_dev *dev = notifier_to_vip_dev(notifier);
2624         vip_dbg(1, dev, "vip_async_complete\n");
2625         return 0;
2628 static struct device_node *
2629 of_get_next_port(const struct device_node *parent,
2630                  struct device_node *prev)
2632         struct device_node *port = NULL;
2634         if (!parent)
2635                 return NULL;
2637         if (!prev) {
2638                 struct device_node *ports;
2639                 /*
2640                  * It's the first call, we have to find a port subnode
2641                  * within this node or within an optional 'ports' node.
2642                  */
2643                 ports = of_get_child_by_name(parent, "ports");
2644                 if (ports)
2645                         parent = ports;
2647                 port = of_get_child_by_name(parent, "port");
2649                 /* release the 'ports' node */
2650                 of_node_put(ports);
2651         } else {
2652                 struct device_node *ports;
2654                 ports = of_get_parent(prev);
2655                 if (!ports)
2656                         return NULL;
2658                 do {
2659                         port = of_get_next_child(ports, prev);
2660                         if (!port) {
2661                                 of_node_put(ports);
2662                                 return NULL;
2663                         }
2664                         prev = port;
2665                 } while (of_node_cmp(port->name, "port") != 0);
2666         }
2668         return port;
2671 static struct device_node *
2672 of_get_next_endpoint(const struct device_node *parent,
2673                         struct device_node *prev)
2675         struct device_node *ep = NULL;
2677         if (!parent)
2678                 return NULL;
2680         do {
2681                 ep = of_get_next_child(parent, prev);
2682                 if (!ep)
2683                         return NULL;
2684                 prev = ep;
2685         } while (of_node_cmp(ep->name, "endpoint") != 0);
2687         return ep;
2690 static int vip_of_probe(struct platform_device *pdev, struct vip_dev *dev)
2692         struct device_node *ep_node = NULL, *port, *remote_ep,
2693                         *sensor_node, *parent;
2694         struct device_node *syscon_np;
2695         struct v4l2_of_endpoint *endpoint;
2696         struct v4l2_async_subdev *asd;
2697         u32 regval = 0;
2698         int ret, slice, i = 0, found_port = 0;
2700         parent = pdev->dev.of_node;
2702         syscon_np = of_parse_phandle(pdev->dev.of_node, "syscon", 0);
2703         dev->syscon = syscon_node_to_regmap(syscon_np);
2704         of_node_put(syscon_np);
2706         dev->config = kzalloc(sizeof(struct vip_config), GFP_KERNEL);
2707         if (!dev->config)
2708                 return -ENOMEM;
2710         dev->config->card_name = "VIP Driver";
2712         port = NULL;
2713         vip_dbg(3, dev, "Scanning Port node for slice id: %d\n", dev->slice_id);
2714         for (slice = 0; slice < VIP_NUM_SLICES; slice++) {
2715                 port = of_get_next_port(parent, port);
2716                 if (!port) {
2717                         vip_dbg(1, dev, "No port node found for slice_id:%d\n",
2718                                 slice);
2719                         ret = -EINVAL;
2720                         goto free_config;
2721                 }
2723                 /* Match the slice number with <REG> */
2724                 of_property_read_u32(port, "reg", &regval);
2725                 vip_dbg(3, dev, "slice:%d dev->slice_id:%d <reg>:%d\n",
2726                         slice, dev->slice_id, regval);
2727                 if ((regval == dev->slice_id) && (slice == dev->slice_id)) {
2728                         found_port = 1;
2729                         break;
2730                 }
2731         }
2733         if (!found_port) {
2734                 if (!port)
2735                         of_node_put(port);
2736                 vip_dbg(1, dev, "No port node matches slice_id:%d\n",
2737                         dev->slice_id);
2738                 ret = -EINVAL;
2739                 goto free_config;
2740         }
2742         vip_dbg(3, dev, "Scanning sub-device(s) for slice id: %d\n",
2743                 dev->slice_id);
2744         while (i < VIP_MAX_SUBDEV) {
2746                 asd = &dev->config->asd[i];
2747                 endpoint = &dev->config->endpoints[i];
2749                 remote_ep = NULL;
2750                 sensor_node = NULL;
2752                 ep_node = of_get_next_endpoint(port, ep_node);
2753                 if (!ep_node) {
2754                         vip_dbg(3, dev, "can't get next endpoint: loop: %d\n",
2755                                 i);
2756                         break;
2757                 }
2759                 sensor_node = of_graph_get_remote_port_parent(ep_node);
2760                 if (!sensor_node) {
2761                         vip_dbg(3, dev, "can't get remote parent: loop: %d\n",
2762                                 i);
2763                         goto of_node_cleanup;
2764                 }
2765                 asd->match_type = V4L2_ASYNC_MATCH_OF;
2766                 asd->match.of.node = sensor_node;
2768                 remote_ep = of_parse_phandle(ep_node, "remote-endpoint", 0);
2769                 if (!remote_ep) {
2770                         vip_dbg(3, dev, "can't get remote-endpoint: loop: %d\n",
2771                                 i);
2772                         goto of_node_cleanup;
2773                 }
2774                 v4l2_of_parse_endpoint(remote_ep, endpoint);
2776                 dev->config->asd_list[i++] = asd;
2778 of_node_cleanup:
2779                 if (!remote_ep)
2780                         of_node_put(remote_ep);
2781                 if (!sensor_node)
2782                         of_node_put(sensor_node);
2783         }
2785         if (!ep_node)
2786                 of_node_put(ep_node);
2787         if (!port)
2788                 of_node_put(port);
2790         vip_dbg(1, dev, "Found %d sub-device(s) for slice id: %d\n",
2791                 i, dev->slice_id);
2792         if (i > 0) {
2793                 dev->config->asd_sizes = i;
2794                 dev->notifier.subdevs = dev->config->asd_list;
2795                 dev->notifier.num_subdevs = dev->config->asd_sizes;
2796                 dev->notifier.bound = vip_async_bound;
2797                 dev->notifier.complete = vip_async_complete;
2799                 vip_dbg(1, dev, "registering the sync notifier for sensors %d",
2800                         i);
2801                 ret = v4l2_async_notifier_register(&dev->v4l2_dev,
2802                                                    &dev->notifier);
2803                 if (ret) {
2804                         vip_dbg(1, dev, "Error registering async notifier\n");
2805                         ret = -EINVAL;
2806                         goto free_config;
2807                 }
2808         }
2810         return 0;
2811 free_config:
2812         kfree(dev->config);
2813         return ret;
2816 static const struct of_device_id vip_of_match[];
2817 static int vip_probe(struct platform_device *pdev)
2819         struct vip_dev *dev;
2820         struct vip_shared *shared;
2821         const struct of_device_id *of_dev_id;
2822         struct pinctrl *pinctrl;
2823         int ret, slice = VIP_SLICE1;
2824         u32 tmp, pid;
2825         struct v4l2_ctrl_handler *hdl;
2827         pm_runtime_enable(&pdev->dev);
2829         ret = vip_runtime_get(pdev);
2830         if (ret)
2831                 goto err_runtime_get;
2833         of_dev_id = of_match_device(vip_of_match, &pdev->dev);
2834         if (!of_dev_id) {
2835                 dev_err(&pdev->dev, "%s: Unable to match device\n", __func__);
2836                 return -ENODEV;
2837         }
2839         shared = kzalloc(sizeof(*shared), GFP_KERNEL);
2840         if (!shared)
2841                 return -ENOMEM;
2843         shared->res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vip");
2844         if (shared->res == NULL) {
2845                 dev_err(&pdev->dev, "Missing platform resources data\n");
2846                 ret = -ENODEV;
2847                 goto free_shared;
2848         }
2850         pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2851         if (IS_ERR(pinctrl)) {
2852                 dev_warn(&pdev->dev, "No explicit pinctrl resources data.\n");
2853         }
2855         if (devm_request_mem_region(&pdev->dev, shared->res->start,
2856             resource_size(shared->res), VIP_MODULE_NAME) == NULL) {
2857                 ret = -ENOMEM;
2858                 goto free_shared;
2859         }
2861         shared->base = devm_ioremap(&pdev->dev, shared->res->start,
2862                                     resource_size(shared->res));
2863         if (!shared->base) {
2864                 dev_err(&pdev->dev, "failed to ioremap\n");
2865                 ret = -ENOMEM;
2866                 goto rel_mem_region;
2867         }
2869         /* Make sure H/W module has the right functionality */
2870         pid = read_sreg(shared, VIP_PID);
2871         tmp = get_field(pid, VIP_PID_FUNC_MASK, VIP_PID_FUNC_SHIFT);
2873         if (tmp != VIP_PID_FUNC) {
2874                 dev_info(&pdev->dev, "vip: unexpected PID function: 0x%x\n",
2875                        tmp);
2876                 ret = -ENODEV;
2877                 goto do_iounmap;
2878         }
2880         /* enable clocks, so the firmware will load properly */
2881         vip_shared_set_clock_enable(shared, 1);
2882         vip_top_vpdma_reset(shared);
2884         list_add_tail(&shared->list, &vip_shared_list);
2885         platform_set_drvdata(pdev, shared);
2886         atomic_set(&shared->devs_allocated, 0);
2888         vip_set_idle_mode(shared, VIP_SMART_IDLE_MODE);
2889         vip_set_standby_mode(shared, VIP_SMART_STANDBY_MODE);
2891         for (slice = VIP_SLICE1; slice < VIP_NUM_SLICES; slice++) {
2892                 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2893                 if (!dev)
2894                         return -ENOMEM;
2896                 dev->instance_id = (int)of_dev_id->data;
2897                 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
2898                         "%s%d-s%d", VIP_MODULE_NAME, dev->instance_id, slice);
2900                 dev->irq = platform_get_irq(pdev, slice);
2901                 if (!dev->irq) {
2902                         dev_err(&pdev->dev, "Could not get IRQ");
2903                         goto err_runtime_get;
2904                 }
2906                 if (devm_request_irq(&pdev->dev, dev->irq, vip_irq,
2907                                      0, dev->v4l2_dev.name, dev) < 0) {
2908                         ret = -ENOMEM;
2909                         goto dev_unreg;
2910                 }
2912                 spin_lock_init(&dev->slock);
2913                 spin_lock_init(&dev->lock);
2915                 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2916                 if (ret)
2917                         goto err_runtime_get;
2919                 mutex_init(&dev->mutex);
2921                 hdl = &dev->ctrl_handler;
2922                 v4l2_ctrl_handler_init(hdl, 11);
2923                 dev->v4l2_dev.ctrl_handler = hdl;
2925                 dev->slice_id = slice;
2926                 dev->pdev = pdev;
2927                 dev->res = shared->res;
2928                 dev->base = shared->base;
2930                 dev->shared = shared;
2931                 shared->devs[slice] = dev;
2933                 atomic_inc(&shared->devs_allocated);
2935                 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2936                 if (IS_ERR(dev->alloc_ctx)) {
2937                         vip_err(dev, "Failed to alloc vb2 context\n");
2938                         ret = PTR_ERR(dev->alloc_ctx);
2939                         goto dev_unreg;
2940                 }
2942                 vip_top_reset(dev);
2943                 vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT);
2944         }
2946         shared->vpdma = &shared->vpdma_data;
2947         ret = vpdma_create(pdev, shared->vpdma, vip_vpdma_fw_cb);
2948         if (ret) {
2949                 dev_err(&pdev->dev, "Creating VPDMA failed");
2950                 goto dev_unreg;
2951         }
2953         return 0;
2955 dev_unreg:
2956         v4l2_device_unregister(&dev->v4l2_dev);
2957 do_iounmap:
2958         iounmap(shared->base);
2959 rel_mem_region:
2960         release_mem_region(shared->res->start, resource_size(shared->res));
2961 free_shared:
2962         kfree(shared);
2963 err_runtime_get:
2964         if (slice == VIP_SLICE1) {
2965                 pm_runtime_disable(&pdev->dev);
2966                 return ret;
2967         } else
2968                 return 0;
2971 static int vip_remove(struct platform_device *pdev)
2973         struct vip_shared *shared = platform_get_drvdata(pdev);
2974         struct vip_dev *dev;
2975         int slice;
2977         for (slice = 0; slice < atomic_read(&shared->devs_allocated); slice++) {
2978                 dev = shared->devs[slice];
2979                 if (!dev)
2980                         continue;
2981                 vip_info(dev, "Removing " VIP_MODULE_NAME);
2982                 free_port(dev->ports[0]);
2983                 v4l2_async_notifier_unregister(&dev->notifier);
2984                 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2985                 free_irq(dev->irq, dev);
2986                 kfree(dev);
2987         }
2988         remove_shared(shared);
2990         return 0;
2993 #if defined(CONFIG_OF)
2994 static const struct of_device_id vip_of_match[] = {
2995         {
2996                 .compatible = "ti,vip1", .data = (void *) VIP_INSTANCE1,
2997         },
2999         {
3000                 .compatible = "ti,vip2", .data = (void *) VIP_INSTANCE2,
3001         },
3003         {
3004                 .compatible = "ti,vip3", .data = (void *) VIP_INSTANCE3,
3005         },
3006         {},
3007 };
3008 MODULE_DEVICE_TABLE(of, vip_of_match);
3009 #else
3010 #define vip_of_match NULL
3011 #endif
3013 static struct platform_driver vip_pdrv = {
3014         .probe          = vip_probe,
3015         .remove         = vip_remove,
3016         .driver         = {
3017                 .name   = VIP_MODULE_NAME,
3018                 .owner  = THIS_MODULE,
3019                 .of_match_table = vip_of_match,
3020         },
3021 };
3023 module_platform_driver(vip_pdrv);
3025 MODULE_DESCRIPTION("TI VIP driver");
3026 MODULE_AUTHOR("Texas Instruments");
3027 MODULE_LICENSE("GPL v2");