Merge branch 'i2c-next-s3c' into i2c-next

Ben Dooks 88f60f62 e355204e

+78 -107
+78 -107
drivers/i2c/busses/i2c-s3c2410.c
··· 35 35 #include <linux/clk.h> 36 36 #include <linux/cpufreq.h> 37 37 38 - #include <mach/hardware.h> 39 38 #include <asm/irq.h> 40 39 #include <asm/io.h> 41 40 42 - #include <mach/regs-gpio.h> 43 41 #include <asm/plat-s3c/regs-iic.h> 44 42 #include <asm/plat-s3c/iic.h> 45 43 ··· 61 63 unsigned int msg_ptr; 62 64 63 65 unsigned int tx_setup; 66 + unsigned int irq; 64 67 65 68 enum s3c24xx_i2c_state state; 66 69 unsigned long clkrate; ··· 69 70 void __iomem *regs; 70 71 struct clk *clk; 71 72 struct device *dev; 72 - struct resource *irq; 73 73 struct resource *ioarea; 74 74 struct i2c_adapter adap; 75 75 ··· 77 79 #endif 78 80 }; 79 81 80 - /* default platform data to use if not supplied in the platform_device 81 - */ 82 - 83 - static struct s3c2410_platform_i2c s3c24xx_i2c_default_platform = { 84 - .flags = 0, 85 - .slave_addr = 0x10, 86 - .bus_freq = 100*1000, 87 - .max_freq = 400*1000, 88 - .sda_delay = S3C2410_IICLC_SDA_DELAY5 | S3C2410_IICLC_FILTER_ON, 89 - }; 82 + /* default platform data removed, dev should always carry data. */ 90 83 91 84 /* s3c24xx_i2c_is2440() 92 85 * ··· 89 100 struct platform_device *pdev = to_platform_device(i2c->dev); 90 101 91 102 return !strcmp(pdev->name, "s3c2440-i2c"); 92 - } 93 - 94 - 95 - /* s3c24xx_i2c_get_platformdata 96 - * 97 - * get the platform data associated with the given device, or return 98 - * the default if there is none 99 - */ 100 - 101 - static inline struct s3c2410_platform_i2c *s3c24xx_i2c_get_platformdata(struct device *dev) 102 - { 103 - if (dev->platform_data != NULL) 104 - return (struct s3c2410_platform_i2c *)dev->platform_data; 105 - 106 - return &s3c24xx_i2c_default_platform; 107 103 } 108 104 109 105 /* s3c24xx_i2c_master_complete ··· 103 129 104 130 i2c->msg_ptr = 0; 105 131 i2c->msg = NULL; 106 - i2c->msg_idx ++; 132 + i2c->msg_idx++; 107 133 i2c->msg_num = 0; 108 134 if (ret) 109 135 i2c->msg_idx = ret; ··· 114 140 static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c) 115 141 { 116 142 unsigned long tmp; 117 - 143 + 118 144 tmp = readl(i2c->regs + S3C2410_IICCON); 119 145 writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 120 - 121 146 } 122 147 123 148 static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c) 124 149 { 125 150 unsigned long tmp; 126 - 151 + 127 152 tmp = readl(i2c->regs + S3C2410_IICCON); 128 153 writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 129 - 130 154 } 131 155 132 156 /* irq enable/disable functions */ ··· 132 160 static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c) 133 161 { 134 162 unsigned long tmp; 135 - 163 + 136 164 tmp = readl(i2c->regs + S3C2410_IICCON); 137 165 writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 138 166 } ··· 140 168 static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c) 141 169 { 142 170 unsigned long tmp; 143 - 171 + 144 172 tmp = readl(i2c->regs + S3C2410_IICCON); 145 173 writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 146 174 } ··· 148 176 149 177 /* s3c24xx_i2c_message_start 150 178 * 151 - * put the start of a message onto the bus 179 + * put the start of a message onto the bus 152 180 */ 153 181 154 - static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, 182 + static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, 155 183 struct i2c_msg *msg) 156 184 { 157 185 unsigned int addr = (msg->addr & 0x7f) << 1; ··· 170 198 if (msg->flags & I2C_M_REV_DIR_ADDR) 171 199 addr ^= 1; 172 200 173 - // todo - check for wether ack wanted or not 201 + /* todo - check for wether ack wanted or not */ 174 202 s3c24xx_i2c_enable_ack(i2c); 175 203 176 204 iiccon = readl(i2c->regs + S3C2410_IICCON); 177 205 writel(stat, i2c->regs + S3C2410_IICSTAT); 178 - 206 + 179 207 dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr); 180 208 writeb(addr, i2c->regs + S3C2410_IICDS); 181 - 209 + 182 210 /* delay here to ensure the data byte has gotten onto the bus 183 211 * before the transaction is started */ 184 212 ··· 186 214 187 215 dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon); 188 216 writel(iiccon, i2c->regs + S3C2410_IICCON); 189 - 190 - stat |= S3C2410_IICSTAT_START; 217 + 218 + stat |= S3C2410_IICSTAT_START; 191 219 writel(stat, i2c->regs + S3C2410_IICSTAT); 192 220 } 193 221 ··· 198 226 dev_dbg(i2c->dev, "STOP\n"); 199 227 200 228 /* stop the transfer */ 201 - iicstat &= ~ S3C2410_IICSTAT_START; 229 + iicstat &= ~S3C2410_IICSTAT_START; 202 230 writel(iicstat, i2c->regs + S3C2410_IICSTAT); 203 - 231 + 204 232 i2c->state = STATE_STOP; 205 - 233 + 206 234 s3c24xx_i2c_master_complete(i2c, ret); 207 235 s3c24xx_i2c_disable_irq(i2c); 208 236 } ··· 212 240 213 241 /* is_lastmsg() 214 242 * 215 - * returns TRUE if the current message is the last in the set 243 + * returns TRUE if the current message is the last in the set 216 244 */ 217 245 218 246 static inline int is_lastmsg(struct s3c24xx_i2c *i2c) ··· 260 288 261 289 case STATE_STOP: 262 290 dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__); 263 - s3c24xx_i2c_disable_irq(i2c); 291 + s3c24xx_i2c_disable_irq(i2c); 264 292 goto out_ack; 265 293 266 294 case STATE_START: 267 295 /* last thing we did was send a start condition on the 268 296 * bus, or started a new i2c message 269 297 */ 270 - 298 + 271 299 if (iicstat & S3C2410_IICSTAT_LASTBIT && 272 300 !(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 273 301 /* ack was not received... */ ··· 293 321 if (i2c->state == STATE_READ) 294 322 goto prepare_read; 295 323 296 - /* fall through to the write state, as we will need to 324 + /* fall through to the write state, as we will need to 297 325 * send a byte as well */ 298 326 299 327 case STATE_WRITE: ··· 310 338 } 311 339 } 312 340 313 - retry_write: 341 + retry_write: 314 342 315 343 if (!is_msgend(i2c)) { 316 344 byte = i2c->msg->buf[i2c->msg_ptr++]; ··· 330 358 dev_dbg(i2c->dev, "WRITE: Next Message\n"); 331 359 332 360 i2c->msg_ptr = 0; 333 - i2c->msg_idx ++; 361 + i2c->msg_idx++; 334 362 i2c->msg++; 335 - 363 + 336 364 /* check to see if we need to do another message */ 337 365 if (i2c->msg->flags & I2C_M_NOSTART) { 338 366 ··· 346 374 347 375 goto retry_write; 348 376 } else { 349 - 350 377 /* send the new start */ 351 378 s3c24xx_i2c_message_start(i2c, i2c->msg); 352 379 i2c->state = STATE_START; ··· 359 388 break; 360 389 361 390 case STATE_READ: 362 - /* we have a byte of data in the data register, do 391 + /* we have a byte of data in the data register, do 363 392 * something with it, and then work out wether we are 364 393 * going to do any more read/write 365 394 */ ··· 367 396 byte = readb(i2c->regs + S3C2410_IICDS); 368 397 i2c->msg->buf[i2c->msg_ptr++] = byte; 369 398 370 - prepare_read: 399 + prepare_read: 371 400 if (is_msglast(i2c)) { 372 401 /* last byte of buffer */ 373 402 374 403 if (is_lastmsg(i2c)) 375 404 s3c24xx_i2c_disable_ack(i2c); 376 - 405 + 377 406 } else if (is_msgend(i2c)) { 378 407 /* ok, we've read the entire buffer, see if there 379 408 * is anything else we need to do */ ··· 399 428 /* acknowlegde the IRQ and get back on with the work */ 400 429 401 430 out_ack: 402 - tmp = readl(i2c->regs + S3C2410_IICCON); 431 + tmp = readl(i2c->regs + S3C2410_IICCON); 403 432 tmp &= ~S3C2410_IICCON_IRQPEND; 404 433 writel(tmp, i2c->regs + S3C2410_IICCON); 405 434 out: ··· 420 449 status = readl(i2c->regs + S3C2410_IICSTAT); 421 450 422 451 if (status & S3C2410_IICSTAT_ARBITR) { 423 - // deal with arbitration loss 452 + /* deal with arbitration loss */ 424 453 dev_err(i2c->dev, "deal with arbitration loss\n"); 425 454 } 426 455 427 456 if (i2c->state == STATE_IDLE) { 428 457 dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n"); 429 458 430 - tmp = readl(i2c->regs + S3C2410_IICCON); 459 + tmp = readl(i2c->regs + S3C2410_IICCON); 431 460 tmp &= ~S3C2410_IICCON_IRQPEND; 432 461 writel(tmp, i2c->regs + S3C2410_IICCON); 433 462 goto out; 434 463 } 435 - 464 + 436 465 /* pretty much this leaves us with the fact that we've 437 466 * transmitted or received whatever byte we last sent */ 438 467 ··· 455 484 456 485 while (timeout-- > 0) { 457 486 iicstat = readl(i2c->regs + S3C2410_IICSTAT); 458 - 487 + 459 488 if (!(iicstat & S3C2410_IICSTAT_BUSBUSY)) 460 489 return 0; 461 490 462 491 msleep(1); 463 492 } 464 - 465 - dev_dbg(i2c->dev, "timeout: GPEDAT is %08x\n", 466 - __raw_readl(S3C2410_GPEDAT)); 467 493 468 494 return -ETIMEDOUT; 469 495 } ··· 470 502 * this starts an i2c transfer 471 503 */ 472 504 473 - static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, struct i2c_msg *msgs, int num) 505 + static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, 506 + struct i2c_msg *msgs, int num) 474 507 { 475 508 unsigned long timeout; 476 509 int ret; ··· 497 528 s3c24xx_i2c_enable_irq(i2c); 498 529 s3c24xx_i2c_message_start(i2c, msgs); 499 530 spin_unlock_irq(&i2c->lock); 500 - 531 + 501 532 timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); 502 533 503 534 ret = i2c->msg_idx; 504 535 505 - /* having these next two as dev_err() makes life very 536 + /* having these next two as dev_err() makes life very 506 537 * noisy when doing an i2cdetect */ 507 538 508 539 if (timeout == 0) ··· 559 590 .functionality = s3c24xx_i2c_func, 560 591 }; 561 592 562 - static struct s3c24xx_i2c s3c24xx_i2c = { 563 - .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_i2c.lock), 564 - .wait = __WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait), 565 - .tx_setup = 50, 566 - .adap = { 567 - .name = "s3c2410-i2c", 568 - .owner = THIS_MODULE, 569 - .algo = &s3c24xx_i2c_algorithm, 570 - .retries = 2, 571 - .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, 572 - }, 573 - }; 574 - 575 593 /* s3c24xx_i2c_calcdivisor 576 594 * 577 595 * return the divisor settings for a given frequency ··· 598 642 { 599 643 int diff = freq - wanted; 600 644 601 - return (diff >= -2 && diff <= 2); 645 + return diff >= -2 && diff <= 2; 602 646 } 603 647 604 648 /* s3c24xx_i2c_clockrate ··· 610 654 611 655 static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) 612 656 { 613 - struct s3c2410_platform_i2c *pdata; 657 + struct s3c2410_platform_i2c *pdata = i2c->dev->platform_data; 614 658 unsigned long clkin = clk_get_rate(i2c->clk); 615 659 unsigned int divs, div1; 616 660 u32 iiccon; ··· 618 662 int start, end; 619 663 620 664 i2c->clkrate = clkin; 621 - 622 - pdata = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent); 623 665 clkin /= 1000; /* clkin now in KHz */ 624 - 666 + 625 667 dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n", 626 668 pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq); 627 669 ··· 727 773 728 774 /* s3c24xx_i2c_init 729 775 * 730 - * initialise the controller, set the IO lines and frequency 776 + * initialise the controller, set the IO lines and frequency 731 777 */ 732 778 733 779 static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) ··· 738 784 739 785 /* get the plafrom data */ 740 786 741 - pdata = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent); 787 + pdata = i2c->dev->platform_data; 742 788 743 789 /* inititalise the gpio */ 744 790 745 - s3c2410_gpio_cfgpin(S3C2410_GPE15, S3C2410_GPE15_IICSDA); 746 - s3c2410_gpio_cfgpin(S3C2410_GPE14, S3C2410_GPE14_IICSCL); 791 + if (pdata->cfg_gpio) 792 + pdata->cfg_gpio(to_platform_device(i2c->dev)); 747 793 748 794 /* write slave address */ 749 - 795 + 750 796 writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD); 751 797 752 798 dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); ··· 784 830 785 831 static int s3c24xx_i2c_probe(struct platform_device *pdev) 786 832 { 787 - struct s3c24xx_i2c *i2c = &s3c24xx_i2c; 833 + struct s3c24xx_i2c *i2c; 788 834 struct s3c2410_platform_i2c *pdata; 789 835 struct resource *res; 790 836 int ret; 791 837 792 - pdata = s3c24xx_i2c_get_platformdata(&pdev->dev); 838 + pdata = pdev->dev.platform_data; 839 + if (!pdata) { 840 + dev_err(&pdev->dev, "no platform data\n"); 841 + return -EINVAL; 842 + } 843 + 844 + i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL); 845 + if (!i2c) { 846 + dev_err(&pdev->dev, "no memory for state\n"); 847 + return -ENOMEM; 848 + } 849 + 850 + strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); 851 + i2c->adap.owner = THIS_MODULE; 852 + i2c->adap.algo = &s3c24xx_i2c_algorithm; 853 + i2c->adap.retries = 2; 854 + i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD; 855 + i2c->tx_setup = 50; 856 + 857 + spin_lock_init(&i2c->lock); 858 + init_waitqueue_head(&i2c->wait); 793 859 794 860 /* find the clock and enable it */ 795 861 ··· 851 877 goto err_ioarea; 852 878 } 853 879 854 - dev_dbg(&pdev->dev, "registers %p (%p, %p)\n", i2c->regs, i2c->ioarea, res); 880 + dev_dbg(&pdev->dev, "registers %p (%p, %p)\n", 881 + i2c->regs, i2c->ioarea, res); 855 882 856 883 /* setup info block for the i2c core */ 857 884 ··· 866 891 goto err_iomap; 867 892 868 893 /* find the IRQ for this unit (note, this relies on the init call to 869 - * ensure no current IRQs pending 894 + * ensure no current IRQs pending 870 895 */ 871 896 872 - res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 873 - if (res == NULL) { 897 + i2c->irq = ret = platform_get_irq(pdev, 0); 898 + if (ret <= 0) { 874 899 dev_err(&pdev->dev, "cannot find IRQ\n"); 875 - ret = -ENOENT; 876 900 goto err_iomap; 877 901 } 878 902 879 - ret = request_irq(res->start, s3c24xx_i2c_irq, IRQF_DISABLED, 880 - pdev->name, i2c); 903 + ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED, 904 + dev_name(&pdev->dev), i2c); 881 905 882 906 if (ret != 0) { 883 - dev_err(&pdev->dev, "cannot claim IRQ\n"); 907 + dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); 884 908 goto err_iomap; 885 909 } 886 - 887 - i2c->irq = res; 888 - 889 - dev_dbg(&pdev->dev, "irq resource %p (%lu)\n", res, 890 - (unsigned long)res->start); 891 910 892 911 ret = s3c24xx_i2c_register_cpufreq(i2c); 893 912 if (ret < 0) { ··· 912 943 s3c24xx_i2c_deregister_cpufreq(i2c); 913 944 914 945 err_irq: 915 - free_irq(i2c->irq->start, i2c); 946 + free_irq(i2c->irq, i2c); 916 947 917 948 err_iomap: 918 949 iounmap(i2c->regs); ··· 926 957 clk_put(i2c->clk); 927 958 928 959 err_noclk: 960 + kfree(i2c); 929 961 return ret; 930 962 } 931 963 ··· 942 972 s3c24xx_i2c_deregister_cpufreq(i2c); 943 973 944 974 i2c_del_adapter(&i2c->adap); 945 - free_irq(i2c->irq->start, i2c); 975 + free_irq(i2c->irq, i2c); 946 976 947 977 clk_disable(i2c->clk); 948 978 clk_put(i2c->clk); ··· 951 981 952 982 release_resource(i2c->ioarea); 953 983 kfree(i2c->ioarea); 984 + kfree(i2c); 954 985 955 986 return 0; 956 987 }