Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* linux/drivers/i2c/busses/i2c-s3c2410.c |
| 2 | * |
Daniel Silverstone | c564e6a | 2009-03-13 13:53:46 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004,2005,2009 Simtec Electronics |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Ben Dooks <ben@simtec.co.uk> |
| 5 | * |
| 6 | * S3C2410 I2C Controller |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | |
| 26 | #include <linux/i2c.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/init.h> |
| 28 | #include <linux/time.h> |
| 29 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/delay.h> |
| 31 | #include <linux/errno.h> |
| 32 | #include <linux/err.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 33 | #include <linux/platform_device.h> |
Mark Brown | c62c3ca | 2012-01-21 13:28:47 +0000 | [diff] [blame] | 34 | #include <linux/pm_runtime.h> |
Russell King | f8ce254 | 2006-01-07 16:15:52 +0000 | [diff] [blame] | 35 | #include <linux/clk.h> |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 36 | #include <linux/cpufreq.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
H Hartley Sweeten | 2178218 | 2010-05-21 18:41:01 +0200 | [diff] [blame] | 38 | #include <linux/io.h> |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 39 | #include <linux/of_i2c.h> |
| 40 | #include <linux/of_gpio.h> |
Tomasz Figa | 2693ac6 | 2012-11-13 11:33:40 +0100 | [diff] [blame] | 41 | #include <linux/pinctrl/consumer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Arnd Bergmann | 436d42c | 2012-08-24 15:22:12 +0200 | [diff] [blame] | 45 | #include <linux/platform_data/i2c-s3c2410.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Heiko Stübner | e636602 | 2013-03-21 04:09:25 +0000 | [diff] [blame^] | 47 | /* see s3c2410x user guide, v1.1, section 9 (p447) for more info */ |
| 48 | |
| 49 | #define S3C2410_IICREG(x) (x) |
| 50 | |
| 51 | #define S3C2410_IICCON S3C2410_IICREG(0x00) |
| 52 | #define S3C2410_IICSTAT S3C2410_IICREG(0x04) |
| 53 | #define S3C2410_IICADD S3C2410_IICREG(0x08) |
| 54 | #define S3C2410_IICDS S3C2410_IICREG(0x0C) |
| 55 | #define S3C2440_IICLC S3C2410_IICREG(0x10) |
| 56 | |
| 57 | #define S3C2410_IICCON_ACKEN (1<<7) |
| 58 | #define S3C2410_IICCON_TXDIV_16 (0<<6) |
| 59 | #define S3C2410_IICCON_TXDIV_512 (1<<6) |
| 60 | #define S3C2410_IICCON_IRQEN (1<<5) |
| 61 | #define S3C2410_IICCON_IRQPEND (1<<4) |
| 62 | #define S3C2410_IICCON_SCALE(x) ((x)&15) |
| 63 | #define S3C2410_IICCON_SCALEMASK (0xf) |
| 64 | |
| 65 | #define S3C2410_IICSTAT_MASTER_RX (2<<6) |
| 66 | #define S3C2410_IICSTAT_MASTER_TX (3<<6) |
| 67 | #define S3C2410_IICSTAT_SLAVE_RX (0<<6) |
| 68 | #define S3C2410_IICSTAT_SLAVE_TX (1<<6) |
| 69 | #define S3C2410_IICSTAT_MODEMASK (3<<6) |
| 70 | |
| 71 | #define S3C2410_IICSTAT_START (1<<5) |
| 72 | #define S3C2410_IICSTAT_BUSBUSY (1<<5) |
| 73 | #define S3C2410_IICSTAT_TXRXEN (1<<4) |
| 74 | #define S3C2410_IICSTAT_ARBITR (1<<3) |
| 75 | #define S3C2410_IICSTAT_ASSLAVE (1<<2) |
| 76 | #define S3C2410_IICSTAT_ADDR0 (1<<1) |
| 77 | #define S3C2410_IICSTAT_LASTBIT (1<<0) |
| 78 | |
| 79 | #define S3C2410_IICLC_SDA_DELAY0 (0 << 0) |
| 80 | #define S3C2410_IICLC_SDA_DELAY5 (1 << 0) |
| 81 | #define S3C2410_IICLC_SDA_DELAY10 (2 << 0) |
| 82 | #define S3C2410_IICLC_SDA_DELAY15 (3 << 0) |
| 83 | #define S3C2410_IICLC_SDA_DELAY_MASK (3 << 0) |
| 84 | |
| 85 | #define S3C2410_IICLC_FILTER_ON (1<<2) |
| 86 | |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 87 | /* Treat S3C2410 as baseline hardware, anything else is supported via quirks */ |
| 88 | #define QUIRK_S3C2440 (1 << 0) |
Karol Lewandowski | ec39ef8 | 2012-04-23 18:24:01 +0200 | [diff] [blame] | 89 | #define QUIRK_HDMIPHY (1 << 1) |
| 90 | #define QUIRK_NO_GPIO (1 << 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Daniel Kurtz | fe724bf | 2012-11-15 17:43:32 +0530 | [diff] [blame] | 92 | /* Max time to wait for bus to become idle after a xfer (in us) */ |
| 93 | #define S3C2410_IDLE_TIMEOUT 5000 |
| 94 | |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 95 | /* i2c controller state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | enum s3c24xx_i2c_state { |
| 97 | STATE_IDLE, |
| 98 | STATE_START, |
| 99 | STATE_READ, |
| 100 | STATE_WRITE, |
| 101 | STATE_STOP |
| 102 | }; |
| 103 | |
| 104 | struct s3c24xx_i2c { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | wait_queue_head_t wait; |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 106 | unsigned int quirks; |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 107 | unsigned int suspended:1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | struct i2c_msg *msg; |
| 110 | unsigned int msg_num; |
| 111 | unsigned int msg_idx; |
| 112 | unsigned int msg_ptr; |
| 113 | |
Ben Dooks | e00a8cd | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 114 | unsigned int tx_setup; |
Ben Dooks | e0d1ec9 | 2008-10-31 16:10:30 +0000 | [diff] [blame] | 115 | unsigned int irq; |
Ben Dooks | e00a8cd | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | enum s3c24xx_i2c_state state; |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 118 | unsigned long clkrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | void __iomem *regs; |
| 121 | struct clk *clk; |
| 122 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | struct i2c_adapter adap; |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 124 | |
Thomas Abraham | 4fd81eb | 2011-09-13 09:46:04 +0530 | [diff] [blame] | 125 | struct s3c2410_platform_i2c *pdata; |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 126 | int gpios[2]; |
Tomasz Figa | 2693ac6 | 2012-11-13 11:33:40 +0100 | [diff] [blame] | 127 | struct pinctrl *pctrl; |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 128 | #ifdef CONFIG_CPU_FREQ |
| 129 | struct notifier_block freq_transition; |
| 130 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | }; |
| 132 | |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 133 | static struct platform_device_id s3c24xx_driver_ids[] = { |
| 134 | { |
| 135 | .name = "s3c2410-i2c", |
| 136 | .driver_data = 0, |
| 137 | }, { |
| 138 | .name = "s3c2440-i2c", |
| 139 | .driver_data = QUIRK_S3C2440, |
Karol Lewandowski | ec39ef8 | 2012-04-23 18:24:01 +0200 | [diff] [blame] | 140 | }, { |
| 141 | .name = "s3c2440-hdmiphy-i2c", |
| 142 | .driver_data = QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO, |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 143 | }, { }, |
| 144 | }; |
| 145 | MODULE_DEVICE_TABLE(platform, s3c24xx_driver_ids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 147 | #ifdef CONFIG_OF |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 148 | static const struct of_device_id s3c24xx_i2c_match[] = { |
| 149 | { .compatible = "samsung,s3c2410-i2c", .data = (void *)0 }, |
| 150 | { .compatible = "samsung,s3c2440-i2c", .data = (void *)QUIRK_S3C2440 }, |
Karol Lewandowski | ec39ef8 | 2012-04-23 18:24:01 +0200 | [diff] [blame] | 151 | { .compatible = "samsung,s3c2440-hdmiphy-i2c", |
| 152 | .data = (void *)(QUIRK_S3C2440 | QUIRK_HDMIPHY | QUIRK_NO_GPIO) }, |
Giridhar Maruthy | faf93ff | 2013-01-24 11:27:51 -0800 | [diff] [blame] | 153 | { .compatible = "samsung,exynos5440-i2c", |
| 154 | .data = (void *)(QUIRK_S3C2440 | QUIRK_NO_GPIO) }, |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 155 | {}, |
| 156 | }; |
| 157 | MODULE_DEVICE_TABLE(of, s3c24xx_i2c_match); |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 158 | #endif |
| 159 | |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 160 | /* s3c24xx_get_device_quirks |
| 161 | * |
| 162 | * Get controller type either from device tree or platform device variant. |
| 163 | */ |
| 164 | |
| 165 | static inline unsigned int s3c24xx_get_device_quirks(struct platform_device *pdev) |
| 166 | { |
| 167 | if (pdev->dev.of_node) { |
| 168 | const struct of_device_id *match; |
Karol Lewandowski | b900ba4 | 2012-05-30 11:43:05 +0200 | [diff] [blame] | 169 | match = of_match_node(s3c24xx_i2c_match, pdev->dev.of_node); |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 170 | return (unsigned int)match->data; |
| 171 | } |
| 172 | |
| 173 | return platform_get_device_id(pdev)->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | /* s3c24xx_i2c_master_complete |
| 177 | * |
| 178 | * complete the message and wake up the caller, using the given return code, |
| 179 | * or zero to mean ok. |
| 180 | */ |
| 181 | |
| 182 | static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret) |
| 183 | { |
| 184 | dev_dbg(i2c->dev, "master_complete %d\n", ret); |
| 185 | |
| 186 | i2c->msg_ptr = 0; |
| 187 | i2c->msg = NULL; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 188 | i2c->msg_idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | i2c->msg_num = 0; |
| 190 | if (ret) |
| 191 | i2c->msg_idx = ret; |
| 192 | |
| 193 | wake_up(&i2c->wait); |
| 194 | } |
| 195 | |
| 196 | static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c) |
| 197 | { |
| 198 | unsigned long tmp; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | tmp = readl(i2c->regs + S3C2410_IICCON); |
| 201 | writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c) |
| 205 | { |
| 206 | unsigned long tmp; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | tmp = readl(i2c->regs + S3C2410_IICCON); |
| 209 | writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* irq enable/disable functions */ |
| 213 | |
| 214 | static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c) |
| 215 | { |
| 216 | unsigned long tmp; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | tmp = readl(i2c->regs + S3C2410_IICCON); |
| 219 | writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); |
| 220 | } |
| 221 | |
| 222 | static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c) |
| 223 | { |
| 224 | unsigned long tmp; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | tmp = readl(i2c->regs + S3C2410_IICCON); |
| 227 | writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /* s3c24xx_i2c_message_start |
| 232 | * |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 233 | * put the start of a message onto the bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | */ |
| 235 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 236 | static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | struct i2c_msg *msg) |
| 238 | { |
| 239 | unsigned int addr = (msg->addr & 0x7f) << 1; |
| 240 | unsigned long stat; |
| 241 | unsigned long iiccon; |
| 242 | |
| 243 | stat = 0; |
| 244 | stat |= S3C2410_IICSTAT_TXRXEN; |
| 245 | |
| 246 | if (msg->flags & I2C_M_RD) { |
| 247 | stat |= S3C2410_IICSTAT_MASTER_RX; |
| 248 | addr |= 1; |
| 249 | } else |
| 250 | stat |= S3C2410_IICSTAT_MASTER_TX; |
| 251 | |
| 252 | if (msg->flags & I2C_M_REV_DIR_ADDR) |
| 253 | addr ^= 1; |
| 254 | |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 255 | /* todo - check for whether ack wanted or not */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | s3c24xx_i2c_enable_ack(i2c); |
| 257 | |
| 258 | iiccon = readl(i2c->regs + S3C2410_IICCON); |
| 259 | writel(stat, i2c->regs + S3C2410_IICSTAT); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr); |
| 262 | writeb(addr, i2c->regs + S3C2410_IICDS); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 263 | |
Ben Dooks | e00a8cd | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 264 | /* delay here to ensure the data byte has gotten onto the bus |
| 265 | * before the transaction is started */ |
| 266 | |
| 267 | ndelay(i2c->tx_setup); |
| 268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon); |
| 270 | writel(iiccon, i2c->regs + S3C2410_IICCON); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 271 | |
| 272 | stat |= S3C2410_IICSTAT_START; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | writel(stat, i2c->regs + S3C2410_IICSTAT); |
| 274 | } |
| 275 | |
| 276 | static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret) |
| 277 | { |
| 278 | unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT); |
| 279 | |
| 280 | dev_dbg(i2c->dev, "STOP\n"); |
| 281 | |
Daniel Kurtz | 0da2e77 | 2012-11-15 17:43:31 +0530 | [diff] [blame] | 282 | /* |
| 283 | * The datasheet says that the STOP sequence should be: |
| 284 | * 1) I2CSTAT.5 = 0 - Clear BUSY (or 'generate STOP') |
| 285 | * 2) I2CCON.4 = 0 - Clear IRQPEND |
| 286 | * 3) Wait until the stop condition takes effect. |
| 287 | * 4*) I2CSTAT.4 = 0 - Clear TXRXEN |
| 288 | * |
| 289 | * Where, step "4*" is only for buses with the "HDMIPHY" quirk. |
| 290 | * |
| 291 | * However, after much experimentation, it appears that: |
| 292 | * a) normal buses automatically clear BUSY and transition from |
| 293 | * Master->Slave when they complete generating a STOP condition. |
| 294 | * Therefore, step (3) can be done in doxfer() by polling I2CCON.4 |
| 295 | * after starting the STOP generation here. |
| 296 | * b) HDMIPHY bus does neither, so there is no way to do step 3. |
| 297 | * There is no indication when this bus has finished generating |
| 298 | * STOP. |
| 299 | * |
| 300 | * In fact, we have found that as soon as the IRQPEND bit is cleared in |
| 301 | * step 2, the HDMIPHY bus generates the STOP condition, and then |
| 302 | * immediately starts transferring another data byte, even though the |
| 303 | * bus is supposedly stopped. This is presumably because the bus is |
| 304 | * still in "Master" mode, and its BUSY bit is still set. |
| 305 | * |
| 306 | * To avoid these extra post-STOP transactions on HDMI phy devices, we |
| 307 | * just disable Serial Output on the bus (I2CSTAT.4 = 0) directly, |
| 308 | * instead of first generating a proper STOP condition. This should |
| 309 | * float SDA & SCK terminating the transfer. Subsequent transfers |
| 310 | * start with a proper START condition, and proceed normally. |
| 311 | * |
| 312 | * The HDMIPHY bus is an internal bus that always has exactly two |
| 313 | * devices, the host as Master and the HDMIPHY device as the slave. |
| 314 | * Skipping the STOP condition has been tested on this bus and works. |
| 315 | */ |
| 316 | if (i2c->quirks & QUIRK_HDMIPHY) { |
| 317 | /* Stop driving the I2C pins */ |
| 318 | iicstat &= ~S3C2410_IICSTAT_TXRXEN; |
| 319 | } else { |
| 320 | /* stop the transfer */ |
| 321 | iicstat &= ~S3C2410_IICSTAT_START; |
| 322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | writel(iicstat, i2c->regs + S3C2410_IICSTAT); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | i2c->state = STATE_STOP; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | s3c24xx_i2c_master_complete(i2c, ret); |
| 328 | s3c24xx_i2c_disable_irq(i2c); |
| 329 | } |
| 330 | |
| 331 | /* helper functions to determine the current state in the set of |
| 332 | * messages we are sending */ |
| 333 | |
| 334 | /* is_lastmsg() |
| 335 | * |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 336 | * returns TRUE if the current message is the last in the set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | */ |
| 338 | |
| 339 | static inline int is_lastmsg(struct s3c24xx_i2c *i2c) |
| 340 | { |
| 341 | return i2c->msg_idx >= (i2c->msg_num - 1); |
| 342 | } |
| 343 | |
| 344 | /* is_msglast |
| 345 | * |
| 346 | * returns TRUE if we this is the last byte in the current message |
| 347 | */ |
| 348 | |
| 349 | static inline int is_msglast(struct s3c24xx_i2c *i2c) |
| 350 | { |
| 351 | return i2c->msg_ptr == i2c->msg->len-1; |
| 352 | } |
| 353 | |
| 354 | /* is_msgend |
| 355 | * |
| 356 | * returns TRUE if we reached the end of the current message |
| 357 | */ |
| 358 | |
| 359 | static inline int is_msgend(struct s3c24xx_i2c *i2c) |
| 360 | { |
| 361 | return i2c->msg_ptr >= i2c->msg->len; |
| 362 | } |
| 363 | |
Huisung Kang | 1982051 | 2011-06-23 21:37:33 +0900 | [diff] [blame] | 364 | /* i2c_s3c_irq_nextbyte |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * |
| 366 | * process an interrupt and work out what to do |
| 367 | */ |
| 368 | |
Huisung Kang | 1982051 | 2011-06-23 21:37:33 +0900 | [diff] [blame] | 369 | static int i2c_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | unsigned long tmp; |
| 372 | unsigned char byte; |
| 373 | int ret = 0; |
| 374 | |
| 375 | switch (i2c->state) { |
| 376 | |
| 377 | case STATE_IDLE: |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 378 | dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
| 381 | case STATE_STOP: |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 382 | dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 383 | s3c24xx_i2c_disable_irq(i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | goto out_ack; |
| 385 | |
| 386 | case STATE_START: |
| 387 | /* last thing we did was send a start condition on the |
| 388 | * bus, or started a new i2c message |
| 389 | */ |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 390 | |
Ben Dooks | 63f5c28 | 2008-07-01 11:59:42 +0100 | [diff] [blame] | 391 | if (iicstat & S3C2410_IICSTAT_LASTBIT && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | !(i2c->msg->flags & I2C_M_IGNORE_NAK)) { |
| 393 | /* ack was not received... */ |
| 394 | |
| 395 | dev_dbg(i2c->dev, "ack was not received\n"); |
Ben Dooks | 63f5c28 | 2008-07-01 11:59:42 +0100 | [diff] [blame] | 396 | s3c24xx_i2c_stop(i2c, -ENXIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | goto out_ack; |
| 398 | } |
| 399 | |
| 400 | if (i2c->msg->flags & I2C_M_RD) |
| 401 | i2c->state = STATE_READ; |
| 402 | else |
| 403 | i2c->state = STATE_WRITE; |
| 404 | |
| 405 | /* terminate the transfer if there is nothing to do |
Ben Dooks | 63f5c28 | 2008-07-01 11:59:42 +0100 | [diff] [blame] | 406 | * as this is used by the i2c probe to find devices. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
| 408 | if (is_lastmsg(i2c) && i2c->msg->len == 0) { |
| 409 | s3c24xx_i2c_stop(i2c, 0); |
| 410 | goto out_ack; |
| 411 | } |
| 412 | |
| 413 | if (i2c->state == STATE_READ) |
| 414 | goto prepare_read; |
| 415 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 416 | /* fall through to the write state, as we will need to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | * send a byte as well */ |
| 418 | |
| 419 | case STATE_WRITE: |
| 420 | /* we are writing data to the device... check for the |
| 421 | * end of the message, and if so, work out what to do |
| 422 | */ |
| 423 | |
Ben Dooks | 2709781 | 2008-07-01 11:59:41 +0100 | [diff] [blame] | 424 | if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) { |
| 425 | if (iicstat & S3C2410_IICSTAT_LASTBIT) { |
| 426 | dev_dbg(i2c->dev, "WRITE: No Ack\n"); |
| 427 | |
| 428 | s3c24xx_i2c_stop(i2c, -ECONNREFUSED); |
| 429 | goto out_ack; |
| 430 | } |
| 431 | } |
| 432 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 433 | retry_write: |
Ben Dooks | 2709781 | 2008-07-01 11:59:41 +0100 | [diff] [blame] | 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (!is_msgend(i2c)) { |
| 436 | byte = i2c->msg->buf[i2c->msg_ptr++]; |
| 437 | writeb(byte, i2c->regs + S3C2410_IICDS); |
Ben Dooks | e00a8cd | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 438 | |
| 439 | /* delay after writing the byte to allow the |
| 440 | * data setup time on the bus, as writing the |
| 441 | * data to the register causes the first bit |
| 442 | * to appear on SDA, and SCL will change as |
| 443 | * soon as the interrupt is acknowledged */ |
| 444 | |
| 445 | ndelay(i2c->tx_setup); |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } else if (!is_lastmsg(i2c)) { |
| 448 | /* we need to go to the next i2c message */ |
| 449 | |
| 450 | dev_dbg(i2c->dev, "WRITE: Next Message\n"); |
| 451 | |
| 452 | i2c->msg_ptr = 0; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 453 | i2c->msg_idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | i2c->msg++; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | /* check to see if we need to do another message */ |
| 457 | if (i2c->msg->flags & I2C_M_NOSTART) { |
| 458 | |
| 459 | if (i2c->msg->flags & I2C_M_RD) { |
| 460 | /* cannot do this, the controller |
| 461 | * forces us to send a new START |
| 462 | * when we change direction */ |
| 463 | |
| 464 | s3c24xx_i2c_stop(i2c, -EINVAL); |
| 465 | } |
| 466 | |
| 467 | goto retry_write; |
| 468 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | /* send the new start */ |
| 470 | s3c24xx_i2c_message_start(i2c, i2c->msg); |
| 471 | i2c->state = STATE_START; |
| 472 | } |
| 473 | |
| 474 | } else { |
| 475 | /* send stop */ |
| 476 | |
| 477 | s3c24xx_i2c_stop(i2c, 0); |
| 478 | } |
| 479 | break; |
| 480 | |
| 481 | case STATE_READ: |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 482 | /* we have a byte of data in the data register, do |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 483 | * something with it, and then work out whether we are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | * going to do any more read/write |
| 485 | */ |
| 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | byte = readb(i2c->regs + S3C2410_IICDS); |
| 488 | i2c->msg->buf[i2c->msg_ptr++] = byte; |
| 489 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 490 | prepare_read: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | if (is_msglast(i2c)) { |
| 492 | /* last byte of buffer */ |
| 493 | |
| 494 | if (is_lastmsg(i2c)) |
| 495 | s3c24xx_i2c_disable_ack(i2c); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | } else if (is_msgend(i2c)) { |
| 498 | /* ok, we've read the entire buffer, see if there |
| 499 | * is anything else we need to do */ |
| 500 | |
| 501 | if (is_lastmsg(i2c)) { |
| 502 | /* last message, send stop and complete */ |
| 503 | dev_dbg(i2c->dev, "READ: Send Stop\n"); |
| 504 | |
| 505 | s3c24xx_i2c_stop(i2c, 0); |
| 506 | } else { |
| 507 | /* go to the next transfer */ |
| 508 | dev_dbg(i2c->dev, "READ: Next Transfer\n"); |
| 509 | |
| 510 | i2c->msg_ptr = 0; |
| 511 | i2c->msg_idx++; |
| 512 | i2c->msg++; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | break; |
| 517 | } |
| 518 | |
| 519 | /* acknowlegde the IRQ and get back on with the work */ |
| 520 | |
| 521 | out_ack: |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 522 | tmp = readl(i2c->regs + S3C2410_IICCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | tmp &= ~S3C2410_IICCON_IRQPEND; |
| 524 | writel(tmp, i2c->regs + S3C2410_IICCON); |
| 525 | out: |
| 526 | return ret; |
| 527 | } |
| 528 | |
| 529 | /* s3c24xx_i2c_irq |
| 530 | * |
| 531 | * top level IRQ servicing routine |
| 532 | */ |
| 533 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 534 | static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | { |
| 536 | struct s3c24xx_i2c *i2c = dev_id; |
| 537 | unsigned long status; |
| 538 | unsigned long tmp; |
| 539 | |
| 540 | status = readl(i2c->regs + S3C2410_IICSTAT); |
| 541 | |
| 542 | if (status & S3C2410_IICSTAT_ARBITR) { |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 543 | /* deal with arbitration loss */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | dev_err(i2c->dev, "deal with arbitration loss\n"); |
| 545 | } |
| 546 | |
| 547 | if (i2c->state == STATE_IDLE) { |
| 548 | dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n"); |
| 549 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 550 | tmp = readl(i2c->regs + S3C2410_IICCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | tmp &= ~S3C2410_IICCON_IRQPEND; |
| 552 | writel(tmp, i2c->regs + S3C2410_IICCON); |
| 553 | goto out; |
| 554 | } |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | /* pretty much this leaves us with the fact that we've |
| 557 | * transmitted or received whatever byte we last sent */ |
| 558 | |
Huisung Kang | 1982051 | 2011-06-23 21:37:33 +0900 | [diff] [blame] | 559 | i2c_s3c_irq_nextbyte(i2c, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
| 561 | out: |
| 562 | return IRQ_HANDLED; |
| 563 | } |
| 564 | |
| 565 | |
| 566 | /* s3c24xx_i2c_set_master |
| 567 | * |
| 568 | * get the i2c bus for a master transaction |
| 569 | */ |
| 570 | |
| 571 | static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c) |
| 572 | { |
| 573 | unsigned long iicstat; |
| 574 | int timeout = 400; |
| 575 | |
| 576 | while (timeout-- > 0) { |
| 577 | iicstat = readl(i2c->regs + S3C2410_IICSTAT); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | if (!(iicstat & S3C2410_IICSTAT_BUSBUSY)) |
| 580 | return 0; |
| 581 | |
| 582 | msleep(1); |
| 583 | } |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | return -ETIMEDOUT; |
| 586 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Daniel Kurtz | fe724bf | 2012-11-15 17:43:32 +0530 | [diff] [blame] | 588 | /* s3c24xx_i2c_wait_idle |
| 589 | * |
| 590 | * wait for the i2c bus to become idle. |
| 591 | */ |
| 592 | |
| 593 | static void s3c24xx_i2c_wait_idle(struct s3c24xx_i2c *i2c) |
| 594 | { |
| 595 | unsigned long iicstat; |
| 596 | ktime_t start, now; |
| 597 | unsigned long delay; |
Mark Brown | 31f313d | 2012-11-21 13:12:11 +0900 | [diff] [blame] | 598 | int spins; |
Daniel Kurtz | fe724bf | 2012-11-15 17:43:32 +0530 | [diff] [blame] | 599 | |
| 600 | /* ensure the stop has been through the bus */ |
| 601 | |
| 602 | dev_dbg(i2c->dev, "waiting for bus idle\n"); |
| 603 | |
| 604 | start = now = ktime_get(); |
| 605 | |
| 606 | /* |
| 607 | * Most of the time, the bus is already idle within a few usec of the |
| 608 | * end of a transaction. However, really slow i2c devices can stretch |
| 609 | * the clock, delaying STOP generation. |
| 610 | * |
Mark Brown | 31f313d | 2012-11-21 13:12:11 +0900 | [diff] [blame] | 611 | * On slower SoCs this typically happens within a very small number of |
| 612 | * instructions so busy wait briefly to avoid scheduling overhead. |
Daniel Kurtz | fe724bf | 2012-11-15 17:43:32 +0530 | [diff] [blame] | 613 | */ |
Mark Brown | 31f313d | 2012-11-21 13:12:11 +0900 | [diff] [blame] | 614 | spins = 3; |
Daniel Kurtz | fe724bf | 2012-11-15 17:43:32 +0530 | [diff] [blame] | 615 | iicstat = readl(i2c->regs + S3C2410_IICSTAT); |
Mark Brown | 31f313d | 2012-11-21 13:12:11 +0900 | [diff] [blame] | 616 | while ((iicstat & S3C2410_IICSTAT_START) && --spins) { |
| 617 | cpu_relax(); |
| 618 | iicstat = readl(i2c->regs + S3C2410_IICSTAT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Mark Brown | 31f313d | 2012-11-21 13:12:11 +0900 | [diff] [blame] | 621 | /* |
| 622 | * If we do get an appreciable delay as a compromise between idle |
| 623 | * detection latency for the normal, fast case, and system load in the |
| 624 | * slow device case, use an exponential back off in the polling loop, |
| 625 | * up to 1/10th of the total timeout, then continue to poll at a |
| 626 | * constant rate up to the timeout. |
| 627 | */ |
Daniel Kurtz | fe724bf | 2012-11-15 17:43:32 +0530 | [diff] [blame] | 628 | delay = 1; |
| 629 | while ((iicstat & S3C2410_IICSTAT_START) && |
| 630 | ktime_us_delta(now, start) < S3C2410_IDLE_TIMEOUT) { |
| 631 | usleep_range(delay, 2 * delay); |
| 632 | if (delay < S3C2410_IDLE_TIMEOUT / 10) |
| 633 | delay <<= 1; |
| 634 | now = ktime_get(); |
| 635 | iicstat = readl(i2c->regs + S3C2410_IICSTAT); |
| 636 | } |
| 637 | |
| 638 | if (iicstat & S3C2410_IICSTAT_START) |
| 639 | dev_warn(i2c->dev, "timeout waiting for bus idle\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* s3c24xx_i2c_doxfer |
| 643 | * |
| 644 | * this starts an i2c transfer |
| 645 | */ |
| 646 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 647 | static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, |
| 648 | struct i2c_msg *msgs, int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | { |
Daniel Kurtz | fe724bf | 2012-11-15 17:43:32 +0530 | [diff] [blame] | 650 | unsigned long timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | int ret; |
| 652 | |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 653 | if (i2c->suspended) |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 654 | return -EIO; |
| 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | ret = s3c24xx_i2c_set_master(i2c); |
| 657 | if (ret != 0) { |
| 658 | dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); |
| 659 | ret = -EAGAIN; |
| 660 | goto out; |
| 661 | } |
| 662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | i2c->msg = msgs; |
| 664 | i2c->msg_num = num; |
| 665 | i2c->msg_ptr = 0; |
| 666 | i2c->msg_idx = 0; |
| 667 | i2c->state = STATE_START; |
| 668 | |
| 669 | s3c24xx_i2c_enable_irq(i2c); |
| 670 | s3c24xx_i2c_message_start(i2c, msgs); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); |
| 673 | |
| 674 | ret = i2c->msg_idx; |
| 675 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 676 | /* having these next two as dev_err() makes life very |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | * noisy when doing an i2cdetect */ |
| 678 | |
| 679 | if (timeout == 0) |
| 680 | dev_dbg(i2c->dev, "timeout\n"); |
| 681 | else if (ret != num) |
| 682 | dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); |
| 683 | |
Daniel Kurtz | 0da2e77 | 2012-11-15 17:43:31 +0530 | [diff] [blame] | 684 | /* For QUIRK_HDMIPHY, bus is already disabled */ |
| 685 | if (i2c->quirks & QUIRK_HDMIPHY) |
| 686 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Daniel Kurtz | fe724bf | 2012-11-15 17:43:32 +0530 | [diff] [blame] | 688 | s3c24xx_i2c_wait_idle(i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
| 690 | out: |
| 691 | return ret; |
| 692 | } |
| 693 | |
| 694 | /* s3c24xx_i2c_xfer |
| 695 | * |
| 696 | * first port of call from the i2c bus code when an message needs |
Steven Cole | 44bbe87 | 2005-05-03 18:21:25 -0600 | [diff] [blame] | 697 | * transferring across the i2c bus. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | */ |
| 699 | |
| 700 | static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, |
| 701 | struct i2c_msg *msgs, int num) |
| 702 | { |
| 703 | struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; |
| 704 | int retry; |
| 705 | int ret; |
| 706 | |
Mark Brown | c62c3ca | 2012-01-21 13:28:47 +0000 | [diff] [blame] | 707 | pm_runtime_get_sync(&adap->dev); |
Thomas Abraham | d3b64c5 | 2012-10-03 08:26:39 +0900 | [diff] [blame] | 708 | clk_prepare_enable(i2c->clk); |
Arnaud Patard (Rtp) | d2360b8 | 2010-07-15 15:06:14 +0200 | [diff] [blame] | 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | for (retry = 0; retry < adap->retries; retry++) { |
| 711 | |
| 712 | ret = s3c24xx_i2c_doxfer(i2c, msgs, num); |
| 713 | |
Arnaud Patard (Rtp) | d2360b8 | 2010-07-15 15:06:14 +0200 | [diff] [blame] | 714 | if (ret != -EAGAIN) { |
Thomas Abraham | d3b64c5 | 2012-10-03 08:26:39 +0900 | [diff] [blame] | 715 | clk_disable_unprepare(i2c->clk); |
Mark Brown | a86ae9f | 2012-06-28 14:08:26 +0100 | [diff] [blame] | 716 | pm_runtime_put(&adap->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | return ret; |
Arnaud Patard (Rtp) | d2360b8 | 2010-07-15 15:06:14 +0200 | [diff] [blame] | 718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
| 720 | dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); |
| 721 | |
| 722 | udelay(100); |
| 723 | } |
| 724 | |
Thomas Abraham | d3b64c5 | 2012-10-03 08:26:39 +0900 | [diff] [blame] | 725 | clk_disable_unprepare(i2c->clk); |
Mark Brown | a86ae9f | 2012-06-28 14:08:26 +0100 | [diff] [blame] | 726 | pm_runtime_put(&adap->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | return -EREMOTEIO; |
| 728 | } |
| 729 | |
| 730 | /* declare our i2c functionality */ |
| 731 | static u32 s3c24xx_i2c_func(struct i2c_adapter *adap) |
| 732 | { |
Mark Brown | 14674e7 | 2012-05-30 10:55:34 +0200 | [diff] [blame] | 733 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_NOSTART | |
| 734 | I2C_FUNC_PROTOCOL_MANGLING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* i2c bus registration info */ |
| 738 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 739 | static const struct i2c_algorithm s3c24xx_i2c_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | .master_xfer = s3c24xx_i2c_xfer, |
| 741 | .functionality = s3c24xx_i2c_func, |
| 742 | }; |
| 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | /* s3c24xx_i2c_calcdivisor |
| 745 | * |
| 746 | * return the divisor settings for a given frequency |
| 747 | */ |
| 748 | |
| 749 | static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted, |
| 750 | unsigned int *div1, unsigned int *divs) |
| 751 | { |
| 752 | unsigned int calc_divs = clkin / wanted; |
| 753 | unsigned int calc_div1; |
| 754 | |
| 755 | if (calc_divs > (16*16)) |
| 756 | calc_div1 = 512; |
| 757 | else |
| 758 | calc_div1 = 16; |
| 759 | |
| 760 | calc_divs += calc_div1-1; |
| 761 | calc_divs /= calc_div1; |
| 762 | |
| 763 | if (calc_divs == 0) |
| 764 | calc_divs = 1; |
| 765 | if (calc_divs > 17) |
| 766 | calc_divs = 17; |
| 767 | |
| 768 | *divs = calc_divs; |
| 769 | *div1 = calc_div1; |
| 770 | |
| 771 | return clkin / (calc_divs * calc_div1); |
| 772 | } |
| 773 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 774 | /* s3c24xx_i2c_clockrate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | * |
| 776 | * work out a divisor for the user requested frequency setting, |
| 777 | * either by the requested frequency, or scanning the acceptable |
| 778 | * range of frequencies until something is found |
| 779 | */ |
| 780 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 781 | static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | { |
Thomas Abraham | 4fd81eb | 2011-09-13 09:46:04 +0530 | [diff] [blame] | 783 | struct s3c2410_platform_i2c *pdata = i2c->pdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | unsigned long clkin = clk_get_rate(i2c->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | unsigned int divs, div1; |
Daniel Silverstone | c564e6a | 2009-03-13 13:53:46 +0000 | [diff] [blame] | 786 | unsigned long target_frequency; |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 787 | u32 iiccon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | int freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 790 | i2c->clkrate = clkin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | clkin /= 1000; /* clkin now in KHz */ |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 792 | |
Daniel Silverstone | c564e6a | 2009-03-13 13:53:46 +0000 | [diff] [blame] | 793 | dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
Daniel Silverstone | c564e6a | 2009-03-13 13:53:46 +0000 | [diff] [blame] | 795 | target_frequency = pdata->frequency ? pdata->frequency : 100000; |
| 796 | |
| 797 | target_frequency /= 1000; /* Target frequency now in KHz */ |
| 798 | |
| 799 | freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs); |
| 800 | |
| 801 | if (freq > target_frequency) { |
| 802 | dev_err(i2c->dev, |
| 803 | "Unable to achieve desired frequency %luKHz." \ |
| 804 | " Lowest achievable %dKHz\n", target_frequency, freq); |
| 805 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | *got = freq; |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 809 | |
| 810 | iiccon = readl(i2c->regs + S3C2410_IICCON); |
| 811 | iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512); |
| 812 | iiccon |= (divs-1); |
| 813 | |
| 814 | if (div1 == 512) |
| 815 | iiccon |= S3C2410_IICCON_TXDIV_512; |
| 816 | |
| 817 | writel(iiccon, i2c->regs + S3C2410_IICCON); |
| 818 | |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 819 | if (i2c->quirks & QUIRK_S3C2440) { |
Ben Dooks | a192f71 | 2009-03-27 10:52:13 +0000 | [diff] [blame] | 820 | unsigned long sda_delay; |
| 821 | |
| 822 | if (pdata->sda_delay) { |
MyungJoo Ham | 7031307a | 2010-09-30 15:54:46 +0200 | [diff] [blame] | 823 | sda_delay = clkin * pdata->sda_delay; |
| 824 | sda_delay = DIV_ROUND_UP(sda_delay, 1000000); |
Ben Dooks | a192f71 | 2009-03-27 10:52:13 +0000 | [diff] [blame] | 825 | sda_delay = DIV_ROUND_UP(sda_delay, 5); |
| 826 | if (sda_delay > 3) |
| 827 | sda_delay = 3; |
| 828 | sda_delay |= S3C2410_IICLC_FILTER_ON; |
| 829 | } else |
| 830 | sda_delay = 0; |
| 831 | |
| 832 | dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay); |
| 833 | writel(sda_delay, i2c->regs + S3C2440_IICLC); |
| 834 | } |
| 835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | return 0; |
| 837 | } |
| 838 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 839 | #ifdef CONFIG_CPU_FREQ |
| 840 | |
| 841 | #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition) |
| 842 | |
| 843 | static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb, |
| 844 | unsigned long val, void *data) |
| 845 | { |
| 846 | struct s3c24xx_i2c *i2c = freq_to_i2c(nb); |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 847 | unsigned int got; |
| 848 | int delta_f; |
| 849 | int ret; |
| 850 | |
| 851 | delta_f = clk_get_rate(i2c->clk) - i2c->clkrate; |
| 852 | |
| 853 | /* if we're post-change and the input clock has slowed down |
| 854 | * or at pre-change and the clock is about to speed up, then |
| 855 | * adjust our clock rate. <0 is slow, >0 speedup. |
| 856 | */ |
| 857 | |
| 858 | if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) || |
| 859 | (val == CPUFREQ_PRECHANGE && delta_f > 0)) { |
Daniel Kurtz | 9bcd04b | 2012-11-15 17:43:30 +0530 | [diff] [blame] | 860 | i2c_lock_adapter(&i2c->adap); |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 861 | ret = s3c24xx_i2c_clockrate(i2c, &got); |
Daniel Kurtz | 9bcd04b | 2012-11-15 17:43:30 +0530 | [diff] [blame] | 862 | i2c_unlock_adapter(&i2c->adap); |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 863 | |
| 864 | if (ret < 0) |
| 865 | dev_err(i2c->dev, "cannot find frequency\n"); |
| 866 | else |
| 867 | dev_info(i2c->dev, "setting freq %d\n", got); |
| 868 | } |
| 869 | |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) |
| 874 | { |
| 875 | i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition; |
| 876 | |
| 877 | return cpufreq_register_notifier(&i2c->freq_transition, |
| 878 | CPUFREQ_TRANSITION_NOTIFIER); |
| 879 | } |
| 880 | |
| 881 | static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) |
| 882 | { |
| 883 | cpufreq_unregister_notifier(&i2c->freq_transition, |
| 884 | CPUFREQ_TRANSITION_NOTIFIER); |
| 885 | } |
| 886 | |
| 887 | #else |
| 888 | static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) |
| 889 | { |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) |
| 894 | { |
| 895 | } |
| 896 | #endif |
| 897 | |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 898 | #ifdef CONFIG_OF |
| 899 | static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) |
| 900 | { |
| 901 | int idx, gpio, ret; |
| 902 | |
Karol Lewandowski | ec39ef8 | 2012-04-23 18:24:01 +0200 | [diff] [blame] | 903 | if (i2c->quirks & QUIRK_NO_GPIO) |
| 904 | return 0; |
| 905 | |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 906 | for (idx = 0; idx < 2; idx++) { |
| 907 | gpio = of_get_gpio(i2c->dev->of_node, idx); |
| 908 | if (!gpio_is_valid(gpio)) { |
| 909 | dev_err(i2c->dev, "invalid gpio[%d]: %d\n", idx, gpio); |
| 910 | goto free_gpio; |
| 911 | } |
Abhilash Kesavan | 963f207 | 2012-11-19 15:48:46 +0530 | [diff] [blame] | 912 | i2c->gpios[idx] = gpio; |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 913 | |
| 914 | ret = gpio_request(gpio, "i2c-bus"); |
| 915 | if (ret) { |
| 916 | dev_err(i2c->dev, "gpio [%d] request failed\n", gpio); |
| 917 | goto free_gpio; |
| 918 | } |
| 919 | } |
| 920 | return 0; |
| 921 | |
| 922 | free_gpio: |
| 923 | while (--idx >= 0) |
| 924 | gpio_free(i2c->gpios[idx]); |
| 925 | return -EINVAL; |
| 926 | } |
| 927 | |
| 928 | static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c) |
| 929 | { |
| 930 | unsigned int idx; |
Karol Lewandowski | ec39ef8 | 2012-04-23 18:24:01 +0200 | [diff] [blame] | 931 | |
| 932 | if (i2c->quirks & QUIRK_NO_GPIO) |
| 933 | return; |
| 934 | |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 935 | for (idx = 0; idx < 2; idx++) |
| 936 | gpio_free(i2c->gpios[idx]); |
| 937 | } |
| 938 | #else |
| 939 | static int s3c24xx_i2c_parse_dt_gpio(struct s3c24xx_i2c *i2c) |
| 940 | { |
Tushar Behera | 8ebe661 | 2011-12-09 15:33:55 +0530 | [diff] [blame] | 941 | return 0; |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | static void s3c24xx_i2c_dt_gpio_free(struct s3c24xx_i2c *i2c) |
| 945 | { |
| 946 | } |
| 947 | #endif |
| 948 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | /* s3c24xx_i2c_init |
| 950 | * |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 951 | * initialise the controller, set the IO lines and frequency |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | */ |
| 953 | |
| 954 | static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) |
| 955 | { |
| 956 | unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN; |
| 957 | struct s3c2410_platform_i2c *pdata; |
| 958 | unsigned int freq; |
| 959 | |
| 960 | /* get the plafrom data */ |
| 961 | |
Thomas Abraham | 4fd81eb | 2011-09-13 09:46:04 +0530 | [diff] [blame] | 962 | pdata = i2c->pdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | /* write slave address */ |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD); |
| 967 | |
| 968 | dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); |
| 969 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 970 | writel(iicon, i2c->regs + S3C2410_IICCON); |
| 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | /* we need to work out the divisors for the clock... */ |
| 973 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 974 | if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { |
| 975 | writel(0, i2c->regs + S3C2410_IICCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | dev_err(i2c->dev, "cannot meet bus frequency required\n"); |
| 977 | return -EINVAL; |
| 978 | } |
| 979 | |
| 980 | /* todo - check that the i2c lines aren't being dragged anywhere */ |
| 981 | |
| 982 | dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); |
| 983 | dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | return 0; |
| 986 | } |
| 987 | |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 988 | #ifdef CONFIG_OF |
| 989 | /* s3c24xx_i2c_parse_dt |
| 990 | * |
| 991 | * Parse the device tree node and retreive the platform data. |
| 992 | */ |
| 993 | |
| 994 | static void |
| 995 | s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) |
| 996 | { |
| 997 | struct s3c2410_platform_i2c *pdata = i2c->pdata; |
| 998 | |
| 999 | if (!np) |
| 1000 | return; |
| 1001 | |
| 1002 | pdata->bus_num = -1; /* i2c bus number is dynamically assigned */ |
| 1003 | of_property_read_u32(np, "samsung,i2c-sda-delay", &pdata->sda_delay); |
| 1004 | of_property_read_u32(np, "samsung,i2c-slave-addr", &pdata->slave_addr); |
| 1005 | of_property_read_u32(np, "samsung,i2c-max-bus-freq", |
| 1006 | (u32 *)&pdata->frequency); |
| 1007 | } |
| 1008 | #else |
| 1009 | static void |
| 1010 | s3c24xx_i2c_parse_dt(struct device_node *np, struct s3c24xx_i2c *i2c) |
| 1011 | { |
| 1012 | return; |
| 1013 | } |
| 1014 | #endif |
| 1015 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | /* s3c24xx_i2c_probe |
| 1017 | * |
| 1018 | * called by the bus driver when a suitable device is found |
| 1019 | */ |
| 1020 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1021 | static int s3c24xx_i2c_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | { |
Ben Dooks | 692acbd | 2008-10-31 16:10:28 +0000 | [diff] [blame] | 1023 | struct s3c24xx_i2c *i2c; |
Thomas Abraham | 4fd81eb | 2011-09-13 09:46:04 +0530 | [diff] [blame] | 1024 | struct s3c2410_platform_i2c *pdata = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | struct resource *res; |
| 1026 | int ret; |
| 1027 | |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 1028 | if (!pdev->dev.of_node) { |
| 1029 | pdata = pdev->dev.platform_data; |
| 1030 | if (!pdata) { |
| 1031 | dev_err(&pdev->dev, "no platform data\n"); |
| 1032 | return -EINVAL; |
| 1033 | } |
Ben Dooks | 6a039ca | 2008-10-31 16:10:27 +0000 | [diff] [blame] | 1034 | } |
Ben Dooks | 399dee2 | 2008-07-28 12:04:06 +0100 | [diff] [blame] | 1035 | |
Mark Brown | 4ea1557 | 2012-01-21 13:28:46 +0000 | [diff] [blame] | 1036 | i2c = devm_kzalloc(&pdev->dev, sizeof(struct s3c24xx_i2c), GFP_KERNEL); |
Ben Dooks | 692acbd | 2008-10-31 16:10:28 +0000 | [diff] [blame] | 1037 | if (!i2c) { |
| 1038 | dev_err(&pdev->dev, "no memory for state\n"); |
| 1039 | return -ENOMEM; |
| 1040 | } |
| 1041 | |
Thomas Abraham | 4fd81eb | 2011-09-13 09:46:04 +0530 | [diff] [blame] | 1042 | i2c->pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 1043 | if (!i2c->pdata) { |
Tushar Behera | 669da30 | 2013-01-24 15:41:06 +0530 | [diff] [blame] | 1044 | dev_err(&pdev->dev, "no memory for platform data\n"); |
| 1045 | return -ENOMEM; |
Thomas Abraham | 4fd81eb | 2011-09-13 09:46:04 +0530 | [diff] [blame] | 1046 | } |
| 1047 | |
Karol Lewandowski | 2745249 | 2012-04-23 18:24:00 +0200 | [diff] [blame] | 1048 | i2c->quirks = s3c24xx_get_device_quirks(pdev); |
Thomas Abraham | 4fd81eb | 2011-09-13 09:46:04 +0530 | [diff] [blame] | 1049 | if (pdata) |
| 1050 | memcpy(i2c->pdata, pdata, sizeof(*pdata)); |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 1051 | else |
| 1052 | s3c24xx_i2c_parse_dt(pdev->dev.of_node, i2c); |
Thomas Abraham | 4fd81eb | 2011-09-13 09:46:04 +0530 | [diff] [blame] | 1053 | |
Ben Dooks | 692acbd | 2008-10-31 16:10:28 +0000 | [diff] [blame] | 1054 | strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); |
| 1055 | i2c->adap.owner = THIS_MODULE; |
| 1056 | i2c->adap.algo = &s3c24xx_i2c_algorithm; |
| 1057 | i2c->adap.retries = 2; |
| 1058 | i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
| 1059 | i2c->tx_setup = 50; |
| 1060 | |
Ben Dooks | 692acbd | 2008-10-31 16:10:28 +0000 | [diff] [blame] | 1061 | init_waitqueue_head(&i2c->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
| 1063 | /* find the clock and enable it */ |
| 1064 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1065 | i2c->dev = &pdev->dev; |
Tushar Behera | 2b255b9 | 2013-01-24 15:41:07 +0530 | [diff] [blame] | 1066 | i2c->clk = devm_clk_get(&pdev->dev, "i2c"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | if (IS_ERR(i2c->clk)) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1068 | dev_err(&pdev->dev, "cannot get clock\n"); |
Tushar Behera | 669da30 | 2013-01-24 15:41:06 +0530 | [diff] [blame] | 1069 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | } |
| 1071 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1072 | dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | |
| 1075 | /* map the registers */ |
| 1076 | |
| 1077 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1078 | if (res == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1079 | dev_err(&pdev->dev, "cannot find IO resource\n"); |
Tushar Behera | d16933b | 2013-01-24 15:41:08 +0530 | [diff] [blame] | 1080 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Thierry Reding | 84dbf80 | 2013-01-21 11:09:03 +0100 | [diff] [blame] | 1083 | i2c->regs = devm_ioremap_resource(&pdev->dev, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
Linus Torvalds | 52caa59 | 2013-02-26 09:41:53 -0800 | [diff] [blame] | 1085 | if (IS_ERR(i2c->regs)) |
| 1086 | return PTR_ERR(i2c->regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
Mark Brown | a72ad45 | 2012-11-05 09:33:39 +0100 | [diff] [blame] | 1088 | dev_dbg(&pdev->dev, "registers %p (%p)\n", |
| 1089 | i2c->regs, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | |
| 1091 | /* setup info block for the i2c core */ |
| 1092 | |
| 1093 | i2c->adap.algo_data = i2c; |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1094 | i2c->adap.dev.parent = &pdev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | |
Tomasz Figa | 2693ac6 | 2012-11-13 11:33:40 +0100 | [diff] [blame] | 1096 | i2c->pctrl = devm_pinctrl_get_select_default(i2c->dev); |
| 1097 | |
Abhilash Kesavan | 658122f | 2012-11-19 15:47:17 +0530 | [diff] [blame] | 1098 | /* inititalise the i2c gpio lines */ |
| 1099 | |
| 1100 | if (i2c->pdata->cfg_gpio) { |
| 1101 | i2c->pdata->cfg_gpio(to_platform_device(i2c->dev)); |
| 1102 | } else if (IS_ERR(i2c->pctrl) && s3c24xx_i2c_parse_dt_gpio(i2c)) { |
Tushar Behera | d16933b | 2013-01-24 15:41:08 +0530 | [diff] [blame] | 1103 | return -EINVAL; |
Abhilash Kesavan | 658122f | 2012-11-19 15:47:17 +0530 | [diff] [blame] | 1104 | } |
| 1105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | /* initialise the i2c controller */ |
| 1107 | |
Tushar Behera | d16933b | 2013-01-24 15:41:08 +0530 | [diff] [blame] | 1108 | clk_prepare_enable(i2c->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | ret = s3c24xx_i2c_init(i2c); |
Tushar Behera | d16933b | 2013-01-24 15:41:08 +0530 | [diff] [blame] | 1110 | clk_disable_unprepare(i2c->clk); |
| 1111 | if (ret != 0) { |
| 1112 | dev_err(&pdev->dev, "I2C controller init failed\n"); |
| 1113 | return ret; |
| 1114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | /* find the IRQ for this unit (note, this relies on the init call to |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 1116 | * ensure no current IRQs pending |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | */ |
| 1118 | |
Ben Dooks | e0d1ec9 | 2008-10-31 16:10:30 +0000 | [diff] [blame] | 1119 | i2c->irq = ret = platform_get_irq(pdev, 0); |
| 1120 | if (ret <= 0) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1121 | dev_err(&pdev->dev, "cannot find IRQ\n"); |
Tushar Behera | d16933b | 2013-01-24 15:41:08 +0530 | [diff] [blame] | 1122 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Tushar Behera | 2b255b9 | 2013-01-24 15:41:07 +0530 | [diff] [blame] | 1125 | ret = devm_request_irq(&pdev->dev, i2c->irq, s3c24xx_i2c_irq, 0, |
| 1126 | dev_name(&pdev->dev), i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
| 1128 | if (ret != 0) { |
Ben Dooks | e0d1ec9 | 2008-10-31 16:10:30 +0000 | [diff] [blame] | 1129 | dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); |
Tushar Behera | d16933b | 2013-01-24 15:41:08 +0530 | [diff] [blame] | 1130 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 1133 | ret = s3c24xx_i2c_register_cpufreq(i2c); |
| 1134 | if (ret < 0) { |
| 1135 | dev_err(&pdev->dev, "failed to register cpufreq notifier\n"); |
Tushar Behera | d16933b | 2013-01-24 15:41:08 +0530 | [diff] [blame] | 1136 | return ret; |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
Ben Dooks | 399dee2 | 2008-07-28 12:04:06 +0100 | [diff] [blame] | 1139 | /* Note, previous versions of the driver used i2c_add_adapter() |
| 1140 | * to add the bus at any number. We now pass the bus number via |
| 1141 | * the platform data, so if unset it will now default to always |
| 1142 | * being bus 0. |
| 1143 | */ |
| 1144 | |
Thomas Abraham | 4fd81eb | 2011-09-13 09:46:04 +0530 | [diff] [blame] | 1145 | i2c->adap.nr = i2c->pdata->bus_num; |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 1146 | i2c->adap.dev.of_node = pdev->dev.of_node; |
Ben Dooks | 399dee2 | 2008-07-28 12:04:06 +0100 | [diff] [blame] | 1147 | |
| 1148 | ret = i2c_add_numbered_adapter(&i2c->adap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | if (ret < 0) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1150 | dev_err(&pdev->dev, "failed to add bus to i2c core\n"); |
Tushar Behera | dc6fea4 | 2013-01-24 15:41:09 +0530 | [diff] [blame] | 1151 | s3c24xx_i2c_deregister_cpufreq(i2c); |
| 1152 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Thomas Abraham | 5a5f508 | 2011-09-13 09:46:05 +0530 | [diff] [blame] | 1155 | of_i2c_register_devices(&i2c->adap); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1156 | platform_set_drvdata(pdev, i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
Mark Brown | c62c3ca | 2012-01-21 13:28:47 +0000 | [diff] [blame] | 1158 | pm_runtime_enable(&pdev->dev); |
| 1159 | pm_runtime_enable(&i2c->adap.dev); |
| 1160 | |
Jean Delvare | 22e965c | 2009-01-07 14:29:16 +0100 | [diff] [blame] | 1161 | dev_info(&pdev->dev, "%s: S3C I2C adapter\n", dev_name(&i2c->adap.dev)); |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 1162 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | /* s3c24xx_i2c_remove |
| 1166 | * |
| 1167 | * called when device is removed from the bus |
| 1168 | */ |
| 1169 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1170 | static int s3c24xx_i2c_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1172 | struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 1173 | |
Mark Brown | c62c3ca | 2012-01-21 13:28:47 +0000 | [diff] [blame] | 1174 | pm_runtime_disable(&i2c->adap.dev); |
| 1175 | pm_runtime_disable(&pdev->dev); |
| 1176 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 1177 | s3c24xx_i2c_deregister_cpufreq(i2c); |
| 1178 | |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 1179 | i2c_del_adapter(&i2c->adap); |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 1180 | |
Thomas Abraham | d3b64c5 | 2012-10-03 08:26:39 +0900 | [diff] [blame] | 1181 | clk_disable_unprepare(i2c->clk); |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 1182 | |
Tomasz Figa | 2693ac6 | 2012-11-13 11:33:40 +0100 | [diff] [blame] | 1183 | if (pdev->dev.of_node && IS_ERR(i2c->pctrl)) |
| 1184 | s3c24xx_i2c_dt_gpio_free(i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Mark Brown | 2935e0e | 2012-11-05 09:33:38 +0100 | [diff] [blame] | 1189 | #ifdef CONFIG_PM_SLEEP |
Magnus Damm | 6a6c6189 | 2009-07-08 13:22:47 +0200 | [diff] [blame] | 1190 | static int s3c24xx_i2c_suspend_noirq(struct device *dev) |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 1191 | { |
Magnus Damm | 6a6c6189 | 2009-07-08 13:22:47 +0200 | [diff] [blame] | 1192 | struct platform_device *pdev = to_platform_device(dev); |
| 1193 | struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); |
| 1194 | |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 1195 | i2c->suspended = 1; |
Magnus Damm | 6a6c6189 | 2009-07-08 13:22:47 +0200 | [diff] [blame] | 1196 | |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 1197 | return 0; |
| 1198 | } |
| 1199 | |
Magnus Damm | 6a6c6189 | 2009-07-08 13:22:47 +0200 | [diff] [blame] | 1200 | static int s3c24xx_i2c_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | { |
Magnus Damm | 6a6c6189 | 2009-07-08 13:22:47 +0200 | [diff] [blame] | 1202 | struct platform_device *pdev = to_platform_device(dev); |
| 1203 | struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 1204 | |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 1205 | i2c->suspended = 0; |
Thomas Abraham | d3b64c5 | 2012-10-03 08:26:39 +0900 | [diff] [blame] | 1206 | clk_prepare_enable(i2c->clk); |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 1207 | s3c24xx_i2c_init(i2c); |
Thomas Abraham | d3b64c5 | 2012-10-03 08:26:39 +0900 | [diff] [blame] | 1208 | clk_disable_unprepare(i2c->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | |
| 1210 | return 0; |
| 1211 | } |
Mark Brown | 2935e0e | 2012-11-05 09:33:38 +0100 | [diff] [blame] | 1212 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
Mark Brown | 2935e0e | 2012-11-05 09:33:38 +0100 | [diff] [blame] | 1214 | #ifdef CONFIG_PM |
Alexey Dobriyan | 4714521 | 2009-12-14 18:00:08 -0800 | [diff] [blame] | 1215 | static const struct dev_pm_ops s3c24xx_i2c_dev_pm_ops = { |
Mark Brown | 2935e0e | 2012-11-05 09:33:38 +0100 | [diff] [blame] | 1216 | #ifdef CONFIG_PM_SLEEP |
Magnus Damm | 6a6c6189 | 2009-07-08 13:22:47 +0200 | [diff] [blame] | 1217 | .suspend_noirq = s3c24xx_i2c_suspend_noirq, |
| 1218 | .resume = s3c24xx_i2c_resume, |
Mark Brown | 2935e0e | 2012-11-05 09:33:38 +0100 | [diff] [blame] | 1219 | #endif |
Magnus Damm | 6a6c6189 | 2009-07-08 13:22:47 +0200 | [diff] [blame] | 1220 | }; |
| 1221 | |
| 1222 | #define S3C24XX_DEV_PM_OPS (&s3c24xx_i2c_dev_pm_ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | #else |
Magnus Damm | 6a6c6189 | 2009-07-08 13:22:47 +0200 | [diff] [blame] | 1224 | #define S3C24XX_DEV_PM_OPS NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | #endif |
| 1226 | |
| 1227 | /* device driver for platform bus bits */ |
| 1228 | |
Ben Dooks | 7d85ccd | 2009-06-12 10:45:29 +0100 | [diff] [blame] | 1229 | static struct platform_driver s3c24xx_i2c_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | .probe = s3c24xx_i2c_probe, |
| 1231 | .remove = s3c24xx_i2c_remove, |
Ben Dooks | 7d85ccd | 2009-06-12 10:45:29 +0100 | [diff] [blame] | 1232 | .id_table = s3c24xx_driver_ids, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1233 | .driver = { |
| 1234 | .owner = THIS_MODULE, |
Ben Dooks | 7d85ccd | 2009-06-12 10:45:29 +0100 | [diff] [blame] | 1235 | .name = "s3c-i2c", |
Magnus Damm | 6a6c6189 | 2009-07-08 13:22:47 +0200 | [diff] [blame] | 1236 | .pm = S3C24XX_DEV_PM_OPS, |
Karol Lewandowski | 9df7ead | 2012-03-21 20:11:51 +0100 | [diff] [blame] | 1237 | .of_match_table = of_match_ptr(s3c24xx_i2c_match), |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1238 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | }; |
| 1240 | |
| 1241 | static int __init i2c_adap_s3c_init(void) |
| 1242 | { |
Ben Dooks | 7d85ccd | 2009-06-12 10:45:29 +0100 | [diff] [blame] | 1243 | return platform_driver_register(&s3c24xx_i2c_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | } |
Mark Brown | 18dc83a | 2009-02-26 16:29:22 +0000 | [diff] [blame] | 1245 | subsys_initcall(i2c_adap_s3c_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | |
| 1247 | static void __exit i2c_adap_s3c_exit(void) |
| 1248 | { |
Ben Dooks | 7d85ccd | 2009-06-12 10:45:29 +0100 | [diff] [blame] | 1249 | platform_driver_unregister(&s3c24xx_i2c_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | module_exit(i2c_adap_s3c_exit); |
| 1252 | |
| 1253 | MODULE_DESCRIPTION("S3C24XX I2C Bus driver"); |
| 1254 | MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); |
| 1255 | MODULE_LICENSE("GPL"); |