blob: e76c84e806ad5f7f91df6fbb0aabe6c146213a6e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +02002/*
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
4 *
5 * Derived from linux/arch/mips/bcm63xx/gpio.c:
6 * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
7 * Copyright (C) 2008-2011 Florian Fainelli <florian@openwrt.org>
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +02008 */
9
Simon Glass11c89f32017-05-17 17:18:03 -060010#include <dm.h>
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020011#include <errno.h>
12#include <asm/gpio.h>
13#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020015
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020016struct bcm6345_gpio_priv {
17 void __iomem *reg_dirout;
18 void __iomem *reg_data;
19};
20
21static int bcm6345_gpio_get_value(struct udevice *dev, unsigned offset)
22{
23 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
24
Philippe Reynesa8e21332019-03-07 11:36:31 +010025 return !!(readl(priv->reg_data) & BIT(offset));
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020026}
27
28static int bcm6345_gpio_set_value(struct udevice *dev, unsigned offset,
29 int value)
30{
31 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
32
33 if (value)
Philippe Reynesa8e21332019-03-07 11:36:31 +010034 setbits_32(priv->reg_data, BIT(offset));
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020035 else
Philippe Reynesa8e21332019-03-07 11:36:31 +010036 clrbits_32(priv->reg_data, BIT(offset));
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020037
38 return 0;
39}
40
41static int bcm6345_gpio_set_direction(void __iomem *dirout, unsigned offset,
42 bool input)
43{
44 if (input)
Philippe Reynesa8e21332019-03-07 11:36:31 +010045 clrbits_32(dirout, BIT(offset));
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020046 else
Philippe Reynesa8e21332019-03-07 11:36:31 +010047 setbits_32(dirout, BIT(offset));
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020048
49 return 0;
50}
51
52static int bcm6345_gpio_direction_input(struct udevice *dev, unsigned offset)
53{
54 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
55
56 return bcm6345_gpio_set_direction(priv->reg_dirout, offset, 1);
57}
58
59static int bcm6345_gpio_direction_output(struct udevice *dev, unsigned offset,
60 int value)
61{
62 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
63
Axel Lin90a59f22017-06-08 17:20:12 +080064 bcm6345_gpio_set_value(dev, offset, value);
65
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020066 return bcm6345_gpio_set_direction(priv->reg_dirout, offset, 0);
67}
68
69static int bcm6345_gpio_get_function(struct udevice *dev, unsigned offset)
70{
71 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
72
Philippe Reynesa8e21332019-03-07 11:36:31 +010073 if (readl(priv->reg_dirout) & BIT(offset))
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020074 return GPIOF_OUTPUT;
75 else
76 return GPIOF_INPUT;
77}
78
79static const struct dm_gpio_ops bcm6345_gpio_ops = {
80 .direction_input = bcm6345_gpio_direction_input,
81 .direction_output = bcm6345_gpio_direction_output,
82 .get_value = bcm6345_gpio_get_value,
83 .set_value = bcm6345_gpio_set_value,
84 .get_function = bcm6345_gpio_get_function,
85};
86
87static int bcm6345_gpio_probe(struct udevice *dev)
88{
89 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
90 struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020091
Álvaro Fernández Rojas18778842018-03-22 19:39:32 +010092 priv->reg_dirout = dev_remap_addr_index(dev, 0);
93 if (!priv->reg_dirout)
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020094 return -EINVAL;
95
Álvaro Fernández Rojas18778842018-03-22 19:39:32 +010096 priv->reg_data = dev_remap_addr_index(dev, 1);
97 if (!priv->reg_data)
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +020098 return -EINVAL;
99
Álvaro Fernández Rojas18778842018-03-22 19:39:32 +0100100 uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios", 32);
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +0200101 uc_priv->bank_name = dev->name;
102
103 return 0;
104}
105
106static const struct udevice_id bcm6345_gpio_ids[] = {
107 { .compatible = "brcm,bcm6345-gpio" },
108 { /* sentinel */ }
109};
110
111U_BOOT_DRIVER(bcm6345_gpio) = {
112 .name = "bcm6345-gpio",
113 .id = UCLASS_GPIO,
114 .of_match = bcm6345_gpio_ids,
115 .ops = &bcm6345_gpio_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700116 .priv_auto = sizeof(struct bcm6345_gpio_priv),
Álvaro Fernández Rojase9301112017-05-07 20:09:30 +0200117 .probe = bcm6345_gpio_probe,
118};