Linux Audio

Check our new training course

Linux BSP development engineering services

Need help to port Linux and bootloaders to your hardware?
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2/* Author: Dan Scally <djrscally@gmail.com> */
  3
  4#include <linux/acpi.h>
  5#include <linux/clkdev.h>
  6#include <linux/clk-provider.h>
  7#include <linux/device.h>
  8#include <linux/gpio/consumer.h>
  9#include <linux/gpio/machine.h>
 10#include <linux/i2c.h>
 11#include <linux/kernel.h>
 12#include <linux/module.h>
 13#include <linux/overflow.h>
 14#include <linux/platform_device.h>
 15#include <linux/uuid.h>
 16
 17#include "common.h"
 18
 19/*
 20 * 79234640-9e10-4fea-a5c1-b5aa8b19756f
 21 * This _DSM GUID returns information about the GPIO lines mapped to a
 22 * discrete INT3472 device. Function number 1 returns a count of the GPIO
 23 * lines that are mapped. Subsequent functions return 32 bit ints encoding
 24 * information about the GPIO line, including its purpose.
 25 */
 26static const guid_t int3472_gpio_guid =
 27	GUID_INIT(0x79234640, 0x9e10, 0x4fea,
 28		  0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
 29
 
 
 
 
 30/*
 31 * 822ace8f-2814-4174-a56b-5f029fe079ee
 32 * This _DSM GUID returns a string from the sensor device, which acts as a
 33 * module identifier.
 34 */
 35static const guid_t cio2_sensor_module_guid =
 36	GUID_INIT(0x822ace8f, 0x2814, 0x4174,
 37		  0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
 38
 39/*
 40 * Here follows platform specific mapping information that we can pass to
 41 * the functions mapping resources to the sensors. Where the sensors have
 42 * a power enable pin defined in DSDT we need to provide a supply name so
 43 * the sensor drivers can find the regulator. The device name will be derived
 44 * from the sensor's ACPI device within the code. Optionally, we can provide a
 45 * NULL terminated array of function name mappings to deal with any platform
 46 * specific deviations from the documented behaviour of GPIOs.
 47 *
 48 * Map a GPIO function name to NULL to prevent the driver from mapping that
 49 * GPIO at all.
 50 */
 51
 52static const struct int3472_gpio_function_remap ov2680_gpio_function_remaps[] = {
 53	{ "reset", NULL },
 54	{ "powerdown", "reset" },
 55	{ }
 56};
 57
 58static const struct int3472_sensor_config int3472_sensor_configs[] = {
 59	/* Lenovo Miix 510-12ISK - OV2680, Front */
 60	{ "GNDF140809R", { 0 }, ov2680_gpio_function_remaps },
 61	/* Lenovo Miix 510-12ISK - OV5648, Rear */
 62	{ "GEFF150023R", REGULATOR_SUPPLY("avdd", NULL), NULL },
 63	/* Surface Go 1&2 - OV5693, Front */
 64	{ "YHCU", REGULATOR_SUPPLY("avdd", NULL), NULL },
 65};
 66
 67static const struct int3472_sensor_config *
 68skl_int3472_get_sensor_module_config(struct int3472_discrete_device *int3472)
 69{
 70	union acpi_object *obj;
 71	unsigned int i;
 72
 73	obj = acpi_evaluate_dsm_typed(int3472->sensor->handle,
 74				      &cio2_sensor_module_guid, 0x00,
 75				      0x01, NULL, ACPI_TYPE_STRING);
 76
 77	if (!obj) {
 78		dev_err(int3472->dev,
 79			"Failed to get sensor module string from _DSM\n");
 80		return ERR_PTR(-ENODEV);
 81	}
 82
 83	if (obj->string.type != ACPI_TYPE_STRING) {
 84		dev_err(int3472->dev,
 85			"Sensor _DSM returned a non-string value\n");
 86
 87		ACPI_FREE(obj);
 88		return ERR_PTR(-EINVAL);
 89	}
 90
 91	for (i = 0; i < ARRAY_SIZE(int3472_sensor_configs); i++) {
 92		if (!strcmp(int3472_sensor_configs[i].sensor_module_name,
 93			    obj->string.pointer))
 94			break;
 95	}
 96
 97	ACPI_FREE(obj);
 98
 99	if (i >= ARRAY_SIZE(int3472_sensor_configs))
100		return ERR_PTR(-EINVAL);
101
102	return &int3472_sensor_configs[i];
103}
104
105static int skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device *int3472,
106					  struct acpi_resource_gpio *agpio,
107					  const char *func, u32 polarity)
108{
109	const struct int3472_sensor_config *sensor_config;
110	char *path = agpio->resource_source.string_ptr;
111	struct gpiod_lookup *table_entry;
112	struct acpi_device *adev;
113	acpi_handle handle;
114	acpi_status status;
115
116	if (int3472->n_sensor_gpios >= INT3472_MAX_SENSOR_GPIOS) {
117		dev_warn(int3472->dev, "Too many GPIOs mapped\n");
118		return -EINVAL;
119	}
120
121	sensor_config = int3472->sensor_config;
122	if (!IS_ERR(sensor_config) && sensor_config->function_maps) {
123		const struct int3472_gpio_function_remap *remap;
124
125		for (remap = sensor_config->function_maps; remap->documented; remap++) {
126			if (!strcmp(func, remap->documented)) {
127				func = remap->actual;
128				break;
129			}
130		}
131	}
132
133	/* Functions mapped to NULL should not be mapped to the sensor */
134	if (!func)
135		return 0;
136
137	status = acpi_get_handle(NULL, path, &handle);
138	if (ACPI_FAILURE(status))
139		return -EINVAL;
140
141	adev = acpi_fetch_acpi_dev(handle);
142	if (!adev)
143		return -ENODEV;
144
145	table_entry = &int3472->gpios.table[int3472->n_sensor_gpios];
146	table_entry->key = acpi_dev_name(adev);
147	table_entry->chip_hwnum = agpio->pin_table[0];
148	table_entry->con_id = func;
149	table_entry->idx = 0;
150	table_entry->flags = polarity;
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152	int3472->n_sensor_gpios++;
153
154	return 0;
155}
156
157static int skl_int3472_map_gpio_to_clk(struct int3472_discrete_device *int3472,
158				       struct acpi_resource_gpio *agpio, u8 type)
 
 
 
159{
160	char *path = agpio->resource_source.string_ptr;
161	u16 pin = agpio->pin_table[0];
162	struct gpio_desc *gpio;
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164	switch (type) {
 
 
 
 
 
 
 
 
165	case INT3472_GPIO_TYPE_CLK_ENABLE:
166		gpio = acpi_get_and_request_gpiod(path, pin, "int3472,clk-enable");
167		if (IS_ERR(gpio))
168			return (PTR_ERR(gpio));
169
170		int3472->clock.ena_gpio = gpio;
171		/* Ensure the pin is in output mode and non-active state */
172		gpiod_direction_output(int3472->clock.ena_gpio, 0);
173		break;
174	case INT3472_GPIO_TYPE_PRIVACY_LED:
175		gpio = acpi_get_and_request_gpiod(path, pin, "int3472,privacy-led");
176		if (IS_ERR(gpio))
177			return (PTR_ERR(gpio));
178
179		int3472->clock.led_gpio = gpio;
180		/* Ensure the pin is in output mode and non-active state */
181		gpiod_direction_output(int3472->clock.led_gpio, 0);
182		break;
183	default:
184		dev_err(int3472->dev, "Invalid GPIO type 0x%02x for clock\n", type);
 
185		break;
186	}
187
188	return 0;
189}
190
191/**
192 * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor
193 * @ares: A pointer to a &struct acpi_resource
194 * @data: A pointer to a &struct int3472_discrete_device
195 *
196 * This function handles GPIO resources that are against an INT3472
197 * ACPI device, by checking the value of the corresponding _DSM entry.
198 * This will return a 32bit int, where the lowest byte represents the
199 * function of the GPIO pin:
200 *
201 * 0x00 Reset
202 * 0x01 Power down
203 * 0x0b Power enable
204 * 0x0c Clock enable
205 * 0x0d Privacy LED
206 *
207 * There are some known platform specific quirks where that does not quite
208 * hold up; for example where a pin with type 0x01 (Power down) is mapped to
209 * a sensor pin that performs a reset function or entries in _CRS and _DSM that
210 * do not actually correspond to a physical connection. These will be handled
211 * by the mapping sub-functions.
212 *
213 * GPIOs will either be mapped directly to the sensor device or else used
214 * to create clocks and regulators via the usual frameworks.
215 *
216 * Return:
217 * * 1		- To continue the loop
218 * * 0		- When all resources found are handled properly.
219 * * -EINVAL	- If the resource is not a GPIO IO resource
220 * * -ENODEV	- If the resource has no corresponding _DSM entry
221 * * -Other	- Errors propagated from one of the sub-functions.
222 */
223static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares,
224					     void *data)
225{
226	struct int3472_discrete_device *int3472 = data;
227	struct acpi_resource_gpio *agpio;
 
228	union acpi_object *obj;
 
229	const char *err_msg;
 
 
230	int ret;
231	u8 type;
232
233	if (!acpi_gpio_get_io_resource(ares, &agpio))
234		return 1;
235
236	/*
237	 * ngpios + 2 because the index of this _DSM function is 1-based and
238	 * the first function is just a count.
239	 */
240	obj = acpi_evaluate_dsm_typed(int3472->adev->handle,
241				      &int3472_gpio_guid, 0x00,
242				      int3472->ngpios + 2,
243				      NULL, ACPI_TYPE_INTEGER);
244
245	if (!obj) {
246		dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n",
247			 agpio->pin_table[0]);
248		return 1;
249	}
250
251	type = obj->integer.value & 0xff;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252
253	switch (type) {
254	case INT3472_GPIO_TYPE_RESET:
255		ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, "reset",
256						     GPIO_ACTIVE_LOW);
257		if (ret)
258			err_msg = "Failed to map reset pin to sensor\n";
259
260		break;
261	case INT3472_GPIO_TYPE_POWERDOWN:
262		ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, "powerdown",
263						     GPIO_ACTIVE_LOW);
264		if (ret)
265			err_msg = "Failed to map powerdown pin to sensor\n";
266
267		break;
268	case INT3472_GPIO_TYPE_CLK_ENABLE:
269	case INT3472_GPIO_TYPE_PRIVACY_LED:
270		ret = skl_int3472_map_gpio_to_clk(int3472, agpio, type);
271		if (ret)
272			err_msg = "Failed to map GPIO to clock\n";
273
274		break;
275	case INT3472_GPIO_TYPE_POWER_ENABLE:
276		ret = skl_int3472_register_regulator(int3472, agpio);
277		if (ret)
278			err_msg = "Failed to map regulator to sensor\n";
 
 
 
 
 
 
 
 
 
