diff --git a/arch/arm/soc/nxp_kinetis/k6x/Kconfig.defconfig.mk64f12 b/arch/arm/soc/nxp_kinetis/k6x/Kconfig.defconfig.mk64f12 index cadf5b012ef..a0e5e5a794e 100644 --- a/arch/arm/soc/nxp_kinetis/k6x/Kconfig.defconfig.mk64f12 +++ b/arch/arm/soc/nxp_kinetis/k6x/Kconfig.defconfig.mk64f12 @@ -108,6 +108,39 @@ endif # GPIO_K64 endif # GPIO +if I2C + +config I2C_KSDK + def_bool y + +config I2C_0 + def_bool y + +if I2C_0 + +config I2C_0_DEFAULT_CFG + default 0x12 + +config I2C_0_IRQ_PRI + default 0 + +endif # I2C_0 + +config I2C_1 + def_bool n + +if I2C_1 + +config I2C_1_DEFAULT_CFG + default 0x12 + +config I2C_1_IRQ_PRI + default 0 + +endif # I2C_1 + +endif # I2C + if PINMUX config PINMUX_K64 diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig index 8465f7ee144..4cffa602840 100644 --- a/drivers/i2c/Kconfig +++ b/drivers/i2c/Kconfig @@ -74,6 +74,13 @@ config I2C_ATMEL_SAM3 Says y to enable additional options to enable support for individual controllers. +config I2C_KSDK + bool "KSDK I2C driver" + depends on I2C && HAS_KSDK + default n + help + Enable the ksdk I2C driver. + config I2C_INIT_PRIORITY int depends on I2C diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile index 9219b7573c8..d3c750e689d 100644 --- a/drivers/i2c/Makefile +++ b/drivers/i2c/Makefile @@ -3,3 +3,4 @@ obj-$(CONFIG_I2C_QMSI) += i2c_qmsi.o obj-$(CONFIG_I2C_QMSI_SS) += i2c_qmsi_ss.o obj-$(CONFIG_I2C_QUARK_SE_SS) += i2c_quark_se_ss.o obj-$(CONFIG_I2C_ATMEL_SAM3) += i2c_atmel_sam3.o +obj-$(CONFIG_I2C_KSDK) += i2c_ksdk.o diff --git a/drivers/i2c/i2c_ksdk.c b/drivers/i2c/i2c_ksdk.c new file mode 100644 index 00000000000..8f8a7c7a1d3 --- /dev/null +++ b/drivers/i2c/i2c_ksdk.c @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2016 Freescale Semiconductor, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +#define DEV_CFG(dev) \ + ((struct i2c_ksdk_config * const)(dev)->config->config_info) +#define DEV_DATA(dev) \ + ((struct i2c_ksdk_data * const)(dev)->driver_data) +#define DEV_BASE(dev) \ + ((I2C_Type *)(DEV_CFG(dev))->base) + +struct i2c_ksdk_config { + I2C_Type *base; + clock_name_t clock_source; + void (*irq_config_func)(struct device *dev); + union dev_config default_cfg; +}; + +struct i2c_ksdk_data { + i2c_master_handle_t handle; + device_sync_call_t sync; + status_t callback_status; +}; + +static int i2c_ksdk_configure(struct device *dev, uint32_t dev_config_raw) +{ + I2C_Type *base = DEV_BASE(dev); + struct i2c_ksdk_config *config = DEV_CFG(dev); + union dev_config dev_config = (union dev_config)dev_config_raw; + uint32_t clock_freq; + uint32_t baudrate; + + if (!dev_config.bits.is_master_device) { + return -EINVAL; + } + + if (dev_config.bits.is_slave_read) { + return -EINVAL; + } + + if (dev_config.bits.use_10_bit_addr) { + return -EINVAL; + } + + switch (dev_config.bits.speed) { + case I2C_SPEED_STANDARD: + baudrate = KHZ(100); + break; + case I2C_SPEED_FAST: + baudrate = MHZ(1); + break; + default: + return -EINVAL; + } + + clock_freq = CLOCK_GetFreq(config->clock_source); + I2C_MasterSetBaudRate(base, baudrate, clock_freq); + + return 0; +} + +static void i2c_ksdk_master_transfer_callback(I2C_Type *base, + i2c_master_handle_t *handle, status_t status, void *userData) +{ + struct device *dev = userData; + struct i2c_ksdk_data *data = DEV_DATA(dev); + + data->callback_status = status; + device_sync_call_complete(&data->sync); +} + +static uint32_t i2c_ksdk_convert_flags(int msg_flags) +{ + uint32_t flags = 0; + + if (!(msg_flags & I2C_MSG_STOP)) { + flags |= kI2C_TransferNoStopFlag; + } + + if (msg_flags & I2C_MSG_RESTART) { + flags |= kI2C_TransferRepeatedStartFlag; + } + + return flags; +} + +static int i2c_ksdk_transfer(struct device *dev, struct i2c_msg *msgs, + uint8_t num_msgs, uint16_t addr) +{ + I2C_Type *base = DEV_BASE(dev); + struct i2c_ksdk_data *data = DEV_DATA(dev); + i2c_master_transfer_t transfer; + status_t status; + + /* Iterate over all the messages */ + for (int i = 0; i < num_msgs; i++) { + + /* Initialize the transfer descriptor */ + transfer.flags = i2c_ksdk_convert_flags(msgs->flags); + transfer.slaveAddress = addr; + transfer.direction = (msgs->flags & I2C_MSG_READ) + ? kI2C_Read : kI2C_Write; + transfer.subaddress = 0; + transfer.subaddressSize = 0; + transfer.data = msgs->buf; + transfer.dataSize = msgs->len; + + /* Start the transfer */ + status = I2C_MasterTransferNonBlocking(base, + &data->handle, &transfer); + + /* Return an error if the transfer didn't start successfully + * e.g., if the bus was busy + */ + if (status != kStatus_Success) { + return -EIO; + } + + /* Wait for the transfer to complete */ + device_sync_call_wait(&data->sync); + + /* Return an error if the transfer didn't complete + * successfully. e.g., nak, timeout, lost arbitration + */ + if (data->callback_status != kStatus_Success) { + return -EIO; + } + + /* Move to the next message */ + msgs++; + } + + return 0; +} + +static void i2c_ksdk_isr(void *arg) +{ + struct device *dev = (struct device *)arg; + I2C_Type *base = DEV_BASE(dev); + struct i2c_ksdk_data *data = DEV_DATA(dev); + + I2C_MasterTransferHandleIRQ(base, &data->handle); +} + +static int i2c_ksdk_init(struct device *dev) +{ + I2C_Type *base = DEV_BASE(dev); + struct i2c_ksdk_config *config = DEV_CFG(dev); + struct i2c_ksdk_data *data = DEV_DATA(dev); + uint32_t clock_freq; + i2c_master_config_t master_config; + int error; + + device_sync_call_init(&data->sync); + + clock_freq = CLOCK_GetFreq(config->clock_source); + I2C_MasterGetDefaultConfig(&master_config); + I2C_MasterInit(base, &master_config, clock_freq); + I2C_MasterTransferCreateHandle(base, &data->handle, + i2c_ksdk_master_transfer_callback, dev); + + error = i2c_ksdk_configure(dev, config->default_cfg.raw); + if (error) { + return error; + } + + config->irq_config_func(dev); + + return 0; +} + +static struct i2c_driver_api i2c_ksdk_driver_api = { + .configure = i2c_ksdk_configure, + .transfer = i2c_ksdk_transfer, +}; + +#ifdef CONFIG_I2C_0 +static void i2c_ksdk_config_func_0(struct device *dev); + +static struct i2c_ksdk_config i2c_ksdk_config_0 = { + .base = I2C0, + .clock_source = I2C0_CLK_SRC, + .irq_config_func = i2c_ksdk_config_func_0, + .default_cfg.raw = CONFIG_I2C_0_DEFAULT_CFG, +}; + +static struct i2c_ksdk_data i2c_ksdk_data_0; + +DEVICE_AND_API_INIT(i2c_ksdk_0, CONFIG_I2C_0_NAME, &i2c_ksdk_init, + &i2c_ksdk_data_0, &i2c_ksdk_config_0, + SECONDARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, + &i2c_ksdk_driver_api); + +static void i2c_ksdk_config_func_0(struct device *dev) +{ + IRQ_CONNECT(IRQ_I2C0, CONFIG_I2C_0_IRQ_PRI, + i2c_ksdk_isr, DEVICE_GET(i2c_ksdk_0), 0); + + irq_enable(I2C0_IRQn); +} +#endif /* CONFIG_I2C_0 */ + +#ifdef CONFIG_I2C_1 +static void i2c_ksdk_config_func_1(struct device *dev); + +static struct i2c_ksdk_config i2c_ksdk_config_1 = { + .base = I2C1, + .clock_source = I2C1_CLK_SRC, + .irq_config_func = i2c_ksdk_config_func_1, + .default_cfg.raw = CONFIG_I2C_1_DEFAULT_CFG, +}; + +static struct i2c_ksdk_data i2c_ksdk_data_1; + +DEVICE_AND_API_INIT(i2c_ksdk_1, CONFIG_I2C_1_NAME, &i2c_ksdk_init, + &i2c_ksdk_data_1, &i2c_ksdk_config_1, + SECONDARY, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, + &i2c_ksdk_driver_api); + +static void i2c_ksdk_config_func_1(struct device *dev) +{ + IRQ_CONNECT(IRQ_I2C1, CONFIG_I2C_1_IRQ_PRI, + i2c_ksdk_isr, DEVICE_GET(i2c_ksdk_1), 0); + + irq_enable(I2C1_IRQn); +} +#endif /* CONFIG_I2C_1 */ diff --git a/ext/hal/ksdk/Makefile b/ext/hal/ksdk/Makefile index 07618383d0e..3a265c006ab 100644 --- a/ext/hal/ksdk/Makefile +++ b/ext/hal/ksdk/Makefile @@ -35,4 +35,7 @@ KBUILD_CFLAGS += -D$(KSDK_CPU) # for every ksdk device. obj-y += devices/$(KSDK_DEVICE)/fsl_clock.o +# Build ksdk drivers that can be used for multiple SoC's. +obj-y += drivers/ + endif diff --git a/ext/hal/ksdk/drivers/Makefile b/ext/hal/ksdk/drivers/Makefile new file mode 100644 index 00000000000..1b547fb1062 --- /dev/null +++ b/ext/hal/ksdk/drivers/Makefile @@ -0,0 +1,17 @@ +# Makefile - Kinetis SDK drivers +# +# Copyright (c) 2016, Freescale Semiconductor, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +obj-$(CONFIG_I2C_KSDK) += fsl_i2c.o