1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
8  */
9 
10 #include <common.h>
11 #include <dm.h>
12 #include <errno.h>
13 #include <asm/gpio.h>
14 #include <asm/io.h>
15 #include <linux/bitops.h>
16 
17 struct bcm6345_gpio_priv {
18 	void __iomem *reg_dirout;
19 	void __iomem *reg_data;
20 };
21 
bcm6345_gpio_get_value(struct udevice * dev,unsigned offset)22 static int bcm6345_gpio_get_value(struct udevice *dev, unsigned offset)
23 {
24 	struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
25 
26 	return !!(readl(priv->reg_data) & BIT(offset));
27 }
28 
bcm6345_gpio_set_value(struct udevice * dev,unsigned offset,int value)29 static int bcm6345_gpio_set_value(struct udevice *dev, unsigned offset,
30 				  int value)
31 {
32 	struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
33 
34 	if (value)
35 		setbits_32(priv->reg_data, BIT(offset));
36 	else
37 		clrbits_32(priv->reg_data, BIT(offset));
38 
39 	return 0;
40 }
41 
bcm6345_gpio_set_direction(void __iomem * dirout,unsigned offset,bool input)42 static int bcm6345_gpio_set_direction(void __iomem *dirout, unsigned offset,
43 				      bool input)
44 {
45 	if (input)
46 		clrbits_32(dirout, BIT(offset));
47 	else
48 		setbits_32(dirout, BIT(offset));
49 
50 	return 0;
51 }
52 
bcm6345_gpio_direction_input(struct udevice * dev,unsigned offset)53 static int bcm6345_gpio_direction_input(struct udevice *dev, unsigned offset)
54 {
55 	struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
56 
57 	return bcm6345_gpio_set_direction(priv->reg_dirout, offset, 1);
58 }
59 
bcm6345_gpio_direction_output(struct udevice * dev,unsigned offset,int value)60 static int bcm6345_gpio_direction_output(struct udevice *dev, unsigned offset,
61 					 int value)
62 {
63 	struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
64 
65 	bcm6345_gpio_set_value(dev, offset, value);
66 
67 	return bcm6345_gpio_set_direction(priv->reg_dirout, offset, 0);
68 }
69 
bcm6345_gpio_get_function(struct udevice * dev,unsigned offset)70 static int bcm6345_gpio_get_function(struct udevice *dev, unsigned offset)
71 {
72 	struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
73 
74 	if (readl(priv->reg_dirout) & BIT(offset))
75 		return GPIOF_OUTPUT;
76 	else
77 		return GPIOF_INPUT;
78 }
79 
80 static const struct dm_gpio_ops bcm6345_gpio_ops = {
81 	.direction_input = bcm6345_gpio_direction_input,
82 	.direction_output = bcm6345_gpio_direction_output,
83 	.get_value = bcm6345_gpio_get_value,
84 	.set_value = bcm6345_gpio_set_value,
85 	.get_function = bcm6345_gpio_get_function,
86 };
87 
bcm6345_gpio_probe(struct udevice * dev)88 static int bcm6345_gpio_probe(struct udevice *dev)
89 {
90 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
91 	struct bcm6345_gpio_priv *priv = dev_get_priv(dev);
92 
93 	priv->reg_dirout = dev_remap_addr_index(dev, 0);
94 	if (!priv->reg_dirout)
95 		return -EINVAL;
96 
97 	priv->reg_data = dev_remap_addr_index(dev, 1);
98 	if (!priv->reg_data)
99 		return -EINVAL;
100 
101 	uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios", 32);
102 	uc_priv->bank_name = dev->name;
103 
104 	return 0;
105 }
106 
107 static const struct udevice_id bcm6345_gpio_ids[] = {
108 	{ .compatible = "brcm,bcm6345-gpio" },
109 	{ /* sentinel */ }
110 };
111 
112 U_BOOT_DRIVER(bcm6345_gpio) = {
113 	.name = "bcm6345-gpio",
114 	.id = UCLASS_GPIO,
115 	.of_match = bcm6345_gpio_ids,
116 	.ops = &bcm6345_gpio_ops,
117 	.priv_auto	= sizeof(struct bcm6345_gpio_priv),
118 	.probe = bcm6345_gpio_probe,
119 };
120