summaryrefslogtreecommitdiff
path: root/drivers/misc/wl127x-rfkill.c
blob: f5b95152948b2aae3a2bc04800b15a237535b083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Bluetooth TI wl127x rfkill power control via GPIO
 *
 * Copyright (C) 2009 Motorola, Inc.
 * Copyright (C) 2008 Texas Instruments
 * Initial code: Pavan Savoy <pavan.savoy@gmail.com> (wl127x_power.c)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/rfkill.h>
#include <linux/platform_device.h>
#include <linux/wl127x-rfkill.h>

static int wl127x_rfkill_set_power(void *data, enum rfkill_state state)
{
	int nshutdown_gpio = (int) data;

	switch (state) {
	case RFKILL_STATE_UNBLOCKED:
		gpio_set_value(nshutdown_gpio, 1);
		break;
	case RFKILL_STATE_SOFT_BLOCKED:
		gpio_set_value(nshutdown_gpio, 0);
		break;
	default:
		printk(KERN_ERR "invalid bluetooth rfkill state %d\n", state);
	}
	return 0;
}

static int wl127x_rfkill_probe(struct platform_device *pdev)
{
	int rc = 0;
	struct wl127x_rfkill_platform_data *pdata = pdev->dev.platform_data;
	enum rfkill_state default_state = RFKILL_STATE_SOFT_BLOCKED;  /* off */

	rc = gpio_request(pdata->nshutdown_gpio, "wl127x_nshutdown_gpio");
	if (unlikely(rc))
		return rc;

	rc = gpio_direction_output(pdata->nshutdown_gpio, 0);
	if (unlikely(rc))
		return rc;

	rfkill_set_default(RFKILL_TYPE_BLUETOOTH, default_state);
	wl127x_rfkill_set_power(NULL, default_state);

	pdata->rfkill = rfkill_allocate(&pdev->dev, RFKILL_TYPE_BLUETOOTH);
	if (unlikely(!pdata->rfkill))
		return -ENOMEM;

	pdata->rfkill->name = "wl127x";
	pdata->rfkill->state = default_state;
	/* userspace cannot take exclusive control */
	pdata->rfkill->user_claim_unsupported = 1;
	pdata->rfkill->user_claim = 0;
	pdata->rfkill->data = (void *) pdata->nshutdown_gpio;
	pdata->rfkill->toggle_radio = wl127x_rfkill_set_power;

	rc = rfkill_register(pdata->rfkill);

	if (unlikely(rc))
		rfkill_free(pdata->rfkill);

	return 0;
}

static int wl127x_rfkill_remove(struct platform_device *pdev)
{
	struct wl127x_rfkill_platform_data *pdata = pdev->dev.platform_data;

	rfkill_unregister(pdata->rfkill);
	rfkill_free(pdata->rfkill);
	gpio_free(pdata->nshutdown_gpio);

	return 0;
}

static struct platform_driver wl127x_rfkill_platform_driver = {
	.probe = wl127x_rfkill_probe,
	.remove = wl127x_rfkill_remove,
	.driver = {
		   .name = "wl127x-rfkill",
		   .owner = THIS_MODULE,
		   },
};

static int __init wl127x_rfkill_init(void)
{
	return platform_driver_register(&wl127x_rfkill_platform_driver);
}

static void __exit wl127x_rfkill_exit(void)
{
	platform_driver_unregister(&wl127x_rfkill_platform_driver);
}

module_init(wl127x_rfkill_init);
module_exit(wl127x_rfkill_exit);

MODULE_ALIAS("platform:wl127x");
MODULE_DESCRIPTION("wl127x-rfkill");
MODULE_AUTHOR("Motorola");
MODULE_LICENSE("GPL");