279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280		break;
281	default:
282		dev_warn(int3472->dev,
283			 "GPIO type 0x%02x unknown; the sensor may not work\n",
284			 type);
285		ret = 1;
286		break;
287	}
288
289	int3472->ngpios++;
290	ACPI_FREE(obj);
291
292	if (ret < 0)
293		return dev_err_probe(int3472->dev, ret, err_msg);
294
295	return ret;
296}
297
298static int skl_int3472_parse_crs(struct int3472_discrete_device *int3472)
299{
300	LIST_HEAD(resource_list);
301	int ret;
302
303	/*
304	 * No error check, because not having a sensor config is not necessarily
305	 * a failure mode.
306	 */
307	int3472->sensor_config = skl_int3472_get_sensor_module_config(int3472);
308
309	ret = acpi_dev_get_resources(int3472->adev, &resource_list,
310				     skl_int3472_handle_gpio_resources,
311				     int3472);
312	if (ret < 0)
313		return ret;
314
315	acpi_dev_free_resource_list(&resource_list);
316
317	/*
318	 * If we find no clock enable GPIO pin then the privacy LED won't work.
319	 * We've never seen that situation, but it's possible. Warn the user so
320	 * it's clear what's happened.
321	 */
322	if (int3472->clock.ena_gpio) {
323		ret = skl_int3472_register_clock(int3472);
324		if (ret)
325			return ret;
326	} else {
327		if (int3472->clock.led_gpio)
328			dev_warn(int3472->dev,
329				 "No clk GPIO. The privacy LED won't work\n");
330	}
331
332	int3472->gpios.dev_id = int3472->sensor_name;
333	gpiod_add_lookup_table(&int3472->gpios);
334
335	return 0;
336}
337
338static int skl_int3472_discrete_remove(struct platform_device *pdev)
339{
340	struct int3472_discrete_device *int3472 = platform_get_drvdata(pdev);
341
342	gpiod_remove_lookup_table(&int3472->gpios);
343
344	if (int3472->clock.cl)
345		skl_int3472_unregister_clock(int3472);
346
347	gpiod_put(int3472->clock.ena_gpio);
348	gpiod_put(int3472->clock.led_gpio);
349
350	skl_int3472_unregister_regulator(int3472);
351
352	return 0;
353}
354
355static int skl_int3472_discrete_probe(struct platform_device *pdev)
356{
357	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
358	struct int3472_discrete_device *int3472;
359	struct int3472_cldb cldb;
360	int ret;
361
362	ret = skl_int3472_fill_cldb(adev, &cldb);
363	if (ret) {
364		dev_err(&pdev->dev, "Couldn't fill CLDB structure\n");
365		return ret;
366	}
367
368	if (cldb.control_logic_type != 1) {
369		dev_err(&pdev->dev, "Unsupported control logic type %u\n",
370			cldb.control_logic_type);
371		return -EINVAL;
372	}
373
374	/* Max num GPIOs we've seen plus a terminator */
375	int3472 = devm_kzalloc(&pdev->dev, struct_size(int3472, gpios.table,
376			       INT3472_MAX_SENSOR_GPIOS + 1), GFP_KERNEL);
377	if (!int3472)
378		return -ENOMEM;
379
380	int3472->adev = adev;
381	int3472->dev = &pdev->dev;
382	platform_set_drvdata(pdev, int3472);
 
383
384	ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor,
385						   &int3472->sensor_name);
386	if (ret)
387		return ret;
388
389	/*
390	 * Initialising this list means we can call gpiod_remove_lookup_table()
391	 * in failure paths without issue.
392	 */
393	INIT_LIST_HEAD(&int3472->gpios.list);
394
395	ret = skl_int3472_parse_crs(int3472);
396	if (ret) {
397		skl_int3472_discrete_remove(pdev);
398		return ret;
399	}
400
401	acpi_dev_clear_dependencies(adev);
402	return 0;
403}
404
405static const struct acpi_device_id int3472_device_id[] = {
406	{ "INT3472", 0 },
407	{ }
408};
409MODULE_DEVICE_TABLE(acpi, int3472_device_id);
410
411static struct platform_driver int3472_discrete = {
412	.driver = {
413		.name = "int3472-discrete",
414		.acpi_match_table = int3472_device_id,
415	},
416	.probe = skl_int3472_discrete_probe,
417	.remove = skl_int3472_discrete_remove,
418};
419module_platform_driver(int3472_discrete);
420
421MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver");
422MODULE_AUTHOR("Daniel Scally <djrscally@gmail.com>");
423MODULE_LICENSE("GPL v2");
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/* Author: Dan Scally <djrscally@gmail.com> */
  3
  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>
  9#include <linux/i2c.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>
 15
 16#include "common.h"
 17
 18/*
 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.
 24 */
 25static const guid_t int3472_gpio_guid =
 26	GUID_INIT(0x79234640, 0x9e10, 0x4fea,
 27		  0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
 28
 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)
 32
 33/*
 34 * 822ace8f-2814-4174-a56b-5f029fe079ee
 35 * This _DSM GUID returns a string from the sensor device, which acts as a
 36 * module identifier.
 37 */
 38static const guid_t cio2_sensor_module_guid =
 39	GUID_INIT(0x822ace8f, 0x2814, 0x4174,
 40		  0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
 41
 42static void skl_int3472_log_sensor_module_name(struct int3472_discrete_device *int3472)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 43{
 44	union acpi_object *obj;
 
 45
 46	obj = acpi_evaluate_dsm_typed(int3472->sensor->handle,
 47				      &cio2_sensor_module_guid, 0x00,
 48				      0x01, NULL, ACPI_TYPE_STRING);
 49	if (obj) {
 50		dev_dbg(int3472->dev, "Sensor module id: '%s'\n", obj->string.pointer);
 
 
 
 
 
 
 
 
 
 51		ACPI_FREE(obj);
 
 52	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 53}
 54
 55static int skl_int3472_fill_gpiod_lookup(struct gpiod_lookup *table_entry,
 56					 struct acpi_resource_gpio *agpio,
 57					 const char *func, u32 polarity)
 58{
 
 59	char *path = agpio->resource_source.string_ptr;
 
 60	struct acpi_device *adev;
 61	acpi_handle handle;
 62	acpi_status status;
 63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 64	status = acpi_get_handle(NULL, path, &handle);
 65	if (ACPI_FAILURE(status))
 66		return -EINVAL;
 67
 68	adev = acpi_fetch_acpi_dev(handle);
 69	if (!adev)
 70		return -ENODEV;
 71
 
 72	table_entry->key = acpi_dev_name(adev);
 73	table_entry->chip_hwnum = agpio->pin_table[0];
 74	table_entry->con_id = func;
 75	table_entry->idx = 0;
 76	table_entry->flags = polarity;
 77
 78	return 0;
 79}
 80
 81static int skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device *int3472,
 82					  struct acpi_resource_gpio *agpio,
 83					  const char *func, u32 polarity)
 84{
 85	int ret;
 86
 87	if (int3472->n_sensor_gpios >= INT3472_MAX_SENSOR_GPIOS) {
 88		dev_warn(int3472->dev, "Too many GPIOs mapped\n");
 89		return -EINVAL;
 90	}
 91
 92	ret = skl_int3472_fill_gpiod_lookup(&int3472->gpios.table[int3472->n_sensor_gpios],
 93					    agpio, func, polarity);
 94	if (ret)
 95		return ret;
 96
 97	int3472->n_sensor_gpios++;
 98
 99	return 0;
100}
101
102/* This should *really* only be used when there's no other way... */
103static struct gpio_desc *
104skl_int3472_gpiod_get_from_temp_lookup(struct int3472_discrete_device *int3472,
105				       struct acpi_resource_gpio *agpio,
106				       const char *func, u32 polarity)
107{
108	struct gpio_desc *desc;
109	int ret;
 
110
111	struct gpiod_lookup_table *lookup __free(kfree) =
112			kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
113	if (!lookup)
114		return ERR_PTR(-ENOMEM);
115
116	lookup->dev_id = dev_name(int3472->dev);
117	ret = skl_int3472_fill_gpiod_lookup(&lookup->table[0], agpio, func, polarity);
118	if (ret)
119		return ERR_PTR(ret);
120
121	gpiod_add_lookup_table(lookup);
122	desc = devm_gpiod_get(int3472->dev, func, GPIOD_OUT_LOW);
123	gpiod_remove_lookup_table(lookup);
124
125	return desc;
126}
127
128static void int3472_get_func_and_polarity(u8 type, const char **func, u32 *polarity)
129{
130	switch (type) {
131	case INT3472_GPIO_TYPE_RESET:
132		*func = "reset";
133		*polarity = GPIO_ACTIVE_LOW;
134		break;
135	case INT3472_GPIO_TYPE_POWERDOWN:
136		*func = "powerdown";
137		*polarity = GPIO_ACTIVE_LOW;
138		break;
139	case INT3472_GPIO_TYPE_CLK_ENABLE:
140		*func = "clk-enable";
141		*polarity = GPIO_ACTIVE_HIGH;
 
 
 
 
 
142		break;
143	case INT3472_GPIO_TYPE_PRIVACY_LED:
144		*func = "privacy-led";
145		*polarity = GPIO_ACTIVE_HIGH;
146		break;
147	case INT3472_GPIO_TYPE_POWER_ENABLE:
148		*func = "power-enable";
149		*polarity = GPIO_ACTIVE_HIGH;
 
150		break;
151	default:
152		*func = "unknown";
153		*polarity = GPIO_ACTIVE_HIGH;
154		break;
155	}
 
 
156}
157
158/**
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
162 *
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:
167 *
168 * 0x00 Reset
169 * 0x01 Power down
170 * 0x0b Power enable
171 * 0x0c Clock enable
172 * 0x0d Privacy LED
173 *
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.
179 *
180 * GPIOs will either be mapped directly to the sensor device or else used
181 * to create clocks and regulators via the usual frameworks.
182 *
183 * Return:
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.
189 */
190static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares,
191					     void *data)
192{
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;
198	const char *err_msg;
199	const char *func;
200	u32 polarity;
201	int ret;
 
202
203	if (!acpi_gpio_get_io_resource(ares, &agpio))
204		return 1;
205
206	/*
207	 * ngpios + 2 because the index of this _DSM function is 1-based and
208	 * the first function is just a count.
209	 */
210	obj = acpi_evaluate_dsm_typed(int3472->adev->handle,
211				      &int3472_gpio_guid, 0x00,
212				      int3472->ngpios + 2,
213				      NULL, ACPI_TYPE_INTEGER);
214
215	if (!obj) {
216		dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n",
217			 agpio->pin_table[0]);
218		return 1;
219	}
220
221	type = FIELD_GET(INT3472_GPIO_DSM_TYPE, obj->integer.value);
222
223	int3472_get_func_and_polarity(type, &func, &polarity);
224
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]);
230
231	active_value = FIELD_GET(INT3472_GPIO_DSM_SENSOR_ON_VAL, obj->integer.value);
232	if (!active_value)
233		polarity ^= GPIO_ACTIVE_LOW;
234
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");
238
239	switch (type) {
240	case INT3472_GPIO_TYPE_RESET:
 
 
 
 
 
 
241	case INT3472_GPIO_TYPE_POWERDOWN:
242		ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, func, polarity);
 
