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