/* * CBUS I2C driver for Nokia Internet Tablets. * * Copyright (C) 2004-2010 Nokia Corporation * * Based on code written by Juha Yrjölä, David Weinehall, Mikko Ylinen and * Felipe Balbi. Converted to I2C driver by Aaro Koskinen. * * This file is subject to the terms and conditions of the GNU General * Public License. See the file "COPYING" in the main directory of this * archive for more details. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details.
*/
/* * Bit counts are derived from Nokia implementation. These should be checked * if other CBUS implementations appear.
*/ #define CBUS_ADDR_BITS 3 #define CBUS_REG_BITS 5
/** * cbus_send_bit - sends one bit over the bus * @host: the host we're using * @bit: one bit of information to send
*/ staticvoid cbus_send_bit(struct cbus_host *host, unsigned bit)
{
gpiod_set_value(host->dat, bit ? 1 : 0);
gpiod_set_value(host->clk, 1);
gpiod_set_value(host->clk, 0);
}
/** * cbus_send_data - sends @len amount of data over the bus * @host: the host we're using * @data: the data to send * @len: size of the transfer
*/ staticvoid cbus_send_data(struct cbus_host *host, unsigned data, unsigned len)
{ int i;
for (i = len; i > 0; i--)
cbus_send_bit(host, data & (1 << (i - 1)));
}
/** * cbus_receive_bit - receives one bit from the bus * @host: the host we're using
*/ staticint cbus_receive_bit(struct cbus_host *host)
{ int ret;
gpiod_set_value(host->clk, 1);
ret = gpiod_get_value(host->dat);
gpiod_set_value(host->clk, 0); return ret;
}
/** * cbus_receive_word - receives 16-bit word from the bus * @host: the host we're using
*/ staticint cbus_receive_word(struct cbus_host *host)
{ int ret = 0; int i;
for (i = 16; i > 0; i--) { int bit = cbus_receive_bit(host);
if (bit < 0) return bit;
if (bit)
ret |= 1 << (i - 1);
} return ret;
}
/** * cbus_transfer - transfers data over the bus * @host: the host we're using * @rw: read/write flag * @dev: device address * @reg: register address * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
*/ staticint cbus_transfer(struct cbus_host *host, char rw, unsigned dev, unsigned reg, unsigned data)
{ unsignedlong flags; int ret;
/* We don't want interrupts disturbing our transfer */
spin_lock_irqsave(&host->lock, flags);
/* Reset state and start of transfer, SEL stays down during transfer */
gpiod_set_value(host->sel, 0);
/* Set the DAT pin to output */
gpiod_direction_output(host->dat, 1);
/* Send the device address */
cbus_send_data(host, dev, CBUS_ADDR_BITS);
/* Send the rw flag */
cbus_send_bit(host, rw == I2C_SMBUS_READ);
/* Send the register address */
cbus_send_data(host, reg, CBUS_REG_BITS);
if (rw == I2C_SMBUS_WRITE) {
cbus_send_data(host, data, 16);
ret = 0;
} else {
ret = gpiod_direction_input(host->dat); if (ret) {
dev_dbg(host->dev, "failed setting direction\n"); goto out;
}
gpiod_set_value(host->clk, 1);
ret = cbus_receive_word(host); if (ret < 0) {
dev_dbg(host->dev, "failed receiving data\n"); goto out;
}
}
/* Indicate end of transfer, SEL goes up until next transfer */
gpiod_set_value(host->sel, 1);
gpiod_set_value(host->clk, 1);
gpiod_set_value(host->clk, 0);
out:
spin_unlock_irqrestore(&host->lock, flags);
return ret;
}
staticint cbus_i2c_smbus_xfer(struct i2c_adapter *adapter,
u16 addr, unsignedshort flags, char read_write,
u8 command, int size, union i2c_smbus_data *data)
{ struct cbus_host *chost = i2c_get_adapdata(adapter); int ret;
if (size != I2C_SMBUS_WORD_DATA) return -EINVAL;
ret = cbus_transfer(chost, read_write == I2C_SMBUS_READ, addr,
command, data->word); if (ret < 0) return ret;
if (read_write == I2C_SMBUS_READ)
data->word = ret;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.