linux_i2c.c revision 1.2.4.2 1 /* $NetBSD: linux_i2c.c,v 1.2.4.2 2014/05/18 17:46:01 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2013 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.2.4.2 2014/05/18 17:46:01 rmind Exp $");
34
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/queue.h> /* XXX include order botch: i2cvar.h needs */
38
39 #include <dev/i2c/i2cvar.h>
40 #include <dev/i2c/i2c_bitbang.h> /* XXX include order botch */
41
42 #include <linux/i2c.h>
43 #include <linux/i2c-algo-bit.h>
44
45 static int netbsd_i2c_transfer(i2c_tag_t, struct i2c_msg *, int);
46 static i2c_op_t linux_i2c_flags_op(uint16_t, bool);
47 static int linux_i2c_flags_flags(uint16_t);
48 static uint32_t linux_i2cbb_functionality(struct i2c_adapter *);
49 static int linux_i2cbb_xfer(struct i2c_adapter *, struct i2c_msg *, int);
50 static void linux_i2cbb_set_bits(void *, uint32_t);
51 static uint32_t linux_i2cbb_read_bits(void *);
52 static void linux_i2cbb_set_dir(void *, uint32_t);
53 static int linux_i2cbb_send_start(void *, int);
54 static int linux_i2cbb_send_stop(void *, int);
55 static int linux_i2cbb_initiate_xfer(void *, i2c_addr_t, int);
56 static int linux_i2cbb_read_byte(void *, uint8_t *, int);
57 static int linux_i2cbb_write_byte(void *, uint8_t, int);
58
59 int
61 i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
62 {
63
64 return (*adapter->algo->master_xfer)(adapter, msgs, n);
65 }
66
67 static int
68 netbsd_i2c_transfer(i2c_tag_t i2c, struct i2c_msg *msgs, int n)
69 {
70 int i;
71 int error;
72
73 for (i = 0; i < n; i++) {
74 const i2c_op_t op = linux_i2c_flags_op(msgs[i].flags,
75 ((i + 1) == n));
76 const int flags = linux_i2c_flags_flags(msgs[i].flags);
77
78 switch (op) {
79 case I2C_OP_READ:
80 case I2C_OP_READ_WITH_STOP:
81 error = iic_exec(i2c, op, msgs[i].addr,
82 NULL, 0, msgs[i].buf, msgs[i].len, flags);
83 break;
84
85 case I2C_OP_WRITE:
86 case I2C_OP_WRITE_WITH_STOP:
87 error = iic_exec(i2c, op, msgs[i].addr,
88 msgs[i].buf, msgs[i].len, NULL, 0, flags);
89 break;
90
91 default:
92 error = EINVAL;
93 }
94
95 if (error)
96 /* XXX errno NetBSD->Linux */
97 return -error;
98 }
99
100 return n;
101 }
102
103 static i2c_op_t
104 linux_i2c_flags_op(uint16_t flags, bool stop)
105 {
106
107 if (ISSET(flags, I2C_M_RD))
108 return (stop? I2C_OP_READ_WITH_STOP : I2C_OP_READ);
109 else
110 return (stop? I2C_OP_WRITE_WITH_STOP : I2C_OP_WRITE);
111 }
112
113 static int
114 linux_i2c_flags_flags(uint16_t flags __unused)
115 {
116
117 return 0;
118 }
119
120 const struct i2c_algorithm i2c_bit_algo = {
122 .master_xfer = linux_i2cbb_xfer,
123 .functionality = linux_i2cbb_functionality,
124 };
125
126 static uint32_t
127 linux_i2cbb_functionality(struct i2c_adapter *adapter __unused)
128 {
129 uint32_t functions = 0;
130
131 functions |= I2C_FUNC_I2C;
132 functions |= I2C_FUNC_NOSTART;
133 functions |= I2C_FUNC_SMBUS_EMUL;
134 functions |= I2C_FUNC_SMBUS_READ_BLOCK_DATA;
135 functions |= I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
136 #if 0
137 functions |= I2C_FUNC_10BIT_ADDR;
138 functions |= I2C_FUNC_PROTOCOL_MANGLING;
139 #endif
140
141 return functions;
142 }
143
144 static int
145 linux_i2cbb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
146 {
147 struct i2c_algo_bit_data *const abd = adapter->algo_data;
148 struct i2c_controller controller = {
149 .ic_cookie = abd,
150 .ic_send_start = linux_i2cbb_send_start,
151 .ic_send_stop = linux_i2cbb_send_stop,
152 .ic_initiate_xfer = linux_i2cbb_initiate_xfer,
153 .ic_read_byte = linux_i2cbb_read_byte,
154 .ic_write_byte = linux_i2cbb_write_byte,
155 };
156 i2c_tag_t i2c = &controller;
157 int error;
158
159 if (abd->pre_xfer) {
160 error = (*abd->pre_xfer)(adapter);
161 if (error)
162 return error;
163 }
164
165 error = netbsd_i2c_transfer(i2c, msgs, n);
166
167 if (abd->post_xfer)
168 (*abd->post_xfer)(adapter);
169
170 return error;
171 }
172
173 #define LI2CBB_SDA 0x01
175 #define LI2CBB_SCL 0x02
176 #define LI2CBB_INPUT 0x04
177 #define LI2CBB_OUTPUT 0x08
178
179 static struct i2c_bitbang_ops linux_i2cbb_ops = {
180 .ibo_set_bits = linux_i2cbb_set_bits,
181 .ibo_set_dir = linux_i2cbb_set_dir,
182 .ibo_read_bits = linux_i2cbb_read_bits,
183 .ibo_bits = {
184 [I2C_BIT_SDA] = LI2CBB_SDA,
185 [I2C_BIT_SCL] = LI2CBB_SCL,
186 [I2C_BIT_INPUT] = LI2CBB_INPUT,
187 [I2C_BIT_OUTPUT] = LI2CBB_OUTPUT,
188 },
189 };
190
191 static void
192 linux_i2cbb_set_bits(void *cookie, uint32_t bits)
193 {
194 struct i2c_algo_bit_data *const abd = cookie;
195
196 (*abd->setsda)(abd->data, (ISSET(bits, LI2CBB_SDA)? 1 : 0));
197 (*abd->setscl)(abd->data, (ISSET(bits, LI2CBB_SCL)? 1 : 0));
198 }
199
200 static uint32_t
201 linux_i2cbb_read_bits(void *cookie)
202 {
203 struct i2c_algo_bit_data *const abd = cookie;
204 uint32_t bits = 0;
205
206 if ((*abd->getsda)(abd->data))
207 bits |= LI2CBB_SDA;
208 if ((*abd->getscl)(abd->data))
209 bits |= LI2CBB_SCL;
210
211 return bits;
212 }
213
214 static void
215 linux_i2cbb_set_dir(void *cookie __unused, uint32_t bits __unused)
216 {
217 /* Linux doesn't do anything here... */
218 }
219
220 static int
222 linux_i2cbb_send_start(void *cookie, int flags)
223 {
224
225 return i2c_bitbang_send_start(cookie, flags, &linux_i2cbb_ops);
226 }
227
228 static int
229 linux_i2cbb_send_stop(void *cookie, int flags)
230 {
231
232 return i2c_bitbang_send_stop(cookie, flags, &linux_i2cbb_ops);
233 }
234
235 static int
236 linux_i2cbb_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
237 {
238
239 return i2c_bitbang_initiate_xfer(cookie, addr, flags,
240 &linux_i2cbb_ops);
241 }
242
243 static int
244 linux_i2cbb_read_byte(void *cookie, uint8_t *bytep, int flags)
245 {
246
247 return i2c_bitbang_read_byte(cookie, bytep, flags, &linux_i2cbb_ops);
248 }
249
250 static int
251 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