i2c-core-smbus.c 20 KB

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