Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * Redistribution and use in source and binary forms, with or without |
| 4 | * modification, are permitted provided that the following conditions are |
| 5 | * met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above |
| 9 | * copyright notice, this list of conditions and the following |
| 10 | * disclaimer in the documentation and/or other materials provided |
| 11 | * with the distribution. |
| 12 | * * Neither the name of Code Aurora Forum, Inc. nor the names of its |
| 13 | * contributors may be used to endorse or promote products derived |
| 14 | * from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #ifndef __I2C_QUP__ |
| 30 | #define __I2C_QUP__ |
| 31 | |
| 32 | /** |
| 33 | * struct i2c_msg - an I2C transaction segment beginning with START |
| 34 | * @addr: Slave address, either seven or ten bits. When this is a ten |
| 35 | * bit address, I2C_M_TEN must be set in @flags and the adapter |
| 36 | * must support I2C_FUNC_10BIT_ADDR. |
| 37 | * @flags: I2C_M_RD is handled by all adapters. No other flags may be |
| 38 | * provided unless the adapter exported the relevant I2C_FUNC_* |
| 39 | * flags through i2c_check_functionality(). |
| 40 | * @len: Number of data bytes in @buf being read from or written to the |
| 41 | * I2C slave address. For read transactions where I2C_M_RECV_LEN |
| 42 | * is set, the caller guarantees that this buffer can hold up to |
| 43 | * 32 bytes in addition to the initial length byte sent by the |
| 44 | * slave (plus, if used, the SMBus PEC); and this value will be |
| 45 | * incremented by the number of block data bytes received. |
| 46 | * @buf: The buffer into which data is read, or from which it's written. |
| 47 | * |
| 48 | * An i2c_msg is the low level representation of one segment of an I2C |
| 49 | * transaction. It is visible to drivers in the @i2c_transfer() procedure, |
| 50 | * to userspace from i2c-dev, and to I2C adapter drivers through the |
| 51 | * @i2c_adapter.@master_xfer() method. |
| 52 | * |
| 53 | * Except when I2C "protocol mangling" is used, all I2C adapters implement |
| 54 | * the standard rules for I2C transactions. Each transaction begins with a |
| 55 | * START. That is followed by the slave address, and a bit encoding read |
| 56 | * versus write. Then follow all the data bytes, possibly including a byte |
| 57 | * with SMBus PEC. The transfer terminates with a NAK, or when all those |
| 58 | * bytes have been transferred and ACKed. If this is the last message in a |
| 59 | * group, it is followed by a STOP. Otherwise it is followed by the next |
| 60 | * @i2c_msg transaction segment, beginning with a (repeated) START. |
| 61 | * |
| 62 | * Alternatively, when the adapter supports I2C_FUNC_PROTOCOL_MANGLING then |
| 63 | * passing certain @flags may have changed those standard protocol behaviors. |
| 64 | * Those flags are only for use with broken/nonconforming slaves, and with |
| 65 | * adapters which are known to support the specific mangling options they |
| 66 | * need (one or more of IGNORE_NAK, NO_RD_ACK, NOSTART, and REV_DIR_ADDR). |
| 67 | */ |
| 68 | struct i2c_msg { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 69 | unsigned short addr; /* slave address */ |
| 70 | unsigned short flags; |
| 71 | #define I2C_M_TEN 0x0010 /* this is a ten bit chip address */ |
| 72 | #define I2C_M_WR 0x0000 /* write data, from master to slave */ |
| 73 | #define I2C_M_RD 0x0001 /* read data, from slave to master */ |
| 74 | #define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */ |
| 75 | #define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */ |
| 76 | #define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */ |
| 77 | #define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */ |
| 78 | #define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */ |
| 79 | unsigned short len; /* msg length */ |
| 80 | unsigned char *buf; /* pointer to msg data */ |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | struct qup_i2c_dev { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 84 | unsigned int gsbi_base; |
| 85 | unsigned int qup_base; |
| 86 | unsigned int gsbi_number; |
| 87 | int qup_irq; |
| 88 | int num_irqs; |
| 89 | struct i2c_msg *msg; |
| 90 | int pos; |
| 91 | int cnt; |
| 92 | int err; |
| 93 | int mode; |
| 94 | int clk_ctl; |
| 95 | int clk_freq; |
| 96 | int src_clk_freq; |
| 97 | int one_bit_t; |
| 98 | int out_fifo_sz; |
| 99 | int in_fifo_sz; |
| 100 | int out_blk_sz; |
| 101 | int in_blk_sz; |
| 102 | int wr_sz; |
| 103 | int suspended; |
| 104 | int clk_state; |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | /* Function Definitions */ |
Amol Jadi | c52c8a3 | 2011-07-12 11:27:04 -0700 | [diff] [blame] | 108 | struct qup_i2c_dev *qup_i2c_init(uint8_t gsbi_id, |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 109 | unsigned clk_freq, unsigned src_clk_freq); |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 110 | int qup_i2c_deinit(struct qup_i2c_dev *dev); |
| 111 | int qup_i2c_xfer(struct qup_i2c_dev *dev, struct i2c_msg msgs[], int num); |
| 112 | |
| 113 | struct device { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 114 | struct device *parent; |
| 115 | const char *init_name; /* initial name of the device */ |
| 116 | void (*release) (struct device * dev); |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | /** |
| 120 | * enum irqreturn |
| 121 | * @IRQ_NONE interrupt was not from this device |
| 122 | * @IRQ_HANDLED interrupt was handled by this device |
| 123 | * @IRQ_WAKE_THREAD handler requests to wake the handler thread |
| 124 | */ |
| 125 | enum irqreturn { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 126 | IRQ_NONE, |
| 127 | IRQ_HANDLED, |
| 128 | IRQ_WAKE_THREAD, |
| 129 | IRQ_FAIL, |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | typedef enum irqreturn irqreturn_t; |
| 133 | |
| 134 | #define I2C_SMBUS_BLOCK_MAX 32 |
| 135 | union i2c_smbus_data { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 136 | unsigned char byte; |
| 137 | unsigned short word; |
| 138 | unsigned char block[I2C_SMBUS_BLOCK_MAX + 2]; |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | /* |
| 142 | * i2c_adapter is the structure used to identify a physical i2c bus along |
| 143 | * with the access algorithms necessary to access it. |
| 144 | */ |
| 145 | struct i2c_adapter { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 146 | struct module *owner; |
| 147 | unsigned int id; |
| 148 | unsigned int class; /* classes to allow probing for */ |
| 149 | const struct i2c_algorithm *algo; /* the algorithm to access the bus */ |
| 150 | void *algo_data; |
| 151 | /* data fields that are valid for all devices */ |
| 152 | unsigned int level; /* nesting level for lockdep */ |
| 153 | int timeout; /* in jiffies */ |
| 154 | int retries; |
| 155 | struct device dev; /* the adapter device */ |
| 156 | int nr; |
| 157 | char name[48]; |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | /* |
| 161 | * The following structs are for those who like to implement new bus drivers: |
| 162 | * i2c_algorithm is the interface to a class of hardware solutions which can |
| 163 | * be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584 |
| 164 | * to name two of the most common. |
| 165 | */ |
| 166 | struct i2c_algorithm { |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 167 | /* If an adapter algorithm can't do I2C-level access, set master_xfer to |
| 168 | NULL. If an adapter algorithm can do SMBus access, set smbus_xfer. If |
| 169 | set to NULL, the SMBus protocol is simulated using common I2C messages */ |
| 170 | /* master_xfer should return the number of messages successfully processed, |
| 171 | or a negative value on error */ |
| 172 | int (*master_xfer) (struct i2c_adapter * adap, struct i2c_msg * msgs, |
| 173 | int num); |
| 174 | int (*smbus_xfer) (struct i2c_adapter * adap, unsigned short addr, |
| 175 | unsigned short flags, char read_write, |
| 176 | unsigned char command, int size, |
| 177 | union i2c_smbus_data * data); |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 178 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 179 | /* To determine what the adapter supports */ |
| 180 | unsigned int (*functionality) (struct i2c_adapter *); |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | #define EIO 5 |
| 184 | #define ENOMEM 12 |
| 185 | #define EBUSY 16 |
| 186 | #define ENODEV 19 |
| 187 | #define ENOSYS 38 |
| 188 | #define EPROTONOSUPPORT 93 |
| 189 | #define ETIMEDOUT 110 |
| 190 | |
| 191 | #define FALSE 0 |
| 192 | #define TRUE 1 |
| 193 | |
| 194 | #define USEC_PER_SEC 1000000L |
| 195 | |
| 196 | #define IRQF_TRIGGER_NONE 0x00000000 |
| 197 | #define IRQF_TRIGGER_RISING 0x00000001 |
| 198 | #define IRQF_TRIGGER_FALLING 0x00000002 |
| 199 | #define IRQF_TRIGGER_HIGH 0x00000004 |
| 200 | #define IRQF_TRIGGER_LOW 0x00000008 |
| 201 | #define IRQF_TRIGGER_MASK (IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW | \ |
| 202 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING) |
| 203 | #define IRQF_TRIGGER_PROBE 0x00000010 |
| 204 | |
| 205 | /* To determine what functionality is present */ |
| 206 | |
| 207 | #define I2C_FUNC_I2C 0x00000001 |
| 208 | #define I2C_FUNC_10BIT_ADDR 0x00000002 |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 209 | #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_NOSTART etc. */ |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 210 | #define I2C_FUNC_SMBUS_PEC 0x00000008 |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 211 | #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */ |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 212 | #define I2C_FUNC_SMBUS_QUICK 0x00010000 |
| 213 | #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000 |
| 214 | #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000 |
| 215 | #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000 |
| 216 | #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000 |
| 217 | #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000 |
| 218 | #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000 |
| 219 | #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000 |
| 220 | #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000 |
| 221 | #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000 |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 222 | #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */ |
| 223 | #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */ |
Shashank Mittal | c69512e | 2010-09-22 16:40:48 -0700 | [diff] [blame] | 224 | |
| 225 | #define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \ |
| 226 | I2C_FUNC_SMBUS_WRITE_BYTE) |
| 227 | #define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \ |
| 228 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA) |
| 229 | #define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \ |
| 230 | I2C_FUNC_SMBUS_WRITE_WORD_DATA) |
| 231 | #define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \ |
| 232 | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA) |
| 233 | #define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \ |
| 234 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK) |
| 235 | |
| 236 | #define I2C_FUNC_SMBUS_EMUL (I2C_FUNC_SMBUS_QUICK | \ |
| 237 | I2C_FUNC_SMBUS_BYTE | \ |
| 238 | I2C_FUNC_SMBUS_BYTE_DATA | \ |
| 239 | I2C_FUNC_SMBUS_WORD_DATA | \ |
| 240 | I2C_FUNC_SMBUS_PROC_CALL | \ |
| 241 | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \ |
| 242 | I2C_FUNC_SMBUS_I2C_BLOCK | \ |
| 243 | I2C_FUNC_SMBUS_PEC) |
| 244 | |
Ajay Dudani | b01e506 | 2011-12-03 23:23:42 -0800 | [diff] [blame^] | 245 | #endif /* __I2C_QUP__ */ |