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 | d5ff0b9 | 2015-02-05 21:41:33 -0700 | [diff] [blame] | 33 | struct udevice; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 34 | /** |
| 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 Glass | 713c3f0 | 2015-01-25 08:27:13 -0700 | [diff] [blame] | 40 | * To obtain this structure, use dev_get_parent_platdata(dev) where dev is |
| 41 | * the chip to examine. |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 42 | * |
| 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 Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 48 | * @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 Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 61 | * @emul: Emulator for this chip address (only used for emulation) |
| 62 | */ |
| 63 | struct dm_i2c_chip { |
| 64 | uint chip_addr; |
| 65 | uint offset_len; |
| 66 | uint flags; |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 67 | uint chip_addr_offset_mask; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 68 | #ifdef CONFIG_SANDBOX |
| 69 | struct udevice *emul; |
Simon Glass | 4c70ed9 | 2015-04-20 12:37:15 -0600 | [diff] [blame] | 70 | bool test_mode; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 71 | #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 Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 81 | * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the |
| 82 | * I2C bus udevice. |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 83 | * |
| 84 | * @speed_hz: Bus speed in hertz (typically 100000) |
Lukasz Majewski | bc9aad6 | 2019-04-04 12:35:34 +0200 | [diff] [blame] | 85 | * @max_transaction_bytes: Maximal size of single I2C transfer |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 86 | */ |
| 87 | struct dm_i2c_bus { |
| 88 | int speed_hz; |
Lukasz Majewski | bc9aad6 | 2019-04-04 12:35:34 +0200 | [diff] [blame] | 89 | int max_transaction_bytes; |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Simon Glass | 727ff7d | 2015-07-02 18:15:41 -0600 | [diff] [blame] | 92 | /* |
| 93 | * Not all of these flags are implemented in the U-Boot API |
| 94 | */ |
| 95 | enum 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 | */ |
| 114 | struct 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 | */ |
| 130 | struct i2c_msg_list { |
| 131 | struct i2c_msg *msgs; |
| 132 | uint nmsgs; |
| 133 | }; |
| 134 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 135 | /** |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 136 | * dm_i2c_read() - read bytes from an I2C chip |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 137 | * |
| 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 Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 152 | 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] | 153 | |
| 154 | /** |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 155 | * dm_i2c_write() - write bytes to an I2C chip |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 156 | * |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 157 | * See notes for dm_i2c_read() above. |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 158 | * |
| 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 Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 166 | int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer, |
| 167 | int len); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
Simon Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 170 | * dm_i2c_probe() - probe a particular chip address |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 171 | * |
| 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 Glass | 7d72276 | 2015-01-12 18:02:07 -0700 | [diff] [blame] | 182 | int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags, |
| 183 | struct udevice **devp); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 184 | |
| 185 | /** |
Simon Glass | 0c70261 | 2015-04-20 12:37:14 -0600 | [diff] [blame] | 186 | * 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 Glass | cefb0ef | 2015-07-02 18:15:40 -0600 | [diff] [blame] | 190 | * @dev: Device to use for transfer |
Simon Glass | 0c70261 | 2015-04-20 12:37:14 -0600 | [diff] [blame] | 191 | * @addr: Address to read from |
| 192 | * @return value read, or -ve on error |
| 193 | */ |
| 194 | int 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 Glass | cefb0ef | 2015-07-02 18:15:40 -0600 | [diff] [blame] | 201 | * @dev: Device to use for transfer |
Simon Glass | 0c70261 | 2015-04-20 12:37:14 -0600 | [diff] [blame] | 202 | * @addr: Address to write to |
| 203 | * @val: Value to write (normally a byte) |
| 204 | * @return 0 on success, -ve on error |
| 205 | */ |
| 206 | int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val); |
| 207 | |
| 208 | /** |
Simon Glass | f281885 | 2015-07-02 18:15:42 -0600 | [diff] [blame] | 209 | * 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 | */ |
| 219 | int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs); |
| 220 | |
| 221 | /** |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 222 | * dm_i2c_set_bus_speed() - set the speed of a bus |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 223 | * |
| 224 | * @bus: Bus to adjust |
| 225 | * @speed: Requested speed in Hz |
| 226 | * @return 0 if OK, -EINVAL for invalid values |
| 227 | */ |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 228 | int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 229 | |
| 230 | /** |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 231 | * dm_i2c_get_bus_speed() - get the speed of a bus |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 232 | * |
| 233 | * @bus: Bus to check |
| 234 | * @return speed of selected I2C bus in Hz, -ve on error |
| 235 | */ |
Simon Glass | e4e8ff2 | 2015-02-05 21:41:32 -0700 | [diff] [blame] | 236 | int dm_i2c_get_bus_speed(struct udevice *bus); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 237 | |
| 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 | */ |
| 248 | int 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 | */ |
| 257 | int 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 Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 269 | int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len); |
Simon Glass | 9a1589a | 2015-05-04 11:30:58 -0600 | [diff] [blame] | 270 | |
| 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 | */ |
| 276 | int i2c_get_chip_offset_len(struct udevice *dev); |
| 277 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 278 | /** |
Robert Beckett | 5f8e41a | 2019-10-28 17:44:57 +0000 | [diff] [blame] | 279 | * 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 | */ |
| 288 | int 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 | */ |
| 295 | uint i2c_get_chip_addr_offset_mask(struct udevice *dev); |
| 296 | |
| 297 | /** |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 298 | * 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 | */ |
| 305 | int i2c_deblock(struct udevice *bus); |
| 306 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 307 | /** |
| 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 | */ |
| 314 | struct 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 Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 347 | * dev_get_uclass_priv(bus)->speed_hz |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 348 | * |
| 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 Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 361 | * normally be expected to return dev_get_uclass_priv(bus)->speed_hz. |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 362 | * |
| 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 Glass | 2a80c40 | 2015-08-03 08:19:21 -0600 | [diff] [blame] | 402 | * 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 | */ |
| 410 | struct 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 Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 441 | * 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 Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 448 | * @offset_len: Length of a register offset in bytes (normally 1) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 449 | * @devp: Returns pointer to new device if found or -ENODEV if not |
| 450 | * found |
| 451 | */ |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 452 | int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len, |
| 453 | struct udevice **devp); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 454 | |
| 455 | /** |
Stefan Roese | c2df68ff | 2015-11-25 07:41:58 +0100 | [diff] [blame] | 456 | * i2c_get_chip_for_busnum() - get a device to use to access a chip on |
| 457 | * a bus number |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 458 | * |
| 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 Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 464 | * @offset_len: Length of a register offset in bytes (normally 1) |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 465 | * @devp: Returns pointer to new device if found or -ENODEV if not |
| 466 | * found |
| 467 | */ |
Simon Glass | a2723ae | 2015-01-25 08:26:55 -0700 | [diff] [blame] | 468 | int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len, |
| 469 | struct udevice **devp); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 470 | |
| 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 Glass | 654e3a0 | 2017-05-18 20:09:30 -0600 | [diff] [blame] | 482 | 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] | 483 | |
Simon Glass | a8e64c4 | 2015-07-02 18:15:39 -0600 | [diff] [blame] | 484 | /** |
| 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 | */ |
| 492 | void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs); |
| 493 | |
Simon Glass | 4b0ecc6 | 2018-11-18 08:14:33 -0700 | [diff] [blame] | 494 | /** |
| 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 | */ |
| 503 | int 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 | */ |
| 513 | struct udevice *i2c_emul_get_device(struct udevice *emul); |
| 514 | |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 515 | #ifndef CONFIG_DM_I2C |
| 516 | |
| 517 | /* |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 518 | * 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 Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 534 | #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 Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 539 | #else |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 540 | /* we use i2c muxes */ |
| 541 | #undef CONFIG_SYS_I2C_DIRECT_BUS |
Stefan Roese | d07117e | 2007-02-20 10:27:08 +0100 | [diff] [blame] | 542 | #endif |
| 543 | |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 544 | /* 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] | 545 | #if !defined(CONFIG_SYS_RTC_BUS_NUM) |
| 546 | #define CONFIG_SYS_RTC_BUS_NUM 0 |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 547 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 548 | #if !defined(CONFIG_SYS_SPD_BUS_NUM) |
| 549 | #define CONFIG_SYS_SPD_BUS_NUM 0 |
Matthias Fuchs | 66067c3 | 2007-03-08 16:20:32 +0100 | [diff] [blame] | 550 | #endif |
Stefan Roese | 9de4fcb | 2007-03-01 07:03:25 +0100 | [diff] [blame] | 551 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 552 | struct 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 Petermaier | d588505 | 2014-02-03 21:22:18 +0100 | [diff] [blame] | 565 | int waitdelay; |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 566 | 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 | |
| 593 | struct i2c_adapter *i2c_get_adapter(int index); |
| 594 | |
| 595 | #ifndef CONFIG_SYS_I2C_DIRECT_BUS |
| 596 | struct i2c_mux { |
| 597 | int id; |
| 598 | char name[16]; |
| 599 | }; |
| 600 | |
| 601 | struct i2c_next_hop { |
| 602 | struct i2c_mux mux; |
| 603 | uint8_t chip; |
| 604 | uint8_t channel; |
| 605 | }; |
| 606 | |
| 607 | struct 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} |
| 612 | extern 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 Burr | a04a55c | 2013-09-23 22:35:45 +0000 | [diff] [blame] | 633 | #define I2C_MUX_PCA9548_ID 5 |
| 634 | #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"} |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 635 | #endif |
wdenk | 6b58f33 | 2003-03-14 20:47:52 +0000 | [diff] [blame] | 636 | |
Heiko Schocher | dc7d22a | 2008-10-15 09:35:26 +0200 | [diff] [blame] | 637 | #ifndef I2C_SOFT_DECLARATIONS |
Heiko Schocher | 71cb3e9 | 2017-06-07 17:33:10 +0200 | [diff] [blame] | 638 | # if (defined(CONFIG_AT91RM9200) || \ |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 639 | defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \ |
Andreas Bießmann | a864979 | 2013-10-30 15:18:18 +0100 | [diff] [blame] | 640 | defined(CONFIG_AT91SAM9263)) |
esw@bus-elektronik.de | 5082bab | 2011-12-20 06:05:30 +0000 | [diff] [blame] | 641 | # 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] | 642 | # else |
| 643 | # define I2C_SOFT_DECLARATIONS |
| 644 | # endif |
| 645 | #endif |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 646 | |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 647 | /* |
| 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 | */ |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 653 | #ifndef CONFIG_SYS_I2C_SLAVE |
Peter Tyser | 769069e | 2009-04-24 15:34:05 -0500 | [diff] [blame] | 654 | #define CONFIG_SYS_I2C_SLAVE 0xfe |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 655 | #endif |
| 656 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 657 | /* |
| 658 | * Initialization, must be called once on start up, may be called |
| 659 | * repeatedly to change the speed and slave addresses. |
| 660 | */ |
Yuan Yao | d40c885 | 2016-06-08 18:24:51 +0800 | [diff] [blame] | 661 | #ifdef CONFIG_SYS_I2C_EARLY_INIT |
| 662 | void i2c_early_init_f(void); |
| 663 | #endif |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 664 | void i2c_init(int speed, int slaveaddr); |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 665 | void i2c_init_board(void); |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 666 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 667 | #ifdef CONFIG_SYS_I2C |
| 668 | /* |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 669 | * i2c_get_bus_num: |
| 670 | * |
| 671 | * Returns index of currently active I2C bus. Zero-based. |
| 672 | */ |
| 673 | unsigned int i2c_get_bus_num(void); |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 674 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 675 | /* |
| 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 | */ |
| 686 | int i2c_set_bus_num(unsigned int bus); |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 687 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 688 | /* |
| 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 | */ |
| 695 | void i2c_init_all(void); |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 696 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 697 | /* |
| 698 | * Probe the given I2C chip address. Returns 0 if a chip responded, |
| 699 | * not 0 on failure. |
| 700 | */ |
| 701 | int 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 | */ |
| 715 | int i2c_read(uint8_t chip, unsigned int addr, int alen, |
| 716 | uint8_t *buffer, int len); |
| 717 | |
| 718 | int 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 | */ |
| 724 | uint8_t i2c_reg_read(uint8_t addr, uint8_t reg); |
| 725 | |
| 726 | void 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 | */ |
| 738 | unsigned 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 | |
| 746 | unsigned int i2c_get_bus_speed(void); |
| 747 | |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 748 | #else |
Heiko Schocher | 6ee861b | 2008-10-15 09:39:47 +0200 | [diff] [blame] | 749 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 750 | /* |
| 751 | * Probe the given I2C chip address. Returns 0 if a chip responded, |
| 752 | * not 0 on failure. |
| 753 | */ |
| 754 | int 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 | */ |
| 768 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len); |
| 769 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len); |
| 770 | |
| 771 | /* |
| 772 | * Utility routines to read/write registers. |
| 773 | */ |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 774 | static inline u8 i2c_reg_read(u8 addr, u8 reg) |
| 775 | { |
| 776 | u8 buf; |
| 777 | |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 778 | #ifdef DEBUG |
| 779 | printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg); |
| 780 | #endif |
| 781 | |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 782 | i2c_read(addr, reg, 1, &buf, 1); |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 783 | |
| 784 | return buf; |
| 785 | } |
| 786 | |
| 787 | static inline void i2c_reg_write(u8 addr, u8 reg, u8 val) |
| 788 | { |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 789 | #ifdef DEBUG |
| 790 | printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n", |
| 791 | __func__, addr, reg, val); |
| 792 | #endif |
| 793 | |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 794 | i2c_write(addr, reg, 1, &val, 1); |
Timur Tabi | 13d598f | 2008-12-03 11:28:30 -0600 | [diff] [blame] | 795 | } |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 796 | |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 797 | /* |
| 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 Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 807 | * bus - bus index, zero based |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 808 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 809 | * Returns: 0 on success, not 0 on failure |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 810 | * |
| 811 | */ |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 812 | int i2c_set_bus_num(unsigned int bus); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 813 | |
| 814 | /* |
| 815 | * i2c_get_bus_num: |
| 816 | * |
| 817 | * Returns index of currently active I2C bus. Zero-based. |
| 818 | */ |
| 819 | |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 820 | unsigned int i2c_get_bus_num(void); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 821 | |
| 822 | /* |
| 823 | * i2c_set_bus_speed: |
| 824 | * |
| 825 | * Change the speed of the active I2C bus |
| 826 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 827 | * speed - bus speed in Hz |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 828 | * |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 829 | * Returns: 0 on success, not 0 on failure |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 830 | * |
| 831 | */ |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 832 | int i2c_set_bus_speed(unsigned int); |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 833 | |
| 834 | /* |
| 835 | * i2c_get_bus_speed: |
| 836 | * |
| 837 | * Returns speed of currently active I2C bus in Hz |
| 838 | */ |
| 839 | |
Timur Tabi | c0b114a | 2006-10-31 21:23:16 -0600 | [diff] [blame] | 840 | unsigned int i2c_get_bus_speed(void); |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 841 | #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 Majewski | 1a6a2a9 | 2013-08-16 15:31:45 +0200 | [diff] [blame] | 851 | # define I2C_MULTI_BUS 1 |
Heiko Schocher | e7d9c4f | 2012-01-16 21:12:23 +0000 | [diff] [blame] | 852 | #else |
| 853 | # define CONFIG_SYS_MAX_I2C_BUS 1 |
| 854 | # define I2C_MULTI_BUS 0 |
| 855 | #endif |
Ben Warren | 4565715 | 2006-09-07 16:50:54 -0400 | [diff] [blame] | 856 | |
Marek Vasut | 513b820 | 2011-10-25 11:40:57 +0200 | [diff] [blame] | 857 | /* NOTE: These two functions MUST be always_inline to avoid code growth! */ |
| 858 | static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline)); |
| 859 | static inline unsigned int I2C_GET_BUS(void) |
| 860 | { |
| 861 | return I2C_MULTI_BUS ? i2c_get_bus_num() : 0; |
| 862 | } |
| 863 | |
| 864 | static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline)); |
| 865 | static inline void I2C_SET_BUS(unsigned int bus) |
| 866 | { |
| 867 | if (I2C_MULTI_BUS) |
| 868 | i2c_set_bus_num(bus); |
| 869 | } |
| 870 | |
Łukasz Majewski | b2d7652 | 2012-09-04 23:15:20 +0000 | [diff] [blame] | 871 | /* Multi I2C definitions */ |
| 872 | enum { |
| 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 Shinde | 53cfac5 | 2012-12-26 20:03:12 +0000 | [diff] [blame] | 877 | /** |
| 878 | * Get FDT values for i2c bus. |
| 879 | * |
| 880 | * @param blob Device tree blbo |
| 881 | * @return the number of I2C bus |
| 882 | */ |
| 883 | void 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 | */ |
| 892 | int 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 | */ |
| 901 | int i2c_reset_port_fdt(const void *blob, int node); |
Simon Glass | c7a5890 | 2014-12-10 08:55:47 -0700 | [diff] [blame] | 902 | |
| 903 | #endif /* !CONFIG_DM_I2C */ |
| 904 | |
wdenk | 1f04521 | 2002-03-10 14:37:15 +0000 | [diff] [blame] | 905 | #endif /* _I2C_H_ */ |