9325123c0e9a2f7b8f4aa3157a4ade0b816d89ad
[openwrt/svn-archive/archive.git] / target / linux / s3c24xx / files-2.6.30 / arch / arm / mach-s3c2442 / gta02-pm-gps.c
1 /*
2 * GPS Power Management code for the Openmoko Freerunner GSM Phone
3 *
4 * (C) 2007-2009 by Openmoko Inc.
5 * Author: Harald Welte <laforge@openmoko.org>
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation
11 *
12 */
13
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/platform_device.h>
19
20 #include <mach/hardware.h>
21 #include <mach/cpu.h>
22
23 #include <asm/mach-types.h>
24
25 #include <linux/gta02-shadow.h>
26
27 #include <mach/gta02.h>
28 #include <linux/mfd/pcf50633/core.h>
29 #include <linux/mfd/pcf50633/pmic.h>
30
31 #include <linux/regulator/consumer.h>
32 #include <linux/err.h>
33
34 struct gta02_pm_gps_data {
35 #ifdef CONFIG_PM
36 int keep_on_in_suspend;
37 #endif
38 int power_was_on;
39 struct regulator *regulator;
40 };
41
42 static struct gta02_pm_gps_data gta02_gps;
43
44 int gta02_pm_gps_is_on(void)
45 {
46 return gta02_gps.power_was_on;
47 }
48 EXPORT_SYMBOL_GPL(gta02_pm_gps_is_on);
49
50 /* This is the POWERON pin */
51 static void gps_pwron_set(int on)
52 {
53 if (on) {
54 /* return UART pins to being UART pins */
55 s3c2410_gpio_cfgpin(S3C2410_GPH4, S3C2410_GPH4_TXD1);
56 /* remove pulldown now it won't be floating any more */
57 s3c2410_gpio_pullup(S3C2410_GPH5, 0);
58
59 if (!gta02_gps.power_was_on)
60 regulator_enable(gta02_gps.regulator);
61 } else {
62 /*
63 * take care not to power unpowered GPS from UART TX
64 * return them to GPIO and force low
65 */
66 s3c2410_gpio_cfgpin(S3C2410_GPH4, S3C2410_GPH4_OUTP);
67 s3c2410_gpio_setpin(S3C2410_GPH4, 0);
68 /* don't let RX from unpowered GPS float */
69 s3c2410_gpio_pullup(S3C2410_GPH5, 1);
70 if (gta02_gps.power_was_on)
71 regulator_disable(gta02_gps.regulator);
72 }
73 }
74
75 static int gps_pwron_get(void)
76 {
77 return regulator_is_enabled(gta02_gps.regulator);
78 }
79
80 #ifdef CONFIG_PM
81 /* This is the flag for keeping gps ON during suspend */
82 static void gps_keep_on_in_suspend_set(int on)
83 {
84 gta02_gps.keep_on_in_suspend = on;
85 }
86
87 static int gps_keep_on_in_suspend_get(void)
88 {
89 return gta02_gps.keep_on_in_suspend;
90 }
91 #endif
92
93 static ssize_t power_gps_read(struct device *dev,
94 struct device_attribute *attr, char *buf)
95 {
96 int ret = 0;
97
98 if (!strcmp(attr->attr.name, "power_on") ||
99 !strcmp(attr->attr.name, "pwron")) {
100 ret = gps_pwron_get();
101 #ifdef CONFIG_PM
102 } else if (!strcmp(attr->attr.name, "keep_on_in_suspend")) {
103 ret = gps_keep_on_in_suspend_get();
104 #endif
105 }
106 if (ret)
107 return strlcpy(buf, "1\n", 3);
108 else
109 return strlcpy(buf, "0\n", 3);
110 }
111
112 static ssize_t power_gps_write(struct device *dev,
113 struct device_attribute *attr, const char *buf,
114 size_t count)
115 {
116 unsigned long on = simple_strtoul(buf, NULL, 10);
117
118 if (!strcmp(attr->attr.name, "power_on") ||
119 !strcmp(attr->attr.name, "pwron")) {
120 gps_pwron_set(on);
121 gta02_gps.power_was_on = !!on;
122 #ifdef CONFIG_PM
123 } else if (!strcmp(attr->attr.name, "keep_on_in_suspend")) {
124 gps_keep_on_in_suspend_set(on);
125 #endif
126 }
127 return count;
128 }
129
130 #ifdef CONFIG_PM
131 static int gta02_pm_gps_suspend(struct platform_device *pdev,
132 pm_message_t state)
133 {
134 if (!gta02_gps.keep_on_in_suspend ||
135 !gta02_gps.power_was_on)
136 gps_pwron_set(0);
137 else
138 dev_warn(&pdev->dev, "GTA02: keeping gps ON "
139 "during suspend\n");
140 return 0;
141 }
142
143 static int gta02_pm_gps_resume(struct platform_device *pdev)
144 {
145 if (!gta02_gps.keep_on_in_suspend && gta02_gps.power_was_on)
146 gps_pwron_set(1);
147
148 return 0;
149 }
150
151 static DEVICE_ATTR(keep_on_in_suspend, 0644, power_gps_read, power_gps_write);
152 #else
153 #define gta02_pm_gps_suspend NULL
154 #define gta02_pm_gps_resume NULL
155 #endif
156
157 static DEVICE_ATTR(power_on, 0644, power_gps_read, power_gps_write);
158
159 static struct attribute *gta02_gps_sysfs_entries[] = {
160 &dev_attr_power_on.attr,
161 #ifdef CONFIG_PM
162 &dev_attr_keep_on_in_suspend.attr,
163 #endif
164 NULL
165 };
166
167 static struct attribute_group gta02_gps_attr_group = {
168 .name = NULL,
169 .attrs = gta02_gps_sysfs_entries,
170 };
171
172 static int __init gta02_pm_gps_probe(struct platform_device *pdev)
173 {
174 gta02_gps.regulator = regulator_get(&pdev->dev, "RF_3V");
175 if (IS_ERR(gta02_gps.regulator)) {
176 dev_err(&pdev->dev, "probe failed %ld\n",
177 PTR_ERR(gta02_gps.regulator));
178
179 return PTR_ERR(gta02_gps.regulator);
180 }
181
182 dev_info(&pdev->dev, "starting\n");
183
184 /*
185 * Here we should call the code that handles the set GPS power
186 * off action. But, the regulator API does not allow us to
187 * reassert regulator state, and when we read the regulator API
188 * logical state, it can differ from the actual state, So
189 * a workaround for this is to just set the regulator off in the
190 * PMU directly. Because that's different from normal flow, we
191 * have to reproduce other things from the OFF action here too.
192 */
193
194 /*
195 * u-boot enables LDO5 (GPS), which doesn't make sense and
196 * causes confusion. We therefore disable the regulator here.
197 */
198 pcf50633_reg_write(gta02_pcf, PCF50633_REG_LDO5ENA, 0);
199
200 /*
201 * take care not to power unpowered GPS from UART TX
202 * return them to GPIO and force low
203 */
204 s3c2410_gpio_cfgpin(S3C2410_GPH4, S3C2410_GPH4_OUTP);
205 s3c2410_gpio_setpin(S3C2410_GPH4, 0);
206 /* don't let RX from unpowered GPS float */
207 s3c2410_gpio_pullup(S3C2410_GPH5, 1);
208
209 return sysfs_create_group(&pdev->dev.kobj,
210 &gta02_gps_attr_group);
211 }
212
213 static int gta02_pm_gps_remove(struct platform_device *pdev)
214 {
215 regulator_put(gta02_gps.regulator);
216 sysfs_remove_group(&pdev->dev.kobj, &gta02_gps_attr_group);
217 return 0;
218 }
219
220 static struct platform_driver gta02_pm_gps_driver = {
221 .probe = gta02_pm_gps_probe,
222 .remove = gta02_pm_gps_remove,
223 .suspend = gta02_pm_gps_suspend,
224 .resume = gta02_pm_gps_resume,
225 .driver = {
226 .name = "gta02-pm-gps",
227 },
228 };
229
230 static int __devinit gta02_pm_gps_init(void)
231 {
232 return platform_driver_register(&gta02_pm_gps_driver);
233 }
234
235 static void gta02_pm_gps_exit(void)
236 {
237 platform_driver_unregister(&gta02_pm_gps_driver);
238 }
239
240 module_init(gta02_pm_gps_init);
241 module_exit(gta02_pm_gps_exit);
242
243 MODULE_LICENSE("GPL");
244 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");