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