Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Linux I2C core SMBus and SMBus emulation code
  4 *
  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.
  8 *
  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>
 12 */
 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>
 19
 20#include "i2c-core.h"
 21
 22#define CREATE_TRACE_POINTS
 23#include <trace/events/smbus.h>
 24
 25
 26/* The SMBus parts */
 27
 28#define POLY    (0x1070U << 3)
 29static u8 crc8(u16 data)
 30{
 31	int i;
 32
 33	for (i = 0; i < 8; i++) {
 34		if (data & 0x8000)
 35			data = data ^ POLY;
 36		data = data << 1;
 37	}
 38	return (u8)(data >> 8);
 39}
 40
 41/**
 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.
 46 *
 47 * Incremental CRC8 over count bytes in the array pointed to by p
 48 */
 49u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
 50{
 51	int i;
 52
 53	for (i = 0; i < count; i++)
 54		crc = crc8((crc ^ p[i]) << 8);
 55	return crc;
 56}
 57EXPORT_SYMBOL(i2c_smbus_pec);
 58
 59/* Assume a 7-bit address, which is reasonable for SMBus */
 60static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
 61{
 62	/* The address will be sent first */
 63	u8 addr = i2c_8bit_addr_from_msg(msg);
 64	pec = i2c_smbus_pec(pec, &addr, 1);
 65
 66	/* The data buffer follows */
 67	return i2c_smbus_pec(pec, msg->buf, msg->len);
 68}
 69
 70/* Used for write only transactions */
 71static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
 72{
 73	msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
 74	msg->len++;
 75}
 76
 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). */
 82static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
 83{
 84	u8 rpec = msg->buf[--msg->len];
 85	cpec = i2c_smbus_msg_pec(cpec, msg);
 86
 87	if (rpec != cpec) {
 88		pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
 89			rpec, cpec);
 90		return -EBADMSG;
 91	}
 92	return 0;
 93}
 94
 95/**
 96 * i2c_smbus_read_byte - SMBus "receive byte" protocol
 97 * @client: Handle to slave device
 98 *
 99 * This executes the SMBus "receive byte" protocol, returning negative errno
100 * else the byte received from the device.
101 */
102s32 i2c_smbus_read_byte(const struct i2c_client *client)
103{
104	union i2c_smbus_data data;
105	int status;
106
107	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
108				I2C_SMBUS_READ, 0,
109				I2C_SMBUS_BYTE, &data);
110	return (status < 0) ? status : data.byte;
111}
112EXPORT_SYMBOL(i2c_smbus_read_byte);
113
114/**
115 * i2c_smbus_write_byte - SMBus "send byte" protocol
116 * @client: Handle to slave device
117 * @value: Byte to be sent
118 *
119 * This executes the SMBus "send byte" protocol, returning negative errno
120 * else zero on success.
121 */
122s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
123{
124	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
125			      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
126}
127EXPORT_SYMBOL(i2c_smbus_write_byte);
128
129/**
130 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
131 * @client: Handle to slave device
132 * @command: Byte interpreted by slave
133 *
134 * This executes the SMBus "read byte" protocol, returning negative errno
135 * else a data byte received from the device.
136 */
137s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
138{
139	union i2c_smbus_data data;
140	int status;
141
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;
146}
147EXPORT_SYMBOL(i2c_smbus_read_byte_data);
148
149/**
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
154 *
155 * This executes the SMBus "write byte" protocol, returning negative errno
156 * else zero on success.
157 */
158s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
159			      u8 value)
160{
161	union i2c_smbus_data data;
162	data.byte = value;
163	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
164			      I2C_SMBUS_WRITE, command,
165			      I2C_SMBUS_BYTE_DATA, &data);
166}
167EXPORT_SYMBOL(i2c_smbus_write_byte_data);
168
169/**
170 * i2c_smbus_read_word_data - SMBus "read word" protocol
171 * @client: Handle to slave device
172 * @command: Byte interpreted by slave
173 *
174 * This executes the SMBus "read word" protocol, returning negative errno
175 * else a 16-bit unsigned "word" received from the device.
176 */
177s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
178{
179	union i2c_smbus_data data;
180	int status;
181
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;
186}
187EXPORT_SYMBOL(i2c_smbus_read_word_data);
188
189/**
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
194 *
195 * This executes the SMBus "write word" protocol, returning negative errno
196 * else zero on success.
197 */
198s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
199			      u16 value)
200{
201	union i2c_smbus_data data;
202	data.word = value;
203	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
204			      I2C_SMBUS_WRITE, command,
205			      I2C_SMBUS_WORD_DATA, &data);
206}
207EXPORT_SYMBOL(i2c_smbus_write_word_data);
208
209/**
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.
215 *
216 * This executes the SMBus "block read" protocol, returning negative errno
217 * else the number of data bytes in the slave's response.
218 *
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.
223 */
224s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
225			      u8 *values)
226{
227	union i2c_smbus_data data;
228	int status;
229
230	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
231				I2C_SMBUS_READ, command,
232				I2C_SMBUS_BLOCK_DATA, &data);
233	if (status)
234		return status;
235
236	memcpy(values, &data.block[1], data.block[0]);
237	return data.block[0];
238}
239EXPORT_SYMBOL(i2c_smbus_read_block_data);
240
241/**
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.
247 *
248 * This executes the SMBus "block write" protocol, returning negative errno
249 * else zero on success.
250 */
251s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
252			       u8 length, const u8 *values)
253{
254	union i2c_smbus_data data;
255
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);
263}
264EXPORT_SYMBOL(i2c_smbus_write_block_data);
265
266/* Returns the number of read bytes */
267s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
268				  u8 length, u8 *values)
269{
270	union i2c_smbus_data data;
271	int status;
272
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);
279	if (status < 0)
280		return status;
281
282	memcpy(values, &data.block[1], data.block[0]);
283	return data.block[0];
284}
285EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
286
287s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
288				   u8 length, const u8 *values)
289{
290	union i2c_smbus_data data;
291
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);
299}
300EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
301
302static void i2c_smbus_try_get_dmabuf(struct i2c_msg *msg, u8 init_val)
303{
304	bool is_read = msg->flags & I2C_M_RD;
305	unsigned char *dma_buf;
306
307	dma_buf = kzalloc(I2C_SMBUS_BLOCK_MAX + (is_read ? 2 : 3), GFP_KERNEL);
308	if (!dma_buf)
309		return;
310
311	msg->buf = dma_buf;
312	msg->flags |= I2C_M_DMA_SAFE;
313
314	if (init_val)
315		msg->buf[0] = init_val;
316}
317
318/*
319 * Simulate a SMBus command using the I2C protocol.
320 * No checking of parameters is done!
321 */
322static 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)
326{
327	/*
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
331	 * somewhat simpler.
332	 */
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;
336	u8 partial_pec = 0;
337	int status;
338	struct i2c_msg msg[2] = {
339		{
340			.addr = addr,
341			.flags = flags,
342			.len = 1,
343			.buf = msgbuf0,
344		}, {
345			.addr = addr,
346			.flags = flags | I2C_M_RD,
347			.len = 0,
348			.buf = msgbuf1,
349		},
350	};
351	bool wants_pec = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
352			  && size != I2C_SMBUS_I2C_BLOCK_DATA);
353
354	msgbuf0[0] = command;
355	switch (size) {
356	case I2C_SMBUS_QUICK:
357		msg[0].len = 0;
358		/* Special case: The read/write field is used as data */
359		msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
360					I2C_M_RD : 0);
361		nmsgs = 1;
362		break;
363	case I2C_SMBUS_BYTE:
364		if (read_write == I2C_SMBUS_READ) {
365			/* Special case: only a read! */
366			msg[0].flags = I2C_M_RD | flags;
367			nmsgs = 1;
368		}
369		break;
370	case I2C_SMBUS_BYTE_DATA:
371		if (read_write == I2C_SMBUS_READ)
372			msg[1].len = 1;
373		else {
374			msg[0].len = 2;
375			msgbuf0[1] = data->byte;
376		}
377		break;
378	case I2C_SMBUS_WORD_DATA:
379		if (read_write == I2C_SMBUS_READ)
380			msg[1].len = 2;
381		else {
382			msg[0].len = 3;
383			msgbuf0[1] = data->word & 0xff;
384			msgbuf0[2] = data->word >> 8;
385		}
386		break;
387	case I2C_SMBUS_PROC_CALL:
388		nmsgs = 2; /* Special case */
389		read_write = I2C_SMBUS_READ;
390		msg[0].len = 3;
391		msg[1].len = 2;
392		msgbuf0[1] = data->word & 0xff;
393		msgbuf0[2] = data->word >> 8;
394		break;
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);
401		} else {
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",
406					data->block[0]);
407				return -EINVAL;
408			}
409
410			i2c_smbus_try_get_dmabuf(&msg[0], command);
411			memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
412		}
413		break;
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",
420				data->block[0]);
421			return -EINVAL;
422		}
423
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);
427
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);
432		break;
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",
437				data->block[0]);
438			return -EINVAL;
439		}
440
441		if (read_write == I2C_SMBUS_READ) {
442			msg[1].len = data->block[0];
443			i2c_smbus_try_get_dmabuf(&msg[1], 0);
444		} else {
445			msg[0].len = data->block[0] + 1;
446
447			i2c_smbus_try_get_dmabuf(&msg[0], command);
448			memcpy(msg[0].buf + 1, data->block + 1, data->block[0]);
449		}
450		break;
451	default:
452		dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
453		return -EOPNOTSUPP;
454	}
455
456	if (wants_pec) {
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]);
463		}
464		/* Ask for PEC if last message is a read */
465		if (msg[nmsgs - 1].flags & I2C_M_RD)
466			msg[nmsgs - 1].len++;
467	}
468
469	status = __i2c_transfer(adapter, msg, nmsgs);
470	if (status < 0)
471		goto cleanup;
472	if (status != nmsgs) {
473		status = -EIO;
474		goto cleanup;
475	}
476	status = 0;
477
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]);
481		if (status < 0)
482			goto cleanup;
483	}
484
485	if (read_write == I2C_SMBUS_READ)
486		switch (size) {
487		case I2C_SMBUS_BYTE:
488			data->byte = msgbuf0[0];
489			break;
490		case I2C_SMBUS_BYTE_DATA:
491			data->byte = msgbuf1[0];
492			break;
493		case I2C_SMBUS_WORD_DATA:
494		case I2C_SMBUS_PROC_CALL:
495			data->word = msgbuf1[0] | (msgbuf1[1] << 8);
496			break;
497		case I2C_SMBUS_I2C_BLOCK_DATA:
498			memcpy(data->block + 1, msg[1].buf, data->block[0]);
499			break;
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",
505					msg[1].buf[0]);
506				status = -EPROTO;
507				goto cleanup;
508			}
509			memcpy(data->block, msg[1].buf, msg[1].buf[0] + 1);
510			break;
511		}
512
513cleanup:
514	if (msg[0].flags & I2C_M_DMA_SAFE)
515		kfree(msg[0].buf);
516	if (msg[1].flags & I2C_M_DMA_SAFE)
517		kfree(msg[1].buf);
518
519	return status;
520}
521
522/**
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
531 *
532 * This executes an SMBus protocol operation, and returns a negative
533 * errno code else zero on success.
534 */
535s32 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)
538{
539	s32 res;
540
541	res = __i2c_lock_bus_helper(adapter);
542	if (res)
543		return res;
544
545	res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
546			       command, protocol, data);
547	i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
548
549	return res;
550}
551EXPORT_SYMBOL(i2c_smbus_xfer);
552
553s32 __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)
556{
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;
561	int try;
562	s32 res;
563
564	res = __i2c_check_suspended(adapter);
565	if (res)
566		return res;
567
568	/* If enabled, the following two tracepoints are conditional on
569	 * read_write and protocol.
570	 */
571	trace_smbus_write(adapter, addr, flags, read_write,
572			  command, protocol, data);
573	trace_smbus_read(adapter, addr, flags, read_write,
574			 command, protocol);
575
576	flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
577
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 */
584	}
585
586	if (xfer_func) {
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);
592			if (res != -EAGAIN)
593				break;
594			if (time_after(jiffies,
595				       orig_jiffies + adapter->timeout))
596				break;
597		}
598
599		if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
600			goto trace;
601		/*
602		 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
603		 * implement native support for the SMBus operation.
604		 */
605	}
606
607	res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
608				      command, protocol, data);
609
610trace:
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);
616
617	return res;
618}
619EXPORT_SYMBOL(__i2c_smbus_xfer);
620
621/**
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.
629 *
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.
633 *
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
638 * transfer.
639 */
640s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
641					      u8 command, u8 length, u8 *values)
642{
643	u8 i = 0;
644	int status;
645
646	if (length > I2C_SMBUS_BLOCK_MAX)
647		length = I2C_SMBUS_BLOCK_MAX;
648
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);
651
652	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
653		return -EOPNOTSUPP;
654
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);
658			if (status < 0)
659				return status;
660			values[i] = status & 0xff;
661			values[i + 1] = status >> 8;
662			i += 2;
663		}
664	}
665
666	while (i < length) {
667		status = i2c_smbus_read_byte_data(client, command + i);
668		if (status < 0)
669			return status;
670		values[i] = status;
671		i++;
672	}
673
674	return i;
675}
676EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
677
678/**
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
682 * Context: can sleep
683 *
684 * Setup handling of the SMBus alert protocol on a given I2C bus segment.
685 *
686 * Handling can be done either through our IRQ handler, or by the
687 * adapter (from its handler, periodic polling, or whatever).
688 *
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.
692 */
693struct i2c_client *i2c_new_smbus_alert_device(struct i2c_adapter *adapter,
694					      struct i2c_smbus_alert_setup *setup)
695{
696	struct i2c_board_info ara_board_info = {
697		I2C_BOARD_INFO("smbus_alert", 0x0c),
698		.platform_data = setup,
699	};
700
701	return i2c_new_client_device(adapter, &ara_board_info);
702}
703EXPORT_SYMBOL_GPL(i2c_new_smbus_alert_device);
704
705#if IS_ENABLED(CONFIG_I2C_SMBUS)
706int i2c_setup_smbus_alert(struct i2c_adapter *adapter)
707{
708	struct device *parent = adapter->dev.parent;
709	int irq;
710
711	/* Adapter instantiated without parent, skip the SMBus alert setup */
712	if (!parent)
 
713		return 0;
714
715	/* Report serious errors */
716	irq = device_property_match_string(parent, "interrupt-names", "smbus_alert");
717	if (irq < 0 && irq != -EINVAL && irq != -ENODATA)
718		return irq;
719
720	/* Skip setup when no irq was found */
721	if (irq < 0 && !device_property_present(parent, "smbalert-gpios"))
722		return 0;
723
724	return PTR_ERR_OR_ZERO(i2c_new_smbus_alert_device(adapter, NULL));
725}
 
