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