i2c.h revision 1.8 1 1.8 riastrad /* $NetBSD: i2c.h,v 1.8 2015/03/05 17:29:18 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.8 riastrad
44 1.2 riastrad struct i2c_adapter;
45 1.2 riastrad struct i2c_algorithm;
46 1.2 riastrad struct i2c_msg;
47 1.2 riastrad
48 1.2 riastrad #define I2C_NAME_SIZE 20
49 1.2 riastrad
50 1.8 riastrad /*
51 1.8 riastrad * I2C_M_*: i2c_msg flags
52 1.8 riastrad */
53 1.8 riastrad #define I2C_M_RD 0x01 /* xfer is read, not write */
54 1.8 riastrad #define I2C_M_NOSTART 0x02 /* don't initiate xfer */
55 1.8 riastrad #define I2C_M_TEN 0x04 /* 10-bit chip address */
56 1.8 riastrad
57 1.8 riastrad /*
58 1.8 riastrad * I2C_CLASS_*: i2c_adapter classes
59 1.8 riastrad */
60 1.2 riastrad #define I2C_CLASS_DDC 0x01
61 1.2 riastrad
62 1.8 riastrad /*
63 1.8 riastrad * I2C_FUNC_*: i2c_adapter functionality bits
64 1.8 riastrad */
65 1.8 riastrad #define I2C_FUNC_I2C 0x01
66 1.8 riastrad #define I2C_FUNC_NOSTART 0x02
67 1.8 riastrad #define I2C_FUNC_SMBUS_EMUL 0x04
68 1.8 riastrad #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x08
69 1.8 riastrad #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x10
70 1.8 riastrad #define I2C_FUNC_10BIT_ADDR 0x20
71 1.8 riastrad
72 1.8 riastrad /*
73 1.8 riastrad * struct i2c_msg: A single i2c message request on a particular
74 1.8 riastrad * address. Read if I2C_M_RD is set, write otherwise.
75 1.8 riastrad */
76 1.8 riastrad struct i2c_msg {
77 1.8 riastrad i2c_addr_t addr;
78 1.8 riastrad uint16_t flags; /* I2C_M_* */
79 1.8 riastrad uint16_t len;
80 1.8 riastrad uint8_t *buf;
81 1.8 riastrad };
82 1.8 riastrad
83 1.8 riastrad /*
84 1.8 riastrad * struct i2c_adapter: An i2c bus controller.
85 1.8 riastrad */
86 1.8 riastrad struct i2c_adapter {
87 1.8 riastrad char name[I2C_NAME_SIZE];
88 1.8 riastrad const struct i2c_algorithm *algo;
89 1.8 riastrad void *algo_data;
90 1.8 riastrad int retries;
91 1.8 riastrad struct module *owner;
92 1.8 riastrad unsigned int class; /* I2C_CLASS_* */
93 1.8 riastrad struct {
94 1.8 riastrad device_t parent;
95 1.8 riastrad } dev;
96 1.8 riastrad void *i2ca_adapdata;
97 1.8 riastrad };
98 1.8 riastrad
99 1.8 riastrad /*
100 1.8 riastrad * struct i2c_algorithm: A procedure for transferring an i2c message on
101 1.8 riastrad * an i2c bus, along with a set of flags describing its functionality.
102 1.8 riastrad */
103 1.8 riastrad struct i2c_algorithm {
104 1.8 riastrad int (*master_xfer)(struct i2c_adapter *, struct i2c_msg *,
105 1.8 riastrad int);
106 1.8 riastrad uint32_t (*functionality)(struct i2c_adapter *);
107 1.8 riastrad };
108 1.8 riastrad
109 1.8 riastrad /*
110 1.8 riastrad * struct i2c_board_info: Parameters to find an i2c bus and a slave on
111 1.8 riastrad * it. type is the name of an i2c driver; addr is the slave address;
112 1.8 riastrad * platform_data is an extra parameter to pass to the i2c driver.
113 1.8 riastrad */
114 1.3 riastrad struct i2c_board_info {
115 1.3 riastrad char type[I2C_NAME_SIZE];
116 1.3 riastrad uint16_t addr;
117 1.8 riastrad uint16_t flags;
118 1.5 riastrad void *platform_data;
119 1.3 riastrad };
120 1.3 riastrad
121 1.6 riastrad #define I2C_BOARD_INFO(board_type, board_addr) \
122 1.6 riastrad .type = (board_type), \
123 1.6 riastrad .addr = (board_addr)
124 1.6 riastrad
125 1.8 riastrad /*
126 1.8 riastrad * struct i2c_client: An i2c slave device at a particular address on a
127 1.8 riastrad * particular bus.
128 1.8 riastrad */
129 1.8 riastrad struct i2c_client {
130 1.8 riastrad struct i2c_adapter *adapter;
131 1.8 riastrad uint16_t addr;
132 1.8 riastrad uint16_t flags;
133 1.8 riastrad };
134 1.3 riastrad
135 1.8 riastrad /*
136 1.8 riastrad * struct i2c_device_id: Device id naming a class of i2c slave devices
137 1.8 riastrad * and parameters to the driver for the devices.
138 1.8 riastrad */
139 1.8 riastrad struct i2c_device_id {
140 1.8 riastrad char name[I2C_NAME_SIZE];
141 1.8 riastrad unsigned long driver_data;
142 1.4 riastrad };
143 1.4 riastrad
144 1.8 riastrad /*
145 1.8 riastrad * struct i2c_driver: A driver for a class of i2c slave devices. We
146 1.8 riastrad * don't actually use this.
147 1.8 riastrad */
148 1.8 riastrad struct i2c_driver {
149 1.8 riastrad int (*probe)(struct i2c_client *, const struct i2c_device_id *);
150 1.8 riastrad int (*remove)(struct i2c_client *);
151 1.2 riastrad struct {
152 1.8 riastrad char name[I2C_NAME_SIZE];
153 1.8 riastrad const struct dev_pm_ops pm;
154 1.8 riastrad } driver;
155 1.2 riastrad };
156 1.2 riastrad
157 1.8 riastrad /*
158 1.8 riastrad * Adapter management. We don't register these in a global database
159 1.8 riastrad * like Linux, so these are just stubs.
160 1.8 riastrad */
161 1.2 riastrad static inline int
162 1.2 riastrad i2c_add_adapter(struct i2c_adapter *adapter __unused)
163 1.2 riastrad {
164 1.8 riastrad
165 1.2 riastrad return 0;
166 1.2 riastrad }
167 1.2 riastrad
168 1.2 riastrad static inline void
169 1.2 riastrad i2c_del_adapter(struct i2c_adapter *adapter __unused)
170 1.2 riastrad {
171 1.2 riastrad }
172 1.2 riastrad
173 1.3 riastrad static inline void *
174 1.3 riastrad i2c_get_adapdata(const struct i2c_adapter *adapter)
175 1.3 riastrad {
176 1.3 riastrad
177 1.3 riastrad return adapter->i2ca_adapdata;
178 1.3 riastrad }
179 1.3 riastrad
180 1.3 riastrad static inline void
181 1.3 riastrad i2c_set_adapdata(struct i2c_adapter *adapter, void *data)
182 1.3 riastrad {
183 1.3 riastrad
184 1.3 riastrad adapter->i2ca_adapdata = data;
185 1.3 riastrad }
186 1.3 riastrad
187 1.2 riastrad /* XXX Make the nm output a little more greppable... */
188 1.8 riastrad #define i2c_master_recv linux_i2c_master_recv
189 1.8 riastrad #define i2c_master_send linux_i2c_master_send
190 1.8 riastrad #define i2c_new_device linux_i2c_new_device
191 1.8 riastrad #define i2c_transfer linux_i2c_transfer
192 1.8 riastrad #define i2c_unregister_device linux_i2c_unregister_device
193 1.8 riastrad
194 1.8 riastrad int i2c_master_send(const struct i2c_client *, const char *, int);
195 1.8 riastrad int i2c_master_recv(const struct i2c_client *, char *, int);
196 1.8 riastrad struct i2c_client *
197 1.8 riastrad i2c_new_device(struct i2c_adapter *, const struct i2c_board_info *);
198 1.2 riastrad int i2c_transfer(struct i2c_adapter *, struct i2c_msg *, int);
199 1.8 riastrad void i2c_unregister_device(struct i2c_client *);
200 1.2 riastrad
201 1.2 riastrad #endif /* _LINUX_I2C_H_ */
202