i2c-core-smbus.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux I2C core SMBus and SMBus emulation code
  4. *
  5. * This file contains the SMBus functions which are always included in the I2C
  6. * core because they can be emulated via I2C. SMBus specific extensions
  7. * (e.g. smbalert) are handled in a separate i2c-smbus module.
  8. *
  9. * All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
  10. * SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
  11. * Jean Delvare <jdelvare@suse.de>
  12. */
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/i2c.h>
  16. #include <linux/i2c-smbus.h>
  17. #include <linux/property.h>
  18. #include <linux/slab.h>
  19. #include <linux/string_choices.h>
  20. #include "i2c-core.h"
  21. #define CREATE_TRACE_POINTS
  22. #include <trace/events/smbus.h>
  23. /* The SMBus parts */
  24. #define POLY (0x1070U << 3)
  25. static u8 crc8(u16 data)
  26. {
  27. int i;
  28. for (i = 0; i < 8; i++) {
  29. if (data & 0x8000)
  30. data = data ^ POLY;
  31. data = data << 1;
  32. }
  33. return (u8)(data >> 8);
  34. }
  35. /**
  36. * i2c_smbus_pec - Incremental CRC8 over the given input data array
  37. * @crc: previous return crc8 value
  38. * @p: pointer to data buffer.
  39. * @count: number of bytes in data buffer.
  40. *
  41. * Incremental CRC8 over count bytes in the array pointed to by p
  42. */
  43. u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
  44. {
  45. int i;
  46. for (i = 0; i < count; i++)
  47. crc = crc8((crc ^ p[i]) << 8);
  48. return crc;
  49. }
  50. EXPORT_SYMBOL(i2c_smbus_pec);
  51. /* Assume a 7-bit address, which is reasonable for SMBus */
  52. static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
  53. {
  54. /* The address will be sent first */
  55. u8 addr = i2c_8bit_addr_from_msg(msg);
  56. pec = i2c_smbus_pec(pec, &addr, 1);
  57. /* The data buffer follows */
  58. return i2c_smbus_pec(pec, msg->buf, msg->len);
  59. }
  60. /* Used for write only transactions */
  61. static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
  62. {
  63. msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
  64. msg->len++;
  65. }
  66. /* Return <0 on CRC error
  67. If there was a write before this read (most cases) we need to take the
  68. partial CRC from the write part into account.
  69. Note that this function does modify the message (we need to decrease the
  70. message length to hide the CRC byte from the caller). */
  71. static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
  72. {
  73. u8 rpec = msg->buf[--msg->len];
  74. cpec = i2c_smbus_msg_pec(cpec, msg);
  75. if (rpec != cpec) {
  76. pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
  77. rpec, cpec);
  78. return -EBADMSG;
  79. }
  80. return 0;
  81. }
  82. /**
  83. * i2c_smbus_read_byte - SMBus "receive byte" protocol
  84. * @client: Handle to slave device
  85. *
  86. * This executes the SMBus "receive byte" protocol, returning negative errno
  87. * else the byte received from the device.
  88. */
  89. s32 i2c_smbus_read_byte(const struct i2c_client *client)
  90. {
  91. union i2c_smbus_data data;
  92. int status;
  93. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  94. I2C_SMBUS_READ, 0,
  95. I2C_SMBUS_BYTE, &data);
  96. return (status < 0) ? status : data.byte;
  97. }
  98. EXPORT_SYMBOL(i2c_smbus_read_byte);
  99. /**
  100. * i2c_smbus_write_byte - SMBus "send byte" protocol
  101. * @client: Handle to slave device
  102. * @value: Byte to be sent
  103. *
  104. * This executes the SMBus "send byte" protocol, returning negative errno
  105. * else zero on success.
  106. */
  107. s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
  108. {
  109. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  110. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  111. }
  112. EXPORT_SYMBOL(i2c_smbus_write_byte);
  113. /**
  114. * i2c_smbus_read_byte_data - SMBus "read byte" protocol
  115. * @client: Handle to slave device
  116. * @command: Byte interpreted by slave
  117. *
  118. * This executes the SMBus "read byte" protocol, returning negative errno
  119. * else a data byte received from the device.
  120. */
  121. s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
  122. {
  123. union i2c_smbus_data data;
  124. int status;
  125. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  126. I2C_SMBUS_READ, command,
  127. I2C_SMBUS_BYTE_DATA, &data);
  128. return (status < 0) ? status : data.byte;
  129. }
  130. EXPORT_SYMBOL(i2c_smbus_read_byte_data);
  131. /**
  132. * i2c_smbus_write_byte_data - SMBus "write byte" protocol
  133. * @client: Handle to slave device
  134. * @command: Byte interpreted by slave
  135. * @value: Byte being written
  136. *
  137. * This executes the SMBus "write byte" protocol, returning negative errno
  138. * else zero on success.
  139. */
  140. s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
  141. u8 value)
  142. {
  143. union i2c_smbus_data data;
  144. data.byte = value;
  145. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  146. I2C_SMBUS_WRITE, command,
  147. I2C_SMBUS_BYTE_DATA, &data);
  148. }
  149. EXPORT_SYMBOL(i2c_smbus_write_byte_data);
  150. /**
  151. * i2c_smbus_read_word_data - SMBus "read word" protocol
  152. * @client: Handle to slave device
  153. * @command: Byte interpreted by slave
  154. *
  155. * This executes the SMBus "read word" protocol, returning negative errno
  156. * else a 16-bit unsigned "word" received from the device.
  157. */
  158. s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
  159. {
  160. union i2c_smbus_data data;
  161. int status;
  162. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  163. I2C_SMBUS_READ, command,
  164. I2C_SMBUS_WORD_DATA, &data);
  165. return (status < 0) ? status : data.word;
  166. }
  167. EXPORT_SYMBOL(i2c_smbus_read_word_data);
  168. /**
  169. * i2c_smbus_write_word_data - SMBus "write word" protocol
  170. * @client: Handle to slave device
  171. * @command: Byte interpreted by slave
  172. * @value: 16-bit "word" being written
  173. *
  174. * This executes the SMBus "write word" protocol, returning negative errno
  175. * else zero on success.
  176. */
  177. s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
  178. u16 value)
  179. {
  180. union i2c_smbus_data data;
  181. data.word = value;
  182. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  183. I2C_SMBUS_WRITE, command,
  184. I2C_SMBUS_WORD_DATA, &data);
  185. }
  186. EXPORT_SYMBOL(i2c_smbus_write_word_data);
  187. /**
  188. * i2c_smbus_read_block_data - SMBus "block read" protocol
  189. * @client: Handle to slave device
  190. * @command: Byte interpreted by slave
  191. * @values: Byte array into which data will be read; big enough to hold
  192. * the data returned by the slave. SMBus allows at most 32 bytes.
  193. *
  194. * This executes the SMBus "block read" protocol, returning negative errno
  195. * else the number of data bytes in the slave's response.
  196. *
  197. * Note that using this function requires that the client's adapter support
  198. * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
  199. * support this; its emulation through I2C messaging relies on a specific
  200. * mechanism (I2C_M_RECV_LEN) which may not be implemented.
  201. */
  202. s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
  203. u8 *values)
  204. {
  205. union i2c_smbus_data data;
  206. int status;
  207. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  208. I2C_SMBUS_READ, command,
  209. I2C_SMBUS_BLOCK_DATA, &data);
  210. if (status)
  211. return status;
  212. memcpy(values, &data.block[1], data.block[0]);
  213. return data.block[0];
  214. }
  215. EXPORT_SYMBOL(i2c_smbus_read_block_data);
  216. /**
  217. * i2c_smbus_write_block_data - SMBus "block write" protocol
  218. * @client: Handle to slave device
  219. * @command: Byte interpreted by slave
  220. * @length: Size of data block; SMBus allows at most 32 bytes
  221. * @values: Byte array which will be written.
  222. *
  223. * This executes the SMBus "block write" protocol, returning negative errno
  224. * else zero on success.
  225. */
  226. s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
  227. u8 length, const u8 *values)
  228. {
  229. union i2c_smbus_data data;
  230. if (length > I2C_SMBUS_BLOCK_MAX)
  231. length = I2C_SMBUS_BLOCK_MAX;
  232. data.block[0] = length;
  233. memcpy(&data.block[1], values, length);
  234. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  235. I2C_SMBUS_WRITE, command,
  236. I2C_SMBUS_BLOCK_DATA, &data);
  237. }
  238. EXPORT_SYMBOL(i2c_smbus_write_block_data);
  239. /* Returns the number of read bytes */
  240. s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
  241. u8 length, u8 *values)
  242. {
  243. union i2c_smbus_data data;
  244. int status;
  245. if (length > I2C_SMBUS_BLOCK_MAX)
  246. length = I2C_SMBUS_BLOCK_MAX;
  247. data.block[0] = length;
  248. status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  249. I2C_SMBUS_READ, command,
  250. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  251. if (status < 0)
  252. return status;
  253. memcpy(values, &data.block[1], data.block[0]);
  254. return data.block[0];
  255. }
  256. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
  257. s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
  258. u8 length, const u8 *values)
  259. {
  260. union i2c_smbus_data data;
  261. if (length > I2C_SMBUS_BLOCK_MAX)
  262. length = I2C_SMBUS_BLOCK_MAX;
  263. data.block[0] = length;
  264. memcpy(data.block + 1, values, length);
  265. return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
  266. I2C_SMBUS_WRITE, command,
  267. I2C_SMBUS_I2C_BLOCK_DATA, &data);
  268. }
  269. EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
  270. static void i2c_smbus_try_get_dmabuf(struct i2c_msg *msg, u8 init_val)
  271. {
  272. bool is_read = msg->flags & I2C_M_RD;
  273. unsigned char *dma_buf;
  274. dma_buf = kzalloc(I2C_SMBUS_BLOCK_MAX + (is_read ? 2 : 3), GFP_KERNEL);
  275. if (!dma_buf)
  276. return;
  277. msg->buf = dma_buf;
  278. msg->flags |= I2C_M_DMA_SAFE;
  279. if (init_val)
  280. msg->buf[0] = init_val;
  281. }
  282. /*
  283. * Simulate a SMBus command using the I2C protocol.
  284. * No checking of parameters is done!
  285. */
  286. static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
  287. unsigned short flags,
  288. char read_write, u8 command, int size,
  289. union i2c_smbus_data *data)
  290. {
  291. /*
  292. * So we need to generate a series of msgs. In the case of writing, we
  293. * need to use only one message; when reading, we need two. We
  294. * initialize most things with sane defaults, to keep the code below
  295. * somewhat simpler.
  296. */
  297. unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
  298. unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
  299. int nmsgs = read_write == I2C_SMBUS_READ ? 2 : 1;
  300. u8 partial_pec = 0;
  301. int status;
  302. struct i2c_msg msg[2] = {
  303. {
  304. .addr = addr,
  305. .flags = flags,
  306. .len = 1,
  307. .buf = msgbuf0,
  308. }, {
  309. .addr = addr,
  310. .flags = flags | I2C_M_RD,
  311. .len = 0,
  312. .buf = msgbuf1,
  313. },
  314. };
  315. bool wants_pec = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
  316. && size != I2C_SMBUS_I2C_BLOCK_DATA);
  317. msgbuf0[0] = command;
  318. switch (size) {
  319. case I2C_SMBUS_QUICK:
  320. msg[0].len = 0;
  321. /* Special case: The read/write field is used as data */
  322. msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
  323. I2C_M_RD : 0);
  324. nmsgs = 1;
  325. break;
  326. case I2C_SMBUS_BYTE:
  327. if (read_write == I2C_SMBUS_READ) {
  328. /* Special case: only a read! */
  329. msg[0].flags = I2C_M_RD | flags;
  330. nmsgs = 1;
  331. }
  332. break;
  333. case I2C_SMBUS_BYTE_DATA:
  334. if (read_write == I2C_SMBUS_READ)
  335. msg[1].len = 1;
  336. else {
  337. msg[0].len = 2;
  338. msgbuf0[1] = data->byte;
  339. }
  340. break;
  341. case I2C_SMBUS_WORD_DATA:
  342. if (read_write == I2C_SMBUS_READ)
  343. msg[1].len = 2;
  344. else {
  345. msg[0].len = 3;
  346. msgbuf0[1] = data->word & 0xff;
  347. msgbuf0[2] = data->word >> 8;
  348. }
  349. break;
  350. case I2C_SMBUS_PROC_CALL:
  351. nmsgs = 2; /* Special case */
  352. read_write = I2C_SMBUS_READ;
  353. msg[0].len = 3;
  354. msg[1].len = 2;
  355. msgbuf0[1] = data->word & 0xff;
  356. msgbuf0[2] = data->word >> 8;
  357. break;
  358. case I2C_SMBUS_BLOCK_DATA:
  359. if (read_write == I2C_SMBUS_READ) {
  360. msg[1].flags |= I2C_M_RECV_LEN;
  361. msg[1].len = 1; /* block length will be added by
  362. the underlying bus driver */
  363. i2c_smbus_try_get_dmabuf(&msg[1], 0);
  364. } else {
  365. msg[0].len = data->block[0] + 2;
  366. if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
  367. dev_err(&adapter->dev,
  368. "Invalid block write size %d\n",
  369. data->block[0]);
  370. return -EINVAL;
  371. }
  372. i2c_smbus_try_get_dmabuf(&msg[0], command);
  373. memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
  374. }
  375. break;
  376. case I2C_SMBUS_BLOCK_PROC_CALL:
  377. nmsgs = 2; /* Another special case */
  378. read_write = I2C_SMBUS_READ;
  379. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  380. dev_err(&adapter->dev,
  381. "Invalid block write size %d\n",
  382. data->block[0]);
  383. return -EINVAL;
  384. }
  385. msg[0].len = data->block[0] + 2;
  386. i2c_smbus_try_get_dmabuf(&msg[0], command);
  387. memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
  388. msg[1].flags |= I2C_M_RECV_LEN;
  389. msg[1].len = 1; /* block length will be added by
  390. the underlying bus driver */
  391. i2c_smbus_try_get_dmabuf(&msg[1], 0);
  392. break;
  393. case I2C_SMBUS_I2C_BLOCK_DATA:
  394. if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
  395. dev_err(&adapter->dev, "Invalid block %s size %d\n",
  396. str_read_write(read_write == I2C_SMBUS_READ),
  397. data->block[0]);
  398. return -EINVAL;
  399. }
  400. if (read_write == I2C_SMBUS_READ) {
  401. msg[1].len = data->block[0];
  402. i2c_smbus_try_get_dmabuf(&msg[1], 0);
  403. } else {
  404. msg[0].len = data->block[0] + 1;
  405. i2c_smbus_try_get_dmabuf(&msg[0], command);
  406. memcpy(msg[0].buf + 1, data->block + 1, data->block[0]);
  407. }
  408. break;
  409. default:
  410. dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
  411. return -EOPNOTSUPP;
  412. }
  413. if (wants_pec) {
  414. /* Compute PEC if first message is a write */
  415. if (!(msg[0].flags & I2C_M_RD)) {
  416. if (nmsgs == 1) /* Write only */
  417. i2c_smbus_add_pec(&msg[0]);
  418. else /* Write followed by read */
  419. partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
  420. }
  421. /* Ask for PEC if last message is a read */
  422. if (msg[nmsgs - 1].flags & I2C_M_RD)
  423. msg[nmsgs - 1].len++;
  424. }
  425. status = __i2c_transfer(adapter, msg, nmsgs);
  426. if (status < 0)
  427. goto cleanup;
  428. if (status != nmsgs) {
  429. status = -EIO;
  430. goto cleanup;
  431. }
  432. status = 0;
  433. /* Check PEC if last message is a read */
  434. if (wants_pec && (msg[nmsgs - 1].flags & I2C_M_RD)) {
  435. status = i2c_smbus_check_pec(partial_pec, &msg[nmsgs - 1]);
  436. if (status < 0)
  437. goto cleanup;
  438. }
  439. if (read_write == I2C_SMBUS_READ)
  440. switch (size) {
  441. case I2C_SMBUS_BYTE:
  442. data->byte = msgbuf0[0];
  443. break;
  444. case I2C_SMBUS_BYTE_DATA:
  445. data->byte = msgbuf1[0];
  446. break;
  447. case I2C_SMBUS_WORD_DATA:
  448. case I2C_SMBUS_PROC_CALL:
  449. data->word = msgbuf1[0] | (msgbuf1[1] << 8);
  450. break;
  451. case I2C_SMBUS_I2C_BLOCK_DATA:
  452. memcpy(data->block + 1, msg[1].buf, data->block[0]);
  453. break;
  454. case I2C_SMBUS_BLOCK_DATA:
  455. case I2C_SMBUS_BLOCK_PROC_CALL:
  456. if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
  457. dev_err(&adapter->dev,
  458. "Invalid block size returned: %d\n",
  459. msg[1].buf[0]);
  460. status = -EPROTO;
  461. goto cleanup;
  462. }
  463. memcpy(data->block, msg[1].buf, msg[1].buf[0] + 1);
  464. break;
  465. }
  466. cleanup:
  467. if (msg[0].flags & I2C_M_DMA_SAFE)
  468. kfree(msg[0].buf);
  469. if (msg[1].flags & I2C_M_DMA_SAFE)
  470. kfree(msg[1].buf);
  471. return status;
  472. }
  473. /**
  474. * i2c_smbus_xfer - execute SMBus protocol operations
  475. * @adapter: Handle to I2C bus
  476. * @addr: Address of SMBus slave on that bus
  477. * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
  478. * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
  479. * @command: Byte interpreted by slave, for protocols which use such bytes
  480. * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
  481. * @data: Data to be read or written
  482. *
  483. * This executes an SMBus protocol operation, and returns a negative
  484. * errno code else zero on success.
  485. */
  486. s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
  487. unsigned short flags, char read_write,
  488. u8 command, int protocol, union i2c_smbus_data *data)
  489. {
  490. s32 res;
  491. res = __i2c_lock_bus_helper(adapter);
  492. if (res)
  493. return res;
  494. res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
  495. command, protocol, data);
  496. i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
  497. return res;
  498. }
  499. EXPORT_SYMBOL(i2c_smbus_xfer);
  500. s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
  501. unsigned short flags, char read_write,
  502. u8 command, int protocol, union i2c_smbus_data *data)
  503. {
  504. int (*xfer_func)(struct i2c_adapter *adap, u16 addr,
  505. unsigned short flags, char read_write,
  506. u8 command, int size, union i2c_smbus_data *data);
  507. unsigned long orig_jiffies;
  508. int try;
  509. s32 res;
  510. res = __i2c_check_suspended(adapter);
  511. if (res)
  512. return res;
  513. /* If enabled, the following two tracepoints are conditional on
  514. * read_write and protocol.
  515. */
  516. trace_smbus_write(adapter, addr, flags, read_write,
  517. command, protocol, data);
  518. trace_smbus_read(adapter, addr, flags, read_write,
  519. command, protocol);
  520. flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
  521. xfer_func = adapter->algo->smbus_xfer;
  522. if (i2c_in_atomic_xfer_mode()) {
  523. if (adapter->algo->smbus_xfer_atomic)
  524. xfer_func = adapter->algo->smbus_xfer_atomic;
  525. else if (adapter->algo->master_xfer_atomic)
  526. xfer_func = NULL; /* fallback to I2C emulation */
  527. }
  528. if (xfer_func) {
  529. /* Retry automatically on arbitration loss */
  530. orig_jiffies = jiffies;
  531. for (res = 0, try = 0; try <= adapter->retries; try++) {
  532. res = xfer_func(adapter, addr, flags, read_write,
  533. command, protocol, data);
  534. if (res != -EAGAIN)
  535. break;
  536. if (time_after(jiffies,
  537. orig_jiffies + adapter->timeout))
  538. break;
  539. }
  540. if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
  541. goto trace;
  542. /*
  543. * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
  544. * implement native support for the SMBus operation.
  545. */
  546. }
  547. res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
  548. command, protocol, data);
  549. trace:
  550. /* If enabled, the reply tracepoint is conditional on read_write. */
  551. trace_smbus_reply(adapter, addr, flags, read_write,
  552. command, protocol, data, res);
  553. trace_smbus_result(adapter, addr, flags, read_write,
  554. command, protocol, res);
  555. return res;
  556. }
  557. EXPORT_SYMBOL(__i2c_smbus_xfer);
  558. /**
  559. * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
  560. * @client: Handle to slave device
  561. * @command: Byte interpreted by slave
  562. * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
  563. * @values: Byte array into which data will be read; big enough to hold
  564. * the data returned by the slave. SMBus allows at most
  565. * I2C_SMBUS_BLOCK_MAX bytes.
  566. *
  567. * This executes the SMBus "block read" protocol if supported by the adapter.
  568. * If block read is not supported, it emulates it using either word or byte
  569. * read protocols depending on availability.
  570. *
  571. * The addresses of the I2C slave device that are accessed with this function
  572. * must be mapped to a linear region, so that a block read will have the same
  573. * effect as a byte read. Before using this function you must double-check
  574. * if the I2C slave does support exchanging a block transfer with a byte
  575. * transfer.
  576. */
  577. s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
  578. u8 command, u8 length, u8 *values)
  579. {
  580. u8 i = 0;
  581. int status;
  582. if (length > I2C_SMBUS_BLOCK_MAX)
  583. length = I2C_SMBUS_BLOCK_MAX;
  584. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
  585. return i2c_smbus_read_i2c_block_data(client, command, length, values);
  586. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
  587. return -EOPNOTSUPP;
  588. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  589. while ((i + 2) <= length) {
  590. status = i2c_smbus_read_word_data(client, command + i);
  591. if (status < 0)
  592. return status;
  593. values[i] = status & 0xff;
  594. values[i + 1] = status >> 8;
  595. i += 2;
  596. }
  597. }
  598. while (i < length) {
  599. status = i2c_smbus_read_byte_data(client, command + i);
  600. if (status < 0)
  601. return status;
  602. values[i] = status;
  603. i++;
  604. }
  605. return i;
  606. }
  607. EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
  608. /**
  609. * i2c_new_smbus_alert_device - get ara client for SMBus alert support
  610. * @adapter: the target adapter
  611. * @setup: setup data for the SMBus alert handler
  612. * Context: can sleep
  613. *
  614. * Setup handling of the SMBus alert protocol on a given I2C bus segment.
  615. *
  616. * Handling can be done either through our IRQ handler, or by the
  617. * adapter (from its handler, periodic polling, or whatever).
  618. *
  619. * This returns the ara client, which should be saved for later use with
  620. * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or an
  621. * ERRPTR to indicate an error.
  622. */
  623. struct i2c_client *i2c_new_smbus_alert_device(struct i2c_adapter *adapter,
  624. struct i2c_smbus_alert_setup *setup)
  625. {
  626. struct i2c_board_info ara_board_info = {
  627. I2C_BOARD_INFO("smbus_alert", 0x0c),
  628. .platform_data = setup,
  629. };
  630. return i2c_new_client_device(adapter, &ara_board_info);
  631. }
  632. EXPORT_SYMBOL_GPL(i2c_new_smbus_alert_device);
  633. #if IS_ENABLED(CONFIG_I2C_SMBUS)
  634. int i2c_setup_smbus_alert(struct i2c_adapter *adapter)
  635. {
  636. struct device *parent = adapter->dev.parent;
  637. int irq;
  638. /* Adapter instantiated without parent, skip the SMBus alert setup */
  639. if (!parent)
  640. return 0;
  641. /* Report serious errors */
  642. irq = device_property_match_string(parent, "interrupt-names", "smbus_alert");
  643. if (irq < 0 && irq != -EINVAL && irq != -ENODATA)
  644. return irq;
  645. /* Skip setup when no irq was found */
  646. if (irq < 0 && !device_property_present(parent, "smbalert-gpios"))
  647. return 0;
  648. return PTR_ERR_OR_ZERO(i2c_new_smbus_alert_device(adapter, NULL));
  649. }
  650. #endif