Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 2 | /* |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 3 | * Qualcomm generic pmic gpio driver |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 4 | * |
| 5 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 10 | #include <dm/device-internal.h> |
| 11 | #include <dm/lists.h> |
| 12 | #include <dm/pinctrl.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 14 | #include <power/pmic.h> |
| 15 | #include <spmi/spmi.h> |
| 16 | #include <asm/io.h> |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 17 | #include <stdlib.h> |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 18 | #include <asm/gpio.h> |
| 19 | #include <linux/bitops.h> |
| 20 | |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 21 | /* Register offset for each gpio */ |
| 22 | #define REG_OFFSET(x) ((x) * 0x100) |
| 23 | |
| 24 | /* Register maps */ |
| 25 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 26 | /* Type and subtype are shared for all PMIC peripherals */ |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 27 | #define REG_TYPE 0x4 |
| 28 | #define REG_SUBTYPE 0x5 |
| 29 | |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 30 | /* GPIO peripheral type and subtype out_values */ |
| 31 | #define REG_TYPE_VAL 0x10 |
| 32 | #define REG_SUBTYPE_GPIO_4CH 0x1 |
| 33 | #define REG_SUBTYPE_GPIOC_4CH 0x5 |
| 34 | #define REG_SUBTYPE_GPIO_8CH 0x9 |
| 35 | #define REG_SUBTYPE_GPIOC_8CH 0xd |
| 36 | #define REG_SUBTYPE_GPIO_LV 0x10 |
| 37 | #define REG_SUBTYPE_GPIO_MV 0x11 |
Neil Armstrong | d44be41 | 2024-04-10 17:59:43 +0200 | [diff] [blame] | 38 | #define REG_SUBTYPE_GPIO_LV_VIN2 0x12 |
| 39 | #define REG_SUBTYPE_GPIO_MV_VIN3 0x13 |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 40 | |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 41 | #define REG_STATUS 0x08 |
| 42 | #define REG_STATUS_VAL_MASK 0x1 |
| 43 | |
| 44 | /* MODE_CTL */ |
Jorge Ramirez-Ortiz | 3556161 | 2018-01-10 11:33:51 +0100 | [diff] [blame] | 45 | #define REG_CTL 0x40 |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 46 | #define REG_CTL_MODE_MASK 0x70 |
| 47 | #define REG_CTL_MODE_INPUT 0x00 |
| 48 | #define REG_CTL_MODE_INOUT 0x20 |
| 49 | #define REG_CTL_MODE_OUTPUT 0x10 |
| 50 | #define REG_CTL_OUTPUT_MASK 0x0F |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 51 | #define REG_CTL_LV_MV_MODE_MASK 0x3 |
| 52 | #define REG_CTL_LV_MV_MODE_INPUT 0x0 |
| 53 | #define REG_CTL_LV_MV_MODE_INOUT 0x2 |
| 54 | #define REG_CTL_LV_MV_MODE_OUTPUT 0x1 |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 55 | |
| 56 | #define REG_DIG_VIN_CTL 0x41 |
| 57 | #define REG_DIG_VIN_VIN0 0 |
| 58 | |
| 59 | #define REG_DIG_PULL_CTL 0x42 |
| 60 | #define REG_DIG_PULL_NO_PU 0x5 |
| 61 | |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 62 | #define REG_LV_MV_OUTPUT_CTL 0x44 |
| 63 | #define REG_LV_MV_OUTPUT_CTL_MASK 0x80 |
| 64 | #define REG_LV_MV_OUTPUT_CTL_SHIFT 7 |
| 65 | |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 66 | #define REG_DIG_OUT_CTL 0x45 |
| 67 | #define REG_DIG_OUT_CTL_CMOS (0x0 << 4) |
| 68 | #define REG_DIG_OUT_CTL_DRIVE_L 0x1 |
| 69 | |
| 70 | #define REG_EN_CTL 0x46 |
| 71 | #define REG_EN_CTL_ENABLE (1 << 7) |
| 72 | |
Caleb Connolly | 0b9e500 | 2024-02-26 17:26:14 +0000 | [diff] [blame] | 73 | /** |
| 74 | * pmic_gpio_match_data - platform specific configuration |
| 75 | * |
| 76 | * @PMIC_MATCH_READONLY: treat all GPIOs as readonly, don't attempt to configure them. |
| 77 | * This is a workaround for an unknown bug on some platforms where trying to write the |
| 78 | * GPIO configuration registers causes the board to hang. |
| 79 | */ |
| 80 | enum pmic_gpio_quirks { |
| 81 | QCOM_PMIC_QUIRK_READONLY = (1 << 0), |
| 82 | }; |
| 83 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 84 | struct qcom_pmic_gpio_data { |
Tom Rini | 44d15c2 | 2016-04-12 15:11:23 -0400 | [diff] [blame] | 85 | uint32_t pid; /* Peripheral ID on SPMI bus */ |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 86 | bool lv_mv_type; /* If subtype is GPIO_LV(0x10) or GPIO_MV(0x11) */ |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 87 | u32 pin_count; |
| 88 | struct udevice *pmic; /* Reference to pmic device for read/write */ |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 91 | /* dev can be the GPIO or pinctrl device */ |
| 92 | static int _qcom_gpio_set_direction(struct udevice *dev, u32 offset, bool input, int value) |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 93 | { |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 94 | struct qcom_pmic_gpio_data *plat = dev_get_plat(dev); |
| 95 | u32 gpio_base = plat->pid + REG_OFFSET(offset); |
| 96 | u32 reg_ctl_val; |
Caleb Connolly | 0b9e500 | 2024-02-26 17:26:14 +0000 | [diff] [blame] | 97 | int ret = 0; |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 98 | |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 99 | /* Select the mode and output */ |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 100 | if (plat->lv_mv_type) { |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 101 | if (input) |
| 102 | reg_ctl_val = REG_CTL_LV_MV_MODE_INPUT; |
| 103 | else |
| 104 | reg_ctl_val = REG_CTL_LV_MV_MODE_INOUT; |
| 105 | } else { |
| 106 | if (input) |
| 107 | reg_ctl_val = REG_CTL_MODE_INPUT; |
| 108 | else |
| 109 | reg_ctl_val = REG_CTL_MODE_INOUT | !!value; |
| 110 | } |
| 111 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 112 | ret = pmic_reg_write(plat->pmic, gpio_base + REG_CTL, reg_ctl_val); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 113 | if (ret < 0) |
| 114 | return ret; |
| 115 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 116 | if (plat->lv_mv_type && !input) { |
| 117 | ret = pmic_reg_write(plat->pmic, |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 118 | gpio_base + REG_LV_MV_OUTPUT_CTL, |
| 119 | !!value << REG_LV_MV_OUTPUT_CTL_SHIFT); |
| 120 | if (ret < 0) |
| 121 | return ret; |
| 122 | } |
| 123 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int qcom_gpio_set_direction(struct udevice *dev, unsigned int offset, |
| 128 | bool input, int value) |
| 129 | { |
| 130 | struct qcom_pmic_gpio_data *plat = dev_get_plat(dev); |
| 131 | uint32_t gpio_base = plat->pid + REG_OFFSET(offset); |
| 132 | ulong quirks = dev_get_driver_data(dev); |
| 133 | int ret = 0; |
| 134 | |
| 135 | /* Some PMICs don't like their GPIOs being configured */ |
| 136 | if (quirks & QCOM_PMIC_QUIRK_READONLY) |
| 137 | return 0; |
| 138 | |
| 139 | /* Disable the GPIO */ |
| 140 | ret = pmic_clrsetbits(dev->parent, gpio_base + REG_EN_CTL, |
| 141 | REG_EN_CTL_ENABLE, 0); |
| 142 | if (ret < 0) |
| 143 | return ret; |
| 144 | |
| 145 | _qcom_gpio_set_direction(dev, offset, input, value); |
| 146 | |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 147 | /* Set the right pull (no pull) */ |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 148 | ret = pmic_reg_write(plat->pmic, gpio_base + REG_DIG_PULL_CTL, |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 149 | REG_DIG_PULL_NO_PU); |
| 150 | if (ret < 0) |
| 151 | return ret; |
| 152 | |
| 153 | /* Configure output pin drivers if needed */ |
| 154 | if (!input) { |
| 155 | /* Select the VIN - VIN0, pin is input so it doesn't matter */ |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 156 | ret = pmic_reg_write(plat->pmic, gpio_base + REG_DIG_VIN_CTL, |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 157 | REG_DIG_VIN_VIN0); |
| 158 | if (ret < 0) |
| 159 | return ret; |
| 160 | |
| 161 | /* Set the right dig out control */ |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 162 | ret = pmic_reg_write(plat->pmic, gpio_base + REG_DIG_OUT_CTL, |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 163 | REG_DIG_OUT_CTL_CMOS | |
| 164 | REG_DIG_OUT_CTL_DRIVE_L); |
| 165 | if (ret < 0) |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | /* Enable the GPIO */ |
| 170 | return pmic_clrsetbits(dev->parent, gpio_base + REG_EN_CTL, 0, |
| 171 | REG_EN_CTL_ENABLE); |
| 172 | } |
| 173 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 174 | static int qcom_gpio_direction_input(struct udevice *dev, unsigned offset) |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 175 | { |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 176 | return qcom_gpio_set_direction(dev, offset, true, 0); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 179 | static int qcom_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 180 | int value) |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 181 | { |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 182 | return qcom_gpio_set_direction(dev, offset, false, value); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 185 | static int qcom_gpio_get_function(struct udevice *dev, unsigned offset) |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 186 | { |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 187 | struct qcom_pmic_gpio_data *plat = dev_get_plat(dev); |
| 188 | uint32_t gpio_base = plat->pid + REG_OFFSET(offset); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 189 | int reg; |
| 190 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 191 | reg = pmic_reg_read(plat->pmic, gpio_base + REG_CTL); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 192 | if (reg < 0) |
| 193 | return reg; |
| 194 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 195 | if (plat->lv_mv_type) { |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 196 | switch (reg & REG_CTL_LV_MV_MODE_MASK) { |
| 197 | case REG_CTL_LV_MV_MODE_INPUT: |
| 198 | return GPIOF_INPUT; |
| 199 | case REG_CTL_LV_MV_MODE_INOUT: /* Fallthrough */ |
| 200 | case REG_CTL_LV_MV_MODE_OUTPUT: |
| 201 | return GPIOF_OUTPUT; |
| 202 | default: |
| 203 | return GPIOF_UNKNOWN; |
| 204 | } |
| 205 | } else { |
| 206 | switch (reg & REG_CTL_MODE_MASK) { |
| 207 | case REG_CTL_MODE_INPUT: |
| 208 | return GPIOF_INPUT; |
| 209 | case REG_CTL_MODE_INOUT: /* Fallthrough */ |
| 210 | case REG_CTL_MODE_OUTPUT: |
| 211 | return GPIOF_OUTPUT; |
| 212 | default: |
| 213 | return GPIOF_UNKNOWN; |
| 214 | } |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 218 | static int qcom_gpio_get_value(struct udevice *dev, unsigned offset) |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 219 | { |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 220 | struct qcom_pmic_gpio_data *plat = dev_get_plat(dev); |
| 221 | uint32_t gpio_base = plat->pid + REG_OFFSET(offset); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 222 | int reg; |
| 223 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 224 | reg = pmic_reg_read(plat->pmic, gpio_base + REG_STATUS); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 225 | if (reg < 0) |
| 226 | return reg; |
| 227 | |
| 228 | return !!(reg & REG_STATUS_VAL_MASK); |
| 229 | } |
| 230 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 231 | static int qcom_gpio_set_value(struct udevice *dev, unsigned offset, |
| 232 | int value) |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 233 | { |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 234 | struct qcom_pmic_gpio_data *plat = dev_get_plat(dev); |
| 235 | uint32_t gpio_base = plat->pid + REG_OFFSET(offset); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 236 | |
| 237 | /* Set the output value of the gpio */ |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 238 | if (plat->lv_mv_type) |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 239 | return pmic_clrsetbits(dev->parent, |
| 240 | gpio_base + REG_LV_MV_OUTPUT_CTL, |
| 241 | REG_LV_MV_OUTPUT_CTL_MASK, |
| 242 | !!value << REG_LV_MV_OUTPUT_CTL_SHIFT); |
| 243 | else |
| 244 | return pmic_clrsetbits(dev->parent, gpio_base + REG_CTL, |
| 245 | REG_CTL_OUTPUT_MASK, !!value); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Caleb Connolly | 9bd7079 | 2024-02-26 17:26:13 +0000 | [diff] [blame] | 248 | static int qcom_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
| 249 | struct ofnode_phandle_args *args) |
| 250 | { |
| 251 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 252 | |
| 253 | if (args->args_count < 1) |
| 254 | return -EINVAL; |
| 255 | |
| 256 | /* GPIOs in DT are 1-based */ |
| 257 | desc->offset = args->args[0] - 1; |
| 258 | if (desc->offset >= uc_priv->gpio_count) |
| 259 | return -EINVAL; |
| 260 | |
| 261 | if (args->args_count < 2) |
| 262 | return 0; |
| 263 | |
| 264 | desc->flags = gpio_flags_xlate(args->args[1]); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 269 | static const struct dm_gpio_ops qcom_gpio_ops = { |
| 270 | .direction_input = qcom_gpio_direction_input, |
| 271 | .direction_output = qcom_gpio_direction_output, |
| 272 | .get_value = qcom_gpio_get_value, |
| 273 | .set_value = qcom_gpio_set_value, |
| 274 | .get_function = qcom_gpio_get_function, |
Caleb Connolly | 9bd7079 | 2024-02-26 17:26:13 +0000 | [diff] [blame] | 275 | .xlate = qcom_gpio_xlate, |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 276 | }; |
| 277 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 278 | static int qcom_gpio_bind(struct udevice *dev) |
| 279 | { |
Tom Rini | 5f908f3 | 2024-03-01 13:54:49 -0500 | [diff] [blame] | 280 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 281 | struct qcom_pmic_gpio_data *plat = dev_get_plat(dev); |
| 282 | ulong quirks = dev_get_driver_data(dev); |
| 283 | struct udevice *child; |
| 284 | struct driver *drv; |
| 285 | int ret; |
| 286 | |
| 287 | drv = lists_driver_lookup_name("qcom_pmic_pinctrl"); |
| 288 | if (!drv) { |
| 289 | log_warning("Cannot find driver '%s'\n", "qcom_pmic_pinctrl"); |
| 290 | return -ENOENT; |
| 291 | } |
| 292 | |
| 293 | /* Bind the GPIO driver as a child of the PMIC. */ |
| 294 | ret = device_bind_with_driver_data(dev, drv, |
| 295 | dev->name, |
| 296 | quirks, dev_ofnode(dev), &child); |
| 297 | if (ret) |
| 298 | return log_msg_ret("bind", ret); |
| 299 | |
| 300 | dev_set_plat(child, plat); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 305 | static int qcom_gpio_probe(struct udevice *dev) |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 306 | { |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 307 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 308 | struct qcom_pmic_gpio_data *plat = dev_get_plat(dev); |
| 309 | struct ofnode_phandle_args args; |
| 310 | int val, ret; |
Caleb Connolly | add0436 | 2023-12-05 13:46:46 +0000 | [diff] [blame] | 311 | u64 pid; |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 312 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 313 | plat->pmic = dev->parent; |
| 314 | |
Caleb Connolly | add0436 | 2023-12-05 13:46:46 +0000 | [diff] [blame] | 315 | pid = dev_read_addr(dev); |
| 316 | if (pid == FDT_ADDR_T_NONE) |
Simon Glass | 9513997 | 2019-09-25 08:55:59 -0600 | [diff] [blame] | 317 | return log_msg_ret("bad address", -EINVAL); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 318 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 319 | plat->pid = pid; |
Caleb Connolly | add0436 | 2023-12-05 13:46:46 +0000 | [diff] [blame] | 320 | |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 321 | /* Do a sanity check */ |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 322 | val = pmic_reg_read(plat->pmic, plat->pid + REG_TYPE); |
| 323 | if (val != REG_TYPE_VAL) |
Simon Glass | 9513997 | 2019-09-25 08:55:59 -0600 | [diff] [blame] | 324 | return log_msg_ret("bad type", -ENXIO); |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 325 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 326 | val = pmic_reg_read(plat->pmic, plat->pid + REG_SUBTYPE); |
Neil Armstrong | d44be41 | 2024-04-10 17:59:43 +0200 | [diff] [blame] | 327 | switch (val) { |
| 328 | case REG_SUBTYPE_GPIO_4CH: |
| 329 | case REG_SUBTYPE_GPIOC_4CH: |
| 330 | plat->lv_mv_type = false; |
| 331 | break; |
| 332 | case REG_SUBTYPE_GPIO_LV: |
| 333 | case REG_SUBTYPE_GPIO_MV: |
| 334 | case REG_SUBTYPE_GPIO_LV_VIN2: |
| 335 | case REG_SUBTYPE_GPIO_MV_VIN3: |
| 336 | plat->lv_mv_type = true; |
| 337 | break; |
| 338 | default: |
Simon Glass | 9513997 | 2019-09-25 08:55:59 -0600 | [diff] [blame] | 339 | return log_msg_ret("bad subtype", -ENXIO); |
Neil Armstrong | d44be41 | 2024-04-10 17:59:43 +0200 | [diff] [blame] | 340 | } |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 341 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 342 | plat->lv_mv_type = val == REG_SUBTYPE_GPIO_LV || |
| 343 | val == REG_SUBTYPE_GPIO_MV; |
Caleb Connolly | 9482a9e | 2023-12-05 13:46:50 +0000 | [diff] [blame] | 344 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 345 | /* |
| 346 | * Parse basic GPIO count specified via the gpio-ranges property |
| 347 | * as specified in upstream devicetrees |
| 348 | */ |
Caleb Connolly | 9482a9e | 2023-12-05 13:46:50 +0000 | [diff] [blame] | 349 | ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "gpio-ranges", |
| 350 | NULL, 3, 0, &args); |
| 351 | if (ret) |
| 352 | return log_msg_ret("gpio-ranges", ret); |
| 353 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 354 | plat->pin_count = args.args[2]; |
Caleb Connolly | 9482a9e | 2023-12-05 13:46:50 +0000 | [diff] [blame] | 355 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 356 | uc_priv->gpio_count = plat->pin_count; |
Caleb Connolly | 9482a9e | 2023-12-05 13:46:50 +0000 | [diff] [blame] | 357 | uc_priv->bank_name = "pmic"; |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 362 | static const struct udevice_id qcom_gpio_ids[] = { |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 363 | { .compatible = "qcom,pm8916-gpio" }, |
Jorge Ramirez-Ortiz | 3556161 | 2018-01-10 11:33:51 +0100 | [diff] [blame] | 364 | { .compatible = "qcom,pm8994-gpio" }, /* 22 GPIO's */ |
Caleb Connolly | 0b9e500 | 2024-02-26 17:26:14 +0000 | [diff] [blame] | 365 | { .compatible = "qcom,pm8998-gpio", .data = QCOM_PMIC_QUIRK_READONLY }, |
Sumit Garg | 3564257 | 2022-08-04 19:57:18 +0530 | [diff] [blame] | 366 | { .compatible = "qcom,pms405-gpio" }, |
Caleb Connolly | 1a9186e | 2024-04-18 18:25:44 +0100 | [diff] [blame^] | 367 | { .compatible = "qcom,pm6125-gpio", .data = QCOM_PMIC_QUIRK_READONLY }, |
Neil Armstrong | d44be41 | 2024-04-10 17:59:43 +0200 | [diff] [blame] | 368 | { .compatible = "qcom,pm8550-gpio", .data = QCOM_PMIC_QUIRK_READONLY }, |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 369 | { } |
| 370 | }; |
| 371 | |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 372 | U_BOOT_DRIVER(qcom_pmic_gpio) = { |
| 373 | .name = "qcom_pmic_gpio", |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 374 | .id = UCLASS_GPIO, |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 375 | .of_match = qcom_gpio_ids, |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 376 | .bind = qcom_gpio_bind, |
| 377 | .probe = qcom_gpio_probe, |
Sumit Garg | 60900b4 | 2022-08-04 19:57:17 +0530 | [diff] [blame] | 378 | .ops = &qcom_gpio_ops, |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 379 | .plat_auto = sizeof(struct qcom_pmic_gpio_data), |
| 380 | .flags = DM_FLAG_ALLOC_PDATA, |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 381 | }; |
| 382 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 383 | static const struct pinconf_param qcom_pmic_pinctrl_conf_params[] = { |
| 384 | { "output-high", PIN_CONFIG_OUTPUT_ENABLE, 1 }, |
| 385 | { "output-low", PIN_CONFIG_OUTPUT, 0 }, |
Mateusz Kulikowski | 15a5853 | 2016-03-31 23:12:31 +0200 | [diff] [blame] | 386 | }; |
| 387 | |
Caleb Connolly | c17a5c7 | 2024-02-26 17:26:15 +0000 | [diff] [blame] | 388 | static int qcom_pmic_pinctrl_get_pins_count(struct udevice *dev) |
| 389 | { |
| 390 | struct qcom_pmic_gpio_data *plat = dev_get_plat(dev); |
| 391 | |
| 392 | return plat->pin_count; |
| 393 | } |
| 394 | |
| 395 | static const char *qcom_pmic_pinctrl_get_pin_name(struct udevice *dev, unsigned int selector) |
| 396 | { |
| 397 | static char name[8]; |
| 398 | |
| 399 | /* DT indexes from 1 */ |
| 400 | snprintf(name, sizeof(name), "gpio%u", selector + 1); |
| 401 | |
| 402 | return name; |
| 403 | } |
| 404 | |
| 405 | static int qcom_pmic_pinctrl_pinconf_set(struct udevice *dev, unsigned int selector, |
| 406 | unsigned int param, unsigned int arg) |
| 407 | { |
| 408 | /* We only support configuring the pin as an output, either low or high */ |
| 409 | return _qcom_gpio_set_direction(dev, selector, false, |
| 410 | param == PIN_CONFIG_OUTPUT_ENABLE); |
| 411 | } |
| 412 | |
| 413 | static const char *qcom_pmic_pinctrl_get_function_name(struct udevice *dev, unsigned int selector) |
| 414 | { |
| 415 | if (!selector) |
| 416 | return "normal"; |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | static int qcom_pmic_pinctrl_generic_get_functions_count(struct udevice *dev) |
| 421 | { |
| 422 | return 1; |
| 423 | } |
| 424 | |
| 425 | static int qcom_pmic_pinctrl_generic_pinmux_set_mux(struct udevice *dev, unsigned int selector, |
| 426 | unsigned int func_selector) |
| 427 | { |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | struct pinctrl_ops qcom_pmic_pinctrl_ops = { |
| 432 | .get_pins_count = qcom_pmic_pinctrl_get_pins_count, |
| 433 | .get_pin_name = qcom_pmic_pinctrl_get_pin_name, |
| 434 | .set_state = pinctrl_generic_set_state, |
| 435 | .pinconf_num_params = ARRAY_SIZE(qcom_pmic_pinctrl_conf_params), |
| 436 | .pinconf_params = qcom_pmic_pinctrl_conf_params, |
| 437 | .pinconf_set = qcom_pmic_pinctrl_pinconf_set, |
| 438 | .get_function_name = qcom_pmic_pinctrl_get_function_name, |
| 439 | .get_functions_count = qcom_pmic_pinctrl_generic_get_functions_count, |
| 440 | .pinmux_set = qcom_pmic_pinctrl_generic_pinmux_set_mux, |
| 441 | }; |
| 442 | |
| 443 | U_BOOT_DRIVER(qcom_pmic_pinctrl) = { |
| 444 | .name = "qcom_pmic_pinctrl", |
| 445 | .id = UCLASS_PINCTRL, |
| 446 | .ops = &qcom_pmic_pinctrl_ops, |
| 447 | }; |