Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for I2C adapter in Rockchip RK3xxx SoC |
| 3 | * |
| 4 | * Max Schwarz <max.schwarz@online.de> |
| 5 | * based on the patches by Rockchip Inc. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/i2c.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/of_address.h> |
| 21 | #include <linux/of_irq.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/clk.h> |
| 24 | #include <linux/wait.h> |
| 25 | #include <linux/mfd/syscon.h> |
| 26 | #include <linux/regmap.h> |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 27 | #include <linux/math64.h> |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 28 | |
| 29 | |
| 30 | /* Register Map */ |
| 31 | #define REG_CON 0x00 /* control register */ |
| 32 | #define REG_CLKDIV 0x04 /* clock divisor register */ |
| 33 | #define REG_MRXADDR 0x08 /* slave address for REGISTER_TX */ |
| 34 | #define REG_MRXRADDR 0x0c /* slave register address for REGISTER_TX */ |
| 35 | #define REG_MTXCNT 0x10 /* number of bytes to be transmitted */ |
| 36 | #define REG_MRXCNT 0x14 /* number of bytes to be received */ |
| 37 | #define REG_IEN 0x18 /* interrupt enable */ |
| 38 | #define REG_IPD 0x1c /* interrupt pending */ |
| 39 | #define REG_FCNT 0x20 /* finished count */ |
| 40 | |
| 41 | /* Data buffer offsets */ |
| 42 | #define TXBUFFER_BASE 0x100 |
| 43 | #define RXBUFFER_BASE 0x200 |
| 44 | |
| 45 | /* REG_CON bits */ |
| 46 | #define REG_CON_EN BIT(0) |
| 47 | enum { |
| 48 | REG_CON_MOD_TX = 0, /* transmit data */ |
| 49 | REG_CON_MOD_REGISTER_TX, /* select register and restart */ |
| 50 | REG_CON_MOD_RX, /* receive data */ |
| 51 | REG_CON_MOD_REGISTER_RX, /* broken: transmits read addr AND writes |
| 52 | * register addr */ |
| 53 | }; |
| 54 | #define REG_CON_MOD(mod) ((mod) << 1) |
| 55 | #define REG_CON_MOD_MASK (BIT(1) | BIT(2)) |
| 56 | #define REG_CON_START BIT(3) |
| 57 | #define REG_CON_STOP BIT(4) |
| 58 | #define REG_CON_LASTACK BIT(5) /* 1: send NACK after last received byte */ |
| 59 | #define REG_CON_ACTACK BIT(6) /* 1: stop if NACK is received */ |
| 60 | |
| 61 | /* REG_MRXADDR bits */ |
| 62 | #define REG_MRXADDR_VALID(x) BIT(24 + (x)) /* [x*8+7:x*8] of MRX[R]ADDR valid */ |
| 63 | |
| 64 | /* REG_IEN/REG_IPD bits */ |
| 65 | #define REG_INT_BTF BIT(0) /* a byte was transmitted */ |
| 66 | #define REG_INT_BRF BIT(1) /* a byte was received */ |
| 67 | #define REG_INT_MBTF BIT(2) /* master data transmit finished */ |
| 68 | #define REG_INT_MBRF BIT(3) /* master data receive finished */ |
| 69 | #define REG_INT_START BIT(4) /* START condition generated */ |
| 70 | #define REG_INT_STOP BIT(5) /* STOP condition generated */ |
| 71 | #define REG_INT_NAKRCV BIT(6) /* NACK received */ |
| 72 | #define REG_INT_ALL 0x7f |
| 73 | |
| 74 | /* Constants */ |
Doug Anderson | 4489750 | 2015-05-11 12:44:28 -0700 | [diff] [blame] | 75 | #define WAIT_TIMEOUT 1000 /* ms */ |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 76 | #define DEFAULT_SCL_RATE (100 * 1000) /* Hz */ |
| 77 | |
| 78 | enum rk3x_i2c_state { |
| 79 | STATE_IDLE, |
| 80 | STATE_START, |
| 81 | STATE_READ, |
| 82 | STATE_WRITE, |
| 83 | STATE_STOP |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * @grf_offset: offset inside the grf regmap for setting the i2c type |
| 88 | */ |
| 89 | struct rk3x_i2c_soc_data { |
| 90 | int grf_offset; |
| 91 | }; |
| 92 | |
David Wu | 0a6ad2f | 2016-05-16 21:57:36 +0800 | [diff] [blame^] | 93 | /** |
| 94 | * struct rk3x_i2c - private data of the controller |
| 95 | * @adap: corresponding I2C adapter |
| 96 | * @dev: device for this controller |
| 97 | * @soc_data: related soc data struct |
| 98 | * @regs: virtual memory area |
| 99 | * @clk: clock of i2c bus |
| 100 | * @clk_rate_nb: i2c clk rate change notify |
| 101 | * @t: I2C known timing information |
| 102 | * @lock: spinlock for the i2c bus |
| 103 | * @wait: the waitqueue to wait for i2c transfer |
| 104 | * @busy: the condition for the event to wait for |
| 105 | * @msg: current i2c message |
| 106 | * @addr: addr of i2c slave device |
| 107 | * @mode: mode of i2c transfer |
| 108 | * @is_last_msg: flag determines whether it is the last msg in this transfer |
| 109 | * @state: state of i2c transfer |
| 110 | * @processed: byte length which has been send or received |
| 111 | * @error: error code for i2c transfer |
| 112 | */ |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 113 | struct rk3x_i2c { |
| 114 | struct i2c_adapter adap; |
| 115 | struct device *dev; |
| 116 | struct rk3x_i2c_soc_data *soc_data; |
| 117 | |
| 118 | /* Hardware resources */ |
| 119 | void __iomem *regs; |
| 120 | struct clk *clk; |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 121 | struct notifier_block clk_rate_nb; |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 122 | |
| 123 | /* Settings */ |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 124 | struct i2c_timings t; |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 125 | |
| 126 | /* Synchronization & notification */ |
| 127 | spinlock_t lock; |
| 128 | wait_queue_head_t wait; |
| 129 | bool busy; |
| 130 | |
| 131 | /* Current message */ |
| 132 | struct i2c_msg *msg; |
| 133 | u8 addr; |
| 134 | unsigned int mode; |
| 135 | bool is_last_msg; |
| 136 | |
| 137 | /* I2C state machine */ |
| 138 | enum rk3x_i2c_state state; |
David Wu | 0a6ad2f | 2016-05-16 21:57:36 +0800 | [diff] [blame^] | 139 | unsigned int processed; |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 140 | int error; |
| 141 | }; |
| 142 | |
| 143 | static inline void i2c_writel(struct rk3x_i2c *i2c, u32 value, |
| 144 | unsigned int offset) |
| 145 | { |
| 146 | writel(value, i2c->regs + offset); |
| 147 | } |
| 148 | |
| 149 | static inline u32 i2c_readl(struct rk3x_i2c *i2c, unsigned int offset) |
| 150 | { |
| 151 | return readl(i2c->regs + offset); |
| 152 | } |
| 153 | |
| 154 | /* Reset all interrupt pending bits */ |
| 155 | static inline void rk3x_i2c_clean_ipd(struct rk3x_i2c *i2c) |
| 156 | { |
| 157 | i2c_writel(i2c, REG_INT_ALL, REG_IPD); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Generate a START condition, which triggers a REG_INT_START interrupt. |
| 162 | */ |
| 163 | static void rk3x_i2c_start(struct rk3x_i2c *i2c) |
| 164 | { |
| 165 | u32 val; |
| 166 | |
| 167 | rk3x_i2c_clean_ipd(i2c); |
| 168 | i2c_writel(i2c, REG_INT_START, REG_IEN); |
| 169 | |
| 170 | /* enable adapter with correct mode, send START condition */ |
| 171 | val = REG_CON_EN | REG_CON_MOD(i2c->mode) | REG_CON_START; |
| 172 | |
| 173 | /* if we want to react to NACK, set ACTACK bit */ |
| 174 | if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) |
| 175 | val |= REG_CON_ACTACK; |
| 176 | |
| 177 | i2c_writel(i2c, val, REG_CON); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Generate a STOP condition, which triggers a REG_INT_STOP interrupt. |
| 182 | * |
| 183 | * @error: Error code to return in rk3x_i2c_xfer |
| 184 | */ |
| 185 | static void rk3x_i2c_stop(struct rk3x_i2c *i2c, int error) |
| 186 | { |
| 187 | unsigned int ctrl; |
| 188 | |
| 189 | i2c->processed = 0; |
| 190 | i2c->msg = NULL; |
| 191 | i2c->error = error; |
| 192 | |
| 193 | if (i2c->is_last_msg) { |
| 194 | /* Enable stop interrupt */ |
| 195 | i2c_writel(i2c, REG_INT_STOP, REG_IEN); |
| 196 | |
| 197 | i2c->state = STATE_STOP; |
| 198 | |
| 199 | ctrl = i2c_readl(i2c, REG_CON); |
| 200 | ctrl |= REG_CON_STOP; |
| 201 | i2c_writel(i2c, ctrl, REG_CON); |
| 202 | } else { |
| 203 | /* Signal rk3x_i2c_xfer to start the next message. */ |
| 204 | i2c->busy = false; |
| 205 | i2c->state = STATE_IDLE; |
| 206 | |
| 207 | /* |
| 208 | * The HW is actually not capable of REPEATED START. But we can |
| 209 | * get the intended effect by resetting its internal state |
| 210 | * and issuing an ordinary START. |
| 211 | */ |
| 212 | i2c_writel(i2c, 0, REG_CON); |
| 213 | |
| 214 | /* signal that we are finished with the current msg */ |
| 215 | wake_up(&i2c->wait); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Setup a read according to i2c->msg |
| 221 | */ |
| 222 | static void rk3x_i2c_prepare_read(struct rk3x_i2c *i2c) |
| 223 | { |
| 224 | unsigned int len = i2c->msg->len - i2c->processed; |
| 225 | u32 con; |
| 226 | |
| 227 | con = i2c_readl(i2c, REG_CON); |
| 228 | |
| 229 | /* |
| 230 | * The hw can read up to 32 bytes at a time. If we need more than one |
| 231 | * chunk, send an ACK after the last byte of the current chunk. |
| 232 | */ |
Doug Anderson | 2920933 | 2014-08-22 10:43:44 -0700 | [diff] [blame] | 233 | if (len > 32) { |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 234 | len = 32; |
| 235 | con &= ~REG_CON_LASTACK; |
| 236 | } else { |
| 237 | con |= REG_CON_LASTACK; |
| 238 | } |
| 239 | |
| 240 | /* make sure we are in plain RX mode if we read a second chunk */ |
| 241 | if (i2c->processed != 0) { |
| 242 | con &= ~REG_CON_MOD_MASK; |
| 243 | con |= REG_CON_MOD(REG_CON_MOD_RX); |
| 244 | } |
| 245 | |
| 246 | i2c_writel(i2c, con, REG_CON); |
| 247 | i2c_writel(i2c, len, REG_MRXCNT); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Fill the transmit buffer with data from i2c->msg |
| 252 | */ |
| 253 | static void rk3x_i2c_fill_transmit_buf(struct rk3x_i2c *i2c) |
| 254 | { |
| 255 | unsigned int i, j; |
| 256 | u32 cnt = 0; |
| 257 | u32 val; |
| 258 | u8 byte; |
| 259 | |
| 260 | for (i = 0; i < 8; ++i) { |
| 261 | val = 0; |
| 262 | for (j = 0; j < 4; ++j) { |
Alexandru M Stan | cf27020 | 2014-10-01 10:40:41 -0700 | [diff] [blame] | 263 | if ((i2c->processed == i2c->msg->len) && (cnt != 0)) |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 264 | break; |
| 265 | |
| 266 | if (i2c->processed == 0 && cnt == 0) |
| 267 | byte = (i2c->addr & 0x7f) << 1; |
| 268 | else |
| 269 | byte = i2c->msg->buf[i2c->processed++]; |
| 270 | |
| 271 | val |= byte << (j * 8); |
| 272 | cnt++; |
| 273 | } |
| 274 | |
| 275 | i2c_writel(i2c, val, TXBUFFER_BASE + 4 * i); |
| 276 | |
| 277 | if (i2c->processed == i2c->msg->len) |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | i2c_writel(i2c, cnt, REG_MTXCNT); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /* IRQ handlers for individual states */ |
| 286 | |
| 287 | static void rk3x_i2c_handle_start(struct rk3x_i2c *i2c, unsigned int ipd) |
| 288 | { |
| 289 | if (!(ipd & REG_INT_START)) { |
| 290 | rk3x_i2c_stop(i2c, -EIO); |
| 291 | dev_warn(i2c->dev, "unexpected irq in START: 0x%x\n", ipd); |
| 292 | rk3x_i2c_clean_ipd(i2c); |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | /* ack interrupt */ |
| 297 | i2c_writel(i2c, REG_INT_START, REG_IPD); |
| 298 | |
| 299 | /* disable start bit */ |
| 300 | i2c_writel(i2c, i2c_readl(i2c, REG_CON) & ~REG_CON_START, REG_CON); |
| 301 | |
| 302 | /* enable appropriate interrupts and transition */ |
| 303 | if (i2c->mode == REG_CON_MOD_TX) { |
| 304 | i2c_writel(i2c, REG_INT_MBTF | REG_INT_NAKRCV, REG_IEN); |
| 305 | i2c->state = STATE_WRITE; |
| 306 | rk3x_i2c_fill_transmit_buf(i2c); |
| 307 | } else { |
| 308 | /* in any other case, we are going to be reading. */ |
| 309 | i2c_writel(i2c, REG_INT_MBRF | REG_INT_NAKRCV, REG_IEN); |
| 310 | i2c->state = STATE_READ; |
| 311 | rk3x_i2c_prepare_read(i2c); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | static void rk3x_i2c_handle_write(struct rk3x_i2c *i2c, unsigned int ipd) |
| 316 | { |
| 317 | if (!(ipd & REG_INT_MBTF)) { |
| 318 | rk3x_i2c_stop(i2c, -EIO); |
| 319 | dev_err(i2c->dev, "unexpected irq in WRITE: 0x%x\n", ipd); |
| 320 | rk3x_i2c_clean_ipd(i2c); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | /* ack interrupt */ |
| 325 | i2c_writel(i2c, REG_INT_MBTF, REG_IPD); |
| 326 | |
| 327 | /* are we finished? */ |
| 328 | if (i2c->processed == i2c->msg->len) |
| 329 | rk3x_i2c_stop(i2c, i2c->error); |
| 330 | else |
| 331 | rk3x_i2c_fill_transmit_buf(i2c); |
| 332 | } |
| 333 | |
| 334 | static void rk3x_i2c_handle_read(struct rk3x_i2c *i2c, unsigned int ipd) |
| 335 | { |
| 336 | unsigned int i; |
| 337 | unsigned int len = i2c->msg->len - i2c->processed; |
| 338 | u32 uninitialized_var(val); |
| 339 | u8 byte; |
| 340 | |
| 341 | /* we only care for MBRF here. */ |
| 342 | if (!(ipd & REG_INT_MBRF)) |
| 343 | return; |
| 344 | |
| 345 | /* ack interrupt */ |
| 346 | i2c_writel(i2c, REG_INT_MBRF, REG_IPD); |
| 347 | |
addy ke | 5da4309 | 2014-08-23 02:00:52 +0800 | [diff] [blame] | 348 | /* Can only handle a maximum of 32 bytes at a time */ |
| 349 | if (len > 32) |
| 350 | len = 32; |
| 351 | |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 352 | /* read the data from receive buffer */ |
| 353 | for (i = 0; i < len; ++i) { |
| 354 | if (i % 4 == 0) |
| 355 | val = i2c_readl(i2c, RXBUFFER_BASE + (i / 4) * 4); |
| 356 | |
| 357 | byte = (val >> ((i % 4) * 8)) & 0xff; |
| 358 | i2c->msg->buf[i2c->processed++] = byte; |
| 359 | } |
| 360 | |
| 361 | /* are we finished? */ |
| 362 | if (i2c->processed == i2c->msg->len) |
| 363 | rk3x_i2c_stop(i2c, i2c->error); |
| 364 | else |
| 365 | rk3x_i2c_prepare_read(i2c); |
| 366 | } |
| 367 | |
| 368 | static void rk3x_i2c_handle_stop(struct rk3x_i2c *i2c, unsigned int ipd) |
| 369 | { |
| 370 | unsigned int con; |
| 371 | |
| 372 | if (!(ipd & REG_INT_STOP)) { |
| 373 | rk3x_i2c_stop(i2c, -EIO); |
| 374 | dev_err(i2c->dev, "unexpected irq in STOP: 0x%x\n", ipd); |
| 375 | rk3x_i2c_clean_ipd(i2c); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | /* ack interrupt */ |
| 380 | i2c_writel(i2c, REG_INT_STOP, REG_IPD); |
| 381 | |
| 382 | /* disable STOP bit */ |
| 383 | con = i2c_readl(i2c, REG_CON); |
| 384 | con &= ~REG_CON_STOP; |
| 385 | i2c_writel(i2c, con, REG_CON); |
| 386 | |
| 387 | i2c->busy = false; |
| 388 | i2c->state = STATE_IDLE; |
| 389 | |
| 390 | /* signal rk3x_i2c_xfer that we are finished */ |
| 391 | wake_up(&i2c->wait); |
| 392 | } |
| 393 | |
| 394 | static irqreturn_t rk3x_i2c_irq(int irqno, void *dev_id) |
| 395 | { |
| 396 | struct rk3x_i2c *i2c = dev_id; |
| 397 | unsigned int ipd; |
| 398 | |
| 399 | spin_lock(&i2c->lock); |
| 400 | |
| 401 | ipd = i2c_readl(i2c, REG_IPD); |
| 402 | if (i2c->state == STATE_IDLE) { |
| 403 | dev_warn(i2c->dev, "irq in STATE_IDLE, ipd = 0x%x\n", ipd); |
| 404 | rk3x_i2c_clean_ipd(i2c); |
| 405 | goto out; |
| 406 | } |
| 407 | |
| 408 | dev_dbg(i2c->dev, "IRQ: state %d, ipd: %x\n", i2c->state, ipd); |
| 409 | |
| 410 | /* Clean interrupt bits we don't care about */ |
| 411 | ipd &= ~(REG_INT_BRF | REG_INT_BTF); |
| 412 | |
| 413 | if (ipd & REG_INT_NAKRCV) { |
| 414 | /* |
| 415 | * We got a NACK in the last operation. Depending on whether |
| 416 | * IGNORE_NAK is set, we have to stop the operation and report |
| 417 | * an error. |
| 418 | */ |
| 419 | i2c_writel(i2c, REG_INT_NAKRCV, REG_IPD); |
| 420 | |
| 421 | ipd &= ~REG_INT_NAKRCV; |
| 422 | |
| 423 | if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) |
| 424 | rk3x_i2c_stop(i2c, -ENXIO); |
| 425 | } |
| 426 | |
| 427 | /* is there anything left to handle? */ |
Doug Anderson | 2920933 | 2014-08-22 10:43:44 -0700 | [diff] [blame] | 428 | if ((ipd & REG_INT_ALL) == 0) |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 429 | goto out; |
| 430 | |
| 431 | switch (i2c->state) { |
| 432 | case STATE_START: |
| 433 | rk3x_i2c_handle_start(i2c, ipd); |
| 434 | break; |
| 435 | case STATE_WRITE: |
| 436 | rk3x_i2c_handle_write(i2c, ipd); |
| 437 | break; |
| 438 | case STATE_READ: |
| 439 | rk3x_i2c_handle_read(i2c, ipd); |
| 440 | break; |
| 441 | case STATE_STOP: |
| 442 | rk3x_i2c_handle_stop(i2c, ipd); |
| 443 | break; |
| 444 | case STATE_IDLE: |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | out: |
| 449 | spin_unlock(&i2c->lock); |
| 450 | return IRQ_HANDLED; |
| 451 | } |
| 452 | |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 453 | /** |
| 454 | * Calculate divider values for desired SCL frequency |
| 455 | * |
| 456 | * @clk_rate: I2C input clock rate |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 457 | * @t: Known I2C timing information. |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 458 | * @div_low: Divider output for low |
| 459 | * @div_high: Divider output for high |
| 460 | * |
| 461 | * Returns: 0 on success, -EINVAL if the goal SCL rate is too slow. In that case |
| 462 | * a best-effort divider value is returned in divs. If the target rate is |
| 463 | * too high, we silently use the highest possible rate. |
| 464 | */ |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 465 | static int rk3x_i2c_calc_divs(unsigned long clk_rate, |
| 466 | struct i2c_timings *t, |
| 467 | unsigned long *div_low, |
| 468 | unsigned long *div_high) |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 469 | { |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 470 | unsigned long spec_min_low_ns, spec_min_high_ns; |
Doug Anderson | 387f0de | 2014-12-18 09:44:07 -0800 | [diff] [blame] | 471 | unsigned long spec_setup_start, spec_max_data_hold_ns; |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 472 | unsigned long data_hold_buffer_ns; |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 473 | |
| 474 | unsigned long min_low_ns, min_high_ns; |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 475 | unsigned long max_low_ns, min_total_ns; |
| 476 | |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 477 | unsigned long clk_rate_khz, scl_rate_khz; |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 478 | |
| 479 | unsigned long min_low_div, min_high_div; |
| 480 | unsigned long max_low_div; |
| 481 | |
| 482 | unsigned long min_div_for_hold, min_total_div; |
| 483 | unsigned long extra_div, extra_low_div, ideal_low_div; |
| 484 | |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 485 | int ret = 0; |
| 486 | |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 487 | /* Only support standard-mode and fast-mode */ |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 488 | if (WARN_ON(t->bus_freq_hz > 400000)) |
| 489 | t->bus_freq_hz = 400000; |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 490 | |
| 491 | /* prevent scl_rate_khz from becoming 0 */ |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 492 | if (WARN_ON(t->bus_freq_hz < 1000)) |
| 493 | t->bus_freq_hz = 1000; |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 494 | |
| 495 | /* |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 496 | * min_low_ns: The minimum number of ns we need to hold low to |
| 497 | * meet I2C specification, should include fall time. |
| 498 | * min_high_ns: The minimum number of ns we need to hold high to |
| 499 | * meet I2C specification, should include rise time. |
| 500 | * max_low_ns: The maximum number of ns we can hold low to meet |
| 501 | * I2C specification. |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 502 | * |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 503 | * Note: max_low_ns should be (maximum data hold time * 2 - buffer) |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 504 | * This is because the i2c host on Rockchip holds the data line |
| 505 | * for half the low time. |
| 506 | */ |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 507 | if (t->bus_freq_hz <= 100000) { |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 508 | /* Standard-mode */ |
| 509 | spec_min_low_ns = 4700; |
Doug Anderson | 387f0de | 2014-12-18 09:44:07 -0800 | [diff] [blame] | 510 | spec_setup_start = 4700; |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 511 | spec_min_high_ns = 4000; |
| 512 | spec_max_data_hold_ns = 3450; |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 513 | data_hold_buffer_ns = 50; |
| 514 | } else { |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 515 | /* Fast-mode */ |
| 516 | spec_min_low_ns = 1300; |
Doug Anderson | 387f0de | 2014-12-18 09:44:07 -0800 | [diff] [blame] | 517 | spec_setup_start = 600; |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 518 | spec_min_high_ns = 600; |
| 519 | spec_max_data_hold_ns = 900; |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 520 | data_hold_buffer_ns = 50; |
| 521 | } |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 522 | min_high_ns = t->scl_rise_ns + spec_min_high_ns; |
Doug Anderson | 387f0de | 2014-12-18 09:44:07 -0800 | [diff] [blame] | 523 | |
| 524 | /* |
| 525 | * Timings for repeated start: |
| 526 | * - controller appears to drop SDA at .875x (7/8) programmed clk high. |
| 527 | * - controller appears to keep SCL high for 2x programmed clk high. |
| 528 | * |
| 529 | * We need to account for those rules in picking our "high" time so |
| 530 | * we meet tSU;STA and tHD;STA times. |
| 531 | */ |
| 532 | min_high_ns = max(min_high_ns, |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 533 | DIV_ROUND_UP((t->scl_rise_ns + spec_setup_start) * 1000, 875)); |
Doug Anderson | 387f0de | 2014-12-18 09:44:07 -0800 | [diff] [blame] | 534 | min_high_ns = max(min_high_ns, |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 535 | DIV_ROUND_UP((t->scl_rise_ns + spec_setup_start + |
| 536 | t->sda_fall_ns + spec_min_high_ns), 2)); |
Doug Anderson | 387f0de | 2014-12-18 09:44:07 -0800 | [diff] [blame] | 537 | |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 538 | min_low_ns = t->scl_fall_ns + spec_min_low_ns; |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 539 | max_low_ns = spec_max_data_hold_ns * 2 - data_hold_buffer_ns; |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 540 | min_total_ns = min_low_ns + min_high_ns; |
| 541 | |
| 542 | /* Adjust to avoid overflow */ |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 543 | clk_rate_khz = DIV_ROUND_UP(clk_rate, 1000); |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 544 | scl_rate_khz = t->bus_freq_hz / 1000; |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 545 | |
| 546 | /* |
| 547 | * We need the total div to be >= this number |
| 548 | * so we don't clock too fast. |
| 549 | */ |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 550 | min_total_div = DIV_ROUND_UP(clk_rate_khz, scl_rate_khz * 8); |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 551 | |
| 552 | /* These are the min dividers needed for min hold times. */ |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 553 | min_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, 8 * 1000000); |
| 554 | min_high_div = DIV_ROUND_UP(clk_rate_khz * min_high_ns, 8 * 1000000); |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 555 | min_div_for_hold = (min_low_div + min_high_div); |
| 556 | |
| 557 | /* |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 558 | * This is the maximum divider so we don't go over the maximum. |
| 559 | * We don't round up here (we round down) since this is a maximum. |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 560 | */ |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 561 | max_low_div = clk_rate_khz * max_low_ns / (8 * 1000000); |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 562 | |
| 563 | if (min_low_div > max_low_div) { |
| 564 | WARN_ONCE(true, |
| 565 | "Conflicting, min_low_div %lu, max_low_div %lu\n", |
| 566 | min_low_div, max_low_div); |
| 567 | max_low_div = min_low_div; |
| 568 | } |
| 569 | |
| 570 | if (min_div_for_hold > min_total_div) { |
| 571 | /* |
| 572 | * Time needed to meet hold requirements is important. |
| 573 | * Just use that. |
| 574 | */ |
| 575 | *div_low = min_low_div; |
| 576 | *div_high = min_high_div; |
| 577 | } else { |
| 578 | /* |
| 579 | * We've got to distribute some time among the low and high |
| 580 | * so we don't run too fast. |
| 581 | */ |
| 582 | extra_div = min_total_div - min_div_for_hold; |
| 583 | |
| 584 | /* |
| 585 | * We'll try to split things up perfectly evenly, |
| 586 | * biasing slightly towards having a higher div |
| 587 | * for low (spend more time low). |
| 588 | */ |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 589 | ideal_low_div = DIV_ROUND_UP(clk_rate_khz * min_low_ns, |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 590 | scl_rate_khz * 8 * min_total_ns); |
| 591 | |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 592 | /* Don't allow it to go over the maximum */ |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 593 | if (ideal_low_div > max_low_div) |
| 594 | ideal_low_div = max_low_div; |
| 595 | |
| 596 | /* |
| 597 | * Handle when the ideal low div is going to take up |
| 598 | * more than we have. |
| 599 | */ |
| 600 | if (ideal_low_div > min_low_div + extra_div) |
| 601 | ideal_low_div = min_low_div + extra_div; |
| 602 | |
| 603 | /* Give low the "ideal" and give high whatever extra is left */ |
| 604 | extra_low_div = ideal_low_div - min_low_div; |
| 605 | *div_low = ideal_low_div; |
| 606 | *div_high = min_high_div + (extra_div - extra_low_div); |
| 607 | } |
| 608 | |
| 609 | /* |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 610 | * Adjust to the fact that the hardware has an implicit "+1". |
| 611 | * NOTE: Above calculations always produce div_low > 0 and div_high > 0. |
| 612 | */ |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 613 | *div_low = *div_low - 1; |
| 614 | *div_high = *div_high - 1; |
| 615 | |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 616 | /* Maximum divider supported by hw is 0xffff */ |
| 617 | if (*div_low > 0xffff) { |
| 618 | *div_low = 0xffff; |
| 619 | ret = -EINVAL; |
| 620 | } |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 621 | |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 622 | if (*div_high > 0xffff) { |
| 623 | *div_high = 0xffff; |
| 624 | ret = -EINVAL; |
| 625 | } |
addy ke | 0285f8f | 2014-10-14 14:09:21 +0800 | [diff] [blame] | 626 | |
| 627 | return ret; |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 628 | } |
| 629 | |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 630 | static void rk3x_i2c_adapt_div(struct rk3x_i2c *i2c, unsigned long clk_rate) |
| 631 | { |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 632 | struct i2c_timings *t = &i2c->t; |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 633 | unsigned long div_low, div_high; |
| 634 | u64 t_low_ns, t_high_ns; |
| 635 | int ret; |
| 636 | |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 637 | ret = rk3x_i2c_calc_divs(clk_rate, t, &div_low, &div_high); |
| 638 | WARN_ONCE(ret != 0, "Could not reach SCL freq %u", t->bus_freq_hz); |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 639 | |
| 640 | clk_enable(i2c->clk); |
| 641 | i2c_writel(i2c, (div_high << 16) | (div_low & 0xffff), REG_CLKDIV); |
| 642 | clk_disable(i2c->clk); |
| 643 | |
| 644 | t_low_ns = div_u64(((u64)div_low + 1) * 8 * 1000000000, clk_rate); |
| 645 | t_high_ns = div_u64(((u64)div_high + 1) * 8 * 1000000000, clk_rate); |
| 646 | dev_dbg(i2c->dev, |
| 647 | "CLK %lukhz, Req %uns, Act low %lluns high %lluns\n", |
| 648 | clk_rate / 1000, |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 649 | 1000000000 / t->bus_freq_hz, |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 650 | t_low_ns, t_high_ns); |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * rk3x_i2c_clk_notifier_cb - Clock rate change callback |
| 655 | * @nb: Pointer to notifier block |
| 656 | * @event: Notification reason |
| 657 | * @data: Pointer to notification data object |
| 658 | * |
| 659 | * The callback checks whether a valid bus frequency can be generated after the |
| 660 | * change. If so, the change is acknowledged, otherwise the change is aborted. |
| 661 | * New dividers are written to the HW in the pre- or post change notification |
| 662 | * depending on the scaling direction. |
| 663 | * |
| 664 | * Code adapted from i2c-cadence.c. |
| 665 | * |
| 666 | * Return: NOTIFY_STOP if the rate change should be aborted, NOTIFY_OK |
| 667 | * to acknowedge the change, NOTIFY_DONE if the notification is |
| 668 | * considered irrelevant. |
| 669 | */ |
| 670 | static int rk3x_i2c_clk_notifier_cb(struct notifier_block *nb, unsigned long |
| 671 | event, void *data) |
| 672 | { |
| 673 | struct clk_notifier_data *ndata = data; |
| 674 | struct rk3x_i2c *i2c = container_of(nb, struct rk3x_i2c, clk_rate_nb); |
| 675 | unsigned long div_low, div_high; |
| 676 | |
| 677 | switch (event) { |
| 678 | case PRE_RATE_CHANGE: |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 679 | if (rk3x_i2c_calc_divs(ndata->new_rate, &i2c->t, |
Doug Anderson | 387f0de | 2014-12-18 09:44:07 -0800 | [diff] [blame] | 680 | &div_low, &div_high) != 0) |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 681 | return NOTIFY_STOP; |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 682 | |
| 683 | /* scale up */ |
| 684 | if (ndata->new_rate > ndata->old_rate) |
| 685 | rk3x_i2c_adapt_div(i2c, ndata->new_rate); |
| 686 | |
| 687 | return NOTIFY_OK; |
| 688 | case POST_RATE_CHANGE: |
| 689 | /* scale down */ |
| 690 | if (ndata->new_rate < ndata->old_rate) |
| 691 | rk3x_i2c_adapt_div(i2c, ndata->new_rate); |
| 692 | return NOTIFY_OK; |
| 693 | case ABORT_RATE_CHANGE: |
| 694 | /* scale up */ |
| 695 | if (ndata->new_rate > ndata->old_rate) |
| 696 | rk3x_i2c_adapt_div(i2c, ndata->old_rate); |
| 697 | return NOTIFY_OK; |
| 698 | default: |
| 699 | return NOTIFY_DONE; |
| 700 | } |
| 701 | } |
| 702 | |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 703 | /** |
| 704 | * Setup I2C registers for an I2C operation specified by msgs, num. |
| 705 | * |
| 706 | * Must be called with i2c->lock held. |
| 707 | * |
| 708 | * @msgs: I2C msgs to process |
| 709 | * @num: Number of msgs |
| 710 | * |
| 711 | * returns: Number of I2C msgs processed or negative in case of error |
| 712 | */ |
| 713 | static int rk3x_i2c_setup(struct rk3x_i2c *i2c, struct i2c_msg *msgs, int num) |
| 714 | { |
| 715 | u32 addr = (msgs[0].addr & 0x7f) << 1; |
| 716 | int ret = 0; |
| 717 | |
| 718 | /* |
| 719 | * The I2C adapter can issue a small (len < 4) write packet before |
| 720 | * reading. This speeds up SMBus-style register reads. |
| 721 | * The MRXADDR/MRXRADDR hold the slave address and the slave register |
| 722 | * address in this case. |
| 723 | */ |
| 724 | |
| 725 | if (num >= 2 && msgs[0].len < 4 && |
| 726 | !(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD)) { |
| 727 | u32 reg_addr = 0; |
| 728 | int i; |
| 729 | |
| 730 | dev_dbg(i2c->dev, "Combined write/read from addr 0x%x\n", |
| 731 | addr >> 1); |
| 732 | |
| 733 | /* Fill MRXRADDR with the register address(es) */ |
| 734 | for (i = 0; i < msgs[0].len; ++i) { |
| 735 | reg_addr |= msgs[0].buf[i] << (i * 8); |
| 736 | reg_addr |= REG_MRXADDR_VALID(i); |
| 737 | } |
| 738 | |
| 739 | /* msgs[0] is handled by hw. */ |
| 740 | i2c->msg = &msgs[1]; |
| 741 | |
| 742 | i2c->mode = REG_CON_MOD_REGISTER_TX; |
| 743 | |
| 744 | i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), REG_MRXADDR); |
| 745 | i2c_writel(i2c, reg_addr, REG_MRXRADDR); |
| 746 | |
| 747 | ret = 2; |
| 748 | } else { |
| 749 | /* |
| 750 | * We'll have to do it the boring way and process the msgs |
| 751 | * one-by-one. |
| 752 | */ |
| 753 | |
| 754 | if (msgs[0].flags & I2C_M_RD) { |
| 755 | addr |= 1; /* set read bit */ |
| 756 | |
| 757 | /* |
| 758 | * We have to transmit the slave addr first. Use |
| 759 | * MOD_REGISTER_TX for that purpose. |
| 760 | */ |
| 761 | i2c->mode = REG_CON_MOD_REGISTER_TX; |
| 762 | i2c_writel(i2c, addr | REG_MRXADDR_VALID(0), |
| 763 | REG_MRXADDR); |
| 764 | i2c_writel(i2c, 0, REG_MRXRADDR); |
| 765 | } else { |
| 766 | i2c->mode = REG_CON_MOD_TX; |
| 767 | } |
| 768 | |
| 769 | i2c->msg = &msgs[0]; |
| 770 | |
| 771 | ret = 1; |
| 772 | } |
| 773 | |
| 774 | i2c->addr = msgs[0].addr; |
| 775 | i2c->busy = true; |
| 776 | i2c->state = STATE_START; |
| 777 | i2c->processed = 0; |
| 778 | i2c->error = 0; |
| 779 | |
| 780 | rk3x_i2c_clean_ipd(i2c); |
| 781 | |
| 782 | return ret; |
| 783 | } |
| 784 | |
| 785 | static int rk3x_i2c_xfer(struct i2c_adapter *adap, |
| 786 | struct i2c_msg *msgs, int num) |
| 787 | { |
| 788 | struct rk3x_i2c *i2c = (struct rk3x_i2c *)adap->algo_data; |
| 789 | unsigned long timeout, flags; |
| 790 | int ret = 0; |
| 791 | int i; |
| 792 | |
| 793 | spin_lock_irqsave(&i2c->lock, flags); |
| 794 | |
| 795 | clk_enable(i2c->clk); |
| 796 | |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 797 | i2c->is_last_msg = false; |
| 798 | |
| 799 | /* |
| 800 | * Process msgs. We can handle more than one message at once (see |
| 801 | * rk3x_i2c_setup()). |
| 802 | */ |
| 803 | for (i = 0; i < num; i += ret) { |
| 804 | ret = rk3x_i2c_setup(i2c, msgs + i, num - i); |
| 805 | |
| 806 | if (ret < 0) { |
| 807 | dev_err(i2c->dev, "rk3x_i2c_setup() failed\n"); |
| 808 | break; |
| 809 | } |
| 810 | |
| 811 | if (i + ret >= num) |
| 812 | i2c->is_last_msg = true; |
| 813 | |
| 814 | spin_unlock_irqrestore(&i2c->lock, flags); |
| 815 | |
| 816 | rk3x_i2c_start(i2c); |
| 817 | |
| 818 | timeout = wait_event_timeout(i2c->wait, !i2c->busy, |
| 819 | msecs_to_jiffies(WAIT_TIMEOUT)); |
| 820 | |
| 821 | spin_lock_irqsave(&i2c->lock, flags); |
| 822 | |
| 823 | if (timeout == 0) { |
| 824 | dev_err(i2c->dev, "timeout, ipd: 0x%02x, state: %d\n", |
| 825 | i2c_readl(i2c, REG_IPD), i2c->state); |
| 826 | |
| 827 | /* Force a STOP condition without interrupt */ |
| 828 | i2c_writel(i2c, 0, REG_IEN); |
| 829 | i2c_writel(i2c, REG_CON_EN | REG_CON_STOP, REG_CON); |
| 830 | |
| 831 | i2c->state = STATE_IDLE; |
| 832 | |
| 833 | ret = -ETIMEDOUT; |
| 834 | break; |
| 835 | } |
| 836 | |
| 837 | if (i2c->error) { |
| 838 | ret = i2c->error; |
| 839 | break; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | clk_disable(i2c->clk); |
| 844 | spin_unlock_irqrestore(&i2c->lock, flags); |
| 845 | |
Dmitry Torokhov | c6cbfb9 | 2015-04-20 15:14:47 -0700 | [diff] [blame] | 846 | return ret < 0 ? ret : num; |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | static u32 rk3x_i2c_func(struct i2c_adapter *adap) |
| 850 | { |
| 851 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING; |
| 852 | } |
| 853 | |
| 854 | static const struct i2c_algorithm rk3x_i2c_algorithm = { |
| 855 | .master_xfer = rk3x_i2c_xfer, |
| 856 | .functionality = rk3x_i2c_func, |
| 857 | }; |
| 858 | |
| 859 | static struct rk3x_i2c_soc_data soc_data[3] = { |
| 860 | { .grf_offset = 0x154 }, /* rk3066 */ |
| 861 | { .grf_offset = 0x0a4 }, /* rk3188 */ |
| 862 | { .grf_offset = -1 }, /* no I2C switching needed */ |
| 863 | }; |
| 864 | |
| 865 | static const struct of_device_id rk3x_i2c_match[] = { |
| 866 | { .compatible = "rockchip,rk3066-i2c", .data = (void *)&soc_data[0] }, |
| 867 | { .compatible = "rockchip,rk3188-i2c", .data = (void *)&soc_data[1] }, |
Yakir Yang | b0b6d12 | 2016-03-14 11:09:15 +0800 | [diff] [blame] | 868 | { .compatible = "rockchip,rk3228-i2c", .data = (void *)&soc_data[2] }, |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 869 | { .compatible = "rockchip,rk3288-i2c", .data = (void *)&soc_data[2] }, |
Dan Carpenter | c51bd6a | 2014-06-12 23:56:09 +0200 | [diff] [blame] | 870 | {}, |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 871 | }; |
Luis de Bethencourt | 598cf16 | 2015-10-20 15:16:29 +0100 | [diff] [blame] | 872 | MODULE_DEVICE_TABLE(of, rk3x_i2c_match); |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 873 | |
| 874 | static int rk3x_i2c_probe(struct platform_device *pdev) |
| 875 | { |
| 876 | struct device_node *np = pdev->dev.of_node; |
| 877 | const struct of_device_id *match; |
| 878 | struct rk3x_i2c *i2c; |
| 879 | struct resource *mem; |
| 880 | int ret = 0; |
| 881 | int bus_nr; |
| 882 | u32 value; |
| 883 | int irq; |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 884 | unsigned long clk_rate; |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 885 | |
| 886 | i2c = devm_kzalloc(&pdev->dev, sizeof(struct rk3x_i2c), GFP_KERNEL); |
| 887 | if (!i2c) |
| 888 | return -ENOMEM; |
| 889 | |
| 890 | match = of_match_node(rk3x_i2c_match, np); |
| 891 | i2c->soc_data = (struct rk3x_i2c_soc_data *)match->data; |
| 892 | |
David Wu | 1ab9295 | 2016-03-17 00:57:17 +0800 | [diff] [blame] | 893 | /* use common interface to get I2C timing properties */ |
| 894 | i2c_parse_fw_timings(&pdev->dev, &i2c->t, true); |
addy ke | 1330e29 | 2014-12-11 19:02:40 +0800 | [diff] [blame] | 895 | |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 896 | strlcpy(i2c->adap.name, "rk3x-i2c", sizeof(i2c->adap.name)); |
| 897 | i2c->adap.owner = THIS_MODULE; |
| 898 | i2c->adap.algo = &rk3x_i2c_algorithm; |
| 899 | i2c->adap.retries = 3; |
| 900 | i2c->adap.dev.of_node = np; |
| 901 | i2c->adap.algo_data = i2c; |
| 902 | i2c->adap.dev.parent = &pdev->dev; |
| 903 | |
| 904 | i2c->dev = &pdev->dev; |
| 905 | |
| 906 | spin_lock_init(&i2c->lock); |
| 907 | init_waitqueue_head(&i2c->wait); |
| 908 | |
| 909 | i2c->clk = devm_clk_get(&pdev->dev, NULL); |
| 910 | if (IS_ERR(i2c->clk)) { |
| 911 | dev_err(&pdev->dev, "cannot get clock\n"); |
| 912 | return PTR_ERR(i2c->clk); |
| 913 | } |
| 914 | |
| 915 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 916 | i2c->regs = devm_ioremap_resource(&pdev->dev, mem); |
| 917 | if (IS_ERR(i2c->regs)) |
| 918 | return PTR_ERR(i2c->regs); |
| 919 | |
| 920 | /* Try to set the I2C adapter number from dt */ |
| 921 | bus_nr = of_alias_get_id(np, "i2c"); |
| 922 | |
| 923 | /* |
| 924 | * Switch to new interface if the SoC also offers the old one. |
| 925 | * The control bit is located in the GRF register space. |
| 926 | */ |
| 927 | if (i2c->soc_data->grf_offset >= 0) { |
| 928 | struct regmap *grf; |
| 929 | |
| 930 | grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf"); |
| 931 | if (IS_ERR(grf)) { |
| 932 | dev_err(&pdev->dev, |
| 933 | "rk3x-i2c needs 'rockchip,grf' property\n"); |
| 934 | return PTR_ERR(grf); |
| 935 | } |
| 936 | |
| 937 | if (bus_nr < 0) { |
| 938 | dev_err(&pdev->dev, "rk3x-i2c needs i2cX alias"); |
| 939 | return -EINVAL; |
| 940 | } |
| 941 | |
| 942 | /* 27+i: write mask, 11+i: value */ |
| 943 | value = BIT(27 + bus_nr) | BIT(11 + bus_nr); |
| 944 | |
| 945 | ret = regmap_write(grf, i2c->soc_data->grf_offset, value); |
| 946 | if (ret != 0) { |
| 947 | dev_err(i2c->dev, "Could not write to GRF: %d\n", ret); |
| 948 | return ret; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | /* IRQ setup */ |
| 953 | irq = platform_get_irq(pdev, 0); |
| 954 | if (irq < 0) { |
| 955 | dev_err(&pdev->dev, "cannot find rk3x IRQ\n"); |
| 956 | return irq; |
| 957 | } |
| 958 | |
| 959 | ret = devm_request_irq(&pdev->dev, irq, rk3x_i2c_irq, |
| 960 | 0, dev_name(&pdev->dev), i2c); |
| 961 | if (ret < 0) { |
| 962 | dev_err(&pdev->dev, "cannot request IRQ\n"); |
| 963 | return ret; |
| 964 | } |
| 965 | |
| 966 | platform_set_drvdata(pdev, i2c); |
| 967 | |
| 968 | ret = clk_prepare(i2c->clk); |
| 969 | if (ret < 0) { |
| 970 | dev_err(&pdev->dev, "Could not prepare clock\n"); |
| 971 | return ret; |
| 972 | } |
| 973 | |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 974 | i2c->clk_rate_nb.notifier_call = rk3x_i2c_clk_notifier_cb; |
| 975 | ret = clk_notifier_register(i2c->clk, &i2c->clk_rate_nb); |
| 976 | if (ret != 0) { |
| 977 | dev_err(&pdev->dev, "Unable to register clock notifier\n"); |
| 978 | goto err_clk; |
| 979 | } |
| 980 | |
| 981 | clk_rate = clk_get_rate(i2c->clk); |
| 982 | rk3x_i2c_adapt_div(i2c, clk_rate); |
| 983 | |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 984 | ret = i2c_add_adapter(&i2c->adap); |
| 985 | if (ret < 0) { |
| 986 | dev_err(&pdev->dev, "Could not register adapter\n"); |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 987 | goto err_clk_notifier; |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | dev_info(&pdev->dev, "Initialized RK3xxx I2C bus at %p\n", i2c->regs); |
| 991 | |
| 992 | return 0; |
| 993 | |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 994 | err_clk_notifier: |
| 995 | clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb); |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 996 | err_clk: |
| 997 | clk_unprepare(i2c->clk); |
| 998 | return ret; |
| 999 | } |
| 1000 | |
| 1001 | static int rk3x_i2c_remove(struct platform_device *pdev) |
| 1002 | { |
| 1003 | struct rk3x_i2c *i2c = platform_get_drvdata(pdev); |
| 1004 | |
| 1005 | i2c_del_adapter(&i2c->adap); |
Max Schwarz | 249051f | 2014-11-20 10:26:50 +0100 | [diff] [blame] | 1006 | |
| 1007 | clk_notifier_unregister(i2c->clk, &i2c->clk_rate_nb); |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 1008 | clk_unprepare(i2c->clk); |
| 1009 | |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
| 1013 | static struct platform_driver rk3x_i2c_driver = { |
| 1014 | .probe = rk3x_i2c_probe, |
| 1015 | .remove = rk3x_i2c_remove, |
| 1016 | .driver = { |
Max Schwarz | c41aa3c | 2014-06-11 22:34:37 +0200 | [diff] [blame] | 1017 | .name = "rk3x-i2c", |
| 1018 | .of_match_table = rk3x_i2c_match, |
| 1019 | }, |
| 1020 | }; |
| 1021 | |
| 1022 | module_platform_driver(rk3x_i2c_driver); |
| 1023 | |
| 1024 | MODULE_DESCRIPTION("Rockchip RK3xxx I2C Bus driver"); |
| 1025 | MODULE_AUTHOR("Max Schwarz <max.schwarz@online.de>"); |
| 1026 | MODULE_LICENSE("GPL v2"); |