1/* $NetBSD: linux_i2c.c,v 1.3 2015/03/05 17:29:18 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.3 2015/03/05 17:29:18 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
46static int netbsd_i2c_transfer(i2c_tag_t, struct i2c_msg *, int);
47static i2c_op_t linux_i2c_flags_op(uint16_t, bool);
48static int linux_i2c_flags_flags(uint16_t);
49static uint32_t linux_i2cbb_functionality(struct i2c_adapter *);
50static int linux_i2cbb_xfer(struct i2c_adapter *, struct i2c_msg *, int);
51static void linux_i2cbb_set_bits(void *, uint32_t);
52static uint32_t linux_i2cbb_read_bits(void *);
53static void linux_i2cbb_set_dir(void *, uint32_t);
54static int linux_i2cbb_send_start(void *, int);
55static int linux_i2cbb_send_stop(void *, int);
56static int linux_i2cbb_initiate_xfer(void *, i2c_addr_t, int);
57static int linux_i2cbb_read_byte(void *, uint8_t *, int);
58static int linux_i2cbb_write_byte(void *, uint8_t, int);
59
60/*
61 * Client operations: operations with a particular i2c slave device.
62 */
63
64struct i2c_client *
65i2c_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
77void
78i2c_unregister_device(struct i2c_client *client)
79{
80
81 kmem_free(client, sizeof(*client));
82}
83
84int
85i2c_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
104int
105i2c_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
127int
128i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
129{
130
131 return (*adapter->algo->master_xfer)(adapter, msgs, n);
132}
133
134static int
135netbsd_i2c_transfer(i2c_tag_t i2c, struct i2c_msg *msgs, int n)
136{
137 int i;
138 int error;
139
140 for (i = 0; i < n; i++) {
141 const i2c_op_t op = linux_i2c_flags_op(msgs[i].flags,
142 ((i + 1) == n));
143 const int flags = linux_i2c_flags_flags(msgs[i].flags);
144
145 switch (op) {
146 case I2C_OP_READ:
147 case I2C_OP_READ_WITH_STOP:
148 error = iic_exec(i2c, op, msgs[i].addr,
149 NULL, 0, msgs[i].buf, msgs[i].len, flags);
150 break;
151
152 case I2C_OP_WRITE:
153 case I2C_OP_WRITE_WITH_STOP:
154 error = iic_exec(i2c, op, msgs[i].addr,
155 msgs[i].buf, msgs[i].len, NULL, 0, flags);
156 break;
157
158 default:
159 error = EINVAL;
160 }
161
162 if (error)
163 /* XXX errno NetBSD->Linux */
164 return -error;
165 }
166
167 return n;
168}
169
170static i2c_op_t
171linux_i2c_flags_op(uint16_t flags, bool stop)
172{
173
174 if (ISSET(flags, I2C_M_RD))
175 return (stop? I2C_OP_READ_WITH_STOP : I2C_OP_READ);
176 else
177 return (stop? I2C_OP_WRITE_WITH_STOP : I2C_OP_WRITE);
178}
179
180static int
181linux_i2c_flags_flags(uint16_t flags __unused)
182{
183
184 return 0;
185}
186
187/* Bit-banging */
188
189const struct i2c_algorithm i2c_bit_algo = {
190 .master_xfer = linux_i2cbb_xfer,
191 .functionality = linux_i2cbb_functionality,
192};
193
194static uint32_t
195linux_i2cbb_functionality(struct i2c_adapter *adapter __unused)
196{
197 uint32_t functions = 0;
198
199 functions |= I2C_FUNC_I2C;
200 functions |= I2C_FUNC_NOSTART;
201 functions |= I2C_FUNC_SMBUS_EMUL;
202 functions |= I2C_FUNC_SMBUS_READ_BLOCK_DATA;
203 functions |= I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
204#if 0
205 functions |= I2C_FUNC_10BIT_ADDR;
206 functions |= I2C_FUNC_PROTOCOL_MANGLING;
207#endif
208
209 return functions;
210}
211
212static int
213linux_i2cbb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
214{
215 struct i2c_algo_bit_data *const abd = adapter->algo_data;
216 struct i2c_controller controller = {
217 .ic_cookie = abd,
218 .ic_send_start = linux_i2cbb_send_start,
219 .ic_send_stop = linux_i2cbb_send_stop,
220 .ic_initiate_xfer = linux_i2cbb_initiate_xfer,
221 .ic_read_byte = linux_i2cbb_read_byte,
222 .ic_write_byte = linux_i2cbb_write_byte,
223 };
224 i2c_tag_t i2c = &controller;
225 int error;
226
227 if (abd->pre_xfer) {
228 error = (*abd->pre_xfer)(adapter);
229 if (error)
230 return error;
231 }
232
233 error = netbsd_i2c_transfer(i2c, msgs, n);
234
235 if (abd->post_xfer)
236 (*abd->post_xfer)(adapter);
237
238 return error;
239}
240
241#define LI2CBB_SDA 0x01
242#define LI2CBB_SCL 0x02
243#define LI2CBB_INPUT 0x04
244#define LI2CBB_OUTPUT 0x08
245
246static struct i2c_bitbang_ops linux_i2cbb_ops = {
247 .ibo_set_bits = linux_i2cbb_set_bits,
248 .ibo_set_dir = linux_i2cbb_set_dir,
249 .ibo_read_bits = linux_i2cbb_read_bits,
250 .ibo_bits = {
251 [I2C_BIT_SDA] = LI2CBB_SDA,
252 [I2C_BIT_SCL] = LI2CBB_SCL,
253 [I2C_BIT_INPUT] = LI2CBB_INPUT,
254 [I2C_BIT_OUTPUT] = LI2CBB_OUTPUT,
255 },
256};
257
258static void
259linux_i2cbb_set_bits(void *cookie, uint32_t bits)
260{
261 struct i2c_algo_bit_data *const abd = cookie;
262
263 (*abd->setsda)(abd->data, (ISSET(bits, LI2CBB_SDA)? 1 : 0));
264 (*abd->setscl)(abd->data, (ISSET(bits, LI2CBB_SCL)? 1 : 0));
265}
266
267static uint32_t
268linux_i2cbb_read_bits(void *cookie)
269{
270 struct i2c_algo_bit_data *const abd = cookie;
271 uint32_t bits = 0;
272
273 if ((*abd->getsda)(abd->data))
274 bits |= LI2CBB_SDA;
275 if ((*abd->getscl)(abd->data))
276 bits |= LI2CBB_SCL;
277
278 return bits;
279}
280
281static void
282linux_i2cbb_set_dir(void *cookie __unused, uint32_t bits __unused)
283{
284 /* Linux doesn't do anything here... */
285}
286
287static int
288linux_i2cbb_send_start(void *cookie, int flags)
289{
290
291 return i2c_bitbang_send_start(cookie, flags, &linux_i2cbb_ops);
292}
293
294static int
295linux_i2cbb_send_stop(void *cookie, int flags)
296{
297
298 return i2c_bitbang_send_stop(cookie, flags, &linux_i2cbb_ops);
299}
300
301static int
302linux_i2cbb_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
303{
304
305 return i2c_bitbang_initiate_xfer(cookie, addr, flags,
306 &linux_i2cbb_ops);
307}
308
309static int
310linux_i2cbb_read_byte(void *cookie, uint8_t *bytep, int flags)
311{
312
313 return i2c_bitbang_read_byte(cookie, bytep, flags, &linux_i2cbb_ops);
314}
315
316static int
317linux_i2cbb_write_byte(void *cookie, uint8_t byte, int flags)
318{
319
320 return i2c_bitbang_write_byte(cookie, byte, flags, &linux_i2cbb_ops);
321}
322