Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Qualcomm GPIO driver |
| 4 | * |
| 5 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame^] | 11 | #include <asm/global_data.h> |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 12 | #include <asm/gpio.h> |
| 13 | #include <asm/io.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | /* Register offsets */ |
| 18 | #define GPIO_CONFIG_OFF(no) ((no) * 0x1000) |
| 19 | #define GPIO_IN_OUT_OFF(no) ((no) * 0x1000 + 0x4) |
| 20 | |
| 21 | /* OE */ |
| 22 | #define GPIO_OE_DISABLE (0x0 << 9) |
| 23 | #define GPIO_OE_ENABLE (0x1 << 9) |
| 24 | #define GPIO_OE_MASK (0x1 << 9) |
| 25 | |
| 26 | /* GPIO_IN_OUT register shifts. */ |
| 27 | #define GPIO_IN 0 |
| 28 | #define GPIO_OUT 1 |
| 29 | |
| 30 | struct msm_gpio_bank { |
| 31 | phys_addr_t base; |
| 32 | }; |
| 33 | |
| 34 | static int msm_gpio_direction_input(struct udevice *dev, unsigned int gpio) |
| 35 | { |
| 36 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
| 37 | phys_addr_t reg = priv->base + GPIO_CONFIG_OFF(gpio); |
| 38 | |
| 39 | /* Disable OE bit */ |
| 40 | clrsetbits_le32(reg, GPIO_OE_MASK, GPIO_OE_DISABLE); |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static int msm_gpio_set_value(struct udevice *dev, unsigned gpio, int value) |
| 46 | { |
| 47 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
| 48 | |
| 49 | value = !!value; |
| 50 | /* set value */ |
| 51 | writel(value << GPIO_OUT, priv->base + GPIO_IN_OUT_OFF(gpio)); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int msm_gpio_direction_output(struct udevice *dev, unsigned gpio, |
| 57 | int value) |
| 58 | { |
| 59 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
| 60 | phys_addr_t reg = priv->base + GPIO_CONFIG_OFF(gpio); |
| 61 | |
| 62 | value = !!value; |
| 63 | /* set value */ |
| 64 | writel(value << GPIO_OUT, priv->base + GPIO_IN_OUT_OFF(gpio)); |
| 65 | /* switch direction */ |
| 66 | clrsetbits_le32(reg, GPIO_OE_MASK, GPIO_OE_ENABLE); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static int msm_gpio_get_value(struct udevice *dev, unsigned gpio) |
| 72 | { |
| 73 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
| 74 | |
| 75 | return !!(readl(priv->base + GPIO_IN_OUT_OFF(gpio)) >> GPIO_IN); |
| 76 | } |
| 77 | |
| 78 | static int msm_gpio_get_function(struct udevice *dev, unsigned offset) |
| 79 | { |
| 80 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
| 81 | |
| 82 | if (readl(priv->base + GPIO_CONFIG_OFF(offset)) & GPIO_OE_ENABLE) |
| 83 | return GPIOF_OUTPUT; |
| 84 | |
| 85 | return GPIOF_INPUT; |
| 86 | } |
| 87 | |
| 88 | static const struct dm_gpio_ops gpio_msm_ops = { |
| 89 | .direction_input = msm_gpio_direction_input, |
| 90 | .direction_output = msm_gpio_direction_output, |
| 91 | .get_value = msm_gpio_get_value, |
| 92 | .set_value = msm_gpio_set_value, |
| 93 | .get_function = msm_gpio_get_function, |
| 94 | }; |
| 95 | |
| 96 | static int msm_gpio_probe(struct udevice *dev) |
| 97 | { |
| 98 | struct msm_gpio_bank *priv = dev_get_priv(dev); |
| 99 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 100 | priv->base = dev_read_addr(dev); |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 101 | |
| 102 | return priv->base == FDT_ADDR_T_NONE ? -EINVAL : 0; |
| 103 | } |
| 104 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 105 | static int msm_gpio_of_to_plat(struct udevice *dev) |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 106 | { |
| 107 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 108 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 109 | uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 110 | "gpio-count", 0); |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 111 | uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 112 | "gpio-bank-name", NULL); |
| 113 | if (uc_priv->bank_name == NULL) |
| 114 | uc_priv->bank_name = "soc"; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static const struct udevice_id msm_gpio_ids[] = { |
| 120 | { .compatible = "qcom,msm8916-pinctrl" }, |
| 121 | { .compatible = "qcom,apq8016-pinctrl" }, |
Robert Marko | 0c7eb6e | 2020-07-06 10:37:56 +0200 | [diff] [blame] | 122 | { .compatible = "qcom,ipq4019-pinctrl" }, |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 123 | { } |
| 124 | }; |
| 125 | |
| 126 | U_BOOT_DRIVER(gpio_msm) = { |
| 127 | .name = "gpio_msm", |
| 128 | .id = UCLASS_GPIO, |
| 129 | .of_match = msm_gpio_ids, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 130 | .of_to_plat = msm_gpio_of_to_plat, |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 131 | .probe = msm_gpio_probe, |
| 132 | .ops = &gpio_msm_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 133 | .priv_auto = sizeof(struct msm_gpio_bank), |
Mateusz Kulikowski | 81a87e1 | 2016-03-31 23:12:15 +0200 | [diff] [blame] | 134 | }; |