2021-07-27 17:39:02 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Telink Semiconductor
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT telink_b91_pwm
|
|
|
|
|
|
|
|
#include "pwm.h"
|
|
|
|
#include "clock.h"
|
|
|
|
#include <drivers/pwm.h>
|
2022-02-11 20:17:22 +08:00
|
|
|
#include <drivers/pinctrl.h>
|
2021-07-27 17:39:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
#define PWM_PCLK_SPEED DT_INST_PROP(0, clock_frequency)
|
|
|
|
#define NUM_OF_CHANNELS DT_INST_PROP(0, channels)
|
|
|
|
|
2022-02-11 20:17:22 +08:00
|
|
|
PINCTRL_DT_INST_DEFINE(0);
|
|
|
|
|
|
|
|
static const struct pinctrl_dev_config *pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(0);
|
2021-07-27 17:39:02 +08:00
|
|
|
|
|
|
|
/* API implementation: init */
|
|
|
|
static int pwm_b91_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
2022-02-11 20:17:22 +08:00
|
|
|
uint32_t status = 0;
|
2021-07-27 17:39:02 +08:00
|
|
|
uint8_t clk_32k_en = 0;
|
|
|
|
uint32_t pwm_clk_div = 0;
|
|
|
|
|
|
|
|
/* Calculate and check PWM clock divider */
|
|
|
|
pwm_clk_div = sys_clk.pclk * 1000 * 1000 / PWM_PCLK_SPEED - 1;
|
|
|
|
if (pwm_clk_div > 255) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set PWM Peripheral clock */
|
|
|
|
pwm_set_clk((unsigned char) (pwm_clk_div & 0xFF));
|
|
|
|
|
|
|
|
/* Set PWM 32k Channel clock if enabled */
|
|
|
|
clk_32k_en |= DT_INST_PROP(0, clk32k_ch0_enable) ? PWM_CLOCK_32K_CHN_PWM0 : 0;
|
|
|
|
clk_32k_en |= DT_INST_PROP(0, clk32k_ch1_enable) ? PWM_CLOCK_32K_CHN_PWM1 : 0;
|
|
|
|
clk_32k_en |= DT_INST_PROP(0, clk32k_ch2_enable) ? PWM_CLOCK_32K_CHN_PWM2 : 0;
|
|
|
|
clk_32k_en |= DT_INST_PROP(0, clk32k_ch3_enable) ? PWM_CLOCK_32K_CHN_PWM3 : 0;
|
|
|
|
clk_32k_en |= DT_INST_PROP(0, clk32k_ch4_enable) ? PWM_CLOCK_32K_CHN_PWM4 : 0;
|
|
|
|
clk_32k_en |= DT_INST_PROP(0, clk32k_ch5_enable) ? PWM_CLOCK_32K_CHN_PWM5 : 0;
|
|
|
|
pwm_32k_chn_en(clk_32k_en);
|
|
|
|
|
|
|
|
/* Config PWM pins */
|
2022-02-11 20:17:22 +08:00
|
|
|
status = pinctrl_apply_state(pcfg, PINCTRL_STATE_DEFAULT);
|
|
|
|
if (status < 0) {
|
|
|
|
return status;
|
2021-07-27 17:39:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:06:43 +08:00
|
|
|
/* API implementation: set_cycles */
|
|
|
|
static int pwm_b91_set_cycles(const struct device *dev, uint32_t channel,
|
|
|
|
uint32_t period_cycles, uint32_t pulse_cycles,
|
|
|
|
pwm_flags_t flags)
|
2021-07-27 17:39:02 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
/* check pwm channel */
|
2022-04-04 22:35:22 +08:00
|
|
|
if (channel >= NUM_OF_CHANNELS) {
|
2021-07-27 17:39:02 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check size of pulse and period (2 bytes) */
|
|
|
|
if ((period_cycles > 0xFFFFu) ||
|
|
|
|
(pulse_cycles > 0xFFFFu)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set polarity */
|
|
|
|
if (flags & PWM_POLARITY_INVERTED) {
|
2022-04-04 22:35:22 +08:00
|
|
|
pwm_invert_en(channel);
|
2021-07-27 17:39:02 +08:00
|
|
|
} else {
|
2022-04-04 22:35:22 +08:00
|
|
|
pwm_invert_dis(channel);
|
2021-07-27 17:39:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set pulse and period */
|
2022-04-04 22:35:22 +08:00
|
|
|
pwm_set_tcmp(channel, pulse_cycles);
|
|
|
|
pwm_set_tmax(channel, period_cycles);
|
2021-07-27 17:39:02 +08:00
|
|
|
|
|
|
|
/* start pwm */
|
2022-04-04 22:35:22 +08:00
|
|
|
pwm_start(channel);
|
2021-07-27 17:39:02 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* API implementation: get_cycles_per_sec */
|
2022-04-04 22:35:22 +08:00
|
|
|
static int pwm_b91_get_cycles_per_sec(const struct device *dev,
|
|
|
|
uint32_t channel, uint64_t *cycles)
|
2021-07-27 17:39:02 +08:00
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
/* check pwm channel */
|
2022-04-04 22:35:22 +08:00
|
|
|
if (channel >= NUM_OF_CHANNELS) {
|
2021-07-27 17:39:02 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2022-04-04 22:35:22 +08:00
|
|
|
((channel == 0u) && DT_INST_PROP(0, clk32k_ch0_enable)) ||
|
|
|
|
((channel == 1u) && DT_INST_PROP(0, clk32k_ch1_enable)) ||
|
|
|
|
((channel == 2u) && DT_INST_PROP(0, clk32k_ch2_enable)) ||
|
|
|
|
((channel == 3u) && DT_INST_PROP(0, clk32k_ch3_enable)) ||
|
|
|
|
((channel == 4u) && DT_INST_PROP(0, clk32k_ch4_enable)) ||
|
|
|
|
((channel == 5u) && DT_INST_PROP(0, clk32k_ch5_enable))
|
2021-07-27 17:39:02 +08:00
|
|
|
) {
|
|
|
|
*cycles = 32000u;
|
|
|
|
} else {
|
|
|
|
*cycles = sys_clk.pclk * 1000 * 1000 / (reg_pwm_clkdiv + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PWM driver APIs structure */
|
|
|
|
static const struct pwm_driver_api pwm_b91_driver_api = {
|
2022-04-01 17:06:43 +08:00
|
|
|
.set_cycles = pwm_b91_set_cycles,
|
2021-07-27 17:39:02 +08:00
|
|
|
.get_cycles_per_sec = pwm_b91_get_cycles_per_sec,
|
|
|
|
};
|
|
|
|
|
|
|
|
BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) <= 1,
|
|
|
|
"unsupported PWM instance");
|
|
|
|
|
|
|
|
/* PWM driver registration */
|
|
|
|
#define PWM_B91_INIT(n) \
|
|
|
|
\
|
|
|
|
DEVICE_DT_INST_DEFINE(n, pwm_b91_init, \
|
|
|
|
NULL, NULL, NULL, \
|
|
|
|
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
|
|
|
|
&pwm_b91_driver_api);
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(PWM_B91_INIT)
|