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
8#include <media/mipi-csi2.h>
9#include <media/v4l2-rect.h>
10
11#include "rkisp1-common.h"
12
13static const struct rkisp1_mbus_info rkisp1_formats[] = {
14	{
15		.mbus_code	= MEDIA_BUS_FMT_YUYV8_2X8,
16		.pixel_enc	= V4L2_PIXEL_ENC_YUV,
17		.direction	= RKISP1_ISP_SD_SRC,
18	}, {
19		.mbus_code	= MEDIA_BUS_FMT_SRGGB10_1X10,
20		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
21		.mipi_dt	= MIPI_CSI2_DT_RAW10,
22		.bayer_pat	= RKISP1_RAW_RGGB,
23		.bus_width	= 10,
24		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
25	}, {
26		.mbus_code	= MEDIA_BUS_FMT_SBGGR10_1X10,
27		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
28		.mipi_dt	= MIPI_CSI2_DT_RAW10,
29		.bayer_pat	= RKISP1_RAW_BGGR,
30		.bus_width	= 10,
31		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
32	}, {
33		.mbus_code	= MEDIA_BUS_FMT_SGBRG10_1X10,
34		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
35		.mipi_dt	= MIPI_CSI2_DT_RAW10,
36		.bayer_pat	= RKISP1_RAW_GBRG,
37		.bus_width	= 10,
38		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
39	}, {
40		.mbus_code	= MEDIA_BUS_FMT_SGRBG10_1X10,
41		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
42		.mipi_dt	= MIPI_CSI2_DT_RAW10,
43		.bayer_pat	= RKISP1_RAW_GRBG,
44		.bus_width	= 10,
45		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
46	}, {
47		.mbus_code	= MEDIA_BUS_FMT_SRGGB12_1X12,
48		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
49		.mipi_dt	= MIPI_CSI2_DT_RAW12,
50		.bayer_pat	= RKISP1_RAW_RGGB,
51		.bus_width	= 12,
52		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
53	}, {
54		.mbus_code	= MEDIA_BUS_FMT_SBGGR12_1X12,
55		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
56		.mipi_dt	= MIPI_CSI2_DT_RAW12,
57		.bayer_pat	= RKISP1_RAW_BGGR,
58		.bus_width	= 12,
59		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
60	}, {
61		.mbus_code	= MEDIA_BUS_FMT_SGBRG12_1X12,
62		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
63		.mipi_dt	= MIPI_CSI2_DT_RAW12,
64		.bayer_pat	= RKISP1_RAW_GBRG,
65		.bus_width	= 12,
66		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
67	}, {
68		.mbus_code	= MEDIA_BUS_FMT_SGRBG12_1X12,
69		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
70		.mipi_dt	= MIPI_CSI2_DT_RAW12,
71		.bayer_pat	= RKISP1_RAW_GRBG,
72		.bus_width	= 12,
73		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
74	}, {
75		.mbus_code	= MEDIA_BUS_FMT_SRGGB8_1X8,
76		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
77		.mipi_dt	= MIPI_CSI2_DT_RAW8,
78		.bayer_pat	= RKISP1_RAW_RGGB,
79		.bus_width	= 8,
80		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
81	}, {
82		.mbus_code	= MEDIA_BUS_FMT_SBGGR8_1X8,
83		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
84		.mipi_dt	= MIPI_CSI2_DT_RAW8,
85		.bayer_pat	= RKISP1_RAW_BGGR,
86		.bus_width	= 8,
87		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
88	}, {
89		.mbus_code	= MEDIA_BUS_FMT_SGBRG8_1X8,
90		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
91		.mipi_dt	= MIPI_CSI2_DT_RAW8,
92		.bayer_pat	= RKISP1_RAW_GBRG,
93		.bus_width	= 8,
94		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
95	}, {
96		.mbus_code	= MEDIA_BUS_FMT_SGRBG8_1X8,
97		.pixel_enc	= V4L2_PIXEL_ENC_BAYER,
98		.mipi_dt	= MIPI_CSI2_DT_RAW8,
99		.bayer_pat	= RKISP1_RAW_GRBG,
100		.bus_width	= 8,
101		.direction	= RKISP1_ISP_SD_SINK | RKISP1_ISP_SD_SRC,
102	}, {
103		.mbus_code	= MEDIA_BUS_FMT_YUYV8_1X16,
104		.pixel_enc	= V4L2_PIXEL_ENC_YUV,
105		.mipi_dt	= MIPI_CSI2_DT_YUV422_8B,
106		.yuv_seq	= RKISP1_CIF_ISP_ACQ_PROP_YCBYCR,
107		.bus_width	= 16,
108		.direction	= RKISP1_ISP_SD_SINK,
109	}, {
110		.mbus_code	= MEDIA_BUS_FMT_YVYU8_1X16,
111		.pixel_enc	= V4L2_PIXEL_ENC_YUV,
112		.mipi_dt	= MIPI_CSI2_DT_YUV422_8B,
113		.yuv_seq	= RKISP1_CIF_ISP_ACQ_PROP_YCRYCB,
114		.bus_width	= 16,
115		.direction	= RKISP1_ISP_SD_SINK,
116	}, {
117		.mbus_code	= MEDIA_BUS_FMT_UYVY8_1X16,
118		.pixel_enc	= V4L2_PIXEL_ENC_YUV,
119		.mipi_dt	= MIPI_CSI2_DT_YUV422_8B,
120		.yuv_seq	= RKISP1_CIF_ISP_ACQ_PROP_CBYCRY,
121		.bus_width	= 16,
122		.direction	= RKISP1_ISP_SD_SINK,
123	}, {
124		.mbus_code	= MEDIA_BUS_FMT_VYUY8_1X16,
125		.pixel_enc	= V4L2_PIXEL_ENC_YUV,
126		.mipi_dt	= MIPI_CSI2_DT_YUV422_8B,
127		.yuv_seq	= RKISP1_CIF_ISP_ACQ_PROP_CRYCBY,
128		.bus_width	= 16,
129		.direction	= RKISP1_ISP_SD_SINK,
130	},
131};
132
133const struct rkisp1_mbus_info *rkisp1_mbus_info_get_by_index(unsigned int index)
134{
135	if (index >= ARRAY_SIZE(rkisp1_formats))
136		return NULL;
137
138	return &rkisp1_formats[index];
139}
140
141const struct rkisp1_mbus_info *rkisp1_mbus_info_get_by_code(u32 mbus_code)
142{
143	unsigned int i;
144
145	for (i = 0; i < ARRAY_SIZE(rkisp1_formats); i++) {
146		const struct rkisp1_mbus_info *fmt = &rkisp1_formats[i];
147
148		if (fmt->mbus_code == mbus_code)
149			return fmt;
150	}
151
152	return NULL;
153}
154
155static const struct v4l2_rect rkisp1_sd_min_crop = {
156	.width = RKISP1_ISP_MIN_WIDTH,
157	.height = RKISP1_ISP_MIN_HEIGHT,
158	.top = 0,
159	.left = 0,
160};
161
162void rkisp1_sd_adjust_crop_rect(struct v4l2_rect *crop,
163				const struct v4l2_rect *bounds)
164{
165	v4l2_rect_set_min_size(crop, &rkisp1_sd_min_crop);
166	v4l2_rect_map_inside(crop, bounds);
167}
168
169void rkisp1_sd_adjust_crop(struct v4l2_rect *crop,
170			   const struct v4l2_mbus_framefmt *bounds)
171{
172	struct v4l2_rect crop_bounds = {
173		.left = 0,
174		.top = 0,
175		.width = bounds->width,
176		.height = bounds->height,
177	};
178
179	rkisp1_sd_adjust_crop_rect(crop, &crop_bounds);
180}
181