xref: /linux/drivers/platform/x86/intel/int3472/discrete.c (revision d63609e4129596cbf86d91c1fb6903273ca26269)
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/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/string_choices.h>
16 #include <linux/uuid.h>
17 
18 #include "common.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 * func,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 *func, 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], func, 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 * func,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 *func, 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, func, 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 * func,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 *func, 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, func, gpio_flags);
116 	if (ret)
117 		return ERR_PTR(ret);
118 
119 	gpiod_add_lookup_table(lookup);
120 	desc = devm_gpiod_get(int3472->dev, func, 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  * @func: The function, e.g. "enable"
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 *func;
142 };
143 
144 static const struct int3472_gpio_map int3472_gpio_map[] = {
145 	{ "INT347E", INT3472_GPIO_TYPE_RESET, INT3472_GPIO_TYPE_RESET, false, "enable" },
146 };
147 
int3472_get_func_and_polarity(struct acpi_device * adev,u8 * type,const char ** func,unsigned long * gpio_flags)148 static void int3472_get_func_and_polarity(struct acpi_device *adev, u8 *type,
149 					  const char **func, unsigned long *gpio_flags)
150 {
151 	unsigned int i;
152 
153 	for (i = 0; i < ARRAY_SIZE(int3472_gpio_map); i++) {
154 		/*
155 		 * Map the firmware-provided GPIO to whatever a driver expects
156 		 * (as in DT bindings). First check if the type matches with the
157 		 * GPIO map, then further check that the device _HID matches.
158 		 */
159 		if (*type != int3472_gpio_map[i].type_from)
160 			continue;
161 
162 		if (!acpi_dev_hid_uid_match(adev, int3472_gpio_map[i].hid, NULL))
163 			continue;
164 
165 		*type = int3472_gpio_map[i].type_to;
166 		*gpio_flags = int3472_gpio_map[i].polarity_low ?
167 			      GPIO_ACTIVE_LOW : GPIO_ACTIVE_HIGH;
168 		*func = int3472_gpio_map[i].func;
169 		return;
170 	}
171 
172 	switch (*type) {
173 	case INT3472_GPIO_TYPE_RESET:
174 		*func = "reset";
175 		*gpio_flags = GPIO_ACTIVE_LOW;
176 		break;
177 	case INT3472_GPIO_TYPE_POWERDOWN:
178 		*func = "powerdown";
179 		*gpio_flags = GPIO_ACTIVE_LOW;
180 		break;
181 	case INT3472_GPIO_TYPE_CLK_ENABLE:
182 		*func = "clk-enable";
183 		*gpio_flags = GPIO_ACTIVE_HIGH;
184 		break;
185 	case INT3472_GPIO_TYPE_PRIVACY_LED:
186 		*func = "privacy-led";
187 		*gpio_flags = GPIO_ACTIVE_HIGH;
188 		break;
189 	case INT3472_GPIO_TYPE_POWER_ENABLE:
190 		*func = "power-enable";
191 		*gpio_flags = GPIO_ACTIVE_HIGH;
192 		break;
193 	default:
194 		*func = "unknown";
195 		*gpio_flags = GPIO_ACTIVE_HIGH;
196 		break;
197 	}
198 }
199 
200 /**
201  * skl_int3472_handle_gpio_resources: Map PMIC resources to consuming sensor
202  * @ares: A pointer to a &struct acpi_resource
203  * @data: A pointer to a &struct int3472_discrete_device
204  *
205  * This function handles GPIO resources that are against an INT3472
206  * ACPI device, by checking the value of the corresponding _DSM entry.
207  * This will return a 32bit int, where the lowest byte represents the
208  * function of the GPIO pin:
209  *
210  * 0x00 Reset
211  * 0x01 Power down
212  * 0x0b Power enable
213  * 0x0c Clock enable
214  * 0x0d Privacy LED
215  *
216  * There are some known platform specific quirks where that does not quite
217  * hold up; for example where a pin with type 0x01 (Power down) is mapped to
218  * a sensor pin that performs a reset function or entries in _CRS and _DSM that
219  * do not actually correspond to a physical connection. These will be handled
220  * by the mapping sub-functions.
221  *
222  * GPIOs will either be mapped directly to the sensor device or else used
223  * to create clocks and regulators via the usual frameworks.
224  *
225  * Return:
226  * * 1		- Continue the loop without adding a copy of the resource to
227  * *		  the list passed to acpi_dev_get_resources()
228  * * 0		- Continue the loop after adding a copy of the resource to
229  * *		  the list passed to acpi_dev_get_resources()
230  * * -errno	- Error, break loop
231  */
skl_int3472_handle_gpio_resources(struct acpi_resource * ares,void * data)232 static int skl_int3472_handle_gpio_resources(struct acpi_resource *ares,
233 					     void *data)
234 {
235 	struct int3472_discrete_device *int3472 = data;
236 	struct acpi_resource_gpio *agpio;
237 	u8 active_value, pin, type;
238 	union acpi_object *obj;
239 	struct gpio_desc *gpio;
240 	const char *err_msg;
241 	const char *func;
242 	unsigned long gpio_flags;
243 	int ret;
244 
245 	if (!acpi_gpio_get_io_resource(ares, &agpio))
246 		return 1;
247 
248 	/*
249 	 * ngpios + 2 because the index of this _DSM function is 1-based and
250 	 * the first function is just a count.
251 	 */
252 	obj = acpi_evaluate_dsm_typed(int3472->adev->handle,
253 				      &int3472_gpio_guid, 0x00,
254 				      int3472->ngpios + 2,
255 				      NULL, ACPI_TYPE_INTEGER);
256 
257 	if (!obj) {
258 		dev_warn(int3472->dev, "No _DSM entry for GPIO pin %u\n",
259 			 agpio->pin_table[0]);
260 		return 1;
261 	}
262 
263 	type = FIELD_GET(INT3472_GPIO_DSM_TYPE, obj->integer.value);
264 
265 	int3472_get_func_and_polarity(int3472->sensor, &type, &func, &gpio_flags);
266 
267 	pin = FIELD_GET(INT3472_GPIO_DSM_PIN, obj->integer.value);
268 	/* Pin field is not really used under Windows and wraps around at 8 bits */
269 	if (pin != (agpio->pin_table[0] & 0xff))
270 		dev_dbg(int3472->dev, FW_BUG "%s %s pin number mismatch _DSM %d resource %d\n",
271 			func, agpio->resource_source.string_ptr, pin, agpio->pin_table[0]);
272 
273 	active_value = FIELD_GET(INT3472_GPIO_DSM_SENSOR_ON_VAL, obj->integer.value);
274 	if (!active_value)
275 		gpio_flags ^= GPIO_ACTIVE_LOW;
276 
277 	dev_dbg(int3472->dev, "%s %s pin %d active-%s\n", func,
278 		agpio->resource_source.string_ptr, agpio->pin_table[0],
279 		str_high_low(gpio_flags == GPIO_ACTIVE_HIGH));
280 
281 	switch (type) {
282 	case INT3472_GPIO_TYPE_RESET:
283 	case INT3472_GPIO_TYPE_POWERDOWN:
284 		ret = skl_int3472_map_gpio_to_sensor(int3472, agpio, func, gpio_flags);
285 		if (ret)
286 			err_msg = "Failed to map GPIO pin to sensor\n";
287 
288 		break;
289 	case INT3472_GPIO_TYPE_CLK_ENABLE:
290 	case INT3472_GPIO_TYPE_PRIVACY_LED:
291 	case INT3472_GPIO_TYPE_POWER_ENABLE:
292 		gpio = skl_int3472_gpiod_get_from_temp_lookup(int3472, agpio, func, gpio_flags);
293 		if (IS_ERR(gpio)) {
294 			ret = PTR_ERR(gpio);
295 			err_msg = "Failed to get GPIO\n";
296 			break;
297 		}
298 
299 		switch (type) {
300 		case INT3472_GPIO_TYPE_CLK_ENABLE:
301 			ret = skl_int3472_register_gpio_clock(int3472, gpio);
302 			if (ret)
303 				err_msg = "Failed to register clock\n";
304 
305 			break;
306 		case INT3472_GPIO_TYPE_PRIVACY_LED:
307 			ret = skl_int3472_register_pled(int3472, gpio);
308 			if (ret)
309 				err_msg = "Failed to register LED\n";
310 
311 			break;
312 		case INT3472_GPIO_TYPE_POWER_ENABLE:
313 			ret = skl_int3472_register_regulator(int3472, gpio);
314 			if (ret)
315 				err_msg = "Failed to map regulator to sensor\n";
316 
317 			break;
318 		default: /* Never reached */
319 			ret = -EINVAL;
320 			break;
321 		}
322 		break;
323 	default:
324 		dev_warn(int3472->dev,
325 			 "GPIO type 0x%02x unknown; the sensor may not work\n",
326 			 type);
327 		ret = 1;
328 		break;
329 	}
330 
331 	int3472->ngpios++;
332 	ACPI_FREE(obj);
333 
334 	if (ret < 0)
335 		return dev_err_probe(int3472->dev, ret, err_msg);
336 
337 	/* Tell acpi_dev_get_resources() to not make a copy of the resource */
338 	return 1;
339 }
340 
skl_int3472_parse_crs(struct int3472_discrete_device * int3472)341 static int skl_int3472_parse_crs(struct int3472_discrete_device *int3472)
342 {
343 	LIST_HEAD(resource_list);
344 	int ret;
345 
346 	skl_int3472_log_sensor_module_name(int3472);
347 
348 	ret = acpi_dev_get_resources(int3472->adev, &resource_list,
349 				     skl_int3472_handle_gpio_resources,
350 				     int3472);
351 	if (ret < 0)
352 		return ret;
353 
354 	acpi_dev_free_resource_list(&resource_list);
355 
356 	/* Register _DSM based clock (no-op if a GPIO clock was already registered) */
357 	ret = skl_int3472_register_dsm_clock(int3472);
358 	if (ret < 0)
359 		return ret;
360 
361 	int3472->gpios.dev_id = int3472->sensor_name;
362 	gpiod_add_lookup_table(&int3472->gpios);
363 
364 	return 0;
365 }
366 
skl_int3472_discrete_remove(struct platform_device * pdev)367 static void skl_int3472_discrete_remove(struct platform_device *pdev)
368 {
369 	struct int3472_discrete_device *int3472 = platform_get_drvdata(pdev);
370 
371 	gpiod_remove_lookup_table(&int3472->gpios);
372 
373 	skl_int3472_unregister_clock(int3472);
374 	skl_int3472_unregister_pled(int3472);
375 	skl_int3472_unregister_regulator(int3472);
376 }
377 
skl_int3472_discrete_probe(struct platform_device * pdev)378 static int skl_int3472_discrete_probe(struct platform_device *pdev)
379 {
380 	struct acpi_device *adev = ACPI_COMPANION(&pdev->dev);
381 	struct int3472_discrete_device *int3472;
382 	struct int3472_cldb cldb;
383 	int ret;
384 
385 	if (!adev)
386 		return -ENODEV;
387 
388 	ret = skl_int3472_fill_cldb(adev, &cldb);
389 	if (ret) {
390 		dev_err(&pdev->dev, "Couldn't fill CLDB structure\n");
391 		return ret;
392 	}
393 
394 	if (cldb.control_logic_type != 1) {
395 		dev_err(&pdev->dev, "Unsupported control logic type %u\n",
396 			cldb.control_logic_type);
397 		return -EINVAL;
398 	}
399 
400 	/* Max num GPIOs we've seen plus a terminator */
401 	int3472 = devm_kzalloc(&pdev->dev, struct_size(int3472, gpios.table,
402 			       INT3472_MAX_SENSOR_GPIOS + 1), GFP_KERNEL);
403 	if (!int3472)
404 		return -ENOMEM;
405 
406 	int3472->adev = adev;
407 	int3472->dev = &pdev->dev;
408 	platform_set_drvdata(pdev, int3472);
409 	int3472->clock.imgclk_index = cldb.clock_source;
410 
411 	ret = skl_int3472_get_sensor_adev_and_name(&pdev->dev, &int3472->sensor,
412 						   &int3472->sensor_name);
413 	if (ret)
414 		return ret;
415 
416 	/*
417 	 * Initialising this list means we can call gpiod_remove_lookup_table()
418 	 * in failure paths without issue.
419 	 */
420 	INIT_LIST_HEAD(&int3472->gpios.list);
421 
422 	ret = skl_int3472_parse_crs(int3472);
423 	if (ret) {
424 		skl_int3472_discrete_remove(pdev);
425 		return ret;
426 	}
427 
428 	acpi_dev_clear_dependencies(adev);
429 	return 0;
430 }
431 
432 static const struct acpi_device_id int3472_device_id[] = {
433 	{ "INT3472", 0 },
434 	{ }
435 };
436 MODULE_DEVICE_TABLE(acpi, int3472_device_id);
437 
438 static struct platform_driver int3472_discrete = {
439 	.driver = {
440 		.name = "int3472-discrete",
441 		.acpi_match_table = int3472_device_id,
442 	},
443 	.probe = skl_int3472_discrete_probe,
444 	.remove = skl_int3472_discrete_remove,
445 };
446 module_platform_driver(int3472_discrete);
447 
448 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Discrete Device Driver");
449 MODULE_AUTHOR("Daniel Scally <djrscally@gmail.com>");
450 MODULE_LICENSE("GPL v2");
451