|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Renesas Electronics Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT renesas_rx_lvd |
| 8 | + |
| 9 | +#include <zephyr/kernel.h> |
| 10 | +#include <zephyr/devicetree.h> |
| 11 | +#include <zephyr/irq.h> |
| 12 | +#include <soc.h> |
| 13 | +#include <zephyr/logging/log.h> |
| 14 | +#include <zephyr/drivers/misc/renesas_rx_lvd/renesas_rx_lvd.h> |
| 15 | +#include <zephyr/drivers/pinctrl.h> |
| 16 | + |
| 17 | +LOG_MODULE_REGISTER(renesas_rx_lvd, CONFIG_SOC_LOG_LEVEL); |
| 18 | + |
| 19 | +#define LVD0_NODE DT_NODELABEL(lvd0) |
| 20 | +#define LVD1_NODE DT_NODELABEL(lvd1) |
| 21 | +#define LVD_NO_FUNC ((void (*)(void *))NULL) |
| 22 | + |
| 23 | +/* |
| 24 | + * The extern functions below are implemented in the r_lvd_rx_hw.c source file. |
| 25 | + * For more information, please refer to r_lvd_rx_hw.c in HAL Renesas |
| 26 | + */ |
| 27 | +extern void lvd_ch1_isr(void); |
| 28 | +extern void lvd_ch2_isr(void); |
| 29 | + |
| 30 | +struct rx_lvd_data { |
| 31 | + void (*callback)(void *args); |
| 32 | + void (*user_callback)(void *user_data); |
| 33 | + void *user_data; |
| 34 | +}; |
| 35 | + |
| 36 | +struct rx_lvd_config { |
| 37 | + lvd_channel_t channel; |
| 38 | + lvd_config_t lvd_config; |
| 39 | + uint8_t vdet_target; |
| 40 | + uint8_t lvd_action; |
| 41 | + bool lvd_support_cpma2; |
| 42 | +}; |
| 43 | + |
| 44 | +int renesas_rx_pin_set_cmpa2(const struct device *dev) |
| 45 | +{ |
| 46 | + const struct rx_lvd_config *config = dev->config; |
| 47 | + const struct pinctrl_dev_config *pcfg; |
| 48 | + |
| 49 | + if (config->channel == 0) { |
| 50 | + if (DT_NODE_HAS_PROP(LVD0_NODE, pinctrl_0)) { |
| 51 | + PINCTRL_DT_DEFINE(LVD0_NODE); |
| 52 | + pcfg = PINCTRL_DT_DEV_CONFIG_GET(LVD0_NODE); |
| 53 | + } else { |
| 54 | + LOG_ERR("No pinctrl-0 property found in the device tree"); |
| 55 | + return -EINVAL; |
| 56 | + } |
| 57 | + } else { |
| 58 | + if (DT_NODE_HAS_PROP(LVD1_NODE, pinctrl_0)) { |
| 59 | + PINCTRL_DT_DEFINE(LVD1_NODE); |
| 60 | + pcfg = PINCTRL_DT_DEV_CONFIG_GET(LVD1_NODE); |
| 61 | + } else { |
| 62 | + LOG_ERR("No pinctrl_0 property found in the device tree"); |
| 63 | + return -EINVAL; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /* In the case of monitoring the CMPA2 pin, set the CMPA2 pin. */ |
| 68 | + /* This only applicable to channel 1 with the LVDb driver */ |
| 69 | + int ret = pinctrl_apply_state(pcfg, PINCTRL_STATE_DEFAULT); |
| 70 | + |
| 71 | + if (ret < 0) { |
| 72 | + LOG_ERR("Failed to apply pinctrl state: %d\n", ret); |
| 73 | + return -EINVAL; |
| 74 | + } |
| 75 | + |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +int renesas_rx_lvd_get_status(const struct device *dev, lvd_status_position_t *status_position, |
| 80 | + lvd_status_cross_t *status_cross) |
| 81 | +{ |
| 82 | + const struct rx_lvd_config *config = dev->config; |
| 83 | + int ret; |
| 84 | + |
| 85 | + ret = R_LVD_GetStatus(config->channel, status_position, status_cross); |
| 86 | + if (ret != 0) { |
| 87 | + LOG_ERR("Failed to get LVD status"); |
| 88 | + return -EINVAL; |
| 89 | + } |
| 90 | + |
| 91 | + return ret; |
| 92 | +} |
| 93 | + |
| 94 | +int renesas_rx_lvd_clear_status(const struct device *dev) |
| 95 | +{ |
| 96 | + const struct rx_lvd_config *config = dev->config; |
| 97 | + int ret; |
| 98 | + |
| 99 | + ret = R_LVD_ClearStatus(config->channel); |
| 100 | + if (ret != 0) { |
| 101 | + LOG_ERR("Failed to clear LVD status"); |
| 102 | + return -EINVAL; |
| 103 | + } |
| 104 | + |
| 105 | + return ret; |
| 106 | +} |
| 107 | + |
| 108 | +int renesas_rx_lvd_register_callback(const struct device *dev, void (*callback)(void *), |
| 109 | + void *user_data) |
| 110 | +{ |
| 111 | + struct rx_lvd_data *data = dev->data; |
| 112 | + |
| 113 | + data->user_callback = callback; |
| 114 | + data->user_data = user_data; |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
| 118 | + |
| 119 | +#define LVD_IRQ_CONNECT() \ |
| 120 | + do { \ |
| 121 | + IF_ENABLED(DT_NODE_HAS_STATUS_OKAY(LVD0_NODE), ( \ |
| 122 | + IRQ_CONNECT(DT_IRQN(LVD0_NODE), \ |
| 123 | + DT_IRQ(LVD0_NODE, priority), \ |
| 124 | + lvd_ch1_isr, \ |
| 125 | + DEVICE_DT_GET(LVD0_NODE), \ |
| 126 | + 0); \ |
| 127 | + irq_enable(DT_IRQN(LVD0_NODE)); \ |
| 128 | + )) \ |
| 129 | + IF_ENABLED(DT_NODE_HAS_STATUS_OKAY(LVD1_NODE), ( \ |
| 130 | + IRQ_CONNECT(DT_IRQN(LVD1_NODE), \ |
| 131 | + DT_IRQ(LVD1_NODE, priority), \ |
| 132 | + lvd_ch2_isr, \ |
| 133 | + DEVICE_DT_GET(LVD1_NODE), \ |
| 134 | + 0); \ |
| 135 | + irq_enable(DT_IRQN(LVD1_NODE)); \ |
| 136 | + )) \ |
| 137 | + } while (0) |
| 138 | + |
| 139 | +static int renesas_rx_lvd_init(const struct device *dev) |
| 140 | +{ |
| 141 | + lvd_err_t ret; |
| 142 | + |
| 143 | + LVD_IRQ_CONNECT(); |
| 144 | + |
| 145 | + const struct rx_lvd_config *config = dev->config; |
| 146 | + const struct rx_lvd_data *data = dev->data; |
| 147 | + |
| 148 | + /* In reset or no-action when LVD is detected, callback will not be triggered. */ |
| 149 | + ret = R_LVD_Open(config->channel, &config->lvd_config, data->callback); |
| 150 | + if (ret != 0) { |
| 151 | + LOG_ERR("Failed to initialize LVD channel %d", config->channel); |
| 152 | + return -EIO; |
| 153 | + } |
| 154 | + |
| 155 | + /* Set the CMPA2 pin if the target is CMPA2 */ |
| 156 | + /* NOTE: For the RX130 series, CMPA2 is only used on channel 2. */ |
| 157 | + if ((config->lvd_support_cpma2) && (config->vdet_target == 1)) { |
| 158 | + return renesas_rx_pin_set_cmpa2(dev); |
| 159 | + } |
| 160 | + |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
| 164 | +#define RENESAS_RX_LVD_INIT(index) \ |
| 165 | + \ |
| 166 | + static const struct rx_lvd_config rx_lvd_config_##index = { \ |
| 167 | + .channel = DT_INST_PROP(index, channel), \ |
| 168 | + .lvd_config = \ |
| 169 | + { \ |
| 170 | + .trigger = DT_INST_PROP(index, lvd_trigger), \ |
| 171 | + }, \ |
| 172 | + .lvd_action = DT_INST_PROP(index, lvd_action), \ |
| 173 | + .vdet_target = DT_INST_PROP(index, vdet_target), \ |
| 174 | + .lvd_support_cpma2 = DT_INST_PROP(index, renesas_lvd_external_target), \ |
| 175 | + }; \ |
| 176 | + \ |
| 177 | + void rx_lvd_callback_##index(void *args) \ |
| 178 | + { \ |
| 179 | + ARG_UNUSED(args); \ |
| 180 | + const struct device *dev = DEVICE_DT_GET(DT_INST(index, renesas_rx_lvd)); \ |
| 181 | + struct rx_lvd_data *data = dev->data; \ |
| 182 | + \ |
| 183 | + /* Call the user's callback function*/ \ |
| 184 | + if (data->user_callback) { \ |
| 185 | + data->user_callback(data->user_data); \ |
| 186 | + } \ |
| 187 | + }; \ |
| 188 | + \ |
| 189 | + static struct rx_lvd_data rx_lvd_data_##index = { \ |
| 190 | + .callback = rx_lvd_callback_##index, \ |
| 191 | + }; \ |
| 192 | + \ |
| 193 | + static int renesas_rx_lvd_init_##index(const struct device *dev) \ |
| 194 | + { \ |
| 195 | + int err = renesas_rx_lvd_init(dev); \ |
| 196 | + if (err != 0) { \ |
| 197 | + return err; \ |
| 198 | + } \ |
| 199 | + return 0; \ |
| 200 | + } \ |
| 201 | + \ |
| 202 | + DEVICE_DT_INST_DEFINE(index, renesas_rx_lvd_init_##index, NULL, &rx_lvd_data_##index, \ |
| 203 | + &rx_lvd_config_##index, PRE_KERNEL_1, \ |
| 204 | + CONFIG_RENESAS_RX_LVD_INIT_PRIORITY, NULL); |
| 205 | + |
| 206 | +DT_INST_FOREACH_STATUS_OKAY(RENESAS_RX_LVD_INIT) |
0 commit comments