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