Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Synopsys HSDK SDP Generic PLL clock driver |
| 3 | * |
| 4 | * Copyright (C) 2017 Synopsys |
| 5 | * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com> |
| 6 | * |
| 7 | * This file is licensed under the terms of the GNU General Public |
| 8 | * License version 2. This program is licensed "as is" without any |
| 9 | * warranty of any kind, whether express or implied. |
| 10 | */ |
| 11 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 12 | #include <log.h> |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 13 | #include <asm-generic/gpio.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <common.h> |
| 16 | #include <dm.h> |
| 17 | #include <errno.h> |
| 18 | #include <linux/printk.h> |
| 19 | |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 20 | #define DRV_NAME "gpio_creg" |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 21 | |
| 22 | struct hsdk_creg_gpio { |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 23 | u32 *regs; |
| 24 | u8 shift; |
| 25 | u8 activate; |
| 26 | u8 deactivate; |
| 27 | u8 bit_per_gpio; |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | static int hsdk_creg_gpio_set_value(struct udevice *dev, unsigned oft, int val) |
| 31 | { |
| 32 | struct hsdk_creg_gpio *hcg = dev_get_priv(dev); |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 33 | u8 reg_shift = oft * hcg->bit_per_gpio + hcg->shift; |
| 34 | u32 reg = readl(hcg->regs); |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 35 | |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 36 | reg &= ~(GENMASK(hcg->bit_per_gpio - 1, 0) << reg_shift); |
| 37 | reg |= ((val ? hcg->deactivate : hcg->activate) << reg_shift); |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 38 | |
| 39 | writel(reg, hcg->regs); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int hsdk_creg_gpio_direction_output(struct udevice *dev, unsigned oft, |
| 45 | int val) |
| 46 | { |
| 47 | hsdk_creg_gpio_set_value(dev, oft, val); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int hsdk_creg_gpio_direction_input(struct udevice *dev, unsigned oft) |
| 53 | { |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 54 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 55 | |
| 56 | pr_err("%s can't be used as input!\n", uc_priv->bank_name); |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 57 | |
| 58 | return -ENOTSUPP; |
| 59 | } |
| 60 | |
| 61 | static int hsdk_creg_gpio_get_value(struct udevice *dev, unsigned int oft) |
| 62 | { |
| 63 | struct hsdk_creg_gpio *hcg = dev_get_priv(dev); |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 64 | u32 val = readl(hcg->regs); |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 65 | |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 66 | val >>= oft * hcg->bit_per_gpio + hcg->shift; |
| 67 | val &= GENMASK(hcg->bit_per_gpio - 1, 0); |
| 68 | return (val == hcg->deactivate) ? 1 : 0; |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static const struct dm_gpio_ops hsdk_creg_gpio_ops = { |
| 72 | .direction_output = hsdk_creg_gpio_direction_output, |
| 73 | .direction_input = hsdk_creg_gpio_direction_input, |
| 74 | .set_value = hsdk_creg_gpio_set_value, |
| 75 | .get_value = hsdk_creg_gpio_get_value, |
| 76 | }; |
| 77 | |
| 78 | static int hsdk_creg_gpio_probe(struct udevice *dev) |
| 79 | { |
| 80 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 81 | struct hsdk_creg_gpio *hcg = dev_get_priv(dev); |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 82 | u32 shift, bit_per_gpio, activate, deactivate, gpio_count; |
| 83 | const u8 *defaults; |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 84 | |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 85 | hcg->regs = (u32 *)devfdt_get_addr_ptr(dev); |
| 86 | gpio_count = dev_read_u32_default(dev, "gpio-count", 1); |
| 87 | shift = dev_read_u32_default(dev, "gpio-first-shift", 0); |
| 88 | bit_per_gpio = dev_read_u32_default(dev, "gpio-bit-per-line", 1); |
| 89 | activate = dev_read_u32_default(dev, "gpio-activate-val", 1); |
| 90 | deactivate = dev_read_u32_default(dev, "gpio-deactivate-val", 0); |
| 91 | defaults = dev_read_u8_array_ptr(dev, "gpio-default-val", gpio_count); |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 92 | |
| 93 | uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name"); |
| 94 | if (!uc_priv->bank_name) |
| 95 | uc_priv->bank_name = dev_read_name(dev); |
| 96 | |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 97 | if (!bit_per_gpio) { |
| 98 | pr_err("%s: 'gpio-bit-per-line' can't be 0\n", |
| 99 | uc_priv->bank_name); |
| 100 | |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | if (!gpio_count) { |
| 105 | pr_err("%s: 'gpio-count' can't be 0\n", |
| 106 | uc_priv->bank_name); |
| 107 | |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | |
| 111 | if ((gpio_count * bit_per_gpio + shift) > 32) { |
| 112 | pr_err("%s: u32 io register overflow: try to use %u bits\n", |
| 113 | uc_priv->bank_name, gpio_count * bit_per_gpio + shift); |
| 114 | |
| 115 | return -EINVAL; |
| 116 | } |
| 117 | |
| 118 | if (GENMASK(31, bit_per_gpio) & activate) { |
| 119 | pr_err("%s: 'gpio-activate-val' can't be more than %lu\n", |
| 120 | uc_priv->bank_name, GENMASK(bit_per_gpio - 1, 0)); |
| 121 | |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | |
| 125 | if (GENMASK(31, bit_per_gpio) & deactivate) { |
| 126 | pr_err("%s: 'gpio-deactivate-val' can't be more than %lu\n", |
| 127 | uc_priv->bank_name, GENMASK(bit_per_gpio - 1, 0)); |
| 128 | |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | |
| 132 | if (activate == deactivate) { |
| 133 | pr_err("%s: 'gpio-deactivate-val' and 'gpio-activate-val' can't be equal\n", |
| 134 | uc_priv->bank_name); |
| 135 | |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | |
| 139 | hcg->shift = (u8)shift; |
| 140 | hcg->bit_per_gpio = (u8)bit_per_gpio; |
| 141 | hcg->activate = (u8)activate; |
| 142 | hcg->deactivate = (u8)deactivate; |
| 143 | uc_priv->gpio_count = gpio_count; |
| 144 | |
| 145 | /* Setup default GPIO value if we have "gpio-default-val" array */ |
| 146 | if (defaults) |
| 147 | for (u8 i = 0; i < gpio_count; i++) |
| 148 | hsdk_creg_gpio_set_value(dev, i, defaults[i]); |
| 149 | |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 150 | pr_debug("%s GPIO [0x%p] controller with %d gpios probed\n", |
| 151 | uc_priv->bank_name, hcg->regs, uc_priv->gpio_count); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static const struct udevice_id hsdk_creg_gpio_ids[] = { |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 157 | { .compatible = "snps,creg-gpio" }, |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 158 | { } |
| 159 | }; |
| 160 | |
| 161 | U_BOOT_DRIVER(gpio_hsdk_creg) = { |
Eugeniy Paltsev | b23d633 | 2018-06-08 17:58:23 +0300 | [diff] [blame] | 162 | .name = DRV_NAME, |
Eugeniy Paltsev | 92d32fc | 2017-10-16 16:21:32 +0300 | [diff] [blame] | 163 | .id = UCLASS_GPIO, |
| 164 | .ops = &hsdk_creg_gpio_ops, |
| 165 | .probe = hsdk_creg_gpio_probe, |
| 166 | .of_match = hsdk_creg_gpio_ids, |
| 167 | .platdata_auto_alloc_size = sizeof(struct hsdk_creg_gpio), |
| 168 | }; |