726#endif
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Linux I2C core SMBus and SMBus emulation code
  4 *
  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.
  8 *
  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>
 12 */
 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>
 18
 19#include "i2c-core.h"
 20
 21#define CREATE_TRACE_POINTS
 22#include <trace/events/smbus.h>
 23
 24
 25/* The SMBus parts */
 26
 27#define POLY    (0x1070U << 3)
 28static u8 crc8(u16 data)
 29{
 30	int i;
 31
 32	for (i = 0; i < 8; i++) {
 33		if (data & 0x8000)
 34			data = data ^ POLY;
 35		data = data << 1;
 36	}
 37	return (u8)(data >> 8);
 38}
 39
 40/**
 41 * i2c_smbus_pec - Incremental CRC8 over the given input data array
 42 * @crc: previous return crc8 value
 43 * @p: pointer to data buffer.
 44 * @count: number of bytes in data buffer.
 45 *
 46 * Incremental CRC8 over count bytes in the array pointed to by p
 47 */
 48u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
 49{
 50	int i;
 51
 52	for (i = 0; i < count; i++)
 53		crc = crc8((crc ^ p[i]) << 8);
 54	return crc;
 55}
 56EXPORT_SYMBOL(i2c_smbus_pec);
 57
 58/* Assume a 7-bit address, which is reasonable for SMBus */
 59static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
 60{
 61	/* The address will be sent first */
 62	u8 addr = i2c_8bit_addr_from_msg(msg);
 63	pec = i2c_smbus_pec(pec, &addr, 1);
 64
 65	/* The data buffer follows */
 66	return i2c_smbus_pec(pec, msg->buf, msg->len);
 67}
 68
 69/* Used for write only transactions */
 70static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
 71{
 72	msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
 73	msg->len++;
 74}
 75
 76/* Return <0 on CRC error
 77   If there was a write before this read (most cases) we need to take the
 78   partial CRC from the write part into account.
 79   Note that this function does modify the message (we need to decrease the
 80   message length to hide the CRC byte from the caller). */
 81static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
 82{
 83	u8 rpec = msg->buf[--msg->len];
 84	cpec = i2c_smbus_msg_pec(cpec, msg);
 85
 86	if (rpec != cpec) {
 87		pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
 88			rpec, cpec);
 89		return -EBADMSG;
 90	}
 91	return 0;
 92}
 93
 94/**
 95 * i2c_smbus_read_byte - SMBus "receive byte" protocol
 96 * @client: Handle to slave device
 97 *
 98 * This executes the SMBus "receive byte" protocol, returning negative errno
 99 * else the byte received from the device.
100 */
101s32 i2c_smbus_read_byte(const struct i2c_client *client)
102{
103	union i2c_smbus_data data;
104	int status;
105
106	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
107				I2C_SMBUS_READ, 0,
108				I2C_SMBUS_BYTE, &data);
109	return (status < 0) ? status : data.byte;
110}
111EXPORT_SYMBOL(i2c_smbus_read_byte);
112
113/**
114 * i2c_smbus_write_byte - SMBus "send byte" protocol
115 * @client: Handle to slave device
116 * @value: Byte to be sent
117 *
118 * This executes the SMBus "send byte" protocol, returning negative errno
119 * else zero on success.
120 */
121s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
122{
123	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
124	                      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
125}
126EXPORT_SYMBOL(i2c_smbus_write_byte);
127
128/**
129 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
130 * @client: Handle to slave device
131 * @command: Byte interpreted by slave
132 *
133 * This executes the SMBus "read byte" protocol, returning negative errno
134 * else a data byte received from the device.
135 */
136s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
137{
138	union i2c_smbus_data data;
139	int status;
140
141	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
142				I2C_SMBUS_READ, command,
143				I2C_SMBUS_BYTE_DATA, &data);
144	return (status < 0) ? status : data.byte;
145}
146EXPORT_SYMBOL(i2c_smbus_read_byte_data);
147
148/**
149 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
150 * @client: Handle to slave device
151 * @command: Byte interpreted by slave
152 * @value: Byte being written
153 *
154 * This executes the SMBus "write byte" protocol, returning negative errno
155 * else zero on success.
156 */
157s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
158			      u8 value)
159{
160	union i2c_smbus_data data;
161	data.byte = value;
162	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
163			      I2C_SMBUS_WRITE, command,
164			      I2C_SMBUS_BYTE_DATA, &data);
165}
166EXPORT_SYMBOL(i2c_smbus_write_byte_data);
167
168/**
169 * i2c_smbus_read_word_data - SMBus "read word" protocol
170 * @client: Handle to slave device
171 * @command: Byte interpreted by slave
172 *
173 * This executes the SMBus "read word" protocol, returning negative errno
174 * else a 16-bit unsigned "word" received from the device.
175 */
176s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
177{
178	union i2c_smbus_data data;
179	int status;
180
181	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
182				I2C_SMBUS_READ, command,
183				I2C_SMBUS_WORD_DATA, &data);
184	return (status < 0) ? status : data.word;
185}
186EXPORT_SYMBOL(i2c_smbus_read_word_data);
187
188/**
189 * i2c_smbus_write_word_data - SMBus "write word" protocol
190 * @client: Handle to slave device
191 * @command: Byte interpreted by slave
192 * @value: 16-bit "word" being written
193 *
194 * This executes the SMBus "write word" protocol, returning negative errno
195 * else zero on success.
196 */
197s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
198			      u16 value)
199{
200	union i2c_smbus_data data;
201	data.word = value;
202	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
203			      I2C_SMBUS_WRITE, command,
204			      I2C_SMBUS_WORD_DATA, &data);
205}
206EXPORT_SYMBOL(i2c_smbus_write_word_data);
207
208/**
209 * i2c_smbus_read_block_data - SMBus "block read" protocol
210 * @client: Handle to slave device
211 * @command: Byte interpreted by slave
212 * @values: Byte array into which data will be read; big enough to hold
213 *	the data returned by the slave.  SMBus allows at most 32 bytes.
214 *
215 * This executes the SMBus "block read" protocol, returning negative errno
216 * else the number of data bytes in the slave's response.
217 *
218 * Note that using this function requires that the client's adapter support
219 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality.  Not all adapter drivers
220 * support this; its emulation through I2C messaging relies on a specific
221 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
222 */
223s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
224			      u8 *values)
225{
226	union i2c_smbus_data data;
227	int status;
228
229	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
230				I2C_SMBUS_READ, command,
231				I2C_SMBUS_BLOCK_DATA, &data);
232	if (status)
233		return status;
234
235	memcpy(values, &data.block[1], data.block[0]);
236	return data.block[0];
237}
238EXPORT_SYMBOL(i2c_smbus_read_block_data);
239
240/**
241 * i2c_smbus_write_block_data - SMBus "block write" protocol
242 * @client: Handle to slave device
243 * @command: Byte interpreted by slave
244 * @length: Size of data block; SMBus allows at most 32 bytes
245 * @values: Byte array which will be written.
246 *
247 * This executes the SMBus "block write" protocol, returning negative errno
248 * else zero on success.
249 */
250s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
251			       u8 length, const u8 *values)
252{
253	union i2c_smbus_data data;
254
255	if (length > I2C_SMBUS_BLOCK_MAX)
256		length = I2C_SMBUS_BLOCK_MAX;
257	data.block[0] = length;
258	memcpy(&data.block[1], values, length);
259	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
260			      I2C_SMBUS_WRITE, command,
261			      I2C_SMBUS_BLOCK_DATA, &data);
262}
263EXPORT_SYMBOL(i2c_smbus_write_block_data);
264
265/* Returns the number of read bytes */
266s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
267				  u8 length, u8 *values)
268{
269	union i2c_smbus_data data;
270	int status;
271
272	if (length > I2C_SMBUS_BLOCK_MAX)
273		length = I2C_SMBUS_BLOCK_MAX;
274	data.block[0] = length;
275	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
276				I2C_SMBUS_READ, command,
277				I2C_SMBUS_I2C_BLOCK_DATA, &data);
278	if (status < 0)
279		return status;
280
281	memcpy(values, &data.block[1], data.block[0]);
282	return data.block[0];
283}
284EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
285
286s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
287				   u8 length, const u8 *values)
288{
289	union i2c_smbus_data data;
290
291	if (length > I2C_SMBUS_BLOCK_MAX)
292		length = I2C_SMBUS_BLOCK_MAX;
293	data.block[0] = length;
294	memcpy(data.block + 1, values, length);
295	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
296			      I2C_SMBUS_WRITE, command,
297			      I2C_SMBUS_I2C_BLOCK_DATA, &data);
298}
299EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
300
301static void i2c_smbus_try_get_dmabuf(struct i2c_msg *msg, u8 init_val)
302{
303	bool is_read = msg->flags & I2C_M_RD;
304	unsigned char *dma_buf;
305
306	dma_buf = kzalloc(I2C_SMBUS_BLOCK_MAX + (is_read ? 2 : 3), GFP_KERNEL);
307	if (!dma_buf)
308		return;
309
310	msg->buf = dma_buf;
311	msg->flags |= I2C_M_DMA_SAFE;
312
313	if (init_val)
314		msg->buf[0] = init_val;
315}
316
317/*
318 * Simulate a SMBus command using the I2C protocol.
319 * No checking of parameters is done!
320 */
321static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
322				   unsigned short flags,
323				   char read_write, u8 command, int size,
324				   union i2c_smbus_data *data)
325{
326	/*
327	 * So we need to generate a series of msgs. In the case of writing, we
328	 * need to use only one message; when reading, we need two. We
329	 * initialize most things with sane defaults, to keep the code below
330	 * somewhat simpler.
331	 */
332	unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
333	unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
334	int nmsgs = read_write == I2C_SMBUS_READ ? 2 : 1;
335	u8 partial_pec = 0;
336	int status;
337	struct i2c_msg msg[2] = {
338		{
339			.addr = addr,
340			.flags = flags,
341			.len = 1,
342			.buf = msgbuf0,
343		}, {
344			.addr = addr,
345			.flags = flags | I2C_M_RD,
346			.len = 0,
347			.buf = msgbuf1,
348		},
349	};
350	bool wants_pec = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
351			  && size != I2C_SMBUS_I2C_BLOCK_DATA);
352
353	msgbuf0[0] = command;
354	switch (size) {
355	case I2C_SMBUS_QUICK:
356		msg[0].len = 0;
357		/* Special case: The read/write field is used as data */
358		msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
359					I2C_M_RD : 0);
360		nmsgs = 1;
361		break;
362	case I2C_SMBUS_BYTE:
363		if (read_write == I2C_SMBUS_READ) {
364			/* Special case: only a read! */
365			msg[0].flags = I2C_M_RD | flags;
366			nmsgs = 1;
367		}
368		break;
369	case I2C_SMBUS_BYTE_DATA:
370		if (read_write == I2C_SMBUS_READ)
371			msg[1].len = 1;
372		else {
373			msg[0].len = 2;
374			msgbuf0[1] = data->byte;
375		}
376		break;
377	case I2C_SMBUS_WORD_DATA:
378		if (read_write == I2C_SMBUS_READ)
379			msg[1].len = 2;
380		else {
381			msg[0].len = 3;
382			msgbuf0[1] = data->word & 0xff;
383			msgbuf0[2] = data->word >> 8;
384		}
385		break;
386	case I2C_SMBUS_PROC_CALL:
387		nmsgs = 2; /* Special case */
388		read_write = I2C_SMBUS_READ;
389		msg[0].len = 3;
390		msg[1].len = 2;
391		msgbuf0[1] = data->word & 0xff;
392		msgbuf0[2] = data->word >> 8;
393		break;
394	case I2C_SMBUS_BLOCK_DATA:
395		if (read_write == I2C_SMBUS_READ) {
396			msg[1].flags |= I2C_M_RECV_LEN;
397			msg[1].len = 1; /* block length will be added by
398					   the underlying bus driver */
399			i2c_smbus_try_get_dmabuf(&msg[1], 0);
400		} else {
401			msg[0].len = data->block[0] + 2;
402			if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
403				dev_err(&adapter->dev,
404					"Invalid block write size %d\n",
405					data->block[0]);
406				return -EINVAL;
407			}
408
409			i2c_smbus_try_get_dmabuf(&msg[0], command);
410			memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
411		}
412		break;
413	case I2C_SMBUS_BLOCK_PROC_CALL:
414		nmsgs = 2; /* Another special case */
415		read_write = I2C_SMBUS_READ;
416		if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
417			dev_err(&adapter->dev,
418				"Invalid block write size %d\n",
419				data->block[0]);
420			return -EINVAL;
421		}
422
423		msg[0].len = data->block[0] + 2;
424		i2c_smbus_try_get_dmabuf(&msg[0], command);
425		memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
426
427		msg[1].flags |= I2C_M_RECV_LEN;
428		msg[1].len = 1; /* block length will be added by
429				   the underlying bus driver */
430		i2c_smbus_try_get_dmabuf(&msg[1], 0);
431		break;
432	case I2C_SMBUS_I2C_BLOCK_DATA:
433		if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
434			dev_err(&adapter->dev, "Invalid block %s size %d\n",
435				read_write == I2C_SMBUS_READ ? "read" : "write",
436				data->block[0]);
437			return -EINVAL;
438		}
439
440		if (read_write == I2C_SMBUS_READ) {
441			msg[1].len = data->block[0];
442			i2c_smbus_try_get_dmabuf(&msg[1], 0);
443		} else {
444			msg[0].len = data->block[0] + 1;
445
446			i2c_smbus_try_get_dmabuf(&msg[0], command);
447			memcpy(msg[0].buf + 1, data->block + 1, data->block[0]);
448		}
449		break;
450	default:
451		dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
452		return -EOPNOTSUPP;
453	}
454
455	if (wants_pec) {
456		/* Compute PEC if first message is a write */
457		if (!(msg[0].flags & I2C_M_RD)) {
458			if (nmsgs == 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]);
462		}
463		/* Ask for PEC if last message is a read */
464		if (msg[nmsgs - 1].flags & I2C_M_RD)
465			msg[nmsgs - 1].len++;
466	}
467
468	status = __i2c_transfer(adapter, msg, nmsgs);
469	if (status < 0)
470		goto cleanup;
471	if (status != nmsgs) {
472		status = -EIO;
473		goto cleanup;
474	}
475	status = 0;
476
477	/* Check PEC if last message is a read */
478	if (wants_pec && (msg[nmsgs - 1].flags & I2C_M_RD)) {
479		status = i2c_smbus_check_pec(partial_pec, &msg[nmsgs - 1]);
480		if (status < 0)
481			goto cleanup;
482	}
483
484	if (read_write == I2C_SMBUS_READ)
485		switch (size) {
486		case I2C_SMBUS_BYTE:
487			data->byte = msgbuf0[0];
488			break;
489		case I2C_SMBUS_BYTE_DATA:
490			data->byte = msgbuf1[0];
491			break;
492		case I2C_SMBUS_WORD_DATA:
493		case I2C_SMBUS_PROC_CALL:
494			data->word = msgbuf1[0] | (msgbuf1[1] << 8);
495			break;
496		case I2C_SMBUS_I2C_BLOCK_DATA:
497			memcpy(data->block + 1, msg[1].buf, data->block[0]);
498			break;
499		case I2C_SMBUS_BLOCK_DATA:
500		case I2C_SMBUS_BLOCK_PROC_CALL:
501			if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
502				dev_err(&adapter->dev,
503					"Invalid block size returned: %d\n",
504					msg[1].buf[0]);
505				status = -EPROTO;
506				goto cleanup;
507			}
508			memcpy(data->block, msg[1].buf, msg[1].buf[0] + 1);
509			break;
510		}
511
512cleanup:
513	if (msg[0].flags & I2C_M_DMA_SAFE)
514		kfree(msg[0].buf);
515	if (msg[1].flags & I2C_M_DMA_SAFE)
516		kfree(msg[1].buf);
517
518	return status;
519}
520
521/**
522 * i2c_smbus_xfer - execute SMBus protocol operations
523 * @adapter: Handle to I2C bus
524 * @addr: Address of SMBus slave on that bus
525 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
526 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
527 * @command: Byte interpreted by slave, for protocols which use such bytes
528 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
529 * @data: Data to be read or written
530 *
531 * This executes an SMBus protocol operation, and returns a negative
532 * errno code else zero on success.
533 */
534s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
535		   unsigned short flags, char read_write,
536		   u8 command, int protocol, union i2c_smbus_data *data)
537{
538	s32 res;
539
540	res = __i2c_lock_bus_helper(adapter);
541	if (res)
542		return res;
543
544	res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
545			       command, protocol, data);
546	i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
547
548	return res;
549}
550EXPORT_SYMBOL(i2c_smbus_xfer);
551
552s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
553		     unsigned short flags, char read_write,
554		     u8 command, int protocol, union i2c_smbus_data *data)
555{
556	int (*xfer_func)(struct i2c_adapter *adap, u16 addr,
557			 unsigned short flags, char read_write,
558			 u8 command, int size, union i2c_smbus_data *data);
559	unsigned long orig_jiffies;
560	int try;
561	s32 res;
562
563	res = __i2c_check_suspended(adapter);
564	if (res)
565		return res;
566
567	/* If enabled, the following two tracepoints are conditional on
568	 * read_write and protocol.
569	 */
570	trace_smbus_write(adapter, addr, flags, read_write,
571			  command, protocol, data);
572	trace_smbus_read(adapter, addr, flags, read_write,
573			 command, protocol);
574
575	flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
576
577	xfer_func = adapter->algo->smbus_xfer;
578	if (i2c_in_atomic_xfer_mode()) {
579		if (adapter->algo->smbus_xfer_atomic)
580			xfer_func = adapter->algo->smbus_xfer_atomic;
581		else if (adapter->algo->master_xfer_atomic)
582			xfer_func = NULL; /* fallback to I2C emulation */
583	}
584
585	if (xfer_func) {
586		/* Retry automatically on arbitration loss */
587		orig_jiffies = jiffies;
588		for (res = 0, try = 0; try <= adapter->retries; try++) {
589			res = xfer_func(adapter, addr, flags, read_write,
590					command, protocol, data);
591			if (res != -EAGAIN)
592				break;
593			if (time_after(jiffies,
594				       orig_jiffies + adapter->timeout))
595				break;
596		}
597
598		if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
599			goto trace;
600		/*
601		 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
602		 * implement native support for the SMBus operation.
603		 */
604	}
605
606	res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
607				      command, protocol, data);
608
609trace:
610	/* If enabled, the reply tracepoint is conditional on read_write. */
611	trace_smbus_reply(adapter, addr, flags, read_write,
612			  command, protocol, data, res);
613	trace_smbus_result(adapter, addr, flags, read_write,
614			   command, protocol, res);
615
616	return res;
617}
618EXPORT_SYMBOL(__i2c_smbus_xfer);
619
620/**
621 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
622 * @client: Handle to slave device
623 * @command: Byte interpreted by slave
624 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
625 * @values: Byte array into which data will be read; big enough to hold
626 *	the data returned by the slave.  SMBus allows at most
627 *	I2C_SMBUS_BLOCK_MAX bytes.
628 *
629 * This executes the SMBus "block read" protocol if supported by the adapter.
630 * If block read is not supported, it emulates it using either word or byte
631 * read protocols depending on availability.
632 *
633 * The addresses of the I2C slave device that are accessed with this function
634 * must be mapped to a linear region, so that a block read will have the same
635 * effect as a byte read. Before using this function you must double-check
636 * if the I2C slave does support exchanging a block transfer with a byte
637 * transfer.
638 */
639s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
640					      u8 command, u8 length, u8 *values)
641{
642	u8 i = 0;
643	int status;
644
645	if (length > I2C_SMBUS_BLOCK_MAX)
646		length = I2C_SMBUS_BLOCK_MAX;
647
648	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
649		return i2c_smbus_read_i2c_block_data(client, command, length, values);
650
651	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
652		return -EOPNOTSUPP;
653
654	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
655		while ((i + 2) <= length) {
656			status = i2c_smbus_read_word_data(client, command + i);
657			if (status < 0)
658				return status;
659			values[i] = status & 0xff;
660			values[i + 1] = status >> 8;
661			i += 2;
662		}
663	}
664
665	while (i < length) {
666		status = i2c_smbus_read_byte_data(client, command + i);
667		if (status < 0)
668			return status;
669		values[i] = status;
670		i++;
671	}
672
673	return i;
674}
675EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
676
677/**
678 * i2c_new_smbus_alert_device - get ara client for SMBus alert support
679 * @adapter: the target adapter
680 * @setup: setup data for the SMBus alert handler
681 * Context: can sleep
682 *
683 * Setup handling of the SMBus alert protocol on a given I2C bus segment.
684 *
685 * Handling can be done either through our IRQ handler, or by the
686 * adapter (from its handler, periodic polling, or whatever).
687 *
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 an
690 * ERRPTR to indicate an error.
691 */
692struct i2c_client *i2c_new_smbus_alert_device(struct i2c_adapter *adapter,
693					      struct i2c_smbus_alert_setup *setup)
694{
695	struct i2c_board_info ara_board_info = {
696		I2C_BOARD_INFO("smbus_alert", 0x0c),
697		.platform_data = setup,
698	};
699
700	return i2c_new_client_device(adapter, &ara_board_info);
701}
702EXPORT_SYMBOL_GPL(i2c_new_smbus_alert_device);
703
704#if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_OF)
705int of_i2c_setup_smbus_alert(struct i2c_adapter *adapter)
706{
 
707	int irq;
708
709	irq = of_property_match_string(adapter->dev.of_node, "interrupt-names",
710				       "smbus_alert");
711	if (irq == -EINVAL || irq == -ENODATA)
712		return 0;
713	else if (irq < 0)
 
 
 
714		return irq;
715
 
 
 
 
716	return PTR_ERR_OR_ZERO(i2c_new_smbus_alert_device(adapter, NULL));
717}
718EXPORT_SYMBOL_GPL(of_i2c_setup_smbus_alert);
719#endif