xref: /rk3399_rockchip-uboot/include/i2c.h (revision d5243359e1afc957acd373dbbde1cf6c70ee5485)
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 /*
211f045217Swdenk  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
221f045217Swdenk  *
231f045217Swdenk  * The implementation MUST NOT use static or global variables if the
241f045217Swdenk  * I2C routines are used to read SDRAM configuration information
251f045217Swdenk  * because this is done before the memories are initialized. Limited
261f045217Swdenk  * use of stack-based variables are OK (the initial stack size is
271f045217Swdenk  * limited).
281f045217Swdenk  *
291f045217Swdenk  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
301f045217Swdenk  */
311f045217Swdenk 
321f045217Swdenk /*
331f045217Swdenk  * Configuration items.
341f045217Swdenk  */
351f045217Swdenk #define I2C_RXTX_LEN	128	/* maximum tx/rx buffer length */
361f045217Swdenk 
37385c9ef5SHeiko Schocher #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
38385c9ef5SHeiko Schocher /* no muxes used bus = i2c adapters */
39385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_DIRECT_BUS	1
40385c9ef5SHeiko Schocher #define CONFIG_SYS_I2C_MAX_HOPS		0
41385c9ef5SHeiko Schocher #define CONFIG_SYS_NUM_I2C_BUSES	ll_entry_count(struct i2c_adapter, i2c)
4279b2d0bbSStefan Roese #else
43385c9ef5SHeiko Schocher /* we use i2c muxes */
44385c9ef5SHeiko Schocher #undef CONFIG_SYS_I2C_DIRECT_BUS
4579b2d0bbSStefan Roese #endif
4679b2d0bbSStefan Roese 
478c12045aSStefan Roese /* define the I2C bus number for RTC and DTT if not already done */
486d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_RTC_BUS_NUM)
496d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_RTC_BUS_NUM		0
508c12045aSStefan Roese #endif
516d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_DTT_BUS_NUM)
526d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_DTT_BUS_NUM		0
538c12045aSStefan Roese #endif
546d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #if !defined(CONFIG_SYS_SPD_BUS_NUM)
556d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #define CONFIG_SYS_SPD_BUS_NUM		0
56d8a8ea5cSMatthias Fuchs #endif
578c12045aSStefan Roese 
58385c9ef5SHeiko Schocher struct i2c_adapter {
59385c9ef5SHeiko Schocher 	void		(*init)(struct i2c_adapter *adap, int speed,
60385c9ef5SHeiko Schocher 				int slaveaddr);
61385c9ef5SHeiko Schocher 	int		(*probe)(struct i2c_adapter *adap, uint8_t chip);
62385c9ef5SHeiko Schocher 	int		(*read)(struct i2c_adapter *adap, uint8_t chip,
63385c9ef5SHeiko Schocher 				uint addr, int alen, uint8_t *buffer,
64385c9ef5SHeiko Schocher 				int len);
65385c9ef5SHeiko Schocher 	int		(*write)(struct i2c_adapter *adap, uint8_t chip,
66385c9ef5SHeiko Schocher 				uint addr, int alen, uint8_t *buffer,
67385c9ef5SHeiko Schocher 				int len);
68385c9ef5SHeiko Schocher 	uint		(*set_bus_speed)(struct i2c_adapter *adap,
69385c9ef5SHeiko Schocher 				uint speed);
70385c9ef5SHeiko Schocher 	int		speed;
71*d5243359SHannes Petermaier 	int		waitdelay;
72385c9ef5SHeiko Schocher 	int		slaveaddr;
73385c9ef5SHeiko Schocher 	int		init_done;
74385c9ef5SHeiko Schocher 	int		hwadapnr;
75385c9ef5SHeiko Schocher 	char		*name;
76385c9ef5SHeiko Schocher };
77385c9ef5SHeiko Schocher 
78385c9ef5SHeiko Schocher #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
79385c9ef5SHeiko Schocher 		_set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
80385c9ef5SHeiko Schocher 	{ \
81385c9ef5SHeiko Schocher 		.init		=	_init, \
82385c9ef5SHeiko Schocher 		.probe		=	_probe, \
83385c9ef5SHeiko Schocher 		.read		=	_read, \
84385c9ef5SHeiko Schocher 		.write		=	_write, \
85385c9ef5SHeiko Schocher 		.set_bus_speed	=	_set_speed, \
86385c9ef5SHeiko Schocher 		.speed		=	_speed, \
87385c9ef5SHeiko Schocher 		.slaveaddr	=	_slaveaddr, \
88385c9ef5SHeiko Schocher 		.init_done	=	0, \
89385c9ef5SHeiko Schocher 		.hwadapnr	=	_hwadapnr, \
90385c9ef5SHeiko Schocher 		.name		=	#_name \
91385c9ef5SHeiko Schocher };
92385c9ef5SHeiko Schocher 
93385c9ef5SHeiko Schocher #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
94385c9ef5SHeiko Schocher 			_set_speed, _speed, _slaveaddr, _hwadapnr) \
95385c9ef5SHeiko Schocher 	ll_entry_declare(struct i2c_adapter, _name, i2c) = \
96385c9ef5SHeiko Schocher 	U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
97385c9ef5SHeiko Schocher 		 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
98385c9ef5SHeiko Schocher 
99385c9ef5SHeiko Schocher struct i2c_adapter *i2c_get_adapter(int index);
100385c9ef5SHeiko Schocher 
101385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS
102385c9ef5SHeiko Schocher struct i2c_mux {
103385c9ef5SHeiko Schocher 	int	id;
104385c9ef5SHeiko Schocher 	char	name[16];
105385c9ef5SHeiko Schocher };
106385c9ef5SHeiko Schocher 
107385c9ef5SHeiko Schocher struct i2c_next_hop {
108385c9ef5SHeiko Schocher 	struct i2c_mux		mux;
109385c9ef5SHeiko Schocher 	uint8_t		chip;
110385c9ef5SHeiko Schocher 	uint8_t		channel;
111385c9ef5SHeiko Schocher };
112385c9ef5SHeiko Schocher 
113385c9ef5SHeiko Schocher struct i2c_bus_hose {
114385c9ef5SHeiko Schocher 	int	adapter;
115385c9ef5SHeiko Schocher 	struct i2c_next_hop	next_hop[CONFIG_SYS_I2C_MAX_HOPS];
116385c9ef5SHeiko Schocher };
117385c9ef5SHeiko Schocher #define I2C_NULL_HOP	{{-1, ""}, 0, 0}
118385c9ef5SHeiko Schocher extern struct i2c_bus_hose	i2c_bus[];
119385c9ef5SHeiko Schocher 
120385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus)	i2c_bus[bus].adapter
121385c9ef5SHeiko Schocher #else
122385c9ef5SHeiko Schocher #define I2C_ADAPTER(bus)	bus
123385c9ef5SHeiko Schocher #endif
124385c9ef5SHeiko Schocher #define	I2C_BUS			gd->cur_i2c_bus
125385c9ef5SHeiko Schocher 
126385c9ef5SHeiko Schocher #define	I2C_ADAP_NR(bus)	i2c_get_adapter(I2C_ADAPTER(bus))
127385c9ef5SHeiko Schocher #define	I2C_ADAP		I2C_ADAP_NR(gd->cur_i2c_bus)
128385c9ef5SHeiko Schocher #define I2C_ADAP_HWNR		(I2C_ADAP->hwadapnr)
129385c9ef5SHeiko Schocher 
130385c9ef5SHeiko Schocher #ifndef CONFIG_SYS_I2C_DIRECT_BUS
131385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540_ID	1
132385c9ef5SHeiko Schocher #define I2C_MUX_PCA9540		{I2C_MUX_PCA9540_ID, "PCA9540B"}
133385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542_ID	2
134385c9ef5SHeiko Schocher #define I2C_MUX_PCA9542		{I2C_MUX_PCA9542_ID, "PCA9542A"}
135385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544_ID	3
136385c9ef5SHeiko Schocher #define I2C_MUX_PCA9544		{I2C_MUX_PCA9544_ID, "PCA9544A"}
137385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547_ID	4
138385c9ef5SHeiko Schocher #define I2C_MUX_PCA9547		{I2C_MUX_PCA9547_ID, "PCA9547A"}
139e6658749SMichael Burr #define I2C_MUX_PCA9548_ID	5
140e6658749SMichael Burr #define I2C_MUX_PCA9548		{I2C_MUX_PCA9548_ID, "PCA9548"}
141385c9ef5SHeiko Schocher #endif
142385c9ef5SHeiko Schocher 
14398aed379SHeiko Schocher #ifndef I2C_SOFT_DECLARATIONS
14498aed379SHeiko Schocher # if defined(CONFIG_MPC8260)
1456d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #  define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
14698aed379SHeiko Schocher # elif defined(CONFIG_8xx)
1476d0f6bcfSJean-Christophe PLAGNIOL-VILLARD #  define I2C_SOFT_DECLARATIONS	volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
1480cf0b931SJens Scharsig 
1490cf0b931SJens Scharsig # elif (defined(CONFIG_AT91RM9200) || \
1500cf0b931SJens Scharsig 	defined(CONFIG_AT91SAM9260) ||  defined(CONFIG_AT91SAM9261) || \
151cb96a0a4SAndreas Bießmann 	defined(CONFIG_AT91SAM9263))
15278132275Sesw@bus-elektronik.de #  define I2C_SOFT_DECLARATIONS	at91_pio_t *pio	= (at91_pio_t *) ATMEL_BASE_PIOA;
15398aed379SHeiko Schocher # else
15498aed379SHeiko Schocher #  define I2C_SOFT_DECLARATIONS
15598aed379SHeiko Schocher # endif
15698aed379SHeiko Schocher #endif
157ecf5f077STimur Tabi 
158ecf5f077STimur Tabi #ifdef CONFIG_8xx
1599c90a2c8SPeter Tyser /* Set default value for the I2C bus speed on 8xx. In the
160ecf5f077STimur Tabi  * future, we'll define these in all 8xx board config files.
161ecf5f077STimur Tabi  */
162ecf5f077STimur Tabi #ifndef	CONFIG_SYS_I2C_SPEED
163ecf5f077STimur Tabi #define	CONFIG_SYS_I2C_SPEED	50000
164ecf5f077STimur Tabi #endif
165ecf5f077STimur Tabi #endif
1669c90a2c8SPeter Tyser 
1679c90a2c8SPeter Tyser /*
1689c90a2c8SPeter Tyser  * Many boards/controllers/drivers don't support an I2C slave interface so
1699c90a2c8SPeter Tyser  * provide a default slave address for them for use in common code.  A real
1709c90a2c8SPeter Tyser  * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
1719c90a2c8SPeter Tyser  * support a slave interface.
1729c90a2c8SPeter Tyser  */
1739c90a2c8SPeter Tyser #ifndef	CONFIG_SYS_I2C_SLAVE
1749c90a2c8SPeter Tyser #define	CONFIG_SYS_I2C_SLAVE	0xfe
175ecf5f077STimur Tabi #endif
176ecf5f077STimur Tabi 
1771f045217Swdenk /*
1781f045217Swdenk  * Initialization, must be called once on start up, may be called
1791f045217Swdenk  * repeatedly to change the speed and slave addresses.
1801f045217Swdenk  */
1811f045217Swdenk void i2c_init(int speed, int slaveaddr);
18206d01dbeSwdenk void i2c_init_board(void);
18326a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
18426a33504SRichard Retanubun void i2c_board_late_init(void);
18526a33504SRichard Retanubun #endif
1861f045217Swdenk 
187385c9ef5SHeiko Schocher #ifdef CONFIG_SYS_I2C
188385c9ef5SHeiko Schocher /*
189385c9ef5SHeiko Schocher  * i2c_get_bus_num:
190385c9ef5SHeiko Schocher  *
191385c9ef5SHeiko Schocher  *  Returns index of currently active I2C bus.  Zero-based.
192385c9ef5SHeiko Schocher  */
193385c9ef5SHeiko Schocher unsigned int i2c_get_bus_num(void);
19467b23a32SHeiko Schocher 
195385c9ef5SHeiko Schocher /*
196385c9ef5SHeiko Schocher  * i2c_set_bus_num:
197385c9ef5SHeiko Schocher  *
198385c9ef5SHeiko Schocher  *  Change the active I2C bus.  Subsequent read/write calls will
199385c9ef5SHeiko Schocher  *  go to this one.
200385c9ef5SHeiko Schocher  *
201385c9ef5SHeiko Schocher  *	bus - bus index, zero based
202385c9ef5SHeiko Schocher  *
203385c9ef5SHeiko Schocher  *	Returns: 0 on success, not 0 on failure
204385c9ef5SHeiko Schocher  *
205385c9ef5SHeiko Schocher  */
206385c9ef5SHeiko Schocher int i2c_set_bus_num(unsigned int bus);
20767b23a32SHeiko Schocher 
208385c9ef5SHeiko Schocher /*
209385c9ef5SHeiko Schocher  * i2c_init_all():
210385c9ef5SHeiko Schocher  *
211385c9ef5SHeiko Schocher  * Initializes all I2C adapters in the system. All i2c_adap structures must
212385c9ef5SHeiko Schocher  * be initialized beforehead with function pointers and data, including
213385c9ef5SHeiko Schocher  * speed and slaveaddr. Returns 0 on success, non-0 on failure.
214385c9ef5SHeiko Schocher  */
215385c9ef5SHeiko Schocher void i2c_init_all(void);
21667b23a32SHeiko Schocher 
217385c9ef5SHeiko Schocher /*
218385c9ef5SHeiko Schocher  * Probe the given I2C chip address.  Returns 0 if a chip responded,
219385c9ef5SHeiko Schocher  * not 0 on failure.
220385c9ef5SHeiko Schocher  */
221385c9ef5SHeiko Schocher int i2c_probe(uint8_t chip);
222385c9ef5SHeiko Schocher 
223385c9ef5SHeiko Schocher /*
224385c9ef5SHeiko Schocher  * Read/Write interface:
225385c9ef5SHeiko Schocher  *   chip:    I2C chip address, range 0..127
226385c9ef5SHeiko Schocher  *   addr:    Memory (register) address within the chip
227385c9ef5SHeiko Schocher  *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
228385c9ef5SHeiko Schocher  *              memories, 0 for register type devices with only one
229385c9ef5SHeiko Schocher  *              register)
230385c9ef5SHeiko Schocher  *   buffer:  Where to read/write the data
231385c9ef5SHeiko Schocher  *   len:     How many bytes to read/write
232385c9ef5SHeiko Schocher  *
233385c9ef5SHeiko Schocher  *   Returns: 0 on success, not 0 on failure
234385c9ef5SHeiko Schocher  */
235385c9ef5SHeiko Schocher int i2c_read(uint8_t chip, unsigned int addr, int alen,
236385c9ef5SHeiko Schocher 				uint8_t *buffer, int len);
237385c9ef5SHeiko Schocher 
238385c9ef5SHeiko Schocher int i2c_write(uint8_t chip, unsigned int addr, int alen,
239385c9ef5SHeiko Schocher 				uint8_t *buffer, int len);
240385c9ef5SHeiko Schocher 
241385c9ef5SHeiko Schocher /*
242385c9ef5SHeiko Schocher  * Utility routines to read/write registers.
243385c9ef5SHeiko Schocher  */
244385c9ef5SHeiko Schocher uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
245385c9ef5SHeiko Schocher 
246385c9ef5SHeiko Schocher void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
247385c9ef5SHeiko Schocher 
248385c9ef5SHeiko Schocher /*
249385c9ef5SHeiko Schocher  * i2c_set_bus_speed:
250385c9ef5SHeiko Schocher  *
251385c9ef5SHeiko Schocher  *  Change the speed of the active I2C bus
252385c9ef5SHeiko Schocher  *
253385c9ef5SHeiko Schocher  *	speed - bus speed in Hz
254385c9ef5SHeiko Schocher  *
255385c9ef5SHeiko Schocher  *	Returns: new bus speed
256385c9ef5SHeiko Schocher  *
257385c9ef5SHeiko Schocher  */
258385c9ef5SHeiko Schocher unsigned int i2c_set_bus_speed(unsigned int speed);
259385c9ef5SHeiko Schocher 
260385c9ef5SHeiko Schocher /*
261385c9ef5SHeiko Schocher  * i2c_get_bus_speed:
262385c9ef5SHeiko Schocher  *
263385c9ef5SHeiko Schocher  *  Returns speed of currently active I2C bus in Hz
264385c9ef5SHeiko Schocher  */
265385c9ef5SHeiko Schocher 
266385c9ef5SHeiko Schocher unsigned int i2c_get_bus_speed(void);
267385c9ef5SHeiko Schocher 
268385c9ef5SHeiko Schocher /*
269385c9ef5SHeiko Schocher  * i2c_reloc_fixup:
270385c9ef5SHeiko Schocher  *
271385c9ef5SHeiko Schocher  * Adjusts I2C pointers after U-Boot is relocated to DRAM
272385c9ef5SHeiko Schocher  */
273385c9ef5SHeiko Schocher void i2c_reloc_fixup(void);
274ea818dbbSHeiko Schocher #if defined(CONFIG_SYS_I2C_SOFT)
275ea818dbbSHeiko Schocher void i2c_soft_init(void);
276ea818dbbSHeiko Schocher void i2c_soft_active(void);
277ea818dbbSHeiko Schocher void i2c_soft_tristate(void);
278ea818dbbSHeiko Schocher int i2c_soft_read(void);
279ea818dbbSHeiko Schocher void i2c_soft_sda(int bit);
280ea818dbbSHeiko Schocher void i2c_soft_scl(int bit);
281ea818dbbSHeiko Schocher void i2c_soft_delay(void);
28267b23a32SHeiko Schocher #endif
283385c9ef5SHeiko Schocher #else
28467b23a32SHeiko Schocher 
2851f045217Swdenk /*
2861f045217Swdenk  * Probe the given I2C chip address.  Returns 0 if a chip responded,
2871f045217Swdenk  * not 0 on failure.
2881f045217Swdenk  */
2891f045217Swdenk int i2c_probe(uchar chip);
2901f045217Swdenk 
2911f045217Swdenk /*
2921f045217Swdenk  * Read/Write interface:
2931f045217Swdenk  *   chip:    I2C chip address, range 0..127
2941f045217Swdenk  *   addr:    Memory (register) address within the chip
2951f045217Swdenk  *   alen:    Number of bytes to use for addr (typically 1, 2 for larger
2961f045217Swdenk  *              memories, 0 for register type devices with only one
2971f045217Swdenk  *              register)
2981f045217Swdenk  *   buffer:  Where to read/write the data
2991f045217Swdenk  *   len:     How many bytes to read/write
3001f045217Swdenk  *
3011f045217Swdenk  *   Returns: 0 on success, not 0 on failure
3021f045217Swdenk  */
3031f045217Swdenk int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
3041f045217Swdenk int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
3051f045217Swdenk 
3061f045217Swdenk /*
3071f045217Swdenk  * Utility routines to read/write registers.
3081f045217Swdenk  */
309ecf5f077STimur Tabi static inline u8 i2c_reg_read(u8 addr, u8 reg)
310ecf5f077STimur Tabi {
311ecf5f077STimur Tabi 	u8 buf;
312ecf5f077STimur Tabi 
313ecf5f077STimur Tabi #ifdef CONFIG_8xx
314ecf5f077STimur Tabi 	/* MPC8xx needs this.  Maybe one day we can get rid of it. */
315ecf5f077STimur Tabi 	i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
316ecf5f077STimur Tabi #endif
317ecf5f077STimur Tabi 
318ecf5f077STimur Tabi #ifdef DEBUG
319ecf5f077STimur Tabi 	printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
320ecf5f077STimur Tabi #endif
321ecf5f077STimur Tabi 
322ecf5f077STimur Tabi 	i2c_read(addr, reg, 1, &buf, 1);
323ecf5f077STimur Tabi 
324ecf5f077STimur Tabi 	return buf;
325ecf5f077STimur Tabi }
326ecf5f077STimur Tabi 
327ecf5f077STimur Tabi static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
328ecf5f077STimur Tabi {
329ecf5f077STimur Tabi #ifdef CONFIG_8xx
330ecf5f077STimur Tabi 	/* MPC8xx needs this.  Maybe one day we can get rid of it. */
331ecf5f077STimur Tabi 	i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
332ecf5f077STimur Tabi #endif
333ecf5f077STimur Tabi 
334ecf5f077STimur Tabi #ifdef DEBUG
335ecf5f077STimur Tabi 	printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
336ecf5f077STimur Tabi 	       __func__, addr, reg, val);
337ecf5f077STimur Tabi #endif
338ecf5f077STimur Tabi 
339ecf5f077STimur Tabi 	i2c_write(addr, reg, 1, &val, 1);
340ecf5f077STimur Tabi }
3411f045217Swdenk 
342bb99ad6dSBen Warren /*
343bb99ad6dSBen Warren  * Functions for setting the current I2C bus and its speed
344bb99ad6dSBen Warren  */
345bb99ad6dSBen Warren 
346bb99ad6dSBen Warren /*
347bb99ad6dSBen Warren  * i2c_set_bus_num:
348bb99ad6dSBen Warren  *
349bb99ad6dSBen Warren  *  Change the active I2C bus.  Subsequent read/write calls will
350bb99ad6dSBen Warren  *  go to this one.
351bb99ad6dSBen Warren  *
352bb99ad6dSBen Warren  *	bus - bus index, zero based
353bb99ad6dSBen Warren  *
354bb99ad6dSBen Warren  *	Returns: 0 on success, not 0 on failure
355bb99ad6dSBen Warren  *
356bb99ad6dSBen Warren  */
3579ca880a2STimur Tabi int i2c_set_bus_num(unsigned int bus);
358bb99ad6dSBen Warren 
359bb99ad6dSBen Warren /*
360bb99ad6dSBen Warren  * i2c_get_bus_num:
361bb99ad6dSBen Warren  *
362bb99ad6dSBen Warren  *  Returns index of currently active I2C bus.  Zero-based.
363bb99ad6dSBen Warren  */
364bb99ad6dSBen Warren 
3659ca880a2STimur Tabi unsigned int i2c_get_bus_num(void);
366bb99ad6dSBen Warren 
367bb99ad6dSBen Warren /*
368bb99ad6dSBen Warren  * i2c_set_bus_speed:
369bb99ad6dSBen Warren  *
370bb99ad6dSBen Warren  *  Change the speed of the active I2C bus
371bb99ad6dSBen Warren  *
372bb99ad6dSBen Warren  *	speed - bus speed in Hz
373bb99ad6dSBen Warren  *
374bb99ad6dSBen Warren  *	Returns: 0 on success, not 0 on failure
375bb99ad6dSBen Warren  *
376bb99ad6dSBen Warren  */
3779ca880a2STimur Tabi int i2c_set_bus_speed(unsigned int);
378bb99ad6dSBen Warren 
379bb99ad6dSBen Warren /*
380bb99ad6dSBen Warren  * i2c_get_bus_speed:
381bb99ad6dSBen Warren  *
382bb99ad6dSBen Warren  *  Returns speed of currently active I2C bus in Hz
383bb99ad6dSBen Warren  */
384bb99ad6dSBen Warren 
3859ca880a2STimur Tabi unsigned int i2c_get_bus_speed(void);
386385c9ef5SHeiko Schocher #endif /* CONFIG_SYS_I2C */
387385c9ef5SHeiko Schocher 
388385c9ef5SHeiko Schocher /*
389385c9ef5SHeiko Schocher  * only for backwardcompatibility, should go away if we switched
390385c9ef5SHeiko Schocher  * completely to new multibus support.
391385c9ef5SHeiko Schocher  */
392385c9ef5SHeiko Schocher #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
393385c9ef5SHeiko Schocher # if !defined(CONFIG_SYS_MAX_I2C_BUS)
394385c9ef5SHeiko Schocher #  define CONFIG_SYS_MAX_I2C_BUS		2
395385c9ef5SHeiko Schocher # endif
396ea0f73abSŁukasz Majewski # define I2C_MULTI_BUS				1
397385c9ef5SHeiko Schocher #else
398385c9ef5SHeiko Schocher # define CONFIG_SYS_MAX_I2C_BUS		1
399385c9ef5SHeiko Schocher # define I2C_MULTI_BUS				0
400385c9ef5SHeiko Schocher #endif
401bb99ad6dSBen Warren 
402cd7b4e82SMarek Vasut /* NOTE: These two functions MUST be always_inline to avoid code growth! */
403cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
404cd7b4e82SMarek Vasut static inline unsigned int I2C_GET_BUS(void)
405cd7b4e82SMarek Vasut {
406cd7b4e82SMarek Vasut 	return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
407cd7b4e82SMarek Vasut }
408cd7b4e82SMarek Vasut 
409cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
410cd7b4e82SMarek Vasut static inline void I2C_SET_BUS(unsigned int bus)
411cd7b4e82SMarek Vasut {
412cd7b4e82SMarek Vasut 	if (I2C_MULTI_BUS)
413cd7b4e82SMarek Vasut 		i2c_set_bus_num(bus);
414cd7b4e82SMarek Vasut }
415cd7b4e82SMarek Vasut 
4167ca8f73aSŁukasz Majewski /* Multi I2C definitions */
4177ca8f73aSŁukasz Majewski enum {
4187ca8f73aSŁukasz Majewski 	I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
4197ca8f73aSŁukasz Majewski 	I2C_8, I2C_9, I2C_10,
4207ca8f73aSŁukasz Majewski };
4217ca8f73aSŁukasz Majewski 
4227ca8f73aSŁukasz Majewski /* Multi I2C busses handling */
4237ca8f73aSŁukasz Majewski #ifdef CONFIG_SOFT_I2C_MULTI_BUS
4247ca8f73aSŁukasz Majewski extern int get_multi_scl_pin(void);
4257ca8f73aSŁukasz Majewski extern int get_multi_sda_pin(void);
4267ca8f73aSŁukasz Majewski extern int multi_i2c_init(void);
4277ca8f73aSŁukasz Majewski #endif
428a9d2ae70SRajeshwari Shinde 
429a9d2ae70SRajeshwari Shinde /**
430a9d2ae70SRajeshwari Shinde  * Get FDT values for i2c bus.
431a9d2ae70SRajeshwari Shinde  *
432a9d2ae70SRajeshwari Shinde  * @param blob  Device tree blbo
433a9d2ae70SRajeshwari Shinde  * @return the number of I2C bus
434a9d2ae70SRajeshwari Shinde  */
435a9d2ae70SRajeshwari Shinde void board_i2c_init(const void *blob);
436a9d2ae70SRajeshwari Shinde 
437a9d2ae70SRajeshwari Shinde /**
438a9d2ae70SRajeshwari Shinde  * Find the I2C bus number by given a FDT I2C node.
439a9d2ae70SRajeshwari Shinde  *
440a9d2ae70SRajeshwari Shinde  * @param blob  Device tree blbo
441a9d2ae70SRajeshwari Shinde  * @param node  FDT I2C node to find
442a9d2ae70SRajeshwari Shinde  * @return the number of I2C bus (zero based), or -1 on error
443a9d2ae70SRajeshwari Shinde  */
444a9d2ae70SRajeshwari Shinde int i2c_get_bus_num_fdt(int node);
445a9d2ae70SRajeshwari Shinde 
446a9d2ae70SRajeshwari Shinde /**
447a9d2ae70SRajeshwari Shinde  * Reset the I2C bus represented by the given a FDT I2C node.
448a9d2ae70SRajeshwari Shinde  *
449a9d2ae70SRajeshwari Shinde  * @param blob  Device tree blbo
450a9d2ae70SRajeshwari Shinde  * @param node  FDT I2C node to find
451a9d2ae70SRajeshwari Shinde  * @return 0 if port was reset, -1 if not found
452a9d2ae70SRajeshwari Shinde  */
453a9d2ae70SRajeshwari Shinde int i2c_reset_port_fdt(const void *blob, int node);
4541f045217Swdenk #endif	/* _I2C_H_ */
455