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