2021-05-21 23:59:25 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, NXP
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT nxp_sctimer_pwm
|
|
|
|
|
|
|
|
#include <errno.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/pwm.h>
|
2021-05-21 23:59:25 +08:00
|
|
|
#include <fsl_sctimer.h>
|
|
|
|
#include <fsl_clock.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/pinctrl.h>
|
2023-09-07 23:36:57 +08:00
|
|
|
#include <zephyr/drivers/clock_control.h>
|
2021-05-21 23:59:25 +08:00
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2022-06-17 22:15:11 +08:00
|
|
|
|
2022-06-17 20:12:12 +08:00
|
|
|
LOG_MODULE_REGISTER(pwm_mcux_sctimer, CONFIG_PWM_LOG_LEVEL);
|
2021-05-21 23:59:25 +08:00
|
|
|
|
|
|
|
#define CHANNEL_COUNT FSL_FEATURE_SCT_NUMBER_OF_OUTPUTS
|
|
|
|
|
2023-12-08 11:45:23 +08:00
|
|
|
/* Constant identifying that no event number has been set */
|
|
|
|
#define EVENT_NOT_SET FSL_FEATURE_SCT_NUMBER_OF_EVENTS
|
|
|
|
|
2021-05-21 23:59:25 +08:00
|
|
|
struct pwm_mcux_sctimer_config {
|
|
|
|
SCT_Type *base;
|
|
|
|
uint32_t prescale;
|
2022-03-25 04:37:57 +08:00
|
|
|
const struct pinctrl_dev_config *pincfg;
|
2023-09-07 23:36:57 +08:00
|
|
|
const struct device *clock_dev;
|
|
|
|
clock_control_subsys_t clock_subsys;
|
2021-05-21 23:59:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct pwm_mcux_sctimer_data {
|
|
|
|
uint32_t event_number[CHANNEL_COUNT];
|
|
|
|
sctimer_pwm_signal_param_t channel[CHANNEL_COUNT];
|
2023-12-08 11:45:23 +08:00
|
|
|
uint32_t match_period;
|
|
|
|
uint32_t configured_chan;
|
2021-05-21 23:59:25 +08:00
|
|
|
};
|
|
|
|
|
2023-12-08 11:45:23 +08:00
|
|
|
/* Helper to setup channel that has not previously been configured for PWM */
|
|
|
|
static int mcux_sctimer_new_channel(const struct device *dev,
|
|
|
|
uint32_t channel, uint32_t period_cycles,
|
|
|
|
uint32_t duty_cycle)
|
|
|
|
{
|
|
|
|
const struct pwm_mcux_sctimer_config *config = dev->config;
|
|
|
|
struct pwm_mcux_sctimer_data *data = dev->data;
|
|
|
|
uint32_t clock_freq;
|
|
|
|
uint32_t pwm_freq;
|
|
|
|
|
|
|
|
data->match_period = period_cycles;
|
|
|
|
|
|
|
|
if (clock_control_get_rate(config->clock_dev, config->clock_subsys,
|
|
|
|
&clock_freq)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pwm_freq = (clock_freq / config->prescale) / period_cycles;
|
|
|
|
|
|
|
|
if (pwm_freq == 0) {
|
|
|
|
LOG_ERR("Could not set up pwm_freq=%d", pwm_freq);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCTIMER_StopTimer(config->base, kSCTIMER_Counter_U);
|
|
|
|
|
|
|
|
LOG_DBG("SETUP dutycycle to %u\n", duty_cycle);
|
|
|
|
data->channel[channel].dutyCyclePercent = duty_cycle;
|
|
|
|
if (SCTIMER_SetupPwm(config->base, &data->channel[channel],
|
|
|
|
kSCTIMER_EdgeAlignedPwm, pwm_freq,
|
|
|
|
clock_freq, &data->event_number[channel]) == kStatus_Fail) {
|
|
|
|
LOG_ERR("Could not set up pwm");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCTIMER_StartTimer(config->base, kSCTIMER_Counter_U);
|
|
|
|
data->configured_chan++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:06:43 +08:00
|
|
|
static int mcux_sctimer_pwm_set_cycles(const struct device *dev,
|
|
|
|
uint32_t channel, uint32_t period_cycles,
|
|
|
|
uint32_t pulse_cycles, pwm_flags_t flags)
|
2021-05-21 23:59:25 +08:00
|
|
|
{
|
|
|
|
const struct pwm_mcux_sctimer_config *config = dev->config;
|
|
|
|
struct pwm_mcux_sctimer_data *data = dev->data;
|
|
|
|
uint8_t duty_cycle;
|
2023-12-08 11:45:23 +08:00
|
|
|
int ret;
|
2021-05-21 23:59:25 +08:00
|
|
|
|
2022-04-04 22:35:22 +08:00
|
|
|
if (channel >= CHANNEL_COUNT) {
|
2021-05-21 23:59:25 +08:00
|
|
|
LOG_ERR("Invalid channel");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-04-06 18:42:22 +08:00
|
|
|
if (period_cycles == 0) {
|
2022-04-06 18:46:45 +08:00
|
|
|
LOG_ERR("Channel can not be set to inactive level");
|
|
|
|
return -ENOTSUP;
|
2021-05-21 23:59:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((flags & PWM_POLARITY_INVERTED) == 0) {
|
2022-04-04 22:35:22 +08:00
|
|
|
data->channel[channel].level = kSCTIMER_HighTrue;
|
2021-05-21 23:59:25 +08:00
|
|
|
} else {
|
2022-04-04 22:35:22 +08:00
|
|
|
data->channel[channel].level = kSCTIMER_LowTrue;
|
2021-05-21 23:59:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
duty_cycle = 100 * pulse_cycles / period_cycles;
|
|
|
|
|
2023-12-08 11:45:23 +08:00
|
|
|
if (duty_cycle == 0 && data->configured_chan == 1) {
|
|
|
|
/* Only one channel is active. We can turn off the SCTimer
|
|
|
|
* global counter.
|
|
|
|
*/
|
2021-05-21 23:59:25 +08:00
|
|
|
SCT_Type *base = config->base;
|
|
|
|
|
2023-12-08 11:45:23 +08:00
|
|
|
/* Stop timer so we can set output directly */
|
|
|
|
SCTIMER_StopTimer(base, kSCTIMER_Counter_U);
|
2021-05-21 23:59:25 +08:00
|
|
|
|
|
|
|
/* Set the output to inactive State */
|
2022-04-04 22:35:22 +08:00
|
|
|
if (data->channel[channel].level == kSCTIMER_HighTrue) {
|
|
|
|
base->OUTPUT &= ~(1UL << channel);
|
2021-05-21 23:59:25 +08:00
|
|
|
} else {
|
2022-04-04 22:35:22 +08:00
|
|
|
base->OUTPUT |= (1UL << channel);
|
2021-05-21 23:59:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-08 11:45:23 +08:00
|
|
|
/* SCTimer has some unique restrictions when operation as a PWM output.
|
|
|
|
* The peripheral is based around a single counter, with a block of
|
|
|
|
* match registers that can trigger corresponding events. When used
|
|
|
|
* as a PWM peripheral, MCUX SDK sets up the SCTimer as follows:
|
|
|
|
* - one match register is used to set PWM output high, and reset
|
|
|
|
* SCtimer counter. This sets the PWM period
|
|
|
|
* - one match register is used to set PWM output low. This sets the
|
|
|
|
* pulse length
|
|
|
|
*
|
|
|
|
* This means that when configured, multiple channels must have the
|
|
|
|
* same PWM period, since they all share the same SCTimer counter.
|
|
|
|
*/
|
|
|
|
if (period_cycles != data->match_period &&
|
|
|
|
data->event_number[channel] == EVENT_NOT_SET &&
|
|
|
|
data->match_period == 0U) {
|
|
|
|
/* No PWM signals have been configured. We can set up the first
|
|
|
|
* PWM output using the MCUX SDK.
|
|
|
|
*/
|
|
|
|
ret = mcux_sctimer_new_channel(dev, channel, period_cycles,
|
|
|
|
duty_cycle);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2023-09-07 23:36:57 +08:00
|
|
|
}
|
2023-12-08 11:45:23 +08:00
|
|
|
} else if (data->event_number[channel] == EVENT_NOT_SET) {
|
|
|
|
/* We have already configured a PWM signal, but this channel
|
|
|
|
* has not been setup. We can only support this channel
|
|
|
|
* if the period matches that of other PWM signals.
|
|
|
|
*/
|
|
|
|
if (period_cycles != data->match_period) {
|
|
|
|
LOG_ERR("Only one PWM period is supported between "
|
|
|
|
"multiple channels");
|
|
|
|
return -ENOTSUP;
|
2021-05-21 23:59:25 +08:00
|
|
|
}
|
2023-12-08 11:45:23 +08:00
|
|
|
/* Setup PWM output using MCUX SDK */
|
|
|
|
ret = mcux_sctimer_new_channel(dev, channel, period_cycles,
|
|
|
|
duty_cycle);
|
|
|
|
} else if (period_cycles != data->match_period) {
|
|
|
|
uint32_t period_event = data->event_number[channel];
|
|
|
|
/* We are reconfiguring the period of a configured channel
|
|
|
|
* MCUX SDK does not provide support for this feature, and
|
|
|
|
* we cannot do this safely if multiple channels are setup.
|
|
|
|
*/
|
|
|
|
if (data->configured_chan != 1) {
|
|
|
|
LOG_ERR("Cannot change PWM period when multiple "
|
|
|
|
"channels active");
|
2021-05-21 23:59:25 +08:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2023-12-08 11:45:23 +08:00
|
|
|
/* To make this change, we can simply set the MATCHREL
|
|
|
|
* registers for the period match, and the next match
|
|
|
|
* (which the SDK will setup as the pulse match event)
|
|
|
|
*/
|
|
|
|
SCTIMER_StopTimer(config->base, kSCTIMER_Counter_U);
|
|
|
|
config->base->MATCHREL[period_event] = period_cycles - 1U;
|
|
|
|
config->base->MATCHREL[period_event + 1] = pulse_cycles - 1U;
|
2021-05-21 23:59:25 +08:00
|
|
|
SCTIMER_StartTimer(config->base, kSCTIMER_Counter_U);
|
2023-12-08 11:45:23 +08:00
|
|
|
data->match_period = period_cycles;
|
2021-05-21 23:59:25 +08:00
|
|
|
} else {
|
2023-12-08 11:45:23 +08:00
|
|
|
/* Only duty cycle needs to be updated */
|
2022-04-04 22:35:22 +08:00
|
|
|
SCTIMER_UpdatePwmDutycycle(config->base, channel, duty_cycle,
|
|
|
|
data->event_number[channel]);
|
2021-05-21 23:59:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-04 22:35:22 +08:00
|
|
|
static int mcux_sctimer_pwm_get_cycles_per_sec(const struct device *dev,
|
|
|
|
uint32_t channel,
|
|
|
|
uint64_t *cycles)
|
2021-05-21 23:59:25 +08:00
|
|
|
{
|
|
|
|
const struct pwm_mcux_sctimer_config *config = dev->config;
|
2023-09-07 23:36:57 +08:00
|
|
|
uint32_t clock_freq;
|
2021-05-21 23:59:25 +08:00
|
|
|
|
2023-09-07 23:36:57 +08:00
|
|
|
if (clock_control_get_rate(config->clock_dev, config->clock_subsys,
|
|
|
|
&clock_freq)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*cycles = clock_freq / config->prescale;
|
2021-05-21 23:59:25 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mcux_sctimer_pwm_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
const struct pwm_mcux_sctimer_config *config = dev->config;
|
|
|
|
struct pwm_mcux_sctimer_data *data = dev->data;
|
|
|
|
sctimer_config_t pwm_config;
|
|
|
|
status_t status;
|
|
|
|
int i;
|
2022-03-25 04:37:57 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
|
|
|
|
if (err) {
|
|
|
|
return err;
|
|
|
|
}
|
2021-05-21 23:59:25 +08:00
|
|
|
|
|
|
|
SCTIMER_GetDefaultConfig(&pwm_config);
|
2023-09-07 23:36:57 +08:00
|
|
|
|
2021-05-21 23:59:25 +08:00
|
|
|
pwm_config.prescale_l = config->prescale - 1;
|
|
|
|
|
|
|
|
status = SCTIMER_Init(config->base, &pwm_config);
|
|
|
|
if (status != kStatus_Success) {
|
|
|
|
LOG_ERR("Unable to init PWM");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < CHANNEL_COUNT; i++) {
|
|
|
|
data->channel[i].output = i;
|
|
|
|
data->channel[i].level = kSCTIMER_HighTrue;
|
|
|
|
data->channel[i].dutyCyclePercent = 0;
|
2023-12-08 11:45:23 +08:00
|
|
|
data->event_number[i] = EVENT_NOT_SET;
|
2021-05-21 23:59:25 +08:00
|
|
|
}
|
2023-12-08 11:45:23 +08:00
|
|
|
data->match_period = 0;
|
|
|
|
data->configured_chan = 0;
|
2021-05-21 23:59:25 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct pwm_driver_api pwm_mcux_sctimer_driver_api = {
|
2022-04-01 17:06:43 +08:00
|
|
|
.set_cycles = mcux_sctimer_pwm_set_cycles,
|
2021-05-21 23:59:25 +08:00
|
|
|
.get_cycles_per_sec = mcux_sctimer_pwm_get_cycles_per_sec,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PWM_MCUX_SCTIMER_DEVICE_INIT_MCUX(n) \
|
2023-04-19 22:41:53 +08:00
|
|
|
PINCTRL_DT_INST_DEFINE(n); \
|
2021-05-21 23:59:25 +08:00
|
|
|
static struct pwm_mcux_sctimer_data pwm_mcux_sctimer_data_##n; \
|
|
|
|
\
|
|
|
|
static const struct pwm_mcux_sctimer_config pwm_mcux_sctimer_config_##n = { \
|
|
|
|
.base = (SCT_Type *)DT_INST_REG_ADDR(n), \
|
|
|
|
.prescale = DT_INST_PROP(n, prescaler), \
|
2023-04-19 22:41:53 +08:00
|
|
|
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
|
2023-09-07 23:36:57 +08:00
|
|
|
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
|
|
|
|
.clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name),\
|
2021-05-21 23:59:25 +08:00
|
|
|
}; \
|
|
|
|
\
|
|
|
|
DEVICE_DT_INST_DEFINE(n, \
|
|
|
|
mcux_sctimer_pwm_init, \
|
|
|
|
NULL, \
|
|
|
|
&pwm_mcux_sctimer_data_##n, \
|
|
|
|
&pwm_mcux_sctimer_config_##n, \
|
2022-09-21 17:22:06 +08:00
|
|
|
POST_KERNEL, CONFIG_PWM_INIT_PRIORITY, \
|
2021-05-21 23:59:25 +08:00
|
|
|
&pwm_mcux_sctimer_driver_api);
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(PWM_MCUX_SCTIMER_DEVICE_INIT_MCUX)
|