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 * @values: Byte array into which data will be read; big enough to hold 215 * the data returned by the slave. SMBus allows at most 32 bytes. 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 *values) 227 { 228 union i2c_smbus_data data; 229 int status; 230 231 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 232 I2C_SMBUS_READ, command, 233 I2C_SMBUS_BLOCK_DATA, &data); 234 if (status) 235 return status; 236 237 memcpy(values, &data.block[1], data.block[0]); 238 return data.block[0]; 239 } 240 EXPORT_SYMBOL(i2c_smbus_read_block_data); 241 242 /** 243 * i2c_smbus_write_block_data - SMBus "block write" protocol 244 * @client: Handle to slave device 245 * @command: Byte interpreted by slave 246 * @length: Size of data block; SMBus allows at most 32 bytes 247 * @values: Byte array which will be written. 248 * 249 * This executes the SMBus "block write" protocol, returning negative errno 250 * else zero on success. 251 */ 252 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command, 253 u8 length, const u8 *values) 254 { 255 union i2c_smbus_data data; 256 257 if (length > I2C_SMBUS_BLOCK_MAX) 258 length = I2C_SMBUS_BLOCK_MAX; 259 data.block[0] = length; 260 memcpy(&data.block[1], values, length); 261 return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 262 I2C_SMBUS_WRITE, command, 263 I2C_SMBUS_BLOCK_DATA, &data); 264 } 265 EXPORT_SYMBOL(i2c_smbus_write_block_data); 266 267 /* Returns the number of read bytes */ 268 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command, 269 u8 length, u8 *values) 270 { 271 union i2c_smbus_data data; 272 int status; 273 274 if (length > I2C_SMBUS_BLOCK_MAX) 275 length = I2C_SMBUS_BLOCK_MAX; 276 data.block[0] = length; 277 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags, 278 I2C_SMBUS_READ, command, 279 I2C_SMBUS_I2C_BLOCK_DATA, &data); 280 if (status < 0) 281 return status; 282 283 memcpy(values, &data.block[1], data.block[0]); 284 return data.block[0]; 285 } 286 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); 287 288 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command, 289 u8 length, const u8 *values) 290 { 291 union i2c_smbus_data data; 292 293 if (length > I2C_SMBUS_BLOCK_MAX) 294 length = I2C_SMBUS_BLOCK_MAX; 295 data.block[0] = length; 296 memcpy(data.block + 1, values, length); 297 return i2c_smbus_xfer(client->adapter, client->addr, client->flags, 298 I2C_SMBUS_WRITE, command, 299 I2C_SMBUS_I2C_BLOCK_DATA, &data); 300 } 301 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); 302 303 static void i2c_smbus_try_get_dmabuf(struct i2c_msg *msg, u8 init_val) 304 { 305 bool is_read = msg->flags & I2C_M_RD; 306 unsigned char *dma_buf; 307 308 dma_buf = kzalloc(I2C_SMBUS_BLOCK_MAX + (is_read ? 2 : 3), GFP_KERNEL); 309 if (!dma_buf) 310 return; 311 312 msg->buf = dma_buf; 313 msg->flags |= I2C_M_DMA_SAFE; 314 315 if (init_val) 316 msg->buf[0] = init_val; 317 } 318 319 /* 320 * Simulate a SMBus command using the I2C protocol. 321 * No checking of parameters is done! 322 */ 323 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr, 324 unsigned short flags, 325 char read_write, u8 command, int size, 326 union i2c_smbus_data *data) 327 { 328 /* 329 * So we need to generate a series of msgs. In the case of writing, we 330 * need to use only one message; when reading, we need two. We 331 * initialize most things with sane defaults, to keep the code below 332 * somewhat simpler. 333 */ 334 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; 335 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; 336 int nmsgs = read_write == I2C_SMBUS_READ ? 2 : 1; 337 u8 partial_pec = 0; 338 int status; 339 struct i2c_msg msg[2] = { 340 { 341 .addr = addr, 342 .flags = flags, 343 .len = 1, 344 .buf = msgbuf0, 345 }, { 346 .addr = addr, 347 .flags = flags | I2C_M_RD, 348 .len = 0, 349 .buf = msgbuf1, 350 }, 351 }; 352 bool wants_pec = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK 353 && size != I2C_SMBUS_I2C_BLOCK_DATA); 354 355 msgbuf0[0] = command; 356 msgbuf1[0] = 0; 357 switch (size) { 358 case I2C_SMBUS_QUICK: 359 msg[0].len = 0; 360 /* Special case: The read/write field is used as data */ 361 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ? 362 I2C_M_RD : 0); 363 nmsgs = 1; 364 break; 365 case I2C_SMBUS_BYTE: 366 if (read_write == I2C_SMBUS_READ) { 367 /* Special case: only a read! */ 368 msg[0].flags = I2C_M_RD | flags; 369 nmsgs = 1; 370 } 371 break; 372 case I2C_SMBUS_BYTE_DATA: 373 if (read_write == I2C_SMBUS_READ) 374 msg[1].len = 1; 375 else { 376 msg[0].len = 2; 377 msgbuf0[1] = data->byte; 378 } 379 break; 380 case I2C_SMBUS_WORD_DATA: 381 if (read_write == I2C_SMBUS_READ) 382 msg[1].len = 2; 383 else { 384 msg[0].len = 3; 385 msgbuf0[1] = data->word & 0xff; 386 msgbuf0[2] = data->word >> 8; 387 } 388 break; 389 case I2C_SMBUS_PROC_CALL: 390 nmsgs = 2; /* Special case */ 391 read_write = I2C_SMBUS_READ; 392 msg[0].len = 3; 393 msg[1].len = 2; 394 msgbuf0[1] = data->word & 0xff; 395 msgbuf0[2] = data->word >> 8; 396 break; 397 case I2C_SMBUS_BLOCK_DATA: 398 if (read_write == I2C_SMBUS_READ) { 399 msg[1].flags |= I2C_M_RECV_LEN; 400 msg[1].len = 1; /* block length will be added by 401 the underlying bus driver */ 402 i2c_smbus_try_get_dmabuf(&msg[1], 0); 403 } else { 404 msg[0].len = data->block[0] + 2; 405 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { 406 dev_err(&adapter->dev, 407 "Invalid block write size %d\n", 408 data->block[0]); 409 return -EINVAL; 410 } 411 412 i2c_smbus_try_get_dmabuf(&msg[0], command); 413 memcpy(msg[0].buf + 1, data->block, msg[0].len - 1); 414 } 415 break; 416 case I2C_SMBUS_BLOCK_PROC_CALL: 417 nmsgs = 2; /* Another special case */ 418 read_write = I2C_SMBUS_READ; 419 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { 420 dev_err(&adapter->dev, 421 "Invalid block write size %d\n", 422 data->block[0]); 423 return -EINVAL; 424 } 425 426 msg[0].len = data->block[0] + 2; 427 i2c_smbus_try_get_dmabuf(&msg[0], command); 428 memcpy(msg[0].buf + 1, data->block, msg[0].len - 1); 429 430 msg[1].flags |= I2C_M_RECV_LEN; 431 msg[1].len = 1; /* block length will be added by 432 the underlying bus driver */ 433 i2c_smbus_try_get_dmabuf(&msg[1], 0); 434 break; 435 case I2C_SMBUS_I2C_BLOCK_DATA: 436 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { 437 dev_err(&adapter->dev, "Invalid block %s size %d\n", 438 str_read_write(read_write == I2C_SMBUS_READ), 439 data->block[0]); 440 return -EINVAL; 441 } 442 443 if (read_write == I2C_SMBUS_READ) { 444 msg[1].len = data->block[0]; 445 i2c_smbus_try_get_dmabuf(&msg[1], 0); 446 } else { 447 msg[0].len = data->block[0] + 1; 448 449 i2c_smbus_try_get_dmabuf(&msg[0], command); 450 memcpy(msg[0].buf + 1, data->block + 1, data->block[0]); 451 } 452 break; 453 default: 454 dev_err(&adapter->dev, "Unsupported transaction %d\n", size); 455 return -EOPNOTSUPP; 456 } 457 458 if (wants_pec) { 459 /* Compute PEC if first message is a write */ 460 if (!(msg[0].flags & I2C_M_RD)) { 461 if (nmsgs == 1) /* Write only */ 462 i2c_smbus_add_pec(&msg[0]); 463 else /* Write followed by read */ 464 partial_pec = i2c_smbus_msg_pec(0, &msg[0]); 465 } 466 /* Ask for PEC if last message is a read */ 467 if (msg[nmsgs - 1].flags & I2C_M_RD) 468 msg[nmsgs - 1].len++; 469 } 470 471 status = __i2c_transfer(adapter, msg, nmsgs); 472 if (status < 0) 473 goto cleanup; 474 if (status != nmsgs) { 475 status = -EIO; 476 goto cleanup; 477 } 478 status = 0; 479 480 /* Check PEC if last message is a read */ 481 if (wants_pec && (msg[nmsgs - 1].flags & I2C_M_RD)) { 482 status = i2c_smbus_check_pec(partial_pec, &msg[nmsgs - 1]); 483 if (status < 0) 484 goto cleanup; 485 } 486 487 if (read_write == I2C_SMBUS_READ) 488 switch (size) { 489 case I2C_SMBUS_BYTE: 490 data->byte = msgbuf0[0]; 491 break; 492 case I2C_SMBUS_BYTE_DATA: 493 data->byte = msgbuf1[0]; 494 break; 495 case I2C_SMBUS_WORD_DATA: 496 case I2C_SMBUS_PROC_CALL: 497 data->word = msgbuf1[0] | (msgbuf1[1] << 8); 498 break; 499 case I2C_SMBUS_I2C_BLOCK_DATA: 500 memcpy(data->block + 1, msg[1].buf, data->block[0]); 501 break; 502 case I2C_SMBUS_BLOCK_DATA: 503 case I2C_SMBUS_BLOCK_PROC_CALL: 504 if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) { 505 dev_err(&adapter->dev, 506 "Invalid block size returned: %d\n", 507 msg[1].buf[0]); 508 status = -EPROTO; 509 goto cleanup; 510 } 511 memcpy(data->block, msg[1].buf, msg[1].buf[0] + 1); 512 break; 513 } 514 515 cleanup: 516 if (msg[0].flags & I2C_M_DMA_SAFE) 517 kfree(msg[0].buf); 518 if (msg[1].flags & I2C_M_DMA_SAFE) 519 kfree(msg[1].buf); 520 521 return status; 522 } 523 524 /** 525 * i2c_smbus_xfer - execute SMBus protocol operations 526 * @adapter: Handle to I2C bus 527 * @addr: Address of SMBus slave on that bus 528 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC) 529 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE 530 * @command: Byte interpreted by slave, for protocols which use such bytes 531 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL 532 * @data: Data to be read or written 533 * 534 * This executes an SMBus protocol operation, and returns a negative 535 * errno code else zero on success. 536 */ 537 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, 538 unsigned short flags, char read_write, 539 u8 command, int protocol, union i2c_smbus_data *data) 540 { 541 s32 res; 542 543 res = __i2c_lock_bus_helper(adapter); 544 if (res) 545 return res; 546 547 res = __i2c_smbus_xfer(adapter, addr, flags, read_write, 548 command, protocol, data); 549 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT); 550 551 return res; 552 } 553 EXPORT_SYMBOL(i2c_smbus_xfer); 554 555 s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, 556 unsigned short flags, char read_write, 557 u8 command, int protocol, union i2c_smbus_data *data) 558 { 559 int (*xfer_func)(struct i2c_adapter *adap, u16 addr, 560 unsigned short flags, char read_write, 561 u8 command, int size, union i2c_smbus_data *data); 562 unsigned long orig_jiffies; 563 int try; 564 s32 res; 565 566 res = __i2c_check_suspended(adapter); 567 if (res) 568 return res; 569 570 /* Reject invalid caller-supplied block lengths before any 571 * tracepoint or native smbus_xfer callback runs. 572 */ 573 if (data && 574 (protocol == I2C_SMBUS_I2C_BLOCK_DATA || 575 protocol == I2C_SMBUS_BLOCK_PROC_CALL || 576 (protocol == I2C_SMBUS_BLOCK_DATA && 577 read_write == I2C_SMBUS_WRITE)) && 578 (data->block[0] == 0 || 579 data->block[0] > I2C_SMBUS_BLOCK_MAX)) 580 return -EINVAL; 581 582 /* If enabled, the following two tracepoints are conditional on 583 * read_write and protocol. 584 */ 585 trace_smbus_write(adapter, addr, flags, read_write, 586 command, protocol, data); 587 trace_smbus_read(adapter, addr, flags, read_write, 588 command, protocol); 589 590 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB; 591 592 xfer_func = adapter->algo->smbus_xfer; 593 if (i2c_in_atomic_xfer_mode()) { 594 if (adapter->algo->smbus_xfer_atomic) 595 xfer_func = adapter->algo->smbus_xfer_atomic; 596 else if (adapter->algo->master_xfer_atomic) 597 xfer_func = NULL; /* fallback to I2C emulation */ 598 } 599 600 if (xfer_func) { 601 /* Retry automatically on arbitration loss */ 602 orig_jiffies = jiffies; 603 for (res = 0, try = 0; try <= adapter->retries; try++) { 604 res = xfer_func(adapter, addr, flags, read_write, 605 command, protocol, data); 606 if (res != -EAGAIN) 607 break; 608 if (time_after(jiffies, 609 orig_jiffies + adapter->timeout)) 610 break; 611 } 612 613 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer) 614 goto trace; 615 /* 616 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't 617 * implement native support for the SMBus operation. 618 */ 619 } 620 621 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, 622 command, protocol, data); 623 624 trace: 625 /* If enabled, the reply tracepoint is conditional on read_write. */ 626 trace_smbus_reply(adapter, addr, flags, read_write, 627 command, protocol, data, res); 628 trace_smbus_result(adapter, addr, flags, read_write, 629 command, protocol, res); 630 631 return res; 632 } 633 EXPORT_SYMBOL(__i2c_smbus_xfer); 634 635 /** 636 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate 637 * @client: Handle to slave device 638 * @command: Byte interpreted by slave 639 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes 640 * @values: Byte array into which data will be read; big enough to hold 641 * the data returned by the slave. SMBus allows at most 642 * I2C_SMBUS_BLOCK_MAX bytes. 643 * 644 * This executes the SMBus "block read" protocol if supported by the adapter. 645 * If block read is not supported, it emulates it using either word or byte 646 * read protocols depending on availability. 647 * 648 * The addresses of the I2C slave device that are accessed with this function 649 * must be mapped to a linear region, so that a block read will have the same 650 * effect as a byte read. Before using this function you must double-check 651 * if the I2C slave does support exchanging a block transfer with a byte 652 * transfer. 653 */ 654 s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client, 655 u8 command, u8 length, u8 *values) 656 { 657 u8 i = 0; 658 int status; 659 660 if (length > I2C_SMBUS_BLOCK_MAX) 661 length = I2C_SMBUS_BLOCK_MAX; 662 663 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) 664 return i2c_smbus_read_i2c_block_data(client, command, length, values); 665 666 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) 667 return -EOPNOTSUPP; 668 669 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) { 670 while ((i + 2) <= length) { 671 status = i2c_smbus_read_word_data(client, command + i); 672 if (status < 0) 673 return status; 674 values[i] = status & 0xff; 675 values[i + 1] = status >> 8; 676 i += 2; 677 } 678 } 679 680 while (i < length) { 681 status = i2c_smbus_read_byte_data(client, command + i); 682 if (status < 0) 683 return status; 684 values[i] = status; 685 i++; 686 } 687 688 return i; 689 } 690 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated); 691 692 /** 693 * i2c_new_smbus_alert_device - get ara client for SMBus alert support 694 * @adapter: the target adapter 695 * @setup: setup data for the SMBus alert handler 696 * Context: can sleep 697 * 698 * Setup handling of the SMBus alert protocol on a given I2C bus segment. 699 * 700 * Handling can be done either through our IRQ handler, or by the 701 * adapter (from its handler, periodic polling, or whatever). 702 * 703 * This returns the ara client, which should be saved for later use with 704 * i2c_handle_smbus_alert() and ultimately i2c_unregister_device(); or an 705 * ERRPTR to indicate an error. 706 */ 707 struct i2c_client *i2c_new_smbus_alert_device(struct i2c_adapter *adapter, 708 struct i2c_smbus_alert_setup *setup) 709 { 710 struct i2c_board_info ara_board_info = { 711 I2C_BOARD_INFO("smbus_alert", 0x0c), 712 .platform_data = setup, 713 }; 714 715 return i2c_new_client_device(adapter, &ara_board_info); 716 } 717 EXPORT_SYMBOL_GPL(i2c_new_smbus_alert_device); 718 719 #if IS_ENABLED(CONFIG_I2C_SMBUS) 720 int i2c_setup_smbus_alert(struct i2c_adapter *adapter) 721 { 722 struct device *parent = adapter->dev.parent; 723 int irq; 724 725 /* Adapter instantiated without parent, skip the SMBus alert setup */ 726 if (!parent) 727 return 0; 728 729 /* Report serious errors */ 730 irq = device_property_match_string(parent, "interrupt-names", "smbus_alert"); 731 if (irq < 0 && irq != -EINVAL && irq != -ENODATA) 732 return irq; 733 734 /* Skip setup when no irq was found */ 735 if (irq < 0 && !device_property_present(parent, "smbalert-gpios")) 736 return 0; 737 738 return PTR_ERR_OR_ZERO(i2c_new_smbus_alert_device(adapter, NULL)); 739 } 740 #endif 741