i2c-core-smbus.c 20 KB

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