1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#ifndef _LINUX_I2C_H
27#define _LINUX_I2C_H
28
29#include <linux/mod_devicetable.h>
30#include <linux/device.h>
31#include <linux/sched.h>
32#include <linux/mutex.h>
33#include <linux/rtmutex.h>
34#include <linux/irqdomain.h>
35#include <linux/of.h>
36#include <linux/swab.h>
37#include <uapi/linux/i2c.h>
38
39extern struct bus_type i2c_bus_type;
40extern struct device_type i2c_adapter_type;
41extern struct device_type i2c_client_type;
42
43
44
45struct i2c_msg;
46struct i2c_algorithm;
47struct i2c_adapter;
48struct i2c_client;
49struct i2c_driver;
50struct i2c_device_identity;
51union i2c_smbus_data;
52struct i2c_board_info;
53enum i2c_slave_event;
54typedef int (*i2c_slave_cb_t)(struct i2c_client *, enum i2c_slave_event, u8 *);
55
56struct module;
57struct property_entry;
58
59#if IS_ENABLED(CONFIG_I2C)
60
61
62
63
64
65
66
67extern int i2c_transfer_buffer_flags(const struct i2c_client *client,
68 char *buf, int count, u16 flags);
69
70
71
72
73
74
75
76
77
78static inline int i2c_master_recv(const struct i2c_client *client,
79 char *buf, int count)
80{
81 return i2c_transfer_buffer_flags(client, buf, count, I2C_M_RD);
82};
83
84
85
86
87
88
89
90
91
92
93static inline int i2c_master_recv_dmasafe(const struct i2c_client *client,
94 char *buf, int count)
95{
96 return i2c_transfer_buffer_flags(client, buf, count,
97 I2C_M_RD | I2C_M_DMA_SAFE);
98};
99
100
101
102
103
104
105
106
107
108static inline int i2c_master_send(const struct i2c_client *client,
109 const char *buf, int count)
110{
111 return i2c_transfer_buffer_flags(client, (char *)buf, count, 0);
112};
113
114
115
116
117
118
119
120
121
122
123static inline int i2c_master_send_dmasafe(const struct i2c_client *client,
124 const char *buf, int count)
125{
126 return i2c_transfer_buffer_flags(client, (char *)buf, count,
127 I2C_M_DMA_SAFE);
128};
129
130
131
132extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
133 int num);
134
135extern int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
136 int num);
137
138
139
140
141
142
143s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
144 unsigned short flags, char read_write, u8 command,
145 int protocol, union i2c_smbus_data *data);
146
147
148s32 __i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
149 unsigned short flags, char read_write, u8 command,
150 int protocol, union i2c_smbus_data *data);
151
152
153
154
155extern s32 i2c_smbus_read_byte(const struct i2c_client *client);
156extern s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value);
157extern s32 i2c_smbus_read_byte_data(const struct i2c_client *client,
158 u8 command);
159extern s32 i2c_smbus_write_byte_data(const struct i2c_client *client,
160 u8 command, u8 value);
161extern s32 i2c_smbus_read_word_data(const struct i2c_client *client,
162 u8 command);
163extern s32 i2c_smbus_write_word_data(const struct i2c_client *client,
164 u8 command, u16 value);
165
166static inline s32
167i2c_smbus_read_word_swapped(const struct i2c_client *client, u8 command)
168{
169 s32 value = i2c_smbus_read_word_data(client, command);
170
171 return (value < 0) ? value : swab16(value);
172}
173
174static inline s32
175i2c_smbus_write_word_swapped(const struct i2c_client *client,
176 u8 command, u16 value)
177{
178 return i2c_smbus_write_word_data(client, command, swab16(value));
179}
180
181
182extern s32 i2c_smbus_read_block_data(const struct i2c_client *client,
183 u8 command, u8 *values);
184extern s32 i2c_smbus_write_block_data(const struct i2c_client *client,
185 u8 command, u8 length, const u8 *values);
186
187extern s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client,
188 u8 command, u8 length, u8 *values);
189extern s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client,
190 u8 command, u8 length,
191 const u8 *values);
192extern s32
193i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
194 u8 command, u8 length, u8 *values);
195int i2c_get_device_id(const struct i2c_client *client,
196 struct i2c_device_identity *id);
197#endif
198
199
200
201
202
203
204
205struct i2c_device_identity {
206 u16 manufacturer_id;
207#define I2C_DEVICE_ID_NXP_SEMICONDUCTORS 0
208#define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_1 1
209#define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_2 2
210#define I2C_DEVICE_ID_NXP_SEMICONDUCTORS_3 3
211#define I2C_DEVICE_ID_RAMTRON_INTERNATIONAL 4
212#define I2C_DEVICE_ID_ANALOG_DEVICES 5
213#define I2C_DEVICE_ID_STMICROELECTRONICS 6
214#define I2C_DEVICE_ID_ON_SEMICONDUCTOR 7
215#define I2C_DEVICE_ID_SPRINTEK_CORPORATION 8
216#define I2C_DEVICE_ID_ESPROS_PHOTONICS_AG 9
217#define I2C_DEVICE_ID_FUJITSU_SEMICONDUCTOR 10
218#define I2C_DEVICE_ID_FLIR 11
219#define I2C_DEVICE_ID_O2MICRO 12
220#define I2C_DEVICE_ID_ATMEL 13
221#define I2C_DEVICE_ID_NONE 0xffff
222 u16 part_id;
223 u8 die_revision;
224};
225
226enum i2c_alert_protocol {
227 I2C_PROTOCOL_SMBUS_ALERT,
228 I2C_PROTOCOL_SMBUS_HOST_NOTIFY,
229};
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267struct i2c_driver {
268 unsigned int class;
269
270
271 int (*probe)(struct i2c_client *, const struct i2c_device_id *);
272 int (*remove)(struct i2c_client *);
273
274
275
276
277 int (*probe_new)(struct i2c_client *);
278
279
280 void (*shutdown)(struct i2c_client *);
281
282
283
284
285
286
287
288
289 void (*alert)(struct i2c_client *, enum i2c_alert_protocol protocol,
290 unsigned int data);
291
292
293
294
295 int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);
296
297 struct device_driver driver;
298 const struct i2c_device_id *id_table;
299
300
301 int (*detect)(struct i2c_client *, struct i2c_board_info *);
302 const unsigned short *address_list;
303 struct list_head clients;
304
305 bool disable_i2c_core_irq_mapping;
306};
307#define to_i2c_driver(d) container_of(d, struct i2c_driver, driver)
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328struct i2c_client {
329 unsigned short flags;
330 unsigned short addr;
331
332
333 char name[I2C_NAME_SIZE];
334 struct i2c_adapter *adapter;
335 struct device dev;
336 int irq;
337 struct list_head detected;
338#if IS_ENABLED(CONFIG_I2C_SLAVE)
339 i2c_slave_cb_t slave_cb;
340#endif
341};
342#define to_i2c_client(d) container_of(d, struct i2c_client, dev)
343
344extern struct i2c_client *i2c_verify_client(struct device *dev);
345extern struct i2c_adapter *i2c_verify_adapter(struct device *dev);
346extern const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
347 const struct i2c_client *client);
348
349static inline struct i2c_client *kobj_to_i2c_client(struct kobject *kobj)
350{
351 struct device * const dev = container_of(kobj, struct device, kobj);
352 return to_i2c_client(dev);
353}
354
355static inline void *i2c_get_clientdata(const struct i2c_client *dev)
356{
357 return dev_get_drvdata(&dev->dev);
358}
359
360static inline void i2c_set_clientdata(struct i2c_client *dev, void *data)
361{
362 dev_set_drvdata(&dev->dev, data);
363}
364
365
366
367#if IS_ENABLED(CONFIG_I2C_SLAVE)
368enum i2c_slave_event {
369 I2C_SLAVE_READ_REQUESTED,
370 I2C_SLAVE_WRITE_REQUESTED,
371 I2C_SLAVE_READ_PROCESSED,
372 I2C_SLAVE_WRITE_RECEIVED,
373 I2C_SLAVE_STOP,
374};
375
376extern int i2c_slave_register(struct i2c_client *client, i2c_slave_cb_t slave_cb);
377extern int i2c_slave_unregister(struct i2c_client *client);
378extern bool i2c_detect_slave_mode(struct device *dev);
379
380static inline int i2c_slave_event(struct i2c_client *client,
381 enum i2c_slave_event event, u8 *val)
382{
383 return client->slave_cb(client, event, val);
384}
385#else
386static inline bool i2c_detect_slave_mode(struct device *dev) { return false; }
387#endif
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414struct i2c_board_info {
415 char type[I2C_NAME_SIZE];
416 unsigned short flags;
417 unsigned short addr;
418 const char *dev_name;
419 void *platform_data;
420 struct device_node *of_node;
421 struct fwnode_handle *fwnode;
422 const struct property_entry *properties;
423 const struct resource *resources;
424 unsigned int num_resources;
425 int irq;
426};
427
428
429
430
431
432
433
434
435
436
437
438#define I2C_BOARD_INFO(dev_type, dev_addr) \
439 .type = dev_type, .addr = (dev_addr)
440
441
442#if IS_ENABLED(CONFIG_I2C)
443
444
445
446
447extern struct i2c_client *
448i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info);
449
450
451
452
453
454
455
456extern struct i2c_client *
457i2c_new_probed_device(struct i2c_adapter *adap,
458 struct i2c_board_info *info,
459 unsigned short const *addr_list,
460 int (*probe)(struct i2c_adapter *, unsigned short addr));
461
462
463extern int i2c_probe_func_quick_read(struct i2c_adapter *, unsigned short addr);
464
465
466
467
468extern struct i2c_client *
469i2c_new_dummy(struct i2c_adapter *adap, u16 address);
470
471extern struct i2c_client *
472i2c_new_secondary_device(struct i2c_client *client,
473 const char *name,
474 u16 default_addr);
475
476extern void i2c_unregister_device(struct i2c_client *);
477#endif
478
479
480
481
482
483#ifdef CONFIG_I2C_BOARDINFO
484extern int
485i2c_register_board_info(int busnum, struct i2c_board_info const *info,
486 unsigned n);
487#else
488static inline int
489i2c_register_board_info(int busnum, struct i2c_board_info const *info,
490 unsigned n)
491{
492 return 0;
493}
494#endif
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518struct i2c_algorithm {
519
520
521
522
523
524
525 int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,
526 int num);
527 int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,
528 unsigned short flags, char read_write,
529 u8 command, int size, union i2c_smbus_data *data);
530
531
532 u32 (*functionality) (struct i2c_adapter *);
533
534#if IS_ENABLED(CONFIG_I2C_SLAVE)
535 int (*reg_slave)(struct i2c_client *client);
536 int (*unreg_slave)(struct i2c_client *client);
537#endif
538};
539
540
541
542
543
544
545
546
547
548struct i2c_lock_operations {
549 void (*lock_bus)(struct i2c_adapter *, unsigned int flags);
550 int (*trylock_bus)(struct i2c_adapter *, unsigned int flags);
551 void (*unlock_bus)(struct i2c_adapter *, unsigned int flags);
552};
553
554
555
556
557
558
559
560
561
562
563struct i2c_timings {
564 u32 bus_freq_hz;
565 u32 scl_rise_ns;
566 u32 scl_fall_ns;
567 u32 scl_int_delay_ns;
568 u32 sda_fall_ns;
569 u32 sda_hold_ns;
570};
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595struct i2c_bus_recovery_info {
596 int (*recover_bus)(struct i2c_adapter *adap);
597
598 int (*get_scl)(struct i2c_adapter *adap);
599 void (*set_scl)(struct i2c_adapter *adap, int val);
600 int (*get_sda)(struct i2c_adapter *adap);
601 void (*set_sda)(struct i2c_adapter *adap, int val);
602 int (*get_bus_free)(struct i2c_adapter *adap);
603
604 void (*prepare_recovery)(struct i2c_adapter *adap);
605 void (*unprepare_recovery)(struct i2c_adapter *adap);
606
607
608 struct gpio_desc *scl_gpiod;
609 struct gpio_desc *sda_gpiod;
610};
611
612int i2c_recover_bus(struct i2c_adapter *adap);
613
614
615int i2c_generic_scl_recovery(struct i2c_adapter *adap);
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638struct i2c_adapter_quirks {
639 u64 flags;
640 int max_num_msgs;
641 u16 max_write_len;
642 u16 max_read_len;
643 u16 max_comb_1st_msg_len;
644 u16 max_comb_2nd_msg_len;
645};
646
647
648#define I2C_AQ_COMB BIT(0)
649
650#define I2C_AQ_COMB_WRITE_FIRST BIT(1)
651
652#define I2C_AQ_COMB_READ_SECOND BIT(2)
653
654#define I2C_AQ_COMB_SAME_ADDR BIT(3)
655
656#define I2C_AQ_COMB_WRITE_THEN_READ (I2C_AQ_COMB | I2C_AQ_COMB_WRITE_FIRST | \
657 I2C_AQ_COMB_READ_SECOND | I2C_AQ_COMB_SAME_ADDR)
658
659#define I2C_AQ_NO_CLK_STRETCH BIT(4)
660
661#define I2C_AQ_NO_ZERO_LEN_READ BIT(5)
662#define I2C_AQ_NO_ZERO_LEN_WRITE BIT(6)
663#define I2C_AQ_NO_ZERO_LEN (I2C_AQ_NO_ZERO_LEN_READ | I2C_AQ_NO_ZERO_LEN_WRITE)
664
665
666
667
668
669struct i2c_adapter {
670 struct module *owner;
671 unsigned int class;
672 const struct i2c_algorithm *algo;
673 void *algo_data;
674
675
676 const struct i2c_lock_operations *lock_ops;
677 struct rt_mutex bus_lock;
678 struct rt_mutex mux_lock;
679
680 int timeout;
681 int retries;
682 struct device dev;
683
684 int nr;
685 char name[48];
686 struct completion dev_released;
687
688 struct mutex userspace_clients_lock;
689 struct list_head userspace_clients;
690
691 struct i2c_bus_recovery_info *bus_recovery_info;
692 const struct i2c_adapter_quirks *quirks;
693
694 struct irq_domain *host_notify_domain;
695};
696#define to_i2c_adapter(d) container_of(d, struct i2c_adapter, dev)
697
698static inline void *i2c_get_adapdata(const struct i2c_adapter *dev)
699{
700 return dev_get_drvdata(&dev->dev);
701}
702
703static inline void i2c_set_adapdata(struct i2c_adapter *dev, void *data)
704{
705 dev_set_drvdata(&dev->dev, data);
706}
707
708static inline struct i2c_adapter *
709i2c_parent_is_i2c_adapter(const struct i2c_adapter *adapter)
710{
711#if IS_ENABLED(CONFIG_I2C_MUX)
712 struct device *parent = adapter->dev.parent;
713
714 if (parent != NULL && parent->type == &i2c_adapter_type)
715 return to_i2c_adapter(parent);
716 else
717#endif
718 return NULL;
719}
720
721int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *));
722
723
724#define I2C_LOCK_ROOT_ADAPTER BIT(0)
725#define I2C_LOCK_SEGMENT BIT(1)
726
727
728
729
730
731
732
733static inline void
734i2c_lock_bus(struct i2c_adapter *adapter, unsigned int flags)
735{
736 adapter->lock_ops->lock_bus(adapter, flags);
737}
738
739
740
741
742
743
744
745
746
747static inline int
748i2c_trylock_bus(struct i2c_adapter *adapter, unsigned int flags)
749{
750 return adapter->lock_ops->trylock_bus(adapter, flags);
751}
752
753
754
755
756
757
758
759static inline void
760i2c_unlock_bus(struct i2c_adapter *adapter, unsigned int flags)
761{
762 adapter->lock_ops->unlock_bus(adapter, flags);
763}
764
765
766#define I2C_CLIENT_PEC 0x04
767#define I2C_CLIENT_TEN 0x10
768
769#define I2C_CLIENT_SLAVE 0x20
770#define I2C_CLIENT_HOST_NOTIFY 0x40
771#define I2C_CLIENT_WAKE 0x80
772#define I2C_CLIENT_SCCB 0x9000
773
774
775
776#define I2C_CLASS_HWMON (1<<0)
777#define I2C_CLASS_DDC (1<<3)
778#define I2C_CLASS_SPD (1<<7)
779
780#define I2C_CLASS_DEPRECATED (1<<8)
781
782
783#define I2C_CLIENT_END 0xfffeU
784
785
786#define I2C_ADDRS(addr, addrs...) \
787 ((const unsigned short []){ addr, ## addrs, I2C_CLIENT_END })
788
789
790
791
792
793
794#if IS_ENABLED(CONFIG_I2C)
795extern int i2c_add_adapter(struct i2c_adapter *);
796extern void i2c_del_adapter(struct i2c_adapter *);
797extern int i2c_add_numbered_adapter(struct i2c_adapter *);
798
799extern int i2c_register_driver(struct module *, struct i2c_driver *);
800extern void i2c_del_driver(struct i2c_driver *);
801
802
803#define i2c_add_driver(driver) \
804 i2c_register_driver(THIS_MODULE, driver)
805
806extern struct i2c_client *i2c_use_client(struct i2c_client *client);
807extern void i2c_release_client(struct i2c_client *client);
808
809
810
811extern void i2c_clients_command(struct i2c_adapter *adap,
812 unsigned int cmd, void *arg);
813
814extern struct i2c_adapter *i2c_get_adapter(int nr);
815extern void i2c_put_adapter(struct i2c_adapter *adap);
816extern unsigned int i2c_adapter_depth(struct i2c_adapter *adapter);
817
818void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults);
819
820
821static inline u32 i2c_get_functionality(struct i2c_adapter *adap)
822{
823 return adap->algo->functionality(adap);
824}
825
826
827static inline int i2c_check_functionality(struct i2c_adapter *adap, u32 func)
828{
829 return (func & i2c_get_functionality(adap)) == func;
830}
831
832
833
834
835
836
837
838
839static inline bool i2c_check_quirks(struct i2c_adapter *adap, u64 quirks)
840{
841 if (!adap->quirks)
842 return false;
843 return (adap->quirks->flags & quirks) == quirks;
844}
845
846
847static inline int i2c_adapter_id(struct i2c_adapter *adap)
848{
849 return adap->nr;
850}
851
852static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
853{
854 return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);
855}
856
857u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold);
858void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred);
859
860int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);
861
862
863
864
865
866
867
868
869#define module_i2c_driver(__i2c_driver) \
870 module_driver(__i2c_driver, i2c_add_driver, \
871 i2c_del_driver)
872
873
874
875
876
877
878
879
880
881#define builtin_i2c_driver(__i2c_driver) \
882 builtin_driver(__i2c_driver, i2c_add_driver)
883
884#endif
885
886#if IS_ENABLED(CONFIG_OF)
887
888extern struct i2c_client *of_find_i2c_device_by_node(struct device_node *node);
889
890
891extern struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node);
892
893
894struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node);
895
896extern const struct of_device_id
897*i2c_of_match_device(const struct of_device_id *matches,
898 struct i2c_client *client);
899
900int of_i2c_get_board_info(struct device *dev, struct device_node *node,
901 struct i2c_board_info *info);
902
903#else
904
905static inline struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
906{
907 return NULL;
908}
909
910static inline struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
911{
912 return NULL;
913}
914
915static inline struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
916{
917 return NULL;
918}
919
920static inline const struct of_device_id
921*i2c_of_match_device(const struct of_device_id *matches,
922 struct i2c_client *client)
923{
924 return NULL;
925}
926
927static inline int of_i2c_get_board_info(struct device *dev,
928 struct device_node *node,
929 struct i2c_board_info *info)
930{
931 return -ENOTSUPP;
932}
933
934#endif
935
936#if IS_ENABLED(CONFIG_ACPI)
937u32 i2c_acpi_find_bus_speed(struct device *dev);
938struct i2c_client *i2c_acpi_new_device(struct device *dev, int index,
939 struct i2c_board_info *info);
940#else
941static inline u32 i2c_acpi_find_bus_speed(struct device *dev)
942{
943 return 0;
944}
945static inline struct i2c_client *i2c_acpi_new_device(struct device *dev,
946 int index, struct i2c_board_info *info)
947{
948 return NULL;
949}
950#endif
951
952#endif
953