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