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
cht_int33fe_i2c_res_filter(struct acpi_resource * ares,void * data)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
cht_int33fe_count_i2c_clients(struct device * dev)30 static int cht_int33fe_count_i2c_clients(struct device *dev)
31 {
32 struct acpi_device *adev = ACPI_COMPANION(dev);
33 LIST_HEAD(resource_list);
34 int count = 0;
35 int ret;
36
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;
42
43 return count;
44 }
45
cht_int33fe_check_hw_type(struct device * dev)46 static int cht_int33fe_check_hw_type(struct device *dev)
47 {
48 unsigned long long ptyp;
49 acpi_status status;
50 int ret;
51
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;
56 }
57
58 /*
59 * The same ACPI HID is used for different configurations check PTYP
60 * to ensure that we are dealing with the expected config.
61 */
62 if (ptyp != EXPECTED_PTYPE)
63 return -ENODEV;
64
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;
70 }
71
72 ret = cht_int33fe_count_i2c_clients(dev);
73 if (ret < 0)
74 return ret;
75
76 switch (ret) {
77 case 2:
78 return INT33FE_HW_MICROB;
79 case 4:
80 return INT33FE_HW_TYPEC;
81 default:
82 return -ENODEV;
83 }
84 }
85
cht_int33fe_probe(struct platform_device * pdev)86 static int cht_int33fe_probe(struct platform_device *pdev)
87 {
88 struct cht_int33fe_data *data;
89 struct device *dev = &pdev->dev;
90 int ret;
91
92 ret = cht_int33fe_check_hw_type(dev);
93 if (ret < 0)
94 return ret;
95
96 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
97 if (!data)
98 return -ENOMEM;
99
100 data->dev = dev;
101
102 switch (ret) {
103 case INT33FE_HW_MICROB:
104 data->probe = cht_int33fe_microb_probe;
105 data->remove = cht_int33fe_microb_remove;
106 break;
107
108 case INT33FE_HW_TYPEC:
109 data->probe = cht_int33fe_typec_probe;
110 data->remove = cht_int33fe_typec_remove;
111 break;
112 }
113
114 platform_set_drvdata(pdev, data);
115
116 return data->probe(data);
117 }
118
cht_int33fe_remove(struct platform_device * pdev)119 static int cht_int33fe_remove(struct platform_device *pdev)
120 {
121 struct cht_int33fe_data *data = platform_get_drvdata(pdev);
122
123 return data->remove(data);
124 }
125
126 static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
127 { "INT33FE", },
128 { }
129 };
130 MODULE_DEVICE_TABLE(acpi, cht_int33fe_acpi_ids);
131
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),
136 },
137 .probe = cht_int33fe_probe,
138 .remove = cht_int33fe_remove,
139 };
140
141 module_platform_driver(cht_int33fe_driver);
142
143 MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE pseudo device driver");
144 MODULE_AUTHOR("Yauhen Kharuzhy <jekhor@gmail.com>");
145 MODULE_LICENSE("GPL v2");
146