[s3c24xx] Fix freerunner gsm modem pm.
[10.03/openwrt.git] / target / linux / s3c24xx / files-2.6.30 / arch / arm / mach-s3c2442 / gta02-pm-gsm.c
1 /*
2  * GSM Management code for the Openmoko Freerunner GSM Phone
3  *
4  * (C) 2007 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/platform_device.h>
18 #include <linux/console.h>
19 #include <linux/errno.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22
23 #include <mach/gpio.h>
24 #include <asm/mach-types.h>
25
26 #include <mach/hardware.h>
27 #include <mach/cpu.h>
28
29 #include <mach/gta02.h>
30 #include <linux/mfd/pcf50633/gpio.h>
31 #include <mach/regs-gpio.h>
32 #include <mach/regs-gpioj.h>
33 #include <linux/gta02-shadow.h>
34
35 int gta_gsm_interrupts;
36 EXPORT_SYMBOL(gta_gsm_interrupts);
37
38 extern void s3c24xx_serial_console_set_silence(int);
39
40 struct gta02pm_priv {
41         int gpio_ndl_gsm;
42         struct console *con;
43 };
44
45 static struct gta02pm_priv gta02_gsm;
46
47 static struct console *find_s3c24xx_console(void)
48 {
49         struct console *con;
50
51         acquire_console_sem();
52
53         for (con = console_drivers; con; con = con->next) {
54                 if (!strcmp(con->name, "ttySAC"))
55                         break;
56         }
57
58         release_console_sem();
59
60         return con;
61 }
62
63 static ssize_t gsm_read(struct device *dev, struct device_attribute *attr,
64                         char *buf)
65 {
66         if (!strcmp(attr->attr.name, "power_on")) {
67                 if (pcf50633_gpio_get(gta02_pcf, PCF50633_GPIO2))
68                         goto out_1;
69         } else if (!strcmp(attr->attr.name, "download")) {
70                 if (!s3c2410_gpio_getpin(GTA02_GPIO_nDL_GSM))
71                         goto out_1;
72         } else if (!strcmp(attr->attr.name, "flowcontrolled")) {
73                 if (s3c2410_gpio_getcfg(S3C2410_GPH1) == S3C2410_GPIO_OUTPUT)
74                         goto out_1;
75         }
76
77         return strlcpy(buf, "0\n", 3);
78 out_1:
79         return strlcpy(buf, "1\n", 3);
80 }
81
82 static void gsm_on_off(struct device *dev, int on)
83 {
84         if (!on) {
85                 s3c2410_gpio_cfgpin(S3C2410_GPH1, S3C2410_GPIO_INPUT);
86                 s3c2410_gpio_cfgpin(S3C2410_GPH2, S3C2410_GPIO_INPUT);
87
88                 pcf50633_gpio_set(gta02_pcf, PCF50633_GPIO2, 0);
89
90                 if (gta02_gsm.con) {
91                         s3c24xx_serial_console_set_silence(0);
92                         console_start(gta02_gsm.con);
93
94                         dev_dbg(dev, "powered down gta02 GSM, enabling "
95                                         "serial console\n");
96                 }
97
98                 return;
99         }
100
101         if (gta02_gsm.con) {
102                 dev_dbg(dev, "powering up GSM, thus "
103                                 "disconnecting serial console\n");
104
105                 console_stop(gta02_gsm.con);
106                 s3c24xx_serial_console_set_silence(1);
107         }
108
109         /* allow UART to talk to GSM side now we will power it */
110         s3c2410_gpio_cfgpin(S3C2410_GPH1, S3C2410_GPH1_nRTS0);
111         s3c2410_gpio_cfgpin(S3C2410_GPH2, S3C2410_GPH2_TXD0);
112
113         pcf50633_gpio_set(gta02_pcf, PCF50633_GPIO2, 7);
114
115         msleep(100);
116
117         gta02_gpb_setpin(GTA02_GPIO_MODEM_ON, 1);
118         msleep(500);
119         gta02_gpb_setpin(GTA02_GPIO_MODEM_ON, 0);
120
121         /*
122          * workaround for calypso firmware moko10 and earlier,
123          * without this it will leave IRQ line high after
124          * booting
125          */
126         s3c2410_gpio_setpin(S3C2410_GPH1, 1);
127         s3c2410_gpio_cfgpin(S3C2410_GPH1, S3C2410_GPH1_OUTP);
128         msleep(1000);
129         s3c2410_gpio_cfgpin(S3C2410_GPH1, S3C2410_GPH1_nRTS0);
130
131 }
132
133 static ssize_t gsm_write(struct device *dev, struct device_attribute *attr,
134                          const char *buf, size_t count)
135 {
136         unsigned long on = simple_strtoul(buf, NULL, 10);
137
138         if (!strcmp(attr->attr.name, "power_on")) {
139                 gsm_on_off(dev, on);
140
141                 return count;
142         }
143
144         if (!strcmp(attr->attr.name, "download")) {
145                 /*
146                  * the keyboard / buttons driver requests and enables
147                  * the JACK_INSERT IRQ.  We have to take care about
148                  * not enabling and disabling the IRQ when it was
149                  * already in that state or we get "unblanaced IRQ"
150                  * kernel warnings and stack dumps.  So we use the
151                  * copy of the ndl_gsm state to figure out if we should
152                  * enable or disable the jack interrupt
153                  */
154                 if (on) {
155                         if (gta02_gsm.gpio_ndl_gsm)
156                                 disable_irq(gpio_to_irq(
157                                                    GTA02_GPIO_JACK_INSERT));
158                 } else {
159                         if (!gta02_gsm.gpio_ndl_gsm)
160                                 enable_irq(gpio_to_irq(
161                                                    GTA02_GPIO_JACK_INSERT));
162                 }
163
164                 gta02_gsm.gpio_ndl_gsm = !on;
165                 s3c2410_gpio_setpin(GTA02_GPIO_nDL_GSM, !on);
166
167                 return count;
168         }
169
170         if (!strcmp(attr->attr.name, "flowcontrolled")) {
171                 if (on) {
172                         gta_gsm_interrupts = 0;
173                         s3c2410_gpio_setpin(S3C2410_GPH1, 1);
174                         s3c2410_gpio_cfgpin(S3C2410_GPH1, S3C2410_GPH1_OUTP);
175                 } else
176                         s3c2410_gpio_cfgpin(S3C2410_GPH1, S3C2410_GPH1_nRTS0);
177         }
178
179         return count;
180 }
181
182 static DEVICE_ATTR(power_on, 0644, gsm_read, gsm_write);
183 static DEVICE_ATTR(reset, 0644, gsm_read, gsm_write);
184 static DEVICE_ATTR(download, 0644, gsm_read, gsm_write);
185 static DEVICE_ATTR(flowcontrolled, 0644, gsm_read, gsm_write);
186
187 #ifdef CONFIG_PM
188
189 static int gta02_gsm_resume(struct platform_device *pdev);
190 static int gta02_gsm_suspend(struct platform_device *pdev, pm_message_t state)
191 {
192         /* GPIO state is saved/restored by S3C2410 core GPIO driver, so we
193          * don't need to do much here. */
194
195         /* If flowcontrol asserted, abort if GSM already interrupted */
196         if (s3c2410_gpio_getcfg(S3C2410_GPH1) == S3C2410_GPIO_OUTPUT) {
197                 if (gta_gsm_interrupts)
198                         goto busy;
199         }
200
201         /* disable DL GSM to prevent jack_insert becoming 'floating' */
202         s3c2410_gpio_setpin(GTA02_GPIO_nDL_GSM, 1);
203         return 0;
204
205 busy:
206         return -EBUSY;
207 }
208
209 static int
210 gta02_gsm_suspend_late(struct platform_device *pdev, pm_message_t state)
211 {
212         /* Last chance: abort if GSM already interrupted */
213         if (s3c2410_gpio_getcfg(S3C2410_GPH1) == S3C2410_GPIO_OUTPUT) {
214                 if (gta_gsm_interrupts)
215                         return -EBUSY;
216         }
217         return 0;
218 }
219
220 static int gta02_gsm_resume(struct platform_device *pdev)
221 {
222         /* GPIO state is saved/restored by S3C2410 core GPIO driver, so we
223          * don't need to do much here. */
224
225         /* Make sure that the kernel console on the serial port is still
226          * disabled. FIXME: resume ordering race with serial driver! */
227         if (gta02_gsm.con && s3c2410_gpio_getpin(GTA02_GPIO_MODEM_ON))
228                 console_stop(gta02_gsm.con);
229
230         s3c2410_gpio_setpin(GTA02_GPIO_nDL_GSM, gta02_gsm.gpio_ndl_gsm);
231
232         return 0;
233 }
234 #else
235 #define gta02_gsm_suspend       NULL
236 #define gta02_gsm_suspend_late  NULL
237 #define gta02_gsm_resume        NULL
238 #endif /* CONFIG_PM */
239
240 static struct attribute *gta02_gsm_sysfs_entries[] = {
241         &dev_attr_power_on.attr,
242         &dev_attr_reset.attr,
243         &dev_attr_download.attr,
244         &dev_attr_flowcontrolled.attr,
245         NULL
246 };
247
248 static struct attribute_group gta02_gsm_attr_group = {
249         .name   = NULL,
250         .attrs  = gta02_gsm_sysfs_entries,
251 };
252
253 static int __init gta02_gsm_probe(struct platform_device *pdev)
254 {
255         switch (S3C_SYSTEM_REV_ATAG) {
256         case GTA02v1_SYSTEM_REV:
257         case GTA02v2_SYSTEM_REV:
258         case GTA02v3_SYSTEM_REV:
259         case GTA02v4_SYSTEM_REV:
260         case GTA02v5_SYSTEM_REV:
261         case GTA02v6_SYSTEM_REV:
262                 break;
263         default:
264                 dev_warn(&pdev->dev, "Unknown Freerunner Revision 0x%x, "
265                          "some PM features not available!!!\n",
266                          system_rev);
267                 break;
268         }
269
270         gta02_gsm.con = find_s3c24xx_console();
271         if (!gta02_gsm.con)
272                 dev_warn(&pdev->dev,
273                          "cannot find S3C24xx console driver\n");
274
275         /* note that download initially disabled, and enforce that */
276         gta02_gsm.gpio_ndl_gsm = 1;
277         s3c2410_gpio_setpin(GTA02_GPIO_nDL_GSM, 1);
278
279         /* GSM is to be initially off (at boot, or if this module inserted) */
280         gsm_on_off(&pdev->dev, 0);
281
282         return sysfs_create_group(&pdev->dev.kobj, &gta02_gsm_attr_group);
283 }
284
285 static int gta02_gsm_remove(struct platform_device *pdev)
286 {
287         sysfs_remove_group(&pdev->dev.kobj, &gta02_gsm_attr_group);
288
289         return 0;
290 }
291
292 static struct platform_driver gta02_gsm_driver = {
293         .probe          = gta02_gsm_probe,
294         .remove         = gta02_gsm_remove,
295         .suspend        = gta02_gsm_suspend,
296         .suspend_late   = gta02_gsm_suspend_late,
297         .resume         = gta02_gsm_resume,
298         .driver         = {
299                 .name           = "gta02-pm-gsm",
300         },
301 };
302
303 static int __devinit gta02_gsm_init(void)
304 {
305         return platform_driver_register(&gta02_gsm_driver);
306 }
307
308 static void gta02_gsm_exit(void)
309 {
310         platform_driver_unregister(&gta02_gsm_driver);
311 }
312
313 module_init(gta02_gsm_init);
314 module_exit(gta02_gsm_exit);
315
316 MODULE_LICENSE("GPL");
317 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
318 MODULE_DESCRIPTION("Openmoko Freerunner GSM Power Management");