xref: /linux/drivers/i2c/busses/i2c-s3c2410.c (revision b900ba4c1513a8c9a2fab8dca4cc6f50b17d6861)
11da177e4SLinus Torvalds /* linux/drivers/i2c/busses/i2c-s3c2410.c
21da177e4SLinus Torvalds  *
3c564e6aeSDaniel Silverstone  * Copyright (C) 2004,2005,2009 Simtec Electronics
41da177e4SLinus Torvalds  *	Ben Dooks <ben@simtec.co.uk>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * S3C2410 I2C Controller
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
91da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
101da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
111da177e4SLinus Torvalds  * (at your option) any later version.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
141da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
151da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
161da177e4SLinus Torvalds  * GNU General Public License for more details.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
191da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
201da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
211da177e4SLinus Torvalds */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/kernel.h>
241da177e4SLinus Torvalds #include <linux/module.h>
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/i2c.h>
271da177e4SLinus Torvalds #include <linux/init.h>
281da177e4SLinus Torvalds #include <linux/time.h>
291da177e4SLinus Torvalds #include <linux/interrupt.h>
301da177e4SLinus Torvalds #include <linux/delay.h>
311da177e4SLinus Torvalds #include <linux/errno.h>
321da177e4SLinus Torvalds #include <linux/err.h>
33d052d1beSRussell King #include <linux/platform_device.h>
34c62c3ca5SMark Brown #include <linux/pm_runtime.h>
35f8ce2547SRussell King #include <linux/clk.h>
3661c7cff8SBen Dooks #include <linux/cpufreq.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
3821782180SH Hartley Sweeten #include <linux/io.h>
395a5f5080SThomas Abraham #include <linux/of_i2c.h>
405a5f5080SThomas Abraham #include <linux/of_gpio.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/irq.h>
431da177e4SLinus Torvalds 
449498cb79SBen Dooks #include <plat/regs-iic.h>
459498cb79SBen Dooks #include <plat/iic.h>
461da177e4SLinus Torvalds 
4727452498SKarol Lewandowski /* Treat S3C2410 as baseline hardware, anything else is supported via quirks */
4827452498SKarol Lewandowski #define QUIRK_S3C2440		(1 << 0)
49ec39ef83SKarol Lewandowski #define QUIRK_HDMIPHY		(1 << 1)
50ec39ef83SKarol Lewandowski #define QUIRK_NO_GPIO		(1 << 2)
511da177e4SLinus Torvalds 
5227452498SKarol Lewandowski /* i2c controller state */
531da177e4SLinus Torvalds enum s3c24xx_i2c_state {
541da177e4SLinus Torvalds 	STATE_IDLE,
551da177e4SLinus Torvalds 	STATE_START,
561da177e4SLinus Torvalds 	STATE_READ,
571da177e4SLinus Torvalds 	STATE_WRITE,
581da177e4SLinus Torvalds 	STATE_STOP
591da177e4SLinus Torvalds };
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds struct s3c24xx_i2c {
621da177e4SLinus Torvalds 	spinlock_t		lock;
631da177e4SLinus Torvalds 	wait_queue_head_t	wait;
6427452498SKarol Lewandowski 	unsigned int            quirks;
65be44f01eSBen Dooks 	unsigned int		suspended:1;
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	struct i2c_msg		*msg;
681da177e4SLinus Torvalds 	unsigned int		msg_num;
691da177e4SLinus Torvalds 	unsigned int		msg_idx;
701da177e4SLinus Torvalds 	unsigned int		msg_ptr;
711da177e4SLinus Torvalds 
72e00a8cdfSBen Dooks 	unsigned int		tx_setup;
73e0d1ec97SBen Dooks 	unsigned int		irq;
74e00a8cdfSBen Dooks 
751da177e4SLinus Torvalds 	enum s3c24xx_i2c_state	state;
7661c7cff8SBen Dooks 	unsigned long		clkrate;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	void __iomem		*regs;
791da177e4SLinus Torvalds 	struct clk		*clk;
801da177e4SLinus Torvalds 	struct device		*dev;
811da177e4SLinus Torvalds 	struct resource		*ioarea;
821da177e4SLinus Torvalds 	struct i2c_adapter	adap;
8361c7cff8SBen Dooks 
844fd81eb2SThomas Abraham 	struct s3c2410_platform_i2c	*pdata;
855a5f5080SThomas Abraham 	int			gpios[2];
8661c7cff8SBen Dooks #ifdef CONFIG_CPU_FREQ
8761c7cff8SBen Dooks 	struct notifier_block	freq_transition;
8861c7cff8SBen Dooks #endif
891da177e4SLinus Torvalds };
901da177e4SLinus Torvalds 
9127452498SKarol Lewandowski static struct platform_device_id s3c24xx_driver_ids[] = {
921da177e4SLinus Torvalds 	{
9327452498SKarol Lewandowski 		.name		= "s3c2410-i2c",
9427452498SKarol Lewandowski 		.driver_data	= 0,
9527452498SKarol Lewandowski 	}, {
9627452498SKarol Lewandowski 		.name		= "s3c2440-i2c",
9727452498SKarol Lewandowski 		.driver_data	= QUIRK_S3C2440,
98ec39ef83SKarol Lewandowski 	}, {
99ec39ef83SKarol Lewandowski 		.name		= "s3c2440-hdmiphy-i2c",
100ec39ef83SKarol Lewandowski 		.driver_data	= QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
10127452498SKarol Lewandowski 	}, { },
10227452498SKarol Lewandowski };
10327452498SKarol Lewandowski MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
1041da177e4SLinus Torvalds 
1055a5f5080SThomas Abraham #ifdef CONFIG_OF
10627452498SKarol Lewandowski static const struct of_device_id s3c24xx_i2c_match[] = {
10727452498SKarol Lewandowski 	{ .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
10827452498SKarol Lewandowski 	{ .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
109ec39ef83SKarol Lewandowski 	{ .compatible = "samsung,s3c2440-hdmiphy-i2c",
110ec39ef83SKarol Lewandowski 	  .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
11127452498SKarol Lewandowski 	{},
11227452498SKarol Lewandowski };
11327452498SKarol Lewandowski MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
1145a5f5080SThomas Abraham #endif
1155a5f5080SThomas Abraham 
11627452498SKarol Lewandowski /* s3c24xx_get_device_quirks
11727452498SKarol Lewandowski  *
11827452498SKarol Lewandowski  * Get controller type either from device tree or platform device variant.
11927452498SKarol Lewandowski */
12027452498SKarol Lewandowski 
12127452498SKarol Lewandowski static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev)
12227452498SKarol Lewandowski {
12327452498SKarol Lewandowski 	if (pdev->dev.of_node) {
12427452498SKarol Lewandowski 		const struct of_device_id *match;
125*b900ba4cSKarol Lewandowski 		match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node);
12627452498SKarol Lewandowski 		return (unsigned int)match->data;
12727452498SKarol Lewandowski 	}
12827452498SKarol Lewandowski 
12927452498SKarol Lewandowski 	return platform_get_device_id(pdev)->driver_data;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /* s3c24xx_i2c_master_complete
1331da177e4SLinus Torvalds  *
1341da177e4SLinus Torvalds  * complete the message and wake up the caller, using the given return code,
1351da177e4SLinus Torvalds  * or zero to mean ok.
1361da177e4SLinus Torvalds */
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
1391da177e4SLinus Torvalds {
1401da177e4SLinus Torvalds 	dev_dbg(i2c->dev, "master_complete %d\n", ret);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	i2c->msg_ptr = 0;
1431da177e4SLinus Torvalds 	i2c->msg = NULL;
1441da177e4SLinus Torvalds 	i2c->msg_idx++;
1451da177e4SLinus Torvalds 	i2c->msg_num = 0;
1461da177e4SLinus Torvalds 	if (ret)
1471da177e4SLinus Torvalds 		i2c->msg_idx = ret;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	wake_up(&i2c->wait);
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	unsigned long tmp;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	tmp = readl(i2c->regs + S3C2410_IICCON);
1571da177e4SLinus Torvalds 	writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	unsigned long tmp;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	tmp = readl(i2c->regs + S3C2410_IICCON);
1651da177e4SLinus Torvalds 	writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /* irq enable/disable functions */
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	unsigned long tmp;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	tmp = readl(i2c->regs + S3C2410_IICCON);
1751da177e4SLinus Torvalds 	writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	unsigned long tmp;
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	tmp = readl(i2c->regs + S3C2410_IICCON);
1831da177e4SLinus Torvalds 	writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /* s3c24xx_i2c_message_start
1881da177e4SLinus Torvalds  *
1891da177e4SLinus Torvalds  * put the start of a message onto the bus
1901da177e4SLinus Torvalds */
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
1931da177e4SLinus Torvalds 				      struct i2c_msg *msg)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	unsigned int addr = (msg->addr & 0x7f) << 1;
1961da177e4SLinus Torvalds 	unsigned long stat;
1971da177e4SLinus Torvalds 	unsigned long iiccon;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	stat = 0;
2001da177e4SLinus Torvalds 	stat |=  S3C2410_IICSTAT_TXRXEN;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (msg->flags & I2C_M_RD) {
2031da177e4SLinus Torvalds 		stat |= S3C2410_IICSTAT_MASTER_RX;
2041da177e4SLinus Torvalds 		addr |= 1;
2051da177e4SLinus Torvalds 	} else
2061da177e4SLinus Torvalds 		stat |= S3C2410_IICSTAT_MASTER_TX;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	if (msg->flags & I2C_M_REV_DIR_ADDR)
2091da177e4SLinus Torvalds 		addr ^= 1;
2101da177e4SLinus Torvalds 
2113d0911bfSBen Dooks 	/* todo - check for wether ack wanted or not */
2121da177e4SLinus Torvalds 	s3c24xx_i2c_enable_ack(i2c);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	iiccon = readl(i2c->regs + S3C2410_IICCON);
2151da177e4SLinus Torvalds 	writel(stat, i2c->regs + S3C2410_IICSTAT);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
2181da177e4SLinus Torvalds 	writeb(addr, i2c->regs + S3C2410_IICDS);
2191da177e4SLinus Torvalds 
220e00a8cdfSBen Dooks 	/* delay here to ensure the data byte has gotten onto the bus
221e00a8cdfSBen Dooks 	 * before the transaction is started */
222e00a8cdfSBen Dooks 
223e00a8cdfSBen Dooks 	ndelay(i2c->tx_setup);
224e00a8cdfSBen Dooks 
2251da177e4SLinus Torvalds 	dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
2261da177e4SLinus Torvalds 	writel(iiccon, i2c->regs + S3C2410_IICCON);
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	stat |= S3C2410_IICSTAT_START;
2291da177e4SLinus Torvalds 	writel(stat, i2c->regs + S3C2410_IICSTAT);
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	dev_dbg(i2c->dev, "STOP\n");
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	/* stop the transfer */
2391da177e4SLinus Torvalds 	iicstat &= ~S3C2410_IICSTAT_START;
2401da177e4SLinus Torvalds 	writel(iicstat, i2c->regs + S3C2410_IICSTAT);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	i2c->state = STATE_STOP;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	s3c24xx_i2c_master_complete(i2c, ret);
2451da177e4SLinus Torvalds 	s3c24xx_i2c_disable_irq(i2c);
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds /* helper functions to determine the current state in the set of
2491da177e4SLinus Torvalds  * messages we are sending */
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds /* is_lastmsg()
2521da177e4SLinus Torvalds  *
2531da177e4SLinus Torvalds  * returns TRUE if the current message is the last in the set
2541da177e4SLinus Torvalds */
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	return i2c->msg_idx >= (i2c->msg_num - 1);
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds /* is_msglast
2621da177e4SLinus Torvalds  *
2631da177e4SLinus Torvalds  * returns TRUE if we this is the last byte in the current message
2641da177e4SLinus Torvalds */
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds static inline int is_msglast(struct s3c24xx_i2c *i2c)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	return i2c->msg_ptr == i2c->msg->len-1;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /* is_msgend
2721da177e4SLinus Torvalds  *
2731da177e4SLinus Torvalds  * returns TRUE if we reached the end of the current message
2741da177e4SLinus Torvalds */
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds static inline int is_msgend(struct s3c24xx_i2c *i2c)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	return i2c->msg_ptr >= i2c->msg->len;
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
28119820510SHuisung Kang /* i2c_s3c_irq_nextbyte
2821da177e4SLinus Torvalds  *
2831da177e4SLinus Torvalds  * process an interrupt and work out what to do
2841da177e4SLinus Torvalds  */
2851da177e4SLinus Torvalds 
28619820510SHuisung Kang static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	unsigned long tmp;
2891da177e4SLinus Torvalds 	unsigned char byte;
2901da177e4SLinus Torvalds 	int ret = 0;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	switch (i2c->state) {
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	case STATE_IDLE:
29508882d20SHarvey Harrison 		dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
2961da177e4SLinus Torvalds 		goto out;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	case STATE_STOP:
29908882d20SHarvey Harrison 		dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
3001da177e4SLinus Torvalds 		s3c24xx_i2c_disable_irq(i2c);
3011da177e4SLinus Torvalds 		goto out_ack;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	case STATE_START:
3041da177e4SLinus Torvalds 		/* last thing we did was send a start condition on the
3051da177e4SLinus Torvalds 		 * bus, or started a new i2c message
3061da177e4SLinus Torvalds 		 */
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 		if (iicstat & S3C2410_IICSTAT_LASTBIT &&
3091da177e4SLinus Torvalds 		    !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
3101da177e4SLinus Torvalds 			/* ack was not received... */
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 			dev_dbg(i2c->dev, "ack was not received\n");
31363f5c289SBen Dooks 			s3c24xx_i2c_stop(i2c, -ENXIO);
3141da177e4SLinus Torvalds 			goto out_ack;
3151da177e4SLinus Torvalds 		}
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 		if (i2c->msg->flags & I2C_M_RD)
3181da177e4SLinus Torvalds 			i2c->state = STATE_READ;
3191da177e4SLinus Torvalds 		else
3201da177e4SLinus Torvalds 			i2c->state = STATE_WRITE;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 		/* terminate the transfer if there is nothing to do
32363f5c289SBen Dooks 		 * as this is used by the i2c probe to find devices. */
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 		if (is_lastmsg(i2c) && i2c->msg->len == 0) {
3261da177e4SLinus Torvalds 			s3c24xx_i2c_stop(i2c, 0);
3271da177e4SLinus Torvalds 			goto out_ack;
3281da177e4SLinus Torvalds 		}
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 		if (i2c->state == STATE_READ)
3311da177e4SLinus Torvalds 			goto prepare_read;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 		/* fall through to the write state, as we will need to
3341da177e4SLinus Torvalds 		 * send a byte as well */
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	case STATE_WRITE:
3371da177e4SLinus Torvalds 		/* we are writing data to the device... check for the
3381da177e4SLinus Torvalds 		 * end of the message, and if so, work out what to do
3391da177e4SLinus Torvalds 		 */
3401da177e4SLinus Torvalds 
3412709781bSBen Dooks 		if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
3422709781bSBen Dooks 			if (iicstat & S3C2410_IICSTAT_LASTBIT) {
3432709781bSBen Dooks 				dev_dbg(i2c->dev, "WRITE: No Ack\n");
3442709781bSBen Dooks 
3452709781bSBen Dooks 				s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
3462709781bSBen Dooks 				goto out_ack;
3472709781bSBen Dooks 			}
3482709781bSBen Dooks 		}
3492709781bSBen Dooks 
3501da177e4SLinus Torvalds  retry_write:
3512709781bSBen Dooks 
3521da177e4SLinus Torvalds 		if (!is_msgend(i2c)) {
3531da177e4SLinus Torvalds 			byte = i2c->msg->buf[i2c->msg_ptr++];
3541da177e4SLinus Torvalds 			writeb(byte, i2c->regs + S3C2410_IICDS);
3551da177e4SLinus Torvalds 
356e00a8cdfSBen Dooks 			/* delay after writing the byte to allow the
357e00a8cdfSBen Dooks 			 * data setup time on the bus, as writing the
358e00a8cdfSBen Dooks 			 * data to the register causes the first bit
359e00a8cdfSBen Dooks 			 * to appear on SDA, and SCL will change as
360e00a8cdfSBen Dooks 			 * soon as the interrupt is acknowledged */
361e00a8cdfSBen Dooks 
362e00a8cdfSBen Dooks 			ndelay(i2c->tx_setup);
363e00a8cdfSBen Dooks 
3641da177e4SLinus Torvalds 		} else if (!is_lastmsg(i2c)) {
3651da177e4SLinus Torvalds 			/* we need to go to the next i2c message */
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 			dev_dbg(i2c->dev, "WRITE: Next Message\n");
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 			i2c->msg_ptr = 0;
3701da177e4SLinus Torvalds 			i2c->msg_idx++;
3711da177e4SLinus Torvalds 			i2c->msg++;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 			/* check to see if we need to do another message */
3741da177e4SLinus Torvalds 			if (i2c->msg->flags & I2C_M_NOSTART) {
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 				if (i2c->msg->flags & I2C_M_RD) {
3771da177e4SLinus Torvalds 					/* cannot do this, the controller
3781da177e4SLinus Torvalds 					 * forces us to send a new START
3791da177e4SLinus Torvalds 					 * when we change direction */
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 					s3c24xx_i2c_stop(i2c, -EINVAL);
3821da177e4SLinus Torvalds 				}
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 				goto retry_write;
3851da177e4SLinus Torvalds 			} else {
3861da177e4SLinus Torvalds 				/* send the new start */
3871da177e4SLinus Torvalds 				s3c24xx_i2c_message_start(i2c, i2c->msg);
3881da177e4SLinus Torvalds 				i2c->state = STATE_START;
3891da177e4SLinus Torvalds 			}
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 		} else {
3921da177e4SLinus Torvalds 			/* send stop */
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 			s3c24xx_i2c_stop(i2c, 0);
3951da177e4SLinus Torvalds 		}
3961da177e4SLinus Torvalds 		break;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	case STATE_READ:
3991da177e4SLinus Torvalds 		/* we have a byte of data in the data register, do
4001da177e4SLinus Torvalds 		 * something with it, and then work out wether we are
4011da177e4SLinus Torvalds 		 * going to do any more read/write
4021da177e4SLinus Torvalds 		 */
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 		byte = readb(i2c->regs + S3C2410_IICDS);
4051da177e4SLinus Torvalds 		i2c->msg->buf[i2c->msg_ptr++] = byte;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds  prepare_read:
4081da177e4SLinus Torvalds 		if (is_msglast(i2c)) {
4091da177e4SLinus Torvalds 			/* last byte of buffer */
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 			if (is_lastmsg(i2c))
4121da177e4SLinus Torvalds 				s3c24xx_i2c_disable_ack(i2c);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 		} else if (is_msgend(i2c)) {
4151da177e4SLinus Torvalds 			/* ok, we've read the entire buffer, see if there
4161da177e4SLinus Torvalds 			 * is anything else we need to do */
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 			if (is_lastmsg(i2c)) {
4191da177e4SLinus Torvalds 				/* last message, send stop and complete */
4201da177e4SLinus Torvalds 				dev_dbg(i2c->dev, "READ: Send Stop\n");
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 				s3c24xx_i2c_stop(i2c, 0);
4231da177e4SLinus Torvalds 			} else {
4241da177e4SLinus Torvalds 				/* go to the next transfer */
4251da177e4SLinus Torvalds 				dev_dbg(i2c->dev, "READ: Next Transfer\n");
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 				i2c->msg_ptr = 0;
4281da177e4SLinus Torvalds 				i2c->msg_idx++;
4291da177e4SLinus Torvalds 				i2c->msg++;
4301da177e4SLinus Torvalds 			}
4311da177e4SLinus Torvalds 		}
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 		break;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	/* acknowlegde the IRQ and get back on with the work */
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds  out_ack:
4391da177e4SLinus Torvalds 	tmp = readl(i2c->regs + S3C2410_IICCON);
4401da177e4SLinus Torvalds 	tmp &= ~S3C2410_IICCON_IRQPEND;
4411da177e4SLinus Torvalds 	writel(tmp, i2c->regs + S3C2410_IICCON);
4421da177e4SLinus Torvalds  out:
4431da177e4SLinus Torvalds 	return ret;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds /* s3c24xx_i2c_irq
4471da177e4SLinus Torvalds  *
4481da177e4SLinus Torvalds  * top level IRQ servicing routine
4491da177e4SLinus Torvalds */
4501da177e4SLinus Torvalds 
4517d12e780SDavid Howells static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds 	struct s3c24xx_i2c *i2c = dev_id;
4541da177e4SLinus Torvalds 	unsigned long status;
4551da177e4SLinus Torvalds 	unsigned long tmp;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	status = readl(i2c->regs + S3C2410_IICSTAT);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	if (status & S3C2410_IICSTAT_ARBITR) {
4603d0911bfSBen Dooks 		/* deal with arbitration loss */
4611da177e4SLinus Torvalds 		dev_err(i2c->dev, "deal with arbitration loss\n");
4621da177e4SLinus Torvalds 	}
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	if (i2c->state == STATE_IDLE) {
4651da177e4SLinus Torvalds 		dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 		tmp = readl(i2c->regs + S3C2410_IICCON);
4681da177e4SLinus Torvalds 		tmp &= ~S3C2410_IICCON_IRQPEND;
4691da177e4SLinus Torvalds 		writel(tmp, i2c->regs +  S3C2410_IICCON);
4701da177e4SLinus Torvalds 		goto out;
4711da177e4SLinus Torvalds 	}
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	/* pretty much this leaves us with the fact that we've
4741da177e4SLinus Torvalds 	 * transmitted or received whatever byte we last sent */
4751da177e4SLinus Torvalds 
47619820510SHuisung Kang 	i2c_s3c_irq_nextbyte(i2c, status);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds  out:
4791da177e4SLinus Torvalds 	return IRQ_HANDLED;
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds /* s3c24xx_i2c_set_master
4841da177e4SLinus Torvalds  *
4851da177e4SLinus Torvalds  * get the i2c bus for a master transaction
4861da177e4SLinus Torvalds */
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	unsigned long iicstat;
4911da177e4SLinus Torvalds 	int timeout = 400;
4921da177e4SLinus Torvalds 
493ec39ef83SKarol Lewandowski 	/* the timeout for HDMIPHY is reduced to 10 ms because
494ec39ef83SKarol Lewandowski 	 * the hangup is expected to happen, so waiting 400 ms
495ec39ef83SKarol Lewandowski 	 * causes only unnecessary system hangup
496ec39ef83SKarol Lewandowski 	 */
497ec39ef83SKarol Lewandowski 	if (i2c->quirks & QUIRK_HDMIPHY)
498ec39ef83SKarol Lewandowski 		timeout = 10;
499ec39ef83SKarol Lewandowski 
5001da177e4SLinus Torvalds 	while (timeout-- > 0) {
5011da177e4SLinus Torvalds 		iicstat = readl(i2c->regs + S3C2410_IICSTAT);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 		if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
5041da177e4SLinus Torvalds 			return 0;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 		msleep(1);
5071da177e4SLinus Torvalds 	}
5081da177e4SLinus Torvalds 
509ec39ef83SKarol Lewandowski 	/* hang-up of bus dedicated for HDMIPHY occurred, resetting */
510ec39ef83SKarol Lewandowski 	if (i2c->quirks & QUIRK_HDMIPHY) {
511ec39ef83SKarol Lewandowski 		writel(0, i2c->regs + S3C2410_IICCON);
512ec39ef83SKarol Lewandowski 		writel(0, i2c->regs + S3C2410_IICSTAT);
513ec39ef83SKarol Lewandowski 		writel(0, i2c->regs + S3C2410_IICDS);
514ec39ef83SKarol Lewandowski 
515ec39ef83SKarol Lewandowski 		return 0;
516ec39ef83SKarol Lewandowski 	}
517ec39ef83SKarol Lewandowski 
5181da177e4SLinus Torvalds 	return -ETIMEDOUT;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds /* s3c24xx_i2c_doxfer
5221da177e4SLinus Torvalds  *
5231da177e4SLinus Torvalds  * this starts an i2c transfer
5241da177e4SLinus Torvalds */
5251da177e4SLinus Torvalds 
5263d0911bfSBen Dooks static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
5273d0911bfSBen Dooks 			      struct i2c_msg *msgs, int num)
5281da177e4SLinus Torvalds {
5291bc2962eSMark Brown 	unsigned long iicstat, timeout;
5301bc2962eSMark Brown 	int spins = 20;
5311da177e4SLinus Torvalds 	int ret;
5321da177e4SLinus Torvalds 
533be44f01eSBen Dooks 	if (i2c->suspended)
53461c7cff8SBen Dooks 		return -EIO;
53561c7cff8SBen Dooks 
5361da177e4SLinus Torvalds 	ret = s3c24xx_i2c_set_master(i2c);
5371da177e4SLinus Torvalds 	if (ret != 0) {
5381da177e4SLinus Torvalds 		dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
5391da177e4SLinus Torvalds 		ret = -EAGAIN;
5401da177e4SLinus Torvalds 		goto out;
5411da177e4SLinus Torvalds 	}
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	spin_lock_irq(&i2c->lock);
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	i2c->msg     = msgs;
5461da177e4SLinus Torvalds 	i2c->msg_num = num;
5471da177e4SLinus Torvalds 	i2c->msg_ptr = 0;
5481da177e4SLinus Torvalds 	i2c->msg_idx = 0;
5491da177e4SLinus Torvalds 	i2c->state   = STATE_START;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	s3c24xx_i2c_enable_irq(i2c);
5521da177e4SLinus Torvalds 	s3c24xx_i2c_message_start(i2c, msgs);
5531da177e4SLinus Torvalds 	spin_unlock_irq(&i2c->lock);
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	ret = i2c->msg_idx;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	/* having these next two as dev_err() makes life very
5601da177e4SLinus Torvalds 	 * noisy when doing an i2cdetect */
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	if (timeout == 0)
5631da177e4SLinus Torvalds 		dev_dbg(i2c->dev, "timeout\n");
5641da177e4SLinus Torvalds 	else if (ret != num)
5651da177e4SLinus Torvalds 		dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	/* ensure the stop has been through the bus */
5681da177e4SLinus Torvalds 
5691bc2962eSMark Brown 	dev_dbg(i2c->dev, "waiting for bus idle\n");
5701bc2962eSMark Brown 
5711bc2962eSMark Brown 	/* first, try busy waiting briefly */
5721bc2962eSMark Brown 	do {
57337de03eaSMark Brown 		cpu_relax();
5741bc2962eSMark Brown 		iicstat = readl(i2c->regs + S3C2410_IICSTAT);
5751bc2962eSMark Brown 	} while ((iicstat & S3C2410_IICSTAT_START) && --spins);
5761bc2962eSMark Brown 
5771bc2962eSMark Brown 	/* if that timed out sleep */
5781bc2962eSMark Brown 	if (!spins) {
5791da177e4SLinus Torvalds 		msleep(1);
5801bc2962eSMark Brown 		iicstat = readl(i2c->regs + S3C2410_IICSTAT);
5811bc2962eSMark Brown 	}
5821bc2962eSMark Brown 
5831bc2962eSMark Brown 	if (iicstat & S3C2410_IICSTAT_START)
5841bc2962eSMark Brown 		dev_warn(i2c->dev, "timeout waiting for bus idle\n");
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds  out:
5871da177e4SLinus Torvalds 	return ret;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds /* s3c24xx_i2c_xfer
5911da177e4SLinus Torvalds  *
5921da177e4SLinus Torvalds  * first port of call from the i2c bus code when an message needs
59344bbe87eSSteven Cole  * transferring across the i2c bus.
5941da177e4SLinus Torvalds */
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
5971da177e4SLinus Torvalds 			struct i2c_msg *msgs, int num)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
6001da177e4SLinus Torvalds 	int retry;
6011da177e4SLinus Torvalds 	int ret;
6021da177e4SLinus Torvalds 
603c62c3ca5SMark Brown 	pm_runtime_get_sync(&adap->dev);
604d2360b8eSArnaud Patard (Rtp) 	clk_enable(i2c->clk);
605d2360b8eSArnaud Patard (Rtp) 
6061da177e4SLinus Torvalds 	for (retry = 0; retry < adap->retries; retry++) {
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 		ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
6091da177e4SLinus Torvalds 
610d2360b8eSArnaud Patard (Rtp) 		if (ret != -EAGAIN) {
611d2360b8eSArnaud Patard (Rtp) 			clk_disable(i2c->clk);
612c62c3ca5SMark Brown 			pm_runtime_put_sync(&adap->dev);
6131da177e4SLinus Torvalds 			return ret;
614d2360b8eSArnaud Patard (Rtp) 		}
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 		dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 		udelay(100);
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 
621d2360b8eSArnaud Patard (Rtp) 	clk_disable(i2c->clk);
622c62c3ca5SMark Brown 	pm_runtime_put_sync(&adap->dev);
6231da177e4SLinus Torvalds 	return -EREMOTEIO;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds /* declare our i2c functionality */
6271da177e4SLinus Torvalds static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
6281da177e4SLinus Torvalds {
62914674e70SMark Brown 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART |
63014674e70SMark Brown 		I2C_FUNC_PROTOCOL_MANGLING;
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds /* i2c bus registration info */
6341da177e4SLinus Torvalds 
6358f9082c5SJean Delvare static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
6361da177e4SLinus Torvalds 	.master_xfer		= s3c24xx_i2c_xfer,
6371da177e4SLinus Torvalds 	.functionality		= s3c24xx_i2c_func,
6381da177e4SLinus Torvalds };
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds /* s3c24xx_i2c_calcdivisor
6411da177e4SLinus Torvalds  *
6421da177e4SLinus Torvalds  * return the divisor settings for a given frequency
6431da177e4SLinus Torvalds */
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
6461da177e4SLinus Torvalds 				   unsigned int *div1, unsigned int *divs)
6471da177e4SLinus Torvalds {
6481da177e4SLinus Torvalds 	unsigned int calc_divs = clkin / wanted;
6491da177e4SLinus Torvalds 	unsigned int calc_div1;
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	if (calc_divs > (16*16))
6521da177e4SLinus Torvalds 		calc_div1 = 512;
6531da177e4SLinus Torvalds 	else
6541da177e4SLinus Torvalds 		calc_div1 = 16;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	calc_divs += calc_div1-1;
6571da177e4SLinus Torvalds 	calc_divs /= calc_div1;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	if (calc_divs == 0)
6601da177e4SLinus Torvalds 		calc_divs = 1;
6611da177e4SLinus Torvalds 	if (calc_divs > 17)
6621da177e4SLinus Torvalds 		calc_divs = 17;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	*divs = calc_divs;
6651da177e4SLinus Torvalds 	*div1 = calc_div1;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	return clkin / (calc_divs * calc_div1);
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
67061c7cff8SBen Dooks /* s3c24xx_i2c_clockrate
6711da177e4SLinus Torvalds  *
6721da177e4SLinus Torvalds  * work out a divisor for the user requested frequency setting,
6731da177e4SLinus Torvalds  * either by the requested frequency, or scanning the acceptable
6741da177e4SLinus Torvalds  * range of frequencies until something is found
6751da177e4SLinus Torvalds */
6761da177e4SLinus Torvalds 
67761c7cff8SBen Dooks static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
6781da177e4SLinus Torvalds {
6794fd81eb2SThomas Abraham 	struct s3c2410_platform_i2c *pdata = i2c->pdata;
6801da177e4SLinus Torvalds 	unsigned long clkin = clk_get_rate(i2c->clk);
6811da177e4SLinus Torvalds 	unsigned int divs, div1;
682c564e6aeSDaniel Silverstone 	unsigned long target_frequency;
68361c7cff8SBen Dooks 	u32 iiccon;
6841da177e4SLinus Torvalds 	int freq;
6851da177e4SLinus Torvalds 
68661c7cff8SBen Dooks 	i2c->clkrate = clkin;
6871da177e4SLinus Torvalds 	clkin /= 1000;		/* clkin now in KHz */
6881da177e4SLinus Torvalds 
689c564e6aeSDaniel Silverstone 	dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
6901da177e4SLinus Torvalds 
691c564e6aeSDaniel Silverstone 	target_frequency = pdata->frequency ? pdata->frequency : 100000;
6921da177e4SLinus Torvalds 
693c564e6aeSDaniel Silverstone 	target_frequency /= 1000; /* Target frequency now in KHz */
6941da177e4SLinus Torvalds 
695c564e6aeSDaniel Silverstone 	freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
6961da177e4SLinus Torvalds 
697c564e6aeSDaniel Silverstone 	if (freq > target_frequency) {
698c564e6aeSDaniel Silverstone 		dev_err(i2c->dev,
699c564e6aeSDaniel Silverstone 			"Unable to achieve desired frequency %luKHz."	\
700c564e6aeSDaniel Silverstone 			" Lowest achievable %dKHz\n", target_frequency, freq);
7011da177e4SLinus Torvalds 		return -EINVAL;
702c564e6aeSDaniel Silverstone 	}
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	*got = freq;
70561c7cff8SBen Dooks 
70661c7cff8SBen Dooks 	iiccon = readl(i2c->regs + S3C2410_IICCON);
70761c7cff8SBen Dooks 	iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
70861c7cff8SBen Dooks 	iiccon |= (divs-1);
70961c7cff8SBen Dooks 
71061c7cff8SBen Dooks 	if (div1 == 512)
71161c7cff8SBen Dooks 		iiccon |= S3C2410_IICCON_TXDIV_512;
71261c7cff8SBen Dooks 
71361c7cff8SBen Dooks 	writel(iiccon, i2c->regs + S3C2410_IICCON);
71461c7cff8SBen Dooks 
71527452498SKarol Lewandowski 	if (i2c->quirks & QUIRK_S3C2440) {
716a192f715SBen Dooks 		unsigned long sda_delay;
717a192f715SBen Dooks 
718a192f715SBen Dooks 		if (pdata->sda_delay) {
7197031307aSMyungJoo Ham 			sda_delay = clkin * pdata->sda_delay;
7207031307aSMyungJoo Ham 			sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
721a192f715SBen Dooks 			sda_delay = DIV_ROUND_UP(sda_delay, 5);
722a192f715SBen Dooks 			if (sda_delay > 3)
723a192f715SBen Dooks 				sda_delay = 3;
724a192f715SBen Dooks 			sda_delay |= S3C2410_IICLC_FILTER_ON;
725a192f715SBen Dooks 		} else
726a192f715SBen Dooks 			sda_delay = 0;
727a192f715SBen Dooks 
728a192f715SBen Dooks 		dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
729a192f715SBen Dooks 		writel(sda_delay, i2c->regs + S3C2440_IICLC);
730a192f715SBen Dooks 	}
731a192f715SBen Dooks 
7321da177e4SLinus Torvalds 	return 0;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
73561c7cff8SBen Dooks #ifdef CONFIG_CPU_FREQ
73661c7cff8SBen Dooks 
73761c7cff8SBen Dooks #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition)
73861c7cff8SBen Dooks 
73961c7cff8SBen Dooks static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb,
74061c7cff8SBen Dooks 					  unsigned long val, void *data)
74161c7cff8SBen Dooks {
74261c7cff8SBen Dooks 	struct s3c24xx_i2c *i2c = freq_to_i2c(nb);
74361c7cff8SBen Dooks 	unsigned long flags;
74461c7cff8SBen Dooks 	unsigned int got;
74561c7cff8SBen Dooks 	int delta_f;
74661c7cff8SBen Dooks 	int ret;
74761c7cff8SBen Dooks 
74861c7cff8SBen Dooks 	delta_f = clk_get_rate(i2c->clk) - i2c->clkrate;
74961c7cff8SBen Dooks 
75061c7cff8SBen Dooks 	/* if we're post-change and the input clock has slowed down
75161c7cff8SBen Dooks 	 * or at pre-change and the clock is about to speed up, then
75261c7cff8SBen Dooks 	 * adjust our clock rate. <0 is slow, >0 speedup.
75361c7cff8SBen Dooks 	 */
75461c7cff8SBen Dooks 
75561c7cff8SBen Dooks 	if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) ||
75661c7cff8SBen Dooks 	    (val == CPUFREQ_PRECHANGE && delta_f > 0)) {
75761c7cff8SBen Dooks 		spin_lock_irqsave(&i2c->lock, flags);
75861c7cff8SBen Dooks 		ret = s3c24xx_i2c_clockrate(i2c, &got);
75961c7cff8SBen Dooks 		spin_unlock_irqrestore(&i2c->lock, flags);
76061c7cff8SBen Dooks 
76161c7cff8SBen Dooks 		if (ret < 0)
76261c7cff8SBen Dooks 			dev_err(i2c->dev, "cannot find frequency\n");
76361c7cff8SBen Dooks 		else
76461c7cff8SBen Dooks 			dev_info(i2c->dev, "setting freq %d\n", got);
76561c7cff8SBen Dooks 	}
76661c7cff8SBen Dooks 
76761c7cff8SBen Dooks 	return 0;
76861c7cff8SBen Dooks }
76961c7cff8SBen Dooks 
77061c7cff8SBen Dooks static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
77161c7cff8SBen Dooks {
77261c7cff8SBen Dooks 	i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition;
77361c7cff8SBen Dooks 
77461c7cff8SBen Dooks 	return cpufreq_register_notifier(&i2c->freq_transition,
77561c7cff8SBen Dooks 					 CPUFREQ_TRANSITION_NOTIFIER);
77661c7cff8SBen Dooks }
77761c7cff8SBen Dooks 
77861c7cff8SBen Dooks static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
77961c7cff8SBen Dooks {
78061c7cff8SBen Dooks 	cpufreq_unregister_notifier(&i2c->freq_transition,
78161c7cff8SBen Dooks 				    CPUFREQ_TRANSITION_NOTIFIER);
78261c7cff8SBen Dooks }
78361c7cff8SBen Dooks 
78461c7cff8SBen Dooks #else
78561c7cff8SBen Dooks static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c)
78661c7cff8SBen Dooks {
78761c7cff8SBen Dooks 	return 0;
78861c7cff8SBen Dooks }
78961c7cff8SBen Dooks 
79061c7cff8SBen Dooks static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c)
79161c7cff8SBen Dooks {
79261c7cff8SBen Dooks }
79361c7cff8SBen Dooks #endif
79461c7cff8SBen Dooks 
7955a5f5080SThomas Abraham #ifdef CONFIG_OF
7965a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
7975a5f5080SThomas Abraham {
7985a5f5080SThomas Abraham 	int idx, gpio, ret;
7995a5f5080SThomas Abraham 
800ec39ef83SKarol Lewandowski 	if (i2c->quirks & QUIRK_NO_GPIO)
801ec39ef83SKarol Lewandowski 		return 0;
802ec39ef83SKarol Lewandowski 
8035a5f5080SThomas Abraham 	for (idx = 0; idx < 2; idx++) {
8045a5f5080SThomas Abraham 		gpio = of_get_gpio(i2c->dev->of_node, idx);
8055a5f5080SThomas Abraham 		if (!gpio_is_valid(gpio)) {
8065a5f5080SThomas Abraham 			dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio);
8075a5f5080SThomas Abraham 			goto free_gpio;
8085a5f5080SThomas Abraham 		}
8095a5f5080SThomas Abraham 
8105a5f5080SThomas Abraham 		ret = gpio_request(gpio, "i2c-bus");
8115a5f5080SThomas Abraham 		if (ret) {
8125a5f5080SThomas Abraham 			dev_err(i2c->dev, "gpio [%d] request failed\n", gpio);
8135a5f5080SThomas Abraham 			goto free_gpio;
8145a5f5080SThomas Abraham 		}
8155a5f5080SThomas Abraham 	}
8165a5f5080SThomas Abraham 	return 0;
8175a5f5080SThomas Abraham 
8185a5f5080SThomas Abraham free_gpio:
8195a5f5080SThomas Abraham 	while (--idx >= 0)
8205a5f5080SThomas Abraham 		gpio_free(i2c->gpios[idx]);
8215a5f5080SThomas Abraham 	return -EINVAL;
8225a5f5080SThomas Abraham }
8235a5f5080SThomas Abraham 
8245a5f5080SThomas Abraham static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
8255a5f5080SThomas Abraham {
8265a5f5080SThomas Abraham 	unsigned int idx;
827ec39ef83SKarol Lewandowski 
828ec39ef83SKarol Lewandowski 	if (i2c->quirks & QUIRK_NO_GPIO)
829ec39ef83SKarol Lewandowski 		return;
830ec39ef83SKarol Lewandowski 
8315a5f5080SThomas Abraham 	for (idx = 0; idx < 2; idx++)
8325a5f5080SThomas Abraham 		gpio_free(i2c->gpios[idx]);
8335a5f5080SThomas Abraham }
8345a5f5080SThomas Abraham #else
8355a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
8365a5f5080SThomas Abraham {
8378ebe661dSTushar Behera 	return 0;
8385a5f5080SThomas Abraham }
8395a5f5080SThomas Abraham 
8405a5f5080SThomas Abraham static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c)
8415a5f5080SThomas Abraham {
8425a5f5080SThomas Abraham }
8435a5f5080SThomas Abraham #endif
8445a5f5080SThomas Abraham 
8451da177e4SLinus Torvalds /* s3c24xx_i2c_init
8461da177e4SLinus Torvalds  *
8471da177e4SLinus Torvalds  * initialise the controller, set the IO lines and frequency
8481da177e4SLinus Torvalds */
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
8511da177e4SLinus Torvalds {
8521da177e4SLinus Torvalds 	unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN;
8531da177e4SLinus Torvalds 	struct s3c2410_platform_i2c *pdata;
8541da177e4SLinus Torvalds 	unsigned int freq;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 	/* get the plafrom data */
8571da177e4SLinus Torvalds 
8584fd81eb2SThomas Abraham 	pdata = i2c->pdata;
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	/* inititalise the gpio */
8611da177e4SLinus Torvalds 
8628be310a6SBen Dooks 	if (pdata->cfg_gpio)
8638be310a6SBen Dooks 		pdata->cfg_gpio(to_platform_device(i2c->dev));
8645a5f5080SThomas Abraham 	else
8655a5f5080SThomas Abraham 		if (s3c24xx_i2c_parse_dt_gpio(i2c))
8665a5f5080SThomas Abraham 			return -EINVAL;
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	/* write slave address */
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
8731da177e4SLinus Torvalds 
87461c7cff8SBen Dooks 	writel(iicon, i2c->regs + S3C2410_IICCON);
87561c7cff8SBen Dooks 
8761da177e4SLinus Torvalds 	/* we need to work out the divisors for the clock... */
8771da177e4SLinus Torvalds 
87861c7cff8SBen Dooks 	if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
87961c7cff8SBen Dooks 		writel(0, i2c->regs + S3C2410_IICCON);
8801da177e4SLinus Torvalds 		dev_err(i2c->dev, "cannot meet bus frequency required\n");
8811da177e4SLinus Torvalds 		return -EINVAL;
8821da177e4SLinus Torvalds 	}
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	/* todo - check that the i2c lines aren't being dragged anywhere */
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
8871da177e4SLinus Torvalds 	dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon);
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	return 0;
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8925a5f5080SThomas Abraham #ifdef CONFIG_OF
8935a5f5080SThomas Abraham /* s3c24xx_i2c_parse_dt
8945a5f5080SThomas Abraham  *
8955a5f5080SThomas Abraham  * Parse the device tree node and retreive the platform data.
8965a5f5080SThomas Abraham */
8975a5f5080SThomas Abraham 
8985a5f5080SThomas Abraham static void
8995a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
9005a5f5080SThomas Abraham {
9015a5f5080SThomas Abraham 	struct s3c2410_platform_i2c *pdata = i2c->pdata;
9025a5f5080SThomas Abraham 
9035a5f5080SThomas Abraham 	if (!np)
9045a5f5080SThomas Abraham 		return;
9055a5f5080SThomas Abraham 
9065a5f5080SThomas Abraham 	pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
9075a5f5080SThomas Abraham 	of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
9085a5f5080SThomas Abraham 	of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
9095a5f5080SThomas Abraham 	of_property_read_u32(np, "samsung,i2c-max-bus-freq",
9105a5f5080SThomas Abraham 				(u32 *)&pdata->frequency);
9115a5f5080SThomas Abraham }
9125a5f5080SThomas Abraham #else
9135a5f5080SThomas Abraham static void
9145a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
9155a5f5080SThomas Abraham {
9165a5f5080SThomas Abraham 	return;
9175a5f5080SThomas Abraham }
9185a5f5080SThomas Abraham #endif
9195a5f5080SThomas Abraham 
9201da177e4SLinus Torvalds /* s3c24xx_i2c_probe
9211da177e4SLinus Torvalds  *
9221da177e4SLinus Torvalds  * called by the bus driver when a suitable device is found
9231da177e4SLinus Torvalds */
9241da177e4SLinus Torvalds 
9253ae5eaecSRussell King static int s3c24xx_i2c_probe(struct platform_device *pdev)
9261da177e4SLinus Torvalds {
927692acbd3SBen Dooks 	struct s3c24xx_i2c *i2c;
9284fd81eb2SThomas Abraham 	struct s3c2410_platform_i2c *pdata = NULL;
9291da177e4SLinus Torvalds 	struct resource *res;
9301da177e4SLinus Torvalds 	int ret;
9311da177e4SLinus Torvalds 
9325a5f5080SThomas Abraham 	if (!pdev->dev.of_node) {
9336a039cabSBen Dooks 		pdata = pdev->dev.platform_data;
9346a039cabSBen Dooks 		if (!pdata) {
9356a039cabSBen Dooks 			dev_err(&pdev->dev, "no platform data\n");
9366a039cabSBen Dooks 			return -EINVAL;
9376a039cabSBen Dooks 		}
9385a5f5080SThomas Abraham 	}
939399dee23SBen Dooks 
9404ea1557fSMark Brown 	i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
941692acbd3SBen Dooks 	if (!i2c) {
942692acbd3SBen Dooks 		dev_err(&pdev->dev, "no memory for state\n");
943692acbd3SBen Dooks 		return -ENOMEM;
944692acbd3SBen Dooks 	}
945692acbd3SBen Dooks 
9464fd81eb2SThomas Abraham 	i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
9474fd81eb2SThomas Abraham 	if (!i2c->pdata) {
9484fd81eb2SThomas Abraham 		ret = -ENOMEM;
9494fd81eb2SThomas Abraham 		goto err_noclk;
9504fd81eb2SThomas Abraham 	}
9514fd81eb2SThomas Abraham 
95227452498SKarol Lewandowski 	i2c->quirks = s3c24xx_get_device_quirks(pdev);
9534fd81eb2SThomas Abraham 	if (pdata)
9544fd81eb2SThomas Abraham 		memcpy(i2c->pdata, pdata, sizeof(*pdata));
9555a5f5080SThomas Abraham 	else
9565a5f5080SThomas Abraham 		s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
9574fd81eb2SThomas Abraham 
958692acbd3SBen Dooks 	strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
959692acbd3SBen Dooks 	i2c->adap.owner   = THIS_MODULE;
960692acbd3SBen Dooks 	i2c->adap.algo    = &s3c24xx_i2c_algorithm;
961692acbd3SBen Dooks 	i2c->adap.retries = 2;
962692acbd3SBen Dooks 	i2c->adap.class   = I2C_CLASS_HWMON | I2C_CLASS_SPD;
963692acbd3SBen Dooks 	i2c->tx_setup     = 50;
964692acbd3SBen Dooks 
965692acbd3SBen Dooks 	spin_lock_init(&i2c->lock);
966692acbd3SBen Dooks 	init_waitqueue_head(&i2c->wait);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	/* find the clock and enable it */
9691da177e4SLinus Torvalds 
9703ae5eaecSRussell King 	i2c->dev = &pdev->dev;
9713ae5eaecSRussell King 	i2c->clk = clk_get(&pdev->dev, "i2c");
9721da177e4SLinus Torvalds 	if (IS_ERR(i2c->clk)) {
9733ae5eaecSRussell King 		dev_err(&pdev->dev, "cannot get clock\n");
9741da177e4SLinus Torvalds 		ret = -ENOENT;
9755b68790cSBen Dooks 		goto err_noclk;
9761da177e4SLinus Torvalds 	}
9771da177e4SLinus Torvalds 
9783ae5eaecSRussell King 	dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	clk_enable(i2c->clk);
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	/* map the registers */
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9851da177e4SLinus Torvalds 	if (res == NULL) {
9863ae5eaecSRussell King 		dev_err(&pdev->dev, "cannot find IO resource\n");
9871da177e4SLinus Torvalds 		ret = -ENOENT;
9885b68790cSBen Dooks 		goto err_clk;
9891da177e4SLinus Torvalds 	}
9901da177e4SLinus Torvalds 
991933a2aecSBen Dooks 	i2c->ioarea = request_mem_region(res->start, resource_size(res),
9921da177e4SLinus Torvalds 					 pdev->name);
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	if (i2c->ioarea == NULL) {
9953ae5eaecSRussell King 		dev_err(&pdev->dev, "cannot request IO\n");
9961da177e4SLinus Torvalds 		ret = -ENXIO;
9975b68790cSBen Dooks 		goto err_clk;
9981da177e4SLinus Torvalds 	}
9991da177e4SLinus Torvalds 
1000933a2aecSBen Dooks 	i2c->regs = ioremap(res->start, resource_size(res));
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	if (i2c->regs == NULL) {
10033ae5eaecSRussell King 		dev_err(&pdev->dev, "cannot map IO\n");
10041da177e4SLinus Torvalds 		ret = -ENXIO;
10055b68790cSBen Dooks 		goto err_ioarea;
10061da177e4SLinus Torvalds 	}
10071da177e4SLinus Torvalds 
10083d0911bfSBen Dooks 	dev_dbg(&pdev->dev, "registers %p (%p, %p)\n",
10093d0911bfSBen Dooks 		i2c->regs, i2c->ioarea, res);
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	/* setup info block for the i2c core */
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	i2c->adap.algo_data = i2c;
10143ae5eaecSRussell King 	i2c->adap.dev.parent = &pdev->dev;
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	/* initialise the i2c controller */
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	ret = s3c24xx_i2c_init(i2c);
10191da177e4SLinus Torvalds 	if (ret != 0)
10205b68790cSBen Dooks 		goto err_iomap;
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	/* find the IRQ for this unit (note, this relies on the init call to
10231da177e4SLinus Torvalds 	 * ensure no current IRQs pending
10241da177e4SLinus Torvalds 	 */
10251da177e4SLinus Torvalds 
1026e0d1ec97SBen Dooks 	i2c->irq = ret = platform_get_irq(pdev, 0);
1027e0d1ec97SBen Dooks 	if (ret <= 0) {
10283ae5eaecSRussell King 		dev_err(&pdev->dev, "cannot find IRQ\n");
10295b68790cSBen Dooks 		goto err_iomap;
10301da177e4SLinus Torvalds 	}
10311da177e4SLinus Torvalds 
10324311051cSYong Zhang 	ret = request_irq(i2c->irq, s3c24xx_i2c_irq, 0,
1033e0d1ec97SBen Dooks 			  dev_name(&pdev->dev), i2c);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	if (ret != 0) {
1036e0d1ec97SBen Dooks 		dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
10375b68790cSBen Dooks 		goto err_iomap;
10381da177e4SLinus Torvalds 	}
10391da177e4SLinus Torvalds 
104061c7cff8SBen Dooks 	ret = s3c24xx_i2c_register_cpufreq(i2c);
104161c7cff8SBen Dooks 	if (ret < 0) {
104261c7cff8SBen Dooks 		dev_err(&pdev->dev, "failed to register cpufreq notifier\n");
104361c7cff8SBen Dooks 		goto err_irq;
104461c7cff8SBen Dooks 	}
104561c7cff8SBen Dooks 
1046399dee23SBen Dooks 	/* Note, previous versions of the driver used i2c_add_adapter()
1047399dee23SBen Dooks 	 * to add the bus at any number. We now pass the bus number via
1048399dee23SBen Dooks 	 * the platform data, so if unset it will now default to always
1049399dee23SBen Dooks 	 * being bus 0.
1050399dee23SBen Dooks 	 */
1051399dee23SBen Dooks 
10524fd81eb2SThomas Abraham 	i2c->adap.nr = i2c->pdata->bus_num;
10535a5f5080SThomas Abraham 	i2c->adap.dev.of_node = pdev->dev.of_node;
1054399dee23SBen Dooks 
1055399dee23SBen Dooks 	ret = i2c_add_numbered_adapter(&i2c->adap);
10561da177e4SLinus Torvalds 	if (ret < 0) {
10573ae5eaecSRussell King 		dev_err(&pdev->dev, "failed to add bus to i2c core\n");
105861c7cff8SBen Dooks 		goto err_cpufreq;
10591da177e4SLinus Torvalds 	}
10601da177e4SLinus Torvalds 
10615a5f5080SThomas Abraham 	of_i2c_register_devices(&i2c->adap);
10623ae5eaecSRussell King 	platform_set_drvdata(pdev, i2c);
10631da177e4SLinus Torvalds 
1064c62c3ca5SMark Brown 	pm_runtime_enable(&pdev->dev);
1065c62c3ca5SMark Brown 	pm_runtime_enable(&i2c->adap.dev);
1066c62c3ca5SMark Brown 
106722e965c2SJean Delvare 	dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
1068d2360b8eSArnaud Patard (Rtp) 	clk_disable(i2c->clk);
10695b68790cSBen Dooks 	return 0;
10701da177e4SLinus Torvalds 
107161c7cff8SBen Dooks  err_cpufreq:
107261c7cff8SBen Dooks 	s3c24xx_i2c_deregister_cpufreq(i2c);
107361c7cff8SBen Dooks 
10745b68790cSBen Dooks  err_irq:
1075e0d1ec97SBen Dooks 	free_irq(i2c->irq, i2c);
10761da177e4SLinus Torvalds 
10775b68790cSBen Dooks  err_iomap:
10785b68790cSBen Dooks 	iounmap(i2c->regs);
10795b68790cSBen Dooks 
10805b68790cSBen Dooks  err_ioarea:
10815b68790cSBen Dooks 	release_resource(i2c->ioarea);
10825b68790cSBen Dooks 	kfree(i2c->ioarea);
10835b68790cSBen Dooks 
10845b68790cSBen Dooks  err_clk:
10855b68790cSBen Dooks 	clk_disable(i2c->clk);
10865b68790cSBen Dooks 	clk_put(i2c->clk);
10875b68790cSBen Dooks 
10885b68790cSBen Dooks  err_noclk:
10891da177e4SLinus Torvalds 	return ret;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds /* s3c24xx_i2c_remove
10931da177e4SLinus Torvalds  *
10941da177e4SLinus Torvalds  * called when device is removed from the bus
10951da177e4SLinus Torvalds */
10961da177e4SLinus Torvalds 
10973ae5eaecSRussell King static int s3c24xx_i2c_remove(struct platform_device *pdev)
10981da177e4SLinus Torvalds {
10993ae5eaecSRussell King 	struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
11001da177e4SLinus Torvalds 
1101c62c3ca5SMark Brown 	pm_runtime_disable(&i2c->adap.dev);
1102c62c3ca5SMark Brown 	pm_runtime_disable(&pdev->dev);
1103c62c3ca5SMark Brown 
110461c7cff8SBen Dooks 	s3c24xx_i2c_deregister_cpufreq(i2c);
110561c7cff8SBen Dooks 
11065b68790cSBen Dooks 	i2c_del_adapter(&i2c->adap);
1107e0d1ec97SBen Dooks 	free_irq(i2c->irq, i2c);
11085b68790cSBen Dooks 
11095b68790cSBen Dooks 	clk_disable(i2c->clk);
11105b68790cSBen Dooks 	clk_put(i2c->clk);
11115b68790cSBen Dooks 
11125b68790cSBen Dooks 	iounmap(i2c->regs);
11135b68790cSBen Dooks 
11145b68790cSBen Dooks 	release_resource(i2c->ioarea);
11155a5f5080SThomas Abraham 	s3c24xx_i2c_dt_gpio_free(i2c);
11165b68790cSBen Dooks 	kfree(i2c->ioarea);
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	return 0;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds #ifdef CONFIG_PM
11226a6c6189SMagnus Damm static int s3c24xx_i2c_suspend_noirq(struct device *dev)
1123be44f01eSBen Dooks {
11246a6c6189SMagnus Damm 	struct platform_device *pdev = to_platform_device(dev);
11256a6c6189SMagnus Damm 	struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
11266a6c6189SMagnus Damm 
1127be44f01eSBen Dooks 	i2c->suspended = 1;
11286a6c6189SMagnus Damm 
1129be44f01eSBen Dooks 	return 0;
1130be44f01eSBen Dooks }
1131be44f01eSBen Dooks 
11326a6c6189SMagnus Damm static int s3c24xx_i2c_resume(struct device *dev)
11331da177e4SLinus Torvalds {
11346a6c6189SMagnus Damm 	struct platform_device *pdev = to_platform_device(dev);
11356a6c6189SMagnus Damm 	struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
11361da177e4SLinus Torvalds 
1137be44f01eSBen Dooks 	i2c->suspended = 0;
1138d2360b8eSArnaud Patard (Rtp) 	clk_enable(i2c->clk);
11391da177e4SLinus Torvalds 	s3c24xx_i2c_init(i2c);
1140d2360b8eSArnaud Patard (Rtp) 	clk_disable(i2c->clk);
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	return 0;
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
114547145210SAlexey Dobriyan static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
11466a6c6189SMagnus Damm 	.suspend_noirq = s3c24xx_i2c_suspend_noirq,
11476a6c6189SMagnus Damm 	.resume = s3c24xx_i2c_resume,
11486a6c6189SMagnus Damm };
11496a6c6189SMagnus Damm 
11506a6c6189SMagnus Damm #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops)
11511da177e4SLinus Torvalds #else
11526a6c6189SMagnus Damm #define S3C24XX_DEV_PM_OPS NULL
11531da177e4SLinus Torvalds #endif
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds /* device driver for platform bus bits */
11561da177e4SLinus Torvalds 
11577d85ccd8SBen Dooks static struct platform_driver s3c24xx_i2c_driver = {
11581da177e4SLinus Torvalds 	.probe		= s3c24xx_i2c_probe,
11591da177e4SLinus Torvalds 	.remove		= s3c24xx_i2c_remove,
11607d85ccd8SBen Dooks 	.id_table	= s3c24xx_driver_ids,
11613ae5eaecSRussell King 	.driver		= {
11623ae5eaecSRussell King 		.owner	= THIS_MODULE,
11637d85ccd8SBen Dooks 		.name	= "s3c-i2c",
11646a6c6189SMagnus Damm 		.pm	= S3C24XX_DEV_PM_OPS,
11659df7eadfSKarol Lewandowski 		.of_match_table = of_match_ptr(s3c24xx_i2c_match),
11663ae5eaecSRussell King 	},
11671da177e4SLinus Torvalds };
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds static int __init i2c_adap_s3c_init(void)
11701da177e4SLinus Torvalds {
11717d85ccd8SBen Dooks 	return platform_driver_register(&s3c24xx_i2c_driver);
11721da177e4SLinus Torvalds }
117318dc83a6SMark Brown subsys_initcall(i2c_adap_s3c_init);
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds static void __exit i2c_adap_s3c_exit(void)
11761da177e4SLinus Torvalds {
11777d85ccd8SBen Dooks 	platform_driver_unregister(&s3c24xx_i2c_driver);
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds module_exit(i2c_adap_s3c_exit);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
11821da177e4SLinus Torvalds MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
11831da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1184