aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/qcom_liquid_dock.c
blob: 0c4aadf90e2ee8c811f090e72cbe9d4389b26074 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/notifier.h>
#include <linux/platform_device.h>
#include <linux/pm_wakeup.h>
#include <linux/workqueue.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
#include <soc/qcom/liquid_dock.h>

static int docked;
static BLOCKING_NOTIFIER_HEAD(dock_notifier_list);

/**
 * register_liquid_dock_notify - register dock notifier callback
 * @nb: pointer to the notifier block for the callback events.
 *
 * Calls the notifier callback to when dock is inserted or removed, indicated
 * by a boolean passed to the callback's action parameter.
 */
void register_liquid_dock_notify(struct notifier_block *nb)
{
	if (!nb)
		return;

	/* inform new client of current state */
	if (nb->notifier_call)
		nb->notifier_call(nb, docked, NULL);

	blocking_notifier_chain_register(&dock_notifier_list, nb);
}
EXPORT_SYMBOL(register_liquid_dock_notify);

/**
 * unregister_liquid_dock_notify - unregister a notifier callback
 * @nb: pointer to the notifier block for the callback events.
 *
 * register_liquid_dock_notify() must have been previously called for this
 * function to work properly.
 */
void unregister_liquid_dock_notify(struct notifier_block *nb)
{
	blocking_notifier_chain_unregister(&dock_notifier_list, nb);
}
EXPORT_SYMBOL(unregister_liquid_dock_notify);

struct liquid_dock {
	struct device		*dev;
	struct work_struct	dock_work;
	int			dock_detect;
	int			dock_hub_reset;
	int			dock_eth_reset;
	int			dock_enable;
	struct platform_device	*usb3_pdev;
};

static void dock_detected_work(struct work_struct *w)
{
	struct liquid_dock *dock = container_of(w, struct liquid_dock,
						 dock_work);
	docked = gpio_get_value(dock->dock_detect);

	if (dock->dock_enable)
		gpio_direction_output(dock->dock_enable, 0);

	if (docked) {
		/* assert RESETs before turning on power */
		if (dock->dock_hub_reset >= 0)
			gpio_direction_output(dock->dock_hub_reset, 1);
		if (dock->dock_eth_reset >= 0)
			gpio_direction_output(dock->dock_eth_reset, 1);

		if (device_attach(&dock->usb3_pdev->dev) != 1) {
			dev_err(dock->dev, "Could not add USB driver 0x%p\n",
				dock->usb3_pdev);
			return;
		}

		if (dock->dock_enable)
			gpio_direction_output(dock->dock_enable, 1);

		msleep(20); /* short delay before de-asserting RESETs */
		if (dock->dock_hub_reset >= 0)
			gpio_direction_output(dock->dock_hub_reset, 0);
		if (dock->dock_eth_reset >= 0)
			gpio_direction_output(dock->dock_eth_reset, 0);
	} else {
		device_release_driver(&dock->usb3_pdev->dev);
	}

	blocking_notifier_call_chain(&dock_notifier_list, docked, NULL);

	/* Allow system suspend */
	pm_relax(dock->dev);
}

static irqreturn_t dock_detected(int irq, void *data)
{
	struct liquid_dock *dock = data;

	/* Ensure suspend can't happen until after work function commpletes */
	pm_stay_awake(dock->dev);
	schedule_work(&dock->dock_work);
	return IRQ_HANDLED;
}

static int liquid_dock_probe(struct platform_device *pdev)
{
	struct device_node *node = pdev->dev.of_node;
	struct device_node *usb3_node;
	struct liquid_dock *dock;
	int ret;

	dock = devm_kzalloc(&pdev->dev, sizeof(*dock), GFP_KERNEL);
	if (!dock)
		return -ENOMEM;

	dock->dev = &pdev->dev;
	platform_set_drvdata(pdev, dock);
	INIT_WORK(&dock->dock_work, dock_detected_work);

	dock->dock_detect = of_get_named_gpio(node, "qcom,dock-detect-gpio", 0);
	if (dock->dock_detect < 0) {
		dev_err(dock->dev, "unable to get dock-detect-gpio\n");
		return dock->dock_detect;
	}

	ret = devm_gpio_request(dock->dev, dock->dock_detect, "dock_detect");
	if (ret)
		return ret;

	ret = devm_request_irq(&pdev->dev, gpio_to_irq(dock->dock_detect),
				dock_detected, IRQF_TRIGGER_RISING |
				IRQF_TRIGGER_FALLING | IRQF_SHARED,
				"dock_detect_irq", dock);
	if (ret)
		return ret;

	dock->dock_hub_reset = of_get_named_gpio(node,
						 "qcom,dock-hub-reset-gpio", 0);
	if (dock->dock_hub_reset < 0) {
		dev_err(dock->dev, "unable to get dock-hub-reset-gpio\n");
	} else {
	    ret = devm_gpio_request(dock->dev, dock->dock_hub_reset,
				    "dock_hub_reset");
	    if (ret)
			return ret;
	}

	dock->dock_eth_reset = of_get_named_gpio(node,
						 "qcom,dock-eth-reset-gpio", 0);
	if (dock->dock_eth_reset < 0) {
		dev_err(dock->dev, "unable to get dock-eth-reset-gpio\n");
	} else {
	    ret = devm_gpio_request(dock->dev, dock->dock_eth_reset,
				    "dock_eth_reset");
	    if (ret)
			return ret;
	}



	dock->dock_enable = of_get_named_gpio(node, "qcom,dock-enable-gpio", 0);
	if (dock->dock_enable < 0) { /* optional */
		dock->dock_enable = 0;
	} else {
		ret = devm_gpio_request(dock->dev, dock->dock_enable,
					"dock_enable");
		if (ret)
			return ret;
	}

	usb3_node = of_parse_phandle(node, "qcom,usb-host", 0);
	if (!usb3_node) {
		dev_err(dock->dev, "unable to get usb-host\n");
		return -ENODEV;
	}

	dock->usb3_pdev = of_find_device_by_node(usb3_node);
	if (!dock->usb3_pdev || !dock->usb3_pdev->dev.driver) {
		dev_dbg(dock->dev, "usb pdev not ready\n");
		of_node_put(usb3_node);
		return -EPROBE_DEFER;
	}

	of_node_put(usb3_node);
	schedule_work(&dock->dock_work);
	device_init_wakeup(dock->dev, true);
	enable_irq_wake(gpio_to_irq(dock->dock_detect));

	return 0;
}

static int liquid_dock_remove(struct platform_device *pdev)
{
	struct liquid_dock *dock = platform_get_drvdata(pdev);

	disable_irq_wake(gpio_to_irq(dock->dock_detect));
	cancel_work_sync(&dock->dock_work);
	platform_device_put(dock->usb3_pdev);

	return 0;
}

static struct of_device_id of_match_table[] = {
	{ .compatible = "qcom,liquid-dock", },
	{ .compatible = "qcom,apq8084-dock", },
	{ },
};

static struct platform_driver liquid_dock_driver = {
	.driver         = {
		.name   = "liquid-dock-driver",
		.of_match_table = of_match_table,
	},
	.probe          = liquid_dock_probe,
	.remove		= liquid_dock_remove,
};

module_platform_driver(liquid_dock_driver);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("QTI LiQUID Docking Station driver");