1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Renesas R-Car MIPI CSI-2 Receiver
5 * Copyright (C) 2018 Renesas Electronics Corp.
8 #include <linux/delay.h>
9 #include <linux/interrupt.h>
11 #include <linux/module.h>
13 #include <linux/of_graph.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/reset.h>
17 #include <linux/sys_soc.h>
19 #include <media/mipi-csi2.h>
20 #include <media/v4l2-ctrls.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-fwnode.h>
23 #include <media/v4l2-mc.h>
24 #include <media/v4l2-subdev.h>
28 /* Register offsets and bits */
30 /* Control Timing Select */
32 #define TREF_TREF BIT(0)
36 #define SRST_SRST BIT(0)
38 /* PHY Operation Control */
39 #define PHYCNT_REG 0x08
40 #define PHYCNT_SHUTDOWNZ BIT(17)
41 #define PHYCNT_RSTZ BIT(16)
42 #define PHYCNT_ENABLECLK BIT(4)
43 #define PHYCNT_ENABLE_3 BIT(3)
44 #define PHYCNT_ENABLE_2 BIT(2)
45 #define PHYCNT_ENABLE_1 BIT(1)
46 #define PHYCNT_ENABLE_0 BIT(0)
48 /* Checksum Control */
49 #define CHKSUM_REG 0x0c
50 #define CHKSUM_ECC_EN BIT(1)
51 #define CHKSUM_CRC_EN BIT(0)
54 * Channel Data Type Select
55 * VCDT[0-15]: Channel 0 VCDT[16-31]: Channel 1
56 * VCDT2[0-15]: Channel 2 VCDT2[16-31]: Channel 3
59 #define VCDT2_REG 0x14
60 #define VCDT_VCDTN_EN BIT(15)
61 #define VCDT_SEL_VC(n) (((n) & 0x3) << 8)
62 #define VCDT_SEL_DTN_ON BIT(6)
63 #define VCDT_SEL_DT(n) (((n) & 0x3f) << 0)
65 /* Frame Data Type Select */
68 /* Field Detection Control */
70 #define FLD_FLD_NUM(n) (((n) & 0xff) << 16)
71 #define FLD_DET_SEL(n) (((n) & 0x3) << 4)
72 #define FLD_FLD_EN4 BIT(3)
73 #define FLD_FLD_EN3 BIT(2)
74 #define FLD_FLD_EN2 BIT(1)
75 #define FLD_FLD_EN BIT(0)
77 /* Automatic Standby Control */
78 #define ASTBY_REG 0x20
80 /* Long Data Type Setting 0 */
81 #define LNGDT0_REG 0x28
83 /* Long Data Type Setting 1 */
84 #define LNGDT1_REG 0x2c
86 /* Interrupt Enable */
87 #define INTEN_REG 0x30
88 #define INTEN_INT_AFIFO_OF BIT(27)
89 #define INTEN_INT_ERRSOTHS BIT(4)
90 #define INTEN_INT_ERRSOTSYNCHS BIT(3)
92 /* Interrupt Source Mask */
93 #define INTCLOSE_REG 0x34
95 /* Interrupt Status Monitor */
96 #define INTSTATE_REG 0x38
97 #define INTSTATE_INT_ULPS_START BIT(7)
98 #define INTSTATE_INT_ULPS_END BIT(6)
100 /* Interrupt Error Status Monitor */
101 #define INTERRSTATE_REG 0x3c
103 /* Short Packet Data */
104 #define SHPDAT_REG 0x40
106 /* Short Packet Count */
107 #define SHPCNT_REG 0x44
109 /* LINK Operation Control */
110 #define LINKCNT_REG 0x48
111 #define LINKCNT_MONITOR_EN BIT(31)
112 #define LINKCNT_REG_MONI_PACT_EN BIT(25)
113 #define LINKCNT_ICLK_NONSTOP BIT(24)
116 #define LSWAP_REG 0x4c
117 #define LSWAP_L3SEL(n) (((n) & 0x3) << 6)
118 #define LSWAP_L2SEL(n) (((n) & 0x3) << 4)
119 #define LSWAP_L1SEL(n) (((n) & 0x3) << 2)
120 #define LSWAP_L0SEL(n) (((n) & 0x3) << 0)
122 /* PHY Test Interface Write Register */
123 #define PHTW_REG 0x50
124 #define PHTW_DWEN BIT(24)
125 #define PHTW_TESTDIN_DATA(n) (((n & 0xff)) << 16)
126 #define PHTW_CWEN BIT(8)
127 #define PHTW_TESTDIN_CODE(n) ((n & 0xff))
129 #define PHYFRX_REG 0x64
130 #define PHYFRX_FORCERX_MODE_3 BIT(3)
131 #define PHYFRX_FORCERX_MODE_2 BIT(2)
132 #define PHYFRX_FORCERX_MODE_1 BIT(1)
133 #define PHYFRX_FORCERX_MODE_0 BIT(0)
135 /* V4H BASE registers */
136 #define V4H_N_LANES_REG 0x0004
137 #define V4H_CSI2_RESETN_REG 0x0008
139 #define V4H_PHY_MODE_REG 0x001c
140 #define V4H_PHY_MODE_DPHY 0
141 #define V4H_PHY_MODE_CPHY 1
143 #define V4H_PHY_SHUTDOWNZ_REG 0x0040
144 #define V4H_DPHY_RSTZ_REG 0x0044
145 #define V4H_FLDC_REG 0x0804
146 #define V4H_FLDD_REG 0x0808
147 #define V4H_IDIC_REG 0x0810
149 #define V4H_PHY_EN_REG 0x2000
150 #define V4H_PHY_EN_ENABLE_3 BIT(7)
151 #define V4H_PHY_EN_ENABLE_2 BIT(6)
152 #define V4H_PHY_EN_ENABLE_1 BIT(5)
153 #define V4H_PHY_EN_ENABLE_0 BIT(4)
154 #define V4H_PHY_EN_ENABLE_CLK BIT(0)
156 #define V4H_ST_PHYST_REG 0x2814
157 #define V4H_ST_PHYST_ST_PHY_READY BIT(31)
158 #define V4H_ST_PHYST_ST_STOPSTATE_3 BIT(3)
159 #define V4H_ST_PHYST_ST_STOPSTATE_2 BIT(2)
160 #define V4H_ST_PHYST_ST_STOPSTATE_1 BIT(1)
161 #define V4H_ST_PHYST_ST_STOPSTATE_0 BIT(0)
163 /* V4H PPI registers */
164 #define V4H_PPI_STARTUP_RW_COMMON_DPHY_REG(n) (0x21800 + ((n) * 2)) /* n = 0 - 9 */
165 #define V4H_PPI_STARTUP_RW_COMMON_STARTUP_1_1_REG 0x21822
166 #define V4H_PPI_CALIBCTRL_RW_COMMON_BG_0_REG 0x2184c
167 #define V4H_PPI_RW_LPDCOCAL_TIMEBASE_REG 0x21c02
168 #define V4H_PPI_RW_LPDCOCAL_NREF_REG 0x21c04
169 #define V4H_PPI_RW_LPDCOCAL_NREF_RANGE_REG 0x21c06
170 #define V4H_PPI_RW_LPDCOCAL_TWAIT_CONFIG_REG 0x21c0a
171 #define V4H_PPI_RW_LPDCOCAL_VT_CONFIG_REG 0x21c0c
172 #define V4H_PPI_RW_LPDCOCAL_COARSE_CFG_REG 0x21c10
173 #define V4H_PPI_RW_COMMON_CFG_REG 0x21c6c
174 #define V4H_PPI_RW_TERMCAL_CFG_0_REG 0x21c80
175 #define V4H_PPI_RW_OFFSETCAL_CFG_0_REG 0x21ca0
177 /* V4H CORE registers */
178 #define V4H_CORE_DIG_IOCTRL_RW_AFE_LANE0_CTRL_2_REG(n) (0x22040 + ((n) * 2)) /* n = 0 - 15 */
179 #define V4H_CORE_DIG_IOCTRL_RW_AFE_LANE1_CTRL_2_REG(n) (0x22440 + ((n) * 2)) /* n = 0 - 15 */
180 #define V4H_CORE_DIG_IOCTRL_RW_AFE_LANE2_CTRL_2_REG(n) (0x22840 + ((n) * 2)) /* n = 0 - 15 */
181 #define V4H_CORE_DIG_IOCTRL_RW_AFE_LANE3_CTRL_2_REG(n) (0x22c40 + ((n) * 2)) /* n = 0 - 15 */
182 #define V4H_CORE_DIG_IOCTRL_RW_AFE_LANE4_CTRL_2_REG(n) (0x23040 + ((n) * 2)) /* n = 0 - 15 */
183 #define V4H_CORE_DIG_IOCTRL_RW_AFE_CB_CTRL_2_REG(n) (0x23840 + ((n) * 2)) /* n = 0 - 11 */
184 #define V4H_CORE_DIG_RW_COMMON_REG(n) (0x23880 + ((n) * 2)) /* n = 0 - 15 */
185 #define V4H_CORE_DIG_ANACTRL_RW_COMMON_ANACTRL_REG(n) (0x239e0 + ((n) * 2)) /* n = 0 - 3 */
186 #define V4H_CORE_DIG_CLANE_1_RW_CFG_0_REG 0x2a400
187 #define V4H_CORE_DIG_CLANE_1_RW_HS_TX_6_REG 0x2a60c
190 #define V4H_CORE_DIG_RW_TRIO0_REG(n) (0x22100 + ((n) * 2)) /* n = 0 - 3 */
191 #define V4H_CORE_DIG_RW_TRIO1_REG(n) (0x22500 + ((n) * 2)) /* n = 0 - 3 */
192 #define V4H_CORE_DIG_RW_TRIO2_REG(n) (0x22900 + ((n) * 2)) /* n = 0 - 3 */
193 #define V4H_CORE_DIG_CLANE_0_RW_LP_0_REG 0x2a080
194 #define V4H_CORE_DIG_CLANE_0_RW_HS_RX_REG(n) (0x2a100 + ((n) * 2)) /* n = 0 - 6 */
195 #define V4H_CORE_DIG_CLANE_1_RW_LP_0_REG 0x2a480
196 #define V4H_CORE_DIG_CLANE_1_RW_HS_RX_REG(n) (0x2a500 + ((n) * 2)) /* n = 0 - 6 */
197 #define V4H_CORE_DIG_CLANE_2_RW_LP_0_REG 0x2a880
198 #define V4H_CORE_DIG_CLANE_2_RW_HS_RX_REG(n) (0x2a900 + ((n) * 2)) /* n = 0 - 6 */
200 struct rcsi2_cphy_setting {
210 static const struct rcsi2_cphy_setting cphy_setting_table_r8a779g0[] = {
211 { .msps = 80, .rx2 = 0x38, .trio0 = 0x024a, .trio1 = 0x0134, .trio2 = 0x6a, .lane27 = 0x0000, .lane29 = 0x0a24 },
212 { .msps = 100, .rx2 = 0x38, .trio0 = 0x024a, .trio1 = 0x00f5, .trio2 = 0x55, .lane27 = 0x0000, .lane29 = 0x0a24 },
213 { .msps = 200, .rx2 = 0x38, .trio0 = 0x024a, .trio1 = 0x0077, .trio2 = 0x2b, .lane27 = 0x0000, .lane29 = 0x0a44 },
214 { .msps = 300, .rx2 = 0x38, .trio0 = 0x024a, .trio1 = 0x004d, .trio2 = 0x1d, .lane27 = 0x0000, .lane29 = 0x0a44 },
215 { .msps = 400, .rx2 = 0x38, .trio0 = 0x024a, .trio1 = 0x0038, .trio2 = 0x16, .lane27 = 0x0000, .lane29 = 0x0a64 },
216 { .msps = 500, .rx2 = 0x38, .trio0 = 0x024a, .trio1 = 0x002b, .trio2 = 0x12, .lane27 = 0x0000, .lane29 = 0x0a64 },
217 { .msps = 600, .rx2 = 0x38, .trio0 = 0x024a, .trio1 = 0x0023, .trio2 = 0x0f, .lane27 = 0x0000, .lane29 = 0x0a64 },
218 { .msps = 700, .rx2 = 0x38, .trio0 = 0x024a, .trio1 = 0x001d, .trio2 = 0x0d, .lane27 = 0x0000, .lane29 = 0x0a84 },
219 { .msps = 800, .rx2 = 0x38, .trio0 = 0x024a, .trio1 = 0x0018, .trio2 = 0x0c, .lane27 = 0x0000, .lane29 = 0x0a84 },
220 { .msps = 900, .rx2 = 0x38, .trio0 = 0x024a, .trio1 = 0x0015, .trio2 = 0x0b, .lane27 = 0x0000, .lane29 = 0x0a84 },
221 { .msps = 1000, .rx2 = 0x3e, .trio0 = 0x024a, .trio1 = 0x0012, .trio2 = 0x0a, .lane27 = 0x0400, .lane29 = 0x0a84 },
222 { .msps = 1100, .rx2 = 0x44, .trio0 = 0x024a, .trio1 = 0x000f, .trio2 = 0x09, .lane27 = 0x0800, .lane29 = 0x0a84 },
223 { .msps = 1200, .rx2 = 0x4a, .trio0 = 0x024a, .trio1 = 0x000e, .trio2 = 0x08, .lane27 = 0x0c00, .lane29 = 0x0a84 },
224 { .msps = 1300, .rx2 = 0x51, .trio0 = 0x024a, .trio1 = 0x000c, .trio2 = 0x08, .lane27 = 0x0c00, .lane29 = 0x0aa4 },
225 { .msps = 1400, .rx2 = 0x57, .trio0 = 0x024a, .trio1 = 0x000b, .trio2 = 0x07, .lane27 = 0x1000, .lane29 = 0x0aa4 },
226 { .msps = 1500, .rx2 = 0x5d, .trio0 = 0x044a, .trio1 = 0x0009, .trio2 = 0x07, .lane27 = 0x1000, .lane29 = 0x0aa4 },
227 { .msps = 1600, .rx2 = 0x63, .trio0 = 0x044a, .trio1 = 0x0008, .trio2 = 0x07, .lane27 = 0x1400, .lane29 = 0x0aa4 },
228 { .msps = 1700, .rx2 = 0x6a, .trio0 = 0x044a, .trio1 = 0x0007, .trio2 = 0x06, .lane27 = 0x1400, .lane29 = 0x0aa4 },
229 { .msps = 1800, .rx2 = 0x70, .trio0 = 0x044a, .trio1 = 0x0007, .trio2 = 0x06, .lane27 = 0x1400, .lane29 = 0x0aa4 },
230 { .msps = 1900, .rx2 = 0x76, .trio0 = 0x044a, .trio1 = 0x0006, .trio2 = 0x06, .lane27 = 0x1400, .lane29 = 0x0aa4 },
231 { .msps = 2000, .rx2 = 0x7c, .trio0 = 0x044a, .trio1 = 0x0005, .trio2 = 0x06, .lane27 = 0x1800, .lane29 = 0x0aa4 },
232 { .msps = 2100, .rx2 = 0x83, .trio0 = 0x044a, .trio1 = 0x0005, .trio2 = 0x05, .lane27 = 0x1800, .lane29 = 0x0aa4 },
233 { .msps = 2200, .rx2 = 0x89, .trio0 = 0x064a, .trio1 = 0x0004, .trio2 = 0x05, .lane27 = 0x1800, .lane29 = 0x0aa4 },
234 { .msps = 2300, .rx2 = 0x8f, .trio0 = 0x064a, .trio1 = 0x0003, .trio2 = 0x05, .lane27 = 0x1800, .lane29 = 0x0aa4 },
235 { .msps = 2400, .rx2 = 0x95, .trio0 = 0x064a, .trio1 = 0x0003, .trio2 = 0x05, .lane27 = 0x1800, .lane29 = 0x0aa4 },
236 { .msps = 2500, .rx2 = 0x9c, .trio0 = 0x064a, .trio1 = 0x0003, .trio2 = 0x05, .lane27 = 0x1c00, .lane29 = 0x0aa4 },
237 { .msps = 2600, .rx2 = 0xa2, .trio0 = 0x064a, .trio1 = 0x0002, .trio2 = 0x05, .lane27 = 0x1c00, .lane29 = 0x0ad4 },
238 { .msps = 2700, .rx2 = 0xa8, .trio0 = 0x064a, .trio1 = 0x0002, .trio2 = 0x05, .lane27 = 0x1c00, .lane29 = 0x0ad4 },
239 { .msps = 2800, .rx2 = 0xae, .trio0 = 0x064a, .trio1 = 0x0002, .trio2 = 0x04, .lane27 = 0x1c00, .lane29 = 0x0ad4 },
240 { .msps = 2900, .rx2 = 0xb5, .trio0 = 0x084a, .trio1 = 0x0001, .trio2 = 0x04, .lane27 = 0x1c00, .lane29 = 0x0ad4 },
241 { .msps = 3000, .rx2 = 0xbb, .trio0 = 0x084a, .trio1 = 0x0001, .trio2 = 0x04, .lane27 = 0x1c00, .lane29 = 0x0ad4 },
242 { .msps = 3100, .rx2 = 0xc1, .trio0 = 0x084a, .trio1 = 0x0001, .trio2 = 0x04, .lane27 = 0x1c00, .lane29 = 0x0ad4 },
243 { .msps = 3200, .rx2 = 0xc7, .trio0 = 0x084a, .trio1 = 0x0001, .trio2 = 0x04, .lane27 = 0x1c00, .lane29 = 0x0ad4 },
244 { .msps = 3300, .rx2 = 0xce, .trio0 = 0x084a, .trio1 = 0x0001, .trio2 = 0x04, .lane27 = 0x1c00, .lane29 = 0x0ad4 },
245 { .msps = 3400, .rx2 = 0xd4, .trio0 = 0x084a, .trio1 = 0x0001, .trio2 = 0x04, .lane27 = 0x1c00, .lane29 = 0x0ad4 },
246 { .msps = 3500, .rx2 = 0xda, .trio0 = 0x084a, .trio1 = 0x0001, .trio2 = 0x04, .lane27 = 0x1c00, .lane29 = 0x0ad4 },
251 #define V4M_OVR1_REG 0x0848
252 #define V4M_OVR1_FORCERXMODE_3 BIT(12)
253 #define V4M_OVR1_FORCERXMODE_2 BIT(11)
254 #define V4M_OVR1_FORCERXMODE_1 BIT(10)
255 #define V4M_OVR1_FORCERXMODE_0 BIT(9)
257 #define V4M_FRXM_REG 0x2004
258 #define V4M_FRXM_FORCERXMODE_3 BIT(3)
259 #define V4M_FRXM_FORCERXMODE_2 BIT(2)
260 #define V4M_FRXM_FORCERXMODE_1 BIT(1)
261 #define V4M_FRXM_FORCERXMODE_0 BIT(0)
263 #define V4M_PHYPLL_REG 0x02050
264 #define V4M_CSI0CLKFCPR_REG 0x02054
265 #define V4M_PHTW_REG 0x02060
266 #define V4M_PHTR_REG 0x02064
267 #define V4M_PHTC_REG 0x02068
274 struct rcsi2_mbps_info {
277 u16 osc_freq; /* V4M */
280 static const struct rcsi2_mbps_info phtw_mbps_v3u[] = {
281 { .mbps = 1500, .reg = 0xcc },
282 { .mbps = 1550, .reg = 0x1d },
283 { .mbps = 1600, .reg = 0x27 },
284 { .mbps = 1650, .reg = 0x30 },
285 { .mbps = 1700, .reg = 0x39 },
286 { .mbps = 1750, .reg = 0x42 },
287 { .mbps = 1800, .reg = 0x4b },
288 { .mbps = 1850, .reg = 0x55 },
289 { .mbps = 1900, .reg = 0x5e },
290 { .mbps = 1950, .reg = 0x67 },
291 { .mbps = 2000, .reg = 0x71 },
292 { .mbps = 2050, .reg = 0x79 },
293 { .mbps = 2100, .reg = 0x83 },
294 { .mbps = 2150, .reg = 0x8c },
295 { .mbps = 2200, .reg = 0x95 },
296 { .mbps = 2250, .reg = 0x9e },
297 { .mbps = 2300, .reg = 0xa7 },
298 { .mbps = 2350, .reg = 0xb0 },
299 { .mbps = 2400, .reg = 0xba },
300 { .mbps = 2450, .reg = 0xc3 },
301 { .mbps = 2500, .reg = 0xcc },
305 static const struct rcsi2_mbps_info phtw_mbps_h3_v3h_m3n[] = {
306 { .mbps = 80, .reg = 0x86 },
307 { .mbps = 90, .reg = 0x86 },
308 { .mbps = 100, .reg = 0x87 },
309 { .mbps = 110, .reg = 0x87 },
310 { .mbps = 120, .reg = 0x88 },
311 { .mbps = 130, .reg = 0x88 },
312 { .mbps = 140, .reg = 0x89 },
313 { .mbps = 150, .reg = 0x89 },
314 { .mbps = 160, .reg = 0x8a },
315 { .mbps = 170, .reg = 0x8a },
316 { .mbps = 180, .reg = 0x8b },
317 { .mbps = 190, .reg = 0x8b },
318 { .mbps = 205, .reg = 0x8c },
319 { .mbps = 220, .reg = 0x8d },
320 { .mbps = 235, .reg = 0x8e },
321 { .mbps = 250, .reg = 0x8e },
325 static const struct rcsi2_mbps_info phtw_mbps_v3m_e3[] = {
326 { .mbps = 80, .reg = 0x00 },
327 { .mbps = 90, .reg = 0x20 },
328 { .mbps = 100, .reg = 0x40 },
329 { .mbps = 110, .reg = 0x02 },
330 { .mbps = 130, .reg = 0x22 },
331 { .mbps = 140, .reg = 0x42 },
332 { .mbps = 150, .reg = 0x04 },
333 { .mbps = 170, .reg = 0x24 },
334 { .mbps = 180, .reg = 0x44 },
335 { .mbps = 200, .reg = 0x06 },
336 { .mbps = 220, .reg = 0x26 },
337 { .mbps = 240, .reg = 0x46 },
338 { .mbps = 250, .reg = 0x08 },
339 { .mbps = 270, .reg = 0x28 },
340 { .mbps = 300, .reg = 0x0a },
341 { .mbps = 330, .reg = 0x2a },
342 { .mbps = 360, .reg = 0x4a },
343 { .mbps = 400, .reg = 0x0c },
344 { .mbps = 450, .reg = 0x2c },
345 { .mbps = 500, .reg = 0x0e },
346 { .mbps = 550, .reg = 0x2e },
347 { .mbps = 600, .reg = 0x10 },
348 { .mbps = 650, .reg = 0x30 },
349 { .mbps = 700, .reg = 0x12 },
350 { .mbps = 750, .reg = 0x32 },
351 { .mbps = 800, .reg = 0x52 },
352 { .mbps = 850, .reg = 0x72 },
353 { .mbps = 900, .reg = 0x14 },
354 { .mbps = 950, .reg = 0x34 },
355 { .mbps = 1000, .reg = 0x54 },
356 { .mbps = 1050, .reg = 0x74 },
357 { .mbps = 1125, .reg = 0x16 },
361 /* PHY Test Interface Clear */
362 #define PHTC_REG 0x58
363 #define PHTC_TESTCLR BIT(0)
365 /* PHY Frequency Control */
366 #define PHYPLL_REG 0x68
367 #define PHYPLL_HSFREQRANGE(n) ((n) << 16)
369 static const struct rcsi2_mbps_info hsfreqrange_v3u[] = {
370 { .mbps = 80, .reg = 0x00 },
371 { .mbps = 90, .reg = 0x10 },
372 { .mbps = 100, .reg = 0x20 },
373 { .mbps = 110, .reg = 0x30 },
374 { .mbps = 120, .reg = 0x01 },
375 { .mbps = 130, .reg = 0x11 },
376 { .mbps = 140, .reg = 0x21 },
377 { .mbps = 150, .reg = 0x31 },
378 { .mbps = 160, .reg = 0x02 },
379 { .mbps = 170, .reg = 0x12 },
380 { .mbps = 180, .reg = 0x22 },
381 { .mbps = 190, .reg = 0x32 },
382 { .mbps = 205, .reg = 0x03 },
383 { .mbps = 220, .reg = 0x13 },
384 { .mbps = 235, .reg = 0x23 },
385 { .mbps = 250, .reg = 0x33 },
386 { .mbps = 275, .reg = 0x04 },
387 { .mbps = 300, .reg = 0x14 },
388 { .mbps = 325, .reg = 0x25 },
389 { .mbps = 350, .reg = 0x35 },
390 { .mbps = 400, .reg = 0x05 },
391 { .mbps = 450, .reg = 0x16 },
392 { .mbps = 500, .reg = 0x26 },
393 { .mbps = 550, .reg = 0x37 },
394 { .mbps = 600, .reg = 0x07 },
395 { .mbps = 650, .reg = 0x18 },
396 { .mbps = 700, .reg = 0x28 },
397 { .mbps = 750, .reg = 0x39 },
398 { .mbps = 800, .reg = 0x09 },
399 { .mbps = 850, .reg = 0x19 },
400 { .mbps = 900, .reg = 0x29 },
401 { .mbps = 950, .reg = 0x3a },
402 { .mbps = 1000, .reg = 0x0a },
403 { .mbps = 1050, .reg = 0x1a },
404 { .mbps = 1100, .reg = 0x2a },
405 { .mbps = 1150, .reg = 0x3b },
406 { .mbps = 1200, .reg = 0x0b },
407 { .mbps = 1250, .reg = 0x1b },
408 { .mbps = 1300, .reg = 0x2b },
409 { .mbps = 1350, .reg = 0x3c },
410 { .mbps = 1400, .reg = 0x0c },
411 { .mbps = 1450, .reg = 0x1c },
412 { .mbps = 1500, .reg = 0x2c },
413 { .mbps = 1550, .reg = 0x3d },
414 { .mbps = 1600, .reg = 0x0d },
415 { .mbps = 1650, .reg = 0x1d },
416 { .mbps = 1700, .reg = 0x2e },
417 { .mbps = 1750, .reg = 0x3e },
418 { .mbps = 1800, .reg = 0x0e },
419 { .mbps = 1850, .reg = 0x1e },
420 { .mbps = 1900, .reg = 0x2f },
421 { .mbps = 1950, .reg = 0x3f },
422 { .mbps = 2000, .reg = 0x0f },
423 { .mbps = 2050, .reg = 0x40 },
424 { .mbps = 2100, .reg = 0x41 },
425 { .mbps = 2150, .reg = 0x42 },
426 { .mbps = 2200, .reg = 0x43 },
427 { .mbps = 2300, .reg = 0x45 },
428 { .mbps = 2350, .reg = 0x46 },
429 { .mbps = 2400, .reg = 0x47 },
430 { .mbps = 2450, .reg = 0x48 },
431 { .mbps = 2500, .reg = 0x49 },
435 static const struct rcsi2_mbps_info hsfreqrange_h3_v3h_m3n[] = {
436 { .mbps = 80, .reg = 0x00 },
437 { .mbps = 90, .reg = 0x10 },
438 { .mbps = 100, .reg = 0x20 },
439 { .mbps = 110, .reg = 0x30 },
440 { .mbps = 120, .reg = 0x01 },
441 { .mbps = 130, .reg = 0x11 },
442 { .mbps = 140, .reg = 0x21 },
443 { .mbps = 150, .reg = 0x31 },
444 { .mbps = 160, .reg = 0x02 },
445 { .mbps = 170, .reg = 0x12 },
446 { .mbps = 180, .reg = 0x22 },
447 { .mbps = 190, .reg = 0x32 },
448 { .mbps = 205, .reg = 0x03 },
449 { .mbps = 220, .reg = 0x13 },
450 { .mbps = 235, .reg = 0x23 },
451 { .mbps = 250, .reg = 0x33 },
452 { .mbps = 275, .reg = 0x04 },
453 { .mbps = 300, .reg = 0x14 },
454 { .mbps = 325, .reg = 0x25 },
455 { .mbps = 350, .reg = 0x35 },
456 { .mbps = 400, .reg = 0x05 },
457 { .mbps = 450, .reg = 0x16 },
458 { .mbps = 500, .reg = 0x26 },
459 { .mbps = 550, .reg = 0x37 },
460 { .mbps = 600, .reg = 0x07 },
461 { .mbps = 650, .reg = 0x18 },
462 { .mbps = 700, .reg = 0x28 },
463 { .mbps = 750, .reg = 0x39 },
464 { .mbps = 800, .reg = 0x09 },
465 { .mbps = 850, .reg = 0x19 },
466 { .mbps = 900, .reg = 0x29 },
467 { .mbps = 950, .reg = 0x3a },
468 { .mbps = 1000, .reg = 0x0a },
469 { .mbps = 1050, .reg = 0x1a },
470 { .mbps = 1100, .reg = 0x2a },
471 { .mbps = 1150, .reg = 0x3b },
472 { .mbps = 1200, .reg = 0x0b },
473 { .mbps = 1250, .reg = 0x1b },
474 { .mbps = 1300, .reg = 0x2b },
475 { .mbps = 1350, .reg = 0x3c },
476 { .mbps = 1400, .reg = 0x0c },
477 { .mbps = 1450, .reg = 0x1c },
478 { .mbps = 1500, .reg = 0x2c },
482 static const struct rcsi2_mbps_info hsfreqrange_m3w[] = {
483 { .mbps = 80, .reg = 0x00 },
484 { .mbps = 90, .reg = 0x10 },
485 { .mbps = 100, .reg = 0x20 },
486 { .mbps = 110, .reg = 0x30 },
487 { .mbps = 120, .reg = 0x01 },
488 { .mbps = 130, .reg = 0x11 },
489 { .mbps = 140, .reg = 0x21 },
490 { .mbps = 150, .reg = 0x31 },
491 { .mbps = 160, .reg = 0x02 },
492 { .mbps = 170, .reg = 0x12 },
493 { .mbps = 180, .reg = 0x22 },
494 { .mbps = 190, .reg = 0x32 },
495 { .mbps = 205, .reg = 0x03 },
496 { .mbps = 220, .reg = 0x13 },
497 { .mbps = 235, .reg = 0x23 },
498 { .mbps = 250, .reg = 0x33 },
499 { .mbps = 275, .reg = 0x04 },
500 { .mbps = 300, .reg = 0x14 },
501 { .mbps = 325, .reg = 0x05 },
502 { .mbps = 350, .reg = 0x15 },
503 { .mbps = 400, .reg = 0x25 },
504 { .mbps = 450, .reg = 0x06 },
505 { .mbps = 500, .reg = 0x16 },
506 { .mbps = 550, .reg = 0x07 },
507 { .mbps = 600, .reg = 0x17 },
508 { .mbps = 650, .reg = 0x08 },
509 { .mbps = 700, .reg = 0x18 },
510 { .mbps = 750, .reg = 0x09 },
511 { .mbps = 800, .reg = 0x19 },
512 { .mbps = 850, .reg = 0x29 },
513 { .mbps = 900, .reg = 0x39 },
514 { .mbps = 950, .reg = 0x0a },
515 { .mbps = 1000, .reg = 0x1a },
516 { .mbps = 1050, .reg = 0x2a },
517 { .mbps = 1100, .reg = 0x3a },
518 { .mbps = 1150, .reg = 0x0b },
519 { .mbps = 1200, .reg = 0x1b },
520 { .mbps = 1250, .reg = 0x2b },
521 { .mbps = 1300, .reg = 0x3b },
522 { .mbps = 1350, .reg = 0x0c },
523 { .mbps = 1400, .reg = 0x1c },
524 { .mbps = 1450, .reg = 0x2c },
525 { .mbps = 1500, .reg = 0x3c },
529 static const struct rcsi2_mbps_info hsfreqrange_v4m[] = {
530 { .mbps = 80, .reg = 0x00, .osc_freq = 0x01a9 },
531 { .mbps = 90, .reg = 0x10, .osc_freq = 0x01a9 },
532 { .mbps = 100, .reg = 0x20, .osc_freq = 0x01a9 },
533 { .mbps = 110, .reg = 0x30, .osc_freq = 0x01a9 },
534 { .mbps = 120, .reg = 0x01, .osc_freq = 0x01a9 },
535 { .mbps = 130, .reg = 0x11, .osc_freq = 0x01a9 },
536 { .mbps = 140, .reg = 0x21, .osc_freq = 0x01a9 },
537 { .mbps = 150, .reg = 0x31, .osc_freq = 0x01a9 },
538 { .mbps = 160, .reg = 0x02, .osc_freq = 0x01a9 },
539 { .mbps = 170, .reg = 0x12, .osc_freq = 0x01a9 },
540 { .mbps = 180, .reg = 0x22, .osc_freq = 0x01a9 },
541 { .mbps = 190, .reg = 0x32, .osc_freq = 0x01a9 },
542 { .mbps = 205, .reg = 0x03, .osc_freq = 0x01a9 },
543 { .mbps = 220, .reg = 0x13, .osc_freq = 0x01a9 },
544 { .mbps = 235, .reg = 0x23, .osc_freq = 0x01a9 },
545 { .mbps = 250, .reg = 0x33, .osc_freq = 0x01a9 },
546 { .mbps = 275, .reg = 0x04, .osc_freq = 0x01a9 },
547 { .mbps = 300, .reg = 0x14, .osc_freq = 0x01a9 },
548 { .mbps = 325, .reg = 0x25, .osc_freq = 0x01a9 },
549 { .mbps = 350, .reg = 0x35, .osc_freq = 0x01a9 },
550 { .mbps = 400, .reg = 0x05, .osc_freq = 0x01a9 },
551 { .mbps = 450, .reg = 0x16, .osc_freq = 0x01a9 },
552 { .mbps = 500, .reg = 0x26, .osc_freq = 0x01a9 },
553 { .mbps = 550, .reg = 0x37, .osc_freq = 0x01a9 },
554 { .mbps = 600, .reg = 0x07, .osc_freq = 0x01a9 },
555 { .mbps = 650, .reg = 0x18, .osc_freq = 0x01a9 },
556 { .mbps = 700, .reg = 0x28, .osc_freq = 0x01a9 },
557 { .mbps = 750, .reg = 0x39, .osc_freq = 0x01a9 },
558 { .mbps = 800, .reg = 0x09, .osc_freq = 0x01a9 },
559 { .mbps = 850, .reg = 0x19, .osc_freq = 0x01a9 },
560 { .mbps = 900, .reg = 0x29, .osc_freq = 0x01a9 },
561 { .mbps = 950, .reg = 0x3a, .osc_freq = 0x01a9 },
562 { .mbps = 1000, .reg = 0x0a, .osc_freq = 0x01a9 },
563 { .mbps = 1050, .reg = 0x1a, .osc_freq = 0x01a9 },
564 { .mbps = 1100, .reg = 0x2a, .osc_freq = 0x01a9 },
565 { .mbps = 1150, .reg = 0x3b, .osc_freq = 0x01a9 },
566 { .mbps = 1200, .reg = 0x0b, .osc_freq = 0x01a9 },
567 { .mbps = 1250, .reg = 0x1b, .osc_freq = 0x01a9 },
568 { .mbps = 1300, .reg = 0x2b, .osc_freq = 0x01a9 },
569 { .mbps = 1350, .reg = 0x3c, .osc_freq = 0x01a9 },
570 { .mbps = 1400, .reg = 0x0c, .osc_freq = 0x01a9 },
571 { .mbps = 1450, .reg = 0x1c, .osc_freq = 0x01a9 },
572 { .mbps = 1500, .reg = 0x2c, .osc_freq = 0x01a9 },
573 { .mbps = 1550, .reg = 0x3d, .osc_freq = 0x0108 },
574 { .mbps = 1600, .reg = 0x0d, .osc_freq = 0x0110 },
575 { .mbps = 1650, .reg = 0x1d, .osc_freq = 0x0119 },
576 { .mbps = 1700, .reg = 0x2e, .osc_freq = 0x0121 },
577 { .mbps = 1750, .reg = 0x3e, .osc_freq = 0x012a },
578 { .mbps = 1800, .reg = 0x0e, .osc_freq = 0x0132 },
579 { .mbps = 1850, .reg = 0x1e, .osc_freq = 0x013b },
580 { .mbps = 1900, .reg = 0x2f, .osc_freq = 0x0143 },
581 { .mbps = 1950, .reg = 0x3f, .osc_freq = 0x014c },
582 { .mbps = 2000, .reg = 0x0f, .osc_freq = 0x0154 },
583 { .mbps = 2050, .reg = 0x40, .osc_freq = 0x015d },
584 { .mbps = 2100, .reg = 0x41, .osc_freq = 0x0165 },
585 { .mbps = 2150, .reg = 0x42, .osc_freq = 0x016e },
586 { .mbps = 2200, .reg = 0x43, .osc_freq = 0x0176 },
587 { .mbps = 2250, .reg = 0x44, .osc_freq = 0x017f },
588 { .mbps = 2300, .reg = 0x45, .osc_freq = 0x0187 },
589 { .mbps = 2350, .reg = 0x46, .osc_freq = 0x0190 },
590 { .mbps = 2400, .reg = 0x47, .osc_freq = 0x0198 },
591 { .mbps = 2450, .reg = 0x48, .osc_freq = 0x01a1 },
592 { .mbps = 2500, .reg = 0x49, .osc_freq = 0x01a9 },
596 /* PHY ESC Error Monitor */
597 #define PHEERM_REG 0x74
599 /* PHY Clock Lane Monitor */
600 #define PHCLM_REG 0x78
601 #define PHCLM_STOPSTATECKL BIT(0)
603 /* PHY Data Lane Monitor */
604 #define PHDLM_REG 0x7c
606 /* CSI0CLK Frequency Configuration Preset Register */
607 #define CSI0CLKFCPR_REG 0x260
608 #define CSI0CLKFREQRANGE(n) ((n & 0x3f) << 16)
610 struct rcar_csi2_format {
612 unsigned int datatype;
616 static const struct rcar_csi2_format rcar_csi2_formats[] = {
618 .code = MEDIA_BUS_FMT_RGB888_1X24,
619 .datatype = MIPI_CSI2_DT_RGB888,
622 .code = MEDIA_BUS_FMT_UYVY8_1X16,
623 .datatype = MIPI_CSI2_DT_YUV422_8B,
626 .code = MEDIA_BUS_FMT_YUYV8_1X16,
627 .datatype = MIPI_CSI2_DT_YUV422_8B,
630 .code = MEDIA_BUS_FMT_UYVY8_2X8,
631 .datatype = MIPI_CSI2_DT_YUV422_8B,
634 .code = MEDIA_BUS_FMT_YUYV10_2X10,
635 .datatype = MIPI_CSI2_DT_YUV422_8B,
638 .code = MEDIA_BUS_FMT_Y10_1X10,
639 .datatype = MIPI_CSI2_DT_RAW10,
642 .code = MEDIA_BUS_FMT_SBGGR8_1X8,
643 .datatype = MIPI_CSI2_DT_RAW8,
646 .code = MEDIA_BUS_FMT_SGBRG8_1X8,
647 .datatype = MIPI_CSI2_DT_RAW8,
650 .code = MEDIA_BUS_FMT_SGRBG8_1X8,
651 .datatype = MIPI_CSI2_DT_RAW8,
654 .code = MEDIA_BUS_FMT_SRGGB8_1X8,
655 .datatype = MIPI_CSI2_DT_RAW8,
658 .code = MEDIA_BUS_FMT_Y8_1X8,
659 .datatype = MIPI_CSI2_DT_RAW8,
664 static const struct rcar_csi2_format *rcsi2_code_to_fmt(unsigned int code)
668 for (i = 0; i < ARRAY_SIZE(rcar_csi2_formats); i++)
669 if (rcar_csi2_formats[i].code == code)
670 return &rcar_csi2_formats[i];
675 enum rcar_csi2_pads {
677 RCAR_CSI2_SOURCE_VC0,
678 RCAR_CSI2_SOURCE_VC1,
679 RCAR_CSI2_SOURCE_VC2,
680 RCAR_CSI2_SOURCE_VC3,
684 struct rcsi2_register_layout {
689 struct rcar_csi2_info {
690 const struct rcsi2_register_layout *regs;
691 int (*init_phtw)(struct rcar_csi2 *priv, unsigned int mbps);
692 int (*phy_post_init)(struct rcar_csi2 *priv);
693 int (*start_receiver)(struct rcar_csi2 *priv,
694 struct v4l2_subdev_state *state);
695 void (*enter_standby)(struct rcar_csi2 *priv);
696 const struct rcsi2_mbps_info *hsfreqrange;
697 unsigned int csi0clkfreqrange;
698 unsigned int num_channels;
708 const struct rcar_csi2_info *info;
709 struct reset_control *rstc;
711 struct v4l2_subdev subdev;
712 struct media_pad pads[NR_OF_RCAR_CSI2_PAD];
714 struct v4l2_async_notifier notifier;
715 struct v4l2_subdev *remote;
716 unsigned int remote_pad;
723 unsigned short lanes;
724 unsigned char lane_swap[4];
727 static inline struct rcar_csi2 *sd_to_csi2(struct v4l2_subdev *sd)
729 return container_of(sd, struct rcar_csi2, subdev);
732 static inline struct rcar_csi2 *notifier_to_csi2(struct v4l2_async_notifier *n)
734 return container_of(n, struct rcar_csi2, notifier);
737 static unsigned int rcsi2_num_pads(const struct rcar_csi2 *priv)
739 /* Used together with R-Car ISP: one sink and one source pad. */
740 if (priv->info->use_isp)
743 /* Used together with R-Car VIN: one sink and four source pads. */
747 static u32 rcsi2_read(struct rcar_csi2 *priv, unsigned int reg)
749 return ioread32(priv->base + reg);
752 static void rcsi2_write(struct rcar_csi2 *priv, unsigned int reg, u32 data)
754 iowrite32(data, priv->base + reg);
757 static void rcsi2_write16(struct rcar_csi2 *priv, unsigned int reg, u16 data)
759 iowrite16(data, priv->base + reg);
762 static int rcsi2_phtw_write(struct rcar_csi2 *priv, u8 data, u8 code)
764 unsigned int timeout;
766 rcsi2_write(priv, priv->info->regs->phtw,
767 PHTW_DWEN | PHTW_TESTDIN_DATA(data) |
768 PHTW_CWEN | PHTW_TESTDIN_CODE(code));
770 /* Wait for DWEN and CWEN to be cleared by hardware. */
771 for (timeout = 0; timeout <= 20; timeout++) {
772 if (!(rcsi2_read(priv, priv->info->regs->phtw) & (PHTW_DWEN | PHTW_CWEN)))
775 usleep_range(1000, 2000);
778 dev_err(priv->dev, "Timeout waiting for PHTW_DWEN and/or PHTW_CWEN\n");
783 static int rcsi2_phtw_write_array(struct rcar_csi2 *priv,
784 const struct phtw_value *values,
789 for (unsigned int i = 0; i < size; i++) {
790 ret = rcsi2_phtw_write(priv, values[i].data, values[i].code);
798 static const struct rcsi2_mbps_info *
799 rcsi2_mbps_to_info(struct rcar_csi2 *priv,
800 const struct rcsi2_mbps_info *infotable, unsigned int mbps)
802 const struct rcsi2_mbps_info *info;
803 const struct rcsi2_mbps_info *prev = NULL;
805 if (mbps < infotable->mbps)
806 dev_warn(priv->dev, "%u Mbps less than min PHY speed %u Mbps",
807 mbps, infotable->mbps);
809 for (info = infotable; info->mbps != 0; info++) {
810 if (info->mbps >= mbps)
816 dev_err(priv->dev, "Unsupported PHY speed (%u Mbps)", mbps);
820 if (prev && ((mbps - prev->mbps) <= (info->mbps - mbps)))
826 static void rcsi2_enter_standby_gen3(struct rcar_csi2 *priv)
828 rcsi2_write(priv, PHYCNT_REG, 0);
829 rcsi2_write(priv, PHTC_REG, PHTC_TESTCLR);
832 static void rcsi2_enter_standby(struct rcar_csi2 *priv)
834 if (priv->info->enter_standby)
835 priv->info->enter_standby(priv);
837 reset_control_assert(priv->rstc);
838 usleep_range(100, 150);
839 pm_runtime_put(priv->dev);
842 static int rcsi2_exit_standby(struct rcar_csi2 *priv)
846 ret = pm_runtime_resume_and_get(priv->dev);
850 reset_control_deassert(priv->rstc);
855 static int rcsi2_wait_phy_start(struct rcar_csi2 *priv,
858 unsigned int timeout;
860 /* Wait for the clock and data lanes to enter LP-11 state. */
861 for (timeout = 0; timeout <= 20; timeout++) {
862 const u32 lane_mask = (1 << lanes) - 1;
864 if ((rcsi2_read(priv, PHCLM_REG) & PHCLM_STOPSTATECKL) &&
865 (rcsi2_read(priv, PHDLM_REG) & lane_mask) == lane_mask)
868 usleep_range(1000, 2000);
871 dev_err(priv->dev, "Timeout waiting for LP-11 state\n");
876 static int rcsi2_set_phypll(struct rcar_csi2 *priv, unsigned int mbps)
878 const struct rcsi2_mbps_info *info;
880 info = rcsi2_mbps_to_info(priv, priv->info->hsfreqrange, mbps);
884 rcsi2_write(priv, priv->info->regs->phypll, PHYPLL_HSFREQRANGE(info->reg));
889 static int rcsi2_calc_mbps(struct rcar_csi2 *priv, unsigned int bpp,
892 struct v4l2_subdev *source;
893 struct v4l2_ctrl *ctrl;
899 source = priv->remote;
901 /* Read the pixel rate control from remote. */
902 ctrl = v4l2_ctrl_find(source->ctrl_handler, V4L2_CID_PIXEL_RATE);
904 dev_err(priv->dev, "no pixel rate control in subdev %s\n",
910 * Calculate the phypll in mbps.
911 * link_freq = (pixel_rate * bits_per_sample) / (2 * nr_of_lanes)
912 * bps = link_freq * 2
914 mbps = v4l2_ctrl_g_ctrl_int64(ctrl) * bpp;
915 do_div(mbps, lanes * 1000000);
917 /* Adjust for C-PHY, divide by 2.8. */
919 mbps = div_u64(mbps * 5, 14);
924 static int rcsi2_get_active_lanes(struct rcar_csi2 *priv,
927 struct v4l2_mbus_config mbus_config = { 0 };
930 *lanes = priv->lanes;
932 ret = v4l2_subdev_call(priv->remote, pad, get_mbus_config,
933 priv->remote_pad, &mbus_config);
934 if (ret == -ENOIOCTLCMD) {
935 dev_dbg(priv->dev, "No remote mbus configuration available\n");
940 dev_err(priv->dev, "Failed to get remote mbus configuration\n");
944 switch (mbus_config.type) {
945 case V4L2_MBUS_CSI2_CPHY:
949 case V4L2_MBUS_CSI2_DPHY:
954 dev_err(priv->dev, "Unsupported media bus type %u\n",
959 if (mbus_config.bus.mipi_csi2.num_data_lanes > priv->lanes) {
961 "Unsupported mbus config: too many data lanes %u\n",
962 mbus_config.bus.mipi_csi2.num_data_lanes);
966 *lanes = mbus_config.bus.mipi_csi2.num_data_lanes;
971 static int rcsi2_start_receiver_gen3(struct rcar_csi2 *priv,
972 struct v4l2_subdev_state *state)
974 const struct rcar_csi2_format *format;
975 u32 phycnt, vcdt = 0, vcdt2 = 0, fld = 0;
976 const struct v4l2_mbus_framefmt *fmt;
981 /* Use the format on the sink pad to compute the receiver config. */
982 fmt = v4l2_subdev_state_get_format(state, RCAR_CSI2_SINK);
984 dev_dbg(priv->dev, "Input size (%ux%u%c)\n",
985 fmt->width, fmt->height,
986 fmt->field == V4L2_FIELD_NONE ? 'p' : 'i');
988 /* Code is validated in set_fmt. */
989 format = rcsi2_code_to_fmt(fmt->code);
994 * Enable all supported CSI-2 channels with virtual channel and
995 * data type matching.
997 * NOTE: It's not possible to get individual datatype for each
998 * source virtual channel. Once this is possible in V4L2
999 * it should be used here.
1001 for (i = 0; i < priv->info->num_channels; i++) {
1004 if (priv->channel_vc[i] < 0)
1007 vcdt_part = VCDT_SEL_VC(priv->channel_vc[i]) | VCDT_VCDTN_EN |
1008 VCDT_SEL_DTN_ON | VCDT_SEL_DT(format->datatype);
1010 /* Store in correct reg and offset. */
1012 vcdt |= vcdt_part << ((i % 2) * 16);
1014 vcdt2 |= vcdt_part << ((i % 2) * 16);
1017 if (fmt->field == V4L2_FIELD_ALTERNATE) {
1018 fld = FLD_DET_SEL(1) | FLD_FLD_EN4 | FLD_FLD_EN3 | FLD_FLD_EN2
1021 if (fmt->height == 240)
1022 fld |= FLD_FLD_NUM(0);
1024 fld |= FLD_FLD_NUM(1);
1028 * Get the number of active data lanes inspecting the remote mbus
1031 ret = rcsi2_get_active_lanes(priv, &lanes);
1035 phycnt = PHYCNT_ENABLECLK;
1036 phycnt |= (1 << lanes) - 1;
1038 mbps = rcsi2_calc_mbps(priv, format->bpp, lanes);
1042 /* Enable interrupts. */
1043 rcsi2_write(priv, INTEN_REG, INTEN_INT_AFIFO_OF | INTEN_INT_ERRSOTHS
1044 | INTEN_INT_ERRSOTSYNCHS);
1047 rcsi2_write(priv, TREF_REG, TREF_TREF);
1048 rcsi2_write(priv, PHTC_REG, 0);
1051 if (!priv->info->use_isp) {
1052 rcsi2_write(priv, VCDT_REG, vcdt);
1054 rcsi2_write(priv, VCDT2_REG, vcdt2);
1057 /* Lanes are zero indexed. */
1058 rcsi2_write(priv, LSWAP_REG,
1059 LSWAP_L0SEL(priv->lane_swap[0] - 1) |
1060 LSWAP_L1SEL(priv->lane_swap[1] - 1) |
1061 LSWAP_L2SEL(priv->lane_swap[2] - 1) |
1062 LSWAP_L3SEL(priv->lane_swap[3] - 1));
1065 if (priv->info->init_phtw) {
1066 ret = priv->info->init_phtw(priv, mbps);
1071 if (priv->info->hsfreqrange) {
1072 ret = rcsi2_set_phypll(priv, mbps);
1077 if (priv->info->csi0clkfreqrange)
1078 rcsi2_write(priv, CSI0CLKFCPR_REG,
1079 CSI0CLKFREQRANGE(priv->info->csi0clkfreqrange));
1081 if (priv->info->use_isp)
1082 rcsi2_write(priv, PHYFRX_REG,
1083 PHYFRX_FORCERX_MODE_3 | PHYFRX_FORCERX_MODE_2 |
1084 PHYFRX_FORCERX_MODE_1 | PHYFRX_FORCERX_MODE_0);
1086 rcsi2_write(priv, PHYCNT_REG, phycnt);
1087 rcsi2_write(priv, LINKCNT_REG, LINKCNT_MONITOR_EN |
1088 LINKCNT_REG_MONI_PACT_EN | LINKCNT_ICLK_NONSTOP);
1089 rcsi2_write(priv, FLD_REG, fld);
1090 rcsi2_write(priv, PHYCNT_REG, phycnt | PHYCNT_SHUTDOWNZ);
1091 rcsi2_write(priv, PHYCNT_REG, phycnt | PHYCNT_SHUTDOWNZ | PHYCNT_RSTZ);
1093 ret = rcsi2_wait_phy_start(priv, lanes);
1097 if (priv->info->use_isp)
1098 rcsi2_write(priv, PHYFRX_REG, 0);
1100 /* Run post PHY start initialization, if needed. */
1101 if (priv->info->phy_post_init) {
1102 ret = priv->info->phy_post_init(priv);
1107 /* Clear Ultra Low Power interrupt. */
1108 if (priv->info->clear_ulps)
1109 rcsi2_write(priv, INTSTATE_REG,
1110 INTSTATE_INT_ULPS_START |
1111 INTSTATE_INT_ULPS_END);
1115 static int rcsi2_wait_phy_start_v4h(struct rcar_csi2 *priv, u32 match)
1117 unsigned int timeout;
1120 for (timeout = 0; timeout <= 10; timeout++) {
1121 status = rcsi2_read(priv, V4H_ST_PHYST_REG);
1122 if ((status & match) == match)
1125 usleep_range(1000, 2000);
1131 static int rcsi2_c_phy_setting_v4h(struct rcar_csi2 *priv, int msps)
1133 const struct rcsi2_cphy_setting *conf;
1135 for (conf = cphy_setting_table_r8a779g0; conf->msps != 0; conf++) {
1136 if (conf->msps > msps)
1141 dev_err(priv->dev, "Unsupported PHY speed for msps setting (%u Msps)", msps);
1145 /* C-PHY specific */
1146 rcsi2_write16(priv, V4H_CORE_DIG_RW_COMMON_REG(7), 0x0155);
1147 rcsi2_write16(priv, V4H_PPI_STARTUP_RW_COMMON_DPHY_REG(7), 0x0068);
1148 rcsi2_write16(priv, V4H_PPI_STARTUP_RW_COMMON_DPHY_REG(8), 0x0010);
1150 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_0_RW_LP_0_REG, 0x463c);
1151 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_1_RW_LP_0_REG, 0x463c);
1152 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_2_RW_LP_0_REG, 0x463c);
1154 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_0_RW_HS_RX_REG(0), 0x00d5);
1155 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_1_RW_HS_RX_REG(0), 0x00d5);
1156 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_2_RW_HS_RX_REG(0), 0x00d5);
1158 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_0_RW_HS_RX_REG(1), 0x0013);
1159 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_1_RW_HS_RX_REG(1), 0x0013);
1160 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_2_RW_HS_RX_REG(1), 0x0013);
1162 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_0_RW_HS_RX_REG(5), 0x0013);
1163 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_1_RW_HS_RX_REG(5), 0x0013);
1164 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_2_RW_HS_RX_REG(5), 0x0013);
1166 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_0_RW_HS_RX_REG(6), 0x000a);
1167 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_1_RW_HS_RX_REG(6), 0x000a);
1168 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_2_RW_HS_RX_REG(6), 0x000a);
1170 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_0_RW_HS_RX_REG(2), conf->rx2);
1171 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_1_RW_HS_RX_REG(2), conf->rx2);
1172 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_2_RW_HS_RX_REG(2), conf->rx2);
1174 rcsi2_write16(priv, V4H_CORE_DIG_IOCTRL_RW_AFE_LANE0_CTRL_2_REG(2), 0x0001);
1175 rcsi2_write16(priv, V4H_CORE_DIG_IOCTRL_RW_AFE_LANE1_CTRL_2_REG(2), 0);
1176 rcsi2_write16(priv, V4H_CORE_DIG_IOCTRL_RW_AFE_LANE2_CTRL_2_REG(2), 0x0001);
1177 rcsi2_write16(priv, V4H_CORE_DIG_IOCTRL_RW_AFE_LANE3_CTRL_2_REG(2), 0x0001);
1178 rcsi2_write16(priv, V4H_CORE_DIG_IOCTRL_RW_AFE_LANE4_CTRL_2_REG(2), 0);
1180 rcsi2_write16(priv, V4H_CORE_DIG_RW_TRIO0_REG(0), conf->trio0);
1181 rcsi2_write16(priv, V4H_CORE_DIG_RW_TRIO1_REG(0), conf->trio0);
1182 rcsi2_write16(priv, V4H_CORE_DIG_RW_TRIO2_REG(0), conf->trio0);
1184 rcsi2_write16(priv, V4H_CORE_DIG_RW_TRIO0_REG(2), conf->trio2);
1185 rcsi2_write16(priv, V4H_CORE_DIG_RW_TRIO1_REG(2), conf->trio2);
1186 rcsi2_write16(priv, V4H_CORE_DIG_RW_TRIO2_REG(2), conf->trio2);
1188 rcsi2_write16(priv, V4H_CORE_DIG_RW_TRIO0_REG(1), conf->trio1);
1189 rcsi2_write16(priv, V4H_CORE_DIG_RW_TRIO1_REG(1), conf->trio1);
1190 rcsi2_write16(priv, V4H_CORE_DIG_RW_TRIO2_REG(1), conf->trio1);
1193 * Configure pin-swap.
1194 * TODO: This registers is not documented yet, the values should depend
1195 * on the 'clock-lanes' and 'data-lanes' devicetree properties.
1197 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_1_RW_CFG_0_REG, 0xf5);
1198 rcsi2_write16(priv, V4H_CORE_DIG_CLANE_1_RW_HS_TX_6_REG, 0x5000);
1200 /* Leave Shutdown mode */
1201 rcsi2_write(priv, V4H_DPHY_RSTZ_REG, BIT(0));
1202 rcsi2_write(priv, V4H_PHY_SHUTDOWNZ_REG, BIT(0));
1204 /* Wait for calibration */
1205 if (rcsi2_wait_phy_start_v4h(priv, V4H_ST_PHYST_ST_PHY_READY)) {
1206 dev_err(priv->dev, "PHY calibration failed\n");
1210 /* C-PHY setting - analog programing*/
1211 rcsi2_write16(priv, V4H_CORE_DIG_IOCTRL_RW_AFE_LANE0_CTRL_2_REG(9), conf->lane29);
1212 rcsi2_write16(priv, V4H_CORE_DIG_IOCTRL_RW_AFE_LANE0_CTRL_2_REG(7), conf->lane27);
1217 static int rcsi2_start_receiver_v4h(struct rcar_csi2 *priv,
1218 struct v4l2_subdev_state *state)
1220 const struct rcar_csi2_format *format;
1221 const struct v4l2_mbus_framefmt *fmt;
1226 /* Use the format on the sink pad to compute the receiver config. */
1227 fmt = v4l2_subdev_state_get_format(state, RCAR_CSI2_SINK);
1228 format = rcsi2_code_to_fmt(fmt->code);
1232 ret = rcsi2_get_active_lanes(priv, &lanes);
1236 msps = rcsi2_calc_mbps(priv, format->bpp, lanes);
1240 /* Reset LINK and PHY*/
1241 rcsi2_write(priv, V4H_CSI2_RESETN_REG, 0);
1242 rcsi2_write(priv, V4H_DPHY_RSTZ_REG, 0);
1243 rcsi2_write(priv, V4H_PHY_SHUTDOWNZ_REG, 0);
1245 /* PHY static setting */
1246 rcsi2_write(priv, V4H_PHY_EN_REG, V4H_PHY_EN_ENABLE_CLK);
1247 rcsi2_write(priv, V4H_FLDC_REG, 0);
1248 rcsi2_write(priv, V4H_FLDD_REG, 0);
1249 rcsi2_write(priv, V4H_IDIC_REG, 0);
1250 rcsi2_write(priv, V4H_PHY_MODE_REG, V4H_PHY_MODE_CPHY);
1251 rcsi2_write(priv, V4H_N_LANES_REG, lanes - 1);
1254 rcsi2_write(priv, V4H_CSI2_RESETN_REG, BIT(0));
1256 /* Registers static setting through APB */
1257 /* Common setting */
1258 rcsi2_write16(priv, V4H_CORE_DIG_ANACTRL_RW_COMMON_ANACTRL_REG(0), 0x1bfd);
1259 rcsi2_write16(priv, V4H_PPI_STARTUP_RW_COMMON_STARTUP_1_1_REG, 0x0233);
1260 rcsi2_write16(priv, V4H_PPI_STARTUP_RW_COMMON_DPHY_REG(6), 0x0027);
1261 rcsi2_write16(priv, V4H_PPI_CALIBCTRL_RW_COMMON_BG_0_REG, 0x01f4);
1262 rcsi2_write16(priv, V4H_PPI_RW_TERMCAL_CFG_0_REG, 0x0013);
1263 rcsi2_write16(priv, V4H_PPI_RW_OFFSETCAL_CFG_0_REG, 0x0003);
1264 rcsi2_write16(priv, V4H_PPI_RW_LPDCOCAL_TIMEBASE_REG, 0x004f);
1265 rcsi2_write16(priv, V4H_PPI_RW_LPDCOCAL_NREF_REG, 0x0320);
1266 rcsi2_write16(priv, V4H_PPI_RW_LPDCOCAL_NREF_RANGE_REG, 0x000f);
1267 rcsi2_write16(priv, V4H_PPI_RW_LPDCOCAL_TWAIT_CONFIG_REG, 0xfe18);
1268 rcsi2_write16(priv, V4H_PPI_RW_LPDCOCAL_VT_CONFIG_REG, 0x0c3c);
1269 rcsi2_write16(priv, V4H_PPI_RW_LPDCOCAL_COARSE_CFG_REG, 0x0105);
1270 rcsi2_write16(priv, V4H_CORE_DIG_IOCTRL_RW_AFE_CB_CTRL_2_REG(6), 0x1000);
1271 rcsi2_write16(priv, V4H_PPI_RW_COMMON_CFG_REG, 0x0003);
1273 /* C-PHY settings */
1274 ret = rcsi2_c_phy_setting_v4h(priv, msps);
1278 rcsi2_wait_phy_start_v4h(priv, V4H_ST_PHYST_ST_STOPSTATE_0 |
1279 V4H_ST_PHYST_ST_STOPSTATE_1 |
1280 V4H_ST_PHYST_ST_STOPSTATE_2);
1285 static int rcsi2_d_phy_setting_v4m(struct rcar_csi2 *priv, int data_rate)
1287 unsigned int timeout;
1290 static const struct phtw_value step1[] = {
1291 { .data = 0x00, .code = 0x00 },
1292 { .data = 0x00, .code = 0x1e },
1295 /* Shutdown and reset PHY. */
1296 rcsi2_write(priv, V4H_DPHY_RSTZ_REG, BIT(0));
1297 rcsi2_write(priv, V4H_PHY_SHUTDOWNZ_REG, BIT(0));
1299 /* Start internal calibration (POR). */
1300 ret = rcsi2_phtw_write_array(priv, step1, ARRAY_SIZE(step1));
1304 /* Wait for POR to complete. */
1305 for (timeout = 10; timeout > 0; timeout--) {
1306 if ((rcsi2_read(priv, V4M_PHTR_REG) & 0xf0000) == 0x70000)
1308 usleep_range(1000, 2000);
1312 dev_err(priv->dev, "D-PHY calibration failed\n");
1319 static int rcsi2_set_osc_freq(struct rcar_csi2 *priv, unsigned int mbps)
1321 const struct rcsi2_mbps_info *info;
1322 struct phtw_value steps[] = {
1323 { .data = 0x00, .code = 0x00 },
1324 { .code = 0xe2 }, /* Data filled in below. */
1325 { .code = 0xe3 }, /* Data filled in below. */
1326 { .data = 0x01, .code = 0xe4 },
1329 info = rcsi2_mbps_to_info(priv, priv->info->hsfreqrange, mbps);
1333 /* Fill in data for command. */
1334 steps[1].data = (info->osc_freq & 0x00ff) >> 0;
1335 steps[2].data = (info->osc_freq & 0x0f00) >> 8;
1337 return rcsi2_phtw_write_array(priv, steps, ARRAY_SIZE(steps));
1340 static int rcsi2_init_common_v4m(struct rcar_csi2 *priv, unsigned int mbps)
1344 static const struct phtw_value step1[] = {
1345 { .data = 0x00, .code = 0x00 },
1346 { .data = 0x3c, .code = 0x08 },
1349 static const struct phtw_value step2[] = {
1350 { .data = 0x00, .code = 0x00 },
1351 { .data = 0x80, .code = 0xe0 },
1352 { .data = 0x01, .code = 0xe1 },
1353 { .data = 0x06, .code = 0x00 },
1354 { .data = 0x0f, .code = 0x11 },
1355 { .data = 0x08, .code = 0x00 },
1356 { .data = 0x0f, .code = 0x11 },
1357 { .data = 0x0a, .code = 0x00 },
1358 { .data = 0x0f, .code = 0x11 },
1359 { .data = 0x0c, .code = 0x00 },
1360 { .data = 0x0f, .code = 0x11 },
1361 { .data = 0x01, .code = 0x00 },
1362 { .data = 0x31, .code = 0xaa },
1363 { .data = 0x05, .code = 0x00 },
1364 { .data = 0x05, .code = 0x09 },
1365 { .data = 0x07, .code = 0x00 },
1366 { .data = 0x05, .code = 0x09 },
1367 { .data = 0x09, .code = 0x00 },
1368 { .data = 0x05, .code = 0x09 },
1369 { .data = 0x0b, .code = 0x00 },
1370 { .data = 0x05, .code = 0x09 },
1373 if (priv->info->hsfreqrange) {
1374 ret = rcsi2_set_phypll(priv, mbps);
1378 ret = rcsi2_set_osc_freq(priv, mbps);
1384 ret = rcsi2_phtw_write_array(priv, step1, ARRAY_SIZE(step1));
1389 if (priv->info->csi0clkfreqrange)
1390 rcsi2_write(priv, V4M_CSI0CLKFCPR_REG,
1391 CSI0CLKFREQRANGE(priv->info->csi0clkfreqrange));
1393 rcsi2_write(priv, V4H_PHY_EN_REG, V4H_PHY_EN_ENABLE_CLK |
1394 V4H_PHY_EN_ENABLE_0 | V4H_PHY_EN_ENABLE_1 |
1395 V4H_PHY_EN_ENABLE_2 | V4H_PHY_EN_ENABLE_3);
1398 ret = rcsi2_phtw_write_array(priv, step2, ARRAY_SIZE(step2));
1406 static int rcsi2_start_receiver_v4m(struct rcar_csi2 *priv,
1407 struct v4l2_subdev_state *state)
1409 const struct rcar_csi2_format *format;
1410 const struct v4l2_mbus_framefmt *fmt;
1415 /* Calculate parameters */
1416 fmt = v4l2_subdev_state_get_format(state, RCAR_CSI2_SINK);
1417 format = rcsi2_code_to_fmt(fmt->code);
1421 ret = rcsi2_get_active_lanes(priv, &lanes);
1425 mbps = rcsi2_calc_mbps(priv, format->bpp, lanes);
1429 /* Reset LINK and PHY */
1430 rcsi2_write(priv, V4H_CSI2_RESETN_REG, 0);
1431 rcsi2_write(priv, V4H_DPHY_RSTZ_REG, 0);
1432 rcsi2_write(priv, V4H_PHY_SHUTDOWNZ_REG, 0);
1433 rcsi2_write(priv, V4M_PHTC_REG, PHTC_TESTCLR);
1435 /* PHY static setting */
1436 rcsi2_write(priv, V4H_PHY_EN_REG, V4H_PHY_EN_ENABLE_CLK);
1437 rcsi2_write(priv, V4H_FLDC_REG, 0);
1438 rcsi2_write(priv, V4H_FLDD_REG, 0);
1439 rcsi2_write(priv, V4H_IDIC_REG, 0);
1440 rcsi2_write(priv, V4H_PHY_MODE_REG, V4H_PHY_MODE_DPHY);
1441 rcsi2_write(priv, V4H_N_LANES_REG, lanes - 1);
1443 rcsi2_write(priv, V4M_FRXM_REG,
1444 V4M_FRXM_FORCERXMODE_0 | V4M_FRXM_FORCERXMODE_1 |
1445 V4M_FRXM_FORCERXMODE_2 | V4M_FRXM_FORCERXMODE_3);
1446 rcsi2_write(priv, V4M_OVR1_REG,
1447 V4M_OVR1_FORCERXMODE_0 | V4M_OVR1_FORCERXMODE_1 |
1448 V4M_OVR1_FORCERXMODE_2 | V4M_OVR1_FORCERXMODE_3);
1451 rcsi2_write(priv, V4M_PHTC_REG, 0);
1452 rcsi2_write(priv, V4H_CSI2_RESETN_REG, BIT(0));
1454 /* Common settings */
1455 ret = rcsi2_init_common_v4m(priv, mbps);
1459 /* D-PHY settings */
1460 ret = rcsi2_d_phy_setting_v4m(priv, mbps);
1464 rcsi2_wait_phy_start_v4h(priv, V4H_ST_PHYST_ST_STOPSTATE_0 |
1465 V4H_ST_PHYST_ST_STOPSTATE_1 |
1466 V4H_ST_PHYST_ST_STOPSTATE_2 |
1467 V4H_ST_PHYST_ST_STOPSTATE_3);
1469 rcsi2_write(priv, V4M_FRXM_REG, 0);
1474 static int rcsi2_start(struct rcar_csi2 *priv, struct v4l2_subdev_state *state)
1478 ret = rcsi2_exit_standby(priv);
1482 ret = priv->info->start_receiver(priv, state);
1484 rcsi2_enter_standby(priv);
1488 ret = v4l2_subdev_call(priv->remote, video, s_stream, 1);
1490 rcsi2_enter_standby(priv);
1497 static void rcsi2_stop(struct rcar_csi2 *priv)
1499 rcsi2_enter_standby(priv);
1500 v4l2_subdev_call(priv->remote, video, s_stream, 0);
1503 static int rcsi2_s_stream(struct v4l2_subdev *sd, int enable)
1505 struct rcar_csi2 *priv = sd_to_csi2(sd);
1506 struct v4l2_subdev_state *state;
1512 state = v4l2_subdev_lock_and_get_active_state(&priv->subdev);
1514 if (enable && priv->stream_count == 0) {
1515 ret = rcsi2_start(priv, state);
1518 } else if (!enable && priv->stream_count == 1) {
1522 priv->stream_count += enable ? 1 : -1;
1524 v4l2_subdev_unlock_state(state);
1529 static int rcsi2_set_pad_format(struct v4l2_subdev *sd,
1530 struct v4l2_subdev_state *state,
1531 struct v4l2_subdev_format *format)
1533 struct rcar_csi2 *priv = sd_to_csi2(sd);
1534 unsigned int num_pads = rcsi2_num_pads(priv);
1536 if (format->pad > RCAR_CSI2_SINK)
1537 return v4l2_subdev_get_fmt(sd, state, format);
1539 if (!rcsi2_code_to_fmt(format->format.code))
1540 format->format.code = rcar_csi2_formats[0].code;
1542 *v4l2_subdev_state_get_format(state, format->pad) = format->format;
1544 /* Propagate the format to the source pads. */
1545 for (unsigned int i = RCAR_CSI2_SOURCE_VC0; i < num_pads; i++)
1546 *v4l2_subdev_state_get_format(state, i) = format->format;
1551 static const struct v4l2_subdev_video_ops rcar_csi2_video_ops = {
1552 .s_stream = rcsi2_s_stream,
1555 static const struct v4l2_subdev_pad_ops rcar_csi2_pad_ops = {
1556 .set_fmt = rcsi2_set_pad_format,
1557 .get_fmt = v4l2_subdev_get_fmt,
1560 static const struct v4l2_subdev_ops rcar_csi2_subdev_ops = {
1561 .video = &rcar_csi2_video_ops,
1562 .pad = &rcar_csi2_pad_ops,
1565 static int rcsi2_init_state(struct v4l2_subdev *sd,
1566 struct v4l2_subdev_state *state)
1568 struct rcar_csi2 *priv = sd_to_csi2(sd);
1569 unsigned int num_pads = rcsi2_num_pads(priv);
1571 static const struct v4l2_mbus_framefmt rcar_csi2_default_fmt = {
1574 .code = MEDIA_BUS_FMT_RGB888_1X24,
1575 .colorspace = V4L2_COLORSPACE_SRGB,
1576 .field = V4L2_FIELD_NONE,
1577 .ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT,
1578 .quantization = V4L2_QUANTIZATION_DEFAULT,
1579 .xfer_func = V4L2_XFER_FUNC_DEFAULT,
1582 for (unsigned int i = RCAR_CSI2_SINK; i < num_pads; i++)
1583 *v4l2_subdev_state_get_format(state, i) = rcar_csi2_default_fmt;
1588 static const struct v4l2_subdev_internal_ops rcar_csi2_internal_ops = {
1589 .init_state = rcsi2_init_state,
1592 static irqreturn_t rcsi2_irq(int irq, void *data)
1594 struct rcar_csi2 *priv = data;
1595 u32 status, err_status;
1597 status = rcsi2_read(priv, INTSTATE_REG);
1598 err_status = rcsi2_read(priv, INTERRSTATE_REG);
1603 rcsi2_write(priv, INTSTATE_REG, status);
1608 rcsi2_write(priv, INTERRSTATE_REG, err_status);
1610 dev_info(priv->dev, "Transfer error, restarting CSI-2 receiver\n");
1612 return IRQ_WAKE_THREAD;
1615 static irqreturn_t rcsi2_irq_thread(int irq, void *data)
1617 struct v4l2_subdev_state *state;
1618 struct rcar_csi2 *priv = data;
1620 state = v4l2_subdev_lock_and_get_active_state(&priv->subdev);
1623 usleep_range(1000, 2000);
1624 if (rcsi2_start(priv, state))
1625 dev_warn(priv->dev, "Failed to restart CSI-2 receiver\n");
1627 v4l2_subdev_unlock_state(state);
1632 /* -----------------------------------------------------------------------------
1633 * Async handling and registration of subdevices and links.
1636 static int rcsi2_notify_bound(struct v4l2_async_notifier *notifier,
1637 struct v4l2_subdev *subdev,
1638 struct v4l2_async_connection *asc)
1640 struct rcar_csi2 *priv = notifier_to_csi2(notifier);
1643 pad = media_entity_get_fwnode_pad(&subdev->entity, asc->match.fwnode,
1644 MEDIA_PAD_FL_SOURCE);
1646 dev_err(priv->dev, "Failed to find pad for %s\n", subdev->name);
1650 priv->remote = subdev;
1651 priv->remote_pad = pad;
1653 dev_dbg(priv->dev, "Bound %s pad: %d\n", subdev->name, pad);
1655 return media_create_pad_link(&subdev->entity, pad,
1656 &priv->subdev.entity, 0,
1657 MEDIA_LNK_FL_ENABLED |
1658 MEDIA_LNK_FL_IMMUTABLE);
1661 static void rcsi2_notify_unbind(struct v4l2_async_notifier *notifier,
1662 struct v4l2_subdev *subdev,
1663 struct v4l2_async_connection *asc)
1665 struct rcar_csi2 *priv = notifier_to_csi2(notifier);
1667 priv->remote = NULL;
1669 dev_dbg(priv->dev, "Unbind %s\n", subdev->name);
1672 static const struct v4l2_async_notifier_operations rcar_csi2_notify_ops = {
1673 .bound = rcsi2_notify_bound,
1674 .unbind = rcsi2_notify_unbind,
1677 static int rcsi2_parse_v4l2(struct rcar_csi2 *priv,
1678 struct v4l2_fwnode_endpoint *vep)
1682 /* Only port 0 endpoint 0 is valid. */
1683 if (vep->base.port || vep->base.id)
1686 priv->lanes = vep->bus.mipi_csi2.num_data_lanes;
1688 switch (vep->bus_type) {
1689 case V4L2_MBUS_CSI2_DPHY:
1690 if (!priv->info->support_dphy) {
1691 dev_err(priv->dev, "D-PHY not supported\n");
1695 if (priv->lanes != 1 && priv->lanes != 2 && priv->lanes != 4) {
1697 "Unsupported number of data-lanes for D-PHY: %u\n",
1704 case V4L2_MBUS_CSI2_CPHY:
1705 if (!priv->info->support_cphy) {
1706 dev_err(priv->dev, "C-PHY not supported\n");
1710 if (priv->lanes != 3) {
1712 "Unsupported number of data-lanes for C-PHY: %u\n",
1720 dev_err(priv->dev, "Unsupported bus: %u\n", vep->bus_type);
1724 for (i = 0; i < ARRAY_SIZE(priv->lane_swap); i++) {
1725 priv->lane_swap[i] = i < priv->lanes ?
1726 vep->bus.mipi_csi2.data_lanes[i] : i;
1728 /* Check for valid lane number. */
1729 if (priv->lane_swap[i] < 1 || priv->lane_swap[i] > 4) {
1730 dev_err(priv->dev, "data-lanes must be in 1-4 range\n");
1738 static int rcsi2_parse_dt(struct rcar_csi2 *priv)
1740 struct v4l2_async_connection *asc;
1741 struct fwnode_handle *fwnode;
1742 struct fwnode_handle *ep;
1743 struct v4l2_fwnode_endpoint v4l2_ep = {
1744 .bus_type = V4L2_MBUS_UNKNOWN,
1748 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(priv->dev), 0, 0, 0);
1750 dev_err(priv->dev, "Not connected to subdevice\n");
1754 ret = v4l2_fwnode_endpoint_parse(ep, &v4l2_ep);
1756 dev_err(priv->dev, "Could not parse v4l2 endpoint\n");
1757 fwnode_handle_put(ep);
1761 ret = rcsi2_parse_v4l2(priv, &v4l2_ep);
1763 fwnode_handle_put(ep);
1767 fwnode = fwnode_graph_get_remote_endpoint(ep);
1768 fwnode_handle_put(ep);
1770 dev_dbg(priv->dev, "Found '%pOF'\n", to_of_node(fwnode));
1772 v4l2_async_subdev_nf_init(&priv->notifier, &priv->subdev);
1773 priv->notifier.ops = &rcar_csi2_notify_ops;
1775 asc = v4l2_async_nf_add_fwnode(&priv->notifier, fwnode,
1776 struct v4l2_async_connection);
1777 fwnode_handle_put(fwnode);
1779 return PTR_ERR(asc);
1781 ret = v4l2_async_nf_register(&priv->notifier);
1783 v4l2_async_nf_cleanup(&priv->notifier);
1788 /* -----------------------------------------------------------------------------
1789 * PHTW initialization sequences.
1791 * NOTE: Magic values are from the datasheet and lack documentation.
1794 static int rcsi2_phtw_write_mbps(struct rcar_csi2 *priv, unsigned int mbps,
1795 const struct rcsi2_mbps_info *values, u8 code)
1797 const struct rcsi2_mbps_info *info;
1799 info = rcsi2_mbps_to_info(priv, values, mbps);
1803 return rcsi2_phtw_write(priv, info->reg, code);
1806 static int __rcsi2_init_phtw_h3_v3h_m3n(struct rcar_csi2 *priv,
1809 static const struct phtw_value step1[] = {
1810 { .data = 0xcc, .code = 0xe2 },
1811 { .data = 0x01, .code = 0xe3 },
1812 { .data = 0x11, .code = 0xe4 },
1813 { .data = 0x01, .code = 0xe5 },
1814 { .data = 0x10, .code = 0x04 },
1817 static const struct phtw_value step2[] = {
1818 { .data = 0x38, .code = 0x08 },
1819 { .data = 0x01, .code = 0x00 },
1820 { .data = 0x4b, .code = 0xac },
1821 { .data = 0x03, .code = 0x00 },
1822 { .data = 0x80, .code = 0x07 },
1827 ret = rcsi2_phtw_write_array(priv, step1, ARRAY_SIZE(step1));
1831 if (mbps != 0 && mbps <= 250) {
1832 ret = rcsi2_phtw_write(priv, 0x39, 0x05);
1836 ret = rcsi2_phtw_write_mbps(priv, mbps, phtw_mbps_h3_v3h_m3n,
1842 return rcsi2_phtw_write_array(priv, step2, ARRAY_SIZE(step2));
1845 static int rcsi2_init_phtw_h3_v3h_m3n(struct rcar_csi2 *priv, unsigned int mbps)
1847 return __rcsi2_init_phtw_h3_v3h_m3n(priv, mbps);
1850 static int rcsi2_init_phtw_h3es2(struct rcar_csi2 *priv, unsigned int mbps)
1852 return __rcsi2_init_phtw_h3_v3h_m3n(priv, 0);
1855 static int rcsi2_init_phtw_v3m_e3(struct rcar_csi2 *priv, unsigned int mbps)
1857 return rcsi2_phtw_write_mbps(priv, mbps, phtw_mbps_v3m_e3, 0x44);
1860 static int rcsi2_phy_post_init_v3m_e3(struct rcar_csi2 *priv)
1862 static const struct phtw_value step1[] = {
1863 { .data = 0xee, .code = 0x34 },
1864 { .data = 0xee, .code = 0x44 },
1865 { .data = 0xee, .code = 0x54 },
1866 { .data = 0xee, .code = 0x84 },
1867 { .data = 0xee, .code = 0x94 },
1870 return rcsi2_phtw_write_array(priv, step1, ARRAY_SIZE(step1));
1873 static int rcsi2_init_phtw_v3u(struct rcar_csi2 *priv,
1876 /* In case of 1500Mbps or less */
1877 static const struct phtw_value step1[] = {
1878 { .data = 0xcc, .code = 0xe2 },
1881 static const struct phtw_value step2[] = {
1882 { .data = 0x01, .code = 0xe3 },
1883 { .data = 0x11, .code = 0xe4 },
1884 { .data = 0x01, .code = 0xe5 },
1887 /* In case of 1500Mbps or less */
1888 static const struct phtw_value step3[] = {
1889 { .data = 0x38, .code = 0x08 },
1892 static const struct phtw_value step4[] = {
1893 { .data = 0x01, .code = 0x00 },
1894 { .data = 0x4b, .code = 0xac },
1895 { .data = 0x03, .code = 0x00 },
1896 { .data = 0x80, .code = 0x07 },
1901 if (mbps != 0 && mbps <= 1500)
1902 ret = rcsi2_phtw_write_array(priv, step1, ARRAY_SIZE(step1));
1904 ret = rcsi2_phtw_write_mbps(priv, mbps, phtw_mbps_v3u, 0xe2);
1908 ret = rcsi2_phtw_write_array(priv, step2, ARRAY_SIZE(step2));
1912 if (mbps != 0 && mbps <= 1500) {
1913 ret = rcsi2_phtw_write_array(priv, step3, ARRAY_SIZE(step3));
1918 ret = rcsi2_phtw_write_array(priv, step4, ARRAY_SIZE(step4));
1925 /* -----------------------------------------------------------------------------
1926 * Platform Device Driver.
1929 static int rcsi2_link_setup(struct media_entity *entity,
1930 const struct media_pad *local,
1931 const struct media_pad *remote, u32 flags)
1933 struct v4l2_subdev *sd = media_entity_to_v4l2_subdev(entity);
1934 struct rcar_csi2 *priv = sd_to_csi2(sd);
1935 struct video_device *vdev;
1939 if (!is_media_entity_v4l2_video_device(remote->entity)) {
1940 dev_err(priv->dev, "Remote is not a video device\n");
1944 vdev = media_entity_to_video_device(remote->entity);
1946 if (of_property_read_u32(vdev->dev_parent->of_node, "renesas,id", &id)) {
1947 dev_err(priv->dev, "No renesas,id, can't configure routing\n");
1953 if (flags & MEDIA_LNK_FL_ENABLED) {
1954 if (media_pad_remote_pad_first(local)) {
1956 "Each VC can only be routed to one output channel\n");
1960 vc = local->index - 1;
1962 dev_dbg(priv->dev, "Route VC%d to VIN%u on output channel %d\n",
1968 priv->channel_vc[channel] = vc;
1973 static const struct media_entity_operations rcar_csi2_entity_ops = {
1974 .link_setup = rcsi2_link_setup,
1975 .link_validate = v4l2_subdev_link_validate,
1978 static int rcsi2_probe_resources(struct rcar_csi2 *priv,
1979 struct platform_device *pdev)
1983 priv->base = devm_platform_ioremap_resource(pdev, 0);
1984 if (IS_ERR(priv->base))
1985 return PTR_ERR(priv->base);
1987 irq = platform_get_irq(pdev, 0);
1991 ret = devm_request_threaded_irq(&pdev->dev, irq, rcsi2_irq,
1992 rcsi2_irq_thread, IRQF_SHARED,
1993 KBUILD_MODNAME, priv);
1997 priv->rstc = devm_reset_control_get(&pdev->dev, NULL);
1999 return PTR_ERR_OR_ZERO(priv->rstc);
2002 static const struct rcsi2_register_layout rcsi2_registers_gen3 = {
2004 .phypll = PHYPLL_REG,
2007 static const struct rcar_csi2_info rcar_csi2_info_r8a7795 = {
2008 .regs = &rcsi2_registers_gen3,
2009 .init_phtw = rcsi2_init_phtw_h3_v3h_m3n,
2010 .start_receiver = rcsi2_start_receiver_gen3,
2011 .enter_standby = rcsi2_enter_standby_gen3,
2012 .hsfreqrange = hsfreqrange_h3_v3h_m3n,
2013 .csi0clkfreqrange = 0x20,
2016 .support_dphy = true,
2019 static const struct rcar_csi2_info rcar_csi2_info_r8a7795es2 = {
2020 .regs = &rcsi2_registers_gen3,
2021 .init_phtw = rcsi2_init_phtw_h3es2,
2022 .start_receiver = rcsi2_start_receiver_gen3,
2023 .enter_standby = rcsi2_enter_standby_gen3,
2024 .hsfreqrange = hsfreqrange_h3_v3h_m3n,
2025 .csi0clkfreqrange = 0x20,
2028 .support_dphy = true,
2031 static const struct rcar_csi2_info rcar_csi2_info_r8a7796 = {
2032 .regs = &rcsi2_registers_gen3,
2033 .start_receiver = rcsi2_start_receiver_gen3,
2034 .enter_standby = rcsi2_enter_standby_gen3,
2035 .hsfreqrange = hsfreqrange_m3w,
2037 .support_dphy = true,
2040 static const struct rcar_csi2_info rcar_csi2_info_r8a77961 = {
2041 .regs = &rcsi2_registers_gen3,
2042 .start_receiver = rcsi2_start_receiver_gen3,
2043 .enter_standby = rcsi2_enter_standby_gen3,
2044 .hsfreqrange = hsfreqrange_m3w,
2046 .support_dphy = true,
2049 static const struct rcar_csi2_info rcar_csi2_info_r8a77965 = {
2050 .regs = &rcsi2_registers_gen3,
2051 .init_phtw = rcsi2_init_phtw_h3_v3h_m3n,
2052 .start_receiver = rcsi2_start_receiver_gen3,
2053 .enter_standby = rcsi2_enter_standby_gen3,
2054 .hsfreqrange = hsfreqrange_h3_v3h_m3n,
2055 .csi0clkfreqrange = 0x20,
2058 .support_dphy = true,
2061 static const struct rcar_csi2_info rcar_csi2_info_r8a77970 = {
2062 .regs = &rcsi2_registers_gen3,
2063 .init_phtw = rcsi2_init_phtw_v3m_e3,
2064 .phy_post_init = rcsi2_phy_post_init_v3m_e3,
2065 .start_receiver = rcsi2_start_receiver_gen3,
2066 .enter_standby = rcsi2_enter_standby_gen3,
2068 .support_dphy = true,
2071 static const struct rcar_csi2_info rcar_csi2_info_r8a77980 = {
2072 .regs = &rcsi2_registers_gen3,
2073 .init_phtw = rcsi2_init_phtw_h3_v3h_m3n,
2074 .start_receiver = rcsi2_start_receiver_gen3,
2075 .enter_standby = rcsi2_enter_standby_gen3,
2076 .hsfreqrange = hsfreqrange_h3_v3h_m3n,
2077 .csi0clkfreqrange = 0x20,
2079 .support_dphy = true,
2082 static const struct rcar_csi2_info rcar_csi2_info_r8a77990 = {
2083 .regs = &rcsi2_registers_gen3,
2084 .init_phtw = rcsi2_init_phtw_v3m_e3,
2085 .phy_post_init = rcsi2_phy_post_init_v3m_e3,
2086 .start_receiver = rcsi2_start_receiver_gen3,
2087 .enter_standby = rcsi2_enter_standby_gen3,
2089 .support_dphy = true,
2092 static const struct rcar_csi2_info rcar_csi2_info_r8a779a0 = {
2093 .regs = &rcsi2_registers_gen3,
2094 .init_phtw = rcsi2_init_phtw_v3u,
2095 .start_receiver = rcsi2_start_receiver_gen3,
2096 .enter_standby = rcsi2_enter_standby_gen3,
2097 .hsfreqrange = hsfreqrange_v3u,
2098 .csi0clkfreqrange = 0x20,
2101 .support_dphy = true,
2104 static const struct rcar_csi2_info rcar_csi2_info_r8a779g0 = {
2105 .regs = &rcsi2_registers_gen3,
2106 .start_receiver = rcsi2_start_receiver_v4h,
2108 .support_cphy = true,
2111 static const struct rcsi2_register_layout rcsi2_registers_v4m = {
2112 .phtw = V4M_PHTW_REG,
2113 .phypll = V4M_PHYPLL_REG,
2116 static const struct rcar_csi2_info rcar_csi2_info_r8a779h0 = {
2117 .regs = &rcsi2_registers_v4m,
2118 .start_receiver = rcsi2_start_receiver_v4m,
2119 .hsfreqrange = hsfreqrange_v4m,
2120 .csi0clkfreqrange = 0x0c,
2122 .support_dphy = true,
2125 static const struct of_device_id rcar_csi2_of_table[] = {
2127 .compatible = "renesas,r8a774a1-csi2",
2128 .data = &rcar_csi2_info_r8a7796,
2131 .compatible = "renesas,r8a774b1-csi2",
2132 .data = &rcar_csi2_info_r8a77965,
2135 .compatible = "renesas,r8a774c0-csi2",
2136 .data = &rcar_csi2_info_r8a77990,
2139 .compatible = "renesas,r8a774e1-csi2",
2140 .data = &rcar_csi2_info_r8a7795,
2143 .compatible = "renesas,r8a7795-csi2",
2144 .data = &rcar_csi2_info_r8a7795,
2147 .compatible = "renesas,r8a7796-csi2",
2148 .data = &rcar_csi2_info_r8a7796,
2151 .compatible = "renesas,r8a77961-csi2",
2152 .data = &rcar_csi2_info_r8a77961,
2155 .compatible = "renesas,r8a77965-csi2",
2156 .data = &rcar_csi2_info_r8a77965,
2159 .compatible = "renesas,r8a77970-csi2",
2160 .data = &rcar_csi2_info_r8a77970,
2163 .compatible = "renesas,r8a77980-csi2",
2164 .data = &rcar_csi2_info_r8a77980,
2167 .compatible = "renesas,r8a77990-csi2",
2168 .data = &rcar_csi2_info_r8a77990,
2171 .compatible = "renesas,r8a779a0-csi2",
2172 .data = &rcar_csi2_info_r8a779a0,
2175 .compatible = "renesas,r8a779g0-csi2",
2176 .data = &rcar_csi2_info_r8a779g0,
2179 .compatible = "renesas,r8a779h0-csi2",
2180 .data = &rcar_csi2_info_r8a779h0,
2184 MODULE_DEVICE_TABLE(of, rcar_csi2_of_table);
2186 static const struct soc_device_attribute r8a7795[] = {
2188 .soc_id = "r8a7795", .revision = "ES2.*",
2189 .data = &rcar_csi2_info_r8a7795es2,
2194 static int rcsi2_probe(struct platform_device *pdev)
2196 const struct soc_device_attribute *attr;
2197 struct rcar_csi2 *priv;
2198 unsigned int i, num_pads;
2201 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
2205 priv->info = of_device_get_match_data(&pdev->dev);
2208 * The different ES versions of r8a7795 (H3) behave differently but
2209 * share the same compatible string.
2211 attr = soc_device_match(r8a7795);
2213 priv->info = attr->data;
2215 priv->dev = &pdev->dev;
2217 priv->stream_count = 0;
2219 ret = rcsi2_probe_resources(priv, pdev);
2221 dev_err(priv->dev, "Failed to get resources\n");
2225 platform_set_drvdata(pdev, priv);
2227 ret = rcsi2_parse_dt(priv);
2231 priv->subdev.owner = THIS_MODULE;
2232 priv->subdev.dev = &pdev->dev;
2233 priv->subdev.internal_ops = &rcar_csi2_internal_ops;
2234 v4l2_subdev_init(&priv->subdev, &rcar_csi2_subdev_ops);
2235 v4l2_set_subdevdata(&priv->subdev, &pdev->dev);
2236 snprintf(priv->subdev.name, sizeof(priv->subdev.name), "%s %s",
2237 KBUILD_MODNAME, dev_name(&pdev->dev));
2238 priv->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
2240 priv->subdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_PIXEL_FORMATTER;
2241 priv->subdev.entity.ops = &rcar_csi2_entity_ops;
2243 num_pads = rcsi2_num_pads(priv);
2245 priv->pads[RCAR_CSI2_SINK].flags = MEDIA_PAD_FL_SINK;
2246 for (i = RCAR_CSI2_SOURCE_VC0; i < num_pads; i++)
2247 priv->pads[i].flags = MEDIA_PAD_FL_SOURCE;
2249 ret = media_entity_pads_init(&priv->subdev.entity, num_pads,
2254 for (i = 0; i < ARRAY_SIZE(priv->channel_vc); i++)
2255 priv->channel_vc[i] = -1;
2257 pm_runtime_enable(&pdev->dev);
2259 ret = v4l2_subdev_init_finalize(&priv->subdev);
2261 goto error_pm_runtime;
2263 ret = v4l2_async_register_subdev(&priv->subdev);
2267 dev_info(priv->dev, "%d lanes found\n", priv->lanes);
2272 v4l2_subdev_cleanup(&priv->subdev);
2274 pm_runtime_disable(&pdev->dev);
2276 v4l2_async_nf_unregister(&priv->notifier);
2277 v4l2_async_nf_cleanup(&priv->notifier);
2282 static void rcsi2_remove(struct platform_device *pdev)
2284 struct rcar_csi2 *priv = platform_get_drvdata(pdev);
2286 v4l2_async_nf_unregister(&priv->notifier);
2287 v4l2_async_nf_cleanup(&priv->notifier);
2288 v4l2_async_unregister_subdev(&priv->subdev);
2289 v4l2_subdev_cleanup(&priv->subdev);
2291 pm_runtime_disable(&pdev->dev);
2294 static struct platform_driver rcar_csi2_pdrv = {
2295 .remove = rcsi2_remove,
2296 .probe = rcsi2_probe,
2298 .name = "rcar-csi2",
2299 .suppress_bind_attrs = true,
2300 .of_match_table = rcar_csi2_of_table,
2304 module_platform_driver(rcar_csi2_pdrv);
2307 MODULE_DESCRIPTION("Renesas R-Car MIPI CSI-2 receiver driver");
2308 MODULE_LICENSE("GPL");