1 // SPDX-License-Identifier: GPL-2.0 2 /* Author: Dan Scally <djrscally@gmail.com> */ 3 4 #include <linux/acpi.h> 5 #include <linux/array_size.h> 6 #include <linux/bitfield.h> 7 #include <linux/device.h> 8 #include <linux/dmi.h> 9 #include <linux/gpio/consumer.h> 10 #include <linux/gpio/machine.h> 11 #include <linux/i2c.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/overflow.h> 15 #include <linux/platform_data/x86/int3472.h> 16 #include <linux/platform_device.h> 17 #include <linux/string_choices.h> 18 #include <linux/uuid.h> 19 20 /* 21 * 79234640-9e10-4fea-a5c1-b5aa8b19756f 22 * This _DSM GUID returns information about the GPIO lines mapped to a 23 * discrete INT3472 device. Function number 1 returns a count of the GPIO 24 * lines that are mapped. Subsequent functions return 32 bit ints encoding 25 * information about the GPIO line, including its purpose. 26 */ 27 static const guid_t int3472_gpio_guid = 28 GUID_INIT(0x79234640, 0x9e10, 0x4fea, 29 0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f); 30 31 #define INT3472_GPIO_DSM_TYPE GENMASK(7, 0) 32 #define INT3472_GPIO_DSM_PIN GENMASK(15, 8) 33 #define INT3472_GPIO_DSM_SENSOR_ON_VAL GENMASK(31, 24) 34 35 /* 36 * 822ace8f-2814-4174-a56b-5f029fe079ee 37 * This _DSM GUID returns a string from the sensor device, which acts as a 38 * module identifier. 39 */ 40 static const guid_t cio2_sensor_module_guid = 41 GUID_INIT(0x822ace8f, 0x2814, 0x4174, 42 0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee); 43 44 static void skl_int3472_log_sensor_module_name(struct int3472_discrete_device *int3472) 45 { 46 union acpi_object *obj; 47 48 obj = acpi_evaluate_dsm_typed(int3472->sensor->handle, 49 &cio2_sensor_module_guid, 0x00, 50 0x01, NULL, ACPI_TYPE_STRING); 51 if (obj) { 52 dev_dbg(int3472->dev, "Sensor module id: '%s'\n", obj->string.pointer); 53 ACPI_FREE(obj); 54 } 55 } 56 57 static int skl_int3472_fill_gpiod_lookup(struct gpiod_lookup *table_entry, 58 struct acpi_resource_gpio *agpio, 59 const char *con_id, unsigned long gpio_flags) 60 { 61 char *path = agpio->resource_source.string_ptr; 62 struct acpi_device *adev; 63 acpi_handle handle; 64 acpi_status status; 65 66 status = acpi_get_handle(NULL, path, &handle); 67 if (ACPI_FAILURE(status)) 68 return -EINVAL; 69 70 adev = acpi_fetch_acpi_dev(handle); 71 if (!adev) 72 return -ENODEV; 73 74 *table_entry = GPIO_LOOKUP(acpi_dev_name(adev), agpio->pin_table[0], con_id, gpio_flags); 75 76 return 0; 77 } 78 79 static int skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device *int3472, 80 struct acpi_resource_gpio *agpio, 81 const char *con_id, unsigned long gpio_flags) 82 { 83 int ret; 84 85 if (int3472->n_sensor_gpios >= INT3472_MAX_SENSOR_GPIOS) { 86 dev_warn(int3472->dev, "Too many GPIOs mapped\n"); 87 return -EINVAL; 88 } 89 90 ret = skl_int3472_fill_gpiod_lookup(&int3472->gpios.table[int3472->n_sensor_gpios], 91 agpio, con_id, gpio_flags); 92 if (ret) 93 return ret; 94 95 int3472->n_sensor_gpios++; 96 97 return 0; 98 } 99 100 /* This should *really* only be used when there's no other way... */ 101 static struct gpio_desc * 102 skl_int3472_gpiod_get_from_temp_lookup(struct int3472_discrete_device *int3472, 103 struct acpi_resource_gpio *agpio, 104 const char *con_id, unsigned long gpio_flags) 105 { 106 struct gpio_desc *desc; 107 int ret; 108 109 struct gpiod_lookup_table *lookup __free(kfree) = 110 kzalloc(struct_size(lookup, table, 2), GFP_KERNEL); 111 if (!lookup) 112 return ERR_PTR(-ENOMEM); 113 114 lookup->dev_id = dev_name(int3472->dev); 115 ret = skl_int3472_fill_gpiod_lookup(&lookup->table[0], agpio, con_id, gpio_flags); 116 if (ret) 117 return ERR_PTR(ret); 118 119 gpiod_add_lookup_table(lookup); 120 desc = gpiod_get(int3472->dev, con_id, GPIOD_OUT_LOW); 121 gpiod_remove_lookup_table(lookup); 122 123 return desc; 124 } 125 126 /** 127 * struct int3472_gpio_map - Map GPIOs to whatever is expected by the 128 * sensor driver (as in DT bindings) 129 * @hid: The ACPI HID of the device without the instance number e.g. INT347E 130 * @type_from: The GPIO type from ACPI ?SDT 131 * @type_to: The assigned GPIO type, typically same as @type_from 132 * @enable_time_us: Enable time in usec for GPIOs mapped to regulators 133 * @con_id: The name of the GPIO for the device 134 * @polarity_low: GPIO_ACTIVE_LOW true if the @polarity_low is true, 135 * GPIO_ACTIVE_HIGH otherwise 136 */ 137 struct int3472_gpio_map { 138 const char *hid; 139 u8 type_from; 140 u8 type_to; 141 bool polarity_low; 142 unsigned int enable_time_us; 143 const char *con_id; 144 }; 145 146 static const struct int3472_gpio_map int3472_gpio_map[] = { 147 { /* mt9m114 designs declare a powerdown pin which controls the regulators */ 148 .hid = "INT33F0", 149 .type_from = INT3472_GPIO_TYPE_POWERDOWN, 150 .type_to = INT3472_GPIO_TYPE_POWER_ENABLE, 151 .con_id = "vdd", 152 .enable_time_us = GPIO_REGULATOR_ENABLE_TIME, 153 }, 154 { /* ov7251 driver / DT-bindings expect "enable" as con_id for reset */ 155 .hid = "INT347E", 156 .type_from = INT3472_GPIO_TYPE_RESET, 157 .type_to = INT3472_GPIO_TYPE_RESET, 158 .con_id = "enable", 159 }, 160 { /* ov08x40's handshake pin needs a 45 ms delay on some HP laptops */ 161 .hid = "OVTI08F4", 162 .type_from = INT3472_GPIO_TYPE_HANDSHAKE, 163 .type_to = INT3472_GPIO_TYPE_HANDSHAKE, 164 .con_id = "dvdd", 165 .enable_time_us = 45 * USEC_PER_MSEC, 166 }, 167 }; 168 169 static void int3472_get_con_id_and_polarity(struct int3472_discrete_device *int3472, u8 *type, 170 const char **con_id, unsigned long *gpio_flags, 171 unsigned int *enable_time_us) 172 { 173 struct acpi_device *adev = int3472->sensor; 174 unsigned int i; 175 176 for (i = 0; i < ARRAY_SIZE(int3472_gpio_map); i++) { 177 /* 178 * Map the firmware-provided GPIO to whatever a driver expects 179 * (as in DT bindings). First check if the type matches with the 180 * GPIO map, then further check that the device _HID matches. 181 */ 182 if (*type != int3472_gpio_map[i].type_from) 183 continue; 184 185 if (!acpi_dev_hid_uid_match(adev, int3472_gpio_map[i].hid, NULL)) 186 continue; 187 188 dev_dbg(int3472->dev, "mapping type 0x%02x pin to 0x%02x %s\n", 189 *type, int3472_gpio_map[i].type_to, int3472_gpio_map[i].con_id); 190 191 *type = int3472_gpio_map[i].type_to; 192 *gpio_flags = int3472_gpio_map[i].polarity_low ? 193 GPIO_ACTIVE_LOW : GPIO_ACTIVE_HIGH; 194 *con_id = int3472_gpio_map[i].con_id; 195 *enable_time_us = int3472_gpio_map[i].enable_time_us; 196 return; 197 } 198 199 *enable_time_us = GPIO_REGULATOR_ENABLE_TIME; 200 201 switch (*type) { 202 case INT3472_GPIO_TYPE_RESET: 203 *con_id = "reset"; 204 *gpio_flags = GPIO_ACTIVE_LOW; 205 break; 206 case INT3472_GPIO_TYPE_POWERDOWN: 207 *con_id = "powerdown"; 208 *gpio_flags = GPIO_ACTIVE_LOW; 209 break; 210 case INT3472_GPIO_TYPE_CLK_ENABLE: 211 *con_id = "clk-enable"; 212 *gpio_flags = GPIO_ACTIVE_HIGH; 213 break; 214 case INT3472_GPIO_TYPE_PRIVACY_LED: 215 *con_id = "privacy-led"; 216 *gpio_flags = GPIO_ACTIVE_HIGH; 217 break; 218 case INT3472_GPIO_TYPE_HOTPLUG_DETECT: 219 *con_id = "hpd"; 220 *gpio_flags = GPIO_ACTIVE_HIGH; 221 break; 222 case INT3472_GPIO_TYPE_POWER_ENABLE: 223 *con_id = "avdd"; 224 *gpio_flags = GPIO_ACTIVE_HIGH; 225 break; 226 case INT3472_GPIO_TYPE_HANDSHAKE: 227 *con_id = "dvdd"; 228 *gpio_flags = GPIO_ACTIVE_HIGH; 229 /* Setups using a handshake pin need 25 ms enable delay */ 230 *enable_time_us = 25 * USEC_PER_MSEC; 231 break; 232 default: 233 *con_id = "unknown"; 234 *gpio_flags = GPIO_ACTIVE_HIGH; 235 break; 236 } 237 } 238 239 /** 240 * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor 241 * @ares: A pointer to a &struct acpi_resource 242 * @data: A pointer to a &struct int3472_discrete_device 243 * 244 * This function handles GPIO resources that are against an INT3472 245 * ACPI device, by checking the value of the corresponding _DSM entry. 246 * This will return a 32bit int, where the lowest byte represents the 247 * function of the GPIO pin: 248 * 249 * 0x00 Reset 250 * 0x01 Power down 251 * 0x0b Power enable 252 * 0x0c Clock enable 253 * 0x0d Privacy LED 254 * 0x13 Hotplug detect 255 * 256 * There are some known platform specific quirks where that does not quite 257 * hold up; for example where a pin with type 0x01 (Power down) is mapped to 258 * a sensor pin that performs a reset function or entries in _CRS and _DSM that 259 * do not actually correspond to a physical connection. These will be handled 260 * by the mapping sub-functions. 261 * 262 * GPIOs will either be mapped directly to the sensor device or else used 263 * to create clocks and regulators via the usual frameworks. 264 * 265 * Return: 266 * * 1 - Continue the loop without adding a copy of the resource to 267 * * the list passed to acpi_dev_get_resources() 268 * * 0 - Continue the loop after adding a copy of the resource to 269 * * the list passed to acpi_dev_get_resources() 270 * * -errno - Error, break loop 271 */ 272 static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares, 273 void *data) 274 { 275 struct int3472_discrete_device *int3472 = data; 276 const char *second_sensor = NULL; 277 struct acpi_resource_gpio *agpio; 278 unsigned int enable_time_us; 279 u8 active_value, pin, type; 280 unsigned long gpio_flags; 281 union acpi_object *obj; 282 struct gpio_desc *gpio; 283 const char *err_msg; 284 const char *con_id; 285 int ret; 286 287 if (!acpi_gpio_get_io_resource(ares, &agpio)) 288 return 1; 289 290 /* 291 * ngpios + 2 because the index of this _DSM function is 1-based and 292 * the first function is just a count. 293 */ 294 obj = acpi_evaluate_dsm_typed(int3472->adev->handle, 295 &int3472_gpio_guid, 0x00, 296 int3472->ngpios + 2, 297 NULL, ACPI_TYPE_INTEGER); 298 299 if (!obj) { 300 dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n", 301 agpio->pin_table[0]); 302 return 1; 303 } 304 305 type = FIELD_GET(INT3472_GPIO_DSM_TYPE, obj->integer.value); 306 307 int3472_get_con_id_and_polarity(int3472, &type, &con_id, &gpio_flags, &enable_time_us); 308 309 pin = FIELD_GET(INT3472_GPIO_DSM_PIN, obj->integer.value); 310 /* Pin field is not really used under Windows and wraps around at 8 bits */ 311 if (pin != (agpio->pin_table[0] & 0xff)) 312 dev_dbg(int3472->dev, FW_BUG "%s %s pin number mismatch _DSM %d resource %d\n", 313 con_id, agpio->resource_source.string_ptr, pin, agpio->pin_table[0]); 314 315 active_value = FIELD_GET(INT3472_GPIO_DSM_SENSOR_ON_VAL, obj->integer.value); 316 if (!active_value) 317 gpio_flags ^= GPIO_ACTIVE_LOW; 318 319 dev_dbg(int3472->dev, "%s %s pin %d active-%s\n", con_id, 320 agpio->resource_source.string_ptr, agpio->pin_table[0], 321 str_high_low(gpio_flags == GPIO_ACTIVE_HIGH)); 322 323 switch (type) { 324 case INT3472_GPIO_TYPE_RESET: 325 case INT3472_GPIO_TYPE_POWERDOWN: 326 case INT3472_GPIO_TYPE_HOTPLUG_DETECT: 327 ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, con_id, gpio_flags); 328 if (ret) 329 err_msg = "Failed to map GPIO pin to sensor\n"; 330 331 break; 332 case INT3472_GPIO_TYPE_CLK_ENABLE: 333 case INT3472_GPIO_TYPE_PRIVACY_LED: 334 case INT3472_GPIO_TYPE_POWER_ENABLE: 335 case INT3472_GPIO_TYPE_HANDSHAKE: 336 gpio = skl_int3472_gpiod_get_from_temp_lookup(int3472, agpio, con_id, gpio_flags); 337 if (IS_ERR(gpio)) { 338 ret = PTR_ERR(gpio); 339 err_msg = "Failed to get GPIO\n"; 340 break; 341 } 342 343 switch (type) { 344 case INT3472_GPIO_TYPE_CLK_ENABLE: 345 ret = skl_int3472_register_gpio_clock(int3472, gpio); 346 if (ret) 347 err_msg = "Failed to register clock\n"; 348 349 break; 350 case INT3472_GPIO_TYPE_PRIVACY_LED: 351 ret = skl_int3472_register_pled(int3472, gpio); 352 if (ret) 353 err_msg = "Failed to register LED\n"; 354 355 break; 356 case INT3472_GPIO_TYPE_POWER_ENABLE: 357 second_sensor = int3472->quirks.avdd_second_sensor; 358 fallthrough; 359 case INT3472_GPIO_TYPE_HANDSHAKE: 360 ret = skl_int3472_register_regulator(int3472, gpio, enable_time_us, 361 con_id, second_sensor); 362 if (ret) 363 err_msg = "Failed to register regulator\n"; 364 365 break; 366 default: /* Never reached */ 367 ret = -EINVAL; 368 break; 369 } 370 371 if (ret) 372 gpiod_put(gpio); 373 374 break; 375 default: 376 dev_warn(int3472->dev, 377 "GPIO type 0x%02x unknown; the sensor may not work\n", 378 type); 379 ret = 1; 380 break; 381 } 382 383 int3472->ngpios++; 384 ACPI_FREE(obj); 385 386 if (ret < 0) 387 return dev_err_probe(int3472->dev, ret, err_msg); 388 389 /* Tell acpi_dev_get_resources() to not make a copy of the resource */ 390 return 1; 391 } 392 393 int int3472_discrete_parse_crs(struct int3472_discrete_device *int3472) 394 { 395 LIST_HEAD(resource_list); 396 int ret; 397 398 skl_int3472_log_sensor_module_name(int3472); 399 400 ret = acpi_dev_get_resources(int3472->adev, &resource_list, 401 skl_int3472_handle_gpio_resources, 402 int3472); 403 if (ret < 0) 404 return ret; 405 406 acpi_dev_free_resource_list(&resource_list); 407 408 /* Register _DSM based clock (no-op if a GPIO clock was already registered) */ 409 ret = skl_int3472_register_dsm_clock(int3472); 410 if (ret < 0) 411 return ret; 412 413 int3472->gpios.dev_id = int3472->sensor_name; 414 gpiod_add_lookup_table(&int3472->gpios); 415 416 return 0; 417 } 418 EXPORT_SYMBOL_NS_GPL(int3472_discrete_parse_crs, "INTEL_INT3472_DISCRETE"); 419 420 void int3472_discrete_cleanup(struct int3472_discrete_device *int3472) 421 { 422 gpiod_remove_lookup_table(&int3472->gpios); 423 424 skl_int3472_unregister_clock(int3472); 425 skl_int3472_unregister_pled(int3472); 426 skl_int3472_unregister_regulator(int3472); 427 } 428 EXPORT_SYMBOL_NS_GPL(int3472_discrete_cleanup, "INTEL_INT3472_DISCRETE"); 429 430 static void skl_int3472_discrete_remove(struct platform_device *pdev) 431 { 432 int3472_discrete_cleanup(platform_get_drvdata(pdev)); 433 } 434 435 static int skl_int3472_discrete_probe(struct platform_device *pdev) 436 { 437 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev); 438 const struct int3472_discrete_quirks *quirks = NULL; 439 struct int3472_discrete_device *int3472; 440 const struct dmi_system_id *id; 441 struct int3472_cldb cldb; 442 int ret; 443 444 if (!adev) 445 return -ENODEV; 446 447 id = dmi_first_match(skl_int3472_discrete_quirks); 448 if (id) 449 quirks = id->driver_data; 450 451 ret = skl_int3472_fill_cldb(adev, &cldb); 452 if (ret) { 453 dev_err(&pdev->dev, "Couldn't fill CLDB structure\n"); 454 return ret; 455 } 456 457 if (cldb.control_logic_type != 1) { 458 dev_err(&pdev->dev, "Unsupported control logic type %u\n", 459 cldb.control_logic_type); 460 return -EINVAL; 461 } 462 463 /* Max num GPIOs we've seen plus a terminator */ 464 int3472 = devm_kzalloc(&pdev->dev, struct_size(int3472, gpios.table, 465 INT3472_MAX_SENSOR_GPIOS + 1), GFP_KERNEL); 466 if (!int3472) 467 return -ENOMEM; 468 469 int3472->adev = adev; 470 int3472->dev = &pdev->dev; 471 platform_set_drvdata(pdev, int3472); 472 int3472->clock.imgclk_index = cldb.clock_source; 473 474 if (quirks) 475 int3472->quirks = *quirks; 476 477 ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor, 478 &int3472->sensor_name); 479 if (ret) 480 return ret; 481 482 /* 483 * Initialising this list means we can call gpiod_remove_lookup_table() 484 * in failure paths without issue. 485 */ 486 INIT_LIST_HEAD(&int3472->gpios.list); 487 488 ret = int3472_discrete_parse_crs(int3472); 489 if (ret) { 490 skl_int3472_discrete_remove(pdev); 491 return ret; 492 } 493 494 acpi_dev_clear_dependencies(adev); 495 return 0; 496 } 497 498 static const struct acpi_device_id int3472_device_id[] = { 499 { "INT3472", 0 }, 500 { } 501 }; 502 MODULE_DEVICE_TABLE(acpi, int3472_device_id); 503 504 static struct platform_driver int3472_discrete = { 505 .driver = { 506 .name = "int3472-discrete", 507 .acpi_match_table = int3472_device_id, 508 }, 509 .probe = skl_int3472_discrete_probe, 510 .remove = skl_int3472_discrete_remove, 511 }; 512 module_platform_driver(int3472_discrete); 513 514 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver"); 515 MODULE_AUTHOR("Daniel Scally <djrscally@gmail.com>"); 516 MODULE_LICENSE("GPL v2"); 517 MODULE_IMPORT_NS("INTEL_INT3472"); 518