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-audio.h>
32 #include <linux/input.h>
33 #include <linux/slab.h>
39 #define EFFECT_DIR_180_DEG 0x8000 /* range is 0 - 0xFFFF */
43 struct input_dev
*input_dev
;
45 struct workqueue_struct
*workqueue
;
46 struct work_struct play_work
;
55 static void vibra_disable_leds(void)
59 /* Disable LEDA & LEDB, cannot be used with vibra (PWM) */
60 twl_i2c_read_u8(TWL4030_MODULE_LED
, ®
, LEDEN
);
62 twl_i2c_write_u8(TWL4030_MODULE_LED
, LEDEN
, reg
);
65 /* Powers H-Bridge and enables audio clk */
66 static void vibra_enable(struct vibra_info
*info
)
70 twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER
);
72 /* turn H-Bridge on */
73 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
,
74 ®
, TWL4030_REG_VIBRA_CTL
);
75 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
76 (reg
| TWL4030_VIBRA_EN
), TWL4030_REG_VIBRA_CTL
);
78 twl4030_audio_enable_resource(TWL4030_AUDIO_RES_APLL
);
83 static void vibra_disable(struct vibra_info
*info
)
87 /* Power down H-Bridge */
88 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
,
89 ®
, TWL4030_REG_VIBRA_CTL
);
90 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
91 (reg
& ~TWL4030_VIBRA_EN
), TWL4030_REG_VIBRA_CTL
);
93 twl4030_audio_disable_resource(TWL4030_AUDIO_RES_APLL
);
94 twl4030_audio_disable_resource(TWL4030_AUDIO_RES_POWER
);
96 info
->enabled
= false;
99 static void vibra_play_work(struct work_struct
*work
)
101 struct vibra_info
*info
= container_of(work
,
102 struct vibra_info
, play_work
);
107 dir
= info
->direction
;
110 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
,
111 ®
, TWL4030_REG_VIBRA_CTL
);
112 if (pwm
&& (!info
->coexist
|| !(reg
& TWL4030_VIBRA_SEL
))) {
117 /* set vibra rotation direction */
118 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE
,
119 ®
, TWL4030_REG_VIBRA_CTL
);
120 reg
= (dir
) ? (reg
| TWL4030_VIBRA_DIR
) :
121 (reg
& ~TWL4030_VIBRA_DIR
);
122 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
123 reg
, TWL4030_REG_VIBRA_CTL
);
125 /* set PWM, 1 = max, 255 = min */
126 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE
,
127 256 - pwm
, TWL4030_REG_VIBRA_SET
);
134 /*** Input/ForceFeedback ***/
136 static int vibra_play(struct input_dev
*input
, void *data
,
137 struct ff_effect
*effect
)
139 struct vibra_info
*info
= input_get_drvdata(input
);
141 info
->speed
= effect
->u
.rumble
.strong_magnitude
>> 8;
143 info
->speed
= effect
->u
.rumble
.weak_magnitude
>> 9;
144 info
->direction
= effect
->direction
< EFFECT_DIR_180_DEG
? 0 : 1;
145 queue_work(info
->workqueue
, &info
->play_work
);
149 static int twl4030_vibra_open(struct input_dev
*input
)
151 struct vibra_info
*info
= input_get_drvdata(input
);
153 info
->workqueue
= create_singlethread_workqueue("vibra");
154 if (info
->workqueue
== NULL
) {
155 dev_err(&input
->dev
, "couldn't create workqueue\n");
161 static void twl4030_vibra_close(struct input_dev
*input
)
163 struct vibra_info
*info
= input_get_drvdata(input
);
165 cancel_work_sync(&info
->play_work
);
166 INIT_WORK(&info
->play_work
, vibra_play_work
); /* cleanup */
167 destroy_workqueue(info
->workqueue
);
168 info
->workqueue
= NULL
;
176 static int twl4030_vibra_suspend(struct device
*dev
)
178 struct platform_device
*pdev
= to_platform_device(dev
);
179 struct vibra_info
*info
= platform_get_drvdata(pdev
);
187 static int twl4030_vibra_resume(struct device
*dev
)
189 vibra_disable_leds();
194 static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops
,
195 twl4030_vibra_suspend
, twl4030_vibra_resume
);
197 static int __devinit
twl4030_vibra_probe(struct platform_device
*pdev
)
199 struct twl4030_vibra_data
*pdata
= pdev
->dev
.platform_data
;
200 struct vibra_info
*info
;
204 dev_dbg(&pdev
->dev
, "platform_data not available\n");
208 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
212 info
->dev
= &pdev
->dev
;
213 info
->coexist
= pdata
->coexist
;
214 INIT_WORK(&info
->play_work
, vibra_play_work
);
216 info
->input_dev
= input_allocate_device();
217 if (info
->input_dev
== NULL
) {
218 dev_err(&pdev
->dev
, "couldn't allocate input device\n");
223 input_set_drvdata(info
->input_dev
, info
);
225 info
->input_dev
->name
= "twl4030:vibrator";
226 info
->input_dev
->id
.version
= 1;
227 info
->input_dev
->dev
.parent
= pdev
->dev
.parent
;
228 info
->input_dev
->open
= twl4030_vibra_open
;
229 info
->input_dev
->close
= twl4030_vibra_close
;
230 __set_bit(FF_RUMBLE
, info
->input_dev
->ffbit
);
232 ret
= input_ff_create_memless(info
->input_dev
, NULL
, vibra_play
);
234 dev_dbg(&pdev
->dev
, "couldn't register vibrator to FF\n");
238 ret
= input_register_device(info
->input_dev
);
240 dev_dbg(&pdev
->dev
, "couldn't register input device\n");
244 vibra_disable_leds();
246 platform_set_drvdata(pdev
, info
);
250 input_ff_destroy(info
->input_dev
);
252 input_free_device(info
->input_dev
);
258 static int __devexit
twl4030_vibra_remove(struct platform_device
*pdev
)
260 struct vibra_info
*info
= platform_get_drvdata(pdev
);
262 /* this also free ff-memless and calls close if needed */
263 input_unregister_device(info
->input_dev
);
265 platform_set_drvdata(pdev
, NULL
);
270 static struct platform_driver twl4030_vibra_driver
= {
271 .probe
= twl4030_vibra_probe
,
272 .remove
= __devexit_p(twl4030_vibra_remove
),
274 .name
= "twl4030-vibra",
275 .owner
= THIS_MODULE
,
276 .pm
= &twl4030_vibra_pm_ops
,
279 module_platform_driver(twl4030_vibra_driver
);
281 MODULE_ALIAS("platform:twl4030-vibra");
282 MODULE_DESCRIPTION("TWL4030 Vibra driver");
283 MODULE_LICENSE("GPL");
284 MODULE_AUTHOR("Nokia Corporation");