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_flex(*lookup, table, 2); 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_DOVDD: 227 *con_id = "dovdd"; 228 *gpio_flags = GPIO_ACTIVE_HIGH; 229 break; 230 case INT3472_GPIO_TYPE_HANDSHAKE: 231 *con_id = "dvdd"; 232 *gpio_flags = GPIO_ACTIVE_HIGH; 233 /* Setups using a handshake pin need 25 ms enable delay */ 234 *enable_time_us = 25 * USEC_PER_MSEC; 235 break; 236 default: 237 *con_id = "unknown"; 238 *gpio_flags = GPIO_ACTIVE_HIGH; 239 break; 240 } 241 } 242 243 /** 244 * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor 245 * @ares: A pointer to a &struct acpi_resource 246 * @data: A pointer to a &struct int3472_discrete_device 247 * 248 * This function handles GPIO resources that are against an INT3472 249 * ACPI device, by checking the value of the corresponding _DSM entry. 250 * This will return a 32bit int, where the lowest byte represents the 251 * function of the GPIO pin: 252 * 253 * 0x00 Reset 254 * 0x01 Power down 255 * 0x0b Power enable 256 * 0x0c Clock enable 257 * 0x0d Privacy LED 258 * 0x10 DOVDD (digital I/O voltage) 259 * 0x13 Hotplug detect 260 * 261 * There are some known platform specific quirks where that does not quite 262 * hold up; for example where a pin with type 0x01 (Power down) is mapped to 263 * a sensor pin that performs a reset function or entries in _CRS and _DSM that 264 * do not actually correspond to a physical connection. These will be handled 265 * by the mapping sub-functions. 266 * 267 * GPIOs will either be mapped directly to the sensor device or else used 268 * to create clocks and regulators via the usual frameworks. 269 * 270 * Return: 271 * * 1 - Continue the loop without adding a copy of the resource to 272 * * the list passed to acpi_dev_get_resources() 273 * * 0 - Continue the loop after adding a copy of the resource to 274 * * the list passed to acpi_dev_get_resources() 275 * * -errno - Error, break loop 276 */ 277 static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares, 278 void *data) 279 { 280 struct int3472_discrete_device *int3472 = data; 281 const char *second_sensor = NULL; 282 struct acpi_resource_gpio *agpio; 283 unsigned int enable_time_us; 284 u8 active_value, pin, type; 285 unsigned long gpio_flags; 286 union acpi_object *obj; 287 struct gpio_desc *gpio; 288 const char *err_msg; 289 const char *con_id; 290 int ret; 291 292 if (!acpi_gpio_get_io_resource(ares, &agpio)) 293 return 1; 294 295 /* 296 * ngpios + 2 because the index of this _DSM function is 1-based and 297 * the first function is just a count. 298 */ 299 obj = acpi_evaluate_dsm_typed(int3472->adev->handle, 300 &int3472_gpio_guid, 0x00, 301 int3472->ngpios + 2, 302 NULL, ACPI_TYPE_INTEGER); 303 304 if (!obj) { 305 dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n", 306 agpio->pin_table[0]); 307 return 1; 308 } 309 310 type = FIELD_GET(INT3472_GPIO_DSM_TYPE, obj->integer.value); 311 312 int3472_get_con_id_and_polarity(int3472, &type, &con_id, &gpio_flags, &enable_time_us); 313 314 pin = FIELD_GET(INT3472_GPIO_DSM_PIN, obj->integer.value); 315 /* Pin field is not really used under Windows and wraps around at 8 bits */ 316 if (pin != (agpio->pin_table[0] & 0xff)) 317 dev_dbg(int3472->dev, FW_BUG "%s %s pin number mismatch _DSM %d resource %d\n", 318 con_id, agpio->resource_source.string_ptr, pin, agpio->pin_table[0]); 319 320 active_value = FIELD_GET(INT3472_GPIO_DSM_SENSOR_ON_VAL, obj->integer.value); 321 if (!active_value) 322 gpio_flags ^= GPIO_ACTIVE_LOW; 323 324 dev_dbg(int3472->dev, "%s %s pin %d active-%s\n", con_id, 325 agpio->resource_source.string_ptr, agpio->pin_table[0], 326 str_high_low(gpio_flags == GPIO_ACTIVE_HIGH)); 327 328 switch (type) { 329 case INT3472_GPIO_TYPE_RESET: 330 case INT3472_GPIO_TYPE_POWERDOWN: 331 case INT3472_GPIO_TYPE_HOTPLUG_DETECT: 332 ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, con_id, gpio_flags); 333 if (ret) 334 err_msg = "Failed to map GPIO pin to sensor\n"; 335 336 break; 337 case INT3472_GPIO_TYPE_CLK_ENABLE: 338 case INT3472_GPIO_TYPE_PRIVACY_LED: 339 case INT3472_GPIO_TYPE_POWER_ENABLE: 340 case INT3472_GPIO_TYPE_DOVDD: 341 case INT3472_GPIO_TYPE_HANDSHAKE: 342 gpio = skl_int3472_gpiod_get_from_temp_lookup(int3472, agpio, con_id, gpio_flags); 343 if (IS_ERR(gpio)) { 344 ret = PTR_ERR(gpio); 345 err_msg = "Failed to get GPIO\n"; 346 break; 347 } 348 349 switch (type) { 350 case INT3472_GPIO_TYPE_CLK_ENABLE: 351 ret = skl_int3472_register_gpio_clock(int3472, gpio); 352 if (ret) 353 err_msg = "Failed to register clock\n"; 354 355 break; 356 case INT3472_GPIO_TYPE_PRIVACY_LED: 357 ret = skl_int3472_register_pled(int3472, gpio); 358 if (ret) 359 err_msg = "Failed to register LED\n"; 360 361 break; 362 case INT3472_GPIO_TYPE_POWER_ENABLE: 363 second_sensor = int3472->quirks.avdd_second_sensor; 364 fallthrough; 365 case INT3472_GPIO_TYPE_DOVDD: 366 case INT3472_GPIO_TYPE_HANDSHAKE: 367 ret = skl_int3472_register_regulator(int3472, gpio, enable_time_us, 368 con_id, second_sensor); 369 if (ret) 370 err_msg = "Failed to register regulator\n"; 371 372 break; 373 default: /* Never reached */ 374 ret = -EINVAL; 375 break; 376 } 377 378 if (ret) 379 gpiod_put(gpio); 380 381 break; 382 default: 383 dev_warn(int3472->dev, 384 "GPIO type 0x%02x unknown; the sensor may not work\n", 385 type); 386 ret = 1; 387 break; 388 } 389 390 int3472->ngpios++; 391 ACPI_FREE(obj); 392 393 if (ret < 0) 394 return dev_err_probe(int3472->dev, ret, err_msg); 395 396 /* Tell acpi_dev_get_resources() to not make a copy of the resource */ 397 return 1; 398 } 399 400 int int3472_discrete_parse_crs(struct int3472_discrete_device *int3472) 401 { 402 LIST_HEAD(resource_list); 403 int ret; 404 405 skl_int3472_log_sensor_module_name(int3472); 406 407 ret = acpi_dev_get_resources(int3472->adev, &resource_list, 408 skl_int3472_handle_gpio_resources, 409 int3472); 410 if (ret < 0) 411 return ret; 412 413 acpi_dev_free_resource_list(&resource_list); 414 415 /* Register _DSM based clock (no-op if a GPIO clock was already registered) */ 416 ret = skl_int3472_register_dsm_clock(int3472); 417 if (ret < 0) 418 return ret; 419 420 int3472->gpios.dev_id = int3472->sensor_name; 421 gpiod_add_lookup_table(&int3472->gpios); 422 423 return 0; 424 } 425 EXPORT_SYMBOL_NS_GPL(int3472_discrete_parse_crs, "INTEL_INT3472_DISCRETE"); 426 427 void int3472_discrete_cleanup(struct int3472_discrete_device *int3472) 428 { 429 gpiod_remove_lookup_table(&int3472->gpios); 430 431 skl_int3472_unregister_clock(int3472); 432 skl_int3472_unregister_pled(int3472); 433 skl_int3472_unregister_regulator(int3472); 434 } 435 EXPORT_SYMBOL_NS_GPL(int3472_discrete_cleanup, "INTEL_INT3472_DISCRETE"); 436 437 static void skl_int3472_discrete_remove(struct platform_device *pdev) 438 { 439 int3472_discrete_cleanup(platform_get_drvdata(pdev)); 440 } 441 442 static int skl_int3472_discrete_probe(struct platform_device *pdev) 443 { 444 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev); 445 const struct int3472_discrete_quirks *quirks = NULL; 446 struct int3472_discrete_device *int3472; 447 const struct dmi_system_id *id; 448 struct int3472_cldb cldb; 449 int ret; 450 451 if (!adev) 452 return -ENODEV; 453 454 id = dmi_first_match(skl_int3472_discrete_quirks); 455 if (id) 456 quirks = id->driver_data; 457 458 ret = skl_int3472_fill_cldb(adev, &cldb); 459 if (ret) { 460 dev_err(&pdev->dev, "Couldn't fill CLDB structure\n"); 461 return ret; 462 } 463 464 if (cldb.control_logic_type != 1) { 465 dev_err(&pdev->dev, "Unsupported control logic type %u\n", 466 cldb.control_logic_type); 467 return -EINVAL; 468 } 469 470 /* Max num GPIOs we've seen plus a terminator */ 471 int3472 = devm_kzalloc(&pdev->dev, struct_size(int3472, gpios.table, 472 INT3472_MAX_SENSOR_GPIOS + 1), GFP_KERNEL); 473 if (!int3472) 474 return -ENOMEM; 475 476 int3472->adev = adev; 477 int3472->dev = &pdev->dev; 478 platform_set_drvdata(pdev, int3472); 479 int3472->clock.imgclk_index = cldb.clock_source; 480 481 if (quirks) 482 int3472->quirks = *quirks; 483 484 ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor, 485 &int3472->sensor_name); 486 if (ret) 487 return ret; 488 489 /* 490 * Initialising this list means we can call gpiod_remove_lookup_table() 491 * in failure paths without issue. 492 */ 493 INIT_LIST_HEAD(&int3472->gpios.list); 494 495 ret = int3472_discrete_parse_crs(int3472); 496 if (ret) { 497 skl_int3472_discrete_remove(pdev); 498 return ret; 499 } 500 501 acpi_dev_clear_dependencies(adev); 502 return 0; 503 } 504 505 static const struct acpi_device_id int3472_device_id[] = { 506 { "INT3472", 0 }, 507 { } 508 }; 509 MODULE_DEVICE_TABLE(acpi, int3472_device_id); 510 511 static struct platform_driver int3472_discrete = { 512 .driver = { 513 .name = "int3472-discrete", 514 .acpi_match_table = int3472_device_id, 515 }, 516 .probe = skl_int3472_discrete_probe, 517 .remove = skl_int3472_discrete_remove, 518 }; 519 module_platform_driver(int3472_discrete); 520 521 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver"); 522 MODULE_AUTHOR("Daniel Scally <djrscally@gmail.com>"); 523 MODULE_LICENSE("GPL v2"); 524 MODULE_IMPORT_NS("INTEL_INT3472"); 525