2015-09-16 12:10:45 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Intel Corporation.
|
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-09-16 12:10:45 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2019-10-31 14:45:11 -07:00
|
|
|
* @file Driver for PCA95XX I2C-based GPIO driver.
|
2015-09-16 12:10:45 -07:00
|
|
|
*/
|
|
|
|
|
2016-03-09 14:54:42 -03:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-12-19 18:41:17 -05:00
|
|
|
#include <kernel.h>
|
2017-08-31 11:02:47 -04:00
|
|
|
#include <device.h>
|
|
|
|
#include <init.h>
|
2019-06-26 10:33:55 -04:00
|
|
|
#include <sys/util.h>
|
2019-06-25 15:53:52 -04:00
|
|
|
#include <drivers/gpio.h>
|
2019-06-25 15:53:54 -04:00
|
|
|
#include <drivers/i2c.h>
|
2015-09-16 12:10:45 -07:00
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
#include "gpio_pca95xx.h"
|
2015-09-16 12:10:45 -07:00
|
|
|
|
2018-10-08 13:59:55 -04:00
|
|
|
#define LOG_LEVEL CONFIG_GPIO_LOG_LEVEL
|
2018-08-14 14:07:51 +02:00
|
|
|
#include <logging/log.h>
|
2019-10-31 14:45:11 -07:00
|
|
|
LOG_MODULE_REGISTER(gpio_pca95xx);
|
2015-09-16 12:10:45 -07:00
|
|
|
|
|
|
|
/* Register definitions */
|
|
|
|
#define REG_INPUT_PORT0 0x00
|
|
|
|
#define REG_INPUT_PORT1 0x01
|
|
|
|
#define REG_OUTPUT_PORT0 0x02
|
|
|
|
#define REG_OUTPUT_PORT1 0x03
|
|
|
|
#define REG_POL_INV_PORT0 0x04
|
|
|
|
#define REG_POL_INV_PORT1 0x05
|
|
|
|
#define REG_CONF_PORT0 0x06
|
|
|
|
#define REG_CONG_PORT1 0x07
|
|
|
|
#define REG_OUT_DRV_STRENGTH_PORT0_L 0x40
|
|
|
|
#define REG_OUT_DRV_STRENGTH_PORT0_H 0x41
|
|
|
|
#define REG_OUT_DRV_STRENGTH_PORT1_L 0x42
|
|
|
|
#define REG_OUT_DRV_STRENGTH_PORT1_H 0x43
|
|
|
|
#define REG_INPUT_LATCH_PORT0 0x44
|
|
|
|
#define REG_INPUT_LATCH_PORT1 0x45
|
|
|
|
#define REG_PUD_EN_PORT0 0x46
|
|
|
|
#define REG_PUD_EN_PORT1 0x47
|
|
|
|
#define REG_PUD_SEL_PORT0 0x48
|
|
|
|
#define REG_PUD_SEL_PORT1 0x49
|
|
|
|
#define REG_INT_MASK_PORT0 0x4A
|
|
|
|
#define REG_INT_MASK_PORT1 0x4B
|
|
|
|
#define REG_INT_STATUS_PORT0 0x4C
|
|
|
|
#define REG_INT_STATUS_PORT1 0x4D
|
|
|
|
#define REG_OUTPUT_PORT_CONF 0x4F
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
/* Driver flags */
|
|
|
|
#define PCA_HAS_PUD BIT(0)
|
|
|
|
|
2015-09-16 12:10:45 -07:00
|
|
|
/**
|
|
|
|
* @brief Read both port 0 and port 1 registers of certain register function.
|
|
|
|
*
|
|
|
|
* Given the register in reg, read the pair of port 0 and port 1.
|
|
|
|
*
|
2019-10-31 14:45:11 -07:00
|
|
|
* @param dev Device struct of the PCA95XX.
|
2015-09-16 12:10:45 -07:00
|
|
|
* @param reg Register to read (the PORT0 of the pair of registers).
|
|
|
|
* @param buf Buffer to read data into.
|
|
|
|
*
|
2016-03-09 14:01:20 -03:00
|
|
|
* @return 0 if successful, failed otherwise.
|
2015-09-16 12:10:45 -07:00
|
|
|
*/
|
2019-03-12 15:15:42 -06:00
|
|
|
static int read_port_regs(struct device *dev, u8_t reg,
|
2019-10-31 14:45:11 -07:00
|
|
|
union gpio_pca95xx_port_data *buf)
|
2015-09-16 12:10:45 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
const struct gpio_pca95xx_config * const config =
|
2015-09-16 12:10:45 -07:00
|
|
|
dev->config->config_info;
|
2019-10-31 14:45:11 -07:00
|
|
|
struct gpio_pca95xx_drv_data * const drv_data =
|
|
|
|
(struct gpio_pca95xx_drv_data * const)dev->driver_data;
|
2015-09-16 12:10:45 -07:00
|
|
|
struct device * const i2c_master = drv_data->i2c_master;
|
2017-04-21 10:03:20 -05:00
|
|
|
u16_t i2c_addr = config->i2c_slave_addr;
|
2015-09-16 12:10:45 -07:00
|
|
|
int ret;
|
|
|
|
|
2016-04-26 11:39:30 +03:00
|
|
|
ret = i2c_burst_read(i2c_master, i2c_addr, reg, buf->byte, 2);
|
2015-09-16 12:10:45 -07:00
|
|
|
if (ret) {
|
2019-10-31 14:45:11 -07:00
|
|
|
LOG_ERR("PCA95XX[0x%X]: error reading register 0x%X (%d)",
|
2018-08-14 14:07:51 +02:00
|
|
|
i2c_addr, reg, ret);
|
2015-09-16 12:10:45 -07:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
LOG_DBG("PCA95XX[0x%X]: Read: REG[0x%X] = 0x%X, REG[0x%X] = 0x%X",
|
2018-08-14 14:07:51 +02:00
|
|
|
i2c_addr, reg, buf->byte[0], (reg + 1), buf->byte[1]);
|
2015-09-16 12:10:45 -07:00
|
|
|
|
|
|
|
error:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Write both port 0 and port 1 registers of certain register function.
|
|
|
|
*
|
|
|
|
* Given the register in reg, write the pair of port 0 and port 1.
|
|
|
|
*
|
2019-10-31 14:45:11 -07:00
|
|
|
* @param dev Device struct of the PCA95XX.
|
2015-09-16 12:10:45 -07:00
|
|
|
* @param reg Register to write into (the PORT0 of the pair of registers).
|
|
|
|
* @param buf Buffer to write data from.
|
|
|
|
*
|
2016-03-09 14:01:20 -03:00
|
|
|
* @return 0 if successful, failed otherwise.
|
2015-09-16 12:10:45 -07:00
|
|
|
*/
|
2019-03-12 15:15:42 -06:00
|
|
|
static int write_port_regs(struct device *dev, u8_t reg,
|
2019-10-31 14:45:11 -07:00
|
|
|
union gpio_pca95xx_port_data *buf)
|
2015-09-16 12:10:45 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
const struct gpio_pca95xx_config * const config =
|
2015-09-16 12:10:45 -07:00
|
|
|
dev->config->config_info;
|
2019-10-31 14:45:11 -07:00
|
|
|
struct gpio_pca95xx_drv_data * const drv_data =
|
|
|
|
(struct gpio_pca95xx_drv_data * const)dev->driver_data;
|
2015-09-16 12:10:45 -07:00
|
|
|
struct device * const i2c_master = drv_data->i2c_master;
|
2017-04-21 10:03:20 -05:00
|
|
|
u16_t i2c_addr = config->i2c_slave_addr;
|
2015-09-29 11:22:23 -07:00
|
|
|
int ret;
|
2015-09-16 12:10:45 -07:00
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
LOG_DBG("PCA95XX[0x%X]: Write: REG[0x%X] = 0x%X, REG[0x%X] = "
|
2018-08-14 14:07:51 +02:00
|
|
|
"0x%X", i2c_addr, reg, buf->byte[0], (reg + 1),
|
|
|
|
buf->byte[1]);
|
2015-09-22 15:09:08 -07:00
|
|
|
|
2019-11-03 19:21:05 -08:00
|
|
|
ret = i2c_burst_write(i2c_master, i2c_addr, reg, buf->byte, 2);
|
2015-09-29 11:22:23 -07:00
|
|
|
if (ret) {
|
2019-10-31 14:45:11 -07:00
|
|
|
LOG_ERR("PCA95XX[0x%X]: error writing to register 0x%X "
|
2018-08-14 14:07:51 +02:00
|
|
|
"(%d)", i2c_addr, reg, ret);
|
2015-09-29 11:22:23 -07:00
|
|
|
}
|
2019-11-03 19:21:05 -08:00
|
|
|
|
2015-09-29 11:22:23 -07:00
|
|
|
return ret;
|
2015-09-16 12:10:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Setup the pin direction (input or output)
|
|
|
|
*
|
2019-10-31 14:45:11 -07:00
|
|
|
* @param dev Device struct of the PCA95XX
|
2015-09-16 12:10:45 -07:00
|
|
|
* @param access_op Access operation (pin or port)
|
|
|
|
* @param pin The pin number
|
|
|
|
* @param flags Flags of pin or port
|
|
|
|
*
|
2016-03-09 14:01:20 -03:00
|
|
|
* @return 0 if successful, failed otherwise
|
2015-09-16 12:10:45 -07:00
|
|
|
*/
|
2019-03-12 15:15:42 -06:00
|
|
|
static int setup_pin_dir(struct device *dev, int access_op,
|
2017-04-21 10:03:20 -05:00
|
|
|
u32_t pin, int flags)
|
2015-09-16 12:10:45 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
struct gpio_pca95xx_drv_data * const drv_data =
|
|
|
|
(struct gpio_pca95xx_drv_data * const)dev->driver_data;
|
|
|
|
union gpio_pca95xx_port_data *reg_dir = &drv_data->reg_cache.dir;
|
|
|
|
union gpio_pca95xx_port_data *reg_out = &drv_data->reg_cache.output;
|
2015-09-16 12:10:45 -07:00
|
|
|
int ret;
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
/* For each pin, 0 == output, 1 == input */
|
2015-09-16 12:10:45 -07:00
|
|
|
switch (access_op) {
|
2015-10-14 13:29:17 -07:00
|
|
|
case GPIO_ACCESS_BY_PIN:
|
2019-10-28 15:51:54 -07:00
|
|
|
if ((flags & GPIO_OUTPUT) != 0U) {
|
|
|
|
if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0U) {
|
|
|
|
reg_out->all |= BIT(pin);
|
|
|
|
} else if ((flags & GPIO_OUTPUT_INIT_LOW) != 0U) {
|
|
|
|
reg_out->all &= ~BIT(pin);
|
|
|
|
}
|
|
|
|
reg_dir->all &= ~BIT(pin);
|
|
|
|
} else {
|
|
|
|
reg_dir->all |= BIT(pin);
|
2015-10-14 13:29:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case GPIO_ACCESS_BY_PORT:
|
2019-10-28 15:51:54 -07:00
|
|
|
if ((flags & GPIO_OUTPUT) != 0U) {
|
|
|
|
if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0U) {
|
|
|
|
reg_out->all = 0xFFFF;
|
|
|
|
} else if ((flags & GPIO_OUTPUT_INIT_LOW) != 0U) {
|
|
|
|
reg_out->all = 0x0;
|
|
|
|
}
|
|
|
|
reg_dir->all = 0x0;
|
2015-10-14 13:29:17 -07:00
|
|
|
} else {
|
2019-10-28 15:51:54 -07:00
|
|
|
reg_dir->all = 0xFFFF;
|
2015-10-14 13:29:17 -07:00
|
|
|
}
|
2019-10-28 15:51:54 -07:00
|
|
|
|
2015-10-14 13:29:17 -07:00
|
|
|
break;
|
|
|
|
default:
|
2016-03-09 14:54:42 -03:00
|
|
|
ret = -ENOTSUP;
|
2015-10-14 13:29:17 -07:00
|
|
|
goto done;
|
2015-09-16 12:10:45 -07:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
ret = write_port_regs(dev, REG_OUTPUT_PORT0, reg_out);
|
|
|
|
if (ret != 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = write_port_regs(dev, REG_CONF_PORT0, reg_dir);
|
2015-09-16 12:10:45 -07:00
|
|
|
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Setup the pin pull up/pull down status
|
|
|
|
*
|
2019-10-31 14:45:11 -07:00
|
|
|
* @param dev Device struct of the PCA95XX
|
2015-09-16 12:10:45 -07:00
|
|
|
* @param access_op Access operation (pin or port)
|
|
|
|
* @param pin The pin number
|
|
|
|
* @param flags Flags of pin or port
|
|
|
|
*
|
2016-03-09 14:01:20 -03:00
|
|
|
* @return 0 if successful, failed otherwise
|
2015-09-16 12:10:45 -07:00
|
|
|
*/
|
2019-03-12 15:15:42 -06:00
|
|
|
static int setup_pin_pullupdown(struct device *dev, int access_op,
|
2017-04-21 10:03:20 -05:00
|
|
|
u32_t pin, int flags)
|
2015-09-16 12:10:45 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
const struct gpio_pca95xx_config * const config =
|
|
|
|
dev->config->config_info;
|
|
|
|
struct gpio_pca95xx_drv_data * const drv_data =
|
|
|
|
(struct gpio_pca95xx_drv_data * const)dev->driver_data;
|
|
|
|
union gpio_pca95xx_port_data *reg_pud;
|
2017-04-21 10:03:20 -05:00
|
|
|
u16_t bit_mask;
|
2018-11-29 11:12:22 -08:00
|
|
|
u16_t new_value = 0U;
|
2015-09-16 12:10:45 -07:00
|
|
|
int ret;
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
if ((config->capabilities & PCA_HAS_PUD) == 0) {
|
|
|
|
/* Chip does not support pull up/pull down */
|
|
|
|
if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0U) {
|
|
|
|
ret = -ENOTSUP;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If both GPIO_PULL_UP and GPIO_PULL_DOWN are not set,
|
|
|
|
* we should disable them in hardware. But need to skip
|
|
|
|
* if the chip does not support pull up/pull down.
|
|
|
|
*/
|
|
|
|
ret = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2015-09-16 12:10:45 -07:00
|
|
|
/* If disabling pull up/down, there is no need to set the selection
|
|
|
|
* register. Just go straight to disabling.
|
|
|
|
*/
|
2019-10-28 15:51:54 -07:00
|
|
|
if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) == 0U) {
|
2015-09-16 12:10:45 -07:00
|
|
|
goto en_dis;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup pin pull up or pull down */
|
2019-10-28 15:51:54 -07:00
|
|
|
reg_pud = &drv_data->reg_cache.pud_sel;
|
2015-09-16 12:10:45 -07:00
|
|
|
switch (access_op) {
|
2015-10-14 13:29:17 -07:00
|
|
|
case GPIO_ACCESS_BY_PIN:
|
|
|
|
bit_mask = 1 << pin;
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
/* pull down == 0, pull up == 1 */
|
|
|
|
if ((flags & GPIO_PULL_UP) != 0U) {
|
2015-10-14 13:29:17 -07:00
|
|
|
new_value = 1 << pin;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
reg_pud->all &= ~bit_mask;
|
|
|
|
reg_pud->all |= new_value;
|
2015-10-14 13:29:17 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
case GPIO_ACCESS_BY_PORT:
|
|
|
|
/* pull down == 0, pull up == 1*/
|
2019-10-28 15:51:54 -07:00
|
|
|
if ((flags & GPIO_PULL_UP) != 0U) {
|
|
|
|
reg_pud->all = 0xFFFF;
|
2015-10-14 13:29:17 -07:00
|
|
|
} else {
|
2019-10-28 15:51:54 -07:00
|
|
|
reg_pud->all = 0x0;
|
2015-10-14 13:29:17 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2016-03-09 14:54:42 -03:00
|
|
|
ret = -ENOTSUP;
|
2015-10-14 13:29:17 -07:00
|
|
|
goto done;
|
2015-09-16 12:10:45 -07:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
ret = write_port_regs(dev, REG_PUD_SEL_PORT0, reg_pud);
|
2015-09-16 12:10:45 -07:00
|
|
|
if (ret) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
en_dis:
|
|
|
|
/* enable/disable pull up/down */
|
2019-10-28 15:51:54 -07:00
|
|
|
reg_pud = &drv_data->reg_cache.pud_en;
|
2015-09-16 12:10:45 -07:00
|
|
|
switch (access_op) {
|
2015-10-14 13:29:17 -07:00
|
|
|
case GPIO_ACCESS_BY_PIN:
|
|
|
|
bit_mask = 1 << pin;
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0U) {
|
2015-10-14 13:29:17 -07:00
|
|
|
new_value = 1 << pin;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
reg_pud->all &= ~bit_mask;
|
|
|
|
reg_pud->all |= new_value;
|
2015-10-14 13:29:17 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
case GPIO_ACCESS_BY_PORT:
|
2019-10-28 15:51:54 -07:00
|
|
|
if ((flags & (GPIO_PULL_UP | GPIO_PULL_DOWN)) != 0U) {
|
|
|
|
reg_pud->all = 0xFFFF;
|
2015-10-14 13:29:17 -07:00
|
|
|
} else {
|
2019-10-28 15:51:54 -07:00
|
|
|
reg_pud->all = 0x0;
|
2015-10-14 13:29:17 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2016-03-09 14:54:42 -03:00
|
|
|
ret = -ENOTSUP;
|
2015-10-14 13:29:17 -07:00
|
|
|
goto done;
|
2015-09-16 12:10:45 -07:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
ret = write_port_regs(dev, REG_PUD_EN_PORT0, reg_pud);
|
2015-09-16 12:10:45 -07:00
|
|
|
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-19 10:45:34 -07:00
|
|
|
* @brief Configure pin or port
|
2015-09-16 12:10:45 -07:00
|
|
|
*
|
2019-10-31 14:45:11 -07:00
|
|
|
* @param dev Device struct of the PCA95XX
|
2015-09-16 12:10:45 -07:00
|
|
|
* @param access_op Access operation (pin or port)
|
|
|
|
* @param pin The pin number
|
|
|
|
* @param flags Flags of pin or port
|
|
|
|
*
|
2016-03-09 14:01:20 -03:00
|
|
|
* @return 0 if successful, failed otherwise
|
2015-09-16 12:10:45 -07:00
|
|
|
*/
|
2019-10-31 14:45:11 -07:00
|
|
|
static int gpio_pca95xx_config(struct device *dev, int access_op,
|
2017-04-21 10:03:20 -05:00
|
|
|
u32_t pin, int flags)
|
2015-09-16 12:10:45 -07:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-10-08 13:59:55 -04:00
|
|
|
#if (CONFIG_GPIO_LOG_LEVEL >= LOG_LEVEL_DEBUG)
|
2019-10-31 14:45:11 -07:00
|
|
|
const struct gpio_pca95xx_config * const config =
|
2015-09-16 12:10:45 -07:00
|
|
|
dev->config->config_info;
|
2017-04-21 10:03:20 -05:00
|
|
|
u16_t i2c_addr = config->i2c_slave_addr;
|
2015-09-16 12:10:45 -07:00
|
|
|
#endif
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
/* Does not support disconnected pin */
|
|
|
|
if ((flags & (GPIO_INPUT | GPIO_OUTPUT)) == GPIO_DISCONNECTED) {
|
2018-11-15 10:05:48 +01:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
/* Open-drain support is per port, not per pin.
|
|
|
|
* So can't really support the API as-is.
|
|
|
|
*/
|
|
|
|
if ((flags & GPIO_SINGLE_ENDED) != 0U) {
|
|
|
|
return -ENOTSUP;
|
2015-09-16 12:10:45 -07:00
|
|
|
}
|
|
|
|
|
2019-03-12 15:15:42 -06:00
|
|
|
ret = setup_pin_dir(dev, access_op, pin, flags);
|
2015-09-16 12:10:45 -07:00
|
|
|
if (ret) {
|
2019-10-31 14:45:11 -07:00
|
|
|
LOG_ERR("PCA95XX[0x%X]: error setting pin direction (%d)",
|
2018-08-14 14:07:51 +02:00
|
|
|
i2c_addr, ret);
|
2015-09-16 12:10:45 -07:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:15:42 -06:00
|
|
|
ret = setup_pin_pullupdown(dev, access_op, pin, flags);
|
2015-09-16 12:10:45 -07:00
|
|
|
if (ret) {
|
2019-10-31 14:45:11 -07:00
|
|
|
LOG_ERR("PCA95XX[0x%X]: error setting pin pull up/down "
|
2018-08-14 14:07:51 +02:00
|
|
|
"(%d)", i2c_addr, ret);
|
2015-09-16 12:10:45 -07:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the pin or port output
|
|
|
|
*
|
2019-10-31 14:45:11 -07:00
|
|
|
* @param dev Device struct of the PCA95XX
|
2015-09-16 12:10:45 -07:00
|
|
|
* @param access_op Access operation (pin or port)
|
|
|
|
* @param pin The pin number
|
|
|
|
* @param value Value to set (0 or 1)
|
|
|
|
*
|
2016-03-09 14:01:20 -03:00
|
|
|
* @return 0 if successful, failed otherwise
|
2015-09-16 12:10:45 -07:00
|
|
|
*/
|
2019-10-31 14:45:11 -07:00
|
|
|
static int gpio_pca95xx_write(struct device *dev, int access_op,
|
2017-04-21 10:03:20 -05:00
|
|
|
u32_t pin, u32_t value)
|
2015-09-16 12:10:45 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
struct gpio_pca95xx_drv_data * const drv_data =
|
|
|
|
(struct gpio_pca95xx_drv_data * const)dev->driver_data;
|
|
|
|
union gpio_pca95xx_port_data *reg_out = &drv_data->reg_cache.output;
|
2015-09-16 12:10:45 -07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Invert input value for pins configurated as active low. */
|
|
|
|
switch (access_op) {
|
2015-10-14 13:29:17 -07:00
|
|
|
case GPIO_ACCESS_BY_PIN:
|
2019-10-28 15:51:54 -07:00
|
|
|
if (value) {
|
|
|
|
reg_out->all |= BIT(pin);
|
|
|
|
} else {
|
|
|
|
reg_out->all &= ~BIT(pin);
|
|
|
|
}
|
2015-10-14 13:29:17 -07:00
|
|
|
break;
|
|
|
|
case GPIO_ACCESS_BY_PORT:
|
2019-10-28 15:51:54 -07:00
|
|
|
reg_out->all = value;
|
2015-10-14 13:29:17 -07:00
|
|
|
break;
|
|
|
|
default:
|
2016-03-09 14:54:42 -03:00
|
|
|
ret = -ENOTSUP;
|
2015-10-14 13:29:17 -07:00
|
|
|
goto done;
|
2015-09-16 12:10:45 -07:00
|
|
|
}
|
|
|
|
|
2019-10-28 15:51:54 -07:00
|
|
|
ret = write_port_regs(dev, REG_OUTPUT_PORT0, reg_out);
|
2015-09-16 12:10:45 -07:00
|
|
|
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read the pin or port status
|
|
|
|
*
|
2019-10-31 14:45:11 -07:00
|
|
|
* @param dev Device struct of the PCA95XX
|
2015-09-16 12:10:45 -07:00
|
|
|
* @param access_op Access operation (pin or port)
|
|
|
|
* @param pin The pin number
|
|
|
|
* @param value Value of input pin(s)
|
|
|
|
*
|
2016-03-09 14:01:20 -03:00
|
|
|
* @return 0 if successful, failed otherwise
|
2015-09-16 12:10:45 -07:00
|
|
|
*/
|
2019-10-31 14:45:11 -07:00
|
|
|
static int gpio_pca95xx_read(struct device *dev, int access_op,
|
2017-04-21 10:03:20 -05:00
|
|
|
u32_t pin, u32_t *value)
|
2015-09-16 12:10:45 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
union gpio_pca95xx_port_data buf;
|
2015-09-16 12:10:45 -07:00
|
|
|
int ret;
|
|
|
|
|
2019-03-12 15:15:42 -06:00
|
|
|
ret = read_port_regs(dev, REG_INPUT_PORT0, &buf);
|
2016-03-09 14:01:20 -03:00
|
|
|
if (ret != 0) {
|
2015-09-16 12:10:45 -07:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (access_op) {
|
2015-10-14 13:29:17 -07:00
|
|
|
case GPIO_ACCESS_BY_PIN:
|
|
|
|
*value = (buf.all >> pin) & 0x01;
|
|
|
|
break;
|
|
|
|
case GPIO_ACCESS_BY_PORT:
|
|
|
|
*value = buf.all;
|
|
|
|
break;
|
|
|
|
default:
|
2016-03-09 14:54:42 -03:00
|
|
|
ret = -ENOTSUP;
|
2015-10-14 13:29:17 -07:00
|
|
|
break;
|
2015-09-16 12:10:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static int gpio_pca95xx_port_get_raw(struct device *dev, u32_t *value)
|
2019-10-28 15:51:54 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
union gpio_pca95xx_port_data buf;
|
2019-10-28 15:51:54 -07:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = read_port_regs(dev, REG_INPUT_PORT0, &buf);
|
|
|
|
if (ret != 0) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
*value = buf.all;
|
|
|
|
|
|
|
|
done:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static int gpio_pca95xx_port_set_masked_raw(struct device *dev,
|
2019-10-28 15:51:54 -07:00
|
|
|
u32_t mask, u32_t value)
|
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
struct gpio_pca95xx_drv_data * const drv_data =
|
|
|
|
(struct gpio_pca95xx_drv_data * const)dev->driver_data;
|
|
|
|
union gpio_pca95xx_port_data *reg_out = &drv_data->reg_cache.output;
|
2019-10-28 15:51:54 -07:00
|
|
|
|
|
|
|
reg_out->all = (reg_out->all & ~mask) | (mask & value);
|
|
|
|
|
|
|
|
return write_port_regs(dev, REG_OUTPUT_PORT0, reg_out);
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static int gpio_pca95xx_port_set_bits_raw(struct device *dev, u32_t mask)
|
2019-10-28 15:51:54 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
return gpio_pca95xx_port_set_masked_raw(dev, mask, mask);
|
2019-10-28 15:51:54 -07:00
|
|
|
}
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static int gpio_pca95xx_port_clear_bits_raw(struct device *dev, u32_t mask)
|
2019-10-28 15:51:54 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
return gpio_pca95xx_port_set_masked_raw(dev, mask, 0);
|
2019-10-28 15:51:54 -07:00
|
|
|
}
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static int gpio_pca95xx_port_toggle_bits(struct device *dev, u32_t mask)
|
2019-10-28 15:51:54 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
struct gpio_pca95xx_drv_data * const drv_data =
|
|
|
|
(struct gpio_pca95xx_drv_data * const)dev->driver_data;
|
|
|
|
union gpio_pca95xx_port_data *reg_out = &drv_data->reg_cache.output;
|
2019-10-28 15:51:54 -07:00
|
|
|
|
|
|
|
reg_out->all ^= mask;
|
|
|
|
|
|
|
|
return write_port_regs(dev, REG_OUTPUT_PORT0, reg_out);
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static int gpio_pca95xx_pin_interrupt_configure(struct device *dev,
|
2019-10-28 15:51:54 -07:00
|
|
|
unsigned int pin,
|
|
|
|
enum gpio_int_mode mode,
|
|
|
|
enum gpio_int_trig trig)
|
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static const struct gpio_driver_api gpio_pca95xx_drv_api_funcs = {
|
|
|
|
.config = gpio_pca95xx_config,
|
|
|
|
.write = gpio_pca95xx_write,
|
|
|
|
.read = gpio_pca95xx_read,
|
|
|
|
.port_get_raw = gpio_pca95xx_port_get_raw,
|
|
|
|
.port_set_masked_raw = gpio_pca95xx_port_set_masked_raw,
|
|
|
|
.port_set_bits_raw = gpio_pca95xx_port_set_bits_raw,
|
|
|
|
.port_clear_bits_raw = gpio_pca95xx_port_clear_bits_raw,
|
|
|
|
.port_toggle_bits = gpio_pca95xx_port_toggle_bits,
|
|
|
|
.pin_interrupt_configure = gpio_pca95xx_pin_interrupt_configure,
|
2015-09-16 12:10:45 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-10-31 14:45:11 -07:00
|
|
|
* @brief Initialization function of PCA95XX
|
2015-09-16 12:10:45 -07:00
|
|
|
*
|
|
|
|
* @param dev Device struct
|
2016-03-09 14:01:20 -03:00
|
|
|
* @return 0 if successful, failed otherwise.
|
2015-09-16 12:10:45 -07:00
|
|
|
*/
|
2019-10-31 14:45:11 -07:00
|
|
|
static int gpio_pca95xx_init(struct device *dev)
|
2015-09-16 12:10:45 -07:00
|
|
|
{
|
2019-10-31 14:45:11 -07:00
|
|
|
const struct gpio_pca95xx_config * const config =
|
2015-09-16 12:10:45 -07:00
|
|
|
dev->config->config_info;
|
2019-10-31 14:45:11 -07:00
|
|
|
struct gpio_pca95xx_drv_data * const drv_data =
|
|
|
|
(struct gpio_pca95xx_drv_data * const)dev->driver_data;
|
2015-09-16 12:10:45 -07:00
|
|
|
struct device *i2c_master;
|
|
|
|
|
|
|
|
/* Find out the device struct of the I2C master */
|
|
|
|
i2c_master = device_get_binding((char *)config->i2c_master_dev_name);
|
|
|
|
if (!i2c_master) {
|
2016-03-09 15:22:04 -03:00
|
|
|
return -EINVAL;
|
2015-09-16 12:10:45 -07:00
|
|
|
}
|
|
|
|
drv_data->i2c_master = i2c_master;
|
|
|
|
|
2016-03-09 14:01:20 -03:00
|
|
|
return 0;
|
2015-09-16 12:10:45 -07:00
|
|
|
}
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
/* Initialization for PCA95XX_0 */
|
|
|
|
#ifdef CONFIG_GPIO_PCA95XX_0
|
|
|
|
static const struct gpio_pca95xx_config gpio_pca95xx_0_cfg = {
|
|
|
|
.i2c_master_dev_name = CONFIG_GPIO_PCA95XX_0_I2C_MASTER_DEV_NAME,
|
|
|
|
.i2c_slave_addr = CONFIG_GPIO_PCA95XX_0_I2C_ADDR,
|
|
|
|
.capabilities =
|
|
|
|
(CONFIG_GPIO_PCA95XX_0_HAS_PUD ? PCA_HAS_PUD : 0) |
|
|
|
|
0,
|
2015-09-16 12:10:45 -07:00
|
|
|
};
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static struct gpio_pca95xx_drv_data gpio_pca95xx_0_drvdata = {
|
2015-09-22 15:09:08 -07:00
|
|
|
/* Default for registers according to datasheet */
|
|
|
|
.reg_cache.output = { .all = 0xFFFF },
|
|
|
|
.reg_cache.dir = { .all = 0xFFFF },
|
|
|
|
.reg_cache.pud_en = { .all = 0x0 },
|
|
|
|
.reg_cache.pud_sel = { .all = 0xFFFF },
|
|
|
|
};
|
2015-09-16 12:10:45 -07:00
|
|
|
|
|
|
|
/* This has to init after I2C master */
|
2019-10-31 14:45:11 -07:00
|
|
|
DEVICE_AND_API_INIT(gpio_pca95xx_0, CONFIG_GPIO_PCA95XX_0_DEV_NAME,
|
|
|
|
gpio_pca95xx_init,
|
|
|
|
&gpio_pca95xx_0_drvdata, &gpio_pca95xx_0_cfg,
|
|
|
|
POST_KERNEL, CONFIG_GPIO_PCA95XX_INIT_PRIORITY,
|
|
|
|
&gpio_pca95xx_drv_api_funcs);
|
|
|
|
|
|
|
|
#endif /* CONFIG_GPIO_PCA95XX_0 */
|
|
|
|
|
|
|
|
/* Initialization for PCA95XX_1 */
|
|
|
|
#ifdef CONFIG_GPIO_PCA95XX_1
|
|
|
|
static const struct gpio_pca95xx_config gpio_pca95xx_1_cfg = {
|
|
|
|
.i2c_master_dev_name = CONFIG_GPIO_PCA95XX_1_I2C_MASTER_DEV_NAME,
|
|
|
|
.i2c_slave_addr = CONFIG_GPIO_PCA95XX_1_I2C_ADDR,
|
|
|
|
.capabilities =
|
|
|
|
(CONFIG_GPIO_PCA95XX_1_HAS_PUD ? PCA_HAS_PUD : 0) |
|
|
|
|
0,
|
2015-09-16 12:10:45 -07:00
|
|
|
};
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static struct gpio_pca95xx_drv_data gpio_pca95xx_1_drvdata = {
|
2015-09-22 15:09:08 -07:00
|
|
|
/* Default for registers according to datasheet */
|
|
|
|
.reg_cache.output = { .all = 0xFFFF },
|
|
|
|
.reg_cache.dir = { .all = 0xFFFF },
|
|
|
|
.reg_cache.pud_en = { .all = 0x0 },
|
|
|
|
.reg_cache.pud_sel = { .all = 0xFFFF },
|
|
|
|
};
|
2015-09-16 12:10:45 -07:00
|
|
|
|
|
|
|
/* This has to init after I2C master */
|
2019-10-31 14:45:11 -07:00
|
|
|
DEVICE_AND_API_INIT(gpio_pca95xx_1, CONFIG_GPIO_PCA95XX_1_DEV_NAME,
|
|
|
|
gpio_pca95xx_init,
|
|
|
|
&gpio_pca95xx_1_drvdata, &gpio_pca95xx_1_cfg,
|
|
|
|
POST_KERNEL, CONFIG_GPIO_PCA95XX_INIT_PRIORITY,
|
|
|
|
&gpio_pca95xx_drv_api_funcs);
|
|
|
|
|
|
|
|
#endif /* CONFIG_GPIO_PCA95XX_1 */
|
|
|
|
|
|
|
|
/* Initialization for PCA95XX_2 */
|
|
|
|
#ifdef CONFIG_GPIO_PCA95XX_2
|
|
|
|
static const struct gpio_pca95xx_config gpio_pca95xx_2_cfg = {
|
|
|
|
.i2c_master_dev_name = CONFIG_GPIO_PCA95XX_2_I2C_MASTER_DEV_NAME,
|
|
|
|
.i2c_slave_addr = CONFIG_GPIO_PCA95XX_2_I2C_ADDR,
|
|
|
|
.capabilities =
|
|
|
|
(CONFIG_GPIO_PCA95XX_2_HAS_PUD ? PCA_HAS_PUD : 0) |
|
|
|
|
0,
|
2015-09-16 12:10:45 -07:00
|
|
|
};
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static struct gpio_pca95xx_drv_data gpio_pca95xx_2_drvdata = {
|
2015-09-22 15:09:08 -07:00
|
|
|
/* Default for registers according to datasheet */
|
|
|
|
.reg_cache.output = { .all = 0xFFFF },
|
|
|
|
.reg_cache.dir = { .all = 0xFFFF },
|
|
|
|
.reg_cache.pud_en = { .all = 0x0 },
|
|
|
|
.reg_cache.pud_sel = { .all = 0xFFFF },
|
|
|
|
};
|
2015-09-16 12:10:45 -07:00
|
|
|
|
|
|
|
/* This has to init after I2C master */
|
2019-10-31 14:45:11 -07:00
|
|
|
DEVICE_AND_API_INIT(gpio_pca95xx_2, CONFIG_GPIO_PCA95XX_2_DEV_NAME,
|
|
|
|
gpio_pca95xx_init,
|
|
|
|
&gpio_pca95xx_2_drvdata, &gpio_pca95xx_2_cfg,
|
|
|
|
POST_KERNEL, CONFIG_GPIO_PCA95XX_INIT_PRIORITY,
|
|
|
|
&gpio_pca95xx_drv_api_funcs);
|
|
|
|
|
|
|
|
#endif /* CONFIG_GPIO_PCA95XX_2 */
|
|
|
|
|
|
|
|
/* Initialization for PCA95XX_3 */
|
|
|
|
#ifdef CONFIG_GPIO_PCA95XX_3
|
|
|
|
static const struct gpio_pca95xx_config gpio_pca95xx_3_cfg = {
|
|
|
|
.i2c_master_dev_name = CONFIG_GPIO_PCA95XX_3_I2C_MASTER_DEV_NAME,
|
|
|
|
.i2c_slave_addr = CONFIG_GPIO_PCA95XX_3_I2C_ADDR,
|
|
|
|
.capabilities =
|
|
|
|
(CONFIG_GPIO_PCA95XX_3_HAS_PUD ? PCA_HAS_PUD : 0) |
|
|
|
|
0,
|
2015-09-16 12:10:45 -07:00
|
|
|
};
|
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
static struct gpio_pca95xx_drv_data gpio_pca95xx_3_drvdata = {
|
2015-09-22 15:09:08 -07:00
|
|
|
/* Default for registers according to datasheet */
|
|
|
|
.reg_cache.output = { .all = 0xFFFF },
|
|
|
|
.reg_cache.dir = { .all = 0xFFFF },
|
|
|
|
.reg_cache.pud_en = { .all = 0x0 },
|
|
|
|
.reg_cache.pud_sel = { .all = 0xFFFF },
|
|
|
|
};
|
2015-09-16 12:10:45 -07:00
|
|
|
|
|
|
|
/* This has to init after I2C master */
|
2019-10-31 14:45:11 -07:00
|
|
|
DEVICE_AND_API_INIT(gpio_pca95xx_3, CONFIG_GPIO_PCA95XX_3_DEV_NAME,
|
|
|
|
gpio_pca95xx_init,
|
|
|
|
&gpio_pca95xx_3_drvdata, &gpio_pca95xx_3_cfg,
|
|
|
|
POST_KERNEL, CONFIG_GPIO_PCA95XX_INIT_PRIORITY,
|
|
|
|
&gpio_pca95xx_drv_api_funcs);
|
2015-09-16 12:10:45 -07:00
|
|
|
|
2019-10-31 14:45:11 -07:00
|
|
|
#endif /* CONFIG_GPIO_PCA95XX_3 */
|