1
2
3
4
5
6
7
8
9
10
11
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/slab.h>
18
19#include "i2c-core.h"
20
21#define CREATE_TRACE_POINTS
22#include <trace/events/smbus.h>
23
24
25
26
27#define POLY (0x1070U << 3)
28static u8 crc8(u16 data)
29{
30 int i;
31
32 for (i = 0; i < 8; i++) {
33 if (data & 0x8000)
34 data = data ^ POLY;
35 data = data << 1;
36 }
37 return (u8)(data >> 8);
38}
39
40
41static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
42{
43 int i;
44
45 for (i = 0; i < count; i++)
46 crc = crc8((crc ^ p[i]) << 8);
47 return crc;
48}
49
50
51static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
52{
53
54 u8 addr = i2c_8bit_addr_from_msg(msg);
55 pec = i2c_smbus_pec(pec, &addr, 1);
56
57
58 return i2c_smbus_pec(pec, msg->buf, msg->len);
59}
60
61
62static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
63{
64 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
65 msg->len++;
66}
67
68
69
70
71
72
73static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
74{
75 u8 rpec = msg->buf[--msg->len];
76 cpec = i2c_smbus_msg_pec(cpec, msg);
77
78 if (rpec != cpec) {
79 pr_debug("Bad PEC 0x%02x vs. 0x%02x\n",
80 rpec, cpec);
81 return -EBADMSG;
82 }
83 return 0;
84}
85
86
87
88
89
90
91
92
93s32 i2c_smbus_read_byte(const struct i2c_client *client)
94{
95 union i2c_smbus_data data;
96 int status;
97
98 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
99 I2C_SMBUS_READ, 0,
100 I2C_SMBUS_BYTE, &data);
101 return (status < 0) ? status : data.byte;
102}
103EXPORT_SYMBOL(i2c_smbus_read_byte);
104
105
106
107
108
109
110
111
112
113s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
114{
115 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
116 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
117}
118EXPORT_SYMBOL(i2c_smbus_write_byte);
119
120
121
122
123
124
125
126
127
128s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
129{
130 union i2c_smbus_data data;
131 int status;
132
133 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
134 I2C_SMBUS_READ, command,
135 I2C_SMBUS_BYTE_DATA, &data);
136 return (status < 0) ? status : data.byte;
137}
138EXPORT_SYMBOL(i2c_smbus_read_byte_data);
139
140
141
142
143
144
145
146
147
148
149s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
150 u8 value)
151{
152 union i2c_smbus_data data;
153 data.byte = value;
154 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
155 I2C_SMBUS_WRITE, command,
156 I2C_SMBUS_BYTE_DATA, &data);
157}
158EXPORT_SYMBOL(i2c_smbus_write_byte_data);
159
160
161
162
163
164
165
166
167
168s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
169{
170 union i2c_smbus_data data;
171 int status;
172
173 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
174 I2C_SMBUS_READ, command,
175 I2C_SMBUS_WORD_DATA, &data);
176 return (status < 0) ? status : data.word;
177}
178EXPORT_SYMBOL(i2c_smbus_read_word_data);
179
180
181
182
183
184
185
186
187
188
189s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
190 u16 value)
191{
192 union i2c_smbus_data data;
193 data.word = value;
194 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
195 I2C_SMBUS_WRITE, command,
196 I2C_SMBUS_WORD_DATA, &data);
197}
198EXPORT_SYMBOL(i2c_smbus_write_word_data);
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
216 u8 *values)
217{
218 union i2c_smbus_data data;
219 int status;
220
221 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
222 I2C_SMBUS_READ, command,
223 I2C_SMBUS_BLOCK_DATA, &data);
224 if (status)
225 return status;
226
227 memcpy(values, &data.block[1], data.block[0]);
228 return data.block[0];
229}
230EXPORT_SYMBOL(i2c_smbus_read_block_data);
231
232
233
234
235
236
237
238
239
240
241
242s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
243 u8 length, const u8 *values)
244{
245 union i2c_smbus_data data;
246
247 if (length > I2C_SMBUS_BLOCK_MAX)
248 length = I2C_SMBUS_BLOCK_MAX;
249 data.block[0] = length;
250 memcpy(&data.block[1], values, length);
251 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
252 I2C_SMBUS_WRITE, command,
253 I2C_SMBUS_BLOCK_DATA, &data);
254}
255EXPORT_SYMBOL(i2c_smbus_write_block_data);
256
257
258s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
259 u8 length, u8 *values)
260{
261 union i2c_smbus_data data;
262 int status;
263
264 if (length > I2C_SMBUS_BLOCK_MAX)
265 length = I2C_SMBUS_BLOCK_MAX;
266 data.block[0] = length;
267 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
268 I2C_SMBUS_READ, command,
269 I2C_SMBUS_I2C_BLOCK_DATA, &data);
270 if (status < 0)
271 return status;
272
273 memcpy(values, &data.block[1], data.block[0]);
274 return data.block[0];
275}
276EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
277
278s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
279 u8 length, const u8 *values)
280{
281 union i2c_smbus_data data;
282
283 if (length > I2C_SMBUS_BLOCK_MAX)
284 length = I2C_SMBUS_BLOCK_MAX;
285 data.block[0] = length;
286 memcpy(data.block + 1, values, length);
287 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
288 I2C_SMBUS_WRITE, command,
289 I2C_SMBUS_I2C_BLOCK_DATA, &data);
290}
291EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
292
293static void i2c_smbus_try_get_dmabuf(struct i2c_msg *msg, u8 init_val)
294{
295 bool is_read = msg->flags & I2C_M_RD;
296 unsigned char *dma_buf;
297
298 dma_buf = kzalloc(I2C_SMBUS_BLOCK_MAX + (is_read ? 2 : 3), GFP_KERNEL);
299 if (!dma_buf)
300 return;
301
302 msg->buf = dma_buf;
303 msg->flags |= I2C_M_DMA_SAFE;
304
305 if (init_val)
306 msg->buf[0] = init_val;
307}
308
309
310
311
312
313static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
314 unsigned short flags,
315 char read_write, u8 command, int size,
316 union i2c_smbus_data *data)
317{
318
319
320
321
322
323
324 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
325 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
326 int nmsgs = read_write == I2C_SMBUS_READ ? 2 : 1;
327 u8 partial_pec = 0;
328 int status;
329 struct i2c_msg msg[2] = {
330 {
331 .addr = addr,
332 .flags = flags,
333 .len = 1,
334 .buf = msgbuf0,
335 }, {
336 .addr = addr,
337 .flags = flags | I2C_M_RD,
338 .len = 0,
339 .buf = msgbuf1,
340 },
341 };
342 bool wants_pec = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
343 && size != I2C_SMBUS_I2C_BLOCK_DATA);
344
345 msgbuf0[0] = command;
346 switch (size) {
347 case I2C_SMBUS_QUICK:
348 msg[0].len = 0;
349
350 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
351 I2C_M_RD : 0);
352 nmsgs = 1;
353 break;
354 case I2C_SMBUS_BYTE:
355 if (read_write == I2C_SMBUS_READ) {
356
357 msg[0].flags = I2C_M_RD | flags;
358 nmsgs = 1;
359 }
360 break;
361 case I2C_SMBUS_BYTE_DATA:
362 if (read_write == I2C_SMBUS_READ)
363 msg[1].len = 1;
364 else {
365 msg[0].len = 2;
366 msgbuf0[1] = data->byte;
367 }
368 break;
369 case I2C_SMBUS_WORD_DATA:
370 if (read_write == I2C_SMBUS_READ)
371 msg[1].len = 2;
372 else {
373 msg[0].len = 3;
374 msgbuf0[1] = data->word & 0xff;
375 msgbuf0[2] = data->word >> 8;
376 }
377 break;
378 case I2C_SMBUS_PROC_CALL:
379 nmsgs = 2;
380 read_write = I2C_SMBUS_READ;
381 msg[0].len = 3;
382 msg[1].len = 2;
383 msgbuf0[1] = data->word & 0xff;
384 msgbuf0[2] = data->word >> 8;
385 break;
386 case I2C_SMBUS_BLOCK_DATA:
387 if (read_write == I2C_SMBUS_READ) {
388 msg[1].flags |= I2C_M_RECV_LEN;
389 msg[1].len = 1;
390
391 i2c_smbus_try_get_dmabuf(&msg[1], 0);
392 } else {
393 msg[0].len = data->block[0] + 2;
394 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
395 dev_err(&adapter->dev,
396 "Invalid block write size %d\n",
397 data->block[0]);
398 return -EINVAL;
399 }
400
401 i2c_smbus_try_get_dmabuf(&msg[0], command);
402 memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
403 }
404 break;
405 case I2C_SMBUS_BLOCK_PROC_CALL:
406 nmsgs = 2;
407 read_write = I2C_SMBUS_READ;
408 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
409 dev_err(&adapter->dev,
410 "Invalid block write size %d\n",
411 data->block[0]);
412 return -EINVAL;
413 }
414
415 msg[0].len = data->block[0] + 2;
416 i2c_smbus_try_get_dmabuf(&msg[0], command);
417 memcpy(msg[0].buf + 1, data->block, msg[0].len - 1);
418
419 msg[1].flags |= I2C_M_RECV_LEN;
420 msg[1].len = 1;
421
422 i2c_smbus_try_get_dmabuf(&msg[1], 0);
423 break;
424 case I2C_SMBUS_I2C_BLOCK_DATA:
425 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
426 dev_err(&adapter->dev, "Invalid block %s size %d\n",
427 read_write == I2C_SMBUS_READ ? "read" : "write",
428 data->block[0]);
429 return -EINVAL;
430 }
431
432 if (read_write == I2C_SMBUS_READ) {
433 msg[1].len = data->block[0];
434 i2c_smbus_try_get_dmabuf(&msg[1], 0);
435 } else {
436 msg[0].len = data->block[0] + 1;
437
438 i2c_smbus_try_get_dmabuf(&msg[0], command);
439 memcpy(msg[0].buf + 1, data->block + 1, data->block[0]);
440 }
441 break;
442 default:
443 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
444 return -EOPNOTSUPP;
445 }
446
447 if (wants_pec) {
448
449 if (!(msg[0].flags & I2C_M_RD)) {
450 if (nmsgs == 1)
451 i2c_smbus_add_pec(&msg[0]);
452 else
453 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
454 }
455
456 if (msg[nmsgs - 1].flags & I2C_M_RD)
457 msg[nmsgs - 1].len++;
458 }
459
460 status = __i2c_transfer(adapter, msg, nmsgs);
461 if (status < 0)
462 goto cleanup;
463 if (status != nmsgs) {
464 status = -EIO;
465 goto cleanup;
466 }
467 status = 0;
468
469
470 if (wants_pec && (msg[nmsgs - 1].flags & I2C_M_RD)) {
471 status = i2c_smbus_check_pec(partial_pec, &msg[nmsgs - 1]);
472 if (status < 0)
473 goto cleanup;
474 }
475
476 if (read_write == I2C_SMBUS_READ)
477 switch (size) {
478 case I2C_SMBUS_BYTE:
479 data->byte = msgbuf0[0];
480 break;
481 case I2C_SMBUS_BYTE_DATA:
482 data->byte = msgbuf1[0];
483 break;
484 case I2C_SMBUS_WORD_DATA:
485 case I2C_SMBUS_PROC_CALL:
486 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
487 break;
488 case I2C_SMBUS_I2C_BLOCK_DATA:
489 memcpy(data->block + 1, msg[1].buf, data->block[0]);
490 break;
491 case I2C_SMBUS_BLOCK_DATA:
492 case I2C_SMBUS_BLOCK_PROC_CALL:
493 if (msg[1].buf[0] > I2C_SMBUS_BLOCK_MAX) {
494 dev_err(&adapter->dev,
495 "Invalid block size returned: %d\n",
496 msg[1].buf[0]);
497 status = -EPROTO;
498 goto cleanup;
499 }
500 memcpy(data->block, msg[1].buf, msg[1].buf[0] + 1);
501 break;
502 }
503
504cleanup:
505 if (msg[0].flags & I2C_M_DMA_SAFE)
506 kfree(msg[0].buf);
507 if (msg[1].flags & I2C_M_DMA_SAFE)
508 kfree(msg[1].buf);
509
510 return status;
511}
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
527 unsigned short flags, char read_write,
528 u8 command, int protocol, union i2c_smbus_data *data)
529{
530 s32 res;
531
532 res = __i2c_lock_bus_helper(adapter);
533 if (res)
534 return res;
535
536 res = __i2c_smbus_xfer(adapter, addr, flags, read_write,
537 command, protocol, data);
538 i2c_unlock_bus(adapter, I2C_LOCK_SEGMENT);
539
540 return res;
541}
542EXPORT_SYMBOL(i2c_smbus_xfer);
543
544s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
545 unsigned short flags, char read_write,
546 u8 command, int protocol, union i2c_smbus_data *data)
547{
548 int (*xfer_func)(struct i2c_adapter *adap, u16 addr,
549 unsigned short flags, char read_write,
550 u8 command, int size, union i2c_smbus_data *data);
551 unsigned long orig_jiffies;
552 int try;
553 s32 res;
554
555 res = __i2c_check_suspended(adapter);
556 if (res)
557 return res;
558
559
560
561
562 trace_smbus_write(adapter, addr, flags, read_write,
563 command, protocol, data);
564 trace_smbus_read(adapter, addr, flags, read_write,
565 command, protocol);
566
567 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
568
569 xfer_func = adapter->algo->smbus_xfer;
570 if (i2c_in_atomic_xfer_mode()) {
571 if (adapter->algo->smbus_xfer_atomic)
572 xfer_func = adapter->algo->smbus_xfer_atomic;
573 else if (adapter->algo->master_xfer_atomic)
574 xfer_func = NULL;
575 }
576
577 if (xfer_func) {
578
579 orig_jiffies = jiffies;
580 for (res = 0, try = 0; try <= adapter->retries; try++) {
581 res = xfer_func(adapter, addr, flags, read_write,
582 command, protocol, data);
583 if (res != -EAGAIN)
584 break;
585 if (time_after(jiffies,
586 orig_jiffies + adapter->timeout))
587 break;
588 }
589
590 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
591 goto trace;
592
593
594
595
596 }
597
598 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
599 command, protocol, data);
600
601trace:
602
603 trace_smbus_reply(adapter, addr, flags, read_write,
604 command, protocol, data, res);
605 trace_smbus_result(adapter, addr, flags, read_write,
606 command, protocol, res);
607
608 return res;
609}
610EXPORT_SYMBOL(__i2c_smbus_xfer);
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
632 u8 command, u8 length, u8 *values)
633{
634 u8 i = 0;
635 int status;
636
637 if (length > I2C_SMBUS_BLOCK_MAX)
638 length = I2C_SMBUS_BLOCK_MAX;
639
640 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
641 return i2c_smbus_read_i2c_block_data(client, command, length, values);
642
643 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA))
644 return -EOPNOTSUPP;
645
646 if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_WORD_DATA)) {
647 while ((i + 2) <= length) {
648 status = i2c_smbus_read_word_data(client, command + i);
649 if (status < 0)
650 return status;
651 values[i] = status & 0xff;
652 values[i + 1] = status >> 8;
653 i += 2;
654 }
655 }
656
657 while (i < length) {
658 status = i2c_smbus_read_byte_data(client, command + i);
659 if (status < 0)
660 return status;
661 values[i] = status;
662 i++;
663 }
664
665 return i;
666}
667EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated);
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684struct i2c_client *i2c_new_smbus_alert_device(struct i2c_adapter *adapter,
685 struct i2c_smbus_alert_setup *setup)
686{
687 struct i2c_board_info ara_board_info = {
688 I2C_BOARD_INFO("smbus_alert", 0x0c),
689 .platform_data = setup,
690 };
691
692 return i2c_new_client_device(adapter, &ara_board_info);
693}
694EXPORT_SYMBOL_GPL(i2c_new_smbus_alert_device);
695
696#if IS_ENABLED(CONFIG_I2C_SMBUS) && IS_ENABLED(CONFIG_OF)
697int of_i2c_setup_smbus_alert(struct i2c_adapter *adapter)
698{
699 int irq;
700
701 irq = of_property_match_string(adapter->dev.of_node, "interrupt-names",
702 "smbus_alert");
703 if (irq == -EINVAL || irq == -ENODATA)
704 return 0;
705 else if (irq < 0)
706 return irq;
707
708 return PTR_ERR_OR_ZERO(i2c_new_smbus_alert_device(adapter, NULL));
709}
710EXPORT_SYMBOL_GPL(of_i2c_setup_smbus_alert);
711#endif
712