Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Stefan Roese <sr@denx.de> |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 6 | #include <dm.h> |
Pali Rohár | 3a06691 | 2022-07-25 13:56:12 +0200 | [diff] [blame] | 7 | #include <dm/pinctrl.h> |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 8 | #include <asm/gpio.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <errno.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 12 | |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 13 | #define MVEBU_GPIOS_PER_BANK 32 |
| 14 | |
| 15 | struct mvebu_gpio_regs { |
| 16 | u32 data_out; |
| 17 | u32 io_conf; |
| 18 | u32 blink_en; |
| 19 | u32 in_pol; |
| 20 | u32 data_in; |
| 21 | }; |
| 22 | |
| 23 | struct mvebu_gpio_priv { |
| 24 | struct mvebu_gpio_regs *regs; |
Pali Rohár | 6eada42 | 2022-07-25 13:56:14 +0200 | [diff] [blame] | 25 | char name[sizeof("mvebuX_")]; |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | static int mvebu_gpio_direction_input(struct udevice *dev, unsigned int gpio) |
| 29 | { |
| 30 | struct mvebu_gpio_priv *priv = dev_get_priv(dev); |
| 31 | struct mvebu_gpio_regs *regs = priv->regs; |
| 32 | |
| 33 | setbits_le32(®s->io_conf, BIT(gpio)); |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static int mvebu_gpio_direction_output(struct udevice *dev, unsigned gpio, |
| 39 | int value) |
| 40 | { |
| 41 | struct mvebu_gpio_priv *priv = dev_get_priv(dev); |
| 42 | struct mvebu_gpio_regs *regs = priv->regs; |
| 43 | |
Stefan Roese | 7c96a77 | 2016-04-07 07:41:00 +0200 | [diff] [blame] | 44 | if (value) |
| 45 | setbits_le32(®s->data_out, BIT(gpio)); |
| 46 | else |
| 47 | clrbits_le32(®s->data_out, BIT(gpio)); |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 48 | clrbits_le32(®s->io_conf, BIT(gpio)); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int mvebu_gpio_get_function(struct udevice *dev, unsigned gpio) |
| 54 | { |
| 55 | struct mvebu_gpio_priv *priv = dev_get_priv(dev); |
| 56 | struct mvebu_gpio_regs *regs = priv->regs; |
| 57 | u32 val; |
| 58 | |
| 59 | val = readl(®s->io_conf) & BIT(gpio); |
| 60 | if (val) |
| 61 | return GPIOF_INPUT; |
| 62 | else |
| 63 | return GPIOF_OUTPUT; |
| 64 | } |
| 65 | |
| 66 | static int mvebu_gpio_set_value(struct udevice *dev, unsigned gpio, |
| 67 | int value) |
| 68 | { |
| 69 | struct mvebu_gpio_priv *priv = dev_get_priv(dev); |
| 70 | struct mvebu_gpio_regs *regs = priv->regs; |
| 71 | |
| 72 | if (value) |
| 73 | setbits_le32(®s->data_out, BIT(gpio)); |
| 74 | else |
| 75 | clrbits_le32(®s->data_out, BIT(gpio)); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int mvebu_gpio_get_value(struct udevice *dev, unsigned gpio) |
| 81 | { |
| 82 | struct mvebu_gpio_priv *priv = dev_get_priv(dev); |
| 83 | struct mvebu_gpio_regs *regs = priv->regs; |
| 84 | |
| 85 | return !!(readl(®s->data_in) & BIT(gpio)); |
| 86 | } |
| 87 | |
| 88 | static int mvebu_gpio_probe(struct udevice *dev) |
| 89 | { |
| 90 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 91 | struct mvebu_gpio_priv *priv = dev_get_priv(dev); |
| 92 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 93 | priv->regs = dev_read_addr_ptr(dev); |
Pali Rohár | 26490a5 | 2022-07-25 13:56:13 +0200 | [diff] [blame] | 94 | uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios", MVEBU_GPIOS_PER_BANK); |
Pali Rohár | 6eada42 | 2022-07-25 13:56:14 +0200 | [diff] [blame] | 95 | sprintf(priv->name, "mvebu%d_", dev_seq(dev)); |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 96 | uc_priv->bank_name = priv->name; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static const struct dm_gpio_ops mvebu_gpio_ops = { |
Pali Rohár | 3a06691 | 2022-07-25 13:56:12 +0200 | [diff] [blame] | 102 | #if CONFIG_IS_ENABLED(PINCTRL_ARMADA_38X) |
| 103 | .request = pinctrl_gpio_request, |
| 104 | .rfree = pinctrl_gpio_free, |
| 105 | #endif |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 106 | .direction_input = mvebu_gpio_direction_input, |
| 107 | .direction_output = mvebu_gpio_direction_output, |
| 108 | .get_function = mvebu_gpio_get_function, |
| 109 | .get_value = mvebu_gpio_get_value, |
| 110 | .set_value = mvebu_gpio_set_value, |
| 111 | }; |
| 112 | |
| 113 | static const struct udevice_id mvebu_gpio_ids[] = { |
| 114 | { .compatible = "marvell,orion-gpio" }, |
| 115 | { } |
| 116 | }; |
| 117 | |
| 118 | U_BOOT_DRIVER(gpio_mvebu) = { |
| 119 | .name = "gpio_mvebu", |
| 120 | .id = UCLASS_GPIO, |
| 121 | .of_match = mvebu_gpio_ids, |
| 122 | .ops = &mvebu_gpio_ops, |
| 123 | .probe = mvebu_gpio_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 124 | .priv_auto = sizeof(struct mvebu_gpio_priv), |
Stefan Roese | e0e7042 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 125 | }; |