summaryrefstree
path: root/kernel/drivers/bluetooth/bcm4361.c
blob: 49aa78614ac162701c1343aa52f74dd3c28ce75d (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Bluetooth Broadcom GPIO and Low Power Mode control
 *
 *  Copyright (C) 2011 Samsung Electronics Co., Ltd.
 *  Copyright (C) 2011 Google, Inc.
 *
 *  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.
 *
 */

#define DEBUG

#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/hrtimer.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/rfkill.h>
#include <linux/of_gpio.h>

#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>

#include <linux/slab.h>
#include <linux/pinctrl/consumer.h>


static struct rfkill *bt_rfkill;

struct bluetooth_bcm_platform_data {
	/* Bluetooth reset gpio */
	int bt_gpio_bt_en;
};

static struct of_device_id bt_dt_match_table[] = {
	{	.compatible = "bcm,btdriver" },
	{}
};

static struct bluetooth_bcm_platform_data *bt_power_pdata;


static int bcm4361_bt_rfkill_set_power(void *data, bool blocked)
{
	if (!blocked) {
		pr_err("[BT] Bluetooth Power On (%d)\n", bt_power_pdata->bt_gpio_bt_en);

		gpio_direction_output(bt_power_pdata->bt_gpio_bt_en, 1);

	} else {
			pr_err("[BT] Bluetooth Power Off.\n");

			gpio_direction_output(bt_power_pdata->bt_gpio_bt_en, 0);
	}

		return 0;
}

static const struct rfkill_ops bcm4361_bt_rfkill_ops = {
		.set_block = bcm4361_bt_rfkill_set_power,
};

static int bt_populate_dt_pinfo(struct platform_device *pdev)
{
	pr_err("[BT] bcm4361 dt_info\n");

	if (!bt_power_pdata)
		return -ENOMEM;

	if (pdev->dev.of_node) {
		bt_power_pdata->bt_gpio_bt_en =
			of_get_named_gpio(pdev->dev.of_node,
						"bcm,bt-reset-gpio", 0);

		if (bt_power_pdata->bt_gpio_bt_en < 0) {
			pr_err("bt-reset-gpio not provided in device tree");
			return bt_power_pdata->bt_gpio_bt_en;
		}
		pr_err("[BT] bt_en pin is %d\n", bt_power_pdata->bt_gpio_bt_en);
	}
	return 0;
}


static int bcm4361_bluetooth_probe(struct platform_device *pdev)
{
	int rc = 0;
	int ret;

	pr_err("[BT] %s\n", __func__);

	if (!bt_power_pdata)
		bt_power_pdata =
				kzalloc(sizeof(struct bluetooth_bcm_platform_data),	GFP_KERNEL);

	if (!bt_power_pdata) {
		pr_err("Failed to allocate memory");
		return -ENOMEM;
	}

	if (pdev->dev.of_node) {
		ret = bt_populate_dt_pinfo(pdev);
		if (ret < 0) {
			pr_err("[BT] Failed to populate device tree info\n");
			return ret;
		}

		pdev->dev.platform_data = bt_power_pdata;
	}

	rc = gpio_request(bt_power_pdata->bt_gpio_bt_en, "bcm4361_bten_gpio");
	if (rc) {
		pr_err("[BT] %s: gpio_request for GPIO_BT_EN is failed", __func__);
		gpio_free(bt_power_pdata->bt_gpio_bt_en);
	}

	gpio_direction_output(bt_power_pdata->bt_gpio_bt_en, 0);

	bt_rfkill = rfkill_alloc("bcm4361 Bluetooth", &pdev->dev,
							RFKILL_TYPE_BLUETOOTH, &bcm4361_bt_rfkill_ops,
							NULL);

	if (unlikely(!bt_rfkill)) {
			pr_err("[BT] bt_rfkill alloc failed.\n");
			return -ENOMEM;
	}

	rfkill_init_sw_state(bt_rfkill, 0);

	rc = rfkill_register(bt_rfkill);

	if (unlikely(rc)) {
			pr_err("[BT] bt_rfkill register failed.\n");
			rfkill_destroy(bt_rfkill);
			return rc;
	}

	rfkill_set_sw_state(bt_rfkill, true);

	return rc;
}

static int bcm4361_bluetooth_remove(struct platform_device *pdev)
{
		rfkill_unregister(bt_rfkill);
		rfkill_destroy(bt_rfkill);

		gpio_free(bt_power_pdata->bt_gpio_bt_en);

		return 0;
}

static struct platform_driver bcm4361_bluetooth_platform_driver = {
		.probe = bcm4361_bluetooth_probe,
		.remove = bcm4361_bluetooth_remove,
		.driver = {
		.name = "bcm4361_bluetooth",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(bt_dt_match_table),
		},
};

static int __init bcm4361_bluetooth_init(void)
{
	int ret;

	pr_err("[BT] %s\n", __func__);

	ret = platform_driver_register(&bcm4361_bluetooth_platform_driver);
	if (ret) {
		pr_err("[BT] %s failed\n", __func__);
	}
	return ret;
}

static void __exit bcm4361_bluetooth_exit(void)
{
	platform_driver_unregister(&bcm4361_bluetooth_platform_driver);
}

module_init(bcm4361_bluetooth_init);
module_exit(bcm4361_bluetooth_exit);

MODULE_ALIAS("platform:bcm4361");
MODULE_DESCRIPTION("bcm4361_bluetooth");
MODULE_LICENSE("GPL");