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