1 // SPDX-License-Identifier: GPL-2.0+
4 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
7 * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
13 #include <asm/arch/imx-regs.h>
17 enum mxc_gpio_direction {
18 MXC_GPIO_DIRECTION_IN,
19 MXC_GPIO_DIRECTION_OUT,
22 #define GPIO_PER_BANK 32
24 struct mxc_gpio_plat {
26 struct gpio_regs *regs;
29 struct mxc_bank_info {
30 struct gpio_regs *regs;
33 #ifndef CONFIG_DM_GPIO
34 #define GPIO_TO_PORT(n) (n / 32)
36 /* GPIO port description */
37 static unsigned long gpio_ports[] = {
38 [0] = GPIO1_BASE_ADDR,
39 [1] = GPIO2_BASE_ADDR,
40 [2] = GPIO3_BASE_ADDR,
41 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
42 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
43 defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || \
44 defined(CONFIG_ARCH_IMX8)
45 [3] = GPIO4_BASE_ADDR,
47 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
48 defined(CONFIG_MX7) || defined(CONFIG_IMX8M) || \
49 defined(CONFIG_ARCH_IMX8)
50 [4] = GPIO5_BASE_ADDR,
51 #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) || defined(CONFIG_IMX8M))
52 [5] = GPIO6_BASE_ADDR,
55 #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_MX7) || \
56 defined(CONFIG_ARCH_IMX8)
57 #if !(defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL))
58 [6] = GPIO7_BASE_ADDR,
61 #if defined(CONFIG_ARCH_IMX8)
62 [7] = GPIO8_BASE_ADDR,
66 static int mxc_gpio_direction(unsigned int gpio,
67 enum mxc_gpio_direction direction)
69 unsigned int port = GPIO_TO_PORT(gpio);
70 struct gpio_regs *regs;
73 if (port >= ARRAY_SIZE(gpio_ports))
78 regs = (struct gpio_regs *)gpio_ports[port];
80 l = readl(®s->gpio_dir);
83 case MXC_GPIO_DIRECTION_OUT:
86 case MXC_GPIO_DIRECTION_IN:
89 writel(l, ®s->gpio_dir);
94 int gpio_set_value(unsigned gpio, int value)
96 unsigned int port = GPIO_TO_PORT(gpio);
97 struct gpio_regs *regs;
100 if (port >= ARRAY_SIZE(gpio_ports))
105 regs = (struct gpio_regs *)gpio_ports[port];
107 l = readl(®s->gpio_dr);
112 writel(l, ®s->gpio_dr);
117 int gpio_get_value(unsigned gpio)
119 unsigned int port = GPIO_TO_PORT(gpio);
120 struct gpio_regs *regs;
123 if (port >= ARRAY_SIZE(gpio_ports))
128 regs = (struct gpio_regs *)gpio_ports[port];
130 val = (readl(®s->gpio_psr) >> gpio) & 0x01;
135 int gpio_request(unsigned gpio, const char *label)
137 unsigned int port = GPIO_TO_PORT(gpio);
138 if (port >= ARRAY_SIZE(gpio_ports))
143 int gpio_free(unsigned gpio)
148 int gpio_direction_input(unsigned gpio)
150 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
153 int gpio_direction_output(unsigned gpio, int value)
155 int ret = gpio_set_value(gpio, value);
160 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
164 #ifdef CONFIG_DM_GPIO
166 static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
170 val = readl(®s->gpio_dir);
172 return val & (1 << offset) ? 1 : 0;
175 static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
176 enum mxc_gpio_direction direction)
180 l = readl(®s->gpio_dir);
183 case MXC_GPIO_DIRECTION_OUT:
186 case MXC_GPIO_DIRECTION_IN:
189 writel(l, ®s->gpio_dir);
192 static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
197 l = readl(®s->gpio_dr);
202 writel(l, ®s->gpio_dr);
205 static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
207 return (readl(®s->gpio_psr) >> offset) & 0x01;
210 /* set GPIO pin 'gpio' as an input */
211 static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
213 struct mxc_bank_info *bank = dev_get_priv(dev);
215 /* Configure GPIO direction as input. */
216 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
221 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
222 static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
225 struct mxc_bank_info *bank = dev_get_priv(dev);
227 /* Configure GPIO output value. */
228 mxc_gpio_bank_set_value(bank->regs, offset, value);
230 /* Configure GPIO direction as output. */
231 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
236 /* read GPIO IN value of pin 'gpio' */
237 static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
239 struct mxc_bank_info *bank = dev_get_priv(dev);
241 return mxc_gpio_bank_get_value(bank->regs, offset);
244 /* write GPIO OUT value to pin 'gpio' */
245 static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
248 struct mxc_bank_info *bank = dev_get_priv(dev);
250 mxc_gpio_bank_set_value(bank->regs, offset, value);
255 static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
257 struct mxc_bank_info *bank = dev_get_priv(dev);
259 /* GPIOF_FUNC is not implemented yet */
260 if (mxc_gpio_is_output(bank->regs, offset))
266 static const struct dm_gpio_ops gpio_mxc_ops = {
267 .direction_input = mxc_gpio_direction_input,
268 .direction_output = mxc_gpio_direction_output,
269 .get_value = mxc_gpio_get_value,
270 .set_value = mxc_gpio_set_value,
271 .get_function = mxc_gpio_get_function,
274 static int mxc_gpio_probe(struct udevice *dev)
276 struct mxc_bank_info *bank = dev_get_priv(dev);
277 struct mxc_gpio_plat *plat = dev_get_platdata(dev);
278 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
282 banknum = plat->bank_index;
283 sprintf(name, "GPIO%d_", banknum + 1);
287 uc_priv->bank_name = str;
288 uc_priv->gpio_count = GPIO_PER_BANK;
289 bank->regs = plat->regs;
294 static int mxc_gpio_bind(struct udevice *dev)
296 struct mxc_gpio_plat *plat = dev->platdata;
300 * If platdata already exsits, directly return.
301 * Actually only when DT is not supported, platdata
302 * is statically initialized in U_BOOT_DEVICES.Here
308 addr = devfdt_get_addr(dev);
309 if (addr == FDT_ADDR_T_NONE)
314 * When every board is converted to driver model and DT is supported,
315 * this can be done by auto-alloc feature, but not using calloc
316 * to alloc memory for platdata.
318 * For example mxc_plat below uses platform data rather than device
321 * NOTE: DO NOT COPY this code if you are using device tree.
323 plat = calloc(1, sizeof(*plat));
327 plat->regs = (struct gpio_regs *)addr;
328 plat->bank_index = dev->req_seq;
329 dev->platdata = plat;
334 static const struct udevice_id mxc_gpio_ids[] = {
335 { .compatible = "fsl,imx35-gpio" },
339 U_BOOT_DRIVER(gpio_mxc) = {
342 .ops = &gpio_mxc_ops,
343 .probe = mxc_gpio_probe,
344 .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
345 .of_match = mxc_gpio_ids,
346 .bind = mxc_gpio_bind,
349 #if !CONFIG_IS_ENABLED(OF_CONTROL)
350 static const struct mxc_gpio_plat mxc_plat[] = {
351 { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
352 { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
353 { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
354 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
355 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
356 defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8)
357 { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
359 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
360 defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8)
361 { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
363 { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
366 #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_ARCH_IMX8)
367 { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
369 #if defined(CONFIG_ARCH_IMX8)
370 { 7, (struct gpio_regs *)GPIO8_BASE_ADDR },
374 U_BOOT_DEVICES(mxc_gpios) = {
375 { "gpio_mxc", &mxc_plat[0] },
376 { "gpio_mxc", &mxc_plat[1] },
377 { "gpio_mxc", &mxc_plat[2] },
378 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
379 defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
380 defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8)
381 { "gpio_mxc", &mxc_plat[3] },
383 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6) || \
384 defined(CONFIG_IMX8M) || defined(CONFIG_ARCH_IMX8)
385 { "gpio_mxc", &mxc_plat[4] },
387 { "gpio_mxc", &mxc_plat[5] },
390 #if defined(CONFIG_MX53) || defined(CONFIG_MX6) || defined(CONFIG_ARCH_IMX8)
391 { "gpio_mxc", &mxc_plat[6] },
393 #if defined(CONFIG_ARCH_IMX8)
394 { "gpio_mxc", &mxc_plat[7] },