xref: /OK3568_Linux_fs/u-boot/include/i2c.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
3*4882a593Smuzhiyun  * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de>
4*4882a593Smuzhiyun  * Changes for multibus/multiadapter I2C support.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * (C) Copyright 2001
7*4882a593Smuzhiyun  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * The original I2C interface was
12*4882a593Smuzhiyun  *   (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
13*4882a593Smuzhiyun  *   AIRVENT SAM s.p.a - RIMINI(ITALY)
14*4882a593Smuzhiyun  * but has been changed substantially.
15*4882a593Smuzhiyun  */
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #ifndef _I2C_H_
18*4882a593Smuzhiyun #define _I2C_H_
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun /*
21*4882a593Smuzhiyun  * For now there are essentially two parts to this file - driver model
22*4882a593Smuzhiyun  * here at the top, and the older code below (with CONFIG_SYS_I2C being
23*4882a593Smuzhiyun  * most recent). The plan is to migrate everything to driver model.
24*4882a593Smuzhiyun  * The driver model structures and API are separate as they are different
25*4882a593Smuzhiyun  * enough as to be incompatible for compilation purposes.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun enum dm_i2c_chip_flags {
29*4882a593Smuzhiyun 	DM_I2C_CHIP_10BIT	= 1 << 0, /* Use 10-bit addressing */
30*4882a593Smuzhiyun 	DM_I2C_CHIP_RD_ADDRESS	= 1 << 1, /* Send address for each read byte */
31*4882a593Smuzhiyun 	DM_I2C_CHIP_WR_ADDRESS	= 1 << 2, /* Send address for each write byte */
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun struct udevice;
35*4882a593Smuzhiyun /**
36*4882a593Smuzhiyun  * struct dm_i2c_chip - information about an i2c chip
37*4882a593Smuzhiyun  *
38*4882a593Smuzhiyun  * An I2C chip is a device on the I2C bus. It sits at a particular address
39*4882a593Smuzhiyun  * and normally supports 7-bit or 10-bit addressing.
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * To obtain this structure, use dev_get_parent_platdata(dev) where dev is
42*4882a593Smuzhiyun  * the chip to examine.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * @chip_addr:	Chip address on bus
45*4882a593Smuzhiyun  * @offset_len: Length of offset in bytes. A single byte offset can
46*4882a593Smuzhiyun  *		represent up to 256 bytes. A value larger than 1 may be
47*4882a593Smuzhiyun  *		needed for larger devices.
48*4882a593Smuzhiyun  * @flags:	Flags for this chip (dm_i2c_chip_flags)
49*4882a593Smuzhiyun  * @emul: Emulator for this chip address (only used for emulation)
50*4882a593Smuzhiyun  */
51*4882a593Smuzhiyun struct dm_i2c_chip {
52*4882a593Smuzhiyun 	uint chip_addr;
53*4882a593Smuzhiyun 	uint offset_len;
54*4882a593Smuzhiyun 	uint flags;
55*4882a593Smuzhiyun #ifdef CONFIG_SANDBOX
56*4882a593Smuzhiyun 	struct udevice *emul;
57*4882a593Smuzhiyun 	bool test_mode;
58*4882a593Smuzhiyun #endif
59*4882a593Smuzhiyun };
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /**
62*4882a593Smuzhiyun  * struct dm_i2c_bus- information about an i2c bus
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * An I2C bus contains 0 or more chips on it, each at its own address. The
65*4882a593Smuzhiyun  * bus can operate at different speeds (measured in Hz, typically 100KHz
66*4882a593Smuzhiyun  * or 400KHz).
67*4882a593Smuzhiyun  *
68*4882a593Smuzhiyun  * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the
69*4882a593Smuzhiyun  * I2C bus udevice.
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * @speed_hz: Bus speed in hertz (typically 100000)
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun struct dm_i2c_bus {
74*4882a593Smuzhiyun 	int speed_hz;
75*4882a593Smuzhiyun };
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /*
78*4882a593Smuzhiyun  * Not all of these flags are implemented in the U-Boot API
79*4882a593Smuzhiyun  */
80*4882a593Smuzhiyun enum dm_i2c_msg_flags {
81*4882a593Smuzhiyun 	I2C_M_TEN		= 0x0010, /* ten-bit chip address */
82*4882a593Smuzhiyun 	I2C_M_RD		= 0x0001, /* read data, from slave to master */
83*4882a593Smuzhiyun 	I2C_M_STOP		= 0x8000, /* send stop after this message */
84*4882a593Smuzhiyun 	I2C_M_NOSTART		= 0x4000, /* no start before this message */
85*4882a593Smuzhiyun 	I2C_M_REV_DIR_ADDR	= 0x2000, /* invert polarity of R/W bit */
86*4882a593Smuzhiyun 	I2C_M_IGNORE_NAK	= 0x1000, /* continue after NAK */
87*4882a593Smuzhiyun 	I2C_M_NO_RD_ACK		= 0x0800, /* skip the Ack bit on reads */
88*4882a593Smuzhiyun 	I2C_M_RECV_LEN		= 0x0400, /* length is first received byte */
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /**
92*4882a593Smuzhiyun  * struct i2c_msg - an I2C message
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * @addr:	Slave address
95*4882a593Smuzhiyun  * @flags:	Flags (see enum dm_i2c_msg_flags)
96*4882a593Smuzhiyun  * @len:	Length of buffer in bytes, may be 0 for a probe
97*4882a593Smuzhiyun  * @buf:	Buffer to send/receive, or NULL if no data
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun struct i2c_msg {
100*4882a593Smuzhiyun 	uint addr;
101*4882a593Smuzhiyun 	uint flags;
102*4882a593Smuzhiyun 	uint len;
103*4882a593Smuzhiyun 	u8 *buf;
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /**
107*4882a593Smuzhiyun  * struct i2c_msg_list - a list of I2C messages
108*4882a593Smuzhiyun  *
109*4882a593Smuzhiyun  * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
110*4882a593Smuzhiyun  * appropriate in U-Boot.
111*4882a593Smuzhiyun  *
112*4882a593Smuzhiyun  * @msg:	Pointer to i2c_msg array
113*4882a593Smuzhiyun  * @nmsgs:	Number of elements in the array
114*4882a593Smuzhiyun  */
115*4882a593Smuzhiyun struct i2c_msg_list {
116*4882a593Smuzhiyun 	struct i2c_msg *msgs;
117*4882a593Smuzhiyun 	uint nmsgs;
118*4882a593Smuzhiyun };
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /**
121*4882a593Smuzhiyun  * dm_i2c_read() - read bytes from an I2C chip
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  * To obtain an I2C device (called a 'chip') given the I2C bus address you
124*4882a593Smuzhiyun  * can use i2c_get_chip(). To obtain a bus by bus number use
125*4882a593Smuzhiyun  * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
126*4882a593Smuzhiyun  *
127*4882a593Smuzhiyun  * To set the address length of a devce use i2c_set_addr_len(). It
128*4882a593Smuzhiyun  * defaults to 1.
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * @dev:	Chip to read from
131*4882a593Smuzhiyun  * @offset:	Offset within chip to start reading
132*4882a593Smuzhiyun  * @buffer:	Place to put data
133*4882a593Smuzhiyun  * @len:	Number of bytes to read
134*4882a593Smuzhiyun  *
135*4882a593Smuzhiyun  * @return 0 on success, -ve on failure
136*4882a593Smuzhiyun  */
137*4882a593Smuzhiyun int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /**
140*4882a593Smuzhiyun  * dm_i2c_write() - write bytes to an I2C chip
141*4882a593Smuzhiyun  *
142*4882a593Smuzhiyun  * See notes for dm_i2c_read() above.
143*4882a593Smuzhiyun  *
144*4882a593Smuzhiyun  * @dev:	Chip to write to
145*4882a593Smuzhiyun  * @offset:	Offset within chip to start writing
146*4882a593Smuzhiyun  * @buffer:	Buffer containing data to write
147*4882a593Smuzhiyun  * @len:	Number of bytes to write
148*4882a593Smuzhiyun  *
149*4882a593Smuzhiyun  * @return 0 on success, -ve on failure
150*4882a593Smuzhiyun  */
151*4882a593Smuzhiyun int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
152*4882a593Smuzhiyun 		 int len);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /**
155*4882a593Smuzhiyun  * dm_i2c_probe() - probe a particular chip address
156*4882a593Smuzhiyun  *
157*4882a593Smuzhiyun  * This can be useful to check for the existence of a chip on the bus.
158*4882a593Smuzhiyun  * It is typically implemented by writing the chip address to the bus
159*4882a593Smuzhiyun  * and checking that the chip replies with an ACK.
160*4882a593Smuzhiyun  *
161*4882a593Smuzhiyun  * @bus:	Bus to probe
162*4882a593Smuzhiyun  * @chip_addr:	7-bit address to probe (10-bit and others are not supported)
163*4882a593Smuzhiyun  * @chip_flags:	Flags for the probe (see enum dm_i2c_chip_flags)
164*4882a593Smuzhiyun  * @devp:	Returns the device found, or NULL if none
165*4882a593Smuzhiyun  * @return 0 if a chip was found at that address, -ve if not
166*4882a593Smuzhiyun  */
167*4882a593Smuzhiyun int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
168*4882a593Smuzhiyun 		 struct udevice **devp);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun /**
171*4882a593Smuzhiyun  * dm_i2c_reg_read() - Read a value from an I2C register
172*4882a593Smuzhiyun  *
173*4882a593Smuzhiyun  * This reads a single value from the given address in an I2C chip
174*4882a593Smuzhiyun  *
175*4882a593Smuzhiyun  * @dev:	Device to use for transfer
176*4882a593Smuzhiyun  * @addr:	Address to read from
177*4882a593Smuzhiyun  * @return value read, or -ve on error
178*4882a593Smuzhiyun  */
179*4882a593Smuzhiyun int dm_i2c_reg_read(struct udevice *dev, uint offset);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun /**
182*4882a593Smuzhiyun  * dm_i2c_reg_write() - Write a value to an I2C register
183*4882a593Smuzhiyun  *
184*4882a593Smuzhiyun  * This writes a single value to the given address in an I2C chip
185*4882a593Smuzhiyun  *
186*4882a593Smuzhiyun  * @dev:	Device to use for transfer
187*4882a593Smuzhiyun  * @addr:	Address to write to
188*4882a593Smuzhiyun  * @val:	Value to write (normally a byte)
189*4882a593Smuzhiyun  * @return 0 on success, -ve on error
190*4882a593Smuzhiyun  */
191*4882a593Smuzhiyun int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun /**
194*4882a593Smuzhiyun  * dm_i2c_reg_clrset() - Apply bitmask to an I2C register
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  * Read value, apply bitmask and write modified value back to the
197*4882a593Smuzhiyun  * given address in an I2C chip
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  * @dev:	Device to use for transfer
200*4882a593Smuzhiyun  * @offset:	Address for the R/W operation
201*4882a593Smuzhiyun  * @clr:	Bitmask of bits that should be cleared
202*4882a593Smuzhiyun  * @set:	Bitmask of bits that should be set
203*4882a593Smuzhiyun  * @return 0 on success, -ve on error
204*4882a593Smuzhiyun  */
205*4882a593Smuzhiyun int dm_i2c_reg_clrset(struct udevice *dev, uint offset, u32 clr, u32 set);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun /**
208*4882a593Smuzhiyun  * dm_i2c_xfer() - Transfer messages over I2C
209*4882a593Smuzhiyun  *
210*4882a593Smuzhiyun  * This transfers a raw message. It is best to use dm_i2c_reg_read/write()
211*4882a593Smuzhiyun  * instead.
212*4882a593Smuzhiyun  *
213*4882a593Smuzhiyun  * @dev:	Device to use for transfer
214*4882a593Smuzhiyun  * @msg:	List of messages to transfer
215*4882a593Smuzhiyun  * @nmsgs:	Number of messages to transfer
216*4882a593Smuzhiyun  * @return 0 on success, -ve on error
217*4882a593Smuzhiyun  */
218*4882a593Smuzhiyun int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun /**
221*4882a593Smuzhiyun  * dm_i2c_set_bus_speed() - set the speed of a bus
222*4882a593Smuzhiyun  *
223*4882a593Smuzhiyun  * @bus:	Bus to adjust
224*4882a593Smuzhiyun  * @speed:	Requested speed in Hz
225*4882a593Smuzhiyun  * @return 0 if OK, -EINVAL for invalid values
226*4882a593Smuzhiyun  */
227*4882a593Smuzhiyun int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun /**
230*4882a593Smuzhiyun  * dm_i2c_get_bus_speed() - get the speed of a bus
231*4882a593Smuzhiyun  *
232*4882a593Smuzhiyun  * @bus:	Bus to check
233*4882a593Smuzhiyun  * @return speed of selected I2C bus in Hz, -ve on error
234*4882a593Smuzhiyun  */
235*4882a593Smuzhiyun int dm_i2c_get_bus_speed(struct udevice *bus);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun /**
238*4882a593Smuzhiyun  * i2c_set_chip_flags() - set flags for a chip
239*4882a593Smuzhiyun  *
240*4882a593Smuzhiyun  * Typically addresses are 7 bits, but for 10-bit addresses you should set
241*4882a593Smuzhiyun  * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
242*4882a593Smuzhiyun  *
243*4882a593Smuzhiyun  * @dev:	Chip to adjust
244*4882a593Smuzhiyun  * @flags:	New flags
245*4882a593Smuzhiyun  * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error
246*4882a593Smuzhiyun  */
247*4882a593Smuzhiyun int i2c_set_chip_flags(struct udevice *dev, uint flags);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun /**
250*4882a593Smuzhiyun  * i2c_get_chip_flags() - get flags for a chip
251*4882a593Smuzhiyun  *
252*4882a593Smuzhiyun  * @dev:	Chip to check
253*4882a593Smuzhiyun  * @flagsp:	Place to put flags
254*4882a593Smuzhiyun  * @return 0 if OK, other -ve value on error
255*4882a593Smuzhiyun  */
256*4882a593Smuzhiyun int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun /**
259*4882a593Smuzhiyun  * i2c_set_offset_len() - set the offset length for a chip
260*4882a593Smuzhiyun  *
261*4882a593Smuzhiyun  * The offset used to access a chip may be up to 4 bytes long. Typically it
262*4882a593Smuzhiyun  * is only 1 byte, which is enough for chips with 256 bytes of memory or
263*4882a593Smuzhiyun  * registers. The default value is 1, but you can call this function to
264*4882a593Smuzhiyun  * change it.
265*4882a593Smuzhiyun  *
266*4882a593Smuzhiyun  * @offset_len:	New offset length value (typically 1 or 2)
267*4882a593Smuzhiyun  */
268*4882a593Smuzhiyun int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /**
271*4882a593Smuzhiyun  * i2c_get_offset_len() - get the offset length for a chip
272*4882a593Smuzhiyun  *
273*4882a593Smuzhiyun  * @return:	Current offset length value (typically 1 or 2)
274*4882a593Smuzhiyun  */
275*4882a593Smuzhiyun int i2c_get_chip_offset_len(struct udevice *dev);
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun /**
278*4882a593Smuzhiyun  * i2c_deblock() - recover a bus that is in an unknown state
279*4882a593Smuzhiyun  *
280*4882a593Smuzhiyun  * See the deblock() method in 'struct dm_i2c_ops' for full information
281*4882a593Smuzhiyun  *
282*4882a593Smuzhiyun  * @bus:	Bus to recover
283*4882a593Smuzhiyun  * @return 0 if OK, -ve on error
284*4882a593Smuzhiyun  */
285*4882a593Smuzhiyun int i2c_deblock(struct udevice *bus);
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun #ifdef CONFIG_DM_I2C_COMPAT
288*4882a593Smuzhiyun /**
289*4882a593Smuzhiyun  * i2c_probe() - Compatibility function for driver model
290*4882a593Smuzhiyun  *
291*4882a593Smuzhiyun  * Calls dm_i2c_probe() on the current bus
292*4882a593Smuzhiyun  */
293*4882a593Smuzhiyun int i2c_probe(uint8_t chip_addr);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun  * i2c_read() - Compatibility function for driver model
297*4882a593Smuzhiyun  *
298*4882a593Smuzhiyun  * Calls dm_i2c_read() with the device corresponding to @chip_addr, and offset
299*4882a593Smuzhiyun  * set to @addr. @alen must match the current setting for the device.
300*4882a593Smuzhiyun  */
301*4882a593Smuzhiyun int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
302*4882a593Smuzhiyun 	     int len);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun /**
305*4882a593Smuzhiyun  * i2c_write() - Compatibility function for driver model
306*4882a593Smuzhiyun  *
307*4882a593Smuzhiyun  * Calls dm_i2c_write() with the device corresponding to @chip_addr, and offset
308*4882a593Smuzhiyun  * set to @addr. @alen must match the current setting for the device.
309*4882a593Smuzhiyun  */
310*4882a593Smuzhiyun int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
311*4882a593Smuzhiyun 	      int len);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun /**
314*4882a593Smuzhiyun  * i2c_get_bus_num_fdt() - Compatibility function for driver model
315*4882a593Smuzhiyun  *
316*4882a593Smuzhiyun  * @return the bus number associated with the given device tree node
317*4882a593Smuzhiyun  */
318*4882a593Smuzhiyun int i2c_get_bus_num_fdt(int node);
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun /**
321*4882a593Smuzhiyun  * i2c_get_bus_num() - Compatibility function for driver model
322*4882a593Smuzhiyun  *
323*4882a593Smuzhiyun  * @return the 'current' bus number
324*4882a593Smuzhiyun  */
325*4882a593Smuzhiyun unsigned int i2c_get_bus_num(void);
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun /**
328*4882a593Smuzhiyun  * i2c_set_bus_num() - Compatibility function for driver model
329*4882a593Smuzhiyun  *
330*4882a593Smuzhiyun  * Sets the 'current' bus
331*4882a593Smuzhiyun  */
332*4882a593Smuzhiyun int i2c_set_bus_num(unsigned int bus);
333*4882a593Smuzhiyun 
I2C_SET_BUS(unsigned int bus)334*4882a593Smuzhiyun static inline void I2C_SET_BUS(unsigned int bus)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun 	i2c_set_bus_num(bus);
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun 
I2C_GET_BUS(void)339*4882a593Smuzhiyun static inline unsigned int I2C_GET_BUS(void)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun 	return i2c_get_bus_num();
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun /**
345*4882a593Smuzhiyun  * i2c_init() - Compatibility function for driver model
346*4882a593Smuzhiyun  *
347*4882a593Smuzhiyun  * This function does nothing.
348*4882a593Smuzhiyun  */
349*4882a593Smuzhiyun void i2c_init(int speed, int slaveaddr);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun /**
352*4882a593Smuzhiyun  * board_i2c_init() - Compatibility function for driver model
353*4882a593Smuzhiyun  *
354*4882a593Smuzhiyun  * @param blob  Device tree blbo
355*4882a593Smuzhiyun  * @return the number of I2C bus
356*4882a593Smuzhiyun  */
357*4882a593Smuzhiyun void board_i2c_init(const void *blob);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun /*
360*4882a593Smuzhiyun  * Compatibility functions for driver model.
361*4882a593Smuzhiyun  */
362*4882a593Smuzhiyun uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
363*4882a593Smuzhiyun void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun #endif
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun /**
368*4882a593Smuzhiyun  * struct dm_i2c_ops - driver operations for I2C uclass
369*4882a593Smuzhiyun  *
370*4882a593Smuzhiyun  * Drivers should support these operations unless otherwise noted. These
371*4882a593Smuzhiyun  * operations are intended to be used by uclass code, not directly from
372*4882a593Smuzhiyun  * other code.
373*4882a593Smuzhiyun  */
374*4882a593Smuzhiyun struct dm_i2c_ops {
375*4882a593Smuzhiyun 	/**
376*4882a593Smuzhiyun 	 * xfer() - transfer a list of I2C messages
377*4882a593Smuzhiyun 	 *
378*4882a593Smuzhiyun 	 * @bus:	Bus to read from
379*4882a593Smuzhiyun 	 * @msg:	List of messages to transfer
380*4882a593Smuzhiyun 	 * @nmsgs:	Number of messages in the list
381*4882a593Smuzhiyun 	 * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
382*4882a593Smuzhiyun 	 *	-ECOMM if the speed cannot be supported, -EPROTO if the chip
383*4882a593Smuzhiyun 	 *	flags cannot be supported, other -ve value on some other error
384*4882a593Smuzhiyun 	 */
385*4882a593Smuzhiyun 	int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	/**
388*4882a593Smuzhiyun 	 * probe_chip() - probe for the presense of a chip address
389*4882a593Smuzhiyun 	 *
390*4882a593Smuzhiyun 	 * This function is optional. If omitted, the uclass will send a zero
391*4882a593Smuzhiyun 	 * length message instead.
392*4882a593Smuzhiyun 	 *
393*4882a593Smuzhiyun 	 * @bus:	Bus to probe
394*4882a593Smuzhiyun 	 * @chip_addr:	Chip address to probe
395*4882a593Smuzhiyun 	 * @chip_flags:	Probe flags (enum dm_i2c_chip_flags)
396*4882a593Smuzhiyun 	 * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
397*4882a593Smuzhiyun 	 * to default probem other -ve value on error
398*4882a593Smuzhiyun 	 */
399*4882a593Smuzhiyun 	int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	/**
402*4882a593Smuzhiyun 	 * set_bus_speed() - set the speed of a bus (optional)
403*4882a593Smuzhiyun 	 *
404*4882a593Smuzhiyun 	 * The bus speed value will be updated by the uclass if this function
405*4882a593Smuzhiyun 	 * does not return an error. This method is optional - if it is not
406*4882a593Smuzhiyun 	 * provided then the driver can read the speed from
407*4882a593Smuzhiyun 	 * dev_get_uclass_priv(bus)->speed_hz
408*4882a593Smuzhiyun 	 *
409*4882a593Smuzhiyun 	 * @bus:	Bus to adjust
410*4882a593Smuzhiyun 	 * @speed:	Requested speed in Hz
411*4882a593Smuzhiyun 	 * @return 0 if OK, -EINVAL for invalid values
412*4882a593Smuzhiyun 	 */
413*4882a593Smuzhiyun 	int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	/**
416*4882a593Smuzhiyun 	 * get_bus_speed() - get the speed of a bus (optional)
417*4882a593Smuzhiyun 	 *
418*4882a593Smuzhiyun 	 * Normally this can be provided by the uclass, but if you want your
419*4882a593Smuzhiyun 	 * driver to check the bus speed by looking at the hardware, you can
420*4882a593Smuzhiyun 	 * implement that here. This method is optional. This method would
421*4882a593Smuzhiyun 	 * normally be expected to return dev_get_uclass_priv(bus)->speed_hz.
422*4882a593Smuzhiyun 	 *
423*4882a593Smuzhiyun 	 * @bus:	Bus to check
424*4882a593Smuzhiyun 	 * @return speed of selected I2C bus in Hz, -ve on error
425*4882a593Smuzhiyun 	 */
426*4882a593Smuzhiyun 	int (*get_bus_speed)(struct udevice *bus);
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	/**
429*4882a593Smuzhiyun 	 * set_flags() - set the flags for a chip (optional)
430*4882a593Smuzhiyun 	 *
431*4882a593Smuzhiyun 	 * This is generally implemented by the uclass, but drivers can
432*4882a593Smuzhiyun 	 * check the value to ensure that unsupported options are not used.
433*4882a593Smuzhiyun 	 * This method is optional. If provided, this method will always be
434*4882a593Smuzhiyun 	 * called when the flags change.
435*4882a593Smuzhiyun 	 *
436*4882a593Smuzhiyun 	 * @dev:	Chip to adjust
437*4882a593Smuzhiyun 	 * @flags:	New flags value
438*4882a593Smuzhiyun 	 * @return 0 if OK, -EINVAL if value is unsupported
439*4882a593Smuzhiyun 	 */
440*4882a593Smuzhiyun 	int (*set_flags)(struct udevice *dev, uint flags);
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	/**
443*4882a593Smuzhiyun 	 * deblock() - recover a bus that is in an unknown state
444*4882a593Smuzhiyun 	 *
445*4882a593Smuzhiyun 	 * I2C is a synchronous protocol and resets of the processor in the
446*4882a593Smuzhiyun 	 * middle of an access can block the I2C Bus until a powerdown of
447*4882a593Smuzhiyun 	 * the full unit is done. This is because slaves can be stuck
448*4882a593Smuzhiyun 	 * waiting for addition bus transitions for a transaction that will
449*4882a593Smuzhiyun 	 * never complete. Resetting the I2C master does not help. The only
450*4882a593Smuzhiyun 	 * way is to force the bus through a series of transitions to make
451*4882a593Smuzhiyun 	 * sure that all slaves are done with the transaction. This method
452*4882a593Smuzhiyun 	 * performs this 'deblocking' if support by the driver.
453*4882a593Smuzhiyun 	 *
454*4882a593Smuzhiyun 	 * This method is optional.
455*4882a593Smuzhiyun 	 */
456*4882a593Smuzhiyun 	int (*deblock)(struct udevice *bus);
457*4882a593Smuzhiyun };
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun #define i2c_get_ops(dev)	((struct dm_i2c_ops *)(dev)->driver->ops)
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun /**
462*4882a593Smuzhiyun  * struct i2c_mux_ops - operations for an I2C mux
463*4882a593Smuzhiyun  *
464*4882a593Smuzhiyun  * The current mux state is expected to be stored in the mux itself since
465*4882a593Smuzhiyun  * it is the only thing that knows how to make things work. The mux can
466*4882a593Smuzhiyun  * record the current state and then avoid switching unless it is necessary.
467*4882a593Smuzhiyun  * So select() can be skipped if the mux is already in the correct state.
468*4882a593Smuzhiyun  * Also deselect() can be made a nop if required.
469*4882a593Smuzhiyun  */
470*4882a593Smuzhiyun struct i2c_mux_ops {
471*4882a593Smuzhiyun 	/**
472*4882a593Smuzhiyun 	 * select() - select one of of I2C buses attached to a mux
473*4882a593Smuzhiyun 	 *
474*4882a593Smuzhiyun 	 * This will be called when there is no bus currently selected by the
475*4882a593Smuzhiyun 	 * mux. This method does not need to deselect the old bus since
476*4882a593Smuzhiyun 	 * deselect() will be already have been called if necessary.
477*4882a593Smuzhiyun 	 *
478*4882a593Smuzhiyun 	 * @mux:	Mux device
479*4882a593Smuzhiyun 	 * @bus:	I2C bus to select
480*4882a593Smuzhiyun 	 * @channel:	Channel number correponding to the bus to select
481*4882a593Smuzhiyun 	 * @return 0 if OK, -ve on error
482*4882a593Smuzhiyun 	 */
483*4882a593Smuzhiyun 	int (*select)(struct udevice *mux, struct udevice *bus, uint channel);
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	/**
486*4882a593Smuzhiyun 	 * deselect() - select one of of I2C buses attached to a mux
487*4882a593Smuzhiyun 	 *
488*4882a593Smuzhiyun 	 * This is used to deselect the currently selected I2C bus.
489*4882a593Smuzhiyun 	 *
490*4882a593Smuzhiyun 	 * @mux:	Mux device
491*4882a593Smuzhiyun 	 * @bus:	I2C bus to deselect
492*4882a593Smuzhiyun 	 * @channel:	Channel number correponding to the bus to deselect
493*4882a593Smuzhiyun 	 * @return 0 if OK, -ve on error
494*4882a593Smuzhiyun 	 */
495*4882a593Smuzhiyun 	int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel);
496*4882a593Smuzhiyun };
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun #define i2c_mux_get_ops(dev)	((struct i2c_mux_ops *)(dev)->driver->ops)
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun /**
501*4882a593Smuzhiyun  * i2c_get_chip() - get a device to use to access a chip on a bus
502*4882a593Smuzhiyun  *
503*4882a593Smuzhiyun  * This returns the device for the given chip address. The device can then
504*4882a593Smuzhiyun  * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
505*4882a593Smuzhiyun  *
506*4882a593Smuzhiyun  * @bus:	Bus to examine
507*4882a593Smuzhiyun  * @chip_addr:	Chip address for the new device
508*4882a593Smuzhiyun  * @offset_len:	Length of a register offset in bytes (normally 1)
509*4882a593Smuzhiyun  * @devp:	Returns pointer to new device if found or -ENODEV if not
510*4882a593Smuzhiyun  *		found
511*4882a593Smuzhiyun  */
512*4882a593Smuzhiyun int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
513*4882a593Smuzhiyun 		 struct udevice **devp);
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun /**
516*4882a593Smuzhiyun  * i2c_get_chip_for_busnum() - get a device to use to access a chip on
517*4882a593Smuzhiyun  *			       a bus number
518*4882a593Smuzhiyun  *
519*4882a593Smuzhiyun  * This returns the device for the given chip address on a particular bus
520*4882a593Smuzhiyun  * number.
521*4882a593Smuzhiyun  *
522*4882a593Smuzhiyun  * @busnum:	Bus number to examine
523*4882a593Smuzhiyun  * @chip_addr:	Chip address for the new device
524*4882a593Smuzhiyun  * @offset_len:	Length of a register offset in bytes (normally 1)
525*4882a593Smuzhiyun  * @devp:	Returns pointer to new device if found or -ENODEV if not
526*4882a593Smuzhiyun  *		found
527*4882a593Smuzhiyun  */
528*4882a593Smuzhiyun int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
529*4882a593Smuzhiyun 			    struct udevice **devp);
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun /**
532*4882a593Smuzhiyun  * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data
533*4882a593Smuzhiyun  *
534*4882a593Smuzhiyun  * This decodes the chip address from a device tree node and puts it into
535*4882a593Smuzhiyun  * its dm_i2c_chip structure. This should be called in your driver's
536*4882a593Smuzhiyun  * ofdata_to_platdata() method.
537*4882a593Smuzhiyun  *
538*4882a593Smuzhiyun  * @blob:	Device tree blob
539*4882a593Smuzhiyun  * @node:	Node offset to read from
540*4882a593Smuzhiyun  * @spi:	Place to put the decoded information
541*4882a593Smuzhiyun  */
542*4882a593Smuzhiyun int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip);
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun /**
545*4882a593Smuzhiyun  * i2c_dump_msgs() - Dump a list of I2C messages
546*4882a593Smuzhiyun  *
547*4882a593Smuzhiyun  * This may be useful for debugging.
548*4882a593Smuzhiyun  *
549*4882a593Smuzhiyun  * @msg:	Message list to dump
550*4882a593Smuzhiyun  * @nmsgs:	Number of messages
551*4882a593Smuzhiyun  */
552*4882a593Smuzhiyun void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs);
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun #ifndef CONFIG_DM_I2C
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun /*
557*4882a593Smuzhiyun  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
558*4882a593Smuzhiyun  *
559*4882a593Smuzhiyun  * The implementation MUST NOT use static or global variables if the
560*4882a593Smuzhiyun  * I2C routines are used to read SDRAM configuration information
561*4882a593Smuzhiyun  * because this is done before the memories are initialized. Limited
562*4882a593Smuzhiyun  * use of stack-based variables are OK (the initial stack size is
563*4882a593Smuzhiyun  * limited).
564*4882a593Smuzhiyun  *
565*4882a593Smuzhiyun  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
566*4882a593Smuzhiyun  */
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun /*
569*4882a593Smuzhiyun  * Configuration items.
570*4882a593Smuzhiyun  */
571*4882a593Smuzhiyun #define I2C_RXTX_LEN	128	/* maximum tx/rx buffer length */
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
574*4882a593Smuzhiyun /* no muxes used bus = i2c adapters */
575*4882a593Smuzhiyun #define CONFIG_SYS_I2C_DIRECT_BUS	1
576*4882a593Smuzhiyun #define CONFIG_SYS_I2C_MAX_HOPS		0
577*4882a593Smuzhiyun #define CONFIG_SYS_NUM_I2C_BUSES	ll_entry_count(struct i2c_adapter, i2c)
578*4882a593Smuzhiyun #else
579*4882a593Smuzhiyun /* we use i2c muxes */
580*4882a593Smuzhiyun #undef CONFIG_SYS_I2C_DIRECT_BUS
581*4882a593Smuzhiyun #endif
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun /* define the I2C bus number for RTC and DTT if not already done */
584*4882a593Smuzhiyun #if !defined(CONFIG_SYS_RTC_BUS_NUM)
585*4882a593Smuzhiyun #define CONFIG_SYS_RTC_BUS_NUM		0
586*4882a593Smuzhiyun #endif
587*4882a593Smuzhiyun #if !defined(CONFIG_SYS_SPD_BUS_NUM)
588*4882a593Smuzhiyun #define CONFIG_SYS_SPD_BUS_NUM		0
589*4882a593Smuzhiyun #endif
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun struct i2c_adapter {
592*4882a593Smuzhiyun 	void		(*init)(struct i2c_adapter *adap, int speed,
593*4882a593Smuzhiyun 				int slaveaddr);
594*4882a593Smuzhiyun 	int		(*probe)(struct i2c_adapter *adap, uint8_t chip);
595*4882a593Smuzhiyun 	int		(*read)(struct i2c_adapter *adap, uint8_t chip,
596*4882a593Smuzhiyun 				uint addr, int alen, uint8_t *buffer,
597*4882a593Smuzhiyun 				int len);
598*4882a593Smuzhiyun 	int		(*write)(struct i2c_adapter *adap, uint8_t chip,
599*4882a593Smuzhiyun 				uint addr, int alen, uint8_t *buffer,
600*4882a593Smuzhiyun 				int len);
601*4882a593Smuzhiyun 	uint		(*set_bus_speed)(struct i2c_adapter *adap,
602*4882a593Smuzhiyun 				uint speed);
603*4882a593Smuzhiyun 	int		speed;
604*4882a593Smuzhiyun 	int		waitdelay;
605*4882a593Smuzhiyun 	int		slaveaddr;
606*4882a593Smuzhiyun 	int		init_done;
607*4882a593Smuzhiyun 	int		hwadapnr;
608*4882a593Smuzhiyun 	char		*name;
609*4882a593Smuzhiyun };
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
612*4882a593Smuzhiyun 		_set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
613*4882a593Smuzhiyun 	{ \
614*4882a593Smuzhiyun 		.init		=	_init, \
615*4882a593Smuzhiyun 		.probe		=	_probe, \
616*4882a593Smuzhiyun 		.read		=	_read, \
617*4882a593Smuzhiyun 		.write		=	_write, \
618*4882a593Smuzhiyun 		.set_bus_speed	=	_set_speed, \
619*4882a593Smuzhiyun 		.speed		=	_speed, \
620*4882a593Smuzhiyun 		.slaveaddr	=	_slaveaddr, \
621*4882a593Smuzhiyun 		.init_done	=	0, \
622*4882a593Smuzhiyun 		.hwadapnr	=	_hwadapnr, \
623*4882a593Smuzhiyun 		.name		=	#_name \
624*4882a593Smuzhiyun };
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
627*4882a593Smuzhiyun 			_set_speed, _speed, _slaveaddr, _hwadapnr) \
628*4882a593Smuzhiyun 	ll_entry_declare(struct i2c_adapter, _name, i2c) = \
629*4882a593Smuzhiyun 	U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
630*4882a593Smuzhiyun 		 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun struct i2c_adapter *i2c_get_adapter(int index);
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun #ifndef CONFIG_SYS_I2C_DIRECT_BUS
635*4882a593Smuzhiyun struct i2c_mux {
636*4882a593Smuzhiyun 	int	id;
637*4882a593Smuzhiyun 	char	name[16];
638*4882a593Smuzhiyun };
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun struct i2c_next_hop {
641*4882a593Smuzhiyun 	struct i2c_mux		mux;
642*4882a593Smuzhiyun 	uint8_t		chip;
643*4882a593Smuzhiyun 	uint8_t		channel;
644*4882a593Smuzhiyun };
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun struct i2c_bus_hose {
647*4882a593Smuzhiyun 	int	adapter;
648*4882a593Smuzhiyun 	struct i2c_next_hop	next_hop[CONFIG_SYS_I2C_MAX_HOPS];
649*4882a593Smuzhiyun };
650*4882a593Smuzhiyun #define I2C_NULL_HOP	{{-1, ""}, 0, 0}
651*4882a593Smuzhiyun extern struct i2c_bus_hose	i2c_bus[];
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun #define I2C_ADAPTER(bus)	i2c_bus[bus].adapter
654*4882a593Smuzhiyun #else
655*4882a593Smuzhiyun #define I2C_ADAPTER(bus)	bus
656*4882a593Smuzhiyun #endif
657*4882a593Smuzhiyun #define	I2C_BUS			gd->cur_i2c_bus
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun #define	I2C_ADAP_NR(bus)	i2c_get_adapter(I2C_ADAPTER(bus))
660*4882a593Smuzhiyun #define	I2C_ADAP		I2C_ADAP_NR(gd->cur_i2c_bus)
661*4882a593Smuzhiyun #define I2C_ADAP_HWNR		(I2C_ADAP->hwadapnr)
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun #ifndef CONFIG_SYS_I2C_DIRECT_BUS
664*4882a593Smuzhiyun #define I2C_MUX_PCA9540_ID	1
665*4882a593Smuzhiyun #define I2C_MUX_PCA9540		{I2C_MUX_PCA9540_ID, "PCA9540B"}
666*4882a593Smuzhiyun #define I2C_MUX_PCA9542_ID	2
667*4882a593Smuzhiyun #define I2C_MUX_PCA9542		{I2C_MUX_PCA9542_ID, "PCA9542A"}
668*4882a593Smuzhiyun #define I2C_MUX_PCA9544_ID	3
669*4882a593Smuzhiyun #define I2C_MUX_PCA9544		{I2C_MUX_PCA9544_ID, "PCA9544A"}
670*4882a593Smuzhiyun #define I2C_MUX_PCA9547_ID	4
671*4882a593Smuzhiyun #define I2C_MUX_PCA9547		{I2C_MUX_PCA9547_ID, "PCA9547A"}
672*4882a593Smuzhiyun #define I2C_MUX_PCA9548_ID	5
673*4882a593Smuzhiyun #define I2C_MUX_PCA9548		{I2C_MUX_PCA9548_ID, "PCA9548"}
674*4882a593Smuzhiyun #endif
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun #ifndef I2C_SOFT_DECLARATIONS
677*4882a593Smuzhiyun # if (defined(CONFIG_AT91RM9200) || \
678*4882a593Smuzhiyun 	defined(CONFIG_AT91SAM9260) ||  defined(CONFIG_AT91SAM9261) || \
679*4882a593Smuzhiyun 	defined(CONFIG_AT91SAM9263))
680*4882a593Smuzhiyun #  define I2C_SOFT_DECLARATIONS	at91_pio_t *pio	= (at91_pio_t *) ATMEL_BASE_PIOA;
681*4882a593Smuzhiyun # else
682*4882a593Smuzhiyun #  define I2C_SOFT_DECLARATIONS
683*4882a593Smuzhiyun # endif
684*4882a593Smuzhiyun #endif
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun /*
687*4882a593Smuzhiyun  * Many boards/controllers/drivers don't support an I2C slave interface so
688*4882a593Smuzhiyun  * provide a default slave address for them for use in common code.  A real
689*4882a593Smuzhiyun  * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
690*4882a593Smuzhiyun  * support a slave interface.
691*4882a593Smuzhiyun  */
692*4882a593Smuzhiyun #ifndef	CONFIG_SYS_I2C_SLAVE
693*4882a593Smuzhiyun #define	CONFIG_SYS_I2C_SLAVE	0xfe
694*4882a593Smuzhiyun #endif
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun /*
697*4882a593Smuzhiyun  * Initialization, must be called once on start up, may be called
698*4882a593Smuzhiyun  * repeatedly to change the speed and slave addresses.
699*4882a593Smuzhiyun  */
700*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C_EARLY_INIT
701*4882a593Smuzhiyun void i2c_early_init_f(void);
702*4882a593Smuzhiyun #endif
703*4882a593Smuzhiyun void i2c_init(int speed, int slaveaddr);
704*4882a593Smuzhiyun void i2c_init_board(void);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun #ifdef CONFIG_SYS_I2C
707*4882a593Smuzhiyun /*
708*4882a593Smuzhiyun  * i2c_get_bus_num:
709*4882a593Smuzhiyun  *
710*4882a593Smuzhiyun  *  Returns index of currently active I2C bus.  Zero-based.
711*4882a593Smuzhiyun  */
712*4882a593Smuzhiyun unsigned int i2c_get_bus_num(void);
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun /*
715*4882a593Smuzhiyun  * i2c_set_bus_num:
716*4882a593Smuzhiyun  *
717*4882a593Smuzhiyun  *  Change the active I2C bus.  Subsequent read/write calls will
718*4882a593Smuzhiyun  *  go to this one.
719*4882a593Smuzhiyun  *
720*4882a593Smuzhiyun  *	bus - bus index, zero based
721*4882a593Smuzhiyun  *
722*4882a593Smuzhiyun  *	Returns: 0 on success, not 0 on failure
723*4882a593Smuzhiyun  *
724*4882a593Smuzhiyun  */
725*4882a593Smuzhiyun int i2c_set_bus_num(unsigned int bus);
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun /*
728*4882a593Smuzhiyun  * i2c_init_all():
729*4882a593Smuzhiyun  *
730*4882a593Smuzhiyun  * Initializes all I2C adapters in the system. All i2c_adap structures must
731*4882a593Smuzhiyun  * be initialized beforehead with function pointers and data, including
732*4882a593Smuzhiyun  * speed and slaveaddr. Returns 0 on success, non-0 on failure.
733*4882a593Smuzhiyun  */
734*4882a593Smuzhiyun void i2c_init_all(void);
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun /*
737*4882a593Smuzhiyun  * Probe the given I2C chip address.  Returns 0 if a chip responded,
738*4882a593Smuzhiyun  * not 0 on failure.
739*4882a593Smuzhiyun  */
740*4882a593Smuzhiyun int i2c_probe(uint8_t chip);
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun /*
743*4882a593Smuzhiyun  * Read/Write interface:
744*4882a593Smuzhiyun  *   chip:    I2C chip address, range 0..127
745*4882a593Smuzhiyun  *   addr:    Memory (register) address within the chip
746*4882a593Smuzhiyun  *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
747*4882a593Smuzhiyun  *              memories, 0 for register type devices with only one
748*4882a593Smuzhiyun  *              register)
749*4882a593Smuzhiyun  *   buffer:  Where to read/write the data
750*4882a593Smuzhiyun  *   len:     How many bytes to read/write
751*4882a593Smuzhiyun  *
752*4882a593Smuzhiyun  *   Returns: 0 on success, not 0 on failure
753*4882a593Smuzhiyun  */
754*4882a593Smuzhiyun int i2c_read(uint8_t chip, unsigned int addr, int alen,
755*4882a593Smuzhiyun 				uint8_t *buffer, int len);
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun int i2c_write(uint8_t chip, unsigned int addr, int alen,
758*4882a593Smuzhiyun 				uint8_t *buffer, int len);
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun /*
761*4882a593Smuzhiyun  * Utility routines to read/write registers.
762*4882a593Smuzhiyun  */
763*4882a593Smuzhiyun uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun /*
768*4882a593Smuzhiyun  * i2c_set_bus_speed:
769*4882a593Smuzhiyun  *
770*4882a593Smuzhiyun  *  Change the speed of the active I2C bus
771*4882a593Smuzhiyun  *
772*4882a593Smuzhiyun  *	speed - bus speed in Hz
773*4882a593Smuzhiyun  *
774*4882a593Smuzhiyun  *	Returns: new bus speed
775*4882a593Smuzhiyun  *
776*4882a593Smuzhiyun  */
777*4882a593Smuzhiyun unsigned int i2c_set_bus_speed(unsigned int speed);
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun /*
780*4882a593Smuzhiyun  * i2c_get_bus_speed:
781*4882a593Smuzhiyun  *
782*4882a593Smuzhiyun  *  Returns speed of currently active I2C bus in Hz
783*4882a593Smuzhiyun  */
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun unsigned int i2c_get_bus_speed(void);
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun #else
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun /*
790*4882a593Smuzhiyun  * Probe the given I2C chip address.  Returns 0 if a chip responded,
791*4882a593Smuzhiyun  * not 0 on failure.
792*4882a593Smuzhiyun  */
793*4882a593Smuzhiyun int i2c_probe(uchar chip);
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun /*
796*4882a593Smuzhiyun  * Read/Write interface:
797*4882a593Smuzhiyun  *   chip:    I2C chip address, range 0..127
798*4882a593Smuzhiyun  *   addr:    Memory (register) address within the chip
799*4882a593Smuzhiyun  *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
800*4882a593Smuzhiyun  *              memories, 0 for register type devices with only one
801*4882a593Smuzhiyun  *              register)
802*4882a593Smuzhiyun  *   buffer:  Where to read/write the data
803*4882a593Smuzhiyun  *   len:     How many bytes to read/write
804*4882a593Smuzhiyun  *
805*4882a593Smuzhiyun  *   Returns: 0 on success, not 0 on failure
806*4882a593Smuzhiyun  */
807*4882a593Smuzhiyun int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
808*4882a593Smuzhiyun int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun /*
811*4882a593Smuzhiyun  * Utility routines to read/write registers.
812*4882a593Smuzhiyun  */
i2c_reg_read(u8 addr,u8 reg)813*4882a593Smuzhiyun static inline u8 i2c_reg_read(u8 addr, u8 reg)
814*4882a593Smuzhiyun {
815*4882a593Smuzhiyun 	u8 buf;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun #ifdef DEBUG
818*4882a593Smuzhiyun 	printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
819*4882a593Smuzhiyun #endif
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	i2c_read(addr, reg, 1, &buf, 1);
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	return buf;
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun 
i2c_reg_write(u8 addr,u8 reg,u8 val)826*4882a593Smuzhiyun static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
827*4882a593Smuzhiyun {
828*4882a593Smuzhiyun #ifdef DEBUG
829*4882a593Smuzhiyun 	printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
830*4882a593Smuzhiyun 	       __func__, addr, reg, val);
831*4882a593Smuzhiyun #endif
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 	i2c_write(addr, reg, 1, &val, 1);
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun /*
837*4882a593Smuzhiyun  * Functions for setting the current I2C bus and its speed
838*4882a593Smuzhiyun  */
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun /*
841*4882a593Smuzhiyun  * i2c_set_bus_num:
842*4882a593Smuzhiyun  *
843*4882a593Smuzhiyun  *  Change the active I2C bus.  Subsequent read/write calls will
844*4882a593Smuzhiyun  *  go to this one.
845*4882a593Smuzhiyun  *
846*4882a593Smuzhiyun  *	bus - bus index, zero based
847*4882a593Smuzhiyun  *
848*4882a593Smuzhiyun  *	Returns: 0 on success, not 0 on failure
849*4882a593Smuzhiyun  *
850*4882a593Smuzhiyun  */
851*4882a593Smuzhiyun int i2c_set_bus_num(unsigned int bus);
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun /*
854*4882a593Smuzhiyun  * i2c_get_bus_num:
855*4882a593Smuzhiyun  *
856*4882a593Smuzhiyun  *  Returns index of currently active I2C bus.  Zero-based.
857*4882a593Smuzhiyun  */
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun unsigned int i2c_get_bus_num(void);
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun /*
862*4882a593Smuzhiyun  * i2c_set_bus_speed:
863*4882a593Smuzhiyun  *
864*4882a593Smuzhiyun  *  Change the speed of the active I2C bus
865*4882a593Smuzhiyun  *
866*4882a593Smuzhiyun  *	speed - bus speed in Hz
867*4882a593Smuzhiyun  *
868*4882a593Smuzhiyun  *	Returns: 0 on success, not 0 on failure
869*4882a593Smuzhiyun  *
870*4882a593Smuzhiyun  */
871*4882a593Smuzhiyun int i2c_set_bus_speed(unsigned int);
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun /*
874*4882a593Smuzhiyun  * i2c_get_bus_speed:
875*4882a593Smuzhiyun  *
876*4882a593Smuzhiyun  *  Returns speed of currently active I2C bus in Hz
877*4882a593Smuzhiyun  */
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun unsigned int i2c_get_bus_speed(void);
880*4882a593Smuzhiyun #endif /* CONFIG_SYS_I2C */
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun /*
883*4882a593Smuzhiyun  * only for backwardcompatibility, should go away if we switched
884*4882a593Smuzhiyun  * completely to new multibus support.
885*4882a593Smuzhiyun  */
886*4882a593Smuzhiyun #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
887*4882a593Smuzhiyun # if !defined(CONFIG_SYS_MAX_I2C_BUS)
888*4882a593Smuzhiyun #  define CONFIG_SYS_MAX_I2C_BUS		2
889*4882a593Smuzhiyun # endif
890*4882a593Smuzhiyun # define I2C_MULTI_BUS				1
891*4882a593Smuzhiyun #else
892*4882a593Smuzhiyun # define CONFIG_SYS_MAX_I2C_BUS		1
893*4882a593Smuzhiyun # define I2C_MULTI_BUS				0
894*4882a593Smuzhiyun #endif
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun /* NOTE: These two functions MUST be always_inline to avoid code growth! */
897*4882a593Smuzhiyun static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
I2C_GET_BUS(void)898*4882a593Smuzhiyun static inline unsigned int I2C_GET_BUS(void)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun 	return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
I2C_SET_BUS(unsigned int bus)904*4882a593Smuzhiyun static inline void I2C_SET_BUS(unsigned int bus)
905*4882a593Smuzhiyun {
906*4882a593Smuzhiyun 	if (I2C_MULTI_BUS)
907*4882a593Smuzhiyun 		i2c_set_bus_num(bus);
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun /* Multi I2C definitions */
911*4882a593Smuzhiyun enum {
912*4882a593Smuzhiyun 	I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
913*4882a593Smuzhiyun 	I2C_8, I2C_9, I2C_10,
914*4882a593Smuzhiyun };
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun /**
917*4882a593Smuzhiyun  * Get FDT values for i2c bus.
918*4882a593Smuzhiyun  *
919*4882a593Smuzhiyun  * @param blob  Device tree blbo
920*4882a593Smuzhiyun  * @return the number of I2C bus
921*4882a593Smuzhiyun  */
922*4882a593Smuzhiyun void board_i2c_init(const void *blob);
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun /**
925*4882a593Smuzhiyun  * Find the I2C bus number by given a FDT I2C node.
926*4882a593Smuzhiyun  *
927*4882a593Smuzhiyun  * @param blob  Device tree blbo
928*4882a593Smuzhiyun  * @param node  FDT I2C node to find
929*4882a593Smuzhiyun  * @return the number of I2C bus (zero based), or -1 on error
930*4882a593Smuzhiyun  */
931*4882a593Smuzhiyun int i2c_get_bus_num_fdt(int node);
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun /**
934*4882a593Smuzhiyun  * Reset the I2C bus represented by the given a FDT I2C node.
935*4882a593Smuzhiyun  *
936*4882a593Smuzhiyun  * @param blob  Device tree blbo
937*4882a593Smuzhiyun  * @param node  FDT I2C node to find
938*4882a593Smuzhiyun  * @return 0 if port was reset, -1 if not found
939*4882a593Smuzhiyun  */
940*4882a593Smuzhiyun int i2c_reset_port_fdt(const void *blob, int node);
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun #endif /* !CONFIG_DM_I2C */
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun #endif	/* _I2C_H_ */
945