Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 704d9a6 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Stefan Roese <sr@denx.de> |
Stefan Roese | 704d9a6 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Pali Rohár | 7d8bb89 | 2022-07-25 13:56:12 +0200 | [diff] [blame] | 8 | #include <dm/pinctrl.h> |
Stefan Roese | 704d9a6 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 9 | #include <asm/gpio.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <errno.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Stefan Roese | 704d9a6 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 13 | |
Stefan Roese | 704d9a6 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 14 | #define MVEBU_GPIOS_PER_BANK 32 |
| 15 | |
| 16 | struct 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 | |
| 24 | struct mvebu_gpio_priv { |
| 25 | struct mvebu_gpio_regs *regs; |
Pali Rohár | da76996 | 2022-07-25 13:56:14 +0200 | [diff] [blame] | 26 | char name[sizeof("mvebuX_")]; |
Stefan Roese | 704d9a6 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | static 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(®s->io_conf, BIT(gpio)); |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static 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 Roese | b23005c | 2016-04-07 07:41:00 +0200 | [diff] [blame] | 45 | if (value) |
| 46 | setbits_le32(®s->data_out, BIT(gpio)); |
| 47 | else |
| 48 | clrbits_le32(®s->data_out, BIT(gpio)); |
Stefan Roese | 704d9a6 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 49 | clrbits_le32(®s->io_conf, BIT(gpio)); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static 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(®s->io_conf) & BIT(gpio); |
| 61 | if (val) |
| 62 | return GPIOF_INPUT; |
| 63 | else |
| 64 | return GPIOF_OUTPUT; |
| 65 | } |
| 66 | |
| 67 | static 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(®s->data_out, BIT(gpio)); |
| 75 | else |
| 76 | clrbits_le32(®s->data_out, BIT(gpio)); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static 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(®s->data_in) & BIT(gpio)); |
| 87 | } |
| 88 | |
| 89 | static 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 Yamada | 8613c8d | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 94 | priv->regs = dev_read_addr_ptr(dev); |
Pali Rohár | dc986c6 | 2022-07-25 13:56:13 +0200 | [diff] [blame] | 95 | uc_priv->gpio_count = dev_read_u32_default(dev, "ngpios", MVEBU_GPIOS_PER_BANK); |
Pali Rohár | da76996 | 2022-07-25 13:56:14 +0200 | [diff] [blame] | 96 | sprintf(priv->name, "mvebu%d_", dev_seq(dev)); |
Stefan Roese | 704d9a6 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 97 | uc_priv->bank_name = priv->name; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static const struct dm_gpio_ops mvebu_gpio_ops = { |
Pali Rohár | 7d8bb89 | 2022-07-25 13:56:12 +0200 | [diff] [blame] | 103 | #if CONFIG_IS_ENABLED(PINCTRL_ARMADA_38X) |
| 104 | .request = pinctrl_gpio_request, |
| 105 | .rfree = pinctrl_gpio_free, |
| 106 | #endif |
Stefan Roese | 704d9a6 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 107 | .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 | |
| 114 | static const struct udevice_id mvebu_gpio_ids[] = { |
| 115 | { .compatible = "marvell,orion-gpio" }, |
| 116 | { } |
| 117 | }; |
| 118 | |
| 119 | U_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 Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 125 | .priv_auto = sizeof(struct mvebu_gpio_priv), |
Stefan Roese | 704d9a6 | 2016-02-12 13:46:50 +0100 | [diff] [blame] | 126 | }; |