xref: /openbmc/linux/net/rfkill/rfkill-gpio.c (revision e5242c5f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2011, NVIDIA Corporation.
4  */
5 
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/rfkill.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/clk.h>
14 #include <linux/slab.h>
15 #include <linux/acpi.h>
16 #include <linux/gpio/consumer.h>
17 
18 struct rfkill_gpio_data {
19 	const char		*name;
20 	enum rfkill_type	type;
21 	struct gpio_desc	*reset_gpio;
22 	struct gpio_desc	*shutdown_gpio;
23 
24 	struct rfkill		*rfkill_dev;
25 	struct clk		*clk;
26 
27 	bool			clk_enabled;
28 };
29 
30 static int rfkill_gpio_set_power(void *data, bool blocked)
31 {
32 	struct rfkill_gpio_data *rfkill = data;
33 
34 	if (!blocked && !IS_ERR(rfkill->clk) && !rfkill->clk_enabled) {
35 		int ret = clk_enable(rfkill->clk);
36 
37 		if (ret)
38 			return ret;
39 	}
40 
41 	gpiod_set_value_cansleep(rfkill->shutdown_gpio, !blocked);
42 	gpiod_set_value_cansleep(rfkill->reset_gpio, !blocked);
43 
44 	if (blocked && !IS_ERR(rfkill->clk) && rfkill->clk_enabled)
45 		clk_disable(rfkill->clk);
46 
47 	rfkill->clk_enabled = !blocked;
48 
49 	return 0;
50 }
51 
52 static const struct rfkill_ops rfkill_gpio_ops = {
53 	.set_block = rfkill_gpio_set_power,
54 };
55 
56 static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
57 static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
58 
59 static const struct acpi_gpio_mapping acpi_rfkill_default_gpios[] = {
60 	{ "reset-gpios", &reset_gpios, 1 },
61 	{ "shutdown-gpios", &shutdown_gpios, 1 },
62 	{ },
63 };
64 
65 static int rfkill_gpio_acpi_probe(struct device *dev,
66 				  struct rfkill_gpio_data *rfkill)
67 {
68 	const struct acpi_device_id *id;
69 
70 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
71 	if (!id)
72 		return -ENODEV;
73 
74 	rfkill->type = (unsigned)id->driver_data;
75 
76 	return devm_acpi_dev_add_driver_gpios(dev, acpi_rfkill_default_gpios);
77 }
78 
79 static int rfkill_gpio_probe(struct platform_device *pdev)
80 {
81 	struct rfkill_gpio_data *rfkill;
82 	struct gpio_desc *gpio;
83 	const char *name_property;
84 	const char *type_property;
85 	const char *type_name;
86 	int ret;
87 
88 	rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
89 	if (!rfkill)
90 		return -ENOMEM;
91 
92 	if (dev_of_node(&pdev->dev)) {
93 		name_property = "label";
94 		type_property = "radio-type";
95 	} else {
96 		name_property = "name";
97 		type_property = "type";
98 	}
99 	device_property_read_string(&pdev->dev, name_property, &rfkill->name);
100 	device_property_read_string(&pdev->dev, type_property, &type_name);
101 
102 	if (!rfkill->name)
103 		rfkill->name = dev_name(&pdev->dev);
104 
105 	rfkill->type = rfkill_find_type(type_name);
106 
107 	if (ACPI_HANDLE(&pdev->dev)) {
108 		ret = rfkill_gpio_acpi_probe(&pdev->dev, rfkill);
109 		if (ret)
110 			return ret;
111 	}
112 
113 	rfkill->clk = devm_clk_get(&pdev->dev, NULL);
114 
115 	gpio = devm_gpiod_get_optional(&pdev->dev, "reset", GPIOD_ASIS);
116 	if (IS_ERR(gpio))
117 		return PTR_ERR(gpio);
118 
119 	rfkill->reset_gpio = gpio;
120 
121 	gpio = devm_gpiod_get_optional(&pdev->dev, "shutdown", GPIOD_ASIS);
122 	if (IS_ERR(gpio))
123 		return PTR_ERR(gpio);
124 
125 	rfkill->shutdown_gpio = gpio;
126 
127 	/* Make sure at-least one GPIO is defined for this instance */
128 	if (!rfkill->reset_gpio && !rfkill->shutdown_gpio) {
129 		dev_err(&pdev->dev, "invalid platform data\n");
130 		return -EINVAL;
131 	}
132 
133 	ret = gpiod_direction_output(rfkill->reset_gpio, true);
134 	if (ret)
135 		return ret;
136 
137 	ret = gpiod_direction_output(rfkill->shutdown_gpio, true);
138 	if (ret)
139 		return ret;
140 
141 	rfkill->rfkill_dev = rfkill_alloc(rfkill->name, &pdev->dev,
142 					  rfkill->type, &rfkill_gpio_ops,
143 					  rfkill);
144 	if (!rfkill->rfkill_dev)
145 		return -ENOMEM;
146 
147 	ret = rfkill_register(rfkill->rfkill_dev);
148 	if (ret < 0)
149 		goto err_destroy;
150 
151 	platform_set_drvdata(pdev, rfkill);
152 
153 	dev_info(&pdev->dev, "%s device registered.\n", rfkill->name);
154 
155 	return 0;
156 
157 err_destroy:
158 	rfkill_destroy(rfkill->rfkill_dev);
159 
160 	return ret;
161 }
162 
163 static int rfkill_gpio_remove(struct platform_device *pdev)
164 {
165 	struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
166 
167 	rfkill_unregister(rfkill->rfkill_dev);
168 	rfkill_destroy(rfkill->rfkill_dev);
169 
170 	return 0;
171 }
172 
173 #ifdef CONFIG_ACPI
174 static const struct acpi_device_id rfkill_acpi_match[] = {
175 	{ "BCM4752", RFKILL_TYPE_GPS },
176 	{ "LNV4752", RFKILL_TYPE_GPS },
177 	{ },
178 };
179 MODULE_DEVICE_TABLE(acpi, rfkill_acpi_match);
180 #endif
181 
182 static const struct of_device_id rfkill_of_match[] __maybe_unused = {
183 	{ .compatible = "rfkill-gpio", },
184 	{ },
185 };
186 MODULE_DEVICE_TABLE(of, rfkill_of_match);
187 
188 static struct platform_driver rfkill_gpio_driver = {
189 	.probe = rfkill_gpio_probe,
190 	.remove = rfkill_gpio_remove,
191 	.driver = {
192 		.name = "rfkill_gpio",
193 		.acpi_match_table = ACPI_PTR(rfkill_acpi_match),
194 		.of_match_table = of_match_ptr(rfkill_of_match),
195 	},
196 };
197 
198 module_platform_driver(rfkill_gpio_driver);
199 
200 MODULE_DESCRIPTION("gpio rfkill");
201 MODULE_AUTHOR("NVIDIA");
202 MODULE_LICENSE("GPL");
203