2 * Copyright 2016 Freescale Semiconductor, Inc.
4 * RGPIO2P driver for the Freescale i.MX7ULP.
6 * SPDX-License-Identifier: GPL-2.0+
17 enum imx_rgpio2p_direction {
18 IMX_RGPIO2P_DIRECTION_IN,
19 IMX_RGPIO2P_DIRECTION_OUT,
22 #define GPIO_PER_BANK 32
24 struct imx_rgpio2p_data {
25 struct gpio_regs *regs;
28 struct imx_rgpio2p_plat {
30 struct gpio_regs *regs;
33 static int imx_rgpio2p_is_output(struct gpio_regs *regs, int offset)
37 val = readl(®s->gpio_pddr);
39 return val & (1 << offset) ? 1 : 0;
42 static void imx_rgpio2p_bank_direction(struct gpio_regs *regs, int offset,
43 enum imx_rgpio2p_direction direction)
47 l = readl(®s->gpio_pddr);
50 case IMX_RGPIO2P_DIRECTION_OUT:
53 case IMX_RGPIO2P_DIRECTION_IN:
56 writel(l, ®s->gpio_pddr);
59 static void imx_rgpio2p_bank_set_value(struct gpio_regs *regs, int offset,
63 writel((1 << offset), ®s->gpio_psor);
65 writel((1 << offset), ®s->gpio_pcor);
68 static int imx_rgpio2p_bank_get_value(struct gpio_regs *regs, int offset)
70 return (readl(®s->gpio_pdir) >> offset) & 0x01;
73 static int imx_rgpio2p_direction_input(struct udevice *dev, unsigned offset)
75 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
77 /* Configure GPIO direction as input. */
78 imx_rgpio2p_bank_direction(bank->regs, offset, IMX_RGPIO2P_DIRECTION_IN);
83 static int imx_rgpio2p_direction_output(struct udevice *dev, unsigned offset,
86 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
88 /* Configure GPIO output value. */
89 imx_rgpio2p_bank_set_value(bank->regs, offset, value);
91 /* Configure GPIO direction as output. */
92 imx_rgpio2p_bank_direction(bank->regs, offset, IMX_RGPIO2P_DIRECTION_OUT);
97 static int imx_rgpio2p_get_value(struct udevice *dev, unsigned offset)
99 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
101 return imx_rgpio2p_bank_get_value(bank->regs, offset);
104 static int imx_rgpio2p_set_value(struct udevice *dev, unsigned offset,
107 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
109 imx_rgpio2p_bank_set_value(bank->regs, offset, value);
114 static int imx_rgpio2p_get_function(struct udevice *dev, unsigned offset)
116 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
118 /* GPIOF_FUNC is not implemented yet */
119 if (imx_rgpio2p_is_output(bank->regs, offset))
125 static const struct dm_gpio_ops imx_rgpio2p_ops = {
126 .direction_input = imx_rgpio2p_direction_input,
127 .direction_output = imx_rgpio2p_direction_output,
128 .get_value = imx_rgpio2p_get_value,
129 .set_value = imx_rgpio2p_set_value,
130 .get_function = imx_rgpio2p_get_function,
133 static int imx_rgpio2p_probe(struct udevice *dev)
135 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
136 struct imx_rgpio2p_plat *plat = dev_get_platdata(dev);
137 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
141 banknum = plat->bank_index;
142 sprintf(name, "GPIO%d_", banknum + 1);
146 uc_priv->bank_name = str;
147 uc_priv->gpio_count = GPIO_PER_BANK;
148 bank->regs = plat->regs;
153 static int imx_rgpio2p_bind(struct udevice *dev)
155 struct imx_rgpio2p_plat *plat = dev->platdata;
159 * If platdata already exsits, directly return.
160 * Actually only when DT is not supported, platdata
161 * is statically initialized in U_BOOT_DEVICES.Here
167 addr = devfdt_get_addr_index(dev, 1);
168 if (addr == FDT_ADDR_T_NONE)
173 * When every board is converted to driver model and DT is supported,
174 * this can be done by auto-alloc feature, but not using calloc
175 * to alloc memory for platdata.
177 * For example imx_rgpio2p_plat uses platform data rather than device
180 * NOTE: DO NOT COPY this code if you are using device tree.
182 plat = calloc(1, sizeof(*plat));
186 plat->regs = (struct gpio_regs *)addr;
187 plat->bank_index = dev->req_seq;
188 dev->platdata = plat;
194 static const struct udevice_id imx_rgpio2p_ids[] = {
195 { .compatible = "fsl,imx7ulp-gpio" },
199 U_BOOT_DRIVER(imx_rgpio2p) = {
200 .name = "imx_rgpio2p",
202 .ops = &imx_rgpio2p_ops,
203 .probe = imx_rgpio2p_probe,
204 .priv_auto_alloc_size = sizeof(struct imx_rgpio2p_plat),
205 .of_match = imx_rgpio2p_ids,
206 .bind = imx_rgpio2p_bind,
209 #if !CONFIG_IS_ENABLED(OF_CONTROL)
210 static const struct imx_rgpio2p_plat imx_plat[] = {
211 { 0, (struct gpio_regs *)RGPIO2P_GPIO1_BASE_ADDR },
212 { 1, (struct gpio_regs *)RGPIO2P_GPIO2_BASE_ADDR },
213 { 2, (struct gpio_regs *)RGPIO2P_GPIO3_BASE_ADDR },
214 { 3, (struct gpio_regs *)RGPIO2P_GPIO4_BASE_ADDR },
215 { 4, (struct gpio_regs *)RGPIO2P_GPIO5_BASE_ADDR },
216 { 5, (struct gpio_regs *)RGPIO2P_GPIO6_BASE_ADDR },
219 U_BOOT_DEVICES(imx_rgpio2ps) = {
220 { "imx_rgpio2p", &imx_plat[0] },
221 { "imx_rgpio2p", &imx_plat[1] },
222 { "imx_rgpio2p", &imx_plat[2] },
223 { "imx_rgpio2p", &imx_plat[3] },
224 { "imx_rgpio2p", &imx_plat[4] },
225 { "imx_rgpio2p", &imx_plat[5] },