xref: /openbmc/linux/drivers/i2c/i2c-core-smbus.c (revision a977d045)
1 /*
2  * Linux I2C core SMBus and SMBus emulation code
3  *
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.
7  *
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>
11  *
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)
15  * any later version.
16  */
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/i2c.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)
28 static 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 /* 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)
42 {
43 	int i;
44 
45 	for (i = 0; i < count; i++)
46 		crc = crc8((crc ^ p[i]) << 8);
47 	return crc;
48 }
49 
50 /* Assume a 7-bit address, which is reasonable for SMBus */
51 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
52 {
53 	/* The address will be sent first */
54 	u8 addr = i2c_8bit_addr_from_msg(msg);
55 	pec = i2c_smbus_pec(pec, &addr, 1);
56 
57 	/* The data buffer follows */
58 	return i2c_smbus_pec(pec, msg->buf, msg->len);
59 }
60 
61 /* Used for write only transactions */
62 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
63 {
64 	msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
65 	msg->len++;
66 }
67 
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)
74 {
75 	u8 rpec = msg->buf[--msg->len];
76 	cpec = i2c_smbus_msg_pec(cpec, msg);
77 
78 	if (rpec != cpec) {
79 		pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
80 			rpec, cpec);
81 		return -EBADMSG;
82 	}
83 	return 0;
84 }
85 
86 /**
87  * i2c_smbus_read_byte - SMBus "receive byte" protocol
88  * @client: Handle to slave device
89  *
90  * This executes the SMBus "receive byte" protocol, returning negative errno
91  * else the byte received from the device.
92  */
93 s32 i2c_smbus_read_byte(const struct i2c_client *client)
94 {
95 	union i2c_smbus_data data;
96 	int status;
97 
98 	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
99 				I2C_SMBUS_READ, 0,
100 				I2C_SMBUS_BYTE, &data);
101 	return (status < 0) ? status : data.byte;
102 }
103 EXPORT_SYMBOL(i2c_smbus_read_byte);
104 
105 /**
106  * i2c_smbus_write_byte - SMBus "send byte" protocol
107  * @client: Handle to slave device
108  * @value: Byte to be sent
109  *
110  * This executes the SMBus "send byte" protocol, returning negative errno
111  * else zero on success.
112  */
113 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
114 {
115 	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
116 	                      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
117 }
118 EXPORT_SYMBOL(i2c_smbus_write_byte);
119 
120 /**
121  * i2c_smbus_read_byte_data - SMBus "read byte" protocol
122  * @client: Handle to slave device
123  * @command: Byte interpreted by slave
124  *
125  * This executes the SMBus "read byte" protocol, returning negative errno
126  * else a data byte received from the device.
127  */
128 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
129 {
130 	union i2c_smbus_data data;
131 	int status;
132 
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;
137 }
138 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
139 
140 /**
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
145  *
146  * This executes the SMBus "write byte" protocol, returning negative errno
147  * else zero on success.
148  */
149 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
150 			      u8 value)
151 {
152 	union i2c_smbus_data data;
153 	data.byte = value;
154 	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
155 			      I2C_SMBUS_WRITE, command,
156 			      I2C_SMBUS_BYTE_DATA, &data);
157 }
158 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
159 
160 /**
161  * i2c_smbus_read_word_data - SMBus "read word" protocol
162  * @client: Handle to slave device
163  * @command: Byte interpreted by slave
164  *
165  * This executes the SMBus "read word" protocol, returning negative errno
166  * else a 16-bit unsigned "word" received from the device.
167  */
168 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
169 {
170 	union i2c_smbus_data data;
171 	int status;
172 
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;
177 }
178 EXPORT_SYMBOL(i2c_smbus_read_word_data);
179 
180 /**
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
185  *
186  * This executes the SMBus "write word" protocol, returning negative errno
187  * else zero on success.
188  */
189 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
190 			      u16 value)
191 {
192 	union i2c_smbus_data data;
193 	data.word = value;
194 	return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
195 			      I2C_SMBUS_WRITE, command,
196 			      I2C_SMBUS_WORD_DATA, &data);
197 }
198 EXPORT_SYMBOL(i2c_smbus_write_word_data);
199 
200 /**
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.
206  *
207  * This executes the SMBus "block read" protocol, returning negative errno
208  * else the number of data bytes in the slave's response.
209  *
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.
214  */
215 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
216 			      u8 *values)
217 {
218 	union i2c_smbus_data data;
219 	int status;
220 
221 	status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
222 				I2C_SMBUS_READ, command,
223 				I2C_SMBUS_BLOCK_DATA, &data);
224 	if (status)
225 		return status;
226 
227 	memcpy(values, &data.block[1], data.block[0]);
228 	return data.block[0];
229 }
230 EXPORT_SYMBOL(i2c_smbus_read_block_data);
231 
232 /**
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.
238  *
239  * This executes the SMBus "block write" protocol, returning negative errno
240  * else zero on success.
241  */
242 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
243 			       u8 length, const u8 *values)
244 {
245 	union i2c_smbus_data data;
246 
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);
254 }
255 EXPORT_SYMBOL(i2c_smbus_write_block_data);
256 
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)
260 {
261 	union i2c_smbus_data data;
262 	int status;
263 
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);
270 	if (status < 0)
271 		return status;
272 
273 	memcpy(values, &data.block[1], data.block[0]);
274 	return data.block[0];
275 }
276 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
277 
278 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
279 				   u8 length, const u8 *values)
280 {
281 	union i2c_smbus_data data;
282 
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);
290 }
291 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
292 
293 /* Simulate a SMBus command using the i2c protocol
294    No checking of parameters is done!  */
295 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
296 				   unsigned short flags,
297 				   char read_write, u8 command, int size,
298 				   union i2c_smbus_data *data)
299 {
300 	/* So we need to generate a series of msgs. In the case of writing, we
301 	  need to use only one message; when reading, we need two. We initialize
302 	  most things with sane defaults, to keep the code below somewhat
303 	  simpler. */
304 	unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
305 	unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
306 	int num = read_write == I2C_SMBUS_READ ? 2 : 1;
307 	int i;
308 	u8 partial_pec = 0;
309 	int status;
310 	struct i2c_msg msg[2] = {
311 		{
312 			.addr = addr,
313 			.flags = flags,
314 			.len = 1,
315 			.buf = msgbuf0,
316 		}, {
317 			.addr = addr,
318 			.flags = flags | I2C_M_RD,
319 			.len = 0,
320 			.buf = msgbuf1,
321 		},
322 	};
323 
324 	msgbuf0[0] = command;
325 	switch (size) {
326 	case I2C_SMBUS_QUICK:
327 		msg[0].len = 0;
328 		/* Special case: The read/write field is used as data */
329 		msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
330 					I2C_M_RD : 0);
331 		num = 1;
332 		break;
333 	case I2C_SMBUS_BYTE:
334 		if (read_write == I2C_SMBUS_READ) {
335 			/* Special case: only a read! */
336 			msg[0].flags = I2C_M_RD | flags;
337 			num = 1;
338 		}
339 		break;
340 	case I2C_SMBUS_BYTE_DATA:
341 		if (read_write == I2C_SMBUS_READ)
342 			msg[1].len = 1;
343 		else {
344 			msg[0].len = 2;
345 			msgbuf0[1] = data->byte;
346 		}
347 		break;
348 	case I2C_SMBUS_WORD_DATA:
349 		if (read_write == I2C_SMBUS_READ)
350 			msg[1].len = 2;
351 		else {
352 			msg[0].len = 3;
353 			msgbuf0[1] = data->word & 0xff;
354 			msgbuf0[2] = data->word >> 8;
355 		}
356 		break;
357 	case I2C_SMBUS_PROC_CALL:
358 		num = 2; /* Special case */
359 		read_write = I2C_SMBUS_READ;
360 		msg[0].len = 3;
361 		msg[1].len = 2;
362 		msgbuf0[1] = data->word & 0xff;
363 		msgbuf0[2] = data->word >> 8;
364 		break;
365 	case I2C_SMBUS_BLOCK_DATA:
366 		if (read_write == I2C_SMBUS_READ) {
367 			msg[1].flags |= I2C_M_RECV_LEN;
368 			msg[1].len = 1; /* block length will be added by
369 					   the underlying bus driver */
370 		} else {
371 			msg[0].len = data->block[0] + 2;
372 			if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
373 				dev_err(&adapter->dev,
374 					"Invalid block write size %d\n",
375 					data->block[0]);
376 				return -EINVAL;
377 			}
378 			for (i = 1; i < msg[0].len; i++)
379 				msgbuf0[i] = data->block[i-1];
380 		}
381 		break;
382 	case I2C_SMBUS_BLOCK_PROC_CALL:
383 		num = 2; /* Another special case */
384 		read_write = I2C_SMBUS_READ;
385 		if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
386 			dev_err(&adapter->dev,
387 				"Invalid block write size %d\n",
388 				data->block[0]);
389 			return -EINVAL;
390 		}
391 		msg[0].len = data->block[0] + 2;
392 		for (i = 1; i < msg[0].len; i++)
393 			msgbuf0[i] = data->block[i-1];
394 		msg[1].flags |= I2C_M_RECV_LEN;
395 		msg[1].len = 1; /* block length will be added by
396 				   the underlying bus driver */
397 		break;
398 	case I2C_SMBUS_I2C_BLOCK_DATA:
399 		if (read_write == I2C_SMBUS_READ) {
400 			msg[1].len = data->block[0];
401 		} else {
402 			msg[0].len = data->block[0] + 1;
403 			if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
404 				dev_err(&adapter->dev,
405 					"Invalid block write size %d\n",
406 					data->block[0]);
407 				return -EINVAL;
408 			}
409 			for (i = 1; i <= data->block[0]; i++)
410 				msgbuf0[i] = data->block[i];
411 		}
412 		break;
413 	default:
414 		dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
415 		return -EOPNOTSUPP;
416 	}
417 
418 	i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
419 				      && size != I2C_SMBUS_I2C_BLOCK_DATA);
420 	if (i) {
421 		/* Compute PEC if first message is a write */
422 		if (!(msg[0].flags & I2C_M_RD)) {
423 			if (num == 1) /* Write only */
424 				i2c_smbus_add_pec(&msg[0]);
425 			else /* Write followed by read */
426 				partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
427 		}
428 		/* Ask for PEC if last message is a read */
429 		if (msg[num-1].flags & I2C_M_RD)
430 			msg[num-1].len++;
431 	}
432 
433 	status = i2c_transfer(adapter, msg, num);
434 	if (status < 0)
435 		return status;
436 
437 	/* Check PEC if last message is a read */
438 	if (i && (msg[num-1].flags & I2C_M_RD)) {
439 		status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
440 		if (status < 0)
441 			return status;
442 	}
443 
444 	if (read_write == I2C_SMBUS_READ)
445 		switch (size) {
446 		case I2C_SMBUS_BYTE:
447 			data->byte = msgbuf0[0];
448 			break;
449 		case I2C_SMBUS_BYTE_DATA:
450 			data->byte = msgbuf1[0];
451 			break;
452 		case I2C_SMBUS_WORD_DATA:
453 		case I2C_SMBUS_PROC_CALL:
454 			data->word = msgbuf1[0] | (msgbuf1[1] << 8);
455 			break;
456 		case I2C_SMBUS_I2C_BLOCK_DATA:
457 			for (i = 0; i < data->block[0]; i++)
458 				data->block[i+1] = msgbuf1[i];
459 			break;
460 		case I2C_SMBUS_BLOCK_DATA:
461 		case I2C_SMBUS_BLOCK_PROC_CALL:
462 			for (i = 0; i < msgbuf1[0] + 1; i++)
463 				data->block[i] = msgbuf1[i];
464 			break;
465 		}
466 	return 0;
467 }
468 
469 /**
470  * i2c_smbus_xfer - execute SMBus protocol operations
471  * @adapter: Handle to I2C bus
472  * @addr: Address of SMBus slave on that bus
473  * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
474  * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
475  * @command: Byte interpreted by slave, for protocols which use such bytes
476  * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
477  * @data: Data to be read or written
478  *
479  * This executes an SMBus protocol operation, and returns a negative
480  * errno code else zero on success.
481  */
482 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
483 		   char read_write, u8 command, int protocol,
484 		   union i2c_smbus_data *data)
485 {
486 	unsigned long orig_jiffies;
487 	int try;
488 	s32 res;
489 
490 	/* If enabled, the following two tracepoints are conditional on
491 	 * read_write and protocol.
492 	 */
493 	trace_smbus_write(adapter, addr, flags, read_write,
494 			  command, protocol, data);
495 	trace_smbus_read(adapter, addr, flags, read_write,
496 			 command, protocol);
497 
498 	flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
499 
500 	if (adapter->algo->smbus_xfer) {
501 		i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
502 
503 		/* Retry automatically on arbitration loss */
504 		orig_jiffies = jiffies;
505 		for (res = 0, try = 0; try <= adapter->retries; try++) {
506 			res = adapter->algo->smbus_xfer(adapter, addr, flags,
507 							read_write, command,
508 							protocol, data);
509 			if (res != -EAGAIN)
510 				break;
511 			if (time_after(jiffies,
512 				       orig_jiffies + adapter->timeout))
513 				break;
514 		}
515 		i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
516 
517 		if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
518 			goto trace;
519 		/*
520 		 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
521 		 * implement native support for the SMBus operation.
522 		 */
523 	}
524 
525 	res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
526 				      command, protocol, data);
527 
528 trace:
529 	/* If enabled, the reply tracepoint is conditional on read_write. */
530 	trace_smbus_reply(adapter, addr, flags, read_write,
531 			  command, protocol, data);
532 	trace_smbus_result(adapter, addr, flags, read_write,
533 			   command, protocol, res);
534 
535 	return res;
536 }
537 EXPORT_SYMBOL(i2c_smbus_xfer);
538 
539 /**
540  * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
541  * @client: Handle to slave device
542  * @command: Byte interpreted by slave
543  * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
544  * @values: Byte array into which data will be read; big enough to hold
545  *	the data returned by the slave.  SMBus allows at most
546  *	I2C_SMBUS_BLOCK_MAX bytes.
547  *
548  * This executes the SMBus "block read" protocol if supported by the adapter.
549  * If block read is not supported, it emulates it using either word or byte
550  * read protocols depending on availability.
551  *
552  * The addresses of the I2C slave device that are accessed with this function
553  * must be mapped to a linear region, so that a block read will have the same
554  * effect as a byte read. Before using this function you must double-check
555  * if the I2C slave does support exchanging a block transfer with a byte
556  * transfer.
557  */
558 s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
559 					      u8 command, u8 length, u8 *values)
560 {
561 	u8 i = 0;
562 	int status;
563 
564 	if (length > I2C_SMBUS_BLOCK_MAX)
565 		length = I2C_SMBUS_BLOCK_MAX;
566 
567 	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
568 		return i2c_smbus_read_i2c_block_data(client, command, length, values);
569 
570 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
571 		return -EOPNOTSUPP;
572 
573 	if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
574 		while ((i + 2) <= length) {
575 			status = i2c_smbus_read_word_data(client, command + i);
576 			if (status < 0)
577 				return status;
578 			values[i] = status & 0xff;
579 			values[i + 1] = status >> 8;
580 			i += 2;
581 		}
582 	}
583 
584 	while (i < length) {
585 		status = i2c_smbus_read_byte_data(client, command + i);
586 		if (status < 0)
587 			return status;
588 		values[i] = status;
589 		i++;
590 	}
591 
592 	return i;
593 }
594 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
595