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