/* * Bluetooth PM code for the FIC Neo1973 GSM Phone * * (C) 2007 by Openmoko Inc. * Author: Harald Welte * All rights reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation * */ #include #include #include #include #include #include #include #include #include /* For GTA01 */ #include #include /* For GTA02 */ #include #include #include #define DRVMSG "FIC Neo1973 Bluetooth Power Management" struct gta01_pm_bt_data { struct regulator *regulator; }; static ssize_t bt_read(struct device *dev, struct device_attribute *attr, char *buf) { int ret = 0; if (!strcmp(attr->attr.name, "power_on")) { if (machine_is_neo1973_gta01()) { if (pcf50606_onoff_get(pcf50606_global, PCF50606_REGULATOR_D1REG) && pcf50606_voltage_get(pcf50606_global, PCF50606_REGULATOR_D1REG) == 3100) ret = 1; } else if (machine_is_neo1973_gta02()) { if (s3c2410_gpio_getpin(GTA02_GPIO_BT_EN)) ret = 1; } } else if (!strcmp(attr->attr.name, "reset")) { if (machine_is_neo1973_gta01()) { if (s3c2410_gpio_getpin(GTA01_GPIO_BT_EN) == 0) ret = 1; } else if (machine_is_neo1973_gta02()) { if (s3c2410_gpio_getpin(GTA02_GPIO_BT_EN) == 0) ret = 1; } } if (!ret) { return strlcpy(buf, "0\n", 3); } else { return strlcpy(buf, "1\n", 3); } } static int bt_rfkill_toggle_radio(void *data, enum rfkill_state state) { struct device *dev = data; unsigned long on = (state == RFKILL_STATE_ON); struct gta01_pm_bt_data *bt_data; if (machine_is_neo1973_gta01()) { /* if we are powering up, assert reset, then power, * then release reset */ if (on) { neo1973_gpb_setpin(GTA01_GPIO_BT_EN, 0); pcf50606_voltage_set(pcf50606_global, PCF50606_REGULATOR_D1REG, 3100); } pcf50606_onoff_set(pcf50606_global, PCF50606_REGULATOR_D1REG, on); neo1973_gpb_setpin(GTA01_GPIO_BT_EN, on); } else if (machine_is_neo1973_gta02()) { if (s3c2410_gpio_getpin(GTA02_GPIO_BT_EN) == on) return 0; bt_data = dev_get_drvdata(dev); BUG_ON(!bt_data || !bt_data->regulator); neo1973_gpb_setpin(GTA02_GPIO_BT_EN, !on); if (on) { if (!regulator_is_enabled(bt_data->regulator)) regulator_enable(bt_data->regulator); } else { if (regulator_is_enabled(bt_data->regulator)) regulator_disable(bt_data->regulator); } neo1973_gpb_setpin(GTA02_GPIO_BT_EN, on); } return 0; } static ssize_t bt_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { unsigned long on = simple_strtoul(buf, NULL, 10); unsigned int vol; struct gta01_pm_bt_data *bt_data; struct regulator *regulator; if (!strcmp(attr->attr.name, "power_on")) { struct rfkill *rfkill = dev_get_drvdata(dev); enum rfkill_state state = on ? RFKILL_STATE_ON : RFKILL_STATE_OFF; bt_rfkill_toggle_radio(dev, state); rfkill->state = state; if (machine_is_neo1973_gta01()) { /* if we are powering up, assert reset, then power, * then release reset */ if (on) { neo1973_gpb_setpin(GTA01_GPIO_BT_EN, 0); pcf50606_voltage_set(pcf50606_global, PCF50606_REGULATOR_D1REG, 3100); } pcf50606_onoff_set(pcf50606_global, PCF50606_REGULATOR_D1REG, on); neo1973_gpb_setpin(GTA01_GPIO_BT_EN, on); } else if (machine_is_neo1973_gta02()) { if (s3c2410_gpio_getpin(GTA02_GPIO_BT_EN) == on) return count; bt_data = dev_get_drvdata(dev); BUG_ON(!bt_data || !bt_data->regulator); regulator = bt_data->regulator; neo1973_gpb_setpin(GTA02_GPIO_BT_EN, !on); if (on) { if (!regulator_is_enabled(regulator)) regulator_enable(regulator); } else { if (regulator_is_enabled(regulator)) regulator_disable(regulator); } vol = regulator_get_voltage(regulator); dev_info(dev, "GTA02 Set PCF50633 LDO4 = %d\n", vol); neo1973_gpb_setpin(GTA02_GPIO_BT_EN, on); } } else if (!strcmp(attr->attr.name, "reset")) { /* reset is low-active, so we need to invert */ if (machine_is_neo1973_gta01()) { neo1973_gpb_setpin(GTA01_GPIO_BT_EN, on ? 0 : 1); } else if (machine_is_neo1973_gta02()) { neo1973_gpb_setpin(GTA02_GPIO_BT_EN, on ? 0 : 1); } } return count; } static DEVICE_ATTR(power_on, 0644, bt_read, bt_write); static DEVICE_ATTR(reset, 0644, bt_read, bt_write); #ifdef CONFIG_PM static int gta01_bt_suspend(struct platform_device *pdev, pm_message_t state) { dev_dbg(&pdev->dev, DRVMSG ": suspending\n"); /* FIXME: The PMU should save the PMU status, and the GPIO code should * preserve the GPIO level, so there shouldn't be anything left to do * for us, should there? */ return 0; } static int gta01_bt_resume(struct platform_device *pdev) { dev_dbg(&pdev->dev, DRVMSG ": resuming\n"); return 0; } #else #define gta01_bt_suspend NULL #define gta01_bt_resume NULL #endif static struct attribute *gta01_bt_sysfs_entries[] = { &dev_attr_power_on.attr, &dev_attr_reset.attr, NULL }; static struct attribute_group gta01_bt_attr_group = { .name = NULL, .attrs = gta01_bt_sysfs_entries, }; static int __init gta01_bt_probe(struct platform_device *pdev) { struct rfkill *rfkill; struct regulator *regulator; struct gta01_pm_bt_data *bt_data; int ret; dev_info(&pdev->dev, DRVMSG ": starting\n"); if (machine_is_neo1973_gta01()) { /* we make sure that the voltage is off */ pcf50606_onoff_set(pcf50606_global, PCF50606_REGULATOR_D1REG, 0); /* we pull reset to low to make sure that the chip doesn't * drain power through the reset line */ neo1973_gpb_setpin(GTA01_GPIO_BT_EN, 0); } else if (machine_is_neo1973_gta02()) { regulator = regulator_get(&pdev->dev, "BT_3V2"); if (IS_ERR(regulator)) return -ENODEV; bt_data = kmalloc(sizeof(*bt_data), GFP_KERNEL); bt_data->regulator = regulator; dev_set_drvdata(&pdev->dev, bt_data); /* this tests the true physical state of the regulator... */ if (regulator_is_enabled(regulator)) { /* * but these only operate on the logical state of the * regulator... so we need to logicaly "adopt" it on * to turn it off */ regulator_enable(regulator); regulator_disable(regulator); } /* we pull reset to low to make sure that the chip doesn't * drain power through the reset line */ neo1973_gpb_setpin(GTA02_GPIO_BT_EN, 0); } rfkill = rfkill_allocate(&pdev->dev, RFKILL_TYPE_BLUETOOTH); rfkill->name = pdev->name; rfkill->data = &pdev->dev; rfkill->state = RFKILL_STATE_OFF; rfkill->toggle_radio = bt_rfkill_toggle_radio; ret = rfkill_register(rfkill); if (ret) { dev_err(&pdev->dev, "Failed to register rfkill\n"); return ret; } platform_set_drvdata(pdev, rfkill); return sysfs_create_group(&pdev->dev.kobj, >a01_bt_attr_group); } static int gta01_bt_remove(struct platform_device *pdev) { struct rfkill *rfkill = platform_get_drvdata(pdev); struct gta01_pm_bt_data *bt_data; struct regulator *regulator; sysfs_remove_group(&pdev->dev.kobj, >a01_bt_attr_group); rfkill_unregister(rfkill); rfkill_free(rfkill); bt_data = dev_get_drvdata(&pdev->dev); if (!bt_data || !bt_data->regulator) return 0; regulator = bt_data->regulator; /* Make sure regulator is disabled before calling regulator_put */ if (regulator_is_enabled(regulator)) regulator_disable(regulator); regulator_put(regulator); return 0; } static struct platform_driver gta01_bt_driver = { .probe = gta01_bt_probe, .remove = gta01_bt_remove, .suspend = gta01_bt_suspend, .resume = gta01_bt_resume, .driver = { .name = "neo1973-pm-bt", }, }; static int __devinit gta01_bt_init(void) { return platform_driver_register(>a01_bt_driver); } static void gta01_bt_exit(void) { platform_driver_unregister(>a01_bt_driver); } module_init(gta01_bt_init); module_exit(gta01_bt_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Harald Welte "); MODULE_DESCRIPTION(DRVMSG);