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> |
| 27 | #include <linux/i2c-id.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/time.h> |
| 30 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/delay.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/err.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 34 | #include <linux/platform_device.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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/irq.h> |
| 39 | #include <asm/io.h> |
| 40 | |
Ben Dooks | 9498cb7 | 2008-10-30 10:14:33 +0000 | [diff] [blame] | 41 | #include <plat/regs-iic.h> |
| 42 | #include <plat/iic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | /* i2c controller state */ |
| 45 | |
| 46 | enum s3c24xx_i2c_state { |
| 47 | STATE_IDLE, |
| 48 | STATE_START, |
| 49 | STATE_READ, |
| 50 | STATE_WRITE, |
| 51 | STATE_STOP |
| 52 | }; |
| 53 | |
| 54 | struct s3c24xx_i2c { |
| 55 | spinlock_t lock; |
| 56 | wait_queue_head_t wait; |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 57 | unsigned int suspended:1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | struct i2c_msg *msg; |
| 60 | unsigned int msg_num; |
| 61 | unsigned int msg_idx; |
| 62 | unsigned int msg_ptr; |
| 63 | |
Ben Dooks | e00a8cd | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 64 | unsigned int tx_setup; |
Ben Dooks | e0d1ec9 | 2008-10-31 16:10:30 +0000 | [diff] [blame] | 65 | unsigned int irq; |
Ben Dooks | e00a8cd | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | enum s3c24xx_i2c_state state; |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 68 | unsigned long clkrate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | void __iomem *regs; |
| 71 | struct clk *clk; |
| 72 | struct device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | struct resource *ioarea; |
| 74 | struct i2c_adapter adap; |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 75 | |
| 76 | #ifdef CONFIG_CPU_FREQ |
| 77 | struct notifier_block freq_transition; |
| 78 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Ben Dooks | 6a039ca | 2008-10-31 16:10:27 +0000 | [diff] [blame] | 81 | /* default platform data removed, dev should always carry data. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | /* s3c24xx_i2c_is2440() |
| 84 | * |
| 85 | * return true is this is an s3c2440 |
| 86 | */ |
| 87 | |
| 88 | static inline int s3c24xx_i2c_is2440(struct s3c24xx_i2c *i2c) |
| 89 | { |
| 90 | struct platform_device *pdev = to_platform_device(i2c->dev); |
| 91 | |
| 92 | return !strcmp(pdev->name, "s3c2440-i2c"); |
| 93 | } |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | /* s3c24xx_i2c_master_complete |
| 96 | * |
| 97 | * complete the message and wake up the caller, using the given return code, |
| 98 | * or zero to mean ok. |
| 99 | */ |
| 100 | |
| 101 | static inline void s3c24xx_i2c_master_complete(struct s3c24xx_i2c *i2c, int ret) |
| 102 | { |
| 103 | dev_dbg(i2c->dev, "master_complete %d\n", ret); |
| 104 | |
| 105 | i2c->msg_ptr = 0; |
| 106 | i2c->msg = NULL; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 107 | i2c->msg_idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | i2c->msg_num = 0; |
| 109 | if (ret) |
| 110 | i2c->msg_idx = ret; |
| 111 | |
| 112 | wake_up(&i2c->wait); |
| 113 | } |
| 114 | |
| 115 | static inline void s3c24xx_i2c_disable_ack(struct s3c24xx_i2c *i2c) |
| 116 | { |
| 117 | unsigned long tmp; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | tmp = readl(i2c->regs + S3C2410_IICCON); |
| 120 | writel(tmp & ~S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static inline void s3c24xx_i2c_enable_ack(struct s3c24xx_i2c *i2c) |
| 124 | { |
| 125 | unsigned long tmp; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | tmp = readl(i2c->regs + S3C2410_IICCON); |
| 128 | writel(tmp | S3C2410_IICCON_ACKEN, i2c->regs + S3C2410_IICCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* irq enable/disable functions */ |
| 132 | |
| 133 | static inline void s3c24xx_i2c_disable_irq(struct s3c24xx_i2c *i2c) |
| 134 | { |
| 135 | unsigned long tmp; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | tmp = readl(i2c->regs + S3C2410_IICCON); |
| 138 | writel(tmp & ~S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); |
| 139 | } |
| 140 | |
| 141 | static inline void s3c24xx_i2c_enable_irq(struct s3c24xx_i2c *i2c) |
| 142 | { |
| 143 | unsigned long tmp; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | tmp = readl(i2c->regs + S3C2410_IICCON); |
| 146 | writel(tmp | S3C2410_IICCON_IRQEN, i2c->regs + S3C2410_IICCON); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /* s3c24xx_i2c_message_start |
| 151 | * |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 152 | * put the start of a message onto the bus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | */ |
| 154 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 155 | static void s3c24xx_i2c_message_start(struct s3c24xx_i2c *i2c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | struct i2c_msg *msg) |
| 157 | { |
| 158 | unsigned int addr = (msg->addr & 0x7f) << 1; |
| 159 | unsigned long stat; |
| 160 | unsigned long iiccon; |
| 161 | |
| 162 | stat = 0; |
| 163 | stat |= S3C2410_IICSTAT_TXRXEN; |
| 164 | |
| 165 | if (msg->flags & I2C_M_RD) { |
| 166 | stat |= S3C2410_IICSTAT_MASTER_RX; |
| 167 | addr |= 1; |
| 168 | } else |
| 169 | stat |= S3C2410_IICSTAT_MASTER_TX; |
| 170 | |
| 171 | if (msg->flags & I2C_M_REV_DIR_ADDR) |
| 172 | addr ^= 1; |
| 173 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 174 | /* todo - check for wether ack wanted or not */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | s3c24xx_i2c_enable_ack(i2c); |
| 176 | |
| 177 | iiccon = readl(i2c->regs + S3C2410_IICCON); |
| 178 | writel(stat, i2c->regs + S3C2410_IICSTAT); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | dev_dbg(i2c->dev, "START: %08lx to IICSTAT, %02x to DS\n", stat, addr); |
| 181 | writeb(addr, i2c->regs + S3C2410_IICDS); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 182 | |
Ben Dooks | e00a8cd | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 183 | /* delay here to ensure the data byte has gotten onto the bus |
| 184 | * before the transaction is started */ |
| 185 | |
| 186 | ndelay(i2c->tx_setup); |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | dev_dbg(i2c->dev, "iiccon, %08lx\n", iiccon); |
| 189 | writel(iiccon, i2c->regs + S3C2410_IICCON); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 190 | |
| 191 | stat |= S3C2410_IICSTAT_START; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | writel(stat, i2c->regs + S3C2410_IICSTAT); |
| 193 | } |
| 194 | |
| 195 | static inline void s3c24xx_i2c_stop(struct s3c24xx_i2c *i2c, int ret) |
| 196 | { |
| 197 | unsigned long iicstat = readl(i2c->regs + S3C2410_IICSTAT); |
| 198 | |
| 199 | dev_dbg(i2c->dev, "STOP\n"); |
| 200 | |
| 201 | /* stop the transfer */ |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 202 | iicstat &= ~S3C2410_IICSTAT_START; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | writel(iicstat, i2c->regs + S3C2410_IICSTAT); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | i2c->state = STATE_STOP; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | s3c24xx_i2c_master_complete(i2c, ret); |
| 208 | s3c24xx_i2c_disable_irq(i2c); |
| 209 | } |
| 210 | |
| 211 | /* helper functions to determine the current state in the set of |
| 212 | * messages we are sending */ |
| 213 | |
| 214 | /* is_lastmsg() |
| 215 | * |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 216 | * returns TRUE if the current message is the last in the set |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | */ |
| 218 | |
| 219 | static inline int is_lastmsg(struct s3c24xx_i2c *i2c) |
| 220 | { |
| 221 | return i2c->msg_idx >= (i2c->msg_num - 1); |
| 222 | } |
| 223 | |
| 224 | /* is_msglast |
| 225 | * |
| 226 | * returns TRUE if we this is the last byte in the current message |
| 227 | */ |
| 228 | |
| 229 | static inline int is_msglast(struct s3c24xx_i2c *i2c) |
| 230 | { |
| 231 | return i2c->msg_ptr == i2c->msg->len-1; |
| 232 | } |
| 233 | |
| 234 | /* is_msgend |
| 235 | * |
| 236 | * returns TRUE if we reached the end of the current message |
| 237 | */ |
| 238 | |
| 239 | static inline int is_msgend(struct s3c24xx_i2c *i2c) |
| 240 | { |
| 241 | return i2c->msg_ptr >= i2c->msg->len; |
| 242 | } |
| 243 | |
| 244 | /* i2s_s3c_irq_nextbyte |
| 245 | * |
| 246 | * process an interrupt and work out what to do |
| 247 | */ |
| 248 | |
| 249 | static int i2s_s3c_irq_nextbyte(struct s3c24xx_i2c *i2c, unsigned long iicstat) |
| 250 | { |
| 251 | unsigned long tmp; |
| 252 | unsigned char byte; |
| 253 | int ret = 0; |
| 254 | |
| 255 | switch (i2c->state) { |
| 256 | |
| 257 | case STATE_IDLE: |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 258 | dev_err(i2c->dev, "%s: called in STATE_IDLE\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | goto out; |
| 260 | break; |
| 261 | |
| 262 | case STATE_STOP: |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 263 | dev_err(i2c->dev, "%s: called in STATE_STOP\n", __func__); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 264 | s3c24xx_i2c_disable_irq(i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | goto out_ack; |
| 266 | |
| 267 | case STATE_START: |
| 268 | /* last thing we did was send a start condition on the |
| 269 | * bus, or started a new i2c message |
| 270 | */ |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 271 | |
Ben Dooks | 63f5c28 | 2008-07-01 11:59:42 +0100 | [diff] [blame] | 272 | if (iicstat & S3C2410_IICSTAT_LASTBIT && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | !(i2c->msg->flags & I2C_M_IGNORE_NAK)) { |
| 274 | /* ack was not received... */ |
| 275 | |
| 276 | dev_dbg(i2c->dev, "ack was not received\n"); |
Ben Dooks | 63f5c28 | 2008-07-01 11:59:42 +0100 | [diff] [blame] | 277 | s3c24xx_i2c_stop(i2c, -ENXIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | goto out_ack; |
| 279 | } |
| 280 | |
| 281 | if (i2c->msg->flags & I2C_M_RD) |
| 282 | i2c->state = STATE_READ; |
| 283 | else |
| 284 | i2c->state = STATE_WRITE; |
| 285 | |
| 286 | /* terminate the transfer if there is nothing to do |
Ben Dooks | 63f5c28 | 2008-07-01 11:59:42 +0100 | [diff] [blame] | 287 | * as this is used by the i2c probe to find devices. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | if (is_lastmsg(i2c) && i2c->msg->len == 0) { |
| 290 | s3c24xx_i2c_stop(i2c, 0); |
| 291 | goto out_ack; |
| 292 | } |
| 293 | |
| 294 | if (i2c->state == STATE_READ) |
| 295 | goto prepare_read; |
| 296 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 297 | /* fall through to the write state, as we will need to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | * send a byte as well */ |
| 299 | |
| 300 | case STATE_WRITE: |
| 301 | /* we are writing data to the device... check for the |
| 302 | * end of the message, and if so, work out what to do |
| 303 | */ |
| 304 | |
Ben Dooks | 2709781 | 2008-07-01 11:59:41 +0100 | [diff] [blame] | 305 | if (!(i2c->msg->flags & I2C_M_IGNORE_NAK)) { |
| 306 | if (iicstat & S3C2410_IICSTAT_LASTBIT) { |
| 307 | dev_dbg(i2c->dev, "WRITE: No Ack\n"); |
| 308 | |
| 309 | s3c24xx_i2c_stop(i2c, -ECONNREFUSED); |
| 310 | goto out_ack; |
| 311 | } |
| 312 | } |
| 313 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 314 | retry_write: |
Ben Dooks | 2709781 | 2008-07-01 11:59:41 +0100 | [diff] [blame] | 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if (!is_msgend(i2c)) { |
| 317 | byte = i2c->msg->buf[i2c->msg_ptr++]; |
| 318 | writeb(byte, i2c->regs + S3C2410_IICDS); |
Ben Dooks | e00a8cd | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 319 | |
| 320 | /* delay after writing the byte to allow the |
| 321 | * data setup time on the bus, as writing the |
| 322 | * data to the register causes the first bit |
| 323 | * to appear on SDA, and SCL will change as |
| 324 | * soon as the interrupt is acknowledged */ |
| 325 | |
| 326 | ndelay(i2c->tx_setup); |
| 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } else if (!is_lastmsg(i2c)) { |
| 329 | /* we need to go to the next i2c message */ |
| 330 | |
| 331 | dev_dbg(i2c->dev, "WRITE: Next Message\n"); |
| 332 | |
| 333 | i2c->msg_ptr = 0; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 334 | i2c->msg_idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | i2c->msg++; |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | /* check to see if we need to do another message */ |
| 338 | if (i2c->msg->flags & I2C_M_NOSTART) { |
| 339 | |
| 340 | if (i2c->msg->flags & I2C_M_RD) { |
| 341 | /* cannot do this, the controller |
| 342 | * forces us to send a new START |
| 343 | * when we change direction */ |
| 344 | |
| 345 | s3c24xx_i2c_stop(i2c, -EINVAL); |
| 346 | } |
| 347 | |
| 348 | goto retry_write; |
| 349 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | /* send the new start */ |
| 351 | s3c24xx_i2c_message_start(i2c, i2c->msg); |
| 352 | i2c->state = STATE_START; |
| 353 | } |
| 354 | |
| 355 | } else { |
| 356 | /* send stop */ |
| 357 | |
| 358 | s3c24xx_i2c_stop(i2c, 0); |
| 359 | } |
| 360 | break; |
| 361 | |
| 362 | case STATE_READ: |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 363 | /* we have a byte of data in the data register, do |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | * something with it, and then work out wether we are |
| 365 | * going to do any more read/write |
| 366 | */ |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | byte = readb(i2c->regs + S3C2410_IICDS); |
| 369 | i2c->msg->buf[i2c->msg_ptr++] = byte; |
| 370 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 371 | prepare_read: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | if (is_msglast(i2c)) { |
| 373 | /* last byte of buffer */ |
| 374 | |
| 375 | if (is_lastmsg(i2c)) |
| 376 | s3c24xx_i2c_disable_ack(i2c); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } else if (is_msgend(i2c)) { |
| 379 | /* ok, we've read the entire buffer, see if there |
| 380 | * is anything else we need to do */ |
| 381 | |
| 382 | if (is_lastmsg(i2c)) { |
| 383 | /* last message, send stop and complete */ |
| 384 | dev_dbg(i2c->dev, "READ: Send Stop\n"); |
| 385 | |
| 386 | s3c24xx_i2c_stop(i2c, 0); |
| 387 | } else { |
| 388 | /* go to the next transfer */ |
| 389 | dev_dbg(i2c->dev, "READ: Next Transfer\n"); |
| 390 | |
| 391 | i2c->msg_ptr = 0; |
| 392 | i2c->msg_idx++; |
| 393 | i2c->msg++; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | /* acknowlegde the IRQ and get back on with the work */ |
| 401 | |
| 402 | out_ack: |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 403 | tmp = readl(i2c->regs + S3C2410_IICCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | tmp &= ~S3C2410_IICCON_IRQPEND; |
| 405 | writel(tmp, i2c->regs + S3C2410_IICCON); |
| 406 | out: |
| 407 | return ret; |
| 408 | } |
| 409 | |
| 410 | /* s3c24xx_i2c_irq |
| 411 | * |
| 412 | * top level IRQ servicing routine |
| 413 | */ |
| 414 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 415 | static irqreturn_t s3c24xx_i2c_irq(int irqno, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
| 417 | struct s3c24xx_i2c *i2c = dev_id; |
| 418 | unsigned long status; |
| 419 | unsigned long tmp; |
| 420 | |
| 421 | status = readl(i2c->regs + S3C2410_IICSTAT); |
| 422 | |
| 423 | if (status & S3C2410_IICSTAT_ARBITR) { |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 424 | /* deal with arbitration loss */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | dev_err(i2c->dev, "deal with arbitration loss\n"); |
| 426 | } |
| 427 | |
| 428 | if (i2c->state == STATE_IDLE) { |
| 429 | dev_dbg(i2c->dev, "IRQ: error i2c->state == IDLE\n"); |
| 430 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 431 | tmp = readl(i2c->regs + S3C2410_IICCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | tmp &= ~S3C2410_IICCON_IRQPEND; |
| 433 | writel(tmp, i2c->regs + S3C2410_IICCON); |
| 434 | goto out; |
| 435 | } |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | /* pretty much this leaves us with the fact that we've |
| 438 | * transmitted or received whatever byte we last sent */ |
| 439 | |
| 440 | i2s_s3c_irq_nextbyte(i2c, status); |
| 441 | |
| 442 | out: |
| 443 | return IRQ_HANDLED; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | /* s3c24xx_i2c_set_master |
| 448 | * |
| 449 | * get the i2c bus for a master transaction |
| 450 | */ |
| 451 | |
| 452 | static int s3c24xx_i2c_set_master(struct s3c24xx_i2c *i2c) |
| 453 | { |
| 454 | unsigned long iicstat; |
| 455 | int timeout = 400; |
| 456 | |
| 457 | while (timeout-- > 0) { |
| 458 | iicstat = readl(i2c->regs + S3C2410_IICSTAT); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | if (!(iicstat & S3C2410_IICSTAT_BUSBUSY)) |
| 461 | return 0; |
| 462 | |
| 463 | msleep(1); |
| 464 | } |
| 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return -ETIMEDOUT; |
| 467 | } |
| 468 | |
| 469 | /* s3c24xx_i2c_doxfer |
| 470 | * |
| 471 | * this starts an i2c transfer |
| 472 | */ |
| 473 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 474 | static int s3c24xx_i2c_doxfer(struct s3c24xx_i2c *i2c, |
| 475 | struct i2c_msg *msgs, int num) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | { |
| 477 | unsigned long timeout; |
| 478 | int ret; |
| 479 | |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 480 | if (i2c->suspended) |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 481 | return -EIO; |
| 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | ret = s3c24xx_i2c_set_master(i2c); |
| 484 | if (ret != 0) { |
| 485 | dev_err(i2c->dev, "cannot get bus (error %d)\n", ret); |
| 486 | ret = -EAGAIN; |
| 487 | goto out; |
| 488 | } |
| 489 | |
| 490 | spin_lock_irq(&i2c->lock); |
| 491 | |
| 492 | i2c->msg = msgs; |
| 493 | i2c->msg_num = num; |
| 494 | i2c->msg_ptr = 0; |
| 495 | i2c->msg_idx = 0; |
| 496 | i2c->state = STATE_START; |
| 497 | |
| 498 | s3c24xx_i2c_enable_irq(i2c); |
| 499 | s3c24xx_i2c_message_start(i2c, msgs); |
| 500 | spin_unlock_irq(&i2c->lock); |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5); |
| 503 | |
| 504 | ret = i2c->msg_idx; |
| 505 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 506 | /* having these next two as dev_err() makes life very |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | * noisy when doing an i2cdetect */ |
| 508 | |
| 509 | if (timeout == 0) |
| 510 | dev_dbg(i2c->dev, "timeout\n"); |
| 511 | else if (ret != num) |
| 512 | dev_dbg(i2c->dev, "incomplete xfer (%d)\n", ret); |
| 513 | |
| 514 | /* ensure the stop has been through the bus */ |
| 515 | |
| 516 | msleep(1); |
| 517 | |
| 518 | out: |
| 519 | return ret; |
| 520 | } |
| 521 | |
| 522 | /* s3c24xx_i2c_xfer |
| 523 | * |
| 524 | * 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] | 525 | * transferring across the i2c bus. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | */ |
| 527 | |
| 528 | static int s3c24xx_i2c_xfer(struct i2c_adapter *adap, |
| 529 | struct i2c_msg *msgs, int num) |
| 530 | { |
| 531 | struct s3c24xx_i2c *i2c = (struct s3c24xx_i2c *)adap->algo_data; |
| 532 | int retry; |
| 533 | int ret; |
| 534 | |
| 535 | for (retry = 0; retry < adap->retries; retry++) { |
| 536 | |
| 537 | ret = s3c24xx_i2c_doxfer(i2c, msgs, num); |
| 538 | |
| 539 | if (ret != -EAGAIN) |
| 540 | return ret; |
| 541 | |
| 542 | dev_dbg(i2c->dev, "Retrying transmission (%d)\n", retry); |
| 543 | |
| 544 | udelay(100); |
| 545 | } |
| 546 | |
| 547 | return -EREMOTEIO; |
| 548 | } |
| 549 | |
| 550 | /* declare our i2c functionality */ |
| 551 | static u32 s3c24xx_i2c_func(struct i2c_adapter *adap) |
| 552 | { |
| 553 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_PROTOCOL_MANGLING; |
| 554 | } |
| 555 | |
| 556 | /* i2c bus registration info */ |
| 557 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 558 | static const struct i2c_algorithm s3c24xx_i2c_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | .master_xfer = s3c24xx_i2c_xfer, |
| 560 | .functionality = s3c24xx_i2c_func, |
| 561 | }; |
| 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | /* s3c24xx_i2c_calcdivisor |
| 564 | * |
| 565 | * return the divisor settings for a given frequency |
| 566 | */ |
| 567 | |
| 568 | static int s3c24xx_i2c_calcdivisor(unsigned long clkin, unsigned int wanted, |
| 569 | unsigned int *div1, unsigned int *divs) |
| 570 | { |
| 571 | unsigned int calc_divs = clkin / wanted; |
| 572 | unsigned int calc_div1; |
| 573 | |
| 574 | if (calc_divs > (16*16)) |
| 575 | calc_div1 = 512; |
| 576 | else |
| 577 | calc_div1 = 16; |
| 578 | |
| 579 | calc_divs += calc_div1-1; |
| 580 | calc_divs /= calc_div1; |
| 581 | |
| 582 | if (calc_divs == 0) |
| 583 | calc_divs = 1; |
| 584 | if (calc_divs > 17) |
| 585 | calc_divs = 17; |
| 586 | |
| 587 | *divs = calc_divs; |
| 588 | *div1 = calc_div1; |
| 589 | |
| 590 | return clkin / (calc_divs * calc_div1); |
| 591 | } |
| 592 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 593 | /* s3c24xx_i2c_clockrate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | * |
| 595 | * work out a divisor for the user requested frequency setting, |
| 596 | * either by the requested frequency, or scanning the acceptable |
| 597 | * range of frequencies until something is found |
| 598 | */ |
| 599 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 600 | static int s3c24xx_i2c_clockrate(struct s3c24xx_i2c *i2c, unsigned int *got) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | { |
Ben Dooks | 6a039ca | 2008-10-31 16:10:27 +0000 | [diff] [blame] | 602 | struct s3c2410_platform_i2c *pdata = i2c->dev->platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | unsigned long clkin = clk_get_rate(i2c->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | unsigned int divs, div1; |
Daniel Silverstone | c564e6a | 2009-03-13 13:53:46 +0000 | [diff] [blame^] | 605 | unsigned long target_frequency; |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 606 | u32 iiccon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | int freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 609 | i2c->clkrate = clkin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | clkin /= 1000; /* clkin now in KHz */ |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 611 | |
Daniel Silverstone | c564e6a | 2009-03-13 13:53:46 +0000 | [diff] [blame^] | 612 | dev_dbg(i2c->dev, "pdata desired frequency %lu\n", pdata->frequency); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
Daniel Silverstone | c564e6a | 2009-03-13 13:53:46 +0000 | [diff] [blame^] | 614 | target_frequency = pdata->frequency ? pdata->frequency : 100000; |
| 615 | |
| 616 | target_frequency /= 1000; /* Target frequency now in KHz */ |
| 617 | |
| 618 | freq = s3c24xx_i2c_calcdivisor(clkin, target_frequency, &div1, &divs); |
| 619 | |
| 620 | if (freq > target_frequency) { |
| 621 | dev_err(i2c->dev, |
| 622 | "Unable to achieve desired frequency %luKHz." \ |
| 623 | " Lowest achievable %dKHz\n", target_frequency, freq); |
| 624 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | *got = freq; |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 628 | |
| 629 | iiccon = readl(i2c->regs + S3C2410_IICCON); |
| 630 | iiccon &= ~(S3C2410_IICCON_SCALEMASK | S3C2410_IICCON_TXDIV_512); |
| 631 | iiccon |= (divs-1); |
| 632 | |
| 633 | if (div1 == 512) |
| 634 | iiccon |= S3C2410_IICCON_TXDIV_512; |
| 635 | |
| 636 | writel(iiccon, i2c->regs + S3C2410_IICCON); |
| 637 | |
Ben Dooks | a192f71 | 2009-03-27 10:52:13 +0000 | [diff] [blame] | 638 | if (s3c24xx_i2c_is2440(i2c)) { |
| 639 | unsigned long sda_delay; |
| 640 | |
| 641 | if (pdata->sda_delay) { |
| 642 | sda_delay = (freq / 1000) * pdata->sda_delay; |
| 643 | sda_delay /= 1000000; |
| 644 | sda_delay = DIV_ROUND_UP(sda_delay, 5); |
| 645 | if (sda_delay > 3) |
| 646 | sda_delay = 3; |
| 647 | sda_delay |= S3C2410_IICLC_FILTER_ON; |
| 648 | } else |
| 649 | sda_delay = 0; |
| 650 | |
| 651 | dev_dbg(i2c->dev, "IICLC=%08lx\n", sda_delay); |
| 652 | writel(sda_delay, i2c->regs + S3C2440_IICLC); |
| 653 | } |
| 654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 658 | #ifdef CONFIG_CPU_FREQ |
| 659 | |
| 660 | #define freq_to_i2c(_n) container_of(_n, struct s3c24xx_i2c, freq_transition) |
| 661 | |
| 662 | static int s3c24xx_i2c_cpufreq_transition(struct notifier_block *nb, |
| 663 | unsigned long val, void *data) |
| 664 | { |
| 665 | struct s3c24xx_i2c *i2c = freq_to_i2c(nb); |
| 666 | unsigned long flags; |
| 667 | unsigned int got; |
| 668 | int delta_f; |
| 669 | int ret; |
| 670 | |
| 671 | delta_f = clk_get_rate(i2c->clk) - i2c->clkrate; |
| 672 | |
| 673 | /* if we're post-change and the input clock has slowed down |
| 674 | * or at pre-change and the clock is about to speed up, then |
| 675 | * adjust our clock rate. <0 is slow, >0 speedup. |
| 676 | */ |
| 677 | |
| 678 | if ((val == CPUFREQ_POSTCHANGE && delta_f < 0) || |
| 679 | (val == CPUFREQ_PRECHANGE && delta_f > 0)) { |
| 680 | spin_lock_irqsave(&i2c->lock, flags); |
| 681 | ret = s3c24xx_i2c_clockrate(i2c, &got); |
| 682 | spin_unlock_irqrestore(&i2c->lock, flags); |
| 683 | |
| 684 | if (ret < 0) |
| 685 | dev_err(i2c->dev, "cannot find frequency\n"); |
| 686 | else |
| 687 | dev_info(i2c->dev, "setting freq %d\n", got); |
| 688 | } |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) |
| 694 | { |
| 695 | i2c->freq_transition.notifier_call = s3c24xx_i2c_cpufreq_transition; |
| 696 | |
| 697 | return cpufreq_register_notifier(&i2c->freq_transition, |
| 698 | CPUFREQ_TRANSITION_NOTIFIER); |
| 699 | } |
| 700 | |
| 701 | static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) |
| 702 | { |
| 703 | cpufreq_unregister_notifier(&i2c->freq_transition, |
| 704 | CPUFREQ_TRANSITION_NOTIFIER); |
| 705 | } |
| 706 | |
| 707 | #else |
| 708 | static inline int s3c24xx_i2c_register_cpufreq(struct s3c24xx_i2c *i2c) |
| 709 | { |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | static inline void s3c24xx_i2c_deregister_cpufreq(struct s3c24xx_i2c *i2c) |
| 714 | { |
| 715 | } |
| 716 | #endif |
| 717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | /* s3c24xx_i2c_init |
| 719 | * |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 720 | * initialise the controller, set the IO lines and frequency |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | */ |
| 722 | |
| 723 | static int s3c24xx_i2c_init(struct s3c24xx_i2c *i2c) |
| 724 | { |
| 725 | unsigned long iicon = S3C2410_IICCON_IRQEN | S3C2410_IICCON_ACKEN; |
| 726 | struct s3c2410_platform_i2c *pdata; |
| 727 | unsigned int freq; |
| 728 | |
| 729 | /* get the plafrom data */ |
| 730 | |
Ben Dooks | 6a039ca | 2008-10-31 16:10:27 +0000 | [diff] [blame] | 731 | pdata = i2c->dev->platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
| 733 | /* inititalise the gpio */ |
| 734 | |
Ben Dooks | 8be310a | 2008-10-31 16:10:25 +0000 | [diff] [blame] | 735 | if (pdata->cfg_gpio) |
| 736 | pdata->cfg_gpio(to_platform_device(i2c->dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
| 738 | /* write slave address */ |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 739 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | writeb(pdata->slave_addr, i2c->regs + S3C2410_IICADD); |
| 741 | |
| 742 | dev_info(i2c->dev, "slave address 0x%02x\n", pdata->slave_addr); |
| 743 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 744 | writel(iicon, i2c->regs + S3C2410_IICCON); |
| 745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | /* we need to work out the divisors for the clock... */ |
| 747 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 748 | if (s3c24xx_i2c_clockrate(i2c, &freq) != 0) { |
| 749 | writel(0, i2c->regs + S3C2410_IICCON); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | dev_err(i2c->dev, "cannot meet bus frequency required\n"); |
| 751 | return -EINVAL; |
| 752 | } |
| 753 | |
| 754 | /* todo - check that the i2c lines aren't being dragged anywhere */ |
| 755 | |
| 756 | dev_info(i2c->dev, "bus frequency set to %d KHz\n", freq); |
| 757 | dev_dbg(i2c->dev, "S3C2410_IICCON=0x%02lx\n", iicon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | |
| 759 | /* check for s3c2440 i2c controller */ |
| 760 | |
Ben Dooks | a192f71 | 2009-03-27 10:52:13 +0000 | [diff] [blame] | 761 | if (s3c24xx_i2c_is2440(i2c)) |
| 762 | writel(0x0, i2c->regs + S3C2440_IICLC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | /* s3c24xx_i2c_probe |
| 768 | * |
| 769 | * called by the bus driver when a suitable device is found |
| 770 | */ |
| 771 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 772 | static int s3c24xx_i2c_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | { |
Ben Dooks | 692acbd | 2008-10-31 16:10:28 +0000 | [diff] [blame] | 774 | struct s3c24xx_i2c *i2c; |
Ben Dooks | 399dee2 | 2008-07-28 12:04:06 +0100 | [diff] [blame] | 775 | struct s3c2410_platform_i2c *pdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | struct resource *res; |
| 777 | int ret; |
| 778 | |
Ben Dooks | 6a039ca | 2008-10-31 16:10:27 +0000 | [diff] [blame] | 779 | pdata = pdev->dev.platform_data; |
| 780 | if (!pdata) { |
| 781 | dev_err(&pdev->dev, "no platform data\n"); |
| 782 | return -EINVAL; |
| 783 | } |
Ben Dooks | 399dee2 | 2008-07-28 12:04:06 +0100 | [diff] [blame] | 784 | |
Ben Dooks | 692acbd | 2008-10-31 16:10:28 +0000 | [diff] [blame] | 785 | i2c = kzalloc(sizeof(struct s3c24xx_i2c), GFP_KERNEL); |
| 786 | if (!i2c) { |
| 787 | dev_err(&pdev->dev, "no memory for state\n"); |
| 788 | return -ENOMEM; |
| 789 | } |
| 790 | |
| 791 | strlcpy(i2c->adap.name, "s3c2410-i2c", sizeof(i2c->adap.name)); |
| 792 | i2c->adap.owner = THIS_MODULE; |
| 793 | i2c->adap.algo = &s3c24xx_i2c_algorithm; |
| 794 | i2c->adap.retries = 2; |
| 795 | i2c->adap.class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
| 796 | i2c->tx_setup = 50; |
| 797 | |
| 798 | spin_lock_init(&i2c->lock); |
| 799 | init_waitqueue_head(&i2c->wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
| 801 | /* find the clock and enable it */ |
| 802 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 803 | i2c->dev = &pdev->dev; |
| 804 | i2c->clk = clk_get(&pdev->dev, "i2c"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | if (IS_ERR(i2c->clk)) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 806 | dev_err(&pdev->dev, "cannot get clock\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | ret = -ENOENT; |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 808 | goto err_noclk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 811 | dev_dbg(&pdev->dev, "clock source %p\n", i2c->clk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | clk_enable(i2c->clk); |
| 814 | |
| 815 | /* map the registers */ |
| 816 | |
| 817 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 818 | if (res == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 819 | dev_err(&pdev->dev, "cannot find IO resource\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | ret = -ENOENT; |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 821 | goto err_clk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | i2c->ioarea = request_mem_region(res->start, (res->end-res->start)+1, |
| 825 | pdev->name); |
| 826 | |
| 827 | if (i2c->ioarea == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 828 | dev_err(&pdev->dev, "cannot request IO\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | ret = -ENXIO; |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 830 | goto err_clk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | i2c->regs = ioremap(res->start, (res->end-res->start)+1); |
| 834 | |
| 835 | if (i2c->regs == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 836 | dev_err(&pdev->dev, "cannot map IO\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | ret = -ENXIO; |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 838 | goto err_ioarea; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Ben Dooks | 3d0911b | 2008-10-31 16:10:24 +0000 | [diff] [blame] | 841 | dev_dbg(&pdev->dev, "registers %p (%p, %p)\n", |
| 842 | i2c->regs, i2c->ioarea, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
| 844 | /* setup info block for the i2c core */ |
| 845 | |
| 846 | i2c->adap.algo_data = i2c; |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 847 | i2c->adap.dev.parent = &pdev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | |
| 849 | /* initialise the i2c controller */ |
| 850 | |
| 851 | ret = s3c24xx_i2c_init(i2c); |
| 852 | if (ret != 0) |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 853 | goto err_iomap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
| 855 | /* 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] | 856 | * ensure no current IRQs pending |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | */ |
| 858 | |
Ben Dooks | e0d1ec9 | 2008-10-31 16:10:30 +0000 | [diff] [blame] | 859 | i2c->irq = ret = platform_get_irq(pdev, 0); |
| 860 | if (ret <= 0) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 861 | dev_err(&pdev->dev, "cannot find IRQ\n"); |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 862 | goto err_iomap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Ben Dooks | e0d1ec9 | 2008-10-31 16:10:30 +0000 | [diff] [blame] | 865 | ret = request_irq(i2c->irq, s3c24xx_i2c_irq, IRQF_DISABLED, |
| 866 | dev_name(&pdev->dev), i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
| 868 | if (ret != 0) { |
Ben Dooks | e0d1ec9 | 2008-10-31 16:10:30 +0000 | [diff] [blame] | 869 | dev_err(&pdev->dev, "cannot claim IRQ %d\n", i2c->irq); |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 870 | goto err_iomap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 873 | ret = s3c24xx_i2c_register_cpufreq(i2c); |
| 874 | if (ret < 0) { |
| 875 | dev_err(&pdev->dev, "failed to register cpufreq notifier\n"); |
| 876 | goto err_irq; |
| 877 | } |
| 878 | |
Ben Dooks | 399dee2 | 2008-07-28 12:04:06 +0100 | [diff] [blame] | 879 | /* Note, previous versions of the driver used i2c_add_adapter() |
| 880 | * to add the bus at any number. We now pass the bus number via |
| 881 | * the platform data, so if unset it will now default to always |
| 882 | * being bus 0. |
| 883 | */ |
| 884 | |
| 885 | i2c->adap.nr = pdata->bus_num; |
| 886 | |
| 887 | ret = i2c_add_numbered_adapter(&i2c->adap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | if (ret < 0) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 889 | dev_err(&pdev->dev, "failed to add bus to i2c core\n"); |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 890 | goto err_cpufreq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 893 | platform_set_drvdata(pdev, i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
Jean Delvare | 22e965c | 2009-01-07 14:29:16 +0100 | [diff] [blame] | 895 | 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] | 896 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 898 | err_cpufreq: |
| 899 | s3c24xx_i2c_deregister_cpufreq(i2c); |
| 900 | |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 901 | err_irq: |
Ben Dooks | e0d1ec9 | 2008-10-31 16:10:30 +0000 | [diff] [blame] | 902 | free_irq(i2c->irq, i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 904 | err_iomap: |
| 905 | iounmap(i2c->regs); |
| 906 | |
| 907 | err_ioarea: |
| 908 | release_resource(i2c->ioarea); |
| 909 | kfree(i2c->ioarea); |
| 910 | |
| 911 | err_clk: |
| 912 | clk_disable(i2c->clk); |
| 913 | clk_put(i2c->clk); |
| 914 | |
| 915 | err_noclk: |
Ben Dooks | 692acbd | 2008-10-31 16:10:28 +0000 | [diff] [blame] | 916 | kfree(i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | return ret; |
| 918 | } |
| 919 | |
| 920 | /* s3c24xx_i2c_remove |
| 921 | * |
| 922 | * called when device is removed from the bus |
| 923 | */ |
| 924 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 925 | static int s3c24xx_i2c_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 927 | struct s3c24xx_i2c *i2c = platform_get_drvdata(pdev); |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 928 | |
Ben Dooks | 61c7cff | 2008-07-28 12:04:07 +0100 | [diff] [blame] | 929 | s3c24xx_i2c_deregister_cpufreq(i2c); |
| 930 | |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 931 | i2c_del_adapter(&i2c->adap); |
Ben Dooks | e0d1ec9 | 2008-10-31 16:10:30 +0000 | [diff] [blame] | 932 | free_irq(i2c->irq, i2c); |
Ben Dooks | 5b68790 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 933 | |
| 934 | clk_disable(i2c->clk); |
| 935 | clk_put(i2c->clk); |
| 936 | |
| 937 | iounmap(i2c->regs); |
| 938 | |
| 939 | release_resource(i2c->ioarea); |
| 940 | kfree(i2c->ioarea); |
Ben Dooks | 692acbd | 2008-10-31 16:10:28 +0000 | [diff] [blame] | 941 | kfree(i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | #ifdef CONFIG_PM |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 947 | static int s3c24xx_i2c_suspend_late(struct platform_device *dev, |
| 948 | pm_message_t msg) |
| 949 | { |
| 950 | struct s3c24xx_i2c *i2c = platform_get_drvdata(dev); |
| 951 | i2c->suspended = 1; |
| 952 | return 0; |
| 953 | } |
| 954 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 955 | static int s3c24xx_i2c_resume(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 957 | struct s3c24xx_i2c *i2c = platform_get_drvdata(dev); |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 958 | |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 959 | i2c->suspended = 0; |
| 960 | s3c24xx_i2c_init(i2c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | #else |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 966 | #define s3c24xx_i2c_suspend_late NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | #define s3c24xx_i2c_resume NULL |
| 968 | #endif |
| 969 | |
| 970 | /* device driver for platform bus bits */ |
| 971 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 972 | static struct platform_driver s3c2410_i2c_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | .probe = s3c24xx_i2c_probe, |
| 974 | .remove = s3c24xx_i2c_remove, |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 975 | .suspend_late = s3c24xx_i2c_suspend_late, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | .resume = s3c24xx_i2c_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 977 | .driver = { |
| 978 | .owner = THIS_MODULE, |
| 979 | .name = "s3c2410-i2c", |
| 980 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | }; |
| 982 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 983 | static struct platform_driver s3c2440_i2c_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | .probe = s3c24xx_i2c_probe, |
| 985 | .remove = s3c24xx_i2c_remove, |
Ben Dooks | be44f01 | 2008-10-31 16:10:22 +0000 | [diff] [blame] | 986 | .suspend_late = s3c24xx_i2c_suspend_late, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | .resume = s3c24xx_i2c_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 988 | .driver = { |
| 989 | .owner = THIS_MODULE, |
| 990 | .name = "s3c2440-i2c", |
| 991 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | }; |
| 993 | |
| 994 | static int __init i2c_adap_s3c_init(void) |
| 995 | { |
| 996 | int ret; |
| 997 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 998 | ret = platform_driver_register(&s3c2410_i2c_driver); |
Russell King | e32e28e | 2005-10-30 16:32:27 +0000 | [diff] [blame] | 999 | if (ret == 0) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1000 | ret = platform_driver_register(&s3c2440_i2c_driver); |
Russell King | e32e28e | 2005-10-30 16:32:27 +0000 | [diff] [blame] | 1001 | if (ret) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1002 | platform_driver_unregister(&s3c2410_i2c_driver); |
Russell King | e32e28e | 2005-10-30 16:32:27 +0000 | [diff] [blame] | 1003 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | static void __exit i2c_adap_s3c_exit(void) |
| 1009 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1010 | platform_driver_unregister(&s3c2410_i2c_driver); |
| 1011 | platform_driver_unregister(&s3c2440_i2c_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | module_init(i2c_adap_s3c_init); |
| 1015 | module_exit(i2c_adap_s3c_exit); |
| 1016 | |
| 1017 | MODULE_DESCRIPTION("S3C24XX I2C Bus driver"); |
| 1018 | MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); |
| 1019 | MODULE_LICENSE("GPL"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 1020 | MODULE_ALIAS("platform:s3c2410-i2c"); |
Ben Dooks | d150a4b | 2008-07-01 11:59:43 +0100 | [diff] [blame] | 1021 | MODULE_ALIAS("platform:s3c2440-i2c"); |