Alper Nebi Yasak | 53f2033 | 2020-10-22 22:43:13 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd |
| 4 | * Author: Eric Gao <eric.gao@rock-chips.com> |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
| 9 | #include <display.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 12 | #include <panel.h> |
| 13 | #include <regmap.h> |
eric.gao@rock-chips.com | b7e4b8c | 2017-06-21 11:20:33 +0800 | [diff] [blame] | 14 | #include "rk_mipi.h" |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 15 | #include <syscon.h> |
| 16 | #include <asm/gpio.h> |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <dm/uclass-internal.h> |
| 19 | #include <linux/kernel.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 20 | #include <asm/arch-rockchip/clock.h> |
Jagan Teki | 783acfd | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 21 | #include <asm/arch-rockchip/cru.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 22 | #include <asm/arch-rockchip/grf_rk3399.h> |
| 23 | #include <asm/arch-rockchip/rockchip_mipi_dsi.h> |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 24 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
eric.gao@rock-chips.com | b7e4b8c | 2017-06-21 11:20:33 +0800 | [diff] [blame] | 27 | int rk_mipi_read_timing(struct udevice *dev, |
| 28 | struct display_timing *timing) |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 29 | { |
| 30 | int ret; |
| 31 | |
Kever Yang | 8cda38d | 2020-02-19 09:45:37 +0800 | [diff] [blame] | 32 | ret = ofnode_decode_display_timing(dev_ofnode(dev), 0, timing); |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 33 | if (ret) { |
| 34 | debug("%s: Failed to decode display timing (ret=%d)\n", |
| 35 | __func__, ret); |
| 36 | return -EINVAL; |
| 37 | } |
| 38 | |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * Register write function used only for mipi dsi controller. |
| 44 | * Parameter: |
| 45 | * @regs: mipi controller address |
| 46 | * @reg: combination of regaddr(16bit)|bitswidth(8bit)|offset(8bit) you can |
| 47 | * use define in rk_mipi.h directly for this parameter |
| 48 | * @val: value that will be write to specified bits of register |
| 49 | */ |
eric.gao@rock-chips.com | c7dd29a | 2017-06-21 11:12:50 +0800 | [diff] [blame] | 50 | static void rk_mipi_dsi_write(uintptr_t regs, u32 reg, u32 val) |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 51 | { |
| 52 | u32 dat; |
| 53 | u32 mask; |
| 54 | u32 offset = (reg >> OFFSET_SHIFT) & 0xff; |
| 55 | u32 bits = (reg >> BITS_SHIFT) & 0xff; |
eric.gao@rock-chips.com | c7dd29a | 2017-06-21 11:12:50 +0800 | [diff] [blame] | 56 | uintptr_t addr = (reg >> ADDR_SHIFT) + regs; |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 57 | |
| 58 | /* Mask for specifiled bits,the corresponding bits will be clear */ |
| 59 | mask = ~((0xffffffff << offset) & (0xffffffff >> (32 - offset - bits))); |
| 60 | |
| 61 | /* Make sure val in the available range */ |
| 62 | val &= ~(0xffffffff << bits); |
| 63 | |
| 64 | /* Get register's original val */ |
| 65 | dat = readl(addr); |
| 66 | |
| 67 | /* Clear specified bits */ |
| 68 | dat &= mask; |
| 69 | |
| 70 | /* Fill specified bits */ |
| 71 | dat |= val << offset; |
| 72 | |
| 73 | writel(dat, addr); |
| 74 | } |
| 75 | |
eric.gao@rock-chips.com | b7e4b8c | 2017-06-21 11:20:33 +0800 | [diff] [blame] | 76 | int rk_mipi_dsi_enable(struct udevice *dev, |
| 77 | const struct display_timing *timing) |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 78 | { |
Kever Yang | e686739 | 2020-02-19 09:45:38 +0800 | [diff] [blame] | 79 | ofnode node, timing_node; |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 80 | int val; |
| 81 | struct rk_mipi_priv *priv = dev_get_priv(dev); |
eric.gao@rock-chips.com | c7dd29a | 2017-06-21 11:12:50 +0800 | [diff] [blame] | 82 | uintptr_t regs = priv->regs; |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 83 | u32 txbyte_clk = priv->txbyte_clk; |
| 84 | u32 txesc_clk = priv->txesc_clk; |
| 85 | |
| 86 | txesc_clk = txbyte_clk/(txbyte_clk/txesc_clk + 1); |
| 87 | |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 88 | /* Set Display timing parameter */ |
| 89 | rk_mipi_dsi_write(regs, VID_HSA_TIME, timing->hsync_len.typ); |
| 90 | rk_mipi_dsi_write(regs, VID_HBP_TIME, timing->hback_porch.typ); |
| 91 | rk_mipi_dsi_write(regs, VID_HLINE_TIME, (timing->hsync_len.typ |
| 92 | + timing->hback_porch.typ + timing->hactive.typ |
| 93 | + timing->hfront_porch.typ)); |
| 94 | rk_mipi_dsi_write(regs, VID_VSA_LINES, timing->vsync_len.typ); |
| 95 | rk_mipi_dsi_write(regs, VID_VBP_LINES, timing->vback_porch.typ); |
| 96 | rk_mipi_dsi_write(regs, VID_VFP_LINES, timing->vfront_porch.typ); |
| 97 | rk_mipi_dsi_write(regs, VID_ACTIVE_LINES, timing->vactive.typ); |
| 98 | |
| 99 | /* Set Signal Polarity */ |
| 100 | val = (timing->flags & DISPLAY_FLAGS_HSYNC_LOW) ? 1 : 0; |
| 101 | rk_mipi_dsi_write(regs, HSYNC_ACTIVE_LOW, val); |
| 102 | |
| 103 | val = (timing->flags & DISPLAY_FLAGS_VSYNC_LOW) ? 1 : 0; |
| 104 | rk_mipi_dsi_write(regs, VSYNC_ACTIVE_LOW, val); |
| 105 | |
| 106 | val = (timing->flags & DISPLAY_FLAGS_DE_LOW) ? 1 : 0; |
Richard Röjfors | dab5b83 | 2018-11-07 11:34:44 +0100 | [diff] [blame] | 107 | rk_mipi_dsi_write(regs, DATAEN_ACTIVE_LOW, val); |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 108 | |
| 109 | val = (timing->flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE) ? 1 : 0; |
| 110 | rk_mipi_dsi_write(regs, COLORM_ACTIVE_LOW, val); |
| 111 | |
| 112 | /* Set video mode */ |
| 113 | rk_mipi_dsi_write(regs, CMD_VIDEO_MODE, VIDEO_MODE); |
| 114 | |
| 115 | /* Set video mode transmission type as burst mode */ |
| 116 | rk_mipi_dsi_write(regs, VID_MODE_TYPE, BURST_MODE); |
| 117 | |
| 118 | /* Set pix num in a video package */ |
| 119 | rk_mipi_dsi_write(regs, VID_PKT_SIZE, 0x4b0); |
| 120 | |
| 121 | /* Set dpi color coding depth 24 bit */ |
Kever Yang | e686739 | 2020-02-19 09:45:38 +0800 | [diff] [blame] | 122 | timing_node = ofnode_find_subnode(dev->node, "display-timings"); |
| 123 | node = ofnode_first_subnode(timing_node); |
| 124 | |
| 125 | val = ofnode_read_u32_default(node, "bits-per-pixel", -1); |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 126 | switch (val) { |
| 127 | case 16: |
| 128 | rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_16BIT_CFG_1); |
| 129 | break; |
| 130 | case 24: |
| 131 | rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_24BIT); |
| 132 | break; |
| 133 | case 30: |
| 134 | rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_30BIT); |
| 135 | break; |
| 136 | default: |
| 137 | rk_mipi_dsi_write(regs, DPI_COLOR_CODING, DPI_24BIT); |
| 138 | } |
| 139 | /* Enable low power mode */ |
| 140 | rk_mipi_dsi_write(regs, LP_CMD_EN, 1); |
| 141 | rk_mipi_dsi_write(regs, LP_HFP_EN, 1); |
| 142 | rk_mipi_dsi_write(regs, LP_VACT_EN, 1); |
| 143 | rk_mipi_dsi_write(regs, LP_VFP_EN, 1); |
| 144 | rk_mipi_dsi_write(regs, LP_VBP_EN, 1); |
| 145 | rk_mipi_dsi_write(regs, LP_VSA_EN, 1); |
| 146 | |
| 147 | /* Division for timeout counter clk */ |
| 148 | rk_mipi_dsi_write(regs, TO_CLK_DIVISION, 0x0a); |
| 149 | |
| 150 | /* Tx esc clk division from txbyte clk */ |
| 151 | rk_mipi_dsi_write(regs, TX_ESC_CLK_DIVISION, txbyte_clk/txesc_clk); |
| 152 | |
| 153 | /* Timeout count for hs<->lp transation between Line period */ |
| 154 | rk_mipi_dsi_write(regs, HSTX_TO_CNT, 0x3e8); |
| 155 | |
| 156 | /* Phy State transfer timing */ |
| 157 | rk_mipi_dsi_write(regs, PHY_STOP_WAIT_TIME, 32); |
| 158 | rk_mipi_dsi_write(regs, PHY_TXREQUESTCLKHS, 1); |
| 159 | rk_mipi_dsi_write(regs, PHY_HS2LP_TIME, 0x14); |
| 160 | rk_mipi_dsi_write(regs, PHY_LP2HS_TIME, 0x10); |
| 161 | rk_mipi_dsi_write(regs, MAX_RD_TIME, 0x2710); |
| 162 | |
| 163 | /* Power on */ |
| 164 | rk_mipi_dsi_write(regs, SHUTDOWNZ, 1); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* rk mipi dphy write function. It is used to write test data to dphy */ |
eric.gao@rock-chips.com | c7dd29a | 2017-06-21 11:12:50 +0800 | [diff] [blame] | 170 | static void rk_mipi_phy_write(uintptr_t regs, unsigned char test_code, |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 171 | unsigned char *test_data, unsigned char size) |
| 172 | { |
| 173 | int i = 0; |
| 174 | |
| 175 | /* Write Test code */ |
| 176 | rk_mipi_dsi_write(regs, PHY_TESTCLK, 1); |
| 177 | rk_mipi_dsi_write(regs, PHY_TESTDIN, test_code); |
| 178 | rk_mipi_dsi_write(regs, PHY_TESTEN, 1); |
| 179 | rk_mipi_dsi_write(regs, PHY_TESTCLK, 0); |
| 180 | rk_mipi_dsi_write(regs, PHY_TESTEN, 0); |
| 181 | |
| 182 | /* Write Test data */ |
| 183 | for (i = 0; i < size; i++) { |
| 184 | rk_mipi_dsi_write(regs, PHY_TESTCLK, 0); |
| 185 | rk_mipi_dsi_write(regs, PHY_TESTDIN, test_data[i]); |
| 186 | rk_mipi_dsi_write(regs, PHY_TESTCLK, 1); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Mipi dphy config function. Calculate the suitable prediv, feedback div, |
| 192 | * fsfreqrang value ,cap ,lpf and so on according to the given pix clk rate, |
| 193 | * and then enable phy. |
| 194 | */ |
eric.gao@rock-chips.com | b7e4b8c | 2017-06-21 11:20:33 +0800 | [diff] [blame] | 195 | int rk_mipi_phy_enable(struct udevice *dev) |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 196 | { |
| 197 | int i; |
| 198 | struct rk_mipi_priv *priv = dev_get_priv(dev); |
eric.gao@rock-chips.com | c7dd29a | 2017-06-21 11:12:50 +0800 | [diff] [blame] | 199 | uintptr_t regs = priv->regs; |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 200 | u64 fbdiv; |
| 201 | u64 prediv = 1; |
| 202 | u32 max_fbdiv = 512; |
| 203 | u32 max_prediv, min_prediv; |
| 204 | u64 ddr_clk = priv->phy_clk; |
| 205 | u32 refclk = priv->ref_clk; |
| 206 | u32 remain = refclk; |
| 207 | unsigned char test_data[2] = {0}; |
| 208 | |
| 209 | int freq_rang[][2] = { |
| 210 | {90, 0x01}, {100, 0x10}, {110, 0x20}, {130, 0x01}, |
| 211 | {140, 0x11}, {150, 0x21}, {170, 0x02}, {180, 0x12}, |
| 212 | {200, 0x22}, {220, 0x03}, {240, 0x13}, {250, 0x23}, |
| 213 | {270, 0x04}, {300, 0x14}, {330, 0x05}, {360, 0x15}, |
| 214 | {400, 0x25}, {450, 0x06}, {500, 0x16}, {550, 0x07}, |
| 215 | {600, 0x17}, {650, 0x08}, {700, 0x18}, {750, 0x09}, |
| 216 | {800, 0x19}, {850, 0x29}, {900, 0x39}, {950, 0x0a}, |
| 217 | {1000, 0x1a}, {1050, 0x2a}, {1100, 0x3a}, {1150, 0x0b}, |
| 218 | {1200, 0x1b}, {1250, 0x2b}, {1300, 0x3b}, {1350, 0x0c}, |
| 219 | {1400, 0x1c}, {1450, 0x2c}, {1500, 0x3c} |
| 220 | }; |
| 221 | |
| 222 | /* Shutdown mode */ |
| 223 | rk_mipi_dsi_write(regs, PHY_SHUTDOWNZ, 0); |
| 224 | rk_mipi_dsi_write(regs, PHY_RSTZ, 0); |
| 225 | rk_mipi_dsi_write(regs, PHY_TESTCLR, 1); |
| 226 | |
| 227 | /* Pll locking */ |
| 228 | rk_mipi_dsi_write(regs, PHY_TESTCLR, 0); |
| 229 | |
| 230 | /* config cp and lfp */ |
| 231 | test_data[0] = 0x80 | (ddr_clk / (200 * MHz)) << 3 | 0x3; |
| 232 | rk_mipi_phy_write(regs, CODE_PLL_VCORANGE_VCOCAP, test_data, 1); |
| 233 | |
| 234 | test_data[0] = 0x8; |
| 235 | rk_mipi_phy_write(regs, CODE_PLL_CPCTRL, test_data, 1); |
| 236 | |
| 237 | test_data[0] = 0x80 | 0x40; |
| 238 | rk_mipi_phy_write(regs, CODE_PLL_LPF_CP, test_data, 1); |
| 239 | |
| 240 | /* select the suitable value for fsfreqrang reg */ |
| 241 | for (i = 0; i < ARRAY_SIZE(freq_rang); i++) { |
Richard Röjfors | 3c4b72b | 2018-11-14 14:13:53 +0100 | [diff] [blame] | 242 | if (ddr_clk / (MHz) <= freq_rang[i][0]) |
Eric Gao | 4789bc1 | 2017-05-02 18:23:50 +0800 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | if (i == ARRAY_SIZE(freq_rang)) { |
| 246 | debug("%s: Dphy freq out of range!\n", __func__); |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | test_data[0] = freq_rang[i][1] << 1; |
| 250 | rk_mipi_phy_write(regs, CODE_HS_RX_LANE0, test_data, 1); |
| 251 | |
| 252 | /* |
| 253 | * Calculate the best ddrclk and it's corresponding div value. If the |
| 254 | * given pixelclock is great than 250M, ddrclk will be fix 1500M. |
| 255 | * Otherwise, |
| 256 | * it's equal to ddr_clk= pixclk * 6. 40MHz >= refclk / prediv >= 5MHz |
| 257 | * according to spec. |
| 258 | */ |
| 259 | max_prediv = (refclk / (5 * MHz)); |
| 260 | min_prediv = ((refclk / (40 * MHz)) ? (refclk / (40 * MHz) + 1) : 1); |
| 261 | |
| 262 | debug("%s: DEBUG: max_prediv=%u, min_prediv=%u\n", __func__, max_prediv, |
| 263 | min_prediv); |
| 264 | |
| 265 | if (max_prediv < min_prediv) { |
| 266 | debug("%s: Invalid refclk value\n", __func__); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | /* Calculate the best refclk and feedback division value for dphy pll */ |
| 271 | for (i = min_prediv; i < max_prediv; i++) { |
| 272 | if ((ddr_clk * i % refclk < remain) && |
| 273 | (ddr_clk * i / refclk) < max_fbdiv) { |
| 274 | prediv = i; |
| 275 | remain = ddr_clk * i % refclk; |
| 276 | } |
| 277 | } |
| 278 | fbdiv = ddr_clk * prediv / refclk; |
| 279 | ddr_clk = refclk * fbdiv / prediv; |
| 280 | priv->phy_clk = ddr_clk; |
| 281 | |
| 282 | debug("%s: DEBUG: refclk=%u, refclk=%llu, fbdiv=%llu, phyclk=%llu\n", |
| 283 | __func__, refclk, prediv, fbdiv, ddr_clk); |
| 284 | |
| 285 | /* config prediv and feedback reg */ |
| 286 | test_data[0] = prediv - 1; |
| 287 | rk_mipi_phy_write(regs, CODE_PLL_INPUT_DIV_RAT, test_data, 1); |
| 288 | test_data[0] = (fbdiv - 1) & 0x1f; |
| 289 | rk_mipi_phy_write(regs, CODE_PLL_LOOP_DIV_RAT, test_data, 1); |
| 290 | test_data[0] = (fbdiv - 1) >> 5 | 0x80; |
| 291 | rk_mipi_phy_write(regs, CODE_PLL_LOOP_DIV_RAT, test_data, 1); |
| 292 | test_data[0] = 0x30; |
| 293 | rk_mipi_phy_write(regs, CODE_PLL_INPUT_LOOP_DIV_RAT, test_data, 1); |
| 294 | |
| 295 | /* rest config */ |
| 296 | test_data[0] = 0x4d; |
| 297 | rk_mipi_phy_write(regs, CODE_BANDGAP_BIAS_CTRL, test_data, 1); |
| 298 | |
| 299 | test_data[0] = 0x3d; |
| 300 | rk_mipi_phy_write(regs, CODE_TERMINATION_CTRL, test_data, 1); |
| 301 | |
| 302 | test_data[0] = 0xdf; |
| 303 | rk_mipi_phy_write(regs, CODE_TERMINATION_CTRL, test_data, 1); |
| 304 | |
| 305 | test_data[0] = 0x7; |
| 306 | rk_mipi_phy_write(regs, CODE_AFE_BIAS_BANDGAP_ANOLOG, test_data, 1); |
| 307 | |
| 308 | test_data[0] = 0x80 | 0x7; |
| 309 | rk_mipi_phy_write(regs, CODE_AFE_BIAS_BANDGAP_ANOLOG, test_data, 1); |
| 310 | |
| 311 | test_data[0] = 0x80 | 15; |
| 312 | rk_mipi_phy_write(regs, CODE_HSTXDATALANEREQUSETSTATETIME, |
| 313 | test_data, 1); |
| 314 | test_data[0] = 0x80 | 85; |
| 315 | rk_mipi_phy_write(regs, CODE_HSTXDATALANEPREPARESTATETIME, |
| 316 | test_data, 1); |
| 317 | test_data[0] = 0x40 | 10; |
| 318 | rk_mipi_phy_write(regs, CODE_HSTXDATALANEHSZEROSTATETIME, |
| 319 | test_data, 1); |
| 320 | |
| 321 | /* enter into stop mode */ |
| 322 | rk_mipi_dsi_write(regs, N_LANES, 0x03); |
| 323 | rk_mipi_dsi_write(regs, PHY_ENABLECLK, 1); |
| 324 | rk_mipi_dsi_write(regs, PHY_FORCEPLL, 1); |
| 325 | rk_mipi_dsi_write(regs, PHY_SHUTDOWNZ, 1); |
| 326 | rk_mipi_dsi_write(regs, PHY_RSTZ, 1); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |