module: Convert symbol namespace to string literal
[linux.git] / drivers / media / i2c / ds90ub960.c
blob1b1ff7f7505b00949c2e8bd848888d4c29909a16
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Driver for the Texas Instruments DS90UB960-Q1 video deserializer
5 * Copyright (c) 2019 Luca Ceresoli <luca@lucaceresoli.net>
6 * Copyright (c) 2023 Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
7 */
9 /*
10 * (Possible) TODOs:
12 * - PM for serializer and remote peripherals. We need to manage:
13 * - VPOC
14 * - Power domain? Regulator? Somehow any remote device should be able to
15 * cause the VPOC to be turned on.
16 * - Link between the deserializer and the serializer
17 * - Related to VPOC management. We probably always want to turn on the VPOC
18 * and then enable the link.
19 * - Serializer's services: i2c, gpios, power
20 * - The serializer needs to resume before the remote peripherals can
21 * e.g. use the i2c.
22 * - How to handle gpios? Reserving a gpio essentially keeps the provider
23 * (serializer) always powered on.
24 * - Do we need a new bus for the FPD-Link? At the moment the serializers
25 * are children of the same i2c-adapter where the deserializer resides.
26 * - i2c-atr could be made embeddable instead of allocatable.
29 #include <linux/bitops.h>
30 #include <linux/clk.h>
31 #include <linux/delay.h>
32 #include <linux/fwnode.h>
33 #include <linux/gpio/consumer.h>
34 #include <linux/i2c-atr.h>
35 #include <linux/i2c.h>
36 #include <linux/init.h>
37 #include <linux/interrupt.h>
38 #include <linux/kernel.h>
39 #include <linux/kthread.h>
40 #include <linux/module.h>
41 #include <linux/mutex.h>
42 #include <linux/property.h>
43 #include <linux/regmap.h>
44 #include <linux/regulator/consumer.h>
45 #include <linux/slab.h>
46 #include <linux/workqueue.h>
48 #include <media/i2c/ds90ub9xx.h>
49 #include <media/mipi-csi2.h>
50 #include <media/v4l2-ctrls.h>
51 #include <media/v4l2-fwnode.h>
52 #include <media/v4l2-subdev.h>
54 #define MHZ(v) ((u32)((v) * 1000000U))
56 #define UB960_POLL_TIME_MS 500
58 #define UB960_MAX_RX_NPORTS 4
59 #define UB960_MAX_TX_NPORTS 2
60 #define UB960_MAX_NPORTS (UB960_MAX_RX_NPORTS + UB960_MAX_TX_NPORTS)
62 #define UB960_MAX_PORT_ALIASES 8
64 #define UB960_NUM_BC_GPIOS 4
67 * Register map
69 * 0x00-0x32 Shared (UB960_SR)
70 * 0x33-0x3a CSI-2 TX (per-port paged on DS90UB960, shared on 954) (UB960_TR)
71 * 0x4c Shared (UB960_SR)
72 * 0x4d-0x7f FPD-Link RX, per-port paged (UB960_RR)
73 * 0xb0-0xbf Shared (UB960_SR)
74 * 0xd0-0xdf FPD-Link RX, per-port paged (UB960_RR)
75 * 0xf0-0xf5 Shared (UB960_SR)
76 * 0xf8-0xfb Shared (UB960_SR)
77 * All others Reserved
79 * Register prefixes:
80 * UB960_SR_* = Shared register
81 * UB960_RR_* = FPD-Link RX, per-port paged register
82 * UB960_TR_* = CSI-2 TX, per-port paged register
83 * UB960_XR_* = Reserved register
84 * UB960_IR_* = Indirect register
87 #define UB960_SR_I2C_DEV_ID 0x00
88 #define UB960_SR_RESET 0x01
89 #define UB960_SR_RESET_DIGITAL_RESET1 BIT(1)
90 #define UB960_SR_RESET_DIGITAL_RESET0 BIT(0)
91 #define UB960_SR_RESET_GPIO_LOCK_RELEASE BIT(5)
93 #define UB960_SR_GEN_CONFIG 0x02
94 #define UB960_SR_REV_MASK 0x03
95 #define UB960_SR_DEVICE_STS 0x04
96 #define UB960_SR_PAR_ERR_THOLD_HI 0x05
97 #define UB960_SR_PAR_ERR_THOLD_LO 0x06
98 #define UB960_SR_BCC_WDOG_CTL 0x07
99 #define UB960_SR_I2C_CTL1 0x08
100 #define UB960_SR_I2C_CTL2 0x09
101 #define UB960_SR_SCL_HIGH_TIME 0x0a
102 #define UB960_SR_SCL_LOW_TIME 0x0b
103 #define UB960_SR_RX_PORT_CTL 0x0c
104 #define UB960_SR_IO_CTL 0x0d
105 #define UB960_SR_GPIO_PIN_STS 0x0e
106 #define UB960_SR_GPIO_INPUT_CTL 0x0f
107 #define UB960_SR_GPIO_PIN_CTL(n) (0x10 + (n)) /* n < UB960_NUM_GPIOS */
108 #define UB960_SR_GPIO_PIN_CTL_GPIO_OUT_SEL 5
109 #define UB960_SR_GPIO_PIN_CTL_GPIO_OUT_SRC_SHIFT 2
110 #define UB960_SR_GPIO_PIN_CTL_GPIO_OUT_EN BIT(0)
112 #define UB960_SR_FS_CTL 0x18
113 #define UB960_SR_FS_HIGH_TIME_1 0x19
114 #define UB960_SR_FS_HIGH_TIME_0 0x1a
115 #define UB960_SR_FS_LOW_TIME_1 0x1b
116 #define UB960_SR_FS_LOW_TIME_0 0x1c
117 #define UB960_SR_MAX_FRM_HI 0x1d
118 #define UB960_SR_MAX_FRM_LO 0x1e
119 #define UB960_SR_CSI_PLL_CTL 0x1f
121 #define UB960_SR_FWD_CTL1 0x20
122 #define UB960_SR_FWD_CTL1_PORT_DIS(n) BIT((n) + 4)
124 #define UB960_SR_FWD_CTL2 0x21
125 #define UB960_SR_FWD_STS 0x22
127 #define UB960_SR_INTERRUPT_CTL 0x23
128 #define UB960_SR_INTERRUPT_CTL_INT_EN BIT(7)
129 #define UB960_SR_INTERRUPT_CTL_IE_CSI_TX0 BIT(4)
130 #define UB960_SR_INTERRUPT_CTL_IE_RX(n) BIT((n)) /* rxport[n] IRQ */
132 #define UB960_SR_INTERRUPT_STS 0x24
133 #define UB960_SR_INTERRUPT_STS_INT BIT(7)
134 #define UB960_SR_INTERRUPT_STS_IS_CSI_TX(n) BIT(4 + (n)) /* txport[n] IRQ */
135 #define UB960_SR_INTERRUPT_STS_IS_RX(n) BIT((n)) /* rxport[n] IRQ */
137 #define UB960_SR_TS_CONFIG 0x25
138 #define UB960_SR_TS_CONTROL 0x26
139 #define UB960_SR_TS_LINE_HI 0x27
140 #define UB960_SR_TS_LINE_LO 0x28
141 #define UB960_SR_TS_STATUS 0x29
142 #define UB960_SR_TIMESTAMP_P0_HI 0x2a
143 #define UB960_SR_TIMESTAMP_P0_LO 0x2b
144 #define UB960_SR_TIMESTAMP_P1_HI 0x2c
145 #define UB960_SR_TIMESTAMP_P1_LO 0x2d
147 #define UB960_SR_CSI_PORT_SEL 0x32
149 #define UB960_TR_CSI_CTL 0x33
150 #define UB960_TR_CSI_CTL_CSI_CAL_EN BIT(6)
151 #define UB960_TR_CSI_CTL_CSI_CONTS_CLOCK BIT(1)
152 #define UB960_TR_CSI_CTL_CSI_ENABLE BIT(0)
154 #define UB960_TR_CSI_CTL2 0x34
155 #define UB960_TR_CSI_STS 0x35
156 #define UB960_TR_CSI_TX_ICR 0x36
158 #define UB960_TR_CSI_TX_ISR 0x37
159 #define UB960_TR_CSI_TX_ISR_IS_CSI_SYNC_ERROR BIT(3)
160 #define UB960_TR_CSI_TX_ISR_IS_CSI_PASS_ERROR BIT(1)
162 #define UB960_TR_CSI_TEST_CTL 0x38
163 #define UB960_TR_CSI_TEST_PATT_HI 0x39
164 #define UB960_TR_CSI_TEST_PATT_LO 0x3a
166 #define UB960_XR_SFILTER_CFG 0x41
167 #define UB960_XR_SFILTER_CFG_SFILTER_MAX_SHIFT 4
168 #define UB960_XR_SFILTER_CFG_SFILTER_MIN_SHIFT 0
170 #define UB960_XR_AEQ_CTL1 0x42
171 #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_FPD_CLK BIT(6)
172 #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_ENCODING BIT(5)
173 #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_PARITY BIT(4)
174 #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_MASK \
175 (UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_FPD_CLK | \
176 UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_ENCODING | \
177 UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_PARITY)
178 #define UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN BIT(0)
180 #define UB960_XR_AEQ_ERR_THOLD 0x43
182 #define UB960_RR_BCC_ERR_CTL 0x46
183 #define UB960_RR_BCC_STATUS 0x47
184 #define UB960_RR_BCC_STATUS_SEQ_ERROR BIT(5)
185 #define UB960_RR_BCC_STATUS_MASTER_ERR BIT(4)
186 #define UB960_RR_BCC_STATUS_MASTER_TO BIT(3)
187 #define UB960_RR_BCC_STATUS_SLAVE_ERR BIT(2)
188 #define UB960_RR_BCC_STATUS_SLAVE_TO BIT(1)
189 #define UB960_RR_BCC_STATUS_RESP_ERR BIT(0)
190 #define UB960_RR_BCC_STATUS_ERROR_MASK \
191 (UB960_RR_BCC_STATUS_SEQ_ERROR | UB960_RR_BCC_STATUS_MASTER_ERR | \
192 UB960_RR_BCC_STATUS_MASTER_TO | UB960_RR_BCC_STATUS_SLAVE_ERR | \
193 UB960_RR_BCC_STATUS_SLAVE_TO | UB960_RR_BCC_STATUS_RESP_ERR)
195 #define UB960_RR_FPD3_CAP 0x4a
196 #define UB960_RR_RAW_EMBED_DTYPE 0x4b
197 #define UB960_RR_RAW_EMBED_DTYPE_LINES_SHIFT 6
199 #define UB960_SR_FPD3_PORT_SEL 0x4c
201 #define UB960_RR_RX_PORT_STS1 0x4d
202 #define UB960_RR_RX_PORT_STS1_BCC_CRC_ERROR BIT(5)
203 #define UB960_RR_RX_PORT_STS1_LOCK_STS_CHG BIT(4)
204 #define UB960_RR_RX_PORT_STS1_BCC_SEQ_ERROR BIT(3)
205 #define UB960_RR_RX_PORT_STS1_PARITY_ERROR BIT(2)
206 #define UB960_RR_RX_PORT_STS1_PORT_PASS BIT(1)
207 #define UB960_RR_RX_PORT_STS1_LOCK_STS BIT(0)
208 #define UB960_RR_RX_PORT_STS1_ERROR_MASK \
209 (UB960_RR_RX_PORT_STS1_BCC_CRC_ERROR | \
210 UB960_RR_RX_PORT_STS1_BCC_SEQ_ERROR | \
211 UB960_RR_RX_PORT_STS1_PARITY_ERROR)
213 #define UB960_RR_RX_PORT_STS2 0x4e
214 #define UB960_RR_RX_PORT_STS2_LINE_LEN_UNSTABLE BIT(7)
215 #define UB960_RR_RX_PORT_STS2_LINE_LEN_CHG BIT(6)
216 #define UB960_RR_RX_PORT_STS2_FPD3_ENCODE_ERROR BIT(5)
217 #define UB960_RR_RX_PORT_STS2_BUFFER_ERROR BIT(4)
218 #define UB960_RR_RX_PORT_STS2_CSI_ERROR BIT(3)
219 #define UB960_RR_RX_PORT_STS2_FREQ_STABLE BIT(2)
220 #define UB960_RR_RX_PORT_STS2_CABLE_FAULT BIT(1)
221 #define UB960_RR_RX_PORT_STS2_LINE_CNT_CHG BIT(0)
222 #define UB960_RR_RX_PORT_STS2_ERROR_MASK \
223 UB960_RR_RX_PORT_STS2_BUFFER_ERROR
225 #define UB960_RR_RX_FREQ_HIGH 0x4f
226 #define UB960_RR_RX_FREQ_LOW 0x50
227 #define UB960_RR_SENSOR_STS_0 0x51
228 #define UB960_RR_SENSOR_STS_1 0x52
229 #define UB960_RR_SENSOR_STS_2 0x53
230 #define UB960_RR_SENSOR_STS_3 0x54
231 #define UB960_RR_RX_PAR_ERR_HI 0x55
232 #define UB960_RR_RX_PAR_ERR_LO 0x56
233 #define UB960_RR_BIST_ERR_COUNT 0x57
235 #define UB960_RR_BCC_CONFIG 0x58
236 #define UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH BIT(6)
237 #define UB960_RR_BCC_CONFIG_BC_FREQ_SEL_MASK GENMASK(2, 0)
239 #define UB960_RR_DATAPATH_CTL1 0x59
240 #define UB960_RR_DATAPATH_CTL2 0x5a
241 #define UB960_RR_SER_ID 0x5b
242 #define UB960_RR_SER_ALIAS_ID 0x5c
244 /* For these two register sets: n < UB960_MAX_PORT_ALIASES */
245 #define UB960_RR_SLAVE_ID(n) (0x5d + (n))
246 #define UB960_RR_SLAVE_ALIAS(n) (0x65 + (n))
248 #define UB960_RR_PORT_CONFIG 0x6d
249 #define UB960_RR_PORT_CONFIG_FPD3_MODE_MASK GENMASK(1, 0)
251 #define UB960_RR_BC_GPIO_CTL(n) (0x6e + (n)) /* n < 2 */
252 #define UB960_RR_RAW10_ID 0x70
253 #define UB960_RR_RAW10_ID_VC_SHIFT 6
254 #define UB960_RR_RAW10_ID_DT_SHIFT 0
256 #define UB960_RR_RAW12_ID 0x71
257 #define UB960_RR_CSI_VC_MAP 0x72
258 #define UB960_RR_CSI_VC_MAP_SHIFT(x) ((x) * 2)
260 #define UB960_RR_LINE_COUNT_HI 0x73
261 #define UB960_RR_LINE_COUNT_LO 0x74
262 #define UB960_RR_LINE_LEN_1 0x75
263 #define UB960_RR_LINE_LEN_0 0x76
264 #define UB960_RR_FREQ_DET_CTL 0x77
265 #define UB960_RR_MAILBOX_1 0x78
266 #define UB960_RR_MAILBOX_2 0x79
268 #define UB960_RR_CSI_RX_STS 0x7a
269 #define UB960_RR_CSI_RX_STS_LENGTH_ERR BIT(3)
270 #define UB960_RR_CSI_RX_STS_CKSUM_ERR BIT(2)
271 #define UB960_RR_CSI_RX_STS_ECC2_ERR BIT(1)
272 #define UB960_RR_CSI_RX_STS_ECC1_ERR BIT(0)
273 #define UB960_RR_CSI_RX_STS_ERROR_MASK \
274 (UB960_RR_CSI_RX_STS_LENGTH_ERR | UB960_RR_CSI_RX_STS_CKSUM_ERR | \
275 UB960_RR_CSI_RX_STS_ECC2_ERR | UB960_RR_CSI_RX_STS_ECC1_ERR)
277 #define UB960_RR_CSI_ERR_COUNTER 0x7b
278 #define UB960_RR_PORT_CONFIG2 0x7c
279 #define UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_MASK GENMASK(7, 6)
280 #define UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_SHIFT 6
282 #define UB960_RR_PORT_CONFIG2_LV_POL_LOW BIT(1)
283 #define UB960_RR_PORT_CONFIG2_FV_POL_LOW BIT(0)
285 #define UB960_RR_PORT_PASS_CTL 0x7d
286 #define UB960_RR_SEN_INT_RISE_CTL 0x7e
287 #define UB960_RR_SEN_INT_FALL_CTL 0x7f
289 #define UB960_SR_CSI_FRAME_COUNT_HI(n) (0x90 + 8 * (n))
290 #define UB960_SR_CSI_FRAME_COUNT_LO(n) (0x91 + 8 * (n))
291 #define UB960_SR_CSI_FRAME_ERR_COUNT_HI(n) (0x92 + 8 * (n))
292 #define UB960_SR_CSI_FRAME_ERR_COUNT_LO(n) (0x93 + 8 * (n))
293 #define UB960_SR_CSI_LINE_COUNT_HI(n) (0x94 + 8 * (n))
294 #define UB960_SR_CSI_LINE_COUNT_LO(n) (0x95 + 8 * (n))
295 #define UB960_SR_CSI_LINE_ERR_COUNT_HI(n) (0x96 + 8 * (n))
296 #define UB960_SR_CSI_LINE_ERR_COUNT_LO(n) (0x97 + 8 * (n))
298 #define UB960_XR_REFCLK_FREQ 0xa5 /* UB960 */
300 #define UB960_RR_VC_ID_MAP(x) (0xa0 + (x)) /* UB9702 */
302 #define UB960_SR_IND_ACC_CTL 0xb0
303 #define UB960_SR_IND_ACC_CTL_IA_AUTO_INC BIT(1)
305 #define UB960_SR_IND_ACC_ADDR 0xb1
306 #define UB960_SR_IND_ACC_DATA 0xb2
307 #define UB960_SR_BIST_CONTROL 0xb3
308 #define UB960_SR_MODE_IDX_STS 0xb8
309 #define UB960_SR_LINK_ERROR_COUNT 0xb9
310 #define UB960_SR_FPD3_ENC_CTL 0xba
311 #define UB960_SR_FV_MIN_TIME 0xbc
312 #define UB960_SR_GPIO_PD_CTL 0xbe
314 #define UB960_SR_FPD_RATE_CFG 0xc2 /* UB9702 */
315 #define UB960_SR_CSI_PLL_DIV 0xc9 /* UB9702 */
317 #define UB960_RR_PORT_DEBUG 0xd0
318 #define UB960_RR_AEQ_CTL2 0xd2
319 #define UB960_RR_AEQ_CTL2_SET_AEQ_FLOOR BIT(2)
321 #define UB960_RR_AEQ_STATUS 0xd3
322 #define UB960_RR_AEQ_STATUS_STATUS_2 GENMASK(5, 3)
323 #define UB960_RR_AEQ_STATUS_STATUS_1 GENMASK(2, 0)
325 #define UB960_RR_AEQ_BYPASS 0xd4
326 #define UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_SHIFT 5
327 #define UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_MASK GENMASK(7, 5)
328 #define UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_SHIFT 1
329 #define UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_MASK GENMASK(3, 1)
330 #define UB960_RR_AEQ_BYPASS_ENABLE BIT(0)
332 #define UB960_RR_AEQ_MIN_MAX 0xd5
333 #define UB960_RR_AEQ_MIN_MAX_AEQ_MAX_SHIFT 4
334 #define UB960_RR_AEQ_MIN_MAX_AEQ_FLOOR_SHIFT 0
336 #define UB960_RR_SFILTER_STS_0 0xd6
337 #define UB960_RR_SFILTER_STS_1 0xd7
338 #define UB960_RR_PORT_ICR_HI 0xd8
339 #define UB960_RR_PORT_ICR_LO 0xd9
340 #define UB960_RR_PORT_ISR_HI 0xda
341 #define UB960_RR_PORT_ISR_LO 0xdb
342 #define UB960_RR_FC_GPIO_STS 0xdc
343 #define UB960_RR_FC_GPIO_ICR 0xdd
344 #define UB960_RR_SEN_INT_RISE_STS 0xde
345 #define UB960_RR_SEN_INT_FALL_STS 0xdf
347 #define UB960_RR_CHANNEL_MODE 0xe4 /* UB9702 */
349 #define UB960_SR_FPD3_RX_ID(n) (0xf0 + (n))
350 #define UB960_SR_FPD3_RX_ID_LEN 6
352 #define UB960_SR_I2C_RX_ID(n) (0xf8 + (n)) /* < UB960_FPD_RX_NPORTS */
354 /* Indirect register blocks */
355 #define UB960_IND_TARGET_PAT_GEN 0x00
356 #define UB960_IND_TARGET_RX_ANA(n) (0x01 + (n))
357 #define UB960_IND_TARGET_CSI_CSIPLL_REG_1 0x92 /* UB9702 */
358 #define UB960_IND_TARGET_CSI_ANA 0x07
360 /* UB960_IR_PGEN_*: Indirect Registers for Test Pattern Generator */
362 #define UB960_IR_PGEN_CTL 0x01
363 #define UB960_IR_PGEN_CTL_PGEN_ENABLE BIT(0)
365 #define UB960_IR_PGEN_CFG 0x02
366 #define UB960_IR_PGEN_CSI_DI 0x03
367 #define UB960_IR_PGEN_LINE_SIZE1 0x04
368 #define UB960_IR_PGEN_LINE_SIZE0 0x05
369 #define UB960_IR_PGEN_BAR_SIZE1 0x06
370 #define UB960_IR_PGEN_BAR_SIZE0 0x07
371 #define UB960_IR_PGEN_ACT_LPF1 0x08
372 #define UB960_IR_PGEN_ACT_LPF0 0x09
373 #define UB960_IR_PGEN_TOT_LPF1 0x0a
374 #define UB960_IR_PGEN_TOT_LPF0 0x0b
375 #define UB960_IR_PGEN_LINE_PD1 0x0c
376 #define UB960_IR_PGEN_LINE_PD0 0x0d
377 #define UB960_IR_PGEN_VBP 0x0e
378 #define UB960_IR_PGEN_VFP 0x0f
379 #define UB960_IR_PGEN_COLOR(n) (0x10 + (n)) /* n < 15 */
381 #define UB960_IR_RX_ANA_STROBE_SET_CLK 0x08
382 #define UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY BIT(3)
383 #define UB960_IR_RX_ANA_STROBE_SET_CLK_DELAY_MASK GENMASK(2, 0)
385 #define UB960_IR_RX_ANA_STROBE_SET_DATA 0x09
386 #define UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY BIT(3)
387 #define UB960_IR_RX_ANA_STROBE_SET_DATA_DELAY_MASK GENMASK(2, 0)
389 /* EQ related */
391 #define UB960_MIN_AEQ_STROBE_POS -7
392 #define UB960_MAX_AEQ_STROBE_POS 7
394 #define UB960_MANUAL_STROBE_EXTRA_DELAY 6
396 #define UB960_MIN_MANUAL_STROBE_POS -(7 + UB960_MANUAL_STROBE_EXTRA_DELAY)
397 #define UB960_MAX_MANUAL_STROBE_POS (7 + UB960_MANUAL_STROBE_EXTRA_DELAY)
398 #define UB960_NUM_MANUAL_STROBE_POS (UB960_MAX_MANUAL_STROBE_POS - UB960_MIN_MANUAL_STROBE_POS + 1)
400 #define UB960_MIN_EQ_LEVEL 0
401 #define UB960_MAX_EQ_LEVEL 14
402 #define UB960_NUM_EQ_LEVELS (UB960_MAX_EQ_LEVEL - UB960_MIN_EQ_LEVEL + 1)
404 struct ub960_hw_data {
405 const char *model;
406 u8 num_rxports;
407 u8 num_txports;
408 bool is_ub9702;
409 bool is_fpdlink4;
412 enum ub960_rxport_mode {
413 RXPORT_MODE_RAW10 = 0,
414 RXPORT_MODE_RAW12_HF = 1,
415 RXPORT_MODE_RAW12_LF = 2,
416 RXPORT_MODE_CSI2_SYNC = 3,
417 RXPORT_MODE_CSI2_NONSYNC = 4,
418 RXPORT_MODE_LAST = RXPORT_MODE_CSI2_NONSYNC,
421 enum ub960_rxport_cdr {
422 RXPORT_CDR_FPD3 = 0,
423 RXPORT_CDR_FPD4 = 1,
424 RXPORT_CDR_LAST = RXPORT_CDR_FPD4,
427 struct ub960_rxport {
428 struct ub960_data *priv;
429 u8 nport; /* RX port number, and index in priv->rxport[] */
431 struct {
432 struct v4l2_subdev *sd;
433 u16 pad;
434 struct fwnode_handle *ep_fwnode;
435 } source;
437 /* Serializer */
438 struct {
439 struct fwnode_handle *fwnode;
440 struct i2c_client *client;
441 unsigned short alias; /* I2C alias (lower 7 bits) */
442 struct ds90ub9xx_platform_data pdata;
443 } ser;
445 enum ub960_rxport_mode rx_mode;
446 enum ub960_rxport_cdr cdr_mode;
448 u8 lv_fv_pol; /* LV and FV polarities */
450 struct regulator *vpoc;
452 /* EQ settings */
453 struct {
454 bool manual_eq;
456 s8 strobe_pos;
458 union {
459 struct {
460 u8 eq_level_min;
461 u8 eq_level_max;
462 } aeq;
464 struct {
465 u8 eq_level;
466 } manual;
468 } eq;
470 const struct i2c_client *aliased_clients[UB960_MAX_PORT_ALIASES];
473 struct ub960_asd {
474 struct v4l2_async_connection base;
475 struct ub960_rxport *rxport;
478 static inline struct ub960_asd *to_ub960_asd(struct v4l2_async_connection *asd)
480 return container_of(asd, struct ub960_asd, base);
483 struct ub960_txport {
484 struct ub960_data *priv;
485 u8 nport; /* TX port number, and index in priv->txport[] */
487 u32 num_data_lanes;
488 bool non_continous_clk;
491 struct ub960_data {
492 const struct ub960_hw_data *hw_data;
493 struct i2c_client *client; /* for shared local registers */
494 struct regmap *regmap;
496 /* lock for register access */
497 struct mutex reg_lock;
499 struct clk *refclk;
501 struct regulator *vddio;
503 struct gpio_desc *pd_gpio;
504 struct delayed_work poll_work;
505 struct ub960_rxport *rxports[UB960_MAX_RX_NPORTS];
506 struct ub960_txport *txports[UB960_MAX_TX_NPORTS];
508 struct v4l2_subdev sd;
509 struct media_pad pads[UB960_MAX_NPORTS];
511 struct v4l2_ctrl_handler ctrl_handler;
512 struct v4l2_async_notifier notifier;
514 u32 tx_data_rate; /* Nominal data rate (Gb/s) */
515 s64 tx_link_freq[1];
517 struct i2c_atr *atr;
519 struct {
520 u8 rxport;
521 u8 txport;
522 u8 indirect_target;
523 } reg_current;
525 bool streaming;
527 u8 stored_fwd_ctl;
529 u64 stream_enable_mask[UB960_MAX_NPORTS];
531 /* These are common to all ports */
532 struct {
533 bool manual;
535 s8 min;
536 s8 max;
537 } strobe;
540 static inline struct ub960_data *sd_to_ub960(struct v4l2_subdev *sd)
542 return container_of(sd, struct ub960_data, sd);
545 static inline bool ub960_pad_is_sink(struct ub960_data *priv, u32 pad)
547 return pad < priv->hw_data->num_rxports;
550 static inline bool ub960_pad_is_source(struct ub960_data *priv, u32 pad)
552 return pad >= priv->hw_data->num_rxports;
555 static inline unsigned int ub960_pad_to_port(struct ub960_data *priv, u32 pad)
557 if (ub960_pad_is_sink(priv, pad))
558 return pad;
559 else
560 return pad - priv->hw_data->num_rxports;
563 struct ub960_format_info {
564 u32 code;
565 u32 bpp;
566 u8 datatype;
567 bool meta;
570 static const struct ub960_format_info ub960_formats[] = {
571 { .code = MEDIA_BUS_FMT_YUYV8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
572 { .code = MEDIA_BUS_FMT_UYVY8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
573 { .code = MEDIA_BUS_FMT_VYUY8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
574 { .code = MEDIA_BUS_FMT_YVYU8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
576 { .code = MEDIA_BUS_FMT_SBGGR12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
577 { .code = MEDIA_BUS_FMT_SGBRG12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
578 { .code = MEDIA_BUS_FMT_SGRBG12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
579 { .code = MEDIA_BUS_FMT_SRGGB12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
582 static const struct ub960_format_info *ub960_find_format(u32 code)
584 unsigned int i;
586 for (i = 0; i < ARRAY_SIZE(ub960_formats); i++) {
587 if (ub960_formats[i].code == code)
588 return &ub960_formats[i];
591 return NULL;
594 /* -----------------------------------------------------------------------------
595 * Basic device access
598 static int ub960_read(struct ub960_data *priv, u8 reg, u8 *val)
600 struct device *dev = &priv->client->dev;
601 unsigned int v;
602 int ret;
604 mutex_lock(&priv->reg_lock);
606 ret = regmap_read(priv->regmap, reg, &v);
607 if (ret) {
608 dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
609 __func__, reg, ret);
610 goto out_unlock;
613 *val = v;
615 out_unlock:
616 mutex_unlock(&priv->reg_lock);
618 return ret;
621 static int ub960_write(struct ub960_data *priv, u8 reg, u8 val)
623 struct device *dev = &priv->client->dev;
624 int ret;
626 mutex_lock(&priv->reg_lock);
628 ret = regmap_write(priv->regmap, reg, val);
629 if (ret)
630 dev_err(dev, "%s: cannot write register 0x%02x (%d)!\n",
631 __func__, reg, ret);
633 mutex_unlock(&priv->reg_lock);
635 return ret;
638 static int ub960_update_bits(struct ub960_data *priv, u8 reg, u8 mask, u8 val)
640 struct device *dev = &priv->client->dev;
641 int ret;
643 mutex_lock(&priv->reg_lock);
645 ret = regmap_update_bits(priv->regmap, reg, mask, val);
646 if (ret)
647 dev_err(dev, "%s: cannot update register 0x%02x (%d)!\n",
648 __func__, reg, ret);
650 mutex_unlock(&priv->reg_lock);
652 return ret;
655 static int ub960_read16(struct ub960_data *priv, u8 reg, u16 *val)
657 struct device *dev = &priv->client->dev;
658 __be16 __v;
659 int ret;
661 mutex_lock(&priv->reg_lock);
663 ret = regmap_bulk_read(priv->regmap, reg, &__v, sizeof(__v));
664 if (ret) {
665 dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
666 __func__, reg, ret);
667 goto out_unlock;
670 *val = be16_to_cpu(__v);
672 out_unlock:
673 mutex_unlock(&priv->reg_lock);
675 return ret;
678 static int ub960_rxport_select(struct ub960_data *priv, u8 nport)
680 struct device *dev = &priv->client->dev;
681 int ret;
683 lockdep_assert_held(&priv->reg_lock);
685 if (priv->reg_current.rxport == nport)
686 return 0;
688 ret = regmap_write(priv->regmap, UB960_SR_FPD3_PORT_SEL,
689 (nport << 4) | BIT(nport));
690 if (ret) {
691 dev_err(dev, "%s: cannot select rxport %d (%d)!\n", __func__,
692 nport, ret);
693 return ret;
696 priv->reg_current.rxport = nport;
698 return 0;
701 static int ub960_rxport_read(struct ub960_data *priv, u8 nport, u8 reg, u8 *val)
703 struct device *dev = &priv->client->dev;
704 unsigned int v;
705 int ret;
707 mutex_lock(&priv->reg_lock);
709 ret = ub960_rxport_select(priv, nport);
710 if (ret)
711 goto out_unlock;
713 ret = regmap_read(priv->regmap, reg, &v);
714 if (ret) {
715 dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
716 __func__, reg, ret);
717 goto out_unlock;
720 *val = v;
722 out_unlock:
723 mutex_unlock(&priv->reg_lock);
725 return ret;
728 static int ub960_rxport_write(struct ub960_data *priv, u8 nport, u8 reg, u8 val)
730 struct device *dev = &priv->client->dev;
731 int ret;
733 mutex_lock(&priv->reg_lock);
735 ret = ub960_rxport_select(priv, nport);
736 if (ret)
737 goto out_unlock;
739 ret = regmap_write(priv->regmap, reg, val);
740 if (ret)
741 dev_err(dev, "%s: cannot write register 0x%02x (%d)!\n",
742 __func__, reg, ret);
744 out_unlock:
745 mutex_unlock(&priv->reg_lock);
747 return ret;
750 static int ub960_rxport_update_bits(struct ub960_data *priv, u8 nport, u8 reg,
751 u8 mask, u8 val)
753 struct device *dev = &priv->client->dev;
754 int ret;
756 mutex_lock(&priv->reg_lock);
758 ret = ub960_rxport_select(priv, nport);
759 if (ret)
760 goto out_unlock;
762 ret = regmap_update_bits(priv->regmap, reg, mask, val);
763 if (ret)
764 dev_err(dev, "%s: cannot update register 0x%02x (%d)!\n",
765 __func__, reg, ret);
767 out_unlock:
768 mutex_unlock(&priv->reg_lock);
770 return ret;
773 static int ub960_rxport_read16(struct ub960_data *priv, u8 nport, u8 reg,
774 u16 *val)
776 struct device *dev = &priv->client->dev;
777 __be16 __v;
778 int ret;
780 mutex_lock(&priv->reg_lock);
782 ret = ub960_rxport_select(priv, nport);
783 if (ret)
784 goto out_unlock;
786 ret = regmap_bulk_read(priv->regmap, reg, &__v, sizeof(__v));
787 if (ret) {
788 dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
789 __func__, reg, ret);
790 goto out_unlock;
793 *val = be16_to_cpu(__v);
795 out_unlock:
796 mutex_unlock(&priv->reg_lock);
798 return ret;
801 static int ub960_txport_select(struct ub960_data *priv, u8 nport)
803 struct device *dev = &priv->client->dev;
804 int ret;
806 lockdep_assert_held(&priv->reg_lock);
808 if (priv->reg_current.txport == nport)
809 return 0;
811 ret = regmap_write(priv->regmap, UB960_SR_CSI_PORT_SEL,
812 (nport << 4) | BIT(nport));
813 if (ret) {
814 dev_err(dev, "%s: cannot select tx port %d (%d)!\n", __func__,
815 nport, ret);
816 return ret;
819 priv->reg_current.txport = nport;
821 return 0;
824 static int ub960_txport_read(struct ub960_data *priv, u8 nport, u8 reg, u8 *val)
826 struct device *dev = &priv->client->dev;
827 unsigned int v;
828 int ret;
830 mutex_lock(&priv->reg_lock);
832 ret = ub960_txport_select(priv, nport);
833 if (ret)
834 goto out_unlock;
836 ret = regmap_read(priv->regmap, reg, &v);
837 if (ret) {
838 dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
839 __func__, reg, ret);
840 goto out_unlock;
843 *val = v;
845 out_unlock:
846 mutex_unlock(&priv->reg_lock);
848 return ret;
851 static int ub960_txport_write(struct ub960_data *priv, u8 nport, u8 reg, u8 val)
853 struct device *dev = &priv->client->dev;
854 int ret;
856 mutex_lock(&priv->reg_lock);
858 ret = ub960_txport_select(priv, nport);
859 if (ret)
860 goto out_unlock;
862 ret = regmap_write(priv->regmap, reg, val);
863 if (ret)
864 dev_err(dev, "%s: cannot write register 0x%02x (%d)!\n",
865 __func__, reg, ret);
867 out_unlock:
868 mutex_unlock(&priv->reg_lock);
870 return ret;
873 static int ub960_txport_update_bits(struct ub960_data *priv, u8 nport, u8 reg,
874 u8 mask, u8 val)
876 struct device *dev = &priv->client->dev;
877 int ret;
879 mutex_lock(&priv->reg_lock);
881 ret = ub960_txport_select(priv, nport);
882 if (ret)
883 goto out_unlock;
885 ret = regmap_update_bits(priv->regmap, reg, mask, val);
886 if (ret)
887 dev_err(dev, "%s: cannot update register 0x%02x (%d)!\n",
888 __func__, reg, ret);
890 out_unlock:
891 mutex_unlock(&priv->reg_lock);
893 return ret;
896 static int ub960_select_ind_reg_block(struct ub960_data *priv, u8 block)
898 struct device *dev = &priv->client->dev;
899 int ret;
901 lockdep_assert_held(&priv->reg_lock);
903 if (priv->reg_current.indirect_target == block)
904 return 0;
906 ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_CTL, block << 2);
907 if (ret) {
908 dev_err(dev, "%s: cannot select indirect target %u (%d)!\n",
909 __func__, block, ret);
910 return ret;
913 priv->reg_current.indirect_target = block;
915 return 0;
918 static int ub960_read_ind(struct ub960_data *priv, u8 block, u8 reg, u8 *val)
920 struct device *dev = &priv->client->dev;
921 unsigned int v;
922 int ret;
924 mutex_lock(&priv->reg_lock);
926 ret = ub960_select_ind_reg_block(priv, block);
927 if (ret)
928 goto out_unlock;
930 ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_ADDR, reg);
931 if (ret) {
932 dev_err(dev,
933 "Write to IND_ACC_ADDR failed when reading %u:%x02x: %d\n",
934 block, reg, ret);
935 goto out_unlock;
938 ret = regmap_read(priv->regmap, UB960_SR_IND_ACC_DATA, &v);
939 if (ret) {
940 dev_err(dev,
941 "Write to IND_ACC_DATA failed when reading %u:%x02x: %d\n",
942 block, reg, ret);
943 goto out_unlock;
946 *val = v;
948 out_unlock:
949 mutex_unlock(&priv->reg_lock);
951 return ret;
954 static int ub960_write_ind(struct ub960_data *priv, u8 block, u8 reg, u8 val)
956 struct device *dev = &priv->client->dev;
957 int ret;
959 mutex_lock(&priv->reg_lock);
961 ret = ub960_select_ind_reg_block(priv, block);
962 if (ret)
963 goto out_unlock;
965 ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_ADDR, reg);
966 if (ret) {
967 dev_err(dev,
968 "Write to IND_ACC_ADDR failed when writing %u:%x02x: %d\n",
969 block, reg, ret);
970 goto out_unlock;
973 ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_DATA, val);
974 if (ret) {
975 dev_err(dev,
976 "Write to IND_ACC_DATA failed when writing %u:%x02x: %d\n",
977 block, reg, ret);
978 goto out_unlock;
981 out_unlock:
982 mutex_unlock(&priv->reg_lock);
984 return ret;
987 static int ub960_ind_update_bits(struct ub960_data *priv, u8 block, u8 reg,
988 u8 mask, u8 val)
990 struct device *dev = &priv->client->dev;
991 int ret;
993 mutex_lock(&priv->reg_lock);
995 ret = ub960_select_ind_reg_block(priv, block);
996 if (ret)
997 goto out_unlock;
999 ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_ADDR, reg);
1000 if (ret) {
1001 dev_err(dev,
1002 "Write to IND_ACC_ADDR failed when updating %u:%x02x: %d\n",
1003 block, reg, ret);
1004 goto out_unlock;
1007 ret = regmap_update_bits(priv->regmap, UB960_SR_IND_ACC_DATA, mask,
1008 val);
1009 if (ret) {
1010 dev_err(dev,
1011 "Write to IND_ACC_DATA failed when updating %u:%x02x: %d\n",
1012 block, reg, ret);
1013 goto out_unlock;
1016 out_unlock:
1017 mutex_unlock(&priv->reg_lock);
1019 return ret;
1022 /* -----------------------------------------------------------------------------
1023 * I2C-ATR (address translator)
1026 static int ub960_atr_attach_client(struct i2c_atr *atr, u32 chan_id,
1027 const struct i2c_client *client, u16 alias)
1029 struct ub960_data *priv = i2c_atr_get_driver_data(atr);
1030 struct ub960_rxport *rxport = priv->rxports[chan_id];
1031 struct device *dev = &priv->client->dev;
1032 unsigned int reg_idx;
1034 for (reg_idx = 0; reg_idx < ARRAY_SIZE(rxport->aliased_clients); reg_idx++) {
1035 if (!rxport->aliased_clients[reg_idx])
1036 break;
1039 if (reg_idx == ARRAY_SIZE(rxport->aliased_clients)) {
1040 dev_err(dev, "rx%u: alias pool exhausted\n", rxport->nport);
1041 return -EADDRNOTAVAIL;
1044 rxport->aliased_clients[reg_idx] = client;
1046 ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ID(reg_idx),
1047 client->addr << 1);
1048 ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx),
1049 alias << 1);
1051 dev_dbg(dev, "rx%u: client 0x%02x assigned alias 0x%02x at slot %u\n",
1052 rxport->nport, client->addr, alias, reg_idx);
1054 return 0;
1057 static void ub960_atr_detach_client(struct i2c_atr *atr, u32 chan_id,
1058 const struct i2c_client *client)
1060 struct ub960_data *priv = i2c_atr_get_driver_data(atr);
1061 struct ub960_rxport *rxport = priv->rxports[chan_id];
1062 struct device *dev = &priv->client->dev;
1063 unsigned int reg_idx;
1065 for (reg_idx = 0; reg_idx < ARRAY_SIZE(rxport->aliased_clients); reg_idx++) {
1066 if (rxport->aliased_clients[reg_idx] == client)
1067 break;
1070 if (reg_idx == ARRAY_SIZE(rxport->aliased_clients)) {
1071 dev_err(dev, "rx%u: client 0x%02x is not mapped!\n",
1072 rxport->nport, client->addr);
1073 return;
1076 rxport->aliased_clients[reg_idx] = NULL;
1078 ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx), 0);
1080 dev_dbg(dev, "rx%u: client 0x%02x released at slot %u\n", rxport->nport,
1081 client->addr, reg_idx);
1084 static const struct i2c_atr_ops ub960_atr_ops = {
1085 .attach_client = ub960_atr_attach_client,
1086 .detach_client = ub960_atr_detach_client,
1089 static int ub960_init_atr(struct ub960_data *priv)
1091 struct device *dev = &priv->client->dev;
1092 struct i2c_adapter *parent_adap = priv->client->adapter;
1094 priv->atr = i2c_atr_new(parent_adap, dev, &ub960_atr_ops,
1095 priv->hw_data->num_rxports);
1096 if (IS_ERR(priv->atr))
1097 return PTR_ERR(priv->atr);
1099 i2c_atr_set_driver_data(priv->atr, priv);
1101 return 0;
1104 static void ub960_uninit_atr(struct ub960_data *priv)
1106 i2c_atr_delete(priv->atr);
1107 priv->atr = NULL;
1110 /* -----------------------------------------------------------------------------
1111 * TX ports
1114 static int ub960_parse_dt_txport(struct ub960_data *priv,
1115 struct fwnode_handle *ep_fwnode,
1116 u8 nport)
1118 struct device *dev = &priv->client->dev;
1119 struct v4l2_fwnode_endpoint vep = {};
1120 struct ub960_txport *txport;
1121 int ret;
1123 txport = kzalloc(sizeof(*txport), GFP_KERNEL);
1124 if (!txport)
1125 return -ENOMEM;
1127 txport->priv = priv;
1128 txport->nport = nport;
1130 vep.bus_type = V4L2_MBUS_CSI2_DPHY;
1131 ret = v4l2_fwnode_endpoint_alloc_parse(ep_fwnode, &vep);
1132 if (ret) {
1133 dev_err(dev, "tx%u: failed to parse endpoint data\n", nport);
1134 goto err_free_txport;
1137 txport->non_continous_clk = vep.bus.mipi_csi2.flags &
1138 V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1140 txport->num_data_lanes = vep.bus.mipi_csi2.num_data_lanes;
1142 if (vep.nr_of_link_frequencies != 1) {
1143 ret = -EINVAL;
1144 goto err_free_vep;
1147 priv->tx_link_freq[0] = vep.link_frequencies[0];
1148 priv->tx_data_rate = priv->tx_link_freq[0] * 2;
1150 if (priv->tx_data_rate != MHZ(1600) &&
1151 priv->tx_data_rate != MHZ(1200) &&
1152 priv->tx_data_rate != MHZ(800) &&
1153 priv->tx_data_rate != MHZ(400)) {
1154 dev_err(dev, "tx%u: invalid 'link-frequencies' value\n", nport);
1155 ret = -EINVAL;
1156 goto err_free_vep;
1159 v4l2_fwnode_endpoint_free(&vep);
1161 priv->txports[nport] = txport;
1163 return 0;
1165 err_free_vep:
1166 v4l2_fwnode_endpoint_free(&vep);
1167 err_free_txport:
1168 kfree(txport);
1170 return ret;
1173 static void ub960_csi_handle_events(struct ub960_data *priv, u8 nport)
1175 struct device *dev = &priv->client->dev;
1176 u8 csi_tx_isr;
1177 int ret;
1179 ret = ub960_txport_read(priv, nport, UB960_TR_CSI_TX_ISR, &csi_tx_isr);
1180 if (ret)
1181 return;
1183 if (csi_tx_isr & UB960_TR_CSI_TX_ISR_IS_CSI_SYNC_ERROR)
1184 dev_warn(dev, "TX%u: CSI_SYNC_ERROR\n", nport);
1186 if (csi_tx_isr & UB960_TR_CSI_TX_ISR_IS_CSI_PASS_ERROR)
1187 dev_warn(dev, "TX%u: CSI_PASS_ERROR\n", nport);
1190 /* -----------------------------------------------------------------------------
1191 * RX ports
1194 static int ub960_rxport_enable_vpocs(struct ub960_data *priv)
1196 unsigned int nport;
1197 int ret;
1199 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
1200 struct ub960_rxport *rxport = priv->rxports[nport];
1202 if (!rxport || !rxport->vpoc)
1203 continue;
1205 ret = regulator_enable(rxport->vpoc);
1206 if (ret)
1207 goto err_disable_vpocs;
1210 return 0;
1212 err_disable_vpocs:
1213 while (nport--) {
1214 struct ub960_rxport *rxport = priv->rxports[nport];
1216 if (!rxport || !rxport->vpoc)
1217 continue;
1219 regulator_disable(rxport->vpoc);
1222 return ret;
1225 static void ub960_rxport_disable_vpocs(struct ub960_data *priv)
1227 unsigned int nport;
1229 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
1230 struct ub960_rxport *rxport = priv->rxports[nport];
1232 if (!rxport || !rxport->vpoc)
1233 continue;
1235 regulator_disable(rxport->vpoc);
1239 static void ub960_rxport_clear_errors(struct ub960_data *priv,
1240 unsigned int nport)
1242 u8 v;
1244 ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1, &v);
1245 ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2, &v);
1246 ub960_rxport_read(priv, nport, UB960_RR_CSI_RX_STS, &v);
1247 ub960_rxport_read(priv, nport, UB960_RR_BCC_STATUS, &v);
1249 ub960_rxport_read(priv, nport, UB960_RR_RX_PAR_ERR_HI, &v);
1250 ub960_rxport_read(priv, nport, UB960_RR_RX_PAR_ERR_LO, &v);
1252 ub960_rxport_read(priv, nport, UB960_RR_CSI_ERR_COUNTER, &v);
1255 static void ub960_clear_rx_errors(struct ub960_data *priv)
1257 unsigned int nport;
1259 for (nport = 0; nport < priv->hw_data->num_rxports; nport++)
1260 ub960_rxport_clear_errors(priv, nport);
1263 static int ub960_rxport_get_strobe_pos(struct ub960_data *priv,
1264 unsigned int nport, s8 *strobe_pos)
1266 u8 v;
1267 u8 clk_delay, data_delay;
1268 int ret;
1270 ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
1271 UB960_IR_RX_ANA_STROBE_SET_CLK, &v);
1273 clk_delay = (v & UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY) ?
1274 0 : UB960_MANUAL_STROBE_EXTRA_DELAY;
1276 ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
1277 UB960_IR_RX_ANA_STROBE_SET_DATA, &v);
1279 data_delay = (v & UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY) ?
1280 0 : UB960_MANUAL_STROBE_EXTRA_DELAY;
1282 ret = ub960_rxport_read(priv, nport, UB960_RR_SFILTER_STS_0, &v);
1283 if (ret)
1284 return ret;
1286 clk_delay += v & UB960_IR_RX_ANA_STROBE_SET_CLK_DELAY_MASK;
1288 ret = ub960_rxport_read(priv, nport, UB960_RR_SFILTER_STS_1, &v);
1289 if (ret)
1290 return ret;
1292 data_delay += v & UB960_IR_RX_ANA_STROBE_SET_DATA_DELAY_MASK;
1294 *strobe_pos = data_delay - clk_delay;
1296 return 0;
1299 static void ub960_rxport_set_strobe_pos(struct ub960_data *priv,
1300 unsigned int nport, s8 strobe_pos)
1302 u8 clk_delay, data_delay;
1304 clk_delay = UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY;
1305 data_delay = UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY;
1307 if (strobe_pos < UB960_MIN_AEQ_STROBE_POS)
1308 clk_delay = abs(strobe_pos) - UB960_MANUAL_STROBE_EXTRA_DELAY;
1309 else if (strobe_pos > UB960_MAX_AEQ_STROBE_POS)
1310 data_delay = strobe_pos - UB960_MANUAL_STROBE_EXTRA_DELAY;
1311 else if (strobe_pos < 0)
1312 clk_delay = abs(strobe_pos) | UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY;
1313 else if (strobe_pos > 0)
1314 data_delay = strobe_pos | UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY;
1316 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
1317 UB960_IR_RX_ANA_STROBE_SET_CLK, clk_delay);
1319 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
1320 UB960_IR_RX_ANA_STROBE_SET_DATA, data_delay);
1323 static void ub960_rxport_set_strobe_range(struct ub960_data *priv,
1324 s8 strobe_min, s8 strobe_max)
1326 /* Convert the signed strobe pos to positive zero based value */
1327 strobe_min -= UB960_MIN_AEQ_STROBE_POS;
1328 strobe_max -= UB960_MIN_AEQ_STROBE_POS;
1330 ub960_write(priv, UB960_XR_SFILTER_CFG,
1331 ((u8)strobe_min << UB960_XR_SFILTER_CFG_SFILTER_MIN_SHIFT) |
1332 ((u8)strobe_max << UB960_XR_SFILTER_CFG_SFILTER_MAX_SHIFT));
1335 static int ub960_rxport_get_eq_level(struct ub960_data *priv,
1336 unsigned int nport, u8 *eq_level)
1338 int ret;
1339 u8 v;
1341 ret = ub960_rxport_read(priv, nport, UB960_RR_AEQ_STATUS, &v);
1342 if (ret)
1343 return ret;
1345 *eq_level = (v & UB960_RR_AEQ_STATUS_STATUS_1) +
1346 (v & UB960_RR_AEQ_STATUS_STATUS_2);
1348 return 0;
1351 static void ub960_rxport_set_eq_level(struct ub960_data *priv,
1352 unsigned int nport, u8 eq_level)
1354 u8 eq_stage_1_select_value, eq_stage_2_select_value;
1355 const unsigned int eq_stage_max = 7;
1356 u8 v;
1358 if (eq_level <= eq_stage_max) {
1359 eq_stage_1_select_value = eq_level;
1360 eq_stage_2_select_value = 0;
1361 } else {
1362 eq_stage_1_select_value = eq_stage_max;
1363 eq_stage_2_select_value = eq_level - eq_stage_max;
1366 ub960_rxport_read(priv, nport, UB960_RR_AEQ_BYPASS, &v);
1368 v &= ~(UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_MASK |
1369 UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_MASK);
1370 v |= eq_stage_1_select_value << UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_SHIFT;
1371 v |= eq_stage_2_select_value << UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_SHIFT;
1372 v |= UB960_RR_AEQ_BYPASS_ENABLE;
1374 ub960_rxport_write(priv, nport, UB960_RR_AEQ_BYPASS, v);
1377 static void ub960_rxport_set_eq_range(struct ub960_data *priv,
1378 unsigned int nport, u8 eq_min, u8 eq_max)
1380 ub960_rxport_write(priv, nport, UB960_RR_AEQ_MIN_MAX,
1381 (eq_min << UB960_RR_AEQ_MIN_MAX_AEQ_FLOOR_SHIFT) |
1382 (eq_max << UB960_RR_AEQ_MIN_MAX_AEQ_MAX_SHIFT));
1384 /* Enable AEQ min setting */
1385 ub960_rxport_update_bits(priv, nport, UB960_RR_AEQ_CTL2,
1386 UB960_RR_AEQ_CTL2_SET_AEQ_FLOOR,
1387 UB960_RR_AEQ_CTL2_SET_AEQ_FLOOR);
1390 static void ub960_rxport_config_eq(struct ub960_data *priv, unsigned int nport)
1392 struct ub960_rxport *rxport = priv->rxports[nport];
1394 /* We also set common settings here. Should be moved elsewhere. */
1396 if (priv->strobe.manual) {
1397 /* Disable AEQ_SFILTER_EN */
1398 ub960_update_bits(priv, UB960_XR_AEQ_CTL1,
1399 UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN, 0);
1400 } else {
1401 /* Enable SFILTER and error control */
1402 ub960_write(priv, UB960_XR_AEQ_CTL1,
1403 UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_MASK |
1404 UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN);
1406 /* Set AEQ strobe range */
1407 ub960_rxport_set_strobe_range(priv, priv->strobe.min,
1408 priv->strobe.max);
1411 /* The rest are port specific */
1413 if (priv->strobe.manual)
1414 ub960_rxport_set_strobe_pos(priv, nport, rxport->eq.strobe_pos);
1415 else
1416 ub960_rxport_set_strobe_pos(priv, nport, 0);
1418 if (rxport->eq.manual_eq) {
1419 ub960_rxport_set_eq_level(priv, nport,
1420 rxport->eq.manual.eq_level);
1422 /* Enable AEQ Bypass */
1423 ub960_rxport_update_bits(priv, nport, UB960_RR_AEQ_BYPASS,
1424 UB960_RR_AEQ_BYPASS_ENABLE,
1425 UB960_RR_AEQ_BYPASS_ENABLE);
1426 } else {
1427 ub960_rxport_set_eq_range(priv, nport,
1428 rxport->eq.aeq.eq_level_min,
1429 rxport->eq.aeq.eq_level_max);
1431 /* Disable AEQ Bypass */
1432 ub960_rxport_update_bits(priv, nport, UB960_RR_AEQ_BYPASS,
1433 UB960_RR_AEQ_BYPASS_ENABLE, 0);
1437 static int ub960_rxport_link_ok(struct ub960_data *priv, unsigned int nport,
1438 bool *ok)
1440 u8 rx_port_sts1, rx_port_sts2;
1441 u16 parity_errors;
1442 u8 csi_rx_sts;
1443 u8 csi_err_cnt;
1444 u8 bcc_sts;
1445 int ret;
1446 bool errors;
1448 ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1,
1449 &rx_port_sts1);
1450 if (ret)
1451 return ret;
1453 if (!(rx_port_sts1 & UB960_RR_RX_PORT_STS1_LOCK_STS)) {
1454 *ok = false;
1455 return 0;
1458 ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2,
1459 &rx_port_sts2);
1460 if (ret)
1461 return ret;
1463 ret = ub960_rxport_read(priv, nport, UB960_RR_CSI_RX_STS, &csi_rx_sts);
1464 if (ret)
1465 return ret;
1467 ret = ub960_rxport_read(priv, nport, UB960_RR_CSI_ERR_COUNTER,
1468 &csi_err_cnt);
1469 if (ret)
1470 return ret;
1472 ret = ub960_rxport_read(priv, nport, UB960_RR_BCC_STATUS, &bcc_sts);
1473 if (ret)
1474 return ret;
1476 ret = ub960_rxport_read16(priv, nport, UB960_RR_RX_PAR_ERR_HI,
1477 &parity_errors);
1478 if (ret)
1479 return ret;
1481 errors = (rx_port_sts1 & UB960_RR_RX_PORT_STS1_ERROR_MASK) ||
1482 (rx_port_sts2 & UB960_RR_RX_PORT_STS2_ERROR_MASK) ||
1483 (bcc_sts & UB960_RR_BCC_STATUS_ERROR_MASK) ||
1484 (csi_rx_sts & UB960_RR_CSI_RX_STS_ERROR_MASK) || csi_err_cnt ||
1485 parity_errors;
1487 *ok = !errors;
1489 return 0;
1493 * Wait for the RX ports to lock, have no errors and have stable strobe position
1494 * and EQ level.
1496 static int ub960_rxport_wait_locks(struct ub960_data *priv,
1497 unsigned long port_mask,
1498 unsigned int *lock_mask)
1500 struct device *dev = &priv->client->dev;
1501 unsigned long timeout;
1502 unsigned int link_ok_mask;
1503 unsigned int missing;
1504 unsigned int loops;
1505 u8 nport;
1506 int ret;
1508 if (port_mask == 0) {
1509 if (lock_mask)
1510 *lock_mask = 0;
1511 return 0;
1514 if (port_mask >= BIT(priv->hw_data->num_rxports))
1515 return -EINVAL;
1517 timeout = jiffies + msecs_to_jiffies(1000);
1518 loops = 0;
1519 link_ok_mask = 0;
1521 while (time_before(jiffies, timeout)) {
1522 missing = 0;
1524 for_each_set_bit(nport, &port_mask,
1525 priv->hw_data->num_rxports) {
1526 struct ub960_rxport *rxport = priv->rxports[nport];
1527 bool ok;
1529 if (!rxport)
1530 continue;
1532 ret = ub960_rxport_link_ok(priv, nport, &ok);
1533 if (ret)
1534 return ret;
1537 * We want the link to be ok for two consecutive loops,
1538 * as a link could get established just before our test
1539 * and drop soon after.
1541 if (!ok || !(link_ok_mask & BIT(nport)))
1542 missing++;
1544 if (ok)
1545 link_ok_mask |= BIT(nport);
1546 else
1547 link_ok_mask &= ~BIT(nport);
1550 loops++;
1552 if (missing == 0)
1553 break;
1555 msleep(50);
1558 if (lock_mask)
1559 *lock_mask = link_ok_mask;
1561 dev_dbg(dev, "Wait locks done in %u loops\n", loops);
1562 for_each_set_bit(nport, &port_mask, priv->hw_data->num_rxports) {
1563 struct ub960_rxport *rxport = priv->rxports[nport];
1564 s8 strobe_pos, eq_level;
1565 u16 v;
1567 if (!rxport)
1568 continue;
1570 if (!(link_ok_mask & BIT(nport))) {
1571 dev_dbg(dev, "\trx%u: not locked\n", nport);
1572 continue;
1575 ub960_rxport_read16(priv, nport, UB960_RR_RX_FREQ_HIGH, &v);
1577 ret = ub960_rxport_get_strobe_pos(priv, nport, &strobe_pos);
1578 if (ret)
1579 return ret;
1581 ret = ub960_rxport_get_eq_level(priv, nport, &eq_level);
1582 if (ret)
1583 return ret;
1585 dev_dbg(dev, "\trx%u: locked, SP: %d, EQ: %u, freq %llu Hz\n",
1586 nport, strobe_pos, eq_level, (v * 1000000ULL) >> 8);
1589 return 0;
1592 static unsigned long ub960_calc_bc_clk_rate_ub960(struct ub960_data *priv,
1593 struct ub960_rxport *rxport)
1595 unsigned int mult;
1596 unsigned int div;
1598 switch (rxport->rx_mode) {
1599 case RXPORT_MODE_RAW10:
1600 case RXPORT_MODE_RAW12_HF:
1601 case RXPORT_MODE_RAW12_LF:
1602 mult = 1;
1603 div = 10;
1604 break;
1606 case RXPORT_MODE_CSI2_SYNC:
1607 mult = 2;
1608 div = 1;
1609 break;
1611 case RXPORT_MODE_CSI2_NONSYNC:
1612 mult = 2;
1613 div = 5;
1614 break;
1616 default:
1617 return 0;
1620 return clk_get_rate(priv->refclk) * mult / div;
1623 static unsigned long ub960_calc_bc_clk_rate_ub9702(struct ub960_data *priv,
1624 struct ub960_rxport *rxport)
1626 switch (rxport->rx_mode) {
1627 case RXPORT_MODE_RAW10:
1628 case RXPORT_MODE_RAW12_HF:
1629 case RXPORT_MODE_RAW12_LF:
1630 return 2359400;
1632 case RXPORT_MODE_CSI2_SYNC:
1633 return 47187500;
1635 case RXPORT_MODE_CSI2_NONSYNC:
1636 return 9437500;
1638 default:
1639 return 0;
1643 static int ub960_rxport_add_serializer(struct ub960_data *priv, u8 nport)
1645 struct ub960_rxport *rxport = priv->rxports[nport];
1646 struct device *dev = &priv->client->dev;
1647 struct ds90ub9xx_platform_data *ser_pdata = &rxport->ser.pdata;
1648 struct i2c_board_info ser_info = {
1649 .of_node = to_of_node(rxport->ser.fwnode),
1650 .fwnode = rxport->ser.fwnode,
1651 .platform_data = ser_pdata,
1654 ser_pdata->port = nport;
1655 ser_pdata->atr = priv->atr;
1656 if (priv->hw_data->is_ub9702)
1657 ser_pdata->bc_rate = ub960_calc_bc_clk_rate_ub9702(priv, rxport);
1658 else
1659 ser_pdata->bc_rate = ub960_calc_bc_clk_rate_ub960(priv, rxport);
1662 * The serializer is added under the same i2c adapter as the
1663 * deserializer. This is not quite right, as the serializer is behind
1664 * the FPD-Link.
1666 ser_info.addr = rxport->ser.alias;
1667 rxport->ser.client =
1668 i2c_new_client_device(priv->client->adapter, &ser_info);
1669 if (IS_ERR(rxport->ser.client)) {
1670 dev_err(dev, "rx%u: cannot add %s i2c device", nport,
1671 ser_info.type);
1672 return PTR_ERR(rxport->ser.client);
1675 dev_dbg(dev, "rx%u: remote serializer at alias 0x%02x (%u-%04x)\n",
1676 nport, rxport->ser.client->addr,
1677 rxport->ser.client->adapter->nr, rxport->ser.client->addr);
1679 return 0;
1682 static void ub960_rxport_remove_serializer(struct ub960_data *priv, u8 nport)
1684 struct ub960_rxport *rxport = priv->rxports[nport];
1686 i2c_unregister_device(rxport->ser.client);
1687 rxport->ser.client = NULL;
1690 /* Add serializer i2c devices for all initialized ports */
1691 static int ub960_rxport_add_serializers(struct ub960_data *priv)
1693 unsigned int nport;
1694 int ret;
1696 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
1697 struct ub960_rxport *rxport = priv->rxports[nport];
1699 if (!rxport)
1700 continue;
1702 ret = ub960_rxport_add_serializer(priv, nport);
1703 if (ret)
1704 goto err_remove_sers;
1707 return 0;
1709 err_remove_sers:
1710 while (nport--) {
1711 struct ub960_rxport *rxport = priv->rxports[nport];
1713 if (!rxport)
1714 continue;
1716 ub960_rxport_remove_serializer(priv, nport);
1719 return ret;
1722 static void ub960_rxport_remove_serializers(struct ub960_data *priv)
1724 unsigned int nport;
1726 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
1727 struct ub960_rxport *rxport = priv->rxports[nport];
1729 if (!rxport)
1730 continue;
1732 ub960_rxport_remove_serializer(priv, nport);
1736 static void ub960_init_tx_port(struct ub960_data *priv,
1737 struct ub960_txport *txport)
1739 unsigned int nport = txport->nport;
1740 u8 csi_ctl = 0;
1743 * From the datasheet: "initial CSI Skew-Calibration
1744 * sequence [...] should be set when operating at 1.6 Gbps"
1746 if (priv->tx_data_rate == MHZ(1600))
1747 csi_ctl |= UB960_TR_CSI_CTL_CSI_CAL_EN;
1749 csi_ctl |= (4 - txport->num_data_lanes) << 4;
1751 if (!txport->non_continous_clk)
1752 csi_ctl |= UB960_TR_CSI_CTL_CSI_CONTS_CLOCK;
1754 ub960_txport_write(priv, nport, UB960_TR_CSI_CTL, csi_ctl);
1757 static int ub960_init_tx_ports(struct ub960_data *priv)
1759 unsigned int nport;
1760 u8 speed_select;
1761 u8 pll_div;
1763 /* TX ports */
1765 switch (priv->tx_data_rate) {
1766 case MHZ(1600):
1767 default:
1768 speed_select = 0;
1769 pll_div = 0x10;
1770 break;
1771 case MHZ(1200):
1772 speed_select = 1;
1773 pll_div = 0x18;
1774 break;
1775 case MHZ(800):
1776 speed_select = 2;
1777 pll_div = 0x10;
1778 break;
1779 case MHZ(400):
1780 speed_select = 3;
1781 pll_div = 0x10;
1782 break;
1785 ub960_write(priv, UB960_SR_CSI_PLL_CTL, speed_select);
1787 if (priv->hw_data->is_ub9702) {
1788 ub960_write(priv, UB960_SR_CSI_PLL_DIV, pll_div);
1790 switch (priv->tx_data_rate) {
1791 case MHZ(1600):
1792 default:
1793 ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x92, 0x80);
1794 ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x4b, 0x2a);
1795 break;
1796 case MHZ(800):
1797 ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x92, 0x90);
1798 ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x4f, 0x2a);
1799 ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x4b, 0x2a);
1800 break;
1801 case MHZ(400):
1802 ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x92, 0xa0);
1803 break;
1807 for (nport = 0; nport < priv->hw_data->num_txports; nport++) {
1808 struct ub960_txport *txport = priv->txports[nport];
1810 if (!txport)
1811 continue;
1813 ub960_init_tx_port(priv, txport);
1816 return 0;
1819 static void ub960_init_rx_port_ub960(struct ub960_data *priv,
1820 struct ub960_rxport *rxport)
1822 unsigned int nport = rxport->nport;
1823 u32 bc_freq_val;
1826 * Back channel frequency select.
1827 * Override FREQ_SELECT from the strap.
1828 * 0 - 2.5 Mbps (DS90UB913A-Q1 / DS90UB933-Q1)
1829 * 2 - 10 Mbps
1830 * 6 - 50 Mbps (DS90UB953-Q1)
1832 * Note that changing this setting will result in some errors on the back
1833 * channel for a short period of time.
1836 switch (rxport->rx_mode) {
1837 case RXPORT_MODE_RAW10:
1838 case RXPORT_MODE_RAW12_HF:
1839 case RXPORT_MODE_RAW12_LF:
1840 bc_freq_val = 0;
1841 break;
1843 case RXPORT_MODE_CSI2_NONSYNC:
1844 bc_freq_val = 2;
1845 break;
1847 case RXPORT_MODE_CSI2_SYNC:
1848 bc_freq_val = 6;
1849 break;
1851 default:
1852 return;
1855 ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
1856 UB960_RR_BCC_CONFIG_BC_FREQ_SEL_MASK,
1857 bc_freq_val);
1859 switch (rxport->rx_mode) {
1860 case RXPORT_MODE_RAW10:
1861 /* FPD3_MODE = RAW10 Mode (DS90UB913A-Q1 / DS90UB933-Q1 compatible) */
1862 ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG,
1863 UB960_RR_PORT_CONFIG_FPD3_MODE_MASK,
1864 0x3);
1867 * RAW10_8BIT_CTL = 0b10 : 8-bit processing using upper 8 bits
1869 ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2,
1870 UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_MASK,
1871 0x2 << UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_SHIFT);
1873 break;
1875 case RXPORT_MODE_RAW12_HF:
1876 case RXPORT_MODE_RAW12_LF:
1877 /* Not implemented */
1878 return;
1880 case RXPORT_MODE_CSI2_SYNC:
1881 case RXPORT_MODE_CSI2_NONSYNC:
1882 /* CSI-2 Mode (DS90UB953-Q1 compatible) */
1883 ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG, 0x3,
1884 0x0);
1886 break;
1889 /* LV_POLARITY & FV_POLARITY */
1890 ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2, 0x3,
1891 rxport->lv_fv_pol);
1893 /* Enable all interrupt sources from this port */
1894 ub960_rxport_write(priv, nport, UB960_RR_PORT_ICR_HI, 0x07);
1895 ub960_rxport_write(priv, nport, UB960_RR_PORT_ICR_LO, 0x7f);
1897 /* Enable I2C_PASS_THROUGH */
1898 ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
1899 UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH,
1900 UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH);
1902 /* Enable I2C communication to the serializer via the alias addr */
1903 ub960_rxport_write(priv, nport, UB960_RR_SER_ALIAS_ID,
1904 rxport->ser.alias << 1);
1906 /* Configure EQ related settings */
1907 ub960_rxport_config_eq(priv, nport);
1909 /* Enable RX port */
1910 ub960_update_bits(priv, UB960_SR_RX_PORT_CTL, BIT(nport), BIT(nport));
1913 static void ub960_init_rx_port_ub9702_fpd3(struct ub960_data *priv,
1914 struct ub960_rxport *rxport)
1916 unsigned int nport = rxport->nport;
1917 u8 bc_freq_val;
1918 u8 fpd_func_mode;
1920 switch (rxport->rx_mode) {
1921 case RXPORT_MODE_RAW10:
1922 bc_freq_val = 0;
1923 fpd_func_mode = 5;
1924 break;
1926 case RXPORT_MODE_RAW12_HF:
1927 bc_freq_val = 0;
1928 fpd_func_mode = 4;
1929 break;
1931 case RXPORT_MODE_RAW12_LF:
1932 bc_freq_val = 0;
1933 fpd_func_mode = 6;
1934 break;
1936 case RXPORT_MODE_CSI2_SYNC:
1937 bc_freq_val = 6;
1938 fpd_func_mode = 2;
1939 break;
1941 case RXPORT_MODE_CSI2_NONSYNC:
1942 bc_freq_val = 2;
1943 fpd_func_mode = 2;
1944 break;
1946 default:
1947 return;
1950 ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG, 0x7,
1951 bc_freq_val);
1952 ub960_rxport_write(priv, nport, UB960_RR_CHANNEL_MODE, fpd_func_mode);
1954 /* set serdes_eq_mode = 1 */
1955 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0xa8, 0x80);
1957 /* enable serdes driver */
1958 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x0d, 0x7f);
1960 /* set serdes_eq_offset=4 */
1961 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x2b, 0x04);
1963 /* init default serdes_eq_max in 0xa9 */
1964 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0xa9, 0x23);
1966 /* init serdes_eq_min in 0xaa */
1967 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0xaa, 0);
1969 /* serdes_driver_ctl2 control: DS90UB953-Q1/DS90UB933-Q1/DS90UB913A-Q1 */
1970 ub960_ind_update_bits(priv, UB960_IND_TARGET_RX_ANA(nport), 0x1b,
1971 BIT(3), BIT(3));
1973 /* RX port to half-rate */
1974 ub960_update_bits(priv, UB960_SR_FPD_RATE_CFG, 0x3 << (nport * 2),
1975 BIT(nport * 2));
1978 static void ub960_init_rx_port_ub9702_fpd4_aeq(struct ub960_data *priv,
1979 struct ub960_rxport *rxport)
1981 unsigned int nport = rxport->nport;
1982 bool first_time_power_up = true;
1984 if (first_time_power_up) {
1985 u8 v;
1987 /* AEQ init */
1988 ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x2c, &v);
1990 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x27, v);
1991 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x28, v + 1);
1993 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x2b, 0x00);
1996 /* enable serdes_eq_ctl2 */
1997 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x9e, 0x00);
1999 /* enable serdes_eq_ctl1 */
2000 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x90, 0x40);
2002 /* enable serdes_eq_en */
2003 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x2e, 0x40);
2005 /* disable serdes_eq_override */
2006 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0xf0, 0x00);
2008 /* disable serdes_gain_override */
2009 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x71, 0x00);
2012 static void ub960_init_rx_port_ub9702_fpd4(struct ub960_data *priv,
2013 struct ub960_rxport *rxport)
2015 unsigned int nport = rxport->nport;
2016 u8 bc_freq_val;
2018 switch (rxport->rx_mode) {
2019 case RXPORT_MODE_RAW10:
2020 bc_freq_val = 0;
2021 break;
2023 case RXPORT_MODE_RAW12_HF:
2024 bc_freq_val = 0;
2025 break;
2027 case RXPORT_MODE_RAW12_LF:
2028 bc_freq_val = 0;
2029 break;
2031 case RXPORT_MODE_CSI2_SYNC:
2032 bc_freq_val = 6;
2033 break;
2035 case RXPORT_MODE_CSI2_NONSYNC:
2036 bc_freq_val = 2;
2037 break;
2039 default:
2040 return;
2043 ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG, 0x7,
2044 bc_freq_val);
2046 /* FPD4 Sync Mode */
2047 ub960_rxport_write(priv, nport, UB960_RR_CHANNEL_MODE, 0);
2049 /* add serdes_eq_offset of 4 */
2050 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x2b, 0x04);
2052 /* FPD4 serdes_start_eq in 0x27: assign default */
2053 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x27, 0x0);
2054 /* FPD4 serdes_end_eq in 0x28: assign default */
2055 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x28, 0x23);
2057 /* set serdes_driver_mode into FPD IV mode */
2058 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x04, 0x00);
2059 /* set FPD PBC drv into FPD IV mode */
2060 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x1b, 0x00);
2062 /* set serdes_system_init to 0x2f */
2063 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x21, 0x2f);
2064 /* set serdes_system_rst in reset mode */
2065 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x25, 0xc1);
2067 /* RX port to 7.55G mode */
2068 ub960_update_bits(priv, UB960_SR_FPD_RATE_CFG, 0x3 << (nport * 2),
2069 0 << (nport * 2));
2071 ub960_init_rx_port_ub9702_fpd4_aeq(priv, rxport);
2074 static void ub960_init_rx_port_ub9702(struct ub960_data *priv,
2075 struct ub960_rxport *rxport)
2077 unsigned int nport = rxport->nport;
2079 if (rxport->cdr_mode == RXPORT_CDR_FPD3)
2080 ub960_init_rx_port_ub9702_fpd3(priv, rxport);
2081 else /* RXPORT_CDR_FPD4 */
2082 ub960_init_rx_port_ub9702_fpd4(priv, rxport);
2084 switch (rxport->rx_mode) {
2085 case RXPORT_MODE_RAW10:
2087 * RAW10_8BIT_CTL = 0b11 : 8-bit processing using lower 8 bits
2088 * 0b10 : 8-bit processing using upper 8 bits
2090 ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2,
2091 0x3 << 6, 0x2 << 6);
2093 break;
2095 case RXPORT_MODE_RAW12_HF:
2096 case RXPORT_MODE_RAW12_LF:
2097 /* Not implemented */
2098 return;
2100 case RXPORT_MODE_CSI2_SYNC:
2101 case RXPORT_MODE_CSI2_NONSYNC:
2103 break;
2106 /* LV_POLARITY & FV_POLARITY */
2107 ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2, 0x3,
2108 rxport->lv_fv_pol);
2110 /* Enable all interrupt sources from this port */
2111 ub960_rxport_write(priv, nport, UB960_RR_PORT_ICR_HI, 0x07);
2112 ub960_rxport_write(priv, nport, UB960_RR_PORT_ICR_LO, 0x7f);
2114 /* Enable I2C_PASS_THROUGH */
2115 ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
2116 UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH,
2117 UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH);
2119 /* Enable I2C communication to the serializer via the alias addr */
2120 ub960_rxport_write(priv, nport, UB960_RR_SER_ALIAS_ID,
2121 rxport->ser.alias << 1);
2123 /* Enable RX port */
2124 ub960_update_bits(priv, UB960_SR_RX_PORT_CTL, BIT(nport), BIT(nport));
2126 if (rxport->cdr_mode == RXPORT_CDR_FPD4) {
2127 /* unreset 960 AEQ */
2128 ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x25, 0x41);
2132 static int ub960_init_rx_ports(struct ub960_data *priv)
2134 unsigned int nport;
2136 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2137 struct ub960_rxport *rxport = priv->rxports[nport];
2139 if (!rxport)
2140 continue;
2142 if (priv->hw_data->is_ub9702)
2143 ub960_init_rx_port_ub9702(priv, rxport);
2144 else
2145 ub960_init_rx_port_ub960(priv, rxport);
2148 return 0;
2151 static void ub960_rxport_handle_events(struct ub960_data *priv, u8 nport)
2153 struct device *dev = &priv->client->dev;
2154 u8 rx_port_sts1;
2155 u8 rx_port_sts2;
2156 u8 csi_rx_sts;
2157 u8 bcc_sts;
2158 int ret = 0;
2160 /* Read interrupts (also clears most of them) */
2161 if (!ret)
2162 ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1,
2163 &rx_port_sts1);
2164 if (!ret)
2165 ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2,
2166 &rx_port_sts2);
2167 if (!ret)
2168 ret = ub960_rxport_read(priv, nport, UB960_RR_CSI_RX_STS,
2169 &csi_rx_sts);
2170 if (!ret)
2171 ret = ub960_rxport_read(priv, nport, UB960_RR_BCC_STATUS,
2172 &bcc_sts);
2174 if (ret)
2175 return;
2177 if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_PARITY_ERROR) {
2178 u16 v;
2180 ret = ub960_rxport_read16(priv, nport, UB960_RR_RX_PAR_ERR_HI,
2181 &v);
2182 if (!ret)
2183 dev_err(dev, "rx%u parity errors: %u\n", nport, v);
2186 if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_BCC_CRC_ERROR)
2187 dev_err(dev, "rx%u BCC CRC error\n", nport);
2189 if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_BCC_SEQ_ERROR)
2190 dev_err(dev, "rx%u BCC SEQ error\n", nport);
2192 if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_LINE_LEN_UNSTABLE)
2193 dev_err(dev, "rx%u line length unstable\n", nport);
2195 if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_FPD3_ENCODE_ERROR)
2196 dev_err(dev, "rx%u FPD3 encode error\n", nport);
2198 if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_BUFFER_ERROR)
2199 dev_err(dev, "rx%u buffer error\n", nport);
2201 if (csi_rx_sts)
2202 dev_err(dev, "rx%u CSI error: %#02x\n", nport, csi_rx_sts);
2204 if (csi_rx_sts & UB960_RR_CSI_RX_STS_ECC1_ERR)
2205 dev_err(dev, "rx%u CSI ECC1 error\n", nport);
2207 if (csi_rx_sts & UB960_RR_CSI_RX_STS_ECC2_ERR)
2208 dev_err(dev, "rx%u CSI ECC2 error\n", nport);
2210 if (csi_rx_sts & UB960_RR_CSI_RX_STS_CKSUM_ERR)
2211 dev_err(dev, "rx%u CSI checksum error\n", nport);
2213 if (csi_rx_sts & UB960_RR_CSI_RX_STS_LENGTH_ERR)
2214 dev_err(dev, "rx%u CSI length error\n", nport);
2216 if (bcc_sts)
2217 dev_err(dev, "rx%u BCC error: %#02x\n", nport, bcc_sts);
2219 if (bcc_sts & UB960_RR_BCC_STATUS_RESP_ERR)
2220 dev_err(dev, "rx%u BCC response error", nport);
2222 if (bcc_sts & UB960_RR_BCC_STATUS_SLAVE_TO)
2223 dev_err(dev, "rx%u BCC slave timeout", nport);
2225 if (bcc_sts & UB960_RR_BCC_STATUS_SLAVE_ERR)
2226 dev_err(dev, "rx%u BCC slave error", nport);
2228 if (bcc_sts & UB960_RR_BCC_STATUS_MASTER_TO)
2229 dev_err(dev, "rx%u BCC master timeout", nport);
2231 if (bcc_sts & UB960_RR_BCC_STATUS_MASTER_ERR)
2232 dev_err(dev, "rx%u BCC master error", nport);
2234 if (bcc_sts & UB960_RR_BCC_STATUS_SEQ_ERROR)
2235 dev_err(dev, "rx%u BCC sequence error", nport);
2237 if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_LINE_LEN_CHG) {
2238 u16 v;
2240 ret = ub960_rxport_read16(priv, nport, UB960_RR_LINE_LEN_1, &v);
2241 if (!ret)
2242 dev_dbg(dev, "rx%u line len changed: %u\n", nport, v);
2245 if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_LINE_CNT_CHG) {
2246 u16 v;
2248 ret = ub960_rxport_read16(priv, nport, UB960_RR_LINE_COUNT_HI,
2249 &v);
2250 if (!ret)
2251 dev_dbg(dev, "rx%u line count changed: %u\n", nport, v);
2254 if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_LOCK_STS_CHG) {
2255 dev_dbg(dev, "rx%u: %s, %s, %s, %s\n", nport,
2256 (rx_port_sts1 & UB960_RR_RX_PORT_STS1_LOCK_STS) ?
2257 "locked" :
2258 "unlocked",
2259 (rx_port_sts1 & UB960_RR_RX_PORT_STS1_PORT_PASS) ?
2260 "passed" :
2261 "not passed",
2262 (rx_port_sts2 & UB960_RR_RX_PORT_STS2_CABLE_FAULT) ?
2263 "no clock" :
2264 "clock ok",
2265 (rx_port_sts2 & UB960_RR_RX_PORT_STS2_FREQ_STABLE) ?
2266 "stable freq" :
2267 "unstable freq");
2271 /* -----------------------------------------------------------------------------
2272 * V4L2
2276 * The current implementation only supports a simple VC mapping, where all VCs
2277 * from a one RX port will be mapped to the same VC. Also, the hardware
2278 * dictates that all streams from an RX port must go to a single TX port.
2280 * This function decides the target VC numbers for each RX port with a simple
2281 * algorithm, so that for each TX port, we get VC numbers starting from 0,
2282 * and counting up.
2284 * E.g. if all four RX ports are in use, of which the first two go to the
2285 * first TX port and the secont two go to the second TX port, we would get
2286 * the following VCs for the four RX ports: 0, 1, 0, 1.
2288 * TODO: implement a more sophisticated VC mapping. As the driver cannot know
2289 * what VCs the sinks expect (say, an FPGA with hardcoded VC routing), this
2290 * probably needs to be somehow configurable. Device tree?
2292 static void ub960_get_vc_maps(struct ub960_data *priv,
2293 struct v4l2_subdev_state *state, u8 *vc)
2295 u8 cur_vc[UB960_MAX_TX_NPORTS] = {};
2296 struct v4l2_subdev_route *route;
2297 u8 handled_mask = 0;
2299 for_each_active_route(&state->routing, route) {
2300 unsigned int rx, tx;
2302 rx = ub960_pad_to_port(priv, route->sink_pad);
2303 if (BIT(rx) & handled_mask)
2304 continue;
2306 tx = ub960_pad_to_port(priv, route->source_pad);
2308 vc[rx] = cur_vc[tx]++;
2309 handled_mask |= BIT(rx);
2313 static int ub960_enable_tx_port(struct ub960_data *priv, unsigned int nport)
2315 struct device *dev = &priv->client->dev;
2317 dev_dbg(dev, "enable TX port %u\n", nport);
2319 return ub960_txport_update_bits(priv, nport, UB960_TR_CSI_CTL,
2320 UB960_TR_CSI_CTL_CSI_ENABLE,
2321 UB960_TR_CSI_CTL_CSI_ENABLE);
2324 static void ub960_disable_tx_port(struct ub960_data *priv, unsigned int nport)
2326 struct device *dev = &priv->client->dev;
2328 dev_dbg(dev, "disable TX port %u\n", nport);
2330 ub960_txport_update_bits(priv, nport, UB960_TR_CSI_CTL,
2331 UB960_TR_CSI_CTL_CSI_ENABLE, 0);
2334 static int ub960_enable_rx_port(struct ub960_data *priv, unsigned int nport)
2336 struct device *dev = &priv->client->dev;
2338 dev_dbg(dev, "enable RX port %u\n", nport);
2340 /* Enable forwarding */
2341 return ub960_update_bits(priv, UB960_SR_FWD_CTL1,
2342 UB960_SR_FWD_CTL1_PORT_DIS(nport), 0);
2345 static void ub960_disable_rx_port(struct ub960_data *priv, unsigned int nport)
2347 struct device *dev = &priv->client->dev;
2349 dev_dbg(dev, "disable RX port %u\n", nport);
2351 /* Disable forwarding */
2352 ub960_update_bits(priv, UB960_SR_FWD_CTL1,
2353 UB960_SR_FWD_CTL1_PORT_DIS(nport),
2354 UB960_SR_FWD_CTL1_PORT_DIS(nport));
2358 * The driver only supports using a single VC for each source. This function
2359 * checks that each source only provides streams using a single VC.
2361 static int ub960_validate_stream_vcs(struct ub960_data *priv)
2363 unsigned int nport;
2364 unsigned int i;
2366 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2367 struct ub960_rxport *rxport = priv->rxports[nport];
2368 struct v4l2_mbus_frame_desc desc;
2369 int ret;
2370 u8 vc;
2372 if (!rxport)
2373 continue;
2375 ret = v4l2_subdev_call(rxport->source.sd, pad, get_frame_desc,
2376 rxport->source.pad, &desc);
2377 if (ret)
2378 return ret;
2380 if (desc.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
2381 continue;
2383 if (desc.num_entries == 0)
2384 continue;
2386 vc = desc.entry[0].bus.csi2.vc;
2388 for (i = 1; i < desc.num_entries; i++) {
2389 if (vc == desc.entry[i].bus.csi2.vc)
2390 continue;
2392 dev_err(&priv->client->dev,
2393 "rx%u: source with multiple virtual-channels is not supported\n",
2394 nport);
2395 return -ENODEV;
2399 return 0;
2402 static int ub960_configure_ports_for_streaming(struct ub960_data *priv,
2403 struct v4l2_subdev_state *state)
2405 u8 fwd_ctl;
2406 struct {
2407 u32 num_streams;
2408 u8 pixel_dt;
2409 u8 meta_dt;
2410 u32 meta_lines;
2411 u32 tx_port;
2412 } rx_data[UB960_MAX_RX_NPORTS] = {};
2413 u8 vc_map[UB960_MAX_RX_NPORTS] = {};
2414 struct v4l2_subdev_route *route;
2415 unsigned int nport;
2416 int ret;
2418 ret = ub960_validate_stream_vcs(priv);
2419 if (ret)
2420 return ret;
2422 ub960_get_vc_maps(priv, state, vc_map);
2424 for_each_active_route(&state->routing, route) {
2425 struct ub960_rxport *rxport;
2426 struct ub960_txport *txport;
2427 struct v4l2_mbus_framefmt *fmt;
2428 const struct ub960_format_info *ub960_fmt;
2429 unsigned int nport;
2431 nport = ub960_pad_to_port(priv, route->sink_pad);
2433 rxport = priv->rxports[nport];
2434 if (!rxport)
2435 return -EINVAL;
2437 txport = priv->txports[ub960_pad_to_port(priv, route->source_pad)];
2438 if (!txport)
2439 return -EINVAL;
2441 rx_data[nport].tx_port = ub960_pad_to_port(priv, route->source_pad);
2443 rx_data[nport].num_streams++;
2445 /* For the rest, we are only interested in parallel busses */
2446 if (rxport->rx_mode == RXPORT_MODE_CSI2_SYNC ||
2447 rxport->rx_mode == RXPORT_MODE_CSI2_NONSYNC)
2448 continue;
2450 if (rx_data[nport].num_streams > 2)
2451 return -EPIPE;
2453 fmt = v4l2_subdev_state_get_format(state, route->sink_pad,
2454 route->sink_stream);
2455 if (!fmt)
2456 return -EPIPE;
2458 ub960_fmt = ub960_find_format(fmt->code);
2459 if (!ub960_fmt)
2460 return -EPIPE;
2462 if (ub960_fmt->meta) {
2463 if (fmt->height > 3) {
2464 dev_err(&priv->client->dev,
2465 "rx%u: unsupported metadata height %u\n",
2466 nport, fmt->height);
2467 return -EPIPE;
2470 rx_data[nport].meta_dt = ub960_fmt->datatype;
2471 rx_data[nport].meta_lines = fmt->height;
2472 } else {
2473 rx_data[nport].pixel_dt = ub960_fmt->datatype;
2477 /* Configure RX ports */
2480 * Keep all port forwardings disabled by default. Forwarding will be
2481 * enabled in ub960_enable_rx_port.
2483 fwd_ctl = GENMASK(7, 4);
2485 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2486 struct ub960_rxport *rxport = priv->rxports[nport];
2487 u8 vc = vc_map[nport];
2489 if (rx_data[nport].num_streams == 0)
2490 continue;
2492 switch (rxport->rx_mode) {
2493 case RXPORT_MODE_RAW10:
2494 ub960_rxport_write(priv, nport, UB960_RR_RAW10_ID,
2495 rx_data[nport].pixel_dt | (vc << UB960_RR_RAW10_ID_VC_SHIFT));
2497 ub960_rxport_write(priv, rxport->nport,
2498 UB960_RR_RAW_EMBED_DTYPE,
2499 (rx_data[nport].meta_lines << UB960_RR_RAW_EMBED_DTYPE_LINES_SHIFT) |
2500 rx_data[nport].meta_dt);
2502 break;
2504 case RXPORT_MODE_RAW12_HF:
2505 case RXPORT_MODE_RAW12_LF:
2506 /* Not implemented */
2507 break;
2509 case RXPORT_MODE_CSI2_SYNC:
2510 case RXPORT_MODE_CSI2_NONSYNC:
2511 if (!priv->hw_data->is_ub9702) {
2512 /* Map all VCs from this port to the same VC */
2513 ub960_rxport_write(priv, nport, UB960_RR_CSI_VC_MAP,
2514 (vc << UB960_RR_CSI_VC_MAP_SHIFT(3)) |
2515 (vc << UB960_RR_CSI_VC_MAP_SHIFT(2)) |
2516 (vc << UB960_RR_CSI_VC_MAP_SHIFT(1)) |
2517 (vc << UB960_RR_CSI_VC_MAP_SHIFT(0)));
2518 } else {
2519 unsigned int i;
2521 /* Map all VCs from this port to VC(nport) */
2522 for (i = 0; i < 8; i++)
2523 ub960_rxport_write(priv, nport,
2524 UB960_RR_VC_ID_MAP(i),
2525 nport);
2528 break;
2531 if (rx_data[nport].tx_port == 1)
2532 fwd_ctl |= BIT(nport); /* forward to TX1 */
2533 else
2534 fwd_ctl &= ~BIT(nport); /* forward to TX0 */
2537 ub960_write(priv, UB960_SR_FWD_CTL1, fwd_ctl);
2539 return 0;
2542 static void ub960_update_streaming_status(struct ub960_data *priv)
2544 unsigned int i;
2546 for (i = 0; i < UB960_MAX_NPORTS; i++) {
2547 if (priv->stream_enable_mask[i])
2548 break;
2551 priv->streaming = i < UB960_MAX_NPORTS;
2554 static int ub960_enable_streams(struct v4l2_subdev *sd,
2555 struct v4l2_subdev_state *state, u32 source_pad,
2556 u64 source_streams_mask)
2558 struct ub960_data *priv = sd_to_ub960(sd);
2559 struct device *dev = &priv->client->dev;
2560 u64 sink_streams[UB960_MAX_RX_NPORTS] = {};
2561 struct v4l2_subdev_route *route;
2562 unsigned int failed_port;
2563 unsigned int nport;
2564 int ret;
2566 if (!priv->streaming) {
2567 dev_dbg(dev, "Prepare for streaming\n");
2568 ret = ub960_configure_ports_for_streaming(priv, state);
2569 if (ret)
2570 return ret;
2573 /* Enable TX port if not yet enabled */
2574 if (!priv->stream_enable_mask[source_pad]) {
2575 ret = ub960_enable_tx_port(priv,
2576 ub960_pad_to_port(priv, source_pad));
2577 if (ret)
2578 return ret;
2581 priv->stream_enable_mask[source_pad] |= source_streams_mask;
2583 /* Collect sink streams per pad which we need to enable */
2584 for_each_active_route(&state->routing, route) {
2585 if (route->source_pad != source_pad)
2586 continue;
2588 if (!(source_streams_mask & BIT_ULL(route->source_stream)))
2589 continue;
2591 nport = ub960_pad_to_port(priv, route->sink_pad);
2593 sink_streams[nport] |= BIT_ULL(route->sink_stream);
2596 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2597 if (!sink_streams[nport])
2598 continue;
2600 /* Enable the RX port if not yet enabled */
2601 if (!priv->stream_enable_mask[nport]) {
2602 ret = ub960_enable_rx_port(priv, nport);
2603 if (ret) {
2604 failed_port = nport;
2605 goto err;
2609 priv->stream_enable_mask[nport] |= sink_streams[nport];
2611 dev_dbg(dev, "enable RX port %u streams %#llx\n", nport,
2612 sink_streams[nport]);
2614 ret = v4l2_subdev_enable_streams(
2615 priv->rxports[nport]->source.sd,
2616 priv->rxports[nport]->source.pad,
2617 sink_streams[nport]);
2618 if (ret) {
2619 priv->stream_enable_mask[nport] &= ~sink_streams[nport];
2621 if (!priv->stream_enable_mask[nport])
2622 ub960_disable_rx_port(priv, nport);
2624 failed_port = nport;
2625 goto err;
2629 priv->streaming = true;
2631 return 0;
2633 err:
2634 for (nport = 0; nport < failed_port; nport++) {
2635 if (!sink_streams[nport])
2636 continue;
2638 dev_dbg(dev, "disable RX port %u streams %#llx\n", nport,
2639 sink_streams[nport]);
2641 ret = v4l2_subdev_disable_streams(
2642 priv->rxports[nport]->source.sd,
2643 priv->rxports[nport]->source.pad,
2644 sink_streams[nport]);
2645 if (ret)
2646 dev_err(dev, "Failed to disable streams: %d\n", ret);
2648 priv->stream_enable_mask[nport] &= ~sink_streams[nport];
2650 /* Disable RX port if no active streams */
2651 if (!priv->stream_enable_mask[nport])
2652 ub960_disable_rx_port(priv, nport);
2655 priv->stream_enable_mask[source_pad] &= ~source_streams_mask;
2657 if (!priv->stream_enable_mask[source_pad])
2658 ub960_disable_tx_port(priv,
2659 ub960_pad_to_port(priv, source_pad));
2661 ub960_update_streaming_status(priv);
2663 return ret;
2666 static int ub960_disable_streams(struct v4l2_subdev *sd,
2667 struct v4l2_subdev_state *state,
2668 u32 source_pad, u64 source_streams_mask)
2670 struct ub960_data *priv = sd_to_ub960(sd);
2671 struct device *dev = &priv->client->dev;
2672 u64 sink_streams[UB960_MAX_RX_NPORTS] = {};
2673 struct v4l2_subdev_route *route;
2674 unsigned int nport;
2675 int ret;
2677 /* Collect sink streams per pad which we need to disable */
2678 for_each_active_route(&state->routing, route) {
2679 if (route->source_pad != source_pad)
2680 continue;
2682 if (!(source_streams_mask & BIT_ULL(route->source_stream)))
2683 continue;
2685 nport = ub960_pad_to_port(priv, route->sink_pad);
2687 sink_streams[nport] |= BIT_ULL(route->sink_stream);
2690 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2691 if (!sink_streams[nport])
2692 continue;
2694 dev_dbg(dev, "disable RX port %u streams %#llx\n", nport,
2695 sink_streams[nport]);
2697 ret = v4l2_subdev_disable_streams(
2698 priv->rxports[nport]->source.sd,
2699 priv->rxports[nport]->source.pad,
2700 sink_streams[nport]);
2701 if (ret)
2702 dev_err(dev, "Failed to disable streams: %d\n", ret);
2704 priv->stream_enable_mask[nport] &= ~sink_streams[nport];
2706 /* Disable RX port if no active streams */
2707 if (!priv->stream_enable_mask[nport])
2708 ub960_disable_rx_port(priv, nport);
2711 /* Disable TX port if no active streams */
2713 priv->stream_enable_mask[source_pad] &= ~source_streams_mask;
2715 if (!priv->stream_enable_mask[source_pad])
2716 ub960_disable_tx_port(priv,
2717 ub960_pad_to_port(priv, source_pad));
2719 ub960_update_streaming_status(priv);
2721 return 0;
2724 static int _ub960_set_routing(struct v4l2_subdev *sd,
2725 struct v4l2_subdev_state *state,
2726 struct v4l2_subdev_krouting *routing)
2728 static const struct v4l2_mbus_framefmt format = {
2729 .width = 640,
2730 .height = 480,
2731 .code = MEDIA_BUS_FMT_UYVY8_1X16,
2732 .field = V4L2_FIELD_NONE,
2733 .colorspace = V4L2_COLORSPACE_SRGB,
2734 .ycbcr_enc = V4L2_YCBCR_ENC_601,
2735 .quantization = V4L2_QUANTIZATION_LIM_RANGE,
2736 .xfer_func = V4L2_XFER_FUNC_SRGB,
2738 int ret;
2741 * Note: we can only support up to V4L2_FRAME_DESC_ENTRY_MAX, until
2742 * frame desc is made dynamically allocated.
2745 if (routing->num_routes > V4L2_FRAME_DESC_ENTRY_MAX)
2746 return -E2BIG;
2748 ret = v4l2_subdev_routing_validate(sd, routing,
2749 V4L2_SUBDEV_ROUTING_ONLY_1_TO_1 |
2750 V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX);
2751 if (ret)
2752 return ret;
2754 ret = v4l2_subdev_set_routing_with_fmt(sd, state, routing, &format);
2755 if (ret)
2756 return ret;
2758 return 0;
2761 static int ub960_set_routing(struct v4l2_subdev *sd,
2762 struct v4l2_subdev_state *state,
2763 enum v4l2_subdev_format_whence which,
2764 struct v4l2_subdev_krouting *routing)
2766 struct ub960_data *priv = sd_to_ub960(sd);
2768 if (which == V4L2_SUBDEV_FORMAT_ACTIVE && priv->streaming)
2769 return -EBUSY;
2771 return _ub960_set_routing(sd, state, routing);
2774 static int ub960_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
2775 struct v4l2_mbus_frame_desc *fd)
2777 struct ub960_data *priv = sd_to_ub960(sd);
2778 struct v4l2_subdev_route *route;
2779 struct v4l2_subdev_state *state;
2780 int ret = 0;
2781 struct device *dev = &priv->client->dev;
2782 u8 vc_map[UB960_MAX_RX_NPORTS] = {};
2784 if (!ub960_pad_is_source(priv, pad))
2785 return -EINVAL;
2787 fd->type = V4L2_MBUS_FRAME_DESC_TYPE_CSI2;
2789 state = v4l2_subdev_lock_and_get_active_state(&priv->sd);
2791 ub960_get_vc_maps(priv, state, vc_map);
2793 for_each_active_route(&state->routing, route) {
2794 struct v4l2_mbus_frame_desc_entry *source_entry = NULL;
2795 struct v4l2_mbus_frame_desc source_fd;
2796 unsigned int nport;
2797 unsigned int i;
2799 if (route->source_pad != pad)
2800 continue;
2802 nport = ub960_pad_to_port(priv, route->sink_pad);
2804 ret = v4l2_subdev_call(priv->rxports[nport]->source.sd, pad,
2805 get_frame_desc,
2806 priv->rxports[nport]->source.pad,
2807 &source_fd);
2808 if (ret) {
2809 dev_err(dev,
2810 "Failed to get source frame desc for pad %u\n",
2811 route->sink_pad);
2812 goto out_unlock;
2815 for (i = 0; i < source_fd.num_entries; i++) {
2816 if (source_fd.entry[i].stream == route->sink_stream) {
2817 source_entry = &source_fd.entry[i];
2818 break;
2822 if (!source_entry) {
2823 dev_err(dev,
2824 "Failed to find stream from source frame desc\n");
2825 ret = -EPIPE;
2826 goto out_unlock;
2829 fd->entry[fd->num_entries].stream = route->source_stream;
2830 fd->entry[fd->num_entries].flags = source_entry->flags;
2831 fd->entry[fd->num_entries].length = source_entry->length;
2832 fd->entry[fd->num_entries].pixelcode = source_entry->pixelcode;
2834 fd->entry[fd->num_entries].bus.csi2.vc = vc_map[nport];
2836 if (source_fd.type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
2837 fd->entry[fd->num_entries].bus.csi2.dt =
2838 source_entry->bus.csi2.dt;
2839 } else {
2840 const struct ub960_format_info *ub960_fmt;
2841 struct v4l2_mbus_framefmt *fmt;
2843 fmt = v4l2_subdev_state_get_format(state, pad,
2844 route->source_stream);
2846 if (!fmt) {
2847 ret = -EINVAL;
2848 goto out_unlock;
2851 ub960_fmt = ub960_find_format(fmt->code);
2852 if (!ub960_fmt) {
2853 dev_err(dev, "Unable to find format\n");
2854 ret = -EINVAL;
2855 goto out_unlock;
2858 fd->entry[fd->num_entries].bus.csi2.dt =
2859 ub960_fmt->datatype;
2862 fd->num_entries++;
2865 out_unlock:
2866 v4l2_subdev_unlock_state(state);
2868 return ret;
2871 static int ub960_set_fmt(struct v4l2_subdev *sd,
2872 struct v4l2_subdev_state *state,
2873 struct v4l2_subdev_format *format)
2875 struct ub960_data *priv = sd_to_ub960(sd);
2876 struct v4l2_mbus_framefmt *fmt;
2878 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE && priv->streaming)
2879 return -EBUSY;
2881 /* No transcoding, source and sink formats must match. */
2882 if (ub960_pad_is_source(priv, format->pad))
2883 return v4l2_subdev_get_fmt(sd, state, format);
2886 * Default to the first format if the requested media bus code isn't
2887 * supported.
2889 if (!ub960_find_format(format->format.code))
2890 format->format.code = ub960_formats[0].code;
2892 fmt = v4l2_subdev_state_get_format(state, format->pad, format->stream);
2893 if (!fmt)
2894 return -EINVAL;
2896 *fmt = format->format;
2898 fmt = v4l2_subdev_state_get_opposite_stream_format(state, format->pad,
2899 format->stream);
2900 if (!fmt)
2901 return -EINVAL;
2903 *fmt = format->format;
2905 return 0;
2908 static int ub960_init_state(struct v4l2_subdev *sd,
2909 struct v4l2_subdev_state *state)
2911 struct ub960_data *priv = sd_to_ub960(sd);
2913 struct v4l2_subdev_route routes[] = {
2915 .sink_pad = 0,
2916 .sink_stream = 0,
2917 .source_pad = priv->hw_data->num_rxports,
2918 .source_stream = 0,
2919 .flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE,
2923 struct v4l2_subdev_krouting routing = {
2924 .num_routes = ARRAY_SIZE(routes),
2925 .routes = routes,
2928 return _ub960_set_routing(sd, state, &routing);
2931 static const struct v4l2_subdev_pad_ops ub960_pad_ops = {
2932 .enable_streams = ub960_enable_streams,
2933 .disable_streams = ub960_disable_streams,
2935 .set_routing = ub960_set_routing,
2936 .get_frame_desc = ub960_get_frame_desc,
2938 .get_fmt = v4l2_subdev_get_fmt,
2939 .set_fmt = ub960_set_fmt,
2942 static int ub960_log_status(struct v4l2_subdev *sd)
2944 struct ub960_data *priv = sd_to_ub960(sd);
2945 struct device *dev = &priv->client->dev;
2946 struct v4l2_subdev_state *state;
2947 unsigned int nport;
2948 unsigned int i;
2949 u16 v16 = 0;
2950 u8 v = 0;
2951 u8 id[UB960_SR_FPD3_RX_ID_LEN];
2953 state = v4l2_subdev_lock_and_get_active_state(sd);
2955 for (i = 0; i < sizeof(id); i++)
2956 ub960_read(priv, UB960_SR_FPD3_RX_ID(i), &id[i]);
2958 dev_info(dev, "ID '%.*s'\n", (int)sizeof(id), id);
2960 for (nport = 0; nport < priv->hw_data->num_txports; nport++) {
2961 struct ub960_txport *txport = priv->txports[nport];
2963 dev_info(dev, "TX %u\n", nport);
2965 if (!txport) {
2966 dev_info(dev, "\tNot initialized\n");
2967 continue;
2970 ub960_txport_read(priv, nport, UB960_TR_CSI_STS, &v);
2971 dev_info(dev, "\tsync %u, pass %u\n", v & (u8)BIT(1),
2972 v & (u8)BIT(0));
2974 ub960_read16(priv, UB960_SR_CSI_FRAME_COUNT_HI(nport), &v16);
2975 dev_info(dev, "\tframe counter %u\n", v16);
2977 ub960_read16(priv, UB960_SR_CSI_FRAME_ERR_COUNT_HI(nport), &v16);
2978 dev_info(dev, "\tframe error counter %u\n", v16);
2980 ub960_read16(priv, UB960_SR_CSI_LINE_COUNT_HI(nport), &v16);
2981 dev_info(dev, "\tline counter %u\n", v16);
2983 ub960_read16(priv, UB960_SR_CSI_LINE_ERR_COUNT_HI(nport), &v16);
2984 dev_info(dev, "\tline error counter %u\n", v16);
2987 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2988 struct ub960_rxport *rxport = priv->rxports[nport];
2989 u8 eq_level;
2990 s8 strobe_pos;
2991 unsigned int i;
2993 dev_info(dev, "RX %u\n", nport);
2995 if (!rxport) {
2996 dev_info(dev, "\tNot initialized\n");
2997 continue;
3000 ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1, &v);
3002 if (v & UB960_RR_RX_PORT_STS1_LOCK_STS)
3003 dev_info(dev, "\tLocked\n");
3004 else
3005 dev_info(dev, "\tNot locked\n");
3007 dev_info(dev, "\trx_port_sts1 %#02x\n", v);
3008 ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2, &v);
3009 dev_info(dev, "\trx_port_sts2 %#02x\n", v);
3011 ub960_rxport_read16(priv, nport, UB960_RR_RX_FREQ_HIGH, &v16);
3012 dev_info(dev, "\tlink freq %llu Hz\n", (v16 * 1000000ULL) >> 8);
3014 ub960_rxport_read16(priv, nport, UB960_RR_RX_PAR_ERR_HI, &v16);
3015 dev_info(dev, "\tparity errors %u\n", v16);
3017 ub960_rxport_read16(priv, nport, UB960_RR_LINE_COUNT_HI, &v16);
3018 dev_info(dev, "\tlines per frame %u\n", v16);
3020 ub960_rxport_read16(priv, nport, UB960_RR_LINE_LEN_1, &v16);
3021 dev_info(dev, "\tbytes per line %u\n", v16);
3023 ub960_rxport_read(priv, nport, UB960_RR_CSI_ERR_COUNTER, &v);
3024 dev_info(dev, "\tcsi_err_counter %u\n", v);
3026 /* Strobe */
3028 ub960_read(priv, UB960_XR_AEQ_CTL1, &v);
3030 dev_info(dev, "\t%s strobe\n",
3031 (v & UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN) ? "Adaptive" :
3032 "Manual");
3034 if (v & UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN) {
3035 ub960_read(priv, UB960_XR_SFILTER_CFG, &v);
3037 dev_info(dev, "\tStrobe range [%d, %d]\n",
3038 ((v >> UB960_XR_SFILTER_CFG_SFILTER_MIN_SHIFT) & 0xf) - 7,
3039 ((v >> UB960_XR_SFILTER_CFG_SFILTER_MAX_SHIFT) & 0xf) - 7);
3042 ub960_rxport_get_strobe_pos(priv, nport, &strobe_pos);
3044 dev_info(dev, "\tStrobe pos %d\n", strobe_pos);
3046 /* EQ */
3048 ub960_rxport_read(priv, nport, UB960_RR_AEQ_BYPASS, &v);
3050 dev_info(dev, "\t%s EQ\n",
3051 (v & UB960_RR_AEQ_BYPASS_ENABLE) ? "Manual" :
3052 "Adaptive");
3054 if (!(v & UB960_RR_AEQ_BYPASS_ENABLE)) {
3055 ub960_rxport_read(priv, nport, UB960_RR_AEQ_MIN_MAX, &v);
3057 dev_info(dev, "\tEQ range [%u, %u]\n",
3058 (v >> UB960_RR_AEQ_MIN_MAX_AEQ_FLOOR_SHIFT) & 0xf,
3059 (v >> UB960_RR_AEQ_MIN_MAX_AEQ_MAX_SHIFT) & 0xf);
3062 if (ub960_rxport_get_eq_level(priv, nport, &eq_level) == 0)
3063 dev_info(dev, "\tEQ level %u\n", eq_level);
3065 /* GPIOs */
3066 for (i = 0; i < UB960_NUM_BC_GPIOS; i++) {
3067 u8 ctl_reg;
3068 u8 ctl_shift;
3070 ctl_reg = UB960_RR_BC_GPIO_CTL(i / 2);
3071 ctl_shift = (i % 2) * 4;
3073 ub960_rxport_read(priv, nport, ctl_reg, &v);
3075 dev_info(dev, "\tGPIO%u: mode %u\n", i,
3076 (v >> ctl_shift) & 0xf);
3080 v4l2_subdev_unlock_state(state);
3082 return 0;
3085 static const struct v4l2_subdev_core_ops ub960_subdev_core_ops = {
3086 .log_status = ub960_log_status,
3089 static const struct v4l2_subdev_internal_ops ub960_internal_ops = {
3090 .init_state = ub960_init_state,
3093 static const struct v4l2_subdev_ops ub960_subdev_ops = {
3094 .core = &ub960_subdev_core_ops,
3095 .pad = &ub960_pad_ops,
3098 static const struct media_entity_operations ub960_entity_ops = {
3099 .get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
3100 .link_validate = v4l2_subdev_link_validate,
3101 .has_pad_interdep = v4l2_subdev_has_pad_interdep,
3104 /* -----------------------------------------------------------------------------
3105 * Core
3108 static irqreturn_t ub960_handle_events(int irq, void *arg)
3110 struct ub960_data *priv = arg;
3111 unsigned int i;
3112 u8 int_sts;
3113 u8 fwd_sts;
3114 int ret;
3116 ret = ub960_read(priv, UB960_SR_INTERRUPT_STS, &int_sts);
3117 if (ret || !int_sts)
3118 return IRQ_NONE;
3120 dev_dbg(&priv->client->dev, "INTERRUPT_STS %x\n", int_sts);
3122 ret = ub960_read(priv, UB960_SR_FWD_STS, &fwd_sts);
3123 if (ret)
3124 return IRQ_NONE;
3126 dev_dbg(&priv->client->dev, "FWD_STS %#02x\n", fwd_sts);
3128 for (i = 0; i < priv->hw_data->num_txports; i++) {
3129 if (int_sts & UB960_SR_INTERRUPT_STS_IS_CSI_TX(i))
3130 ub960_csi_handle_events(priv, i);
3133 for (i = 0; i < priv->hw_data->num_rxports; i++) {
3134 if (!priv->rxports[i])
3135 continue;
3137 if (int_sts & UB960_SR_INTERRUPT_STS_IS_RX(i))
3138 ub960_rxport_handle_events(priv, i);
3141 return IRQ_HANDLED;
3144 static void ub960_handler_work(struct work_struct *work)
3146 struct delayed_work *dwork = to_delayed_work(work);
3147 struct ub960_data *priv =
3148 container_of(dwork, struct ub960_data, poll_work);
3150 ub960_handle_events(0, priv);
3152 schedule_delayed_work(&priv->poll_work,
3153 msecs_to_jiffies(UB960_POLL_TIME_MS));
3156 static void ub960_txport_free_ports(struct ub960_data *priv)
3158 unsigned int nport;
3160 for (nport = 0; nport < priv->hw_data->num_txports; nport++) {
3161 struct ub960_txport *txport = priv->txports[nport];
3163 if (!txport)
3164 continue;
3166 kfree(txport);
3167 priv->txports[nport] = NULL;
3171 static void ub960_rxport_free_ports(struct ub960_data *priv)
3173 unsigned int nport;
3175 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
3176 struct ub960_rxport *rxport = priv->rxports[nport];
3178 if (!rxport)
3179 continue;
3181 fwnode_handle_put(rxport->source.ep_fwnode);
3182 fwnode_handle_put(rxport->ser.fwnode);
3184 kfree(rxport);
3185 priv->rxports[nport] = NULL;
3189 static int
3190 ub960_parse_dt_rxport_link_properties(struct ub960_data *priv,
3191 struct fwnode_handle *link_fwnode,
3192 struct ub960_rxport *rxport)
3194 struct device *dev = &priv->client->dev;
3195 unsigned int nport = rxport->nport;
3196 u32 rx_mode;
3197 u32 cdr_mode;
3198 s32 strobe_pos;
3199 u32 eq_level;
3200 u32 ser_i2c_alias;
3201 int ret;
3203 cdr_mode = RXPORT_CDR_FPD3;
3205 ret = fwnode_property_read_u32(link_fwnode, "ti,cdr-mode", &cdr_mode);
3206 if (ret < 0 && ret != -EINVAL) {
3207 dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
3208 "ti,cdr-mode", ret);
3209 return ret;
3212 if (cdr_mode > RXPORT_CDR_LAST) {
3213 dev_err(dev, "rx%u: bad 'ti,cdr-mode' %u\n", nport, cdr_mode);
3214 return -EINVAL;
3217 if (!priv->hw_data->is_fpdlink4 && cdr_mode == RXPORT_CDR_FPD4) {
3218 dev_err(dev, "rx%u: FPD-Link 4 CDR not supported\n", nport);
3219 return -EINVAL;
3222 rxport->cdr_mode = cdr_mode;
3224 ret = fwnode_property_read_u32(link_fwnode, "ti,rx-mode", &rx_mode);
3225 if (ret < 0) {
3226 dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
3227 "ti,rx-mode", ret);
3228 return ret;
3231 if (rx_mode > RXPORT_MODE_LAST) {
3232 dev_err(dev, "rx%u: bad 'ti,rx-mode' %u\n", nport, rx_mode);
3233 return -EINVAL;
3236 switch (rx_mode) {
3237 case RXPORT_MODE_RAW12_HF:
3238 case RXPORT_MODE_RAW12_LF:
3239 dev_err(dev, "rx%u: unsupported 'ti,rx-mode' %u\n", nport,
3240 rx_mode);
3241 return -EINVAL;
3242 default:
3243 break;
3246 rxport->rx_mode = rx_mode;
3248 /* EQ & Strobe related */
3250 /* Defaults */
3251 rxport->eq.manual_eq = false;
3252 rxport->eq.aeq.eq_level_min = UB960_MIN_EQ_LEVEL;
3253 rxport->eq.aeq.eq_level_max = UB960_MAX_EQ_LEVEL;
3255 ret = fwnode_property_read_u32(link_fwnode, "ti,strobe-pos",
3256 &strobe_pos);
3257 if (ret) {
3258 if (ret != -EINVAL) {
3259 dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
3260 "ti,strobe-pos", ret);
3261 return ret;
3263 } else {
3264 if (strobe_pos < UB960_MIN_MANUAL_STROBE_POS ||
3265 strobe_pos > UB960_MAX_MANUAL_STROBE_POS) {
3266 dev_err(dev, "rx%u: illegal 'strobe-pos' value: %d\n",
3267 nport, strobe_pos);
3268 return -EINVAL;
3271 /* NOTE: ignored unless global manual strobe pos is also set */
3272 rxport->eq.strobe_pos = strobe_pos;
3273 if (!priv->strobe.manual)
3274 dev_warn(dev,
3275 "rx%u: 'ti,strobe-pos' ignored as 'ti,manual-strobe' not set\n",
3276 nport);
3279 ret = fwnode_property_read_u32(link_fwnode, "ti,eq-level", &eq_level);
3280 if (ret) {
3281 if (ret != -EINVAL) {
3282 dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
3283 "ti,eq-level", ret);
3284 return ret;
3286 } else {
3287 if (eq_level > UB960_MAX_EQ_LEVEL) {
3288 dev_err(dev, "rx%u: illegal 'ti,eq-level' value: %d\n",
3289 nport, eq_level);
3290 return -EINVAL;
3293 rxport->eq.manual_eq = true;
3294 rxport->eq.manual.eq_level = eq_level;
3297 ret = fwnode_property_read_u32(link_fwnode, "i2c-alias",
3298 &ser_i2c_alias);
3299 if (ret) {
3300 dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
3301 "i2c-alias", ret);
3302 return ret;
3304 rxport->ser.alias = ser_i2c_alias;
3306 rxport->ser.fwnode = fwnode_get_named_child_node(link_fwnode, "serializer");
3307 if (!rxport->ser.fwnode) {
3308 dev_err(dev, "rx%u: missing 'serializer' node\n", nport);
3309 return -EINVAL;
3312 return 0;
3315 static int ub960_parse_dt_rxport_ep_properties(struct ub960_data *priv,
3316 struct fwnode_handle *ep_fwnode,
3317 struct ub960_rxport *rxport)
3319 struct device *dev = &priv->client->dev;
3320 struct v4l2_fwnode_endpoint vep = {};
3321 unsigned int nport = rxport->nport;
3322 bool hsync_hi;
3323 bool vsync_hi;
3324 int ret;
3326 rxport->source.ep_fwnode = fwnode_graph_get_remote_endpoint(ep_fwnode);
3327 if (!rxport->source.ep_fwnode) {
3328 dev_err(dev, "rx%u: no remote endpoint\n", nport);
3329 return -ENODEV;
3332 /* We currently have properties only for RAW modes */
3334 switch (rxport->rx_mode) {
3335 case RXPORT_MODE_RAW10:
3336 case RXPORT_MODE_RAW12_HF:
3337 case RXPORT_MODE_RAW12_LF:
3338 break;
3339 default:
3340 return 0;
3343 vep.bus_type = V4L2_MBUS_PARALLEL;
3344 ret = v4l2_fwnode_endpoint_parse(ep_fwnode, &vep);
3345 if (ret) {
3346 dev_err(dev, "rx%u: failed to parse endpoint data\n", nport);
3347 goto err_put_source_ep_fwnode;
3350 hsync_hi = !!(vep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH);
3351 vsync_hi = !!(vep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH);
3353 /* LineValid and FrameValid are inverse to the h/vsync active */
3354 rxport->lv_fv_pol = (hsync_hi ? UB960_RR_PORT_CONFIG2_LV_POL_LOW : 0) |
3355 (vsync_hi ? UB960_RR_PORT_CONFIG2_FV_POL_LOW : 0);
3357 return 0;
3359 err_put_source_ep_fwnode:
3360 fwnode_handle_put(rxport->source.ep_fwnode);
3361 return ret;
3364 static int ub960_parse_dt_rxport(struct ub960_data *priv, unsigned int nport,
3365 struct fwnode_handle *link_fwnode,
3366 struct fwnode_handle *ep_fwnode)
3368 static const char *vpoc_names[UB960_MAX_RX_NPORTS] = {
3369 "vpoc0", "vpoc1", "vpoc2", "vpoc3"
3371 struct device *dev = &priv->client->dev;
3372 struct ub960_rxport *rxport;
3373 int ret;
3375 rxport = kzalloc(sizeof(*rxport), GFP_KERNEL);
3376 if (!rxport)
3377 return -ENOMEM;
3379 priv->rxports[nport] = rxport;
3381 rxport->nport = nport;
3382 rxport->priv = priv;
3384 ret = ub960_parse_dt_rxport_link_properties(priv, link_fwnode, rxport);
3385 if (ret)
3386 goto err_free_rxport;
3388 rxport->vpoc = devm_regulator_get_optional(dev, vpoc_names[nport]);
3389 if (IS_ERR(rxport->vpoc)) {
3390 ret = PTR_ERR(rxport->vpoc);
3391 if (ret == -ENODEV) {
3392 rxport->vpoc = NULL;
3393 } else {
3394 dev_err(dev, "rx%u: failed to get VPOC supply: %d\n",
3395 nport, ret);
3396 goto err_put_remote_fwnode;
3400 ret = ub960_parse_dt_rxport_ep_properties(priv, ep_fwnode, rxport);
3401 if (ret)
3402 goto err_put_remote_fwnode;
3404 return 0;
3406 err_put_remote_fwnode:
3407 fwnode_handle_put(rxport->ser.fwnode);
3408 err_free_rxport:
3409 priv->rxports[nport] = NULL;
3410 kfree(rxport);
3411 return ret;
3414 static struct fwnode_handle *
3415 ub960_fwnode_get_link_by_regs(struct fwnode_handle *links_fwnode,
3416 unsigned int nport)
3418 struct fwnode_handle *link_fwnode;
3419 int ret;
3421 fwnode_for_each_child_node(links_fwnode, link_fwnode) {
3422 u32 link_num;
3424 if (!str_has_prefix(fwnode_get_name(link_fwnode), "link@"))
3425 continue;
3427 ret = fwnode_property_read_u32(link_fwnode, "reg", &link_num);
3428 if (ret) {
3429 fwnode_handle_put(link_fwnode);
3430 return NULL;
3433 if (nport == link_num)
3434 return link_fwnode;
3437 return NULL;
3440 static int ub960_parse_dt_rxports(struct ub960_data *priv)
3442 struct device *dev = &priv->client->dev;
3443 struct fwnode_handle *links_fwnode;
3444 unsigned int nport;
3445 int ret;
3447 links_fwnode = fwnode_get_named_child_node(dev_fwnode(dev), "links");
3448 if (!links_fwnode) {
3449 dev_err(dev, "'links' node missing\n");
3450 return -ENODEV;
3453 /* Defaults, recommended by TI */
3454 priv->strobe.min = 2;
3455 priv->strobe.max = 3;
3457 priv->strobe.manual = fwnode_property_read_bool(links_fwnode, "ti,manual-strobe");
3459 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
3460 struct fwnode_handle *link_fwnode;
3461 struct fwnode_handle *ep_fwnode;
3463 link_fwnode = ub960_fwnode_get_link_by_regs(links_fwnode, nport);
3464 if (!link_fwnode)
3465 continue;
3467 ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
3468 nport, 0, 0);
3469 if (!ep_fwnode) {
3470 fwnode_handle_put(link_fwnode);
3471 continue;
3474 ret = ub960_parse_dt_rxport(priv, nport, link_fwnode,
3475 ep_fwnode);
3477 fwnode_handle_put(link_fwnode);
3478 fwnode_handle_put(ep_fwnode);
3480 if (ret) {
3481 dev_err(dev, "rx%u: failed to parse RX port\n", nport);
3482 goto err_put_links;
3486 fwnode_handle_put(links_fwnode);
3488 return 0;
3490 err_put_links:
3491 fwnode_handle_put(links_fwnode);
3493 return ret;
3496 static int ub960_parse_dt_txports(struct ub960_data *priv)
3498 struct device *dev = &priv->client->dev;
3499 u32 nport;
3500 int ret;
3502 for (nport = 0; nport < priv->hw_data->num_txports; nport++) {
3503 unsigned int port = nport + priv->hw_data->num_rxports;
3504 struct fwnode_handle *ep_fwnode;
3506 ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
3507 port, 0, 0);
3508 if (!ep_fwnode)
3509 continue;
3511 ret = ub960_parse_dt_txport(priv, ep_fwnode, nport);
3513 fwnode_handle_put(ep_fwnode);
3515 if (ret)
3516 break;
3519 return 0;
3522 static int ub960_parse_dt(struct ub960_data *priv)
3524 int ret;
3526 ret = ub960_parse_dt_rxports(priv);
3527 if (ret)
3528 return ret;
3530 ret = ub960_parse_dt_txports(priv);
3531 if (ret)
3532 goto err_free_rxports;
3534 return 0;
3536 err_free_rxports:
3537 ub960_rxport_free_ports(priv);
3539 return ret;
3542 static int ub960_notify_bound(struct v4l2_async_notifier *notifier,
3543 struct v4l2_subdev *subdev,
3544 struct v4l2_async_connection *asd)
3546 struct ub960_data *priv = sd_to_ub960(notifier->sd);
3547 struct ub960_rxport *rxport = to_ub960_asd(asd)->rxport;
3548 struct device *dev = &priv->client->dev;
3549 u8 nport = rxport->nport;
3550 unsigned int i;
3551 int ret;
3553 ret = media_entity_get_fwnode_pad(&subdev->entity,
3554 rxport->source.ep_fwnode,
3555 MEDIA_PAD_FL_SOURCE);
3556 if (ret < 0) {
3557 dev_err(dev, "Failed to find pad for %s\n", subdev->name);
3558 return ret;
3561 rxport->source.sd = subdev;
3562 rxport->source.pad = ret;
3564 ret = media_create_pad_link(&rxport->source.sd->entity,
3565 rxport->source.pad, &priv->sd.entity, nport,
3566 MEDIA_LNK_FL_ENABLED |
3567 MEDIA_LNK_FL_IMMUTABLE);
3568 if (ret) {
3569 dev_err(dev, "Unable to link %s:%u -> %s:%u\n",
3570 rxport->source.sd->name, rxport->source.pad,
3571 priv->sd.name, nport);
3572 return ret;
3575 for (i = 0; i < priv->hw_data->num_rxports; i++) {
3576 if (priv->rxports[i] && !priv->rxports[i]->source.sd) {
3577 dev_dbg(dev, "Waiting for more subdevs to be bound\n");
3578 return 0;
3582 return 0;
3585 static void ub960_notify_unbind(struct v4l2_async_notifier *notifier,
3586 struct v4l2_subdev *subdev,
3587 struct v4l2_async_connection *asd)
3589 struct ub960_rxport *rxport = to_ub960_asd(asd)->rxport;
3591 rxport->source.sd = NULL;
3594 static const struct v4l2_async_notifier_operations ub960_notify_ops = {
3595 .bound = ub960_notify_bound,
3596 .unbind = ub960_notify_unbind,
3599 static int ub960_v4l2_notifier_register(struct ub960_data *priv)
3601 struct device *dev = &priv->client->dev;
3602 unsigned int i;
3603 int ret;
3605 v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd);
3607 for (i = 0; i < priv->hw_data->num_rxports; i++) {
3608 struct ub960_rxport *rxport = priv->rxports[i];
3609 struct ub960_asd *asd;
3611 if (!rxport)
3612 continue;
3614 asd = v4l2_async_nf_add_fwnode(&priv->notifier,
3615 rxport->source.ep_fwnode,
3616 struct ub960_asd);
3617 if (IS_ERR(asd)) {
3618 dev_err(dev, "Failed to add subdev for source %u: %pe",
3619 i, asd);
3620 v4l2_async_nf_cleanup(&priv->notifier);
3621 return PTR_ERR(asd);
3624 asd->rxport = rxport;
3627 priv->notifier.ops = &ub960_notify_ops;
3629 ret = v4l2_async_nf_register(&priv->notifier);
3630 if (ret) {
3631 dev_err(dev, "Failed to register subdev_notifier");
3632 v4l2_async_nf_cleanup(&priv->notifier);
3633 return ret;
3636 return 0;
3639 static void ub960_v4l2_notifier_unregister(struct ub960_data *priv)
3641 v4l2_async_nf_unregister(&priv->notifier);
3642 v4l2_async_nf_cleanup(&priv->notifier);
3645 static int ub960_create_subdev(struct ub960_data *priv)
3647 struct device *dev = &priv->client->dev;
3648 unsigned int i;
3649 int ret;
3651 v4l2_i2c_subdev_init(&priv->sd, priv->client, &ub960_subdev_ops);
3652 priv->sd.internal_ops = &ub960_internal_ops;
3654 v4l2_ctrl_handler_init(&priv->ctrl_handler, 1);
3655 priv->sd.ctrl_handler = &priv->ctrl_handler;
3657 v4l2_ctrl_new_int_menu(&priv->ctrl_handler, NULL, V4L2_CID_LINK_FREQ,
3658 ARRAY_SIZE(priv->tx_link_freq) - 1, 0,
3659 priv->tx_link_freq);
3661 if (priv->ctrl_handler.error) {
3662 ret = priv->ctrl_handler.error;
3663 goto err_free_ctrl;
3666 priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3667 V4L2_SUBDEV_FL_STREAMS;
3668 priv->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
3669 priv->sd.entity.ops = &ub960_entity_ops;
3671 for (i = 0; i < priv->hw_data->num_rxports + priv->hw_data->num_txports; i++) {
3672 priv->pads[i].flags = ub960_pad_is_sink(priv, i) ?
3673 MEDIA_PAD_FL_SINK :
3674 MEDIA_PAD_FL_SOURCE;
3677 ret = media_entity_pads_init(&priv->sd.entity,
3678 priv->hw_data->num_rxports +
3679 priv->hw_data->num_txports,
3680 priv->pads);
3681 if (ret)
3682 goto err_free_ctrl;
3684 priv->sd.state_lock = priv->sd.ctrl_handler->lock;
3686 ret = v4l2_subdev_init_finalize(&priv->sd);
3687 if (ret)
3688 goto err_entity_cleanup;
3690 ret = ub960_v4l2_notifier_register(priv);
3691 if (ret) {
3692 dev_err(dev, "v4l2 subdev notifier register failed: %d\n", ret);
3693 goto err_subdev_cleanup;
3696 ret = v4l2_async_register_subdev(&priv->sd);
3697 if (ret) {
3698 dev_err(dev, "v4l2_async_register_subdev error: %d\n", ret);
3699 goto err_unreg_notif;
3702 return 0;
3704 err_unreg_notif:
3705 ub960_v4l2_notifier_unregister(priv);
3706 err_subdev_cleanup:
3707 v4l2_subdev_cleanup(&priv->sd);
3708 err_entity_cleanup:
3709 media_entity_cleanup(&priv->sd.entity);
3710 err_free_ctrl:
3711 v4l2_ctrl_handler_free(&priv->ctrl_handler);
3713 return ret;
3716 static void ub960_destroy_subdev(struct ub960_data *priv)
3718 ub960_v4l2_notifier_unregister(priv);
3719 v4l2_async_unregister_subdev(&priv->sd);
3721 v4l2_subdev_cleanup(&priv->sd);
3723 media_entity_cleanup(&priv->sd.entity);
3724 v4l2_ctrl_handler_free(&priv->ctrl_handler);
3727 static const struct regmap_config ub960_regmap_config = {
3728 .name = "ds90ub960",
3730 .reg_bits = 8,
3731 .val_bits = 8,
3733 .max_register = 0xff,
3736 * We do locking in the driver to cover the TX/RX port selection and the
3737 * indirect register access.
3739 .disable_locking = true,
3742 static void ub960_reset(struct ub960_data *priv, bool reset_regs)
3744 struct device *dev = &priv->client->dev;
3745 unsigned int v;
3746 int ret;
3747 u8 bit;
3749 bit = reset_regs ? UB960_SR_RESET_DIGITAL_RESET1 :
3750 UB960_SR_RESET_DIGITAL_RESET0;
3752 ub960_write(priv, UB960_SR_RESET, bit);
3754 mutex_lock(&priv->reg_lock);
3756 ret = regmap_read_poll_timeout(priv->regmap, UB960_SR_RESET, v,
3757 (v & bit) == 0, 2000, 100000);
3759 mutex_unlock(&priv->reg_lock);
3761 if (ret)
3762 dev_err(dev, "reset failed: %d\n", ret);
3765 static int ub960_get_hw_resources(struct ub960_data *priv)
3767 struct device *dev = &priv->client->dev;
3769 priv->regmap = devm_regmap_init_i2c(priv->client, &ub960_regmap_config);
3770 if (IS_ERR(priv->regmap))
3771 return PTR_ERR(priv->regmap);
3773 priv->vddio = devm_regulator_get(dev, "vddio");
3774 if (IS_ERR(priv->vddio))
3775 return dev_err_probe(dev, PTR_ERR(priv->vddio),
3776 "cannot get VDDIO regulator\n");
3778 /* get power-down pin from DT */
3779 priv->pd_gpio =
3780 devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
3781 if (IS_ERR(priv->pd_gpio))
3782 return dev_err_probe(dev, PTR_ERR(priv->pd_gpio),
3783 "Cannot get powerdown GPIO\n");
3785 priv->refclk = devm_clk_get(dev, "refclk");
3786 if (IS_ERR(priv->refclk))
3787 return dev_err_probe(dev, PTR_ERR(priv->refclk),
3788 "Cannot get REFCLK\n");
3790 return 0;
3793 static int ub960_enable_core_hw(struct ub960_data *priv)
3795 struct device *dev = &priv->client->dev;
3796 u8 rev_mask;
3797 int ret;
3798 u8 dev_sts;
3799 u8 refclk_freq;
3801 ret = regulator_enable(priv->vddio);
3802 if (ret)
3803 return dev_err_probe(dev, ret,
3804 "failed to enable VDDIO regulator\n");
3806 ret = clk_prepare_enable(priv->refclk);
3807 if (ret) {
3808 dev_err_probe(dev, ret, "Failed to enable refclk\n");
3809 goto err_disable_vddio;
3812 if (priv->pd_gpio) {
3813 gpiod_set_value_cansleep(priv->pd_gpio, 1);
3814 /* wait min 2 ms for reset to complete */
3815 fsleep(2000);
3816 gpiod_set_value_cansleep(priv->pd_gpio, 0);
3817 /* wait min 2 ms for power up to finish */
3818 fsleep(2000);
3821 ub960_reset(priv, true);
3823 /* Runtime check register accessibility */
3824 ret = ub960_read(priv, UB960_SR_REV_MASK, &rev_mask);
3825 if (ret) {
3826 dev_err_probe(dev, ret, "Cannot read first register, abort\n");
3827 goto err_pd_gpio;
3830 dev_dbg(dev, "Found %s (rev/mask %#04x)\n", priv->hw_data->model,
3831 rev_mask);
3833 ret = ub960_read(priv, UB960_SR_DEVICE_STS, &dev_sts);
3834 if (ret)
3835 goto err_pd_gpio;
3837 ret = ub960_read(priv, UB960_XR_REFCLK_FREQ, &refclk_freq);
3838 if (ret)
3839 goto err_pd_gpio;
3841 dev_dbg(dev, "refclk valid %u freq %u MHz (clk fw freq %lu MHz)\n",
3842 !!(dev_sts & BIT(4)), refclk_freq,
3843 clk_get_rate(priv->refclk) / 1000000);
3845 /* Disable all RX ports by default */
3846 ret = ub960_write(priv, UB960_SR_RX_PORT_CTL, 0);
3847 if (ret)
3848 goto err_pd_gpio;
3850 /* release GPIO lock */
3851 if (priv->hw_data->is_ub9702) {
3852 ret = ub960_update_bits(priv, UB960_SR_RESET,
3853 UB960_SR_RESET_GPIO_LOCK_RELEASE,
3854 UB960_SR_RESET_GPIO_LOCK_RELEASE);
3855 if (ret)
3856 goto err_pd_gpio;
3859 return 0;
3861 err_pd_gpio:
3862 gpiod_set_value_cansleep(priv->pd_gpio, 1);
3863 clk_disable_unprepare(priv->refclk);
3864 err_disable_vddio:
3865 regulator_disable(priv->vddio);
3867 return ret;
3870 static void ub960_disable_core_hw(struct ub960_data *priv)
3872 gpiod_set_value_cansleep(priv->pd_gpio, 1);
3873 clk_disable_unprepare(priv->refclk);
3874 regulator_disable(priv->vddio);
3877 static int ub960_probe(struct i2c_client *client)
3879 struct device *dev = &client->dev;
3880 struct ub960_data *priv;
3881 unsigned int port_lock_mask;
3882 unsigned int port_mask;
3883 unsigned int nport;
3884 int ret;
3886 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
3887 if (!priv)
3888 return -ENOMEM;
3890 priv->client = client;
3892 priv->hw_data = device_get_match_data(dev);
3894 mutex_init(&priv->reg_lock);
3896 INIT_DELAYED_WORK(&priv->poll_work, ub960_handler_work);
3899 * Initialize these to invalid values so that the first reg writes will
3900 * configure the target.
3902 priv->reg_current.indirect_target = 0xff;
3903 priv->reg_current.rxport = 0xff;
3904 priv->reg_current.txport = 0xff;
3906 ret = ub960_get_hw_resources(priv);
3907 if (ret)
3908 goto err_mutex_destroy;
3910 ret = ub960_enable_core_hw(priv);
3911 if (ret)
3912 goto err_mutex_destroy;
3914 ret = ub960_parse_dt(priv);
3915 if (ret)
3916 goto err_disable_core_hw;
3918 ret = ub960_init_tx_ports(priv);
3919 if (ret)
3920 goto err_free_ports;
3922 ret = ub960_rxport_enable_vpocs(priv);
3923 if (ret)
3924 goto err_free_ports;
3926 ret = ub960_init_rx_ports(priv);
3927 if (ret)
3928 goto err_disable_vpocs;
3930 ub960_reset(priv, false);
3932 port_mask = 0;
3934 for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
3935 struct ub960_rxport *rxport = priv->rxports[nport];
3937 if (!rxport)
3938 continue;
3940 port_mask |= BIT(nport);
3943 ret = ub960_rxport_wait_locks(priv, port_mask, &port_lock_mask);
3944 if (ret)
3945 goto err_disable_vpocs;
3947 if (port_mask != port_lock_mask) {
3948 ret = -EIO;
3949 dev_err_probe(dev, ret, "Failed to lock all RX ports\n");
3950 goto err_disable_vpocs;
3954 * Clear any errors caused by switching the RX port settings while
3955 * probing.
3957 ub960_clear_rx_errors(priv);
3959 ret = ub960_init_atr(priv);
3960 if (ret)
3961 goto err_disable_vpocs;
3963 ret = ub960_rxport_add_serializers(priv);
3964 if (ret)
3965 goto err_uninit_atr;
3967 ret = ub960_create_subdev(priv);
3968 if (ret)
3969 goto err_free_sers;
3971 if (client->irq)
3972 dev_warn(dev, "irq support not implemented, using polling\n");
3974 schedule_delayed_work(&priv->poll_work,
3975 msecs_to_jiffies(UB960_POLL_TIME_MS));
3977 return 0;
3979 err_free_sers:
3980 ub960_rxport_remove_serializers(priv);
3981 err_uninit_atr:
3982 ub960_uninit_atr(priv);
3983 err_disable_vpocs:
3984 ub960_rxport_disable_vpocs(priv);
3985 err_free_ports:
3986 ub960_rxport_free_ports(priv);
3987 ub960_txport_free_ports(priv);
3988 err_disable_core_hw:
3989 ub960_disable_core_hw(priv);
3990 err_mutex_destroy:
3991 mutex_destroy(&priv->reg_lock);
3992 return ret;
3995 static void ub960_remove(struct i2c_client *client)
3997 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3998 struct ub960_data *priv = sd_to_ub960(sd);
4000 cancel_delayed_work_sync(&priv->poll_work);
4002 ub960_destroy_subdev(priv);
4003 ub960_rxport_remove_serializers(priv);
4004 ub960_uninit_atr(priv);
4005 ub960_rxport_disable_vpocs(priv);
4006 ub960_rxport_free_ports(priv);
4007 ub960_txport_free_ports(priv);
4008 ub960_disable_core_hw(priv);
4009 mutex_destroy(&priv->reg_lock);
4012 static const struct ub960_hw_data ds90ub960_hw = {
4013 .model = "ub960",
4014 .num_rxports = 4,
4015 .num_txports = 2,
4018 static const struct ub960_hw_data ds90ub9702_hw = {
4019 .model = "ub9702",
4020 .num_rxports = 4,
4021 .num_txports = 2,
4022 .is_ub9702 = true,
4023 .is_fpdlink4 = true,
4026 static const struct i2c_device_id ub960_id[] = {
4027 { "ds90ub960-q1", (kernel_ulong_t)&ds90ub960_hw },
4028 { "ds90ub9702-q1", (kernel_ulong_t)&ds90ub9702_hw },
4031 MODULE_DEVICE_TABLE(i2c, ub960_id);
4033 static const struct of_device_id ub960_dt_ids[] = {
4034 { .compatible = "ti,ds90ub960-q1", .data = &ds90ub960_hw },
4035 { .compatible = "ti,ds90ub9702-q1", .data = &ds90ub9702_hw },
4038 MODULE_DEVICE_TABLE(of, ub960_dt_ids);
4040 static struct i2c_driver ds90ub960_driver = {
4041 .probe = ub960_probe,
4042 .remove = ub960_remove,
4043 .id_table = ub960_id,
4044 .driver = {
4045 .name = "ds90ub960",
4046 .of_match_table = ub960_dt_ids,
4049 module_i2c_driver(ds90ub960_driver);
4051 MODULE_LICENSE("GPL");
4052 MODULE_DESCRIPTION("Texas Instruments FPD-Link III/IV Deserializers Driver");
4053 MODULE_AUTHOR("Luca Ceresoli <luca@lucaceresoli.net>");
4054 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>");
4055 MODULE_IMPORT_NS("I2C_ATR");