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/input.h>
38 #define EFFECT_DIR_180_DEG 0x8000 /* range is 0 - 0xFFFF */
42 struct input_dev
*input_dev
;
44 struct workqueue_struct
*workqueue
;
45 struct work_struct play_work
;
54 static void vibra_disable_leds(void)
58 /* Disable LEDA & LEDB, cannot be used with vibra (PWM) */
59 twl_i2c_read_u8(TWL4030_MODULE_LED
, ®
, LEDEN
);
61 twl_i2c_write_u8(TWL4030_MODULE_LED
, LEDEN
, reg
);
64 /* Powers H-Bridge and enables audio clk */
65 static void vibra_enable(struct vibra_info
*info
)
69 twl4030_codec_enable_resource(TWL4030_CODEC_RES_POWER
);
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
);
77 twl4030_codec_enable_resource(TWL4030_CODEC_RES_APLL
);
82 static void vibra_disable(struct vibra_info
*info
)
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
);
92 twl4030_codec_disable_resource(TWL4030_CODEC_RES_POWER
);
93 twl4030_codec_disable_resource(TWL4030_CODEC_RES_APLL
);
95 info
->enabled
= false;
98 static void vibra_play_work(struct work_struct
*work
)
100 struct vibra_info
*info
= container_of(work
,
101 struct vibra_info
, play_work
);
106 dir
= info
->direction
;
109 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
,
110 ®
, TWL4030_REG_VIBRA_CTL
);
111 if (pwm
&& (!info
->coexist
|| !(reg
& TWL4030_VIBRA_SEL
))) {
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
);
124 /* set PWM, 1 = max, 255 = min */
125 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
126 256 - pwm
, TWL4030_REG_VIBRA_SET
);
133 /*** Input/ForceFeedback ***/
135 static int vibra_play(struct input_dev
*input
, void *data
,
136 struct ff_effect
*effect
)
138 struct vibra_info
*info
= input_get_drvdata(input
);
140 info
->speed
= effect
->u
.rumble
.strong_magnitude
>> 8;
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
);
148 static int twl4030_vibra_open(struct input_dev
*input
)
150 struct vibra_info
*info
= input_get_drvdata(input
);
152 info
->workqueue
= create_singlethread_workqueue("vibra");
153 if (info
->workqueue
== NULL
) {
154 dev_err(&input
->dev
, "couldn't create workqueue\n");
160 static void twl4030_vibra_close(struct input_dev
*input
)
162 struct vibra_info
*info
= input_get_drvdata(input
);
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
;
175 static int twl4030_vibra_suspend(struct device
*dev
)
177 struct platform_device
*pdev
= to_platform_device(dev
);
178 struct vibra_info
*info
= platform_get_drvdata(pdev
);
186 static int twl4030_vibra_resume(struct device
*dev
)
188 vibra_disable_leds();
192 static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops
,
193 twl4030_vibra_suspend
, twl4030_vibra_resume
);
196 static int __devinit
twl4030_vibra_probe(struct platform_device
*pdev
)
198 struct twl4030_codec_vibra_data
*pdata
= pdev
->dev
.platform_data
;
199 struct vibra_info
*info
;
203 dev_dbg(&pdev
->dev
, "platform_data not available\n");
207 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
211 info
->dev
= &pdev
->dev
;
212 info
->coexist
= pdata
->coexist
;
213 INIT_WORK(&info
->play_work
, vibra_play_work
);
215 info
->input_dev
= input_allocate_device();
216 if (info
->input_dev
== NULL
) {
217 dev_err(&pdev
->dev
, "couldn't allocate input device\n");
222 input_set_drvdata(info
->input_dev
, info
);
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
);
231 ret
= input_ff_create_memless(info
->input_dev
, NULL
, vibra_play
);
233 dev_dbg(&pdev
->dev
, "couldn't register vibrator to FF\n");
237 ret
= input_register_device(info
->input_dev
);
239 dev_dbg(&pdev
->dev
, "couldn't register input device\n");
243 vibra_disable_leds();
245 platform_set_drvdata(pdev
, info
);
249 input_ff_destroy(info
->input_dev
);
251 input_free_device(info
->input_dev
);
257 static int __devexit
twl4030_vibra_remove(struct platform_device
*pdev
)
259 struct vibra_info
*info
= platform_get_drvdata(pdev
);
261 /* this also free ff-memless and calls close if needed */
262 input_unregister_device(info
->input_dev
);
264 platform_set_drvdata(pdev
, NULL
);
269 static struct platform_driver twl4030_vibra_driver
= {
270 .probe
= twl4030_vibra_probe
,
271 .remove
= __devexit_p(twl4030_vibra_remove
),
273 .name
= "twl4030_codec_vibra",
274 .owner
= THIS_MODULE
,
276 .pm
= &twl4030_vibra_pm_ops
,
281 static int __init
twl4030_vibra_init(void)
283 return platform_driver_register(&twl4030_vibra_driver
);
285 module_init(twl4030_vibra_init
);
287 static void __exit
twl4030_vibra_exit(void)
289 platform_driver_unregister(&twl4030_vibra_driver
);
291 module_exit(twl4030_vibra_exit
);
293 MODULE_ALIAS("platform:twl4030_codec_vibra");
295 MODULE_DESCRIPTION("TWL4030 Vibra driver");
296 MODULE_LICENSE("GPL");
297 MODULE_AUTHOR("Nokia Corporation");