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.
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>
21 #include <linux/slab.h>
25 #define CREATE_TRACE_POINTS
26 #include <trace/events/smbus.h>
31 #define POLY (0x1070U << 3)
32 static u8 crc8(u16 data)
36 for (i = 0; i < 8; i++) {
41 return (u8)(data >> 8);
44 /* Incremental CRC8 over count bytes in the array pointed to by p */
45 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
49 for (i = 0; i < count; i++)
50 crc = crc8((crc ^ p[i]) << 8);
54 /* Assume a 7-bit address, which is reasonable for SMBus */
55 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
57 /* The address will be sent first */
58 u8 addr = i2c_8bit_addr_from_msg(msg);
59 pec = i2c_smbus_pec(pec, &addr, 1);
61 /* The data buffer follows */
62 return i2c_smbus_pec(pec, msg->buf, msg->len);
65 /* Used for write only transactions */
66 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
68 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
72 /* Return <0 on CRC error
73 If there was a write before this read (most cases) we need to take the
74 partial CRC from the write part into account.
75 Note that this function does modify the message (we need to decrease the
76 message length to hide the CRC byte from the caller). */
77 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
79 u8 rpec = msg->buf[--msg->len];
80 cpec = i2c_smbus_msg_pec(cpec, msg);
83 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
91 * i2c_smbus_read_byte - SMBus "receive byte" protocol
92 * @client: Handle to slave device
94 * This executes the SMBus "receive byte" protocol, returning negative errno
95 * else the byte received from the device.
97 s32 i2c_smbus_read_byte(const struct i2c_client *client)
99 union i2c_smbus_data data;
102 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
104 I2C_SMBUS_BYTE, &data);
105 return (status < 0) ? status : data.byte;
107 EXPORT_SYMBOL(i2c_smbus_read_byte);
110 * i2c_smbus_write_byte - SMBus "send byte" protocol
111 * @client: Handle to slave device
112 * @value: Byte to be sent
114 * This executes the SMBus "send byte" protocol, returning negative errno
115 * else zero on success.
117 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
119 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
120 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
122 EXPORT_SYMBOL(i2c_smbus_write_byte);
125 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
126 * @client: Handle to slave device
127 * @command: Byte interpreted by slave
129 * This executes the SMBus "read byte" protocol, returning negative errno
130 * else a data byte received from the device.
132 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
134 union i2c_smbus_data data;
137 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
138 I2C_SMBUS_READ, command,
139 I2C_SMBUS_BYTE_DATA, &data);
140 return (status < 0) ? status : data.byte;
142 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
145 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
146 * @client: Handle to slave device
147 * @command: Byte interpreted by slave
148 * @value: Byte being written
150 * This executes the SMBus "write byte" protocol, returning negative errno
151 * else zero on success.
153 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
156 union i2c_smbus_data data;
158 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
159 I2C_SMBUS_WRITE, command,
160 I2C_SMBUS_BYTE_DATA, &data);
162 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
165 * i2c_smbus_read_word_data - SMBus "read word" protocol
166 * @client: Handle to slave device
167 * @command: Byte interpreted by slave
169 * This executes the SMBus "read word" protocol, returning negative errno
170 * else a 16-bit unsigned "word" received from the device.
172 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
174 union i2c_smbus_data data;
177 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
178 I2C_SMBUS_READ, command,
179 I2C_SMBUS_WORD_DATA, &data);
180 return (status < 0) ? status : data.word;
182 EXPORT_SYMBOL(i2c_smbus_read_word_data);
185 * i2c_smbus_write_word_data - SMBus "write word" protocol
186 * @client: Handle to slave device
187 * @command: Byte interpreted by slave
188 * @value: 16-bit "word" being written
190 * This executes the SMBus "write word" protocol, returning negative errno
191 * else zero on success.
193 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
196 union i2c_smbus_data data;
198 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
199 I2C_SMBUS_WRITE, command,
200 I2C_SMBUS_WORD_DATA, &data);
202 EXPORT_SYMBOL(i2c_smbus_write_word_data);
205 * i2c_smbus_read_block_data - SMBus "block read" protocol
206 * @client: Handle to slave device
207 * @command: Byte interpreted by slave
208 * @values: Byte array into which data will be read; big enough to hold
209 * the data returned by the slave. SMBus allows at most 32 bytes.
211 * This executes the SMBus "block read" protocol, returning negative errno
212 * else the number of data bytes in the slave's response.
214 * Note that using this function requires that the client's adapter support
215 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
216 * support this; its emulation through I2C messaging relies on a specific
217 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
219 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
222 union i2c_smbus_data data;
225 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
226 I2C_SMBUS_READ, command,
227 I2C_SMBUS_BLOCK_DATA, &data);
231 memcpy(values, &data.block[1], data.block[0]);
232 return data.block[0];
234 EXPORT_SYMBOL(i2c_smbus_read_block_data);
237 * i2c_smbus_write_block_data - SMBus "block write" protocol
238 * @client: Handle to slave device
239 * @command: Byte interpreted by slave
240 * @length: Size of data block; SMBus allows at most 32 bytes
241 * @values: Byte array which will be written.
243 * This executes the SMBus "block write" protocol, returning negative errno
244 * else zero on success.
246 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
247 u8 length, const u8 *values)
249 union i2c_smbus_data data;
251 if (length > I2C_SMBUS_BLOCK_MAX)
252 length = I2C_SMBUS_BLOCK_MAX;
253 data.block[0] = length;
254 memcpy(&data.block[1], values, length);
255 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
256 I2C_SMBUS_WRITE, command,
257 I2C_SMBUS_BLOCK_DATA, &data);
259 EXPORT_SYMBOL(i2c_smbus_write_block_data);
261 /* Returns the number of read bytes */
262 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
263 u8 length, u8 *values)
265 union i2c_smbus_data data;
268 if (length > I2C_SMBUS_BLOCK_MAX)
269 length = I2C_SMBUS_BLOCK_MAX;
270 data.block[0] = length;
271 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
272 I2C_SMBUS_READ, command,
273 I2C_SMBUS_I2C_BLOCK_DATA, &data);
277 memcpy(values, &data.block[1], data.block[0]);
278 return data.block[0];
280 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
282 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
283 u8 length, const u8 *values)
285 union i2c_smbus_data data;
287 if (length > I2C_SMBUS_BLOCK_MAX)
288 length = I2C_SMBUS_BLOCK_MAX;
289 data.block[0] = length;
290 memcpy(data.block + 1, values, length);
291 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
292 I2C_SMBUS_WRITE, command,
293 I2C_SMBUS_I2C_BLOCK_DATA, &data);
295 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
297 static void i2c_smbus_try_get_dmabuf(struct i2c_msg *msg, u8 init_val)
299 bool is_read = msg->flags & I2C_M_RD;
300 unsigned char *dma_buf;
302 dma_buf = kzalloc(I2C_SMBUS_BLOCK_MAX + (is_read ? 2 : 3), GFP_KERNEL);
307 msg->flags |= I2C_M_DMA_SAFE;
310 msg->buf[0] = init_val;
314 * Simulate a SMBus command using the I2C protocol.
315 * No checking of parameters is done!
317 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
318 unsigned short flags,
319 char read_write, u8 command, int size,
320 union i2c_smbus_data *data)
323 * So we need to generate a series of msgs. In the case of writing, we
324 * need to use only one message; when reading, we need two. We
325 * initialize most things with sane defaults, to keep the code below
328 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
329 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
330 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
334 struct i2c_msg msg[2] = {
342 .flags = flags | I2C_M_RD,
348 msgbuf0[0] = command;
350 case I2C_SMBUS_QUICK:
352 /* Special case: The read/write field is used as data */
353 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
358 if (read_write == I2C_SMBUS_READ) {
359 /* Special case: only a read! */
360 msg[0].flags = I2C_M_RD | flags;
364 case I2C_SMBUS_BYTE_DATA:
365 if (read_write == I2C_SMBUS_READ)
369 msgbuf0[1] = data->byte;
372 case I2C_SMBUS_WORD_DATA:
373 if (read_write == I2C_SMBUS_READ)
377 msgbuf0[1] = data->word & 0xff;
378 msgbuf0[2] = data->word >> 8;
381 case I2C_SMBUS_PROC_CALL:
382 num = 2; /* Special case */
383 read_write = I2C_SMBUS_READ;
386 msgbuf0[1] = data->word & 0xff;
387 msgbuf0[2] = data->word >> 8;
389 case I2C_SMBUS_BLOCK_DATA:
390 if (read_write == I2C_SMBUS_READ) {
391 msg[1].flags |= I2C_M_RECV_LEN;
392 msg[1].len = 1; /* block length will be added by
393 the underlying bus driver */
394 i2c_smbus_try_get_dmabuf(&msg[1], 0);
396 msg[0].len = data->block[0] + 2;
397 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
398 dev_err(&adapter->dev,
399 "Invalid block write size %d\n",
404 i2c_smbus_try_get_dmabuf(&msg[0], command);
405 for (i = 1; i < msg[0].len; i++)
406 msg[0].buf[i] = data->block[i - 1];
409 case I2C_SMBUS_BLOCK_PROC_CALL:
410 num = 2; /* Another special case */
411 read_write = I2C_SMBUS_READ;
412 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
413 dev_err(&adapter->dev,
414 "Invalid block write size %d\n",
419 msg[0].len = data->block[0] + 2;
420 i2c_smbus_try_get_dmabuf(&msg[0], command);
421 for (i = 1; i < msg[0].len; i++)
422 msg[0].buf[i] = data->block[i - 1];
424 msg[1].flags |= I2C_M_RECV_LEN;
425 msg[1].len = 1; /* block length will be added by
426 the underlying bus driver */
427 i2c_smbus_try_get_dmabuf(&msg[1], 0);
429 case I2C_SMBUS_I2C_BLOCK_DATA:
430 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
431 dev_err(&adapter->dev, "Invalid block %s size %d\n",
432 read_write == I2C_SMBUS_READ ? "read" : "write",
437 if (read_write == I2C_SMBUS_READ) {
438 msg[1].len = data->block[0];
439 i2c_smbus_try_get_dmabuf(&msg[1], 0);
441 msg[0].len = data->block[0] + 1;
443 i2c_smbus_try_get_dmabuf(&msg[0], command);
444 for (i = 1; i <= data->block[0]; i++)
445 msg[0].buf[i] = data->block[i];
449 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
453 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
454 && size != I2C_SMBUS_I2C_BLOCK_DATA);
456 /* Compute PEC if first message is a write */
457 if (!(msg[0].flags & I2C_M_RD)) {
458 if (num == 1) /* Write only */
459 i2c_smbus_add_pec(&msg[0]);
460 else /* Write followed by read */
461 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
463 /* Ask for PEC if last message is a read */
464 if (msg[num-1].flags & I2C_M_RD)
468 status = __i2c_transfer(adapter, msg, num);
477 /* Check PEC if last message is a read */
478 if (i && (msg[num-1].flags & I2C_M_RD)) {
479 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
484 if (read_write == I2C_SMBUS_READ)
487 data->byte = msgbuf0[0];
489 case I2C_SMBUS_BYTE_DATA:
490 data->byte = msgbuf1[0];
492 case I2C_SMBUS_WORD_DATA:
493 case I2C_SMBUS_PROC_CALL:
494 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
496 case I2C_SMBUS_I2C_BLOCK_DATA:
497 for (i = 0; i < data->block[0]; i++)
498 data->block[i + 1] = msg[1].buf[i];
500 case I2C_SMBUS_BLOCK_DATA:
501 case I2C_SMBUS_BLOCK_PROC_CALL:
502 for (i = 0; i < msg[1].buf[0] + 1; i++)
503 data->block[i] = msg[1].buf[i];
508 if (msg[0].flags & I2C_M_DMA_SAFE)
510 if (msg[1].flags & I2C_M_DMA_SAFE)
517 * i2c_smbus_xfer - execute SMBus protocol operations
518 * @adapter: Handle to I2C bus
519 * @addr: Address of SMBus slave on that bus
520 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
521 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
522 * @command: Byte interpreted by slave, for protocols which use such bytes
523 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
524 * @data: Data to be read or written
526 * This executes an SMBus protocol operation, and returns a negative
527 * errno code else zero on success.
529 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
530 unsigned short flags, char read_write,
531 u8 command, int protocol, union i2c_smbus_data *data)
535 res = __i2c_lock_bus_helper(adapter);
539 res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
540 command, protocol, data);
541 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
545 EXPORT_SYMBOL(i2c_smbus_xfer);
547 s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
548 unsigned short flags, char read_write,
549 u8 command, int protocol, union i2c_smbus_data *data)
551 int (*xfer_func)(struct i2c_adapter *adap, u16 addr,
552 unsigned short flags, char read_write,
553 u8 command, int size, union i2c_smbus_data *data);
554 unsigned long orig_jiffies;
558 res = __i2c_check_suspended(adapter);
562 /* If enabled, the following two tracepoints are conditional on
563 * read_write and protocol.
565 trace_smbus_write(adapter, addr, flags, read_write,
566 command, protocol, data);
567 trace_smbus_read(adapter, addr, flags, read_write,
570 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
572 xfer_func = adapter->algo->smbus_xfer;
573 if (i2c_in_atomic_xfer_mode()) {
574 if (adapter->algo->smbus_xfer_atomic)
575 xfer_func = adapter->algo->smbus_xfer_atomic;
576 else if (adapter->algo->master_xfer_atomic)
577 xfer_func = NULL; /* fallback to I2C emulation */
581 /* Retry automatically on arbitration loss */
582 orig_jiffies = jiffies;
583 for (res = 0, try = 0; try <= adapter->retries; try++) {
584 res = xfer_func(adapter, addr, flags, read_write,
585 command, protocol, data);
588 if (time_after(jiffies,
589 orig_jiffies + adapter->timeout))
593 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
596 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
597 * implement native support for the SMBus operation.
601 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
602 command, protocol, data);
605 /* If enabled, the reply tracepoint is conditional on read_write. */
606 trace_smbus_reply(adapter, addr, flags, read_write,
607 command, protocol, data, res);
608 trace_smbus_result(adapter, addr, flags, read_write,
609 command, protocol, res);
613 EXPORT_SYMBOL(__i2c_smbus_xfer);
616 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
617 * @client: Handle to slave device
618 * @command: Byte interpreted by slave
619 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
620 * @values: Byte array into which data will be read; big enough to hold
621 * the data returned by the slave. SMBus allows at most
622 * I2C_SMBUS_BLOCK_MAX bytes.
624 * This executes the SMBus "block read" protocol if supported by the adapter.
625 * If block read is not supported, it emulates it using either word or byte
626 * read protocols depending on availability.
628 * The addresses of the I2C slave device that are accessed with this function
629 * must be mapped to a linear region, so that a block read will have the same
630 * effect as a byte read. Before using this function you must double-check
631 * if the I2C slave does support exchanging a block transfer with a byte
634 s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
635 u8 command, u8 length, u8 *values)
640 if (length > I2C_SMBUS_BLOCK_MAX)
641 length = I2C_SMBUS_BLOCK_MAX;
643 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
644 return i2c_smbus_read_i2c_block_data(client, command, length, values);
646 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
649 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
650 while ((i + 2) <= length) {
651 status = i2c_smbus_read_word_data(client, command + i);
654 values[i] = status & 0xff;
655 values[i + 1] = status >> 8;
661 status = i2c_smbus_read_byte_data(client, command + i);
670 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
673 * i2c_setup_smbus_alert - Setup SMBus alert support
674 * @adapter: the target adapter
675 * @setup: setup data for the SMBus alert handler
678 * Setup handling of the SMBus alert protocol on a given I2C bus segment.
680 * Handling can be done either through our IRQ handler, or by the
681 * adapter (from its handler, periodic polling, or whatever).
683 * NOTE that if we manage the IRQ, we *MUST* know if it's level or
684 * edge triggered in order to hand it to the workqueue correctly.
685 * If triggering the alert seems to wedge the system, you probably
686 * should have said it's level triggered.
688 * This returns the ara client, which should be saved for later use with
689 * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or NULL
690 * to indicate an error.
692 struct i2c_client *i2c_setup_smbus_alert(struct i2c_adapter *adapter,
693 struct i2c_smbus_alert_setup *setup)
695 struct i2c_board_info ara_board_info = {
696 I2C_BOARD_INFO("smbus_alert", 0x0c),
697 .platform_data = setup,
700 return i2c_new_device(adapter, &ara_board_info);
702 EXPORT_SYMBOL_GPL(i2c_setup_smbus_alert);
704 #if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_OF)
705 int of_i2c_setup_smbus_alert(struct i2c_adapter *adapter)
707 struct i2c_client *client;
710 irq = of_property_match_string(adapter->dev.of_node, "interrupt-names",
712 if (irq == -EINVAL || irq == -ENODATA)
717 client = i2c_setup_smbus_alert(adapter, NULL);
723 EXPORT_SYMBOL_GPL(of_i2c_setup_smbus_alert);