Todd Poynor | 4727216 | 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> |
| 19 | #include <linux/notifier.h> |
| 20 | #include <linux/wakelock.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/usb/otg.h> |
| 23 | |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 24 | #define TEMPORARY_HOLD_TIME 2000 |
| 25 | |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 26 | static bool enabled = true; |
| 27 | static struct otg_transceiver *otgwl_xceiv; |
| 28 | static struct notifier_block otgwl_nb; |
| 29 | |
| 30 | /* |
| 31 | * otgwl_spinlock is held while the VBUS lock is grabbed or dropped and the |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 32 | * held field is updated to match. |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | static DEFINE_SPINLOCK(otgwl_spinlock); |
| 36 | |
| 37 | /* |
| 38 | * Only one lock, but since these 3 fields are associated with each other... |
| 39 | */ |
| 40 | |
| 41 | struct otgwl_lock { |
| 42 | char name[40]; |
| 43 | struct wake_lock wakelock; |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 44 | bool held; |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 48 | * VBUS present lock. Also used as a timed lock on charger |
| 49 | * connect/disconnect and USB host disconnect, to allow the system |
| 50 | * to react to the change in power. |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 51 | */ |
| 52 | |
| 53 | static struct otgwl_lock vbus_lock; |
| 54 | |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 55 | static void otgwl_hold(struct otgwl_lock *lock) |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 56 | { |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 57 | if (!lock->held) { |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 58 | wake_lock(&lock->wakelock); |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 59 | lock->held = true; |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 63 | static void otgwl_temporary_hold(struct otgwl_lock *lock) |
| 64 | { |
| 65 | wake_lock_timeout(&lock->wakelock, |
| 66 | msecs_to_jiffies(TEMPORARY_HOLD_TIME)); |
| 67 | lock->held = false; |
| 68 | } |
| 69 | |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 70 | static void otgwl_drop(struct otgwl_lock *lock) |
| 71 | { |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 72 | if (lock->held) { |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 73 | wake_unlock(&lock->wakelock); |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 74 | lock->held = false; |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 78 | static void otgwl_handle_event(unsigned long event) |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 79 | { |
| 80 | unsigned long irqflags; |
| 81 | |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 82 | spin_lock_irqsave(&otgwl_spinlock, irqflags); |
| 83 | |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 84 | if (!enabled) { |
| 85 | otgwl_drop(&vbus_lock); |
| 86 | spin_unlock_irqrestore(&otgwl_spinlock, irqflags); |
| 87 | return; |
| 88 | } |
| 89 | |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 90 | switch (event) { |
| 91 | case USB_EVENT_VBUS: |
| 92 | case USB_EVENT_ENUMERATED: |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 93 | otgwl_hold(&vbus_lock); |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 94 | break; |
| 95 | |
| 96 | case USB_EVENT_NONE: |
| 97 | case USB_EVENT_ID: |
| 98 | case USB_EVENT_CHARGER: |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 99 | otgwl_temporary_hold(&vbus_lock); |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 100 | break; |
| 101 | |
| 102 | default: |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | spin_unlock_irqrestore(&otgwl_spinlock, irqflags); |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 107 | } |
| 108 | |
| 109 | static int otgwl_otg_notifications(struct notifier_block *nb, |
| 110 | unsigned long event, void *unused) |
| 111 | { |
| 112 | otgwl_handle_event(event); |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 113 | return NOTIFY_OK; |
| 114 | } |
| 115 | |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 116 | static int set_enabled(const char *val, const struct kernel_param *kp) |
| 117 | { |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 118 | int rv = param_set_bool(val, kp); |
| 119 | |
| 120 | if (rv) |
| 121 | return rv; |
| 122 | |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 123 | if (otgwl_xceiv) |
| 124 | otgwl_handle_event(otgwl_xceiv->last_event); |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 125 | |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static struct kernel_param_ops enabled_param_ops = { |
| 130 | .set = set_enabled, |
| 131 | .get = param_get_bool, |
| 132 | }; |
| 133 | |
| 134 | module_param_cb(enabled, &enabled_param_ops, &enabled, 0644); |
| 135 | MODULE_PARM_DESC(enabled, "enable wakelock when VBUS present"); |
| 136 | |
| 137 | static int __init otg_wakelock_init(void) |
| 138 | { |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 139 | int ret; |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 140 | |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 141 | otgwl_xceiv = otg_get_transceiver(); |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 142 | |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 143 | if (!otgwl_xceiv) { |
| 144 | pr_err("%s: No OTG transceiver found\n", __func__); |
| 145 | return -ENODEV; |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Todd Poynor | 6c8568c | 2011-09-26 20:35:30 -0700 | [diff] [blame^] | 148 | snprintf(vbus_lock.name, sizeof(vbus_lock.name), "vbus-%s", |
| 149 | dev_name(otgwl_xceiv->dev)); |
| 150 | wake_lock_init(&vbus_lock.wakelock, WAKE_LOCK_SUSPEND, |
| 151 | vbus_lock.name); |
| 152 | |
| 153 | otgwl_nb.notifier_call = otgwl_otg_notifications; |
| 154 | ret = otg_register_notifier(otgwl_xceiv, &otgwl_nb); |
| 155 | |
| 156 | if (ret) { |
| 157 | pr_err("%s: otg_register_notifier on transceiver %s" |
| 158 | " failed\n", __func__, |
| 159 | dev_name(otgwl_xceiv->dev)); |
| 160 | otgwl_xceiv = NULL; |
| 161 | wake_lock_destroy(&vbus_lock.wakelock); |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | otgwl_handle_event(otgwl_xceiv->last_event); |
| 166 | return ret; |
Todd Poynor | 4727216 | 2011-07-01 17:19:56 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | late_initcall(otg_wakelock_init); |