xref: /linux/drivers/platform/x86/intel/int3472/discrete.c (revision b5d46539626833bf3bdd5a2295e85ec1c2a76a78)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
3 
4 #include <linux/acpi.h>
5 #include <linux/array_size.h>
6 #include <linux/bitfield.h>
7 #include <linux/device.h>
8 #include <linux/dmi.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/gpio/machine.h>
11 #include <linux/i2c.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/overflow.h>
15 #include <linux/platform_device.h>
16 #include <linux/string_choices.h>
17 #include <linux/uuid.h>
18 
19 #include "common.h"
20 
21 /*
22  * 79234640-9e10-4fea-a5c1-b5aa8b19756f
23  * This _DSM GUID returns information about the GPIO lines mapped to a
24  * discrete INT3472 device. Function number 1 returns a count of the GPIO
25  * lines that are mapped. Subsequent functions return 32 bit ints encoding
26  * information about the GPIO line, including its purpose.
27  */
28 static const guid_t int3472_gpio_guid =
29 	GUID_INIT(0x79234640, 0x9e10, 0x4fea,
30 		  0xa5, 0xc1, 0xb5, 0xaa, 0x8b, 0x19, 0x75, 0x6f);
31 
32 #define INT3472_GPIO_DSM_TYPE				GENMASK(7, 0)
33 #define INT3472_GPIO_DSM_PIN				GENMASK(15, 8)
34 #define INT3472_GPIO_DSM_SENSOR_ON_VAL			GENMASK(31, 24)
35 
36 /*
37  * 822ace8f-2814-4174-a56b-5f029fe079ee
38  * This _DSM GUID returns a string from the sensor device, which acts as a
39  * module identifier.
40  */
41 static const guid_t cio2_sensor_module_guid =
42 	GUID_INIT(0x822ace8f, 0x2814, 0x4174,
43 		  0xa5, 0x6b, 0x5f, 0x02, 0x9f, 0xe0, 0x79, 0xee);
44 
45 static void skl_int3472_log_sensor_module_name(struct int3472_discrete_device *int3472)
46 {
47 	union acpi_object *obj;
48 
49 	obj = acpi_evaluate_dsm_typed(int3472->sensor->handle,
50 				      &cio2_sensor_module_guid, 0x00,
51 				      0x01, NULL, ACPI_TYPE_STRING);
52 	if (obj) {
53 		dev_dbg(int3472->dev, "Sensor module id: '%s'\n", obj->string.pointer);
54 		ACPI_FREE(obj);
55 	}
56 }
57 
58 static int skl_int3472_fill_gpiod_lookup(struct gpiod_lookup *table_entry,
59 					 struct acpi_resource_gpio *agpio,
60 					 const char *con_id, unsigned long gpio_flags)
61 {
62 	char *path = agpio->resource_source.string_ptr;
63 	struct acpi_device *adev;
64 	acpi_handle handle;
65 	acpi_status status;
66 
67 	status = acpi_get_handle(NULL, path, &handle);
68 	if (ACPI_FAILURE(status))
69 		return -EINVAL;
70 
71 	adev = acpi_fetch_acpi_dev(handle);
72 	if (!adev)
73 		return -ENODEV;
74 
75 	*table_entry = GPIO_LOOKUP(acpi_dev_name(adev), agpio->pin_table[0], con_id, gpio_flags);
76 
77 	return 0;
78 }
79 
80 static int skl_int3472_map_gpio_to_sensor(struct int3472_discrete_device *int3472,
81 					  struct acpi_resource_gpio *agpio,
82 					  const char *con_id, unsigned long gpio_flags)
83 {
84 	int ret;
85 
86 	if (int3472->n_sensor_gpios >= INT3472_MAX_SENSOR_GPIOS) {
87 		dev_warn(int3472->dev, "Too many GPIOs mapped\n");
88 		return -EINVAL;
89 	}
90 
91 	ret = skl_int3472_fill_gpiod_lookup(&int3472->gpios.table[int3472->n_sensor_gpios],
92 					    agpio, con_id, gpio_flags);
93 	if (ret)
94 		return ret;
95 
96 	int3472->n_sensor_gpios++;
97 
98 	return 0;
99 }
100 
101 /* This should *really* only be used when there's no other way... */
102 static struct gpio_desc *
103 skl_int3472_gpiod_get_from_temp_lookup(struct int3472_discrete_device *int3472,
104 				       struct acpi_resource_gpio *agpio,
105 				       const char *con_id, unsigned long gpio_flags)
106 {
107 	struct gpio_desc *desc;
108 	int ret;
109 
110 	struct gpiod_lookup_table *lookup __free(kfree) =
111 			kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
112 	if (!lookup)
113 		return ERR_PTR(-ENOMEM);
114 
115 	lookup->dev_id = dev_name(int3472->dev);
116 	ret = skl_int3472_fill_gpiod_lookup(&lookup->table[0], agpio, con_id, gpio_flags);
117 	if (ret)
118 		return ERR_PTR(ret);
119 
120 	gpiod_add_lookup_table(lookup);
121 	desc = devm_gpiod_get(int3472->dev, con_id, GPIOD_OUT_LOW);
122 	gpiod_remove_lookup_table(lookup);
123 
124 	return desc;
125 }
126 
127 /**
128  * struct int3472_gpio_map - Map GPIOs to whatever is expected by the
129  * sensor driver (as in DT bindings)
130  * @hid: The ACPI HID of the device without the instance number e.g. INT347E
131  * @type_from: The GPIO type from ACPI ?SDT
132  * @type_to: The assigned GPIO type, typically same as @type_from
133  * @con_id: The name of the GPIO for the device
134  * @polarity_low: GPIO_ACTIVE_LOW true if the @polarity_low is true,
135  * GPIO_ACTIVE_HIGH otherwise
136  */
137 struct int3472_gpio_map {
138 	const char *hid;
139 	u8 type_from;
140 	u8 type_to;
141 	bool polarity_low;
142 	const char *con_id;
143 };
144 
145 static const struct int3472_gpio_map int3472_gpio_map[] = {
146 	{ "INT347E", INT3472_GPIO_TYPE_RESET, INT3472_GPIO_TYPE_RESET, false, "enable" },
147 };
148 
149 static void int3472_get_con_id_and_polarity(struct int3472_discrete_device *int3472, u8 *type,
150 					    const char **con_id, unsigned long *gpio_flags)
151 {
152 	struct acpi_device *adev = int3472->sensor;
153 	unsigned int i;
154 
155 	for (i = 0; i < ARRAY_SIZE(int3472_gpio_map); i++) {
156 		/*
157 		 * Map the firmware-provided GPIO to whatever a driver expects
158 		 * (as in DT bindings). First check if the type matches with the
159 		 * GPIO map, then further check that the device _HID matches.
160 		 */
161 		if (*type != int3472_gpio_map[i].type_from)
162 			continue;
163 
164 		if (!acpi_dev_hid_uid_match(adev, int3472_gpio_map[i].hid, NULL))
165 			continue;
166 
167 		dev_dbg(int3472->dev, "mapping type 0x%02x pin to 0x%02x %s\n",
168 			*type, int3472_gpio_map[i].type_to, int3472_gpio_map[i].con_id);
169 
170 		*type = int3472_gpio_map[i].type_to;
171 		*gpio_flags = int3472_gpio_map[i].polarity_low ?
172 			      GPIO_ACTIVE_LOW : GPIO_ACTIVE_HIGH;
173 		*con_id = int3472_gpio_map[i].con_id;
174 		return;
175 	}
176 
177 	switch (*type) {
178 	case INT3472_GPIO_TYPE_RESET:
179 		*con_id = "reset";
180 		*gpio_flags = GPIO_ACTIVE_LOW;
181 		break;
182 	case INT3472_GPIO_TYPE_POWERDOWN:
183 		*con_id = "powerdown";
184 		*gpio_flags = GPIO_ACTIVE_LOW;
185 		break;
186 	case INT3472_GPIO_TYPE_CLK_ENABLE:
187 		*con_id = "clk-enable";
188 		*gpio_flags = GPIO_ACTIVE_HIGH;
189 		break;
190 	case INT3472_GPIO_TYPE_PRIVACY_LED:
191 		*con_id = "privacy-led";
192 		*gpio_flags = GPIO_ACTIVE_HIGH;
193 		break;
194 	case INT3472_GPIO_TYPE_POWER_ENABLE:
195 		*con_id = "avdd";
196 		*gpio_flags = GPIO_ACTIVE_HIGH;
197 		break;
198 	case INT3472_GPIO_TYPE_HANDSHAKE:
199 		*con_id = "dvdd";
200 		*gpio_flags = GPIO_ACTIVE_HIGH;
201 		break;
202 	default:
203 		*con_id = "unknown";
204 		*gpio_flags = GPIO_ACTIVE_HIGH;
205 		break;
206 	}
207 }
208 
209 /**
210  * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor
211  * @ares: A pointer to a &struct acpi_resource
212  * @data: A pointer to a &struct int3472_discrete_device
213  *
214  * This function handles GPIO resources that are against an INT3472
215  * ACPI device, by checking the value of the corresponding _DSM entry.
216  * This will return a 32bit int, where the lowest byte represents the
217  * function of the GPIO pin:
218  *
219  * 0x00 Reset
220  * 0x01 Power down
221  * 0x0b Power enable
222  * 0x0c Clock enable
223  * 0x0d Privacy LED
224  *
225  * There are some known platform specific quirks where that does not quite
226  * hold up; for example where a pin with type 0x01 (Power down) is mapped to
227  * a sensor pin that performs a reset function or entries in _CRS and _DSM that
228  * do not actually correspond to a physical connection. These will be handled
229  * by the mapping sub-functions.
230  *
231  * GPIOs will either be mapped directly to the sensor device or else used
232  * to create clocks and regulators via the usual frameworks.
233  *
234  * Return:
235  * * 1		- Continue the loop without adding a copy of the resource to
236  * *		  the list passed to acpi_dev_get_resources()
237  * * 0		- Continue the loop after adding a copy of the resource to
238  * *		  the list passed to acpi_dev_get_resources()
239  * * -errno	- Error, break loop
240  */
241 static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares,
242 					     void *data)
243 {
244 	struct int3472_discrete_device *int3472 = data;
245 	struct acpi_resource_gpio *agpio;
246 	u8 active_value, pin, type;
247 	union acpi_object *obj;
248 	struct gpio_desc *gpio;
249 	const char *err_msg;
250 	const char *con_id;
251 	unsigned long gpio_flags;
252 	int ret;
253 
254 	if (!acpi_gpio_get_io_resource(ares, &agpio))
255 		return 1;
256 
257 	/*
258 	 * ngpios + 2 because the index of this _DSM function is 1-based and
259 	 * the first function is just a count.
260 	 */
261 	obj = acpi_evaluate_dsm_typed(int3472->adev->handle,
262 				      &int3472_gpio_guid, 0x00,
263 				      int3472->ngpios + 2,
264 				      NULL, ACPI_TYPE_INTEGER);
265 
266 	if (!obj) {
267 		dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n",
268 			 agpio->pin_table[0]);
269 		return 1;
270 	}
271 
272 	type = FIELD_GET(INT3472_GPIO_DSM_TYPE, obj->integer.value);
273 
274 	int3472_get_con_id_and_polarity(int3472, &type, &con_id, &gpio_flags);
275 
276 	pin = FIELD_GET(INT3472_GPIO_DSM_PIN, obj->integer.value);
277 	/* Pin field is not really used under Windows and wraps around at 8 bits */
278 	if (pin != (agpio->pin_table[0] & 0xff))
279 		dev_dbg(int3472->dev, FW_BUG "%s %s pin number mismatch _DSM %d resource %d\n",
280 			con_id, agpio->resource_source.string_ptr, pin, agpio->pin_table[0]);
281 
282 	active_value = FIELD_GET(INT3472_GPIO_DSM_SENSOR_ON_VAL, obj->integer.value);
283 	if (!active_value)
284 		gpio_flags ^= GPIO_ACTIVE_LOW;
285 
286 	dev_dbg(int3472->dev, "%s %s pin %d active-%s\n", con_id,
287 		agpio->resource_source.string_ptr, agpio->pin_table[0],
288 		str_high_low(gpio_flags == GPIO_ACTIVE_HIGH));
289 
290 	switch (type) {
291 	case INT3472_GPIO_TYPE_RESET:
292 	case INT3472_GPIO_TYPE_POWERDOWN:
293 		ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, con_id, gpio_flags);
294 		if (ret)
295 			err_msg = "Failed to map GPIO pin to sensor\n";
296 
297 		break;
298 	case INT3472_GPIO_TYPE_CLK_ENABLE:
299 	case INT3472_GPIO_TYPE_PRIVACY_LED:
300 	case INT3472_GPIO_TYPE_POWER_ENABLE:
301 	case INT3472_GPIO_TYPE_HANDSHAKE:
302 		gpio = skl_int3472_gpiod_get_from_temp_lookup(int3472, agpio, con_id, gpio_flags);
303 		if (IS_ERR(gpio)) {
304 			ret = PTR_ERR(gpio);
305 			err_msg = "Failed to get GPIO\n";
306 			break;
307 		}
308 
309 		switch (type) {
310 		case INT3472_GPIO_TYPE_CLK_ENABLE:
311 			ret = skl_int3472_register_gpio_clock(int3472, gpio);
312 			if (ret)
313 				err_msg = "Failed to register clock\n";
314 
315 			break;
316 		case INT3472_GPIO_TYPE_PRIVACY_LED:
317 			ret = skl_int3472_register_pled(int3472, gpio);
318 			if (ret)
319 				err_msg = "Failed to register LED\n";
320 
321 			break;
322 		case INT3472_GPIO_TYPE_POWER_ENABLE:
323 			ret = skl_int3472_register_regulator(int3472, gpio,
324 							     GPIO_REGULATOR_ENABLE_TIME,
325 							     con_id,
326 							     int3472->quirks.avdd_second_sensor);
327 			if (ret)
328 				err_msg = "Failed to map power-enable to sensor\n";
329 
330 			break;
331 		case INT3472_GPIO_TYPE_HANDSHAKE:
332 			/* Setups using a handshake pin need 25 ms enable delay */
333 			ret = skl_int3472_register_regulator(int3472, gpio,
334 							     25 * USEC_PER_MSEC,
335 							     con_id, NULL);
336 			if (ret)
337 				err_msg = "Failed to map handshake to sensor\n";
338 
339 			break;
340 		default: /* Never reached */
341 			ret = -EINVAL;
342 			break;
343 		}
344 		break;
345 	default:
346 		dev_warn(int3472->dev,
347 			 "GPIO type 0x%02x unknown; the sensor may not work\n",
348 			 type);
349 		ret = 1;
350 		break;
351 	}
352 
353 	int3472->ngpios++;
354 	ACPI_FREE(obj);
355 
356 	if (ret < 0)
357 		return dev_err_probe(int3472->dev, ret, err_msg);
358 
359 	/* Tell acpi_dev_get_resources() to not make a copy of the resource */
360 	return 1;
361 }
362 
363 static int skl_int3472_parse_crs(struct int3472_discrete_device *int3472)
364 {
365 	LIST_HEAD(resource_list);
366 	int ret;
367 
368 	skl_int3472_log_sensor_module_name(int3472);
369 
370 	ret = acpi_dev_get_resources(int3472->adev, &resource_list,
371 				     skl_int3472_handle_gpio_resources,
372 				     int3472);
373 	if (ret < 0)
374 		return ret;
375 
376 	acpi_dev_free_resource_list(&resource_list);
377 
378 	/* Register _DSM based clock (no-op if a GPIO clock was already registered) */
379 	ret = skl_int3472_register_dsm_clock(int3472);
380 	if (ret < 0)
381 		return ret;
382 
383 	int3472->gpios.dev_id = int3472->sensor_name;
384 	gpiod_add_lookup_table(&int3472->gpios);
385 
386 	return 0;
387 }
388 
389 static void skl_int3472_discrete_remove(struct platform_device *pdev)
390 {
391 	struct int3472_discrete_device *int3472 = platform_get_drvdata(pdev);
392 
393 	gpiod_remove_lookup_table(&int3472->gpios);
394 
395 	skl_int3472_unregister_clock(int3472);
396 	skl_int3472_unregister_pled(int3472);
397 	skl_int3472_unregister_regulator(int3472);
398 }
399 
400 static int skl_int3472_discrete_probe(struct platform_device *pdev)
401 {
402 	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
403 	const struct int3472_discrete_quirks *quirks = NULL;
404 	struct int3472_discrete_device *int3472;
405 	const struct dmi_system_id *id;
406 	struct int3472_cldb cldb;
407 	int ret;
408 
409 	if (!adev)
410 		return -ENODEV;
411 
412 	id = dmi_first_match(skl_int3472_discrete_quirks);
413 	if (id)
414 		quirks = id->driver_data;
415 
416 	ret = skl_int3472_fill_cldb(adev, &cldb);
417 	if (ret) {
418 		dev_err(&pdev->dev, "Couldn't fill CLDB structure\n");
419 		return ret;
420 	}
421 
422 	if (cldb.control_logic_type != 1) {
423 		dev_err(&pdev->dev, "Unsupported control logic type %u\n",
424 			cldb.control_logic_type);
425 		return -EINVAL;
426 	}
427 
428 	/* Max num GPIOs we've seen plus a terminator */
429 	int3472 = devm_kzalloc(&pdev->dev, struct_size(int3472, gpios.table,
430 			       INT3472_MAX_SENSOR_GPIOS + 1), GFP_KERNEL);
431 	if (!int3472)
432 		return -ENOMEM;
433 
434 	int3472->adev = adev;
435 	int3472->dev = &pdev->dev;
436 	platform_set_drvdata(pdev, int3472);
437 	int3472->clock.imgclk_index = cldb.clock_source;
438 
439 	if (quirks)
440 		int3472->quirks = *quirks;
441 
442 	ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor,
443 						   &int3472->sensor_name);
444 	if (ret)
445 		return ret;
446 
447 	/*
448 	 * Initialising this list means we can call gpiod_remove_lookup_table()
449 	 * in failure paths without issue.
450 	 */
451 	INIT_LIST_HEAD(&int3472->gpios.list);
452 
453 	ret = skl_int3472_parse_crs(int3472);
454 	if (ret) {
455 		skl_int3472_discrete_remove(pdev);
456 		return ret;
457 	}
458 
459 	acpi_dev_clear_dependencies(adev);
460 	return 0;
461 }
462 
463 static const struct acpi_device_id int3472_device_id[] = {
464 	{ "INT3472", 0 },
465 	{ }
466 };
467 MODULE_DEVICE_TABLE(acpi, int3472_device_id);
468 
469 static struct platform_driver int3472_discrete = {
470 	.driver = {
471 		.name = "int3472-discrete",
472 		.acpi_match_table = int3472_device_id,
473 	},
474 	.probe = skl_int3472_discrete_probe,
475 	.remove = skl_int3472_discrete_remove,
476 };
477 module_platform_driver(int3472_discrete);
478 
479 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver");
480 MODULE_AUTHOR("Daniel Scally <djrscally@gmail.com>");
481 MODULE_LICENSE("GPL v2");
482