Source
x
/*
* Support Infineon TLE62x0 driver chips
*
* Copyright (c) 2007 Simtec Electronics
* Ben Dooks, <ben@simtec.co.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
struct tle62x0_state {
struct spi_device *us;
struct mutex lock;
unsigned int nr_gpio;
unsigned int gpio_state;
unsigned char tx_buff[4];
unsigned char rx_buff[4];
};
static int to_gpio_num(struct device_attribute *attr);
static inline int tle62x0_write(struct tle62x0_state *st)
{
unsigned char *buff = st->tx_buff;
unsigned int gpio_state = st->gpio_state;
buff[0] = CMD_SET;
if (st->nr_gpio == 16) {
buff[1] = gpio_state >> 8;
buff[2] = gpio_state;
} else {
buff[1] = gpio_state;
}
dev_dbg(&st->us->dev, "buff %3ph\n", buff);
return spi_write(st->us, buff, (st->nr_gpio == 16) ? 3 : 2);
}
static inline int tle62x0_read(struct tle62x0_state *st)
{
unsigned char *txbuff = st->tx_buff;
struct spi_transfer xfer = {
.tx_buf = txbuff,
.rx_buf = st->rx_buff,
.len = (st->nr_gpio * 2) / 8,
};
struct spi_message msg;
txbuff[0] = CMD_READ;
txbuff[1] = 0x00;
txbuff[2] = 0x00;
txbuff[3] = 0x00;
spi_message_init(&msg);
spi_message_add_tail(&xfer, &msg);
return spi_sync(st->us, &msg);
}
static unsigned char *decode_fault(unsigned int fault_code)
{
fault_code &= 3;
switch (fault_code) {
case DIAG_NORMAL:
return "N";
case DIAG_OVERLOAD:
return "V";
case DIAG_OPEN:
return "O";
case DIAG_SHORTGND: