i2c.h 24 KB

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