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