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> |
| 23 | |
| 24 | #include <asm/blackfin.h> |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 25 | #include <asm/portmux.h> |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 26 | #include <asm/irq.h> |
| 27 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 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; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 45 | struct i2c_adapter adap; |
| 46 | struct completion complete; |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 47 | struct i2c_msg *pmsg; |
| 48 | int msg_num; |
| 49 | int cur_msg; |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 50 | u16 saved_clkdiv; |
| 51 | u16 saved_control; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 52 | void __iomem *regs_base; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 55 | |
| 56 | #define DEFINE_TWI_REG(reg, off) \ |
| 57 | static inline u16 read_##reg(struct bfin_twi_iface *iface) \ |
| 58 | { return bfin_read16(iface->regs_base + (off)); } \ |
| 59 | static inline void write_##reg(struct bfin_twi_iface *iface, u16 v) \ |
| 60 | { bfin_write16(iface->regs_base + (off), v); } |
| 61 | |
| 62 | DEFINE_TWI_REG(CLKDIV, 0x00) |
| 63 | DEFINE_TWI_REG(CONTROL, 0x04) |
| 64 | DEFINE_TWI_REG(SLAVE_CTL, 0x08) |
| 65 | DEFINE_TWI_REG(SLAVE_STAT, 0x0C) |
| 66 | DEFINE_TWI_REG(SLAVE_ADDR, 0x10) |
| 67 | DEFINE_TWI_REG(MASTER_CTL, 0x14) |
| 68 | DEFINE_TWI_REG(MASTER_STAT, 0x18) |
| 69 | DEFINE_TWI_REG(MASTER_ADDR, 0x1C) |
| 70 | DEFINE_TWI_REG(INT_STAT, 0x20) |
| 71 | DEFINE_TWI_REG(INT_MASK, 0x24) |
| 72 | DEFINE_TWI_REG(FIFO_CTL, 0x28) |
| 73 | DEFINE_TWI_REG(FIFO_STAT, 0x2C) |
| 74 | DEFINE_TWI_REG(XMT_DATA8, 0x80) |
| 75 | DEFINE_TWI_REG(XMT_DATA16, 0x84) |
| 76 | DEFINE_TWI_REG(RCV_DATA8, 0x88) |
| 77 | DEFINE_TWI_REG(RCV_DATA16, 0x8C) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 78 | |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 79 | static const u16 pin_req[2][3] = { |
| 80 | {P_TWI0_SCL, P_TWI0_SDA, 0}, |
| 81 | {P_TWI1_SCL, P_TWI1_SDA, 0}, |
| 82 | }; |
| 83 | |
Sonic Zhang | 5481d07 | 2010-03-22 03:23:18 -0400 | [diff] [blame] | 84 | static void bfin_twi_handle_interrupt(struct bfin_twi_iface *iface, |
| 85 | unsigned short twi_int_status) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 86 | { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 87 | unsigned short mast_stat = read_MASTER_STAT(iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 88 | |
| 89 | if (twi_int_status & XMTSERV) { |
| 90 | /* Transmit next data */ |
| 91 | if (iface->writeNum > 0) { |
Sonic Zhang | 5481d07 | 2010-03-22 03:23:18 -0400 | [diff] [blame] | 92 | SSYNC(); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 93 | write_XMT_DATA8(iface, *(iface->transPtr++)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 94 | iface->writeNum--; |
| 95 | } |
| 96 | /* start receive immediately after complete sending in |
| 97 | * combine mode. |
| 98 | */ |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 99 | else if (iface->cur_mode == TWI_I2C_MODE_COMBINED) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 100 | write_MASTER_CTL(iface, |
| 101 | read_MASTER_CTL(iface) | MDIR | RSTART); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 102 | else if (iface->manual_stop) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 103 | write_MASTER_CTL(iface, |
| 104 | read_MASTER_CTL(iface) | STOP); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 105 | else if (iface->cur_mode == TWI_I2C_MODE_REPEAT && |
Frank Shew | 94327d0 | 2009-05-19 07:23:49 -0400 | [diff] [blame] | 106 | iface->cur_msg + 1 < iface->msg_num) { |
| 107 | if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD) |
| 108 | write_MASTER_CTL(iface, |
| 109 | read_MASTER_CTL(iface) | RSTART | MDIR); |
| 110 | else |
| 111 | write_MASTER_CTL(iface, |
| 112 | (read_MASTER_CTL(iface) | RSTART) & ~MDIR); |
| 113 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 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); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 135 | } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT && |
Frank Shew | 94327d0 | 2009-05-19 07:23:49 -0400 | [diff] [blame] | 136 | iface->cur_msg + 1 < iface->msg_num) { |
| 137 | if (iface->pmsg[iface->cur_msg + 1].flags & I2C_M_RD) |
| 138 | write_MASTER_CTL(iface, |
| 139 | read_MASTER_CTL(iface) | RSTART | MDIR); |
| 140 | else |
| 141 | write_MASTER_CTL(iface, |
| 142 | (read_MASTER_CTL(iface) | RSTART) & ~MDIR); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 143 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 144 | } |
| 145 | if (twi_int_status & MERR) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 146 | write_INT_MASK(iface, 0); |
| 147 | write_MASTER_STAT(iface, 0x3e); |
| 148 | write_MASTER_CTL(iface, 0); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 149 | iface->result = -EIO; |
Michael Hennerich | 5cfafc1 | 2010-03-22 03:23:17 -0400 | [diff] [blame] | 150 | |
| 151 | if (mast_stat & LOSTARB) |
| 152 | dev_dbg(&iface->adap.dev, "Lost Arbitration\n"); |
| 153 | if (mast_stat & ANAK) |
| 154 | dev_dbg(&iface->adap.dev, "Address Not Acknowledged\n"); |
| 155 | if (mast_stat & DNAK) |
| 156 | dev_dbg(&iface->adap.dev, "Data Not Acknowledged\n"); |
| 157 | if (mast_stat & BUFRDERR) |
| 158 | dev_dbg(&iface->adap.dev, "Buffer Read Error\n"); |
| 159 | if (mast_stat & BUFWRERR) |
| 160 | dev_dbg(&iface->adap.dev, "Buffer Write Error\n"); |
| 161 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 162 | /* if both err and complete int stats are set, return proper |
| 163 | * results. |
| 164 | */ |
| 165 | if (twi_int_status & MCOMP) { |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 166 | /* If it is a quick transfer, only address without data, |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 167 | * not an err, return 1. |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 168 | * If address is acknowledged return 1. |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 169 | */ |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 170 | if ((iface->writeNum == 0 && (mast_stat & BUFRDERR)) |
| 171 | || !(mast_stat & ANAK)) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 172 | iface->result = 1; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 173 | } |
| 174 | complete(&iface->complete); |
| 175 | return; |
| 176 | } |
| 177 | if (twi_int_status & MCOMP) { |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 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); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 199 | } else if (iface->cur_mode == TWI_I2C_MODE_REPEAT && |
| 200 | iface->cur_msg+1 < iface->msg_num) { |
| 201 | iface->cur_msg++; |
| 202 | iface->transPtr = iface->pmsg[iface->cur_msg].buf; |
| 203 | iface->writeNum = iface->readNum = |
| 204 | iface->pmsg[iface->cur_msg].len; |
| 205 | /* Set Transmit device address */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 206 | write_MASTER_ADDR(iface, |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 207 | iface->pmsg[iface->cur_msg].addr); |
| 208 | if (iface->pmsg[iface->cur_msg].flags & I2C_M_RD) |
| 209 | iface->read_write = I2C_SMBUS_READ; |
| 210 | else { |
| 211 | iface->read_write = I2C_SMBUS_WRITE; |
| 212 | /* Transmit first data */ |
| 213 | if (iface->writeNum > 0) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 214 | write_XMT_DATA8(iface, |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 215 | *(iface->transPtr++)); |
| 216 | iface->writeNum--; |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | if (iface->pmsg[iface->cur_msg].len <= 255) |
Sonic Zhang | 57a8f32 | 2009-05-19 07:21:58 -0400 | [diff] [blame] | 221 | write_MASTER_CTL(iface, |
| 222 | (read_MASTER_CTL(iface) & |
| 223 | (~(0xff << 6))) | |
| 224 | (iface->pmsg[iface->cur_msg].len << 6)); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 225 | else { |
Sonic Zhang | 57a8f32 | 2009-05-19 07:21:58 -0400 | [diff] [blame] | 226 | write_MASTER_CTL(iface, |
| 227 | (read_MASTER_CTL(iface) | |
| 228 | (0xff << 6))); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 229 | iface->manual_stop = 1; |
| 230 | } |
| 231 | /* remove restart bit and enable master receive */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 232 | write_MASTER_CTL(iface, |
| 233 | read_MASTER_CTL(iface) & ~RSTART); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 234 | } else { |
| 235 | iface->result = 1; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 236 | write_INT_MASK(iface, 0); |
| 237 | write_MASTER_CTL(iface, 0); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 238 | } |
| 239 | } |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 240 | complete(&iface->complete); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* Interrupt handler */ |
| 244 | static irqreturn_t bfin_twi_interrupt_entry(int irq, void *dev_id) |
| 245 | { |
| 246 | struct bfin_twi_iface *iface = dev_id; |
| 247 | unsigned long flags; |
Sonic Zhang | 5481d07 | 2010-03-22 03:23:18 -0400 | [diff] [blame] | 248 | unsigned short twi_int_status; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 249 | |
| 250 | spin_lock_irqsave(&iface->lock, flags); |
Sonic Zhang | 5481d07 | 2010-03-22 03:23:18 -0400 | [diff] [blame] | 251 | while (1) { |
| 252 | twi_int_status = read_INT_STAT(iface); |
| 253 | if (!twi_int_status) |
| 254 | break; |
| 255 | /* Clear interrupt status */ |
| 256 | write_INT_STAT(iface, twi_int_status); |
| 257 | bfin_twi_handle_interrupt(iface, twi_int_status); |
| 258 | SSYNC(); |
| 259 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 260 | spin_unlock_irqrestore(&iface->lock, flags); |
| 261 | return IRQ_HANDLED; |
| 262 | } |
| 263 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 264 | /* |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 265 | * One i2c master transfer |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 266 | */ |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 267 | static int bfin_twi_do_master_xfer(struct i2c_adapter *adap, |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 268 | struct i2c_msg *msgs, int num) |
| 269 | { |
| 270 | struct bfin_twi_iface *iface = adap->algo_data; |
| 271 | struct i2c_msg *pmsg; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 272 | int rc = 0; |
| 273 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 274 | if (!(read_CONTROL(iface) & TWI_ENA)) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 275 | return -ENXIO; |
| 276 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 277 | while (read_MASTER_STAT(iface) & BUSBUSY) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 278 | yield(); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 279 | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 280 | iface->pmsg = msgs; |
| 281 | iface->msg_num = num; |
| 282 | iface->cur_msg = 0; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 283 | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 284 | pmsg = &msgs[0]; |
| 285 | if (pmsg->flags & I2C_M_TEN) { |
| 286 | dev_err(&adap->dev, "10 bits addr not supported!\n"); |
| 287 | return -EINVAL; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 290 | iface->cur_mode = TWI_I2C_MODE_REPEAT; |
| 291 | iface->manual_stop = 0; |
| 292 | iface->transPtr = pmsg->buf; |
| 293 | iface->writeNum = iface->readNum = pmsg->len; |
| 294 | iface->result = 0; |
Hans Schillstrom | afc13b7 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 295 | init_completion(&(iface->complete)); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 296 | /* Set Transmit device address */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 297 | write_MASTER_ADDR(iface, pmsg->addr); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 298 | |
| 299 | /* FIFO Initiation. Data in FIFO should be |
| 300 | * discarded before start a new operation. |
| 301 | */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 302 | write_FIFO_CTL(iface, 0x3); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 303 | SSYNC(); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 304 | write_FIFO_CTL(iface, 0); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 305 | SSYNC(); |
| 306 | |
| 307 | if (pmsg->flags & I2C_M_RD) |
| 308 | iface->read_write = I2C_SMBUS_READ; |
| 309 | else { |
| 310 | iface->read_write = I2C_SMBUS_WRITE; |
| 311 | /* Transmit first data */ |
| 312 | if (iface->writeNum > 0) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 313 | write_XMT_DATA8(iface, *(iface->transPtr++)); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 314 | iface->writeNum--; |
| 315 | SSYNC(); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* clear int stat */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 320 | write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 321 | |
| 322 | /* Interrupt mask . Enable XMT, RCV interrupt */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 323 | write_INT_MASK(iface, MCOMP | MERR | RCVSERV | XMTSERV); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 324 | SSYNC(); |
| 325 | |
| 326 | if (pmsg->len <= 255) |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 327 | write_MASTER_CTL(iface, pmsg->len << 6); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 328 | else { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 329 | write_MASTER_CTL(iface, 0xff << 6); |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 330 | iface->manual_stop = 1; |
| 331 | } |
| 332 | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 333 | /* Master enable */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 334 | write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 335 | ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) | |
| 336 | ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0)); |
| 337 | SSYNC(); |
| 338 | |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 339 | while (!iface->result) { |
| 340 | if (!wait_for_completion_timeout(&iface->complete, |
| 341 | adap->timeout)) { |
| 342 | iface->result = -1; |
| 343 | dev_err(&adap->dev, "master transfer timeout\n"); |
| 344 | } |
| 345 | } |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 346 | |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 347 | if (iface->result == 1) |
| 348 | rc = iface->cur_msg + 1; |
Sonic Zhang | 4dd39bb | 2008-04-22 22:16:47 +0200 | [diff] [blame] | 349 | else |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 350 | rc = iface->result; |
| 351 | |
| 352 | return rc; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 356 | * Generic i2c master transfer entrypoint |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 357 | */ |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 358 | static int bfin_twi_master_xfer(struct i2c_adapter *adap, |
| 359 | struct i2c_msg *msgs, int num) |
| 360 | { |
Sonic Zhang | be2f80f | 2010-03-22 03:23:19 -0400 | [diff] [blame^] | 361 | return bfin_twi_do_master_xfer(adap, msgs, num); |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* |
| 365 | * One I2C SMBus transfer |
| 366 | */ |
| 367 | int bfin_twi_do_smbus_xfer(struct i2c_adapter *adap, u16 addr, |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 368 | unsigned short flags, char read_write, |
| 369 | u8 command, int size, union i2c_smbus_data *data) |
| 370 | { |
| 371 | struct bfin_twi_iface *iface = adap->algo_data; |
| 372 | int rc = 0; |
| 373 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 374 | if (!(read_CONTROL(iface) & TWI_ENA)) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 375 | return -ENXIO; |
| 376 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 377 | while (read_MASTER_STAT(iface) & BUSBUSY) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 378 | yield(); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 379 | |
| 380 | iface->writeNum = 0; |
| 381 | iface->readNum = 0; |
| 382 | |
| 383 | /* Prepare datas & select mode */ |
| 384 | switch (size) { |
| 385 | case I2C_SMBUS_QUICK: |
| 386 | iface->transPtr = NULL; |
| 387 | iface->cur_mode = TWI_I2C_MODE_STANDARD; |
| 388 | break; |
| 389 | case I2C_SMBUS_BYTE: |
| 390 | if (data == NULL) |
| 391 | iface->transPtr = NULL; |
| 392 | else { |
| 393 | if (read_write == I2C_SMBUS_READ) |
| 394 | iface->readNum = 1; |
| 395 | else |
| 396 | iface->writeNum = 1; |
| 397 | iface->transPtr = &data->byte; |
| 398 | } |
| 399 | iface->cur_mode = TWI_I2C_MODE_STANDARD; |
| 400 | break; |
| 401 | case I2C_SMBUS_BYTE_DATA: |
| 402 | if (read_write == I2C_SMBUS_READ) { |
| 403 | iface->readNum = 1; |
| 404 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 405 | } else { |
| 406 | iface->writeNum = 1; |
| 407 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 408 | } |
| 409 | iface->transPtr = &data->byte; |
| 410 | break; |
| 411 | case I2C_SMBUS_WORD_DATA: |
| 412 | if (read_write == I2C_SMBUS_READ) { |
| 413 | iface->readNum = 2; |
| 414 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 415 | } else { |
| 416 | iface->writeNum = 2; |
| 417 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 418 | } |
| 419 | iface->transPtr = (u8 *)&data->word; |
| 420 | break; |
| 421 | case I2C_SMBUS_PROC_CALL: |
| 422 | iface->writeNum = 2; |
| 423 | iface->readNum = 2; |
| 424 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 425 | iface->transPtr = (u8 *)&data->word; |
| 426 | break; |
| 427 | case I2C_SMBUS_BLOCK_DATA: |
| 428 | if (read_write == I2C_SMBUS_READ) { |
| 429 | iface->readNum = 0; |
| 430 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 431 | } else { |
| 432 | iface->writeNum = data->block[0] + 1; |
| 433 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 434 | } |
| 435 | iface->transPtr = data->block; |
| 436 | break; |
Michael Hennerich | e0cd2dd | 2009-05-27 09:24:10 +0000 | [diff] [blame] | 437 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 438 | if (read_write == I2C_SMBUS_READ) { |
| 439 | iface->readNum = data->block[0]; |
| 440 | iface->cur_mode = TWI_I2C_MODE_COMBINED; |
| 441 | } else { |
| 442 | iface->writeNum = data->block[0]; |
| 443 | iface->cur_mode = TWI_I2C_MODE_STANDARDSUB; |
| 444 | } |
| 445 | iface->transPtr = (u8 *)&data->block[1]; |
| 446 | break; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 447 | default: |
| 448 | return -1; |
| 449 | } |
| 450 | |
| 451 | iface->result = 0; |
| 452 | iface->manual_stop = 0; |
| 453 | iface->read_write = read_write; |
| 454 | iface->command = command; |
Hans Schillstrom | afc13b7 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 455 | init_completion(&(iface->complete)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 456 | |
| 457 | /* FIFO Initiation. Data in FIFO should be discarded before |
| 458 | * start a new operation. |
| 459 | */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 460 | write_FIFO_CTL(iface, 0x3); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 461 | SSYNC(); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 462 | write_FIFO_CTL(iface, 0); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 463 | |
| 464 | /* clear int stat */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 465 | write_INT_STAT(iface, MERR | MCOMP | XMTSERV | RCVSERV); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 466 | |
| 467 | /* Set Transmit device address */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 468 | write_MASTER_ADDR(iface, addr); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 469 | SSYNC(); |
| 470 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 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; |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 532 | } else |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 533 | break; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 534 | } |
| 535 | } |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 536 | write_INT_MASK(iface, MCOMP | MERR | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 537 | ((iface->read_write == I2C_SMBUS_READ) ? |
| 538 | RCVSERV : XMTSERV)); |
| 539 | SSYNC(); |
| 540 | |
| 541 | /* Master enable */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 542 | write_MASTER_CTL(iface, read_MASTER_CTL(iface) | MEN | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 543 | ((iface->read_write == I2C_SMBUS_READ) ? MDIR : 0) | |
| 544 | ((CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ > 100) ? FAST : 0)); |
| 545 | break; |
| 546 | } |
| 547 | SSYNC(); |
| 548 | |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 549 | while (!iface->result) { |
| 550 | if (!wait_for_completion_timeout(&iface->complete, |
| 551 | adap->timeout)) { |
| 552 | iface->result = -1; |
| 553 | dev_err(&adap->dev, "smbus transfer timeout\n"); |
| 554 | } |
| 555 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 556 | |
| 557 | rc = (iface->result >= 0) ? 0 : -1; |
| 558 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 559 | return rc; |
| 560 | } |
| 561 | |
| 562 | /* |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 563 | * Generic I2C SMBus transfer entrypoint |
| 564 | */ |
| 565 | int bfin_twi_smbus_xfer(struct i2c_adapter *adap, u16 addr, |
| 566 | unsigned short flags, char read_write, |
| 567 | u8 command, int size, union i2c_smbus_data *data) |
| 568 | { |
Sonic Zhang | be2f80f | 2010-03-22 03:23:19 -0400 | [diff] [blame^] | 569 | return bfin_twi_do_smbus_xfer(adap, addr, flags, |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 570 | read_write, command, size, data); |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 574 | * Return what the adapter supports |
| 575 | */ |
| 576 | static u32 bfin_twi_functionality(struct i2c_adapter *adap) |
| 577 | { |
| 578 | return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE | |
| 579 | I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA | |
| 580 | I2C_FUNC_SMBUS_BLOCK_DATA | I2C_FUNC_SMBUS_PROC_CALL | |
Michael Hennerich | e0cd2dd | 2009-05-27 09:24:10 +0000 | [diff] [blame] | 581 | I2C_FUNC_I2C | I2C_FUNC_SMBUS_I2C_BLOCK; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 582 | } |
| 583 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 584 | static struct i2c_algorithm bfin_twi_algorithm = { |
| 585 | .master_xfer = bfin_twi_master_xfer, |
| 586 | .smbus_xfer = bfin_twi_smbus_xfer, |
| 587 | .functionality = bfin_twi_functionality, |
| 588 | }; |
| 589 | |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 590 | 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] | 591 | { |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 592 | struct bfin_twi_iface *iface = platform_get_drvdata(pdev); |
| 593 | |
| 594 | iface->saved_clkdiv = read_CLKDIV(iface); |
| 595 | iface->saved_control = read_CONTROL(iface); |
| 596 | |
| 597 | free_irq(iface->irq, iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 598 | |
| 599 | /* Disable TWI */ |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 600 | write_CONTROL(iface, iface->saved_control & ~TWI_ENA); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 605 | static int i2c_bfin_twi_resume(struct platform_device *pdev) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 606 | { |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 607 | struct bfin_twi_iface *iface = platform_get_drvdata(pdev); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 608 | |
Michael Hennerich | 958585f | 2008-07-27 14:41:54 +0800 | [diff] [blame] | 609 | int rc = request_irq(iface->irq, bfin_twi_interrupt_entry, |
| 610 | IRQF_DISABLED, pdev->name, iface); |
| 611 | if (rc) { |
| 612 | dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq); |
| 613 | return -ENODEV; |
| 614 | } |
| 615 | |
| 616 | /* Resume TWI interface clock as specified */ |
| 617 | write_CLKDIV(iface, iface->saved_clkdiv); |
| 618 | |
| 619 | /* Resume TWI */ |
| 620 | write_CONTROL(iface, iface->saved_control); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 625 | static int i2c_bfin_twi_probe(struct platform_device *pdev) |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 626 | { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 627 | struct bfin_twi_iface *iface; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 628 | struct i2c_adapter *p_adap; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 629 | struct resource *res; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 630 | int rc; |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 631 | unsigned int clkhilow; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 632 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 633 | iface = kzalloc(sizeof(struct bfin_twi_iface), GFP_KERNEL); |
| 634 | if (!iface) { |
| 635 | dev_err(&pdev->dev, "Cannot allocate memory\n"); |
| 636 | rc = -ENOMEM; |
| 637 | goto out_error_nomem; |
| 638 | } |
| 639 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 640 | spin_lock_init(&(iface->lock)); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 641 | |
| 642 | /* Find and map our resources */ |
| 643 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 644 | if (res == NULL) { |
| 645 | dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n"); |
| 646 | rc = -ENOENT; |
| 647 | goto out_error_get_res; |
| 648 | } |
| 649 | |
Linus Walleij | c6ffdde | 2009-06-14 00:20:36 +0200 | [diff] [blame] | 650 | iface->regs_base = ioremap(res->start, resource_size(res)); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 651 | if (iface->regs_base == NULL) { |
| 652 | dev_err(&pdev->dev, "Cannot map IO\n"); |
| 653 | rc = -ENXIO; |
| 654 | goto out_error_ioremap; |
| 655 | } |
| 656 | |
| 657 | iface->irq = platform_get_irq(pdev, 0); |
| 658 | if (iface->irq < 0) { |
| 659 | dev_err(&pdev->dev, "No IRQ specified\n"); |
| 660 | rc = -ENOENT; |
| 661 | goto out_error_no_irq; |
| 662 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 663 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 664 | p_adap = &iface->adap; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 665 | p_adap->nr = pdev->id; |
| 666 | strlcpy(p_adap->name, pdev->name, sizeof(p_adap->name)); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 667 | p_adap->algo = &bfin_twi_algorithm; |
| 668 | p_adap->algo_data = iface; |
Jean Delvare | e1995f6 | 2009-01-07 14:29:16 +0100 | [diff] [blame] | 669 | p_adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 670 | p_adap->dev.parent = &pdev->dev; |
Sonic Zhang | dd7319a | 2010-03-22 03:23:16 -0400 | [diff] [blame] | 671 | p_adap->timeout = 5 * HZ; |
| 672 | p_adap->retries = 3; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 673 | |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 674 | rc = peripheral_request_list(pin_req[pdev->id], "i2c-bfin-twi"); |
| 675 | if (rc) { |
| 676 | dev_err(&pdev->dev, "Can't setup pin mux!\n"); |
| 677 | goto out_error_pin_mux; |
| 678 | } |
| 679 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 680 | rc = request_irq(iface->irq, bfin_twi_interrupt_entry, |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 681 | IRQF_DISABLED, pdev->name, iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 682 | if (rc) { |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 683 | dev_err(&pdev->dev, "Can't get IRQ %d !\n", iface->irq); |
| 684 | rc = -ENODEV; |
| 685 | goto out_error_req_irq; |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* Set TWI internal clock as 10MHz */ |
Sonic Zhang | ac07fb4 | 2009-12-21 09:28:30 -0500 | [diff] [blame] | 689 | write_CONTROL(iface, ((get_sclk() / 1000 / 1000 + 5) / 10) & 0x7F); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 690 | |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 691 | /* |
| 692 | * 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] | 693 | * 20kHz SCL or less in Kconfig now. (5 * 1000 / 20 = 250) |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 694 | */ |
Sonic Zhang | ac07fb4 | 2009-12-21 09:28:30 -0500 | [diff] [blame] | 695 | clkhilow = ((10 * 1000 / CONFIG_I2C_BLACKFIN_TWI_CLK_KHZ) + 1) / 2; |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 696 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 697 | /* Set Twi interface clock as specified */ |
Michael Hennerich | 9528d1c | 2009-05-18 08:14:41 -0400 | [diff] [blame] | 698 | write_CLKDIV(iface, (clkhilow << 8) | clkhilow); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 699 | |
| 700 | /* Enable TWI */ |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 701 | write_CONTROL(iface, read_CONTROL(iface) | TWI_ENA); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 702 | SSYNC(); |
| 703 | |
Kalle Pokki | 991dee5 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 704 | rc = i2c_add_numbered_adapter(p_adap); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 705 | if (rc < 0) { |
| 706 | dev_err(&pdev->dev, "Can't add i2c adapter!\n"); |
| 707 | goto out_error_add_adapter; |
| 708 | } |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 709 | |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 710 | platform_set_drvdata(pdev, iface); |
| 711 | |
Bryan Wu | fa6ad22 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 712 | dev_info(&pdev->dev, "Blackfin BF5xx on-chip I2C TWI Contoller, " |
| 713 | "regs_base@%p\n", iface->regs_base); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 714 | |
| 715 | return 0; |
| 716 | |
| 717 | out_error_add_adapter: |
| 718 | free_irq(iface->irq, iface); |
| 719 | out_error_req_irq: |
| 720 | out_error_no_irq: |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 721 | peripheral_free_list(pin_req[pdev->id]); |
| 722 | out_error_pin_mux: |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 723 | iounmap(iface->regs_base); |
| 724 | out_error_ioremap: |
| 725 | out_error_get_res: |
| 726 | kfree(iface); |
| 727 | out_error_nomem: |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 728 | return rc; |
| 729 | } |
| 730 | |
| 731 | static int i2c_bfin_twi_remove(struct platform_device *pdev) |
| 732 | { |
| 733 | struct bfin_twi_iface *iface = platform_get_drvdata(pdev); |
| 734 | |
| 735 | platform_set_drvdata(pdev, NULL); |
| 736 | |
| 737 | i2c_del_adapter(&(iface->adap)); |
| 738 | free_irq(iface->irq, iface); |
Bryan Wu | 74d362e | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 739 | peripheral_free_list(pin_req[pdev->id]); |
Bryan Wu | aa3d020 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 740 | iounmap(iface->regs_base); |
| 741 | kfree(iface); |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static struct platform_driver i2c_bfin_twi_driver = { |
| 747 | .probe = i2c_bfin_twi_probe, |
| 748 | .remove = i2c_bfin_twi_remove, |
| 749 | .suspend = i2c_bfin_twi_suspend, |
| 750 | .resume = i2c_bfin_twi_resume, |
| 751 | .driver = { |
| 752 | .name = "i2c-bfin-twi", |
| 753 | .owner = THIS_MODULE, |
| 754 | }, |
| 755 | }; |
| 756 | |
| 757 | static int __init i2c_bfin_twi_init(void) |
| 758 | { |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 759 | return platform_driver_register(&i2c_bfin_twi_driver); |
| 760 | } |
| 761 | |
| 762 | static void __exit i2c_bfin_twi_exit(void) |
| 763 | { |
| 764 | platform_driver_unregister(&i2c_bfin_twi_driver); |
| 765 | } |
| 766 | |
Bryan Wu | d24ecfc | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 767 | module_init(i2c_bfin_twi_init); |
| 768 | module_exit(i2c_bfin_twi_exit); |
Bryan Wu | fa6ad22 | 2008-04-22 22:16:48 +0200 | [diff] [blame] | 769 | |
| 770 | MODULE_AUTHOR("Bryan Wu, Sonic Zhang"); |
| 771 | MODULE_DESCRIPTION("Blackfin BF5xx on-chip I2C TWI Contoller Driver"); |
| 772 | MODULE_LICENSE("GPL"); |
Kay Sievers | add8eda | 2008-04-22 22:16:49 +0200 | [diff] [blame] | 773 | MODULE_ALIAS("platform:i2c-bfin-twi"); |