1 /* $NetBSD: i2c.h,v 1.14 2021/12/19 11:49:12 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LINUX_I2C_H_
33 #define _LINUX_I2C_H_
34
35 #include <sys/types.h>
36 #include <sys/device_if.h>
37 #include <sys/queue.h> /* XXX include order botch: i2cvar.h needs */
38 #include <sys/systm.h>
39
40 #include <dev/i2c/i2cvar.h>
41
42 #include <linux/pm.h>
43 #include <linux/module.h>
44 #include <linux/mutex.h>
45
46 struct i2c_adapter;
47 struct i2c_algorithm;
48 struct i2c_msg;
49
50 #define I2C_NAME_SIZE 20
51
52 /*
53 * I2C_M_*: i2c_msg flags
54 */
55 #define I2C_M_RD 0x01 /* xfer is read, not write */
56 #define I2C_M_NOSTART 0x02 /* don't initiate xfer */
57 #define I2C_M_TEN 0x04 /* 10-bit chip address */
58 #define I2C_M_STOP 0x08 /* send stop after msg */
59
60 /*
61 * I2C_CLASS_*: i2c_adapter classes
62 */
63 #define I2C_CLASS_DDC 0x01
64 #define I2C_CLASS_SPD 0x02
65
66 /*
67 * I2C_FUNC_*: i2c_adapter functionality bits
68 */
69 #define I2C_FUNC_I2C 0x01
70 #define I2C_FUNC_NOSTART 0x02
71 #define I2C_FUNC_SMBUS_EMUL 0x04
72 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x08
73 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x10
74 #define I2C_FUNC_10BIT_ADDR 0x20
75
76 /*
77 * struct i2c_msg: A single i2c message request on a particular
78 * address. Read if I2C_M_RD is set, write otherwise.
79 */
80 struct i2c_msg {
81 i2c_addr_t addr;
82 uint16_t flags; /* I2C_M_* */
83 uint16_t len;
84 uint8_t *buf;
85 };
86
87 /*
88 * struct i2c_adapter: An i2c bus controller.
89 */
90 struct i2c_adapter {
91 char name[I2C_NAME_SIZE];
92 const struct i2c_algorithm *algo;
93 void *algo_data;
94 const struct i2c_lock_operations *lock_ops;
95 int retries;
96 struct module *owner;
97 unsigned int class; /* I2C_CLASS_* */
98 struct {
99 device_t parent;
100 } dev;
101 void *i2ca_adapdata;
102 };
103
104 /*
105 * struct i2c_algorithm: A procedure for transferring an i2c message on
106 * an i2c bus, along with a set of flags describing its functionality.
107 */
108 struct i2c_algorithm {
109 int (*master_xfer)(struct i2c_adapter *, struct i2c_msg *,
110 int);
111 uint32_t (*functionality)(struct i2c_adapter *);
112 };
113
114 /*
115 * struct i2c_lock_operations: i2c bus lock operations.
116 */
117 struct i2c_lock_operations {
118 void (*lock_bus)(struct i2c_adapter *, unsigned);
119 int (*trylock_bus)(struct i2c_adapter *, unsigned);
120 void (*unlock_bus)(struct i2c_adapter *, unsigned);
121 };
122
123 /*
124 * struct i2c_board_info: Parameters to find an i2c bus and a slave on
125 * it. type is the name of an i2c driver; addr is the slave address;
126 * platform_data is an extra parameter to pass to the i2c driver.
127 */
128 struct i2c_board_info {
129 char type[I2C_NAME_SIZE];
130 uint16_t addr;
131 uint16_t flags;
132 void *platform_data;
133 };
134
135 #define I2C_BOARD_INFO(board_type, board_addr) \
136 .type = (board_type), \
137 .addr = (board_addr)
138
139 /*
140 * struct i2c_client: An i2c slave device at a particular address on a
141 * particular bus.
142 */
143 struct i2c_client {
144 struct i2c_adapter *adapter;
145 uint16_t addr;
146 uint16_t flags;
147 };
148
149 /*
150 * struct i2c_device_id: Device id naming a class of i2c slave devices
151 * and parameters to the driver for the devices.
152 */
153 struct i2c_device_id {
154 char name[I2C_NAME_SIZE];
155 unsigned long driver_data;
156 };
157
158 /*
159 * struct i2c_driver: A driver for a class of i2c slave devices. We
160 * don't actually use this.
161 */
162 struct i2c_driver {
163 int (*probe)(struct i2c_client *, const struct i2c_device_id *);
164 int (*remove)(struct i2c_client *);
165 struct {
166 char name[I2C_NAME_SIZE];
167 const struct dev_pm_ops pm;
168 } driver;
169 };
170
171 /*
172 * Adapter management. We don't register these in a global database
173 * like Linux, so these are just stubs.
174 */
175 static inline int
i2c_add_adapter(struct i2c_adapter * adapter __unused)176 i2c_add_adapter(struct i2c_adapter *adapter __unused)
177 {
178
179 return 0;
180 }
181
182 static inline void
i2c_del_adapter(struct i2c_adapter * adapter __unused)183 i2c_del_adapter(struct i2c_adapter *adapter __unused)
184 {
185 }
186
187 static inline void *
i2c_get_adapdata(const struct i2c_adapter * adapter)188 i2c_get_adapdata(const struct i2c_adapter *adapter)
189 {
190
191 return adapter->i2ca_adapdata;
192 }
193
194 static inline void
i2c_set_adapdata(struct i2c_adapter * adapter,void * data)195 i2c_set_adapdata(struct i2c_adapter *adapter, void *data)
196 {
197
198 adapter->i2ca_adapdata = data;
199 }
200
201 /* XXX Make the nm output a little more greppable... */
202 #define __i2c_transfer linux___i2c_transfer
203 #define i2c_master_recv linux_i2c_master_recv
204 #define i2c_master_send linux_i2c_master_send
205 #define i2c_new_device linux_i2c_new_device
206 #define i2c_transfer linux_i2c_transfer
207 #define i2c_unregister_device linux_i2c_unregister_device
208
209 int i2c_master_send(const struct i2c_client *, const char *, int);
210 int i2c_master_recv(const struct i2c_client *, char *, int);
211 struct i2c_client *
212 i2c_new_device(struct i2c_adapter *, const struct i2c_board_info *);
213 int __i2c_transfer(struct i2c_adapter *, struct i2c_msg *, int);
214 int i2c_transfer(struct i2c_adapter *, struct i2c_msg *, int);
215 void i2c_unregister_device(struct i2c_client *);
216
217 #endif /* _LINUX_I2C_H_ */
218