b62fda00dc1576b318aca491cd77e819f559eb14
[cascardo/linux.git] / drivers / media / platform / sti / hva / hva.h
1 /*
2  * Copyright (C) STMicroelectronics SA 2015
3  * Authors: Yannick Fertre <yannick.fertre@st.com>
4  *          Hugues Fruchet <hugues.fruchet@st.com>
5  * License terms:  GNU General Public License (GPL), version 2
6  */
7
8 #ifndef HVA_H
9 #define HVA_H
10
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-device.h>
13 #include <media/videobuf2-v4l2.h>
14 #include <media/v4l2-mem2mem.h>
15
16 #define fh_to_ctx(f)    (container_of(f, struct hva_ctx, fh))
17
18 #define hva_to_dev(h)   (h->dev)
19
20 #define ctx_to_dev(c)   (c->hva_dev->dev)
21
22 #define ctx_to_hdev(c)  (c->hva_dev)
23
24 #define HVA_PREFIX "[---:----]"
25
26 /**
27  * struct hva_frameinfo - information about hva frame
28  *
29  * @pixelformat:    fourcc code for uncompressed video format
30  * @width:          width of frame
31  * @height:         height of frame
32  * @aligned_width:  width of frame (with encoder alignment constraint)
33  * @aligned_height: height of frame (with encoder alignment constraint)
34  * @size:           maximum size in bytes required for data
35 */
36 struct hva_frameinfo {
37         u32     pixelformat;
38         u32     width;
39         u32     height;
40         u32     aligned_width;
41         u32     aligned_height;
42         u32     size;
43 };
44
45 /**
46  * struct hva_streaminfo - information about hva stream
47  *
48  * @streamformat: fourcc code of compressed video format (H.264...)
49  * @width:        width of stream
50  * @height:       height of stream
51  * @profile:      profile string
52  * @level:        level string
53  */
54 struct hva_streaminfo {
55         u32     streamformat;
56         u32     width;
57         u32     height;
58         u8      profile[32];
59         u8      level[32];
60 };
61
62 /**
63  * struct hva_controls - hva controls set
64  *
65  * @time_per_frame: time per frame in seconds
66  * @bitrate_mode:   bitrate mode (constant bitrate or variable bitrate)
67  * @gop_size:       groupe of picture size
68  * @bitrate:        bitrate (in bps)
69  * @aspect:         video aspect
70  */
71 struct hva_controls {
72         struct v4l2_fract                       time_per_frame;
73         enum v4l2_mpeg_video_bitrate_mode       bitrate_mode;
74         u32                                     gop_size;
75         u32                                     bitrate;
76         enum v4l2_mpeg_video_aspect             aspect;
77 };
78
79 /**
80  * struct hva_frame - hva frame buffer (output)
81  *
82  * @vbuf:     video buffer information for V4L2
83  * @list:     V4L2 m2m list that the frame belongs to
84  * @info:     frame information (width, height, format, alignment...)
85  * @paddr:    physical address (for hardware)
86  * @vaddr:    virtual address (kernel can read/write)
87  * @prepared: true if vaddr/paddr are resolved
88  */
89 struct hva_frame {
90         struct vb2_v4l2_buffer  vbuf;
91         struct list_head        list;
92         struct hva_frameinfo    info;
93         dma_addr_t              paddr;
94         void                    *vaddr;
95         bool                    prepared;
96 };
97
98 /*
99  * to_hva_frame() - cast struct vb2_v4l2_buffer * to struct hva_frame *
100  */
101 #define to_hva_frame(vb) \
102         container_of(vb, struct hva_frame, vbuf)
103
104 /**
105  * struct hva_stream - hva stream buffer (capture)
106  *
107  * @v4l2:       video buffer information for V4L2
108  * @list:       V4L2 m2m list that the frame belongs to
109  * @paddr:      physical address (for hardware)
110  * @vaddr:      virtual address (kernel can read/write)
111  * @prepared:   true if vaddr/paddr are resolved
112  * @size:       size of the buffer in bytes
113  * @bytesused:  number of bytes occupied by data in the buffer
114  */
115 struct hva_stream {
116         struct vb2_v4l2_buffer  vbuf;
117         struct list_head        list;
118         dma_addr_t              paddr;
119         void                    *vaddr;
120         bool                    prepared;
121         unsigned int            size;
122         unsigned int            bytesused;
123 };
124
125 /*
126  * to_hva_stream() - cast struct vb2_v4l2_buffer * to struct hva_stream *
127  */
128 #define to_hva_stream(vb) \
129         container_of(vb, struct hva_stream, vbuf)
130
131 struct hva_dev;
132 struct hva_enc;
133
134 /**
135  * struct hva_ctx - context of hva instance
136  *
137  * @hva_dev:         the device that this instance is associated with
138  * @fh:              V4L2 file handle
139  * @ctrl_handler:    V4L2 controls handler
140  * @ctrls:           hva controls set
141  * @id:              instance identifier
142  * @aborting:        true if current job aborted
143  * @name:            instance name (debug purpose)
144  * @run_work:        encode work
145  * @lock:            mutex used to lock access of this context
146  * @flags:           validity of streaminfo and frameinfo fields
147  * @frame_num:       frame number
148  * @stream_num:      stream number
149  * @max_stream_size: maximum size in bytes required for stream data
150  * @colorspace:      colorspace identifier
151  * @xfer_func:       transfer function identifier
152  * @ycbcr_enc:       Y'CbCr encoding identifier
153  * @quantization:    quantization identifier
154  * @streaminfo:      stream properties
155  * @frameinfo:       frame properties
156  * @enc:             current encoder
157  * @priv:            private codec data for this instance, allocated
158  *                   by encoder @open time
159  * @hw_err:          true if hardware error detected
160  */
161 struct hva_ctx {
162         struct hva_dev                  *hva_dev;
163         struct v4l2_fh                  fh;
164         struct v4l2_ctrl_handler        ctrl_handler;
165         struct hva_controls             ctrls;
166         u8                              id;
167         bool                            aborting;
168         char                            name[100];
169         struct work_struct              run_work;
170         /* mutex protecting this data structure */
171         struct mutex                    lock;
172         u32                             flags;
173         u32                             frame_num;
174         u32                             stream_num;
175         u32                             max_stream_size;
176         enum v4l2_colorspace            colorspace;
177         enum v4l2_xfer_func             xfer_func;
178         enum v4l2_ycbcr_encoding        ycbcr_enc;
179         enum v4l2_quantization          quantization;
180         struct hva_streaminfo           streaminfo;
181         struct hva_frameinfo            frameinfo;
182         struct hva_enc                  *enc;
183         void                            *priv;
184         bool                            hw_err;
185 };
186
187 #define HVA_FLAG_STREAMINFO     0x0001
188 #define HVA_FLAG_FRAMEINFO      0x0002
189
190 #define HVA_MAX_INSTANCES       16
191 #define HVA_MAX_ENCODERS        10
192 #define HVA_MAX_FORMATS         HVA_MAX_ENCODERS
193
194 /**
195  * struct hva_dev - abstraction for hva entity
196  *
197  * @v4l2_dev:            V4L2 device
198  * @vdev:                video device
199  * @pdev:                platform device
200  * @dev:                 device
201  * @lock:                mutex used for critical sections & V4L2 ops
202  *                       serialization
203  * @m2m_dev:             memory-to-memory V4L2 device information
204  * @instances:           opened instances
205  * @nb_of_instances:     number of opened instances
206  * @instance_id:         rolling counter identifying an instance (debug purpose)
207  * @regs:                register io memory access
208  * @esram_addr:          esram address
209  * @esram_size:          esram size
210  * @clk:                 hva clock
211  * @irq_its:             status interruption
212  * @irq_err:             error interruption
213  * @work_queue:          work queue to handle the encode jobs
214  * @protect_mutex:       mutex used to lock access of hardware
215  * @interrupt:           completion interrupt
216  * @ip_version:          IP hardware version
217  * @encoders:            registered encoders
218  * @nb_of_encoders:      number of registered encoders
219  * @pixelformats:        supported uncompressed video formats
220  * @nb_of_pixelformats:  number of supported umcompressed video formats
221  * @streamformats:       supported compressed video formats
222  * @nb_of_streamformats: number of supported compressed video formats
223  * @sfl_reg:             status fifo level register value
224  * @sts_reg:             status register value
225  * @lmi_err_reg:         local memory interface error register value
226  * @emi_err_reg:         external memory interface error register value
227  * @hec_mif_err_reg:     HEC memory interface error register value
228  */
229 struct hva_dev {
230         struct v4l2_device      v4l2_dev;
231         struct video_device     *vdev;
232         struct platform_device  *pdev;
233         struct device           *dev;
234         /* mutex protecting vb2_queue structure */
235         struct mutex            lock;
236         struct v4l2_m2m_dev     *m2m_dev;
237         struct hva_ctx          *instances[HVA_MAX_INSTANCES];
238         unsigned int            nb_of_instances;
239         unsigned int            instance_id;
240         void __iomem            *regs;
241         u32                     esram_addr;
242         u32                     esram_size;
243         struct clk              *clk;
244         int                     irq_its;
245         int                     irq_err;
246         struct workqueue_struct *work_queue;
247         /* mutex protecting hardware access */
248         struct mutex            protect_mutex;
249         struct completion       interrupt;
250         unsigned long int       ip_version;
251         const struct hva_enc    *encoders[HVA_MAX_ENCODERS];
252         u32                     nb_of_encoders;
253         u32                     pixelformats[HVA_MAX_FORMATS];
254         u32                     nb_of_pixelformats;
255         u32                     streamformats[HVA_MAX_FORMATS];
256         u32                     nb_of_streamformats;
257         u32                     sfl_reg;
258         u32                     sts_reg;
259         u32                     lmi_err_reg;
260         u32                     emi_err_reg;
261         u32                     hec_mif_err_reg;
262 };
263
264 /**
265  * struct hva_enc - hva encoder
266  *
267  * @name:         encoder name
268  * @streamformat: fourcc code for compressed video format (H.264...)
269  * @pixelformat:  fourcc code for uncompressed video format
270  * @max_width:    maximum width of frame for this encoder
271  * @max_height:   maximum height of frame for this encoder
272  * @open:         open encoder
273  * @close:        close encoder
274  * @encode:       encode a frame (struct hva_frame) in a stream
275  *                (struct hva_stream)
276  */
277
278 struct hva_enc {
279         const char      *name;
280         u32             streamformat;
281         u32             pixelformat;
282         u32             max_width;
283         u32             max_height;
284         int             (*open)(struct hva_ctx *ctx);
285         int             (*close)(struct hva_ctx *ctx);
286         int             (*encode)(struct hva_ctx *ctx, struct hva_frame *frame,
287                                   struct hva_stream *stream);
288 };
289
290 #endif /* HVA_H */