1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Microchip Technology Inc
4 * Purna Chandra Mandal <purna.mandal@microchip.com>
11 #include <asm/global_data.h>
14 #include <linux/bitops.h>
15 #include <linux/compat.h>
16 #include <mach/pic32.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 /* Peripheral Pin Control */
21 struct pic32_reg_port {
22 struct pic32_reg_atomic ansel;
23 struct pic32_reg_atomic tris;
24 struct pic32_reg_atomic port;
25 struct pic32_reg_atomic lat;
26 struct pic32_reg_atomic open_drain;
27 struct pic32_reg_atomic cnpu;
28 struct pic32_reg_atomic cnpd;
29 struct pic32_reg_atomic cncon;
33 MICROCHIP_GPIO_DIR_OUT,
34 MICROCHIP_GPIO_DIR_IN,
35 MICROCHIP_GPIOS_PER_BANK = 16,
38 struct pic32_gpio_priv {
39 struct pic32_reg_port *regs;
43 static int pic32_gpio_get_value(struct udevice *dev, unsigned offset)
45 struct pic32_gpio_priv *priv = dev_get_priv(dev);
47 return !!(readl(&priv->regs->port.raw) & BIT(offset));
50 static int pic32_gpio_set_value(struct udevice *dev, unsigned offset,
53 struct pic32_gpio_priv *priv = dev_get_priv(dev);
54 int mask = BIT(offset);
57 writel(mask, &priv->regs->port.set);
59 writel(mask, &priv->regs->port.clr);
64 static int pic32_gpio_direction(struct udevice *dev, unsigned offset)
66 struct pic32_gpio_priv *priv = dev_get_priv(dev);
68 /* pin in analog mode ? */
69 if (readl(&priv->regs->ansel.raw) & BIT(offset))
72 if (readl(&priv->regs->tris.raw) & BIT(offset))
73 return MICROCHIP_GPIO_DIR_IN;
75 return MICROCHIP_GPIO_DIR_OUT;
78 static int pic32_gpio_direction_input(struct udevice *dev, unsigned offset)
80 struct pic32_gpio_priv *priv = dev_get_priv(dev);
81 int mask = BIT(offset);
83 writel(mask, &priv->regs->ansel.clr);
84 writel(mask, &priv->regs->tris.set);
89 static int pic32_gpio_direction_output(struct udevice *dev,
90 unsigned offset, int value)
92 struct pic32_gpio_priv *priv = dev_get_priv(dev);
93 int mask = BIT(offset);
95 writel(mask, &priv->regs->ansel.clr);
96 writel(mask, &priv->regs->tris.clr);
98 pic32_gpio_set_value(dev, offset, value);
102 static int pic32_gpio_get_function(struct udevice *dev, unsigned offset)
104 int ret = GPIOF_UNUSED;
106 switch (pic32_gpio_direction(dev, offset)) {
107 case MICROCHIP_GPIO_DIR_OUT:
110 case MICROCHIP_GPIO_DIR_IN:
120 static const struct dm_gpio_ops gpio_pic32_ops = {
121 .direction_input = pic32_gpio_direction_input,
122 .direction_output = pic32_gpio_direction_output,
123 .get_value = pic32_gpio_get_value,
124 .set_value = pic32_gpio_set_value,
125 .get_function = pic32_gpio_get_function,
128 static int pic32_gpio_probe(struct udevice *dev)
130 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
131 struct pic32_gpio_priv *priv = dev_get_priv(dev);
137 addr = fdtdec_get_addr_size(gd->fdt_blob, dev_of_offset(dev), "reg",
139 if (addr == FDT_ADDR_T_NONE)
142 priv->regs = ioremap(addr, size);
144 uc_priv->gpio_count = MICROCHIP_GPIOS_PER_BANK;
145 /* extract bank name */
146 end = strrchr(dev->name, '@');
147 bank = trailing_strtoln(dev->name, end);
148 priv->name[0] = 'A' + bank;
149 uc_priv->bank_name = priv->name;
154 static const struct udevice_id pic32_gpio_ids[] = {
155 { .compatible = "microchip,pic32mzda-gpio" },
159 U_BOOT_DRIVER(gpio_pic32) = {
160 .name = "gpio_pic32",
162 .of_match = pic32_gpio_ids,
163 .ops = &gpio_pic32_ops,
164 .probe = pic32_gpio_probe,
165 .priv_auto = sizeof(struct pic32_gpio_priv),