]> 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 'rpmsg-ti-linux-3.14.y' of git://git.ti.com/rpmsg/rpmsg into ti-linux...
[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                 /* bus order is reversed so flip Y and UV bytes */
166                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CBY422],
167                                   },
168         },
169         {
170                 .name           = "YUYV 422 packed",
171                 .fourcc         = V4L2_PIX_FMT_YUYV,
172                 .code           = V4L2_MBUS_FMT_YUYV8_2X8,
173                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
174                 .coplanar       = 0,
175                 /* bus order is reversed so flip Y and UV bytes */
176                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CBY422],
177                                   },
178         },
179         {
180                 .name           = "VYUY 422 packed",
181                 .fourcc         = V4L2_PIX_FMT_VYUY,
182                 .code           = V4L2_MBUS_FMT_VYUY8_2X8,
183                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
184                 .coplanar       = 0,
185                 /* bus order is reversed so flip Y and UV bytes */
186                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CBY422],
187                                   },
188         },
189         {
190                 .name           = "YVYU 422 packed",
191                 .fourcc         = V4L2_PIX_FMT_YVYU,
192                 .code           = V4L2_MBUS_FMT_YVYU8_2X8,
193                 .colorspace     = V4L2_COLORSPACE_SMPTE170M,
194                 .coplanar       = 0,
195                 /* bus order is reversed so flip Y and UV bytes */
196                 .vpdma_fmt      = { &vpdma_yuv_fmts[VPDMA_DATA_FMT_CBY422],
197                                   },
198         },
199         {
200                 .name           = "RGB888 packed",
201                 .fourcc         = V4L2_PIX_FMT_RGB24,
202                 .code           = V4L2_MBUS_FMT_RGB888_1X24,
203                 .colorspace     = V4L2_COLORSPACE_SRGB,
204                 .coplanar       = 0,
205                 .vpdma_fmt      = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_RGB24],
206                                   },
207         },
208         {
209                 .name           = "ARGB888 packed",
210                 .fourcc         = V4L2_PIX_FMT_RGB32,
211                 .code           = V4L2_MBUS_FMT_ARGB8888_1X32,
212                 .colorspace     = V4L2_COLORSPACE_SRGB,
213                 .coplanar       = 0,
214                 .vpdma_fmt      = { &vpdma_rgb_fmts[VPDMA_DATA_FMT_ARGB32],
215                                   },
216         },
217 };
219 /*  Print Four-character-code (FOURCC) */
220 static char *fourcc_to_str(u32 fmt)
222         static char code[5];
223         code[0] = (unsigned char)(fmt & 0xff);
224         code[1] = (unsigned char)((fmt>>8) & 0xff);
225         code[2] = (unsigned char)((fmt>>16) & 0xff);
226         code[3] = (unsigned char)((fmt>>24) & 0xff);
227         code[4] = '\0';
229         return code;
232 /*
233  * Find our format description corresponding to the passed v4l2_format
234  */
235 #ifdef DISABLED_FOR_NOW
236 static struct vip_fmt *find_format_by_pix(u32 pixelformat)
238         struct vip_fmt *fmt;
239         unsigned int k;
241         for (k = 0; k < ARRAY_SIZE(vip_formats); k++) {
242                 fmt = &vip_formats[k];
243                 if (fmt->fourcc == pixelformat)
244                         return fmt;
245         }
247         return NULL;
249 #endif
251 static struct vip_fmt *find_format_by_code(u32 code)
253         struct vip_fmt *fmt;
254         unsigned int k;
256         for (k = 0; k < ARRAY_SIZE(vip_formats); k++) {
257                 fmt = &vip_formats[k];
258                 if (fmt->code == code)
259                         return fmt;
260         }
262         return NULL;
265 static struct vip_fmt *find_active_format_by_pix(struct vip_dev *dev,
266                                                  u32 pixelformat)
268         struct vip_fmt *fmt;
269         unsigned int k;
271         for (k = 0; k < dev->num_active_fmt; k++) {
272                 fmt = dev->active_fmt[k];
273                 if (fmt->fourcc == pixelformat)
274                         return fmt;
275         }
277         return NULL;
280 static struct vip_fmt *find_active_format_by_code(struct vip_dev *dev,
281                                                  u32 code)
283         struct vip_fmt *fmt;
284         unsigned int k;
286         for (k = 0; k < dev->num_active_fmt; k++) {
287                 fmt = dev->active_fmt[k];
288                 if (fmt->code == code)
289                         return fmt;
290         }
292         return NULL;
295 static LIST_HEAD(vip_shared_list);
297 static inline struct vip_dev *notifier_to_vip_dev(struct v4l2_async_notifier *n)
299         return container_of(n, struct vip_dev, notifier);
302 /*
303  * port flag bits
304  */
305 #define FLAG_FRAME_1D           (1 << 0)
306 #define FLAG_EVEN_LINE_SKIP     (1 << 1)
307 #define FLAG_ODD_LINE_SKIP      (1 << 2)
308 #define FLAG_MODE_TILED         (1 << 3)
309 #define FLAG_INTERLACED         (1 << 4)
310 #define FLAG_MULTIPLEXED        (1 << 5)
311 #define FLAG_MULT_PORT          (1 << 6)
312 #define FLAG_MULT_ANC           (1 << 7)
314 /*
315  * Function prototype declarations
316  */
317 static int alloc_port(struct vip_dev *, int);
318 static void free_port(struct vip_port *);
319 static int vip_setup_parser(struct vip_port *port);
320 static void stop_dma(struct vip_stream *stream);
322 static inline u32 read_sreg(struct vip_shared *shared, int offset)
324         return ioread32(shared->base + offset);
327 static inline void write_sreg(struct vip_shared *shared, int offset, u32 value)
329         iowrite32(value, shared->base + offset);
332 static inline u32 read_vreg(struct vip_dev *dev, int offset)
334         return ioread32(dev->base + offset);
337 static inline void write_vreg(struct vip_dev *dev, int offset, u32 value)
339         iowrite32(value, dev->base + offset);
342 /*
343  * Insert a masked field into a 32-bit field
344  */
345 static void insert_field(u32 *valp, u32 field, u32 mask, int shift)
347         u32 val = *valp;
349         val &= ~(mask << shift);
350         val |= (field & mask) << shift;
351         *valp = val;
354 /*
355  * Set the system idle mode
356  */
357 static void vip_set_idle_mode(struct vip_shared *shared, int mode)
359         u32 reg = read_sreg(shared, VIP_SYSCONFIG);
360         insert_field(&reg, mode, VIP_SYSCONFIG_IDLE_MASK,
361                      VIP_SYSCONFIG_IDLE_SHIFT);
362         write_sreg(shared, VIP_SYSCONFIG, reg);
365 /*
366  * Set the VIP standby mode
367  */
368 static void vip_set_standby_mode(struct vip_shared *shared, int mode)
370         u32 reg = read_sreg(shared, VIP_SYSCONFIG);
371         insert_field(&reg, mode, VIP_SYSCONFIG_STANDBY_MASK,
372                      VIP_SYSCONFIG_STANDBY_SHIFT);
373         write_sreg(shared, VIP_SYSCONFIG, reg);
376 /*
377  * Enable or disable the VIP clocks
378  */
379 static void vip_set_clock_enable(struct vip_dev *dev, bool on)
381         u32 val = 0;
383         val = read_vreg(dev, VIP_CLK_ENABLE);
384         if (on) {
385                 val |= VIP_VPDMA_CLK_ENABLE;
386                 if (dev->slice_id == VIP_SLICE1)
387                         val |= VIP_VIP1_DATA_PATH_CLK_ENABLE;
388                 else
389                         val |= VIP_VIP2_DATA_PATH_CLK_ENABLE;
390         } else {
391                 if (dev->slice_id == VIP_SLICE1)
392                         val &= ~VIP_VIP1_DATA_PATH_CLK_ENABLE;
393                 else
394                         val &= ~VIP_VIP2_DATA_PATH_CLK_ENABLE;
396                 /* Both VIP are disabled then shutdown VPDMA also */
397                 if (!(val & (VIP_VIP1_DATA_PATH_CLK_ENABLE|
398                              VIP_VIP2_DATA_PATH_CLK_ENABLE)))
399                         val = 0;
400         }
402         write_vreg(dev, VIP_CLK_ENABLE, val);
405 /* This helper function is used to enable the clock early on to
406  * enable vpdma firmware loading before the slice device are created
407  */
408 static void vip_shared_set_clock_enable(struct vip_shared *shared, bool on)
410         u32 val = 0;
412         if (on)
413                 val = VIP_VIP1_DATA_PATH_CLK_ENABLE | VIP_VPDMA_CLK_ENABLE;
415         write_sreg(shared, VIP_CLK_ENABLE, val);
418 static void vip_top_reset(struct vip_dev *dev)
420         u32 val = 0;
422         val = read_vreg(dev, VIP_CLK_RESET);
424         if (dev->slice_id == VIP_SLICE1)
425                 insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
426                         VIP_VIP1_DATA_PATH_RESET_SHIFT);
427         else
428                 insert_field(&val, 1, VIP_DATA_PATH_CLK_RESET_MASK,
429                         VIP_VIP2_DATA_PATH_RESET_SHIFT);
431         write_vreg(dev, VIP_CLK_RESET, val);
433         usleep_range(200, 250);
435         val = read_vreg(dev, VIP_CLK_RESET);
437         if (dev->slice_id == VIP_SLICE1)
438                 insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
439                         VIP_VIP1_DATA_PATH_RESET_SHIFT);
440         else
441                 insert_field(&val, 0, VIP_DATA_PATH_CLK_RESET_MASK,
442                         VIP_VIP2_DATA_PATH_RESET_SHIFT);
443         write_vreg(dev, VIP_CLK_RESET, val);
446 static void vip_top_vpdma_reset(struct vip_shared *shared)
448         u32 val;
450         val = read_sreg(shared, VIP_CLK_RESET);
451         insert_field(&val, 1, VIP_VPDMA_CLK_RESET_MASK,
452                 VIP_VPDMA_CLK_RESET_SHIFT);
453         write_sreg(shared, VIP_CLK_RESET, val);
455         usleep_range(200, 250);
457         val = read_sreg(shared, VIP_CLK_RESET);
458         insert_field(&val, 0, VIP_VPDMA_CLK_RESET_MASK,
459                 VIP_VPDMA_CLK_RESET_SHIFT);
460         write_sreg(shared, VIP_CLK_RESET, val);
463 static void vip_xtra_set_repack_sel(struct vip_port *port, int repack_mode)
465         u32 val;
467         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
468                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
469                                 VIP_PARSER_PORTA_1);
470                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
471                              VIP_REPACK_SEL_SHFT);
473                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
474                            VIP_PARSER_PORTA_1, val);
475         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
476                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
477                                 VIP_PARSER_PORTA_1);
478                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
479                              VIP_REPACK_SEL_SHFT);
481                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
482                            VIP_PARSER_PORTA_1, val);
483         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
484                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
485                                 VIP_PARSER_PORTB_1);
486                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
487                              VIP_REPACK_SEL_SHFT);
489                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
490                            VIP_PARSER_PORTB_1, val);
491         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
492                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
493                                 VIP_PARSER_PORTB_1);
494                 insert_field(&val, repack_mode, VIP_REPACK_SEL_MASK,
495                              VIP_REPACK_SEL_SHFT);
497                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
498                            VIP_PARSER_PORTB_1, val);
499         }
502 static void vip_set_discrete_basic_mode(struct vip_port *port)
504         u32 val;
506         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
507                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
508                                 VIP_PARSER_PORTA_0);
509                 val |= VIP_DISCRETE_BASIC_MODE;
511                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
512                            VIP_PARSER_PORTA_0, val);
513         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
514                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
515                                 VIP_PARSER_PORTA_0);
516                 val |= VIP_DISCRETE_BASIC_MODE;
518                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
519                            VIP_PARSER_PORTA_0, val);
520         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
521                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
522                                 VIP_PARSER_PORTB_0);
523                 val |= VIP_DISCRETE_BASIC_MODE;
525                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
526                            VIP_PARSER_PORTB_0, val);
527         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
528                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
529                                 VIP_PARSER_PORTB_0);
530                 val |= VIP_DISCRETE_BASIC_MODE;
532                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
533                            VIP_PARSER_PORTB_0, val);
534         }
537 static void vip_set_pclk_polarity(struct vip_port *port, int polarity)
539         u32 val, ret, offset;
541         if (polarity == 1 && port->dev->syscon) {
543                 /*
544                  * When the VIP parser is configured to so that the pixel clock
545                  * is to be sampled at falling edge, the pixel clock needs to be
546                  * inverted before it is given to the VIP module. This is done
547                  * by setting a bit in the CTRL_CORE_SMA_SW1 register.
548                  */
550                 if (port->dev->instance_id == VIP_INSTANCE1)
551                         offset = 0 + 2 * port->port_id + port->dev->slice_id;
552                 else if (port->dev->instance_id == VIP_INSTANCE2)
553                         offset = 4 + 2 * port->port_id + port->dev->slice_id;
554                 else if (port->dev->instance_id == VIP_INSTANCE3)
555                         offset = 10 - port->dev->slice_id;
556                 else
557                         BUG();
559                 ret = regmap_update_bits(port->dev->syscon,
560                         CTRL_CORE_SMA_SW_1, 1 << offset, 1 << offset);
561         }
563         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
564                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
565                                 VIP_PARSER_PORTA_0);
566                 if (polarity)
567                         val |= VIP_PIXCLK_EDGE_POLARITY;
568                 else
569                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
571                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
572                            VIP_PARSER_PORTA_0, val);
573         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
574                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET
575                                 + VIP_PARSER_PORTA_0);
576                 if (polarity)
577                         val |= VIP_PIXCLK_EDGE_POLARITY;
578                 else
579                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
580                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
581                            VIP_PARSER_PORTA_0, val);
582         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
583                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
584                                 VIP_PARSER_PORTB_0);
585                 if (polarity)
586                         val |= VIP_PIXCLK_EDGE_POLARITY;
587                 else
588                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
589                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
590                            VIP_PARSER_PORTB_0, val);
591         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
592                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
593                                 VIP_PARSER_PORTB_0);
594                 if (polarity)
595                         val |= VIP_PIXCLK_EDGE_POLARITY;
596                 else
597                         val &= ~VIP_PIXCLK_EDGE_POLARITY;
599                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
600                            VIP_PARSER_PORTB_0, val);
601         }
604 static void vip_set_vsync_polarity(struct vip_port *port, int polarity)
606         u32 val;
608         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
609                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
610                                 VIP_PARSER_PORTA_0);
611                 if (polarity)
612                         val |= VIP_VSYNC_POLARITY;
613                 else
614                         val &= ~VIP_VSYNC_POLARITY;
616                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
617                            VIP_PARSER_PORTA_0, val);
618         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
619                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
620                                 VIP_PARSER_PORTA_0);
621                 if (polarity)
622                         val |= VIP_VSYNC_POLARITY;
623                 else
624                         val &= ~VIP_VSYNC_POLARITY;
625                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
626                            VIP_PARSER_PORTA_0, val);
627         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
628                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
629                                 VIP_PARSER_PORTB_0);
630                 if (polarity)
631                         val |= VIP_VSYNC_POLARITY;
632                 else
633                         val &= ~VIP_VSYNC_POLARITY;
634                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
635                            VIP_PARSER_PORTB_0, val);
636         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
637                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
638                                 VIP_PARSER_PORTB_0);
639                 if (polarity)
640                         val |= VIP_VSYNC_POLARITY;
641                 else
642                         val &= ~VIP_VSYNC_POLARITY;
644                 write_vreg(port->dev,  VIP2_PARSER_REG_OFFSET +
645                            VIP_PARSER_PORTB_0, val);
646         }
649 static void vip_set_hsync_polarity(struct vip_port *port, int polarity)
651         u32 val;
653         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
654                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
655                                 VIP_PARSER_PORTA_0);
656                 if (polarity)
657                         val |= VIP_HSYNC_POLARITY;
658                 else
659                         val &= ~VIP_HSYNC_POLARITY;
661                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
662                            VIP_PARSER_PORTA_0, val);
663         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
664                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
665                                 VIP_PARSER_PORTA_0);
666                 if (polarity)
667                         val |= VIP_HSYNC_POLARITY;
668                 else
669                         val &= ~VIP_HSYNC_POLARITY;
670                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
671                            VIP_PARSER_PORTA_0, val);
672         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
673                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
674                                 VIP_PARSER_PORTB_0);
675                 if (polarity)
676                         val |= VIP_HSYNC_POLARITY;
677                 else
678                         val &= ~VIP_HSYNC_POLARITY;
679                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
680                            VIP_PARSER_PORTB_0, val);
681         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
682                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
683                                 VIP_PARSER_PORTB_0);
684                 if (polarity)
685                         val |= VIP_HSYNC_POLARITY;
686                 else
687                         val &= ~VIP_HSYNC_POLARITY;
688                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
689                            VIP_PARSER_PORTB_0, val);
690         }
693 static void vip_set_actvid_polarity(struct vip_port *port, int polarity)
695         u32 val;
697         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
698                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
699                                 VIP_PARSER_PORTA_0);
700                 if (polarity)
701                         val |= VIP_ACTVID_POLARITY;
702                 else
703                         val &= ~VIP_ACTVID_POLARITY;
705                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
706                            VIP_PARSER_PORTA_0, val);
707         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
708                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
709                                 VIP_PARSER_PORTA_0);
710                 if (polarity)
711                         val |= VIP_ACTVID_POLARITY;
712                 else
713                         val &= ~VIP_ACTVID_POLARITY;
714                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
715                            VIP_PARSER_PORTA_0, val);
716         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
717                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
718                                 VIP_PARSER_PORTB_0);
719                 if (polarity)
720                         val |= VIP_ACTVID_POLARITY;
721                 else
722                         val &= ~VIP_ACTVID_POLARITY;
723                 write_vreg(port->dev,  VIP1_PARSER_REG_OFFSET +
724                            VIP_PARSER_PORTB_0, val);
725         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
726                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
727                                 VIP_PARSER_PORTB_0);
728                 if (polarity)
729                         val |= VIP_ACTVID_POLARITY;
730                 else
731                         val &= ~VIP_ACTVID_POLARITY;
733                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
734                            VIP_PARSER_PORTB_0, val);
735         }
738 static void vip_set_actvid_hsync_n(struct vip_port *port, int enable)
740         u32 val;
742         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
743                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
744                                 VIP_PARSER_PORTA_0);
745                 if (enable)
746                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
747                 else
748                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
750                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
751                            VIP_PARSER_PORTA_0, val);
752         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
753                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
754                                 VIP_PARSER_PORTA_0);
755                 if (enable)
756                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
757                 else
758                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
759                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
760                            VIP_PARSER_PORTA_0, val);
761         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
762                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
763                                 VIP_PARSER_PORTB_0);
764                 if (enable)
765                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
766                 else
767                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
768                 write_vreg(port->dev,  VIP1_PARSER_REG_OFFSET +
769                            VIP_PARSER_PORTB_0, val);
770         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
771                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
772                                 VIP_PARSER_PORTB_0);
773                 if (enable)
774                         val |= VIP_USE_ACTVID_HSYNC_ONLY;
775                 else
776                         val &= ~VIP_USE_ACTVID_HSYNC_ONLY;
778                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
779                            VIP_PARSER_PORTB_0, val);
780         }
783 static void vip_sync_type(struct vip_port *port, enum sync_types sync)
785         u32 val;
787         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
788                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
789                                 VIP_PARSER_PORTA_0);
790                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
791                              VIP_SYNC_TYPE_SHFT);
793                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
794                            VIP_PARSER_PORTA_0, val);
795         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
796                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
797                                 VIP_PARSER_PORTA_0);
798                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
799                              VIP_SYNC_TYPE_SHFT);
801                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
802                            VIP_PARSER_PORTA_0, val);
803         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
804                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
805                                 VIP_PARSER_PORTB_0);
806                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
807                              VIP_SYNC_TYPE_SHFT);
809                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
810                            VIP_PARSER_PORTB_0, val);
811         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
812                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
813                                 VIP_PARSER_PORTB_0);
814                 insert_field(&val, sync, VIP_SYNC_TYPE_MASK,
815                              VIP_SYNC_TYPE_SHFT);
817                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
818                            VIP_PARSER_PORTB_0, val);
819         }
822 static void vip_set_data_interface(struct vip_port *port,
823                                    enum data_interface_modes mode)
825         u32 val = 0;
827         if (port->dev->slice_id == VIP_SLICE1) {
828                 insert_field(&val, mode, VIP_DATA_INTERFACE_MODE_MASK,
829                              VIP_DATA_INTERFACE_MODE_SHFT);
831                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET, val);
832         } else if (port->dev->slice_id == VIP_SLICE2) {
833                 insert_field(&val, mode, VIP_DATA_INTERFACE_MODE_MASK,
834                              VIP_DATA_INTERFACE_MODE_SHFT);
836                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET, val);
837         }
840 static void vip_reset_port(struct vip_port *port)
842         u32 val = 0;
844         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
845                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
846                            VIP_PARSER_PORTA_0, VIP_SW_RESET);
848                 usleep_range(200, 250);
850                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
851                                 VIP_PARSER_PORTA_0);
853                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
854                            VIP_PARSER_PORTA_0, 0);
855         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
856                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
857                            VIP_PARSER_PORTA_0, VIP_SW_RESET);
859                 usleep_range(200, 250);
861                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
862                                 VIP_PARSER_PORTA_0);
864                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
865                            VIP_PARSER_PORTA_0, 0);
867         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
868                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
869                            VIP_PARSER_PORTB_0, VIP_SW_RESET);
871                 usleep_range(200, 250);
873                 val = read_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
874                                 VIP_PARSER_PORTB_0);
876                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
877                            VIP_PARSER_PORTB_0, 0);
878         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
879                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
880                            VIP_PARSER_PORTB_0, VIP_SW_RESET);
882                 usleep_range(200, 250);
884                 val = read_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
885                                 VIP_PARSER_PORTB_0);
887                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
888                            VIP_PARSER_PORTB_0, 0);
889         }
892 static void vip_set_port_enable(struct vip_port *port, bool on)
894         u32 val = 0;
896         if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE1) {
897                 if (on)
898                         val |= VIP_PORT_ENABLE;
899                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
900                            VIP_PARSER_PORTA_0, val);
901         } else if (port->port_id == 0 && port->dev->slice_id == VIP_SLICE2) {
902                 if (on)
903                         val |= VIP_PORT_ENABLE;
904                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
905                            VIP_PARSER_PORTA_0, val);
906         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE1) {
907                 if (on)
908                         val |= VIP_PORT_ENABLE;
909                 write_vreg(port->dev, VIP1_PARSER_REG_OFFSET +
910                            VIP_PARSER_PORTB_0, val);
911         } else if (port->port_id == 1 && port->dev->slice_id == VIP_SLICE2) {
912                 if (on)
913                         val |= VIP_PORT_ENABLE;
914                 write_vreg(port->dev, VIP2_PARSER_REG_OFFSET +
915                            VIP_PARSER_PORTB_0, val);
916         }
919 static void vip_set_slice_path(struct vip_dev *dev,
920                                enum data_path_select data_path)
922         u32 val = 0;
924         switch (data_path) {
925         case VIP_MULTI_CHANNEL_DATA_SELECT:
926                 if (dev->slice_id == VIP_SLICE1) {
927                         val |= VIP_MULTI_CHANNEL_SELECT;
928                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
929                                      VIP_DATAPATH_SELECT_SHFT);
931                         write_vreg(dev, VIP_VIP1_DATA_PATH_SELECT, val);
932                 } else if (dev->slice_id == VIP_SLICE2) {
933                         val |= VIP_MULTI_CHANNEL_SELECT;
934                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
935                                      VIP_DATAPATH_SELECT_SHFT);
937                         write_vreg(dev, VIP_VIP2_DATA_PATH_SELECT, val);
938                 }
939                 break;
940         case VIP_RGB_OUT_LO_DATA_SELECT:
941                 if (dev->slice_id == VIP_SLICE1) {
942                         val |= VIP_RGB_OUT_LO_SRC_SELECT;
943                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
944                                      VIP_DATAPATH_SELECT_SHFT);
946                         write_vreg(dev, VIP_VIP1_DATA_PATH_SELECT, val);
947                 } else if (dev->slice_id == VIP_SLICE2) {
948                         val |= VIP_RGB_OUT_LO_SRC_SELECT;
949                         insert_field(&val, data_path, VIP_DATAPATH_SELECT_MASK,
950                                      VIP_DATAPATH_SELECT_SHFT);
952                         write_vreg(dev, VIP_VIP2_DATA_PATH_SELECT, val);
953                 }
954                 break;
955         default:
956                 BUG();
957         }
960 /*
961  * Return the vip_stream structure for a given struct file
962  */
963 static inline struct vip_stream *file2stream(struct file *file)
965         return video_drvdata(file);
969 /*
970  * Append a destination descriptor to the current descriptor list,
971  * setting up dma to the given srce.
972  */
973 static int add_out_dtd(struct vip_stream *stream, int srce_type)
975         struct vip_port *port = stream->port;
976         struct vip_dev *dev = port->dev;
977         struct vip_srce_info *sinfo = &srce_info[srce_type];
978         struct v4l2_rect *c_rect = &port->c_rect;
979         struct vip_fmt *fmt = port->fmt;
980         int channel, plane = 0;
981         int max_width, max_height;
982         dma_addr_t dma_addr;
983         u32 flags;
985         channel = sinfo->base_channel;
987         switch (srce_type) {
988         case VIP_SRCE_MULT_PORT:
989         case VIP_SRCE_MULT_ANC:
990                 if (port->port_id == VIP_PORTB)
991                         channel += VIP_CHAN_MULT_PORTB_OFFSET;
992                 channel += stream->stream_id;
993                 flags = 0;
994                 break;
995         case VIP_SRCE_CHROMA:
996                 plane = 1;
997         case VIP_SRCE_LUMA:
998                 if (port->port_id == VIP_PORTB)
999                         channel += VIP_CHAN_YUV_PORTB_OFFSET;
1000                 flags = port->flags;
1001                 break;
1002         case VIP_SRCE_RGB:
1003                 if (port->port_id == VIP_PORTB)
1004                         channel += VIP_CHAN_RGB_PORTB_OFFSET;
1005                 flags = port->flags;
1006                 break;
1007         default:
1008                 BUG();
1009         }
1011         if (dev->slice_id == VIP_SLICE2)
1012                 channel += VIP_CHAN_VIP2_OFFSET;
1014         /* This is just for initialization purposes.
1015          * The actual dma_addr will be configured in vpdma_update_dma_addr
1016          */
1017         dma_addr = (dma_addr_t)NULL;
1019         /*
1020          * Use VPDMA_MAX_SIZE1 or VPDMA_MAX_SIZE2 register for slice0/1
1021          */
1023         if (dev->slice_id == VIP_SLICE1) {
1024                 vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE1,
1025                         stream->width, stream->height);
1027                 max_width = MAX_OUT_WIDTH_REG1;
1028                 max_height = MAX_OUT_HEIGHT_REG1;
1029         } else {
1030                 vpdma_set_max_size(dev->shared->vpdma, VPDMA_MAX_SIZE2,
1031                         stream->width, stream->height);
1033                 max_width = MAX_OUT_WIDTH_REG2;
1034                 max_height = MAX_OUT_HEIGHT_REG2;
1035         }
1037         /* Mark this channel to be cleared while cleaning up resources
1038          * This will make sure that an abort descriptor for this channel
1039          * would be submitted to VPDMA causing any ongoing  transaction to be
1040          * aborted and cleanup the VPDMA FSM for this channel */
1041         stream->vpdma_channels[channel] = 1;
1043         vpdma_rawchan_add_out_dtd(&stream->desc_list, c_rect->width, c_rect,
1044                 fmt->vpdma_fmt[plane], dma_addr, max_width, max_height,
1045                 channel, flags);
1047         return 0;
1050 /*
1051  * add_stream_dtds - prepares and starts DMA for pending transfers
1052  */
1053 static void add_stream_dtds(struct vip_stream *stream)
1055         struct vip_port *port = stream->port;
1056         int srce_type;
1058         if (port->flags & FLAG_MULT_PORT)
1059                 srce_type = VIP_SRCE_MULT_PORT;
1060         else if (port->flags & FLAG_MULT_ANC)
1061                 srce_type = VIP_SRCE_MULT_ANC;
1062         else if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
1063                 srce_type = VIP_SRCE_RGB;
1064         else
1065                 srce_type = VIP_SRCE_LUMA;
1067         add_out_dtd(stream, srce_type);
1069         if (srce_type == VIP_SRCE_LUMA && port->fmt->coplanar)
1070                 add_out_dtd(stream, VIP_SRCE_CHROMA);
1073 static void enable_irqs(struct vip_dev *dev, int irq_num, int list_num)
1075         u32 reg_addr = VIP_INT0_ENABLE0_SET +
1076                         VIP_INTC_INTX_OFFSET * irq_num;
1078         write_sreg(dev->shared, reg_addr, 1 << (list_num * 2));
1080         vpdma_enable_list_complete_irq(dev->shared->vpdma,
1081                 irq_num, list_num, true);
1084 static void disable_irqs(struct vip_dev *dev, int irq_num, int list_num)
1086         u32 reg_addr = VIP_INT0_ENABLE0_CLR +
1087                         VIP_INTC_INTX_OFFSET * irq_num;
1089         write_sreg(dev->shared, reg_addr, 1 << (list_num * 2));
1091         vpdma_enable_list_complete_irq(dev->shared->vpdma,
1092                 irq_num, list_num, false);
1095 static void clear_irqs(struct vip_dev *dev, int irq_num, int list_num)
1097         u32 reg_addr = VIP_INT0_STATUS0_CLR +
1098                         VIP_INTC_INTX_OFFSET * irq_num;
1100         write_sreg(dev->shared, reg_addr, 1 << (list_num * 2));
1102         vpdma_clear_list_stat(dev->shared->vpdma, irq_num, dev->slice_id);
1105 static void populate_desc_list(struct vip_stream *stream)
1107         struct vip_port *port = stream->port;
1108         struct vip_dev *dev = port->dev;
1109         unsigned int list_length;
1111         stream->desc_next = stream->desc_list.buf.addr;
1112         add_stream_dtds(stream);
1114         list_length = stream->desc_next - stream->desc_list.buf.addr;
1115         vpdma_map_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
1118 /*
1119  * start_dma - adds descriptors to the dma list and submits them.
1120  * Should be called after a new vb is queued and on a vpdma list
1121  * completion interrupt.
1122  */
1123 static void start_dma(struct vip_stream *stream, struct vip_buffer *buf)
1125         struct vip_dev *dev = stream->port->dev;
1126         struct vpdma_data *vpdma = dev->shared->vpdma;
1127         int list_num = stream->list_num;
1128         dma_addr_t dma_addr;
1129         int drop_data;
1131         if (vpdma_list_busy(vpdma, list_num)) {
1132                 vip_err(dev, "vpdma list busy, cannot post");
1133                 return;                         /* nothing to do */
1134         }
1136         if (buf) {
1137                 dma_addr = vb2_dma_contig_plane_dma_addr(&buf->vb, 0);
1138                 drop_data = 0;
1139                 vip_dbg(4, dev, "start_dma: buf:0x%08x, vb:0x%08x, dma_addr:0x%08x\n",
1140                         (unsigned int)buf, (unsigned int)&buf->vb, dma_addr);
1141         } else {
1142                 dma_addr = (dma_addr_t)NULL;
1143                 drop_data = 1;
1144                 vip_dbg(4, dev, "start_dma: dropped\n");
1145         }
1147         vpdma_update_dma_addr(dev->shared->vpdma, &stream->desc_list,
1148                               dma_addr, stream->write_desc, drop_data, 0);
1150         if (stream->port->fmt->coplanar) {
1151                 dma_addr += stream->width * stream->height;
1152                 vpdma_update_dma_addr(dev->shared->vpdma, &stream->desc_list,
1153                               dma_addr, stream->write_desc + 1, drop_data, 1);
1154         }
1156         vpdma_submit_descs(dev->shared->vpdma,
1157                         &stream->desc_list, stream->list_num);
1160 static void vip_schedule_next_buffer(struct vip_stream *stream)
1162         struct vip_dev *dev = stream->port->dev;
1163         struct vip_buffer *buf;
1164         unsigned long flags;
1166         spin_lock_irqsave(&dev->slock, flags);
1167         if (list_empty(&stream->vidq)) {
1168                 vip_dbg(4, dev, "Dropping frame\n");
1169                 if (list_empty(&stream->dropq)) {
1170                         vip_err(dev, "No dropq buffer left!");
1171                         spin_unlock_irqrestore(&dev->slock, flags);
1172                         return;
1173                 }
1174                 buf = list_entry(stream->dropq.next,
1175                                  struct vip_buffer, list);
1177                 buf->drop = true;
1178                 list_move_tail(&buf->list, &stream->post_bufs);
1179                 buf = NULL;
1180         } else if (vb2_is_streaming(&stream->vb_vidq)) {
1181                 buf = list_entry(stream->vidq.next,
1182                                 struct vip_buffer, list);
1183                 buf->drop = false;
1184                 list_move_tail(&buf->list, &stream->post_bufs);
1185                 vip_dbg(4, dev, "added next buffer\n");
1186         } else {
1187                 vip_err(dev, "IRQ occurred when not streaming\n");
1188                 if (list_empty(&stream->dropq)) {
1189                         vip_err(dev, "No dropq buffer left!");
1190                         spin_unlock_irqrestore(&dev->slock, flags);
1191                         return;
1192                 }
1193                 buf = list_entry(stream->dropq.next,
1194                                  struct vip_buffer, list);
1195                 buf->drop = true;
1196                 list_move_tail(&buf->list, &stream->post_bufs);
1197                 buf = NULL;
1198         }
1200         spin_unlock_irqrestore(&dev->slock, flags);
1201         start_dma(stream, buf);
1204 static void vip_process_buffer_complete(struct vip_stream *stream)
1206         struct vip_dev *dev = stream->port->dev;
1207         struct vip_buffer *buf;
1208         struct vb2_buffer *vb = NULL;
1209         unsigned long flags, fld;
1211         buf = list_first_entry(&stream->post_bufs, struct vip_buffer, list);
1213         if (stream->port->flags & FLAG_INTERLACED) {
1214                 vpdma_unmap_desc_buf(dev->shared->vpdma,
1215                                         &stream->desc_list.buf);
1217                 fld = dtd_get_field(stream->write_desc);
1218                 stream->field = fld ? V4L2_FIELD_BOTTOM : V4L2_FIELD_TOP;
1220                 vpdma_map_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
1221         }
1223         if (buf) {
1224                 vip_dbg(4, dev, "vip buffer complete 0x%x, 0x%x\n",
1225                         (unsigned int)buf, buf->drop);
1227                 vb = &buf->vb;
1228                 vb->v4l2_buf.field = stream->field;
1229                 vb->v4l2_buf.sequence = stream->sequence;
1230                 v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
1232                 if (buf->drop) {
1233                         spin_lock_irqsave(&dev->slock, flags);
1234                         list_move_tail(&buf->list, &stream->dropq);
1235                         spin_unlock_irqrestore(&dev->slock, flags);
1236                 } else {
1237                         spin_lock_irqsave(&dev->slock, flags);
1238                         list_del(&buf->list);
1239                         spin_unlock_irqrestore(&dev->slock, flags);
1240                         vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
1241                 }
1242         } else {
1243                 BUG();
1244         }
1246         stream->sequence++;
1249 static irqreturn_t vip_irq(int irq_vip, void *data)
1251         struct vip_dev *dev = (struct vip_dev *)data;
1252         struct vpdma_data *vpdma = dev->shared->vpdma;
1253         struct vip_stream *stream;
1254         int list_num;
1255         int irq_num = dev->slice_id;
1256         u32 irqst, reg_addr;
1258         if (!dev->shared)
1259                 return IRQ_HANDLED;
1261         reg_addr = VIP_INT0_STATUS0 +
1262                         VIP_INTC_INTX_OFFSET * irq_num;
1263         irqst = read_sreg(dev->shared, reg_addr);
1265         vip_dbg(8, dev, "IRQ %d VIP_INT%d_STATUS0 0x%x\n",
1266                 irq_vip, irq_num, irqst);
1267         if (irqst) {
1268                 reg_addr = VIP_INT0_STATUS0_CLR +
1269                         VIP_INTC_INTX_OFFSET * irq_num;
1270                 write_sreg(dev->shared, reg_addr, irqst);
1272                 for (list_num = 0; list_num < 8;  list_num++) {
1273                         /* Check for LIST_COMPLETE IRQ */
1274                         if (!(irqst & (1 << list_num * 2)))
1275                                 continue;
1277                         vip_dbg(8, dev, "IRQ %d: handling LIST%d_COMPLETE\n",
1278                                 irq_num, list_num);
1280                         stream = vpdma_hwlist_get_priv(vpdma, list_num);
1281                         if (!stream || stream->list_num != list_num) {
1282                                 vip_err(dev, "IRQ occured for unused list");
1283                                 continue;
1284                         }
1286                         vpdma_clear_list_stat(vpdma, irq_num, list_num);
1288                         if (dev->num_skip_irq)
1289                                 dev->num_skip_irq--;
1290                         else
1291                                 vip_process_buffer_complete(stream);
1293                         vip_schedule_next_buffer(stream);
1294                         irqst &= ~((1 << list_num * 2));
1295                 }
1296         }
1298         return IRQ_HANDLED;
1301 /*
1302  * video ioctls
1303  */
1304 static struct v4l2_mbus_framefmt *
1305 vip_video_pix_to_mbus(const struct v4l2_pix_format *pix,
1306                       struct v4l2_mbus_framefmt *mbus)
1308         unsigned int i;
1310         memset(mbus, 0, sizeof(*mbus));
1311         mbus->width = pix->width;
1312         mbus->height = pix->height;
1314         mbus->code = V4L2_MBUS_FMT_YUYV8_2X8;
1315         for (i = 0; i < ARRAY_SIZE(vip_formats) - 1; ++i) {
1316                 if (vip_formats[i].fourcc == pix->pixelformat) {
1317                         mbus->code = vip_formats[i].code;
1318                         break;
1319                 }
1320         }
1322         mbus->colorspace = pix->colorspace;
1323         mbus->field = pix->field;
1325         return mbus;
1328 static int vip_querycap(struct file *file, void *priv,
1329                         struct v4l2_capability *cap)
1331         strncpy(cap->driver, VIP_MODULE_NAME, sizeof(cap->driver) - 1);
1332         strncpy(cap->card, VIP_MODULE_NAME, sizeof(cap->card) - 1);
1333         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
1334                 VIP_MODULE_NAME);
1335         cap->device_caps  = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_CAPTURE;
1336         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1337         return 0;
1340 static int vip_enuminput(struct file *file, void *priv,
1341                                 struct v4l2_input *inp)
1343         struct vip_stream *stream = file2stream(file);
1345         if (inp->index)
1346                 return -EINVAL;
1348         inp->type = V4L2_INPUT_TYPE_CAMERA;
1349         sprintf(inp->name, "camera %u", stream->vfd->num);
1351         return 0;
1354 static int vip_g_input(struct file *file, void *priv, unsigned int *i)
1356         *i = 0;
1357         return 0;
1360 static int vip_s_input(struct file *file, void *priv, unsigned int i)
1362         if (i != 0)
1363                 return -EINVAL;
1364         return 0;
1367 static int vip_querystd(struct file *file, void *fh, v4l2_std_id *std)
1369         struct vip_stream *stream = file2stream(file);
1370         struct vip_dev *dev = stream->port->dev;
1372         v4l2_subdev_call(dev->sensor, video, querystd, std);
1373         return 0;
1376 static int vip_g_std(struct file *file, void *fh, v4l2_std_id *std)
1378         struct vip_stream *stream = file2stream(file);
1379         struct vip_dev *dev = stream->port->dev;
1381         *std = 0;
1382         v4l2_subdev_call(dev->sensor, video, g_std_output, std);
1383         return 0;
1386 static int vip_s_std(struct file *file, void *fh, v4l2_std_id std)
1388         struct vip_stream *stream = file2stream(file);
1389         struct vip_dev *dev = stream->port->dev;
1391         v4l2_subdev_call(dev->sensor, video, s_std_output, std);
1392         return 0;
1395 static int vip_enum_fmt_vid_cap(struct file *file, void *priv,
1396                                 struct v4l2_fmtdesc *f)
1398         struct vip_stream *stream = file2stream(file);
1399         struct vip_dev *dev = stream->port->dev;
1400         struct vip_fmt *fmt;
1402         vip_dbg(3, dev, "enum_fmt index:%d\n", f->index);
1403         if (f->index >= dev->num_active_fmt)
1404                 return -EINVAL;
1406         fmt = dev->active_fmt[f->index];
1408         strncpy(f->description, fmt->name, sizeof(f->description) - 1);
1409         f->pixelformat = fmt->fourcc;
1411         vip_dbg(3, dev, "enum_fmt fourcc:%s description:%s\n",
1412                 fourcc_to_str(f->pixelformat), f->description);
1414         return 0;
1417 /*
1418   * TODO: Change from hard coding values to reading these through
1419   * IOCTLS directly from sensor
1420   */
1422 static int vip_enum_framesizes(struct file *file, void *priv,
1423                         struct v4l2_frmsizeenum *f)
1425         struct vip_stream *stream = file2stream(file);
1426         struct vip_dev *dev = stream->port->dev;
1427         struct vip_fmt *fmt;
1428         int ret;
1430         fmt = find_active_format_by_pix(dev, f->pixel_format);
1431         if (!fmt)
1432                 return -EINVAL;
1434         ret = v4l2_subdev_call(dev->sensor, video, enum_framesizes, f);
1435         if (ret)
1436                 vip_dbg(1, dev, "enum_framesizes failed in subdev\n");
1438         return ret;
1441 static int vip_enum_frameintervals(struct file *file, void *priv,
1442                         struct v4l2_frmivalenum *f)
1444         struct vip_stream *stream = file2stream(file);
1445         struct vip_dev *dev = stream->port->dev;
1446         struct v4l2_frmsizeenum fsize;
1447         struct vip_fmt *fmt;
1448         int ret;
1450         if (f->index)
1451                 return -EINVAL;
1453         fmt = find_active_format_by_pix(dev, f->pixel_format);
1454         if (!fmt)
1455                 return -EINVAL;
1457         /* check for valid width/height */
1458         ret = 0;
1459         for (fsize.index = 0; ; fsize.index++) {
1460                 ret = v4l2_subdev_call(dev->sensor, video,
1461                                         enum_framesizes, &fsize);
1462                 if (ret) {
1463                         if (fsize.index == 0)
1464                                 vip_dbg(1, dev, "enum_frameinterval failed on the first enum_framesize\n");
1465                         return -EINVAL;
1466                 }
1468                 if (fsize.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
1469                         if ((f->width == fsize.discrete.width) &&
1470                             (f->height == fsize.discrete.height))
1471                                 break;
1472                 } else if ((fsize.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) ||
1473                            (fsize.type == V4L2_FRMSIZE_TYPE_STEPWISE)) {
1474                         if ((f->width >= fsize.stepwise.min_width) &&
1475                             (f->width <= fsize.stepwise.max_width) &&
1476                             (f->height >= fsize.stepwise.min_height) &&
1477                             (f->height <= fsize.stepwise.max_height))
1478                                 break;
1479                 } else
1480                         return -EINVAL;
1481         }
1483         f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1484         f->discrete.numerator = 1;
1485         f->discrete.denominator = 30;
1487         return 0;
1490 static int vip_s_parm(struct file *file, void *priv,
1491                         struct v4l2_streamparm *parm)
1493         if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1494                 return -EINVAL;
1496         parm->parm.capture.timeperframe.numerator = 1;
1497         parm->parm.capture.timeperframe.denominator = 30;
1499         return 0;
1502 static int vip_try_fmt_vid_cap(struct file *file, void *priv,
1503                                struct v4l2_format *f)
1505         struct vip_stream *stream = file2stream(file);
1506         struct vip_dev *dev = stream->port->dev;
1507         struct vip_fmt *fmt = find_active_format_by_pix(dev,
1508                                                         f->fmt.pix.pixelformat);
1509         enum v4l2_field field;
1511         if (!fmt) {
1512                 vip_err(dev,
1513                         "Fourcc format (0x%08x) invalid.\n",
1514                         f->fmt.pix.pixelformat);
1515                 return -EINVAL;
1516         }
1518         field = f->fmt.pix.field;
1520         if (field == V4L2_FIELD_ANY)
1521                 field = V4L2_FIELD_NONE;
1522         else if (V4L2_FIELD_NONE != field && V4L2_FIELD_ALTERNATE != field)
1523                 return -EINVAL;
1525         f->fmt.pix.field = field;
1527         v4l_bound_align_image(&f->fmt.pix.width, MIN_W, MAX_W, W_ALIGN,
1528                               &f->fmt.pix.height, MIN_H, MAX_H, H_ALIGN,
1529                               S_ALIGN);
1531         f->fmt.pix.bytesperline = f->fmt.pix.width *
1532                                   (fmt->vpdma_fmt[0]->depth >> 3);
1533         f->fmt.pix.bytesperline = ALIGN(f->fmt.pix.bytesperline,
1534                                         VPDMA_STRIDE_ALIGN);
1535         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.width *
1536                 (fmt->vpdma_fmt[0]->depth +
1537                  (fmt->coplanar ? fmt->vpdma_fmt[1]->depth : 0)) >> 3;
1538         f->fmt.pix.colorspace = fmt->colorspace;
1539         f->fmt.pix.priv = 0;
1541         vip_dbg(3, dev, "try_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1542                 fourcc_to_str(f->fmt.pix.pixelformat),
1543                 f->fmt.pix.width, f->fmt.pix.height,
1544                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1546         return 0;
1549 static int vip_g_fmt_vid_cap(struct file *file, void *priv,
1550                              struct v4l2_format *f)
1552         struct vip_stream *stream = file2stream(file);
1553         struct vip_port *port = stream->port;
1554         struct vip_dev *dev = stream->port->dev;
1555         struct v4l2_mbus_framefmt mbus_fmt;
1556         struct vip_fmt *fmt;
1557         struct v4l2_format try_f;
1558         int ret;
1560         /* Use last known values or defaults */
1561         f->fmt.pix.width        = stream->width;
1562         f->fmt.pix.height       = stream->height;
1563         f->fmt.pix.pixelformat  = port->fmt->fourcc;
1564         f->fmt.pix.field        = stream->sup_field;
1565         f->fmt.pix.colorspace   = port->fmt->colorspace;
1566         f->fmt.pix.bytesperline = stream->bytesperline;
1567         f->fmt.pix.sizeimage    = stream->sizeimage;
1569         /* Check with the subdevice */
1570         ret = v4l2_subdev_call(dev->sensor, video, g_mbus_fmt, &mbus_fmt);
1571         if (ret)
1572                 vip_dbg(1, dev, "g_mbus_fmt failed in subdev\n");
1574         fmt = find_active_format_by_code(dev, mbus_fmt.code);
1575         if (!fmt) {
1576                 vip_err(dev,
1577                         "mbus_code (0x%08x) invalid.\n",
1578                         mbus_fmt.code);
1579                 return -EINVAL;
1580         }
1582         vip_dbg(3, dev, "g_fmt subdev mbus_code: %04X fourcc:%s size: %dx%d\n",
1583                 fmt->code,
1584                 fourcc_to_str(fmt->fourcc),
1585                 mbus_fmt.width, mbus_fmt.height);
1587         /*
1588          * Run a try_fmt call to properly calculate
1589          * the sizeimage and bytesperline values
1590          * in case the defaults were not accurate.
1591          */
1592         try_f = *f;
1593         try_f.fmt.pix.pixelformat = fmt->fourcc;
1594         try_f.fmt.pix.width = mbus_fmt.width;
1595         try_f.fmt.pix.height = mbus_fmt.height;
1596         try_f.fmt.pix.field = mbus_fmt.field;
1597         try_f.fmt.pix.colorspace = mbus_fmt.colorspace;
1599         ret = vip_try_fmt_vid_cap(file, priv, &try_f);
1600         if (ret)
1601                 return ret;
1603         if (port->fmt != fmt) {
1604                 vip_dbg(1, dev, "g_fmt fmt mismatch port->fmt:%p fmt:%p\n",
1605                         port->fmt, fmt);
1606                 vip_dbg(1, dev, "g_fmt port->fmt->fourcc:%s\n",
1607                         fourcc_to_str(port->fmt->fourcc));
1608                 vip_dbg(1, dev, "fmt->fourcc:%s\n",
1609                         fourcc_to_str(fmt->fourcc));
1610                 vip_dbg(1, dev, "g_fmt port->fmt->name:%s fmt->name:%s\n",
1611                         port->fmt->name, fmt->name);
1612                 port->fmt = fmt;
1613         }
1614         /*
1615          * Since everything looks correct update
1616          * the local copy as well to make sure we are consistent
1617          */
1618         *f = try_f;
1619         stream->width = f->fmt.pix.width;
1620         stream->height = f->fmt.pix.height;
1621         stream->sup_field = f->fmt.pix.field;
1622         if (stream->sup_field == V4L2_FIELD_ALTERNATE)
1623                 port->flags |= FLAG_INTERLACED;
1624         else
1625                 port->flags &= ~FLAG_INTERLACED;
1626         stream->bytesperline = f->fmt.pix.bytesperline;
1627         stream->sizeimage = f->fmt.pix.sizeimage;
1628         port->c_rect.left       = 0;
1629         port->c_rect.top        = 0;
1630         port->c_rect.width      = stream->width;
1631         port->c_rect.height     = stream->height;
1633         vip_dbg(3, dev, "g_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1634                 fourcc_to_str(f->fmt.pix.pixelformat),
1635                 f->fmt.pix.width, f->fmt.pix.height,
1636                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1637         vip_dbg(3, dev, "g_fmt vpdma data type: 0x%02X\n",
1638                 port->fmt->vpdma_fmt[0]->data_type);
1640         return 0;
1643 /*
1644  * Set the registers that are modified when the video format changes.
1645  */
1646 static void set_fmt_params(struct vip_stream *stream)
1648         struct vip_dev *dev = stream->port->dev;
1649         int data_path_reg;
1651         stream->sequence = 0;
1652         stream->field = V4L2_FIELD_TOP;
1654         if (stream->port->fmt->colorspace == V4L2_COLORSPACE_SRGB) {
1655                 vip_set_slice_path(dev, VIP_RGB_OUT_LO_DATA_SELECT);
1656                 /* Set alpha component in background color */
1657                 vpdma_set_bg_color(dev->shared->vpdma,
1658                         (struct vpdma_data_format *)
1659                          stream->port->fmt->vpdma_fmt[0],
1660                         0xff);
1661         }
1663         data_path_reg = VIP_VIP1_DATA_PATH_SELECT + 4 * dev->slice_id;
1664         if (stream->port->fmt->coplanar) {
1665                 stream->port->flags &= ~FLAG_MULT_PORT;
1666                 write_vreg(dev, data_path_reg, 0x600);
1667         } else {
1668                 stream->port->flags |= FLAG_MULT_PORT;
1669                 write_vreg(dev, data_path_reg, 0x8000);
1670         }
1673 int vip_s_fmt_vid_cap(struct file *file, void *priv,
1674                              struct v4l2_format *f)
1676         struct vip_stream *stream = file2stream(file);
1677         struct vip_port *port = stream->port;
1678         struct vip_dev *dev = port->dev;
1679         struct v4l2_subdev_format sfmt;
1680         struct v4l2_mbus_framefmt *mf;
1681         int ret;
1683         vip_dbg(3, dev, "s_fmt input fourcc:%s size: %dx%d\n",
1684                 fourcc_to_str(f->fmt.pix.pixelformat),
1685                 f->fmt.pix.width, f->fmt.pix.height);
1687         ret = vip_try_fmt_vid_cap(file, priv, f);
1688         if (ret)
1689                 return ret;
1691         vip_dbg(3, dev, "s_fmt try_fmt fourcc:%s size: %dx%d\n",
1692                 fourcc_to_str(f->fmt.pix.pixelformat),
1693                 f->fmt.pix.width, f->fmt.pix.height);
1695         if (vb2_is_busy(&stream->vb_vidq)) {
1696                 vip_err(dev, "%s queue busy\n", __func__);
1697                 return -EBUSY;
1698         }
1700         port->fmt               = find_active_format_by_pix(dev,
1701                                         f->fmt.pix.pixelformat);
1702         stream->width           = f->fmt.pix.width;
1703         stream->height          = f->fmt.pix.height;
1704         if (port->fmt->colorspace != f->fmt.pix.colorspace)
1705                 vip_dbg(1, dev, "s_fmt colorspace mismatch port->fmt %d f->fmt %d\n",
1706                         port->fmt->colorspace, f->fmt.pix.colorspace);
1708         stream->bytesperline    = f->fmt.pix.bytesperline;
1709         stream->sizeimage       = f->fmt.pix.sizeimage;
1710         stream->sup_field       = f->fmt.pix.field;
1712         port->c_rect.left       = 0;
1713         port->c_rect.top        = 0;
1714         port->c_rect.width      = stream->width;
1715         port->c_rect.height     = stream->height;
1717         if (stream->sup_field == V4L2_FIELD_ALTERNATE)
1718                 port->flags |= FLAG_INTERLACED;
1719         else
1720                 port->flags &= ~FLAG_INTERLACED;
1722         vip_dbg(1, dev,
1723                 "Setting format for type %d, wxh: %dx%d, fourcc:%s\n",
1724                 f->type, stream->width, stream->height,
1725                 fourcc_to_str(port->fmt->fourcc));
1727         vip_dbg(3, dev, "s_fmt fourcc:%s size: %dx%d bpl:%d img_size:%d\n",
1728                 fourcc_to_str(f->fmt.pix.pixelformat),
1729                 f->fmt.pix.width, f->fmt.pix.height,
1730                 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
1732         mf = vip_video_pix_to_mbus(&f->fmt.pix, &sfmt.format);
1734         vip_dbg(3, dev, "s_fmt pix_to_mbus mbus_code: %04X size: %dx%d\n",
1735                 mf->code,
1736                 mf->width, mf->height);
1738         ret = v4l2_subdev_call(dev->sensor, video, try_mbus_fmt, mf);
1739         if (ret) {
1740                 vip_dbg(1, dev, "try_mbus_fmt failed in subdev\n");
1741                 return ret;
1742         }
1743         vip_dbg(3, dev, "s_fmt subdev try_fmt mbus_code: %04X size: %dx%d\n",
1744                 mf->code,
1745                 mf->width, mf->height);
1746         ret = v4l2_subdev_call(dev->sensor, video, s_mbus_fmt, mf);
1747         if (ret) {
1748                 vip_dbg(1, dev, "s_mbus_fmt failed in subdev\n");
1749                 return ret;
1750         }
1751         vip_dbg(3, dev, "s_fmt subdev s_fmt mbus_code: %04X size: %dx%d\n",
1752                 mf->code,
1753                 mf->width, mf->height);
1754         vip_dbg(3, dev, "s_fmt vpdma data type: 0x%02X\n",
1755                 port->fmt->vpdma_fmt[0]->data_type);
1757         return 0;
1759 EXPORT_SYMBOL(vip_s_fmt_vid_cap);
1761 static int vip_g_selection(struct file *file, void *fh,
1762                            struct v4l2_selection *s)
1764         struct vip_stream *stream = file2stream(file);
1766         switch (s->target) {
1767         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1768         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1769         case V4L2_SEL_TGT_CROP_BOUNDS:
1770         case V4L2_SEL_TGT_CROP_DEFAULT:
1771                 s->r.left = 0;
1772                 s->r.top = 0;
1773                 s->r.width = stream->width;
1774                 s->r.height = stream->height;
1775                 return 0;
1777         case V4L2_SEL_TGT_COMPOSE:
1778         case V4L2_SEL_TGT_CROP:
1779                 s->r = stream->port->c_rect;
1780                 return 0;
1781         }
1783         return -EINVAL;
1786 static int enclosed_rectangle(struct v4l2_rect *a, struct v4l2_rect *b)
1788         if (a->left < b->left || a->top < b->top)
1789                 return 0;
1790         if (a->left + a->width > b->left + b->width)
1791                 return 0;
1792         if (a->top + a->height > b->top + b->height)
1793                 return 0;
1795         return 1;
1798 static int vip_s_selection(struct file *file, void *fh,
1799                            struct v4l2_selection *s)
1801         struct vip_stream *stream = file2stream(file);
1802         struct vip_port *port = stream->port;
1803         struct v4l2_rect r = s->r;
1805         v4l_bound_align_image(&r.width, 0, stream->width, 0,
1806                               &r.height, 0, stream->height, 0, 0);
1808         r.left = clamp_t(unsigned int, r.left, 0, stream->width - r.width);
1809         r.top  = clamp_t(unsigned int, r.top, 0, stream->height - r.height);
1811         if (s->flags & V4L2_SEL_FLAG_LE && !enclosed_rectangle(&r, &s->r))
1812                 return -ERANGE;
1814         if (s->flags & V4L2_SEL_FLAG_GE && !enclosed_rectangle(&s->r, &r))
1815                 return -ERANGE;
1817         s->r = stream->port->c_rect = r;
1819         set_fmt_params(stream);
1821         vip_dbg(1, port->dev, "cropped (%d,%d)/%dx%d of %dx%d\n",
1822                 r.left, r.top, r.width, r.height,
1823                 stream->width, stream->height);
1825         return 0;
1828 static long vip_ioctl_default(struct file *file, void *fh, bool valid_prio,
1829                               unsigned int cmd, void *arg)
1831         struct vip_stream *stream = file2stream(file);
1832         struct vip_port *port = stream->port;
1834         if (!valid_prio) {
1835                 vip_err(port->dev, "%s device busy\n", __func__);
1836                 return -EBUSY;
1837         }
1839         switch (cmd) {
1840         default:
1841                 return -ENOTTY;
1842         }
1845 static const struct v4l2_ioctl_ops vip_ioctl_ops = {
1846         .vidioc_querycap        = vip_querycap,
1847         .vidioc_enum_input      = vip_enuminput,
1848         .vidioc_g_input         = vip_g_input,
1849         .vidioc_s_input         = vip_s_input,
1851         .vidioc_querystd        = vip_querystd,
1852         .vidioc_g_std           = vip_g_std,
1853         .vidioc_s_std           = vip_s_std,
1855         .vidioc_enum_fmt_vid_cap = vip_enum_fmt_vid_cap,
1856         .vidioc_g_fmt_vid_cap   = vip_g_fmt_vid_cap,
1857         .vidioc_try_fmt_vid_cap = vip_try_fmt_vid_cap,
1858         .vidioc_s_fmt_vid_cap   = vip_s_fmt_vid_cap,
1860         .vidioc_enum_frameintervals     = vip_enum_frameintervals,
1861         .vidioc_enum_framesizes         = vip_enum_framesizes,
1862         .vidioc_s_parm                  = vip_s_parm,
1864         .vidioc_g_selection     = vip_g_selection,
1865         .vidioc_s_selection     = vip_s_selection,
1866         .vidioc_reqbufs         = vb2_ioctl_reqbufs,
1867         .vidioc_create_bufs     = vb2_ioctl_create_bufs,
1868         .vidioc_prepare_buf     = vb2_ioctl_prepare_buf,
1869         .vidioc_querybuf        = vb2_ioctl_querybuf,
1870         .vidioc_qbuf            = vb2_ioctl_qbuf,
1871         .vidioc_dqbuf           = vb2_ioctl_dqbuf,
1873         .vidioc_streamon        = vb2_ioctl_streamon,
1874         .vidioc_streamoff       = vb2_ioctl_streamoff,
1875         .vidioc_log_status      = v4l2_ctrl_log_status,
1876         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1877         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1878         .vidioc_default         = vip_ioctl_default,
1879 };
1881 /*
1882  * Videobuf operations
1883  */
1884 static int vip_queue_setup(struct vb2_queue *vq,
1885                            const struct v4l2_format *fmt,
1886                            unsigned int *nbuffers, unsigned int *nplanes,
1887                            unsigned int sizes[], void *alloc_ctxs[])
1889         struct vip_stream *stream = vb2_get_drv_priv(vq);
1890         struct vip_dev *dev = stream->port->dev;
1892         *nplanes = 1;
1893         sizes[0] = stream->sizeimage;
1894         alloc_ctxs[0] = dev->alloc_ctx;
1895         vip_dbg(1, dev, "get %d buffer(s) of size %d each.\n",
1896                 *nbuffers, sizes[0]);
1898         return 0;
1901 static int vip_buf_prepare(struct vb2_buffer *vb)
1903         struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
1904         struct vip_dev *dev = stream->port->dev;
1906         if (vb2_plane_size(vb, 0) < stream->sizeimage) {
1907                 vip_dbg(1, dev,
1908                         "%s data will not fit into plane (%lu < %lu)\n",
1909                         __func__, vb2_plane_size(vb, 0),
1910                         (long)stream->sizeimage);
1911                 return -EINVAL;
1912         }
1914         vb2_set_plane_payload(vb, 0, stream->sizeimage);
1916         return 0;
1919 static void vip_buf_queue(struct vb2_buffer *vb)
1921         struct vip_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
1922         struct vip_dev *dev = stream->port->dev;
1923         struct vip_buffer *buf = container_of(vb, struct vip_buffer, vb);
1924         unsigned long flags;
1926         spin_lock_irqsave(&dev->slock, flags);
1927         list_add_tail(&buf->list, &stream->vidq);
1928         spin_unlock_irqrestore(&dev->slock, flags);
1931 static int vip_start_streaming(struct vb2_queue *vq, unsigned int count)
1933         struct vip_stream *stream = vb2_get_drv_priv(vq);
1934         struct vip_port *port = stream->port;
1935         struct vip_dev *dev = port->dev;
1936         struct vip_buffer *buf;
1937         unsigned long flags;
1938         int ret;
1940         set_fmt_params(stream);
1941         vip_setup_parser(dev->ports[0]);
1943         buf = list_entry(stream->vidq.next,
1944                          struct vip_buffer, list);
1946         vip_dbg(2, dev, "start_streaming: buf 0x%x %d\n",
1947                 (unsigned int)buf, count);
1948         buf->drop = false;
1949         stream->sequence = 0;
1950         stream->field = V4L2_FIELD_TOP;
1952         if (dev->sensor) {
1953                 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 1);
1954                 if (ret) {
1955                         vip_dbg(1, dev, "stream on failed in subdev\n");
1956                         return ret;
1957                 }
1958         }
1960         populate_desc_list(stream);
1962         /* The first few VPDMA ListComplete interrupts fire pretty quiclky
1963          * until the internal VPDMA descriptor fifo is full.
1964          * The subsequent ListComplete interrupts will fire at the actual
1965          * capture frame rate. The first few interrupts are therefore used
1966          * only to queue up descriptors, and then they will also be used
1967          * as End of Frame (EOF) event
1968          */
1969         dev->num_skip_irq = VIP_VPDMA_FIFO_SIZE;
1971         spin_lock_irqsave(&dev->slock, flags);
1972         if (vpdma_list_busy(dev->shared->vpdma, stream->list_num)) {
1973                 spin_unlock_irqrestore(&dev->slock, flags);
1974                 vpdma_unmap_desc_buf(dev->shared->vpdma,
1975                                 &stream->desc_list.buf);
1976                 vpdma_reset_desc_list(&stream->desc_list);
1977                 return -EBUSY;
1978         }
1980         list_move_tail(&buf->list, &stream->post_bufs);
1981         spin_unlock_irqrestore(&dev->slock, flags);
1983         vip_dbg(2, dev, "start_streaming: start_dma buf 0x%x\n",
1984                 (unsigned int)buf);
1985         start_dma(stream, buf);
1987         /* We enable the irq after posting the vpdma descriptor
1988          * to prevent sprurious interrupt coming in before the
1989          * vb2 layer is completely ready to handle them
1990          * otherwise the vb2_streaming test would fail early on
1991           */
1992         enable_irqs(dev, dev->slice_id, stream->list_num);
1994         return 0;
1997 /*
1998  * Abort streaming and wait for last buffer
1999  */
2000 static int vip_stop_streaming(struct vb2_queue *vq)
2002         struct vip_stream *stream = vb2_get_drv_priv(vq);
2003         struct vip_port *port = stream->port;
2004         struct vip_dev *dev = port->dev;
2005         struct vip_buffer *buf;
2006         int ret;
2008         if (dev->sensor) {
2009                 ret = v4l2_subdev_call(dev->sensor, video, s_stream, 0);
2010                 if (ret)
2011                         vip_dbg(1, dev, "stream on failed in subdev\n");
2012         }
2014         disable_irqs(dev, dev->slice_id, stream->list_num);
2015         clear_irqs(dev, dev->slice_id, stream->list_num);
2016         stop_dma(stream);
2018         /* release all active buffers */
2019         while (!list_empty(&stream->post_bufs)) {
2020                 buf = list_entry(stream->post_bufs.next,
2021                                         struct vip_buffer, list);
2022                 list_del(&buf->list);
2023                 if (buf->drop == 1)
2024                         list_add_tail(&buf->list, &stream->dropq);
2025                 else
2026                         vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
2027         }
2028         while (!list_empty(&stream->vidq)) {
2029                 buf = list_entry(stream->vidq.next, struct vip_buffer, list);
2030                 list_del(&buf->list);
2031                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
2032         }
2034         if (!vb2_is_streaming(vq))
2035                 return 0;
2037         vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
2038         vpdma_reset_desc_list(&stream->desc_list);
2040         return 0;
2043 /*
2044  * Lock access to the device
2045  */
2046 static void vip_lock(struct vb2_queue *vq)
2048         struct vip_stream *stream = vb2_get_drv_priv(vq);
2050         mutex_lock(&stream->port->dev->mutex);
2053 static void vip_unlock(struct vb2_queue *vq)
2055         struct vip_stream *stream = vb2_get_drv_priv(vq);
2056         mutex_unlock(&stream->port->dev->mutex);
2059 static struct vb2_ops vip_video_qops = {
2060         .queue_setup            = vip_queue_setup,
2061         .buf_prepare            = vip_buf_prepare,
2062         .buf_queue              = vip_buf_queue,
2063         .start_streaming        = vip_start_streaming,
2064         .stop_streaming         = vip_stop_streaming,
2065         .wait_prepare           = vip_unlock,
2066         .wait_finish            = vip_lock,
2067 };
2069 /*
2070  * File operations
2071  */
2073 static int vip_init_dev(struct vip_dev *dev)
2075         if (dev->num_ports != 0)
2076                 goto done;
2078         vip_set_clock_enable(dev, 1);
2079 done:
2080         dev->num_ports++;
2082         return 0;
2085 static int vip_init_port(struct vip_port *port)
2087         int ret;
2089         if (port->num_streams != 0)
2090                 goto done;
2092         ret = vip_init_dev(port->dev);
2093         if (ret)
2094                 goto done;
2096         port->fmt = port->dev->active_fmt[0];
2097         port->src_colorspace = port->fmt->colorspace;
2098         port->c_rect.left = 0;
2099         port->c_rect.top = 0;
2101 done:
2102         port->num_streams++;
2103         return 0;
2106 static int vip_init_stream(struct vip_stream *stream)
2108         int ret;
2110         ret = vip_init_port(stream->port);
2111         if (ret != 0)
2112                 return ret;
2114         ret = vpdma_create_desc_list(&stream->desc_list, VIP_DESC_LIST_SIZE,
2115                         VPDMA_LIST_TYPE_NORMAL);
2117         if (ret != 0)
2118                 return ret;
2120         stream->write_desc = (struct vpdma_dtd *)stream->desc_list.buf.addr
2121                                 + 15;
2122         return 0;
2125 static void vip_release_dev(struct vip_dev *dev)
2127         /*
2128          * On last close, disable clocks to conserve power
2129          */
2131         if (--dev->num_ports == 0)
2132                 vip_set_clock_enable(dev, 0);
2135 static int vip_setup_parser(struct vip_port *port)
2137         struct vip_dev *dev = port->dev;
2138         struct v4l2_of_endpoint *endpoint = dev->endpoint;
2139         int iface = DUAL_8B_INTERFACE;
2140         int sync_type, pclk_type;
2141         unsigned int flags;
2143         flags = endpoint->bus.parallel.flags;
2144         vip_reset_port(port);
2145         vip_set_port_enable(port, 1);
2147         if (endpoint->bus_type == V4L2_MBUS_BT656) {
2148                 iface = DUAL_8B_INTERFACE;
2150                 /* Ideally, this should come from sensor
2151                    port->fmt can be anything once CSC is enabled */
2152                 if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
2153                         sync_type = EMBEDDED_SYNC_SINGLE_RGB_OR_YUV444;
2154                 else {
2155                         switch (endpoint->bus.parallel.num_channels) {
2156                         case 4:
2157                                 sync_type = EMBEDDED_SYNC_4X_MULTIPLEXED_YUV422;
2158                         break;
2159                         case 2:
2160                                 sync_type = EMBEDDED_SYNC_2X_MULTIPLEXED_YUV422;
2161                         break;
2162                         case 1:
2163                         default:
2164                                 sync_type =
2165                                 EMBEDDED_SYNC_LINE_MULTIPLEXED_YUV422;
2166                         }
2167                 }
2169         } else if (endpoint->bus_type == V4L2_MBUS_PARALLEL) {
2170                 switch (endpoint->bus.parallel.bus_width) {
2171                 case 24:
2172                         iface = SINGLE_24B_INTERFACE;
2173                 break;
2174                 case 16:
2175                         iface = SINGLE_16B_INTERFACE;
2176                 break;
2177                 case 8:
2178                 default:
2179                         iface = DUAL_8B_INTERFACE;
2180                 }
2182                 if (port->fmt->colorspace == V4L2_COLORSPACE_SRGB)
2183                         sync_type = DISCRETE_SYNC_SINGLE_RGB_24B;
2184                 else
2185                         sync_type = DISCRETE_SYNC_SINGLE_YUV422;
2187                 if (flags & (V4L2_MBUS_HSYNC_ACTIVE_HIGH |
2188                         V4L2_MBUS_HSYNC_ACTIVE_LOW))
2189                         vip_set_vsync_polarity(port,
2190                                 flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH ? 1 : 0);
2192                 if (flags & (V4L2_MBUS_VSYNC_ACTIVE_HIGH |
2193                         V4L2_MBUS_VSYNC_ACTIVE_LOW))
2194                         vip_set_hsync_polarity(port,
2195                                 flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH ? 1 : 0);
2197                 vip_xtra_set_repack_sel(port, 0);
2198                 vip_set_actvid_hsync_n(port, 0);
2199                 vip_set_actvid_polarity(port, 1);
2200                 vip_set_discrete_basic_mode(port);
2202         } else {
2203                 vip_err(dev, "Device doesn't support CSI2");
2204                 return -EINVAL;
2205         }
2207         pclk_type = flags & V4L2_MBUS_PCLK_SAMPLE_RISING ? 0 : 1;
2208         vip_set_pclk_polarity(port, pclk_type);
2209         vip_set_data_interface(port, iface);
2210         vip_sync_type(port, sync_type);
2211         return 0;
2214 static void vip_release_stream(struct vip_stream *stream)
2216         struct vip_dev *dev = stream->port->dev;
2218         vpdma_unmap_desc_buf(dev->shared->vpdma, &stream->desc_list.buf);
2219         vpdma_free_desc_buf(&stream->desc_list.buf);
2220         vpdma_free_desc_list(&stream->desc_list);
2223 static void stop_dma(struct vip_stream *stream)
2225         struct vip_dev *dev = stream->port->dev;
2226         int ch, size = 0;
2228         /* Create a list of channels to be cleared */
2229         for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++) {
2230                 if (stream->vpdma_channels[ch] == 1) {
2231                         stream->vpdma_channels[size++] = ch;
2232                         vip_dbg(2, dev, "Clear channel no: %d\n", ch);
2233                 }
2234         }
2236         /* Clear all the used channels for the list */
2237         vpdma_list_cleanup(dev->shared->vpdma, stream->list_num,
2238                 stream->vpdma_channels, size);
2240         for (ch = 0; ch < VPDMA_MAX_CHANNELS; ch++)
2241                 stream->vpdma_channels[ch] = 0;
2244 int vip_open(struct file *file)
2246         struct vip_stream *stream = video_drvdata(file);
2247         struct vip_port *port = stream->port;
2248         struct vip_dev *dev = port->dev;
2249         struct v4l2_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
2250         int ret = 0;
2252         vip_dbg(2, dev, "vip_open\n");
2254         file->private_data = fh;
2255         if (fh == NULL)
2256                 return -ENOMEM;
2258         mutex_lock(&dev->mutex);
2260         v4l2_fh_init(fh, video_devdata(file));
2261         v4l2_fh_add(fh);
2263         /*
2264          * If this is the first open file.
2265          * Then initialize hw module.
2266          */
2267         if (v4l2_fh_is_singular_file(file)) {
2268                 if (vip_init_stream(stream)) {
2269                         goto free_fh;
2270                         ret = -ENODEV;
2271                 }
2272                 stream->width = 1280;
2273                 stream->height = 720;
2274                 stream->sizeimage = stream->width * stream->height *
2275                         (port->fmt->vpdma_fmt[0]->depth +
2276                         (port->fmt->coplanar ?
2277                                 port->fmt->vpdma_fmt[1]->depth : 0)) >> 3;
2278                 stream->bytesperline = round_up((stream->width *
2279                                         port->fmt->vpdma_fmt[0]->depth) >> 3,
2280                                         1 << L_ALIGN);
2281                 stream->sup_field = V4L2_FIELD_NONE;
2282                 port->c_rect.width = stream->width;
2283                 port->c_rect.height = stream->height;
2284                 vip_dbg(1, dev, "Created stream instance %p\n", stream);
2285         }
2287         mutex_unlock(&dev->mutex);
2288         return 0;
2290 free_fh:
2291         mutex_unlock(&dev->mutex);
2292         if (fh) {
2293                 v4l2_fh_del(fh);
2294                 v4l2_fh_exit(fh);
2295                 kfree(fh);
2296         }
2297         return ret;
2299 EXPORT_SYMBOL(vip_open);
2301 int vip_release(struct file *file)
2303         struct vip_stream *stream = video_drvdata(file);
2304         struct vip_port *port = stream->port;
2305         struct vip_dev *dev = port->dev;
2306         struct vb2_queue *q = &stream->vb_vidq;
2308         vip_dbg(2, dev, "vip_release\n");
2310         /*
2311          * If this is the last open file.
2312          * Then de-initialize hw module.
2313          */
2314         if (v4l2_fh_is_singular_file(file)) {
2315                 mutex_lock(&dev->mutex);
2317                 vip_stop_streaming(q);
2318                 vip_release_stream(stream);
2320                 if (--port->num_streams == 0)
2321                         vip_release_dev(port->dev);
2323                 mutex_unlock(&dev->mutex);
2324                 vip_dbg(1, dev, "Releasing stream instance %p\n", stream);
2325         }
2327         return vb2_fop_release(file);
2329 EXPORT_SYMBOL(vip_release);
2331 static const struct v4l2_file_operations vip_fops = {
2332         .owner          = THIS_MODULE,
2333         .open           = vip_open,
2334         .release        = vip_release,
2335         .poll           = vb2_fop_poll,
2336         .unlocked_ioctl = video_ioctl2,
2337         .mmap           = vb2_fop_mmap,
2338 };
2340 static struct video_device vip_videodev = {
2341         .name           = VIP_MODULE_NAME,
2342         .fops           = &vip_fops,
2343         .ioctl_ops      = &vip_ioctl_ops,
2344         .minor          = -1,
2345         .release        = video_device_release,
2346 };
2348 static int alloc_stream(struct vip_port *port, int stream_id, int vfl_type)
2350         struct vip_stream *stream;
2351         struct vip_dev *dev = port->dev;
2352         struct vb2_queue *q;
2353         struct video_device *vfd;
2354         struct vip_buffer *buf;
2355         int ret, i;
2357         stream = kzalloc(sizeof(*stream), GFP_KERNEL);
2358         if (!stream)
2359                 return -ENOMEM;
2361         stream->port = port;
2362         stream->stream_id = stream_id;
2363         stream->vfl_type = vfl_type;
2365         stream->list_num = vpdma_hwlist_alloc(dev->shared->vpdma, stream);
2366         if (stream->list_num < 0) {
2367                 vip_err(dev, "Could not get VPDMA hwlist");
2368                 ret = -ENODEV;
2369                 goto do_free_stream;
2370         }
2372         INIT_LIST_HEAD(&stream->post_bufs);
2374         if (vfl_type == VFL_TYPE_GRABBER)
2375                 port->cap_streams[stream_id] = stream;
2376         else
2377                 port->vbi_streams[stream_id] = stream;
2379         /*
2380          * Initialize queue
2381          */
2382         q = &stream->vb_vidq;
2383         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2384         q->io_modes = VB2_MMAP | VB2_DMABUF;
2385         q->drv_priv = stream;
2386         q->buf_struct_size = sizeof(struct vip_buffer);
2387         q->ops = &vip_video_qops;
2388         q->mem_ops = &vb2_dma_contig_memops;
2389         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2391         /* Feature not back-ported yet. Enable when available */
2392         /* q->min_buffers_needed = 3; */
2394         ret = vb2_queue_init(q);
2395         if (ret)
2396                 goto do_free_stream;
2398         INIT_LIST_HEAD(&stream->vidq);
2400         /* Allocate/populate Drop queue entries */
2401         INIT_LIST_HEAD(&stream->dropq);
2402         for (i = 0; i < VIP_DROPQ_SIZE; i++) {
2404                 buf = kzalloc(sizeof(*buf), GFP_ATOMIC);
2405                 if (!buf) {
2406                         vip_err(dev, "No memory!!");
2407                         ret = -ENOMEM;
2408                         goto do_free_stream;
2409                 }
2410                 buf->drop = true;
2411                 list_add(&buf->list, &stream->dropq);
2412         }
2414         vfd = &stream->vdev;
2415         *vfd = vip_videodev;
2416         vfd->v4l2_dev = &dev->v4l2_dev;
2417         vfd->queue = q;
2418         set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
2420         vfd->lock = &dev->mutex;
2421         video_set_drvdata(vfd, stream);
2423         ret = video_register_device(vfd, vfl_type, -1);
2424         if (ret) {
2425                 vip_err(dev, "Failed to register video device\n");
2426                 goto do_free_stream;
2427         }
2429         snprintf(vfd->name, sizeof(vfd->name), "%s", vip_videodev.name);
2430         stream->vfd = vfd;
2432         vip_info(dev, VIP_MODULE_NAME
2433                  " Device registered as /dev/video%d\n", vfd->num);
2434         return 0;
2436 do_free_stream:
2437         kfree(stream);
2438         return ret;
2441 static void free_stream(struct vip_stream *stream)
2443         struct vip_dev *dev = stream->port->dev;
2444         struct vip_buffer *buf;
2445         struct list_head *pos, *q;
2447         if (!stream)
2448                 return;
2450         /* Free up the Drop queue */
2451         list_for_each_safe(pos, q, &stream->dropq) {
2452                 buf = list_entry(stream->dropq.next,
2453                                  struct vip_buffer, list);
2454                 vip_dbg(1, dev, "dropq buffer\n");
2455                 list_del(pos);
2456                 kfree(buf);
2457         }
2459         video_unregister_device(stream->vfd);
2460         video_device_release(stream->vfd);
2461         vpdma_hwlist_release(dev->shared->vpdma, stream->list_num);
2462         kfree(stream);
2465 static int alloc_port(struct vip_dev *dev, int id)
2467         struct vip_port *port;
2468         int ret;
2470         port = kzalloc(sizeof(*port), GFP_KERNEL);
2471         if (!port)
2472                 return -ENOMEM;
2474         dev->ports[id] = port;
2475         port->dev = dev;
2476         port->port_id = id;
2477         port->num_streams = 0;
2478         port->flags |= FLAG_MULT_PORT;
2480         ret = alloc_stream(port, 0, VFL_TYPE_GRABBER);
2482         if (dev->endpoint->bus_type == V4L2_MBUS_BT656) {
2483                 /* Allocate streams for 4 channels */
2484                 ret = alloc_stream(port, 2, VFL_TYPE_GRABBER);
2485                 ret = alloc_stream(port, 4, VFL_TYPE_GRABBER);
2486                 ret = alloc_stream(port, 6, VFL_TYPE_GRABBER);
2487         }
2489         return 0;
2492 static void free_port(struct vip_port *port)
2494         if (!port)
2495                 return;
2497         free_stream(port->cap_streams[0]);
2499         kfree(port);
2502 static int get_field(u32 value, u32 mask, int shift)
2504         return (value & (mask << shift)) >> shift;
2507 static int vip_of_probe(struct platform_device *pdev, struct vip_dev *dev);
2508 static void vip_vpdma_fw_cb(struct platform_device *pdev)
2510         struct vip_shared *shared = platform_get_drvdata(pdev);
2511         struct vip_dev *dev;
2512         int slice, ret;
2514         dev_info(&pdev->dev, "VPDMA firmware loaded\n");
2516         for (slice = 0; slice < atomic_read(&shared->devs_allocated); slice++) {
2517                 dev = shared->devs[slice];
2519                 if (pdev->dev.of_node) {
2520                         ret = vip_of_probe(pdev, dev);
2521                         if (ret)
2522                                 goto free_port;
2523                 }
2524         }
2526         return;
2528 free_port:
2529         free_port(dev->ports[0]);
2532 static void remove_shared(struct vip_shared *shared)
2534         if (atomic_dec_return(&shared->devs_allocated) != 0)
2535                 return;
2537         iounmap(shared->base);
2538         release_mem_region(shared->res->start, resource_size(shared->res));
2539         kfree(shared);
2542 static int vip_runtime_get(struct platform_device *pdev)
2544         int r;
2546         r = pm_runtime_get_sync(&pdev->dev);
2547         WARN_ON(r < 0);
2548         return r < 0 ? r : 0;
2551 static int get_subdev_active_format(struct vip_dev *dev,
2552                                     struct v4l2_subdev *subdev)
2554         struct vip_fmt *fmt;
2555         enum v4l2_mbus_pixelcode code;
2556         int ret = 0;
2557         unsigned int k;
2559         /* first find how many formats to allocate the correct size */
2560         dev->num_active_fmt = 0;
2561         for (k = 0;
2562              (ret != -EINVAL) && (dev->num_active_fmt < VIP_MAX_ACTIVE_FMT);
2563              k++) {
2564                 ret = v4l2_subdev_call(subdev, video, enum_mbus_fmt, k, &code);
2565                 if (ret == 0) {
2566                         fmt = find_format_by_code(code);
2567                         if (fmt) {
2568                                 dev->active_fmt[dev->num_active_fmt] = fmt;
2569                                 dev->num_active_fmt++;
2570                         }
2571                 }
2572         }
2574         if (find_active_format_by_pix(dev, V4L2_PIX_FMT_YUYV))
2575                 /* When YUYV format is supported, NV12 can also be supported */
2576                 dev->active_fmt[dev->num_active_fmt++] = &vip_formats[2];
2578         if (dev->num_active_fmt == 0) {
2580                 vip_err(dev, "No suitable format reported by subdev %s\n",
2581                         subdev->name);
2582                 return -EINVAL;
2583         }
2585         return 0;
2588 static int vip_async_bound(struct v4l2_async_notifier *notifier,
2589                         struct v4l2_subdev *subdev,
2590                         struct v4l2_async_subdev *asd)
2592         struct vip_dev *dev = notifier_to_vip_dev(notifier);
2593         unsigned int idx = asd - &dev->config->asd[0];
2595         vip_dbg(1, dev, "vip_async_bound\n");
2596         if (idx > dev->config->asd_sizes)
2597                 return -EINVAL;
2599         if (get_subdev_active_format(dev, subdev))
2600                 return 0;
2602         if (dev->sensor) {
2603                 if (asd < dev->sensor->asd) {
2604                         /* Notified of a subdev earlier in the array */
2605                         dev->sensor = subdev;
2606                         dev->endpoint = &dev->config->endpoints[idx];
2607                         vip_info(dev, "Switching to subdev %s (High priority)",
2608                                  subdev->name);
2610                 } else
2611                         vip_info(dev, "Rejecting subdev %s (Low priority)",
2612                                  subdev->name);
2613                 return 0;
2614         }
2616         dev->sensor = subdev;
2617         dev->endpoint = &dev->config->endpoints[idx];
2618         vip_info(dev, "Using sensor %s for capture\n", subdev->name);
2619         alloc_port(dev, 0);
2621         return 0;
2624 static int vip_async_complete(struct v4l2_async_notifier *notifier)
2626         struct vip_dev *dev = notifier_to_vip_dev(notifier);
2627         vip_dbg(1, dev, "vip_async_complete\n");
2628         return 0;
2631 static struct device_node *
2632 of_get_next_port(const struct device_node *parent,
2633                  struct device_node *prev)
2635         struct device_node *port = NULL;
2637         if (!parent)
2638                 return NULL;
2640         if (!prev) {
2641                 struct device_node *ports;
2642                 /*
2643                  * It's the first call, we have to find a port subnode
2644                  * within this node or within an optional 'ports' node.
2645                  */
2646                 ports = of_get_child_by_name(parent, "ports");
2647                 if (ports)
2648                         parent = ports;
2650                 port = of_get_child_by_name(parent, "port");
2652                 /* release the 'ports' node */
2653                 of_node_put(ports);
2654         } else {
2655                 struct device_node *ports;
2657                 ports = of_get_parent(prev);
2658                 if (!ports)
2659                         return NULL;
2661                 do {
2662                         port = of_get_next_child(ports, prev);
2663                         if (!port) {
2664                                 of_node_put(ports);
2665                                 return NULL;
2666                         }
2667                         prev = port;
2668                 } while (of_node_cmp(port->name, "port") != 0);
2669         }
2671         return port;
2674 static struct device_node *
2675 of_get_next_endpoint(const struct device_node *parent,
2676                         struct device_node *prev)
2678         struct device_node *ep = NULL;
2680         if (!parent)
2681                 return NULL;
2683         do {
2684                 ep = of_get_next_child(parent, prev);
2685                 if (!ep)
2686                         return NULL;
2687                 prev = ep;
2688         } while (of_node_cmp(ep->name, "endpoint") != 0);
2690         return ep;
2693 static int vip_of_probe(struct platform_device *pdev, struct vip_dev *dev)
2695         struct device_node *ep_node = NULL, *port, *remote_ep,
2696                         *sensor_node, *parent;
2697         struct device_node *syscon_np;
2698         struct v4l2_of_endpoint *endpoint;
2699         struct v4l2_async_subdev *asd;
2700         u32 regval = 0;
2701         int ret, slice, i = 0, found_port = 0;
2703         parent = pdev->dev.of_node;
2705         syscon_np = of_parse_phandle(pdev->dev.of_node, "syscon", 0);
2706         dev->syscon = syscon_node_to_regmap(syscon_np);
2707         of_node_put(syscon_np);
2709         dev->config = kzalloc(sizeof(struct vip_config), GFP_KERNEL);
2710         if (!dev->config)
2711                 return -ENOMEM;
2713         dev->config->card_name = "VIP Driver";
2715         port = NULL;
2716         vip_dbg(3, dev, "Scanning Port node for slice id: %d\n", dev->slice_id);
2717         for (slice = 0; slice < VIP_NUM_SLICES; slice++) {
2718                 port = of_get_next_port(parent, port);
2719                 if (!port) {
2720                         vip_dbg(1, dev, "No port node found for slice_id:%d\n",
2721                                 slice);
2722                         ret = -EINVAL;
2723                         goto free_config;
2724                 }
2726                 /* Match the slice number with <REG> */
2727                 of_property_read_u32(port, "reg", &regval);
2728                 vip_dbg(3, dev, "slice:%d dev->slice_id:%d <reg>:%d\n",
2729                         slice, dev->slice_id, regval);
2730                 if ((regval == dev->slice_id) && (slice == dev->slice_id)) {
2731                         found_port = 1;
2732                         break;
2733                 }
2734         }
2736         if (!found_port) {
2737                 if (!port)
2738                         of_node_put(port);
2739                 vip_dbg(1, dev, "No port node matches slice_id:%d\n",
2740                         dev->slice_id);
2741                 ret = -EINVAL;
2742                 goto free_config;
2743         }
2745         vip_dbg(3, dev, "Scanning sub-device(s) for slice id: %d\n",
2746                 dev->slice_id);
2747         while (i < VIP_MAX_SUBDEV) {
2749                 asd = &dev->config->asd[i];
2750                 endpoint = &dev->config->endpoints[i];
2752                 remote_ep = NULL;
2753                 sensor_node = NULL;
2755                 ep_node = of_get_next_endpoint(port, ep_node);
2756                 if (!ep_node) {
2757                         vip_dbg(3, dev, "can't get next endpoint: loop: %d\n",
2758                                 i);
2759                         break;
2760                 }
2762                 sensor_node = of_graph_get_remote_port_parent(ep_node);
2763                 if (!sensor_node) {
2764                         vip_dbg(3, dev, "can't get remote parent: loop: %d\n",
2765                                 i);
2766                         goto of_node_cleanup;
2767                 }
2768                 asd->match_type = V4L2_ASYNC_MATCH_OF;
2769                 asd->match.of.node = sensor_node;
2771                 remote_ep = of_parse_phandle(ep_node, "remote-endpoint", 0);
2772                 if (!remote_ep) {
2773                         vip_dbg(3, dev, "can't get remote-endpoint: loop: %d\n",
2774                                 i);
2775                         goto of_node_cleanup;
2776                 }
2777                 v4l2_of_parse_endpoint(remote_ep, endpoint);
2779                 dev->config->asd_list[i++] = asd;
2781 of_node_cleanup:
2782                 if (!remote_ep)
2783                         of_node_put(remote_ep);
2784                 if (!sensor_node)
2785                         of_node_put(sensor_node);
2786         }
2788         if (!ep_node)
2789                 of_node_put(ep_node);
2790         if (!port)
2791                 of_node_put(port);
2793         vip_dbg(1, dev, "Found %d sub-device(s) for slice id: %d\n",
2794                 i, dev->slice_id);
2795         if (i > 0) {
2796                 dev->config->asd_sizes = i;
2797                 dev->notifier.subdevs = dev->config->asd_list;
2798                 dev->notifier.num_subdevs = dev->config->asd_sizes;
2799                 dev->notifier.bound = vip_async_bound;
2800                 dev->notifier.complete = vip_async_complete;
2802                 vip_dbg(1, dev, "registering the sync notifier for sensors %d",
2803                         i);
2804                 ret = v4l2_async_notifier_register(&dev->v4l2_dev,
2805                                                    &dev->notifier);
2806                 if (ret) {
2807                         vip_dbg(1, dev, "Error registering async notifier\n");
2808                         ret = -EINVAL;
2809                         goto free_config;
2810                 }
2811         }
2813         return 0;
2814 free_config:
2815         kfree(dev->config);
2816         return ret;
2819 static const struct of_device_id vip_of_match[];
2820 static int vip_probe(struct platform_device *pdev)
2822         struct vip_dev *dev;
2823         struct vip_shared *shared;
2824         const struct of_device_id *of_dev_id;
2825         struct pinctrl *pinctrl;
2826         int ret, slice = VIP_SLICE1;
2827         u32 tmp, pid;
2828         struct v4l2_ctrl_handler *hdl;
2830         pm_runtime_enable(&pdev->dev);
2832         ret = vip_runtime_get(pdev);
2833         if (ret)
2834                 goto err_runtime_get;
2836         of_dev_id = of_match_device(vip_of_match, &pdev->dev);
2837         if (!of_dev_id) {
2838                 dev_err(&pdev->dev, "%s: Unable to match device\n", __func__);
2839                 return -ENODEV;
2840         }
2842         shared = kzalloc(sizeof(*shared), GFP_KERNEL);
2843         if (!shared)
2844                 return -ENOMEM;
2846         shared->res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vip");
2847         if (shared->res == NULL) {
2848                 dev_err(&pdev->dev, "Missing platform resources data\n");
2849                 ret = -ENODEV;
2850                 goto free_shared;
2851         }
2853         pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
2854         if (IS_ERR(pinctrl)) {
2855                 dev_warn(&pdev->dev, "No explicit pinctrl resources data.\n");
2856         }
2858         if (devm_request_mem_region(&pdev->dev, shared->res->start,
2859             resource_size(shared->res), VIP_MODULE_NAME) == NULL) {
2860                 ret = -ENOMEM;
2861                 goto free_shared;
2862         }
2864         shared->base = devm_ioremap(&pdev->dev, shared->res->start,
2865                                     resource_size(shared->res));
2866         if (!shared->base) {
2867                 dev_err(&pdev->dev, "failed to ioremap\n");
2868                 ret = -ENOMEM;
2869                 goto rel_mem_region;
2870         }
2872         /* Make sure H/W module has the right functionality */
2873         pid = read_sreg(shared, VIP_PID);
2874         tmp = get_field(pid, VIP_PID_FUNC_MASK, VIP_PID_FUNC_SHIFT);
2876         if (tmp != VIP_PID_FUNC) {
2877                 dev_info(&pdev->dev, "vip: unexpected PID function: 0x%x\n",
2878                        tmp);
2879                 ret = -ENODEV;
2880                 goto do_iounmap;
2881         }
2883         /* enable clocks, so the firmware will load properly */
2884         vip_shared_set_clock_enable(shared, 1);
2885         vip_top_vpdma_reset(shared);
2887         list_add_tail(&shared->list, &vip_shared_list);
2888         platform_set_drvdata(pdev, shared);
2889         atomic_set(&shared->devs_allocated, 0);
2891         vip_set_idle_mode(shared, VIP_SMART_IDLE_MODE);
2892         vip_set_standby_mode(shared, VIP_SMART_STANDBY_MODE);
2894         for (slice = VIP_SLICE1; slice < VIP_NUM_SLICES; slice++) {
2895                 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2896                 if (!dev)
2897                         return -ENOMEM;
2899                 dev->instance_id = (int)of_dev_id->data;
2900                 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
2901                         "%s%d-s%d", VIP_MODULE_NAME, dev->instance_id, slice);
2903                 dev->irq = platform_get_irq(pdev, slice);
2904                 if (!dev->irq) {
2905                         dev_err(&pdev->dev, "Could not get IRQ");
2906                         goto err_runtime_get;
2907                 }
2909                 if (devm_request_irq(&pdev->dev, dev->irq, vip_irq,
2910                                      0, dev->v4l2_dev.name, dev) < 0) {
2911                         ret = -ENOMEM;
2912                         goto dev_unreg;
2913                 }
2915                 spin_lock_init(&dev->slock);
2916                 spin_lock_init(&dev->lock);
2918                 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2919                 if (ret)
2920                         goto err_runtime_get;
2922                 mutex_init(&dev->mutex);
2924                 hdl = &dev->ctrl_handler;
2925                 v4l2_ctrl_handler_init(hdl, 11);
2926                 dev->v4l2_dev.ctrl_handler = hdl;
2928                 dev->slice_id = slice;
2929                 dev->pdev = pdev;
2930                 dev->res = shared->res;
2931                 dev->base = shared->base;
2933                 dev->shared = shared;
2934                 shared->devs[slice] = dev;
2936                 atomic_inc(&shared->devs_allocated);
2938                 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
2939                 if (IS_ERR(dev->alloc_ctx)) {
2940                         vip_err(dev, "Failed to alloc vb2 context\n");
2941                         ret = PTR_ERR(dev->alloc_ctx);
2942                         goto dev_unreg;
2943                 }
2945                 vip_top_reset(dev);
2946                 vip_set_slice_path(dev, VIP_MULTI_CHANNEL_DATA_SELECT);
2947         }
2949         shared->vpdma = vpdma_create(pdev, vip_vpdma_fw_cb);
2950         if (!shared->vpdma) {
2951                 dev_err(&pdev->dev, "Creating VPDMA failed");
2952                 goto dev_unreg;
2953         }
2955         return 0;
2957 dev_unreg:
2958         v4l2_device_unregister(&dev->v4l2_dev);
2959 do_iounmap:
2960         iounmap(shared->base);
2961 rel_mem_region:
2962         release_mem_region(shared->res->start, resource_size(shared->res));
2963 free_shared:
2964         kfree(shared);
2965 err_runtime_get:
2966         if (slice == VIP_SLICE1) {
2967                 pm_runtime_disable(&pdev->dev);
2968                 return ret;
2969         } else
2970                 return 0;
2973 static int vip_remove(struct platform_device *pdev)
2975         struct vip_shared *shared = platform_get_drvdata(pdev);
2976         struct vip_dev *dev;
2977         int slice;
2979         for (slice = 0; slice < atomic_read(&shared->devs_allocated); slice++) {
2980                 dev = shared->devs[slice];
2981                 if (!dev)
2982                         continue;
2983                 vip_info(dev, "Removing " VIP_MODULE_NAME);
2984                 free_port(dev->ports[0]);
2985                 v4l2_async_notifier_unregister(&dev->notifier);
2986                 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
2987                 free_irq(dev->irq, dev);
2988                 kfree(dev);
2989         }
2990         remove_shared(shared);
2992         return 0;
2995 #if defined(CONFIG_OF)
2996 static const struct of_device_id vip_of_match[] = {
2997         {
2998                 .compatible = "ti,vip1", .data = (void *) VIP_INSTANCE1,
2999         },
3001         {
3002                 .compatible = "ti,vip2", .data = (void *) VIP_INSTANCE2,
3003         },
3005         {
3006                 .compatible = "ti,vip3", .data = (void *) VIP_INSTANCE3,
3007         },
3008         {},
3009 };
3010 MODULE_DEVICE_TABLE(of, vip_of_match);
3011 #else
3012 #define vip_of_match NULL
3013 #endif
3015 static struct platform_driver vip_pdrv = {
3016         .probe          = vip_probe,
3017         .remove         = vip_remove,
3018         .driver         = {
3019                 .name   = VIP_MODULE_NAME,
3020                 .owner  = THIS_MODULE,
3021                 .of_match_table = vip_of_match,
3022         },
3023 };
3025 module_platform_driver(vip_pdrv);
3027 MODULE_DESCRIPTION("TI VIP driver");
3028 MODULE_AUTHOR("Texas Instruments");
3029 MODULE_LICENSE("GPL v2");