1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2016 Freescale Semiconductor, Inc.
5 * RGPIO2P driver for the Freescale i.MX7ULP.
16 enum imx_rgpio2p_direction {
17 IMX_RGPIO2P_DIRECTION_IN,
18 IMX_RGPIO2P_DIRECTION_OUT,
21 #define GPIO_PER_BANK 32
23 struct imx_rgpio2p_data {
24 struct gpio_regs *regs;
27 struct imx_rgpio2p_plat {
29 struct gpio_regs *regs;
32 static int imx_rgpio2p_is_output(struct gpio_regs *regs, int offset)
36 val = readl(®s->gpio_pddr);
38 return val & (1 << offset) ? 1 : 0;
41 static void imx_rgpio2p_bank_direction(struct gpio_regs *regs, int offset,
42 enum imx_rgpio2p_direction direction)
46 l = readl(®s->gpio_pddr);
49 case IMX_RGPIO2P_DIRECTION_OUT:
52 case IMX_RGPIO2P_DIRECTION_IN:
55 writel(l, ®s->gpio_pddr);
58 static void imx_rgpio2p_bank_set_value(struct gpio_regs *regs, int offset,
62 writel((1 << offset), ®s->gpio_psor);
64 writel((1 << offset), ®s->gpio_pcor);
67 static int imx_rgpio2p_bank_get_value(struct gpio_regs *regs, int offset)
69 return (readl(®s->gpio_pdir) >> offset) & 0x01;
72 static int imx_rgpio2p_direction_input(struct udevice *dev, unsigned offset)
74 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
76 /* Configure GPIO direction as input. */
77 imx_rgpio2p_bank_direction(bank->regs, offset, IMX_RGPIO2P_DIRECTION_IN);
82 static int imx_rgpio2p_direction_output(struct udevice *dev, unsigned offset,
85 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
87 /* Configure GPIO output value. */
88 imx_rgpio2p_bank_set_value(bank->regs, offset, value);
90 /* Configure GPIO direction as output. */
91 imx_rgpio2p_bank_direction(bank->regs, offset, IMX_RGPIO2P_DIRECTION_OUT);
96 static int imx_rgpio2p_get_value(struct udevice *dev, unsigned offset)
98 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
100 return imx_rgpio2p_bank_get_value(bank->regs, offset);
103 static int imx_rgpio2p_set_value(struct udevice *dev, unsigned offset,
106 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
108 imx_rgpio2p_bank_set_value(bank->regs, offset, value);
113 static int imx_rgpio2p_get_function(struct udevice *dev, unsigned offset)
115 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
117 /* GPIOF_FUNC is not implemented yet */
118 if (imx_rgpio2p_is_output(bank->regs, offset))
124 static const struct dm_gpio_ops imx_rgpio2p_ops = {
125 .direction_input = imx_rgpio2p_direction_input,
126 .direction_output = imx_rgpio2p_direction_output,
127 .get_value = imx_rgpio2p_get_value,
128 .set_value = imx_rgpio2p_set_value,
129 .get_function = imx_rgpio2p_get_function,
132 static int imx_rgpio2p_probe(struct udevice *dev)
134 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
135 struct imx_rgpio2p_plat *plat = dev_get_platdata(dev);
136 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
140 banknum = plat->bank_index;
141 sprintf(name, "GPIO%d_", banknum + 1);
145 uc_priv->bank_name = str;
146 uc_priv->gpio_count = GPIO_PER_BANK;
147 bank->regs = plat->regs;
152 static int imx_rgpio2p_bind(struct udevice *dev)
154 struct imx_rgpio2p_plat *plat = dev->platdata;
158 * If platdata already exsits, directly return.
159 * Actually only when DT is not supported, platdata
160 * is statically initialized in U_BOOT_DEVICES.Here
166 addr = devfdt_get_addr_index(dev, 1);
167 if (addr == FDT_ADDR_T_NONE)
172 * When every board is converted to driver model and DT is supported,
173 * this can be done by auto-alloc feature, but not using calloc
174 * to alloc memory for platdata.
176 * For example imx_rgpio2p_plat uses platform data rather than device
179 * NOTE: DO NOT COPY this code if you are using device tree.
181 plat = calloc(1, sizeof(*plat));
185 plat->regs = (struct gpio_regs *)addr;
186 plat->bank_index = dev->req_seq;
187 dev->platdata = plat;
193 static const struct udevice_id imx_rgpio2p_ids[] = {
194 { .compatible = "fsl,imx7ulp-gpio" },
198 U_BOOT_DRIVER(imx_rgpio2p) = {
199 .name = "imx_rgpio2p",
201 .ops = &imx_rgpio2p_ops,
202 .probe = imx_rgpio2p_probe,
203 .priv_auto_alloc_size = sizeof(struct imx_rgpio2p_plat),
204 .of_match = imx_rgpio2p_ids,
205 .bind = imx_rgpio2p_bind,
208 #if !CONFIG_IS_ENABLED(OF_CONTROL)
209 static const struct imx_rgpio2p_plat imx_plat[] = {
210 { 0, (struct gpio_regs *)RGPIO2P_GPIO1_BASE_ADDR },
211 { 1, (struct gpio_regs *)RGPIO2P_GPIO2_BASE_ADDR },
212 { 2, (struct gpio_regs *)RGPIO2P_GPIO3_BASE_ADDR },
213 { 3, (struct gpio_regs *)RGPIO2P_GPIO4_BASE_ADDR },
214 { 4, (struct gpio_regs *)RGPIO2P_GPIO5_BASE_ADDR },
215 { 5, (struct gpio_regs *)RGPIO2P_GPIO6_BASE_ADDR },
218 U_BOOT_DEVICES(imx_rgpio2ps) = {
219 { "imx_rgpio2p", &imx_plat[0] },
220 { "imx_rgpio2p", &imx_plat[1] },
221 { "imx_rgpio2p", &imx_plat[2] },
222 { "imx_rgpio2p", &imx_plat[3] },
223 { "imx_rgpio2p", &imx_plat[4] },
224 { "imx_rgpio2p", &imx_plat[5] },