Lines Matching +full:pwm +full:- +full:clock

4  * SPDX-License-Identifier: Apache-2.0
10 #include <zephyr/drivers/pwm.h>
11 #include <zephyr/dt-bindings/clock/npcx_clock.h>
20 /* 16-bit period cycles/prescaler in NPCX PWM modules */
24 /* PWM clock sources */
30 /* PWM heart-beat mode selection */
38 /* pwm controller base address */
40 /* clock configuration */
48 /* PWM cycles per second */
52 /* PWM local functions */
55 const struct pwm_npcx_config *config = dev->config; in pwm_npcx_configure()
56 struct pwm_reg *inst = config->base; in pwm_npcx_configure()
58 /* Disable PWM for module configuration first */ in pwm_npcx_configure()
59 inst->PWMCTL &= ~BIT(NPCX_PWMCTL_PWR); in pwm_npcx_configure()
61 /* Set default PWM polarity to normal */ in pwm_npcx_configure()
62 inst->PWMCTL &= ~BIT(NPCX_PWMCTL_INVP); in pwm_npcx_configure()
64 /* Turn off PWM heart-beat mode */ in pwm_npcx_configure()
65 SET_FIELD(inst->PWMCTL, NPCX_PWMCTL_HB_DC_CTL_FIELD, in pwm_npcx_configure()
68 /* Select APB CLK/LFCLK clock sources to PWM module by default */ in pwm_npcx_configure()
69 SET_FIELD(inst->PWMCTLEX, NPCX_PWMCTLEX_FCK_SEL_FIELD, in pwm_npcx_configure()
72 /* Select clock source to LFCLK by flag, otherwise APB clock source */ in pwm_npcx_configure()
74 inst->PWMCTL |= BIT(NPCX_PWMCTL_CKSEL); in pwm_npcx_configure()
76 inst->PWMCTL &= ~BIT(NPCX_PWMCTL_CKSEL); in pwm_npcx_configure()
80 /* PWM api functions */
85 /* Single channel for each pwm device */ in pwm_npcx_set_cycles()
87 const struct pwm_npcx_config *config = dev->config; in pwm_npcx_set_cycles()
88 struct pwm_npcx_data *const data = dev->data; in pwm_npcx_set_cycles()
89 struct pwm_reg *inst = config->base; in pwm_npcx_set_cycles()
96 ctl = inst->PWMCTL | BIT(NPCX_PWMCTL_PWR); in pwm_npcx_set_cycles()
98 /* Select PWM inverted polarity (ie. active-low pulse). */ in pwm_npcx_set_cycles()
105 /* If pulse_cycles is 0, switch PWM off and return. */ in pwm_npcx_set_cycles()
108 inst->PWMCTL = ctl; in pwm_npcx_set_cycles()
113 * Calculate PWM prescaler that let period_cycles map to in pwm_npcx_set_cycles()
114 * maximum pwm period cycles and won't exceed it. in pwm_npcx_set_cycles()
119 return -EINVAL; in pwm_npcx_set_cycles()
122 /* Set PWM prescaler. */ in pwm_npcx_set_cycles()
123 prsc = prescaler - 1; in pwm_npcx_set_cycles()
125 /* Set PWM period cycles. */ in pwm_npcx_set_cycles()
126 ctr = (period_cycles / prescaler) - 1; in pwm_npcx_set_cycles()
128 /* Set PWM pulse cycles. */ in pwm_npcx_set_cycles()
129 dcr = (pulse_cycles / prescaler) - 1; in pwm_npcx_set_cycles()
132 data->cycles_per_sec / period_cycles, prsc, ctr, dcr); in pwm_npcx_set_cycles()
135 if (inst->PWMCTL != ctl || inst->PRSC != prsc || inst->CTR != ctr) { in pwm_npcx_set_cycles()
136 /* Disable PWM before configuring. */ in pwm_npcx_set_cycles()
137 inst->PWMCTL &= ~BIT(NPCX_PWMCTL_PWR); in pwm_npcx_set_cycles()
139 inst->PRSC = prsc; in pwm_npcx_set_cycles()
140 inst->CTR = ctr; in pwm_npcx_set_cycles()
141 inst->DCR = dcr; in pwm_npcx_set_cycles()
143 /* Enable PWM now. */ in pwm_npcx_set_cycles()
144 inst->PWMCTL = ctl; in pwm_npcx_set_cycles()
149 inst->DCR = dcr; in pwm_npcx_set_cycles()
157 /* Single channel for each pwm device */ in pwm_npcx_get_cycles_per_sec()
159 struct pwm_npcx_data *const data = dev->data; in pwm_npcx_get_cycles_per_sec()
161 *cycles = data->cycles_per_sec; in pwm_npcx_get_cycles_per_sec()
165 /* PWM driver registration */
166 static DEVICE_API(pwm, pwm_npcx_driver_api) = {
173 const struct pwm_npcx_config *const config = dev->config; in pwm_npcx_init()
174 struct pwm_npcx_data *const data = dev->data; in pwm_npcx_init()
175 struct pwm_reg *const inst = config->base; in pwm_npcx_init()
180 * NPCX PWM module mixes byte and word registers together. Make sure in pwm_npcx_init()
184 NPCX_REG_WORD_ACCESS_CHECK(inst->PRSC, 0xA55A); in pwm_npcx_init()
188 LOG_ERR("clock control device not ready"); in pwm_npcx_init()
189 return -ENODEV; in pwm_npcx_init()
192 /* Turn on device clock first and get source clock freq. */ in pwm_npcx_init()
194 &config->clk_cfg); in pwm_npcx_init()
196 LOG_ERR("Turn on PWM clock fail %d", ret); in pwm_npcx_init()
201 &config->clk_cfg, &data->cycles_per_sec); in pwm_npcx_init()
203 LOG_ERR("Get PWM clock rate error %d", ret); in pwm_npcx_init()
207 /* Configure PWM device initially */ in pwm_npcx_init()
208 pwm_npcx_configure(dev, config->clk_cfg.bus); in pwm_npcx_init()
210 /* Configure pin-mux for PWM device */ in pwm_npcx_init()
211 ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); in pwm_npcx_init()
213 LOG_ERR("PWM pinctrl setup failed (%d)", ret); in pwm_npcx_init()