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 | |
| 106 | static int msm_pinconf_set(struct udevice *dev, unsigned int pin_selector, |
| 107 | unsigned int param, unsigned int argument) |
| 108 | { |
| 109 | struct msm_pinctrl_priv *priv = dev_get_priv(dev); |
| 110 | |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 111 | /* Always NOP for special pins */ |
| 112 | if (qcom_is_special_pin(&priv->data->pin_data, pin_selector)) |
| 113 | return 0; |
| 114 | |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 115 | switch (param) { |
| 116 | case PIN_CONFIG_DRIVE_STRENGTH: |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 117 | argument = (argument / 2) - 1; |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 118 | clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 119 | TLMM_DRV_STRENGTH_MASK, argument << 6); |
| 120 | break; |
| 121 | case PIN_CONFIG_BIAS_DISABLE: |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 122 | clrbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 123 | TLMM_GPIO_PULL_MASK); |
| 124 | break; |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 125 | case PIN_CONFIG_BIAS_PULL_UP: |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 126 | clrsetbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
Sumit Garg | fd1ad93 | 2023-02-01 19:28:52 +0530 | [diff] [blame] | 127 | TLMM_GPIO_PULL_MASK, argument); |
| 128 | break; |
Sumit Garg | adc3bcb | 2024-04-12 15:24:35 +0530 | [diff] [blame] | 129 | case PIN_CONFIG_OUTPUT: |
| 130 | writel(argument << GPIO_OUT_SHIFT, |
| 131 | priv->base + GPIO_IN_OUT_REG(priv, pin_selector)); |
| 132 | setbits_le32(priv->base + GPIO_CONFIG_REG(priv, pin_selector), |
| 133 | TLMM_GPIO_OE_MASK); |
| 134 | break; |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 135 | default: |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 142 | struct pinctrl_ops msm_pinctrl_ops = { |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 143 | .get_pins_count = msm_get_pins_count, |
| 144 | .get_pin_name = msm_get_pin_name, |
| 145 | .set_state = pinctrl_generic_set_state, |
| 146 | .pinmux_set = msm_pinmux_set, |
| 147 | .pinconf_num_params = ARRAY_SIZE(msm_conf_params), |
| 148 | .pinconf_params = msm_conf_params, |
| 149 | .pinconf_set = msm_pinconf_set, |
| 150 | .get_functions_count = msm_get_functions_count, |
| 151 | .get_function_name = msm_get_function_name, |
| 152 | }; |
| 153 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 154 | int msm_pinctrl_bind(struct udevice *dev) |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 155 | { |
| 156 | ofnode node = dev_ofnode(dev); |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 157 | struct msm_pinctrl_data *data = (struct msm_pinctrl_data *)dev_get_driver_data(dev); |
| 158 | struct driver *drv; |
| 159 | struct udevice *pinctrl_dev; |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 160 | const char *name; |
| 161 | int ret; |
| 162 | |
Caleb Connolly | 190005c | 2024-02-26 17:26:17 +0000 | [diff] [blame] | 163 | if (!data->pin_data.special_pins_start) |
| 164 | dev_warn(dev, "Special pins start index not defined!\n"); |
| 165 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 166 | drv = lists_driver_lookup_name("pinctrl_qcom"); |
| 167 | if (!drv) |
| 168 | return -ENOENT; |
| 169 | |
| 170 | ret = device_bind_with_driver_data(dev_get_parent(dev), drv, ofnode_get_name(node), (ulong)data, |
| 171 | dev_ofnode(dev), &pinctrl_dev); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 175 | ofnode_get_property(node, "gpio-controller", &ret); |
| 176 | if (ret < 0) |
| 177 | return 0; |
| 178 | |
| 179 | /* Get the name of gpio node */ |
| 180 | name = ofnode_get_name(node); |
| 181 | if (!name) |
| 182 | return -EINVAL; |
| 183 | |
Caleb Connolly | fabb897 | 2023-11-14 12:55:42 +0000 | [diff] [blame] | 184 | drv = lists_driver_lookup_name("gpio_msm"); |
| 185 | if (!drv) { |
| 186 | printf("Can't find gpio_msm driver\n"); |
| 187 | return -ENODEV; |
| 188 | } |
| 189 | |
| 190 | /* Bind gpio device as a child of the pinctrl device */ |
| 191 | ret = device_bind_with_driver_data(pinctrl_dev, drv, |
| 192 | name, (ulong)&data->pin_data, node, NULL); |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 193 | if (ret) { |
| 194 | device_unbind(pinctrl_dev); |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 195 | return ret; |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 196 | } |
Sumit Garg | b7572e5 | 2022-07-27 13:52:04 +0530 | [diff] [blame] | 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Caleb Connolly | 506eb53 | 2023-11-14 12:55:40 +0000 | [diff] [blame] | 201 | U_BOOT_DRIVER(pinctrl_qcom) = { |
| 202 | .name = "pinctrl_qcom", |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 203 | .id = UCLASS_PINCTRL, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 204 | .priv_auto = sizeof(struct msm_pinctrl_priv), |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 205 | .ops = &msm_pinctrl_ops, |
| 206 | .probe = msm_pinctrl_probe, |
Ramon Fried | e43d8e7 | 2018-05-16 12:13:40 +0300 | [diff] [blame] | 207 | }; |