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