11da177e4SLinus Torvalds /* linux/drivers/i2c/busses/i2c-s3c2410.c 21da177e4SLinus Torvalds * 31da177e4SLinus Torvalds * Copyright (C) 2004,2005 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/i2c-id.h> 281da177e4SLinus Torvalds #include <linux/init.h> 291da177e4SLinus Torvalds #include <linux/time.h> 301da177e4SLinus Torvalds #include <linux/interrupt.h> 311da177e4SLinus Torvalds #include <linux/delay.h> 321da177e4SLinus Torvalds #include <linux/errno.h> 331da177e4SLinus Torvalds #include <linux/err.h> 34d052d1beSRussell King #include <linux/platform_device.h> 35f8ce2547SRussell King #include <linux/clk.h> 3661c7cff8SBen Dooks #include <linux/cpufreq.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include <asm/irq.h> 391da177e4SLinus Torvalds #include <asm/io.h> 401da177e4SLinus Torvalds 41b5d0b4baSBen Dooks #include <asm/plat-s3c/regs-iic.h> 42b5d0b4baSBen Dooks #include <asm/plat-s3c/iic.h> 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds /* i2c controller state */ 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds enum s3c24xx_i2c_state { 471da177e4SLinus Torvalds STATE_IDLE, 481da177e4SLinus Torvalds STATE_START, 491da177e4SLinus Torvalds STATE_READ, 501da177e4SLinus Torvalds STATE_WRITE, 511da177e4SLinus Torvalds STATE_STOP 521da177e4SLinus Torvalds }; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds struct s3c24xx_i2c { 551da177e4SLinus Torvalds spinlock_t lock; 561da177e4SLinus Torvalds wait_queue_head_t wait; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds struct i2c_msg *msg; 591da177e4SLinus Torvalds unsigned int msg_num; 601da177e4SLinus Torvalds unsigned int msg_idx; 611da177e4SLinus Torvalds unsigned int msg_ptr; 621da177e4SLinus Torvalds 63e00a8cdfSBen Dooks unsigned int tx_setup; 64e00a8cdfSBen Dooks 651da177e4SLinus Torvalds enum s3c24xx_i2c_state state; 6661c7cff8SBen Dooks unsigned long clkrate; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds void __iomem *regs; 691da177e4SLinus Torvalds struct clk *clk; 701da177e4SLinus Torvalds struct device *dev; 711da177e4SLinus Torvalds struct resource *irq; 721da177e4SLinus Torvalds struct resource *ioarea; 731da177e4SLinus Torvalds struct i2c_adapter adap; 7461c7cff8SBen Dooks 7561c7cff8SBen Dooks #ifdef CONFIG_CPU_FREQ 7661c7cff8SBen Dooks struct notifier_block freq_transition; 7761c7cff8SBen Dooks #endif 781da177e4SLinus Torvalds }; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /* default platform data to use if not supplied in the platform_device 811da177e4SLinus Torvalds */ 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds static struct s3c2410_platform_i2c s3c24xx_i2c_default_platform = { 841da177e4SLinus Torvalds .flags = 0, 851da177e4SLinus Torvalds .slave_addr = 0x10, 861da177e4SLinus Torvalds .bus_freq = 100*1000, 871da177e4SLinus Torvalds .max_freq = 400*1000, 881da177e4SLinus Torvalds .sda_delay = S3C2410_IICLC_SDA_DELAY5 | S3C2410_IICLC_FILTER_ON, 891da177e4SLinus Torvalds }; 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* s3c24xx_i2c_is2440() 921da177e4SLinus Torvalds * 931da177e4SLinus Torvalds * return true is this is an s3c2440 941da177e4SLinus Torvalds */ 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c *i2c) 971da177e4SLinus Torvalds { 981da177e4SLinus Torvalds struct platform_device *pdev = to_platform_device(i2c->dev); 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds return !strcmp(pdev->name, "s3c2440-i2c"); 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* s3c24xx_i2c_get_platformdata 1051da177e4SLinus Torvalds * 1061da177e4SLinus Torvalds * get the platform data associated with the given device, or return 1071da177e4SLinus Torvalds * the default if there is none 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds 1103d0911bfSBen Dooks static inline struct s3c2410_platform_i2c * 1113d0911bfSBen Dooks s3c24xx_i2c_get_platformdata(struct device *dev) 1121da177e4SLinus Torvalds { 1131da177e4SLinus Torvalds if (dev->platform_data != NULL) 1141da177e4SLinus Torvalds return (struct s3c2410_platform_i2c *)dev->platform_data; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds return &s3c24xx_i2c_default_platform; 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds /* s3c24xx_i2c_master_complete 1201da177e4SLinus Torvalds * 1211da177e4SLinus Torvalds * complete the message and wake up the caller, using the given return code, 1221da177e4SLinus Torvalds * or zero to mean ok. 1231da177e4SLinus Torvalds */ 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret) 1261da177e4SLinus Torvalds { 1271da177e4SLinus Torvalds dev_dbg(i2c->dev, "master_complete %d\n", ret); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds i2c->msg_ptr = 0; 1301da177e4SLinus Torvalds i2c->msg = NULL; 1311da177e4SLinus Torvalds i2c->msg_idx++; 1321da177e4SLinus Torvalds i2c->msg_num = 0; 1331da177e4SLinus Torvalds if (ret) 1341da177e4SLinus Torvalds i2c->msg_idx = ret; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds wake_up(&i2c->wait); 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds unsigned long tmp; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 1441da177e4SLinus Torvalds writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c) 1481da177e4SLinus Torvalds { 1491da177e4SLinus Torvalds unsigned long tmp; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 1521da177e4SLinus Torvalds writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* irq enable/disable functions */ 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c) 1581da177e4SLinus Torvalds { 1591da177e4SLinus Torvalds unsigned long tmp; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 1621da177e4SLinus Torvalds writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c) 1661da177e4SLinus Torvalds { 1671da177e4SLinus Torvalds unsigned long tmp; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 1701da177e4SLinus Torvalds writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds /* s3c24xx_i2c_message_start 1751da177e4SLinus Torvalds * 1761da177e4SLinus Torvalds * put the start of a message onto the bus 1771da177e4SLinus Torvalds */ 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, 1801da177e4SLinus Torvalds struct i2c_msg *msg) 1811da177e4SLinus Torvalds { 1821da177e4SLinus Torvalds unsigned int addr = (msg->addr & 0x7f) << 1; 1831da177e4SLinus Torvalds unsigned long stat; 1841da177e4SLinus Torvalds unsigned long iiccon; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds stat = 0; 1871da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_TXRXEN; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds if (msg->flags & I2C_M_RD) { 1901da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_MASTER_RX; 1911da177e4SLinus Torvalds addr |= 1; 1921da177e4SLinus Torvalds } else 1931da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_MASTER_TX; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds if (msg->flags & I2C_M_REV_DIR_ADDR) 1961da177e4SLinus Torvalds addr ^= 1; 1971da177e4SLinus Torvalds 1983d0911bfSBen Dooks /* todo - check for wether ack wanted or not */ 1991da177e4SLinus Torvalds s3c24xx_i2c_enable_ack(i2c); 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds iiccon = readl(i2c->regs + S3C2410_IICCON); 2021da177e4SLinus Torvalds writel(stat, i2c->regs + S3C2410_IICSTAT); 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr); 2051da177e4SLinus Torvalds writeb(addr, i2c->regs + S3C2410_IICDS); 2061da177e4SLinus Torvalds 207e00a8cdfSBen Dooks /* delay here to ensure the data byte has gotten onto the bus 208e00a8cdfSBen Dooks * before the transaction is started */ 209e00a8cdfSBen Dooks 210e00a8cdfSBen Dooks ndelay(i2c->tx_setup); 211e00a8cdfSBen Dooks 2121da177e4SLinus Torvalds dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon); 2131da177e4SLinus Torvalds writel(iiccon, i2c->regs + S3C2410_IICCON); 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_START; 2161da177e4SLinus Torvalds writel(stat, i2c->regs + S3C2410_IICSTAT); 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret) 2201da177e4SLinus Torvalds { 2211da177e4SLinus Torvalds unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT); 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds dev_dbg(i2c->dev, "STOP\n"); 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds /* stop the transfer */ 2261da177e4SLinus Torvalds iicstat &= ~S3C2410_IICSTAT_START; 2271da177e4SLinus Torvalds writel(iicstat, i2c->regs + S3C2410_IICSTAT); 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds i2c->state = STATE_STOP; 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds s3c24xx_i2c_master_complete(i2c, ret); 2321da177e4SLinus Torvalds s3c24xx_i2c_disable_irq(i2c); 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds /* helper functions to determine the current state in the set of 2361da177e4SLinus Torvalds * messages we are sending */ 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds /* is_lastmsg() 2391da177e4SLinus Torvalds * 2401da177e4SLinus Torvalds * returns TRUE if the current message is the last in the set 2411da177e4SLinus Torvalds */ 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds static inline int is_lastmsg(struct s3c24xx_i2c *i2c) 2441da177e4SLinus Torvalds { 2451da177e4SLinus Torvalds return i2c->msg_idx >= (i2c->msg_num - 1); 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds /* is_msglast 2491da177e4SLinus Torvalds * 2501da177e4SLinus Torvalds * returns TRUE if we this is the last byte in the current message 2511da177e4SLinus Torvalds */ 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds static inline int is_msglast(struct s3c24xx_i2c *i2c) 2541da177e4SLinus Torvalds { 2551da177e4SLinus Torvalds return i2c->msg_ptr == i2c->msg->len-1; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds /* is_msgend 2591da177e4SLinus Torvalds * 2601da177e4SLinus Torvalds * returns TRUE if we reached the end of the current message 2611da177e4SLinus Torvalds */ 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds static inline int is_msgend(struct s3c24xx_i2c *i2c) 2641da177e4SLinus Torvalds { 2651da177e4SLinus Torvalds return i2c->msg_ptr >= i2c->msg->len; 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /* i2s_s3c_irq_nextbyte 2691da177e4SLinus Torvalds * 2701da177e4SLinus Torvalds * process an interrupt and work out what to do 2711da177e4SLinus Torvalds */ 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat) 2741da177e4SLinus Torvalds { 2751da177e4SLinus Torvalds unsigned long tmp; 2761da177e4SLinus Torvalds unsigned char byte; 2771da177e4SLinus Torvalds int ret = 0; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds switch (i2c->state) { 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds case STATE_IDLE: 28208882d20SHarvey Harrison dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__); 2831da177e4SLinus Torvalds goto out; 2841da177e4SLinus Torvalds break; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds case STATE_STOP: 28708882d20SHarvey Harrison dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__); 2881da177e4SLinus Torvalds s3c24xx_i2c_disable_irq(i2c); 2891da177e4SLinus Torvalds goto out_ack; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds case STATE_START: 2921da177e4SLinus Torvalds /* last thing we did was send a start condition on the 2931da177e4SLinus Torvalds * bus, or started a new i2c message 2941da177e4SLinus Torvalds */ 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds if (iicstat & S3C2410_IICSTAT_LASTBIT && 2971da177e4SLinus Torvalds !(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 2981da177e4SLinus Torvalds /* ack was not received... */ 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds dev_dbg(i2c->dev, "ack was not received\n"); 30163f5c289SBen Dooks s3c24xx_i2c_stop(i2c, -ENXIO); 3021da177e4SLinus Torvalds goto out_ack; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_RD) 3061da177e4SLinus Torvalds i2c->state = STATE_READ; 3071da177e4SLinus Torvalds else 3081da177e4SLinus Torvalds i2c->state = STATE_WRITE; 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds /* terminate the transfer if there is nothing to do 31163f5c289SBen Dooks * as this is used by the i2c probe to find devices. */ 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds if (is_lastmsg(i2c) && i2c->msg->len == 0) { 3141da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 3151da177e4SLinus Torvalds goto out_ack; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds if (i2c->state == STATE_READ) 3191da177e4SLinus Torvalds goto prepare_read; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds /* fall through to the write state, as we will need to 3221da177e4SLinus Torvalds * send a byte as well */ 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds case STATE_WRITE: 3251da177e4SLinus Torvalds /* we are writing data to the device... check for the 3261da177e4SLinus Torvalds * end of the message, and if so, work out what to do 3271da177e4SLinus Torvalds */ 3281da177e4SLinus Torvalds 3292709781bSBen Dooks if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) { 3302709781bSBen Dooks if (iicstat & S3C2410_IICSTAT_LASTBIT) { 3312709781bSBen Dooks dev_dbg(i2c->dev, "WRITE: No Ack\n"); 3322709781bSBen Dooks 3332709781bSBen Dooks s3c24xx_i2c_stop(i2c, -ECONNREFUSED); 3342709781bSBen Dooks goto out_ack; 3352709781bSBen Dooks } 3362709781bSBen Dooks } 3372709781bSBen Dooks 3381da177e4SLinus Torvalds retry_write: 3392709781bSBen Dooks 3401da177e4SLinus Torvalds if (!is_msgend(i2c)) { 3411da177e4SLinus Torvalds byte = i2c->msg->buf[i2c->msg_ptr++]; 3421da177e4SLinus Torvalds writeb(byte, i2c->regs + S3C2410_IICDS); 3431da177e4SLinus Torvalds 344e00a8cdfSBen Dooks /* delay after writing the byte to allow the 345e00a8cdfSBen Dooks * data setup time on the bus, as writing the 346e00a8cdfSBen Dooks * data to the register causes the first bit 347e00a8cdfSBen Dooks * to appear on SDA, and SCL will change as 348e00a8cdfSBen Dooks * soon as the interrupt is acknowledged */ 349e00a8cdfSBen Dooks 350e00a8cdfSBen Dooks ndelay(i2c->tx_setup); 351e00a8cdfSBen Dooks 3521da177e4SLinus Torvalds } else if (!is_lastmsg(i2c)) { 3531da177e4SLinus Torvalds /* we need to go to the next i2c message */ 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds dev_dbg(i2c->dev, "WRITE: Next Message\n"); 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds i2c->msg_ptr = 0; 3581da177e4SLinus Torvalds i2c->msg_idx++; 3591da177e4SLinus Torvalds i2c->msg++; 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds /* check to see if we need to do another message */ 3621da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_NOSTART) { 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_RD) { 3651da177e4SLinus Torvalds /* cannot do this, the controller 3661da177e4SLinus Torvalds * forces us to send a new START 3671da177e4SLinus Torvalds * when we change direction */ 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, -EINVAL); 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds goto retry_write; 3731da177e4SLinus Torvalds } else { 3741da177e4SLinus Torvalds /* send the new start */ 3751da177e4SLinus Torvalds s3c24xx_i2c_message_start(i2c, i2c->msg); 3761da177e4SLinus Torvalds i2c->state = STATE_START; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds } else { 3801da177e4SLinus Torvalds /* send stop */ 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds break; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds case STATE_READ: 3871da177e4SLinus Torvalds /* we have a byte of data in the data register, do 3881da177e4SLinus Torvalds * something with it, and then work out wether we are 3891da177e4SLinus Torvalds * going to do any more read/write 3901da177e4SLinus Torvalds */ 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds byte = readb(i2c->regs + S3C2410_IICDS); 3931da177e4SLinus Torvalds i2c->msg->buf[i2c->msg_ptr++] = byte; 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds prepare_read: 3961da177e4SLinus Torvalds if (is_msglast(i2c)) { 3971da177e4SLinus Torvalds /* last byte of buffer */ 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds if (is_lastmsg(i2c)) 4001da177e4SLinus Torvalds s3c24xx_i2c_disable_ack(i2c); 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds } else if (is_msgend(i2c)) { 4031da177e4SLinus Torvalds /* ok, we've read the entire buffer, see if there 4041da177e4SLinus Torvalds * is anything else we need to do */ 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds if (is_lastmsg(i2c)) { 4071da177e4SLinus Torvalds /* last message, send stop and complete */ 4081da177e4SLinus Torvalds dev_dbg(i2c->dev, "READ: Send Stop\n"); 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0); 4111da177e4SLinus Torvalds } else { 4121da177e4SLinus Torvalds /* go to the next transfer */ 4131da177e4SLinus Torvalds dev_dbg(i2c->dev, "READ: Next Transfer\n"); 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds i2c->msg_ptr = 0; 4161da177e4SLinus Torvalds i2c->msg_idx++; 4171da177e4SLinus Torvalds i2c->msg++; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds break; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds /* acknowlegde the IRQ and get back on with the work */ 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds out_ack: 4271da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 4281da177e4SLinus Torvalds tmp &= ~S3C2410_IICCON_IRQPEND; 4291da177e4SLinus Torvalds writel(tmp, i2c->regs + S3C2410_IICCON); 4301da177e4SLinus Torvalds out: 4311da177e4SLinus Torvalds return ret; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* s3c24xx_i2c_irq 4351da177e4SLinus Torvalds * 4361da177e4SLinus Torvalds * top level IRQ servicing routine 4371da177e4SLinus Torvalds */ 4381da177e4SLinus Torvalds 4397d12e780SDavid Howells static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = dev_id; 4421da177e4SLinus Torvalds unsigned long status; 4431da177e4SLinus Torvalds unsigned long tmp; 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds status = readl(i2c->regs + S3C2410_IICSTAT); 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds if (status & S3C2410_IICSTAT_ARBITR) { 4483d0911bfSBen Dooks /* deal with arbitration loss */ 4491da177e4SLinus Torvalds dev_err(i2c->dev, "deal with arbitration loss\n"); 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds if (i2c->state == STATE_IDLE) { 4531da177e4SLinus Torvalds dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n"); 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON); 4561da177e4SLinus Torvalds tmp &= ~S3C2410_IICCON_IRQPEND; 4571da177e4SLinus Torvalds writel(tmp, i2c->regs + S3C2410_IICCON); 4581da177e4SLinus Torvalds goto out; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds /* pretty much this leaves us with the fact that we've 4621da177e4SLinus Torvalds * transmitted or received whatever byte we last sent */ 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds i2s_s3c_irq_nextbyte(i2c, status); 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds out: 4671da177e4SLinus Torvalds return IRQ_HANDLED; 4681da177e4SLinus Torvalds } 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds /* s3c24xx_i2c_set_master 4721da177e4SLinus Torvalds * 4731da177e4SLinus Torvalds * get the i2c bus for a master transaction 4741da177e4SLinus Torvalds */ 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds unsigned long iicstat; 4791da177e4SLinus Torvalds int timeout = 400; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds while (timeout-- > 0) { 4821da177e4SLinus Torvalds iicstat = readl(i2c->regs + S3C2410_IICSTAT); 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds if (!(iicstat & S3C2410_IICSTAT_BUSBUSY)) 4851da177e4SLinus Torvalds return 0; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds msleep(1); 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds return -ETIMEDOUT; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds /* s3c24xx_i2c_doxfer 4941da177e4SLinus Torvalds * 4951da177e4SLinus Torvalds * this starts an i2c transfer 4961da177e4SLinus Torvalds */ 4971da177e4SLinus Torvalds 4983d0911bfSBen Dooks static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, 4993d0911bfSBen Dooks struct i2c_msg *msgs, int num) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds unsigned long timeout; 5021da177e4SLinus Torvalds int ret; 5031da177e4SLinus Torvalds 504da6801e3SJulia Lawall if (!(readl(i2c->regs + S3C2410_IICCON) & S3C2410_IICCON_IRQEN)) 50561c7cff8SBen Dooks return -EIO; 50661c7cff8SBen Dooks 5071da177e4SLinus Torvalds ret = s3c24xx_i2c_set_master(i2c); 5081da177e4SLinus Torvalds if (ret != 0) { 5091da177e4SLinus Torvalds dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); 5101da177e4SLinus Torvalds ret = -EAGAIN; 5111da177e4SLinus Torvalds goto out; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds spin_lock_irq(&i2c->lock); 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds i2c->msg = msgs; 5171da177e4SLinus Torvalds i2c->msg_num = num; 5181da177e4SLinus Torvalds i2c->msg_ptr = 0; 5191da177e4SLinus Torvalds i2c->msg_idx = 0; 5201da177e4SLinus Torvalds i2c->state = STATE_START; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds s3c24xx_i2c_enable_irq(i2c); 5231da177e4SLinus Torvalds s3c24xx_i2c_message_start(i2c, msgs); 5241da177e4SLinus Torvalds spin_unlock_irq(&i2c->lock); 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds ret = i2c->msg_idx; 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds /* having these next two as dev_err() makes life very 5311da177e4SLinus Torvalds * noisy when doing an i2cdetect */ 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds if (timeout == 0) 5341da177e4SLinus Torvalds dev_dbg(i2c->dev, "timeout\n"); 5351da177e4SLinus Torvalds else if (ret != num) 5361da177e4SLinus Torvalds dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds /* ensure the stop has been through the bus */ 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds msleep(1); 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds out: 5431da177e4SLinus Torvalds return ret; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds /* s3c24xx_i2c_xfer 5471da177e4SLinus Torvalds * 5481da177e4SLinus Torvalds * first port of call from the i2c bus code when an message needs 54944bbe87eSSteven Cole * transferring across the i2c bus. 5501da177e4SLinus Torvalds */ 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, 5531da177e4SLinus Torvalds struct i2c_msg *msgs, int num) 5541da177e4SLinus Torvalds { 5551da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; 5561da177e4SLinus Torvalds int retry; 5571da177e4SLinus Torvalds int ret; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds for (retry = 0; retry < adap->retries; retry++) { 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds ret = s3c24xx_i2c_doxfer(i2c, msgs, num); 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds if (ret != -EAGAIN) 5641da177e4SLinus Torvalds return ret; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds udelay(100); 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds return -EREMOTEIO; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds /* declare our i2c functionality */ 5751da177e4SLinus Torvalds static u32 s3c24xx_i2c_func(struct i2c_adapter *adap) 5761da177e4SLinus Torvalds { 5771da177e4SLinus Torvalds return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds /* i2c bus registration info */ 5811da177e4SLinus Torvalds 5828f9082c5SJean Delvare static const struct i2c_algorithm s3c24xx_i2c_algorithm = { 5831da177e4SLinus Torvalds .master_xfer = s3c24xx_i2c_xfer, 5841da177e4SLinus Torvalds .functionality = s3c24xx_i2c_func, 5851da177e4SLinus Torvalds }; 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds static struct s3c24xx_i2c s3c24xx_i2c = { 58835532d20SMilind Arun Choudhary .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_i2c.lock), 5891da177e4SLinus Torvalds .wait = __WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait), 590e00a8cdfSBen Dooks .tx_setup = 50, 5911da177e4SLinus Torvalds .adap = { 5921da177e4SLinus Torvalds .name = "s3c2410-i2c", 5931da177e4SLinus Torvalds .owner = THIS_MODULE, 5941da177e4SLinus Torvalds .algo = &s3c24xx_i2c_algorithm, 5951da177e4SLinus Torvalds .retries = 2, 5963401b2ffSJean Delvare .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, 5971da177e4SLinus Torvalds }, 5981da177e4SLinus Torvalds }; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds /* s3c24xx_i2c_calcdivisor 6011da177e4SLinus Torvalds * 6021da177e4SLinus Torvalds * return the divisor settings for a given frequency 6031da177e4SLinus Torvalds */ 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted, 6061da177e4SLinus Torvalds unsigned int *div1, unsigned int *divs) 6071da177e4SLinus Torvalds { 6081da177e4SLinus Torvalds unsigned int calc_divs = clkin / wanted; 6091da177e4SLinus Torvalds unsigned int calc_div1; 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds if (calc_divs > (16*16)) 6121da177e4SLinus Torvalds calc_div1 = 512; 6131da177e4SLinus Torvalds else 6141da177e4SLinus Torvalds calc_div1 = 16; 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds calc_divs += calc_div1-1; 6171da177e4SLinus Torvalds calc_divs /= calc_div1; 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds if (calc_divs == 0) 6201da177e4SLinus Torvalds calc_divs = 1; 6211da177e4SLinus Torvalds if (calc_divs > 17) 6221da177e4SLinus Torvalds calc_divs = 17; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds *divs = calc_divs; 6251da177e4SLinus Torvalds *div1 = calc_div1; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds return clkin / (calc_divs * calc_div1); 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds /* freq_acceptable 6311da177e4SLinus Torvalds * 6321da177e4SLinus Torvalds * test wether a frequency is within the acceptable range of error 6331da177e4SLinus Torvalds */ 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds static inline int freq_acceptable(unsigned int freq, unsigned int wanted) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds int diff = freq - wanted; 6381da177e4SLinus Torvalds 6393d0911bfSBen Dooks return diff >= -2 && diff <= 2; 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 64261c7cff8SBen Dooks /* s3c24xx_i2c_clockrate 6431da177e4SLinus Torvalds * 6441da177e4SLinus Torvalds * work out a divisor for the user requested frequency setting, 6451da177e4SLinus Torvalds * either by the requested frequency, or scanning the acceptable 6461da177e4SLinus Torvalds * range of frequencies until something is found 6471da177e4SLinus Torvalds */ 6481da177e4SLinus Torvalds 64961c7cff8SBen Dooks static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) 6501da177e4SLinus Torvalds { 65161c7cff8SBen Dooks struct s3c2410_platform_i2c *pdata; 6521da177e4SLinus Torvalds unsigned long clkin = clk_get_rate(i2c->clk); 6531da177e4SLinus Torvalds unsigned int divs, div1; 65461c7cff8SBen Dooks u32 iiccon; 6551da177e4SLinus Torvalds int freq; 6561da177e4SLinus Torvalds int start, end; 6571da177e4SLinus Torvalds 65861c7cff8SBen Dooks i2c->clkrate = clkin; 65961c7cff8SBen Dooks 66061c7cff8SBen Dooks pdata = s3c24xx_i2c_get_platformdata(i2c->adap.dev.parent); 6611da177e4SLinus Torvalds clkin /= 1000; /* clkin now in KHz */ 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds dev_dbg(i2c->dev, "pdata %p, freq %lu %lu..%lu\n", 6641da177e4SLinus Torvalds pdata, pdata->bus_freq, pdata->min_freq, pdata->max_freq); 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds if (pdata->bus_freq != 0) { 6671da177e4SLinus Torvalds freq = s3c24xx_i2c_calcdivisor(clkin, pdata->bus_freq/1000, 6681da177e4SLinus Torvalds &div1, &divs); 6691da177e4SLinus Torvalds if (freq_acceptable(freq, pdata->bus_freq/1000)) 6701da177e4SLinus Torvalds goto found; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds /* ok, we may have to search for something suitable... */ 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds start = (pdata->max_freq == 0) ? pdata->bus_freq : pdata->max_freq; 6761da177e4SLinus Torvalds end = pdata->min_freq; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds start /= 1000; 6791da177e4SLinus Torvalds end /= 1000; 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds /* search loop... */ 6821da177e4SLinus Torvalds 6831da177e4SLinus Torvalds for (; start > end; start--) { 6841da177e4SLinus Torvalds freq = s3c24xx_i2c_calcdivisor(clkin, start, &div1, &divs); 6851da177e4SLinus Torvalds if (freq_acceptable(freq, start)) 6861da177e4SLinus Torvalds goto found; 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds /* cannot find frequency spec */ 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds return -EINVAL; 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds found: 6941da177e4SLinus Torvalds *got = freq; 69561c7cff8SBen Dooks 69661c7cff8SBen Dooks iiccon = readl(i2c->regs + S3C2410_IICCON); 69761c7cff8SBen Dooks iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512); 69861c7cff8SBen Dooks iiccon |= (divs-1); 69961c7cff8SBen Dooks 70061c7cff8SBen Dooks if (div1 == 512) 70161c7cff8SBen Dooks iiccon |= S3C2410_IICCON_TXDIV_512; 70261c7cff8SBen Dooks 70361c7cff8SBen Dooks writel(iiccon, i2c->regs + S3C2410_IICCON); 70461c7cff8SBen Dooks 7051da177e4SLinus Torvalds return 0; 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 70861c7cff8SBen Dooks #ifdef CONFIG_CPU_FREQ 70961c7cff8SBen Dooks 71061c7cff8SBen Dooks #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition) 71161c7cff8SBen Dooks 71261c7cff8SBen Dooks static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb, 71361c7cff8SBen Dooks unsigned long val, void *data) 71461c7cff8SBen Dooks { 71561c7cff8SBen Dooks struct s3c24xx_i2c *i2c = freq_to_i2c(nb); 71661c7cff8SBen Dooks unsigned long flags; 71761c7cff8SBen Dooks unsigned int got; 71861c7cff8SBen Dooks int delta_f; 71961c7cff8SBen Dooks int ret; 72061c7cff8SBen Dooks 72161c7cff8SBen Dooks delta_f = clk_get_rate(i2c->clk) - i2c->clkrate; 72261c7cff8SBen Dooks 72361c7cff8SBen Dooks /* if we're post-change and the input clock has slowed down 72461c7cff8SBen Dooks * or at pre-change and the clock is about to speed up, then 72561c7cff8SBen Dooks * adjust our clock rate. <0 is slow, >0 speedup. 72661c7cff8SBen Dooks */ 72761c7cff8SBen Dooks 72861c7cff8SBen Dooks if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) || 72961c7cff8SBen Dooks (val == CPUFREQ_PRECHANGE && delta_f > 0)) { 73061c7cff8SBen Dooks spin_lock_irqsave(&i2c->lock, flags); 73161c7cff8SBen Dooks ret = s3c24xx_i2c_clockrate(i2c, &got); 73261c7cff8SBen Dooks spin_unlock_irqrestore(&i2c->lock, flags); 73361c7cff8SBen Dooks 73461c7cff8SBen Dooks if (ret < 0) 73561c7cff8SBen Dooks dev_err(i2c->dev, "cannot find frequency\n"); 73661c7cff8SBen Dooks else 73761c7cff8SBen Dooks dev_info(i2c->dev, "setting freq %d\n", got); 73861c7cff8SBen Dooks } 73961c7cff8SBen Dooks 74061c7cff8SBen Dooks return 0; 74161c7cff8SBen Dooks } 74261c7cff8SBen Dooks 74361c7cff8SBen Dooks static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) 74461c7cff8SBen Dooks { 74561c7cff8SBen Dooks i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition; 74661c7cff8SBen Dooks 74761c7cff8SBen Dooks return cpufreq_register_notifier(&i2c->freq_transition, 74861c7cff8SBen Dooks CPUFREQ_TRANSITION_NOTIFIER); 74961c7cff8SBen Dooks } 75061c7cff8SBen Dooks 75161c7cff8SBen Dooks static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) 75261c7cff8SBen Dooks { 75361c7cff8SBen Dooks cpufreq_unregister_notifier(&i2c->freq_transition, 75461c7cff8SBen Dooks CPUFREQ_TRANSITION_NOTIFIER); 75561c7cff8SBen Dooks } 75661c7cff8SBen Dooks 75761c7cff8SBen Dooks #else 75861c7cff8SBen Dooks static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) 75961c7cff8SBen Dooks { 76061c7cff8SBen Dooks return 0; 76161c7cff8SBen Dooks } 76261c7cff8SBen Dooks 76361c7cff8SBen Dooks static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) 76461c7cff8SBen Dooks { 76561c7cff8SBen Dooks } 76661c7cff8SBen Dooks #endif 76761c7cff8SBen Dooks 7681da177e4SLinus Torvalds /* s3c24xx_i2c_init 7691da177e4SLinus Torvalds * 7701da177e4SLinus Torvalds * initialise the controller, set the IO lines and frequency 7711da177e4SLinus Torvalds */ 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) 7741da177e4SLinus Torvalds { 7751da177e4SLinus Torvalds unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN; 7761da177e4SLinus Torvalds struct s3c2410_platform_i2c *pdata; 7771da177e4SLinus Torvalds unsigned int freq; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds /* get the plafrom data */ 7801da177e4SLinus Torvalds 781*8be310a6SBen Dooks pdata = s3c24xx_i2c_get_platformdata(i2c->dev); 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds /* inititalise the gpio */ 7841da177e4SLinus Torvalds 785*8be310a6SBen Dooks if (pdata->cfg_gpio) 786*8be310a6SBen Dooks pdata->cfg_gpio(to_platform_device(i2c->dev)); 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* write slave address */ 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD); 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); 7931da177e4SLinus Torvalds 79461c7cff8SBen Dooks writel(iicon, i2c->regs + S3C2410_IICCON); 79561c7cff8SBen Dooks 7961da177e4SLinus Torvalds /* we need to work out the divisors for the clock... */ 7971da177e4SLinus Torvalds 79861c7cff8SBen Dooks if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { 79961c7cff8SBen Dooks writel(0, i2c->regs + S3C2410_IICCON); 8001da177e4SLinus Torvalds dev_err(i2c->dev, "cannot meet bus frequency required\n"); 8011da177e4SLinus Torvalds return -EINVAL; 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds /* todo - check that the i2c lines aren't being dragged anywhere */ 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); 8071da177e4SLinus Torvalds dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon); 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds /* check for s3c2440 i2c controller */ 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds if (s3c24xx_i2c_is2440(i2c)) { 8121da177e4SLinus Torvalds dev_dbg(i2c->dev, "S3C2440_IICLC=%08x\n", pdata->sda_delay); 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds writel(pdata->sda_delay, i2c->regs + S3C2440_IICLC); 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds return 0; 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds /* s3c24xx_i2c_probe 8211da177e4SLinus Torvalds * 8221da177e4SLinus Torvalds * called by the bus driver when a suitable device is found 8231da177e4SLinus Torvalds */ 8241da177e4SLinus Torvalds 8253ae5eaecSRussell King static int s3c24xx_i2c_probe(struct platform_device *pdev) 8261da177e4SLinus Torvalds { 8271da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = &s3c24xx_i2c; 828399dee23SBen Dooks struct s3c2410_platform_i2c *pdata; 8291da177e4SLinus Torvalds struct resource *res; 8301da177e4SLinus Torvalds int ret; 8311da177e4SLinus Torvalds 832399dee23SBen Dooks pdata = s3c24xx_i2c_get_platformdata(&pdev->dev); 833399dee23SBen Dooks 8341da177e4SLinus Torvalds /* find the clock and enable it */ 8351da177e4SLinus Torvalds 8363ae5eaecSRussell King i2c->dev = &pdev->dev; 8373ae5eaecSRussell King i2c->clk = clk_get(&pdev->dev, "i2c"); 8381da177e4SLinus Torvalds if (IS_ERR(i2c->clk)) { 8393ae5eaecSRussell King dev_err(&pdev->dev, "cannot get clock\n"); 8401da177e4SLinus Torvalds ret = -ENOENT; 8415b68790cSBen Dooks goto err_noclk; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8443ae5eaecSRussell King dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk); 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds clk_enable(i2c->clk); 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds /* map the registers */ 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 8511da177e4SLinus Torvalds if (res == NULL) { 8523ae5eaecSRussell King dev_err(&pdev->dev, "cannot find IO resource\n"); 8531da177e4SLinus Torvalds ret = -ENOENT; 8545b68790cSBen Dooks goto err_clk; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1, 8581da177e4SLinus Torvalds pdev->name); 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds if (i2c->ioarea == NULL) { 8613ae5eaecSRussell King dev_err(&pdev->dev, "cannot request IO\n"); 8621da177e4SLinus Torvalds ret = -ENXIO; 8635b68790cSBen Dooks goto err_clk; 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds 8661da177e4SLinus Torvalds i2c->regs = ioremap(res->start, (res->end-res->start)+1); 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds if (i2c->regs == NULL) { 8693ae5eaecSRussell King dev_err(&pdev->dev, "cannot map IO\n"); 8701da177e4SLinus Torvalds ret = -ENXIO; 8715b68790cSBen Dooks goto err_ioarea; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds 8743d0911bfSBen Dooks dev_dbg(&pdev->dev, "registers %p (%p, %p)\n", 8753d0911bfSBen Dooks i2c->regs, i2c->ioarea, res); 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds /* setup info block for the i2c core */ 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds i2c->adap.algo_data = i2c; 8803ae5eaecSRussell King i2c->adap.dev.parent = &pdev->dev; 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds /* initialise the i2c controller */ 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds ret = s3c24xx_i2c_init(i2c); 8851da177e4SLinus Torvalds if (ret != 0) 8865b68790cSBen Dooks goto err_iomap; 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds /* find the IRQ for this unit (note, this relies on the init call to 8891da177e4SLinus Torvalds * ensure no current IRQs pending 8901da177e4SLinus Torvalds */ 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 8931da177e4SLinus Torvalds if (res == NULL) { 8943ae5eaecSRussell King dev_err(&pdev->dev, "cannot find IRQ\n"); 8951da177e4SLinus Torvalds ret = -ENOENT; 8965b68790cSBen Dooks goto err_iomap; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 899dace1453SThomas Gleixner ret = request_irq(res->start, s3c24xx_i2c_irq, IRQF_DISABLED, 9001da177e4SLinus Torvalds pdev->name, i2c); 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds if (ret != 0) { 9033ae5eaecSRussell King dev_err(&pdev->dev, "cannot claim IRQ\n"); 9045b68790cSBen Dooks goto err_iomap; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds i2c->irq = res; 9081da177e4SLinus Torvalds 909a1ba1583SArnaud Patard dev_dbg(&pdev->dev, "irq resource %p (%lu)\n", res, 910a1ba1583SArnaud Patard (unsigned long)res->start); 9111da177e4SLinus Torvalds 91261c7cff8SBen Dooks ret = s3c24xx_i2c_register_cpufreq(i2c); 91361c7cff8SBen Dooks if (ret < 0) { 91461c7cff8SBen Dooks dev_err(&pdev->dev, "failed to register cpufreq notifier\n"); 91561c7cff8SBen Dooks goto err_irq; 91661c7cff8SBen Dooks } 91761c7cff8SBen Dooks 918399dee23SBen Dooks /* Note, previous versions of the driver used i2c_add_adapter() 919399dee23SBen Dooks * to add the bus at any number. We now pass the bus number via 920399dee23SBen Dooks * the platform data, so if unset it will now default to always 921399dee23SBen Dooks * being bus 0. 922399dee23SBen Dooks */ 923399dee23SBen Dooks 924399dee23SBen Dooks i2c->adap.nr = pdata->bus_num; 925399dee23SBen Dooks 926399dee23SBen Dooks ret = i2c_add_numbered_adapter(&i2c->adap); 9271da177e4SLinus Torvalds if (ret < 0) { 9283ae5eaecSRussell King dev_err(&pdev->dev, "failed to add bus to i2c core\n"); 92961c7cff8SBen Dooks goto err_cpufreq; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds 9323ae5eaecSRussell King platform_set_drvdata(pdev, i2c); 9331da177e4SLinus Torvalds 9343ae5eaecSRussell King dev_info(&pdev->dev, "%s: S3C I2C adapter\n", i2c->adap.dev.bus_id); 9355b68790cSBen Dooks return 0; 9361da177e4SLinus Torvalds 93761c7cff8SBen Dooks err_cpufreq: 93861c7cff8SBen Dooks s3c24xx_i2c_deregister_cpufreq(i2c); 93961c7cff8SBen Dooks 9405b68790cSBen Dooks err_irq: 9415b68790cSBen Dooks free_irq(i2c->irq->start, i2c); 9421da177e4SLinus Torvalds 9435b68790cSBen Dooks err_iomap: 9445b68790cSBen Dooks iounmap(i2c->regs); 9455b68790cSBen Dooks 9465b68790cSBen Dooks err_ioarea: 9475b68790cSBen Dooks release_resource(i2c->ioarea); 9485b68790cSBen Dooks kfree(i2c->ioarea); 9495b68790cSBen Dooks 9505b68790cSBen Dooks err_clk: 9515b68790cSBen Dooks clk_disable(i2c->clk); 9525b68790cSBen Dooks clk_put(i2c->clk); 9535b68790cSBen Dooks 9545b68790cSBen Dooks err_noclk: 9551da177e4SLinus Torvalds return ret; 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds /* s3c24xx_i2c_remove 9591da177e4SLinus Torvalds * 9601da177e4SLinus Torvalds * called when device is removed from the bus 9611da177e4SLinus Torvalds */ 9621da177e4SLinus Torvalds 9633ae5eaecSRussell King static int s3c24xx_i2c_remove(struct platform_device *pdev) 9641da177e4SLinus Torvalds { 9653ae5eaecSRussell King struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); 9661da177e4SLinus Torvalds 96761c7cff8SBen Dooks s3c24xx_i2c_deregister_cpufreq(i2c); 96861c7cff8SBen Dooks 9695b68790cSBen Dooks i2c_del_adapter(&i2c->adap); 9705b68790cSBen Dooks free_irq(i2c->irq->start, i2c); 9715b68790cSBen Dooks 9725b68790cSBen Dooks clk_disable(i2c->clk); 9735b68790cSBen Dooks clk_put(i2c->clk); 9745b68790cSBen Dooks 9755b68790cSBen Dooks iounmap(i2c->regs); 9765b68790cSBen Dooks 9775b68790cSBen Dooks release_resource(i2c->ioarea); 9785b68790cSBen Dooks kfree(i2c->ioarea); 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds return 0; 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds #ifdef CONFIG_PM 9843ae5eaecSRussell King static int s3c24xx_i2c_resume(struct platform_device *dev) 9851da177e4SLinus Torvalds { 9863ae5eaecSRussell King struct s3c24xx_i2c *i2c = platform_get_drvdata(dev); 9871da177e4SLinus Torvalds 9889480e307SRussell King if (i2c != NULL) 9891da177e4SLinus Torvalds s3c24xx_i2c_init(i2c); 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds return 0; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds #else 9951da177e4SLinus Torvalds #define s3c24xx_i2c_resume NULL 9961da177e4SLinus Torvalds #endif 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds /* device driver for platform bus bits */ 9991da177e4SLinus Torvalds 10003ae5eaecSRussell King static struct platform_driver s3c2410_i2c_driver = { 10013ae5eaecSRussell King .probe = s3c24xx_i2c_probe, 10023ae5eaecSRussell King .remove = s3c24xx_i2c_remove, 10033ae5eaecSRussell King .resume = s3c24xx_i2c_resume, 10043ae5eaecSRussell King .driver = { 10050cf36281SLaurent Riffard .owner = THIS_MODULE, 10061da177e4SLinus Torvalds .name = "s3c2410-i2c", 10073ae5eaecSRussell King }, 10081da177e4SLinus Torvalds }; 10091da177e4SLinus Torvalds 10103ae5eaecSRussell King static struct platform_driver s3c2440_i2c_driver = { 10111da177e4SLinus Torvalds .probe = s3c24xx_i2c_probe, 10121da177e4SLinus Torvalds .remove = s3c24xx_i2c_remove, 10131da177e4SLinus Torvalds .resume = s3c24xx_i2c_resume, 10143ae5eaecSRussell King .driver = { 10153ae5eaecSRussell King .owner = THIS_MODULE, 10163ae5eaecSRussell King .name = "s3c2440-i2c", 10173ae5eaecSRussell King }, 10181da177e4SLinus Torvalds }; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds static int __init i2c_adap_s3c_init(void) 10211da177e4SLinus Torvalds { 10221da177e4SLinus Torvalds int ret; 10231da177e4SLinus Torvalds 10243ae5eaecSRussell King ret = platform_driver_register(&s3c2410_i2c_driver); 1025e32e28edSRussell King if (ret == 0) { 10263ae5eaecSRussell King ret = platform_driver_register(&s3c2440_i2c_driver); 1027e32e28edSRussell King if (ret) 10283ae5eaecSRussell King platform_driver_unregister(&s3c2410_i2c_driver); 1029e32e28edSRussell King } 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds return ret; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds static void __exit i2c_adap_s3c_exit(void) 10351da177e4SLinus Torvalds { 10363ae5eaecSRussell King platform_driver_unregister(&s3c2410_i2c_driver); 10373ae5eaecSRussell King platform_driver_unregister(&s3c2440_i2c_driver); 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds module_init(i2c_adap_s3c_init); 10411da177e4SLinus Torvalds module_exit(i2c_adap_s3c_exit); 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds MODULE_DESCRIPTION("S3C24XX I2C Bus driver"); 10441da177e4SLinus Torvalds MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); 10451da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1046add8eda7SKay Sievers MODULE_ALIAS("platform:s3c2410-i2c"); 1047d150a4bbSBen Dooks MODULE_ALIAS("platform:s3c2440-i2c"); 1048