i2c-core-smbus.c 19 KB

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