Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * |
| 5 | * (C) Copyright 2008-2014 Rockchip Electronics |
| 6 | * Peter, Software Engineering, <superpeter.cai@gmail.com>. |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 9 | #include <clk.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
| 12 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 14 | #include <asm/io.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 15 | #include <asm/arch-rockchip/clock.h> |
| 16 | #include <asm/arch-rockchip/i2c.h> |
| 17 | #include <asm/arch-rockchip/periph.h> |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 18 | #include <dm/pinctrl.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 20 | #include <linux/sizes.h> |
| 21 | |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 22 | /* i2c timerout */ |
| 23 | #define I2C_TIMEOUT_MS 100 |
| 24 | #define I2C_RETRY_COUNT 3 |
| 25 | |
| 26 | /* rk i2c fifo max transfer bytes */ |
| 27 | #define RK_I2C_FIFO_SIZE 32 |
| 28 | |
| 29 | struct rk_i2c { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 30 | struct clk clk; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 31 | struct i2c_regs *regs; |
| 32 | unsigned int speed; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 33 | }; |
| 34 | |
Alexander Kochetkov | c0830bf | 2018-02-26 20:42:54 +0300 | [diff] [blame] | 35 | enum { |
| 36 | RK_I2C_LEGACY, |
| 37 | RK_I2C_NEW, |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * @controller_type: i2c controller type |
| 42 | */ |
| 43 | struct rk_i2c_soc_data { |
| 44 | int controller_type; |
| 45 | }; |
| 46 | |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 47 | static inline void rk_i2c_get_div(int div, int *divh, int *divl) |
| 48 | { |
| 49 | *divl = div / 2; |
| 50 | if (div % 2 == 0) |
| 51 | *divh = div / 2; |
| 52 | else |
| 53 | *divh = DIV_ROUND_UP(div, 2); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * SCL Divisor = 8 * (CLKDIVL+1 + CLKDIVH+1) |
| 58 | * SCL = PCLK / SCLK Divisor |
| 59 | * i2c_rate = PCLK |
| 60 | */ |
| 61 | static void rk_i2c_set_clk(struct rk_i2c *i2c, uint32_t scl_rate) |
| 62 | { |
| 63 | uint32_t i2c_rate; |
| 64 | int div, divl, divh; |
| 65 | |
| 66 | /* First get i2c rate from pclk */ |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 67 | i2c_rate = clk_get_rate(&i2c->clk); |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 68 | |
| 69 | div = DIV_ROUND_UP(i2c_rate, scl_rate * 8) - 2; |
| 70 | divh = 0; |
| 71 | divl = 0; |
| 72 | if (div >= 0) |
| 73 | rk_i2c_get_div(div, &divh, &divl); |
| 74 | writel(I2C_CLKDIV_VAL(divl, divh), &i2c->regs->clkdiv); |
| 75 | |
| 76 | debug("rk_i2c_set_clk: i2c rate = %d, scl rate = %d\n", i2c_rate, |
| 77 | scl_rate); |
| 78 | debug("set i2c clk div = %d, divh = %d, divl = %d\n", div, divh, divl); |
| 79 | debug("set clk(I2C_CLKDIV: 0x%08x)\n", readl(&i2c->regs->clkdiv)); |
| 80 | } |
| 81 | |
| 82 | static void rk_i2c_show_regs(struct i2c_regs *regs) |
| 83 | { |
| 84 | #ifdef DEBUG |
| 85 | uint i; |
| 86 | |
| 87 | debug("i2c_con: 0x%08x\n", readl(®s->con)); |
| 88 | debug("i2c_clkdiv: 0x%08x\n", readl(®s->clkdiv)); |
| 89 | debug("i2c_mrxaddr: 0x%08x\n", readl(®s->mrxaddr)); |
| 90 | debug("i2c_mrxraddR: 0x%08x\n", readl(®s->mrxraddr)); |
| 91 | debug("i2c_mtxcnt: 0x%08x\n", readl(®s->mtxcnt)); |
| 92 | debug("i2c_mrxcnt: 0x%08x\n", readl(®s->mrxcnt)); |
| 93 | debug("i2c_ien: 0x%08x\n", readl(®s->ien)); |
| 94 | debug("i2c_ipd: 0x%08x\n", readl(®s->ipd)); |
| 95 | debug("i2c_fcnt: 0x%08x\n", readl(®s->fcnt)); |
| 96 | for (i = 0; i < 8; i++) |
| 97 | debug("i2c_txdata%d: 0x%08x\n", i, readl(®s->txdata[i])); |
| 98 | for (i = 0; i < 8; i++) |
| 99 | debug("i2c_rxdata%d: 0x%08x\n", i, readl(®s->rxdata[i])); |
| 100 | #endif |
| 101 | } |
| 102 | |
| 103 | static int rk_i2c_send_start_bit(struct rk_i2c *i2c) |
| 104 | { |
| 105 | struct i2c_regs *regs = i2c->regs; |
| 106 | ulong start; |
| 107 | |
| 108 | debug("I2c Send Start bit.\n"); |
| 109 | writel(I2C_IPD_ALL_CLEAN, ®s->ipd); |
| 110 | |
| 111 | writel(I2C_CON_EN | I2C_CON_START, ®s->con); |
| 112 | writel(I2C_STARTIEN, ®s->ien); |
| 113 | |
| 114 | start = get_timer(0); |
| 115 | while (1) { |
| 116 | if (readl(®s->ipd) & I2C_STARTIPD) { |
| 117 | writel(I2C_STARTIPD, ®s->ipd); |
| 118 | break; |
| 119 | } |
| 120 | if (get_timer(start) > I2C_TIMEOUT_MS) { |
| 121 | debug("I2C Send Start Bit Timeout\n"); |
| 122 | rk_i2c_show_regs(regs); |
| 123 | return -ETIMEDOUT; |
| 124 | } |
| 125 | udelay(1); |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int rk_i2c_send_stop_bit(struct rk_i2c *i2c) |
| 132 | { |
| 133 | struct i2c_regs *regs = i2c->regs; |
| 134 | ulong start; |
| 135 | |
| 136 | debug("I2c Send Stop bit.\n"); |
| 137 | writel(I2C_IPD_ALL_CLEAN, ®s->ipd); |
| 138 | |
| 139 | writel(I2C_CON_EN | I2C_CON_STOP, ®s->con); |
| 140 | writel(I2C_CON_STOP, ®s->ien); |
| 141 | |
| 142 | start = get_timer(0); |
| 143 | while (1) { |
| 144 | if (readl(®s->ipd) & I2C_STOPIPD) { |
| 145 | writel(I2C_STOPIPD, ®s->ipd); |
| 146 | break; |
| 147 | } |
| 148 | if (get_timer(start) > I2C_TIMEOUT_MS) { |
| 149 | debug("I2C Send Start Bit Timeout\n"); |
| 150 | rk_i2c_show_regs(regs); |
| 151 | return -ETIMEDOUT; |
| 152 | } |
| 153 | udelay(1); |
| 154 | } |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static inline void rk_i2c_disable(struct rk_i2c *i2c) |
| 160 | { |
| 161 | writel(0, &i2c->regs->con); |
| 162 | } |
| 163 | |
| 164 | static int rk_i2c_read(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len, |
| 165 | uchar *buf, uint b_len) |
| 166 | { |
| 167 | struct i2c_regs *regs = i2c->regs; |
| 168 | uchar *pbuf = buf; |
| 169 | uint bytes_remain_len = b_len; |
| 170 | uint bytes_xferred = 0; |
| 171 | uint words_xferred = 0; |
| 172 | ulong start; |
| 173 | uint con = 0; |
| 174 | uint rxdata; |
| 175 | uint i, j; |
| 176 | int err; |
Wadim Egorov | 838d754 | 2017-08-03 13:48:11 +0200 | [diff] [blame] | 177 | bool snd_chunk = false; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 178 | |
| 179 | debug("rk_i2c_read: chip = %d, reg = %d, r_len = %d, b_len = %d\n", |
| 180 | chip, reg, r_len, b_len); |
| 181 | |
| 182 | err = rk_i2c_send_start_bit(i2c); |
| 183 | if (err) |
| 184 | return err; |
| 185 | |
| 186 | writel(I2C_MRXADDR_SET(1, chip << 1 | 1), ®s->mrxaddr); |
| 187 | if (r_len == 0) { |
| 188 | writel(0, ®s->mrxraddr); |
| 189 | } else if (r_len < 4) { |
| 190 | writel(I2C_MRXRADDR_SET(r_len, reg), ®s->mrxraddr); |
| 191 | } else { |
| 192 | debug("I2C Read: addr len %d not supported\n", r_len); |
| 193 | return -EIO; |
| 194 | } |
| 195 | |
| 196 | while (bytes_remain_len) { |
| 197 | if (bytes_remain_len > RK_I2C_FIFO_SIZE) { |
Wadim Egorov | 838d754 | 2017-08-03 13:48:11 +0200 | [diff] [blame] | 198 | con = I2C_CON_EN; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 199 | bytes_xferred = 32; |
| 200 | } else { |
Wadim Egorov | 838d754 | 2017-08-03 13:48:11 +0200 | [diff] [blame] | 201 | /* |
| 202 | * The hw can read up to 32 bytes at a time. If we need |
| 203 | * more than one chunk, send an ACK after the last byte. |
| 204 | */ |
| 205 | con = I2C_CON_EN | I2C_CON_LASTACK; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 206 | bytes_xferred = bytes_remain_len; |
| 207 | } |
| 208 | words_xferred = DIV_ROUND_UP(bytes_xferred, 4); |
| 209 | |
Wadim Egorov | 838d754 | 2017-08-03 13:48:11 +0200 | [diff] [blame] | 210 | /* |
| 211 | * make sure we are in plain RX mode if we read a second chunk |
| 212 | */ |
| 213 | if (snd_chunk) |
| 214 | con |= I2C_CON_MOD(I2C_MODE_RX); |
| 215 | else |
| 216 | con |= I2C_CON_MOD(I2C_MODE_TRX); |
| 217 | |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 218 | writel(con, ®s->con); |
| 219 | writel(bytes_xferred, ®s->mrxcnt); |
| 220 | writel(I2C_MBRFIEN | I2C_NAKRCVIEN, ®s->ien); |
| 221 | |
| 222 | start = get_timer(0); |
| 223 | while (1) { |
| 224 | if (readl(®s->ipd) & I2C_NAKRCVIPD) { |
| 225 | writel(I2C_NAKRCVIPD, ®s->ipd); |
| 226 | err = -EREMOTEIO; |
| 227 | } |
| 228 | if (readl(®s->ipd) & I2C_MBRFIPD) { |
| 229 | writel(I2C_MBRFIPD, ®s->ipd); |
| 230 | break; |
| 231 | } |
| 232 | if (get_timer(start) > I2C_TIMEOUT_MS) { |
| 233 | debug("I2C Read Data Timeout\n"); |
| 234 | err = -ETIMEDOUT; |
| 235 | rk_i2c_show_regs(regs); |
| 236 | goto i2c_exit; |
| 237 | } |
| 238 | udelay(1); |
| 239 | } |
| 240 | |
| 241 | for (i = 0; i < words_xferred; i++) { |
| 242 | rxdata = readl(®s->rxdata[i]); |
| 243 | debug("I2c Read RXDATA[%d] = 0x%x\n", i, rxdata); |
| 244 | for (j = 0; j < 4; j++) { |
| 245 | if ((i * 4 + j) == bytes_xferred) |
| 246 | break; |
| 247 | *pbuf++ = (rxdata >> (j * 8)) & 0xff; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | bytes_remain_len -= bytes_xferred; |
Wadim Egorov | 838d754 | 2017-08-03 13:48:11 +0200 | [diff] [blame] | 252 | snd_chunk = true; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 253 | debug("I2C Read bytes_remain_len %d\n", bytes_remain_len); |
| 254 | } |
| 255 | |
| 256 | i2c_exit: |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 257 | rk_i2c_disable(i2c); |
| 258 | |
| 259 | return err; |
| 260 | } |
| 261 | |
| 262 | static int rk_i2c_write(struct rk_i2c *i2c, uchar chip, uint reg, uint r_len, |
| 263 | uchar *buf, uint b_len) |
| 264 | { |
| 265 | struct i2c_regs *regs = i2c->regs; |
| 266 | int err; |
| 267 | uchar *pbuf = buf; |
| 268 | uint bytes_remain_len = b_len + r_len + 1; |
| 269 | uint bytes_xferred = 0; |
| 270 | uint words_xferred = 0; |
| 271 | ulong start; |
| 272 | uint txdata; |
| 273 | uint i, j; |
| 274 | |
| 275 | debug("rk_i2c_write: chip = %d, reg = %d, r_len = %d, b_len = %d\n", |
| 276 | chip, reg, r_len, b_len); |
| 277 | err = rk_i2c_send_start_bit(i2c); |
| 278 | if (err) |
| 279 | return err; |
| 280 | |
| 281 | while (bytes_remain_len) { |
| 282 | if (bytes_remain_len > RK_I2C_FIFO_SIZE) |
John Keeping | febe763 | 2016-08-18 20:08:40 +0100 | [diff] [blame] | 283 | bytes_xferred = RK_I2C_FIFO_SIZE; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 284 | else |
| 285 | bytes_xferred = bytes_remain_len; |
| 286 | words_xferred = DIV_ROUND_UP(bytes_xferred, 4); |
| 287 | |
| 288 | for (i = 0; i < words_xferred; i++) { |
| 289 | txdata = 0; |
| 290 | for (j = 0; j < 4; j++) { |
| 291 | if ((i * 4 + j) == bytes_xferred) |
| 292 | break; |
| 293 | |
John Keeping | 27dfc94 | 2016-08-18 20:08:42 +0100 | [diff] [blame] | 294 | if (i == 0 && j == 0 && pbuf == buf) { |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 295 | txdata |= (chip << 1); |
John Keeping | 27dfc94 | 2016-08-18 20:08:42 +0100 | [diff] [blame] | 296 | } else if (i == 0 && j <= r_len && pbuf == buf) { |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 297 | txdata |= (reg & |
| 298 | (0xff << ((j - 1) * 8))) << 8; |
| 299 | } else { |
| 300 | txdata |= (*pbuf++)<<(j * 8); |
| 301 | } |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 302 | } |
John Keeping | bcd11c4 | 2016-08-18 20:08:41 +0100 | [diff] [blame] | 303 | writel(txdata, ®s->txdata[i]); |
| 304 | debug("I2c Write TXDATA[%d] = 0x%08x\n", i, txdata); |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | writel(I2C_CON_EN | I2C_CON_MOD(I2C_MODE_TX), ®s->con); |
| 308 | writel(bytes_xferred, ®s->mtxcnt); |
| 309 | writel(I2C_MBTFIEN | I2C_NAKRCVIEN, ®s->ien); |
| 310 | |
| 311 | start = get_timer(0); |
| 312 | while (1) { |
| 313 | if (readl(®s->ipd) & I2C_NAKRCVIPD) { |
| 314 | writel(I2C_NAKRCVIPD, ®s->ipd); |
| 315 | err = -EREMOTEIO; |
| 316 | } |
| 317 | if (readl(®s->ipd) & I2C_MBTFIPD) { |
| 318 | writel(I2C_MBTFIPD, ®s->ipd); |
| 319 | break; |
| 320 | } |
| 321 | if (get_timer(start) > I2C_TIMEOUT_MS) { |
| 322 | debug("I2C Write Data Timeout\n"); |
| 323 | err = -ETIMEDOUT; |
| 324 | rk_i2c_show_regs(regs); |
| 325 | goto i2c_exit; |
| 326 | } |
| 327 | udelay(1); |
| 328 | } |
| 329 | |
| 330 | bytes_remain_len -= bytes_xferred; |
| 331 | debug("I2C Write bytes_remain_len %d\n", bytes_remain_len); |
| 332 | } |
| 333 | |
| 334 | i2c_exit: |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 335 | rk_i2c_disable(i2c); |
| 336 | |
| 337 | return err; |
| 338 | } |
| 339 | |
| 340 | static int rockchip_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, |
| 341 | int nmsgs) |
| 342 | { |
| 343 | struct rk_i2c *i2c = dev_get_priv(bus); |
Ondrej Jirman | dc6e2d1 | 2023-05-25 14:18:17 +0200 | [diff] [blame] | 344 | int ret = 0; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 345 | |
| 346 | debug("i2c_xfer: %d messages\n", nmsgs); |
| 347 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 348 | debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len); |
| 349 | if (msg->flags & I2C_M_RD) { |
| 350 | ret = rk_i2c_read(i2c, msg->addr, 0, 0, msg->buf, |
| 351 | msg->len); |
| 352 | } else { |
| 353 | ret = rk_i2c_write(i2c, msg->addr, 0, 0, msg->buf, |
| 354 | msg->len); |
| 355 | } |
| 356 | if (ret) { |
| 357 | debug("i2c_write: error sending\n"); |
Ondrej Jirman | dc6e2d1 | 2023-05-25 14:18:17 +0200 | [diff] [blame] | 358 | ret = -EREMOTEIO; |
| 359 | break; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Vasily Khoruzhick | c795489 | 2019-11-16 11:32:57 -0800 | [diff] [blame] | 363 | rk_i2c_send_stop_bit(i2c); |
| 364 | rk_i2c_disable(i2c); |
| 365 | |
Ondrej Jirman | dc6e2d1 | 2023-05-25 14:18:17 +0200 | [diff] [blame] | 366 | return ret; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | int rockchip_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 370 | { |
| 371 | struct rk_i2c *i2c = dev_get_priv(bus); |
| 372 | |
| 373 | rk_i2c_set_clk(i2c, speed); |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 378 | static int rockchip_i2c_of_to_plat(struct udevice *bus) |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 379 | { |
Simon Glass | 3d15605 | 2016-01-21 19:43:42 -0700 | [diff] [blame] | 380 | struct rk_i2c *priv = dev_get_priv(bus); |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 381 | int ret; |
| 382 | |
Simon Glass | 3d15605 | 2016-01-21 19:43:42 -0700 | [diff] [blame] | 383 | ret = clk_get_by_index(bus, 0, &priv->clk); |
| 384 | if (ret < 0) { |
| 385 | debug("%s: Could not get clock for %s: %d\n", __func__, |
| 386 | bus->name, ret); |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 387 | return ret; |
Simon Glass | 3d15605 | 2016-01-21 19:43:42 -0700 | [diff] [blame] | 388 | } |
Simon Glass | 3d15605 | 2016-01-21 19:43:42 -0700 | [diff] [blame] | 389 | |
| 390 | return 0; |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 391 | } |
| 392 | |
Simon Glass | 3d15605 | 2016-01-21 19:43:42 -0700 | [diff] [blame] | 393 | static int rockchip_i2c_probe(struct udevice *bus) |
| 394 | { |
| 395 | struct rk_i2c *priv = dev_get_priv(bus); |
Alexander Kochetkov | c0830bf | 2018-02-26 20:42:54 +0300 | [diff] [blame] | 396 | struct rk_i2c_soc_data *soc_data; |
| 397 | struct udevice *pinctrl; |
| 398 | int bus_nr; |
| 399 | int ret; |
Simon Glass | 3d15605 | 2016-01-21 19:43:42 -0700 | [diff] [blame] | 400 | |
Philipp Tomsich | a9d953f | 2017-09-11 22:04:23 +0200 | [diff] [blame] | 401 | priv->regs = dev_read_addr_ptr(bus); |
Simon Glass | 3d15605 | 2016-01-21 19:43:42 -0700 | [diff] [blame] | 402 | |
Alexander Kochetkov | c0830bf | 2018-02-26 20:42:54 +0300 | [diff] [blame] | 403 | soc_data = (struct rk_i2c_soc_data*)dev_get_driver_data(bus); |
| 404 | |
| 405 | if (soc_data->controller_type == RK_I2C_LEGACY) { |
| 406 | ret = dev_read_alias_seq(bus, &bus_nr); |
| 407 | if (ret < 0) { |
| 408 | debug("%s: Could not get alias for %s: %d\n", |
| 409 | __func__, bus->name, ret); |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | ret = uclass_get_device(UCLASS_PINCTRL, 0, &pinctrl); |
| 414 | if (ret) { |
| 415 | debug("%s: Cannot find pinctrl device\n", __func__); |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | /* pinctrl will switch I2C to new type */ |
| 420 | ret = pinctrl_request_noflags(pinctrl, PERIPH_ID_I2C0 + bus_nr); |
| 421 | if (ret) { |
| 422 | debug("%s: Failed to switch I2C to new type %s: %d\n", |
| 423 | __func__, bus->name, ret); |
| 424 | return ret; |
| 425 | } |
| 426 | } |
| 427 | |
Simon Glass | 3d15605 | 2016-01-21 19:43:42 -0700 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 431 | static const struct dm_i2c_ops rockchip_i2c_ops = { |
| 432 | .xfer = rockchip_i2c_xfer, |
| 433 | .set_bus_speed = rockchip_i2c_set_bus_speed, |
| 434 | }; |
| 435 | |
Alexander Kochetkov | c0830bf | 2018-02-26 20:42:54 +0300 | [diff] [blame] | 436 | static const struct rk_i2c_soc_data rk3066_soc_data = { |
| 437 | .controller_type = RK_I2C_LEGACY, |
| 438 | }; |
| 439 | |
| 440 | static const struct rk_i2c_soc_data rk3188_soc_data = { |
| 441 | .controller_type = RK_I2C_LEGACY, |
| 442 | }; |
| 443 | |
| 444 | static const struct rk_i2c_soc_data rk3228_soc_data = { |
| 445 | .controller_type = RK_I2C_NEW, |
| 446 | }; |
| 447 | |
| 448 | static const struct rk_i2c_soc_data rk3288_soc_data = { |
| 449 | .controller_type = RK_I2C_NEW, |
| 450 | }; |
| 451 | |
| 452 | static const struct rk_i2c_soc_data rk3328_soc_data = { |
| 453 | .controller_type = RK_I2C_NEW, |
| 454 | }; |
| 455 | |
| 456 | static const struct rk_i2c_soc_data rk3399_soc_data = { |
| 457 | .controller_type = RK_I2C_NEW, |
| 458 | }; |
| 459 | |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 460 | static const struct udevice_id rockchip_i2c_ids[] = { |
Alexander Kochetkov | c0830bf | 2018-02-26 20:42:54 +0300 | [diff] [blame] | 461 | { |
| 462 | .compatible = "rockchip,rk3066-i2c", |
| 463 | .data = (ulong)&rk3066_soc_data, |
| 464 | }, |
| 465 | { |
| 466 | .compatible = "rockchip,rk3188-i2c", |
| 467 | .data = (ulong)&rk3188_soc_data, |
| 468 | }, |
| 469 | { |
| 470 | .compatible = "rockchip,rk3228-i2c", |
| 471 | .data = (ulong)&rk3228_soc_data, |
| 472 | }, |
| 473 | { |
| 474 | .compatible = "rockchip,rk3288-i2c", |
| 475 | .data = (ulong)&rk3288_soc_data, |
| 476 | }, |
| 477 | { |
| 478 | .compatible = "rockchip,rk3328-i2c", |
| 479 | .data = (ulong)&rk3328_soc_data, |
| 480 | }, |
| 481 | { |
| 482 | .compatible = "rockchip,rk3399-i2c", |
| 483 | .data = (ulong)&rk3399_soc_data, |
| 484 | }, |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 485 | { } |
| 486 | }; |
| 487 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 488 | U_BOOT_DRIVER(rockchip_rk3066_i2c) = { |
| 489 | .name = "rockchip_rk3066_i2c", |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 490 | .id = UCLASS_I2C, |
| 491 | .of_match = rockchip_i2c_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 492 | .of_to_plat = rockchip_i2c_of_to_plat, |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 493 | .probe = rockchip_i2c_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 494 | .priv_auto = sizeof(struct rk_i2c), |
Simon Glass | 3595f95 | 2015-08-30 16:55:39 -0600 | [diff] [blame] | 495 | .ops = &rockchip_i2c_ops, |
| 496 | }; |
Walter Lozano | 48e5b04 | 2020-06-25 01:10:06 -0300 | [diff] [blame] | 497 | |
Simon Glass | df65db8 | 2020-12-28 20:34:57 -0700 | [diff] [blame] | 498 | DM_DRIVER_ALIAS(rockchip_rk3066_i2c, rockchip_rk3288_i2c) |