blob: fc1d418315c560f2e794fb383d3764d98d482b39 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fanc5f49862017-02-22 16:21:45 +08002/*
3 * Copyright 2016 Freescale Semiconductor, Inc.
4 *
5 * RGPIO2P driver for the Freescale i.MX7ULP.
Peng Fanc5f49862017-02-22 16:21:45 +08006 */
7
Peng Fanc5f49862017-02-22 16:21:45 +08008#include <dm.h>
9#include <errno.h>
10#include <fdtdec.h>
11#include <asm/gpio.h>
12#include <asm/io.h>
Simon Glass95588622020-12-22 19:30:28 -070013#include <dm/device-internal.h>
Peng Fanc5f49862017-02-22 16:21:45 +080014#include <malloc.h>
15
Peng Fanc5f49862017-02-22 16:21:45 +080016enum imx_rgpio2p_direction {
17 IMX_RGPIO2P_DIRECTION_IN,
18 IMX_RGPIO2P_DIRECTION_OUT,
19};
20
21#define GPIO_PER_BANK 32
22
Peng Fanbcdc6162024-04-12 22:24:51 +080023struct imx_rgpio2p_soc_data {
24 bool have_dual_base;
25};
26
27#define IMX8ULP_GPIO_BASE_OFF 0x40
28
Peng Fanc5f49862017-02-22 16:21:45 +080029struct imx_rgpio2p_data {
30 struct gpio_regs *regs;
31};
32
33struct imx_rgpio2p_plat {
34 int bank_index;
35 struct gpio_regs *regs;
36};
37
38static int imx_rgpio2p_is_output(struct gpio_regs *regs, int offset)
39{
40 u32 val;
41
42 val = readl(&regs->gpio_pddr);
43
44 return val & (1 << offset) ? 1 : 0;
45}
46
Christoph Fritzd76d9c82022-04-05 12:29:30 +020047static int imx_rgpio2p_bank_get_direction(struct gpio_regs *regs, int offset)
48{
49 if ((readl(&regs->gpio_pddr) >> offset) & 0x01)
50 return IMX_RGPIO2P_DIRECTION_OUT;
51
52 return IMX_RGPIO2P_DIRECTION_IN;
53}
54
Peng Fanc5f49862017-02-22 16:21:45 +080055static void imx_rgpio2p_bank_direction(struct gpio_regs *regs, int offset,
56 enum imx_rgpio2p_direction direction)
57{
58 u32 l;
59
60 l = readl(&regs->gpio_pddr);
61
62 switch (direction) {
63 case IMX_RGPIO2P_DIRECTION_OUT:
64 l |= 1 << offset;
65 break;
66 case IMX_RGPIO2P_DIRECTION_IN:
67 l &= ~(1 << offset);
68 }
69 writel(l, &regs->gpio_pddr);
70}
71
72static void imx_rgpio2p_bank_set_value(struct gpio_regs *regs, int offset,
73 int value)
74{
75 if (value)
76 writel((1 << offset), &regs->gpio_psor);
77 else
78 writel((1 << offset), &regs->gpio_pcor);
79}
80
81static int imx_rgpio2p_bank_get_value(struct gpio_regs *regs, int offset)
82{
Christoph Fritzd76d9c82022-04-05 12:29:30 +020083 if (imx_rgpio2p_bank_get_direction(regs, offset) ==
84 IMX_RGPIO2P_DIRECTION_IN)
85 return (readl(&regs->gpio_pdir) >> offset) & 0x01;
86
87 return (readl(&regs->gpio_pdor) >> offset) & 0x01;
Peng Fanc5f49862017-02-22 16:21:45 +080088}
89
90static int imx_rgpio2p_direction_input(struct udevice *dev, unsigned offset)
91{
92 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
93
94 /* Configure GPIO direction as input. */
95 imx_rgpio2p_bank_direction(bank->regs, offset, IMX_RGPIO2P_DIRECTION_IN);
96
97 return 0;
98}
99
100static int imx_rgpio2p_direction_output(struct udevice *dev, unsigned offset,
101 int value)
102{
103 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
104
105 /* Configure GPIO output value. */
106 imx_rgpio2p_bank_set_value(bank->regs, offset, value);
107
108 /* Configure GPIO direction as output. */
109 imx_rgpio2p_bank_direction(bank->regs, offset, IMX_RGPIO2P_DIRECTION_OUT);
110
111 return 0;
112}
113
114static int imx_rgpio2p_get_value(struct udevice *dev, unsigned offset)
115{
116 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
117
118 return imx_rgpio2p_bank_get_value(bank->regs, offset);
119}
120
121static int imx_rgpio2p_set_value(struct udevice *dev, unsigned offset,
122 int value)
123{
124 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
125
126 imx_rgpio2p_bank_set_value(bank->regs, offset, value);
127
128 return 0;
129}
130
131static int imx_rgpio2p_get_function(struct udevice *dev, unsigned offset)
132{
133 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
134
135 /* GPIOF_FUNC is not implemented yet */
136 if (imx_rgpio2p_is_output(bank->regs, offset))
137 return GPIOF_OUTPUT;
138 else
139 return GPIOF_INPUT;
140}
141
142static const struct dm_gpio_ops imx_rgpio2p_ops = {
143 .direction_input = imx_rgpio2p_direction_input,
144 .direction_output = imx_rgpio2p_direction_output,
145 .get_value = imx_rgpio2p_get_value,
146 .set_value = imx_rgpio2p_set_value,
147 .get_function = imx_rgpio2p_get_function,
148};
149
150static int imx_rgpio2p_probe(struct udevice *dev)
151{
152 struct imx_rgpio2p_data *bank = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700153 struct imx_rgpio2p_plat *plat = dev_get_plat(dev);
Peng Fanc5f49862017-02-22 16:21:45 +0800154 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
155 int banknum;
156 char name[18], *str;
157
158 banknum = plat->bank_index;
159 sprintf(name, "GPIO%d_", banknum + 1);
160 str = strdup(name);
161 if (!str)
162 return -ENOMEM;
163 uc_priv->bank_name = str;
164 uc_priv->gpio_count = GPIO_PER_BANK;
165 bank->regs = plat->regs;
166
167 return 0;
168}
169
170static int imx_rgpio2p_bind(struct udevice *dev)
171{
Simon Glass95588622020-12-22 19:30:28 -0700172 struct imx_rgpio2p_plat *plat = dev_get_plat(dev);
Peng Fanbcdc6162024-04-12 22:24:51 +0800173 struct imx_rgpio2p_soc_data *data =
174 (struct imx_rgpio2p_soc_data *)dev_get_driver_data(dev);
175 bool dual_base = data->have_dual_base;
Peng Fanc5f49862017-02-22 16:21:45 +0800176 fdt_addr_t addr;
177
178 /*
Simon Glass71fa5b42020-12-03 16:55:18 -0700179 * If plat already exsits, directly return.
180 * Actually only when DT is not supported, plat
Simon Glass1d8364a2020-12-28 20:34:54 -0700181 * is statically initialized in U_BOOT_DRVINFOS.Here
Peng Fanc5f49862017-02-22 16:21:45 +0800182 * will return.
183 */
184 if (plat)
185 return 0;
186
Peng Fanbcdc6162024-04-12 22:24:51 +0800187 /*
188 * Handle legacy compatible combinations which used two reg values
189 * for the i.MX8ULP and i.MX93.
190 */
191 if (device_is_compatible(dev, "fsl,imx7ulp-gpio") &&
192 (device_is_compatible(dev, "fsl,imx93-gpio") ||
193 (device_is_compatible(dev, "fsl,imx8ulp-gpio"))))
194 dual_base = true;
195
196 if (dual_base) {
197 addr = devfdt_get_addr_index(dev, 1);
198 if (addr == FDT_ADDR_T_NONE)
199 return -EINVAL;
200 } else {
201 addr = devfdt_get_addr_index(dev, 0);
202 if (addr == FDT_ADDR_T_NONE)
203 return -EINVAL;
204
205 addr += IMX8ULP_GPIO_BASE_OFF;
206 }
Peng Fanc5f49862017-02-22 16:21:45 +0800207
208 /*
209 * TODO:
210 * When every board is converted to driver model and DT is supported,
211 * this can be done by auto-alloc feature, but not using calloc
Simon Glass71fa5b42020-12-03 16:55:18 -0700212 * to alloc memory for plat.
Simon Glass87f4cb72017-09-17 16:54:52 -0600213 *
214 * For example imx_rgpio2p_plat uses platform data rather than device
215 * tree.
216 *
217 * NOTE: DO NOT COPY this code if you are using device tree.
Peng Fanc5f49862017-02-22 16:21:45 +0800218 */
219 plat = calloc(1, sizeof(*plat));
220 if (!plat)
221 return -ENOMEM;
222
223 plat->regs = (struct gpio_regs *)addr;
Simon Glassb4db3722020-12-16 21:20:24 -0700224 plat->bank_index = dev_seq(dev);
Simon Glass95588622020-12-22 19:30:28 -0700225 dev_set_plat(dev, plat);
Peng Fanc5f49862017-02-22 16:21:45 +0800226
227 return 0;
228}
229
Peng Fanbcdc6162024-04-12 22:24:51 +0800230static struct imx_rgpio2p_soc_data imx7ulp_data = {
231 .have_dual_base = true,
232};
233
234static struct imx_rgpio2p_soc_data imx8ulp_data = {
235 .have_dual_base = false,
236};
Peng Fanc5f49862017-02-22 16:21:45 +0800237
238static const struct udevice_id imx_rgpio2p_ids[] = {
Peng Fanbcdc6162024-04-12 22:24:51 +0800239 { .compatible = "fsl,imx7ulp-gpio", .data = (ulong)&imx7ulp_data },
240 { .compatible = "fsl,imx8ulp-gpio", .data = (ulong)&imx8ulp_data },
Peng Fanc5f49862017-02-22 16:21:45 +0800241 { }
242};
243
244U_BOOT_DRIVER(imx_rgpio2p) = {
245 .name = "imx_rgpio2p",
246 .id = UCLASS_GPIO,
247 .ops = &imx_rgpio2p_ops,
248 .probe = imx_rgpio2p_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700249 .priv_auto = sizeof(struct imx_rgpio2p_plat),
Peng Fanc5f49862017-02-22 16:21:45 +0800250 .of_match = imx_rgpio2p_ids,
251 .bind = imx_rgpio2p_bind,
252};
253
254#if !CONFIG_IS_ENABLED(OF_CONTROL)
255static const struct imx_rgpio2p_plat imx_plat[] = {
256 { 0, (struct gpio_regs *)RGPIO2P_GPIO1_BASE_ADDR },
257 { 1, (struct gpio_regs *)RGPIO2P_GPIO2_BASE_ADDR },
258 { 2, (struct gpio_regs *)RGPIO2P_GPIO3_BASE_ADDR },
259 { 3, (struct gpio_regs *)RGPIO2P_GPIO4_BASE_ADDR },
260 { 4, (struct gpio_regs *)RGPIO2P_GPIO5_BASE_ADDR },
261 { 5, (struct gpio_regs *)RGPIO2P_GPIO6_BASE_ADDR },
262};
263
Simon Glass1d8364a2020-12-28 20:34:54 -0700264U_BOOT_DRVINFOS(imx_rgpio2ps) = {
Peng Fanc5f49862017-02-22 16:21:45 +0800265 { "imx_rgpio2p", &imx_plat[0] },
266 { "imx_rgpio2p", &imx_plat[1] },
267 { "imx_rgpio2p", &imx_plat[2] },
268 { "imx_rgpio2p", &imx_plat[3] },
269 { "imx_rgpio2p", &imx_plat[4] },
270 { "imx_rgpio2p", &imx_plat[5] },
271};
272#endif