1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Hans de Goede <hdegoede@redhat.com> */
3 
4 #include <linux/acpi.h>
5 #include <linux/gpio/consumer.h>
6 #include <linux/leds.h>
7 #include "common.h"
8 
int3472_pled_set(struct led_classdev * led_cdev,enum led_brightness brightness)9 static int int3472_pled_set(struct led_classdev *led_cdev,
10 				     enum led_brightness brightness)
11 {
12 	struct int3472_discrete_device *int3472 =
13 		container_of(led_cdev, struct int3472_discrete_device, pled.classdev);
14 
15 	gpiod_set_value_cansleep(int3472->pled.gpio, brightness);
16 	return 0;
17 }
18 
skl_int3472_register_pled(struct int3472_discrete_device * int3472,struct acpi_resource_gpio * agpio,u32 polarity)19 int skl_int3472_register_pled(struct int3472_discrete_device *int3472,
20 			      struct acpi_resource_gpio *agpio, u32 polarity)
21 {
22 	char *p, *path = agpio->resource_source.string_ptr;
23 	int ret;
24 
25 	if (int3472->pled.classdev.dev)
26 		return -EBUSY;
27 
28 	int3472->pled.gpio = acpi_get_and_request_gpiod(path, agpio->pin_table[0],
29 							     "int3472,privacy-led");
30 	if (IS_ERR(int3472->pled.gpio))
31 		return dev_err_probe(int3472->dev, PTR_ERR(int3472->pled.gpio),
32 				     "getting privacy LED GPIO\n");
33 
34 	if (polarity == GPIO_ACTIVE_LOW)
35 		gpiod_toggle_active_low(int3472->pled.gpio);
36 
37 	/* Ensure the pin is in output mode and non-active state */
38 	gpiod_direction_output(int3472->pled.gpio, 0);
39 
40 	/* Generate the name, replacing the ':' in the ACPI devname with '_' */
41 	snprintf(int3472->pled.name, sizeof(int3472->pled.name),
42 		 "%s::privacy_led", acpi_dev_name(int3472->sensor));
43 	p = strchr(int3472->pled.name, ':');
44 	if (p)
45 		*p = '_';
46 
47 	int3472->pled.classdev.name = int3472->pled.name;
48 	int3472->pled.classdev.max_brightness = 1;
49 	int3472->pled.classdev.brightness_set_blocking = int3472_pled_set;
50 
51 	ret = led_classdev_register(int3472->dev, &int3472->pled.classdev);
52 	if (ret)
53 		goto err_free_gpio;
54 
55 	int3472->pled.lookup.provider = int3472->pled.name;
56 	int3472->pled.lookup.dev_id = int3472->sensor_name;
57 	int3472->pled.lookup.con_id = "privacy-led";
58 	led_add_lookup(&int3472->pled.lookup);
59 
60 	return 0;
61 
62 err_free_gpio:
63 	gpiod_put(int3472->pled.gpio);
64 	return ret;
65 }
66 
skl_int3472_unregister_pled(struct int3472_discrete_device * int3472)67 void skl_int3472_unregister_pled(struct int3472_discrete_device *int3472)
68 {
69 	if (IS_ERR_OR_NULL(int3472->pled.classdev.dev))
70 		return;
71 
72 	led_remove_lookup(&int3472->pled.lookup);
73 	led_classdev_unregister(&int3472->pled.classdev);
74 	gpiod_put(int3472->pled.gpio);
75 }
76