blob: b5fd2bd0f135815441075281204c457a7d30f02b [file] [log] [blame]
Channagoud Kadabi69787652013-01-18 10:15:32 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Shashank Mittalc69512e2010-09-22 16:40:48 -07002 *
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.
Channagoud Kadabi69787652013-01-18 10:15:32 -080012 * * Neither the name of The Linux Foundation, Inc. nor the names of its
Shashank Mittalc69512e2010-09-22 16:40:48 -070013 * 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
Channagoud Kadabi634ac6d2012-12-12 18:13:56 -080032#include <stdint.h>
33
Shashank Mittalc69512e2010-09-22 16:40:48 -070034/**
35 * struct i2c_msg - an I2C transaction segment beginning with START
36 * @addr: Slave address, either seven or ten bits. When this is a ten
37 * bit address, I2C_M_TEN must be set in @flags and the adapter
38 * must support I2C_FUNC_10BIT_ADDR.
39 * @flags: I2C_M_RD is handled by all adapters. No other flags may be
40 * provided unless the adapter exported the relevant I2C_FUNC_*
41 * flags through i2c_check_functionality().
42 * @len: Number of data bytes in @buf being read from or written to the
43 * I2C slave address. For read transactions where I2C_M_RECV_LEN
44 * is set, the caller guarantees that this buffer can hold up to
45 * 32 bytes in addition to the initial length byte sent by the
46 * slave (plus, if used, the SMBus PEC); and this value will be
47 * incremented by the number of block data bytes received.
48 * @buf: The buffer into which data is read, or from which it's written.
49 *
50 * An i2c_msg is the low level representation of one segment of an I2C
51 * transaction. It is visible to drivers in the @i2c_transfer() procedure,
52 * to userspace from i2c-dev, and to I2C adapter drivers through the
53 * @i2c_adapter.@master_xfer() method.
54 *
55 * Except when I2C "protocol mangling" is used, all I2C adapters implement
56 * the standard rules for I2C transactions. Each transaction begins with a
57 * START. That is followed by the slave address, and a bit encoding read
58 * versus write. Then follow all the data bytes, possibly including a byte
59 * with SMBus PEC. The transfer terminates with a NAK, or when all those
60 * bytes have been transferred and ACKed. If this is the last message in a
61 * group, it is followed by a STOP. Otherwise it is followed by the next
62 * @i2c_msg transaction segment, beginning with a (repeated) START.
63 *
64 * Alternatively, when the adapter supports I2C_FUNC_PROTOCOL_MANGLING then
65 * passing certain @flags may have changed those standard protocol behaviors.
66 * Those flags are only for use with broken/nonconforming slaves, and with
67 * adapters which are known to support the specific mangling options they
68 * need (one or more of IGNORE_NAK, NO_RD_ACK, NOSTART, and REV_DIR_ADDR).
69 */
70struct i2c_msg {
Ajay Dudanib01e5062011-12-03 23:23:42 -080071 unsigned short addr; /* slave address */
72 unsigned short flags;
73#define I2C_M_TEN 0x0010 /* this is a ten bit chip address */
74#define I2C_M_WR 0x0000 /* write data, from master to slave */
75#define I2C_M_RD 0x0001 /* read data, from slave to master */
76#define I2C_M_NOSTART 0x4000 /* if I2C_FUNC_PROTOCOL_MANGLING */
77#define I2C_M_REV_DIR_ADDR 0x2000 /* if I2C_FUNC_PROTOCOL_MANGLING */
78#define I2C_M_IGNORE_NAK 0x1000 /* if I2C_FUNC_PROTOCOL_MANGLING */
79#define I2C_M_NO_RD_ACK 0x0800 /* if I2C_FUNC_PROTOCOL_MANGLING */
80#define I2C_M_RECV_LEN 0x0400 /* length will be first received byte */
81 unsigned short len; /* msg length */
82 unsigned char *buf; /* pointer to msg data */
Shashank Mittalc69512e2010-09-22 16:40:48 -070083};
84
85struct qup_i2c_dev {
Ajay Dudanib01e5062011-12-03 23:23:42 -080086 unsigned int gsbi_base;
87 unsigned int qup_base;
88 unsigned int gsbi_number;
89 int qup_irq;
90 int num_irqs;
91 struct i2c_msg *msg;
92 int pos;
93 int cnt;
94 int err;
95 int mode;
96 int clk_ctl;
97 int clk_freq;
98 int src_clk_freq;
99 int one_bit_t;
100 int out_fifo_sz;
101 int in_fifo_sz;
102 int out_blk_sz;
103 int in_blk_sz;
104 int wr_sz;
105 int suspended;
106 int clk_state;
Shashank Mittalc69512e2010-09-22 16:40:48 -0700107};
108
109/* Function Definitions */
Amol Jadic52c8a32011-07-12 11:27:04 -0700110struct qup_i2c_dev *qup_i2c_init(uint8_t gsbi_id,
Ajay Dudanib01e5062011-12-03 23:23:42 -0800111 unsigned clk_freq, unsigned src_clk_freq);
Channagoud Kadabi634ac6d2012-12-12 18:13:56 -0800112struct qup_i2c_dev *qup_blsp_i2c_init(uint8_t blsp_id, uint8_t qup_id,
113 uint32_t clk_freq, uint32_t src_clk_freq);
Shashank Mittalc69512e2010-09-22 16:40:48 -0700114int qup_i2c_deinit(struct qup_i2c_dev *dev);
115int qup_i2c_xfer(struct qup_i2c_dev *dev, struct i2c_msg msgs[], int num);
116
117struct device {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800118 struct device *parent;
119 const char *init_name; /* initial name of the device */
120 void (*release) (struct device * dev);
Shashank Mittalc69512e2010-09-22 16:40:48 -0700121};
122
123/**
124 * enum irqreturn
125 * @IRQ_NONE interrupt was not from this device
126 * @IRQ_HANDLED interrupt was handled by this device
127 * @IRQ_WAKE_THREAD handler requests to wake the handler thread
128 */
129enum irqreturn {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800130 IRQ_NONE,
131 IRQ_HANDLED,
132 IRQ_WAKE_THREAD,
133 IRQ_FAIL,
Shashank Mittalc69512e2010-09-22 16:40:48 -0700134};
135
136typedef enum irqreturn irqreturn_t;
137
138#define I2C_SMBUS_BLOCK_MAX 32
139union i2c_smbus_data {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800140 unsigned char byte;
141 unsigned short word;
142 unsigned char block[I2C_SMBUS_BLOCK_MAX + 2];
Shashank Mittalc69512e2010-09-22 16:40:48 -0700143};
144
145/*
146 * i2c_adapter is the structure used to identify a physical i2c bus along
147 * with the access algorithms necessary to access it.
148 */
149struct i2c_adapter {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800150 struct module *owner;
151 unsigned int id;
152 unsigned int class; /* classes to allow probing for */
153 const struct i2c_algorithm *algo; /* the algorithm to access the bus */
154 void *algo_data;
155 /* data fields that are valid for all devices */
156 unsigned int level; /* nesting level for lockdep */
157 int timeout; /* in jiffies */
158 int retries;
159 struct device dev; /* the adapter device */
160 int nr;
161 char name[48];
Shashank Mittalc69512e2010-09-22 16:40:48 -0700162};
163
164/*
165 * The following structs are for those who like to implement new bus drivers:
166 * i2c_algorithm is the interface to a class of hardware solutions which can
167 * be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584
168 * to name two of the most common.
169 */
170struct i2c_algorithm {
Ajay Dudanib01e5062011-12-03 23:23:42 -0800171 /* If an adapter algorithm can't do I2C-level access, set master_xfer to
172 NULL. If an adapter algorithm can do SMBus access, set smbus_xfer. If
173 set to NULL, the SMBus protocol is simulated using common I2C messages */
174 /* master_xfer should return the number of messages successfully processed,
175 or a negative value on error */
176 int (*master_xfer) (struct i2c_adapter * adap, struct i2c_msg * msgs,
177 int num);
178 int (*smbus_xfer) (struct i2c_adapter * adap, unsigned short addr,
179 unsigned short flags, char read_write,
180 unsigned char command, int size,
181 union i2c_smbus_data * data);
Shashank Mittalc69512e2010-09-22 16:40:48 -0700182
Ajay Dudanib01e5062011-12-03 23:23:42 -0800183 /* To determine what the adapter supports */
184 unsigned int (*functionality) (struct i2c_adapter *);
Shashank Mittalc69512e2010-09-22 16:40:48 -0700185};
186
187#define EIO 5
188#define ENOMEM 12
189#define EBUSY 16
190#define ENODEV 19
191#define ENOSYS 38
192#define EPROTONOSUPPORT 93
193#define ETIMEDOUT 110
194
195#define FALSE 0
196#define TRUE 1
197
198#define USEC_PER_SEC 1000000L
199
200#define IRQF_TRIGGER_NONE 0x00000000
201#define IRQF_TRIGGER_RISING 0x00000001
202#define IRQF_TRIGGER_FALLING 0x00000002
203#define IRQF_TRIGGER_HIGH 0x00000004
204#define IRQF_TRIGGER_LOW 0x00000008
205#define IRQF_TRIGGER_MASK (IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW | \
206 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
207#define IRQF_TRIGGER_PROBE 0x00000010
208
209/* To determine what functionality is present */
210
211#define I2C_FUNC_I2C 0x00000001
212#define I2C_FUNC_10BIT_ADDR 0x00000002
Ajay Dudanib01e5062011-12-03 23:23:42 -0800213#define I2C_FUNC_PROTOCOL_MANGLING 0x00000004 /* I2C_M_NOSTART etc. */
Shashank Mittalc69512e2010-09-22 16:40:48 -0700214#define I2C_FUNC_SMBUS_PEC 0x00000008
Ajay Dudanib01e5062011-12-03 23:23:42 -0800215#define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 /* SMBus 2.0 */
Shashank Mittalc69512e2010-09-22 16:40:48 -0700216#define I2C_FUNC_SMBUS_QUICK 0x00010000
217#define I2C_FUNC_SMBUS_READ_BYTE 0x00020000
218#define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000
219#define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000
220#define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000
221#define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000
222#define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000
223#define I2C_FUNC_SMBUS_PROC_CALL 0x00800000
224#define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000
225#define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000
Ajay Dudanib01e5062011-12-03 23:23:42 -0800226#define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 /* I2C-like block xfer */
227#define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 /* w/ 1-byte reg. addr. */
Shashank Mittalc69512e2010-09-22 16:40:48 -0700228
229#define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
230 I2C_FUNC_SMBUS_WRITE_BYTE)
231#define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
232 I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
233#define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
234 I2C_FUNC_SMBUS_WRITE_WORD_DATA)
235#define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
236 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
237#define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
238 I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
239
240#define I2C_FUNC_SMBUS_EMUL (I2C_FUNC_SMBUS_QUICK | \
241 I2C_FUNC_SMBUS_BYTE | \
242 I2C_FUNC_SMBUS_BYTE_DATA | \
243 I2C_FUNC_SMBUS_WORD_DATA | \
244 I2C_FUNC_SMBUS_PROC_CALL | \
245 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA | \
246 I2C_FUNC_SMBUS_I2C_BLOCK | \
247 I2C_FUNC_SMBUS_PEC)
248
Ajay Dudanib01e5062011-12-03 23:23:42 -0800249#endif /* __I2C_QUP__ */