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