diff options
author | Jari Vanhala <ext-jari.vanhala@nokia.com> | 2010-03-09 03:29:46 -0500 |
---|---|---|
committer | Dmitry Torokhov <dmitry.torokhov@gmail.com> | 2010-03-09 03:30:20 -0500 |
commit | 3dd1b39497b6820219581af16e6a8831a582bb3a (patch) | |
tree | 20408f3c96c955a9aa386b000d410582ea6257d0 /drivers/input/misc/twl4030-vibra.c | |
parent | 3e6e15a862a0bc20128497bbdc54254cdec21835 (diff) |
Input: add driver for TWL4030 vibrator device
TWL4030 Vibrator implemented via Force Feedback interface.
This uses MFD TWL4030 codec and own dynamic workqueue.
Signed-off-by: Jari Vanhala <ext-jari.vanhala@nokia.com>
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Diffstat (limited to 'drivers/input/misc/twl4030-vibra.c')
-rw-r--r-- | drivers/input/misc/twl4030-vibra.c | 297 |
1 files changed, 297 insertions, 0 deletions
diff --git a/drivers/input/misc/twl4030-vibra.c b/drivers/input/misc/twl4030-vibra.c new file mode 100644 index 000000000000..2fb79e064da3 --- /dev/null +++ b/drivers/input/misc/twl4030-vibra.c | |||
@@ -0,0 +1,297 @@ | |||
1 | /* | ||
2 | * twl4030-vibra.c - TWL4030 Vibrator driver | ||
3 | * | ||
4 | * Copyright (C) 2008-2010 Nokia Corporation | ||
5 | * | ||
6 | * Written by Henrik Saari <henrik.saari@nokia.com> | ||
7 | * Updates by Felipe Balbi <felipe.balbi@nokia.com> | ||
8 | * Input by Jari Vanhala <ext-jari.vanhala@nokia.com> | ||
9 | * | ||
10 | * This program is free software; you can redistribute it and/or modify | ||
11 | * it under the terms of the GNU General Public License version 2 as | ||
12 | * published by the Free Software Foundation. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, but | ||
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with this program; if not, write to the Free Software | ||
21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
22 | * 02110-1301 USA | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | #include <linux/module.h> | ||
27 | #include <linux/jiffies.h> | ||
28 | #include <linux/platform_device.h> | ||
29 | #include <linux/workqueue.h> | ||
30 | #include <linux/i2c/twl.h> | ||
31 | #include <linux/mfd/twl4030-codec.h> | ||
32 | #include <linux/input.h> | ||
33 | |||
34 | /* MODULE ID2 */ | ||
35 | #define LEDEN 0x00 | ||
36 | |||
37 | /* ForceFeedback */ | ||
38 | #define EFFECT_DIR_180_DEG 0x8000 /* range is 0 - 0xFFFF */ | ||
39 | |||
40 | struct vibra_info { | ||
41 | struct device *dev; | ||
42 | struct input_dev *input_dev; | ||
43 | |||
44 | struct workqueue_struct *workqueue; | ||
45 | struct work_struct play_work; | ||
46 | |||
47 | bool enabled; | ||
48 | int speed; | ||
49 | int direction; | ||
50 | |||
51 | bool coexist; | ||
52 | }; | ||
53 | |||
54 | static void vibra_disable_leds(void) | ||
55 | { | ||
56 | u8 reg; | ||
57 | |||
58 | /* Disable LEDA & LEDB, cannot be used with vibra (PWM) */ | ||
59 | twl_i2c_read_u8(TWL4030_MODULE_LED, ®, LEDEN); | ||
60 | reg &= ~0x03; | ||
61 | twl_i2c_write_u8(TWL4030_MODULE_LED, LEDEN, reg); | ||
62 | } | ||
63 | |||
64 | /* Powers H-Bridge and enables audio clk */ | ||
65 | static void vibra_enable(struct vibra_info *info) | ||
66 | { | ||
67 | u8 reg; | ||
68 | |||
69 | twl4030_codec_enable_resource(TWL4030_CODEC_RES_POWER); | ||
70 | |||
71 | /* turn H-Bridge on */ | ||
72 | twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, | ||
73 | ®, TWL4030_REG_VIBRA_CTL); | ||
74 | twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, | ||
75 | (reg | TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL); | ||
76 | |||
77 | twl4030_codec_enable_resource(TWL4030_CODEC_RES_APLL); | ||
78 | |||
79 | info->enabled = true; | ||
80 | } | ||
81 | |||
82 | static void vibra_disable(struct vibra_info *info) | ||
83 | { | ||
84 | u8 reg; | ||
85 | |||
86 | /* Power down H-Bridge */ | ||
87 | twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, | ||
88 | ®, TWL4030_REG_VIBRA_CTL); | ||
89 | twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, | ||
90 | (reg & ~TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL); | ||
91 | |||
92 | twl4030_codec_disable_resource(TWL4030_CODEC_RES_POWER); | ||
93 | twl4030_codec_disable_resource(TWL4030_CODEC_RES_APLL); | ||
94 | |||
95 | info->enabled = false; | ||
96 | } | ||
97 | |||
98 | static void vibra_play_work(struct work_struct *work) | ||
99 | { | ||
100 | struct vibra_info *info = container_of(work, | ||
101 | struct vibra_info, play_work); | ||
102 | int dir; | ||
103 | int pwm; | ||
104 | u8 reg; | ||
105 | |||
106 | dir = info->direction; | ||
107 | pwm = info->speed; | ||
108 | |||
109 | twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, | ||
110 | ®, TWL4030_REG_VIBRA_CTL); | ||
111 | if (pwm && (!info->coexist || !(reg & TWL4030_VIBRA_SEL))) { | ||
112 | |||
113 | if (!info->enabled) | ||
114 | vibra_enable(info); | ||
115 | |||
116 | /* set vibra rotation direction */ | ||
117 | twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, | ||
118 | ®, TWL4030_REG_VIBRA_CTL); | ||
119 | reg = (dir) ? (reg | TWL4030_VIBRA_DIR) : | ||
120 | (reg & ~TWL4030_VIBRA_DIR); | ||
121 | twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, | ||
122 | reg, TWL4030_REG_VIBRA_CTL); | ||
123 | |||
124 | /* set PWM, 1 = max, 255 = min */ | ||
125 | twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, | ||
126 | 256 - pwm, TWL4030_REG_VIBRA_SET); | ||
127 | } else { | ||
128 | if (info->enabled) | ||
129 | vibra_disable(info); | ||
130 | } | ||
131 | } | ||
132 | |||
133 | /*** Input/ForceFeedback ***/ | ||
134 | |||
135 | static int vibra_play(struct input_dev *input, void *data, | ||
136 | struct ff_effect *effect) | ||
137 | { | ||
138 | struct vibra_info *info = input_get_drvdata(input); | ||
139 | |||
140 | info->speed = effect->u.rumble.strong_magnitude >> 8; | ||
141 | if (!info->speed) | ||
142 | info->speed = effect->u.rumble.weak_magnitude >> 9; | ||
143 | info->direction = effect->direction < EFFECT_DIR_180_DEG ? 0 : 1; | ||
144 | queue_work(info->workqueue, &info->play_work); | ||
145 | return 0; | ||
146 | } | ||
147 | |||
148 | static int twl4030_vibra_open(struct input_dev *input) | ||
149 | { | ||
150 | struct vibra_info *info = input_get_drvdata(input); | ||
151 | |||
152 | info->workqueue = create_singlethread_workqueue("vibra"); | ||
153 | if (info->workqueue == NULL) { | ||
154 | dev_err(&input->dev, "couldn't create workqueue\n"); | ||
155 | return -ENOMEM; | ||
156 | } | ||
157 | return 0; | ||
158 | } | ||
159 | |||
160 | static void twl4030_vibra_close(struct input_dev *input) | ||
161 | { | ||
162 | struct vibra_info *info = input_get_drvdata(input); | ||
163 | |||
164 | cancel_work_sync(&info->play_work); | ||
165 | INIT_WORK(&info->play_work, vibra_play_work); /* cleanup */ | ||
166 | destroy_workqueue(info->workqueue); | ||
167 | info->workqueue = NULL; | ||
168 | |||
169 | if (info->enabled) | ||
170 | vibra_disable(info); | ||
171 | } | ||
172 | |||
173 | /*** Module ***/ | ||
174 | #if CONFIG_PM | ||
175 | static int twl4030_vibra_suspend(struct device *dev) | ||
176 | { | ||
177 | struct platform_device *pdev = to_platform_device(dev); | ||
178 | struct vibra_info *info = platform_get_drvdata(pdev); | ||
179 | |||
180 | if (info->enabled) | ||
181 | vibra_disable(info); | ||
182 | |||
183 | return 0; | ||
184 | } | ||
185 | |||
186 | static int twl4030_vibra_resume(struct device *dev) | ||
187 | { | ||
188 | vibra_disable_leds(); | ||
189 | return 0; | ||
190 | } | ||
191 | |||
192 | static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops, | ||
193 | twl4030_vibra_suspend, twl4030_vibra_resume); | ||
194 | #endif | ||
195 | |||
196 | static int __devinit twl4030_vibra_probe(struct platform_device *pdev) | ||
197 | { | ||
198 | struct twl4030_codec_vibra_data *pdata = pdev->dev.platform_data; | ||
199 | struct vibra_info *info; | ||
200 | int ret; | ||
201 | |||
202 | if (!pdata) { | ||
203 | dev_dbg(&pdev->dev, "platform_data not available\n"); | ||
204 | return -EINVAL; | ||
205 | } | ||
206 | |||
207 | info = kzalloc(sizeof(*info), GFP_KERNEL); | ||
208 | if (!info) | ||
209 | return -ENOMEM; | ||
210 | |||
211 | info->dev = &pdev->dev; | ||
212 | info->coexist = pdata->coexist; | ||
213 | INIT_WORK(&info->play_work, vibra_play_work); | ||
214 | |||
215 | info->input_dev = input_allocate_device(); | ||
216 | if (info->input_dev == NULL) { | ||
217 | dev_err(&pdev->dev, "couldn't allocate input device\n"); | ||
218 | ret = -ENOMEM; | ||
219 | goto err_kzalloc; | ||
220 | } | ||
221 | |||
222 | input_set_drvdata(info->input_dev, info); | ||
223 | |||
224 | info->input_dev->name = "twl4030:vibrator"; | ||
225 | info->input_dev->id.version = 1; | ||
226 | info->input_dev->dev.parent = pdev->dev.parent; | ||
227 | info->input_dev->open = twl4030_vibra_open; | ||
228 | info->input_dev->close = twl4030_vibra_close; | ||
229 | __set_bit(FF_RUMBLE, info->input_dev->ffbit); | ||
230 | |||
231 | ret = input_ff_create_memless(info->input_dev, NULL, vibra_play); | ||
232 | if (ret < 0) { | ||
233 | dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n"); | ||
234 | goto err_ialloc; | ||
235 | } | ||
236 | |||
237 | ret = input_register_device(info->input_dev); | ||
238 | if (ret < 0) { | ||
239 | dev_dbg(&pdev->dev, "couldn't register input device\n"); | ||
240 | goto err_iff; | ||
241 | } | ||
242 | |||
243 | vibra_disable_leds(); | ||
244 | |||
245 | platform_set_drvdata(pdev, info); | ||
246 | return 0; | ||
247 | |||
248 | err_iff: | ||
249 | input_ff_destroy(info->input_dev); | ||
250 | err_ialloc: | ||
251 | input_free_device(info->input_dev); | ||
252 | err_kzalloc: | ||
253 | kfree(info); | ||
254 | return ret; | ||
255 | } | ||
256 | |||
257 | static int __devexit twl4030_vibra_remove(struct platform_device *pdev) | ||
258 | { | ||
259 | struct vibra_info *info = platform_get_drvdata(pdev); | ||
260 | |||
261 | /* this also free ff-memless and calls close if needed */ | ||
262 | input_unregister_device(info->input_dev); | ||
263 | kfree(info); | ||
264 | platform_set_drvdata(pdev, NULL); | ||
265 | |||
266 | return 0; | ||
267 | } | ||
268 | |||
269 | static struct platform_driver twl4030_vibra_driver = { | ||
270 | .probe = twl4030_vibra_probe, | ||
271 | .remove = __devexit_p(twl4030_vibra_remove), | ||
272 | .driver = { | ||
273 | .name = "twl4030_codec_vibra", | ||
274 | .owner = THIS_MODULE, | ||
275 | #ifdef CONFIG_PM | ||
276 | .pm = &twl4030_vibra_pm_ops, | ||
277 | #endif | ||
278 | }, | ||
279 | }; | ||
280 | |||
281 | static int __init twl4030_vibra_init(void) | ||
282 | { | ||
283 | return platform_driver_register(&twl4030_vibra_driver); | ||
284 | } | ||
285 | module_init(twl4030_vibra_init); | ||
286 | |||
287 | static void __exit twl4030_vibra_exit(void) | ||
288 | { | ||
289 | platform_driver_unregister(&twl4030_vibra_driver); | ||
290 | } | ||
291 | module_exit(twl4030_vibra_exit); | ||
292 | |||
293 | MODULE_ALIAS("platform:twl4030_codec_vibra"); | ||
294 | |||
295 | MODULE_DESCRIPTION("TWL4030 Vibra driver"); | ||
296 | MODULE_LICENSE("GPL"); | ||
297 | MODULE_AUTHOR("Nokia Corporation"); | ||