]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - android-sdk/kernel-video.git/blob - drivers/media/platform/ti-vpe/vip.c
Merge branch 'pm-ti-linux-3.14.y' of git://git.ti.com/~kristo/ti-linux-kernel/pm...
[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           = "YUV 444 co-planar",
131                 .fourcc         = V4L2_PIX_FMT_NV24,
132                 .code           = V4L2_MBUS_FMT_YDYUYDYV8_1X16,
133                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
134                 .coplanar       = 1,
135                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y444],
136                                     &vpdma_yuv_fmts[VPDMA_DATA_FMT_C444],
137                                   },
138         },
139         {
140                 .name           = "YUV 422 co-planar",
141                 .fourcc         = V4L2_PIX_FMT_NV16,
142                 .code           = V4L2_MBUS_FMT_YDYUYDYV8_1X16,
143                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
144                 .coplanar       = 1,
145                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y422],
146                                     &vpdma_yuv_fmts[VPDMA_DATA_FMT_C422],
147                                   },
148         },
149         {
150                 .name           = "YUV 420 co-planar",
151                 .fourcc         = V4L2_PIX_FMT_NV12,
152                 .code           = V4L2_MBUS_FMT_YDYUYDYV8_1X16,
153                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
154                 .coplanar       = 1,
155                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_Y420],
156                                     &vpdma_yuv_fmts[VPDMA_DATA_FMT_C420],
157                                   },
158         },
159         {
160                 .name           = "UYVY 422 packed",
161                 .fourcc         = V4L2_PIX_FMT_UYVY,
162                 .code           = V4L2_MBUS_FMT_UYVY8_2X8,
163                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
164                 .coplanar       = 0,
165                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CY422],
166                                   },
167         },
168         {
169                 .name           = "YUYV 422 packed",
170                 .fourcc         = V4L2_PIX_FMT_YUYV,
171                 .code           = V4L2_MBUS_FMT_YUYV8_2X8,
172                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
173                 .coplanar       = 0,
174                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YC422],
175                                   },
176         },
177         {
178                 .name           = "VYUY 422 packed",
179                 .fourcc         = V4L2_PIX_FMT_VYUY,
180                 .code           = V4L2_MBUS_FMT_VYUY8_2X8,
181                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
182                 .coplanar       = 0,
183                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_YC422],
184                                   },
185         },
186         {
187                 .name           = "RGB888 packed",
188                 .fourcc         = V4L2_PIX_FMT_RGB24,
189                 .code           = V4L2_MBUS_FMT_RGB888_1X24,
190                 .colorspace     = V4L2_COLORSPACE_SRGB,
191                 .coplanar       = 0,
192                 .vpdma_fmt      = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
193                                   },
194         },
195         {
196                 .name           = "ARGB888 packed",
197                 .fourcc         = V4L2_PIX_FMT_RGB32,
198                 .code           = V4L2_MBUS_FMT_ARGB8888_1X32,
199                 .colorspace     = V4L2_COLORSPACE_SRGB,
200                 .coplanar       = 0,
201                 .vpdma_fmt      = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
202                                   },
203         },
204 };
206 /*  Print Four-character-code (FOURCC) */
207 static char *fourcc_to_str(u32 fmt)
209         static char code[5];
210         code[0] = (unsigned char)(fmt & 0xff);
211         code[1] = (unsigned char)((fmt>>8) & 0xff);
212         code[2] = (unsigned char)((fmt>>16) & 0xff);
213         code[3] = (unsigned char)((fmt>>24) & 0xff);
214         code[4] = '\0';
216         return code;
219 /*
220  * Find our format description corresponding to the passed v4l2_format
221  */
222 #ifdef DISABLED_FOR_NOW
223 static struct vip_fmt *find_format_by_pix(u32 pixelformat)
225         struct vip_fmt *fmt;
226         unsigned int k;
228         for (k = 0; k < ARRAY_SIZE(vip_formats); k++) {
229                 fmt = &vip_formats[k];
230                 if (fmt->fourcc == pixelformat)
231                         return fmt;
232         }
234         return NULL;
236 #endif
238 static struct vip_fmt *find_format_by_code(u32 code)
240         struct vip_fmt *fmt;
241         unsigned int k;
243         for (k = 0; k < ARRAY_SIZE(vip_formats); k++) {
244                 fmt = &vip_formats[k];
245                 if (fmt->code == code)
246                         return fmt;
247         }
249         return NULL;
252 static struct vip_fmt *find_active_format_by_pix(struct vip_dev *dev,
253                                                  u32 pixelformat)
255         struct vip_fmt *fmt;
256         unsigned int k;
258         for (k = 0; k < dev->num_active_fmt; k++) {
259                 fmt = dev->active_fmt[k];
260                 if (fmt->fourcc == pixelformat)
261                         return fmt;
262         }
264         return NULL;
267 static struct vip_fmt *find_active_format_by_code(struct vip_dev *dev,
268                                                  u32 code)
270         struct vip_fmt *fmt;
271         unsigned int k;
273         for (k = 0; k < dev->num_active_fmt; k++) {
274                 fmt = dev->active_fmt[k];
275                 if (fmt->code == code)
276                         return fmt;
277         }
279         return NULL;
282 static LIST_HEAD(vip_shared_list);
284 static inline struct vip_dev *notifier_to_vip_dev(struct v4l2_async_notifier *n)
286         return container_of(n, struct vip_dev, notifier);
289 /*
290  * port flag bits
291  */
292 #define FLAG_FRAME_1D           (1 << 0)
293 #define FLAG_EVEN_LINE_SKIP     (1 << 1)
294 #define FLAG_ODD_LINE_SKIP      (1 << 2)
295 #define FLAG_MODE_TILED         (1 << 3)
296 #define FLAG_INTERLACED         (1 << 4)
297 #define FLAG_MULTIPLEXED        (1 << 5)
298 #define FLAG_MULT_PORT          (1 << 6)
299 #define FLAG_MULT_ANC           (1 << 7)
301 /*
302  * Function prototype declarations
303  */
304 static int alloc_port(struct vip_dev *, int);
305 static void free_port(struct vip_port *);
306 static int vip_setup_parser(struct vip_port *port);
307 static void stop_dma(struct vip_stream *stream);
309 static inline u32 read_sreg(struct vip_shared *shared, int offset)
311         return ioread32(shared->base + offset);
314 static inline void write_sreg(struct vip_shared *shared, int offset, u32 value)
316         iowrite32(value, shared->base + offset);
319 static inline u32 read_vreg(struct vip_dev *dev, int offset)
321         return ioread32(dev->base + offset);
324 static inline void write_vreg(struct vip_dev *dev, int offset, u32 value)
326         iowrite32(value, dev->base + offset);
329 /*
330  * Insert a masked field into a 32-bit field
331  */
332 static void insert_field(u32 *valp, u32 field, u32 mask, int shift)
334         u32 val = *valp;
336         val &= ~(mask << shift);
337         val |= (field & mask) << shift;
338         *valp = val;
341 /*
342  * Set the system idle mode
343  */
344 static void vip_set_idle_mode(struct vip_shared *shared, int mode)
346         u32 reg = read_sreg(shared, VIP_SYSCONFIG);
347         insert_field(&reg, mode, VIP_SYSCONFIG_IDLE_MASK,
348                      VIP_SYSCONFIG_IDLE_SHIFT);
349         write_sreg(shared, VIP_SYSCONFIG, reg);
352 /*
353  * Set the VIP standby mode
354  */
355 static void vip_set_standby_mode(struct vip_shared *shared, int mode)
357         u32 reg = read_sreg(shared, VIP_SYSCONFIG);
358         insert_field(&reg, mode, VIP_SYSCONFIG_STANDBY_MASK,
359                      VIP_SYSCONFIG_STANDBY_SHIFT);
360         write_sreg(shared, VIP_SYSCONFIG, reg);
363 /*
364  * Enable or disable the VIP clocks
365  */
366 static void vip_set_clock_enable(struct vip_dev *dev, bool on)
368         u32 val = 0;
370         val = read_vreg(dev, VIP_CLK_ENABLE);
371         if (on) {
372                 val |= VIP_VPDMA_CLK_ENABLE;
373                 if (dev->slice_id == VIP_SLICE1)
374                         val |= VIP_VIP1_DATA_PATH_CLK_ENABLE;
375                 else
376                         val |= VIP_VIP2_DATA_PATH_CLK_ENABLE;
377         } else {
378                 if (dev->slice_id == VIP_SLICE1)
379                         val &= ~VIP_VIP1_DATA_PATH_CLK_ENABLE;
380                 else
381                         val &= ~VIP_VIP2_DATA_PATH_CLK_ENABLE;
383                 /* Both VIP are disabled then shutdown VPDMA also */
384                 if (!(val & (VIP_VIP1_DATA_PATH_CLK_ENABLE|
385                              VIP_VIP2_DATA_PATH_CLK_ENABLE)))
386                         val = 0;
387         }
389         write_vreg(dev, VIP_CLK_ENABLE, val);
392 /* This helper function is used to enable the clock early on to
393  * enable vpdma firmware loading before the slice device are created
394  */
395 static void vip_shared_set_clock_enable(struct vip_shared *shared, bool on)
397         u32 val = 0;
399         if (on)
400                 val = VIP_VIP1_DATA_PATH_CLK_ENABLE | VIP_VPDMA_CLK_ENABLE;
402         write_sreg(shared, VIP_CLK_ENABLE, val);
405 static void vip_top_reset(struct vip_dev *dev)
407         u32 val = 0;
409         val = read_vreg(dev, VIP_CLK_RESET);
411         if (dev->slice_id == VIP_SLICE1)
412                 insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
413                         VIP_VIP1_DATA_PATH_RESET_SHIFT);
414         else
415                 insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
416                         VIP_VIP2_DATA_PATH_RESET_SHIFT);
418         write_vreg(dev, VIP_CLK_RESET, val);
420         usleep_range(200, 250);
422         val = read_vreg(dev, VIP_CLK_RESET);
424         if (dev->slice_id == VIP_SLICE1)
425                 insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
426                         VIP_VIP1_DATA_PATH_RESET_SHIFT);
427         else
428                 insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
429                         VIP_VIP2_DATA_PATH_RESET_SHIFT);
430         write_vreg(dev, VIP_CLK_RESET, val);
433 static void vip_top_vpdma_reset(struct vip_shared *shared)
435         u32 val;
437         val = read_sreg(shared, VIP_CLK_RESET);
438         insert_field(&val, 1, VIP_VPDMA_CLK_RESET_MASK,
439                 VIP_VPDMA_CLK_RESET_SHIFT);
440         write_sreg(shared, VIP_CLK_RESET, val);
442         usleep_range(200, 250);
444         val = read_sreg(shared, VIP_CLK_RESET);
445         insert_field(&val, 0, VIP_VPDMA_CLK_RESET_MASK,
446                 VIP_VPDMA_CLK_RESET_SHIFT);
447         write_sreg(shared, VIP_CLK_RESET, val);
450 static void vip_xtra_set_repack_sel(struct vip_port *port, int repack_mode)
452         u32 val;
454         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
455                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
456                                 VIP_PARSER_PORTA_1);
457                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
458                              VIP_REPACK_SEL_SHFT);
460                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
461                            VIP_PARSER_PORTA_1, val);
462         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
463                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
464                                 VIP_PARSER_PORTA_1);
465                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
466                              VIP_REPACK_SEL_SHFT);
468                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
469                            VIP_PARSER_PORTA_1, val);
470         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
471                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
472                                 VIP_PARSER_PORTB_1);
473                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
474                              VIP_REPACK_SEL_SHFT);
476                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
477                            VIP_PARSER_PORTB_1, val);
478         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
479                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
480                                 VIP_PARSER_PORTB_1);
481                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
482                              VIP_REPACK_SEL_SHFT);
484                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
485                            VIP_PARSER_PORTB_1, val);
486         }
489 static void vip_set_discrete_basic_mode(struct vip_port *port)
491         u32 val;
493         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
494                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
495                                 VIP_PARSER_PORTA_0);
496                 val |= VIP_DISCRETE_BASIC_MODE;
498                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
499                            VIP_PARSER_PORTA_0, val);
500         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
501                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
502                                 VIP_PARSER_PORTA_0);
503                 val |= VIP_DISCRETE_BASIC_MODE;
505                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
506                            VIP_PARSER_PORTA_0, val);
507         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
508                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
509                                 VIP_PARSER_PORTB_0);
510                 val |= VIP_DISCRETE_BASIC_MODE;
512                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
513                            VIP_PARSER_PORTB_0, val);
514         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
515                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
516                                 VIP_PARSER_PORTB_0);
517                 val |= VIP_DISCRETE_BASIC_MODE;
519                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
520                            VIP_PARSER_PORTB_0, val);
521         }
524 static void vip_set_pclk_polarity(struct vip_port *port, int polarity)
526         u32 val, ret, offset;
528         if (polarity == 0 && port->dev->syscon) {
530                 /*
531                  * When the VIP parser is configured to so that the pixel clock
532                  * is to be sampled at falling edge, the pixel clock needs to be
533                  * inverted before it is given to the VIP module. This is done
534                  * by setting a bit in the CTRL_CORE_SMA_SW1 register.
535                  */
537                 if (port->dev->instance_id == VIP_INSTANCE1)
538                         offset = 0 + 2 * port->port_id + port->dev->slice_id;
539                 else if (port->dev->instance_id == VIP_INSTANCE2)
540                         offset = 4 + 2 * port->port_id + port->dev->slice_id;
541                 else if (port->dev->instance_id == VIP_INSTANCE3)
542                         offset = 10 - port->dev->slice_id;
543                 else
544                         BUG();
546                 ret = regmap_update_bits(port->dev->syscon,
547                         CTRL_CORE_SMA_SW_1, 1 << offset, 1 << offset);
548         }
550         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
551                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
552                                 VIP_PARSER_PORTA_0);
553                 if (polarity)
554                         val |= VIP_PIXCLK_EDGE_POLARITY;
555                 else
556                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
558                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
559                            VIP_PARSER_PORTA_0, val);
560         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
561                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET
562                                 + VIP_PARSER_PORTA_0);
563                 if (polarity)
564                         val |= VIP_PIXCLK_EDGE_POLARITY;
565                 else
566                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
567                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
568                            VIP_PARSER_PORTA_0, val);
569         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
570                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
571                                 VIP_PARSER_PORTB_0);
572                 if (polarity)
573                         val |= VIP_PIXCLK_EDGE_POLARITY;
574                 else
575                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
576                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
577                            VIP_PARSER_PORTB_0, val);
578         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
579                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
580                                 VIP_PARSER_PORTB_0);
581                 if (polarity)
582                         val |= VIP_PIXCLK_EDGE_POLARITY;
583                 else
584                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
586                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
587                            VIP_PARSER_PORTB_0, val);
588         }
591 static void vip_set_vsync_polarity(struct vip_port *port, int polarity)
593         u32 val;
595         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
596                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
597                                 VIP_PARSER_PORTA_0);
598                 if (polarity)
599                         val |= VIP_VSYNC_POLARITY;
600                 else
601                         val &= ~VIP_VSYNC_POLARITY;
603                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
604                            VIP_PARSER_PORTA_0, val);
605         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
606                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
607                                 VIP_PARSER_PORTA_0);
608                 if (polarity)
609                         val |= VIP_VSYNC_POLARITY;
610                 else
611                         val &= ~VIP_VSYNC_POLARITY;
612                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
613                            VIP_PARSER_PORTA_0, val);
614         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
615                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
616                                 VIP_PARSER_PORTB_0);
617                 if (polarity)
618                         val |= VIP_VSYNC_POLARITY;
619                 else
620                         val &= ~VIP_VSYNC_POLARITY;
621                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
622                            VIP_PARSER_PORTB_0, val);
623         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
624                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
625                                 VIP_PARSER_PORTB_0);
626                 if (polarity)
627                         val |= VIP_VSYNC_POLARITY;
628                 else
629                         val &= ~VIP_VSYNC_POLARITY;
631                 write_vreg(port->dev,  VIP2_PARSER_REG_OFFSET +
632                            VIP_PARSER_PORTB_0, val);
633         }
636 static void vip_set_hsync_polarity(struct vip_port *port, int polarity)
638         u32 val;
640         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
641                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
642                                 VIP_PARSER_PORTA_0);
643                 if (polarity)
644                         val |= VIP_HSYNC_POLARITY;
645                 else
646                         val &= ~VIP_HSYNC_POLARITY;
648                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
649                            VIP_PARSER_PORTA_0, val);
650         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
651                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
652                                 VIP_PARSER_PORTA_0);
653                 if (polarity)
654                         val |= VIP_HSYNC_POLARITY;
655                 else
656                         val &= ~VIP_HSYNC_POLARITY;
657                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
658                            VIP_PARSER_PORTA_0, val);
659         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
660                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
661                                 VIP_PARSER_PORTB_0);
662                 if (polarity)
663                         val |= VIP_HSYNC_POLARITY;
664                 else
665                         val &= ~VIP_HSYNC_POLARITY;
666                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
667                            VIP_PARSER_PORTB_0, val);
668         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
669                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
670                                 VIP_PARSER_PORTB_0);
671                 if (polarity)
672                         val |= VIP_HSYNC_POLARITY;
673                 else
674                         val &= ~VIP_HSYNC_POLARITY;
675                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
676                            VIP_PARSER_PORTB_0, val);
677         }
680 static void vip_set_actvid_polarity(struct vip_port *port, int polarity)
682         u32 val;
684         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
685                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
686                                 VIP_PARSER_PORTA_0);
687                 if (polarity)
688                         val |= VIP_ACTVID_POLARITY;
689                 else
690                         val &= ~VIP_ACTVID_POLARITY;
692                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
693                            VIP_PARSER_PORTA_0, val);
694         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
695                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
696                                 VIP_PARSER_PORTA_0);
697                 if (polarity)
698                         val |= VIP_ACTVID_POLARITY;
699                 else
700                         val &= ~VIP_ACTVID_POLARITY;
701                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
702                            VIP_PARSER_PORTA_0, val);
703         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
704                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
705                                 VIP_PARSER_PORTB_0);
706                 if (polarity)
707                         val |= VIP_ACTVID_POLARITY;
708                 else
709                         val &= ~VIP_ACTVID_POLARITY;
710                 write_vreg(port->dev,  VIP1_PARSER_REG_OFFSET +
711                            VIP_PARSER_PORTB_0, val);
712         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
713                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
714                                 VIP_PARSER_PORTB_0);
715                 if (polarity)
716                         val |= VIP_ACTVID_POLARITY;
717                 else
718                         val &= ~VIP_ACTVID_POLARITY;
720                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
721                            VIP_PARSER_PORTB_0, val);
722         }
725 static void vip_set_actvid_hsync_n(struct vip_port *port, int enable)
727         u32 val;
729         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
730                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
731                                 VIP_PARSER_PORTA_0);
732                 if (enable)
733                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
734                 else
735                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
737                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
738                            VIP_PARSER_PORTA_0, val);
739         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
740                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
741                                 VIP_PARSER_PORTA_0);
742                 if (enable)
743                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
744                 else
745                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
746                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
747                            VIP_PARSER_PORTA_0, val);
748         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
749                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
750                                 VIP_PARSER_PORTB_0);
751                 if (enable)
752                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
753                 else
754                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
755                 write_vreg(port->dev,  VIP1_PARSER_REG_OFFSET +
756                            VIP_PARSER_PORTB_0, val);
757         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
758                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
759                                 VIP_PARSER_PORTB_0);
760                 if (enable)
761                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
762                 else
763                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
765                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
766                            VIP_PARSER_PORTB_0, val);
767         }
770 static void vip_sync_type(struct vip_port *port, enum sync_types sync)
772         u32 val;
774         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
775                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
776                                 VIP_PARSER_PORTA_0);
777                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
778                              VIP_SYNC_TYPE_SHFT);
780                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
781                            VIP_PARSER_PORTA_0, val);
782         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
783                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
784                                 VIP_PARSER_PORTA_0);
785                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
786                              VIP_SYNC_TYPE_SHFT);
788                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
789                            VIP_PARSER_PORTA_0, val);
790         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
791                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
792                                 VIP_PARSER_PORTB_0);
793                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
794                              VIP_SYNC_TYPE_SHFT);
796                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
797                            VIP_PARSER_PORTB_0, val);
798         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
799                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
800                                 VIP_PARSER_PORTB_0);
801                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
802                              VIP_SYNC_TYPE_SHFT);
804                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
805                            VIP_PARSER_PORTB_0, val);
806         }
809 static void vip_set_data_interface(struct vip_port *port,
810                                    enum data_interface_modes mode)
812         u32 val = 0;
814         if (port->dev->slice_id == VIP_SLICE1) {
815                 insert_field(&val, mode, VIP_DATA_INTERFACE_MODE_MASK,
816                              VIP_DATA_INTERFACE_MODE_SHFT);
818                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET, val);
819         } else if (port->dev->slice_id == VIP_SLICE2) {
820                 insert_field(&val, mode, VIP_DATA_INTERFACE_MODE_MASK,
821                              VIP_DATA_INTERFACE_MODE_SHFT);
823                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET, val);
824         }
827 static void vip_reset_port(struct vip_port *port)
829         u32 val = 0;
831         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
832                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
833                            VIP_PARSER_PORTA_0, VIP_SW_RESET);
835                 usleep_range(200, 250);
837                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
838                                 VIP_PARSER_PORTA_0);
840                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
841                            VIP_PARSER_PORTA_0, 0);
842         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
843                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
844                            VIP_PARSER_PORTA_0, VIP_SW_RESET);
846                 usleep_range(200, 250);
848                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
849                                 VIP_PARSER_PORTA_0);
851                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
852                            VIP_PARSER_PORTA_0, 0);
854         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
855                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
856                            VIP_PARSER_PORTB_0, VIP_SW_RESET);
858                 usleep_range(200, 250);
860                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
861                                 VIP_PARSER_PORTB_0);
863                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
864                            VIP_PARSER_PORTB_0, 0);
865         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
866                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
867                            VIP_PARSER_PORTB_0, VIP_SW_RESET);
869                 usleep_range(200, 250);
871                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
872                                 VIP_PARSER_PORTB_0);
874                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
875                            VIP_PARSER_PORTB_0, 0);
876         }
879 static void vip_set_port_enable(struct vip_port *port, bool on)
881         u32 val = 0;
883         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
884                 if (on)
885                         val |= VIP_PORT_ENABLE;
886                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
887                            VIP_PARSER_PORTA_0, val);
888         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
889                 if (on)
890                         val |= VIP_PORT_ENABLE;
891                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
892                            VIP_PARSER_PORTA_0, val);
893         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
894                 if (on)
895                         val |= VIP_PORT_ENABLE;
896                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
897                            VIP_PARSER_PORTB_0, val);
898         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
899                 if (on)
900                         val |= VIP_PORT_ENABLE;
901                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
902                            VIP_PARSER_PORTB_0, val);
903         }
906 static void vip_set_slice_path(struct vip_dev *dev,
907                                enum data_path_select data_path)
909         u32 val = 0;
911         switch (data_path) {
912         case VIP_MULTI_CHANNEL_DATA_SELECT:
913                 if (dev->slice_id == VIP_SLICE1) {
914                         val |= VIP_MULTI_CHANNEL_SELECT;
915                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
916                                      VIP_DATAPATH_SELECT_SHFT);
918                         write_vreg(dev, VIP_VIP1_DATA_PATH_SELECT, val);
919                 } else if (dev->slice_id == VIP_SLICE2) {
920                         val |= VIP_MULTI_CHANNEL_SELECT;
921                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
922                                      VIP_DATAPATH_SELECT_SHFT);
924                         write_vreg(dev, VIP_VIP2_DATA_PATH_SELECT, val);
925                 }
926                 break;
927         case VIP_RGB_OUT_LO_DATA_SELECT:
928                 if (dev->slice_id == VIP_SLICE1) {
929                         val |= VIP_RGB_OUT_LO_SRC_SELECT;
930                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
931                                      VIP_DATAPATH_SELECT_SHFT);
933                         write_vreg(dev, VIP_VIP1_DATA_PATH_SELECT, val);
934                 } else if (dev->slice_id == VIP_SLICE2) {
935                         val |= VIP_RGB_OUT_LO_SRC_SELECT;
936                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
937                                      VIP_DATAPATH_SELECT_SHFT);
939                         write_vreg(dev, VIP_VIP2_DATA_PATH_SELECT, val);
940                 }
941                 break;
942         default:
943                 BUG();
944         }
947 /*
948  * Return the vip_stream structure for a given struct file
949  */
950 static inline struct vip_stream *file2stream(struct file *file)
952         return video_drvdata(file);
956 /*
957  * Append a destination descriptor to the current descriptor list,
958  * setting up dma to the given srce.
959  */
960 static int add_out_dtd(struct vip_stream *stream, int srce_type)
962         struct vip_port *port = stream->port;
963         struct vip_dev *dev = port->dev;
964         struct vip_srce_info *sinfo = &srce_info[srce_type];
965         struct v4l2_rect *c_rect = &port->c_rect;
966         struct vip_fmt *fmt = port->fmt;
967         int channel, plane = 0;
968         int max_width, max_height;
969         dma_addr_t dma_addr;
970         u32 flags;
972         channel = sinfo->base_channel;
974         switch (srce_type) {
975         case VIP_SRCE_MULT_PORT:
976         case VIP_SRCE_MULT_ANC:
977                 if (port->port_id == VIP_PORTB)
978                         channel += VIP_CHAN_MULT_PORTB_OFFSET;
979                 channel += stream->stream_id;
980                 flags = 0;
981                 break;
982         case VIP_SRCE_CHROMA:
983                 plane = 1;
984         case VIP_SRCE_LUMA:
985                 if (port->port_id == VIP_PORTB)
986                         channel += VIP_CHAN_YUV_PORTB_OFFSET;
987                 flags = port->flags;
988                 break;
989         case VIP_SRCE_RGB:
990                 if (port->port_id == VIP_PORTB)
991                         channel += VIP_CHAN_RGB_PORTB_OFFSET;
992                 flags = port->flags;
993                 break;
994         default:
995                 BUG();
996         }
998         if (dev->slice_id == VIP_SLICE2)
999                 channel += VIP_CHAN_VIP2_OFFSET;
1001         /* This is just for initialization purposes.
1002          * The actual dma_addr will be configured in vpdma_update_dma_addr
1003          */
1004         dma_addr = (dma_addr_t)NULL;
1006         /*
1007          * Use VPDMA_MAX_SIZE1 or VPDMA_MAX_SIZE2 register for slice0/1
1008          */
1010         if (dev->slice_id == VIP_SLICE1) {
1011                 vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE1,
1012                         stream->width, stream->height);
1014                 max_width = MAX_OUT_WIDTH_REG1;
1015                 max_height = MAX_OUT_HEIGHT_REG1;
1016         } else {
1017                 vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE2,
1018                         stream->width, stream->height);
1020                 max_width = MAX_OUT_WIDTH_REG2;
1021                 max_height = MAX_OUT_HEIGHT_REG2;
1022         }
1024         /* Mark this channel to be cleared while cleaning up resources
1025          * This will make sure that an abort descriptor for this channel
1026          * would be submitted to VPDMA causing any ongoing  transaction to be
1027          * aborted and cleanup the VPDMA FSM for this channel */
1028         stream->vpdma_channels[channel] = 1;
1030         vpdma_rawchan_add_out_dtd(&stream->desc_list, c_rect->width, c_rect,
1031                 fmt->vpdma_fmt[plane], dma_addr, max_width, max_height,
1032                 channel, flags);
1034         return 0;
1037 /*
1038  * add_stream_dtds - prepares and starts DMA for pending transfers
1039  */
1040 static void add_stream_dtds(struct vip_stream *stream)
1042         struct vip_port *port = stream->port;
1043         int srce_type;
1045         if (port->flags & FLAG_MULT_PORT)
1046                 srce_type = VIP_SRCE_MULT_PORT;
1047         else if (port->flags & FLAG_MULT_ANC)
1048                 srce_type = VIP_SRCE_MULT_ANC;
1049         else if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
1050                 srce_type = VIP_SRCE_RGB;
1051         else
1052                 srce_type = VIP_SRCE_LUMA;
1054         add_out_dtd(stream, srce_type);
1056         if (srce_type == VIP_SRCE_LUMA && port->fmt->coplanar)
1057                 add_out_dtd(stream, VIP_SRCE_CHROMA);
1060 static void enable_irqs(struct vip_dev *dev, int irq_num, int list_num)
1062         u32 reg_addr = VIP_INT0_ENABLE0_SET +
1063                         VIP_INTC_INTX_OFFSET * irq_num;
1065         write_sreg(dev->shared, reg_addr, 1 << (list_num * 2));
1067         vpdma_enable_list_complete_irq(dev->shared->vpdma,
1068                 irq_num, list_num, true);
1071 static void disable_irqs(struct vip_dev *dev, int irq_num, int list_num)
1073         u32 reg_addr = VIP_INT0_ENABLE0_CLR +
1074                         VIP_INTC_INTX_OFFSET * irq_num;
1076         write_sreg(dev->shared, reg_addr, 1 << (list_num * 2));
1078         vpdma_enable_list_complete_irq(dev->shared->vpdma,
1079                 irq_num, list_num, false);
1082 static void clear_irqs(struct vip_dev *dev, int irq_num, int list_num)
1084         u32 reg_addr = VIP_INT0_STATUS0_CLR +
1085                         VIP_INTC_INTX_OFFSET * irq_num;
1087         write_sreg(dev->shared, reg_addr, 1 << (list_num * 2));
1089         vpdma_clear_list_stat(dev->shared->vpdma, irq_num, dev->slice_id);
1092 static void populate_desc_list(struct vip_stream *stream)
1094         struct vip_port *port = stream->port;
1095         struct vip_dev *dev = port->dev;
1096         unsigned int list_length;
1098         stream->desc_next = stream->desc_list.buf.addr;
1099         add_stream_dtds(stream);
1101         list_length = stream->desc_next - stream->desc_list.buf.addr;
1102         vpdma_map_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
1105 /*
1106  * start_dma - adds descriptors to the dma list and submits them.
1107  * Should be called after a new vb is queued and on a vpdma list
1108  * completion interrupt.
1109  */
1110 static void start_dma(struct vip_stream *stream, struct vip_buffer *buf)
1112         struct vip_dev *dev = stream->port->dev;
1113         struct vpdma_data *vpdma = dev->shared->vpdma;
1114         int list_num = stream->list_num;
1115         dma_addr_t dma_addr;
1116         int drop_data;
1118         if (vpdma_list_busy(vpdma, list_num)) {
1119                 vip_err(dev, "vpdma list busy, cannot post");
1120                 return;                         /* nothing to do */
1121         }
1123         if (buf) {
1124                 dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vb, 0);
1125                 drop_data = 0;
1126                 vip_dbg(4, dev, "start_dma: buf:0x%08x, vb:0x%08x, dma_addr:0x%08x\n",
1127                         (unsigned int)buf, (unsigned int)&buf->vb, dma_addr);
1128         } else {
1129                 dma_addr = (dma_addr_t)NULL;
1130                 drop_data = 1;
1131                 vip_dbg(4, dev, "start_dma: dropped\n");
1132         }
1134         vpdma_update_dma_addr(dev->shared->vpdma, &stream->desc_list,
1135                               dma_addr, stream->write_desc, drop_data, 0);
1137         if (stream->port->fmt->coplanar) {
1138                 dma_addr += stream->width * stream->height;
1139                 vpdma_update_dma_addr(dev->shared->vpdma, &stream->desc_list,
1140                               dma_addr, stream->write_desc + 1, drop_data, 1);
1141         }
1143         vpdma_submit_descs(dev->shared->vpdma,
1144                         &stream->desc_list, stream->list_num);
1147 static void vip_schedule_next_buffer(struct vip_stream *stream)
1149         struct vip_dev *dev = stream->port->dev;
1150         struct vip_buffer *buf;
1151         unsigned long flags;
1153         spin_lock_irqsave(&dev->slock, flags);
1154         if (list_empty(&stream->vidq)) {
1155                 vip_dbg(4, dev, "Dropping frame\n");
1156                 if (list_empty(&stream->dropq)) {
1157                         vip_err(dev, "No dropq buffer left!");
1158                         spin_unlock_irqrestore(&dev->slock, flags);
1159                         return;
1160                 }
1161                 buf = list_entry(stream->dropq.next,
1162                                  struct vip_buffer, list);
1164                 buf->drop = true;
1165                 list_move_tail(&buf->list, &stream->post_bufs);
1166                 buf = NULL;
1167         } else if (vb2_is_streaming(&stream->vb_vidq)) {
1168                 buf = list_entry(stream->vidq.next,
1169                                 struct vip_buffer, list);
1170                 buf->drop = false;
1171                 list_move_tail(&buf->list, &stream->post_bufs);
1172                 vip_dbg(4, dev, "added next buffer\n");
1173         } else {
1174                 vip_err(dev, "IRQ occurred when not streaming\n");
1175                 if (list_empty(&stream->dropq)) {
1176                         vip_err(dev, "No dropq buffer left!");
1177                         spin_unlock_irqrestore(&dev->slock, flags);
1178                         return;
1179                 }
1180                 buf = list_entry(stream->dropq.next,
1181                                  struct vip_buffer, list);
1182                 buf->drop = true;
1183                 list_move_tail(&buf->list, &stream->post_bufs);
1184                 buf = NULL;
1185         }
1187         spin_unlock_irqrestore(&dev->slock, flags);
1188         start_dma(stream, buf);
1191 static void vip_process_buffer_complete(struct vip_stream *stream)
1193         struct vip_dev *dev = stream->port->dev;
1194         struct vip_buffer *buf;
1195         struct vb2_buffer *vb = NULL;
1196         unsigned long flags, fld;
1198         buf = list_first_entry(&stream->post_bufs, struct vip_buffer, list);
1200         if (stream->port->flags & FLAG_INTERLACED) {
1201                 vpdma_unmap_desc_buf(dev->shared->vpdma,
1202                                         &stream->desc_list.buf);
1204                 fld = dtd_get_field(stream->write_desc);
1205                 stream->field = fld ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
1207                 vpdma_map_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
1208         }
1210         if (buf) {
1211                 vip_dbg(4, dev, "vip buffer complete 0x%x, 0x%x\n",
1212                         (unsigned int)buf, buf->drop);
1214                 vb = &buf->vb;
1215                 vb->v4l2_buf.field = stream->field;
1216                 vb->v4l2_buf.sequence = stream->sequence;
1217                 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
1219                 if (buf->drop) {
1220                         spin_lock_irqsave(&dev->slock, flags);
1221                         list_move_tail(&buf->list, &stream->dropq);
1222                         spin_unlock_irqrestore(&dev->slock, flags);
1223                 } else {
1224                         spin_lock_irqsave(&dev->slock, flags);
1225                         list_del(&buf->list);
1226                         spin_unlock_irqrestore(&dev->slock, flags);
1227                         vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1228                 }
1229         } else {
1230                 BUG();
1231         }
1233         stream->sequence++;
1236 static irqreturn_t vip_irq(int irq_vip, void *data)
1238         struct vip_dev *dev = (struct vip_dev *)data;
1239         struct vpdma_data *vpdma = dev->shared->vpdma;
1240         struct vip_stream *stream;
1241         int list_num;
1242         int irq_num = dev->slice_id;
1243         u32 irqst, reg_addr;
1245         if (!dev->shared)
1246                 return IRQ_HANDLED;
1248         reg_addr = VIP_INT0_STATUS0 +
1249                         VIP_INTC_INTX_OFFSET * irq_num;
1250         irqst = read_sreg(dev->shared, reg_addr);
1252         vip_dbg(8, dev, "IRQ %d VIP_INT%d_STATUS0 0x%x\n",
1253                 irq_vip, irq_num, irqst);
1254         if (irqst) {
1255                 reg_addr = VIP_INT0_STATUS0_CLR +
1256                         VIP_INTC_INTX_OFFSET * irq_num;
1257                 write_sreg(dev->shared, reg_addr, irqst);
1259                 for (list_num = 0; list_num < 8;  list_num++) {
1260                         /* Check for LIST_COMPLETE IRQ */
1261                         if (!(irqst & (1 << list_num * 2)))
1262                                 continue;
1264                         vip_dbg(8, dev, "IRQ %d: handling LIST%d_COMPLETE\n",
1265                                 irq_num, list_num);
1267                         stream = vpdma_hwlist_get_priv(vpdma, list_num);
1268                         if (!stream || stream->list_num != list_num) {
1269                                 vip_err(dev, "IRQ occured for unused list");
1270                                 continue;
1271                         }
1273                         vpdma_clear_list_stat(vpdma, irq_num, list_num);
1275                         if (dev->num_skip_irq)
1276                                 dev->num_skip_irq--;
1277                         else
1278                                 vip_process_buffer_complete(stream);
1280                         vip_schedule_next_buffer(stream);
1281                         irqst &= ~((1 << list_num * 2));
1282                 }
1283         }
1285         return IRQ_HANDLED;
1288 /*
1289  * video ioctls
1290  */
1291 static struct v4l2_mbus_framefmt *
1292 vip_video_pix_to_mbus(const struct v4l2_pix_format *pix,
1293                       struct v4l2_mbus_framefmt *mbus)
1295         unsigned int i;
1297         memset(mbus, 0, sizeof(*mbus));
1298         mbus->width = pix->width;
1299         mbus->height = pix->height;
1301         mbus->code = V4L2_MBUS_FMT_YUYV8_2X8;
1302         for (i = 0; i < ARRAY_SIZE(vip_formats) - 1; ++i) {
1303                 if (vip_formats[i].fourcc == pix->pixelformat) {
1304                         mbus->code = vip_formats[i].code;
1305                         break;
1306                 }
1307         }
1309         mbus->colorspace = pix->colorspace;
1310         mbus->field = pix->field;
1312         return mbus;
1315 static int vip_querycap(struct file *file, void *priv,
1316                         struct v4l2_capability *cap)
1318         strncpy(cap->driver, VIP_MODULE_NAME, sizeof(cap->driver) - 1);
1319         strncpy(cap->card, VIP_MODULE_NAME, sizeof(cap->card) - 1);
1320         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1321                 VIP_MODULE_NAME);
1322         cap->device_caps  = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
1323         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1324         return 0;
1327 static int vip_enuminput(struct file *file, void *priv,
1328                                 struct v4l2_input *inp)
1330         struct vip_stream *stream = file2stream(file);
1332         if (inp->index)
1333                 return -EINVAL;
1335         inp->type = V4L2_INPUT_TYPE_CAMERA;
1336         sprintf(inp->name, "camera %u", stream->vfd->num);
1338         return 0;
1341 static int vip_g_input(struct file *file, void *priv, unsigned int *i)
1343         *i = 0;
1344         return 0;
1347 static int vip_s_input(struct file *file, void *priv, unsigned int i)
1349         if (i != 0)
1350                 return -EINVAL;
1351         return 0;
1354 static int vip_querystd(struct file *file, void *fh, v4l2_std_id *std)
1356         struct vip_stream *stream = file2stream(file);
1357         struct vip_dev *dev = stream->port->dev;
1359         v4l2_subdev_call(dev->sensor, video, querystd, std);
1360         return 0;
1363 static int vip_g_std(struct file *file, void *fh, v4l2_std_id *std)
1365         struct vip_stream *stream = file2stream(file);
1366         struct vip_dev *dev = stream->port->dev;
1368         *std = 0;
1369         v4l2_subdev_call(dev->sensor, video, g_std_output, std);
1370         return 0;
1373 static int vip_s_std(struct file *file, void *fh, v4l2_std_id std)
1375         struct vip_stream *stream = file2stream(file);
1376         struct vip_dev *dev = stream->port->dev;
1378         v4l2_subdev_call(dev->sensor, video, s_std_output, std);
1379         return 0;
1382 static int vip_enum_fmt_vid_cap(struct file *file, void *priv,
1383                                 struct v4l2_fmtdesc *f)
1385         struct vip_stream *stream = file2stream(file);
1386         struct vip_dev *dev = stream->port->dev;
1387         struct vip_fmt *fmt;
1389         vip_dbg(3, dev, "enum_fmt index:%d\n", f->index);
1390         if (f->index >= dev->num_active_fmt)
1391                 return -EINVAL;
1393         fmt = dev->active_fmt[f->index];
1395         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
1396         f->pixelformat = fmt->fourcc;
1398         vip_dbg(3, dev, "enum_fmt fourcc:%s description:%s\n",
1399                 fourcc_to_str(f->pixelformat), f->description);
1401         return 0;
1404 /*
1405   * TODO: Change from hard coding values to reading these through
1406   * IOCTLS directly from sensor
1407   */
1409 static int vip_enum_framesizes(struct file *file, void *priv,
1410                         struct v4l2_frmsizeenum *f)
1412         struct vip_stream *stream = file2stream(file);
1413         struct vip_dev *dev = stream->port->dev;
1414         struct vip_fmt *fmt;
1415         int ret;
1417         fmt = find_active_format_by_pix(dev, f->pixel_format);
1418         if (!fmt)
1419                 return -EINVAL;
1421         ret = v4l2_subdev_call(dev->sensor, video, enum_framesizes, f);
1422         if (ret)
1423                 vip_dbg(1, dev, "enum_framesizes failed in subdev\n");
1425         return ret;
1428 static int vip_enum_frameintervals(struct file *file, void *priv,
1429                         struct v4l2_frmivalenum *f)
1431         struct vip_stream *stream = file2stream(file);
1432         struct vip_dev *dev = stream->port->dev;
1433         struct v4l2_frmsizeenum fsize;
1434         struct vip_fmt *fmt;
1435         int ret;
1437         if (f->index)
1438                 return -EINVAL;
1440         fmt = find_active_format_by_pix(dev, f->pixel_format);
1441         if (!fmt)
1442                 return -EINVAL;
1444         /* check for valid width/height */
1445         ret = 0;
1446         for (fsize.index = 0; ; fsize.index++) {
1447                 ret = v4l2_subdev_call(dev->sensor, video,
1448                                         enum_framesizes, &fsize);
1449                 if (ret) {
1450                         if (fsize.index == 0)
1451                                 vip_dbg(1, dev, "enum_frameinterval failed on the first enum_framesize\n");
1452                         return -EINVAL;
1453                 }
1455                 if (fsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1456                         if ((f->width == fsize.discrete.width) &&
1457                             (f->height == fsize.discrete.height))
1458                                 break;
1459                 } else if ((fsize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) ||
1460                            (fsize.type == V4L2_FRMSIZE_TYPE_STEPWISE)) {
1461                         if ((f->width >= fsize.stepwise.min_width) &&
1462                             (f->width <= fsize.stepwise.max_width) &&
1463                             (f->height >= fsize.stepwise.min_height) &&
1464                             (f->height <= fsize.stepwise.max_height))
1465                                 break;
1466                 } else
1467                         return -EINVAL;
1468         }
1470         f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1471         f->discrete.numerator = 1;
1472         f->discrete.denominator = 30;
1474         return 0;
1477 static int vip_s_parm(struct file *file, void *priv,
1478                         struct v4l2_streamparm *parm)
1480         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1481                 return -EINVAL;
1483         parm->parm.capture.timeperframe.numerator = 1;
1484         parm->parm.capture.timeperframe.denominator = 30;
1486         return 0;
1489 static int vip_try_fmt_vid_cap(struct file *file, void *priv,
1490                                struct v4l2_format *f)
1492         struct vip_stream *stream = file2stream(file);
1493         struct vip_dev *dev = stream->port->dev;
1494         struct vip_fmt *fmt = find_active_format_by_pix(dev,
1495                                                         f->fmt.pix.pixelformat);
1496         enum v4l2_field field;
1498         if (!fmt) {
1499                 vip_err(dev,
1500                         "Fourcc format (0x%08x) invalid.\n",
1501                         f->fmt.pix.pixelformat);
1502                 return -EINVAL;
1503         }
1505         field = f->fmt.pix.field;
1507         if (field == V4L2_FIELD_ANY)
1508                 field = V4L2_FIELD_NONE;
1509         else if (V4L2_FIELD_NONE != field && V4L2_FIELD_ALTERNATE != field)
1510                 return -EINVAL;
1512         f->fmt.pix.field = field;
1514         v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W, W_ALIGN,
1515                               &f->fmt.pix.height, MIN_H, MAX_H, H_ALIGN,
1516                               S_ALIGN);
1518         f->fmt.pix.bytesperline = f->fmt.pix.width *
1519                                   (fmt->vpdma_fmt[0]->depth >> 3);
1520         f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
1521                                         VPDMA_STRIDE_ALIGN);
1522         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.width *
1523                 (fmt->vpdma_fmt[0]->depth +
1524                  (fmt->coplanar ? fmt->vpdma_fmt[1]->depth : 0)) >> 3;
1525         f->fmt.pix.colorspace = fmt->colorspace;
1526         f->fmt.pix.priv = 0;
1528         vip_dbg(3, dev, "try_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1529                 fourcc_to_str(f->fmt.pix.pixelformat),
1530                 f->fmt.pix.width, f->fmt.pix.height,
1531                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1533         return 0;
1536 static int vip_g_fmt_vid_cap(struct file *file, void *priv,
1537                              struct v4l2_format *f)
1539         struct vip_stream *stream = file2stream(file);
1540         struct vip_port *port = stream->port;
1541         struct vip_dev *dev = stream->port->dev;
1542         struct v4l2_mbus_framefmt mbus_fmt;
1543         struct vip_fmt *fmt;
1544         struct v4l2_format try_f;
1545         int ret;
1547         /* Use last known values or defaults */
1548         f->fmt.pix.width        = stream->width;
1549         f->fmt.pix.height       = stream->height;
1550         f->fmt.pix.pixelformat  = port->fmt->fourcc;
1551         f->fmt.pix.field        = stream->sup_field;
1552         f->fmt.pix.colorspace   = port->fmt->colorspace;
1553         f->fmt.pix.bytesperline = stream->bytesperline;
1554         f->fmt.pix.sizeimage    = stream->sizeimage;
1556         /* Check with the subdevice */
1557         ret = v4l2_subdev_call(dev->sensor, video, g_mbus_fmt, &mbus_fmt);
1558         if (ret)
1559                 vip_dbg(1, dev, "g_mbus_fmt failed in subdev\n");
1561         fmt = find_active_format_by_code(dev, mbus_fmt.code);
1562         if (!fmt) {
1563                 vip_err(dev,
1564                         "mbus_code (0x%08x) invalid.\n",
1565                         mbus_fmt.code);
1566                 return -EINVAL;
1567         }
1569         vip_dbg(3, dev, "g_fmt subdev mbus_code: %04X fourcc:%s size: %dx%d\n",
1570                 fmt->code,
1571                 fourcc_to_str(fmt->fourcc),
1572                 mbus_fmt.width, mbus_fmt.height);
1574         /*
1575          * Run a try_fmt call to properly calculate
1576          * the sizeimage and bytesperline values
1577          * in case the defaults were not accurate.
1578          */
1579         try_f = *f;
1580         try_f.fmt.pix.pixelformat = fmt->fourcc;
1581         try_f.fmt.pix.width = mbus_fmt.width;
1582         try_f.fmt.pix.height = mbus_fmt.height;
1583         try_f.fmt.pix.field = mbus_fmt.field;
1584         try_f.fmt.pix.colorspace = mbus_fmt.colorspace;
1586         ret = vip_try_fmt_vid_cap(file, priv, &try_f);
1587         if (ret)
1588                 return ret;
1590         if (port->fmt != fmt) {
1591                 vip_dbg(1, dev, "g_fmt fmt mismatch port->fmt:%p fmt:%p\n",
1592                         port->fmt, fmt);
1593                 vip_dbg(1, dev, "g_fmt port->fmt->fourcc:%s\n",
1594                         fourcc_to_str(port->fmt->fourcc));
1595                 vip_dbg(1, dev, "fmt->fourcc:%s\n",
1596                         fourcc_to_str(fmt->fourcc));
1597                 vip_dbg(1, dev, "g_fmt port->fmt->name:%s fmt->name:%s\n",
1598                         port->fmt->name, fmt->name);
1599                 port->fmt = fmt;
1600         }
1601         /*
1602          * Since everything looks correct update
1603          * the local copy as well to make sure we are consistent
1604          */
1605         *f = try_f;
1606         stream->width = f->fmt.pix.width;
1607         stream->height = f->fmt.pix.height;
1608         stream->sup_field = f->fmt.pix.field;
1609         if (stream->sup_field == V4L2_FIELD_ALTERNATE)
1610                 port->flags |= FLAG_INTERLACED;
1611         else
1612                 port->flags &= ~FLAG_INTERLACED;
1613         stream->bytesperline = f->fmt.pix.bytesperline;
1614         stream->sizeimage = f->fmt.pix.sizeimage;
1615         port->c_rect.left       = 0;
1616         port->c_rect.top        = 0;
1617         port->c_rect.width      = stream->width;
1618         port->c_rect.height     = stream->height;
1620         vip_dbg(3, dev, "g_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1621                 fourcc_to_str(f->fmt.pix.pixelformat),
1622                 f->fmt.pix.width, f->fmt.pix.height,
1623                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1625         return 0;
1628 /*
1629  * Set the registers that are modified when the video format changes.
1630  */
1631 static void set_fmt_params(struct vip_stream *stream)
1633         struct vip_dev *dev = stream->port->dev;
1634         int data_path_reg;
1636         stream->sequence = 0;
1637         stream->field = V4L2_FIELD_TOP;
1639         if (stream->port->fmt->colorspace == V4L2_COLORSPACE_SRGB) {
1640                 vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT);
1641                 /* Set alpha component in background color */
1642                 vpdma_set_bg_color(dev->shared->vpdma,
1643                         (struct vpdma_data_format *)
1644                          stream->port->fmt->vpdma_fmt[0],
1645                         0xff);
1646         }
1648         data_path_reg = VIP_VIP1_DATA_PATH_SELECT + 4 * dev->slice_id;
1649         if (stream->port->fmt->coplanar) {
1650                 stream->port->flags &= ~FLAG_MULT_PORT;
1651                 write_vreg(dev, data_path_reg, 0x600);
1652         } else {
1653                 stream->port->flags |= FLAG_MULT_PORT;
1654                 write_vreg(dev, data_path_reg, 0x8000);
1655         }
1658 int vip_s_fmt_vid_cap(struct file *file, void *priv,
1659                              struct v4l2_format *f)
1661         struct vip_stream *stream = file2stream(file);
1662         struct vip_port *port = stream->port;
1663         struct vip_dev *dev = port->dev;
1664         struct v4l2_subdev_format sfmt;
1665         struct v4l2_mbus_framefmt *mf;
1666         int ret;
1668         vip_dbg(3, dev, "s_fmt input fourcc:%s size: %dx%d\n",
1669                 fourcc_to_str(f->fmt.pix.pixelformat),
1670                 f->fmt.pix.width, f->fmt.pix.height);
1672         ret = vip_try_fmt_vid_cap(file, priv, f);
1673         if (ret)
1674                 return ret;
1676         vip_dbg(3, dev, "s_fmt try_fmt fourcc:%s size: %dx%d\n",
1677                 fourcc_to_str(f->fmt.pix.pixelformat),
1678                 f->fmt.pix.width, f->fmt.pix.height);
1680         if (vb2_is_busy(&stream->vb_vidq)) {
1681                 vip_err(dev, "%s queue busy\n", __func__);
1682                 return -EBUSY;
1683         }
1685         port->fmt               = find_active_format_by_pix(dev,
1686                                         f->fmt.pix.pixelformat);
1687         stream->width           = f->fmt.pix.width;
1688         stream->height          = f->fmt.pix.height;
1689         if (port->fmt->colorspace != f->fmt.pix.colorspace)
1690                 vip_dbg(1, dev, "s_fmt colorspace mismatch port->fmt %d f->fmt %d\n",
1691                         port->fmt->colorspace, f->fmt.pix.colorspace);
1693         stream->bytesperline    = f->fmt.pix.bytesperline;
1694         stream->sizeimage       = f->fmt.pix.sizeimage;
1695         stream->sup_field       = f->fmt.pix.field;
1697         port->c_rect.left       = 0;
1698         port->c_rect.top        = 0;
1699         port->c_rect.width      = stream->width;
1700         port->c_rect.height     = stream->height;
1702         if (stream->sup_field == V4L2_FIELD_ALTERNATE)
1703                 port->flags |= FLAG_INTERLACED;
1704         else
1705                 port->flags &= ~FLAG_INTERLACED;
1707         vip_dbg(1, dev,
1708                 "Setting format for type %d, wxh: %dx%d, fourcc:%s\n",
1709                 f->type, stream->width, stream->height,
1710                 fourcc_to_str(port->fmt->fourcc));
1712         vip_dbg(3, dev, "s_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1713                 fourcc_to_str(f->fmt.pix.pixelformat),
1714                 f->fmt.pix.width, f->fmt.pix.height,
1715                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1717         mf = vip_video_pix_to_mbus(&f->fmt.pix, &sfmt.format);
1719         vip_dbg(3, dev, "s_fmt pix_to_mbus mbus_code: %04X size: %dx%d\n",
1720                 mf->code,
1721                 mf->width, mf->height);
1723         ret = v4l2_subdev_call(dev->sensor, video, try_mbus_fmt, mf);
1724         if (ret) {
1725                 vip_dbg(1, dev, "try_mbus_fmt failed in subdev\n");
1726                 return ret;
1727         }
1728         vip_dbg(3, dev, "s_fmt subdev try_fmt mbus_code: %04X size: %dx%d\n",
1729                 mf->code,
1730                 mf->width, mf->height);
1731         ret = v4l2_subdev_call(dev->sensor, video, s_mbus_fmt, mf);
1732         if (ret) {
1733                 vip_dbg(1, dev, "s_mbus_fmt failed in subdev\n");
1734                 return ret;
1735         }
1736         vip_dbg(3, dev, "s_fmt subdev s_fmt mbus_code: %04X size: %dx%d\n",
1737                 mf->code,
1738                 mf->width, mf->height);
1740         return 0;
1742 EXPORT_SYMBOL(vip_s_fmt_vid_cap);
1744 static int vip_g_selection(struct file *file, void *fh,
1745                            struct v4l2_selection *s)
1747         struct vip_stream *stream = file2stream(file);
1749         switch (s->target) {
1750         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1751         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1752         case V4L2_SEL_TGT_CROP_BOUNDS:
1753         case V4L2_SEL_TGT_CROP_DEFAULT:
1754                 s->r.left = 0;
1755                 s->r.top = 0;
1756                 s->r.width = stream->width;
1757                 s->r.height = stream->height;
1758                 return 0;
1760         case V4L2_SEL_TGT_COMPOSE:
1761         case V4L2_SEL_TGT_CROP:
1762                 s->r = stream->port->c_rect;
1763                 return 0;
1764         }
1766         return -EINVAL;
1769 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1771         if (a->left < b->left || a->top < b->top)
1772                 return 0;
1773         if (a->left + a->width > b->left + b->width)
1774                 return 0;
1775         if (a->top + a->height > b->top + b->height)
1776                 return 0;
1778         return 1;
1781 static int vip_s_selection(struct file *file, void *fh,
1782                            struct v4l2_selection *s)
1784         struct vip_stream *stream = file2stream(file);
1785         struct vip_port *port = stream->port;
1786         struct v4l2_rect r = s->r;
1788         v4l_bound_align_image(&r.width, 0, stream->width, 0,
1789                               &r.height, 0, stream->height, 0, 0);
1791         r.left = clamp_t(unsigned int, r.left, 0, stream->width - r.width);
1792         r.top  = clamp_t(unsigned int, r.top, 0, stream->height - r.height);
1794         if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
1795                 return -ERANGE;
1797         if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
1798                 return -ERANGE;
1800         s->r = stream->port->c_rect = r;
1802         set_fmt_params(stream);
1804         vip_dbg(1, port->dev, "cropped (%d,%d)/%dx%d of %dx%d\n",
1805                 r.left, r.top, r.width, r.height,
1806                 stream->width, stream->height);
1808         return 0;
1811 static long vip_ioctl_default(struct file *file, void *fh, bool valid_prio,
1812                               unsigned int cmd, void *arg)
1814         struct vip_stream *stream = file2stream(file);
1815         struct vip_port *port = stream->port;
1817         if (!valid_prio) {
1818                 vip_err(port->dev, "%s device busy\n", __func__);
1819                 return -EBUSY;
1820         }
1822         switch (cmd) {
1823         default:
1824                 return -ENOTTY;
1825         }
1828 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
1829         .vidioc_querycap        = vip_querycap,
1830         .vidioc_enum_input      = vip_enuminput,
1831         .vidioc_g_input         = vip_g_input,
1832         .vidioc_s_input         = vip_s_input,
1834         .vidioc_querystd        = vip_querystd,
1835         .vidioc_g_std           = vip_g_std,
1836         .vidioc_s_std           = vip_s_std,
1838         .vidioc_enum_fmt_vid_cap = vip_enum_fmt_vid_cap,
1839         .vidioc_g_fmt_vid_cap   = vip_g_fmt_vid_cap,
1840         .vidioc_try_fmt_vid_cap = vip_try_fmt_vid_cap,
1841         .vidioc_s_fmt_vid_cap   = vip_s_fmt_vid_cap,
1843         .vidioc_enum_frameintervals     = vip_enum_frameintervals,
1844         .vidioc_enum_framesizes         = vip_enum_framesizes,
1845         .vidioc_s_parm                  = vip_s_parm,
1847         .vidioc_g_selection     = vip_g_selection,
1848         .vidioc_s_selection     = vip_s_selection,
1849         .vidioc_reqbufs         = vb2_ioctl_reqbufs,
1850         .vidioc_create_bufs     = vb2_ioctl_create_bufs,
1851         .vidioc_prepare_buf     = vb2_ioctl_prepare_buf,
1852         .vidioc_querybuf        = vb2_ioctl_querybuf,
1853         .vidioc_qbuf            = vb2_ioctl_qbuf,
1854         .vidioc_dqbuf           = vb2_ioctl_dqbuf,
1856         .vidioc_streamon        = vb2_ioctl_streamon,
1857         .vidioc_streamoff       = vb2_ioctl_streamoff,
1858         .vidioc_log_status      = v4l2_ctrl_log_status,
1859         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1860         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1861         .vidioc_default         = vip_ioctl_default,
1862 };
1864 /*
1865  * Videobuf operations
1866  */
1867 static int vip_queue_setup(struct vb2_queue *vq,
1868                            const struct v4l2_format *fmt,
1869                            unsigned int *nbuffers, unsigned int *nplanes,
1870                            unsigned int sizes[], void *alloc_ctxs[])
1872         struct vip_stream *stream = vb2_get_drv_priv(vq);
1873         struct vip_dev *dev = stream->port->dev;
1875         *nplanes = 1;
1876         sizes[0] = stream->sizeimage;
1877         alloc_ctxs[0] = dev->alloc_ctx;
1878         vip_dbg(1, dev, "get %d buffer(s) of size %d each.\n",
1879                 *nbuffers, sizes[0]);
1881         return 0;
1884 static int vip_buf_prepare(struct vb2_buffer *vb)
1886         struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
1887         struct vip_dev *dev = stream->port->dev;
1889         if (vb2_plane_size(vb, 0) < stream->sizeimage) {
1890                 vip_dbg(1, dev,
1891                         "%s data will not fit into plane (%lu < %lu)\n",
1892                         __func__, vb2_plane_size(vb, 0),
1893                         (long)stream->sizeimage);
1894                 return -EINVAL;
1895         }
1897         vb2_set_plane_payload(vb, 0, stream->sizeimage);
1899         return 0;
1902 static void vip_buf_queue(struct vb2_buffer *vb)
1904         struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
1905         struct vip_dev *dev = stream->port->dev;
1906         struct vip_buffer *buf = container_of(vb, struct vip_buffer, vb);
1907         unsigned long flags;
1909         spin_lock_irqsave(&dev->slock, flags);
1910         list_add_tail(&buf->list, &stream->vidq);
1911         spin_unlock_irqrestore(&dev->slock, flags);
1914 static int vip_start_streaming(struct vb2_queue *vq, unsigned int count)
1916         struct vip_stream *stream = vb2_get_drv_priv(vq);
1917         struct vip_port *port = stream->port;
1918         struct vip_dev *dev = port->dev;
1919         struct vip_buffer *buf;
1920         unsigned long flags;
1921         int ret;
1923         set_fmt_params(stream);
1924         vip_setup_parser(dev->ports[0]);
1926         buf = list_entry(stream->vidq.next,
1927                          struct vip_buffer, list);
1929         vip_dbg(2, dev, "start_streaming: buf 0x%x %d\n",
1930                 (unsigned int)buf, count);
1931         buf->drop = false;
1932         stream->sequence = 0;
1933         stream->field = V4L2_FIELD_TOP;
1935         if (dev->sensor) {
1936                 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1937                 if (ret) {
1938                         vip_dbg(1, dev, "stream on failed in subdev\n");
1939                         return ret;
1940                 }
1941         }
1943         populate_desc_list(stream);
1945         /* The first few VPDMA ListComplete interrupts fire pretty quiclky
1946          * until the internal VPDMA descriptor fifo is full.
1947          * The subsequent ListComplete interrupts will fire at the actual
1948          * capture frame rate. The first few interrupts are therefore used
1949          * only to queue up descriptors, and then they will also be used
1950          * as End of Frame (EOF) event
1951          */
1952         dev->num_skip_irq = VIP_VPDMA_FIFO_SIZE;
1954         spin_lock_irqsave(&dev->slock, flags);
1955         if (vpdma_list_busy(dev->shared->vpdma, stream->list_num)) {
1956                 spin_unlock_irqrestore(&dev->slock, flags);
1957                 vpdma_unmap_desc_buf(dev->shared->vpdma,
1958                                 &stream->desc_list.buf);
1959                 vpdma_reset_desc_list(&stream->desc_list);
1960                 return -EBUSY;
1961         }
1963         list_move_tail(&buf->list, &stream->post_bufs);
1964         spin_unlock_irqrestore(&dev->slock, flags);
1966         vip_dbg(2, dev, "start_streaming: start_dma buf 0x%x\n",
1967                 (unsigned int)buf);
1968         start_dma(stream, buf);
1970         /* We enable the irq after posting the vpdma descriptor
1971          * to prevent sprurious interrupt coming in before the
1972          * vb2 layer is completely ready to handle them
1973          * otherwise the vb2_streaming test would fail early on
1974           */
1975         enable_irqs(dev, dev->slice_id, stream->list_num);
1977         return 0;
1980 /*
1981  * Abort streaming and wait for last buffer
1982  */
1983 static int vip_stop_streaming(struct vb2_queue *vq)
1985         struct vip_stream *stream = vb2_get_drv_priv(vq);
1986         struct vip_port *port = stream->port;
1987         struct vip_dev *dev = port->dev;
1988         struct vip_buffer *buf;
1989         int ret;
1991         if (dev->sensor) {
1992                 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 0);
1993                 if (ret)
1994                         vip_dbg(1, dev, "stream on failed in subdev\n");
1995         }
1997         disable_irqs(dev, dev->slice_id, stream->list_num);
1998         clear_irqs(dev, dev->slice_id, stream->list_num);
1999         stop_dma(stream);
2001         /* release all active buffers */
2002         while (!list_empty(&stream->post_bufs)) {
2003                 buf = list_entry(stream->post_bufs.next,
2004                                         struct vip_buffer, list);
2005                 list_del(&buf->list);
2006                 if (buf->drop == 1)
2007                         list_add_tail(&buf->list, &stream->dropq);
2008                 else
2009                         vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
2010         }
2011         while (!list_empty(&stream->vidq)) {
2012                 buf = list_entry(stream->vidq.next, struct vip_buffer, list);
2013                 list_del(&buf->list);
2014                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
2015         }
2017         if (!vb2_is_streaming(vq))
2018                 return 0;
2020         vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
2021         vpdma_reset_desc_list(&stream->desc_list);
2023         return 0;
2026 /*
2027  * Lock access to the device
2028  */
2029 static void vip_lock(struct vb2_queue *vq)
2031         struct vip_stream *stream = vb2_get_drv_priv(vq);
2033         mutex_lock(&stream->port->dev->mutex);
2036 static void vip_unlock(struct vb2_queue *vq)
2038         struct vip_stream *stream = vb2_get_drv_priv(vq);
2039         mutex_unlock(&stream->port->dev->mutex);
2042 static struct vb2_ops vip_video_qops = {
2043         .queue_setup            = vip_queue_setup,
2044         .buf_prepare            = vip_buf_prepare,
2045         .buf_queue              = vip_buf_queue,
2046         .start_streaming        = vip_start_streaming,
2047         .stop_streaming         = vip_stop_streaming,
2048         .wait_prepare           = vip_unlock,
2049         .wait_finish            = vip_lock,
2050 };
2052 /*
2053  * File operations
2054  */
2056 static int vip_init_dev(struct vip_dev *dev)
2058         if (dev->num_ports != 0)
2059                 goto done;
2061         vip_set_clock_enable(dev, 1);
2062 done:
2063         dev->num_ports++;
2065         return 0;
2068 static int vip_init_port(struct vip_port *port)
2070         int ret;
2072         if (port->num_streams != 0)
2073                 goto done;
2075         ret = vip_init_dev(port->dev);
2076         if (ret)
2077                 goto done;
2079         port->fmt = port->dev->active_fmt[0];
2080         port->src_colorspace = port->fmt->colorspace;
2081         port->c_rect.left = 0;
2082         port->c_rect.top = 0;
2084 done:
2085         port->num_streams++;
2086         return 0;
2089 static int vip_init_stream(struct vip_stream *stream)
2091         int ret;
2093         ret = vip_init_port(stream->port);
2094         if (ret != 0)
2095                 return ret;
2097         ret = vpdma_create_desc_list(&stream->desc_list, VIP_DESC_LIST_SIZE,
2098                         VPDMA_LIST_TYPE_NORMAL);
2100         if (ret != 0)
2101                 return ret;
2103         stream->write_desc = (struct vpdma_dtd *)stream->desc_list.buf.addr
2104                                 + 15;
2105         return 0;
2108 static void vip_release_dev(struct vip_dev *dev)
2110         /*
2111          * On last close, disable clocks to conserve power
2112          */
2114         if (--dev->num_ports == 0)
2115                 vip_set_clock_enable(dev, 0);
2118 static int vip_setup_parser(struct vip_port *port)
2120         struct vip_dev *dev = port->dev;
2121         struct v4l2_of_endpoint *endpoint = dev->endpoint;
2122         int iface = DUAL_8B_INTERFACE;
2123         int sync_type;
2124         unsigned int flags;
2126         vip_reset_port(port);
2127         vip_set_port_enable(port, 1);
2129         if (endpoint->bus_type == V4L2_MBUS_BT656) {
2130                 iface = DUAL_8B_INTERFACE;
2132                 /* Ideally, this should come from sensor
2133                    port->fmt can be anything once CSC is enabled */
2134                 if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
2135                         sync_type = EMBEDDED_SYNC_SINGLE_RGB_OR_YUV444;
2136                 else {
2137                         switch (endpoint->bus.parallel.num_channels) {
2138                         case 4:
2139                                 sync_type = EMBEDDED_SYNC_4X_MULTIPLEXED_YUV422;
2140                         break;
2141                         case 2:
2142                                 sync_type = EMBEDDED_SYNC_2X_MULTIPLEXED_YUV422;
2143                         break;
2144                         case 1:
2145                         default:
2146                                 sync_type =
2147                                 EMBEDDED_SYNC_LINE_MULTIPLEXED_YUV422;
2148                         }
2149                 }
2151         } else if (endpoint->bus_type == V4L2_MBUS_PARALLEL) {
2152                 switch (endpoint->bus.parallel.bus_width) {
2153                 case 24:
2154                         iface = SINGLE_24B_INTERFACE;
2155                 break;
2156                 case 16:
2157                         iface = SINGLE_16B_INTERFACE;
2158                 break;
2159                 case 8:
2160                 default:
2161                         iface = DUAL_8B_INTERFACE;
2162                 }
2164                 if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
2165                         sync_type = DISCRETE_SYNC_SINGLE_RGB_24B;
2166                 else
2167                         sync_type = DISCRETE_SYNC_SINGLE_YUV422;
2169                 flags = endpoint->bus.parallel.flags;
2170                 if (flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH |
2171                         V4L2_MBUS_HSYNC_ACTIVE_LOW))
2172                         vip_set_vsync_polarity(port,
2173                                 flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH ? 1 : 0);
2175                 if (flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH |
2176                         V4L2_MBUS_VSYNC_ACTIVE_LOW))
2177                         vip_set_hsync_polarity(port,
2178                                 flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH ? 1 : 0);
2180                 if (flags & (V4L2_MBUS_PCLK_SAMPLE_RISING |
2181                         V4L2_MBUS_PCLK_SAMPLE_FALLING))
2182                         vip_set_pclk_polarity(port,
2183                                 flags & V4L2_MBUS_PCLK_SAMPLE_RISING ? 1 : 0);
2185                 vip_xtra_set_repack_sel(port, 0);
2186                 vip_set_actvid_hsync_n(port, 0);
2187                 vip_set_actvid_polarity(port, 1);
2188                 vip_set_discrete_basic_mode(port);
2190         } else {
2191                 vip_err(dev, "Device doesn't support CSI2");
2192                 return -EINVAL;
2193         }
2195         vip_set_data_interface(port, iface);
2196         vip_sync_type(port, sync_type);
2197         return 0;
2200 static void vip_release_stream(struct vip_stream *stream)
2202         struct vip_dev *dev = stream->port->dev;
2204         vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
2205         vpdma_free_desc_buf(&stream->desc_list.buf);
2206         vpdma_free_desc_list(&stream->desc_list);
2209 static void stop_dma(struct vip_stream *stream)
2211         struct vip_dev *dev = stream->port->dev;
2212         int ch, size = 0;
2214         /* Create a list of channels to be cleared */
2215         for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++) {
2216                 if (stream->vpdma_channels[ch] == 1) {
2217                         stream->vpdma_channels[size++] = ch;
2218                         vip_dbg(2, dev, "Clear channel no: %d\n", ch);
2219                 }
2220         }
2222         /* Clear all the used channels for the list */
2223         vpdma_list_cleanup(dev->shared->vpdma, stream->list_num,
2224                 stream->vpdma_channels, size);
2226         for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++)
2227                 stream->vpdma_channels[ch] = 0;
2230 int vip_open(struct file *file)
2232         struct vip_stream *stream = video_drvdata(file);
2233         struct vip_port *port = stream->port;
2234         struct vip_dev *dev = port->dev;
2235         struct v4l2_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
2236         int ret = 0;
2238         vip_dbg(2, dev, "vip_open\n");
2240         file->private_data = fh;
2241         if (fh == NULL)
2242                 return -ENOMEM;
2244         mutex_lock(&dev->mutex);
2246         v4l2_fh_init(fh, video_devdata(file));
2247         v4l2_fh_add(fh);
2249         /*
2250          * If this is the first open file.
2251          * Then initialize hw module.
2252          */
2253         if (v4l2_fh_is_singular_file(file)) {
2254                 if (vip_init_stream(stream)) {
2255                         goto free_fh;
2256                         ret = -ENODEV;
2257                 }
2258                 stream->width = 1280;
2259                 stream->height = 720;
2260                 stream->sizeimage = stream->width * stream->height *
2261                         (port->fmt->vpdma_fmt[0]->depth +
2262                         (port->fmt->coplanar ?
2263                                 port->fmt->vpdma_fmt[1]->depth : 0)) >> 3;
2264                 stream->bytesperline = round_up((stream->width *
2265                                         port->fmt->vpdma_fmt[0]->depth) >> 3,
2266                                         1 << L_ALIGN);
2267                 stream->sup_field = V4L2_FIELD_NONE;
2268                 port->c_rect.width = stream->width;
2269                 port->c_rect.height = stream->height;
2270                 vip_dbg(1, dev, "Created stream instance %p\n", stream);
2271         }
2273         mutex_unlock(&dev->mutex);
2274         return 0;
2276 free_fh:
2277         mutex_unlock(&dev->mutex);
2278         if (fh) {
2279                 v4l2_fh_del(fh);
2280                 v4l2_fh_exit(fh);
2281                 kfree(fh);
2282         }
2283         return ret;
2285 EXPORT_SYMBOL(vip_open);
2287 int vip_release(struct file *file)
2289         struct vip_stream *stream = video_drvdata(file);
2290         struct vip_port *port = stream->port;
2291         struct vip_dev *dev = port->dev;
2292         struct vb2_queue *q = &stream->vb_vidq;
2294         vip_dbg(2, dev, "vip_release\n");
2296         /*
2297          * If this is the last open file.
2298          * Then de-initialize hw module.
2299          */
2300         if (v4l2_fh_is_singular_file(file)) {
2301                 mutex_lock(&dev->mutex);
2303                 vip_stop_streaming(q);
2304                 vip_release_stream(stream);
2306                 if (--port->num_streams == 0)
2307                         vip_release_dev(port->dev);
2309                 mutex_unlock(&dev->mutex);
2310                 vip_dbg(1, dev, "Releasing stream instance %p\n", stream);
2311         }
2313         return vb2_fop_release(file);
2315 EXPORT_SYMBOL(vip_release);
2317 static const struct v4l2_file_operations vip_fops = {
2318         .owner          = THIS_MODULE,
2319         .open           = vip_open,
2320         .release        = vip_release,
2321         .poll           = vb2_fop_poll,
2322         .unlocked_ioctl = video_ioctl2,
2323         .mmap           = vb2_fop_mmap,
2324 };
2326 static struct video_device vip_videodev = {
2327         .name           = VIP_MODULE_NAME,
2328         .fops           = &vip_fops,
2329         .ioctl_ops      = &vip_ioctl_ops,
2330         .minor          = -1,
2331         .release        = video_device_release,
2332 };
2334 static int alloc_stream(struct vip_port *port, int stream_id, int vfl_type)
2336         struct vip_stream *stream;
2337         struct vip_dev *dev = port->dev;
2338         struct vb2_queue *q;
2339         struct video_device *vfd;
2340         struct vip_buffer *buf;
2341         int ret, i;
2343         stream = kzalloc(sizeof(*stream), GFP_KERNEL);
2344         if (!stream)
2345                 return -ENOMEM;
2347         stream->port = port;
2348         stream->stream_id = stream_id;
2349         stream->vfl_type = vfl_type;
2351         stream->list_num = vpdma_hwlist_alloc(dev->shared->vpdma, stream);
2352         if (stream->list_num < 0) {
2353                 vip_err(dev, "Could not get VPDMA hwlist");
2354                 ret = -ENODEV;
2355                 goto do_free_stream;
2356         }
2358         INIT_LIST_HEAD(&stream->post_bufs);
2360         if (vfl_type == VFL_TYPE_GRABBER)
2361                 port->cap_streams[stream_id] = stream;
2362         else
2363                 port->vbi_streams[stream_id] = stream;
2365         /*
2366          * Initialize queue
2367          */
2368         q = &stream->vb_vidq;
2369         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2370         q->io_modes = VB2_MMAP | VB2_DMABUF;
2371         q->drv_priv = stream;
2372         q->buf_struct_size = sizeof(struct vip_buffer);
2373         q->ops = &vip_video_qops;
2374         q->mem_ops = &vb2_dma_contig_memops;
2375         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2377         /* Feature not back-ported yet. Enable when available */
2378         /* q->min_buffers_needed = 3; */
2380         ret = vb2_queue_init(q);
2381         if (ret)
2382                 goto do_free_stream;
2384         INIT_LIST_HEAD(&stream->vidq);
2386         /* Allocate/populate Drop queue entries */
2387         INIT_LIST_HEAD(&stream->dropq);
2388         for (i = 0; i < VIP_DROPQ_SIZE; i++) {
2390                 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
2391                 if (!buf) {
2392                         vip_err(dev, "No memory!!");
2393                         ret = -ENOMEM;
2394                         goto do_free_stream;
2395                 }
2396                 buf->drop = true;
2397                 list_add(&buf->list, &stream->dropq);
2398         }
2400         vfd = &stream->vdev;
2401         *vfd = vip_videodev;
2402         vfd->v4l2_dev = &dev->v4l2_dev;
2403         vfd->queue = q;
2404         set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
2406         vfd->lock = &dev->mutex;
2407         video_set_drvdata(vfd, stream);
2409         ret = video_register_device(vfd, vfl_type, -1);
2410         if (ret) {
2411                 vip_err(dev, "Failed to register video device\n");
2412                 goto do_free_stream;
2413         }
2415         snprintf(vfd->name, sizeof(vfd->name), "%s", vip_videodev.name);
2416         stream->vfd = vfd;
2418         vip_info(dev, VIP_MODULE_NAME
2419                  " Device registered as /dev/video%d\n", vfd->num);
2420         return 0;
2422 do_free_stream:
2423         kfree(stream);
2424         return ret;
2427 static void free_stream(struct vip_stream *stream)
2429         struct vip_dev *dev = stream->port->dev;
2430         struct vip_buffer *buf;
2431         struct list_head *pos, *q;
2433         if (!stream)
2434                 return;
2436         /* Free up the Drop queue */
2437         list_for_each_safe(pos, q, &stream->dropq) {
2438                 buf = list_entry(stream->dropq.next,
2439                                  struct vip_buffer, list);
2440                 vip_dbg(1, dev, "dropq buffer\n");
2441                 list_del(pos);
2442                 kfree(buf);
2443         }
2445         video_unregister_device(stream->vfd);
2446         video_device_release(stream->vfd);
2447         vpdma_hwlist_release(dev->shared->vpdma, stream->list_num);
2448         kfree(stream);
2451 static int alloc_port(struct vip_dev *dev, int id)
2453         struct vip_port *port;
2454         int ret;
2456         port = kzalloc(sizeof(*port), GFP_KERNEL);
2457         if (!port)
2458                 return -ENOMEM;
2460         dev->ports[id] = port;
2461         port->dev = dev;
2462         port->port_id = id;
2463         port->num_streams = 0;
2464         port->flags |= FLAG_MULT_PORT;
2466         ret = alloc_stream(port, 0, VFL_TYPE_GRABBER);
2468         if (dev->endpoint->bus_type == V4L2_MBUS_BT656) {
2469                 /* Allocate streams for 4 channels */
2470                 ret = alloc_stream(port, 2, VFL_TYPE_GRABBER);
2471                 ret = alloc_stream(port, 4, VFL_TYPE_GRABBER);
2472                 ret = alloc_stream(port, 6, VFL_TYPE_GRABBER);
2473         }
2475         return 0;
2478 static void free_port(struct vip_port *port)
2480         if (!port)
2481                 return;
2483         free_stream(port->cap_streams[0]);
2485         kfree(port);
2488 static int get_field(u32 value, u32 mask, int shift)
2490         return (value & (mask << shift)) >> shift;
2493 static int vip_of_probe(struct platform_device *pdev, struct vip_dev *dev);
2494 static void vip_vpdma_fw_cb(struct platform_device *pdev)
2496         struct vip_shared *shared = platform_get_drvdata(pdev);
2497         struct vip_dev *dev;
2498         int slice, ret;
2500         dev_info(&pdev->dev, "VPDMA firmware loaded\n");
2502         for (slice = 0; slice < atomic_read(&shared->devs_allocated); slice++) {
2503                 dev = shared->devs[slice];
2505                 if (pdev->dev.of_node) {
2506                         ret = vip_of_probe(pdev, dev);
2507                         if (ret)
2508                                 goto free_port;
2509                 }
2510         }
2512         return;
2514 free_port:
2515         free_port(dev->ports[0]);
2518 static void remove_shared(struct vip_shared *shared)
2520         if (atomic_dec_return(&shared->devs_allocated) != 0)
2521                 return;
2523         iounmap(shared->base);
2524         release_mem_region(shared->res->start, resource_size(shared->res));
2525         kfree(shared);
2528 static int vip_runtime_get(struct platform_device *pdev)
2530         int r;
2532         r = pm_runtime_get_sync(&pdev->dev);
2533         WARN_ON(r < 0);
2534         return r < 0 ? r : 0;
2537 static int get_subdev_active_format(struct vip_dev *dev,
2538                                     struct v4l2_subdev *subdev)
2540         struct vip_fmt *fmt;
2541         enum v4l2_mbus_pixelcode code;
2542         int ret = 0;
2543         unsigned int k;
2545         /* first find how many formats to allocate the correct size */
2546         dev->num_active_fmt = 0;
2547         for (k = 0;
2548              (ret != -EINVAL) && (dev->num_active_fmt < VIP_MAX_ACTIVE_FMT);
2549              k++) {
2550                 ret = v4l2_subdev_call(subdev, video, enum_mbus_fmt, k, &code);
2551                 if (ret == 0) {
2552                         fmt = find_format_by_code(code);
2553                         if (fmt) {
2554                                 dev->active_fmt[dev->num_active_fmt] = fmt;
2555                                 dev->num_active_fmt++;
2556                         }
2557                 }
2558         }
2560         if (find_active_format_by_pix(dev, V4L2_PIX_FMT_YUYV))
2561                 /* When YUYV format is supported, NV12 can also be supported */
2562                 dev->active_fmt[dev->num_active_fmt++] = &vip_formats[2];
2564         if (dev->num_active_fmt == 0) {
2566                 vip_err(dev, "No suitable format reported by subdev %s\n",
2567                         subdev->name);
2568                 return -EINVAL;
2569         }
2571         return 0;
2574 static int vip_async_bound(struct v4l2_async_notifier *notifier,
2575                         struct v4l2_subdev *subdev,
2576                         struct v4l2_async_subdev *asd)
2578         struct vip_dev *dev = notifier_to_vip_dev(notifier);
2579         unsigned int idx = asd - &dev->config->asd[0];
2581         vip_dbg(1, dev, "vip_async_bound\n");
2582         if (idx > dev->config->asd_sizes)
2583                 return -EINVAL;
2585         if (get_subdev_active_format(dev, subdev))
2586                 return 0;
2588         if (dev->sensor) {
2589                 if (asd < dev->sensor->asd) {
2590                         /* Notified of a subdev earlier in the array */
2591                         dev->sensor = subdev;
2592                         dev->endpoint = &dev->config->endpoints[idx];
2593                         vip_info(dev, "Switching to subdev %s (High priority)",
2594                                  subdev->name);
2596                 } else
2597                         vip_info(dev, "Rejecting subdev %s (Low priority)",
2598                                  subdev->name);
2599                 return 0;
2600         }
2602         dev->sensor = subdev;
2603         dev->endpoint = &dev->config->endpoints[idx];
2604         vip_info(dev, "Using sensor %s for capture\n", subdev->name);
2605         alloc_port(dev, 0);
2607         return 0;
2610 static int vip_async_complete(struct v4l2_async_notifier *notifier)
2612         struct vip_dev *dev = notifier_to_vip_dev(notifier);
2613         vip_dbg(1, dev, "vip_async_complete\n");
2614         return 0;
2617 static struct device_node *
2618 of_get_next_port(const struct device_node *parent,
2619                  struct device_node *prev)
2621         struct device_node *port = NULL;
2623         if (!parent)
2624                 return NULL;
2626         if (!prev) {
2627                 struct device_node *ports;
2628                 /*
2629                  * It's the first call, we have to find a port subnode
2630                  * within this node or within an optional 'ports' node.
2631                  */
2632                 ports = of_get_child_by_name(parent, "ports");
2633                 if (ports)
2634                         parent = ports;
2636                 port = of_get_child_by_name(parent, "port");
2638                 /* release the 'ports' node */
2639                 of_node_put(ports);
2640         } else {
2641                 struct device_node *ports;
2643                 ports = of_get_parent(prev);
2644                 if (!ports)
2645                         return NULL;
2647                 do {
2648                         port = of_get_next_child(ports, prev);
2649                         if (!port) {
2650                                 of_node_put(ports);
2651                                 return NULL;
2652                         }
2653                         prev = port;
2654                 } while (of_node_cmp(port->name, "port") != 0);
2655         }
2657         return port;
2660 static struct device_node *
2661 of_get_next_endpoint(const struct device_node *parent,
2662                         struct device_node *prev)
2664         struct device_node *ep = NULL;
2666         if (!parent)
2667                 return NULL;
2669         do {
2670                 ep = of_get_next_child(parent, prev);
2671                 if (!ep)
2672                         return NULL;
2673                 prev = ep;
2674         } while (of_node_cmp(ep->name, "endpoint") != 0);
2676         return ep;
2679 static int vip_of_probe(struct platform_device *pdev, struct vip_dev *dev)
2681         struct device_node *ep_node = NULL, *port, *remote_ep,
2682                         *sensor_node, *parent;
2683         struct device_node *syscon_np;
2684         struct v4l2_of_endpoint *endpoint;
2685         struct v4l2_async_subdev *asd;
2686         u32 regval = 0;
2687         int ret, slice, i = 0, found_port = 0;
2689         parent = pdev->dev.of_node;
2691         syscon_np = of_parse_phandle(pdev->dev.of_node, "syscon", 0);
2692         dev->syscon = syscon_node_to_regmap(syscon_np);
2693         of_node_put(syscon_np);
2695         dev->config = kzalloc(sizeof(struct vip_config), GFP_KERNEL);
2696         if (!dev->config)
2697                 return -ENOMEM;
2699         dev->config->card_name = "VIP Driver";
2701         port = NULL;
2702         vip_dbg(3, dev, "Scanning Port node for slice id: %d\n", dev->slice_id);
2703         for (slice = 0; slice < VIP_NUM_SLICES; slice++) {
2704                 port = of_get_next_port(parent, port);
2705                 if (!port) {
2706                         vip_dbg(1, dev, "No port node found for slice_id:%d\n",
2707                                 slice);
2708                         ret = -EINVAL;
2709                         goto free_config;
2710                 }
2712                 /* Match the slice number with <REG> */
2713                 of_property_read_u32(port, "reg", &regval);
2714                 vip_dbg(3, dev, "slice:%d dev->slice_id:%d <reg>:%d\n",
2715                         slice, dev->slice_id, regval);
2716                 if ((regval == dev->slice_id) && (slice == dev->slice_id)) {
2717                         found_port = 1;
2718                         break;
2719                 }
2720         }
2722         if (!found_port) {
2723                 if (!port)
2724                         of_node_put(port);
2725                 vip_dbg(1, dev, "No port node matches slice_id:%d\n",
2726                         dev->slice_id);
2727                 ret = -EINVAL;
2728                 goto free_config;
2729         }
2731         vip_dbg(3, dev, "Scanning sub-device(s) for slice id: %d\n",
2732                 dev->slice_id);
2733         while (i < VIP_MAX_SUBDEV) {
2735                 asd = &dev->config->asd[i];
2736                 endpoint = &dev->config->endpoints[i];
2738                 remote_ep = NULL;
2739                 sensor_node = NULL;
2741                 ep_node = of_get_next_endpoint(port, ep_node);
2742                 if (!ep_node) {
2743                         vip_dbg(3, dev, "can't get next endpoint: loop: %d\n",
2744                                 i);
2745                         break;
2746                 }
2748                 sensor_node = of_graph_get_remote_port_parent(ep_node);
2749                 if (!sensor_node) {
2750                         vip_dbg(3, dev, "can't get remote parent: loop: %d\n",
2751                                 i);
2752                         goto of_node_cleanup;
2753                 }
2754                 asd->match_type = V4L2_ASYNC_MATCH_OF;
2755                 asd->match.of.node = sensor_node;
2757                 remote_ep = of_parse_phandle(ep_node, "remote-endpoint", 0);
2758                 if (!remote_ep) {
2759                         vip_dbg(3, dev, "can't get remote-endpoint: loop: %d\n",
2760                                 i);
2761                         goto of_node_cleanup;
2762                 }
2763                 v4l2_of_parse_endpoint(remote_ep, endpoint);
2765                 dev->config->asd_list[i++] = asd;
2767 of_node_cleanup:
2768                 if (!remote_ep)
2769                         of_node_put(remote_ep);
2770                 if (!sensor_node)
2771                         of_node_put(sensor_node);
2772         }
2774         if (!ep_node)
2775                 of_node_put(ep_node);
2776         if (!port)
2777                 of_node_put(port);
2779         vip_dbg(1, dev, "Found %d sub-device(s) for slice id: %d\n",
2780                 i, dev->slice_id);
2781         if (i > 0) {
2782                 dev->config->asd_sizes = i;
2783                 dev->notifier.subdevs = dev->config->asd_list;
2784                 dev->notifier.num_subdevs = dev->config->asd_sizes;
2785                 dev->notifier.bound = vip_async_bound;
2786                 dev->notifier.complete = vip_async_complete;
2788                 vip_dbg(1, dev, "registering the sync notifier for sensors %d",
2789                         i);
2790                 ret = v4l2_async_notifier_register(&dev->v4l2_dev,
2791                                                    &dev->notifier);
2792                 if (ret) {
2793                         vip_dbg(1, dev, "Error registering async notifier\n");
2794                         ret = -EINVAL;
2795                         goto free_config;
2796                 }
2797         }
2799         return 0;
2800 free_config:
2801         kfree(dev->config);
2802         return ret;
2805 static const struct of_device_id vip_of_match[];
2806 static int vip_probe(struct platform_device *pdev)
2808         struct vip_dev *dev;
2809         struct vip_shared *shared;
2810         const struct of_device_id *of_dev_id;
2811         struct pinctrl *pinctrl;
2812         int ret, slice = VIP_SLICE1;
2813         u32 tmp, pid;
2814         struct v4l2_ctrl_handler *hdl;
2816         pm_runtime_enable(&pdev->dev);
2818         ret = vip_runtime_get(pdev);
2819         if (ret)
2820                 goto err_runtime_get;
2822         of_dev_id = of_match_device(vip_of_match, &pdev->dev);
2823         if (!of_dev_id) {
2824                 dev_err(&pdev->dev, "%s: Unable to match device\n", __func__);
2825                 return -ENODEV;
2826         }
2828         shared = kzalloc(sizeof(*shared), GFP_KERNEL);
2829         if (!shared)
2830                 return -ENOMEM;
2832         shared->res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vip");
2833         if (shared->res == NULL) {
2834                 dev_err(&pdev->dev, "Missing platform resources data\n");
2835                 ret = -ENODEV;
2836                 goto free_shared;
2837         }
2839         pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2840         if (IS_ERR(pinctrl)) {
2841                 dev_warn(&pdev->dev, "No explicit pinctrl resources data.\n");
2842         }
2844         if (devm_request_mem_region(&pdev->dev, shared->res->start,
2845             resource_size(shared->res), VIP_MODULE_NAME) == NULL) {
2846                 ret = -ENOMEM;
2847                 goto free_shared;
2848         }
2850         shared->base = devm_ioremap(&pdev->dev, shared->res->start,
2851                                     resource_size(shared->res));
2852         if (!shared->base) {
2853                 dev_err(&pdev->dev, "failed to ioremap\n");
2854                 ret = -ENOMEM;
2855                 goto rel_mem_region;
2856         }
2858         /* Make sure H/W module has the right functionality */
2859         pid = read_sreg(shared, VIP_PID);
2860         tmp = get_field(pid, VIP_PID_FUNC_MASK, VIP_PID_FUNC_SHIFT);
2862         if (tmp != VIP_PID_FUNC) {
2863                 dev_info(&pdev->dev, "vip: unexpected PID function: 0x%x\n",
2864                        tmp);
2865                 ret = -ENODEV;
2866                 goto do_iounmap;
2867         }
2869         /* enable clocks, so the firmware will load properly */
2870         vip_shared_set_clock_enable(shared, 1);
2871         vip_top_vpdma_reset(shared);
2873         shared->vpdma = vpdma_create(pdev, vip_vpdma_fw_cb);
2874         if (!shared->vpdma) {
2875                 dev_err(&pdev->dev, "Creating VPDMA failed");
2876                 goto do_iounmap;
2877         }
2879         list_add_tail(&shared->list, &vip_shared_list);
2880         platform_set_drvdata(pdev, shared);
2881         atomic_set(&shared->devs_allocated, 0);
2883         vip_set_idle_mode(shared, VIP_SMART_IDLE_MODE);
2884         vip_set_standby_mode(shared, VIP_SMART_STANDBY_MODE);
2886         for (slice = VIP_SLICE1; slice < VIP_NUM_SLICES; slice++) {
2887                 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2888                 if (!dev)
2889                         return -ENOMEM;
2891                 dev->irq = platform_get_irq(pdev, slice);
2892                 if (!dev->irq) {
2893                         dev_err(&pdev->dev, "Could not get IRQ");
2894                         goto err_runtime_get;
2895                 }
2897                 if (devm_request_irq(&pdev->dev, dev->irq, vip_irq,
2898                                      0, VIP_MODULE_NAME, dev) < 0) {
2899                         ret = -ENOMEM;
2900                         goto dev_unreg;
2901                 }
2903                 spin_lock_init(&dev->slock);
2904                 spin_lock_init(&dev->lock);
2906                 dev->instance_id = (int)of_dev_id->data;
2908                 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
2909                         "%s%d-%d", VIP_MODULE_NAME, dev->instance_id, slice);
2910                 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2911                 if (ret)
2912                         goto err_runtime_get;
2914                 mutex_init(&dev->mutex);
2916                 hdl = &dev->ctrl_handler;
2917                 v4l2_ctrl_handler_init(hdl, 11);
2918                 dev->v4l2_dev.ctrl_handler = hdl;
2920                 dev->slice_id = slice;
2921                 dev->pdev = pdev;
2922                 dev->res = shared->res;
2923                 dev->base = shared->base;
2925                 dev->shared = shared;
2926                 shared->devs[slice] = dev;
2928                 atomic_inc(&shared->devs_allocated);
2930                 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2931                 if (IS_ERR(dev->alloc_ctx)) {
2932                         vip_err(dev, "Failed to alloc vb2 context\n");
2933                         ret = PTR_ERR(dev->alloc_ctx);
2934                         goto dev_unreg;
2935                 }
2937                 vip_top_reset(dev);
2938                 vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT);
2939         }
2941         return 0;
2943 dev_unreg:
2944         v4l2_device_unregister(&dev->v4l2_dev);
2945 do_iounmap:
2946         iounmap(shared->base);
2947 rel_mem_region:
2948         release_mem_region(shared->res->start, resource_size(shared->res));
2949 free_shared:
2950         kfree(shared);
2951 err_runtime_get:
2952         if (slice == VIP_SLICE1) {
2953                 pm_runtime_disable(&pdev->dev);
2954                 return ret;
2955         } else
2956                 return 0;
2959 static int vip_remove(struct platform_device *pdev)
2961         struct vip_shared *shared = platform_get_drvdata(pdev);
2962         struct vip_dev *dev;
2963         int slice;
2965         for (slice = 0; slice < atomic_read(&shared->devs_allocated); slice++) {
2966                 dev = shared->devs[slice];
2967                 if (!dev)
2968                         continue;
2969                 vip_info(dev, "Removing " VIP_MODULE_NAME);
2970                 free_port(dev->ports[0]);
2971                 v4l2_async_notifier_unregister(&dev->notifier);
2972                 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2973                 free_irq(dev->irq, dev);
2974                 kfree(dev);
2975         }
2976         remove_shared(shared);
2978         return 0;
2981 #if defined(CONFIG_OF)
2982 static const struct of_device_id vip_of_match[] = {
2983         {
2984                 .compatible = "ti,vip1", .data = (void *) VIP_INSTANCE1,
2985         },
2987         {
2988                 .compatible = "ti,vip2", .data = (void *) VIP_INSTANCE2,
2989         },
2991         {
2992                 .compatible = "ti,vip3", .data = (void *) VIP_INSTANCE3,
2993         },
2994         {},
2995 };
2996 #else
2997 #define vip_of_match NULL
2998 #endif
3000 static struct platform_driver vip_pdrv = {
3001         .probe          = vip_probe,
3002         .remove         = vip_remove,
3003         .driver         = {
3004                 .name   = VIP_MODULE_NAME,
3005                 .owner  = THIS_MODULE,
3006                 .of_match_table = vip_of_match,
3007         },
3008 };
3010 module_platform_driver(vip_pdrv);
3012 MODULE_DESCRIPTION("TI VIP driver");
3013 MODULE_AUTHOR("Texas Instruments");
3014 MODULE_LICENSE("GPL v2");