summaryrefslogtreecommitdiff
path: root/net/rfkill/rfkill-wl18xx.c
blob: 1318e1ed8696f610f3d292f29191fd2abf23cddb (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * Bluetooth TI wl18xx rfkill power control via GPIO
 *
 * Copyright (C) 2014 Motorola, Inc.
 * Copyright (C) 2008 Texas Instruments
 * Initial code: Pavan Savoy <pavan.savoy@gmail.com> (wl18xx_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/rfkill-wl18xx.h>
#include <linux/delay.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>

enum wl18xx_devices {
	WL18XX_BLUETOOTH = 0,
	WL18XX_FM,
	WL18XX_MAX_DEV,
};

struct wl18xx_rfkill_driver_data {
	struct wl18xx_rfkill_platform_data *pdata;
	struct rfkill *rfkill[WL18XX_MAX_DEV];
};

#define WL18XX_BT_SUPPORTED(x) ((x)->bt_enable_gpio >= 0)
#define WL18XX_FM_SUPPORTED(x) ((x)->fm_enable_gpio >= 0)

static void wl18xx_rfkill_destroy(struct wl18xx_rfkill_driver_data *p_drvdata);

static int wl18xx_bt_rfkill_set_power(void *data, bool blocked)
{
	struct wl18xx_rfkill_platform_data *pdata =
		(struct wl18xx_rfkill_platform_data *)data;

	if (blocked)
		gpio_set_value(pdata->bt_enable_gpio, 0);
	else
		gpio_set_value(pdata->bt_enable_gpio, 1);

	return 0;
}

static int wl18xx_fm_rfkill_set_power(void *data, bool blocked)
{
	struct wl18xx_rfkill_platform_data *pdata =
		(struct wl18xx_rfkill_platform_data *)data;

	if (blocked)
		gpio_set_value(pdata->fm_enable_gpio, 0);
	else
		gpio_set_value(pdata->fm_enable_gpio, 1);

	return 0;
}

static const struct rfkill_ops wl18xx_bt_rfkill_ops = {
	.set_block = wl18xx_bt_rfkill_set_power,
};

static const struct rfkill_ops wl18xx_fm_rfkill_ops = {
	.set_block = wl18xx_fm_rfkill_set_power,
};

/**
Added to reset the BT chip after Ram download
*/
static ssize_t reset_wl18xx_chip(struct device *dev,
					struct device_attribute
					*attr, const char *buf, size_t size)
{
	struct wl18xx_rfkill_platform_data *pd = dev->platform_data;

	BUG_ON(!pd);
	if (WL18XX_BT_SUPPORTED(pd)) {
		gpio_set_value(pd->bt_enable_gpio, 0);
		msleep(5);
		gpio_set_value(pd->bt_enable_gpio, 1);
	}
	return size;
}
static DEVICE_ATTR(reset_vio, 0200, NULL, reset_wl18xx_chip);

#ifdef CONFIG_OF
static int wl18xx_rfkill_of_init(
				struct wl18xx_rfkill_platform_data *pdata,
				struct device_node *dt_node)
{
	int rc;
	int bt_fm_support = 0;
	int gpio;

	/* Set default values */
	pdata->bt_enable_gpio = -1;
	pdata->fm_enable_gpio = -1;

	/* Set default values */
	rc = of_property_read_u32(dt_node, "mot,bt_fm", &bt_fm_support);
	if (!rc) {
		if (bt_fm_support & 0x01) {
			gpio = of_get_named_gpio_flags(dt_node,
					"mot,bt_en-gpio", 0, NULL);
			pdata->bt_enable_gpio = (gpio < 0) ? -1 : gpio;
		} else {
			pr_err("wl18xx_rfkill: BT support disabled");
		}

		if (bt_fm_support & 0x02) {
			gpio = of_get_named_gpio_flags(dt_node,
					"mot,fm_en-gpio", 0, NULL);
			pdata->fm_enable_gpio = (gpio < 0) ? -1 : gpio;
		} else {
			pr_err("wl18xx_rfkill: FM support disabled");
		}
	} else {
		pr_err("%s: failed to read device tree", __func__);
	}

	return rc;
}
#endif

static int wl18xx_rfkill_gpio_init(struct wl18xx_rfkill_platform_data *pdata)
{
	int rc = 0;
	struct gpio wl18xx_rfkill_bt_gpio[] = {
		{pdata->bt_enable_gpio, GPIOF_OUT_INIT_LOW, "wl18xx_bt_en"},
	};
	struct gpio wl18xx_rfkill_fm_gpio[] = {
		{pdata->fm_enable_gpio, GPIOF_OUT_INIT_LOW, "wl18xx_fm_en"},
	};

	if (WL18XX_BT_SUPPORTED(pdata)) {
		rc = gpio_request_array(wl18xx_rfkill_bt_gpio,
					ARRAY_SIZE(wl18xx_rfkill_bt_gpio));
		if (rc)
			pr_err("%s: Failed to request BT GPIOs\n", __func__);
	}

	if (WL18XX_FM_SUPPORTED(pdata)) {
		rc = gpio_request_array(wl18xx_rfkill_fm_gpio,
					ARRAY_SIZE(wl18xx_rfkill_fm_gpio));
		if (rc)
			pr_err("%s: Failed to request FM GPIOs\n", __func__);
	}

	return rc;
}

static int wl18xx_rfkill_probe(struct platform_device *pdev)
{
	int rc = 0;
	struct wl18xx_rfkill_driver_data *dd = NULL;
	struct wl18xx_rfkill_platform_data *pd = NULL;

	pr_info("%s\n", __func__);

	dd = kzalloc(sizeof(struct wl18xx_rfkill_driver_data), GFP_KERNEL);
	if (dd == NULL) {
		pr_err("%s: memory allocation failure\n", __func__);
		rc = -ENOMEM;
		goto wl18xx_probe_fail;
	}

	/* set platform data */
	if (pdev->dev.of_node) {
		pd = devm_kzalloc(&pdev->dev,
				sizeof(struct wl18xx_rfkill_platform_data),
				GFP_KERNEL);
		if (!pd) {
			pr_err("%s: memory allocation failure\n", __func__);
			rc = -ENOMEM;
			goto wl18xx_probe_fail;
		}

		rc = wl18xx_rfkill_of_init(pd, pdev->dev.of_node);
		if (rc)
			goto wl18xx_probe_fail;
		pdev->dev.platform_data = pd;
	} else {
		pd = pdev->dev.platform_data;
	}
	dd->pdata = pd;

	/* set driver data */
	dev_set_drvdata(&pdev->dev, dd);

	/* verify pdata */
	if (!WL18XX_BT_SUPPORTED(pd) && WL18XX_FM_SUPPORTED(pd)) {
		pr_err("%s: invalid configuration\n", __func__);
		rc = -EINVAL;
		goto wl18xx_probe_fail;
	}

	/* requesting gpios */
	rc = wl18xx_rfkill_gpio_init(pd);
	if (rc)
		goto gpio_err;

	if (WL18XX_BT_SUPPORTED(pd)) {
		/* init rfkill for BT */
		dd->rfkill[WL18XX_BLUETOOTH] = rfkill_alloc(
					"wl18xx Bluetooth",
					&pdev->dev,
					RFKILL_TYPE_BLUETOOTH,
					&wl18xx_bt_rfkill_ops,
					(void *)pd);
		if (unlikely(!dd->rfkill[WL18XX_BLUETOOTH])) {
			pr_err("%s: memory allocation failure\n", __func__);
			rc = -ENOMEM;
			goto err_rfkill_register;
		}

		rfkill_set_states(dd->rfkill[WL18XX_BLUETOOTH],
				  true, false);
		rc = rfkill_register(dd->rfkill[WL18XX_BLUETOOTH]);
		if (unlikely(rc)) {
			pr_err("%s: failed to register BT rfkill\n", __func__);
			goto err_rfkill_register;
		}
	}

	if (WL18XX_FM_SUPPORTED(pd)) {
		/* init rfkill for FM */
		dd->rfkill[WL18XX_FM] = rfkill_alloc(
					"wl18xx FM Radio", &pdev->dev,
					RFKILL_TYPE_FM, &wl18xx_fm_rfkill_ops,
					(void *)pd);
		if (unlikely(!dd->rfkill[WL18XX_FM])) {
			pr_err("%s: memory allocation failure\n", __func__);
			rc = -ENOMEM;
			goto err_rfkill_register;
		}

		rfkill_set_states(dd->rfkill[WL18XX_FM],
				  true, false);
		rc = rfkill_register(dd->rfkill[WL18XX_FM]);
		if (unlikely(rc)) {
			pr_err("%s: failed to register BT rfkill\n", __func__);
			goto err_rfkill_register;
		}
	}

	/* Create device file to expose interface to user space to
	reset the vio of BT, this should be done independent of whether
	BT/FM is initialised as both run on the same w18xx chip
	*/
	rc = device_create_file(&pdev->dev, &dev_attr_reset_vio);
	if (rc) {
		pr_err("%s: failed to create sys entry\n", __func__);
		goto err_rfkill_register;
	}

	goto done;

	/* Clean up for BT generic registration */
err_rfkill_register:
	wl18xx_rfkill_destroy(dd);

gpio_err:
	if (pd->bt_enable_gpio > 0)
		gpio_free(pd->bt_enable_gpio);
	if (pd->fm_enable_gpio > 0)
		gpio_free(pd->fm_enable_gpio);

wl18xx_probe_fail:
	kfree(dd);

	pr_err("%s: failed with error = %d\n", __func__, rc);

done:
	return rc;
}

static void wl18xx_rfkill_destroy(struct wl18xx_rfkill_driver_data *p_drvdata)
{
	BUG_ON(!p_drvdata);

	if (p_drvdata->rfkill[WL18XX_BLUETOOTH]) {
		rfkill_unregister(p_drvdata->rfkill[WL18XX_BLUETOOTH]);
		rfkill_destroy(p_drvdata->rfkill[WL18XX_BLUETOOTH]);
	}

	if (p_drvdata->rfkill[WL18XX_FM]) {
		rfkill_unregister(p_drvdata->rfkill[WL18XX_FM]);
		rfkill_destroy(p_drvdata->rfkill[WL18XX_FM]);
	}
}

static int wl18xx_rfkill_remove(struct platform_device *pdev)
{
	struct wl18xx_rfkill_driver_data *dd;
	struct wl18xx_rfkill_platform_data *pd;

	dd =  dev_get_drvdata(&pdev->dev);
	pd = pdev->dev.platform_data;

	/* remove the sys fs file created as part of probe*/
	device_remove_file(&pdev->dev, &dev_attr_reset_vio);

	if (pd->bt_enable_gpio > 0)
		gpio_free(pd->bt_enable_gpio);
	if (pd->fm_enable_gpio > 0)
		gpio_free(pd->fm_enable_gpio);
	kfree(pd);

	return 0;
}

#ifdef CONFIG_OF
static struct of_device_id mot_wl18xx_rfkill_table[] = {
	{ .compatible = "mot,wl18xx-rfkill",},
	{ },
};
#else
#define mot_wl18xx_rfkill_table NULL
#endif

static struct platform_driver wl18xx_rfkill_platform_driver = {
	.probe = wl18xx_rfkill_probe,
	.remove = wl18xx_rfkill_remove,
	.driver = {
		   .name = "wl18xx-rfkill",
		   .owner = THIS_MODULE,
		   .of_match_table = mot_wl18xx_rfkill_table,
		   },
};

static int __init wl18xx_rfkill_init(void)
{
	return platform_driver_register(&wl18xx_rfkill_platform_driver);
}

static void __exit wl18xx_rfkill_exit(void)
{
	platform_driver_unregister(&wl18xx_rfkill_platform_driver);
}

module_init(wl18xx_rfkill_init);
module_exit(wl18xx_rfkill_exit);

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