]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/platform/x86/intel_cht_int33fe_common.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux.git] / drivers / platform / x86 / intel_cht_int33fe_common.c
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
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>
14
15 #include "intel_cht_int33fe_common.h"
16
17 #define EXPECTED_PTYPE          4
18
19 static int cht_int33fe_i2c_res_filter(struct acpi_resource *ares, void *data)
20 {
21         struct acpi_resource_i2c_serialbus *sb;
22         int *count = data;
23
24         if (i2c_acpi_get_i2c_resource(ares, &sb))
25                 (*count)++;
26
27         return 1;
28 }
29
30 static int cht_int33fe_count_i2c_clients(struct device *dev)
31 {
32         struct acpi_device *adev;
33         LIST_HEAD(resource_list);
34         int count = 0;
35
36         adev = ACPI_COMPANION(dev);
37         if (!adev)
38                 return -EINVAL;
39
40         acpi_dev_get_resources(adev, &resource_list,
41                                cht_int33fe_i2c_res_filter, &count);
42
43         acpi_dev_free_resource_list(&resource_list);
44
45         return count;
46 }
47
48 static int cht_int33fe_check_hw_type(struct device *dev)
49 {
50         unsigned long long ptyp;
51         acpi_status status;
52         int ret;
53
54         status = acpi_evaluate_integer(ACPI_HANDLE(dev), "PTYP", NULL, &ptyp);
55         if (ACPI_FAILURE(status)) {
56                 dev_err(dev, "Error getting PTYPE\n");
57                 return -ENODEV;
58         }
59
60         /*
61          * The same ACPI HID is used for different configurations check PTYP
62          * to ensure that we are dealing with the expected config.
63          */
64         if (ptyp != EXPECTED_PTYPE)
65                 return -ENODEV;
66
67         /* Check presence of INT34D3 (hardware-rev 3) expected for ptype == 4 */
68         if (!acpi_dev_present("INT34D3", "1", 3)) {
69                 dev_err(dev, "Error PTYPE == %d, but no INT34D3 device\n",
70                         EXPECTED_PTYPE);
71                 return -ENODEV;
72         }
73
74         ret = cht_int33fe_count_i2c_clients(dev);
75         if (ret < 0)
76                 return ret;
77
78         switch (ret) {
79         case 2:
80                 return INT33FE_HW_MICROB;
81         case 4:
82                 return INT33FE_HW_TYPEC;
83         default:
84                 return -ENODEV;
85         }
86 }
87
88 static int cht_int33fe_probe(struct platform_device *pdev)
89 {
90         struct cht_int33fe_data *data;
91         struct device *dev = &pdev->dev;
92         int ret;
93
94         ret = cht_int33fe_check_hw_type(dev);
95         if (ret < 0)
96                 return ret;
97
98         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
99         if (!data)
100                 return -ENOMEM;
101
102         data->dev = dev;
103
104         switch (ret) {
105         case INT33FE_HW_MICROB:
106                 data->probe = cht_int33fe_microb_probe;
107                 data->remove = cht_int33fe_microb_remove;
108                 break;
109
110         case INT33FE_HW_TYPEC:
111                 data->probe = cht_int33fe_typec_probe;
112                 data->remove = cht_int33fe_typec_remove;
113                 break;
114         }
115
116         platform_set_drvdata(pdev, data);
117
118         return data->probe(data);
119 }
120
121 static int cht_int33fe_remove(struct platform_device *pdev)
122 {
123         struct cht_int33fe_data *data = platform_get_drvdata(pdev);
124
125         return data->remove(data);
126 }
127
128 static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
129         { "INT33FE", },
130         { }
131 };
132 MODULE_DEVICE_TABLE(acpi, cht_int33fe_acpi_ids);
133
134 static struct platform_driver cht_int33fe_driver = {
135         .driver = {
136                 .name = "Intel Cherry Trail ACPI INT33FE driver",
137                 .acpi_match_table = ACPI_PTR(cht_int33fe_acpi_ids),
138         },
139         .probe = cht_int33fe_probe,
140         .remove = cht_int33fe_remove,
141 };
142
143 module_platform_driver(cht_int33fe_driver);
144
145 MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE pseudo device driver");
146 MODULE_AUTHOR("Yauhen Kharuzhy <jekhor@gmail.com>");
147 MODULE_LICENSE("GPL v2");