i2c.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*
  2. * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
  3. * Copyright (C) 2009 - 2013 Heiko Schocher <hs@denx.de>
  4. * Changes for multibus/multiadapter I2C support.
  5. *
  6. * (C) Copyright 2001
  7. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. *
  11. * The original I2C interface was
  12. * (C) 2000 by Paolo Scaffardi (arsenio@tin.it)
  13. * AIRVENT SAM s.p.a - RIMINI(ITALY)
  14. * but has been changed substantially.
  15. */
  16. #ifndef _I2C_H_
  17. #define _I2C_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 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. struct udevice;
  31. /**
  32. * struct dm_i2c_chip - information about an i2c chip
  33. *
  34. * An I2C chip is a device on the I2C bus. It sits at a particular address
  35. * and normally supports 7-bit or 10-bit addressing.
  36. *
  37. * To obtain this structure, use dev_get_parent_platdata(dev) where dev is
  38. * the chip to examine.
  39. *
  40. * @chip_addr: Chip address on bus
  41. * @offset_len: Length of offset in bytes. A single byte offset can
  42. * represent up to 256 bytes. A value larger than 1 may be
  43. * needed for larger devices.
  44. * @flags: Flags for this chip (dm_i2c_chip_flags)
  45. * @emul: Emulator for this chip address (only used for emulation)
  46. */
  47. struct dm_i2c_chip {
  48. uint chip_addr;
  49. uint offset_len;
  50. uint flags;
  51. #ifdef CONFIG_SANDBOX
  52. struct udevice *emul;
  53. bool test_mode;
  54. #endif
  55. };
  56. /**
  57. * struct dm_i2c_bus- information about an i2c bus
  58. *
  59. * An I2C bus contains 0 or more chips on it, each at its own address. The
  60. * bus can operate at different speeds (measured in Hz, typically 100KHz
  61. * or 400KHz).
  62. *
  63. * To obtain this structure, use dev_get_uclass_priv(bus) where bus is the
  64. * I2C bus udevice.
  65. *
  66. * @speed_hz: Bus speed in hertz (typically 100000)
  67. */
  68. struct dm_i2c_bus {
  69. int speed_hz;
  70. };
  71. /*
  72. * Not all of these flags are implemented in the U-Boot API
  73. */
  74. enum dm_i2c_msg_flags {
  75. I2C_M_TEN = 0x0010, /* ten-bit chip address */
  76. I2C_M_RD = 0x0001, /* read data, from slave to master */
  77. I2C_M_STOP = 0x8000, /* send stop after this message */
  78. I2C_M_NOSTART = 0x4000, /* no start before this message */
  79. I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */
  80. I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */
  81. I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */
  82. I2C_M_RECV_LEN = 0x0400, /* length is first received byte */
  83. };
  84. /**
  85. * struct i2c_msg - an I2C message
  86. *
  87. * @addr: Slave address
  88. * @flags: Flags (see enum dm_i2c_msg_flags)
  89. * @len: Length of buffer in bytes, may be 0 for a probe
  90. * @buf: Buffer to send/receive, or NULL if no data
  91. */
  92. struct i2c_msg {
  93. uint addr;
  94. uint flags;
  95. uint len;
  96. u8 *buf;
  97. };
  98. /**
  99. * struct i2c_msg_list - a list of I2C messages
  100. *
  101. * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
  102. * appropriate in U-Boot.
  103. *
  104. * @msg: Pointer to i2c_msg array
  105. * @nmsgs: Number of elements in the array
  106. */
  107. struct i2c_msg_list {
  108. struct i2c_msg *msgs;
  109. uint nmsgs;
  110. };
  111. /**
  112. * dm_i2c_read() - read bytes from an I2C chip
  113. *
  114. * To obtain an I2C device (called a 'chip') given the I2C bus address you
  115. * can use i2c_get_chip(). To obtain a bus by bus number use
  116. * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
  117. *
  118. * To set the address length of a devce use i2c_set_addr_len(). It
  119. * defaults to 1.
  120. *
  121. * @dev: Chip to read from
  122. * @offset: Offset within chip to start reading
  123. * @buffer: Place to put data
  124. * @len: Number of bytes to read
  125. *
  126. * @return 0 on success, -ve on failure
  127. */
  128. int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len);
  129. /**
  130. * dm_i2c_write() - write bytes to an I2C chip
  131. *
  132. * See notes for dm_i2c_read() above.
  133. *
  134. * @dev: Chip to write to
  135. * @offset: Offset within chip to start writing
  136. * @buffer: Buffer containing data to write
  137. * @len: Number of bytes to write
  138. *
  139. * @return 0 on success, -ve on failure
  140. */
  141. int dm_i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
  142. int len);
  143. /**
  144. * dm_i2c_probe() - probe a particular chip address
  145. *
  146. * This can be useful to check for the existence of a chip on the bus.
  147. * It is typically implemented by writing the chip address to the bus
  148. * and checking that the chip replies with an ACK.
  149. *
  150. * @bus: Bus to probe
  151. * @chip_addr: 7-bit address to probe (10-bit and others are not supported)
  152. * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags)
  153. * @devp: Returns the device found, or NULL if none
  154. * @return 0 if a chip was found at that address, -ve if not
  155. */
  156. int dm_i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
  157. struct udevice **devp);
  158. /**
  159. * dm_i2c_reg_read() - Read a value from an I2C register
  160. *
  161. * This reads a single value from the given address in an I2C chip
  162. *
  163. * @dev: Device to use for transfer
  164. * @addr: Address to read from
  165. * @return value read, or -ve on error
  166. */
  167. int dm_i2c_reg_read(struct udevice *dev, uint offset);
  168. /**
  169. * dm_i2c_reg_write() - Write a value to an I2C register
  170. *
  171. * This writes a single value to the given address in an I2C chip
  172. *
  173. * @dev: Device to use for transfer
  174. * @addr: Address to write to
  175. * @val: Value to write (normally a byte)
  176. * @return 0 on success, -ve on error
  177. */
  178. int dm_i2c_reg_write(struct udevice *dev, uint offset, unsigned int val);
  179. /**
  180. * dm_i2c_xfer() - Transfer messages over I2C
  181. *
  182. * This transfers a raw message. It is best to use dm_i2c_reg_read/write()
  183. * instead.
  184. *
  185. * @dev: Device to use for transfer
  186. * @msg: List of messages to transfer
  187. * @nmsgs: Number of messages to transfer
  188. * @return 0 on success, -ve on error
  189. */
  190. int dm_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs);
  191. /**
  192. * dm_i2c_set_bus_speed() - set the speed of a bus
  193. *
  194. * @bus: Bus to adjust
  195. * @speed: Requested speed in Hz
  196. * @return 0 if OK, -EINVAL for invalid values
  197. */
  198. int dm_i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
  199. /**
  200. * dm_i2c_get_bus_speed() - get the speed of a bus
  201. *
  202. * @bus: Bus to check
  203. * @return speed of selected I2C bus in Hz, -ve on error
  204. */
  205. int dm_i2c_get_bus_speed(struct udevice *bus);
  206. /**
  207. * i2c_set_chip_flags() - set flags for a chip
  208. *
  209. * Typically addresses are 7 bits, but for 10-bit addresses you should set
  210. * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
  211. *
  212. * @dev: Chip to adjust
  213. * @flags: New flags
  214. * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error
  215. */
  216. int i2c_set_chip_flags(struct udevice *dev, uint flags);
  217. /**
  218. * i2c_get_chip_flags() - get flags for a chip
  219. *
  220. * @dev: Chip to check
  221. * @flagsp: Place to put flags
  222. * @return 0 if OK, other -ve value on error
  223. */
  224. int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
  225. /**
  226. * i2c_set_offset_len() - set the offset length for a chip
  227. *
  228. * The offset used to access a chip may be up to 4 bytes long. Typically it
  229. * is only 1 byte, which is enough for chips with 256 bytes of memory or
  230. * registers. The default value is 1, but you can call this function to
  231. * change it.
  232. *
  233. * @offset_len: New offset length value (typically 1 or 2)
  234. */
  235. int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
  236. /**
  237. * i2c_get_offset_len() - get the offset length for a chip
  238. *
  239. * @return: Current offset length value (typically 1 or 2)
  240. */
  241. int i2c_get_chip_offset_len(struct udevice *dev);
  242. /**
  243. * i2c_deblock() - recover a bus that is in an unknown state
  244. *
  245. * See the deblock() method in 'struct dm_i2c_ops' for full information
  246. *
  247. * @bus: Bus to recover
  248. * @return 0 if OK, -ve on error
  249. */
  250. int i2c_deblock(struct udevice *bus);
  251. #ifdef CONFIG_DM_I2C_COMPAT
  252. /**
  253. * i2c_probe() - Compatibility function for driver model
  254. *
  255. * Calls dm_i2c_probe() on the current bus
  256. */
  257. int i2c_probe(uint8_t chip_addr);
  258. /**
  259. * i2c_read() - Compatibility function for driver model
  260. *
  261. * Calls dm_i2c_read() with the device corresponding to @chip_addr, and offset
  262. * set to @addr. @alen must match the current setting for the device.
  263. */
  264. int i2c_read(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
  265. int len);
  266. /**
  267. * i2c_write() - Compatibility function for driver model
  268. *
  269. * Calls dm_i2c_write() with the device corresponding to @chip_addr, and offset
  270. * set to @addr. @alen must match the current setting for the device.
  271. */
  272. int i2c_write(uint8_t chip_addr, unsigned int addr, int alen, uint8_t *buffer,
  273. int len);
  274. /**
  275. * i2c_get_bus_num_fdt() - Compatibility function for driver model
  276. *
  277. * @return the bus number associated with the given device tree node
  278. */
  279. int i2c_get_bus_num_fdt(int node);
  280. /**
  281. * i2c_get_bus_num() - Compatibility function for driver model
  282. *
  283. * @return the 'current' bus number
  284. */
  285. unsigned int i2c_get_bus_num(void);
  286. /**
  287. * i2c_set_bus_num() - Compatibility function for driver model
  288. *
  289. * Sets the 'current' bus
  290. */
  291. int i2c_set_bus_num(unsigned int bus);
  292. static inline void I2C_SET_BUS(unsigned int bus)
  293. {
  294. i2c_set_bus_num(bus);
  295. }
  296. static inline unsigned int I2C_GET_BUS(void)
  297. {
  298. return i2c_get_bus_num();
  299. }
  300. /**
  301. * i2c_init() - Compatibility function for driver model
  302. *
  303. * This function does nothing.
  304. */
  305. void i2c_init(int speed, int slaveaddr);
  306. /**
  307. * board_i2c_init() - Compatibility function for driver model
  308. *
  309. * @param blob Device tree blbo
  310. * @return the number of I2C bus
  311. */
  312. void board_i2c_init(const void *blob);
  313. /*
  314. * Compatibility functions for driver model.
  315. */
  316. uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
  317. void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
  318. #endif
  319. /**
  320. * struct dm_i2c_ops - driver operations for I2C uclass
  321. *
  322. * Drivers should support these operations unless otherwise noted. These
  323. * operations are intended to be used by uclass code, not directly from
  324. * other code.
  325. */
  326. struct dm_i2c_ops {
  327. /**
  328. * xfer() - transfer a list of I2C messages
  329. *
  330. * @bus: Bus to read from
  331. * @msg: List of messages to transfer
  332. * @nmsgs: Number of messages in the list
  333. * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
  334. * -ECOMM if the speed cannot be supported, -EPROTO if the chip
  335. * flags cannot be supported, other -ve value on some other error
  336. */
  337. int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
  338. /**
  339. * probe_chip() - probe for the presense of a chip address
  340. *
  341. * This function is optional. If omitted, the uclass will send a zero
  342. * length message instead.
  343. *
  344. * @bus: Bus to probe
  345. * @chip_addr: Chip address to probe
  346. * @chip_flags: Probe flags (enum dm_i2c_chip_flags)
  347. * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
  348. * to default probem other -ve value on error
  349. */
  350. int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
  351. /**
  352. * set_bus_speed() - set the speed of a bus (optional)
  353. *
  354. * The bus speed value will be updated by the uclass if this function
  355. * does not return an error. This method is optional - if it is not
  356. * provided then the driver can read the speed from
  357. * dev_get_uclass_priv(bus)->speed_hz
  358. *
  359. * @bus: Bus to adjust
  360. * @speed: Requested speed in Hz
  361. * @return 0 if OK, -EINVAL for invalid values
  362. */
  363. int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
  364. /**
  365. * get_bus_speed() - get the speed of a bus (optional)
  366. *
  367. * Normally this can be provided by the uclass, but if you want your
  368. * driver to check the bus speed by looking at the hardware, you can
  369. * implement that here. This method is optional. This method would
  370. * normally be expected to return dev_get_uclass_priv(bus)->speed_hz.
  371. *
  372. * @bus: Bus to check
  373. * @return speed of selected I2C bus in Hz, -ve on error
  374. */
  375. int (*get_bus_speed)(struct udevice *bus);
  376. /**
  377. * set_flags() - set the flags for a chip (optional)
  378. *
  379. * This is generally implemented by the uclass, but drivers can
  380. * check the value to ensure that unsupported options are not used.
  381. * This method is optional. If provided, this method will always be
  382. * called when the flags change.
  383. *
  384. * @dev: Chip to adjust
  385. * @flags: New flags value
  386. * @return 0 if OK, -EINVAL if value is unsupported
  387. */
  388. int (*set_flags)(struct udevice *dev, uint flags);
  389. /**
  390. * deblock() - recover a bus that is in an unknown state
  391. *
  392. * I2C is a synchronous protocol and resets of the processor in the
  393. * middle of an access can block the I2C Bus until a powerdown of
  394. * the full unit is done. This is because slaves can be stuck
  395. * waiting for addition bus transitions for a transaction that will
  396. * never complete. Resetting the I2C master does not help. The only
  397. * way is to force the bus through a series of transitions to make
  398. * sure that all slaves are done with the transaction. This method
  399. * performs this 'deblocking' if support by the driver.
  400. *
  401. * This method is optional.
  402. */
  403. int (*deblock)(struct udevice *bus);
  404. };
  405. #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops)
  406. /**
  407. * struct i2c_mux_ops - operations for an I2C mux
  408. *
  409. * The current mux state is expected to be stored in the mux itself since
  410. * it is the only thing that knows how to make things work. The mux can
  411. * record the current state and then avoid switching unless it is necessary.
  412. * So select() can be skipped if the mux is already in the correct state.
  413. * Also deselect() can be made a nop if required.
  414. */
  415. struct i2c_mux_ops {
  416. /**
  417. * select() - select one of of I2C buses attached to a mux
  418. *
  419. * This will be called when there is no bus currently selected by the
  420. * mux. This method does not need to deselect the old bus since
  421. * deselect() will be already have been called if necessary.
  422. *
  423. * @mux: Mux device
  424. * @bus: I2C bus to select
  425. * @channel: Channel number correponding to the bus to select
  426. * @return 0 if OK, -ve on error
  427. */
  428. int (*select)(struct udevice *mux, struct udevice *bus, uint channel);
  429. /**
  430. * deselect() - select one of of I2C buses attached to a mux
  431. *
  432. * This is used to deselect the currently selected I2C bus.
  433. *
  434. * @mux: Mux device
  435. * @bus: I2C bus to deselect
  436. * @channel: Channel number correponding to the bus to deselect
  437. * @return 0 if OK, -ve on error
  438. */
  439. int (*deselect)(struct udevice *mux, struct udevice *bus, uint channel);
  440. };
  441. #define i2c_mux_get_ops(dev) ((struct i2c_mux_ops *)(dev)->driver->ops)
  442. /**
  443. * i2c_get_chip() - get a device to use to access a chip on a bus
  444. *
  445. * This returns the device for the given chip address. The device can then
  446. * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
  447. *
  448. * @bus: Bus to examine
  449. * @chip_addr: Chip address for the new device
  450. * @offset_len: Length of a register offset in bytes (normally 1)
  451. * @devp: Returns pointer to new device if found or -ENODEV if not
  452. * found
  453. */
  454. int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
  455. struct udevice **devp);
  456. /**
  457. * i2c_get_chip_for_busnum() - get a device to use to access a chip on
  458. * a bus number
  459. *
  460. * This returns the device for the given chip address on a particular bus
  461. * number.
  462. *
  463. * @busnum: Bus number to examine
  464. * @chip_addr: Chip address for the new device
  465. * @offset_len: Length of a register offset in bytes (normally 1)
  466. * @devp: Returns pointer to new device if found or -ENODEV if not
  467. * found
  468. */
  469. int i2c_get_chip_for_busnum(int busnum, int chip_addr, uint offset_len,
  470. struct udevice **devp);
  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. */
  482. int i2c_chip_ofdata_to_platdata(const void *blob, int node,
  483. struct dm_i2c_chip *chip);
  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. #ifndef CONFIG_DM_I2C
  494. /*
  495. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  496. *
  497. * The implementation MUST NOT use static or global variables if the
  498. * I2C routines are used to read SDRAM configuration information
  499. * because this is done before the memories are initialized. Limited
  500. * use of stack-based variables are OK (the initial stack size is
  501. * limited).
  502. *
  503. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  504. */
  505. /*
  506. * Configuration items.
  507. */
  508. #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
  509. #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
  510. /* no muxes used bus = i2c adapters */
  511. #define CONFIG_SYS_I2C_DIRECT_BUS 1
  512. #define CONFIG_SYS_I2C_MAX_HOPS 0
  513. #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
  514. #else
  515. /* we use i2c muxes */
  516. #undef CONFIG_SYS_I2C_DIRECT_BUS
  517. #endif
  518. /* define the I2C bus number for RTC and DTT if not already done */
  519. #if !defined(CONFIG_SYS_RTC_BUS_NUM)
  520. #define CONFIG_SYS_RTC_BUS_NUM 0
  521. #endif
  522. #if !defined(CONFIG_SYS_DTT_BUS_NUM)
  523. #define CONFIG_SYS_DTT_BUS_NUM 0
  524. #endif
  525. #if !defined(CONFIG_SYS_SPD_BUS_NUM)
  526. #define CONFIG_SYS_SPD_BUS_NUM 0
  527. #endif
  528. struct i2c_adapter {
  529. void (*init)(struct i2c_adapter *adap, int speed,
  530. int slaveaddr);
  531. int (*probe)(struct i2c_adapter *adap, uint8_t chip);
  532. int (*read)(struct i2c_adapter *adap, uint8_t chip,
  533. uint addr, int alen, uint8_t *buffer,
  534. int len);
  535. int (*write)(struct i2c_adapter *adap, uint8_t chip,
  536. uint addr, int alen, uint8_t *buffer,
  537. int len);
  538. uint (*set_bus_speed)(struct i2c_adapter *adap,
  539. uint speed);
  540. int speed;
  541. int waitdelay;
  542. int slaveaddr;
  543. int init_done;
  544. int hwadapnr;
  545. char *name;
  546. };
  547. #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
  548. _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
  549. { \
  550. .init = _init, \
  551. .probe = _probe, \
  552. .read = _read, \
  553. .write = _write, \
  554. .set_bus_speed = _set_speed, \
  555. .speed = _speed, \
  556. .slaveaddr = _slaveaddr, \
  557. .init_done = 0, \
  558. .hwadapnr = _hwadapnr, \
  559. .name = #_name \
  560. };
  561. #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
  562. _set_speed, _speed, _slaveaddr, _hwadapnr) \
  563. ll_entry_declare(struct i2c_adapter, _name, i2c) = \
  564. U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
  565. _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
  566. struct i2c_adapter *i2c_get_adapter(int index);
  567. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  568. struct i2c_mux {
  569. int id;
  570. char name[16];
  571. };
  572. struct i2c_next_hop {
  573. struct i2c_mux mux;
  574. uint8_t chip;
  575. uint8_t channel;
  576. };
  577. struct i2c_bus_hose {
  578. int adapter;
  579. struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
  580. };
  581. #define I2C_NULL_HOP {{-1, ""}, 0, 0}
  582. extern struct i2c_bus_hose i2c_bus[];
  583. #define I2C_ADAPTER(bus) i2c_bus[bus].adapter
  584. #else
  585. #define I2C_ADAPTER(bus) bus
  586. #endif
  587. #define I2C_BUS gd->cur_i2c_bus
  588. #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
  589. #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
  590. #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
  591. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  592. #define I2C_MUX_PCA9540_ID 1
  593. #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
  594. #define I2C_MUX_PCA9542_ID 2
  595. #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
  596. #define I2C_MUX_PCA9544_ID 3
  597. #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
  598. #define I2C_MUX_PCA9547_ID 4
  599. #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
  600. #define I2C_MUX_PCA9548_ID 5
  601. #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
  602. #endif
  603. #ifndef I2C_SOFT_DECLARATIONS
  604. # if defined(CONFIG_MPC8260)
  605. # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
  606. # elif defined(CONFIG_8xx)
  607. # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  608. # elif (defined(CONFIG_AT91RM9200) || \
  609. defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
  610. defined(CONFIG_AT91SAM9263))
  611. # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  612. # else
  613. # define I2C_SOFT_DECLARATIONS
  614. # endif
  615. #endif
  616. #ifdef CONFIG_8xx
  617. /* Set default value for the I2C bus speed on 8xx. In the
  618. * future, we'll define these in all 8xx board config files.
  619. */
  620. #ifndef CONFIG_SYS_I2C_SPEED
  621. #define CONFIG_SYS_I2C_SPEED 50000
  622. #endif
  623. #endif
  624. /*
  625. * Many boards/controllers/drivers don't support an I2C slave interface so
  626. * provide a default slave address for them for use in common code. A real
  627. * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
  628. * support a slave interface.
  629. */
  630. #ifndef CONFIG_SYS_I2C_SLAVE
  631. #define CONFIG_SYS_I2C_SLAVE 0xfe
  632. #endif
  633. /*
  634. * Initialization, must be called once on start up, may be called
  635. * repeatedly to change the speed and slave addresses.
  636. */
  637. #ifdef CONFIG_SYS_I2C_EARLY_INIT
  638. void i2c_early_init_f(void);
  639. #endif
  640. void i2c_init(int speed, int slaveaddr);
  641. void i2c_init_board(void);
  642. #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
  643. void i2c_board_late_init(void);
  644. #endif
  645. #ifdef CONFIG_SYS_I2C
  646. /*
  647. * i2c_get_bus_num:
  648. *
  649. * Returns index of currently active I2C bus. Zero-based.
  650. */
  651. unsigned int i2c_get_bus_num(void);
  652. /*
  653. * i2c_set_bus_num:
  654. *
  655. * Change the active I2C bus. Subsequent read/write calls will
  656. * go to this one.
  657. *
  658. * bus - bus index, zero based
  659. *
  660. * Returns: 0 on success, not 0 on failure
  661. *
  662. */
  663. int i2c_set_bus_num(unsigned int bus);
  664. /*
  665. * i2c_init_all():
  666. *
  667. * Initializes all I2C adapters in the system. All i2c_adap structures must
  668. * be initialized beforehead with function pointers and data, including
  669. * speed and slaveaddr. Returns 0 on success, non-0 on failure.
  670. */
  671. void i2c_init_all(void);
  672. /*
  673. * Probe the given I2C chip address. Returns 0 if a chip responded,
  674. * not 0 on failure.
  675. */
  676. int i2c_probe(uint8_t chip);
  677. /*
  678. * Read/Write interface:
  679. * chip: I2C chip address, range 0..127
  680. * addr: Memory (register) address within the chip
  681. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  682. * memories, 0 for register type devices with only one
  683. * register)
  684. * buffer: Where to read/write the data
  685. * len: How many bytes to read/write
  686. *
  687. * Returns: 0 on success, not 0 on failure
  688. */
  689. int i2c_read(uint8_t chip, unsigned int addr, int alen,
  690. uint8_t *buffer, int len);
  691. int i2c_write(uint8_t chip, unsigned int addr, int alen,
  692. uint8_t *buffer, int len);
  693. /*
  694. * Utility routines to read/write registers.
  695. */
  696. uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
  697. void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
  698. /*
  699. * i2c_set_bus_speed:
  700. *
  701. * Change the speed of the active I2C bus
  702. *
  703. * speed - bus speed in Hz
  704. *
  705. * Returns: new bus speed
  706. *
  707. */
  708. unsigned int i2c_set_bus_speed(unsigned int speed);
  709. /*
  710. * i2c_get_bus_speed:
  711. *
  712. * Returns speed of currently active I2C bus in Hz
  713. */
  714. unsigned int i2c_get_bus_speed(void);
  715. /*
  716. * i2c_reloc_fixup:
  717. *
  718. * Adjusts I2C pointers after U-Boot is relocated to DRAM
  719. */
  720. void i2c_reloc_fixup(void);
  721. #if defined(CONFIG_SYS_I2C_SOFT)
  722. void i2c_soft_init(void);
  723. void i2c_soft_active(void);
  724. void i2c_soft_tristate(void);
  725. int i2c_soft_read(void);
  726. void i2c_soft_sda(int bit);
  727. void i2c_soft_scl(int bit);
  728. void i2c_soft_delay(void);
  729. #endif
  730. #else
  731. /*
  732. * Probe the given I2C chip address. Returns 0 if a chip responded,
  733. * not 0 on failure.
  734. */
  735. int i2c_probe(uchar chip);
  736. /*
  737. * Read/Write interface:
  738. * chip: I2C chip address, range 0..127
  739. * addr: Memory (register) address within the chip
  740. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  741. * memories, 0 for register type devices with only one
  742. * register)
  743. * buffer: Where to read/write the data
  744. * len: How many bytes to read/write
  745. *
  746. * Returns: 0 on success, not 0 on failure
  747. */
  748. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
  749. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
  750. /*
  751. * Utility routines to read/write registers.
  752. */
  753. static inline u8 i2c_reg_read(u8 addr, u8 reg)
  754. {
  755. u8 buf;
  756. #ifdef CONFIG_8xx
  757. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  758. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  759. #endif
  760. #ifdef DEBUG
  761. printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
  762. #endif
  763. i2c_read(addr, reg, 1, &buf, 1);
  764. return buf;
  765. }
  766. static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
  767. {
  768. #ifdef CONFIG_8xx
  769. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  770. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  771. #endif
  772. #ifdef DEBUG
  773. printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
  774. __func__, addr, reg, val);
  775. #endif
  776. i2c_write(addr, reg, 1, &val, 1);
  777. }
  778. /*
  779. * Functions for setting the current I2C bus and its speed
  780. */
  781. /*
  782. * i2c_set_bus_num:
  783. *
  784. * Change the active I2C bus. Subsequent read/write calls will
  785. * go to this one.
  786. *
  787. * bus - bus index, zero based
  788. *
  789. * Returns: 0 on success, not 0 on failure
  790. *
  791. */
  792. int i2c_set_bus_num(unsigned int bus);
  793. /*
  794. * i2c_get_bus_num:
  795. *
  796. * Returns index of currently active I2C bus. Zero-based.
  797. */
  798. unsigned int i2c_get_bus_num(void);
  799. /*
  800. * i2c_set_bus_speed:
  801. *
  802. * Change the speed of the active I2C bus
  803. *
  804. * speed - bus speed in Hz
  805. *
  806. * Returns: 0 on success, not 0 on failure
  807. *
  808. */
  809. int i2c_set_bus_speed(unsigned int);
  810. /*
  811. * i2c_get_bus_speed:
  812. *
  813. * Returns speed of currently active I2C bus in Hz
  814. */
  815. unsigned int i2c_get_bus_speed(void);
  816. #endif /* CONFIG_SYS_I2C */
  817. /*
  818. * only for backwardcompatibility, should go away if we switched
  819. * completely to new multibus support.
  820. */
  821. #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
  822. # if !defined(CONFIG_SYS_MAX_I2C_BUS)
  823. # define CONFIG_SYS_MAX_I2C_BUS 2
  824. # endif
  825. # define I2C_MULTI_BUS 1
  826. #else
  827. # define CONFIG_SYS_MAX_I2C_BUS 1
  828. # define I2C_MULTI_BUS 0
  829. #endif
  830. /* NOTE: These two functions MUST be always_inline to avoid code growth! */
  831. static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
  832. static inline unsigned int I2C_GET_BUS(void)
  833. {
  834. return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
  835. }
  836. static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
  837. static inline void I2C_SET_BUS(unsigned int bus)
  838. {
  839. if (I2C_MULTI_BUS)
  840. i2c_set_bus_num(bus);
  841. }
  842. /* Multi I2C definitions */
  843. enum {
  844. I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
  845. I2C_8, I2C_9, I2C_10,
  846. };
  847. /* Multi I2C busses handling */
  848. #ifdef CONFIG_SOFT_I2C_MULTI_BUS
  849. extern int get_multi_scl_pin(void);
  850. extern int get_multi_sda_pin(void);
  851. extern int multi_i2c_init(void);
  852. #endif
  853. /**
  854. * Get FDT values for i2c bus.
  855. *
  856. * @param blob Device tree blbo
  857. * @return the number of I2C bus
  858. */
  859. void board_i2c_init(const void *blob);
  860. /**
  861. * Find the I2C bus number by given a FDT I2C node.
  862. *
  863. * @param blob Device tree blbo
  864. * @param node FDT I2C node to find
  865. * @return the number of I2C bus (zero based), or -1 on error
  866. */
  867. int i2c_get_bus_num_fdt(int node);
  868. /**
  869. * Reset the I2C bus represented by the given a FDT I2C node.
  870. *
  871. * @param blob Device tree blbo
  872. * @param node FDT I2C node to find
  873. * @return 0 if port was reset, -1 if not found
  874. */
  875. int i2c_reset_port_fdt(const void *blob, int node);
  876. #endif /* !CONFIG_DM_I2C */
  877. #endif /* _I2C_H_ */