linux_i2c.c revision 1.1.2.1 1 1.1.2.1 riastrad /* $NetBSD: linux_i2c.c,v 1.1.2.1 2013/07/24 03:11:44 riastradh Exp $ */
2 1.1.2.1 riastrad
3 1.1.2.1 riastrad /*-
4 1.1.2.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1.2.1 riastrad * All rights reserved.
6 1.1.2.1 riastrad *
7 1.1.2.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 riastrad * by Taylor R. Campbell.
9 1.1.2.1 riastrad *
10 1.1.2.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 riastrad * modification, are permitted provided that the following conditions
12 1.1.2.1 riastrad * are met:
13 1.1.2.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1.2.1 riastrad *
19 1.1.2.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 riastrad */
31 1.1.2.1 riastrad
32 1.1.2.1 riastrad #include <sys/cdefs.h>
33 1.1.2.1 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_i2c.c,v 1.1.2.1 2013/07/24 03:11:44 riastradh Exp $");
34 1.1.2.1 riastrad
35 1.1.2.1 riastrad #include <sys/types.h>
36 1.1.2.1 riastrad #include <sys/errno.h>
37 1.1.2.1 riastrad #include <sys/queue.h> /* XXX include order botch: i2cvar.h needs */
38 1.1.2.1 riastrad
39 1.1.2.1 riastrad #include <dev/i2c/i2cvar.h>
40 1.1.2.1 riastrad #include <dev/i2c/i2c_bitbang.h> /* XXX include order botch */
41 1.1.2.1 riastrad
42 1.1.2.1 riastrad #include <linux/i2c.h>
43 1.1.2.1 riastrad #include <linux/i2c-algo-bit.h>
44 1.1.2.1 riastrad
45 1.1.2.1 riastrad static int netbsd_i2c_transfer(i2c_tag_t, struct i2c_msg *, int);
46 1.1.2.1 riastrad static i2c_op_t linux_i2c_flags_op(uint16_t, bool);
47 1.1.2.1 riastrad static int linux_i2c_flags_flags(uint16_t);
48 1.1.2.1 riastrad static uint32_t linux_i2cbb_functionality(struct i2c_adapter *);
49 1.1.2.1 riastrad static int linux_i2cbb_xfer(struct i2c_adapter *, struct i2c_msg *, int);
50 1.1.2.1 riastrad static void linux_i2cbb_set_bits(void *, uint32_t);
51 1.1.2.1 riastrad static uint32_t linux_i2cbb_read_bits(void *);
52 1.1.2.1 riastrad static void linux_i2cbb_set_dir(void *, uint32_t);
53 1.1.2.1 riastrad static int linux_i2cbb_send_start(void *, int);
54 1.1.2.1 riastrad static int linux_i2cbb_send_stop(void *, int);
55 1.1.2.1 riastrad static int linux_i2cbb_initiate_xfer(void *, i2c_addr_t, int);
56 1.1.2.1 riastrad static int linux_i2cbb_read_byte(void *, uint8_t *, int);
57 1.1.2.1 riastrad static int linux_i2cbb_write_byte(void *, uint8_t, int);
58 1.1.2.1 riastrad
59 1.1.2.1 riastrad int
61 1.1.2.1 riastrad i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
62 1.1.2.1 riastrad {
63 1.1.2.1 riastrad
64 1.1.2.1 riastrad return (*adapter->algo->master_xfer)(adapter, msgs, n);
65 1.1.2.1 riastrad }
66 1.1.2.1 riastrad
67 1.1.2.1 riastrad static int
68 1.1.2.1 riastrad netbsd_i2c_transfer(i2c_tag_t i2c, struct i2c_msg *msgs, int n)
69 1.1.2.1 riastrad {
70 1.1.2.1 riastrad int i;
71 1.1.2.1 riastrad int error;
72 1.1.2.1 riastrad
73 1.1.2.1 riastrad for (i = 0; i < n; i++) {
74 1.1.2.1 riastrad const i2c_op_t op = linux_i2c_flags_op(msgs[i].flags,
75 1.1.2.1 riastrad ((i + 1) == n));
76 1.1.2.1 riastrad const int flags = linux_i2c_flags_flags(msgs[i].flags);
77 1.1.2.1 riastrad
78 1.1.2.1 riastrad switch (op) {
79 1.1.2.1 riastrad case I2C_OP_READ:
80 1.1.2.1 riastrad case I2C_OP_READ_WITH_STOP:
81 1.1.2.1 riastrad error = iic_exec(i2c, op, msgs[i].addr,
82 1.1.2.1 riastrad NULL, 0, msgs[i].buf, msgs[i].len, flags);
83 1.1.2.1 riastrad break;
84 1.1.2.1 riastrad
85 1.1.2.1 riastrad case I2C_OP_WRITE:
86 1.1.2.1 riastrad case I2C_OP_WRITE_WITH_STOP:
87 1.1.2.1 riastrad error = iic_exec(i2c, op, msgs[i].addr,
88 1.1.2.1 riastrad msgs[i].buf, msgs[i].len, NULL, 0, flags);
89 1.1.2.1 riastrad break;
90 1.1.2.1 riastrad
91 1.1.2.1 riastrad default:
92 1.1.2.1 riastrad error = EINVAL;
93 1.1.2.1 riastrad }
94 1.1.2.1 riastrad
95 1.1.2.1 riastrad if (error)
96 1.1.2.1 riastrad /* XXX errno NetBSD->Linux */
97 1.1.2.1 riastrad return -error;
98 1.1.2.1 riastrad }
99 1.1.2.1 riastrad
100 1.1.2.1 riastrad return n;
101 1.1.2.1 riastrad }
102 1.1.2.1 riastrad
103 1.1.2.1 riastrad static i2c_op_t
104 1.1.2.1 riastrad linux_i2c_flags_op(uint16_t flags, bool stop)
105 1.1.2.1 riastrad {
106 1.1.2.1 riastrad
107 1.1.2.1 riastrad if (ISSET(flags, I2C_M_RD))
108 1.1.2.1 riastrad return (stop? I2C_OP_READ_WITH_STOP : I2C_OP_READ);
109 1.1.2.1 riastrad else
110 1.1.2.1 riastrad return (stop? I2C_OP_WRITE_WITH_STOP : I2C_OP_WRITE);
111 1.1.2.1 riastrad }
112 1.1.2.1 riastrad
113 1.1.2.1 riastrad static int
114 1.1.2.1 riastrad linux_i2c_flags_flags(uint16_t flags __unused)
115 1.1.2.1 riastrad {
116 1.1.2.1 riastrad
117 1.1.2.1 riastrad return 0;
118 1.1.2.1 riastrad }
119 1.1.2.1 riastrad
120 1.1.2.1 riastrad const struct i2c_algorithm i2c_bit_algo = {
122 1.1.2.1 riastrad .master_xfer = linux_i2cbb_xfer,
123 1.1.2.1 riastrad .functionality = linux_i2cbb_functionality,
124 1.1.2.1 riastrad };
125 1.1.2.1 riastrad
126 1.1.2.1 riastrad static uint32_t
127 1.1.2.1 riastrad linux_i2cbb_functionality(struct i2c_adapter *adapter __unused)
128 1.1.2.1 riastrad {
129 1.1.2.1 riastrad uint32_t functions = 0;
130 1.1.2.1 riastrad
131 1.1.2.1 riastrad functions |= I2C_FUNC_I2C;
132 1.1.2.1 riastrad functions |= I2C_FUNC_NOSTART;
133 1.1.2.1 riastrad functions |= I2C_FUNC_SMBUS_EMUL;
134 1.1.2.1 riastrad functions |= I2C_FUNC_SMBUS_READ_BLOCK_DATA;
135 1.1.2.1 riastrad functions |= I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
136 1.1.2.1 riastrad #if 0
137 1.1.2.1 riastrad functions |= I2C_FUNC_10BIT_ADDR;
138 1.1.2.1 riastrad functions |= I2C_FUNC_PROTOCOL_MANGLING;
139 1.1.2.1 riastrad #endif
140 1.1.2.1 riastrad
141 1.1.2.1 riastrad return functions;
142 1.1.2.1 riastrad }
143 1.1.2.1 riastrad
144 1.1.2.1 riastrad static int
145 1.1.2.1 riastrad linux_i2cbb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
146 1.1.2.1 riastrad {
147 1.1.2.1 riastrad struct i2c_algo_bit_data *const abd = adapter->algo_data;
148 1.1.2.1 riastrad struct i2c_controller controller = {
149 1.1.2.1 riastrad .ic_cookie = abd,
150 1.1.2.1 riastrad .ic_send_start = linux_i2cbb_send_start,
151 1.1.2.1 riastrad .ic_send_stop = linux_i2cbb_send_stop,
152 1.1.2.1 riastrad .ic_initiate_xfer = linux_i2cbb_initiate_xfer,
153 1.1.2.1 riastrad .ic_read_byte = linux_i2cbb_read_byte,
154 1.1.2.1 riastrad .ic_write_byte = linux_i2cbb_write_byte,
155 1.1.2.1 riastrad };
156 1.1.2.1 riastrad i2c_tag_t i2c = &controller;
157 1.1.2.1 riastrad int error;
158 1.1.2.1 riastrad
159 1.1.2.1 riastrad if (abd->pre_xfer) {
160 1.1.2.1 riastrad error = (*abd->pre_xfer)(adapter);
161 1.1.2.1 riastrad if (error)
162 1.1.2.1 riastrad return error;
163 1.1.2.1 riastrad }
164 1.1.2.1 riastrad
165 1.1.2.1 riastrad error = netbsd_i2c_transfer(i2c, msgs, n);
166 1.1.2.1 riastrad
167 1.1.2.1 riastrad if (abd->post_xfer)
168 1.1.2.1 riastrad (*abd->post_xfer)(adapter);
169 1.1.2.1 riastrad
170 1.1.2.1 riastrad return error;
171 1.1.2.1 riastrad }
172 1.1.2.1 riastrad
173 1.1.2.1 riastrad #define LI2CBB_SDA 0x01
175 1.1.2.1 riastrad #define LI2CBB_SCL 0x02
176 1.1.2.1 riastrad #define LI2CBB_INPUT 0x04
177 1.1.2.1 riastrad #define LI2CBB_OUTPUT 0x08
178 1.1.2.1 riastrad
179 1.1.2.1 riastrad static struct i2c_bitbang_ops linux_i2cbb_ops = {
180 1.1.2.1 riastrad .ibo_set_bits = linux_i2cbb_set_bits,
181 1.1.2.1 riastrad .ibo_set_dir = linux_i2cbb_set_dir,
182 1.1.2.1 riastrad .ibo_read_bits = linux_i2cbb_read_bits,
183 1.1.2.1 riastrad .ibo_bits = {
184 1.1.2.1 riastrad [I2C_BIT_SDA] = LI2CBB_SDA,
185 1.1.2.1 riastrad [I2C_BIT_SCL] = LI2CBB_SCL,
186 1.1.2.1 riastrad [I2C_BIT_INPUT] = LI2CBB_INPUT,
187 1.1.2.1 riastrad [I2C_BIT_OUTPUT] = LI2CBB_OUTPUT,
188 1.1.2.1 riastrad },
189 1.1.2.1 riastrad };
190 1.1.2.1 riastrad
191 1.1.2.1 riastrad static void
192 1.1.2.1 riastrad linux_i2cbb_set_bits(void *cookie, uint32_t bits)
193 1.1.2.1 riastrad {
194 1.1.2.1 riastrad struct i2c_algo_bit_data *const abd = cookie;
195 1.1.2.1 riastrad
196 1.1.2.1 riastrad (*abd->setsda)(abd->data, (ISSET(bits, LI2CBB_SDA)? 1 : 0));
197 1.1.2.1 riastrad (*abd->setscl)(abd->data, (ISSET(bits, LI2CBB_SCL)? 1 : 0));
198 1.1.2.1 riastrad }
199 1.1.2.1 riastrad
200 1.1.2.1 riastrad static uint32_t
201 1.1.2.1 riastrad linux_i2cbb_read_bits(void *cookie)
202 1.1.2.1 riastrad {
203 1.1.2.1 riastrad struct i2c_algo_bit_data *const abd = cookie;
204 1.1.2.1 riastrad uint32_t bits = 0;
205 1.1.2.1 riastrad
206 1.1.2.1 riastrad if ((*abd->getsda)(abd->data))
207 1.1.2.1 riastrad bits |= LI2CBB_SDA;
208 1.1.2.1 riastrad if ((*abd->getscl)(abd->data))
209 1.1.2.1 riastrad bits |= LI2CBB_SCL;
210 1.1.2.1 riastrad
211 1.1.2.1 riastrad return bits;
212 1.1.2.1 riastrad }
213 1.1.2.1 riastrad
214 1.1.2.1 riastrad static void
215 1.1.2.1 riastrad linux_i2cbb_set_dir(void *cookie __unused, uint32_t bits __unused)
216 1.1.2.1 riastrad {
217 1.1.2.1 riastrad /* Linux doesn't do anything here... */
218 1.1.2.1 riastrad }
219 1.1.2.1 riastrad
220 1.1.2.1 riastrad static int
222 1.1.2.1 riastrad linux_i2cbb_send_start(void *cookie, int flags)
223 1.1.2.1 riastrad {
224 1.1.2.1 riastrad
225 1.1.2.1 riastrad return i2c_bitbang_send_start(cookie, flags, &linux_i2cbb_ops);
226 1.1.2.1 riastrad }
227 1.1.2.1 riastrad
228 1.1.2.1 riastrad static int
229 1.1.2.1 riastrad linux_i2cbb_send_stop(void *cookie, int flags)
230 1.1.2.1 riastrad {
231 1.1.2.1 riastrad
232 1.1.2.1 riastrad return i2c_bitbang_send_stop(cookie, flags, &linux_i2cbb_ops);
233 1.1.2.1 riastrad }
234 1.1.2.1 riastrad
235 1.1.2.1 riastrad static int
236 1.1.2.1 riastrad linux_i2cbb_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
237 1.1.2.1 riastrad {
238 1.1.2.1 riastrad
239 1.1.2.1 riastrad return i2c_bitbang_initiate_xfer(cookie, addr, flags,
240 1.1.2.1 riastrad &linux_i2cbb_ops);
241 1.1.2.1 riastrad }
242 1.1.2.1 riastrad
243 1.1.2.1 riastrad static int
244 1.1.2.1 riastrad linux_i2cbb_read_byte(void *cookie, uint8_t *bytep, int flags)
245 1.1.2.1 riastrad {
246 1.1.2.1 riastrad
247 1.1.2.1 riastrad return i2c_bitbang_read_byte(cookie, bytep, flags, &linux_i2cbb_ops);
248 1.1.2.1 riastrad }
249 1.1.2.1 riastrad
250 1.1.2.1 riastrad static int
251 1.1.2.1 riastrad linux_i2cbb_write_byte(void *cookie, uint8_t byte, int flags)
252 {
253
254 return i2c_bitbang_write_byte(cookie, byte, flags, &linux_i2cbb_ops);
255 }
256