Home | History | Annotate | Line # | Download | only in linux
      1 /*	$NetBSD: linux_i2c.c,v 1.7 2022/05/22 18:41:14 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.7 2022/05/22 18:41:14 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 /*
     62  * Client operations: operations with a particular i2c slave device.
     63  */
     64 
     65 struct i2c_client *
     66 i2c_new_device(struct i2c_adapter *adapter, const struct i2c_board_info *info)
     67 {
     68 	struct i2c_client *client;
     69 
     70 	client = kmem_alloc(sizeof(*client), KM_SLEEP);
     71 	client->adapter = adapter;
     72 	client->addr = info->addr;
     73 	client->flags = info->flags;
     74 
     75 	return client;
     76 }
     77 
     78 void
     79 i2c_unregister_device(struct i2c_client *client)
     80 {
     81 
     82 	kmem_free(client, sizeof(*client));
     83 }
     84 
     85 int
     86 i2c_master_send(const struct i2c_client *client, const char *buf, int count)
     87 {
     88 	struct i2c_msg msg = {
     89 		.addr = client->addr,
     90 		.flags = client->flags & I2C_M_TEN,
     91 		.len = count,
     92 		.buf = __UNCONST(buf),
     93 	};
     94 	int ret;
     95 
     96 	KASSERT(0 <= count);
     97 
     98 	ret = i2c_transfer(client->adapter, &msg, 1);
     99 	if (ret <= 0)
    100 		return ret;
    101 
    102 	return count;
    103 }
    104 
    105 int
    106 i2c_master_recv(const struct i2c_client *client, char *buf, int count)
    107 {
    108 	struct i2c_msg msg = {
    109 		.addr = client->addr,
    110 		.flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
    111 		.len = count,
    112 		.buf = buf,
    113 	};
    114 	int ret;
    115 
    116 	ret = i2c_transfer(client->adapter, &msg, 1);
    117 	if (ret <= 0)
    118 		return ret;
    119 
    120 	return count;
    121 }
    122 
    123 /*
    125  * Adapter operations: operations over an i2c bus via a particular
    126  * controller.
    127  */
    128 
    129 int
    130 __i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
    131 {
    132 	unsigned timeout = hz;	/* XXX adapter->timeout */
    133 	unsigned start = getticks();
    134 	int ret, nretries = 0;
    135 
    136 	do {
    137 		ret = (*adapter->algo->master_xfer)(adapter, msgs, n);
    138 		if (ret != -EAGAIN)
    139 			break;
    140 	} while (nretries++ < adapter->retries &&
    141 	    getticks() - start < timeout);
    142 
    143 	return ret;
    144 }
    145 
    146 int
    147 i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
    148 {
    149 	int ret;
    150 
    151 	if (adapter->lock_ops)
    152 		(*adapter->lock_ops->lock_bus)(adapter, 0);
    153 	ret = __i2c_transfer(adapter, msgs, n);
    154 	if (adapter->lock_ops)
    155 		(*adapter->lock_ops->unlock_bus)(adapter, 0);
    156 
    157 	return ret;
    158 }
    159 
    160 static int
    161 netbsd_i2c_transfer(i2c_tag_t i2c, struct i2c_msg *msgs, int n)
    162 {
    163 	int i;
    164 	int error;
    165 
    166 	for (i = 0; i < n; i++) {
    167 		const i2c_op_t op = linux_i2c_flags_op(msgs[i].flags,
    168 		    ((i + 1) == n));
    169 		const int flags = linux_i2c_flags_flags(msgs[i].flags);
    170 
    171 		switch (op) {
    172 		case I2C_OP_READ:
    173 		case I2C_OP_READ_WITH_STOP:
    174 			error = iic_exec(i2c, op, msgs[i].addr,
    175 			    NULL, 0, msgs[i].buf, msgs[i].len, flags);
    176 			break;
    177 
    178 		case I2C_OP_WRITE:
    179 		case I2C_OP_WRITE_WITH_STOP:
    180 			error = iic_exec(i2c, op, msgs[i].addr,
    181 			    msgs[i].buf, msgs[i].len, NULL, 0, flags);
    182 			break;
    183 
    184 		default:
    185 			error = EINVAL;
    186 		}
    187 
    188 		if (error)
    189 			/* XXX errno NetBSD->Linux */
    190 			return -error;
    191 	}
    192 
    193 	return n;
    194 }
    195 
    196 static i2c_op_t
    197 linux_i2c_flags_op(uint16_t flags, bool stop)
    198 {
    199 
    200 	if (ISSET(flags, I2C_M_STOP))
    201 		stop = true;
    202 
    203 	if (ISSET(flags, I2C_M_RD))
    204 		return (stop? I2C_OP_READ_WITH_STOP : I2C_OP_READ);
    205 	else
    206 		return (stop? I2C_OP_WRITE_WITH_STOP : I2C_OP_WRITE);
    207 }
    208 
    209 static int
    210 linux_i2c_flags_flags(uint16_t flags __unused)
    211 {
    212 
    213 	return 0;
    214 }
    215 
    216 /* Bit-banging */
    218 
    219 const struct i2c_algorithm i2c_bit_algo = {
    220 	.master_xfer	= linux_i2cbb_xfer,
    221 	.functionality	= linux_i2cbb_functionality,
    222 };
    223 
    224 static uint32_t
    225 linux_i2cbb_functionality(struct i2c_adapter *adapter __unused)
    226 {
    227 	uint32_t functions = 0;
    228 
    229 	functions |= I2C_FUNC_I2C;
    230 	functions |= I2C_FUNC_NOSTART;
    231 	functions |= I2C_FUNC_SMBUS_EMUL;
    232 	functions |= I2C_FUNC_SMBUS_READ_BLOCK_DATA;
    233 	functions |= I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
    234 #if 0
    235 	functions |= I2C_FUNC_10BIT_ADDR;
    236 	functions |= I2C_FUNC_PROTOCOL_MANGLING;
    237 #endif
    238 
    239 	return functions;
    240 }
    241 
    242 static int
    243 linux_i2cbb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
    244 {
    245 	struct i2c_algo_bit_data *const abd = adapter->algo_data;
    246 	struct i2c_controller controller = {
    247 		.ic_cookie		= abd,
    248 		.ic_send_start		= linux_i2cbb_send_start,
    249 		.ic_send_stop		= linux_i2cbb_send_stop,
    250 		.ic_initiate_xfer	= linux_i2cbb_initiate_xfer,
    251 		.ic_read_byte		= linux_i2cbb_read_byte,
    252 		.ic_write_byte		= linux_i2cbb_write_byte,
    253 	};
    254 	i2c_tag_t i2c = &controller;
    255 	int error;
    256 
    257 	if (abd->pre_xfer) {
    258 		error = (*abd->pre_xfer)(adapter);
    259 		if (error)
    260 			return error;
    261 	}
    262 
    263 	error = netbsd_i2c_transfer(i2c, msgs, n);
    264 
    265 	if (abd->post_xfer)
    266 		(*abd->post_xfer)(adapter);
    267 
    268 	return error;
    269 }
    270 
    271 #define	LI2CBB_SDA	0x01
    273 #define	LI2CBB_SCL	0x02
    274 #define	LI2CBB_INPUT	0x04
    275 #define	LI2CBB_OUTPUT	0x08
    276 
    277 static struct i2c_bitbang_ops linux_i2cbb_ops = {
    278 	.ibo_set_bits	= linux_i2cbb_set_bits,
    279 	.ibo_set_dir	= linux_i2cbb_set_dir,
    280 	.ibo_read_bits	= linux_i2cbb_read_bits,
    281 	.ibo_bits	= {
    282 		[I2C_BIT_SDA]		= LI2CBB_SDA,
    283 		[I2C_BIT_SCL]		= LI2CBB_SCL,
    284 		[I2C_BIT_INPUT]		= LI2CBB_INPUT,
    285 		[I2C_BIT_OUTPUT]	= LI2CBB_OUTPUT,
    286 	},
    287 };
    288 
    289 static void
    290 linux_i2cbb_set_bits(void *cookie, uint32_t bits)
    291 {
    292 	struct i2c_algo_bit_data *const abd = cookie;
    293 
    294 	(*abd->setsda)(abd->data, (ISSET(bits, LI2CBB_SDA)? 1 : 0));
    295 	(*abd->setscl)(abd->data, (ISSET(bits, LI2CBB_SCL)? 1 : 0));
    296 }
    297 
    298 static uint32_t
    299 linux_i2cbb_read_bits(void *cookie)
    300 {
    301 	struct i2c_algo_bit_data *const abd = cookie;
    302 	uint32_t bits = 0;
    303 
    304 	if ((*abd->getsda)(abd->data))
    305 		bits |= LI2CBB_SDA;
    306 	if ((*abd->getscl)(abd->data))
    307 		bits |= LI2CBB_SCL;
    308 
    309 	return bits;
    310 }
    311 
    312 static void
    313 linux_i2cbb_set_dir(void *cookie __unused, uint32_t bits __unused)
    314 {
    315 	/* Linux doesn't do anything here...  */
    316 }
    317 
    318 static int
    320 linux_i2cbb_send_start(void *cookie, int flags)
    321 {
    322 
    323 	return i2c_bitbang_send_start(cookie, flags, &linux_i2cbb_ops);
    324 }
    325 
    326 static int
    327 linux_i2cbb_send_stop(void *cookie, int flags)
    328 {
    329 
    330 	return i2c_bitbang_send_stop(cookie, flags, &linux_i2cbb_ops);
    331 }
    332 
    333 static int
    334 linux_i2cbb_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    335 {
    336 
    337 	return i2c_bitbang_initiate_xfer(cookie, addr, flags,
    338 	    &linux_i2cbb_ops);
    339 }
    340 
    341 static int
    342 linux_i2cbb_read_byte(void *cookie, uint8_t *bytep, int flags)
    343 {
    344 
    345 	return i2c_bitbang_read_byte(cookie, bytep, flags, &linux_i2cbb_ops);
    346 }
    347 
    348 static int
    349 linux_i2cbb_write_byte(void *cookie, uint8_t byte, int flags)
    350 {
    351 
    352 	return i2c_bitbang_write_byte(cookie, byte, flags, &linux_i2cbb_ops);
    353 }
    354