1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020 Broadcom
11 #include <dm/device_compat.h>
12 #include <dm/devres.h>
13 #include <dm/pinctrl.h>
16 * There are five GPIO bank register. Each bank can configure max of 32 gpios.
17 * BANK0 - gpios 0 to 31
18 * BANK1 - gpios 32 to 63
19 * BANK2 - gpios 64 to 95
20 * BANK3 - gpios 96 to 127
21 * BANK4 - gpios 128 to 150
23 * Offset difference between consecutive bank register is 0x200
25 #define NGPIO_PER_BANK 32
26 #define GPIO_BANK_SIZE 0x200
27 #define GPIO_BANK(pin) ((pin) / NGPIO_PER_BANK)
28 #define GPIO_SHIFT(pin) ((pin) % NGPIO_PER_BANK)
29 #define GPIO_REG(pin, reg) (GPIO_BANK_SIZE * GPIO_BANK(pin) + (reg))
31 /* device register offset */
32 #define DATA_IN_OFFSET 0x00
33 #define DATA_OUT_OFFSET 0x04
34 #define OUT_EN_OFFSET 0x08
37 * struct iproc_gpio_pctrl_map - gpio and pinctrl mapping
38 * @gpio_pin: start of gpio number in gpio-ranges
39 * @pctrl_pin: start of pinctrl number in gpio-ranges
40 * @npins: total number of pins in gpio-ranges
43 struct iproc_gpio_pctrl_map {
47 struct list_head node;
51 * struct iproc_gpio_pctrl_map - gpio device instance
52 * @pinctrl_dev:pointer to pinctrl device
53 * @gpiomap: list node having mapping between gpio and pinctrl
54 * @base: I/O register base address of gpio device
55 * @name: gpio device name, ex GPIO0, GPIO1
56 * @ngpios: total number of gpios
58 struct iproc_gpio_plat {
59 struct udevice *pinctrl_dev;
60 struct list_head gpiomap;
67 * iproc_gpio_set_bit - set or clear one bit in an iproc GPIO register.
69 * The bit relates to a GPIO pin.
71 * @plat: iproc GPIO device
72 * @reg: register offset
76 static inline void iproc_gpio_set_bit(struct iproc_gpio_plat *plat,
77 u32 reg, u32 gpio, bool set)
79 u32 offset = GPIO_REG(gpio, reg);
80 u32 shift = GPIO_SHIFT(gpio);
82 clrsetbits_le32(plat->base + offset, BIT(shift),
83 (set ? BIT(shift) : 0));
86 static inline bool iproc_gpio_get_bit(struct iproc_gpio_plat *plat,
89 u32 offset = GPIO_REG(gpio, reg);
90 u32 shift = GPIO_SHIFT(gpio);
92 return readl(plat->base + offset) & BIT(shift);
96 * iproc_get_gpio_pctrl_mapping() - get associated pinctrl pin from gpio pin
98 * @plat: iproc GPIO device
101 static u32 iproc_get_pctrl_from_gpio(struct iproc_gpio_plat *plat, u32 gpio)
103 struct iproc_gpio_pctrl_map *range = NULL;
104 struct list_head *pos, *tmp;
107 list_for_each_safe(pos, tmp, &plat->gpiomap) {
108 range = list_entry(pos, struct iproc_gpio_pctrl_map, node);
109 if (gpio == range->gpio_pin ||
110 gpio < (range->gpio_pin + range->npins)) {
111 ret = range->pctrl_pin + (gpio - range->gpio_pin);
120 * iproc_get_gpio_pctrl_mapping() - get mapping between gpio and pinctrl
122 * Read dt node "gpio-ranges" to get gpio and pinctrl mapping and store
123 * in private data structure to use it later while enabling gpio.
125 * @dev: pointer to GPIO device
126 * @return 0 on success and -ENOMEM on failure
128 static int iproc_get_gpio_pctrl_mapping(struct udevice *dev)
130 struct iproc_gpio_plat *plat = dev_get_plat(dev);
131 struct iproc_gpio_pctrl_map *range = NULL;
132 struct ofnode_phandle_args args;
136 ret = dev_read_phandle_with_args(dev, "gpio-ranges",
137 NULL, 3, index, &args);
141 range = devm_kzalloc(dev, sizeof(*range), GFP_KERNEL);
145 range->gpio_pin = args.args[0];
146 range->pctrl_pin = args.args[1];
147 range->npins = args.args[2];
148 list_add_tail(&range->node, &plat->gpiomap);
154 static int iproc_gpio_request(struct udevice *dev, u32 gpio, const char *label)
156 struct iproc_gpio_plat *plat = dev_get_plat(dev);
159 /* nothing to do if there is no corresponding pinctrl device */
160 if (!plat->pinctrl_dev)
163 pctrl = iproc_get_pctrl_from_gpio(plat, gpio);
165 return pinctrl_request(plat->pinctrl_dev, pctrl, 0);
168 static int iproc_gpio_direction_input(struct udevice *dev, u32 gpio)
170 struct iproc_gpio_plat *plat = dev_get_plat(dev);
172 iproc_gpio_set_bit(plat, OUT_EN_OFFSET, gpio, false);
173 dev_dbg(dev, "gpio:%u set input\n", gpio);
178 static int iproc_gpio_direction_output(struct udevice *dev, u32 gpio, int value)
180 struct iproc_gpio_plat *plat = dev_get_plat(dev);
182 iproc_gpio_set_bit(plat, OUT_EN_OFFSET, gpio, true);
183 iproc_gpio_set_bit(plat, DATA_OUT_OFFSET, gpio, value);
184 dev_dbg(dev, "gpio:%u set output, value:%d\n", gpio, value);
189 static int iproc_gpio_get_value(struct udevice *dev, u32 gpio)
191 struct iproc_gpio_plat *plat = dev_get_plat(dev);
194 value = iproc_gpio_get_bit(plat, DATA_IN_OFFSET, gpio);
195 dev_dbg(dev, "gpio:%u get, value:%d\n", gpio, value);
200 static int iproc_gpio_set_value(struct udevice *dev, u32 gpio, int value)
202 struct iproc_gpio_plat *plat = dev_get_plat(dev);
204 if (iproc_gpio_get_bit(plat, OUT_EN_OFFSET, gpio))
205 iproc_gpio_set_bit(plat, DATA_OUT_OFFSET, gpio, value);
207 dev_dbg(dev, "gpio:%u set, value:%d\n", gpio, value);
211 static int iproc_gpio_get_function(struct udevice *dev, u32 gpio)
213 struct iproc_gpio_plat *plat = dev_get_plat(dev);
215 if (iproc_gpio_get_bit(plat, OUT_EN_OFFSET, gpio))
221 static int iproc_gpio_of_to_plat(struct udevice *dev)
223 struct iproc_gpio_plat *plat = dev_get_plat(dev);
224 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
228 plat->base = dev_read_addr_ptr(dev);
230 debug("%s: Failed to get base address\n", __func__);
234 ret = dev_read_u32(dev, "ngpios", &plat->ngpios);
236 dev_err(dev, "%s: Failed to get ngpios\n", __func__);
240 uclass_get_device_by_phandle(UCLASS_PINCTRL, dev, "gpio-ranges",
243 dev_err(dev, "%s: Failed to get pinctrl phandle\n", __func__);
247 INIT_LIST_HEAD(&plat->gpiomap);
248 ret = iproc_get_gpio_pctrl_mapping(dev);
250 dev_err(dev, "%s: Failed to get gpio to pctrl map ret(%d)\n",
255 snprintf(name, sizeof(name), "GPIO%d", dev_seq(dev));
256 plat->name = strdup(name);
260 uc_priv->gpio_count = plat->ngpios;
261 uc_priv->bank_name = plat->name;
263 dev_info(dev, ":bank name(%s) base %p, #gpios %d\n",
264 plat->name, plat->base, plat->ngpios);
269 static const struct dm_gpio_ops iproc_gpio_ops = {
270 .request = iproc_gpio_request,
271 .direction_input = iproc_gpio_direction_input,
272 .direction_output = iproc_gpio_direction_output,
273 .get_value = iproc_gpio_get_value,
274 .set_value = iproc_gpio_set_value,
275 .get_function = iproc_gpio_get_function,
278 static const struct udevice_id iproc_gpio_ids[] = {
279 { .compatible = "brcm,iproc-gpio" },
283 U_BOOT_DRIVER(iproc_gpio) = {
284 .name = "iproc_gpio",
286 .of_match = iproc_gpio_ids,
287 .ops = &iproc_gpio_ops,
288 .of_to_plat = iproc_gpio_of_to_plat,
289 .plat_auto = sizeof(struct iproc_gpio_plat),