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 * You should have received a copy of the GNU General Public License 191da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 201da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <linux/kernel.h> 241da177e4SLinus Torvalds #include <linux/module.h> 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds #include <linux/i2c.h> 271da177e4SLinus Torvalds #include <linux/init.h> 281da177e4SLinus Torvalds #include <linux/time.h> 291da177e4SLinus Torvalds #include <linux/interrupt.h> 301da177e4SLinus Torvalds #include <linux/delay.h> 311da177e4SLinus Torvalds #include <linux/errno.h> 321da177e4SLinus Torvalds #include <linux/err.h> 33d052d1beSRussell King #include <linux/platform_device.h> 34c62c3ca5SMark Brown #include <linux/pm_runtime.h> 35f8ce2547SRussell King #include <linux/clk.h> 3661c7cff8SBen Dooks #include <linux/cpufreq.h> 375a0e3ad6STejun Heo #include <linux/slab.h> 3821782180SH Hartley Sweeten #include <linux/io.h> 395a5f5080SThomas Abraham #include <linux/of_i2c.h> 405a5f5080SThomas Abraham #include <linux/of_gpio.h> 412693ac69STomasz Figa #include <linux/pinctrl/consumer.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #include <asm/irq.h> 441da177e4SLinus Torvalds 45436d42c6SArnd Bergmann #include <linux/platform_data/i2c-s3c2410.h> 461da177e4SLinus Torvalds 47*e636602aSHeiko Stübner /* see s3c2410x user guide, v1.1, section 9 (p447) for more info */ 48*e636602aSHeiko Stübner 49*e636602aSHeiko Stübner #define S3C2410_IICREG(x) (x) 50*e636602aSHeiko Stübner 51*e636602aSHeiko Stübner #define S3C2410_IICCON S3C2410_IICREG(0x00) 52*e636602aSHeiko Stübner #define S3C2410_IICSTAT S3C2410_IICREG(0x04) 53*e636602aSHeiko Stübner #define S3C2410_IICADD S3C2410_IICREG(0x08) 54*e636602aSHeiko Stübner #define S3C2410_IICDS S3C2410_IICREG(0x0C) 55*e636602aSHeiko Stübner #define S3C2440_IICLC S3C2410_IICREG(0x10) 56*e636602aSHeiko Stübner 57*e636602aSHeiko Stübner #define S3C2410_IICCON_ACKEN (1<<7) 58*e636602aSHeiko Stübner #define S3C2410_IICCON_TXDIV_16 (0<<6) 59*e636602aSHeiko Stübner #define S3C2410_IICCON_TXDIV_512 (1<<6) 60*e636602aSHeiko Stübner #define S3C2410_IICCON_IRQEN (1<<5) 61*e636602aSHeiko Stübner #define S3C2410_IICCON_IRQPEND (1<<4) 62*e636602aSHeiko Stübner #define S3C2410_IICCON_SCALE(x) ((x)&15) 63*e636602aSHeiko Stübner #define S3C2410_IICCON_SCALEMASK (0xf) 64*e636602aSHeiko Stübner 65*e636602aSHeiko Stübner #define S3C2410_IICSTAT_MASTER_RX (2<<6) 66*e636602aSHeiko Stübner #define S3C2410_IICSTAT_MASTER_TX (3<<6) 67*e636602aSHeiko Stübner #define S3C2410_IICSTAT_SLAVE_RX (0<<6) 68*e636602aSHeiko Stübner #define S3C2410_IICSTAT_SLAVE_TX (1<<6) 69*e636602aSHeiko Stübner #define S3C2410_IICSTAT_MODEMASK (3<<6) 70*e636602aSHeiko Stübner 71*e636602aSHeiko Stübner #define S3C2410_IICSTAT_START (1<<5) 72*e636602aSHeiko Stübner #define S3C2410_IICSTAT_BUSBUSY (1<<5) 73*e636602aSHeiko Stübner #define S3C2410_IICSTAT_TXRXEN (1<<4) 74*e636602aSHeiko Stübner #define S3C2410_IICSTAT_ARBITR (1<<3) 75*e636602aSHeiko Stübner #define S3C2410_IICSTAT_ASSLAVE (1<<2) 76*e636602aSHeiko Stübner #define S3C2410_IICSTAT_ADDR0 (1<<1) 77*e636602aSHeiko Stübner #define S3C2410_IICSTAT_LASTBIT (1<<0) 78*e636602aSHeiko Stübner 79*e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY0 (0 << 0) 80*e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY5 (1 << 0) 81*e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY10 (2 << 0) 82*e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY15 (3 << 0) 83*e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY_MASK (3 << 0) 84*e636602aSHeiko Stübner 85*e636602aSHeiko Stübner #define S3C2410_IICLC_FILTER_ON (1<<2) 86*e636602aSHeiko Stübner 8727452498SKarol Lewandowski /* Treat S3C2410 as baseline hardware, anything else is supported via quirks */ 8827452498SKarol Lewandowski #define QUIRK_S3C2440 (1 << 0) 89ec39ef83SKarol Lewandowski #define QUIRK_HDMIPHY (1 << 1) 90ec39ef83SKarol Lewandowski #define QUIRK_NO_GPIO (1 << 2) 911da177e4SLinus Torvalds 92fe724bf9SDaniel Kurtz /* Max time to wait for bus to become idle after a xfer (in us) */ 93fe724bf9SDaniel Kurtz #define S3C2410_IDLE_TIMEOUT 5000 94fe724bf9SDaniel Kurtz 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; 10627452498SKarol Lewandowski unsigned int 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; 12861c7cff8SBen Dooks #ifdef CONFIG_CPU_FREQ 12961c7cff8SBen Dooks struct notifier_block freq_transition; 13061c7cff8SBen Dooks #endif 1311da177e4SLinus Torvalds }; 1321da177e4SLinus Torvalds 13327452498SKarol Lewandowski static struct platform_device_id s3c24xx_driver_ids[] = { 1341da177e4SLinus Torvalds { 13527452498SKarol Lewandowski .name = "s3c2410-i2c", 13627452498SKarol Lewandowski .driver_data = 0, 13727452498SKarol Lewandowski }, { 13827452498SKarol Lewandowski .name = "s3c2440-i2c", 13927452498SKarol Lewandowski .driver_data = QUIRK_S3C2440, 140ec39ef83SKarol Lewandowski }, { 141ec39ef83SKarol Lewandowski .name = "s3c2440-hdmiphy-i2c", 142ec39ef83SKarol Lewandowski .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO, 14327452498SKarol Lewandowski }, { }, 14427452498SKarol Lewandowski }; 14527452498SKarol Lewandowski MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids); 1461da177e4SLinus Torvalds 1475a5f5080SThomas Abraham #ifdef CONFIG_OF 14827452498SKarol Lewandowski static const struct of_device_id s3c24xx_i2c_match[] = { 14927452498SKarol Lewandowski { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 }, 15027452498SKarol Lewandowski { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 }, 151ec39ef83SKarol Lewandowski { .compatible = "samsung,s3c2440-hdmiphy-i2c", 152ec39ef83SKarol Lewandowski .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) }, 153faf93ff6SGiridhar Maruthy { .compatible = "samsung,exynos5440-i2c", 154faf93ff6SGiridhar Maruthy .data = (void *)(QUIRK_S3C2440 | QUIRK_NO_GPIO) }, 15527452498SKarol Lewandowski {}, 15627452498SKarol Lewandowski }; 15727452498SKarol Lewandowski MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match); 1585a5f5080SThomas Abraham #endif 1595a5f5080SThomas Abraham 16027452498SKarol Lewandowski /* s3c24xx_get_device_quirks 16127452498SKarol Lewandowski * 16227452498SKarol Lewandowski * Get controller type either from device tree or platform device variant. 16327452498SKarol Lewandowski */ 16427452498SKarol Lewandowski 16527452498SKarol Lewandowski static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev) 16627452498SKarol Lewandowski { 16727452498SKarol Lewandowski if (pdev->dev.of_node) { 16827452498SKarol Lewandowski const struct of_device_id *match; 169b900ba4cSKarol Lewandowski match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node); 17027452498SKarol Lewandowski return (unsigned int)match->data; 17127452498SKarol Lewandowski } 17227452498SKarol Lewandowski 17327452498SKarol Lewandowski return platform_get_device_id(pdev)->driver_data; 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds /* s3c24xx_i2c_master_complete 1771da177e4SLinus Torvalds * 1781da177e4SLinus Torvalds * complete the message and wake up the caller, using the given return code, 1791da177e4SLinus Torvalds * or zero to mean ok. 1801da177e4SLinus Torvalds */ 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds dev_dbg(i2c->dev, "master_complete %d\n", ret); 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds i2c->msg_ptr = 0; 1871da177e4SLinus Torvalds i2c->msg = NULL; 1881da177e4SLinus Torvalds i2c->msg_idx++; 1891da177e4SLinus Torvalds i2c->msg_num = 0; 1901da177e4SLinus Torvalds if (ret) 1911da177e4SLinus Torvalds i2c->msg_idx = ret; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds wake_up(&i2c->wait); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds unsigned long tmp; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 2011da177e4SLinus Torvalds writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c) 2051da177e4SLinus Torvalds { 2061da177e4SLinus Torvalds unsigned long tmp; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 2091da177e4SLinus Torvalds writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds /* irq enable/disable functions */ 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c) 2151da177e4SLinus Torvalds { 2161da177e4SLinus Torvalds unsigned long tmp; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 2191da177e4SLinus Torvalds writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c) 2231da177e4SLinus Torvalds { 2241da177e4SLinus Torvalds unsigned long tmp; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 2271da177e4SLinus Torvalds writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /* s3c24xx_i2c_message_start 2321da177e4SLinus Torvalds * 2331da177e4SLinus Torvalds * put the start of a message onto the bus 2341da177e4SLinus Torvalds */ 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, 2371da177e4SLinus Torvalds struct i2c_msg *msg) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds unsigned int addr = (msg->addr & 0x7f) << 1; 2401da177e4SLinus Torvalds unsigned long stat; 2411da177e4SLinus Torvalds unsigned long iiccon; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds stat = 0; 2441da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_TXRXEN; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds if (msg->flags & I2C_M_RD) { 2471da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_MASTER_RX; 2481da177e4SLinus Torvalds addr |= 1; 2491da177e4SLinus Torvalds } else 2501da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_MASTER_TX; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds if (msg->flags & I2C_M_REV_DIR_ADDR) 2531da177e4SLinus Torvalds addr ^= 1; 2541da177e4SLinus Torvalds 25548fc7f7eSAdam Buchbinder /* todo - check for whether ack wanted or not */ 2561da177e4SLinus Torvalds s3c24xx_i2c_enable_ack(i2c); 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds iiccon = readl(i2c->regs + S3C2410_IICCON); 2591da177e4SLinus Torvalds writel(stat, i2c->regs + S3C2410_IICSTAT); 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr); 2621da177e4SLinus Torvalds writeb(addr, i2c->regs + S3C2410_IICDS); 2631da177e4SLinus Torvalds 264e00a8cdfSBen Dooks /* delay here to ensure the data byte has gotten onto the bus 265e00a8cdfSBen Dooks * before the transaction is started */ 266e00a8cdfSBen Dooks 267e00a8cdfSBen Dooks ndelay(i2c->tx_setup); 268e00a8cdfSBen Dooks 2691da177e4SLinus Torvalds dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon); 2701da177e4SLinus Torvalds writel(iiccon, i2c->regs + S3C2410_IICCON); 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_START; 2731da177e4SLinus Torvalds writel(stat, i2c->regs + S3C2410_IICSTAT); 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret) 2771da177e4SLinus Torvalds { 2781da177e4SLinus Torvalds unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds dev_dbg(i2c->dev, "STOP\n"); 2811da177e4SLinus Torvalds 2820da2e776SDaniel Kurtz /* 2830da2e776SDaniel Kurtz * The datasheet says that the STOP sequence should be: 2840da2e776SDaniel Kurtz * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP') 2850da2e776SDaniel Kurtz * 2) I2CCON.4 = 0 - Clear IRQPEND 2860da2e776SDaniel Kurtz * 3) Wait until the stop condition takes effect. 2870da2e776SDaniel Kurtz * 4*) I2CSTAT.4 = 0 - Clear TXRXEN 2880da2e776SDaniel Kurtz * 2890da2e776SDaniel Kurtz * Where, step "4*" is only for buses with the "HDMIPHY" quirk. 2900da2e776SDaniel Kurtz * 2910da2e776SDaniel Kurtz * However, after much experimentation, it appears that: 2920da2e776SDaniel Kurtz * a) normal buses automatically clear BUSY and transition from 2930da2e776SDaniel Kurtz * Master->Slave when they complete generating a STOP condition. 2940da2e776SDaniel Kurtz * Therefore, step (3) can be done in doxfer() by polling I2CCON.4 2950da2e776SDaniel Kurtz * after starting the STOP generation here. 2960da2e776SDaniel Kurtz * b) HDMIPHY bus does neither, so there is no way to do step 3. 2970da2e776SDaniel Kurtz * There is no indication when this bus has finished generating 2980da2e776SDaniel Kurtz * STOP. 2990da2e776SDaniel Kurtz * 3000da2e776SDaniel Kurtz * In fact, we have found that as soon as the IRQPEND bit is cleared in 3010da2e776SDaniel Kurtz * step 2, the HDMIPHY bus generates the STOP condition, and then 3020da2e776SDaniel Kurtz * immediately starts transferring another data byte, even though the 3030da2e776SDaniel Kurtz * bus is supposedly stopped. This is presumably because the bus is 3040da2e776SDaniel Kurtz * still in "Master" mode, and its BUSY bit is still set. 3050da2e776SDaniel Kurtz * 3060da2e776SDaniel Kurtz * To avoid these extra post-STOP transactions on HDMI phy devices, we 3070da2e776SDaniel Kurtz * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly, 3080da2e776SDaniel Kurtz * instead of first generating a proper STOP condition. This should 3090da2e776SDaniel Kurtz * float SDA & SCK terminating the transfer. Subsequent transfers 3100da2e776SDaniel Kurtz * start with a proper START condition, and proceed normally. 3110da2e776SDaniel Kurtz * 3120da2e776SDaniel Kurtz * The HDMIPHY bus is an internal bus that always has exactly two 3130da2e776SDaniel Kurtz * devices, the host as Master and the HDMIPHY device as the slave. 3140da2e776SDaniel Kurtz * Skipping the STOP condition has been tested on this bus and works. 3150da2e776SDaniel Kurtz */ 3160da2e776SDaniel Kurtz if (i2c->quirks & QUIRK_HDMIPHY) { 3170da2e776SDaniel Kurtz /* Stop driving the I2C pins */ 3180da2e776SDaniel Kurtz iicstat &= ~S3C2410_IICSTAT_TXRXEN; 3190da2e776SDaniel Kurtz } else { 3201da177e4SLinus Torvalds /* stop the transfer */ 3211da177e4SLinus Torvalds iicstat &= ~S3C2410_IICSTAT_START; 3220da2e776SDaniel Kurtz } 3231da177e4SLinus Torvalds writel(iicstat, i2c->regs + S3C2410_IICSTAT); 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds i2c->state = STATE_STOP; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds s3c24xx_i2c_master_complete(i2c, ret); 3281da177e4SLinus Torvalds s3c24xx_i2c_disable_irq(i2c); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* helper functions to determine the current state in the set of 3321da177e4SLinus Torvalds * messages we are sending */ 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* is_lastmsg() 3351da177e4SLinus Torvalds * 3361da177e4SLinus Torvalds * returns TRUE if the current message is the last in the set 3371da177e4SLinus Torvalds */ 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds static inline int is_lastmsg(struct s3c24xx_i2c *i2c) 3401da177e4SLinus Torvalds { 3411da177e4SLinus Torvalds return i2c->msg_idx >= (i2c->msg_num - 1); 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds /* is_msglast 3451da177e4SLinus Torvalds * 3461da177e4SLinus Torvalds * returns TRUE if we this is the last byte in the current message 3471da177e4SLinus Torvalds */ 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds static inline int is_msglast(struct s3c24xx_i2c *i2c) 3501da177e4SLinus Torvalds { 3511da177e4SLinus Torvalds return i2c->msg_ptr == i2c->msg->len-1; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds /* is_msgend 3551da177e4SLinus Torvalds * 3561da177e4SLinus Torvalds * returns TRUE if we reached the end of the current message 3571da177e4SLinus Torvalds */ 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds static inline int is_msgend(struct s3c24xx_i2c *i2c) 3601da177e4SLinus Torvalds { 3611da177e4SLinus Torvalds return i2c->msg_ptr >= i2c->msg->len; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 36419820510SHuisung Kang /* i2c_s3c_irq_nextbyte 3651da177e4SLinus Torvalds * 3661da177e4SLinus Torvalds * process an interrupt and work out what to do 3671da177e4SLinus Torvalds */ 3681da177e4SLinus Torvalds 36919820510SHuisung Kang static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat) 3701da177e4SLinus Torvalds { 3711da177e4SLinus Torvalds unsigned long tmp; 3721da177e4SLinus Torvalds unsigned char byte; 3731da177e4SLinus Torvalds int ret = 0; 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds switch (i2c->state) { 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds case STATE_IDLE: 37808882d20SHarvey Harrison dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__); 3791da177e4SLinus Torvalds goto out; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds case STATE_STOP: 38208882d20SHarvey Harrison dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__); 3831da177e4SLinus Torvalds s3c24xx_i2c_disable_irq(i2c); 3841da177e4SLinus Torvalds goto out_ack; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds case STATE_START: 3871da177e4SLinus Torvalds /* last thing we did was send a start condition on the 3881da177e4SLinus Torvalds * bus, or started a new i2c message 3891da177e4SLinus Torvalds */ 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds if (iicstat & S3C2410_IICSTAT_LASTBIT && 3921da177e4SLinus Torvalds !(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 3931da177e4SLinus Torvalds /* ack was not received... */ 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds dev_dbg(i2c->dev, "ack was not received\n"); 39663f5c289SBen Dooks s3c24xx_i2c_stop(i2c, -ENXIO); 3971da177e4SLinus Torvalds goto out_ack; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_RD) 4011da177e4SLinus Torvalds i2c->state = STATE_READ; 4021da177e4SLinus Torvalds else 4031da177e4SLinus Torvalds i2c->state = STATE_WRITE; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* terminate the transfer if there is nothing to do 40663f5c289SBen Dooks * as this is used by the i2c probe to find devices. */ 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds if (is_lastmsg(i2c) && i2c->msg->len == 0) { 4091da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 4101da177e4SLinus Torvalds goto out_ack; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds if (i2c->state == STATE_READ) 4141da177e4SLinus Torvalds goto prepare_read; 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds /* fall through to the write state, as we will need to 4171da177e4SLinus Torvalds * send a byte as well */ 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds case STATE_WRITE: 4201da177e4SLinus Torvalds /* we are writing data to the device... check for the 4211da177e4SLinus Torvalds * end of the message, and if so, work out what to do 4221da177e4SLinus Torvalds */ 4231da177e4SLinus Torvalds 4242709781bSBen Dooks if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 4252709781bSBen Dooks if (iicstat & S3C2410_IICSTAT_LASTBIT) { 4262709781bSBen Dooks dev_dbg(i2c->dev, "WRITE: No Ack\n"); 4272709781bSBen Dooks 4282709781bSBen Dooks s3c24xx_i2c_stop(i2c, -ECONNREFUSED); 4292709781bSBen Dooks goto out_ack; 4302709781bSBen Dooks } 4312709781bSBen Dooks } 4322709781bSBen Dooks 4331da177e4SLinus Torvalds retry_write: 4342709781bSBen Dooks 4351da177e4SLinus Torvalds if (!is_msgend(i2c)) { 4361da177e4SLinus Torvalds byte = i2c->msg->buf[i2c->msg_ptr++]; 4371da177e4SLinus Torvalds writeb(byte, i2c->regs + S3C2410_IICDS); 4381da177e4SLinus Torvalds 439e00a8cdfSBen Dooks /* delay after writing the byte to allow the 440e00a8cdfSBen Dooks * data setup time on the bus, as writing the 441e00a8cdfSBen Dooks * data to the register causes the first bit 442e00a8cdfSBen Dooks * to appear on SDA, and SCL will change as 443e00a8cdfSBen Dooks * soon as the interrupt is acknowledged */ 444e00a8cdfSBen Dooks 445e00a8cdfSBen Dooks ndelay(i2c->tx_setup); 446e00a8cdfSBen Dooks 4471da177e4SLinus Torvalds } else if (!is_lastmsg(i2c)) { 4481da177e4SLinus Torvalds /* we need to go to the next i2c message */ 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds dev_dbg(i2c->dev, "WRITE: Next Message\n"); 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds i2c->msg_ptr = 0; 4531da177e4SLinus Torvalds i2c->msg_idx++; 4541da177e4SLinus Torvalds i2c->msg++; 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds /* check to see if we need to do another message */ 4571da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_NOSTART) { 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_RD) { 4601da177e4SLinus Torvalds /* cannot do this, the controller 4611da177e4SLinus Torvalds * forces us to send a new START 4621da177e4SLinus Torvalds * when we change direction */ 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, -EINVAL); 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds goto retry_write; 4681da177e4SLinus Torvalds } else { 4691da177e4SLinus Torvalds /* send the new start */ 4701da177e4SLinus Torvalds s3c24xx_i2c_message_start(i2c, i2c->msg); 4711da177e4SLinus Torvalds i2c->state = STATE_START; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds } else { 4751da177e4SLinus Torvalds /* send stop */ 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds break; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds case STATE_READ: 4821da177e4SLinus Torvalds /* we have a byte of data in the data register, do 48348fc7f7eSAdam Buchbinder * something with it, and then work out whether we are 4841da177e4SLinus Torvalds * going to do any more read/write 4851da177e4SLinus Torvalds */ 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds byte = readb(i2c->regs + S3C2410_IICDS); 4881da177e4SLinus Torvalds i2c->msg->buf[i2c->msg_ptr++] = byte; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds prepare_read: 4911da177e4SLinus Torvalds if (is_msglast(i2c)) { 4921da177e4SLinus Torvalds /* last byte of buffer */ 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds if (is_lastmsg(i2c)) 4951da177e4SLinus Torvalds s3c24xx_i2c_disable_ack(i2c); 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds } else if (is_msgend(i2c)) { 4981da177e4SLinus Torvalds /* ok, we've read the entire buffer, see if there 4991da177e4SLinus Torvalds * is anything else we need to do */ 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds if (is_lastmsg(i2c)) { 5021da177e4SLinus Torvalds /* last message, send stop and complete */ 5031da177e4SLinus Torvalds dev_dbg(i2c->dev, "READ: Send Stop\n"); 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 5061da177e4SLinus Torvalds } else { 5071da177e4SLinus Torvalds /* go to the next transfer */ 5081da177e4SLinus Torvalds dev_dbg(i2c->dev, "READ: Next Transfer\n"); 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds i2c->msg_ptr = 0; 5111da177e4SLinus Torvalds i2c->msg_idx++; 5121da177e4SLinus Torvalds i2c->msg++; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds break; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds /* acknowlegde the IRQ and get back on with the work */ 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds out_ack: 5221da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 5231da177e4SLinus Torvalds tmp &= ~S3C2410_IICCON_IRQPEND; 5241da177e4SLinus Torvalds writel(tmp, i2c->regs + S3C2410_IICCON); 5251da177e4SLinus Torvalds out: 5261da177e4SLinus Torvalds return ret; 5271da177e4SLinus Torvalds } 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds /* s3c24xx_i2c_irq 5301da177e4SLinus Torvalds * 5311da177e4SLinus Torvalds * top level IRQ servicing routine 5321da177e4SLinus Torvalds */ 5331da177e4SLinus Torvalds 5347d12e780SDavid Howells static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id) 5351da177e4SLinus Torvalds { 5361da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = dev_id; 5371da177e4SLinus Torvalds unsigned long status; 5381da177e4SLinus Torvalds unsigned long tmp; 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds status = readl(i2c->regs + S3C2410_IICSTAT); 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds if (status & S3C2410_IICSTAT_ARBITR) { 5433d0911bfSBen Dooks /* deal with arbitration loss */ 5441da177e4SLinus Torvalds dev_err(i2c->dev, "deal with arbitration loss\n"); 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds if (i2c->state == STATE_IDLE) { 5481da177e4SLinus Torvalds dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n"); 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 5511da177e4SLinus Torvalds tmp &= ~S3C2410_IICCON_IRQPEND; 5521da177e4SLinus Torvalds writel(tmp, i2c->regs + S3C2410_IICCON); 5531da177e4SLinus Torvalds goto out; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds /* pretty much this leaves us with the fact that we've 5571da177e4SLinus Torvalds * transmitted or received whatever byte we last sent */ 5581da177e4SLinus Torvalds 55919820510SHuisung Kang i2c_s3c_irq_nextbyte(i2c, status); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds out: 5621da177e4SLinus Torvalds return IRQ_HANDLED; 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds /* s3c24xx_i2c_set_master 5671da177e4SLinus Torvalds * 5681da177e4SLinus Torvalds * get the i2c bus for a master transaction 5691da177e4SLinus Torvalds */ 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c) 5721da177e4SLinus Torvalds { 5731da177e4SLinus Torvalds unsigned long iicstat; 5741da177e4SLinus Torvalds int timeout = 400; 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds while (timeout-- > 0) { 5771da177e4SLinus Torvalds iicstat = readl(i2c->regs + S3C2410_IICSTAT); 5781da177e4SLinus Torvalds 5791da177e4SLinus Torvalds if (!(iicstat & S3C2410_IICSTAT_BUSBUSY)) 5801da177e4SLinus Torvalds return 0; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds msleep(1); 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds return -ETIMEDOUT; 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 588fe724bf9SDaniel Kurtz /* s3c24xx_i2c_wait_idle 589fe724bf9SDaniel Kurtz * 590fe724bf9SDaniel Kurtz * wait for the i2c bus to become idle. 591fe724bf9SDaniel Kurtz */ 592fe724bf9SDaniel Kurtz 593fe724bf9SDaniel Kurtz static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c) 594fe724bf9SDaniel Kurtz { 595fe724bf9SDaniel Kurtz unsigned long iicstat; 596fe724bf9SDaniel Kurtz ktime_t start, now; 597fe724bf9SDaniel Kurtz unsigned long delay; 59831f313d9SMark Brown int spins; 599fe724bf9SDaniel Kurtz 600fe724bf9SDaniel Kurtz /* ensure the stop has been through the bus */ 601fe724bf9SDaniel Kurtz 602fe724bf9SDaniel Kurtz dev_dbg(i2c->dev, "waiting for bus idle\n"); 603fe724bf9SDaniel Kurtz 604fe724bf9SDaniel Kurtz start = now = ktime_get(); 605fe724bf9SDaniel Kurtz 606fe724bf9SDaniel Kurtz /* 607fe724bf9SDaniel Kurtz * Most of the time, the bus is already idle within a few usec of the 608fe724bf9SDaniel Kurtz * end of a transaction. However, really slow i2c devices can stretch 609fe724bf9SDaniel Kurtz * the clock, delaying STOP generation. 610fe724bf9SDaniel Kurtz * 61131f313d9SMark Brown * On slower SoCs this typically happens within a very small number of 61231f313d9SMark Brown * instructions so busy wait briefly to avoid scheduling overhead. 613fe724bf9SDaniel Kurtz */ 61431f313d9SMark Brown spins = 3; 615fe724bf9SDaniel Kurtz iicstat = readl(i2c->regs + S3C2410_IICSTAT); 61631f313d9SMark Brown while ((iicstat & S3C2410_IICSTAT_START) && --spins) { 61731f313d9SMark Brown cpu_relax(); 61831f313d9SMark Brown iicstat = readl(i2c->regs + S3C2410_IICSTAT); 61931f313d9SMark Brown } 62031f313d9SMark Brown 62131f313d9SMark Brown /* 62231f313d9SMark Brown * If we do get an appreciable delay as a compromise between idle 62331f313d9SMark Brown * detection latency for the normal, fast case, and system load in the 62431f313d9SMark Brown * slow device case, use an exponential back off in the polling loop, 62531f313d9SMark Brown * up to 1/10th of the total timeout, then continue to poll at a 62631f313d9SMark Brown * constant rate up to the timeout. 62731f313d9SMark Brown */ 628fe724bf9SDaniel Kurtz delay = 1; 629fe724bf9SDaniel Kurtz while ((iicstat & S3C2410_IICSTAT_START) && 630fe724bf9SDaniel Kurtz ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) { 631fe724bf9SDaniel Kurtz usleep_range(delay, 2 * delay); 632fe724bf9SDaniel Kurtz if (delay < S3C2410_IDLE_TIMEOUT / 10) 633fe724bf9SDaniel Kurtz delay <<= 1; 634fe724bf9SDaniel Kurtz now = ktime_get(); 635fe724bf9SDaniel Kurtz iicstat = readl(i2c->regs + S3C2410_IICSTAT); 636fe724bf9SDaniel Kurtz } 637fe724bf9SDaniel Kurtz 638fe724bf9SDaniel Kurtz if (iicstat & S3C2410_IICSTAT_START) 639fe724bf9SDaniel Kurtz dev_warn(i2c->dev, "timeout waiting for bus idle\n"); 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* s3c24xx_i2c_doxfer 6431da177e4SLinus Torvalds * 6441da177e4SLinus Torvalds * this starts an i2c transfer 6451da177e4SLinus Torvalds */ 6461da177e4SLinus Torvalds 6473d0911bfSBen Dooks static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, 6483d0911bfSBen Dooks struct i2c_msg *msgs, int num) 6491da177e4SLinus Torvalds { 650fe724bf9SDaniel Kurtz unsigned long timeout; 6511da177e4SLinus Torvalds int ret; 6521da177e4SLinus Torvalds 653be44f01eSBen Dooks if (i2c->suspended) 65461c7cff8SBen Dooks return -EIO; 65561c7cff8SBen Dooks 6561da177e4SLinus Torvalds ret = s3c24xx_i2c_set_master(i2c); 6571da177e4SLinus Torvalds if (ret != 0) { 6581da177e4SLinus Torvalds dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); 6591da177e4SLinus Torvalds ret = -EAGAIN; 6601da177e4SLinus Torvalds goto out; 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds i2c->msg = msgs; 6641da177e4SLinus Torvalds i2c->msg_num = num; 6651da177e4SLinus Torvalds i2c->msg_ptr = 0; 6661da177e4SLinus Torvalds i2c->msg_idx = 0; 6671da177e4SLinus Torvalds i2c->state = STATE_START; 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds s3c24xx_i2c_enable_irq(i2c); 6701da177e4SLinus Torvalds s3c24xx_i2c_message_start(i2c, msgs); 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds ret = i2c->msg_idx; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* having these next two as dev_err() makes life very 6771da177e4SLinus Torvalds * noisy when doing an i2cdetect */ 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds if (timeout == 0) 6801da177e4SLinus Torvalds dev_dbg(i2c->dev, "timeout\n"); 6811da177e4SLinus Torvalds else if (ret != num) 6821da177e4SLinus Torvalds dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); 6831da177e4SLinus Torvalds 6840da2e776SDaniel Kurtz /* For QUIRK_HDMIPHY, bus is already disabled */ 6850da2e776SDaniel Kurtz if (i2c->quirks & QUIRK_HDMIPHY) 6860da2e776SDaniel Kurtz goto out; 6871da177e4SLinus Torvalds 688fe724bf9SDaniel Kurtz s3c24xx_i2c_wait_idle(i2c); 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds out: 6911da177e4SLinus Torvalds return ret; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds /* s3c24xx_i2c_xfer 6951da177e4SLinus Torvalds * 6961da177e4SLinus Torvalds * first port of call from the i2c bus code when an message needs 69744bbe87eSSteven Cole * transferring across the i2c bus. 6981da177e4SLinus Torvalds */ 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, 7011da177e4SLinus Torvalds struct i2c_msg *msgs, int num) 7021da177e4SLinus Torvalds { 7031da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; 7041da177e4SLinus Torvalds int retry; 7051da177e4SLinus Torvalds int ret; 7061da177e4SLinus Torvalds 707c62c3ca5SMark Brown pm_runtime_get_sync(&adap->dev); 708d3b64c59SThomas Abraham clk_prepare_enable(i2c->clk); 709d2360b8eSArnaud Patard (Rtp) 7101da177e4SLinus Torvalds for (retry = 0; retry < adap->retries; retry++) { 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds ret = s3c24xx_i2c_doxfer(i2c, msgs, num); 7131da177e4SLinus Torvalds 714d2360b8eSArnaud Patard (Rtp) if (ret != -EAGAIN) { 715d3b64c59SThomas Abraham clk_disable_unprepare(i2c->clk); 716a86ae9ffSMark Brown pm_runtime_put(&adap->dev); 7171da177e4SLinus Torvalds return ret; 718d2360b8eSArnaud Patard (Rtp) } 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds udelay(100); 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds 725d3b64c59SThomas Abraham clk_disable_unprepare(i2c->clk); 726a86ae9ffSMark Brown pm_runtime_put(&adap->dev); 7271da177e4SLinus Torvalds return -EREMOTEIO; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /* declare our i2c functionality */ 7311da177e4SLinus Torvalds static u32 s3c24xx_i2c_func(struct i2c_adapter *adap) 7321da177e4SLinus Torvalds { 73314674e70SMark Brown return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART | 73414674e70SMark Brown I2C_FUNC_PROTOCOL_MANGLING; 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds /* i2c bus registration info */ 7381da177e4SLinus Torvalds 7398f9082c5SJean Delvare static const struct i2c_algorithm s3c24xx_i2c_algorithm = { 7401da177e4SLinus Torvalds .master_xfer = s3c24xx_i2c_xfer, 7411da177e4SLinus Torvalds .functionality = s3c24xx_i2c_func, 7421da177e4SLinus Torvalds }; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds /* s3c24xx_i2c_calcdivisor 7451da177e4SLinus Torvalds * 7461da177e4SLinus Torvalds * return the divisor settings for a given frequency 7471da177e4SLinus Torvalds */ 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted, 7501da177e4SLinus Torvalds unsigned int *div1, unsigned int *divs) 7511da177e4SLinus Torvalds { 7521da177e4SLinus Torvalds unsigned int calc_divs = clkin / wanted; 7531da177e4SLinus Torvalds unsigned int calc_div1; 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds if (calc_divs > (16*16)) 7561da177e4SLinus Torvalds calc_div1 = 512; 7571da177e4SLinus Torvalds else 7581da177e4SLinus Torvalds calc_div1 = 16; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds calc_divs += calc_div1-1; 7611da177e4SLinus Torvalds calc_divs /= calc_div1; 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds if (calc_divs == 0) 7641da177e4SLinus Torvalds calc_divs = 1; 7651da177e4SLinus Torvalds if (calc_divs > 17) 7661da177e4SLinus Torvalds calc_divs = 17; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds *divs = calc_divs; 7691da177e4SLinus Torvalds *div1 = calc_div1; 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds return clkin / (calc_divs * calc_div1); 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 77461c7cff8SBen Dooks /* s3c24xx_i2c_clockrate 7751da177e4SLinus Torvalds * 7761da177e4SLinus Torvalds * work out a divisor for the user requested frequency setting, 7771da177e4SLinus Torvalds * either by the requested frequency, or scanning the acceptable 7781da177e4SLinus Torvalds * range of frequencies until something is found 7791da177e4SLinus Torvalds */ 7801da177e4SLinus Torvalds 78161c7cff8SBen Dooks static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) 7821da177e4SLinus Torvalds { 7834fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata = i2c->pdata; 7841da177e4SLinus Torvalds unsigned long clkin = clk_get_rate(i2c->clk); 7851da177e4SLinus Torvalds unsigned int divs, div1; 786c564e6aeSDaniel Silverstone unsigned long target_frequency; 78761c7cff8SBen Dooks u32 iiccon; 7881da177e4SLinus Torvalds int freq; 7891da177e4SLinus Torvalds 79061c7cff8SBen Dooks i2c->clkrate = clkin; 7911da177e4SLinus Torvalds clkin /= 1000; /* clkin now in KHz */ 7921da177e4SLinus Torvalds 793c564e6aeSDaniel Silverstone dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency); 7941da177e4SLinus Torvalds 795c564e6aeSDaniel Silverstone target_frequency = pdata->frequency ? pdata->frequency : 100000; 7961da177e4SLinus Torvalds 797c564e6aeSDaniel Silverstone target_frequency /= 1000; /* Target frequency now in KHz */ 7981da177e4SLinus Torvalds 799c564e6aeSDaniel Silverstone freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs); 8001da177e4SLinus Torvalds 801c564e6aeSDaniel Silverstone if (freq > target_frequency) { 802c564e6aeSDaniel Silverstone dev_err(i2c->dev, 803c564e6aeSDaniel Silverstone "Unable to achieve desired frequency %luKHz." \ 804c564e6aeSDaniel Silverstone " Lowest achievable %dKHz\n", target_frequency, freq); 8051da177e4SLinus Torvalds return -EINVAL; 806c564e6aeSDaniel Silverstone } 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds *got = freq; 80961c7cff8SBen Dooks 81061c7cff8SBen Dooks iiccon = readl(i2c->regs + S3C2410_IICCON); 81161c7cff8SBen Dooks iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512); 81261c7cff8SBen Dooks iiccon |= (divs-1); 81361c7cff8SBen Dooks 81461c7cff8SBen Dooks if (div1 == 512) 81561c7cff8SBen Dooks iiccon |= S3C2410_IICCON_TXDIV_512; 81661c7cff8SBen Dooks 81761c7cff8SBen Dooks writel(iiccon, i2c->regs + S3C2410_IICCON); 81861c7cff8SBen Dooks 81927452498SKarol Lewandowski if (i2c->quirks & QUIRK_S3C2440) { 820a192f715SBen Dooks unsigned long sda_delay; 821a192f715SBen Dooks 822a192f715SBen Dooks if (pdata->sda_delay) { 8237031307aSMyungJoo Ham sda_delay = clkin * pdata->sda_delay; 8247031307aSMyungJoo Ham sda_delay = DIV_ROUND_UP(sda_delay, 1000000); 825a192f715SBen Dooks sda_delay = DIV_ROUND_UP(sda_delay, 5); 826a192f715SBen Dooks if (sda_delay > 3) 827a192f715SBen Dooks sda_delay = 3; 828a192f715SBen Dooks sda_delay |= S3C2410_IICLC_FILTER_ON; 829a192f715SBen Dooks } else 830a192f715SBen Dooks sda_delay = 0; 831a192f715SBen Dooks 832a192f715SBen Dooks dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay); 833a192f715SBen Dooks writel(sda_delay, i2c->regs + S3C2440_IICLC); 834a192f715SBen Dooks } 835a192f715SBen Dooks 8361da177e4SLinus Torvalds return 0; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 83961c7cff8SBen Dooks #ifdef CONFIG_CPU_FREQ 84061c7cff8SBen Dooks 84161c7cff8SBen Dooks #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition) 84261c7cff8SBen Dooks 84361c7cff8SBen Dooks static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb, 84461c7cff8SBen Dooks unsigned long val, void *data) 84561c7cff8SBen Dooks { 84661c7cff8SBen Dooks struct s3c24xx_i2c *i2c = freq_to_i2c(nb); 84761c7cff8SBen Dooks unsigned int got; 84861c7cff8SBen Dooks int delta_f; 84961c7cff8SBen Dooks int ret; 85061c7cff8SBen Dooks 85161c7cff8SBen Dooks delta_f = clk_get_rate(i2c->clk) - i2c->clkrate; 85261c7cff8SBen Dooks 85361c7cff8SBen Dooks /* if we're post-change and the input clock has slowed down 85461c7cff8SBen Dooks * or at pre-change and the clock is about to speed up, then 85561c7cff8SBen Dooks * adjust our clock rate. <0 is slow, >0 speedup. 85661c7cff8SBen Dooks */ 85761c7cff8SBen Dooks 85861c7cff8SBen Dooks if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) || 85961c7cff8SBen Dooks (val == CPUFREQ_PRECHANGE && delta_f > 0)) { 8609bcd04bfSDaniel Kurtz i2c_lock_adapter(&i2c->adap); 86161c7cff8SBen Dooks ret = s3c24xx_i2c_clockrate(i2c, &got); 8629bcd04bfSDaniel Kurtz i2c_unlock_adapter(&i2c->adap); 86361c7cff8SBen Dooks 86461c7cff8SBen Dooks if (ret < 0) 86561c7cff8SBen Dooks dev_err(i2c->dev, "cannot find frequency\n"); 86661c7cff8SBen Dooks else 86761c7cff8SBen Dooks dev_info(i2c->dev, "setting freq %d\n", got); 86861c7cff8SBen Dooks } 86961c7cff8SBen Dooks 87061c7cff8SBen Dooks return 0; 87161c7cff8SBen Dooks } 87261c7cff8SBen Dooks 87361c7cff8SBen Dooks static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) 87461c7cff8SBen Dooks { 87561c7cff8SBen Dooks i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition; 87661c7cff8SBen Dooks 87761c7cff8SBen Dooks return cpufreq_register_notifier(&i2c->freq_transition, 87861c7cff8SBen Dooks CPUFREQ_TRANSITION_NOTIFIER); 87961c7cff8SBen Dooks } 88061c7cff8SBen Dooks 88161c7cff8SBen Dooks static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) 88261c7cff8SBen Dooks { 88361c7cff8SBen Dooks cpufreq_unregister_notifier(&i2c->freq_transition, 88461c7cff8SBen Dooks CPUFREQ_TRANSITION_NOTIFIER); 88561c7cff8SBen Dooks } 88661c7cff8SBen Dooks 88761c7cff8SBen Dooks #else 88861c7cff8SBen Dooks static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) 88961c7cff8SBen Dooks { 89061c7cff8SBen Dooks return 0; 89161c7cff8SBen Dooks } 89261c7cff8SBen Dooks 89361c7cff8SBen Dooks static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) 89461c7cff8SBen Dooks { 89561c7cff8SBen Dooks } 89661c7cff8SBen Dooks #endif 89761c7cff8SBen Dooks 8985a5f5080SThomas Abraham #ifdef CONFIG_OF 8995a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) 9005a5f5080SThomas Abraham { 9015a5f5080SThomas Abraham int idx, gpio, ret; 9025a5f5080SThomas Abraham 903ec39ef83SKarol Lewandowski if (i2c->quirks & QUIRK_NO_GPIO) 904ec39ef83SKarol Lewandowski return 0; 905ec39ef83SKarol Lewandowski 9065a5f5080SThomas Abraham for (idx = 0; idx < 2; idx++) { 9075a5f5080SThomas Abraham gpio = of_get_gpio(i2c->dev->of_node, idx); 9085a5f5080SThomas Abraham if (!gpio_is_valid(gpio)) { 9095a5f5080SThomas Abraham dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio); 9105a5f5080SThomas Abraham goto free_gpio; 9115a5f5080SThomas Abraham } 912963f2076SAbhilash Kesavan i2c->gpios[idx] = gpio; 9135a5f5080SThomas Abraham 9145a5f5080SThomas Abraham ret = gpio_request(gpio, "i2c-bus"); 9155a5f5080SThomas Abraham if (ret) { 9165a5f5080SThomas Abraham dev_err(i2c->dev, "gpio [%d] request failed\n", gpio); 9175a5f5080SThomas Abraham goto free_gpio; 9185a5f5080SThomas Abraham } 9195a5f5080SThomas Abraham } 9205a5f5080SThomas Abraham return 0; 9215a5f5080SThomas Abraham 9225a5f5080SThomas Abraham free_gpio: 9235a5f5080SThomas Abraham while (--idx >= 0) 9245a5f5080SThomas Abraham gpio_free(i2c->gpios[idx]); 9255a5f5080SThomas Abraham return -EINVAL; 9265a5f5080SThomas Abraham } 9275a5f5080SThomas Abraham 9285a5f5080SThomas Abraham static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c) 9295a5f5080SThomas Abraham { 9305a5f5080SThomas Abraham unsigned int idx; 931ec39ef83SKarol Lewandowski 932ec39ef83SKarol Lewandowski if (i2c->quirks & QUIRK_NO_GPIO) 933ec39ef83SKarol Lewandowski return; 934ec39ef83SKarol Lewandowski 9355a5f5080SThomas Abraham for (idx = 0; idx < 2; idx++) 9365a5f5080SThomas Abraham gpio_free(i2c->gpios[idx]); 9375a5f5080SThomas Abraham } 9385a5f5080SThomas Abraham #else 9395a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) 9405a5f5080SThomas Abraham { 9418ebe661dSTushar Behera return 0; 9425a5f5080SThomas Abraham } 9435a5f5080SThomas Abraham 9445a5f5080SThomas Abraham static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c) 9455a5f5080SThomas Abraham { 9465a5f5080SThomas Abraham } 9475a5f5080SThomas Abraham #endif 9485a5f5080SThomas Abraham 9491da177e4SLinus Torvalds /* s3c24xx_i2c_init 9501da177e4SLinus Torvalds * 9511da177e4SLinus Torvalds * initialise the controller, set the IO lines and frequency 9521da177e4SLinus Torvalds */ 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) 9551da177e4SLinus Torvalds { 9561da177e4SLinus Torvalds unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN; 9571da177e4SLinus Torvalds struct s3c2410_platform_i2c *pdata; 9581da177e4SLinus Torvalds unsigned int freq; 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /* get the plafrom data */ 9611da177e4SLinus Torvalds 9624fd81eb2SThomas Abraham pdata = i2c->pdata; 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds /* write slave address */ 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD); 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); 9691da177e4SLinus Torvalds 97061c7cff8SBen Dooks writel(iicon, i2c->regs + S3C2410_IICCON); 97161c7cff8SBen Dooks 9721da177e4SLinus Torvalds /* we need to work out the divisors for the clock... */ 9731da177e4SLinus Torvalds 97461c7cff8SBen Dooks if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { 97561c7cff8SBen Dooks writel(0, i2c->regs + S3C2410_IICCON); 9761da177e4SLinus Torvalds dev_err(i2c->dev, "cannot meet bus frequency required\n"); 9771da177e4SLinus Torvalds return -EINVAL; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* todo - check that the i2c lines aren't being dragged anywhere */ 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); 9831da177e4SLinus Torvalds dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon); 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds return 0; 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 9885a5f5080SThomas Abraham #ifdef CONFIG_OF 9895a5f5080SThomas Abraham /* s3c24xx_i2c_parse_dt 9905a5f5080SThomas Abraham * 9915a5f5080SThomas Abraham * Parse the device tree node and retreive the platform data. 9925a5f5080SThomas Abraham */ 9935a5f5080SThomas Abraham 9945a5f5080SThomas Abraham static void 9955a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) 9965a5f5080SThomas Abraham { 9975a5f5080SThomas Abraham struct s3c2410_platform_i2c *pdata = i2c->pdata; 9985a5f5080SThomas Abraham 9995a5f5080SThomas Abraham if (!np) 10005a5f5080SThomas Abraham return; 10015a5f5080SThomas Abraham 10025a5f5080SThomas Abraham pdata->bus_num = -1; /* i2c bus number is dynamically assigned */ 10035a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay); 10045a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr); 10055a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-max-bus-freq", 10065a5f5080SThomas Abraham (u32 *)&pdata->frequency); 10075a5f5080SThomas Abraham } 10085a5f5080SThomas Abraham #else 10095a5f5080SThomas Abraham static void 10105a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) 10115a5f5080SThomas Abraham { 10125a5f5080SThomas Abraham return; 10135a5f5080SThomas Abraham } 10145a5f5080SThomas Abraham #endif 10155a5f5080SThomas Abraham 10161da177e4SLinus Torvalds /* s3c24xx_i2c_probe 10171da177e4SLinus Torvalds * 10181da177e4SLinus Torvalds * called by the bus driver when a suitable device is found 10191da177e4SLinus Torvalds */ 10201da177e4SLinus Torvalds 10213ae5eaecSRussell King static int s3c24xx_i2c_probe(struct platform_device *pdev) 10221da177e4SLinus Torvalds { 1023692acbd3SBen Dooks struct s3c24xx_i2c *i2c; 10244fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata = NULL; 10251da177e4SLinus Torvalds struct resource *res; 10261da177e4SLinus Torvalds int ret; 10271da177e4SLinus Torvalds 10285a5f5080SThomas Abraham if (!pdev->dev.of_node) { 10296a039cabSBen Dooks pdata = pdev->dev.platform_data; 10306a039cabSBen Dooks if (!pdata) { 10316a039cabSBen Dooks dev_err(&pdev->dev, "no platform data\n"); 10326a039cabSBen Dooks return -EINVAL; 10336a039cabSBen Dooks } 10345a5f5080SThomas Abraham } 1035399dee23SBen Dooks 10364ea1557fSMark Brown i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL); 1037692acbd3SBen Dooks if (!i2c) { 1038692acbd3SBen Dooks dev_err(&pdev->dev, "no memory for state\n"); 1039692acbd3SBen Dooks return -ENOMEM; 1040692acbd3SBen Dooks } 1041692acbd3SBen Dooks 10424fd81eb2SThomas Abraham i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); 10434fd81eb2SThomas Abraham if (!i2c->pdata) { 1044669da30dSTushar Behera dev_err(&pdev->dev, "no memory for platform data\n"); 1045669da30dSTushar Behera return -ENOMEM; 10464fd81eb2SThomas Abraham } 10474fd81eb2SThomas Abraham 104827452498SKarol Lewandowski i2c->quirks = s3c24xx_get_device_quirks(pdev); 10494fd81eb2SThomas Abraham if (pdata) 10504fd81eb2SThomas Abraham memcpy(i2c->pdata, pdata, sizeof(*pdata)); 10515a5f5080SThomas Abraham else 10525a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c); 10534fd81eb2SThomas Abraham 1054692acbd3SBen Dooks strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); 1055692acbd3SBen Dooks i2c->adap.owner = THIS_MODULE; 1056692acbd3SBen Dooks i2c->adap.algo = &s3c24xx_i2c_algorithm; 1057692acbd3SBen Dooks i2c->adap.retries = 2; 1058692acbd3SBen Dooks i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD; 1059692acbd3SBen Dooks i2c->tx_setup = 50; 1060692acbd3SBen Dooks 1061692acbd3SBen Dooks init_waitqueue_head(&i2c->wait); 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds /* find the clock and enable it */ 10641da177e4SLinus Torvalds 10653ae5eaecSRussell King i2c->dev = &pdev->dev; 10662b255b94STushar Behera i2c->clk = devm_clk_get(&pdev->dev, "i2c"); 10671da177e4SLinus Torvalds if (IS_ERR(i2c->clk)) { 10683ae5eaecSRussell King dev_err(&pdev->dev, "cannot get clock\n"); 1069669da30dSTushar Behera return -ENOENT; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 10723ae5eaecSRussell King dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk); 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds /* map the registers */ 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 10781da177e4SLinus Torvalds if (res == NULL) { 10793ae5eaecSRussell King dev_err(&pdev->dev, "cannot find IO resource\n"); 1080d16933b3STushar Behera return -ENOENT; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 108384dbf809SThierry Reding i2c->regs = devm_ioremap_resource(&pdev->dev, res); 10841da177e4SLinus Torvalds 108552caa59eSLinus Torvalds if (IS_ERR(i2c->regs)) 108652caa59eSLinus Torvalds return PTR_ERR(i2c->regs); 10871da177e4SLinus Torvalds 1088a72ad456SMark Brown dev_dbg(&pdev->dev, "registers %p (%p)\n", 1089a72ad456SMark Brown i2c->regs, res); 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds /* setup info block for the i2c core */ 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds i2c->adap.algo_data = i2c; 10943ae5eaecSRussell King i2c->adap.dev.parent = &pdev->dev; 10951da177e4SLinus Torvalds 10962693ac69STomasz Figa i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev); 10972693ac69STomasz Figa 1098658122feSAbhilash Kesavan /* inititalise the i2c gpio lines */ 1099658122feSAbhilash Kesavan 1100658122feSAbhilash Kesavan if (i2c->pdata->cfg_gpio) { 1101658122feSAbhilash Kesavan i2c->pdata->cfg_gpio(to_platform_device(i2c->dev)); 1102658122feSAbhilash Kesavan } else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) { 1103d16933b3STushar Behera return -EINVAL; 1104658122feSAbhilash Kesavan } 1105658122feSAbhilash Kesavan 11061da177e4SLinus Torvalds /* initialise the i2c controller */ 11071da177e4SLinus Torvalds 1108d16933b3STushar Behera clk_prepare_enable(i2c->clk); 11091da177e4SLinus Torvalds ret = s3c24xx_i2c_init(i2c); 1110d16933b3STushar Behera clk_disable_unprepare(i2c->clk); 1111d16933b3STushar Behera if (ret != 0) { 1112d16933b3STushar Behera dev_err(&pdev->dev, "I2C controller init failed\n"); 1113d16933b3STushar Behera return ret; 1114d16933b3STushar Behera } 11151da177e4SLinus Torvalds /* find the IRQ for this unit (note, this relies on the init call to 11161da177e4SLinus Torvalds * ensure no current IRQs pending 11171da177e4SLinus Torvalds */ 11181da177e4SLinus Torvalds 1119e0d1ec97SBen Dooks i2c->irq = ret = platform_get_irq(pdev, 0); 1120e0d1ec97SBen Dooks if (ret <= 0) { 11213ae5eaecSRussell King dev_err(&pdev->dev, "cannot find IRQ\n"); 1122d16933b3STushar Behera return ret; 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 11252b255b94STushar Behera ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 0, 1126e0d1ec97SBen Dooks dev_name(&pdev->dev), i2c); 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds if (ret != 0) { 1129e0d1ec97SBen Dooks dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); 1130d16933b3STushar Behera return ret; 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 113361c7cff8SBen Dooks ret = s3c24xx_i2c_register_cpufreq(i2c); 113461c7cff8SBen Dooks if (ret < 0) { 113561c7cff8SBen Dooks dev_err(&pdev->dev, "failed to register cpufreq notifier\n"); 1136d16933b3STushar Behera return ret; 113761c7cff8SBen Dooks } 113861c7cff8SBen Dooks 1139399dee23SBen Dooks /* Note, previous versions of the driver used i2c_add_adapter() 1140399dee23SBen Dooks * to add the bus at any number. We now pass the bus number via 1141399dee23SBen Dooks * the platform data, so if unset it will now default to always 1142399dee23SBen Dooks * being bus 0. 1143399dee23SBen Dooks */ 1144399dee23SBen Dooks 11454fd81eb2SThomas Abraham i2c->adap.nr = i2c->pdata->bus_num; 11465a5f5080SThomas Abraham i2c->adap.dev.of_node = pdev->dev.of_node; 1147399dee23SBen Dooks 1148399dee23SBen Dooks ret = i2c_add_numbered_adapter(&i2c->adap); 11491da177e4SLinus Torvalds if (ret < 0) { 11503ae5eaecSRussell King dev_err(&pdev->dev, "failed to add bus to i2c core\n"); 1151dc6fea44STushar Behera s3c24xx_i2c_deregister_cpufreq(i2c); 1152dc6fea44STushar Behera return ret; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 11555a5f5080SThomas Abraham of_i2c_register_devices(&i2c->adap); 11563ae5eaecSRussell King platform_set_drvdata(pdev, i2c); 11571da177e4SLinus Torvalds 1158c62c3ca5SMark Brown pm_runtime_enable(&pdev->dev); 1159c62c3ca5SMark Brown pm_runtime_enable(&i2c->adap.dev); 1160c62c3ca5SMark Brown 116122e965c2SJean Delvare dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev)); 11625b68790cSBen Dooks return 0; 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds /* s3c24xx_i2c_remove 11661da177e4SLinus Torvalds * 11671da177e4SLinus Torvalds * called when device is removed from the bus 11681da177e4SLinus Torvalds */ 11691da177e4SLinus Torvalds 11703ae5eaecSRussell King static int s3c24xx_i2c_remove(struct platform_device *pdev) 11711da177e4SLinus Torvalds { 11723ae5eaecSRussell King struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 11731da177e4SLinus Torvalds 1174c62c3ca5SMark Brown pm_runtime_disable(&i2c->adap.dev); 1175c62c3ca5SMark Brown pm_runtime_disable(&pdev->dev); 1176c62c3ca5SMark Brown 117761c7cff8SBen Dooks s3c24xx_i2c_deregister_cpufreq(i2c); 117861c7cff8SBen Dooks 11795b68790cSBen Dooks i2c_del_adapter(&i2c->adap); 11805b68790cSBen Dooks 1181d3b64c59SThomas Abraham clk_disable_unprepare(i2c->clk); 11825b68790cSBen Dooks 11832693ac69STomasz Figa if (pdev->dev.of_node && IS_ERR(i2c->pctrl)) 11845a5f5080SThomas Abraham s3c24xx_i2c_dt_gpio_free(i2c); 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds return 0; 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds 11892935e0e0SMark Brown #ifdef CONFIG_PM_SLEEP 11906a6c6189SMagnus Damm static int s3c24xx_i2c_suspend_noirq(struct device *dev) 1191be44f01eSBen Dooks { 11926a6c6189SMagnus Damm struct platform_device *pdev = to_platform_device(dev); 11936a6c6189SMagnus Damm struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 11946a6c6189SMagnus Damm 1195be44f01eSBen Dooks i2c->suspended = 1; 11966a6c6189SMagnus Damm 1197be44f01eSBen Dooks return 0; 1198be44f01eSBen Dooks } 1199be44f01eSBen Dooks 12006a6c6189SMagnus Damm static int s3c24xx_i2c_resume(struct device *dev) 12011da177e4SLinus Torvalds { 12026a6c6189SMagnus Damm struct platform_device *pdev = to_platform_device(dev); 12036a6c6189SMagnus Damm struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 12041da177e4SLinus Torvalds 1205be44f01eSBen Dooks i2c->suspended = 0; 1206d3b64c59SThomas Abraham clk_prepare_enable(i2c->clk); 12071da177e4SLinus Torvalds s3c24xx_i2c_init(i2c); 1208d3b64c59SThomas Abraham clk_disable_unprepare(i2c->clk); 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds return 0; 12111da177e4SLinus Torvalds } 12122935e0e0SMark Brown #endif 12131da177e4SLinus Torvalds 12142935e0e0SMark Brown #ifdef CONFIG_PM 121547145210SAlexey Dobriyan static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = { 12162935e0e0SMark Brown #ifdef CONFIG_PM_SLEEP 12176a6c6189SMagnus Damm .suspend_noirq = s3c24xx_i2c_suspend_noirq, 12186a6c6189SMagnus Damm .resume = s3c24xx_i2c_resume, 12192935e0e0SMark Brown #endif 12206a6c6189SMagnus Damm }; 12216a6c6189SMagnus Damm 12226a6c6189SMagnus Damm #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops) 12231da177e4SLinus Torvalds #else 12246a6c6189SMagnus Damm #define S3C24XX_DEV_PM_OPS NULL 12251da177e4SLinus Torvalds #endif 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds /* device driver for platform bus bits */ 12281da177e4SLinus Torvalds 12297d85ccd8SBen Dooks static struct platform_driver s3c24xx_i2c_driver = { 12301da177e4SLinus Torvalds .probe = s3c24xx_i2c_probe, 12311da177e4SLinus Torvalds .remove = s3c24xx_i2c_remove, 12327d85ccd8SBen Dooks .id_table = s3c24xx_driver_ids, 12333ae5eaecSRussell King .driver = { 12343ae5eaecSRussell King .owner = THIS_MODULE, 12357d85ccd8SBen Dooks .name = "s3c-i2c", 12366a6c6189SMagnus Damm .pm = S3C24XX_DEV_PM_OPS, 12379df7eadfSKarol Lewandowski .of_match_table = of_match_ptr(s3c24xx_i2c_match), 12383ae5eaecSRussell King }, 12391da177e4SLinus Torvalds }; 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds static int __init i2c_adap_s3c_init(void) 12421da177e4SLinus Torvalds { 12437d85ccd8SBen Dooks return platform_driver_register(&s3c24xx_i2c_driver); 12441da177e4SLinus Torvalds } 124518dc83a6SMark Brown subsys_initcall(i2c_adap_s3c_init); 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds static void __exit i2c_adap_s3c_exit(void) 12481da177e4SLinus Torvalds { 12497d85ccd8SBen Dooks platform_driver_unregister(&s3c24xx_i2c_driver); 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds module_exit(i2c_adap_s3c_exit); 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds MODULE_DESCRIPTION("S3C24XX I2C Bus driver"); 12541da177e4SLinus Torvalds MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); 12551da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1256