Sergei Antonov | 03027a5 | 2022-09-12 13:09:15 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Faraday Technology's FTGPIO010 controller. |
| 4 | */ |
| 5 | |
Sergei Antonov | 03027a5 | 2022-09-12 13:09:15 +0300 | [diff] [blame] | 6 | #include <dm.h> |
| 7 | #include <asm/io.h> |
| 8 | #include <asm/gpio.h> |
| 9 | |
| 10 | struct ftgpio010_regs { |
| 11 | u32 out; |
| 12 | u32 in; |
| 13 | u32 direction; // 1 - output |
| 14 | u32 reserved; |
| 15 | u32 set; |
| 16 | u32 clear; |
| 17 | }; |
| 18 | |
| 19 | struct ftgpio010_plat { |
| 20 | struct ftgpio010_regs __iomem *regs; |
| 21 | }; |
| 22 | |
| 23 | static int ftgpio010_direction_input(struct udevice *dev, unsigned int pin) |
| 24 | { |
| 25 | struct ftgpio010_plat *plat = dev_get_plat(dev); |
| 26 | struct ftgpio010_regs *const regs = plat->regs; |
| 27 | |
| 28 | clrbits_le32(®s->direction, 1 << pin); |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | static int ftgpio010_direction_output(struct udevice *dev, unsigned int pin, |
| 33 | int val) |
| 34 | { |
| 35 | struct ftgpio010_plat *plat = dev_get_plat(dev); |
| 36 | struct ftgpio010_regs *const regs = plat->regs; |
| 37 | |
| 38 | /* change the data first, then the direction. to avoid glitch */ |
| 39 | out_le32(val ? ®s->set : ®s->clear, 1 << pin); |
| 40 | setbits_le32(®s->direction, 1 << pin); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static int ftgpio010_get_value(struct udevice *dev, unsigned int pin) |
| 46 | { |
| 47 | struct ftgpio010_plat *plat = dev_get_plat(dev); |
| 48 | struct ftgpio010_regs *const regs = plat->regs; |
| 49 | |
| 50 | return in_le32(®s->in) >> pin & 1; |
| 51 | } |
| 52 | |
| 53 | static int ftgpio010_set_value(struct udevice *dev, unsigned int pin, int val) |
| 54 | { |
| 55 | struct ftgpio010_plat *plat = dev_get_plat(dev); |
| 56 | struct ftgpio010_regs *const regs = plat->regs; |
| 57 | |
| 58 | out_le32(val ? ®s->set : ®s->clear, 1 << pin); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int ftgpio010_get_function(struct udevice *dev, unsigned int pin) |
| 63 | { |
| 64 | struct ftgpio010_plat *plat = dev_get_plat(dev); |
| 65 | struct ftgpio010_regs *const regs = plat->regs; |
| 66 | |
| 67 | if (in_le32(®s->direction) >> pin & 1) |
| 68 | return GPIOF_OUTPUT; |
| 69 | return GPIOF_INPUT; |
| 70 | } |
| 71 | |
| 72 | static int ftgpio010_probe(struct udevice *dev) |
| 73 | { |
| 74 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 75 | |
| 76 | uc_priv->gpio_count = ofnode_read_u32_default(dev_ofnode(dev), |
| 77 | "nr-gpios", 32); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int ftgpio010_of_to_plat(struct udevice *dev) |
| 82 | { |
| 83 | struct ftgpio010_plat *plat = dev_get_plat(dev); |
| 84 | |
| 85 | plat->regs = dev_read_addr_ptr(dev); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static const struct dm_gpio_ops ftgpio010_ops = { |
| 90 | .direction_input = ftgpio010_direction_input, |
| 91 | .direction_output = ftgpio010_direction_output, |
| 92 | .get_value = ftgpio010_get_value, |
| 93 | .set_value = ftgpio010_set_value, |
| 94 | .get_function = ftgpio010_get_function, |
| 95 | }; |
| 96 | |
| 97 | static const struct udevice_id ftgpio010_ids[] = { |
| 98 | { .compatible = "faraday,ftgpio010" }, |
| 99 | { } |
| 100 | }; |
| 101 | |
| 102 | U_BOOT_DRIVER(ftgpio010) = { |
| 103 | .name = "ftgpio010", |
| 104 | .id = UCLASS_GPIO, |
| 105 | .of_match = ftgpio010_ids, |
| 106 | .ops = &ftgpio010_ops, |
| 107 | .of_to_plat = ftgpio010_of_to_plat, |
| 108 | .plat_auto = sizeof(struct ftgpio010_plat), |
| 109 | .probe = ftgpio010_probe, |
| 110 | }; |