i2c.h 24 KB

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