Nick Pelly | 5dfd8cd | 2009-06-11 10:49:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Bluetooth TI wl127x rfkill power control via GPIO |
| 3 | * |
| 4 | * Copyright (C) 2009 Motorola, Inc. |
| 5 | * Copyright (C) 2008 Texas Instruments |
| 6 | * Initial code: Pavan Savoy <pavan.savoy@gmail.com> (wl127x_power.c) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/gpio.h> |
| 27 | #include <linux/rfkill.h> |
| 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/wl127x-rfkill.h> |
| 30 | |
| 31 | static int wl127x_rfkill_set_power(void *data, enum rfkill_state state) |
| 32 | { |
| 33 | int nshutdown_gpio = (int) data; |
| 34 | |
| 35 | switch (state) { |
| 36 | case RFKILL_STATE_UNBLOCKED: |
| 37 | gpio_set_value(nshutdown_gpio, 1); |
| 38 | break; |
| 39 | case RFKILL_STATE_SOFT_BLOCKED: |
| 40 | gpio_set_value(nshutdown_gpio, 0); |
| 41 | break; |
| 42 | default: |
| 43 | printk(KERN_ERR "invalid bluetooth rfkill state %d\n", state); |
| 44 | } |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | static int wl127x_rfkill_probe(struct platform_device *pdev) |
| 49 | { |
| 50 | int rc = 0; |
| 51 | struct wl127x_rfkill_platform_data *pdata = pdev->dev.platform_data; |
| 52 | enum rfkill_state default_state = RFKILL_STATE_SOFT_BLOCKED; /* off */ |
| 53 | |
| 54 | rc = gpio_request(pdata->nshutdown_gpio, "wl127x_nshutdown_gpio"); |
| 55 | if (unlikely(rc)) |
| 56 | return rc; |
| 57 | |
| 58 | rc = gpio_direction_output(pdata->nshutdown_gpio, 0); |
| 59 | if (unlikely(rc)) |
| 60 | return rc; |
| 61 | |
| 62 | rfkill_set_default(RFKILL_TYPE_BLUETOOTH, default_state); |
| 63 | wl127x_rfkill_set_power(NULL, default_state); |
| 64 | |
| 65 | pdata->rfkill = rfkill_allocate(&pdev->dev, RFKILL_TYPE_BLUETOOTH); |
| 66 | if (unlikely(!pdata->rfkill)) |
| 67 | return -ENOMEM; |
| 68 | |
| 69 | pdata->rfkill->name = "wl127x"; |
| 70 | pdata->rfkill->state = default_state; |
| 71 | /* userspace cannot take exclusive control */ |
| 72 | pdata->rfkill->user_claim_unsupported = 1; |
| 73 | pdata->rfkill->user_claim = 0; |
| 74 | pdata->rfkill->data = (void *) pdata->nshutdown_gpio; |
| 75 | pdata->rfkill->toggle_radio = wl127x_rfkill_set_power; |
| 76 | |
| 77 | rc = rfkill_register(pdata->rfkill); |
| 78 | |
| 79 | if (unlikely(rc)) |
| 80 | rfkill_free(pdata->rfkill); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int wl127x_rfkill_remove(struct platform_device *pdev) |
| 86 | { |
| 87 | struct wl127x_rfkill_platform_data *pdata = pdev->dev.platform_data; |
| 88 | |
| 89 | rfkill_unregister(pdata->rfkill); |
| 90 | rfkill_free(pdata->rfkill); |
| 91 | gpio_free(pdata->nshutdown_gpio); |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static struct platform_driver wl127x_rfkill_platform_driver = { |
| 97 | .probe = wl127x_rfkill_probe, |
| 98 | .remove = wl127x_rfkill_remove, |
| 99 | .driver = { |
| 100 | .name = "wl127x-rfkill", |
| 101 | .owner = THIS_MODULE, |
| 102 | }, |
| 103 | }; |
| 104 | |
| 105 | static int __init wl127x_rfkill_init(void) |
| 106 | { |
| 107 | return platform_driver_register(&wl127x_rfkill_platform_driver); |
| 108 | } |
| 109 | |
| 110 | static void __exit wl127x_rfkill_exit(void) |
| 111 | { |
| 112 | platform_driver_unregister(&wl127x_rfkill_platform_driver); |
| 113 | } |
| 114 | |
| 115 | module_init(wl127x_rfkill_init); |
| 116 | module_exit(wl127x_rfkill_exit); |
| 117 | |
| 118 | MODULE_ALIAS("platform:wl127x"); |
| 119 | MODULE_DESCRIPTION("wl127x-rfkill"); |
| 120 | MODULE_AUTHOR("Motorola"); |
| 121 | MODULE_LICENSE("GPL"); |