forked from zephyrproject-rtos/zephyr
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwm_imx.c
179 lines (144 loc) · 4.44 KB
/
pwm_imx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) 2018, Diego Sueiro <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/kernel.h>
#include <soc.h>
#include <device_imx.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(pwm_imx, CONFIG_PWM_LOG_LEVEL);
#define PWM_PWMSR_FIFOAV_4WORDS 0x4
#define PWM_PWMCR_SWR(x) (((uint32_t)(((uint32_t)(x)) \
<<PWM_PWMCR_SWR_SHIFT))&PWM_PWMCR_SWR_MASK)
struct imx_pwm_config {
PWM_Type *base;
uint16_t prescaler;
const struct pinctrl_dev_config *pincfg;
};
struct imx_pwm_data {
uint32_t period_cycles;
};
static bool imx_pwm_is_enabled(PWM_Type *base)
{
return PWM_PWMCR_REG(base) & PWM_PWMCR_EN_MASK;
}
static int imx_pwm_get_cycles_per_sec(const struct device *dev, uint32_t pwm,
uint64_t *cycles)
{
const struct imx_pwm_config *config = dev->config;
*cycles = get_pwm_clock_freq(config->base) >> config->prescaler;
return 0;
}
static int imx_pwm_set_cycles(const struct device *dev, uint32_t channel,
uint32_t period_cycles, uint32_t pulse_cycles,
pwm_flags_t flags)
{
const struct imx_pwm_config *config = dev->config;
struct imx_pwm_data *data = dev->data;
unsigned int period_ms;
bool enabled = imx_pwm_is_enabled(config->base);
int wait_count = 0, fifoav;
uint32_t cr, sr;
if (period_cycles == 0U) {
LOG_ERR("Channel can not be set to inactive level");
return -ENOTSUP;
}
if (flags) {
/* PWM polarity not supported (yet?) */
return -ENOTSUP;
}
LOG_DBG("enabled=%d, pulse_cycles=%d, period_cycles=%d,"
" duty_cycle=%d\n", enabled, pulse_cycles, period_cycles,
(pulse_cycles * 100U / period_cycles));
/*
* i.MX PWMv2 has a 4-word sample FIFO.
* In order to avoid FIFO overflow issue, we do software reset
* to clear all sample FIFO if the controller is disabled or
* wait for a full PWM cycle to get a relinquished FIFO slot
* when the controller is enabled and the FIFO is fully loaded.
*/
if (enabled) {
sr = PWM_PWMSR_REG(config->base);
fifoav = PWM_PWMSR_FIFOAV(sr);
if (fifoav == PWM_PWMSR_FIFOAV_4WORDS) {
period_ms = (get_pwm_clock_freq(config->base) >>
config->prescaler) * MSEC_PER_SEC;
k_sleep(K_MSEC(period_ms));
sr = PWM_PWMSR_REG(config->base);
if (fifoav == PWM_PWMSR_FIFOAV(sr)) {
LOG_WRN("there is no free FIFO slot\n");
}
}
} else {
PWM_PWMCR_REG(config->base) = PWM_PWMCR_SWR(1);
do {
k_sleep(K_MSEC(1));
cr = PWM_PWMCR_REG(config->base);
} while ((PWM_PWMCR_SWR(cr)) &&
(++wait_count < CONFIG_PWM_PWMSWR_LOOP));
if (PWM_PWMCR_SWR(cr)) {
LOG_WRN("software reset timeout\n");
}
}
/*
* according to imx pwm RM, the real period value should be
* PERIOD value in PWMPR plus 2.
*/
if (period_cycles > 2) {
period_cycles -= 2U;
} else {
return -EINVAL;
}
PWM_PWMSAR_REG(config->base) = pulse_cycles;
if (data->period_cycles != period_cycles) {
LOG_WRN("Changing period cycles from %d to %d in %s",
data->period_cycles, period_cycles,
dev->name);
data->period_cycles = period_cycles;
PWM_PWMPR_REG(config->base) = period_cycles;
}
cr = PWM_PWMCR_EN_MASK | PWM_PWMCR_PRESCALER(config->prescaler) |
PWM_PWMCR_DOZEN_MASK | PWM_PWMCR_WAITEN_MASK |
PWM_PWMCR_DBGEN_MASK | PWM_PWMCR_CLKSRC(2);
PWM_PWMCR_REG(config->base) = cr;
return 0;
}
static int imx_pwm_init(const struct device *dev)
{
const struct imx_pwm_config *config = dev->config;
struct imx_pwm_data *data = dev->data;
int err;
err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT);
if (err) {
return err;
}
PWM_PWMPR_REG(config->base) = data->period_cycles;
return 0;
}
static const struct pwm_driver_api imx_pwm_driver_api = {
.set_cycles = imx_pwm_set_cycles,
.get_cycles_per_sec = imx_pwm_get_cycles_per_sec,
};
#define PWM_IMX_INIT(n) \
PINCTRL_DT_INST_DEFINE(n); \
static const struct imx_pwm_config imx_pwm_config_##n = { \
.base = (PWM_Type *)DT_INST_REG_ADDR(n), \
.prescaler = DT_INST_PROP(n, prescaler), \
.pincfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
}; \
\
static struct imx_pwm_data imx_pwm_data_##n; \
\
DEVICE_DT_INST_DEFINE(n, &imx_pwm_init, NULL, \
&imx_pwm_data_##n, \
&imx_pwm_config_##n, POST_KERNEL, \
CONFIG_PWM_INIT_PRIORITY, \
&imx_pwm_driver_api);
#if DT_HAS_COMPAT_STATUS_OKAY(fsl_imx27_pwm)
#define DT_DRV_COMPAT fsl_imx27_pwm
DT_INST_FOREACH_STATUS_OKAY(PWM_IMX_INIT)
#endif