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