1 // SPDX-License-Identifier: GPL-2.0
3 * camss-csiphy-3ph-1-0.c
5 * Qualcomm MSM Camera Subsystem - CSIPHY Module 3phase v1.0
7 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
8 * Copyright (C) 2016-2018 Linaro Ltd.
11 #include "camss-csiphy.h"
13 #include <linux/delay.h>
14 #include <linux/interrupt.h>
17 #define CSIPHY_3PH_LNn_CFG1(n) (0x000 + 0x100 * (n))
18 #define CSIPHY_3PH_LNn_CFG1_SWI_REC_DLY_PRG (BIT(7) | BIT(6))
19 #define CSIPHY_3PH_LNn_CFG2(n) (0x004 + 0x100 * (n))
20 #define CSIPHY_3PH_LNn_CFG2_LP_REC_EN_INT BIT(3)
21 #define CSIPHY_3PH_LNn_CFG3(n) (0x008 + 0x100 * (n))
22 #define CSIPHY_3PH_LNn_CFG4(n) (0x00c + 0x100 * (n))
23 #define CSIPHY_3PH_LNn_CFG4_T_HS_CLK_MISS 0xa4
24 #define CSIPHY_3PH_LNn_CFG5(n) (0x010 + 0x100 * (n))
25 #define CSIPHY_3PH_LNn_CFG5_T_HS_DTERM 0x02
26 #define CSIPHY_3PH_LNn_CFG5_HS_REC_EQ_FQ_INT 0x50
27 #define CSIPHY_3PH_LNn_TEST_IMP(n) (0x01c + 0x100 * (n))
28 #define CSIPHY_3PH_LNn_TEST_IMP_HS_TERM_IMP 0xa
29 #define CSIPHY_3PH_LNn_MISC1(n) (0x028 + 0x100 * (n))
30 #define CSIPHY_3PH_LNn_MISC1_IS_CLKLANE BIT(2)
31 #define CSIPHY_3PH_LNn_CFG6(n) (0x02c + 0x100 * (n))
32 #define CSIPHY_3PH_LNn_CFG6_SWI_FORCE_INIT_EXIT BIT(0)
33 #define CSIPHY_3PH_LNn_CFG7(n) (0x030 + 0x100 * (n))
34 #define CSIPHY_3PH_LNn_CFG7_SWI_T_INIT 0x2
35 #define CSIPHY_3PH_LNn_CFG8(n) (0x034 + 0x100 * (n))
36 #define CSIPHY_3PH_LNn_CFG8_SWI_SKIP_WAKEUP BIT(0)
37 #define CSIPHY_3PH_LNn_CFG8_SKEW_FILTER_ENABLE BIT(1)
38 #define CSIPHY_3PH_LNn_CFG9(n) (0x038 + 0x100 * (n))
39 #define CSIPHY_3PH_LNn_CFG9_SWI_T_WAKEUP 0x1
40 #define CSIPHY_3PH_LNn_CSI_LANE_CTRL15(n) (0x03c + 0x100 * (n))
41 #define CSIPHY_3PH_LNn_CSI_LANE_CTRL15_SWI_SOT_SYMBOL 0xb8
43 #define CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(n) (0x800 + 0x4 * (n))
44 #define CSIPHY_3PH_CMN_CSI_COMMON_CTRL6_COMMON_PWRDN_B BIT(0)
45 #define CSIPHY_3PH_CMN_CSI_COMMON_CTRL6_SHOW_REV_ID BIT(1)
46 #define CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(n) (0x8b0 + 0x4 * (n))
48 static void csiphy_hw_version_read(struct csiphy_device *csiphy,
53 writel(CSIPHY_3PH_CMN_CSI_COMMON_CTRL6_SHOW_REV_ID,
54 csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(6));
56 hw_version = readl_relaxed(csiphy->base +
57 CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(12));
58 hw_version |= readl_relaxed(csiphy->base +
59 CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(13)) << 8;
60 hw_version |= readl_relaxed(csiphy->base +
61 CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(14)) << 16;
62 hw_version |= readl_relaxed(csiphy->base +
63 CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(15)) << 24;
65 dev_err(dev, "CSIPHY 3PH HW Version = 0x%08x\n", hw_version);
69 * csiphy_reset - Perform software reset on CSIPHY module
70 * @csiphy: CSIPHY device
72 static void csiphy_reset(struct csiphy_device *csiphy)
74 writel_relaxed(0x1, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(0));
75 usleep_range(5000, 8000);
76 writel_relaxed(0x0, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(0));
79 static irqreturn_t csiphy_isr(int irq, void *dev)
81 struct csiphy_device *csiphy = dev;
84 for (i = 0; i < 11; i++) {
86 u8 val = readl_relaxed(csiphy->base +
87 CSIPHY_3PH_CMN_CSI_COMMON_STATUSn(i));
89 writel_relaxed(val, csiphy->base +
90 CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(c));
93 writel_relaxed(0x1, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(10));
94 writel_relaxed(0x0, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(10));
96 for (i = 22; i < 33; i++)
97 writel_relaxed(0x0, csiphy->base +
98 CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(i));
104 * csiphy_settle_cnt_calc - Calculate settle count value
106 * Helper function to calculate settle count value. This is
107 * based on the CSI2 T_hs_settle parameter which in turn
108 * is calculated based on the CSI2 transmitter pixel clock
111 * Return settle count value or 0 if the CSI2 pixel clock
112 * frequency is not available
114 static u8 csiphy_settle_cnt_calc(u32 pixel_clock, u8 bpp, u8 num_lanes,
117 u32 mipi_clock; /* Hz */
119 u32 timer_period; /* ps */
120 u32 t_hs_prepare_max; /* ps */
121 u32 t_hs_settle; /* ps */
124 mipi_clock = pixel_clock * bpp / (2 * num_lanes);
125 ui = div_u64(1000000000000LL, mipi_clock);
127 t_hs_prepare_max = 85000 + 6 * ui;
128 t_hs_settle = t_hs_prepare_max;
130 timer_period = div_u64(1000000000000LL, timer_clk_rate);
131 settle_cnt = t_hs_settle / timer_period - 6;
136 static void csiphy_lanes_enable(struct csiphy_device *csiphy,
137 struct csiphy_config *cfg,
138 u32 pixel_clock, u8 bpp, u8 lane_mask)
140 struct csiphy_lanes_cfg *c = &cfg->csi2->lane_cfg;
145 settle_cnt = csiphy_settle_cnt_calc(pixel_clock, bpp, c->num_data,
146 csiphy->timer_clk_rate);
148 val = BIT(c->clk.pos);
149 for (i = 0; i < c->num_data; i++)
150 val |= BIT(c->data[i].pos * 2);
152 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(5));
154 val = CSIPHY_3PH_CMN_CSI_COMMON_CTRL6_COMMON_PWRDN_B;
155 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(6));
157 for (i = 0; i <= c->num_data; i++) {
158 if (i == c->num_data)
161 l = c->data[i].pos * 2;
163 val = CSIPHY_3PH_LNn_CFG1_SWI_REC_DLY_PRG;
165 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_CFG1(l));
167 val = CSIPHY_3PH_LNn_CFG2_LP_REC_EN_INT;
168 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_CFG2(l));
171 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_CFG3(l));
173 val = CSIPHY_3PH_LNn_CFG5_T_HS_DTERM |
174 CSIPHY_3PH_LNn_CFG5_HS_REC_EQ_FQ_INT;
175 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_CFG5(l));
177 val = CSIPHY_3PH_LNn_CFG6_SWI_FORCE_INIT_EXIT;
178 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_CFG6(l));
180 val = CSIPHY_3PH_LNn_CFG7_SWI_T_INIT;
181 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_CFG7(l));
183 val = CSIPHY_3PH_LNn_CFG8_SWI_SKIP_WAKEUP |
184 CSIPHY_3PH_LNn_CFG8_SKEW_FILTER_ENABLE;
185 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_CFG8(l));
187 val = CSIPHY_3PH_LNn_CFG9_SWI_T_WAKEUP;
188 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_CFG9(l));
190 val = CSIPHY_3PH_LNn_TEST_IMP_HS_TERM_IMP;
191 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_TEST_IMP(l));
193 val = CSIPHY_3PH_LNn_CSI_LANE_CTRL15_SWI_SOT_SYMBOL;
194 writel_relaxed(val, csiphy->base +
195 CSIPHY_3PH_LNn_CSI_LANE_CTRL15(l));
198 val = CSIPHY_3PH_LNn_CFG1_SWI_REC_DLY_PRG;
199 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_CFG1(l));
201 val = CSIPHY_3PH_LNn_CFG4_T_HS_CLK_MISS;
202 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_CFG4(l));
204 val = CSIPHY_3PH_LNn_MISC1_IS_CLKLANE;
205 writel_relaxed(val, csiphy->base + CSIPHY_3PH_LNn_MISC1(l));
208 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(11));
211 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(12));
214 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(13));
217 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(14));
220 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(15));
223 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(16));
226 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(17));
229 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(18));
232 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(19));
235 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(20));
238 writel_relaxed(val, csiphy->base + CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(21));
241 static void csiphy_lanes_disable(struct csiphy_device *csiphy,
242 struct csiphy_config *cfg)
244 writel_relaxed(0, csiphy->base +
245 CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(5));
247 writel_relaxed(0, csiphy->base +
248 CSIPHY_3PH_CMN_CSI_COMMON_CTRLn(6));
251 const struct csiphy_hw_ops csiphy_ops_3ph_1_0 = {
252 .hw_version_read = csiphy_hw_version_read,
253 .reset = csiphy_reset,
254 .lanes_enable = csiphy_lanes_enable,
255 .lanes_disable = csiphy_lanes_disable,