aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-s3c6410/om-gta03-features.c
blob: 0fc59c9b0b5a7fd84f96aba1b7435f41f8ae70cf (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
/*
 * Support for features of Openmoko GTA03
 *
 * (C) 2008 by Openmoko Inc.
 * Author: Andy Green <andy@openmoko.com>
 * All rights reserved.
 *
 * Somewhat based on the GTA01 / 02 neo1973_pm_ stuff mainly by Harald Welte
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/platform_device.h>

#include <mach/hardware.h>
#include <mach/om-gta03.h>
#include <asm/mach-types.h>

#include <linux/regulator/consumer.h>
#include <linux/mfd/pcf50633/core.h>
#include <linux/mfd/pcf50633/gpio.h>

#include <plat/gpio-cfg.h>

struct om_gta03_features_data {
	int gps_power_was_on;
	struct regulator *gps_regulator;

	int wlan_bt_power_was_on;

	int gsm_power_was_on;

	int usbhost_power_was_on;

};

static struct om_gta03_features_data om_gta03_features;

/* GPS power */

static void om_gta03_gps_pwron_set(int on)
{
	if ((on) && (!om_gta03_features.gps_power_was_on)) {
		regulator_enable(om_gta03_features.gps_regulator);
		/* enable LNA */
		gpio_direction_output(GTA03_GPIO_GPS_LNA_EN, 1);
	}

	if ((!on) && (om_gta03_features.gps_power_was_on)) {
		/* disable LNA */
		gpio_direction_output(GTA03_GPIO_GPS_LNA_EN, 0);
		regulator_disable(om_gta03_features.gps_regulator);
	}
}

static ssize_t om_gta03_gps_power_read(struct device *dev,
				       struct device_attribute *attr, char *buf)
{
	if (regulator_is_enabled(om_gta03_features.gps_regulator))
		return strlcpy(buf, "1\n", 3);
	else
		return strlcpy(buf, "0\n", 3);
}

static ssize_t om_gta03_gps_power_write(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	int on = !!simple_strtoul(buf, NULL, 10);

	om_gta03_gps_pwron_set(on);
	om_gta03_features.gps_power_was_on = on;

	return count;
}

static DEVICE_ATTR(gps_power, 0644, om_gta03_gps_power_read,
						      om_gta03_gps_power_write);

/* WLAN / BT power */

static void om_gta03_wlan_bt_pwron_set(int on)
{
	if (!on) {
		/* remove power from WLAN / BT module */
		gpio_direction_output(GTA03_GPIO_NWLAN_POWER, 1);
		s3c_gpio_setpull(GTA03_GPIO_NWLAN_POWER, S3C_GPIO_PULL_NONE);
		s3c_gpio_cfgpin(GTA03_GPIO_NWLAN_POWER, S3C_GPIO_SFN(1));

		return;
	}

	/* give power to WLAN / BT module */
	s3c_gpio_setpull(GTA03_GPIO_WLAN_RESET, S3C_GPIO_PULL_NONE);
	s3c_gpio_cfgpin(GTA03_GPIO_WLAN_RESET, S3C_GPIO_SFN(1));
	gpio_direction_output(GTA03_GPIO_WLAN_RESET, 0);

	gpio_direction_output(GTA03_GPIO_NWLAN_POWER, 0);
	s3c_gpio_setpull(GTA03_GPIO_NWLAN_POWER, S3C_GPIO_PULL_NONE);
	s3c_gpio_cfgpin(GTA03_GPIO_NWLAN_POWER, S3C_GPIO_SFN(1));
	msleep(1);
	gpio_direction_output(GTA03_GPIO_WLAN_RESET, 1);
}

static ssize_t om_gta03_wlan_bt_power_read(struct device *dev,
				       struct device_attribute *attr, char *buf)
{
	if (om_gta03_features.wlan_bt_power_was_on)
		return strlcpy(buf, "1\n", 3);
	else
		return strlcpy(buf, "0\n", 3);
}

static ssize_t om_gta03_wlan_bt_power_write(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	om_gta03_wlan_bt_pwron_set(simple_strtoul(buf, NULL, 10));

	return count;
}

static DEVICE_ATTR(wlan_bt_power, 0644, om_gta03_wlan_bt_power_read,
						  om_gta03_wlan_bt_power_write);


/* GSM Module on / off */

static void om_gta03_gsm_pwron_set(int on)
{
	if (!on) {
		/* remove power from WLAN / BT module */
		gpio_direction_output(GTA03_GPIO_MODEN_ON, 0);
		s3c_gpio_setpull(GTA03_GPIO_MODEN_ON, S3C_GPIO_PULL_NONE);
		s3c_gpio_cfgpin(GTA03_GPIO_MODEN_ON, S3C_GPIO_SFN(1));

		return;
	}

	/* give power to GSM module */
	s3c_gpio_setpull(GTA03_GPIO_N_MODEM_RESET, S3C_GPIO_PULL_NONE);
	s3c_gpio_cfgpin(GTA03_GPIO_N_MODEM_RESET, S3C_GPIO_SFN(1));
	gpio_direction_output(GTA03_GPIO_N_MODEM_RESET, 0);

	gpio_direction_output(GTA03_GPIO_MODEN_ON, 1);
	s3c_gpio_setpull(GTA03_GPIO_MODEN_ON, S3C_GPIO_PULL_NONE);
	s3c_gpio_cfgpin(GTA03_GPIO_MODEN_ON, S3C_GPIO_SFN(1));
	msleep(1);
	gpio_direction_output(GTA03_GPIO_N_MODEM_RESET, 1);
}

static ssize_t om_gta03_gsm_power_read(struct device *dev,
				       struct device_attribute *attr, char *buf)
{
	if (om_gta03_features.gsm_power_was_on)
		return strlcpy(buf, "1\n", 3);
	else
		return strlcpy(buf, "0\n", 3);
}

static ssize_t om_gta03_gsm_power_write(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	int on = !!simple_strtoul(buf, NULL, 10);

	om_gta03_gsm_pwron_set(on);
	om_gta03_features.gsm_power_was_on = on;

	return count;
}

static DEVICE_ATTR(gsm_power, 0644, om_gta03_gsm_power_read,
						  om_gta03_gsm_power_write);


/* USB host power on / off */

static void om_gta03_usbhost_pwron_set(int on)
{
	pcf50633_gpio_set(om_gta03_pcf_pdata.pcf, PCF50633_GPO, on);
}

static ssize_t om_gta03_usbhost_power_read(struct device *dev,
				       struct device_attribute *attr, char *buf)
{
	if (pcf50633_gpio_get(om_gta03_pcf_pdata.pcf, PCF50633_GPO))
		return strlcpy(buf, "1\n", 3);
	else
		return strlcpy(buf, "0\n", 3);
}

static ssize_t om_gta03_usbhost_power_write(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	int on = !!simple_strtoul(buf, NULL, 10);

	om_gta03_usbhost_pwron_set(on);
	om_gta03_features.usbhost_power_was_on = on;

	return count;
}

static DEVICE_ATTR(usbhost_power, 0644, om_gta03_usbhost_power_read,
						  om_gta03_usbhost_power_write);




static struct attribute *om_gta03_features_sysfs_entries[] = {
	&dev_attr_gps_power.attr,
	&dev_attr_wlan_bt_power.attr,
	&dev_attr_gsm_power.attr,
	&dev_attr_usbhost_power.attr,
	NULL
};

static struct attribute_group om_gta03_features_attr_group = {
	.name	= NULL,
	.attrs	= om_gta03_features_sysfs_entries,
};

static int __init om_gta03_features_probe(struct platform_device *pdev)
{
	om_gta03_features.gps_regulator = regulator_get(&pdev->dev, "RF_3V");
	dev_info(&pdev->dev, "starting\n");

	om_gta03_features.wlan_bt_power_was_on = 1; /* defaults to on */

	return sysfs_create_group(&pdev->dev.kobj, &om_gta03_features_attr_group);
}

static int om_gta03_features_remove(struct platform_device *pdev)
{

	regulator_put(om_gta03_features.gps_regulator);
	sysfs_remove_group(&pdev->dev.kobj, &om_gta03_features_attr_group);

	return 0;
}


#ifdef CONFIG_PM
static int om_gta03_features_suspend(struct platform_device *pdev,
				     pm_message_t state)
{
	om_gta03_gps_pwron_set(0);
	om_gta03_wlan_bt_pwron_set(0);
	om_gta03_usbhost_pwron_set(0);

	return 0;
}

static int om_gta03_features_resume(struct platform_device *pdev)
{
	if (om_gta03_features.gps_power_was_on)
		om_gta03_gps_pwron_set(1);

	if (om_gta03_features.wlan_bt_power_was_on)
		om_gta03_wlan_bt_pwron_set(1);

	if (om_gta03_features.usbhost_power_was_on)
		om_gta03_usbhost_pwron_set(1);

	return 0;
}
#else
#define om_gta03_features_suspend	NULL
#define om_gta03_features_resume	NULL
#endif

static struct platform_driver om_gta03_features_driver = {
	.probe		= om_gta03_features_probe,
	.remove		= om_gta03_features_remove,
	.suspend	= om_gta03_features_suspend,
	.resume		= om_gta03_features_resume,
	.driver		= {
		.name		= "om-gta03",
	},
};

static int __devinit om_gta03_features_init(void)
{
	return platform_driver_register(&om_gta03_features_driver);
}

static void om_gta03_features_exit(void)
{
	platform_driver_unregister(&om_gta03_features_driver);
}

module_init(om_gta03_features_init);
module_exit(om_gta03_features_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy Green <andy@openmoko.com>");
MODULE_DESCRIPTION("Openmoko GTA03 Feature Driver");