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