11da177e4SLinus Torvalds /* linux/drivers/i2c/busses/i2c-s3c2410.c 21da177e4SLinus Torvalds * 3c564e6aeSDaniel Silverstone * Copyright (C) 2004,2005,2009 Simtec Electronics 41da177e4SLinus Torvalds * Ben Dooks <ben@simtec.co.uk> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * S3C2410 I2C Controller 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 91da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 101da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 111da177e4SLinus Torvalds * (at your option) any later version. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 141da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 151da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 161da177e4SLinus Torvalds * GNU General Public License for more details. 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <linux/kernel.h> 201da177e4SLinus Torvalds #include <linux/module.h> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds #include <linux/i2c.h> 231da177e4SLinus Torvalds #include <linux/init.h> 241da177e4SLinus Torvalds #include <linux/time.h> 251da177e4SLinus Torvalds #include <linux/interrupt.h> 261da177e4SLinus Torvalds #include <linux/delay.h> 271da177e4SLinus Torvalds #include <linux/errno.h> 281da177e4SLinus Torvalds #include <linux/err.h> 29d052d1beSRussell King #include <linux/platform_device.h> 30c62c3ca5SMark Brown #include <linux/pm_runtime.h> 31f8ce2547SRussell King #include <linux/clk.h> 3261c7cff8SBen Dooks #include <linux/cpufreq.h> 335a0e3ad6STejun Heo #include <linux/slab.h> 3421782180SH Hartley Sweeten #include <linux/io.h> 354edd65e6SSachin Kamat #include <linux/of.h> 365a5f5080SThomas Abraham #include <linux/of_gpio.h> 372693ac69STomasz Figa #include <linux/pinctrl/consumer.h> 38a7750c3eSPankaj Dubey #include <linux/mfd/syscon.h> 39a7750c3eSPankaj Dubey #include <linux/regmap.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <asm/irq.h> 421da177e4SLinus Torvalds 43436d42c6SArnd Bergmann #include <linux/platform_data/i2c-s3c2410.h> 441da177e4SLinus Torvalds 45e636602aSHeiko Stübner /* see s3c2410x user guide, v1.1, section 9 (p447) for more info */ 46e636602aSHeiko Stübner 477a6674daSHeiko Stübner #define S3C2410_IICCON 0x00 487a6674daSHeiko Stübner #define S3C2410_IICSTAT 0x04 497a6674daSHeiko Stübner #define S3C2410_IICADD 0x08 507a6674daSHeiko Stübner #define S3C2410_IICDS 0x0C 517a6674daSHeiko Stübner #define S3C2440_IICLC 0x10 52e636602aSHeiko Stübner 53e636602aSHeiko Stübner #define S3C2410_IICCON_ACKEN (1 << 7) 54e636602aSHeiko Stübner #define S3C2410_IICCON_TXDIV_16 (0 << 6) 55e636602aSHeiko Stübner #define S3C2410_IICCON_TXDIV_512 (1 << 6) 56e636602aSHeiko Stübner #define S3C2410_IICCON_IRQEN (1 << 5) 57e636602aSHeiko Stübner #define S3C2410_IICCON_IRQPEND (1 << 4) 587a6674daSHeiko Stübner #define S3C2410_IICCON_SCALE(x) ((x) & 0xf) 59e636602aSHeiko Stübner #define S3C2410_IICCON_SCALEMASK (0xf) 60e636602aSHeiko Stübner 61e636602aSHeiko Stübner #define S3C2410_IICSTAT_MASTER_RX (2 << 6) 62e636602aSHeiko Stübner #define S3C2410_IICSTAT_MASTER_TX (3 << 6) 63e636602aSHeiko Stübner #define S3C2410_IICSTAT_SLAVE_RX (0 << 6) 64e636602aSHeiko Stübner #define S3C2410_IICSTAT_SLAVE_TX (1 << 6) 65e636602aSHeiko Stübner #define S3C2410_IICSTAT_MODEMASK (3 << 6) 66e636602aSHeiko Stübner 67e636602aSHeiko Stübner #define S3C2410_IICSTAT_START (1 << 5) 68e636602aSHeiko Stübner #define S3C2410_IICSTAT_BUSBUSY (1 << 5) 69e636602aSHeiko Stübner #define S3C2410_IICSTAT_TXRXEN (1 << 4) 70e636602aSHeiko Stübner #define S3C2410_IICSTAT_ARBITR (1 << 3) 71e636602aSHeiko Stübner #define S3C2410_IICSTAT_ASSLAVE (1 << 2) 72e636602aSHeiko Stübner #define S3C2410_IICSTAT_ADDR0 (1 << 1) 73e636602aSHeiko Stübner #define S3C2410_IICSTAT_LASTBIT (1 << 0) 74e636602aSHeiko Stübner 75e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY0 (0 << 0) 76e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY5 (1 << 0) 77e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY10 (2 << 0) 78e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY15 (3 << 0) 79e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY_MASK (3 << 0) 80e636602aSHeiko Stübner 81e636602aSHeiko Stübner #define S3C2410_IICLC_FILTER_ON (1 << 2) 82e636602aSHeiko Stübner 8327452498SKarol Lewandowski /* Treat S3C2410 as baseline hardware, anything else is supported via quirks */ 8427452498SKarol Lewandowski #define QUIRK_S3C2440 (1 << 0) 85ec39ef83SKarol Lewandowski #define QUIRK_HDMIPHY (1 << 1) 86ec39ef83SKarol Lewandowski #define QUIRK_NO_GPIO (1 << 2) 87117053f7SVasanth Ananthan #define QUIRK_POLL (1 << 3) 881da177e4SLinus Torvalds 89fe724bf9SDaniel Kurtz /* Max time to wait for bus to become idle after a xfer (in us) */ 90fe724bf9SDaniel Kurtz #define S3C2410_IDLE_TIMEOUT 5000 91fe724bf9SDaniel Kurtz 92a7750c3eSPankaj Dubey /* Exynos5 Sysreg offset */ 93a7750c3eSPankaj Dubey #define EXYNOS5_SYS_I2C_CFG 0x0234 94a7750c3eSPankaj Dubey 9527452498SKarol Lewandowski /* i2c controller state */ 961da177e4SLinus Torvalds enum s3c24xx_i2c_state { 971da177e4SLinus Torvalds STATE_IDLE, 981da177e4SLinus Torvalds STATE_START, 991da177e4SLinus Torvalds STATE_READ, 1001da177e4SLinus Torvalds STATE_WRITE, 1011da177e4SLinus Torvalds STATE_STOP 1021da177e4SLinus Torvalds }; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds struct s3c24xx_i2c { 1051da177e4SLinus Torvalds wait_queue_head_t wait; 1065f1b1155SPankaj Dubey kernel_ulong_t quirks; 107be44f01eSBen Dooks unsigned int suspended:1; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds struct i2c_msg *msg; 1101da177e4SLinus Torvalds unsigned int msg_num; 1111da177e4SLinus Torvalds unsigned int msg_idx; 1121da177e4SLinus Torvalds unsigned int msg_ptr; 1131da177e4SLinus Torvalds 114e00a8cdfSBen Dooks unsigned int tx_setup; 115e0d1ec97SBen Dooks unsigned int irq; 116e00a8cdfSBen Dooks 1171da177e4SLinus Torvalds enum s3c24xx_i2c_state state; 11861c7cff8SBen Dooks unsigned long clkrate; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds void __iomem *regs; 1211da177e4SLinus Torvalds struct clk *clk; 1221da177e4SLinus Torvalds struct device *dev; 1231da177e4SLinus Torvalds struct i2c_adapter adap; 12461c7cff8SBen Dooks 1254fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata; 1265a5f5080SThomas Abraham int gpios[2]; 1272693ac69STomasz Figa struct pinctrl *pctrl; 12861f4d6b4SNaveen Krishna Ch #if defined(CONFIG_ARM_S3C24XX_CPUFREQ) 12961c7cff8SBen Dooks struct notifier_block freq_transition; 13061c7cff8SBen Dooks #endif 131a7750c3eSPankaj Dubey struct regmap *sysreg; 132a7750c3eSPankaj Dubey unsigned int sys_i2c_cfg; 1331da177e4SLinus Torvalds }; 1341da177e4SLinus Torvalds 13527452498SKarol Lewandowski static struct platform_device_id s3c24xx_driver_ids[] = { 1361da177e4SLinus Torvalds { 13727452498SKarol Lewandowski .name = "s3c2410-i2c", 13827452498SKarol Lewandowski .driver_data = 0, 13927452498SKarol Lewandowski }, { 14027452498SKarol Lewandowski .name = "s3c2440-i2c", 14127452498SKarol Lewandowski .driver_data = QUIRK_S3C2440, 142ec39ef83SKarol Lewandowski }, { 143ec39ef83SKarol Lewandowski .name = "s3c2440-hdmiphy-i2c", 144ec39ef83SKarol Lewandowski .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO, 14527452498SKarol Lewandowski }, { }, 14627452498SKarol Lewandowski }; 14727452498SKarol Lewandowski MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids); 1481da177e4SLinus Torvalds 149117053f7SVasanth Ananthan static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat); 150117053f7SVasanth Ananthan 1515a5f5080SThomas Abraham #ifdef CONFIG_OF 15227452498SKarol Lewandowski static const struct of_device_id s3c24xx_i2c_match[] = { 15327452498SKarol Lewandowski { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 }, 15427452498SKarol Lewandowski { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 }, 155ec39ef83SKarol Lewandowski { .compatible = "samsung,s3c2440-hdmiphy-i2c", 156ec39ef83SKarol Lewandowski .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) }, 157faf93ff6SGiridhar Maruthy { .compatible = "samsung,exynos5440-i2c", 158faf93ff6SGiridhar Maruthy .data = (void *)(QUIRK_S3C2440 | QUIRK_NO_GPIO) }, 159117053f7SVasanth Ananthan { .compatible = "samsung,exynos5-sata-phy-i2c", 160117053f7SVasanth Ananthan .data = (void *)(QUIRK_S3C2440 | QUIRK_POLL | QUIRK_NO_GPIO) }, 16127452498SKarol Lewandowski {}, 16227452498SKarol Lewandowski }; 16327452498SKarol Lewandowski MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match); 1645a5f5080SThomas Abraham #endif 1655a5f5080SThomas Abraham 16627452498SKarol Lewandowski /* s3c24xx_get_device_quirks 16727452498SKarol Lewandowski * 16827452498SKarol Lewandowski * Get controller type either from device tree or platform device variant. 16927452498SKarol Lewandowski */ 17027452498SKarol Lewandowski 1715f1b1155SPankaj Dubey static inline kernel_ulong_t s3c24xx_get_device_quirks(struct platform_device *pdev) 17227452498SKarol Lewandowski { 17327452498SKarol Lewandowski if (pdev->dev.of_node) { 17427452498SKarol Lewandowski const struct of_device_id *match; 175b900ba4cSKarol Lewandowski match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node); 1765f1b1155SPankaj Dubey return (kernel_ulong_t)match->data; 17727452498SKarol Lewandowski } 17827452498SKarol Lewandowski 17927452498SKarol Lewandowski return platform_get_device_id(pdev)->driver_data; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* s3c24xx_i2c_master_complete 1831da177e4SLinus Torvalds * 1841da177e4SLinus Torvalds * complete the message and wake up the caller, using the given return code, 1851da177e4SLinus Torvalds * or zero to mean ok. 1861da177e4SLinus Torvalds */ 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds dev_dbg(i2c->dev, "master_complete %d\n", ret); 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds i2c->msg_ptr = 0; 1931da177e4SLinus Torvalds i2c->msg = NULL; 1941da177e4SLinus Torvalds i2c->msg_idx++; 1951da177e4SLinus Torvalds i2c->msg_num = 0; 1961da177e4SLinus Torvalds if (ret) 1971da177e4SLinus Torvalds i2c->msg_idx = ret; 1981da177e4SLinus Torvalds 199117053f7SVasanth Ananthan if (!(i2c->quirks & QUIRK_POLL)) 2001da177e4SLinus Torvalds wake_up(&i2c->wait); 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c) 2041da177e4SLinus Torvalds { 2051da177e4SLinus Torvalds unsigned long tmp; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 2081da177e4SLinus Torvalds writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds unsigned long tmp; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 2161da177e4SLinus Torvalds writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds /* irq enable/disable functions */ 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds unsigned long tmp; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 2261da177e4SLinus Torvalds writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds unsigned long tmp; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 2341da177e4SLinus Torvalds writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds 237117053f7SVasanth Ananthan static bool is_ack(struct s3c24xx_i2c *i2c) 238117053f7SVasanth Ananthan { 239117053f7SVasanth Ananthan int tries; 240117053f7SVasanth Ananthan 241117053f7SVasanth Ananthan for (tries = 50; tries; --tries) { 242117053f7SVasanth Ananthan if (readl(i2c->regs + S3C2410_IICCON) 243117053f7SVasanth Ananthan & S3C2410_IICCON_IRQPEND) { 244117053f7SVasanth Ananthan if (!(readl(i2c->regs + S3C2410_IICSTAT) 245117053f7SVasanth Ananthan & S3C2410_IICSTAT_LASTBIT)) 246117053f7SVasanth Ananthan return true; 247117053f7SVasanth Ananthan } 248117053f7SVasanth Ananthan usleep_range(1000, 2000); 249117053f7SVasanth Ananthan } 2509b13494cSMasanari Iida dev_err(i2c->dev, "ack was not received\n"); 251117053f7SVasanth Ananthan return false; 252117053f7SVasanth Ananthan } 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds /* s3c24xx_i2c_message_start 2551da177e4SLinus Torvalds * 2561da177e4SLinus Torvalds * put the start of a message onto the bus 2571da177e4SLinus Torvalds */ 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, 2601da177e4SLinus Torvalds struct i2c_msg *msg) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds unsigned int addr = (msg->addr & 0x7f) << 1; 2631da177e4SLinus Torvalds unsigned long stat; 2641da177e4SLinus Torvalds unsigned long iiccon; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds stat = 0; 2671da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_TXRXEN; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds if (msg->flags & I2C_M_RD) { 2701da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_MASTER_RX; 2711da177e4SLinus Torvalds addr |= 1; 2721da177e4SLinus Torvalds } else 2731da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_MASTER_TX; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds if (msg->flags & I2C_M_REV_DIR_ADDR) 2761da177e4SLinus Torvalds addr ^= 1; 2771da177e4SLinus Torvalds 27848fc7f7eSAdam Buchbinder /* todo - check for whether ack wanted or not */ 2791da177e4SLinus Torvalds s3c24xx_i2c_enable_ack(i2c); 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds iiccon = readl(i2c->regs + S3C2410_IICCON); 2821da177e4SLinus Torvalds writel(stat, i2c->regs + S3C2410_IICSTAT); 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr); 2851da177e4SLinus Torvalds writeb(addr, i2c->regs + S3C2410_IICDS); 2861da177e4SLinus Torvalds 287e00a8cdfSBen Dooks /* delay here to ensure the data byte has gotten onto the bus 288e00a8cdfSBen Dooks * before the transaction is started */ 289e00a8cdfSBen Dooks 290e00a8cdfSBen Dooks ndelay(i2c->tx_setup); 291e00a8cdfSBen Dooks 2921da177e4SLinus Torvalds dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon); 2931da177e4SLinus Torvalds writel(iiccon, i2c->regs + S3C2410_IICCON); 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_START; 2961da177e4SLinus Torvalds writel(stat, i2c->regs + S3C2410_IICSTAT); 297117053f7SVasanth Ananthan 298117053f7SVasanth Ananthan if (i2c->quirks & QUIRK_POLL) { 299117053f7SVasanth Ananthan while ((i2c->msg_num != 0) && is_ack(i2c)) { 300117053f7SVasanth Ananthan i2c_s3c_irq_nextbyte(i2c, stat); 301117053f7SVasanth Ananthan stat = readl(i2c->regs + S3C2410_IICSTAT); 302117053f7SVasanth Ananthan 303117053f7SVasanth Ananthan if (stat & S3C2410_IICSTAT_ARBITR) 304117053f7SVasanth Ananthan dev_err(i2c->dev, "deal with arbitration loss\n"); 305117053f7SVasanth Ananthan } 306117053f7SVasanth Ananthan } 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret) 3101da177e4SLinus Torvalds { 3111da177e4SLinus Torvalds unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds dev_dbg(i2c->dev, "STOP\n"); 3141da177e4SLinus Torvalds 3150da2e776SDaniel Kurtz /* 3160da2e776SDaniel Kurtz * The datasheet says that the STOP sequence should be: 3170da2e776SDaniel Kurtz * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP') 3180da2e776SDaniel Kurtz * 2) I2CCON.4 = 0 - Clear IRQPEND 3190da2e776SDaniel Kurtz * 3) Wait until the stop condition takes effect. 3200da2e776SDaniel Kurtz * 4*) I2CSTAT.4 = 0 - Clear TXRXEN 3210da2e776SDaniel Kurtz * 3220da2e776SDaniel Kurtz * Where, step "4*" is only for buses with the "HDMIPHY" quirk. 3230da2e776SDaniel Kurtz * 3240da2e776SDaniel Kurtz * However, after much experimentation, it appears that: 3250da2e776SDaniel Kurtz * a) normal buses automatically clear BUSY and transition from 3260da2e776SDaniel Kurtz * Master->Slave when they complete generating a STOP condition. 3270da2e776SDaniel Kurtz * Therefore, step (3) can be done in doxfer() by polling I2CCON.4 3280da2e776SDaniel Kurtz * after starting the STOP generation here. 3290da2e776SDaniel Kurtz * b) HDMIPHY bus does neither, so there is no way to do step 3. 3300da2e776SDaniel Kurtz * There is no indication when this bus has finished generating 3310da2e776SDaniel Kurtz * STOP. 3320da2e776SDaniel Kurtz * 3330da2e776SDaniel Kurtz * In fact, we have found that as soon as the IRQPEND bit is cleared in 3340da2e776SDaniel Kurtz * step 2, the HDMIPHY bus generates the STOP condition, and then 3350da2e776SDaniel Kurtz * immediately starts transferring another data byte, even though the 3360da2e776SDaniel Kurtz * bus is supposedly stopped. This is presumably because the bus is 3370da2e776SDaniel Kurtz * still in "Master" mode, and its BUSY bit is still set. 3380da2e776SDaniel Kurtz * 3390da2e776SDaniel Kurtz * To avoid these extra post-STOP transactions on HDMI phy devices, we 3400da2e776SDaniel Kurtz * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly, 3410da2e776SDaniel Kurtz * instead of first generating a proper STOP condition. This should 3420da2e776SDaniel Kurtz * float SDA & SCK terminating the transfer. Subsequent transfers 3430da2e776SDaniel Kurtz * start with a proper START condition, and proceed normally. 3440da2e776SDaniel Kurtz * 3450da2e776SDaniel Kurtz * The HDMIPHY bus is an internal bus that always has exactly two 3460da2e776SDaniel Kurtz * devices, the host as Master and the HDMIPHY device as the slave. 3470da2e776SDaniel Kurtz * Skipping the STOP condition has been tested on this bus and works. 3480da2e776SDaniel Kurtz */ 3490da2e776SDaniel Kurtz if (i2c->quirks & QUIRK_HDMIPHY) { 3500da2e776SDaniel Kurtz /* Stop driving the I2C pins */ 3510da2e776SDaniel Kurtz iicstat &= ~S3C2410_IICSTAT_TXRXEN; 3520da2e776SDaniel Kurtz } else { 3531da177e4SLinus Torvalds /* stop the transfer */ 3541da177e4SLinus Torvalds iicstat &= ~S3C2410_IICSTAT_START; 3550da2e776SDaniel Kurtz } 3561da177e4SLinus Torvalds writel(iicstat, i2c->regs + S3C2410_IICSTAT); 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds i2c->state = STATE_STOP; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds s3c24xx_i2c_master_complete(i2c, ret); 3611da177e4SLinus Torvalds s3c24xx_i2c_disable_irq(i2c); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds /* helper functions to determine the current state in the set of 3651da177e4SLinus Torvalds * messages we are sending */ 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds /* is_lastmsg() 3681da177e4SLinus Torvalds * 3691da177e4SLinus Torvalds * returns TRUE if the current message is the last in the set 3701da177e4SLinus Torvalds */ 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds static inline int is_lastmsg(struct s3c24xx_i2c *i2c) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds return i2c->msg_idx >= (i2c->msg_num - 1); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds /* is_msglast 3781da177e4SLinus Torvalds * 3791da177e4SLinus Torvalds * returns TRUE if we this is the last byte in the current message 3801da177e4SLinus Torvalds */ 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds static inline int is_msglast(struct s3c24xx_i2c *i2c) 3831da177e4SLinus Torvalds { 38485747311SJaemin Yoo /* msg->len is always 1 for the first byte of smbus block read. 38585747311SJaemin Yoo * Actual length will be read from slave. More bytes will be 38685747311SJaemin Yoo * read according to the length then. */ 38785747311SJaemin Yoo if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1) 38885747311SJaemin Yoo return 0; 38985747311SJaemin Yoo 3901da177e4SLinus Torvalds return i2c->msg_ptr == i2c->msg->len-1; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /* is_msgend 3941da177e4SLinus Torvalds * 3951da177e4SLinus Torvalds * returns TRUE if we reached the end of the current message 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds static inline int is_msgend(struct s3c24xx_i2c *i2c) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds return i2c->msg_ptr >= i2c->msg->len; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 40319820510SHuisung Kang /* i2c_s3c_irq_nextbyte 4041da177e4SLinus Torvalds * 4051da177e4SLinus Torvalds * process an interrupt and work out what to do 4061da177e4SLinus Torvalds */ 4071da177e4SLinus Torvalds 40819820510SHuisung Kang static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat) 4091da177e4SLinus Torvalds { 4101da177e4SLinus Torvalds unsigned long tmp; 4111da177e4SLinus Torvalds unsigned char byte; 4121da177e4SLinus Torvalds int ret = 0; 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds switch (i2c->state) { 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds case STATE_IDLE: 41708882d20SHarvey Harrison dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__); 4181da177e4SLinus Torvalds goto out; 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds case STATE_STOP: 42108882d20SHarvey Harrison dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__); 4221da177e4SLinus Torvalds s3c24xx_i2c_disable_irq(i2c); 4231da177e4SLinus Torvalds goto out_ack; 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds case STATE_START: 4261da177e4SLinus Torvalds /* last thing we did was send a start condition on the 4271da177e4SLinus Torvalds * bus, or started a new i2c message 4281da177e4SLinus Torvalds */ 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds if (iicstat & S3C2410_IICSTAT_LASTBIT && 4311da177e4SLinus Torvalds !(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 4321da177e4SLinus Torvalds /* ack was not received... */ 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds dev_dbg(i2c->dev, "ack was not received\n"); 43563f5c289SBen Dooks s3c24xx_i2c_stop(i2c, -ENXIO); 4361da177e4SLinus Torvalds goto out_ack; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_RD) 4401da177e4SLinus Torvalds i2c->state = STATE_READ; 4411da177e4SLinus Torvalds else 4421da177e4SLinus Torvalds i2c->state = STATE_WRITE; 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds /* terminate the transfer if there is nothing to do 44563f5c289SBen Dooks * as this is used by the i2c probe to find devices. */ 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds if (is_lastmsg(i2c) && i2c->msg->len == 0) { 4481da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 4491da177e4SLinus Torvalds goto out_ack; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds if (i2c->state == STATE_READ) 4531da177e4SLinus Torvalds goto prepare_read; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /* fall through to the write state, as we will need to 4561da177e4SLinus Torvalds * send a byte as well */ 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds case STATE_WRITE: 4591da177e4SLinus Torvalds /* we are writing data to the device... check for the 4601da177e4SLinus Torvalds * end of the message, and if so, work out what to do 4611da177e4SLinus Torvalds */ 4621da177e4SLinus Torvalds 4632709781bSBen Dooks if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 4642709781bSBen Dooks if (iicstat & S3C2410_IICSTAT_LASTBIT) { 4652709781bSBen Dooks dev_dbg(i2c->dev, "WRITE: No Ack\n"); 4662709781bSBen Dooks 4672709781bSBen Dooks s3c24xx_i2c_stop(i2c, -ECONNREFUSED); 4682709781bSBen Dooks goto out_ack; 4692709781bSBen Dooks } 4702709781bSBen Dooks } 4712709781bSBen Dooks 4721da177e4SLinus Torvalds retry_write: 4732709781bSBen Dooks 4741da177e4SLinus Torvalds if (!is_msgend(i2c)) { 4751da177e4SLinus Torvalds byte = i2c->msg->buf[i2c->msg_ptr++]; 4761da177e4SLinus Torvalds writeb(byte, i2c->regs + S3C2410_IICDS); 4771da177e4SLinus Torvalds 478e00a8cdfSBen Dooks /* delay after writing the byte to allow the 479e00a8cdfSBen Dooks * data setup time on the bus, as writing the 480e00a8cdfSBen Dooks * data to the register causes the first bit 481e00a8cdfSBen Dooks * to appear on SDA, and SCL will change as 482e00a8cdfSBen Dooks * soon as the interrupt is acknowledged */ 483e00a8cdfSBen Dooks 484e00a8cdfSBen Dooks ndelay(i2c->tx_setup); 485e00a8cdfSBen Dooks 4861da177e4SLinus Torvalds } else if (!is_lastmsg(i2c)) { 4871da177e4SLinus Torvalds /* we need to go to the next i2c message */ 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds dev_dbg(i2c->dev, "WRITE: Next Message\n"); 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds i2c->msg_ptr = 0; 4921da177e4SLinus Torvalds i2c->msg_idx++; 4931da177e4SLinus Torvalds i2c->msg++; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* check to see if we need to do another message */ 4961da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_NOSTART) { 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_RD) { 4991da177e4SLinus Torvalds /* cannot do this, the controller 5001da177e4SLinus Torvalds * forces us to send a new START 5011da177e4SLinus Torvalds * when we change direction */ 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, -EINVAL); 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds goto retry_write; 5071da177e4SLinus Torvalds } else { 5081da177e4SLinus Torvalds /* send the new start */ 5091da177e4SLinus Torvalds s3c24xx_i2c_message_start(i2c, i2c->msg); 5101da177e4SLinus Torvalds i2c->state = STATE_START; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds } else { 5141da177e4SLinus Torvalds /* send stop */ 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds break; 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds case STATE_READ: 5211da177e4SLinus Torvalds /* we have a byte of data in the data register, do 52248fc7f7eSAdam Buchbinder * something with it, and then work out whether we are 5231da177e4SLinus Torvalds * going to do any more read/write 5241da177e4SLinus Torvalds */ 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds byte = readb(i2c->regs + S3C2410_IICDS); 5271da177e4SLinus Torvalds i2c->msg->buf[i2c->msg_ptr++] = byte; 5281da177e4SLinus Torvalds 52985747311SJaemin Yoo /* Add actual length to read for smbus block read */ 53085747311SJaemin Yoo if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1) 53185747311SJaemin Yoo i2c->msg->len += byte; 5321da177e4SLinus Torvalds prepare_read: 5331da177e4SLinus Torvalds if (is_msglast(i2c)) { 5341da177e4SLinus Torvalds /* last byte of buffer */ 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds if (is_lastmsg(i2c)) 5371da177e4SLinus Torvalds s3c24xx_i2c_disable_ack(i2c); 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds } else if (is_msgend(i2c)) { 5401da177e4SLinus Torvalds /* ok, we've read the entire buffer, see if there 5411da177e4SLinus Torvalds * is anything else we need to do */ 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds if (is_lastmsg(i2c)) { 5441da177e4SLinus Torvalds /* last message, send stop and complete */ 5451da177e4SLinus Torvalds dev_dbg(i2c->dev, "READ: Send Stop\n"); 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 5481da177e4SLinus Torvalds } else { 5491da177e4SLinus Torvalds /* go to the next transfer */ 5501da177e4SLinus Torvalds dev_dbg(i2c->dev, "READ: Next Transfer\n"); 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds i2c->msg_ptr = 0; 5531da177e4SLinus Torvalds i2c->msg_idx++; 5541da177e4SLinus Torvalds i2c->msg++; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds break; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds /* acknowlegde the IRQ and get back on with the work */ 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds out_ack: 5641da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 5651da177e4SLinus Torvalds tmp &= ~S3C2410_IICCON_IRQPEND; 5661da177e4SLinus Torvalds writel(tmp, i2c->regs + S3C2410_IICCON); 5671da177e4SLinus Torvalds out: 5681da177e4SLinus Torvalds return ret; 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds /* s3c24xx_i2c_irq 5721da177e4SLinus Torvalds * 5731da177e4SLinus Torvalds * top level IRQ servicing routine 5741da177e4SLinus Torvalds */ 5751da177e4SLinus Torvalds 5767d12e780SDavid Howells static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id) 5771da177e4SLinus Torvalds { 5781da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = dev_id; 5791da177e4SLinus Torvalds unsigned long status; 5801da177e4SLinus Torvalds unsigned long tmp; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds status = readl(i2c->regs + S3C2410_IICSTAT); 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds if (status & S3C2410_IICSTAT_ARBITR) { 5853d0911bfSBen Dooks /* deal with arbitration loss */ 5861da177e4SLinus Torvalds dev_err(i2c->dev, "deal with arbitration loss\n"); 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds if (i2c->state == STATE_IDLE) { 5901da177e4SLinus Torvalds dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n"); 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 5931da177e4SLinus Torvalds tmp &= ~S3C2410_IICCON_IRQPEND; 5941da177e4SLinus Torvalds writel(tmp, i2c->regs + S3C2410_IICCON); 5951da177e4SLinus Torvalds goto out; 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds /* pretty much this leaves us with the fact that we've 5991da177e4SLinus Torvalds * transmitted or received whatever byte we last sent */ 6001da177e4SLinus Torvalds 60119820510SHuisung Kang i2c_s3c_irq_nextbyte(i2c, status); 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds out: 6041da177e4SLinus Torvalds return IRQ_HANDLED; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds 607069a9502SSimon Glass /* 608069a9502SSimon Glass * Disable the bus so that we won't get any interrupts from now on, or try 609069a9502SSimon Glass * to drive any lines. This is the default state when we don't have 610069a9502SSimon Glass * anything to send/receive. 611069a9502SSimon Glass * 612069a9502SSimon Glass * If there is an event on the bus, or we have a pre-existing event at 613069a9502SSimon Glass * kernel boot time, we may not notice the event and the I2C controller 614069a9502SSimon Glass * will lock the bus with the I2C clock line low indefinitely. 615069a9502SSimon Glass */ 616069a9502SSimon Glass static inline void s3c24xx_i2c_disable_bus(struct s3c24xx_i2c *i2c) 617069a9502SSimon Glass { 618069a9502SSimon Glass unsigned long tmp; 619069a9502SSimon Glass 620069a9502SSimon Glass /* Stop driving the I2C pins */ 621069a9502SSimon Glass tmp = readl(i2c->regs + S3C2410_IICSTAT); 622069a9502SSimon Glass tmp &= ~S3C2410_IICSTAT_TXRXEN; 623069a9502SSimon Glass writel(tmp, i2c->regs + S3C2410_IICSTAT); 624069a9502SSimon Glass 625069a9502SSimon Glass /* We don't expect any interrupts now, and don't want send acks */ 626069a9502SSimon Glass tmp = readl(i2c->regs + S3C2410_IICCON); 627069a9502SSimon Glass tmp &= ~(S3C2410_IICCON_IRQEN | S3C2410_IICCON_IRQPEND | 628069a9502SSimon Glass S3C2410_IICCON_ACKEN); 629069a9502SSimon Glass writel(tmp, i2c->regs + S3C2410_IICCON); 630069a9502SSimon Glass } 631069a9502SSimon Glass 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds /* s3c24xx_i2c_set_master 6341da177e4SLinus Torvalds * 6351da177e4SLinus Torvalds * get the i2c bus for a master transaction 6361da177e4SLinus Torvalds */ 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c) 6391da177e4SLinus Torvalds { 6401da177e4SLinus Torvalds unsigned long iicstat; 6411da177e4SLinus Torvalds int timeout = 400; 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds while (timeout-- > 0) { 6441da177e4SLinus Torvalds iicstat = readl(i2c->regs + S3C2410_IICSTAT); 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds if (!(iicstat & S3C2410_IICSTAT_BUSBUSY)) 6471da177e4SLinus Torvalds return 0; 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds msleep(1); 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds return -ETIMEDOUT; 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds 655fe724bf9SDaniel Kurtz /* s3c24xx_i2c_wait_idle 656fe724bf9SDaniel Kurtz * 657fe724bf9SDaniel Kurtz * wait for the i2c bus to become idle. 658fe724bf9SDaniel Kurtz */ 659fe724bf9SDaniel Kurtz 660fe724bf9SDaniel Kurtz static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c) 661fe724bf9SDaniel Kurtz { 662fe724bf9SDaniel Kurtz unsigned long iicstat; 663fe724bf9SDaniel Kurtz ktime_t start, now; 664fe724bf9SDaniel Kurtz unsigned long delay; 66531f313d9SMark Brown int spins; 666fe724bf9SDaniel Kurtz 667fe724bf9SDaniel Kurtz /* ensure the stop has been through the bus */ 668fe724bf9SDaniel Kurtz 669fe724bf9SDaniel Kurtz dev_dbg(i2c->dev, "waiting for bus idle\n"); 670fe724bf9SDaniel Kurtz 671fe724bf9SDaniel Kurtz start = now = ktime_get(); 672fe724bf9SDaniel Kurtz 673fe724bf9SDaniel Kurtz /* 674fe724bf9SDaniel Kurtz * Most of the time, the bus is already idle within a few usec of the 675fe724bf9SDaniel Kurtz * end of a transaction. However, really slow i2c devices can stretch 676fe724bf9SDaniel Kurtz * the clock, delaying STOP generation. 677fe724bf9SDaniel Kurtz * 67831f313d9SMark Brown * On slower SoCs this typically happens within a very small number of 67931f313d9SMark Brown * instructions so busy wait briefly to avoid scheduling overhead. 680fe724bf9SDaniel Kurtz */ 68131f313d9SMark Brown spins = 3; 682fe724bf9SDaniel Kurtz iicstat = readl(i2c->regs + S3C2410_IICSTAT); 68331f313d9SMark Brown while ((iicstat & S3C2410_IICSTAT_START) && --spins) { 68431f313d9SMark Brown cpu_relax(); 68531f313d9SMark Brown iicstat = readl(i2c->regs + S3C2410_IICSTAT); 68631f313d9SMark Brown } 68731f313d9SMark Brown 68831f313d9SMark Brown /* 68931f313d9SMark Brown * If we do get an appreciable delay as a compromise between idle 69031f313d9SMark Brown * detection latency for the normal, fast case, and system load in the 69131f313d9SMark Brown * slow device case, use an exponential back off in the polling loop, 69231f313d9SMark Brown * up to 1/10th of the total timeout, then continue to poll at a 69331f313d9SMark Brown * constant rate up to the timeout. 69431f313d9SMark Brown */ 695fe724bf9SDaniel Kurtz delay = 1; 696fe724bf9SDaniel Kurtz while ((iicstat & S3C2410_IICSTAT_START) && 697fe724bf9SDaniel Kurtz ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) { 698fe724bf9SDaniel Kurtz usleep_range(delay, 2 * delay); 699fe724bf9SDaniel Kurtz if (delay < S3C2410_IDLE_TIMEOUT / 10) 700fe724bf9SDaniel Kurtz delay <<= 1; 701fe724bf9SDaniel Kurtz now = ktime_get(); 702fe724bf9SDaniel Kurtz iicstat = readl(i2c->regs + S3C2410_IICSTAT); 703fe724bf9SDaniel Kurtz } 704fe724bf9SDaniel Kurtz 705fe724bf9SDaniel Kurtz if (iicstat & S3C2410_IICSTAT_START) 706fe724bf9SDaniel Kurtz dev_warn(i2c->dev, "timeout waiting for bus idle\n"); 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /* s3c24xx_i2c_doxfer 7101da177e4SLinus Torvalds * 7111da177e4SLinus Torvalds * this starts an i2c transfer 7121da177e4SLinus Torvalds */ 7131da177e4SLinus Torvalds 7143d0911bfSBen Dooks static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, 7153d0911bfSBen Dooks struct i2c_msg *msgs, int num) 7161da177e4SLinus Torvalds { 717fe724bf9SDaniel Kurtz unsigned long timeout; 7181da177e4SLinus Torvalds int ret; 7191da177e4SLinus Torvalds 720be44f01eSBen Dooks if (i2c->suspended) 72161c7cff8SBen Dooks return -EIO; 72261c7cff8SBen Dooks 7231da177e4SLinus Torvalds ret = s3c24xx_i2c_set_master(i2c); 7241da177e4SLinus Torvalds if (ret != 0) { 7251da177e4SLinus Torvalds dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); 7261da177e4SLinus Torvalds ret = -EAGAIN; 7271da177e4SLinus Torvalds goto out; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds i2c->msg = msgs; 7311da177e4SLinus Torvalds i2c->msg_num = num; 7321da177e4SLinus Torvalds i2c->msg_ptr = 0; 7331da177e4SLinus Torvalds i2c->msg_idx = 0; 7341da177e4SLinus Torvalds i2c->state = STATE_START; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds s3c24xx_i2c_enable_irq(i2c); 7371da177e4SLinus Torvalds s3c24xx_i2c_message_start(i2c, msgs); 7381da177e4SLinus Torvalds 739117053f7SVasanth Ananthan if (i2c->quirks & QUIRK_POLL) { 740117053f7SVasanth Ananthan ret = i2c->msg_idx; 741117053f7SVasanth Ananthan 742117053f7SVasanth Ananthan if (ret != num) 743117053f7SVasanth Ananthan dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); 744117053f7SVasanth Ananthan 745117053f7SVasanth Ananthan goto out; 746117053f7SVasanth Ananthan } 747117053f7SVasanth Ananthan 7481da177e4SLinus Torvalds timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds ret = i2c->msg_idx; 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /* having these next two as dev_err() makes life very 7531da177e4SLinus Torvalds * noisy when doing an i2cdetect */ 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds if (timeout == 0) 7561da177e4SLinus Torvalds dev_dbg(i2c->dev, "timeout\n"); 7571da177e4SLinus Torvalds else if (ret != num) 7581da177e4SLinus Torvalds dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); 7591da177e4SLinus Torvalds 7600da2e776SDaniel Kurtz /* For QUIRK_HDMIPHY, bus is already disabled */ 7610da2e776SDaniel Kurtz if (i2c->quirks & QUIRK_HDMIPHY) 7620da2e776SDaniel Kurtz goto out; 7631da177e4SLinus Torvalds 764fe724bf9SDaniel Kurtz s3c24xx_i2c_wait_idle(i2c); 7651da177e4SLinus Torvalds 766069a9502SSimon Glass s3c24xx_i2c_disable_bus(i2c); 767069a9502SSimon Glass 7681da177e4SLinus Torvalds out: 769069a9502SSimon Glass i2c->state = STATE_IDLE; 770069a9502SSimon Glass 7711da177e4SLinus Torvalds return ret; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds /* s3c24xx_i2c_xfer 7751da177e4SLinus Torvalds * 7761da177e4SLinus Torvalds * first port of call from the i2c bus code when an message needs 77744bbe87eSSteven Cole * transferring across the i2c bus. 7781da177e4SLinus Torvalds */ 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, 7811da177e4SLinus Torvalds struct i2c_msg *msgs, int num) 7821da177e4SLinus Torvalds { 7831da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; 7841da177e4SLinus Torvalds int retry; 7851da177e4SLinus Torvalds int ret; 7861da177e4SLinus Torvalds 787c62c3ca5SMark Brown pm_runtime_get_sync(&adap->dev); 78834e81ad5SPaul Osmialowski ret = clk_enable(i2c->clk); 78934e81ad5SPaul Osmialowski if (ret) 79034e81ad5SPaul Osmialowski return ret; 791d2360b8eSArnaud Patard (Rtp) 7921da177e4SLinus Torvalds for (retry = 0; retry < adap->retries; retry++) { 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds ret = s3c24xx_i2c_doxfer(i2c, msgs, num); 7951da177e4SLinus Torvalds 796d2360b8eSArnaud Patard (Rtp) if (ret != -EAGAIN) { 79734e81ad5SPaul Osmialowski clk_disable(i2c->clk); 798a86ae9ffSMark Brown pm_runtime_put(&adap->dev); 7991da177e4SLinus Torvalds return ret; 800d2360b8eSArnaud Patard (Rtp) } 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds udelay(100); 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds 80734e81ad5SPaul Osmialowski clk_disable(i2c->clk); 808a86ae9ffSMark Brown pm_runtime_put(&adap->dev); 8091da177e4SLinus Torvalds return -EREMOTEIO; 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds /* declare our i2c functionality */ 8131da177e4SLinus Torvalds static u32 s3c24xx_i2c_func(struct i2c_adapter *adap) 8141da177e4SLinus Torvalds { 81514674e70SMark Brown return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART | 81614674e70SMark Brown I2C_FUNC_PROTOCOL_MANGLING; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds /* i2c bus registration info */ 8201da177e4SLinus Torvalds 8218f9082c5SJean Delvare static const struct i2c_algorithm s3c24xx_i2c_algorithm = { 8221da177e4SLinus Torvalds .master_xfer = s3c24xx_i2c_xfer, 8231da177e4SLinus Torvalds .functionality = s3c24xx_i2c_func, 8241da177e4SLinus Torvalds }; 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds /* s3c24xx_i2c_calcdivisor 8271da177e4SLinus Torvalds * 8281da177e4SLinus Torvalds * return the divisor settings for a given frequency 8291da177e4SLinus Torvalds */ 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted, 8321da177e4SLinus Torvalds unsigned int *div1, unsigned int *divs) 8331da177e4SLinus Torvalds { 8341da177e4SLinus Torvalds unsigned int calc_divs = clkin / wanted; 8351da177e4SLinus Torvalds unsigned int calc_div1; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds if (calc_divs > (16*16)) 8381da177e4SLinus Torvalds calc_div1 = 512; 8391da177e4SLinus Torvalds else 8401da177e4SLinus Torvalds calc_div1 = 16; 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds calc_divs += calc_div1-1; 8431da177e4SLinus Torvalds calc_divs /= calc_div1; 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds if (calc_divs == 0) 8461da177e4SLinus Torvalds calc_divs = 1; 8471da177e4SLinus Torvalds if (calc_divs > 17) 8481da177e4SLinus Torvalds calc_divs = 17; 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds *divs = calc_divs; 8511da177e4SLinus Torvalds *div1 = calc_div1; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds return clkin / (calc_divs * calc_div1); 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 85661c7cff8SBen Dooks /* s3c24xx_i2c_clockrate 8571da177e4SLinus Torvalds * 8581da177e4SLinus Torvalds * work out a divisor for the user requested frequency setting, 8591da177e4SLinus Torvalds * either by the requested frequency, or scanning the acceptable 8601da177e4SLinus Torvalds * range of frequencies until something is found 8611da177e4SLinus Torvalds */ 8621da177e4SLinus Torvalds 86361c7cff8SBen Dooks static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) 8641da177e4SLinus Torvalds { 8654fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata = i2c->pdata; 8661da177e4SLinus Torvalds unsigned long clkin = clk_get_rate(i2c->clk); 8671da177e4SLinus Torvalds unsigned int divs, div1; 868c564e6aeSDaniel Silverstone unsigned long target_frequency; 86961c7cff8SBen Dooks u32 iiccon; 8701da177e4SLinus Torvalds int freq; 8711da177e4SLinus Torvalds 87261c7cff8SBen Dooks i2c->clkrate = clkin; 8731da177e4SLinus Torvalds clkin /= 1000; /* clkin now in KHz */ 8741da177e4SLinus Torvalds 875c564e6aeSDaniel Silverstone dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency); 8761da177e4SLinus Torvalds 877c564e6aeSDaniel Silverstone target_frequency = pdata->frequency ? pdata->frequency : 100000; 8781da177e4SLinus Torvalds 879c564e6aeSDaniel Silverstone target_frequency /= 1000; /* Target frequency now in KHz */ 8801da177e4SLinus Torvalds 881c564e6aeSDaniel Silverstone freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs); 8821da177e4SLinus Torvalds 883c564e6aeSDaniel Silverstone if (freq > target_frequency) { 884c564e6aeSDaniel Silverstone dev_err(i2c->dev, 885c564e6aeSDaniel Silverstone "Unable to achieve desired frequency %luKHz." \ 886c564e6aeSDaniel Silverstone " Lowest achievable %dKHz\n", target_frequency, freq); 8871da177e4SLinus Torvalds return -EINVAL; 888c564e6aeSDaniel Silverstone } 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds *got = freq; 89161c7cff8SBen Dooks 89261c7cff8SBen Dooks iiccon = readl(i2c->regs + S3C2410_IICCON); 89361c7cff8SBen Dooks iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512); 89461c7cff8SBen Dooks iiccon |= (divs-1); 89561c7cff8SBen Dooks 89661c7cff8SBen Dooks if (div1 == 512) 89761c7cff8SBen Dooks iiccon |= S3C2410_IICCON_TXDIV_512; 89861c7cff8SBen Dooks 899117053f7SVasanth Ananthan if (i2c->quirks & QUIRK_POLL) 900117053f7SVasanth Ananthan iiccon |= S3C2410_IICCON_SCALE(2); 901117053f7SVasanth Ananthan 90261c7cff8SBen Dooks writel(iiccon, i2c->regs + S3C2410_IICCON); 90361c7cff8SBen Dooks 90427452498SKarol Lewandowski if (i2c->quirks & QUIRK_S3C2440) { 905a192f715SBen Dooks unsigned long sda_delay; 906a192f715SBen Dooks 907a192f715SBen Dooks if (pdata->sda_delay) { 9087031307aSMyungJoo Ham sda_delay = clkin * pdata->sda_delay; 9097031307aSMyungJoo Ham sda_delay = DIV_ROUND_UP(sda_delay, 1000000); 910a192f715SBen Dooks sda_delay = DIV_ROUND_UP(sda_delay, 5); 911a192f715SBen Dooks if (sda_delay > 3) 912a192f715SBen Dooks sda_delay = 3; 913a192f715SBen Dooks sda_delay |= S3C2410_IICLC_FILTER_ON; 914a192f715SBen Dooks } else 915a192f715SBen Dooks sda_delay = 0; 916a192f715SBen Dooks 917a192f715SBen Dooks dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay); 918a192f715SBen Dooks writel(sda_delay, i2c->regs + S3C2440_IICLC); 919a192f715SBen Dooks } 920a192f715SBen Dooks 9211da177e4SLinus Torvalds return 0; 9221da177e4SLinus Torvalds } 9231da177e4SLinus Torvalds 92461f4d6b4SNaveen Krishna Ch #if defined(CONFIG_ARM_S3C24XX_CPUFREQ) 92561c7cff8SBen Dooks 92661c7cff8SBen Dooks #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition) 92761c7cff8SBen Dooks 92861c7cff8SBen Dooks static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb, 92961c7cff8SBen Dooks unsigned long val, void *data) 93061c7cff8SBen Dooks { 93161c7cff8SBen Dooks struct s3c24xx_i2c *i2c = freq_to_i2c(nb); 93261c7cff8SBen Dooks unsigned int got; 93361c7cff8SBen Dooks int delta_f; 93461c7cff8SBen Dooks int ret; 93561c7cff8SBen Dooks 93661c7cff8SBen Dooks delta_f = clk_get_rate(i2c->clk) - i2c->clkrate; 93761c7cff8SBen Dooks 93861c7cff8SBen Dooks /* if we're post-change and the input clock has slowed down 93961c7cff8SBen Dooks * or at pre-change and the clock is about to speed up, then 94061c7cff8SBen Dooks * adjust our clock rate. <0 is slow, >0 speedup. 94161c7cff8SBen Dooks */ 94261c7cff8SBen Dooks 94361c7cff8SBen Dooks if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) || 94461c7cff8SBen Dooks (val == CPUFREQ_PRECHANGE && delta_f > 0)) { 9459bcd04bfSDaniel Kurtz i2c_lock_adapter(&i2c->adap); 94661c7cff8SBen Dooks ret = s3c24xx_i2c_clockrate(i2c, &got); 9479bcd04bfSDaniel Kurtz i2c_unlock_adapter(&i2c->adap); 94861c7cff8SBen Dooks 94961c7cff8SBen Dooks if (ret < 0) 95061c7cff8SBen Dooks dev_err(i2c->dev, "cannot find frequency\n"); 95161c7cff8SBen Dooks else 95261c7cff8SBen Dooks dev_info(i2c->dev, "setting freq %d\n", got); 95361c7cff8SBen Dooks } 95461c7cff8SBen Dooks 95561c7cff8SBen Dooks return 0; 95661c7cff8SBen Dooks } 95761c7cff8SBen Dooks 95861c7cff8SBen Dooks static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) 95961c7cff8SBen Dooks { 96061c7cff8SBen Dooks i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition; 96161c7cff8SBen Dooks 96261c7cff8SBen Dooks return cpufreq_register_notifier(&i2c->freq_transition, 96361c7cff8SBen Dooks CPUFREQ_TRANSITION_NOTIFIER); 96461c7cff8SBen Dooks } 96561c7cff8SBen Dooks 96661c7cff8SBen Dooks static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) 96761c7cff8SBen Dooks { 96861c7cff8SBen Dooks cpufreq_unregister_notifier(&i2c->freq_transition, 96961c7cff8SBen Dooks CPUFREQ_TRANSITION_NOTIFIER); 97061c7cff8SBen Dooks } 97161c7cff8SBen Dooks 97261c7cff8SBen Dooks #else 97361c7cff8SBen Dooks static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) 97461c7cff8SBen Dooks { 97561c7cff8SBen Dooks return 0; 97661c7cff8SBen Dooks } 97761c7cff8SBen Dooks 97861c7cff8SBen Dooks static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) 97961c7cff8SBen Dooks { 98061c7cff8SBen Dooks } 98161c7cff8SBen Dooks #endif 98261c7cff8SBen Dooks 9835a5f5080SThomas Abraham #ifdef CONFIG_OF 9845a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) 9855a5f5080SThomas Abraham { 9865a5f5080SThomas Abraham int idx, gpio, ret; 9875a5f5080SThomas Abraham 988ec39ef83SKarol Lewandowski if (i2c->quirks & QUIRK_NO_GPIO) 989ec39ef83SKarol Lewandowski return 0; 990ec39ef83SKarol Lewandowski 9915a5f5080SThomas Abraham for (idx = 0; idx < 2; idx++) { 9925a5f5080SThomas Abraham gpio = of_get_gpio(i2c->dev->of_node, idx); 9935a5f5080SThomas Abraham if (!gpio_is_valid(gpio)) { 9945a5f5080SThomas Abraham dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio); 9955a5f5080SThomas Abraham goto free_gpio; 9965a5f5080SThomas Abraham } 997963f2076SAbhilash Kesavan i2c->gpios[idx] = gpio; 9985a5f5080SThomas Abraham 9995a5f5080SThomas Abraham ret = gpio_request(gpio, "i2c-bus"); 10005a5f5080SThomas Abraham if (ret) { 10015a5f5080SThomas Abraham dev_err(i2c->dev, "gpio [%d] request failed\n", gpio); 10025a5f5080SThomas Abraham goto free_gpio; 10035a5f5080SThomas Abraham } 10045a5f5080SThomas Abraham } 10055a5f5080SThomas Abraham return 0; 10065a5f5080SThomas Abraham 10075a5f5080SThomas Abraham free_gpio: 10085a5f5080SThomas Abraham while (--idx >= 0) 10095a5f5080SThomas Abraham gpio_free(i2c->gpios[idx]); 10105a5f5080SThomas Abraham return -EINVAL; 10115a5f5080SThomas Abraham } 10125a5f5080SThomas Abraham 10135a5f5080SThomas Abraham static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c) 10145a5f5080SThomas Abraham { 10155a5f5080SThomas Abraham unsigned int idx; 1016ec39ef83SKarol Lewandowski 1017ec39ef83SKarol Lewandowski if (i2c->quirks & QUIRK_NO_GPIO) 1018ec39ef83SKarol Lewandowski return; 1019ec39ef83SKarol Lewandowski 10205a5f5080SThomas Abraham for (idx = 0; idx < 2; idx++) 10215a5f5080SThomas Abraham gpio_free(i2c->gpios[idx]); 10225a5f5080SThomas Abraham } 10235a5f5080SThomas Abraham #else 10245a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) 10255a5f5080SThomas Abraham { 10268ebe661dSTushar Behera return 0; 10275a5f5080SThomas Abraham } 10285a5f5080SThomas Abraham 10295a5f5080SThomas Abraham static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c) 10305a5f5080SThomas Abraham { 10315a5f5080SThomas Abraham } 10325a5f5080SThomas Abraham #endif 10335a5f5080SThomas Abraham 10341da177e4SLinus Torvalds /* s3c24xx_i2c_init 10351da177e4SLinus Torvalds * 10361da177e4SLinus Torvalds * initialise the controller, set the IO lines and frequency 10371da177e4SLinus Torvalds */ 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) 10401da177e4SLinus Torvalds { 10411da177e4SLinus Torvalds struct s3c2410_platform_i2c *pdata; 10421da177e4SLinus Torvalds unsigned int freq; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds /* get the plafrom data */ 10451da177e4SLinus Torvalds 10464fd81eb2SThomas Abraham pdata = i2c->pdata; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds /* write slave address */ 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD); 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); 10531da177e4SLinus Torvalds 1054069a9502SSimon Glass writel(0, i2c->regs + S3C2410_IICCON); 1055069a9502SSimon Glass writel(0, i2c->regs + S3C2410_IICSTAT); 105661c7cff8SBen Dooks 10571da177e4SLinus Torvalds /* we need to work out the divisors for the clock... */ 10581da177e4SLinus Torvalds 105961c7cff8SBen Dooks if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { 10601da177e4SLinus Torvalds dev_err(i2c->dev, "cannot meet bus frequency required\n"); 10611da177e4SLinus Torvalds return -EINVAL; 10621da177e4SLinus Torvalds } 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds /* todo - check that the i2c lines aren't being dragged anywhere */ 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); 1067069a9502SSimon Glass dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02x\n", 1068069a9502SSimon Glass readl(i2c->regs + S3C2410_IICCON)); 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds return 0; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds 10735a5f5080SThomas Abraham #ifdef CONFIG_OF 10745a5f5080SThomas Abraham /* s3c24xx_i2c_parse_dt 10755a5f5080SThomas Abraham * 10765a5f5080SThomas Abraham * Parse the device tree node and retreive the platform data. 10775a5f5080SThomas Abraham */ 10785a5f5080SThomas Abraham 10795a5f5080SThomas Abraham static void 10805a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) 10815a5f5080SThomas Abraham { 10825a5f5080SThomas Abraham struct s3c2410_platform_i2c *pdata = i2c->pdata; 1083a7750c3eSPankaj Dubey int id; 10845a5f5080SThomas Abraham 10855a5f5080SThomas Abraham if (!np) 10865a5f5080SThomas Abraham return; 10875a5f5080SThomas Abraham 10885a5f5080SThomas Abraham pdata->bus_num = -1; /* i2c bus number is dynamically assigned */ 10895a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay); 10905a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr); 10915a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-max-bus-freq", 10925a5f5080SThomas Abraham (u32 *)&pdata->frequency); 1093a7750c3eSPankaj Dubey /* 1094a7750c3eSPankaj Dubey * Exynos5's legacy i2c controller and new high speed i2c 1095a7750c3eSPankaj Dubey * controller have muxed interrupt sources. By default the 1096a7750c3eSPankaj Dubey * interrupts for 4-channel HS-I2C controller are enabled. 1097a7750c3eSPankaj Dubey * If nodes for first four channels of legacy i2c controller 1098a7750c3eSPankaj Dubey * are available then re-configure the interrupts via the 1099a7750c3eSPankaj Dubey * system register. 1100a7750c3eSPankaj Dubey */ 1101a7750c3eSPankaj Dubey id = of_alias_get_id(np, "i2c"); 1102a7750c3eSPankaj Dubey i2c->sysreg = syscon_regmap_lookup_by_phandle(np, 1103a7750c3eSPankaj Dubey "samsung,sysreg-phandle"); 1104a7750c3eSPankaj Dubey if (IS_ERR(i2c->sysreg)) 1105a7750c3eSPankaj Dubey return; 1106a7750c3eSPankaj Dubey 1107a7750c3eSPankaj Dubey regmap_update_bits(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, BIT(id), 0); 11085a5f5080SThomas Abraham } 11095a5f5080SThomas Abraham #else 11105a5f5080SThomas Abraham static void 11115a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) 11125a5f5080SThomas Abraham { 11135a5f5080SThomas Abraham return; 11145a5f5080SThomas Abraham } 11155a5f5080SThomas Abraham #endif 11165a5f5080SThomas Abraham 11171da177e4SLinus Torvalds /* s3c24xx_i2c_probe 11181da177e4SLinus Torvalds * 11191da177e4SLinus Torvalds * called by the bus driver when a suitable device is found 11201da177e4SLinus Torvalds */ 11211da177e4SLinus Torvalds 11223ae5eaecSRussell King static int s3c24xx_i2c_probe(struct platform_device *pdev) 11231da177e4SLinus Torvalds { 1124692acbd3SBen Dooks struct s3c24xx_i2c *i2c; 11254fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata = NULL; 11261da177e4SLinus Torvalds struct resource *res; 11271da177e4SLinus Torvalds int ret; 11281da177e4SLinus Torvalds 11295a5f5080SThomas Abraham if (!pdev->dev.of_node) { 11306d4028c6SJingoo Han pdata = dev_get_platdata(&pdev->dev); 11316a039cabSBen Dooks if (!pdata) { 11326a039cabSBen Dooks dev_err(&pdev->dev, "no platform data\n"); 11336a039cabSBen Dooks return -EINVAL; 11346a039cabSBen Dooks } 11355a5f5080SThomas Abraham } 1136399dee23SBen Dooks 11374ea1557fSMark Brown i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL); 113846797a2aSJingoo Han if (!i2c) 1139692acbd3SBen Dooks return -ENOMEM; 1140692acbd3SBen Dooks 11414fd81eb2SThomas Abraham i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 114246797a2aSJingoo Han if (!i2c->pdata) 1143669da30dSTushar Behera return -ENOMEM; 11444fd81eb2SThomas Abraham 114527452498SKarol Lewandowski i2c->quirks = s3c24xx_get_device_quirks(pdev); 1146*8d487a43SVasily Khoruzhick i2c->sysreg = ERR_PTR(-ENOENT); 11474fd81eb2SThomas Abraham if (pdata) 11484fd81eb2SThomas Abraham memcpy(i2c->pdata, pdata, sizeof(*pdata)); 11495a5f5080SThomas Abraham else 11505a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c); 11514fd81eb2SThomas Abraham 1152692acbd3SBen Dooks strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); 1153692acbd3SBen Dooks i2c->adap.owner = THIS_MODULE; 1154692acbd3SBen Dooks i2c->adap.algo = &s3c24xx_i2c_algorithm; 1155692acbd3SBen Dooks i2c->adap.retries = 2; 11566031d3dfSWolfram Sang i2c->adap.class = I2C_CLASS_DEPRECATED; 1157692acbd3SBen Dooks i2c->tx_setup = 50; 1158692acbd3SBen Dooks 1159692acbd3SBen Dooks init_waitqueue_head(&i2c->wait); 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds /* find the clock and enable it */ 11621da177e4SLinus Torvalds 11633ae5eaecSRussell King i2c->dev = &pdev->dev; 11642b255b94STushar Behera i2c->clk = devm_clk_get(&pdev->dev, "i2c"); 11651da177e4SLinus Torvalds if (IS_ERR(i2c->clk)) { 11663ae5eaecSRussell King dev_err(&pdev->dev, "cannot get clock\n"); 1167669da30dSTushar Behera return -ENOENT; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11703ae5eaecSRussell King dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk); 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds /* map the registers */ 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 117684dbf809SThierry Reding i2c->regs = devm_ioremap_resource(&pdev->dev, res); 11771da177e4SLinus Torvalds 117852caa59eSLinus Torvalds if (IS_ERR(i2c->regs)) 117952caa59eSLinus Torvalds return PTR_ERR(i2c->regs); 11801da177e4SLinus Torvalds 1181a72ad456SMark Brown dev_dbg(&pdev->dev, "registers %p (%p)\n", 1182a72ad456SMark Brown i2c->regs, res); 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds /* setup info block for the i2c core */ 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds i2c->adap.algo_data = i2c; 11873ae5eaecSRussell King i2c->adap.dev.parent = &pdev->dev; 11881da177e4SLinus Torvalds 11892693ac69STomasz Figa i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev); 11902693ac69STomasz Figa 1191658122feSAbhilash Kesavan /* inititalise the i2c gpio lines */ 1192658122feSAbhilash Kesavan 1193658122feSAbhilash Kesavan if (i2c->pdata->cfg_gpio) { 1194658122feSAbhilash Kesavan i2c->pdata->cfg_gpio(to_platform_device(i2c->dev)); 1195658122feSAbhilash Kesavan } else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) { 1196d16933b3STushar Behera return -EINVAL; 1197658122feSAbhilash Kesavan } 1198658122feSAbhilash Kesavan 11991da177e4SLinus Torvalds /* initialise the i2c controller */ 12001da177e4SLinus Torvalds 1201d16933b3STushar Behera clk_prepare_enable(i2c->clk); 12021da177e4SLinus Torvalds ret = s3c24xx_i2c_init(i2c); 120334e81ad5SPaul Osmialowski clk_disable(i2c->clk); 1204d16933b3STushar Behera if (ret != 0) { 1205d16933b3STushar Behera dev_err(&pdev->dev, "I2C controller init failed\n"); 1206d16933b3STushar Behera return ret; 1207d16933b3STushar Behera } 12081da177e4SLinus Torvalds /* find the IRQ for this unit (note, this relies on the init call to 12091da177e4SLinus Torvalds * ensure no current IRQs pending 12101da177e4SLinus Torvalds */ 12111da177e4SLinus Torvalds 1212117053f7SVasanth Ananthan if (!(i2c->quirks & QUIRK_POLL)) { 1213e0d1ec97SBen Dooks i2c->irq = ret = platform_get_irq(pdev, 0); 1214e0d1ec97SBen Dooks if (ret <= 0) { 12153ae5eaecSRussell King dev_err(&pdev->dev, "cannot find IRQ\n"); 121634e81ad5SPaul Osmialowski clk_unprepare(i2c->clk); 1217d16933b3STushar Behera return ret; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12202b255b94STushar Behera ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 0, 1221e0d1ec97SBen Dooks dev_name(&pdev->dev), i2c); 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds if (ret != 0) { 1224e0d1ec97SBen Dooks dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); 122534e81ad5SPaul Osmialowski clk_unprepare(i2c->clk); 1226d16933b3STushar Behera return ret; 12271da177e4SLinus Torvalds } 1228117053f7SVasanth Ananthan } 12291da177e4SLinus Torvalds 123061c7cff8SBen Dooks ret = s3c24xx_i2c_register_cpufreq(i2c); 123161c7cff8SBen Dooks if (ret < 0) { 123261c7cff8SBen Dooks dev_err(&pdev->dev, "failed to register cpufreq notifier\n"); 123334e81ad5SPaul Osmialowski clk_unprepare(i2c->clk); 1234d16933b3STushar Behera return ret; 123561c7cff8SBen Dooks } 123661c7cff8SBen Dooks 1237399dee23SBen Dooks /* Note, previous versions of the driver used i2c_add_adapter() 1238399dee23SBen Dooks * to add the bus at any number. We now pass the bus number via 1239399dee23SBen Dooks * the platform data, so if unset it will now default to always 1240399dee23SBen Dooks * being bus 0. 1241399dee23SBen Dooks */ 1242399dee23SBen Dooks 12434fd81eb2SThomas Abraham i2c->adap.nr = i2c->pdata->bus_num; 12445a5f5080SThomas Abraham i2c->adap.dev.of_node = pdev->dev.of_node; 1245399dee23SBen Dooks 1246399dee23SBen Dooks ret = i2c_add_numbered_adapter(&i2c->adap); 12471da177e4SLinus Torvalds if (ret < 0) { 12483ae5eaecSRussell King dev_err(&pdev->dev, "failed to add bus to i2c core\n"); 1249dc6fea44STushar Behera s3c24xx_i2c_deregister_cpufreq(i2c); 125034e81ad5SPaul Osmialowski clk_unprepare(i2c->clk); 1251dc6fea44STushar Behera return ret; 12521da177e4SLinus Torvalds } 12531da177e4SLinus Torvalds 12543ae5eaecSRussell King platform_set_drvdata(pdev, i2c); 12551da177e4SLinus Torvalds 1256c62c3ca5SMark Brown pm_runtime_enable(&pdev->dev); 1257c62c3ca5SMark Brown pm_runtime_enable(&i2c->adap.dev); 1258c62c3ca5SMark Brown 125922e965c2SJean Delvare dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev)); 12605b68790cSBen Dooks return 0; 12611da177e4SLinus Torvalds } 12621da177e4SLinus Torvalds 12631da177e4SLinus Torvalds /* s3c24xx_i2c_remove 12641da177e4SLinus Torvalds * 12651da177e4SLinus Torvalds * called when device is removed from the bus 12661da177e4SLinus Torvalds */ 12671da177e4SLinus Torvalds 12683ae5eaecSRussell King static int s3c24xx_i2c_remove(struct platform_device *pdev) 12691da177e4SLinus Torvalds { 12703ae5eaecSRussell King struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 12711da177e4SLinus Torvalds 127234e81ad5SPaul Osmialowski clk_unprepare(i2c->clk); 127334e81ad5SPaul Osmialowski 1274c62c3ca5SMark Brown pm_runtime_disable(&i2c->adap.dev); 1275c62c3ca5SMark Brown pm_runtime_disable(&pdev->dev); 1276c62c3ca5SMark Brown 127761c7cff8SBen Dooks s3c24xx_i2c_deregister_cpufreq(i2c); 127861c7cff8SBen Dooks 12795b68790cSBen Dooks i2c_del_adapter(&i2c->adap); 12805b68790cSBen Dooks 12812693ac69STomasz Figa if (pdev->dev.of_node && IS_ERR(i2c->pctrl)) 12825a5f5080SThomas Abraham s3c24xx_i2c_dt_gpio_free(i2c); 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds return 0; 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds 12872935e0e0SMark Brown #ifdef CONFIG_PM_SLEEP 12886a6c6189SMagnus Damm static int s3c24xx_i2c_suspend_noirq(struct device *dev) 1289be44f01eSBen Dooks { 12906a6c6189SMagnus Damm struct platform_device *pdev = to_platform_device(dev); 12916a6c6189SMagnus Damm struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 12926a6c6189SMagnus Damm 1293be44f01eSBen Dooks i2c->suspended = 1; 12946a6c6189SMagnus Damm 1295a7750c3eSPankaj Dubey if (!IS_ERR(i2c->sysreg)) 1296a7750c3eSPankaj Dubey regmap_read(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, &i2c->sys_i2c_cfg); 1297a7750c3eSPankaj Dubey 1298be44f01eSBen Dooks return 0; 1299be44f01eSBen Dooks } 1300be44f01eSBen Dooks 1301b19c1959SVincent Palatin static int s3c24xx_i2c_resume_noirq(struct device *dev) 13021da177e4SLinus Torvalds { 13036a6c6189SMagnus Damm struct platform_device *pdev = to_platform_device(dev); 13046a6c6189SMagnus Damm struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 130534e81ad5SPaul Osmialowski int ret; 13061da177e4SLinus Torvalds 1307a7750c3eSPankaj Dubey if (!IS_ERR(i2c->sysreg)) 1308a7750c3eSPankaj Dubey regmap_write(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, i2c->sys_i2c_cfg); 1309a7750c3eSPankaj Dubey 131034e81ad5SPaul Osmialowski ret = clk_enable(i2c->clk); 131134e81ad5SPaul Osmialowski if (ret) 131234e81ad5SPaul Osmialowski return ret; 13131da177e4SLinus Torvalds s3c24xx_i2c_init(i2c); 131434e81ad5SPaul Osmialowski clk_disable(i2c->clk); 1315ce78cc07SOlof Johansson i2c->suspended = 0; 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds return 0; 13181da177e4SLinus Torvalds } 13192935e0e0SMark Brown #endif 13201da177e4SLinus Torvalds 13212935e0e0SMark Brown #ifdef CONFIG_PM 132247145210SAlexey Dobriyan static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = { 13232935e0e0SMark Brown #ifdef CONFIG_PM_SLEEP 13246a6c6189SMagnus Damm .suspend_noirq = s3c24xx_i2c_suspend_noirq, 1325b19c1959SVincent Palatin .resume_noirq = s3c24xx_i2c_resume_noirq, 1326b19c1959SVincent Palatin .freeze_noirq = s3c24xx_i2c_suspend_noirq, 1327b19c1959SVincent Palatin .thaw_noirq = s3c24xx_i2c_resume_noirq, 1328b19c1959SVincent Palatin .poweroff_noirq = s3c24xx_i2c_suspend_noirq, 1329b19c1959SVincent Palatin .restore_noirq = s3c24xx_i2c_resume_noirq, 13302935e0e0SMark Brown #endif 13316a6c6189SMagnus Damm }; 13326a6c6189SMagnus Damm 13336a6c6189SMagnus Damm #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops) 13341da177e4SLinus Torvalds #else 13356a6c6189SMagnus Damm #define S3C24XX_DEV_PM_OPS NULL 13361da177e4SLinus Torvalds #endif 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds /* device driver for platform bus bits */ 13391da177e4SLinus Torvalds 13407d85ccd8SBen Dooks static struct platform_driver s3c24xx_i2c_driver = { 13411da177e4SLinus Torvalds .probe = s3c24xx_i2c_probe, 13421da177e4SLinus Torvalds .remove = s3c24xx_i2c_remove, 13437d85ccd8SBen Dooks .id_table = s3c24xx_driver_ids, 13443ae5eaecSRussell King .driver = { 13457d85ccd8SBen Dooks .name = "s3c-i2c", 13466a6c6189SMagnus Damm .pm = S3C24XX_DEV_PM_OPS, 13479df7eadfSKarol Lewandowski .of_match_table = of_match_ptr(s3c24xx_i2c_match), 13483ae5eaecSRussell King }, 13491da177e4SLinus Torvalds }; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds static int __init i2c_adap_s3c_init(void) 13521da177e4SLinus Torvalds { 13537d85ccd8SBen Dooks return platform_driver_register(&s3c24xx_i2c_driver); 13541da177e4SLinus Torvalds } 135518dc83a6SMark Brown subsys_initcall(i2c_adap_s3c_init); 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds static void __exit i2c_adap_s3c_exit(void) 13581da177e4SLinus Torvalds { 13597d85ccd8SBen Dooks platform_driver_unregister(&s3c24xx_i2c_driver); 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds module_exit(i2c_adap_s3c_exit); 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds MODULE_DESCRIPTION("S3C24XX I2C Bus driver"); 13641da177e4SLinus Torvalds MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); 13651da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1366