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