2 * Copyright (C) 2016 Socionext Inc.
3 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <linux/bitops.h>
12 #include <linux/sizes.h>
13 #include <linux/errno.h>
16 #define UNIPHIER_GPIO_PORTS_PER_BANK 8
18 #define UNIPHIER_GPIO_REG_DATA 0 /* data */
19 #define UNIPHIER_GPIO_REG_DIR 4 /* direction (1:in, 0:out) */
21 struct uniphier_gpio_priv
{
26 static void uniphier_gpio_offset_write(struct udevice
*dev
, unsigned offset
,
27 unsigned reg
, int value
)
29 struct uniphier_gpio_priv
*priv
= dev_get_priv(dev
);
32 tmp
= readl(priv
->base
+ reg
);
37 writel(tmp
, priv
->base
+ reg
);
40 static int uniphier_gpio_offset_read(struct udevice
*dev
, unsigned offset
,
43 struct uniphier_gpio_priv
*priv
= dev_get_priv(dev
);
45 return !!(readl(priv
->base
+ reg
) & BIT(offset
));
48 static int uniphier_gpio_direction_input(struct udevice
*dev
, unsigned offset
)
50 uniphier_gpio_offset_write(dev
, offset
, UNIPHIER_GPIO_REG_DIR
, 1);
55 static int uniphier_gpio_direction_output(struct udevice
*dev
, unsigned offset
,
58 uniphier_gpio_offset_write(dev
, offset
, UNIPHIER_GPIO_REG_DATA
, value
);
59 uniphier_gpio_offset_write(dev
, offset
, UNIPHIER_GPIO_REG_DIR
, 0);
64 static int uniphier_gpio_get_value(struct udevice
*dev
, unsigned offset
)
66 return uniphier_gpio_offset_read(dev
, offset
, UNIPHIER_GPIO_REG_DATA
);
69 static int uniphier_gpio_set_value(struct udevice
*dev
, unsigned offset
,
72 uniphier_gpio_offset_write(dev
, offset
, UNIPHIER_GPIO_REG_DATA
, value
);
77 static int uniphier_gpio_get_function(struct udevice
*dev
, unsigned offset
)
79 return uniphier_gpio_offset_read(dev
, offset
, UNIPHIER_GPIO_REG_DIR
) ?
80 GPIOF_INPUT
: GPIOF_OUTPUT
;
83 static const struct dm_gpio_ops uniphier_gpio_ops
= {
84 .direction_input
= uniphier_gpio_direction_input
,
85 .direction_output
= uniphier_gpio_direction_output
,
86 .get_value
= uniphier_gpio_get_value
,
87 .set_value
= uniphier_gpio_set_value
,
88 .get_function
= uniphier_gpio_get_function
,
91 static int uniphier_gpio_probe(struct udevice
*dev
)
93 struct uniphier_gpio_priv
*priv
= dev_get_priv(dev
);
94 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
98 addr
= devfdt_get_addr(dev
);
99 if (addr
== FDT_ADDR_T_NONE
)
102 priv
->base
= devm_ioremap(dev
, addr
, SZ_8
);
106 uc_priv
->gpio_count
= UNIPHIER_GPIO_PORTS_PER_BANK
;
108 tmp
= (addr
& 0xfff);
110 /* Unfortunately, there is a register hole at offset 0x90-0x9f. */
114 snprintf(priv
->bank_name
, sizeof(priv
->bank_name
) - 1,
115 "port%d-", (tmp
- 8) / 8);
117 uc_priv
->bank_name
= priv
->bank_name
;
122 /* .data = the number of GPIO banks */
123 static const struct udevice_id uniphier_gpio_match
[] = {
124 { .compatible
= "socionext,uniphier-gpio" },
128 U_BOOT_DRIVER(uniphier_gpio
) = {
129 .name
= "uniphier_gpio",
131 .of_match
= uniphier_gpio_match
,
132 .probe
= uniphier_gpio_probe
,
133 .priv_auto_alloc_size
= sizeof(struct uniphier_gpio_priv
),
134 .ops
= &uniphier_gpio_ops
,