drivers: i2c: Add driver for LiteX I2C controller

This adds I2C bitbang driver for LiteX SoC builder with its bindings.

Signed-off-by: Robert Winkler <rwinkler@internships.antmicro.com>
Signed-off-by: Mateusz Holenko <mholenko@antmicro.com>
This commit is contained in:
Robert Winkler 2019-07-09 15:37:17 +02:00 committed by Johan Hedberg
commit 7f8a70a74b
6 changed files with 202 additions and 1 deletions

View file

@ -137,6 +137,7 @@
/drivers/gpio/*ht16k33* @henrikbrixandersen
/drivers/gpio/*stm32* @rsalveti @idlethread
/drivers/hwinfo/ @alexanderwachter
/drivers/i2c/*litex* @mateusz-holenko @kgugala @pgielda
/drivers/i2s/i2s_ll_stm32* @avisconti
/drivers/ieee802154/ @jukkar @tbursztyka
/drivers/interrupt_controller/ @andrewboie

View file

@ -21,6 +21,7 @@ zephyr_library_sources_ifdef(CONFIG_I2C_NIOS2 i2c_nios2.c)
zephyr_library_sources_ifdef(CONFIG_I2C_GECKO i2c_gecko.c)
zephyr_library_sources_ifdef(CONFIG_I2C_RV32M1_LPI2C i2c_rv32m1_lpi2c.c)
zephyr_library_sources_ifdef(CONFIG_I2C_SAM0 i2c_sam0.c)
zephyr_library_sources_ifdef(CONFIG_I2C_LITEX i2c_litex.c)
zephyr_library_sources_ifdef(CONFIG_I2C_STM32_V1
i2c_ll_stm32_v1.c

View file

@ -27,7 +27,7 @@ source "drivers/i2c/Kconfig.sbcon"
source "drivers/i2c/Kconfig.sifive"
source "drivers/i2c/Kconfig.stm32"
source "drivers/i2c/Kconfig.sam0"
source "drivers/i2c/Kconfig.litex"
config I2C_INIT_PRIORITY
int "Init priority"

13
drivers/i2c/Kconfig.litex Normal file
View file

@ -0,0 +1,13 @@
#
# Copyright (c) 2019 Antmicro <www.antmicro.com>
#
# SPDX-License-Identifier: Apache-2.0
#
menuconfig I2C_LITEX
bool "LiteX I2C driver"
depends on SOC_RISCV32_LITEX_VEXRISCV
select HAS_DTS_I2C
select I2C_BITBANG
help
Enable support for Litex I2C driver

171
drivers/i2c/i2c_litex.c Normal file
View file

@ -0,0 +1,171 @@
/*
* Copyright (c) 2019 Antmicro <www.antmicro.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <device.h>
#include <drivers/i2c.h>
#include "i2c_bitbang.h"
#define SCL_BIT_POS 0
#define SDA_DIR_BIT_POS 1
#define SDA_BIT_W_POS 2
#define SDA_BIT_R_POS 0
#define SDA_DIR_OUTPUT 1
#define SDA_DIR_INPUT 0
#define HIGH_STATE_ON_I2C_LINES 0x7
struct i2c_litex_cfg {
volatile u32_t *w_reg;
volatile const u32_t *r_reg;
};
#define GET_I2C_CFG(dev) \
((const struct i2c_litex_cfg *) dev->config->config_info)
#define GET_I2C_BITBANG(dev) \
((struct i2c_bitbang *) dev->driver_data)
static inline void set_bit(volatile u32_t *reg, u32_t bit, u32_t val)
{
u32_t mask = BIT(bit);
if (val) {
*reg |= mask;
} else {
*reg &= ~mask;
}
}
static inline int get_bit(volatile const u32_t *reg, u32_t bit)
{
u32_t mask = BIT(bit);
return !!((*reg) & mask);
}
static void i2c_litex_bitbang_set_scl(void *context, int state)
{
const struct i2c_litex_cfg *config =
(const struct i2c_litex_cfg *) context;
set_bit(config->w_reg, SCL_BIT_POS, state);
}
static void i2c_litex_bitbang_set_sda(void *context, int state)
{
const struct i2c_litex_cfg *config =
(const struct i2c_litex_cfg *) context;
set_bit(config->w_reg, SDA_DIR_BIT_POS, SDA_DIR_OUTPUT);
set_bit(config->w_reg, SDA_BIT_W_POS, state);
}
static int i2c_litex_bitbang_get_sda(void *context)
{
const struct i2c_litex_cfg *config =
(const struct i2c_litex_cfg *) context;
set_bit(config->w_reg, SDA_DIR_BIT_POS, SDA_DIR_INPUT);
return get_bit(config->r_reg, SDA_BIT_R_POS);
}
static const struct i2c_bitbang_io i2c_litex_bitbang_io = {
.set_scl = i2c_litex_bitbang_set_scl,
.set_sda = i2c_litex_bitbang_set_sda,
.get_sda = i2c_litex_bitbang_get_sda,
};
static int i2c_litex_init(struct device *dev)
{
const struct i2c_litex_cfg *config = GET_I2C_CFG(dev);
struct i2c_bitbang *bitbang = GET_I2C_BITBANG(dev);
*(config->w_reg) |= HIGH_STATE_ON_I2C_LINES;
i2c_bitbang_init(bitbang, &i2c_litex_bitbang_io, (void *)config);
return 0;
}
static int i2c_litex_configure(struct device *dev, u32_t dev_config)
{
struct i2c_bitbang *bitbang = GET_I2C_BITBANG(dev);
return i2c_bitbang_configure(bitbang, dev_config);
}
static int i2c_litex_transfer(struct device *dev, struct i2c_msg *msgs,
u8_t num_msgs, u16_t addr)
{
struct i2c_bitbang *bitbang = GET_I2C_BITBANG(dev);
return i2c_bitbang_transfer(bitbang, msgs, num_msgs, addr);
}
static const struct i2c_driver_api i2c_litex_driver_api = {
.configure = i2c_litex_configure,
.transfer = i2c_litex_transfer,
.slave_register = NULL,
.slave_unregister = NULL,
};
/* Device Instantiation */
#define I2C_LITEX_INIT(n) \
static const struct i2c_litex_cfg i2c_litex_cfg_##n = { \
.w_reg = \
(volatile u32_t *) DT_INST_##n##_LITEX_I2C_WRITE_BASE_ADDRESS,\
.r_reg = \
(volatile u32_t *) DT_INST_##n##_LITEX_I2C_READ_BASE_ADDRESS, \
}; \
\
static struct i2c_bitbang i2c_bitbang_##n; \
\
DEVICE_AND_API_INIT(litex_i2c_##n, \
DT_INST_##n##_LITEX_I2C_LABEL, \
i2c_litex_init, \
&i2c_bitbang_##n, \
&i2c_litex_cfg_##n, \
POST_KERNEL, \
CONFIG_I2C_INIT_PRIORITY, \
&i2c_litex_driver_api \
)
#ifdef DT_INST_0_LITEX_I2C_LABEL
I2C_LITEX_INIT(0);
#endif
#ifdef DT_INST_1_LITEX_I2C_LABEL
I2C_LITEX_INIT(1);
#endif
#ifdef DT_INST_2_LITEX_I2C_LABEL
I2C_LITEX_INIT(2);
#endif
#ifdef DT_INST_3_LITEX_I2C_LABEL
I2C_LITEX_INIT(3);
#endif
#ifdef DT_INST_4_LITEX_I2C_LABEL
I2C_LITEX_INIT(4);
#endif
#ifdef DT_INST_5_LITEX_I2C_LABEL
I2C_LITEX_INIT(5);
#endif
#ifdef DT_INST_6_LITEX_I2C_LABEL
I2C_LITEX_INIT(6);
#endif
#ifdef DT_INST_7_LITEX_I2C_LABEL
I2C_LITEX_INIT(7);
#endif
#ifdef DT_INST_8_LITEX_I2C_LABEL
I2C_LITEX_INIT(8);
#endif

View file

@ -0,0 +1,15 @@
#
# Copyright (c) 2019 Antmicro <www.antmicro.com>
#
# SPDX-License-Identifier: Apache-2.0
#
description: LiteX I2C controller
compatible: "litex,i2c"
include: i2c-controller.yaml
properties:
reg:
required: true