36ce4a0b25cff438f0fcc95ba4001e53ae6a2375
[linux-2.6-microblaze.git] / drivers / media / platform / rockchip / rkisp1 / rkisp1-common.h
1 /* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
2 /*
3  * Rockchip ISP1 Driver - Common definitions
4  *
5  * Copyright (C) 2019 Collabora, Ltd.
6  *
7  * Based on Rockchip ISP1 driver by Rockchip Electronics Co., Ltd.
8  * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
9  */
10
11 #ifndef _RKISP1_COMMON_H
12 #define _RKISP1_COMMON_H
13
14 #include <linux/clk.h>
15 #include <linux/interrupt.h>
16 #include <linux/mutex.h>
17 #include <linux/rkisp1-config.h>
18 #include <media/media-device.h>
19 #include <media/media-entity.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-device.h>
22 #include <media/videobuf2-v4l2.h>
23
24 #include "rkisp1-regs.h"
25
26 /*
27  * flags on the 'direction' field in struct 'rkisp1_isp_mbus_info' that indicate
28  * on which pad the media bus format is supported
29  */
30 #define RKISP1_ISP_SD_SRC BIT(0)
31 #define RKISP1_ISP_SD_SINK BIT(1)
32
33 /* min and max values for the widths and heights of the entities */
34 #define RKISP1_ISP_MAX_WIDTH            4032
35 #define RKISP1_ISP_MAX_HEIGHT           3024
36 #define RKISP1_ISP_MIN_WIDTH            32
37 #define RKISP1_ISP_MIN_HEIGHT           32
38
39 #define RKISP1_RSZ_MP_SRC_MAX_WIDTH             4416
40 #define RKISP1_RSZ_MP_SRC_MAX_HEIGHT            3312
41 #define RKISP1_RSZ_SP_SRC_MAX_WIDTH             1920
42 #define RKISP1_RSZ_SP_SRC_MAX_HEIGHT            1920
43 #define RKISP1_RSZ_SRC_MIN_WIDTH                32
44 #define RKISP1_RSZ_SRC_MIN_HEIGHT               16
45
46 /* the default width and height of all the entities */
47 #define RKISP1_DEFAULT_WIDTH            800
48 #define RKISP1_DEFAULT_HEIGHT           600
49
50 #define RKISP1_DRIVER_NAME      "rkisp1"
51 #define RKISP1_BUS_INFO         "platform:" RKISP1_DRIVER_NAME
52
53 /* maximum number of clocks */
54 #define RKISP1_MAX_BUS_CLK      8
55
56 /* a bitmask of the ready stats */
57 #define RKISP1_STATS_MEAS_MASK          (RKISP1_CIF_ISP_AWB_DONE |      \
58                                          RKISP1_CIF_ISP_AFM_FIN |       \
59                                          RKISP1_CIF_ISP_EXP_END |       \
60                                          RKISP1_CIF_ISP_HIST_MEASURE_RDY)
61
62 /* enum for the resizer pads */
63 enum rkisp1_rsz_pad {
64         RKISP1_RSZ_PAD_SINK,
65         RKISP1_RSZ_PAD_SRC,
66         RKISP1_RSZ_PAD_MAX
67 };
68
69 /* enum for the capture id */
70 enum rkisp1_stream_id {
71         RKISP1_MAINPATH,
72         RKISP1_SELFPATH,
73 };
74
75 /* bayer patterns */
76 enum rkisp1_fmt_raw_pat_type {
77         RKISP1_RAW_RGGB = 0,
78         RKISP1_RAW_GRBG,
79         RKISP1_RAW_GBRG,
80         RKISP1_RAW_BGGR,
81 };
82
83 /* enum for the isp pads */
84 enum rkisp1_isp_pad {
85         RKISP1_ISP_PAD_SINK_VIDEO,
86         RKISP1_ISP_PAD_SINK_PARAMS,
87         RKISP1_ISP_PAD_SOURCE_VIDEO,
88         RKISP1_ISP_PAD_SOURCE_STATS,
89         RKISP1_ISP_PAD_MAX
90 };
91
92 /*
93  * struct rkisp1_sensor_async - A container for the v4l2_async_subdev to add to the notifier
94  *                              of the v4l2-async API
95  *
96  * @asd:                async_subdev variable for the sensor
97  * @lanes:              number of lanes
98  * @mbus_type:          type of bus (currently only CSI2 is supported)
99  * @mbus_flags:         media bus (V4L2_MBUS_*) flags
100  * @sd:                 a pointer to v4l2_subdev struct of the sensor
101  * @pixel_rate_ctrl:    pixel rate of the sensor, used to initialize the phy
102  * @dphy:               a pointer to the phy
103  */
104 struct rkisp1_sensor_async {
105         struct v4l2_async_subdev asd;
106         unsigned int lanes;
107         enum v4l2_mbus_type mbus_type;
108         unsigned int mbus_flags;
109         struct v4l2_subdev *sd;
110         struct v4l2_ctrl *pixel_rate_ctrl;
111         struct phy *dphy;
112 };
113
114 /*
115  * struct rkisp1_isp - ISP subdev entity
116  *
117  * @sd:                         v4l2_subdev variable
118  * @rkisp1:                     pointer to rkisp1_device
119  * @pads:                       media pads
120  * @pad_cfg:                    pads configurations
121  * @sink_fmt:                   input format
122  * @src_fmt:                    output format
123  * @ops_lock:                   ops serialization
124  * @is_dphy_errctrl_disabled:   if dphy errctrl is disabled (avoid endless interrupt)
125  * @frame_sequence:             used to synchronize frame_id between video devices.
126  */
127 struct rkisp1_isp {
128         struct v4l2_subdev sd;
129         struct media_pad pads[RKISP1_ISP_PAD_MAX];
130         struct v4l2_subdev_pad_config pad_cfg[RKISP1_ISP_PAD_MAX];
131         const struct rkisp1_isp_mbus_info *sink_fmt;
132         const struct rkisp1_isp_mbus_info *src_fmt;
133         struct mutex ops_lock; /* serialize the subdevice ops */
134         bool is_dphy_errctrl_disabled;
135         __u32 frame_sequence;
136 };
137
138 /*
139  * struct rkisp1_vdev_node - Container for the video nodes: params, stats, mainpath, selfpath
140  *
141  * @buf_queue:  queue of buffers
142  * @vlock:      lock of the video node
143  * @vdev:       video node
144  * @pad:        media pad
145  */
146 struct rkisp1_vdev_node {
147         struct vb2_queue buf_queue;
148         struct mutex vlock; /* ioctl serialization mutex */
149         struct video_device vdev;
150         struct media_pad pad;
151 };
152
153 /*
154  * struct rkisp1_buffer - A container for the vb2 buffers used by the video devices:
155  *                        params, stats, mainpath, selfpath
156  *
157  * @vb:         vb2 buffer
158  * @queue:      entry of the buffer in the queue
159  * @buff_addr:  dma addresses of each plane, used only by the capture devices: selfpath, mainpath
160  */
161 struct rkisp1_buffer {
162         struct vb2_v4l2_buffer vb;
163         struct list_head queue;
164         u32 buff_addr[VIDEO_MAX_PLANES];
165 };
166
167 /*
168  * struct rkisp1_dummy_buffer - A buffer to write the next frame to in case
169  *                              there are no vb2 buffers available.
170  *
171  * @vaddr:      return value of call to dma_alloc_attrs.
172  * @dma_addr:   dma address of the buffer.
173  * @size:       size of the buffer.
174  */
175 struct rkisp1_dummy_buffer {
176         void *vaddr;
177         dma_addr_t dma_addr;
178         u32 size;
179 };
180
181 struct rkisp1_device;
182
183 /*
184  * struct rkisp1_capture - ISP capture video device
185  *
186  * @vnode:        video node
187  * @rkisp1:       pointer to rkisp1_device
188  * @id:           id of the capture, one of RKISP1_SELFPATH, RKISP1_MAINPATH
189  * @ops:          list of callbacks to configure the capture device.
190  * @config:       a pointer to the list of registers to configure the capture format.
191  * @is_streaming: device is streaming
192  * @is_stopping:  stop_streaming callback was called and the device is in the process of
193  *                stopping the streaming.
194  * @done:         when stop_streaming callback is called, the device waits for the next irq
195  *                handler to stop the streaming by waiting on the 'done' wait queue.
196  *                If the irq handler is not called, the stream is stopped by the callback
197  *                after timeout.
198  * @sp_y_stride:  the selfpath allows to configure a y stride that is longer than the image width.
199  * @buf.lock:     lock to protect buf.queue
200  * @buf.queue:    queued buffer list
201  * @buf.dummy:    dummy space to store dropped data
202  *
203  * rkisp1 uses shadow registers, so it needs two buffers at a time
204  * @buf.curr:     the buffer used for current frame
205  * @buf.next:     the buffer used for next frame
206  * @pix.cfg:      pixel configuration
207  * @pix.info:     a pointer to the v4l2_format_info of the pixel format
208  * @pix.fmt:      buffer format
209  */
210 struct rkisp1_capture {
211         struct rkisp1_vdev_node vnode;
212         struct rkisp1_device *rkisp1;
213         enum rkisp1_stream_id id;
214         const struct rkisp1_capture_ops *ops;
215         const struct rkisp1_capture_config *config;
216         bool is_streaming;
217         bool is_stopping;
218         wait_queue_head_t done;
219         unsigned int sp_y_stride;
220         struct {
221                 /* protects queue, curr and next */
222                 spinlock_t lock;
223                 struct list_head queue;
224                 struct rkisp1_dummy_buffer dummy;
225                 struct rkisp1_buffer *curr;
226                 struct rkisp1_buffer *next;
227         } buf;
228         struct {
229                 const struct rkisp1_capture_fmt_cfg *cfg;
230                 const struct v4l2_format_info *info;
231                 struct v4l2_pix_format_mplane fmt;
232         } pix;
233 };
234
235 /*
236  * struct rkisp1_stats - ISP Statistics device
237  *
238  * @vnode:        video node
239  * @rkisp1:       pointer to the rkisp1 device
240  * @lock:         locks the buffer list 'stat'
241  * @stat:         queue of rkisp1_buffer
242  * @vdev_fmt:     v4l2_format of the metadata format
243  */
244 struct rkisp1_stats {
245         struct rkisp1_vdev_node vnode;
246         struct rkisp1_device *rkisp1;
247
248         spinlock_t lock; /* locks the buffers list 'stats' */
249         struct list_head stat;
250         struct v4l2_format vdev_fmt;
251 };
252
253 /*
254  * struct rkisp1_params - ISP input parameters device
255  *
256  * @vnode:              video node
257  * @rkisp1:             pointer to the rkisp1 device
258  * @config_lock:        locks the buffer list 'params'
259  * @params:             queue of rkisp1_buffer
260  * @vdev_fmt:           v4l2_format of the metadata format
261  * @quantization:       the quantization configured on the isp's src pad
262  * @raw_type:           the bayer pattern on the isp video sink pad
263  */
264 struct rkisp1_params {
265         struct rkisp1_vdev_node vnode;
266         struct rkisp1_device *rkisp1;
267
268         spinlock_t config_lock; /* locks the buffers list 'params' */
269         struct list_head params;
270         struct v4l2_format vdev_fmt;
271
272         enum v4l2_quantization quantization;
273         enum rkisp1_fmt_raw_pat_type raw_type;
274 };
275
276 /*
277  * struct rkisp1_resizer - Resizer subdev
278  *
279  * @sd:        v4l2_subdev variable
280  * @id:        id of the resizer, one of RKISP1_SELFPATH, RKISP1_MAINPATH
281  * @rkisp1:    pointer to the rkisp1 device
282  * @pads:      media pads
283  * @pad_cfg:   configurations for the pads
284  * @config:    the set of registers to configure the resizer
285  * @pixel_enc: pixel encoding of the resizer
286  * @ops_lock:  a lock for the subdev ops
287  */
288 struct rkisp1_resizer {
289         struct v4l2_subdev sd;
290         enum rkisp1_stream_id id;
291         struct rkisp1_device *rkisp1;
292         struct media_pad pads[RKISP1_RSZ_PAD_MAX];
293         struct v4l2_subdev_pad_config pad_cfg[RKISP1_RSZ_PAD_MAX];
294         const struct rkisp1_rsz_config *config;
295         enum v4l2_pixel_encoding pixel_enc;
296         struct mutex ops_lock; /* serialize the subdevice ops */
297 };
298
299 /*
300  * struct rkisp1_debug - Values to be exposed on debugfs.
301  *                       The parameters are counters of the number of times the
302  *                       event occurred since the driver was loaded.
303  *
304  * @data_loss:                    loss of data occurred within a line, processing failure
305  * @outform_size_error:           size error is generated in outmux submodule
306  * @img_stabilization_size_error: size error is generated in image stabilization submodule
307  * @inform_size_err:              size error is generated in inform submodule
308  * @mipi_error:                   mipi error occurred
309  * @stats_error:                  writing to the 'Interrupt clear register' did not clear
310  *                                it in the register 'Masked interrupt status'
311  * @stop_timeout:                 upon stream stop, the capture waits 1 second for the isr to stop
312  *                                the stream. This param is incremented in case of timeout.
313  * @frame_drop:                   a frame was ready but the buffer queue was empty so the frame
314  *                                was not sent to userspace
315  */
316 struct rkisp1_debug {
317         struct dentry *debugfs_dir;
318         unsigned long data_loss;
319         unsigned long outform_size_error;
320         unsigned long img_stabilization_size_error;
321         unsigned long inform_size_error;
322         unsigned long irq_delay;
323         unsigned long mipi_error;
324         unsigned long stats_error;
325         unsigned long stop_timeout[2];
326         unsigned long frame_drop[2];
327 };
328
329 /*
330  * struct rkisp1_device - ISP platform device
331  *
332  * @base_addr:     base register address
333  * @irq:           the irq number
334  * @dev:           a pointer to the struct device
335  * @clk_size:      number of clocks
336  * @clks:          array of clocks
337  * @v4l2_dev:      v4l2_device variable
338  * @media_dev:     media_device variable
339  * @notifier:      a notifier to register on the v4l2-async API to be notified on the sensor
340  * @active_sensor: sensor in-use, set when streaming on
341  * @isp:           ISP sub-device
342  * @resizer_devs:  resizer sub-devices
343  * @capture_devs:  capture devices
344  * @stats:         ISP statistics metadata capture device
345  * @params:        ISP parameters metadata output device
346  * @pipe:          media pipeline
347  * @stream_lock:   serializes {start/stop}_streaming callbacks between the capture devices.
348  * @debug:         debug params to be exposed on debugfs
349  */
350 struct rkisp1_device {
351         void __iomem *base_addr;
352         struct device *dev;
353         unsigned int clk_size;
354         struct clk_bulk_data clks[RKISP1_MAX_BUS_CLK];
355         struct v4l2_device v4l2_dev;
356         struct media_device media_dev;
357         struct v4l2_async_notifier notifier;
358         struct rkisp1_sensor_async *active_sensor;
359         struct rkisp1_isp isp;
360         struct rkisp1_resizer resizer_devs[2];
361         struct rkisp1_capture capture_devs[2];
362         struct rkisp1_stats stats;
363         struct rkisp1_params params;
364         struct media_pipeline pipe;
365         struct mutex stream_lock; /* serialize {start/stop}_streaming cb between capture devices */
366         struct rkisp1_debug debug;
367 };
368
369 /*
370  * struct rkisp1_isp_mbus_info - ISP media bus info, Translates media bus code to hardware
371  *                               format values
372  *
373  * @mbus_code: media bus code
374  * @pixel_enc: pixel encoding
375  * @mipi_dt:   mipi data type
376  * @yuv_seq:   the order of the Y, Cb, Cr values
377  * @bus_width: bus width
378  * @bayer_pat: bayer pattern
379  * @direction: a bitmask of the flags indicating on which pad the format is supported on
380  */
381 struct rkisp1_isp_mbus_info {
382         u32 mbus_code;
383         enum v4l2_pixel_encoding pixel_enc;
384         u32 mipi_dt;
385         u32 yuv_seq;
386         u8 bus_width;
387         enum rkisp1_fmt_raw_pat_type bayer_pat;
388         unsigned int direction;
389 };
390
391 static inline void
392 rkisp1_write(struct rkisp1_device *rkisp1, u32 val, unsigned int addr)
393 {
394         writel(val, rkisp1->base_addr + addr);
395 }
396
397 static inline u32 rkisp1_read(struct rkisp1_device *rkisp1, unsigned int addr)
398 {
399         return readl(rkisp1->base_addr + addr);
400 }
401
402 /*
403  * rkisp1_cap_enum_mbus_codes - A helper function that return the i'th supported mbus code
404  *                              of the capture entity. This is used to enumerate the supported
405  *                              mbus codes on the source pad of the resizer.
406  *
407  * @cap:  the capture entity
408  * @code: the mbus code, the function reads the code->index and fills the code->code
409  */
410 int rkisp1_cap_enum_mbus_codes(struct rkisp1_capture *cap,
411                                struct v4l2_subdev_mbus_code_enum *code);
412
413 /*
414  * rkisp1_sd_adjust_crop_rect - adjust a rectangle to fit into another rectangle.
415  *
416  * @crop:   rectangle to adjust.
417  * @bounds: rectangle used as bounds.
418  */
419 void rkisp1_sd_adjust_crop_rect(struct v4l2_rect *crop,
420                                 const struct v4l2_rect *bounds);
421
422 /*
423  * rkisp1_sd_adjust_crop - adjust a rectangle to fit into media bus format
424  *
425  * @crop:   rectangle to adjust.
426  * @bounds: media bus format used as bounds.
427  */
428 void rkisp1_sd_adjust_crop(struct v4l2_rect *crop,
429                            const struct v4l2_mbus_framefmt *bounds);
430
431 /*
432  * rkisp1_isp_mbus_info - get the isp info of the media bus code
433  *
434  * @mbus_code: the media bus code
435  */
436 const struct rkisp1_isp_mbus_info *rkisp1_isp_mbus_info_get(u32 mbus_code);
437
438 /* rkisp1_params_configure - configure the params when stream starts.
439  *                           This function is called by the isp entity upon stream starts.
440  *                           The function applies the initial configuration of the parameters.
441  *
442  * @params:       pointer to rkisp1_params.
443  * @bayer_pat:    the bayer pattern on the isp video sink pad
444  * @quantization: the quantization configured on the isp's src pad
445  */
446 void rkisp1_params_configure(struct rkisp1_params *params,
447                              enum rkisp1_fmt_raw_pat_type bayer_pat,
448                              enum v4l2_quantization quantization);
449
450 /* rkisp1_params_disable - disable all parameters.
451  *                         This function is called by the isp entity upon stream start
452  *                         when capturing bayer format.
453  *
454  * @params: pointer to rkisp1_params.
455  */
456 void rkisp1_params_disable(struct rkisp1_params *params);
457
458 /* irq handlers */
459 irqreturn_t rkisp1_isp_isr(int irq, void *ctx);
460 irqreturn_t rkisp1_mipi_isr(int irq, void *ctx);
461 irqreturn_t rkisp1_capture_isr(int irq, void *ctx);
462 void rkisp1_stats_isr(struct rkisp1_stats *stats, u32 isp_ris);
463 void rkisp1_params_isr(struct rkisp1_device *rkisp1);
464
465 /* register/unregisters functions of the entities */
466 int rkisp1_capture_devs_register(struct rkisp1_device *rkisp1);
467 void rkisp1_capture_devs_unregister(struct rkisp1_device *rkisp1);
468
469 int rkisp1_isp_register(struct rkisp1_device *rkisp1);
470 void rkisp1_isp_unregister(struct rkisp1_device *rkisp1);
471
472 int rkisp1_resizer_devs_register(struct rkisp1_device *rkisp1);
473 void rkisp1_resizer_devs_unregister(struct rkisp1_device *rkisp1);
474
475 int rkisp1_stats_register(struct rkisp1_device *rkisp1);
476 void rkisp1_stats_unregister(struct rkisp1_device *rkisp1);
477
478 int rkisp1_params_register(struct rkisp1_device *rkisp1);
479 void rkisp1_params_unregister(struct rkisp1_device *rkisp1);
480
481 #endif /* _RKISP1_COMMON_H */