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