1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /* linux/drivers/i2c/busses/i2c-s3c2410.c
31da177e4SLinus Torvalds *
4c564e6aeSDaniel Silverstone * Copyright (C) 2004,2005,2009 Simtec Electronics
51da177e4SLinus Torvalds * Ben Dooks <ben@simtec.co.uk>
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * S3C2410 I2C Controller
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds
101da177e4SLinus Torvalds #include <linux/kernel.h>
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds
131da177e4SLinus Torvalds #include <linux/i2c.h>
141da177e4SLinus Torvalds #include <linux/init.h>
151da177e4SLinus Torvalds #include <linux/time.h>
161da177e4SLinus Torvalds #include <linux/interrupt.h>
171da177e4SLinus Torvalds #include <linux/delay.h>
181da177e4SLinus Torvalds #include <linux/errno.h>
191da177e4SLinus Torvalds #include <linux/err.h>
20d052d1beSRussell King #include <linux/platform_device.h>
21c62c3ca5SMark Brown #include <linux/pm_runtime.h>
22f8ce2547SRussell King #include <linux/clk.h>
2361c7cff8SBen Dooks #include <linux/cpufreq.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
2521782180SH Hartley Sweeten #include <linux/io.h>
264edd65e6SSachin Kamat #include <linux/of.h>
27ed7357c9SLinus Walleij #include <linux/gpio/consumer.h>
282693ac69STomasz Figa #include <linux/pinctrl/consumer.h>
29a7750c3eSPankaj Dubey #include <linux/mfd/syscon.h>
30a7750c3eSPankaj Dubey #include <linux/regmap.h>
311da177e4SLinus Torvalds
321da177e4SLinus Torvalds #include <asm/irq.h>
331da177e4SLinus Torvalds
34436d42c6SArnd Bergmann #include <linux/platform_data/i2c-s3c2410.h>
351da177e4SLinus Torvalds
36e636602aSHeiko Stübner /* see s3c2410x user guide, v1.1, section 9 (p447) for more info */
37e636602aSHeiko Stübner
387a6674daSHeiko Stübner #define S3C2410_IICCON 0x00
397a6674daSHeiko Stübner #define S3C2410_IICSTAT 0x04
407a6674daSHeiko Stübner #define S3C2410_IICADD 0x08
417a6674daSHeiko Stübner #define S3C2410_IICDS 0x0C
427a6674daSHeiko Stübner #define S3C2440_IICLC 0x10
43e636602aSHeiko Stübner
44e636602aSHeiko Stübner #define S3C2410_IICCON_ACKEN (1 << 7)
45e636602aSHeiko Stübner #define S3C2410_IICCON_TXDIV_16 (0 << 6)
46e636602aSHeiko Stübner #define S3C2410_IICCON_TXDIV_512 (1 << 6)
47e636602aSHeiko Stübner #define S3C2410_IICCON_IRQEN (1 << 5)
48e636602aSHeiko Stübner #define S3C2410_IICCON_IRQPEND (1 << 4)
497a6674daSHeiko Stübner #define S3C2410_IICCON_SCALE(x) ((x) & 0xf)
50e636602aSHeiko Stübner #define S3C2410_IICCON_SCALEMASK (0xf)
51e636602aSHeiko Stübner
52e636602aSHeiko Stübner #define S3C2410_IICSTAT_MASTER_RX (2 << 6)
53e636602aSHeiko Stübner #define S3C2410_IICSTAT_MASTER_TX (3 << 6)
54e636602aSHeiko Stübner #define S3C2410_IICSTAT_SLAVE_RX (0 << 6)
55e636602aSHeiko Stübner #define S3C2410_IICSTAT_SLAVE_TX (1 << 6)
56e636602aSHeiko Stübner #define S3C2410_IICSTAT_MODEMASK (3 << 6)
57e636602aSHeiko Stübner
58e636602aSHeiko Stübner #define S3C2410_IICSTAT_START (1 << 5)
59e636602aSHeiko Stübner #define S3C2410_IICSTAT_BUSBUSY (1 << 5)
60e636602aSHeiko Stübner #define S3C2410_IICSTAT_TXRXEN (1 << 4)
61e636602aSHeiko Stübner #define S3C2410_IICSTAT_ARBITR (1 << 3)
62e636602aSHeiko Stübner #define S3C2410_IICSTAT_ASSLAVE (1 << 2)
63e636602aSHeiko Stübner #define S3C2410_IICSTAT_ADDR0 (1 << 1)
64e636602aSHeiko Stübner #define S3C2410_IICSTAT_LASTBIT (1 << 0)
65e636602aSHeiko Stübner
66e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY0 (0 << 0)
67e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY5 (1 << 0)
68e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY10 (2 << 0)
69e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY15 (3 << 0)
70e636602aSHeiko Stübner #define S3C2410_IICLC_SDA_DELAY_MASK (3 << 0)
71e636602aSHeiko Stübner
72e636602aSHeiko Stübner #define S3C2410_IICLC_FILTER_ON (1 << 2)
73e636602aSHeiko Stübner
7427452498SKarol Lewandowski /* Treat S3C2410 as baseline hardware, anything else is supported via quirks */
7527452498SKarol Lewandowski #define QUIRK_S3C2440 (1 << 0)
76ec39ef83SKarol Lewandowski #define QUIRK_HDMIPHY (1 << 1)
77ec39ef83SKarol Lewandowski #define QUIRK_NO_GPIO (1 << 2)
78117053f7SVasanth Ananthan #define QUIRK_POLL (1 << 3)
79187432b8SMarek Szyprowski #define QUIRK_ATOMIC (1 << 4)
801da177e4SLinus Torvalds
81fe724bf9SDaniel Kurtz /* Max time to wait for bus to become idle after a xfer (in us) */
82fe724bf9SDaniel Kurtz #define S3C2410_IDLE_TIMEOUT 5000
83fe724bf9SDaniel Kurtz
84a7750c3eSPankaj Dubey /* Exynos5 Sysreg offset */
85a7750c3eSPankaj Dubey #define EXYNOS5_SYS_I2C_CFG 0x0234
86a7750c3eSPankaj Dubey
8727452498SKarol Lewandowski /* i2c controller state */
881da177e4SLinus Torvalds enum s3c24xx_i2c_state {
891da177e4SLinus Torvalds STATE_IDLE,
901da177e4SLinus Torvalds STATE_START,
911da177e4SLinus Torvalds STATE_READ,
921da177e4SLinus Torvalds STATE_WRITE,
931da177e4SLinus Torvalds STATE_STOP
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds
961da177e4SLinus Torvalds struct s3c24xx_i2c {
971da177e4SLinus Torvalds wait_queue_head_t wait;
985f1b1155SPankaj Dubey kernel_ulong_t quirks;
991da177e4SLinus Torvalds
1001da177e4SLinus Torvalds struct i2c_msg *msg;
1011da177e4SLinus Torvalds unsigned int msg_num;
1021da177e4SLinus Torvalds unsigned int msg_idx;
1031da177e4SLinus Torvalds unsigned int msg_ptr;
1041da177e4SLinus Torvalds
105e00a8cdfSBen Dooks unsigned int tx_setup;
106e0d1ec97SBen Dooks unsigned int irq;
107e00a8cdfSBen Dooks
1081da177e4SLinus Torvalds enum s3c24xx_i2c_state state;
10961c7cff8SBen Dooks unsigned long clkrate;
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvalds void __iomem *regs;
1121da177e4SLinus Torvalds struct clk *clk;
1131da177e4SLinus Torvalds struct device *dev;
1141da177e4SLinus Torvalds struct i2c_adapter adap;
11561c7cff8SBen Dooks
1164fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata;
117ed7357c9SLinus Walleij struct gpio_desc *gpios[2];
1182693ac69STomasz Figa struct pinctrl *pctrl;
119a7750c3eSPankaj Dubey struct regmap *sysreg;
120a7750c3eSPankaj Dubey unsigned int sys_i2c_cfg;
1211da177e4SLinus Torvalds };
1221da177e4SLinus Torvalds
123e9a02a3dSKrzysztof Kozlowski static const struct platform_device_id s3c24xx_driver_ids[] = {
1241da177e4SLinus Torvalds {
12527452498SKarol Lewandowski .name = "s3c2410-i2c",
12627452498SKarol Lewandowski .driver_data = 0,
12727452498SKarol Lewandowski }, {
12827452498SKarol Lewandowski .name = "s3c2440-i2c",
12927452498SKarol Lewandowski .driver_data = QUIRK_S3C2440,
130ec39ef83SKarol Lewandowski }, {
131ec39ef83SKarol Lewandowski .name = "s3c2440-hdmiphy-i2c",
132ec39ef83SKarol Lewandowski .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO,
133*1dc8baa4SWolfram Sang }, { }
13427452498SKarol Lewandowski };
13527452498SKarol Lewandowski MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids);
1361da177e4SLinus Torvalds
13710e806d3SJason Yan static void i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat);
138117053f7SVasanth Ananthan
1395a5f5080SThomas Abraham #ifdef CONFIG_OF
14027452498SKarol Lewandowski static const struct of_device_id s3c24xx_i2c_match[] = {
14127452498SKarol Lewandowski { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 },
14227452498SKarol Lewandowski { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 },
143ec39ef83SKarol Lewandowski { .compatible = "samsung,s3c2440-hdmiphy-i2c",
144ec39ef83SKarol Lewandowski .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) },
145117053f7SVasanth Ananthan { .compatible = "samsung,exynos5-sata-phy-i2c",
146117053f7SVasanth Ananthan .data = (void *)(QUIRK_S3C2440 | QUIRK_POLL | QUIRK_NO_GPIO) },
14727452498SKarol Lewandowski {},
14827452498SKarol Lewandowski };
14927452498SKarol Lewandowski MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match);
1505a5f5080SThomas Abraham #endif
1515a5f5080SThomas Abraham
152ec7c34a4SKrzysztof Kozlowski /*
15327452498SKarol Lewandowski * Get controller type either from device tree or platform device variant.
15427452498SKarol Lewandowski */
s3c24xx_get_device_quirks(struct platform_device * pdev)1555f1b1155SPankaj Dubey static inline kernel_ulong_t s3c24xx_get_device_quirks(struct platform_device *pdev)
15627452498SKarol Lewandowski {
157af92cca1SKrzysztof Kozlowski if (pdev->dev.of_node)
158af92cca1SKrzysztof Kozlowski return (kernel_ulong_t)of_device_get_match_data(&pdev->dev);
15927452498SKarol Lewandowski
16027452498SKarol Lewandowski return platform_get_device_id(pdev)->driver_data;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds
163ec7c34a4SKrzysztof Kozlowski /*
164ec7c34a4SKrzysztof Kozlowski * Complete the message and wake up the caller, using the given return code,
1651da177e4SLinus Torvalds * or zero to mean ok.
1661da177e4SLinus Torvalds */
s3c24xx_i2c_master_complete(struct s3c24xx_i2c * i2c,int ret)1671da177e4SLinus Torvalds static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds dev_dbg(i2c->dev, "master_complete %d\n", ret);
1701da177e4SLinus Torvalds
1711da177e4SLinus Torvalds i2c->msg_ptr = 0;
1721da177e4SLinus Torvalds i2c->msg = NULL;
1731da177e4SLinus Torvalds i2c->msg_idx++;
1741da177e4SLinus Torvalds i2c->msg_num = 0;
1751da177e4SLinus Torvalds if (ret)
1761da177e4SLinus Torvalds i2c->msg_idx = ret;
1771da177e4SLinus Torvalds
178187432b8SMarek Szyprowski if (!(i2c->quirks & (QUIRK_POLL | QUIRK_ATOMIC)))
1791da177e4SLinus Torvalds wake_up(&i2c->wait);
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds
s3c24xx_i2c_disable_ack(struct s3c24xx_i2c * i2c)1821da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds unsigned long tmp;
1851da177e4SLinus Torvalds
1861da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON);
1871da177e4SLinus Torvalds writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds
s3c24xx_i2c_enable_ack(struct s3c24xx_i2c * i2c)1901da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds unsigned long tmp;
1931da177e4SLinus Torvalds
1941da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON);
1951da177e4SLinus Torvalds writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds
1981da177e4SLinus Torvalds /* irq enable/disable functions */
s3c24xx_i2c_disable_irq(struct s3c24xx_i2c * i2c)1991da177e4SLinus Torvalds static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds unsigned long tmp;
2021da177e4SLinus Torvalds
2031da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON);
2041da177e4SLinus Torvalds writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds
s3c24xx_i2c_enable_irq(struct s3c24xx_i2c * i2c)2071da177e4SLinus Torvalds static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds unsigned long tmp;
2101da177e4SLinus Torvalds
2111da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON);
2121da177e4SLinus Torvalds writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON);
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds
is_ack(struct s3c24xx_i2c * i2c)215117053f7SVasanth Ananthan static bool is_ack(struct s3c24xx_i2c *i2c)
216117053f7SVasanth Ananthan {
217117053f7SVasanth Ananthan int tries;
218117053f7SVasanth Ananthan
219117053f7SVasanth Ananthan for (tries = 50; tries; --tries) {
2200d9cf23eSMarek Szyprowski unsigned long tmp = readl(i2c->regs + S3C2410_IICCON);
2210d9cf23eSMarek Szyprowski
2220d9cf23eSMarek Szyprowski if (!(tmp & S3C2410_IICCON_ACKEN)) {
2230d9cf23eSMarek Szyprowski /*
2240d9cf23eSMarek Szyprowski * Wait a bit for the bus to stabilize,
2250d9cf23eSMarek Szyprowski * delay estimated experimentally.
2260d9cf23eSMarek Szyprowski */
2270d9cf23eSMarek Szyprowski usleep_range(100, 200);
2280d9cf23eSMarek Szyprowski return true;
2290d9cf23eSMarek Szyprowski }
2300d9cf23eSMarek Szyprowski if (tmp & S3C2410_IICCON_IRQPEND) {
231117053f7SVasanth Ananthan if (!(readl(i2c->regs + S3C2410_IICSTAT)
232117053f7SVasanth Ananthan & S3C2410_IICSTAT_LASTBIT))
233117053f7SVasanth Ananthan return true;
234117053f7SVasanth Ananthan }
235117053f7SVasanth Ananthan usleep_range(1000, 2000);
236117053f7SVasanth Ananthan }
2379b13494cSMasanari Iida dev_err(i2c->dev, "ack was not received\n");
238117053f7SVasanth Ananthan return false;
239117053f7SVasanth Ananthan }
2401da177e4SLinus Torvalds
241ec7c34a4SKrzysztof Kozlowski /*
2421da177e4SLinus Torvalds * put the start of a message onto the bus
2431da177e4SLinus Torvalds */
s3c24xx_i2c_message_start(struct s3c24xx_i2c * i2c,struct i2c_msg * msg)2441da177e4SLinus Torvalds static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c,
2451da177e4SLinus Torvalds struct i2c_msg *msg)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds unsigned int addr = (msg->addr & 0x7f) << 1;
2481da177e4SLinus Torvalds unsigned long stat;
2491da177e4SLinus Torvalds unsigned long iiccon;
2501da177e4SLinus Torvalds
2511da177e4SLinus Torvalds stat = 0;
2521da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_TXRXEN;
2531da177e4SLinus Torvalds
2541da177e4SLinus Torvalds if (msg->flags & I2C_M_RD) {
2551da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_MASTER_RX;
2561da177e4SLinus Torvalds addr |= 1;
2571da177e4SLinus Torvalds } else
2581da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_MASTER_TX;
2591da177e4SLinus Torvalds
2601da177e4SLinus Torvalds if (msg->flags & I2C_M_REV_DIR_ADDR)
2611da177e4SLinus Torvalds addr ^= 1;
2621da177e4SLinus Torvalds
26348fc7f7eSAdam Buchbinder /* todo - check for whether ack wanted or not */
2641da177e4SLinus Torvalds s3c24xx_i2c_enable_ack(i2c);
2651da177e4SLinus Torvalds
2661da177e4SLinus Torvalds iiccon = readl(i2c->regs + S3C2410_IICCON);
2671da177e4SLinus Torvalds writel(stat, i2c->regs + S3C2410_IICSTAT);
2681da177e4SLinus Torvalds
2691da177e4SLinus Torvalds dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr);
2701da177e4SLinus Torvalds writeb(addr, i2c->regs + S3C2410_IICDS);
2711da177e4SLinus Torvalds
2720915833bSKrzysztof Kozlowski /*
2730915833bSKrzysztof Kozlowski * delay here to ensure the data byte has gotten onto the bus
2740915833bSKrzysztof Kozlowski * before the transaction is started
2750915833bSKrzysztof Kozlowski */
276e00a8cdfSBen Dooks ndelay(i2c->tx_setup);
277e00a8cdfSBen Dooks
2781da177e4SLinus Torvalds dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon);
2791da177e4SLinus Torvalds writel(iiccon, i2c->regs + S3C2410_IICCON);
2801da177e4SLinus Torvalds
2811da177e4SLinus Torvalds stat |= S3C2410_IICSTAT_START;
2821da177e4SLinus Torvalds writel(stat, i2c->regs + S3C2410_IICSTAT);
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds
s3c24xx_i2c_stop(struct s3c24xx_i2c * i2c,int ret)2851da177e4SLinus Torvalds static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret)
2861da177e4SLinus Torvalds {
2871da177e4SLinus Torvalds unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT);
2881da177e4SLinus Torvalds
2891da177e4SLinus Torvalds dev_dbg(i2c->dev, "STOP\n");
2901da177e4SLinus Torvalds
2910da2e776SDaniel Kurtz /*
2920da2e776SDaniel Kurtz * The datasheet says that the STOP sequence should be:
2930da2e776SDaniel Kurtz * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP')
2940da2e776SDaniel Kurtz * 2) I2CCON.4 = 0 - Clear IRQPEND
2950da2e776SDaniel Kurtz * 3) Wait until the stop condition takes effect.
2960da2e776SDaniel Kurtz * 4*) I2CSTAT.4 = 0 - Clear TXRXEN
2970da2e776SDaniel Kurtz *
2980da2e776SDaniel Kurtz * Where, step "4*" is only for buses with the "HDMIPHY" quirk.
2990da2e776SDaniel Kurtz *
3000da2e776SDaniel Kurtz * However, after much experimentation, it appears that:
3010da2e776SDaniel Kurtz * a) normal buses automatically clear BUSY and transition from
3020da2e776SDaniel Kurtz * Master->Slave when they complete generating a STOP condition.
3030da2e776SDaniel Kurtz * Therefore, step (3) can be done in doxfer() by polling I2CCON.4
3040da2e776SDaniel Kurtz * after starting the STOP generation here.
3050da2e776SDaniel Kurtz * b) HDMIPHY bus does neither, so there is no way to do step 3.
3060da2e776SDaniel Kurtz * There is no indication when this bus has finished generating
3070da2e776SDaniel Kurtz * STOP.
3080da2e776SDaniel Kurtz *
3090da2e776SDaniel Kurtz * In fact, we have found that as soon as the IRQPEND bit is cleared in
3100da2e776SDaniel Kurtz * step 2, the HDMIPHY bus generates the STOP condition, and then
3110da2e776SDaniel Kurtz * immediately starts transferring another data byte, even though the
3120da2e776SDaniel Kurtz * bus is supposedly stopped. This is presumably because the bus is
3130da2e776SDaniel Kurtz * still in "Master" mode, and its BUSY bit is still set.
3140da2e776SDaniel Kurtz *
3150da2e776SDaniel Kurtz * To avoid these extra post-STOP transactions on HDMI phy devices, we
3160da2e776SDaniel Kurtz * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly,
3170da2e776SDaniel Kurtz * instead of first generating a proper STOP condition. This should
3180da2e776SDaniel Kurtz * float SDA & SCK terminating the transfer. Subsequent transfers
3190da2e776SDaniel Kurtz * start with a proper START condition, and proceed normally.
3200da2e776SDaniel Kurtz *
3210da2e776SDaniel Kurtz * The HDMIPHY bus is an internal bus that always has exactly two
3220da2e776SDaniel Kurtz * devices, the host as Master and the HDMIPHY device as the slave.
3230da2e776SDaniel Kurtz * Skipping the STOP condition has been tested on this bus and works.
3240da2e776SDaniel Kurtz */
3250da2e776SDaniel Kurtz if (i2c->quirks & QUIRK_HDMIPHY) {
3260da2e776SDaniel Kurtz /* Stop driving the I2C pins */
3270da2e776SDaniel Kurtz iicstat &= ~S3C2410_IICSTAT_TXRXEN;
3280da2e776SDaniel Kurtz } else {
3291da177e4SLinus Torvalds /* stop the transfer */
3301da177e4SLinus Torvalds iicstat &= ~S3C2410_IICSTAT_START;
3310da2e776SDaniel Kurtz }
3321da177e4SLinus Torvalds writel(iicstat, i2c->regs + S3C2410_IICSTAT);
3331da177e4SLinus Torvalds
3341da177e4SLinus Torvalds i2c->state = STATE_STOP;
3351da177e4SLinus Torvalds
3361da177e4SLinus Torvalds s3c24xx_i2c_master_complete(i2c, ret);
3371da177e4SLinus Torvalds s3c24xx_i2c_disable_irq(i2c);
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds
3400915833bSKrzysztof Kozlowski /*
3410915833bSKrzysztof Kozlowski * helper functions to determine the current state in the set of
3420915833bSKrzysztof Kozlowski * messages we are sending
3430915833bSKrzysztof Kozlowski */
3441da177e4SLinus Torvalds
345ec7c34a4SKrzysztof Kozlowski /*
3461da177e4SLinus Torvalds * returns TRUE if the current message is the last in the set
3471da177e4SLinus Torvalds */
is_lastmsg(struct s3c24xx_i2c * i2c)3481da177e4SLinus Torvalds static inline int is_lastmsg(struct s3c24xx_i2c *i2c)
3491da177e4SLinus Torvalds {
3501da177e4SLinus Torvalds return i2c->msg_idx >= (i2c->msg_num - 1);
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds
353ec7c34a4SKrzysztof Kozlowski /*
3541da177e4SLinus Torvalds * returns TRUE if we this is the last byte in the current message
3551da177e4SLinus Torvalds */
is_msglast(struct s3c24xx_i2c * i2c)3561da177e4SLinus Torvalds static inline int is_msglast(struct s3c24xx_i2c *i2c)
3571da177e4SLinus Torvalds {
3580915833bSKrzysztof Kozlowski /*
3590915833bSKrzysztof Kozlowski * msg->len is always 1 for the first byte of smbus block read.
36085747311SJaemin Yoo * Actual length will be read from slave. More bytes will be
3610915833bSKrzysztof Kozlowski * read according to the length then.
3620915833bSKrzysztof Kozlowski */
36385747311SJaemin Yoo if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1)
36485747311SJaemin Yoo return 0;
36585747311SJaemin Yoo
3661da177e4SLinus Torvalds return i2c->msg_ptr == i2c->msg->len-1;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds
369ec7c34a4SKrzysztof Kozlowski /*
3701da177e4SLinus Torvalds * returns TRUE if we reached the end of the current message
3711da177e4SLinus Torvalds */
is_msgend(struct s3c24xx_i2c * i2c)3721da177e4SLinus Torvalds static inline int is_msgend(struct s3c24xx_i2c *i2c)
3731da177e4SLinus Torvalds {
3741da177e4SLinus Torvalds return i2c->msg_ptr >= i2c->msg->len;
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds
377ec7c34a4SKrzysztof Kozlowski /*
3781da177e4SLinus Torvalds * process an interrupt and work out what to do
3791da177e4SLinus Torvalds */
i2c_s3c_irq_nextbyte(struct s3c24xx_i2c * i2c,unsigned long iicstat)38010e806d3SJason Yan static void i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat)
3811da177e4SLinus Torvalds {
3821da177e4SLinus Torvalds unsigned long tmp;
3831da177e4SLinus Torvalds unsigned char byte;
3841da177e4SLinus Torvalds
3851da177e4SLinus Torvalds switch (i2c->state) {
3861da177e4SLinus Torvalds
3871da177e4SLinus Torvalds case STATE_IDLE:
38808882d20SHarvey Harrison dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__);
3891da177e4SLinus Torvalds goto out;
3901da177e4SLinus Torvalds
3911da177e4SLinus Torvalds case STATE_STOP:
39208882d20SHarvey Harrison dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__);
3931da177e4SLinus Torvalds s3c24xx_i2c_disable_irq(i2c);
3941da177e4SLinus Torvalds goto out_ack;
3951da177e4SLinus Torvalds
3961da177e4SLinus Torvalds case STATE_START:
3970915833bSKrzysztof Kozlowski /*
3980915833bSKrzysztof Kozlowski * last thing we did was send a start condition on the
3991da177e4SLinus Torvalds * bus, or started a new i2c message
4001da177e4SLinus Torvalds */
4011da177e4SLinus Torvalds if (iicstat & S3C2410_IICSTAT_LASTBIT &&
4021da177e4SLinus Torvalds !(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
4031da177e4SLinus Torvalds /* ack was not received... */
4041da177e4SLinus Torvalds dev_dbg(i2c->dev, "ack was not received\n");
40563f5c289SBen Dooks s3c24xx_i2c_stop(i2c, -ENXIO);
4061da177e4SLinus Torvalds goto out_ack;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds
4091da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_RD)
4101da177e4SLinus Torvalds i2c->state = STATE_READ;
4111da177e4SLinus Torvalds else
4121da177e4SLinus Torvalds i2c->state = STATE_WRITE;
4131da177e4SLinus Torvalds
4140915833bSKrzysztof Kozlowski /*
4150915833bSKrzysztof Kozlowski * Terminate the transfer if there is nothing to do
4160915833bSKrzysztof Kozlowski * as this is used by the i2c probe to find devices.
4170915833bSKrzysztof Kozlowski */
4181da177e4SLinus Torvalds if (is_lastmsg(i2c) && i2c->msg->len == 0) {
4191da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0);
4201da177e4SLinus Torvalds goto out_ack;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds
4231da177e4SLinus Torvalds if (i2c->state == STATE_READ)
4241da177e4SLinus Torvalds goto prepare_read;
4251da177e4SLinus Torvalds
4260915833bSKrzysztof Kozlowski /*
4270915833bSKrzysztof Kozlowski * fall through to the write state, as we will need to
4280915833bSKrzysztof Kozlowski * send a byte as well
4291da177e4SLinus Torvalds */
43081eeae43SJoe Perches fallthrough;
4310915833bSKrzysztof Kozlowski case STATE_WRITE:
4320915833bSKrzysztof Kozlowski /*
4330915833bSKrzysztof Kozlowski * we are writing data to the device... check for the
4340915833bSKrzysztof Kozlowski * end of the message, and if so, work out what to do
4350915833bSKrzysztof Kozlowski */
4362709781bSBen Dooks if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) {
4372709781bSBen Dooks if (iicstat & S3C2410_IICSTAT_LASTBIT) {
4382709781bSBen Dooks dev_dbg(i2c->dev, "WRITE: No Ack\n");
4392709781bSBen Dooks
4402709781bSBen Dooks s3c24xx_i2c_stop(i2c, -ECONNREFUSED);
4412709781bSBen Dooks goto out_ack;
4422709781bSBen Dooks }
4432709781bSBen Dooks }
4442709781bSBen Dooks
4451da177e4SLinus Torvalds retry_write:
4462709781bSBen Dooks
4471da177e4SLinus Torvalds if (!is_msgend(i2c)) {
4481da177e4SLinus Torvalds byte = i2c->msg->buf[i2c->msg_ptr++];
4491da177e4SLinus Torvalds writeb(byte, i2c->regs + S3C2410_IICDS);
4501da177e4SLinus Torvalds
4510915833bSKrzysztof Kozlowski /*
4520915833bSKrzysztof Kozlowski * delay after writing the byte to allow the
453e00a8cdfSBen Dooks * data setup time on the bus, as writing the
454e00a8cdfSBen Dooks * data to the register causes the first bit
455e00a8cdfSBen Dooks * to appear on SDA, and SCL will change as
4560915833bSKrzysztof Kozlowski * soon as the interrupt is acknowledged
4570915833bSKrzysztof Kozlowski */
458e00a8cdfSBen Dooks ndelay(i2c->tx_setup);
459e00a8cdfSBen Dooks
4601da177e4SLinus Torvalds } else if (!is_lastmsg(i2c)) {
4611da177e4SLinus Torvalds /* we need to go to the next i2c message */
4621da177e4SLinus Torvalds
4631da177e4SLinus Torvalds dev_dbg(i2c->dev, "WRITE: Next Message\n");
4641da177e4SLinus Torvalds
4651da177e4SLinus Torvalds i2c->msg_ptr = 0;
4661da177e4SLinus Torvalds i2c->msg_idx++;
4671da177e4SLinus Torvalds i2c->msg++;
4681da177e4SLinus Torvalds
4691da177e4SLinus Torvalds /* check to see if we need to do another message */
4701da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_NOSTART) {
4711da177e4SLinus Torvalds
4721da177e4SLinus Torvalds if (i2c->msg->flags & I2C_M_RD) {
4730915833bSKrzysztof Kozlowski /*
4740915833bSKrzysztof Kozlowski * cannot do this, the controller
4751da177e4SLinus Torvalds * forces us to send a new START
4760915833bSKrzysztof Kozlowski * when we change direction
4770915833bSKrzysztof Kozlowski */
47824990423SKrzysztof Kozlowski dev_dbg(i2c->dev,
47924990423SKrzysztof Kozlowski "missing START before write->read\n");
4801da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, -EINVAL);
48124990423SKrzysztof Kozlowski break;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds
4841da177e4SLinus Torvalds goto retry_write;
4851da177e4SLinus Torvalds } else {
4861da177e4SLinus Torvalds /* send the new start */
4871da177e4SLinus Torvalds s3c24xx_i2c_message_start(i2c, i2c->msg);
4881da177e4SLinus Torvalds i2c->state = STATE_START;
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds
4911da177e4SLinus Torvalds } else {
4921da177e4SLinus Torvalds /* send stop */
4931da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0);
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds break;
4961da177e4SLinus Torvalds
4971da177e4SLinus Torvalds case STATE_READ:
4980915833bSKrzysztof Kozlowski /*
4990915833bSKrzysztof Kozlowski * we have a byte of data in the data register, do
50048fc7f7eSAdam Buchbinder * something with it, and then work out whether we are
5011da177e4SLinus Torvalds * going to do any more read/write
5021da177e4SLinus Torvalds */
5031da177e4SLinus Torvalds byte = readb(i2c->regs + S3C2410_IICDS);
5041da177e4SLinus Torvalds i2c->msg->buf[i2c->msg_ptr++] = byte;
5051da177e4SLinus Torvalds
50685747311SJaemin Yoo /* Add actual length to read for smbus block read */
50785747311SJaemin Yoo if (i2c->msg->flags & I2C_M_RECV_LEN && i2c->msg->len == 1)
50885747311SJaemin Yoo i2c->msg->len += byte;
5091da177e4SLinus Torvalds prepare_read:
5101da177e4SLinus Torvalds if (is_msglast(i2c)) {
5111da177e4SLinus Torvalds /* last byte of buffer */
5121da177e4SLinus Torvalds
5131da177e4SLinus Torvalds if (is_lastmsg(i2c))
5141da177e4SLinus Torvalds s3c24xx_i2c_disable_ack(i2c);
5151da177e4SLinus Torvalds
5161da177e4SLinus Torvalds } else if (is_msgend(i2c)) {
5170915833bSKrzysztof Kozlowski /*
5180915833bSKrzysztof Kozlowski * ok, we've read the entire buffer, see if there
5190915833bSKrzysztof Kozlowski * is anything else we need to do
5200915833bSKrzysztof Kozlowski */
5211da177e4SLinus Torvalds if (is_lastmsg(i2c)) {
5221da177e4SLinus Torvalds /* last message, send stop and complete */
5231da177e4SLinus Torvalds dev_dbg(i2c->dev, "READ: Send Stop\n");
5241da177e4SLinus Torvalds
5251da177e4SLinus Torvalds s3c24xx_i2c_stop(i2c, 0);
5261da177e4SLinus Torvalds } else {
5271da177e4SLinus Torvalds /* go to the next transfer */
5281da177e4SLinus Torvalds dev_dbg(i2c->dev, "READ: Next Transfer\n");
5291da177e4SLinus Torvalds
5301da177e4SLinus Torvalds i2c->msg_ptr = 0;
5311da177e4SLinus Torvalds i2c->msg_idx++;
5321da177e4SLinus Torvalds i2c->msg++;
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds }
5351da177e4SLinus Torvalds
5361da177e4SLinus Torvalds break;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds
5391da177e4SLinus Torvalds /* acknowlegde the IRQ and get back on with the work */
5401da177e4SLinus Torvalds
5411da177e4SLinus Torvalds out_ack:
5421da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON);
5431da177e4SLinus Torvalds tmp &= ~S3C2410_IICCON_IRQPEND;
5441da177e4SLinus Torvalds writel(tmp, i2c->regs + S3C2410_IICCON);
5451da177e4SLinus Torvalds out:
54610e806d3SJason Yan return;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds
549ec7c34a4SKrzysztof Kozlowski /*
5501da177e4SLinus Torvalds * top level IRQ servicing routine
5511da177e4SLinus Torvalds */
s3c24xx_i2c_irq(int irqno,void * dev_id)5527d12e780SDavid Howells static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id)
5531da177e4SLinus Torvalds {
5541da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = dev_id;
5551da177e4SLinus Torvalds unsigned long status;
5561da177e4SLinus Torvalds unsigned long tmp;
5571da177e4SLinus Torvalds
5581da177e4SLinus Torvalds status = readl(i2c->regs + S3C2410_IICSTAT);
5591da177e4SLinus Torvalds
5601da177e4SLinus Torvalds if (status & S3C2410_IICSTAT_ARBITR) {
5613d0911bfSBen Dooks /* deal with arbitration loss */
5621da177e4SLinus Torvalds dev_err(i2c->dev, "deal with arbitration loss\n");
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds
5651da177e4SLinus Torvalds if (i2c->state == STATE_IDLE) {
5661da177e4SLinus Torvalds dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n");
5671da177e4SLinus Torvalds
5681da177e4SLinus Torvalds tmp = readl(i2c->regs + S3C2410_IICCON);
5691da177e4SLinus Torvalds tmp &= ~S3C2410_IICCON_IRQPEND;
5701da177e4SLinus Torvalds writel(tmp, i2c->regs + S3C2410_IICCON);
5711da177e4SLinus Torvalds goto out;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds
5740915833bSKrzysztof Kozlowski /*
5750915833bSKrzysztof Kozlowski * pretty much this leaves us with the fact that we've
5760915833bSKrzysztof Kozlowski * transmitted or received whatever byte we last sent
5770915833bSKrzysztof Kozlowski */
57819820510SHuisung Kang i2c_s3c_irq_nextbyte(i2c, status);
5791da177e4SLinus Torvalds
5801da177e4SLinus Torvalds out:
5811da177e4SLinus Torvalds return IRQ_HANDLED;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds
584069a9502SSimon Glass /*
585069a9502SSimon Glass * Disable the bus so that we won't get any interrupts from now on, or try
586069a9502SSimon Glass * to drive any lines. This is the default state when we don't have
587069a9502SSimon Glass * anything to send/receive.
588069a9502SSimon Glass *
589069a9502SSimon Glass * If there is an event on the bus, or we have a pre-existing event at
590069a9502SSimon Glass * kernel boot time, we may not notice the event and the I2C controller
591069a9502SSimon Glass * will lock the bus with the I2C clock line low indefinitely.
592069a9502SSimon Glass */
s3c24xx_i2c_disable_bus(struct s3c24xx_i2c * i2c)593069a9502SSimon Glass static inline void s3c24xx_i2c_disable_bus(struct s3c24xx_i2c *i2c)
594069a9502SSimon Glass {
595069a9502SSimon Glass unsigned long tmp;
596069a9502SSimon Glass
597069a9502SSimon Glass /* Stop driving the I2C pins */
598069a9502SSimon Glass tmp = readl(i2c->regs + S3C2410_IICSTAT);
599069a9502SSimon Glass tmp &= ~S3C2410_IICSTAT_TXRXEN;
600069a9502SSimon Glass writel(tmp, i2c->regs + S3C2410_IICSTAT);
601069a9502SSimon Glass
602069a9502SSimon Glass /* We don't expect any interrupts now, and don't want send acks */
603069a9502SSimon Glass tmp = readl(i2c->regs + S3C2410_IICCON);
604069a9502SSimon Glass tmp &= ~(S3C2410_IICCON_IRQEN | S3C2410_IICCON_IRQPEND |
605069a9502SSimon Glass S3C2410_IICCON_ACKEN);
606069a9502SSimon Glass writel(tmp, i2c->regs + S3C2410_IICCON);
607069a9502SSimon Glass }
608069a9502SSimon Glass
6091da177e4SLinus Torvalds
610ec7c34a4SKrzysztof Kozlowski /*
6111da177e4SLinus Torvalds * get the i2c bus for a master transaction
6121da177e4SLinus Torvalds */
s3c24xx_i2c_set_master(struct s3c24xx_i2c * i2c)6131da177e4SLinus Torvalds static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c)
6141da177e4SLinus Torvalds {
6151da177e4SLinus Torvalds unsigned long iicstat;
6161da177e4SLinus Torvalds int timeout = 400;
6171da177e4SLinus Torvalds
6181da177e4SLinus Torvalds while (timeout-- > 0) {
6191da177e4SLinus Torvalds iicstat = readl(i2c->regs + S3C2410_IICSTAT);
6201da177e4SLinus Torvalds
6211da177e4SLinus Torvalds if (!(iicstat & S3C2410_IICSTAT_BUSBUSY))
6221da177e4SLinus Torvalds return 0;
6231da177e4SLinus Torvalds
6241da177e4SLinus Torvalds msleep(1);
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds
6271da177e4SLinus Torvalds return -ETIMEDOUT;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds
630ec7c34a4SKrzysztof Kozlowski /*
631fe724bf9SDaniel Kurtz * wait for the i2c bus to become idle.
632fe724bf9SDaniel Kurtz */
s3c24xx_i2c_wait_idle(struct s3c24xx_i2c * i2c)633fe724bf9SDaniel Kurtz static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c)
634fe724bf9SDaniel Kurtz {
635fe724bf9SDaniel Kurtz unsigned long iicstat;
636fe724bf9SDaniel Kurtz ktime_t start, now;
637fe724bf9SDaniel Kurtz unsigned long delay;
63831f313d9SMark Brown int spins;
639fe724bf9SDaniel Kurtz
640fe724bf9SDaniel Kurtz /* ensure the stop has been through the bus */
641fe724bf9SDaniel Kurtz
642fe724bf9SDaniel Kurtz dev_dbg(i2c->dev, "waiting for bus idle\n");
643fe724bf9SDaniel Kurtz
644fe724bf9SDaniel Kurtz start = now = ktime_get();
645fe724bf9SDaniel Kurtz
646fe724bf9SDaniel Kurtz /*
647fe724bf9SDaniel Kurtz * Most of the time, the bus is already idle within a few usec of the
648fe724bf9SDaniel Kurtz * end of a transaction. However, really slow i2c devices can stretch
649fe724bf9SDaniel Kurtz * the clock, delaying STOP generation.
650fe724bf9SDaniel Kurtz *
65131f313d9SMark Brown * On slower SoCs this typically happens within a very small number of
65231f313d9SMark Brown * instructions so busy wait briefly to avoid scheduling overhead.
653fe724bf9SDaniel Kurtz */
65431f313d9SMark Brown spins = 3;
655fe724bf9SDaniel Kurtz iicstat = readl(i2c->regs + S3C2410_IICSTAT);
65631f313d9SMark Brown while ((iicstat & S3C2410_IICSTAT_START) && --spins) {
65731f313d9SMark Brown cpu_relax();
65831f313d9SMark Brown iicstat = readl(i2c->regs + S3C2410_IICSTAT);
65931f313d9SMark Brown }
66031f313d9SMark Brown
66131f313d9SMark Brown /*
66231f313d9SMark Brown * If we do get an appreciable delay as a compromise between idle
66331f313d9SMark Brown * detection latency for the normal, fast case, and system load in the
66431f313d9SMark Brown * slow device case, use an exponential back off in the polling loop,
66531f313d9SMark Brown * up to 1/10th of the total timeout, then continue to poll at a
66631f313d9SMark Brown * constant rate up to the timeout.
66731f313d9SMark Brown */
668fe724bf9SDaniel Kurtz delay = 1;
669fe724bf9SDaniel Kurtz while ((iicstat & S3C2410_IICSTAT_START) &&
670fe724bf9SDaniel Kurtz ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) {
671fe724bf9SDaniel Kurtz usleep_range(delay, 2 * delay);
672fe724bf9SDaniel Kurtz if (delay < S3C2410_IDLE_TIMEOUT / 10)
673fe724bf9SDaniel Kurtz delay <<= 1;
674fe724bf9SDaniel Kurtz now = ktime_get();
675fe724bf9SDaniel Kurtz iicstat = readl(i2c->regs + S3C2410_IICSTAT);
676fe724bf9SDaniel Kurtz }
677fe724bf9SDaniel Kurtz
678fe724bf9SDaniel Kurtz if (iicstat & S3C2410_IICSTAT_START)
679fe724bf9SDaniel Kurtz dev_warn(i2c->dev, "timeout waiting for bus idle\n");
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds
682ec7c34a4SKrzysztof Kozlowski /*
6831da177e4SLinus Torvalds * this starts an i2c transfer
6841da177e4SLinus Torvalds */
s3c24xx_i2c_doxfer(struct s3c24xx_i2c * i2c,struct i2c_msg * msgs,int num)6853d0911bfSBen Dooks static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c,
6863d0911bfSBen Dooks struct i2c_msg *msgs, int num)
6871da177e4SLinus Torvalds {
688bc0ff802SWolfram Sang long time_left = 0;
6891da177e4SLinus Torvalds int ret;
6901da177e4SLinus Torvalds
6911da177e4SLinus Torvalds ret = s3c24xx_i2c_set_master(i2c);
6921da177e4SLinus Torvalds if (ret != 0) {
6931da177e4SLinus Torvalds dev_err(i2c->dev, "cannot get bus (error %d)\n", ret);
6941da177e4SLinus Torvalds ret = -EAGAIN;
6951da177e4SLinus Torvalds goto out;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds
6981da177e4SLinus Torvalds i2c->msg = msgs;
6991da177e4SLinus Torvalds i2c->msg_num = num;
7001da177e4SLinus Torvalds i2c->msg_ptr = 0;
7011da177e4SLinus Torvalds i2c->msg_idx = 0;
7021da177e4SLinus Torvalds i2c->state = STATE_START;
7031da177e4SLinus Torvalds
7041da177e4SLinus Torvalds s3c24xx_i2c_enable_irq(i2c);
7051da177e4SLinus Torvalds s3c24xx_i2c_message_start(i2c, msgs);
7061da177e4SLinus Torvalds
707187432b8SMarek Szyprowski if (i2c->quirks & (QUIRK_POLL | QUIRK_ATOMIC)) {
708990489e1SMarek Szyprowski while ((i2c->msg_num != 0) && is_ack(i2c)) {
709990489e1SMarek Szyprowski unsigned long stat = readl(i2c->regs + S3C2410_IICSTAT);
710117053f7SVasanth Ananthan
711990489e1SMarek Szyprowski i2c_s3c_irq_nextbyte(i2c, stat);
712117053f7SVasanth Ananthan
713990489e1SMarek Szyprowski stat = readl(i2c->regs + S3C2410_IICSTAT);
714990489e1SMarek Szyprowski if (stat & S3C2410_IICSTAT_ARBITR)
715990489e1SMarek Szyprowski dev_err(i2c->dev, "deal with arbitration loss\n");
716117053f7SVasanth Ananthan }
717990489e1SMarek Szyprowski } else {
718bc0ff802SWolfram Sang time_left = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
719990489e1SMarek Szyprowski }
7201da177e4SLinus Torvalds
7211da177e4SLinus Torvalds ret = i2c->msg_idx;
7221da177e4SLinus Torvalds
7230915833bSKrzysztof Kozlowski /*
7240915833bSKrzysztof Kozlowski * Having these next two as dev_err() makes life very
7250915833bSKrzysztof Kozlowski * noisy when doing an i2cdetect
7260915833bSKrzysztof Kozlowski */
727bc0ff802SWolfram Sang if (time_left == 0)
7281da177e4SLinus Torvalds dev_dbg(i2c->dev, "timeout\n");
7291da177e4SLinus Torvalds else if (ret != num)
7301da177e4SLinus Torvalds dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret);
7311da177e4SLinus Torvalds
7320da2e776SDaniel Kurtz /* For QUIRK_HDMIPHY, bus is already disabled */
7330da2e776SDaniel Kurtz if (i2c->quirks & QUIRK_HDMIPHY)
7340da2e776SDaniel Kurtz goto out;
7351da177e4SLinus Torvalds
736fe724bf9SDaniel Kurtz s3c24xx_i2c_wait_idle(i2c);
7371da177e4SLinus Torvalds
738069a9502SSimon Glass s3c24xx_i2c_disable_bus(i2c);
739069a9502SSimon Glass
7401da177e4SLinus Torvalds out:
741069a9502SSimon Glass i2c->state = STATE_IDLE;
742069a9502SSimon Glass
7431da177e4SLinus Torvalds return ret;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds
746ec7c34a4SKrzysztof Kozlowski /*
7471da177e4SLinus Torvalds * first port of call from the i2c bus code when an message needs
74844bbe87eSSteven Cole * transferring across the i2c bus.
7491da177e4SLinus Torvalds */
s3c24xx_i2c_xfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)7501da177e4SLinus Torvalds static int s3c24xx_i2c_xfer(struct i2c_adapter *adap,
7511da177e4SLinus Torvalds struct i2c_msg *msgs, int num)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
7541da177e4SLinus Torvalds int retry;
7551da177e4SLinus Torvalds int ret;
7561da177e4SLinus Torvalds
75734e81ad5SPaul Osmialowski ret = clk_enable(i2c->clk);
75834e81ad5SPaul Osmialowski if (ret)
75934e81ad5SPaul Osmialowski return ret;
760d2360b8eSArnaud Patard (Rtp)
7611da177e4SLinus Torvalds for (retry = 0; retry < adap->retries; retry++) {
7621da177e4SLinus Torvalds
7631da177e4SLinus Torvalds ret = s3c24xx_i2c_doxfer(i2c, msgs, num);
7641da177e4SLinus Torvalds
765d2360b8eSArnaud Patard (Rtp) if (ret != -EAGAIN) {
76634e81ad5SPaul Osmialowski clk_disable(i2c->clk);
7671da177e4SLinus Torvalds return ret;
768d2360b8eSArnaud Patard (Rtp) }
7691da177e4SLinus Torvalds
7701da177e4SLinus Torvalds dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry);
7711da177e4SLinus Torvalds
7721da177e4SLinus Torvalds udelay(100);
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds
77534e81ad5SPaul Osmialowski clk_disable(i2c->clk);
7761da177e4SLinus Torvalds return -EREMOTEIO;
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds
s3c24xx_i2c_xfer_atomic(struct i2c_adapter * adap,struct i2c_msg * msgs,int num)779187432b8SMarek Szyprowski static int s3c24xx_i2c_xfer_atomic(struct i2c_adapter *adap,
780187432b8SMarek Szyprowski struct i2c_msg *msgs, int num)
781187432b8SMarek Szyprowski {
782187432b8SMarek Szyprowski struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data;
783187432b8SMarek Szyprowski int ret;
784187432b8SMarek Szyprowski
785187432b8SMarek Szyprowski disable_irq(i2c->irq);
786187432b8SMarek Szyprowski i2c->quirks |= QUIRK_ATOMIC;
787187432b8SMarek Szyprowski ret = s3c24xx_i2c_xfer(adap, msgs, num);
788187432b8SMarek Szyprowski i2c->quirks &= ~QUIRK_ATOMIC;
789187432b8SMarek Szyprowski enable_irq(i2c->irq);
790187432b8SMarek Szyprowski
791187432b8SMarek Szyprowski return ret;
792187432b8SMarek Szyprowski }
793187432b8SMarek Szyprowski
7941da177e4SLinus Torvalds /* declare our i2c functionality */
s3c24xx_i2c_func(struct i2c_adapter * adap)7951da177e4SLinus Torvalds static u32 s3c24xx_i2c_func(struct i2c_adapter *adap)
7961da177e4SLinus Torvalds {
79781f19932SWolfram Sang return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL_ALL | I2C_FUNC_NOSTART |
79814674e70SMark Brown I2C_FUNC_PROTOCOL_MANGLING;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds
8011da177e4SLinus Torvalds /* i2c bus registration info */
8028f9082c5SJean Delvare static const struct i2c_algorithm s3c24xx_i2c_algorithm = {
8031da177e4SLinus Torvalds .master_xfer = s3c24xx_i2c_xfer,
804187432b8SMarek Szyprowski .master_xfer_atomic = s3c24xx_i2c_xfer_atomic,
8051da177e4SLinus Torvalds .functionality = s3c24xx_i2c_func,
8061da177e4SLinus Torvalds };
8071da177e4SLinus Torvalds
808ec7c34a4SKrzysztof Kozlowski /*
8091da177e4SLinus Torvalds * return the divisor settings for a given frequency
8101da177e4SLinus Torvalds */
s3c24xx_i2c_calcdivisor(unsigned long clkin,unsigned int wanted,unsigned int * div1,unsigned int * divs)8111da177e4SLinus Torvalds static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted,
8121da177e4SLinus Torvalds unsigned int *div1, unsigned int *divs)
8131da177e4SLinus Torvalds {
8141da177e4SLinus Torvalds unsigned int calc_divs = clkin / wanted;
8151da177e4SLinus Torvalds unsigned int calc_div1;
8161da177e4SLinus Torvalds
8171da177e4SLinus Torvalds if (calc_divs > (16*16))
8181da177e4SLinus Torvalds calc_div1 = 512;
8191da177e4SLinus Torvalds else
8201da177e4SLinus Torvalds calc_div1 = 16;
8211da177e4SLinus Torvalds
8221da177e4SLinus Torvalds calc_divs += calc_div1-1;
8231da177e4SLinus Torvalds calc_divs /= calc_div1;
8241da177e4SLinus Torvalds
8251da177e4SLinus Torvalds if (calc_divs == 0)
8261da177e4SLinus Torvalds calc_divs = 1;
8271da177e4SLinus Torvalds if (calc_divs > 17)
8281da177e4SLinus Torvalds calc_divs = 17;
8291da177e4SLinus Torvalds
8301da177e4SLinus Torvalds *divs = calc_divs;
8311da177e4SLinus Torvalds *div1 = calc_div1;
8321da177e4SLinus Torvalds
8331da177e4SLinus Torvalds return clkin / (calc_divs * calc_div1);
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds
836ec7c34a4SKrzysztof Kozlowski /*
8371da177e4SLinus Torvalds * work out a divisor for the user requested frequency setting,
8381da177e4SLinus Torvalds * either by the requested frequency, or scanning the acceptable
8391da177e4SLinus Torvalds * range of frequencies until something is found
8401da177e4SLinus Torvalds */
s3c24xx_i2c_clockrate(struct s3c24xx_i2c * i2c,unsigned int * got)84161c7cff8SBen Dooks static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got)
8421da177e4SLinus Torvalds {
8434fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata = i2c->pdata;
8441da177e4SLinus Torvalds unsigned long clkin = clk_get_rate(i2c->clk);
8451da177e4SLinus Torvalds unsigned int divs, div1;
846c564e6aeSDaniel Silverstone unsigned long target_frequency;
84761c7cff8SBen Dooks u32 iiccon;
8481da177e4SLinus Torvalds int freq;
8491da177e4SLinus Torvalds
85061c7cff8SBen Dooks i2c->clkrate = clkin;
8511da177e4SLinus Torvalds clkin /= 1000; /* clkin now in KHz */
8521da177e4SLinus Torvalds
853c564e6aeSDaniel Silverstone dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency);
8541da177e4SLinus Torvalds
85590224e64SAndy Shevchenko target_frequency = pdata->frequency ?: I2C_MAX_STANDARD_MODE_FREQ;
8561da177e4SLinus Torvalds
857c564e6aeSDaniel Silverstone target_frequency /= 1000; /* Target frequency now in KHz */
8581da177e4SLinus Torvalds
859c564e6aeSDaniel Silverstone freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs);
8601da177e4SLinus Torvalds
861c564e6aeSDaniel Silverstone if (freq > target_frequency) {
862c564e6aeSDaniel Silverstone dev_err(i2c->dev,
863c564e6aeSDaniel Silverstone "Unable to achieve desired frequency %luKHz." \
864c564e6aeSDaniel Silverstone " Lowest achievable %dKHz\n", target_frequency, freq);
8651da177e4SLinus Torvalds return -EINVAL;
866c564e6aeSDaniel Silverstone }
8671da177e4SLinus Torvalds
8681da177e4SLinus Torvalds *got = freq;
86961c7cff8SBen Dooks
87061c7cff8SBen Dooks iiccon = readl(i2c->regs + S3C2410_IICCON);
87161c7cff8SBen Dooks iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512);
87261c7cff8SBen Dooks iiccon |= (divs-1);
87361c7cff8SBen Dooks
87461c7cff8SBen Dooks if (div1 == 512)
87561c7cff8SBen Dooks iiccon |= S3C2410_IICCON_TXDIV_512;
87661c7cff8SBen Dooks
877117053f7SVasanth Ananthan if (i2c->quirks & QUIRK_POLL)
878117053f7SVasanth Ananthan iiccon |= S3C2410_IICCON_SCALE(2);
879117053f7SVasanth Ananthan
88061c7cff8SBen Dooks writel(iiccon, i2c->regs + S3C2410_IICCON);
88161c7cff8SBen Dooks
88227452498SKarol Lewandowski if (i2c->quirks & QUIRK_S3C2440) {
883a192f715SBen Dooks unsigned long sda_delay;
884a192f715SBen Dooks
885a192f715SBen Dooks if (pdata->sda_delay) {
8867031307aSMyungJoo Ham sda_delay = clkin * pdata->sda_delay;
8877031307aSMyungJoo Ham sda_delay = DIV_ROUND_UP(sda_delay, 1000000);
888a192f715SBen Dooks sda_delay = DIV_ROUND_UP(sda_delay, 5);
889a192f715SBen Dooks if (sda_delay > 3)
890a192f715SBen Dooks sda_delay = 3;
891a192f715SBen Dooks sda_delay |= S3C2410_IICLC_FILTER_ON;
892a192f715SBen Dooks } else
893a192f715SBen Dooks sda_delay = 0;
894a192f715SBen Dooks
895a192f715SBen Dooks dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay);
896a192f715SBen Dooks writel(sda_delay, i2c->regs + S3C2440_IICLC);
897a192f715SBen Dooks }
898a192f715SBen Dooks
8991da177e4SLinus Torvalds return 0;
9001da177e4SLinus Torvalds }
9011da177e4SLinus Torvalds
9025a5f5080SThomas Abraham #ifdef CONFIG_OF
s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c * i2c)9035a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
9045a5f5080SThomas Abraham {
905ed7357c9SLinus Walleij int i;
9065a5f5080SThomas Abraham
907ec39ef83SKarol Lewandowski if (i2c->quirks & QUIRK_NO_GPIO)
908ec39ef83SKarol Lewandowski return 0;
909ec39ef83SKarol Lewandowski
910ed7357c9SLinus Walleij for (i = 0; i < 2; i++) {
911ed7357c9SLinus Walleij i2c->gpios[i] = devm_gpiod_get_index(i2c->dev, NULL,
912ed7357c9SLinus Walleij i, GPIOD_ASIS);
913ed7357c9SLinus Walleij if (IS_ERR(i2c->gpios[i])) {
914ed7357c9SLinus Walleij dev_err(i2c->dev, "i2c gpio invalid at index %d\n", i);
9155a5f5080SThomas Abraham return -EINVAL;
9165a5f5080SThomas Abraham }
9175a5f5080SThomas Abraham }
918ed7357c9SLinus Walleij return 0;
919ed7357c9SLinus Walleij }
920ed7357c9SLinus Walleij
9215a5f5080SThomas Abraham #else
s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c * i2c)9225a5f5080SThomas Abraham static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c)
9235a5f5080SThomas Abraham {
9248ebe661dSTushar Behera return 0;
9255a5f5080SThomas Abraham }
9265a5f5080SThomas Abraham #endif
9275a5f5080SThomas Abraham
928ec7c34a4SKrzysztof Kozlowski /*
9291da177e4SLinus Torvalds * initialise the controller, set the IO lines and frequency
9301da177e4SLinus Torvalds */
s3c24xx_i2c_init(struct s3c24xx_i2c * i2c)9311da177e4SLinus Torvalds static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c)
9321da177e4SLinus Torvalds {
9331da177e4SLinus Torvalds struct s3c2410_platform_i2c *pdata;
9341da177e4SLinus Torvalds unsigned int freq;
9351da177e4SLinus Torvalds
9361da177e4SLinus Torvalds /* get the plafrom data */
9371da177e4SLinus Torvalds
9384fd81eb2SThomas Abraham pdata = i2c->pdata;
9391da177e4SLinus Torvalds
9401da177e4SLinus Torvalds /* write slave address */
9411da177e4SLinus Torvalds
9421da177e4SLinus Torvalds writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD);
9431da177e4SLinus Torvalds
9441da177e4SLinus Torvalds dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr);
9451da177e4SLinus Torvalds
946069a9502SSimon Glass writel(0, i2c->regs + S3C2410_IICCON);
947069a9502SSimon Glass writel(0, i2c->regs + S3C2410_IICSTAT);
94861c7cff8SBen Dooks
9491da177e4SLinus Torvalds /* we need to work out the divisors for the clock... */
9501da177e4SLinus Torvalds
95161c7cff8SBen Dooks if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) {
9521da177e4SLinus Torvalds dev_err(i2c->dev, "cannot meet bus frequency required\n");
9531da177e4SLinus Torvalds return -EINVAL;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds
9561da177e4SLinus Torvalds /* todo - check that the i2c lines aren't being dragged anywhere */
9571da177e4SLinus Torvalds
9581da177e4SLinus Torvalds dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq);
959069a9502SSimon Glass dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02x\n",
960069a9502SSimon Glass readl(i2c->regs + S3C2410_IICCON));
9611da177e4SLinus Torvalds
9621da177e4SLinus Torvalds return 0;
9631da177e4SLinus Torvalds }
9641da177e4SLinus Torvalds
9655a5f5080SThomas Abraham #ifdef CONFIG_OF
966ec7c34a4SKrzysztof Kozlowski /*
9675a5f5080SThomas Abraham * Parse the device tree node and retreive the platform data.
9685a5f5080SThomas Abraham */
9695a5f5080SThomas Abraham static void
s3c24xx_i2c_parse_dt(struct device_node * np,struct s3c24xx_i2c * i2c)9705a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c)
9715a5f5080SThomas Abraham {
9725a5f5080SThomas Abraham struct s3c2410_platform_i2c *pdata = i2c->pdata;
973a7750c3eSPankaj Dubey int id;
9745a5f5080SThomas Abraham
9755a5f5080SThomas Abraham if (!np)
9765a5f5080SThomas Abraham return;
9775a5f5080SThomas Abraham
9785a5f5080SThomas Abraham pdata->bus_num = -1; /* i2c bus number is dynamically assigned */
9795a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay);
9805a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr);
9815a5f5080SThomas Abraham of_property_read_u32(np, "samsung,i2c-max-bus-freq",
9825a5f5080SThomas Abraham (u32 *)&pdata->frequency);
983a7750c3eSPankaj Dubey /*
984a7750c3eSPankaj Dubey * Exynos5's legacy i2c controller and new high speed i2c
985a7750c3eSPankaj Dubey * controller have muxed interrupt sources. By default the
986a7750c3eSPankaj Dubey * interrupts for 4-channel HS-I2C controller are enabled.
987a7750c3eSPankaj Dubey * If nodes for first four channels of legacy i2c controller
988a7750c3eSPankaj Dubey * are available then re-configure the interrupts via the
989a7750c3eSPankaj Dubey * system register.
990a7750c3eSPankaj Dubey */
991a7750c3eSPankaj Dubey id = of_alias_get_id(np, "i2c");
992a7750c3eSPankaj Dubey i2c->sysreg = syscon_regmap_lookup_by_phandle(np,
993a7750c3eSPankaj Dubey "samsung,sysreg-phandle");
994a7750c3eSPankaj Dubey if (IS_ERR(i2c->sysreg))
995a7750c3eSPankaj Dubey return;
996a7750c3eSPankaj Dubey
997a7750c3eSPankaj Dubey regmap_update_bits(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, BIT(id), 0);
9985a5f5080SThomas Abraham }
9995a5f5080SThomas Abraham #else
10005a5f5080SThomas Abraham static void
s3c24xx_i2c_parse_dt(struct device_node * np,struct s3c24xx_i2c * i2c)10010915833bSKrzysztof Kozlowski s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) { }
10025a5f5080SThomas Abraham #endif
10035a5f5080SThomas Abraham
s3c24xx_i2c_probe(struct platform_device * pdev)10043ae5eaecSRussell King static int s3c24xx_i2c_probe(struct platform_device *pdev)
10051da177e4SLinus Torvalds {
1006692acbd3SBen Dooks struct s3c24xx_i2c *i2c;
10074fd81eb2SThomas Abraham struct s3c2410_platform_i2c *pdata = NULL;
10081da177e4SLinus Torvalds struct resource *res;
10091da177e4SLinus Torvalds int ret;
10101da177e4SLinus Torvalds
10115a5f5080SThomas Abraham if (!pdev->dev.of_node) {
10126d4028c6SJingoo Han pdata = dev_get_platdata(&pdev->dev);
10136a039cabSBen Dooks if (!pdata) {
10146a039cabSBen Dooks dev_err(&pdev->dev, "no platform data\n");
10156a039cabSBen Dooks return -EINVAL;
10166a039cabSBen Dooks }
10175a5f5080SThomas Abraham }
1018399dee23SBen Dooks
10194ea1557fSMark Brown i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL);
102046797a2aSJingoo Han if (!i2c)
1021692acbd3SBen Dooks return -ENOMEM;
1022692acbd3SBen Dooks
10234fd81eb2SThomas Abraham i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
102446797a2aSJingoo Han if (!i2c->pdata)
1025669da30dSTushar Behera return -ENOMEM;
10264fd81eb2SThomas Abraham
102727452498SKarol Lewandowski i2c->quirks = s3c24xx_get_device_quirks(pdev);
10288d487a43SVasily Khoruzhick i2c->sysreg = ERR_PTR(-ENOENT);
10294fd81eb2SThomas Abraham if (pdata)
10304fd81eb2SThomas Abraham memcpy(i2c->pdata, pdata, sizeof(*pdata));
10315a5f5080SThomas Abraham else
10325a5f5080SThomas Abraham s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c);
10334fd81eb2SThomas Abraham
1034ea1558ceSWolfram Sang strscpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name));
1035692acbd3SBen Dooks i2c->adap.owner = THIS_MODULE;
1036692acbd3SBen Dooks i2c->adap.algo = &s3c24xx_i2c_algorithm;
1037692acbd3SBen Dooks i2c->adap.retries = 2;
10386031d3dfSWolfram Sang i2c->adap.class = I2C_CLASS_DEPRECATED;
1039692acbd3SBen Dooks i2c->tx_setup = 50;
1040692acbd3SBen Dooks
1041692acbd3SBen Dooks init_waitqueue_head(&i2c->wait);
10421da177e4SLinus Torvalds
10431da177e4SLinus Torvalds /* find the clock and enable it */
10443ae5eaecSRussell King i2c->dev = &pdev->dev;
10452b255b94STushar Behera i2c->clk = devm_clk_get(&pdev->dev, "i2c");
10461da177e4SLinus Torvalds if (IS_ERR(i2c->clk)) {
10473ae5eaecSRussell King dev_err(&pdev->dev, "cannot get clock\n");
1048669da30dSTushar Behera return -ENOENT;
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds
10513ae5eaecSRussell King dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk);
10521da177e4SLinus Torvalds
10531da177e4SLinus Torvalds /* map the registers */
1054b15eb803SYangtao Li i2c->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
105552caa59eSLinus Torvalds if (IS_ERR(i2c->regs))
105652caa59eSLinus Torvalds return PTR_ERR(i2c->regs);
10571da177e4SLinus Torvalds
1058a72ad456SMark Brown dev_dbg(&pdev->dev, "registers %p (%p)\n",
1059a72ad456SMark Brown i2c->regs, res);
10601da177e4SLinus Torvalds
10611da177e4SLinus Torvalds /* setup info block for the i2c core */
10621da177e4SLinus Torvalds i2c->adap.algo_data = i2c;
10633ae5eaecSRussell King i2c->adap.dev.parent = &pdev->dev;
10642693ac69STomasz Figa i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev);
10652693ac69STomasz Figa
1066658122feSAbhilash Kesavan /* inititalise the i2c gpio lines */
10670915833bSKrzysztof Kozlowski if (i2c->pdata->cfg_gpio)
1068658122feSAbhilash Kesavan i2c->pdata->cfg_gpio(to_platform_device(i2c->dev));
10690915833bSKrzysztof Kozlowski else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c))
1070d16933b3STushar Behera return -EINVAL;
1071658122feSAbhilash Kesavan
10721da177e4SLinus Torvalds /* initialise the i2c controller */
1073d16415b2SJavier Martinez Canillas ret = clk_prepare_enable(i2c->clk);
1074d16415b2SJavier Martinez Canillas if (ret) {
1075d16415b2SJavier Martinez Canillas dev_err(&pdev->dev, "I2C clock enable failed\n");
1076d16415b2SJavier Martinez Canillas return ret;
1077d16415b2SJavier Martinez Canillas }
1078d16415b2SJavier Martinez Canillas
10791da177e4SLinus Torvalds ret = s3c24xx_i2c_init(i2c);
108034e81ad5SPaul Osmialowski clk_disable(i2c->clk);
1081d16933b3STushar Behera if (ret != 0) {
1082d16933b3STushar Behera dev_err(&pdev->dev, "I2C controller init failed\n");
1083b1b3df2fSKrzysztof Kozlowski clk_unprepare(i2c->clk);
1084d16933b3STushar Behera return ret;
1085d16933b3STushar Behera }
10860915833bSKrzysztof Kozlowski
10870915833bSKrzysztof Kozlowski /*
10880915833bSKrzysztof Kozlowski * find the IRQ for this unit (note, this relies on the init call to
10891da177e4SLinus Torvalds * ensure no current IRQs pending
10901da177e4SLinus Torvalds */
1091117053f7SVasanth Ananthan if (!(i2c->quirks & QUIRK_POLL)) {
1092e0d1ec97SBen Dooks i2c->irq = ret = platform_get_irq(pdev, 0);
1093d6840a5eSSergey Shtylyov if (ret < 0) {
109434e81ad5SPaul Osmialowski clk_unprepare(i2c->clk);
1095d16933b3STushar Behera return ret;
10961da177e4SLinus Torvalds }
10971da177e4SLinus Torvalds
10980915833bSKrzysztof Kozlowski ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq,
10990915833bSKrzysztof Kozlowski 0, dev_name(&pdev->dev), i2c);
11001da177e4SLinus Torvalds if (ret != 0) {
1101e0d1ec97SBen Dooks dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq);
110234e81ad5SPaul Osmialowski clk_unprepare(i2c->clk);
1103d16933b3STushar Behera return ret;
11041da177e4SLinus Torvalds }
1105117053f7SVasanth Ananthan }
11061da177e4SLinus Torvalds
11070915833bSKrzysztof Kozlowski /*
11080915833bSKrzysztof Kozlowski * Note, previous versions of the driver used i2c_add_adapter()
1109399dee23SBen Dooks * to add the bus at any number. We now pass the bus number via
1110399dee23SBen Dooks * the platform data, so if unset it will now default to always
1111399dee23SBen Dooks * being bus 0.
1112399dee23SBen Dooks */
11134fd81eb2SThomas Abraham i2c->adap.nr = i2c->pdata->bus_num;
11145a5f5080SThomas Abraham i2c->adap.dev.of_node = pdev->dev.of_node;
1115399dee23SBen Dooks
1116eadd709fSWolfram Sang platform_set_drvdata(pdev, i2c);
1117eadd709fSWolfram Sang
1118eadd709fSWolfram Sang pm_runtime_enable(&pdev->dev);
1119eadd709fSWolfram Sang
1120399dee23SBen Dooks ret = i2c_add_numbered_adapter(&i2c->adap);
11211da177e4SLinus Torvalds if (ret < 0) {
1122eadd709fSWolfram Sang pm_runtime_disable(&pdev->dev);
112334e81ad5SPaul Osmialowski clk_unprepare(i2c->clk);
1124dc6fea44STushar Behera return ret;
11251da177e4SLinus Torvalds }
11261da177e4SLinus Torvalds
112722e965c2SJean Delvare dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev));
11285b68790cSBen Dooks return 0;
11291da177e4SLinus Torvalds }
11301da177e4SLinus Torvalds
s3c24xx_i2c_remove(struct platform_device * pdev)1131e190a0c3SUwe Kleine-König static void s3c24xx_i2c_remove(struct platform_device *pdev)
11321da177e4SLinus Torvalds {
11333ae5eaecSRussell King struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev);
11341da177e4SLinus Torvalds
113534e81ad5SPaul Osmialowski clk_unprepare(i2c->clk);
113634e81ad5SPaul Osmialowski
1137c62c3ca5SMark Brown pm_runtime_disable(&pdev->dev);
1138c62c3ca5SMark Brown
11395b68790cSBen Dooks i2c_del_adapter(&i2c->adap);
11401da177e4SLinus Torvalds }
11411da177e4SLinus Torvalds
s3c24xx_i2c_suspend_noirq(struct device * dev)11426a6c6189SMagnus Damm static int s3c24xx_i2c_suspend_noirq(struct device *dev)
1143be44f01eSBen Dooks {
11449242e72aSMasahiro Yamada struct s3c24xx_i2c *i2c = dev_get_drvdata(dev);
11456a6c6189SMagnus Damm
11462088716fSWolfram Sang i2c_mark_adapter_suspended(&i2c->adap);
11476a6c6189SMagnus Damm
1148a7750c3eSPankaj Dubey if (!IS_ERR(i2c->sysreg))
1149a7750c3eSPankaj Dubey regmap_read(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, &i2c->sys_i2c_cfg);
1150a7750c3eSPankaj Dubey
1151be44f01eSBen Dooks return 0;
1152be44f01eSBen Dooks }
1153be44f01eSBen Dooks
s3c24xx_i2c_resume_noirq(struct device * dev)1154b19c1959SVincent Palatin static int s3c24xx_i2c_resume_noirq(struct device *dev)
11551da177e4SLinus Torvalds {
11569242e72aSMasahiro Yamada struct s3c24xx_i2c *i2c = dev_get_drvdata(dev);
115734e81ad5SPaul Osmialowski int ret;
11581da177e4SLinus Torvalds
1159a7750c3eSPankaj Dubey if (!IS_ERR(i2c->sysreg))
1160a7750c3eSPankaj Dubey regmap_write(i2c->sysreg, EXYNOS5_SYS_I2C_CFG, i2c->sys_i2c_cfg);
1161a7750c3eSPankaj Dubey
116234e81ad5SPaul Osmialowski ret = clk_enable(i2c->clk);
116334e81ad5SPaul Osmialowski if (ret)
116434e81ad5SPaul Osmialowski return ret;
11651da177e4SLinus Torvalds s3c24xx_i2c_init(i2c);
116634e81ad5SPaul Osmialowski clk_disable(i2c->clk);
11672088716fSWolfram Sang i2c_mark_adapter_resumed(&i2c->adap);
11681da177e4SLinus Torvalds
11691da177e4SLinus Torvalds return 0;
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds
117247145210SAlexey Dobriyan static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = {
117367cd4351SPaul Cercueil NOIRQ_SYSTEM_SLEEP_PM_OPS(s3c24xx_i2c_suspend_noirq,
1174f6903783SAxel Lin s3c24xx_i2c_resume_noirq)
11756a6c6189SMagnus Damm };
11766a6c6189SMagnus Damm
11777d85ccd8SBen Dooks static struct platform_driver s3c24xx_i2c_driver = {
11781da177e4SLinus Torvalds .probe = s3c24xx_i2c_probe,
1179e190a0c3SUwe Kleine-König .remove_new = s3c24xx_i2c_remove,
11807d85ccd8SBen Dooks .id_table = s3c24xx_driver_ids,
11813ae5eaecSRussell King .driver = {
11827d85ccd8SBen Dooks .name = "s3c-i2c",
118367cd4351SPaul Cercueil .pm = pm_sleep_ptr(&s3c24xx_i2c_dev_pm_ops),
11849df7eadfSKarol Lewandowski .of_match_table = of_match_ptr(s3c24xx_i2c_match),
11853ae5eaecSRussell King },
11861da177e4SLinus Torvalds };
11871da177e4SLinus Torvalds
i2c_adap_s3c_init(void)11881da177e4SLinus Torvalds static int __init i2c_adap_s3c_init(void)
11891da177e4SLinus Torvalds {
11907d85ccd8SBen Dooks return platform_driver_register(&s3c24xx_i2c_driver);
11911da177e4SLinus Torvalds }
119218dc83a6SMark Brown subsys_initcall(i2c_adap_s3c_init);
11931da177e4SLinus Torvalds
i2c_adap_s3c_exit(void)11941da177e4SLinus Torvalds static void __exit i2c_adap_s3c_exit(void)
11951da177e4SLinus Torvalds {
11967d85ccd8SBen Dooks platform_driver_unregister(&s3c24xx_i2c_driver);
11971da177e4SLinus Torvalds }
11981da177e4SLinus Torvalds module_exit(i2c_adap_s3c_exit);
11991da177e4SLinus Torvalds
12001da177e4SLinus Torvalds MODULE_DESCRIPTION("S3C24XX I2C Bus driver");
1201d790eeb3SJean Delvare MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
12021da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1203