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