2017-05-21 02:47:32 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2017 Piotr Mienkowski
|
2023-03-07 19:25:10 +01:00
|
|
|
|
* Copyright (c) 2023 Gerson Fernando Budke
|
2017-05-21 02:47:32 +02:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
2020-04-20 11:27:42 +02:00
|
|
|
|
#define DT_DRV_COMPAT atmel_sam_i2c_twi
|
|
|
|
|
|
2017-05-21 02:47:32 +02:00
|
|
|
|
/** @file
|
|
|
|
|
* @brief I2C bus (TWI) driver for Atmel SAM MCU family.
|
|
|
|
|
*
|
|
|
|
|
* Limitations:
|
|
|
|
|
* - Only I2C Master Mode with 7 bit addressing is currently supported.
|
|
|
|
|
* - No reentrancy support.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
|
#include <zephyr/sys/__assert.h>
|
2017-05-21 02:47:32 +02:00
|
|
|
|
#include <stdbool.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
|
#include <zephyr/device.h>
|
|
|
|
|
#include <zephyr/init.h>
|
2017-05-21 02:47:32 +02:00
|
|
|
|
#include <soc.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
|
#include <zephyr/drivers/i2c.h>
|
|
|
|
|
#include <zephyr/drivers/pinctrl.h>
|
2023-03-07 19:25:10 +01:00
|
|
|
|
#include <zephyr/drivers/clock_control/atmel_sam_pmc.h>
|
2017-05-21 02:47:32 +02:00
|
|
|
|
|
2018-09-17 20:24:08 +02:00
|
|
|
|
#define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
|
2022-05-06 10:25:46 +02:00
|
|
|
|
#include <zephyr/logging/log.h>
|
2022-10-17 10:24:11 +02:00
|
|
|
|
#include <zephyr/irq.h>
|
2018-11-02 15:17:09 +01:00
|
|
|
|
LOG_MODULE_REGISTER(i2c_sam_twi);
|
2018-09-17 20:24:08 +02:00
|
|
|
|
|
|
|
|
|
#include "i2c-priv.h"
|
2017-05-21 02:47:32 +02:00
|
|
|
|
|
|
|
|
|
/** I2C bus speed [Hz] in Standard Mode */
|
|
|
|
|
#define BUS_SPEED_STANDARD_HZ 100000U
|
|
|
|
|
/** I2C bus speed [Hz] in Fast Mode */
|
|
|
|
|
#define BUS_SPEED_FAST_HZ 400000U
|
|
|
|
|
/* Maximum value of Clock Divider (CKDIV) */
|
|
|
|
|
#define CKDIV_MAX 7
|
|
|
|
|
|
|
|
|
|
/* Device constant configuration parameters */
|
|
|
|
|
struct i2c_sam_twi_dev_cfg {
|
|
|
|
|
Twi *regs;
|
|
|
|
|
void (*irq_config)(void);
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint32_t bitrate;
|
2023-03-07 19:25:10 +01:00
|
|
|
|
const struct atmel_sam_pmc_config clock_cfg;
|
2022-03-12 19:56:05 +01:00
|
|
|
|
const struct pinctrl_dev_config *pcfg;
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint8_t irq_id;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct twi_msg {
|
|
|
|
|
/* Buffer containing data to read or write */
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint8_t *buf;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
/* Length of the buffer */
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint32_t len;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
/* Index of the next byte to be read/written from/to the buffer */
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint32_t idx;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
/* Value of TWI_SR at the end of the message */
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint32_t twi_sr;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
/* Transfer flags as defined in the i2c.h file */
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint8_t flags;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Device run time data */
|
|
|
|
|
struct i2c_sam_twi_dev_data {
|
2022-05-30 11:58:44 +02:00
|
|
|
|
struct k_sem lock;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
struct k_sem sem;
|
|
|
|
|
struct twi_msg msg;
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
|
static int i2c_clk_set(Twi *const twi, uint32_t speed)
|
2017-05-21 02:47:32 +02:00
|
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint32_t ck_div = 0U;
|
|
|
|
|
uint32_t cl_div;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
bool div_completed = false;
|
|
|
|
|
|
|
|
|
|
/* From the datasheet "TWI Clock Waveform Generator Register"
|
|
|
|
|
* T_low = ( ( CLDIV × 2^CKDIV ) + 4 ) × T_MCK
|
|
|
|
|
*/
|
|
|
|
|
while (!div_completed) {
|
2019-03-28 21:57:54 +01:00
|
|
|
|
cl_div = ((SOC_ATMEL_SAM_MCK_FREQ_HZ / (speed * 2U)) - 4)
|
2017-05-21 02:47:32 +02:00
|
|
|
|
/ (1 << ck_div);
|
|
|
|
|
|
2019-03-27 02:57:45 +01:00
|
|
|
|
if (cl_div <= 255U) {
|
2017-05-21 02:47:32 +02:00
|
|
|
|
div_completed = true;
|
|
|
|
|
} else {
|
|
|
|
|
ck_div++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ck_div > CKDIV_MAX) {
|
2018-09-17 20:24:08 +02:00
|
|
|
|
LOG_ERR("Failed to configure I2C clock");
|
2017-05-21 02:47:32 +02:00
|
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Set TWI clock duty cycle to 50% */
|
|
|
|
|
twi->TWI_CWGR = TWI_CWGR_CLDIV(cl_div) | TWI_CWGR_CHDIV(cl_div)
|
|
|
|
|
| TWI_CWGR_CKDIV(ck_div);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
|
static int i2c_sam_twi_configure(const struct device *dev, uint32_t config)
|
2017-05-21 02:47:32 +02:00
|
|
|
|
{
|
2022-01-18 16:06:19 +01:00
|
|
|
|
const struct i2c_sam_twi_dev_cfg *const dev_cfg = dev->config;
|
2022-05-30 11:58:44 +02:00
|
|
|
|
struct i2c_sam_twi_dev_data *const dev_data = dev->data;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
Twi *const twi = dev_cfg->regs;
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint32_t bitrate;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
int ret;
|
|
|
|
|
|
2022-05-23 19:15:02 +02:00
|
|
|
|
if (!(config & I2C_MODE_CONTROLLER)) {
|
2018-09-17 20:24:08 +02:00
|
|
|
|
LOG_ERR("Master Mode is not enabled");
|
2017-05-21 02:47:32 +02:00
|
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config & I2C_ADDR_10_BITS) {
|
2018-09-17 20:24:08 +02:00
|
|
|
|
LOG_ERR("I2C 10-bit addressing is currently not supported");
|
|
|
|
|
LOG_ERR("Please submit a patch");
|
2017-05-21 02:47:32 +02:00
|
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Configure clock */
|
|
|
|
|
switch (I2C_SPEED_GET(config)) {
|
|
|
|
|
case I2C_SPEED_STANDARD:
|
|
|
|
|
bitrate = BUS_SPEED_STANDARD_HZ;
|
|
|
|
|
break;
|
|
|
|
|
case I2C_SPEED_FAST:
|
|
|
|
|
bitrate = BUS_SPEED_FAST_HZ;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2018-09-17 20:24:08 +02:00
|
|
|
|
LOG_ERR("Unsupported I2C speed value");
|
2017-05-21 02:47:32 +02:00
|
|
|
|
return -EIO;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-30 11:58:44 +02:00
|
|
|
|
k_sem_take(&dev_data->lock, K_FOREVER);
|
|
|
|
|
|
2017-05-21 02:47:32 +02:00
|
|
|
|
/* Setup clock waveform */
|
|
|
|
|
ret = i2c_clk_set(twi, bitrate);
|
|
|
|
|
if (ret < 0) {
|
2022-05-30 11:58:44 +02:00
|
|
|
|
goto unlock;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Disable Slave Mode */
|
|
|
|
|
twi->TWI_CR = TWI_CR_SVDIS;
|
|
|
|
|
|
|
|
|
|
/* Enable Master Mode */
|
|
|
|
|
twi->TWI_CR = TWI_CR_MSEN;
|
|
|
|
|
|
2022-05-30 11:58:44 +02:00
|
|
|
|
ret = 0;
|
|
|
|
|
unlock:
|
|
|
|
|
k_sem_give(&dev_data->lock);
|
|
|
|
|
|
|
|
|
|
return ret;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
|
static void write_msg_start(Twi *const twi, struct twi_msg *msg, uint8_t daddr)
|
2017-05-21 02:47:32 +02:00
|
|
|
|
{
|
|
|
|
|
/* Set slave address and number of internal address bytes. */
|
|
|
|
|
twi->TWI_MMR = TWI_MMR_DADR(daddr);
|
|
|
|
|
|
|
|
|
|
/* Write first data byte on I2C bus */
|
|
|
|
|
twi->TWI_THR = msg->buf[msg->idx++];
|
|
|
|
|
|
|
|
|
|
/* Enable Transmit Ready and Transmission Completed interrupts */
|
|
|
|
|
twi->TWI_IER = TWI_IER_TXRDY | TWI_IER_TXCOMP | TWI_IER_NACK;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-27 18:26:57 +02:00
|
|
|
|
static void read_msg_start(Twi *const twi, struct twi_msg *msg, uint8_t daddr)
|
2017-05-21 02:47:32 +02:00
|
|
|
|
{
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint32_t twi_cr_stop;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
|
|
|
|
|
/* Set slave address and number of internal address bytes */
|
|
|
|
|
twi->TWI_MMR = TWI_MMR_MREAD | TWI_MMR_DADR(daddr);
|
|
|
|
|
|
|
|
|
|
/* In single data byte read the START and STOP must both be set */
|
2019-03-27 02:57:45 +01:00
|
|
|
|
twi_cr_stop = (msg->len == 1U) ? TWI_CR_STOP : 0;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
/* Start the transfer by sending START condition */
|
|
|
|
|
twi->TWI_CR = TWI_CR_START | twi_cr_stop;
|
|
|
|
|
|
|
|
|
|
/* Enable Receive Ready and Transmission Completed interrupts */
|
|
|
|
|
twi->TWI_IER = TWI_IER_RXRDY | TWI_IER_TXCOMP | TWI_IER_NACK;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
|
static int i2c_sam_twi_transfer(const struct device *dev,
|
|
|
|
|
struct i2c_msg *msgs,
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint8_t num_msgs, uint16_t addr)
|
2017-05-21 02:47:32 +02:00
|
|
|
|
{
|
2022-01-18 16:06:19 +01:00
|
|
|
|
const struct i2c_sam_twi_dev_cfg *const dev_cfg = dev->config;
|
|
|
|
|
struct i2c_sam_twi_dev_data *const dev_data = dev->data;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
Twi *const twi = dev_cfg->regs;
|
2022-05-30 11:58:44 +02:00
|
|
|
|
int ret;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
|
|
|
|
|
__ASSERT_NO_MSG(msgs);
|
|
|
|
|
if (!num_msgs) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-05-30 11:58:44 +02:00
|
|
|
|
k_sem_take(&dev_data->lock, K_FOREVER);
|
2017-05-21 02:47:32 +02:00
|
|
|
|
|
|
|
|
|
/* Clear pending interrupts, such as NACK. */
|
|
|
|
|
(void)twi->TWI_SR;
|
|
|
|
|
|
|
|
|
|
/* Set number of internal address bytes to 0, not used. */
|
|
|
|
|
twi->TWI_IADR = 0;
|
|
|
|
|
|
|
|
|
|
for (; num_msgs > 0; num_msgs--, msgs++) {
|
|
|
|
|
dev_data->msg.buf = msgs->buf;
|
|
|
|
|
dev_data->msg.len = msgs->len;
|
2018-11-29 20:12:22 +01:00
|
|
|
|
dev_data->msg.idx = 0U;
|
|
|
|
|
dev_data->msg.twi_sr = 0U;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
dev_data->msg.flags = msgs->flags;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* REMARK: Dirty workaround:
|
|
|
|
|
*
|
|
|
|
|
* The controller does not have a documented, generic way to
|
|
|
|
|
* issue RESTART when changing transfer direction as master.
|
|
|
|
|
* Send a stop condition in such a case.
|
|
|
|
|
*/
|
|
|
|
|
if (num_msgs > 1) {
|
|
|
|
|
if ((msgs[0].flags & I2C_MSG_RW_MASK) !=
|
|
|
|
|
(msgs[1].flags & I2C_MSG_RW_MASK)) {
|
|
|
|
|
dev_data->msg.flags |= I2C_MSG_STOP;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((msgs->flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) {
|
|
|
|
|
read_msg_start(twi, &dev_data->msg, addr);
|
|
|
|
|
} else {
|
|
|
|
|
write_msg_start(twi, &dev_data->msg, addr);
|
|
|
|
|
}
|
|
|
|
|
/* Wait for the transfer to complete */
|
|
|
|
|
k_sem_take(&dev_data->sem, K_FOREVER);
|
|
|
|
|
|
|
|
|
|
if (dev_data->msg.twi_sr > 0) {
|
|
|
|
|
/* Something went wrong */
|
2022-05-30 11:58:44 +02:00
|
|
|
|
ret = -EIO;
|
|
|
|
|
goto unlock;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-30 11:58:44 +02:00
|
|
|
|
ret = 0;
|
|
|
|
|
unlock:
|
|
|
|
|
k_sem_give(&dev_data->lock);
|
|
|
|
|
|
|
|
|
|
return ret;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
|
|
|
|
static void i2c_sam_twi_isr(const struct device *dev)
|
2017-05-21 02:47:32 +02:00
|
|
|
|
{
|
2022-01-18 16:06:19 +01:00
|
|
|
|
const struct i2c_sam_twi_dev_cfg *const dev_cfg = dev->config;
|
|
|
|
|
struct i2c_sam_twi_dev_data *const dev_data = dev->data;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
Twi *const twi = dev_cfg->regs;
|
|
|
|
|
struct twi_msg *msg = &dev_data->msg;
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint32_t isr_status;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
|
|
|
|
|
/* Retrieve interrupt status */
|
|
|
|
|
isr_status = twi->TWI_SR & twi->TWI_IMR;
|
|
|
|
|
|
|
|
|
|
/* Not Acknowledged */
|
|
|
|
|
if (isr_status & TWI_SR_NACK) {
|
|
|
|
|
msg->twi_sr = isr_status;
|
|
|
|
|
goto tx_comp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Byte received */
|
|
|
|
|
if (isr_status & TWI_SR_RXRDY) {
|
|
|
|
|
|
|
|
|
|
msg->buf[msg->idx++] = twi->TWI_RHR;
|
|
|
|
|
|
2019-03-27 02:57:45 +01:00
|
|
|
|
if (msg->idx == msg->len - 1U) {
|
2017-05-21 02:47:32 +02:00
|
|
|
|
/* Send a STOP condition on the TWI */
|
|
|
|
|
twi->TWI_CR = TWI_CR_STOP;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Byte sent */
|
|
|
|
|
if (isr_status & TWI_SR_TXRDY) {
|
|
|
|
|
if (msg->idx == msg->len) {
|
|
|
|
|
if (msg->flags & I2C_MSG_STOP) {
|
|
|
|
|
/* Send a STOP condition on the TWI */
|
|
|
|
|
twi->TWI_CR = TWI_CR_STOP;
|
|
|
|
|
/* Disable Transmit Ready interrupt */
|
|
|
|
|
twi->TWI_IDR = TWI_IDR_TXRDY;
|
|
|
|
|
} else {
|
|
|
|
|
/* Transmission completed */
|
|
|
|
|
goto tx_comp;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
twi->TWI_THR = msg->buf[msg->idx++];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Transmission completed */
|
|
|
|
|
if (isr_status & TWI_SR_TXCOMP) {
|
|
|
|
|
goto tx_comp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
tx_comp:
|
|
|
|
|
/* Disable all enabled interrupts */
|
|
|
|
|
twi->TWI_IDR = twi->TWI_IMR;
|
|
|
|
|
/* We are done */
|
|
|
|
|
k_sem_give(&dev_data->sem);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
|
static int i2c_sam_twi_initialize(const struct device *dev)
|
2017-05-21 02:47:32 +02:00
|
|
|
|
{
|
2022-01-18 16:06:19 +01:00
|
|
|
|
const struct i2c_sam_twi_dev_cfg *const dev_cfg = dev->config;
|
|
|
|
|
struct i2c_sam_twi_dev_data *const dev_data = dev->data;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
Twi *const twi = dev_cfg->regs;
|
2020-05-27 18:26:57 +02:00
|
|
|
|
uint32_t bitrate_cfg;
|
2017-05-21 02:47:32 +02:00
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
/* Configure interrupts */
|
|
|
|
|
dev_cfg->irq_config();
|
|
|
|
|
|
2022-05-30 11:58:44 +02:00
|
|
|
|
/* Initialize semaphores */
|
|
|
|
|
k_sem_init(&dev_data->lock, 1, 1);
|
2017-05-21 02:47:32 +02:00
|
|
|
|
k_sem_init(&dev_data->sem, 0, 1);
|
|
|
|
|
|
|
|
|
|
/* Connect pins to the peripheral */
|
2022-03-12 19:56:05 +01:00
|
|
|
|
ret = pinctrl_apply_state(dev_cfg->pcfg, PINCTRL_STATE_DEFAULT);
|
|
|
|
|
if (ret < 0) {
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2017-05-21 02:47:32 +02:00
|
|
|
|
|
2023-03-07 19:25:10 +01:00
|
|
|
|
/* Enable TWI clock in PMC */
|
|
|
|
|
(void)clock_control_on(SAM_DT_PMC_CONTROLLER,
|
2023-03-28 08:24:07 +02:00
|
|
|
|
(clock_control_subsys_t)&dev_cfg->clock_cfg);
|
2017-05-21 02:47:32 +02:00
|
|
|
|
|
|
|
|
|
/* Reset TWI module */
|
|
|
|
|
twi->TWI_CR = TWI_CR_SWRST;
|
|
|
|
|
|
2019-03-12 22:15:42 +01:00
|
|
|
|
bitrate_cfg = i2c_map_dt_bitrate(dev_cfg->bitrate);
|
2017-05-21 02:47:32 +02:00
|
|
|
|
|
2022-05-23 19:15:02 +02:00
|
|
|
|
ret = i2c_sam_twi_configure(dev, I2C_MODE_CONTROLLER | bitrate_cfg);
|
2017-05-21 02:47:32 +02:00
|
|
|
|
if (ret < 0) {
|
2022-02-20 15:37:38 +01:00
|
|
|
|
LOG_ERR("Failed to initialize %s device", dev->name);
|
2017-05-21 02:47:32 +02:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Enable module's IRQ */
|
|
|
|
|
irq_enable(dev_cfg->irq_id);
|
|
|
|
|
|
2022-02-20 15:37:38 +01:00
|
|
|
|
LOG_INF("Device %s initialized", dev->name);
|
2017-05-21 02:47:32 +02:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct i2c_driver_api i2c_sam_twi_driver_api = {
|
|
|
|
|
.configure = i2c_sam_twi_configure,
|
|
|
|
|
.transfer = i2c_sam_twi_transfer,
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-20 11:27:42 +02:00
|
|
|
|
#define I2C_TWI_SAM_INIT(n) \
|
2022-03-12 19:56:05 +01:00
|
|
|
|
PINCTRL_DT_INST_DEFINE(n); \
|
2020-04-20 11:27:42 +02:00
|
|
|
|
static void i2c##n##_sam_irq_config(void) \
|
|
|
|
|
{ \
|
|
|
|
|
IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), \
|
|
|
|
|
i2c_sam_twi_isr, \
|
2020-12-09 18:00:35 +01:00
|
|
|
|
DEVICE_DT_INST_GET(n), 0); \
|
2020-04-20 11:27:42 +02:00
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
static const struct i2c_sam_twi_dev_cfg i2c##n##_sam_config = { \
|
|
|
|
|
.regs = (Twi *)DT_INST_REG_ADDR(n), \
|
|
|
|
|
.irq_config = i2c##n##_sam_irq_config, \
|
2023-03-07 19:25:10 +01:00
|
|
|
|
.clock_cfg = SAM_DT_INST_CLOCK_PMC_CFG(n), \
|
2020-04-20 11:27:42 +02:00
|
|
|
|
.irq_id = DT_INST_IRQN(n), \
|
2022-03-12 19:56:05 +01:00
|
|
|
|
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
|
2020-04-20 11:27:42 +02:00
|
|
|
|
.bitrate = DT_INST_PROP(n, clock_frequency), \
|
|
|
|
|
}; \
|
|
|
|
|
\
|
|
|
|
|
static struct i2c_sam_twi_dev_data i2c##n##_sam_data; \
|
|
|
|
|
\
|
2021-10-27 22:07:10 +02:00
|
|
|
|
I2C_DEVICE_DT_INST_DEFINE(n, i2c_sam_twi_initialize, \
|
2021-04-28 11:03:29 +02:00
|
|
|
|
NULL, \
|
2020-04-20 11:27:42 +02:00
|
|
|
|
&i2c##n##_sam_data, &i2c##n##_sam_config, \
|
|
|
|
|
POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \
|
2020-05-07 21:09:05 +02:00
|
|
|
|
&i2c_sam_twi_driver_api);
|
2020-04-20 11:27:42 +02:00
|
|
|
|
|
2020-05-06 20:23:07 +02:00
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(I2C_TWI_SAM_INIT)
|