blob: bbea29b47e3f30580c5fd758243cd00aa35803ac [file] [log] [blame]
wdenk1f045212002-03-10 14:37:15 +00001/*
2 * (C) Copyright 2001
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk1f045212002-03-10 14:37:15 +00006 *
7 * The original I2C interface was
8 * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
9 * AIRVENT SAM s.p.a - RIMINI(ITALY)
10 * but has been changed substantially.
11 */
12
13#ifndef _I2C_H_
14#define _I2C_H_
15
16/*
17 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
18 *
19 * The implementation MUST NOT use static or global variables if the
20 * I2C routines are used to read SDRAM configuration information
21 * because this is done before the memories are initialized. Limited
22 * use of stack-based variables are OK (the initial stack size is
23 * limited).
24 *
25 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
26 */
27
28/*
29 * Configuration items.
30 */
31#define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
32
Marek Vasut513b8202011-10-25 11:40:57 +020033#ifdef CONFIG_I2C_MULTI_BUS
34#define MAX_I2C_BUS 2
35#define I2C_MULTI_BUS 1
Stefan Roesed07117e2007-02-20 10:27:08 +010036#else
Marek Vasut513b8202011-10-25 11:40:57 +020037#define MAX_I2C_BUS 1
38#define I2C_MULTI_BUS 0
39#endif
40
41#if !defined(CONFIG_SYS_MAX_I2C_BUS)
42#define CONFIG_SYS_MAX_I2C_BUS MAX_I2C_BUS
Stefan Roesed07117e2007-02-20 10:27:08 +010043#endif
44
Stefan Roese9de4fcb2007-03-01 07:03:25 +010045/* define the I2C bus number for RTC and DTT if not already done */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020046#if !defined(CONFIG_SYS_RTC_BUS_NUM)
47#define CONFIG_SYS_RTC_BUS_NUM 0
Stefan Roese9de4fcb2007-03-01 07:03:25 +010048#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020049#if !defined(CONFIG_SYS_DTT_BUS_NUM)
50#define CONFIG_SYS_DTT_BUS_NUM 0
Stefan Roese9de4fcb2007-03-01 07:03:25 +010051#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020052#if !defined(CONFIG_SYS_SPD_BUS_NUM)
53#define CONFIG_SYS_SPD_BUS_NUM 0
Matthias Fuchs66067c32007-03-08 16:20:32 +010054#endif
Stefan Roese9de4fcb2007-03-01 07:03:25 +010055
Heiko Schocherdc7d22a2008-10-15 09:35:26 +020056#ifndef I2C_SOFT_DECLARATIONS
57# if defined(CONFIG_MPC8260)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020058# define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
Heiko Schocherdc7d22a2008-10-15 09:35:26 +020059# elif defined(CONFIG_8xx)
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020060# define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010061
62# elif (defined(CONFIG_AT91RM9200) || \
63 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
64 defined(CONFIG_AT91SAM9263)) && !defined(CONFIG_AT91_LEGACY)
esw@bus-elektronik.de5082bab2011-12-20 06:05:30 +000065# define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
Heiko Schocherdc7d22a2008-10-15 09:35:26 +020066# else
67# define I2C_SOFT_DECLARATIONS
68# endif
69#endif
Timur Tabi13d598f2008-12-03 11:28:30 -060070
71#ifdef CONFIG_8xx
Peter Tyser769069e2009-04-24 15:34:05 -050072/* Set default value for the I2C bus speed on 8xx. In the
Timur Tabi13d598f2008-12-03 11:28:30 -060073 * future, we'll define these in all 8xx board config files.
74 */
75#ifndef CONFIG_SYS_I2C_SPEED
76#define CONFIG_SYS_I2C_SPEED 50000
77#endif
Peter Tyser769069e2009-04-24 15:34:05 -050078#endif
Timur Tabi13d598f2008-12-03 11:28:30 -060079
Peter Tyser769069e2009-04-24 15:34:05 -050080/*
81 * Many boards/controllers/drivers don't support an I2C slave interface so
82 * provide a default slave address for them for use in common code. A real
83 * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
84 * support a slave interface.
85 */
Timur Tabi13d598f2008-12-03 11:28:30 -060086#ifndef CONFIG_SYS_I2C_SLAVE
Peter Tyser769069e2009-04-24 15:34:05 -050087#define CONFIG_SYS_I2C_SLAVE 0xfe
Timur Tabi13d598f2008-12-03 11:28:30 -060088#endif
89
wdenk1f045212002-03-10 14:37:15 +000090/*
91 * Initialization, must be called once on start up, may be called
92 * repeatedly to change the speed and slave addresses.
93 */
94void i2c_init(int speed, int slaveaddr);
wdenk6b58f332003-03-14 20:47:52 +000095void i2c_init_board(void);
Richard Retanubundf0149c2010-04-12 15:08:17 -040096#ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
97void i2c_board_late_init(void);
98#endif
wdenk1f045212002-03-10 14:37:15 +000099
Heiko Schocher6ee861b2008-10-15 09:39:47 +0200100#if defined(CONFIG_I2C_MUX)
101
102typedef struct _mux {
103 uchar chip;
104 uchar channel;
105 char *name;
106 struct _mux *next;
107} I2C_MUX;
108
109typedef struct _mux_device {
110 int busid;
111 I2C_MUX *mux; /* List of muxes, to reach the device */
112 struct _mux_device *next;
113} I2C_MUX_DEVICE;
114
Heiko Schocher6ee861b2008-10-15 09:39:47 +0200115I2C_MUX_DEVICE *i2c_mux_search_device(int id);
116I2C_MUX_DEVICE *i2c_mux_ident_muxstring (uchar *buf);
117int i2x_mux_select_mux(int bus);
118int i2c_mux_ident_muxstring_f (uchar *buf);
119#endif
120
wdenk1f045212002-03-10 14:37:15 +0000121/*
122 * Probe the given I2C chip address. Returns 0 if a chip responded,
123 * not 0 on failure.
124 */
125int i2c_probe(uchar chip);
126
127/*
128 * Read/Write interface:
129 * chip: I2C chip address, range 0..127
130 * addr: Memory (register) address within the chip
131 * alen: Number of bytes to use for addr (typically 1, 2 for larger
132 * memories, 0 for register type devices with only one
133 * register)
134 * buffer: Where to read/write the data
135 * len: How many bytes to read/write
136 *
137 * Returns: 0 on success, not 0 on failure
138 */
139int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
140int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
141
142/*
143 * Utility routines to read/write registers.
144 */
Timur Tabi13d598f2008-12-03 11:28:30 -0600145static inline u8 i2c_reg_read(u8 addr, u8 reg)
146{
147 u8 buf;
148
149#ifdef CONFIG_8xx
150 /* MPC8xx needs this. Maybe one day we can get rid of it. */
151 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
152#endif
153
154#ifdef DEBUG
155 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
156#endif
157
Timur Tabi13d598f2008-12-03 11:28:30 -0600158 i2c_read(addr, reg, 1, &buf, 1);
Timur Tabi13d598f2008-12-03 11:28:30 -0600159
160 return buf;
161}
162
163static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
164{
165#ifdef CONFIG_8xx
166 /* MPC8xx needs this. Maybe one day we can get rid of it. */
167 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
168#endif
169
170#ifdef DEBUG
171 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
172 __func__, addr, reg, val);
173#endif
174
Timur Tabi13d598f2008-12-03 11:28:30 -0600175 i2c_write(addr, reg, 1, &val, 1);
Timur Tabi13d598f2008-12-03 11:28:30 -0600176}
wdenk1f045212002-03-10 14:37:15 +0000177
Ben Warren45657152006-09-07 16:50:54 -0400178/*
179 * Functions for setting the current I2C bus and its speed
180 */
181
182/*
183 * i2c_set_bus_num:
184 *
185 * Change the active I2C bus. Subsequent read/write calls will
186 * go to this one.
187 *
Wolfgang Denka1be4762008-05-20 16:00:29 +0200188 * bus - bus index, zero based
Ben Warren45657152006-09-07 16:50:54 -0400189 *
Wolfgang Denka1be4762008-05-20 16:00:29 +0200190 * Returns: 0 on success, not 0 on failure
Ben Warren45657152006-09-07 16:50:54 -0400191 *
192 */
Timur Tabic0b114a2006-10-31 21:23:16 -0600193int i2c_set_bus_num(unsigned int bus);
Ben Warren45657152006-09-07 16:50:54 -0400194
195/*
196 * i2c_get_bus_num:
197 *
198 * Returns index of currently active I2C bus. Zero-based.
199 */
200
Timur Tabic0b114a2006-10-31 21:23:16 -0600201unsigned int i2c_get_bus_num(void);
Ben Warren45657152006-09-07 16:50:54 -0400202
203/*
204 * i2c_set_bus_speed:
205 *
206 * Change the speed of the active I2C bus
207 *
Wolfgang Denka1be4762008-05-20 16:00:29 +0200208 * speed - bus speed in Hz
Ben Warren45657152006-09-07 16:50:54 -0400209 *
Wolfgang Denka1be4762008-05-20 16:00:29 +0200210 * Returns: 0 on success, not 0 on failure
Ben Warren45657152006-09-07 16:50:54 -0400211 *
212 */
Timur Tabic0b114a2006-10-31 21:23:16 -0600213int i2c_set_bus_speed(unsigned int);
Ben Warren45657152006-09-07 16:50:54 -0400214
215/*
216 * i2c_get_bus_speed:
217 *
218 * Returns speed of currently active I2C bus in Hz
219 */
220
Timur Tabic0b114a2006-10-31 21:23:16 -0600221unsigned int i2c_get_bus_speed(void);
Ben Warren45657152006-09-07 16:50:54 -0400222
Marek Vasut513b8202011-10-25 11:40:57 +0200223/* NOTE: These two functions MUST be always_inline to avoid code growth! */
224static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
225static inline unsigned int I2C_GET_BUS(void)
226{
227 return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
228}
229
230static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
231static inline void I2C_SET_BUS(unsigned int bus)
232{
233 if (I2C_MULTI_BUS)
234 i2c_set_bus_num(bus);
235}
236
Ɓukasz Majewskib2d76522012-09-04 23:15:20 +0000237/* Multi I2C definitions */
238enum {
239 I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
240 I2C_8, I2C_9, I2C_10,
241};
242
243/* Multi I2C busses handling */
244#ifdef CONFIG_SOFT_I2C_MULTI_BUS
245extern int get_multi_scl_pin(void);
246extern int get_multi_sda_pin(void);
247extern int multi_i2c_init(void);
248#endif
Rajeshwari Shinde53cfac52012-12-26 20:03:12 +0000249
250/**
251 * Get FDT values for i2c bus.
252 *
253 * @param blob Device tree blbo
254 * @return the number of I2C bus
255 */
256void board_i2c_init(const void *blob);
257
258/**
259 * Find the I2C bus number by given a FDT I2C node.
260 *
261 * @param blob Device tree blbo
262 * @param node FDT I2C node to find
263 * @return the number of I2C bus (zero based), or -1 on error
264 */
265int i2c_get_bus_num_fdt(int node);
266
267/**
268 * Reset the I2C bus represented by the given a FDT I2C node.
269 *
270 * @param blob Device tree blbo
271 * @param node FDT I2C node to find
272 * @return 0 if port was reset, -1 if not found
273 */
274int i2c_reset_port_fdt(const void *blob, int node);
wdenk1f045212002-03-10 14:37:15 +0000275#endif /* _I2C_H_ */