Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 2 | /* |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 3 | * 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 | * |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 7 | * (C) Copyright 2001 |
| 8 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. |
| 9 | * |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 10 | * 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 Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 20 | * 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 Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 27 | enum 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 Glass | 0d9dc0c | 2020-01-23 11:48:16 -0700 | [diff] [blame] | 33 | /** enum i2c_speed_mode - standard I2C speed modes */ |
| 34 | enum 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 */ |
| 45 | enum 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 */ |
| 54 | enum i2c_address_mode { |
| 55 | I2C_MODE_7_BIT, |
| 56 | I2C_MODE_10_BIT |
| 57 | }; |
| 58 | |
Simon Glass | d5ff0b9 | 2015-02-05 21:41:33 -0700 | [diff] [blame] | 59 | struct udevice; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 60 | /** |
| 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 Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 66 | * To obtain this structure, use dev_get_parent_platdata(dev) where dev is |
| 67 | * the chip to examine. |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 68 | * |
| 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 Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 74 | * @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 Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 87 | * @emul: Emulator for this chip address (only used for emulation) |
| 88 | */ |
| 89 | struct dm_i2c_chip { |
| 90 | uint chip_addr; |
| 91 | uint offset_len; |
| 92 | uint flags; |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 93 | uint chip_addr_offset_mask; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 94 | #ifdef CONFIG_SANDBOX |
| 95 | struct udevice *emul; |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 96 | bool test_mode; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 97 | #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 Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 107 | * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the |
| 108 | * I2C bus udevice. |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 109 | * |
| 110 | * @speed_hz: Bus speed in hertz (typically 100000) |
Lukasz Majewski | bc9aad6 | 2019-04-04 12:35:34 +0200 | [diff] [blame] | 111 | * @max_transaction_bytes: Maximal size of single I2C transfer |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 112 | */ |
| 113 | struct dm_i2c_bus { |
| 114 | int speed_hz; |
Lukasz Majewski | bc9aad6 | 2019-04-04 12:35:34 +0200 | [diff] [blame] | 115 | int max_transaction_bytes; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Simon Glass | 727ff7d | 2015-07-02 18:15:41 -0600 | [diff] [blame] | 118 | /* |
| 119 | * Not all of these flags are implemented in the U-Boot API |
| 120 | */ |
| 121 | enum 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 | */ |
| 140 | struct 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 | */ |
| 156 | struct i2c_msg_list { |
| 157 | struct i2c_msg *msgs; |
| 158 | uint nmsgs; |
| 159 | }; |
| 160 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 161 | /** |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 162 | * dm_i2c_read() - read bytes from an I2C chip |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 163 | * |
| 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 Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 178 | int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 181 | * dm_i2c_write() - write bytes to an I2C chip |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 182 | * |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 183 | * See notes for dm_i2c_read() above. |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 184 | * |
| 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 Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 192 | int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, |
| 193 | int len); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 194 | |
| 195 | /** |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 196 | * dm_i2c_probe() - probe a particular chip address |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 197 | * |
| 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 Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 208 | int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, |
| 209 | struct udevice **devp); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 210 | |
| 211 | /** |
Simon Glass | 0c70261 | 2015-04-20 12:37:14 -0600 | [diff] [blame] | 212 | * 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 Glass | cefb0ef | 2015-07-02 18:15:40 -0600 | [diff] [blame] | 216 | * @dev: Device to use for transfer |
Simon Glass | 0c70261 | 2015-04-20 12:37:14 -0600 | [diff] [blame] | 217 | * @addr: Address to read from |
| 218 | * @return value read, or -ve on error |
| 219 | */ |
| 220 | int 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 Glass | cefb0ef | 2015-07-02 18:15:40 -0600 | [diff] [blame] | 227 | * @dev: Device to use for transfer |
Simon Glass | 0c70261 | 2015-04-20 12:37:14 -0600 | [diff] [blame] | 228 | * @addr: Address to write to |
| 229 | * @val: Value to write (normally a byte) |
| 230 | * @return 0 on success, -ve on error |
| 231 | */ |
| 232 | int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val); |
| 233 | |
| 234 | /** |
Simon Glass | f281885 | 2015-07-02 18:15:42 -0600 | [diff] [blame] | 235 | * 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 | */ |
| 245 | int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs); |
| 246 | |
| 247 | /** |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 248 | * dm_i2c_set_bus_speed() - set the speed of a bus |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 249 | * |
| 250 | * @bus: Bus to adjust |
| 251 | * @speed: Requested speed in Hz |
| 252 | * @return 0 if OK, -EINVAL for invalid values |
| 253 | */ |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 254 | int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 255 | |
| 256 | /** |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 257 | * dm_i2c_get_bus_speed() - get the speed of a bus |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 258 | * |
| 259 | * @bus: Bus to check |
| 260 | * @return speed of selected I2C bus in Hz, -ve on error |
| 261 | */ |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 262 | int dm_i2c_get_bus_speed(struct udevice *bus); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 263 | |
| 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 | */ |
| 274 | int 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 | */ |
| 283 | int 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 Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 295 | int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len); |
Simon Glass | 9a1589a | 2015-05-04 11:30:58 -0600 | [diff] [blame] | 296 | |
| 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 | */ |
| 302 | int i2c_get_chip_offset_len(struct udevice *dev); |
| 303 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 304 | /** |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 305 | * 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 | */ |
| 314 | int 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 | */ |
| 321 | uint i2c_get_chip_addr_offset_mask(struct udevice *dev); |
| 322 | |
| 323 | /** |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 324 | * 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 | */ |
| 331 | int i2c_deblock(struct udevice *bus); |
| 332 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 333 | /** |
| 334 | * struct dm_i2c_ops - driver operations for I2C uclass |
| 335 | * |
| 336 | * Drivers should support these operations unless otherwise noted. These |
| 337 | * operations are intended to be used by uclass code, not directly from |
| 338 | * other code. |
| 339 | */ |
| 340 | struct dm_i2c_ops { |
| 341 | /** |
| 342 | * xfer() - transfer a list of I2C messages |
| 343 | * |
| 344 | * @bus: Bus to read from |
| 345 | * @msg: List of messages to transfer |
| 346 | * @nmsgs: Number of messages in the list |
| 347 | * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte, |
| 348 | * -ECOMM if the speed cannot be supported, -EPROTO if the chip |
| 349 | * flags cannot be supported, other -ve value on some other error |
| 350 | */ |
| 351 | int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs); |
| 352 | |
| 353 | /** |
| 354 | * probe_chip() - probe for the presense of a chip address |
| 355 | * |
| 356 | * This function is optional. If omitted, the uclass will send a zero |
| 357 | * length message instead. |
| 358 | * |
| 359 | * @bus: Bus to probe |
| 360 | * @chip_addr: Chip address to probe |
| 361 | * @chip_flags: Probe flags (enum dm_i2c_chip_flags) |
| 362 | * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back |
| 363 | * to default probem other -ve value on error |
| 364 | */ |
| 365 | int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags); |
| 366 | |
| 367 | /** |
| 368 | * set_bus_speed() - set the speed of a bus (optional) |
| 369 | * |
| 370 | * The bus speed value will be updated by the uclass if this function |
| 371 | * does not return an error. This method is optional - if it is not |
| 372 | * provided then the driver can read the speed from |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 373 | * dev_get_uclass_priv(bus)->speed_hz |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 374 | * |
| 375 | * @bus: Bus to adjust |
| 376 | * @speed: Requested speed in Hz |
| 377 | * @return 0 if OK, -EINVAL for invalid values |
| 378 | */ |
| 379 | int (*set_bus_speed)(struct udevice *bus, unsigned int speed); |
| 380 | |
| 381 | /** |
| 382 | * get_bus_speed() - get the speed of a bus (optional) |
| 383 | * |
| 384 | * Normally this can be provided by the uclass, but if you want your |
| 385 | * driver to check the bus speed by looking at the hardware, you can |
| 386 | * implement that here. This method is optional. This method would |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 387 | * normally be expected to return dev_get_uclass_priv(bus)->speed_hz. |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 388 | * |
| 389 | * @bus: Bus to check |
| 390 | * @return speed of selected I2C bus in Hz, -ve on error |
| 391 | */ |
| 392 | int (*get_bus_speed)(struct udevice *bus); |
| 393 | |
| 394 | /** |
| 395 | * set_flags() - set the flags for a chip (optional) |
| 396 | * |
| 397 | * This is generally implemented by the uclass, but drivers can |
| 398 | * check the value to ensure that unsupported options are not used. |
| 399 | * This method is optional. If provided, this method will always be |
| 400 | * called when the flags change. |
| 401 | * |
| 402 | * @dev: Chip to adjust |
| 403 | * @flags: New flags value |
| 404 | * @return 0 if OK, -EINVAL if value is unsupported |
| 405 | */ |
| 406 | int (*set_flags)(struct udevice *dev, uint flags); |
| 407 | |
| 408 | /** |
| 409 | * deblock() - recover a bus that is in an unknown state |
| 410 | * |
| 411 | * I2C is a synchronous protocol and resets of the processor in the |
| 412 | * middle of an access can block the I2C Bus until a powerdown of |
| 413 | * the full unit is done. This is because slaves can be stuck |
| 414 | * waiting for addition bus transitions for a transaction that will |
| 415 | * never complete. Resetting the I2C master does not help. The only |
| 416 | * way is to force the bus through a series of transitions to make |
| 417 | * sure that all slaves are done with the transaction. This method |
| 418 | * performs this 'deblocking' if support by the driver. |
| 419 | * |
| 420 | * This method is optional. |
| 421 | */ |
| 422 | int (*deblock)(struct udevice *bus); |
| 423 | }; |
| 424 | |
| 425 | #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops) |
| 426 | |
| 427 | /** |
Simon Glass | 2a80c40 | 2015-08-03 08:19:21 -0600 | [diff] [blame] | 428 | * struct i2c_mux_ops - operations for an I2C mux |
| 429 | * |
| 430 | * The current mux state is expected to be stored in the mux itself since |
| 431 | * it is the only thing that knows how to make things work. The mux can |
| 432 | * record the current state and then avoid switching unless it is necessary. |
| 433 | * So select() can be skipped if the mux is already in the correct state. |
| 434 | * Also deselect() can be made a nop if required. |
| 435 | */ |
| 436 | struct i2c_mux_ops { |
| 437 | /** |
| 438 | * select() - select one of of I2C buses attached to a mux |
| 439 | * |
| 440 | * This will be called when there is no bus currently selected by the |
| 441 | * mux. This method does not need to deselect the old bus since |
| 442 | * deselect() will be already have been called if necessary. |
| 443 | * |
| 444 | * @mux: Mux device |
| 445 | * @bus: I2C bus to select |
| 446 | * @channel: Channel number correponding to the bus to select |
| 447 | * @return 0 if OK, -ve on error |
| 448 | */ |
| 449 | int (*select)(struct udevice *mux, struct udevice *bus, uint channel); |
| 450 | |
| 451 | /** |
| 452 | * deselect() - select one of of I2C buses attached to a mux |
| 453 | * |
| 454 | * This is used to deselect the currently selected I2C bus. |
| 455 | * |
| 456 | * @mux: Mux device |
| 457 | * @bus: I2C bus to deselect |
| 458 | * @channel: Channel number correponding to the bus to deselect |
| 459 | * @return 0 if OK, -ve on error |
| 460 | */ |
| 461 | int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel); |
| 462 | }; |
| 463 | |
| 464 | #define i2c_mux_get_ops(dev) ((struct i2c_mux_ops *)(dev)->driver->ops) |
| 465 | |
| 466 | /** |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 467 | * i2c_get_chip() - get a device to use to access a chip on a bus |
| 468 | * |
| 469 | * This returns the device for the given chip address. The device can then |
| 470 | * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc. |
| 471 | * |
| 472 | * @bus: Bus to examine |
| 473 | * @chip_addr: Chip address for the new device |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 474 | * @offset_len: Length of a register offset in bytes (normally 1) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 475 | * @devp: Returns pointer to new device if found or -ENODEV if not |
| 476 | * found |
| 477 | */ |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 478 | int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len, |
| 479 | struct udevice **devp); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 480 | |
| 481 | /** |
Stefan Roese | c2df68ff | 2015-11-25 07:41:58 +0100 | [diff] [blame] | 482 | * i2c_get_chip_for_busnum() - get a device to use to access a chip on |
| 483 | * a bus number |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 484 | * |
| 485 | * This returns the device for the given chip address on a particular bus |
| 486 | * number. |
| 487 | * |
| 488 | * @busnum: Bus number to examine |
| 489 | * @chip_addr: Chip address for the new device |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 490 | * @offset_len: Length of a register offset in bytes (normally 1) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 491 | * @devp: Returns pointer to new device if found or -ENODEV if not |
| 492 | * found |
| 493 | */ |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 494 | int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len, |
| 495 | struct udevice **devp); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 496 | |
| 497 | /** |
| 498 | * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data |
| 499 | * |
| 500 | * This decodes the chip address from a device tree node and puts it into |
| 501 | * its dm_i2c_chip structure. This should be called in your driver's |
| 502 | * ofdata_to_platdata() method. |
| 503 | * |
| 504 | * @blob: Device tree blob |
| 505 | * @node: Node offset to read from |
| 506 | * @spi: Place to put the decoded information |
| 507 | */ |
Simon Glass | 654e3a0 | 2017-05-18 20:09:30 -0600 | [diff] [blame] | 508 | int i2c_chip_ofdata_to_platdata(struct udevice *dev, struct dm_i2c_chip *chip); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 509 | |
Simon Glass | a8e64c4 | 2015-07-02 18:15:39 -0600 | [diff] [blame] | 510 | /** |
| 511 | * i2c_dump_msgs() - Dump a list of I2C messages |
| 512 | * |
| 513 | * This may be useful for debugging. |
| 514 | * |
| 515 | * @msg: Message list to dump |
| 516 | * @nmsgs: Number of messages |
| 517 | */ |
| 518 | void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs); |
| 519 | |
Simon Glass | 4b0ecc6 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 520 | /** |
| 521 | * i2c_emul_find() - Find an emulator for an i2c sandbox device |
| 522 | * |
| 523 | * This looks at the device's 'emul' phandle |
| 524 | * |
| 525 | * @dev: Device to find an emulator for |
| 526 | * @emulp: Returns the associated emulator, if found * |
| 527 | * @return 0 if OK, -ENOENT or -ENODEV if not found |
| 528 | */ |
| 529 | int i2c_emul_find(struct udevice *dev, struct udevice **emulp); |
| 530 | |
| 531 | /** |
| 532 | * i2c_emul_get_device() - Find the device being emulated |
| 533 | * |
| 534 | * Given an emulator this returns the associated device |
| 535 | * |
| 536 | * @emul: Emulator for the device |
| 537 | * @return device that @emul is emulating |
| 538 | */ |
| 539 | struct udevice *i2c_emul_get_device(struct udevice *emul); |
| 540 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 541 | #ifndef CONFIG_DM_I2C |
| 542 | |
| 543 | /* |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 544 | * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 545 | * |
| 546 | * The implementation MUST NOT use static or global variables if the |
| 547 | * I2C routines are used to read SDRAM configuration information |
| 548 | * because this is done before the memories are initialized. Limited |
| 549 | * use of stack-based variables are OK (the initial stack size is |
| 550 | * limited). |
| 551 | * |
| 552 | * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING |
| 553 | */ |
| 554 | |
| 555 | /* |
| 556 | * Configuration items. |
| 557 | */ |
| 558 | #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */ |
| 559 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 560 | #if !defined(CONFIG_SYS_I2C_MAX_HOPS) |
| 561 | /* no muxes used bus = i2c adapters */ |
| 562 | #define CONFIG_SYS_I2C_DIRECT_BUS 1 |
| 563 | #define CONFIG_SYS_I2C_MAX_HOPS 0 |
| 564 | #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c) |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 565 | #else |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 566 | /* we use i2c muxes */ |
| 567 | #undef CONFIG_SYS_I2C_DIRECT_BUS |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 568 | #endif |
| 569 | |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 570 | /* define the I2C bus number for RTC and DTT if not already done */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 571 | #if !defined(CONFIG_SYS_RTC_BUS_NUM) |
| 572 | #define CONFIG_SYS_RTC_BUS_NUM 0 |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 573 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 574 | #if !defined(CONFIG_SYS_SPD_BUS_NUM) |
| 575 | #define CONFIG_SYS_SPD_BUS_NUM 0 |
Matthias Fuchs | 66067c3 | 2007-03-08 16:20:32 +0100 | [diff] [blame] | 576 | #endif |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 577 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 578 | struct i2c_adapter { |
| 579 | void (*init)(struct i2c_adapter *adap, int speed, |
| 580 | int slaveaddr); |
| 581 | int (*probe)(struct i2c_adapter *adap, uint8_t chip); |
| 582 | int (*read)(struct i2c_adapter *adap, uint8_t chip, |
| 583 | uint addr, int alen, uint8_t *buffer, |
| 584 | int len); |
| 585 | int (*write)(struct i2c_adapter *adap, uint8_t chip, |
| 586 | uint addr, int alen, uint8_t *buffer, |
| 587 | int len); |
| 588 | uint (*set_bus_speed)(struct i2c_adapter *adap, |
| 589 | uint speed); |
| 590 | int speed; |
Hannes Petermaier | d588505 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 591 | int waitdelay; |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 592 | int slaveaddr; |
| 593 | int init_done; |
| 594 | int hwadapnr; |
| 595 | char *name; |
| 596 | }; |
| 597 | |
| 598 | #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ |
| 599 | _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \ |
| 600 | { \ |
| 601 | .init = _init, \ |
| 602 | .probe = _probe, \ |
| 603 | .read = _read, \ |
| 604 | .write = _write, \ |
| 605 | .set_bus_speed = _set_speed, \ |
| 606 | .speed = _speed, \ |
| 607 | .slaveaddr = _slaveaddr, \ |
| 608 | .init_done = 0, \ |
| 609 | .hwadapnr = _hwadapnr, \ |
| 610 | .name = #_name \ |
| 611 | }; |
| 612 | |
| 613 | #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \ |
| 614 | _set_speed, _speed, _slaveaddr, _hwadapnr) \ |
| 615 | ll_entry_declare(struct i2c_adapter, _name, i2c) = \ |
| 616 | U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \ |
| 617 | _set_speed, _speed, _slaveaddr, _hwadapnr, _name); |
| 618 | |
| 619 | struct i2c_adapter *i2c_get_adapter(int index); |
| 620 | |
| 621 | #ifndef CONFIG_SYS_I2C_DIRECT_BUS |
| 622 | struct i2c_mux { |
| 623 | int id; |
| 624 | char name[16]; |
| 625 | }; |
| 626 | |
| 627 | struct i2c_next_hop { |
| 628 | struct i2c_mux mux; |
| 629 | uint8_t chip; |
| 630 | uint8_t channel; |
| 631 | }; |
| 632 | |
| 633 | struct i2c_bus_hose { |
| 634 | int adapter; |
| 635 | struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS]; |
| 636 | }; |
| 637 | #define I2C_NULL_HOP {{-1, ""}, 0, 0} |
| 638 | extern struct i2c_bus_hose i2c_bus[]; |
| 639 | |
| 640 | #define I2C_ADAPTER(bus) i2c_bus[bus].adapter |
| 641 | #else |
| 642 | #define I2C_ADAPTER(bus) bus |
| 643 | #endif |
| 644 | #define I2C_BUS gd->cur_i2c_bus |
| 645 | |
| 646 | #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus)) |
| 647 | #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus) |
| 648 | #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr) |
| 649 | |
| 650 | #ifndef CONFIG_SYS_I2C_DIRECT_BUS |
| 651 | #define I2C_MUX_PCA9540_ID 1 |
| 652 | #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"} |
| 653 | #define I2C_MUX_PCA9542_ID 2 |
| 654 | #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"} |
| 655 | #define I2C_MUX_PCA9544_ID 3 |
| 656 | #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"} |
| 657 | #define I2C_MUX_PCA9547_ID 4 |
| 658 | #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"} |
Michael Burr | a04a55c | 2013-09-23 22:35:45 +0000 | [diff] [blame] | 659 | #define I2C_MUX_PCA9548_ID 5 |
| 660 | #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"} |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 661 | #endif |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 662 | |
Heiko Schocher | dc7d22a | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 663 | #ifndef I2C_SOFT_DECLARATIONS |
Heiko Schocher | 71cb3e9 | 2017-06-07 17:33:10 +0200 | [diff] [blame] | 664 | # if (defined(CONFIG_AT91RM9200) || \ |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 665 | defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \ |
Andreas Bießmann | a864979 | 2013-10-30 15:18:18 +0100 | [diff] [blame] | 666 | defined(CONFIG_AT91SAM9263)) |
esw@bus-elektronik.de | 5082bab | 2011-12-20 06:05:30 +0000 | [diff] [blame] | 667 | # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA; |
Heiko Schocher | dc7d22a | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 668 | # else |
| 669 | # define I2C_SOFT_DECLARATIONS |
| 670 | # endif |
| 671 | #endif |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 672 | |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 673 | /* |
| 674 | * Many boards/controllers/drivers don't support an I2C slave interface so |
| 675 | * provide a default slave address for them for use in common code. A real |
| 676 | * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does |
| 677 | * support a slave interface. |
| 678 | */ |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 679 | #ifndef CONFIG_SYS_I2C_SLAVE |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 680 | #define CONFIG_SYS_I2C_SLAVE 0xfe |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 681 | #endif |
| 682 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 683 | /* |
| 684 | * Initialization, must be called once on start up, may be called |
| 685 | * repeatedly to change the speed and slave addresses. |
| 686 | */ |
Yuan Yao | d40c885 | 2016-06-08 18:24:51 +0800 | [diff] [blame] | 687 | #ifdef CONFIG_SYS_I2C_EARLY_INIT |
| 688 | void i2c_early_init_f(void); |
| 689 | #endif |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 690 | void i2c_init(int speed, int slaveaddr); |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 691 | void i2c_init_board(void); |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 692 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 693 | #ifdef CONFIG_SYS_I2C |
| 694 | /* |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 695 | * i2c_get_bus_num: |
| 696 | * |
| 697 | * Returns index of currently active I2C bus. Zero-based. |
| 698 | */ |
| 699 | unsigned int i2c_get_bus_num(void); |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 700 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 701 | /* |
| 702 | * i2c_set_bus_num: |
| 703 | * |
| 704 | * Change the active I2C bus. Subsequent read/write calls will |
| 705 | * go to this one. |
| 706 | * |
| 707 | * bus - bus index, zero based |
| 708 | * |
| 709 | * Returns: 0 on success, not 0 on failure |
| 710 | * |
| 711 | */ |
| 712 | int i2c_set_bus_num(unsigned int bus); |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 713 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 714 | /* |
| 715 | * i2c_init_all(): |
| 716 | * |
| 717 | * Initializes all I2C adapters in the system. All i2c_adap structures must |
| 718 | * be initialized beforehead with function pointers and data, including |
| 719 | * speed and slaveaddr. Returns 0 on success, non-0 on failure. |
| 720 | */ |
| 721 | void i2c_init_all(void); |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 722 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 723 | /* |
| 724 | * Probe the given I2C chip address. Returns 0 if a chip responded, |
| 725 | * not 0 on failure. |
| 726 | */ |
| 727 | int i2c_probe(uint8_t chip); |
| 728 | |
| 729 | /* |
| 730 | * Read/Write interface: |
| 731 | * chip: I2C chip address, range 0..127 |
| 732 | * addr: Memory (register) address within the chip |
| 733 | * alen: Number of bytes to use for addr (typically 1, 2 for larger |
| 734 | * memories, 0 for register type devices with only one |
| 735 | * register) |
| 736 | * buffer: Where to read/write the data |
| 737 | * len: How many bytes to read/write |
| 738 | * |
| 739 | * Returns: 0 on success, not 0 on failure |
| 740 | */ |
| 741 | int i2c_read(uint8_t chip, unsigned int addr, int alen, |
| 742 | uint8_t *buffer, int len); |
| 743 | |
| 744 | int i2c_write(uint8_t chip, unsigned int addr, int alen, |
| 745 | uint8_t *buffer, int len); |
| 746 | |
| 747 | /* |
| 748 | * Utility routines to read/write registers. |
| 749 | */ |
| 750 | uint8_t i2c_reg_read(uint8_t addr, uint8_t reg); |
| 751 | |
| 752 | void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val); |
| 753 | |
| 754 | /* |
| 755 | * i2c_set_bus_speed: |
| 756 | * |
| 757 | * Change the speed of the active I2C bus |
| 758 | * |
| 759 | * speed - bus speed in Hz |
| 760 | * |
| 761 | * Returns: new bus speed |
| 762 | * |
| 763 | */ |
| 764 | unsigned int i2c_set_bus_speed(unsigned int speed); |
| 765 | |
| 766 | /* |
| 767 | * i2c_get_bus_speed: |
| 768 | * |
| 769 | * Returns speed of currently active I2C bus in Hz |
| 770 | */ |
| 771 | |
| 772 | unsigned int i2c_get_bus_speed(void); |
| 773 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 774 | #else |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 775 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 776 | /* |
| 777 | * Probe the given I2C chip address. Returns 0 if a chip responded, |
| 778 | * not 0 on failure. |
| 779 | */ |
| 780 | int i2c_probe(uchar chip); |
| 781 | |
| 782 | /* |
| 783 | * Read/Write interface: |
| 784 | * chip: I2C chip address, range 0..127 |
| 785 | * addr: Memory (register) address within the chip |
| 786 | * alen: Number of bytes to use for addr (typically 1, 2 for larger |
| 787 | * memories, 0 for register type devices with only one |
| 788 | * register) |
| 789 | * buffer: Where to read/write the data |
| 790 | * len: How many bytes to read/write |
| 791 | * |
| 792 | * Returns: 0 on success, not 0 on failure |
| 793 | */ |
| 794 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len); |
| 795 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len); |
| 796 | |
| 797 | /* |
| 798 | * Utility routines to read/write registers. |
| 799 | */ |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 800 | static inline u8 i2c_reg_read(u8 addr, u8 reg) |
| 801 | { |
| 802 | u8 buf; |
| 803 | |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 804 | #ifdef DEBUG |
| 805 | printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg); |
| 806 | #endif |
| 807 | |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 808 | i2c_read(addr, reg, 1, &buf, 1); |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 809 | |
| 810 | return buf; |
| 811 | } |
| 812 | |
| 813 | static inline void i2c_reg_write(u8 addr, u8 reg, u8 val) |
| 814 | { |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 815 | #ifdef DEBUG |
| 816 | printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n", |
| 817 | __func__, addr, reg, val); |
| 818 | #endif |
| 819 | |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 820 | i2c_write(addr, reg, 1, &val, 1); |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 821 | } |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 822 | |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 823 | /* |
| 824 | * Functions for setting the current I2C bus and its speed |
| 825 | */ |
| 826 | |
| 827 | /* |
| 828 | * i2c_set_bus_num: |
| 829 | * |
| 830 | * Change the active I2C bus. Subsequent read/write calls will |
| 831 | * go to this one. |
| 832 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 833 | * bus - bus index, zero based |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 834 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 835 | * Returns: 0 on success, not 0 on failure |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 836 | * |
| 837 | */ |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 838 | int i2c_set_bus_num(unsigned int bus); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 839 | |
| 840 | /* |
| 841 | * i2c_get_bus_num: |
| 842 | * |
| 843 | * Returns index of currently active I2C bus. Zero-based. |
| 844 | */ |
| 845 | |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 846 | unsigned int i2c_get_bus_num(void); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 847 | |
| 848 | /* |
| 849 | * i2c_set_bus_speed: |
| 850 | * |
| 851 | * Change the speed of the active I2C bus |
| 852 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 853 | * speed - bus speed in Hz |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 854 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 855 | * Returns: 0 on success, not 0 on failure |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 856 | * |
| 857 | */ |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 858 | int i2c_set_bus_speed(unsigned int); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 859 | |
| 860 | /* |
| 861 | * i2c_get_bus_speed: |
| 862 | * |
| 863 | * Returns speed of currently active I2C bus in Hz |
| 864 | */ |
| 865 | |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 866 | unsigned int i2c_get_bus_speed(void); |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 867 | #endif /* CONFIG_SYS_I2C */ |
| 868 | |
| 869 | /* |
| 870 | * only for backwardcompatibility, should go away if we switched |
| 871 | * completely to new multibus support. |
| 872 | */ |
| 873 | #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS) |
| 874 | # if !defined(CONFIG_SYS_MAX_I2C_BUS) |
| 875 | # define CONFIG_SYS_MAX_I2C_BUS 2 |
| 876 | # endif |
Łukasz Majewski | 1a6a2a9 | 2013-08-16 15:31:45 +0200 | [diff] [blame] | 877 | # define I2C_MULTI_BUS 1 |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 878 | #else |
| 879 | # define CONFIG_SYS_MAX_I2C_BUS 1 |
| 880 | # define I2C_MULTI_BUS 0 |
| 881 | #endif |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 882 | |
Marek Vasut | 513b820 | 2011-10-25 11:40:57 +0200 | [diff] [blame] | 883 | /* NOTE: These two functions MUST be always_inline to avoid code growth! */ |
| 884 | static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline)); |
| 885 | static inline unsigned int I2C_GET_BUS(void) |
| 886 | { |
| 887 | return I2C_MULTI_BUS ? i2c_get_bus_num() : 0; |
| 888 | } |
| 889 | |
| 890 | static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline)); |
| 891 | static inline void I2C_SET_BUS(unsigned int bus) |
| 892 | { |
| 893 | if (I2C_MULTI_BUS) |
| 894 | i2c_set_bus_num(bus); |
| 895 | } |
| 896 | |
Łukasz Majewski | b2d7652 | 2012-09-04 23:15:20 +0000 | [diff] [blame] | 897 | /* Multi I2C definitions */ |
| 898 | enum { |
| 899 | I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7, |
| 900 | I2C_8, I2C_9, I2C_10, |
| 901 | }; |
| 902 | |
Rajeshwari Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 903 | /** |
| 904 | * Get FDT values for i2c bus. |
| 905 | * |
| 906 | * @param blob Device tree blbo |
| 907 | * @return the number of I2C bus |
| 908 | */ |
| 909 | void board_i2c_init(const void *blob); |
| 910 | |
| 911 | /** |
| 912 | * Find the I2C bus number by given a FDT I2C node. |
| 913 | * |
| 914 | * @param blob Device tree blbo |
| 915 | * @param node FDT I2C node to find |
| 916 | * @return the number of I2C bus (zero based), or -1 on error |
| 917 | */ |
| 918 | int i2c_get_bus_num_fdt(int node); |
| 919 | |
| 920 | /** |
| 921 | * Reset the I2C bus represented by the given a FDT I2C node. |
| 922 | * |
| 923 | * @param blob Device tree blbo |
| 924 | * @param node FDT I2C node to find |
| 925 | * @return 0 if port was reset, -1 if not found |
| 926 | */ |
| 927 | int i2c_reset_port_fdt(const void *blob, int node); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 928 | |
| 929 | #endif /* !CONFIG_DM_I2C */ |
| 930 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 931 | #endif /* _I2C_H_ */ |