blob: 059200115a8e030958350e439c3dc0f37e38c0dd [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
19/*
Simon Glassc6202d82014-12-10 08:55:47 -070020 * For now there are essentially two parts to this file - driver model
21 * here at the top, and the older code below (with CONFIG_SYS_I2C being
22 * most recent). The plan is to migrate everything to driver model.
23 * The driver model structures and API are separate as they are different
24 * enough as to be incompatible for compilation purposes.
25 */
26
Simon Glassc6202d82014-12-10 08:55:47 -070027enum dm_i2c_chip_flags {
28 DM_I2C_CHIP_10BIT = 1 << 0, /* Use 10-bit addressing */
29 DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send address for each read byte */
30 DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address for each write byte */
31};
32
Simon Glass7bd21b62020-01-23 11:48:16 -070033/** enum i2c_speed_mode - standard I2C speed modes */
34enum i2c_speed_mode {
35 IC_SPEED_MODE_STANDARD,
36 IC_SPEED_MODE_FAST,
37 IC_SPEED_MODE_FAST_PLUS,
38 IC_SPEED_MODE_HIGH,
39 IC_SPEED_MODE_FAST_ULTRA,
40
41 IC_SPEED_MODE_COUNT,
42};
43
44/** enum i2c_speed_rate - standard I2C speeds in Hz */
45enum i2c_speed_rate {
46 I2C_SPEED_STANDARD_RATE = 100000,
47 I2C_SPEED_FAST_RATE = 400000,
48 I2C_SPEED_FAST_PLUS_RATE = 1000000,
49 I2C_SPEED_HIGH_RATE = 3400000,
50 I2C_SPEED_FAST_ULTRA_RATE = 5000000,
51};
52
53/** enum i2c_address_mode - available address modes */
54enum i2c_address_mode {
55 I2C_MODE_7_BIT,
56 I2C_MODE_10_BIT
57};
58
Simon Glassfffff722015-02-05 21:41:33 -070059struct udevice;
Simon Glassc6202d82014-12-10 08:55:47 -070060/**
61 * struct dm_i2c_chip - information about an i2c chip
62 *
63 * An I2C chip is a device on the I2C bus. It sits at a particular address
64 * and normally supports 7-bit or 10-bit addressing.
65 *
Simon Glasse6f66ec2015-01-25 08:27:13 -070066 * To obtain this structure, use dev_get_parent_platdata(dev) where dev is
67 * the chip to examine.
Simon Glassc6202d82014-12-10 08:55:47 -070068 *
69 * @chip_addr: Chip address on bus
70 * @offset_len: Length of offset in bytes. A single byte offset can
71 * represent up to 256 bytes. A value larger than 1 may be
72 * needed for larger devices.
73 * @flags: Flags for this chip (dm_i2c_chip_flags)
Robert Beckett85968522019-10-28 17:44:57 +000074 * @chip_addr_offset_mask: Mask of offset bits within chip_addr. Used for
75 * devices which steal addresses as part of offset.
76 * If offset_len is zero, then the offset is encoded
77 * completely within the chip address itself.
78 * e.g. a devce with chip address of 0x2c with 512
79 * registers might use the bottom bit of the address
80 * to indicate which half of the address space is being
81 * accessed while still only using 1 byte offset.
82 * This means it will respond to chip address 0x2c and
83 * 0x2d.
84 * A real world example is the Atmel AT24C04. It's
85 * datasheet explains it's usage of this addressing
86 * mode.
Simon Glassc6202d82014-12-10 08:55:47 -070087 * @emul: Emulator for this chip address (only used for emulation)
88 */
89struct dm_i2c_chip {
90 uint chip_addr;
91 uint offset_len;
92 uint flags;
Robert Beckett85968522019-10-28 17:44:57 +000093 uint chip_addr_offset_mask;
Simon Glassc6202d82014-12-10 08:55:47 -070094#ifdef CONFIG_SANDBOX
95 struct udevice *emul;
Simon Glass182bf922015-04-20 12:37:15 -060096 bool test_mode;
Simon Glassc6202d82014-12-10 08:55:47 -070097#endif
98};
99
100/**
101 * struct dm_i2c_bus- information about an i2c bus
102 *
103 * An I2C bus contains 0 or more chips on it, each at its own address. The
104 * bus can operate at different speeds (measured in Hz, typically 100KHz
105 * or 400KHz).
106 *
Simon Glasse564f052015-03-05 12:25:20 -0700107 * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the
108 * I2C bus udevice.
Simon Glassc6202d82014-12-10 08:55:47 -0700109 *
110 * @speed_hz: Bus speed in hertz (typically 100000)
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200111 * @max_transaction_bytes: Maximal size of single I2C transfer
Simon Glassc6202d82014-12-10 08:55:47 -0700112 */
113struct dm_i2c_bus {
114 int speed_hz;
Lukasz Majewskia40fe212019-04-04 12:35:34 +0200115 int max_transaction_bytes;
Simon Glassc6202d82014-12-10 08:55:47 -0700116};
117
Simon Glass7fc65bc2015-07-02 18:15:41 -0600118/*
119 * Not all of these flags are implemented in the U-Boot API
120 */
121enum dm_i2c_msg_flags {
122 I2C_M_TEN = 0x0010, /* ten-bit chip address */
123 I2C_M_RD = 0x0001, /* read data, from slave to master */
124 I2C_M_STOP = 0x8000, /* send stop after this message */
125 I2C_M_NOSTART = 0x4000, /* no start before this message */
126 I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */
127 I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */
128 I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */
129 I2C_M_RECV_LEN = 0x0400, /* length is first received byte */
130};
131
132/**
133 * struct i2c_msg - an I2C message
134 *
135 * @addr: Slave address
136 * @flags: Flags (see enum dm_i2c_msg_flags)
137 * @len: Length of buffer in bytes, may be 0 for a probe
138 * @buf: Buffer to send/receive, or NULL if no data
139 */
140struct i2c_msg {
141 uint addr;
142 uint flags;
143 uint len;
144 u8 *buf;
145};
146
147/**
148 * struct i2c_msg_list - a list of I2C messages
149 *
150 * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
151 * appropriate in U-Boot.
152 *
153 * @msg: Pointer to i2c_msg array
154 * @nmsgs: Number of elements in the array
155 */
156struct i2c_msg_list {
157 struct i2c_msg *msgs;
158 uint nmsgs;
159};
160
Simon Glassc6202d82014-12-10 08:55:47 -0700161/**
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700162 * dm_i2c_read() - read bytes from an I2C chip
Simon Glassc6202d82014-12-10 08:55:47 -0700163 *
164 * To obtain an I2C device (called a 'chip') given the I2C bus address you
165 * can use i2c_get_chip(). To obtain a bus by bus number use
166 * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
167 *
168 * To set the address length of a devce use i2c_set_addr_len(). It
169 * defaults to 1.
170 *
171 * @dev: Chip to read from
172 * @offset: Offset within chip to start reading
173 * @buffer: Place to put data
174 * @len: Number of bytes to read
175 *
176 * @return 0 on success, -ve on failure
177 */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700178int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
Simon Glassc6202d82014-12-10 08:55:47 -0700179
180/**
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700181 * dm_i2c_write() - write bytes to an I2C chip
Simon Glassc6202d82014-12-10 08:55:47 -0700182 *
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700183 * See notes for dm_i2c_read() above.
Simon Glassc6202d82014-12-10 08:55:47 -0700184 *
185 * @dev: Chip to write to
186 * @offset: Offset within chip to start writing
187 * @buffer: Buffer containing data to write
188 * @len: Number of bytes to write
189 *
190 * @return 0 on success, -ve on failure
191 */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700192int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
193 int len);
Simon Glassc6202d82014-12-10 08:55:47 -0700194
195/**
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700196 * dm_i2c_probe() - probe a particular chip address
Simon Glassc6202d82014-12-10 08:55:47 -0700197 *
198 * This can be useful to check for the existence of a chip on the bus.
199 * It is typically implemented by writing the chip address to the bus
200 * and checking that the chip replies with an ACK.
201 *
202 * @bus: Bus to probe
203 * @chip_addr: 7-bit address to probe (10-bit and others are not supported)
204 * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags)
205 * @devp: Returns the device found, or NULL if none
206 * @return 0 if a chip was found at that address, -ve if not
207 */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700208int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
209 struct udevice **devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700210
211/**
Simon Glassba3864f2015-04-20 12:37:14 -0600212 * dm_i2c_reg_read() - Read a value from an I2C register
213 *
214 * This reads a single value from the given address in an I2C chip
215 *
Simon Glass25a0fb42015-07-02 18:15:40 -0600216 * @dev: Device to use for transfer
Simon Glassba3864f2015-04-20 12:37:14 -0600217 * @addr: Address to read from
218 * @return value read, or -ve on error
219 */
220int dm_i2c_reg_read(struct udevice *dev, uint offset);
221
222/**
223 * dm_i2c_reg_write() - Write a value to an I2C register
224 *
225 * This writes a single value to the given address in an I2C chip
226 *
Simon Glass25a0fb42015-07-02 18:15:40 -0600227 * @dev: Device to use for transfer
Simon Glassba3864f2015-04-20 12:37:14 -0600228 * @addr: Address to write to
229 * @val: Value to write (normally a byte)
230 * @return 0 on success, -ve on error
231 */
232int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val);
233
234/**
Simon Glassdf358c62015-07-02 18:15:42 -0600235 * dm_i2c_xfer() - Transfer messages over I2C
236 *
237 * This transfers a raw message. It is best to use dm_i2c_reg_read/write()
238 * instead.
239 *
240 * @dev: Device to use for transfer
241 * @msg: List of messages to transfer
242 * @nmsgs: Number of messages to transfer
243 * @return 0 on success, -ve on error
244 */
245int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs);
246
247/**
Simon Glassca88b9b2015-02-05 21:41:32 -0700248 * dm_i2c_set_bus_speed() - set the speed of a bus
Simon Glassc6202d82014-12-10 08:55:47 -0700249 *
250 * @bus: Bus to adjust
251 * @speed: Requested speed in Hz
252 * @return 0 if OK, -EINVAL for invalid values
253 */
Simon Glassca88b9b2015-02-05 21:41:32 -0700254int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
Simon Glassc6202d82014-12-10 08:55:47 -0700255
256/**
Simon Glassca88b9b2015-02-05 21:41:32 -0700257 * dm_i2c_get_bus_speed() - get the speed of a bus
Simon Glassc6202d82014-12-10 08:55:47 -0700258 *
259 * @bus: Bus to check
260 * @return speed of selected I2C bus in Hz, -ve on error
261 */
Simon Glassca88b9b2015-02-05 21:41:32 -0700262int dm_i2c_get_bus_speed(struct udevice *bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700263
264/**
265 * i2c_set_chip_flags() - set flags for a chip
266 *
267 * Typically addresses are 7 bits, but for 10-bit addresses you should set
268 * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
269 *
270 * @dev: Chip to adjust
271 * @flags: New flags
272 * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error
273 */
274int i2c_set_chip_flags(struct udevice *dev, uint flags);
275
276/**
277 * i2c_get_chip_flags() - get flags for a chip
278 *
279 * @dev: Chip to check
280 * @flagsp: Place to put flags
281 * @return 0 if OK, other -ve value on error
282 */
283int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
284
285/**
286 * i2c_set_offset_len() - set the offset length for a chip
287 *
288 * The offset used to access a chip may be up to 4 bytes long. Typically it
289 * is only 1 byte, which is enough for chips with 256 bytes of memory or
290 * registers. The default value is 1, but you can call this function to
291 * change it.
292 *
293 * @offset_len: New offset length value (typically 1 or 2)
294 */
Simon Glassc6202d82014-12-10 08:55:47 -0700295int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
Simon Glass01501802015-05-04 11:30:58 -0600296
297/**
298 * i2c_get_offset_len() - get the offset length for a chip
299 *
300 * @return: Current offset length value (typically 1 or 2)
301 */
302int i2c_get_chip_offset_len(struct udevice *dev);
303
Simon Glassc6202d82014-12-10 08:55:47 -0700304/**
Robert Beckett85968522019-10-28 17:44:57 +0000305 * i2c_set_chip_addr_offset_mask() - set mask of address bits usable by offset
306 *
307 * Some devices listen on multiple chip addresses to achieve larger offsets
308 * than their single or multiple byte offsets would allow for. You can use this
309 * function to set the bits that are valid to be used for offset overflow.
310 *
311 * @mask: The mask to be used for high offset bits within address
312 * @return 0 if OK, other -ve value on error
313 */
314int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask);
315
316/*
317 * i2c_get_chip_addr_offset_mask() - get mask of address bits usable by offset
318 *
319 * @return current chip addr offset mask
320 */
321uint i2c_get_chip_addr_offset_mask(struct udevice *dev);
322
323/**
Simon Glassc6202d82014-12-10 08:55:47 -0700324 * i2c_deblock() - recover a bus that is in an unknown state
325 *
326 * See the deblock() method in 'struct dm_i2c_ops' for full information
327 *
328 * @bus: Bus to recover
329 * @return 0 if OK, -ve on error
330 */
331int i2c_deblock(struct udevice *bus);
332
Simon Glassc6202d82014-12-10 08:55:47 -0700333/**
Marek Vasut72315222020-02-07 16:57:50 +0100334 * i2c_deblock_gpio_loop() - recover a bus from an unknown state by toggling SDA/SCL
335 *
336 * This is the inner logic used for toggling I2C SDA/SCL lines as GPIOs
337 * for deblocking the I2C bus.
338 *
339 * @sda_pin: SDA GPIO
340 * @scl_pin: SCL GPIO
341 * @scl_count: Number of SCL clock cycles generated to deblock SDA
Marek Vasuta1917282020-02-07 16:57:51 +0100342 * @start_count:Number of I2C start conditions sent after deblocking SDA
Marek Vasut72315222020-02-07 16:57:50 +0100343 * @delay: Delay between SCL clock line changes
344 * @return 0 if OK, -ve on error
345 */
346struct gpio_desc;
347int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, struct gpio_desc *scl_pin,
Marek Vasuta1917282020-02-07 16:57:51 +0100348 unsigned int scl_count, unsigned int start_count,
349 unsigned int delay);
Marek Vasut72315222020-02-07 16:57:50 +0100350
351/**
Simon Glassc6202d82014-12-10 08:55:47 -0700352 * struct dm_i2c_ops - driver operations for I2C uclass
353 *
354 * Drivers should support these operations unless otherwise noted. These
355 * operations are intended to be used by uclass code, not directly from
356 * other code.
357 */
358struct dm_i2c_ops {
359 /**
360 * xfer() - transfer a list of I2C messages
361 *
362 * @bus: Bus to read from
363 * @msg: List of messages to transfer
364 * @nmsgs: Number of messages in the list
365 * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
366 * -ECOMM if the speed cannot be supported, -EPROTO if the chip
367 * flags cannot be supported, other -ve value on some other error
368 */
369 int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
370
371 /**
372 * probe_chip() - probe for the presense of a chip address
373 *
374 * This function is optional. If omitted, the uclass will send a zero
375 * length message instead.
376 *
377 * @bus: Bus to probe
378 * @chip_addr: Chip address to probe
379 * @chip_flags: Probe flags (enum dm_i2c_chip_flags)
380 * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
381 * to default probem other -ve value on error
382 */
383 int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
384
385 /**
386 * set_bus_speed() - set the speed of a bus (optional)
387 *
388 * The bus speed value will be updated by the uclass if this function
389 * does not return an error. This method is optional - if it is not
390 * provided then the driver can read the speed from
Simon Glasse564f052015-03-05 12:25:20 -0700391 * dev_get_uclass_priv(bus)->speed_hz
Simon Glassc6202d82014-12-10 08:55:47 -0700392 *
393 * @bus: Bus to adjust
394 * @speed: Requested speed in Hz
395 * @return 0 if OK, -EINVAL for invalid values
396 */
397 int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
398
399 /**
400 * get_bus_speed() - get the speed of a bus (optional)
401 *
402 * Normally this can be provided by the uclass, but if you want your
403 * driver to check the bus speed by looking at the hardware, you can
404 * implement that here. This method is optional. This method would
Simon Glasse564f052015-03-05 12:25:20 -0700405 * normally be expected to return dev_get_uclass_priv(bus)->speed_hz.
Simon Glassc6202d82014-12-10 08:55:47 -0700406 *
407 * @bus: Bus to check
408 * @return speed of selected I2C bus in Hz, -ve on error
409 */
410 int (*get_bus_speed)(struct udevice *bus);
411
412 /**
413 * set_flags() - set the flags for a chip (optional)
414 *
415 * This is generally implemented by the uclass, but drivers can
416 * check the value to ensure that unsupported options are not used.
417 * This method is optional. If provided, this method will always be
418 * called when the flags change.
419 *
420 * @dev: Chip to adjust
421 * @flags: New flags value
422 * @return 0 if OK, -EINVAL if value is unsupported
423 */
424 int (*set_flags)(struct udevice *dev, uint flags);
425
426 /**
427 * deblock() - recover a bus that is in an unknown state
428 *
429 * I2C is a synchronous protocol and resets of the processor in the
430 * middle of an access can block the I2C Bus until a powerdown of
431 * the full unit is done. This is because slaves can be stuck
432 * waiting for addition bus transitions for a transaction that will
433 * never complete. Resetting the I2C master does not help. The only
434 * way is to force the bus through a series of transitions to make
435 * sure that all slaves are done with the transaction. This method
436 * performs this 'deblocking' if support by the driver.
437 *
438 * This method is optional.
439 */
440 int (*deblock)(struct udevice *bus);
441};
442
443#define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops)
444
445/**
Simon Glass3d1957f2015-08-03 08:19:21 -0600446 * struct i2c_mux_ops - operations for an I2C mux
447 *
448 * The current mux state is expected to be stored in the mux itself since
449 * it is the only thing that knows how to make things work. The mux can
450 * record the current state and then avoid switching unless it is necessary.
451 * So select() can be skipped if the mux is already in the correct state.
452 * Also deselect() can be made a nop if required.
453 */
454struct i2c_mux_ops {
455 /**
456 * select() - select one of of I2C buses attached to a mux
457 *
458 * This will be called when there is no bus currently selected by the
459 * mux. This method does not need to deselect the old bus since
460 * deselect() will be already have been called if necessary.
461 *
462 * @mux: Mux device
463 * @bus: I2C bus to select
464 * @channel: Channel number correponding to the bus to select
465 * @return 0 if OK, -ve on error
466 */
467 int (*select)(struct udevice *mux, struct udevice *bus, uint channel);
468
469 /**
470 * deselect() - select one of of I2C buses attached to a mux
471 *
472 * This is used to deselect the currently selected I2C bus.
473 *
474 * @mux: Mux device
475 * @bus: I2C bus to deselect
476 * @channel: Channel number correponding to the bus to deselect
477 * @return 0 if OK, -ve on error
478 */
479 int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel);
480};
481
482#define i2c_mux_get_ops(dev) ((struct i2c_mux_ops *)(dev)->driver->ops)
483
484/**
Simon Glassc6202d82014-12-10 08:55:47 -0700485 * i2c_get_chip() - get a device to use to access a chip on a bus
486 *
487 * This returns the device for the given chip address. The device can then
488 * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
489 *
490 * @bus: Bus to examine
491 * @chip_addr: Chip address for the new device
Simon Glass25ab4b02015-01-25 08:26:55 -0700492 * @offset_len: Length of a register offset in bytes (normally 1)
Simon Glassc6202d82014-12-10 08:55:47 -0700493 * @devp: Returns pointer to new device if found or -ENODEV if not
494 * found
495 */
Simon Glass25ab4b02015-01-25 08:26:55 -0700496int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
497 struct udevice **devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700498
499/**
Stefan Roesea06728c2015-11-25 07:41:58 +0100500 * i2c_get_chip_for_busnum() - get a device to use to access a chip on
501 * a bus number
Simon Glassc6202d82014-12-10 08:55:47 -0700502 *
503 * This returns the device for the given chip address on a particular bus
504 * number.
505 *
506 * @busnum: Bus number to examine
507 * @chip_addr: Chip address for the new device
Simon Glass25ab4b02015-01-25 08:26:55 -0700508 * @offset_len: Length of a register offset in bytes (normally 1)
Simon Glassc6202d82014-12-10 08:55:47 -0700509 * @devp: Returns pointer to new device if found or -ENODEV if not
510 * found
511 */
Simon Glass25ab4b02015-01-25 08:26:55 -0700512int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
513 struct udevice **devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700514
515/**
516 * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data
517 *
518 * This decodes the chip address from a device tree node and puts it into
519 * its dm_i2c_chip structure. This should be called in your driver's
520 * ofdata_to_platdata() method.
521 *
522 * @blob: Device tree blob
523 * @node: Node offset to read from
524 * @spi: Place to put the decoded information
525 */
Simon Glass17043082017-05-18 20:09:30 -0600526int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip);
Simon Glassc6202d82014-12-10 08:55:47 -0700527
Simon Glass7d7db222015-07-02 18:15:39 -0600528/**
529 * i2c_dump_msgs() - Dump a list of I2C messages
530 *
531 * This may be useful for debugging.
532 *
533 * @msg: Message list to dump
534 * @nmsgs: Number of messages
535 */
536void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs);
537
Simon Glassb7c25b12018-11-18 08:14:33 -0700538/**
539 * i2c_emul_find() - Find an emulator for an i2c sandbox device
540 *
541 * This looks at the device's 'emul' phandle
542 *
543 * @dev: Device to find an emulator for
544 * @emulp: Returns the associated emulator, if found *
545 * @return 0 if OK, -ENOENT or -ENODEV if not found
546 */
547int i2c_emul_find(struct udevice *dev, struct udevice **emulp);
548
549/**
550 * i2c_emul_get_device() - Find the device being emulated
551 *
552 * Given an emulator this returns the associated device
553 *
554 * @emul: Emulator for the device
555 * @return device that @emul is emulating
556 */
557struct udevice *i2c_emul_get_device(struct udevice *emul);
558
Simon Glassc6202d82014-12-10 08:55:47 -0700559#ifndef CONFIG_DM_I2C
560
561/*
wdenk1f045212002-03-10 14:37:15 +0000562 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
563 *
564 * The implementation MUST NOT use static or global variables if the
565 * I2C routines are used to read SDRAM configuration information
566 * because this is done before the memories are initialized. Limited
567 * use of stack-based variables are OK (the initial stack size is
568 * limited).
569 *
570 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
571 */
572
573/*
574 * Configuration items.
575 */
576#define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
577
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000578#if !defined(CONFIG_SYS_I2C_MAX_HOPS)
579/* no muxes used bus = i2c adapters */
580#define CONFIG_SYS_I2C_DIRECT_BUS 1
581#define CONFIG_SYS_I2C_MAX_HOPS 0
582#define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100583#else
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000584/* we use i2c muxes */
585#undef CONFIG_SYS_I2C_DIRECT_BUS
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100586#endif
587
Stefan Roese8c120452007-03-01 07:03:25 +0100588/* define the I2C bus number for RTC and DTT if not already done */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200589#if !defined(CONFIG_SYS_RTC_BUS_NUM)
590#define CONFIG_SYS_RTC_BUS_NUM 0
Stefan Roese8c120452007-03-01 07:03:25 +0100591#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200592#if !defined(CONFIG_SYS_SPD_BUS_NUM)
593#define CONFIG_SYS_SPD_BUS_NUM 0
Matthias Fuchsd8a8ea52007-03-08 16:20:32 +0100594#endif
Stefan Roese8c120452007-03-01 07:03:25 +0100595
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000596struct i2c_adapter {
597 void (*init)(struct i2c_adapter *adap, int speed,
598 int slaveaddr);
599 int (*probe)(struct i2c_adapter *adap, uint8_t chip);
600 int (*read)(struct i2c_adapter *adap, uint8_t chip,
601 uint addr, int alen, uint8_t *buffer,
602 int len);
603 int (*write)(struct i2c_adapter *adap, uint8_t chip,
604 uint addr, int alen, uint8_t *buffer,
605 int len);
606 uint (*set_bus_speed)(struct i2c_adapter *adap,
607 uint speed);
608 int speed;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100609 int waitdelay;
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000610 int slaveaddr;
611 int init_done;
612 int hwadapnr;
613 char *name;
614};
615
616#define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
617 _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
618 { \
619 .init = _init, \
620 .probe = _probe, \
621 .read = _read, \
622 .write = _write, \
623 .set_bus_speed = _set_speed, \
624 .speed = _speed, \
625 .slaveaddr = _slaveaddr, \
626 .init_done = 0, \
627 .hwadapnr = _hwadapnr, \
628 .name = #_name \
629};
630
631#define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
632 _set_speed, _speed, _slaveaddr, _hwadapnr) \
633 ll_entry_declare(struct i2c_adapter, _name, i2c) = \
634 U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
635 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
636
637struct i2c_adapter *i2c_get_adapter(int index);
638
639#ifndef CONFIG_SYS_I2C_DIRECT_BUS
640struct i2c_mux {
641 int id;
642 char name[16];
643};
644
645struct i2c_next_hop {
646 struct i2c_mux mux;
647 uint8_t chip;
648 uint8_t channel;
649};
650
651struct i2c_bus_hose {
652 int adapter;
653 struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
654};
655#define I2C_NULL_HOP {{-1, ""}, 0, 0}
656extern struct i2c_bus_hose i2c_bus[];
657
658#define I2C_ADAPTER(bus) i2c_bus[bus].adapter
659#else
660#define I2C_ADAPTER(bus) bus
661#endif
662#define I2C_BUS gd->cur_i2c_bus
663
664#define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
665#define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
666#define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
667
668#ifndef CONFIG_SYS_I2C_DIRECT_BUS
669#define I2C_MUX_PCA9540_ID 1
670#define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
671#define I2C_MUX_PCA9542_ID 2
672#define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
673#define I2C_MUX_PCA9544_ID 3
674#define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
675#define I2C_MUX_PCA9547_ID 4
676#define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
Michael Burre6658742013-09-23 22:35:45 +0000677#define I2C_MUX_PCA9548_ID 5
678#define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000679#endif
680
Heiko Schocher98aed372008-10-15 09:35:26 +0200681#ifndef I2C_SOFT_DECLARATIONS
Heiko Schocher2eb48ff2017-06-07 17:33:10 +0200682# if (defined(CONFIG_AT91RM9200) || \
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100683 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
Andreas Bießmanncb96a0a2013-10-30 15:18:18 +0100684 defined(CONFIG_AT91SAM9263))
esw@bus-elektronik.de78132272011-12-20 06:05:30 +0000685# define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
Heiko Schocher98aed372008-10-15 09:35:26 +0200686# else
687# define I2C_SOFT_DECLARATIONS
688# endif
689#endif
Timur Tabiecf5f072008-12-03 11:28:30 -0600690
Peter Tyser9c90a2c2009-04-24 15:34:05 -0500691/*
692 * Many boards/controllers/drivers don't support an I2C slave interface so
693 * provide a default slave address for them for use in common code. A real
694 * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
695 * support a slave interface.
696 */
697#ifndef CONFIG_SYS_I2C_SLAVE
698#define CONFIG_SYS_I2C_SLAVE 0xfe
Timur Tabiecf5f072008-12-03 11:28:30 -0600699#endif
700
wdenk1f045212002-03-10 14:37:15 +0000701/*
702 * Initialization, must be called once on start up, may be called
703 * repeatedly to change the speed and slave addresses.
704 */
Yuan Yao9d10c2d2016-06-08 18:24:51 +0800705#ifdef CONFIG_SYS_I2C_EARLY_INIT
706void i2c_early_init_f(void);
707#endif
wdenk1f045212002-03-10 14:37:15 +0000708void i2c_init(int speed, int slaveaddr);
wdenk06d01db2003-03-14 20:47:52 +0000709void i2c_init_board(void);
wdenk1f045212002-03-10 14:37:15 +0000710
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000711#ifdef CONFIG_SYS_I2C
712/*
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000713 * i2c_get_bus_num:
714 *
715 * Returns index of currently active I2C bus. Zero-based.
716 */
717unsigned int i2c_get_bus_num(void);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200718
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000719/*
720 * i2c_set_bus_num:
721 *
722 * Change the active I2C bus. Subsequent read/write calls will
723 * go to this one.
724 *
725 * bus - bus index, zero based
726 *
727 * Returns: 0 on success, not 0 on failure
728 *
729 */
730int i2c_set_bus_num(unsigned int bus);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200731
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000732/*
733 * i2c_init_all():
734 *
735 * Initializes all I2C adapters in the system. All i2c_adap structures must
736 * be initialized beforehead with function pointers and data, including
737 * speed and slaveaddr. Returns 0 on success, non-0 on failure.
738 */
739void i2c_init_all(void);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200740
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000741/*
742 * Probe the given I2C chip address. Returns 0 if a chip responded,
743 * not 0 on failure.
744 */
745int i2c_probe(uint8_t chip);
746
747/*
748 * Read/Write interface:
749 * chip: I2C chip address, range 0..127
750 * addr: Memory (register) address within the chip
751 * alen: Number of bytes to use for addr (typically 1, 2 for larger
752 * memories, 0 for register type devices with only one
753 * register)
754 * buffer: Where to read/write the data
755 * len: How many bytes to read/write
756 *
757 * Returns: 0 on success, not 0 on failure
758 */
759int i2c_read(uint8_t chip, unsigned int addr, int alen,
760 uint8_t *buffer, int len);
761
762int i2c_write(uint8_t chip, unsigned int addr, int alen,
763 uint8_t *buffer, int len);
764
765/*
766 * Utility routines to read/write registers.
767 */
768uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
769
770void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
771
772/*
773 * i2c_set_bus_speed:
774 *
775 * Change the speed of the active I2C bus
776 *
777 * speed - bus speed in Hz
778 *
779 * Returns: new bus speed
780 *
781 */
782unsigned int i2c_set_bus_speed(unsigned int speed);
783
784/*
785 * i2c_get_bus_speed:
786 *
787 * Returns speed of currently active I2C bus in Hz
788 */
789
790unsigned int i2c_get_bus_speed(void);
791
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000792#else
Heiko Schocher67b23a32008-10-15 09:39:47 +0200793
wdenk1f045212002-03-10 14:37:15 +0000794/*
795 * Probe the given I2C chip address. Returns 0 if a chip responded,
796 * not 0 on failure.
797 */
798int i2c_probe(uchar chip);
799
800/*
801 * Read/Write interface:
802 * chip: I2C chip address, range 0..127
803 * addr: Memory (register) address within the chip
804 * alen: Number of bytes to use for addr (typically 1, 2 for larger
805 * memories, 0 for register type devices with only one
806 * register)
807 * buffer: Where to read/write the data
808 * len: How many bytes to read/write
809 *
810 * Returns: 0 on success, not 0 on failure
811 */
812int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
813int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
814
815/*
816 * Utility routines to read/write registers.
817 */
Timur Tabiecf5f072008-12-03 11:28:30 -0600818static inline u8 i2c_reg_read(u8 addr, u8 reg)
819{
820 u8 buf;
821
Timur Tabiecf5f072008-12-03 11:28:30 -0600822#ifdef DEBUG
823 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
824#endif
825
Timur Tabiecf5f072008-12-03 11:28:30 -0600826 i2c_read(addr, reg, 1, &buf, 1);
Timur Tabiecf5f072008-12-03 11:28:30 -0600827
828 return buf;
829}
830
831static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
832{
Timur Tabiecf5f072008-12-03 11:28:30 -0600833#ifdef DEBUG
834 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
835 __func__, addr, reg, val);
836#endif
837
Timur Tabiecf5f072008-12-03 11:28:30 -0600838 i2c_write(addr, reg, 1, &val, 1);
Timur Tabiecf5f072008-12-03 11:28:30 -0600839}
wdenk1f045212002-03-10 14:37:15 +0000840
Ben Warrenbb99ad62006-09-07 16:50:54 -0400841/*
842 * Functions for setting the current I2C bus and its speed
843 */
844
845/*
846 * i2c_set_bus_num:
847 *
848 * Change the active I2C bus. Subsequent read/write calls will
849 * go to this one.
850 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200851 * bus - bus index, zero based
Ben Warrenbb99ad62006-09-07 16:50:54 -0400852 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200853 * Returns: 0 on success, not 0 on failure
Ben Warrenbb99ad62006-09-07 16:50:54 -0400854 *
855 */
Timur Tabi9ca880a2006-10-31 21:23:16 -0600856int i2c_set_bus_num(unsigned int bus);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400857
858/*
859 * i2c_get_bus_num:
860 *
861 * Returns index of currently active I2C bus. Zero-based.
862 */
863
Timur Tabi9ca880a2006-10-31 21:23:16 -0600864unsigned int i2c_get_bus_num(void);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400865
866/*
867 * i2c_set_bus_speed:
868 *
869 * Change the speed of the active I2C bus
870 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200871 * speed - bus speed in Hz
Ben Warrenbb99ad62006-09-07 16:50:54 -0400872 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200873 * Returns: 0 on success, not 0 on failure
Ben Warrenbb99ad62006-09-07 16:50:54 -0400874 *
875 */
Timur Tabi9ca880a2006-10-31 21:23:16 -0600876int i2c_set_bus_speed(unsigned int);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400877
878/*
879 * i2c_get_bus_speed:
880 *
881 * Returns speed of currently active I2C bus in Hz
882 */
883
Timur Tabi9ca880a2006-10-31 21:23:16 -0600884unsigned int i2c_get_bus_speed(void);
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000885#endif /* CONFIG_SYS_I2C */
886
887/*
888 * only for backwardcompatibility, should go away if we switched
889 * completely to new multibus support.
890 */
891#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
892# if !defined(CONFIG_SYS_MAX_I2C_BUS)
893# define CONFIG_SYS_MAX_I2C_BUS 2
894# endif
Łukasz Majewskiea0f73a2013-08-16 15:31:45 +0200895# define I2C_MULTI_BUS 1
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000896#else
897# define CONFIG_SYS_MAX_I2C_BUS 1
898# define I2C_MULTI_BUS 0
899#endif
Ben Warrenbb99ad62006-09-07 16:50:54 -0400900
Marek Vasutcd7b4e82011-10-25 11:40:57 +0200901/* NOTE: These two functions MUST be always_inline to avoid code growth! */
902static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
903static inline unsigned int I2C_GET_BUS(void)
904{
905 return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
906}
907
908static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
909static inline void I2C_SET_BUS(unsigned int bus)
910{
911 if (I2C_MULTI_BUS)
912 i2c_set_bus_num(bus);
913}
914
Łukasz Majewski7ca8f732012-09-04 23:15:20 +0000915/* Multi I2C definitions */
916enum {
917 I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
918 I2C_8, I2C_9, I2C_10,
919};
920
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000921/**
922 * Get FDT values for i2c bus.
923 *
924 * @param blob Device tree blbo
925 * @return the number of I2C bus
926 */
927void board_i2c_init(const void *blob);
928
929/**
930 * Find the I2C bus number by given a FDT I2C node.
931 *
932 * @param blob Device tree blbo
933 * @param node FDT I2C node to find
934 * @return the number of I2C bus (zero based), or -1 on error
935 */
936int i2c_get_bus_num_fdt(int node);
937
938/**
939 * Reset the I2C bus represented by the given a FDT I2C node.
940 *
941 * @param blob Device tree blbo
942 * @param node FDT I2C node to find
943 * @return 0 if port was reset, -1 if not found
944 */
945int i2c_reset_port_fdt(const void *blob, int node);
Simon Glassc6202d82014-12-10 08:55:47 -0700946
947#endif /* !CONFIG_DM_I2C */
948
wdenk1f045212002-03-10 14:37:15 +0000949#endif /* _I2C_H_ */