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