drm/panfrost: Move gpu_{write, read}() macros to panfrost_regs.h
[linux/fpc-iii.git] / drivers / platform / x86 / intel_cht_int33fe.c
blob6fa3cced6f8e5809a8681750d940c4a09199b56c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Intel Cherry Trail ACPI INT33FE pseudo device driver
5 * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
7 * Some Intel Cherry Trail based device which ship with Windows 10, have
8 * this weird INT33FE ACPI device with a CRS table with 4 I2cSerialBusV2
9 * resources, for 4 different chips attached to various i2c busses:
10 * 1. The Whiskey Cove pmic, which is also described by the INT34D3 ACPI device
11 * 2. Maxim MAX17047 Fuel Gauge Controller
12 * 3. FUSB302 USB Type-C Controller
13 * 4. PI3USB30532 USB switch
15 * So this driver is a stub / pseudo driver whose only purpose is to
16 * instantiate i2c-clients for chips 2 - 4, so that standard i2c drivers
17 * for these chips can bind to the them.
20 #include <linux/acpi.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
28 #define EXPECTED_PTYPE 4
30 struct cht_int33fe_data {
31 struct i2c_client *max17047;
32 struct i2c_client *fusb302;
33 struct i2c_client *pi3usb30532;
34 /* Contain a list-head must be per device */
35 struct device_connection connections[4];
39 * Grrr I severly dislike buggy BIOS-es. At least one BIOS enumerates
40 * the max17047 both through the INT33FE ACPI device (it is right there
41 * in the resources table) as well as through a separate MAX17047 device.
43 * These helpers are used to work around this by checking if an i2c-client
44 * for the max17047 has already been registered.
46 static int cht_int33fe_check_for_max17047(struct device *dev, void *data)
48 struct i2c_client **max17047 = data;
49 struct acpi_device *adev;
50 const char *hid;
52 adev = ACPI_COMPANION(dev);
53 if (!adev)
54 return 0;
56 hid = acpi_device_hid(adev);
58 /* The MAX17047 ACPI node doesn't have an UID, so we don't check that */
59 if (strcmp(hid, "MAX17047"))
60 return 0;
62 *max17047 = to_i2c_client(dev);
63 return 1;
66 static struct i2c_client *cht_int33fe_find_max17047(void)
68 struct i2c_client *max17047 = NULL;
70 i2c_for_each_dev(&max17047, cht_int33fe_check_for_max17047);
71 return max17047;
74 static const char * const max17047_suppliers[] = { "bq24190-charger" };
76 static const struct property_entry max17047_props[] = {
77 PROPERTY_ENTRY_STRING_ARRAY("supplied-from", max17047_suppliers),
78 { }
81 static const struct property_entry fusb302_props[] = {
82 PROPERTY_ENTRY_STRING("linux,extcon-name", "cht_wcove_pwrsrc"),
83 PROPERTY_ENTRY_U32("fcs,max-sink-microvolt", 12000000),
84 PROPERTY_ENTRY_U32("fcs,max-sink-microamp", 3000000),
85 PROPERTY_ENTRY_U32("fcs,max-sink-microwatt", 36000000),
86 { }
89 static int cht_int33fe_probe(struct platform_device *pdev)
91 struct device *dev = &pdev->dev;
92 struct i2c_board_info board_info;
93 struct cht_int33fe_data *data;
94 struct i2c_client *max17047;
95 struct regulator *regulator;
96 unsigned long long ptyp;
97 acpi_status status;
98 int fusb302_irq;
99 int ret;
101 status = acpi_evaluate_integer(ACPI_HANDLE(dev), "PTYP", NULL, &ptyp);
102 if (ACPI_FAILURE(status)) {
103 dev_err(dev, "Error getting PTYPE\n");
104 return -ENODEV;
108 * The same ACPI HID is used for different configurations check PTYP
109 * to ensure that we are dealing with the expected config.
111 if (ptyp != EXPECTED_PTYPE)
112 return -ENODEV;
114 /* Check presence of INT34D3 (hardware-rev 3) expected for ptype == 4 */
115 if (!acpi_dev_present("INT34D3", "1", 3)) {
116 dev_err(dev, "Error PTYPE == %d, but no INT34D3 device\n",
117 EXPECTED_PTYPE);
118 return -ENODEV;
122 * We expect the WC PMIC to be paired with a TI bq24292i charger-IC.
123 * We check for the bq24292i vbus regulator here, this has 2 purposes:
124 * 1) The bq24292i allows charging with up to 12V, setting the fusb302's
125 * max-snk voltage to 12V with another charger-IC is not good.
126 * 2) For the fusb302 driver to get the bq24292i vbus regulator, the
127 * regulator-map, which is part of the bq24292i regulator_init_data,
128 * must be registered before the fusb302 is instantiated, otherwise
129 * it will end up with a dummy-regulator.
130 * Note "cht_wc_usb_typec_vbus" comes from the regulator_init_data
131 * which is defined in i2c-cht-wc.c from where the bq24292i i2c-client
132 * gets instantiated. We use regulator_get_optional here so that we
133 * don't end up getting a dummy-regulator ourselves.
135 regulator = regulator_get_optional(dev, "cht_wc_usb_typec_vbus");
136 if (IS_ERR(regulator)) {
137 ret = PTR_ERR(regulator);
138 return (ret == -ENODEV) ? -EPROBE_DEFER : ret;
140 regulator_put(regulator);
142 /* The FUSB302 uses the irq at index 1 and is the only irq user */
143 fusb302_irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 1);
144 if (fusb302_irq < 0) {
145 if (fusb302_irq != -EPROBE_DEFER)
146 dev_err(dev, "Error getting FUSB302 irq\n");
147 return fusb302_irq;
150 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
151 if (!data)
152 return -ENOMEM;
154 /* Work around BIOS bug, see comment on cht_int33fe_find_max17047 */
155 max17047 = cht_int33fe_find_max17047();
156 if (max17047) {
157 /* Pre-existing i2c-client for the max17047, add device-props */
158 ret = device_add_properties(&max17047->dev, max17047_props);
159 if (ret)
160 return ret;
161 /* And re-probe to get the new device-props applied. */
162 ret = device_reprobe(&max17047->dev);
163 if (ret)
164 dev_warn(dev, "Reprobing max17047 error: %d\n", ret);
165 } else {
166 memset(&board_info, 0, sizeof(board_info));
167 strlcpy(board_info.type, "max17047", I2C_NAME_SIZE);
168 board_info.dev_name = "max17047";
169 board_info.properties = max17047_props;
170 data->max17047 = i2c_acpi_new_device(dev, 1, &board_info);
171 if (IS_ERR(data->max17047))
172 return PTR_ERR(data->max17047);
175 data->connections[0].endpoint[0] = "port0";
176 data->connections[0].endpoint[1] = "i2c-pi3usb30532";
177 data->connections[0].id = "orientation-switch";
178 data->connections[1].endpoint[0] = "port0";
179 data->connections[1].endpoint[1] = "i2c-pi3usb30532";
180 data->connections[1].id = "mode-switch";
181 data->connections[2].endpoint[0] = "i2c-fusb302";
182 data->connections[2].endpoint[1] = "intel_xhci_usb_sw-role-switch";
183 data->connections[2].id = "usb-role-switch";
185 device_connections_add(data->connections);
187 memset(&board_info, 0, sizeof(board_info));
188 strlcpy(board_info.type, "typec_fusb302", I2C_NAME_SIZE);
189 board_info.dev_name = "fusb302";
190 board_info.properties = fusb302_props;
191 board_info.irq = fusb302_irq;
193 data->fusb302 = i2c_acpi_new_device(dev, 2, &board_info);
194 if (IS_ERR(data->fusb302)) {
195 ret = PTR_ERR(data->fusb302);
196 goto out_unregister_max17047;
199 memset(&board_info, 0, sizeof(board_info));
200 board_info.dev_name = "pi3usb30532";
201 strlcpy(board_info.type, "pi3usb30532", I2C_NAME_SIZE);
203 data->pi3usb30532 = i2c_acpi_new_device(dev, 3, &board_info);
204 if (IS_ERR(data->pi3usb30532)) {
205 ret = PTR_ERR(data->pi3usb30532);
206 goto out_unregister_fusb302;
209 platform_set_drvdata(pdev, data);
211 return 0;
213 out_unregister_fusb302:
214 i2c_unregister_device(data->fusb302);
216 out_unregister_max17047:
217 i2c_unregister_device(data->max17047);
219 device_connections_remove(data->connections);
221 return ret;
224 static int cht_int33fe_remove(struct platform_device *pdev)
226 struct cht_int33fe_data *data = platform_get_drvdata(pdev);
228 i2c_unregister_device(data->pi3usb30532);
229 i2c_unregister_device(data->fusb302);
230 i2c_unregister_device(data->max17047);
232 device_connections_remove(data->connections);
234 return 0;
237 static const struct acpi_device_id cht_int33fe_acpi_ids[] = {
238 { "INT33FE", },
241 MODULE_DEVICE_TABLE(acpi, cht_int33fe_acpi_ids);
243 static struct platform_driver cht_int33fe_driver = {
244 .driver = {
245 .name = "Intel Cherry Trail ACPI INT33FE driver",
246 .acpi_match_table = ACPI_PTR(cht_int33fe_acpi_ids),
248 .probe = cht_int33fe_probe,
249 .remove = cht_int33fe_remove,
252 module_platform_driver(cht_int33fe_driver);
254 MODULE_DESCRIPTION("Intel Cherry Trail ACPI INT33FE pseudo device driver");
255 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
256 MODULE_LICENSE("GPL v2");