Lines Matching +full:byte +full:- +full:len

1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
16 #include <linux/i2c-smbus.h>
20 #include "i2c-core.h"
42 * i2c_smbus_pec - Incremental CRC8 over the given input data array
59 /* Assume a 7-bit address, which is reasonable for SMBus */
67 return i2c_smbus_pec(pec, msg->buf, msg->len); in i2c_smbus_msg_pec()
73 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); in i2c_smbus_add_pec()
74 msg->len++; in i2c_smbus_add_pec()
81 message length to hide the CRC byte from the caller). */
84 u8 rpec = msg->buf[--msg->len]; in i2c_smbus_check_pec()
90 return -EBADMSG; in i2c_smbus_check_pec()
96 * i2c_smbus_read_byte - SMBus "receive byte" protocol
99 * This executes the SMBus "receive byte" protocol, returning negative errno
100 * else the byte received from the device.
107 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, in i2c_smbus_read_byte()
110 return (status < 0) ? status : data.byte; in i2c_smbus_read_byte()
115 * i2c_smbus_write_byte - SMBus "send byte" protocol
117 * @value: Byte to be sent
119 * This executes the SMBus "send byte" protocol, returning negative errno
124 return i2c_smbus_xfer(client->adapter, client->addr, client->flags, in i2c_smbus_write_byte()
130 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
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.
142 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, in i2c_smbus_read_byte_data()
145 return (status < 0) ? status : data.byte; in i2c_smbus_read_byte_data()
150 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
152 * @command: Byte interpreted by slave
153 * @value: Byte being written
155 * This executes the SMBus "write byte" protocol, returning negative errno
162 data.byte = value; in i2c_smbus_write_byte_data()
163 return i2c_smbus_xfer(client->adapter, client->addr, client->flags, in i2c_smbus_write_byte_data()
170 * i2c_smbus_read_word_data - SMBus "read word" protocol
172 * @command: Byte interpreted by slave
175 * else a 16-bit unsigned "word" received from the device.
182 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, in i2c_smbus_read_word_data()
190 * i2c_smbus_write_word_data - SMBus "write word" protocol
192 * @command: Byte interpreted by slave
193 * @value: 16-bit "word" being written
203 return i2c_smbus_xfer(client->adapter, client->addr, client->flags, in i2c_smbus_write_word_data()
210 * i2c_smbus_read_block_data - SMBus "block read" protocol
212 * @command: Byte interpreted by slave
213 * @values: Byte array into which data will be read; big enough to hold
230 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, in i2c_smbus_read_block_data()
242 * i2c_smbus_write_block_data - SMBus "block write" protocol
244 * @command: Byte interpreted by slave
246 * @values: Byte array which will be written.
260 return i2c_smbus_xfer(client->adapter, client->addr, client->flags, in i2c_smbus_write_block_data()
276 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, in i2c_smbus_read_i2c_block_data()
296 return i2c_smbus_xfer(client->adapter, client->addr, client->flags, in i2c_smbus_write_i2c_block_data()
304 bool is_read = msg->flags & I2C_M_RD; in i2c_smbus_try_get_dmabuf()
311 msg->buf = dma_buf; in i2c_smbus_try_get_dmabuf()
312 msg->flags |= I2C_M_DMA_SAFE; in i2c_smbus_try_get_dmabuf()
315 msg->buf[0] = init_val; in i2c_smbus_try_get_dmabuf()
342 .len = 1, in i2c_smbus_xfer_emulated()
347 .len = 0, in i2c_smbus_xfer_emulated()
357 msg[0].len = 0; in i2c_smbus_xfer_emulated()
372 msg[1].len = 1; in i2c_smbus_xfer_emulated()
374 msg[0].len = 2; in i2c_smbus_xfer_emulated()
375 msgbuf0[1] = data->byte; in i2c_smbus_xfer_emulated()
380 msg[1].len = 2; in i2c_smbus_xfer_emulated()
382 msg[0].len = 3; in i2c_smbus_xfer_emulated()
383 msgbuf0[1] = data->word & 0xff; in i2c_smbus_xfer_emulated()
384 msgbuf0[2] = data->word >> 8; in i2c_smbus_xfer_emulated()
390 msg[0].len = 3; in i2c_smbus_xfer_emulated()
391 msg[1].len = 2; in i2c_smbus_xfer_emulated()
392 msgbuf0[1] = data->word & 0xff; in i2c_smbus_xfer_emulated()
393 msgbuf0[2] = data->word >> 8; in i2c_smbus_xfer_emulated()
398 msg[1].len = 1; /* block length will be added by in i2c_smbus_xfer_emulated()
402 msg[0].len = data->block[0] + 2; in i2c_smbus_xfer_emulated()
403 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { in i2c_smbus_xfer_emulated()
404 dev_err(&adapter->dev, in i2c_smbus_xfer_emulated()
406 data->block[0]); in i2c_smbus_xfer_emulated()
407 return -EINVAL; in i2c_smbus_xfer_emulated()
411 memcpy(msg[0].buf + 1, data->block, msg[0].len - 1); in i2c_smbus_xfer_emulated()
417 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { in i2c_smbus_xfer_emulated()
418 dev_err(&adapter->dev, in i2c_smbus_xfer_emulated()
420 data->block[0]); in i2c_smbus_xfer_emulated()
421 return -EINVAL; in i2c_smbus_xfer_emulated()
424 msg[0].len = data->block[0] + 2; in i2c_smbus_xfer_emulated()
426 memcpy(msg[0].buf + 1, data->block, msg[0].len - 1); in i2c_smbus_xfer_emulated()
429 msg[1].len = 1; /* block length will be added by in i2c_smbus_xfer_emulated()
434 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { in i2c_smbus_xfer_emulated()
435 dev_err(&adapter->dev, "Invalid block %s size %d\n", in i2c_smbus_xfer_emulated()
437 data->block[0]); in i2c_smbus_xfer_emulated()
438 return -EINVAL; in i2c_smbus_xfer_emulated()
442 msg[1].len = data->block[0]; in i2c_smbus_xfer_emulated()
445 msg[0].len = data->block[0] + 1; in i2c_smbus_xfer_emulated()
448 memcpy(msg[0].buf + 1, data->block + 1, data->block[0]); in i2c_smbus_xfer_emulated()
452 dev_err(&adapter->dev, "Unsupported transaction %d\n", size); in i2c_smbus_xfer_emulated()
453 return -EOPNOTSUPP; in i2c_smbus_xfer_emulated()
465 if (msg[nmsgs - 1].flags & I2C_M_RD) in i2c_smbus_xfer_emulated()
466 msg[nmsgs - 1].len++; in i2c_smbus_xfer_emulated()
473 status = -EIO; in i2c_smbus_xfer_emulated()
479 if (wants_pec && (msg[nmsgs - 1].flags & I2C_M_RD)) { in i2c_smbus_xfer_emulated()
480 status = i2c_smbus_check_pec(partial_pec, &msg[nmsgs - 1]); in i2c_smbus_xfer_emulated()
488 data->byte = msgbuf0[0]; in i2c_smbus_xfer_emulated()
491 data->byte = msgbuf1[0]; in i2c_smbus_xfer_emulated()
495 data->word = msgbuf1[0] | (msgbuf1[1] << 8); in i2c_smbus_xfer_emulated()
498 memcpy(data->block + 1, msg[1].buf, data->block[0]); in i2c_smbus_xfer_emulated()
503 dev_err(&adapter->dev, in i2c_smbus_xfer_emulated()
506 status = -EPROTO; in i2c_smbus_xfer_emulated()
509 memcpy(data->block, msg[1].buf, msg[1].buf[0] + 1); in i2c_smbus_xfer_emulated()
523 * i2c_smbus_xfer - execute SMBus protocol operations
528 * @command: Byte interpreted by slave, for protocols which use such bytes
578 xfer_func = adapter->algo->smbus_xfer; in __i2c_smbus_xfer()
580 if (adapter->algo->smbus_xfer_atomic) in __i2c_smbus_xfer()
581 xfer_func = adapter->algo->smbus_xfer_atomic; in __i2c_smbus_xfer()
582 else if (adapter->algo->master_xfer_atomic) in __i2c_smbus_xfer()
589 for (res = 0, try = 0; try <= adapter->retries; try++) { in __i2c_smbus_xfer()
592 if (res != -EAGAIN) in __i2c_smbus_xfer()
595 orig_jiffies + adapter->timeout)) in __i2c_smbus_xfer()
599 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer) in __i2c_smbus_xfer()
622 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
624 * @command: Byte interpreted by slave
626 * @values: Byte array into which data will be read; big enough to hold
631 * If block read is not supported, it emulates it using either word or byte
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
649 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) in i2c_smbus_read_i2c_block_data_or_emulated()
652 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) in i2c_smbus_read_i2c_block_data_or_emulated()
653 return -EOPNOTSUPP; in i2c_smbus_read_i2c_block_data_or_emulated()
655 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) { in i2c_smbus_read_i2c_block_data_or_emulated()
679 * i2c_new_smbus_alert_device - get ara client for SMBus alert support
708 struct device *parent = adapter->dev.parent; in i2c_setup_smbus_alert()
715 irq = device_property_match_string(parent, "interrupt-names", "smbus_alert"); in i2c_setup_smbus_alert()
716 if (irq == -EINVAL || irq == -ENODATA) in i2c_setup_smbus_alert()