1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2025 Intel Corporation. 4 * Copyright (c) 2025 Red Hat, Inc. 5 */ 6 7 #include <linux/acpi.h> 8 #include <linux/auxiliary_bus.h> 9 #include <linux/cleanup.h> 10 #include <linux/device.h> 11 #include <linux/gpio/driver.h> 12 #include <linux/mutex.h> 13 #include <linux/types.h> 14 #include <linux/usb/usbio.h> 15 16 struct usbio_gpio_bank { 17 u8 config[USBIO_GPIOSPERBANK]; 18 u32 bitmap; 19 }; 20 21 struct usbio_gpio { 22 struct mutex config_mutex; /* Protects banks[x].config */ 23 struct usbio_gpio_bank banks[USBIO_MAX_GPIOBANKS]; 24 struct gpio_chip gc; 25 struct auxiliary_device *adev; 26 }; 27 28 static const struct acpi_device_id usbio_gpio_acpi_hids[] = { 29 { "INTC1007" }, /* MTL */ 30 { "INTC10B2" }, /* ARL */ 31 { "INTC10B5" }, /* LNL */ 32 { "INTC10D1" }, /* MTL-CVF */ 33 { "INTC10E2" }, /* PTL */ 34 { "INTC1116" }, /* NVL */ 35 { } 36 }; 37 38 static void usbio_gpio_get_bank_and_pin(struct gpio_chip *gc, unsigned int offset, 39 struct usbio_gpio_bank **bank_ret, 40 unsigned int *pin_ret) 41 { 42 struct usbio_gpio *gpio = gpiochip_get_data(gc); 43 struct device *dev = &gpio->adev->dev; 44 struct usbio_gpio_bank *bank; 45 unsigned int pin; 46 47 bank = &gpio->banks[offset / USBIO_GPIOSPERBANK]; 48 pin = offset % USBIO_GPIOSPERBANK; 49 if (~bank->bitmap & BIT(pin)) { 50 /* The FW bitmap sometimes is invalid, warn and continue */ 51 dev_warn_once(dev, FW_BUG "GPIO %u is not in FW pins bitmap\n", offset); 52 } 53 54 *bank_ret = bank; 55 *pin_ret = pin; 56 } 57 58 static int usbio_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) 59 { 60 struct usbio_gpio_bank *bank; 61 unsigned int pin; 62 u8 cfg; 63 64 usbio_gpio_get_bank_and_pin(gc, offset, &bank, &pin); 65 66 cfg = bank->config[pin] & USBIO_GPIO_PINMOD_MASK; 67 68 return (cfg == USBIO_GPIO_PINMOD_OUTPUT) ? 69 GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN; 70 } 71 72 static int usbio_gpio_get(struct gpio_chip *gc, unsigned int offset) 73 { 74 struct usbio_gpio *gpio = gpiochip_get_data(gc); 75 struct usbio_gpio_bank *bank; 76 struct usbio_gpio_rw gbuf; 77 unsigned int pin; 78 int ret; 79 80 usbio_gpio_get_bank_and_pin(gc, offset, &bank, &pin); 81 82 gbuf.bankid = offset / USBIO_GPIOSPERBANK; 83 gbuf.pincount = 1; 84 gbuf.pin = pin; 85 86 ret = usbio_control_msg(gpio->adev, USBIO_PKTTYPE_GPIO, USBIO_GPIOCMD_READ, 87 &gbuf, sizeof(gbuf) - sizeof(gbuf.value), 88 &gbuf, sizeof(gbuf)); 89 if (ret != sizeof(gbuf)) 90 return (ret < 0) ? ret : -EPROTO; 91 92 return (le32_to_cpu(gbuf.value) >> pin) & 1; 93 } 94 95 static int usbio_gpio_set(struct gpio_chip *gc, unsigned int offset, int value) 96 { 97 struct usbio_gpio *gpio = gpiochip_get_data(gc); 98 struct usbio_gpio_bank *bank; 99 struct usbio_gpio_rw gbuf; 100 unsigned int pin; 101 102 usbio_gpio_get_bank_and_pin(gc, offset, &bank, &pin); 103 104 gbuf.bankid = offset / USBIO_GPIOSPERBANK; 105 gbuf.pincount = 1; 106 gbuf.pin = pin; 107 gbuf.value = cpu_to_le32(value << pin); 108 109 return usbio_control_msg(gpio->adev, USBIO_PKTTYPE_GPIO, USBIO_GPIOCMD_WRITE, 110 &gbuf, sizeof(gbuf), NULL, 0); 111 } 112 113 static int usbio_gpio_update_config(struct gpio_chip *gc, unsigned int offset, 114 u8 mask, u8 value) 115 { 116 struct usbio_gpio *gpio = gpiochip_get_data(gc); 117 struct usbio_gpio_bank *bank; 118 struct usbio_gpio_init gbuf; 119 unsigned int pin; 120 121 usbio_gpio_get_bank_and_pin(gc, offset, &bank, &pin); 122 123 guard(mutex)(&gpio->config_mutex); 124 125 bank->config[pin] &= ~mask; 126 bank->config[pin] |= value; 127 128 gbuf.bankid = offset / USBIO_GPIOSPERBANK; 129 gbuf.config = bank->config[pin]; 130 gbuf.pincount = 1; 131 gbuf.pin = pin; 132 133 return usbio_control_msg(gpio->adev, USBIO_PKTTYPE_GPIO, USBIO_GPIOCMD_INIT, 134 &gbuf, sizeof(gbuf), NULL, 0); 135 } 136 137 static int usbio_gpio_direction_input(struct gpio_chip *gc, unsigned int offset) 138 { 139 return usbio_gpio_update_config(gc, offset, USBIO_GPIO_PINMOD_MASK, 140 USBIO_GPIO_SET_PINMOD(USBIO_GPIO_PINMOD_INPUT)); 141 } 142 143 static int usbio_gpio_direction_output(struct gpio_chip *gc, 144 unsigned int offset, int value) 145 { 146 int ret; 147 148 ret = usbio_gpio_update_config(gc, offset, USBIO_GPIO_PINMOD_MASK, 149 USBIO_GPIO_SET_PINMOD(USBIO_GPIO_PINMOD_OUTPUT)); 150 if (ret) 151 return ret; 152 153 return usbio_gpio_set(gc, offset, value); 154 } 155 156 static int usbio_gpio_set_config(struct gpio_chip *gc, unsigned int offset, 157 unsigned long config) 158 { 159 u8 value; 160 161 switch (pinconf_to_config_param(config)) { 162 case PIN_CONFIG_BIAS_PULL_PIN_DEFAULT: 163 value = USBIO_GPIO_SET_PINCFG(USBIO_GPIO_PINCFG_DEFAULT); 164 break; 165 case PIN_CONFIG_BIAS_PULL_UP: 166 value = USBIO_GPIO_SET_PINCFG(USBIO_GPIO_PINCFG_PULLUP); 167 break; 168 case PIN_CONFIG_BIAS_PULL_DOWN: 169 value = USBIO_GPIO_SET_PINCFG(USBIO_GPIO_PINCFG_PULLDOWN); 170 break; 171 case PIN_CONFIG_DRIVE_PUSH_PULL: 172 value = USBIO_GPIO_SET_PINCFG(USBIO_GPIO_PINCFG_PUSHPULL); 173 break; 174 default: 175 return -ENOTSUPP; 176 } 177 178 return usbio_gpio_update_config(gc, offset, USBIO_GPIO_PINCFG_MASK, value); 179 } 180 181 static int usbio_gpio_probe(struct auxiliary_device *adev, 182 const struct auxiliary_device_id *adev_id) 183 { 184 struct usbio_gpio_bank_desc *bank_desc; 185 struct device *dev = &adev->dev; 186 struct usbio_gpio *gpio; 187 int bank, ret; 188 189 bank_desc = dev_get_platdata(dev); 190 if (!bank_desc) 191 return -EINVAL; 192 193 gpio = devm_kzalloc(dev, sizeof(*gpio), GFP_KERNEL); 194 if (!gpio) 195 return -ENOMEM; 196 197 ret = devm_mutex_init(dev, &gpio->config_mutex); 198 if (ret) 199 return ret; 200 201 gpio->adev = adev; 202 203 usbio_acpi_bind(gpio->adev, usbio_gpio_acpi_hids); 204 205 for (bank = 0; bank < USBIO_MAX_GPIOBANKS && bank_desc[bank].bmap; bank++) 206 gpio->banks[bank].bitmap = le32_to_cpu(bank_desc[bank].bmap); 207 208 gpio->gc.label = ACPI_COMPANION(dev) ? 209 acpi_dev_name(ACPI_COMPANION(dev)) : dev_name(dev); 210 gpio->gc.parent = dev; 211 gpio->gc.owner = THIS_MODULE; 212 gpio->gc.get_direction = usbio_gpio_get_direction; 213 gpio->gc.direction_input = usbio_gpio_direction_input; 214 gpio->gc.direction_output = usbio_gpio_direction_output; 215 gpio->gc.get = usbio_gpio_get; 216 gpio->gc.set = usbio_gpio_set; 217 gpio->gc.set_config = usbio_gpio_set_config; 218 gpio->gc.base = -1; 219 gpio->gc.ngpio = bank * USBIO_GPIOSPERBANK; 220 gpio->gc.can_sleep = true; 221 222 ret = devm_gpiochip_add_data(dev, &gpio->gc, gpio); 223 if (ret) 224 return ret; 225 226 if (has_acpi_companion(dev)) 227 acpi_dev_clear_dependencies(ACPI_COMPANION(dev)); 228 229 return 0; 230 } 231 232 static const struct auxiliary_device_id usbio_gpio_id_table[] = { 233 { "usbio.usbio-gpio" }, 234 { } 235 }; 236 MODULE_DEVICE_TABLE(auxiliary, usbio_gpio_id_table); 237 238 static struct auxiliary_driver usbio_gpio_driver = { 239 .name = USBIO_GPIO_CLIENT, 240 .probe = usbio_gpio_probe, 241 .id_table = usbio_gpio_id_table 242 }; 243 module_auxiliary_driver(usbio_gpio_driver); 244 245 MODULE_DESCRIPTION("Intel USBIO GPIO driver"); 246 MODULE_AUTHOR("Israel Cepeda <israel.a.cepeda.lopez@intel.com>"); 247 MODULE_AUTHOR("Hans de Goede <hansg@kernel.org>"); 248 MODULE_LICENSE("GPL"); 249 MODULE_IMPORT_NS("USBIO"); 250