/** * struct zynq_gpio - gpio device private data structure * @chip: instance of the gpio_chip * @base_addr: base address of the GPIO device * @clk: clock resource for this controller * @irq: interrupt for the GPIO device * @p_data: pointer to platform data * @context: context registers * @dirlock: lock used for direction in/out synchronization
*/ struct zynq_gpio { struct gpio_chip chip; void __iomem *base_addr; struct clk *clk; int irq; conststruct zynq_platform_data *p_data; struct gpio_regs context;
spinlock_t dirlock; /* lock */
};
/** * struct zynq_platform_data - zynq gpio platform data structure * @label: string to store in gpio->label * @quirks: Flags is used to identify the platform * @ngpio: max number of gpio pins * @max_bank: maximum number of gpio banks * @bank_min: this array represents bank's min pin * @bank_max: this array represents bank's max pin
*/ struct zynq_platform_data { constchar *label;
u32 quirks;
u16 ngpio; int max_bank; int bank_min[ZYNQMP_GPIO_MAX_BANK]; int bank_max[ZYNQMP_GPIO_MAX_BANK];
};
/** * zynq_gpio_is_zynq - test if HW is zynq or zynqmp * @gpio: Pointer to driver data struct * * Return: 0 if zynqmp, 1 if zynq.
*/ staticint zynq_gpio_is_zynq(struct zynq_gpio *gpio)
{ return !!(gpio->p_data->quirks & ZYNQ_GPIO_QUIRK_IS_ZYNQ);
}
/** * gpio_data_ro_bug - test if HW bug exists or not * @gpio: Pointer to driver data struct * * Return: 0 if bug doesnot exist, 1 if bug exists.
*/ staticint gpio_data_ro_bug(struct zynq_gpio *gpio)
{ return !!(gpio->p_data->quirks & GPIO_QUIRK_DATA_RO_BUG);
}
/** * zynq_gpio_get_bank_pin - Get the bank number and pin number within that bank * for a given pin in the GPIO device * @pin_num: gpio pin number within the device * @bank_num: an output parameter used to return the bank number of the gpio * pin * @bank_pin_num: an output parameter used to return pin number within a bank * for the given gpio pin * @gpio: gpio device data structure * * Returns the bank number and pin offset within the bank.
*/ staticinlinevoid zynq_gpio_get_bank_pin(unsignedint pin_num, unsignedint *bank_num, unsignedint *bank_pin_num, struct zynq_gpio *gpio)
{ int bank;
for (bank = 0; bank < gpio->p_data->max_bank; bank++) { if ((pin_num >= gpio->p_data->bank_min[bank]) &&
(pin_num <= gpio->p_data->bank_max[bank])) {
*bank_num = bank;
*bank_pin_num = pin_num -
gpio->p_data->bank_min[bank]; return;
} if (gpio->p_data->quirks & GPIO_QUIRK_VERSAL)
bank = bank + VERSAL_UNUSED_BANKS;
}
/** * zynq_gpio_get_value - Get the state of the specified pin of GPIO device * @chip: gpio_chip instance to be worked on * @pin: gpio pin number within the device * * This function reads the state of the specified pin of the GPIO device. * * Return: 0 if the pin is low, 1 if pin is high.
*/ staticint zynq_gpio_get_value(struct gpio_chip *chip, unsignedint pin)
{
u32 data; unsignedint bank_num, bank_pin_num; struct zynq_gpio *gpio = gpiochip_get_data(chip);
if (gpio_data_ro_bug(gpio)) { if (zynq_gpio_is_zynq(gpio)) { if (bank_num <= 1) {
data = readl_relaxed(gpio->base_addr +
ZYNQ_GPIO_DATA_RO_OFFSET(bank_num));
} else {
data = readl_relaxed(gpio->base_addr +
ZYNQ_GPIO_DATA_OFFSET(bank_num));
}
} else { if (bank_num <= 2) {
data = readl_relaxed(gpio->base_addr +
ZYNQ_GPIO_DATA_RO_OFFSET(bank_num));
} else {
data = readl_relaxed(gpio->base_addr +
ZYNQ_GPIO_DATA_OFFSET(bank_num));
}
}
} else {
data = readl_relaxed(gpio->base_addr +
ZYNQ_GPIO_DATA_RO_OFFSET(bank_num));
} return (data >> bank_pin_num) & 1;
}
/** * zynq_gpio_set_value - Modify the state of the pin with specified value * @chip: gpio_chip instance to be worked on * @pin: gpio pin number within the device * @state: value used to modify the state of the specified pin * * This function calculates the register offset (i.e to lower 16 bits or * upper 16 bits) based on the given pin number and sets the state of a * gpio pin to the specified value. The state is either 0 or non-zero.
*/ staticint zynq_gpio_set_value(struct gpio_chip *chip, unsignedint pin, int state)
{ unsignedint reg_offset, bank_num, bank_pin_num; struct zynq_gpio *gpio = gpiochip_get_data(chip);
if (bank_pin_num >= ZYNQ_GPIO_MID_PIN_NUM) { /* only 16 data bits in bit maskable reg */
bank_pin_num -= ZYNQ_GPIO_MID_PIN_NUM;
reg_offset = ZYNQ_GPIO_DATA_MSW_OFFSET(bank_num);
} else {
reg_offset = ZYNQ_GPIO_DATA_LSW_OFFSET(bank_num);
}
/* * get the 32 bit value to be written to the mask/data register where * the upper 16 bits is the mask and lower 16 bits is the data
*/
state = !!state;
state = ~(1 << (bank_pin_num + ZYNQ_GPIO_MID_PIN_NUM)) &
((state << bank_pin_num) | ZYNQ_GPIO_UPPER_MASK);
/** * zynq_gpio_dir_in - Set the direction of the specified GPIO pin as input * @chip: gpio_chip instance to be worked on * @pin: gpio pin number within the device * * This function uses the read-modify-write sequence to set the direction of * the gpio pin as input. * * Return: 0 always
*/ staticint zynq_gpio_dir_in(struct gpio_chip *chip, unsignedint pin)
{
u32 reg; unsignedint bank_num, bank_pin_num; unsignedlong flags; struct zynq_gpio *gpio = gpiochip_get_data(chip);
/* * On zynq bank 0 pins 7 and 8 are special and cannot be used * as inputs.
*/ if (zynq_gpio_is_zynq(gpio) && bank_num == 0 &&
(bank_pin_num == 7 || bank_pin_num == 8)) return -EINVAL;
/* clear the bit in direction mode reg to set the pin as input */
spin_lock_irqsave(&gpio->dirlock, flags);
reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
reg &= ~BIT(bank_pin_num);
writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
spin_unlock_irqrestore(&gpio->dirlock, flags);
return 0;
}
/** * zynq_gpio_dir_out - Set the direction of the specified GPIO pin as output * @chip: gpio_chip instance to be worked on * @pin: gpio pin number within the device * @state: value to be written to specified pin * * This function sets the direction of specified GPIO pin as output, configures * the Output Enable register for the pin and uses zynq_gpio_set to set * the state of the pin to the value specified. * * Return: 0 always
*/ staticint zynq_gpio_dir_out(struct gpio_chip *chip, unsignedint pin, int state)
{
u32 reg; unsignedint bank_num, bank_pin_num; unsignedlong flags; struct zynq_gpio *gpio = gpiochip_get_data(chip);
/* set the GPIO pin as output */
spin_lock_irqsave(&gpio->dirlock, flags);
reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
reg |= BIT(bank_pin_num);
writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
/* configure the output enable reg for the pin */
reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_OUTEN_OFFSET(bank_num));
reg |= BIT(bank_pin_num);
writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_OUTEN_OFFSET(bank_num));
spin_unlock_irqrestore(&gpio->dirlock, flags);
/* set the state of the pin */
zynq_gpio_set_value(chip, pin, state); return 0;
}
/** * zynq_gpio_get_direction - Read the direction of the specified GPIO pin * @chip: gpio_chip instance to be worked on * @pin: gpio pin number within the device * * This function returns the direction of the specified GPIO. * * Return: GPIO_LINE_DIRECTION_OUT or GPIO_LINE_DIRECTION_IN
*/ staticint zynq_gpio_get_direction(struct gpio_chip *chip, unsignedint pin)
{
u32 reg; unsignedint bank_num, bank_pin_num; struct zynq_gpio *gpio = gpiochip_get_data(chip);
if (reg & BIT(bank_pin_num)) return GPIO_LINE_DIRECTION_OUT;
return GPIO_LINE_DIRECTION_IN;
}
/** * zynq_gpio_irq_mask - Disable the interrupts for a gpio pin * @irq_data: per irq and chip data passed down to chip functions * * This function calculates gpio pin number from irq number and sets the * bit in the Interrupt Disable register of the corresponding bank to disable * interrupts for that pin.
*/ staticvoid zynq_gpio_irq_mask(struct irq_data *irq_data)
{ unsignedint device_pin_num, bank_num, bank_pin_num; constunsignedlong offset = irqd_to_hwirq(irq_data); struct gpio_chip *chip = irq_data_get_irq_chip_data(irq_data); struct zynq_gpio *gpio =
gpiochip_get_data(irq_data_get_irq_chip_data(irq_data));
/** * zynq_gpio_irq_unmask - Enable the interrupts for a gpio pin * @irq_data: irq data containing irq number of gpio pin for the interrupt * to enable * * This function calculates the gpio pin number from irq number and sets the * bit in the Interrupt Enable register of the corresponding bank to enable * interrupts for that pin.
*/ staticvoid zynq_gpio_irq_unmask(struct irq_data *irq_data)
{ unsignedint device_pin_num, bank_num, bank_pin_num; constunsignedlong offset = irqd_to_hwirq(irq_data); struct gpio_chip *chip = irq_data_get_irq_chip_data(irq_data); struct zynq_gpio *gpio =
gpiochip_get_data(irq_data_get_irq_chip_data(irq_data));
/** * zynq_gpio_irq_ack - Acknowledge the interrupt of a gpio pin * @irq_data: irq data containing irq number of gpio pin for the interrupt * to ack * * This function calculates gpio pin number from irq number and sets the bit * in the Interrupt Status Register of the corresponding bank, to ACK the irq.
*/ staticvoid zynq_gpio_irq_ack(struct irq_data *irq_data)
{ unsignedint device_pin_num, bank_num, bank_pin_num; struct zynq_gpio *gpio =
gpiochip_get_data(irq_data_get_irq_chip_data(irq_data));
/** * zynq_gpio_irq_enable - Enable the interrupts for a gpio pin * @irq_data: irq data containing irq number of gpio pin for the interrupt * to enable * * Clears the INTSTS bit and unmasks the given interrupt.
*/ staticvoid zynq_gpio_irq_enable(struct irq_data *irq_data)
{ /* * The Zynq GPIO controller does not disable interrupt detection when * the interrupt is masked and only disables the propagation of the * interrupt. This means when the controller detects an interrupt * condition while the interrupt is logically disabled it will propagate * that interrupt event once the interrupt is enabled. This will cause * the interrupt consumer to see spurious interrupts to prevent this * first make sure that the interrupt is not asserted and then enable * it.
*/
zynq_gpio_irq_ack(irq_data);
zynq_gpio_irq_unmask(irq_data);
}
/** * zynq_gpio_set_irq_type - Set the irq type for a gpio pin * @irq_data: irq data containing irq number of gpio pin * @type: interrupt type that is to be set for the gpio pin * * This function gets the gpio pin number and its bank from the gpio pin number * and configures the INT_TYPE, INT_POLARITY and INT_ANY registers. * * Return: 0, negative error otherwise. * TYPE-EDGE_RISING, INT_TYPE - 1, INT_POLARITY - 1, INT_ANY - 0; * TYPE-EDGE_FALLING, INT_TYPE - 1, INT_POLARITY - 0, INT_ANY - 0; * TYPE-EDGE_BOTH, INT_TYPE - 1, INT_POLARITY - NA, INT_ANY - 1; * TYPE-LEVEL_HIGH, INT_TYPE - 0, INT_POLARITY - 1, INT_ANY - NA; * TYPE-LEVEL_LOW, INT_TYPE - 0, INT_POLARITY - 0, INT_ANY - NA
*/ staticint zynq_gpio_set_irq_type(struct irq_data *irq_data, unsignedint type)
{
u32 int_type, int_pol, int_any; unsignedint device_pin_num, bank_num, bank_pin_num; struct zynq_gpio *gpio =
gpiochip_get_data(irq_data_get_irq_chip_data(irq_data));
/** * zynq_gpio_irqhandler - IRQ handler for the gpio banks of a gpio device * @desc: irq descriptor instance of the 'irq' * * This function reads the Interrupt Status Register of each bank to get the * gpio pin number which has triggered an interrupt. It then acks the triggered * interrupt and calls the pin specific handler set by the higher layer * application for that pin. * Note: A bug is reported if no handler is set for the gpio pin.
*/ staticvoid zynq_gpio_irqhandler(struct irq_desc *desc)
{
u32 int_sts, int_enb; unsignedint bank_num; struct zynq_gpio *gpio =
gpiochip_get_data(irq_desc_get_handler_data(desc)); struct irq_chip *irqchip = irq_desc_get_chip(desc);
staticint zynq_gpio_request(struct gpio_chip *chip, unsignedint offset)
{ int ret;
ret = pm_runtime_get_sync(chip->parent);
/* * If the device is already active pm_runtime_get() will return 1 on * success, but gpio_request still needs to return 0.
*/ return ret < 0 ? ret : 0;
}
/** * zynq_gpio_probe - Initialization method for a zynq_gpio device * @pdev: platform device instance * * This function allocates memory resources for the gpio device and registers * all the banks of the device. It will also set up interrupts for the gpio * pins. * Note: Interrupts are disabled for all the banks during initialization. * * Return: 0 on success, negative error otherwise.
*/ staticint zynq_gpio_probe(struct platform_device *pdev)
{ int ret, bank_num; struct zynq_gpio *gpio; struct gpio_chip *chip; struct gpio_irq_chip *girq; conststruct of_device_id *match;
gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL); if (!gpio) return -ENOMEM;
match = of_match_node(zynq_gpio_of_match, pdev->dev.of_node); if (!match) {
dev_err(&pdev->dev, "of_match_node() failed\n"); return -EINVAL;
}
gpio->p_data = match->data;
platform_set_drvdata(pdev, gpio);
gpio->base_addr = devm_platform_ioremap_resource(pdev, 0); if (IS_ERR(gpio->base_addr)) return PTR_ERR(gpio->base_addr);
gpio->irq = platform_get_irq(pdev, 0); if (gpio->irq < 0) return gpio->irq;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.