xref: /openbmc/u-boot/include/i2c.h (revision ba3864f8037f58dd1f46cad3b76adc0a84bd33b3)
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 /*
21c6202d85SSimon Glass  * For now there are essentially two parts to this file - driver model
22c6202d85SSimon Glass  * here at the top, and the older code below (with CONFIG_SYS_I2C being
23c6202d85SSimon Glass  * most recent). The plan is to migrate everything to driver model.
24c6202d85SSimon Glass  * The driver model structures and API are separate as they are different
25c6202d85SSimon Glass  * enough as to be incompatible for compilation purposes.
26c6202d85SSimon Glass  */
27c6202d85SSimon Glass 
28c6202d85SSimon Glass enum dm_i2c_chip_flags {
29c6202d85SSimon Glass 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit addressing */
30c6202d85SSimon Glass 	DM_I2C_CHIP_RD_ADDRESS	= 1 << 1, /* Send address for each read byte */
31c6202d85SSimon Glass 	DM_I2C_CHIP_WR_ADDRESS	= 1 << 2, /* Send address for each write byte */
32c6202d85SSimon Glass };
33c6202d85SSimon Glass 
34fffff726SSimon Glass struct udevice;
35c6202d85SSimon Glass /**
36c6202d85SSimon Glass  * struct dm_i2c_chip - information about an i2c chip
37c6202d85SSimon Glass  *
38c6202d85SSimon Glass  * An I2C chip is a device on the I2C bus. It sits at a particular address
39c6202d85SSimon Glass  * and normally supports 7-bit or 10-bit addressing.
40c6202d85SSimon Glass  *
41e6f66ec0SSimon Glass  * To obtain this structure, use dev_get_parent_platdata(dev) where dev is
42e6f66ec0SSimon Glass  * the chip to examine.
43c6202d85SSimon Glass  *
44c6202d85SSimon Glass  * @chip_addr:	Chip address on bus
45c6202d85SSimon Glass  * @offset_len: Length of offset in bytes. A single byte offset can
46c6202d85SSimon Glass  *		represent up to 256 bytes. A value larger than 1 may be
47c6202d85SSimon Glass  *		needed for larger devices.
48c6202d85SSimon Glass  * @flags:	Flags for this chip (dm_i2c_chip_flags)
49c6202d85SSimon Glass  * @emul: Emulator for this chip address (only used for emulation)
50c6202d85SSimon Glass  */
51c6202d85SSimon Glass struct dm_i2c_chip {
52c6202d85SSimon Glass 	uint chip_addr;
53c6202d85SSimon Glass 	uint offset_len;
54c6202d85SSimon Glass 	uint flags;
55c6202d85SSimon Glass #ifdef CONFIG_SANDBOX
56c6202d85SSimon Glass 	struct udevice *emul;
57c6202d85SSimon Glass #endif
58c6202d85SSimon Glass };
59c6202d85SSimon Glass 
60c6202d85SSimon Glass /**
61c6202d85SSimon Glass  * struct dm_i2c_bus- information about an i2c bus
62c6202d85SSimon Glass  *
63c6202d85SSimon Glass  * An I2C bus contains 0 or more chips on it, each at its own address. The
64c6202d85SSimon Glass  * bus can operate at different speeds (measured in Hz, typically 100KHz
65c6202d85SSimon Glass  * or 400KHz).
66c6202d85SSimon Glass  *
67e564f054SSimon Glass  * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the
68e564f054SSimon Glass  * I2C bus udevice.
69c6202d85SSimon Glass  *
70c6202d85SSimon Glass  * @speed_hz: Bus speed in hertz (typically 100000)
71c6202d85SSimon Glass  */
72c6202d85SSimon Glass struct dm_i2c_bus {
73c6202d85SSimon Glass 	int speed_hz;
74c6202d85SSimon Glass };
75c6202d85SSimon Glass 
76c6202d85SSimon Glass /**
77f9a4c2daSSimon Glass  * dm_i2c_read() - read bytes from an I2C chip
78c6202d85SSimon Glass  *
79c6202d85SSimon Glass  * To obtain an I2C device (called a 'chip') given the I2C bus address you
80c6202d85SSimon Glass  * can use i2c_get_chip(). To obtain a bus by bus number use
81c6202d85SSimon Glass  * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
82c6202d85SSimon Glass  *
83c6202d85SSimon Glass  * To set the address length of a devce use i2c_set_addr_len(). It
84c6202d85SSimon Glass  * defaults to 1.
85c6202d85SSimon Glass  *
86c6202d85SSimon Glass  * @dev:	Chip to read from
87c6202d85SSimon Glass  * @offset:	Offset within chip to start reading
88c6202d85SSimon Glass  * @buffer:	Place to put data
89c6202d85SSimon Glass  * @len:	Number of bytes to read
90c6202d85SSimon Glass  *
91c6202d85SSimon Glass  * @return 0 on success, -ve on failure
92c6202d85SSimon Glass  */
93f9a4c2daSSimon Glass int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
94c6202d85SSimon Glass 
95c6202d85SSimon Glass /**
96f9a4c2daSSimon Glass  * dm_i2c_write() - write bytes to an I2C chip
97c6202d85SSimon Glass  *
98f9a4c2daSSimon Glass  * See notes for dm_i2c_read() above.
99c6202d85SSimon Glass  *
100c6202d85SSimon Glass  * @dev:	Chip to write to
101c6202d85SSimon Glass  * @offset:	Offset within chip to start writing
102c6202d85SSimon Glass  * @buffer:	Buffer containing data to write
103c6202d85SSimon Glass  * @len:	Number of bytes to write
104c6202d85SSimon Glass  *
105c6202d85SSimon Glass  * @return 0 on success, -ve on failure
106c6202d85SSimon Glass  */
107f9a4c2daSSimon Glass int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
108c6202d85SSimon Glass 		 int len);
109c6202d85SSimon Glass 
110c6202d85SSimon Glass /**
111f9a4c2daSSimon Glass  * dm_i2c_probe() - probe a particular chip address
112c6202d85SSimon Glass  *
113c6202d85SSimon Glass  * This can be useful to check for the existence of a chip on the bus.
114c6202d85SSimon Glass  * It is typically implemented by writing the chip address to the bus
115c6202d85SSimon Glass  * and checking that the chip replies with an ACK.
116c6202d85SSimon Glass  *
117c6202d85SSimon Glass  * @bus:	Bus to probe
118c6202d85SSimon Glass  * @chip_addr:	7-bit address to probe (10-bit and others are not supported)
119c6202d85SSimon Glass  * @chip_flags:	Flags for the probe (see enum dm_i2c_chip_flags)
120c6202d85SSimon Glass  * @devp:	Returns the device found, or NULL if none
121c6202d85SSimon Glass  * @return 0 if a chip was found at that address, -ve if not
122c6202d85SSimon Glass  */
123f9a4c2daSSimon Glass int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
124c6202d85SSimon Glass 		 struct udevice **devp);
125c6202d85SSimon Glass 
126c6202d85SSimon Glass /**
127*ba3864f8SSimon Glass  * dm_i2c_reg_read() - Read a value from an I2C register
128*ba3864f8SSimon Glass  *
129*ba3864f8SSimon Glass  * This reads a single value from the given address in an I2C chip
130*ba3864f8SSimon Glass  *
131*ba3864f8SSimon Glass  * @addr:	Address to read from
132*ba3864f8SSimon Glass  * @return value read, or -ve on error
133*ba3864f8SSimon Glass  */
134*ba3864f8SSimon Glass int dm_i2c_reg_read(struct udevice *dev, uint offset);
135*ba3864f8SSimon Glass 
136*ba3864f8SSimon Glass /**
137*ba3864f8SSimon Glass  * dm_i2c_reg_write() - Write a value to an I2C register
138*ba3864f8SSimon Glass  *
139*ba3864f8SSimon Glass  * This writes a single value to the given address in an I2C chip
140*ba3864f8SSimon Glass  *
141*ba3864f8SSimon Glass  * @addr:	Address to write to
142*ba3864f8SSimon Glass  * @val:	Value to write (normally a byte)
143*ba3864f8SSimon Glass  * @return 0 on success, -ve on error
144*ba3864f8SSimon Glass  */
145*ba3864f8SSimon Glass int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val);
146*ba3864f8SSimon Glass 
147*ba3864f8SSimon Glass /**
148ca88b9b9SSimon Glass  * dm_i2c_set_bus_speed() - set the speed of a bus
149c6202d85SSimon Glass  *
150c6202d85SSimon Glass  * @bus:	Bus to adjust
151c6202d85SSimon Glass  * @speed:	Requested speed in Hz
152c6202d85SSimon Glass  * @return 0 if OK, -EINVAL for invalid values
153c6202d85SSimon Glass  */
154ca88b9b9SSimon Glass int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
155c6202d85SSimon Glass 
156c6202d85SSimon Glass /**
157ca88b9b9SSimon Glass  * dm_i2c_get_bus_speed() - get the speed of a bus
158c6202d85SSimon Glass  *
159c6202d85SSimon Glass  * @bus:	Bus to check
160c6202d85SSimon Glass  * @return speed of selected I2C bus in Hz, -ve on error
161c6202d85SSimon Glass  */
162ca88b9b9SSimon Glass int dm_i2c_get_bus_speed(struct udevice *bus);
163c6202d85SSimon Glass 
164c6202d85SSimon Glass /**
165c6202d85SSimon Glass  * i2c_set_chip_flags() - set flags for a chip
166c6202d85SSimon Glass  *
167c6202d85SSimon Glass  * Typically addresses are 7 bits, but for 10-bit addresses you should set
168c6202d85SSimon Glass  * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
169c6202d85SSimon Glass  *
170c6202d85SSimon Glass  * @dev:	Chip to adjust
171c6202d85SSimon Glass  * @flags:	New flags
172c6202d85SSimon Glass  * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error
173c6202d85SSimon Glass  */
174c6202d85SSimon Glass int i2c_set_chip_flags(struct udevice *dev, uint flags);
175c6202d85SSimon Glass 
176c6202d85SSimon Glass /**
177c6202d85SSimon Glass  * i2c_get_chip_flags() - get flags for a chip
178c6202d85SSimon Glass  *
179c6202d85SSimon Glass  * @dev:	Chip to check
180c6202d85SSimon Glass  * @flagsp:	Place to put flags
181c6202d85SSimon Glass  * @return 0 if OK, other -ve value on error
182c6202d85SSimon Glass  */
183c6202d85SSimon Glass int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
184c6202d85SSimon Glass 
185c6202d85SSimon Glass /**
186c6202d85SSimon Glass  * i2c_set_offset_len() - set the offset length for a chip
187c6202d85SSimon Glass  *
188c6202d85SSimon Glass  * The offset used to access a chip may be up to 4 bytes long. Typically it
189c6202d85SSimon Glass  * is only 1 byte, which is enough for chips with 256 bytes of memory or
190c6202d85SSimon Glass  * registers. The default value is 1, but you can call this function to
191c6202d85SSimon Glass  * change it.
192c6202d85SSimon Glass  *
193c6202d85SSimon Glass  * @offset_len:	New offset length value (typically 1 or 2)
194c6202d85SSimon Glass  */
195c6202d85SSimon Glass 
196c6202d85SSimon Glass int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
197c6202d85SSimon Glass /**
198c6202d85SSimon Glass  * i2c_deblock() - recover a bus that is in an unknown state
199c6202d85SSimon Glass  *
200c6202d85SSimon Glass  * See the deblock() method in 'struct dm_i2c_ops' for full information
201c6202d85SSimon Glass  *
202c6202d85SSimon Glass  * @bus:	Bus to recover
203c6202d85SSimon Glass  * @return 0 if OK, -ve on error
204c6202d85SSimon Glass  */
205c6202d85SSimon Glass int i2c_deblock(struct udevice *bus);
206c6202d85SSimon Glass 
20773845350SSimon Glass #ifdef CONFIG_DM_I2C_COMPAT
20873845350SSimon Glass /**
20973845350SSimon Glass  * i2c_probe() - Compatibility function for driver model
21073845350SSimon Glass  *
21173845350SSimon Glass  * Calls dm_i2c_probe() on the current bus
21273845350SSimon Glass  */
21373845350SSimon Glass int i2c_probe(uint8_t chip_addr);
21473845350SSimon Glass 
21573845350SSimon Glass /**
21673845350SSimon Glass  * i2c_read() - Compatibility function for driver model
21773845350SSimon Glass  *
21873845350SSimon Glass  * Calls dm_i2c_read() with the device corresponding to @chip_addr, and offset
21973845350SSimon Glass  * set to @addr. @alen must match the current setting for the device.
22073845350SSimon Glass  */
22173845350SSimon Glass int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
22273845350SSimon Glass 	     int len);
22373845350SSimon Glass 
22473845350SSimon Glass /**
22573845350SSimon Glass  * i2c_write() - Compatibility function for driver model
22673845350SSimon Glass  *
22773845350SSimon Glass  * Calls dm_i2c_write() with the device corresponding to @chip_addr, and offset
22873845350SSimon Glass  * set to @addr. @alen must match the current setting for the device.
22973845350SSimon Glass  */
23073845350SSimon Glass int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
23173845350SSimon Glass 	      int len);
23273845350SSimon Glass 
23373845350SSimon Glass /**
23473845350SSimon Glass  * i2c_get_bus_num_fdt() - Compatibility function for driver model
23573845350SSimon Glass  *
23673845350SSimon Glass  * @return the bus number associated with the given device tree node
23773845350SSimon Glass  */
23873845350SSimon Glass int i2c_get_bus_num_fdt(int node);
23973845350SSimon Glass 
24073845350SSimon Glass /**
24173845350SSimon Glass  * i2c_get_bus_num() - Compatibility function for driver model
24273845350SSimon Glass  *
24373845350SSimon Glass  * @return the 'current' bus number
24473845350SSimon Glass  */
24573845350SSimon Glass unsigned int i2c_get_bus_num(void);
24673845350SSimon Glass 
24773845350SSimon Glass /**
248d744d561SSimon Glass  * i2c_set_bus_num() - Compatibility function for driver model
24973845350SSimon Glass  *
25073845350SSimon Glass  * Sets the 'current' bus
25173845350SSimon Glass  */
25273845350SSimon Glass int i2c_set_bus_num(unsigned int bus);
25373845350SSimon Glass 
25473845350SSimon Glass static inline void I2C_SET_BUS(unsigned int bus)
25573845350SSimon Glass {
25673845350SSimon Glass 	i2c_set_bus_num(bus);
25773845350SSimon Glass }
25873845350SSimon Glass 
25973845350SSimon Glass static inline unsigned int I2C_GET_BUS(void)
26073845350SSimon Glass {
26173845350SSimon Glass 	return i2c_get_bus_num();
26273845350SSimon Glass }
26373845350SSimon Glass 
264d744d561SSimon Glass /**
265d744d561SSimon Glass  * i2c_init() - Compatibility function for driver model
266d744d561SSimon Glass  *
267d744d561SSimon Glass  * This function does nothing.
268d744d561SSimon Glass  */
269d744d561SSimon Glass void i2c_init(int speed, int slaveaddr);
270d744d561SSimon Glass 
271d744d561SSimon Glass /**
272d744d561SSimon Glass  * board_i2c_init() - Compatibility function for driver model
273d744d561SSimon Glass  *
274d744d561SSimon Glass  * @param blob  Device tree blbo
275d744d561SSimon Glass  * @return the number of I2C bus
276d744d561SSimon Glass  */
277d744d561SSimon Glass void board_i2c_init(const void *blob);
278d744d561SSimon Glass 
27973845350SSimon Glass #endif
28073845350SSimon Glass 
281c6202d85SSimon Glass /*
282c6202d85SSimon Glass  * Not all of these flags are implemented in the U-Boot API
283c6202d85SSimon Glass  */
284c6202d85SSimon Glass enum dm_i2c_msg_flags {
285c6202d85SSimon Glass 	I2C_M_TEN		= 0x0010, /* ten-bit chip address */
286c6202d85SSimon Glass 	I2C_M_RD		= 0x0001, /* read data, from slave to master */
287c6202d85SSimon Glass 	I2C_M_STOP		= 0x8000, /* send stop after this message */
288c6202d85SSimon Glass 	I2C_M_NOSTART		= 0x4000, /* no start before this message */
289c6202d85SSimon Glass 	I2C_M_REV_DIR_ADDR	= 0x2000, /* invert polarity of R/W bit */
290c6202d85SSimon Glass 	I2C_M_IGNORE_NAK	= 0x1000, /* continue after NAK */
291c6202d85SSimon Glass 	I2C_M_NO_RD_ACK		= 0x0800, /* skip the Ack bit on reads */
292c6202d85SSimon Glass 	I2C_M_RECV_LEN		= 0x0400, /* length is first received byte */
293c6202d85SSimon Glass };
294c6202d85SSimon Glass 
295c6202d85SSimon Glass /**
296c6202d85SSimon Glass  * struct i2c_msg - an I2C message
297c6202d85SSimon Glass  *
298c6202d85SSimon Glass  * @addr:	Slave address
299c6202d85SSimon Glass  * @flags:	Flags (see enum dm_i2c_msg_flags)
300c6202d85SSimon Glass  * @len:	Length of buffer in bytes, may be 0 for a probe
301c6202d85SSimon Glass  * @buf:	Buffer to send/receive, or NULL if no data
302c6202d85SSimon Glass  */
303c6202d85SSimon Glass struct i2c_msg {
304c6202d85SSimon Glass 	uint addr;
305c6202d85SSimon Glass 	uint flags;
306c6202d85SSimon Glass 	uint len;
307c6202d85SSimon Glass 	u8 *buf;
308c6202d85SSimon Glass };
309c6202d85SSimon Glass 
310c6202d85SSimon Glass /**
311c6202d85SSimon Glass  * struct i2c_msg_list - a list of I2C messages
312c6202d85SSimon Glass  *
313c6202d85SSimon Glass  * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
314c6202d85SSimon Glass  * appropriate in U-Boot.
315c6202d85SSimon Glass  *
316c6202d85SSimon Glass  * @msg:	Pointer to i2c_msg array
317c6202d85SSimon Glass  * @nmsgs:	Number of elements in the array
318c6202d85SSimon Glass  */
319c6202d85SSimon Glass struct i2c_msg_list {
320c6202d85SSimon Glass 	struct i2c_msg *msgs;
321c6202d85SSimon Glass 	uint nmsgs;
322c6202d85SSimon Glass };
323c6202d85SSimon Glass 
324c6202d85SSimon Glass /**
325c6202d85SSimon Glass  * struct dm_i2c_ops - driver operations for I2C uclass
326c6202d85SSimon Glass  *
327c6202d85SSimon Glass  * Drivers should support these operations unless otherwise noted. These
328c6202d85SSimon Glass  * operations are intended to be used by uclass code, not directly from
329c6202d85SSimon Glass  * other code.
330c6202d85SSimon Glass  */
331c6202d85SSimon Glass struct dm_i2c_ops {
332c6202d85SSimon Glass 	/**
333c6202d85SSimon Glass 	 * xfer() - transfer a list of I2C messages
334c6202d85SSimon Glass 	 *
335c6202d85SSimon Glass 	 * @bus:	Bus to read from
336c6202d85SSimon Glass 	 * @msg:	List of messages to transfer
337c6202d85SSimon Glass 	 * @nmsgs:	Number of messages in the list
338c6202d85SSimon Glass 	 * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
339c6202d85SSimon Glass 	 *	-ECOMM if the speed cannot be supported, -EPROTO if the chip
340c6202d85SSimon Glass 	 *	flags cannot be supported, other -ve value on some other error
341c6202d85SSimon Glass 	 */
342c6202d85SSimon Glass 	int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
343c6202d85SSimon Glass 
344c6202d85SSimon Glass 	/**
345c6202d85SSimon Glass 	 * probe_chip() - probe for the presense of a chip address
346c6202d85SSimon Glass 	 *
347c6202d85SSimon Glass 	 * This function is optional. If omitted, the uclass will send a zero
348c6202d85SSimon Glass 	 * length message instead.
349c6202d85SSimon Glass 	 *
350c6202d85SSimon Glass 	 * @bus:	Bus to probe
351c6202d85SSimon Glass 	 * @chip_addr:	Chip address to probe
352c6202d85SSimon Glass 	 * @chip_flags:	Probe flags (enum dm_i2c_chip_flags)
353c6202d85SSimon Glass 	 * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
354c6202d85SSimon Glass 	 * to default probem other -ve value on error
355c6202d85SSimon Glass 	 */
356c6202d85SSimon Glass 	int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
357c6202d85SSimon Glass 
358c6202d85SSimon Glass 	/**
359c6202d85SSimon Glass 	 * set_bus_speed() - set the speed of a bus (optional)
360c6202d85SSimon Glass 	 *
361c6202d85SSimon Glass 	 * The bus speed value will be updated by the uclass if this function
362c6202d85SSimon Glass 	 * does not return an error. This method is optional - if it is not
363c6202d85SSimon Glass 	 * provided then the driver can read the speed from
364e564f054SSimon Glass 	 * dev_get_uclass_priv(bus)->speed_hz
365c6202d85SSimon Glass 	 *
366c6202d85SSimon Glass 	 * @bus:	Bus to adjust
367c6202d85SSimon Glass 	 * @speed:	Requested speed in Hz
368c6202d85SSimon Glass 	 * @return 0 if OK, -EINVAL for invalid values
369c6202d85SSimon Glass 	 */
370c6202d85SSimon Glass 	int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
371c6202d85SSimon Glass 
372c6202d85SSimon Glass 	/**
373c6202d85SSimon Glass 	 * get_bus_speed() - get the speed of a bus (optional)
374c6202d85SSimon Glass 	 *
375c6202d85SSimon Glass 	 * Normally this can be provided by the uclass, but if you want your
376c6202d85SSimon Glass 	 * driver to check the bus speed by looking at the hardware, you can
377c6202d85SSimon Glass 	 * implement that here. This method is optional. This method would
378e564f054SSimon Glass 	 * normally be expected to return dev_get_uclass_priv(bus)->speed_hz.
379c6202d85SSimon Glass 	 *
380c6202d85SSimon Glass 	 * @bus:	Bus to check
381c6202d85SSimon Glass 	 * @return speed of selected I2C bus in Hz, -ve on error
382c6202d85SSimon Glass 	 */
383c6202d85SSimon Glass 	int (*get_bus_speed)(struct udevice *bus);
384c6202d85SSimon Glass 
385c6202d85SSimon Glass 	/**
386c6202d85SSimon Glass 	 * set_flags() - set the flags for a chip (optional)
387c6202d85SSimon Glass 	 *
388c6202d85SSimon Glass 	 * This is generally implemented by the uclass, but drivers can
389c6202d85SSimon Glass 	 * check the value to ensure that unsupported options are not used.
390c6202d85SSimon Glass 	 * This method is optional. If provided, this method will always be
391c6202d85SSimon Glass 	 * called when the flags change.
392c6202d85SSimon Glass 	 *
393c6202d85SSimon Glass 	 * @dev:	Chip to adjust
394c6202d85SSimon Glass 	 * @flags:	New flags value
395c6202d85SSimon Glass 	 * @return 0 if OK, -EINVAL if value is unsupported
396c6202d85SSimon Glass 	 */
397c6202d85SSimon Glass 	int (*set_flags)(struct udevice *dev, uint flags);
398c6202d85SSimon Glass 
399c6202d85SSimon Glass 	/**
400c6202d85SSimon Glass 	 * deblock() - recover a bus that is in an unknown state
401c6202d85SSimon Glass 	 *
402c6202d85SSimon Glass 	 * I2C is a synchronous protocol and resets of the processor in the
403c6202d85SSimon Glass 	 * middle of an access can block the I2C Bus until a powerdown of
404c6202d85SSimon Glass 	 * the full unit is done. This is because slaves can be stuck
405c6202d85SSimon Glass 	 * waiting for addition bus transitions for a transaction that will
406c6202d85SSimon Glass 	 * never complete. Resetting the I2C master does not help. The only
407c6202d85SSimon Glass 	 * way is to force the bus through a series of transitions to make
408c6202d85SSimon Glass 	 * sure that all slaves are done with the transaction. This method
409c6202d85SSimon Glass 	 * performs this 'deblocking' if support by the driver.
410c6202d85SSimon Glass 	 *
411c6202d85SSimon Glass 	 * This method is optional.
412c6202d85SSimon Glass 	 */
413c6202d85SSimon Glass 	int (*deblock)(struct udevice *bus);
414c6202d85SSimon Glass };
415c6202d85SSimon Glass 
416c6202d85SSimon Glass #define i2c_get_ops(dev)	((struct dm_i2c_ops *)(dev)->driver->ops)
417c6202d85SSimon Glass 
418c6202d85SSimon Glass /**
419c6202d85SSimon Glass  * i2c_get_chip() - get a device to use to access a chip on a bus
420c6202d85SSimon Glass  *
421c6202d85SSimon Glass  * This returns the device for the given chip address. The device can then
422c6202d85SSimon Glass  * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
423c6202d85SSimon Glass  *
424c6202d85SSimon Glass  * @bus:	Bus to examine
425c6202d85SSimon Glass  * @chip_addr:	Chip address for the new device
42625ab4b03SSimon Glass  * @offset_len:	Length of a register offset in bytes (normally 1)
427c6202d85SSimon Glass  * @devp:	Returns pointer to new device if found or -ENODEV if not
428c6202d85SSimon Glass  *		found
429c6202d85SSimon Glass  */
43025ab4b03SSimon Glass int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
43125ab4b03SSimon Glass 		 struct udevice **devp);
432c6202d85SSimon Glass 
433c6202d85SSimon Glass /**
434c6202d85SSimon Glass  * i2c_get_chip() - get a device to use to access a chip on a bus number
435c6202d85SSimon Glass  *
436c6202d85SSimon Glass  * This returns the device for the given chip address on a particular bus
437c6202d85SSimon Glass  * number.
438c6202d85SSimon Glass  *
439c6202d85SSimon Glass  * @busnum:	Bus number to examine
440c6202d85SSimon Glass  * @chip_addr:	Chip address for the new device
44125ab4b03SSimon Glass  * @offset_len:	Length of a register offset in bytes (normally 1)
442c6202d85SSimon Glass  * @devp:	Returns pointer to new device if found or -ENODEV if not
443c6202d85SSimon Glass  *		found
444c6202d85SSimon Glass  */
44525ab4b03SSimon Glass int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
44625ab4b03SSimon Glass 			    struct udevice **devp);
447c6202d85SSimon Glass 
448c6202d85SSimon Glass /**
449c6202d85SSimon Glass  * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data
450c6202d85SSimon Glass  *
451c6202d85SSimon Glass  * This decodes the chip address from a device tree node and puts it into
452c6202d85SSimon Glass  * its dm_i2c_chip structure. This should be called in your driver's
453c6202d85SSimon Glass  * ofdata_to_platdata() method.
454c6202d85SSimon Glass  *
455c6202d85SSimon Glass  * @blob:	Device tree blob
456c6202d85SSimon Glass  * @node:	Node offset to read from
457c6202d85SSimon Glass  * @spi:	Place to put the decoded information
458c6202d85SSimon Glass  */
459c6202d85SSimon Glass int i2c_chip_ofdata_to_platdata(const void *blob, int node,
460c6202d85SSimon Glass 				struct dm_i2c_chip *chip);
461c6202d85SSimon Glass 
462c6202d85SSimon Glass #ifndef CONFIG_DM_I2C
463c6202d85SSimon Glass 
464c6202d85SSimon Glass /*
4651f045217Swdenk  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
4661f045217Swdenk  *
4671f045217Swdenk  * The implementation MUST NOT use static or global variables if the
4681f045217Swdenk  * I2C routines are used to read SDRAM configuration information
4691f045217Swdenk  * because this is done before the memories are initialized. Limited
4701f045217Swdenk  * use of stack-based variables are OK (the initial stack size is
4711f045217Swdenk  * limited).
4721f045217Swdenk  *
4731f045217Swdenk  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
4741f045217Swdenk  */
4751f045217Swdenk 
4761f045217Swdenk /*
4771f045217Swdenk  * Configuration items.
4781f045217Swdenk  */
4791f045217Swdenk #define I2C_RXTX_LEN	128	/* maximum tx/rx buffer length */
4801f045217Swdenk 
481385c9ef5SHeiko Schocher #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
482385c9ef5SHeiko Schocher /* no muxes used bus = i2c adapters */
483385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_DIRECT_BUS	1
484385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_MAX_HOPS		0
485385c9ef5SHeiko Schocher #define CONFIG_SYS_NUM_I2C_BUSES	ll_entry_count(struct i2c_adapter, i2c)
48679b2d0bbSStefan Roese #else
487385c9ef5SHeiko Schocher /* we use i2c muxes */
488385c9ef5SHeiko Schocher #undef CONFIG_SYS_I2C_DIRECT_BUS
48979b2d0bbSStefan Roese #endif
49079b2d0bbSStefan Roese 
4918c12045aSStefan Roese /* define the I2C bus number for RTC and DTT if not already done */
4926d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_RTC_BUS_NUM)
4936d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_RTC_BUS_NUM		0
4948c12045aSStefan Roese #endif
4956d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_DTT_BUS_NUM)
4966d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_DTT_BUS_NUM		0
4978c12045aSStefan Roese #endif
4986d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_SPD_BUS_NUM)
4996d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_SPD_BUS_NUM		0
500d8a8ea5cSMatthias Fuchs #endif
5018c12045aSStefan Roese 
502385c9ef5SHeiko Schocher struct i2c_adapter {
503385c9ef5SHeiko Schocher 	void		(*init)(struct i2c_adapter *adap, int speed,
504385c9ef5SHeiko Schocher 				int slaveaddr);
505385c9ef5SHeiko Schocher 	int		(*probe)(struct i2c_adapter *adap, uint8_t chip);
506385c9ef5SHeiko Schocher 	int		(*read)(struct i2c_adapter *adap, uint8_t chip,
507385c9ef5SHeiko Schocher 				uint addr, int alen, uint8_t *buffer,
508385c9ef5SHeiko Schocher 				int len);
509385c9ef5SHeiko Schocher 	int		(*write)(struct i2c_adapter *adap, uint8_t chip,
510385c9ef5SHeiko Schocher 				uint addr, int alen, uint8_t *buffer,
511385c9ef5SHeiko Schocher 				int len);
512385c9ef5SHeiko Schocher 	uint		(*set_bus_speed)(struct i2c_adapter *adap,
513385c9ef5SHeiko Schocher 				uint speed);
514385c9ef5SHeiko Schocher 	int		speed;
515d5243359SHannes Petermaier 	int		waitdelay;
516385c9ef5SHeiko Schocher 	int		slaveaddr;
517385c9ef5SHeiko Schocher 	int		init_done;
518385c9ef5SHeiko Schocher 	int		hwadapnr;
519385c9ef5SHeiko Schocher 	char		*name;
520385c9ef5SHeiko Schocher };
521385c9ef5SHeiko Schocher 
522385c9ef5SHeiko Schocher #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
523385c9ef5SHeiko Schocher 		_set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
524385c9ef5SHeiko Schocher 	{ \
525385c9ef5SHeiko Schocher 		.init		=	_init, \
526385c9ef5SHeiko Schocher 		.probe		=	_probe, \
527385c9ef5SHeiko Schocher 		.read		=	_read, \
528385c9ef5SHeiko Schocher 		.write		=	_write, \
529385c9ef5SHeiko Schocher 		.set_bus_speed	=	_set_speed, \
530385c9ef5SHeiko Schocher 		.speed		=	_speed, \
531385c9ef5SHeiko Schocher 		.slaveaddr	=	_slaveaddr, \
532385c9ef5SHeiko Schocher 		.init_done	=	0, \
533385c9ef5SHeiko Schocher 		.hwadapnr	=	_hwadapnr, \
534385c9ef5SHeiko Schocher 		.name		=	#_name \
535385c9ef5SHeiko Schocher };
536385c9ef5SHeiko Schocher 
537385c9ef5SHeiko Schocher #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
538385c9ef5SHeiko Schocher 			_set_speed, _speed, _slaveaddr, _hwadapnr) \
539385c9ef5SHeiko Schocher 	ll_entry_declare(struct i2c_adapter, _name, i2c) = \
540385c9ef5SHeiko Schocher 	U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
541385c9ef5SHeiko Schocher 		 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
542385c9ef5SHeiko Schocher 
543385c9ef5SHeiko Schocher struct i2c_adapter *i2c_get_adapter(int index);
544385c9ef5SHeiko Schocher 
545385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS
546385c9ef5SHeiko Schocher struct i2c_mux {
547385c9ef5SHeiko Schocher 	int	id;
548385c9ef5SHeiko Schocher 	char	name[16];
549385c9ef5SHeiko Schocher };
550385c9ef5SHeiko Schocher 
551385c9ef5SHeiko Schocher struct i2c_next_hop {
552385c9ef5SHeiko Schocher 	struct i2c_mux		mux;
553385c9ef5SHeiko Schocher 	uint8_t		chip;
554385c9ef5SHeiko Schocher 	uint8_t		channel;
555385c9ef5SHeiko Schocher };
556385c9ef5SHeiko Schocher 
557385c9ef5SHeiko Schocher struct i2c_bus_hose {
558385c9ef5SHeiko Schocher 	int	adapter;
559385c9ef5SHeiko Schocher 	struct i2c_next_hop	next_hop[CONFIG_SYS_I2C_MAX_HOPS];
560385c9ef5SHeiko Schocher };
561385c9ef5SHeiko Schocher #define I2C_NULL_HOP	{{-1, ""}, 0, 0}
562385c9ef5SHeiko Schocher extern struct i2c_bus_hose	i2c_bus[];
563385c9ef5SHeiko Schocher 
564385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus)	i2c_bus[bus].adapter
565385c9ef5SHeiko Schocher #else
566385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus)	bus
567385c9ef5SHeiko Schocher #endif
568385c9ef5SHeiko Schocher #define	I2C_BUS			gd->cur_i2c_bus
569385c9ef5SHeiko Schocher 
570385c9ef5SHeiko Schocher #define	I2C_ADAP_NR(bus)	i2c_get_adapter(I2C_ADAPTER(bus))
571385c9ef5SHeiko Schocher #define	I2C_ADAP		I2C_ADAP_NR(gd->cur_i2c_bus)
572385c9ef5SHeiko Schocher #define I2C_ADAP_HWNR		(I2C_ADAP->hwadapnr)
573385c9ef5SHeiko Schocher 
574385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS
575385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540_ID	1
576385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540		{I2C_MUX_PCA9540_ID, "PCA9540B"}
577385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542_ID	2
578385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542		{I2C_MUX_PCA9542_ID, "PCA9542A"}
579385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544_ID	3
580385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544		{I2C_MUX_PCA9544_ID, "PCA9544A"}
581385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547_ID	4
582385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547		{I2C_MUX_PCA9547_ID, "PCA9547A"}
583e6658749SMichael Burr #define I2C_MUX_PCA9548_ID	5
584e6658749SMichael Burr #define I2C_MUX_PCA9548		{I2C_MUX_PCA9548_ID, "PCA9548"}
585385c9ef5SHeiko Schocher #endif
586385c9ef5SHeiko Schocher 
58798aed379SHeiko Schocher #ifndef I2C_SOFT_DECLARATIONS
58898aed379SHeiko Schocher # if defined(CONFIG_MPC8260)
5896d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #  define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
59098aed379SHeiko Schocher # elif defined(CONFIG_8xx)
5916d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #  define I2C_SOFT_DECLARATIONS	volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
5920cf0b931SJens Scharsig 
5930cf0b931SJens Scharsig # elif (defined(CONFIG_AT91RM9200) || \
5940cf0b931SJens Scharsig 	defined(CONFIG_AT91SAM9260) ||  defined(CONFIG_AT91SAM9261) || \
595cb96a0a4SAndreas Bießmann 	defined(CONFIG_AT91SAM9263))
59678132275Sesw@bus-elektronik.de #  define I2C_SOFT_DECLARATIONS	at91_pio_t *pio	= (at91_pio_t *) ATMEL_BASE_PIOA;
59798aed379SHeiko Schocher # else
59898aed379SHeiko Schocher #  define I2C_SOFT_DECLARATIONS
59998aed379SHeiko Schocher # endif
60098aed379SHeiko Schocher #endif
601ecf5f077STimur Tabi 
602ecf5f077STimur Tabi #ifdef CONFIG_8xx
6039c90a2c8SPeter Tyser /* Set default value for the I2C bus speed on 8xx. In the
604ecf5f077STimur Tabi  * future, we'll define these in all 8xx board config files.
605ecf5f077STimur Tabi  */
606ecf5f077STimur Tabi #ifndef	CONFIG_SYS_I2C_SPEED
607ecf5f077STimur Tabi #define	CONFIG_SYS_I2C_SPEED	50000
608ecf5f077STimur Tabi #endif
609ecf5f077STimur Tabi #endif
6109c90a2c8SPeter Tyser 
6119c90a2c8SPeter Tyser /*
6129c90a2c8SPeter Tyser  * Many boards/controllers/drivers don't support an I2C slave interface so
6139c90a2c8SPeter Tyser  * provide a default slave address for them for use in common code.  A real
6149c90a2c8SPeter Tyser  * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
6159c90a2c8SPeter Tyser  * support a slave interface.
6169c90a2c8SPeter Tyser  */
6179c90a2c8SPeter Tyser #ifndef	CONFIG_SYS_I2C_SLAVE
6189c90a2c8SPeter Tyser #define	CONFIG_SYS_I2C_SLAVE	0xfe
619ecf5f077STimur Tabi #endif
620ecf5f077STimur Tabi 
6211f045217Swdenk /*
6221f045217Swdenk  * Initialization, must be called once on start up, may be called
6231f045217Swdenk  * repeatedly to change the speed and slave addresses.
6241f045217Swdenk  */
6251f045217Swdenk void i2c_init(int speed, int slaveaddr);
62606d01dbeSwdenk void i2c_init_board(void);
62726a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
62826a33504SRichard Retanubun void i2c_board_late_init(void);
62926a33504SRichard Retanubun #endif
6301f045217Swdenk 
631385c9ef5SHeiko Schocher #ifdef CONFIG_SYS_I2C
632385c9ef5SHeiko Schocher /*
633385c9ef5SHeiko Schocher  * i2c_get_bus_num:
634385c9ef5SHeiko Schocher  *
635385c9ef5SHeiko Schocher  *  Returns index of currently active I2C bus.  Zero-based.
636385c9ef5SHeiko Schocher  */
637385c9ef5SHeiko Schocher unsigned int i2c_get_bus_num(void);
63867b23a32SHeiko Schocher 
639385c9ef5SHeiko Schocher /*
640385c9ef5SHeiko Schocher  * i2c_set_bus_num:
641385c9ef5SHeiko Schocher  *
642385c9ef5SHeiko Schocher  *  Change the active I2C bus.  Subsequent read/write calls will
643385c9ef5SHeiko Schocher  *  go to this one.
644385c9ef5SHeiko Schocher  *
645385c9ef5SHeiko Schocher  *	bus - bus index, zero based
646385c9ef5SHeiko Schocher  *
647385c9ef5SHeiko Schocher  *	Returns: 0 on success, not 0 on failure
648385c9ef5SHeiko Schocher  *
649385c9ef5SHeiko Schocher  */
650385c9ef5SHeiko Schocher int i2c_set_bus_num(unsigned int bus);
65167b23a32SHeiko Schocher 
652385c9ef5SHeiko Schocher /*
653385c9ef5SHeiko Schocher  * i2c_init_all():
654385c9ef5SHeiko Schocher  *
655385c9ef5SHeiko Schocher  * Initializes all I2C adapters in the system. All i2c_adap structures must
656385c9ef5SHeiko Schocher  * be initialized beforehead with function pointers and data, including
657385c9ef5SHeiko Schocher  * speed and slaveaddr. Returns 0 on success, non-0 on failure.
658385c9ef5SHeiko Schocher  */
659385c9ef5SHeiko Schocher void i2c_init_all(void);
66067b23a32SHeiko Schocher 
661385c9ef5SHeiko Schocher /*
662385c9ef5SHeiko Schocher  * Probe the given I2C chip address.  Returns 0 if a chip responded,
663385c9ef5SHeiko Schocher  * not 0 on failure.
664385c9ef5SHeiko Schocher  */
665385c9ef5SHeiko Schocher int i2c_probe(uint8_t chip);
666385c9ef5SHeiko Schocher 
667385c9ef5SHeiko Schocher /*
668385c9ef5SHeiko Schocher  * Read/Write interface:
669385c9ef5SHeiko Schocher  *   chip:    I2C chip address, range 0..127
670385c9ef5SHeiko Schocher  *   addr:    Memory (register) address within the chip
671385c9ef5SHeiko Schocher  *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
672385c9ef5SHeiko Schocher  *              memories, 0 for register type devices with only one
673385c9ef5SHeiko Schocher  *              register)
674385c9ef5SHeiko Schocher  *   buffer:  Where to read/write the data
675385c9ef5SHeiko Schocher  *   len:     How many bytes to read/write
676385c9ef5SHeiko Schocher  *
677385c9ef5SHeiko Schocher  *   Returns: 0 on success, not 0 on failure
678385c9ef5SHeiko Schocher  */
679385c9ef5SHeiko Schocher int i2c_read(uint8_t chip, unsigned int addr, int alen,
680385c9ef5SHeiko Schocher 				uint8_t *buffer, int len);
681385c9ef5SHeiko Schocher 
682385c9ef5SHeiko Schocher int i2c_write(uint8_t chip, unsigned int addr, int alen,
683385c9ef5SHeiko Schocher 				uint8_t *buffer, int len);
684385c9ef5SHeiko Schocher 
685385c9ef5SHeiko Schocher /*
686385c9ef5SHeiko Schocher  * Utility routines to read/write registers.
687385c9ef5SHeiko Schocher  */
688385c9ef5SHeiko Schocher uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
689385c9ef5SHeiko Schocher 
690385c9ef5SHeiko Schocher void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
691385c9ef5SHeiko Schocher 
692385c9ef5SHeiko Schocher /*
693385c9ef5SHeiko Schocher  * i2c_set_bus_speed:
694385c9ef5SHeiko Schocher  *
695385c9ef5SHeiko Schocher  *  Change the speed of the active I2C bus
696385c9ef5SHeiko Schocher  *
697385c9ef5SHeiko Schocher  *	speed - bus speed in Hz
698385c9ef5SHeiko Schocher  *
699385c9ef5SHeiko Schocher  *	Returns: new bus speed
700385c9ef5SHeiko Schocher  *
701385c9ef5SHeiko Schocher  */
702385c9ef5SHeiko Schocher unsigned int i2c_set_bus_speed(unsigned int speed);
703385c9ef5SHeiko Schocher 
704385c9ef5SHeiko Schocher /*
705385c9ef5SHeiko Schocher  * i2c_get_bus_speed:
706385c9ef5SHeiko Schocher  *
707385c9ef5SHeiko Schocher  *  Returns speed of currently active I2C bus in Hz
708385c9ef5SHeiko Schocher  */
709385c9ef5SHeiko Schocher 
710385c9ef5SHeiko Schocher unsigned int i2c_get_bus_speed(void);
711385c9ef5SHeiko Schocher 
712385c9ef5SHeiko Schocher /*
713385c9ef5SHeiko Schocher  * i2c_reloc_fixup:
714385c9ef5SHeiko Schocher  *
715385c9ef5SHeiko Schocher  * Adjusts I2C pointers after U-Boot is relocated to DRAM
716385c9ef5SHeiko Schocher  */
717385c9ef5SHeiko Schocher void i2c_reloc_fixup(void);
718ea818dbbSHeiko Schocher #if defined(CONFIG_SYS_I2C_SOFT)
719ea818dbbSHeiko Schocher void i2c_soft_init(void);
720ea818dbbSHeiko Schocher void i2c_soft_active(void);
721ea818dbbSHeiko Schocher void i2c_soft_tristate(void);
722ea818dbbSHeiko Schocher int i2c_soft_read(void);
723ea818dbbSHeiko Schocher void i2c_soft_sda(int bit);
724ea818dbbSHeiko Schocher void i2c_soft_scl(int bit);
725ea818dbbSHeiko Schocher void i2c_soft_delay(void);
72667b23a32SHeiko Schocher #endif
727385c9ef5SHeiko Schocher #else
72867b23a32SHeiko Schocher 
7291f045217Swdenk /*
7301f045217Swdenk  * Probe the given I2C chip address.  Returns 0 if a chip responded,
7311f045217Swdenk  * not 0 on failure.
7321f045217Swdenk  */
7331f045217Swdenk int i2c_probe(uchar chip);
7341f045217Swdenk 
7351f045217Swdenk /*
7361f045217Swdenk  * Read/Write interface:
7371f045217Swdenk  *   chip:    I2C chip address, range 0..127
7381f045217Swdenk  *   addr:    Memory (register) address within the chip
7391f045217Swdenk  *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
7401f045217Swdenk  *              memories, 0 for register type devices with only one
7411f045217Swdenk  *              register)
7421f045217Swdenk  *   buffer:  Where to read/write the data
7431f045217Swdenk  *   len:     How many bytes to read/write
7441f045217Swdenk  *
7451f045217Swdenk  *   Returns: 0 on success, not 0 on failure
7461f045217Swdenk  */
7471f045217Swdenk int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
7481f045217Swdenk int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
7491f045217Swdenk 
7501f045217Swdenk /*
7511f045217Swdenk  * Utility routines to read/write registers.
7521f045217Swdenk  */
753ecf5f077STimur Tabi static inline u8 i2c_reg_read(u8 addr, u8 reg)
754ecf5f077STimur Tabi {
755ecf5f077STimur Tabi 	u8 buf;
756ecf5f077STimur Tabi 
757ecf5f077STimur Tabi #ifdef CONFIG_8xx
758ecf5f077STimur Tabi 	/* MPC8xx needs this.  Maybe one day we can get rid of it. */
759ecf5f077STimur Tabi 	i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
760ecf5f077STimur Tabi #endif
761ecf5f077STimur Tabi 
762ecf5f077STimur Tabi #ifdef DEBUG
763ecf5f077STimur Tabi 	printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
764ecf5f077STimur Tabi #endif
765ecf5f077STimur Tabi 
766ecf5f077STimur Tabi 	i2c_read(addr, reg, 1, &buf, 1);
767ecf5f077STimur Tabi 
768ecf5f077STimur Tabi 	return buf;
769ecf5f077STimur Tabi }
770ecf5f077STimur Tabi 
771ecf5f077STimur Tabi static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
772ecf5f077STimur Tabi {
773ecf5f077STimur Tabi #ifdef CONFIG_8xx
774ecf5f077STimur Tabi 	/* MPC8xx needs this.  Maybe one day we can get rid of it. */
775ecf5f077STimur Tabi 	i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
776ecf5f077STimur Tabi #endif
777ecf5f077STimur Tabi 
778ecf5f077STimur Tabi #ifdef DEBUG
779ecf5f077STimur Tabi 	printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
780ecf5f077STimur Tabi 	       __func__, addr, reg, val);
781ecf5f077STimur Tabi #endif
782ecf5f077STimur Tabi 
783ecf5f077STimur Tabi 	i2c_write(addr, reg, 1, &val, 1);
784ecf5f077STimur Tabi }
7851f045217Swdenk 
786bb99ad6dSBen Warren /*
787bb99ad6dSBen Warren  * Functions for setting the current I2C bus and its speed
788bb99ad6dSBen Warren  */
789bb99ad6dSBen Warren 
790bb99ad6dSBen Warren /*
791bb99ad6dSBen Warren  * i2c_set_bus_num:
792bb99ad6dSBen Warren  *
793bb99ad6dSBen Warren  *  Change the active I2C bus.  Subsequent read/write calls will
794bb99ad6dSBen Warren  *  go to this one.
795bb99ad6dSBen Warren  *
796bb99ad6dSBen Warren  *	bus - bus index, zero based
797bb99ad6dSBen Warren  *
798bb99ad6dSBen Warren  *	Returns: 0 on success, not 0 on failure
799bb99ad6dSBen Warren  *
800bb99ad6dSBen Warren  */
8019ca880a2STimur Tabi int i2c_set_bus_num(unsigned int bus);
802bb99ad6dSBen Warren 
803bb99ad6dSBen Warren /*
804bb99ad6dSBen Warren  * i2c_get_bus_num:
805bb99ad6dSBen Warren  *
806bb99ad6dSBen Warren  *  Returns index of currently active I2C bus.  Zero-based.
807bb99ad6dSBen Warren  */
808bb99ad6dSBen Warren 
8099ca880a2STimur Tabi unsigned int i2c_get_bus_num(void);
810bb99ad6dSBen Warren 
811bb99ad6dSBen Warren /*
812bb99ad6dSBen Warren  * i2c_set_bus_speed:
813bb99ad6dSBen Warren  *
814bb99ad6dSBen Warren  *  Change the speed of the active I2C bus
815bb99ad6dSBen Warren  *
816bb99ad6dSBen Warren  *	speed - bus speed in Hz
817bb99ad6dSBen Warren  *
818bb99ad6dSBen Warren  *	Returns: 0 on success, not 0 on failure
819bb99ad6dSBen Warren  *
820bb99ad6dSBen Warren  */
8219ca880a2STimur Tabi int i2c_set_bus_speed(unsigned int);
822bb99ad6dSBen Warren 
823bb99ad6dSBen Warren /*
824bb99ad6dSBen Warren  * i2c_get_bus_speed:
825bb99ad6dSBen Warren  *
826bb99ad6dSBen Warren  *  Returns speed of currently active I2C bus in Hz
827bb99ad6dSBen Warren  */
828bb99ad6dSBen Warren 
8299ca880a2STimur Tabi unsigned int i2c_get_bus_speed(void);
830385c9ef5SHeiko Schocher #endif /* CONFIG_SYS_I2C */
831385c9ef5SHeiko Schocher 
832385c9ef5SHeiko Schocher /*
833385c9ef5SHeiko Schocher  * only for backwardcompatibility, should go away if we switched
834385c9ef5SHeiko Schocher  * completely to new multibus support.
835385c9ef5SHeiko Schocher  */
836385c9ef5SHeiko Schocher #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
837385c9ef5SHeiko Schocher # if !defined(CONFIG_SYS_MAX_I2C_BUS)
838385c9ef5SHeiko Schocher #  define CONFIG_SYS_MAX_I2C_BUS		2
839385c9ef5SHeiko Schocher # endif
840ea0f73abSŁukasz Majewski # define I2C_MULTI_BUS				1
841385c9ef5SHeiko Schocher #else
842385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS		1
843385c9ef5SHeiko Schocher # define I2C_MULTI_BUS				0
844385c9ef5SHeiko Schocher #endif
845bb99ad6dSBen Warren 
846cd7b4e82SMarek Vasut /* NOTE: These two functions MUST be always_inline to avoid code growth! */
847cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
848cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void)
849cd7b4e82SMarek Vasut {
850cd7b4e82SMarek Vasut 	return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
851cd7b4e82SMarek Vasut }
852cd7b4e82SMarek Vasut 
853cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
854cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus)
855cd7b4e82SMarek Vasut {
856cd7b4e82SMarek Vasut 	if (I2C_MULTI_BUS)
857cd7b4e82SMarek Vasut 		i2c_set_bus_num(bus);
858cd7b4e82SMarek Vasut }
859cd7b4e82SMarek Vasut 
8607ca8f73aSŁukasz Majewski /* Multi I2C definitions */
8617ca8f73aSŁukasz Majewski enum {
8627ca8f73aSŁukasz Majewski 	I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
8637ca8f73aSŁukasz Majewski 	I2C_8, I2C_9, I2C_10,
8647ca8f73aSŁukasz Majewski };
8657ca8f73aSŁukasz Majewski 
8667ca8f73aSŁukasz Majewski /* Multi I2C busses handling */
8677ca8f73aSŁukasz Majewski #ifdef CONFIG_SOFT_I2C_MULTI_BUS
8687ca8f73aSŁukasz Majewski extern int get_multi_scl_pin(void);
8697ca8f73aSŁukasz Majewski extern int get_multi_sda_pin(void);
8707ca8f73aSŁukasz Majewski extern int multi_i2c_init(void);
8717ca8f73aSŁukasz Majewski #endif
872a9d2ae70SRajeshwari Shinde 
873a9d2ae70SRajeshwari Shinde /**
874a9d2ae70SRajeshwari Shinde  * Get FDT values for i2c bus.
875a9d2ae70SRajeshwari Shinde  *
876a9d2ae70SRajeshwari Shinde  * @param blob  Device tree blbo
877a9d2ae70SRajeshwari Shinde  * @return the number of I2C bus
878a9d2ae70SRajeshwari Shinde  */
879a9d2ae70SRajeshwari Shinde void board_i2c_init(const void *blob);
880a9d2ae70SRajeshwari Shinde 
881a9d2ae70SRajeshwari Shinde /**
882a9d2ae70SRajeshwari Shinde  * Find the I2C bus number by given a FDT I2C node.
883a9d2ae70SRajeshwari Shinde  *
884a9d2ae70SRajeshwari Shinde  * @param blob  Device tree blbo
885a9d2ae70SRajeshwari Shinde  * @param node  FDT I2C node to find
886a9d2ae70SRajeshwari Shinde  * @return the number of I2C bus (zero based), or -1 on error
887a9d2ae70SRajeshwari Shinde  */
888a9d2ae70SRajeshwari Shinde int i2c_get_bus_num_fdt(int node);
889a9d2ae70SRajeshwari Shinde 
890a9d2ae70SRajeshwari Shinde /**
891a9d2ae70SRajeshwari Shinde  * Reset the I2C bus represented by the given a FDT I2C node.
892a9d2ae70SRajeshwari Shinde  *
893a9d2ae70SRajeshwari Shinde  * @param blob  Device tree blbo
894a9d2ae70SRajeshwari Shinde  * @param node  FDT I2C node to find
895a9d2ae70SRajeshwari Shinde  * @return 0 if port was reset, -1 if not found
896a9d2ae70SRajeshwari Shinde  */
897a9d2ae70SRajeshwari Shinde int i2c_reset_port_fdt(const void *blob, int node);
898c6202d85SSimon Glass 
899c6202d85SSimon Glass #endif /* !CONFIG_DM_I2C */
900c6202d85SSimon Glass 
9011f045217Swdenk #endif	/* _I2C_H_ */
902