2019-04-23 15:33:18 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Brett Witherspoon
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-24 15:34:46 -05:00
|
|
|
#define DT_DRV_COMPAT ti_cc13xx_cc26xx_i2c
|
|
|
|
|
2019-04-23 15:33:18 -05:00
|
|
|
#include <kernel.h>
|
2019-06-25 15:53:54 -04:00
|
|
|
#include <drivers/i2c.h>
|
2021-05-03 17:26:38 +02:00
|
|
|
#include <pm/device.h>
|
2021-06-04 10:31:31 +03:00
|
|
|
#include <pm/pm.h>
|
2019-04-23 15:33:18 -05:00
|
|
|
|
|
|
|
#define LOG_LEVEL CONFIG_I2C_LOG_LEVEL
|
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(i2c_cc13xx_cc26xx);
|
|
|
|
|
|
|
|
#include <driverlib/i2c.h>
|
|
|
|
#include <driverlib/ioc.h>
|
|
|
|
#include <driverlib/prcm.h>
|
|
|
|
|
2019-11-06 14:56:13 -08:00
|
|
|
#include <ti/drivers/Power.h>
|
|
|
|
#include <ti/drivers/power/PowerCC26X2.h>
|
|
|
|
|
2019-04-23 15:33:18 -05:00
|
|
|
#include "i2c-priv.h"
|
|
|
|
|
|
|
|
struct i2c_cc13xx_cc26xx_data {
|
|
|
|
struct k_sem lock;
|
|
|
|
struct k_sem complete;
|
2020-05-27 11:26:57 -05:00
|
|
|
volatile uint32_t error;
|
2020-09-01 18:31:40 -04:00
|
|
|
#ifdef CONFIG_PM
|
2019-11-06 14:56:13 -08:00
|
|
|
Power_NotifyObj postNotify;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t dev_config;
|
2019-11-06 14:56:13 -08:00
|
|
|
#endif
|
2019-04-23 15:33:18 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
struct i2c_cc13xx_cc26xx_config {
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t base;
|
|
|
|
uint32_t scl_pin;
|
|
|
|
uint32_t sda_pin;
|
2019-04-23 15:33:18 -05:00
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static inline struct i2c_cc13xx_cc26xx_data *get_dev_data(const struct device *dev)
|
2019-04-23 15:33:18 -05:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
return dev->data;
|
2019-04-23 15:33:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const struct i2c_cc13xx_cc26xx_config *
|
2020-04-30 20:33:38 +02:00
|
|
|
get_dev_config(const struct device *dev)
|
2019-04-23 15:33:18 -05:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
return dev->config;
|
2019-04-23 15:33:18 -05:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int i2c_cc13xx_cc26xx_transmit(const struct device *dev,
|
|
|
|
const uint8_t *buf,
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t len, uint16_t addr)
|
2019-04-23 15:33:18 -05:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
const uint32_t base = get_dev_config(dev)->base;
|
2019-04-23 15:33:18 -05:00
|
|
|
struct i2c_cc13xx_cc26xx_data *data = get_dev_data(dev);
|
|
|
|
|
|
|
|
/* Sending address without data is not supported */
|
|
|
|
if (len == 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
I2CMasterSlaveAddrSet(base, addr, false);
|
|
|
|
|
|
|
|
/* The following assumes a single master. Use I2CMasterBusBusy() if
|
|
|
|
* wanting to implement multiple master support.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Single transmission */
|
|
|
|
if (len == 1) {
|
|
|
|
I2CMasterDataPut(base, *buf);
|
|
|
|
|
|
|
|
I2CMasterControl(base, I2C_MASTER_CMD_SINGLE_SEND);
|
|
|
|
|
|
|
|
k_sem_take(&data->complete, K_FOREVER);
|
|
|
|
|
|
|
|
return data->error == I2C_MASTER_ERR_NONE ? 0 : -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Burst transmission */
|
|
|
|
I2CMasterDataPut(base, buf[0]);
|
|
|
|
|
|
|
|
I2CMasterControl(base, I2C_MASTER_CMD_BURST_SEND_START);
|
|
|
|
|
|
|
|
k_sem_take(&data->complete, K_FOREVER);
|
|
|
|
|
|
|
|
if (data->error != I2C_MASTER_ERR_NONE) {
|
|
|
|
goto send_error_stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < len - 1; i++) {
|
|
|
|
I2CMasterDataPut(base, buf[i]);
|
|
|
|
|
|
|
|
I2CMasterControl(base, I2C_MASTER_CMD_BURST_SEND_CONT);
|
|
|
|
|
|
|
|
k_sem_take(&data->complete, K_FOREVER);
|
|
|
|
|
|
|
|
if (data->error != I2C_MASTER_ERR_NONE) {
|
|
|
|
goto send_error_stop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
I2CMasterDataPut(base, buf[len - 1]);
|
|
|
|
|
|
|
|
I2CMasterControl(base, I2C_MASTER_CMD_BURST_SEND_FINISH);
|
|
|
|
|
|
|
|
k_sem_take(&data->complete, K_FOREVER);
|
|
|
|
|
|
|
|
if (data->error != I2C_MASTER_ERR_NONE) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
send_error_stop:
|
|
|
|
I2CMasterControl(base, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int i2c_cc13xx_cc26xx_receive(const struct device *dev, uint8_t *buf,
|
|
|
|
uint32_t len,
|
2020-05-27 11:26:57 -05:00
|
|
|
uint16_t addr)
|
2019-04-23 15:33:18 -05:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
const uint32_t base = get_dev_config(dev)->base;
|
2019-04-23 15:33:18 -05:00
|
|
|
struct i2c_cc13xx_cc26xx_data *data = get_dev_data(dev);
|
|
|
|
|
|
|
|
/* Sending address without data is not supported */
|
|
|
|
if (len == 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
I2CMasterSlaveAddrSet(base, addr, true);
|
|
|
|
|
|
|
|
/* The following assumes a single master. Use I2CMasterBusBusy() if
|
|
|
|
* wanting to implement multiple master support.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Single receive */
|
|
|
|
if (len == 1) {
|
|
|
|
I2CMasterControl(base, I2C_MASTER_CMD_SINGLE_RECEIVE);
|
|
|
|
|
|
|
|
k_sem_take(&data->complete, K_FOREVER);
|
|
|
|
|
|
|
|
if (data->error != I2C_MASTER_ERR_NONE) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
*buf = I2CMasterDataGet(base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Burst receive */
|
|
|
|
I2CMasterControl(base, I2C_MASTER_CMD_BURST_RECEIVE_START);
|
|
|
|
|
|
|
|
k_sem_take(&data->complete, K_FOREVER);
|
|
|
|
|
|
|
|
if (data->error != I2C_MASTER_ERR_NONE) {
|
|
|
|
goto recv_error_stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[0] = I2CMasterDataGet(base);
|
|
|
|
|
|
|
|
for (int i = 1; i < len - 1; i++) {
|
|
|
|
I2CMasterControl(base, I2C_MASTER_CMD_BURST_RECEIVE_CONT);
|
|
|
|
|
|
|
|
k_sem_take(&data->complete, K_FOREVER);
|
|
|
|
|
|
|
|
if (data->error != I2C_MASTER_ERR_NONE) {
|
|
|
|
goto recv_error_stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[i] = I2CMasterDataGet(base);
|
|
|
|
}
|
|
|
|
|
|
|
|
I2CMasterControl(base, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
|
|
|
|
|
|
|
|
k_sem_take(&data->complete, K_FOREVER);
|
|
|
|
|
|
|
|
if (data->error != I2C_MASTER_ERR_NONE) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[len - 1] = I2CMasterDataGet(base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
recv_error_stop:
|
|
|
|
I2CMasterControl(base, I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int i2c_cc13xx_cc26xx_transfer(const struct device *dev,
|
|
|
|
struct i2c_msg *msgs,
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t num_msgs, uint16_t addr)
|
2019-04-23 15:33:18 -05:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (num_msgs == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
k_sem_take(&get_dev_data(dev)->lock, K_FOREVER);
|
|
|
|
|
2021-01-07 09:29:17 -08:00
|
|
|
#ifdef CONFIG_PM
|
2021-01-25 18:03:08 -08:00
|
|
|
pm_constraint_set(PM_STATE_STANDBY);
|
2019-11-06 14:56:13 -08:00
|
|
|
#endif
|
|
|
|
|
2019-04-23 15:33:18 -05:00
|
|
|
for (int i = 0; i < num_msgs; i++) {
|
|
|
|
/* Not supported by hardware */
|
|
|
|
if (msgs[i].flags & I2C_MSG_ADDR_10_BITS) {
|
|
|
|
ret = -EIO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((msgs[i].flags & I2C_MSG_RW_MASK) == I2C_MSG_WRITE) {
|
|
|
|
ret = i2c_cc13xx_cc26xx_transmit(dev, msgs[i].buf,
|
|
|
|
msgs[i].len, addr);
|
|
|
|
} else {
|
|
|
|
ret = i2c_cc13xx_cc26xx_receive(dev, msgs[i].buf,
|
|
|
|
msgs[i].len, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 09:29:17 -08:00
|
|
|
#ifdef CONFIG_PM
|
2021-01-25 18:03:08 -08:00
|
|
|
pm_constraint_release(PM_STATE_STANDBY);
|
2019-11-06 14:56:13 -08:00
|
|
|
#endif
|
|
|
|
|
2019-04-23 15:33:18 -05:00
|
|
|
k_sem_give(&get_dev_data(dev)->lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-22 07:06:40 -05:00
|
|
|
#define CPU_FREQ DT_PROP(DT_PATH(cpus, cpu_0), clock_frequency)
|
2020-04-30 20:33:38 +02:00
|
|
|
static int i2c_cc13xx_cc26xx_configure(const struct device *dev,
|
|
|
|
uint32_t dev_config)
|
2019-04-23 15:33:18 -05:00
|
|
|
{
|
|
|
|
bool fast;
|
|
|
|
|
|
|
|
switch (I2C_SPEED_GET(dev_config)) {
|
|
|
|
case I2C_SPEED_STANDARD:
|
|
|
|
fast = false;
|
|
|
|
break;
|
|
|
|
case I2C_SPEED_FAST:
|
|
|
|
fast = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG_ERR("Unsupported speed");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Support for slave mode has not been implemented */
|
|
|
|
if (!(dev_config & I2C_MODE_MASTER)) {
|
|
|
|
LOG_ERR("Slave mode is not supported");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is deprecated and could be ignored in the future */
|
|
|
|
if (dev_config & I2C_ADDR_10_BITS) {
|
|
|
|
LOG_ERR("10-bit addressing mode is not supported");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enables and configures I2C master */
|
2020-04-22 07:06:40 -05:00
|
|
|
I2CMasterInitExpClk(get_dev_config(dev)->base, CPU_FREQ, fast);
|
2019-04-23 15:33:18 -05:00
|
|
|
|
2020-09-01 18:31:40 -04:00
|
|
|
#ifdef CONFIG_PM
|
2019-11-06 14:56:13 -08:00
|
|
|
get_dev_data(dev)->dev_config = dev_config;
|
|
|
|
#endif
|
|
|
|
|
2019-04-23 15:33:18 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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_cc13xx_cc26xx_isr(const void *arg)
|
2019-04-23 15:33:18 -05:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
const uint32_t base = get_dev_config(arg)->base;
|
2019-04-23 15:33:18 -05:00
|
|
|
struct i2c_cc13xx_cc26xx_data *data = get_dev_data(arg);
|
|
|
|
|
|
|
|
if (I2CMasterIntStatus(base, true)) {
|
|
|
|
I2CMasterIntClear(base);
|
|
|
|
|
|
|
|
data->error = I2CMasterErr(base);
|
|
|
|
|
|
|
|
k_sem_give(&data->complete);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 18:31:40 -04:00
|
|
|
#ifdef CONFIG_PM
|
2019-11-06 14:56:13 -08:00
|
|
|
/*
|
|
|
|
* ======== postNotifyFxn ========
|
|
|
|
* Called by Power module when waking up the CPU from Standby. The i2c needs
|
|
|
|
* to be reconfigured afterwards, unless Zephyr's device PM turned it off, in
|
|
|
|
* which case it'd be responsible for turning it back on and reconfigure it.
|
|
|
|
*/
|
|
|
|
static int postNotifyFxn(unsigned int eventType, uintptr_t eventArg,
|
|
|
|
uintptr_t clientArg)
|
|
|
|
{
|
2020-04-30 20:33:38 +02:00
|
|
|
const struct device *dev = (const struct device *)clientArg;
|
2019-11-06 14:56:13 -08:00
|
|
|
int ret = Power_NOTIFYDONE;
|
2020-05-27 11:26:57 -05:00
|
|
|
int16_t res_id;
|
2019-11-06 14:56:13 -08:00
|
|
|
|
|
|
|
/* Reconfigure the hardware if returning from sleep */
|
|
|
|
if (eventType == PowerCC26XX_AWAKE_STANDBY) {
|
|
|
|
res_id = PowerCC26XX_PERIPH_I2C0;
|
|
|
|
|
|
|
|
if (Power_getDependencyCount(res_id) != 0) {
|
|
|
|
/* Reconfigure and enable I2C only if powered */
|
|
|
|
if (i2c_cc13xx_cc26xx_configure(dev,
|
|
|
|
get_dev_data(dev)->dev_config) != 0) {
|
|
|
|
ret = Power_NOTIFYERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
I2CMasterIntEnable(get_dev_config(dev)->base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-09-01 18:31:40 -04:00
|
|
|
#ifdef CONFIG_PM_DEVICE
|
2020-04-30 20:33:38 +02:00
|
|
|
static int i2c_cc13xx_cc26xx_set_power_state(const struct device *dev,
|
2021-06-03 19:06:53 +02:00
|
|
|
enum pm_device_state new_state)
|
2019-11-06 14:56:13 -08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2021-06-04 17:41:39 +02:00
|
|
|
enum pm_device_state state;
|
2019-11-06 14:56:13 -08:00
|
|
|
|
2021-06-04 17:41:39 +02:00
|
|
|
(void)pm_device_state_get(dev, &state);
|
|
|
|
|
|
|
|
if ((new_state == PM_DEVICE_STATE_ACTIVE) && (new_state != state) {
|
2019-11-06 14:56:13 -08:00
|
|
|
Power_setDependency(PowerCC26XX_PERIPH_I2C0);
|
2019-11-13 13:34:29 -08:00
|
|
|
IOCPinTypeI2c(get_dev_config(dev)->base,
|
|
|
|
get_dev_config(dev)->sda_pin,
|
|
|
|
get_dev_config(dev)->scl_pin);
|
2019-11-06 14:56:13 -08:00
|
|
|
ret = i2c_cc13xx_cc26xx_configure(dev,
|
|
|
|
get_dev_data(dev)->dev_config);
|
|
|
|
if (ret == 0) {
|
|
|
|
I2CMasterIntEnable(get_dev_config(dev)->base);
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-07 14:18:57 -07:00
|
|
|
__ASSERT_NO_MSG(new_state == PM_DEVICE_STATE_LOW_POWER ||
|
|
|
|
new_state == PM_DEVICE_STATE_SUSPEND ||
|
|
|
|
new_state == PM_DEVICE_STATE_OFF);
|
2019-11-06 14:56:13 -08:00
|
|
|
|
2021-06-04 17:41:39 +02:00
|
|
|
if (state == PM_DEVICE_STATE_ACTIVE) {
|
2019-11-06 14:56:13 -08:00
|
|
|
I2CMasterIntDisable(get_dev_config(dev)->base);
|
|
|
|
I2CMasterDisable(get_dev_config(dev)->base);
|
2019-11-13 13:34:29 -08:00
|
|
|
/* Reset pin type to default GPIO configuration */
|
|
|
|
IOCPortConfigureSet(get_dev_config(dev)->scl_pin,
|
|
|
|
IOC_PORT_GPIO, IOC_STD_OUTPUT);
|
|
|
|
IOCPortConfigureSet(get_dev_config(dev)->sda_pin,
|
|
|
|
IOC_PORT_GPIO, IOC_STD_OUTPUT);
|
2019-11-06 14:56:13 -08:00
|
|
|
Power_releaseDependency(PowerCC26XX_PERIPH_I2C0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int i2c_cc13xx_cc26xx_pm_control(const struct device *dev,
|
2021-06-04 12:30:35 +02:00
|
|
|
enum pm_device_state *state)
|
2019-11-06 14:56:13 -08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2021-06-07 18:27:39 +02:00
|
|
|
enum pm_device_state curr_state;
|
2019-11-06 14:56:13 -08:00
|
|
|
|
2021-06-07 18:27:39 +02:00
|
|
|
(void)pm_device_state_get(dev, &curr_state);
|
|
|
|
if (*state != curr_state) {
|
|
|
|
ret = i2c_cc13xx_cc26xx_set_power_state(dev,
|
|
|
|
new_state);
|
2019-11-06 14:56:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2020-09-01 18:31:40 -04:00
|
|
|
#endif /* CONFIG_PM_DEVICE */
|
2019-11-06 14:56:13 -08:00
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int i2c_cc13xx_cc26xx_init(const struct device *dev)
|
2019-04-23 15:33:18 -05:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t cfg;
|
2019-04-23 15:33:18 -05:00
|
|
|
int err;
|
|
|
|
|
2020-09-01 18:31:40 -04:00
|
|
|
#ifdef CONFIG_PM
|
2019-11-06 14:56:13 -08:00
|
|
|
/* Set Power dependencies & constraints */
|
|
|
|
Power_setDependency(PowerCC26XX_PERIPH_I2C0);
|
|
|
|
|
|
|
|
/* Register notification function */
|
|
|
|
Power_registerNotify(&get_dev_data(dev)->postNotify,
|
|
|
|
PowerCC26XX_AWAKE_STANDBY,
|
|
|
|
postNotifyFxn, (uintptr_t)dev);
|
|
|
|
#else
|
2019-04-23 15:33:18 -05:00
|
|
|
/* Enable I2C power domain */
|
|
|
|
PRCMPowerDomainOn(PRCM_DOMAIN_SERIAL);
|
|
|
|
|
|
|
|
/* Enable I2C peripheral clock */
|
|
|
|
PRCMPeripheralRunEnable(PRCM_PERIPH_I2C0);
|
2019-06-18 14:45:40 -04:00
|
|
|
/* Enable in sleep mode until proper power management is added */
|
2019-04-23 15:33:18 -05:00
|
|
|
PRCMPeripheralSleepEnable(PRCM_PERIPH_I2C0);
|
|
|
|
PRCMPeripheralDeepSleepEnable(PRCM_PERIPH_I2C0);
|
|
|
|
|
|
|
|
/* Load PRCM settings */
|
|
|
|
PRCMLoadSet();
|
|
|
|
while (!PRCMLoadGet()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* I2C should not be accessed until power domain is on. */
|
|
|
|
while (PRCMPowerDomainStatus(PRCM_DOMAIN_SERIAL) !=
|
|
|
|
PRCM_DOMAIN_POWER_ON) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-06 14:56:13 -08:00
|
|
|
#endif
|
2019-04-23 15:33:18 -05:00
|
|
|
|
2020-03-24 15:34:46 -05:00
|
|
|
IRQ_CONNECT(DT_INST_IRQN(0),
|
|
|
|
DT_INST_IRQ(0, priority),
|
2020-12-09 11:00:35 -06:00
|
|
|
i2c_cc13xx_cc26xx_isr, DEVICE_DT_INST_GET(0), 0);
|
2020-03-24 15:34:46 -05:00
|
|
|
irq_enable(DT_INST_IRQN(0));
|
2019-04-23 15:33:18 -05:00
|
|
|
|
|
|
|
/* Configure IOC module to route SDA and SCL signals */
|
|
|
|
IOCPinTypeI2c(get_dev_config(dev)->base, get_dev_config(dev)->sda_pin,
|
|
|
|
get_dev_config(dev)->scl_pin);
|
|
|
|
|
2020-03-24 15:34:46 -05:00
|
|
|
cfg = i2c_map_dt_bitrate(DT_INST_PROP(0, clock_frequency));
|
2019-04-23 15:33:18 -05:00
|
|
|
err = i2c_cc13xx_cc26xx_configure(dev, cfg | I2C_MODE_MASTER);
|
|
|
|
if (err) {
|
|
|
|
LOG_ERR("Failed to configure");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
I2CMasterIntEnable(get_dev_config(dev)->base);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct i2c_driver_api i2c_cc13xx_cc26xx_driver_api = {
|
|
|
|
.configure = i2c_cc13xx_cc26xx_configure,
|
|
|
|
.transfer = i2c_cc13xx_cc26xx_transfer
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct i2c_cc13xx_cc26xx_config i2c_cc13xx_cc26xx_config = {
|
2020-03-24 15:34:46 -05:00
|
|
|
.base = DT_INST_REG_ADDR(0),
|
|
|
|
.sda_pin = DT_INST_PROP(0, sda_pin),
|
|
|
|
.scl_pin = DT_INST_PROP(0, scl_pin)
|
2019-04-23 15:33:18 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct i2c_cc13xx_cc26xx_data i2c_cc13xx_cc26xx_data = {
|
|
|
|
.lock = Z_SEM_INITIALIZER(i2c_cc13xx_cc26xx_data.lock, 1, 1),
|
|
|
|
.complete = Z_SEM_INITIALIZER(i2c_cc13xx_cc26xx_data.complete, 0, 1),
|
|
|
|
.error = I2C_MASTER_ERR_NONE
|
|
|
|
};
|
|
|
|
|
2020-12-09 11:00:35 -06:00
|
|
|
DEVICE_DT_INST_DEFINE(0,
|
2019-11-06 14:56:13 -08:00
|
|
|
i2c_cc13xx_cc26xx_init,
|
|
|
|
i2c_cc13xx_cc26xx_pm_control,
|
|
|
|
&i2c_cc13xx_cc26xx_data, &i2c_cc13xx_cc26xx_config,
|
|
|
|
POST_KERNEL, CONFIG_I2C_INIT_PRIORITY,
|
|
|
|
&i2c_cc13xx_cc26xx_driver_api);
|