blob: 2f11472dd2b3fd2bed1e664f266946e1a40b3215 [file] [log] [blame]
Todd Poynor47272162011-07-01 17:19:56 -07001/*
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 Poynor6c8568c2011-09-26 20:35:30 -070024#define TEMPORARY_HOLD_TIME 2000
25
Todd Poynor47272162011-07-01 17:19:56 -070026static bool enabled = true;
27static struct otg_transceiver *otgwl_xceiv;
28static struct notifier_block otgwl_nb;
29
30/*
31 * otgwl_spinlock is held while the VBUS lock is grabbed or dropped and the
Todd Poynor6c8568c2011-09-26 20:35:30 -070032 * held field is updated to match.
Todd Poynor47272162011-07-01 17:19:56 -070033 */
34
35static DEFINE_SPINLOCK(otgwl_spinlock);
36
37/*
38 * Only one lock, but since these 3 fields are associated with each other...
39 */
40
41struct otgwl_lock {
42 char name[40];
43 struct wake_lock wakelock;
Todd Poynor6c8568c2011-09-26 20:35:30 -070044 bool held;
Todd Poynor47272162011-07-01 17:19:56 -070045};
46
47/*
Todd Poynor6c8568c2011-09-26 20:35:30 -070048 * 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 Poynor47272162011-07-01 17:19:56 -070051 */
52
53static struct otgwl_lock vbus_lock;
54
Todd Poynor6c8568c2011-09-26 20:35:30 -070055static void otgwl_hold(struct otgwl_lock *lock)
Todd Poynor47272162011-07-01 17:19:56 -070056{
Todd Poynor6c8568c2011-09-26 20:35:30 -070057 if (!lock->held) {
Todd Poynor47272162011-07-01 17:19:56 -070058 wake_lock(&lock->wakelock);
Todd Poynor6c8568c2011-09-26 20:35:30 -070059 lock->held = true;
Todd Poynor47272162011-07-01 17:19:56 -070060 }
61}
62
Todd Poynor6c8568c2011-09-26 20:35:30 -070063static 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 Poynor47272162011-07-01 17:19:56 -070070static void otgwl_drop(struct otgwl_lock *lock)
71{
Todd Poynor6c8568c2011-09-26 20:35:30 -070072 if (lock->held) {
Todd Poynor47272162011-07-01 17:19:56 -070073 wake_unlock(&lock->wakelock);
Todd Poynor6c8568c2011-09-26 20:35:30 -070074 lock->held = false;
Todd Poynor47272162011-07-01 17:19:56 -070075 }
76}
77
Todd Poynor6c8568c2011-09-26 20:35:30 -070078static void otgwl_handle_event(unsigned long event)
Todd Poynor47272162011-07-01 17:19:56 -070079{
80 unsigned long irqflags;
81
Todd Poynor47272162011-07-01 17:19:56 -070082 spin_lock_irqsave(&otgwl_spinlock, irqflags);
83
Todd Poynor6c8568c2011-09-26 20:35:30 -070084 if (!enabled) {
85 otgwl_drop(&vbus_lock);
86 spin_unlock_irqrestore(&otgwl_spinlock, irqflags);
87 return;
88 }
89
Todd Poynor47272162011-07-01 17:19:56 -070090 switch (event) {
91 case USB_EVENT_VBUS:
92 case USB_EVENT_ENUMERATED:
Todd Poynor6c8568c2011-09-26 20:35:30 -070093 otgwl_hold(&vbus_lock);
Todd Poynor47272162011-07-01 17:19:56 -070094 break;
95
96 case USB_EVENT_NONE:
97 case USB_EVENT_ID:
98 case USB_EVENT_CHARGER:
Todd Poynor6c8568c2011-09-26 20:35:30 -070099 otgwl_temporary_hold(&vbus_lock);
Todd Poynor47272162011-07-01 17:19:56 -0700100 break;
101
102 default:
103 break;
104 }
105
106 spin_unlock_irqrestore(&otgwl_spinlock, irqflags);
Todd Poynor6c8568c2011-09-26 20:35:30 -0700107}
108
109static int otgwl_otg_notifications(struct notifier_block *nb,
110 unsigned long event, void *unused)
111{
112 otgwl_handle_event(event);
Todd Poynor47272162011-07-01 17:19:56 -0700113 return NOTIFY_OK;
114}
115
Todd Poynor47272162011-07-01 17:19:56 -0700116static int set_enabled(const char *val, const struct kernel_param *kp)
117{
Todd Poynor47272162011-07-01 17:19:56 -0700118 int rv = param_set_bool(val, kp);
119
120 if (rv)
121 return rv;
122
Todd Poynor6c8568c2011-09-26 20:35:30 -0700123 if (otgwl_xceiv)
124 otgwl_handle_event(otgwl_xceiv->last_event);
Todd Poynor47272162011-07-01 17:19:56 -0700125
Todd Poynor47272162011-07-01 17:19:56 -0700126 return 0;
127}
128
129static struct kernel_param_ops enabled_param_ops = {
130 .set = set_enabled,
131 .get = param_get_bool,
132};
133
134module_param_cb(enabled, &enabled_param_ops, &enabled, 0644);
135MODULE_PARM_DESC(enabled, "enable wakelock when VBUS present");
136
137static int __init otg_wakelock_init(void)
138{
Todd Poynor6c8568c2011-09-26 20:35:30 -0700139 int ret;
Todd Poynor47272162011-07-01 17:19:56 -0700140
Todd Poynor6c8568c2011-09-26 20:35:30 -0700141 otgwl_xceiv = otg_get_transceiver();
Todd Poynor47272162011-07-01 17:19:56 -0700142
Todd Poynor6c8568c2011-09-26 20:35:30 -0700143 if (!otgwl_xceiv) {
144 pr_err("%s: No OTG transceiver found\n", __func__);
145 return -ENODEV;
Todd Poynor47272162011-07-01 17:19:56 -0700146 }
147
Todd Poynor6c8568c2011-09-26 20:35:30 -0700148 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 Poynor47272162011-07-01 17:19:56 -0700167}
168
169late_initcall(otg_wakelock_init);