1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2011, NVIDIA Corporation. 4 */ 5 6 #include <linux/dmi.h> 7 #include <linux/init.h> 8 #include <linux/kernel.h> 9 #include <linux/module.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 /* List of DMI matches for devices on which rfkill-gpio should not load, 80 * to avoid firmware bugs. 81 */ 82 static const struct dmi_system_id rfkill_gpio_deny_table[] = { 83 { 84 /* Lenovo Yoga Tab 3 Pro YT3-X90, bogus "BCM4752" device in DSDT */ 85 .matches = { 86 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), 87 DMI_MATCH(DMI_PRODUCT_VERSION, "Blade3-10A-001"), 88 }, 89 }, 90 { } 91 }; 92 93 static int rfkill_gpio_probe(struct platform_device *pdev) 94 { 95 struct rfkill_gpio_data *rfkill; 96 const char *type_name = NULL; 97 const char *name_property; 98 const char *type_property; 99 struct gpio_desc *gpio; 100 int ret; 101 102 if (dmi_check_system(rfkill_gpio_deny_table)) 103 return -ENODEV; 104 105 rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL); 106 if (!rfkill) 107 return -ENOMEM; 108 109 if (dev_of_node(&pdev->dev)) { 110 name_property = "label"; 111 type_property = "radio-type"; 112 } else { 113 name_property = "name"; 114 type_property = "type"; 115 } 116 device_property_read_string(&pdev->dev, name_property, &rfkill->name); 117 device_property_read_string(&pdev->dev, type_property, &type_name); 118 119 if (!rfkill->name) 120 rfkill->name = dev_name(&pdev->dev); 121 122 rfkill->type = rfkill_find_type(type_name); 123 124 if (ACPI_HANDLE(&pdev->dev)) { 125 ret = rfkill_gpio_acpi_probe(&pdev->dev, rfkill); 126 if (ret) 127 return ret; 128 } 129 130 rfkill->clk = devm_clk_get(&pdev->dev, NULL); 131 132 gpio = devm_gpiod_get_optional(&pdev->dev, "reset", GPIOD_ASIS); 133 if (IS_ERR(gpio)) 134 return PTR_ERR(gpio); 135 136 rfkill->reset_gpio = gpio; 137 138 gpio = devm_gpiod_get_optional(&pdev->dev, "shutdown", GPIOD_ASIS); 139 if (IS_ERR(gpio)) 140 return PTR_ERR(gpio); 141 142 rfkill->shutdown_gpio = gpio; 143 144 /* Make sure at-least one GPIO is defined for this instance */ 145 if (!rfkill->reset_gpio && !rfkill->shutdown_gpio) { 146 dev_err(&pdev->dev, "invalid platform data\n"); 147 return -EINVAL; 148 } 149 150 ret = gpiod_direction_output(rfkill->reset_gpio, true); 151 if (ret) 152 return ret; 153 154 ret = gpiod_direction_output(rfkill->shutdown_gpio, true); 155 if (ret) 156 return ret; 157 158 rfkill->rfkill_dev = rfkill_alloc(rfkill->name, &pdev->dev, 159 rfkill->type, &rfkill_gpio_ops, 160 rfkill); 161 if (!rfkill->rfkill_dev) 162 return -ENOMEM; 163 164 if (device_property_present(&pdev->dev, "default-blocked")) 165 rfkill_init_sw_state(rfkill->rfkill_dev, true); 166 167 ret = rfkill_register(rfkill->rfkill_dev); 168 if (ret < 0) 169 goto err_destroy; 170 171 platform_set_drvdata(pdev, rfkill); 172 173 dev_info(&pdev->dev, "%s device registered.\n", rfkill->name); 174 175 return 0; 176 177 err_destroy: 178 rfkill_destroy(rfkill->rfkill_dev); 179 180 return ret; 181 } 182 183 static void rfkill_gpio_remove(struct platform_device *pdev) 184 { 185 struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev); 186 187 rfkill_unregister(rfkill->rfkill_dev); 188 rfkill_destroy(rfkill->rfkill_dev); 189 } 190 191 #ifdef CONFIG_ACPI 192 static const struct acpi_device_id rfkill_acpi_match[] = { 193 { "BCM4752", RFKILL_TYPE_GPS }, 194 { "LNV4752", RFKILL_TYPE_GPS }, 195 { }, 196 }; 197 MODULE_DEVICE_TABLE(acpi, rfkill_acpi_match); 198 #endif 199 200 static const struct of_device_id rfkill_of_match[] __maybe_unused = { 201 { .compatible = "rfkill-gpio", }, 202 { }, 203 }; 204 MODULE_DEVICE_TABLE(of, rfkill_of_match); 205 206 static struct platform_driver rfkill_gpio_driver = { 207 .probe = rfkill_gpio_probe, 208 .remove = rfkill_gpio_remove, 209 .driver = { 210 .name = "rfkill_gpio", 211 .acpi_match_table = ACPI_PTR(rfkill_acpi_match), 212 .of_match_table = of_match_ptr(rfkill_of_match), 213 }, 214 }; 215 216 module_platform_driver(rfkill_gpio_driver); 217 218 MODULE_DESCRIPTION("gpio rfkill"); 219 MODULE_AUTHOR("NVIDIA"); 220 MODULE_LICENSE("GPL"); 221