|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Microchip Technology Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * @file pinctrl_mchp_port_g1.c |
| 9 | + * @brief Pin control driver for Microchip devices. |
| 10 | + * |
| 11 | + * This file provides the implementation of pin control functions |
| 12 | + * for Microchip-based systems. |
| 13 | + */ |
| 14 | + |
| 15 | +#include <zephyr/drivers/pinctrl.h> |
| 16 | +#include <soc.h> |
| 17 | + |
| 18 | +/** |
| 19 | + * @brief Utility macro that expands to the PORT address if it exists. |
| 20 | + * |
| 21 | + * This macro checks if a node label exists in the device tree and, if it does, |
| 22 | + * it expands to the register address of that node label. If the node label does |
| 23 | + * not exist, it expands to nothing. |
| 24 | + * |
| 25 | + * @param nodelabel The node label to check in the device tree. |
| 26 | + */ |
| 27 | +/* clang-format off */ |
| 28 | +#define MCHP_PORT_ADDR_OR_NONE(nodelabel) \ |
| 29 | + IF_ENABLED(DT_NODE_EXISTS(DT_NODELABEL(nodelabel)), \ |
| 30 | + (DT_REG_ADDR(DT_NODELABEL(nodelabel)))) |
| 31 | +/* clang-format on */ |
| 32 | + |
| 33 | +/** |
| 34 | + * @brief Array of port addresses for the MCHP SAMD5x_E5x series. |
| 35 | + * |
| 36 | + * This array contains the register addresses of the ports (PORTA, PORTB, PORTC, and PORTD) |
| 37 | + * for the MCHP SAMD5x_E5x series microcontrollers. The addresses are obtained using the |
| 38 | + * MCHP_PORT_ADDR_OR_NONE macro, which ensures that only existing ports are included. |
| 39 | + * This can be updated for other devices using conditional comiplation directives |
| 40 | + */ |
| 41 | +static const uint32_t mchp_port_addrs[] = { |
| 42 | + MCHP_PORT_ADDR_OR_NONE(porta), |
| 43 | + MCHP_PORT_ADDR_OR_NONE(portb), |
| 44 | + MCHP_PORT_ADDR_OR_NONE(portc), |
| 45 | + MCHP_PORT_ADDR_OR_NONE(portd), |
| 46 | +}; |
| 47 | + |
| 48 | +/** |
| 49 | + * @brief Set pinmux registers using odd/even logic |
| 50 | + * |
| 51 | + * This function configures the pinmux registers for a given pin based on |
| 52 | + * the provided pin control information. It determines whether the pin number |
| 53 | + * is odd or even and sets the appropriate bits in the PORT_PMUX register. |
| 54 | + * |
| 55 | + * @param pin Pointer to the pin control information |
| 56 | + * @param reg Pointer to the base address of the port group registers |
| 57 | + */ |
| 58 | +static void pinctrl_pinmux(const pinctrl_soc_pin_t *pin) |
| 59 | +{ |
| 60 | + port_group_registers_t *pRegister; |
| 61 | + |
| 62 | + uint8_t pin_num = MCHP_PINMUX_PIN_GET(pin->pinmux); |
| 63 | + uint8_t port_id = MCHP_PINMUX_PORT_GET(pin->pinmux); |
| 64 | + uint8_t pin_mux = MCHP_PINMUX_PERIPH_GET(pin->pinmux); |
| 65 | + |
| 66 | + bool is_odd = pin_num & 1; |
| 67 | + uint32_t idx = pin_num / 2U; |
| 68 | + |
| 69 | + pRegister = (port_group_registers_t *)mchp_port_addrs[port_id]; |
| 70 | + |
| 71 | + if (pRegister != NULL) { |
| 72 | + /* Each pinmux register holds the config for two pins. The |
| 73 | + * even numbered pin goes in the bits 0..3 and the odd |
| 74 | + * numbered pin in bits 4..7. |
| 75 | + */ |
| 76 | + if (is_odd == true) { |
| 77 | + pRegister->PORT_PMUX[idx] |= PORT_PMUX_PMUXO(pin_mux); |
| 78 | + } else { |
| 79 | + pRegister->PORT_PMUX[idx] |= PORT_PMUX_PMUXE(pin_mux); |
| 80 | + } |
| 81 | + pRegister->PORT_PINCFG[pin_num] |= (uint8_t)PORT_PINCFG_PMUXEN_Msk; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * @brief Set all pin configuration registers by checking the flags |
| 87 | + * |
| 88 | + * This function configures various pin settings such as pull-up, pull-down, |
| 89 | + * input enable, output enable, and drive strength based on the provided |
| 90 | + * pin control information. |
| 91 | + * |
| 92 | + * @param pin Pointer to the pin control information |
| 93 | + * @param reg Pointer to the base address of the port group registers |
| 94 | + */ |
| 95 | +static void pinctrl_set_flags(const pinctrl_soc_pin_t *pin) |
| 96 | +{ |
| 97 | + port_group_registers_t *pRegister; |
| 98 | + |
| 99 | + uint8_t pin_num = MCHP_PINMUX_PIN_GET(pin->pinmux); |
| 100 | + uint8_t port_id = MCHP_PINMUX_PORT_GET(pin->pinmux); |
| 101 | + |
| 102 | + pRegister = (port_group_registers_t *)mchp_port_addrs[port_id]; |
| 103 | + |
| 104 | + if (pRegister != NULL) { |
| 105 | + |
| 106 | + /* Check if pull-up or pull-down resistors need to be configured */ |
| 107 | + if (((pin->pinflag & MCHP_PINCTRL_PULLUP) != 0) || |
| 108 | + ((pin->pinflag & MCHP_PINCTRL_PULLDOWN) != 0)) { |
| 109 | + if ((pin->pinflag & MCHP_PINCTRL_PULLUP) != 0) { |
| 110 | + /* If pull-up resistor enabled, |
| 111 | + * set the corresponding bit in PORT_OUT reg |
| 112 | + */ |
| 113 | + pRegister->PORT_OUT |= (1 << pin_num); |
| 114 | + } |
| 115 | + pRegister->PORT_PINCFG[pin_num] |= PORT_PINCFG_PULLEN(1); |
| 116 | + } else { |
| 117 | + pRegister->PORT_PINCFG[pin_num] &= ~PORT_PINCFG_PULLEN(1); |
| 118 | + } |
| 119 | + |
| 120 | + /* if input is enabled, set the corresponding bit in PORT_PINCFG register */ |
| 121 | + if ((pin->pinflag & MCHP_PINCTRL_INPUTENABLE) != 0) { |
| 122 | + pRegister->PORT_PINCFG[pin_num] |= PORT_PINCFG_INEN(1); |
| 123 | + } else { |
| 124 | + pRegister->PORT_PINCFG[pin_num] &= ~PORT_PINCFG_INEN(1); |
| 125 | + } |
| 126 | + |
| 127 | + /* if output is enabled, set the corresponding bit in PORT_DIR register */ |
| 128 | + if ((pin->pinflag & MCHP_PINCTRL_OUTPUTENABLE) != 0) { |
| 129 | + pRegister->PORT_DIR |= (1 << pin_num); |
| 130 | + } else { |
| 131 | + pRegister->PORT_DIR &= ~(1 << pin_num); |
| 132 | + } |
| 133 | + |
| 134 | + /* if drive strength is enabled, set the corresponding bit in PORT_PINCFG reg */ |
| 135 | + if ((pin->pinflag & MCHP_PINCTRL_DRIVESTRENGTH) != 0) { |
| 136 | + pRegister->PORT_PINCFG[pin_num] |= PORT_PINCFG_DRVSTR(1); |
| 137 | + } else { |
| 138 | + pRegister->PORT_PINCFG[pin_num] &= ~PORT_PINCFG_DRVSTR(1); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * @brief Configure a specific pin based on the provided pin configuration. |
| 145 | + * |
| 146 | + * This helper function configures a pin by determining its port function and then |
| 147 | + * calling the appropriate functions to set the pinmux and other pin configurations. |
| 148 | + * |
| 149 | + * @param pin The pin configuration to be applied. This is of type pinctrl_soc_pin_t. |
| 150 | + */ |
| 151 | +static void pinctrl_configure_pin(pinctrl_soc_pin_t pin) |
| 152 | +{ |
| 153 | + uint8_t port_func = MCHP_PINMUX_FUNC_GET(pin.pinmux); |
| 154 | + |
| 155 | + /* call pinmux function if alternate function is configured */ |
| 156 | + if (port_func == MCHP_PINMUX_FUNC_periph) { |
| 157 | + pinctrl_pinmux(&pin); |
| 158 | + } |
| 159 | + |
| 160 | + /* set all other pin configurations */ |
| 161 | + pinctrl_set_flags(&pin); |
| 162 | +} |
| 163 | + |
| 164 | +/** |
| 165 | + * @brief Configure multiple pins. |
| 166 | + * |
| 167 | + * This function configures a set of pins based on the provided pin |
| 168 | + * configuration array. |
| 169 | + * |
| 170 | + * @param pins Pointer to an array of pinctrl_soc_pin_t structures that |
| 171 | + * define the pin configurations. |
| 172 | + * @param pin_cnt Number of pins to configure. |
| 173 | + * @param reg Unused parameter. |
| 174 | + * |
| 175 | + * @return 0 on success. |
| 176 | + */ |
| 177 | +int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt, uintptr_t reg) |
| 178 | +{ |
| 179 | + ARG_UNUSED(reg); |
| 180 | + |
| 181 | + for (uint8_t i = 0U; i < pin_cnt; i++) { |
| 182 | + pinctrl_configure_pin(*pins); |
| 183 | + pins++; |
| 184 | + } |
| 185 | + |
| 186 | + return 0; |
| 187 | +} |
0 commit comments