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