1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Allied Vision Technologies GmbH Alvium camera driver
5 * Copyright (C) 2023 Tommaso Merciai
6 * Copyright (C) 2023 Martin Hecht
7 * Copyright (C) 2023 Avnet EMG GmbH
10 #ifndef ALVIUM_CSI2_H_
11 #define ALVIUM_CSI2_H_
13 #include <linux/kernel.h>
14 #include <linux/regulator/consumer.h>
15 #include <media/v4l2-cci.h>
16 #include <media/v4l2-common.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/v4l2-fwnode.h>
19 #include <media/v4l2-subdev.h>
21 #define REG_BCRM_V4L2 BIT(31)
23 #define REG_BCRM_V4L2_8BIT(n) (REG_BCRM_V4L2 | CCI_REG8(n))
24 #define REG_BCRM_V4L2_16BIT(n) (REG_BCRM_V4L2 | CCI_REG16(n))
25 #define REG_BCRM_V4L2_32BIT(n) (REG_BCRM_V4L2 | CCI_REG32(n))
26 #define REG_BCRM_V4L2_64BIT(n) (REG_BCRM_V4L2 | CCI_REG64(n))
28 /* Basic Control Register Map register offsets (BCRM) */
29 #define REG_BCRM_MINOR_VERSION_R CCI_REG16(0x0000)
30 #define REG_BCRM_MAJOR_VERSION_R CCI_REG16(0x0002)
31 #define REG_BCRM_REG_ADDR_R CCI_REG16(0x0014)
33 #define REG_BCRM_FEATURE_INQUIRY_R REG_BCRM_V4L2_64BIT(0x0008)
34 #define REG_BCRM_DEVICE_FW REG_BCRM_V4L2_64BIT(0x0010)
35 #define REG_BCRM_WRITE_HANDSHAKE_RW REG_BCRM_V4L2_8BIT(0x0018)
37 /* Streaming Control Registers */
38 #define REG_BCRM_SUPPORTED_CSI2_LANE_COUNTS_R REG_BCRM_V4L2_8BIT(0x0040)
39 #define REG_BCRM_CSI2_LANE_COUNT_RW REG_BCRM_V4L2_8BIT(0x0044)
40 #define REG_BCRM_CSI2_CLOCK_MIN_R REG_BCRM_V4L2_32BIT(0x0048)
41 #define REG_BCRM_CSI2_CLOCK_MAX_R REG_BCRM_V4L2_32BIT(0x004c)
42 #define REG_BCRM_CSI2_CLOCK_RW REG_BCRM_V4L2_32BIT(0x0050)
43 #define REG_BCRM_BUFFER_SIZE_R REG_BCRM_V4L2_32BIT(0x0054)
45 #define REG_BCRM_IPU_X_MIN_W REG_BCRM_V4L2_32BIT(0x0058)
46 #define REG_BCRM_IPU_X_MAX_W REG_BCRM_V4L2_32BIT(0x005c)
47 #define REG_BCRM_IPU_X_INC_W REG_BCRM_V4L2_32BIT(0x0060)
48 #define REG_BCRM_IPU_Y_MIN_W REG_BCRM_V4L2_32BIT(0x0064)
49 #define REG_BCRM_IPU_Y_MAX_W REG_BCRM_V4L2_32BIT(0x0068)
50 #define REG_BCRM_IPU_Y_INC_W REG_BCRM_V4L2_32BIT(0x006c)
51 #define REG_BCRM_IPU_X_R REG_BCRM_V4L2_32BIT(0x0070)
52 #define REG_BCRM_IPU_Y_R REG_BCRM_V4L2_32BIT(0x0074)
54 #define REG_BCRM_PHY_RESET_RW REG_BCRM_V4L2_8BIT(0x0078)
55 #define REG_BCRM_LP2HS_DELAY_RW REG_BCRM_V4L2_32BIT(0x007c)
57 /* Acquisition Control Registers */
58 #define REG_BCRM_ACQUISITION_START_RW REG_BCRM_V4L2_8BIT(0x0080)
59 #define REG_BCRM_ACQUISITION_STOP_RW REG_BCRM_V4L2_8BIT(0x0084)
60 #define REG_BCRM_ACQUISITION_ABORT_RW REG_BCRM_V4L2_8BIT(0x0088)
61 #define REG_BCRM_ACQUISITION_STATUS_R REG_BCRM_V4L2_8BIT(0x008c)
62 #define REG_BCRM_ACQUISITION_FRAME_RATE_RW REG_BCRM_V4L2_64BIT(0x0090)
63 #define REG_BCRM_ACQUISITION_FRAME_RATE_MIN_R REG_BCRM_V4L2_64BIT(0x0098)
64 #define REG_BCRM_ACQUISITION_FRAME_RATE_MAX_R REG_BCRM_V4L2_64BIT(0x00a0)
65 #define REG_BCRM_ACQUISITION_FRAME_RATE_INC_R REG_BCRM_V4L2_64BIT(0x00a8)
66 #define REG_BCRM_ACQUISITION_FRAME_RATE_EN_RW REG_BCRM_V4L2_8BIT(0x00b0)
68 #define REG_BCRM_FRAME_START_TRIGGER_MODE_RW REG_BCRM_V4L2_8BIT(0x00b4)
69 #define REG_BCRM_FRAME_START_TRIGGER_SOURCE_RW REG_BCRM_V4L2_8BIT(0x00b8)
70 #define REG_BCRM_FRAME_START_TRIGGER_ACTIVATION_RW REG_BCRM_V4L2_8BIT(0x00bc)
71 #define REG_BCRM_FRAME_START_TRIGGER_SOFTWARE_W REG_BCRM_V4L2_8BIT(0x00c0)
72 #define REG_BCRM_FRAME_START_TRIGGER_DELAY_RW REG_BCRM_V4L2_32BIT(0x00c4)
73 #define REG_BCRM_EXPOSURE_ACTIVE_LINE_MODE_RW REG_BCRM_V4L2_8BIT(0x00c8)
74 #define REG_BCRM_EXPOSURE_ACTIVE_LINE_SELECTOR_RW REG_BCRM_V4L2_8BIT(0x00cc)
75 #define REG_BCRM_LINE_CONFIGURATION_RW REG_BCRM_V4L2_32BIT(0x00d0)
77 #define REG_BCRM_IMG_WIDTH_RW REG_BCRM_V4L2_32BIT(0x0100)
78 #define REG_BCRM_IMG_WIDTH_MIN_R REG_BCRM_V4L2_32BIT(0x0104)
79 #define REG_BCRM_IMG_WIDTH_MAX_R REG_BCRM_V4L2_32BIT(0x0108)
80 #define REG_BCRM_IMG_WIDTH_INC_R REG_BCRM_V4L2_32BIT(0x010c)
82 #define REG_BCRM_IMG_HEIGHT_RW REG_BCRM_V4L2_32BIT(0x0110)
83 #define REG_BCRM_IMG_HEIGHT_MIN_R REG_BCRM_V4L2_32BIT(0x0114)
84 #define REG_BCRM_IMG_HEIGHT_MAX_R REG_BCRM_V4L2_32BIT(0x0118)
85 #define REG_BCRM_IMG_HEIGHT_INC_R REG_BCRM_V4L2_32BIT(0x011c)
87 #define REG_BCRM_IMG_OFFSET_X_RW REG_BCRM_V4L2_32BIT(0x0120)
88 #define REG_BCRM_IMG_OFFSET_X_MIN_R REG_BCRM_V4L2_32BIT(0x0124)
89 #define REG_BCRM_IMG_OFFSET_X_MAX_R REG_BCRM_V4L2_32BIT(0x0128)
90 #define REG_BCRM_IMG_OFFSET_X_INC_R REG_BCRM_V4L2_32BIT(0x012c)
92 #define REG_BCRM_IMG_OFFSET_Y_RW REG_BCRM_V4L2_32BIT(0x0130)
93 #define REG_BCRM_IMG_OFFSET_Y_MIN_R REG_BCRM_V4L2_32BIT(0x0134)
94 #define REG_BCRM_IMG_OFFSET_Y_MAX_R REG_BCRM_V4L2_32BIT(0x0138)
95 #define REG_BCRM_IMG_OFFSET_Y_INC_R REG_BCRM_V4L2_32BIT(0x013c)
97 #define REG_BCRM_IMG_MIPI_DATA_FORMAT_RW REG_BCRM_V4L2_32BIT(0x0140)
98 #define REG_BCRM_IMG_AVAILABLE_MIPI_DATA_FORMATS_R REG_BCRM_V4L2_64BIT(0x0148)
99 #define REG_BCRM_IMG_BAYER_PATTERN_INQUIRY_R REG_BCRM_V4L2_8BIT(0x0150)
100 #define REG_BCRM_IMG_BAYER_PATTERN_RW REG_BCRM_V4L2_8BIT(0x0154)
101 #define REG_BCRM_IMG_REVERSE_X_RW REG_BCRM_V4L2_8BIT(0x0158)
102 #define REG_BCRM_IMG_REVERSE_Y_RW REG_BCRM_V4L2_8BIT(0x015c)
104 #define REG_BCRM_SENSOR_WIDTH_R REG_BCRM_V4L2_32BIT(0x0160)
105 #define REG_BCRM_SENSOR_HEIGHT_R REG_BCRM_V4L2_32BIT(0x0164)
106 #define REG_BCRM_WIDTH_MAX_R REG_BCRM_V4L2_32BIT(0x0168)
107 #define REG_BCRM_HEIGHT_MAX_R REG_BCRM_V4L2_32BIT(0x016c)
109 #define REG_BCRM_EXPOSURE_TIME_RW REG_BCRM_V4L2_64BIT(0x0180)
110 #define REG_BCRM_EXPOSURE_TIME_MIN_R REG_BCRM_V4L2_64BIT(0x0188)
111 #define REG_BCRM_EXPOSURE_TIME_MAX_R REG_BCRM_V4L2_64BIT(0x0190)
112 #define REG_BCRM_EXPOSURE_TIME_INC_R REG_BCRM_V4L2_64BIT(0x0198)
113 #define REG_BCRM_EXPOSURE_AUTO_RW REG_BCRM_V4L2_8BIT(0x01a0)
115 #define REG_BCRM_INTENSITY_AUTO_PRECEDENCE_RW REG_BCRM_V4L2_8BIT(0x01a4)
116 #define REG_BCRM_INTENSITY_AUTO_PRECEDENCE_VALUE_RW REG_BCRM_V4L2_32BIT(0x01a8)
117 #define REG_BCRM_INTENSITY_AUTO_PRECEDENCE_MIN_R REG_BCRM_V4L2_32BIT(0x01ac)
118 #define REG_BCRM_INTENSITY_AUTO_PRECEDENCE_MAX_R REG_BCRM_V4L2_32BIT(0x01b0)
119 #define REG_BCRM_INTENSITY_AUTO_PRECEDENCE_INC_R REG_BCRM_V4L2_32BIT(0x01b4)
121 #define REG_BCRM_BLACK_LEVEL_RW REG_BCRM_V4L2_32BIT(0x01b8)
122 #define REG_BCRM_BLACK_LEVEL_MIN_R REG_BCRM_V4L2_32BIT(0x01bc)
123 #define REG_BCRM_BLACK_LEVEL_MAX_R REG_BCRM_V4L2_32BIT(0x01c0)
124 #define REG_BCRM_BLACK_LEVEL_INC_R REG_BCRM_V4L2_32BIT(0x01c4)
126 #define REG_BCRM_GAIN_RW REG_BCRM_V4L2_64BIT(0x01c8)
127 #define REG_BCRM_GAIN_MIN_R REG_BCRM_V4L2_64BIT(0x01d0)
128 #define REG_BCRM_GAIN_MAX_R REG_BCRM_V4L2_64BIT(0x01d8)
129 #define REG_BCRM_GAIN_INC_R REG_BCRM_V4L2_64BIT(0x01e0)
130 #define REG_BCRM_GAIN_AUTO_RW REG_BCRM_V4L2_8BIT(0x01e8)
132 #define REG_BCRM_GAMMA_RW REG_BCRM_V4L2_64BIT(0x01f0)
133 #define REG_BCRM_GAMMA_MIN_R REG_BCRM_V4L2_64BIT(0x01f8)
134 #define REG_BCRM_GAMMA_MAX_R REG_BCRM_V4L2_64BIT(0x0200)
135 #define REG_BCRM_GAMMA_INC_R REG_BCRM_V4L2_64BIT(0x0208)
137 #define REG_BCRM_CONTRAST_VALUE_RW REG_BCRM_V4L2_32BIT(0x0214)
138 #define REG_BCRM_CONTRAST_VALUE_MIN_R REG_BCRM_V4L2_32BIT(0x0218)
139 #define REG_BCRM_CONTRAST_VALUE_MAX_R REG_BCRM_V4L2_32BIT(0x021c)
140 #define REG_BCRM_CONTRAST_VALUE_INC_R REG_BCRM_V4L2_32BIT(0x0220)
142 #define REG_BCRM_SATURATION_RW REG_BCRM_V4L2_32BIT(0x0240)
143 #define REG_BCRM_SATURATION_MIN_R REG_BCRM_V4L2_32BIT(0x0244)
144 #define REG_BCRM_SATURATION_MAX_R REG_BCRM_V4L2_32BIT(0x0248)
145 #define REG_BCRM_SATURATION_INC_R REG_BCRM_V4L2_32BIT(0x024c)
147 #define REG_BCRM_HUE_RW REG_BCRM_V4L2_32BIT(0x0250)
148 #define REG_BCRM_HUE_MIN_R REG_BCRM_V4L2_32BIT(0x0254)
149 #define REG_BCRM_HUE_MAX_R REG_BCRM_V4L2_32BIT(0x0258)
150 #define REG_BCRM_HUE_INC_R REG_BCRM_V4L2_32BIT(0x025c)
152 #define REG_BCRM_ALL_BALANCE_RATIO_RW REG_BCRM_V4L2_64BIT(0x0260)
153 #define REG_BCRM_ALL_BALANCE_RATIO_MIN_R REG_BCRM_V4L2_64BIT(0x0268)
154 #define REG_BCRM_ALL_BALANCE_RATIO_MAX_R REG_BCRM_V4L2_64BIT(0x0270)
155 #define REG_BCRM_ALL_BALANCE_RATIO_INC_R REG_BCRM_V4L2_64BIT(0x0278)
157 #define REG_BCRM_RED_BALANCE_RATIO_RW REG_BCRM_V4L2_64BIT(0x0280)
158 #define REG_BCRM_RED_BALANCE_RATIO_MIN_R REG_BCRM_V4L2_64BIT(0x0288)
159 #define REG_BCRM_RED_BALANCE_RATIO_MAX_R REG_BCRM_V4L2_64BIT(0x0290)
160 #define REG_BCRM_RED_BALANCE_RATIO_INC_R REG_BCRM_V4L2_64BIT(0x0298)
162 #define REG_BCRM_GREEN_BALANCE_RATIO_RW REG_BCRM_V4L2_64BIT(0x02a0)
163 #define REG_BCRM_GREEN_BALANCE_RATIO_MIN_R REG_BCRM_V4L2_64BIT(0x02a8)
164 #define REG_BCRM_GREEN_BALANCE_RATIO_MAX_R REG_BCRM_V4L2_64BIT(0x02b0)
165 #define REG_BCRM_GREEN_BALANCE_RATIO_INC_R REG_BCRM_V4L2_64BIT(0x02b8)
167 #define REG_BCRM_BLUE_BALANCE_RATIO_RW REG_BCRM_V4L2_64BIT(0x02c0)
168 #define REG_BCRM_BLUE_BALANCE_RATIO_MIN_R REG_BCRM_V4L2_64BIT(0x02c8)
169 #define REG_BCRM_BLUE_BALANCE_RATIO_MAX_R REG_BCRM_V4L2_64BIT(0x02d0)
170 #define REG_BCRM_BLUE_BALANCE_RATIO_INC_R REG_BCRM_V4L2_64BIT(0x02d8)
172 #define REG_BCRM_WHITE_BALANCE_AUTO_RW REG_BCRM_V4L2_8BIT(0x02e0)
173 #define REG_BCRM_SHARPNESS_RW REG_BCRM_V4L2_32BIT(0x0300)
174 #define REG_BCRM_SHARPNESS_MIN_R REG_BCRM_V4L2_32BIT(0x0304)
175 #define REG_BCRM_SHARPNESS_MAX_R REG_BCRM_V4L2_32BIT(0x0308)
176 #define REG_BCRM_SHARPNESS_INC_R REG_BCRM_V4L2_32BIT(0x030c)
178 #define REG_BCRM_DEVICE_TEMPERATURE_R REG_BCRM_V4L2_32BIT(0x0310)
179 #define REG_BCRM_EXPOSURE_AUTO_MIN_RW REG_BCRM_V4L2_64BIT(0x0330)
180 #define REG_BCRM_EXPOSURE_AUTO_MAX_RW REG_BCRM_V4L2_64BIT(0x0338)
181 #define REG_BCRM_GAIN_AUTO_MIN_RW REG_BCRM_V4L2_64BIT(0x0340)
182 #define REG_BCRM_GAIN_AUTO_MAX_RW REG_BCRM_V4L2_64BIT(0x0348)
185 #define REG_BCRM_HEARTBEAT_RW CCI_REG8(0x021f)
187 /* GenCP Registers */
188 #define REG_GENCP_CHANGEMODE_W CCI_REG8(0x021c)
189 #define REG_GENCP_CURRENTMODE_R CCI_REG8(0x021d)
190 #define REG_GENCP_IN_HANDSHAKE_RW CCI_REG8(0x001c)
191 #define REG_GENCP_OUT_SIZE_W CCI_REG16(0x0020)
192 #define REG_GENCP_IN_SIZE_R CCI_REG16(0x0024)
195 #define REG_BCRM_HANDSHAKE_STATUS_MASK 0x01
196 #define REG_BCRM_HANDSHAKE_AVAILABLE_MASK 0x80
198 #define BCRM_HANDSHAKE_W_DONE_EN_BIT BIT(0)
200 #define ALVIUM_DEFAULT_FR_HZ 10
201 #define ALVIUM_DEFAULT_PIXEL_RATE_MHZ 148000000
203 #define ALVIUM_LP2HS_DELAY_MS 100
205 #define BCRM_DEVICE_FW_MAJOR_MASK GENMASK_ULL(15, 8)
206 #define BCRM_DEVICE_FW_MAJOR_SHIFT 8
207 #define BCRM_DEVICE_FW_MINOR_MASK GENMASK_ULL(31, 16)
208 #define BCRM_DEVICE_FW_MINOR_SHIFT 16
209 #define BCRM_DEVICE_FW_PATCH_MASK GENMASK_ULL(63, 32)
210 #define BCRM_DEVICE_FW_PATCH_SHIFT 32
211 #define BCRM_DEVICE_FW_SPEC_MASK GENMASK_ULL(7, 0)
212 #define BCRM_DEVICE_FW_SPEC_SHIFT 0
214 enum alvium_bcrm_mode
{
220 enum alvium_mipi_fmt
{
221 ALVIUM_FMT_UYVY8_2X8
= 0,
222 ALVIUM_FMT_UYVY8_1X16
,
223 ALVIUM_FMT_YUYV8_1X16
,
224 ALVIUM_FMT_YUYV8_2X8
,
225 ALVIUM_FMT_YUYV10_1X20
,
226 ALVIUM_FMT_RGB888_1X24
,
227 ALVIUM_FMT_RBG888_1X24
,
228 ALVIUM_FMT_BGR888_1X24
,
229 ALVIUM_FMT_RGB888_3X8
,
231 ALVIUM_FMT_SGRBG8_1X8
,
232 ALVIUM_FMT_SRGGB8_1X8
,
233 ALVIUM_FMT_SGBRG8_1X8
,
234 ALVIUM_FMT_SBGGR8_1X8
,
236 ALVIUM_FMT_SGRBG10_1X10
,
237 ALVIUM_FMT_SRGGB10_1X10
,
238 ALVIUM_FMT_SGBRG10_1X10
,
239 ALVIUM_FMT_SBGGR10_1X10
,
241 ALVIUM_FMT_SGRBG12_1X12
,
242 ALVIUM_FMT_SRGGB12_1X12
,
243 ALVIUM_FMT_SGBRG12_1X12
,
244 ALVIUM_FMT_SBGGR12_1X12
,
245 ALVIUM_FMT_SBGGR14_1X14
,
246 ALVIUM_FMT_SGBRG14_1X14
,
247 ALVIUM_FMT_SRGGB14_1X14
,
248 ALVIUM_FMT_SGRBG14_1X14
,
249 ALVIUM_NUM_SUPP_MIPI_DATA_FMT
252 enum alvium_av_bayer_bit
{
253 ALVIUM_BIT_BAY_NONE
= -1,
254 ALVIUM_BIT_BAY_MONO
= 0,
259 ALVIUM_NUM_BAY_AV_BIT
262 enum alvium_av_mipi_bit
{
263 ALVIUM_BIT_YUV420_8_LEG
= 0,
265 ALVIUM_BIT_YUV420_10
,
266 ALVIUM_BIT_YUV420_8_CSPS
,
267 ALVIUM_BIT_YUV420_10_CSPS
,
269 ALVIUM_BIT_YUV422_10
,
282 ALVIUM_NUM_SUPP_MIPI_DATA_BIT
285 struct alvium_avail_feat
{
308 struct alvium_avail_mipi_fmt
{
313 u64 yuv420_10_csps
:1;
331 struct alvium_avail_bayer
{
341 struct v4l2_rect crop
;
342 struct v4l2_mbus_framefmt fmt
;
347 struct alvium_pixfmt
{
358 struct alvium_ctrls
{
359 struct v4l2_ctrl_handler handler
;
360 struct v4l2_ctrl
*pixel_rate
;
361 struct v4l2_ctrl
*link_freq
;
362 struct v4l2_ctrl
*auto_exp
;
363 struct v4l2_ctrl
*exposure
;
364 struct v4l2_ctrl
*auto_wb
;
365 struct v4l2_ctrl
*blue_balance
;
366 struct v4l2_ctrl
*red_balance
;
367 struct v4l2_ctrl
*auto_gain
;
368 struct v4l2_ctrl
*gain
;
369 struct v4l2_ctrl
*saturation
;
370 struct v4l2_ctrl
*hue
;
371 struct v4l2_ctrl
*contrast
;
372 struct v4l2_ctrl
*gamma
;
373 struct v4l2_ctrl
*sharpness
;
374 struct v4l2_ctrl
*hflip
;
375 struct v4l2_ctrl
*vflip
;
379 struct i2c_client
*i2c_client
;
380 struct v4l2_subdev sd
;
381 struct v4l2_fwnode_endpoint ep
;
382 struct media_pad pad
;
383 struct regmap
*regmap
;
385 struct regulator
*reg_vcc
;
389 struct alvium_avail_feat avail_ft
;
390 u8 is_mipi_fmt_avail
[ALVIUM_NUM_SUPP_MIPI_DATA_BIT
];
391 u8 is_bay_avail
[ALVIUM_NUM_BAY_AV_BIT
];
450 struct alvium_mode mode
;
455 struct alvium_ctrls ctrls
;
459 struct alvium_pixfmt
*alvium_csi2_fmt
;
460 u8 alvium_csi2_fmt_n
;
466 static inline struct alvium_dev
*sd_to_alvium(struct v4l2_subdev
*sd
)
468 return container_of_const(sd
, struct alvium_dev
, sd
);
471 static inline struct v4l2_subdev
*ctrl_to_sd(struct v4l2_ctrl
*ctrl
)
473 return &container_of_const(ctrl
->handler
, struct alvium_dev
,
476 #endif /* ALVIUM_CSI2_H_ */