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