3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
6 * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
8 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/arch/imx-regs.h>
18 enum mxc_gpio_direction {
19 MXC_GPIO_DIRECTION_IN,
20 MXC_GPIO_DIRECTION_OUT,
23 #define GPIO_PER_BANK 32
25 struct mxc_gpio_plat {
27 struct gpio_regs *regs;
30 struct mxc_bank_info {
31 struct gpio_regs *regs;
34 #ifndef CONFIG_DM_GPIO
35 #define GPIO_TO_PORT(n) (n / 32)
37 /* GPIO port description */
38 static unsigned long gpio_ports[] = {
39 [0] = GPIO1_BASE_ADDR,
40 [1] = GPIO2_BASE_ADDR,
41 [2] = GPIO3_BASE_ADDR,
42 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
43 defined(CONFIG_MX53) || defined(CONFIG_MX6)
44 [3] = GPIO4_BASE_ADDR,
46 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
47 [4] = GPIO5_BASE_ADDR,
48 [5] = GPIO6_BASE_ADDR,
50 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
51 [6] = GPIO7_BASE_ADDR,
55 static int mxc_gpio_direction(unsigned int gpio,
56 enum mxc_gpio_direction direction)
58 unsigned int port = GPIO_TO_PORT(gpio);
59 struct gpio_regs *regs;
62 if (port >= ARRAY_SIZE(gpio_ports))
67 regs = (struct gpio_regs *)gpio_ports[port];
69 l = readl(®s->gpio_dir);
72 case MXC_GPIO_DIRECTION_OUT:
75 case MXC_GPIO_DIRECTION_IN:
78 writel(l, ®s->gpio_dir);
83 int gpio_set_value(unsigned gpio, int value)
85 unsigned int port = GPIO_TO_PORT(gpio);
86 struct gpio_regs *regs;
89 if (port >= ARRAY_SIZE(gpio_ports))
94 regs = (struct gpio_regs *)gpio_ports[port];
96 l = readl(®s->gpio_dr);
101 writel(l, ®s->gpio_dr);
106 int gpio_get_value(unsigned gpio)
108 unsigned int port = GPIO_TO_PORT(gpio);
109 struct gpio_regs *regs;
112 if (port >= ARRAY_SIZE(gpio_ports))
117 regs = (struct gpio_regs *)gpio_ports[port];
119 val = (readl(®s->gpio_psr) >> gpio) & 0x01;
124 int gpio_request(unsigned gpio, const char *label)
126 unsigned int port = GPIO_TO_PORT(gpio);
127 if (port >= ARRAY_SIZE(gpio_ports))
132 int gpio_free(unsigned gpio)
137 int gpio_direction_input(unsigned gpio)
139 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
142 int gpio_direction_output(unsigned gpio, int value)
144 int ret = gpio_set_value(gpio, value);
149 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
153 #ifdef CONFIG_DM_GPIO
155 DECLARE_GLOBAL_DATA_PTR;
157 static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
161 val = readl(®s->gpio_dir);
163 return val & (1 << offset) ? 1 : 0;
166 static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
167 enum mxc_gpio_direction direction)
171 l = readl(®s->gpio_dir);
174 case MXC_GPIO_DIRECTION_OUT:
177 case MXC_GPIO_DIRECTION_IN:
180 writel(l, ®s->gpio_dir);
183 static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
188 l = readl(®s->gpio_dr);
193 writel(l, ®s->gpio_dr);
196 static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
198 return (readl(®s->gpio_psr) >> offset) & 0x01;
201 /* set GPIO pin 'gpio' as an input */
202 static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
204 struct mxc_bank_info *bank = dev_get_priv(dev);
206 /* Configure GPIO direction as input. */
207 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
212 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
213 static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
216 struct mxc_bank_info *bank = dev_get_priv(dev);
218 /* Configure GPIO output value. */
219 mxc_gpio_bank_set_value(bank->regs, offset, value);
221 /* Configure GPIO direction as output. */
222 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
227 /* read GPIO IN value of pin 'gpio' */
228 static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
230 struct mxc_bank_info *bank = dev_get_priv(dev);
232 return mxc_gpio_bank_get_value(bank->regs, offset);
235 /* write GPIO OUT value to pin 'gpio' */
236 static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
239 struct mxc_bank_info *bank = dev_get_priv(dev);
241 mxc_gpio_bank_set_value(bank->regs, offset, value);
246 static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
248 struct mxc_bank_info *bank = dev_get_priv(dev);
250 /* GPIOF_FUNC is not implemented yet */
251 if (mxc_gpio_is_output(bank->regs, offset))
257 static const struct dm_gpio_ops gpio_mxc_ops = {
258 .direction_input = mxc_gpio_direction_input,
259 .direction_output = mxc_gpio_direction_output,
260 .get_value = mxc_gpio_get_value,
261 .set_value = mxc_gpio_set_value,
262 .get_function = mxc_gpio_get_function,
265 static int mxc_gpio_probe(struct udevice *dev)
267 struct mxc_bank_info *bank = dev_get_priv(dev);
268 struct mxc_gpio_plat *plat = dev_get_platdata(dev);
269 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
273 banknum = plat->bank_index;
274 sprintf(name, "GPIO%d_", banknum + 1);
278 uc_priv->bank_name = str;
279 uc_priv->gpio_count = GPIO_PER_BANK;
280 bank->regs = plat->regs;
285 static int mxc_gpio_bind(struct udevice *dev)
287 struct mxc_gpio_plat *plat = dev->platdata;
291 * If platdata already exsits, directly return.
292 * Actually only when DT is not supported, platdata
293 * is statically initialized in U_BOOT_DEVICES.Here
299 addr = dev_get_addr(dev);
300 if (addr == FDT_ADDR_T_NONE)
305 * When every board is converted to driver model and DT is supported,
306 * this can be done by auto-alloc feature, but not using calloc
307 * to alloc memory for platdata.
309 plat = calloc(1, sizeof(*plat));
313 plat->regs = (struct gpio_regs *)addr;
314 plat->bank_index = dev->req_seq;
315 dev->platdata = plat;
320 static const struct udevice_id mxc_gpio_ids[] = {
321 { .compatible = "fsl,imx35-gpio" },
325 U_BOOT_DRIVER(gpio_mxc) = {
328 .ops = &gpio_mxc_ops,
329 .probe = mxc_gpio_probe,
330 .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
331 .of_match = mxc_gpio_ids,
332 .bind = mxc_gpio_bind,
335 #ifndef CONFIG_OF_CONTROL
336 static const struct mxc_gpio_plat mxc_plat[] = {
337 { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
338 { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
339 { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
340 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
341 defined(CONFIG_MX53) || defined(CONFIG_MX6)
342 { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
344 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
345 { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
346 { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
348 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
349 { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
353 U_BOOT_DEVICES(mxc_gpios) = {
354 { "gpio_mxc", &mxc_plat[0] },
355 { "gpio_mxc", &mxc_plat[1] },
356 { "gpio_mxc", &mxc_plat[2] },
357 #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX51) || \
358 defined(CONFIG_MX53) || defined(CONFIG_MX6)
359 { "gpio_mxc", &mxc_plat[3] },
361 #if defined(CONFIG_MX27) || defined(CONFIG_MX53) || defined(CONFIG_MX6)
362 { "gpio_mxc", &mxc_plat[4] },
363 { "gpio_mxc", &mxc_plat[5] },
365 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
366 { "gpio_mxc", &mxc_plat[6] },