summaryrefslogtreecommitdiff
path: root/drivers/misc/tegra-baseband/bb-power.c
blob: f0d40964cdbb05a472ae55846ce40ee5f95c8a2f (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
/*
 * drivers/misc/tegra-baseband/bb-power.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 <linux/platform_data/tegra_usb.h>
#include <mach/usb_phy.h>
#include <mach/tegra-bb-power.h>
#include "bb-power.h"

static int bb_id;
static bool bb_registered;

static bb_init_cb init_cb_list[] = {
	NULL,
	NULL,
	NULL,
	M7400_INIT_CB,
};

static bb_power_cb power_cb_list[] = {
	NULL,
	NULL,
	NULL,
	M7400_PWR_CB,
};

static int tegra_bb_power_gpio_init(struct tegra_bb_power_gdata *gdata)
{
	int ret;
	int irq;
	unsigned gpio_id;
	const char *gpio_label;
	unsigned long gpio_flags;
	struct tegra_bb_gpio_data *gpiolist;
	struct tegra_bb_gpio_irqdata *gpioirq;

	gpiolist = gdata->gpio;
	for (; gpiolist->data.gpio != GPIO_INVALID; ++gpiolist) {
		gpio_id = (gpiolist->data.gpio);
		gpio_label = (gpiolist->data.label);
		gpio_flags = (gpiolist->data.flags);

		/* Request the gpio */
		ret = gpio_request(gpio_id, gpio_label);
		if (ret) {
			pr_err("%s: gpio_request for gpio %d failed.\n",
							 __func__, gpio_id);
			return ret;
		}

		/* Set gpio direction, as requested */
		if (gpio_flags == GPIOF_IN)
			gpio_direction_input(gpio_id);
		else
			gpio_direction_output(gpio_id, (!gpio_flags ? 0 : 1));

		/* Enable the gpio */
		tegra_gpio_enable(gpio_id);

		/* Create a sysfs node, if requested */
		if (gpiolist->doexport)
			gpio_export(gpio_id, false);
	}

	gpioirq = gdata->gpioirq;
	for (; gpioirq->id != GPIO_INVALID; ++gpioirq) {

		/* Create interrupt handler, if requested */
		if (gpioirq->handler != NULL) {
			irq = gpio_to_irq(gpioirq->id);
			ret = request_threaded_irq(irq, NULL, gpioirq->handler,
				gpioirq->flags, gpioirq->name, gpioirq->cookie);
			if (ret < 0) {
				pr_err("%s: request_threaded_irq error\n",
								 __func__);
				return ret;
			}
			ret = enable_irq_wake(irq);
			if (ret) {
				pr_err("%s: enable_irq_wake error\n", __func__);
				return ret;
			}
		}
	}
	return 0;
}

static int tegra_bb_power_gpio_deinit(struct tegra_bb_power_gdata *gdata)
{
	struct tegra_bb_gpio_data *gpiolist;
	struct tegra_bb_gpio_irqdata *gpioirq;

	gpiolist = gdata->gpio;
	for (; gpiolist->data.gpio != GPIO_INVALID; ++gpiolist) {

		/* Free the gpio */
		gpio_free(gpiolist->data.gpio);
	}

	gpioirq = gdata->gpioirq;
	for (; gpioirq->id != GPIO_INVALID; ++gpioirq) {

		/* Free the irq */
		free_irq(gpio_to_irq(gpioirq->id), gpioirq->cookie);
	}
	return 0;
}

static int baseband_l2_suspend(void)
{
	/* BB specific callback */
	if (power_cb_list[bb_id] != NULL)
		power_cb_list[bb_id](CB_CODE_L0L2);
	return 0;
}

static int baseband_l2_resume(void)
{
	/* BB specific callback */
	if (power_cb_list[bb_id] != NULL)
		power_cb_list[bb_id](CB_CODE_L2L0);
	return 0;
}

