Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | /*
* Copyright (c) 2019, Henrik Brix Andersen <henrik@brixandersen.dk>
*
* Based on the i2c_mcux_lpi2c.c driver, which is:
* Copyright (c) 2016 Freescale Semiconductor, Inc.
* Copyright (c) 2019, NXP
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <drivers/i2c.h>
#include <drivers/clock_control.h>
#include <fsl_lpi2c.h>
#include <logging/log.h>
LOG_MODULE_REGISTER(rv32m1_lpi2c);
#include "i2c-priv.h"
struct rv32m1_lpi2c_config {
LPI2C_Type *base;
char *clock_controller;
clock_control_subsys_t clock_subsys;
clock_ip_name_t clock_ip_name;
u32_t clock_ip_src;
u32_t bitrate;
void (*irq_config_func)(struct device *dev);
};
struct rv32m1_lpi2c_data {
lpi2c_master_handle_t handle;
struct k_sem transfer_sync;
struct k_sem completion_sync;
status_t completion_status;
};
static int rv32m1_lpi2c_configure(struct device *dev, u32_t dev_config)
{
const struct rv32m1_lpi2c_config *config = dev->config->config_info;
struct device *clk;
u32_t baudrate;
u32_t clk_freq;
int err;
if (!(I2C_MODE_MASTER & dev_config)) {
/* Slave mode not supported - yet */
LOG_ERR("Slave mode not supported");
return -ENOTSUP;
}
if (I2C_ADDR_10_BITS & dev_config) {
/* FSL LPI2C driver only supports 7-bit addressing */
LOG_ERR("10 bit addressing not supported");
return -ENOTSUP;
}
switch (I2C_SPEED_GET(dev_config)) {
case I2C_SPEED_STANDARD:
baudrate = KHZ(100);
break;
case I2C_SPEED_FAST:
baudrate = KHZ(400);
break;
case I2C_SPEED_FAST_PLUS:
baudrate = MHZ(1);
break;
/* TODO: only if SCL pin implements current source pull-up */
/* case I2C_SPEED_HIGH: */
/* baudrate = KHZ(3400); */
/* break; */
/* TODO: ultra-fast requires pin_config setting */
/* case I2C_SPEED_ULTRA: */
/* baudrate = MHZ(5); */
/* break; */
default:
LOG_ERR("Unsupported speed");
return -ENOTSUP;
}
clk = device_get_binding(config->clock_controller);
if (!clk) {
LOG_ERR("Could not get clock controller '%s'",
config->clock_controller);
return -EINVAL;
}
err = clock_control_get_rate(clk, config->clock_subsys, &clk_freq);
if (err) {
LOG_ERR("Could not get clock frequency (err %d)", err);
return -EINVAL;
}
LPI2C_MasterSetBaudRate(config->base, clk_freq, baudrate);
return 0;
}
static void rv32m1_lpi2c_master_transfer_callback(LPI2C_Type *base,
lpi2c_master_handle_t *handle,
status_t completionStatus,
void *userData)
{
struct device *dev = userData;
struct rv32m1_lpi2c_data *data = dev->driver_data;
ARG_UNUSED(base);
ARG_UNUSED(handle);
data->completion_status = completionStatus;
k_sem_give(&data->completion_sync);
}
static u32_t rv32m1_lpi2c_convert_flags(int msg_flags)
{
u32_t flags = 0U;
if (!(msg_flags & I2C_MSG_STOP)) {
flags |= kLPI2C_TransferNoStopFlag;
}
if (msg_flags & I2C_MSG_RESTART) {
flags |= kLPI2C_TransferRepeatedStartFlag;
}
return flags;
}
static int rv32m1_lpi2c_transfer(struct device *dev, struct i2c_msg *msgs,
u8_t num_msgs, u16_t addr)
{
const struct rv32m1_lpi2c_config *config = dev->config->config_info;
struct rv32m1_lpi2c_data *data = dev->driver_data;
lpi2c_master_transfer_t transfer;
status_t status;
int ret = 0;
k_sem_take(&data->transfer_sync, K_FOREVER);
/* Iterate over all the messages */
for (int i = 0; i < num_msgs; i++) {
if (I2C_MSG_ADDR_10_BITS & msgs->flags) {
ret = -ENOTSUP;
goto out;
}
/* Initialize the transfer descriptor */
transfer.flags = rv32m1_lpi2c_convert_flags(msgs->flags);
/* Prevent the controller to send a start condition between
* messages, except if explicitly requested.
*/
if (i != 0 && !(msgs->flags & I2C_MSG_RESTART)) {
transfer.flags |= kLPI2C_TransferNoStartFlag;
}
transfer.slaveAddress = addr;
transfer.direction = (msgs->flags & I2C_MSG_READ)
? kLPI2C_Read : kLPI2C_Write;
transfer.subaddress = 0;
transfer.subaddressSize = 0;
transfer.data = msgs->buf;
transfer.dataSize = msgs->len;
/* Start the transfer */
status = LPI2C_MasterTransferNonBlocking(config->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) {
LOG_DBG("Could not start transfer (status %d)", status);
ret = -EIO;
goto out;
}
/* Wait for the transfer to complete */
k_sem_take(&data->completion_sync, K_FOREVER);
/* Return an error if the transfer didn't complete
* successfully. e.g., nak, timeout, lost arbitration
*/
if (data->completion_status != kStatus_Success) {
LOG_DBG("Transfer failed (status %d)",
data->completion_status);
LPI2C_MasterTransferAbort(config->base, &data->handle);
ret = -EIO;
goto out;
}
/* Move to the next message */
msgs++;
}
out:
k_sem_give(&data->transfer_sync);
return ret;
}
static void rv32m1_lpi2c_isr(void *arg)
{
struct device *dev = (struct device *)arg;
const struct rv32m1_lpi2c_config *config = dev->config->config_info;
struct rv32m1_lpi2c_data *data = dev->driver_data;
LPI2C_MasterTransferHandleIRQ(config->base, &data->handle);
}
static int rv32m1_lpi2c_init(struct device *dev)
{
const struct rv32m1_lpi2c_config *config = dev->config->config_info;
struct rv32m1_lpi2c_data *data = dev->driver_data;
lpi2c_master_config_t master_config;
struct device *clk;
u32_t clk_freq, dev_cfg;
int err;
CLOCK_SetIpSrc(config->clock_ip_name, config->clock_ip_src);
clk = device_get_binding(config->clock_controller);
if (!clk) {
LOG_ERR("Could not get clock controller '%s'",
config->clock_controller);
return -EINVAL;
}
err = clock_control_on(clk, config->clock_subsys);
if (err) {
LOG_ERR("Could not turn on clock (err %d)", err);
return -EINVAL;
}
err = clock_control_get_rate(clk, config->clock_subsys, &clk_freq);
if (err) {
LOG_ERR("Could not get clock frequency (err %d)", err);
return -EINVAL;
}
LPI2C_MasterGetDefaultConfig(&master_config);
LPI2C_MasterInit(config->base, &master_config, clk_freq);
LPI2C_MasterTransferCreateHandle(config->base, &data->handle,
rv32m1_lpi2c_master_transfer_callback,
dev);
dev_cfg = i2c_map_dt_bitrate(config->bitrate);
err = rv32m1_lpi2c_configure(dev, dev_cfg | I2C_MODE_MASTER);
if (err) {
LOG_ERR("Could not configure controller (err %d)", err);
return err;
}
config->irq_config_func(dev);
return 0;
}
static const struct i2c_driver_api rv32m1_lpi2c_driver_api = {
.configure = rv32m1_lpi2c_configure,
.transfer = rv32m1_lpi2c_transfer,
};
#define RV32M1_LPI2C_DEVICE(id) \
static void rv32m1_lpi2c_irq_config_func_##id(struct device *dev); \
static const struct rv32m1_lpi2c_config rv32m1_lpi2c_##id##_config = { \
.base = \
(LPI2C_Type *)DT_OPENISA_RV32M1_LPI2C_I2C_##id##_BASE_ADDRESS, \
.clock_controller = \
DT_OPENISA_RV32M1_LPI2C_I2C_##id##_CLOCK_CONTROLLER, \
.clock_subsys = \
(clock_control_subsys_t) \
DT_OPENISA_RV32M1_LPI2C_I2C_##id##_CLOCK_NAME, \
.clock_ip_name = kCLOCK_Lpi2c##id, \
.clock_ip_src = kCLOCK_IpSrcFircAsync, \
.bitrate = DT_OPENISA_RV32M1_LPI2C_I2C_##id##_CLOCK_FREQUENCY, \
.irq_config_func = rv32m1_lpi2c_irq_config_func_##id, \
}; \
static struct rv32m1_lpi2c_data rv32m1_lpi2c_##id##_data = { \
.transfer_sync = Z_SEM_INITIALIZER( \
rv32m1_lpi2c_##id##_data.transfer_sync, 1, 1), \
.completion_sync = Z_SEM_INITIALIZER( \
rv32m1_lpi2c_##id##_data.completion_sync, 0, 1), \
}; \
DEVICE_AND_API_INIT(rv32m1_lpi2c_##id, \
DT_OPENISA_RV32M1_LPI2C_I2C_##id##_LABEL, \
&rv32m1_lpi2c_init, \
&rv32m1_lpi2c_##id##_data, \
&rv32m1_lpi2c_##id##_config, \
POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \
&rv32m1_lpi2c_driver_api); \
static void rv32m1_lpi2c_irq_config_func_##id(struct device *dev) \
{ \
IRQ_CONNECT(DT_OPENISA_RV32M1_LPI2C_I2C_##id##_IRQ_0, \
0, \
rv32m1_lpi2c_isr, DEVICE_GET(rv32m1_lpi2c_##id), \
0); \
irq_enable(DT_OPENISA_RV32M1_LPI2C_I2C_##id##_IRQ_0); \
} \
#ifdef CONFIG_I2C_0
RV32M1_LPI2C_DEVICE(0)
#endif
#ifdef CONFIG_I2C_1
RV32M1_LPI2C_DEVICE(1)
#endif
#ifdef CONFIG_I2C_2
RV32M1_LPI2C_DEVICE(2)
#endif
#ifdef CONFIG_I2C_3
RV32M1_LPI2C_DEVICE(3)
#endif
|