1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* linux/drivers/i2c/busses/i2c-s3c2410.c 3 * 4 * Copyright (C) 2004,2005,2009 Simtec Electronics 5 * Ben Dooks <ben@simtec.co.uk> 6 * 7 * S3C2410 I2C Controller 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 13 #include <linux/i2c.h> 14 #include <linux/init.h> 15 #include <linux/time.h> 16 #include <linux/interrupt.h> 17 #include <linux/delay.h> 18 #include <linux/errno.h> 19 #include <linux/err.h> 20 #include <linux/platform_device.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/clk.h> 23 #include <linux/cpufreq.h> 24 #include <linux/slab.h> 25 #include <linux/io.h> 26 #include <linux/of.h> 27 #include <linux/gpio/consumer.h> 28 #include <linux/pinctrl/consumer.h> 29 #include <linux/mfd/syscon.h> 30 #include <linux/regmap.h> 31 32 #include <asm/irq.h> 33 34 #include <linux/platform_data/i2c-s3c2410.h> 35 36 /* see s3c2410x user guide, v1.1, section 9 (p447) for more info */ 37 38 #define S3C2410_IICCON 0x00 39 #define S3C2410_IICSTAT 0x04 40 #define S3C2410_IICADD 0x08 41 #define S3C2410_IICDS 0x0C 42 #define S3C2440_IICLC 0x10 43 44 #define S3C2410_IICCON_ACKEN (1 << 7) 45 #define S3C2410_IICCON_TXDIV_16 (0 << 6) 46 #define S3C2410_IICCON_TXDIV_512 (1 << 6) 47 #define S3C2410_IICCON_IRQEN (1 << 5) 48 #define S3C2410_IICCON_IRQPEND (1 << 4) 49 #define S3C2410_IICCON_SCALE(x) ((x) & 0xf) 50 #define S3C2410_IICCON_SCALEMASK (0xf) 51 52 #define S3C2410_IICSTAT_MASTER_RX (2 << 6) 53 #define S3C2410_IICSTAT_MASTER_TX (3 << 6) 54 #define S3C2410_IICSTAT_SLAVE_RX (0 << 6) 55 #define S3C2410_IICSTAT_SLAVE_TX (1 << 6) 56 #define S3C2410_IICSTAT_MODEMASK (3 << 6) 57 58 #define S3C2410_IICSTAT_START (1 << 5) 59 #define S3C2410_IICSTAT_BUSBUSY (1 << 5) 60 #define S3C2410_IICSTAT_TXRXEN (1 << 4) 61 #define S3C2410_IICSTAT_ARBITR (1 << 3) 62 #define S3C2410_IICSTAT_ASSLAVE (1 << 2) 63 #define S3C2410_IICSTAT_ADDR0 (1 << 1) 64 #define S3C2410_IICSTAT_LASTBIT (1 << 0) 65 66 #define S3C2410_IICLC_SDA_DELAY0 (0 << 0) 67 #define S3C2410_IICLC_SDA_DELAY5 (1 << 0) 68 #define S3C2410_IICLC_SDA_DELAY10 (2 << 0) 69 #define S3C2410_IICLC_SDA_DELAY15 (3 << 0) 70 #define S3C2410_IICLC_SDA_DELAY_MASK (3 << 0) 71 72 #define S3C2410_IICLC_FILTER_ON (1 << 2) 73 74 /* Treat S3C2410 as baseline hardware, anything else is supported via quirks */ 75 #define QUIRK_S3C2440 (1 << 0) 76 #define QUIRK_HDMIPHY (1 << 1) 77 #define QUIRK_NO_GPIO (1 << 2) 78 #define QUIRK_POLL (1 << 3) 79 #define QUIRK_ATOMIC (1 << 4) 80 81 /* Max time to wait for bus to become idle after a xfer (in us) */ 82 #define S3C2410_IDLE_TIMEOUT 5000 83 84 /* Exynos5 Sysreg offset */ 85 #define EXYNOS5_SYS_I2C_CFG 0x0234 86 87 /* i2c controller state */ 88 enum s3c24xx_i2c_state { 89 STATE_IDLE, 90 STATE_START, 91 STATE_READ, 92 STATE_WRITE, 93 STATE_STOP 94 }; 95 96 struct s3c24xx_i2c { 97 wait_queue_head_t wait; 98 kernel_ulong_t quirks; 99 100 struct i2c_msg *msg; 101 unsigned int msg_num; 102 unsigned int msg_idx; 103 unsigned int msg_ptr; 104 105 unsigned int tx_setup; 106 unsigned int irq; 107 108 enum s3c24xx_i2c_state state; 109 unsigned long clkrate; 110 111 void __iomem *regs; 112 struct clk *clk; 113 struct device *dev; 114 struct i2c_adapter adap; 115 116 struct s3c2410_platform_i2c *pdata; 117 struct gpio_desc *gpios[2]; 118 struct pinctrl *pctrl; 119 struct regmap *sysreg; 120 unsigned int sys_i2c_cfg; 121 }; 122 123 static const struct platform_device_id s3c24xx_driver_ids[] = { 124 { 125 .name = "s3c2410-i2c", 126 .driver_data = 0, 127 }, { 128 .name = "s3c2440-i2c", 129 .driver_data = QUIRK_S3C2440, 130 }, { 131 .name = "s3c2440-hdmiphy-i2c", 132 .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO, 133 }, { } 134 }; 135 MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids); 136 137 static void i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat); 138 139 #ifdef CONFIG_OF 140 static const struct of_device_id s3c24xx_i2c_match[] = { 141 { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 }, 142 { .compatible = "samsung,s3c2440-hdmiphy-i2c", 143 .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) }, 144 { .compatible = "samsung,exynos5-sata-phy-i2c", 145 .data = (void *)(QUIRK_S3C2440 | QUIRK_POLL | QUIRK_NO_GPIO) }, 146 {}, 147 }; 148 MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match); 149 #endif 150 151 /* 152 * Get controller type either from device tree or platform device variant. 153 */ 154 static inline kernel_ulong_t s3c24xx_get_device_quirks(struct platform_device *pdev) 155 { 156 if (pdev->dev.of_node) 157 return (kernel_ulong_t)of_device_get_match_data(&pdev->dev); 158 159 return platform_get_device_id(pdev)->driver_data; 160 } 161 162 /* 163 * Complete the message and wake up the caller, using the given return code, 164 * or zero to mean ok. 165 */ 166 static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret) 167 { 168 dev_dbg(i2c->dev, "master_complete %d\n", ret); 169 170 i2c->msg_ptr = 0; 171 i2c->msg = NULL; 172 i2c->msg_idx++; 173 i2c->msg_num = 0; 174 if (ret) 175 i2c->msg_idx = ret; 176 177 if (!(i2c->quirks & (QUIRK_POLL | QUIRK_ATOMIC))) 178 wake_up(&i2c->wait); 179 } 180 181 static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c) 182 { 183 unsigned long tmp; 184 185 tmp = readl(i2c->regs + S3C2410_IICCON); 186 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 187 } 188 189 static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c) 190 { 191 unsigned long tmp; 192 193 tmp = readl(i2c->regs + S3C2410_IICCON); 194 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 195 } 196 197 /* irq enable/disable functions */ 198 static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c) 199 { 200 unsigned long tmp; 201 202 tmp = readl(i2c->regs + S3C2410_IICCON); 203 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 204 } 205 206 static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c) 207 { 208 unsigned long tmp; 209 210 tmp = readl(i2c->regs + S3C2410_IICCON); 211 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 212 } 213 214 static bool is_ack(struct s3c24xx_i2c *i2c) 215 { 216 int tries; 217 218 for (tries = 50; tries; --tries) { 219 unsigned long tmp = readl(i2c->regs + S3C2410_IICCON); 220 221 if (!(tmp & S3C2410_IICCON_ACKEN)) { 222 /* 223 * Wait a bit for the bus to stabilize, 224 * delay estimated experimentally. 225 */ 226 usleep_range(100, 200); 227 return true; 228 } 229 if (tmp & S3C2410_IICCON_IRQPEND) { 230 if (!(readl(i2c->regs + S3C2410_IICSTAT) 231 & S3C2410_IICSTAT_LASTBIT)) 232 return true; 233 } 234 usleep_range(1000, 2000); 235 } 236 dev_err(i2c->dev, "ack was not received\n"); 237 return false; 238 } 239 240 /* 241 * put the start of a message onto the bus 242 */ 243 static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, 244 struct i2c_msg *msg) 245 { 246 unsigned int addr = (msg->addr & 0x7f) << 1; 247 unsigned long stat; 248 unsigned long iiccon; 249 250 stat = 0; 251 stat |= S3C2410_IICSTAT_TXRXEN; 252 253 if (msg->flags & I2C_M_RD) { 254 stat |= S3C2410_IICSTAT_MASTER_RX; 255 addr |= 1; 256 } else 257 stat |= S3C2410_IICSTAT_MASTER_TX; 258 259 if (msg->flags & I2C_M_REV_DIR_ADDR) 260 addr ^= 1; 261 262 /* todo - check for whether ack wanted or not */ 263 s3c24xx_i2c_enable_ack(i2c); 264 265 iiccon = readl(i2c->regs + S3C2410_IICCON); 266 writel(stat, i2c->regs + S3C2410_IICSTAT); 267 268 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr); 269 writeb(addr, i2c->regs + S3C2410_IICDS); 270 271 /* 272 * delay here to ensure the data byte has gotten onto the bus 273 * before the transaction is started 274 */ 275 ndelay(i2c->tx_setup); 276 277 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon); 278 writel(iiccon, i2c->regs + S3C2410_IICCON); 279 280 stat |= S3C2410_IICSTAT_START; 281 writel(stat, i2c->regs + S3C2410_IICSTAT); 282 } 283 284 static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret) 285 { 286 unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT); 287 288 dev_dbg(i2c->dev, "STOP\n"); 289 290 /* 291 * The datasheet says that the STOP sequence should be: 292 * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP') 293 * 2) I2CCON.4 = 0 - Clear IRQPEND 294 * 3) Wait until the stop condition takes effect. 295 * 4*) I2CSTAT.4 = 0 - Clear TXRXEN 296 * 297 * Where, step "4*" is only for buses with the "HDMIPHY" quirk. 298 * 299 * However, after much experimentation, it appears that: 300 * a) normal buses automatically clear BUSY and transition from 301 * Master->Slave when they complete generating a STOP condition. 302 * Therefore, step (3) can be done in doxfer() by polling I2CCON.4 303 * after starting the STOP generation here. 304 * b) HDMIPHY bus does neither, so there is no way to do step 3. 305 * There is no indication when this bus has finished generating 306 * STOP. 307 * 308 * In fact, we have found that as soon as the IRQPEND bit is cleared in 309 * step 2, the HDMIPHY bus generates the STOP condition, and then 310 * immediately starts transferring another data byte, even though the 311 * bus is supposedly stopped. This is presumably because the bus is 312 * still in "Master" mode, and its BUSY bit is still set. 313 * 314 * To avoid these extra post-STOP transactions on HDMI phy devices, we 315 * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly, 316 * instead of first generating a proper STOP condition. This should 317 * float SDA & SCK terminating the transfer. Subsequent transfers 318 * start with a proper START condition, and proceed normally. 319 * 320 * The HDMIPHY bus is an internal bus that always has exactly two 321 * devices, the host as Master and the HDMIPHY device as the slave. 322 * Skipping the STOP condition has been tested on this bus and works. 323 */ 324 if (i2c->quirks & QUIRK_HDMIPHY) { 325 /* Stop driving the I2C pins */ 326 iicstat &= ~S3C2410_IICSTAT_TXRXEN; 327 } else { 328 /* stop the transfer */ 329 iicstat &= ~S3C2410_IICSTAT_START; 330 } 331 writel(iicstat, i2c->regs + S3C2410_IICSTAT); 332 333 i2c->state = STATE_STOP; 334 335 s3c24xx_i2c_master_complete(i2c, ret); 336 s3c24xx_i2c_disable_irq(i2c); 337 } 338 339 /* 340 * helper functions to determine the current state in the set of 341 * messages we are sending 342 */ 343 344 /* 345 * returns TRUE if the current message is the last in the set 346 */ 347 static inline int is_lastmsg(struct s3c24xx_i2c *i2c) 348 { 349 return i2c->msg_idx >= (i2c->msg_num - 1); 350 } 351 352 /* 353 * returns TRUE if we this is the last byte in the current message 354 */ 355 static inline int is_msglast(struct s3c24xx_i2c *i2c) 356 { 357 /* 358 * msg->len is always 1 for the first byte of smbus block read. 359 * Actual length will be read from slave. More bytes will be 360 * read according to the length then. 361 */ 362 if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1) 363 return 0; 364 365 return i2c->msg_ptr == i2c->msg->len-1; 366 } 367 368 /* 369 * returns TRUE if we reached the end of the current message 370 */ 371 static inline int is_msgend(struct s3c24xx_i2c *i2c) 372 { 373 return i2c->msg_ptr >= i2c->msg->len; 374 } 375 376 /* 377 * process an interrupt and work out what to do 378 */ 379 static void i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat) 380 { 381 unsigned long tmp; 382 unsigned char byte; 383 384 switch (i2c->state) { 385 386 case STATE_IDLE: 387 dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__); 388 goto out; 389 390 case STATE_STOP: 391 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__); 392 s3c24xx_i2c_disable_irq(i2c); 393 goto out_ack; 394 395 case STATE_START: 396 /* 397 * last thing we did was send a start condition on the 398 * bus, or started a new i2c message 399 */ 400 if (iicstat & S3C2410_IICSTAT_LASTBIT && 401 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 402 /* ack was not received... */ 403 dev_dbg(i2c->dev, "ack was not received\n"); 404 s3c24xx_i2c_stop(i2c, -ENXIO); 405 goto out_ack; 406 } 407 408 if (i2c->msg->flags & I2C_M_RD) 409 i2c->state = STATE_READ; 410 else 411 i2c->state = STATE_WRITE; 412 413 /* 414 * Terminate the transfer if there is nothing to do 415 * as this is used by the i2c probe to find devices. 416 */ 417 if (is_lastmsg(i2c) && i2c->msg->len == 0) { 418 s3c24xx_i2c_stop(i2c, 0); 419 goto out_ack; 420 } 421 422 if (i2c->state == STATE_READ) 423 goto prepare_read; 424 425 /* 426 * fall through to the write state, as we will need to 427 * send a byte as well 428 */ 429 fallthrough; 430 case STATE_WRITE: 431 /* 432 * we are writing data to the device... check for the 433 * end of the message, and if so, work out what to do 434 */ 435 if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 436 if (iicstat & S3C2410_IICSTAT_LASTBIT) { 437 dev_dbg(i2c->dev, "WRITE: No Ack\n"); 438 439 s3c24xx_i2c_stop(i2c, -ECONNREFUSED); 440 goto out_ack; 441 } 442 } 443 444 retry_write: 445 446 if (!is_msgend(i2c)) { 447 byte = i2c->msg->buf[i2c->msg_ptr++]; 448 writeb(byte, i2c->regs + S3C2410_IICDS); 449 450 /* 451 * delay after writing the byte to allow the 452 * data setup time on the bus, as writing the 453 * data to the register causes the first bit 454 * to appear on SDA, and SCL will change as 455 * soon as the interrupt is acknowledged 456 */ 457 ndelay(i2c->tx_setup); 458 459 } else if (!is_lastmsg(i2c)) { 460 /* we need to go to the next i2c message */ 461 462 dev_dbg(i2c->dev, "WRITE: Next Message\n"); 463 464 i2c->msg_ptr = 0; 465 i2c->msg_idx++; 466 i2c->msg++; 467 468 /* check to see if we need to do another message */ 469 if (i2c->msg->flags & I2C_M_NOSTART) { 470 471 if (i2c->msg->flags & I2C_M_RD) { 472 /* 473 * cannot do this, the controller 474 * forces us to send a new START 475 * when we change direction 476 */ 477 dev_dbg(i2c->dev, 478 "missing START before write->read\n"); 479 s3c24xx_i2c_stop(i2c, -EINVAL); 480 break; 481 } 482 483 goto retry_write; 484 } else { 485 /* send the new start */ 486 s3c24xx_i2c_message_start(i2c, i2c->msg); 487 i2c->state = STATE_START; 488 } 489 490 } else { 491 /* send stop */ 492 s3c24xx_i2c_stop(i2c, 0); 493 } 494 break; 495 496 case STATE_READ: 497 /* 498 * we have a byte of data in the data register, do 499 * something with it, and then work out whether we are 500 * going to do any more read/write 501 */ 502 byte = readb(i2c->regs + S3C2410_IICDS); 503 i2c->msg->buf[i2c->msg_ptr++] = byte; 504 505 /* Add actual length to read for smbus block read */ 506 if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1) 507 i2c->msg->len += byte; 508 prepare_read: 509 if (is_msglast(i2c)) { 510 /* last byte of buffer */ 511 512 if (is_lastmsg(i2c)) 513 s3c24xx_i2c_disable_ack(i2c); 514 515 } else if (is_msgend(i2c)) { 516 /* 517 * ok, we've read the entire buffer, see if there 518 * is anything else we need to do 519 */ 520 if (is_lastmsg(i2c)) { 521 /* last message, send stop and complete */ 522 dev_dbg(i2c->dev, "READ: Send Stop\n"); 523 524 s3c24xx_i2c_stop(i2c, 0); 525 } else { 526 /* go to the next transfer */ 527 dev_dbg(i2c->dev, "READ: Next Transfer\n"); 528 529 i2c->msg_ptr = 0; 530 i2c->msg_idx++; 531 i2c->msg++; 532 } 533 } 534 535 break; 536 } 537 538 /* acknowlegde the IRQ and get back on with the work */ 539 540 out_ack: 541 tmp = readl(i2c->regs + S3C2410_IICCON); 542 tmp &= ~S3C2410_IICCON_IRQPEND; 543 writel(tmp, i2c->regs + S3C2410_IICCON); 544 out: 545 return; 546 } 547 548 /* 549 * top level IRQ servicing routine 550 */ 551 static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id) 552 { 553 struct s3c24xx_i2c *i2c = dev_id; 554 unsigned long status; 555 unsigned long tmp; 556 557 status = readl(i2c->regs + S3C2410_IICSTAT); 558 559 if (status & S3C2410_IICSTAT_ARBITR) { 560 /* deal with arbitration loss */ 561 dev_err(i2c->dev, "deal with arbitration loss\n"); 562 } 563 564 if (i2c->state == STATE_IDLE) { 565 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n"); 566 567 tmp = readl(i2c->regs + S3C2410_IICCON); 568 tmp &= ~S3C2410_IICCON_IRQPEND; 569 writel(tmp, i2c->regs + S3C2410_IICCON); 570 goto out; 571 } 572 573 /* 574 * pretty much this leaves us with the fact that we've 575 * transmitted or received whatever byte we last sent 576 */ 577 i2c_s3c_irq_nextbyte(i2c, status); 578 579 out: 580 return IRQ_HANDLED; 581 } 582 583 /* 584 * Disable the bus so that we won't get any interrupts from now on, or try 585 * to drive any lines. This is the default state when we don't have 586 * anything to send/receive. 587 * 588 * If there is an event on the bus, or we have a pre-existing event at 589 * kernel boot time, we may not notice the event and the I2C controller 590 * will lock the bus with the I2C clock line low indefinitely. 591 */ 592 static inline void s3c24xx_i2c_disable_bus(struct s3c24xx_i2c *i2c) 593 { 594 unsigned long tmp; 595 596 /* Stop driving the I2C pins */ 597 tmp = readl(i2c->regs + S3C2410_IICSTAT); 598 tmp &= ~S3C2410_IICSTAT_TXRXEN; 599 writel(tmp, i2c->regs + S3C2410_IICSTAT); 600 601 /* We don't expect any interrupts now, and don't want send acks */ 602 tmp = readl(i2c->regs + S3C2410_IICCON); 603 tmp &= ~(S3C2410_IICCON_IRQEN | S3C2410_IICCON_IRQPEND | 604 S3C2410_IICCON_ACKEN); 605 writel(tmp, i2c->regs + S3C2410_IICCON); 606 } 607 608 609 /* 610 * get the i2c bus for a master transaction 611 */ 612 static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c) 613 { 614 unsigned long iicstat; 615 int timeout = 400; 616 617 while (timeout-- > 0) { 618 iicstat = readl(i2c->regs + S3C2410_IICSTAT); 619 620 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY)) 621 return 0; 622 623 msleep(1); 624 } 625 626 return -ETIMEDOUT; 627 } 628 629 /* 630 * wait for the i2c bus to become idle. 631 */ 632 static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c) 633 { 634 unsigned long iicstat; 635 ktime_t start, now; 636 unsigned long delay; 637 int spins; 638 639 /* ensure the stop has been through the bus */ 640 641 dev_dbg(i2c->dev, "waiting for bus idle\n"); 642 643 start = now = ktime_get(); 644 645 /* 646 * Most of the time, the bus is already idle within a few usec of the 647 * end of a transaction. However, really slow i2c devices can stretch 648 * the clock, delaying STOP generation. 649 * 650 * On slower SoCs this typically happens within a very small number of 651 * instructions so busy wait briefly to avoid scheduling overhead. 652 */ 653 spins = 3; 654 iicstat = readl(i2c->regs + S3C2410_IICSTAT); 655 while ((iicstat & S3C2410_IICSTAT_START) && --spins) { 656 cpu_relax(); 657 iicstat = readl(i2c->regs + S3C2410_IICSTAT); 658 } 659 660 /* 661 * If we do get an appreciable delay as a compromise between idle 662 * detection latency for the normal, fast case, and system load in the 663 * slow device case, use an exponential back off in the polling loop, 664 * up to 1/10th of the total timeout, then continue to poll at a 665 * constant rate up to the timeout. 666 */ 667 delay = 1; 668 while ((iicstat & S3C2410_IICSTAT_START) && 669 ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) { 670 usleep_range(delay, 2 * delay); 671 if (delay < S3C2410_IDLE_TIMEOUT / 10) 672 delay <<= 1; 673 now = ktime_get(); 674 iicstat = readl(i2c->regs + S3C2410_IICSTAT); 675 } 676 677 if (iicstat & S3C2410_IICSTAT_START) 678 dev_warn(i2c->dev, "timeout waiting for bus idle\n"); 679 } 680 681 /* 682 * this starts an i2c transfer 683 */ 684 static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, 685 struct i2c_msg *msgs, int num) 686 { 687 long time_left = 0; 688 int ret; 689 690 ret = s3c24xx_i2c_set_master(i2c); 691 if (ret != 0) { 692 dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); 693 ret = -EAGAIN; 694 goto out; 695 } 696 697 i2c->msg = msgs; 698 i2c->msg_num = num; 699 i2c->msg_ptr = 0; 700 i2c->msg_idx = 0; 701 i2c->state = STATE_START; 702 703 s3c24xx_i2c_enable_irq(i2c); 704 s3c24xx_i2c_message_start(i2c, msgs); 705 706 if (i2c->quirks & (QUIRK_POLL | QUIRK_ATOMIC)) { 707 while ((i2c->msg_num != 0) && is_ack(i2c)) { 708 unsigned long stat = readl(i2c->regs + S3C2410_IICSTAT); 709 710 i2c_s3c_irq_nextbyte(i2c, stat); 711 712 stat = readl(i2c->regs + S3C2410_IICSTAT); 713 if (stat & S3C2410_IICSTAT_ARBITR) 714 dev_err(i2c->dev, "deal with arbitration loss\n"); 715 } 716 } else { 717 time_left = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); 718 } 719 720 ret = i2c->msg_idx; 721 722 /* 723 * Having these next two as dev_err() makes life very 724 * noisy when doing an i2cdetect 725 */ 726 if (time_left == 0) 727 dev_dbg(i2c->dev, "timeout\n"); 728 else if (ret != num) 729 dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); 730 731 /* For QUIRK_HDMIPHY, bus is already disabled */ 732 if (i2c->quirks & QUIRK_HDMIPHY) 733 goto out; 734 735 s3c24xx_i2c_wait_idle(i2c); 736 737 s3c24xx_i2c_disable_bus(i2c); 738 739 out: 740 i2c->state = STATE_IDLE; 741 742 return ret; 743 } 744 745 /* 746 * first port of call from the i2c bus code when an message needs 747 * transferring across the i2c bus. 748 */ 749 static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, 750 struct i2c_msg *msgs, int num) 751 { 752 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; 753 int retry; 754 int ret; 755 756 ret = clk_enable(i2c->clk); 757 if (ret) 758 return ret; 759 760 for (retry = 0; retry < adap->retries; retry++) { 761 762 ret = s3c24xx_i2c_doxfer(i2c, msgs, num); 763 764 if (ret != -EAGAIN) { 765 clk_disable(i2c->clk); 766 return ret; 767 } 768 769 dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); 770 771 udelay(100); 772 } 773 774 clk_disable(i2c->clk); 775 return -EREMOTEIO; 776 } 777 778 static int s3c24xx_i2c_xfer_atomic(struct i2c_adapter *adap, 779 struct i2c_msg *msgs, int num) 780 { 781 struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; 782 int ret; 783 784 disable_irq(i2c->irq); 785 i2c->quirks |= QUIRK_ATOMIC; 786 ret = s3c24xx_i2c_xfer(adap, msgs, num); 787 i2c->quirks &= ~QUIRK_ATOMIC; 788 enable_irq(i2c->irq); 789 790 return ret; 791 } 792 793 /* declare our i2c functionality */ 794 static u32 s3c24xx_i2c_func(struct i2c_adapter *adap) 795 { 796 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL_ALL | I2C_FUNC_NOSTART | 797 I2C_FUNC_PROTOCOL_MANGLING; 798 } 799 800 /* i2c bus registration info */ 801 static const struct i2c_algorithm s3c24xx_i2c_algorithm = { 802 .xfer = s3c24xx_i2c_xfer, 803 .xfer_atomic = s3c24xx_i2c_xfer_atomic, 804 .functionality = s3c24xx_i2c_func, 805 }; 806 807 /* 808 * return the divisor settings for a given frequency 809 */ 810 static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted, 811 unsigned int *div1, unsigned int *divs) 812 { 813 unsigned int calc_divs = clkin / wanted; 814 unsigned int calc_div1; 815 816 if (calc_divs > (16*16)) 817 calc_div1 = 512; 818 else 819 calc_div1 = 16; 820 821 calc_divs += calc_div1-1; 822 calc_divs /= calc_div1; 823 824 if (calc_divs == 0) 825 calc_divs = 1; 826 if (calc_divs > 17) 827 calc_divs = 17; 828 829 *divs = calc_divs; 830 *div1 = calc_div1; 831 832 return clkin / (calc_divs * calc_div1); 833 } 834 835 /* 836 * work out a divisor for the user requested frequency setting, 837 * either by the requested frequency, or scanning the acceptable 838 * range of frequencies until something is found 839 */ 840 static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) 841 { 842 struct s3c2410_platform_i2c *pdata = i2c->pdata; 843 unsigned long clkin = clk_get_rate(i2c->clk); 844 unsigned int divs, div1; 845 unsigned long target_frequency; 846 u32 iiccon; 847 int freq; 848 849 i2c->clkrate = clkin; 850 clkin /= 1000; /* clkin now in KHz */ 851 852 dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency); 853 854 target_frequency = pdata->frequency ?: I2C_MAX_STANDARD_MODE_FREQ; 855 856 target_frequency /= 1000; /* Target frequency now in KHz */ 857 858 freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs); 859 860 if (freq > target_frequency) { 861 dev_err(i2c->dev, 862 "Unable to achieve desired frequency %luKHz." \ 863 " Lowest achievable %dKHz\n", target_frequency, freq); 864 return -EINVAL; 865 } 866 867 *got = freq; 868 869 iiccon = readl(i2c->regs + S3C2410_IICCON); 870 iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512); 871 iiccon |= (divs-1); 872 873 if (div1 == 512) 874 iiccon |= S3C2410_IICCON_TXDIV_512; 875 876 if (i2c->quirks & QUIRK_POLL) 877 iiccon |= S3C2410_IICCON_SCALE(2); 878 879 writel(iiccon, i2c->regs + S3C2410_IICCON); 880 881 if (i2c->quirks & QUIRK_S3C2440) { 882 unsigned long sda_delay; 883 884 if (pdata->sda_delay) { 885 sda_delay = clkin * pdata->sda_delay; 886 sda_delay = DIV_ROUND_UP(sda_delay, 1000000); 887 sda_delay = DIV_ROUND_UP(sda_delay, 5); 888 if (sda_delay > 3) 889 sda_delay = 3; 890 sda_delay |= S3C2410_IICLC_FILTER_ON; 891 } else 892 sda_delay = 0; 893 894 dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay); 895 writel(sda_delay, i2c->regs + S3C2440_IICLC); 896 } 897 898 return 0; 899 } 900 901 #ifdef CONFIG_OF 902 static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) 903 { 904 int i; 905 906 if (i2c->quirks & QUIRK_NO_GPIO) 907 return 0; 908 909 for (i = 0; i < 2; i++) { 910 i2c->gpios[i] = devm_gpiod_get_index(i2c->dev, NULL, 911 i, GPIOD_ASIS); 912 if (IS_ERR(i2c->gpios[i])) { 913 dev_err(i2c->dev, "i2c gpio invalid at index %d\n", i); 914 return -EINVAL; 915 } 916 } 917 return 0; 918 } 919 920 #else 921 static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) 922 { 923 return 0; 924 } 925 #endif 926 927 /* 928 * initialise the controller, set the IO lines and frequency 929 */ 930 static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) 931 { 932 struct s3c2410_platform_i2c *pdata; 933 unsigned int freq; 934 935 /* get the plafrom data */ 936 937 pdata = i2c->pdata; 938 939 /* write slave address */ 940 941 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD); 942 943 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); 944 945 writel(0, i2c->regs + S3C2410_IICCON); 946 writel(0, i2c->regs + S3C2410_IICSTAT); 947 948 /* we need to work out the divisors for the clock... */ 949 950 if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { 951 dev_err(i2c->dev, "cannot meet bus frequency required\n"); 952 return -EINVAL; 953 } 954 955 /* todo - check that the i2c lines aren't being dragged anywhere */ 956 957 dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); 958 dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02x\n", 959 readl(i2c->regs + S3C2410_IICCON)); 960 961 return 0; 962 } 963 964 #ifdef CONFIG_OF 965 /* 966 * Parse the device tree node and retreive the platform data. 967 */ 968 static void 969 s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) 970 { 971 struct s3c2410_platform_i2c *pdata = i2c->pdata; 972 int id; 973 974 if (!np) 975 return; 976 977 pdata->bus_num = -1; /* i2c bus number is dynamically assigned */ 978 of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay); 979 of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr); 980 of_property_read_u32(np, "samsung,i2c-max-bus-freq", 981 (u32 *)&pdata->frequency); 982 /* 983 * Exynos5's legacy i2c controller and new high speed i2c 984 * controller have muxed interrupt sources. By default the 985 * interrupts for 4-channel HS-I2C controller are enabled. 986 * If nodes for first four channels of legacy i2c controller 987 * are available then re-configure the interrupts via the 988 * system register. 989 */ 990 id = of_alias_get_id(np, "i2c"); 991 i2c->sysreg = syscon_regmap_lookup_by_phandle(np, 992 "samsung,sysreg-phandle"); 993 if (IS_ERR(i2c->sysreg)) 994 return; 995 996 regmap_update_bits(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, BIT(id), 0); 997 } 998 #else 999 static void 1000 s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) { } 1001 #endif 1002 1003 static int s3c24xx_i2c_probe(struct platform_device *pdev) 1004 { 1005 struct s3c24xx_i2c *i2c; 1006 struct s3c2410_platform_i2c *pdata = NULL; 1007 struct resource *res; 1008 int ret; 1009 1010 if (!pdev->dev.of_node) { 1011 pdata = dev_get_platdata(&pdev->dev); 1012 if (!pdata) { 1013 dev_err(&pdev->dev, "no platform data\n"); 1014 return -EINVAL; 1015 } 1016 } 1017 1018 i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL); 1019 if (!i2c) 1020 return -ENOMEM; 1021 1022 i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 1023 if (!i2c->pdata) 1024 return -ENOMEM; 1025 1026 i2c->quirks = s3c24xx_get_device_quirks(pdev); 1027 i2c->sysreg = ERR_PTR(-ENOENT); 1028 if (pdata) 1029 memcpy(i2c->pdata, pdata, sizeof(*pdata)); 1030 else 1031 s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c); 1032 1033 strscpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); 1034 i2c->adap.owner = THIS_MODULE; 1035 i2c->adap.algo = &s3c24xx_i2c_algorithm; 1036 i2c->adap.retries = 2; 1037 i2c->adap.class = I2C_CLASS_DEPRECATED; 1038 i2c->tx_setup = 50; 1039 1040 init_waitqueue_head(&i2c->wait); 1041 1042 /* find the clock and enable it */ 1043 i2c->dev = &pdev->dev; 1044 i2c->clk = devm_clk_get(&pdev->dev, "i2c"); 1045 if (IS_ERR(i2c->clk)) { 1046 dev_err(&pdev->dev, "cannot get clock\n"); 1047 return -ENOENT; 1048 } 1049 1050 dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk); 1051 1052 /* map the registers */ 1053 i2c->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 1054 if (IS_ERR(i2c->regs)) 1055 return PTR_ERR(i2c->regs); 1056 1057 dev_dbg(&pdev->dev, "registers %p (%p)\n", 1058 i2c->regs, res); 1059 1060 /* setup info block for the i2c core */ 1061 i2c->adap.algo_data = i2c; 1062 i2c->adap.dev.parent = &pdev->dev; 1063 i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev); 1064 1065 /* inititalise the i2c gpio lines */ 1066 if (i2c->pdata->cfg_gpio) 1067 i2c->pdata->cfg_gpio(to_platform_device(i2c->dev)); 1068 else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) 1069 return -EINVAL; 1070 1071 /* initialise the i2c controller */ 1072 ret = clk_prepare_enable(i2c->clk); 1073 if (ret) { 1074 dev_err(&pdev->dev, "I2C clock enable failed\n"); 1075 return ret; 1076 } 1077 1078 ret = s3c24xx_i2c_init(i2c); 1079 clk_disable(i2c->clk); 1080 if (ret != 0) { 1081 dev_err(&pdev->dev, "I2C controller init failed\n"); 1082 clk_unprepare(i2c->clk); 1083 return ret; 1084 } 1085 1086 /* 1087 * find the IRQ for this unit (note, this relies on the init call to 1088 * ensure no current IRQs pending 1089 */ 1090 if (!(i2c->quirks & QUIRK_POLL)) { 1091 i2c->irq = ret = platform_get_irq(pdev, 0); 1092 if (ret < 0) { 1093 clk_unprepare(i2c->clk); 1094 return ret; 1095 } 1096 1097 ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 1098 0, dev_name(&pdev->dev), i2c); 1099 if (ret != 0) { 1100 dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); 1101 clk_unprepare(i2c->clk); 1102 return ret; 1103 } 1104 } 1105 1106 /* 1107 * Note, previous versions of the driver used i2c_add_adapter() 1108 * to add the bus at any number. We now pass the bus number via 1109 * the platform data, so if unset it will now default to always 1110 * being bus 0. 1111 */ 1112 i2c->adap.nr = i2c->pdata->bus_num; 1113 i2c->adap.dev.of_node = pdev->dev.of_node; 1114 1115 platform_set_drvdata(pdev, i2c); 1116 1117 pm_runtime_enable(&pdev->dev); 1118 1119 ret = i2c_add_numbered_adapter(&i2c->adap); 1120 if (ret < 0) { 1121 pm_runtime_disable(&pdev->dev); 1122 clk_unprepare(i2c->clk); 1123 return ret; 1124 } 1125 1126 dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev)); 1127 return 0; 1128 } 1129 1130 static void s3c24xx_i2c_remove(struct platform_device *pdev) 1131 { 1132 struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 1133 1134 clk_unprepare(i2c->clk); 1135 1136 pm_runtime_disable(&pdev->dev); 1137 1138 i2c_del_adapter(&i2c->adap); 1139 } 1140 1141 static int s3c24xx_i2c_suspend_noirq(struct device *dev) 1142 { 1143 struct s3c24xx_i2c *i2c = dev_get_drvdata(dev); 1144 1145 i2c_mark_adapter_suspended(&i2c->adap); 1146 1147 if (!IS_ERR(i2c->sysreg)) 1148 regmap_read(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, &i2c->sys_i2c_cfg); 1149 1150 return 0; 1151 } 1152 1153 static int s3c24xx_i2c_resume_noirq(struct device *dev) 1154 { 1155 struct s3c24xx_i2c *i2c = dev_get_drvdata(dev); 1156 int ret; 1157 1158 if (!IS_ERR(i2c->sysreg)) 1159 regmap_write(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, i2c->sys_i2c_cfg); 1160 1161 ret = clk_enable(i2c->clk); 1162 if (ret) 1163 return ret; 1164 s3c24xx_i2c_init(i2c); 1165 clk_disable(i2c->clk); 1166 i2c_mark_adapter_resumed(&i2c->adap); 1167 1168 return 0; 1169 } 1170 1171 static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = { 1172 NOIRQ_SYSTEM_SLEEP_PM_OPS(s3c24xx_i2c_suspend_noirq, 1173 s3c24xx_i2c_resume_noirq) 1174 }; 1175 1176 static struct platform_driver s3c24xx_i2c_driver = { 1177 .probe = s3c24xx_i2c_probe, 1178 .remove = s3c24xx_i2c_remove, 1179 .id_table = s3c24xx_driver_ids, 1180 .driver = { 1181 .name = "s3c-i2c", 1182 .pm = pm_sleep_ptr(&s3c24xx_i2c_dev_pm_ops), 1183 .of_match_table = of_match_ptr(s3c24xx_i2c_match), 1184 }, 1185 }; 1186 1187 static int __init i2c_adap_s3c_init(void) 1188 { 1189 return platform_driver_register(&s3c24xx_i2c_driver); 1190 } 1191 subsys_initcall(i2c_adap_s3c_init); 1192 1193 static void __exit i2c_adap_s3c_exit(void) 1194 { 1195 platform_driver_unregister(&s3c24xx_i2c_driver); 1196 } 1197 module_exit(i2c_adap_s3c_exit); 1198 1199 MODULE_DESCRIPTION("S3C24XX I2C Bus driver"); 1200 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); 1201 MODULE_LICENSE("GPL"); 1202