Home | History | Annotate | Line # | Download | only in i2c
i2c_bitbang.c revision 1.12.14.1
      1  1.12.14.1  uebayasi /*	$NetBSD: i2c_bitbang.c,v 1.12.14.1 2010/04/30 14:43:11 uebayasi Exp $	*/
      2        1.1   thorpej 
      3        1.1   thorpej /*
      4        1.1   thorpej  * Copyright (c) 2003 Wasabi Systems, Inc.
      5        1.1   thorpej  * All rights reserved.
      6        1.1   thorpej  *
      7        1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8        1.1   thorpej  *
      9        1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10        1.1   thorpej  * modification, are permitted provided that the following conditions
     11        1.1   thorpej  * are met:
     12        1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13        1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14        1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16        1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17        1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18        1.1   thorpej  *    must display the following acknowledgement:
     19        1.1   thorpej  *      This product includes software developed for the NetBSD Project by
     20        1.1   thorpej  *      Wasabi Systems, Inc.
     21        1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22        1.1   thorpej  *    or promote products derived from this software without specific prior
     23        1.1   thorpej  *    written permission.
     24        1.1   thorpej  *
     25        1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26        1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27        1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28        1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29        1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30        1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31        1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32        1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33        1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34        1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35        1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36        1.1   thorpej  */
     37        1.1   thorpej 
     38        1.1   thorpej /*
     39        1.1   thorpej  * Common module for bit-bang'ing an I2C bus.
     40        1.1   thorpej  */
     41        1.1   thorpej 
     42        1.9     lukem #include <sys/cdefs.h>
     43  1.12.14.1  uebayasi __KERNEL_RCSID(0, "$NetBSD: i2c_bitbang.c,v 1.12.14.1 2010/04/30 14:43:11 uebayasi Exp $");
     44        1.9     lukem 
     45        1.1   thorpej #include <sys/param.h>
     46        1.1   thorpej 
     47        1.1   thorpej #include <dev/i2c/i2cvar.h>
     48        1.1   thorpej #include <dev/i2c/i2c_bitbang.h>
     49        1.1   thorpej 
     50        1.3  christos #define	SETBITS(x)	ops->ibo_set_bits(v, (x))
     51        1.1   thorpej #define	DIR(x)		ops->ibo_set_dir(v, (x))
     52        1.1   thorpej #define	READ		ops->ibo_read_bits(v)
     53        1.1   thorpej 
     54        1.1   thorpej #define	SDA		ops->ibo_bits[I2C_BIT_SDA]	/* i2c signal */
     55        1.1   thorpej #define	SCL		ops->ibo_bits[I2C_BIT_SCL]	/* i2c signal */
     56        1.1   thorpej #define	OUTPUT		ops->ibo_bits[I2C_BIT_OUTPUT]	/* SDA is output */
     57        1.1   thorpej #define	INPUT		ops->ibo_bits[I2C_BIT_INPUT]	/* SDA is input */
     58        1.1   thorpej 
     59        1.7  macallan #ifndef SCL_BAIL_COUNT
     60        1.7  macallan #define SCL_BAIL_COUNT 1000
     61        1.7  macallan #endif
     62        1.7  macallan 
     63        1.7  macallan static inline int i2c_wait_for_scl(void *, i2c_bitbang_ops_t);
     64        1.7  macallan 
     65        1.7  macallan static inline int
     66        1.7  macallan i2c_wait_for_scl(void *v, i2c_bitbang_ops_t ops)
     67        1.7  macallan {
     68        1.7  macallan 	int bail = 0;
     69        1.7  macallan 
     70        1.7  macallan 	while (((READ & SCL) == 0) && (bail < SCL_BAIL_COUNT)) {
     71        1.7  macallan 		delay(1);
     72        1.7  macallan 		bail++;
     73        1.7  macallan 	}
     74        1.7  macallan 	if (bail == SCL_BAIL_COUNT) {
     75        1.7  macallan 		i2c_bitbang_send_stop(v, 0, ops);
     76        1.7  macallan 		return EIO;
     77        1.7  macallan 	}
     78        1.7  macallan 	return 0;
     79        1.7  macallan }
     80        1.7  macallan 
     81        1.1   thorpej /*ARGSUSED*/
     82        1.1   thorpej int
     83        1.6  christos i2c_bitbang_send_start(void *v, int flags, i2c_bitbang_ops_t ops)
     84        1.1   thorpej {
     85        1.1   thorpej 
     86  1.12.14.1  uebayasi 	/* start condition: put SDA H->L edge during SCL=H */
     87       1.11   tsutsui 
     88        1.1   thorpej 	DIR(OUTPUT);
     89        1.3  christos 	SETBITS(SDA | SCL);
     90       1.10   tsutsui 	delay(5);		/* bus free time (4.7 us) */
     91       1.11   tsutsui 	SETBITS(  0 | SCL);
     92        1.7  macallan 	if (i2c_wait_for_scl(v, ops) != 0)
     93        1.7  macallan 		return EIO;
     94       1.10   tsutsui 	delay(4);		/* start hold time (4.0 us) */
     95        1.8  kiyohara 
     96  1.12.14.1  uebayasi 	/* leave SCL=L and SDA=L to avoid unexpected start/stop condition */
     97       1.11   tsutsui 	SETBITS(  0 |   0);
     98        1.1   thorpej 
     99       1.10   tsutsui 	return 0;
    100        1.1   thorpej }
    101        1.1   thorpej 
    102        1.1   thorpej /*ARGSUSED*/
    103        1.1   thorpej int
    104        1.6  christos i2c_bitbang_send_stop(void *v, int flags, i2c_bitbang_ops_t ops)
    105        1.1   thorpej {
    106        1.1   thorpej 
    107  1.12.14.1  uebayasi 	/* stop condition: put SDA L->H edge during SCL=H */
    108       1.11   tsutsui 
    109  1.12.14.1  uebayasi 	/* assume SCL=L, SDA=L here */
    110        1.1   thorpej 	DIR(OUTPUT);
    111       1.11   tsutsui 	SETBITS(  0 | SCL);
    112       1.10   tsutsui 	delay(4);		/* stop setup time (4.0 us) */
    113        1.3  christos 	SETBITS(SDA | SCL);
    114        1.1   thorpej 
    115       1.10   tsutsui 	return 0;
    116        1.1   thorpej }
    117        1.1   thorpej 
    118        1.1   thorpej int
    119        1.1   thorpej i2c_bitbang_initiate_xfer(void *v, i2c_addr_t addr, int flags,
    120        1.1   thorpej     i2c_bitbang_ops_t ops)
    121        1.1   thorpej {
    122        1.1   thorpej 
    123        1.4   gdamore 	if (addr < 0x80) {
    124        1.4   gdamore 		uint8_t i2caddr;
    125        1.1   thorpej 
    126        1.4   gdamore 		/* disallow the 10-bit address prefix */
    127        1.4   gdamore 		if ((addr & 0x78) == 0x78)
    128        1.4   gdamore 			return EINVAL;
    129        1.4   gdamore 		i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0);
    130        1.4   gdamore 		(void) i2c_bitbang_send_start(v, flags, ops);
    131        1.4   gdamore 
    132        1.4   gdamore 		return (i2c_bitbang_write_byte(v, i2caddr,
    133        1.4   gdamore 			    flags & ~I2C_F_STOP, ops));
    134        1.4   gdamore 
    135        1.4   gdamore 	} else if (addr < 0x400) {
    136        1.4   gdamore 		uint16_t	i2caddr;
    137        1.4   gdamore 		int		rv;
    138        1.4   gdamore 
    139        1.4   gdamore 		i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0) |
    140        1.4   gdamore 		    0xf000;
    141        1.4   gdamore 
    142        1.4   gdamore 		(void) i2c_bitbang_send_start(v, flags, ops);
    143        1.4   gdamore 		rv = i2c_bitbang_write_byte(v, i2caddr >> 8,
    144        1.4   gdamore 		    flags & ~I2C_F_STOP, ops);
    145        1.4   gdamore 		/* did a slave ack the 10-bit prefix? */
    146        1.4   gdamore 		if (rv != 0)
    147        1.4   gdamore 			return rv;
    148        1.4   gdamore 
    149        1.4   gdamore 		/* send the lower 7-bits (+ read/write mode) */
    150        1.4   gdamore 		return (i2c_bitbang_write_byte(v, i2caddr & 0xff,
    151        1.4   gdamore 			    flags & ~I2C_F_STOP, ops));
    152        1.1   thorpej 
    153        1.4   gdamore 	} else
    154        1.4   gdamore 		return EINVAL;
    155        1.1   thorpej }
    156        1.1   thorpej 
    157        1.1   thorpej int
    158       1.10   tsutsui i2c_bitbang_read_byte(void *v, uint8_t *valp, int flags, i2c_bitbang_ops_t ops)
    159        1.1   thorpej {
    160        1.1   thorpej 	int i;
    161        1.1   thorpej 	uint8_t val = 0;
    162        1.1   thorpej 	uint32_t bit;
    163        1.1   thorpej 
    164       1.11   tsutsui 	/* assume SCL=L, SDA=L here */
    165       1.11   tsutsui 
    166       1.11   tsutsui 	DIR(INPUT);
    167        1.1   thorpej 
    168        1.1   thorpej 	for (i = 0; i < 8; i++) {
    169        1.1   thorpej 		val <<= 1;
    170        1.8  kiyohara 
    171       1.11   tsutsui 		/* data is set at SCL H->L edge */
    172       1.12   tsutsui 		/* SDA is set here because DIR() is INPUT */
    173       1.12   tsutsui 		SETBITS(SDA |   0);
    174       1.11   tsutsui 		delay(5);	/* clock low time (4.7 us) */
    175        1.8  kiyohara 
    176       1.11   tsutsui 		/* read data at SCL L->H edge */
    177       1.12   tsutsui 		SETBITS(SDA | SCL);
    178        1.7  macallan 		if (i2c_wait_for_scl(v, ops) != 0)
    179        1.7  macallan 			return EIO;
    180        1.1   thorpej 		if (READ & SDA)
    181        1.1   thorpej 			val |= 1;
    182       1.11   tsutsui 		delay(4);	/* clock high time (4.0 us) */
    183        1.1   thorpej 	}
    184       1.11   tsutsui 	/* set SCL H->L before set SDA direction OUTPUT */
    185       1.12   tsutsui 	SETBITS(SDA |   0);
    186        1.1   thorpej 
    187       1.11   tsutsui 	/* set ack after SCL H->L edge */
    188        1.1   thorpej 	bit = (flags & I2C_F_LAST) ? SDA : 0;
    189       1.11   tsutsui 	DIR(OUTPUT);
    190       1.11   tsutsui 	SETBITS(bit |   0);
    191       1.11   tsutsui 	delay(5);	/* clock low time (4.7 us) */
    192        1.8  kiyohara 
    193       1.11   tsutsui 	/* ack is checked at SCL L->H edge */
    194        1.3  christos 	SETBITS(bit | SCL);
    195        1.7  macallan 	if (i2c_wait_for_scl(v, ops) != 0)
    196        1.7  macallan 		return EIO;
    197       1.10   tsutsui 	delay(4);	/* clock high time (4.0 us) */
    198        1.8  kiyohara 
    199       1.11   tsutsui 	/* set SCL H->L for next data; don't change SDA here */
    200       1.11   tsutsui 	SETBITS(bit |   0);
    201        1.1   thorpej 
    202  1.12.14.1  uebayasi 	/* leave SCL=L and SDA=L to avoid unexpected start/stop condition */
    203       1.11   tsutsui 	SETBITS(  0 |   0);
    204       1.11   tsutsui 
    205        1.1   thorpej 
    206        1.1   thorpej 	if ((flags & (I2C_F_STOP | I2C_F_LAST)) == (I2C_F_STOP | I2C_F_LAST))
    207        1.1   thorpej 		(void) i2c_bitbang_send_stop(v, flags, ops);
    208        1.1   thorpej 
    209        1.1   thorpej 	*valp = val;
    210       1.10   tsutsui 	return 0;
    211        1.1   thorpej }
    212        1.1   thorpej 
    213        1.1   thorpej int
    214       1.10   tsutsui i2c_bitbang_write_byte(void *v, uint8_t val, int flags, i2c_bitbang_ops_t ops)
    215        1.1   thorpej {
    216        1.1   thorpej 	uint32_t bit;
    217        1.1   thorpej 	uint8_t mask;
    218        1.1   thorpej 	int error;
    219        1.1   thorpej 
    220       1.11   tsutsui 	/* assume at SCL=L, SDA=L here */
    221       1.11   tsutsui 
    222       1.11   tsutsui 	DIR(OUTPUT);
    223       1.11   tsutsui 
    224        1.1   thorpej 	for (mask = 0x80; mask != 0; mask >>= 1) {
    225        1.1   thorpej 		bit = (val & mask) ? SDA : 0;
    226        1.8  kiyohara 
    227       1.11   tsutsui 		/* set data after SCL H->L edge */
    228       1.11   tsutsui 		SETBITS(bit |   0);
    229       1.11   tsutsui 		delay(5);	/* clock low time (4.7 us) */
    230       1.11   tsutsui 
    231       1.11   tsutsui 		/* data is fetched at SCL L->H edge */
    232        1.3  christos 		SETBITS(bit | SCL);
    233        1.7  macallan 		if (i2c_wait_for_scl(v, ops))
    234        1.7  macallan 			return EIO;
    235       1.10   tsutsui 		delay(4);	/* clock high time (4.0 us) */
    236        1.8  kiyohara 
    237       1.11   tsutsui 		/* put SCL H->L edge; don't change SDA here */
    238       1.11   tsutsui 		SETBITS(bit |   0);
    239        1.1   thorpej 	}
    240        1.1   thorpej 
    241       1.11   tsutsui 	/* ack is set at H->L edge */
    242       1.11   tsutsui 	DIR(INPUT);
    243       1.11   tsutsui 	delay(5);	/* clock low time (4.7 us) */
    244        1.8  kiyohara 
    245       1.11   tsutsui 	/* read ack at L->H edge */
    246       1.12   tsutsui 	/* SDA is set here because DIR() is INPUT */
    247       1.12   tsutsui 	SETBITS(SDA | SCL);
    248        1.7  macallan 	if (i2c_wait_for_scl(v, ops) != 0)
    249        1.7  macallan 		return EIO;
    250        1.1   thorpej 	error = (READ & SDA) ? EIO : 0;
    251       1.11   tsutsui 	delay(4);	/* clock high time (4.0 us) */
    252        1.8  kiyohara 
    253       1.12   tsutsui 	/* set SCL H->L before set SDA direction OUTPUT */
    254       1.12   tsutsui 	SETBITS(SDA |   0);
    255       1.12   tsutsui 	DIR(OUTPUT);
    256  1.12.14.1  uebayasi 	/* leave SCL=L and SDA=L to avoid unexpected start/stop condition */
    257       1.11   tsutsui 	SETBITS(  0 |   0);
    258        1.1   thorpej 
    259        1.1   thorpej 	if (flags & I2C_F_STOP)
    260        1.1   thorpej 		(void) i2c_bitbang_send_stop(v, flags, ops);
    261        1.1   thorpej 
    262       1.10   tsutsui 	return error;
    263        1.1   thorpej }
    264