Jerome Brunet | 8587839 | 2018-10-05 09:36:37 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Jerome Brunet <jbrunet@baylibre.com> |
| 4 | * Copyright (C) 2017 Xingyu Chen <xingyu.chen@amlogic.com> |
| 5 | */ |
| 6 | |
| 7 | #include <asm/gpio.h> |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <dm/pinctrl.h> |
| 11 | #include <linux/io.h> |
| 12 | #include "pinctrl-meson-axg.h" |
| 13 | |
| 14 | static int meson_axg_pmx_get_bank(struct udevice *dev, unsigned int pin, |
| 15 | struct meson_pmx_bank **bank) |
| 16 | { |
| 17 | int i; |
| 18 | struct meson_pinctrl *priv = dev_get_priv(dev); |
| 19 | struct meson_axg_pmx_data *pmx = priv->data->pmx_data; |
| 20 | |
| 21 | for (i = 0; i < pmx->num_pmx_banks; i++) |
| 22 | if (pin >= pmx->pmx_banks[i].first && |
| 23 | pin <= pmx->pmx_banks[i].last) { |
| 24 | *bank = &pmx->pmx_banks[i]; |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | return -EINVAL; |
| 29 | } |
| 30 | |
| 31 | static int meson_axg_pmx_calc_reg_and_offset(struct meson_pmx_bank *bank, |
| 32 | unsigned int pin, |
| 33 | unsigned int *reg, |
| 34 | unsigned int *offset) |
| 35 | { |
| 36 | int shift; |
| 37 | |
| 38 | shift = pin - bank->first; |
| 39 | |
| 40 | *reg = bank->reg + (bank->offset + (shift << 2)) / 32; |
| 41 | *offset = (bank->offset + (shift << 2)) % 32; |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static int meson_axg_pmx_update_function(struct udevice *dev, |
| 47 | unsigned int pin, unsigned int func) |
| 48 | { |
| 49 | struct meson_pinctrl *priv = dev_get_priv(dev); |
| 50 | struct meson_pmx_bank *bank; |
| 51 | unsigned int offset; |
| 52 | unsigned int reg; |
| 53 | unsigned int tmp; |
| 54 | int ret; |
| 55 | |
| 56 | ret = meson_axg_pmx_get_bank(dev, pin, &bank); |
| 57 | if (ret) |
| 58 | return ret; |
| 59 | |
| 60 | meson_axg_pmx_calc_reg_and_offset(bank, pin, ®, &offset); |
| 61 | |
| 62 | tmp = readl(priv->reg_mux + (reg << 2)); |
| 63 | tmp &= ~(0xf << offset); |
| 64 | tmp |= (func & 0xf) << offset; |
| 65 | writel(tmp, priv->reg_mux + (reg << 2)); |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | static int meson_axg_pinmux_group_set(struct udevice *dev, |
| 71 | unsigned int group_selector, |
| 72 | unsigned int func_selector) |
| 73 | { |
| 74 | struct meson_pinctrl *priv = dev_get_priv(dev); |
| 75 | const struct meson_pmx_group *group; |
| 76 | const struct meson_pmx_func *func; |
| 77 | struct meson_pmx_axg_data *pmx_data; |
| 78 | int i, ret; |
| 79 | |
| 80 | group = &priv->data->groups[group_selector]; |
| 81 | pmx_data = (struct meson_pmx_axg_data *)group->data; |
| 82 | func = &priv->data->funcs[func_selector]; |
| 83 | |
| 84 | debug("pinmux: set group %s func %s\n", group->name, func->name); |
| 85 | |
| 86 | for (i = 0; i < group->num_pins; i++) { |
| 87 | ret = meson_axg_pmx_update_function(dev, group->pins[i], |
| 88 | pmx_data->func); |
| 89 | if (ret) |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
Neil Armstrong | b9308f2 | 2019-06-04 11:04:54 +0200 | [diff] [blame^] | 96 | static int meson_axg_pinmux_get(struct udevice *dev, unsigned int selector, |
| 97 | char *buf, int size) |
| 98 | { |
| 99 | struct meson_pinctrl *priv = dev_get_priv(dev); |
| 100 | struct meson_pmx_axg_data *pmx_data; |
| 101 | struct meson_pmx_group *group; |
| 102 | struct meson_pmx_bank *bank; |
| 103 | unsigned int offset; |
| 104 | unsigned int func; |
| 105 | unsigned int reg; |
| 106 | int ret, i, j; |
| 107 | |
| 108 | selector += priv->data->pin_base; |
| 109 | |
| 110 | ret = meson_axg_pmx_get_bank(dev, selector, &bank); |
| 111 | if (ret) { |
| 112 | snprintf(buf, size, "Unhandled"); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | meson_axg_pmx_calc_reg_and_offset(bank, selector, ®, &offset); |
| 117 | |
| 118 | func = (readl(priv->reg_mux + (reg << 2)) >> offset) & 0xf; |
| 119 | |
| 120 | for (i = 0; i < priv->data->num_groups; i++) { |
| 121 | group = &priv->data->groups[i]; |
| 122 | pmx_data = (struct meson_pmx_axg_data *)group->data; |
| 123 | |
| 124 | if (pmx_data->func != func) |
| 125 | continue; |
| 126 | |
| 127 | for (j = 0; j < group->num_pins; j++) { |
| 128 | if (group->pins[j] == selector) { |
| 129 | snprintf(buf, size, "%s (%x)", |
| 130 | group->name, func); |
| 131 | return 0; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | snprintf(buf, size, "Unknown (%x)", func); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
Jerome Brunet | c4c726c | 2019-01-04 15:44:34 +0100 | [diff] [blame] | 141 | const struct pinconf_param meson_axg_pinconf_params[] = { |
| 142 | { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, |
| 143 | { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 }, |
| 144 | { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 }, |
| 145 | }; |
| 146 | |
Jerome Brunet | 8587839 | 2018-10-05 09:36:37 +0200 | [diff] [blame] | 147 | const struct pinctrl_ops meson_axg_pinctrl_ops = { |
| 148 | .get_groups_count = meson_pinctrl_get_groups_count, |
| 149 | .get_group_name = meson_pinctrl_get_group_name, |
| 150 | .get_functions_count = meson_pinmux_get_functions_count, |
| 151 | .get_function_name = meson_pinmux_get_function_name, |
| 152 | .pinmux_group_set = meson_axg_pinmux_group_set, |
| 153 | .set_state = pinctrl_generic_set_state, |
Jerome Brunet | c4c726c | 2019-01-04 15:44:34 +0100 | [diff] [blame] | 154 | .pinconf_params = meson_axg_pinconf_params, |
| 155 | .pinconf_num_params = ARRAY_SIZE(meson_axg_pinconf_params), |
| 156 | .pinconf_set = meson_pinconf_set, |
| 157 | .pinconf_group_set = meson_pinconf_group_set, |
Neil Armstrong | b9308f2 | 2019-06-04 11:04:54 +0200 | [diff] [blame^] | 158 | .get_pin_name = meson_pinctrl_get_pin_name, |
| 159 | .get_pins_count = meson_pinctrl_get_pins_count, |
| 160 | .get_pin_muxing = meson_axg_pinmux_get, |
Jerome Brunet | 8587839 | 2018-10-05 09:36:37 +0200 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | static int meson_axg_gpio_request(struct udevice *dev, |
| 164 | unsigned int offset, const char *label) |
| 165 | { |
| 166 | return meson_axg_pmx_update_function(dev->parent, offset, 0); |
| 167 | } |
| 168 | |
| 169 | static const struct dm_gpio_ops meson_axg_gpio_ops = { |
| 170 | .request = meson_axg_gpio_request, |
| 171 | .set_value = meson_gpio_set, |
| 172 | .get_value = meson_gpio_get, |
| 173 | .get_function = meson_gpio_get_direction, |
| 174 | .direction_input = meson_gpio_direction_input, |
| 175 | .direction_output = meson_gpio_direction_output, |
| 176 | }; |
| 177 | |
| 178 | const struct driver meson_axg_gpio_driver = { |
| 179 | .name = "meson-axg-gpio", |
| 180 | .id = UCLASS_GPIO, |
| 181 | .probe = meson_gpio_probe, |
| 182 | .ops = &meson_axg_gpio_ops, |
| 183 | }; |