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