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