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