Jerome Brunet | 004447d | 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 | 062e57b | 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 | 8702199 | 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 }, |
Guillaume La Roque | 3076735 | 2019-06-04 13:53:07 +0200 | [diff] [blame] | 145 | { "drive-strength-microamp", PIN_CONFIG_DRIVE_STRENGTH_UA, 0 }, |
Jerome Brunet | 8702199 | 2019-01-04 15:44:34 +0100 | [diff] [blame] | 146 | }; |
| 147 | |
Jerome Brunet | 004447d | 2018-10-05 09:36:37 +0200 | [diff] [blame] | 148 | const struct pinctrl_ops meson_axg_pinctrl_ops = { |
| 149 | .get_groups_count = meson_pinctrl_get_groups_count, |
| 150 | .get_group_name = meson_pinctrl_get_group_name, |
| 151 | .get_functions_count = meson_pinmux_get_functions_count, |
| 152 | .get_function_name = meson_pinmux_get_function_name, |
| 153 | .pinmux_group_set = meson_axg_pinmux_group_set, |
| 154 | .set_state = pinctrl_generic_set_state, |
Jerome Brunet | 8702199 | 2019-01-04 15:44:34 +0100 | [diff] [blame] | 155 | .pinconf_params = meson_axg_pinconf_params, |
| 156 | .pinconf_num_params = ARRAY_SIZE(meson_axg_pinconf_params), |
| 157 | .pinconf_set = meson_pinconf_set, |
| 158 | .pinconf_group_set = meson_pinconf_group_set, |
Neil Armstrong | 062e57b | 2019-06-04 11:04:54 +0200 | [diff] [blame] | 159 | .get_pin_name = meson_pinctrl_get_pin_name, |
| 160 | .get_pins_count = meson_pinctrl_get_pins_count, |
| 161 | .get_pin_muxing = meson_axg_pinmux_get, |
Jerome Brunet | 004447d | 2018-10-05 09:36:37 +0200 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | static int meson_axg_gpio_request(struct udevice *dev, |
| 165 | unsigned int offset, const char *label) |
| 166 | { |
| 167 | return meson_axg_pmx_update_function(dev->parent, offset, 0); |
| 168 | } |
| 169 | |
| 170 | static const struct dm_gpio_ops meson_axg_gpio_ops = { |
| 171 | .request = meson_axg_gpio_request, |
| 172 | .set_value = meson_gpio_set, |
| 173 | .get_value = meson_gpio_get, |
| 174 | .get_function = meson_gpio_get_direction, |
| 175 | .direction_input = meson_gpio_direction_input, |
| 176 | .direction_output = meson_gpio_direction_output, |
| 177 | }; |
| 178 | |
| 179 | const struct driver meson_axg_gpio_driver = { |
| 180 | .name = "meson-axg-gpio", |
| 181 | .id = UCLASS_GPIO, |
| 182 | .probe = meson_gpio_probe, |
| 183 | .ops = &meson_axg_gpio_ops, |
| 184 | }; |