From f7ad7d5e90a4ab68e7e25df3bd4a56b6c23f5e45 Mon Sep 17 00:00:00 2001
From: mokopatches <mokopatches@openmoko.org>
Date: Fri, 4 Apr 2008 11:31:55 +0100
Subject: [PATCH] gta01-vibrator.patch
 This patch adds driver support for the vibator device of the FIC/OpenMoko
 Neo1973 GSM phone. The driver uses the existing LED class driver framework,
 since there's a lot of similarity between the LED and the vibrator function.

Signed-off-by: Harald Welte <laforge@openmoko.org>
---
 drivers/leds/Kconfig                 |    8 ++-
 drivers/leds/Makefile                |    1 +
 drivers/leds/leds-neo1973-vibrator.c |  181 ++++++++++++++++++++++++++++++++++
 3 files changed, 189 insertions(+), 1 deletions(-)
 create mode 100644 drivers/leds/leds-neo1973-vibrator.c

diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
index ec568fa..4579a88 100644
--- a/drivers/leds/Kconfig
+++ b/drivers/leds/Kconfig
@@ -57,7 +57,7 @@ config LEDS_TOSA
 
 config LEDS_S3C24XX
 	tristate "LED Support for Samsung S3C24XX GPIO LEDs"
-	depends on LEDS_CLASS && ARCH_S3C2410
+	depends on LEDS_CLASS && ARCH_S3C2410 && S3C2410_PWM
 	help
 	  This option enables support for LEDs connected to GPIO lines
 	  on Samsung S3C24XX series CPUs, such as the S3C2410 and S3C2440.
@@ -114,6 +114,12 @@ config LEDS_CM_X270
 	help
 	  This option enables support for the CM-X270 LEDs.
 
+config LEDS_NEO1973_VIBRATOR
+	tristate "Vibrator Support for the FIC Neo1973 GSM phone"
+	depends on LEDS_CLASS && MACH_NEO1973
+	help
+	  This option enables support for the vibrator on the FIC Neo1973.
+
 comment "LED Triggers"
 
 config LEDS_TRIGGERS
diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
index a60de1b..f64332d 100644
--- a/drivers/leds/Makefile
+++ b/drivers/leds/Makefile
@@ -19,6 +19,7 @@ obj-$(CONFIG_LEDS_COBALT_QUBE)		+= leds-cobalt-qube.o
 obj-$(CONFIG_LEDS_COBALT_RAQ)		+= leds-cobalt-raq.o
 obj-$(CONFIG_LEDS_GPIO)			+= leds-gpio.o
 obj-$(CONFIG_LEDS_CM_X270)              += leds-cm-x270.o
+obj-$(CONFIG_LEDS_NEO1973_VIBRATOR)	+= leds-neo1973-vibrator.o
 
 # LED Triggers
 obj-$(CONFIG_LEDS_TRIGGER_TIMER)	+= ledtrig-timer.o
