2016-06-07 04:06:27 +03:00
|
|
|
/* $NetBSD: i2c_bitbang.c,v 1.14 2016/06/07 01:06:27 pgoyette Exp $ */
|
2003-09-30 04:35:30 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2003 Wasabi Systems, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Written by Jason R. Thorpe for Wasabi Systems, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed for the NetBSD Project by
|
|
|
|
* Wasabi Systems, Inc.
|
|
|
|
* 4. The name of Wasabi Systems, Inc. may not be used to endorse
|
|
|
|
* or promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common module for bit-bang'ing an I2C bus.
|
|
|
|
*/
|
|
|
|
|
2007-12-11 15:09:21 +03:00
|
|
|
#include <sys/cdefs.h>
|
2016-06-07 04:06:27 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: i2c_bitbang.c,v 1.14 2016/06/07 01:06:27 pgoyette Exp $");
|
2007-12-11 15:09:21 +03:00
|
|
|
|
2016-06-07 04:06:27 +03:00
|
|
|
#include <sys/module.h>
|
2003-09-30 04:35:30 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#include <dev/i2c/i2cvar.h>
|
|
|
|
#include <dev/i2c/i2c_bitbang.h>
|
|
|
|
|
2006-03-05 20:33:33 +03:00
|
|
|
#define SETBITS(x) ops->ibo_set_bits(v, (x))
|
2003-09-30 04:35:30 +04:00
|
|
|
#define DIR(x) ops->ibo_set_dir(v, (x))
|
|
|
|
#define READ ops->ibo_read_bits(v)
|
|
|
|
|
|
|
|
#define SDA ops->ibo_bits[I2C_BIT_SDA] /* i2c signal */
|
|
|
|
#define SCL ops->ibo_bits[I2C_BIT_SCL] /* i2c signal */
|
|
|
|
#define OUTPUT ops->ibo_bits[I2C_BIT_OUTPUT] /* SDA is output */
|
|
|
|
#define INPUT ops->ibo_bits[I2C_BIT_INPUT] /* SDA is input */
|
|
|
|
|
2007-04-30 04:07:54 +04:00
|
|
|
#ifndef SCL_BAIL_COUNT
|
|
|
|
#define SCL_BAIL_COUNT 1000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline int i2c_wait_for_scl(void *, i2c_bitbang_ops_t);
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
i2c_wait_for_scl(void *v, i2c_bitbang_ops_t ops)
|
|
|
|
{
|
|
|
|
int bail = 0;
|
|
|
|
|
|
|
|
while (((READ & SCL) == 0) && (bail < SCL_BAIL_COUNT)) {
|
|
|
|
delay(1);
|
|
|
|
bail++;
|
|
|
|
}
|
|
|
|
if (bail == SCL_BAIL_COUNT) {
|
|
|
|
i2c_bitbang_send_stop(v, 0, ops);
|
|
|
|
return EIO;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-30 04:35:30 +04:00
|
|
|
/*ARGSUSED*/
|
|
|
|
int
|
2006-11-16 04:32:37 +03:00
|
|
|
i2c_bitbang_send_start(void *v, int flags, i2c_bitbang_ops_t ops)
|
2003-09-30 04:35:30 +04:00
|
|
|
{
|
|
|
|
|
2010-04-25 04:35:58 +04:00
|
|
|
/* start condition: put SDA H->L edge during SCL=H */
|
2008-06-01 05:13:18 +04:00
|
|
|
|
2003-09-30 04:35:30 +04:00
|
|
|
DIR(OUTPUT);
|
2006-03-05 20:33:33 +03:00
|
|
|
SETBITS(SDA | SCL);
|
2008-05-31 22:26:43 +04:00
|
|
|
delay(5); /* bus free time (4.7 us) */
|
2008-06-01 05:13:18 +04:00
|
|
|
SETBITS( 0 | SCL);
|
2007-04-30 04:07:54 +04:00
|
|
|
if (i2c_wait_for_scl(v, ops) != 0)
|
|
|
|
return EIO;
|
2008-05-31 22:26:43 +04:00
|
|
|
delay(4); /* start hold time (4.0 us) */
|
2007-12-01 09:32:54 +03:00
|
|
|
|
2010-04-25 04:35:58 +04:00
|
|
|
/* leave SCL=L and SDA=L to avoid unexpected start/stop condition */
|
2008-06-01 05:13:18 +04:00
|
|
|
SETBITS( 0 | 0);
|
2003-09-30 04:35:30 +04:00
|
|
|
|
2008-05-31 22:26:43 +04:00
|
|
|
return 0;
|
2003-09-30 04:35:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
int
|
2006-11-16 04:32:37 +03:00
|
|
|
i2c_bitbang_send_stop(void *v, int flags, i2c_bitbang_ops_t ops)
|
2003-09-30 04:35:30 +04:00
|
|
|
{
|
|
|
|
|
2010-04-25 04:35:58 +04:00
|
|
|
/* stop condition: put SDA L->H edge during SCL=H */
|
2008-06-01 05:13:18 +04:00
|
|
|
|
2010-04-25 04:35:58 +04:00
|
|
|
/* assume SCL=L, SDA=L here */
|
2003-09-30 04:35:30 +04:00
|
|
|
DIR(OUTPUT);
|
2008-06-01 05:13:18 +04:00
|
|
|
SETBITS( 0 | SCL);
|
2008-05-31 22:26:43 +04:00
|
|
|
delay(4); /* stop setup time (4.0 us) */
|
2006-03-05 20:33:33 +03:00
|
|
|
SETBITS(SDA | SCL);
|
2003-09-30 04:35:30 +04:00
|
|
|
|
2008-05-31 22:26:43 +04:00
|
|
|
return 0;
|
2003-09-30 04:35:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
i2c_bitbang_initiate_xfer(void *v, i2c_addr_t addr, int flags,
|
|
|
|
i2c_bitbang_ops_t ops)
|
|
|
|
{
|
|
|
|
|
2006-07-14 21:25:17 +04:00
|
|
|
if (addr < 0x80) {
|
|
|
|
uint8_t i2caddr;
|
2003-09-30 04:35:30 +04:00
|
|
|
|
2006-07-14 21:25:17 +04:00
|
|
|
/* disallow the 10-bit address prefix */
|
|
|
|
if ((addr & 0x78) == 0x78)
|
|
|
|
return EINVAL;
|
|
|
|
i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0);
|
|
|
|
(void) i2c_bitbang_send_start(v, flags, ops);
|
2003-09-30 04:35:30 +04:00
|
|
|
|
2006-07-14 21:25:17 +04:00
|
|
|
return (i2c_bitbang_write_byte(v, i2caddr,
|
|
|
|
flags & ~I2C_F_STOP, ops));
|
|
|
|
|
|
|
|
} else if (addr < 0x400) {
|
|
|
|
uint16_t i2caddr;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
i2caddr = (addr << 1) | ((flags & I2C_F_READ) ? 1 : 0) |
|
|
|
|
0xf000;
|
|
|
|
|
|
|
|
(void) i2c_bitbang_send_start(v, flags, ops);
|
|
|
|
rv = i2c_bitbang_write_byte(v, i2caddr >> 8,
|
|
|
|
flags & ~I2C_F_STOP, ops);
|
|
|
|
/* did a slave ack the 10-bit prefix? */
|
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
/* send the lower 7-bits (+ read/write mode) */
|
|
|
|
return (i2c_bitbang_write_byte(v, i2caddr & 0xff,
|
|
|
|
flags & ~I2C_F_STOP, ops));
|
|
|
|
|
|
|
|
} else
|
|
|
|
return EINVAL;
|
2003-09-30 04:35:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-05-31 22:26:43 +04:00
|
|
|
i2c_bitbang_read_byte(void *v, uint8_t *valp, int flags, i2c_bitbang_ops_t ops)
|
2003-09-30 04:35:30 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint8_t val = 0;
|
|
|
|
uint32_t bit;
|
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* assume SCL=L, SDA=L here */
|
|
|
|
|
|
|
|
DIR(INPUT);
|
2003-09-30 04:35:30 +04:00
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
val <<= 1;
|
2007-12-01 09:32:54 +03:00
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* data is set at SCL H->L edge */
|
2008-07-12 06:11:32 +04:00
|
|
|
/* SDA is set here because DIR() is INPUT */
|
|
|
|
SETBITS(SDA | 0);
|
2008-06-01 05:13:18 +04:00
|
|
|
delay(5); /* clock low time (4.7 us) */
|
2007-12-01 09:32:54 +03:00
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* read data at SCL L->H edge */
|
2008-07-12 06:11:32 +04:00
|
|
|
SETBITS(SDA | SCL);
|
2007-04-30 04:07:54 +04:00
|
|
|
if (i2c_wait_for_scl(v, ops) != 0)
|
|
|
|
return EIO;
|
2003-09-30 04:35:30 +04:00
|
|
|
if (READ & SDA)
|
|
|
|
val |= 1;
|
2008-06-01 05:13:18 +04:00
|
|
|
delay(4); /* clock high time (4.0 us) */
|
2003-09-30 04:35:30 +04:00
|
|
|
}
|
2008-06-01 05:13:18 +04:00
|
|
|
/* set SCL H->L before set SDA direction OUTPUT */
|
2008-07-12 06:11:32 +04:00
|
|
|
SETBITS(SDA | 0);
|
2003-09-30 04:35:30 +04:00
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* set ack after SCL H->L edge */
|
2003-09-30 04:35:30 +04:00
|
|
|
bit = (flags & I2C_F_LAST) ? SDA : 0;
|
|
|
|
DIR(OUTPUT);
|
2008-06-01 05:13:18 +04:00
|
|
|
SETBITS(bit | 0);
|
|
|
|
delay(5); /* clock low time (4.7 us) */
|
2007-12-01 09:32:54 +03:00
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* ack is checked at SCL L->H edge */
|
|
|
|
SETBITS(bit | SCL);
|
2007-04-30 04:07:54 +04:00
|
|
|
if (i2c_wait_for_scl(v, ops) != 0)
|
|
|
|
return EIO;
|
2008-05-31 22:26:43 +04:00
|
|
|
delay(4); /* clock high time (4.0 us) */
|
2007-12-01 09:32:54 +03:00
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* set SCL H->L for next data; don't change SDA here */
|
|
|
|
SETBITS(bit | 0);
|
2003-09-30 04:35:30 +04:00
|
|
|
|
2010-04-25 04:35:58 +04:00
|
|
|
/* leave SCL=L and SDA=L to avoid unexpected start/stop condition */
|
2008-06-01 05:13:18 +04:00
|
|
|
SETBITS( 0 | 0);
|
|
|
|
|
2003-09-30 04:35:30 +04:00
|
|
|
|
|
|
|
if ((flags & (I2C_F_STOP | I2C_F_LAST)) == (I2C_F_STOP | I2C_F_LAST))
|
|
|
|
(void) i2c_bitbang_send_stop(v, flags, ops);
|
|
|
|
|
|
|
|
*valp = val;
|
2008-05-31 22:26:43 +04:00
|
|
|
return 0;
|
2003-09-30 04:35:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2008-05-31 22:26:43 +04:00
|
|
|
i2c_bitbang_write_byte(void *v, uint8_t val, int flags, i2c_bitbang_ops_t ops)
|
2003-09-30 04:35:30 +04:00
|
|
|
{
|
|
|
|
uint32_t bit;
|
|
|
|
uint8_t mask;
|
|
|
|
int error;
|
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* assume at SCL=L, SDA=L here */
|
|
|
|
|
|
|
|
DIR(OUTPUT);
|
|
|
|
|
2003-09-30 04:35:30 +04:00
|
|
|
for (mask = 0x80; mask != 0; mask >>= 1) {
|
|
|
|
bit = (val & mask) ? SDA : 0;
|
2007-12-01 09:32:54 +03:00
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* set data after SCL H->L edge */
|
|
|
|
SETBITS(bit | 0);
|
|
|
|
delay(5); /* clock low time (4.7 us) */
|
2007-12-01 09:32:54 +03:00
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* data is fetched at SCL L->H edge */
|
|
|
|
SETBITS(bit | SCL);
|
2007-04-30 04:07:54 +04:00
|
|
|
if (i2c_wait_for_scl(v, ops))
|
|
|
|
return EIO;
|
2008-05-31 22:26:43 +04:00
|
|
|
delay(4); /* clock high time (4.0 us) */
|
2007-12-01 09:32:54 +03:00
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* put SCL H->L edge; don't change SDA here */
|
|
|
|
SETBITS(bit | 0);
|
2003-09-30 04:35:30 +04:00
|
|
|
}
|
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* ack is set at H->L edge */
|
|
|
|
DIR(INPUT);
|
|
|
|
delay(5); /* clock low time (4.7 us) */
|
2007-12-01 09:32:54 +03:00
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* read ack at L->H edge */
|
2008-07-12 06:11:32 +04:00
|
|
|
/* SDA is set here because DIR() is INPUT */
|
|
|
|
SETBITS(SDA | SCL);
|
2007-04-30 04:07:54 +04:00
|
|
|
if (i2c_wait_for_scl(v, ops) != 0)
|
|
|
|
return EIO;
|
2003-09-30 04:35:30 +04:00
|
|
|
error = (READ & SDA) ? EIO : 0;
|
2008-06-01 05:13:18 +04:00
|
|
|
delay(4); /* clock high time (4.0 us) */
|
2007-12-01 09:32:54 +03:00
|
|
|
|
2008-06-01 05:13:18 +04:00
|
|
|
/* set SCL H->L before set SDA direction OUTPUT */
|
2008-07-12 06:11:32 +04:00
|
|
|
SETBITS(SDA | 0);
|
2007-12-01 09:32:54 +03:00
|
|
|
DIR(OUTPUT);
|
2010-04-25 04:35:58 +04:00
|
|
|
/* leave SCL=L and SDA=L to avoid unexpected start/stop condition */
|
2008-07-12 06:11:32 +04:00
|
|
|
SETBITS( 0 | 0);
|
2003-09-30 04:35:30 +04:00
|
|
|
|
|
|
|
if (flags & I2C_F_STOP)
|
|
|
|
(void) i2c_bitbang_send_stop(v, flags, ops);
|
|
|
|
|
2008-05-31 22:26:43 +04:00
|
|
|
return error;
|
2003-09-30 04:35:30 +04:00
|
|
|
}
|
2016-06-07 04:06:27 +03:00
|
|
|
|
|
|
|
MODULE(MODULE_CLASS_MISC, i2c_bitbang, NULL);
|
|
|
|
|
|
|
|
static int
|
|
|
|
i2c_bitbang_modcmd(modcmd_t cmd, void *opaque)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case MODULE_CMD_INIT:
|
|
|
|
return 0;
|
|
|
|
case MODULE_CMD_FINI:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return ENOTTY;
|
|
|
|
}
|
|
|
|
}
|