Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * TLMM driver for Qualcomm APQ8016, APQ8096 |
| 4 | * |
| 5 | * (C) Copyright 2018 Ramon Fried <ramon.fried@gmail.com> |
| 6 | * |
| 7 | */ |
| 8 | |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <asm/io.h> |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 12 | #include <dm/device_compat.h> |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 14 | #include <dm/lists.h> |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 15 | #include <asm/gpio.h> |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 16 | #include <dm/pinctrl.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Caleb Connolly | ab1c889 | 2025-04-10 10:52:38 +0200 | [diff] [blame] | 18 | #include <linux/bitmap.h> |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 19 | #include <linux/bug.h> |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 20 | #include <mach/gpio.h> |
| 21 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 22 | #include "pinctrl-qcom.h" |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 23 | |
Caleb Connolly | ab1c889 | 2025-04-10 10:52:38 +0200 | [diff] [blame] | 24 | #define MSM_PINCTRL_MAX_PINS 256 |
| 25 | |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 26 | struct msm_pinctrl_priv { |
| 27 | phys_addr_t base; |
| 28 | struct msm_pinctrl_data *data; |
Caleb Connolly | ab1c889 | 2025-04-10 10:52:38 +0200 | [diff] [blame] | 29 | DECLARE_BITMAP(reserved_map, MSM_PINCTRL_MAX_PINS); |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 30 | }; |
| 31 | |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 32 | #define GPIO_CONFIG_REG(priv, x) \ |
| 33 | (qcom_pin_offset((priv)->data->pin_data.pin_offsets, x)) |
| 34 | |
Sumit Garg | adc3bcb | 2024-04-12 15:24:35 +0530 | [diff] [blame] | 35 | #define GPIO_IN_OUT_REG(priv, x) \ |
| 36 | (GPIO_CONFIG_REG(priv, x) + 0x4) |
| 37 | |
| 38 | #define TLMM_GPIO_PULL_MASK GENMASK(1, 0) |
| 39 | #define TLMM_FUNC_SEL_MASK GENMASK(5, 2) |
| 40 | #define TLMM_DRV_STRENGTH_MASK GENMASK(8, 6) |
| 41 | #define TLMM_GPIO_OUTPUT_MASK BIT(1) |
| 42 | #define TLMM_GPIO_OE_MASK BIT(9) |
| 43 | |
| 44 | /* GPIO register shifts. */ |
| 45 | #define GPIO_OUT_SHIFT 1 |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 46 | |
| 47 | static const struct pinconf_param msm_conf_params[] = { |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 48 | { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 2 }, |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 49 | { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 50 | { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 3 }, |
Neil Armstrong | d3eed4b | 2024-05-28 10:31:53 +0200 | [diff] [blame] | 51 | { "bias-pull-down", PIN_CONFIG_BIAS_PULL_UP, 1 }, |
Sumit Garg | adc3bcb | 2024-04-12 15:24:35 +0530 | [diff] [blame] | 52 | { "output-high", PIN_CONFIG_OUTPUT, 1, }, |
| 53 | { "output-low", PIN_CONFIG_OUTPUT, 0, }, |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static int msm_get_functions_count(struct udevice *dev) |
| 57 | { |
| 58 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 59 | |
| 60 | return priv->data->functions_count; |
| 61 | } |
| 62 | |
| 63 | static int msm_get_pins_count(struct udevice *dev) |
| 64 | { |
| 65 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 66 | |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 67 | return priv->data->pin_data.pin_count; |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static const char *msm_get_function_name(struct udevice *dev, |
| 71 | unsigned int selector) |
| 72 | { |
| 73 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 74 | |
| 75 | return priv->data->get_function_name(dev, selector); |
| 76 | } |
| 77 | |
Caleb Connolly | ab1c889 | 2025-04-10 10:52:38 +0200 | [diff] [blame] | 78 | static int msm_pinctrl_parse_ranges(struct udevice *dev) |
| 79 | { |
| 80 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 81 | ofnode node = dev_ofnode(dev); |
| 82 | int ret, count, i; |
| 83 | u32 *ranges; |
| 84 | |
| 85 | if (ofnode_read_prop(node, "gpio-reserved-ranges", &count)) { |
| 86 | if (count % 2 == 1) { |
| 87 | dev_err(dev, "gpio-reserved-ranges must be a multiple of 2\n"); |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
| 91 | ranges = malloc(count); |
| 92 | if (!ranges) |
| 93 | return -ENOMEM; |
| 94 | |
| 95 | ret = ofnode_read_u32_array(node, "gpio-reserved-ranges", ranges, count / 4); |
| 96 | if (ret) { |
| 97 | dev_err(dev, "failed to read gpio-reserved-ranges array (%d)\n", ret); |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | for (i = 0; i < count / 4; i += 2) { |
| 102 | if (ranges[i] >= MSM_PINCTRL_MAX_PINS || |
| 103 | (ranges[i] + ranges[i + 1]) >= MSM_PINCTRL_MAX_PINS) { |
| 104 | dev_err(dev, "invalid reserved-range (%d;%d)\n", |
| 105 | ranges[i], ranges[i + 1]); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | |
| 109 | bitmap_set(priv->reserved_map, ranges[i], ranges[i + 1]); |
| 110 | } |
| 111 | |
| 112 | free(ranges); |
| 113 | } |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 118 | static int msm_pinctrl_probe(struct udevice *dev) |
| 119 | { |
| 120 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
Caleb Connolly | ab1c889 | 2025-04-10 10:52:38 +0200 | [diff] [blame] | 121 | int ret; |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 122 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 123 | priv->base = dev_read_addr(dev); |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 124 | priv->data = (struct msm_pinctrl_data *)dev_get_driver_data(dev); |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 125 | |
Caleb Connolly | ab1c889 | 2025-04-10 10:52:38 +0200 | [diff] [blame] | 126 | ret = msm_pinctrl_parse_ranges(dev); |
| 127 | if (ret) { |
| 128 | printf("Couldn't parse reserved GPIO ranges!\n"); |
| 129 | return ret; |
| 130 | } |
| 131 | |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 132 | return priv->base == FDT_ADDR_T_NONE ? -EINVAL : 0; |
| 133 | } |
| 134 | |
| 135 | static const char *msm_get_pin_name(struct udevice *dev, unsigned int selector) |
| 136 | { |
| 137 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 138 | |
| 139 | return priv->data->get_pin_name(dev, selector); |
| 140 | } |
| 141 | |
| 142 | static int msm_pinmux_set(struct udevice *dev, unsigned int pin_selector, |
| 143 | unsigned int func_selector) |
| 144 | { |
| 145 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
Varadarajan Narayanan | e7f07e0 | 2025-02-26 12:15:02 +0530 | [diff] [blame] | 146 | int func = priv->data->get_function_mux(pin_selector, func_selector); |
| 147 | |
| 148 | if (func < 0) |
| 149 | return func; |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 150 | |
Caleb Connolly | ab1c889 | 2025-04-10 10:52:38 +0200 | [diff] [blame] | 151 | if (msm_pinctrl_is_reserved(dev, pin_selector)) |
| 152 | return -EPERM; |
| 153 | |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 154 | /* Always NOP for special pins, assume they're in the correct state */ |
| 155 | if (qcom_is_special_pin(&priv->data->pin_data, pin_selector)) |
| 156 | return 0; |
| 157 | |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 158 | clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Sumit Garg | adc3bcb | 2024-04-12 15:24:35 +0530 | [diff] [blame] | 159 | TLMM_FUNC_SEL_MASK | TLMM_GPIO_OE_MASK, func << 2); |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Neil Armstrong | 0154d92 | 2024-05-28 10:31:55 +0200 | [diff] [blame] | 163 | static int msm_pinconf_set_special(struct msm_pinctrl_priv *priv, unsigned int pin_selector, |
| 164 | unsigned int param, unsigned int argument) |
| 165 | { |
| 166 | unsigned int offset = pin_selector - priv->data->pin_data.special_pins_start; |
| 167 | const struct msm_special_pin_data *data; |
| 168 | |
| 169 | if (!priv->data->pin_data.special_pins_data) |
| 170 | return 0; |
| 171 | |
| 172 | data = &priv->data->pin_data.special_pins_data[offset]; |
| 173 | |
| 174 | switch (param) { |
| 175 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 176 | argument = (argument / 2) - 1; |
| 177 | clrsetbits_le32(priv->base + data->ctl_reg, |
| 178 | GENMASK(2, 0) << data->drv_bit, |
| 179 | argument << data->drv_bit); |
| 180 | break; |
| 181 | case PIN_CONFIG_BIAS_DISABLE: |
| 182 | clrbits_le32(priv->base + data->ctl_reg, |
| 183 | TLMM_GPIO_PULL_MASK << data->pull_bit); |
| 184 | break; |
| 185 | case PIN_CONFIG_BIAS_PULL_UP: |
| 186 | clrsetbits_le32(priv->base + data->ctl_reg, |
| 187 | TLMM_GPIO_PULL_MASK << data->pull_bit, |
| 188 | argument << data->pull_bit); |
| 189 | break; |
| 190 | default: |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 197 | static int msm_pinconf_set(struct udevice *dev, unsigned int pin_selector, |
| 198 | unsigned int param, unsigned int argument) |
| 199 | { |
| 200 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 201 | |
Caleb Connolly | ab1c889 | 2025-04-10 10:52:38 +0200 | [diff] [blame] | 202 | if (msm_pinctrl_is_reserved(dev, pin_selector)) |
| 203 | return -EPERM; |
| 204 | |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 205 | if (qcom_is_special_pin(&priv->data->pin_data, pin_selector)) |
Neil Armstrong | 0154d92 | 2024-05-28 10:31:55 +0200 | [diff] [blame] | 206 | return msm_pinconf_set_special(priv, pin_selector, param, argument); |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 207 | |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 208 | switch (param) { |
| 209 | case PIN_CONFIG_DRIVE_STRENGTH: |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 210 | argument = (argument / 2) - 1; |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 211 | clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 212 | TLMM_DRV_STRENGTH_MASK, argument << 6); |
| 213 | break; |
| 214 | case PIN_CONFIG_BIAS_DISABLE: |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 215 | clrbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 216 | TLMM_GPIO_PULL_MASK); |
| 217 | break; |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 218 | case PIN_CONFIG_BIAS_PULL_UP: |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 219 | clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 220 | TLMM_GPIO_PULL_MASK, argument); |
| 221 | break; |
Sumit Garg | adc3bcb | 2024-04-12 15:24:35 +0530 | [diff] [blame] | 222 | case PIN_CONFIG_OUTPUT: |
| 223 | writel(argument << GPIO_OUT_SHIFT, |
| 224 | priv->base + GPIO_IN_OUT_REG(priv, pin_selector)); |
| 225 | setbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
| 226 | TLMM_GPIO_OE_MASK); |
| 227 | break; |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 228 | default: |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 235 | struct pinctrl_ops msm_pinctrl_ops = { |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 236 | .get_pins_count = msm_get_pins_count, |
| 237 | .get_pin_name = msm_get_pin_name, |
| 238 | .set_state = pinctrl_generic_set_state, |
| 239 | .pinmux_set = msm_pinmux_set, |
| 240 | .pinconf_num_params = ARRAY_SIZE(msm_conf_params), |
| 241 | .pinconf_params = msm_conf_params, |
| 242 | .pinconf_set = msm_pinconf_set, |
| 243 | .get_functions_count = msm_get_functions_count, |
| 244 | .get_function_name = msm_get_function_name, |
| 245 | }; |
| 246 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 247 | int msm_pinctrl_bind(struct udevice *dev) |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 248 | { |
| 249 | ofnode node = dev_ofnode(dev); |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 250 | struct msm_pinctrl_data *data = (struct msm_pinctrl_data *)dev_get_driver_data(dev); |
| 251 | struct driver *drv; |
| 252 | struct udevice *pinctrl_dev; |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 253 | const char *name; |
| 254 | int ret; |
| 255 | |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 256 | if (!data->pin_data.special_pins_start) |
| 257 | dev_warn(dev, "Special pins start index not defined!\n"); |
| 258 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 259 | drv = lists_driver_lookup_name("pinctrl_qcom"); |
| 260 | if (!drv) |
| 261 | return -ENOENT; |
| 262 | |
| 263 | ret = device_bind_with_driver_data(dev_get_parent(dev), drv, ofnode_get_name(node), (ulong)data, |
| 264 | dev_ofnode(dev), &pinctrl_dev); |
| 265 | if (ret) |
| 266 | return ret; |
| 267 | |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 268 | ofnode_get_property(node, "gpio-controller", &ret); |
| 269 | if (ret < 0) |
| 270 | return 0; |
| 271 | |
| 272 | /* Get the name of gpio node */ |
| 273 | name = ofnode_get_name(node); |
| 274 | if (!name) |
| 275 | return -EINVAL; |
| 276 | |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 277 | drv = lists_driver_lookup_name("gpio_msm"); |
| 278 | if (!drv) { |
| 279 | printf("Can't find gpio_msm driver\n"); |
| 280 | return -ENODEV; |
| 281 | } |
| 282 | |
| 283 | /* Bind gpio device as a child of the pinctrl device */ |
| 284 | ret = device_bind_with_driver_data(pinctrl_dev, drv, |
| 285 | name, (ulong)&data->pin_data, node, NULL); |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 286 | if (ret) { |
| 287 | device_unbind(pinctrl_dev); |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 288 | return ret; |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 289 | } |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 294 | U_BOOT_DRIVER(pinctrl_qcom) = { |
| 295 | .name = "pinctrl_qcom", |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 296 | .id = UCLASS_PINCTRL, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 297 | .priv_auto = sizeof(struct msm_pinctrl_priv), |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 298 | .ops = &msm_pinctrl_ops, |
| 299 | .probe = msm_pinctrl_probe, |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 300 | }; |
Caleb Connolly | ab1c889 | 2025-04-10 10:52:38 +0200 | [diff] [blame] | 301 | |
| 302 | bool msm_pinctrl_is_reserved(struct udevice *dev, unsigned int pin) |
| 303 | { |
| 304 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 305 | |
| 306 | if (pin >= MSM_PINCTRL_MAX_PINS) |
| 307 | return false; |
| 308 | |
| 309 | return test_bit(pin, priv->reserved_map); |
| 310 | } |