2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2011-10-03 23:13:51 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2011
|
2018-03-06 07:04:58 +00:00
|
|
|
* Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
|
2011-10-03 23:13:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Driver for NXP's pca9698 40 bit I2C gpio expander
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <i2c.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2016-09-21 02:28:55 +00:00
|
|
|
#include <linux/errno.h>
|
2011-10-03 23:13:51 +00:00
|
|
|
#include <pca9698.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The pca9698 registers
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define PCA9698_REG_INPUT 0x00
|
|
|
|
#define PCA9698_REG_OUTPUT 0x08
|
|
|
|
#define PCA9698_REG_POLARITY 0x10
|
|
|
|
#define PCA9698_REG_CONFIG 0x18
|
|
|
|
|
|
|
|
#define PCA9698_BUFFER_SIZE 5
|
2011-10-31 08:39:12 +00:00
|
|
|
#define PCA9698_GPIO_COUNT 40
|
2011-10-03 23:13:51 +00:00
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
static int pca9698_read40(u8 addr, u8 offset, u8 *buffer)
|
2011-10-03 23:13:51 +00:00
|
|
|
{
|
|
|
|
u8 command = offset | 0x80; /* autoincrement */
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
return i2c_read(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
|
2011-10-03 23:13:51 +00:00
|
|
|
}
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
static int pca9698_write40(u8 addr, u8 offset, u8 *buffer)
|
2011-10-03 23:13:51 +00:00
|
|
|
{
|
|
|
|
u8 command = offset | 0x80; /* autoincrement */
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
return i2c_write(addr, command, 1, buffer, PCA9698_BUFFER_SIZE);
|
2011-10-03 23:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pca9698_set_bit(unsigned gpio, u8 *buffer, unsigned value)
|
|
|
|
{
|
|
|
|
unsigned byte = gpio / 8;
|
|
|
|
unsigned bit = gpio % 8;
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
buffer[byte] |= (1 << bit);
|
|
|
|
else
|
|
|
|
buffer[byte] &= ~(1 << bit);
|
|
|
|
}
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
int pca9698_request(unsigned gpio, const char *label)
|
|
|
|
{
|
|
|
|
if (gpio >= PCA9698_GPIO_COUNT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pca9698_free(unsigned gpio)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int pca9698_direction_input(u8 addr, unsigned gpio)
|
2011-10-03 23:13:51 +00:00
|
|
|
{
|
|
|
|
u8 data[PCA9698_BUFFER_SIZE];
|
|
|
|
int res;
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
|
2011-10-03 23:13:51 +00:00
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
pca9698_set_bit(gpio, data, 1);
|
|
|
|
|
|
|
|
return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
|
2011-10-03 23:13:51 +00:00
|
|
|
}
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
int pca9698_direction_output(u8 addr, unsigned gpio, int value)
|
2011-10-03 23:13:51 +00:00
|
|
|
{
|
|
|
|
u8 data[PCA9698_BUFFER_SIZE];
|
|
|
|
int res;
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
res = pca9698_set_value(addr, gpio, value);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res = pca9698_read40(addr, PCA9698_REG_CONFIG, data);
|
2011-10-03 23:13:51 +00:00
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
pca9698_set_bit(gpio, data, 0);
|
|
|
|
|
|
|
|
return pca9698_write40(addr, PCA9698_REG_CONFIG, data);
|
2011-10-03 23:13:51 +00:00
|
|
|
}
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
int pca9698_get_value(u8 addr, unsigned gpio)
|
2011-10-03 23:13:51 +00:00
|
|
|
{
|
2011-10-31 08:39:12 +00:00
|
|
|
unsigned config_byte = gpio / 8;
|
|
|
|
unsigned config_bit = gpio % 8;
|
2011-10-03 23:13:51 +00:00
|
|
|
unsigned value;
|
|
|
|
u8 data[PCA9698_BUFFER_SIZE];
|
|
|
|
int res;
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
res = pca9698_read40(addr, PCA9698_REG_INPUT, data);
|
2011-10-03 23:13:51 +00:00
|
|
|
if (res)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
value = data[config_byte] & (1 << config_bit);
|
|
|
|
|
|
|
|
return !!value;
|
|
|
|
}
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
int pca9698_set_value(u8 addr, unsigned gpio, int value)
|
2011-10-03 23:13:51 +00:00
|
|
|
{
|
|
|
|
u8 data[PCA9698_BUFFER_SIZE];
|
|
|
|
int res;
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
res = pca9698_read40(addr, PCA9698_REG_OUTPUT, data);
|
2011-10-03 23:13:51 +00:00
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
2011-10-31 08:39:12 +00:00
|
|
|
pca9698_set_bit(gpio, data, value);
|
|
|
|
|
|
|
|
return pca9698_write40(addr, PCA9698_REG_OUTPUT, data);
|
2011-10-03 23:13:51 +00:00
|
|
|
}
|