]> rtime.felk.cvut.cz Git - hercules2020/nv-tegra/linux-4.4.git/blob - drivers/media/platform/tegra/camera/vi/mc_common.h
drivers: media: camera: Add sync op support
[hercules2020/nv-tegra/linux-4.4.git] / drivers / media / platform / tegra / camera / vi / mc_common.h
1 /*
2  * drivers/media/platform/tegra/camera/mc_common.h
3  *
4  * Tegra Media controller common APIs
5  *
6  * Copyright (c) 2012-2017, NVIDIA CORPORATION. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #ifndef __CAMERA_MC_COMMON_H__
22 #define __CAMERA_MC_COMMON_H__
23
24 #include <media/media-device.h>
25 #include <media/media-entity.h>
26 #include <media/v4l2-async.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-dev.h>
30 #include <media/videobuf2-core.h>
31 #include <linux/workqueue.h>
32
33 #include "core.h"
34 #include "../csi/csi.h"
35
36 #define MAX_FORMAT_NUM  64
37 #define MAX_SUBDEVICES  4
38 #define QUEUED_BUFFERS  4
39 #define ENABLE          1
40 #define DISABLE         0
41 #define MAX_SYNCPT_PER_CHANNEL 3
42 #define TPG_CHANNELS 6
43
44 #define TEGRA_MEM_FORMAT 0
45 #define TEGRA_ISP_FORMAT 1
46
47 enum channel_capture_state {
48         CAPTURE_IDLE = 0,
49         CAPTURE_GOOD,
50         CAPTURE_TIMEOUT,
51         CAPTURE_ERROR,
52 };
53
54 enum tegra_vi_pg_mode {
55         TEGRA_VI_PG_DISABLED = 0,
56         TEGRA_VI_PG_DIRECT,
57         TEGRA_VI_PG_PATCH,
58 };
59
60 /**
61  * struct tegra_channel_buffer - video channel buffer
62  * @buf: vb2 buffer base object
63  * @queue: buffer list entry in the channel queued buffers list
64  * @chan: channel that uses the buffer
65  * @addr: Tegra IOVA buffer address for VI output
66  */
67 struct tegra_channel_buffer {
68         struct vb2_v4l2_buffer buf;
69         struct list_head queue;
70         struct tegra_channel *chan;
71
72         dma_addr_t addr;
73 };
74
75 #define to_tegra_channel_buffer(vb) \
76         container_of(vb, struct tegra_channel_buffer, buf)
77
78 /**
79  * struct tegra_vi_graph_entity - Entity in the video graph
80  * @list: list entry in a graph entities list
81  * @node: the entity's DT node
82  * @entity: media entity, from the corresponding V4L2 subdev
83  * @asd: subdev asynchronous registration information
84  * @subdev: V4L2 subdev
85  */
86 struct tegra_vi_graph_entity {
87         struct list_head list;
88         struct device_node *node;
89         struct media_entity *entity;
90
91         struct v4l2_async_subdev asd;
92         struct v4l2_subdev *subdev;
93 };
94
95 /**
96  * struct tegra_channel - Tegra video channel
97  * @list: list entry in a composite device dmas list
98  * @video: V4L2 video device associated with the video channel
99  * @video_lock:
100  * @pad: media pad for the video device entity
101  * @pipe: pipeline belonging to the channel
102  *
103  * @vi: composite device DT node port number for the channel
104  *
105  * @kthread_capture: kernel thread task structure of this video channel
106  * @wait: wait queue structure for kernel thread
107  *
108  * @format: active V4L2 pixel format
109  * @fmtinfo: format information corresponding to the active @format
110  *
111  * @queue: vb2 buffers queue
112  * @alloc_ctx: allocation context for the vb2 @queue
113  * @sequence: V4L2 buffers sequence number
114  *
115  * @capture: list of queued buffers for capture
116  * @queued_lock: protects the buf_queued list
117  *
118  * @csi: CSI register bases
119  * @stride_align: channel buffer stride alignment, default is 64
120  * @width_align: image width alignment, default is 4
121  * @height_align: channel buffer height alignment, default is 1
122  * @size_align: channel buffer size alignment, default is 128K bytes
123  * @port: CSI port of this video channel
124  * @io_id: Tegra IO rail ID of this video channel
125  *
126  * @fmts_bitmap: a bitmap for formats supported
127  * @bypass: bypass flag for VI bypass mode
128  */
129 struct tegra_channel {
130         int id;
131         struct list_head list;
132         struct video_device video;
133         struct media_pad pad;
134         struct media_pipeline pipe;
135         struct mutex video_lock;
136
137         struct tegra_mc_vi *vi;
138         struct v4l2_subdev *subdev[MAX_SUBDEVICES];
139         struct v4l2_subdev *subdev_on_csi;
140
141         struct v4l2_ctrl_handler ctrl_handler;
142         struct v4l2_pix_format format;
143         const struct tegra_video_format *fmtinfo;
144         const struct tegra_video_format *video_formats[MAX_FORMAT_NUM];
145         unsigned int num_video_formats;
146         struct mutex stop_kthread_lock;
147
148         unsigned char port[TEGRA_CSI_BLOCKS];
149         unsigned int syncpt[TEGRA_CSI_BLOCKS][MAX_SYNCPT_PER_CHANNEL];
150         unsigned int syncpoint_fifo[TEGRA_CSI_BLOCKS];
151         unsigned int buffer_offset[TEGRA_CSI_BLOCKS];
152         unsigned int buffer_state[QUEUED_BUFFERS];
153         struct vb2_v4l2_buffer *buffers[QUEUED_BUFFERS];
154         unsigned int timeout;
155         unsigned int save_index;
156         unsigned int free_index;
157         unsigned int num_buffers;
158         unsigned int released_bufs;
159
160         struct task_struct *kthread_capture_start;
161         wait_queue_head_t start_wait;
162         struct vb2_queue queue;
163         void *alloc_ctx;
164         bool init_done;
165         struct list_head capture;
166         spinlock_t start_lock;
167         struct work_struct status_work;
168         struct work_struct error_work;
169
170         void __iomem *csibase[TEGRA_CSI_BLOCKS];
171         unsigned int stride_align;
172         unsigned int width_align;
173         unsigned int height_align;
174         unsigned int size_align;
175         unsigned int valid_ports;
176         unsigned int total_ports;
177         unsigned int numlanes;
178         unsigned int io_id;
179         unsigned int num_subdevs;
180         unsigned int sequence;
181         unsigned int saved_ctx_bypass;
182         unsigned int saved_ctx_pgmode;
183         unsigned int gang_mode;
184         unsigned int gang_width;
185         unsigned int gang_height;
186         unsigned int gang_bytesperline;
187         unsigned int gang_sizeimage;
188         unsigned int embedded_data_width;
189         unsigned int embedded_data_height;
190
191         DECLARE_BITMAP(fmts_bitmap, MAX_FORMAT_NUM);
192         atomic_t power_on_refcnt;
193         struct v4l2_fh *fh;
194         bool bypass;
195         bool write_ispformat;
196         enum tegra_vi_pg_mode pg_mode;
197         bool bfirst_fstart;
198         enum channel_capture_state capture_state;
199         spinlock_t capture_state_lock;
200         atomic_t is_streaming;
201         int requested_kbyteps;
202         unsigned long requested_hz;
203
204         struct vi_notify_channel *vnc[TEGRA_CSI_BLOCKS];
205         int vnc_id[TEGRA_CSI_BLOCKS];
206         int grp_id;
207 };
208
209 #define to_tegra_channel(vdev) \
210         container_of(vdev, struct tegra_channel, video)
211
212 /**
213  * struct tegra_mc_vi - NVIDIA Tegra Media controller structure
214  * @v4l2_dev: V4L2 device
215  * @media_dev: media device
216  * @dev: device struct
217  * @tegra_camera: tegra camera structure
218  * @nvhost_device_data: NvHost VI device information
219  *
220  * @notifier: V4L2 asynchronous subdevs notifier
221  * @entities: entities in the graph as a list of tegra_vi_graph_entity
222  * @num_subdevs: number of subdevs in the pipeline
223  *
224  * @channels: list of channels at the pipeline output and input
225  *
226  * @ctrl_handler: V4L2 control handler
227  * @pattern: test pattern generator V4L2 control
228  * @pg_mode: test pattern generator mode (disabled/direct/patch)
229  *
230  * @has_sensors: a flag to indicate whether is a real sensor connecting
231  */
232 struct tegra_mc_vi {
233         struct vi *vi;
234         struct platform_device *ndev;
235         struct v4l2_device v4l2_dev;
236         struct media_device media_dev;
237         struct device *dev;
238         struct nvhost_device_data *ndata;
239
240         struct regulator *reg;
241         struct clk *clk;
242         struct clk *parent_clk;
243
244         struct v4l2_async_notifier notifier;
245         struct list_head entities;
246         unsigned int num_channels;
247         unsigned int num_subdevs;
248
249         struct tegra_csi_device *csi;
250         struct list_head vi_chans;
251         struct tegra_channel *tpg_start;
252         void __iomem *iomem;
253
254         struct v4l2_ctrl_handler ctrl_handler;
255         struct v4l2_ctrl *pattern;
256         enum tegra_vi_pg_mode pg_mode;
257
258         bool has_sensors;
259         atomic_t power_on_refcnt;
260         unsigned int link_status;
261         unsigned int subdevs_bound;
262         struct mutex bw_update_lock;
263         unsigned long aggregated_kbyteps;
264         unsigned long max_requested_hz;
265         struct mutex mipical_lock;
266
267         bool bypass;
268
269         struct tegra_vi_fops *fops;
270
271         dma_addr_t emb_buf;
272         void *emb_buf_addr;
273         unsigned int emb_buf_size;
274 };
275
276 int tegra_vi_get_port_info(struct tegra_channel *chan,
277                         struct device_node *node, unsigned int index);
278 void tegra_vi_v4l2_cleanup(struct tegra_mc_vi *vi);
279 int tegra_vi_v4l2_init(struct tegra_mc_vi *vi);
280 int tegra_vi_tpg_graph_init(struct tegra_mc_vi *vi);
281 int tegra_vi_graph_init(struct tegra_mc_vi *vi);
282 void tegra_vi_graph_cleanup(struct tegra_mc_vi *vi);
283 int tegra_channel_init(struct tegra_channel *chan);
284 int tegra_vi_channels_register(struct tegra_mc_vi *vi);
285 void tegra_vi_channels_unregister(struct tegra_mc_vi *vi);
286 int tegra_vi_channels_init(struct tegra_mc_vi *vi);
287 int tegra_channel_cleanup(struct tegra_channel *chan);
288 int tegra_vi_channels_cleanup(struct tegra_mc_vi *vi);
289 int tegra_channel_init_subdevices(struct tegra_channel *chan);
290 int tegra_vi2_power_on(struct tegra_mc_vi *vi);
291 void tegra_vi2_power_off(struct tegra_mc_vi *vi);
292 int tegra_vi4_power_on(struct tegra_mc_vi *vi);
293 void tegra_vi4_power_off(struct tegra_mc_vi *vi);
294 int tegra_clean_unlinked_channels(struct tegra_mc_vi *vi);
295 int tegra_channel_s_ctrl(struct v4l2_ctrl *ctrl);
296 int tegra_vi_media_controller_init(struct tegra_mc_vi *mc_vi,
297                         struct platform_device *pdev);
298 void tegra_vi_media_controller_cleanup(struct tegra_mc_vi *mc_vi);
299 void tegra_channel_ec_close(struct tegra_mc_vi *mc_vi);
300 void tegra_channel_query_hdmiin_unplug(struct tegra_channel *chan,
301                 struct v4l2_event *event);
302 int tegra_vi_mfi_work(struct tegra_mc_vi *mc_vi, int csiport);
303 int tpg_vi_media_controller_init(struct tegra_mc_vi *mc_vi, int pg_mode);
304 void tpg_vi_media_controller_cleanup(struct tegra_mc_vi *mc_vi);
305 struct tegra_mc_vi *tegra_get_mc_vi(void);
306
307 u32 tegra_core_get_fourcc_by_idx(struct tegra_channel *chan,
308                 unsigned int index);
309 int tegra_core_get_idx_by_code(struct tegra_channel *chan,
310                 unsigned int code, unsigned offset);
311 const struct tegra_video_format *tegra_core_get_format_by_code(
312                 struct tegra_channel *chan,
313                 unsigned int code, unsigned offset);
314 const struct tegra_video_format *tegra_core_get_format_by_fourcc(
315                 struct tegra_channel *chan, u32 fourcc);
316 void tegra_core_get_description_by_idx(struct tegra_channel *chan,
317                 unsigned int index, __u8 *description);
318
319 struct tegra_vi_fops {
320         int (*vi_power_on)(struct tegra_channel *chan);
321         void (*vi_power_off)(struct tegra_channel *chan);
322         int (*vi_start_streaming)(struct vb2_queue *vq, u32 count);
323         int (*vi_stop_streaming)(struct vb2_queue *vq);
324         int (*vi_add_ctrls)(struct tegra_channel *chan);
325         void (*vi_init_video_formats)(struct tegra_channel *chan);
326         int (*vi_mfi_work)(struct tegra_mc_vi *vi, int port);
327 };
328
329 struct tegra_csi_fops {
330         int (*csi_power_on)(struct tegra_csi_device *csi);
331         int (*csi_power_off)(struct tegra_csi_device *csi);
332         int (*csi_start_streaming)(struct tegra_csi_channel *chan,
333                 enum tegra_csi_port_num port_num);
334         void (*csi_stop_streaming)(struct tegra_csi_channel *chan,
335                 enum tegra_csi_port_num port_num);
336         void (*csi_override_format)(struct tegra_csi_channel *chan,
337                 enum tegra_csi_port_num port_num);
338         int (*mipical)(struct tegra_csi_channel *chan);
339         int (*hw_init)(struct tegra_csi_device *csi);
340 };
341
342 struct tegra_t210_vi_data {
343         struct nvhost_device_data *info;
344         struct tegra_vi_fops *vi_fops;
345         struct tegra_csi_fops *csi_fops;
346 };
347
348 struct tegra_vi_data {
349         struct nvhost_device_data *info;
350         struct tegra_vi_fops *vi_fops;
351 };
352
353 struct tegra_csi_data {
354         struct nvhost_device_data *info;
355         struct tegra_csi_fops *csi_fops;
356 };
357 #endif