aboutsummaryrefslogtreecommitdiff
path: root/drivers/leds/leds-neo1973-vibrator.c
blob: 8bfb1179d03cad079b5661fdefb6af2bc8c8ad50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * LED driver for the vibrator of the Openmoko GTA01/GTA02 GSM Phones
 *
 * (C) 2006-2008 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 <mach/hardware.h>
#include <asm/mach-types.h>
#include <plat/pwm.h>
#include <mach/gta01.h>
#include <plat/regs-timer.h>
#include <linux/neo1973_vibrator.h>

#include <asm/plat-s3c24xx/neo1973.h>

#define COUNTER 64

static struct neo1973_vib_priv {
	struct led_classdev cdev;
	unsigned int gpio;
	spinlock_t lock;
	unsigned int has_pwm;
	struct s3c2410_pwm pwm;
	
	unsigned long vib_gpio_pin; /* which pin to meddle with */
	u8 vib_pwm; /* 0 = OFF -- will ensure GPIO deasserted and stop FIQ */
	u8 vib_pwm_latched;
	u32 fiq_count;

	struct neo1973_vib_platform_data *pdata;
} neo1973_vib_priv;

int neo1973_vibrator_fiq_handler(void)
{
	neo1973_vib_priv.fiq_count++;

	if (!neo1973_vib_priv.vib_pwm_latched && !neo1973_vib_priv.vib_pwm)
		/* idle */
		return 0;

	if ((u8)neo1973_vib_priv.fiq_count == neo1973_vib_priv.vib_pwm_latched)
		neo1973_gpb_setpin(neo1973_vib_priv.vib_gpio_pin, 0);

	if ((u8)neo1973_vib_priv.fiq_count)
		return 1;

	neo1973_vib_priv.vib_pwm_latched = neo1973_vib_priv.vib_pwm;
	if (neo1973_vib_priv.vib_pwm_latched)
		neo1973_gpb_setpin(neo1973_vib_priv.vib_gpio_pin, 1);

	return 1;
}

static void neo1973_vib_vib_set(struct led_classdev *led_cdev,
				enum led_brightness value)
{
	unsigned long flags;
	struct neo1973_vib_priv *vp = container_of(led_cdev,
						   struct neo1973_vib_priv,
						   cdev);

#ifdef CONFIG_MACH_NEO1973_GTA02
	if (machine_is_neo1973_gta02()) { /* use FIQ to control GPIO */
		neo1973_vib_priv.vib_pwm = value; /* set it for FIQ */
		neo1973_vib_priv.pdata->kick_fiq(); /* start up FIQs if not already going */
		return;
	}
#endif
	/*
	 * value == 255 -> 99% duty cycle (full power)
	 * value == 128 -> 50% duty cycle (medium power)
	 * value == 0 -> 0% duty cycle (zero power)
	 */
	spin_lock_irqsave(&vp->lock, flags);

	if (vp->has_pwm)
		s3c2410_pwm_duty_cycle(value / 4, &vp->pwm);
	else
		neo1973_gpb_setpin(vp->gpio, value ? 1 : 0);

	spin_unlock_irqrestore(&vp->lock, flags);
}

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);
	if (neo1973_vib_priv.pdata)
		neo1973_vib_priv.pdata->disable_fiq();
	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);
	if (neo1973_vib_priv.pdata)
		neo1973_vib_priv.pdata->enable_fiq();

	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() && !machine_is_neo1973_gta02())
		return -EIO;

	r = platform_get_resource(pdev, 0, 0);
	if (!r || !r->start)
		return -EIO;

	neo1973_vib_led.gpio = r->start;

	neo1973_vib_priv.pdata = pdev->dev.platform_data;
	platform_set_drvdata(pdev, &neo1973_vib_led);

#ifdef CONFIG_MACH_NEO1973_GTA02
	if (machine_is_neo1973_gta02()) { /* use FIQ to control GPIO */
		neo1973_gpb_setpin(neo1973_vib_led.gpio, 0); /* off */
		s3c2410_gpio_cfgpin(neo1973_vib_led.gpio, S3C2410_GPIO_OUTPUT);
		/* safe, kmalloc'd copy needed for FIQ ISR */
		neo1973_vib_priv.vib_gpio_pin = neo1973_vib_led.gpio;
		neo1973_vib_priv.vib_pwm = 0; /* off */
		goto configured;
	}
#endif

	/* 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;
	}
#ifdef CONFIG_MACH_NEO1973_GTA02
configured:
#endif
	spin_lock_init(&neo1973_vib_led.lock);

	return led_classdev_register(&pdev->dev, &neo1973_vib_led.cdev);
}

static int neo1973_vib_remove(struct platform_device *pdev)
{
#ifdef CONFIG_MACH_NEO1973_GTA02
	if (machine_is_neo1973_gta02()) /* use FIQ to control GPIO */
		neo1973_vib_priv.vib_pwm = 0; /* off */
	/* would only need kick if already off so no kick needed */
#endif

	if (neo1973_vib_led.has_pwm)
		s3c2410_pwm_disable(&neo1973_vib_led.pwm);

	led_classdev_unregister(&neo1973_vib_led.cdev);

	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("Openmoko GTA01/GTA02 vibrator driver");
MODULE_LICENSE("GPL");