blob: ecd741027f53d9357ccb41205a84f046793038e9 [file] [log] [blame]
Todd Poynor21b36ea2011-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>
Arve Hjønnevåg73693452012-11-26 16:09:13 -080019#include <linux/err.h>
Colin Cross666167c2012-02-01 14:23:15 -080020#include <linux/module.h>
Todd Poynor21b36ea2011-07-01 17:19:56 -070021#include <linux/notifier.h>
Todd Poynor21b36ea2011-07-01 17:19:56 -070022#include <linux/spinlock.h>
23#include <linux/usb/otg.h>
24
Todd Poynor2c7e3242011-09-26 20:35:30 -070025#define TEMPORARY_HOLD_TIME 2000
26
Todd Poynor21b36ea2011-07-01 17:19:56 -070027static bool enabled = true;
Benoit Goby3f9860a2012-05-10 16:41:40 -070028static struct usb_phy *otgwl_xceiv;
Todd Poynor21b36ea2011-07-01 17:19:56 -070029static struct notifier_block otgwl_nb;
30
31/*
32 * otgwl_spinlock is held while the VBUS lock is grabbed or dropped and the
Todd Poynor2c7e3242011-09-26 20:35:30 -070033 * held field is updated to match.
Todd Poynor21b36ea2011-07-01 17:19:56 -070034 */
35
36static DEFINE_SPINLOCK(otgwl_spinlock);
37
38/*
39 * Only one lock, but since these 3 fields are associated with each other...
40 */
41
42struct otgwl_lock {
43 char name[40];
Dmitry Shmidt48baaa32017-01-12 12:45:46 -080044 struct wakeup_source wakesrc;
Todd Poynor2c7e3242011-09-26 20:35:30 -070045 bool held;
Todd Poynor21b36ea2011-07-01 17:19:56 -070046};
47
48/*
Todd Poynor2c7e3242011-09-26 20:35:30 -070049 * VBUS present lock. Also used as a timed lock on charger
50 * connect/disconnect and USB host disconnect, to allow the system
51 * to react to the change in power.
Todd Poynor21b36ea2011-07-01 17:19:56 -070052 */
53
54static struct otgwl_lock vbus_lock;
55
Todd Poynor2c7e3242011-09-26 20:35:30 -070056static void otgwl_hold(struct otgwl_lock *lock)
Todd Poynor21b36ea2011-07-01 17:19:56 -070057{
Todd Poynor2c7e3242011-09-26 20:35:30 -070058 if (!lock->held) {
Dmitry Shmidt48baaa32017-01-12 12:45:46 -080059 __pm_stay_awake(&lock->wakesrc);
Todd Poynor2c7e3242011-09-26 20:35:30 -070060 lock->held = true;
Todd Poynor21b36ea2011-07-01 17:19:56 -070061 }
62}
63
Todd Poynor2c7e3242011-09-26 20:35:30 -070064static void otgwl_temporary_hold(struct otgwl_lock *lock)
65{
Dmitry Shmidt48baaa32017-01-12 12:45:46 -080066 __pm_wakeup_event(&lock->wakesrc, TEMPORARY_HOLD_TIME);
Todd Poynor2c7e3242011-09-26 20:35:30 -070067 lock->held = false;
68}
69
Todd Poynor21b36ea2011-07-01 17:19:56 -070070static void otgwl_drop(struct otgwl_lock *lock)
71{
Todd Poynor2c7e3242011-09-26 20:35:30 -070072 if (lock->held) {
Dmitry Shmidt48baaa32017-01-12 12:45:46 -080073 __pm_relax(&lock->wakesrc);
Todd Poynor2c7e3242011-09-26 20:35:30 -070074 lock->held = false;
Todd Poynor21b36ea2011-07-01 17:19:56 -070075 }
76}
77
Todd Poynor2c7e3242011-09-26 20:35:30 -070078static void otgwl_handle_event(unsigned long event)
Todd Poynor21b36ea2011-07-01 17:19:56 -070079{
80 unsigned long irqflags;
81
Todd Poynor21b36ea2011-07-01 17:19:56 -070082 spin_lock_irqsave(&otgwl_spinlock, irqflags);
83
Todd Poynor2c7e3242011-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 Poynor21b36ea2011-07-01 17:19:56 -070090 switch (event) {
91 case USB_EVENT_VBUS:
92 case USB_EVENT_ENUMERATED:
Todd Poynor2c7e3242011-09-26 20:35:30 -070093 otgwl_hold(&vbus_lock);
Todd Poynor21b36ea2011-07-01 17:19:56 -070094 break;
95
96 case USB_EVENT_NONE:
97 case USB_EVENT_ID:
98 case USB_EVENT_CHARGER:
Todd Poynor2c7e3242011-09-26 20:35:30 -070099 otgwl_temporary_hold(&vbus_lock);
Todd Poynor21b36ea2011-07-01 17:19:56 -0700100 break;
101
102 default:
103 break;
104 }
105
106 spin_unlock_irqrestore(&otgwl_spinlock, irqflags);
Todd Poynor2c7e3242011-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 Poynor21b36ea2011-07-01 17:19:56 -0700113 return NOTIFY_OK;
114}
115
Todd Poynor21b36ea2011-07-01 17:19:56 -0700116static int set_enabled(const char *val, const struct kernel_param *kp)
117{
Todd Poynor21b36ea2011-07-01 17:19:56 -0700118 int rv = param_set_bool(val, kp);
119
120 if (rv)
121 return rv;
122
Todd Poynor2c7e3242011-09-26 20:35:30 -0700123 if (otgwl_xceiv)
124 otgwl_handle_event(otgwl_xceiv->last_event);
Todd Poynor21b36ea2011-07-01 17:19:56 -0700125
Todd Poynor21b36ea2011-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 Poynor2c7e3242011-09-26 20:35:30 -0700139 int ret;
Arve Hjønnevåg73693452012-11-26 16:09:13 -0800140 struct usb_phy *phy;
Todd Poynor21b36ea2011-07-01 17:19:56 -0700141
Arve Hjønnevåg73693452012-11-26 16:09:13 -0800142 phy = usb_get_phy(USB_PHY_TYPE_USB2);
Todd Poynor21b36ea2011-07-01 17:19:56 -0700143
Arve Hjønnevåg73693452012-11-26 16:09:13 -0800144 if (IS_ERR(phy)) {
Benoit Goby3f9860a2012-05-10 16:41:40 -0700145 pr_err("%s: No USB transceiver found\n", __func__);
Arve Hjønnevåg73693452012-11-26 16:09:13 -0800146 return PTR_ERR(phy);
Todd Poynor21b36ea2011-07-01 17:19:56 -0700147 }
Arve Hjønnevåg73693452012-11-26 16:09:13 -0800148 otgwl_xceiv = phy;
Todd Poynor21b36ea2011-07-01 17:19:56 -0700149
Todd Poynor2c7e3242011-09-26 20:35:30 -0700150 snprintf(vbus_lock.name, sizeof(vbus_lock.name), "vbus-%s",
151 dev_name(otgwl_xceiv->dev));
Dmitry Shmidt48baaa32017-01-12 12:45:46 -0800152 wakeup_source_init(&vbus_lock.wakesrc, vbus_lock.name);
Todd Poynor2c7e3242011-09-26 20:35:30 -0700153
154 otgwl_nb.notifier_call = otgwl_otg_notifications;
Benoit Goby3f9860a2012-05-10 16:41:40 -0700155 ret = usb_register_notifier(otgwl_xceiv, &otgwl_nb);
Todd Poynor2c7e3242011-09-26 20:35:30 -0700156
157 if (ret) {
Benoit Goby3f9860a2012-05-10 16:41:40 -0700158 pr_err("%s: usb_register_notifier on transceiver %s"
Todd Poynor2c7e3242011-09-26 20:35:30 -0700159 " failed\n", __func__,
160 dev_name(otgwl_xceiv->dev));
161 otgwl_xceiv = NULL;
Dmitry Shmidt48baaa32017-01-12 12:45:46 -0800162 wakeup_source_trash(&vbus_lock.wakesrc);
Todd Poynor2c7e3242011-09-26 20:35:30 -0700163 return ret;
164 }
165
166 otgwl_handle_event(otgwl_xceiv->last_event);
167 return ret;
Todd Poynor21b36ea2011-07-01 17:19:56 -0700168}
169
170late_initcall(otg_wakelock_init);