i2c-core-smbus.c 20 KB

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