linux_i2c.c revision 1.5 1 /* $NetBSD: linux_i2c.c,v 1.5 2021/12/19 09:43:56 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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_i2c.c,v 1.5 2021/12/19 09:43:56 riastradh Exp $");
34
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/kmem.h>
38 #include <sys/queue.h> /* XXX include order botch: i2cvar.h needs */
39
40 #include <dev/i2c/i2cvar.h>
41 #include <dev/i2c/i2c_bitbang.h> /* XXX include order botch */
42
43 #include <linux/i2c.h>
44 #include <linux/i2c-algo-bit.h>
45
46 static int netbsd_i2c_transfer(i2c_tag_t, struct i2c_msg *, int);
47 static i2c_op_t linux_i2c_flags_op(uint16_t, bool);
48 static int linux_i2c_flags_flags(uint16_t);
49 static uint32_t linux_i2cbb_functionality(struct i2c_adapter *);
50 static int linux_i2cbb_xfer(struct i2c_adapter *, struct i2c_msg *, int);
51 static void linux_i2cbb_set_bits(void *, uint32_t);
52 static uint32_t linux_i2cbb_read_bits(void *);
53 static void linux_i2cbb_set_dir(void *, uint32_t);
54 static int linux_i2cbb_send_start(void *, int);
55 static int linux_i2cbb_send_stop(void *, int);
56 static int linux_i2cbb_initiate_xfer(void *, i2c_addr_t, int);
57 static int linux_i2cbb_read_byte(void *, uint8_t *, int);
58 static int linux_i2cbb_write_byte(void *, uint8_t, int);
59
60 /*
62 * Client operations: operations with a particular i2c slave device.
63 */
64
65 struct i2c_client *
66 i2c_new_device(struct i2c_adapter *adapter, const struct i2c_board_info *info)
67 {
68 struct i2c_client *client;
69
70 client = kmem_alloc(sizeof(*client), KM_SLEEP);
71 client->adapter = adapter;
72 client->addr = info->addr;
73 client->flags = info->flags;
74
75 return client;
76 }
77
78 void
79 i2c_unregister_device(struct i2c_client *client)
80 {
81
82 kmem_free(client, sizeof(*client));
83 }
84
85 int
86 i2c_master_send(const struct i2c_client *client, const char *buf, int count)
87 {
88 struct i2c_msg msg = {
89 .addr = client->addr,
90 .flags = client->flags & I2C_M_TEN,
91 .len = count,
92 .buf = __UNCONST(buf),
93 };
94 int ret;
95
96 KASSERT(0 <= count);
97
98 ret = i2c_transfer(client->adapter, &msg, 1);
99 if (ret <= 0)
100 return ret;
101
102 return count;
103 }
104
105 int
106 i2c_master_recv(const struct i2c_client *client, char *buf, int count)
107 {
108 struct i2c_msg msg = {
109 .addr = client->addr,
110 .flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
111 .len = count,
112 .buf = buf,
113 };
114 int ret;
115
116 ret = i2c_transfer(client->adapter, &msg, 1);
117 if (ret <= 0)
118 return ret;
119
120 return count;
121 }
122
123 /*
125 * Adapter operations: operations over an i2c bus via a particular
126 * controller.
127 */
128
129 int
130 i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
131 {
132 int ret;
133
134 if (adapter->lock_ops)
135 (*adapter->lock_ops->lock_bus)(adapter, 0);
136 ret = (*adapter->algo->master_xfer)(adapter, msgs, n);
137 if (adapter->lock_ops)
138 (*adapter->lock_ops->unlock_bus)(adapter, 0);
139
140 return ret;
141 }
142
143 static int
144 netbsd_i2c_transfer(i2c_tag_t i2c, struct i2c_msg *msgs, int n)
145 {
146 int i;
147 int error;
148
149 for (i = 0; i < n; i++) {
150 const i2c_op_t op = linux_i2c_flags_op(msgs[i].flags,
151 ((i + 1) == n));
152 const int flags = linux_i2c_flags_flags(msgs[i].flags);
153
154 switch (op) {
155 case I2C_OP_READ:
156 case I2C_OP_READ_WITH_STOP:
157 error = iic_exec(i2c, op, msgs[i].addr,
158 NULL, 0, msgs[i].buf, msgs[i].len, flags);
159 break;
160
161 case I2C_OP_WRITE:
162 case I2C_OP_WRITE_WITH_STOP:
163 error = iic_exec(i2c, op, msgs[i].addr,
164 msgs[i].buf, msgs[i].len, NULL, 0, flags);
165 break;
166
167 default:
168 error = EINVAL;
169 }
170
171 if (error)
172 /* XXX errno NetBSD->Linux */
173 return -error;
174 }
175
176 return n;
177 }
178
179 static i2c_op_t
180 linux_i2c_flags_op(uint16_t flags, bool stop)
181 {
182
183 if (ISSET(flags, I2C_M_STOP))
184 stop = true;
185
186 if (ISSET(flags, I2C_M_RD))
187 return (stop? I2C_OP_READ_WITH_STOP : I2C_OP_READ);
188 else
189 return (stop? I2C_OP_WRITE_WITH_STOP : I2C_OP_WRITE);
190 }
191
192 static int
193 linux_i2c_flags_flags(uint16_t flags __unused)
194 {
195
196 return 0;
197 }
198
199 /* Bit-banging */
201
202 const struct i2c_algorithm i2c_bit_algo = {
203 .master_xfer = linux_i2cbb_xfer,
204 .functionality = linux_i2cbb_functionality,
205 };
206
207 static uint32_t
208 linux_i2cbb_functionality(struct i2c_adapter *adapter __unused)
209 {
210 uint32_t functions = 0;
211
212 functions |= I2C_FUNC_I2C;
213 functions |= I2C_FUNC_NOSTART;
214 functions |= I2C_FUNC_SMBUS_EMUL;
215 functions |= I2C_FUNC_SMBUS_READ_BLOCK_DATA;
216 functions |= I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
217 #if 0
218 functions |= I2C_FUNC_10BIT_ADDR;
219 functions |= I2C_FUNC_PROTOCOL_MANGLING;
220 #endif
221
222 return functions;
223 }
224
225 static int
226 linux_i2cbb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
227 {
228 struct i2c_algo_bit_data *const abd = adapter->algo_data;
229 struct i2c_controller controller = {
230 .ic_cookie = abd,
231 .ic_send_start = linux_i2cbb_send_start,
232 .ic_send_stop = linux_i2cbb_send_stop,
233 .ic_initiate_xfer = linux_i2cbb_initiate_xfer,
234 .ic_read_byte = linux_i2cbb_read_byte,
235 .ic_write_byte = linux_i2cbb_write_byte,
236 };
237 i2c_tag_t i2c = &controller;
238 int error;
239
240 if (abd->pre_xfer) {
241 error = (*abd->pre_xfer)(adapter);
242 if (error)
243 return error;
244 }
245
246 error = netbsd_i2c_transfer(i2c, msgs, n);
247
248 if (abd->post_xfer)
249 (*abd->post_xfer)(adapter);
250
251 return error;
252 }
253
254 #define LI2CBB_SDA 0x01
256 #define LI2CBB_SCL 0x02
257 #define LI2CBB_INPUT 0x04
258 #define LI2CBB_OUTPUT 0x08
259
260 static struct i2c_bitbang_ops linux_i2cbb_ops = {
261 .ibo_set_bits = linux_i2cbb_set_bits,
262 .ibo_set_dir = linux_i2cbb_set_dir,
263 .ibo_read_bits = linux_i2cbb_read_bits,
264 .ibo_bits = {
265 [I2C_BIT_SDA] = LI2CBB_SDA,
266 [I2C_BIT_SCL] = LI2CBB_SCL,
267 [I2C_BIT_INPUT] = LI2CBB_INPUT,
268 [I2C_BIT_OUTPUT] = LI2CBB_OUTPUT,
269 },
270 };
271
272 static void
273 linux_i2cbb_set_bits(void *cookie, uint32_t bits)
274 {
275 struct i2c_algo_bit_data *const abd = cookie;
276
277 (*abd->setsda)(abd->data, (ISSET(bits, LI2CBB_SDA)? 1 : 0));
278 (*abd->setscl)(abd->data, (ISSET(bits, LI2CBB_SCL)? 1 : 0));
279 }
280
281 static uint32_t
282 linux_i2cbb_read_bits(void *cookie)
283 {
284 struct i2c_algo_bit_data *const abd = cookie;
285 uint32_t bits = 0;
286
287 if ((*abd->getsda)(abd->data))
288 bits |= LI2CBB_SDA;
289 if ((*abd->getscl)(abd->data))
290 bits |= LI2CBB_SCL;
291
292 return bits;
293 }
294
295 static void
296 linux_i2cbb_set_dir(void *cookie __unused, uint32_t bits __unused)
297 {
298 /* Linux doesn't do anything here... */
299 }
300
301 static int
303 linux_i2cbb_send_start(void *cookie, int flags)
304 {
305
306 return i2c_bitbang_send_start(cookie, flags, &linux_i2cbb_ops);
307 }
308
309 static int
310 linux_i2cbb_send_stop(void *cookie, int flags)
311 {
312
313 return i2c_bitbang_send_stop(cookie, flags, &linux_i2cbb_ops);
314 }
315
316 static int
317 linux_i2cbb_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
318 {
319
320 return i2c_bitbang_initiate_xfer(cookie, addr, flags,
321 &linux_i2cbb_ops);
322 }
323
324 static int
325 linux_i2cbb_read_byte(void *cookie, uint8_t *bytep, int flags)
326 {
327
328 return i2c_bitbang_read_byte(cookie, bytep, flags, &linux_i2cbb_ops);
329 }
330
331 static int
332 linux_i2cbb_write_byte(void *cookie, uint8_t byte, int flags)
333 {
334
335 return i2c_bitbang_write_byte(cookie, byte, flags, &linux_i2cbb_ops);
336 }
337