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