blob: d2c1d7d859013da212ce19cf80f25d1653e31fc2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +05302/*
3 * Copyright (C) 2015
4 * Bhuvanchandra DV, Toradex, Inc.
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +05305 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <fdtdec.h>
11#include <asm/gpio.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020012#include <asm/mach-imx/iomux-v3.h>
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +053013#include <asm/io.h>
14#include <malloc.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18struct vybrid_gpios {
19 unsigned int chip;
20 struct vybrid_gpio_regs *reg;
21};
22
23static int vybrid_gpio_direction_input(struct udevice *dev, unsigned gpio)
24{
25 const struct vybrid_gpios *gpios = dev_get_priv(dev);
26
27 gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT);
28 imx_iomux_gpio_set_direction(gpio, VF610_GPIO_DIRECTION_IN);
29
30 return 0;
31}
32
33static int vybrid_gpio_direction_output(struct udevice *dev, unsigned gpio,
34 int value)
35{
36 const struct vybrid_gpios *gpios = dev_get_priv(dev);
37
38 gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT);
39 gpio_set_value(gpio, value);
40 imx_iomux_gpio_set_direction(gpio, VF610_GPIO_DIRECTION_OUT);
41
42 return 0;
43}
44
45static int vybrid_gpio_get_value(struct udevice *dev, unsigned gpio)
46{
47 const struct vybrid_gpios *gpios = dev_get_priv(dev);
48
49 return ((readl(&gpios->reg->gpio_pdir) & (1 << gpio))) ? 1 : 0;
50}
51
52static int vybrid_gpio_set_value(struct udevice *dev, unsigned gpio,
53 int value)
54{
55 const struct vybrid_gpios *gpios = dev_get_priv(dev);
56 if (value)
57 writel((1 << gpio), &gpios->reg->gpio_psor);
58 else
59 writel((1 << gpio), &gpios->reg->gpio_pcor);
60
61 return 0;
62}
63
64static int vybrid_gpio_get_function(struct udevice *dev, unsigned gpio)
65{
66 const struct vybrid_gpios *gpios = dev_get_priv(dev);
67 u32 g_state = 0;
68
69 gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT);
70
71 imx_iomux_gpio_get_function(gpio, &g_state);
72
73 if (((g_state & (0x07 << PAD_MUX_MODE_SHIFT)) >> PAD_MUX_MODE_SHIFT) > 0)
74 return GPIOF_FUNC;
75 if (g_state & PAD_CTL_OBE_ENABLE)
76 return GPIOF_OUTPUT;
77 if (g_state & PAD_CTL_IBE_ENABLE)
78 return GPIOF_INPUT;
79 if (!(g_state & PAD_CTL_OBE_IBE_ENABLE))
80 return GPIOF_UNUSED;
81
82 return GPIOF_UNKNOWN;
83}
84
85static const struct dm_gpio_ops gpio_vybrid_ops = {
86 .direction_input = vybrid_gpio_direction_input,
87 .direction_output = vybrid_gpio_direction_output,
88 .get_value = vybrid_gpio_get_value,
89 .set_value = vybrid_gpio_set_value,
90 .get_function = vybrid_gpio_get_function,
91};
92
93static int vybrid_gpio_probe(struct udevice *dev)
94{
95 struct vybrid_gpios *gpios = dev_get_priv(dev);
96 struct vybrid_gpio_platdata *plat = dev_get_platdata(dev);
97 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
98
99 uc_priv->bank_name = plat->port_name;
100 uc_priv->gpio_count = VYBRID_GPIO_COUNT;
101 gpios->reg = (struct vybrid_gpio_regs *)plat->base;
102 gpios->chip = plat->chip;
103
104 return 0;
105}
106
Simon Glass9e1bf802017-09-17 16:54:51 -0600107static int vybrid_gpio_odata_to_platdata(struct udevice *dev)
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +0530108{
Simon Glass9e1bf802017-09-17 16:54:51 -0600109 struct vybrid_gpio_platdata *plat = dev_get_platdata(dev);
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +0530110 fdt_addr_t base_addr;
111
Simon Glassba1dea42017-05-17 17:18:05 -0600112 base_addr = devfdt_get_addr(dev);
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +0530113 if (base_addr == FDT_ADDR_T_NONE)
Simon Glass9e1bf802017-09-17 16:54:51 -0600114 return -EINVAL;
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +0530115
116 plat->base = base_addr;
117 plat->chip = dev->req_seq;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700118 plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL);
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +0530119
120 return 0;
121}
122
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +0530123static const struct udevice_id vybrid_gpio_ids[] = {
124 { .compatible = "fsl,vf610-gpio" },
125 { }
126};
127
128U_BOOT_DRIVER(gpio_vybrid) = {
129 .name = "gpio_vybrid",
130 .id = UCLASS_GPIO,
131 .ops = &gpio_vybrid_ops,
Simon Glass9e1bf802017-09-17 16:54:51 -0600132 .of_match = vybrid_gpio_ids,
133 .ofdata_to_platdata = vybrid_gpio_odata_to_platdata,
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +0530134 .probe = vybrid_gpio_probe,
135 .priv_auto_alloc_size = sizeof(struct vybrid_gpios),
Simon Glass9e1bf802017-09-17 16:54:51 -0600136 .platdata_auto_alloc_size = sizeof(struct vybrid_gpio_platdata),
Bhuvanchandra DV6d236aa2015-06-01 18:37:16 +0530137};