Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 1 | /* |
Mike Frysinger | bd58499 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 2 | * Blackfin On-Chip Two Wire Interface Driver |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 3 | * |
Mike Frysinger | bd58499 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 4 | * Copyright 2005-2007 Analog Devices Inc. |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 5 | * |
Mike Frysinger | bd58499 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 6 | * Enter bugs at http://blackfin.uclinux.org/ |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 7 | * |
Mike Frysinger | bd58499 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 8 | * Licensed under the GPL-2 or later. |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/i2c.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Mike Frysinger | 6df263c | 2009-06-14 01:55:37 -0400 | [diff] [blame] | 16 | #include <linux/io.h> |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 17 | #include <linux/mm.h> |
| 18 | #include <linux/timer.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/completion.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/platform_device.h> |
Michael Hennerich | 540ac55 | 2011-01-11 00:25:08 -0500 | [diff] [blame] | 23 | #include <linux/delay.h> |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 24 | |
| 25 | #include <asm/blackfin.h> |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 26 | #include <asm/portmux.h> |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 27 | #include <asm/irq.h> |
Sonic Zhang | c9d87ed | 2012-06-13 16:22:45 +0800 | [diff] [blame^] | 28 | #include <asm/bfin_twi.h> |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 29 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 30 | /* SMBus mode*/ |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 31 | #define TWI_I2C_MODE_STANDARD 1 |
| 32 | #define TWI_I2C_MODE_STANDARDSUB 2 |
| 33 | #define TWI_I2C_MODE_COMBINED 3 |
| 34 | #define TWI_I2C_MODE_REPEAT 4 |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 35 | |
| 36 | struct bfin_twi_iface { |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 37 | int irq; |
| 38 | spinlock_t lock; |
| 39 | char read_write; |
| 40 | u8 command; |
| 41 | u8 *transPtr; |
| 42 | int readNum; |
| 43 | int writeNum; |
| 44 | int cur_mode; |
| 45 | int manual_stop; |
| 46 | int result; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 47 | struct i2c_adapter adap; |
| 48 | struct completion complete; |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 49 | struct i2c_msg *pmsg; |
| 50 | int msg_num; |
| 51 | int cur_msg; |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 52 | u16 saved_clkdiv; |
| 53 | u16 saved_control; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 54 | void __iomem *regs_base; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 57 | |
| 58 | #define DEFINE_TWI_REG(reg, off) \ |
| 59 | static inline u16 read_##reg(struct bfin_twi_iface *iface) \ |
| 60 | { return bfin_read16(iface->regs_base + (off)); } \ |
| 61 | static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \ |
| 62 | { bfin_write16(iface->regs_base + (off), v); } |
| 63 | |
| 64 | DEFINE_TWI_REG(CLKDIV, 0x00) |
| 65 | DEFINE_TWI_REG(CONTROL, 0x04) |
| 66 | DEFINE_TWI_REG(SLAVE_CTL, 0x08) |
| 67 | DEFINE_TWI_REG(SLAVE_STAT, 0x0C) |
| 68 | DEFINE_TWI_REG(SLAVE_ADDR, 0x10) |
| 69 | DEFINE_TWI_REG(MASTER_CTL, 0x14) |
| 70 | DEFINE_TWI_REG(MASTER_STAT, 0x18) |
| 71 | DEFINE_TWI_REG(MASTER_ADDR, 0x1C) |
| 72 | DEFINE_TWI_REG(INT_STAT, 0x20) |
| 73 | DEFINE_TWI_REG(INT_MASK, 0x24) |
| 74 | DEFINE_TWI_REG(FIFO_CTL, 0x28) |
| 75 | DEFINE_TWI_REG(FIFO_STAT, 0x2C) |
| 76 | DEFINE_TWI_REG(XMT_DATA8, 0x80) |
| 77 | DEFINE_TWI_REG(XMT_DATA16, 0x84) |
| 78 | DEFINE_TWI_REG(RCV_DATA8, 0x88) |
| 79 | DEFINE_TWI_REG(RCV_DATA16, 0x8C) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 80 | |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 81 | static const u16 pin_req[2][3] = { |
| 82 | {P_TWI0_SCL, P_TWI0_SDA, 0}, |
| 83 | {P_TWI1_SCL, P_TWI1_SDA, 0}, |
| 84 | }; |
| 85 | |
Sonic Zhang | 5481d07 | 2010-03-22 03:23:18 -0400 | [diff] [blame] | 86 | static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface, |
| 87 | unsigned short twi_int_status) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 88 | { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 89 | unsigned short mast_stat = read_MASTER_STAT(iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 90 | |
| 91 | if (twi_int_status & XMTSERV) { |
| 92 | /* Transmit next data */ |
| 93 | if (iface->writeNum > 0) { |
Sonic Zhang | 5481d07 | 2010-03-22 03:23:18 -0400 | [diff] [blame] | 94 | SSYNC(); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 95 | write_XMT_DATA8(iface, *(iface->transPtr++)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 96 | iface->writeNum--; |
| 97 | } |
| 98 | /* start receive immediately after complete sending in |
| 99 | * combine mode. |
| 100 | */ |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 101 | else if (iface->cur_mode == TWI_I2C_MODE_COMBINED) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 102 | write_MASTER_CTL(iface, |
Sonic Zhang | 28a377c | 2012-06-13 16:22:44 +0800 | [diff] [blame] | 103 | read_MASTER_CTL(iface) | MDIR); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 104 | else if (iface->manual_stop) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 105 | write_MASTER_CTL(iface, |
| 106 | read_MASTER_CTL(iface) | STOP); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 107 | else if (iface->cur_mode == TWI_I2C_MODE_REPEAT && |
Frank Shew | 94327d0 | 2009-05-19 07:23:49 -0400 | [diff] [blame] | 108 | iface->cur_msg + 1 < iface->msg_num) { |
| 109 | if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD) |
| 110 | write_MASTER_CTL(iface, |
Sonic Zhang | 28a377c | 2012-06-13 16:22:44 +0800 | [diff] [blame] | 111 | read_MASTER_CTL(iface) | MDIR); |
Frank Shew | 94327d0 | 2009-05-19 07:23:49 -0400 | [diff] [blame] | 112 | else |
| 113 | write_MASTER_CTL(iface, |
Sonic Zhang | 28a377c | 2012-06-13 16:22:44 +0800 | [diff] [blame] | 114 | read_MASTER_CTL(iface) & ~MDIR); |
Frank Shew | 94327d0 | 2009-05-19 07:23:49 -0400 | [diff] [blame] | 115 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 116 | } |
| 117 | if (twi_int_status & RCVSERV) { |
| 118 | if (iface->readNum > 0) { |
| 119 | /* Receive next data */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 120 | *(iface->transPtr) = read_RCV_DATA8(iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 121 | if (iface->cur_mode == TWI_I2C_MODE_COMBINED) { |
| 122 | /* Change combine mode into sub mode after |
| 123 | * read first data. |
| 124 | */ |
| 125 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 126 | /* Get read number from first byte in block |
| 127 | * combine mode. |
| 128 | */ |
| 129 | if (iface->readNum == 1 && iface->manual_stop) |
| 130 | iface->readNum = *iface->transPtr + 1; |
| 131 | } |
| 132 | iface->transPtr++; |
| 133 | iface->readNum--; |
Sonic Zhang | a20a64d | 2012-06-13 16:22:41 +0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | if (iface->readNum == 0) { |
| 137 | if (iface->manual_stop) { |
| 138 | /* Temporary workaround to avoid possible bus stall - |
| 139 | * Flush FIFO before issuing the STOP condition |
| 140 | */ |
| 141 | read_RCV_DATA16(iface); |
Frank Shew | 94327d0 | 2009-05-19 07:23:49 -0400 | [diff] [blame] | 142 | write_MASTER_CTL(iface, |
Sonic Zhang | a20a64d | 2012-06-13 16:22:41 +0800 | [diff] [blame] | 143 | read_MASTER_CTL(iface) | STOP); |
| 144 | } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT && |
| 145 | iface->cur_msg + 1 < iface->msg_num) { |
| 146 | if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD) |
| 147 | write_MASTER_CTL(iface, |
Sonic Zhang | 28a377c | 2012-06-13 16:22:44 +0800 | [diff] [blame] | 148 | read_MASTER_CTL(iface) | MDIR); |
Sonic Zhang | a20a64d | 2012-06-13 16:22:41 +0800 | [diff] [blame] | 149 | else |
| 150 | write_MASTER_CTL(iface, |
Sonic Zhang | 28a377c | 2012-06-13 16:22:44 +0800 | [diff] [blame] | 151 | read_MASTER_CTL(iface) & ~MDIR); |
Sonic Zhang | a20a64d | 2012-06-13 16:22:41 +0800 | [diff] [blame] | 152 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 153 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 154 | } |
| 155 | if (twi_int_status & MERR) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 156 | write_INT_MASK(iface, 0); |
| 157 | write_MASTER_STAT(iface, 0x3e); |
| 158 | write_MASTER_CTL(iface, 0); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 159 | iface->result = -EIO; |
Michael Hennerich | 5cfafc1 | 2010-03-22 03:23:17 -0400 | [diff] [blame] | 160 | |
| 161 | if (mast_stat & LOSTARB) |
| 162 | dev_dbg(&iface->adap.dev, "Lost Arbitration\n"); |
| 163 | if (mast_stat & ANAK) |
| 164 | dev_dbg(&iface->adap.dev, "Address Not Acknowledged\n"); |
| 165 | if (mast_stat & DNAK) |
| 166 | dev_dbg(&iface->adap.dev, "Data Not Acknowledged\n"); |
| 167 | if (mast_stat & BUFRDERR) |
| 168 | dev_dbg(&iface->adap.dev, "Buffer Read Error\n"); |
| 169 | if (mast_stat & BUFWRERR) |
| 170 | dev_dbg(&iface->adap.dev, "Buffer Write Error\n"); |
| 171 | |
Michael Hennerich | 540ac55 | 2011-01-11 00:25:08 -0500 | [diff] [blame] | 172 | /* Faulty slave devices, may drive SDA low after a transfer |
| 173 | * finishes. To release the bus this code generates up to 9 |
| 174 | * extra clocks until SDA is released. |
| 175 | */ |
| 176 | |
| 177 | if (read_MASTER_STAT(iface) & SDASEN) { |
| 178 | int cnt = 9; |
| 179 | do { |
| 180 | write_MASTER_CTL(iface, SCLOVR); |
| 181 | udelay(6); |
| 182 | write_MASTER_CTL(iface, 0); |
| 183 | udelay(6); |
| 184 | } while ((read_MASTER_STAT(iface) & SDASEN) && cnt--); |
| 185 | |
| 186 | write_MASTER_CTL(iface, SDAOVR | SCLOVR); |
| 187 | udelay(6); |
| 188 | write_MASTER_CTL(iface, SDAOVR); |
| 189 | udelay(6); |
| 190 | write_MASTER_CTL(iface, 0); |
| 191 | } |
| 192 | |
Sonic Zhang | f0ac131 | 2010-03-22 03:23:20 -0400 | [diff] [blame] | 193 | /* If it is a quick transfer, only address without data, |
| 194 | * not an err, return 1. |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 195 | */ |
Sonic Zhang | f0ac131 | 2010-03-22 03:23:20 -0400 | [diff] [blame] | 196 | if (iface->cur_mode == TWI_I2C_MODE_STANDARD && |
| 197 | iface->transPtr == NULL && |
| 198 | (twi_int_status & MCOMP) && (mast_stat & DNAK)) |
| 199 | iface->result = 1; |
| 200 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 201 | complete(&iface->complete); |
| 202 | return; |
| 203 | } |
| 204 | if (twi_int_status & MCOMP) { |
Sonic Zhang | 2ee74eb | 2012-06-13 16:22:43 +0800 | [diff] [blame] | 205 | if (twi_int_status & (XMTSERV | RCVSERV) && |
| 206 | (read_MASTER_CTL(iface) & MEN) == 0 && |
Sonic Zhang | 4a65163 | 2011-06-23 17:07:54 -0400 | [diff] [blame] | 207 | (iface->cur_mode == TWI_I2C_MODE_REPEAT || |
| 208 | iface->cur_mode == TWI_I2C_MODE_COMBINED)) { |
| 209 | iface->result = -1; |
| 210 | write_INT_MASK(iface, 0); |
| 211 | write_MASTER_CTL(iface, 0); |
| 212 | } else if (iface->cur_mode == TWI_I2C_MODE_COMBINED) { |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 213 | if (iface->readNum == 0) { |
| 214 | /* set the read number to 1 and ask for manual |
| 215 | * stop in block combine mode |
| 216 | */ |
| 217 | iface->readNum = 1; |
| 218 | iface->manual_stop = 1; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 219 | write_MASTER_CTL(iface, |
| 220 | read_MASTER_CTL(iface) | (0xff << 6)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 221 | } else { |
| 222 | /* set the readd number in other |
| 223 | * combine mode. |
| 224 | */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 225 | write_MASTER_CTL(iface, |
| 226 | (read_MASTER_CTL(iface) & |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 227 | (~(0xff << 6))) | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 228 | (iface->readNum << 6)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 229 | } |
| 230 | /* remove restart bit and enable master receive */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 231 | write_MASTER_CTL(iface, |
| 232 | read_MASTER_CTL(iface) & ~RSTART); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 233 | } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT && |
Sonic Zhang | 28a377c | 2012-06-13 16:22:44 +0800 | [diff] [blame] | 234 | iface->cur_msg + 1 < iface->msg_num) { |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 235 | iface->cur_msg++; |
| 236 | iface->transPtr = iface->pmsg[iface->cur_msg].buf; |
| 237 | iface->writeNum = iface->readNum = |
| 238 | iface->pmsg[iface->cur_msg].len; |
| 239 | /* Set Transmit device address */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 240 | write_MASTER_ADDR(iface, |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 241 | iface->pmsg[iface->cur_msg].addr); |
| 242 | if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD) |
| 243 | iface->read_write = I2C_SMBUS_READ; |
| 244 | else { |
| 245 | iface->read_write = I2C_SMBUS_WRITE; |
| 246 | /* Transmit first data */ |
| 247 | if (iface->writeNum > 0) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 248 | write_XMT_DATA8(iface, |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 249 | *(iface->transPtr++)); |
| 250 | iface->writeNum--; |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Sonic Zhang | a20a64d | 2012-06-13 16:22:41 +0800 | [diff] [blame] | 254 | if (iface->pmsg[iface->cur_msg].len <= 255) { |
| 255 | write_MASTER_CTL(iface, |
Sonic Zhang | 57a8f32 | 2009-05-19 07:21:58 -0400 | [diff] [blame] | 256 | (read_MASTER_CTL(iface) & |
| 257 | (~(0xff << 6))) | |
Sonic Zhang | a20a64d | 2012-06-13 16:22:41 +0800 | [diff] [blame] | 258 | (iface->pmsg[iface->cur_msg].len << 6)); |
| 259 | iface->manual_stop = 0; |
| 260 | } else { |
Sonic Zhang | 57a8f32 | 2009-05-19 07:21:58 -0400 | [diff] [blame] | 261 | write_MASTER_CTL(iface, |
| 262 | (read_MASTER_CTL(iface) | |
| 263 | (0xff << 6))); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 264 | iface->manual_stop = 1; |
| 265 | } |
Sonic Zhang | 28a377c | 2012-06-13 16:22:44 +0800 | [diff] [blame] | 266 | /* remove restart bit before last message */ |
| 267 | if (iface->cur_msg + 1 == iface->msg_num) |
| 268 | write_MASTER_CTL(iface, |
| 269 | read_MASTER_CTL(iface) & ~RSTART); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 270 | } else { |
| 271 | iface->result = 1; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 272 | write_INT_MASK(iface, 0); |
| 273 | write_MASTER_CTL(iface, 0); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 274 | } |
Sonic Zhang | a20a64d | 2012-06-13 16:22:41 +0800 | [diff] [blame] | 275 | complete(&iface->complete); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | /* Interrupt handler */ |
| 280 | static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id) |
| 281 | { |
| 282 | struct bfin_twi_iface *iface = dev_id; |
| 283 | unsigned long flags; |
Sonic Zhang | 5481d07 | 2010-03-22 03:23:18 -0400 | [diff] [blame] | 284 | unsigned short twi_int_status; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 285 | |
| 286 | spin_lock_irqsave(&iface->lock, flags); |
Sonic Zhang | 5481d07 | 2010-03-22 03:23:18 -0400 | [diff] [blame] | 287 | while (1) { |
| 288 | twi_int_status = read_INT_STAT(iface); |
| 289 | if (!twi_int_status) |
| 290 | break; |
| 291 | /* Clear interrupt status */ |
| 292 | write_INT_STAT(iface, twi_int_status); |
| 293 | bfin_twi_handle_interrupt(iface, twi_int_status); |
| 294 | SSYNC(); |
| 295 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 296 | spin_unlock_irqrestore(&iface->lock, flags); |
| 297 | return IRQ_HANDLED; |
| 298 | } |
| 299 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 300 | /* |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 301 | * One i2c master transfer |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 302 | */ |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 303 | static int bfin_twi_do_master_xfer(struct i2c_adapter *adap, |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 304 | struct i2c_msg *msgs, int num) |
| 305 | { |
| 306 | struct bfin_twi_iface *iface = adap->algo_data; |
| 307 | struct i2c_msg *pmsg; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 308 | int rc = 0; |
| 309 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 310 | if (!(read_CONTROL(iface) & TWI_ENA)) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 311 | return -ENXIO; |
| 312 | |
Sonic Zhang | a25733d | 2012-06-13 16:22:42 +0800 | [diff] [blame] | 313 | if (read_MASTER_STAT(iface) & BUSBUSY) |
| 314 | return -EAGAIN; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 315 | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 316 | iface->pmsg = msgs; |
| 317 | iface->msg_num = num; |
| 318 | iface->cur_msg = 0; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 319 | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 320 | pmsg = &msgs[0]; |
| 321 | if (pmsg->flags & I2C_M_TEN) { |
| 322 | dev_err(&adap->dev, "10 bits addr not supported!\n"); |
| 323 | return -EINVAL; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Sonic Zhang | 28a377c | 2012-06-13 16:22:44 +0800 | [diff] [blame] | 326 | if (iface->msg_num > 1) |
| 327 | iface->cur_mode = TWI_I2C_MODE_REPEAT; |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 328 | iface->manual_stop = 0; |
| 329 | iface->transPtr = pmsg->buf; |
| 330 | iface->writeNum = iface->readNum = pmsg->len; |
| 331 | iface->result = 0; |
Hans Schillstrom | afc13b7 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 332 | init_completion(&(iface->complete)); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 333 | /* Set Transmit device address */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 334 | write_MASTER_ADDR(iface, pmsg->addr); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 335 | |
| 336 | /* FIFO Initiation. Data in FIFO should be |
| 337 | * discarded before start a new operation. |
| 338 | */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 339 | write_FIFO_CTL(iface, 0x3); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 340 | SSYNC(); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 341 | write_FIFO_CTL(iface, 0); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 342 | SSYNC(); |
| 343 | |
| 344 | if (pmsg->flags & I2C_M_RD) |
| 345 | iface->read_write = I2C_SMBUS_READ; |
| 346 | else { |
| 347 | iface->read_write = I2C_SMBUS_WRITE; |
| 348 | /* Transmit first data */ |
| 349 | if (iface->writeNum > 0) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 350 | write_XMT_DATA8(iface, *(iface->transPtr++)); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 351 | iface->writeNum--; |
| 352 | SSYNC(); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /* clear int stat */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 357 | write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 358 | |
| 359 | /* Interrupt mask . Enable XMT, RCV interrupt */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 360 | write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 361 | SSYNC(); |
| 362 | |
| 363 | if (pmsg->len <= 255) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 364 | write_MASTER_CTL(iface, pmsg->len << 6); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 365 | else { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 366 | write_MASTER_CTL(iface, 0xff << 6); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 367 | iface->manual_stop = 1; |
| 368 | } |
| 369 | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 370 | /* Master enable */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 371 | write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | |
Sonic Zhang | 28a377c | 2012-06-13 16:22:44 +0800 | [diff] [blame] | 372 | (iface->msg_num > 1 ? RSTART : 0) | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 373 | ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) | |
| 374 | ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0)); |
| 375 | SSYNC(); |
| 376 | |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 377 | while (!iface->result) { |
| 378 | if (!wait_for_completion_timeout(&iface->complete, |
| 379 | adap->timeout)) { |
| 380 | iface->result = -1; |
| 381 | dev_err(&adap->dev, "master transfer timeout\n"); |
| 382 | } |
| 383 | } |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 384 | |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 385 | if (iface->result == 1) |
| 386 | rc = iface->cur_msg + 1; |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 387 | else |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 388 | rc = iface->result; |
| 389 | |
| 390 | return rc; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /* |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 394 | * Generic i2c master transfer entrypoint |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 395 | */ |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 396 | static int bfin_twi_master_xfer(struct i2c_adapter *adap, |
| 397 | struct i2c_msg *msgs, int num) |
| 398 | { |
Sonic Zhang | be2f80f | 2010-03-22 03:23:19 -0400 | [diff] [blame] | 399 | return bfin_twi_do_master_xfer(adap, msgs, num); |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* |
| 403 | * One I2C SMBus transfer |
| 404 | */ |
| 405 | int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr, |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 406 | unsigned short flags, char read_write, |
| 407 | u8 command, int size, union i2c_smbus_data *data) |
| 408 | { |
| 409 | struct bfin_twi_iface *iface = adap->algo_data; |
| 410 | int rc = 0; |
| 411 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 412 | if (!(read_CONTROL(iface) & TWI_ENA)) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 413 | return -ENXIO; |
| 414 | |
Sonic Zhang | a25733d | 2012-06-13 16:22:42 +0800 | [diff] [blame] | 415 | if (read_MASTER_STAT(iface) & BUSBUSY) |
| 416 | return -EAGAIN; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 417 | |
| 418 | iface->writeNum = 0; |
| 419 | iface->readNum = 0; |
| 420 | |
| 421 | /* Prepare datas & select mode */ |
| 422 | switch (size) { |
| 423 | case I2C_SMBUS_QUICK: |
| 424 | iface->transPtr = NULL; |
| 425 | iface->cur_mode = TWI_I2C_MODE_STANDARD; |
| 426 | break; |
| 427 | case I2C_SMBUS_BYTE: |
| 428 | if (data == NULL) |
| 429 | iface->transPtr = NULL; |
| 430 | else { |
| 431 | if (read_write == I2C_SMBUS_READ) |
| 432 | iface->readNum = 1; |
| 433 | else |
| 434 | iface->writeNum = 1; |
| 435 | iface->transPtr = &data->byte; |
| 436 | } |
| 437 | iface->cur_mode = TWI_I2C_MODE_STANDARD; |
| 438 | break; |
| 439 | case I2C_SMBUS_BYTE_DATA: |
| 440 | if (read_write == I2C_SMBUS_READ) { |
| 441 | iface->readNum = 1; |
| 442 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 443 | } else { |
| 444 | iface->writeNum = 1; |
| 445 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 446 | } |
| 447 | iface->transPtr = &data->byte; |
| 448 | break; |
| 449 | case I2C_SMBUS_WORD_DATA: |
| 450 | if (read_write == I2C_SMBUS_READ) { |
| 451 | iface->readNum = 2; |
| 452 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 453 | } else { |
| 454 | iface->writeNum = 2; |
| 455 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 456 | } |
| 457 | iface->transPtr = (u8 *)&data->word; |
| 458 | break; |
| 459 | case I2C_SMBUS_PROC_CALL: |
| 460 | iface->writeNum = 2; |
| 461 | iface->readNum = 2; |
| 462 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 463 | iface->transPtr = (u8 *)&data->word; |
| 464 | break; |
| 465 | case I2C_SMBUS_BLOCK_DATA: |
| 466 | if (read_write == I2C_SMBUS_READ) { |
| 467 | iface->readNum = 0; |
| 468 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 469 | } else { |
| 470 | iface->writeNum = data->block[0] + 1; |
| 471 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 472 | } |
| 473 | iface->transPtr = data->block; |
| 474 | break; |
Michael Hennerich | e0cd2dd | 2009-05-27 09:24:10 +0000 | [diff] [blame] | 475 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 476 | if (read_write == I2C_SMBUS_READ) { |
| 477 | iface->readNum = data->block[0]; |
| 478 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 479 | } else { |
| 480 | iface->writeNum = data->block[0]; |
| 481 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 482 | } |
| 483 | iface->transPtr = (u8 *)&data->block[1]; |
| 484 | break; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 485 | default: |
| 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | iface->result = 0; |
| 490 | iface->manual_stop = 0; |
| 491 | iface->read_write = read_write; |
| 492 | iface->command = command; |
Hans Schillstrom | afc13b7 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 493 | init_completion(&(iface->complete)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 494 | |
| 495 | /* FIFO Initiation. Data in FIFO should be discarded before |
| 496 | * start a new operation. |
| 497 | */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 498 | write_FIFO_CTL(iface, 0x3); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 499 | SSYNC(); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 500 | write_FIFO_CTL(iface, 0); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 501 | |
| 502 | /* clear int stat */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 503 | write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 504 | |
| 505 | /* Set Transmit device address */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 506 | write_MASTER_ADDR(iface, addr); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 507 | SSYNC(); |
| 508 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 509 | switch (iface->cur_mode) { |
| 510 | case TWI_I2C_MODE_STANDARDSUB: |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 511 | write_XMT_DATA8(iface, iface->command); |
| 512 | write_INT_MASK(iface, MCOMP | MERR | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 513 | ((iface->read_write == I2C_SMBUS_READ) ? |
| 514 | RCVSERV : XMTSERV)); |
| 515 | SSYNC(); |
| 516 | |
| 517 | if (iface->writeNum + 1 <= 255) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 518 | write_MASTER_CTL(iface, (iface->writeNum + 1) << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 519 | else { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 520 | write_MASTER_CTL(iface, 0xff << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 521 | iface->manual_stop = 1; |
| 522 | } |
| 523 | /* Master enable */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 524 | write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 525 | ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0)); |
| 526 | break; |
| 527 | case TWI_I2C_MODE_COMBINED: |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 528 | write_XMT_DATA8(iface, iface->command); |
| 529 | write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 530 | SSYNC(); |
| 531 | |
| 532 | if (iface->writeNum > 0) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 533 | write_MASTER_CTL(iface, (iface->writeNum + 1) << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 534 | else |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 535 | write_MASTER_CTL(iface, 0x1 << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 536 | /* Master enable */ |
Sonic Zhang | 28a377c | 2012-06-13 16:22:44 +0800 | [diff] [blame] | 537 | write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | RSTART | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 538 | ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0)); |
| 539 | break; |
| 540 | default: |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 541 | write_MASTER_CTL(iface, 0); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 542 | if (size != I2C_SMBUS_QUICK) { |
| 543 | /* Don't access xmit data register when this is a |
| 544 | * read operation. |
| 545 | */ |
| 546 | if (iface->read_write != I2C_SMBUS_READ) { |
| 547 | if (iface->writeNum > 0) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 548 | write_XMT_DATA8(iface, |
| 549 | *(iface->transPtr++)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 550 | if (iface->writeNum <= 255) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 551 | write_MASTER_CTL(iface, |
| 552 | iface->writeNum << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 553 | else { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 554 | write_MASTER_CTL(iface, |
| 555 | 0xff << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 556 | iface->manual_stop = 1; |
| 557 | } |
| 558 | iface->writeNum--; |
| 559 | } else { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 560 | write_XMT_DATA8(iface, iface->command); |
| 561 | write_MASTER_CTL(iface, 1 << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 562 | } |
| 563 | } else { |
| 564 | if (iface->readNum > 0 && iface->readNum <= 255) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 565 | write_MASTER_CTL(iface, |
| 566 | iface->readNum << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 567 | else if (iface->readNum > 255) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 568 | write_MASTER_CTL(iface, 0xff << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 569 | iface->manual_stop = 1; |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 570 | } else |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 571 | break; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 572 | } |
| 573 | } |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 574 | write_INT_MASK(iface, MCOMP | MERR | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 575 | ((iface->read_write == I2C_SMBUS_READ) ? |
| 576 | RCVSERV : XMTSERV)); |
| 577 | SSYNC(); |
| 578 | |
| 579 | /* Master enable */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 580 | write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 581 | ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) | |
| 582 | ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0)); |
| 583 | break; |
| 584 | } |
| 585 | SSYNC(); |
| 586 | |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 587 | while (!iface->result) { |
| 588 | if (!wait_for_completion_timeout(&iface->complete, |
| 589 | adap->timeout)) { |
| 590 | iface->result = -1; |
| 591 | dev_err(&adap->dev, "smbus transfer timeout\n"); |
| 592 | } |
| 593 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 594 | |
| 595 | rc = (iface->result >= 0) ? 0 : -1; |
| 596 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 597 | return rc; |
| 598 | } |
| 599 | |
| 600 | /* |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 601 | * Generic I2C SMBus transfer entrypoint |
| 602 | */ |
| 603 | int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr, |
| 604 | unsigned short flags, char read_write, |
| 605 | u8 command, int size, union i2c_smbus_data *data) |
| 606 | { |
Sonic Zhang | be2f80f | 2010-03-22 03:23:19 -0400 | [diff] [blame] | 607 | return bfin_twi_do_smbus_xfer(adap, addr, flags, |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 608 | read_write, command, size, data); |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 612 | * Return what the adapter supports |
| 613 | */ |
| 614 | static u32 bfin_twi_functionality(struct i2c_adapter *adap) |
| 615 | { |
| 616 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 617 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | |
| 618 | I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL | |
Michael Hennerich | e0cd2dd | 2009-05-27 09:24:10 +0000 | [diff] [blame] | 619 | I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 622 | static struct i2c_algorithm bfin_twi_algorithm = { |
| 623 | .master_xfer = bfin_twi_master_xfer, |
| 624 | .smbus_xfer = bfin_twi_smbus_xfer, |
| 625 | .functionality = bfin_twi_functionality, |
| 626 | }; |
| 627 | |
Rafael J. Wysocki | 85777ad | 2012-07-11 21:23:31 +0200 | [diff] [blame] | 628 | static int i2c_bfin_twi_suspend(struct device *dev) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 629 | { |
Rafael J. Wysocki | 85777ad | 2012-07-11 21:23:31 +0200 | [diff] [blame] | 630 | struct bfin_twi_iface *iface = dev_get_drvdata(dev); |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 631 | |
| 632 | iface->saved_clkdiv = read_CLKDIV(iface); |
| 633 | iface->saved_control = read_CONTROL(iface); |
| 634 | |
| 635 | free_irq(iface->irq, iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 636 | |
| 637 | /* Disable TWI */ |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 638 | write_CONTROL(iface, iface->saved_control & ~TWI_ENA); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
Rafael J. Wysocki | 85777ad | 2012-07-11 21:23:31 +0200 | [diff] [blame] | 643 | static int i2c_bfin_twi_resume(struct device *dev) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 644 | { |
Rafael J. Wysocki | 85777ad | 2012-07-11 21:23:31 +0200 | [diff] [blame] | 645 | struct bfin_twi_iface *iface = dev_get_drvdata(dev); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 646 | |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 647 | int rc = request_irq(iface->irq, bfin_twi_interrupt_entry, |
Rafael J. Wysocki | 85777ad | 2012-07-11 21:23:31 +0200 | [diff] [blame] | 648 | 0, to_platform_device(dev)->name, iface); |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 649 | if (rc) { |
Rafael J. Wysocki | 85777ad | 2012-07-11 21:23:31 +0200 | [diff] [blame] | 650 | dev_err(dev, "Can't get IRQ %d !\n", iface->irq); |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 651 | return -ENODEV; |
| 652 | } |
| 653 | |
| 654 | /* Resume TWI interface clock as specified */ |
| 655 | write_CLKDIV(iface, iface->saved_clkdiv); |
| 656 | |
| 657 | /* Resume TWI */ |
| 658 | write_CONTROL(iface, iface->saved_control); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
Rafael J. Wysocki | 85777ad | 2012-07-11 21:23:31 +0200 | [diff] [blame] | 663 | static SIMPLE_DEV_PM_OPS(i2c_bfin_twi_pm, |
| 664 | i2c_bfin_twi_suspend, i2c_bfin_twi_resume); |
| 665 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 666 | static int i2c_bfin_twi_probe(struct platform_device *pdev) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 667 | { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 668 | struct bfin_twi_iface *iface; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 669 | struct i2c_adapter *p_adap; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 670 | struct resource *res; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 671 | int rc; |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 672 | unsigned int clkhilow; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 673 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 674 | iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL); |
| 675 | if (!iface) { |
| 676 | dev_err(&pdev->dev, "Cannot allocate memory\n"); |
| 677 | rc = -ENOMEM; |
| 678 | goto out_error_nomem; |
| 679 | } |
| 680 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 681 | spin_lock_init(&(iface->lock)); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 682 | |
| 683 | /* Find and map our resources */ |
| 684 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 685 | if (res == NULL) { |
| 686 | dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n"); |
| 687 | rc = -ENOENT; |
| 688 | goto out_error_get_res; |
| 689 | } |
| 690 | |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 691 | iface->regs_base = ioremap(res->start, resource_size(res)); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 692 | if (iface->regs_base == NULL) { |
| 693 | dev_err(&pdev->dev, "Cannot map IO\n"); |
| 694 | rc = -ENXIO; |
| 695 | goto out_error_ioremap; |
| 696 | } |
| 697 | |
| 698 | iface->irq = platform_get_irq(pdev, 0); |
| 699 | if (iface->irq < 0) { |
| 700 | dev_err(&pdev->dev, "No IRQ specified\n"); |
| 701 | rc = -ENOENT; |
| 702 | goto out_error_no_irq; |
| 703 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 704 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 705 | p_adap = &iface->adap; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 706 | p_adap->nr = pdev->id; |
| 707 | strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 708 | p_adap->algo = &bfin_twi_algorithm; |
| 709 | p_adap->algo_data = iface; |
Jean Delvare | e1995f6 | 2009-01-07 14:29:16 +0100 | [diff] [blame] | 710 | p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 711 | p_adap->dev.parent = &pdev->dev; |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 712 | p_adap->timeout = 5 * HZ; |
| 713 | p_adap->retries = 3; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 714 | |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 715 | rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi"); |
| 716 | if (rc) { |
| 717 | dev_err(&pdev->dev, "Can't setup pin mux!\n"); |
| 718 | goto out_error_pin_mux; |
| 719 | } |
| 720 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 721 | rc = request_irq(iface->irq, bfin_twi_interrupt_entry, |
Yong Zhang | 4311051 | 2011-09-21 17:28:33 +0800 | [diff] [blame] | 722 | 0, pdev->name, iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 723 | if (rc) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 724 | dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq); |
| 725 | rc = -ENODEV; |
| 726 | goto out_error_req_irq; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /* Set TWI internal clock as 10MHz */ |
Sonic Zhang | ac07fb4 | 2009-12-21 09:28:30 -0500 | [diff] [blame] | 730 | write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 731 | |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 732 | /* |
| 733 | * We will not end up with a CLKDIV=0 because no one will specify |
Sonic Zhang | ac07fb4 | 2009-12-21 09:28:30 -0500 | [diff] [blame] | 734 | * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250) |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 735 | */ |
Sonic Zhang | ac07fb4 | 2009-12-21 09:28:30 -0500 | [diff] [blame] | 736 | clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2; |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 737 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 738 | /* Set Twi interface clock as specified */ |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 739 | write_CLKDIV(iface, (clkhilow << 8) | clkhilow); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 740 | |
| 741 | /* Enable TWI */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 742 | write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 743 | SSYNC(); |
| 744 | |
Kalle Pokki | 991dee5 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 745 | rc = i2c_add_numbered_adapter(p_adap); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 746 | if (rc < 0) { |
| 747 | dev_err(&pdev->dev, "Can't add i2c adapter!\n"); |
| 748 | goto out_error_add_adapter; |
| 749 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 750 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 751 | platform_set_drvdata(pdev, iface); |
| 752 | |
Bryan Wu | fa6ad22 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 753 | dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, " |
| 754 | "regs_base@%p\n", iface->regs_base); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 755 | |
| 756 | return 0; |
| 757 | |
| 758 | out_error_add_adapter: |
| 759 | free_irq(iface->irq, iface); |
| 760 | out_error_req_irq: |
| 761 | out_error_no_irq: |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 762 | peripheral_free_list(pin_req[pdev->id]); |
| 763 | out_error_pin_mux: |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 764 | iounmap(iface->regs_base); |
| 765 | out_error_ioremap: |
| 766 | out_error_get_res: |
| 767 | kfree(iface); |
| 768 | out_error_nomem: |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 769 | return rc; |
| 770 | } |
| 771 | |
| 772 | static int i2c_bfin_twi_remove(struct platform_device *pdev) |
| 773 | { |
| 774 | struct bfin_twi_iface *iface = platform_get_drvdata(pdev); |
| 775 | |
| 776 | platform_set_drvdata(pdev, NULL); |
| 777 | |
| 778 | i2c_del_adapter(&(iface->adap)); |
| 779 | free_irq(iface->irq, iface); |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 780 | peripheral_free_list(pin_req[pdev->id]); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 781 | iounmap(iface->regs_base); |
| 782 | kfree(iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static struct platform_driver i2c_bfin_twi_driver = { |
| 788 | .probe = i2c_bfin_twi_probe, |
| 789 | .remove = i2c_bfin_twi_remove, |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 790 | .driver = { |
| 791 | .name = "i2c-bfin-twi", |
| 792 | .owner = THIS_MODULE, |
Rafael J. Wysocki | 85777ad | 2012-07-11 21:23:31 +0200 | [diff] [blame] | 793 | .pm = &i2c_bfin_twi_pm, |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 794 | }, |
| 795 | }; |
| 796 | |
| 797 | static int __init i2c_bfin_twi_init(void) |
| 798 | { |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 799 | return platform_driver_register(&i2c_bfin_twi_driver); |
| 800 | } |
| 801 | |
| 802 | static void __exit i2c_bfin_twi_exit(void) |
| 803 | { |
| 804 | platform_driver_unregister(&i2c_bfin_twi_driver); |
| 805 | } |
| 806 | |
Michael Hennerich | 74f56c4 | 2011-01-11 00:25:09 -0500 | [diff] [blame] | 807 | subsys_initcall(i2c_bfin_twi_init); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 808 | module_exit(i2c_bfin_twi_exit); |
Bryan Wu | fa6ad22 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 809 | |
| 810 | MODULE_AUTHOR("Bryan Wu, Sonic Zhang"); |
| 811 | MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver"); |
| 812 | MODULE_LICENSE("GPL"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 813 | MODULE_ALIAS("platform:i2c-bfin-twi"); |