243		if (ret)
244			err_msg = "Failed to map GPIO pin to sensor\n";
245
246		break;
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);
251		if (IS_ERR(gpio)) {
252			ret = PTR_ERR(gpio);
253			err_msg = "Failed to get GPIO\n";
254			break;
255		}
256
257		switch (type) {
258		case INT3472_GPIO_TYPE_CLK_ENABLE:
259			ret = skl_int3472_register_gpio_clock(int3472, gpio);
260			if (ret)
261				err_msg = "Failed to register clock\n";
262
263			break;
264		case INT3472_GPIO_TYPE_PRIVACY_LED:
265			ret = skl_int3472_register_pled(int3472, gpio);
266			if (ret)
267				err_msg = "Failed to register LED\n";
268
269			break;
270		case INT3472_GPIO_TYPE_POWER_ENABLE:
271			ret = skl_int3472_register_regulator(int3472, gpio);
272			if (ret)
273				err_msg = "Failed to map regulator to sensor\n";
274
275			break;
276		default: /* Never reached */
277			ret = -EINVAL;
278			break;
279		}
280		break;
281	default:
282		dev_warn(int3472->dev,
283			 "GPIO type 0x%02x unknown; the sensor may not work\n",
284			 type);
285		ret = 1;
286		break;
287	}
288
289	int3472->ngpios++;
290	ACPI_FREE(obj);
291
292	if (ret < 0)
293		return dev_err_probe(int3472->dev, ret, err_msg);
294
295	return ret;
296}
297
298static int skl_int3472_parse_crs(struct int3472_discrete_device *int3472)
299{
300	LIST_HEAD(resource_list);
301	int ret;
302
303	skl_int3472_log_sensor_module_name(int3472);
 
 
 
 
304
305	ret = acpi_dev_get_resources(int3472->adev, &resource_list,
306				     skl_int3472_handle_gpio_resources,
307				     int3472);
308	if (ret < 0)
309		return ret;
310
311	acpi_dev_free_resource_list(&resource_list);
312
313	/* Register _DSM based clock (no-op if a GPIO clock was already registered) */
314	ret = skl_int3472_register_dsm_clock(int3472);
315	if (ret < 0)
316		return ret;
 
 
 
 
 
 
 
 
 
 
317
318	int3472->gpios.dev_id = int3472->sensor_name;
319	gpiod_add_lookup_table(&int3472->gpios);
320
321	return 0;
322}
323
324static void skl_int3472_discrete_remove(struct platform_device *pdev)
325{
326	struct int3472_discrete_device *int3472 = platform_get_drvdata(pdev);
327
328	gpiod_remove_lookup_table(&int3472->gpios);
329
330	skl_int3472_unregister_clock(int3472);
331	skl_int3472_unregister_pled(int3472);
 
 
 
 
332	skl_int3472_unregister_regulator(int3472);
 
 
333}
334
335static int skl_int3472_discrete_probe(struct platform_device *pdev)
336{
337	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
338	struct int3472_discrete_device *int3472;
339	struct int3472_cldb cldb;
340	int ret;
341
342	ret = skl_int3472_fill_cldb(adev, &cldb);
343	if (ret) {
344		dev_err(&pdev->dev, "Couldn't fill CLDB structure\n");
345		return ret;
346	}
347
348	if (cldb.control_logic_type != 1) {
349		dev_err(&pdev->dev, "Unsupported control logic type %u\n",
350			cldb.control_logic_type);
351		return -EINVAL;
352	}
353
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);
357	if (!int3472)
358		return -ENOMEM;
359
360	int3472->adev = adev;
361	int3472->dev = &pdev->dev;
362	platform_set_drvdata(pdev, int3472);
363	int3472->clock.imgclk_index = cldb.clock_source;
364
365	ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor,
366						   &int3472->sensor_name);
367	if (ret)
368		return ret;
369
370	/*
371	 * Initialising this list means we can call gpiod_remove_lookup_table()
372	 * in failure paths without issue.
373	 */
374	INIT_LIST_HEAD(&int3472->gpios.list);
375
376	ret = skl_int3472_parse_crs(int3472);
377	if (ret) {
378		skl_int3472_discrete_remove(pdev);
379		return ret;
380	}
381
382	acpi_dev_clear_dependencies(adev);
383	return 0;
384}
385
386static const struct acpi_device_id int3472_device_id[] = {
387	{ "INT3472", 0 },
388	{ }
389};
390MODULE_DEVICE_TABLE(acpi, int3472_device_id);
391
392static struct platform_driver int3472_discrete = {
393	.driver = {
394		.name = "int3472-discrete",
395		.acpi_match_table = int3472_device_id,
396	},
397	.probe = skl_int3472_discrete_probe,
398	.remove_new = skl_int3472_discrete_remove,
399};
400module_platform_driver(int3472_discrete);
401
402MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver");
403MODULE_AUTHOR("Daniel Scally <djrscally@gmail.com>");
404MODULE_LICENSE("GPL v2");