diff --git a/drivers/leds/leds-neo1973-vibrator.c b/drivers/leds/leds-neo1973-vibrator.c
new file mode 100644
index 0000000..0336e36
--- /dev/null
+++ b/drivers/leds/leds-neo1973-vibrator.c
@@ -0,0 +1,181 @@
+/*
+ * LED driver for the vibrator of the FIC Neo1973 GSM Phone
+ *
+ * (C) 2006-2007 by OpenMoko, Inc.
+ * Author: Harald Welte <laforge@openmoko.org>
+ * 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.
+ *
+ * Javi Roman <javiroman@kernel-labs.org>:
+ * 	Implement PWM support for GTA01Bv4 and later
+ */
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/platform_device.h>
+#include <linux/leds.h>
+#include <asm/hardware.h>
+#include <asm/mach-types.h>
+#include <asm/arch/pwm.h>
+#include <asm/arch/gta01.h>
+#include <asm/plat-s3c/regs-timer.h>
+
+#define COUNTER 64
+
+struct neo1973_vib_priv {
+	struct led_classdev cdev;
+	unsigned int gpio;
+	struct mutex mutex;
+	unsigned int has_pwm;
+	struct s3c2410_pwm pwm;
+};
+
+static void neo1973_vib_vib_set(struct led_classdev *led_cdev,
+		enum led_brightness value)
+{
+	struct neo1973_vib_priv *vp =
+		container_of(led_cdev, struct neo1973_vib_priv, cdev);
+
+	/*
+	 * value == 255 -> 99% duty cycle (full power)
+	 * value == 128 -> 50% duty cycle (medium power)
+	 * value == 0 -> 0% duty cycle (zero power)
+	 */
+	mutex_lock(&vp->mutex);
+	if (vp->has_pwm)
+		s3c2410_pwm_duty_cycle(value/4, &vp->pwm);
+	else {
+		if (value)
+			s3c2410_gpio_setpin(vp->gpio, 1);
+		else
+			s3c2410_gpio_setpin(vp->gpio, 0);
+	}
+
+	mutex_unlock(&vp->mutex);
+}
+
+static struct neo1973_vib_priv neo1973_vib_led = {
+	.cdev = {
+		.name = "neo1973:vibrator",
+		.brightness_set = neo1973_vib_vib_set,
+	},
+};
+
+static int neo1973_vib_init_hw(struct neo1973_vib_priv *vp)
+{
+	int rc;
+
+	rc = s3c2410_pwm_init(&vp->pwm);
+	if (rc)
+		return rc;
+
+	vp->pwm.timerid = PWM3;
+	/* use same prescaler as arch/arm/plat-s3c24xx/time.c */
+	vp->pwm.prescaler = (6 - 1) / 2;
+	vp->pwm.divider = S3C2410_TCFG1_MUX3_DIV2;
+	vp->pwm.counter = COUNTER;
+	vp->pwm.comparer = COUNTER;
+
+	rc = s3c2410_pwm_enable(&vp->pwm);
+	if (rc)
+		return rc;
+
+	s3c2410_pwm_start(&vp->pwm);
+
+	return 0;
+}
+
+#ifdef CONFIG_PM
+static int neo1973_vib_suspend(struct platform_device *dev, pm_message_t state)
+{
+	led_classdev_suspend(&neo1973_vib_led.cdev);
+	return 0;
+}
+
+static int neo1973_vib_resume(struct platform_device *dev)
+{
+	struct neo1973_vib_priv *vp = platform_get_drvdata(dev);
+
+	if (vp->has_pwm)
+		neo1973_vib_init_hw(vp);
+
+	led_classdev_resume(&neo1973_vib_led.cdev);
+
+	return 0;
+}
+#endif /* CONFIG_PM */
+
+static int __init neo1973_vib_probe(struct platform_device *pdev)
+{
+	struct resource *r;
+	int rc;
+
+	if (!machine_is_neo1973_gta01())
+		return -EIO;
+
+	r = platform_get_resource(pdev, 0, 0);
+	if (!r || !r->start)
+		return -EIO;
+
+	neo1973_vib_led.gpio = r->start;
+	platform_set_drvdata(pdev, &neo1973_vib_led);
+
+	/* TOUT3 */
+	if (neo1973_vib_led.gpio == S3C2410_GPB3) {
+		rc = neo1973_vib_init_hw(&neo1973_vib_led);
+		if (rc)
+			return rc;
+
+		s3c2410_pwm_duty_cycle(0, &neo1973_vib_led.pwm);
+		s3c2410_gpio_cfgpin(neo1973_vib_led.gpio, S3C2410_GPB3_TOUT3);
+		neo1973_vib_led.has_pwm = 1;
+	}
+
+	mutex_init(&neo1973_vib_led.mutex);
+
+	return led_classdev_register(&pdev->dev, &neo1973_vib_led.cdev);
+}
+
+static int neo1973_vib_remove(struct platform_device *pdev)
+{
+	if (neo1973_vib_led.has_pwm)
+		s3c2410_pwm_disable(&neo1973_vib_led.pwm);
+
+	led_classdev_unregister(&neo1973_vib_led.cdev);
+
+	mutex_destroy(&neo1973_vib_led.mutex);
+
+	return 0;
+}
+
+static struct platform_driver neo1973_vib_driver = {
+	.probe		= neo1973_vib_probe,
+	.remove		= neo1973_vib_remove,
+#ifdef CONFIG_PM
+	.suspend	= neo1973_vib_suspend,
+	.resume		= neo1973_vib_resume,
+#endif
+	.driver		= {
+		.name		= "neo1973-vibrator",
+	},
+};
+
+static int __init neo1973_vib_init(void)
+{
+	return platform_driver_register(&neo1973_vib_driver);
+}
+
+static void __exit neo1973_vib_exit(void)
+{
+	platform_driver_unregister(&neo1973_vib_driver);
+}
+
+module_init(neo1973_vib_init);
+module_exit(neo1973_vib_exit);
+
+MODULE_AUTHOR("Harald Welte <laforge@openmoko.org>");
+MODULE_DESCRIPTION("FIC Neo1973 vibrator driver");
+MODULE_LICENSE("GPL");
-- 
1.5.6.5