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