blob: 1d792db454a1c2ec6e056dc20794f93c01189e14 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenk1f045212002-03-10 14:37:15 +00002/*
Heiko Schocher385c9ef2012-01-16 21:12:23 +00003 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
4 * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de>
5 * Changes for multibus/multiadapter I2C support.
6 *
wdenk1f045212002-03-10 14:37:15 +00007 * (C) Copyright 2001
8 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
9 *
wdenk1f045212002-03-10 14:37:15 +000010 * The original I2C interface was
11 * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
12 * AIRVENT SAM s.p.a - RIMINI(ITALY)
13 * but has been changed substantially.
14 */
15
16#ifndef _I2C_H_
17#define _I2C_H_
18
Simon Glassf7ae49f2020-05-10 11:40:05 -060019#include <linker_lists.h>
20
wdenk1f045212002-03-10 14:37:15 +000021/*
Simon Glassc6202d82014-12-10 08:55:47 -070022 * For now there are essentially two parts to this file - driver model
23 * here at the top, and the older code below (with CONFIG_SYS_I2C being
24 * most recent). The plan is to migrate everything to driver model.
25 * The driver model structures and API are separate as they are different
26 * enough as to be incompatible for compilation purposes.
27 */
28
Simon Glassc6202d82014-12-10 08:55:47 -070029enum dm_i2c_chip_flags {
30 DM_I2C_CHIP_10BIT = 1 << 0, /* Use 10-bit addressing */
31 DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send address for each read byte */
32 DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address for each write byte */
33};
34
Simon Glass7bd21b62020-01-23 11:48:16 -070035/** enum i2c_speed_mode - standard I2C speed modes */
36enum i2c_speed_mode {
37 IC_SPEED_MODE_STANDARD,
38 IC_SPEED_MODE_FAST,
39 IC_SPEED_MODE_FAST_PLUS,
40 IC_SPEED_MODE_HIGH,
41 IC_SPEED_MODE_FAST_ULTRA,
42
43 IC_SPEED_MODE_COUNT,
44};
45
46/** enum i2c_speed_rate - standard I2C speeds in Hz */
47enum i2c_speed_rate {
48 I2C_SPEED_STANDARD_RATE = 100000,
49 I2C_SPEED_FAST_RATE = 400000,
50 I2C_SPEED_FAST_PLUS_RATE = 1000000,
51 I2C_SPEED_HIGH_RATE = 3400000,
52 I2C_SPEED_FAST_ULTRA_RATE = 5000000,
53};
54
55/** enum i2c_address_mode - available address modes */
56enum i2c_address_mode {
57 I2C_MODE_7_BIT,
58 I2C_MODE_10_BIT
59};
60
Simon Glassfffff722015-02-05 21:41:33 -070061struct udevice;
Simon Glassc6202d82014-12-10 08:55:47 -070062/**
63 * struct dm_i2c_chip - information about an i2c chip
64 *
65 * An I2C chip is a device on the I2C bus. It sits at a particular address
66 * and normally supports 7-bit or 10-bit addressing.
67 *
Simon Glasse6f66ec2015-01-25 08:27:13 -070068 * To obtain this structure, use dev_get_parent_platdata(dev) where dev is
69 * the chip to examine.
Simon Glassc6202d82014-12-10 08:55:47 -070070 *
71 * @chip_addr: Chip address on bus
72 * @offset_len: Length of offset in bytes. A single byte offset can
73 * represent up to 256 bytes. A value larger than 1 may be
74 * needed for larger devices.
75 * @flags: Flags for this chip (dm_i2c_chip_flags)
Robert Beckett85968522019-10-28 17:44:57 +000076 * @chip_addr_offset_mask: Mask of offset bits within chip_addr. Used for
77 * devices which steal addresses as part of offset.
78 * If offset_len is zero, then the offset is encoded
79 * completely within the chip address itself.
80 * e.g. a devce with chip address of 0x2c with 512
81 * registers might use the bottom bit of the address
82 * to indicate which half of the address space is being
83 * accessed while still only using 1 byte offset.
84 * This means it will respond to chip address 0x2c and
85 * 0x2d.
86 * A real world example is the Atmel AT24C04. It's
87 * datasheet explains it's usage of this addressing
88 * mode.
Simon Glassc6202d82014-12-10 08:55:47 -070089 * @emul: Emulator for this chip address (only used for emulation)
90 */
91struct dm_i2c_chip {
92 uint chip_addr;
93 uint offset_len;
94 uint flags;
Robert Beckett85968522019-10-28 17:44:57 +000095 uint chip_addr_offset_mask;
Simon Glassc6202d82014-12-10 08:55:47 -070096#ifdef CONFIG_SANDBOX
97 struct udevice *emul;
Simon Glass182bf922015-04-20 12:37:15 -060098 bool test_mode;
Simon Glassc6202d82014-12-10 08:55:47 -070099#endif
100};
101
102/**
103 * struct dm_i2c_bus- information about an i2c bus
104 *
105 * An I2C bus contains 0 or more chips on it, each at its own address. The
106 * bus can operate at different speeds (measured in Hz, typically 100KHz
107 * or 400KHz).
108 *
Simon Glasse564f052015-03-05 12:25:20 -0700109 * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the
110 * I2C bus udevice.
Simon Glassc6202d82014-12-10 08:55:47 -0700111 *
112 * @speed_hz: Bus speed in hertz (typically 100000)
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200113 * @max_transaction_bytes: Maximal size of single I2C transfer
Simon Glassc6202d82014-12-10 08:55:47 -0700114 */
115struct dm_i2c_bus {
116 int speed_hz;
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200117 int max_transaction_bytes;
Simon Glassc6202d82014-12-10 08:55:47 -0700118};
119
Simon Glass7fc65bc2015-07-02 18:15:41 -0600120/*
121 * Not all of these flags are implemented in the U-Boot API
122 */
123enum dm_i2c_msg_flags {
124 I2C_M_TEN = 0x0010, /* ten-bit chip address */
125 I2C_M_RD = 0x0001, /* read data, from slave to master */
126 I2C_M_STOP = 0x8000, /* send stop after this message */
127 I2C_M_NOSTART = 0x4000, /* no start before this message */
128 I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */
129 I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */
130 I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */
131 I2C_M_RECV_LEN = 0x0400, /* length is first received byte */
132};
133
134/**
135 * struct i2c_msg - an I2C message
136 *
137 * @addr: Slave address
138 * @flags: Flags (see enum dm_i2c_msg_flags)
139 * @len: Length of buffer in bytes, may be 0 for a probe
140 * @buf: Buffer to send/receive, or NULL if no data
141 */
142struct i2c_msg {
143 uint addr;
144 uint flags;
145 uint len;
146 u8 *buf;
147};
148
149/**
150 * struct i2c_msg_list - a list of I2C messages
151 *
152 * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
153 * appropriate in U-Boot.
154 *
155 * @msg: Pointer to i2c_msg array
156 * @nmsgs: Number of elements in the array
157 */
158struct i2c_msg_list {
159 struct i2c_msg *msgs;
160 uint nmsgs;
161};
162
Simon Glassc6202d82014-12-10 08:55:47 -0700163/**
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700164 * dm_i2c_read() - read bytes from an I2C chip
Simon Glassc6202d82014-12-10 08:55:47 -0700165 *
166 * To obtain an I2C device (called a 'chip') given the I2C bus address you
167 * can use i2c_get_chip(). To obtain a bus by bus number use
168 * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
169 *
170 * To set the address length of a devce use i2c_set_addr_len(). It
171 * defaults to 1.
172 *
173 * @dev: Chip to read from
174 * @offset: Offset within chip to start reading
175 * @buffer: Place to put data
176 * @len: Number of bytes to read
177 *
178 * @return 0 on success, -ve on failure
179 */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700180int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
Simon Glassc6202d82014-12-10 08:55:47 -0700181
182/**
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700183 * dm_i2c_write() - write bytes to an I2C chip
Simon Glassc6202d82014-12-10 08:55:47 -0700184 *
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700185 * See notes for dm_i2c_read() above.
Simon Glassc6202d82014-12-10 08:55:47 -0700186 *
187 * @dev: Chip to write to
188 * @offset: Offset within chip to start writing
189 * @buffer: Buffer containing data to write
190 * @len: Number of bytes to write
191 *
192 * @return 0 on success, -ve on failure
193 */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700194int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
195 int len);
Simon Glassc6202d82014-12-10 08:55:47 -0700196
197/**
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700198 * dm_i2c_probe() - probe a particular chip address
Simon Glassc6202d82014-12-10 08:55:47 -0700199 *
200 * This can be useful to check for the existence of a chip on the bus.
201 * It is typically implemented by writing the chip address to the bus
202 * and checking that the chip replies with an ACK.
203 *
204 * @bus: Bus to probe
205 * @chip_addr: 7-bit address to probe (10-bit and others are not supported)
206 * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags)
207 * @devp: Returns the device found, or NULL if none
208 * @return 0 if a chip was found at that address, -ve if not
209 */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700210int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
211 struct udevice **devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700212
213/**
Simon Glassba3864f2015-04-20 12:37:14 -0600214 * dm_i2c_reg_read() - Read a value from an I2C register
215 *
216 * This reads a single value from the given address in an I2C chip
217 *
Simon Glass25a0fb42015-07-02 18:15:40 -0600218 * @dev: Device to use for transfer
Simon Glassba3864f2015-04-20 12:37:14 -0600219 * @addr: Address to read from
220 * @return value read, or -ve on error
221 */
222int dm_i2c_reg_read(struct udevice *dev, uint offset);
223
224/**
225 * dm_i2c_reg_write() - Write a value to an I2C register
226 *
227 * This writes a single value to the given address in an I2C chip
228 *
Simon Glass25a0fb42015-07-02 18:15:40 -0600229 * @dev: Device to use for transfer
Simon Glassba3864f2015-04-20 12:37:14 -0600230 * @addr: Address to write to
231 * @val: Value to write (normally a byte)
232 * @return 0 on success, -ve on error
233 */
234int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val);
235
236/**
Simon Glassdf358c62015-07-02 18:15:42 -0600237 * dm_i2c_xfer() - Transfer messages over I2C
238 *
239 * This transfers a raw message. It is best to use dm_i2c_reg_read/write()
240 * instead.
241 *
242 * @dev: Device to use for transfer
243 * @msg: List of messages to transfer
244 * @nmsgs: Number of messages to transfer
245 * @return 0 on success, -ve on error
246 */
247int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs);
248
249/**
Simon Glassca88b9b2015-02-05 21:41:32 -0700250 * dm_i2c_set_bus_speed() - set the speed of a bus
Simon Glassc6202d82014-12-10 08:55:47 -0700251 *
252 * @bus: Bus to adjust
253 * @speed: Requested speed in Hz
254 * @return 0 if OK, -EINVAL for invalid values
255 */
Simon Glassca88b9b2015-02-05 21:41:32 -0700256int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
Simon Glassc6202d82014-12-10 08:55:47 -0700257
258/**
Simon Glassca88b9b2015-02-05 21:41:32 -0700259 * dm_i2c_get_bus_speed() - get the speed of a bus
Simon Glassc6202d82014-12-10 08:55:47 -0700260 *
261 * @bus: Bus to check
262 * @return speed of selected I2C bus in Hz, -ve on error
263 */
Simon Glassca88b9b2015-02-05 21:41:32 -0700264int dm_i2c_get_bus_speed(struct udevice *bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700265
266/**
267 * i2c_set_chip_flags() - set flags for a chip
268 *
269 * Typically addresses are 7 bits, but for 10-bit addresses you should set
270 * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
271 *
272 * @dev: Chip to adjust
273 * @flags: New flags
274 * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error
275 */
276int i2c_set_chip_flags(struct udevice *dev, uint flags);
277
278/**
279 * i2c_get_chip_flags() - get flags for a chip
280 *
281 * @dev: Chip to check
282 * @flagsp: Place to put flags
283 * @return 0 if OK, other -ve value on error
284 */
285int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
286
287/**
288 * i2c_set_offset_len() - set the offset length for a chip
289 *
290 * The offset used to access a chip may be up to 4 bytes long. Typically it
291 * is only 1 byte, which is enough for chips with 256 bytes of memory or
292 * registers. The default value is 1, but you can call this function to
293 * change it.
294 *
295 * @offset_len: New offset length value (typically 1 or 2)
296 */
Simon Glassc6202d82014-12-10 08:55:47 -0700297int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
Simon Glass01501802015-05-04 11:30:58 -0600298
299/**
300 * i2c_get_offset_len() - get the offset length for a chip
301 *
302 * @return: Current offset length value (typically 1 or 2)
303 */
304int i2c_get_chip_offset_len(struct udevice *dev);
305
Simon Glassc6202d82014-12-10 08:55:47 -0700306/**
Robert Beckett85968522019-10-28 17:44:57 +0000307 * i2c_set_chip_addr_offset_mask() - set mask of address bits usable by offset
308 *
309 * Some devices listen on multiple chip addresses to achieve larger offsets
310 * than their single or multiple byte offsets would allow for. You can use this
311 * function to set the bits that are valid to be used for offset overflow.
312 *
313 * @mask: The mask to be used for high offset bits within address
314 * @return 0 if OK, other -ve value on error
315 */
316int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask);
317
318/*
319 * i2c_get_chip_addr_offset_mask() - get mask of address bits usable by offset
320 *
321 * @return current chip addr offset mask
322 */
323uint i2c_get_chip_addr_offset_mask(struct udevice *dev);
324
325/**
Simon Glassc6202d82014-12-10 08:55:47 -0700326 * i2c_deblock() - recover a bus that is in an unknown state
327 *
328 * See the deblock() method in 'struct dm_i2c_ops' for full information
329 *
330 * @bus: Bus to recover
331 * @return 0 if OK, -ve on error
332 */
333int i2c_deblock(struct udevice *bus);
334
Simon Glassc6202d82014-12-10 08:55:47 -0700335/**
Marek Vasut72315222020-02-07 16:57:50 +0100336 * i2c_deblock_gpio_loop() - recover a bus from an unknown state by toggling SDA/SCL
337 *
338 * This is the inner logic used for toggling I2C SDA/SCL lines as GPIOs
339 * for deblocking the I2C bus.
340 *
341 * @sda_pin: SDA GPIO
342 * @scl_pin: SCL GPIO
343 * @scl_count: Number of SCL clock cycles generated to deblock SDA
Marek Vasuta1917282020-02-07 16:57:51 +0100344 * @start_count:Number of I2C start conditions sent after deblocking SDA
Marek Vasut72315222020-02-07 16:57:50 +0100345 * @delay: Delay between SCL clock line changes
346 * @return 0 if OK, -ve on error
347 */
348struct gpio_desc;
349int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, struct gpio_desc *scl_pin,
Marek Vasuta1917282020-02-07 16:57:51 +0100350 unsigned int scl_count, unsigned int start_count,
351 unsigned int delay);
Marek Vasut72315222020-02-07 16:57:50 +0100352
353/**
Simon Glassc6202d82014-12-10 08:55:47 -0700354 * struct dm_i2c_ops - driver operations for I2C uclass
355 *
356 * Drivers should support these operations unless otherwise noted. These
357 * operations are intended to be used by uclass code, not directly from
358 * other code.
359 */
360struct dm_i2c_ops {
361 /**
362 * xfer() - transfer a list of I2C messages
363 *
364 * @bus: Bus to read from
365 * @msg: List of messages to transfer
366 * @nmsgs: Number of messages in the list
367 * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
368 * -ECOMM if the speed cannot be supported, -EPROTO if the chip
369 * flags cannot be supported, other -ve value on some other error
370 */
371 int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
372
373 /**
374 * probe_chip() - probe for the presense of a chip address
375 *
376 * This function is optional. If omitted, the uclass will send a zero
377 * length message instead.
378 *
379 * @bus: Bus to probe
380 * @chip_addr: Chip address to probe
381 * @chip_flags: Probe flags (enum dm_i2c_chip_flags)
382 * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
383 * to default probem other -ve value on error
384 */
385 int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
386
387 /**
388 * set_bus_speed() - set the speed of a bus (optional)
389 *
390 * The bus speed value will be updated by the uclass if this function
391 * does not return an error. This method is optional - if it is not
392 * provided then the driver can read the speed from
Simon Glasse564f052015-03-05 12:25:20 -0700393 * dev_get_uclass_priv(bus)->speed_hz
Simon Glassc6202d82014-12-10 08:55:47 -0700394 *
395 * @bus: Bus to adjust
396 * @speed: Requested speed in Hz
397 * @return 0 if OK, -EINVAL for invalid values
398 */
399 int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
400
401 /**
402 * get_bus_speed() - get the speed of a bus (optional)
403 *
404 * Normally this can be provided by the uclass, but if you want your
405 * driver to check the bus speed by looking at the hardware, you can
406 * implement that here. This method is optional. This method would
Simon Glasse564f052015-03-05 12:25:20 -0700407 * normally be expected to return dev_get_uclass_priv(bus)->speed_hz.
Simon Glassc6202d82014-12-10 08:55:47 -0700408 *
409 * @bus: Bus to check
410 * @return speed of selected I2C bus in Hz, -ve on error
411 */
412 int (*get_bus_speed)(struct udevice *bus);
413
414 /**
415 * set_flags() - set the flags for a chip (optional)
416 *
417 * This is generally implemented by the uclass, but drivers can
418 * check the value to ensure that unsupported options are not used.
419 * This method is optional. If provided, this method will always be
420 * called when the flags change.
421 *
422 * @dev: Chip to adjust
423 * @flags: New flags value
424 * @return 0 if OK, -EINVAL if value is unsupported
425 */
426 int (*set_flags)(struct udevice *dev, uint flags);
427
428 /**
429 * deblock() - recover a bus that is in an unknown state
430 *
431 * I2C is a synchronous protocol and resets of the processor in the
432 * middle of an access can block the I2C Bus until a powerdown of
433 * the full unit is done. This is because slaves can be stuck
434 * waiting for addition bus transitions for a transaction that will
435 * never complete. Resetting the I2C master does not help. The only
436 * way is to force the bus through a series of transitions to make
437 * sure that all slaves are done with the transaction. This method
438 * performs this 'deblocking' if support by the driver.
439 *
440 * This method is optional.
441 */
442 int (*deblock)(struct udevice *bus);
443};
444
445#define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops)
446
447/**
Simon Glass3d1957f2015-08-03 08:19:21 -0600448 * struct i2c_mux_ops - operations for an I2C mux
449 *
450 * The current mux state is expected to be stored in the mux itself since
451 * it is the only thing that knows how to make things work. The mux can
452 * record the current state and then avoid switching unless it is necessary.
453 * So select() can be skipped if the mux is already in the correct state.
454 * Also deselect() can be made a nop if required.
455 */
456struct i2c_mux_ops {
457 /**
458 * select() - select one of of I2C buses attached to a mux
459 *
460 * This will be called when there is no bus currently selected by the
461 * mux. This method does not need to deselect the old bus since
462 * deselect() will be already have been called if necessary.
463 *
464 * @mux: Mux device
465 * @bus: I2C bus to select
466 * @channel: Channel number correponding to the bus to select
467 * @return 0 if OK, -ve on error
468 */
469 int (*select)(struct udevice *mux, struct udevice *bus, uint channel);
470
471 /**
472 * deselect() - select one of of I2C buses attached to a mux
473 *
474 * This is used to deselect the currently selected I2C bus.
475 *
476 * @mux: Mux device
477 * @bus: I2C bus to deselect
478 * @channel: Channel number correponding to the bus to deselect
479 * @return 0 if OK, -ve on error
480 */
481 int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel);
482};
483
484#define i2c_mux_get_ops(dev) ((struct i2c_mux_ops *)(dev)->driver->ops)
485
486/**
Simon Glassc6202d82014-12-10 08:55:47 -0700487 * i2c_get_chip() - get a device to use to access a chip on a bus
488 *
489 * This returns the device for the given chip address. The device can then
490 * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
491 *
492 * @bus: Bus to examine
493 * @chip_addr: Chip address for the new device
Simon Glass25ab4b02015-01-25 08:26:55 -0700494 * @offset_len: Length of a register offset in bytes (normally 1)
Simon Glassc6202d82014-12-10 08:55:47 -0700495 * @devp: Returns pointer to new device if found or -ENODEV if not
496 * found
497 */
Simon Glass25ab4b02015-01-25 08:26:55 -0700498int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
499 struct udevice **devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700500
501/**
Stefan Roesea06728c2015-11-25 07:41:58 +0100502 * i2c_get_chip_for_busnum() - get a device to use to access a chip on
503 * a bus number
Simon Glassc6202d82014-12-10 08:55:47 -0700504 *
505 * This returns the device for the given chip address on a particular bus
506 * number.
507 *
508 * @busnum: Bus number to examine
509 * @chip_addr: Chip address for the new device
Simon Glass25ab4b02015-01-25 08:26:55 -0700510 * @offset_len: Length of a register offset in bytes (normally 1)
Simon Glassc6202d82014-12-10 08:55:47 -0700511 * @devp: Returns pointer to new device if found or -ENODEV if not
512 * found
513 */
Simon Glass25ab4b02015-01-25 08:26:55 -0700514int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
515 struct udevice **devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700516
517/**
518 * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data
519 *
520 * This decodes the chip address from a device tree node and puts it into
521 * its dm_i2c_chip structure. This should be called in your driver's
522 * ofdata_to_platdata() method.
523 *
524 * @blob: Device tree blob
525 * @node: Node offset to read from
526 * @spi: Place to put the decoded information
527 */
Simon Glass17043082017-05-18 20:09:30 -0600528int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip);
Simon Glassc6202d82014-12-10 08:55:47 -0700529
Simon Glass7d7db222015-07-02 18:15:39 -0600530/**
531 * i2c_dump_msgs() - Dump a list of I2C messages
532 *
533 * This may be useful for debugging.
534 *
535 * @msg: Message list to dump
536 * @nmsgs: Number of messages
537 */
538void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs);
539
Simon Glassb7c25b12018-11-18 08:14:33 -0700540/**
541 * i2c_emul_find() - Find an emulator for an i2c sandbox device
542 *
543 * This looks at the device's 'emul' phandle
544 *
545 * @dev: Device to find an emulator for
546 * @emulp: Returns the associated emulator, if found *
547 * @return 0 if OK, -ENOENT or -ENODEV if not found
548 */
549int i2c_emul_find(struct udevice *dev, struct udevice **emulp);
550
551/**
552 * i2c_emul_get_device() - Find the device being emulated
553 *
554 * Given an emulator this returns the associated device
555 *
556 * @emul: Emulator for the device
557 * @return device that @emul is emulating
558 */
559struct udevice *i2c_emul_get_device(struct udevice *emul);
560
Simon Glassc6202d82014-12-10 08:55:47 -0700561#ifndef CONFIG_DM_I2C
562
563/*
wdenk1f045212002-03-10 14:37:15 +0000564 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
565 *
566 * The implementation MUST NOT use static or global variables if the
567 * I2C routines are used to read SDRAM configuration information
568 * because this is done before the memories are initialized. Limited
569 * use of stack-based variables are OK (the initial stack size is
570 * limited).
571 *
572 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
573 */
574
575/*
576 * Configuration items.
577 */
578#define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
579
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000580#if !defined(CONFIG_SYS_I2C_MAX_HOPS)
581/* no muxes used bus = i2c adapters */
582#define CONFIG_SYS_I2C_DIRECT_BUS 1
583#define CONFIG_SYS_I2C_MAX_HOPS 0
584#define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100585#else
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000586/* we use i2c muxes */
587#undef CONFIG_SYS_I2C_DIRECT_BUS
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100588#endif
589
Stefan Roese8c120452007-03-01 07:03:25 +0100590/* define the I2C bus number for RTC and DTT if not already done */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200591#if !defined(CONFIG_SYS_RTC_BUS_NUM)
592#define CONFIG_SYS_RTC_BUS_NUM 0
Stefan Roese8c120452007-03-01 07:03:25 +0100593#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200594#if !defined(CONFIG_SYS_SPD_BUS_NUM)
595#define CONFIG_SYS_SPD_BUS_NUM 0
Matthias Fuchsd8a8ea52007-03-08 16:20:32 +0100596#endif
Stefan Roese8c120452007-03-01 07:03:25 +0100597
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000598struct i2c_adapter {
599 void (*init)(struct i2c_adapter *adap, int speed,
600 int slaveaddr);
601 int (*probe)(struct i2c_adapter *adap, uint8_t chip);
602 int (*read)(struct i2c_adapter *adap, uint8_t chip,
603 uint addr, int alen, uint8_t *buffer,
604 int len);
605 int (*write)(struct i2c_adapter *adap, uint8_t chip,
606 uint addr, int alen, uint8_t *buffer,
607 int len);
608 uint (*set_bus_speed)(struct i2c_adapter *adap,
609 uint speed);
610 int speed;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100611 int waitdelay;
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000612 int slaveaddr;
613 int init_done;
614 int hwadapnr;
615 char *name;
616};
617
618#define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
619 _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
620 { \
621 .init = _init, \
622 .probe = _probe, \
623 .read = _read, \
624 .write = _write, \
625 .set_bus_speed = _set_speed, \
626 .speed = _speed, \
627 .slaveaddr = _slaveaddr, \
628 .init_done = 0, \
629 .hwadapnr = _hwadapnr, \
630 .name = #_name \
631};
632
633#define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
634 _set_speed, _speed, _slaveaddr, _hwadapnr) \
635 ll_entry_declare(struct i2c_adapter, _name, i2c) = \
636 U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
637 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
638
639struct i2c_adapter *i2c_get_adapter(int index);
640
641#ifndef CONFIG_SYS_I2C_DIRECT_BUS
642struct i2c_mux {
643 int id;
644 char name[16];
645};
646
647struct i2c_next_hop {
648 struct i2c_mux mux;
649 uint8_t chip;
650 uint8_t channel;
651};
652
653struct i2c_bus_hose {
654 int adapter;
655 struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
656};
657#define I2C_NULL_HOP {{-1, ""}, 0, 0}
658extern struct i2c_bus_hose i2c_bus[];
659
660#define I2C_ADAPTER(bus) i2c_bus[bus].adapter
661#else
662#define I2C_ADAPTER(bus) bus
663#endif
664#define I2C_BUS gd->cur_i2c_bus
665
666#define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
667#define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
668#define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
669
670#ifndef CONFIG_SYS_I2C_DIRECT_BUS
671#define I2C_MUX_PCA9540_ID 1
672#define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
673#define I2C_MUX_PCA9542_ID 2
674#define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
675#define I2C_MUX_PCA9544_ID 3
676#define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
677#define I2C_MUX_PCA9547_ID 4
678#define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
Michael Burre6658742013-09-23 22:35:45 +0000679#define I2C_MUX_PCA9548_ID 5
680#define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000681#endif
682
Heiko Schocher98aed372008-10-15 09:35:26 +0200683#ifndef I2C_SOFT_DECLARATIONS
Heiko Schocher2eb48ff2017-06-07 17:33:10 +0200684# if (defined(CONFIG_AT91RM9200) || \
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100685 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
Andreas Bießmanncb96a0a2013-10-30 15:18:18 +0100686 defined(CONFIG_AT91SAM9263))
esw@bus-elektronik.de78132272011-12-20 06:05:30 +0000687# define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
Heiko Schocher98aed372008-10-15 09:35:26 +0200688# else
689# define I2C_SOFT_DECLARATIONS
690# endif
691#endif
Timur Tabiecf5f072008-12-03 11:28:30 -0600692
Peter Tyser9c90a2c2009-04-24 15:34:05 -0500693/*
694 * Many boards/controllers/drivers don't support an I2C slave interface so
695 * provide a default slave address for them for use in common code. A real
696 * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
697 * support a slave interface.
698 */
699#ifndef CONFIG_SYS_I2C_SLAVE
700#define CONFIG_SYS_I2C_SLAVE 0xfe
Timur Tabiecf5f072008-12-03 11:28:30 -0600701#endif
702
wdenk1f045212002-03-10 14:37:15 +0000703/*
704 * Initialization, must be called once on start up, may be called
705 * repeatedly to change the speed and slave addresses.
706 */
Yuan Yao9d10c2d2016-06-08 18:24:51 +0800707#ifdef CONFIG_SYS_I2C_EARLY_INIT
708void i2c_early_init_f(void);
709#endif
wdenk1f045212002-03-10 14:37:15 +0000710void i2c_init(int speed, int slaveaddr);
wdenk06d01db2003-03-14 20:47:52 +0000711void i2c_init_board(void);
wdenk1f045212002-03-10 14:37:15 +0000712
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000713#ifdef CONFIG_SYS_I2C
714/*
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000715 * i2c_get_bus_num:
716 *
717 * Returns index of currently active I2C bus. Zero-based.
718 */
719unsigned int i2c_get_bus_num(void);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200720
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000721/*
722 * i2c_set_bus_num:
723 *
724 * Change the active I2C bus. Subsequent read/write calls will
725 * go to this one.
726 *
727 * bus - bus index, zero based
728 *
729 * Returns: 0 on success, not 0 on failure
730 *
731 */
732int i2c_set_bus_num(unsigned int bus);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200733
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000734/*
735 * i2c_init_all():
736 *
737 * Initializes all I2C adapters in the system. All i2c_adap structures must
738 * be initialized beforehead with function pointers and data, including
739 * speed and slaveaddr. Returns 0 on success, non-0 on failure.
740 */
741void i2c_init_all(void);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200742
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000743/*
744 * Probe the given I2C chip address. Returns 0 if a chip responded,
745 * not 0 on failure.
746 */
747int i2c_probe(uint8_t chip);
748
749/*
750 * Read/Write interface:
751 * chip: I2C chip address, range 0..127
752 * addr: Memory (register) address within the chip
753 * alen: Number of bytes to use for addr (typically 1, 2 for larger
754 * memories, 0 for register type devices with only one
755 * register)
756 * buffer: Where to read/write the data
757 * len: How many bytes to read/write
758 *
759 * Returns: 0 on success, not 0 on failure
760 */
761int i2c_read(uint8_t chip, unsigned int addr, int alen,
762 uint8_t *buffer, int len);
763
764int i2c_write(uint8_t chip, unsigned int addr, int alen,
765 uint8_t *buffer, int len);
766
767/*
768 * Utility routines to read/write registers.
769 */
770uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
771
772void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
773
774/*
775 * i2c_set_bus_speed:
776 *
777 * Change the speed of the active I2C bus
778 *
779 * speed - bus speed in Hz
780 *
781 * Returns: new bus speed
782 *
783 */
784unsigned int i2c_set_bus_speed(unsigned int speed);
785
786/*
787 * i2c_get_bus_speed:
788 *
789 * Returns speed of currently active I2C bus in Hz
790 */
791
792unsigned int i2c_get_bus_speed(void);
793
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000794#else
Heiko Schocher67b23a32008-10-15 09:39:47 +0200795
wdenk1f045212002-03-10 14:37:15 +0000796/*
797 * Probe the given I2C chip address. Returns 0 if a chip responded,
798 * not 0 on failure.
799 */
800int i2c_probe(uchar chip);
801
802/*
803 * Read/Write interface:
804 * chip: I2C chip address, range 0..127
805 * addr: Memory (register) address within the chip
806 * alen: Number of bytes to use for addr (typically 1, 2 for larger
807 * memories, 0 for register type devices with only one
808 * register)
809 * buffer: Where to read/write the data
810 * len: How many bytes to read/write
811 *
812 * Returns: 0 on success, not 0 on failure
813 */
814int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
815int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
816
817/*
818 * Utility routines to read/write registers.
819 */
Timur Tabiecf5f072008-12-03 11:28:30 -0600820static inline u8 i2c_reg_read(u8 addr, u8 reg)
821{
822 u8 buf;
823
Timur Tabiecf5f072008-12-03 11:28:30 -0600824#ifdef DEBUG
825 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
826#endif
827
Timur Tabiecf5f072008-12-03 11:28:30 -0600828 i2c_read(addr, reg, 1, &buf, 1);
Timur Tabiecf5f072008-12-03 11:28:30 -0600829
830 return buf;
831}
832
833static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
834{
Timur Tabiecf5f072008-12-03 11:28:30 -0600835#ifdef DEBUG
836 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
837 __func__, addr, reg, val);
838#endif
839
Timur Tabiecf5f072008-12-03 11:28:30 -0600840 i2c_write(addr, reg, 1, &val, 1);
Timur Tabiecf5f072008-12-03 11:28:30 -0600841}
wdenk1f045212002-03-10 14:37:15 +0000842
Ben Warrenbb99ad62006-09-07 16:50:54 -0400843/*
844 * Functions for setting the current I2C bus and its speed
845 */
846
847/*
848 * i2c_set_bus_num:
849 *
850 * Change the active I2C bus. Subsequent read/write calls will
851 * go to this one.
852 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200853 * bus - bus index, zero based
Ben Warrenbb99ad62006-09-07 16:50:54 -0400854 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200855 * Returns: 0 on success, not 0 on failure
Ben Warrenbb99ad62006-09-07 16:50:54 -0400856 *
857 */
Timur Tabi9ca880a2006-10-31 21:23:16 -0600858int i2c_set_bus_num(unsigned int bus);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400859
860/*
861 * i2c_get_bus_num:
862 *
863 * Returns index of currently active I2C bus. Zero-based.
864 */
865
Timur Tabi9ca880a2006-10-31 21:23:16 -0600866unsigned int i2c_get_bus_num(void);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400867
868/*
869 * i2c_set_bus_speed:
870 *
871 * Change the speed of the active I2C bus
872 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200873 * speed - bus speed in Hz
Ben Warrenbb99ad62006-09-07 16:50:54 -0400874 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200875 * Returns: 0 on success, not 0 on failure
Ben Warrenbb99ad62006-09-07 16:50:54 -0400876 *
877 */
Timur Tabi9ca880a2006-10-31 21:23:16 -0600878int i2c_set_bus_speed(unsigned int);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400879
880/*
881 * i2c_get_bus_speed:
882 *
883 * Returns speed of currently active I2C bus in Hz
884 */
885
Timur Tabi9ca880a2006-10-31 21:23:16 -0600886unsigned int i2c_get_bus_speed(void);
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000887#endif /* CONFIG_SYS_I2C */
888
889/*
890 * only for backwardcompatibility, should go away if we switched
891 * completely to new multibus support.
892 */
893#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
894# if !defined(CONFIG_SYS_MAX_I2C_BUS)
895# define CONFIG_SYS_MAX_I2C_BUS 2
896# endif
Łukasz Majewskiea0f73a2013-08-16 15:31:45 +0200897# define I2C_MULTI_BUS 1
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000898#else
899# define CONFIG_SYS_MAX_I2C_BUS 1
900# define I2C_MULTI_BUS 0
901#endif
Ben Warrenbb99ad62006-09-07 16:50:54 -0400902
Marek Vasutcd7b4e82011-10-25 11:40:57 +0200903/* NOTE: These two functions MUST be always_inline to avoid code growth! */
904static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
905static inline unsigned int I2C_GET_BUS(void)
906{
907 return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
908}
909
910static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
911static inline void I2C_SET_BUS(unsigned int bus)
912{
913 if (I2C_MULTI_BUS)
914 i2c_set_bus_num(bus);
915}
916
Łukasz Majewski7ca8f732012-09-04 23:15:20 +0000917/* Multi I2C definitions */
918enum {
919 I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
920 I2C_8, I2C_9, I2C_10,
921};
922
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000923/**
924 * Get FDT values for i2c bus.
925 *
926 * @param blob Device tree blbo
927 * @return the number of I2C bus
928 */
929void board_i2c_init(const void *blob);
930
931/**
932 * Find the I2C bus number by given a FDT I2C node.
933 *
934 * @param blob Device tree blbo
935 * @param node FDT I2C node to find
936 * @return the number of I2C bus (zero based), or -1 on error
937 */
938int i2c_get_bus_num_fdt(int node);
939
940/**
941 * Reset the I2C bus represented by the given a FDT I2C node.
942 *
943 * @param blob Device tree blbo
944 * @param node FDT I2C node to find
945 * @return 0 if port was reset, -1 if not found
946 */
947int i2c_reset_port_fdt(const void *blob, int node);
Simon Glassc6202d82014-12-10 08:55:47 -0700948
949#endif /* !CONFIG_DM_I2C */
950
wdenk1f045212002-03-10 14:37:15 +0000951#endif /* _I2C_H_ */