Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | /* |
| 13 | * Bluetooth Power Switch Module |
| 14 | * controls power to external Bluetooth device |
| 15 | * with interface to power management device |
| 16 | */ |
| 17 | |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/rfkill.h> |
| 23 | |
| 24 | static bool previous; |
| 25 | |
| 26 | static int bluetooth_toggle_radio(void *data, bool blocked) |
| 27 | { |
| 28 | int ret = 0; |
| 29 | int (*power_control)(int enable); |
| 30 | |
| 31 | power_control = data; |
| 32 | if (previous != blocked) |
| 33 | ret = (*power_control)(!blocked); |
| 34 | if (!ret) |
| 35 | previous = blocked; |
| 36 | return ret; |
| 37 | } |
| 38 | |
| 39 | static const struct rfkill_ops bluetooth_power_rfkill_ops = { |
| 40 | .set_block = bluetooth_toggle_radio, |
| 41 | }; |
| 42 | |
| 43 | static int bluetooth_power_rfkill_probe(struct platform_device *pdev) |
| 44 | { |
| 45 | struct rfkill *rfkill; |
| 46 | int ret; |
| 47 | |
| 48 | rfkill = rfkill_alloc("bt_power", &pdev->dev, RFKILL_TYPE_BLUETOOTH, |
| 49 | &bluetooth_power_rfkill_ops, |
| 50 | pdev->dev.platform_data); |
| 51 | |
| 52 | if (!rfkill) { |
| 53 | dev_err(&pdev->dev, "rfkill allocate failed\n"); |
| 54 | return -ENOMEM; |
| 55 | } |
| 56 | |
| 57 | /* force Bluetooth off during init to allow for user control */ |
| 58 | rfkill_init_sw_state(rfkill, 1); |
| 59 | previous = 1; |
| 60 | |
| 61 | ret = rfkill_register(rfkill); |
| 62 | if (ret) { |
| 63 | dev_err(&pdev->dev, "rfkill register failed=%d\n", ret); |
| 64 | rfkill_destroy(rfkill); |
| 65 | return ret; |
| 66 | } |
| 67 | |
| 68 | platform_set_drvdata(pdev, rfkill); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static void bluetooth_power_rfkill_remove(struct platform_device *pdev) |
| 74 | { |
| 75 | struct rfkill *rfkill; |
| 76 | |
| 77 | dev_dbg(&pdev->dev, "%s\n", __func__); |
| 78 | |
| 79 | rfkill = platform_get_drvdata(pdev); |
| 80 | if (rfkill) |
| 81 | rfkill_unregister(rfkill); |
| 82 | rfkill_destroy(rfkill); |
| 83 | platform_set_drvdata(pdev, NULL); |
| 84 | } |
| 85 | |
| 86 | static int __devinit bt_power_probe(struct platform_device *pdev) |
| 87 | { |
| 88 | int ret = 0; |
| 89 | |
| 90 | dev_dbg(&pdev->dev, "%s\n", __func__); |
| 91 | |
| 92 | if (!pdev->dev.platform_data) { |
| 93 | dev_err(&pdev->dev, "platform data not initialized\n"); |
| 94 | return -ENOSYS; |
| 95 | } |
| 96 | |
| 97 | ret = bluetooth_power_rfkill_probe(pdev); |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | static int __devexit bt_power_remove(struct platform_device *pdev) |
| 103 | { |
| 104 | dev_dbg(&pdev->dev, "%s\n", __func__); |
| 105 | |
| 106 | bluetooth_power_rfkill_remove(pdev); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static struct platform_driver bt_power_driver = { |
| 112 | .probe = bt_power_probe, |
| 113 | .remove = __devexit_p(bt_power_remove), |
| 114 | .driver = { |
| 115 | .name = "bt_power", |
| 116 | .owner = THIS_MODULE, |
| 117 | }, |
| 118 | }; |
| 119 | |
| 120 | static int __init bluetooth_power_init(void) |
| 121 | { |
| 122 | int ret; |
| 123 | |
| 124 | ret = platform_driver_register(&bt_power_driver); |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | static void __exit bluetooth_power_exit(void) |
| 129 | { |
| 130 | platform_driver_unregister(&bt_power_driver); |
| 131 | } |
| 132 | |
| 133 | MODULE_LICENSE("GPL v2"); |
| 134 | MODULE_DESCRIPTION("MSM Bluetooth power control driver"); |
| 135 | MODULE_VERSION("1.40"); |
| 136 | |
| 137 | module_init(bluetooth_power_init); |
| 138 | module_exit(bluetooth_power_exit); |