Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / platform / x86 / intel_cht_int33fe_common.c
blob251ed9bac789114a161d517ddf797c99816bdf54
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).
6 * Copyright (c) 2019 Yauhen Kharuzhy <jekhor@gmail.com>
7 */
9 #include <linux/acpi.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
15 #include "intel_cht_int33fe_common.h"
17 #define EXPECTED_PTYPE 4
19 static int cht_int33fe_i2c_res_filter(struct acpi_resource *ares, void *data)
21 struct acpi_resource_i2c_serialbus *sb;
22 int *count = data;
24 if (i2c_acpi_get_i2c_resource(ares, &sb))
25 (*count)++;
27 return 1;
30 static int cht_int33fe_count_i2c_clients(struct device *dev)
32 struct acpi_device *adev = ACPI_COMPANION(dev);
33 LIST_HEAD(resource_list);
34 int count = 0;
35 int ret;
37 ret = acpi_dev_get_resources(adev, &resource_list,
38 cht_int33fe_i2c_res_filter, &count);
39 acpi_dev_free_resource_list(&resource_list);
40 if (ret < 0)
41 return ret;
43 return count;
46 static int cht_int33fe_check_hw_type(struct device *dev)
48 unsigned long long ptyp;
49 acpi_status status;
50 int ret;
52 status = acpi_evaluate_integer(ACPI_HANDLE(dev), "PTYP", NULL, &ptyp);
53 if (ACPI_FAILURE(status)) {
54 dev_err(dev, "Error getting PTYPE\n");
55 return -ENODEV;
59 * The same ACPI HID is used for different configurations check PTYP
60 * to ensure that we are dealing with the expected config.
62 if (ptyp != EXPECTED_PTYPE)
63 return -ENODEV;
65 /* Check presence of INT34D3 (hardware-rev 3) expected for ptype == 4 */
66 if (!acpi_dev_present("INT34D3", "1", 3)) {
67 dev_err(dev, "Error PTYPE == %d, but no INT34D3 device\n",
68 EXPECTED_PTYPE);
69 return -ENODEV;
72 ret = cht_int33fe_count_i2c_clients(dev);
73 if (ret < 0)
74 return ret;
76 switch (ret) {
77 case 2:
78 return INT33FE_HW_MICROB;
79 case 4:
80 return INT33FE_HW_TYPEC;
81 default:
82 return -ENODEV;
86 static int cht_int33fe_probe(struct platform_device *pdev)
88 struct cht_int33fe_data *data;
89 struct device *dev = &pdev->dev;
90 int ret;
92 ret = cht_int33fe_check_hw_type(dev);
93 if (ret < 0)
94 return ret;
96 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
97 if (!data)
98 return -ENOMEM;
100 data->dev = dev;
102 switch (ret) {
103 case INT33FE_HW_MICROB:
104 data->probe = cht_int33fe_microb_probe;
105 data->remove = cht_int33fe_microb_remove;
106 break;
108 case INT33FE_HW_TYPEC:
109 data->probe = cht_int33fe_typec_probe;
110 data->remove = cht_int33fe_typec_remove;
111 break;
114 platform_set_drvdata(pdev, data);
116 return data->probe(data);
119 static int cht_int33fe_remove(struct platform_device *pdev)
121 struct cht_int33fe_data *data = platform_get_drvdata(pdev);
123 return data->remove(data);
126 static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
127 { "INT33FE", },
130 MODULE_DEVICE_TABLE(acpi, cht_int33fe_acpi_ids);
132 static struct platform_driver cht_int33fe_driver = {
133 .driver = {
134 .name = "Intel Cherry Trail ACPI INT33FE driver",
135 .acpi_match_table = ACPI_PTR(cht_int33fe_acpi_ids),
137 .probe = cht_int33fe_probe,
138 .remove = cht_int33fe_remove,
141 module_platform_driver(cht_int33fe_driver);
143 MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE pseudo device driver");
144 MODULE_AUTHOR("Yauhen Kharuzhy <jekhor@gmail.com>");
145 MODULE_LICENSE("GPL v2");