|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Renesas Electronics Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/drivers/pinctrl.h> |
| 8 | +#include <soc.h> |
| 9 | + |
| 10 | +/* Renesas FIT module for iodefine.h data structures */ |
| 11 | +#include "platform.h" |
| 12 | +#include "r_gpio_rx_if.h" |
| 13 | +#include "r_mpc_rx_if.h" |
| 14 | + |
| 15 | +#define PORT_POS (8) |
| 16 | + |
| 17 | +extern const uint8_t g_gpio_open_drain_n_support[]; |
| 18 | +extern const uint8_t g_gpio_pull_up_support[]; |
| 19 | +extern const uint8_t g_gpio_dscr_support[]; |
| 20 | + |
| 21 | +static bool gpio_pin_function_check(uint8_t const *check_array, uint8_t port_number, |
| 22 | + uint8_t pin_number) |
| 23 | +{ |
| 24 | + if ((check_array[port_number] & (1 << pin_number)) != 0) { |
| 25 | + return true; |
| 26 | + } else { |
| 27 | + return false; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +static int pinctrl_configure_pullup(const pinctrl_soc_pin_t *pin, uint32_t value) |
| 32 | +{ |
| 33 | + gpio_port_pin_t port_pin; |
| 34 | + bool pin_check; |
| 35 | + int ret = 0; |
| 36 | + |
| 37 | + port_pin = (pin->port_num << PORT_POS) | pin->pin_num; |
| 38 | + pin_check = gpio_pin_function_check(g_gpio_pull_up_support, pin->port_num, pin->pin_num); |
| 39 | + |
| 40 | + if (pin_check) { |
| 41 | + ret = R_GPIO_PinControl(port_pin, (value ? GPIO_CMD_IN_PULL_UP_ENABLE |
| 42 | + : GPIO_CMD_IN_PULL_UP_DISABLE)); |
| 43 | + } |
| 44 | + |
| 45 | + return ret; |
| 46 | +} |
| 47 | + |
| 48 | +static int pinctrl_configure_dscr(const pinctrl_soc_pin_t *pin, uint32_t value) |
| 49 | +{ |
| 50 | + gpio_port_pin_t port_pin; |
| 51 | + bool pin_check; |
| 52 | + int ret = 0; |
| 53 | + |
| 54 | + port_pin = (pin->port_num << PORT_POS) | pin->pin_num; |
| 55 | + pin_check = gpio_pin_function_check(g_gpio_dscr_support, pin->port_num, pin->pin_num); |
| 56 | + |
| 57 | + if (pin_check) { |
| 58 | + ret = R_GPIO_PinControl(port_pin, |
| 59 | + (value ? GPIO_CMD_DSCR_ENABLE : GPIO_CMD_DSCR_DISABLE)); |
| 60 | + } |
| 61 | + |
| 62 | + return ret; |
| 63 | +} |
| 64 | + |
| 65 | +static int pinctrl_configure_opendrain(const pinctrl_soc_pin_t *pin, uint32_t value) |
| 66 | +{ |
| 67 | + gpio_port_pin_t port_pin; |
| 68 | + bool pin_check; |
| 69 | + int ret = 0; |
| 70 | + |
| 71 | + port_pin = (pin->port_num << PORT_POS) | pin->pin_num; |
| 72 | + pin_check = |
| 73 | + gpio_pin_function_check(g_gpio_open_drain_n_support, pin->port_num, pin->pin_num); |
| 74 | + |
| 75 | + if (pin_check) { |
| 76 | + ret = R_GPIO_PinControl( |
| 77 | + port_pin, (value ? GPIO_CMD_OUT_OPEN_DRAIN_N_CHAN : GPIO_CMD_OUT_CMOS)); |
| 78 | + } |
| 79 | + |
| 80 | + return ret; |
| 81 | +} |
| 82 | + |
| 83 | +int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt, uintptr_t reg) |
| 84 | +{ |
| 85 | + gpio_port_pin_t port_pin; |
| 86 | + mpc_config_t pconfig = { |
| 87 | + .pin_function = 0x0, |
| 88 | + .irq_enable = false, |
| 89 | + .analog_enable = false, |
| 90 | + }; |
| 91 | + int ret; |
| 92 | + |
| 93 | + for (uint8_t i = 0U; i < pin_cnt; i++) { |
| 94 | + const pinctrl_soc_pin_t *pin = &pins[i]; |
| 95 | + |
| 96 | + port_pin = (pin->port_num << PORT_POS) | pin->pin_num; |
| 97 | + |
| 98 | + /* Set PMR register to 0 before setting pin control register */ |
| 99 | + ret = R_GPIO_PinControl(port_pin, GPIO_CMD_ASSIGN_TO_GPIO); |
| 100 | + if (ret != 0) { |
| 101 | + return -EINVAL; |
| 102 | + } |
| 103 | + |
| 104 | + /* Set output high */ |
| 105 | + if (pin->cfg.output_high) { |
| 106 | + R_GPIO_PinWrite(port_pin, GPIO_LEVEL_HIGH); |
| 107 | + } |
| 108 | + |
| 109 | + /* Set port direction */ |
| 110 | + if (pin->cfg.output_enable) { |
| 111 | + R_GPIO_PinDirectionSet(port_pin, GPIO_DIRECTION_OUTPUT); |
| 112 | + } |
| 113 | + |
| 114 | + /* Set pull-up */ |
| 115 | + ret = pinctrl_configure_pullup(pin, pin->cfg.bias_pull_up); |
| 116 | + |
| 117 | + if (ret != 0) { |
| 118 | + return -EINVAL; |
| 119 | + } |
| 120 | + |
| 121 | + /* Set open-drain */ |
| 122 | + ret = pinctrl_configure_opendrain(pin, pin->cfg.drive_open_drain); |
| 123 | + |
| 124 | + if (ret != 0) { |
| 125 | + return -EINVAL; |
| 126 | + } |
| 127 | + |
| 128 | + /* Set drive-strength */ |
| 129 | + ret = pinctrl_configure_dscr(pin, pin->cfg.drive_strength); |
| 130 | + |
| 131 | + if (ret != 0) { |
| 132 | + return -EINVAL; |
| 133 | + } |
| 134 | + |
| 135 | + /* Set pin function */ |
| 136 | + pconfig.analog_enable = pin->cfg.analog_enable; |
| 137 | + pconfig.pin_function = pin->cfg.psels; |
| 138 | + ret = R_MPC_Write(port_pin, &pconfig); |
| 139 | + if (ret != 0) { |
| 140 | + return -EINVAL; |
| 141 | + } |
| 142 | + |
| 143 | + /* Set MODE */ |
| 144 | + if (pin->cfg.pin_mode) { |
| 145 | + ret = R_GPIO_PinControl(port_pin, GPIO_CMD_ASSIGN_TO_PERIPHERAL); |
| 146 | + if (ret != 0) { |
| 147 | + return -EINVAL; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return 0; |
| 153 | +} |
0 commit comments