1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux I2C core SMBus and SMBus emulation code
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.
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>
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>
22 #define CREATE_TRACE_POINTS
23 #include <trace/events/smbus.h>
28 #define POLY (0x1070U << 3)
29 static u8
crc8(u16 data
)
33 for (i
= 0; i
< 8; i
++) {
38 return (u8
)(data
>> 8);
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.
47 * Incremental CRC8 over count bytes in the array pointed to by p
49 u8
i2c_smbus_pec(u8 crc
, u8
*p
, size_t count
)
53 for (i
= 0; i
< count
; i
++)
54 crc
= crc8((crc
^ p
[i
]) << 8);
57 EXPORT_SYMBOL(i2c_smbus_pec
);
59 /* Assume a 7-bit address, which is reasonable for SMBus */
60 static u8
i2c_smbus_msg_pec(u8 pec
, struct i2c_msg
*msg
)
62 /* The address will be sent first */
63 u8 addr
= i2c_8bit_addr_from_msg(msg
);
64 pec
= i2c_smbus_pec(pec
, &addr
, 1);
66 /* The data buffer follows */
67 return i2c_smbus_pec(pec
, msg
->buf
, msg
->len
);
70 /* Used for write only transactions */
71 static inline void i2c_smbus_add_pec(struct i2c_msg
*msg
)
73 msg
->buf
[msg
->len
] = i2c_smbus_msg_pec(0, msg
);
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). */
82 static int i2c_smbus_check_pec(u8 cpec
, struct i2c_msg
*msg
)
84 u8 rpec
= msg
->buf
[--msg
->len
];
85 cpec
= i2c_smbus_msg_pec(cpec
, msg
);
88 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
96 * i2c_smbus_read_byte - SMBus "receive byte" protocol
97 * @client: Handle to slave device
99 * This executes the SMBus "receive byte" protocol, returning negative errno
100 * else the byte received from the device.
102 s32
i2c_smbus_read_byte(const struct i2c_client
*client
)
104 union i2c_smbus_data data
;
107 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
109 I2C_SMBUS_BYTE
, &data
);
110 return (status
< 0) ? status
: data
.byte
;
112 EXPORT_SYMBOL(i2c_smbus_read_byte
);
115 * i2c_smbus_write_byte - SMBus "send byte" protocol
116 * @client: Handle to slave device
117 * @value: Byte to be sent
119 * This executes the SMBus "send byte" protocol, returning negative errno
120 * else zero on success.
122 s32
i2c_smbus_write_byte(const struct i2c_client
*client
, u8 value
)
124 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
125 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
127 EXPORT_SYMBOL(i2c_smbus_write_byte
);
130 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
131 * @client: Handle to slave device
132 * @command: Byte interpreted by slave
134 * This executes the SMBus "read byte" protocol, returning negative errno
135 * else a data byte received from the device.
137 s32
i2c_smbus_read_byte_data(const struct i2c_client
*client
, u8 command
)
139 union i2c_smbus_data data
;
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
;
147 EXPORT_SYMBOL(i2c_smbus_read_byte_data
);
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
155 * This executes the SMBus "write byte" protocol, returning negative errno
156 * else zero on success.
158 s32
i2c_smbus_write_byte_data(const struct i2c_client
*client
, u8 command
,
161 union i2c_smbus_data data
;
163 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
164 I2C_SMBUS_WRITE
, command
,
165 I2C_SMBUS_BYTE_DATA
, &data
);
167 EXPORT_SYMBOL(i2c_smbus_write_byte_data
);
170 * i2c_smbus_read_word_data - SMBus "read word" protocol
171 * @client: Handle to slave device
172 * @command: Byte interpreted by slave
174 * This executes the SMBus "read word" protocol, returning negative errno
175 * else a 16-bit unsigned "word" received from the device.
177 s32
i2c_smbus_read_word_data(const struct i2c_client
*client
, u8 command
)
179 union i2c_smbus_data data
;
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
;
187 EXPORT_SYMBOL(i2c_smbus_read_word_data
);
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
195 * This executes the SMBus "write word" protocol, returning negative errno
196 * else zero on success.
198 s32
i2c_smbus_write_word_data(const struct i2c_client
*client
, u8 command
,
201 union i2c_smbus_data data
;
203 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
204 I2C_SMBUS_WRITE
, command
,
205 I2C_SMBUS_WORD_DATA
, &data
);
207 EXPORT_SYMBOL(i2c_smbus_write_word_data
);
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.
216 * This executes the SMBus "block read" protocol, returning negative errno
217 * else the number of data bytes in the slave's response.
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.
224 s32
i2c_smbus_read_block_data(const struct i2c_client
*client
, u8 command
,
227 union i2c_smbus_data data
;
230 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
231 I2C_SMBUS_READ
, command
,
232 I2C_SMBUS_BLOCK_DATA
, &data
);
236 memcpy(values
, &data
.block
[1], data
.block
[0]);
237 return data
.block
[0];
239 EXPORT_SYMBOL(i2c_smbus_read_block_data
);
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.
248 * This executes the SMBus "block write" protocol, returning negative errno
249 * else zero on success.
251 s32
i2c_smbus_write_block_data(const struct i2c_client
*client
, u8 command
,
252 u8 length
, const u8
*values
)
254 union i2c_smbus_data data
;
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
);
264 EXPORT_SYMBOL(i2c_smbus_write_block_data
);
266 /* Returns the number of read bytes */
267 s32
i2c_smbus_read_i2c_block_data(const struct i2c_client
*client
, u8 command
,
268 u8 length
, u8
*values
)
270 union i2c_smbus_data data
;
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
);
282 memcpy(values
, &data
.block
[1], data
.block
[0]);
283 return data
.block
[0];
285 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data
);
287 s32
i2c_smbus_write_i2c_block_data(const struct i2c_client
*client
, u8 command
,
288 u8 length
, const u8
*values
)
290 union i2c_smbus_data data
;
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
);
300 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data
);
302 static void i2c_smbus_try_get_dmabuf(struct i2c_msg
*msg
, u8 init_val
)
304 bool is_read
= msg
->flags
& I2C_M_RD
;
305 unsigned char *dma_buf
;
307 dma_buf
= kzalloc(I2C_SMBUS_BLOCK_MAX
+ (is_read
? 2 : 3), GFP_KERNEL
);
312 msg
->flags
|= I2C_M_DMA_SAFE
;
315 msg
->buf
[0] = init_val
;
319 * Simulate a SMBus command using the I2C protocol.
320 * No checking of parameters is done!
322 static 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
)
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
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;
338 struct i2c_msg msg
[2] = {
346 .flags
= flags
| I2C_M_RD
,
351 bool wants_pec
= ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
352 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
);
354 msgbuf0
[0] = command
;
356 case I2C_SMBUS_QUICK
:
358 /* Special case: The read/write field is used as data */
359 msg
[0].flags
= flags
| (read_write
== I2C_SMBUS_READ
?
364 if (read_write
== I2C_SMBUS_READ
) {
365 /* Special case: only a read! */
366 msg
[0].flags
= I2C_M_RD
| flags
;
370 case I2C_SMBUS_BYTE_DATA
:
371 if (read_write
== I2C_SMBUS_READ
)
375 msgbuf0
[1] = data
->byte
;
378 case I2C_SMBUS_WORD_DATA
:
379 if (read_write
== I2C_SMBUS_READ
)
383 msgbuf0
[1] = data
->word
& 0xff;
384 msgbuf0
[2] = data
->word
>> 8;
387 case I2C_SMBUS_PROC_CALL
:
388 nmsgs
= 2; /* Special case */
389 read_write
= I2C_SMBUS_READ
;
392 msgbuf0
[1] = data
->word
& 0xff;
393 msgbuf0
[2] = data
->word
>> 8;
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);
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",
410 i2c_smbus_try_get_dmabuf(&msg
[0], command
);
411 memcpy(msg
[0].buf
+ 1, data
->block
, msg
[0].len
- 1);
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",
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);
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);
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",
441 if (read_write
== I2C_SMBUS_READ
) {
442 msg
[1].len
= data
->block
[0];
443 i2c_smbus_try_get_dmabuf(&msg
[1], 0);
445 msg
[0].len
= data
->block
[0] + 1;
447 i2c_smbus_try_get_dmabuf(&msg
[0], command
);
448 memcpy(msg
[0].buf
+ 1, data
->block
+ 1, data
->block
[0]);
452 dev_err(&adapter
->dev
, "Unsupported transaction %d\n", size
);
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]);
464 /* Ask for PEC if last message is a read */
465 if (msg
[nmsgs
- 1].flags
& I2C_M_RD
)
466 msg
[nmsgs
- 1].len
++;
469 status
= __i2c_transfer(adapter
, msg
, nmsgs
);
472 if (status
!= nmsgs
) {
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]);
485 if (read_write
== I2C_SMBUS_READ
)
488 data
->byte
= msgbuf0
[0];
490 case I2C_SMBUS_BYTE_DATA
:
491 data
->byte
= msgbuf1
[0];
493 case I2C_SMBUS_WORD_DATA
:
494 case I2C_SMBUS_PROC_CALL
:
495 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
497 case I2C_SMBUS_I2C_BLOCK_DATA
:
498 memcpy(data
->block
+ 1, msg
[1].buf
, data
->block
[0]);
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",
509 memcpy(data
->block
, msg
[1].buf
, msg
[1].buf
[0] + 1);
514 if (msg
[0].flags
& I2C_M_DMA_SAFE
)
516 if (msg
[1].flags
& I2C_M_DMA_SAFE
)
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
532 * This executes an SMBus protocol operation, and returns a negative
533 * errno code else zero on success.
535 s32
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
)
541 res
= __i2c_lock_bus_helper(adapter
);
545 res
= __i2c_smbus_xfer(adapter
, addr
, flags
, read_write
,
546 command
, protocol
, data
);
547 i2c_unlock_bus(adapter
, I2C_LOCK_SEGMENT
);
551 EXPORT_SYMBOL(i2c_smbus_xfer
);
553 s32
__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
)
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
;
564 res
= __i2c_check_suspended(adapter
);
568 /* If enabled, the following two tracepoints are conditional on
569 * read_write and protocol.
571 trace_smbus_write(adapter
, addr
, flags
, read_write
,
572 command
, protocol
, data
);
573 trace_smbus_read(adapter
, addr
, flags
, read_write
,
576 flags
&= I2C_M_TEN
| I2C_CLIENT_PEC
| I2C_CLIENT_SCCB
;
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 */
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
);
594 if (time_after(jiffies
,
595 orig_jiffies
+ adapter
->timeout
))
599 if (res
!= -EOPNOTSUPP
|| !adapter
->algo
->master_xfer
)
602 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
603 * implement native support for the SMBus operation.
607 res
= i2c_smbus_xfer_emulated(adapter
, addr
, flags
, read_write
,
608 command
, protocol
, data
);
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
);
619 EXPORT_SYMBOL(__i2c_smbus_xfer
);
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.
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.
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
640 s32
i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client
*client
,
641 u8 command
, u8 length
, u8
*values
)
646 if (length
> I2C_SMBUS_BLOCK_MAX
)
647 length
= I2C_SMBUS_BLOCK_MAX
;
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
);
652 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_READ_BYTE_DATA
))
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
);
660 values
[i
] = status
& 0xff;
661 values
[i
+ 1] = status
>> 8;
667 status
= i2c_smbus_read_byte_data(client
, command
+ i
);
676 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated
);
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
684 * Setup handling of the SMBus alert protocol on a given I2C bus segment.
686 * Handling can be done either through our IRQ handler, or by the
687 * adapter (from its handler, periodic polling, or whatever).
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.
693 struct i2c_client
*i2c_new_smbus_alert_device(struct i2c_adapter
*adapter
,
694 struct i2c_smbus_alert_setup
*setup
)
696 struct i2c_board_info ara_board_info
= {
697 I2C_BOARD_INFO("smbus_alert", 0x0c),
698 .platform_data
= setup
,
701 return i2c_new_client_device(adapter
, &ara_board_info
);
703 EXPORT_SYMBOL_GPL(i2c_new_smbus_alert_device
);
705 #if IS_ENABLED(CONFIG_I2C_SMBUS)
706 int i2c_setup_smbus_alert(struct i2c_adapter
*adapter
)
708 struct device
*parent
= adapter
->dev
.parent
;
711 /* Adapter instantiated without parent, skip the SMBus alert setup */
715 /* Report serious errors */
716 irq
= device_property_match_string(parent
, "interrupt-names", "smbus_alert");
717 if (irq
< 0 && irq
!= -EINVAL
&& irq
!= -ENODATA
)
720 /* Skip setup when no irq was found */
721 if (irq
< 0 && !device_property_present(parent
, "smbalert-gpios"))
724 return PTR_ERR_OR_ZERO(i2c_new_smbus_alert_device(adapter
, NULL
));