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