xref: /openbmc/u-boot/include/i2c.h (revision c6202d857ebce5d6c8f0ec1d8d30352195546280)
11f045217Swdenk /*
2385c9ef5SHeiko Schocher  * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
3385c9ef5SHeiko Schocher  * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de>
4385c9ef5SHeiko Schocher  * Changes for multibus/multiadapter I2C support.
5385c9ef5SHeiko Schocher  *
61f045217Swdenk  * (C) Copyright 2001
71f045217Swdenk  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
81f045217Swdenk  *
91a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
101f045217Swdenk  *
111f045217Swdenk  * The original I2C interface was
121f045217Swdenk  *   (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
131f045217Swdenk  *   AIRVENT SAM s.p.a - RIMINI(ITALY)
141f045217Swdenk  * but has been changed substantially.
151f045217Swdenk  */
161f045217Swdenk 
171f045217Swdenk #ifndef _I2C_H_
181f045217Swdenk #define _I2C_H_
191f045217Swdenk 
201f045217Swdenk /*
21*c6202d85SSimon Glass  * For now there are essentially two parts to this file - driver model
22*c6202d85SSimon Glass  * here at the top, and the older code below (with CONFIG_SYS_I2C being
23*c6202d85SSimon Glass  * most recent). The plan is to migrate everything to driver model.
24*c6202d85SSimon Glass  * The driver model structures and API are separate as they are different
25*c6202d85SSimon Glass  * enough as to be incompatible for compilation purposes.
26*c6202d85SSimon Glass  */
27*c6202d85SSimon Glass 
28*c6202d85SSimon Glass #ifdef CONFIG_DM_I2C
29*c6202d85SSimon Glass 
30*c6202d85SSimon Glass enum dm_i2c_chip_flags {
31*c6202d85SSimon Glass 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit addressing */
32*c6202d85SSimon Glass 	DM_I2C_CHIP_RD_ADDRESS	= 1 << 1, /* Send address for each read byte */
33*c6202d85SSimon Glass 	DM_I2C_CHIP_WR_ADDRESS	= 1 << 2, /* Send address for each write byte */
34*c6202d85SSimon Glass };
35*c6202d85SSimon Glass 
36*c6202d85SSimon Glass /**
37*c6202d85SSimon Glass  * struct dm_i2c_chip - information about an i2c chip
38*c6202d85SSimon Glass  *
39*c6202d85SSimon Glass  * An I2C chip is a device on the I2C bus. It sits at a particular address
40*c6202d85SSimon Glass  * and normally supports 7-bit or 10-bit addressing.
41*c6202d85SSimon Glass  *
42*c6202d85SSimon Glass  * To obtain this structure, use dev_get_parentdata(dev) where dev is the
43*c6202d85SSimon Glass  * chip to examine.
44*c6202d85SSimon Glass  *
45*c6202d85SSimon Glass  * @chip_addr:	Chip address on bus
46*c6202d85SSimon Glass  * @offset_len: Length of offset in bytes. A single byte offset can
47*c6202d85SSimon Glass  *		represent up to 256 bytes. A value larger than 1 may be
48*c6202d85SSimon Glass  *		needed for larger devices.
49*c6202d85SSimon Glass  * @flags:	Flags for this chip (dm_i2c_chip_flags)
50*c6202d85SSimon Glass  * @emul: Emulator for this chip address (only used for emulation)
51*c6202d85SSimon Glass  */
52*c6202d85SSimon Glass struct dm_i2c_chip {
53*c6202d85SSimon Glass 	uint chip_addr;
54*c6202d85SSimon Glass 	uint offset_len;
55*c6202d85SSimon Glass 	uint flags;
56*c6202d85SSimon Glass #ifdef CONFIG_SANDBOX
57*c6202d85SSimon Glass 	struct udevice *emul;
58*c6202d85SSimon Glass #endif
59*c6202d85SSimon Glass };
60*c6202d85SSimon Glass 
61*c6202d85SSimon Glass /**
62*c6202d85SSimon Glass  * struct dm_i2c_bus- information about an i2c bus
63*c6202d85SSimon Glass  *
64*c6202d85SSimon Glass  * An I2C bus contains 0 or more chips on it, each at its own address. The
65*c6202d85SSimon Glass  * bus can operate at different speeds (measured in Hz, typically 100KHz
66*c6202d85SSimon Glass  * or 400KHz).
67*c6202d85SSimon Glass  *
68*c6202d85SSimon Glass  * To obtain this structure, use bus->uclass_priv where bus is the I2C
69*c6202d85SSimon Glass  * bus udevice.
70*c6202d85SSimon Glass  *
71*c6202d85SSimon Glass  * @speed_hz: Bus speed in hertz (typically 100000)
72*c6202d85SSimon Glass  */
73*c6202d85SSimon Glass struct dm_i2c_bus {
74*c6202d85SSimon Glass 	int speed_hz;
75*c6202d85SSimon Glass };
76*c6202d85SSimon Glass 
77*c6202d85SSimon Glass /**
78*c6202d85SSimon Glass  * i2c_read() - read bytes from an I2C chip
79*c6202d85SSimon Glass  *
80*c6202d85SSimon Glass  * To obtain an I2C device (called a 'chip') given the I2C bus address you
81*c6202d85SSimon Glass  * can use i2c_get_chip(). To obtain a bus by bus number use
82*c6202d85SSimon Glass  * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
83*c6202d85SSimon Glass  *
84*c6202d85SSimon Glass  * To set the address length of a devce use i2c_set_addr_len(). It
85*c6202d85SSimon Glass  * defaults to 1.
86*c6202d85SSimon Glass  *
87*c6202d85SSimon Glass  * @dev:	Chip to read from
88*c6202d85SSimon Glass  * @offset:	Offset within chip to start reading
89*c6202d85SSimon Glass  * @buffer:	Place to put data
90*c6202d85SSimon Glass  * @len:	Number of bytes to read
91*c6202d85SSimon Glass  *
92*c6202d85SSimon Glass  * @return 0 on success, -ve on failure
93*c6202d85SSimon Glass  */
94*c6202d85SSimon Glass int i2c_read(struct udevice *dev, uint offset, uint8_t *buffer,
95*c6202d85SSimon Glass 	     int len);
96*c6202d85SSimon Glass 
97*c6202d85SSimon Glass /**
98*c6202d85SSimon Glass  * i2c_write() - write bytes to an I2C chip
99*c6202d85SSimon Glass  *
100*c6202d85SSimon Glass  * See notes for i2c_read() above.
101*c6202d85SSimon Glass  *
102*c6202d85SSimon Glass  * @dev:	Chip to write to
103*c6202d85SSimon Glass  * @offset:	Offset within chip to start writing
104*c6202d85SSimon Glass  * @buffer:	Buffer containing data to write
105*c6202d85SSimon Glass  * @len:	Number of bytes to write
106*c6202d85SSimon Glass  *
107*c6202d85SSimon Glass  * @return 0 on success, -ve on failure
108*c6202d85SSimon Glass  */
109*c6202d85SSimon Glass int i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
110*c6202d85SSimon Glass 	      int len);
111*c6202d85SSimon Glass 
112*c6202d85SSimon Glass /**
113*c6202d85SSimon Glass  * i2c_probe() - probe a particular chip address
114*c6202d85SSimon Glass  *
115*c6202d85SSimon Glass  * This can be useful to check for the existence of a chip on the bus.
116*c6202d85SSimon Glass  * It is typically implemented by writing the chip address to the bus
117*c6202d85SSimon Glass  * and checking that the chip replies with an ACK.
118*c6202d85SSimon Glass  *
119*c6202d85SSimon Glass  * @bus:	Bus to probe
120*c6202d85SSimon Glass  * @chip_addr:	7-bit address to probe (10-bit and others are not supported)
121*c6202d85SSimon Glass  * @chip_flags:	Flags for the probe (see enum dm_i2c_chip_flags)
122*c6202d85SSimon Glass  * @devp:	Returns the device found, or NULL if none
123*c6202d85SSimon Glass  * @return 0 if a chip was found at that address, -ve if not
124*c6202d85SSimon Glass  */
125*c6202d85SSimon Glass int i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
126*c6202d85SSimon Glass 	      struct udevice **devp);
127*c6202d85SSimon Glass 
128*c6202d85SSimon Glass /**
129*c6202d85SSimon Glass  * i2c_set_bus_speed() - set the speed of a bus
130*c6202d85SSimon Glass  *
131*c6202d85SSimon Glass  * @bus:	Bus to adjust
132*c6202d85SSimon Glass  * @speed:	Requested speed in Hz
133*c6202d85SSimon Glass  * @return 0 if OK, -EINVAL for invalid values
134*c6202d85SSimon Glass  */
135*c6202d85SSimon Glass int i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
136*c6202d85SSimon Glass 
137*c6202d85SSimon Glass /**
138*c6202d85SSimon Glass  * i2c_get_bus_speed() - get the speed of a bus
139*c6202d85SSimon Glass  *
140*c6202d85SSimon Glass  * @bus:	Bus to check
141*c6202d85SSimon Glass  * @return speed of selected I2C bus in Hz, -ve on error
142*c6202d85SSimon Glass  */
143*c6202d85SSimon Glass int i2c_get_bus_speed(struct udevice *bus);
144*c6202d85SSimon Glass 
145*c6202d85SSimon Glass /**
146*c6202d85SSimon Glass  * i2c_set_chip_flags() - set flags for a chip
147*c6202d85SSimon Glass  *
148*c6202d85SSimon Glass  * Typically addresses are 7 bits, but for 10-bit addresses you should set
149*c6202d85SSimon Glass  * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
150*c6202d85SSimon Glass  *
151*c6202d85SSimon Glass  * @dev:	Chip to adjust
152*c6202d85SSimon Glass  * @flags:	New flags
153*c6202d85SSimon Glass  * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error
154*c6202d85SSimon Glass  */
155*c6202d85SSimon Glass int i2c_set_chip_flags(struct udevice *dev, uint flags);
156*c6202d85SSimon Glass 
157*c6202d85SSimon Glass /**
158*c6202d85SSimon Glass  * i2c_get_chip_flags() - get flags for a chip
159*c6202d85SSimon Glass  *
160*c6202d85SSimon Glass  * @dev:	Chip to check
161*c6202d85SSimon Glass  * @flagsp:	Place to put flags
162*c6202d85SSimon Glass  * @return 0 if OK, other -ve value on error
163*c6202d85SSimon Glass  */
164*c6202d85SSimon Glass int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
165*c6202d85SSimon Glass 
166*c6202d85SSimon Glass /**
167*c6202d85SSimon Glass  * i2c_set_offset_len() - set the offset length for a chip
168*c6202d85SSimon Glass  *
169*c6202d85SSimon Glass  * The offset used to access a chip may be up to 4 bytes long. Typically it
170*c6202d85SSimon Glass  * is only 1 byte, which is enough for chips with 256 bytes of memory or
171*c6202d85SSimon Glass  * registers. The default value is 1, but you can call this function to
172*c6202d85SSimon Glass  * change it.
173*c6202d85SSimon Glass  *
174*c6202d85SSimon Glass  * @offset_len:	New offset length value (typically 1 or 2)
175*c6202d85SSimon Glass  */
176*c6202d85SSimon Glass 
177*c6202d85SSimon Glass int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
178*c6202d85SSimon Glass /**
179*c6202d85SSimon Glass  * i2c_deblock() - recover a bus that is in an unknown state
180*c6202d85SSimon Glass  *
181*c6202d85SSimon Glass  * See the deblock() method in 'struct dm_i2c_ops' for full information
182*c6202d85SSimon Glass  *
183*c6202d85SSimon Glass  * @bus:	Bus to recover
184*c6202d85SSimon Glass  * @return 0 if OK, -ve on error
185*c6202d85SSimon Glass  */
186*c6202d85SSimon Glass int i2c_deblock(struct udevice *bus);
187*c6202d85SSimon Glass 
188*c6202d85SSimon Glass /*
189*c6202d85SSimon Glass  * Not all of these flags are implemented in the U-Boot API
190*c6202d85SSimon Glass  */
191*c6202d85SSimon Glass enum dm_i2c_msg_flags {
192*c6202d85SSimon Glass 	I2C_M_TEN		= 0x0010, /* ten-bit chip address */
193*c6202d85SSimon Glass 	I2C_M_RD		= 0x0001, /* read data, from slave to master */
194*c6202d85SSimon Glass 	I2C_M_STOP		= 0x8000, /* send stop after this message */
195*c6202d85SSimon Glass 	I2C_M_NOSTART		= 0x4000, /* no start before this message */
196*c6202d85SSimon Glass 	I2C_M_REV_DIR_ADDR	= 0x2000, /* invert polarity of R/W bit */
197*c6202d85SSimon Glass 	I2C_M_IGNORE_NAK	= 0x1000, /* continue after NAK */
198*c6202d85SSimon Glass 	I2C_M_NO_RD_ACK		= 0x0800, /* skip the Ack bit on reads */
199*c6202d85SSimon Glass 	I2C_M_RECV_LEN		= 0x0400, /* length is first received byte */
200*c6202d85SSimon Glass };
201*c6202d85SSimon Glass 
202*c6202d85SSimon Glass /**
203*c6202d85SSimon Glass  * struct i2c_msg - an I2C message
204*c6202d85SSimon Glass  *
205*c6202d85SSimon Glass  * @addr:	Slave address
206*c6202d85SSimon Glass  * @flags:	Flags (see enum dm_i2c_msg_flags)
207*c6202d85SSimon Glass  * @len:	Length of buffer in bytes, may be 0 for a probe
208*c6202d85SSimon Glass  * @buf:	Buffer to send/receive, or NULL if no data
209*c6202d85SSimon Glass  */
210*c6202d85SSimon Glass struct i2c_msg {
211*c6202d85SSimon Glass 	uint addr;
212*c6202d85SSimon Glass 	uint flags;
213*c6202d85SSimon Glass 	uint len;
214*c6202d85SSimon Glass 	u8 *buf;
215*c6202d85SSimon Glass };
216*c6202d85SSimon Glass 
217*c6202d85SSimon Glass /**
218*c6202d85SSimon Glass  * struct i2c_msg_list - a list of I2C messages
219*c6202d85SSimon Glass  *
220*c6202d85SSimon Glass  * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
221*c6202d85SSimon Glass  * appropriate in U-Boot.
222*c6202d85SSimon Glass  *
223*c6202d85SSimon Glass  * @msg:	Pointer to i2c_msg array
224*c6202d85SSimon Glass  * @nmsgs:	Number of elements in the array
225*c6202d85SSimon Glass  */
226*c6202d85SSimon Glass struct i2c_msg_list {
227*c6202d85SSimon Glass 	struct i2c_msg *msgs;
228*c6202d85SSimon Glass 	uint nmsgs;
229*c6202d85SSimon Glass };
230*c6202d85SSimon Glass 
231*c6202d85SSimon Glass /**
232*c6202d85SSimon Glass  * struct dm_i2c_ops - driver operations for I2C uclass
233*c6202d85SSimon Glass  *
234*c6202d85SSimon Glass  * Drivers should support these operations unless otherwise noted. These
235*c6202d85SSimon Glass  * operations are intended to be used by uclass code, not directly from
236*c6202d85SSimon Glass  * other code.
237*c6202d85SSimon Glass  */
238*c6202d85SSimon Glass struct dm_i2c_ops {
239*c6202d85SSimon Glass 	/**
240*c6202d85SSimon Glass 	 * xfer() - transfer a list of I2C messages
241*c6202d85SSimon Glass 	 *
242*c6202d85SSimon Glass 	 * @bus:	Bus to read from
243*c6202d85SSimon Glass 	 * @msg:	List of messages to transfer
244*c6202d85SSimon Glass 	 * @nmsgs:	Number of messages in the list
245*c6202d85SSimon Glass 	 * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
246*c6202d85SSimon Glass 	 *	-ECOMM if the speed cannot be supported, -EPROTO if the chip
247*c6202d85SSimon Glass 	 *	flags cannot be supported, other -ve value on some other error
248*c6202d85SSimon Glass 	 */
249*c6202d85SSimon Glass 	int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
250*c6202d85SSimon Glass 
251*c6202d85SSimon Glass 	/**
252*c6202d85SSimon Glass 	 * probe_chip() - probe for the presense of a chip address
253*c6202d85SSimon Glass 	 *
254*c6202d85SSimon Glass 	 * This function is optional. If omitted, the uclass will send a zero
255*c6202d85SSimon Glass 	 * length message instead.
256*c6202d85SSimon Glass 	 *
257*c6202d85SSimon Glass 	 * @bus:	Bus to probe
258*c6202d85SSimon Glass 	 * @chip_addr:	Chip address to probe
259*c6202d85SSimon Glass 	 * @chip_flags:	Probe flags (enum dm_i2c_chip_flags)
260*c6202d85SSimon Glass 	 * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
261*c6202d85SSimon Glass 	 * to default probem other -ve value on error
262*c6202d85SSimon Glass 	 */
263*c6202d85SSimon Glass 	int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
264*c6202d85SSimon Glass 
265*c6202d85SSimon Glass 	/**
266*c6202d85SSimon Glass 	 * set_bus_speed() - set the speed of a bus (optional)
267*c6202d85SSimon Glass 	 *
268*c6202d85SSimon Glass 	 * The bus speed value will be updated by the uclass if this function
269*c6202d85SSimon Glass 	 * does not return an error. This method is optional - if it is not
270*c6202d85SSimon Glass 	 * provided then the driver can read the speed from
271*c6202d85SSimon Glass 	 * bus->uclass_priv->speed_hz
272*c6202d85SSimon Glass 	 *
273*c6202d85SSimon Glass 	 * @bus:	Bus to adjust
274*c6202d85SSimon Glass 	 * @speed:	Requested speed in Hz
275*c6202d85SSimon Glass 	 * @return 0 if OK, -EINVAL for invalid values
276*c6202d85SSimon Glass 	 */
277*c6202d85SSimon Glass 	int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
278*c6202d85SSimon Glass 
279*c6202d85SSimon Glass 	/**
280*c6202d85SSimon Glass 	 * get_bus_speed() - get the speed of a bus (optional)
281*c6202d85SSimon Glass 	 *
282*c6202d85SSimon Glass 	 * Normally this can be provided by the uclass, but if you want your
283*c6202d85SSimon Glass 	 * driver to check the bus speed by looking at the hardware, you can
284*c6202d85SSimon Glass 	 * implement that here. This method is optional. This method would
285*c6202d85SSimon Glass 	 * normally be expected to return bus->uclass_priv->speed_hz.
286*c6202d85SSimon Glass 	 *
287*c6202d85SSimon Glass 	 * @bus:	Bus to check
288*c6202d85SSimon Glass 	 * @return speed of selected I2C bus in Hz, -ve on error
289*c6202d85SSimon Glass 	 */
290*c6202d85SSimon Glass 	int (*get_bus_speed)(struct udevice *bus);
291*c6202d85SSimon Glass 
292*c6202d85SSimon Glass 	/**
293*c6202d85SSimon Glass 	 * set_flags() - set the flags for a chip (optional)
294*c6202d85SSimon Glass 	 *
295*c6202d85SSimon Glass 	 * This is generally implemented by the uclass, but drivers can
296*c6202d85SSimon Glass 	 * check the value to ensure that unsupported options are not used.
297*c6202d85SSimon Glass 	 * This method is optional. If provided, this method will always be
298*c6202d85SSimon Glass 	 * called when the flags change.
299*c6202d85SSimon Glass 	 *
300*c6202d85SSimon Glass 	 * @dev:	Chip to adjust
301*c6202d85SSimon Glass 	 * @flags:	New flags value
302*c6202d85SSimon Glass 	 * @return 0 if OK, -EINVAL if value is unsupported
303*c6202d85SSimon Glass 	 */
304*c6202d85SSimon Glass 	int (*set_flags)(struct udevice *dev, uint flags);
305*c6202d85SSimon Glass 
306*c6202d85SSimon Glass 	/**
307*c6202d85SSimon Glass 	 * deblock() - recover a bus that is in an unknown state
308*c6202d85SSimon Glass 	 *
309*c6202d85SSimon Glass 	 * I2C is a synchronous protocol and resets of the processor in the
310*c6202d85SSimon Glass 	 * middle of an access can block the I2C Bus until a powerdown of
311*c6202d85SSimon Glass 	 * the full unit is done. This is because slaves can be stuck
312*c6202d85SSimon Glass 	 * waiting for addition bus transitions for a transaction that will
313*c6202d85SSimon Glass 	 * never complete. Resetting the I2C master does not help. The only
314*c6202d85SSimon Glass 	 * way is to force the bus through a series of transitions to make
315*c6202d85SSimon Glass 	 * sure that all slaves are done with the transaction. This method
316*c6202d85SSimon Glass 	 * performs this 'deblocking' if support by the driver.
317*c6202d85SSimon Glass 	 *
318*c6202d85SSimon Glass 	 * This method is optional.
319*c6202d85SSimon Glass 	 */
320*c6202d85SSimon Glass 	int (*deblock)(struct udevice *bus);
321*c6202d85SSimon Glass };
322*c6202d85SSimon Glass 
323*c6202d85SSimon Glass #define i2c_get_ops(dev)	((struct dm_i2c_ops *)(dev)->driver->ops)
324*c6202d85SSimon Glass 
325*c6202d85SSimon Glass /**
326*c6202d85SSimon Glass  * i2c_get_chip() - get a device to use to access a chip on a bus
327*c6202d85SSimon Glass  *
328*c6202d85SSimon Glass  * This returns the device for the given chip address. The device can then
329*c6202d85SSimon Glass  * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
330*c6202d85SSimon Glass  *
331*c6202d85SSimon Glass  * @bus:	Bus to examine
332*c6202d85SSimon Glass  * @chip_addr:	Chip address for the new device
333*c6202d85SSimon Glass  * @devp:	Returns pointer to new device if found or -ENODEV if not
334*c6202d85SSimon Glass  *		found
335*c6202d85SSimon Glass  */
336*c6202d85SSimon Glass int i2c_get_chip(struct udevice *bus, uint chip_addr, struct udevice **devp);
337*c6202d85SSimon Glass 
338*c6202d85SSimon Glass /**
339*c6202d85SSimon Glass  * i2c_get_chip() - get a device to use to access a chip on a bus number
340*c6202d85SSimon Glass  *
341*c6202d85SSimon Glass  * This returns the device for the given chip address on a particular bus
342*c6202d85SSimon Glass  * number.
343*c6202d85SSimon Glass  *
344*c6202d85SSimon Glass  * @busnum:	Bus number to examine
345*c6202d85SSimon Glass  * @chip_addr:	Chip address for the new device
346*c6202d85SSimon Glass  * @devp:	Returns pointer to new device if found or -ENODEV if not
347*c6202d85SSimon Glass  *		found
348*c6202d85SSimon Glass  */
349*c6202d85SSimon Glass int i2c_get_chip_for_busnum(int busnum, int chip_addr, struct udevice **devp);
350*c6202d85SSimon Glass 
351*c6202d85SSimon Glass /**
352*c6202d85SSimon Glass  * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data
353*c6202d85SSimon Glass  *
354*c6202d85SSimon Glass  * This decodes the chip address from a device tree node and puts it into
355*c6202d85SSimon Glass  * its dm_i2c_chip structure. This should be called in your driver's
356*c6202d85SSimon Glass  * ofdata_to_platdata() method.
357*c6202d85SSimon Glass  *
358*c6202d85SSimon Glass  * @blob:	Device tree blob
359*c6202d85SSimon Glass  * @node:	Node offset to read from
360*c6202d85SSimon Glass  * @spi:	Place to put the decoded information
361*c6202d85SSimon Glass  */
362*c6202d85SSimon Glass int i2c_chip_ofdata_to_platdata(const void *blob, int node,
363*c6202d85SSimon Glass 				struct dm_i2c_chip *chip);
364*c6202d85SSimon Glass 
365*c6202d85SSimon Glass #endif
366*c6202d85SSimon Glass 
367*c6202d85SSimon Glass #ifndef CONFIG_DM_I2C
368*c6202d85SSimon Glass 
369*c6202d85SSimon Glass /*
3701f045217Swdenk  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
3711f045217Swdenk  *
3721f045217Swdenk  * The implementation MUST NOT use static or global variables if the
3731f045217Swdenk  * I2C routines are used to read SDRAM configuration information
3741f045217Swdenk  * because this is done before the memories are initialized. Limited
3751f045217Swdenk  * use of stack-based variables are OK (the initial stack size is
3761f045217Swdenk  * limited).
3771f045217Swdenk  *
3781f045217Swdenk  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
3791f045217Swdenk  */
3801f045217Swdenk 
3811f045217Swdenk /*
3821f045217Swdenk  * Configuration items.
3831f045217Swdenk  */
3841f045217Swdenk #define I2C_RXTX_LEN	128	/* maximum tx/rx buffer length */
3851f045217Swdenk 
386385c9ef5SHeiko Schocher #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
387385c9ef5SHeiko Schocher /* no muxes used bus = i2c adapters */
388385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_DIRECT_BUS	1
389385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_MAX_HOPS		0
390385c9ef5SHeiko Schocher #define CONFIG_SYS_NUM_I2C_BUSES	ll_entry_count(struct i2c_adapter, i2c)
39179b2d0bbSStefan Roese #else
392385c9ef5SHeiko Schocher /* we use i2c muxes */
393385c9ef5SHeiko Schocher #undef CONFIG_SYS_I2C_DIRECT_BUS
39479b2d0bbSStefan Roese #endif
39579b2d0bbSStefan Roese 
3968c12045aSStefan Roese /* define the I2C bus number for RTC and DTT if not already done */
3976d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_RTC_BUS_NUM)
3986d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_RTC_BUS_NUM		0
3998c12045aSStefan Roese #endif
4006d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_DTT_BUS_NUM)
4016d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_DTT_BUS_NUM		0
4028c12045aSStefan Roese #endif
4036d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_SPD_BUS_NUM)
4046d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_SPD_BUS_NUM		0
405d8a8ea5cSMatthias Fuchs #endif
4068c12045aSStefan Roese 
407385c9ef5SHeiko Schocher struct i2c_adapter {
408385c9ef5SHeiko Schocher 	void		(*init)(struct i2c_adapter *adap, int speed,
409385c9ef5SHeiko Schocher 				int slaveaddr);
410385c9ef5SHeiko Schocher 	int		(*probe)(struct i2c_adapter *adap, uint8_t chip);
411385c9ef5SHeiko Schocher 	int		(*read)(struct i2c_adapter *adap, uint8_t chip,
412385c9ef5SHeiko Schocher 				uint addr, int alen, uint8_t *buffer,
413385c9ef5SHeiko Schocher 				int len);
414385c9ef5SHeiko Schocher 	int		(*write)(struct i2c_adapter *adap, uint8_t chip,
415385c9ef5SHeiko Schocher 				uint addr, int alen, uint8_t *buffer,
416385c9ef5SHeiko Schocher 				int len);
417385c9ef5SHeiko Schocher 	uint		(*set_bus_speed)(struct i2c_adapter *adap,
418385c9ef5SHeiko Schocher 				uint speed);
419385c9ef5SHeiko Schocher 	int		speed;
420d5243359SHannes Petermaier 	int		waitdelay;
421385c9ef5SHeiko Schocher 	int		slaveaddr;
422385c9ef5SHeiko Schocher 	int		init_done;
423385c9ef5SHeiko Schocher 	int		hwadapnr;
424385c9ef5SHeiko Schocher 	char		*name;
425385c9ef5SHeiko Schocher };
426385c9ef5SHeiko Schocher 
427385c9ef5SHeiko Schocher #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
428385c9ef5SHeiko Schocher 		_set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
429385c9ef5SHeiko Schocher 	{ \
430385c9ef5SHeiko Schocher 		.init		=	_init, \
431385c9ef5SHeiko Schocher 		.probe		=	_probe, \
432385c9ef5SHeiko Schocher 		.read		=	_read, \
433385c9ef5SHeiko Schocher 		.write		=	_write, \
434385c9ef5SHeiko Schocher 		.set_bus_speed	=	_set_speed, \
435385c9ef5SHeiko Schocher 		.speed		=	_speed, \
436385c9ef5SHeiko Schocher 		.slaveaddr	=	_slaveaddr, \
437385c9ef5SHeiko Schocher 		.init_done	=	0, \
438385c9ef5SHeiko Schocher 		.hwadapnr	=	_hwadapnr, \
439385c9ef5SHeiko Schocher 		.name		=	#_name \
440385c9ef5SHeiko Schocher };
441385c9ef5SHeiko Schocher 
442385c9ef5SHeiko Schocher #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
443385c9ef5SHeiko Schocher 			_set_speed, _speed, _slaveaddr, _hwadapnr) \
444385c9ef5SHeiko Schocher 	ll_entry_declare(struct i2c_adapter, _name, i2c) = \
445385c9ef5SHeiko Schocher 	U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
446385c9ef5SHeiko Schocher 		 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
447385c9ef5SHeiko Schocher 
448385c9ef5SHeiko Schocher struct i2c_adapter *i2c_get_adapter(int index);
449385c9ef5SHeiko Schocher 
450385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS
451385c9ef5SHeiko Schocher struct i2c_mux {
452385c9ef5SHeiko Schocher 	int	id;
453385c9ef5SHeiko Schocher 	char	name[16];
454385c9ef5SHeiko Schocher };
455385c9ef5SHeiko Schocher 
456385c9ef5SHeiko Schocher struct i2c_next_hop {
457385c9ef5SHeiko Schocher 	struct i2c_mux		mux;
458385c9ef5SHeiko Schocher 	uint8_t		chip;
459385c9ef5SHeiko Schocher 	uint8_t		channel;
460385c9ef5SHeiko Schocher };
461385c9ef5SHeiko Schocher 
462385c9ef5SHeiko Schocher struct i2c_bus_hose {
463385c9ef5SHeiko Schocher 	int	adapter;
464385c9ef5SHeiko Schocher 	struct i2c_next_hop	next_hop[CONFIG_SYS_I2C_MAX_HOPS];
465385c9ef5SHeiko Schocher };
466385c9ef5SHeiko Schocher #define I2C_NULL_HOP	{{-1, ""}, 0, 0}
467385c9ef5SHeiko Schocher extern struct i2c_bus_hose	i2c_bus[];
468385c9ef5SHeiko Schocher 
469385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus)	i2c_bus[bus].adapter
470385c9ef5SHeiko Schocher #else
471385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus)	bus
472385c9ef5SHeiko Schocher #endif
473385c9ef5SHeiko Schocher #define	I2C_BUS			gd->cur_i2c_bus
474385c9ef5SHeiko Schocher 
475385c9ef5SHeiko Schocher #define	I2C_ADAP_NR(bus)	i2c_get_adapter(I2C_ADAPTER(bus))
476385c9ef5SHeiko Schocher #define	I2C_ADAP		I2C_ADAP_NR(gd->cur_i2c_bus)
477385c9ef5SHeiko Schocher #define I2C_ADAP_HWNR		(I2C_ADAP->hwadapnr)
478385c9ef5SHeiko Schocher 
479385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS
480385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540_ID	1
481385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540		{I2C_MUX_PCA9540_ID, "PCA9540B"}
482385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542_ID	2
483385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542		{I2C_MUX_PCA9542_ID, "PCA9542A"}
484385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544_ID	3
485385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544		{I2C_MUX_PCA9544_ID, "PCA9544A"}
486385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547_ID	4
487385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547		{I2C_MUX_PCA9547_ID, "PCA9547A"}
488e6658749SMichael Burr #define I2C_MUX_PCA9548_ID	5
489e6658749SMichael Burr #define I2C_MUX_PCA9548		{I2C_MUX_PCA9548_ID, "PCA9548"}
490385c9ef5SHeiko Schocher #endif
491385c9ef5SHeiko Schocher 
49298aed379SHeiko Schocher #ifndef I2C_SOFT_DECLARATIONS
49398aed379SHeiko Schocher # if defined(CONFIG_MPC8260)
4946d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #  define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
49598aed379SHeiko Schocher # elif defined(CONFIG_8xx)
4966d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #  define I2C_SOFT_DECLARATIONS	volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
4970cf0b931SJens Scharsig 
4980cf0b931SJens Scharsig # elif (defined(CONFIG_AT91RM9200) || \
4990cf0b931SJens Scharsig 	defined(CONFIG_AT91SAM9260) ||  defined(CONFIG_AT91SAM9261) || \
500cb96a0a4SAndreas Bießmann 	defined(CONFIG_AT91SAM9263))
50178132275Sesw@bus-elektronik.de #  define I2C_SOFT_DECLARATIONS	at91_pio_t *pio	= (at91_pio_t *) ATMEL_BASE_PIOA;
50298aed379SHeiko Schocher # else
50398aed379SHeiko Schocher #  define I2C_SOFT_DECLARATIONS
50498aed379SHeiko Schocher # endif
50598aed379SHeiko Schocher #endif
506ecf5f077STimur Tabi 
507ecf5f077STimur Tabi #ifdef CONFIG_8xx
5089c90a2c8SPeter Tyser /* Set default value for the I2C bus speed on 8xx. In the
509ecf5f077STimur Tabi  * future, we'll define these in all 8xx board config files.
510ecf5f077STimur Tabi  */
511ecf5f077STimur Tabi #ifndef	CONFIG_SYS_I2C_SPEED
512ecf5f077STimur Tabi #define	CONFIG_SYS_I2C_SPEED	50000
513ecf5f077STimur Tabi #endif
514ecf5f077STimur Tabi #endif
5159c90a2c8SPeter Tyser 
5169c90a2c8SPeter Tyser /*
5179c90a2c8SPeter Tyser  * Many boards/controllers/drivers don't support an I2C slave interface so
5189c90a2c8SPeter Tyser  * provide a default slave address for them for use in common code.  A real
5199c90a2c8SPeter Tyser  * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
5209c90a2c8SPeter Tyser  * support a slave interface.
5219c90a2c8SPeter Tyser  */
5229c90a2c8SPeter Tyser #ifndef	CONFIG_SYS_I2C_SLAVE
5239c90a2c8SPeter Tyser #define	CONFIG_SYS_I2C_SLAVE	0xfe
524ecf5f077STimur Tabi #endif
525ecf5f077STimur Tabi 
5261f045217Swdenk /*
5271f045217Swdenk  * Initialization, must be called once on start up, may be called
5281f045217Swdenk  * repeatedly to change the speed and slave addresses.
5291f045217Swdenk  */
5301f045217Swdenk void i2c_init(int speed, int slaveaddr);
53106d01dbeSwdenk void i2c_init_board(void);
53226a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
53326a33504SRichard Retanubun void i2c_board_late_init(void);
53426a33504SRichard Retanubun #endif
5351f045217Swdenk 
536385c9ef5SHeiko Schocher #ifdef CONFIG_SYS_I2C
537385c9ef5SHeiko Schocher /*
538385c9ef5SHeiko Schocher  * i2c_get_bus_num:
539385c9ef5SHeiko Schocher  *
540385c9ef5SHeiko Schocher  *  Returns index of currently active I2C bus.  Zero-based.
541385c9ef5SHeiko Schocher  */
542385c9ef5SHeiko Schocher unsigned int i2c_get_bus_num(void);
54367b23a32SHeiko Schocher 
544385c9ef5SHeiko Schocher /*
545385c9ef5SHeiko Schocher  * i2c_set_bus_num:
546385c9ef5SHeiko Schocher  *
547385c9ef5SHeiko Schocher  *  Change the active I2C bus.  Subsequent read/write calls will
548385c9ef5SHeiko Schocher  *  go to this one.
549385c9ef5SHeiko Schocher  *
550385c9ef5SHeiko Schocher  *	bus - bus index, zero based
551385c9ef5SHeiko Schocher  *
552385c9ef5SHeiko Schocher  *	Returns: 0 on success, not 0 on failure
553385c9ef5SHeiko Schocher  *
554385c9ef5SHeiko Schocher  */
555385c9ef5SHeiko Schocher int i2c_set_bus_num(unsigned int bus);
55667b23a32SHeiko Schocher 
557385c9ef5SHeiko Schocher /*
558385c9ef5SHeiko Schocher  * i2c_init_all():
559385c9ef5SHeiko Schocher  *
560385c9ef5SHeiko Schocher  * Initializes all I2C adapters in the system. All i2c_adap structures must
561385c9ef5SHeiko Schocher  * be initialized beforehead with function pointers and data, including
562385c9ef5SHeiko Schocher  * speed and slaveaddr. Returns 0 on success, non-0 on failure.
563385c9ef5SHeiko Schocher  */
564385c9ef5SHeiko Schocher void i2c_init_all(void);
56567b23a32SHeiko Schocher 
566385c9ef5SHeiko Schocher /*
567385c9ef5SHeiko Schocher  * Probe the given I2C chip address.  Returns 0 if a chip responded,
568385c9ef5SHeiko Schocher  * not 0 on failure.
569385c9ef5SHeiko Schocher  */
570385c9ef5SHeiko Schocher int i2c_probe(uint8_t chip);
571385c9ef5SHeiko Schocher 
572385c9ef5SHeiko Schocher /*
573385c9ef5SHeiko Schocher  * Read/Write interface:
574385c9ef5SHeiko Schocher  *   chip:    I2C chip address, range 0..127
575385c9ef5SHeiko Schocher  *   addr:    Memory (register) address within the chip
576385c9ef5SHeiko Schocher  *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
577385c9ef5SHeiko Schocher  *              memories, 0 for register type devices with only one
578385c9ef5SHeiko Schocher  *              register)
579385c9ef5SHeiko Schocher  *   buffer:  Where to read/write the data
580385c9ef5SHeiko Schocher  *   len:     How many bytes to read/write
581385c9ef5SHeiko Schocher  *
582385c9ef5SHeiko Schocher  *   Returns: 0 on success, not 0 on failure
583385c9ef5SHeiko Schocher  */
584385c9ef5SHeiko Schocher int i2c_read(uint8_t chip, unsigned int addr, int alen,
585385c9ef5SHeiko Schocher 				uint8_t *buffer, int len);
586385c9ef5SHeiko Schocher 
587385c9ef5SHeiko Schocher int i2c_write(uint8_t chip, unsigned int addr, int alen,
588385c9ef5SHeiko Schocher 				uint8_t *buffer, int len);
589385c9ef5SHeiko Schocher 
590385c9ef5SHeiko Schocher /*
591385c9ef5SHeiko Schocher  * Utility routines to read/write registers.
592385c9ef5SHeiko Schocher  */
593385c9ef5SHeiko Schocher uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
594385c9ef5SHeiko Schocher 
595385c9ef5SHeiko Schocher void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
596385c9ef5SHeiko Schocher 
597385c9ef5SHeiko Schocher /*
598385c9ef5SHeiko Schocher  * i2c_set_bus_speed:
599385c9ef5SHeiko Schocher  *
600385c9ef5SHeiko Schocher  *  Change the speed of the active I2C bus
601385c9ef5SHeiko Schocher  *
602385c9ef5SHeiko Schocher  *	speed - bus speed in Hz
603385c9ef5SHeiko Schocher  *
604385c9ef5SHeiko Schocher  *	Returns: new bus speed
605385c9ef5SHeiko Schocher  *
606385c9ef5SHeiko Schocher  */
607385c9ef5SHeiko Schocher unsigned int i2c_set_bus_speed(unsigned int speed);
608385c9ef5SHeiko Schocher 
609385c9ef5SHeiko Schocher /*
610385c9ef5SHeiko Schocher  * i2c_get_bus_speed:
611385c9ef5SHeiko Schocher  *
612385c9ef5SHeiko Schocher  *  Returns speed of currently active I2C bus in Hz
613385c9ef5SHeiko Schocher  */
614385c9ef5SHeiko Schocher 
615385c9ef5SHeiko Schocher unsigned int i2c_get_bus_speed(void);
616385c9ef5SHeiko Schocher 
617385c9ef5SHeiko Schocher /*
618385c9ef5SHeiko Schocher  * i2c_reloc_fixup:
619385c9ef5SHeiko Schocher  *
620385c9ef5SHeiko Schocher  * Adjusts I2C pointers after U-Boot is relocated to DRAM
621385c9ef5SHeiko Schocher  */
622385c9ef5SHeiko Schocher void i2c_reloc_fixup(void);
623ea818dbbSHeiko Schocher #if defined(CONFIG_SYS_I2C_SOFT)
624ea818dbbSHeiko Schocher void i2c_soft_init(void);
625ea818dbbSHeiko Schocher void i2c_soft_active(void);
626ea818dbbSHeiko Schocher void i2c_soft_tristate(void);
627ea818dbbSHeiko Schocher int i2c_soft_read(void);
628ea818dbbSHeiko Schocher void i2c_soft_sda(int bit);
629ea818dbbSHeiko Schocher void i2c_soft_scl(int bit);
630ea818dbbSHeiko Schocher void i2c_soft_delay(void);
63167b23a32SHeiko Schocher #endif
632385c9ef5SHeiko Schocher #else
63367b23a32SHeiko Schocher 
6341f045217Swdenk /*
6351f045217Swdenk  * Probe the given I2C chip address.  Returns 0 if a chip responded,
6361f045217Swdenk  * not 0 on failure.
6371f045217Swdenk  */
6381f045217Swdenk int i2c_probe(uchar chip);
6391f045217Swdenk 
6401f045217Swdenk /*
6411f045217Swdenk  * Read/Write interface:
6421f045217Swdenk  *   chip:    I2C chip address, range 0..127
6431f045217Swdenk  *   addr:    Memory (register) address within the chip
6441f045217Swdenk  *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
6451f045217Swdenk  *              memories, 0 for register type devices with only one
6461f045217Swdenk  *              register)
6471f045217Swdenk  *   buffer:  Where to read/write the data
6481f045217Swdenk  *   len:     How many bytes to read/write
6491f045217Swdenk  *
6501f045217Swdenk  *   Returns: 0 on success, not 0 on failure
6511f045217Swdenk  */
6521f045217Swdenk int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
6531f045217Swdenk int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
6541f045217Swdenk 
6551f045217Swdenk /*
6561f045217Swdenk  * Utility routines to read/write registers.
6571f045217Swdenk  */
658ecf5f077STimur Tabi static inline u8 i2c_reg_read(u8 addr, u8 reg)
659ecf5f077STimur Tabi {
660ecf5f077STimur Tabi 	u8 buf;
661ecf5f077STimur Tabi 
662ecf5f077STimur Tabi #ifdef CONFIG_8xx
663ecf5f077STimur Tabi 	/* MPC8xx needs this.  Maybe one day we can get rid of it. */
664ecf5f077STimur Tabi 	i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
665ecf5f077STimur Tabi #endif
666ecf5f077STimur Tabi 
667ecf5f077STimur Tabi #ifdef DEBUG
668ecf5f077STimur Tabi 	printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
669ecf5f077STimur Tabi #endif
670ecf5f077STimur Tabi 
671ecf5f077STimur Tabi 	i2c_read(addr, reg, 1, &buf, 1);
672ecf5f077STimur Tabi 
673ecf5f077STimur Tabi 	return buf;
674ecf5f077STimur Tabi }
675ecf5f077STimur Tabi 
676ecf5f077STimur Tabi static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
677ecf5f077STimur Tabi {
678ecf5f077STimur Tabi #ifdef CONFIG_8xx
679ecf5f077STimur Tabi 	/* MPC8xx needs this.  Maybe one day we can get rid of it. */
680ecf5f077STimur Tabi 	i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
681ecf5f077STimur Tabi #endif
682ecf5f077STimur Tabi 
683ecf5f077STimur Tabi #ifdef DEBUG
684ecf5f077STimur Tabi 	printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
685ecf5f077STimur Tabi 	       __func__, addr, reg, val);
686ecf5f077STimur Tabi #endif
687ecf5f077STimur Tabi 
688ecf5f077STimur Tabi 	i2c_write(addr, reg, 1, &val, 1);
689ecf5f077STimur Tabi }
6901f045217Swdenk 
691bb99ad6dSBen Warren /*
692bb99ad6dSBen Warren  * Functions for setting the current I2C bus and its speed
693bb99ad6dSBen Warren  */
694bb99ad6dSBen Warren 
695bb99ad6dSBen Warren /*
696bb99ad6dSBen Warren  * i2c_set_bus_num:
697bb99ad6dSBen Warren  *
698bb99ad6dSBen Warren  *  Change the active I2C bus.  Subsequent read/write calls will
699bb99ad6dSBen Warren  *  go to this one.
700bb99ad6dSBen Warren  *
701bb99ad6dSBen Warren  *	bus - bus index, zero based
702bb99ad6dSBen Warren  *
703bb99ad6dSBen Warren  *	Returns: 0 on success, not 0 on failure
704bb99ad6dSBen Warren  *
705bb99ad6dSBen Warren  */
7069ca880a2STimur Tabi int i2c_set_bus_num(unsigned int bus);
707bb99ad6dSBen Warren 
708bb99ad6dSBen Warren /*
709bb99ad6dSBen Warren  * i2c_get_bus_num:
710bb99ad6dSBen Warren  *
711bb99ad6dSBen Warren  *  Returns index of currently active I2C bus.  Zero-based.
712bb99ad6dSBen Warren  */
713bb99ad6dSBen Warren 
7149ca880a2STimur Tabi unsigned int i2c_get_bus_num(void);
715bb99ad6dSBen Warren 
716bb99ad6dSBen Warren /*
717bb99ad6dSBen Warren  * i2c_set_bus_speed:
718bb99ad6dSBen Warren  *
719bb99ad6dSBen Warren  *  Change the speed of the active I2C bus
720bb99ad6dSBen Warren  *
721bb99ad6dSBen Warren  *	speed - bus speed in Hz
722bb99ad6dSBen Warren  *
723bb99ad6dSBen Warren  *	Returns: 0 on success, not 0 on failure
724bb99ad6dSBen Warren  *
725bb99ad6dSBen Warren  */
7269ca880a2STimur Tabi int i2c_set_bus_speed(unsigned int);
727bb99ad6dSBen Warren 
728bb99ad6dSBen Warren /*
729bb99ad6dSBen Warren  * i2c_get_bus_speed:
730bb99ad6dSBen Warren  *
731bb99ad6dSBen Warren  *  Returns speed of currently active I2C bus in Hz
732bb99ad6dSBen Warren  */
733bb99ad6dSBen Warren 
7349ca880a2STimur Tabi unsigned int i2c_get_bus_speed(void);
735385c9ef5SHeiko Schocher #endif /* CONFIG_SYS_I2C */
736385c9ef5SHeiko Schocher 
737385c9ef5SHeiko Schocher /*
738385c9ef5SHeiko Schocher  * only for backwardcompatibility, should go away if we switched
739385c9ef5SHeiko Schocher  * completely to new multibus support.
740385c9ef5SHeiko Schocher  */
741385c9ef5SHeiko Schocher #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
742385c9ef5SHeiko Schocher # if !defined(CONFIG_SYS_MAX_I2C_BUS)
743385c9ef5SHeiko Schocher #  define CONFIG_SYS_MAX_I2C_BUS		2
744385c9ef5SHeiko Schocher # endif
745ea0f73abSŁukasz Majewski # define I2C_MULTI_BUS				1
746385c9ef5SHeiko Schocher #else
747385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS		1
748385c9ef5SHeiko Schocher # define I2C_MULTI_BUS				0
749385c9ef5SHeiko Schocher #endif
750bb99ad6dSBen Warren 
751cd7b4e82SMarek Vasut /* NOTE: These two functions MUST be always_inline to avoid code growth! */
752cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
753cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void)
754cd7b4e82SMarek Vasut {
755cd7b4e82SMarek Vasut 	return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
756cd7b4e82SMarek Vasut }
757cd7b4e82SMarek Vasut 
758cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
759cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus)
760cd7b4e82SMarek Vasut {
761cd7b4e82SMarek Vasut 	if (I2C_MULTI_BUS)
762cd7b4e82SMarek Vasut 		i2c_set_bus_num(bus);
763cd7b4e82SMarek Vasut }
764cd7b4e82SMarek Vasut 
7657ca8f73aSŁukasz Majewski /* Multi I2C definitions */
7667ca8f73aSŁukasz Majewski enum {
7677ca8f73aSŁukasz Majewski 	I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
7687ca8f73aSŁukasz Majewski 	I2C_8, I2C_9, I2C_10,
7697ca8f73aSŁukasz Majewski };
7707ca8f73aSŁukasz Majewski 
7717ca8f73aSŁukasz Majewski /* Multi I2C busses handling */
7727ca8f73aSŁukasz Majewski #ifdef CONFIG_SOFT_I2C_MULTI_BUS
7737ca8f73aSŁukasz Majewski extern int get_multi_scl_pin(void);
7747ca8f73aSŁukasz Majewski extern int get_multi_sda_pin(void);
7757ca8f73aSŁukasz Majewski extern int multi_i2c_init(void);
7767ca8f73aSŁukasz Majewski #endif
777a9d2ae70SRajeshwari Shinde 
778a9d2ae70SRajeshwari Shinde /**
779a9d2ae70SRajeshwari Shinde  * Get FDT values for i2c bus.
780a9d2ae70SRajeshwari Shinde  *
781a9d2ae70SRajeshwari Shinde  * @param blob  Device tree blbo
782a9d2ae70SRajeshwari Shinde  * @return the number of I2C bus
783a9d2ae70SRajeshwari Shinde  */
784a9d2ae70SRajeshwari Shinde void board_i2c_init(const void *blob);
785a9d2ae70SRajeshwari Shinde 
786a9d2ae70SRajeshwari Shinde /**
787a9d2ae70SRajeshwari Shinde  * Find the I2C bus number by given a FDT I2C node.
788a9d2ae70SRajeshwari Shinde  *
789a9d2ae70SRajeshwari Shinde  * @param blob  Device tree blbo
790a9d2ae70SRajeshwari Shinde  * @param node  FDT I2C node to find
791a9d2ae70SRajeshwari Shinde  * @return the number of I2C bus (zero based), or -1 on error
792a9d2ae70SRajeshwari Shinde  */
793a9d2ae70SRajeshwari Shinde int i2c_get_bus_num_fdt(int node);
794a9d2ae70SRajeshwari Shinde 
795a9d2ae70SRajeshwari Shinde /**
796a9d2ae70SRajeshwari Shinde  * Reset the I2C bus represented by the given a FDT I2C node.
797a9d2ae70SRajeshwari Shinde  *
798a9d2ae70SRajeshwari Shinde  * @param blob  Device tree blbo
799a9d2ae70SRajeshwari Shinde  * @param node  FDT I2C node to find
800a9d2ae70SRajeshwari Shinde  * @return 0 if port was reset, -1 if not found
801a9d2ae70SRajeshwari Shinde  */
802a9d2ae70SRajeshwari Shinde int i2c_reset_port_fdt(const void *blob, int node);
803*c6202d85SSimon Glass 
804*c6202d85SSimon Glass #endif /* !CONFIG_DM_I2C */
805*c6202d85SSimon Glass 
8061f045217Swdenk #endif	/* _I2C_H_ */
807