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