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