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/slab.h>
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/smbus.h>
27 #define POLY (0x1070U << 3)
28 static u8 crc8(u16 data)
32 for (i = 0; i < 8; i++) {
37 return (u8)(data >> 8);
40 /* Incremental CRC8 over count bytes in the array pointed to by p */
41 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
45 for (i = 0; i < count; i++)
46 crc = crc8((crc ^ p[i]) << 8);
50 /* Assume a 7-bit address, which is reasonable for SMBus */
51 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
53 /* The address will be sent first */
54 u8 addr = i2c_8bit_addr_from_msg(msg);
55 pec = i2c_smbus_pec(pec, &addr, 1);
57 /* The data buffer follows */
58 return i2c_smbus_pec(pec, msg->buf, msg->len);
61 /* Used for write only transactions */
62 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
64 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
68 /* Return <0 on CRC error
69 If there was a write before this read (most cases) we need to take the
70 partial CRC from the write part into account.
71 Note that this function does modify the message (we need to decrease the
72 message length to hide the CRC byte from the caller). */
73 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
75 u8 rpec = msg->buf[--msg->len];
76 cpec = i2c_smbus_msg_pec(cpec, msg);
79 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
87 * i2c_smbus_read_byte - SMBus "receive byte" protocol
88 * @client: Handle to slave device
90 * This executes the SMBus "receive byte" protocol, returning negative errno
91 * else the byte received from the device.
93 s32 i2c_smbus_read_byte(const struct i2c_client *client)
95 union i2c_smbus_data data;
98 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
100 I2C_SMBUS_BYTE, &data);
101 return (status < 0) ? status : data.byte;
103 EXPORT_SYMBOL(i2c_smbus_read_byte);
106 * i2c_smbus_write_byte - SMBus "send byte" protocol
107 * @client: Handle to slave device
108 * @value: Byte to be sent
110 * This executes the SMBus "send byte" protocol, returning negative errno
111 * else zero on success.
113 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
115 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
116 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
118 EXPORT_SYMBOL(i2c_smbus_write_byte);
121 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
122 * @client: Handle to slave device
123 * @command: Byte interpreted by slave
125 * This executes the SMBus "read byte" protocol, returning negative errno
126 * else a data byte received from the device.
128 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
130 union i2c_smbus_data data;
133 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
134 I2C_SMBUS_READ, command,
135 I2C_SMBUS_BYTE_DATA, &data);
136 return (status < 0) ? status : data.byte;
138 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
141 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
142 * @client: Handle to slave device
143 * @command: Byte interpreted by slave
144 * @value: Byte being written
146 * This executes the SMBus "write byte" protocol, returning negative errno
147 * else zero on success.
149 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
152 union i2c_smbus_data data;
154 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
155 I2C_SMBUS_WRITE, command,
156 I2C_SMBUS_BYTE_DATA, &data);
158 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
161 * i2c_smbus_read_word_data - SMBus "read word" protocol
162 * @client: Handle to slave device
163 * @command: Byte interpreted by slave
165 * This executes the SMBus "read word" protocol, returning negative errno
166 * else a 16-bit unsigned "word" received from the device.
168 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
170 union i2c_smbus_data data;
173 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
174 I2C_SMBUS_READ, command,
175 I2C_SMBUS_WORD_DATA, &data);
176 return (status < 0) ? status : data.word;
178 EXPORT_SYMBOL(i2c_smbus_read_word_data);
181 * i2c_smbus_write_word_data - SMBus "write word" protocol
182 * @client: Handle to slave device
183 * @command: Byte interpreted by slave
184 * @value: 16-bit "word" being written
186 * This executes the SMBus "write word" protocol, returning negative errno
187 * else zero on success.
189 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
192 union i2c_smbus_data data;
194 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
195 I2C_SMBUS_WRITE, command,
196 I2C_SMBUS_WORD_DATA, &data);
198 EXPORT_SYMBOL(i2c_smbus_write_word_data);
201 * i2c_smbus_read_block_data - SMBus "block read" protocol
202 * @client: Handle to slave device
203 * @command: Byte interpreted by slave
204 * @values: Byte array into which data will be read; big enough to hold
205 * the data returned by the slave. SMBus allows at most 32 bytes.
207 * This executes the SMBus "block read" protocol, returning negative errno
208 * else the number of data bytes in the slave's response.
210 * Note that using this function requires that the client's adapter support
211 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
212 * support this; its emulation through I2C messaging relies on a specific
213 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
215 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
218 union i2c_smbus_data data;
221 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
222 I2C_SMBUS_READ, command,
223 I2C_SMBUS_BLOCK_DATA, &data);
227 memcpy(values, &data.block[1], data.block[0]);
228 return data.block[0];
230 EXPORT_SYMBOL(i2c_smbus_read_block_data);
233 * i2c_smbus_write_block_data - SMBus "block write" protocol
234 * @client: Handle to slave device
235 * @command: Byte interpreted by slave
236 * @length: Size of data block; SMBus allows at most 32 bytes
237 * @values: Byte array which will be written.
239 * This executes the SMBus "block write" protocol, returning negative errno
240 * else zero on success.
242 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
243 u8 length, const u8 *values)
245 union i2c_smbus_data data;
247 if (length > I2C_SMBUS_BLOCK_MAX)
248 length = I2C_SMBUS_BLOCK_MAX;
249 data.block[0] = length;
250 memcpy(&data.block[1], values, length);
251 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
252 I2C_SMBUS_WRITE, command,
253 I2C_SMBUS_BLOCK_DATA, &data);
255 EXPORT_SYMBOL(i2c_smbus_write_block_data);
257 /* Returns the number of read bytes */
258 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
259 u8 length, u8 *values)
261 union i2c_smbus_data data;
264 if (length > I2C_SMBUS_BLOCK_MAX)
265 length = I2C_SMBUS_BLOCK_MAX;
266 data.block[0] = length;
267 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
268 I2C_SMBUS_READ, command,
269 I2C_SMBUS_I2C_BLOCK_DATA, &data);
273 memcpy(values, &data.block[1], data.block[0]);
274 return data.block[0];
276 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
278 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
279 u8 length, const u8 *values)
281 union i2c_smbus_data data;
283 if (length > I2C_SMBUS_BLOCK_MAX)
284 length = I2C_SMBUS_BLOCK_MAX;
285 data.block[0] = length;
286 memcpy(data.block + 1, values, length);
287 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
288 I2C_SMBUS_WRITE, command,
289 I2C_SMBUS_I2C_BLOCK_DATA, &data);
291 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
293 static void i2c_smbus_try_get_dmabuf(struct i2c_msg *msg, u8 init_val)
295 bool is_read = msg->flags & I2C_M_RD;
296 unsigned char *dma_buf;
298 dma_buf = kzalloc(I2C_SMBUS_BLOCK_MAX + (is_read ? 2 : 3), GFP_KERNEL);
303 msg->flags |= I2C_M_DMA_SAFE;
306 msg->buf[0] = init_val;
310 * Simulate a SMBus command using the I2C protocol.
311 * No checking of parameters is done!
313 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
314 unsigned short flags,
315 char read_write, u8 command, int size,
316 union i2c_smbus_data *data)
319 * So we need to generate a series of msgs. In the case of writing, we
320 * need to use only one message; when reading, we need two. We
321 * initialize most things with sane defaults, to keep the code below
324 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
325 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
326 int nmsgs = read_write == I2C_SMBUS_READ ? 2 : 1;
329 struct i2c_msg msg[2] = {
337 .flags = flags | I2C_M_RD,
342 bool wants_pec = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
343 && size != I2C_SMBUS_I2C_BLOCK_DATA);
345 msgbuf0[0] = command;
347 case I2C_SMBUS_QUICK:
349 /* Special case: The read/write field is used as data */
350 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
355 if (read_write == I2C_SMBUS_READ) {
356 /* Special case: only a read! */
357 msg[0].flags = I2C_M_RD | flags;
361 case I2C_SMBUS_BYTE_DATA:
362 if (read_write == I2C_SMBUS_READ)
366 msgbuf0[1] = data->byte;
369 case I2C_SMBUS_WORD_DATA:
370 if (read_write == I2C_SMBUS_READ)
374 msgbuf0[1] = data->word & 0xff;
375 msgbuf0[2] = data->word >> 8;
378 case I2C_SMBUS_PROC_CALL:
379 nmsgs = 2; /* Special case */
380 read_write = I2C_SMBUS_READ;
383 msgbuf0[1] = data->word & 0xff;
384 msgbuf0[2] = data->word >> 8;
386 case I2C_SMBUS_BLOCK_DATA:
387 if (read_write == I2C_SMBUS_READ) {
388 msg[1].flags |= I2C_M_RECV_LEN;
389 msg[1].len = 1; /* block length will be added by
390 the underlying bus driver */
391 i2c_smbus_try_get_dmabuf(&msg[1], 0);
393 msg[0].len = data->block[0] + 2;
394 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
395 dev_err(&adapter->dev,
396 "Invalid block write size %d\n",
401 i2c_smbus_try_get_dmabuf(&msg[0], command);
402 memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
405 case I2C_SMBUS_BLOCK_PROC_CALL:
406 nmsgs = 2; /* Another special case */
407 read_write = I2C_SMBUS_READ;
408 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
409 dev_err(&adapter->dev,
410 "Invalid block write size %d\n",
415 msg[0].len = data->block[0] + 2;
416 i2c_smbus_try_get_dmabuf(&msg[0], command);
417 memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
419 msg[1].flags |= I2C_M_RECV_LEN;
420 msg[1].len = 1; /* block length will be added by
421 the underlying bus driver */
422 i2c_smbus_try_get_dmabuf(&msg[1], 0);
424 case I2C_SMBUS_I2C_BLOCK_DATA:
425 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
426 dev_err(&adapter->dev, "Invalid block %s size %d\n",
427 read_write == I2C_SMBUS_READ ? "read" : "write",
432 if (read_write == I2C_SMBUS_READ) {
433 msg[1].len = data->block[0];
434 i2c_smbus_try_get_dmabuf(&msg[1], 0);
436 msg[0].len = data->block[0] + 1;
438 i2c_smbus_try_get_dmabuf(&msg[0], command);
439 memcpy(msg[0].buf + 1, data->block + 1, data->block[0]);
443 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
448 /* Compute PEC if first message is a write */
449 if (!(msg[0].flags & I2C_M_RD)) {
450 if (nmsgs == 1) /* Write only */
451 i2c_smbus_add_pec(&msg[0]);
452 else /* Write followed by read */
453 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
455 /* Ask for PEC if last message is a read */
456 if (msg[nmsgs - 1].flags & I2C_M_RD)
457 msg[nmsgs - 1].len++;
460 status = __i2c_transfer(adapter, msg, nmsgs);
463 if (status != nmsgs) {
469 /* Check PEC if last message is a read */
470 if (wants_pec && (msg[nmsgs - 1].flags & I2C_M_RD)) {
471 status = i2c_smbus_check_pec(partial_pec, &msg[nmsgs - 1]);
476 if (read_write == I2C_SMBUS_READ)
479 data->byte = msgbuf0[0];
481 case I2C_SMBUS_BYTE_DATA:
482 data->byte = msgbuf1[0];
484 case I2C_SMBUS_WORD_DATA:
485 case I2C_SMBUS_PROC_CALL:
486 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
488 case I2C_SMBUS_I2C_BLOCK_DATA:
489 memcpy(data->block + 1, msg[1].buf, data->block[0]);
491 case I2C_SMBUS_BLOCK_DATA:
492 case I2C_SMBUS_BLOCK_PROC_CALL:
493 if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
494 dev_err(&adapter->dev,
495 "Invalid block size returned: %d\n",
500 memcpy(data->block, msg[1].buf, msg[1].buf[0] + 1);
505 if (msg[0].flags & I2C_M_DMA_SAFE)
507 if (msg[1].flags & I2C_M_DMA_SAFE)
514 * i2c_smbus_xfer - execute SMBus protocol operations
515 * @adapter: Handle to I2C bus
516 * @addr: Address of SMBus slave on that bus
517 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
518 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
519 * @command: Byte interpreted by slave, for protocols which use such bytes
520 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
521 * @data: Data to be read or written
523 * This executes an SMBus protocol operation, and returns a negative
524 * errno code else zero on success.
526 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
527 unsigned short flags, char read_write,
528 u8 command, int protocol, union i2c_smbus_data *data)
532 res = __i2c_lock_bus_helper(adapter);
536 res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
537 command, protocol, data);
538 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
542 EXPORT_SYMBOL(i2c_smbus_xfer);
544 s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
545 unsigned short flags, char read_write,
546 u8 command, int protocol, union i2c_smbus_data *data)
548 int (*xfer_func)(struct i2c_adapter *adap, u16 addr,
549 unsigned short flags, char read_write,
550 u8 command, int size, union i2c_smbus_data *data);
551 unsigned long orig_jiffies;
555 res = __i2c_check_suspended(adapter);
559 /* If enabled, the following two tracepoints are conditional on
560 * read_write and protocol.
562 trace_smbus_write(adapter, addr, flags, read_write,
563 command, protocol, data);
564 trace_smbus_read(adapter, addr, flags, read_write,
567 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
569 xfer_func = adapter->algo->smbus_xfer;
570 if (i2c_in_atomic_xfer_mode()) {
571 if (adapter->algo->smbus_xfer_atomic)
572 xfer_func = adapter->algo->smbus_xfer_atomic;
573 else if (adapter->algo->master_xfer_atomic)
574 xfer_func = NULL; /* fallback to I2C emulation */
578 /* Retry automatically on arbitration loss */
579 orig_jiffies = jiffies;
580 for (res = 0, try = 0; try <= adapter->retries; try++) {
581 res = xfer_func(adapter, addr, flags, read_write,
582 command, protocol, data);
585 if (time_after(jiffies,
586 orig_jiffies + adapter->timeout))
590 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
593 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
594 * implement native support for the SMBus operation.
598 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
599 command, protocol, data);
602 /* If enabled, the reply tracepoint is conditional on read_write. */
603 trace_smbus_reply(adapter, addr, flags, read_write,
604 command, protocol, data, res);
605 trace_smbus_result(adapter, addr, flags, read_write,
606 command, protocol, res);
610 EXPORT_SYMBOL(__i2c_smbus_xfer);
613 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
614 * @client: Handle to slave device
615 * @command: Byte interpreted by slave
616 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
617 * @values: Byte array into which data will be read; big enough to hold
618 * the data returned by the slave. SMBus allows at most
619 * I2C_SMBUS_BLOCK_MAX bytes.
621 * This executes the SMBus "block read" protocol if supported by the adapter.
622 * If block read is not supported, it emulates it using either word or byte
623 * read protocols depending on availability.
625 * The addresses of the I2C slave device that are accessed with this function
626 * must be mapped to a linear region, so that a block read will have the same
627 * effect as a byte read. Before using this function you must double-check
628 * if the I2C slave does support exchanging a block transfer with a byte
631 s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
632 u8 command, u8 length, u8 *values)
637 if (length > I2C_SMBUS_BLOCK_MAX)
638 length = I2C_SMBUS_BLOCK_MAX;
640 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
641 return i2c_smbus_read_i2c_block_data(client, command, length, values);
643 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
646 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
647 while ((i + 2) <= length) {
648 status = i2c_smbus_read_word_data(client, command + i);
651 values[i] = status & 0xff;
652 values[i + 1] = status >> 8;
658 status = i2c_smbus_read_byte_data(client, command + i);
667 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
670 * i2c_new_smbus_alert_device - get ara client for SMBus alert support
671 * @adapter: the target adapter
672 * @setup: setup data for the SMBus alert handler
675 * Setup handling of the SMBus alert protocol on a given I2C bus segment.
677 * Handling can be done either through our IRQ handler, or by the
678 * adapter (from its handler, periodic polling, or whatever).
680 * This returns the ara client, which should be saved for later use with
681 * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or an
682 * ERRPTR to indicate an error.
684 struct i2c_client *i2c_new_smbus_alert_device(struct i2c_adapter *adapter,
685 struct i2c_smbus_alert_setup *setup)
687 struct i2c_board_info ara_board_info = {
688 I2C_BOARD_INFO("smbus_alert", 0x0c),
689 .platform_data = setup,
692 return i2c_new_client_device(adapter, &ara_board_info);
694 EXPORT_SYMBOL_GPL(i2c_new_smbus_alert_device);
696 #if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_OF)
697 int of_i2c_setup_smbus_alert(struct i2c_adapter *adapter)
701 irq = of_property_match_string(adapter->dev.of_node, "interrupt-names",
703 if (irq == -EINVAL || irq == -ENODATA)
708 return PTR_ERR_OR_ZERO(i2c_new_smbus_alert_device(adapter, NULL));
710 EXPORT_SYMBOL_GPL(of_i2c_setup_smbus_alert);