media: ti-vpe: vip: fix try_format for interlaced frames usecase
[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.field == V4L2_FIELD_ALTERNATE) ?
1534                             f->fmt.pix.height * 2 : f->fmt.pix.height) ==
1535                             fsize.discrete.height)) {
1536                                 found = true;
1537                                 break;
1538                         }
1539                 } else if ((fsize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) ||
1540                            (fsize.type == V4L2_FRMSIZE_TYPE_STEPWISE)) {
1541                         if ((f->fmt.pix.width >= fsize.stepwise.min_width) &&
1542                             (f->fmt.pix.width <= fsize.stepwise.max_width) &&
1543                             (f->fmt.pix.height >= fsize.stepwise.min_height) &&
1544                             (f->fmt.pix.height <= fsize.stepwise.max_height)) {
1545                                 found = true;
1546                                 break;
1547                         }
1548                 }
1549         }
1551         if (!found) {
1552                 /* use existing values as default */
1553                 f->fmt.pix.width = port->mbus_framefmt.width;
1554                 f->fmt.pix.height =  port->mbus_framefmt.height;
1555         }
1557         /* That we have a fmt calculate imagesize and bytesperline */
1558         return vip_calc_format_size(port, fmt, f);
1561 static int vip_g_fmt_vid_cap(struct file *file, void *priv,
1562                              struct v4l2_format *f)
1564         struct vip_stream *stream = file2stream(file);
1565         struct vip_port *port = stream->port;
1566         struct vip_dev *dev = stream->port->dev;
1567         struct vip_fmt *fmt = port->fmt;
1569         /* Use last known values or defaults */
1570         f->fmt.pix.width        = stream->width;
1571         f->fmt.pix.height       = stream->height;
1572         f->fmt.pix.pixelformat  = port->fmt->fourcc;
1573         f->fmt.pix.field        = stream->sup_field;
1574         f->fmt.pix.colorspace   = port->fmt->colorspace;
1575         f->fmt.pix.bytesperline = stream->bytesperline;
1576         f->fmt.pix.sizeimage    = stream->sizeimage;
1578         vip_dbg(3, dev,
1579                 "g_fmt fourcc:%s code: %04x size: %dx%d bpl:%d img_size:%d\n",
1580                 fourcc_to_str(f->fmt.pix.pixelformat),
1581                 fmt->code,
1582                 f->fmt.pix.width, f->fmt.pix.height,
1583                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1584         vip_dbg(3, dev, "g_fmt vpdma data type: 0x%02X\n",
1585                 port->fmt->vpdma_fmt[0]->data_type);
1587         return 0;
1590 static int vip_s_fmt_vid_cap(struct file *file, void *priv,
1591                              struct v4l2_format *f)
1593         struct vip_stream *stream = file2stream(file);
1594         struct vip_port *port = stream->port;
1595         struct vip_dev *dev = port->dev;
1596         struct v4l2_subdev_format sfmt;
1597         struct v4l2_mbus_framefmt *mf;
1598         int ret;
1600         vip_dbg(3, dev, "s_fmt input fourcc:%s size: %dx%d\n",
1601                 fourcc_to_str(f->fmt.pix.pixelformat),
1602                 f->fmt.pix.width, f->fmt.pix.height);
1604         ret = vip_try_fmt_vid_cap(file, priv, f);
1605         if (ret)
1606                 return ret;
1608         vip_dbg(3, dev, "s_fmt try_fmt fourcc:%s size: %dx%d\n",
1609                 fourcc_to_str(f->fmt.pix.pixelformat),
1610                 f->fmt.pix.width, f->fmt.pix.height);
1612         if (vb2_is_busy(&stream->vb_vidq)) {
1613                 vip_err(dev, "%s queue busy\n", __func__);
1614                 return -EBUSY;
1615         }
1617         port->fmt = find_port_format_by_pix(port,
1618                                             f->fmt.pix.pixelformat);
1619         stream->width           = f->fmt.pix.width;
1620         stream->height          = f->fmt.pix.height;
1621         stream->bytesperline    = f->fmt.pix.bytesperline;
1622         stream->sizeimage       = f->fmt.pix.sizeimage;
1623         stream->sup_field       = f->fmt.pix.field;
1625         port->c_rect.left       = 0;
1626         port->c_rect.top        = 0;
1627         port->c_rect.width      = stream->width;
1628         port->c_rect.height     = stream->height;
1630         if (stream->sup_field == V4L2_FIELD_ALTERNATE)
1631                 port->flags |= FLAG_INTERLACED;
1632         else
1633                 port->flags &= ~FLAG_INTERLACED;
1635         vip_dbg(3, dev, "s_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1636                 fourcc_to_str(f->fmt.pix.pixelformat),
1637                 f->fmt.pix.width, f->fmt.pix.height,
1638                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1640         mf = &sfmt.format;
1641         v4l2_fill_mbus_format(mf, &f->fmt.pix, port->fmt->code);
1643         /* Save it */
1644         port->mbus_framefmt = *mf;
1646         vip_dbg(3, dev, "s_fmt pix_to_mbus mbus_code: %04X size: %dx%d\n",
1647                 mf->code,
1648                 mf->width, mf->height);
1650         ret = v4l2_subdev_call(dev->sensor, video, try_mbus_fmt, mf);
1651         if (ret) {
1652                 vip_dbg(1, dev, "try_mbus_fmt failed in subdev\n");
1653                 return ret;
1654         }
1655         vip_dbg(3, dev, "s_fmt subdev try_fmt mbus_code: %04X size: %dx%d\n",
1656                 mf->code,
1657                 mf->width, mf->height);
1658         ret = v4l2_subdev_call(dev->sensor, video, s_mbus_fmt, mf);
1659         if (ret) {
1660                 vip_dbg(1, dev, "s_mbus_fmt failed in subdev\n");
1661                 return ret;
1662         }
1663         vip_dbg(3, dev, "s_fmt subdev s_fmt mbus_code: %04X size: %dx%d\n",
1664                 mf->code,
1665                 mf->width, mf->height);
1666         vip_dbg(3, dev, "s_fmt vpdma data type: 0x%02X\n",
1667                 port->fmt->vpdma_fmt[0]->data_type);
1669         return 0;
1672 /*
1673  * Set the registers that are modified when the video format changes.
1674  */
1675 static void set_fmt_params(struct vip_stream *stream)
1677         struct vip_dev *dev = stream->port->dev;
1678         int data_path_reg;
1680         stream->sequence = 0;
1681         stream->field = V4L2_FIELD_TOP;
1683         if (stream->port->fmt->colorspace == V4L2_COLORSPACE_SRGB) {
1684                 vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT);
1685                 /* Set alpha component in background color */
1686                 vpdma_set_bg_color(dev->shared->vpdma,
1687                                    (struct vpdma_data_format *)
1688                                    stream->port->fmt->vpdma_fmt[0],
1689                                    0xff);
1690         }
1692         data_path_reg = VIP_VIP1_DATA_PATH_SELECT + 4 * dev->slice_id;
1693         if (stream->port->fmt->coplanar) {
1694                 stream->port->flags &= ~FLAG_MULT_PORT;
1695                 write_vreg(dev, data_path_reg, 0x600);
1696         } else {
1697                 stream->port->flags |= FLAG_MULT_PORT;
1698                 write_vreg(dev, data_path_reg, 0x8000);
1699         }
1702 static int vip_g_selection(struct file *file, void *fh,
1703                            struct v4l2_selection *s)
1705         struct vip_stream *stream = file2stream(file);
1707         switch (s->target) {
1708         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1709         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1710         case V4L2_SEL_TGT_CROP_BOUNDS:
1711         case V4L2_SEL_TGT_CROP_DEFAULT:
1712                 s->r.left = 0;
1713                 s->r.top = 0;
1714                 s->r.width = stream->width;
1715                 s->r.height = stream->height;
1716                 return 0;
1718         case V4L2_SEL_TGT_COMPOSE:
1719         case V4L2_SEL_TGT_CROP:
1720                 s->r = stream->port->c_rect;
1721                 return 0;
1722         }
1724         return -EINVAL;
1727 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1729         if (a->left < b->left || a->top < b->top)
1730                 return 0;
1731         if (a->left + a->width > b->left + b->width)
1732                 return 0;
1733         if (a->top + a->height > b->top + b->height)
1734                 return 0;
1736         return 1;
1739 static int vip_s_selection(struct file *file, void *fh,
1740                            struct v4l2_selection *s)
1742         struct vip_stream *stream = file2stream(file);
1743         struct vip_port *port = stream->port;
1744         struct v4l2_rect r = s->r;
1746         v4l_bound_align_image(&r.width, 0, stream->width, 0,
1747                               &r.height, 0, stream->height, 0, 0);
1749         r.left = clamp_t(unsigned int, r.left, 0, stream->width - r.width);
1750         r.top  = clamp_t(unsigned int, r.top, 0, stream->height - r.height);
1752         if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
1753                 return -ERANGE;
1755         if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
1756                 return -ERANGE;
1758         s->r = stream->port->c_rect = r;
1760         set_fmt_params(stream);
1762         vip_dbg(1, port->dev, "cropped (%d,%d)/%dx%d of %dx%d\n",
1763                 r.left, r.top, r.width, r.height,
1764                 stream->width, stream->height);
1766         return 0;
1769 static long vip_ioctl_default(struct file *file, void *fh, bool valid_prio,
1770                               unsigned int cmd, void *arg)
1772         struct vip_stream *stream = file2stream(file);
1773         struct vip_port *port = stream->port;
1775         if (!valid_prio) {
1776                 vip_err(port->dev, "%s device busy\n", __func__);
1777                 return -EBUSY;
1778         }
1780         switch (cmd) {
1781         default:
1782                 return -ENOTTY;
1783         }
1786 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
1787         .vidioc_querycap        = vip_querycap,
1788         .vidioc_enum_input      = vip_enuminput,
1789         .vidioc_g_input         = vip_g_input,
1790         .vidioc_s_input         = vip_s_input,
1792         .vidioc_querystd        = vip_querystd,
1793         .vidioc_g_std           = vip_g_std,
1794         .vidioc_s_std           = vip_s_std,
1796         .vidioc_enum_fmt_vid_cap = vip_enum_fmt_vid_cap,
1797         .vidioc_g_fmt_vid_cap   = vip_g_fmt_vid_cap,
1798         .vidioc_try_fmt_vid_cap = vip_try_fmt_vid_cap,
1799         .vidioc_s_fmt_vid_cap   = vip_s_fmt_vid_cap,
1801         .vidioc_enum_frameintervals     = vip_enum_frameintervals,
1802         .vidioc_enum_framesizes         = vip_enum_framesizes,
1803         .vidioc_s_parm                  = vip_s_parm,
1804         .vidioc_g_parm                  = vip_g_parm,
1805         .vidioc_g_selection     = vip_g_selection,
1806         .vidioc_s_selection     = vip_s_selection,
1807         .vidioc_reqbufs         = vb2_ioctl_reqbufs,
1808         .vidioc_create_bufs     = vb2_ioctl_create_bufs,
1809         .vidioc_prepare_buf     = vb2_ioctl_prepare_buf,
1810         .vidioc_querybuf        = vb2_ioctl_querybuf,
1811         .vidioc_qbuf            = vb2_ioctl_qbuf,
1812         .vidioc_dqbuf           = vb2_ioctl_dqbuf,
1814         .vidioc_streamon        = vb2_ioctl_streamon,
1815         .vidioc_streamoff       = vb2_ioctl_streamoff,
1816         .vidioc_log_status      = v4l2_ctrl_log_status,
1817         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1818         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1819         .vidioc_default         = vip_ioctl_default,
1820 };
1822 /*
1823  * Videobuf operations
1824  */
1825 static int vip_queue_setup(struct vb2_queue *vq,
1826                            const struct v4l2_format *fmt,
1827                            unsigned int *nbuffers, unsigned int *nplanes,
1828                            unsigned int sizes[], void *alloc_ctxs[])
1830         struct vip_stream *stream = vb2_get_drv_priv(vq);
1831         struct vip_dev *dev = stream->port->dev;
1833         *nplanes = 1;
1834         sizes[0] = stream->sizeimage;
1835         alloc_ctxs[0] = dev->alloc_ctx;
1836         vip_dbg(1, dev, "get %d buffer(s) of size %d each.\n",
1837                 *nbuffers, sizes[0]);
1839         return 0;
1842 static int vip_buf_prepare(struct vb2_buffer *vb)
1844         struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
1845         struct vip_dev *dev = stream->port->dev;
1847         if (vb2_plane_size(vb, 0) < stream->sizeimage) {
1848                 vip_dbg(1, dev,
1849                         "%s data will not fit into plane (%lu < %lu)\n",
1850                         __func__, vb2_plane_size(vb, 0),
1851                         (long)stream->sizeimage);
1852                 return -EINVAL;
1853         }
1855         vb2_set_plane_payload(vb, 0, stream->sizeimage);
1857         return 0;
1860 static void vip_buf_queue(struct vb2_buffer *vb)
1862         struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
1863         struct vip_dev *dev = stream->port->dev;
1864         struct vip_buffer *buf = container_of(vb, struct vip_buffer, vb);
1865         unsigned long flags;
1867         spin_lock_irqsave(&dev->slock, flags);
1868         list_add_tail(&buf->list, &stream->vidq);
1869         spin_unlock_irqrestore(&dev->slock, flags);
1872 static int vip_start_streaming(struct vb2_queue *vq, unsigned int count)
1874         struct vip_stream *stream = vb2_get_drv_priv(vq);
1875         struct vip_port *port = stream->port;
1876         struct vip_dev *dev = port->dev;
1877         struct vip_buffer *buf;
1878         unsigned long flags;
1879         int ret;
1881         set_fmt_params(stream);
1882         vip_setup_parser(dev->ports[0]);
1884         buf = list_entry(stream->vidq.next,
1885                          struct vip_buffer, list);
1887         vip_dbg(2, dev, "start_streaming: buf 0x%x %d\n",
1888                 (unsigned int)buf, count);
1889         buf->drop = false;
1890         stream->sequence = 0;
1891         stream->field = V4L2_FIELD_TOP;
1893         if (dev->sensor) {
1894                 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1895                 if (ret) {
1896                         vip_dbg(1, dev, "stream on failed in subdev\n");
1897                         return ret;
1898                 }
1899         }
1901         populate_desc_list(stream);
1903         /* The first few VPDMA ListComplete interrupts fire pretty quiclky
1904          * until the internal VPDMA descriptor fifo is full.
1905          * The subsequent ListComplete interrupts will fire at the actual
1906          * capture frame rate. The first few interrupts are therefore used
1907          * only to queue up descriptors, and then they will also be used
1908          * as End of Frame (EOF) event
1909          */
1910         dev->num_skip_irq = VIP_VPDMA_FIFO_SIZE;
1912         spin_lock_irqsave(&dev->slock, flags);
1913         if (vpdma_list_busy(dev->shared->vpdma, stream->list_num)) {
1914                 spin_unlock_irqrestore(&dev->slock, flags);
1915                 vpdma_unmap_desc_buf(dev->shared->vpdma,
1916                                 &stream->desc_list.buf);
1917                 vpdma_reset_desc_list(&stream->desc_list);
1918                 return -EBUSY;
1919         }
1921         list_move_tail(&buf->list, &stream->post_bufs);
1922         spin_unlock_irqrestore(&dev->slock, flags);
1924         vip_dbg(2, dev, "start_streaming: start_dma buf 0x%x\n",
1925                 (unsigned int)buf);
1926         start_dma(stream, buf);
1928         /* We enable the irq after posting the vpdma descriptor
1929          * to prevent sprurious interrupt coming in before the
1930          * vb2 layer is completely ready to handle them
1931          * otherwise the vb2_streaming test would fail early on
1932           */
1933         enable_irqs(dev, dev->slice_id, stream->list_num);
1935         return 0;
1938 /*
1939  * Abort streaming and wait for last buffer
1940  */
1941 static int vip_stop_streaming(struct vb2_queue *vq)
1943         struct vip_stream *stream = vb2_get_drv_priv(vq);
1944         struct vip_port *port = stream->port;
1945         struct vip_dev *dev = port->dev;
1946         struct vip_buffer *buf;
1947         int ret;
1949         if (dev->sensor) {
1950                 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 0);
1951                 if (ret)
1952                         vip_dbg(1, dev, "stream on failed in subdev\n");
1953         }
1955         disable_irqs(dev, dev->slice_id, stream->list_num);
1956         clear_irqs(dev, dev->slice_id, stream->list_num);
1957         stop_dma(stream);
1959         /* release all active buffers */
1960         while (!list_empty(&stream->post_bufs)) {
1961                 buf = list_entry(stream->post_bufs.next,
1962                                         struct vip_buffer, list);
1963                 list_del(&buf->list);
1964                 if (buf->drop == 1)
1965                         list_add_tail(&buf->list, &stream->dropq);
1966                 else
1967                         vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1968         }
1969         while (!list_empty(&stream->vidq)) {
1970                 buf = list_entry(stream->vidq.next, struct vip_buffer, list);
1971                 list_del(&buf->list);
1972                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
1973         }
1975         if (!vb2_is_streaming(vq))
1976                 return 0;
1978         vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
1979         vpdma_reset_desc_list(&stream->desc_list);
1981         return 0;
1984 /*
1985  * Lock access to the device
1986  */
1987 static void vip_lock(struct vb2_queue *vq)
1989         struct vip_stream *stream = vb2_get_drv_priv(vq);
1991         mutex_lock(&stream->port->dev->mutex);
1994 static void vip_unlock(struct vb2_queue *vq)
1996         struct vip_stream *stream = vb2_get_drv_priv(vq);
1997         mutex_unlock(&stream->port->dev->mutex);
2000 static struct vb2_ops vip_video_qops = {
2001         .queue_setup            = vip_queue_setup,
2002         .buf_prepare            = vip_buf_prepare,
2003         .buf_queue              = vip_buf_queue,
2004         .start_streaming        = vip_start_streaming,
2005         .stop_streaming         = vip_stop_streaming,
2006         .wait_prepare           = vip_unlock,
2007         .wait_finish            = vip_lock,
2008 };
2010 /*
2011  * File operations
2012  */
2014 static int vip_init_dev(struct vip_dev *dev)
2016         if (dev->num_ports != 0)
2017                 goto done;
2019         vip_set_clock_enable(dev, 1);
2020 done:
2021         dev->num_ports++;
2023         return 0;
2026 static int vip_init_port(struct vip_port *port)
2028         int ret;
2029         struct vip_dev *dev = port->dev;
2030         struct vip_fmt *fmt;
2031         struct v4l2_mbus_framefmt *mbus_fmt = &port->mbus_framefmt;
2033         if (port->num_streams != 0)
2034                 goto done;
2036         ret = vip_init_dev(port->dev);
2037         if (ret)
2038                 goto done;
2040         /* Get subdevice current frame format */
2041         ret = v4l2_subdev_call(dev->sensor, video, g_mbus_fmt, mbus_fmt);
2042         if (ret)
2043                 vip_dbg(1, dev, "init_port g_mbus_fmt failed in subdev\n");
2045         /* try to find one that matches */
2046         fmt = find_port_format_by_pix(port, mbus_fmt->code);
2047         if (!fmt) {
2048                 vip_dbg(1, dev, "subdev default mbus_fmt %04x is not matched.\n",
2049                         mbus_fmt->code);
2050                 /* if all else fails just pick the first one */
2051                 fmt = dev->active_fmt[0];
2053                 mbus_fmt->code = fmt->code;
2054                 ret = v4l2_subdev_call(dev->sensor, video,
2055                                        s_mbus_fmt, mbus_fmt);
2056                 if (ret)
2057                         vip_dbg(1, dev, "init_port s_mbus_fmt failed in subdev\n");
2058         }
2060         /* Assign current format */
2061         port->fmt = fmt;
2063         vip_dbg(3, dev, "vip_init_port: g_mbus_fmt subdev mbus_code: %04X fourcc:%s size: %dx%d\n",
2064                 fmt->code,
2065                 fourcc_to_str(fmt->fourcc),
2066                 mbus_fmt->width, mbus_fmt->height);
2068         if (mbus_fmt->field == V4L2_FIELD_ALTERNATE)
2069                 port->flags |= FLAG_INTERLACED;
2070         else
2071                 port->flags &= ~FLAG_INTERLACED;
2073         port->c_rect.left       = 0;
2074         port->c_rect.top        = 0;
2075         port->c_rect.width      = mbus_fmt->width;
2076         port->c_rect.height     = mbus_fmt->height;
2078 done:
2079         port->num_streams++;
2080         return 0;
2083 static int vip_init_stream(struct vip_stream *stream)
2085         struct vip_port *port = stream->port;
2086         struct vip_dev *dev = port->dev;
2087         struct vip_fmt *fmt;
2088         struct v4l2_mbus_framefmt *mbus_fmt;
2089         struct v4l2_format f;
2090         int ret;
2092         ret = vip_init_port(port);
2093         if (ret != 0)
2094                 return ret;
2096         fmt = port->fmt;
2097         mbus_fmt = &port->mbus_framefmt;
2099         /* Properly calculate the sizeimage and bytesperline values. */
2100         v4l2_fill_pix_format(&f.fmt.pix, mbus_fmt);
2101         f.fmt.pix.pixelformat = fmt->fourcc;
2102         ret = vip_calc_format_size(port, fmt, &f);
2103         if (ret)
2104                 return ret;
2106         stream->width = f.fmt.pix.width;
2107         stream->height = f.fmt.pix.height;
2108         stream->sup_field = f.fmt.pix.field;
2109         stream->bytesperline = f.fmt.pix.bytesperline;
2110         stream->sizeimage = f.fmt.pix.sizeimage;
2112         vip_dbg(3, dev, "init_stream fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
2113                 fourcc_to_str(f.fmt.pix.pixelformat),
2114                 f.fmt.pix.width, f.fmt.pix.height,
2115                 f.fmt.pix.bytesperline, f.fmt.pix.sizeimage);
2116         vip_dbg(3, dev, "init_stream vpdma data type: 0x%02X\n",
2117                 port->fmt->vpdma_fmt[0]->data_type);
2119         ret = vpdma_create_desc_list(&stream->desc_list, VIP_DESC_LIST_SIZE,
2120                         VPDMA_LIST_TYPE_NORMAL);
2122         if (ret != 0)
2123                 return ret;
2125         stream->write_desc = (struct vpdma_dtd *)stream->desc_list.buf.addr
2126                                 + 15;
2127         return 0;
2130 static void vip_release_dev(struct vip_dev *dev)
2132         /*
2133          * On last close, disable clocks to conserve power
2134          */
2136         if (--dev->num_ports == 0)
2137                 vip_set_clock_enable(dev, 0);
2140 static int vip_setup_parser(struct vip_port *port)
2142         struct vip_dev *dev = port->dev;
2143         struct v4l2_of_endpoint *endpoint = dev->endpoint;
2144         int iface = DUAL_8B_INTERFACE;
2145         int sync_type, pclk_type;
2146         unsigned int flags;
2148         flags = endpoint->bus.parallel.flags;
2149         vip_reset_port(port);
2150         vip_set_port_enable(port, 1);
2152         if (endpoint->bus_type == V4L2_MBUS_BT656) {
2153                 iface = DUAL_8B_INTERFACE;
2155                 /* Ideally, this should come from sensor
2156                    port->fmt can be anything once CSC is enabled */
2157                 if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
2158                         sync_type = EMBEDDED_SYNC_SINGLE_RGB_OR_YUV444;
2159                 else {
2160                         switch (endpoint->bus.parallel.num_channels) {
2161                         case 4:
2162                                 sync_type = EMBEDDED_SYNC_4X_MULTIPLEXED_YUV422;
2163                         break;
2164                         case 2:
2165                                 sync_type = EMBEDDED_SYNC_2X_MULTIPLEXED_YUV422;
2166                         break;
2167                         case 1:
2168                         default:
2169                                 sync_type =
2170                                 EMBEDDED_SYNC_LINE_MULTIPLEXED_YUV422;
2171                         }
2172                 }
2174         } else if (endpoint->bus_type == V4L2_MBUS_PARALLEL) {
2175                 switch (endpoint->bus.parallel.bus_width) {
2176                 case 24:
2177                         iface = SINGLE_24B_INTERFACE;
2178                 break;
2179                 case 16:
2180                         iface = SINGLE_16B_INTERFACE;
2181                 break;
2182                 case 8:
2183                 default:
2184                         iface = DUAL_8B_INTERFACE;
2185                 }
2187                 if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
2188                         sync_type = DISCRETE_SYNC_SINGLE_RGB_24B;
2189                 else
2190                         sync_type = DISCRETE_SYNC_SINGLE_YUV422;
2192                 if (flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH |
2193                         V4L2_MBUS_HSYNC_ACTIVE_LOW))
2194                         vip_set_vsync_polarity(port,
2195                                 flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH ? 1 : 0);
2197                 if (flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH |
2198                         V4L2_MBUS_VSYNC_ACTIVE_LOW))
2199                         vip_set_hsync_polarity(port,
2200                                 flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH ? 1 : 0);
2202                 vip_xtra_set_repack_sel(port, 0);
2203                 vip_set_actvid_hsync_n(port, 0);
2204                 vip_set_actvid_polarity(port, 1);
2205                 vip_set_discrete_basic_mode(port);
2207         } else {
2208                 vip_err(dev, "Device doesn't support CSI2");
2209                 return -EINVAL;
2210         }
2212         pclk_type = flags & V4L2_MBUS_PCLK_SAMPLE_RISING ? 0 : 1;
2213         vip_set_pclk_polarity(port, pclk_type);
2214         vip_set_data_interface(port, iface);
2215         vip_sync_type(port, sync_type);
2216         return 0;
2219 static void vip_release_stream(struct vip_stream *stream)
2221         struct vip_dev *dev = stream->port->dev;
2223         vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
2224         vpdma_free_desc_buf(&stream->desc_list.buf);
2225         vpdma_free_desc_list(&stream->desc_list);
2228 static void stop_dma(struct vip_stream *stream)
2230         struct vip_dev *dev = stream->port->dev;
2231         int ch, size = 0;
2233         /* Create a list of channels to be cleared */
2234         for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++) {
2235                 if (stream->vpdma_channels[ch] == 1) {
2236                         stream->vpdma_channels[size++] = ch;
2237                         vip_dbg(2, dev, "Clear channel no: %d\n", ch);
2238                 }
2239         }
2241         /* Clear all the used channels for the list */
2242         vpdma_list_cleanup(dev->shared->vpdma, stream->list_num,
2243                 stream->vpdma_channels, size);
2245         for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++)
2246                 stream->vpdma_channels[ch] = 0;
2249 static int vip_open(struct file *file)
2251         struct vip_stream *stream = video_drvdata(file);
2252         struct vip_port *port = stream->port;
2253         struct vip_dev *dev = port->dev;
2254         struct v4l2_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
2255         int ret = 0;
2257         vip_dbg(2, dev, "vip_open\n");
2259         file->private_data = fh;
2260         if (fh == NULL)
2261                 return -ENOMEM;
2263         mutex_lock(&dev->mutex);
2265         v4l2_fh_init(fh, video_devdata(file));
2266         v4l2_fh_add(fh);
2268         /*
2269          * If this is the first open file.
2270          * Then initialize hw module.
2271          */
2272         if (v4l2_fh_is_singular_file(file)) {
2273                 if (vip_init_stream(stream)) {
2274                         ret = -ENODEV;
2275                         goto free_fh;
2276                 }
2277                 vip_dbg(1, dev, "Created stream instance %p\n", stream);
2278         }
2280         mutex_unlock(&dev->mutex);
2281         return 0;
2283 free_fh:
2284         mutex_unlock(&dev->mutex);
2285         if (fh) {
2286                 v4l2_fh_del(fh);
2287                 v4l2_fh_exit(fh);
2288                 kfree(fh);
2289         }
2290         return ret;
2293 static int vip_release(struct file *file)
2295         struct vip_stream *stream = video_drvdata(file);
2296         struct vip_port *port = stream->port;
2297         struct vip_dev *dev = port->dev;
2298         struct vb2_queue *q = &stream->vb_vidq;
2300         vip_dbg(2, dev, "vip_release\n");
2302         /*
2303          * If this is the last open file.
2304          * Then de-initialize hw module.
2305          */
2306         if (v4l2_fh_is_singular_file(file)) {
2307                 mutex_lock(&dev->mutex);
2309                 vip_stop_streaming(q);
2310                 vip_release_stream(stream);
2312                 if (--port->num_streams == 0)
2313                         vip_release_dev(port->dev);
2315                 mutex_unlock(&dev->mutex);
2316                 vip_dbg(1, dev, "Releasing stream instance %p\n", stream);
2317         }
2319         return vb2_fop_release(file);
2322 static const struct v4l2_file_operations vip_fops = {
2323         .owner          = THIS_MODULE,
2324         .open           = vip_open,
2325         .release        = vip_release,
2326         .read           = vb2_fop_read,
2327         .poll           = vb2_fop_poll,
2328         .unlocked_ioctl = video_ioctl2,
2329         .mmap           = vb2_fop_mmap,
2330 };
2332 static struct video_device vip_videodev = {
2333         .name           = VIP_MODULE_NAME,
2334         .fops           = &vip_fops,
2335         .ioctl_ops      = &vip_ioctl_ops,
2336         .minor          = -1,
2337         .release        = video_device_release,
2338         .tvnorms        = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
2339 };
2341 static int alloc_stream(struct vip_port *port, int stream_id, int vfl_type)
2343         struct vip_stream *stream;
2344         struct vip_dev *dev = port->dev;
2345         struct vb2_queue *q;
2346         struct video_device *vfd;
2347         struct vip_buffer *buf;
2348         int ret, i;
2350         stream = kzalloc(sizeof(*stream), GFP_KERNEL);
2351         if (!stream)
2352                 return -ENOMEM;
2354         stream->port = port;
2355         stream->stream_id = stream_id;
2356         stream->vfl_type = vfl_type;
2358         stream->list_num = vpdma_hwlist_alloc(dev->shared->vpdma, stream);
2359         if (stream->list_num < 0) {
2360                 vip_err(dev, "Could not get VPDMA hwlist");
2361                 ret = -ENODEV;
2362                 goto do_free_stream;
2363         }
2365         INIT_LIST_HEAD(&stream->post_bufs);
2367         if (vfl_type == VFL_TYPE_GRABBER)
2368                 port->cap_streams[stream_id] = stream;
2369         else
2370                 port->vbi_streams[stream_id] = stream;
2372         /*
2373          * Initialize queue
2374          */
2375         q = &stream->vb_vidq;
2376         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2377         q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2378         q->drv_priv = stream;
2379         q->buf_struct_size = sizeof(struct vip_buffer);
2380         q->ops = &vip_video_qops;
2381         q->mem_ops = &vb2_dma_contig_memops;
2382         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2384         /* Feature not back-ported yet. Enable when available */
2385         /* q->min_buffers_needed = 3; */
2387         ret = vb2_queue_init(q);
2388         if (ret)
2389                 goto do_free_stream;
2391         INIT_LIST_HEAD(&stream->vidq);
2393         /* Allocate/populate Drop queue entries */
2394         INIT_LIST_HEAD(&stream->dropq);
2395         for (i = 0; i < VIP_DROPQ_SIZE; i++) {
2397                 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
2398                 if (!buf) {
2399                         vip_err(dev, "No memory!!");
2400                         ret = -ENOMEM;
2401                         goto do_free_stream;
2402                 }
2403                 buf->drop = true;
2404                 list_add(&buf->list, &stream->dropq);
2405         }
2407         vfd = &stream->vdev;
2408         *vfd = vip_videodev;
2409         vfd->v4l2_dev = &dev->v4l2_dev;
2410         vfd->queue = q;
2411         set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
2413         vfd->lock = &dev->mutex;
2414         video_set_drvdata(vfd, stream);
2416         ret = video_register_device(vfd, vfl_type, -1);
2417         if (ret) {
2418                 vip_err(dev, "Failed to register video device\n");
2419                 goto do_free_stream;
2420         }
2422         snprintf(vfd->name, sizeof(vfd->name), "%s", vip_videodev.name);
2423         stream->vfd = vfd;
2425         vip_info(dev, VIP_MODULE_NAME
2426                  " Device registered as /dev/video%d\n", vfd->num);
2427         return 0;
2429 do_free_stream:
2430         kfree(stream);
2431         return ret;
2434 static void free_stream(struct vip_stream *stream)
2436         struct vip_dev *dev = stream->port->dev;
2437         struct vip_buffer *buf;
2438         struct list_head *pos, *q;
2440         if (!stream)
2441                 return;
2443         /* Free up the Drop queue */
2444         list_for_each_safe(pos, q, &stream->dropq) {
2445                 buf = list_entry(stream->dropq.next,
2446                                  struct vip_buffer, list);
2447                 vip_dbg(1, dev, "dropq buffer\n");
2448                 list_del(pos);
2449                 kfree(buf);
2450         }
2452         video_unregister_device(stream->vfd);
2453         video_device_release(stream->vfd);
2454         vpdma_hwlist_release(dev->shared->vpdma, stream->list_num);
2455         kfree(stream);
2458 static int get_subdev_active_format(struct vip_port *port,
2459                                     struct v4l2_subdev *subdev)
2461         struct vip_dev *dev = port->dev;
2462         struct vip_fmt *fmt;
2463         enum v4l2_mbus_pixelcode code;
2464         int ret = 0;
2465         unsigned int k, i, j;
2467         /* Enumerate sub device formats and enable all matching local formats */
2468         dev->num_active_fmt = 0;
2469         for (k = 0, i = 0;
2470              (ret != -EINVAL);
2471              k++) {
2472                 ret = v4l2_subdev_call(subdev, video, enum_mbus_fmt, k, &code);
2473                 if (ret == 0) {
2474                         vip_dbg(2, dev,
2475                                 "subdev %s: code: %04x idx: %d\n",
2476                                 subdev->name, code, k);
2478                         for (j = 0; j < ARRAY_SIZE(vip_formats); j++) {
2479                                 fmt = &vip_formats[j];
2480                                 if (code == fmt->code) {
2481                                         dev->active_fmt[i] = fmt;
2482                                         dev->num_active_fmt = i++;
2483                                         vip_dbg(2, dev,
2484                                                 "matched fourcc: %s: code: %04x idx: %d\n",
2485                                                 fourcc_to_str(fmt->fourcc),
2486                                                 fmt->code,
2487                                                 dev->num_active_fmt);
2488                                 }
2489                         }
2490                 }
2491         }
2493         if (i == 0) {
2494                 vip_err(dev, "No suitable format reported by subdev %s\n",
2495                         subdev->name);
2496                 return -EINVAL;
2497         }
2498         return 0;
2501 static int alloc_port(struct vip_dev *dev, int id)
2503         struct vip_port *port;
2504         int ret;
2506         port = kzalloc(sizeof(*port), GFP_KERNEL);
2507         if (!port)
2508                 return -ENOMEM;
2510         dev->ports[id] = port;
2511         port->dev = dev;
2512         port->port_id = id;
2513         port->num_streams = 0;
2514         port->flags |= FLAG_MULT_PORT;
2516         ret = alloc_stream(port, 0, VFL_TYPE_GRABBER);
2518         if (dev->endpoint->bus_type == V4L2_MBUS_BT656) {
2519                 /* Allocate streams for 4 channels */
2520                 ret = alloc_stream(port, 2, VFL_TYPE_GRABBER);
2521                 ret = alloc_stream(port, 4, VFL_TYPE_GRABBER);
2522                 ret = alloc_stream(port, 6, VFL_TYPE_GRABBER);
2523         }
2525         return get_subdev_active_format(port, dev->sensor);
2528 static void free_port(struct vip_port *port)
2530         if (!port)
2531                 return;
2533         free_stream(port->cap_streams[0]);
2535         kfree(port);
2538 static int get_field(u32 value, u32 mask, int shift)
2540         return (value & (mask << shift)) >> shift;
2543 static int vip_of_probe(struct platform_device *pdev, struct vip_dev *dev);
2544 static void vip_vpdma_fw_cb(struct platform_device *pdev)
2546         struct vip_shared *shared = platform_get_drvdata(pdev);
2547         struct vip_dev *dev;
2548         int slice, ret;
2550         dev_info(&pdev->dev, "VPDMA firmware loaded\n");
2552         for (slice = 0; slice < atomic_read(&shared->devs_allocated); slice++) {
2553                 dev = shared->devs[slice];
2555                 if (pdev->dev.of_node) {
2556                         ret = vip_of_probe(pdev, dev);
2557                         if (ret)
2558                                 goto free_port;
2559                 }
2560         }
2562         return;
2564 free_port:
2565         free_port(dev->ports[0]);
2568 static void remove_shared(struct vip_shared *shared)
2570         if (atomic_dec_return(&shared->devs_allocated) != 0)
2571                 return;
2573         iounmap(shared->base);
2574         release_mem_region(shared->res->start, resource_size(shared->res));
2575         kfree(shared);
2578 static int vip_runtime_get(struct platform_device *pdev)
2580         int r;
2582         r = pm_runtime_get_sync(&pdev->dev);
2583         WARN_ON(r < 0);
2584         return r < 0 ? r : 0;
2588 static int vip_async_bound(struct v4l2_async_notifier *notifier,
2589                         struct v4l2_subdev *subdev,
2590                         struct v4l2_async_subdev *asd)
2592         struct vip_dev *dev = notifier_to_vip_dev(notifier);
2593         unsigned int idx = asd - &dev->config->asd[0];
2594         int ret;
2596         vip_dbg(1, dev, "vip_async_bound\n");
2597         if (idx > dev->config->asd_sizes)
2598                 return -EINVAL;
2600         if (dev->sensor) {
2601                 if (asd < dev->sensor->asd) {
2602                         /* Notified of a subdev earlier in the array */
2603                         dev->sensor = subdev;
2604                         dev->endpoint = &dev->config->endpoints[idx];
2605                         vip_info(dev, "Switching to subdev %s (High priority)",
2606                                  subdev->name);
2608                 } else
2609                         vip_info(dev, "Rejecting subdev %s (Low priority)",
2610                                  subdev->name);
2611                 return 0;
2612         }
2614         dev->sensor = subdev;
2615         dev->endpoint = &dev->config->endpoints[idx];
2616         ret = alloc_port(dev, 0);
2617         if (!ret)
2618                 vip_info(dev, "Using sensor %s for capture\n", subdev->name);
2620         return ret;
2623 static int vip_async_complete(struct v4l2_async_notifier *notifier)
2625         struct vip_dev *dev = notifier_to_vip_dev(notifier);
2626         vip_dbg(1, dev, "vip_async_complete\n");
2627         return 0;
2630 static struct device_node *
2631 of_get_next_port(const struct device_node *parent,
2632                  struct device_node *prev)
2634         struct device_node *port = NULL;
2636         if (!parent)
2637                 return NULL;
2639         if (!prev) {
2640                 struct device_node *ports;
2641                 /*
2642                  * It's the first call, we have to find a port subnode
2643                  * within this node or within an optional 'ports' node.
2644                  */
2645                 ports = of_get_child_by_name(parent, "ports");
2646                 if (ports)
2647                         parent = ports;
2649                 port = of_get_child_by_name(parent, "port");
2651                 /* release the 'ports' node */
2652                 of_node_put(ports);
2653         } else {
2654                 struct device_node *ports;
2656                 ports = of_get_parent(prev);
2657                 if (!ports)
2658                         return NULL;
2660                 do {
2661                         port = of_get_next_child(ports, prev);
2662                         if (!port) {
2663                                 of_node_put(ports);
2664                                 return NULL;
2665                         }
2666                         prev = port;
2667                 } while (of_node_cmp(port->name, "port") != 0);
2668         }
2670         return port;
2673 static struct device_node *
2674 of_get_next_endpoint(const struct device_node *parent,
2675                         struct device_node *prev)
2677         struct device_node *ep = NULL;
2679         if (!parent)
2680                 return NULL;
2682         do {
2683                 ep = of_get_next_child(parent, prev);
2684                 if (!ep)
2685                         return NULL;
2686                 prev = ep;
2687         } while (of_node_cmp(ep->name, "endpoint") != 0);
2689         return ep;
2692 static int vip_of_probe(struct platform_device *pdev, struct vip_dev *dev)
2694         struct device_node *ep_node = NULL, *port, *remote_ep,
2695                         *sensor_node, *parent;
2696         struct device_node *syscon_np;
2697         struct v4l2_of_endpoint *endpoint;
2698         struct v4l2_async_subdev *asd;
2699         u32 regval = 0;
2700         int ret, slice, i = 0, found_port = 0;
2702         parent = pdev->dev.of_node;
2704         syscon_np = of_parse_phandle(pdev->dev.of_node, "syscon", 0);
2705         dev->syscon = syscon_node_to_regmap(syscon_np);
2706         of_node_put(syscon_np);
2708         dev->config = kzalloc(sizeof(struct vip_config), GFP_KERNEL);
2709         if (!dev->config)
2710                 return -ENOMEM;
2712         dev->config->card_name = "VIP Driver";
2714         port = NULL;
2715         vip_dbg(3, dev, "Scanning Port node for slice id: %d\n", dev->slice_id);
2716         for (slice = 0; slice < VIP_NUM_SLICES; slice++) {
2717                 port = of_get_next_port(parent, port);
2718                 if (!port) {
2719                         vip_dbg(1, dev, "No port node found for slice_id:%d\n",
2720                                 slice);
2721                         ret = -EINVAL;
2722                         goto free_config;
2723                 }
2725                 /* Match the slice number with <REG> */
2726                 of_property_read_u32(port, "reg", &regval);
2727                 vip_dbg(3, dev, "slice:%d dev->slice_id:%d <reg>:%d\n",
2728                         slice, dev->slice_id, regval);
2729                 if ((regval == dev->slice_id) && (slice == dev->slice_id)) {
2730                         found_port = 1;
2731                         break;
2732                 }
2733         }
2735         if (!found_port) {
2736                 if (!port)
2737                         of_node_put(port);
2738                 vip_dbg(1, dev, "No port node matches slice_id:%d\n",
2739                         dev->slice_id);
2740                 ret = -EINVAL;
2741                 goto free_config;
2742         }
2744         vip_dbg(3, dev, "Scanning sub-device(s) for slice id: %d\n",
2745                 dev->slice_id);
2746         while (i < VIP_MAX_SUBDEV) {
2748                 asd = &dev->config->asd[i];
2749                 endpoint = &dev->config->endpoints[i];
2751                 remote_ep = NULL;
2752                 sensor_node = NULL;
2754                 ep_node = of_get_next_endpoint(port, ep_node);
2755                 if (!ep_node) {
2756                         vip_dbg(3, dev, "can't get next endpoint: loop: %d\n",
2757                                 i);
2758                         break;
2759                 }
2761                 sensor_node = of_graph_get_remote_port_parent(ep_node);
2762                 if (!sensor_node) {
2763                         vip_dbg(3, dev, "can't get remote parent: loop: %d\n",
2764                                 i);
2765                         goto of_node_cleanup;
2766                 }
2767                 asd->match_type = V4L2_ASYNC_MATCH_OF;
2768                 asd->match.of.node = sensor_node;
2770                 remote_ep = of_parse_phandle(ep_node, "remote-endpoint", 0);
2771                 if (!remote_ep) {
2772                         vip_dbg(3, dev, "can't get remote-endpoint: loop: %d\n",
2773                                 i);
2774                         goto of_node_cleanup;
2775                 }
2776                 v4l2_of_parse_endpoint(remote_ep, endpoint);
2778                 dev->config->asd_list[i++] = asd;
2780 of_node_cleanup:
2781                 if (!remote_ep)
2782                         of_node_put(remote_ep);
2783                 if (!sensor_node)
2784                         of_node_put(sensor_node);
2785         }
2787         if (!ep_node)
2788                 of_node_put(ep_node);
2789         if (!port)
2790                 of_node_put(port);
2792         vip_dbg(1, dev, "Found %d sub-device(s) for slice id: %d\n",
2793                 i, dev->slice_id);
2794         if (i > 0) {
2795                 dev->config->asd_sizes = i;
2796                 dev->notifier.subdevs = dev->config->asd_list;
2797                 dev->notifier.num_subdevs = dev->config->asd_sizes;
2798                 dev->notifier.bound = vip_async_bound;
2799                 dev->notifier.complete = vip_async_complete;
2801                 vip_dbg(1, dev, "registering the sync notifier for sensors %d",
2802                         i);
2803                 ret = v4l2_async_notifier_register(&dev->v4l2_dev,
2804                                                    &dev->notifier);
2805                 if (ret) {
2806                         vip_dbg(1, dev, "Error registering async notifier\n");
2807                         ret = -EINVAL;
2808                         goto free_config;
2809                 }
2810         }
2812         return 0;
2813 free_config:
2814         kfree(dev->config);
2815         return ret;
2818 static const struct of_device_id vip_of_match[];
2819 static int vip_probe(struct platform_device *pdev)
2821         struct vip_dev *dev;
2822         struct vip_shared *shared;
2823         const struct of_device_id *of_dev_id;
2824         struct pinctrl *pinctrl;
2825         int ret, slice = VIP_SLICE1;
2826         u32 tmp, pid;
2827         struct v4l2_ctrl_handler *hdl;
2829         pm_runtime_enable(&pdev->dev);
2831         ret = vip_runtime_get(pdev);
2832         if (ret)
2833                 goto err_runtime_get;
2835         of_dev_id = of_match_device(vip_of_match, &pdev->dev);
2836         if (!of_dev_id) {
2837                 dev_err(&pdev->dev, "%s: Unable to match device\n", __func__);
2838                 return -ENODEV;
2839         }
2841         shared = kzalloc(sizeof(*shared), GFP_KERNEL);
2842         if (!shared)
2843                 return -ENOMEM;
2845         shared->res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vip");
2846         if (shared->res == NULL) {
2847                 dev_err(&pdev->dev, "Missing platform resources data\n");
2848                 ret = -ENODEV;
2849                 goto free_shared;
2850         }
2852         pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2853         if (IS_ERR(pinctrl)) {
2854                 dev_warn(&pdev->dev, "No explicit pinctrl resources data.\n");
2855         }
2857         if (devm_request_mem_region(&pdev->dev, shared->res->start,
2858             resource_size(shared->res), VIP_MODULE_NAME) == NULL) {
2859                 ret = -ENOMEM;
2860                 goto free_shared;
2861         }
2863         shared->base = devm_ioremap(&pdev->dev, shared->res->start,
2864                                     resource_size(shared->res));
2865         if (!shared->base) {
2866                 dev_err(&pdev->dev, "failed to ioremap\n");
2867                 ret = -ENOMEM;
2868                 goto rel_mem_region;
2869         }
2871         /* Make sure H/W module has the right functionality */
2872         pid = read_sreg(shared, VIP_PID);
2873         tmp = get_field(pid, VIP_PID_FUNC_MASK, VIP_PID_FUNC_SHIFT);
2875         if (tmp != VIP_PID_FUNC) {
2876                 dev_info(&pdev->dev, "vip: unexpected PID function: 0x%x\n",
2877                        tmp);
2878                 ret = -ENODEV;
2879                 goto do_iounmap;
2880         }
2882         /* enable clocks, so the firmware will load properly */
2883         vip_shared_set_clock_enable(shared, 1);
2884         vip_top_vpdma_reset(shared);
2886         list_add_tail(&shared->list, &vip_shared_list);
2887         platform_set_drvdata(pdev, shared);
2888         atomic_set(&shared->devs_allocated, 0);
2890         vip_set_idle_mode(shared, VIP_SMART_IDLE_MODE);
2891         vip_set_standby_mode(shared, VIP_SMART_STANDBY_MODE);
2893         for (slice = VIP_SLICE1; slice < VIP_NUM_SLICES; slice++) {
2894                 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2895                 if (!dev)
2896                         return -ENOMEM;
2898                 dev->instance_id = (int)of_dev_id->data;
2899                 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
2900                         "%s%d-s%d", VIP_MODULE_NAME, dev->instance_id, slice);
2902                 dev->irq = platform_get_irq(pdev, slice);
2903                 if (!dev->irq) {
2904                         dev_err(&pdev->dev, "Could not get IRQ");
2905                         goto err_runtime_get;
2906                 }
2908                 if (devm_request_irq(&pdev->dev, dev->irq, vip_irq,
2909                                      0, dev->v4l2_dev.name, dev) < 0) {
2910                         ret = -ENOMEM;
2911                         goto dev_unreg;
2912                 }
2914                 spin_lock_init(&dev->slock);
2915                 spin_lock_init(&dev->lock);
2917                 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2918                 if (ret)
2919                         goto err_runtime_get;
2921                 mutex_init(&dev->mutex);
2923                 hdl = &dev->ctrl_handler;
2924                 v4l2_ctrl_handler_init(hdl, 11);
2925                 dev->v4l2_dev.ctrl_handler = hdl;
2927                 dev->slice_id = slice;
2928                 dev->pdev = pdev;
2929                 dev->res = shared->res;
2930                 dev->base = shared->base;
2932                 dev->shared = shared;
2933                 shared->devs[slice] = dev;
2935                 atomic_inc(&shared->devs_allocated);
2937                 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2938                 if (IS_ERR(dev->alloc_ctx)) {
2939                         vip_err(dev, "Failed to alloc vb2 context\n");
2940                         ret = PTR_ERR(dev->alloc_ctx);
2941                         goto dev_unreg;
2942                 }
2944                 vip_top_reset(dev);
2945                 vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT);
2946         }
2948         shared->vpdma = &shared->vpdma_data;
2949         ret = vpdma_create(pdev, shared->vpdma, vip_vpdma_fw_cb);
2950         if (ret) {
2951                 dev_err(&pdev->dev, "Creating VPDMA failed");
2952                 goto dev_unreg;
2953         }
2955         return 0;
2957 dev_unreg:
2958         v4l2_device_unregister(&dev->v4l2_dev);
2959 do_iounmap:
2960         iounmap(shared->base);
2961 rel_mem_region:
2962         release_mem_region(shared->res->start, resource_size(shared->res));
2963 free_shared:
2964         kfree(shared);
2965 err_runtime_get:
2966         if (slice == VIP_SLICE1) {
2967                 pm_runtime_disable(&pdev->dev);
2968                 return ret;
2969         } else
2970                 return 0;
2973 static int vip_remove(struct platform_device *pdev)
2975         struct vip_shared *shared = platform_get_drvdata(pdev);
2976         struct vip_dev *dev;
2977         int slice;
2979         for (slice = 0; slice < atomic_read(&shared->devs_allocated); slice++) {
2980                 dev = shared->devs[slice];
2981                 if (!dev)
2982                         continue;
2983                 vip_info(dev, "Removing " VIP_MODULE_NAME);
2984                 free_port(dev->ports[0]);
2985                 v4l2_async_notifier_unregister(&dev->notifier);
2986                 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2987                 free_irq(dev->irq, dev);
2988                 kfree(dev);
2989         }
2990         remove_shared(shared);
2992         return 0;
2995 #if defined(CONFIG_OF)
2996 static const struct of_device_id vip_of_match[] = {
2997         {
2998                 .compatible = "ti,vip1", .data = (void *) VIP_INSTANCE1,
2999         },
3001         {
3002                 .compatible = "ti,vip2", .data = (void *) VIP_INSTANCE2,
3003         },
3005         {
3006                 .compatible = "ti,vip3", .data = (void *) VIP_INSTANCE3,
3007         },
3008         {},
3009 };
3010 MODULE_DEVICE_TABLE(of, vip_of_match);
3011 #else
3012 #define vip_of_match NULL
3013 #endif
3015 static struct platform_driver vip_pdrv = {
3016         .probe          = vip_probe,
3017         .remove         = vip_remove,
3018         .driver         = {
3019                 .name   = VIP_MODULE_NAME,
3020                 .owner  = THIS_MODULE,
3021                 .of_match_table = vip_of_match,
3022         },
3023 };
3025 module_platform_driver(vip_pdrv);
3027 MODULE_DESCRIPTION("TI VIP driver");
3028 MODULE_AUTHOR("Texas Instruments");
3029 MODULE_LICENSE("GPL v2");