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