i2c.h 24 KB

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