// SPDX-License-Identifier: GPL-2.0-or-later /* * Support for Digigram AlpX PCI-e boards * * Copyright (c) 2024 Digigram Digital (info@digigram.com) */ #include "alpx_gpio.h" #include static inline bool alpx_gpio_is_an_input (struct alpx_device *alpx_dev, unsigned offset) { return offset < alpx_dev->variant->gpios.inputs_qty; } int alpx_gpio_get(struct gpio_chip *chip, unsigned offset) { struct alpx_device *alpx_dev = gpiochip_get_data(chip); u32 value; if (alpx_gpio_is_an_input(alpx_dev, offset)) { value = readl(alpx_dev->base + alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.inputs_reg_offset); dev_dbg( alpx_dev->dev,"[0x%08x] =>[0x%08x]\n", alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.inputs_reg_offset, value); } else { value = readl(alpx_dev->base + alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset); dev_dbg( alpx_dev->dev,"[0x%08x] =>[0x%08x]\n", alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset, value); //translate offset -> bit rank offset -= alpx_dev->variant->gpios.inputs_qty; } return ALPX_GPIO_VALUE(offset, value); } void alpx_gpio_set(struct gpio_chip *chip, unsigned offset, int gpio_value) { struct alpx_device *alpx_dev = gpiochip_get_data(chip); unsigned int value; if (alpx_gpio_is_an_input(alpx_dev, offset)) return; //translate Offset to bit rank offset -= alpx_dev->variant->gpios.inputs_qty; value = readl(alpx_dev->base + alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset ); dev_dbg( alpx_dev->dev,"read [0x%08x] =>0x%08x\n",alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset, value); value &= ~ALPX_GPIO_MASK(offset); value |= ALPX_GPIO_SEL(offset, gpio_value); dev_dbg( alpx_dev->dev,"write 0x%08x =>[0x%08x]\n", value , alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset); writel(value, alpx_dev->base + alpx_dev->variant->gpios.base + alpx_dev->variant->gpios.outputs_reg_offset); } int alpx_gpio_get_direction(struct gpio_chip *chip, unsigned offset) { struct alpx_device *alpx_dev = gpiochip_get_data(chip); if (offset >= chip->ngpio) return -EINVAL; dev_dbg( alpx_dev->dev,"Offset %d is %s\n", offset, alpx_gpio_is_an_input(alpx_dev, offset) ? "INPUT" : "OUTPUT"); return alpx_gpio_is_an_input(alpx_dev, offset) ? GPIOF_DIR_IN : GPIOF_DIR_OUT; } int alpx_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int gpio_value) { struct alpx_device *alpx_dev = gpiochip_get_data(chip); if (alpx_gpio_is_an_input(alpx_dev, offset)) return -EINVAL; alpx_gpio_set(chip, offset, gpio_value); return 0; } int alpx_gpio_direction_input(struct gpio_chip *chip, unsigned offset) { /* All ports handle INPUTs operations */ return 0; } int alpx_gpio_register(struct alpx_device *alpx_dev, const char* card_name) { struct gpio_chip *chip = &alpx_dev->gpio_chip; chip->parent = alpx_dev->dev; chip->owner = THIS_MODULE; chip->label = card_name; chip->base = -1; chip->ngpio = alpx_dev->variant->gpios.inputs_qty + alpx_dev->variant->gpios.outputs_qty; chip->get = alpx_gpio_get; chip->set = alpx_gpio_set; chip->get_direction = alpx_gpio_get_direction; chip->direction_output = alpx_gpio_direction_output; chip->direction_input = alpx_gpio_direction_input; dev_dbg( alpx_dev->dev,"%s(): creating GP(%d)I(%d)O for %s.\n", __func__, alpx_dev->variant->gpios.inputs_qty, alpx_dev->variant->gpios.outputs_qty, chip->label); return gpiochip_add_data(chip, alpx_dev); } void alpx_gpio_unregister(struct alpx_device *alpx_dev) { struct gpio_chip* const chip = &alpx_dev->gpio_chip; if (chip->parent == alpx_dev->dev) gpiochip_remove(chip); }