btrfs: rename btrfs_device::scrub_device to scrub_ctx
[linux/fpc-iii.git] / drivers / i2c / i2c-core-smbus.c
bloba1082c04ac5c63c097ac5b0c2d33a7df741d2070
1 /*
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.
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>
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.
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/i2c.h>
20 #include <linux/i2c-smbus.h>
22 #define CREATE_TRACE_POINTS
23 #include <trace/events/smbus.h>
26 /* The SMBus parts */
28 #define POLY (0x1070U << 3)
29 static u8 crc8(u16 data)
31 int i;
33 for (i = 0; i < 8; i++) {
34 if (data & 0x8000)
35 data = data ^ POLY;
36 data = data << 1;
38 return (u8)(data >> 8);
41 /* Incremental CRC8 over count bytes in the array pointed to by p */
42 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
44 int i;
46 for (i = 0; i < count; i++)
47 crc = crc8((crc ^ p[i]) << 8);
48 return crc;
51 /* Assume a 7-bit address, which is reasonable for SMBus */
52 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
54 /* The address will be sent first */
55 u8 addr = i2c_8bit_addr_from_msg(msg);
56 pec = i2c_smbus_pec(pec, &addr, 1);
58 /* The data buffer follows */
59 return i2c_smbus_pec(pec, msg->buf, msg->len);
62 /* Used for write only transactions */
63 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
65 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
66 msg->len++;
69 /* Return <0 on CRC error
70 If there was a write before this read (most cases) we need to take the
71 partial CRC from the write part into account.
72 Note that this function does modify the message (we need to decrease the
73 message length to hide the CRC byte from the caller). */
74 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
76 u8 rpec = msg->buf[--msg->len];
77 cpec = i2c_smbus_msg_pec(cpec, msg);
79 if (rpec != cpec) {
80 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
81 rpec, cpec);
82 return -EBADMSG;
84 return 0;
87 /**
88 * i2c_smbus_read_byte - SMBus "receive byte" protocol
89 * @client: Handle to slave device
91 * This executes the SMBus "receive byte" protocol, returning negative errno
92 * else the byte received from the device.
94 s32 i2c_smbus_read_byte(const struct i2c_client *client)
96 union i2c_smbus_data data;
97 int status;
99 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
100 I2C_SMBUS_READ, 0,
101 I2C_SMBUS_BYTE, &data);
102 return (status < 0) ? status : data.byte;
104 EXPORT_SYMBOL(i2c_smbus_read_byte);
107 * i2c_smbus_write_byte - SMBus "send byte" protocol
108 * @client: Handle to slave device
109 * @value: Byte to be sent
111 * This executes the SMBus "send byte" protocol, returning negative errno
112 * else zero on success.
114 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
116 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
117 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
119 EXPORT_SYMBOL(i2c_smbus_write_byte);
122 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
123 * @client: Handle to slave device
124 * @command: Byte interpreted by slave
126 * This executes the SMBus "read byte" protocol, returning negative errno
127 * else a data byte received from the device.
129 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
131 union i2c_smbus_data data;
132 int status;
134 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
135 I2C_SMBUS_READ, command,
136 I2C_SMBUS_BYTE_DATA, &data);
137 return (status < 0) ? status : data.byte;
139 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
142 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
143 * @client: Handle to slave device
144 * @command: Byte interpreted by slave
145 * @value: Byte being written
147 * This executes the SMBus "write byte" protocol, returning negative errno
148 * else zero on success.
150 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
151 u8 value)
153 union i2c_smbus_data data;
154 data.byte = value;
155 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
156 I2C_SMBUS_WRITE, command,
157 I2C_SMBUS_BYTE_DATA, &data);
159 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
162 * i2c_smbus_read_word_data - SMBus "read word" protocol
163 * @client: Handle to slave device
164 * @command: Byte interpreted by slave
166 * This executes the SMBus "read word" protocol, returning negative errno
167 * else a 16-bit unsigned "word" received from the device.
169 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
171 union i2c_smbus_data data;
172 int status;
174 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
175 I2C_SMBUS_READ, command,
176 I2C_SMBUS_WORD_DATA, &data);
177 return (status < 0) ? status : data.word;
179 EXPORT_SYMBOL(i2c_smbus_read_word_data);
182 * i2c_smbus_write_word_data - SMBus "write word" protocol
183 * @client: Handle to slave device
184 * @command: Byte interpreted by slave
185 * @value: 16-bit "word" being written
187 * This executes the SMBus "write word" protocol, returning negative errno
188 * else zero on success.
190 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
191 u16 value)
193 union i2c_smbus_data data;
194 data.word = value;
195 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
196 I2C_SMBUS_WRITE, command,
197 I2C_SMBUS_WORD_DATA, &data);
199 EXPORT_SYMBOL(i2c_smbus_write_word_data);
202 * i2c_smbus_read_block_data - SMBus "block read" protocol
203 * @client: Handle to slave device
204 * @command: Byte interpreted by slave
205 * @values: Byte array into which data will be read; big enough to hold
206 * the data returned by the slave. SMBus allows at most 32 bytes.
208 * This executes the SMBus "block read" protocol, returning negative errno
209 * else the number of data bytes in the slave's response.
211 * Note that using this function requires that the client's adapter support
212 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
213 * support this; its emulation through I2C messaging relies on a specific
214 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
216 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
217 u8 *values)
219 union i2c_smbus_data data;
220 int status;
222 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
223 I2C_SMBUS_READ, command,
224 I2C_SMBUS_BLOCK_DATA, &data);
225 if (status)
226 return status;
228 memcpy(values, &data.block[1], data.block[0]);
229 return data.block[0];
231 EXPORT_SYMBOL(i2c_smbus_read_block_data);
234 * i2c_smbus_write_block_data - SMBus "block write" protocol
235 * @client: Handle to slave device
236 * @command: Byte interpreted by slave
237 * @length: Size of data block; SMBus allows at most 32 bytes
238 * @values: Byte array which will be written.
240 * This executes the SMBus "block write" protocol, returning negative errno
241 * else zero on success.
243 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
244 u8 length, const u8 *values)
246 union i2c_smbus_data data;
248 if (length > I2C_SMBUS_BLOCK_MAX)
249 length = I2C_SMBUS_BLOCK_MAX;
250 data.block[0] = length;
251 memcpy(&data.block[1], values, length);
252 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
253 I2C_SMBUS_WRITE, command,
254 I2C_SMBUS_BLOCK_DATA, &data);
256 EXPORT_SYMBOL(i2c_smbus_write_block_data);
258 /* Returns the number of read bytes */
259 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
260 u8 length, u8 *values)
262 union i2c_smbus_data data;
263 int status;
265 if (length > I2C_SMBUS_BLOCK_MAX)
266 length = I2C_SMBUS_BLOCK_MAX;
267 data.block[0] = length;
268 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
269 I2C_SMBUS_READ, command,
270 I2C_SMBUS_I2C_BLOCK_DATA, &data);
271 if (status < 0)
272 return status;
274 memcpy(values, &data.block[1], data.block[0]);
275 return data.block[0];
277 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
279 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
280 u8 length, const u8 *values)
282 union i2c_smbus_data data;
284 if (length > I2C_SMBUS_BLOCK_MAX)
285 length = I2C_SMBUS_BLOCK_MAX;
286 data.block[0] = length;
287 memcpy(data.block + 1, values, length);
288 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
289 I2C_SMBUS_WRITE, command,
290 I2C_SMBUS_I2C_BLOCK_DATA, &data);
292 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
294 /* Simulate a SMBus command using the i2c protocol
295 No checking of parameters is done! */
296 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
297 unsigned short flags,
298 char read_write, u8 command, int size,
299 union i2c_smbus_data *data)
301 /* So we need to generate a series of msgs. In the case of writing, we
302 need to use only one message; when reading, we need two. We initialize
303 most things with sane defaults, to keep the code below somewhat
304 simpler. */
305 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
306 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
307 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
308 int i;
309 u8 partial_pec = 0;
310 int status;
311 struct i2c_msg msg[2] = {
313 .addr = addr,
314 .flags = flags,
315 .len = 1,
316 .buf = msgbuf0,
317 }, {
318 .addr = addr,
319 .flags = flags | I2C_M_RD,
320 .len = 0,
321 .buf = msgbuf1,
325 msgbuf0[0] = command;
326 switch (size) {
327 case I2C_SMBUS_QUICK:
328 msg[0].len = 0;
329 /* Special case: The read/write field is used as data */
330 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
331 I2C_M_RD : 0);
332 num = 1;
333 break;
334 case I2C_SMBUS_BYTE:
335 if (read_write == I2C_SMBUS_READ) {
336 /* Special case: only a read! */
337 msg[0].flags = I2C_M_RD | flags;
338 num = 1;
340 break;
341 case I2C_SMBUS_BYTE_DATA:
342 if (read_write == I2C_SMBUS_READ)
343 msg[1].len = 1;
344 else {
345 msg[0].len = 2;
346 msgbuf0[1] = data->byte;
348 break;
349 case I2C_SMBUS_WORD_DATA:
350 if (read_write == I2C_SMBUS_READ)
351 msg[1].len = 2;
352 else {
353 msg[0].len = 3;
354 msgbuf0[1] = data->word & 0xff;
355 msgbuf0[2] = data->word >> 8;
357 break;
358 case I2C_SMBUS_PROC_CALL:
359 num = 2; /* Special case */
360 read_write = I2C_SMBUS_READ;
361 msg[0].len = 3;
362 msg[1].len = 2;
363 msgbuf0[1] = data->word & 0xff;
364 msgbuf0[2] = data->word >> 8;
365 break;
366 case I2C_SMBUS_BLOCK_DATA:
367 if (read_write == I2C_SMBUS_READ) {
368 msg[1].flags |= I2C_M_RECV_LEN;
369 msg[1].len = 1; /* block length will be added by
370 the underlying bus driver */
371 } else {
372 msg[0].len = data->block[0] + 2;
373 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
374 dev_err(&adapter->dev,
375 "Invalid block write size %d\n",
376 data->block[0]);
377 return -EINVAL;
379 for (i = 1; i < msg[0].len; i++)
380 msgbuf0[i] = data->block[i-1];
382 break;
383 case I2C_SMBUS_BLOCK_PROC_CALL:
384 num = 2; /* Another special case */
385 read_write = I2C_SMBUS_READ;
386 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
387 dev_err(&adapter->dev,
388 "Invalid block write size %d\n",
389 data->block[0]);
390 return -EINVAL;
392 msg[0].len = data->block[0] + 2;
393 for (i = 1; i < msg[0].len; i++)
394 msgbuf0[i] = data->block[i-1];
395 msg[1].flags |= I2C_M_RECV_LEN;
396 msg[1].len = 1; /* block length will be added by
397 the underlying bus driver */
398 break;
399 case I2C_SMBUS_I2C_BLOCK_DATA:
400 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
401 dev_err(&adapter->dev, "Invalid block %s size %d\n",
402 read_write == I2C_SMBUS_READ ? "read" : "write",
403 data->block[0]);
404 return -EINVAL;
407 if (read_write == I2C_SMBUS_READ) {
408 msg[1].len = data->block[0];
409 } else {
410 msg[0].len = data->block[0] + 1;
411 for (i = 1; i <= data->block[0]; i++)
412 msgbuf0[i] = data->block[i];
414 break;
415 default:
416 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
417 return -EOPNOTSUPP;
420 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
421 && size != I2C_SMBUS_I2C_BLOCK_DATA);
422 if (i) {
423 /* Compute PEC if first message is a write */
424 if (!(msg[0].flags & I2C_M_RD)) {
425 if (num == 1) /* Write only */
426 i2c_smbus_add_pec(&msg[0]);
427 else /* Write followed by read */
428 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
430 /* Ask for PEC if last message is a read */
431 if (msg[num-1].flags & I2C_M_RD)
432 msg[num-1].len++;
435 status = i2c_transfer(adapter, msg, num);
436 if (status < 0)
437 return status;
439 /* Check PEC if last message is a read */
440 if (i && (msg[num-1].flags & I2C_M_RD)) {
441 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
442 if (status < 0)
443 return status;
446 if (read_write == I2C_SMBUS_READ)
447 switch (size) {
448 case I2C_SMBUS_BYTE:
449 data->byte = msgbuf0[0];
450 break;
451 case I2C_SMBUS_BYTE_DATA:
452 data->byte = msgbuf1[0];
453 break;
454 case I2C_SMBUS_WORD_DATA:
455 case I2C_SMBUS_PROC_CALL:
456 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
457 break;
458 case I2C_SMBUS_I2C_BLOCK_DATA:
459 for (i = 0; i < data->block[0]; i++)
460 data->block[i+1] = msgbuf1[i];
461 break;
462 case I2C_SMBUS_BLOCK_DATA:
463 case I2C_SMBUS_BLOCK_PROC_CALL:
464 for (i = 0; i < msgbuf1[0] + 1; i++)
465 data->block[i] = msgbuf1[i];
466 break;
468 return 0;
472 * i2c_smbus_xfer - execute SMBus protocol operations
473 * @adapter: Handle to I2C bus
474 * @addr: Address of SMBus slave on that bus
475 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
476 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
477 * @command: Byte interpreted by slave, for protocols which use such bytes
478 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
479 * @data: Data to be read or written
481 * This executes an SMBus protocol operation, and returns a negative
482 * errno code else zero on success.
484 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
485 char read_write, u8 command, int protocol,
486 union i2c_smbus_data *data)
488 unsigned long orig_jiffies;
489 int try;
490 s32 res;
492 /* If enabled, the following two tracepoints are conditional on
493 * read_write and protocol.
495 trace_smbus_write(adapter, addr, flags, read_write,
496 command, protocol, data);
497 trace_smbus_read(adapter, addr, flags, read_write,
498 command, protocol);
500 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
502 if (adapter->algo->smbus_xfer) {
503 i2c_lock_bus(adapter, I2C_LOCK_SEGMENT);
505 /* Retry automatically on arbitration loss */
506 orig_jiffies = jiffies;
507 for (res = 0, try = 0; try <= adapter->retries; try++) {
508 res = adapter->algo->smbus_xfer(adapter, addr, flags,
509 read_write, command,
510 protocol, data);
511 if (res != -EAGAIN)
512 break;
513 if (time_after(jiffies,
514 orig_jiffies + adapter->timeout))
515 break;
517 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
519 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
520 goto trace;
522 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
523 * implement native support for the SMBus operation.
527 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
528 command, protocol, data);
530 trace:
531 /* If enabled, the reply tracepoint is conditional on read_write. */
532 trace_smbus_reply(adapter, addr, flags, read_write,
533 command, protocol, data);
534 trace_smbus_result(adapter, addr, flags, read_write,
535 command, protocol, res);
537 return res;
539 EXPORT_SYMBOL(i2c_smbus_xfer);
542 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
543 * @client: Handle to slave device
544 * @command: Byte interpreted by slave
545 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
546 * @values: Byte array into which data will be read; big enough to hold
547 * the data returned by the slave. SMBus allows at most
548 * I2C_SMBUS_BLOCK_MAX bytes.
550 * This executes the SMBus "block read" protocol if supported by the adapter.
551 * If block read is not supported, it emulates it using either word or byte
552 * read protocols depending on availability.
554 * The addresses of the I2C slave device that are accessed with this function
555 * must be mapped to a linear region, so that a block read will have the same
556 * effect as a byte read. Before using this function you must double-check
557 * if the I2C slave does support exchanging a block transfer with a byte
558 * transfer.
560 s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
561 u8 command, u8 length, u8 *values)
563 u8 i = 0;
564 int status;
566 if (length > I2C_SMBUS_BLOCK_MAX)
567 length = I2C_SMBUS_BLOCK_MAX;
569 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
570 return i2c_smbus_read_i2c_block_data(client, command, length, values);
572 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
573 return -EOPNOTSUPP;
575 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
576 while ((i + 2) <= length) {
577 status = i2c_smbus_read_word_data(client, command + i);
578 if (status < 0)
579 return status;
580 values[i] = status & 0xff;
581 values[i + 1] = status >> 8;
582 i += 2;
586 while (i < length) {
587 status = i2c_smbus_read_byte_data(client, command + i);
588 if (status < 0)
589 return status;
590 values[i] = status;
591 i++;
594 return i;
596 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
599 * i2c_setup_smbus_alert - Setup SMBus alert support
600 * @adapter: the target adapter
601 * @setup: setup data for the SMBus alert handler
602 * Context: can sleep
604 * Setup handling of the SMBus alert protocol on a given I2C bus segment.
606 * Handling can be done either through our IRQ handler, or by the
607 * adapter (from its handler, periodic polling, or whatever).
609 * NOTE that if we manage the IRQ, we *MUST* know if it's level or
610 * edge triggered in order to hand it to the workqueue correctly.
611 * If triggering the alert seems to wedge the system, you probably
612 * should have said it's level triggered.
614 * This returns the ara client, which should be saved for later use with
615 * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or NULL
616 * to indicate an error.
618 struct i2c_client *i2c_setup_smbus_alert(struct i2c_adapter *adapter,
619 struct i2c_smbus_alert_setup *setup)
621 struct i2c_board_info ara_board_info = {
622 I2C_BOARD_INFO("smbus_alert", 0x0c),
623 .platform_data = setup,
626 return i2c_new_device(adapter, &ara_board_info);
628 EXPORT_SYMBOL_GPL(i2c_setup_smbus_alert);
630 #if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_OF)
631 int of_i2c_setup_smbus_alert(struct i2c_adapter *adapter)
633 struct i2c_client *client;
634 int irq;
636 irq = of_property_match_string(adapter->dev.of_node, "interrupt-names",
637 "smbus_alert");
638 if (irq == -EINVAL || irq == -ENODATA)
639 return 0;
640 else if (irq < 0)
641 return irq;
643 client = i2c_setup_smbus_alert(adapter, NULL);
644 if (!client)
645 return -ENODEV;
647 return 0;
649 EXPORT_SYMBOL_GPL(of_i2c_setup_smbus_alert);
650 #endif