xref: /linux/drivers/i2c/busses/i2c-s3c2410.c (revision 37de03ea1246c8280f6c8c6f61ea98973e9b9448)
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>
34f8ce2547SRussell King #include <linux/clk.h>
3561c7cff8SBen Dooks #include <linux/cpufreq.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
3721782180SH Hartley Sweeten #include <linux/io.h>
385a5f5080SThomas Abraham #include <linux/of_i2c.h>
395a5f5080SThomas Abraham #include <linux/of_gpio.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <asm/irq.h>
421da177e4SLinus Torvalds 
439498cb79SBen Dooks #include <plat/regs-iic.h>
449498cb79SBen Dooks #include <plat/iic.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds /* i2c controller state */
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds enum s3c24xx_i2c_state {
491da177e4SLinus Torvalds 	STATE_IDLE,
501da177e4SLinus Torvalds 	STATE_START,
511da177e4SLinus Torvalds 	STATE_READ,
521da177e4SLinus Torvalds 	STATE_WRITE,
531da177e4SLinus Torvalds 	STATE_STOP
541da177e4SLinus Torvalds };
551da177e4SLinus Torvalds 
567d85ccd8SBen Dooks enum s3c24xx_i2c_type {
577d85ccd8SBen Dooks 	TYPE_S3C2410,
587d85ccd8SBen Dooks 	TYPE_S3C2440,
597d85ccd8SBen Dooks };
607d85ccd8SBen Dooks 
611da177e4SLinus Torvalds struct s3c24xx_i2c {
621da177e4SLinus Torvalds 	spinlock_t		lock;
631da177e4SLinus Torvalds 	wait_queue_head_t	wait;
64be44f01eSBen Dooks 	unsigned int		suspended:1;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	struct i2c_msg		*msg;
671da177e4SLinus Torvalds 	unsigned int		msg_num;
681da177e4SLinus Torvalds 	unsigned int		msg_idx;
691da177e4SLinus Torvalds 	unsigned int		msg_ptr;
701da177e4SLinus Torvalds 
71e00a8cdfSBen Dooks 	unsigned int		tx_setup;
72e0d1ec97SBen Dooks 	unsigned int		irq;
73e00a8cdfSBen Dooks 
741da177e4SLinus Torvalds 	enum s3c24xx_i2c_state	state;
7561c7cff8SBen Dooks 	unsigned long		clkrate;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	void __iomem		*regs;
781da177e4SLinus Torvalds 	struct clk		*clk;
791da177e4SLinus Torvalds 	struct device		*dev;
801da177e4SLinus Torvalds 	struct resource		*ioarea;
811da177e4SLinus Torvalds 	struct i2c_adapter	adap;
8261c7cff8SBen Dooks 
834fd81eb2SThomas Abraham 	struct s3c2410_platform_i2c	*pdata;
845a5f5080SThomas Abraham 	int			gpios[2];
8561c7cff8SBen Dooks #ifdef CONFIG_CPU_FREQ
8661c7cff8SBen Dooks 	struct notifier_block	freq_transition;
8761c7cff8SBen Dooks #endif
881da177e4SLinus Torvalds };
891da177e4SLinus Torvalds 
906a039cabSBen Dooks /* default platform data removed, dev should always carry data. */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* s3c24xx_i2c_is2440()
931da177e4SLinus Torvalds  *
941da177e4SLinus Torvalds  * return true is this is an s3c2440
951da177e4SLinus Torvalds */
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c *i2c)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	struct platform_device *pdev = to_platform_device(i2c->dev);
1007d85ccd8SBen Dooks 	enum s3c24xx_i2c_type type;
1011da177e4SLinus Torvalds 
1025a5f5080SThomas Abraham #ifdef CONFIG_OF
1035a5f5080SThomas Abraham 	if (i2c->dev->of_node)
1045a5f5080SThomas Abraham 		return of_device_is_compatible(i2c->dev->of_node,
1055a5f5080SThomas Abraham 				"samsung,s3c2440-i2c");
1065a5f5080SThomas Abraham #endif
1075a5f5080SThomas Abraham 
1087d85ccd8SBen Dooks 	type = platform_get_device_id(pdev)->driver_data;
1097d85ccd8SBen Dooks 	return type == TYPE_S3C2440;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /* s3c24xx_i2c_master_complete
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * complete the message and wake up the caller, using the given return code,
1151da177e4SLinus Torvalds  * or zero to mean ok.
1161da177e4SLinus Torvalds */
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	dev_dbg(i2c->dev, "master_complete %d\n", ret);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	i2c->msg_ptr = 0;
1231da177e4SLinus Torvalds 	i2c->msg = NULL;
1241da177e4SLinus Torvalds 	i2c->msg_idx++;
1251da177e4SLinus Torvalds 	i2c->msg_num = 0;
1261da177e4SLinus Torvalds 	if (ret)
1271da177e4SLinus Torvalds 		i2c->msg_idx = ret;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	wake_up(&i2c->wait);
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	unsigned long tmp;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	tmp = readl(i2c->regs + S3C2410_IICCON);
1371da177e4SLinus Torvalds 	writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	unsigned long tmp;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	tmp = readl(i2c->regs + S3C2410_IICCON);
1451da177e4SLinus Torvalds 	writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds /* irq enable/disable functions */
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	unsigned long tmp;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	tmp = readl(i2c->regs + S3C2410_IICCON);
1551da177e4SLinus Torvalds 	writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	unsigned long tmp;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	tmp = readl(i2c->regs + S3C2410_IICCON);
1631da177e4SLinus Torvalds 	writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds /* s3c24xx_i2c_message_start
1681da177e4SLinus Torvalds  *
1691da177e4SLinus Torvalds  * put the start of a message onto the bus
1701da177e4SLinus Torvalds */
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
1731da177e4SLinus Torvalds 				      struct i2c_msg *msg)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	unsigned int addr = (msg->addr & 0x7f) << 1;
1761da177e4SLinus Torvalds 	unsigned long stat;
1771da177e4SLinus Torvalds 	unsigned long iiccon;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	stat = 0;
1801da177e4SLinus Torvalds 	stat |=  S3C2410_IICSTAT_TXRXEN;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	if (msg->flags & I2C_M_RD) {
1831da177e4SLinus Torvalds 		stat |= S3C2410_IICSTAT_MASTER_RX;
1841da177e4SLinus Torvalds 		addr |= 1;
1851da177e4SLinus Torvalds 	} else
1861da177e4SLinus Torvalds 		stat |= S3C2410_IICSTAT_MASTER_TX;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	if (msg->flags & I2C_M_REV_DIR_ADDR)
1891da177e4SLinus Torvalds 		addr ^= 1;
1901da177e4SLinus Torvalds 
1913d0911bfSBen Dooks 	/* todo - check for wether ack wanted or not */
1921da177e4SLinus Torvalds 	s3c24xx_i2c_enable_ack(i2c);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	iiccon = readl(i2c->regs + S3C2410_IICCON);
1951da177e4SLinus Torvalds 	writel(stat, i2c->regs + S3C2410_IICSTAT);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
1981da177e4SLinus Torvalds 	writeb(addr, i2c->regs + S3C2410_IICDS);
1991da177e4SLinus Torvalds 
200e00a8cdfSBen Dooks 	/* delay here to ensure the data byte has gotten onto the bus
201e00a8cdfSBen Dooks 	 * before the transaction is started */
202e00a8cdfSBen Dooks 
203e00a8cdfSBen Dooks 	ndelay(i2c->tx_setup);
204e00a8cdfSBen Dooks 
2051da177e4SLinus Torvalds 	dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
2061da177e4SLinus Torvalds 	writel(iiccon, i2c->regs + S3C2410_IICCON);
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	stat |= S3C2410_IICSTAT_START;
2091da177e4SLinus Torvalds 	writel(stat, i2c->regs + S3C2410_IICSTAT);
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	dev_dbg(i2c->dev, "STOP\n");
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	/* stop the transfer */
2191da177e4SLinus Torvalds 	iicstat &= ~S3C2410_IICSTAT_START;
2201da177e4SLinus Torvalds 	writel(iicstat, i2c->regs + S3C2410_IICSTAT);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	i2c->state = STATE_STOP;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	s3c24xx_i2c_master_complete(i2c, ret);
2251da177e4SLinus Torvalds 	s3c24xx_i2c_disable_irq(i2c);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds /* helper functions to determine the current state in the set of
2291da177e4SLinus Torvalds  * messages we are sending */
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds /* is_lastmsg()
2321da177e4SLinus Torvalds  *
2331da177e4SLinus Torvalds  * returns TRUE if the current message is the last in the set
2341da177e4SLinus Torvalds */
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	return i2c->msg_idx >= (i2c->msg_num - 1);
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds /* is_msglast
2421da177e4SLinus Torvalds  *
2431da177e4SLinus Torvalds  * returns TRUE if we this is the last byte in the current message
2441da177e4SLinus Torvalds */
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds static inline int is_msglast(struct s3c24xx_i2c *i2c)
2471da177e4SLinus Torvalds {
2481da177e4SLinus Torvalds 	return i2c->msg_ptr == i2c->msg->len-1;
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds /* is_msgend
2521da177e4SLinus Torvalds  *
2531da177e4SLinus Torvalds  * returns TRUE if we reached the end of the current message
2541da177e4SLinus Torvalds */
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static inline int is_msgend(struct s3c24xx_i2c *i2c)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	return i2c->msg_ptr >= i2c->msg->len;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
26119820510SHuisung Kang /* i2c_s3c_irq_nextbyte
2621da177e4SLinus Torvalds  *
2631da177e4SLinus Torvalds  * process an interrupt and work out what to do
2641da177e4SLinus Torvalds  */
2651da177e4SLinus Torvalds 
26619820510SHuisung Kang static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	unsigned long tmp;
2691da177e4SLinus Torvalds 	unsigned char byte;
2701da177e4SLinus Torvalds 	int ret = 0;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	switch (i2c->state) {
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	case STATE_IDLE:
27508882d20SHarvey Harrison 		dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
2761da177e4SLinus Torvalds 		goto out;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	case STATE_STOP:
27908882d20SHarvey Harrison 		dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
2801da177e4SLinus Torvalds 		s3c24xx_i2c_disable_irq(i2c);
2811da177e4SLinus Torvalds 		goto out_ack;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	case STATE_START:
2841da177e4SLinus Torvalds 		/* last thing we did was send a start condition on the
2851da177e4SLinus Torvalds 		 * bus, or started a new i2c message
2861da177e4SLinus Torvalds 		 */
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 		if (iicstat & S3C2410_IICSTAT_LASTBIT &&
2891da177e4SLinus Torvalds 		    !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
2901da177e4SLinus Torvalds 			/* ack was not received... */
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 			dev_dbg(i2c->dev, "ack was not received\n");
29363f5c289SBen Dooks 			s3c24xx_i2c_stop(i2c, -ENXIO);
2941da177e4SLinus Torvalds 			goto out_ack;
2951da177e4SLinus Torvalds 		}
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 		if (i2c->msg->flags & I2C_M_RD)
2981da177e4SLinus Torvalds 			i2c->state = STATE_READ;
2991da177e4SLinus Torvalds 		else
3001da177e4SLinus Torvalds 			i2c->state = STATE_WRITE;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 		/* terminate the transfer if there is nothing to do
30363f5c289SBen Dooks 		 * as this is used by the i2c probe to find devices. */
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 		if (is_lastmsg(i2c) && i2c->msg->len == 0) {
3061da177e4SLinus Torvalds 			s3c24xx_i2c_stop(i2c, 0);
3071da177e4SLinus Torvalds 			goto out_ack;
3081da177e4SLinus Torvalds 		}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 		if (i2c->state == STATE_READ)
3111da177e4SLinus Torvalds 			goto prepare_read;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 		/* fall through to the write state, as we will need to
3141da177e4SLinus Torvalds 		 * send a byte as well */
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	case STATE_WRITE:
3171da177e4SLinus Torvalds 		/* we are writing data to the device... check for the
3181da177e4SLinus Torvalds 		 * end of the message, and if so, work out what to do
3191da177e4SLinus Torvalds 		 */
3201da177e4SLinus Torvalds 
3212709781bSBen Dooks 		if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
3222709781bSBen Dooks 			if (iicstat & S3C2410_IICSTAT_LASTBIT) {
3232709781bSBen Dooks 				dev_dbg(i2c->dev, "WRITE: No Ack\n");
3242709781bSBen Dooks 
3252709781bSBen Dooks 				s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
3262709781bSBen Dooks 				goto out_ack;
3272709781bSBen Dooks 			}
3282709781bSBen Dooks 		}
3292709781bSBen Dooks 
3301da177e4SLinus Torvalds  retry_write:
3312709781bSBen Dooks 
3321da177e4SLinus Torvalds 		if (!is_msgend(i2c)) {
3331da177e4SLinus Torvalds 			byte = i2c->msg->buf[i2c->msg_ptr++];
3341da177e4SLinus Torvalds 			writeb(byte, i2c->regs + S3C2410_IICDS);
3351da177e4SLinus Torvalds 
336e00a8cdfSBen Dooks 			/* delay after writing the byte to allow the
337e00a8cdfSBen Dooks 			 * data setup time on the bus, as writing the
338e00a8cdfSBen Dooks 			 * data to the register causes the first bit
339e00a8cdfSBen Dooks 			 * to appear on SDA, and SCL will change as
340e00a8cdfSBen Dooks 			 * soon as the interrupt is acknowledged */
341e00a8cdfSBen Dooks 
342e00a8cdfSBen Dooks 			ndelay(i2c->tx_setup);
343e00a8cdfSBen Dooks 
3441da177e4SLinus Torvalds 		} else if (!is_lastmsg(i2c)) {
3451da177e4SLinus Torvalds 			/* we need to go to the next i2c message */
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 			dev_dbg(i2c->dev, "WRITE: Next Message\n");
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 			i2c->msg_ptr = 0;
3501da177e4SLinus Torvalds 			i2c->msg_idx++;
3511da177e4SLinus Torvalds 			i2c->msg++;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 			/* check to see if we need to do another message */
3541da177e4SLinus Torvalds 			if (i2c->msg->flags & I2C_M_NOSTART) {
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 				if (i2c->msg->flags & I2C_M_RD) {
3571da177e4SLinus Torvalds 					/* cannot do this, the controller
3581da177e4SLinus Torvalds 					 * forces us to send a new START
3591da177e4SLinus Torvalds 					 * when we change direction */
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 					s3c24xx_i2c_stop(i2c, -EINVAL);
3621da177e4SLinus Torvalds 				}
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 				goto retry_write;
3651da177e4SLinus Torvalds 			} else {
3661da177e4SLinus Torvalds 				/* send the new start */
3671da177e4SLinus Torvalds 				s3c24xx_i2c_message_start(i2c, i2c->msg);
3681da177e4SLinus Torvalds 				i2c->state = STATE_START;
3691da177e4SLinus Torvalds 			}
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 		} else {
3721da177e4SLinus Torvalds 			/* send stop */
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 			s3c24xx_i2c_stop(i2c, 0);
3751da177e4SLinus Torvalds 		}
3761da177e4SLinus Torvalds 		break;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	case STATE_READ:
3791da177e4SLinus Torvalds 		/* we have a byte of data in the data register, do
3801da177e4SLinus Torvalds 		 * something with it, and then work out wether we are
3811da177e4SLinus Torvalds 		 * going to do any more read/write
3821da177e4SLinus Torvalds 		 */
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 		byte = readb(i2c->regs + S3C2410_IICDS);
3851da177e4SLinus Torvalds 		i2c->msg->buf[i2c->msg_ptr++] = byte;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds  prepare_read:
3881da177e4SLinus Torvalds 		if (is_msglast(i2c)) {
3891da177e4SLinus Torvalds 			/* last byte of buffer */
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 			if (is_lastmsg(i2c))
3921da177e4SLinus Torvalds 				s3c24xx_i2c_disable_ack(i2c);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 		} else if (is_msgend(i2c)) {
3951da177e4SLinus Torvalds 			/* ok, we've read the entire buffer, see if there
3961da177e4SLinus Torvalds 			 * is anything else we need to do */
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 			if (is_lastmsg(i2c)) {
3991da177e4SLinus Torvalds 				/* last message, send stop and complete */
4001da177e4SLinus Torvalds 				dev_dbg(i2c->dev, "READ: Send Stop\n");
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 				s3c24xx_i2c_stop(i2c, 0);
4031da177e4SLinus Torvalds 			} else {
4041da177e4SLinus Torvalds 				/* go to the next transfer */
4051da177e4SLinus Torvalds 				dev_dbg(i2c->dev, "READ: Next Transfer\n");
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 				i2c->msg_ptr = 0;
4081da177e4SLinus Torvalds 				i2c->msg_idx++;
4091da177e4SLinus Torvalds 				i2c->msg++;
4101da177e4SLinus Torvalds 			}
4111da177e4SLinus Torvalds 		}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		break;
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds 	/* acknowlegde the IRQ and get back on with the work */
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds  out_ack:
4191da177e4SLinus Torvalds 	tmp = readl(i2c->regs + S3C2410_IICCON);
4201da177e4SLinus Torvalds 	tmp &= ~S3C2410_IICCON_IRQPEND;
4211da177e4SLinus Torvalds 	writel(tmp, i2c->regs + S3C2410_IICCON);
4221da177e4SLinus Torvalds  out:
4231da177e4SLinus Torvalds 	return ret;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds /* s3c24xx_i2c_irq
4271da177e4SLinus Torvalds  *
4281da177e4SLinus Torvalds  * top level IRQ servicing routine
4291da177e4SLinus Torvalds */
4301da177e4SLinus Torvalds 
4317d12e780SDavid Howells static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct s3c24xx_i2c *i2c = dev_id;
4341da177e4SLinus Torvalds 	unsigned long status;
4351da177e4SLinus Torvalds 	unsigned long tmp;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	status = readl(i2c->regs + S3C2410_IICSTAT);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	if (status & S3C2410_IICSTAT_ARBITR) {
4403d0911bfSBen Dooks 		/* deal with arbitration loss */
4411da177e4SLinus Torvalds 		dev_err(i2c->dev, "deal with arbitration loss\n");
4421da177e4SLinus Torvalds 	}
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	if (i2c->state == STATE_IDLE) {
4451da177e4SLinus Torvalds 		dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 		tmp = readl(i2c->regs + S3C2410_IICCON);
4481da177e4SLinus Torvalds 		tmp &= ~S3C2410_IICCON_IRQPEND;
4491da177e4SLinus Torvalds 		writel(tmp, i2c->regs +  S3C2410_IICCON);
4501da177e4SLinus Torvalds 		goto out;
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	/* pretty much this leaves us with the fact that we've
4541da177e4SLinus Torvalds 	 * transmitted or received whatever byte we last sent */
4551da177e4SLinus Torvalds 
45619820510SHuisung Kang 	i2c_s3c_irq_nextbyte(i2c, status);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds  out:
4591da177e4SLinus Torvalds 	return IRQ_HANDLED;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds /* s3c24xx_i2c_set_master
4641da177e4SLinus Torvalds  *
4651da177e4SLinus Torvalds  * get the i2c bus for a master transaction
4661da177e4SLinus Torvalds */
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	unsigned long iicstat;
4711da177e4SLinus Torvalds 	int timeout = 400;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	while (timeout-- > 0) {
4741da177e4SLinus Torvalds 		iicstat = readl(i2c->regs + S3C2410_IICSTAT);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 		if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
4771da177e4SLinus Torvalds 			return 0;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 		msleep(1);
4801da177e4SLinus Torvalds 	}
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	return -ETIMEDOUT;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /* s3c24xx_i2c_doxfer
4861da177e4SLinus Torvalds  *
4871da177e4SLinus Torvalds  * this starts an i2c transfer
4881da177e4SLinus Torvalds */
4891da177e4SLinus Torvalds 
4903d0911bfSBen Dooks static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
4913d0911bfSBen Dooks 			      struct i2c_msg *msgs, int num)
4921da177e4SLinus Torvalds {
4931bc2962eSMark Brown 	unsigned long iicstat, timeout;
4941bc2962eSMark Brown 	int spins = 20;
4951da177e4SLinus Torvalds 	int ret;
4961da177e4SLinus Torvalds 
497be44f01eSBen Dooks 	if (i2c->suspended)
49861c7cff8SBen Dooks 		return -EIO;
49961c7cff8SBen Dooks 
5001da177e4SLinus Torvalds 	ret = s3c24xx_i2c_set_master(i2c);
5011da177e4SLinus Torvalds 	if (ret != 0) {
5021da177e4SLinus Torvalds 		dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
5031da177e4SLinus Torvalds 		ret = -EAGAIN;
5041da177e4SLinus Torvalds 		goto out;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	spin_lock_irq(&i2c->lock);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	i2c->msg     = msgs;
5101da177e4SLinus Torvalds 	i2c->msg_num = num;
5111da177e4SLinus Torvalds 	i2c->msg_ptr = 0;
5121da177e4SLinus Torvalds 	i2c->msg_idx = 0;
5131da177e4SLinus Torvalds 	i2c->state   = STATE_START;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	s3c24xx_i2c_enable_irq(i2c);
5161da177e4SLinus Torvalds 	s3c24xx_i2c_message_start(i2c, msgs);
5171da177e4SLinus Torvalds 	spin_unlock_irq(&i2c->lock);
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	ret = i2c->msg_idx;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	/* having these next two as dev_err() makes life very
5241da177e4SLinus Torvalds 	 * noisy when doing an i2cdetect */
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	if (timeout == 0)
5271da177e4SLinus Torvalds 		dev_dbg(i2c->dev, "timeout\n");
5281da177e4SLinus Torvalds 	else if (ret != num)
5291da177e4SLinus Torvalds 		dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	/* ensure the stop has been through the bus */
5321da177e4SLinus Torvalds 
5331bc2962eSMark Brown 	dev_dbg(i2c->dev, "waiting for bus idle\n");
5341bc2962eSMark Brown 
5351bc2962eSMark Brown 	/* first, try busy waiting briefly */
5361bc2962eSMark Brown 	do {
537*37de03eaSMark Brown 		cpu_relax();
5381bc2962eSMark Brown 		iicstat = readl(i2c->regs + S3C2410_IICSTAT);
5391bc2962eSMark Brown 	} while ((iicstat & S3C2410_IICSTAT_START) && --spins);
5401bc2962eSMark Brown 
5411bc2962eSMark Brown 	/* if that timed out sleep */
5421bc2962eSMark Brown 	if (!spins) {
5431da177e4SLinus Torvalds 		msleep(1);
5441bc2962eSMark Brown 		iicstat = readl(i2c->regs + S3C2410_IICSTAT);
5451bc2962eSMark Brown 	}
5461bc2962eSMark Brown 
5471bc2962eSMark Brown 	if (iicstat & S3C2410_IICSTAT_START)
5481bc2962eSMark Brown 		dev_warn(i2c->dev, "timeout waiting for bus idle\n");
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds  out:
5511da177e4SLinus Torvalds 	return ret;
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds /* s3c24xx_i2c_xfer
5551da177e4SLinus Torvalds  *
5561da177e4SLinus Torvalds  * first port of call from the i2c bus code when an message needs
55744bbe87eSSteven Cole  * transferring across the i2c bus.
5581da177e4SLinus Torvalds */
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
5611da177e4SLinus Torvalds 			struct i2c_msg *msgs, int num)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
5641da177e4SLinus Torvalds 	int retry;
5651da177e4SLinus Torvalds 	int ret;
5661da177e4SLinus Torvalds 
567d2360b8eSArnaud Patard (Rtp) 	clk_enable(i2c->clk);
568d2360b8eSArnaud Patard (Rtp) 
5691da177e4SLinus Torvalds 	for (retry = 0; retry < adap->retries; retry++) {
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 		ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
5721da177e4SLinus Torvalds 
573d2360b8eSArnaud Patard (Rtp) 		if (ret != -EAGAIN) {
574d2360b8eSArnaud Patard (Rtp) 			clk_disable(i2c->clk);
5751da177e4SLinus Torvalds 			return ret;
576d2360b8eSArnaud Patard (Rtp) 		}
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 		dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 		udelay(100);
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 
583d2360b8eSArnaud Patard (Rtp) 	clk_disable(i2c->clk);
5841da177e4SLinus Torvalds 	return -EREMOTEIO;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds /* declare our i2c functionality */
5881da177e4SLinus Torvalds static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
5891da177e4SLinus Torvalds {
5901da177e4SLinus Torvalds 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING;
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds /* i2c bus registration info */
5941da177e4SLinus Torvalds 
5958f9082c5SJean Delvare static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
5961da177e4SLinus Torvalds 	.master_xfer		= s3c24xx_i2c_xfer,
5971da177e4SLinus Torvalds 	.functionality		= s3c24xx_i2c_func,
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 
63061c7cff8SBen Dooks /* s3c24xx_i2c_clockrate
6311da177e4SLinus Torvalds  *
6321da177e4SLinus Torvalds  * work out a divisor for the user requested frequency setting,
6331da177e4SLinus Torvalds  * either by the requested frequency, or scanning the acceptable
6341da177e4SLinus Torvalds  * range of frequencies until something is found
6351da177e4SLinus Torvalds */
6361da177e4SLinus Torvalds 
63761c7cff8SBen Dooks static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
6381da177e4SLinus Torvalds {
6394fd81eb2SThomas Abraham 	struct s3c2410_platform_i2c *pdata = i2c->pdata;
6401da177e4SLinus Torvalds 	unsigned long clkin = clk_get_rate(i2c->clk);
6411da177e4SLinus Torvalds 	unsigned int divs, div1;
642c564e6aeSDaniel Silverstone 	unsigned long target_frequency;
64361c7cff8SBen Dooks 	u32 iiccon;
6441da177e4SLinus Torvalds 	int freq;
6451da177e4SLinus Torvalds 
64661c7cff8SBen Dooks 	i2c->clkrate = clkin;
6471da177e4SLinus Torvalds 	clkin /= 1000;		/* clkin now in KHz */
6481da177e4SLinus Torvalds 
649c564e6aeSDaniel Silverstone 	dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
6501da177e4SLinus Torvalds 
651c564e6aeSDaniel Silverstone 	target_frequency = pdata->frequency ? pdata->frequency : 100000;
6521da177e4SLinus Torvalds 
653c564e6aeSDaniel Silverstone 	target_frequency /= 1000; /* Target frequency now in KHz */
6541da177e4SLinus Torvalds 
655c564e6aeSDaniel Silverstone 	freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
6561da177e4SLinus Torvalds 
657c564e6aeSDaniel Silverstone 	if (freq > target_frequency) {
658c564e6aeSDaniel Silverstone 		dev_err(i2c->dev,
659c564e6aeSDaniel Silverstone 			"Unable to achieve desired frequency %luKHz."	\
660c564e6aeSDaniel Silverstone 			" Lowest achievable %dKHz\n", target_frequency, freq);
6611da177e4SLinus Torvalds 		return -EINVAL;
662c564e6aeSDaniel Silverstone 	}
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	*got = freq;
66561c7cff8SBen Dooks 
66661c7cff8SBen Dooks 	iiccon = readl(i2c->regs + S3C2410_IICCON);
66761c7cff8SBen Dooks 	iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
66861c7cff8SBen Dooks 	iiccon |= (divs-1);
66961c7cff8SBen Dooks 
67061c7cff8SBen Dooks 	if (div1 == 512)
67161c7cff8SBen Dooks 		iiccon |= S3C2410_IICCON_TXDIV_512;
67261c7cff8SBen Dooks 
67361c7cff8SBen Dooks 	writel(iiccon, i2c->regs + S3C2410_IICCON);
67461c7cff8SBen Dooks 
675a192f715SBen Dooks 	if (s3c24xx_i2c_is2440(i2c)) {
676a192f715SBen Dooks 		unsigned long sda_delay;
677a192f715SBen Dooks 
678a192f715SBen Dooks 		if (pdata->sda_delay) {
6797031307aSMyungJoo Ham 			sda_delay = clkin * pdata->sda_delay;
6807031307aSMyungJoo Ham 			sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
681a192f715SBen Dooks 			sda_delay = DIV_ROUND_UP(sda_delay, 5);
682a192f715SBen Dooks 			if (sda_delay > 3)
683a192f715SBen Dooks 				sda_delay = 3;
684a192f715SBen Dooks 			sda_delay |= S3C2410_IICLC_FILTER_ON;
685a192f715SBen Dooks 		} else
686a192f715SBen Dooks 			sda_delay = 0;
687a192f715SBen Dooks 
688a192f715SBen Dooks 		dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
689a192f715SBen Dooks 		writel(sda_delay, i2c->regs + S3C2440_IICLC);
690a192f715SBen Dooks 	}
691a192f715SBen Dooks 
6921da177e4SLinus Torvalds 	return 0;
6931da177e4SLinus Torvalds }
6941da177e4SLinus Torvalds 
69561c7cff8SBen Dooks #ifdef CONFIG_CPU_FREQ
69661c7cff8SBen Dooks 
69761c7cff8SBen Dooks #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
69861c7cff8SBen Dooks 
69961c7cff8SBen Dooks static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
70061c7cff8SBen Dooks 					  unsigned long val, void *data)
70161c7cff8SBen Dooks {
70261c7cff8SBen Dooks 	struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
70361c7cff8SBen Dooks 	unsigned long flags;
70461c7cff8SBen Dooks 	unsigned int got;
70561c7cff8SBen Dooks 	int delta_f;
70661c7cff8SBen Dooks 	int ret;
70761c7cff8SBen Dooks 
70861c7cff8SBen Dooks 	delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
70961c7cff8SBen Dooks 
71061c7cff8SBen Dooks 	/* if we're post-change and the input clock has slowed down
71161c7cff8SBen Dooks 	 * or at pre-change and the clock is about to speed up, then
71261c7cff8SBen Dooks 	 * adjust our clock rate. <0 is slow, >0 speedup.
71361c7cff8SBen Dooks 	 */
71461c7cff8SBen Dooks 
71561c7cff8SBen Dooks 	if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
71661c7cff8SBen Dooks 	    (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
71761c7cff8SBen Dooks 		spin_lock_irqsave(&i2c->lock, flags);
71861c7cff8SBen Dooks 		ret = s3c24xx_i2c_clockrate(i2c, &got);
71961c7cff8SBen Dooks 		spin_unlock_irqrestore(&i2c->lock, flags);
72061c7cff8SBen Dooks 
72161c7cff8SBen Dooks 		if (ret < 0)
72261c7cff8SBen Dooks 			dev_err(i2c->dev, "cannot find frequency\n");
72361c7cff8SBen Dooks 		else
72461c7cff8SBen Dooks 			dev_info(i2c->dev, "setting freq %d\n", got);
72561c7cff8SBen Dooks 	}
72661c7cff8SBen Dooks 
72761c7cff8SBen Dooks 	return 0;
72861c7cff8SBen Dooks }
72961c7cff8SBen Dooks 
73061c7cff8SBen Dooks static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
73161c7cff8SBen Dooks {
73261c7cff8SBen Dooks 	i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
73361c7cff8SBen Dooks 
73461c7cff8SBen Dooks 	return cpufreq_register_notifier(&i2c->freq_transition,
73561c7cff8SBen Dooks 					 CPUFREQ_TRANSITION_NOTIFIER);
73661c7cff8SBen Dooks }
73761c7cff8SBen Dooks 
73861c7cff8SBen Dooks static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
73961c7cff8SBen Dooks {
74061c7cff8SBen Dooks 	cpufreq_unregister_notifier(&i2c->freq_transition,
74161c7cff8SBen Dooks 				    CPUFREQ_TRANSITION_NOTIFIER);
74261c7cff8SBen Dooks }
74361c7cff8SBen Dooks 
74461c7cff8SBen Dooks #else
74561c7cff8SBen Dooks static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
74661c7cff8SBen Dooks {
74761c7cff8SBen Dooks 	return 0;
74861c7cff8SBen Dooks }
74961c7cff8SBen Dooks 
75061c7cff8SBen Dooks static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
75161c7cff8SBen Dooks {
75261c7cff8SBen Dooks }
75361c7cff8SBen Dooks #endif
75461c7cff8SBen Dooks 
7555a5f5080SThomas Abraham #ifdef CONFIG_OF
7565a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
7575a5f5080SThomas Abraham {
7585a5f5080SThomas Abraham 	int idx, gpio, ret;
7595a5f5080SThomas Abraham 
7605a5f5080SThomas Abraham 	for (idx = 0; idx < 2; idx++) {
7615a5f5080SThomas Abraham 		gpio = of_get_gpio(i2c->dev->of_node, idx);
7625a5f5080SThomas Abraham 		if (!gpio_is_valid(gpio)) {
7635a5f5080SThomas Abraham 			dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
7645a5f5080SThomas Abraham 			goto free_gpio;
7655a5f5080SThomas Abraham 		}
7665a5f5080SThomas Abraham 
7675a5f5080SThomas Abraham 		ret = gpio_request(gpio, "i2c-bus");
7685a5f5080SThomas Abraham 		if (ret) {
7695a5f5080SThomas Abraham 			dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
7705a5f5080SThomas Abraham 			goto free_gpio;
7715a5f5080SThomas Abraham 		}
7725a5f5080SThomas Abraham 	}
7735a5f5080SThomas Abraham 	return 0;
7745a5f5080SThomas Abraham 
7755a5f5080SThomas Abraham free_gpio:
7765a5f5080SThomas Abraham 	while (--idx >= 0)
7775a5f5080SThomas Abraham 		gpio_free(i2c->gpios[idx]);
7785a5f5080SThomas Abraham 	return -EINVAL;
7795a5f5080SThomas Abraham }
7805a5f5080SThomas Abraham 
7815a5f5080SThomas Abraham static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
7825a5f5080SThomas Abraham {
7835a5f5080SThomas Abraham 	unsigned int idx;
7845a5f5080SThomas Abraham 	for (idx = 0; idx < 2; idx++)
7855a5f5080SThomas Abraham 		gpio_free(i2c->gpios[idx]);
7865a5f5080SThomas Abraham }
7875a5f5080SThomas Abraham #else
7885a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
7895a5f5080SThomas Abraham {
7905a5f5080SThomas Abraham 	return -EINVAL;
7915a5f5080SThomas Abraham }
7925a5f5080SThomas Abraham 
7935a5f5080SThomas Abraham static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
7945a5f5080SThomas Abraham {
7955a5f5080SThomas Abraham }
7965a5f5080SThomas Abraham #endif
7975a5f5080SThomas Abraham 
7981da177e4SLinus Torvalds /* s3c24xx_i2c_init
7991da177e4SLinus Torvalds  *
8001da177e4SLinus Torvalds  * initialise the controller, set the IO lines and frequency
8011da177e4SLinus Torvalds */
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
8041da177e4SLinus Torvalds {
8051da177e4SLinus Torvalds 	unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
8061da177e4SLinus Torvalds 	struct s3c2410_platform_i2c *pdata;
8071da177e4SLinus Torvalds 	unsigned int freq;
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	/* get the plafrom data */
8101da177e4SLinus Torvalds 
8114fd81eb2SThomas Abraham 	pdata = i2c->pdata;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	/* inititalise the gpio */
8141da177e4SLinus Torvalds 
8158be310a6SBen Dooks 	if (pdata->cfg_gpio)
8168be310a6SBen Dooks 		pdata->cfg_gpio(to_platform_device(i2c->dev));
8175a5f5080SThomas Abraham 	else
8185a5f5080SThomas Abraham 		if (s3c24xx_i2c_parse_dt_gpio(i2c))
8195a5f5080SThomas Abraham 			return -EINVAL;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	/* write slave address */
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
8261da177e4SLinus Torvalds 
82761c7cff8SBen Dooks 	writel(iicon, i2c->regs + S3C2410_IICCON);
82861c7cff8SBen Dooks 
8291da177e4SLinus Torvalds 	/* we need to work out the divisors for the clock... */
8301da177e4SLinus Torvalds 
83161c7cff8SBen Dooks 	if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
83261c7cff8SBen Dooks 		writel(0, i2c->regs + S3C2410_IICCON);
8331da177e4SLinus Torvalds 		dev_err(i2c->dev, "cannot meet bus frequency required\n");
8341da177e4SLinus Torvalds 		return -EINVAL;
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	/* todo - check that the i2c lines aren't being dragged anywhere */
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
8401da177e4SLinus Torvalds 	dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	return 0;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
8455a5f5080SThomas Abraham #ifdef CONFIG_OF
8465a5f5080SThomas Abraham /* s3c24xx_i2c_parse_dt
8475a5f5080SThomas Abraham  *
8485a5f5080SThomas Abraham  * Parse the device tree node and retreive the platform data.
8495a5f5080SThomas Abraham */
8505a5f5080SThomas Abraham 
8515a5f5080SThomas Abraham static void
8525a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
8535a5f5080SThomas Abraham {
8545a5f5080SThomas Abraham 	struct s3c2410_platform_i2c *pdata = i2c->pdata;
8555a5f5080SThomas Abraham 
8565a5f5080SThomas Abraham 	if (!np)
8575a5f5080SThomas Abraham 		return;
8585a5f5080SThomas Abraham 
8595a5f5080SThomas Abraham 	pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
8605a5f5080SThomas Abraham 	of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
8615a5f5080SThomas Abraham 	of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
8625a5f5080SThomas Abraham 	of_property_read_u32(np, "samsung,i2c-max-bus-freq",
8635a5f5080SThomas Abraham 				(u32 *)&pdata->frequency);
8645a5f5080SThomas Abraham }
8655a5f5080SThomas Abraham #else
8665a5f5080SThomas Abraham static void
8675a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
8685a5f5080SThomas Abraham {
8695a5f5080SThomas Abraham 	return;
8705a5f5080SThomas Abraham }
8715a5f5080SThomas Abraham #endif
8725a5f5080SThomas Abraham 
8731da177e4SLinus Torvalds /* s3c24xx_i2c_probe
8741da177e4SLinus Torvalds  *
8751da177e4SLinus Torvalds  * called by the bus driver when a suitable device is found
8761da177e4SLinus Torvalds */
8771da177e4SLinus Torvalds 
8783ae5eaecSRussell King static int s3c24xx_i2c_probe(struct platform_device *pdev)
8791da177e4SLinus Torvalds {
880692acbd3SBen Dooks 	struct s3c24xx_i2c *i2c;
8814fd81eb2SThomas Abraham 	struct s3c2410_platform_i2c *pdata = NULL;
8821da177e4SLinus Torvalds 	struct resource *res;
8831da177e4SLinus Torvalds 	int ret;
8841da177e4SLinus Torvalds 
8855a5f5080SThomas Abraham 	if (!pdev->dev.of_node) {
8866a039cabSBen Dooks 		pdata = pdev->dev.platform_data;
8876a039cabSBen Dooks 		if (!pdata) {
8886a039cabSBen Dooks 			dev_err(&pdev->dev, "no platform data\n");
8896a039cabSBen Dooks 			return -EINVAL;
8906a039cabSBen Dooks 		}
8915a5f5080SThomas Abraham 	}
892399dee23SBen Dooks 
893692acbd3SBen Dooks 	i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL);
894692acbd3SBen Dooks 	if (!i2c) {
895692acbd3SBen Dooks 		dev_err(&pdev->dev, "no memory for state\n");
896692acbd3SBen Dooks 		return -ENOMEM;
897692acbd3SBen Dooks 	}
898692acbd3SBen Dooks 
8994fd81eb2SThomas Abraham 	i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
9004fd81eb2SThomas Abraham 	if (!i2c->pdata) {
9014fd81eb2SThomas Abraham 		ret = -ENOMEM;
9024fd81eb2SThomas Abraham 		goto err_noclk;
9034fd81eb2SThomas Abraham 	}
9044fd81eb2SThomas Abraham 
9054fd81eb2SThomas Abraham 	if (pdata)
9064fd81eb2SThomas Abraham 		memcpy(i2c->pdata, pdata, sizeof(*pdata));
9075a5f5080SThomas Abraham 	else
9085a5f5080SThomas Abraham 		s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
9094fd81eb2SThomas Abraham 
910692acbd3SBen Dooks 	strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
911692acbd3SBen Dooks 	i2c->adap.owner   = THIS_MODULE;
912692acbd3SBen Dooks 	i2c->adap.algo    = &s3c24xx_i2c_algorithm;
913692acbd3SBen Dooks 	i2c->adap.retries = 2;
914692acbd3SBen Dooks 	i2c->adap.class   = I2C_CLASS_HWMON | I2C_CLASS_SPD;
915692acbd3SBen Dooks 	i2c->tx_setup     = 50;
916692acbd3SBen Dooks 
917692acbd3SBen Dooks 	spin_lock_init(&i2c->lock);
918692acbd3SBen Dooks 	init_waitqueue_head(&i2c->wait);
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	/* find the clock and enable it */
9211da177e4SLinus Torvalds 
9223ae5eaecSRussell King 	i2c->dev = &pdev->dev;
9233ae5eaecSRussell King 	i2c->clk = clk_get(&pdev->dev, "i2c");
9241da177e4SLinus Torvalds 	if (IS_ERR(i2c->clk)) {
9253ae5eaecSRussell King 		dev_err(&pdev->dev, "cannot get clock\n");
9261da177e4SLinus Torvalds 		ret = -ENOENT;
9275b68790cSBen Dooks 		goto err_noclk;
9281da177e4SLinus Torvalds 	}
9291da177e4SLinus Torvalds 
9303ae5eaecSRussell King 	dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	clk_enable(i2c->clk);
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	/* map the registers */
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9371da177e4SLinus Torvalds 	if (res == NULL) {
9383ae5eaecSRussell King 		dev_err(&pdev->dev, "cannot find IO resource\n");
9391da177e4SLinus Torvalds 		ret = -ENOENT;
9405b68790cSBen Dooks 		goto err_clk;
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 
943933a2aecSBen Dooks 	i2c->ioarea = request_mem_region(res->start, resource_size(res),
9441da177e4SLinus Torvalds 					 pdev->name);
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds 	if (i2c->ioarea == NULL) {
9473ae5eaecSRussell King 		dev_err(&pdev->dev, "cannot request IO\n");
9481da177e4SLinus Torvalds 		ret = -ENXIO;
9495b68790cSBen Dooks 		goto err_clk;
9501da177e4SLinus Torvalds 	}
9511da177e4SLinus Torvalds 
952933a2aecSBen Dooks 	i2c->regs = ioremap(res->start, resource_size(res));
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	if (i2c->regs == NULL) {
9553ae5eaecSRussell King 		dev_err(&pdev->dev, "cannot map IO\n");
9561da177e4SLinus Torvalds 		ret = -ENXIO;
9575b68790cSBen Dooks 		goto err_ioarea;
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
9603d0911bfSBen Dooks 	dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
9613d0911bfSBen Dooks 		i2c->regs, i2c->ioarea, res);
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	/* setup info block for the i2c core */
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	i2c->adap.algo_data = i2c;
9663ae5eaecSRussell King 	i2c->adap.dev.parent = &pdev->dev;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	/* initialise the i2c controller */
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	ret = s3c24xx_i2c_init(i2c);
9711da177e4SLinus Torvalds 	if (ret != 0)
9725b68790cSBen Dooks 		goto err_iomap;
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	/* find the IRQ for this unit (note, this relies on the init call to
9751da177e4SLinus Torvalds 	 * ensure no current IRQs pending
9761da177e4SLinus Torvalds 	 */
9771da177e4SLinus Torvalds 
978e0d1ec97SBen Dooks 	i2c->irq = ret = platform_get_irq(pdev, 0);
979e0d1ec97SBen Dooks 	if (ret <= 0) {
9803ae5eaecSRussell King 		dev_err(&pdev->dev, "cannot find IRQ\n");
9815b68790cSBen Dooks 		goto err_iomap;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 
9844311051cSYong Zhang 	ret = request_irq(i2c->irq, s3c24xx_i2c_irq, 0,
985e0d1ec97SBen Dooks 			  dev_name(&pdev->dev), i2c);
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	if (ret != 0) {
988e0d1ec97SBen Dooks 		dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
9895b68790cSBen Dooks 		goto err_iomap;
9901da177e4SLinus Torvalds 	}
9911da177e4SLinus Torvalds 
99261c7cff8SBen Dooks 	ret = s3c24xx_i2c_register_cpufreq(i2c);
99361c7cff8SBen Dooks 	if (ret < 0) {
99461c7cff8SBen Dooks 		dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
99561c7cff8SBen Dooks 		goto err_irq;
99661c7cff8SBen Dooks 	}
99761c7cff8SBen Dooks 
998399dee23SBen Dooks 	/* Note, previous versions of the driver used i2c_add_adapter()
999399dee23SBen Dooks 	 * to add the bus at any number. We now pass the bus number via
1000399dee23SBen Dooks 	 * the platform data, so if unset it will now default to always
1001399dee23SBen Dooks 	 * being bus 0.
1002399dee23SBen Dooks 	 */
1003399dee23SBen Dooks 
10044fd81eb2SThomas Abraham 	i2c->adap.nr = i2c->pdata->bus_num;
10055a5f5080SThomas Abraham 	i2c->adap.dev.of_node = pdev->dev.of_node;
1006399dee23SBen Dooks 
1007399dee23SBen Dooks 	ret = i2c_add_numbered_adapter(&i2c->adap);
10081da177e4SLinus Torvalds 	if (ret < 0) {
10093ae5eaecSRussell King 		dev_err(&pdev->dev, "failed to add bus to i2c core\n");
101061c7cff8SBen Dooks 		goto err_cpufreq;
10111da177e4SLinus Torvalds 	}
10121da177e4SLinus Torvalds 
10135a5f5080SThomas Abraham 	of_i2c_register_devices(&i2c->adap);
10143ae5eaecSRussell King 	platform_set_drvdata(pdev, i2c);
10151da177e4SLinus Torvalds 
101622e965c2SJean Delvare 	dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
1017d2360b8eSArnaud Patard (Rtp) 	clk_disable(i2c->clk);
10185b68790cSBen Dooks 	return 0;
10191da177e4SLinus Torvalds 
102061c7cff8SBen Dooks  err_cpufreq:
102161c7cff8SBen Dooks 	s3c24xx_i2c_deregister_cpufreq(i2c);
102261c7cff8SBen Dooks 
10235b68790cSBen Dooks  err_irq:
1024e0d1ec97SBen Dooks 	free_irq(i2c->irq, i2c);
10251da177e4SLinus Torvalds 
10265b68790cSBen Dooks  err_iomap:
10275b68790cSBen Dooks 	iounmap(i2c->regs);
10285b68790cSBen Dooks 
10295b68790cSBen Dooks  err_ioarea:
10305b68790cSBen Dooks 	release_resource(i2c->ioarea);
10315b68790cSBen Dooks 	kfree(i2c->ioarea);
10325b68790cSBen Dooks 
10335b68790cSBen Dooks  err_clk:
10345b68790cSBen Dooks 	clk_disable(i2c->clk);
10355b68790cSBen Dooks 	clk_put(i2c->clk);
10365b68790cSBen Dooks 
10375b68790cSBen Dooks  err_noclk:
1038692acbd3SBen Dooks 	kfree(i2c);
10391da177e4SLinus Torvalds 	return ret;
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds /* s3c24xx_i2c_remove
10431da177e4SLinus Torvalds  *
10441da177e4SLinus Torvalds  * called when device is removed from the bus
10451da177e4SLinus Torvalds */
10461da177e4SLinus Torvalds 
10473ae5eaecSRussell King static int s3c24xx_i2c_remove(struct platform_device *pdev)
10481da177e4SLinus Torvalds {
10493ae5eaecSRussell King 	struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
10501da177e4SLinus Torvalds 
105161c7cff8SBen Dooks 	s3c24xx_i2c_deregister_cpufreq(i2c);
105261c7cff8SBen Dooks 
10535b68790cSBen Dooks 	i2c_del_adapter(&i2c->adap);
1054e0d1ec97SBen Dooks 	free_irq(i2c->irq, i2c);
10555b68790cSBen Dooks 
10565b68790cSBen Dooks 	clk_disable(i2c->clk);
10575b68790cSBen Dooks 	clk_put(i2c->clk);
10585b68790cSBen Dooks 
10595b68790cSBen Dooks 	iounmap(i2c->regs);
10605b68790cSBen Dooks 
10615b68790cSBen Dooks 	release_resource(i2c->ioarea);
10625a5f5080SThomas Abraham 	s3c24xx_i2c_dt_gpio_free(i2c);
10635b68790cSBen Dooks 	kfree(i2c->ioarea);
1064692acbd3SBen Dooks 	kfree(i2c);
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	return 0;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
10691da177e4SLinus Torvalds #ifdef CONFIG_PM
10706a6c6189SMagnus Damm static int s3c24xx_i2c_suspend_noirq(struct device *dev)
1071be44f01eSBen Dooks {
10726a6c6189SMagnus Damm 	struct platform_device *pdev = to_platform_device(dev);
10736a6c6189SMagnus Damm 	struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
10746a6c6189SMagnus Damm 
1075be44f01eSBen Dooks 	i2c->suspended = 1;
10766a6c6189SMagnus Damm 
1077be44f01eSBen Dooks 	return 0;
1078be44f01eSBen Dooks }
1079be44f01eSBen Dooks 
10806a6c6189SMagnus Damm static int s3c24xx_i2c_resume(struct device *dev)
10811da177e4SLinus Torvalds {
10826a6c6189SMagnus Damm 	struct platform_device *pdev = to_platform_device(dev);
10836a6c6189SMagnus Damm 	struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
10841da177e4SLinus Torvalds 
1085be44f01eSBen Dooks 	i2c->suspended = 0;
1086d2360b8eSArnaud Patard (Rtp) 	clk_enable(i2c->clk);
10871da177e4SLinus Torvalds 	s3c24xx_i2c_init(i2c);
1088d2360b8eSArnaud Patard (Rtp) 	clk_disable(i2c->clk);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	return 0;
10911da177e4SLinus Torvalds }
10921da177e4SLinus Torvalds 
109347145210SAlexey Dobriyan static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
10946a6c6189SMagnus Damm 	.suspend_noirq = s3c24xx_i2c_suspend_noirq,
10956a6c6189SMagnus Damm 	.resume = s3c24xx_i2c_resume,
10966a6c6189SMagnus Damm };
10976a6c6189SMagnus Damm 
10986a6c6189SMagnus Damm #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
10991da177e4SLinus Torvalds #else
11006a6c6189SMagnus Damm #define S3C24XX_DEV_PM_OPS NULL
11011da177e4SLinus Torvalds #endif
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds /* device driver for platform bus bits */
11041da177e4SLinus Torvalds 
11057d85ccd8SBen Dooks static struct platform_device_id s3c24xx_driver_ids[] = {
11067d85ccd8SBen Dooks 	{
11071da177e4SLinus Torvalds 		.name		= "s3c2410-i2c",
11087d85ccd8SBen Dooks 		.driver_data	= TYPE_S3C2410,
11097d85ccd8SBen Dooks 	}, {
11107d85ccd8SBen Dooks 		.name		= "s3c2440-i2c",
11117d85ccd8SBen Dooks 		.driver_data	= TYPE_S3C2440,
11127d85ccd8SBen Dooks 	}, { },
11131da177e4SLinus Torvalds };
11147d85ccd8SBen Dooks MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
11151da177e4SLinus Torvalds 
11165a5f5080SThomas Abraham #ifdef CONFIG_OF
11175a5f5080SThomas Abraham static const struct of_device_id s3c24xx_i2c_match[] = {
11185a5f5080SThomas Abraham 	{ .compatible = "samsung,s3c2410-i2c" },
11195a5f5080SThomas Abraham 	{ .compatible = "samsung,s3c2440-i2c" },
11205a5f5080SThomas Abraham 	{},
11215a5f5080SThomas Abraham };
11225a5f5080SThomas Abraham MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
11235a5f5080SThomas Abraham #else
11245a5f5080SThomas Abraham #define s3c24xx_i2c_match NULL
11255a5f5080SThomas Abraham #endif
11265a5f5080SThomas Abraham 
11277d85ccd8SBen Dooks static struct platform_driver s3c24xx_i2c_driver = {
11281da177e4SLinus Torvalds 	.probe		= s3c24xx_i2c_probe,
11291da177e4SLinus Torvalds 	.remove		= s3c24xx_i2c_remove,
11307d85ccd8SBen Dooks 	.id_table	= s3c24xx_driver_ids,
11313ae5eaecSRussell King 	.driver		= {
11323ae5eaecSRussell King 		.owner	= THIS_MODULE,
11337d85ccd8SBen Dooks 		.name	= "s3c-i2c",
11346a6c6189SMagnus Damm 		.pm	= S3C24XX_DEV_PM_OPS,
11355a5f5080SThomas Abraham 		.of_match_table = s3c24xx_i2c_match,
11363ae5eaecSRussell King 	},
11371da177e4SLinus Torvalds };
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds static int __init i2c_adap_s3c_init(void)
11401da177e4SLinus Torvalds {
11417d85ccd8SBen Dooks 	return platform_driver_register(&s3c24xx_i2c_driver);
11421da177e4SLinus Torvalds }
114318dc83a6SMark Brown subsys_initcall(i2c_adap_s3c_init);
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds static void __exit i2c_adap_s3c_exit(void)
11461da177e4SLinus Torvalds {
11477d85ccd8SBen Dooks 	platform_driver_unregister(&s3c24xx_i2c_driver);
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds module_exit(i2c_adap_s3c_exit);
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
11521da177e4SLinus Torvalds MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
11531da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1154