Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 |
| 4 | * Bhuvanchandra DV, Toradex, Inc. |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <fdtdec.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 12 | #include <asm/gpio.h> |
Stefano Babic | 552a848 | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 13 | #include <asm/mach-imx/iomux-v3.h> |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 14 | #include <asm/io.h> |
| 15 | #include <malloc.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | struct vybrid_gpios { |
| 20 | unsigned int chip; |
| 21 | struct vybrid_gpio_regs *reg; |
| 22 | }; |
| 23 | |
| 24 | static int vybrid_gpio_direction_input(struct udevice *dev, unsigned gpio) |
| 25 | { |
| 26 | const struct vybrid_gpios *gpios = dev_get_priv(dev); |
| 27 | |
| 28 | gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT); |
| 29 | imx_iomux_gpio_set_direction(gpio, VF610_GPIO_DIRECTION_IN); |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | static int vybrid_gpio_direction_output(struct udevice *dev, unsigned gpio, |
| 35 | int value) |
| 36 | { |
| 37 | const struct vybrid_gpios *gpios = dev_get_priv(dev); |
| 38 | |
| 39 | gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT); |
| 40 | gpio_set_value(gpio, value); |
| 41 | imx_iomux_gpio_set_direction(gpio, VF610_GPIO_DIRECTION_OUT); |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static int vybrid_gpio_get_value(struct udevice *dev, unsigned gpio) |
| 47 | { |
| 48 | const struct vybrid_gpios *gpios = dev_get_priv(dev); |
| 49 | |
| 50 | return ((readl(&gpios->reg->gpio_pdir) & (1 << gpio))) ? 1 : 0; |
| 51 | } |
| 52 | |
| 53 | static int vybrid_gpio_set_value(struct udevice *dev, unsigned gpio, |
| 54 | int value) |
| 55 | { |
| 56 | const struct vybrid_gpios *gpios = dev_get_priv(dev); |
| 57 | if (value) |
| 58 | writel((1 << gpio), &gpios->reg->gpio_psor); |
| 59 | else |
| 60 | writel((1 << gpio), &gpios->reg->gpio_pcor); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int vybrid_gpio_get_function(struct udevice *dev, unsigned gpio) |
| 66 | { |
| 67 | const struct vybrid_gpios *gpios = dev_get_priv(dev); |
| 68 | u32 g_state = 0; |
| 69 | |
| 70 | gpio = gpio + (gpios->chip * VYBRID_GPIO_COUNT); |
| 71 | |
| 72 | imx_iomux_gpio_get_function(gpio, &g_state); |
| 73 | |
| 74 | if (((g_state & (0x07 << PAD_MUX_MODE_SHIFT)) >> PAD_MUX_MODE_SHIFT) > 0) |
| 75 | return GPIOF_FUNC; |
| 76 | if (g_state & PAD_CTL_OBE_ENABLE) |
| 77 | return GPIOF_OUTPUT; |
| 78 | if (g_state & PAD_CTL_IBE_ENABLE) |
| 79 | return GPIOF_INPUT; |
| 80 | if (!(g_state & PAD_CTL_OBE_IBE_ENABLE)) |
| 81 | return GPIOF_UNUSED; |
| 82 | |
| 83 | return GPIOF_UNKNOWN; |
| 84 | } |
| 85 | |
| 86 | static const struct dm_gpio_ops gpio_vybrid_ops = { |
| 87 | .direction_input = vybrid_gpio_direction_input, |
| 88 | .direction_output = vybrid_gpio_direction_output, |
| 89 | .get_value = vybrid_gpio_get_value, |
| 90 | .set_value = vybrid_gpio_set_value, |
| 91 | .get_function = vybrid_gpio_get_function, |
| 92 | }; |
| 93 | |
| 94 | static int vybrid_gpio_probe(struct udevice *dev) |
| 95 | { |
| 96 | struct vybrid_gpios *gpios = dev_get_priv(dev); |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 97 | struct vybrid_gpio_plat *plat = dev_get_plat(dev); |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 98 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 99 | |
| 100 | uc_priv->bank_name = plat->port_name; |
| 101 | uc_priv->gpio_count = VYBRID_GPIO_COUNT; |
| 102 | gpios->reg = (struct vybrid_gpio_regs *)plat->base; |
| 103 | gpios->chip = plat->chip; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 108 | static int vybrid_gpio_odata_to_plat(struct udevice *dev) |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 109 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 110 | struct vybrid_gpio_plat *plat = dev_get_plat(dev); |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 111 | fdt_addr_t base_addr; |
| 112 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 113 | base_addr = dev_read_addr(dev); |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 114 | if (base_addr == FDT_ADDR_T_NONE) |
Simon Glass | 1ba2147 | 2017-09-17 16:54:51 -0600 | [diff] [blame] | 115 | return -EINVAL; |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 116 | |
| 117 | plat->base = base_addr; |
Simon Glass | fb0ea6a | 2020-12-16 21:20:24 -0700 | [diff] [blame] | 118 | plat->chip = dev_seq(dev); |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 119 | plat->port_name = fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL); |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 124 | static const struct udevice_id vybrid_gpio_ids[] = { |
| 125 | { .compatible = "fsl,vf610-gpio" }, |
| 126 | { } |
| 127 | }; |
| 128 | |
| 129 | U_BOOT_DRIVER(gpio_vybrid) = { |
| 130 | .name = "gpio_vybrid", |
| 131 | .id = UCLASS_GPIO, |
| 132 | .ops = &gpio_vybrid_ops, |
Simon Glass | 1ba2147 | 2017-09-17 16:54:51 -0600 | [diff] [blame] | 133 | .of_match = vybrid_gpio_ids, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 134 | .of_to_plat = vybrid_gpio_odata_to_plat, |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 135 | .probe = vybrid_gpio_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 136 | .priv_auto = sizeof(struct vybrid_gpios), |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 137 | .plat_auto = sizeof(struct vybrid_gpio_plat), |
Bhuvanchandra DV | d348a94 | 2015-06-01 18:37:16 +0530 | [diff] [blame] | 138 | }; |