blob: f706a6dfa4fed81cf7924074b420367f868f1cdb [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese704d9a62016-02-12 13:46:50 +01002/*
3 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
Stefan Roese704d9a62016-02-12 13:46:50 +01004 */
5
6#include <common.h>
7#include <dm.h>
Pali Rohár7d8bb892022-07-25 13:56:12 +02008#include <dm/pinctrl.h>
Stefan Roese704d9a62016-02-12 13:46:50 +01009#include <asm/gpio.h>
10#include <asm/io.h>
11#include <errno.h>
Simon Glasscd93d622020-05-10 11:40:13 -060012#include <linux/bitops.h>
Stefan Roese704d9a62016-02-12 13:46:50 +010013
Stefan Roese704d9a62016-02-12 13:46:50 +010014#define MVEBU_GPIOS_PER_BANK 32
15
16struct mvebu_gpio_regs {
17 u32 data_out;
18 u32 io_conf;
19 u32 blink_en;
20 u32 in_pol;
21 u32 data_in;
22};
23
24struct mvebu_gpio_priv {
25 struct mvebu_gpio_regs *regs;
Pali Rohárda769962022-07-25 13:56:14 +020026 char name[sizeof("mvebuX_")];
Stefan Roese704d9a62016-02-12 13:46:50 +010027};
28
29static int mvebu_gpio_direction_input(struct udevice *dev, unsigned int gpio)
30{
31 struct mvebu_gpio_priv *priv = dev_get_priv(dev);
32 struct mvebu_gpio_regs *regs = priv->regs;
33
34 setbits_le32(&regs->io_conf, BIT(gpio));
35
36 return 0;
37}
38
39static int mvebu_gpio_direction_output(struct udevice *dev, unsigned gpio,
40 int value)
41{
42 struct mvebu_gpio_priv *priv = dev_get_priv(dev);
43 struct mvebu_gpio_regs *regs = priv->regs;
44
Stefan Roeseb23005c2016-04-07 07:41:00 +020045 if (value)
46 setbits_le32(&regs->data_out, BIT(gpio));
47 else
48 clrbits_le32(&regs->data_out, BIT(gpio));
Stefan Roese704d9a62016-02-12 13:46:50 +010049 clrbits_le32(&regs->io_conf, BIT(gpio));
50
51 return 0;
52}
53
54static int mvebu_gpio_get_function(struct udevice *dev, unsigned gpio)
55{
56 struct mvebu_gpio_priv *priv = dev_get_priv(dev);
57 struct mvebu_gpio_regs *regs = priv->regs;
58 u32 val;
59
60 val = readl(&regs->io_conf) & BIT(gpio);
61 if (val)
62 return GPIOF_INPUT;
63 else
64 return GPIOF_OUTPUT;
65}
66
67static int mvebu_gpio_set_value(struct udevice *dev, unsigned gpio,
68 int value)
69{
70 struct mvebu_gpio_priv *priv = dev_get_priv(dev);
71 struct mvebu_gpio_regs *regs = priv->regs;
72
73 if (value)
74 setbits_le32(&regs->data_out, BIT(gpio));
75 else
76 clrbits_le32(&regs->data_out, BIT(gpio));
77
78 return 0;
79}
80
81static int mvebu_gpio_get_value(struct udevice *dev, unsigned gpio)
82{
83 struct mvebu_gpio_priv *priv = dev_get_priv(dev);
84 struct mvebu_gpio_regs *regs = priv->regs;
85
86 return !!(readl(&regs->data_in) & BIT(gpio));
87}
88
89static int mvebu_gpio_probe(struct udevice *dev)
90{
91 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
92 struct mvebu_gpio_priv *priv = dev_get_priv(dev);
93
Masahiro Yamada8613c8d2020-07-17 14:36:46 +090094 priv->regs = dev_read_addr_ptr(dev);
Pali Rohárdc986c62022-07-25 13:56:13 +020095 uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios", MVEBU_GPIOS_PER_BANK);
Pali Rohárda769962022-07-25 13:56:14 +020096 sprintf(priv->name, "mvebu%d_", dev_seq(dev));
Stefan Roese704d9a62016-02-12 13:46:50 +010097 uc_priv->bank_name = priv->name;
98
99 return 0;
100}
101
102static const struct dm_gpio_ops mvebu_gpio_ops = {
Pali Rohár7d8bb892022-07-25 13:56:12 +0200103#if CONFIG_IS_ENABLED(PINCTRL_ARMADA_38X)
104 .request = pinctrl_gpio_request,
105 .rfree = pinctrl_gpio_free,
106#endif
Stefan Roese704d9a62016-02-12 13:46:50 +0100107 .direction_input = mvebu_gpio_direction_input,
108 .direction_output = mvebu_gpio_direction_output,
109 .get_function = mvebu_gpio_get_function,
110 .get_value = mvebu_gpio_get_value,
111 .set_value = mvebu_gpio_set_value,
112};
113
114static const struct udevice_id mvebu_gpio_ids[] = {
115 { .compatible = "marvell,orion-gpio" },
116 { }
117};
118
119U_BOOT_DRIVER(gpio_mvebu) = {
120 .name = "gpio_mvebu",
121 .id = UCLASS_GPIO,
122 .of_match = mvebu_gpio_ids,
123 .ops = &mvebu_gpio_ops,
124 .probe = mvebu_gpio_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700125 .priv_auto = sizeof(struct mvebu_gpio_priv),
Stefan Roese704d9a62016-02-12 13:46:50 +0100126};