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