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