1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Xilinx Scene Change Detection driver
5 * Copyright (C) 2018 Xilinx, Inc.
7 * Authors: Anand Ashok Dumbre <anand.ashok.dumbre@xilinx.com>
8 * Satish Kumar Nagireddy <satish.nagireddy.nagireddy@xilinx.com>
11 #ifndef _XILINX_SCENECHANGE_H_
12 #define _XILINX_SCENECHANGE_H_
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/dmaengine.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/xilinx-v4l2-controls.h>
25 #include <media/v4l2-async.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-subdev.h>
29 #include "../../../dma/dmaengine.h"
31 /* Register/Descriptor Offsets */
32 #define XSCD_CTRL_OFFSET 0x000
33 #define XSCD_CTRL_AP_START BIT(0)
34 #define XSCD_CTRL_AP_DONE BIT(1)
35 #define XSCD_CTRL_AP_IDLE BIT(2)
36 #define XSCD_CTRL_AP_READY BIT(3)
37 #define XSCD_CTRL_AUTO_RESTART BIT(7)
39 #define XSCD_GIE_OFFSET 0x004
40 #define XSCD_GIE_EN BIT(0)
42 #define XSCD_IE_OFFSET 0x008
43 #define XSCD_IE_AP_DONE BIT(0)
44 #define XSCD_IE_AP_READY BIT(1)
46 #define XSCD_ISR_OFFSET 0x00c
47 #define XSCD_WIDTH_OFFSET 0x010
48 #define XSCD_HEIGHT_OFFSET 0x018
49 #define XSCD_STRIDE_OFFSET 0x020
50 #define XSCD_VID_FMT_OFFSET 0x028
51 #define XSCD_VID_FMT_RGB 0
52 #define XSCD_VID_FMT_YUV_444 1
53 #define XSCD_VID_FMT_YUV_422 2
54 #define XSCD_VID_FMT_YUV_420 3
55 #define XSCD_VID_FMT_Y8 24
56 #define XSCD_VID_FMT_Y10 25
58 #define XSCD_SUBSAMPLE_OFFSET 0x030
59 #define XSCD_SAD_OFFSET 0x038
60 #define XSCD_ADDR_OFFSET 0x040
61 #define XSCD_CHAN_OFFSET 0x100
62 #define XSCD_CHAN_EN_OFFSET 0x780
64 #define XSCD_MAX_CHANNELS 8
66 /****************************** PROTOTYPES ************************************/
71 * struct xscd_dma_desc - DMA channel
72 * @luma_plane_addr: Luma plane buffer address
73 * @vsize: width of the luma frame
74 * @hsize: height of the luma frame
75 * @stride: stride of the luma frame
77 struct xscd_dma_desc {
78 dma_addr_t luma_plane_addr;
85 * struct xscd_dma_tx_descriptor - Per Transaction structure
86 * @async_tx: Async transaction descriptor
87 * @sw: Software Descriptor
88 * @node: Node in the channel descriptor list
90 struct xscd_dma_tx_descriptor {
91 struct dma_async_tx_descriptor async_tx;
92 struct xscd_dma_desc sw;
93 struct list_head node;
96 static inline struct xscd_dma_tx_descriptor *
97 to_xscd_dma_tx_descriptor(struct dma_async_tx_descriptor *tx)
99 return container_of(tx, struct xscd_dma_tx_descriptor, async_tx);
103 * struct xscd_dma_chan - DMA Channel structure
105 * @iomem: I/O memory address of the channel registers
106 * @lock: Descriptor operation lock
107 * @chan_node: Member of a list of framebuffer channel instances
108 * @pending_list: Descriptors waiting
109 * @done_list: Complete descriptors
110 * @staged_desc: Next buffer to be programmed
111 * @active_desc: Currently active buffer being read/written to
112 * @common: DMA common channel
113 * @idle: Channel idle state
114 * @tasklet: Cleanup work after irq
115 * @id: scene change channel ID
116 * @en: Channel is enabled
117 * @valid_interrupt: Valid interrupt for the channel
119 struct xscd_dma_chan {
120 struct xscd_device *xscd;
123 /* Descriptor operation Lock */
125 struct list_head chan_node;
126 struct list_head pending_list;
127 struct list_head done_list;
128 struct xscd_dma_tx_descriptor *staged_desc;
129 struct xscd_dma_tx_descriptor *active_desc;
130 struct dma_chan common;
132 struct tasklet_struct tasklet;
135 bool valid_interrupt;
138 static inline struct xscd_dma_chan *to_xscd_dma_chan(struct dma_chan *chan)
140 return container_of(chan, struct xscd_dma_chan, common);
144 * struct xscd_chan - Video Stream structure
145 * @id: scene change channel ID
146 * @iomem: I/O memory address of the channel registers
148 * @subdev: V4L2 subdevice
150 * @format: active V4L2 media bus format for the pad
151 * @event: scene change event
152 * @dmachan: dma channel part of the scenechange stream
153 * @lock: lock to protect active stream count variable
158 struct xscd_device *xscd;
159 struct v4l2_subdev subdev;
160 struct media_pad pads[2];
161 struct v4l2_mbus_framefmt format;
162 struct v4l2_event event;
163 struct xscd_dma_chan dmachan;
165 /* Lock to protect active stream count */
169 static inline struct xscd_chan *to_xscd_chan(struct v4l2_subdev *subdev)
171 return container_of(subdev, struct xscd_chan, subdev);
175 * struct xscd_device - Xilinx Scene Change Detection device structure
177 * @iomem: device I/O register space remapped to kernel virtual memory
178 * @rst_gpio: reset GPIO
179 * @clk: video core clock
181 * @memory_based: Flag to identify memory based mode
182 * @num_streams: Number of streams in the design
183 * @chans: video stream instances
184 * @dma_device: DMA device structure
185 * @channels: DMA channels
186 * @active_streams: Number of active streams
191 struct gpio_desc *rst_gpio;
198 struct xscd_chan *chans;
200 struct dma_device dma_device;
201 struct xscd_dma_chan *channels[XSCD_MAX_CHANNELS];
206 * Register related operations
208 static inline u32 xscd_read(void __iomem *iomem, u32 addr)
210 return ioread32(iomem + addr);
213 static inline void xscd_write(void __iomem *iomem, u32 addr, u32 value)
215 iowrite32(value, iomem + addr);
218 static inline void xscd_clr(void __iomem *iomem, u32 addr, u32 clr)
220 xscd_write(iomem, addr, xscd_read(iomem, addr) & ~clr);
223 static inline void xscd_set(void __iomem *iomem, u32 addr, u32 set)
225 xscd_write(iomem, addr, xscd_read(iomem, addr) | set);
228 void xscd_dma_start_transfer(struct xscd_dma_chan *chan);
229 void xscd_dma_start(struct xscd_dma_chan *chan);
230 void xscd_dma_chan_enable(struct xscd_dma_chan *chan, int chan_en);
231 void xscd_dma_reset(struct xscd_dma_chan *chan);
232 void xscd_dma_halt(struct xscd_dma_chan *chan);
233 void xscd_dma_irq_handler(struct xscd_device *xscd);
234 int xscd_dma_init(struct xscd_device *xscd);
235 void xscd_dma_cleanup(struct xscd_device *xscd);
237 void xscd_chan_irq_handler(struct xscd_chan *chan);
238 int xscd_chan_init(struct xscd_device *xscd, unsigned int chan_id,
239 struct device_node *node);