GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / media / platform / mtk-mdp / mtk_mdp_core.h
1 /*
2  * Copyright (c) 2015-2016 MediaTek Inc.
3  * Author: Houlong Wei <houlong.wei@mediatek.com>
4  *         Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #ifndef __MTK_MDP_CORE_H__
17 #define __MTK_MDP_CORE_H__
18
19 #include <linux/videodev2.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-mem2mem.h>
23 #include <media/videobuf2-core.h>
24 #include <media/videobuf2-dma-contig.h>
25
26 #include "mtk_mdp_vpu.h"
27 #include "mtk_mdp_comp.h"
28
29
30 #define MTK_MDP_MODULE_NAME             "mtk-mdp"
31
32 #define MTK_MDP_SHUTDOWN_TIMEOUT        ((100*HZ)/1000) /* 100ms */
33 #define MTK_MDP_MAX_CTRL_NUM            10
34
35 #define MTK_MDP_FMT_FLAG_OUTPUT         BIT(0)
36 #define MTK_MDP_FMT_FLAG_CAPTURE        BIT(1)
37
38 #define MTK_MDP_VPU_INIT                BIT(0)
39 #define MTK_MDP_SRC_FMT                 BIT(1)
40 #define MTK_MDP_DST_FMT                 BIT(2)
41 #define MTK_MDP_CTX_ERROR               BIT(5)
42
43 /**
44  *  struct mtk_mdp_pix_align - alignement of image
45  *  @org_w: source alignment of width
46  *  @org_h: source alignment of height
47  *  @target_w: dst alignment of width
48  *  @target_h: dst alignment of height
49  */
50 struct mtk_mdp_pix_align {
51         u16 org_w;
52         u16 org_h;
53         u16 target_w;
54         u16 target_h;
55 };
56
57 /**
58  * struct mtk_mdp_fmt - the driver's internal color format data
59  * @pixelformat: the fourcc code for this format, 0 if not applicable
60  * @num_planes: number of physically non-contiguous data planes
61  * @num_comp: number of logical data planes
62  * @depth: per plane driver's private 'number of bits per pixel'
63  * @row_depth: per plane driver's private 'number of bits per pixel per row'
64  * @flags: flags indicating which operation mode format applies to
65            MTK_MDP_FMT_FLAG_OUTPUT is used in OUTPUT stream
66            MTK_MDP_FMT_FLAG_CAPTURE is used in CAPTURE stream
67  * @align: pointer to a pixel alignment struct, NULL if using default value
68  */
69 struct mtk_mdp_fmt {
70         u32     pixelformat;
71         u16     num_planes;
72         u16     num_comp;
73         u8      depth[VIDEO_MAX_PLANES];
74         u8      row_depth[VIDEO_MAX_PLANES];
75         u32     flags;
76         struct mtk_mdp_pix_align *align;
77 };
78
79 /**
80  * struct mtk_mdp_addr - the image processor physical address set
81  * @addr:       address of planes
82  */
83 struct mtk_mdp_addr {
84         dma_addr_t addr[MTK_MDP_MAX_NUM_PLANE];
85 };
86
87 /* struct mtk_mdp_ctrls - the image processor control set
88  * @rotate: rotation degree
89  * @hflip: horizontal flip
90  * @vflip: vertical flip
91  * @global_alpha: the alpha value of current frame
92  */
93 struct mtk_mdp_ctrls {
94         struct v4l2_ctrl *rotate;
95         struct v4l2_ctrl *hflip;
96         struct v4l2_ctrl *vflip;
97         struct v4l2_ctrl *global_alpha;
98 };
99
100 /**
101  * struct mtk_mdp_frame - source/target frame properties
102  * @width:      SRC : SRCIMG_WIDTH, DST : OUTPUTDMA_WHOLE_IMG_WIDTH
103  * @height:     SRC : SRCIMG_HEIGHT, DST : OUTPUTDMA_WHOLE_IMG_HEIGHT
104  * @crop:       cropped(source)/scaled(destination) size
105  * @payload:    image size in bytes (w x h x bpp)
106  * @pitch:      bytes per line of image in memory
107  * @addr:       image frame buffer physical addresses
108  * @fmt:        color format pointer
109  * @alpha:      frame's alpha value
110  */
111 struct mtk_mdp_frame {
112         u32                             width;
113         u32                             height;
114         struct v4l2_rect                crop;
115         unsigned long                   payload[VIDEO_MAX_PLANES];
116         unsigned int                    pitch[VIDEO_MAX_PLANES];
117         struct mtk_mdp_addr             addr;
118         const struct mtk_mdp_fmt        *fmt;
119         u8                              alpha;
120 };
121
122 /**
123  * struct mtk_mdp_variant - image processor variant information
124  * @pix_max:            maximum limit of image size
125  * @pix_min:            minimun limit of image size
126  * @pix_align:          alignement of image
127  * @h_scale_up_max:     maximum scale-up in horizontal
128  * @v_scale_up_max:     maximum scale-up in vertical
129  * @h_scale_down_max:   maximum scale-down in horizontal
130  * @v_scale_down_max:   maximum scale-down in vertical
131  */
132 struct mtk_mdp_variant {
133         struct mtk_mdp_pix_limit        *pix_max;
134         struct mtk_mdp_pix_limit        *pix_min;
135         struct mtk_mdp_pix_align        *pix_align;
136         u16                             h_scale_up_max;
137         u16                             v_scale_up_max;
138         u16                             h_scale_down_max;
139         u16                             v_scale_down_max;
140 };
141
142 /**
143  * struct mtk_mdp_dev - abstraction for image processor entity
144  * @lock:       the mutex protecting this data structure
145  * @vpulock:    the mutex protecting the communication with VPU
146  * @pdev:       pointer to the image processor platform device
147  * @variant:    the IP variant information
148  * @id:         image processor device index (0..MTK_MDP_MAX_DEVS)
149  * @comp:       MDP function components
150  * @m2m_dev:    v4l2 memory-to-memory device data
151  * @ctx_list:   list of struct mtk_mdp_ctx
152  * @vdev:       video device for image processor driver
153  * @v4l2_dev:   V4L2 device to register video devices for.
154  * @job_wq:     processor work queue
155  * @vpu_dev:    VPU platform device
156  * @ctx_num:    counter of active MTK MDP context
157  * @id_counter: An integer id given to the next opened context
158  * @wdt_wq:     work queue for VPU watchdog
159  * @wdt_work:   worker for VPU watchdog
160  */
161 struct mtk_mdp_dev {
162         struct mutex                    lock;
163         struct mutex                    vpulock;
164         struct platform_device          *pdev;
165         struct mtk_mdp_variant          *variant;
166         u16                             id;
167         struct mtk_mdp_comp             *comp[MTK_MDP_COMP_ID_MAX];
168         struct v4l2_m2m_dev             *m2m_dev;
169         struct list_head                ctx_list;
170         struct video_device             *vdev;
171         struct v4l2_device              v4l2_dev;
172         struct workqueue_struct         *job_wq;
173         struct platform_device          *vpu_dev;
174         int                             ctx_num;
175         unsigned long                   id_counter;
176         struct workqueue_struct         *wdt_wq;
177         struct work_struct              wdt_work;
178 };
179
180 /**
181  * mtk_mdp_ctx - the device context data
182  * @list:               link to ctx_list of mtk_mdp_dev
183  * @s_frame:            source frame properties
184  * @d_frame:            destination frame properties
185  * @id:                 index of the context that this structure describes
186  * @flags:              additional flags for image conversion
187  * @state:              flags to keep track of user configuration
188                         Protected by slock
189  * @rotation:           rotates the image by specified angle
190  * @hflip:              mirror the picture horizontally
191  * @vflip:              mirror the picture vertically
192  * @mdp_dev:            the image processor device this context applies to
193  * @m2m_ctx:            memory-to-memory device context
194  * @fh:                 v4l2 file handle
195  * @ctrl_handler:       v4l2 controls handler
196  * @ctrls               image processor control set
197  * @ctrls_rdy:          true if the control handler is initialized
198  * @colorspace:         enum v4l2_colorspace; supplemental to pixelformat
199  * @ycbcr_enc:          enum v4l2_ycbcr_encoding, Y'CbCr encoding
200  * @xfer_func:          enum v4l2_xfer_func, colorspace transfer function
201  * @quant:              enum v4l2_quantization, colorspace quantization
202  * @vpu:                VPU instance
203  * @slock:              the mutex protecting mtp_mdp_ctx.state
204  * @work:               worker for image processing
205  */
206 struct mtk_mdp_ctx {
207         struct list_head                list;
208         struct mtk_mdp_frame            s_frame;
209         struct mtk_mdp_frame            d_frame;
210         u32                             flags;
211         u32                             state;
212         int                             id;
213         int                             rotation;
214         u32                             hflip:1;
215         u32                             vflip:1;
216         struct mtk_mdp_dev              *mdp_dev;
217         struct v4l2_m2m_ctx             *m2m_ctx;
218         struct v4l2_fh                  fh;
219         struct v4l2_ctrl_handler        ctrl_handler;
220         struct mtk_mdp_ctrls            ctrls;
221         bool                            ctrls_rdy;
222         enum v4l2_colorspace            colorspace;
223         enum v4l2_ycbcr_encoding        ycbcr_enc;
224         enum v4l2_xfer_func             xfer_func;
225         enum v4l2_quantization          quant;
226
227         struct mtk_mdp_vpu              vpu;
228         struct mutex                    slock;
229         struct work_struct              work;
230 };
231
232 extern int mtk_mdp_dbg_level;
233
234 #if defined(DEBUG)
235
236 #define mtk_mdp_dbg(level, fmt, args...)                                 \
237         do {                                                             \
238                 if (mtk_mdp_dbg_level >= level)                          \
239                         pr_info("[MTK_MDP] level=%d %s(),%d: " fmt "\n", \
240                                 level, __func__, __LINE__, ##args);      \
241         } while (0)
242
243 #define mtk_mdp_err(fmt, args...)                                       \
244         pr_err("[MTK_MDP][ERROR] %s:%d: " fmt "\n", __func__, __LINE__, \
245                ##args)
246
247
248 #define mtk_mdp_dbg_enter()  mtk_mdp_dbg(3, "+")
249 #define mtk_mdp_dbg_leave()  mtk_mdp_dbg(3, "-")
250
251 #else
252
253 #define mtk_mdp_dbg(level, fmt, args...) {}
254 #define mtk_mdp_err(fmt, args...)
255 #define mtk_mdp_dbg_enter()
256 #define mtk_mdp_dbg_leave()
257
258 #endif
259
260 #endif /* __MTK_MDP_CORE_H__ */