blob: 72e2e8e4260e7f2f270c6c6ce07a4c0f0007c04c [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 Glassfffff722015-02-05 21:41:33 -070033struct udevice;
Simon Glassc6202d82014-12-10 08:55:47 -070034/**
35 * struct dm_i2c_chip - information about an i2c chip
36 *
37 * An I2C chip is a device on the I2C bus. It sits at a particular address
38 * and normally supports 7-bit or 10-bit addressing.
39 *
Simon Glasse6f66ec2015-01-25 08:27:13 -070040 * To obtain this structure, use dev_get_parent_platdata(dev) where dev is
41 * the chip to examine.
Simon Glassc6202d82014-12-10 08:55:47 -070042 *
43 * @chip_addr: Chip address on bus
44 * @offset_len: Length of offset in bytes. A single byte offset can
45 * represent up to 256 bytes. A value larger than 1 may be
46 * needed for larger devices.
47 * @flags: Flags for this chip (dm_i2c_chip_flags)
Robert Beckett85968522019-10-28 17:44:57 +000048 * @chip_addr_offset_mask: Mask of offset bits within chip_addr. Used for
49 * devices which steal addresses as part of offset.
50 * If offset_len is zero, then the offset is encoded
51 * completely within the chip address itself.
52 * e.g. a devce with chip address of 0x2c with 512
53 * registers might use the bottom bit of the address
54 * to indicate which half of the address space is being
55 * accessed while still only using 1 byte offset.
56 * This means it will respond to chip address 0x2c and
57 * 0x2d.
58 * A real world example is the Atmel AT24C04. It's
59 * datasheet explains it's usage of this addressing
60 * mode.
Simon Glassc6202d82014-12-10 08:55:47 -070061 * @emul: Emulator for this chip address (only used for emulation)
62 */
63struct dm_i2c_chip {
64 uint chip_addr;
65 uint offset_len;
66 uint flags;
Robert Beckett85968522019-10-28 17:44:57 +000067 uint chip_addr_offset_mask;
Simon Glassc6202d82014-12-10 08:55:47 -070068#ifdef CONFIG_SANDBOX
69 struct udevice *emul;
Simon Glass182bf922015-04-20 12:37:15 -060070 bool test_mode;
Simon Glassc6202d82014-12-10 08:55:47 -070071#endif
72};
73
74/**
75 * struct dm_i2c_bus- information about an i2c bus
76 *
77 * An I2C bus contains 0 or more chips on it, each at its own address. The
78 * bus can operate at different speeds (measured in Hz, typically 100KHz
79 * or 400KHz).
80 *
Simon Glasse564f052015-03-05 12:25:20 -070081 * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the
82 * I2C bus udevice.
Simon Glassc6202d82014-12-10 08:55:47 -070083 *
84 * @speed_hz: Bus speed in hertz (typically 100000)
Lukasz Majewskia40fe212019-04-04 12:35:34 +020085 * @max_transaction_bytes: Maximal size of single I2C transfer
Simon Glassc6202d82014-12-10 08:55:47 -070086 */
87struct dm_i2c_bus {
88 int speed_hz;
Lukasz Majewskia40fe212019-04-04 12:35:34 +020089 int max_transaction_bytes;
Simon Glassc6202d82014-12-10 08:55:47 -070090};
91
Simon Glass7fc65bc2015-07-02 18:15:41 -060092/*
93 * Not all of these flags are implemented in the U-Boot API
94 */
95enum dm_i2c_msg_flags {
96 I2C_M_TEN = 0x0010, /* ten-bit chip address */
97 I2C_M_RD = 0x0001, /* read data, from slave to master */
98 I2C_M_STOP = 0x8000, /* send stop after this message */
99 I2C_M_NOSTART = 0x4000, /* no start before this message */
100 I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */
101 I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */
102 I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */
103 I2C_M_RECV_LEN = 0x0400, /* length is first received byte */
104};
105
106/**
107 * struct i2c_msg - an I2C message
108 *
109 * @addr: Slave address
110 * @flags: Flags (see enum dm_i2c_msg_flags)
111 * @len: Length of buffer in bytes, may be 0 for a probe
112 * @buf: Buffer to send/receive, or NULL if no data
113 */
114struct i2c_msg {
115 uint addr;
116 uint flags;
117 uint len;
118 u8 *buf;
119};
120
121/**
122 * struct i2c_msg_list - a list of I2C messages
123 *
124 * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
125 * appropriate in U-Boot.
126 *
127 * @msg: Pointer to i2c_msg array
128 * @nmsgs: Number of elements in the array
129 */
130struct i2c_msg_list {
131 struct i2c_msg *msgs;
132 uint nmsgs;
133};
134
Simon Glassc6202d82014-12-10 08:55:47 -0700135/**
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700136 * dm_i2c_read() - read bytes from an I2C chip
Simon Glassc6202d82014-12-10 08:55:47 -0700137 *
138 * To obtain an I2C device (called a 'chip') given the I2C bus address you
139 * can use i2c_get_chip(). To obtain a bus by bus number use
140 * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
141 *
142 * To set the address length of a devce use i2c_set_addr_len(). It
143 * defaults to 1.
144 *
145 * @dev: Chip to read from
146 * @offset: Offset within chip to start reading
147 * @buffer: Place to put data
148 * @len: Number of bytes to read
149 *
150 * @return 0 on success, -ve on failure
151 */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700152int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
Simon Glassc6202d82014-12-10 08:55:47 -0700153
154/**
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700155 * dm_i2c_write() - write bytes to an I2C chip
Simon Glassc6202d82014-12-10 08:55:47 -0700156 *
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700157 * See notes for dm_i2c_read() above.
Simon Glassc6202d82014-12-10 08:55:47 -0700158 *
159 * @dev: Chip to write to
160 * @offset: Offset within chip to start writing
161 * @buffer: Buffer containing data to write
162 * @len: Number of bytes to write
163 *
164 * @return 0 on success, -ve on failure
165 */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700166int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
167 int len);
Simon Glassc6202d82014-12-10 08:55:47 -0700168
169/**
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700170 * dm_i2c_probe() - probe a particular chip address
Simon Glassc6202d82014-12-10 08:55:47 -0700171 *
172 * This can be useful to check for the existence of a chip on the bus.
173 * It is typically implemented by writing the chip address to the bus
174 * and checking that the chip replies with an ACK.
175 *
176 * @bus: Bus to probe
177 * @chip_addr: 7-bit address to probe (10-bit and others are not supported)
178 * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags)
179 * @devp: Returns the device found, or NULL if none
180 * @return 0 if a chip was found at that address, -ve if not
181 */
Simon Glassf9a4c2d2015-01-12 18:02:07 -0700182int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
183 struct udevice **devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700184
185/**
Simon Glassba3864f2015-04-20 12:37:14 -0600186 * dm_i2c_reg_read() - Read a value from an I2C register
187 *
188 * This reads a single value from the given address in an I2C chip
189 *
Simon Glass25a0fb42015-07-02 18:15:40 -0600190 * @dev: Device to use for transfer
Simon Glassba3864f2015-04-20 12:37:14 -0600191 * @addr: Address to read from
192 * @return value read, or -ve on error
193 */
194int dm_i2c_reg_read(struct udevice *dev, uint offset);
195
196/**
197 * dm_i2c_reg_write() - Write a value to an I2C register
198 *
199 * This writes a single value to the given address in an I2C chip
200 *
Simon Glass25a0fb42015-07-02 18:15:40 -0600201 * @dev: Device to use for transfer
Simon Glassba3864f2015-04-20 12:37:14 -0600202 * @addr: Address to write to
203 * @val: Value to write (normally a byte)
204 * @return 0 on success, -ve on error
205 */
206int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val);
207
208/**
Simon Glassdf358c62015-07-02 18:15:42 -0600209 * dm_i2c_xfer() - Transfer messages over I2C
210 *
211 * This transfers a raw message. It is best to use dm_i2c_reg_read/write()
212 * instead.
213 *
214 * @dev: Device to use for transfer
215 * @msg: List of messages to transfer
216 * @nmsgs: Number of messages to transfer
217 * @return 0 on success, -ve on error
218 */
219int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs);
220
221/**
Simon Glassca88b9b2015-02-05 21:41:32 -0700222 * dm_i2c_set_bus_speed() - set the speed of a bus
Simon Glassc6202d82014-12-10 08:55:47 -0700223 *
224 * @bus: Bus to adjust
225 * @speed: Requested speed in Hz
226 * @return 0 if OK, -EINVAL for invalid values
227 */
Simon Glassca88b9b2015-02-05 21:41:32 -0700228int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
Simon Glassc6202d82014-12-10 08:55:47 -0700229
230/**
Simon Glassca88b9b2015-02-05 21:41:32 -0700231 * dm_i2c_get_bus_speed() - get the speed of a bus
Simon Glassc6202d82014-12-10 08:55:47 -0700232 *
233 * @bus: Bus to check
234 * @return speed of selected I2C bus in Hz, -ve on error
235 */
Simon Glassca88b9b2015-02-05 21:41:32 -0700236int dm_i2c_get_bus_speed(struct udevice *bus);
Simon Glassc6202d82014-12-10 08:55:47 -0700237
238/**
239 * i2c_set_chip_flags() - set flags for a chip
240 *
241 * Typically addresses are 7 bits, but for 10-bit addresses you should set
242 * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
243 *
244 * @dev: Chip to adjust
245 * @flags: New flags
246 * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error
247 */
248int i2c_set_chip_flags(struct udevice *dev, uint flags);
249
250/**
251 * i2c_get_chip_flags() - get flags for a chip
252 *
253 * @dev: Chip to check
254 * @flagsp: Place to put flags
255 * @return 0 if OK, other -ve value on error
256 */
257int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
258
259/**
260 * i2c_set_offset_len() - set the offset length for a chip
261 *
262 * The offset used to access a chip may be up to 4 bytes long. Typically it
263 * is only 1 byte, which is enough for chips with 256 bytes of memory or
264 * registers. The default value is 1, but you can call this function to
265 * change it.
266 *
267 * @offset_len: New offset length value (typically 1 or 2)
268 */
Simon Glassc6202d82014-12-10 08:55:47 -0700269int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
Simon Glass01501802015-05-04 11:30:58 -0600270
271/**
272 * i2c_get_offset_len() - get the offset length for a chip
273 *
274 * @return: Current offset length value (typically 1 or 2)
275 */
276int i2c_get_chip_offset_len(struct udevice *dev);
277
Simon Glassc6202d82014-12-10 08:55:47 -0700278/**
Robert Beckett85968522019-10-28 17:44:57 +0000279 * i2c_set_chip_addr_offset_mask() - set mask of address bits usable by offset
280 *
281 * Some devices listen on multiple chip addresses to achieve larger offsets
282 * than their single or multiple byte offsets would allow for. You can use this
283 * function to set the bits that are valid to be used for offset overflow.
284 *
285 * @mask: The mask to be used for high offset bits within address
286 * @return 0 if OK, other -ve value on error
287 */
288int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask);
289
290/*
291 * i2c_get_chip_addr_offset_mask() - get mask of address bits usable by offset
292 *
293 * @return current chip addr offset mask
294 */
295uint i2c_get_chip_addr_offset_mask(struct udevice *dev);
296
297/**
Simon Glassc6202d82014-12-10 08:55:47 -0700298 * i2c_deblock() - recover a bus that is in an unknown state
299 *
300 * See the deblock() method in 'struct dm_i2c_ops' for full information
301 *
302 * @bus: Bus to recover
303 * @return 0 if OK, -ve on error
304 */
305int i2c_deblock(struct udevice *bus);
306
Simon Glassc6202d82014-12-10 08:55:47 -0700307/**
308 * struct dm_i2c_ops - driver operations for I2C uclass
309 *
310 * Drivers should support these operations unless otherwise noted. These
311 * operations are intended to be used by uclass code, not directly from
312 * other code.
313 */
314struct dm_i2c_ops {
315 /**
316 * xfer() - transfer a list of I2C messages
317 *
318 * @bus: Bus to read from
319 * @msg: List of messages to transfer
320 * @nmsgs: Number of messages in the list
321 * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
322 * -ECOMM if the speed cannot be supported, -EPROTO if the chip
323 * flags cannot be supported, other -ve value on some other error
324 */
325 int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
326
327 /**
328 * probe_chip() - probe for the presense of a chip address
329 *
330 * This function is optional. If omitted, the uclass will send a zero
331 * length message instead.
332 *
333 * @bus: Bus to probe
334 * @chip_addr: Chip address to probe
335 * @chip_flags: Probe flags (enum dm_i2c_chip_flags)
336 * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
337 * to default probem other -ve value on error
338 */
339 int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
340
341 /**
342 * set_bus_speed() - set the speed of a bus (optional)
343 *
344 * The bus speed value will be updated by the uclass if this function
345 * does not return an error. This method is optional - if it is not
346 * provided then the driver can read the speed from
Simon Glasse564f052015-03-05 12:25:20 -0700347 * dev_get_uclass_priv(bus)->speed_hz
Simon Glassc6202d82014-12-10 08:55:47 -0700348 *
349 * @bus: Bus to adjust
350 * @speed: Requested speed in Hz
351 * @return 0 if OK, -EINVAL for invalid values
352 */
353 int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
354
355 /**
356 * get_bus_speed() - get the speed of a bus (optional)
357 *
358 * Normally this can be provided by the uclass, but if you want your
359 * driver to check the bus speed by looking at the hardware, you can
360 * implement that here. This method is optional. This method would
Simon Glasse564f052015-03-05 12:25:20 -0700361 * normally be expected to return dev_get_uclass_priv(bus)->speed_hz.
Simon Glassc6202d82014-12-10 08:55:47 -0700362 *
363 * @bus: Bus to check
364 * @return speed of selected I2C bus in Hz, -ve on error
365 */
366 int (*get_bus_speed)(struct udevice *bus);
367
368 /**
369 * set_flags() - set the flags for a chip (optional)
370 *
371 * This is generally implemented by the uclass, but drivers can
372 * check the value to ensure that unsupported options are not used.
373 * This method is optional. If provided, this method will always be
374 * called when the flags change.
375 *
376 * @dev: Chip to adjust
377 * @flags: New flags value
378 * @return 0 if OK, -EINVAL if value is unsupported
379 */
380 int (*set_flags)(struct udevice *dev, uint flags);
381
382 /**
383 * deblock() - recover a bus that is in an unknown state
384 *
385 * I2C is a synchronous protocol and resets of the processor in the
386 * middle of an access can block the I2C Bus until a powerdown of
387 * the full unit is done. This is because slaves can be stuck
388 * waiting for addition bus transitions for a transaction that will
389 * never complete. Resetting the I2C master does not help. The only
390 * way is to force the bus through a series of transitions to make
391 * sure that all slaves are done with the transaction. This method
392 * performs this 'deblocking' if support by the driver.
393 *
394 * This method is optional.
395 */
396 int (*deblock)(struct udevice *bus);
397};
398
399#define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops)
400
401/**
Simon Glass3d1957f2015-08-03 08:19:21 -0600402 * struct i2c_mux_ops - operations for an I2C mux
403 *
404 * The current mux state is expected to be stored in the mux itself since
405 * it is the only thing that knows how to make things work. The mux can
406 * record the current state and then avoid switching unless it is necessary.
407 * So select() can be skipped if the mux is already in the correct state.
408 * Also deselect() can be made a nop if required.
409 */
410struct i2c_mux_ops {
411 /**
412 * select() - select one of of I2C buses attached to a mux
413 *
414 * This will be called when there is no bus currently selected by the
415 * mux. This method does not need to deselect the old bus since
416 * deselect() will be already have been called if necessary.
417 *
418 * @mux: Mux device
419 * @bus: I2C bus to select
420 * @channel: Channel number correponding to the bus to select
421 * @return 0 if OK, -ve on error
422 */
423 int (*select)(struct udevice *mux, struct udevice *bus, uint channel);
424
425 /**
426 * deselect() - select one of of I2C buses attached to a mux
427 *
428 * This is used to deselect the currently selected I2C bus.
429 *
430 * @mux: Mux device
431 * @bus: I2C bus to deselect
432 * @channel: Channel number correponding to the bus to deselect
433 * @return 0 if OK, -ve on error
434 */
435 int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel);
436};
437
438#define i2c_mux_get_ops(dev) ((struct i2c_mux_ops *)(dev)->driver->ops)
439
440/**
Simon Glassc6202d82014-12-10 08:55:47 -0700441 * i2c_get_chip() - get a device to use to access a chip on a bus
442 *
443 * This returns the device for the given chip address. The device can then
444 * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
445 *
446 * @bus: Bus to examine
447 * @chip_addr: Chip address for the new device
Simon Glass25ab4b02015-01-25 08:26:55 -0700448 * @offset_len: Length of a register offset in bytes (normally 1)
Simon Glassc6202d82014-12-10 08:55:47 -0700449 * @devp: Returns pointer to new device if found or -ENODEV if not
450 * found
451 */
Simon Glass25ab4b02015-01-25 08:26:55 -0700452int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
453 struct udevice **devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700454
455/**
Stefan Roesea06728c2015-11-25 07:41:58 +0100456 * i2c_get_chip_for_busnum() - get a device to use to access a chip on
457 * a bus number
Simon Glassc6202d82014-12-10 08:55:47 -0700458 *
459 * This returns the device for the given chip address on a particular bus
460 * number.
461 *
462 * @busnum: Bus number to examine
463 * @chip_addr: Chip address for the new device
Simon Glass25ab4b02015-01-25 08:26:55 -0700464 * @offset_len: Length of a register offset in bytes (normally 1)
Simon Glassc6202d82014-12-10 08:55:47 -0700465 * @devp: Returns pointer to new device if found or -ENODEV if not
466 * found
467 */
Simon Glass25ab4b02015-01-25 08:26:55 -0700468int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
469 struct udevice **devp);
Simon Glassc6202d82014-12-10 08:55:47 -0700470
471/**
472 * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data
473 *
474 * This decodes the chip address from a device tree node and puts it into
475 * its dm_i2c_chip structure. This should be called in your driver's
476 * ofdata_to_platdata() method.
477 *
478 * @blob: Device tree blob
479 * @node: Node offset to read from
480 * @spi: Place to put the decoded information
481 */
Simon Glass17043082017-05-18 20:09:30 -0600482int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip);
Simon Glassc6202d82014-12-10 08:55:47 -0700483
Simon Glass7d7db222015-07-02 18:15:39 -0600484/**
485 * i2c_dump_msgs() - Dump a list of I2C messages
486 *
487 * This may be useful for debugging.
488 *
489 * @msg: Message list to dump
490 * @nmsgs: Number of messages
491 */
492void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs);
493
Simon Glassb7c25b12018-11-18 08:14:33 -0700494/**
495 * i2c_emul_find() - Find an emulator for an i2c sandbox device
496 *
497 * This looks at the device's 'emul' phandle
498 *
499 * @dev: Device to find an emulator for
500 * @emulp: Returns the associated emulator, if found *
501 * @return 0 if OK, -ENOENT or -ENODEV if not found
502 */
503int i2c_emul_find(struct udevice *dev, struct udevice **emulp);
504
505/**
506 * i2c_emul_get_device() - Find the device being emulated
507 *
508 * Given an emulator this returns the associated device
509 *
510 * @emul: Emulator for the device
511 * @return device that @emul is emulating
512 */
513struct udevice *i2c_emul_get_device(struct udevice *emul);
514
Simon Glassc6202d82014-12-10 08:55:47 -0700515#ifndef CONFIG_DM_I2C
516
517/*
wdenk1f045212002-03-10 14:37:15 +0000518 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
519 *
520 * The implementation MUST NOT use static or global variables if the
521 * I2C routines are used to read SDRAM configuration information
522 * because this is done before the memories are initialized. Limited
523 * use of stack-based variables are OK (the initial stack size is
524 * limited).
525 *
526 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
527 */
528
529/*
530 * Configuration items.
531 */
532#define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
533
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000534#if !defined(CONFIG_SYS_I2C_MAX_HOPS)
535/* no muxes used bus = i2c adapters */
536#define CONFIG_SYS_I2C_DIRECT_BUS 1
537#define CONFIG_SYS_I2C_MAX_HOPS 0
538#define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100539#else
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000540/* we use i2c muxes */
541#undef CONFIG_SYS_I2C_DIRECT_BUS
Stefan Roese79b2d0b2007-02-20 10:27:08 +0100542#endif
543
Stefan Roese8c120452007-03-01 07:03:25 +0100544/* define the I2C bus number for RTC and DTT if not already done */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200545#if !defined(CONFIG_SYS_RTC_BUS_NUM)
546#define CONFIG_SYS_RTC_BUS_NUM 0
Stefan Roese8c120452007-03-01 07:03:25 +0100547#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200548#if !defined(CONFIG_SYS_SPD_BUS_NUM)
549#define CONFIG_SYS_SPD_BUS_NUM 0
Matthias Fuchsd8a8ea52007-03-08 16:20:32 +0100550#endif
Stefan Roese8c120452007-03-01 07:03:25 +0100551
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000552struct i2c_adapter {
553 void (*init)(struct i2c_adapter *adap, int speed,
554 int slaveaddr);
555 int (*probe)(struct i2c_adapter *adap, uint8_t chip);
556 int (*read)(struct i2c_adapter *adap, uint8_t chip,
557 uint addr, int alen, uint8_t *buffer,
558 int len);
559 int (*write)(struct i2c_adapter *adap, uint8_t chip,
560 uint addr, int alen, uint8_t *buffer,
561 int len);
562 uint (*set_bus_speed)(struct i2c_adapter *adap,
563 uint speed);
564 int speed;
Hannes Petermaierd5243352014-02-03 21:22:18 +0100565 int waitdelay;
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000566 int slaveaddr;
567 int init_done;
568 int hwadapnr;
569 char *name;
570};
571
572#define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
573 _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
574 { \
575 .init = _init, \
576 .probe = _probe, \
577 .read = _read, \
578 .write = _write, \
579 .set_bus_speed = _set_speed, \
580 .speed = _speed, \
581 .slaveaddr = _slaveaddr, \
582 .init_done = 0, \
583 .hwadapnr = _hwadapnr, \
584 .name = #_name \
585};
586
587#define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
588 _set_speed, _speed, _slaveaddr, _hwadapnr) \
589 ll_entry_declare(struct i2c_adapter, _name, i2c) = \
590 U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
591 _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
592
593struct i2c_adapter *i2c_get_adapter(int index);
594
595#ifndef CONFIG_SYS_I2C_DIRECT_BUS
596struct i2c_mux {
597 int id;
598 char name[16];
599};
600
601struct i2c_next_hop {
602 struct i2c_mux mux;
603 uint8_t chip;
604 uint8_t channel;
605};
606
607struct i2c_bus_hose {
608 int adapter;
609 struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
610};
611#define I2C_NULL_HOP {{-1, ""}, 0, 0}
612extern struct i2c_bus_hose i2c_bus[];
613
614#define I2C_ADAPTER(bus) i2c_bus[bus].adapter
615#else
616#define I2C_ADAPTER(bus) bus
617#endif
618#define I2C_BUS gd->cur_i2c_bus
619
620#define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
621#define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
622#define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
623
624#ifndef CONFIG_SYS_I2C_DIRECT_BUS
625#define I2C_MUX_PCA9540_ID 1
626#define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
627#define I2C_MUX_PCA9542_ID 2
628#define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
629#define I2C_MUX_PCA9544_ID 3
630#define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
631#define I2C_MUX_PCA9547_ID 4
632#define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
Michael Burre6658742013-09-23 22:35:45 +0000633#define I2C_MUX_PCA9548_ID 5
634#define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000635#endif
636
Heiko Schocher98aed372008-10-15 09:35:26 +0200637#ifndef I2C_SOFT_DECLARATIONS
Heiko Schocher2eb48ff2017-06-07 17:33:10 +0200638# if (defined(CONFIG_AT91RM9200) || \
Jens Scharsig0cf0b932010-02-03 22:46:58 +0100639 defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
Andreas Bießmanncb96a0a2013-10-30 15:18:18 +0100640 defined(CONFIG_AT91SAM9263))
esw@bus-elektronik.de78132272011-12-20 06:05:30 +0000641# define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
Heiko Schocher98aed372008-10-15 09:35:26 +0200642# else
643# define I2C_SOFT_DECLARATIONS
644# endif
645#endif
Timur Tabiecf5f072008-12-03 11:28:30 -0600646
Peter Tyser9c90a2c2009-04-24 15:34:05 -0500647/*
648 * Many boards/controllers/drivers don't support an I2C slave interface so
649 * provide a default slave address for them for use in common code. A real
650 * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
651 * support a slave interface.
652 */
653#ifndef CONFIG_SYS_I2C_SLAVE
654#define CONFIG_SYS_I2C_SLAVE 0xfe
Timur Tabiecf5f072008-12-03 11:28:30 -0600655#endif
656
wdenk1f045212002-03-10 14:37:15 +0000657/*
658 * Initialization, must be called once on start up, may be called
659 * repeatedly to change the speed and slave addresses.
660 */
Yuan Yao9d10c2d2016-06-08 18:24:51 +0800661#ifdef CONFIG_SYS_I2C_EARLY_INIT
662void i2c_early_init_f(void);
663#endif
wdenk1f045212002-03-10 14:37:15 +0000664void i2c_init(int speed, int slaveaddr);
wdenk06d01db2003-03-14 20:47:52 +0000665void i2c_init_board(void);
wdenk1f045212002-03-10 14:37:15 +0000666
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000667#ifdef CONFIG_SYS_I2C
668/*
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000669 * i2c_get_bus_num:
670 *
671 * Returns index of currently active I2C bus. Zero-based.
672 */
673unsigned int i2c_get_bus_num(void);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200674
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000675/*
676 * i2c_set_bus_num:
677 *
678 * Change the active I2C bus. Subsequent read/write calls will
679 * go to this one.
680 *
681 * bus - bus index, zero based
682 *
683 * Returns: 0 on success, not 0 on failure
684 *
685 */
686int i2c_set_bus_num(unsigned int bus);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200687
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000688/*
689 * i2c_init_all():
690 *
691 * Initializes all I2C adapters in the system. All i2c_adap structures must
692 * be initialized beforehead with function pointers and data, including
693 * speed and slaveaddr. Returns 0 on success, non-0 on failure.
694 */
695void i2c_init_all(void);
Heiko Schocher67b23a32008-10-15 09:39:47 +0200696
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000697/*
698 * Probe the given I2C chip address. Returns 0 if a chip responded,
699 * not 0 on failure.
700 */
701int i2c_probe(uint8_t chip);
702
703/*
704 * Read/Write interface:
705 * chip: I2C chip address, range 0..127
706 * addr: Memory (register) address within the chip
707 * alen: Number of bytes to use for addr (typically 1, 2 for larger
708 * memories, 0 for register type devices with only one
709 * register)
710 * buffer: Where to read/write the data
711 * len: How many bytes to read/write
712 *
713 * Returns: 0 on success, not 0 on failure
714 */
715int i2c_read(uint8_t chip, unsigned int addr, int alen,
716 uint8_t *buffer, int len);
717
718int i2c_write(uint8_t chip, unsigned int addr, int alen,
719 uint8_t *buffer, int len);
720
721/*
722 * Utility routines to read/write registers.
723 */
724uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
725
726void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
727
728/*
729 * i2c_set_bus_speed:
730 *
731 * Change the speed of the active I2C bus
732 *
733 * speed - bus speed in Hz
734 *
735 * Returns: new bus speed
736 *
737 */
738unsigned int i2c_set_bus_speed(unsigned int speed);
739
740/*
741 * i2c_get_bus_speed:
742 *
743 * Returns speed of currently active I2C bus in Hz
744 */
745
746unsigned int i2c_get_bus_speed(void);
747
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000748#else
Heiko Schocher67b23a32008-10-15 09:39:47 +0200749
wdenk1f045212002-03-10 14:37:15 +0000750/*
751 * Probe the given I2C chip address. Returns 0 if a chip responded,
752 * not 0 on failure.
753 */
754int i2c_probe(uchar chip);
755
756/*
757 * Read/Write interface:
758 * chip: I2C chip address, range 0..127
759 * addr: Memory (register) address within the chip
760 * alen: Number of bytes to use for addr (typically 1, 2 for larger
761 * memories, 0 for register type devices with only one
762 * register)
763 * buffer: Where to read/write the data
764 * len: How many bytes to read/write
765 *
766 * Returns: 0 on success, not 0 on failure
767 */
768int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
769int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
770
771/*
772 * Utility routines to read/write registers.
773 */
Timur Tabiecf5f072008-12-03 11:28:30 -0600774static inline u8 i2c_reg_read(u8 addr, u8 reg)
775{
776 u8 buf;
777
Timur Tabiecf5f072008-12-03 11:28:30 -0600778#ifdef DEBUG
779 printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
780#endif
781
Timur Tabiecf5f072008-12-03 11:28:30 -0600782 i2c_read(addr, reg, 1, &buf, 1);
Timur Tabiecf5f072008-12-03 11:28:30 -0600783
784 return buf;
785}
786
787static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
788{
Timur Tabiecf5f072008-12-03 11:28:30 -0600789#ifdef DEBUG
790 printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
791 __func__, addr, reg, val);
792#endif
793
Timur Tabiecf5f072008-12-03 11:28:30 -0600794 i2c_write(addr, reg, 1, &val, 1);
Timur Tabiecf5f072008-12-03 11:28:30 -0600795}
wdenk1f045212002-03-10 14:37:15 +0000796
Ben Warrenbb99ad62006-09-07 16:50:54 -0400797/*
798 * Functions for setting the current I2C bus and its speed
799 */
800
801/*
802 * i2c_set_bus_num:
803 *
804 * Change the active I2C bus. Subsequent read/write calls will
805 * go to this one.
806 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200807 * bus - bus index, zero based
Ben Warrenbb99ad62006-09-07 16:50:54 -0400808 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200809 * Returns: 0 on success, not 0 on failure
Ben Warrenbb99ad62006-09-07 16:50:54 -0400810 *
811 */
Timur Tabi9ca880a2006-10-31 21:23:16 -0600812int i2c_set_bus_num(unsigned int bus);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400813
814/*
815 * i2c_get_bus_num:
816 *
817 * Returns index of currently active I2C bus. Zero-based.
818 */
819
Timur Tabi9ca880a2006-10-31 21:23:16 -0600820unsigned int i2c_get_bus_num(void);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400821
822/*
823 * i2c_set_bus_speed:
824 *
825 * Change the speed of the active I2C bus
826 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200827 * speed - bus speed in Hz
Ben Warrenbb99ad62006-09-07 16:50:54 -0400828 *
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200829 * Returns: 0 on success, not 0 on failure
Ben Warrenbb99ad62006-09-07 16:50:54 -0400830 *
831 */
Timur Tabi9ca880a2006-10-31 21:23:16 -0600832int i2c_set_bus_speed(unsigned int);
Ben Warrenbb99ad62006-09-07 16:50:54 -0400833
834/*
835 * i2c_get_bus_speed:
836 *
837 * Returns speed of currently active I2C bus in Hz
838 */
839
Timur Tabi9ca880a2006-10-31 21:23:16 -0600840unsigned int i2c_get_bus_speed(void);
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000841#endif /* CONFIG_SYS_I2C */
842
843/*
844 * only for backwardcompatibility, should go away if we switched
845 * completely to new multibus support.
846 */
847#if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
848# if !defined(CONFIG_SYS_MAX_I2C_BUS)
849# define CONFIG_SYS_MAX_I2C_BUS 2
850# endif
Łukasz Majewskiea0f73a2013-08-16 15:31:45 +0200851# define I2C_MULTI_BUS 1
Heiko Schocher385c9ef2012-01-16 21:12:23 +0000852#else
853# define CONFIG_SYS_MAX_I2C_BUS 1
854# define I2C_MULTI_BUS 0
855#endif
Ben Warrenbb99ad62006-09-07 16:50:54 -0400856
Marek Vasutcd7b4e82011-10-25 11:40:57 +0200857/* NOTE: These two functions MUST be always_inline to avoid code growth! */
858static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
859static inline unsigned int I2C_GET_BUS(void)
860{
861 return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
862}
863
864static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
865static inline void I2C_SET_BUS(unsigned int bus)
866{
867 if (I2C_MULTI_BUS)
868 i2c_set_bus_num(bus);
869}
870
Łukasz Majewski7ca8f732012-09-04 23:15:20 +0000871/* Multi I2C definitions */
872enum {
873 I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
874 I2C_8, I2C_9, I2C_10,
875};
876
Rajeshwari Shindea9d2ae72012-12-26 20:03:12 +0000877/**
878 * Get FDT values for i2c bus.
879 *
880 * @param blob Device tree blbo
881 * @return the number of I2C bus
882 */
883void board_i2c_init(const void *blob);
884
885/**
886 * Find the I2C bus number by given a FDT I2C node.
887 *
888 * @param blob Device tree blbo
889 * @param node FDT I2C node to find
890 * @return the number of I2C bus (zero based), or -1 on error
891 */
892int i2c_get_bus_num_fdt(int node);
893
894/**
895 * Reset the I2C bus represented by the given a FDT I2C node.
896 *
897 * @param blob Device tree blbo
898 * @param node FDT I2C node to find
899 * @return 0 if port was reset, -1 if not found
900 */
901int i2c_reset_port_fdt(const void *blob, int node);
Simon Glassc6202d82014-12-10 08:55:47 -0700902
903#endif /* !CONFIG_DM_I2C */
904
wdenk1f045212002-03-10 14:37:15 +0000905#endif /* _I2C_H_ */