linux_i2c.c revision 1.6 1 /* $NetBSD: linux_i2c.c,v 1.6 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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_i2c.c,v 1.6 2021/12/19 11:49:12 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
133 return (*adapter->algo->master_xfer)(adapter, msgs, n);
134 }
135
136 int
137 i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
138 {
139 int ret;
140
141 if (adapter->lock_ops)
142 (*adapter->lock_ops->lock_bus)(adapter, 0);
143 ret = __i2c_transfer(adapter, msgs, n);
144 if (adapter->lock_ops)
145 (*adapter->lock_ops->unlock_bus)(adapter, 0);
146
147 return ret;
148 }
149
150 static int
151 netbsd_i2c_transfer(i2c_tag_t i2c, struct i2c_msg *msgs, int n)
152 {
153 int i;
154 int error;
155
156 for (i = 0; i < n; i++) {
157 const i2c_op_t op = linux_i2c_flags_op(msgs[i].flags,
158 ((i + 1) == n));
159 const int flags = linux_i2c_flags_flags(msgs[i].flags);
160
161 switch (op) {
162 case I2C_OP_READ:
163 case I2C_OP_READ_WITH_STOP:
164 error = iic_exec(i2c, op, msgs[i].addr,
165 NULL, 0, msgs[i].buf, msgs[i].len, flags);
166 break;
167
168 case I2C_OP_WRITE:
169 case I2C_OP_WRITE_WITH_STOP:
170 error = iic_exec(i2c, op, msgs[i].addr,
171 msgs[i].buf, msgs[i].len, NULL, 0, flags);
172 break;
173
174 default:
175 error = EINVAL;
176 }
177
178 if (error)
179 /* XXX errno NetBSD->Linux */
180 return -error;
181 }
182
183 return n;
184 }
185
186 static i2c_op_t
187 linux_i2c_flags_op(uint16_t flags, bool stop)
188 {
189
190 if (ISSET(flags, I2C_M_STOP))
191 stop = true;
192
193 if (ISSET(flags, I2C_M_RD))
194 return (stop? I2C_OP_READ_WITH_STOP : I2C_OP_READ);
195 else
196 return (stop? I2C_OP_WRITE_WITH_STOP : I2C_OP_WRITE);
197 }
198
199 static int
200 linux_i2c_flags_flags(uint16_t flags __unused)
201 {
202
203 return 0;
204 }
205
206 /* Bit-banging */
208
209 const struct i2c_algorithm i2c_bit_algo = {
210 .master_xfer = linux_i2cbb_xfer,
211 .functionality = linux_i2cbb_functionality,
212 };
213
214 static uint32_t
215 linux_i2cbb_functionality(struct i2c_adapter *adapter __unused)
216 {
217 uint32_t functions = 0;
218
219 functions |= I2C_FUNC_I2C;
220 functions |= I2C_FUNC_NOSTART;
221 functions |= I2C_FUNC_SMBUS_EMUL;
222 functions |= I2C_FUNC_SMBUS_READ_BLOCK_DATA;
223 functions |= I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
224 #if 0
225 functions |= I2C_FUNC_10BIT_ADDR;
226 functions |= I2C_FUNC_PROTOCOL_MANGLING;
227 #endif
228
229 return functions;
230 }
231
232 static int
233 linux_i2cbb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
234 {
235 struct i2c_algo_bit_data *const abd = adapter->algo_data;
236 struct i2c_controller controller = {
237 .ic_cookie = abd,
238 .ic_send_start = linux_i2cbb_send_start,
239 .ic_send_stop = linux_i2cbb_send_stop,
240 .ic_initiate_xfer = linux_i2cbb_initiate_xfer,
241 .ic_read_byte = linux_i2cbb_read_byte,
242 .ic_write_byte = linux_i2cbb_write_byte,
243 };
244 i2c_tag_t i2c = &controller;
245 int error;
246
247 if (abd->pre_xfer) {
248 error = (*abd->pre_xfer)(adapter);
249 if (error)
250 return error;
251 }
252
253 error = netbsd_i2c_transfer(i2c, msgs, n);
254
255 if (abd->post_xfer)
256 (*abd->post_xfer)(adapter);
257
258 return error;
259 }
260
261 #define LI2CBB_SDA 0x01
263 #define LI2CBB_SCL 0x02
264 #define LI2CBB_INPUT 0x04
265 #define LI2CBB_OUTPUT 0x08
266
267 static struct i2c_bitbang_ops linux_i2cbb_ops = {
268 .ibo_set_bits = linux_i2cbb_set_bits,
269 .ibo_set_dir = linux_i2cbb_set_dir,
270 .ibo_read_bits = linux_i2cbb_read_bits,
271 .ibo_bits = {
272 [I2C_BIT_SDA] = LI2CBB_SDA,
273 [I2C_BIT_SCL] = LI2CBB_SCL,
274 [I2C_BIT_INPUT] = LI2CBB_INPUT,
275 [I2C_BIT_OUTPUT] = LI2CBB_OUTPUT,
276 },
277 };
278
279 static void
280 linux_i2cbb_set_bits(void *cookie, uint32_t bits)
281 {
282 struct i2c_algo_bit_data *const abd = cookie;
283
284 (*abd->setsda)(abd->data, (ISSET(bits, LI2CBB_SDA)? 1 : 0));
285 (*abd->setscl)(abd->data, (ISSET(bits, LI2CBB_SCL)? 1 : 0));
286 }
287
288 static uint32_t
289 linux_i2cbb_read_bits(void *cookie)
290 {
291 struct i2c_algo_bit_data *const abd = cookie;
292 uint32_t bits = 0;
293
294 if ((*abd->getsda)(abd->data))
295 bits |= LI2CBB_SDA;
296 if ((*abd->getscl)(abd->data))
297 bits |= LI2CBB_SCL;
298
299 return bits;
300 }
301
302 static void
303 linux_i2cbb_set_dir(void *cookie __unused, uint32_t bits __unused)
304 {
305 /* Linux doesn't do anything here... */
306 }
307
308 static int
310 linux_i2cbb_send_start(void *cookie, int flags)
311 {
312
313 return i2c_bitbang_send_start(cookie, flags, &linux_i2cbb_ops);
314 }
315
316 static int
317 linux_i2cbb_send_stop(void *cookie, int flags)
318 {
319
320 return i2c_bitbang_send_stop(cookie, flags, &linux_i2cbb_ops);
321 }
322
323 static int
324 linux_i2cbb_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
325 {
326
327 return i2c_bitbang_initiate_xfer(cookie, addr, flags,
328 &linux_i2cbb_ops);
329 }
330
331 static int
332 linux_i2cbb_read_byte(void *cookie, uint8_t *bytep, int flags)
333 {
334
335 return i2c_bitbang_read_byte(cookie, bytep, flags, &linux_i2cbb_ops);
336 }
337
338 static int
339 linux_i2cbb_write_byte(void *cookie, uint8_t byte, int flags)
340 {
341
342 return i2c_bitbang_write_byte(cookie, byte, flags, &linux_i2cbb_ops);
343 }
344