|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Silicon Laboratories Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <errno.h> |
| 8 | +#include <zephyr/irq.h> |
| 9 | +#include <zephyr/sys/util.h> |
| 10 | +#include <zephyr/sys/util_macro.h> |
| 11 | +#include <zephyr/device.h> |
| 12 | +#include <zephyr/devicetree.h> |
| 13 | +#include <zephyr/drivers/pwm.h> |
| 14 | +#include <zephyr/drivers/pinctrl.h> |
| 15 | +#include <zephyr/drivers/clock_control.h> |
| 16 | +#include <zephyr/types.h> |
| 17 | +#include "sl_si91x_pwm.h" |
| 18 | + |
| 19 | +#define DT_DRV_COMPAT silabs_siwx91x_pwm |
| 20 | + |
| 21 | +#define PWM_CHANNELS 4 |
| 22 | +#define DEFAULT_VALUE 0xFF |
| 23 | + |
| 24 | +struct pwm_siwx91x_config { |
| 25 | + /* Pointer to the clock device structure */ |
| 26 | + const struct device *clock_dev; |
| 27 | + /* Clock control subsystem */ |
| 28 | + clock_control_subsys_t clock_subsys; |
| 29 | + /* Pointer to the pin control device configuration */ |
| 30 | + const struct pinctrl_dev_config *pcfg; |
| 31 | + /* Prescaler information of the channels */ |
| 32 | + uint8_t ch_prescaler[PWM_CHANNELS]; |
| 33 | +}; |
| 34 | + |
| 35 | +struct pwm_siwx91x_data { |
| 36 | + sl_pwm_config_t pwm_channel_config[PWM_CHANNELS]; |
| 37 | + uint8_t pwm_polarity; |
| 38 | +}; |
| 39 | + |
| 40 | +/* Function to convert prescaler value to a programmable reg value */ |
| 41 | +static int siwx91x_prescale_convert(uint8_t prescale) |
| 42 | +{ |
| 43 | + |
| 44 | + switch (prescale) { |
| 45 | + case 1: |
| 46 | + return SL_TIME_PERIOD_PRESCALE_1; |
| 47 | + case 2: |
| 48 | + return SL_TIME_PERIOD_PRESCALE_2; |
| 49 | + case 4: |
| 50 | + return SL_TIME_PERIOD_PRESCALE_4; |
| 51 | + case 8: |
| 52 | + return SL_TIME_PERIOD_PRESCALE_8; |
| 53 | + case 16: |
| 54 | + return SL_TIME_PERIOD_PRESCALE_16; |
| 55 | + case 32: |
| 56 | + return SL_TIME_PERIOD_PRESCALE_32; |
| 57 | + case 64: |
| 58 | + return SL_TIME_PERIOD_PRESCALE_64; |
| 59 | + default: |
| 60 | + return -EINVAL; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/* Program PWM channel with the default configurations */ |
| 65 | +static int siwx91x_default_channel_config(const struct device *dev, uint32_t channel) |
| 66 | +{ |
| 67 | + const struct pwm_siwx91x_config *config = dev->config; |
| 68 | + struct pwm_siwx91x_data *data = dev->data; |
| 69 | + int prescale_reg_value = siwx91x_prescale_convert(config->ch_prescaler[channel]); |
| 70 | + |
| 71 | + if (prescale_reg_value < 0) { |
| 72 | + return -EINVAL; |
| 73 | + } |
| 74 | + |
| 75 | + if (sl_si91x_pwm_set_output_mode(data->pwm_channel_config[channel].is_mode, channel)) { |
| 76 | + return -EINVAL; |
| 77 | + } |
| 78 | + |
| 79 | + if (sl_si91x_pwm_set_base_timer_mode(data->pwm_channel_config[channel].base_timer_mode, |
| 80 | + channel)) { |
| 81 | + return -EINVAL; |
| 82 | + } |
| 83 | + |
| 84 | + if (sl_si91x_pwm_control_base_timer( |
| 85 | + data->pwm_channel_config[channel].channel_timer_selection)) { |
| 86 | + return -EINVAL; |
| 87 | + } |
| 88 | + |
| 89 | + if (sl_si91x_pwm_control_period(SL_TIME_PERIOD_POSTSCALE_1_1, prescale_reg_value, |
| 90 | + channel)) { |
| 91 | + return -EINVAL; |
| 92 | + } |
| 93 | + |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +static int pwm_siwx91x_set_cycles(const struct device *dev, uint32_t channel, |
| 98 | + uint32_t period_cycles, uint32_t pulse_cycles, pwm_flags_t flags) |
| 99 | +{ |
| 100 | + struct pwm_siwx91x_data *data = dev->data; |
| 101 | + uint32_t prev_period; |
| 102 | + uint32_t duty_cycle; |
| 103 | + |
| 104 | + if (channel >= PWM_CHANNELS) { |
| 105 | + return -EINVAL; |
| 106 | + } |
| 107 | + |
| 108 | + if (flags != data->pwm_polarity) { |
| 109 | + if (flags == PWM_POLARITY_NORMAL) { |
| 110 | + /* Active high pulse */ |
| 111 | + data->pwm_channel_config[channel].is_polarity_high = true; |
| 112 | + data->pwm_channel_config[channel].is_polarity_low = false; |
| 113 | + } else { |
| 114 | + /* Active low pulse */ |
| 115 | + data->pwm_channel_config[channel].is_polarity_high = false; |
| 116 | + data->pwm_channel_config[channel].is_polarity_low = true; |
| 117 | + } |
| 118 | + if (sl_si91x_pwm_set_output_polarity( |
| 119 | + data->pwm_channel_config[channel].is_polarity_low, |
| 120 | + data->pwm_channel_config[channel].is_polarity_high)) { |
| 121 | + return -EINVAL; |
| 122 | + } |
| 123 | + data->pwm_polarity = flags; |
| 124 | + } |
| 125 | + |
| 126 | + if (data->pwm_channel_config[channel].channel == DEFAULT_VALUE) { |
| 127 | + /* Configure the channel with default parameters */ |
| 128 | + if (siwx91x_default_channel_config(dev, channel)) { |
| 129 | + return -EINVAL; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (sl_si91x_pwm_get_time_period(channel, (uint16_t *)&prev_period)) { |
| 134 | + return -EINVAL; |
| 135 | + } |
| 136 | + |
| 137 | + if (period_cycles != prev_period) { |
| 138 | + if (sl_si91x_pwm_set_time_period(channel, period_cycles, 0)) { |
| 139 | + /* Programmed value must be out of range (>65535) */ |
| 140 | + return -ENOTSUP; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /* Calculate the duty cycle */ |
| 145 | + duty_cycle = pulse_cycles * 100; |
| 146 | + duty_cycle /= period_cycles; |
| 147 | + |
| 148 | + if (duty_cycle != data->pwm_channel_config[channel].duty_cycle) { |
| 149 | + if (sl_si91x_pwm_set_duty_cycle(pulse_cycles, channel)) { |
| 150 | + /* Programmed value must be out of range (>65535) */ |
| 151 | + return -ENOTSUP; |
| 152 | + } |
| 153 | + data->pwm_channel_config[channel].duty_cycle = duty_cycle; |
| 154 | + } |
| 155 | + |
| 156 | + if (data->pwm_channel_config[channel].channel == DEFAULT_VALUE) { |
| 157 | + /* Start PWM after configuring the channel for first time */ |
| 158 | + if (sl_si91x_pwm_start(channel)) { |
| 159 | + return -EINVAL; |
| 160 | + } |
| 161 | + data->pwm_channel_config[channel].channel = channel; |
| 162 | + } |
| 163 | + |
| 164 | + return 0; |
| 165 | +} |
| 166 | + |
| 167 | +static int pwm_siwx91x_get_cycles_per_sec(const struct device *dev, uint32_t channel, |
| 168 | + uint64_t *cycles) |
| 169 | +{ |
| 170 | + struct pwm_siwx91x_data *data = dev->data; |
| 171 | + |
| 172 | + if (channel >= PWM_CHANNELS) { |
| 173 | + return -EINVAL; |
| 174 | + } |
| 175 | + |
| 176 | + *cycles = (uint64_t)data->pwm_channel_config[channel].frequency; |
| 177 | + |
| 178 | + return 0; |
| 179 | +} |
| 180 | + |
| 181 | +static int pwm_siwx91x_init(const struct device *dev) |
| 182 | +{ |
| 183 | + const struct pwm_siwx91x_config *config = dev->config; |
| 184 | + struct pwm_siwx91x_data *data = dev->data; |
| 185 | + int ret; |
| 186 | + |
| 187 | + ret = clock_control_on(config->clock_dev, config->clock_subsys); |
| 188 | + if (ret) { |
| 189 | + return ret; |
| 190 | + } |
| 191 | + |
| 192 | + ret = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT); |
| 193 | + if (ret) { |
| 194 | + return ret; |
| 195 | + } |
| 196 | + |
| 197 | + /* Initialize the channel config structures with default values */ |
| 198 | + for (int i = 0; i < PWM_CHANNELS; i++) { |
| 199 | + data->pwm_channel_config[i].channel = DEFAULT_VALUE; |
| 200 | + data->pwm_channel_config[i].is_mode = SL_MODE_INDEPENDENT; |
| 201 | + data->pwm_channel_config[i].base_timer_mode = SL_FREE_RUN_MODE; |
| 202 | + data->pwm_channel_config[i].channel_timer_selection = SL_BASE_TIMER_EACH_CHANNEL; |
| 203 | + data->pwm_channel_config[i].frequency = |
| 204 | + CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC / config->ch_prescaler[i]; |
| 205 | + } |
| 206 | + |
| 207 | + data->pwm_polarity = DEFAULT_VALUE; |
| 208 | + |
| 209 | + return 0; |
| 210 | +} |
| 211 | + |
| 212 | +static const struct pwm_driver_api pwm_siwx91x_driver_api = { |
| 213 | + .set_cycles = pwm_siwx91x_set_cycles, |
| 214 | + .get_cycles_per_sec = pwm_siwx91x_get_cycles_per_sec, |
| 215 | +}; |
| 216 | + |
| 217 | +#define SIWX91X_PWM_INIT(inst) \ |
| 218 | + PINCTRL_DT_INST_DEFINE(inst); \ |
| 219 | + static struct pwm_siwx91x_data pwm_siwx91x_data_##inst; \ |
| 220 | + static const struct pwm_siwx91x_config pwm_config_##inst = { \ |
| 221 | + .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(inst)), \ |
| 222 | + .clock_subsys = (clock_control_subsys_t)DT_INST_PHA(inst, clocks, clkid), \ |
| 223 | + .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \ |
| 224 | + .ch_prescaler = DT_INST_PROP(inst, silabs_ch_prescaler), \ |
| 225 | + }; \ |
| 226 | + DEVICE_DT_INST_DEFINE(inst, &pwm_siwx91x_init, NULL, &pwm_siwx91x_data_##inst, \ |
| 227 | + &pwm_config_##inst, PRE_KERNEL_1, CONFIG_PWM_INIT_PRIORITY, \ |
| 228 | + &pwm_siwx91x_driver_api); |
| 229 | + |
| 230 | +DT_INST_FOREACH_STATUS_OKAY(SIWX91X_PWM_INIT) |
0 commit comments