1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/acpi.h>
5 #include <linux/device.h>
6 #include <linux/gpio/consumer.h>
7 #include <linux/gpio/machine.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/overflow.h>
12 #include <linux/platform_device.h>
13 #include <linux/uuid.h>
18 * 79234640-9e10-4fea-a5c1-b5aa8b19756f
19 * This _DSM GUID returns information about the GPIO lines mapped to a
20 * discrete INT3472 device. Function number 1 returns a count of the GPIO
21 * lines that are mapped. Subsequent functions return 32 bit ints encoding
22 * information about the GPIO line, including its purpose.
24 static const guid_t int3472_gpio_guid =
25 GUID_INIT(0x79234640, 0x9e10, 0x4fea,
26 0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
29 * 822ace8f-2814-4174-a56b-5f029fe079ee
30 * This _DSM GUID returns a string from the sensor device, which acts as a
33 static const guid_t cio2_sensor_module_guid =
34 GUID_INIT(0x822ace8f, 0x2814, 0x4174,
35 0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
38 * Here follows platform specific mapping information that we can pass to
39 * the functions mapping resources to the sensors. Where the sensors have
40 * a power enable pin defined in DSDT we need to provide a supply name so
41 * the sensor drivers can find the regulator. The device name will be derived
42 * from the sensor's ACPI device within the code. Optionally, we can provide a
43 * NULL terminated array of function name mappings to deal with any platform
44 * specific deviations from the documented behaviour of GPIOs.
46 * Map a GPIO function name to NULL to prevent the driver from mapping that
50 static const struct int3472_gpio_function_remap ov2680_gpio_function_remaps[] = {
52 { "powerdown", "reset" },
56 static const struct int3472_sensor_config int3472_sensor_configs[] = {
57 /* Lenovo Miix 510-12ISK - OV2680, Front */
58 { "GNDF140809R", { 0 }, ov2680_gpio_function_remaps },
59 /* Lenovo Miix 510-12ISK - OV5648, Rear */
60 { "GEFF150023R", REGULATOR_SUPPLY("avdd", NULL), NULL },
61 /* Surface Go 1&2 - OV5693, Front */
62 { "YHCU", REGULATOR_SUPPLY("avdd", NULL), NULL },
65 static const struct int3472_sensor_config *
66 skl_int3472_get_sensor_module_config(struct int3472_discrete_device *int3472)
68 union acpi_object *obj;
71 obj = acpi_evaluate_dsm_typed(int3472->sensor->handle,
72 &cio2_sensor_module_guid, 0x00,
73 0x01, NULL, ACPI_TYPE_STRING);
77 "Failed to get sensor module string from _DSM\n");
78 return ERR_PTR(-ENODEV);
81 for (i = 0; i < ARRAY_SIZE(int3472_sensor_configs); i++) {
82 if (!strcmp(int3472_sensor_configs[i].sensor_module_name,
89 if (i >= ARRAY_SIZE(int3472_sensor_configs))
90 return ERR_PTR(-EINVAL);
92 return &int3472_sensor_configs[i];
95 static int skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device *int3472,
96 struct acpi_resource_gpio *agpio,
97 const char *func, u32 polarity)
99 const struct int3472_sensor_config *sensor_config;
100 char *path = agpio->resource_source.string_ptr;
101 struct gpiod_lookup *table_entry;
102 struct acpi_device *adev;
106 if (int3472->n_sensor_gpios >= INT3472_MAX_SENSOR_GPIOS) {
107 dev_warn(int3472->dev, "Too many GPIOs mapped\n");
111 sensor_config = int3472->sensor_config;
112 if (!IS_ERR(sensor_config) && sensor_config->function_maps) {
113 const struct int3472_gpio_function_remap *remap;
115 for (remap = sensor_config->function_maps; remap->documented; remap++) {
116 if (!strcmp(func, remap->documented)) {
117 func = remap->actual;
123 /* Functions mapped to NULL should not be mapped to the sensor */
127 status = acpi_get_handle(NULL, path, &handle);
128 if (ACPI_FAILURE(status))
131 adev = acpi_fetch_acpi_dev(handle);
135 table_entry = &int3472->gpios.table[int3472->n_sensor_gpios];
136 table_entry->key = acpi_dev_name(adev);
137 table_entry->chip_hwnum = agpio->pin_table[0];
138 table_entry->con_id = func;
139 table_entry->idx = 0;
140 table_entry->flags = polarity;
142 int3472->n_sensor_gpios++;
147 static void int3472_get_func_and_polarity(u8 type, const char **func, u32 *polarity)
150 case INT3472_GPIO_TYPE_RESET:
152 *polarity = GPIO_ACTIVE_LOW;
154 case INT3472_GPIO_TYPE_POWERDOWN:
156 *polarity = GPIO_ACTIVE_LOW;
158 case INT3472_GPIO_TYPE_CLK_ENABLE:
159 *func = "clk-enable";
160 *polarity = GPIO_ACTIVE_HIGH;
162 case INT3472_GPIO_TYPE_PRIVACY_LED:
163 *func = "privacy-led";
164 *polarity = GPIO_ACTIVE_HIGH;
166 case INT3472_GPIO_TYPE_POWER_ENABLE:
167 *func = "power-enable";
168 *polarity = GPIO_ACTIVE_HIGH;
172 *polarity = GPIO_ACTIVE_HIGH;
178 * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor
179 * @ares: A pointer to a &struct acpi_resource
180 * @data: A pointer to a &struct int3472_discrete_device
182 * This function handles GPIO resources that are against an INT3472
183 * ACPI device, by checking the value of the corresponding _DSM entry.
184 * This will return a 32bit int, where the lowest byte represents the
185 * function of the GPIO pin:
193 * There are some known platform specific quirks where that does not quite
194 * hold up; for example where a pin with type 0x01 (Power down) is mapped to
195 * a sensor pin that performs a reset function or entries in _CRS and _DSM that
196 * do not actually correspond to a physical connection. These will be handled
197 * by the mapping sub-functions.
199 * GPIOs will either be mapped directly to the sensor device or else used
200 * to create clocks and regulators via the usual frameworks.
203 * * 1 - To continue the loop
204 * * 0 - When all resources found are handled properly.
205 * * -EINVAL - If the resource is not a GPIO IO resource
206 * * -ENODEV - If the resource has no corresponding _DSM entry
207 * * -Other - Errors propagated from one of the sub-functions.
209 static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares,
212 struct int3472_discrete_device *int3472 = data;
213 struct acpi_resource_gpio *agpio;
214 union acpi_object *obj;
215 u8 active_value, type;
221 if (!acpi_gpio_get_io_resource(ares, &agpio))
225 * ngpios + 2 because the index of this _DSM function is 1-based and
226 * the first function is just a count.
228 obj = acpi_evaluate_dsm_typed(int3472->adev->handle,
229 &int3472_gpio_guid, 0x00,
231 NULL, ACPI_TYPE_INTEGER);
234 dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n",
235 agpio->pin_table[0]);
239 type = obj->integer.value & 0xff;
241 int3472_get_func_and_polarity(type, &func, &polarity);
243 /* If bits 31-24 of the _DSM entry are all 0 then the signal is inverted */
244 active_value = obj->integer.value >> 24;
246 polarity ^= GPIO_ACTIVE_LOW;
248 dev_dbg(int3472->dev, "%s %s pin %d active-%s\n", func,
249 agpio->resource_source.string_ptr, agpio->pin_table[0],
250 (polarity == GPIO_ACTIVE_HIGH) ? "high" : "low");
253 case INT3472_GPIO_TYPE_RESET:
254 case INT3472_GPIO_TYPE_POWERDOWN:
255 ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, func, polarity);
257 err_msg = "Failed to map GPIO pin to sensor\n";
260 case INT3472_GPIO_TYPE_CLK_ENABLE:
261 ret = skl_int3472_register_clock(int3472, agpio, polarity);
263 err_msg = "Failed to register clock\n";
266 case INT3472_GPIO_TYPE_PRIVACY_LED:
267 ret = skl_int3472_register_pled(int3472, agpio, polarity);
269 err_msg = "Failed to register LED\n";
272 case INT3472_GPIO_TYPE_POWER_ENABLE:
273 ret = skl_int3472_register_regulator(int3472, agpio);
275 err_msg = "Failed to map regulator to sensor\n";
279 dev_warn(int3472->dev,
280 "GPIO type 0x%02x unknown; the sensor may not work\n",
290 return dev_err_probe(int3472->dev, ret, err_msg);
295 static int skl_int3472_parse_crs(struct int3472_discrete_device *int3472)
297 LIST_HEAD(resource_list);
301 * No error check, because not having a sensor config is not necessarily
304 int3472->sensor_config = skl_int3472_get_sensor_module_config(int3472);
306 ret = acpi_dev_get_resources(int3472->adev, &resource_list,
307 skl_int3472_handle_gpio_resources,
312 acpi_dev_free_resource_list(&resource_list);
314 int3472->gpios.dev_id = int3472->sensor_name;
315 gpiod_add_lookup_table(&int3472->gpios);
320 static int skl_int3472_discrete_remove(struct platform_device *pdev)
322 struct int3472_discrete_device *int3472 = platform_get_drvdata(pdev);
324 gpiod_remove_lookup_table(&int3472->gpios);
326 skl_int3472_unregister_clock(int3472);
327 skl_int3472_unregister_pled(int3472);
328 skl_int3472_unregister_regulator(int3472);
333 static int skl_int3472_discrete_probe(struct platform_device *pdev)
335 struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
336 struct int3472_discrete_device *int3472;
337 struct int3472_cldb cldb;
340 ret = skl_int3472_fill_cldb(adev, &cldb);
342 dev_err(&pdev->dev, "Couldn't fill CLDB structure\n");
346 if (cldb.control_logic_type != 1) {
347 dev_err(&pdev->dev, "Unsupported control logic type %u\n",
348 cldb.control_logic_type);
352 /* Max num GPIOs we've seen plus a terminator */
353 int3472 = devm_kzalloc(&pdev->dev, struct_size(int3472, gpios.table,
354 INT3472_MAX_SENSOR_GPIOS + 1), GFP_KERNEL);
358 int3472->adev = adev;
359 int3472->dev = &pdev->dev;
360 platform_set_drvdata(pdev, int3472);
362 ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor,
363 &int3472->sensor_name);
368 * Initialising this list means we can call gpiod_remove_lookup_table()
369 * in failure paths without issue.
371 INIT_LIST_HEAD(&int3472->gpios.list);
373 ret = skl_int3472_parse_crs(int3472);
375 skl_int3472_discrete_remove(pdev);
379 acpi_dev_clear_dependencies(adev);
383 static const struct acpi_device_id int3472_device_id[] = {
387 MODULE_DEVICE_TABLE(acpi, int3472_device_id);
389 static struct platform_driver int3472_discrete = {
391 .name = "int3472-discrete",
392 .acpi_match_table = int3472_device_id,
394 .probe = skl_int3472_discrete_probe,
395 .remove = skl_int3472_discrete_remove,
397 module_platform_driver(int3472_discrete);
399 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver");
401 MODULE_LICENSE("GPL v2");