1 // SPDX-License-Identifier: GPL-2.0+
5 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
14 DECLARE_GLOBAL_DATA_PTR;
16 /* Register offsets */
17 #define GPIO_CONFIG_OFF(no) ((no) * 0x1000)
18 #define GPIO_IN_OUT_OFF(no) ((no) * 0x1000 + 0x4)
21 #define GPIO_OE_DISABLE (0x0 << 9)
22 #define GPIO_OE_ENABLE (0x1 << 9)
23 #define GPIO_OE_MASK (0x1 << 9)
25 /* GPIO_IN_OUT register shifts. */
29 struct msm_gpio_bank {
33 static int msm_gpio_direction_input(struct udevice *dev, unsigned int gpio)
35 struct msm_gpio_bank *priv = dev_get_priv(dev);
36 phys_addr_t reg = priv->base + GPIO_CONFIG_OFF(gpio);
39 clrsetbits_le32(reg, GPIO_OE_MASK, GPIO_OE_DISABLE);
44 static int msm_gpio_set_value(struct udevice *dev, unsigned gpio, int value)
46 struct msm_gpio_bank *priv = dev_get_priv(dev);
50 writel(value << GPIO_OUT, priv->base + GPIO_IN_OUT_OFF(gpio));
55 static int msm_gpio_direction_output(struct udevice *dev, unsigned gpio,
58 struct msm_gpio_bank *priv = dev_get_priv(dev);
59 phys_addr_t reg = priv->base + GPIO_CONFIG_OFF(gpio);
63 writel(value << GPIO_OUT, priv->base + GPIO_IN_OUT_OFF(gpio));
64 /* switch direction */
65 clrsetbits_le32(reg, GPIO_OE_MASK, GPIO_OE_ENABLE);
70 static int msm_gpio_get_value(struct udevice *dev, unsigned gpio)
72 struct msm_gpio_bank *priv = dev_get_priv(dev);
74 return !!(readl(priv->base + GPIO_IN_OUT_OFF(gpio)) >> GPIO_IN);
77 static int msm_gpio_get_function(struct udevice *dev, unsigned offset)
79 struct msm_gpio_bank *priv = dev_get_priv(dev);
81 if (readl(priv->base + GPIO_CONFIG_OFF(offset)) & GPIO_OE_ENABLE)
87 static const struct dm_gpio_ops gpio_msm_ops = {
88 .direction_input = msm_gpio_direction_input,
89 .direction_output = msm_gpio_direction_output,
90 .get_value = msm_gpio_get_value,
91 .set_value = msm_gpio_set_value,
92 .get_function = msm_gpio_get_function,
95 static int msm_gpio_probe(struct udevice *dev)
97 struct msm_gpio_bank *priv = dev_get_priv(dev);
99 priv->base = devfdt_get_addr(dev);
101 return priv->base == FDT_ADDR_T_NONE ? -EINVAL : 0;
104 static int msm_gpio_ofdata_to_platdata(struct udevice *dev)
106 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
108 uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
110 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev),
111 "gpio-bank-name", NULL);
112 if (uc_priv->bank_name == NULL)
113 uc_priv->bank_name = "soc";
118 static const struct udevice_id msm_gpio_ids[] = {
119 { .compatible = "qcom,msm8916-pinctrl" },
120 { .compatible = "qcom,apq8016-pinctrl" },
124 U_BOOT_DRIVER(gpio_msm) = {
127 .of_match = msm_gpio_ids,
128 .ofdata_to_platdata = msm_gpio_ofdata_to_platdata,
129 .probe = msm_gpio_probe,
130 .ops = &gpio_msm_ops,
131 .priv_auto_alloc_size = sizeof(struct msm_gpio_bank),