blob: f5afc9bcdee6543ec6a26d373f29b920fff528b3 [file] [log] [blame]
Thomas Gleixner16216332019-05-19 15:51:31 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rhyland Klein7176ba22011-05-16 14:41:48 -07002/*
3 * Copyright (c) 2011, NVIDIA Corporation.
Rhyland Klein7176ba22011-05-16 14:41:48 -07004 */
5
Rhyland Klein7176ba22011-05-16 14:41:48 -07006#include <linux/init.h>
7#include <linux/kernel.h>
8#include <linux/module.h>
Arnd Bergmannf623f752018-08-14 00:35:24 +02009#include <linux/mod_devicetable.h>
Rhyland Klein7176ba22011-05-16 14:41:48 -070010#include <linux/rfkill.h>
11#include <linux/platform_device.h>
12#include <linux/clk.h>
13#include <linux/slab.h>
Heikki Krogerusef91ffa2013-10-16 13:53:43 +030014#include <linux/acpi.h>
Heikki Krogerus2111cad2013-11-26 12:05:46 +020015#include <linux/gpio/consumer.h>
Rhyland Klein7176ba22011-05-16 14:41:48 -070016
Rhyland Klein7176ba22011-05-16 14:41:48 -070017struct rfkill_gpio_data {
Heikki Krogerus262c91e2013-10-16 13:53:42 +030018 const char *name;
19 enum rfkill_type type;
Heikki Krogerus2111cad2013-11-26 12:05:46 +020020 struct gpio_desc *reset_gpio;
21 struct gpio_desc *shutdown_gpio;
Heikki Krogerus262c91e2013-10-16 13:53:42 +030022
23 struct rfkill *rfkill_dev;
Heikki Krogerus262c91e2013-10-16 13:53:42 +030024 struct clk *clk;
25
26 bool clk_enabled;
Rhyland Klein7176ba22011-05-16 14:41:48 -070027};
28
29static int rfkill_gpio_set_power(void *data, bool blocked)
30{
31 struct rfkill_gpio_data *rfkill = data;
32
Loic Poulain771bb3d2014-05-07 11:38:11 +020033 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);
Rhyland Klein7176ba22011-05-16 14:41:48 -070041
Loic Poulainfa5c1072014-09-16 14:53:58 +020042 rfkill->clk_enabled = !blocked;
Rhyland Klein7176ba22011-05-16 14:41:48 -070043
44 return 0;
45}
46
47static const struct rfkill_ops rfkill_gpio_ops = {
48 .set_block = rfkill_gpio_set_power,
49};
50
Mika Westerberg72daceb2014-10-27 12:15:14 +020051static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
52static const struct acpi_gpio_params shutdown_gpios = { 1, 0, false };
53
54static const struct acpi_gpio_mapping acpi_rfkill_default_gpios[] = {
55 { "reset-gpios", &reset_gpios, 1 },
56 { "shutdown-gpios", &shutdown_gpios, 1 },
57 { },
58};
59
Heikki Krogerusef91ffa2013-10-16 13:53:43 +030060static 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
Heikki Krogerusef91ffa2013-10-16 13:53:43 +030069 rfkill->type = (unsigned)id->driver_data;
Heikki Krogerusef91ffa2013-10-16 13:53:43 +030070
Andy Shevchenko45246672017-06-10 22:10:34 +030071 return devm_acpi_dev_add_driver_gpios(dev, acpi_rfkill_default_gpios);
Heikki Krogerusef91ffa2013-10-16 13:53:43 +030072}
73
Rhyland Klein7176ba22011-05-16 14:41:48 -070074static int rfkill_gpio_probe(struct platform_device *pdev)
75{
Heikki Krogerus262c91e2013-10-16 13:53:42 +030076 struct rfkill_gpio_data *rfkill;
Heikki Krogerus2111cad2013-11-26 12:05:46 +020077 struct gpio_desc *gpio;
Heikki Krogerus7d5e9732016-01-25 12:03:47 +030078 const char *type_name;
Heikki Krogerus2111cad2013-11-26 12:05:46 +020079 int ret;
Rhyland Klein7176ba22011-05-16 14:41:48 -070080
Heikki Krogerus5e7ca392013-10-16 13:53:39 +030081 rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
Rhyland Klein7176ba22011-05-16 14:41:48 -070082 if (!rfkill)
83 return -ENOMEM;
84
Heikki Krogerus7d5e9732016-01-25 12:03:47 +030085 device_property_read_string(&pdev->dev, "name", &rfkill->name);
86 device_property_read_string(&pdev->dev, "type", &type_name);
87
88 if (!rfkill->name)
89 rfkill->name = dev_name(&pdev->dev);
90
91 rfkill->type = rfkill_find_type(type_name);
92
Heikki Krogerusef91ffa2013-10-16 13:53:43 +030093 if (ACPI_HANDLE(&pdev->dev)) {
94 ret = rfkill_gpio_acpi_probe(&pdev->dev, rfkill);
95 if (ret)
96 return ret;
Heikki Krogerus262c91e2013-10-16 13:53:42 +030097 }
98
Heikki Krogerus848ef582014-04-01 17:02:53 +030099 rfkill->clk = devm_clk_get(&pdev->dev, NULL);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700100
Uwe Kleine-Königf7959e92015-05-28 11:46:12 +0200101 gpio = devm_gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
102 if (IS_ERR(gpio))
103 return PTR_ERR(gpio);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700104
Uwe Kleine-Königf7959e92015-05-28 11:46:12 +0200105 rfkill->reset_gpio = gpio;
106
107 gpio = devm_gpiod_get_optional(&pdev->dev, "shutdown", GPIOD_OUT_LOW);
108 if (IS_ERR(gpio))
109 return PTR_ERR(gpio);
110
111 rfkill->shutdown_gpio = gpio;
Heikki Krogerus2111cad2013-11-26 12:05:46 +0200112
Heikki Krogerus7d5e9732016-01-25 12:03:47 +0300113 /* Make sure at-least one GPIO is defined for this instance */
114 if (!rfkill->reset_gpio && !rfkill->shutdown_gpio) {
Heikki Krogerus2111cad2013-11-26 12:05:46 +0200115 dev_err(&pdev->dev, "invalid platform data\n");
116 return -EINVAL;
117 }
118
Heikki Krogerus262c91e2013-10-16 13:53:42 +0300119 rfkill->rfkill_dev = rfkill_alloc(rfkill->name, &pdev->dev,
120 rfkill->type, &rfkill_gpio_ops,
121 rfkill);
Heikki Krogerus5e7ca392013-10-16 13:53:39 +0300122 if (!rfkill->rfkill_dev)
123 return -ENOMEM;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700124
125 ret = rfkill_register(rfkill->rfkill_dev);
126 if (ret < 0)
Johan Hovold4bf01ca2018-04-26 09:31:52 +0200127 goto err_destroy;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700128
129 platform_set_drvdata(pdev, rfkill);
130
Heikki Krogerus262c91e2013-10-16 13:53:42 +0300131 dev_info(&pdev->dev, "%s device registered.\n", rfkill->name);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700132
133 return 0;
Johan Hovold4bf01ca2018-04-26 09:31:52 +0200134
135err_destroy:
136 rfkill_destroy(rfkill->rfkill_dev);
137
138 return ret;
Rhyland Klein7176ba22011-05-16 14:41:48 -0700139}
140
141static int rfkill_gpio_remove(struct platform_device *pdev)
142{
143 struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
144
Rhyland Klein7176ba22011-05-16 14:41:48 -0700145 rfkill_unregister(rfkill->rfkill_dev);
146 rfkill_destroy(rfkill->rfkill_dev);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700147
148 return 0;
149}
150
Heikki Krogerus41d09df2014-04-01 17:02:55 +0300151#ifdef CONFIG_ACPI
Heikki Krogerusef91ffa2013-10-16 13:53:43 +0300152static const struct acpi_device_id rfkill_acpi_match[] = {
153 { "BCM4752", RFKILL_TYPE_GPS },
Heikki Krogerus04f1b472014-04-01 17:02:54 +0300154 { "LNV4752", RFKILL_TYPE_GPS },
Heikki Krogerusef91ffa2013-10-16 13:53:43 +0300155 { },
156};
Marcel Holtmanndda3b192014-09-12 21:49:28 +0200157MODULE_DEVICE_TABLE(acpi, rfkill_acpi_match);
Heikki Krogerus41d09df2014-04-01 17:02:55 +0300158#endif
Heikki Krogerusef91ffa2013-10-16 13:53:43 +0300159
Rhyland Klein7176ba22011-05-16 14:41:48 -0700160static struct platform_driver rfkill_gpio_driver = {
161 .probe = rfkill_gpio_probe,
Bill Pemberton9e2b29d2012-12-03 09:56:26 -0500162 .remove = rfkill_gpio_remove,
Rhyland Klein7176ba22011-05-16 14:41:48 -0700163 .driver = {
Heikki Krogerus262c91e2013-10-16 13:53:42 +0300164 .name = "rfkill_gpio",
Heikki Krogerusef91ffa2013-10-16 13:53:43 +0300165 .acpi_match_table = ACPI_PTR(rfkill_acpi_match),
Rhyland Klein7176ba22011-05-16 14:41:48 -0700166 },
167};
168
Axel Lin98ef55f62011-11-28 17:15:04 +0800169module_platform_driver(rfkill_gpio_driver);
Rhyland Klein7176ba22011-05-16 14:41:48 -0700170
171MODULE_DESCRIPTION("gpio rfkill");
172MODULE_AUTHOR("NVIDIA");
173MODULE_LICENSE("GPL");