Home | History | Annotate | Line # | Download | only in linux
linux_i2c.c revision 1.2.10.3
      1  1.2.10.2       tls /*	$NetBSD: linux_i2c.c,v 1.2.10.3 2017/12/03 11:38:00 jdolecek Exp $	*/
      2  1.2.10.2       tls 
      3  1.2.10.2       tls /*-
      4  1.2.10.3  jdolecek  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  1.2.10.2       tls  * All rights reserved.
      6  1.2.10.2       tls  *
      7  1.2.10.2       tls  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.10.2       tls  * by Taylor R. Campbell.
      9  1.2.10.2       tls  *
     10  1.2.10.2       tls  * Redistribution and use in source and binary forms, with or without
     11  1.2.10.2       tls  * modification, are permitted provided that the following conditions
     12  1.2.10.2       tls  * are met:
     13  1.2.10.2       tls  * 1. Redistributions of source code must retain the above copyright
     14  1.2.10.2       tls  *    notice, this list of conditions and the following disclaimer.
     15  1.2.10.2       tls  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.10.2       tls  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.10.2       tls  *    documentation and/or other materials provided with the distribution.
     18  1.2.10.2       tls  *
     19  1.2.10.2       tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2.10.2       tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2.10.2       tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2.10.2       tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2.10.2       tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2.10.2       tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2.10.2       tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2.10.2       tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2.10.2       tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2.10.2       tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2.10.2       tls  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2.10.2       tls  */
     31  1.2.10.2       tls 
     32  1.2.10.2       tls #include <sys/cdefs.h>
     33  1.2.10.2       tls __KERNEL_RCSID(0, "$NetBSD: linux_i2c.c,v 1.2.10.3 2017/12/03 11:38:00 jdolecek Exp $");
     34  1.2.10.2       tls 
     35  1.2.10.2       tls #include <sys/types.h>
     36  1.2.10.2       tls #include <sys/errno.h>
     37  1.2.10.3  jdolecek #include <sys/kmem.h>
     38  1.2.10.2       tls #include <sys/queue.h>		/* XXX include order botch: i2cvar.h needs */
     39  1.2.10.2       tls 
     40  1.2.10.2       tls #include <dev/i2c/i2cvar.h>
     41  1.2.10.2       tls #include <dev/i2c/i2c_bitbang.h> /* XXX include order botch */
     42  1.2.10.2       tls 
     43  1.2.10.2       tls #include <linux/i2c.h>
     44  1.2.10.2       tls #include <linux/i2c-algo-bit.h>
     45  1.2.10.2       tls 
     46  1.2.10.2       tls static int	netbsd_i2c_transfer(i2c_tag_t, struct i2c_msg *, int);
     47  1.2.10.2       tls static i2c_op_t	linux_i2c_flags_op(uint16_t, bool);
     48  1.2.10.2       tls static int	linux_i2c_flags_flags(uint16_t);
     49  1.2.10.2       tls static uint32_t	linux_i2cbb_functionality(struct i2c_adapter *);
     50  1.2.10.2       tls static int	linux_i2cbb_xfer(struct i2c_adapter *, struct i2c_msg *, int);
     51  1.2.10.2       tls static void	linux_i2cbb_set_bits(void *, uint32_t);
     52  1.2.10.2       tls static uint32_t	linux_i2cbb_read_bits(void *);
     53  1.2.10.2       tls static void	linux_i2cbb_set_dir(void *, uint32_t);
     54  1.2.10.2       tls static int	linux_i2cbb_send_start(void *, int);
     55  1.2.10.2       tls static int	linux_i2cbb_send_stop(void *, int);
     56  1.2.10.2       tls static int	linux_i2cbb_initiate_xfer(void *, i2c_addr_t, int);
     57  1.2.10.2       tls static int	linux_i2cbb_read_byte(void *, uint8_t *, int);
     58  1.2.10.2       tls static int	linux_i2cbb_write_byte(void *, uint8_t, int);
     59  1.2.10.2       tls 
     60  1.2.10.3  jdolecek /*
     62  1.2.10.3  jdolecek  * Client operations: operations with a particular i2c slave device.
     63  1.2.10.3  jdolecek  */
     64  1.2.10.3  jdolecek 
     65  1.2.10.3  jdolecek struct i2c_client *
     66  1.2.10.3  jdolecek i2c_new_device(struct i2c_adapter *adapter, const struct i2c_board_info *info)
     67  1.2.10.3  jdolecek {
     68  1.2.10.3  jdolecek 	struct i2c_client *client;
     69  1.2.10.3  jdolecek 
     70  1.2.10.3  jdolecek 	client = kmem_alloc(sizeof(*client), KM_SLEEP);
     71  1.2.10.3  jdolecek 	client->adapter = adapter;
     72  1.2.10.3  jdolecek 	client->addr = info->addr;
     73  1.2.10.3  jdolecek 	client->flags = info->flags;
     74  1.2.10.3  jdolecek 
     75  1.2.10.3  jdolecek 	return client;
     76  1.2.10.3  jdolecek }
     77  1.2.10.3  jdolecek 
     78  1.2.10.3  jdolecek void
     79  1.2.10.3  jdolecek i2c_unregister_device(struct i2c_client *client)
     80  1.2.10.3  jdolecek {
     81  1.2.10.3  jdolecek 
     82  1.2.10.3  jdolecek 	kmem_free(client, sizeof(*client));
     83  1.2.10.3  jdolecek }
     84  1.2.10.3  jdolecek 
     85  1.2.10.3  jdolecek int
     86  1.2.10.3  jdolecek i2c_master_send(const struct i2c_client *client, const char *buf, int count)
     87  1.2.10.3  jdolecek {
     88  1.2.10.3  jdolecek 	struct i2c_msg msg = {
     89  1.2.10.3  jdolecek 		.addr = client->addr,
     90  1.2.10.3  jdolecek 		.flags = client->flags & I2C_M_TEN,
     91  1.2.10.3  jdolecek 		.len = count,
     92  1.2.10.3  jdolecek 		.buf = __UNCONST(buf),
     93  1.2.10.3  jdolecek 	};
     94  1.2.10.3  jdolecek 	int ret;
     95  1.2.10.3  jdolecek 
     96  1.2.10.3  jdolecek 	KASSERT(0 <= count);
     97  1.2.10.3  jdolecek 
     98  1.2.10.3  jdolecek 	ret = i2c_transfer(client->adapter, &msg, 1);
     99  1.2.10.3  jdolecek 	if (ret <= 0)
    100  1.2.10.3  jdolecek 		return ret;
    101  1.2.10.3  jdolecek 
    102  1.2.10.3  jdolecek 	return count;
    103  1.2.10.3  jdolecek }
    104  1.2.10.3  jdolecek 
    105  1.2.10.3  jdolecek int
    106  1.2.10.3  jdolecek i2c_master_recv(const struct i2c_client *client, char *buf, int count)
    107  1.2.10.3  jdolecek {
    108  1.2.10.3  jdolecek 	struct i2c_msg msg = {
    109  1.2.10.3  jdolecek 		.addr = client->addr,
    110  1.2.10.3  jdolecek 		.flags = (client->flags & I2C_M_TEN) | I2C_M_RD,
    111  1.2.10.3  jdolecek 		.len = count,
    112  1.2.10.3  jdolecek 		.buf = buf,
    113  1.2.10.3  jdolecek 	};
    114  1.2.10.3  jdolecek 	int ret;
    115  1.2.10.3  jdolecek 
    116  1.2.10.3  jdolecek 	ret = i2c_transfer(client->adapter, &msg, 1);
    117  1.2.10.3  jdolecek 	if (ret <= 0)
    118  1.2.10.3  jdolecek 		return ret;
    119  1.2.10.3  jdolecek 
    120  1.2.10.3  jdolecek 	return count;
    121  1.2.10.3  jdolecek }
    122  1.2.10.3  jdolecek 
    123  1.2.10.3  jdolecek /*
    125  1.2.10.3  jdolecek  * Adapter operations: operations over an i2c bus via a particular
    126  1.2.10.3  jdolecek  * controller.
    127  1.2.10.2       tls  */
    128  1.2.10.2       tls 
    129  1.2.10.2       tls int
    130  1.2.10.2       tls i2c_transfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
    131  1.2.10.2       tls {
    132  1.2.10.2       tls 
    133  1.2.10.2       tls 	return (*adapter->algo->master_xfer)(adapter, msgs, n);
    134  1.2.10.2       tls }
    135  1.2.10.2       tls 
    136  1.2.10.2       tls static int
    137  1.2.10.2       tls netbsd_i2c_transfer(i2c_tag_t i2c, struct i2c_msg *msgs, int n)
    138  1.2.10.2       tls {
    139  1.2.10.2       tls 	int i;
    140  1.2.10.2       tls 	int error;
    141  1.2.10.2       tls 
    142  1.2.10.2       tls 	for (i = 0; i < n; i++) {
    143  1.2.10.2       tls 		const i2c_op_t op = linux_i2c_flags_op(msgs[i].flags,
    144  1.2.10.2       tls 		    ((i + 1) == n));
    145  1.2.10.2       tls 		const int flags = linux_i2c_flags_flags(msgs[i].flags);
    146  1.2.10.2       tls 
    147  1.2.10.2       tls 		switch (op) {
    148  1.2.10.2       tls 		case I2C_OP_READ:
    149  1.2.10.2       tls 		case I2C_OP_READ_WITH_STOP:
    150  1.2.10.2       tls 			error = iic_exec(i2c, op, msgs[i].addr,
    151  1.2.10.2       tls 			    NULL, 0, msgs[i].buf, msgs[i].len, flags);
    152  1.2.10.2       tls 			break;
    153  1.2.10.2       tls 
    154  1.2.10.2       tls 		case I2C_OP_WRITE:
    155  1.2.10.2       tls 		case I2C_OP_WRITE_WITH_STOP:
    156  1.2.10.2       tls 			error = iic_exec(i2c, op, msgs[i].addr,
    157  1.2.10.2       tls 			    msgs[i].buf, msgs[i].len, NULL, 0, flags);
    158  1.2.10.2       tls 			break;
    159  1.2.10.2       tls 
    160  1.2.10.2       tls 		default:
    161  1.2.10.2       tls 			error = EINVAL;
    162  1.2.10.2       tls 		}
    163  1.2.10.2       tls 
    164  1.2.10.2       tls 		if (error)
    165  1.2.10.2       tls 			/* XXX errno NetBSD->Linux */
    166  1.2.10.2       tls 			return -error;
    167  1.2.10.2       tls 	}
    168  1.2.10.2       tls 
    169  1.2.10.2       tls 	return n;
    170  1.2.10.2       tls }
    171  1.2.10.2       tls 
    172  1.2.10.2       tls static i2c_op_t
    173  1.2.10.2       tls linux_i2c_flags_op(uint16_t flags, bool stop)
    174  1.2.10.2       tls {
    175  1.2.10.2       tls 
    176  1.2.10.2       tls 	if (ISSET(flags, I2C_M_RD))
    177  1.2.10.2       tls 		return (stop? I2C_OP_READ_WITH_STOP : I2C_OP_READ);
    178  1.2.10.2       tls 	else
    179  1.2.10.2       tls 		return (stop? I2C_OP_WRITE_WITH_STOP : I2C_OP_WRITE);
    180  1.2.10.2       tls }
    181  1.2.10.2       tls 
    182  1.2.10.2       tls static int
    183  1.2.10.2       tls linux_i2c_flags_flags(uint16_t flags __unused)
    184  1.2.10.2       tls {
    185  1.2.10.2       tls 
    186  1.2.10.2       tls 	return 0;
    187  1.2.10.3  jdolecek }
    188  1.2.10.3  jdolecek 
    189  1.2.10.2       tls /* Bit-banging */
    191  1.2.10.2       tls 
    192  1.2.10.2       tls const struct i2c_algorithm i2c_bit_algo = {
    193  1.2.10.2       tls 	.master_xfer	= linux_i2cbb_xfer,
    194  1.2.10.2       tls 	.functionality	= linux_i2cbb_functionality,
    195  1.2.10.2       tls };
    196  1.2.10.2       tls 
    197  1.2.10.2       tls static uint32_t
    198  1.2.10.2       tls linux_i2cbb_functionality(struct i2c_adapter *adapter __unused)
    199  1.2.10.2       tls {
    200  1.2.10.2       tls 	uint32_t functions = 0;
    201  1.2.10.2       tls 
    202  1.2.10.2       tls 	functions |= I2C_FUNC_I2C;
    203  1.2.10.2       tls 	functions |= I2C_FUNC_NOSTART;
    204  1.2.10.2       tls 	functions |= I2C_FUNC_SMBUS_EMUL;
    205  1.2.10.2       tls 	functions |= I2C_FUNC_SMBUS_READ_BLOCK_DATA;
    206  1.2.10.2       tls 	functions |= I2C_FUNC_SMBUS_BLOCK_PROC_CALL;
    207  1.2.10.2       tls #if 0
    208  1.2.10.2       tls 	functions |= I2C_FUNC_10BIT_ADDR;
    209  1.2.10.2       tls 	functions |= I2C_FUNC_PROTOCOL_MANGLING;
    210  1.2.10.2       tls #endif
    211  1.2.10.2       tls 
    212  1.2.10.2       tls 	return functions;
    213  1.2.10.2       tls }
    214  1.2.10.2       tls 
    215  1.2.10.2       tls static int
    216  1.2.10.2       tls linux_i2cbb_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int n)
    217  1.2.10.2       tls {
    218  1.2.10.2       tls 	struct i2c_algo_bit_data *const abd = adapter->algo_data;
    219  1.2.10.2       tls 	struct i2c_controller controller = {
    220  1.2.10.2       tls 		.ic_cookie		= abd,
    221  1.2.10.2       tls 		.ic_send_start		= linux_i2cbb_send_start,
    222  1.2.10.2       tls 		.ic_send_stop		= linux_i2cbb_send_stop,
    223  1.2.10.2       tls 		.ic_initiate_xfer	= linux_i2cbb_initiate_xfer,
    224  1.2.10.2       tls 		.ic_read_byte		= linux_i2cbb_read_byte,
    225  1.2.10.2       tls 		.ic_write_byte		= linux_i2cbb_write_byte,
    226  1.2.10.2       tls 	};
    227  1.2.10.2       tls 	i2c_tag_t i2c = &controller;
    228  1.2.10.2       tls 	int error;
    229  1.2.10.2       tls 
    230  1.2.10.2       tls 	if (abd->pre_xfer) {
    231  1.2.10.2       tls 		error = (*abd->pre_xfer)(adapter);
    232  1.2.10.2       tls 		if (error)
    233  1.2.10.2       tls 			return error;
    234  1.2.10.2       tls 	}
    235  1.2.10.2       tls 
    236  1.2.10.2       tls 	error = netbsd_i2c_transfer(i2c, msgs, n);
    237  1.2.10.2       tls 
    238  1.2.10.2       tls 	if (abd->post_xfer)
    239  1.2.10.2       tls 		(*abd->post_xfer)(adapter);
    240  1.2.10.2       tls 
    241  1.2.10.2       tls 	return error;
    242  1.2.10.2       tls }
    243  1.2.10.2       tls 
    244  1.2.10.2       tls #define	LI2CBB_SDA	0x01
    246  1.2.10.2       tls #define	LI2CBB_SCL	0x02
    247  1.2.10.2       tls #define	LI2CBB_INPUT	0x04
    248  1.2.10.2       tls #define	LI2CBB_OUTPUT	0x08
    249  1.2.10.2       tls 
    250  1.2.10.2       tls static struct i2c_bitbang_ops linux_i2cbb_ops = {
    251  1.2.10.2       tls 	.ibo_set_bits	= linux_i2cbb_set_bits,
    252  1.2.10.2       tls 	.ibo_set_dir	= linux_i2cbb_set_dir,
    253  1.2.10.2       tls 	.ibo_read_bits	= linux_i2cbb_read_bits,
    254  1.2.10.2       tls 	.ibo_bits	= {
    255  1.2.10.2       tls 		[I2C_BIT_SDA]		= LI2CBB_SDA,
    256  1.2.10.2       tls 		[I2C_BIT_SCL]		= LI2CBB_SCL,
    257  1.2.10.2       tls 		[I2C_BIT_INPUT]		= LI2CBB_INPUT,
    258  1.2.10.2       tls 		[I2C_BIT_OUTPUT]	= LI2CBB_OUTPUT,
    259  1.2.10.2       tls 	},
    260  1.2.10.2       tls };
    261  1.2.10.2       tls 
    262  1.2.10.2       tls static void
    263  1.2.10.2       tls linux_i2cbb_set_bits(void *cookie, uint32_t bits)
    264  1.2.10.2       tls {
    265  1.2.10.2       tls 	struct i2c_algo_bit_data *const abd = cookie;
    266  1.2.10.2       tls 
    267  1.2.10.2       tls 	(*abd->setsda)(abd->data, (ISSET(bits, LI2CBB_SDA)? 1 : 0));
    268  1.2.10.2       tls 	(*abd->setscl)(abd->data, (ISSET(bits, LI2CBB_SCL)? 1 : 0));
    269  1.2.10.2       tls }
    270  1.2.10.2       tls 
    271  1.2.10.2       tls static uint32_t
    272  1.2.10.2       tls linux_i2cbb_read_bits(void *cookie)
    273  1.2.10.2       tls {
    274  1.2.10.2       tls 	struct i2c_algo_bit_data *const abd = cookie;
    275  1.2.10.2       tls 	uint32_t bits = 0;
    276  1.2.10.2       tls 
    277  1.2.10.2       tls 	if ((*abd->getsda)(abd->data))
    278  1.2.10.2       tls 		bits |= LI2CBB_SDA;
    279  1.2.10.2       tls 	if ((*abd->getscl)(abd->data))
    280  1.2.10.2       tls 		bits |= LI2CBB_SCL;
    281  1.2.10.2       tls 
    282  1.2.10.2       tls 	return bits;
    283  1.2.10.2       tls }
    284  1.2.10.2       tls 
    285  1.2.10.2       tls static void
    286  1.2.10.2       tls linux_i2cbb_set_dir(void *cookie __unused, uint32_t bits __unused)
    287  1.2.10.2       tls {
    288  1.2.10.2       tls 	/* Linux doesn't do anything here...  */
    289  1.2.10.2       tls }
    290  1.2.10.2       tls 
    291  1.2.10.2       tls static int
    293  1.2.10.2       tls linux_i2cbb_send_start(void *cookie, int flags)
    294  1.2.10.2       tls {
    295  1.2.10.2       tls 
    296  1.2.10.2       tls 	return i2c_bitbang_send_start(cookie, flags, &linux_i2cbb_ops);
    297  1.2.10.2       tls }
    298  1.2.10.2       tls 
    299  1.2.10.2       tls static int
    300  1.2.10.2       tls linux_i2cbb_send_stop(void *cookie, int flags)
    301  1.2.10.2       tls {
    302  1.2.10.2       tls 
    303  1.2.10.2       tls 	return i2c_bitbang_send_stop(cookie, flags, &linux_i2cbb_ops);
    304  1.2.10.2       tls }
    305  1.2.10.2       tls 
    306  1.2.10.2       tls static int
    307  1.2.10.2       tls linux_i2cbb_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    308  1.2.10.2       tls {
    309  1.2.10.2       tls 
    310  1.2.10.2       tls 	return i2c_bitbang_initiate_xfer(cookie, addr, flags,
    311  1.2.10.2       tls 	    &linux_i2cbb_ops);
    312  1.2.10.2       tls }
    313  1.2.10.2       tls 
    314  1.2.10.2       tls static int
    315  1.2.10.2       tls linux_i2cbb_read_byte(void *cookie, uint8_t *bytep, int flags)
    316  1.2.10.2       tls {
    317  1.2.10.2       tls 
    318  1.2.10.2       tls 	return i2c_bitbang_read_byte(cookie, bytep, flags, &linux_i2cbb_ops);
    319  1.2.10.2       tls }
    320  1.2.10.2       tls 
    321  1.2.10.2       tls 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