blob: 253a9ba2f84cc63fabe5656f98da5e5a16191cfb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Purna Chandra Mandal0ee99ca2016-01-28 15:30:13 +05302/*
3 * Copyright (c) 2015 Microchip Technology Inc
4 * Purna Chandra Mandal <purna.mandal@microchip.com>
Purna Chandra Mandal0ee99ca2016-01-28 15:30:13 +05305 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <malloc.h>
11#include <asm/io.h>
12#include <asm/gpio.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060013#include <linux/bitops.h>
Purna Chandra Mandal0ee99ca2016-01-28 15:30:13 +053014#include <linux/compat.h>
Purna Chandra Mandal0ee99ca2016-01-28 15:30:13 +053015#include <mach/pic32.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19/* Peripheral Pin Control */
20struct pic32_reg_port {
21 struct pic32_reg_atomic ansel;
22 struct pic32_reg_atomic tris;
23 struct pic32_reg_atomic port;
24 struct pic32_reg_atomic lat;
25 struct pic32_reg_atomic open_drain;
26 struct pic32_reg_atomic cnpu;
27 struct pic32_reg_atomic cnpd;
28 struct pic32_reg_atomic cncon;
29};
30
31enum {
32 MICROCHIP_GPIO_DIR_OUT,
33 MICROCHIP_GPIO_DIR_IN,
34 MICROCHIP_GPIOS_PER_BANK = 16,
35};
36
37struct pic32_gpio_priv {
38 struct pic32_reg_port *regs;
39 char name[2];
40};
41
42static int pic32_gpio_get_value(struct udevice *dev, unsigned offset)
43{
44 struct pic32_gpio_priv *priv = dev_get_priv(dev);
45
46 return !!(readl(&priv->regs->port.raw) & BIT(offset));
47}
48
49static int pic32_gpio_set_value(struct udevice *dev, unsigned offset,
50 int value)
51{
52 struct pic32_gpio_priv *priv = dev_get_priv(dev);
53 int mask = BIT(offset);
54
55 if (value)
56 writel(mask, &priv->regs->port.set);
57 else
58 writel(mask, &priv->regs->port.clr);
59
60 return 0;
61}
62
63static int pic32_gpio_direction(struct udevice *dev, unsigned offset)
64{
65 struct pic32_gpio_priv *priv = dev_get_priv(dev);
66
67 /* pin in analog mode ? */
68 if (readl(&priv->regs->ansel.raw) & BIT(offset))
69 return -EPERM;
70
71 if (readl(&priv->regs->tris.raw) & BIT(offset))
72 return MICROCHIP_GPIO_DIR_IN;
73 else
74 return MICROCHIP_GPIO_DIR_OUT;
75}
76
77static int pic32_gpio_direction_input(struct udevice *dev, unsigned offset)
78{
79 struct pic32_gpio_priv *priv = dev_get_priv(dev);
80 int mask = BIT(offset);
81
82 writel(mask, &priv->regs->ansel.clr);
83 writel(mask, &priv->regs->tris.set);
84
85 return 0;
86}
87
88static int pic32_gpio_direction_output(struct udevice *dev,
89 unsigned offset, int value)
90{
91 struct pic32_gpio_priv *priv = dev_get_priv(dev);
92 int mask = BIT(offset);
93
94 writel(mask, &priv->regs->ansel.clr);
95 writel(mask, &priv->regs->tris.clr);
96
97 pic32_gpio_set_value(dev, offset, value);
98 return 0;
99}
100
Purna Chandra Mandal0ee99ca2016-01-28 15:30:13 +0530101static int pic32_gpio_get_function(struct udevice *dev, unsigned offset)
102{
103 int ret = GPIOF_UNUSED;
104
105 switch (pic32_gpio_direction(dev, offset)) {
106 case MICROCHIP_GPIO_DIR_OUT:
107 ret = GPIOF_OUTPUT;
108 break;
109 case MICROCHIP_GPIO_DIR_IN:
110 ret = GPIOF_INPUT;
111 break;
112 default:
113 ret = GPIOF_UNUSED;
114 break;
115 }
116 return ret;
117}
118
119static const struct dm_gpio_ops gpio_pic32_ops = {
120 .direction_input = pic32_gpio_direction_input,
121 .direction_output = pic32_gpio_direction_output,
122 .get_value = pic32_gpio_get_value,
123 .set_value = pic32_gpio_set_value,
124 .get_function = pic32_gpio_get_function,
Purna Chandra Mandal0ee99ca2016-01-28 15:30:13 +0530125};
126
127static int pic32_gpio_probe(struct udevice *dev)
128{
129 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
130 struct pic32_gpio_priv *priv = dev_get_priv(dev);
131 fdt_addr_t addr;
132 fdt_size_t size;
133 char *end;
134 int bank;
135
Simon Glassdd79d6e2017-01-17 16:52:55 -0700136 addr = fdtdec_get_addr_size(gd->fdt_blob, dev_of_offset(dev), "reg",
137 &size);
Purna Chandra Mandal0ee99ca2016-01-28 15:30:13 +0530138 if (addr == FDT_ADDR_T_NONE)
139 return -EINVAL;
140
141 priv->regs = ioremap(addr, size);
142
143 uc_priv->gpio_count = MICROCHIP_GPIOS_PER_BANK;
144 /* extract bank name */
145 end = strrchr(dev->name, '@');
146 bank = trailing_strtoln(dev->name, end);
147 priv->name[0] = 'A' + bank;
148 uc_priv->bank_name = priv->name;
149
150 return 0;
151}
152
153static const struct udevice_id pic32_gpio_ids[] = {
154 { .compatible = "microchip,pic32mzda-gpio" },
155 { }
156};
157
158U_BOOT_DRIVER(gpio_pic32) = {
159 .name = "gpio_pic32",
160 .id = UCLASS_GPIO,
161 .of_match = pic32_gpio_ids,
162 .ops = &gpio_pic32_ops,
163 .probe = pic32_gpio_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700164 .priv_auto = sizeof(struct pic32_gpio_priv),
Purna Chandra Mandal0ee99ca2016-01-28 15:30:13 +0530165};