Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * otg-wakelock.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/device.h> |
Arve Hjønnevåg | 7369345 | 2012-11-26 16:09:13 -0800 | [diff] [blame^] | 19 | #include <linux/err.h> |
Colin Cross | 666167c | 2012-02-01 14:23:15 -0800 | [diff] [blame] | 20 | #include <linux/module.h> |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 21 | #include <linux/notifier.h> |
| 22 | #include <linux/wakelock.h> |
| 23 | #include <linux/spinlock.h> |
| 24 | #include <linux/usb/otg.h> |
| 25 | |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 26 | #define TEMPORARY_HOLD_TIME 2000 |
| 27 | |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 28 | static bool enabled = true; |
Benoit Goby | 3f9860a | 2012-05-10 16:41:40 -0700 | [diff] [blame] | 29 | static struct usb_phy *otgwl_xceiv; |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 30 | static struct notifier_block otgwl_nb; |
| 31 | |
| 32 | /* |
| 33 | * otgwl_spinlock is held while the VBUS lock is grabbed or dropped and the |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 34 | * held field is updated to match. |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | static DEFINE_SPINLOCK(otgwl_spinlock); |
| 38 | |
| 39 | /* |
| 40 | * Only one lock, but since these 3 fields are associated with each other... |
| 41 | */ |
| 42 | |
| 43 | struct otgwl_lock { |
| 44 | char name[40]; |
| 45 | struct wake_lock wakelock; |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 46 | bool held; |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | /* |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 50 | * VBUS present lock. Also used as a timed lock on charger |
| 51 | * connect/disconnect and USB host disconnect, to allow the system |
| 52 | * to react to the change in power. |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 53 | */ |
| 54 | |
| 55 | static struct otgwl_lock vbus_lock; |
| 56 | |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 57 | static void otgwl_hold(struct otgwl_lock *lock) |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 58 | { |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 59 | if (!lock->held) { |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 60 | wake_lock(&lock->wakelock); |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 61 | lock->held = true; |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 65 | static void otgwl_temporary_hold(struct otgwl_lock *lock) |
| 66 | { |
| 67 | wake_lock_timeout(&lock->wakelock, |
| 68 | msecs_to_jiffies(TEMPORARY_HOLD_TIME)); |
| 69 | lock->held = false; |
| 70 | } |
| 71 | |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 72 | static void otgwl_drop(struct otgwl_lock *lock) |
| 73 | { |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 74 | if (lock->held) { |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 75 | wake_unlock(&lock->wakelock); |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 76 | lock->held = false; |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 80 | static void otgwl_handle_event(unsigned long event) |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 81 | { |
| 82 | unsigned long irqflags; |
| 83 | |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 84 | spin_lock_irqsave(&otgwl_spinlock, irqflags); |
| 85 | |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 86 | if (!enabled) { |
| 87 | otgwl_drop(&vbus_lock); |
| 88 | spin_unlock_irqrestore(&otgwl_spinlock, irqflags); |
| 89 | return; |
| 90 | } |
| 91 | |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 92 | switch (event) { |
| 93 | case USB_EVENT_VBUS: |
| 94 | case USB_EVENT_ENUMERATED: |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 95 | otgwl_hold(&vbus_lock); |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 96 | break; |
| 97 | |
| 98 | case USB_EVENT_NONE: |
| 99 | case USB_EVENT_ID: |
| 100 | case USB_EVENT_CHARGER: |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 101 | otgwl_temporary_hold(&vbus_lock); |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 102 | break; |
| 103 | |
| 104 | default: |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | spin_unlock_irqrestore(&otgwl_spinlock, irqflags); |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static int otgwl_otg_notifications(struct notifier_block *nb, |
| 112 | unsigned long event, void *unused) |
| 113 | { |
| 114 | otgwl_handle_event(event); |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 115 | return NOTIFY_OK; |
| 116 | } |
| 117 | |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 118 | static int set_enabled(const char *val, const struct kernel_param *kp) |
| 119 | { |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 120 | int rv = param_set_bool(val, kp); |
| 121 | |
| 122 | if (rv) |
| 123 | return rv; |
| 124 | |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 125 | if (otgwl_xceiv) |
| 126 | otgwl_handle_event(otgwl_xceiv->last_event); |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 127 | |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static struct kernel_param_ops enabled_param_ops = { |
| 132 | .set = set_enabled, |
| 133 | .get = param_get_bool, |
| 134 | }; |
| 135 | |
| 136 | module_param_cb(enabled, &enabled_param_ops, &enabled, 0644); |
| 137 | MODULE_PARM_DESC(enabled, "enable wakelock when VBUS present"); |
| 138 | |
| 139 | static int __init otg_wakelock_init(void) |
| 140 | { |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 141 | int ret; |
Arve Hjønnevåg | 7369345 | 2012-11-26 16:09:13 -0800 | [diff] [blame^] | 142 | struct usb_phy *phy; |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 143 | |
Arve Hjønnevåg | 7369345 | 2012-11-26 16:09:13 -0800 | [diff] [blame^] | 144 | phy = usb_get_phy(USB_PHY_TYPE_USB2); |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 145 | |
Arve Hjønnevåg | 7369345 | 2012-11-26 16:09:13 -0800 | [diff] [blame^] | 146 | if (IS_ERR(phy)) { |
Benoit Goby | 3f9860a | 2012-05-10 16:41:40 -0700 | [diff] [blame] | 147 | pr_err("%s: No USB transceiver found\n", __func__); |
Arve Hjønnevåg | 7369345 | 2012-11-26 16:09:13 -0800 | [diff] [blame^] | 148 | return PTR_ERR(phy); |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 149 | } |
Arve Hjønnevåg | 7369345 | 2012-11-26 16:09:13 -0800 | [diff] [blame^] | 150 | otgwl_xceiv = phy; |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 151 | |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 152 | snprintf(vbus_lock.name, sizeof(vbus_lock.name), "vbus-%s", |
| 153 | dev_name(otgwl_xceiv->dev)); |
| 154 | wake_lock_init(&vbus_lock.wakelock, WAKE_LOCK_SUSPEND, |
| 155 | vbus_lock.name); |
| 156 | |
| 157 | otgwl_nb.notifier_call = otgwl_otg_notifications; |
Benoit Goby | 3f9860a | 2012-05-10 16:41:40 -0700 | [diff] [blame] | 158 | ret = usb_register_notifier(otgwl_xceiv, &otgwl_nb); |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 159 | |
| 160 | if (ret) { |
Benoit Goby | 3f9860a | 2012-05-10 16:41:40 -0700 | [diff] [blame] | 161 | pr_err("%s: usb_register_notifier on transceiver %s" |
Todd Poynor | 2c7e324 | 2011-09-26 20:35:30 -0700 | [diff] [blame] | 162 | " failed\n", __func__, |
| 163 | dev_name(otgwl_xceiv->dev)); |
| 164 | otgwl_xceiv = NULL; |
| 165 | wake_lock_destroy(&vbus_lock.wakelock); |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | otgwl_handle_event(otgwl_xceiv->last_event); |
| 170 | return ret; |
Todd Poynor | 21b36ea | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | late_initcall(otg_wakelock_init); |