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 | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 18 | #include <linux/bug.h> |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 19 | #include <mach/gpio.h> |
| 20 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 21 | #include "pinctrl-qcom.h" |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 22 | |
| 23 | struct msm_pinctrl_priv { |
| 24 | phys_addr_t base; |
| 25 | struct msm_pinctrl_data *data; |
| 26 | }; |
| 27 | |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 28 | #define GPIO_CONFIG_REG(priv, x) \ |
| 29 | (qcom_pin_offset((priv)->data->pin_data.pin_offsets, x)) |
| 30 | |
Sumit Garg | adc3bcb | 2024-04-12 15:24:35 +0530 | [diff] [blame] | 31 | #define GPIO_IN_OUT_REG(priv, x) \ |
| 32 | (GPIO_CONFIG_REG(priv, x) + 0x4) |
| 33 | |
| 34 | #define TLMM_GPIO_PULL_MASK GENMASK(1, 0) |
| 35 | #define TLMM_FUNC_SEL_MASK GENMASK(5, 2) |
| 36 | #define TLMM_DRV_STRENGTH_MASK GENMASK(8, 6) |
| 37 | #define TLMM_GPIO_OUTPUT_MASK BIT(1) |
| 38 | #define TLMM_GPIO_OE_MASK BIT(9) |
| 39 | |
| 40 | /* GPIO register shifts. */ |
| 41 | #define GPIO_OUT_SHIFT 1 |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 42 | |
| 43 | static const struct pinconf_param msm_conf_params[] = { |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 44 | { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 2 }, |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 45 | { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 }, |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 46 | { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 3 }, |
Neil Armstrong | d3eed4b | 2024-05-28 10:31:53 +0200 | [diff] [blame] | 47 | { "bias-pull-down", PIN_CONFIG_BIAS_PULL_UP, 1 }, |
Sumit Garg | adc3bcb | 2024-04-12 15:24:35 +0530 | [diff] [blame] | 48 | { "output-high", PIN_CONFIG_OUTPUT, 1, }, |
| 49 | { "output-low", PIN_CONFIG_OUTPUT, 0, }, |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static int msm_get_functions_count(struct udevice *dev) |
| 53 | { |
| 54 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 55 | |
| 56 | return priv->data->functions_count; |
| 57 | } |
| 58 | |
| 59 | static int msm_get_pins_count(struct udevice *dev) |
| 60 | { |
| 61 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 62 | |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 63 | return priv->data->pin_data.pin_count; |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static const char *msm_get_function_name(struct udevice *dev, |
| 67 | unsigned int selector) |
| 68 | { |
| 69 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 70 | |
| 71 | return priv->data->get_function_name(dev, selector); |
| 72 | } |
| 73 | |
| 74 | static int msm_pinctrl_probe(struct udevice *dev) |
| 75 | { |
| 76 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 77 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 78 | priv->base = dev_read_addr(dev); |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 79 | priv->data = (struct msm_pinctrl_data *)dev_get_driver_data(dev); |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 80 | |
| 81 | return priv->base == FDT_ADDR_T_NONE ? -EINVAL : 0; |
| 82 | } |
| 83 | |
| 84 | static const char *msm_get_pin_name(struct udevice *dev, unsigned int selector) |
| 85 | { |
| 86 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 87 | |
| 88 | return priv->data->get_pin_name(dev, selector); |
| 89 | } |
| 90 | |
| 91 | static int msm_pinmux_set(struct udevice *dev, unsigned int pin_selector, |
| 92 | unsigned int func_selector) |
| 93 | { |
| 94 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
Volodymyr Babchuk | c4cc979 | 2024-03-11 21:33:46 +0000 | [diff] [blame] | 95 | u32 func = priv->data->get_function_mux(pin_selector, func_selector); |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 96 | |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 97 | /* Always NOP for special pins, assume they're in the correct state */ |
| 98 | if (qcom_is_special_pin(&priv->data->pin_data, pin_selector)) |
| 99 | return 0; |
| 100 | |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 101 | clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Sumit Garg | adc3bcb | 2024-04-12 15:24:35 +0530 | [diff] [blame] | 102 | TLMM_FUNC_SEL_MASK | TLMM_GPIO_OE_MASK, func << 2); |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
Neil Armstrong | 0154d92 | 2024-05-28 10:31:55 +0200 | [diff] [blame] | 106 | static int msm_pinconf_set_special(struct msm_pinctrl_priv *priv, unsigned int pin_selector, |
| 107 | unsigned int param, unsigned int argument) |
| 108 | { |
| 109 | unsigned int offset = pin_selector - priv->data->pin_data.special_pins_start; |
| 110 | const struct msm_special_pin_data *data; |
| 111 | |
| 112 | if (!priv->data->pin_data.special_pins_data) |
| 113 | return 0; |
| 114 | |
| 115 | data = &priv->data->pin_data.special_pins_data[offset]; |
| 116 | |
| 117 | switch (param) { |
| 118 | case PIN_CONFIG_DRIVE_STRENGTH: |
| 119 | argument = (argument / 2) - 1; |
| 120 | clrsetbits_le32(priv->base + data->ctl_reg, |
| 121 | GENMASK(2, 0) << data->drv_bit, |
| 122 | argument << data->drv_bit); |
| 123 | break; |
| 124 | case PIN_CONFIG_BIAS_DISABLE: |
| 125 | clrbits_le32(priv->base + data->ctl_reg, |
| 126 | TLMM_GPIO_PULL_MASK << data->pull_bit); |
| 127 | break; |
| 128 | case PIN_CONFIG_BIAS_PULL_UP: |
| 129 | clrsetbits_le32(priv->base + data->ctl_reg, |
| 130 | TLMM_GPIO_PULL_MASK << data->pull_bit, |
| 131 | argument << data->pull_bit); |
| 132 | break; |
| 133 | default: |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 140 | static int msm_pinconf_set(struct udevice *dev, unsigned int pin_selector, |
| 141 | unsigned int param, unsigned int argument) |
| 142 | { |
| 143 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 144 | |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 145 | if (qcom_is_special_pin(&priv->data->pin_data, pin_selector)) |
Neil Armstrong | 0154d92 | 2024-05-28 10:31:55 +0200 | [diff] [blame] | 146 | return msm_pinconf_set_special(priv, pin_selector, param, argument); |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 147 | |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 148 | switch (param) { |
| 149 | case PIN_CONFIG_DRIVE_STRENGTH: |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 150 | argument = (argument / 2) - 1; |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 151 | clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 152 | TLMM_DRV_STRENGTH_MASK, argument << 6); |
| 153 | break; |
| 154 | case PIN_CONFIG_BIAS_DISABLE: |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 155 | clrbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 156 | TLMM_GPIO_PULL_MASK); |
| 157 | break; |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 158 | case PIN_CONFIG_BIAS_PULL_UP: |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 159 | clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 160 | TLMM_GPIO_PULL_MASK, argument); |
| 161 | break; |
Sumit Garg | adc3bcb | 2024-04-12 15:24:35 +0530 | [diff] [blame] | 162 | case PIN_CONFIG_OUTPUT: |
| 163 | writel(argument << GPIO_OUT_SHIFT, |
| 164 | priv->base + GPIO_IN_OUT_REG(priv, pin_selector)); |
| 165 | setbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
| 166 | TLMM_GPIO_OE_MASK); |
| 167 | break; |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 168 | default: |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 175 | struct pinctrl_ops msm_pinctrl_ops = { |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 176 | .get_pins_count = msm_get_pins_count, |
| 177 | .get_pin_name = msm_get_pin_name, |
| 178 | .set_state = pinctrl_generic_set_state, |
| 179 | .pinmux_set = msm_pinmux_set, |
| 180 | .pinconf_num_params = ARRAY_SIZE(msm_conf_params), |
| 181 | .pinconf_params = msm_conf_params, |
| 182 | .pinconf_set = msm_pinconf_set, |
| 183 | .get_functions_count = msm_get_functions_count, |
| 184 | .get_function_name = msm_get_function_name, |
| 185 | }; |
| 186 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 187 | int msm_pinctrl_bind(struct udevice *dev) |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 188 | { |
| 189 | ofnode node = dev_ofnode(dev); |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 190 | struct msm_pinctrl_data *data = (struct msm_pinctrl_data *)dev_get_driver_data(dev); |
| 191 | struct driver *drv; |
| 192 | struct udevice *pinctrl_dev; |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 193 | const char *name; |
| 194 | int ret; |
| 195 | |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 196 | if (!data->pin_data.special_pins_start) |
| 197 | dev_warn(dev, "Special pins start index not defined!\n"); |
| 198 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 199 | drv = lists_driver_lookup_name("pinctrl_qcom"); |
| 200 | if (!drv) |
| 201 | return -ENOENT; |
| 202 | |
| 203 | ret = device_bind_with_driver_data(dev_get_parent(dev), drv, ofnode_get_name(node), (ulong)data, |
| 204 | dev_ofnode(dev), &pinctrl_dev); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 208 | ofnode_get_property(node, "gpio-controller", &ret); |
| 209 | if (ret < 0) |
| 210 | return 0; |
| 211 | |
| 212 | /* Get the name of gpio node */ |
| 213 | name = ofnode_get_name(node); |
| 214 | if (!name) |
| 215 | return -EINVAL; |
| 216 | |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 217 | drv = lists_driver_lookup_name("gpio_msm"); |
| 218 | if (!drv) { |
| 219 | printf("Can't find gpio_msm driver\n"); |
| 220 | return -ENODEV; |
| 221 | } |
| 222 | |
| 223 | /* Bind gpio device as a child of the pinctrl device */ |
| 224 | ret = device_bind_with_driver_data(pinctrl_dev, drv, |
| 225 | name, (ulong)&data->pin_data, node, NULL); |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 226 | if (ret) { |
| 227 | device_unbind(pinctrl_dev); |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 228 | return ret; |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 229 | } |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 234 | U_BOOT_DRIVER(pinctrl_qcom) = { |
| 235 | .name = "pinctrl_qcom", |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 236 | .id = UCLASS_PINCTRL, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 237 | .priv_auto = sizeof(struct msm_pinctrl_priv), |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 238 | .ops = &msm_pinctrl_ops, |
| 239 | .probe = msm_pinctrl_probe, |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 240 | }; |