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