2 * Bluetooth PM code for the Openmoko Freerunner GSM Phone
4 * (C) 2007 by Openmoko Inc.
5 * Author: Harald Welte <laforge@openmoko.org>
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
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>
22 #include <mach/hardware.h>
23 #include <asm/mach-types.h>
25 #include <mach/gta02.h>
26 #include <linux/mfd/pcf50633/gpio.h>
28 #include <linux/regulator/consumer.h>
30 #define DRVMSG "Openmoko Freerunner Bluetooth Power Management"
32 struct gta02_pm_bt_data
{
33 struct regulator
*regulator
;
34 struct rfkill
*rfkill
;
38 static ssize_t
bt_read(struct device
*dev
, struct device_attribute
*attr
,
42 if (!strcmp(attr
->attr
.name
, "power_on")) {
43 if (s3c2410_gpio_getpin(GTA02_GPIO_BT_EN
))
45 } else if (!strcmp(attr
->attr
.name
, "reset")) {
46 if (s3c2410_gpio_getpin(GTA02_GPIO_BT_EN
) == 0)
51 return strlcpy(buf
, "0\n", 3);
53 return strlcpy(buf
, "1\n", 3);
57 static void __gta02_pm_bt_toggle_radio(struct device
*dev
, unsigned int on
)
59 struct gta02_pm_bt_data
*bt_data
= dev_get_drvdata(dev
);
61 dev_info(dev
, "__gta02_pm_bt_toggle_radio %d\n", on
);
63 bt_data
= dev_get_drvdata(dev
);
65 s3c2410_gpio_setpin(GTA02_GPIO_BT_EN
, !on
);
68 if (!regulator_is_enabled(bt_data
->regulator
))
69 regulator_enable(bt_data
->regulator
);
71 if (regulator_is_enabled(bt_data
->regulator
))
72 regulator_disable(bt_data
->regulator
);
75 s3c2410_gpio_setpin(GTA02_GPIO_BT_EN
, on
);
79 static int bt_rfkill_set_block(void *data
, bool blocked
)
81 struct device
*dev
= data
;
83 __gta02_pm_bt_toggle_radio(dev
, !blocked
);
88 static const struct rfkill_ops gta02_bt_rfkill_ops
= {
89 .set_block
= bt_rfkill_set_block
,
93 static ssize_t
bt_write(struct device
*dev
, struct device_attribute
*attr
,
94 const char *buf
, size_t count
)
96 unsigned long on
= simple_strtoul(buf
, NULL
, 10);
97 struct gta02_pm_bt_data
*bt_data
= dev_get_drvdata(dev
);
99 if (!strcmp(attr
->attr
.name
, "power_on")) {
100 rfkill_set_sw_state(bt_data
->rfkill
, on
? 1 : 0);
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);
111 static DEVICE_ATTR(power_on
, 0644, bt_read
, bt_write
);
112 static DEVICE_ATTR(reset
, 0644, bt_read
, bt_write
);
115 static int gta02_bt_suspend(struct platform_device
*pdev
, pm_message_t state
)
117 struct gta02_pm_bt_data
*bt_data
= dev_get_drvdata(&pdev
->dev
);
119 dev_dbg(&pdev
->dev
, DRVMSG
": suspending\n");
121 bt_data
->pre_resume_state
= s3c2410_gpio_getpin(GTA02_GPIO_BT_EN
);
122 __gta02_pm_bt_toggle_radio(&pdev
->dev
, 0);
127 static int gta02_bt_resume(struct platform_device
*pdev
)
129 struct gta02_pm_bt_data
*bt_data
= dev_get_drvdata(&pdev
->dev
);
130 dev_dbg(&pdev
->dev
, DRVMSG
": resuming\n");
132 __gta02_pm_bt_toggle_radio(&pdev
->dev
, bt_data
->pre_resume_state
);
136 #define gta02_bt_suspend NULL
137 #define gta02_bt_resume NULL
140 static struct attribute
*gta02_bt_sysfs_entries
[] = {
141 &dev_attr_power_on
.attr
,
142 &dev_attr_reset
.attr
,
146 static struct attribute_group gta02_bt_attr_group
= {
148 .attrs
= gta02_bt_sysfs_entries
,
151 static int __init
gta02_bt_probe(struct platform_device
*pdev
)
153 struct rfkill
*rfkill
;
154 struct regulator
*regulator
;
155 struct gta02_pm_bt_data
*bt_data
;
158 dev_info(&pdev
->dev
, DRVMSG
": starting\n");
160 bt_data
= kzalloc(sizeof(*bt_data
), GFP_KERNEL
);
161 dev_set_drvdata(&pdev
->dev
, bt_data
);
163 regulator
= regulator_get(&pdev
->dev
, "BT_3V2");
164 if (IS_ERR(regulator
))
167 bt_data
->regulator
= regulator
;
169 /* this tests the true physical state of the regulator... */
170 if (regulator_is_enabled(regulator
)) {
172 * but these only operate on the logical state of the
173 * regulator... so we need to logicaly "adopt" it on
176 regulator_enable(regulator
);
177 regulator_disable(regulator
);
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);
184 rfkill
= rfkill_alloc(pdev
->name
, &pdev
->dev
, RFKILL_TYPE_BLUETOOTH
,
185 >a02_bt_rfkill_ops
, &pdev
->dev
);
188 dev_err(&pdev
->dev
, "Failed to allocate rfkill\n");
192 rfkill_init_sw_state(rfkill
, 0);
194 ret
= rfkill_register(rfkill
);
196 rfkill_destroy(rfkill
);
197 dev_err(&pdev
->dev
, "Failed to register rfkill\n");
201 bt_data
->rfkill
= rfkill
;
203 return sysfs_create_group(&pdev
->dev
.kobj
, >a02_bt_attr_group
);
206 static int gta02_bt_remove(struct platform_device
*pdev
)
208 struct gta02_pm_bt_data
*bt_data
= dev_get_drvdata(&pdev
->dev
);
209 struct regulator
*regulator
;
211 sysfs_remove_group(&pdev
->dev
.kobj
, >a02_bt_attr_group
);
213 if (bt_data
->rfkill
) {
214 rfkill_destroy(bt_data
->rfkill
);
217 if (!bt_data
|| !bt_data
->regulator
)
220 regulator
= bt_data
->regulator
;
222 /* Make sure regulator is disabled before calling regulator_put */
223 if (regulator_is_enabled(regulator
))
224 regulator_disable(regulator
);
226 regulator_put(regulator
);
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
,
239 .name
= "gta02-pm-bt",
243 static int __devinit
gta02_bt_init(void)
245 return platform_driver_register(>a02_bt_driver
);
248 static void gta02_bt_exit(void)
250 platform_driver_unregister(>a02_bt_driver
);
253 module_init(gta02_bt_init
);
254 module_exit(gta02_bt_exit
);
256 MODULE_LICENSE("GPL");
257 MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
258 MODULE_DESCRIPTION(DRVMSG
);