static ssize_t tegra_bb_attr_write(struct device *dev,
			struct device_attribute *attr,
			const char *buf, size_t count)
{
	struct tegra_bb_pdata *pdata;
	struct tegra_ehci_platform_data *ehci_data;
	struct tegra_uhsic_config *hsic_config;
	int load;

	if (sscanf(buf, "%d", &load) != 1)
		return -EINVAL;

	if (load == 1 && !bb_registered) {
		pdata = (struct tegra_bb_pdata *) dev->platform_data;
		ehci_data = (struct tegra_ehci_platform_data *)
					pdata->device->dev.platform_data;
		hsic_config = (struct tegra_uhsic_config *)
					ehci_data->phy_config;

		/* Register PHY callbacks */
		hsic_config->postsuspend = baseband_l2_suspend;
		hsic_config->preresume = baseband_l2_resume;

		/* Override required settings */
		ehci_data->power_down_on_bus_suspend = 0;

		/* Register the ehci device. */
		platform_device_register(pdata->device);
		bb_registered = true;
	}

	return count;
}

static ssize_t tegra_bb_attr_read(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	int ret = 0;
	return sprintf(buf, "%d", ret);
}

static DEVICE_ATTR(load, S_IRUSR | S_IWUSR | S_IRGRP,
		tegra_bb_attr_read, tegra_bb_attr_write);

static int tegra_bb_power_probe(struct platform_device *device)
{
	struct device *dev = &device->dev;
	struct tegra_bb_pdata *pdata;
	struct tegra_bb_power_gdata *gdata;
	int err;

	pdata = (struct tegra_bb_pdata *) dev->platform_data;
	if (!pdata) {
		pr_err("%s - Error: platform data is empty.\n", __func__);
		return -ENODEV;
	}

	/* BB specific callback */
	bb_id = pdata->bb_id;
	if (init_cb_list[bb_id] != NULL) {
		gdata = (struct tegra_bb_power_gdata *)
		init_cb_list[pdata->bb_id]((void *)pdata, CB_CODE_INIT);

		if (!gdata) {
			pr_err("%s - Error: Gpio data is empty.\n", __func__);
			return -ENODEV;
		}

		/* Initialize gpio as required */
		tegra_bb_power_gpio_init(gdata);
	}

	bb_registered = false;

	/* Create the control sysfs node */
	err = device_create_file(dev, &dev_attr_load);
	if (err < 0) {
		pr_err("%s - device_create_file failed\n", __func__);
		return -ENODEV;
	}

	return 0;
}

static int tegra_bb_power_remove(struct platform_device *device)
{
	struct device *dev = &device->dev;
	struct tegra_bb_pdata *pdata;
	struct tegra_bb_power_gdata *gdata;

	/* BB specific callback */
	if (init_cb_list[bb_id] != NULL) {
		pdata = (struct tegra_bb_pdata *) dev->platform_data;
		gdata = (struct tegra_bb_power_gdata *)
			init_cb_list[bb_id]((void *)pdata, CB_CODE_DEINIT);

		/* Deinitialize gpios */
		if (gdata)
			tegra_bb_power_gpio_deinit(gdata);
	}

	/* Remove the control sysfs node */
	device_remove_file(dev, &dev_attr_load);

	return 0;
}

#ifdef CONFIG_PM
static int tegra_bb_power_suspend(struct platform_device *device,
	pm_message_t state)
{
	/* BB specific callback */
	if (power_cb_list[bb_id] != NULL)
		power_cb_list[bb_id](CB_CODE_L2L3);

	return 0;
}

static int tegra_bb_power_resume(struct platform_device *device)
{
	/* BB specific callback */
	if (power_cb_list[bb_id] != NULL)
		power_cb_list[bb_id](CB_CODE_L3L0);

	return 0;
}
#endif

static struct platform_driver tegra_bb_power_driver = {
	.probe = tegra_bb_power_probe,
	.remove = tegra_bb_power_remove,
#ifdef CONFIG_PM
	.suspend = tegra_bb_power_suspend,
	.resume = tegra_bb_power_resume,
#endif
	.driver = {
		.name = "tegra_baseband_power",
	},
};

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

static void __exit tegra_baseband_power_exit(void)
{
	pr_debug("%s\n", __func__);
	platform_driver_unregister(&tegra_bb_power_driver);
}

module_init(tegra_baseband_power_init)
module_exit(tegra_baseband_power_exit)
MODULE_AUTHOR("NVIDIA Corporation");
MODULE_DESCRIPTION("Tegra modem power management driver");
MODULE_LICENSE("GPL");