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