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> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/timer.h> |
| 17 | #include <linux/spinlock.h> |
| 18 | #include <linux/completion.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | |
| 22 | #include <asm/blackfin.h> |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 23 | #include <asm/portmux.h> |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 24 | #include <asm/irq.h> |
| 25 | |
| 26 | #define POLL_TIMEOUT (2 * HZ) |
| 27 | |
| 28 | /* SMBus mode*/ |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 29 | #define TWI_I2C_MODE_STANDARD 1 |
| 30 | #define TWI_I2C_MODE_STANDARDSUB 2 |
| 31 | #define TWI_I2C_MODE_COMBINED 3 |
| 32 | #define TWI_I2C_MODE_REPEAT 4 |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 33 | |
| 34 | struct bfin_twi_iface { |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 35 | int irq; |
| 36 | spinlock_t lock; |
| 37 | char read_write; |
| 38 | u8 command; |
| 39 | u8 *transPtr; |
| 40 | int readNum; |
| 41 | int writeNum; |
| 42 | int cur_mode; |
| 43 | int manual_stop; |
| 44 | int result; |
| 45 | int timeout_count; |
| 46 | struct timer_list timeout_timer; |
| 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 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 86 | static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface) |
| 87 | { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 88 | unsigned short twi_int_status = read_INT_STAT(iface); |
| 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) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 94 | write_XMT_DATA8(iface, *(iface->transPtr++)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 95 | iface->writeNum--; |
| 96 | } |
| 97 | /* start receive immediately after complete sending in |
| 98 | * combine mode. |
| 99 | */ |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 100 | else if (iface->cur_mode == TWI_I2C_MODE_COMBINED) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 101 | write_MASTER_CTL(iface, |
| 102 | read_MASTER_CTL(iface) | MDIR | RSTART); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 103 | else if (iface->manual_stop) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 104 | write_MASTER_CTL(iface, |
| 105 | read_MASTER_CTL(iface) | STOP); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 106 | else if (iface->cur_mode == TWI_I2C_MODE_REPEAT && |
| 107 | iface->cur_msg+1 < iface->msg_num) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 108 | write_MASTER_CTL(iface, |
| 109 | read_MASTER_CTL(iface) | RSTART); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 110 | SSYNC(); |
| 111 | /* Clear status */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 112 | write_INT_STAT(iface, XMTSERV); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 113 | SSYNC(); |
| 114 | } |
| 115 | if (twi_int_status & RCVSERV) { |
| 116 | if (iface->readNum > 0) { |
| 117 | /* Receive next data */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 118 | *(iface->transPtr) = read_RCV_DATA8(iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 119 | if (iface->cur_mode == TWI_I2C_MODE_COMBINED) { |
| 120 | /* Change combine mode into sub mode after |
| 121 | * read first data. |
| 122 | */ |
| 123 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 124 | /* Get read number from first byte in block |
| 125 | * combine mode. |
| 126 | */ |
| 127 | if (iface->readNum == 1 && iface->manual_stop) |
| 128 | iface->readNum = *iface->transPtr + 1; |
| 129 | } |
| 130 | iface->transPtr++; |
| 131 | iface->readNum--; |
| 132 | } else if (iface->manual_stop) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 133 | write_MASTER_CTL(iface, |
| 134 | read_MASTER_CTL(iface) | STOP); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 135 | SSYNC(); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 136 | } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT && |
| 137 | iface->cur_msg+1 < iface->msg_num) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 138 | write_MASTER_CTL(iface, |
| 139 | read_MASTER_CTL(iface) | RSTART); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 140 | SSYNC(); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 141 | } |
| 142 | /* Clear interrupt source */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 143 | write_INT_STAT(iface, RCVSERV); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 144 | SSYNC(); |
| 145 | } |
| 146 | if (twi_int_status & MERR) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 147 | write_INT_STAT(iface, MERR); |
| 148 | write_INT_MASK(iface, 0); |
| 149 | write_MASTER_STAT(iface, 0x3e); |
| 150 | write_MASTER_CTL(iface, 0); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 151 | SSYNC(); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 152 | iface->result = -EIO; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 153 | /* if both err and complete int stats are set, return proper |
| 154 | * results. |
| 155 | */ |
| 156 | if (twi_int_status & MCOMP) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 157 | write_INT_STAT(iface, MCOMP); |
| 158 | write_INT_MASK(iface, 0); |
| 159 | write_MASTER_CTL(iface, 0); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 160 | SSYNC(); |
| 161 | /* If it is a quick transfer, only address bug no data, |
| 162 | * not an err, return 1. |
| 163 | */ |
| 164 | if (iface->writeNum == 0 && (mast_stat & BUFRDERR)) |
| 165 | iface->result = 1; |
| 166 | /* If address not acknowledged return -1, |
| 167 | * else return 0. |
| 168 | */ |
| 169 | else if (!(mast_stat & ANAK)) |
| 170 | iface->result = 0; |
| 171 | } |
| 172 | complete(&iface->complete); |
| 173 | return; |
| 174 | } |
| 175 | if (twi_int_status & MCOMP) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 176 | write_INT_STAT(iface, MCOMP); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 177 | SSYNC(); |
| 178 | if (iface->cur_mode == TWI_I2C_MODE_COMBINED) { |
| 179 | if (iface->readNum == 0) { |
| 180 | /* set the read number to 1 and ask for manual |
| 181 | * stop in block combine mode |
| 182 | */ |
| 183 | iface->readNum = 1; |
| 184 | iface->manual_stop = 1; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 185 | write_MASTER_CTL(iface, |
| 186 | read_MASTER_CTL(iface) | (0xff << 6)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 187 | } else { |
| 188 | /* set the readd number in other |
| 189 | * combine mode. |
| 190 | */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 191 | write_MASTER_CTL(iface, |
| 192 | (read_MASTER_CTL(iface) & |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 193 | (~(0xff << 6))) | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 194 | (iface->readNum << 6)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 195 | } |
| 196 | /* remove restart bit and enable master receive */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 197 | write_MASTER_CTL(iface, |
| 198 | read_MASTER_CTL(iface) & ~RSTART); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 199 | SSYNC(); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 200 | } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT && |
| 201 | iface->cur_msg+1 < iface->msg_num) { |
| 202 | iface->cur_msg++; |
| 203 | iface->transPtr = iface->pmsg[iface->cur_msg].buf; |
| 204 | iface->writeNum = iface->readNum = |
| 205 | iface->pmsg[iface->cur_msg].len; |
| 206 | /* Set Transmit device address */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 207 | write_MASTER_ADDR(iface, |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 208 | iface->pmsg[iface->cur_msg].addr); |
| 209 | if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD) |
| 210 | iface->read_write = I2C_SMBUS_READ; |
| 211 | else { |
| 212 | iface->read_write = I2C_SMBUS_WRITE; |
| 213 | /* Transmit first data */ |
| 214 | if (iface->writeNum > 0) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 215 | write_XMT_DATA8(iface, |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 216 | *(iface->transPtr++)); |
| 217 | iface->writeNum--; |
| 218 | SSYNC(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (iface->pmsg[iface->cur_msg].len <= 255) |
Sonic Zhang | 57a8f32 | 2009-05-19 07:21:58 -0400 | [diff] [blame^] | 223 | write_MASTER_CTL(iface, |
| 224 | (read_MASTER_CTL(iface) & |
| 225 | (~(0xff << 6))) | |
| 226 | (iface->pmsg[iface->cur_msg].len << 6)); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 227 | else { |
Sonic Zhang | 57a8f32 | 2009-05-19 07:21:58 -0400 | [diff] [blame^] | 228 | write_MASTER_CTL(iface, |
| 229 | (read_MASTER_CTL(iface) | |
| 230 | (0xff << 6))); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 231 | iface->manual_stop = 1; |
| 232 | } |
| 233 | /* remove restart bit and enable master receive */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 234 | write_MASTER_CTL(iface, |
| 235 | read_MASTER_CTL(iface) & ~RSTART); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 236 | SSYNC(); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 237 | } else { |
| 238 | iface->result = 1; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 239 | write_INT_MASK(iface, 0); |
| 240 | write_MASTER_CTL(iface, 0); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 241 | SSYNC(); |
| 242 | complete(&iface->complete); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /* Interrupt handler */ |
| 248 | static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id) |
| 249 | { |
| 250 | struct bfin_twi_iface *iface = dev_id; |
| 251 | unsigned long flags; |
| 252 | |
| 253 | spin_lock_irqsave(&iface->lock, flags); |
| 254 | del_timer(&iface->timeout_timer); |
| 255 | bfin_twi_handle_interrupt(iface); |
| 256 | spin_unlock_irqrestore(&iface->lock, flags); |
| 257 | return IRQ_HANDLED; |
| 258 | } |
| 259 | |
| 260 | static void bfin_twi_timeout(unsigned long data) |
| 261 | { |
| 262 | struct bfin_twi_iface *iface = (struct bfin_twi_iface *)data; |
| 263 | unsigned long flags; |
| 264 | |
| 265 | spin_lock_irqsave(&iface->lock, flags); |
| 266 | bfin_twi_handle_interrupt(iface); |
| 267 | if (iface->result == 0) { |
| 268 | iface->timeout_count--; |
| 269 | if (iface->timeout_count > 0) { |
| 270 | iface->timeout_timer.expires = jiffies + POLL_TIMEOUT; |
| 271 | add_timer(&iface->timeout_timer); |
| 272 | } else { |
| 273 | iface->result = -1; |
| 274 | complete(&iface->complete); |
| 275 | } |
| 276 | } |
| 277 | spin_unlock_irqrestore(&iface->lock, flags); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Generic i2c master transfer entrypoint |
| 282 | */ |
| 283 | static int bfin_twi_master_xfer(struct i2c_adapter *adap, |
| 284 | struct i2c_msg *msgs, int num) |
| 285 | { |
| 286 | struct bfin_twi_iface *iface = adap->algo_data; |
| 287 | struct i2c_msg *pmsg; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 288 | int rc = 0; |
| 289 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 290 | if (!(read_CONTROL(iface) & TWI_ENA)) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 291 | return -ENXIO; |
| 292 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 293 | while (read_MASTER_STAT(iface) & BUSBUSY) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 294 | yield(); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 295 | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 296 | iface->pmsg = msgs; |
| 297 | iface->msg_num = num; |
| 298 | iface->cur_msg = 0; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 299 | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 300 | pmsg = &msgs[0]; |
| 301 | if (pmsg->flags & I2C_M_TEN) { |
| 302 | dev_err(&adap->dev, "10 bits addr not supported!\n"); |
| 303 | return -EINVAL; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 306 | iface->cur_mode = TWI_I2C_MODE_REPEAT; |
| 307 | iface->manual_stop = 0; |
| 308 | iface->transPtr = pmsg->buf; |
| 309 | iface->writeNum = iface->readNum = pmsg->len; |
| 310 | iface->result = 0; |
| 311 | iface->timeout_count = 10; |
Hans Schillstrom | afc13b7 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 312 | init_completion(&(iface->complete)); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 313 | /* Set Transmit device address */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 314 | write_MASTER_ADDR(iface, pmsg->addr); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 315 | |
| 316 | /* FIFO Initiation. Data in FIFO should be |
| 317 | * discarded before start a new operation. |
| 318 | */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 319 | write_FIFO_CTL(iface, 0x3); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 320 | SSYNC(); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 321 | write_FIFO_CTL(iface, 0); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 322 | SSYNC(); |
| 323 | |
| 324 | if (pmsg->flags & I2C_M_RD) |
| 325 | iface->read_write = I2C_SMBUS_READ; |
| 326 | else { |
| 327 | iface->read_write = I2C_SMBUS_WRITE; |
| 328 | /* Transmit first data */ |
| 329 | if (iface->writeNum > 0) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 330 | write_XMT_DATA8(iface, *(iface->transPtr++)); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 331 | iface->writeNum--; |
| 332 | SSYNC(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /* clear int stat */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 337 | write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 338 | |
| 339 | /* Interrupt mask . Enable XMT, RCV interrupt */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 340 | write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 341 | SSYNC(); |
| 342 | |
| 343 | if (pmsg->len <= 255) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 344 | write_MASTER_CTL(iface, pmsg->len << 6); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 345 | else { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 346 | write_MASTER_CTL(iface, 0xff << 6); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 347 | iface->manual_stop = 1; |
| 348 | } |
| 349 | |
| 350 | iface->timeout_timer.expires = jiffies + POLL_TIMEOUT; |
| 351 | add_timer(&iface->timeout_timer); |
| 352 | |
| 353 | /* Master enable */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 354 | write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 355 | ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) | |
| 356 | ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0)); |
| 357 | SSYNC(); |
| 358 | |
| 359 | wait_for_completion(&iface->complete); |
| 360 | |
| 361 | rc = iface->result; |
| 362 | |
| 363 | if (rc == 1) |
| 364 | return num; |
| 365 | else |
| 366 | return rc; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /* |
| 370 | * SMBus type transfer entrypoint |
| 371 | */ |
| 372 | |
| 373 | int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr, |
| 374 | unsigned short flags, char read_write, |
| 375 | u8 command, int size, union i2c_smbus_data *data) |
| 376 | { |
| 377 | struct bfin_twi_iface *iface = adap->algo_data; |
| 378 | int rc = 0; |
| 379 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 380 | if (!(read_CONTROL(iface) & TWI_ENA)) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 381 | return -ENXIO; |
| 382 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 383 | while (read_MASTER_STAT(iface) & BUSBUSY) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 384 | yield(); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 385 | |
| 386 | iface->writeNum = 0; |
| 387 | iface->readNum = 0; |
| 388 | |
| 389 | /* Prepare datas & select mode */ |
| 390 | switch (size) { |
| 391 | case I2C_SMBUS_QUICK: |
| 392 | iface->transPtr = NULL; |
| 393 | iface->cur_mode = TWI_I2C_MODE_STANDARD; |
| 394 | break; |
| 395 | case I2C_SMBUS_BYTE: |
| 396 | if (data == NULL) |
| 397 | iface->transPtr = NULL; |
| 398 | else { |
| 399 | if (read_write == I2C_SMBUS_READ) |
| 400 | iface->readNum = 1; |
| 401 | else |
| 402 | iface->writeNum = 1; |
| 403 | iface->transPtr = &data->byte; |
| 404 | } |
| 405 | iface->cur_mode = TWI_I2C_MODE_STANDARD; |
| 406 | break; |
| 407 | case I2C_SMBUS_BYTE_DATA: |
| 408 | if (read_write == I2C_SMBUS_READ) { |
| 409 | iface->readNum = 1; |
| 410 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 411 | } else { |
| 412 | iface->writeNum = 1; |
| 413 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 414 | } |
| 415 | iface->transPtr = &data->byte; |
| 416 | break; |
| 417 | case I2C_SMBUS_WORD_DATA: |
| 418 | if (read_write == I2C_SMBUS_READ) { |
| 419 | iface->readNum = 2; |
| 420 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 421 | } else { |
| 422 | iface->writeNum = 2; |
| 423 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 424 | } |
| 425 | iface->transPtr = (u8 *)&data->word; |
| 426 | break; |
| 427 | case I2C_SMBUS_PROC_CALL: |
| 428 | iface->writeNum = 2; |
| 429 | iface->readNum = 2; |
| 430 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 431 | iface->transPtr = (u8 *)&data->word; |
| 432 | break; |
| 433 | case I2C_SMBUS_BLOCK_DATA: |
| 434 | if (read_write == I2C_SMBUS_READ) { |
| 435 | iface->readNum = 0; |
| 436 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 437 | } else { |
| 438 | iface->writeNum = data->block[0] + 1; |
| 439 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 440 | } |
| 441 | iface->transPtr = data->block; |
| 442 | break; |
| 443 | default: |
| 444 | return -1; |
| 445 | } |
| 446 | |
| 447 | iface->result = 0; |
| 448 | iface->manual_stop = 0; |
| 449 | iface->read_write = read_write; |
| 450 | iface->command = command; |
| 451 | iface->timeout_count = 10; |
Hans Schillstrom | afc13b7 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 452 | init_completion(&(iface->complete)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 453 | |
| 454 | /* FIFO Initiation. Data in FIFO should be discarded before |
| 455 | * start a new operation. |
| 456 | */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 457 | write_FIFO_CTL(iface, 0x3); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 458 | SSYNC(); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 459 | write_FIFO_CTL(iface, 0); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 460 | |
| 461 | /* clear int stat */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 462 | write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 463 | |
| 464 | /* Set Transmit device address */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 465 | write_MASTER_ADDR(iface, addr); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 466 | SSYNC(); |
| 467 | |
| 468 | iface->timeout_timer.expires = jiffies + POLL_TIMEOUT; |
| 469 | add_timer(&iface->timeout_timer); |
| 470 | |
| 471 | switch (iface->cur_mode) { |
| 472 | case TWI_I2C_MODE_STANDARDSUB: |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 473 | write_XMT_DATA8(iface, iface->command); |
| 474 | write_INT_MASK(iface, MCOMP | MERR | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 475 | ((iface->read_write == I2C_SMBUS_READ) ? |
| 476 | RCVSERV : XMTSERV)); |
| 477 | SSYNC(); |
| 478 | |
| 479 | if (iface->writeNum + 1 <= 255) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 480 | write_MASTER_CTL(iface, (iface->writeNum + 1) << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 481 | else { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 482 | write_MASTER_CTL(iface, 0xff << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 483 | iface->manual_stop = 1; |
| 484 | } |
| 485 | /* Master enable */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 486 | write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 487 | ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0)); |
| 488 | break; |
| 489 | case TWI_I2C_MODE_COMBINED: |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 490 | write_XMT_DATA8(iface, iface->command); |
| 491 | write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 492 | SSYNC(); |
| 493 | |
| 494 | if (iface->writeNum > 0) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 495 | write_MASTER_CTL(iface, (iface->writeNum + 1) << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 496 | else |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 497 | write_MASTER_CTL(iface, 0x1 << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 498 | /* Master enable */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 499 | write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 500 | ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ>100) ? FAST : 0)); |
| 501 | break; |
| 502 | default: |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 503 | write_MASTER_CTL(iface, 0); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 504 | if (size != I2C_SMBUS_QUICK) { |
| 505 | /* Don't access xmit data register when this is a |
| 506 | * read operation. |
| 507 | */ |
| 508 | if (iface->read_write != I2C_SMBUS_READ) { |
| 509 | if (iface->writeNum > 0) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 510 | write_XMT_DATA8(iface, |
| 511 | *(iface->transPtr++)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 512 | if (iface->writeNum <= 255) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 513 | write_MASTER_CTL(iface, |
| 514 | iface->writeNum << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 515 | else { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 516 | write_MASTER_CTL(iface, |
| 517 | 0xff << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 518 | iface->manual_stop = 1; |
| 519 | } |
| 520 | iface->writeNum--; |
| 521 | } else { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 522 | write_XMT_DATA8(iface, iface->command); |
| 523 | write_MASTER_CTL(iface, 1 << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 524 | } |
| 525 | } else { |
| 526 | if (iface->readNum > 0 && iface->readNum <= 255) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 527 | write_MASTER_CTL(iface, |
| 528 | iface->readNum << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 529 | else if (iface->readNum > 255) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 530 | write_MASTER_CTL(iface, 0xff << 6); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 531 | iface->manual_stop = 1; |
| 532 | } else { |
| 533 | del_timer(&iface->timeout_timer); |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | } |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 538 | write_INT_MASK(iface, MCOMP | MERR | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 539 | ((iface->read_write == I2C_SMBUS_READ) ? |
| 540 | RCVSERV : XMTSERV)); |
| 541 | SSYNC(); |
| 542 | |
| 543 | /* Master enable */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 544 | write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 545 | ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) | |
| 546 | ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0)); |
| 547 | break; |
| 548 | } |
| 549 | SSYNC(); |
| 550 | |
| 551 | wait_for_completion(&iface->complete); |
| 552 | |
| 553 | rc = (iface->result >= 0) ? 0 : -1; |
| 554 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 555 | return rc; |
| 556 | } |
| 557 | |
| 558 | /* |
| 559 | * Return what the adapter supports |
| 560 | */ |
| 561 | static u32 bfin_twi_functionality(struct i2c_adapter *adap) |
| 562 | { |
| 563 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 564 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | |
| 565 | I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL | |
| 566 | I2C_FUNC_I2C; |
| 567 | } |
| 568 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 569 | static struct i2c_algorithm bfin_twi_algorithm = { |
| 570 | .master_xfer = bfin_twi_master_xfer, |
| 571 | .smbus_xfer = bfin_twi_smbus_xfer, |
| 572 | .functionality = bfin_twi_functionality, |
| 573 | }; |
| 574 | |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 575 | static int i2c_bfin_twi_suspend(struct platform_device *pdev, pm_message_t state) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 576 | { |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 577 | struct bfin_twi_iface *iface = platform_get_drvdata(pdev); |
| 578 | |
| 579 | iface->saved_clkdiv = read_CLKDIV(iface); |
| 580 | iface->saved_control = read_CONTROL(iface); |
| 581 | |
| 582 | free_irq(iface->irq, iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 583 | |
| 584 | /* Disable TWI */ |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 585 | write_CONTROL(iface, iface->saved_control & ~TWI_ENA); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 590 | static int i2c_bfin_twi_resume(struct platform_device *pdev) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 591 | { |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 592 | struct bfin_twi_iface *iface = platform_get_drvdata(pdev); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 593 | |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 594 | int rc = request_irq(iface->irq, bfin_twi_interrupt_entry, |
| 595 | IRQF_DISABLED, pdev->name, iface); |
| 596 | if (rc) { |
| 597 | dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq); |
| 598 | return -ENODEV; |
| 599 | } |
| 600 | |
| 601 | /* Resume TWI interface clock as specified */ |
| 602 | write_CLKDIV(iface, iface->saved_clkdiv); |
| 603 | |
| 604 | /* Resume TWI */ |
| 605 | write_CONTROL(iface, iface->saved_control); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 610 | static int i2c_bfin_twi_probe(struct platform_device *pdev) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 611 | { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 612 | struct bfin_twi_iface *iface; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 613 | struct i2c_adapter *p_adap; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 614 | struct resource *res; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 615 | int rc; |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 616 | unsigned int clkhilow; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 617 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 618 | iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL); |
| 619 | if (!iface) { |
| 620 | dev_err(&pdev->dev, "Cannot allocate memory\n"); |
| 621 | rc = -ENOMEM; |
| 622 | goto out_error_nomem; |
| 623 | } |
| 624 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 625 | spin_lock_init(&(iface->lock)); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 626 | |
| 627 | /* Find and map our resources */ |
| 628 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 629 | if (res == NULL) { |
| 630 | dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n"); |
| 631 | rc = -ENOENT; |
| 632 | goto out_error_get_res; |
| 633 | } |
| 634 | |
| 635 | iface->regs_base = ioremap(res->start, res->end - res->start + 1); |
| 636 | if (iface->regs_base == NULL) { |
| 637 | dev_err(&pdev->dev, "Cannot map IO\n"); |
| 638 | rc = -ENXIO; |
| 639 | goto out_error_ioremap; |
| 640 | } |
| 641 | |
| 642 | iface->irq = platform_get_irq(pdev, 0); |
| 643 | if (iface->irq < 0) { |
| 644 | dev_err(&pdev->dev, "No IRQ specified\n"); |
| 645 | rc = -ENOENT; |
| 646 | goto out_error_no_irq; |
| 647 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 648 | |
| 649 | init_timer(&(iface->timeout_timer)); |
| 650 | iface->timeout_timer.function = bfin_twi_timeout; |
| 651 | iface->timeout_timer.data = (unsigned long)iface; |
| 652 | |
| 653 | p_adap = &iface->adap; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 654 | p_adap->nr = pdev->id; |
| 655 | strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 656 | p_adap->algo = &bfin_twi_algorithm; |
| 657 | p_adap->algo_data = iface; |
Jean Delvare | e1995f6 | 2009-01-07 14:29:16 +0100 | [diff] [blame] | 658 | p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 659 | p_adap->dev.parent = &pdev->dev; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 660 | |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 661 | rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi"); |
| 662 | if (rc) { |
| 663 | dev_err(&pdev->dev, "Can't setup pin mux!\n"); |
| 664 | goto out_error_pin_mux; |
| 665 | } |
| 666 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 667 | rc = request_irq(iface->irq, bfin_twi_interrupt_entry, |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 668 | IRQF_DISABLED, pdev->name, iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 669 | if (rc) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 670 | dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq); |
| 671 | rc = -ENODEV; |
| 672 | goto out_error_req_irq; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* Set TWI internal clock as 10MHz */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 676 | write_CONTROL(iface, ((get_sclk() / 1024 / 1024 + 5) / 10) & 0x7F); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 677 | |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 678 | /* |
| 679 | * We will not end up with a CLKDIV=0 because no one will specify |
| 680 | * 20kHz SCL or less in Kconfig now. (5 * 1024 / 20 = 0x100) |
| 681 | */ |
| 682 | clkhilow = 5 * 1024 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ; |
| 683 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 684 | /* Set Twi interface clock as specified */ |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 685 | write_CLKDIV(iface, (clkhilow << 8) | clkhilow); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 686 | |
| 687 | /* Enable TWI */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 688 | write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 689 | SSYNC(); |
| 690 | |
Kalle Pokki | 991dee5 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 691 | rc = i2c_add_numbered_adapter(p_adap); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 692 | if (rc < 0) { |
| 693 | dev_err(&pdev->dev, "Can't add i2c adapter!\n"); |
| 694 | goto out_error_add_adapter; |
| 695 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 696 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 697 | platform_set_drvdata(pdev, iface); |
| 698 | |
Bryan Wu | fa6ad22 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 699 | dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, " |
| 700 | "regs_base@%p\n", iface->regs_base); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 701 | |
| 702 | return 0; |
| 703 | |
| 704 | out_error_add_adapter: |
| 705 | free_irq(iface->irq, iface); |
| 706 | out_error_req_irq: |
| 707 | out_error_no_irq: |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 708 | peripheral_free_list(pin_req[pdev->id]); |
| 709 | out_error_pin_mux: |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 710 | iounmap(iface->regs_base); |
| 711 | out_error_ioremap: |
| 712 | out_error_get_res: |
| 713 | kfree(iface); |
| 714 | out_error_nomem: |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 715 | return rc; |
| 716 | } |
| 717 | |
| 718 | static int i2c_bfin_twi_remove(struct platform_device *pdev) |
| 719 | { |
| 720 | struct bfin_twi_iface *iface = platform_get_drvdata(pdev); |
| 721 | |
| 722 | platform_set_drvdata(pdev, NULL); |
| 723 | |
| 724 | i2c_del_adapter(&(iface->adap)); |
| 725 | free_irq(iface->irq, iface); |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 726 | peripheral_free_list(pin_req[pdev->id]); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 727 | iounmap(iface->regs_base); |
| 728 | kfree(iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static struct platform_driver i2c_bfin_twi_driver = { |
| 734 | .probe = i2c_bfin_twi_probe, |
| 735 | .remove = i2c_bfin_twi_remove, |
| 736 | .suspend = i2c_bfin_twi_suspend, |
| 737 | .resume = i2c_bfin_twi_resume, |
| 738 | .driver = { |
| 739 | .name = "i2c-bfin-twi", |
| 740 | .owner = THIS_MODULE, |
| 741 | }, |
| 742 | }; |
| 743 | |
| 744 | static int __init i2c_bfin_twi_init(void) |
| 745 | { |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 746 | return platform_driver_register(&i2c_bfin_twi_driver); |
| 747 | } |
| 748 | |
| 749 | static void __exit i2c_bfin_twi_exit(void) |
| 750 | { |
| 751 | platform_driver_unregister(&i2c_bfin_twi_driver); |
| 752 | } |
| 753 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 754 | module_init(i2c_bfin_twi_init); |
| 755 | module_exit(i2c_bfin_twi_exit); |
Bryan Wu | fa6ad22 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 756 | |
| 757 | MODULE_AUTHOR("Bryan Wu, Sonic Zhang"); |
| 758 | MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver"); |
| 759 | MODULE_LICENSE("GPL"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 760 | MODULE_ALIAS("platform:i2c-bfin-twi"); |