1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/acpi.h>
5 #include <linux/bitfield.h>
6 #include <linux/device.h>
7 #include <linux/gpio/consumer.h>
8 #include <linux/gpio/machine.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/overflow.h>
13 #include <linux/platform_device.h>
14 #include <linux/uuid.h>
19 * 79234640-9e10-4fea-a5c1-b5aa8b19756f
20 * This _DSM GUID returns information about the GPIO lines mapped to a
21 * discrete INT3472 device. Function number 1 returns a count of the GPIO
22 * lines that are mapped. Subsequent functions return 32 bit ints encoding
23 * information about the GPIO line, including its purpose.
25 static const guid_t int3472_gpio_guid =
26 GUID_INIT(0x79234640, 0x9e10, 0x4fea,
27 0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
29 #define INT3472_GPIO_DSM_TYPE GENMASK(7, 0)
30 #define INT3472_GPIO_DSM_PIN GENMASK(15, 8)
31 #define INT3472_GPIO_DSM_SENSOR_ON_VAL GENMASK(31, 24)
34 * 822ace8f-2814-4174-a56b-5f029fe079ee
35 * This _DSM GUID returns a string from the sensor device, which acts as a
38 static const guid_t cio2_sensor_module_guid =
39 GUID_INIT(0x822ace8f, 0x2814, 0x4174,
40 0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
42 static void skl_int3472_log_sensor_module_name(struct int3472_discrete_device *int3472)
44 union acpi_object *obj;
46 obj = acpi_evaluate_dsm_typed(int3472->sensor->handle,
47 &cio2_sensor_module_guid, 0x00,
48 0x01, NULL, ACPI_TYPE_STRING);
50 dev_dbg(int3472->dev, "Sensor module id: '%s'\n", obj->string.pointer);
55 static int skl_int3472_fill_gpiod_lookup(struct gpiod_lookup *table_entry,
56 struct acpi_resource_gpio *agpio,
57 const char *func, u32 polarity)
59 char *path = agpio->resource_source.string_ptr;
60 struct acpi_device *adev;
64 status = acpi_get_handle(NULL, path, &handle);
65 if (ACPI_FAILURE(status))
68 adev = acpi_fetch_acpi_dev(handle);
72 table_entry->key = acpi_dev_name(adev);
73 table_entry->chip_hwnum = agpio->pin_table[0];
74 table_entry->con_id = func;
76 table_entry->flags = polarity;
81 static int skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device *int3472,
82 struct acpi_resource_gpio *agpio,
83 const char *func, u32 polarity)
87 if (int3472->n_sensor_gpios >= INT3472_MAX_SENSOR_GPIOS) {
88 dev_warn(int3472->dev, "Too many GPIOs mapped\n");
92 ret = skl_int3472_fill_gpiod_lookup(&int3472->gpios.table[int3472->n_sensor_gpios],
93 agpio, func, polarity);
97 int3472->n_sensor_gpios++;
102 /* This should *really* only be used when there's no other way... */
103 static struct gpio_desc *
104 skl_int3472_gpiod_get_from_temp_lookup(struct int3472_discrete_device *int3472,
105 struct acpi_resource_gpio *agpio,
106 const char *func, u32 polarity)
108 struct gpio_desc *desc;
111 struct gpiod_lookup_table *lookup __free(kfree) =
112 kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
114 return ERR_PTR(-ENOMEM);
116 lookup->dev_id = dev_name(int3472->dev);
117 ret = skl_int3472_fill_gpiod_lookup(&lookup->table[0], agpio, func, polarity);
121 gpiod_add_lookup_table(lookup);
122 desc = devm_gpiod_get(int3472->dev, func, GPIOD_OUT_LOW);
123 gpiod_remove_lookup_table(lookup);
128 static void int3472_get_func_and_polarity(u8 type, const char **func, u32 *polarity)
131 case INT3472_GPIO_TYPE_RESET:
133 *polarity = GPIO_ACTIVE_LOW;
135 case INT3472_GPIO_TYPE_POWERDOWN:
137 *polarity = GPIO_ACTIVE_LOW;
139 case INT3472_GPIO_TYPE_CLK_ENABLE:
140 *func = "clk-enable";
141 *polarity = GPIO_ACTIVE_HIGH;
143 case INT3472_GPIO_TYPE_PRIVACY_LED:
144 *func = "privacy-led";
145 *polarity = GPIO_ACTIVE_HIGH;
147 case INT3472_GPIO_TYPE_POWER_ENABLE:
148 *func = "power-enable";
149 *polarity = GPIO_ACTIVE_HIGH;
153 *polarity = GPIO_ACTIVE_HIGH;
159 * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor
160 * @ares: A pointer to a &struct acpi_resource
161 * @data: A pointer to a &struct int3472_discrete_device
163 * This function handles GPIO resources that are against an INT3472
164 * ACPI device, by checking the value of the corresponding _DSM entry.
165 * This will return a 32bit int, where the lowest byte represents the
166 * function of the GPIO pin:
174 * There are some known platform specific quirks where that does not quite
175 * hold up; for example where a pin with type 0x01 (Power down) is mapped to
176 * a sensor pin that performs a reset function or entries in _CRS and _DSM that
177 * do not actually correspond to a physical connection. These will be handled
178 * by the mapping sub-functions.
180 * GPIOs will either be mapped directly to the sensor device or else used
181 * to create clocks and regulators via the usual frameworks.
184 * * 1 - To continue the loop
185 * * 0 - When all resources found are handled properly.
186 * * -EINVAL - If the resource is not a GPIO IO resource
187 * * -ENODEV - If the resource has no corresponding _DSM entry
188 * * -Other - Errors propagated from one of the sub-functions.
190 static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares,
193 struct int3472_discrete_device *int3472 = data;
194 struct acpi_resource_gpio *agpio;
195 u8 active_value, pin, type;
196 union acpi_object *obj;
197 struct gpio_desc *gpio;
203 if (!acpi_gpio_get_io_resource(ares, &agpio))
207 * ngpios + 2 because the index of this _DSM function is 1-based and
208 * the first function is just a count.
210 obj = acpi_evaluate_dsm_typed(int3472->adev->handle,
211 &int3472_gpio_guid, 0x00,
213 NULL, ACPI_TYPE_INTEGER);
216 dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n",
217 agpio->pin_table[0]);
221 type = FIELD_GET(INT3472_GPIO_DSM_TYPE, obj->integer.value);
223 int3472_get_func_and_polarity(type, &func, &polarity);
225 pin = FIELD_GET(INT3472_GPIO_DSM_PIN, obj->integer.value);
226 if (pin != agpio->pin_table[0])
227 dev_warn(int3472->dev, "%s %s pin number mismatch _DSM %d resource %d\n",
228 func, agpio->resource_source.string_ptr, pin,
229 agpio->pin_table[0]);
231 active_value = FIELD_GET(INT3472_GPIO_DSM_SENSOR_ON_VAL, obj->integer.value);
233 polarity ^= GPIO_ACTIVE_LOW;
235 dev_dbg(int3472->dev, "%s %s pin %d active-%s\n", func,
236 agpio->resource_source.string_ptr, agpio->pin_table[0],
237 (polarity == GPIO_ACTIVE_HIGH) ? "high" : "low");
240 case INT3472_GPIO_TYPE_RESET:
241 case INT3472_GPIO_TYPE_POWERDOWN:
242 ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, func, polarity);
244 err_msg = "Failed to map GPIO pin to sensor\n";
247 case INT3472_GPIO_TYPE_CLK_ENABLE:
248 case INT3472_GPIO_TYPE_PRIVACY_LED:
249 case INT3472_GPIO_TYPE_POWER_ENABLE:
250 gpio = skl_int3472_gpiod_get_from_temp_lookup(int3472, agpio, func, polarity);
253 err_msg = "Failed to get GPIO\n";
258 case INT3472_GPIO_TYPE_CLK_ENABLE:
259 ret = skl_int3472_register_gpio_clock(int3472, gpio);
261 err_msg = "Failed to register clock\n";
264 case INT3472_GPIO_TYPE_PRIVACY_LED:
265 ret = skl_int3472_register_pled(int3472, gpio);
267 err_msg = "Failed to register LED\n";
270 case INT3472_GPIO_TYPE_POWER_ENABLE:
271 ret = skl_int3472_register_regulator(int3472, gpio);
273 err_msg = "Failed to map regulator to sensor\n";
276 default: /* Never reached */
282 dev_warn(int3472->dev,
283 "GPIO type 0x%02x unknown; the sensor may not work\n",
293 return dev_err_probe(int3472->dev, ret, err_msg);
298 static int skl_int3472_parse_crs(struct int3472_discrete_device *int3472)
300 LIST_HEAD(resource_list);
303 skl_int3472_log_sensor_module_name(int3472);
305 ret = acpi_dev_get_resources(int3472->adev, &resource_list,
306 skl_int3472_handle_gpio_resources,
311 acpi_dev_free_resource_list(&resource_list);
313 /* Register _DSM based clock (no-op if a GPIO clock was already registered) */
314 ret = skl_int3472_register_dsm_clock(int3472);
318 int3472->gpios.dev_id = int3472->sensor_name;
319 gpiod_add_lookup_table(&int3472->gpios);
324 static void skl_int3472_discrete_remove(struct platform_device *pdev)
326 struct int3472_discrete_device *int3472 = platform_get_drvdata(pdev);
328 gpiod_remove_lookup_table(&int3472->gpios);
330 skl_int3472_unregister_clock(int3472);
331 skl_int3472_unregister_pled(int3472);
332 skl_int3472_unregister_regulator(int3472);
335 static int skl_int3472_discrete_probe(struct platform_device *pdev)
337 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
338 struct int3472_discrete_device *int3472;
339 struct int3472_cldb cldb;
342 ret = skl_int3472_fill_cldb(adev, &cldb);
344 dev_err(&pdev->dev, "Couldn't fill CLDB structure\n");
348 if (cldb.control_logic_type != 1) {
349 dev_err(&pdev->dev, "Unsupported control logic type %u\n",
350 cldb.control_logic_type);
354 /* Max num GPIOs we've seen plus a terminator */
355 int3472 = devm_kzalloc(&pdev->dev, struct_size(int3472, gpios.table,
356 INT3472_MAX_SENSOR_GPIOS + 1), GFP_KERNEL);
360 int3472->adev = adev;
361 int3472->dev = &pdev->dev;
362 platform_set_drvdata(pdev, int3472);
363 int3472->clock.imgclk_index = cldb.clock_source;
365 ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor,
366 &int3472->sensor_name);
371 * Initialising this list means we can call gpiod_remove_lookup_table()
372 * in failure paths without issue.
374 INIT_LIST_HEAD(&int3472->gpios.list);
376 ret = skl_int3472_parse_crs(int3472);
378 skl_int3472_discrete_remove(pdev);
382 acpi_dev_clear_dependencies(adev);
386 static const struct acpi_device_id int3472_device_id[] = {
390 MODULE_DEVICE_TABLE(acpi, int3472_device_id);
392 static struct platform_driver int3472_discrete = {
394 .name = "int3472-discrete",
395 .acpi_match_table = int3472_device_id,
397 .probe = skl_int3472_discrete_probe,
398 .remove_new = skl_int3472_discrete_remove,
400 module_platform_driver(int3472_discrete);
402 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver");
404 MODULE_LICENSE("GPL v2");