summaryrefslogtreecommitdiff
path: root/arch/arm/mach-tegra/baseband-xmm-power2.c
blob: dd79986813a8fc0bd8d8b97caa905e7ecd5effab (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * arch/arm/mach-tegra/baseband-xmm-power2.c
 *
 * Copyright (C) 2011 NVIDIA Corporation
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <mach/usb_phy.h>
#include "baseband-xmm-power.h"
#include "board.h"
#include "devices.h"

MODULE_LICENSE("GPL");

static struct baseband_power_platform_data *baseband_power2_driver_data;

static enum {
	IPC_HSIC_SUS_REQ_UNINIT,
	IPC_HSIC_SUS_REQ_IRQ_READY,
	IPC_HSIC_SUS_REQ_INIT,
	IPC_HSIC_SUS_REQ_L,
	IPC_HSIC_SUS_REQ_H,
} ipc_hsic_sus_req_state;

static enum {
	IPC_AP_WAKE_UNINIT,
	IPC_AP_WAKE_IRQ_READY,
	IPC_AP_WAKE_INIT1,
	IPC_AP_WAKE_INIT2,
	IPC_AP_WAKE_L,
	IPC_AP_WAKE_H,
} ipc_ap_wake_state;

static struct workqueue_struct *workqueue;
static struct work_struct init1_work;
static struct work_struct init2_work;

static irqreturn_t ipc_hsic_sus_req_irq(int irq, void *dev_id)
{
	int value;

	pr_debug("%s\n", __func__);

	/* check for platform data */
	if (!baseband_power2_driver_data)
		return IRQ_HANDLED;

	/* IPC_HSIC_SUS_REQ state machine */
	if (ipc_hsic_sus_req_state < IPC_HSIC_SUS_REQ_IRQ_READY) {
		pr_err("%s - spurious irq\n", __func__);
	} else {
		value = gpio_get_value(baseband_power2_driver_data->
			modem.xmm.ipc_hsic_sus_req);
		if (!value) {
			pr_debug("%s - falling\n", __func__);
			ipc_hsic_sus_req_state = IPC_HSIC_SUS_REQ_L;
		} else {
			pr_debug("%s - rising\n", __func__);
			ipc_hsic_sus_req_state = IPC_HSIC_SUS_REQ_H;
		}
	}

	return IRQ_HANDLED;
}

static irqreturn_t ipc_ap_wake_irq(int irq, void *dev_id)
{
	int value;

	pr_debug("%s\n", __func__);

	/* check for platform data */
	if (!baseband_power2_driver_data)
		return IRQ_HANDLED;

	/* IPC_AP_WAKE state machine */
	if (ipc_ap_wake_state < IPC_AP_WAKE_IRQ_READY) {
		pr_err("%s - spurious irq\n", __func__);
	} else if (ipc_ap_wake_state == IPC_AP_WAKE_IRQ_READY) {
		value = gpio_get_value(baseband_power2_driver_data->
			modem.xmm.ipc_ap_wake);
		if (!value) {
			pr_debug("%s - IPC_AP_WAKE_INIT1 - got falling edge\n",
				__func__);
			/* go to IPC_AP_WAKE_INIT1 state */
			ipc_ap_wake_state = IPC_AP_WAKE_INIT1;
			/* queue work */
			queue_work(workqueue, &init1_work);
		} else {
			pr_debug("%s - IPC_AP_WAKE_INIT1 - wait for falling edge\n",
				__func__);
		}
	} else if (ipc_ap_wake_state == IPC_AP_WAKE_INIT1) {
		value = gpio_get_value(baseband_power2_driver_data->
			modem.xmm.ipc_ap_wake);
		if (!value) {
			pr_debug("%s - IPC_AP_WAKE_INIT2 - wait for rising edge\n",
				__func__);
		} else {
			pr_debug("%s - IPC_AP_WAKE_INIT2 - got rising edge\n",
				__func__);
			/* go to IPC_AP_WAKE_INIT2 state */
			ipc_ap_wake_state = IPC_AP_WAKE_INIT2;
			/* queue work */
			queue_work(workqueue, &init2_work);
		}
	} else {
		value = gpio_get_value(baseband_power2_driver_data->
			modem.xmm.ipc_ap_wake);
		if (!value) {
			pr_debug("%s - falling\n", __func__);
			ipc_ap_wake_state = IPC_AP_WAKE_L;
		} else {
			pr_debug("%s - rising\n", __func__);
			ipc_ap_wake_state = IPC_AP_WAKE_H;
		}
	}

	return IRQ_HANDLED;
}

static void baseband_xmm_power2_init1_work(struct work_struct *work)
{
	int value;

	pr_debug("%s {\n", __func__);

	/* check for platform data */
	if (!baseband_power2_driver_data)
		return;

	/* check if IPC_HSIC_ACTIVE high */
	value = gpio_get_value(baseband_power2_driver_data->
		modem.xmm.ipc_hsic_active);
	if (value != 1) {
		pr_err("%s - expected IPC_HSIC_ACTIVE high!\n", __func__);
		return;
	}

	/* wait 30 ms */
	mdelay(30);

	/* set IPC_HSIC_ACTIVE low */
	gpio_set_value(baseband_power2_driver_data->
		modem.xmm.ipc_hsic_active, 0);

	pr_debug("%s }\n", __func__);
}

static void baseband_xmm_power2_init2_work(struct work_struct *work)
{
	int value;

	pr_debug("%s {\n", __func__);

	/* check for platform data */
	if (!baseband_power2_driver_data)
		return;

	/* check if IPC_HSIC_ACTIVE low */
	value = gpio_get_value(baseband_power2_driver_data->
		modem.xmm.ipc_hsic_active);
	if (value != 0) {
		pr_err("%s - expected IPC_HSIC_ACTIVE low!\n", __func__);
		return;
	}

	/* wait 1 ms */
	mdelay(1);

	/* turn on usb host controller */
	{
		mm_segment_t oldfs;
		struct file *filp;
		oldfs = get_fs();
		set_fs(KERNEL_DS);
		filp = filp_open("/sys/devices/platform/tegra-ehci.1/ehci_power", O_RDWR, 0);
		if (!filp) {
			pr_err("open ehci_power failed\n");
		} else {
			filp->f_op->write(filp, "1", 1, &filp->f_pos);
			filp_close(filp, NULL);
		}
		set_fs(oldfs);
	}

	/* set IPC_HSIC_ACTIVE high */
	gpio_set_value(baseband_power2_driver_data->
		modem.xmm.ipc_hsic_active, 1);

	/* wait 20 ms */
	mdelay(20);

	/* set IPC_HSIC_ACTIVE low */
	gpio_set_value(baseband_power2_driver_data->
		modem.xmm.ipc_hsic_active, 0);

	/* wait 20 ms */
	mdelay(20);

	/* set IPC_HSIC_ACTIVE high */
	gpio_set_value(baseband_power2_driver_data->
		modem.xmm.ipc_hsic_active, 1);

	pr_debug("%s }\n", __func__);
}

static int baseband_xmm_power2_driver_probe(struct platform_device *device)
{
	struct baseband_power_platform_data *data
		= (struct baseband_power_platform_data *) device->dev.platform_data;
	int err;

	pr_debug("%s\n", __func__);

	/* save platform data */
	baseband_power2_driver_data = data;

	/* request baseband irq(s) */
	ipc_hsic_sus_req_state = IPC_HSIC_SUS_REQ_UNINIT;
	err = request_irq(gpio_to_irq(data->modem.xmm.ipc_hsic_sus_req),
		ipc_hsic_sus_req_irq,
		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
		"IPC_HSIC_SUS_REQ_IRQ",
		NULL);
	if (err < 0) {
		pr_err("%s - request irq IPC_HSIC_SUS_REQ_IRQ failed\n",
			__func__);
		return err;
	}
	ipc_hsic_sus_req_state = IPC_HSIC_SUS_REQ_IRQ_READY;
	ipc_ap_wake_state = IPC_AP_WAKE_UNINIT;
	err = request_irq(gpio_to_irq(data->modem.xmm.ipc_ap_wake),
		ipc_ap_wake_irq,
		IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
		"IPC_AP_WAKE_IRQ",
		NULL);
	if (err < 0) {
		pr_err("%s - request irq IPC_AP_WAKE_IRQ failed\n",
			__func__);
		return err;
	}
	ipc_ap_wake_state = IPC_AP_WAKE_IRQ_READY;

	/* init work queue */
	workqueue = create_singlethread_workqueue
		("baseband_power_workqueue");
	if (!workqueue) {
		pr_err("cannot create workqueue\n");
		return -1;
	}
	INIT_WORK(&init1_work, baseband_xmm_power2_init1_work);
	INIT_WORK(&init2_work, baseband_xmm_power2_init2_work);

	return 0;
}

static int baseband_xmm_power2_driver_remove(struct platform_device *device)
{
	pr_debug("%s\n", __func__);

	/* check for platform data */
	if (!baseband_power2_driver_data)
		return 0;

	/* free baseband irq(s) */
	free_irq(gpio_to_irq(baseband_power2_driver_data
		->modem.xmm.ipc_ap_wake), NULL);
	free_irq(gpio_to_irq(baseband_power2_driver_data
		->modem.xmm.ipc_hsic_sus_req), NULL);

	return 0;
}

#ifdef CONFIG_PM
static int baseband_xmm_power2_driver_suspend(struct platform_device *device,
	pm_message_t state)
{
	pr_debug("%s\n", __func__);

	/* check for platform data */
	if (!baseband_power2_driver_data)
		return 0;

	/* signal bb to suspend hsic */
	gpio_set_value(baseband_power2_driver_data
		->modem.xmm.ipc_hsic_active, 0);

	return 0;
}

static int baseband_xmm_power2_driver_resume(struct platform_device *device)
{
	pr_debug("%s\n", __func__);

	/* check for platform data */
	if (!baseband_power2_driver_data)
		return 0;

	/* wake bb */
	gpio_set_value(baseband_power2_driver_data
		->modem.xmm.ipc_bb_wake, 1);

	/* signal bb to resume hsic */
	gpio_set_value(baseband_power2_driver_data
		->modem.xmm.ipc_hsic_active, 1);

	return 0;
}
#endif

static struct platform_driver baseband_power2_driver = {
	.probe = baseband_xmm_power2_driver_probe,
	.remove = baseband_xmm_power2_driver_remove,
#ifdef CONFIG_PM
	.suspend = baseband_xmm_power2_driver_suspend,
	.resume = baseband_xmm_power2_driver_resume,
#endif
	.driver = {
		.name = "baseband_xmm_power2",
	},
};

static int __init baseband_xmm_power2_init(void)
{
	pr_debug("%s\n", __func__);
	return platform_driver_register(&baseband_power2_driver);
}

static int __exit baseband_xmm_power2_exit(void)
{
	pr_debug("%s\n", __func__);
	platform_driver_unregister(&baseband_power2_driver);
	return 0;
}

module_init(baseband_xmm_power2_init)
module_exit(baseband_xmm_power2_exit)