Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Provides I2C support for Philips PNX010x/PNX4008 boards. |
| 3 | * |
| 4 | * Authors: Dennis Kovalev <dkovalev@ru.mvista.com> |
| 5 | * Vitaly Wool <vwool@ru.mvista.com> |
| 6 | * |
| 7 | * 2004-2006 (c) MontaVista Software, Inc. This file is licensed under |
| 8 | * the terms of the GNU General Public License version 2. This program |
| 9 | * is licensed "as is" without any warranty of any kind, whether express |
| 10 | * or implied. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/ioport.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/timer.h> |
| 19 | #include <linux/completion.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/i2c-pnx.h> |
Kevin Wells | a7d73d8 | 2009-11-12 00:25:52 +0100 | [diff] [blame] | 22 | #include <linux/io.h> |
Russell King | 0321cb8 | 2009-11-20 11:12:26 +0000 | [diff] [blame] | 23 | #include <linux/err.h> |
| 24 | #include <linux/clk.h> |
| 25 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 26 | #include <mach/hardware.h> |
Kevin Wells | a7d73d8 | 2009-11-12 00:25:52 +0100 | [diff] [blame] | 27 | #include <mach/i2c.h> |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 28 | #include <asm/irq.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | |
| 31 | #define I2C_PNX_TIMEOUT 10 /* msec */ |
| 32 | #define I2C_PNX_SPEED_KHZ 100 |
| 33 | #define I2C_PNX_REGION_SIZE 0x100 |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 34 | |
| 35 | static inline int wait_timeout(long timeout, struct i2c_pnx_algo_data *data) |
| 36 | { |
| 37 | while (timeout > 0 && |
| 38 | (ioread32(I2C_REG_STS(data)) & mstatus_active)) { |
| 39 | mdelay(1); |
| 40 | timeout--; |
| 41 | } |
| 42 | return (timeout <= 0); |
| 43 | } |
| 44 | |
| 45 | static inline int wait_reset(long timeout, struct i2c_pnx_algo_data *data) |
| 46 | { |
| 47 | while (timeout > 0 && |
| 48 | (ioread32(I2C_REG_CTL(data)) & mcntrl_reset)) { |
| 49 | mdelay(1); |
| 50 | timeout--; |
| 51 | } |
| 52 | return (timeout <= 0); |
| 53 | } |
| 54 | |
| 55 | static inline void i2c_pnx_arm_timer(struct i2c_adapter *adap) |
| 56 | { |
| 57 | struct i2c_pnx_algo_data *data = adap->algo_data; |
| 58 | struct timer_list *timer = &data->mif.timer; |
| 59 | int expires = I2C_PNX_TIMEOUT / (1000 / HZ); |
| 60 | |
Kevin Wells | b2f125b | 2009-11-12 00:28:13 +0100 | [diff] [blame] | 61 | if (expires <= 1) |
| 62 | expires = 2; |
| 63 | |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 64 | del_timer_sync(timer); |
| 65 | |
| 66 | dev_dbg(&adap->dev, "Timer armed at %lu plus %u jiffies.\n", |
| 67 | jiffies, expires); |
| 68 | |
| 69 | timer->expires = jiffies + expires; |
| 70 | timer->data = (unsigned long)adap; |
| 71 | |
| 72 | add_timer(timer); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * i2c_pnx_start - start a device |
| 77 | * @slave_addr: slave address |
| 78 | * @adap: pointer to adapter structure |
| 79 | * |
| 80 | * Generate a START signal in the desired mode. |
| 81 | */ |
| 82 | static int i2c_pnx_start(unsigned char slave_addr, struct i2c_adapter *adap) |
| 83 | { |
| 84 | struct i2c_pnx_algo_data *alg_data = adap->algo_data; |
| 85 | |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 86 | dev_dbg(&adap->dev, "%s(): addr 0x%x mode %d\n", __func__, |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 87 | slave_addr, alg_data->mif.mode); |
| 88 | |
| 89 | /* Check for 7 bit slave addresses only */ |
| 90 | if (slave_addr & ~0x7f) { |
| 91 | dev_err(&adap->dev, "%s: Invalid slave address %x. " |
| 92 | "Only 7-bit addresses are supported\n", |
| 93 | adap->name, slave_addr); |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | |
| 97 | /* First, make sure bus is idle */ |
| 98 | if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) { |
| 99 | /* Somebody else is monopolizing the bus */ |
| 100 | dev_err(&adap->dev, "%s: Bus busy. Slave addr = %02x, " |
| 101 | "cntrl = %x, stat = %x\n", |
| 102 | adap->name, slave_addr, |
| 103 | ioread32(I2C_REG_CTL(alg_data)), |
| 104 | ioread32(I2C_REG_STS(alg_data))); |
| 105 | return -EBUSY; |
| 106 | } else if (ioread32(I2C_REG_STS(alg_data)) & mstatus_afi) { |
| 107 | /* Sorry, we lost the bus */ |
| 108 | dev_err(&adap->dev, "%s: Arbitration failure. " |
| 109 | "Slave addr = %02x\n", adap->name, slave_addr); |
| 110 | return -EIO; |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * OK, I2C is enabled and we have the bus. |
| 115 | * Clear the current TDI and AFI status flags. |
| 116 | */ |
| 117 | iowrite32(ioread32(I2C_REG_STS(alg_data)) | mstatus_tdi | mstatus_afi, |
| 118 | I2C_REG_STS(alg_data)); |
| 119 | |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 120 | dev_dbg(&adap->dev, "%s(): sending %#x\n", __func__, |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 121 | (slave_addr << 1) | start_bit | alg_data->mif.mode); |
| 122 | |
| 123 | /* Write the slave address, START bit and R/W bit */ |
| 124 | iowrite32((slave_addr << 1) | start_bit | alg_data->mif.mode, |
| 125 | I2C_REG_TX(alg_data)); |
| 126 | |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 127 | dev_dbg(&adap->dev, "%s(): exit\n", __func__); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * i2c_pnx_stop - stop a device |
| 134 | * @adap: pointer to I2C adapter structure |
| 135 | * |
| 136 | * Generate a STOP signal to terminate the master transaction. |
| 137 | */ |
| 138 | static void i2c_pnx_stop(struct i2c_adapter *adap) |
| 139 | { |
| 140 | struct i2c_pnx_algo_data *alg_data = adap->algo_data; |
| 141 | /* Only 1 msec max timeout due to interrupt context */ |
| 142 | long timeout = 1000; |
| 143 | |
| 144 | dev_dbg(&adap->dev, "%s(): entering: stat = %04x.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 145 | __func__, ioread32(I2C_REG_STS(alg_data))); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 146 | |
| 147 | /* Write a STOP bit to TX FIFO */ |
| 148 | iowrite32(0xff | stop_bit, I2C_REG_TX(alg_data)); |
| 149 | |
| 150 | /* Wait until the STOP is seen. */ |
| 151 | while (timeout > 0 && |
| 152 | (ioread32(I2C_REG_STS(alg_data)) & mstatus_active)) { |
| 153 | /* may be called from interrupt context */ |
| 154 | udelay(1); |
| 155 | timeout--; |
| 156 | } |
| 157 | |
| 158 | dev_dbg(&adap->dev, "%s(): exiting: stat = %04x.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 159 | __func__, ioread32(I2C_REG_STS(alg_data))); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * i2c_pnx_master_xmit - transmit data to slave |
| 164 | * @adap: pointer to I2C adapter structure |
| 165 | * |
| 166 | * Sends one byte of data to the slave |
| 167 | */ |
| 168 | static int i2c_pnx_master_xmit(struct i2c_adapter *adap) |
| 169 | { |
| 170 | struct i2c_pnx_algo_data *alg_data = adap->algo_data; |
| 171 | u32 val; |
| 172 | |
| 173 | dev_dbg(&adap->dev, "%s(): entering: stat = %04x.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 174 | __func__, ioread32(I2C_REG_STS(alg_data))); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 175 | |
| 176 | if (alg_data->mif.len > 0) { |
| 177 | /* We still have something to talk about... */ |
| 178 | val = *alg_data->mif.buf++; |
| 179 | |
| 180 | if (alg_data->mif.len == 1) { |
| 181 | val |= stop_bit; |
| 182 | if (!alg_data->last) |
| 183 | val |= start_bit; |
| 184 | } |
| 185 | |
| 186 | alg_data->mif.len--; |
| 187 | iowrite32(val, I2C_REG_TX(alg_data)); |
| 188 | |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 189 | dev_dbg(&adap->dev, "%s(): xmit %#x [%d]\n", __func__, |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 190 | val, alg_data->mif.len + 1); |
| 191 | |
| 192 | if (alg_data->mif.len == 0) { |
| 193 | if (alg_data->last) { |
| 194 | /* Wait until the STOP is seen. */ |
| 195 | if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) |
| 196 | dev_err(&adap->dev, "The bus is still " |
| 197 | "active after timeout\n"); |
| 198 | } |
| 199 | /* Disable master interrupts */ |
| 200 | iowrite32(ioread32(I2C_REG_CTL(alg_data)) & |
| 201 | ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie), |
| 202 | I2C_REG_CTL(alg_data)); |
| 203 | |
| 204 | del_timer_sync(&alg_data->mif.timer); |
| 205 | |
| 206 | dev_dbg(&adap->dev, "%s(): Waking up xfer routine.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 207 | __func__); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 208 | |
| 209 | complete(&alg_data->mif.complete); |
| 210 | } |
| 211 | } else if (alg_data->mif.len == 0) { |
| 212 | /* zero-sized transfer */ |
| 213 | i2c_pnx_stop(adap); |
| 214 | |
| 215 | /* Disable master interrupts. */ |
| 216 | iowrite32(ioread32(I2C_REG_CTL(alg_data)) & |
| 217 | ~(mcntrl_afie | mcntrl_naie | mcntrl_drmie), |
| 218 | I2C_REG_CTL(alg_data)); |
| 219 | |
| 220 | /* Stop timer. */ |
| 221 | del_timer_sync(&alg_data->mif.timer); |
| 222 | dev_dbg(&adap->dev, "%s(): Waking up xfer routine after " |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 223 | "zero-xfer.\n", __func__); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 224 | |
| 225 | complete(&alg_data->mif.complete); |
| 226 | } |
| 227 | |
| 228 | dev_dbg(&adap->dev, "%s(): exiting: stat = %04x.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 229 | __func__, ioread32(I2C_REG_STS(alg_data))); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * i2c_pnx_master_rcv - receive data from slave |
| 236 | * @adap: pointer to I2C adapter structure |
| 237 | * |
| 238 | * Reads one byte data from the slave |
| 239 | */ |
| 240 | static int i2c_pnx_master_rcv(struct i2c_adapter *adap) |
| 241 | { |
| 242 | struct i2c_pnx_algo_data *alg_data = adap->algo_data; |
| 243 | unsigned int val = 0; |
| 244 | u32 ctl = 0; |
| 245 | |
| 246 | dev_dbg(&adap->dev, "%s(): entering: stat = %04x.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 247 | __func__, ioread32(I2C_REG_STS(alg_data))); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 248 | |
| 249 | /* Check, whether there is already data, |
| 250 | * or we didn't 'ask' for it yet. |
| 251 | */ |
| 252 | if (ioread32(I2C_REG_STS(alg_data)) & mstatus_rfe) { |
| 253 | dev_dbg(&adap->dev, "%s(): Write dummy data to fill " |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 254 | "Rx-fifo...\n", __func__); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 255 | |
| 256 | if (alg_data->mif.len == 1) { |
| 257 | /* Last byte, do not acknowledge next rcv. */ |
| 258 | val |= stop_bit; |
| 259 | if (!alg_data->last) |
| 260 | val |= start_bit; |
| 261 | |
| 262 | /* |
| 263 | * Enable interrupt RFDAIE (data in Rx fifo), |
| 264 | * and disable DRMIE (need data for Tx) |
| 265 | */ |
| 266 | ctl = ioread32(I2C_REG_CTL(alg_data)); |
| 267 | ctl |= mcntrl_rffie | mcntrl_daie; |
| 268 | ctl &= ~mcntrl_drmie; |
| 269 | iowrite32(ctl, I2C_REG_CTL(alg_data)); |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Now we'll 'ask' for data: |
| 274 | * For each byte we want to receive, we must |
| 275 | * write a (dummy) byte to the Tx-FIFO. |
| 276 | */ |
| 277 | iowrite32(val, I2C_REG_TX(alg_data)); |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* Handle data. */ |
| 283 | if (alg_data->mif.len > 0) { |
| 284 | val = ioread32(I2C_REG_RX(alg_data)); |
| 285 | *alg_data->mif.buf++ = (u8) (val & 0xff); |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 286 | dev_dbg(&adap->dev, "%s(): rcv 0x%x [%d]\n", __func__, val, |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 287 | alg_data->mif.len); |
| 288 | |
| 289 | alg_data->mif.len--; |
| 290 | if (alg_data->mif.len == 0) { |
| 291 | if (alg_data->last) |
| 292 | /* Wait until the STOP is seen. */ |
| 293 | if (wait_timeout(I2C_PNX_TIMEOUT, alg_data)) |
| 294 | dev_err(&adap->dev, "The bus is still " |
| 295 | "active after timeout\n"); |
| 296 | |
| 297 | /* Disable master interrupts */ |
| 298 | ctl = ioread32(I2C_REG_CTL(alg_data)); |
| 299 | ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | |
| 300 | mcntrl_drmie | mcntrl_daie); |
| 301 | iowrite32(ctl, I2C_REG_CTL(alg_data)); |
| 302 | |
| 303 | /* Kill timer. */ |
| 304 | del_timer_sync(&alg_data->mif.timer); |
| 305 | complete(&alg_data->mif.complete); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | dev_dbg(&adap->dev, "%s(): exiting: stat = %04x.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 310 | __func__, ioread32(I2C_REG_STS(alg_data))); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
Vitaly Wool | 6c566fb7 | 2007-01-04 13:07:03 +0100 | [diff] [blame] | 315 | static irqreturn_t i2c_pnx_interrupt(int irq, void *dev_id) |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 316 | { |
| 317 | u32 stat, ctl; |
| 318 | struct i2c_adapter *adap = dev_id; |
| 319 | struct i2c_pnx_algo_data *alg_data = adap->algo_data; |
| 320 | |
| 321 | dev_dbg(&adap->dev, "%s(): mstat = %x mctrl = %x, mode = %d\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 322 | __func__, |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 323 | ioread32(I2C_REG_STS(alg_data)), |
| 324 | ioread32(I2C_REG_CTL(alg_data)), |
| 325 | alg_data->mif.mode); |
| 326 | stat = ioread32(I2C_REG_STS(alg_data)); |
| 327 | |
| 328 | /* let's see what kind of event this is */ |
| 329 | if (stat & mstatus_afi) { |
| 330 | /* We lost arbitration in the midst of a transfer */ |
| 331 | alg_data->mif.ret = -EIO; |
| 332 | |
| 333 | /* Disable master interrupts. */ |
| 334 | ctl = ioread32(I2C_REG_CTL(alg_data)); |
| 335 | ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | |
| 336 | mcntrl_drmie); |
| 337 | iowrite32(ctl, I2C_REG_CTL(alg_data)); |
| 338 | |
| 339 | /* Stop timer, to prevent timeout. */ |
| 340 | del_timer_sync(&alg_data->mif.timer); |
| 341 | complete(&alg_data->mif.complete); |
| 342 | } else if (stat & mstatus_nai) { |
| 343 | /* Slave did not acknowledge, generate a STOP */ |
| 344 | dev_dbg(&adap->dev, "%s(): " |
| 345 | "Slave did not acknowledge, generating a STOP.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 346 | __func__); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 347 | i2c_pnx_stop(adap); |
| 348 | |
| 349 | /* Disable master interrupts. */ |
| 350 | ctl = ioread32(I2C_REG_CTL(alg_data)); |
| 351 | ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | |
| 352 | mcntrl_drmie); |
| 353 | iowrite32(ctl, I2C_REG_CTL(alg_data)); |
| 354 | |
| 355 | /* Our return value. */ |
| 356 | alg_data->mif.ret = -EIO; |
| 357 | |
| 358 | /* Stop timer, to prevent timeout. */ |
| 359 | del_timer_sync(&alg_data->mif.timer); |
| 360 | complete(&alg_data->mif.complete); |
| 361 | } else { |
| 362 | /* |
| 363 | * Two options: |
| 364 | * - Master Tx needs data. |
| 365 | * - There is data in the Rx-fifo |
| 366 | * The latter is only the case if we have requested for data, |
| 367 | * via a dummy write. (See 'i2c_pnx_master_rcv'.) |
| 368 | * We therefore check, as a sanity check, whether that interrupt |
| 369 | * has been enabled. |
| 370 | */ |
| 371 | if ((stat & mstatus_drmi) || !(stat & mstatus_rfe)) { |
| 372 | if (alg_data->mif.mode == I2C_SMBUS_WRITE) { |
| 373 | i2c_pnx_master_xmit(adap); |
| 374 | } else if (alg_data->mif.mode == I2C_SMBUS_READ) { |
| 375 | i2c_pnx_master_rcv(adap); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /* Clear TDI and AFI bits */ |
| 381 | stat = ioread32(I2C_REG_STS(alg_data)); |
| 382 | iowrite32(stat | mstatus_tdi | mstatus_afi, I2C_REG_STS(alg_data)); |
| 383 | |
| 384 | dev_dbg(&adap->dev, "%s(): exiting, stat = %x ctrl = %x.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 385 | __func__, ioread32(I2C_REG_STS(alg_data)), |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 386 | ioread32(I2C_REG_CTL(alg_data))); |
| 387 | |
| 388 | return IRQ_HANDLED; |
| 389 | } |
| 390 | |
| 391 | static void i2c_pnx_timeout(unsigned long data) |
| 392 | { |
| 393 | struct i2c_adapter *adap = (struct i2c_adapter *)data; |
| 394 | struct i2c_pnx_algo_data *alg_data = adap->algo_data; |
| 395 | u32 ctl; |
| 396 | |
| 397 | dev_err(&adap->dev, "Master timed out. stat = %04x, cntrl = %04x. " |
| 398 | "Resetting master...\n", |
| 399 | ioread32(I2C_REG_STS(alg_data)), |
| 400 | ioread32(I2C_REG_CTL(alg_data))); |
| 401 | |
| 402 | /* Reset master and disable interrupts */ |
| 403 | ctl = ioread32(I2C_REG_CTL(alg_data)); |
| 404 | ctl &= ~(mcntrl_afie | mcntrl_naie | mcntrl_rffie | mcntrl_drmie); |
| 405 | iowrite32(ctl, I2C_REG_CTL(alg_data)); |
| 406 | |
| 407 | ctl |= mcntrl_reset; |
| 408 | iowrite32(ctl, I2C_REG_CTL(alg_data)); |
| 409 | wait_reset(I2C_PNX_TIMEOUT, alg_data); |
| 410 | alg_data->mif.ret = -EIO; |
| 411 | complete(&alg_data->mif.complete); |
| 412 | } |
| 413 | |
| 414 | static inline void bus_reset_if_active(struct i2c_adapter *adap) |
| 415 | { |
| 416 | struct i2c_pnx_algo_data *alg_data = adap->algo_data; |
| 417 | u32 stat; |
| 418 | |
| 419 | if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_active) { |
| 420 | dev_err(&adap->dev, |
| 421 | "%s: Bus is still active after xfer. Reset it...\n", |
| 422 | adap->name); |
| 423 | iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset, |
| 424 | I2C_REG_CTL(alg_data)); |
| 425 | wait_reset(I2C_PNX_TIMEOUT, alg_data); |
| 426 | } else if (!(stat & mstatus_rfe) || !(stat & mstatus_tfe)) { |
| 427 | /* If there is data in the fifo's after transfer, |
| 428 | * flush fifo's by reset. |
| 429 | */ |
| 430 | iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset, |
| 431 | I2C_REG_CTL(alg_data)); |
| 432 | wait_reset(I2C_PNX_TIMEOUT, alg_data); |
| 433 | } else if (stat & mstatus_nai) { |
| 434 | iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_reset, |
| 435 | I2C_REG_CTL(alg_data)); |
| 436 | wait_reset(I2C_PNX_TIMEOUT, alg_data); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * i2c_pnx_xfer - generic transfer entry point |
| 442 | * @adap: pointer to I2C adapter structure |
| 443 | * @msgs: array of messages |
| 444 | * @num: number of messages |
| 445 | * |
| 446 | * Initiates the transfer |
| 447 | */ |
| 448 | static int |
| 449 | i2c_pnx_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) |
| 450 | { |
| 451 | struct i2c_msg *pmsg; |
| 452 | int rc = 0, completed = 0, i; |
| 453 | struct i2c_pnx_algo_data *alg_data = adap->algo_data; |
| 454 | u32 stat = ioread32(I2C_REG_STS(alg_data)); |
| 455 | |
| 456 | dev_dbg(&adap->dev, "%s(): entering: %d messages, stat = %04x.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 457 | __func__, num, ioread32(I2C_REG_STS(alg_data))); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 458 | |
| 459 | bus_reset_if_active(adap); |
| 460 | |
| 461 | /* Process transactions in a loop. */ |
| 462 | for (i = 0; rc >= 0 && i < num; i++) { |
| 463 | u8 addr; |
| 464 | |
| 465 | pmsg = &msgs[i]; |
| 466 | addr = pmsg->addr; |
| 467 | |
| 468 | if (pmsg->flags & I2C_M_TEN) { |
| 469 | dev_err(&adap->dev, |
| 470 | "%s: 10 bits addr not supported!\n", |
| 471 | adap->name); |
| 472 | rc = -EINVAL; |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | alg_data->mif.buf = pmsg->buf; |
| 477 | alg_data->mif.len = pmsg->len; |
| 478 | alg_data->mif.mode = (pmsg->flags & I2C_M_RD) ? |
| 479 | I2C_SMBUS_READ : I2C_SMBUS_WRITE; |
| 480 | alg_data->mif.ret = 0; |
| 481 | alg_data->last = (i == num - 1); |
| 482 | |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 483 | dev_dbg(&adap->dev, "%s(): mode %d, %d bytes\n", __func__, |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 484 | alg_data->mif.mode, |
| 485 | alg_data->mif.len); |
| 486 | |
| 487 | i2c_pnx_arm_timer(adap); |
| 488 | |
| 489 | /* initialize the completion var */ |
| 490 | init_completion(&alg_data->mif.complete); |
| 491 | |
| 492 | /* Enable master interrupt */ |
| 493 | iowrite32(ioread32(I2C_REG_CTL(alg_data)) | mcntrl_afie | |
| 494 | mcntrl_naie | mcntrl_drmie, |
| 495 | I2C_REG_CTL(alg_data)); |
| 496 | |
| 497 | /* Put start-code and slave-address on the bus. */ |
| 498 | rc = i2c_pnx_start(addr, adap); |
| 499 | if (rc < 0) |
| 500 | break; |
| 501 | |
| 502 | /* Wait for completion */ |
| 503 | wait_for_completion(&alg_data->mif.complete); |
| 504 | |
| 505 | if (!(rc = alg_data->mif.ret)) |
| 506 | completed++; |
| 507 | dev_dbg(&adap->dev, "%s(): Complete, return code = %d.\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 508 | __func__, rc); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 509 | |
| 510 | /* Clear TDI and AFI bits in case they are set. */ |
| 511 | if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_tdi) { |
| 512 | dev_dbg(&adap->dev, |
| 513 | "%s: TDI still set... clearing now.\n", |
| 514 | adap->name); |
| 515 | iowrite32(stat, I2C_REG_STS(alg_data)); |
| 516 | } |
| 517 | if ((stat = ioread32(I2C_REG_STS(alg_data))) & mstatus_afi) { |
| 518 | dev_dbg(&adap->dev, |
| 519 | "%s: AFI still set... clearing now.\n", |
| 520 | adap->name); |
| 521 | iowrite32(stat, I2C_REG_STS(alg_data)); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | bus_reset_if_active(adap); |
| 526 | |
| 527 | /* Cleanup to be sure... */ |
| 528 | alg_data->mif.buf = NULL; |
| 529 | alg_data->mif.len = 0; |
| 530 | |
| 531 | dev_dbg(&adap->dev, "%s(): exiting, stat = %x\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 532 | __func__, ioread32(I2C_REG_STS(alg_data))); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 533 | |
| 534 | if (completed != num) |
| 535 | return ((rc < 0) ? rc : -EREMOTEIO); |
| 536 | |
| 537 | return num; |
| 538 | } |
| 539 | |
| 540 | static u32 i2c_pnx_func(struct i2c_adapter *adapter) |
| 541 | { |
| 542 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 543 | } |
| 544 | |
| 545 | static struct i2c_algorithm pnx_algorithm = { |
| 546 | .master_xfer = i2c_pnx_xfer, |
| 547 | .functionality = i2c_pnx_func, |
| 548 | }; |
| 549 | |
Russell King | a0dcf19 | 2009-11-20 10:50:34 +0000 | [diff] [blame] | 550 | #ifdef CONFIG_PM |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 551 | static int i2c_pnx_controller_suspend(struct platform_device *pdev, |
| 552 | pm_message_t state) |
| 553 | { |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 554 | struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev); |
Russell King | 0321cb8 | 2009-11-20 11:12:26 +0000 | [diff] [blame] | 555 | |
Russell King | ebdbbf2 | 2009-11-20 11:44:46 +0000 | [diff] [blame] | 556 | /* FIXME: shouldn't this be clk_disable? */ |
| 557 | clk_enable(alg_data->clk); |
Russell King | 0321cb8 | 2009-11-20 11:12:26 +0000 | [diff] [blame] | 558 | |
| 559 | return 0; |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | static int i2c_pnx_controller_resume(struct platform_device *pdev) |
| 563 | { |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 564 | struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev); |
Russell King | 0321cb8 | 2009-11-20 11:12:26 +0000 | [diff] [blame] | 565 | |
Russell King | ebdbbf2 | 2009-11-20 11:44:46 +0000 | [diff] [blame] | 566 | return clk_enable(alg_data->clk); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 567 | } |
Russell King | a0dcf19 | 2009-11-20 10:50:34 +0000 | [diff] [blame] | 568 | #else |
| 569 | #define i2c_pnx_controller_suspend NULL |
| 570 | #define i2c_pnx_controller_resume NULL |
| 571 | #endif |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 572 | |
| 573 | static int __devinit i2c_pnx_probe(struct platform_device *pdev) |
| 574 | { |
| 575 | unsigned long tmp; |
| 576 | int ret = 0; |
| 577 | struct i2c_pnx_algo_data *alg_data; |
Russell King | 6fff3da | 2009-11-20 12:46:07 +0000 | [diff] [blame] | 578 | unsigned long freq; |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 579 | struct i2c_pnx_data *i2c_pnx = pdev->dev.platform_data; |
| 580 | |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 581 | if (!i2c_pnx || !i2c_pnx->name) { |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 582 | dev_err(&pdev->dev, "%s: no platform data supplied\n", |
Harvey Harrison | 08882d2 | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 583 | __func__); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 584 | ret = -EINVAL; |
| 585 | goto out; |
| 586 | } |
| 587 | |
Russell King | 44c5d73 | 2009-11-21 12:10:54 +0000 | [diff] [blame] | 588 | alg_data = kzalloc(sizeof(*alg_data), GFP_KERNEL); |
| 589 | if (!alg_data) { |
| 590 | ret = -ENOMEM; |
| 591 | goto err_kzalloc; |
| 592 | } |
| 593 | |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 594 | platform_set_drvdata(pdev, alg_data); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 595 | |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 596 | strlcpy(alg_data->adapter.name, i2c_pnx->name, |
| 597 | sizeof(alg_data->adapter.name)); |
| 598 | alg_data->adapter.dev.parent = &pdev->dev; |
| 599 | alg_data->adapter.algo = &pnx_algorithm; |
| 600 | alg_data->adapter.algo_data = alg_data; |
| 601 | alg_data->adapter.nr = pdev->id; |
| 602 | alg_data->i2c_pnx = i2c_pnx; |
Russell King | 0321cb8 | 2009-11-20 11:12:26 +0000 | [diff] [blame] | 603 | |
| 604 | alg_data->clk = clk_get(&pdev->dev, NULL); |
| 605 | if (IS_ERR(alg_data->clk)) { |
| 606 | ret = PTR_ERR(alg_data->clk); |
| 607 | goto out_drvdata; |
| 608 | } |
| 609 | |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 610 | init_timer(&alg_data->mif.timer); |
| 611 | alg_data->mif.timer.function = i2c_pnx_timeout; |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 612 | alg_data->mif.timer.data = (unsigned long)&alg_data->adapter; |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 613 | |
| 614 | /* Register I/O resource */ |
Russell King | 44c5d73 | 2009-11-21 12:10:54 +0000 | [diff] [blame] | 615 | if (!request_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE, |
Julia Lawall | 449d2c7 | 2009-09-18 22:45:51 +0200 | [diff] [blame] | 616 | pdev->name)) { |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 617 | dev_err(&pdev->dev, |
| 618 | "I/O region 0x%08x for I2C already in use.\n", |
Russell King | 44c5d73 | 2009-11-21 12:10:54 +0000 | [diff] [blame] | 619 | i2c_pnx->base); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 620 | ret = -ENODEV; |
Russell King | 0321cb8 | 2009-11-20 11:12:26 +0000 | [diff] [blame] | 621 | goto out_clkget; |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Russell King | 44c5d73 | 2009-11-21 12:10:54 +0000 | [diff] [blame] | 624 | alg_data->ioaddr = ioremap(i2c_pnx->base, I2C_PNX_REGION_SIZE); |
Russell King | 88d968b | 2009-11-21 11:58:36 +0000 | [diff] [blame] | 625 | if (!alg_data->ioaddr) { |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 626 | dev_err(&pdev->dev, "Couldn't ioremap I2C I/O region\n"); |
| 627 | ret = -ENOMEM; |
| 628 | goto out_release; |
| 629 | } |
| 630 | |
Russell King | ebdbbf2 | 2009-11-20 11:44:46 +0000 | [diff] [blame] | 631 | ret = clk_enable(alg_data->clk); |
| 632 | if (ret) |
| 633 | goto out_unmap; |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 634 | |
Russell King | 6fff3da | 2009-11-20 12:46:07 +0000 | [diff] [blame] | 635 | freq = clk_get_rate(alg_data->clk); |
| 636 | |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 637 | /* |
| 638 | * Clock Divisor High This value is the number of system clocks |
| 639 | * the serial clock (SCL) will be high. |
| 640 | * For example, if the system clock period is 50 ns and the maximum |
| 641 | * desired serial period is 10000 ns (100 kHz), then CLKHI would be |
| 642 | * set to 0.5*(f_sys/f_i2c)-2=0.5*(20e6/100e3)-2=98. The actual value |
| 643 | * programmed into CLKHI will vary from this slightly due to |
| 644 | * variations in the output pad's rise and fall times as well as |
| 645 | * the deglitching filter length. |
| 646 | */ |
| 647 | |
Russell King | 6fff3da | 2009-11-20 12:46:07 +0000 | [diff] [blame] | 648 | tmp = ((freq / 1000) / I2C_PNX_SPEED_KHZ) / 2 - 2; |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 649 | iowrite32(tmp, I2C_REG_CKH(alg_data)); |
| 650 | iowrite32(tmp, I2C_REG_CKL(alg_data)); |
| 651 | |
| 652 | iowrite32(mcntrl_reset, I2C_REG_CTL(alg_data)); |
| 653 | if (wait_reset(I2C_PNX_TIMEOUT, alg_data)) { |
| 654 | ret = -ENODEV; |
Russell King | ebdbbf2 | 2009-11-20 11:44:46 +0000 | [diff] [blame] | 655 | goto out_clock; |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 656 | } |
| 657 | init_completion(&alg_data->mif.complete); |
| 658 | |
Russell King | 44c5d73 | 2009-11-21 12:10:54 +0000 | [diff] [blame] | 659 | ret = request_irq(i2c_pnx->irq, i2c_pnx_interrupt, |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 660 | 0, pdev->name, &alg_data->adapter); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 661 | if (ret) |
| 662 | goto out_clock; |
| 663 | |
| 664 | /* Register this adapter with the I2C subsystem */ |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 665 | ret = i2c_add_numbered_adapter(&alg_data->adapter); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 666 | if (ret < 0) { |
| 667 | dev_err(&pdev->dev, "I2C: Failed to add bus\n"); |
| 668 | goto out_irq; |
| 669 | } |
| 670 | |
| 671 | dev_dbg(&pdev->dev, "%s: Master at %#8x, irq %d.\n", |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 672 | alg_data->adapter.name, i2c_pnx->base, i2c_pnx->irq); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 673 | |
| 674 | return 0; |
| 675 | |
| 676 | out_irq: |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 677 | free_irq(i2c_pnx->irq, &alg_data->adapter); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 678 | out_clock: |
Russell King | ebdbbf2 | 2009-11-20 11:44:46 +0000 | [diff] [blame] | 679 | clk_disable(alg_data->clk); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 680 | out_unmap: |
Russell King | 88d968b | 2009-11-21 11:58:36 +0000 | [diff] [blame] | 681 | iounmap(alg_data->ioaddr); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 682 | out_release: |
Russell King | 44c5d73 | 2009-11-21 12:10:54 +0000 | [diff] [blame] | 683 | release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE); |
Russell King | 0321cb8 | 2009-11-20 11:12:26 +0000 | [diff] [blame] | 684 | out_clkget: |
| 685 | clk_put(alg_data->clk); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 686 | out_drvdata: |
Russell King | 44c5d73 | 2009-11-21 12:10:54 +0000 | [diff] [blame] | 687 | kfree(alg_data); |
| 688 | err_kzalloc: |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 689 | platform_set_drvdata(pdev, NULL); |
| 690 | out: |
| 691 | return ret; |
| 692 | } |
| 693 | |
| 694 | static int __devexit i2c_pnx_remove(struct platform_device *pdev) |
| 695 | { |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 696 | struct i2c_pnx_algo_data *alg_data = platform_get_drvdata(pdev); |
| 697 | struct i2c_pnx_data *i2c_pnx = alg_data->i2c_pnx; |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 698 | |
Russell King | 9d7f736 | 2009-11-21 12:25:27 +0000 | [diff] [blame^] | 699 | free_irq(i2c_pnx->irq, &alg_data->adapter); |
| 700 | i2c_del_adapter(&alg_data->adapter); |
Russell King | ebdbbf2 | 2009-11-20 11:44:46 +0000 | [diff] [blame] | 701 | clk_disable(alg_data->clk); |
Russell King | 88d968b | 2009-11-21 11:58:36 +0000 | [diff] [blame] | 702 | iounmap(alg_data->ioaddr); |
Russell King | 44c5d73 | 2009-11-21 12:10:54 +0000 | [diff] [blame] | 703 | release_mem_region(i2c_pnx->base, I2C_PNX_REGION_SIZE); |
Russell King | 0321cb8 | 2009-11-20 11:12:26 +0000 | [diff] [blame] | 704 | clk_put(alg_data->clk); |
Russell King | 44c5d73 | 2009-11-21 12:10:54 +0000 | [diff] [blame] | 705 | kfree(alg_data); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 706 | platform_set_drvdata(pdev, NULL); |
| 707 | |
| 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | static struct platform_driver i2c_pnx_driver = { |
| 712 | .driver = { |
| 713 | .name = "pnx-i2c", |
| 714 | .owner = THIS_MODULE, |
| 715 | }, |
| 716 | .probe = i2c_pnx_probe, |
| 717 | .remove = __devexit_p(i2c_pnx_remove), |
| 718 | .suspend = i2c_pnx_controller_suspend, |
| 719 | .resume = i2c_pnx_controller_resume, |
| 720 | }; |
| 721 | |
| 722 | static int __init i2c_adap_pnx_init(void) |
| 723 | { |
| 724 | return platform_driver_register(&i2c_pnx_driver); |
| 725 | } |
| 726 | |
| 727 | static void __exit i2c_adap_pnx_exit(void) |
| 728 | { |
| 729 | platform_driver_unregister(&i2c_pnx_driver); |
| 730 | } |
| 731 | |
| 732 | MODULE_AUTHOR("Vitaly Wool, Dennis Kovalev <source@mvista.com>"); |
| 733 | MODULE_DESCRIPTION("I2C driver for Philips IP3204-based I2C busses"); |
| 734 | MODULE_LICENSE("GPL"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 735 | MODULE_ALIAS("platform:pnx-i2c"); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 736 | |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 737 | /* We need to make sure I2C is initialized before USB */ |
| 738 | subsys_initcall(i2c_adap_pnx_init); |
Vitaly Wool | 41561f2 | 2006-12-10 21:21:29 +0100 | [diff] [blame] | 739 | module_exit(i2c_adap_pnx_exit); |