intel_cht_int33fe_common.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common code for Intel Cherry Trail ACPI INT33FE pseudo device drivers
  4. * (USB Micro-B and Type-C connector variants).
  5. *
  6. * Copyright (c) 2019 Yauhen Kharuzhy <jekhor@gmail.com>
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/i2c.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include "intel_cht_int33fe_common.h"
  14. #define EXPECTED_PTYPE 4
  15. static int cht_int33fe_i2c_res_filter(struct acpi_resource *ares, void *data)
  16. {
  17. struct acpi_resource_i2c_serialbus *sb;
  18. int *count = data;
  19. if (i2c_acpi_get_i2c_resource(ares, &sb))
  20. (*count)++;
  21. return 1;
  22. }
  23. static int cht_int33fe_count_i2c_clients(struct device *dev)
  24. {
  25. struct acpi_device *adev = ACPI_COMPANION(dev);
  26. LIST_HEAD(resource_list);
  27. int count = 0;
  28. int ret;
  29. ret = acpi_dev_get_resources(adev, &resource_list,
  30. cht_int33fe_i2c_res_filter, &count);
  31. acpi_dev_free_resource_list(&resource_list);
  32. if (ret < 0)
  33. return ret;
  34. return count;
  35. }
  36. static int cht_int33fe_check_hw_type(struct device *dev)
  37. {
  38. unsigned long long ptyp;
  39. acpi_status status;
  40. int ret;
  41. status = acpi_evaluate_integer(ACPI_HANDLE(dev), "PTYP", NULL, &ptyp);
  42. if (ACPI_FAILURE(status)) {
  43. dev_err(dev, "Error getting PTYPE\n");
  44. return -ENODEV;
  45. }
  46. /*
  47. * The same ACPI HID is used for different configurations check PTYP
  48. * to ensure that we are dealing with the expected config.
  49. */
  50. if (ptyp != EXPECTED_PTYPE)
  51. return -ENODEV;
  52. /* Check presence of INT34D3 (hardware-rev 3) expected for ptype == 4 */
  53. if (!acpi_dev_present("INT34D3", "1", 3)) {
  54. dev_err(dev, "Error PTYPE == %d, but no INT34D3 device\n",
  55. EXPECTED_PTYPE);
  56. return -ENODEV;
  57. }
  58. ret = cht_int33fe_count_i2c_clients(dev);
  59. if (ret < 0)
  60. return ret;
  61. switch (ret) {
  62. case 2:
  63. return INT33FE_HW_MICROB;
  64. case 4:
  65. return INT33FE_HW_TYPEC;
  66. default:
  67. return -ENODEV;
  68. }
  69. }
  70. static int cht_int33fe_probe(struct platform_device *pdev)
  71. {
  72. struct cht_int33fe_data *data;
  73. struct device *dev = &pdev->dev;
  74. int ret;
  75. ret = cht_int33fe_check_hw_type(dev);
  76. if (ret < 0)
  77. return ret;
  78. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  79. if (!data)
  80. return -ENOMEM;
  81. data->dev = dev;
  82. switch (ret) {
  83. case INT33FE_HW_MICROB:
  84. data->probe = cht_int33fe_microb_probe;
  85. data->remove = cht_int33fe_microb_remove;
  86. break;
  87. case INT33FE_HW_TYPEC:
  88. data->probe = cht_int33fe_typec_probe;
  89. data->remove = cht_int33fe_typec_remove;
  90. break;
  91. }
  92. platform_set_drvdata(pdev, data);
  93. return data->probe(data);
  94. }
  95. static int cht_int33fe_remove(struct platform_device *pdev)
  96. {
  97. struct cht_int33fe_data *data = platform_get_drvdata(pdev);
  98. return data->remove(data);
  99. }
  100. static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
  101. { "INT33FE", },
  102. { }
  103. };
  104. MODULE_DEVICE_TABLE(acpi, cht_int33fe_acpi_ids);
  105. static struct platform_driver cht_int33fe_driver = {
  106. .driver = {
  107. .name = "Intel Cherry Trail ACPI INT33FE driver",
  108. .acpi_match_table = ACPI_PTR(cht_int33fe_acpi_ids),
  109. },
  110. .probe = cht_int33fe_probe,
  111. .remove = cht_int33fe_remove,
  112. };
  113. module_platform_driver(cht_int33fe_driver);
  114. MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE pseudo device driver");
  115. MODULE_AUTHOR("Yauhen Kharuzhy <jekhor@gmail.com>");
  116. MODULE_LICENSE("GPL v2");