1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* linux/drivers/i2c/busses/i2c-s3c2410.c 31da177e4SLinus Torvalds * 4c564e6aeSDaniel Silverstone * Copyright (C) 2004,2005,2009 Simtec Electronics 51da177e4SLinus Torvalds * Ben Dooks <ben@simtec.co.uk> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * S3C2410 I2C Controller 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds #include <linux/kernel.h> 111da177e4SLinus Torvalds #include <linux/module.h> 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #include <linux/i2c.h> 141da177e4SLinus Torvalds #include <linux/init.h> 151da177e4SLinus Torvalds #include <linux/time.h> 161da177e4SLinus Torvalds #include <linux/interrupt.h> 171da177e4SLinus Torvalds #include <linux/delay.h> 181da177e4SLinus Torvalds #include <linux/errno.h> 191da177e4SLinus Torvalds #include <linux/err.h> 20d052d1beSRussell King #include <linux/platform_device.h> 21c62c3ca5SMark Brown #include <linux/pm_runtime.h> 22f8ce2547SRussell King #include <linux/clk.h> 2361c7cff8SBen Dooks #include <linux/cpufreq.h> 245a0e3ad6STejun Heo #include <linux/slab.h> 2521782180SH Hartley Sweeten #include <linux/io.h> 264edd65e6SSachin Kamat #include <linux/of.h> 27ed7357c9SLinus Walleij #include <linux/gpio/consumer.h> 282693ac69STomasz Figa #include <linux/pinctrl/consumer.h> 29a7750c3eSPankaj Dubey #include <linux/mfd/syscon.h> 30a7750c3eSPankaj Dubey #include <linux/regmap.h> 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #include <asm/irq.h> 331da177e4SLinus Torvalds 34436d42c6SArnd Bergmann #include <linux/platform_data/i2c-s3c2410.h> 351da177e4SLinus Torvalds 36e636602aSHeiko Stübner /* see s3c2410x user guide, v1.1, section 9 (p447) for more info */ 37e636602aSHeiko Stübner 387a6674daSHeiko Stübner #define S3C2410_IICCON 0x00 397a6674daSHeiko Stübner #define S3C2410_IICSTAT 0x04 407a6674daSHeiko Stübner #define S3C2410_IICADD 0x08 417a6674daSHeiko Stübner #define S3C2410_IICDS 0x0C 427a6674daSHeiko Stübner #define S3C2440_IICLC 0x10 43e636602aSHeiko Stübner 44e636602aSHeiko Stübner #define S3C2410_IICCON_ACKEN (1 << 7) 45e636602aSHeiko Stübner #define S3C2410_IICCON_TXDIV_16 (0 << 6) 46e636602aSHeiko Stübner #define S3C2410_IICCON_TXDIV_512 (1 << 6) 47e636602aSHeiko Stübner #define S3C2410_IICCON_IRQEN (1 << 5) 48e636602aSHeiko Stübner #define S3C2410_IICCON_IRQPEND (1 << 4) 497a6674daSHeiko Stübner #define S3C2410_IICCON_SCALE(x) ((x) & 0xf) 50e636602aSHeiko Stübner #define S3C2410_IICCON_SCALEMASK (0xf) 51e636602aSHeiko Stübner 52e636602aSHeiko Stübner #define S3C2410_IICSTAT_MASTER_RX (2 << 6) 53e636602aSHeiko Stübner #define S3C2410_IICSTAT_MASTER_TX (3 << 6) 54e636602aSHeiko Stübner #define S3C2410_IICSTAT_SLAVE_RX (0 << 6) 55e636602aSHeiko Stübner #define S3C2410_IICSTAT_SLAVE_TX (1 << 6) 56e636602aSHeiko Stübner #define S3C2410_IICSTAT_MODEMASK (3 << 6) 57e636602aSHeiko Stübner 58e636602aSHeiko Stübner #define S3C2410_IICSTAT_START (1 << 5) 59e636602aSHeiko Stübner #define S3C2410_IICSTAT_BUSBUSY (1 << 5) 60e636602aSHeiko Stübner #define S3C2410_IICSTAT_TXRXEN (1 << 4) 61e636602aSHeiko Stübner #define S3C2410_IICSTAT_ARBITR (1 << 3) 62e636602aSHeiko Stübner #define S3C2410_IICSTAT_ASSLAVE (1 << 2) 63e636602aSHeiko Stübner #define S3C2410_IICSTAT_ADDR0 (1 << 1) 64e636602aSHeiko Stübner #define S3C2410_IICSTAT_LASTBIT (1 << 0) 65e636602aSHeiko Stübner 66e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY0 (0 << 0) 67e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY5 (1 << 0) 68e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY10 (2 << 0) 69e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY15 (3 << 0) 70e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY_MASK (3 << 0) 71e636602aSHeiko Stübner 72e636602aSHeiko Stübner #define S3C2410_IICLC_FILTER_ON (1 << 2) 73e636602aSHeiko Stübner 7427452498SKarol Lewandowski /* Treat S3C2410 as baseline hardware, anything else is supported via quirks */ 7527452498SKarol Lewandowski #define QUIRK_S3C2440 (1 << 0) 76ec39ef83SKarol Lewandowski #define QUIRK_HDMIPHY (1 << 1) 77ec39ef83SKarol Lewandowski #define QUIRK_NO_GPIO (1 << 2) 78117053f7SVasanth Ananthan #define QUIRK_POLL (1 << 3) 791da177e4SLinus Torvalds 80fe724bf9SDaniel Kurtz /* Max time to wait for bus to become idle after a xfer (in us) */ 81fe724bf9SDaniel Kurtz #define S3C2410_IDLE_TIMEOUT 5000 82fe724bf9SDaniel Kurtz 83a7750c3eSPankaj Dubey /* Exynos5 Sysreg offset */ 84a7750c3eSPankaj Dubey #define EXYNOS5_SYS_I2C_CFG 0x0234 85a7750c3eSPankaj Dubey 8627452498SKarol Lewandowski /* i2c controller state */ 871da177e4SLinus Torvalds enum s3c24xx_i2c_state { 881da177e4SLinus Torvalds STATE_IDLE, 891da177e4SLinus Torvalds STATE_START, 901da177e4SLinus Torvalds STATE_READ, 911da177e4SLinus Torvalds STATE_WRITE, 921da177e4SLinus Torvalds STATE_STOP 931da177e4SLinus Torvalds }; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds struct s3c24xx_i2c { 961da177e4SLinus Torvalds wait_queue_head_t wait; 975f1b1155SPankaj Dubey kernel_ulong_t quirks; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds struct i2c_msg *msg; 1001da177e4SLinus Torvalds unsigned int msg_num; 1011da177e4SLinus Torvalds unsigned int msg_idx; 1021da177e4SLinus Torvalds unsigned int msg_ptr; 1031da177e4SLinus Torvalds 104e00a8cdfSBen Dooks unsigned int tx_setup; 105e0d1ec97SBen Dooks unsigned int irq; 106e00a8cdfSBen Dooks 1071da177e4SLinus Torvalds enum s3c24xx_i2c_state state; 10861c7cff8SBen Dooks unsigned long clkrate; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds void __iomem *regs; 1111da177e4SLinus Torvalds struct clk *clk; 1121da177e4SLinus Torvalds struct device *dev; 1131da177e4SLinus Torvalds struct i2c_adapter adap; 11461c7cff8SBen Dooks 1154fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata; 116ed7357c9SLinus Walleij struct gpio_desc *gpios[2]; 1172693ac69STomasz Figa struct pinctrl *pctrl; 118a7750c3eSPankaj Dubey struct regmap *sysreg; 119a7750c3eSPankaj Dubey unsigned int sys_i2c_cfg; 1201da177e4SLinus Torvalds }; 1211da177e4SLinus Torvalds 122e9a02a3dSKrzysztof Kozlowski static const struct platform_device_id s3c24xx_driver_ids[] = { 1231da177e4SLinus Torvalds { 12427452498SKarol Lewandowski .name = "s3c2410-i2c", 12527452498SKarol Lewandowski .driver_data = 0, 12627452498SKarol Lewandowski }, { 12727452498SKarol Lewandowski .name = "s3c2440-i2c", 12827452498SKarol Lewandowski .driver_data = QUIRK_S3C2440, 129ec39ef83SKarol Lewandowski }, { 130ec39ef83SKarol Lewandowski .name = "s3c2440-hdmiphy-i2c", 131ec39ef83SKarol Lewandowski .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO, 13227452498SKarol Lewandowski }, { }, 13327452498SKarol Lewandowski }; 13427452498SKarol Lewandowski MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids); 1351da177e4SLinus Torvalds 136117053f7SVasanth Ananthan static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat); 137117053f7SVasanth Ananthan 1385a5f5080SThomas Abraham #ifdef CONFIG_OF 13927452498SKarol Lewandowski static const struct of_device_id s3c24xx_i2c_match[] = { 14027452498SKarol Lewandowski { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 }, 14127452498SKarol Lewandowski { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 }, 142ec39ef83SKarol Lewandowski { .compatible = "samsung,s3c2440-hdmiphy-i2c", 143ec39ef83SKarol Lewandowski .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) }, 144117053f7SVasanth Ananthan { .compatible = "samsung,exynos5-sata-phy-i2c", 145117053f7SVasanth Ananthan .data = (void *)(QUIRK_S3C2440 | QUIRK_POLL | QUIRK_NO_GPIO) }, 14627452498SKarol Lewandowski {}, 14727452498SKarol Lewandowski }; 14827452498SKarol Lewandowski MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match); 1495a5f5080SThomas Abraham #endif 1505a5f5080SThomas Abraham 151ec7c34a4SKrzysztof Kozlowski /* 15227452498SKarol Lewandowski * Get controller type either from device tree or platform device variant. 15327452498SKarol Lewandowski */ 1545f1b1155SPankaj Dubey static inline kernel_ulong_t s3c24xx_get_device_quirks(struct platform_device *pdev) 15527452498SKarol Lewandowski { 156af92cca1SKrzysztof Kozlowski if (pdev->dev.of_node) 157af92cca1SKrzysztof Kozlowski return (kernel_ulong_t)of_device_get_match_data(&pdev->dev); 15827452498SKarol Lewandowski 15927452498SKarol Lewandowski return platform_get_device_id(pdev)->driver_data; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 162ec7c34a4SKrzysztof Kozlowski /* 163ec7c34a4SKrzysztof Kozlowski * Complete the message and wake up the caller, using the given return code, 1641da177e4SLinus Torvalds * or zero to mean ok. 1651da177e4SLinus Torvalds */ 1661da177e4SLinus Torvalds static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds dev_dbg(i2c->dev, "master_complete %d\n", ret); 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds i2c->msg_ptr = 0; 1711da177e4SLinus Torvalds i2c->msg = NULL; 1721da177e4SLinus Torvalds i2c->msg_idx++; 1731da177e4SLinus Torvalds i2c->msg_num = 0; 1741da177e4SLinus Torvalds if (ret) 1751da177e4SLinus Torvalds i2c->msg_idx = ret; 1761da177e4SLinus Torvalds 177117053f7SVasanth Ananthan if (!(i2c->quirks & QUIRK_POLL)) 1781da177e4SLinus Torvalds wake_up(&i2c->wait); 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c) 1821da177e4SLinus Torvalds { 1831da177e4SLinus Torvalds unsigned long tmp; 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 1861da177e4SLinus Torvalds writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds unsigned long tmp; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 1941da177e4SLinus Torvalds writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /* irq enable/disable functions */ 1981da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c) 1991da177e4SLinus Torvalds { 2001da177e4SLinus Torvalds unsigned long tmp; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 2031da177e4SLinus Torvalds writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c) 2071da177e4SLinus Torvalds { 2081da177e4SLinus Torvalds unsigned long tmp; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 2111da177e4SLinus Torvalds writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 214117053f7SVasanth Ananthan static bool is_ack(struct s3c24xx_i2c *i2c) 215117053f7SVasanth Ananthan { 216117053f7SVasanth Ananthan int tries; 217117053f7SVasanth Ananthan 218117053f7SVasanth Ananthan for (tries = 50; tries; --tries) { 219117053f7SVasanth Ananthan if (readl(i2c->regs + S3C2410_IICCON) 220117053f7SVasanth Ananthan & S3C2410_IICCON_IRQPEND) { 221117053f7SVasanth Ananthan if (!(readl(i2c->regs + S3C2410_IICSTAT) 222117053f7SVasanth Ananthan & S3C2410_IICSTAT_LASTBIT)) 223117053f7SVasanth Ananthan return true; 224117053f7SVasanth Ananthan } 225117053f7SVasanth Ananthan usleep_range(1000, 2000); 226117053f7SVasanth Ananthan } 2279b13494cSMasanari Iida dev_err(i2c->dev, "ack was not received\n"); 228117053f7SVasanth Ananthan return false; 229117053f7SVasanth Ananthan } 2301da177e4SLinus Torvalds 231ec7c34a4SKrzysztof Kozlowski /* 2321da177e4SLinus Torvalds * put the start of a message onto the bus 2331da177e4SLinus Torvalds */ 2341da177e4SLinus Torvalds static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, 2351da177e4SLinus Torvalds struct i2c_msg *msg) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds unsigned int addr = (msg->addr & 0x7f) << 1; 2381da177e4SLinus Torvalds unsigned long stat; 2391da177e4SLinus Torvalds unsigned long iiccon; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds stat = 0; 2421da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_TXRXEN; 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds if (msg->flags & I2C_M_RD) { 2451da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_MASTER_RX; 2461da177e4SLinus Torvalds addr |= 1; 2471da177e4SLinus Torvalds } else 2481da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_MASTER_TX; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds if (msg->flags & I2C_M_REV_DIR_ADDR) 2511da177e4SLinus Torvalds addr ^= 1; 2521da177e4SLinus Torvalds 25348fc7f7eSAdam Buchbinder /* todo - check for whether ack wanted or not */ 2541da177e4SLinus Torvalds s3c24xx_i2c_enable_ack(i2c); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds iiccon = readl(i2c->regs + S3C2410_IICCON); 2571da177e4SLinus Torvalds writel(stat, i2c->regs + S3C2410_IICSTAT); 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr); 2601da177e4SLinus Torvalds writeb(addr, i2c->regs + S3C2410_IICDS); 2611da177e4SLinus Torvalds 2620915833bSKrzysztof Kozlowski /* 2630915833bSKrzysztof Kozlowski * delay here to ensure the data byte has gotten onto the bus 2640915833bSKrzysztof Kozlowski * before the transaction is started 2650915833bSKrzysztof Kozlowski */ 266e00a8cdfSBen Dooks ndelay(i2c->tx_setup); 267e00a8cdfSBen Dooks 2681da177e4SLinus Torvalds dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon); 2691da177e4SLinus Torvalds writel(iiccon, i2c->regs + S3C2410_IICCON); 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_START; 2721da177e4SLinus Torvalds writel(stat, i2c->regs + S3C2410_IICSTAT); 273117053f7SVasanth Ananthan 274117053f7SVasanth Ananthan if (i2c->quirks & QUIRK_POLL) { 275117053f7SVasanth Ananthan while ((i2c->msg_num != 0) && is_ack(i2c)) { 276117053f7SVasanth Ananthan i2c_s3c_irq_nextbyte(i2c, stat); 277117053f7SVasanth Ananthan stat = readl(i2c->regs + S3C2410_IICSTAT); 278117053f7SVasanth Ananthan 279117053f7SVasanth Ananthan if (stat & S3C2410_IICSTAT_ARBITR) 280117053f7SVasanth Ananthan dev_err(i2c->dev, "deal with arbitration loss\n"); 281117053f7SVasanth Ananthan } 282117053f7SVasanth Ananthan } 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT); 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds dev_dbg(i2c->dev, "STOP\n"); 2901da177e4SLinus Torvalds 2910da2e776SDaniel Kurtz /* 2920da2e776SDaniel Kurtz * The datasheet says that the STOP sequence should be: 2930da2e776SDaniel Kurtz * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP') 2940da2e776SDaniel Kurtz * 2) I2CCON.4 = 0 - Clear IRQPEND 2950da2e776SDaniel Kurtz * 3) Wait until the stop condition takes effect. 2960da2e776SDaniel Kurtz * 4*) I2CSTAT.4 = 0 - Clear TXRXEN 2970da2e776SDaniel Kurtz * 2980da2e776SDaniel Kurtz * Where, step "4*" is only for buses with the "HDMIPHY" quirk. 2990da2e776SDaniel Kurtz * 3000da2e776SDaniel Kurtz * However, after much experimentation, it appears that: 3010da2e776SDaniel Kurtz * a) normal buses automatically clear BUSY and transition from 3020da2e776SDaniel Kurtz * Master->Slave when they complete generating a STOP condition. 3030da2e776SDaniel Kurtz * Therefore, step (3) can be done in doxfer() by polling I2CCON.4 3040da2e776SDaniel Kurtz * after starting the STOP generation here. 3050da2e776SDaniel Kurtz * b) HDMIPHY bus does neither, so there is no way to do step 3. 3060da2e776SDaniel Kurtz * There is no indication when this bus has finished generating 3070da2e776SDaniel Kurtz * STOP. 3080da2e776SDaniel Kurtz * 3090da2e776SDaniel Kurtz * In fact, we have found that as soon as the IRQPEND bit is cleared in 3100da2e776SDaniel Kurtz * step 2, the HDMIPHY bus generates the STOP condition, and then 3110da2e776SDaniel Kurtz * immediately starts transferring another data byte, even though the 3120da2e776SDaniel Kurtz * bus is supposedly stopped. This is presumably because the bus is 3130da2e776SDaniel Kurtz * still in "Master" mode, and its BUSY bit is still set. 3140da2e776SDaniel Kurtz * 3150da2e776SDaniel Kurtz * To avoid these extra post-STOP transactions on HDMI phy devices, we 3160da2e776SDaniel Kurtz * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly, 3170da2e776SDaniel Kurtz * instead of first generating a proper STOP condition. This should 3180da2e776SDaniel Kurtz * float SDA & SCK terminating the transfer. Subsequent transfers 3190da2e776SDaniel Kurtz * start with a proper START condition, and proceed normally. 3200da2e776SDaniel Kurtz * 3210da2e776SDaniel Kurtz * The HDMIPHY bus is an internal bus that always has exactly two 3220da2e776SDaniel Kurtz * devices, the host as Master and the HDMIPHY device as the slave. 3230da2e776SDaniel Kurtz * Skipping the STOP condition has been tested on this bus and works. 3240da2e776SDaniel Kurtz */ 3250da2e776SDaniel Kurtz if (i2c->quirks & QUIRK_HDMIPHY) { 3260da2e776SDaniel Kurtz /* Stop driving the I2C pins */ 3270da2e776SDaniel Kurtz iicstat &= ~S3C2410_IICSTAT_TXRXEN; 3280da2e776SDaniel Kurtz } else { 3291da177e4SLinus Torvalds /* stop the transfer */ 3301da177e4SLinus Torvalds iicstat &= ~S3C2410_IICSTAT_START; 3310da2e776SDaniel Kurtz } 3321da177e4SLinus Torvalds writel(iicstat, i2c->regs + S3C2410_IICSTAT); 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds i2c->state = STATE_STOP; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds s3c24xx_i2c_master_complete(i2c, ret); 3371da177e4SLinus Torvalds s3c24xx_i2c_disable_irq(i2c); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3400915833bSKrzysztof Kozlowski /* 3410915833bSKrzysztof Kozlowski * helper functions to determine the current state in the set of 3420915833bSKrzysztof Kozlowski * messages we are sending 3430915833bSKrzysztof Kozlowski */ 3441da177e4SLinus Torvalds 345ec7c34a4SKrzysztof Kozlowski /* 3461da177e4SLinus Torvalds * returns TRUE if the current message is the last in the set 3471da177e4SLinus Torvalds */ 3481da177e4SLinus Torvalds static inline int is_lastmsg(struct s3c24xx_i2c *i2c) 3491da177e4SLinus Torvalds { 3501da177e4SLinus Torvalds return i2c->msg_idx >= (i2c->msg_num - 1); 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 353ec7c34a4SKrzysztof Kozlowski /* 3541da177e4SLinus Torvalds * returns TRUE if we this is the last byte in the current message 3551da177e4SLinus Torvalds */ 3561da177e4SLinus Torvalds static inline int is_msglast(struct s3c24xx_i2c *i2c) 3571da177e4SLinus Torvalds { 3580915833bSKrzysztof Kozlowski /* 3590915833bSKrzysztof Kozlowski * msg->len is always 1 for the first byte of smbus block read. 36085747311SJaemin Yoo * Actual length will be read from slave. More bytes will be 3610915833bSKrzysztof Kozlowski * read according to the length then. 3620915833bSKrzysztof Kozlowski */ 36385747311SJaemin Yoo if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1) 36485747311SJaemin Yoo return 0; 36585747311SJaemin Yoo 3661da177e4SLinus Torvalds return i2c->msg_ptr == i2c->msg->len-1; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 369ec7c34a4SKrzysztof Kozlowski /* 3701da177e4SLinus Torvalds * returns TRUE if we reached the end of the current message 3711da177e4SLinus Torvalds */ 3721da177e4SLinus Torvalds static inline int is_msgend(struct s3c24xx_i2c *i2c) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds return i2c->msg_ptr >= i2c->msg->len; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 377ec7c34a4SKrzysztof Kozlowski /* 3781da177e4SLinus Torvalds * process an interrupt and work out what to do 3791da177e4SLinus Torvalds */ 38019820510SHuisung Kang static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat) 3811da177e4SLinus Torvalds { 3821da177e4SLinus Torvalds unsigned long tmp; 3831da177e4SLinus Torvalds unsigned char byte; 3841da177e4SLinus Torvalds int ret = 0; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds switch (i2c->state) { 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds case STATE_IDLE: 38908882d20SHarvey Harrison dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__); 3901da177e4SLinus Torvalds goto out; 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds case STATE_STOP: 39308882d20SHarvey Harrison dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__); 3941da177e4SLinus Torvalds s3c24xx_i2c_disable_irq(i2c); 3951da177e4SLinus Torvalds goto out_ack; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds case STATE_START: 3980915833bSKrzysztof Kozlowski /* 3990915833bSKrzysztof Kozlowski * last thing we did was send a start condition on the 4001da177e4SLinus Torvalds * bus, or started a new i2c message 4011da177e4SLinus Torvalds */ 4021da177e4SLinus Torvalds if (iicstat & S3C2410_IICSTAT_LASTBIT && 4031da177e4SLinus Torvalds !(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 4041da177e4SLinus Torvalds /* ack was not received... */ 4051da177e4SLinus Torvalds dev_dbg(i2c->dev, "ack was not received\n"); 40663f5c289SBen Dooks s3c24xx_i2c_stop(i2c, -ENXIO); 4071da177e4SLinus Torvalds goto out_ack; 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_RD) 4111da177e4SLinus Torvalds i2c->state = STATE_READ; 4121da177e4SLinus Torvalds else 4131da177e4SLinus Torvalds i2c->state = STATE_WRITE; 4141da177e4SLinus Torvalds 4150915833bSKrzysztof Kozlowski /* 4160915833bSKrzysztof Kozlowski * Terminate the transfer if there is nothing to do 4170915833bSKrzysztof Kozlowski * as this is used by the i2c probe to find devices. 4180915833bSKrzysztof Kozlowski */ 4191da177e4SLinus Torvalds if (is_lastmsg(i2c) && i2c->msg->len == 0) { 4201da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 4211da177e4SLinus Torvalds goto out_ack; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds if (i2c->state == STATE_READ) 4251da177e4SLinus Torvalds goto prepare_read; 4261da177e4SLinus Torvalds 4270915833bSKrzysztof Kozlowski /* 4280915833bSKrzysztof Kozlowski * fall through to the write state, as we will need to 4290915833bSKrzysztof Kozlowski * send a byte as well 4301da177e4SLinus Torvalds */ 43181eeae43SJoe Perches fallthrough; 4320915833bSKrzysztof Kozlowski case STATE_WRITE: 4330915833bSKrzysztof Kozlowski /* 4340915833bSKrzysztof Kozlowski * we are writing data to the device... check for the 4350915833bSKrzysztof Kozlowski * end of the message, and if so, work out what to do 4360915833bSKrzysztof Kozlowski */ 4372709781bSBen Dooks if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 4382709781bSBen Dooks if (iicstat & S3C2410_IICSTAT_LASTBIT) { 4392709781bSBen Dooks dev_dbg(i2c->dev, "WRITE: No Ack\n"); 4402709781bSBen Dooks 4412709781bSBen Dooks s3c24xx_i2c_stop(i2c, -ECONNREFUSED); 4422709781bSBen Dooks goto out_ack; 4432709781bSBen Dooks } 4442709781bSBen Dooks } 4452709781bSBen Dooks 4461da177e4SLinus Torvalds retry_write: 4472709781bSBen Dooks 4481da177e4SLinus Torvalds if (!is_msgend(i2c)) { 4491da177e4SLinus Torvalds byte = i2c->msg->buf[i2c->msg_ptr++]; 4501da177e4SLinus Torvalds writeb(byte, i2c->regs + S3C2410_IICDS); 4511da177e4SLinus Torvalds 4520915833bSKrzysztof Kozlowski /* 4530915833bSKrzysztof Kozlowski * delay after writing the byte to allow the 454e00a8cdfSBen Dooks * data setup time on the bus, as writing the 455e00a8cdfSBen Dooks * data to the register causes the first bit 456e00a8cdfSBen Dooks * to appear on SDA, and SCL will change as 4570915833bSKrzysztof Kozlowski * soon as the interrupt is acknowledged 4580915833bSKrzysztof Kozlowski */ 459e00a8cdfSBen Dooks ndelay(i2c->tx_setup); 460e00a8cdfSBen Dooks 4611da177e4SLinus Torvalds } else if (!is_lastmsg(i2c)) { 4621da177e4SLinus Torvalds /* we need to go to the next i2c message */ 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds dev_dbg(i2c->dev, "WRITE: Next Message\n"); 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds i2c->msg_ptr = 0; 4671da177e4SLinus Torvalds i2c->msg_idx++; 4681da177e4SLinus Torvalds i2c->msg++; 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds /* check to see if we need to do another message */ 4711da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_NOSTART) { 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_RD) { 4740915833bSKrzysztof Kozlowski /* 4750915833bSKrzysztof Kozlowski * cannot do this, the controller 4761da177e4SLinus Torvalds * forces us to send a new START 4770915833bSKrzysztof Kozlowski * when we change direction 4780915833bSKrzysztof Kozlowski */ 47924990423SKrzysztof Kozlowski dev_dbg(i2c->dev, 48024990423SKrzysztof Kozlowski "missing START before write->read\n"); 4811da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, -EINVAL); 48224990423SKrzysztof Kozlowski break; 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds goto retry_write; 4861da177e4SLinus Torvalds } else { 4871da177e4SLinus Torvalds /* send the new start */ 4881da177e4SLinus Torvalds s3c24xx_i2c_message_start(i2c, i2c->msg); 4891da177e4SLinus Torvalds i2c->state = STATE_START; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds } else { 4931da177e4SLinus Torvalds /* send stop */ 4941da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds break; 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds case STATE_READ: 4990915833bSKrzysztof Kozlowski /* 5000915833bSKrzysztof Kozlowski * we have a byte of data in the data register, do 50148fc7f7eSAdam Buchbinder * something with it, and then work out whether we are 5021da177e4SLinus Torvalds * going to do any more read/write 5031da177e4SLinus Torvalds */ 5041da177e4SLinus Torvalds byte = readb(i2c->regs + S3C2410_IICDS); 5051da177e4SLinus Torvalds i2c->msg->buf[i2c->msg_ptr++] = byte; 5061da177e4SLinus Torvalds 50785747311SJaemin Yoo /* Add actual length to read for smbus block read */ 50885747311SJaemin Yoo if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1) 50985747311SJaemin Yoo i2c->msg->len += byte; 5101da177e4SLinus Torvalds prepare_read: 5111da177e4SLinus Torvalds if (is_msglast(i2c)) { 5121da177e4SLinus Torvalds /* last byte of buffer */ 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds if (is_lastmsg(i2c)) 5151da177e4SLinus Torvalds s3c24xx_i2c_disable_ack(i2c); 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds } else if (is_msgend(i2c)) { 5180915833bSKrzysztof Kozlowski /* 5190915833bSKrzysztof Kozlowski * ok, we've read the entire buffer, see if there 5200915833bSKrzysztof Kozlowski * is anything else we need to do 5210915833bSKrzysztof Kozlowski */ 5221da177e4SLinus Torvalds if (is_lastmsg(i2c)) { 5231da177e4SLinus Torvalds /* last message, send stop and complete */ 5241da177e4SLinus Torvalds dev_dbg(i2c->dev, "READ: Send Stop\n"); 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 5271da177e4SLinus Torvalds } else { 5281da177e4SLinus Torvalds /* go to the next transfer */ 5291da177e4SLinus Torvalds dev_dbg(i2c->dev, "READ: Next Transfer\n"); 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds i2c->msg_ptr = 0; 5321da177e4SLinus Torvalds i2c->msg_idx++; 5331da177e4SLinus Torvalds i2c->msg++; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds break; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* acknowlegde the IRQ and get back on with the work */ 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds out_ack: 5431da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 5441da177e4SLinus Torvalds tmp &= ~S3C2410_IICCON_IRQPEND; 5451da177e4SLinus Torvalds writel(tmp, i2c->regs + S3C2410_IICCON); 5461da177e4SLinus Torvalds out: 5471da177e4SLinus Torvalds return ret; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 550ec7c34a4SKrzysztof Kozlowski /* 5511da177e4SLinus Torvalds * top level IRQ servicing routine 5521da177e4SLinus Torvalds */ 5537d12e780SDavid Howells static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id) 5541da177e4SLinus Torvalds { 5551da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = dev_id; 5561da177e4SLinus Torvalds unsigned long status; 5571da177e4SLinus Torvalds unsigned long tmp; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds status = readl(i2c->regs + S3C2410_IICSTAT); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds if (status & S3C2410_IICSTAT_ARBITR) { 5623d0911bfSBen Dooks /* deal with arbitration loss */ 5631da177e4SLinus Torvalds dev_err(i2c->dev, "deal with arbitration loss\n"); 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds if (i2c->state == STATE_IDLE) { 5671da177e4SLinus Torvalds dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n"); 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 5701da177e4SLinus Torvalds tmp &= ~S3C2410_IICCON_IRQPEND; 5711da177e4SLinus Torvalds writel(tmp, i2c->regs + S3C2410_IICCON); 5721da177e4SLinus Torvalds goto out; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5750915833bSKrzysztof Kozlowski /* 5760915833bSKrzysztof Kozlowski * pretty much this leaves us with the fact that we've 5770915833bSKrzysztof Kozlowski * transmitted or received whatever byte we last sent 5780915833bSKrzysztof Kozlowski */ 57919820510SHuisung Kang i2c_s3c_irq_nextbyte(i2c, status); 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds out: 5821da177e4SLinus Torvalds return IRQ_HANDLED; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 585069a9502SSimon Glass /* 586069a9502SSimon Glass * Disable the bus so that we won't get any interrupts from now on, or try 587069a9502SSimon Glass * to drive any lines. This is the default state when we don't have 588069a9502SSimon Glass * anything to send/receive. 589069a9502SSimon Glass * 590069a9502SSimon Glass * If there is an event on the bus, or we have a pre-existing event at 591069a9502SSimon Glass * kernel boot time, we may not notice the event and the I2C controller 592069a9502SSimon Glass * will lock the bus with the I2C clock line low indefinitely. 593069a9502SSimon Glass */ 594069a9502SSimon Glass static inline void s3c24xx_i2c_disable_bus(struct s3c24xx_i2c *i2c) 595069a9502SSimon Glass { 596069a9502SSimon Glass unsigned long tmp; 597069a9502SSimon Glass 598069a9502SSimon Glass /* Stop driving the I2C pins */ 599069a9502SSimon Glass tmp = readl(i2c->regs + S3C2410_IICSTAT); 600069a9502SSimon Glass tmp &= ~S3C2410_IICSTAT_TXRXEN; 601069a9502SSimon Glass writel(tmp, i2c->regs + S3C2410_IICSTAT); 602069a9502SSimon Glass 603069a9502SSimon Glass /* We don't expect any interrupts now, and don't want send acks */ 604069a9502SSimon Glass tmp = readl(i2c->regs + S3C2410_IICCON); 605069a9502SSimon Glass tmp &= ~(S3C2410_IICCON_IRQEN | S3C2410_IICCON_IRQPEND | 606069a9502SSimon Glass S3C2410_IICCON_ACKEN); 607069a9502SSimon Glass writel(tmp, i2c->regs + S3C2410_IICCON); 608069a9502SSimon Glass } 609069a9502SSimon Glass 6101da177e4SLinus Torvalds 611ec7c34a4SKrzysztof Kozlowski /* 6121da177e4SLinus Torvalds * get the i2c bus for a master transaction 6131da177e4SLinus Torvalds */ 6141da177e4SLinus Torvalds static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds unsigned long iicstat; 6171da177e4SLinus Torvalds int timeout = 400; 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds while (timeout-- > 0) { 6201da177e4SLinus Torvalds iicstat = readl(i2c->regs + S3C2410_IICSTAT); 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds if (!(iicstat & S3C2410_IICSTAT_BUSBUSY)) 6231da177e4SLinus Torvalds return 0; 6241da177e4SLinus Torvalds 6251da177e4SLinus Torvalds msleep(1); 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds return -ETIMEDOUT; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds 631ec7c34a4SKrzysztof Kozlowski /* 632fe724bf9SDaniel Kurtz * wait for the i2c bus to become idle. 633fe724bf9SDaniel Kurtz */ 634fe724bf9SDaniel Kurtz static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c) 635fe724bf9SDaniel Kurtz { 636fe724bf9SDaniel Kurtz unsigned long iicstat; 637fe724bf9SDaniel Kurtz ktime_t start, now; 638fe724bf9SDaniel Kurtz unsigned long delay; 63931f313d9SMark Brown int spins; 640fe724bf9SDaniel Kurtz 641fe724bf9SDaniel Kurtz /* ensure the stop has been through the bus */ 642fe724bf9SDaniel Kurtz 643fe724bf9SDaniel Kurtz dev_dbg(i2c->dev, "waiting for bus idle\n"); 644fe724bf9SDaniel Kurtz 645fe724bf9SDaniel Kurtz start = now = ktime_get(); 646fe724bf9SDaniel Kurtz 647fe724bf9SDaniel Kurtz /* 648fe724bf9SDaniel Kurtz * Most of the time, the bus is already idle within a few usec of the 649fe724bf9SDaniel Kurtz * end of a transaction. However, really slow i2c devices can stretch 650fe724bf9SDaniel Kurtz * the clock, delaying STOP generation. 651fe724bf9SDaniel Kurtz * 65231f313d9SMark Brown * On slower SoCs this typically happens within a very small number of 65331f313d9SMark Brown * instructions so busy wait briefly to avoid scheduling overhead. 654fe724bf9SDaniel Kurtz */ 65531f313d9SMark Brown spins = 3; 656fe724bf9SDaniel Kurtz iicstat = readl(i2c->regs + S3C2410_IICSTAT); 65731f313d9SMark Brown while ((iicstat & S3C2410_IICSTAT_START) && --spins) { 65831f313d9SMark Brown cpu_relax(); 65931f313d9SMark Brown iicstat = readl(i2c->regs + S3C2410_IICSTAT); 66031f313d9SMark Brown } 66131f313d9SMark Brown 66231f313d9SMark Brown /* 66331f313d9SMark Brown * If we do get an appreciable delay as a compromise between idle 66431f313d9SMark Brown * detection latency for the normal, fast case, and system load in the 66531f313d9SMark Brown * slow device case, use an exponential back off in the polling loop, 66631f313d9SMark Brown * up to 1/10th of the total timeout, then continue to poll at a 66731f313d9SMark Brown * constant rate up to the timeout. 66831f313d9SMark Brown */ 669fe724bf9SDaniel Kurtz delay = 1; 670fe724bf9SDaniel Kurtz while ((iicstat & S3C2410_IICSTAT_START) && 671fe724bf9SDaniel Kurtz ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) { 672fe724bf9SDaniel Kurtz usleep_range(delay, 2 * delay); 673fe724bf9SDaniel Kurtz if (delay < S3C2410_IDLE_TIMEOUT / 10) 674fe724bf9SDaniel Kurtz delay <<= 1; 675fe724bf9SDaniel Kurtz now = ktime_get(); 676fe724bf9SDaniel Kurtz iicstat = readl(i2c->regs + S3C2410_IICSTAT); 677fe724bf9SDaniel Kurtz } 678fe724bf9SDaniel Kurtz 679fe724bf9SDaniel Kurtz if (iicstat & S3C2410_IICSTAT_START) 680fe724bf9SDaniel Kurtz dev_warn(i2c->dev, "timeout waiting for bus idle\n"); 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 683ec7c34a4SKrzysztof Kozlowski /* 6841da177e4SLinus Torvalds * this starts an i2c transfer 6851da177e4SLinus Torvalds */ 6863d0911bfSBen Dooks static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, 6873d0911bfSBen Dooks struct i2c_msg *msgs, int num) 6881da177e4SLinus Torvalds { 689fe724bf9SDaniel Kurtz unsigned long timeout; 6901da177e4SLinus Torvalds int ret; 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds ret = s3c24xx_i2c_set_master(i2c); 6931da177e4SLinus Torvalds if (ret != 0) { 6941da177e4SLinus Torvalds dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); 6951da177e4SLinus Torvalds ret = -EAGAIN; 6961da177e4SLinus Torvalds goto out; 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds i2c->msg = msgs; 7001da177e4SLinus Torvalds i2c->msg_num = num; 7011da177e4SLinus Torvalds i2c->msg_ptr = 0; 7021da177e4SLinus Torvalds i2c->msg_idx = 0; 7031da177e4SLinus Torvalds i2c->state = STATE_START; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds s3c24xx_i2c_enable_irq(i2c); 7061da177e4SLinus Torvalds s3c24xx_i2c_message_start(i2c, msgs); 7071da177e4SLinus Torvalds 708117053f7SVasanth Ananthan if (i2c->quirks & QUIRK_POLL) { 709117053f7SVasanth Ananthan ret = i2c->msg_idx; 710117053f7SVasanth Ananthan 711117053f7SVasanth Ananthan if (ret != num) 712117053f7SVasanth Ananthan dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); 713117053f7SVasanth Ananthan 714117053f7SVasanth Ananthan goto out; 715117053f7SVasanth Ananthan } 716117053f7SVasanth Ananthan 7171da177e4SLinus Torvalds timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds ret = i2c->msg_idx; 7201da177e4SLinus Torvalds 7210915833bSKrzysztof Kozlowski /* 7220915833bSKrzysztof Kozlowski * Having these next two as dev_err() makes life very 7230915833bSKrzysztof Kozlowski * noisy when doing an i2cdetect 7240915833bSKrzysztof Kozlowski */ 7251da177e4SLinus Torvalds if (timeout == 0) 7261da177e4SLinus Torvalds dev_dbg(i2c->dev, "timeout\n"); 7271da177e4SLinus Torvalds else if (ret != num) 7281da177e4SLinus Torvalds dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); 7291da177e4SLinus Torvalds 7300da2e776SDaniel Kurtz /* For QUIRK_HDMIPHY, bus is already disabled */ 7310da2e776SDaniel Kurtz if (i2c->quirks & QUIRK_HDMIPHY) 7320da2e776SDaniel Kurtz goto out; 7331da177e4SLinus Torvalds 734fe724bf9SDaniel Kurtz s3c24xx_i2c_wait_idle(i2c); 7351da177e4SLinus Torvalds 736069a9502SSimon Glass s3c24xx_i2c_disable_bus(i2c); 737069a9502SSimon Glass 7381da177e4SLinus Torvalds out: 739069a9502SSimon Glass i2c->state = STATE_IDLE; 740069a9502SSimon Glass 7411da177e4SLinus Torvalds return ret; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds 744ec7c34a4SKrzysztof Kozlowski /* 7451da177e4SLinus Torvalds * first port of call from the i2c bus code when an message needs 74644bbe87eSSteven Cole * transferring across the i2c bus. 7471da177e4SLinus Torvalds */ 7481da177e4SLinus Torvalds static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, 7491da177e4SLinus Torvalds struct i2c_msg *msgs, int num) 7501da177e4SLinus Torvalds { 7511da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; 7521da177e4SLinus Torvalds int retry; 7531da177e4SLinus Torvalds int ret; 7541da177e4SLinus Torvalds 75534e81ad5SPaul Osmialowski ret = clk_enable(i2c->clk); 75634e81ad5SPaul Osmialowski if (ret) 75734e81ad5SPaul Osmialowski return ret; 758d2360b8eSArnaud Patard (Rtp) 7591da177e4SLinus Torvalds for (retry = 0; retry < adap->retries; retry++) { 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds ret = s3c24xx_i2c_doxfer(i2c, msgs, num); 7621da177e4SLinus Torvalds 763d2360b8eSArnaud Patard (Rtp) if (ret != -EAGAIN) { 76434e81ad5SPaul Osmialowski clk_disable(i2c->clk); 7651da177e4SLinus Torvalds return ret; 766d2360b8eSArnaud Patard (Rtp) } 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds udelay(100); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 77334e81ad5SPaul Osmialowski clk_disable(i2c->clk); 7741da177e4SLinus Torvalds return -EREMOTEIO; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds /* declare our i2c functionality */ 7781da177e4SLinus Torvalds static u32 s3c24xx_i2c_func(struct i2c_adapter *adap) 7791da177e4SLinus Torvalds { 78081f19932SWolfram Sang return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL_ALL | I2C_FUNC_NOSTART | 78114674e70SMark Brown I2C_FUNC_PROTOCOL_MANGLING; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds /* i2c bus registration info */ 7858f9082c5SJean Delvare static const struct i2c_algorithm s3c24xx_i2c_algorithm = { 7861da177e4SLinus Torvalds .master_xfer = s3c24xx_i2c_xfer, 7871da177e4SLinus Torvalds .functionality = s3c24xx_i2c_func, 7881da177e4SLinus Torvalds }; 7891da177e4SLinus Torvalds 790ec7c34a4SKrzysztof Kozlowski /* 7911da177e4SLinus Torvalds * return the divisor settings for a given frequency 7921da177e4SLinus Torvalds */ 7931da177e4SLinus Torvalds static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted, 7941da177e4SLinus Torvalds unsigned int *div1, unsigned int *divs) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds unsigned int calc_divs = clkin / wanted; 7971da177e4SLinus Torvalds unsigned int calc_div1; 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds if (calc_divs > (16*16)) 8001da177e4SLinus Torvalds calc_div1 = 512; 8011da177e4SLinus Torvalds else 8021da177e4SLinus Torvalds calc_div1 = 16; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds calc_divs += calc_div1-1; 8051da177e4SLinus Torvalds calc_divs /= calc_div1; 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds if (calc_divs == 0) 8081da177e4SLinus Torvalds calc_divs = 1; 8091da177e4SLinus Torvalds if (calc_divs > 17) 8101da177e4SLinus Torvalds calc_divs = 17; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds *divs = calc_divs; 8131da177e4SLinus Torvalds *div1 = calc_div1; 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds return clkin / (calc_divs * calc_div1); 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds 818ec7c34a4SKrzysztof Kozlowski /* 8191da177e4SLinus Torvalds * work out a divisor for the user requested frequency setting, 8201da177e4SLinus Torvalds * either by the requested frequency, or scanning the acceptable 8211da177e4SLinus Torvalds * range of frequencies until something is found 8221da177e4SLinus Torvalds */ 82361c7cff8SBen Dooks static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) 8241da177e4SLinus Torvalds { 8254fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata = i2c->pdata; 8261da177e4SLinus Torvalds unsigned long clkin = clk_get_rate(i2c->clk); 8271da177e4SLinus Torvalds unsigned int divs, div1; 828c564e6aeSDaniel Silverstone unsigned long target_frequency; 82961c7cff8SBen Dooks u32 iiccon; 8301da177e4SLinus Torvalds int freq; 8311da177e4SLinus Torvalds 83261c7cff8SBen Dooks i2c->clkrate = clkin; 8331da177e4SLinus Torvalds clkin /= 1000; /* clkin now in KHz */ 8341da177e4SLinus Torvalds 835c564e6aeSDaniel Silverstone dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency); 8361da177e4SLinus Torvalds 83790224e64SAndy Shevchenko target_frequency = pdata->frequency ?: I2C_MAX_STANDARD_MODE_FREQ; 8381da177e4SLinus Torvalds 839c564e6aeSDaniel Silverstone target_frequency /= 1000; /* Target frequency now in KHz */ 8401da177e4SLinus Torvalds 841c564e6aeSDaniel Silverstone freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs); 8421da177e4SLinus Torvalds 843c564e6aeSDaniel Silverstone if (freq > target_frequency) { 844c564e6aeSDaniel Silverstone dev_err(i2c->dev, 845c564e6aeSDaniel Silverstone "Unable to achieve desired frequency %luKHz." \ 846c564e6aeSDaniel Silverstone " Lowest achievable %dKHz\n", target_frequency, freq); 8471da177e4SLinus Torvalds return -EINVAL; 848c564e6aeSDaniel Silverstone } 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds *got = freq; 85161c7cff8SBen Dooks 85261c7cff8SBen Dooks iiccon = readl(i2c->regs + S3C2410_IICCON); 85361c7cff8SBen Dooks iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512); 85461c7cff8SBen Dooks iiccon |= (divs-1); 85561c7cff8SBen Dooks 85661c7cff8SBen Dooks if (div1 == 512) 85761c7cff8SBen Dooks iiccon |= S3C2410_IICCON_TXDIV_512; 85861c7cff8SBen Dooks 859117053f7SVasanth Ananthan if (i2c->quirks & QUIRK_POLL) 860117053f7SVasanth Ananthan iiccon |= S3C2410_IICCON_SCALE(2); 861117053f7SVasanth Ananthan 86261c7cff8SBen Dooks writel(iiccon, i2c->regs + S3C2410_IICCON); 86361c7cff8SBen Dooks 86427452498SKarol Lewandowski if (i2c->quirks & QUIRK_S3C2440) { 865a192f715SBen Dooks unsigned long sda_delay; 866a192f715SBen Dooks 867a192f715SBen Dooks if (pdata->sda_delay) { 8687031307aSMyungJoo Ham sda_delay = clkin * pdata->sda_delay; 8697031307aSMyungJoo Ham sda_delay = DIV_ROUND_UP(sda_delay, 1000000); 870a192f715SBen Dooks sda_delay = DIV_ROUND_UP(sda_delay, 5); 871a192f715SBen Dooks if (sda_delay > 3) 872a192f715SBen Dooks sda_delay = 3; 873a192f715SBen Dooks sda_delay |= S3C2410_IICLC_FILTER_ON; 874a192f715SBen Dooks } else 875a192f715SBen Dooks sda_delay = 0; 876a192f715SBen Dooks 877a192f715SBen Dooks dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay); 878a192f715SBen Dooks writel(sda_delay, i2c->regs + S3C2440_IICLC); 879a192f715SBen Dooks } 880a192f715SBen Dooks 8811da177e4SLinus Torvalds return 0; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 8845a5f5080SThomas Abraham #ifdef CONFIG_OF 8855a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) 8865a5f5080SThomas Abraham { 887ed7357c9SLinus Walleij int i; 8885a5f5080SThomas Abraham 889ec39ef83SKarol Lewandowski if (i2c->quirks & QUIRK_NO_GPIO) 890ec39ef83SKarol Lewandowski return 0; 891ec39ef83SKarol Lewandowski 892ed7357c9SLinus Walleij for (i = 0; i < 2; i++) { 893ed7357c9SLinus Walleij i2c->gpios[i] = devm_gpiod_get_index(i2c->dev, NULL, 894ed7357c9SLinus Walleij i, GPIOD_ASIS); 895ed7357c9SLinus Walleij if (IS_ERR(i2c->gpios[i])) { 896ed7357c9SLinus Walleij dev_err(i2c->dev, "i2c gpio invalid at index %d\n", i); 8975a5f5080SThomas Abraham return -EINVAL; 8985a5f5080SThomas Abraham } 8995a5f5080SThomas Abraham } 900ed7357c9SLinus Walleij return 0; 901ed7357c9SLinus Walleij } 902ed7357c9SLinus Walleij 9035a5f5080SThomas Abraham #else 9045a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) 9055a5f5080SThomas Abraham { 9068ebe661dSTushar Behera return 0; 9075a5f5080SThomas Abraham } 9085a5f5080SThomas Abraham #endif 9095a5f5080SThomas Abraham 910ec7c34a4SKrzysztof Kozlowski /* 9111da177e4SLinus Torvalds * initialise the controller, set the IO lines and frequency 9121da177e4SLinus Torvalds */ 9131da177e4SLinus Torvalds static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) 9141da177e4SLinus Torvalds { 9151da177e4SLinus Torvalds struct s3c2410_platform_i2c *pdata; 9161da177e4SLinus Torvalds unsigned int freq; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds /* get the plafrom data */ 9191da177e4SLinus Torvalds 9204fd81eb2SThomas Abraham pdata = i2c->pdata; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds /* write slave address */ 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD); 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); 9271da177e4SLinus Torvalds 928069a9502SSimon Glass writel(0, i2c->regs + S3C2410_IICCON); 929069a9502SSimon Glass writel(0, i2c->regs + S3C2410_IICSTAT); 93061c7cff8SBen Dooks 9311da177e4SLinus Torvalds /* we need to work out the divisors for the clock... */ 9321da177e4SLinus Torvalds 93361c7cff8SBen Dooks if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { 9341da177e4SLinus Torvalds dev_err(i2c->dev, "cannot meet bus frequency required\n"); 9351da177e4SLinus Torvalds return -EINVAL; 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds /* todo - check that the i2c lines aren't being dragged anywhere */ 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); 941069a9502SSimon Glass dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02x\n", 942069a9502SSimon Glass readl(i2c->regs + S3C2410_IICCON)); 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds return 0; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds 9475a5f5080SThomas Abraham #ifdef CONFIG_OF 948ec7c34a4SKrzysztof Kozlowski /* 9495a5f5080SThomas Abraham * Parse the device tree node and retreive the platform data. 9505a5f5080SThomas Abraham */ 9515a5f5080SThomas Abraham static void 9525a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) 9535a5f5080SThomas Abraham { 9545a5f5080SThomas Abraham struct s3c2410_platform_i2c *pdata = i2c->pdata; 955a7750c3eSPankaj Dubey int id; 9565a5f5080SThomas Abraham 9575a5f5080SThomas Abraham if (!np) 9585a5f5080SThomas Abraham return; 9595a5f5080SThomas Abraham 9605a5f5080SThomas Abraham pdata->bus_num = -1; /* i2c bus number is dynamically assigned */ 9615a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay); 9625a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr); 9635a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-max-bus-freq", 9645a5f5080SThomas Abraham (u32 *)&pdata->frequency); 965a7750c3eSPankaj Dubey /* 966a7750c3eSPankaj Dubey * Exynos5's legacy i2c controller and new high speed i2c 967a7750c3eSPankaj Dubey * controller have muxed interrupt sources. By default the 968a7750c3eSPankaj Dubey * interrupts for 4-channel HS-I2C controller are enabled. 969a7750c3eSPankaj Dubey * If nodes for first four channels of legacy i2c controller 970a7750c3eSPankaj Dubey * are available then re-configure the interrupts via the 971a7750c3eSPankaj Dubey * system register. 972a7750c3eSPankaj Dubey */ 973a7750c3eSPankaj Dubey id = of_alias_get_id(np, "i2c"); 974a7750c3eSPankaj Dubey i2c->sysreg = syscon_regmap_lookup_by_phandle(np, 975a7750c3eSPankaj Dubey "samsung,sysreg-phandle"); 976a7750c3eSPankaj Dubey if (IS_ERR(i2c->sysreg)) 977a7750c3eSPankaj Dubey return; 978a7750c3eSPankaj Dubey 979a7750c3eSPankaj Dubey regmap_update_bits(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, BIT(id), 0); 9805a5f5080SThomas Abraham } 9815a5f5080SThomas Abraham #else 9825a5f5080SThomas Abraham static void 9830915833bSKrzysztof Kozlowski s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) { } 9845a5f5080SThomas Abraham #endif 9855a5f5080SThomas Abraham 9863ae5eaecSRussell King static int s3c24xx_i2c_probe(struct platform_device *pdev) 9871da177e4SLinus Torvalds { 988692acbd3SBen Dooks struct s3c24xx_i2c *i2c; 9894fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata = NULL; 9901da177e4SLinus Torvalds struct resource *res; 9911da177e4SLinus Torvalds int ret; 9921da177e4SLinus Torvalds 9935a5f5080SThomas Abraham if (!pdev->dev.of_node) { 9946d4028c6SJingoo Han pdata = dev_get_platdata(&pdev->dev); 9956a039cabSBen Dooks if (!pdata) { 9966a039cabSBen Dooks dev_err(&pdev->dev, "no platform data\n"); 9976a039cabSBen Dooks return -EINVAL; 9986a039cabSBen Dooks } 9995a5f5080SThomas Abraham } 1000399dee23SBen Dooks 10014ea1557fSMark Brown i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL); 100246797a2aSJingoo Han if (!i2c) 1003692acbd3SBen Dooks return -ENOMEM; 1004692acbd3SBen Dooks 10054fd81eb2SThomas Abraham i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 100646797a2aSJingoo Han if (!i2c->pdata) 1007669da30dSTushar Behera return -ENOMEM; 10084fd81eb2SThomas Abraham 100927452498SKarol Lewandowski i2c->quirks = s3c24xx_get_device_quirks(pdev); 10108d487a43SVasily Khoruzhick i2c->sysreg = ERR_PTR(-ENOENT); 10114fd81eb2SThomas Abraham if (pdata) 10124fd81eb2SThomas Abraham memcpy(i2c->pdata, pdata, sizeof(*pdata)); 10135a5f5080SThomas Abraham else 10145a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c); 10154fd81eb2SThomas Abraham 1016ea1558ceSWolfram Sang strscpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); 1017692acbd3SBen Dooks i2c->adap.owner = THIS_MODULE; 1018692acbd3SBen Dooks i2c->adap.algo = &s3c24xx_i2c_algorithm; 1019692acbd3SBen Dooks i2c->adap.retries = 2; 10206031d3dfSWolfram Sang i2c->adap.class = I2C_CLASS_DEPRECATED; 1021692acbd3SBen Dooks i2c->tx_setup = 50; 1022692acbd3SBen Dooks 1023692acbd3SBen Dooks init_waitqueue_head(&i2c->wait); 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* find the clock and enable it */ 10263ae5eaecSRussell King i2c->dev = &pdev->dev; 10272b255b94STushar Behera i2c->clk = devm_clk_get(&pdev->dev, "i2c"); 10281da177e4SLinus Torvalds if (IS_ERR(i2c->clk)) { 10293ae5eaecSRussell King dev_err(&pdev->dev, "cannot get clock\n"); 1030669da30dSTushar Behera return -ENOENT; 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 10333ae5eaecSRussell King dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk); 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds /* map the registers */ 1036*b15eb803SYangtao Li i2c->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 103752caa59eSLinus Torvalds if (IS_ERR(i2c->regs)) 103852caa59eSLinus Torvalds return PTR_ERR(i2c->regs); 10391da177e4SLinus Torvalds 1040a72ad456SMark Brown dev_dbg(&pdev->dev, "registers %p (%p)\n", 1041a72ad456SMark Brown i2c->regs, res); 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds /* setup info block for the i2c core */ 10441da177e4SLinus Torvalds i2c->adap.algo_data = i2c; 10453ae5eaecSRussell King i2c->adap.dev.parent = &pdev->dev; 10462693ac69STomasz Figa i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev); 10472693ac69STomasz Figa 1048658122feSAbhilash Kesavan /* inititalise the i2c gpio lines */ 10490915833bSKrzysztof Kozlowski if (i2c->pdata->cfg_gpio) 1050658122feSAbhilash Kesavan i2c->pdata->cfg_gpio(to_platform_device(i2c->dev)); 10510915833bSKrzysztof Kozlowski else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) 1052d16933b3STushar Behera return -EINVAL; 1053658122feSAbhilash Kesavan 10541da177e4SLinus Torvalds /* initialise the i2c controller */ 1055d16415b2SJavier Martinez Canillas ret = clk_prepare_enable(i2c->clk); 1056d16415b2SJavier Martinez Canillas if (ret) { 1057d16415b2SJavier Martinez Canillas dev_err(&pdev->dev, "I2C clock enable failed\n"); 1058d16415b2SJavier Martinez Canillas return ret; 1059d16415b2SJavier Martinez Canillas } 1060d16415b2SJavier Martinez Canillas 10611da177e4SLinus Torvalds ret = s3c24xx_i2c_init(i2c); 106234e81ad5SPaul Osmialowski clk_disable(i2c->clk); 1063d16933b3STushar Behera if (ret != 0) { 1064d16933b3STushar Behera dev_err(&pdev->dev, "I2C controller init failed\n"); 1065b1b3df2fSKrzysztof Kozlowski clk_unprepare(i2c->clk); 1066d16933b3STushar Behera return ret; 1067d16933b3STushar Behera } 10680915833bSKrzysztof Kozlowski 10690915833bSKrzysztof Kozlowski /* 10700915833bSKrzysztof Kozlowski * find the IRQ for this unit (note, this relies on the init call to 10711da177e4SLinus Torvalds * ensure no current IRQs pending 10721da177e4SLinus Torvalds */ 1073117053f7SVasanth Ananthan if (!(i2c->quirks & QUIRK_POLL)) { 1074e0d1ec97SBen Dooks i2c->irq = ret = platform_get_irq(pdev, 0); 1075d6840a5eSSergey Shtylyov if (ret < 0) { 107634e81ad5SPaul Osmialowski clk_unprepare(i2c->clk); 1077d16933b3STushar Behera return ret; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 10800915833bSKrzysztof Kozlowski ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 10810915833bSKrzysztof Kozlowski 0, dev_name(&pdev->dev), i2c); 10821da177e4SLinus Torvalds if (ret != 0) { 1083e0d1ec97SBen Dooks dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); 108434e81ad5SPaul Osmialowski clk_unprepare(i2c->clk); 1085d16933b3STushar Behera return ret; 10861da177e4SLinus Torvalds } 1087117053f7SVasanth Ananthan } 10881da177e4SLinus Torvalds 10890915833bSKrzysztof Kozlowski /* 10900915833bSKrzysztof Kozlowski * Note, previous versions of the driver used i2c_add_adapter() 1091399dee23SBen Dooks * to add the bus at any number. We now pass the bus number via 1092399dee23SBen Dooks * the platform data, so if unset it will now default to always 1093399dee23SBen Dooks * being bus 0. 1094399dee23SBen Dooks */ 10954fd81eb2SThomas Abraham i2c->adap.nr = i2c->pdata->bus_num; 10965a5f5080SThomas Abraham i2c->adap.dev.of_node = pdev->dev.of_node; 1097399dee23SBen Dooks 1098eadd709fSWolfram Sang platform_set_drvdata(pdev, i2c); 1099eadd709fSWolfram Sang 1100eadd709fSWolfram Sang pm_runtime_enable(&pdev->dev); 1101eadd709fSWolfram Sang 1102399dee23SBen Dooks ret = i2c_add_numbered_adapter(&i2c->adap); 11031da177e4SLinus Torvalds if (ret < 0) { 1104eadd709fSWolfram Sang pm_runtime_disable(&pdev->dev); 110534e81ad5SPaul Osmialowski clk_unprepare(i2c->clk); 1106dc6fea44STushar Behera return ret; 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 110922e965c2SJean Delvare dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev)); 11105b68790cSBen Dooks return 0; 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 1113e190a0c3SUwe Kleine-König static void s3c24xx_i2c_remove(struct platform_device *pdev) 11141da177e4SLinus Torvalds { 11153ae5eaecSRussell King struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 11161da177e4SLinus Torvalds 111734e81ad5SPaul Osmialowski clk_unprepare(i2c->clk); 111834e81ad5SPaul Osmialowski 1119c62c3ca5SMark Brown pm_runtime_disable(&pdev->dev); 1120c62c3ca5SMark Brown 11215b68790cSBen Dooks i2c_del_adapter(&i2c->adap); 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds 11246a6c6189SMagnus Damm static int s3c24xx_i2c_suspend_noirq(struct device *dev) 1125be44f01eSBen Dooks { 11269242e72aSMasahiro Yamada struct s3c24xx_i2c *i2c = dev_get_drvdata(dev); 11276a6c6189SMagnus Damm 11282088716fSWolfram Sang i2c_mark_adapter_suspended(&i2c->adap); 11296a6c6189SMagnus Damm 1130a7750c3eSPankaj Dubey if (!IS_ERR(i2c->sysreg)) 1131a7750c3eSPankaj Dubey regmap_read(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, &i2c->sys_i2c_cfg); 1132a7750c3eSPankaj Dubey 1133be44f01eSBen Dooks return 0; 1134be44f01eSBen Dooks } 1135be44f01eSBen Dooks 1136b19c1959SVincent Palatin static int s3c24xx_i2c_resume_noirq(struct device *dev) 11371da177e4SLinus Torvalds { 11389242e72aSMasahiro Yamada struct s3c24xx_i2c *i2c = dev_get_drvdata(dev); 113934e81ad5SPaul Osmialowski int ret; 11401da177e4SLinus Torvalds 1141a7750c3eSPankaj Dubey if (!IS_ERR(i2c->sysreg)) 1142a7750c3eSPankaj Dubey regmap_write(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, i2c->sys_i2c_cfg); 1143a7750c3eSPankaj Dubey 114434e81ad5SPaul Osmialowski ret = clk_enable(i2c->clk); 114534e81ad5SPaul Osmialowski if (ret) 114634e81ad5SPaul Osmialowski return ret; 11471da177e4SLinus Torvalds s3c24xx_i2c_init(i2c); 114834e81ad5SPaul Osmialowski clk_disable(i2c->clk); 11492088716fSWolfram Sang i2c_mark_adapter_resumed(&i2c->adap); 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds return 0; 11521da177e4SLinus Torvalds } 11531da177e4SLinus Torvalds 115447145210SAlexey Dobriyan static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = { 115567cd4351SPaul Cercueil NOIRQ_SYSTEM_SLEEP_PM_OPS(s3c24xx_i2c_suspend_noirq, 1156f6903783SAxel Lin s3c24xx_i2c_resume_noirq) 11576a6c6189SMagnus Damm }; 11586a6c6189SMagnus Damm 11597d85ccd8SBen Dooks static struct platform_driver s3c24xx_i2c_driver = { 11601da177e4SLinus Torvalds .probe = s3c24xx_i2c_probe, 1161e190a0c3SUwe Kleine-König .remove_new = s3c24xx_i2c_remove, 11627d85ccd8SBen Dooks .id_table = s3c24xx_driver_ids, 11633ae5eaecSRussell King .driver = { 11647d85ccd8SBen Dooks .name = "s3c-i2c", 116567cd4351SPaul Cercueil .pm = pm_sleep_ptr(&s3c24xx_i2c_dev_pm_ops), 11669df7eadfSKarol Lewandowski .of_match_table = of_match_ptr(s3c24xx_i2c_match), 11673ae5eaecSRussell King }, 11681da177e4SLinus Torvalds }; 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds static int __init i2c_adap_s3c_init(void) 11711da177e4SLinus Torvalds { 11727d85ccd8SBen Dooks return platform_driver_register(&s3c24xx_i2c_driver); 11731da177e4SLinus Torvalds } 117418dc83a6SMark Brown subsys_initcall(i2c_adap_s3c_init); 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds static void __exit i2c_adap_s3c_exit(void) 11771da177e4SLinus Torvalds { 11787d85ccd8SBen Dooks platform_driver_unregister(&s3c24xx_i2c_driver); 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds module_exit(i2c_adap_s3c_exit); 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds MODULE_DESCRIPTION("S3C24XX I2C Bus driver"); 1183d790eeb3SJean Delvare MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); 11841da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1185