2 * Linux I2C core SMBus and SMBus emulation code
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.
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>
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)
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/i2c.h>
20 #include <linux/i2c-smbus.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);
41 /* Incremental CRC8 over count bytes in the array pointed to by p */
42 static u8
i2c_smbus_pec(u8 crc
, u8
*p
, size_t count
)
46 for (i
= 0; i
< count
; i
++)
47 crc
= crc8((crc
^ p
[i
]) << 8);
51 /* Assume a 7-bit address, which is reasonable for SMBus */
52 static u8
i2c_smbus_msg_pec(u8 pec
, struct i2c_msg
*msg
)
54 /* The address will be sent first */
55 u8 addr
= i2c_8bit_addr_from_msg(msg
);
56 pec
= i2c_smbus_pec(pec
, &addr
, 1);
58 /* The data buffer follows */
59 return i2c_smbus_pec(pec
, msg
->buf
, msg
->len
);
62 /* Used for write only transactions */
63 static inline void i2c_smbus_add_pec(struct i2c_msg
*msg
)
65 msg
->buf
[msg
->len
] = i2c_smbus_msg_pec(0, msg
);
69 /* Return <0 on CRC error
70 If there was a write before this read (most cases) we need to take the
71 partial CRC from the write part into account.
72 Note that this function does modify the message (we need to decrease the
73 message length to hide the CRC byte from the caller). */
74 static int i2c_smbus_check_pec(u8 cpec
, struct i2c_msg
*msg
)
76 u8 rpec
= msg
->buf
[--msg
->len
];
77 cpec
= i2c_smbus_msg_pec(cpec
, msg
);
80 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
88 * i2c_smbus_read_byte - SMBus "receive byte" protocol
89 * @client: Handle to slave device
91 * This executes the SMBus "receive byte" protocol, returning negative errno
92 * else the byte received from the device.
94 s32
i2c_smbus_read_byte(const struct i2c_client
*client
)
96 union i2c_smbus_data data
;
99 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
101 I2C_SMBUS_BYTE
, &data
);
102 return (status
< 0) ? status
: data
.byte
;
104 EXPORT_SYMBOL(i2c_smbus_read_byte
);
107 * i2c_smbus_write_byte - SMBus "send byte" protocol
108 * @client: Handle to slave device
109 * @value: Byte to be sent
111 * This executes the SMBus "send byte" protocol, returning negative errno
112 * else zero on success.
114 s32
i2c_smbus_write_byte(const struct i2c_client
*client
, u8 value
)
116 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
117 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
119 EXPORT_SYMBOL(i2c_smbus_write_byte
);
122 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
123 * @client: Handle to slave device
124 * @command: Byte interpreted by slave
126 * This executes the SMBus "read byte" protocol, returning negative errno
127 * else a data byte received from the device.
129 s32
i2c_smbus_read_byte_data(const struct i2c_client
*client
, u8 command
)
131 union i2c_smbus_data data
;
134 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
135 I2C_SMBUS_READ
, command
,
136 I2C_SMBUS_BYTE_DATA
, &data
);
137 return (status
< 0) ? status
: data
.byte
;
139 EXPORT_SYMBOL(i2c_smbus_read_byte_data
);
142 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
143 * @client: Handle to slave device
144 * @command: Byte interpreted by slave
145 * @value: Byte being written
147 * This executes the SMBus "write byte" protocol, returning negative errno
148 * else zero on success.
150 s32
i2c_smbus_write_byte_data(const struct i2c_client
*client
, u8 command
,
153 union i2c_smbus_data data
;
155 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
156 I2C_SMBUS_WRITE
, command
,
157 I2C_SMBUS_BYTE_DATA
, &data
);
159 EXPORT_SYMBOL(i2c_smbus_write_byte_data
);
162 * i2c_smbus_read_word_data - SMBus "read word" protocol
163 * @client: Handle to slave device
164 * @command: Byte interpreted by slave
166 * This executes the SMBus "read word" protocol, returning negative errno
167 * else a 16-bit unsigned "word" received from the device.
169 s32
i2c_smbus_read_word_data(const struct i2c_client
*client
, u8 command
)
171 union i2c_smbus_data data
;
174 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
175 I2C_SMBUS_READ
, command
,
176 I2C_SMBUS_WORD_DATA
, &data
);
177 return (status
< 0) ? status
: data
.word
;
179 EXPORT_SYMBOL(i2c_smbus_read_word_data
);
182 * i2c_smbus_write_word_data - SMBus "write word" protocol
183 * @client: Handle to slave device
184 * @command: Byte interpreted by slave
185 * @value: 16-bit "word" being written
187 * This executes the SMBus "write word" protocol, returning negative errno
188 * else zero on success.
190 s32
i2c_smbus_write_word_data(const struct i2c_client
*client
, u8 command
,
193 union i2c_smbus_data data
;
195 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
196 I2C_SMBUS_WRITE
, command
,
197 I2C_SMBUS_WORD_DATA
, &data
);
199 EXPORT_SYMBOL(i2c_smbus_write_word_data
);
202 * i2c_smbus_read_block_data - SMBus "block read" protocol
203 * @client: Handle to slave device
204 * @command: Byte interpreted by slave
205 * @values: Byte array into which data will be read; big enough to hold
206 * the data returned by the slave. SMBus allows at most 32 bytes.
208 * This executes the SMBus "block read" protocol, returning negative errno
209 * else the number of data bytes in the slave's response.
211 * Note that using this function requires that the client's adapter support
212 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
213 * support this; its emulation through I2C messaging relies on a specific
214 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
216 s32
i2c_smbus_read_block_data(const struct i2c_client
*client
, u8 command
,
219 union i2c_smbus_data data
;
222 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
223 I2C_SMBUS_READ
, command
,
224 I2C_SMBUS_BLOCK_DATA
, &data
);
228 memcpy(values
, &data
.block
[1], data
.block
[0]);
229 return data
.block
[0];
231 EXPORT_SYMBOL(i2c_smbus_read_block_data
);
234 * i2c_smbus_write_block_data - SMBus "block write" protocol
235 * @client: Handle to slave device
236 * @command: Byte interpreted by slave
237 * @length: Size of data block; SMBus allows at most 32 bytes
238 * @values: Byte array which will be written.
240 * This executes the SMBus "block write" protocol, returning negative errno
241 * else zero on success.
243 s32
i2c_smbus_write_block_data(const struct i2c_client
*client
, u8 command
,
244 u8 length
, const u8
*values
)
246 union i2c_smbus_data data
;
248 if (length
> I2C_SMBUS_BLOCK_MAX
)
249 length
= I2C_SMBUS_BLOCK_MAX
;
250 data
.block
[0] = length
;
251 memcpy(&data
.block
[1], values
, length
);
252 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
253 I2C_SMBUS_WRITE
, command
,
254 I2C_SMBUS_BLOCK_DATA
, &data
);
256 EXPORT_SYMBOL(i2c_smbus_write_block_data
);
258 /* Returns the number of read bytes */
259 s32
i2c_smbus_read_i2c_block_data(const struct i2c_client
*client
, u8 command
,
260 u8 length
, u8
*values
)
262 union i2c_smbus_data data
;
265 if (length
> I2C_SMBUS_BLOCK_MAX
)
266 length
= I2C_SMBUS_BLOCK_MAX
;
267 data
.block
[0] = length
;
268 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
269 I2C_SMBUS_READ
, command
,
270 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
274 memcpy(values
, &data
.block
[1], data
.block
[0]);
275 return data
.block
[0];
277 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data
);
279 s32
i2c_smbus_write_i2c_block_data(const struct i2c_client
*client
, u8 command
,
280 u8 length
, const u8
*values
)
282 union i2c_smbus_data data
;
284 if (length
> I2C_SMBUS_BLOCK_MAX
)
285 length
= I2C_SMBUS_BLOCK_MAX
;
286 data
.block
[0] = length
;
287 memcpy(data
.block
+ 1, values
, length
);
288 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
289 I2C_SMBUS_WRITE
, command
,
290 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
292 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data
);
294 /* Simulate a SMBus command using the i2c protocol
295 No checking of parameters is done! */
296 static s32
i2c_smbus_xfer_emulated(struct i2c_adapter
*adapter
, u16 addr
,
297 unsigned short flags
,
298 char read_write
, u8 command
, int size
,
299 union i2c_smbus_data
*data
)
301 /* So we need to generate a series of msgs. In the case of writing, we
302 need to use only one message; when reading, we need two. We initialize
303 most things with sane defaults, to keep the code below somewhat
305 unsigned char msgbuf0
[I2C_SMBUS_BLOCK_MAX
+3];
306 unsigned char msgbuf1
[I2C_SMBUS_BLOCK_MAX
+2];
307 int num
= read_write
== I2C_SMBUS_READ
? 2 : 1;
311 struct i2c_msg msg
[2] = {
319 .flags
= flags
| I2C_M_RD
,
325 msgbuf0
[0] = command
;
327 case I2C_SMBUS_QUICK
:
329 /* Special case: The read/write field is used as data */
330 msg
[0].flags
= flags
| (read_write
== I2C_SMBUS_READ
?
335 if (read_write
== I2C_SMBUS_READ
) {
336 /* Special case: only a read! */
337 msg
[0].flags
= I2C_M_RD
| flags
;
341 case I2C_SMBUS_BYTE_DATA
:
342 if (read_write
== I2C_SMBUS_READ
)
346 msgbuf0
[1] = data
->byte
;
349 case I2C_SMBUS_WORD_DATA
:
350 if (read_write
== I2C_SMBUS_READ
)
354 msgbuf0
[1] = data
->word
& 0xff;
355 msgbuf0
[2] = data
->word
>> 8;
358 case I2C_SMBUS_PROC_CALL
:
359 num
= 2; /* Special case */
360 read_write
= I2C_SMBUS_READ
;
363 msgbuf0
[1] = data
->word
& 0xff;
364 msgbuf0
[2] = data
->word
>> 8;
366 case I2C_SMBUS_BLOCK_DATA
:
367 if (read_write
== I2C_SMBUS_READ
) {
368 msg
[1].flags
|= I2C_M_RECV_LEN
;
369 msg
[1].len
= 1; /* block length will be added by
370 the underlying bus driver */
372 msg
[0].len
= data
->block
[0] + 2;
373 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 2) {
374 dev_err(&adapter
->dev
,
375 "Invalid block write size %d\n",
379 for (i
= 1; i
< msg
[0].len
; i
++)
380 msgbuf0
[i
] = data
->block
[i
-1];
383 case I2C_SMBUS_BLOCK_PROC_CALL
:
384 num
= 2; /* Another special case */
385 read_write
= I2C_SMBUS_READ
;
386 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
) {
387 dev_err(&adapter
->dev
,
388 "Invalid block write size %d\n",
392 msg
[0].len
= data
->block
[0] + 2;
393 for (i
= 1; i
< msg
[0].len
; i
++)
394 msgbuf0
[i
] = data
->block
[i
-1];
395 msg
[1].flags
|= I2C_M_RECV_LEN
;
396 msg
[1].len
= 1; /* block length will be added by
397 the underlying bus driver */
399 case I2C_SMBUS_I2C_BLOCK_DATA
:
400 if (read_write
== I2C_SMBUS_READ
) {
401 msg
[1].len
= data
->block
[0];
403 msg
[0].len
= data
->block
[0] + 1;
404 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 1) {
405 dev_err(&adapter
->dev
,
406 "Invalid block write size %d\n",
410 for (i
= 1; i
<= data
->block
[0]; i
++)
411 msgbuf0
[i
] = data
->block
[i
];
415 dev_err(&adapter
->dev
, "Unsupported transaction %d\n", size
);
419 i
= ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
420 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
);
422 /* Compute PEC if first message is a write */
423 if (!(msg
[0].flags
& I2C_M_RD
)) {
424 if (num
== 1) /* Write only */
425 i2c_smbus_add_pec(&msg
[0]);
426 else /* Write followed by read */
427 partial_pec
= i2c_smbus_msg_pec(0, &msg
[0]);
429 /* Ask for PEC if last message is a read */
430 if (msg
[num
-1].flags
& I2C_M_RD
)
434 status
= i2c_transfer(adapter
, msg
, num
);
438 /* Check PEC if last message is a read */
439 if (i
&& (msg
[num
-1].flags
& I2C_M_RD
)) {
440 status
= i2c_smbus_check_pec(partial_pec
, &msg
[num
-1]);
445 if (read_write
== I2C_SMBUS_READ
)
448 data
->byte
= msgbuf0
[0];
450 case I2C_SMBUS_BYTE_DATA
:
451 data
->byte
= msgbuf1
[0];
453 case I2C_SMBUS_WORD_DATA
:
454 case I2C_SMBUS_PROC_CALL
:
455 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
457 case I2C_SMBUS_I2C_BLOCK_DATA
:
458 for (i
= 0; i
< data
->block
[0]; i
++)
459 data
->block
[i
+1] = msgbuf1
[i
];
461 case I2C_SMBUS_BLOCK_DATA
:
462 case I2C_SMBUS_BLOCK_PROC_CALL
:
463 for (i
= 0; i
< msgbuf1
[0] + 1; i
++)
464 data
->block
[i
] = msgbuf1
[i
];
471 * i2c_smbus_xfer - execute SMBus protocol operations
472 * @adapter: Handle to I2C bus
473 * @addr: Address of SMBus slave on that bus
474 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
475 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
476 * @command: Byte interpreted by slave, for protocols which use such bytes
477 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
478 * @data: Data to be read or written
480 * This executes an SMBus protocol operation, and returns a negative
481 * errno code else zero on success.
483 s32
i2c_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
, unsigned short flags
,
484 char read_write
, u8 command
, int protocol
,
485 union i2c_smbus_data
*data
)
487 unsigned long orig_jiffies
;
491 /* If enabled, the following two tracepoints are conditional on
492 * read_write and protocol.
494 trace_smbus_write(adapter
, addr
, flags
, read_write
,
495 command
, protocol
, data
);
496 trace_smbus_read(adapter
, addr
, flags
, read_write
,
499 flags
&= I2C_M_TEN
| I2C_CLIENT_PEC
| I2C_CLIENT_SCCB
;
501 if (adapter
->algo
->smbus_xfer
) {
502 i2c_lock_bus(adapter
, I2C_LOCK_SEGMENT
);
504 /* Retry automatically on arbitration loss */
505 orig_jiffies
= jiffies
;
506 for (res
= 0, try = 0; try <= adapter
->retries
; try++) {
507 res
= adapter
->algo
->smbus_xfer(adapter
, addr
, flags
,
512 if (time_after(jiffies
,
513 orig_jiffies
+ adapter
->timeout
))
516 i2c_unlock_bus(adapter
, I2C_LOCK_SEGMENT
);
518 if (res
!= -EOPNOTSUPP
|| !adapter
->algo
->master_xfer
)
521 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
522 * implement native support for the SMBus operation.
526 res
= i2c_smbus_xfer_emulated(adapter
, addr
, flags
, read_write
,
527 command
, protocol
, data
);
530 /* If enabled, the reply tracepoint is conditional on read_write. */
531 trace_smbus_reply(adapter
, addr
, flags
, read_write
,
532 command
, protocol
, data
);
533 trace_smbus_result(adapter
, addr
, flags
, read_write
,
534 command
, protocol
, res
);
538 EXPORT_SYMBOL(i2c_smbus_xfer
);
541 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
542 * @client: Handle to slave device
543 * @command: Byte interpreted by slave
544 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
545 * @values: Byte array into which data will be read; big enough to hold
546 * the data returned by the slave. SMBus allows at most
547 * I2C_SMBUS_BLOCK_MAX bytes.
549 * This executes the SMBus "block read" protocol if supported by the adapter.
550 * If block read is not supported, it emulates it using either word or byte
551 * read protocols depending on availability.
553 * The addresses of the I2C slave device that are accessed with this function
554 * must be mapped to a linear region, so that a block read will have the same
555 * effect as a byte read. Before using this function you must double-check
556 * if the I2C slave does support exchanging a block transfer with a byte
559 s32
i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client
*client
,
560 u8 command
, u8 length
, u8
*values
)
565 if (length
> I2C_SMBUS_BLOCK_MAX
)
566 length
= I2C_SMBUS_BLOCK_MAX
;
568 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_READ_I2C_BLOCK
))
569 return i2c_smbus_read_i2c_block_data(client
, command
, length
, values
);
571 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_READ_BYTE_DATA
))
574 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_READ_WORD_DATA
)) {
575 while ((i
+ 2) <= length
) {
576 status
= i2c_smbus_read_word_data(client
, command
+ i
);
579 values
[i
] = status
& 0xff;
580 values
[i
+ 1] = status
>> 8;
586 status
= i2c_smbus_read_byte_data(client
, command
+ i
);
595 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated
);
598 * i2c_setup_smbus_alert - Setup SMBus alert support
599 * @adapter: the target adapter
600 * @setup: setup data for the SMBus alert handler
603 * Setup handling of the SMBus alert protocol on a given I2C bus segment.
605 * Handling can be done either through our IRQ handler, or by the
606 * adapter (from its handler, periodic polling, or whatever).
608 * NOTE that if we manage the IRQ, we *MUST* know if it's level or
609 * edge triggered in order to hand it to the workqueue correctly.
610 * If triggering the alert seems to wedge the system, you probably
611 * should have said it's level triggered.
613 * This returns the ara client, which should be saved for later use with
614 * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or NULL
615 * to indicate an error.
617 struct i2c_client
*i2c_setup_smbus_alert(struct i2c_adapter
*adapter
,
618 struct i2c_smbus_alert_setup
*setup
)
620 struct i2c_board_info ara_board_info
= {
621 I2C_BOARD_INFO("smbus_alert", 0x0c),
622 .platform_data
= setup
,
625 return i2c_new_device(adapter
, &ara_board_info
);
627 EXPORT_SYMBOL_GPL(i2c_setup_smbus_alert
);
629 #if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_OF)
630 int of_i2c_setup_smbus_alert(struct i2c_adapter
*adapter
)
632 struct i2c_client
*client
;
635 irq
= of_property_match_string(adapter
->dev
.of_node
, "interrupt-names",
637 if (irq
== -EINVAL
|| irq
== -ENODATA
)
642 client
= i2c_setup_smbus_alert(adapter
, NULL
);
648 EXPORT_SYMBOL_GPL(of_i2c_setup_smbus_alert
);