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