[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-bt.c
1 /*
2  * Bluetooth PM 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/rfkill.h>
19 #include <linux/err.h>
20 #include <mach/gpio-fns.h>
21
22 #include <mach/hardware.h>
23 #include <asm/mach-types.h>
24
25 #include <mach/gta02.h>
26 #include <linux/mfd/pcf50633/gpio.h>
27
28 #include <linux/regulator/consumer.h>
29
30 #define DRVMSG "Openmoko Freerunner Bluetooth Power Management"
31
32 struct gta02_pm_bt_data {
33         struct regulator *regulator;
34         struct rfkill *rfkill;
35         int pre_resume_state;
36 };
37
38 static ssize_t bt_read(struct device *dev, struct device_attribute *attr,
39                        char *buf)
40 {
41         int ret = 0;
42         if (!strcmp(attr->attr.name, "power_on")) {
43                 if (s3c2410_gpio_getpin(GTA02_GPIO_BT_EN))
44                         ret = 1;
45         } else if (!strcmp(attr->attr.name, "reset")) {
46                 if (s3c2410_gpio_getpin(GTA02_GPIO_BT_EN) == 0)
47                         ret = 1;
48         }
49
50         if (!ret) {
51                 return strlcpy(buf, "0\n", 3);
52         } else {
53                 return strlcpy(buf, "1\n", 3);
54         }
55 }
56
57 static void __gta02_pm_bt_toggle_radio(struct device *dev, unsigned int on)
58 {
59         struct gta02_pm_bt_data *bt_data = dev_get_drvdata(dev);
60
61         dev_info(dev, "__gta02_pm_bt_toggle_radio %d\n", on);
62
63         bt_data = dev_get_drvdata(dev);
64
65         s3c2410_gpio_setpin(GTA02_GPIO_BT_EN, !on);
66
67         if (on) {
68                 if (!regulator_is_enabled(bt_data->regulator))
69                         regulator_enable(bt_data->regulator);
70         } else {
71                 if (regulator_is_enabled(bt_data->regulator))
72                         regulator_disable(bt_data->regulator);
73         }
74
75         s3c2410_gpio_setpin(GTA02_GPIO_BT_EN, on);
76 }
77
78
79 static int bt_rfkill_set_block(void *data, bool blocked)
80 {
81         struct device *dev = data;
82
83         __gta02_pm_bt_toggle_radio(dev, !blocked);
84
85         return 0;
86 }
87
88 static const struct rfkill_ops gta02_bt_rfkill_ops = {
89        .set_block = bt_rfkill_set_block,
90 };
91
92
93 static ssize_t bt_write(struct device *dev, struct device_attribute *attr,
94                         const char *buf, size_t count)
95 {
96         unsigned long on = simple_strtoul(buf, NULL, 10);
97         struct gta02_pm_bt_data *bt_data = dev_get_drvdata(dev);
98
99         if (!strcmp(attr->attr.name, "power_on")) {
100                 rfkill_set_sw_state(bt_data->rfkill, on ? 1 : 0);
101
102                 __gta02_pm_bt_toggle_radio(dev, on);
103         } else if (!strcmp(attr->attr.name, "reset")) {
104                 /* reset is low-active, so we need to invert */
105                 s3c2410_gpio_setpin(GTA02_GPIO_BT_EN, on ? 0 : 1);
106         }
107
108         return count;
109 }
110
111 static DEVICE_ATTR(power_on, 0644, bt_read, bt_write);
112 static DEVICE_ATTR(reset, 0644, bt_read, bt_write);
113
114 #ifdef CONFIG_PM
115 static int gta02_bt_suspend(struct platform_device *pdev, pm_message_t state)
116 {
117         struct gta02_pm_bt_data *bt_data = dev_get_drvdata(&pdev->dev);
118
119         dev_dbg(&pdev->dev, DRVMSG ": suspending\n");
120
121         bt_data->pre_resume_state = s3c2410_gpio_getpin(GTA02_GPIO_BT_EN);
122         __gta02_pm_bt_toggle_radio(&pdev->dev, 0);
123
124         return 0;
125 }
126
127 static int gta02_bt_resume(struct platform_device *pdev)
128 {
129         struct gta02_pm_bt_data *bt_data = dev_get_drvdata(&pdev->dev);
130         dev_dbg(&pdev->dev, DRVMSG ": resuming\n");
131
132         __gta02_pm_bt_toggle_radio(&pdev->dev, bt_data->pre_resume_state);
133         return 0;
134 }
135 #else
136 #define gta02_bt_suspend        NULL
137 #define gta02_bt_resume         NULL
138 #endif
139
140 static struct attribute *gta02_bt_sysfs_entries[] = {
141         &dev_attr_power_on.attr,
142         &dev_attr_reset.attr,
143         NULL
144 };
145
146 static struct attribute_group gta02_bt_attr_group = {
147         .name   = NULL,
148         .attrs  = gta02_bt_sysfs_entries,
149 };
150
151 static int __init gta02_bt_probe(struct platform_device *pdev)
152 {
153         struct rfkill *rfkill;
154         struct regulator *regulator;
155         struct gta02_pm_bt_data *bt_data;
156         int ret;
157
158         dev_info(&pdev->dev, DRVMSG ": starting\n");
159
160         bt_data = kzalloc(sizeof(*bt_data), GFP_KERNEL);
161         dev_set_drvdata(&pdev->dev, bt_data);
162
163         regulator = regulator_get(&pdev->dev, "BT_3V2");
164         if (IS_ERR(regulator))
165                 return -ENODEV;
166
167         bt_data->regulator = regulator;
168
169         /* this tests the true physical state of the regulator... */
170         if (regulator_is_enabled(regulator)) {
171                 /*
172                  * but these only operate on the logical state of the
173                  * regulator... so we need to logicaly "adopt" it on
174                  * to turn it off
175                  */
176                 regulator_enable(regulator);
177                 regulator_disable(regulator);
178         }
179
180         /* we pull reset to low to make sure that the chip doesn't
181          * drain power through the reset line */
182         s3c2410_gpio_setpin(GTA02_GPIO_BT_EN, 0);
183
184         rfkill = rfkill_alloc(pdev->name, &pdev->dev, RFKILL_TYPE_BLUETOOTH,
185                             &gta02_bt_rfkill_ops, &pdev->dev);
186
187         if (!rfkill) {
188                 dev_err(&pdev->dev, "Failed to allocate rfkill\n");
189                 return -ENOMEM;
190         }
191
192     rfkill_init_sw_state(rfkill, 0);
193
194         ret = rfkill_register(rfkill);
195         if (ret) {
196                 rfkill_destroy(rfkill);
197                 dev_err(&pdev->dev, "Failed to register rfkill\n");
198                 return ret;
199         }
200
201         bt_data->rfkill = rfkill;
202
203         return sysfs_create_group(&pdev->dev.kobj, &gta02_bt_attr_group);
204 }
205
206 static int gta02_bt_remove(struct platform_device *pdev)
207 {
208         struct gta02_pm_bt_data *bt_data = dev_get_drvdata(&pdev->dev);
209         struct regulator *regulator;
210
211         sysfs_remove_group(&pdev->dev.kobj, &gta02_bt_attr_group);
212
213         if (bt_data->rfkill) {
214                 rfkill_destroy(bt_data->rfkill);
215         }
216
217         if (!bt_data || !bt_data->regulator)
218                 return 0;
219
220         regulator = bt_data->regulator;
221
222         /* Make sure regulator is disabled before calling regulator_put */
223         if (regulator_is_enabled(regulator))
224                 regulator_disable(regulator);
225
226         regulator_put(regulator);
227
228         kfree(bt_data);
229
230         return 0;
231 }
232
233 static struct platform_driver gta02_bt_driver = {
234         .probe          = gta02_bt_probe,
235         .remove         = gta02_bt_remove,
236         .suspend        = gta02_bt_suspend,
237         .resume         = gta02_bt_resume,
238         .driver         = {
239                 .name           = "gta02-pm-bt",
240         },
241 };
242
243 static int __devinit gta02_bt_init(void)
244 {
245         return platform_driver_register(&gta02_bt_driver);
246 }
247
248 static void gta02_bt_exit(void)
249 {
250         platform_driver_unregister(&gta02_bt_driver);
251 }
252
253 module_init(gta02_bt_init);
254 module_exit(gta02_bt_exit);
255
256 MODULE_LICENSE("GPL");
257 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
258 MODULE_DESCRIPTION(DRVMSG);