0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/device.h>
0014 #include <linux/err.h>
0015 #include <linux/i2c.h>
0016 #include <linux/i2c-smbus.h>
0017 #include <linux/property.h>
0018 #include <linux/slab.h>
0019
0020 #include "i2c-core.h"
0021
0022 #define CREATE_TRACE_POINTS
0023 #include <trace/events/smbus.h>
0024
0025
0026
0027
0028 #define POLY (0x1070U << 3)
0029 static u8 crc8(u16 data)
0030 {
0031 int i;
0032
0033 for (i = 0; i < 8; i++) {
0034 if (data & 0x8000)
0035 data = data ^ POLY;
0036 data = data << 1;
0037 }
0038 return (u8)(data >> 8);
0039 }
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
0050 {
0051 int i;
0052
0053 for (i = 0; i < count; i++)
0054 crc = crc8((crc ^ p[i]) << 8);
0055 return crc;
0056 }
0057 EXPORT_SYMBOL(i2c_smbus_pec);
0058
0059
0060 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
0061 {
0062
0063 u8 addr = i2c_8bit_addr_from_msg(msg);
0064 pec = i2c_smbus_pec(pec, &addr, 1);
0065
0066
0067 return i2c_smbus_pec(pec, msg->buf, msg->len);
0068 }
0069
0070
0071 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
0072 {
0073 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
0074 msg->len++;
0075 }
0076
0077
0078
0079
0080
0081
0082 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
0083 {
0084 u8 rpec = msg->buf[--msg->len];
0085 cpec = i2c_smbus_msg_pec(cpec, msg);
0086
0087 if (rpec != cpec) {
0088 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
0089 rpec, cpec);
0090 return -EBADMSG;
0091 }
0092 return 0;
0093 }
0094
0095
0096
0097
0098
0099
0100
0101
0102 s32 i2c_smbus_read_byte(const struct i2c_client *client)
0103 {
0104 union i2c_smbus_data data;
0105 int status;
0106
0107 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0108 I2C_SMBUS_READ, 0,
0109 I2C_SMBUS_BYTE, &data);
0110 return (status < 0) ? status : data.byte;
0111 }
0112 EXPORT_SYMBOL(i2c_smbus_read_byte);
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
0123 {
0124 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0125 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
0126 }
0127 EXPORT_SYMBOL(i2c_smbus_write_byte);
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
0138 {
0139 union i2c_smbus_data data;
0140 int status;
0141
0142 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0143 I2C_SMBUS_READ, command,
0144 I2C_SMBUS_BYTE_DATA, &data);
0145 return (status < 0) ? status : data.byte;
0146 }
0147 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
0159 u8 value)
0160 {
0161 union i2c_smbus_data data;
0162 data.byte = value;
0163 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0164 I2C_SMBUS_WRITE, command,
0165 I2C_SMBUS_BYTE_DATA, &data);
0166 }
0167 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
0178 {
0179 union i2c_smbus_data data;
0180 int status;
0181
0182 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0183 I2C_SMBUS_READ, command,
0184 I2C_SMBUS_WORD_DATA, &data);
0185 return (status < 0) ? status : data.word;
0186 }
0187 EXPORT_SYMBOL(i2c_smbus_read_word_data);
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
0199 u16 value)
0200 {
0201 union i2c_smbus_data data;
0202 data.word = value;
0203 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0204 I2C_SMBUS_WRITE, command,
0205 I2C_SMBUS_WORD_DATA, &data);
0206 }
0207 EXPORT_SYMBOL(i2c_smbus_write_word_data);
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
0225 u8 *values)
0226 {
0227 union i2c_smbus_data data;
0228 int status;
0229
0230 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0231 I2C_SMBUS_READ, command,
0232 I2C_SMBUS_BLOCK_DATA, &data);
0233 if (status)
0234 return status;
0235
0236 memcpy(values, &data.block[1], data.block[0]);
0237 return data.block[0];
0238 }
0239 EXPORT_SYMBOL(i2c_smbus_read_block_data);
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
0252 u8 length, const u8 *values)
0253 {
0254 union i2c_smbus_data data;
0255
0256 if (length > I2C_SMBUS_BLOCK_MAX)
0257 length = I2C_SMBUS_BLOCK_MAX;
0258 data.block[0] = length;
0259 memcpy(&data.block[1], values, length);
0260 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0261 I2C_SMBUS_WRITE, command,
0262 I2C_SMBUS_BLOCK_DATA, &data);
0263 }
0264 EXPORT_SYMBOL(i2c_smbus_write_block_data);
0265
0266
0267 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
0268 u8 length, u8 *values)
0269 {
0270 union i2c_smbus_data data;
0271 int status;
0272
0273 if (length > I2C_SMBUS_BLOCK_MAX)
0274 length = I2C_SMBUS_BLOCK_MAX;
0275 data.block[0] = length;
0276 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0277 I2C_SMBUS_READ, command,
0278 I2C_SMBUS_I2C_BLOCK_DATA, &data);
0279 if (status < 0)
0280 return status;
0281
0282 memcpy(values, &data.block[1], data.block[0]);
0283 return data.block[0];
0284 }
0285 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
0286
0287 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
0288 u8 length, const u8 *values)
0289 {
0290 union i2c_smbus_data data;
0291
0292 if (length > I2C_SMBUS_BLOCK_MAX)
0293 length = I2C_SMBUS_BLOCK_MAX;
0294 data.block[0] = length;
0295 memcpy(data.block + 1, values, length);
0296 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
0297 I2C_SMBUS_WRITE, command,
0298 I2C_SMBUS_I2C_BLOCK_DATA, &data);
0299 }
0300 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
0301
0302 static void i2c_smbus_try_get_dmabuf(struct i2c_msg *msg, u8 init_val)
0303 {
0304 bool is_read = msg->flags & I2C_M_RD;
0305 unsigned char *dma_buf;
0306
0307 dma_buf = kzalloc(I2C_SMBUS_BLOCK_MAX + (is_read ? 2 : 3), GFP_KERNEL);
0308 if (!dma_buf)
0309 return;
0310
0311 msg->buf = dma_buf;
0312 msg->flags |= I2C_M_DMA_SAFE;
0313
0314 if (init_val)
0315 msg->buf[0] = init_val;
0316 }
0317
0318
0319
0320
0321
0322 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
0323 unsigned short flags,
0324 char read_write, u8 command, int size,
0325 union i2c_smbus_data *data)
0326 {
0327
0328
0329
0330
0331
0332
0333 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
0334 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
0335 int nmsgs = read_write == I2C_SMBUS_READ ? 2 : 1;
0336 u8 partial_pec = 0;
0337 int status;
0338 struct i2c_msg msg[2] = {
0339 {
0340 .addr = addr,
0341 .flags = flags,
0342 .len = 1,
0343 .buf = msgbuf0,
0344 }, {
0345 .addr = addr,
0346 .flags = flags | I2C_M_RD,
0347 .len = 0,
0348 .buf = msgbuf1,
0349 },
0350 };
0351 bool wants_pec = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
0352 && size != I2C_SMBUS_I2C_BLOCK_DATA);
0353
0354 msgbuf0[0] = command;
0355 switch (size) {
0356 case I2C_SMBUS_QUICK:
0357 msg[0].len = 0;
0358
0359 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
0360 I2C_M_RD : 0);
0361 nmsgs = 1;
0362 break;
0363 case I2C_SMBUS_BYTE:
0364 if (read_write == I2C_SMBUS_READ) {
0365
0366 msg[0].flags = I2C_M_RD | flags;
0367 nmsgs = 1;
0368 }
0369 break;
0370 case I2C_SMBUS_BYTE_DATA:
0371 if (read_write == I2C_SMBUS_READ)
0372 msg[1].len = 1;
0373 else {
0374 msg[0].len = 2;
0375 msgbuf0[1] = data->byte;
0376 }
0377 break;
0378 case I2C_SMBUS_WORD_DATA:
0379 if (read_write == I2C_SMBUS_READ)
0380 msg[1].len = 2;
0381 else {
0382 msg[0].len = 3;
0383 msgbuf0[1] = data->word & 0xff;
0384 msgbuf0[2] = data->word >> 8;
0385 }
0386 break;
0387 case I2C_SMBUS_PROC_CALL:
0388 nmsgs = 2;
0389 read_write = I2C_SMBUS_READ;
0390 msg[0].len = 3;
0391 msg[1].len = 2;
0392 msgbuf0[1] = data->word & 0xff;
0393 msgbuf0[2] = data->word >> 8;
0394 break;
0395 case I2C_SMBUS_BLOCK_DATA:
0396 if (read_write == I2C_SMBUS_READ) {
0397 msg[1].flags |= I2C_M_RECV_LEN;
0398 msg[1].len = 1;
0399
0400 i2c_smbus_try_get_dmabuf(&msg[1], 0);
0401 } else {
0402 msg[0].len = data->block[0] + 2;
0403 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
0404 dev_err(&adapter->dev,
0405 "Invalid block write size %d\n",
0406 data->block[0]);
0407 return -EINVAL;
0408 }
0409
0410 i2c_smbus_try_get_dmabuf(&msg[0], command);
0411 memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
0412 }
0413 break;
0414 case I2C_SMBUS_BLOCK_PROC_CALL:
0415 nmsgs = 2;
0416 read_write = I2C_SMBUS_READ;
0417 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
0418 dev_err(&adapter->dev,
0419 "Invalid block write size %d\n",
0420 data->block[0]);
0421 return -EINVAL;
0422 }
0423
0424 msg[0].len = data->block[0] + 2;
0425 i2c_smbus_try_get_dmabuf(&msg[0], command);
0426 memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
0427
0428 msg[1].flags |= I2C_M_RECV_LEN;
0429 msg[1].len = 1;
0430
0431 i2c_smbus_try_get_dmabuf(&msg[1], 0);
0432 break;
0433 case I2C_SMBUS_I2C_BLOCK_DATA:
0434 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
0435 dev_err(&adapter->dev, "Invalid block %s size %d\n",
0436 read_write == I2C_SMBUS_READ ? "read" : "write",
0437 data->block[0]);
0438 return -EINVAL;
0439 }
0440
0441 if (read_write == I2C_SMBUS_READ) {
0442 msg[1].len = data->block[0];
0443 i2c_smbus_try_get_dmabuf(&msg[1], 0);
0444 } else {
0445 msg[0].len = data->block[0] + 1;
0446
0447 i2c_smbus_try_get_dmabuf(&msg[0], command);
0448 memcpy(msg[0].buf + 1, data->block + 1, data->block[0]);
0449 }
0450 break;
0451 default:
0452 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
0453 return -EOPNOTSUPP;
0454 }
0455
0456 if (wants_pec) {
0457
0458 if (!(msg[0].flags & I2C_M_RD)) {
0459 if (nmsgs == 1)
0460 i2c_smbus_add_pec(&msg[0]);
0461 else
0462 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
0463 }
0464
0465 if (msg[nmsgs - 1].flags & I2C_M_RD)
0466 msg[nmsgs - 1].len++;
0467 }
0468
0469 status = __i2c_transfer(adapter, msg, nmsgs);
0470 if (status < 0)
0471 goto cleanup;
0472 if (status != nmsgs) {
0473 status = -EIO;
0474 goto cleanup;
0475 }
0476 status = 0;
0477
0478
0479 if (wants_pec && (msg[nmsgs - 1].flags & I2C_M_RD)) {
0480 status = i2c_smbus_check_pec(partial_pec, &msg[nmsgs - 1]);
0481 if (status < 0)
0482 goto cleanup;
0483 }
0484
0485 if (read_write == I2C_SMBUS_READ)
0486 switch (size) {
0487 case I2C_SMBUS_BYTE:
0488 data->byte = msgbuf0[0];
0489 break;
0490 case I2C_SMBUS_BYTE_DATA:
0491 data->byte = msgbuf1[0];
0492 break;
0493 case I2C_SMBUS_WORD_DATA:
0494 case I2C_SMBUS_PROC_CALL:
0495 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
0496 break;
0497 case I2C_SMBUS_I2C_BLOCK_DATA:
0498 memcpy(data->block + 1, msg[1].buf, data->block[0]);
0499 break;
0500 case I2C_SMBUS_BLOCK_DATA:
0501 case I2C_SMBUS_BLOCK_PROC_CALL:
0502 if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
0503 dev_err(&adapter->dev,
0504 "Invalid block size returned: %d\n",
0505 msg[1].buf[0]);
0506 status = -EPROTO;
0507 goto cleanup;
0508 }
0509 memcpy(data->block, msg[1].buf, msg[1].buf[0] + 1);
0510 break;
0511 }
0512
0513 cleanup:
0514 if (msg[0].flags & I2C_M_DMA_SAFE)
0515 kfree(msg[0].buf);
0516 if (msg[1].flags & I2C_M_DMA_SAFE)
0517 kfree(msg[1].buf);
0518
0519 return status;
0520 }
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533
0534
0535 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
0536 unsigned short flags, char read_write,
0537 u8 command, int protocol, union i2c_smbus_data *data)
0538 {
0539 s32 res;
0540
0541 res = __i2c_lock_bus_helper(adapter);
0542 if (res)
0543 return res;
0544
0545 res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
0546 command, protocol, data);
0547 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
0548
0549 return res;
0550 }
0551 EXPORT_SYMBOL(i2c_smbus_xfer);
0552
0553 s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
0554 unsigned short flags, char read_write,
0555 u8 command, int protocol, union i2c_smbus_data *data)
0556 {
0557 int (*xfer_func)(struct i2c_adapter *adap, u16 addr,
0558 unsigned short flags, char read_write,
0559 u8 command, int size, union i2c_smbus_data *data);
0560 unsigned long orig_jiffies;
0561 int try;
0562 s32 res;
0563
0564 res = __i2c_check_suspended(adapter);
0565 if (res)
0566 return res;
0567
0568
0569
0570
0571 trace_smbus_write(adapter, addr, flags, read_write,
0572 command, protocol, data);
0573 trace_smbus_read(adapter, addr, flags, read_write,
0574 command, protocol);
0575
0576 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
0577
0578 xfer_func = adapter->algo->smbus_xfer;
0579 if (i2c_in_atomic_xfer_mode()) {
0580 if (adapter->algo->smbus_xfer_atomic)
0581 xfer_func = adapter->algo->smbus_xfer_atomic;
0582 else if (adapter->algo->master_xfer_atomic)
0583 xfer_func = NULL;
0584 }
0585
0586 if (xfer_func) {
0587
0588 orig_jiffies = jiffies;
0589 for (res = 0, try = 0; try <= adapter->retries; try++) {
0590 res = xfer_func(adapter, addr, flags, read_write,
0591 command, protocol, data);
0592 if (res != -EAGAIN)
0593 break;
0594 if (time_after(jiffies,
0595 orig_jiffies + adapter->timeout))
0596 break;
0597 }
0598
0599 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
0600 goto trace;
0601
0602
0603
0604
0605 }
0606
0607 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
0608 command, protocol, data);
0609
0610 trace:
0611
0612 trace_smbus_reply(adapter, addr, flags, read_write,
0613 command, protocol, data, res);
0614 trace_smbus_result(adapter, addr, flags, read_write,
0615 command, protocol, res);
0616
0617 return res;
0618 }
0619 EXPORT_SYMBOL(__i2c_smbus_xfer);
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638
0639
0640 s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
0641 u8 command, u8 length, u8 *values)
0642 {
0643 u8 i = 0;
0644 int status;
0645
0646 if (length > I2C_SMBUS_BLOCK_MAX)
0647 length = I2C_SMBUS_BLOCK_MAX;
0648
0649 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
0650 return i2c_smbus_read_i2c_block_data(client, command, length, values);
0651
0652 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
0653 return -EOPNOTSUPP;
0654
0655 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
0656 while ((i + 2) <= length) {
0657 status = i2c_smbus_read_word_data(client, command + i);
0658 if (status < 0)
0659 return status;
0660 values[i] = status & 0xff;
0661 values[i + 1] = status >> 8;
0662 i += 2;
0663 }
0664 }
0665
0666 while (i < length) {
0667 status = i2c_smbus_read_byte_data(client, command + i);
0668 if (status < 0)
0669 return status;
0670 values[i] = status;
0671 i++;
0672 }
0673
0674 return i;
0675 }
0676 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693 struct i2c_client *i2c_new_smbus_alert_device(struct i2c_adapter *adapter,
0694 struct i2c_smbus_alert_setup *setup)
0695 {
0696 struct i2c_board_info ara_board_info = {
0697 I2C_BOARD_INFO("smbus_alert", 0x0c),
0698 .platform_data = setup,
0699 };
0700
0701 return i2c_new_client_device(adapter, &ara_board_info);
0702 }
0703 EXPORT_SYMBOL_GPL(i2c_new_smbus_alert_device);
0704
0705 #if IS_ENABLED(CONFIG_I2C_SMBUS)
0706 int i2c_setup_smbus_alert(struct i2c_adapter *adapter)
0707 {
0708 struct device *parent = adapter->dev.parent;
0709 int irq;
0710
0711
0712 if (!parent)
0713 return 0;
0714
0715 irq = device_property_match_string(parent, "interrupt-names", "smbus_alert");
0716 if (irq == -EINVAL || irq == -ENODATA)
0717 return 0;
0718 else if (irq < 0)
0719 return irq;
0720
0721 return PTR_ERR_OR_ZERO(i2c_new_smbus_alert_device(adapter, NULL));
0722 }
0723 #endif