2018-11-22 06:19:47 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 SiFive Inc.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-25 05:21:29 +08:00
|
|
|
#define DT_DRV_COMPAT sifive_pwm0
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
#include <zephyr/sys/sys_io.h>
|
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/drivers/pinctrl.h>
|
|
|
|
#include <zephyr/drivers/pwm.h>
|
2022-04-01 01:51:54 +08:00
|
|
|
#include <soc.h>
|
2018-11-22 06:19:47 +08:00
|
|
|
|
2022-06-17 22:15:11 +08:00
|
|
|
LOG_MODULE_REGISTER(pwm_sifive, CONFIG_PWM_LOG_LEVEL);
|
|
|
|
|
2018-11-22 06:19:47 +08:00
|
|
|
/* Macros */
|
|
|
|
|
2019-03-13 05:15:42 +08:00
|
|
|
#define PWM_REG(z_config, _offset) ((mem_addr_t) ((z_config)->base + _offset))
|
2018-11-22 06:19:47 +08:00
|
|
|
|
|
|
|
/* Register Offsets */
|
|
|
|
#define REG_PWMCFG 0x00
|
|
|
|
#define REG_PWMCOUNT 0x08
|
|
|
|
#define REG_PWMS 0x10
|
|
|
|
#define REG_PWMCMP0 0x20
|
|
|
|
#define REG_PWMCMP(_channel) (REG_PWMCMP0 + ((_channel) * 0x4))
|
|
|
|
|
|
|
|
/* Number of PWM Channels */
|
|
|
|
#define SF_NUMCHANNELS 4
|
|
|
|
|
|
|
|
/* pwmcfg Bit Offsets */
|
|
|
|
#define SF_PWMSTICKY 8
|
|
|
|
#define SF_PWMZEROCMP 9
|
|
|
|
#define SF_PWMDEGLITCH 10
|
|
|
|
#define SF_PWMENALWAYS 12
|
|
|
|
#define SF_PWMENONESHOT 13
|
|
|
|
#define SF_PWMCMPCENTER(_channel) (16 + (_channel))
|
|
|
|
#define SF_PWMCMPGANG(_channel) (24 + (_channel))
|
|
|
|
#define SF_PWMCMPIP(_channel) (28 + (_channel))
|
|
|
|
|
|
|
|
/* pwmcount scale factor */
|
|
|
|
#define SF_PWMSCALEMASK 0xF
|
|
|
|
#define SF_PWMSCALE(_val) (SF_PWMSCALEMASK & (_val))
|
|
|
|
|
|
|
|
#define SF_PWMCOUNT_MIN_WIDTH 15
|
|
|
|
|
|
|
|
/* Structure Declarations */
|
|
|
|
|
|
|
|
struct pwm_sifive_data {};
|
|
|
|
|
|
|
|
struct pwm_sifive_cfg {
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t base;
|
|
|
|
uint32_t f_sys;
|
|
|
|
uint32_t cmpwidth;
|
2022-03-22 16:38:57 +08:00
|
|
|
const struct pinctrl_dev_config *pcfg;
|
2018-11-22 06:19:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Helper Functions */
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
static inline void sys_set_mask(mem_addr_t addr, uint32_t mask, uint32_t value)
|
2018-11-22 06:19:47 +08:00
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t temp = sys_read32(addr);
|
2018-11-22 06:19:47 +08:00
|
|
|
|
|
|
|
temp &= ~(mask);
|
|
|
|
temp |= value;
|
|
|
|
|
|
|
|
sys_write32(temp, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* API Functions */
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int pwm_sifive_init(const struct device *dev)
|
2018-11-22 06:19:47 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct pwm_sifive_cfg *config = dev->config;
|
2022-03-22 16:38:57 +08:00
|
|
|
#ifdef CONFIG_PINCTRL
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
2018-11-22 06:19:47 +08:00
|
|
|
|
|
|
|
/* When pwms == pwmcmp0, reset the counter */
|
|
|
|
sys_set_bit(PWM_REG(config, REG_PWMCFG), SF_PWMZEROCMP);
|
|
|
|
|
|
|
|
/* Enable continuous operation */
|
|
|
|
sys_set_bit(PWM_REG(config, REG_PWMCFG), SF_PWMENALWAYS);
|
|
|
|
|
|
|
|
/* Clear IP config bits */
|
|
|
|
sys_clear_bit(PWM_REG(config, REG_PWMCFG), SF_PWMSTICKY);
|
|
|
|
sys_clear_bit(PWM_REG(config, REG_PWMCFG), SF_PWMDEGLITCH);
|
|
|
|
|
|
|
|
/* Clear all channels */
|
|
|
|
for (int i = 0; i < SF_NUMCHANNELS; i++) {
|
|
|
|
/* Clear the channel comparator */
|
|
|
|
sys_write32(0, PWM_REG(config, REG_PWMCMP(i)));
|
|
|
|
|
|
|
|
/* Clear the compare center and compare gang bits */
|
|
|
|
sys_clear_bit(PWM_REG(config, REG_PWMCFG), SF_PWMCMPCENTER(i));
|
|
|
|
sys_clear_bit(PWM_REG(config, REG_PWMCFG), SF_PWMCMPGANG(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:06:43 +08:00
|
|
|
static int pwm_sifive_set_cycles(const struct device *dev, uint32_t channel,
|
|
|
|
uint32_t period_cycles, uint32_t pulse_cycles,
|
|
|
|
pwm_flags_t flags)
|
2018-11-22 06:19:47 +08:00
|
|
|
{
|
2022-04-06 18:49:38 +08:00
|
|
|
const struct pwm_sifive_cfg *config = dev->config;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t count_max = 0U;
|
|
|
|
uint32_t max_cmp_val = 0U;
|
|
|
|
uint32_t pwmscale = 0U;
|
2018-11-22 06:19:47 +08:00
|
|
|
|
2019-11-13 21:46:37 +08:00
|
|
|
if (flags) {
|
|
|
|
/* PWM polarity not supported (yet?) */
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2022-04-04 22:35:22 +08:00
|
|
|
if (channel >= SF_NUMCHANNELS) {
|
|
|
|
LOG_ERR("The requested PWM channel %d is invalid\n", channel);
|
2018-11-22 06:19:47 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Channel 0 sets the period, we can't output PWM with it */
|
2022-04-04 22:35:22 +08:00
|
|
|
if (channel == 0U) {
|
2018-11-22 06:19:47 +08:00
|
|
|
LOG_ERR("PWM channel 0 cannot be configured\n");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can't support periods greater than we can store in pwmcount */
|
|
|
|
count_max = (1 << (config->cmpwidth + SF_PWMCOUNT_MIN_WIDTH)) - 1;
|
|
|
|
|
|
|
|
if (period_cycles > count_max) {
|
|
|
|
LOG_ERR("Requested period is %d but maximum is %d\n",
|
|
|
|
period_cycles, count_max);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the maximum value that pwmcmpX can be set to */
|
|
|
|
max_cmp_val = ((1 << config->cmpwidth) - 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the minimum value of pwmscale that will allow us to set the
|
|
|
|
* requested period
|
|
|
|
*/
|
|
|
|
while ((period_cycles >> pwmscale) > max_cmp_val) {
|
|
|
|
pwmscale++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that we can scale that much */
|
|
|
|
if (pwmscale > SF_PWMSCALEMASK) {
|
|
|
|
LOG_ERR("Requested period is %d but maximum is %d\n",
|
|
|
|
period_cycles, max_cmp_val << pwmscale);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the pwmscale field */
|
|
|
|
sys_set_mask(PWM_REG(config, REG_PWMCFG),
|
|
|
|
SF_PWMSCALEMASK,
|
|
|
|
SF_PWMSCALE(pwmscale));
|
|
|
|
|
|
|
|
/* Set the period by setting pwmcmp0 */
|
|
|
|
sys_write32((period_cycles >> pwmscale), PWM_REG(config, REG_PWMCMP0));
|
|
|
|
|
|
|
|
/* Set the duty cycle by setting pwmcmpX */
|
|
|
|
sys_write32((pulse_cycles >> pwmscale),
|
2022-04-04 22:35:22 +08:00
|
|
|
PWM_REG(config, REG_PWMCMP(channel)));
|
2018-11-22 06:19:47 +08:00
|
|
|
|
|
|
|
LOG_DBG("channel: %d, pwmscale: %d, pwmcmp0: %d, pwmcmp%d: %d",
|
2022-04-04 22:35:22 +08:00
|
|
|
channel,
|
2018-11-22 06:19:47 +08:00
|
|
|
pwmscale,
|
|
|
|
(period_cycles >> pwmscale),
|
2022-04-04 22:35:22 +08:00
|
|
|
channel,
|
2018-11-22 06:19:47 +08:00
|
|
|
(pulse_cycles >> pwmscale));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int pwm_sifive_get_cycles_per_sec(const struct device *dev,
|
2022-04-04 22:35:22 +08:00
|
|
|
uint32_t channel, uint64_t *cycles)
|
2018-11-22 06:19:47 +08:00
|
|
|
{
|
|
|
|
const struct pwm_sifive_cfg *config;
|
|
|
|
|
|
|
|
if (dev == NULL) {
|
|
|
|
LOG_ERR("The device instance pointer was NULL\n");
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
2020-05-29 02:44:16 +08:00
|
|
|
config = dev->config;
|
2018-11-22 06:19:47 +08:00
|
|
|
if (config == NULL) {
|
|
|
|
LOG_ERR("The device configuration is NULL\n");
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fail if we don't have that channel */
|
2022-04-04 22:35:22 +08:00
|
|
|
if (channel >= SF_NUMCHANNELS) {
|
2018-11-22 06:19:47 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*cycles = config->f_sys;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Device Instantiation */
|
|
|
|
|
|
|
|
static const struct pwm_driver_api pwm_sifive_api = {
|
2022-04-01 17:06:43 +08:00
|
|
|
.set_cycles = pwm_sifive_set_cycles,
|
2018-11-22 06:19:47 +08:00
|
|
|
.get_cycles_per_sec = pwm_sifive_get_cycles_per_sec,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PWM_SIFIVE_INIT(n) \
|
2022-03-22 16:38:57 +08:00
|
|
|
PINCTRL_DT_INST_DEFINE(n); \
|
2018-11-22 06:19:47 +08:00
|
|
|
static struct pwm_sifive_data pwm_sifive_data_##n; \
|
|
|
|
static const struct pwm_sifive_cfg pwm_sifive_cfg_##n = { \
|
2020-03-25 05:21:29 +08:00
|
|
|
.base = DT_INST_REG_ADDR(n), \
|
2022-04-01 01:51:54 +08:00
|
|
|
.f_sys = SIFIVE_PERIPHERAL_CLOCK_FREQUENCY, \
|
2020-03-25 05:21:29 +08:00
|
|
|
.cmpwidth = DT_INST_PROP(n, sifive_compare_width), \
|
2022-03-22 16:38:57 +08:00
|
|
|
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
|
2018-11-22 06:19:47 +08:00
|
|
|
}; \
|
2020-12-10 05:38:54 +08:00
|
|
|
DEVICE_DT_INST_DEFINE(n, \
|
2018-11-22 06:19:47 +08:00
|
|
|
pwm_sifive_init, \
|
2021-04-28 17:36:44 +08:00
|
|
|
NULL, \
|
2018-11-22 06:19:47 +08:00
|
|
|
&pwm_sifive_data_##n, \
|
|
|
|
&pwm_sifive_cfg_##n, \
|
|
|
|
POST_KERNEL, \
|
|
|
|
CONFIG_PWM_SIFIVE_INIT_PRIORITY, \
|
2020-05-08 03:09:05 +08:00
|
|
|
&pwm_sifive_api);
|
2018-11-22 06:19:47 +08:00
|
|
|
|
2020-05-07 02:23:07 +08:00
|
|
|
DT_INST_FOREACH_STATUS_OKAY(PWM_SIFIVE_INIT)
|