Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peter Griffin | 152f4898 | 2015-07-30 18:55:18 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Linaro |
| 4 | * Peter Griffin <peter.griffin@linaro.org> |
Peter Griffin | 152f4898 | 2015-07-30 18:55:18 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Peter Griffin | 152f4898 | 2015-07-30 18:55:18 +0100 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <asm/gpio.h> |
| 9 | #include <asm/io.h> |
| 10 | #include <errno.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Peter Griffin | 152f4898 | 2015-07-30 18:55:18 +0100 | [diff] [blame] | 12 | |
| 13 | static int hi6220_gpio_direction_input(struct udevice *dev, unsigned int gpio) |
| 14 | { |
| 15 | struct gpio_bank *bank = dev_get_priv(dev); |
| 16 | u8 data; |
| 17 | |
| 18 | data = readb(bank->base + HI6220_GPIO_DIR); |
| 19 | data &= ~(1 << gpio); |
| 20 | writeb(data, bank->base + HI6220_GPIO_DIR); |
| 21 | |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | static int hi6220_gpio_set_value(struct udevice *dev, unsigned gpio, |
| 26 | int value) |
| 27 | { |
| 28 | struct gpio_bank *bank = dev_get_priv(dev); |
| 29 | |
| 30 | writeb(!!value << gpio, bank->base + (BIT(gpio + 2))); |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | static int hi6220_gpio_direction_output(struct udevice *dev, unsigned gpio, |
| 35 | int value) |
| 36 | { |
| 37 | struct gpio_bank *bank = dev_get_priv(dev); |
| 38 | u8 data; |
| 39 | |
| 40 | data = readb(bank->base + HI6220_GPIO_DIR); |
| 41 | data |= 1 << gpio; |
| 42 | writeb(data, bank->base + HI6220_GPIO_DIR); |
| 43 | |
| 44 | hi6220_gpio_set_value(dev, gpio, value); |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static int hi6220_gpio_get_value(struct udevice *dev, unsigned gpio) |
| 50 | { |
| 51 | struct gpio_bank *bank = dev_get_priv(dev); |
| 52 | |
| 53 | return !!readb(bank->base + (BIT(gpio + 2))); |
| 54 | } |
| 55 | |
Peter Griffin | 152f4898 | 2015-07-30 18:55:18 +0100 | [diff] [blame] | 56 | static const struct dm_gpio_ops gpio_hi6220_ops = { |
| 57 | .direction_input = hi6220_gpio_direction_input, |
| 58 | .direction_output = hi6220_gpio_direction_output, |
| 59 | .get_value = hi6220_gpio_get_value, |
| 60 | .set_value = hi6220_gpio_set_value, |
| 61 | }; |
| 62 | |
| 63 | static int hi6220_gpio_probe(struct udevice *dev) |
| 64 | { |
| 65 | struct gpio_bank *bank = dev_get_priv(dev); |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 66 | struct hikey_gpio_plat *plat = dev_get_plat(dev); |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 67 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Peter Griffin | 152f4898 | 2015-07-30 18:55:18 +0100 | [diff] [blame] | 68 | char name[18], *str; |
| 69 | |
| 70 | sprintf(name, "GPIO%d_", plat->bank_index); |
| 71 | |
| 72 | str = strdup(name); |
| 73 | if (!str) |
| 74 | return -ENOMEM; |
| 75 | |
| 76 | uc_priv->bank_name = str; |
| 77 | uc_priv->gpio_count = HI6220_GPIO_PER_BANK; |
| 78 | |
| 79 | bank->base = (u8 *)plat->base; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | U_BOOT_DRIVER(gpio_hi6220) = { |
| 85 | .name = "gpio_hi6220", |
| 86 | .id = UCLASS_GPIO, |
| 87 | .ops = &gpio_hi6220_ops, |
| 88 | .probe = hi6220_gpio_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 89 | .priv_auto = sizeof(struct gpio_bank), |
Peter Griffin | 152f4898 | 2015-07-30 18:55:18 +0100 | [diff] [blame] | 90 | }; |