2 * twl4030-vibra.c - TWL4030 Vibrator driver
4 * Copyright (C) 2008-2010 Nokia Corporation
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>
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.
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.
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
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/mfd/core.h>
33 #include <linux/input.h>
34 #include <linux/slab.h>
40 #define EFFECT_DIR_180_DEG 0x8000 /* range is 0 - 0xFFFF */
44 struct input_dev
*input_dev
;
46 struct workqueue_struct
*workqueue
;
47 struct work_struct play_work
;
56 static void vibra_disable_leds(void)
60 /* Disable LEDA & LEDB, cannot be used with vibra (PWM) */
61 twl_i2c_read_u8(TWL4030_MODULE_LED
, ®
, LEDEN
);
63 twl_i2c_write_u8(TWL4030_MODULE_LED
, LEDEN
, reg
);
66 /* Powers H-Bridge and enables audio clk */
67 static void vibra_enable(struct vibra_info
*info
)
71 twl4030_codec_enable_resource(TWL4030_CODEC_RES_POWER
);
73 /* turn H-Bridge on */
74 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
,
75 ®
, TWL4030_REG_VIBRA_CTL
);
76 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
77 (reg
| TWL4030_VIBRA_EN
), TWL4030_REG_VIBRA_CTL
);
79 twl4030_codec_enable_resource(TWL4030_CODEC_RES_APLL
);
84 static void vibra_disable(struct vibra_info
*info
)
88 /* Power down H-Bridge */
89 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
,
90 ®
, TWL4030_REG_VIBRA_CTL
);
91 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
92 (reg
& ~TWL4030_VIBRA_EN
), TWL4030_REG_VIBRA_CTL
);
94 twl4030_codec_disable_resource(TWL4030_CODEC_RES_APLL
);
95 twl4030_codec_disable_resource(TWL4030_CODEC_RES_POWER
);
97 info
->enabled
= false;
100 static void vibra_play_work(struct work_struct
*work
)
102 struct vibra_info
*info
= container_of(work
,
103 struct vibra_info
, play_work
);
108 dir
= info
->direction
;
111 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
,
112 ®
, TWL4030_REG_VIBRA_CTL
);
113 if (pwm
&& (!info
->coexist
|| !(reg
& TWL4030_VIBRA_SEL
))) {
118 /* set vibra rotation direction */
119 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
,
120 ®
, TWL4030_REG_VIBRA_CTL
);
121 reg
= (dir
) ? (reg
| TWL4030_VIBRA_DIR
) :
122 (reg
& ~TWL4030_VIBRA_DIR
);
123 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
124 reg
, TWL4030_REG_VIBRA_CTL
);
126 /* set PWM, 1 = max, 255 = min */
127 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
128 256 - pwm
, TWL4030_REG_VIBRA_SET
);
135 /*** Input/ForceFeedback ***/
137 static int vibra_play(struct input_dev
*input
, void *data
,
138 struct ff_effect
*effect
)
140 struct vibra_info
*info
= input_get_drvdata(input
);
142 info
->speed
= effect
->u
.rumble
.strong_magnitude
>> 8;
144 info
->speed
= effect
->u
.rumble
.weak_magnitude
>> 9;
145 info
->direction
= effect
->direction
< EFFECT_DIR_180_DEG
? 0 : 1;
146 queue_work(info
->workqueue
, &info
->play_work
);
150 static int twl4030_vibra_open(struct input_dev
*input
)
152 struct vibra_info
*info
= input_get_drvdata(input
);
154 info
->workqueue
= create_singlethread_workqueue("vibra");
155 if (info
->workqueue
== NULL
) {
156 dev_err(&input
->dev
, "couldn't create workqueue\n");
162 static void twl4030_vibra_close(struct input_dev
*input
)
164 struct vibra_info
*info
= input_get_drvdata(input
);
166 cancel_work_sync(&info
->play_work
);
167 INIT_WORK(&info
->play_work
, vibra_play_work
); /* cleanup */
168 destroy_workqueue(info
->workqueue
);
169 info
->workqueue
= NULL
;
177 static int twl4030_vibra_suspend(struct device
*dev
)
179 struct platform_device
*pdev
= to_platform_device(dev
);
180 struct vibra_info
*info
= platform_get_drvdata(pdev
);
188 static int twl4030_vibra_resume(struct device
*dev
)
190 vibra_disable_leds();
194 static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops
,
195 twl4030_vibra_suspend
, twl4030_vibra_resume
);
198 static int __devinit
twl4030_vibra_probe(struct platform_device
*pdev
)
200 struct twl4030_codec_vibra_data
*pdata
= mfd_get_data(pdev
);
201 struct vibra_info
*info
;
205 dev_dbg(&pdev
->dev
, "platform_data not available\n");
209 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
213 info
->dev
= &pdev
->dev
;
214 info
->coexist
= pdata
->coexist
;
215 INIT_WORK(&info
->play_work
, vibra_play_work
);
217 info
->input_dev
= input_allocate_device();
218 if (info
->input_dev
== NULL
) {
219 dev_err(&pdev
->dev
, "couldn't allocate input device\n");
224 input_set_drvdata(info
->input_dev
, info
);
226 info
->input_dev
->name
= "twl4030:vibrator";
227 info
->input_dev
->id
.version
= 1;
228 info
->input_dev
->dev
.parent
= pdev
->dev
.parent
;
229 info
->input_dev
->open
= twl4030_vibra_open
;
230 info
->input_dev
->close
= twl4030_vibra_close
;
231 __set_bit(FF_RUMBLE
, info
->input_dev
->ffbit
);
233 ret
= input_ff_create_memless(info
->input_dev
, NULL
, vibra_play
);
235 dev_dbg(&pdev
->dev
, "couldn't register vibrator to FF\n");
239 ret
= input_register_device(info
->input_dev
);
241 dev_dbg(&pdev
->dev
, "couldn't register input device\n");
245 vibra_disable_leds();
247 platform_set_drvdata(pdev
, info
);
251 input_ff_destroy(info
->input_dev
);
253 input_free_device(info
->input_dev
);
259 static int __devexit
twl4030_vibra_remove(struct platform_device
*pdev
)
261 struct vibra_info
*info
= platform_get_drvdata(pdev
);
263 /* this also free ff-memless and calls close if needed */
264 input_unregister_device(info
->input_dev
);
266 platform_set_drvdata(pdev
, NULL
);
271 static struct platform_driver twl4030_vibra_driver
= {
272 .probe
= twl4030_vibra_probe
,
273 .remove
= __devexit_p(twl4030_vibra_remove
),
275 .name
= "twl4030-vibra",
276 .owner
= THIS_MODULE
,
278 .pm
= &twl4030_vibra_pm_ops
,
283 static int __init
twl4030_vibra_init(void)
285 return platform_driver_register(&twl4030_vibra_driver
);
287 module_init(twl4030_vibra_init
);
289 static void __exit
twl4030_vibra_exit(void)
291 platform_driver_unregister(&twl4030_vibra_driver
);
293 module_exit(twl4030_vibra_exit
);
295 MODULE_ALIAS("platform:twl4030-vibra");
297 MODULE_DESCRIPTION("TWL4030 Vibra driver");
298 MODULE_LICENSE("GPL");
299 MODULE_AUTHOR("Nokia Corporation");