OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2010, NVIDIA Corporation. |
| 3 * |
| 4 * This program is free software; you can redistribute it and/or modify |
| 5 * it under the terms of the GNU General Public License as published by |
| 6 * the Free Software Foundation; either version 2 of the License, or |
| 7 * (at your option) any later version. |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 * more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License along |
| 15 * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 */ |
| 18 |
| 19 #include <linux/err.h> |
| 20 #include <linux/types.h> |
| 21 #include <linux/uaccess.h> |
| 22 #include <linux/fs.h> |
| 23 #include <linux/gpio.h> |
| 24 #include <linux/init.h> |
| 25 #include <linux/kernel.h> |
| 26 #include <linux/miscdevice.h> |
| 27 #include <linux/module.h> |
| 28 #include <linux/rfkill.h> |
| 29 #include <linux/platform_device.h> |
| 30 #include <linux/clk.h> |
| 31 #include <linux/slab.h> |
| 32 |
| 33 struct bt_rfkill_data { |
| 34 int gpio_reset; |
| 35 int gpio_shutdown; |
| 36 int delay; |
| 37 struct clk *bt_clk; |
| 38 }; |
| 39 |
| 40 static struct bt_rfkill_data *bt_rfkill; |
| 41 |
| 42 static int bt_rfkill_set_power(void *data, bool blocked) |
| 43 { |
| 44 if (blocked) { |
| 45 if (bt_rfkill->gpio_shutdown) |
| 46 gpio_direction_output(bt_rfkill->gpio_shutdown, 0); |
| 47 if (bt_rfkill->gpio_reset) |
| 48 gpio_direction_output(bt_rfkill->gpio_reset, 0); |
| 49 if (bt_rfkill->bt_clk) |
| 50 clk_disable(bt_rfkill->bt_clk); |
| 51 } else { |
| 52 if (bt_rfkill->bt_clk) |
| 53 clk_enable(bt_rfkill->bt_clk); |
| 54 if (bt_rfkill->gpio_shutdown) |
| 55 gpio_direction_output(bt_rfkill->gpio_shutdown, 1); |
| 56 if (bt_rfkill->gpio_reset) |
| 57 gpio_direction_output(bt_rfkill->gpio_reset, 1); |
| 58 } |
| 59 |
| 60 return 0; |
| 61 } |
| 62 |
| 63 static const struct rfkill_ops bt_rfkill_ops = { |
| 64 .set_block = bt_rfkill_set_power, |
| 65 }; |
| 66 |
| 67 static int bt_rfkill_probe(struct platform_device *pdev) |
| 68 { |
| 69 struct rfkill *bt_rfkill_dev; |
| 70 struct resource *res; |
| 71 int ret; |
| 72 bool enable = false; /* off */ |
| 73 bool default_sw_block_state; |
| 74 |
| 75 bt_rfkill = kzalloc(sizeof(*bt_rfkill), GFP_KERNEL); |
| 76 if (!bt_rfkill) |
| 77 return -ENOMEM; |
| 78 |
| 79 bt_rfkill->bt_clk = clk_get(&pdev->dev, "bt_clk"); |
| 80 if (IS_ERR(bt_rfkill->bt_clk)) { |
| 81 pr_warn("%s: can't find bt_clk.\ |
| 82 assuming clock to chip\n", __func__); |
| 83 bt_rfkill->bt_clk = NULL; |
| 84 } |
| 85 |
| 86 res = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 87 "bt_nreset_gpio"); |
| 88 if (res) { |
| 89 bt_rfkill->gpio_reset = res->start; |
| 90 tegra_gpio_enable(bt_rfkill->gpio_reset); |
| 91 ret = gpio_request(bt_rfkill->gpio_reset, |
| 92 "bt_nreset_gpio"); |
| 93 } else { |
| 94 pr_warn("%s : can't find reset gpio.\n", __func__); |
| 95 bt_rfkill->gpio_reset = 0; |
| 96 } |
| 97 |
| 98 res = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 99 "bt_nshutdown_gpio"); |
| 100 if (res) { |
| 101 bt_rfkill->gpio_shutdown = res->start; |
| 102 tegra_gpio_enable(bt_rfkill->gpio_shutdown); |
| 103 ret = gpio_request(bt_rfkill->gpio_shutdown, |
| 104 "bt_nshutdown_gpio"); |
| 105 } else { |
| 106 pr_warn("%s : can't find shutdown gpio.\n", __func__); |
| 107 bt_rfkill->gpio_shutdown = 0; |
| 108 } |
| 109 |
| 110 /* make sure at-least one of the GPIO is defined */ |
| 111 if (!bt_rfkill->gpio_reset && !bt_rfkill->gpio_shutdown) |
| 112 goto free_bcm_res; |
| 113 |
| 114 if (bt_rfkill->bt_clk && enable) |
| 115 clk_enable(bt_rfkill->bt_clk); |
| 116 if (bt_rfkill->gpio_shutdown) |
| 117 gpio_direction_output(bt_rfkill->gpio_shutdown, enable); |
| 118 if (bt_rfkill->gpio_reset) |
| 119 gpio_direction_output(bt_rfkill->gpio_reset, enable); |
| 120 |
| 121 bt_rfkill_dev = rfkill_alloc("bt dev rfkill", &pdev->dev, |
| 122 RFKILL_TYPE_BLUETOOTH, &bt_rfkill_ops, |
| 123 NULL); |
| 124 |
| 125 if (unlikely(!bt_rfkill_dev)) |
| 126 goto free_bcm_res; |
| 127 |
| 128 default_sw_block_state = !enable; |
| 129 rfkill_set_states(bt_rfkill_dev, default_sw_block_state, false); |
| 130 |
| 131 ret = rfkill_register(bt_rfkill_dev); |
| 132 |
| 133 if (unlikely(ret)) { |
| 134 rfkill_destroy(bt_rfkill_dev); |
| 135 goto free_bcm_res; |
| 136 } |
| 137 |
| 138 return 0; |
| 139 |
| 140 free_bcm_res: |
| 141 if (bt_rfkill->gpio_shutdown) |
| 142 gpio_free(bt_rfkill->gpio_shutdown); |
| 143 if (bt_rfkill->gpio_reset) |
| 144 gpio_free(bt_rfkill->gpio_reset); |
| 145 if (bt_rfkill->bt_clk && enable) |
| 146 clk_disable(bt_rfkill->bt_clk); |
| 147 if (bt_rfkill->bt_clk) |
| 148 clk_put(bt_rfkill->bt_clk); |
| 149 kfree(bt_rfkill); |
| 150 return -ENODEV; |
| 151 } |
| 152 |
| 153 static int bt_rfkill_remove(struct platform_device *pdev) |
| 154 { |
| 155 struct rfkill *bt_rfkill_dev = platform_get_drvdata(pdev); |
| 156 |
| 157 rfkill_unregister(bt_rfkill_dev); |
| 158 rfkill_destroy(bt_rfkill_dev); |
| 159 if (bt_rfkill->bt_clk) |
| 160 clk_put(bt_rfkill->bt_clk); |
| 161 if (bt_rfkill->gpio_shutdown) |
| 162 gpio_free(bt_rfkill->gpio_shutdown); |
| 163 if (bt_rfkill->gpio_reset) |
| 164 gpio_free(bt_rfkill->gpio_reset); |
| 165 kfree(bt_rfkill); |
| 166 |
| 167 return 0; |
| 168 } |
| 169 |
| 170 static struct platform_driver bt_rfkill_driver = { |
| 171 .probe = bt_rfkill_probe, |
| 172 .remove = bt_rfkill_remove, |
| 173 .driver = { |
| 174 .name = "bt_rfkill", |
| 175 .owner = THIS_MODULE, |
| 176 }, |
| 177 }; |
| 178 |
| 179 static int __init bt_rfkill_init(void) |
| 180 { |
| 181 return platform_driver_register(&bt_rfkill_driver); |
| 182 } |
| 183 |
| 184 static void __exit bt_rfkill_exit(void) |
| 185 { |
| 186 platform_driver_unregister(&bt_rfkill_driver); |
| 187 } |
| 188 |
| 189 module_init(bt_rfkill_init); |
| 190 module_exit(bt_rfkill_exit); |
| 191 |
| 192 MODULE_DESCRIPTION("bt rfkill"); |
| 193 MODULE_AUTHOR("NVIDIA"); |
| 194 MODULE_LICENSE("GPL"); |
OLD | NEW |