Chris Morgan | 486c463 | 2023-03-24 13:53:06 -0500 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2018 Rockchip Electronics Co. Ltd. |
| 4 | * |
| 5 | * Author: Wyon Bi <bivvy.bi@rock-chips.com> |
| 6 | */ |
| 7 | |
| 8 | #include <dm.h> |
| 9 | #include <dm/device_compat.h> |
| 10 | #include <dm/devres.h> |
| 11 | #include <div64.h> |
| 12 | #include <generic-phy.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/iopoll.h> |
| 15 | #include <linux/clk-provider.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/math64.h> |
| 18 | #include <phy-mipi-dphy.h> |
| 19 | #include <reset.h> |
| 20 | |
| 21 | #define UPDATE(x, h, l) (((x) << (l)) & GENMASK((h), (l))) |
| 22 | |
| 23 | /* |
| 24 | * The offset address[7:0] is distributed two parts, one from the bit7 to bit5 |
| 25 | * is the first address, the other from the bit4 to bit0 is the second address. |
| 26 | * when you configure the registers, you must set both of them. The Clock Lane |
| 27 | * and Data Lane use the same registers with the same second address, but the |
| 28 | * first address is different. |
| 29 | */ |
| 30 | #define FIRST_ADDRESS(x) (((x) & 0x7) << 5) |
| 31 | #define SECOND_ADDRESS(x) (((x) & 0x1f) << 0) |
| 32 | #define PHY_REG(first, second) (FIRST_ADDRESS(first) | \ |
| 33 | SECOND_ADDRESS(second)) |
| 34 | |
| 35 | /* Analog Register Part: reg00 */ |
| 36 | #define BANDGAP_POWER_MASK BIT(7) |
| 37 | #define BANDGAP_POWER_DOWN BIT(7) |
| 38 | #define BANDGAP_POWER_ON 0 |
| 39 | #define LANE_EN_MASK GENMASK(6, 2) |
| 40 | #define LANE_EN_CK BIT(6) |
| 41 | #define LANE_EN_3 BIT(5) |
| 42 | #define LANE_EN_2 BIT(4) |
| 43 | #define LANE_EN_1 BIT(3) |
| 44 | #define LANE_EN_0 BIT(2) |
| 45 | #define POWER_WORK_MASK GENMASK(1, 0) |
| 46 | #define POWER_WORK_ENABLE UPDATE(1, 1, 0) |
| 47 | #define POWER_WORK_DISABLE UPDATE(2, 1, 0) |
| 48 | /* Analog Register Part: reg01 */ |
| 49 | #define REG_SYNCRST_MASK BIT(2) |
| 50 | #define REG_SYNCRST_RESET BIT(2) |
| 51 | #define REG_SYNCRST_NORMAL 0 |
| 52 | #define REG_LDOPD_MASK BIT(1) |
| 53 | #define REG_LDOPD_POWER_DOWN BIT(1) |
| 54 | #define REG_LDOPD_POWER_ON 0 |
| 55 | #define REG_PLLPD_MASK BIT(0) |
| 56 | #define REG_PLLPD_POWER_DOWN BIT(0) |
| 57 | #define REG_PLLPD_POWER_ON 0 |
| 58 | /* Analog Register Part: reg03 */ |
| 59 | #define REG_FBDIV_HI_MASK BIT(5) |
| 60 | #define REG_FBDIV_HI(x) UPDATE((x >> 8), 5, 5) |
| 61 | #define REG_PREDIV_MASK GENMASK(4, 0) |
| 62 | #define REG_PREDIV(x) UPDATE(x, 4, 0) |
| 63 | /* Analog Register Part: reg04 */ |
| 64 | #define REG_FBDIV_LO_MASK GENMASK(7, 0) |
| 65 | #define REG_FBDIV_LO(x) UPDATE(x, 7, 0) |
| 66 | /* Analog Register Part: reg05 */ |
| 67 | #define SAMPLE_CLOCK_PHASE_MASK GENMASK(6, 4) |
| 68 | #define SAMPLE_CLOCK_PHASE(x) UPDATE(x, 6, 4) |
| 69 | #define CLOCK_LANE_SKEW_PHASE_MASK GENMASK(2, 0) |
| 70 | #define CLOCK_LANE_SKEW_PHASE(x) UPDATE(x, 2, 0) |
| 71 | /* Analog Register Part: reg06 */ |
| 72 | #define DATA_LANE_3_SKEW_PHASE_MASK GENMASK(6, 4) |
| 73 | #define DATA_LANE_3_SKEW_PHASE(x) UPDATE(x, 6, 4) |
| 74 | #define DATA_LANE_2_SKEW_PHASE_MASK GENMASK(2, 0) |
| 75 | #define DATA_LANE_2_SKEW_PHASE(x) UPDATE(x, 2, 0) |
| 76 | /* Analog Register Part: reg07 */ |
| 77 | #define DATA_LANE_1_SKEW_PHASE_MASK GENMASK(6, 4) |
| 78 | #define DATA_LANE_1_SKEW_PHASE(x) UPDATE(x, 6, 4) |
| 79 | #define DATA_LANE_0_SKEW_PHASE_MASK GENMASK(2, 0) |
| 80 | #define DATA_LANE_0_SKEW_PHASE(x) UPDATE(x, 2, 0) |
| 81 | /* Analog Register Part: reg08 */ |
| 82 | #define PLL_POST_DIV_ENABLE_MASK BIT(5) |
| 83 | #define PLL_POST_DIV_ENABLE BIT(5) |
| 84 | #define SAMPLE_CLOCK_DIRECTION_MASK BIT(4) |
| 85 | #define SAMPLE_CLOCK_DIRECTION_REVERSE BIT(4) |
| 86 | #define SAMPLE_CLOCK_DIRECTION_FORWARD 0 |
| 87 | #define LOWFRE_EN_MASK BIT(5) |
| 88 | #define PLL_OUTPUT_FREQUENCY_DIV_BY_1 0 |
| 89 | #define PLL_OUTPUT_FREQUENCY_DIV_BY_2 1 |
| 90 | /* Analog Register Part: reg0b */ |
| 91 | #define CLOCK_LANE_VOD_RANGE_SET_MASK GENMASK(3, 0) |
| 92 | #define CLOCK_LANE_VOD_RANGE_SET(x) UPDATE(x, 3, 0) |
| 93 | #define VOD_MIN_RANGE 0x1 |
| 94 | #define VOD_MID_RANGE 0x3 |
| 95 | #define VOD_BIG_RANGE 0x7 |
| 96 | #define VOD_MAX_RANGE 0xf |
| 97 | /* Analog Register Part: reg1E */ |
| 98 | #define PLL_MODE_SEL_MASK GENMASK(6, 5) |
| 99 | #define PLL_MODE_SEL_LVDS_MODE 0 |
| 100 | #define PLL_MODE_SEL_MIPI_MODE BIT(5) |
| 101 | /* Digital Register Part: reg00 */ |
| 102 | #define REG_DIG_RSTN_MASK BIT(0) |
| 103 | #define REG_DIG_RSTN_NORMAL BIT(0) |
| 104 | #define REG_DIG_RSTN_RESET 0 |
| 105 | /* Digital Register Part: reg01 */ |
| 106 | #define INVERT_TXCLKESC_MASK BIT(1) |
| 107 | #define INVERT_TXCLKESC_ENABLE BIT(1) |
| 108 | #define INVERT_TXCLKESC_DISABLE 0 |
| 109 | #define INVERT_TXBYTECLKHS_MASK BIT(0) |
| 110 | #define INVERT_TXBYTECLKHS_ENABLE BIT(0) |
| 111 | #define INVERT_TXBYTECLKHS_DISABLE 0 |
| 112 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg05 */ |
| 113 | #define T_LPX_CNT_MASK GENMASK(5, 0) |
| 114 | #define T_LPX_CNT(x) UPDATE(x, 5, 0) |
| 115 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg06 */ |
| 116 | #define T_HS_ZERO_CNT_HI_MASK BIT(7) |
| 117 | #define T_HS_ZERO_CNT_HI(x) UPDATE(x, 7, 7) |
| 118 | #define T_HS_PREPARE_CNT_MASK GENMASK(6, 0) |
| 119 | #define T_HS_PREPARE_CNT(x) UPDATE(x, 6, 0) |
| 120 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg07 */ |
| 121 | #define T_HS_ZERO_CNT_LO_MASK GENMASK(5, 0) |
| 122 | #define T_HS_ZERO_CNT_LO(x) UPDATE(x, 5, 0) |
| 123 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg08 */ |
| 124 | #define T_HS_TRAIL_CNT_MASK GENMASK(6, 0) |
| 125 | #define T_HS_TRAIL_CNT(x) UPDATE(x, 6, 0) |
| 126 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg09 */ |
| 127 | #define T_HS_EXIT_CNT_LO_MASK GENMASK(4, 0) |
| 128 | #define T_HS_EXIT_CNT_LO(x) UPDATE(x, 4, 0) |
| 129 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg0a */ |
| 130 | #define T_CLK_POST_CNT_LO_MASK GENMASK(3, 0) |
| 131 | #define T_CLK_POST_CNT_LO(x) UPDATE(x, 3, 0) |
| 132 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg0c */ |
| 133 | #define LPDT_TX_PPI_SYNC_MASK BIT(2) |
| 134 | #define LPDT_TX_PPI_SYNC_ENABLE BIT(2) |
| 135 | #define LPDT_TX_PPI_SYNC_DISABLE 0 |
| 136 | #define T_WAKEUP_CNT_HI_MASK GENMASK(1, 0) |
| 137 | #define T_WAKEUP_CNT_HI(x) UPDATE(x, 1, 0) |
| 138 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg0d */ |
| 139 | #define T_WAKEUP_CNT_LO_MASK GENMASK(7, 0) |
| 140 | #define T_WAKEUP_CNT_LO(x) UPDATE(x, 7, 0) |
| 141 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg0e */ |
| 142 | #define T_CLK_PRE_CNT_MASK GENMASK(3, 0) |
| 143 | #define T_CLK_PRE_CNT(x) UPDATE(x, 3, 0) |
| 144 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg10 */ |
| 145 | #define T_CLK_POST_CNT_HI_MASK GENMASK(7, 6) |
| 146 | #define T_CLK_POST_CNT_HI(x) UPDATE(x, 7, 6) |
| 147 | #define T_TA_GO_CNT_MASK GENMASK(5, 0) |
| 148 | #define T_TA_GO_CNT(x) UPDATE(x, 5, 0) |
| 149 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg11 */ |
| 150 | #define T_HS_EXIT_CNT_HI_MASK BIT(6) |
| 151 | #define T_HS_EXIT_CNT_HI(x) UPDATE(x, 6, 6) |
| 152 | #define T_TA_SURE_CNT_MASK GENMASK(5, 0) |
| 153 | #define T_TA_SURE_CNT(x) UPDATE(x, 5, 0) |
| 154 | /* Clock/Data0/Data1/Data2/Data3 Lane Register Part: reg12 */ |
| 155 | #define T_TA_WAIT_CNT_MASK GENMASK(5, 0) |
| 156 | #define T_TA_WAIT_CNT(x) UPDATE(x, 5, 0) |
| 157 | /* LVDS Register Part: reg00 */ |
| 158 | #define LVDS_DIGITAL_INTERNAL_RESET_MASK BIT(2) |
| 159 | #define LVDS_DIGITAL_INTERNAL_RESET_DISABLE BIT(2) |
| 160 | #define LVDS_DIGITAL_INTERNAL_RESET_ENABLE 0 |
| 161 | /* LVDS Register Part: reg01 */ |
| 162 | #define LVDS_DIGITAL_INTERNAL_ENABLE_MASK BIT(7) |
| 163 | #define LVDS_DIGITAL_INTERNAL_ENABLE BIT(7) |
| 164 | #define LVDS_DIGITAL_INTERNAL_DISABLE 0 |
| 165 | /* LVDS Register Part: reg03 */ |
| 166 | #define MODE_ENABLE_MASK GENMASK(2, 0) |
| 167 | #define TTL_MODE_ENABLE BIT(2) |
| 168 | #define LVDS_MODE_ENABLE BIT(1) |
| 169 | #define MIPI_MODE_ENABLE BIT(0) |
| 170 | /* LVDS Register Part: reg0b */ |
| 171 | #define LVDS_LANE_EN_MASK GENMASK(7, 3) |
| 172 | #define LVDS_DATA_LANE0_EN BIT(7) |
| 173 | #define LVDS_DATA_LANE1_EN BIT(6) |
| 174 | #define LVDS_DATA_LANE2_EN BIT(5) |
| 175 | #define LVDS_DATA_LANE3_EN BIT(4) |
| 176 | #define LVDS_CLK_LANE_EN BIT(3) |
| 177 | #define LVDS_PLL_POWER_MASK BIT(2) |
| 178 | #define LVDS_PLL_POWER_OFF BIT(2) |
| 179 | #define LVDS_PLL_POWER_ON 0 |
| 180 | #define LVDS_BANDGAP_POWER_MASK BIT(0) |
| 181 | #define LVDS_BANDGAP_POWER_DOWN BIT(0) |
| 182 | #define LVDS_BANDGAP_POWER_ON 0 |
| 183 | |
| 184 | #define DSI_PHY_RSTZ 0xa0 |
| 185 | #define PHY_ENABLECLK BIT(2) |
| 186 | #define DSI_PHY_STATUS 0xb0 |
| 187 | #define PHY_LOCK BIT(0) |
| 188 | |
| 189 | #define PSEC_PER_SEC 1000000000000LL |
| 190 | |
| 191 | #define msleep(a) udelay(a * 1000) |
| 192 | |
| 193 | enum phy_max_rate { |
| 194 | MAX_1GHZ, |
| 195 | MAX_2_5GHZ, |
| 196 | }; |
| 197 | |
| 198 | struct clk_hw { |
| 199 | struct clk_core *core; |
| 200 | struct clk *clk; |
| 201 | const struct clk_init_data *init; |
| 202 | }; |
| 203 | |
| 204 | struct inno_video_phy_plat_data { |
| 205 | const struct inno_mipi_dphy_timing *inno_mipi_dphy_timing_table; |
| 206 | const unsigned int num_timings; |
| 207 | enum phy_max_rate max_rate; |
| 208 | }; |
| 209 | |
| 210 | struct inno_dsidphy { |
| 211 | struct udevice *dev; |
| 212 | struct clk *ref_clk; |
| 213 | struct clk *pclk_phy; |
| 214 | struct clk *pclk_host; |
| 215 | const struct inno_video_phy_plat_data *pdata; |
| 216 | void __iomem *phy_base; |
| 217 | void __iomem *host_base; |
| 218 | struct reset_ctl *rst; |
| 219 | struct phy_configure_opts_mipi_dphy dphy_cfg; |
| 220 | |
| 221 | struct clk *pll_clk; |
| 222 | struct { |
| 223 | struct clk_hw hw; |
| 224 | u8 prediv; |
| 225 | u16 fbdiv; |
| 226 | unsigned long rate; |
| 227 | } pll; |
| 228 | }; |
| 229 | |
| 230 | enum { |
| 231 | REGISTER_PART_ANALOG, |
| 232 | REGISTER_PART_DIGITAL, |
| 233 | REGISTER_PART_CLOCK_LANE, |
| 234 | REGISTER_PART_DATA0_LANE, |
| 235 | REGISTER_PART_DATA1_LANE, |
| 236 | REGISTER_PART_DATA2_LANE, |
| 237 | REGISTER_PART_DATA3_LANE, |
| 238 | REGISTER_PART_LVDS, |
| 239 | }; |
| 240 | |
| 241 | struct inno_mipi_dphy_timing { |
| 242 | unsigned long rate; |
| 243 | u8 lpx; |
| 244 | u8 hs_prepare; |
| 245 | u8 clk_lane_hs_zero; |
| 246 | u8 data_lane_hs_zero; |
| 247 | u8 hs_trail; |
| 248 | }; |
| 249 | |
| 250 | static const |
| 251 | struct inno_mipi_dphy_timing inno_mipi_dphy_timing_table_max_1ghz[] = { |
| 252 | { 110000000, 0x0, 0x20, 0x16, 0x02, 0x22}, |
| 253 | { 150000000, 0x0, 0x06, 0x16, 0x03, 0x45}, |
| 254 | { 200000000, 0x0, 0x18, 0x17, 0x04, 0x0b}, |
| 255 | { 250000000, 0x0, 0x05, 0x17, 0x05, 0x16}, |
| 256 | { 300000000, 0x0, 0x51, 0x18, 0x06, 0x2c}, |
| 257 | { 400000000, 0x0, 0x64, 0x19, 0x07, 0x33}, |
| 258 | { 500000000, 0x0, 0x20, 0x1b, 0x07, 0x4e}, |
| 259 | { 600000000, 0x0, 0x6a, 0x1d, 0x08, 0x3a}, |
| 260 | { 700000000, 0x0, 0x3e, 0x1e, 0x08, 0x6a}, |
| 261 | { 800000000, 0x0, 0x21, 0x1f, 0x09, 0x29}, |
| 262 | {1000000000, 0x0, 0x09, 0x20, 0x09, 0x27}, |
| 263 | }; |
| 264 | |
| 265 | static const |
| 266 | struct inno_mipi_dphy_timing inno_mipi_dphy_timing_table_max_2_5ghz[] = { |
| 267 | { 110000000, 0x02, 0x7f, 0x16, 0x02, 0x02}, |
| 268 | { 150000000, 0x02, 0x7f, 0x16, 0x03, 0x02}, |
| 269 | { 200000000, 0x02, 0x7f, 0x17, 0x04, 0x02}, |
| 270 | { 250000000, 0x02, 0x7f, 0x17, 0x05, 0x04}, |
| 271 | { 300000000, 0x02, 0x7f, 0x18, 0x06, 0x04}, |
| 272 | { 400000000, 0x03, 0x7e, 0x19, 0x07, 0x04}, |
| 273 | { 500000000, 0x03, 0x7c, 0x1b, 0x07, 0x08}, |
| 274 | { 600000000, 0x03, 0x70, 0x1d, 0x08, 0x10}, |
| 275 | { 700000000, 0x05, 0x40, 0x1e, 0x08, 0x30}, |
| 276 | { 800000000, 0x05, 0x02, 0x1f, 0x09, 0x30}, |
| 277 | {1000000000, 0x05, 0x08, 0x20, 0x09, 0x30}, |
| 278 | {1200000000, 0x06, 0x03, 0x32, 0x14, 0x0f}, |
| 279 | {1400000000, 0x09, 0x03, 0x32, 0x14, 0x0f}, |
| 280 | {1600000000, 0x0d, 0x42, 0x36, 0x0e, 0x0f}, |
| 281 | {1800000000, 0x0e, 0x47, 0x7a, 0x0e, 0x0f}, |
| 282 | {2000000000, 0x11, 0x64, 0x7a, 0x0e, 0x0b}, |
| 283 | {2200000000, 0x13, 0x64, 0x7e, 0x15, 0x0b}, |
| 284 | {2400000000, 0x13, 0x33, 0x7f, 0x15, 0x6a}, |
| 285 | {2500000000, 0x15, 0x54, 0x7f, 0x15, 0x6a}, |
| 286 | }; |
| 287 | |
| 288 | static void phy_update_bits(struct inno_dsidphy *inno, |
| 289 | u8 first, u8 second, u8 mask, u8 val) |
| 290 | { |
| 291 | u32 reg = PHY_REG(first, second) << 2; |
| 292 | unsigned int tmp, orig; |
| 293 | |
| 294 | orig = readl(inno->phy_base + reg); |
| 295 | tmp = orig & ~mask; |
| 296 | tmp |= val & mask; |
| 297 | writel(tmp, inno->phy_base + reg); |
| 298 | } |
| 299 | |
| 300 | static unsigned long inno_dsidphy_pll_calc_rate(struct inno_dsidphy *inno, |
| 301 | unsigned long rate) |
| 302 | { |
| 303 | unsigned long prate; |
| 304 | unsigned long best_freq = 0; |
| 305 | unsigned long fref, fout; |
| 306 | u8 min_prediv, max_prediv; |
| 307 | u8 _prediv, best_prediv = 1; |
| 308 | u16 _fbdiv, best_fbdiv = 1; |
| 309 | u32 min_delta = UINT_MAX; |
| 310 | |
| 311 | /* |
| 312 | * Upstream Linux tries to read the ref_clk, while the BSP |
| 313 | * U-Boot hard-codes this as 24MHz. Try the first, and if that |
| 314 | * fails do the second. |
| 315 | */ |
| 316 | prate = clk_get_rate(inno->ref_clk); |
| 317 | if (IS_ERR_VALUE(prate)) |
| 318 | prate = 24000000; |
| 319 | |
| 320 | /* |
| 321 | * The PLL output frequency can be calculated using a simple formula: |
| 322 | * PLL_Output_Frequency = (FREF / PREDIV * FBDIV) / 2 |
| 323 | * PLL_Output_Frequency: it is equal to DDR-Clock-Frequency * 2 |
| 324 | */ |
| 325 | fref = prate / 2; |
| 326 | if (rate > 1000000000UL) |
| 327 | fout = 1000000000UL; |
| 328 | else |
| 329 | fout = rate; |
| 330 | |
| 331 | /* 5Mhz < Fref / prediv < 40MHz */ |
| 332 | min_prediv = DIV_ROUND_UP(fref, 40000000); |
| 333 | max_prediv = fref / 5000000; |
| 334 | |
| 335 | for (_prediv = min_prediv; _prediv <= max_prediv; _prediv++) { |
| 336 | u64 tmp; |
| 337 | u32 delta; |
| 338 | |
| 339 | tmp = (u64)fout * _prediv; |
| 340 | do_div(tmp, fref); |
| 341 | _fbdiv = tmp; |
| 342 | |
| 343 | /* |
| 344 | * The possible settings of feedback divider are |
| 345 | * 12, 13, 14, 16, ~ 511 |
| 346 | */ |
| 347 | if (_fbdiv == 15) |
| 348 | continue; |
| 349 | |
| 350 | if (_fbdiv < 12 || _fbdiv > 511) |
| 351 | continue; |
| 352 | |
| 353 | tmp = (u64)_fbdiv * fref; |
| 354 | do_div(tmp, _prediv); |
| 355 | |
| 356 | delta = abs(fout - tmp); |
| 357 | if (!delta) { |
| 358 | best_prediv = _prediv; |
| 359 | best_fbdiv = _fbdiv; |
| 360 | best_freq = tmp; |
| 361 | break; |
| 362 | } else if (delta < min_delta) { |
| 363 | best_prediv = _prediv; |
| 364 | best_fbdiv = _fbdiv; |
| 365 | best_freq = tmp; |
| 366 | min_delta = delta; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | if (best_freq) { |
| 371 | inno->pll.prediv = best_prediv; |
| 372 | inno->pll.fbdiv = best_fbdiv; |
| 373 | inno->pll.rate = best_freq; |
| 374 | } |
| 375 | |
| 376 | return best_freq; |
| 377 | } |
| 378 | |
| 379 | static void inno_dsidphy_mipi_mode_enable(struct inno_dsidphy *inno) |
| 380 | { |
| 381 | struct phy_configure_opts_mipi_dphy *cfg = &inno->dphy_cfg; |
| 382 | const struct inno_mipi_dphy_timing *timings; |
| 383 | u32 t_txbyteclkhs, t_txclkesc; |
| 384 | u32 txbyteclkhs, txclkesc, esc_clk_div; |
| 385 | u32 hs_exit, clk_post, clk_pre, wakeup, lpx, ta_go, ta_sure, ta_wait; |
| 386 | u32 hs_prepare, hs_trail, hs_zero, clk_lane_hs_zero, data_lane_hs_zero; |
| 387 | unsigned int i; |
| 388 | |
| 389 | timings = inno->pdata->inno_mipi_dphy_timing_table; |
| 390 | |
| 391 | inno_dsidphy_pll_calc_rate(inno, cfg->hs_clk_rate); |
| 392 | |
| 393 | /* Select MIPI mode */ |
| 394 | phy_update_bits(inno, REGISTER_PART_LVDS, 0x03, |
| 395 | MODE_ENABLE_MASK, MIPI_MODE_ENABLE); |
| 396 | /* Configure PLL */ |
| 397 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x03, |
| 398 | REG_PREDIV_MASK, REG_PREDIV(inno->pll.prediv)); |
| 399 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x03, |
| 400 | REG_FBDIV_HI_MASK, REG_FBDIV_HI(inno->pll.fbdiv)); |
| 401 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x04, |
| 402 | REG_FBDIV_LO_MASK, REG_FBDIV_LO(inno->pll.fbdiv)); |
| 403 | if (inno->pdata->max_rate == MAX_2_5GHZ) { |
| 404 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x08, |
| 405 | PLL_POST_DIV_ENABLE_MASK, PLL_POST_DIV_ENABLE); |
| 406 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x0b, |
| 407 | CLOCK_LANE_VOD_RANGE_SET_MASK, |
| 408 | CLOCK_LANE_VOD_RANGE_SET(VOD_MAX_RANGE)); |
| 409 | } |
| 410 | /* Enable PLL and LDO */ |
| 411 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x01, |
| 412 | REG_LDOPD_MASK | REG_PLLPD_MASK, |
| 413 | REG_LDOPD_POWER_ON | REG_PLLPD_POWER_ON); |
| 414 | /* Reset analog */ |
| 415 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x01, |
| 416 | REG_SYNCRST_MASK, REG_SYNCRST_RESET); |
| 417 | udelay(1); |
| 418 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x01, |
| 419 | REG_SYNCRST_MASK, REG_SYNCRST_NORMAL); |
| 420 | /* Reset digital */ |
| 421 | phy_update_bits(inno, REGISTER_PART_DIGITAL, 0x00, |
| 422 | REG_DIG_RSTN_MASK, REG_DIG_RSTN_RESET); |
| 423 | udelay(1); |
| 424 | phy_update_bits(inno, REGISTER_PART_DIGITAL, 0x00, |
| 425 | REG_DIG_RSTN_MASK, REG_DIG_RSTN_NORMAL); |
| 426 | |
| 427 | txbyteclkhs = inno->pll.rate / 8; |
| 428 | t_txbyteclkhs = div_u64(PSEC_PER_SEC, txbyteclkhs); |
| 429 | |
| 430 | esc_clk_div = DIV_ROUND_UP(txbyteclkhs, 20000000); |
| 431 | txclkesc = txbyteclkhs / esc_clk_div; |
| 432 | t_txclkesc = div_u64(PSEC_PER_SEC, txclkesc); |
| 433 | |
| 434 | /* |
| 435 | * The value of counter for HS Ths-exit |
| 436 | * Ths-exit = Tpin_txbyteclkhs * value |
| 437 | */ |
| 438 | hs_exit = DIV_ROUND_UP(cfg->hs_exit, t_txbyteclkhs); |
| 439 | /* |
| 440 | * The value of counter for HS Tclk-post |
| 441 | * Tclk-post = Tpin_txbyteclkhs * value |
| 442 | */ |
| 443 | clk_post = DIV_ROUND_UP(cfg->clk_post, t_txbyteclkhs); |
| 444 | /* |
| 445 | * The value of counter for HS Tclk-pre |
| 446 | * Tclk-pre = Tpin_txbyteclkhs * value |
| 447 | */ |
| 448 | clk_pre = DIV_ROUND_UP(cfg->clk_pre, BITS_PER_BYTE); |
| 449 | |
| 450 | /* |
| 451 | * The value of counter for HS Tta-go |
| 452 | * Tta-go for turnaround |
| 453 | * Tta-go = Ttxclkesc * value |
| 454 | */ |
| 455 | ta_go = DIV_ROUND_UP(cfg->ta_go, t_txclkesc); |
| 456 | /* |
| 457 | * The value of counter for HS Tta-sure |
| 458 | * Tta-sure for turnaround |
| 459 | * Tta-sure = Ttxclkesc * value |
| 460 | */ |
| 461 | ta_sure = DIV_ROUND_UP(cfg->ta_sure, t_txclkesc); |
| 462 | /* |
| 463 | * The value of counter for HS Tta-wait |
| 464 | * Tta-wait for turnaround |
| 465 | * Tta-wait = Ttxclkesc * value |
| 466 | */ |
| 467 | ta_wait = DIV_ROUND_UP(cfg->ta_get, t_txclkesc); |
| 468 | |
| 469 | for (i = 0; i < inno->pdata->num_timings; i++) |
| 470 | if (inno->pll.rate <= timings[i].rate) |
| 471 | break; |
| 472 | |
| 473 | if (i == inno->pdata->num_timings) |
| 474 | --i; |
| 475 | |
| 476 | /* |
| 477 | * The value of counter for HS Tlpx Time |
| 478 | * Tlpx = Tpin_txbyteclkhs * (2 + value) |
| 479 | */ |
| 480 | if (inno->pdata->max_rate == MAX_1GHZ) { |
| 481 | lpx = DIV_ROUND_UP(cfg->lpx, t_txbyteclkhs); |
| 482 | if (lpx >= 2) |
| 483 | lpx -= 2; |
| 484 | } else { |
| 485 | lpx = timings[i].lpx; |
| 486 | } |
| 487 | |
| 488 | hs_prepare = timings[i].hs_prepare; |
| 489 | hs_trail = timings[i].hs_trail; |
| 490 | clk_lane_hs_zero = timings[i].clk_lane_hs_zero; |
| 491 | data_lane_hs_zero = timings[i].data_lane_hs_zero; |
| 492 | wakeup = 0x3ff; |
| 493 | |
| 494 | for (i = REGISTER_PART_CLOCK_LANE; i <= REGISTER_PART_DATA3_LANE; i++) { |
| 495 | if (i == REGISTER_PART_CLOCK_LANE) |
| 496 | hs_zero = clk_lane_hs_zero; |
| 497 | else |
| 498 | hs_zero = data_lane_hs_zero; |
| 499 | |
| 500 | phy_update_bits(inno, i, 0x05, T_LPX_CNT_MASK, |
| 501 | T_LPX_CNT(lpx)); |
| 502 | phy_update_bits(inno, i, 0x06, T_HS_PREPARE_CNT_MASK, |
| 503 | T_HS_PREPARE_CNT(hs_prepare)); |
| 504 | if (inno->pdata->max_rate == MAX_2_5GHZ) |
| 505 | phy_update_bits(inno, i, 0x06, T_HS_ZERO_CNT_HI_MASK, |
| 506 | T_HS_ZERO_CNT_HI(hs_zero >> 6)); |
| 507 | phy_update_bits(inno, i, 0x07, T_HS_ZERO_CNT_LO_MASK, |
| 508 | T_HS_ZERO_CNT_LO(hs_zero)); |
| 509 | phy_update_bits(inno, i, 0x08, T_HS_TRAIL_CNT_MASK, |
| 510 | T_HS_TRAIL_CNT(hs_trail)); |
| 511 | if (inno->pdata->max_rate == MAX_2_5GHZ) |
| 512 | phy_update_bits(inno, i, 0x11, T_HS_EXIT_CNT_HI_MASK, |
| 513 | T_HS_EXIT_CNT_HI(hs_exit >> 5)); |
| 514 | phy_update_bits(inno, i, 0x09, T_HS_EXIT_CNT_LO_MASK, |
| 515 | T_HS_EXIT_CNT_LO(hs_exit)); |
| 516 | if (inno->pdata->max_rate == MAX_2_5GHZ) |
| 517 | phy_update_bits(inno, i, 0x10, T_CLK_POST_CNT_HI_MASK, |
| 518 | T_CLK_POST_CNT_HI(clk_post >> 4)); |
| 519 | phy_update_bits(inno, i, 0x0a, T_CLK_POST_CNT_LO_MASK, |
| 520 | T_CLK_POST_CNT_LO(clk_post)); |
| 521 | phy_update_bits(inno, i, 0x0e, T_CLK_PRE_CNT_MASK, |
| 522 | T_CLK_PRE_CNT(clk_pre)); |
| 523 | phy_update_bits(inno, i, 0x0c, T_WAKEUP_CNT_HI_MASK, |
| 524 | T_WAKEUP_CNT_HI(wakeup >> 8)); |
| 525 | phy_update_bits(inno, i, 0x0d, T_WAKEUP_CNT_LO_MASK, |
| 526 | T_WAKEUP_CNT_LO(wakeup)); |
| 527 | phy_update_bits(inno, i, 0x10, T_TA_GO_CNT_MASK, |
| 528 | T_TA_GO_CNT(ta_go)); |
| 529 | phy_update_bits(inno, i, 0x11, T_TA_SURE_CNT_MASK, |
| 530 | T_TA_SURE_CNT(ta_sure)); |
| 531 | phy_update_bits(inno, i, 0x12, T_TA_WAIT_CNT_MASK, |
| 532 | T_TA_WAIT_CNT(ta_wait)); |
| 533 | } |
| 534 | |
| 535 | /* Enable all lanes on analog part */ |
| 536 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00, |
| 537 | LANE_EN_MASK, LANE_EN_CK | LANE_EN_3 | LANE_EN_2 | |
| 538 | LANE_EN_1 | LANE_EN_0); |
| 539 | } |
| 540 | |
| 541 | static int inno_dsidphy_power_on(struct phy *phy) |
| 542 | { |
| 543 | struct inno_dsidphy *inno = dev_get_priv(phy->dev); |
| 544 | |
| 545 | clk_prepare_enable(inno->pclk_phy); |
| 546 | clk_prepare_enable(inno->ref_clk); |
| 547 | |
| 548 | /* Bandgap power on */ |
| 549 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00, |
| 550 | BANDGAP_POWER_MASK, BANDGAP_POWER_ON); |
| 551 | /* Enable power work */ |
| 552 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00, |
| 553 | POWER_WORK_MASK, POWER_WORK_ENABLE); |
| 554 | |
| 555 | inno_dsidphy_mipi_mode_enable(inno); |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | static int inno_dsidphy_power_off(struct phy *phy) |
| 561 | { |
| 562 | struct inno_dsidphy *inno = dev_get_priv(phy->dev); |
| 563 | |
| 564 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00, LANE_EN_MASK, 0); |
| 565 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x01, |
| 566 | REG_LDOPD_MASK | REG_PLLPD_MASK, |
| 567 | REG_LDOPD_POWER_DOWN | REG_PLLPD_POWER_DOWN); |
| 568 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00, |
| 569 | POWER_WORK_MASK, POWER_WORK_DISABLE); |
| 570 | phy_update_bits(inno, REGISTER_PART_ANALOG, 0x00, |
| 571 | BANDGAP_POWER_MASK, BANDGAP_POWER_DOWN); |
| 572 | |
| 573 | phy_update_bits(inno, REGISTER_PART_LVDS, 0x0b, LVDS_LANE_EN_MASK, 0); |
| 574 | phy_update_bits(inno, REGISTER_PART_LVDS, 0x01, |
| 575 | LVDS_DIGITAL_INTERNAL_ENABLE_MASK, |
| 576 | LVDS_DIGITAL_INTERNAL_DISABLE); |
| 577 | phy_update_bits(inno, REGISTER_PART_LVDS, 0x0b, |
| 578 | LVDS_PLL_POWER_MASK | LVDS_BANDGAP_POWER_MASK, |
| 579 | LVDS_PLL_POWER_OFF | LVDS_BANDGAP_POWER_DOWN); |
| 580 | |
| 581 | clk_disable_unprepare(inno->ref_clk); |
| 582 | clk_disable_unprepare(inno->pclk_phy); |
| 583 | |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int inno_dsidphy_configure(struct phy *phy, void *params) |
| 588 | { |
| 589 | struct inno_dsidphy *inno = dev_get_priv(phy->dev); |
| 590 | struct phy_configure_opts_mipi_dphy *config = params; |
| 591 | int ret; |
| 592 | |
| 593 | ret = phy_mipi_dphy_config_validate(config); |
| 594 | if (ret) |
| 595 | return ret; |
| 596 | |
| 597 | memcpy(&inno->dphy_cfg, config, sizeof(inno->dphy_cfg)); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
| 602 | static const struct phy_ops inno_dsidphy_ops = { |
| 603 | .configure = inno_dsidphy_configure, |
| 604 | .power_on = inno_dsidphy_power_on, |
| 605 | .power_off = inno_dsidphy_power_off, |
| 606 | }; |
| 607 | |
| 608 | static const struct inno_video_phy_plat_data max_1ghz_video_phy_plat_data = { |
| 609 | .inno_mipi_dphy_timing_table = inno_mipi_dphy_timing_table_max_1ghz, |
| 610 | .num_timings = ARRAY_SIZE(inno_mipi_dphy_timing_table_max_1ghz), |
| 611 | .max_rate = MAX_1GHZ, |
| 612 | }; |
| 613 | |
| 614 | static const struct inno_video_phy_plat_data max_2_5ghz_video_phy_plat_data = { |
| 615 | .inno_mipi_dphy_timing_table = inno_mipi_dphy_timing_table_max_2_5ghz, |
| 616 | .num_timings = ARRAY_SIZE(inno_mipi_dphy_timing_table_max_2_5ghz), |
| 617 | .max_rate = MAX_2_5GHZ, |
| 618 | }; |
| 619 | |
| 620 | static int inno_dsidphy_probe(struct udevice *dev) |
| 621 | { |
| 622 | struct inno_dsidphy *inno = dev_get_priv(dev); |
| 623 | int ret; |
| 624 | |
| 625 | inno->dev = dev; |
| 626 | inno->pdata = (const struct inno_video_phy_plat_data *)dev_get_driver_data(dev); |
| 627 | |
| 628 | inno->phy_base = dev_read_addr_ptr(dev); |
| 629 | if (IS_ERR(inno->phy_base)) |
| 630 | return PTR_ERR(inno->phy_base); |
| 631 | |
| 632 | inno->ref_clk = devm_clk_get(dev, "ref"); |
| 633 | if (IS_ERR(inno->ref_clk)) { |
| 634 | ret = PTR_ERR(inno->ref_clk); |
| 635 | dev_err(dev, "failed to get ref clock: %d\n", ret); |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | inno->pclk_phy = devm_clk_get(dev, "pclk"); |
| 640 | if (IS_ERR(inno->pclk_phy)) { |
| 641 | ret = PTR_ERR(inno->pclk_phy); |
| 642 | dev_err(dev, "failed to get phy pclk: %d\n", ret); |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | inno->rst = devm_reset_control_get(dev, "apb"); |
| 647 | if (IS_ERR(inno->rst)) { |
| 648 | ret = PTR_ERR(inno->rst); |
| 649 | dev_err(dev, "failed to get system reset control: %d\n", ret); |
| 650 | return ret; |
| 651 | } |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | static const struct udevice_id inno_dsidphy_of_match[] = { |
| 657 | { |
| 658 | .compatible = "rockchip,px30-dsi-dphy", |
| 659 | .data = (long)&max_1ghz_video_phy_plat_data, |
| 660 | }, { |
| 661 | .compatible = "rockchip,rk3128-dsi-dphy", |
| 662 | .data = (long)&max_1ghz_video_phy_plat_data, |
| 663 | }, { |
| 664 | .compatible = "rockchip,rk3368-dsi-dphy", |
| 665 | .data = (long)&max_1ghz_video_phy_plat_data, |
| 666 | }, { |
| 667 | .compatible = "rockchip,rk3568-dsi-dphy", |
| 668 | .data = (long)&max_2_5ghz_video_phy_plat_data, |
| 669 | }, |
| 670 | {} |
| 671 | }; |
| 672 | |
| 673 | U_BOOT_DRIVER(rockchip_inno_dsidphy) = { |
| 674 | .name = "rockchip-inno-dsidphy", |
| 675 | .id = UCLASS_PHY, |
| 676 | .of_match = inno_dsidphy_of_match, |
| 677 | .probe = inno_dsidphy_probe, |
| 678 | .ops = &inno_dsidphy_ops, |
| 679 | .priv_auto = sizeof(struct inno_dsidphy), |
| 680 | }; |