xref: /openbmc/linux/drivers/media/i2c/ds90ub960.c (revision 360823a09426347ea8f232b0b0b5156d0aed0302)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for the Texas Instruments DS90UB960-Q1 video deserializer
4  *
5  * Copyright (c) 2019 Luca Ceresoli <luca@lucaceresoli.net>
6  * Copyright (c) 2023 Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
7  */
8 
9 /*
10  * (Possible) TODOs:
11  *
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.
27  */
28 
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>
47 
48 #include <media/i2c/ds90ub9xx.h>
49 #include <media/mipi-csi2.h>
50 #include <media/v4l2-ctrls.h>
51 #include <media/v4l2-event.h>
52 #include <media/v4l2-fwnode.h>
53 #include <media/v4l2-subdev.h>
54 
55 #define MHZ(v) ((u32)((v) * 1000000U))
56 
57 #define UB960_POLL_TIME_MS	500
58 
59 #define UB960_MAX_RX_NPORTS	4
60 #define UB960_MAX_TX_NPORTS	2
61 #define UB960_MAX_NPORTS	(UB960_MAX_RX_NPORTS + UB960_MAX_TX_NPORTS)
62 
63 #define UB960_MAX_PORT_ALIASES	8
64 
65 #define UB960_NUM_BC_GPIOS		4
66 
67 /*
68  * Register map
69  *
70  * 0x00-0x32   Shared (UB960_SR)
71  * 0x33-0x3a   CSI-2 TX (per-port paged on DS90UB960, shared on 954) (UB960_TR)
72  * 0x4c        Shared (UB960_SR)
73  * 0x4d-0x7f   FPD-Link RX, per-port paged (UB960_RR)
74  * 0xb0-0xbf   Shared (UB960_SR)
75  * 0xd0-0xdf   FPD-Link RX, per-port paged (UB960_RR)
76  * 0xf0-0xf5   Shared (UB960_SR)
77  * 0xf8-0xfb   Shared (UB960_SR)
78  * All others  Reserved
79  *
80  * Register prefixes:
81  * UB960_SR_* = Shared register
82  * UB960_RR_* = FPD-Link RX, per-port paged register
83  * UB960_TR_* = CSI-2 TX, per-port paged register
84  * UB960_XR_* = Reserved register
85  * UB960_IR_* = Indirect register
86  */
87 
88 #define UB960_SR_I2C_DEV_ID			0x00
89 #define UB960_SR_RESET				0x01
90 #define UB960_SR_RESET_DIGITAL_RESET1		BIT(1)
91 #define UB960_SR_RESET_DIGITAL_RESET0		BIT(0)
92 #define UB960_SR_RESET_GPIO_LOCK_RELEASE	BIT(5)
93 
94 #define UB960_SR_GEN_CONFIG			0x02
95 #define UB960_SR_REV_MASK			0x03
96 #define UB960_SR_DEVICE_STS			0x04
97 #define UB960_SR_PAR_ERR_THOLD_HI		0x05
98 #define UB960_SR_PAR_ERR_THOLD_LO		0x06
99 #define UB960_SR_BCC_WDOG_CTL			0x07
100 #define UB960_SR_I2C_CTL1			0x08
101 #define UB960_SR_I2C_CTL2			0x09
102 #define UB960_SR_SCL_HIGH_TIME			0x0a
103 #define UB960_SR_SCL_LOW_TIME			0x0b
104 #define UB960_SR_RX_PORT_CTL			0x0c
105 #define UB960_SR_IO_CTL				0x0d
106 #define UB960_SR_GPIO_PIN_STS			0x0e
107 #define UB960_SR_GPIO_INPUT_CTL			0x0f
108 #define UB960_SR_GPIO_PIN_CTL(n)		(0x10 + (n)) /* n < UB960_NUM_GPIOS */
109 #define UB960_SR_GPIO_PIN_CTL_GPIO_OUT_SEL		5
110 #define UB960_SR_GPIO_PIN_CTL_GPIO_OUT_SRC_SHIFT	2
111 #define UB960_SR_GPIO_PIN_CTL_GPIO_OUT_EN		BIT(0)
112 
113 #define UB960_SR_FS_CTL				0x18
114 #define UB960_SR_FS_HIGH_TIME_1			0x19
115 #define UB960_SR_FS_HIGH_TIME_0			0x1a
116 #define UB960_SR_FS_LOW_TIME_1			0x1b
117 #define UB960_SR_FS_LOW_TIME_0			0x1c
118 #define UB960_SR_MAX_FRM_HI			0x1d
119 #define UB960_SR_MAX_FRM_LO			0x1e
120 #define UB960_SR_CSI_PLL_CTL			0x1f
121 
122 #define UB960_SR_FWD_CTL1			0x20
123 #define UB960_SR_FWD_CTL1_PORT_DIS(n)		BIT((n) + 4)
124 
125 #define UB960_SR_FWD_CTL2			0x21
126 #define UB960_SR_FWD_STS			0x22
127 
128 #define UB960_SR_INTERRUPT_CTL			0x23
129 #define UB960_SR_INTERRUPT_CTL_INT_EN		BIT(7)
130 #define UB960_SR_INTERRUPT_CTL_IE_CSI_TX0	BIT(4)
131 #define UB960_SR_INTERRUPT_CTL_IE_RX(n)		BIT((n)) /* rxport[n] IRQ */
132 
133 #define UB960_SR_INTERRUPT_STS			0x24
134 #define UB960_SR_INTERRUPT_STS_INT		BIT(7)
135 #define UB960_SR_INTERRUPT_STS_IS_CSI_TX(n)	BIT(4 + (n)) /* txport[n] IRQ */
136 #define UB960_SR_INTERRUPT_STS_IS_RX(n)		BIT((n)) /* rxport[n] IRQ */
137 
138 #define UB960_SR_TS_CONFIG			0x25
139 #define UB960_SR_TS_CONTROL			0x26
140 #define UB960_SR_TS_LINE_HI			0x27
141 #define UB960_SR_TS_LINE_LO			0x28
142 #define UB960_SR_TS_STATUS			0x29
143 #define UB960_SR_TIMESTAMP_P0_HI		0x2a
144 #define UB960_SR_TIMESTAMP_P0_LO		0x2b
145 #define UB960_SR_TIMESTAMP_P1_HI		0x2c
146 #define UB960_SR_TIMESTAMP_P1_LO		0x2d
147 
148 #define UB960_SR_CSI_PORT_SEL			0x32
149 
150 #define UB960_TR_CSI_CTL			0x33
151 #define UB960_TR_CSI_CTL_CSI_CAL_EN		BIT(6)
152 #define UB960_TR_CSI_CTL_CSI_CONTS_CLOCK	BIT(1)
153 #define UB960_TR_CSI_CTL_CSI_ENABLE		BIT(0)
154 
155 #define UB960_TR_CSI_CTL2			0x34
156 #define UB960_TR_CSI_STS			0x35
157 #define UB960_TR_CSI_TX_ICR			0x36
158 
159 #define UB960_TR_CSI_TX_ISR			0x37
160 #define UB960_TR_CSI_TX_ISR_IS_CSI_SYNC_ERROR	BIT(3)
161 #define UB960_TR_CSI_TX_ISR_IS_CSI_PASS_ERROR	BIT(1)
162 
163 #define UB960_TR_CSI_TEST_CTL			0x38
164 #define UB960_TR_CSI_TEST_PATT_HI		0x39
165 #define UB960_TR_CSI_TEST_PATT_LO		0x3a
166 
167 #define UB960_XR_SFILTER_CFG			0x41
168 #define UB960_XR_SFILTER_CFG_SFILTER_MAX_SHIFT	4
169 #define UB960_XR_SFILTER_CFG_SFILTER_MIN_SHIFT	0
170 
171 #define UB960_XR_AEQ_CTL1			0x42
172 #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_FPD_CLK	BIT(6)
173 #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_ENCODING	BIT(5)
174 #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_PARITY	BIT(4)
175 #define UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_MASK        \
176 	(UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_FPD_CLK |  \
177 	 UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_ENCODING | \
178 	 UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_PARITY)
179 #define UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN	BIT(0)
180 
181 #define UB960_XR_AEQ_ERR_THOLD			0x43
182 
183 #define UB960_RR_BCC_ERR_CTL			0x46
184 #define UB960_RR_BCC_STATUS			0x47
185 #define UB960_RR_BCC_STATUS_SEQ_ERROR		BIT(5)
186 #define UB960_RR_BCC_STATUS_MASTER_ERR		BIT(4)
187 #define UB960_RR_BCC_STATUS_MASTER_TO		BIT(3)
188 #define UB960_RR_BCC_STATUS_SLAVE_ERR		BIT(2)
189 #define UB960_RR_BCC_STATUS_SLAVE_TO		BIT(1)
190 #define UB960_RR_BCC_STATUS_RESP_ERR		BIT(0)
191 #define UB960_RR_BCC_STATUS_ERROR_MASK                                    \
192 	(UB960_RR_BCC_STATUS_SEQ_ERROR | UB960_RR_BCC_STATUS_MASTER_ERR | \
193 	 UB960_RR_BCC_STATUS_MASTER_TO | UB960_RR_BCC_STATUS_SLAVE_ERR |  \
194 	 UB960_RR_BCC_STATUS_SLAVE_TO | UB960_RR_BCC_STATUS_RESP_ERR)
195 
196 #define UB960_RR_FPD3_CAP			0x4a
197 #define UB960_RR_RAW_EMBED_DTYPE		0x4b
198 #define UB960_RR_RAW_EMBED_DTYPE_LINES_SHIFT	6
199 
200 #define UB960_SR_FPD3_PORT_SEL			0x4c
201 
202 #define UB960_RR_RX_PORT_STS1			0x4d
203 #define UB960_RR_RX_PORT_STS1_BCC_CRC_ERROR	BIT(5)
204 #define UB960_RR_RX_PORT_STS1_LOCK_STS_CHG	BIT(4)
205 #define UB960_RR_RX_PORT_STS1_BCC_SEQ_ERROR	BIT(3)
206 #define UB960_RR_RX_PORT_STS1_PARITY_ERROR	BIT(2)
207 #define UB960_RR_RX_PORT_STS1_PORT_PASS		BIT(1)
208 #define UB960_RR_RX_PORT_STS1_LOCK_STS		BIT(0)
209 #define UB960_RR_RX_PORT_STS1_ERROR_MASK       \
210 	(UB960_RR_RX_PORT_STS1_BCC_CRC_ERROR | \
211 	 UB960_RR_RX_PORT_STS1_BCC_SEQ_ERROR | \
212 	 UB960_RR_RX_PORT_STS1_PARITY_ERROR)
213 
214 #define UB960_RR_RX_PORT_STS2			0x4e
215 #define UB960_RR_RX_PORT_STS2_LINE_LEN_UNSTABLE	BIT(7)
216 #define UB960_RR_RX_PORT_STS2_LINE_LEN_CHG	BIT(6)
217 #define UB960_RR_RX_PORT_STS2_FPD3_ENCODE_ERROR	BIT(5)
218 #define UB960_RR_RX_PORT_STS2_BUFFER_ERROR	BIT(4)
219 #define UB960_RR_RX_PORT_STS2_CSI_ERROR		BIT(3)
220 #define UB960_RR_RX_PORT_STS2_FREQ_STABLE	BIT(2)
221 #define UB960_RR_RX_PORT_STS2_CABLE_FAULT	BIT(1)
222 #define UB960_RR_RX_PORT_STS2_LINE_CNT_CHG	BIT(0)
223 #define UB960_RR_RX_PORT_STS2_ERROR_MASK       \
224 	UB960_RR_RX_PORT_STS2_BUFFER_ERROR
225 
226 #define UB960_RR_RX_FREQ_HIGH			0x4f
227 #define UB960_RR_RX_FREQ_LOW			0x50
228 #define UB960_RR_SENSOR_STS_0			0x51
229 #define UB960_RR_SENSOR_STS_1			0x52
230 #define UB960_RR_SENSOR_STS_2			0x53
231 #define UB960_RR_SENSOR_STS_3			0x54
232 #define UB960_RR_RX_PAR_ERR_HI			0x55
233 #define UB960_RR_RX_PAR_ERR_LO			0x56
234 #define UB960_RR_BIST_ERR_COUNT			0x57
235 
236 #define UB960_RR_BCC_CONFIG			0x58
237 #define UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH	BIT(6)
238 #define UB960_RR_BCC_CONFIG_BC_FREQ_SEL_MASK	GENMASK(2, 0)
239 
240 #define UB960_RR_DATAPATH_CTL1			0x59
241 #define UB960_RR_DATAPATH_CTL2			0x5a
242 #define UB960_RR_SER_ID				0x5b
243 #define UB960_RR_SER_ALIAS_ID			0x5c
244 
245 /* For these two register sets: n < UB960_MAX_PORT_ALIASES */
246 #define UB960_RR_SLAVE_ID(n)			(0x5d + (n))
247 #define UB960_RR_SLAVE_ALIAS(n)			(0x65 + (n))
248 
249 #define UB960_RR_PORT_CONFIG			0x6d
250 #define UB960_RR_PORT_CONFIG_FPD3_MODE_MASK	GENMASK(1, 0)
251 
252 #define UB960_RR_BC_GPIO_CTL(n)			(0x6e + (n)) /* n < 2 */
253 #define UB960_RR_RAW10_ID			0x70
254 #define UB960_RR_RAW10_ID_VC_SHIFT		6
255 #define UB960_RR_RAW10_ID_DT_SHIFT		0
256 
257 #define UB960_RR_RAW12_ID			0x71
258 #define UB960_RR_CSI_VC_MAP			0x72
259 #define UB960_RR_CSI_VC_MAP_SHIFT(x)		((x) * 2)
260 
261 #define UB960_RR_LINE_COUNT_HI			0x73
262 #define UB960_RR_LINE_COUNT_LO			0x74
263 #define UB960_RR_LINE_LEN_1			0x75
264 #define UB960_RR_LINE_LEN_0			0x76
265 #define UB960_RR_FREQ_DET_CTL			0x77
266 #define UB960_RR_MAILBOX_1			0x78
267 #define UB960_RR_MAILBOX_2			0x79
268 
269 #define UB960_RR_CSI_RX_STS			0x7a
270 #define UB960_RR_CSI_RX_STS_LENGTH_ERR		BIT(3)
271 #define UB960_RR_CSI_RX_STS_CKSUM_ERR		BIT(2)
272 #define UB960_RR_CSI_RX_STS_ECC2_ERR		BIT(1)
273 #define UB960_RR_CSI_RX_STS_ECC1_ERR		BIT(0)
274 #define UB960_RR_CSI_RX_STS_ERROR_MASK                                    \
275 	(UB960_RR_CSI_RX_STS_LENGTH_ERR | UB960_RR_CSI_RX_STS_CKSUM_ERR | \
276 	 UB960_RR_CSI_RX_STS_ECC2_ERR | UB960_RR_CSI_RX_STS_ECC1_ERR)
277 
278 #define UB960_RR_CSI_ERR_COUNTER		0x7b
279 #define UB960_RR_PORT_CONFIG2			0x7c
280 #define UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_MASK GENMASK(7, 6)
281 #define UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_SHIFT 6
282 
283 #define UB960_RR_PORT_CONFIG2_LV_POL_LOW	BIT(1)
284 #define UB960_RR_PORT_CONFIG2_FV_POL_LOW	BIT(0)
285 
286 #define UB960_RR_PORT_PASS_CTL			0x7d
287 #define UB960_RR_SEN_INT_RISE_CTL		0x7e
288 #define UB960_RR_SEN_INT_FALL_CTL		0x7f
289 
290 #define UB960_SR_CSI_FRAME_COUNT_HI(n)		(0x90 + 8 * (n))
291 #define UB960_SR_CSI_FRAME_COUNT_LO(n)		(0x91 + 8 * (n))
292 #define UB960_SR_CSI_FRAME_ERR_COUNT_HI(n)	(0x92 + 8 * (n))
293 #define UB960_SR_CSI_FRAME_ERR_COUNT_LO(n)	(0x93 + 8 * (n))
294 #define UB960_SR_CSI_LINE_COUNT_HI(n)		(0x94 + 8 * (n))
295 #define UB960_SR_CSI_LINE_COUNT_LO(n)		(0x95 + 8 * (n))
296 #define UB960_SR_CSI_LINE_ERR_COUNT_HI(n)	(0x96 + 8 * (n))
297 #define UB960_SR_CSI_LINE_ERR_COUNT_LO(n)	(0x97 + 8 * (n))
298 
299 #define UB960_XR_REFCLK_FREQ			0xa5	/* UB960 */
300 
301 #define UB960_RR_VC_ID_MAP(x)			(0xa0 + (x)) /* UB9702 */
302 
303 #define UB960_SR_IND_ACC_CTL			0xb0
304 #define UB960_SR_IND_ACC_CTL_IA_AUTO_INC	BIT(1)
305 
306 #define UB960_SR_IND_ACC_ADDR			0xb1
307 #define UB960_SR_IND_ACC_DATA			0xb2
308 #define UB960_SR_BIST_CONTROL			0xb3
309 #define UB960_SR_MODE_IDX_STS			0xb8
310 #define UB960_SR_LINK_ERROR_COUNT		0xb9
311 #define UB960_SR_FPD3_ENC_CTL			0xba
312 #define UB960_SR_FV_MIN_TIME			0xbc
313 #define UB960_SR_GPIO_PD_CTL			0xbe
314 
315 #define UB960_SR_FPD_RATE_CFG			0xc2	/* UB9702 */
316 #define UB960_SR_CSI_PLL_DIV			0xc9	/* UB9702 */
317 
318 #define UB960_RR_PORT_DEBUG			0xd0
319 #define UB960_RR_AEQ_CTL2			0xd2
320 #define UB960_RR_AEQ_CTL2_SET_AEQ_FLOOR		BIT(2)
321 
322 #define UB960_RR_AEQ_STATUS			0xd3
323 #define UB960_RR_AEQ_STATUS_STATUS_2		GENMASK(5, 3)
324 #define UB960_RR_AEQ_STATUS_STATUS_1		GENMASK(2, 0)
325 
326 #define UB960_RR_AEQ_BYPASS			0xd4
327 #define UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_SHIFT	5
328 #define UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_MASK	GENMASK(7, 5)
329 #define UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_SHIFT	1
330 #define UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_MASK	GENMASK(3, 1)
331 #define UB960_RR_AEQ_BYPASS_ENABLE			BIT(0)
332 
333 #define UB960_RR_AEQ_MIN_MAX			0xd5
334 #define UB960_RR_AEQ_MIN_MAX_AEQ_MAX_SHIFT	4
335 #define UB960_RR_AEQ_MIN_MAX_AEQ_FLOOR_SHIFT	0
336 
337 #define UB960_RR_SFILTER_STS_0			0xd6
338 #define UB960_RR_SFILTER_STS_1			0xd7
339 #define UB960_RR_PORT_ICR_HI			0xd8
340 #define UB960_RR_PORT_ICR_LO			0xd9
341 #define UB960_RR_PORT_ISR_HI			0xda
342 #define UB960_RR_PORT_ISR_LO			0xdb
343 #define UB960_RR_FC_GPIO_STS			0xdc
344 #define UB960_RR_FC_GPIO_ICR			0xdd
345 #define UB960_RR_SEN_INT_RISE_STS		0xde
346 #define UB960_RR_SEN_INT_FALL_STS		0xdf
347 
348 #define UB960_RR_CHANNEL_MODE			0xe4	/* UB9702 */
349 
350 #define UB960_SR_FPD3_RX_ID(n)			(0xf0 + (n))
351 #define UB960_SR_FPD3_RX_ID_LEN			6
352 
353 #define UB960_SR_I2C_RX_ID(n)			(0xf8 + (n)) /* < UB960_FPD_RX_NPORTS */
354 
355 #define UB9702_SR_REFCLK_FREQ			0x3d
356 
357 /* Indirect register blocks */
358 #define UB960_IND_TARGET_PAT_GEN		0x00
359 #define UB960_IND_TARGET_RX_ANA(n)		(0x01 + (n))
360 #define UB960_IND_TARGET_CSI_CSIPLL_REG_1	0x92	/* UB9702 */
361 #define UB960_IND_TARGET_CSI_ANA		0x07
362 
363 /* UB960_IR_PGEN_*: Indirect Registers for Test Pattern Generator */
364 
365 #define UB960_IR_PGEN_CTL			0x01
366 #define UB960_IR_PGEN_CTL_PGEN_ENABLE		BIT(0)
367 
368 #define UB960_IR_PGEN_CFG			0x02
369 #define UB960_IR_PGEN_CSI_DI			0x03
370 #define UB960_IR_PGEN_LINE_SIZE1		0x04
371 #define UB960_IR_PGEN_LINE_SIZE0		0x05
372 #define UB960_IR_PGEN_BAR_SIZE1			0x06
373 #define UB960_IR_PGEN_BAR_SIZE0			0x07
374 #define UB960_IR_PGEN_ACT_LPF1			0x08
375 #define UB960_IR_PGEN_ACT_LPF0			0x09
376 #define UB960_IR_PGEN_TOT_LPF1			0x0a
377 #define UB960_IR_PGEN_TOT_LPF0			0x0b
378 #define UB960_IR_PGEN_LINE_PD1			0x0c
379 #define UB960_IR_PGEN_LINE_PD0			0x0d
380 #define UB960_IR_PGEN_VBP			0x0e
381 #define UB960_IR_PGEN_VFP			0x0f
382 #define UB960_IR_PGEN_COLOR(n)			(0x10 + (n)) /* n < 15 */
383 
384 #define UB960_IR_RX_ANA_STROBE_SET_CLK		0x08
385 #define UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY	BIT(3)
386 #define UB960_IR_RX_ANA_STROBE_SET_CLK_DELAY_MASK	GENMASK(2, 0)
387 
388 #define UB960_IR_RX_ANA_STROBE_SET_DATA		0x09
389 #define UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY	BIT(3)
390 #define UB960_IR_RX_ANA_STROBE_SET_DATA_DELAY_MASK	GENMASK(2, 0)
391 
392 /* EQ related */
393 
394 #define UB960_MIN_AEQ_STROBE_POS -7
395 #define UB960_MAX_AEQ_STROBE_POS  7
396 
397 #define UB960_MANUAL_STROBE_EXTRA_DELAY 6
398 
399 #define UB960_MIN_MANUAL_STROBE_POS -(7 + UB960_MANUAL_STROBE_EXTRA_DELAY)
400 #define UB960_MAX_MANUAL_STROBE_POS  (7 + UB960_MANUAL_STROBE_EXTRA_DELAY)
401 #define UB960_NUM_MANUAL_STROBE_POS  (UB960_MAX_MANUAL_STROBE_POS - UB960_MIN_MANUAL_STROBE_POS + 1)
402 
403 #define UB960_MIN_EQ_LEVEL  0
404 #define UB960_MAX_EQ_LEVEL  14
405 #define UB960_NUM_EQ_LEVELS (UB960_MAX_EQ_LEVEL - UB960_MIN_EQ_LEVEL + 1)
406 
407 struct ub960_hw_data {
408 	const char *model;
409 	u8 num_rxports;
410 	u8 num_txports;
411 	bool is_ub9702;
412 	bool is_fpdlink4;
413 };
414 
415 enum ub960_rxport_mode {
416 	RXPORT_MODE_RAW10 = 0,
417 	RXPORT_MODE_RAW12_HF = 1,
418 	RXPORT_MODE_RAW12_LF = 2,
419 	RXPORT_MODE_CSI2_SYNC = 3,
420 	RXPORT_MODE_CSI2_NONSYNC = 4,
421 	RXPORT_MODE_LAST = RXPORT_MODE_CSI2_NONSYNC,
422 };
423 
424 enum ub960_rxport_cdr {
425 	RXPORT_CDR_FPD3 = 0,
426 	RXPORT_CDR_FPD4 = 1,
427 	RXPORT_CDR_LAST = RXPORT_CDR_FPD4,
428 };
429 
430 struct ub960_rxport {
431 	struct ub960_data      *priv;
432 	u8                      nport;	/* RX port number, and index in priv->rxport[] */
433 
434 	struct {
435 		struct v4l2_subdev *sd;
436 		u16 pad;
437 		struct fwnode_handle *ep_fwnode;
438 	} source;
439 
440 	/* Serializer */
441 	struct {
442 		struct fwnode_handle *fwnode;
443 		struct i2c_client *client;
444 		unsigned short alias; /* I2C alias (lower 7 bits) */
445 		struct ds90ub9xx_platform_data pdata;
446 	} ser;
447 
448 	enum ub960_rxport_mode  rx_mode;
449 	enum ub960_rxport_cdr	cdr_mode;
450 
451 	u8			lv_fv_pol;	/* LV and FV polarities */
452 
453 	struct regulator	*vpoc;
454 
455 	/* EQ settings */
456 	struct {
457 		bool manual_eq;
458 
459 		s8 strobe_pos;
460 
461 		union {
462 			struct {
463 				u8 eq_level_min;
464 				u8 eq_level_max;
465 			} aeq;
466 
467 			struct {
468 				u8 eq_level;
469 			} manual;
470 		};
471 	} eq;
472 
473 	const struct i2c_client *aliased_clients[UB960_MAX_PORT_ALIASES];
474 };
475 
476 struct ub960_asd {
477 	struct v4l2_async_connection base;
478 	struct ub960_rxport *rxport;
479 };
480 
to_ub960_asd(struct v4l2_async_connection * asd)481 static inline struct ub960_asd *to_ub960_asd(struct v4l2_async_connection *asd)
482 {
483 	return container_of(asd, struct ub960_asd, base);
484 }
485 
486 struct ub960_txport {
487 	struct ub960_data      *priv;
488 	u8                      nport;	/* TX port number, and index in priv->txport[] */
489 
490 	u32 num_data_lanes;
491 	bool non_continous_clk;
492 };
493 
494 struct ub960_data {
495 	const struct ub960_hw_data	*hw_data;
496 	struct i2c_client	*client; /* for shared local registers */
497 	struct regmap		*regmap;
498 
499 	/* lock for register access */
500 	struct mutex		reg_lock;
501 
502 	struct clk		*refclk;
503 
504 	struct regulator	*vddio;
505 
506 	struct gpio_desc	*pd_gpio;
507 	struct delayed_work	poll_work;
508 	struct ub960_rxport	*rxports[UB960_MAX_RX_NPORTS];
509 	struct ub960_txport	*txports[UB960_MAX_TX_NPORTS];
510 
511 	struct v4l2_subdev	sd;
512 	struct media_pad	pads[UB960_MAX_NPORTS];
513 
514 	struct v4l2_ctrl_handler   ctrl_handler;
515 	struct v4l2_async_notifier notifier;
516 
517 	u32 tx_data_rate;		/* Nominal data rate (Gb/s) */
518 	s64 tx_link_freq[1];
519 
520 	struct i2c_atr *atr;
521 
522 	struct {
523 		u8 rxport;
524 		u8 txport;
525 		u8 indirect_target;
526 	} reg_current;
527 
528 	bool streaming;
529 
530 	u8 stored_fwd_ctl;
531 
532 	u64 stream_enable_mask[UB960_MAX_NPORTS];
533 
534 	/* These are common to all ports */
535 	struct {
536 		bool manual;
537 
538 		s8 min;
539 		s8 max;
540 	} strobe;
541 };
542 
sd_to_ub960(struct v4l2_subdev * sd)543 static inline struct ub960_data *sd_to_ub960(struct v4l2_subdev *sd)
544 {
545 	return container_of(sd, struct ub960_data, sd);
546 }
547 
ub960_pad_is_sink(struct ub960_data * priv,u32 pad)548 static inline bool ub960_pad_is_sink(struct ub960_data *priv, u32 pad)
549 {
550 	return pad < priv->hw_data->num_rxports;
551 }
552 
ub960_pad_is_source(struct ub960_data * priv,u32 pad)553 static inline bool ub960_pad_is_source(struct ub960_data *priv, u32 pad)
554 {
555 	return pad >= priv->hw_data->num_rxports;
556 }
557 
ub960_pad_to_port(struct ub960_data * priv,u32 pad)558 static inline unsigned int ub960_pad_to_port(struct ub960_data *priv, u32 pad)
559 {
560 	if (ub960_pad_is_sink(priv, pad))
561 		return pad;
562 	else
563 		return pad - priv->hw_data->num_rxports;
564 }
565 
566 struct ub960_format_info {
567 	u32 code;
568 	u32 bpp;
569 	u8 datatype;
570 	bool meta;
571 };
572 
573 static const struct ub960_format_info ub960_formats[] = {
574 	{ .code = MEDIA_BUS_FMT_YUYV8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
575 	{ .code = MEDIA_BUS_FMT_UYVY8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
576 	{ .code = MEDIA_BUS_FMT_VYUY8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
577 	{ .code = MEDIA_BUS_FMT_YVYU8_1X16, .bpp = 16, .datatype = MIPI_CSI2_DT_YUV422_8B, },
578 
579 	{ .code = MEDIA_BUS_FMT_SBGGR12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
580 	{ .code = MEDIA_BUS_FMT_SGBRG12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
581 	{ .code = MEDIA_BUS_FMT_SGRBG12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
582 	{ .code = MEDIA_BUS_FMT_SRGGB12_1X12, .bpp = 12, .datatype = MIPI_CSI2_DT_RAW12, },
583 };
584 
ub960_find_format(u32 code)585 static const struct ub960_format_info *ub960_find_format(u32 code)
586 {
587 	unsigned int i;
588 
589 	for (i = 0; i < ARRAY_SIZE(ub960_formats); i++) {
590 		if (ub960_formats[i].code == code)
591 			return &ub960_formats[i];
592 	}
593 
594 	return NULL;
595 }
596 
597 /* -----------------------------------------------------------------------------
598  * Basic device access
599  */
600 
ub960_read(struct ub960_data * priv,u8 reg,u8 * val)601 static int ub960_read(struct ub960_data *priv, u8 reg, u8 *val)
602 {
603 	struct device *dev = &priv->client->dev;
604 	unsigned int v;
605 	int ret;
606 
607 	mutex_lock(&priv->reg_lock);
608 
609 	ret = regmap_read(priv->regmap, reg, &v);
610 	if (ret) {
611 		dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
612 			__func__, reg, ret);
613 		goto out_unlock;
614 	}
615 
616 	*val = v;
617 
618 out_unlock:
619 	mutex_unlock(&priv->reg_lock);
620 
621 	return ret;
622 }
623 
ub960_write(struct ub960_data * priv,u8 reg,u8 val)624 static int ub960_write(struct ub960_data *priv, u8 reg, u8 val)
625 {
626 	struct device *dev = &priv->client->dev;
627 	int ret;
628 
629 	mutex_lock(&priv->reg_lock);
630 
631 	ret = regmap_write(priv->regmap, reg, val);
632 	if (ret)
633 		dev_err(dev, "%s: cannot write register 0x%02x (%d)!\n",
634 			__func__, reg, ret);
635 
636 	mutex_unlock(&priv->reg_lock);
637 
638 	return ret;
639 }
640 
ub960_update_bits(struct ub960_data * priv,u8 reg,u8 mask,u8 val)641 static int ub960_update_bits(struct ub960_data *priv, u8 reg, u8 mask, u8 val)
642 {
643 	struct device *dev = &priv->client->dev;
644 	int ret;
645 
646 	mutex_lock(&priv->reg_lock);
647 
648 	ret = regmap_update_bits(priv->regmap, reg, mask, val);
649 	if (ret)
650 		dev_err(dev, "%s: cannot update register 0x%02x (%d)!\n",
651 			__func__, reg, ret);
652 
653 	mutex_unlock(&priv->reg_lock);
654 
655 	return ret;
656 }
657 
ub960_read16(struct ub960_data * priv,u8 reg,u16 * val)658 static int ub960_read16(struct ub960_data *priv, u8 reg, u16 *val)
659 {
660 	struct device *dev = &priv->client->dev;
661 	__be16 __v;
662 	int ret;
663 
664 	mutex_lock(&priv->reg_lock);
665 
666 	ret = regmap_bulk_read(priv->regmap, reg, &__v, sizeof(__v));
667 	if (ret) {
668 		dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
669 			__func__, reg, ret);
670 		goto out_unlock;
671 	}
672 
673 	*val = be16_to_cpu(__v);
674 
675 out_unlock:
676 	mutex_unlock(&priv->reg_lock);
677 
678 	return ret;
679 }
680 
ub960_rxport_select(struct ub960_data * priv,u8 nport)681 static int ub960_rxport_select(struct ub960_data *priv, u8 nport)
682 {
683 	struct device *dev = &priv->client->dev;
684 	int ret;
685 
686 	lockdep_assert_held(&priv->reg_lock);
687 
688 	if (priv->reg_current.rxport == nport)
689 		return 0;
690 
691 	ret = regmap_write(priv->regmap, UB960_SR_FPD3_PORT_SEL,
692 			   (nport << 4) | BIT(nport));
693 	if (ret) {
694 		dev_err(dev, "%s: cannot select rxport %d (%d)!\n", __func__,
695 			nport, ret);
696 		return ret;
697 	}
698 
699 	priv->reg_current.rxport = nport;
700 
701 	return 0;
702 }
703 
ub960_rxport_read(struct ub960_data * priv,u8 nport,u8 reg,u8 * val)704 static int ub960_rxport_read(struct ub960_data *priv, u8 nport, u8 reg, u8 *val)
705 {
706 	struct device *dev = &priv->client->dev;
707 	unsigned int v;
708 	int ret;
709 
710 	mutex_lock(&priv->reg_lock);
711 
712 	ret = ub960_rxport_select(priv, nport);
713 	if (ret)
714 		goto out_unlock;
715 
716 	ret = regmap_read(priv->regmap, reg, &v);
717 	if (ret) {
718 		dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
719 			__func__, reg, ret);
720 		goto out_unlock;
721 	}
722 
723 	*val = v;
724 
725 out_unlock:
726 	mutex_unlock(&priv->reg_lock);
727 
728 	return ret;
729 }
730 
ub960_rxport_write(struct ub960_data * priv,u8 nport,u8 reg,u8 val)731 static int ub960_rxport_write(struct ub960_data *priv, u8 nport, u8 reg, u8 val)
732 {
733 	struct device *dev = &priv->client->dev;
734 	int ret;
735 
736 	mutex_lock(&priv->reg_lock);
737 
738 	ret = ub960_rxport_select(priv, nport);
739 	if (ret)
740 		goto out_unlock;
741 
742 	ret = regmap_write(priv->regmap, reg, val);
743 	if (ret)
744 		dev_err(dev, "%s: cannot write register 0x%02x (%d)!\n",
745 			__func__, reg, ret);
746 
747 out_unlock:
748 	mutex_unlock(&priv->reg_lock);
749 
750 	return ret;
751 }
752 
ub960_rxport_update_bits(struct ub960_data * priv,u8 nport,u8 reg,u8 mask,u8 val)753 static int ub960_rxport_update_bits(struct ub960_data *priv, u8 nport, u8 reg,
754 				    u8 mask, u8 val)
755 {
756 	struct device *dev = &priv->client->dev;
757 	int ret;
758 
759 	mutex_lock(&priv->reg_lock);
760 
761 	ret = ub960_rxport_select(priv, nport);
762 	if (ret)
763 		goto out_unlock;
764 
765 	ret = regmap_update_bits(priv->regmap, reg, mask, val);
766 	if (ret)
767 		dev_err(dev, "%s: cannot update register 0x%02x (%d)!\n",
768 			__func__, reg, ret);
769 
770 out_unlock:
771 	mutex_unlock(&priv->reg_lock);
772 
773 	return ret;
774 }
775 
ub960_rxport_read16(struct ub960_data * priv,u8 nport,u8 reg,u16 * val)776 static int ub960_rxport_read16(struct ub960_data *priv, u8 nport, u8 reg,
777 			       u16 *val)
778 {
779 	struct device *dev = &priv->client->dev;
780 	__be16 __v;
781 	int ret;
782 
783 	mutex_lock(&priv->reg_lock);
784 
785 	ret = ub960_rxport_select(priv, nport);
786 	if (ret)
787 		goto out_unlock;
788 
789 	ret = regmap_bulk_read(priv->regmap, reg, &__v, sizeof(__v));
790 	if (ret) {
791 		dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
792 			__func__, reg, ret);
793 		goto out_unlock;
794 	}
795 
796 	*val = be16_to_cpu(__v);
797 
798 out_unlock:
799 	mutex_unlock(&priv->reg_lock);
800 
801 	return ret;
802 }
803 
ub960_txport_select(struct ub960_data * priv,u8 nport)804 static int ub960_txport_select(struct ub960_data *priv, u8 nport)
805 {
806 	struct device *dev = &priv->client->dev;
807 	int ret;
808 
809 	lockdep_assert_held(&priv->reg_lock);
810 
811 	if (priv->reg_current.txport == nport)
812 		return 0;
813 
814 	ret = regmap_write(priv->regmap, UB960_SR_CSI_PORT_SEL,
815 			   (nport << 4) | BIT(nport));
816 	if (ret) {
817 		dev_err(dev, "%s: cannot select tx port %d (%d)!\n", __func__,
818 			nport, ret);
819 		return ret;
820 	}
821 
822 	priv->reg_current.txport = nport;
823 
824 	return 0;
825 }
826 
ub960_txport_read(struct ub960_data * priv,u8 nport,u8 reg,u8 * val)827 static int ub960_txport_read(struct ub960_data *priv, u8 nport, u8 reg, u8 *val)
828 {
829 	struct device *dev = &priv->client->dev;
830 	unsigned int v;
831 	int ret;
832 
833 	mutex_lock(&priv->reg_lock);
834 
835 	ret = ub960_txport_select(priv, nport);
836 	if (ret)
837 		goto out_unlock;
838 
839 	ret = regmap_read(priv->regmap, reg, &v);
840 	if (ret) {
841 		dev_err(dev, "%s: cannot read register 0x%02x (%d)!\n",
842 			__func__, reg, ret);
843 		goto out_unlock;
844 	}
845 
846 	*val = v;
847 
848 out_unlock:
849 	mutex_unlock(&priv->reg_lock);
850 
851 	return ret;
852 }
853 
ub960_txport_write(struct ub960_data * priv,u8 nport,u8 reg,u8 val)854 static int ub960_txport_write(struct ub960_data *priv, u8 nport, u8 reg, u8 val)
855 {
856 	struct device *dev = &priv->client->dev;
857 	int ret;
858 
859 	mutex_lock(&priv->reg_lock);
860 
861 	ret = ub960_txport_select(priv, nport);
862 	if (ret)
863 		goto out_unlock;
864 
865 	ret = regmap_write(priv->regmap, reg, val);
866 	if (ret)
867 		dev_err(dev, "%s: cannot write register 0x%02x (%d)!\n",
868 			__func__, reg, ret);
869 
870 out_unlock:
871 	mutex_unlock(&priv->reg_lock);
872 
873 	return ret;
874 }
875 
ub960_txport_update_bits(struct ub960_data * priv,u8 nport,u8 reg,u8 mask,u8 val)876 static int ub960_txport_update_bits(struct ub960_data *priv, u8 nport, u8 reg,
877 				    u8 mask, u8 val)
878 {
879 	struct device *dev = &priv->client->dev;
880 	int ret;
881 
882 	mutex_lock(&priv->reg_lock);
883 
884 	ret = ub960_txport_select(priv, nport);
885 	if (ret)
886 		goto out_unlock;
887 
888 	ret = regmap_update_bits(priv->regmap, reg, mask, val);
889 	if (ret)
890 		dev_err(dev, "%s: cannot update register 0x%02x (%d)!\n",
891 			__func__, reg, ret);
892 
893 out_unlock:
894 	mutex_unlock(&priv->reg_lock);
895 
896 	return ret;
897 }
898 
ub960_select_ind_reg_block(struct ub960_data * priv,u8 block)899 static int ub960_select_ind_reg_block(struct ub960_data *priv, u8 block)
900 {
901 	struct device *dev = &priv->client->dev;
902 	int ret;
903 
904 	lockdep_assert_held(&priv->reg_lock);
905 
906 	if (priv->reg_current.indirect_target == block)
907 		return 0;
908 
909 	ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_CTL, block << 2);
910 	if (ret) {
911 		dev_err(dev, "%s: cannot select indirect target %u (%d)!\n",
912 			__func__, block, ret);
913 		return ret;
914 	}
915 
916 	priv->reg_current.indirect_target = block;
917 
918 	return 0;
919 }
920 
ub960_read_ind(struct ub960_data * priv,u8 block,u8 reg,u8 * val)921 static int ub960_read_ind(struct ub960_data *priv, u8 block, u8 reg, u8 *val)
922 {
923 	struct device *dev = &priv->client->dev;
924 	unsigned int v;
925 	int ret;
926 
927 	mutex_lock(&priv->reg_lock);
928 
929 	ret = ub960_select_ind_reg_block(priv, block);
930 	if (ret)
931 		goto out_unlock;
932 
933 	ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_ADDR, reg);
934 	if (ret) {
935 		dev_err(dev,
936 			"Write to IND_ACC_ADDR failed when reading %u:%x02x: %d\n",
937 			block, reg, ret);
938 		goto out_unlock;
939 	}
940 
941 	ret = regmap_read(priv->regmap, UB960_SR_IND_ACC_DATA, &v);
942 	if (ret) {
943 		dev_err(dev,
944 			"Write to IND_ACC_DATA failed when reading %u:%x02x: %d\n",
945 			block, reg, ret);
946 		goto out_unlock;
947 	}
948 
949 	*val = v;
950 
951 out_unlock:
952 	mutex_unlock(&priv->reg_lock);
953 
954 	return ret;
955 }
956 
ub960_write_ind(struct ub960_data * priv,u8 block,u8 reg,u8 val)957 static int ub960_write_ind(struct ub960_data *priv, u8 block, u8 reg, u8 val)
958 {
959 	struct device *dev = &priv->client->dev;
960 	int ret;
961 
962 	mutex_lock(&priv->reg_lock);
963 
964 	ret = ub960_select_ind_reg_block(priv, block);
965 	if (ret)
966 		goto out_unlock;
967 
968 	ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_ADDR, reg);
969 	if (ret) {
970 		dev_err(dev,
971 			"Write to IND_ACC_ADDR failed when writing %u:%x02x: %d\n",
972 			block, reg, ret);
973 		goto out_unlock;
974 	}
975 
976 	ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_DATA, val);
977 	if (ret) {
978 		dev_err(dev,
979 			"Write to IND_ACC_DATA failed when writing %u:%x02x: %d\n",
980 			block, reg, ret);
981 		goto out_unlock;
982 	}
983 
984 out_unlock:
985 	mutex_unlock(&priv->reg_lock);
986 
987 	return ret;
988 }
989 
ub960_ind_update_bits(struct ub960_data * priv,u8 block,u8 reg,u8 mask,u8 val)990 static int ub960_ind_update_bits(struct ub960_data *priv, u8 block, u8 reg,
991 				 u8 mask, u8 val)
992 {
993 	struct device *dev = &priv->client->dev;
994 	int ret;
995 
996 	mutex_lock(&priv->reg_lock);
997 
998 	ret = ub960_select_ind_reg_block(priv, block);
999 	if (ret)
1000 		goto out_unlock;
1001 
1002 	ret = regmap_write(priv->regmap, UB960_SR_IND_ACC_ADDR, reg);
1003 	if (ret) {
1004 		dev_err(dev,
1005 			"Write to IND_ACC_ADDR failed when updating %u:%x02x: %d\n",
1006 			block, reg, ret);
1007 		goto out_unlock;
1008 	}
1009 
1010 	ret = regmap_update_bits(priv->regmap, UB960_SR_IND_ACC_DATA, mask,
1011 				 val);
1012 	if (ret) {
1013 		dev_err(dev,
1014 			"Write to IND_ACC_DATA failed when updating %u:%x02x: %d\n",
1015 			block, reg, ret);
1016 		goto out_unlock;
1017 	}
1018 
1019 out_unlock:
1020 	mutex_unlock(&priv->reg_lock);
1021 
1022 	return ret;
1023 }
1024 
1025 /* -----------------------------------------------------------------------------
1026  * I2C-ATR (address translator)
1027  */
1028 
ub960_atr_attach_client(struct i2c_atr * atr,u32 chan_id,const struct i2c_client * client,u16 alias)1029 static int ub960_atr_attach_client(struct i2c_atr *atr, u32 chan_id,
1030 				   const struct i2c_client *client, u16 alias)
1031 {
1032 	struct ub960_data *priv = i2c_atr_get_driver_data(atr);
1033 	struct ub960_rxport *rxport = priv->rxports[chan_id];
1034 	struct device *dev = &priv->client->dev;
1035 	unsigned int reg_idx;
1036 
1037 	for (reg_idx = 0; reg_idx < ARRAY_SIZE(rxport->aliased_clients); reg_idx++) {
1038 		if (!rxport->aliased_clients[reg_idx])
1039 			break;
1040 	}
1041 
1042 	if (reg_idx == ARRAY_SIZE(rxport->aliased_clients)) {
1043 		dev_err(dev, "rx%u: alias pool exhausted\n", rxport->nport);
1044 		return -EADDRNOTAVAIL;
1045 	}
1046 
1047 	rxport->aliased_clients[reg_idx] = client;
1048 
1049 	ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ID(reg_idx),
1050 			   client->addr << 1);
1051 	ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx),
1052 			   alias << 1);
1053 
1054 	dev_dbg(dev, "rx%u: client 0x%02x assigned alias 0x%02x at slot %u\n",
1055 		rxport->nport, client->addr, alias, reg_idx);
1056 
1057 	return 0;
1058 }
1059 
ub960_atr_detach_client(struct i2c_atr * atr,u32 chan_id,const struct i2c_client * client)1060 static void ub960_atr_detach_client(struct i2c_atr *atr, u32 chan_id,
1061 				    const struct i2c_client *client)
1062 {
1063 	struct ub960_data *priv = i2c_atr_get_driver_data(atr);
1064 	struct ub960_rxport *rxport = priv->rxports[chan_id];
1065 	struct device *dev = &priv->client->dev;
1066 	unsigned int reg_idx;
1067 
1068 	for (reg_idx = 0; reg_idx < ARRAY_SIZE(rxport->aliased_clients); reg_idx++) {
1069 		if (rxport->aliased_clients[reg_idx] == client)
1070 			break;
1071 	}
1072 
1073 	if (reg_idx == ARRAY_SIZE(rxport->aliased_clients)) {
1074 		dev_err(dev, "rx%u: client 0x%02x is not mapped!\n",
1075 			rxport->nport, client->addr);
1076 		return;
1077 	}
1078 
1079 	rxport->aliased_clients[reg_idx] = NULL;
1080 
1081 	ub960_rxport_write(priv, chan_id, UB960_RR_SLAVE_ALIAS(reg_idx), 0);
1082 
1083 	dev_dbg(dev, "rx%u: client 0x%02x released at slot %u\n", rxport->nport,
1084 		client->addr, reg_idx);
1085 }
1086 
1087 static const struct i2c_atr_ops ub960_atr_ops = {
1088 	.attach_client = ub960_atr_attach_client,
1089 	.detach_client = ub960_atr_detach_client,
1090 };
1091 
ub960_init_atr(struct ub960_data * priv)1092 static int ub960_init_atr(struct ub960_data *priv)
1093 {
1094 	struct device *dev = &priv->client->dev;
1095 	struct i2c_adapter *parent_adap = priv->client->adapter;
1096 
1097 	priv->atr = i2c_atr_new(parent_adap, dev, &ub960_atr_ops,
1098 				priv->hw_data->num_rxports);
1099 	if (IS_ERR(priv->atr))
1100 		return PTR_ERR(priv->atr);
1101 
1102 	i2c_atr_set_driver_data(priv->atr, priv);
1103 
1104 	return 0;
1105 }
1106 
ub960_uninit_atr(struct ub960_data * priv)1107 static void ub960_uninit_atr(struct ub960_data *priv)
1108 {
1109 	i2c_atr_delete(priv->atr);
1110 	priv->atr = NULL;
1111 }
1112 
1113 /* -----------------------------------------------------------------------------
1114  * TX ports
1115  */
1116 
ub960_parse_dt_txport(struct ub960_data * priv,struct fwnode_handle * ep_fwnode,u8 nport)1117 static int ub960_parse_dt_txport(struct ub960_data *priv,
1118 				 struct fwnode_handle *ep_fwnode,
1119 				 u8 nport)
1120 {
1121 	struct device *dev = &priv->client->dev;
1122 	struct v4l2_fwnode_endpoint vep = {};
1123 	struct ub960_txport *txport;
1124 	int ret;
1125 
1126 	txport = kzalloc(sizeof(*txport), GFP_KERNEL);
1127 	if (!txport)
1128 		return -ENOMEM;
1129 
1130 	txport->priv = priv;
1131 	txport->nport = nport;
1132 
1133 	vep.bus_type = V4L2_MBUS_CSI2_DPHY;
1134 	ret = v4l2_fwnode_endpoint_alloc_parse(ep_fwnode, &vep);
1135 	if (ret) {
1136 		dev_err(dev, "tx%u: failed to parse endpoint data\n", nport);
1137 		goto err_free_txport;
1138 	}
1139 
1140 	txport->non_continous_clk = vep.bus.mipi_csi2.flags &
1141 				    V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1142 
1143 	txport->num_data_lanes = vep.bus.mipi_csi2.num_data_lanes;
1144 
1145 	if (vep.nr_of_link_frequencies != 1) {
1146 		ret = -EINVAL;
1147 		goto err_free_vep;
1148 	}
1149 
1150 	priv->tx_link_freq[0] = vep.link_frequencies[0];
1151 	priv->tx_data_rate = priv->tx_link_freq[0] * 2;
1152 
1153 	if (priv->tx_data_rate != MHZ(1600) &&
1154 	    priv->tx_data_rate != MHZ(1200) &&
1155 	    priv->tx_data_rate != MHZ(800) &&
1156 	    priv->tx_data_rate != MHZ(400)) {
1157 		dev_err(dev, "tx%u: invalid 'link-frequencies' value\n", nport);
1158 		ret = -EINVAL;
1159 		goto err_free_vep;
1160 	}
1161 
1162 	v4l2_fwnode_endpoint_free(&vep);
1163 
1164 	priv->txports[nport] = txport;
1165 
1166 	return 0;
1167 
1168 err_free_vep:
1169 	v4l2_fwnode_endpoint_free(&vep);
1170 err_free_txport:
1171 	kfree(txport);
1172 
1173 	return ret;
1174 }
1175 
ub960_csi_handle_events(struct ub960_data * priv,u8 nport)1176 static void ub960_csi_handle_events(struct ub960_data *priv, u8 nport)
1177 {
1178 	struct device *dev = &priv->client->dev;
1179 	u8 csi_tx_isr;
1180 	int ret;
1181 
1182 	ret = ub960_txport_read(priv, nport, UB960_TR_CSI_TX_ISR, &csi_tx_isr);
1183 	if (ret)
1184 		return;
1185 
1186 	if (csi_tx_isr & UB960_TR_CSI_TX_ISR_IS_CSI_SYNC_ERROR)
1187 		dev_warn(dev, "TX%u: CSI_SYNC_ERROR\n", nport);
1188 
1189 	if (csi_tx_isr & UB960_TR_CSI_TX_ISR_IS_CSI_PASS_ERROR)
1190 		dev_warn(dev, "TX%u: CSI_PASS_ERROR\n", nport);
1191 }
1192 
1193 /* -----------------------------------------------------------------------------
1194  * RX ports
1195  */
1196 
ub960_rxport_enable_vpocs(struct ub960_data * priv)1197 static int ub960_rxport_enable_vpocs(struct ub960_data *priv)
1198 {
1199 	unsigned int nport;
1200 	int ret;
1201 
1202 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
1203 		struct ub960_rxport *rxport = priv->rxports[nport];
1204 
1205 		if (!rxport || !rxport->vpoc)
1206 			continue;
1207 
1208 		ret = regulator_enable(rxport->vpoc);
1209 		if (ret)
1210 			goto err_disable_vpocs;
1211 	}
1212 
1213 	return 0;
1214 
1215 err_disable_vpocs:
1216 	while (nport--) {
1217 		struct ub960_rxport *rxport = priv->rxports[nport];
1218 
1219 		if (!rxport || !rxport->vpoc)
1220 			continue;
1221 
1222 		regulator_disable(rxport->vpoc);
1223 	}
1224 
1225 	return ret;
1226 }
1227 
ub960_rxport_disable_vpocs(struct ub960_data * priv)1228 static void ub960_rxport_disable_vpocs(struct ub960_data *priv)
1229 {
1230 	unsigned int nport;
1231 
1232 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
1233 		struct ub960_rxport *rxport = priv->rxports[nport];
1234 
1235 		if (!rxport || !rxport->vpoc)
1236 			continue;
1237 
1238 		regulator_disable(rxport->vpoc);
1239 	}
1240 }
1241 
ub960_rxport_clear_errors(struct ub960_data * priv,unsigned int nport)1242 static void ub960_rxport_clear_errors(struct ub960_data *priv,
1243 				      unsigned int nport)
1244 {
1245 	u8 v;
1246 
1247 	ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1, &v);
1248 	ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2, &v);
1249 	ub960_rxport_read(priv, nport, UB960_RR_CSI_RX_STS, &v);
1250 	ub960_rxport_read(priv, nport, UB960_RR_BCC_STATUS, &v);
1251 
1252 	ub960_rxport_read(priv, nport, UB960_RR_RX_PAR_ERR_HI, &v);
1253 	ub960_rxport_read(priv, nport, UB960_RR_RX_PAR_ERR_LO, &v);
1254 
1255 	ub960_rxport_read(priv, nport, UB960_RR_CSI_ERR_COUNTER, &v);
1256 }
1257 
ub960_clear_rx_errors(struct ub960_data * priv)1258 static void ub960_clear_rx_errors(struct ub960_data *priv)
1259 {
1260 	unsigned int nport;
1261 
1262 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++)
1263 		ub960_rxport_clear_errors(priv, nport);
1264 }
1265 
ub960_rxport_get_strobe_pos(struct ub960_data * priv,unsigned int nport,s8 * strobe_pos)1266 static int ub960_rxport_get_strobe_pos(struct ub960_data *priv,
1267 				       unsigned int nport, s8 *strobe_pos)
1268 {
1269 	u8 v;
1270 	u8 clk_delay, data_delay;
1271 	int ret;
1272 
1273 	ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
1274 		       UB960_IR_RX_ANA_STROBE_SET_CLK, &v);
1275 
1276 	clk_delay = (v & UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY) ?
1277 			    0 : UB960_MANUAL_STROBE_EXTRA_DELAY;
1278 
1279 	ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
1280 		       UB960_IR_RX_ANA_STROBE_SET_DATA, &v);
1281 
1282 	data_delay = (v & UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY) ?
1283 			     0 : UB960_MANUAL_STROBE_EXTRA_DELAY;
1284 
1285 	ret = ub960_rxport_read(priv, nport, UB960_RR_SFILTER_STS_0, &v);
1286 	if (ret)
1287 		return ret;
1288 
1289 	clk_delay += v & UB960_IR_RX_ANA_STROBE_SET_CLK_DELAY_MASK;
1290 
1291 	ret = ub960_rxport_read(priv, nport, UB960_RR_SFILTER_STS_1, &v);
1292 	if (ret)
1293 		return ret;
1294 
1295 	data_delay += v & UB960_IR_RX_ANA_STROBE_SET_DATA_DELAY_MASK;
1296 
1297 	*strobe_pos = data_delay - clk_delay;
1298 
1299 	return 0;
1300 }
1301 
ub960_rxport_set_strobe_pos(struct ub960_data * priv,unsigned int nport,s8 strobe_pos)1302 static void ub960_rxport_set_strobe_pos(struct ub960_data *priv,
1303 					unsigned int nport, s8 strobe_pos)
1304 {
1305 	u8 clk_delay, data_delay;
1306 
1307 	clk_delay = UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY;
1308 	data_delay = UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY;
1309 
1310 	if (strobe_pos < UB960_MIN_AEQ_STROBE_POS)
1311 		clk_delay = abs(strobe_pos) - UB960_MANUAL_STROBE_EXTRA_DELAY;
1312 	else if (strobe_pos > UB960_MAX_AEQ_STROBE_POS)
1313 		data_delay = strobe_pos - UB960_MANUAL_STROBE_EXTRA_DELAY;
1314 	else if (strobe_pos < 0)
1315 		clk_delay = abs(strobe_pos) | UB960_IR_RX_ANA_STROBE_SET_CLK_NO_EXTRA_DELAY;
1316 	else if (strobe_pos > 0)
1317 		data_delay = strobe_pos | UB960_IR_RX_ANA_STROBE_SET_DATA_NO_EXTRA_DELAY;
1318 
1319 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
1320 			UB960_IR_RX_ANA_STROBE_SET_CLK, clk_delay);
1321 
1322 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport),
1323 			UB960_IR_RX_ANA_STROBE_SET_DATA, data_delay);
1324 }
1325 
ub960_rxport_set_strobe_range(struct ub960_data * priv,s8 strobe_min,s8 strobe_max)1326 static void ub960_rxport_set_strobe_range(struct ub960_data *priv,
1327 					  s8 strobe_min, s8 strobe_max)
1328 {
1329 	/* Convert the signed strobe pos to positive zero based value */
1330 	strobe_min -= UB960_MIN_AEQ_STROBE_POS;
1331 	strobe_max -= UB960_MIN_AEQ_STROBE_POS;
1332 
1333 	ub960_write(priv, UB960_XR_SFILTER_CFG,
1334 		    ((u8)strobe_min << UB960_XR_SFILTER_CFG_SFILTER_MIN_SHIFT) |
1335 		    ((u8)strobe_max << UB960_XR_SFILTER_CFG_SFILTER_MAX_SHIFT));
1336 }
1337 
ub960_rxport_get_eq_level(struct ub960_data * priv,unsigned int nport,u8 * eq_level)1338 static int ub960_rxport_get_eq_level(struct ub960_data *priv,
1339 				     unsigned int nport, u8 *eq_level)
1340 {
1341 	int ret;
1342 	u8 v;
1343 
1344 	ret = ub960_rxport_read(priv, nport, UB960_RR_AEQ_STATUS, &v);
1345 	if (ret)
1346 		return ret;
1347 
1348 	*eq_level = (v & UB960_RR_AEQ_STATUS_STATUS_1) +
1349 		    (v & UB960_RR_AEQ_STATUS_STATUS_2);
1350 
1351 	return 0;
1352 }
1353 
ub960_rxport_set_eq_level(struct ub960_data * priv,unsigned int nport,u8 eq_level)1354 static void ub960_rxport_set_eq_level(struct ub960_data *priv,
1355 				      unsigned int nport, u8 eq_level)
1356 {
1357 	u8 eq_stage_1_select_value, eq_stage_2_select_value;
1358 	const unsigned int eq_stage_max = 7;
1359 	u8 v;
1360 
1361 	if (eq_level <= eq_stage_max) {
1362 		eq_stage_1_select_value = eq_level;
1363 		eq_stage_2_select_value = 0;
1364 	} else {
1365 		eq_stage_1_select_value = eq_stage_max;
1366 		eq_stage_2_select_value = eq_level - eq_stage_max;
1367 	}
1368 
1369 	ub960_rxport_read(priv, nport, UB960_RR_AEQ_BYPASS, &v);
1370 
1371 	v &= ~(UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_MASK |
1372 	       UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_MASK);
1373 	v |= eq_stage_1_select_value << UB960_RR_AEQ_BYPASS_EQ_STAGE1_VALUE_SHIFT;
1374 	v |= eq_stage_2_select_value << UB960_RR_AEQ_BYPASS_EQ_STAGE2_VALUE_SHIFT;
1375 	v |= UB960_RR_AEQ_BYPASS_ENABLE;
1376 
1377 	ub960_rxport_write(priv, nport, UB960_RR_AEQ_BYPASS, v);
1378 }
1379 
ub960_rxport_set_eq_range(struct ub960_data * priv,unsigned int nport,u8 eq_min,u8 eq_max)1380 static void ub960_rxport_set_eq_range(struct ub960_data *priv,
1381 				      unsigned int nport, u8 eq_min, u8 eq_max)
1382 {
1383 	ub960_rxport_write(priv, nport, UB960_RR_AEQ_MIN_MAX,
1384 			   (eq_min << UB960_RR_AEQ_MIN_MAX_AEQ_FLOOR_SHIFT) |
1385 			   (eq_max << UB960_RR_AEQ_MIN_MAX_AEQ_MAX_SHIFT));
1386 
1387 	/* Enable AEQ min setting */
1388 	ub960_rxport_update_bits(priv, nport, UB960_RR_AEQ_CTL2,
1389 				 UB960_RR_AEQ_CTL2_SET_AEQ_FLOOR,
1390 				 UB960_RR_AEQ_CTL2_SET_AEQ_FLOOR);
1391 }
1392 
ub960_rxport_config_eq(struct ub960_data * priv,unsigned int nport)1393 static void ub960_rxport_config_eq(struct ub960_data *priv, unsigned int nport)
1394 {
1395 	struct ub960_rxport *rxport = priv->rxports[nport];
1396 
1397 	/* We also set common settings here. Should be moved elsewhere. */
1398 
1399 	if (priv->strobe.manual) {
1400 		/* Disable AEQ_SFILTER_EN */
1401 		ub960_update_bits(priv, UB960_XR_AEQ_CTL1,
1402 				  UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN, 0);
1403 	} else {
1404 		/* Enable SFILTER and error control */
1405 		ub960_write(priv, UB960_XR_AEQ_CTL1,
1406 			    UB960_XR_AEQ_CTL1_AEQ_ERR_CTL_MASK |
1407 				    UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN);
1408 
1409 		/* Set AEQ strobe range */
1410 		ub960_rxport_set_strobe_range(priv, priv->strobe.min,
1411 					      priv->strobe.max);
1412 	}
1413 
1414 	/* The rest are port specific */
1415 
1416 	if (priv->strobe.manual)
1417 		ub960_rxport_set_strobe_pos(priv, nport, rxport->eq.strobe_pos);
1418 	else
1419 		ub960_rxport_set_strobe_pos(priv, nport, 0);
1420 
1421 	if (rxport->eq.manual_eq) {
1422 		ub960_rxport_set_eq_level(priv, nport,
1423 					  rxport->eq.manual.eq_level);
1424 
1425 		/* Enable AEQ Bypass */
1426 		ub960_rxport_update_bits(priv, nport, UB960_RR_AEQ_BYPASS,
1427 					 UB960_RR_AEQ_BYPASS_ENABLE,
1428 					 UB960_RR_AEQ_BYPASS_ENABLE);
1429 	} else {
1430 		ub960_rxport_set_eq_range(priv, nport,
1431 					  rxport->eq.aeq.eq_level_min,
1432 					  rxport->eq.aeq.eq_level_max);
1433 
1434 		/* Disable AEQ Bypass */
1435 		ub960_rxport_update_bits(priv, nport, UB960_RR_AEQ_BYPASS,
1436 					 UB960_RR_AEQ_BYPASS_ENABLE, 0);
1437 	}
1438 }
1439 
ub960_rxport_link_ok(struct ub960_data * priv,unsigned int nport,bool * ok)1440 static int ub960_rxport_link_ok(struct ub960_data *priv, unsigned int nport,
1441 				bool *ok)
1442 {
1443 	u8 rx_port_sts1, rx_port_sts2;
1444 	u16 parity_errors;
1445 	u8 csi_rx_sts;
1446 	u8 csi_err_cnt;
1447 	u8 bcc_sts;
1448 	int ret;
1449 	bool errors;
1450 
1451 	ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1,
1452 				&rx_port_sts1);
1453 	if (ret)
1454 		return ret;
1455 
1456 	if (!(rx_port_sts1 & UB960_RR_RX_PORT_STS1_LOCK_STS)) {
1457 		*ok = false;
1458 		return 0;
1459 	}
1460 
1461 	ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2,
1462 				&rx_port_sts2);
1463 	if (ret)
1464 		return ret;
1465 
1466 	ret = ub960_rxport_read(priv, nport, UB960_RR_CSI_RX_STS, &csi_rx_sts);
1467 	if (ret)
1468 		return ret;
1469 
1470 	ret = ub960_rxport_read(priv, nport, UB960_RR_CSI_ERR_COUNTER,
1471 				&csi_err_cnt);
1472 	if (ret)
1473 		return ret;
1474 
1475 	ret = ub960_rxport_read(priv, nport, UB960_RR_BCC_STATUS, &bcc_sts);
1476 	if (ret)
1477 		return ret;
1478 
1479 	ret = ub960_rxport_read16(priv, nport, UB960_RR_RX_PAR_ERR_HI,
1480 				  &parity_errors);
1481 	if (ret)
1482 		return ret;
1483 
1484 	errors = (rx_port_sts1 & UB960_RR_RX_PORT_STS1_ERROR_MASK) ||
1485 		 (rx_port_sts2 & UB960_RR_RX_PORT_STS2_ERROR_MASK) ||
1486 		 (bcc_sts & UB960_RR_BCC_STATUS_ERROR_MASK) ||
1487 		 (csi_rx_sts & UB960_RR_CSI_RX_STS_ERROR_MASK) || csi_err_cnt ||
1488 		 parity_errors;
1489 
1490 	*ok = !errors;
1491 
1492 	return 0;
1493 }
1494 
1495 /*
1496  * Wait for the RX ports to lock, have no errors and have stable strobe position
1497  * and EQ level.
1498  */
ub960_rxport_wait_locks(struct ub960_data * priv,unsigned long port_mask,unsigned int * lock_mask)1499 static int ub960_rxport_wait_locks(struct ub960_data *priv,
1500 				   unsigned long port_mask,
1501 				   unsigned int *lock_mask)
1502 {
1503 	struct device *dev = &priv->client->dev;
1504 	unsigned long timeout;
1505 	unsigned int link_ok_mask;
1506 	unsigned int missing;
1507 	unsigned int loops;
1508 	u8 nport;
1509 	int ret;
1510 
1511 	if (port_mask == 0) {
1512 		if (lock_mask)
1513 			*lock_mask = 0;
1514 		return 0;
1515 	}
1516 
1517 	if (port_mask >= BIT(priv->hw_data->num_rxports))
1518 		return -EINVAL;
1519 
1520 	timeout = jiffies + msecs_to_jiffies(1000);
1521 	loops = 0;
1522 	link_ok_mask = 0;
1523 
1524 	while (time_before(jiffies, timeout)) {
1525 		missing = 0;
1526 
1527 		for_each_set_bit(nport, &port_mask,
1528 				 priv->hw_data->num_rxports) {
1529 			struct ub960_rxport *rxport = priv->rxports[nport];
1530 			bool ok;
1531 
1532 			if (!rxport)
1533 				continue;
1534 
1535 			ret = ub960_rxport_link_ok(priv, nport, &ok);
1536 			if (ret)
1537 				return ret;
1538 
1539 			/*
1540 			 * We want the link to be ok for two consecutive loops,
1541 			 * as a link could get established just before our test
1542 			 * and drop soon after.
1543 			 */
1544 			if (!ok || !(link_ok_mask & BIT(nport)))
1545 				missing++;
1546 
1547 			if (ok)
1548 				link_ok_mask |= BIT(nport);
1549 			else
1550 				link_ok_mask &= ~BIT(nport);
1551 		}
1552 
1553 		loops++;
1554 
1555 		if (missing == 0)
1556 			break;
1557 
1558 		msleep(50);
1559 	}
1560 
1561 	if (lock_mask)
1562 		*lock_mask = link_ok_mask;
1563 
1564 	dev_dbg(dev, "Wait locks done in %u loops\n", loops);
1565 	for_each_set_bit(nport, &port_mask, priv->hw_data->num_rxports) {
1566 		struct ub960_rxport *rxport = priv->rxports[nport];
1567 		s8 strobe_pos, eq_level;
1568 		u16 v;
1569 
1570 		if (!rxport)
1571 			continue;
1572 
1573 		if (!(link_ok_mask & BIT(nport))) {
1574 			dev_dbg(dev, "\trx%u: not locked\n", nport);
1575 			continue;
1576 		}
1577 
1578 		ub960_rxport_read16(priv, nport, UB960_RR_RX_FREQ_HIGH, &v);
1579 
1580 		if (priv->hw_data->is_ub9702) {
1581 			dev_dbg(dev, "\trx%u: locked, freq %llu Hz\n",
1582 				nport, (v * 1000000ULL) >> 8);
1583 		} else {
1584 			ret = ub960_rxport_get_strobe_pos(priv, nport,
1585 							  &strobe_pos);
1586 			if (ret)
1587 				return ret;
1588 
1589 			ret = ub960_rxport_get_eq_level(priv, nport, &eq_level);
1590 			if (ret)
1591 				return ret;
1592 
1593 			dev_dbg(dev,
1594 				"\trx%u: locked, SP: %d, EQ: %u, freq %llu Hz\n",
1595 				nport, strobe_pos, eq_level,
1596 				(v * 1000000ULL) >> 8);
1597 		}
1598 	}
1599 
1600 	return 0;
1601 }
1602 
ub960_calc_bc_clk_rate_ub960(struct ub960_data * priv,struct ub960_rxport * rxport)1603 static unsigned long ub960_calc_bc_clk_rate_ub960(struct ub960_data *priv,
1604 						  struct ub960_rxport *rxport)
1605 {
1606 	unsigned int mult;
1607 	unsigned int div;
1608 
1609 	switch (rxport->rx_mode) {
1610 	case RXPORT_MODE_RAW10:
1611 	case RXPORT_MODE_RAW12_HF:
1612 	case RXPORT_MODE_RAW12_LF:
1613 		mult = 1;
1614 		div = 10;
1615 		break;
1616 
1617 	case RXPORT_MODE_CSI2_SYNC:
1618 		mult = 2;
1619 		div = 1;
1620 		break;
1621 
1622 	case RXPORT_MODE_CSI2_NONSYNC:
1623 		mult = 2;
1624 		div = 5;
1625 		break;
1626 
1627 	default:
1628 		return 0;
1629 	}
1630 
1631 	return clk_get_rate(priv->refclk) * mult / div;
1632 }
1633 
ub960_calc_bc_clk_rate_ub9702(struct ub960_data * priv,struct ub960_rxport * rxport)1634 static unsigned long ub960_calc_bc_clk_rate_ub9702(struct ub960_data *priv,
1635 						   struct ub960_rxport *rxport)
1636 {
1637 	switch (rxport->rx_mode) {
1638 	case RXPORT_MODE_RAW10:
1639 	case RXPORT_MODE_RAW12_HF:
1640 	case RXPORT_MODE_RAW12_LF:
1641 		return 2359400;
1642 
1643 	case RXPORT_MODE_CSI2_SYNC:
1644 		return 47187500;
1645 
1646 	case RXPORT_MODE_CSI2_NONSYNC:
1647 		return 9437500;
1648 
1649 	default:
1650 		return 0;
1651 	}
1652 }
1653 
ub960_rxport_add_serializer(struct ub960_data * priv,u8 nport)1654 static int ub960_rxport_add_serializer(struct ub960_data *priv, u8 nport)
1655 {
1656 	struct ub960_rxport *rxport = priv->rxports[nport];
1657 	struct device *dev = &priv->client->dev;
1658 	struct ds90ub9xx_platform_data *ser_pdata = &rxport->ser.pdata;
1659 	struct i2c_board_info ser_info = {
1660 		.of_node = to_of_node(rxport->ser.fwnode),
1661 		.fwnode = rxport->ser.fwnode,
1662 		.platform_data = ser_pdata,
1663 	};
1664 
1665 	ser_pdata->port = nport;
1666 	ser_pdata->atr = priv->atr;
1667 	if (priv->hw_data->is_ub9702)
1668 		ser_pdata->bc_rate = ub960_calc_bc_clk_rate_ub9702(priv, rxport);
1669 	else
1670 		ser_pdata->bc_rate = ub960_calc_bc_clk_rate_ub960(priv, rxport);
1671 
1672 	/*
1673 	 * The serializer is added under the same i2c adapter as the
1674 	 * deserializer. This is not quite right, as the serializer is behind
1675 	 * the FPD-Link.
1676 	 */
1677 	ser_info.addr = rxport->ser.alias;
1678 	rxport->ser.client =
1679 		i2c_new_client_device(priv->client->adapter, &ser_info);
1680 	if (IS_ERR(rxport->ser.client)) {
1681 		dev_err(dev, "rx%u: cannot add %s i2c device", nport,
1682 			ser_info.type);
1683 		return PTR_ERR(rxport->ser.client);
1684 	}
1685 
1686 	dev_dbg(dev, "rx%u: remote serializer at alias 0x%02x (%u-%04x)\n",
1687 		nport, rxport->ser.client->addr,
1688 		rxport->ser.client->adapter->nr, rxport->ser.client->addr);
1689 
1690 	return 0;
1691 }
1692 
ub960_rxport_remove_serializer(struct ub960_data * priv,u8 nport)1693 static void ub960_rxport_remove_serializer(struct ub960_data *priv, u8 nport)
1694 {
1695 	struct ub960_rxport *rxport = priv->rxports[nport];
1696 
1697 	i2c_unregister_device(rxport->ser.client);
1698 	rxport->ser.client = NULL;
1699 }
1700 
1701 /* Add serializer i2c devices for all initialized ports */
ub960_rxport_add_serializers(struct ub960_data * priv)1702 static int ub960_rxport_add_serializers(struct ub960_data *priv)
1703 {
1704 	unsigned int nport;
1705 	int ret;
1706 
1707 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
1708 		struct ub960_rxport *rxport = priv->rxports[nport];
1709 
1710 		if (!rxport)
1711 			continue;
1712 
1713 		ret = ub960_rxport_add_serializer(priv, nport);
1714 		if (ret)
1715 			goto err_remove_sers;
1716 	}
1717 
1718 	return 0;
1719 
1720 err_remove_sers:
1721 	while (nport--) {
1722 		struct ub960_rxport *rxport = priv->rxports[nport];
1723 
1724 		if (!rxport)
1725 			continue;
1726 
1727 		ub960_rxport_remove_serializer(priv, nport);
1728 	}
1729 
1730 	return ret;
1731 }
1732 
ub960_rxport_remove_serializers(struct ub960_data * priv)1733 static void ub960_rxport_remove_serializers(struct ub960_data *priv)
1734 {
1735 	unsigned int nport;
1736 
1737 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
1738 		struct ub960_rxport *rxport = priv->rxports[nport];
1739 
1740 		if (!rxport)
1741 			continue;
1742 
1743 		ub960_rxport_remove_serializer(priv, nport);
1744 	}
1745 }
1746 
ub960_init_tx_port(struct ub960_data * priv,struct ub960_txport * txport)1747 static void ub960_init_tx_port(struct ub960_data *priv,
1748 			       struct ub960_txport *txport)
1749 {
1750 	unsigned int nport = txport->nport;
1751 	u8 csi_ctl = 0;
1752 
1753 	/*
1754 	 * From the datasheet: "initial CSI Skew-Calibration
1755 	 * sequence [...] should be set when operating at 1.6 Gbps"
1756 	 */
1757 	if (priv->tx_data_rate == MHZ(1600))
1758 		csi_ctl |= UB960_TR_CSI_CTL_CSI_CAL_EN;
1759 
1760 	csi_ctl |= (4 - txport->num_data_lanes) << 4;
1761 
1762 	if (!txport->non_continous_clk)
1763 		csi_ctl |= UB960_TR_CSI_CTL_CSI_CONTS_CLOCK;
1764 
1765 	ub960_txport_write(priv, nport, UB960_TR_CSI_CTL, csi_ctl);
1766 }
1767 
ub960_init_tx_ports(struct ub960_data * priv)1768 static int ub960_init_tx_ports(struct ub960_data *priv)
1769 {
1770 	unsigned int nport;
1771 	u8 speed_select;
1772 	u8 pll_div;
1773 
1774 	/* TX ports */
1775 
1776 	switch (priv->tx_data_rate) {
1777 	case MHZ(1600):
1778 	default:
1779 		speed_select = 0;
1780 		pll_div = 0x10;
1781 		break;
1782 	case MHZ(1200):
1783 		speed_select = 1;
1784 		pll_div = 0x18;
1785 		break;
1786 	case MHZ(800):
1787 		speed_select = 2;
1788 		pll_div = 0x10;
1789 		break;
1790 	case MHZ(400):
1791 		speed_select = 3;
1792 		pll_div = 0x10;
1793 		break;
1794 	}
1795 
1796 	ub960_write(priv, UB960_SR_CSI_PLL_CTL, speed_select);
1797 
1798 	if (priv->hw_data->is_ub9702) {
1799 		ub960_write(priv, UB960_SR_CSI_PLL_DIV, pll_div);
1800 
1801 		switch (priv->tx_data_rate) {
1802 		case MHZ(1600):
1803 		default:
1804 			ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x92, 0x80);
1805 			ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x4b, 0x2a);
1806 			break;
1807 		case MHZ(800):
1808 			ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x92, 0x90);
1809 			ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x4f, 0x2a);
1810 			ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x4b, 0x2a);
1811 			break;
1812 		case MHZ(400):
1813 			ub960_write_ind(priv, UB960_IND_TARGET_CSI_ANA, 0x92, 0xa0);
1814 			break;
1815 		}
1816 	}
1817 
1818 	for (nport = 0; nport < priv->hw_data->num_txports; nport++) {
1819 		struct ub960_txport *txport = priv->txports[nport];
1820 
1821 		if (!txport)
1822 			continue;
1823 
1824 		ub960_init_tx_port(priv, txport);
1825 	}
1826 
1827 	return 0;
1828 }
1829 
ub960_init_rx_port_ub960(struct ub960_data * priv,struct ub960_rxport * rxport)1830 static void ub960_init_rx_port_ub960(struct ub960_data *priv,
1831 				     struct ub960_rxport *rxport)
1832 {
1833 	unsigned int nport = rxport->nport;
1834 	u32 bc_freq_val;
1835 
1836 	/*
1837 	 * Back channel frequency select.
1838 	 * Override FREQ_SELECT from the strap.
1839 	 * 0 - 2.5 Mbps (DS90UB913A-Q1 / DS90UB933-Q1)
1840 	 * 2 - 10 Mbps
1841 	 * 6 - 50 Mbps (DS90UB953-Q1)
1842 	 *
1843 	 * Note that changing this setting will result in some errors on the back
1844 	 * channel for a short period of time.
1845 	 */
1846 
1847 	switch (rxport->rx_mode) {
1848 	case RXPORT_MODE_RAW10:
1849 	case RXPORT_MODE_RAW12_HF:
1850 	case RXPORT_MODE_RAW12_LF:
1851 		bc_freq_val = 0;
1852 		break;
1853 
1854 	case RXPORT_MODE_CSI2_NONSYNC:
1855 		bc_freq_val = 2;
1856 		break;
1857 
1858 	case RXPORT_MODE_CSI2_SYNC:
1859 		bc_freq_val = 6;
1860 		break;
1861 
1862 	default:
1863 		return;
1864 	}
1865 
1866 	ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
1867 				 UB960_RR_BCC_CONFIG_BC_FREQ_SEL_MASK,
1868 				 bc_freq_val);
1869 
1870 	switch (rxport->rx_mode) {
1871 	case RXPORT_MODE_RAW10:
1872 		/* FPD3_MODE = RAW10 Mode (DS90UB913A-Q1 / DS90UB933-Q1 compatible) */
1873 		ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG,
1874 					 UB960_RR_PORT_CONFIG_FPD3_MODE_MASK,
1875 					 0x3);
1876 
1877 		/*
1878 		 * RAW10_8BIT_CTL = 0b10 : 8-bit processing using upper 8 bits
1879 		 */
1880 		ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2,
1881 			UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_MASK,
1882 			0x2 << UB960_RR_PORT_CONFIG2_RAW10_8BIT_CTL_SHIFT);
1883 
1884 		break;
1885 
1886 	case RXPORT_MODE_RAW12_HF:
1887 	case RXPORT_MODE_RAW12_LF:
1888 		/* Not implemented */
1889 		return;
1890 
1891 	case RXPORT_MODE_CSI2_SYNC:
1892 	case RXPORT_MODE_CSI2_NONSYNC:
1893 		/* CSI-2 Mode (DS90UB953-Q1 compatible) */
1894 		ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG, 0x3,
1895 					 0x0);
1896 
1897 		break;
1898 	}
1899 
1900 	/* LV_POLARITY & FV_POLARITY */
1901 	ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2, 0x3,
1902 				 rxport->lv_fv_pol);
1903 
1904 	/* Enable all interrupt sources from this port */
1905 	ub960_rxport_write(priv, nport, UB960_RR_PORT_ICR_HI, 0x07);
1906 	ub960_rxport_write(priv, nport, UB960_RR_PORT_ICR_LO, 0x7f);
1907 
1908 	/* Enable I2C_PASS_THROUGH */
1909 	ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
1910 				 UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH,
1911 				 UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH);
1912 
1913 	/* Enable I2C communication to the serializer via the alias addr */
1914 	ub960_rxport_write(priv, nport, UB960_RR_SER_ALIAS_ID,
1915 			   rxport->ser.alias << 1);
1916 
1917 	/* Configure EQ related settings */
1918 	ub960_rxport_config_eq(priv, nport);
1919 
1920 	/* Enable RX port */
1921 	ub960_update_bits(priv, UB960_SR_RX_PORT_CTL, BIT(nport), BIT(nport));
1922 }
1923 
ub960_init_rx_port_ub9702_fpd3(struct ub960_data * priv,struct ub960_rxport * rxport)1924 static void ub960_init_rx_port_ub9702_fpd3(struct ub960_data *priv,
1925 					   struct ub960_rxport *rxport)
1926 {
1927 	unsigned int nport = rxport->nport;
1928 	u8 bc_freq_val;
1929 	u8 fpd_func_mode;
1930 
1931 	switch (rxport->rx_mode) {
1932 	case RXPORT_MODE_RAW10:
1933 		bc_freq_val = 0;
1934 		fpd_func_mode = 5;
1935 		break;
1936 
1937 	case RXPORT_MODE_RAW12_HF:
1938 		bc_freq_val = 0;
1939 		fpd_func_mode = 4;
1940 		break;
1941 
1942 	case RXPORT_MODE_RAW12_LF:
1943 		bc_freq_val = 0;
1944 		fpd_func_mode = 6;
1945 		break;
1946 
1947 	case RXPORT_MODE_CSI2_SYNC:
1948 		bc_freq_val = 6;
1949 		fpd_func_mode = 2;
1950 		break;
1951 
1952 	case RXPORT_MODE_CSI2_NONSYNC:
1953 		bc_freq_val = 2;
1954 		fpd_func_mode = 2;
1955 		break;
1956 
1957 	default:
1958 		return;
1959 	}
1960 
1961 	ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG, 0x7,
1962 				 bc_freq_val);
1963 	ub960_rxport_write(priv, nport, UB960_RR_CHANNEL_MODE, fpd_func_mode);
1964 
1965 	/* set serdes_eq_mode = 1 */
1966 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0xa8, 0x80);
1967 
1968 	/* enable serdes driver */
1969 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x0d, 0x7f);
1970 
1971 	/* set serdes_eq_offset=4 */
1972 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x2b, 0x04);
1973 
1974 	/* init default serdes_eq_max in 0xa9 */
1975 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0xa9, 0x23);
1976 
1977 	/* init serdes_eq_min in 0xaa */
1978 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0xaa, 0);
1979 
1980 	/* serdes_driver_ctl2 control: DS90UB953-Q1/DS90UB933-Q1/DS90UB913A-Q1 */
1981 	ub960_ind_update_bits(priv, UB960_IND_TARGET_RX_ANA(nport), 0x1b,
1982 			      BIT(3), BIT(3));
1983 
1984 	/* RX port to half-rate */
1985 	ub960_update_bits(priv, UB960_SR_FPD_RATE_CFG, 0x3 << (nport * 2),
1986 			  BIT(nport * 2));
1987 }
1988 
ub960_init_rx_port_ub9702_fpd4_aeq(struct ub960_data * priv,struct ub960_rxport * rxport)1989 static void ub960_init_rx_port_ub9702_fpd4_aeq(struct ub960_data *priv,
1990 					       struct ub960_rxport *rxport)
1991 {
1992 	unsigned int nport = rxport->nport;
1993 	bool first_time_power_up = true;
1994 
1995 	if (first_time_power_up) {
1996 		u8 v;
1997 
1998 		/* AEQ init */
1999 		ub960_read_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x2c, &v);
2000 
2001 		ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x27, v);
2002 		ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x28, v + 1);
2003 
2004 		ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x2b, 0x00);
2005 	}
2006 
2007 	/* enable serdes_eq_ctl2 */
2008 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x9e, 0x00);
2009 
2010 	/* enable serdes_eq_ctl1 */
2011 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x90, 0x40);
2012 
2013 	/* enable serdes_eq_en */
2014 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x2e, 0x40);
2015 
2016 	/* disable serdes_eq_override */
2017 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0xf0, 0x00);
2018 
2019 	/* disable serdes_gain_override */
2020 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x71, 0x00);
2021 }
2022 
ub960_init_rx_port_ub9702_fpd4(struct ub960_data * priv,struct ub960_rxport * rxport)2023 static void ub960_init_rx_port_ub9702_fpd4(struct ub960_data *priv,
2024 					   struct ub960_rxport *rxport)
2025 {
2026 	unsigned int nport = rxport->nport;
2027 	u8 bc_freq_val;
2028 
2029 	switch (rxport->rx_mode) {
2030 	case RXPORT_MODE_RAW10:
2031 		bc_freq_val = 0;
2032 		break;
2033 
2034 	case RXPORT_MODE_RAW12_HF:
2035 		bc_freq_val = 0;
2036 		break;
2037 
2038 	case RXPORT_MODE_RAW12_LF:
2039 		bc_freq_val = 0;
2040 		break;
2041 
2042 	case RXPORT_MODE_CSI2_SYNC:
2043 		bc_freq_val = 6;
2044 		break;
2045 
2046 	case RXPORT_MODE_CSI2_NONSYNC:
2047 		bc_freq_val = 2;
2048 		break;
2049 
2050 	default:
2051 		return;
2052 	}
2053 
2054 	ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG, 0x7,
2055 				 bc_freq_val);
2056 
2057 	/* FPD4 Sync Mode */
2058 	ub960_rxport_write(priv, nport, UB960_RR_CHANNEL_MODE, 0);
2059 
2060 	/* add serdes_eq_offset of 4 */
2061 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x2b, 0x04);
2062 
2063 	/* FPD4 serdes_start_eq in 0x27: assign default */
2064 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x27, 0x0);
2065 	/* FPD4 serdes_end_eq in 0x28: assign default */
2066 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x28, 0x23);
2067 
2068 	/* set serdes_driver_mode into FPD IV mode */
2069 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x04, 0x00);
2070 	/* set FPD PBC drv into FPD IV mode */
2071 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x1b, 0x00);
2072 
2073 	/* set serdes_system_init to 0x2f */
2074 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x21, 0x2f);
2075 	/* set serdes_system_rst in reset mode */
2076 	ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x25, 0xc1);
2077 
2078 	/* RX port to 7.55G mode */
2079 	ub960_update_bits(priv, UB960_SR_FPD_RATE_CFG, 0x3 << (nport * 2),
2080 			  0 << (nport * 2));
2081 
2082 	ub960_init_rx_port_ub9702_fpd4_aeq(priv, rxport);
2083 }
2084 
ub960_init_rx_port_ub9702(struct ub960_data * priv,struct ub960_rxport * rxport)2085 static void ub960_init_rx_port_ub9702(struct ub960_data *priv,
2086 				      struct ub960_rxport *rxport)
2087 {
2088 	unsigned int nport = rxport->nport;
2089 
2090 	if (rxport->cdr_mode == RXPORT_CDR_FPD3)
2091 		ub960_init_rx_port_ub9702_fpd3(priv, rxport);
2092 	else /* RXPORT_CDR_FPD4 */
2093 		ub960_init_rx_port_ub9702_fpd4(priv, rxport);
2094 
2095 	switch (rxport->rx_mode) {
2096 	case RXPORT_MODE_RAW10:
2097 		/*
2098 		 * RAW10_8BIT_CTL = 0b11 : 8-bit processing using lower 8 bits
2099 		 * 0b10 : 8-bit processing using upper 8 bits
2100 		 */
2101 		ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2,
2102 					 0x3 << 6, 0x2 << 6);
2103 
2104 		break;
2105 
2106 	case RXPORT_MODE_RAW12_HF:
2107 	case RXPORT_MODE_RAW12_LF:
2108 		/* Not implemented */
2109 		return;
2110 
2111 	case RXPORT_MODE_CSI2_SYNC:
2112 	case RXPORT_MODE_CSI2_NONSYNC:
2113 
2114 		break;
2115 	}
2116 
2117 	/* LV_POLARITY & FV_POLARITY */
2118 	ub960_rxport_update_bits(priv, nport, UB960_RR_PORT_CONFIG2, 0x3,
2119 				 rxport->lv_fv_pol);
2120 
2121 	/* Enable all interrupt sources from this port */
2122 	ub960_rxport_write(priv, nport, UB960_RR_PORT_ICR_HI, 0x07);
2123 	ub960_rxport_write(priv, nport, UB960_RR_PORT_ICR_LO, 0x7f);
2124 
2125 	/* Enable I2C_PASS_THROUGH */
2126 	ub960_rxport_update_bits(priv, nport, UB960_RR_BCC_CONFIG,
2127 				 UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH,
2128 				 UB960_RR_BCC_CONFIG_I2C_PASS_THROUGH);
2129 
2130 	/* Enable I2C communication to the serializer via the alias addr */
2131 	ub960_rxport_write(priv, nport, UB960_RR_SER_ALIAS_ID,
2132 			   rxport->ser.alias << 1);
2133 
2134 	/* Enable RX port */
2135 	ub960_update_bits(priv, UB960_SR_RX_PORT_CTL, BIT(nport), BIT(nport));
2136 
2137 	if (rxport->cdr_mode == RXPORT_CDR_FPD4) {
2138 		/* unreset 960 AEQ */
2139 		ub960_write_ind(priv, UB960_IND_TARGET_RX_ANA(nport), 0x25, 0x41);
2140 	}
2141 }
2142 
ub960_init_rx_ports(struct ub960_data * priv)2143 static int ub960_init_rx_ports(struct ub960_data *priv)
2144 {
2145 	unsigned int nport;
2146 
2147 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2148 		struct ub960_rxport *rxport = priv->rxports[nport];
2149 
2150 		if (!rxport)
2151 			continue;
2152 
2153 		if (priv->hw_data->is_ub9702)
2154 			ub960_init_rx_port_ub9702(priv, rxport);
2155 		else
2156 			ub960_init_rx_port_ub960(priv, rxport);
2157 	}
2158 
2159 	return 0;
2160 }
2161 
ub960_rxport_handle_events(struct ub960_data * priv,u8 nport)2162 static void ub960_rxport_handle_events(struct ub960_data *priv, u8 nport)
2163 {
2164 	struct device *dev = &priv->client->dev;
2165 	u8 rx_port_sts1;
2166 	u8 rx_port_sts2;
2167 	u8 csi_rx_sts;
2168 	u8 bcc_sts;
2169 	int ret = 0;
2170 
2171 	/* Read interrupts (also clears most of them) */
2172 	if (!ret)
2173 		ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1,
2174 					&rx_port_sts1);
2175 	if (!ret)
2176 		ret = ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2,
2177 					&rx_port_sts2);
2178 	if (!ret)
2179 		ret = ub960_rxport_read(priv, nport, UB960_RR_CSI_RX_STS,
2180 					&csi_rx_sts);
2181 	if (!ret)
2182 		ret = ub960_rxport_read(priv, nport, UB960_RR_BCC_STATUS,
2183 					&bcc_sts);
2184 
2185 	if (ret)
2186 		return;
2187 
2188 	if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_PARITY_ERROR) {
2189 		u16 v;
2190 
2191 		ret = ub960_rxport_read16(priv, nport, UB960_RR_RX_PAR_ERR_HI,
2192 					  &v);
2193 		if (!ret)
2194 			dev_err(dev, "rx%u parity errors: %u\n", nport, v);
2195 	}
2196 
2197 	if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_BCC_CRC_ERROR)
2198 		dev_err(dev, "rx%u BCC CRC error\n", nport);
2199 
2200 	if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_BCC_SEQ_ERROR)
2201 		dev_err(dev, "rx%u BCC SEQ error\n", nport);
2202 
2203 	if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_LINE_LEN_UNSTABLE)
2204 		dev_err(dev, "rx%u line length unstable\n", nport);
2205 
2206 	if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_FPD3_ENCODE_ERROR)
2207 		dev_err(dev, "rx%u FPD3 encode error\n", nport);
2208 
2209 	if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_BUFFER_ERROR)
2210 		dev_err(dev, "rx%u buffer error\n", nport);
2211 
2212 	if (csi_rx_sts)
2213 		dev_err(dev, "rx%u CSI error: %#02x\n", nport, csi_rx_sts);
2214 
2215 	if (csi_rx_sts & UB960_RR_CSI_RX_STS_ECC1_ERR)
2216 		dev_err(dev, "rx%u CSI ECC1 error\n", nport);
2217 
2218 	if (csi_rx_sts & UB960_RR_CSI_RX_STS_ECC2_ERR)
2219 		dev_err(dev, "rx%u CSI ECC2 error\n", nport);
2220 
2221 	if (csi_rx_sts & UB960_RR_CSI_RX_STS_CKSUM_ERR)
2222 		dev_err(dev, "rx%u CSI checksum error\n", nport);
2223 
2224 	if (csi_rx_sts & UB960_RR_CSI_RX_STS_LENGTH_ERR)
2225 		dev_err(dev, "rx%u CSI length error\n", nport);
2226 
2227 	if (bcc_sts)
2228 		dev_err(dev, "rx%u BCC error: %#02x\n", nport, bcc_sts);
2229 
2230 	if (bcc_sts & UB960_RR_BCC_STATUS_RESP_ERR)
2231 		dev_err(dev, "rx%u BCC response error", nport);
2232 
2233 	if (bcc_sts & UB960_RR_BCC_STATUS_SLAVE_TO)
2234 		dev_err(dev, "rx%u BCC slave timeout", nport);
2235 
2236 	if (bcc_sts & UB960_RR_BCC_STATUS_SLAVE_ERR)
2237 		dev_err(dev, "rx%u BCC slave error", nport);
2238 
2239 	if (bcc_sts & UB960_RR_BCC_STATUS_MASTER_TO)
2240 		dev_err(dev, "rx%u BCC master timeout", nport);
2241 
2242 	if (bcc_sts & UB960_RR_BCC_STATUS_MASTER_ERR)
2243 		dev_err(dev, "rx%u BCC master error", nport);
2244 
2245 	if (bcc_sts & UB960_RR_BCC_STATUS_SEQ_ERROR)
2246 		dev_err(dev, "rx%u BCC sequence error", nport);
2247 
2248 	if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_LINE_LEN_CHG) {
2249 		u16 v;
2250 
2251 		ret = ub960_rxport_read16(priv, nport, UB960_RR_LINE_LEN_1, &v);
2252 		if (!ret)
2253 			dev_dbg(dev, "rx%u line len changed: %u\n", nport, v);
2254 	}
2255 
2256 	if (rx_port_sts2 & UB960_RR_RX_PORT_STS2_LINE_CNT_CHG) {
2257 		u16 v;
2258 
2259 		ret = ub960_rxport_read16(priv, nport, UB960_RR_LINE_COUNT_HI,
2260 					  &v);
2261 		if (!ret)
2262 			dev_dbg(dev, "rx%u line count changed: %u\n", nport, v);
2263 	}
2264 
2265 	if (rx_port_sts1 & UB960_RR_RX_PORT_STS1_LOCK_STS_CHG) {
2266 		dev_dbg(dev, "rx%u: %s, %s, %s, %s\n", nport,
2267 			(rx_port_sts1 & UB960_RR_RX_PORT_STS1_LOCK_STS) ?
2268 				"locked" :
2269 				"unlocked",
2270 			(rx_port_sts1 & UB960_RR_RX_PORT_STS1_PORT_PASS) ?
2271 				"passed" :
2272 				"not passed",
2273 			(rx_port_sts2 & UB960_RR_RX_PORT_STS2_CABLE_FAULT) ?
2274 				"no clock" :
2275 				"clock ok",
2276 			(rx_port_sts2 & UB960_RR_RX_PORT_STS2_FREQ_STABLE) ?
2277 				"stable freq" :
2278 				"unstable freq");
2279 	}
2280 }
2281 
2282 /* -----------------------------------------------------------------------------
2283  * V4L2
2284  */
2285 
2286 /*
2287  * The current implementation only supports a simple VC mapping, where all VCs
2288  * from a one RX port will be mapped to the same VC. Also, the hardware
2289  * dictates that all streams from an RX port must go to a single TX port.
2290  *
2291  * This function decides the target VC numbers for each RX port with a simple
2292  * algorithm, so that for each TX port, we get VC numbers starting from 0,
2293  * and counting up.
2294  *
2295  * E.g. if all four RX ports are in use, of which the first two go to the
2296  * first TX port and the secont two go to the second TX port, we would get
2297  * the following VCs for the four RX ports: 0, 1, 0, 1.
2298  *
2299  * TODO: implement a more sophisticated VC mapping. As the driver cannot know
2300  * what VCs the sinks expect (say, an FPGA with hardcoded VC routing), this
2301  * probably needs to be somehow configurable. Device tree?
2302  */
ub960_get_vc_maps(struct ub960_data * priv,struct v4l2_subdev_state * state,u8 * vc)2303 static void ub960_get_vc_maps(struct ub960_data *priv,
2304 			      struct v4l2_subdev_state *state, u8 *vc)
2305 {
2306 	u8 cur_vc[UB960_MAX_TX_NPORTS] = {};
2307 	struct v4l2_subdev_route *route;
2308 	u8 handled_mask = 0;
2309 
2310 	for_each_active_route(&state->routing, route) {
2311 		unsigned int rx, tx;
2312 
2313 		rx = ub960_pad_to_port(priv, route->sink_pad);
2314 		if (BIT(rx) & handled_mask)
2315 			continue;
2316 
2317 		tx = ub960_pad_to_port(priv, route->source_pad);
2318 
2319 		vc[rx] = cur_vc[tx]++;
2320 		handled_mask |= BIT(rx);
2321 	}
2322 }
2323 
ub960_enable_tx_port(struct ub960_data * priv,unsigned int nport)2324 static int ub960_enable_tx_port(struct ub960_data *priv, unsigned int nport)
2325 {
2326 	struct device *dev = &priv->client->dev;
2327 
2328 	dev_dbg(dev, "enable TX port %u\n", nport);
2329 
2330 	return ub960_txport_update_bits(priv, nport, UB960_TR_CSI_CTL,
2331 					UB960_TR_CSI_CTL_CSI_ENABLE,
2332 					UB960_TR_CSI_CTL_CSI_ENABLE);
2333 }
2334 
ub960_disable_tx_port(struct ub960_data * priv,unsigned int nport)2335 static void ub960_disable_tx_port(struct ub960_data *priv, unsigned int nport)
2336 {
2337 	struct device *dev = &priv->client->dev;
2338 
2339 	dev_dbg(dev, "disable TX port %u\n", nport);
2340 
2341 	ub960_txport_update_bits(priv, nport, UB960_TR_CSI_CTL,
2342 				 UB960_TR_CSI_CTL_CSI_ENABLE, 0);
2343 }
2344 
ub960_enable_rx_port(struct ub960_data * priv,unsigned int nport)2345 static int ub960_enable_rx_port(struct ub960_data *priv, unsigned int nport)
2346 {
2347 	struct device *dev = &priv->client->dev;
2348 
2349 	dev_dbg(dev, "enable RX port %u\n", nport);
2350 
2351 	/* Enable forwarding */
2352 	return ub960_update_bits(priv, UB960_SR_FWD_CTL1,
2353 				 UB960_SR_FWD_CTL1_PORT_DIS(nport), 0);
2354 }
2355 
ub960_disable_rx_port(struct ub960_data * priv,unsigned int nport)2356 static void ub960_disable_rx_port(struct ub960_data *priv, unsigned int nport)
2357 {
2358 	struct device *dev = &priv->client->dev;
2359 
2360 	dev_dbg(dev, "disable RX port %u\n", nport);
2361 
2362 	/* Disable forwarding */
2363 	ub960_update_bits(priv, UB960_SR_FWD_CTL1,
2364 			  UB960_SR_FWD_CTL1_PORT_DIS(nport),
2365 			  UB960_SR_FWD_CTL1_PORT_DIS(nport));
2366 }
2367 
2368 /*
2369  * The driver only supports using a single VC for each source. This function
2370  * checks that each source only provides streams using a single VC.
2371  */
ub960_validate_stream_vcs(struct ub960_data * priv)2372 static int ub960_validate_stream_vcs(struct ub960_data *priv)
2373 {
2374 	unsigned int nport;
2375 	unsigned int i;
2376 
2377 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2378 		struct ub960_rxport *rxport = priv->rxports[nport];
2379 		struct v4l2_mbus_frame_desc desc;
2380 		int ret;
2381 		u8 vc;
2382 
2383 		if (!rxport)
2384 			continue;
2385 
2386 		ret = v4l2_subdev_call(rxport->source.sd, pad, get_frame_desc,
2387 				       rxport->source.pad, &desc);
2388 		if (ret)
2389 			return ret;
2390 
2391 		if (desc.type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
2392 			continue;
2393 
2394 		if (desc.num_entries == 0)
2395 			continue;
2396 
2397 		vc = desc.entry[0].bus.csi2.vc;
2398 
2399 		for (i = 1; i < desc.num_entries; i++) {
2400 			if (vc == desc.entry[i].bus.csi2.vc)
2401 				continue;
2402 
2403 			dev_err(&priv->client->dev,
2404 				"rx%u: source with multiple virtual-channels is not supported\n",
2405 				nport);
2406 			return -ENODEV;
2407 		}
2408 	}
2409 
2410 	return 0;
2411 }
2412 
ub960_configure_ports_for_streaming(struct ub960_data * priv,struct v4l2_subdev_state * state)2413 static int ub960_configure_ports_for_streaming(struct ub960_data *priv,
2414 					       struct v4l2_subdev_state *state)
2415 {
2416 	u8 fwd_ctl;
2417 	struct {
2418 		u32 num_streams;
2419 		u8 pixel_dt;
2420 		u8 meta_dt;
2421 		u32 meta_lines;
2422 		u32 tx_port;
2423 	} rx_data[UB960_MAX_RX_NPORTS] = {};
2424 	u8 vc_map[UB960_MAX_RX_NPORTS] = {};
2425 	struct v4l2_subdev_route *route;
2426 	unsigned int nport;
2427 	int ret;
2428 
2429 	ret = ub960_validate_stream_vcs(priv);
2430 	if (ret)
2431 		return ret;
2432 
2433 	ub960_get_vc_maps(priv, state, vc_map);
2434 
2435 	for_each_active_route(&state->routing, route) {
2436 		struct ub960_rxport *rxport;
2437 		struct ub960_txport *txport;
2438 		struct v4l2_mbus_framefmt *fmt;
2439 		const struct ub960_format_info *ub960_fmt;
2440 		unsigned int nport;
2441 
2442 		nport = ub960_pad_to_port(priv, route->sink_pad);
2443 
2444 		rxport = priv->rxports[nport];
2445 		if (!rxport)
2446 			return -EINVAL;
2447 
2448 		txport = priv->txports[ub960_pad_to_port(priv, route->source_pad)];
2449 		if (!txport)
2450 			return -EINVAL;
2451 
2452 		rx_data[nport].tx_port = ub960_pad_to_port(priv, route->source_pad);
2453 
2454 		rx_data[nport].num_streams++;
2455 
2456 		/* For the rest, we are only interested in parallel busses */
2457 		if (rxport->rx_mode == RXPORT_MODE_CSI2_SYNC ||
2458 		    rxport->rx_mode == RXPORT_MODE_CSI2_NONSYNC)
2459 			continue;
2460 
2461 		if (rx_data[nport].num_streams > 2)
2462 			return -EPIPE;
2463 
2464 		fmt = v4l2_subdev_state_get_stream_format(state,
2465 							  route->sink_pad,
2466 							  route->sink_stream);
2467 		if (!fmt)
2468 			return -EPIPE;
2469 
2470 		ub960_fmt = ub960_find_format(fmt->code);
2471 		if (!ub960_fmt)
2472 			return -EPIPE;
2473 
2474 		if (ub960_fmt->meta) {
2475 			if (fmt->height > 3) {
2476 				dev_err(&priv->client->dev,
2477 					"rx%u: unsupported metadata height %u\n",
2478 					nport, fmt->height);
2479 				return -EPIPE;
2480 			}
2481 
2482 			rx_data[nport].meta_dt = ub960_fmt->datatype;
2483 			rx_data[nport].meta_lines = fmt->height;
2484 		} else {
2485 			rx_data[nport].pixel_dt = ub960_fmt->datatype;
2486 		}
2487 	}
2488 
2489 	/* Configure RX ports */
2490 
2491 	/*
2492 	 * Keep all port forwardings disabled by default. Forwarding will be
2493 	 * enabled in ub960_enable_rx_port.
2494 	 */
2495 	fwd_ctl = GENMASK(7, 4);
2496 
2497 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2498 		struct ub960_rxport *rxport = priv->rxports[nport];
2499 		u8 vc = vc_map[nport];
2500 
2501 		if (rx_data[nport].num_streams == 0)
2502 			continue;
2503 
2504 		switch (rxport->rx_mode) {
2505 		case RXPORT_MODE_RAW10:
2506 			ub960_rxport_write(priv, nport, UB960_RR_RAW10_ID,
2507 				rx_data[nport].pixel_dt | (vc << UB960_RR_RAW10_ID_VC_SHIFT));
2508 
2509 			ub960_rxport_write(priv, rxport->nport,
2510 				UB960_RR_RAW_EMBED_DTYPE,
2511 				(rx_data[nport].meta_lines << UB960_RR_RAW_EMBED_DTYPE_LINES_SHIFT) |
2512 					rx_data[nport].meta_dt);
2513 
2514 			break;
2515 
2516 		case RXPORT_MODE_RAW12_HF:
2517 		case RXPORT_MODE_RAW12_LF:
2518 			/* Not implemented */
2519 			break;
2520 
2521 		case RXPORT_MODE_CSI2_SYNC:
2522 		case RXPORT_MODE_CSI2_NONSYNC:
2523 			if (!priv->hw_data->is_ub9702) {
2524 				/* Map all VCs from this port to the same VC */
2525 				ub960_rxport_write(priv, nport, UB960_RR_CSI_VC_MAP,
2526 						   (vc << UB960_RR_CSI_VC_MAP_SHIFT(3)) |
2527 						   (vc << UB960_RR_CSI_VC_MAP_SHIFT(2)) |
2528 						   (vc << UB960_RR_CSI_VC_MAP_SHIFT(1)) |
2529 						   (vc << UB960_RR_CSI_VC_MAP_SHIFT(0)));
2530 			} else {
2531 				unsigned int i;
2532 
2533 				/* Map all VCs from this port to VC(nport) */
2534 				for (i = 0; i < 8; i++)
2535 					ub960_rxport_write(priv, nport,
2536 							   UB960_RR_VC_ID_MAP(i),
2537 							   (nport << 4) | nport);
2538 			}
2539 
2540 			break;
2541 		}
2542 
2543 		if (rx_data[nport].tx_port == 1)
2544 			fwd_ctl |= BIT(nport); /* forward to TX1 */
2545 		else
2546 			fwd_ctl &= ~BIT(nport); /* forward to TX0 */
2547 	}
2548 
2549 	ub960_write(priv, UB960_SR_FWD_CTL1, fwd_ctl);
2550 
2551 	return 0;
2552 }
2553 
ub960_update_streaming_status(struct ub960_data * priv)2554 static void ub960_update_streaming_status(struct ub960_data *priv)
2555 {
2556 	unsigned int i;
2557 
2558 	for (i = 0; i < UB960_MAX_NPORTS; i++) {
2559 		if (priv->stream_enable_mask[i])
2560 			break;
2561 	}
2562 
2563 	priv->streaming = i < UB960_MAX_NPORTS;
2564 }
2565 
ub960_enable_streams(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,u32 source_pad,u64 source_streams_mask)2566 static int ub960_enable_streams(struct v4l2_subdev *sd,
2567 				struct v4l2_subdev_state *state, u32 source_pad,
2568 				u64 source_streams_mask)
2569 {
2570 	struct ub960_data *priv = sd_to_ub960(sd);
2571 	struct device *dev = &priv->client->dev;
2572 	u64 sink_streams[UB960_MAX_RX_NPORTS] = {};
2573 	struct v4l2_subdev_route *route;
2574 	unsigned int failed_port;
2575 	unsigned int nport;
2576 	int ret;
2577 
2578 	if (!priv->streaming) {
2579 		dev_dbg(dev, "Prepare for streaming\n");
2580 		ret = ub960_configure_ports_for_streaming(priv, state);
2581 		if (ret)
2582 			return ret;
2583 	}
2584 
2585 	/* Enable TX port if not yet enabled */
2586 	if (!priv->stream_enable_mask[source_pad]) {
2587 		ret = ub960_enable_tx_port(priv,
2588 					   ub960_pad_to_port(priv, source_pad));
2589 		if (ret)
2590 			return ret;
2591 	}
2592 
2593 	priv->stream_enable_mask[source_pad] |= source_streams_mask;
2594 
2595 	/* Collect sink streams per pad which we need to enable */
2596 	for_each_active_route(&state->routing, route) {
2597 		if (route->source_pad != source_pad)
2598 			continue;
2599 
2600 		if (!(source_streams_mask & BIT_ULL(route->source_stream)))
2601 			continue;
2602 
2603 		nport = ub960_pad_to_port(priv, route->sink_pad);
2604 
2605 		sink_streams[nport] |= BIT_ULL(route->sink_stream);
2606 	}
2607 
2608 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2609 		if (!sink_streams[nport])
2610 			continue;
2611 
2612 		/* Enable the RX port if not yet enabled */
2613 		if (!priv->stream_enable_mask[nport]) {
2614 			ret = ub960_enable_rx_port(priv, nport);
2615 			if (ret) {
2616 				failed_port = nport;
2617 				goto err;
2618 			}
2619 		}
2620 
2621 		priv->stream_enable_mask[nport] |= sink_streams[nport];
2622 
2623 		dev_dbg(dev, "enable RX port %u streams %#llx\n", nport,
2624 			sink_streams[nport]);
2625 
2626 		ret = v4l2_subdev_enable_streams(
2627 			priv->rxports[nport]->source.sd,
2628 			priv->rxports[nport]->source.pad,
2629 			sink_streams[nport]);
2630 		if (ret) {
2631 			priv->stream_enable_mask[nport] &= ~sink_streams[nport];
2632 
2633 			if (!priv->stream_enable_mask[nport])
2634 				ub960_disable_rx_port(priv, nport);
2635 
2636 			failed_port = nport;
2637 			goto err;
2638 		}
2639 	}
2640 
2641 	priv->streaming = true;
2642 
2643 	return 0;
2644 
2645 err:
2646 	for (nport = 0; nport < failed_port; nport++) {
2647 		if (!sink_streams[nport])
2648 			continue;
2649 
2650 		dev_dbg(dev, "disable RX port %u streams %#llx\n", nport,
2651 			sink_streams[nport]);
2652 
2653 		ret = v4l2_subdev_disable_streams(
2654 			priv->rxports[nport]->source.sd,
2655 			priv->rxports[nport]->source.pad,
2656 			sink_streams[nport]);
2657 		if (ret)
2658 			dev_err(dev, "Failed to disable streams: %d\n", ret);
2659 
2660 		priv->stream_enable_mask[nport] &= ~sink_streams[nport];
2661 
2662 		/* Disable RX port if no active streams */
2663 		if (!priv->stream_enable_mask[nport])
2664 			ub960_disable_rx_port(priv, nport);
2665 	}
2666 
2667 	priv->stream_enable_mask[source_pad] &= ~source_streams_mask;
2668 
2669 	if (!priv->stream_enable_mask[source_pad])
2670 		ub960_disable_tx_port(priv,
2671 				      ub960_pad_to_port(priv, source_pad));
2672 
2673 	ub960_update_streaming_status(priv);
2674 
2675 	return ret;
2676 }
2677 
ub960_disable_streams(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,u32 source_pad,u64 source_streams_mask)2678 static int ub960_disable_streams(struct v4l2_subdev *sd,
2679 				 struct v4l2_subdev_state *state,
2680 				 u32 source_pad, u64 source_streams_mask)
2681 {
2682 	struct ub960_data *priv = sd_to_ub960(sd);
2683 	struct device *dev = &priv->client->dev;
2684 	u64 sink_streams[UB960_MAX_RX_NPORTS] = {};
2685 	struct v4l2_subdev_route *route;
2686 	unsigned int nport;
2687 	int ret;
2688 
2689 	/* Collect sink streams per pad which we need to disable */
2690 	for_each_active_route(&state->routing, route) {
2691 		if (route->source_pad != source_pad)
2692 			continue;
2693 
2694 		if (!(source_streams_mask & BIT_ULL(route->source_stream)))
2695 			continue;
2696 
2697 		nport = ub960_pad_to_port(priv, route->sink_pad);
2698 
2699 		sink_streams[nport] |= BIT_ULL(route->sink_stream);
2700 	}
2701 
2702 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
2703 		if (!sink_streams[nport])
2704 			continue;
2705 
2706 		dev_dbg(dev, "disable RX port %u streams %#llx\n", nport,
2707 			sink_streams[nport]);
2708 
2709 		ret = v4l2_subdev_disable_streams(
2710 			priv->rxports[nport]->source.sd,
2711 			priv->rxports[nport]->source.pad,
2712 			sink_streams[nport]);
2713 		if (ret)
2714 			dev_err(dev, "Failed to disable streams: %d\n", ret);
2715 
2716 		priv->stream_enable_mask[nport] &= ~sink_streams[nport];
2717 
2718 		/* Disable RX port if no active streams */
2719 		if (!priv->stream_enable_mask[nport])
2720 			ub960_disable_rx_port(priv, nport);
2721 	}
2722 
2723 	/* Disable TX port if no active streams */
2724 
2725 	priv->stream_enable_mask[source_pad] &= ~source_streams_mask;
2726 
2727 	if (!priv->stream_enable_mask[source_pad])
2728 		ub960_disable_tx_port(priv,
2729 				      ub960_pad_to_port(priv, source_pad));
2730 
2731 	ub960_update_streaming_status(priv);
2732 
2733 	return 0;
2734 }
2735 
_ub960_set_routing(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_krouting * routing)2736 static int _ub960_set_routing(struct v4l2_subdev *sd,
2737 			      struct v4l2_subdev_state *state,
2738 			      struct v4l2_subdev_krouting *routing)
2739 {
2740 	static const struct v4l2_mbus_framefmt format = {
2741 		.width = 640,
2742 		.height = 480,
2743 		.code = MEDIA_BUS_FMT_UYVY8_1X16,
2744 		.field = V4L2_FIELD_NONE,
2745 		.colorspace = V4L2_COLORSPACE_SRGB,
2746 		.ycbcr_enc = V4L2_YCBCR_ENC_601,
2747 		.quantization = V4L2_QUANTIZATION_LIM_RANGE,
2748 		.xfer_func = V4L2_XFER_FUNC_SRGB,
2749 	};
2750 	int ret;
2751 
2752 	/*
2753 	 * Note: we can only support up to V4L2_FRAME_DESC_ENTRY_MAX, until
2754 	 * frame desc is made dynamically allocated.
2755 	 */
2756 
2757 	if (routing->num_routes > V4L2_FRAME_DESC_ENTRY_MAX)
2758 		return -E2BIG;
2759 
2760 	ret = v4l2_subdev_routing_validate(sd, routing,
2761 					   V4L2_SUBDEV_ROUTING_ONLY_1_TO_1 |
2762 					   V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX);
2763 	if (ret)
2764 		return ret;
2765 
2766 	ret = v4l2_subdev_set_routing_with_fmt(sd, state, routing, &format);
2767 	if (ret)
2768 		return ret;
2769 
2770 	return 0;
2771 }
2772 
ub960_set_routing(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,enum v4l2_subdev_format_whence which,struct v4l2_subdev_krouting * routing)2773 static int ub960_set_routing(struct v4l2_subdev *sd,
2774 			     struct v4l2_subdev_state *state,
2775 			     enum v4l2_subdev_format_whence which,
2776 			     struct v4l2_subdev_krouting *routing)
2777 {
2778 	struct ub960_data *priv = sd_to_ub960(sd);
2779 
2780 	if (which == V4L2_SUBDEV_FORMAT_ACTIVE && priv->streaming)
2781 		return -EBUSY;
2782 
2783 	return _ub960_set_routing(sd, state, routing);
2784 }
2785 
ub960_get_frame_desc(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_frame_desc * fd)2786 static int ub960_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
2787 				struct v4l2_mbus_frame_desc *fd)
2788 {
2789 	struct ub960_data *priv = sd_to_ub960(sd);
2790 	struct v4l2_subdev_route *route;
2791 	struct v4l2_subdev_state *state;
2792 	int ret = 0;
2793 	struct device *dev = &priv->client->dev;
2794 	u8 vc_map[UB960_MAX_RX_NPORTS] = {};
2795 
2796 	if (!ub960_pad_is_source(priv, pad))
2797 		return -EINVAL;
2798 
2799 	memset(fd, 0, sizeof(*fd));
2800 
2801 	fd->type = V4L2_MBUS_FRAME_DESC_TYPE_CSI2;
2802 
2803 	state = v4l2_subdev_lock_and_get_active_state(&priv->sd);
2804 
2805 	ub960_get_vc_maps(priv, state, vc_map);
2806 
2807 	for_each_active_route(&state->routing, route) {
2808 		struct v4l2_mbus_frame_desc_entry *source_entry = NULL;
2809 		struct v4l2_mbus_frame_desc source_fd;
2810 		unsigned int nport;
2811 		unsigned int i;
2812 
2813 		if (route->source_pad != pad)
2814 			continue;
2815 
2816 		nport = ub960_pad_to_port(priv, route->sink_pad);
2817 
2818 		ret = v4l2_subdev_call(priv->rxports[nport]->source.sd, pad,
2819 				       get_frame_desc,
2820 				       priv->rxports[nport]->source.pad,
2821 				       &source_fd);
2822 		if (ret) {
2823 			dev_err(dev,
2824 				"Failed to get source frame desc for pad %u\n",
2825 				route->sink_pad);
2826 			goto out_unlock;
2827 		}
2828 
2829 		for (i = 0; i < source_fd.num_entries; i++) {
2830 			if (source_fd.entry[i].stream == route->sink_stream) {
2831 				source_entry = &source_fd.entry[i];
2832 				break;
2833 			}
2834 		}
2835 
2836 		if (!source_entry) {
2837 			dev_err(dev,
2838 				"Failed to find stream from source frame desc\n");
2839 			ret = -EPIPE;
2840 			goto out_unlock;
2841 		}
2842 
2843 		fd->entry[fd->num_entries].stream = route->source_stream;
2844 		fd->entry[fd->num_entries].flags = source_entry->flags;
2845 		fd->entry[fd->num_entries].length = source_entry->length;
2846 		fd->entry[fd->num_entries].pixelcode = source_entry->pixelcode;
2847 
2848 		fd->entry[fd->num_entries].bus.csi2.vc = vc_map[nport];
2849 
2850 		if (source_fd.type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
2851 			fd->entry[fd->num_entries].bus.csi2.dt =
2852 				source_entry->bus.csi2.dt;
2853 		} else {
2854 			const struct ub960_format_info *ub960_fmt;
2855 			struct v4l2_mbus_framefmt *fmt;
2856 
2857 			fmt = v4l2_subdev_state_get_stream_format(state, pad,
2858 								  route->source_stream);
2859 
2860 			if (!fmt) {
2861 				ret = -EINVAL;
2862 				goto out_unlock;
2863 			}
2864 
2865 			ub960_fmt = ub960_find_format(fmt->code);
2866 			if (!ub960_fmt) {
2867 				dev_err(dev, "Unable to find format\n");
2868 				ret = -EINVAL;
2869 				goto out_unlock;
2870 			}
2871 
2872 			fd->entry[fd->num_entries].bus.csi2.dt =
2873 				ub960_fmt->datatype;
2874 		}
2875 
2876 		fd->num_entries++;
2877 	}
2878 
2879 out_unlock:
2880 	v4l2_subdev_unlock_state(state);
2881 
2882 	return ret;
2883 }
2884 
ub960_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * state,struct v4l2_subdev_format * format)2885 static int ub960_set_fmt(struct v4l2_subdev *sd,
2886 			 struct v4l2_subdev_state *state,
2887 			 struct v4l2_subdev_format *format)
2888 {
2889 	struct ub960_data *priv = sd_to_ub960(sd);
2890 	struct v4l2_mbus_framefmt *fmt;
2891 
2892 	if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE && priv->streaming)
2893 		return -EBUSY;
2894 
2895 	/* No transcoding, source and sink formats must match. */
2896 	if (ub960_pad_is_source(priv, format->pad))
2897 		return v4l2_subdev_get_fmt(sd, state, format);
2898 
2899 	/*
2900 	 * Default to the first format if the requested media bus code isn't
2901 	 * supported.
2902 	 */
2903 	if (!ub960_find_format(format->format.code))
2904 		format->format.code = ub960_formats[0].code;
2905 
2906 	fmt = v4l2_subdev_state_get_stream_format(state, format->pad,
2907 						  format->stream);
2908 	if (!fmt)
2909 		return -EINVAL;
2910 
2911 	*fmt = format->format;
2912 
2913 	fmt = v4l2_subdev_state_get_opposite_stream_format(state, format->pad,
2914 							   format->stream);
2915 	if (!fmt)
2916 		return -EINVAL;
2917 
2918 	*fmt = format->format;
2919 
2920 	return 0;
2921 }
2922 
ub960_init_cfg(struct v4l2_subdev * sd,struct v4l2_subdev_state * state)2923 static int ub960_init_cfg(struct v4l2_subdev *sd,
2924 			  struct v4l2_subdev_state *state)
2925 {
2926 	struct ub960_data *priv = sd_to_ub960(sd);
2927 
2928 	struct v4l2_subdev_route routes[] = {
2929 		{
2930 			.sink_pad = 0,
2931 			.sink_stream = 0,
2932 			.source_pad = priv->hw_data->num_rxports,
2933 			.source_stream = 0,
2934 			.flags = V4L2_SUBDEV_ROUTE_FL_ACTIVE,
2935 		},
2936 	};
2937 
2938 	struct v4l2_subdev_krouting routing = {
2939 		.num_routes = ARRAY_SIZE(routes),
2940 		.routes = routes,
2941 	};
2942 
2943 	return _ub960_set_routing(sd, state, &routing);
2944 }
2945 
2946 static const struct v4l2_subdev_pad_ops ub960_pad_ops = {
2947 	.enable_streams = ub960_enable_streams,
2948 	.disable_streams = ub960_disable_streams,
2949 
2950 	.set_routing = ub960_set_routing,
2951 	.get_frame_desc = ub960_get_frame_desc,
2952 
2953 	.get_fmt = v4l2_subdev_get_fmt,
2954 	.set_fmt = ub960_set_fmt,
2955 
2956 	.init_cfg = ub960_init_cfg,
2957 };
2958 
ub960_log_status_ub960_sp_eq(struct ub960_data * priv,unsigned int nport)2959 static void ub960_log_status_ub960_sp_eq(struct ub960_data *priv,
2960 					 unsigned int nport)
2961 {
2962 	struct device *dev = &priv->client->dev;
2963 	u8 eq_level;
2964 	s8 strobe_pos;
2965 	u8 v = 0;
2966 
2967 	/* Strobe */
2968 
2969 	ub960_read(priv, UB960_XR_AEQ_CTL1, &v);
2970 
2971 	dev_info(dev, "\t%s strobe\n",
2972 		 (v & UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN) ? "Adaptive" :
2973 							  "Manual");
2974 
2975 	if (v & UB960_XR_AEQ_CTL1_AEQ_SFILTER_EN) {
2976 		ub960_read(priv, UB960_XR_SFILTER_CFG, &v);
2977 
2978 		dev_info(dev, "\tStrobe range [%d, %d]\n",
2979 			 ((v >> UB960_XR_SFILTER_CFG_SFILTER_MIN_SHIFT) & 0xf) - 7,
2980 			 ((v >> UB960_XR_SFILTER_CFG_SFILTER_MAX_SHIFT) & 0xf) - 7);
2981 	}
2982 
2983 	ub960_rxport_get_strobe_pos(priv, nport, &strobe_pos);
2984 
2985 	dev_info(dev, "\tStrobe pos %d\n", strobe_pos);
2986 
2987 	/* EQ */
2988 
2989 	ub960_rxport_read(priv, nport, UB960_RR_AEQ_BYPASS, &v);
2990 
2991 	dev_info(dev, "\t%s EQ\n",
2992 		 (v & UB960_RR_AEQ_BYPASS_ENABLE) ? "Manual" :
2993 						    "Adaptive");
2994 
2995 	if (!(v & UB960_RR_AEQ_BYPASS_ENABLE)) {
2996 		ub960_rxport_read(priv, nport, UB960_RR_AEQ_MIN_MAX, &v);
2997 
2998 		dev_info(dev, "\tEQ range [%u, %u]\n",
2999 			 (v >> UB960_RR_AEQ_MIN_MAX_AEQ_FLOOR_SHIFT) & 0xf,
3000 			 (v >> UB960_RR_AEQ_MIN_MAX_AEQ_MAX_SHIFT) & 0xf);
3001 	}
3002 
3003 	if (ub960_rxport_get_eq_level(priv, nport, &eq_level) == 0)
3004 		dev_info(dev, "\tEQ level %u\n", eq_level);
3005 }
3006 
ub960_log_status(struct v4l2_subdev * sd)3007 static int ub960_log_status(struct v4l2_subdev *sd)
3008 {
3009 	struct ub960_data *priv = sd_to_ub960(sd);
3010 	struct device *dev = &priv->client->dev;
3011 	struct v4l2_subdev_state *state;
3012 	unsigned int nport;
3013 	unsigned int i;
3014 	u16 v16 = 0;
3015 	u8 v = 0;
3016 	u8 id[UB960_SR_FPD3_RX_ID_LEN];
3017 
3018 	state = v4l2_subdev_lock_and_get_active_state(sd);
3019 
3020 	for (i = 0; i < sizeof(id); i++)
3021 		ub960_read(priv, UB960_SR_FPD3_RX_ID(i), &id[i]);
3022 
3023 	dev_info(dev, "ID '%.*s'\n", (int)sizeof(id), id);
3024 
3025 	for (nport = 0; nport < priv->hw_data->num_txports; nport++) {
3026 		struct ub960_txport *txport = priv->txports[nport];
3027 
3028 		dev_info(dev, "TX %u\n", nport);
3029 
3030 		if (!txport) {
3031 			dev_info(dev, "\tNot initialized\n");
3032 			continue;
3033 		}
3034 
3035 		ub960_txport_read(priv, nport, UB960_TR_CSI_STS, &v);
3036 		dev_info(dev, "\tsync %u, pass %u\n", v & (u8)BIT(1),
3037 			 v & (u8)BIT(0));
3038 
3039 		ub960_read16(priv, UB960_SR_CSI_FRAME_COUNT_HI(nport), &v16);
3040 		dev_info(dev, "\tframe counter %u\n", v16);
3041 
3042 		ub960_read16(priv, UB960_SR_CSI_FRAME_ERR_COUNT_HI(nport), &v16);
3043 		dev_info(dev, "\tframe error counter %u\n", v16);
3044 
3045 		ub960_read16(priv, UB960_SR_CSI_LINE_COUNT_HI(nport), &v16);
3046 		dev_info(dev, "\tline counter %u\n", v16);
3047 
3048 		ub960_read16(priv, UB960_SR_CSI_LINE_ERR_COUNT_HI(nport), &v16);
3049 		dev_info(dev, "\tline error counter %u\n", v16);
3050 	}
3051 
3052 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
3053 		struct ub960_rxport *rxport = priv->rxports[nport];
3054 		unsigned int i;
3055 
3056 		dev_info(dev, "RX %u\n", nport);
3057 
3058 		if (!rxport) {
3059 			dev_info(dev, "\tNot initialized\n");
3060 			continue;
3061 		}
3062 
3063 		ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS1, &v);
3064 
3065 		if (v & UB960_RR_RX_PORT_STS1_LOCK_STS)
3066 			dev_info(dev, "\tLocked\n");
3067 		else
3068 			dev_info(dev, "\tNot locked\n");
3069 
3070 		dev_info(dev, "\trx_port_sts1 %#02x\n", v);
3071 		ub960_rxport_read(priv, nport, UB960_RR_RX_PORT_STS2, &v);
3072 		dev_info(dev, "\trx_port_sts2 %#02x\n", v);
3073 
3074 		ub960_rxport_read16(priv, nport, UB960_RR_RX_FREQ_HIGH, &v16);
3075 		dev_info(dev, "\tlink freq %llu Hz\n", (v16 * 1000000ULL) >> 8);
3076 
3077 		ub960_rxport_read16(priv, nport, UB960_RR_RX_PAR_ERR_HI, &v16);
3078 		dev_info(dev, "\tparity errors %u\n", v16);
3079 
3080 		ub960_rxport_read16(priv, nport, UB960_RR_LINE_COUNT_HI, &v16);
3081 		dev_info(dev, "\tlines per frame %u\n", v16);
3082 
3083 		ub960_rxport_read16(priv, nport, UB960_RR_LINE_LEN_1, &v16);
3084 		dev_info(dev, "\tbytes per line %u\n", v16);
3085 
3086 		ub960_rxport_read(priv, nport, UB960_RR_CSI_ERR_COUNTER, &v);
3087 		dev_info(dev, "\tcsi_err_counter %u\n", v);
3088 
3089 		if (!priv->hw_data->is_ub9702)
3090 			ub960_log_status_ub960_sp_eq(priv, nport);
3091 
3092 		/* GPIOs */
3093 		for (i = 0; i < UB960_NUM_BC_GPIOS; i++) {
3094 			u8 ctl_reg;
3095 			u8 ctl_shift;
3096 
3097 			ctl_reg = UB960_RR_BC_GPIO_CTL(i / 2);
3098 			ctl_shift = (i % 2) * 4;
3099 
3100 			ub960_rxport_read(priv, nport, ctl_reg, &v);
3101 
3102 			dev_info(dev, "\tGPIO%u: mode %u\n", i,
3103 				 (v >> ctl_shift) & 0xf);
3104 		}
3105 	}
3106 
3107 	v4l2_subdev_unlock_state(state);
3108 
3109 	return 0;
3110 }
3111 
3112 static const struct v4l2_subdev_core_ops ub960_subdev_core_ops = {
3113 	.log_status = ub960_log_status,
3114 	.subscribe_event = v4l2_ctrl_subdev_subscribe_event,
3115 	.unsubscribe_event = v4l2_event_subdev_unsubscribe,
3116 };
3117 
3118 static const struct v4l2_subdev_ops ub960_subdev_ops = {
3119 	.core = &ub960_subdev_core_ops,
3120 	.pad = &ub960_pad_ops,
3121 };
3122 
3123 static const struct media_entity_operations ub960_entity_ops = {
3124 	.get_fwnode_pad = v4l2_subdev_get_fwnode_pad_1_to_1,
3125 	.link_validate = v4l2_subdev_link_validate,
3126 	.has_pad_interdep = v4l2_subdev_has_pad_interdep,
3127 };
3128 
3129 /* -----------------------------------------------------------------------------
3130  * Core
3131  */
3132 
ub960_handle_events(int irq,void * arg)3133 static irqreturn_t ub960_handle_events(int irq, void *arg)
3134 {
3135 	struct ub960_data *priv = arg;
3136 	unsigned int i;
3137 	u8 int_sts;
3138 	u8 fwd_sts;
3139 	int ret;
3140 
3141 	ret = ub960_read(priv, UB960_SR_INTERRUPT_STS, &int_sts);
3142 	if (ret || !int_sts)
3143 		return IRQ_NONE;
3144 
3145 	dev_dbg(&priv->client->dev, "INTERRUPT_STS %x\n", int_sts);
3146 
3147 	ret = ub960_read(priv, UB960_SR_FWD_STS, &fwd_sts);
3148 	if (ret)
3149 		return IRQ_NONE;
3150 
3151 	dev_dbg(&priv->client->dev, "FWD_STS %#02x\n", fwd_sts);
3152 
3153 	for (i = 0; i < priv->hw_data->num_txports; i++) {
3154 		if (int_sts & UB960_SR_INTERRUPT_STS_IS_CSI_TX(i))
3155 			ub960_csi_handle_events(priv, i);
3156 	}
3157 
3158 	for (i = 0; i < priv->hw_data->num_rxports; i++) {
3159 		if (!priv->rxports[i])
3160 			continue;
3161 
3162 		if (int_sts & UB960_SR_INTERRUPT_STS_IS_RX(i))
3163 			ub960_rxport_handle_events(priv, i);
3164 	}
3165 
3166 	return IRQ_HANDLED;
3167 }
3168 
ub960_handler_work(struct work_struct * work)3169 static void ub960_handler_work(struct work_struct *work)
3170 {
3171 	struct delayed_work *dwork = to_delayed_work(work);
3172 	struct ub960_data *priv =
3173 		container_of(dwork, struct ub960_data, poll_work);
3174 
3175 	ub960_handle_events(0, priv);
3176 
3177 	schedule_delayed_work(&priv->poll_work,
3178 			      msecs_to_jiffies(UB960_POLL_TIME_MS));
3179 }
3180 
ub960_txport_free_ports(struct ub960_data * priv)3181 static void ub960_txport_free_ports(struct ub960_data *priv)
3182 {
3183 	unsigned int nport;
3184 
3185 	for (nport = 0; nport < priv->hw_data->num_txports; nport++) {
3186 		struct ub960_txport *txport = priv->txports[nport];
3187 
3188 		if (!txport)
3189 			continue;
3190 
3191 		kfree(txport);
3192 		priv->txports[nport] = NULL;
3193 	}
3194 }
3195 
ub960_rxport_free_ports(struct ub960_data * priv)3196 static void ub960_rxport_free_ports(struct ub960_data *priv)
3197 {
3198 	unsigned int nport;
3199 
3200 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
3201 		struct ub960_rxport *rxport = priv->rxports[nport];
3202 
3203 		if (!rxport)
3204 			continue;
3205 
3206 		fwnode_handle_put(rxport->source.ep_fwnode);
3207 		fwnode_handle_put(rxport->ser.fwnode);
3208 
3209 		kfree(rxport);
3210 		priv->rxports[nport] = NULL;
3211 	}
3212 }
3213 
3214 static int
ub960_parse_dt_rxport_link_properties(struct ub960_data * priv,struct fwnode_handle * link_fwnode,struct ub960_rxport * rxport)3215 ub960_parse_dt_rxport_link_properties(struct ub960_data *priv,
3216 				      struct fwnode_handle *link_fwnode,
3217 				      struct ub960_rxport *rxport)
3218 {
3219 	struct device *dev = &priv->client->dev;
3220 	unsigned int nport = rxport->nport;
3221 	u32 rx_mode;
3222 	u32 cdr_mode;
3223 	s32 strobe_pos;
3224 	u32 eq_level;
3225 	u32 ser_i2c_alias;
3226 	int ret;
3227 
3228 	cdr_mode = RXPORT_CDR_FPD3;
3229 
3230 	ret = fwnode_property_read_u32(link_fwnode, "ti,cdr-mode", &cdr_mode);
3231 	if (ret < 0 && ret != -EINVAL) {
3232 		dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
3233 			"ti,cdr-mode", ret);
3234 		return ret;
3235 	}
3236 
3237 	if (cdr_mode > RXPORT_CDR_LAST) {
3238 		dev_err(dev, "rx%u: bad 'ti,cdr-mode' %u\n", nport, cdr_mode);
3239 		return -EINVAL;
3240 	}
3241 
3242 	if (!priv->hw_data->is_fpdlink4 && cdr_mode == RXPORT_CDR_FPD4) {
3243 		dev_err(dev, "rx%u: FPD-Link 4 CDR not supported\n", nport);
3244 		return -EINVAL;
3245 	}
3246 
3247 	rxport->cdr_mode = cdr_mode;
3248 
3249 	ret = fwnode_property_read_u32(link_fwnode, "ti,rx-mode", &rx_mode);
3250 	if (ret < 0) {
3251 		dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
3252 			"ti,rx-mode", ret);
3253 		return ret;
3254 	}
3255 
3256 	if (rx_mode > RXPORT_MODE_LAST) {
3257 		dev_err(dev, "rx%u: bad 'ti,rx-mode' %u\n", nport, rx_mode);
3258 		return -EINVAL;
3259 	}
3260 
3261 	switch (rx_mode) {
3262 	case RXPORT_MODE_RAW12_HF:
3263 	case RXPORT_MODE_RAW12_LF:
3264 		dev_err(dev, "rx%u: unsupported 'ti,rx-mode' %u\n", nport,
3265 			rx_mode);
3266 		return -EINVAL;
3267 	default:
3268 		break;
3269 	}
3270 
3271 	rxport->rx_mode = rx_mode;
3272 
3273 	/* EQ & Strobe related */
3274 
3275 	/* Defaults */
3276 	rxport->eq.manual_eq = false;
3277 	rxport->eq.aeq.eq_level_min = UB960_MIN_EQ_LEVEL;
3278 	rxport->eq.aeq.eq_level_max = UB960_MAX_EQ_LEVEL;
3279 
3280 	ret = fwnode_property_read_u32(link_fwnode, "ti,strobe-pos",
3281 				       &strobe_pos);
3282 	if (ret) {
3283 		if (ret != -EINVAL) {
3284 			dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
3285 				"ti,strobe-pos", ret);
3286 			return ret;
3287 		}
3288 	} else {
3289 		if (strobe_pos < UB960_MIN_MANUAL_STROBE_POS ||
3290 		    strobe_pos > UB960_MAX_MANUAL_STROBE_POS) {
3291 			dev_err(dev, "rx%u: illegal 'strobe-pos' value: %d\n",
3292 				nport, strobe_pos);
3293 			return -EINVAL;
3294 		}
3295 
3296 		/* NOTE: ignored unless global manual strobe pos is also set */
3297 		rxport->eq.strobe_pos = strobe_pos;
3298 		if (!priv->strobe.manual)
3299 			dev_warn(dev,
3300 				 "rx%u: 'ti,strobe-pos' ignored as 'ti,manual-strobe' not set\n",
3301 				 nport);
3302 	}
3303 
3304 	ret = fwnode_property_read_u32(link_fwnode, "ti,eq-level", &eq_level);
3305 	if (ret) {
3306 		if (ret != -EINVAL) {
3307 			dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
3308 				"ti,eq-level", ret);
3309 			return ret;
3310 		}
3311 	} else {
3312 		if (eq_level > UB960_MAX_EQ_LEVEL) {
3313 			dev_err(dev, "rx%u: illegal 'ti,eq-level' value: %d\n",
3314 				nport, eq_level);
3315 			return -EINVAL;
3316 		}
3317 
3318 		rxport->eq.manual_eq = true;
3319 		rxport->eq.manual.eq_level = eq_level;
3320 	}
3321 
3322 	ret = fwnode_property_read_u32(link_fwnode, "i2c-alias",
3323 				       &ser_i2c_alias);
3324 	if (ret) {
3325 		dev_err(dev, "rx%u: failed to read '%s': %d\n", nport,
3326 			"i2c-alias", ret);
3327 		return ret;
3328 	}
3329 	rxport->ser.alias = ser_i2c_alias;
3330 
3331 	rxport->ser.fwnode = fwnode_get_named_child_node(link_fwnode, "serializer");
3332 	if (!rxport->ser.fwnode) {
3333 		dev_err(dev, "rx%u: missing 'serializer' node\n", nport);
3334 		return -EINVAL;
3335 	}
3336 
3337 	return 0;
3338 }
3339 
ub960_parse_dt_rxport_ep_properties(struct ub960_data * priv,struct fwnode_handle * ep_fwnode,struct ub960_rxport * rxport)3340 static int ub960_parse_dt_rxport_ep_properties(struct ub960_data *priv,
3341 					       struct fwnode_handle *ep_fwnode,
3342 					       struct ub960_rxport *rxport)
3343 {
3344 	struct device *dev = &priv->client->dev;
3345 	struct v4l2_fwnode_endpoint vep = {};
3346 	unsigned int nport = rxport->nport;
3347 	bool hsync_hi;
3348 	bool vsync_hi;
3349 	int ret;
3350 
3351 	rxport->source.ep_fwnode = fwnode_graph_get_remote_endpoint(ep_fwnode);
3352 	if (!rxport->source.ep_fwnode) {
3353 		dev_err(dev, "rx%u: no remote endpoint\n", nport);
3354 		return -ENODEV;
3355 	}
3356 
3357 	/* We currently have properties only for RAW modes */
3358 
3359 	switch (rxport->rx_mode) {
3360 	case RXPORT_MODE_RAW10:
3361 	case RXPORT_MODE_RAW12_HF:
3362 	case RXPORT_MODE_RAW12_LF:
3363 		break;
3364 	default:
3365 		return 0;
3366 	}
3367 
3368 	vep.bus_type = V4L2_MBUS_PARALLEL;
3369 	ret = v4l2_fwnode_endpoint_parse(ep_fwnode, &vep);
3370 	if (ret) {
3371 		dev_err(dev, "rx%u: failed to parse endpoint data\n", nport);
3372 		goto err_put_source_ep_fwnode;
3373 	}
3374 
3375 	hsync_hi = !!(vep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH);
3376 	vsync_hi = !!(vep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH);
3377 
3378 	/* LineValid and FrameValid are inverse to the h/vsync active */
3379 	rxport->lv_fv_pol = (hsync_hi ? UB960_RR_PORT_CONFIG2_LV_POL_LOW : 0) |
3380 			    (vsync_hi ? UB960_RR_PORT_CONFIG2_FV_POL_LOW : 0);
3381 
3382 	return 0;
3383 
3384 err_put_source_ep_fwnode:
3385 	fwnode_handle_put(rxport->source.ep_fwnode);
3386 	return ret;
3387 }
3388 
ub960_parse_dt_rxport(struct ub960_data * priv,unsigned int nport,struct fwnode_handle * link_fwnode,struct fwnode_handle * ep_fwnode)3389 static int ub960_parse_dt_rxport(struct ub960_data *priv, unsigned int nport,
3390 				 struct fwnode_handle *link_fwnode,
3391 				 struct fwnode_handle *ep_fwnode)
3392 {
3393 	static const char *vpoc_names[UB960_MAX_RX_NPORTS] = {
3394 		"vpoc0", "vpoc1", "vpoc2", "vpoc3"
3395 	};
3396 	struct device *dev = &priv->client->dev;
3397 	struct ub960_rxport *rxport;
3398 	int ret;
3399 
3400 	rxport = kzalloc(sizeof(*rxport), GFP_KERNEL);
3401 	if (!rxport)
3402 		return -ENOMEM;
3403 
3404 	priv->rxports[nport] = rxport;
3405 
3406 	rxport->nport = nport;
3407 	rxport->priv = priv;
3408 
3409 	ret = ub960_parse_dt_rxport_link_properties(priv, link_fwnode, rxport);
3410 	if (ret)
3411 		goto err_free_rxport;
3412 
3413 	rxport->vpoc = devm_regulator_get_optional(dev, vpoc_names[nport]);
3414 	if (IS_ERR(rxport->vpoc)) {
3415 		ret = PTR_ERR(rxport->vpoc);
3416 		if (ret == -ENODEV) {
3417 			rxport->vpoc = NULL;
3418 		} else {
3419 			dev_err(dev, "rx%u: failed to get VPOC supply: %d\n",
3420 				nport, ret);
3421 			goto err_put_remote_fwnode;
3422 		}
3423 	}
3424 
3425 	ret = ub960_parse_dt_rxport_ep_properties(priv, ep_fwnode, rxport);
3426 	if (ret)
3427 		goto err_put_remote_fwnode;
3428 
3429 	return 0;
3430 
3431 err_put_remote_fwnode:
3432 	fwnode_handle_put(rxport->ser.fwnode);
3433 err_free_rxport:
3434 	priv->rxports[nport] = NULL;
3435 	kfree(rxport);
3436 	return ret;
3437 }
3438 
3439 static struct fwnode_handle *
ub960_fwnode_get_link_by_regs(struct fwnode_handle * links_fwnode,unsigned int nport)3440 ub960_fwnode_get_link_by_regs(struct fwnode_handle *links_fwnode,
3441 			      unsigned int nport)
3442 {
3443 	struct fwnode_handle *link_fwnode;
3444 	int ret;
3445 
3446 	fwnode_for_each_child_node(links_fwnode, link_fwnode) {
3447 		u32 link_num;
3448 
3449 		if (!str_has_prefix(fwnode_get_name(link_fwnode), "link@"))
3450 			continue;
3451 
3452 		ret = fwnode_property_read_u32(link_fwnode, "reg", &link_num);
3453 		if (ret) {
3454 			fwnode_handle_put(link_fwnode);
3455 			return NULL;
3456 		}
3457 
3458 		if (nport == link_num)
3459 			return link_fwnode;
3460 	}
3461 
3462 	return NULL;
3463 }
3464 
ub960_parse_dt_rxports(struct ub960_data * priv)3465 static int ub960_parse_dt_rxports(struct ub960_data *priv)
3466 {
3467 	struct device *dev = &priv->client->dev;
3468 	struct fwnode_handle *links_fwnode;
3469 	unsigned int nport;
3470 	int ret;
3471 
3472 	links_fwnode = fwnode_get_named_child_node(dev_fwnode(dev), "links");
3473 	if (!links_fwnode) {
3474 		dev_err(dev, "'links' node missing\n");
3475 		return -ENODEV;
3476 	}
3477 
3478 	/* Defaults, recommended by TI */
3479 	priv->strobe.min = 2;
3480 	priv->strobe.max = 3;
3481 
3482 	priv->strobe.manual = fwnode_property_read_bool(links_fwnode, "ti,manual-strobe");
3483 
3484 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
3485 		struct fwnode_handle *link_fwnode;
3486 		struct fwnode_handle *ep_fwnode;
3487 
3488 		link_fwnode = ub960_fwnode_get_link_by_regs(links_fwnode, nport);
3489 		if (!link_fwnode)
3490 			continue;
3491 
3492 		ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
3493 							    nport, 0, 0);
3494 		if (!ep_fwnode) {
3495 			fwnode_handle_put(link_fwnode);
3496 			continue;
3497 		}
3498 
3499 		ret = ub960_parse_dt_rxport(priv, nport, link_fwnode,
3500 					    ep_fwnode);
3501 
3502 		fwnode_handle_put(link_fwnode);
3503 		fwnode_handle_put(ep_fwnode);
3504 
3505 		if (ret) {
3506 			dev_err(dev, "rx%u: failed to parse RX port\n", nport);
3507 			goto err_put_links;
3508 		}
3509 	}
3510 
3511 	fwnode_handle_put(links_fwnode);
3512 
3513 	return 0;
3514 
3515 err_put_links:
3516 	fwnode_handle_put(links_fwnode);
3517 
3518 	return ret;
3519 }
3520 
ub960_parse_dt_txports(struct ub960_data * priv)3521 static int ub960_parse_dt_txports(struct ub960_data *priv)
3522 {
3523 	struct device *dev = &priv->client->dev;
3524 	u32 nport;
3525 	int ret;
3526 
3527 	for (nport = 0; nport < priv->hw_data->num_txports; nport++) {
3528 		unsigned int port = nport + priv->hw_data->num_rxports;
3529 		struct fwnode_handle *ep_fwnode;
3530 
3531 		ep_fwnode = fwnode_graph_get_endpoint_by_id(dev_fwnode(dev),
3532 							    port, 0, 0);
3533 		if (!ep_fwnode)
3534 			continue;
3535 
3536 		ret = ub960_parse_dt_txport(priv, ep_fwnode, nport);
3537 
3538 		fwnode_handle_put(ep_fwnode);
3539 
3540 		if (ret)
3541 			break;
3542 	}
3543 
3544 	return 0;
3545 }
3546 
ub960_parse_dt(struct ub960_data * priv)3547 static int ub960_parse_dt(struct ub960_data *priv)
3548 {
3549 	int ret;
3550 
3551 	ret = ub960_parse_dt_rxports(priv);
3552 	if (ret)
3553 		return ret;
3554 
3555 	ret = ub960_parse_dt_txports(priv);
3556 	if (ret)
3557 		goto err_free_rxports;
3558 
3559 	return 0;
3560 
3561 err_free_rxports:
3562 	ub960_rxport_free_ports(priv);
3563 
3564 	return ret;
3565 }
3566 
ub960_notify_bound(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_connection * asd)3567 static int ub960_notify_bound(struct v4l2_async_notifier *notifier,
3568 			      struct v4l2_subdev *subdev,
3569 			      struct v4l2_async_connection *asd)
3570 {
3571 	struct ub960_data *priv = sd_to_ub960(notifier->sd);
3572 	struct ub960_rxport *rxport = to_ub960_asd(asd)->rxport;
3573 	struct device *dev = &priv->client->dev;
3574 	u8 nport = rxport->nport;
3575 	unsigned int i;
3576 	int ret;
3577 
3578 	ret = media_entity_get_fwnode_pad(&subdev->entity,
3579 					  rxport->source.ep_fwnode,
3580 					  MEDIA_PAD_FL_SOURCE);
3581 	if (ret < 0) {
3582 		dev_err(dev, "Failed to find pad for %s\n", subdev->name);
3583 		return ret;
3584 	}
3585 
3586 	rxport->source.sd = subdev;
3587 	rxport->source.pad = ret;
3588 
3589 	ret = media_create_pad_link(&rxport->source.sd->entity,
3590 				    rxport->source.pad, &priv->sd.entity, nport,
3591 				    MEDIA_LNK_FL_ENABLED |
3592 					    MEDIA_LNK_FL_IMMUTABLE);
3593 	if (ret) {
3594 		dev_err(dev, "Unable to link %s:%u -> %s:%u\n",
3595 			rxport->source.sd->name, rxport->source.pad,
3596 			priv->sd.name, nport);
3597 		return ret;
3598 	}
3599 
3600 	for (i = 0; i < priv->hw_data->num_rxports; i++) {
3601 		if (priv->rxports[i] && !priv->rxports[i]->source.sd) {
3602 			dev_dbg(dev, "Waiting for more subdevs to be bound\n");
3603 			return 0;
3604 		}
3605 	}
3606 
3607 	return 0;
3608 }
3609 
ub960_notify_unbind(struct v4l2_async_notifier * notifier,struct v4l2_subdev * subdev,struct v4l2_async_connection * asd)3610 static void ub960_notify_unbind(struct v4l2_async_notifier *notifier,
3611 				struct v4l2_subdev *subdev,
3612 				struct v4l2_async_connection *asd)
3613 {
3614 	struct ub960_rxport *rxport = to_ub960_asd(asd)->rxport;
3615 
3616 	rxport->source.sd = NULL;
3617 }
3618 
3619 static const struct v4l2_async_notifier_operations ub960_notify_ops = {
3620 	.bound = ub960_notify_bound,
3621 	.unbind = ub960_notify_unbind,
3622 };
3623 
ub960_v4l2_notifier_register(struct ub960_data * priv)3624 static int ub960_v4l2_notifier_register(struct ub960_data *priv)
3625 {
3626 	struct device *dev = &priv->client->dev;
3627 	unsigned int i;
3628 	int ret;
3629 
3630 	v4l2_async_subdev_nf_init(&priv->notifier, &priv->sd);
3631 
3632 	for (i = 0; i < priv->hw_data->num_rxports; i++) {
3633 		struct ub960_rxport *rxport = priv->rxports[i];
3634 		struct ub960_asd *asd;
3635 
3636 		if (!rxport)
3637 			continue;
3638 
3639 		asd = v4l2_async_nf_add_fwnode(&priv->notifier,
3640 					       rxport->source.ep_fwnode,
3641 					       struct ub960_asd);
3642 		if (IS_ERR(asd)) {
3643 			dev_err(dev, "Failed to add subdev for source %u: %pe",
3644 				i, asd);
3645 			v4l2_async_nf_cleanup(&priv->notifier);
3646 			return PTR_ERR(asd);
3647 		}
3648 
3649 		asd->rxport = rxport;
3650 	}
3651 
3652 	priv->notifier.ops = &ub960_notify_ops;
3653 
3654 	ret = v4l2_async_nf_register(&priv->notifier);
3655 	if (ret) {
3656 		dev_err(dev, "Failed to register subdev_notifier");
3657 		v4l2_async_nf_cleanup(&priv->notifier);
3658 		return ret;
3659 	}
3660 
3661 	return 0;
3662 }
3663 
ub960_v4l2_notifier_unregister(struct ub960_data * priv)3664 static void ub960_v4l2_notifier_unregister(struct ub960_data *priv)
3665 {
3666 	v4l2_async_nf_unregister(&priv->notifier);
3667 	v4l2_async_nf_cleanup(&priv->notifier);
3668 }
3669 
ub960_create_subdev(struct ub960_data * priv)3670 static int ub960_create_subdev(struct ub960_data *priv)
3671 {
3672 	struct device *dev = &priv->client->dev;
3673 	unsigned int i;
3674 	int ret;
3675 
3676 	v4l2_i2c_subdev_init(&priv->sd, priv->client, &ub960_subdev_ops);
3677 
3678 	v4l2_ctrl_handler_init(&priv->ctrl_handler, 1);
3679 	priv->sd.ctrl_handler = &priv->ctrl_handler;
3680 
3681 	v4l2_ctrl_new_int_menu(&priv->ctrl_handler, NULL, V4L2_CID_LINK_FREQ,
3682 			       ARRAY_SIZE(priv->tx_link_freq) - 1, 0,
3683 			       priv->tx_link_freq);
3684 
3685 	if (priv->ctrl_handler.error) {
3686 		ret = priv->ctrl_handler.error;
3687 		goto err_free_ctrl;
3688 	}
3689 
3690 	priv->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3691 			  V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_STREAMS;
3692 	priv->sd.entity.function = MEDIA_ENT_F_VID_IF_BRIDGE;
3693 	priv->sd.entity.ops = &ub960_entity_ops;
3694 
3695 	for (i = 0; i < priv->hw_data->num_rxports + priv->hw_data->num_txports; i++) {
3696 		priv->pads[i].flags = ub960_pad_is_sink(priv, i) ?
3697 					      MEDIA_PAD_FL_SINK :
3698 					      MEDIA_PAD_FL_SOURCE;
3699 	}
3700 
3701 	ret = media_entity_pads_init(&priv->sd.entity,
3702 				     priv->hw_data->num_rxports +
3703 					     priv->hw_data->num_txports,
3704 				     priv->pads);
3705 	if (ret)
3706 		goto err_free_ctrl;
3707 
3708 	priv->sd.state_lock = priv->sd.ctrl_handler->lock;
3709 
3710 	ret = v4l2_subdev_init_finalize(&priv->sd);
3711 	if (ret)
3712 		goto err_entity_cleanup;
3713 
3714 	ret = ub960_v4l2_notifier_register(priv);
3715 	if (ret) {
3716 		dev_err(dev, "v4l2 subdev notifier register failed: %d\n", ret);
3717 		goto err_subdev_cleanup;
3718 	}
3719 
3720 	ret = v4l2_async_register_subdev(&priv->sd);
3721 	if (ret) {
3722 		dev_err(dev, "v4l2_async_register_subdev error: %d\n", ret);
3723 		goto err_unreg_notif;
3724 	}
3725 
3726 	return 0;
3727 
3728 err_unreg_notif:
3729 	ub960_v4l2_notifier_unregister(priv);
3730 err_subdev_cleanup:
3731 	v4l2_subdev_cleanup(&priv->sd);
3732 err_entity_cleanup:
3733 	media_entity_cleanup(&priv->sd.entity);
3734 err_free_ctrl:
3735 	v4l2_ctrl_handler_free(&priv->ctrl_handler);
3736 
3737 	return ret;
3738 }
3739 
ub960_destroy_subdev(struct ub960_data * priv)3740 static void ub960_destroy_subdev(struct ub960_data *priv)
3741 {
3742 	ub960_v4l2_notifier_unregister(priv);
3743 	v4l2_async_unregister_subdev(&priv->sd);
3744 
3745 	v4l2_subdev_cleanup(&priv->sd);
3746 
3747 	media_entity_cleanup(&priv->sd.entity);
3748 	v4l2_ctrl_handler_free(&priv->ctrl_handler);
3749 }
3750 
3751 static const struct regmap_config ub960_regmap_config = {
3752 	.name = "ds90ub960",
3753 
3754 	.reg_bits = 8,
3755 	.val_bits = 8,
3756 
3757 	.max_register = 0xff,
3758 
3759 	/*
3760 	 * We do locking in the driver to cover the TX/RX port selection and the
3761 	 * indirect register access.
3762 	 */
3763 	.disable_locking = true,
3764 };
3765 
ub960_reset(struct ub960_data * priv,bool reset_regs)3766 static void ub960_reset(struct ub960_data *priv, bool reset_regs)
3767 {
3768 	struct device *dev = &priv->client->dev;
3769 	unsigned int v;
3770 	int ret;
3771 	u8 bit;
3772 
3773 	bit = reset_regs ? UB960_SR_RESET_DIGITAL_RESET1 :
3774 			   UB960_SR_RESET_DIGITAL_RESET0;
3775 
3776 	ub960_write(priv, UB960_SR_RESET, bit);
3777 
3778 	mutex_lock(&priv->reg_lock);
3779 
3780 	ret = regmap_read_poll_timeout(priv->regmap, UB960_SR_RESET, v,
3781 				       (v & bit) == 0, 2000, 100000);
3782 
3783 	mutex_unlock(&priv->reg_lock);
3784 
3785 	if (ret)
3786 		dev_err(dev, "reset failed: %d\n", ret);
3787 }
3788 
ub960_get_hw_resources(struct ub960_data * priv)3789 static int ub960_get_hw_resources(struct ub960_data *priv)
3790 {
3791 	struct device *dev = &priv->client->dev;
3792 
3793 	priv->regmap = devm_regmap_init_i2c(priv->client, &ub960_regmap_config);
3794 	if (IS_ERR(priv->regmap))
3795 		return PTR_ERR(priv->regmap);
3796 
3797 	priv->vddio = devm_regulator_get(dev, "vddio");
3798 	if (IS_ERR(priv->vddio))
3799 		return dev_err_probe(dev, PTR_ERR(priv->vddio),
3800 				     "cannot get VDDIO regulator\n");
3801 
3802 	/* get power-down pin from DT */
3803 	priv->pd_gpio =
3804 		devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
3805 	if (IS_ERR(priv->pd_gpio))
3806 		return dev_err_probe(dev, PTR_ERR(priv->pd_gpio),
3807 				     "Cannot get powerdown GPIO\n");
3808 
3809 	priv->refclk = devm_clk_get(dev, "refclk");
3810 	if (IS_ERR(priv->refclk))
3811 		return dev_err_probe(dev, PTR_ERR(priv->refclk),
3812 				     "Cannot get REFCLK\n");
3813 
3814 	return 0;
3815 }
3816 
ub960_enable_core_hw(struct ub960_data * priv)3817 static int ub960_enable_core_hw(struct ub960_data *priv)
3818 {
3819 	struct device *dev = &priv->client->dev;
3820 	u8 rev_mask;
3821 	int ret;
3822 	u8 dev_sts;
3823 	u8 refclk_freq;
3824 
3825 	ret = regulator_enable(priv->vddio);
3826 	if (ret)
3827 		return dev_err_probe(dev, ret,
3828 				     "failed to enable VDDIO regulator\n");
3829 
3830 	ret = clk_prepare_enable(priv->refclk);
3831 	if (ret) {
3832 		dev_err_probe(dev, ret, "Failed to enable refclk\n");
3833 		goto err_disable_vddio;
3834 	}
3835 
3836 	if (priv->pd_gpio) {
3837 		gpiod_set_value_cansleep(priv->pd_gpio, 1);
3838 		/* wait min 2 ms for reset to complete */
3839 		fsleep(2000);
3840 		gpiod_set_value_cansleep(priv->pd_gpio, 0);
3841 		/* wait min 2 ms for power up to finish */
3842 		fsleep(2000);
3843 	}
3844 
3845 	ub960_reset(priv, true);
3846 
3847 	/* Runtime check register accessibility */
3848 	ret = ub960_read(priv, UB960_SR_REV_MASK, &rev_mask);
3849 	if (ret) {
3850 		dev_err_probe(dev, ret, "Cannot read first register, abort\n");
3851 		goto err_pd_gpio;
3852 	}
3853 
3854 	dev_dbg(dev, "Found %s (rev/mask %#04x)\n", priv->hw_data->model,
3855 		rev_mask);
3856 
3857 	ret = ub960_read(priv, UB960_SR_DEVICE_STS, &dev_sts);
3858 	if (ret)
3859 		goto err_pd_gpio;
3860 
3861 	if (priv->hw_data->is_ub9702)
3862 		ret = ub960_read(priv, UB9702_SR_REFCLK_FREQ, &refclk_freq);
3863 	else
3864 		ret = ub960_read(priv, UB960_XR_REFCLK_FREQ, &refclk_freq);
3865 	if (ret)
3866 		goto err_pd_gpio;
3867 
3868 	dev_dbg(dev, "refclk valid %u freq %u MHz (clk fw freq %lu MHz)\n",
3869 		!!(dev_sts & BIT(4)), refclk_freq,
3870 		clk_get_rate(priv->refclk) / 1000000);
3871 
3872 	/* Disable all RX ports by default */
3873 	ret = ub960_write(priv, UB960_SR_RX_PORT_CTL, 0);
3874 	if (ret)
3875 		goto err_pd_gpio;
3876 
3877 	/* release GPIO lock */
3878 	if (priv->hw_data->is_ub9702) {
3879 		ret = ub960_update_bits(priv, UB960_SR_RESET,
3880 					UB960_SR_RESET_GPIO_LOCK_RELEASE,
3881 					UB960_SR_RESET_GPIO_LOCK_RELEASE);
3882 		if (ret)
3883 			goto err_pd_gpio;
3884 	}
3885 
3886 	return 0;
3887 
3888 err_pd_gpio:
3889 	gpiod_set_value_cansleep(priv->pd_gpio, 1);
3890 	clk_disable_unprepare(priv->refclk);
3891 err_disable_vddio:
3892 	regulator_disable(priv->vddio);
3893 
3894 	return ret;
3895 }
3896 
ub960_disable_core_hw(struct ub960_data * priv)3897 static void ub960_disable_core_hw(struct ub960_data *priv)
3898 {
3899 	gpiod_set_value_cansleep(priv->pd_gpio, 1);
3900 	clk_disable_unprepare(priv->refclk);
3901 	regulator_disable(priv->vddio);
3902 }
3903 
ub960_probe(struct i2c_client * client)3904 static int ub960_probe(struct i2c_client *client)
3905 {
3906 	struct device *dev = &client->dev;
3907 	struct ub960_data *priv;
3908 	unsigned int port_lock_mask;
3909 	unsigned int port_mask;
3910 	unsigned int nport;
3911 	int ret;
3912 
3913 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
3914 	if (!priv)
3915 		return -ENOMEM;
3916 
3917 	priv->client = client;
3918 
3919 	priv->hw_data = device_get_match_data(dev);
3920 
3921 	mutex_init(&priv->reg_lock);
3922 
3923 	INIT_DELAYED_WORK(&priv->poll_work, ub960_handler_work);
3924 
3925 	/*
3926 	 * Initialize these to invalid values so that the first reg writes will
3927 	 * configure the target.
3928 	 */
3929 	priv->reg_current.indirect_target = 0xff;
3930 	priv->reg_current.rxport = 0xff;
3931 	priv->reg_current.txport = 0xff;
3932 
3933 	ret = ub960_get_hw_resources(priv);
3934 	if (ret)
3935 		goto err_mutex_destroy;
3936 
3937 	ret = ub960_enable_core_hw(priv);
3938 	if (ret)
3939 		goto err_mutex_destroy;
3940 
3941 	ret = ub960_parse_dt(priv);
3942 	if (ret)
3943 		goto err_disable_core_hw;
3944 
3945 	ret = ub960_init_tx_ports(priv);
3946 	if (ret)
3947 		goto err_free_ports;
3948 
3949 	ret = ub960_rxport_enable_vpocs(priv);
3950 	if (ret)
3951 		goto err_free_ports;
3952 
3953 	ret = ub960_init_rx_ports(priv);
3954 	if (ret)
3955 		goto err_disable_vpocs;
3956 
3957 	ub960_reset(priv, false);
3958 
3959 	port_mask = 0;
3960 
3961 	for (nport = 0; nport < priv->hw_data->num_rxports; nport++) {
3962 		struct ub960_rxport *rxport = priv->rxports[nport];
3963 
3964 		if (!rxport)
3965 			continue;
3966 
3967 		port_mask |= BIT(nport);
3968 	}
3969 
3970 	ret = ub960_rxport_wait_locks(priv, port_mask, &port_lock_mask);
3971 	if (ret)
3972 		goto err_disable_vpocs;
3973 
3974 	if (port_mask != port_lock_mask) {
3975 		ret = -EIO;
3976 		dev_err_probe(dev, ret, "Failed to lock all RX ports\n");
3977 		goto err_disable_vpocs;
3978 	}
3979 
3980 	/*
3981 	 * Clear any errors caused by switching the RX port settings while
3982 	 * probing.
3983 	 */
3984 	ub960_clear_rx_errors(priv);
3985 
3986 	ret = ub960_init_atr(priv);
3987 	if (ret)
3988 		goto err_disable_vpocs;
3989 
3990 	ret = ub960_rxport_add_serializers(priv);
3991 	if (ret)
3992 		goto err_uninit_atr;
3993 
3994 	ret = ub960_create_subdev(priv);
3995 	if (ret)
3996 		goto err_free_sers;
3997 
3998 	if (client->irq)
3999 		dev_warn(dev, "irq support not implemented, using polling\n");
4000 
4001 	schedule_delayed_work(&priv->poll_work,
4002 			      msecs_to_jiffies(UB960_POLL_TIME_MS));
4003 
4004 	return 0;
4005 
4006 err_free_sers:
4007 	ub960_rxport_remove_serializers(priv);
4008 err_uninit_atr:
4009 	ub960_uninit_atr(priv);
4010 err_disable_vpocs:
4011 	ub960_rxport_disable_vpocs(priv);
4012 err_free_ports:
4013 	ub960_rxport_free_ports(priv);
4014 	ub960_txport_free_ports(priv);
4015 err_disable_core_hw:
4016 	ub960_disable_core_hw(priv);
4017 err_mutex_destroy:
4018 	mutex_destroy(&priv->reg_lock);
4019 	return ret;
4020 }
4021 
ub960_remove(struct i2c_client * client)4022 static void ub960_remove(struct i2c_client *client)
4023 {
4024 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
4025 	struct ub960_data *priv = sd_to_ub960(sd);
4026 
4027 	cancel_delayed_work_sync(&priv->poll_work);
4028 
4029 	ub960_destroy_subdev(priv);
4030 	ub960_rxport_remove_serializers(priv);
4031 	ub960_uninit_atr(priv);
4032 	ub960_rxport_disable_vpocs(priv);
4033 	ub960_rxport_free_ports(priv);
4034 	ub960_txport_free_ports(priv);
4035 	ub960_disable_core_hw(priv);
4036 	mutex_destroy(&priv->reg_lock);
4037 }
4038 
4039 static const struct ub960_hw_data ds90ub960_hw = {
4040 	.model = "ub960",
4041 	.num_rxports = 4,
4042 	.num_txports = 2,
4043 };
4044 
4045 static const struct ub960_hw_data ds90ub9702_hw = {
4046 	.model = "ub9702",
4047 	.num_rxports = 4,
4048 	.num_txports = 2,
4049 	.is_ub9702 = true,
4050 	.is_fpdlink4 = true,
4051 };
4052 
4053 static const struct i2c_device_id ub960_id[] = {
4054 	{ "ds90ub960-q1", (kernel_ulong_t)&ds90ub960_hw },
4055 	{ "ds90ub9702-q1", (kernel_ulong_t)&ds90ub9702_hw },
4056 	{}
4057 };
4058 MODULE_DEVICE_TABLE(i2c, ub960_id);
4059 
4060 static const struct of_device_id ub960_dt_ids[] = {
4061 	{ .compatible = "ti,ds90ub960-q1", .data = &ds90ub960_hw },
4062 	{ .compatible = "ti,ds90ub9702-q1", .data = &ds90ub9702_hw },
4063 	{}
4064 };
4065 MODULE_DEVICE_TABLE(of, ub960_dt_ids);
4066 
4067 static struct i2c_driver ds90ub960_driver = {
4068 	.probe		= ub960_probe,
4069 	.remove		= ub960_remove,
4070 	.id_table	= ub960_id,
4071 	.driver = {
4072 		.name	= "ds90ub960",
4073 		.of_match_table = ub960_dt_ids,
4074 	},
4075 };
4076 module_i2c_driver(ds90ub960_driver);
4077 
4078 MODULE_LICENSE("GPL");
4079 MODULE_DESCRIPTION("Texas Instruments FPD-Link III/IV Deserializers Driver");
4080 MODULE_AUTHOR("Luca Ceresoli <luca@lucaceresoli.net>");
4081 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>");
4082 MODULE_IMPORT_NS(I2C_ATR);
4083