2 * Copyright (C) 2012 Vikram Narayananan
3 * <vikram186@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <dm/pinctrl.h>
16 struct bcm2835_gpios
{
17 struct bcm2835_gpio_regs
*reg
;
18 struct udevice
*pinctrl
;
21 static int bcm2835_gpio_direction_input(struct udevice
*dev
, unsigned gpio
)
23 struct bcm2835_gpios
*gpios
= dev_get_priv(dev
);
26 val
= readl(&gpios
->reg
->gpfsel
[BCM2835_GPIO_FSEL_BANK(gpio
)]);
27 val
&= ~(BCM2835_GPIO_FSEL_MASK
<< BCM2835_GPIO_FSEL_SHIFT(gpio
));
28 val
|= (BCM2835_GPIO_INPUT
<< BCM2835_GPIO_FSEL_SHIFT(gpio
));
29 writel(val
, &gpios
->reg
->gpfsel
[BCM2835_GPIO_FSEL_BANK(gpio
)]);
34 static int bcm2835_gpio_direction_output(struct udevice
*dev
, unsigned int gpio
,
37 struct bcm2835_gpios
*gpios
= dev_get_priv(dev
);
40 gpio_set_value(gpio
, value
);
42 val
= readl(&gpios
->reg
->gpfsel
[BCM2835_GPIO_FSEL_BANK(gpio
)]);
43 val
&= ~(BCM2835_GPIO_FSEL_MASK
<< BCM2835_GPIO_FSEL_SHIFT(gpio
));
44 val
|= (BCM2835_GPIO_OUTPUT
<< BCM2835_GPIO_FSEL_SHIFT(gpio
));
45 writel(val
, &gpios
->reg
->gpfsel
[BCM2835_GPIO_FSEL_BANK(gpio
)]);
50 static int bcm2835_get_value(const struct bcm2835_gpios
*gpios
, unsigned gpio
)
54 val
= readl(&gpios
->reg
->gplev
[BCM2835_GPIO_COMMON_BANK(gpio
)]);
56 return (val
>> BCM2835_GPIO_COMMON_SHIFT(gpio
)) & 0x1;
59 static int bcm2835_gpio_get_value(struct udevice
*dev
, unsigned gpio
)
61 const struct bcm2835_gpios
*gpios
= dev_get_priv(dev
);
63 return bcm2835_get_value(gpios
, gpio
);
66 static int bcm2835_gpio_set_value(struct udevice
*dev
, unsigned gpio
,
69 struct bcm2835_gpios
*gpios
= dev_get_priv(dev
);
70 u32
*output_reg
= value
? gpios
->reg
->gpset
: gpios
->reg
->gpclr
;
72 writel(1 << BCM2835_GPIO_COMMON_SHIFT(gpio
),
73 &output_reg
[BCM2835_GPIO_COMMON_BANK(gpio
)]);
78 static int bcm2835_gpio_get_function(struct udevice
*dev
, unsigned offset
)
80 struct bcm2835_gpios
*priv
= dev_get_priv(dev
);
83 funcid
= pinctrl_get_gpio_mux(priv
->pinctrl
, 0, offset
);
86 case BCM2835_GPIO_OUTPUT
:
88 case BCM2835_GPIO_INPUT
:
95 static const struct dm_gpio_ops gpio_bcm2835_ops
= {
96 .direction_input
= bcm2835_gpio_direction_input
,
97 .direction_output
= bcm2835_gpio_direction_output
,
98 .get_value
= bcm2835_gpio_get_value
,
99 .set_value
= bcm2835_gpio_set_value
,
100 .get_function
= bcm2835_gpio_get_function
,
103 static int bcm2835_gpio_probe(struct udevice
*dev
)
105 struct bcm2835_gpios
*gpios
= dev_get_priv(dev
);
106 struct bcm2835_gpio_platdata
*plat
= dev_get_platdata(dev
);
107 struct gpio_dev_priv
*uc_priv
= dev_get_uclass_priv(dev
);
109 uc_priv
->bank_name
= "GPIO";
110 uc_priv
->gpio_count
= BCM2835_GPIO_COUNT
;
111 gpios
->reg
= (struct bcm2835_gpio_regs
*)plat
->base
;
113 /* We know we're spawned by the pinctrl driver */
114 gpios
->pinctrl
= dev
->parent
;
119 #if CONFIG_IS_ENABLED(OF_CONTROL)
120 static int bcm2835_gpio_ofdata_to_platdata(struct udevice
*dev
)
122 struct bcm2835_gpio_platdata
*plat
= dev_get_platdata(dev
);
125 addr
= devfdt_get_addr(dev
);
126 if (addr
== FDT_ADDR_T_NONE
)
134 U_BOOT_DRIVER(gpio_bcm2835
) = {
135 .name
= "gpio_bcm2835",
137 .ofdata_to_platdata
= of_match_ptr(bcm2835_gpio_ofdata_to_platdata
),
138 .platdata_auto_alloc_size
= sizeof(struct bcm2835_gpio_platdata
),
139 .ops
= &gpio_bcm2835_ops
,
140 .probe
= bcm2835_gpio_probe
,
141 .flags
= DM_FLAG_PRE_RELOC
,
142 .priv_auto_alloc_size
= sizeof(struct bcm2835_gpios
),