i2c.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. #ifdef CONFIG_DM_I2C
  26. enum dm_i2c_chip_flags {
  27. DM_I2C_CHIP_10BIT = 1 << 0, /* Use 10-bit addressing */
  28. DM_I2C_CHIP_RD_ADDRESS = 1 << 1, /* Send address for each read byte */
  29. DM_I2C_CHIP_WR_ADDRESS = 1 << 2, /* Send address for each write byte */
  30. };
  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_parentdata(dev) where dev is the
  38. * 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. #endif
  54. };
  55. /**
  56. * struct dm_i2c_bus- information about an i2c bus
  57. *
  58. * An I2C bus contains 0 or more chips on it, each at its own address. The
  59. * bus can operate at different speeds (measured in Hz, typically 100KHz
  60. * or 400KHz).
  61. *
  62. * To obtain this structure, use bus->uclass_priv where bus is the I2C
  63. * bus udevice.
  64. *
  65. * @speed_hz: Bus speed in hertz (typically 100000)
  66. */
  67. struct dm_i2c_bus {
  68. int speed_hz;
  69. };
  70. /**
  71. * i2c_read() - read bytes from an I2C chip
  72. *
  73. * To obtain an I2C device (called a 'chip') given the I2C bus address you
  74. * can use i2c_get_chip(). To obtain a bus by bus number use
  75. * uclass_get_device_by_seq(UCLASS_I2C, <bus number>).
  76. *
  77. * To set the address length of a devce use i2c_set_addr_len(). It
  78. * defaults to 1.
  79. *
  80. * @dev: Chip to read from
  81. * @offset: Offset within chip to start reading
  82. * @buffer: Place to put data
  83. * @len: Number of bytes to read
  84. *
  85. * @return 0 on success, -ve on failure
  86. */
  87. int i2c_read(struct udevice *dev, uint offset, uint8_t *buffer,
  88. int len);
  89. /**
  90. * i2c_write() - write bytes to an I2C chip
  91. *
  92. * See notes for i2c_read() above.
  93. *
  94. * @dev: Chip to write to
  95. * @offset: Offset within chip to start writing
  96. * @buffer: Buffer containing data to write
  97. * @len: Number of bytes to write
  98. *
  99. * @return 0 on success, -ve on failure
  100. */
  101. int i2c_write(struct udevice *dev, uint offset, const uint8_t *buffer,
  102. int len);
  103. /**
  104. * i2c_probe() - probe a particular chip address
  105. *
  106. * This can be useful to check for the existence of a chip on the bus.
  107. * It is typically implemented by writing the chip address to the bus
  108. * and checking that the chip replies with an ACK.
  109. *
  110. * @bus: Bus to probe
  111. * @chip_addr: 7-bit address to probe (10-bit and others are not supported)
  112. * @chip_flags: Flags for the probe (see enum dm_i2c_chip_flags)
  113. * @devp: Returns the device found, or NULL if none
  114. * @return 0 if a chip was found at that address, -ve if not
  115. */
  116. int i2c_probe(struct udevice *bus, uint chip_addr, uint chip_flags,
  117. struct udevice **devp);
  118. /**
  119. * i2c_set_bus_speed() - set the speed of a bus
  120. *
  121. * @bus: Bus to adjust
  122. * @speed: Requested speed in Hz
  123. * @return 0 if OK, -EINVAL for invalid values
  124. */
  125. int i2c_set_bus_speed(struct udevice *bus, unsigned int speed);
  126. /**
  127. * i2c_get_bus_speed() - get the speed of a bus
  128. *
  129. * @bus: Bus to check
  130. * @return speed of selected I2C bus in Hz, -ve on error
  131. */
  132. int i2c_get_bus_speed(struct udevice *bus);
  133. /**
  134. * i2c_set_chip_flags() - set flags for a chip
  135. *
  136. * Typically addresses are 7 bits, but for 10-bit addresses you should set
  137. * flags to DM_I2C_CHIP_10BIT. All accesses will then use 10-bit addressing.
  138. *
  139. * @dev: Chip to adjust
  140. * @flags: New flags
  141. * @return 0 if OK, -EINVAL if value is unsupported, other -ve value on error
  142. */
  143. int i2c_set_chip_flags(struct udevice *dev, uint flags);
  144. /**
  145. * i2c_get_chip_flags() - get flags for a chip
  146. *
  147. * @dev: Chip to check
  148. * @flagsp: Place to put flags
  149. * @return 0 if OK, other -ve value on error
  150. */
  151. int i2c_get_chip_flags(struct udevice *dev, uint *flagsp);
  152. /**
  153. * i2c_set_offset_len() - set the offset length for a chip
  154. *
  155. * The offset used to access a chip may be up to 4 bytes long. Typically it
  156. * is only 1 byte, which is enough for chips with 256 bytes of memory or
  157. * registers. The default value is 1, but you can call this function to
  158. * change it.
  159. *
  160. * @offset_len: New offset length value (typically 1 or 2)
  161. */
  162. int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
  163. /**
  164. * i2c_deblock() - recover a bus that is in an unknown state
  165. *
  166. * See the deblock() method in 'struct dm_i2c_ops' for full information
  167. *
  168. * @bus: Bus to recover
  169. * @return 0 if OK, -ve on error
  170. */
  171. int i2c_deblock(struct udevice *bus);
  172. /*
  173. * Not all of these flags are implemented in the U-Boot API
  174. */
  175. enum dm_i2c_msg_flags {
  176. I2C_M_TEN = 0x0010, /* ten-bit chip address */
  177. I2C_M_RD = 0x0001, /* read data, from slave to master */
  178. I2C_M_STOP = 0x8000, /* send stop after this message */
  179. I2C_M_NOSTART = 0x4000, /* no start before this message */
  180. I2C_M_REV_DIR_ADDR = 0x2000, /* invert polarity of R/W bit */
  181. I2C_M_IGNORE_NAK = 0x1000, /* continue after NAK */
  182. I2C_M_NO_RD_ACK = 0x0800, /* skip the Ack bit on reads */
  183. I2C_M_RECV_LEN = 0x0400, /* length is first received byte */
  184. };
  185. /**
  186. * struct i2c_msg - an I2C message
  187. *
  188. * @addr: Slave address
  189. * @flags: Flags (see enum dm_i2c_msg_flags)
  190. * @len: Length of buffer in bytes, may be 0 for a probe
  191. * @buf: Buffer to send/receive, or NULL if no data
  192. */
  193. struct i2c_msg {
  194. uint addr;
  195. uint flags;
  196. uint len;
  197. u8 *buf;
  198. };
  199. /**
  200. * struct i2c_msg_list - a list of I2C messages
  201. *
  202. * This is called i2c_rdwr_ioctl_data in Linux but the name does not seem
  203. * appropriate in U-Boot.
  204. *
  205. * @msg: Pointer to i2c_msg array
  206. * @nmsgs: Number of elements in the array
  207. */
  208. struct i2c_msg_list {
  209. struct i2c_msg *msgs;
  210. uint nmsgs;
  211. };
  212. /**
  213. * struct dm_i2c_ops - driver operations for I2C uclass
  214. *
  215. * Drivers should support these operations unless otherwise noted. These
  216. * operations are intended to be used by uclass code, not directly from
  217. * other code.
  218. */
  219. struct dm_i2c_ops {
  220. /**
  221. * xfer() - transfer a list of I2C messages
  222. *
  223. * @bus: Bus to read from
  224. * @msg: List of messages to transfer
  225. * @nmsgs: Number of messages in the list
  226. * @return 0 if OK, -EREMOTEIO if the slave did not ACK a byte,
  227. * -ECOMM if the speed cannot be supported, -EPROTO if the chip
  228. * flags cannot be supported, other -ve value on some other error
  229. */
  230. int (*xfer)(struct udevice *bus, struct i2c_msg *msg, int nmsgs);
  231. /**
  232. * probe_chip() - probe for the presense of a chip address
  233. *
  234. * This function is optional. If omitted, the uclass will send a zero
  235. * length message instead.
  236. *
  237. * @bus: Bus to probe
  238. * @chip_addr: Chip address to probe
  239. * @chip_flags: Probe flags (enum dm_i2c_chip_flags)
  240. * @return 0 if chip was found, -EREMOTEIO if not, -ENOSYS to fall back
  241. * to default probem other -ve value on error
  242. */
  243. int (*probe_chip)(struct udevice *bus, uint chip_addr, uint chip_flags);
  244. /**
  245. * set_bus_speed() - set the speed of a bus (optional)
  246. *
  247. * The bus speed value will be updated by the uclass if this function
  248. * does not return an error. This method is optional - if it is not
  249. * provided then the driver can read the speed from
  250. * bus->uclass_priv->speed_hz
  251. *
  252. * @bus: Bus to adjust
  253. * @speed: Requested speed in Hz
  254. * @return 0 if OK, -EINVAL for invalid values
  255. */
  256. int (*set_bus_speed)(struct udevice *bus, unsigned int speed);
  257. /**
  258. * get_bus_speed() - get the speed of a bus (optional)
  259. *
  260. * Normally this can be provided by the uclass, but if you want your
  261. * driver to check the bus speed by looking at the hardware, you can
  262. * implement that here. This method is optional. This method would
  263. * normally be expected to return bus->uclass_priv->speed_hz.
  264. *
  265. * @bus: Bus to check
  266. * @return speed of selected I2C bus in Hz, -ve on error
  267. */
  268. int (*get_bus_speed)(struct udevice *bus);
  269. /**
  270. * set_flags() - set the flags for a chip (optional)
  271. *
  272. * This is generally implemented by the uclass, but drivers can
  273. * check the value to ensure that unsupported options are not used.
  274. * This method is optional. If provided, this method will always be
  275. * called when the flags change.
  276. *
  277. * @dev: Chip to adjust
  278. * @flags: New flags value
  279. * @return 0 if OK, -EINVAL if value is unsupported
  280. */
  281. int (*set_flags)(struct udevice *dev, uint flags);
  282. /**
  283. * deblock() - recover a bus that is in an unknown state
  284. *
  285. * I2C is a synchronous protocol and resets of the processor in the
  286. * middle of an access can block the I2C Bus until a powerdown of
  287. * the full unit is done. This is because slaves can be stuck
  288. * waiting for addition bus transitions for a transaction that will
  289. * never complete. Resetting the I2C master does not help. The only
  290. * way is to force the bus through a series of transitions to make
  291. * sure that all slaves are done with the transaction. This method
  292. * performs this 'deblocking' if support by the driver.
  293. *
  294. * This method is optional.
  295. */
  296. int (*deblock)(struct udevice *bus);
  297. };
  298. #define i2c_get_ops(dev) ((struct dm_i2c_ops *)(dev)->driver->ops)
  299. /**
  300. * i2c_get_chip() - get a device to use to access a chip on a bus
  301. *
  302. * This returns the device for the given chip address. The device can then
  303. * be used with calls to i2c_read(), i2c_write(), i2c_probe(), etc.
  304. *
  305. * @bus: Bus to examine
  306. * @chip_addr: Chip address for the new device
  307. * @devp: Returns pointer to new device if found or -ENODEV if not
  308. * found
  309. */
  310. int i2c_get_chip(struct udevice *bus, uint chip_addr, struct udevice **devp);
  311. /**
  312. * i2c_get_chip() - get a device to use to access a chip on a bus number
  313. *
  314. * This returns the device for the given chip address on a particular bus
  315. * number.
  316. *
  317. * @busnum: Bus number to examine
  318. * @chip_addr: Chip address for the new device
  319. * @devp: Returns pointer to new device if found or -ENODEV if not
  320. * found
  321. */
  322. int i2c_get_chip_for_busnum(int busnum, int chip_addr, struct udevice **devp);
  323. /**
  324. * i2c_chip_ofdata_to_platdata() - Decode standard I2C platform data
  325. *
  326. * This decodes the chip address from a device tree node and puts it into
  327. * its dm_i2c_chip structure. This should be called in your driver's
  328. * ofdata_to_platdata() method.
  329. *
  330. * @blob: Device tree blob
  331. * @node: Node offset to read from
  332. * @spi: Place to put the decoded information
  333. */
  334. int i2c_chip_ofdata_to_platdata(const void *blob, int node,
  335. struct dm_i2c_chip *chip);
  336. #endif
  337. #ifndef CONFIG_DM_I2C
  338. /*
  339. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  340. *
  341. * The implementation MUST NOT use static or global variables if the
  342. * I2C routines are used to read SDRAM configuration information
  343. * because this is done before the memories are initialized. Limited
  344. * use of stack-based variables are OK (the initial stack size is
  345. * limited).
  346. *
  347. * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
  348. */
  349. /*
  350. * Configuration items.
  351. */
  352. #define I2C_RXTX_LEN 128 /* maximum tx/rx buffer length */
  353. #if !defined(CONFIG_SYS_I2C_MAX_HOPS)
  354. /* no muxes used bus = i2c adapters */
  355. #define CONFIG_SYS_I2C_DIRECT_BUS 1
  356. #define CONFIG_SYS_I2C_MAX_HOPS 0
  357. #define CONFIG_SYS_NUM_I2C_BUSES ll_entry_count(struct i2c_adapter, i2c)
  358. #else
  359. /* we use i2c muxes */
  360. #undef CONFIG_SYS_I2C_DIRECT_BUS
  361. #endif
  362. /* define the I2C bus number for RTC and DTT if not already done */
  363. #if !defined(CONFIG_SYS_RTC_BUS_NUM)
  364. #define CONFIG_SYS_RTC_BUS_NUM 0
  365. #endif
  366. #if !defined(CONFIG_SYS_DTT_BUS_NUM)
  367. #define CONFIG_SYS_DTT_BUS_NUM 0
  368. #endif
  369. #if !defined(CONFIG_SYS_SPD_BUS_NUM)
  370. #define CONFIG_SYS_SPD_BUS_NUM 0
  371. #endif
  372. struct i2c_adapter {
  373. void (*init)(struct i2c_adapter *adap, int speed,
  374. int slaveaddr);
  375. int (*probe)(struct i2c_adapter *adap, uint8_t chip);
  376. int (*read)(struct i2c_adapter *adap, uint8_t chip,
  377. uint addr, int alen, uint8_t *buffer,
  378. int len);
  379. int (*write)(struct i2c_adapter *adap, uint8_t chip,
  380. uint addr, int alen, uint8_t *buffer,
  381. int len);
  382. uint (*set_bus_speed)(struct i2c_adapter *adap,
  383. uint speed);
  384. int speed;
  385. int waitdelay;
  386. int slaveaddr;
  387. int init_done;
  388. int hwadapnr;
  389. char *name;
  390. };
  391. #define U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
  392. _set_speed, _speed, _slaveaddr, _hwadapnr, _name) \
  393. { \
  394. .init = _init, \
  395. .probe = _probe, \
  396. .read = _read, \
  397. .write = _write, \
  398. .set_bus_speed = _set_speed, \
  399. .speed = _speed, \
  400. .slaveaddr = _slaveaddr, \
  401. .init_done = 0, \
  402. .hwadapnr = _hwadapnr, \
  403. .name = #_name \
  404. };
  405. #define U_BOOT_I2C_ADAP_COMPLETE(_name, _init, _probe, _read, _write, \
  406. _set_speed, _speed, _slaveaddr, _hwadapnr) \
  407. ll_entry_declare(struct i2c_adapter, _name, i2c) = \
  408. U_BOOT_I2C_MKENT_COMPLETE(_init, _probe, _read, _write, \
  409. _set_speed, _speed, _slaveaddr, _hwadapnr, _name);
  410. struct i2c_adapter *i2c_get_adapter(int index);
  411. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  412. struct i2c_mux {
  413. int id;
  414. char name[16];
  415. };
  416. struct i2c_next_hop {
  417. struct i2c_mux mux;
  418. uint8_t chip;
  419. uint8_t channel;
  420. };
  421. struct i2c_bus_hose {
  422. int adapter;
  423. struct i2c_next_hop next_hop[CONFIG_SYS_I2C_MAX_HOPS];
  424. };
  425. #define I2C_NULL_HOP {{-1, ""}, 0, 0}
  426. extern struct i2c_bus_hose i2c_bus[];
  427. #define I2C_ADAPTER(bus) i2c_bus[bus].adapter
  428. #else
  429. #define I2C_ADAPTER(bus) bus
  430. #endif
  431. #define I2C_BUS gd->cur_i2c_bus
  432. #define I2C_ADAP_NR(bus) i2c_get_adapter(I2C_ADAPTER(bus))
  433. #define I2C_ADAP I2C_ADAP_NR(gd->cur_i2c_bus)
  434. #define I2C_ADAP_HWNR (I2C_ADAP->hwadapnr)
  435. #ifndef CONFIG_SYS_I2C_DIRECT_BUS
  436. #define I2C_MUX_PCA9540_ID 1
  437. #define I2C_MUX_PCA9540 {I2C_MUX_PCA9540_ID, "PCA9540B"}
  438. #define I2C_MUX_PCA9542_ID 2
  439. #define I2C_MUX_PCA9542 {I2C_MUX_PCA9542_ID, "PCA9542A"}
  440. #define I2C_MUX_PCA9544_ID 3
  441. #define I2C_MUX_PCA9544 {I2C_MUX_PCA9544_ID, "PCA9544A"}
  442. #define I2C_MUX_PCA9547_ID 4
  443. #define I2C_MUX_PCA9547 {I2C_MUX_PCA9547_ID, "PCA9547A"}
  444. #define I2C_MUX_PCA9548_ID 5
  445. #define I2C_MUX_PCA9548 {I2C_MUX_PCA9548_ID, "PCA9548"}
  446. #endif
  447. #ifndef I2C_SOFT_DECLARATIONS
  448. # if defined(CONFIG_MPC8260)
  449. # define I2C_SOFT_DECLARATIONS volatile ioport_t *iop = ioport_addr((immap_t *)CONFIG_SYS_IMMR, I2C_PORT);
  450. # elif defined(CONFIG_8xx)
  451. # define I2C_SOFT_DECLARATIONS volatile immap_t *immr = (immap_t *)CONFIG_SYS_IMMR;
  452. # elif (defined(CONFIG_AT91RM9200) || \
  453. defined(CONFIG_AT91SAM9260) || defined(CONFIG_AT91SAM9261) || \
  454. defined(CONFIG_AT91SAM9263))
  455. # define I2C_SOFT_DECLARATIONS at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIOA;
  456. # else
  457. # define I2C_SOFT_DECLARATIONS
  458. # endif
  459. #endif
  460. #ifdef CONFIG_8xx
  461. /* Set default value for the I2C bus speed on 8xx. In the
  462. * future, we'll define these in all 8xx board config files.
  463. */
  464. #ifndef CONFIG_SYS_I2C_SPEED
  465. #define CONFIG_SYS_I2C_SPEED 50000
  466. #endif
  467. #endif
  468. /*
  469. * Many boards/controllers/drivers don't support an I2C slave interface so
  470. * provide a default slave address for them for use in common code. A real
  471. * value for CONFIG_SYS_I2C_SLAVE should be defined for any board which does
  472. * support a slave interface.
  473. */
  474. #ifndef CONFIG_SYS_I2C_SLAVE
  475. #define CONFIG_SYS_I2C_SLAVE 0xfe
  476. #endif
  477. /*
  478. * Initialization, must be called once on start up, may be called
  479. * repeatedly to change the speed and slave addresses.
  480. */
  481. void i2c_init(int speed, int slaveaddr);
  482. void i2c_init_board(void);
  483. #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
  484. void i2c_board_late_init(void);
  485. #endif
  486. #ifdef CONFIG_SYS_I2C
  487. /*
  488. * i2c_get_bus_num:
  489. *
  490. * Returns index of currently active I2C bus. Zero-based.
  491. */
  492. unsigned int i2c_get_bus_num(void);
  493. /*
  494. * i2c_set_bus_num:
  495. *
  496. * Change the active I2C bus. Subsequent read/write calls will
  497. * go to this one.
  498. *
  499. * bus - bus index, zero based
  500. *
  501. * Returns: 0 on success, not 0 on failure
  502. *
  503. */
  504. int i2c_set_bus_num(unsigned int bus);
  505. /*
  506. * i2c_init_all():
  507. *
  508. * Initializes all I2C adapters in the system. All i2c_adap structures must
  509. * be initialized beforehead with function pointers and data, including
  510. * speed and slaveaddr. Returns 0 on success, non-0 on failure.
  511. */
  512. void i2c_init_all(void);
  513. /*
  514. * Probe the given I2C chip address. Returns 0 if a chip responded,
  515. * not 0 on failure.
  516. */
  517. int i2c_probe(uint8_t chip);
  518. /*
  519. * Read/Write interface:
  520. * chip: I2C chip address, range 0..127
  521. * addr: Memory (register) address within the chip
  522. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  523. * memories, 0 for register type devices with only one
  524. * register)
  525. * buffer: Where to read/write the data
  526. * len: How many bytes to read/write
  527. *
  528. * Returns: 0 on success, not 0 on failure
  529. */
  530. int i2c_read(uint8_t chip, unsigned int addr, int alen,
  531. uint8_t *buffer, int len);
  532. int i2c_write(uint8_t chip, unsigned int addr, int alen,
  533. uint8_t *buffer, int len);
  534. /*
  535. * Utility routines to read/write registers.
  536. */
  537. uint8_t i2c_reg_read(uint8_t addr, uint8_t reg);
  538. void i2c_reg_write(uint8_t addr, uint8_t reg, uint8_t val);
  539. /*
  540. * i2c_set_bus_speed:
  541. *
  542. * Change the speed of the active I2C bus
  543. *
  544. * speed - bus speed in Hz
  545. *
  546. * Returns: new bus speed
  547. *
  548. */
  549. unsigned int i2c_set_bus_speed(unsigned int speed);
  550. /*
  551. * i2c_get_bus_speed:
  552. *
  553. * Returns speed of currently active I2C bus in Hz
  554. */
  555. unsigned int i2c_get_bus_speed(void);
  556. /*
  557. * i2c_reloc_fixup:
  558. *
  559. * Adjusts I2C pointers after U-Boot is relocated to DRAM
  560. */
  561. void i2c_reloc_fixup(void);
  562. #if defined(CONFIG_SYS_I2C_SOFT)
  563. void i2c_soft_init(void);
  564. void i2c_soft_active(void);
  565. void i2c_soft_tristate(void);
  566. int i2c_soft_read(void);
  567. void i2c_soft_sda(int bit);
  568. void i2c_soft_scl(int bit);
  569. void i2c_soft_delay(void);
  570. #endif
  571. #else
  572. /*
  573. * Probe the given I2C chip address. Returns 0 if a chip responded,
  574. * not 0 on failure.
  575. */
  576. int i2c_probe(uchar chip);
  577. /*
  578. * Read/Write interface:
  579. * chip: I2C chip address, range 0..127
  580. * addr: Memory (register) address within the chip
  581. * alen: Number of bytes to use for addr (typically 1, 2 for larger
  582. * memories, 0 for register type devices with only one
  583. * register)
  584. * buffer: Where to read/write the data
  585. * len: How many bytes to read/write
  586. *
  587. * Returns: 0 on success, not 0 on failure
  588. */
  589. int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len);
  590. int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len);
  591. /*
  592. * Utility routines to read/write registers.
  593. */
  594. static inline u8 i2c_reg_read(u8 addr, u8 reg)
  595. {
  596. u8 buf;
  597. #ifdef CONFIG_8xx
  598. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  599. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  600. #endif
  601. #ifdef DEBUG
  602. printf("%s: addr=0x%02x, reg=0x%02x\n", __func__, addr, reg);
  603. #endif
  604. i2c_read(addr, reg, 1, &buf, 1);
  605. return buf;
  606. }
  607. static inline void i2c_reg_write(u8 addr, u8 reg, u8 val)
  608. {
  609. #ifdef CONFIG_8xx
  610. /* MPC8xx needs this. Maybe one day we can get rid of it. */
  611. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  612. #endif
  613. #ifdef DEBUG
  614. printf("%s: addr=0x%02x, reg=0x%02x, val=0x%02x\n",
  615. __func__, addr, reg, val);
  616. #endif
  617. i2c_write(addr, reg, 1, &val, 1);
  618. }
  619. /*
  620. * Functions for setting the current I2C bus and its speed
  621. */
  622. /*
  623. * i2c_set_bus_num:
  624. *
  625. * Change the active I2C bus. Subsequent read/write calls will
  626. * go to this one.
  627. *
  628. * bus - bus index, zero based
  629. *
  630. * Returns: 0 on success, not 0 on failure
  631. *
  632. */
  633. int i2c_set_bus_num(unsigned int bus);
  634. /*
  635. * i2c_get_bus_num:
  636. *
  637. * Returns index of currently active I2C bus. Zero-based.
  638. */
  639. unsigned int i2c_get_bus_num(void);
  640. /*
  641. * i2c_set_bus_speed:
  642. *
  643. * Change the speed of the active I2C bus
  644. *
  645. * speed - bus speed in Hz
  646. *
  647. * Returns: 0 on success, not 0 on failure
  648. *
  649. */
  650. int i2c_set_bus_speed(unsigned int);
  651. /*
  652. * i2c_get_bus_speed:
  653. *
  654. * Returns speed of currently active I2C bus in Hz
  655. */
  656. unsigned int i2c_get_bus_speed(void);
  657. #endif /* CONFIG_SYS_I2C */
  658. /*
  659. * only for backwardcompatibility, should go away if we switched
  660. * completely to new multibus support.
  661. */
  662. #if defined(CONFIG_SYS_I2C) || defined(CONFIG_I2C_MULTI_BUS)
  663. # if !defined(CONFIG_SYS_MAX_I2C_BUS)
  664. # define CONFIG_SYS_MAX_I2C_BUS 2
  665. # endif
  666. # define I2C_MULTI_BUS 1
  667. #else
  668. # define CONFIG_SYS_MAX_I2C_BUS 1
  669. # define I2C_MULTI_BUS 0
  670. #endif
  671. /* NOTE: These two functions MUST be always_inline to avoid code growth! */
  672. static inline unsigned int I2C_GET_BUS(void) __attribute__((always_inline));
  673. static inline unsigned int I2C_GET_BUS(void)
  674. {
  675. return I2C_MULTI_BUS ? i2c_get_bus_num() : 0;
  676. }
  677. static inline void I2C_SET_BUS(unsigned int bus) __attribute__((always_inline));
  678. static inline void I2C_SET_BUS(unsigned int bus)
  679. {
  680. if (I2C_MULTI_BUS)
  681. i2c_set_bus_num(bus);
  682. }
  683. /* Multi I2C definitions */
  684. enum {
  685. I2C_0, I2C_1, I2C_2, I2C_3, I2C_4, I2C_5, I2C_6, I2C_7,
  686. I2C_8, I2C_9, I2C_10,
  687. };
  688. /* Multi I2C busses handling */
  689. #ifdef CONFIG_SOFT_I2C_MULTI_BUS
  690. extern int get_multi_scl_pin(void);
  691. extern int get_multi_sda_pin(void);
  692. extern int multi_i2c_init(void);
  693. #endif
  694. /**
  695. * Get FDT values for i2c bus.
  696. *
  697. * @param blob Device tree blbo
  698. * @return the number of I2C bus
  699. */
  700. void board_i2c_init(const void *blob);
  701. /**
  702. * Find the I2C bus number by given a FDT I2C node.
  703. *
  704. * @param blob Device tree blbo
  705. * @param node FDT I2C node to find
  706. * @return the number of I2C bus (zero based), or -1 on error
  707. */
  708. int i2c_get_bus_num_fdt(int node);
  709. /**
  710. * Reset the I2C bus represented by the given a FDT I2C node.
  711. *
  712. * @param blob Device tree blbo
  713. * @param node FDT I2C node to find
  714. * @return 0 if port was reset, -1 if not found
  715. */
  716. int i2c_reset_port_fdt(const void *blob, int node);
  717. #endif /* !CONFIG_DM_I2C */
  718. #endif /* _I2C_H_ */