i2c.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  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. *
  7. * (C) Copyright 2001
  8. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  9. *
  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. #ifndef _I2C_H_
  16. #define _I2C_H_
  17. #include <linker_lists.h>
  18. /*
  19. * For now there are essentially two parts to this file - driver model
  20. * here at the top, and the older code below (with CONFIG_SYS_I2C_LEGACY being
  21. * most recent). The plan is to migrate everything to driver model.
  22. * The driver model structures and API are separate as they are different
  23. * enough as to be incompatible for compilation purposes.
  24. */
  25. enum dm_i2c_chip_flags {
  26. DM_I2C_CHIP_10BIT = 1 << 0, /* Use 10-bit addressing */
  27. DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send address for each read byte */
  28. DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address for each write byte */
  29. };
  30. /** enum i2c_speed_mode - standard I2C speed modes */
  31. enum i2c_speed_mode {
  32. IC_SPEED_MODE_STANDARD,
  33. IC_SPEED_MODE_FAST,
  34. IC_SPEED_MODE_FAST_PLUS,
  35. IC_SPEED_MODE_HIGH,
  36. IC_SPEED_MODE_FAST_ULTRA,
  37. IC_SPEED_MODE_COUNT,
  38. };
  39. /** enum i2c_speed_rate - standard I2C speeds in Hz */
  40. enum i2c_speed_rate {
  41. I2C_SPEED_STANDARD_RATE = 100000,
  42. I2C_SPEED_FAST_RATE = 400000,
  43. I2C_SPEED_FAST_PLUS_RATE = 1000000,
  44. I2C_SPEED_HIGH_RATE = 3400000,
  45. I2C_SPEED_FAST_ULTRA_RATE = 5000000,
  46. };
  47. /** enum i2c_address_mode - available address modes */
  48. enum i2c_address_mode {
  49. I2C_MODE_7_BIT,
  50. I2C_MODE_10_BIT
  51. };
  52. /** enum i2c_device_t - Types of I2C devices, used for compatible strings */
  53. enum i2c_device_t {
  54. I2C_DEVICE_GENERIC,
  55. I2C_DEVICE_HID_OVER_I2C,
  56. };
  57. struct udevice;
  58. /**
  59. * struct dm_i2c_chip - information about an i2c chip
  60. *
  61. * An I2C chip is a device on the I2C bus. It sits at a particular address
  62. * and normally supports 7-bit or 10-bit addressing.
  63. *
  64. * To obtain this structure, use dev_get_parent_plat(dev) where dev is
  65. * the chip to examine.
  66. *
  67. * @chip_addr: Chip address on bus
  68. * @offset_len: Length of offset in bytes. A single byte offset can
  69. * represent up to 256 bytes. A value larger than 1 may be
  70. * needed for larger devices.
  71. * @flags: Flags for this chip (dm_i2c_chip_flags)
  72. * @chip_addr_offset_mask: Mask of offset bits within chip_addr. Used for
  73. * devices which steal addresses as part of offset.
  74. * If offset_len is zero, then the offset is encoded
  75. * completely within the chip address itself.
  76. * e.g. a devce with chip address of 0x2c with 512
  77. * registers might use the bottom bit of the address
  78. * to indicate which half of the address space is being
  79. * accessed while still only using 1 byte offset.
  80. * This means it will respond to chip address 0x2c and
  81. * 0x2d.
  82. * A real world example is the Atmel AT24C04. It's
  83. * datasheet explains it's usage of this addressing
  84. * mode.
  85. * @emul: Emulator for this chip address (only used for emulation)
  86. * @emul_idx: Emulator index, used for of-platdata and set by each i2c chip's
  87. * bind() method. This allows i2c_emul_find() to work with of-platdata.
  88. */
  89. struct dm_i2c_chip {
  90. uint chip_addr;
  91. uint offset_len;
  92. uint flags;
  93. uint chip_addr_offset_mask;
  94. #ifdef CONFIG_SANDBOX
  95. struct udevice *emul;
  96. bool test_mode;
  97. int emul_idx;
  98. #endif
  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. *
  107. * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the
  108. * I2C bus udevice.
  109. *
  110. * @speed_hz: Bus speed in hertz (typically 100000)
  111. * @max_transaction_bytes: Maximal size of single I2C transfer
  112. */
  113. struct dm_i2c_bus {
  114. int speed_hz;
  115. int max_transaction_bytes;
  116. };
  117. /*
  118. * Not all of these flags are implemented in the U-Boot API
  119. */
  120. enum dm_i2c_msg_flags {
  121. I2C_M_TEN = 0x0010, /* ten-bit chip address */
  122. I2C_M_RD = 0x0001, /* read data, from slave to master */
  123. I2C_M_STOP = 0x8000, /* send stop after this message */
  124. I2C_M_NOSTART = 0x4000, /* no start before this message */
  125. I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */
  126. I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */
  127. I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */
  128. I2C_M_RECV_LEN = 0x0400, /* length is first received byte */
  129. };
  130. /**
  131. * struct i2c_msg - an I2C message
  132. *
  133. * @addr: Slave address
  134. * @flags: Flags (see enum dm_i2c_msg_flags)
  135. * @len: Length of buffer in bytes, may be 0 for a probe
  136. * @buf: Buffer to send/receive, or NULL if no data
  137. */
  138. struct i2c_msg {
  139. uint addr;
  140. uint flags;
  141. uint len;
  142. u8 *buf;
  143. };
  144. /**
  145. * struct i2c_msg_list - a list of I2C messages
  146. *
  147. * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
  148. * appropriate in U-Boot.
  149. *
  150. * @msg: Pointer to i2c_msg array
  151. * @nmsgs: Number of elements in the array
  152. */
  153. struct i2c_msg_list {
  154. struct i2c_msg *msgs;
  155. uint nmsgs;
  156. };
  157. /**
  158. * dm_i2c_read() - read bytes from an I2C chip
  159. *
  160. * To obtain an I2C device (called a 'chip') given the I2C bus address you
  161. * can use i2c_get_chip(). To obtain a bus by bus number use
  162. * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
  163. *
  164. * To set the address length of a devce use i2c_set_addr_len(). It
  165. * defaults to 1.
  166. *
  167. * @dev: Chip to read from
  168. * @offset: Offset within chip to start reading
  169. * @buffer: Place to put data
  170. * @len: Number of bytes to read
  171. *
  172. * Return: 0 on success, -ve on failure
  173. */
  174. int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
  175. /**
  176. * dm_i2c_write() - write bytes to an I2C chip
  177. *
  178. * See notes for dm_i2c_read() above.
  179. *
  180. * @dev: Chip to write to
  181. * @offset: Offset within chip to start writing
  182. * @buffer: Buffer containing data to write
  183. * @len: Number of bytes to write
  184. *
  185. * Return: 0 on success, -ve on failure
  186. */
  187. int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
  188. int len);
  189. /**
  190. * dm_i2c_probe() - probe a particular chip address
  191. *
  192. * This can be useful to check for the existence of a chip on the bus.
  193. * It is typically implemented by writing the chip address to the bus
  194. * and checking that the chip replies with an ACK.
  195. *
  196. * @bus: Bus to probe
  197. * @chip_addr: 7-bit address to probe (10-bit and others are not supported)
  198. * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags)
  199. * @devp: Returns the device found, or NULL if none
  200. * Return: 0 if a chip was found at that address, -ve if not
  201. */
  202. int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
  203. struct udevice **devp);
  204. /**
  205. * dm_i2c_reg_read() - Read a value from an I2C register
  206. *
  207. * This reads a single value from the given address in an I2C chip
  208. *
  209. * @dev: Device to use for transfer
  210. * @addr: Address to read from
  211. * Return: value read, or -ve on error
  212. */
  213. int dm_i2c_reg_read(struct udevice *dev, uint offset);
  214. /**
  215. * dm_i2c_reg_write() - Write a value to an I2C register
  216. *
  217. * This writes a single value to the given address in an I2C chip
  218. *
  219. * @dev: Device to use for transfer
  220. * @addr: Address to write to
  221. * @val: Value to write (normally a byte)
  222. * Return: 0 on success, -ve on error
  223. */
  224. int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val);
  225. /**
  226. * dm_i2c_reg_clrset() - Apply bitmask to an I2C register
  227. *
  228. * Read value, apply bitmask and write modified value back to the
  229. * given address in an I2C chip
  230. *
  231. * @dev: Device to use for transfer
  232. * @offset: Address for the R/W operation
  233. * @clr: Bitmask of bits that should be cleared
  234. * @set: Bitmask of bits that should be set
  235. * Return: 0 on success, -ve on error
  236. */
  237. int dm_i2c_reg_clrset(struct udevice *dev, uint offset, u32 clr, u32 set);
  238. /**
  239. * dm_i2c_xfer() - Transfer messages over I2C
  240. *
  241. * This transfers a raw message. It is best to use dm_i2c_reg_read/write()
  242. * instead.
  243. *
  244. * @dev: Device to use for transfer
  245. * @msg: List of messages to transfer
  246. * @nmsgs: Number of messages to transfer
  247. * Return: 0 on success, -ve on error
  248. */
  249. int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs);
  250. /**
  251. * dm_i2c_set_bus_speed() - set the speed of a bus
  252. *
  253. * @bus: Bus to adjust
  254. * @speed: Requested speed in Hz
  255. * Return: 0 if OK, -EINVAL for invalid values
  256. */
  257. int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
  258. /**
  259. * dm_i2c_get_bus_speed() - get the speed of a bus
  260. *
  261. * @bus: Bus to check
  262. * Return: speed of selected I2C bus in Hz, -ve on error
  263. */
  264. int dm_i2c_get_bus_speed(struct udevice *bus);
  265. /**
  266. * i2c_set_chip_flags() - set flags for a chip
  267. *
  268. * Typically addresses are 7 bits, but for 10-bit addresses you should set
  269. * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
  270. *
  271. * @dev: Chip to adjust
  272. * @flags: New flags
  273. * Return: 0 if OK, -EINVAL if value is unsupported, other -ve value on error
  274. */
  275. int i2c_set_chip_flags(struct udevice *dev, uint flags);
  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. * i2c_set_offset_len() - set the offset length for a chip
  286. *
  287. * The offset used to access a chip may be up to 4 bytes long. Typically it
  288. * is only 1 byte, which is enough for chips with 256 bytes of memory or
  289. * registers. The default value is 1, but you can call this function to
  290. * change it.
  291. *
  292. * @offset_len: New offset length value (typically 1 or 2)
  293. */
  294. int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
  295. /**
  296. * i2c_get_offset_len() - get the offset length for a chip
  297. *
  298. * @return: Current offset length value (typically 1 or 2)
  299. */
  300. int i2c_get_chip_offset_len(struct udevice *dev);
  301. /**
  302. * i2c_set_chip_addr_offset_mask() - set mask of address bits usable by offset
  303. *
  304. * Some devices listen on multiple chip addresses to achieve larger offsets
  305. * than their single or multiple byte offsets would allow for. You can use this
  306. * function to set the bits that are valid to be used for offset overflow.
  307. *
  308. * @mask: The mask to be used for high offset bits within address
  309. * Return: 0 if OK, other -ve value on error
  310. */
  311. int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask);
  312. /*
  313. * i2c_get_chip_addr_offset_mask() - get mask of address bits usable by offset
  314. *
  315. * Return: current chip addr offset mask
  316. */
  317. uint i2c_get_chip_addr_offset_mask(struct udevice *dev);
  318. /**
  319. * i2c_deblock() - recover a bus that is in an unknown state
  320. *
  321. * See the deblock() method in 'struct dm_i2c_ops' for full information
  322. *
  323. * @bus: Bus to recover
  324. * Return: 0 if OK, -ve on error
  325. */
  326. int i2c_deblock(struct udevice *bus);
  327. /**
  328. * i2c_deblock_gpio_loop() - recover a bus from an unknown state by toggling SDA/SCL
  329. *
  330. * This is the inner logic used for toggling I2C SDA/SCL lines as GPIOs
  331. * for deblocking the I2C bus.
  332. *
  333. * @sda_pin: SDA GPIO
  334. * @scl_pin: SCL GPIO
  335. * @scl_count: Number of SCL clock cycles generated to deblock SDA
  336. * @start_count:Number of I2C start conditions sent after deblocking SDA
  337. * @delay: Delay between SCL clock line changes
  338. * Return: 0 if OK, -ve on error
  339. */
  340. struct gpio_desc;
  341. int i2c_deblock_gpio_loop(struct gpio_desc *sda_pin, struct gpio_desc *scl_pin,
  342. unsigned int scl_count, unsigned int start_count,
  343. unsigned int delay);
  344. /**
  345. * struct dm_i2c_ops - driver operations for I2C uclass
  346. *
  347. * Drivers should support these operations unless otherwise noted. These
  348. * operations are intended to be used by uclass code, not directly from
  349. * other code.
  350. */
  351. struct dm_i2c_ops {
  352. /**
  353. * xfer() - transfer a list of I2C messages
  354. *
  355. * @bus: Bus to read from
  356. * @msg: List of messages to transfer
  357. * @nmsgs: Number of messages in the list
  358. * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
  359. * -ECOMM if the speed cannot be supported, -EPROTO if the chip
  360. * flags cannot be supported, other -ve value on some other error
  361. */
  362. int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
  363. /**
  364. * probe_chip() - probe for the presense of a chip address
  365. *
  366. * This function is optional. If omitted, the uclass will send a zero
  367. * length message instead.
  368. *
  369. * @bus: Bus to probe
  370. * @chip_addr: Chip address to probe
  371. * @chip_flags: Probe flags (enum dm_i2c_chip_flags)
  372. * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
  373. * to default probem other -ve value on error
  374. */
  375. int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
  376. /**
  377. * set_bus_speed() - set the speed of a bus (optional)
  378. *
  379. * The bus speed value will be updated by the uclass if this function
  380. * does not return an error. This method is optional - if it is not
  381. * provided then the driver can read the speed from
  382. * dev_get_uclass_priv(bus)->speed_hz
  383. *
  384. * @bus: Bus to adjust
  385. * @speed: Requested speed in Hz
  386. * @return 0 if OK, -EINVAL for invalid values
  387. */
  388. int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
  389. /**
  390. * get_bus_speed() - get the speed of a bus (optional)
  391. *
  392. * Normally this can be provided by the uclass, but if you want your
  393. * driver to check the bus speed by looking at the hardware, you can
  394. * implement that here. This method is optional. This method would
  395. * normally be expected to return dev_get_uclass_priv(bus)->speed_hz.
  396. *
  397. * @bus: Bus to check
  398. * @return speed of selected I2C bus in Hz, -ve on error
  399. */
  400. int (*get_bus_speed)(struct udevice *bus);
  401. /**
  402. * set_flags() - set the flags for a chip (optional)
  403. *
  404. * This is generally implemented by the uclass, but drivers can
  405. * check the value to ensure that unsupported options are not used.
  406. * This method is optional. If provided, this method will always be
  407. * called when the flags change.
  408. *
  409. * @dev: Chip to adjust
  410. * @flags: New flags value
  411. * @return 0 if OK, -EINVAL if value is unsupported
  412. */
  413. int (*set_flags)(struct udevice *dev, uint flags);
  414. /**
  415. * deblock() - recover a bus that is in an unknown state
  416. *
  417. * I2C is a synchronous protocol and resets of the processor in the
  418. * middle of an access can block the I2C Bus until a powerdown of
  419. * the full unit is done. This is because slaves can be stuck
  420. * waiting for addition bus transitions for a transaction that will
  421. * never complete. Resetting the I2C master does not help. The only
  422. * way is to force the bus through a series of transitions to make
  423. * sure that all slaves are done with the transaction. This method
  424. * performs this 'deblocking' if support by the driver.
  425. *
  426. * This method is optional.
  427. */
  428. int (*deblock)(struct udevice *bus);
  429. };
  430. #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops)
  431. /**
  432. * struct i2c_mux_ops - operations for an I2C mux
  433. *
  434. * The current mux state is expected to be stored in the mux itself since
  435. * it is the only thing that knows how to make things work. The mux can
  436. * record the current state and then avoid switching unless it is necessary.
  437. * So select() can be skipped if the mux is already in the correct state.
  438. * Also deselect() can be made a nop if required.
  439. */
  440. struct i2c_mux_ops {
  441. /**
  442. * select() - select one of of I2C buses attached to a mux
  443. *
  444. * This will be called when there is no bus currently selected by the
  445. * mux. This method does not need to deselect the old bus since
  446. * deselect() will be already have been called if necessary.
  447. *
  448. * @mux: Mux device
  449. * @bus: I2C bus to select
  450. * @channel: Channel number correponding to the bus to select
  451. * @return 0 if OK, -ve on error
  452. */
  453. int (*select)(struct udevice *mux, struct udevice *bus, uint channel);
  454. /**
  455. * deselect() - select one of of I2C buses attached to a mux
  456. *
  457. * This is used to deselect the currently selected I2C bus.
  458. *
  459. * @mux: Mux device
  460. * @bus: I2C bus to deselect
  461. * @channel: Channel number correponding to the bus to deselect
  462. * @return 0 if OK, -ve on error
  463. */
  464. int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel);
  465. };
  466. #define i2c_mux_get_ops(dev) ((struct i2c_mux_ops *)(dev)->driver->ops)
  467. /**
  468. * i2c_get_chip() - get a device to use to access a chip on a bus
  469. *
  470. * This returns the device for the given chip address. The device can then
  471. * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
  472. *
  473. * @bus: Bus to examine
  474. * @chip_addr: Chip address for the new device
  475. * @offset_len: Length of a register offset in bytes (normally 1)
  476. * @devp: Returns pointer to new device if found or -ENODEV if not
  477. * found
  478. */
  479. int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
  480. struct udevice **devp);
  481. /**
  482. * i2c_get_chip_for_busnum() - get a device to use to access a chip on
  483. * a bus number
  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
  490. * @offset_len: Length of a register offset in bytes (normally 1)
  491. * @devp: Returns pointer to new device if found or -ENODEV if not
  492. * found
  493. */
  494. int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
  495. struct udevice **devp);
  496. /**
  497. * i2c_chip_of_to_plat() - Decode standard I2C platform data
  498. *
  499. * This decodes the chip address from a device tree node and puts it into
  500. * its dm_i2c_chip structure. This should be called in your driver's
  501. * of_to_plat() method.
  502. *
  503. * @blob: Device tree blob
  504. * @node: Node offset to read from
  505. * @spi: Place to put the decoded information
  506. */
  507. int i2c_chip_of_to_plat(struct udevice *dev, struct dm_i2c_chip *chip);
  508. /**
  509. * i2c_dump_msgs() - Dump a list of I2C messages
  510. *
  511. * This may be useful for debugging.
  512. *
  513. * @msg: Message list to dump
  514. * @nmsgs: Number of messages
  515. */
  516. void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs);
  517. /**
  518. * i2c_emul_find() - Find an emulator for an i2c sandbox device
  519. *
  520. * This looks at the device's 'emul' phandle
  521. *
  522. * @dev: Device to find an emulator for
  523. * @emulp: Returns the associated emulator, if found *
  524. * Return: 0 if OK, -ENOENT or -ENODEV if not found
  525. */
  526. int i2c_emul_find(struct udevice *dev, struct udevice **emulp);
  527. /**
  528. * i2c_emul_set_idx() - Set the emulator index for an i2c sandbox device
  529. *
  530. * With of-platdata we cannot find the emulator using the device tree, so rely
  531. * on the bind() method of each i2c driver calling this function to tell us
  532. * the of-platdata idx of the emulator
  533. *
  534. * @dev: i2c device to set the emulator for
  535. * @emul_idx: of-platdata index for that emulator
  536. */
  537. void i2c_emul_set_idx(struct udevice *dev, int emul_idx);
  538. /**
  539. * i2c_emul_get_device() - Find the device being emulated
  540. *
  541. * Given an emulator this returns the associated device
  542. *
  543. * @emul: Emulator for the device
  544. * Return: device that @emul is emulating
  545. */
  546. struct udevice *i2c_emul_get_device(struct udevice *emul);
  547. /* ACPI operations for generic I2C devices */
  548. extern struct acpi_ops i2c_acpi_ops;
  549. /**
  550. * acpi_i2c_of_to_plat() - Read properties intended for ACPI
  551. *
  552. * This reads the generic I2C properties from the device tree, so that these
  553. * can be used to create ACPI information for the device.
  554. *
  555. * See the i2c/generic-acpi.txt binding file for information about the
  556. * properties.
  557. *
  558. * @dev: I2C device to process
  559. * Return: 0 if OK, -EINVAL if acpi,hid is not present
  560. */
  561. int acpi_i2c_of_to_plat(struct udevice *dev);
  562. #ifdef CONFIG_SYS_I2C_EARLY_INIT
  563. void i2c_early_init_f(void);
  564. #endif
  565. #if !CONFIG_IS_ENABLED(DM_I2C)
  566. /*
  567. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  568. *
  569. * The implementation MUST NOT use static or global variables if the
  570. * I2C routines are used to read SDRAM configuration information
  571. * because this is done before the memories are initialized. Limited
  572. * use of stack-based variables are OK (the initial stack size is
  573. * limited).
  574. *
  575. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  576. */
  577. /*
  578. * Configuration items.
  579. */
  580. #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
  581. #if !defined(CFG_SYS_I2C_MAX_HOPS)
  582. /* no muxes used bus = i2c adapters */
  583. #define CFG_SYS_I2C_DIRECT_BUS 1
  584. #define CFG_SYS_I2C_MAX_HOPS 0
  585. #define CFG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
  586. #else
  587. /* we use i2c muxes */
  588. #undef CFG_SYS_I2C_DIRECT_BUS
  589. #endif
  590. /* define the I2C bus number for RTC and DTT if not already done */
  591. #if !defined(CFG_SYS_RTC_BUS_NUM)
  592. #define CFG_SYS_RTC_BUS_NUM 0
  593. #endif
  594. struct i2c_adapter {
  595. void (*init)(struct i2c_adapter *adap, int speed,
  596. int slaveaddr);
  597. int (*probe)(struct i2c_adapter *adap, uint8_t chip);
  598. int (*read)(struct i2c_adapter *adap, uint8_t chip,
  599. uint addr, int alen, uint8_t *buffer,
  600. int len);
  601. int (*write)(struct i2c_adapter *adap, uint8_t chip,
  602. uint addr, int alen, uint8_t *buffer,
  603. int len);
  604. uint (*set_bus_speed)(struct i2c_adapter *adap,
  605. uint speed);
  606. int speed;
  607. int waitdelay;
  608. int slaveaddr;
  609. int init_done;
  610. int hwadapnr;
  611. char *name;
  612. };
  613. #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
  614. _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
  615. { \
  616. .init = _init, \
  617. .probe = _probe, \
  618. .read = _read, \
  619. .write = _write, \
  620. .set_bus_speed = _set_speed, \
  621. .speed = _speed, \
  622. .slaveaddr = _slaveaddr, \
  623. .init_done = 0, \
  624. .hwadapnr = _hwadapnr, \
  625. .name = #_name \
  626. };
  627. #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
  628. _set_speed, _speed, _slaveaddr, _hwadapnr) \
  629. ll_entry_declare(struct i2c_adapter, _name, i2c) = \
  630. U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
  631. _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
  632. struct i2c_adapter *i2c_get_adapter(int index);
  633. #ifndef CFG_SYS_I2C_DIRECT_BUS
  634. struct i2c_mux {
  635. int id;
  636. char name[16];
  637. };
  638. struct i2c_next_hop {
  639. struct i2c_mux mux;
  640. uint8_t chip;
  641. uint8_t channel;
  642. };
  643. struct i2c_bus_hose {
  644. int adapter;
  645. struct i2c_next_hop next_hop[CFG_SYS_I2C_MAX_HOPS];
  646. };
  647. #define I2C_NULL_HOP {{-1, ""}, 0, 0}
  648. extern struct i2c_bus_hose i2c_bus[];
  649. #define I2C_ADAPTER(bus) i2c_bus[bus].adapter
  650. #else
  651. #define I2C_ADAPTER(bus) bus
  652. #endif
  653. #define I2C_BUS gd->cur_i2c_bus
  654. #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
  655. #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
  656. #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
  657. #ifndef CFG_SYS_I2C_DIRECT_BUS
  658. #define I2C_MUX_PCA9540_ID 1
  659. #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
  660. #define I2C_MUX_PCA9542_ID 2
  661. #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
  662. #define I2C_MUX_PCA9544_ID 3
  663. #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
  664. #define I2C_MUX_PCA9547_ID 4
  665. #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
  666. #define I2C_MUX_PCA9548_ID 5
  667. #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
  668. #endif
  669. #ifndef I2C_SOFT_DECLARATIONS
  670. # if (defined(CONFIG_AT91RM9200) || \
  671. defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
  672. defined(CONFIG_AT91SAM9263))
  673. # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  674. # else
  675. # define I2C_SOFT_DECLARATIONS
  676. # endif
  677. #endif
  678. /*
  679. * Initialization, must be called once on start up, may be called
  680. * repeatedly to change the speed and slave addresses.
  681. */
  682. void i2c_init(int speed, int slaveaddr);
  683. void i2c_init_board(void);
  684. #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY)
  685. /*
  686. * i2c_get_bus_num:
  687. *
  688. * Returns index of currently active I2C bus. Zero-based.
  689. */
  690. unsigned int i2c_get_bus_num(void);
  691. /*
  692. * i2c_set_bus_num:
  693. *
  694. * Change the active I2C bus. Subsequent read/write calls will
  695. * go to this one.
  696. *
  697. * bus - bus index, zero based
  698. *
  699. * Returns: 0 on success, not 0 on failure
  700. *
  701. */
  702. int i2c_set_bus_num(unsigned int bus);
  703. /*
  704. * i2c_init_all():
  705. *
  706. * Initializes all I2C adapters in the system. All i2c_adap structures must
  707. * be initialized beforehead with function pointers and data, including
  708. * speed and slaveaddr. Returns 0 on success, non-0 on failure.
  709. */
  710. void i2c_init_all(void);
  711. /*
  712. * Probe the given I2C chip address. Returns 0 if a chip responded,
  713. * not 0 on failure.
  714. */
  715. int i2c_probe(uint8_t chip);
  716. /*
  717. * Read/Write interface:
  718. * chip: I2C chip address, range 0..127
  719. * addr: Memory (register) address within the chip
  720. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  721. * memories, 0 for register type devices with only one
  722. * register)
  723. * buffer: Where to read/write the data
  724. * len: How many bytes to read/write
  725. *
  726. * Returns: 0 on success, not 0 on failure
  727. */
  728. int i2c_read(uint8_t chip, unsigned int addr, int alen,
  729. uint8_t *buffer, int len);
  730. int i2c_write(uint8_t chip, unsigned int addr, int alen,
  731. uint8_t *buffer, int len);
  732. /*
  733. * Utility routines to read/write registers.
  734. */
  735. uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
  736. void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
  737. /*
  738. * i2c_set_bus_speed:
  739. *
  740. * Change the speed of the active I2C bus
  741. *
  742. * speed - bus speed in Hz
  743. *
  744. * Returns: new bus speed
  745. *
  746. */
  747. unsigned int i2c_set_bus_speed(unsigned int speed);
  748. /*
  749. * i2c_get_bus_speed:
  750. *
  751. * Returns speed of currently active I2C bus in Hz
  752. */
  753. unsigned int i2c_get_bus_speed(void);
  754. #else
  755. /*
  756. * Probe the given I2C chip address. Returns 0 if a chip responded,
  757. * not 0 on failure.
  758. */
  759. int i2c_probe(uchar chip);
  760. /*
  761. * Read/Write interface:
  762. * chip: I2C chip address, range 0..127
  763. * addr: Memory (register) address within the chip
  764. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  765. * memories, 0 for register type devices with only one
  766. * register)
  767. * buffer: Where to read/write the data
  768. * len: How many bytes to read/write
  769. *
  770. * Returns: 0 on success, not 0 on failure
  771. */
  772. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
  773. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
  774. /*
  775. * Utility routines to read/write registers.
  776. */
  777. static inline u8 i2c_reg_read(u8 addr, u8 reg)
  778. {
  779. u8 buf;
  780. #ifdef DEBUG
  781. printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
  782. #endif
  783. i2c_read(addr, reg, 1, &buf, 1);
  784. return buf;
  785. }
  786. static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
  787. {
  788. #ifdef DEBUG
  789. printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
  790. __func__, addr, reg, val);
  791. #endif
  792. i2c_write(addr, reg, 1, &val, 1);
  793. }
  794. /*
  795. * Functions for setting the current I2C bus and its speed
  796. */
  797. /*
  798. * i2c_set_bus_num:
  799. *
  800. * Change the active I2C bus. Subsequent read/write calls will
  801. * go to this one.
  802. *
  803. * bus - bus index, zero based
  804. *
  805. * Returns: 0 on success, not 0 on failure
  806. *
  807. */
  808. int i2c_set_bus_num(unsigned int bus);
  809. /*
  810. * i2c_get_bus_num:
  811. *
  812. * Returns index of currently active I2C bus. Zero-based.
  813. */
  814. unsigned int i2c_get_bus_num(void);
  815. /*
  816. * i2c_set_bus_speed:
  817. *
  818. * Change the speed of the active I2C bus
  819. *
  820. * speed - bus speed in Hz
  821. *
  822. * Returns: 0 on success, not 0 on failure
  823. *
  824. */
  825. int i2c_set_bus_speed(unsigned int);
  826. /*
  827. * i2c_get_bus_speed:
  828. *
  829. * Returns speed of currently active I2C bus in Hz
  830. */
  831. unsigned int i2c_get_bus_speed(void);
  832. #endif /* CONFIG_SYS_I2C_LEGACY */
  833. /*
  834. * only for backwardcompatibility, should go away if we switched
  835. * completely to new multibus support.
  836. */
  837. #if CONFIG_IS_ENABLED(SYS_I2C_LEGACY) || defined(CFG_I2C_MULTI_BUS)
  838. # if !defined(CFG_SYS_MAX_I2C_BUS)
  839. # define CFG_SYS_MAX_I2C_BUS 2
  840. # endif
  841. # define I2C_MULTI_BUS 1
  842. #else
  843. # define CFG_SYS_MAX_I2C_BUS 1
  844. # define I2C_MULTI_BUS 0
  845. #endif
  846. /* NOTE: These two functions MUST be always_inline to avoid code growth! */
  847. static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
  848. static inline unsigned int I2C_GET_BUS(void)
  849. {
  850. return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
  851. }
  852. static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
  853. static inline void I2C_SET_BUS(unsigned int bus)
  854. {
  855. if (I2C_MULTI_BUS)
  856. i2c_set_bus_num(bus);
  857. }
  858. /* Multi I2C definitions */
  859. enum {
  860. I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
  861. I2C_8, I2C_9, I2C_10,
  862. };
  863. /**
  864. * Get FDT values for i2c bus.
  865. *
  866. * @param blob Device tree blbo
  867. * Return: the number of I2C bus
  868. */
  869. void board_i2c_init(const void *blob);
  870. /**
  871. * Find the I2C bus number by given a FDT I2C node.
  872. *
  873. * @param blob Device tree blbo
  874. * @param node FDT I2C node to find
  875. * Return: the number of I2C bus (zero based), or -1 on error
  876. */
  877. int i2c_get_bus_num_fdt(int node);
  878. /**
  879. * Reset the I2C bus represented by the given a FDT I2C node.
  880. *
  881. * @param blob Device tree blbo
  882. * @param node FDT I2C node to find
  883. * Return: 0 if port was reset, -1 if not found
  884. */
  885. int i2c_reset_port_fdt(const void *blob, int node);
  886. #endif /* !CONFIG_DM_I2C */
  887. #endif /* _I2C_H_ */