treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / platform / x86 / intel_cht_int33fe_common.c
blob42dd11623f5651fbe51661c0f5ce5390c95b8d1a
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;
33 LIST_HEAD(resource_list);
34 int count = 0;
36 adev = ACPI_COMPANION(dev);
37 if (!adev)
38 return -EINVAL;
40 acpi_dev_get_resources(adev, &resource_list,
41 cht_int33fe_i2c_res_filter, &count);
43 acpi_dev_free_resource_list(&resource_list);
45 return count;
48 static int cht_int33fe_check_hw_type(struct device *dev)
50 unsigned long long ptyp;
51 acpi_status status;
52 int ret;
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;
61 * The same ACPI HID is used for different configurations check PTYP
62 * to ensure that we are dealing with the expected config.
64 if (ptyp != EXPECTED_PTYPE)
65 return -ENODEV;
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;
74 ret = cht_int33fe_count_i2c_clients(dev);
75 if (ret < 0)
76 return ret;
78 switch (ret) {
79 case 2:
80 return INT33FE_HW_MICROB;
81 case 4:
82 return INT33FE_HW_TYPEC;
83 default:
84 return -ENODEV;
88 static int cht_int33fe_probe(struct platform_device *pdev)
90 struct cht_int33fe_data *data;
91 struct device *dev = &pdev->dev;
92 int ret;
94 ret = cht_int33fe_check_hw_type(dev);
95 if (ret < 0)
96 return ret;
98 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
99 if (!data)
100 return -ENOMEM;
102 data->dev = dev;
104 switch (ret) {
105 case INT33FE_HW_MICROB:
106 data->probe = cht_int33fe_microb_probe;
107 data->remove = cht_int33fe_microb_remove;
108 break;
110 case INT33FE_HW_TYPEC:
111 data->probe = cht_int33fe_typec_probe;
112 data->remove = cht_int33fe_typec_remove;
113 break;
116 platform_set_drvdata(pdev, data);
118 return data->probe(data);
121 static int cht_int33fe_remove(struct platform_device *pdev)
123 struct cht_int33fe_data *data = platform_get_drvdata(pdev);
125 return data->remove(data);
128 static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
129 { "INT33FE", },
132 MODULE_DEVICE_TABLE(acpi, cht_int33fe_acpi_ids);
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),
139 .probe = cht_int33fe_probe,
140 .remove = cht_int33fe_remove,
143 module_platform_driver(cht_int33fe_driver);
145 MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE pseudo device driver");
146 MODULE_AUTHOR("Yauhen Kharuzhy <jekhor@gmail.com>");
147 MODULE_LICENSE("GPL v2");