blob: 2194a3c7236addb20583026c3eb9c4075e6a57ea [file] [log] [blame]
Jari Vanhala3dd1b392010-03-09 00:29:46 -08001/*
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>
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +030029#include <linux/of.h>
Jari Vanhala3dd1b392010-03-09 00:29:46 -080030#include <linux/workqueue.h>
31#include <linux/i2c/twl.h>
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030032#include <linux/mfd/twl4030-audio.h>
Jari Vanhala3dd1b392010-03-09 00:29:46 -080033#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Jari Vanhala3dd1b392010-03-09 00:29:46 -080035
36/* MODULE ID2 */
37#define LEDEN 0x00
38
39/* ForceFeedback */
40#define EFFECT_DIR_180_DEG 0x8000 /* range is 0 - 0xFFFF */
41
42struct vibra_info {
43 struct device *dev;
44 struct input_dev *input_dev;
45
46 struct workqueue_struct *workqueue;
47 struct work_struct play_work;
48
49 bool enabled;
50 int speed;
51 int direction;
52
53 bool coexist;
54};
55
56static void vibra_disable_leds(void)
57{
58 u8 reg;
59
60 /* Disable LEDA & LEDB, cannot be used with vibra (PWM) */
61 twl_i2c_read_u8(TWL4030_MODULE_LED, &reg, LEDEN);
62 reg &= ~0x03;
63 twl_i2c_write_u8(TWL4030_MODULE_LED, LEDEN, reg);
64}
65
66/* Powers H-Bridge and enables audio clk */
67static void vibra_enable(struct vibra_info *info)
68{
69 u8 reg;
70
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030071 twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER);
Jari Vanhala3dd1b392010-03-09 00:29:46 -080072
73 /* turn H-Bridge on */
74 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
75 &reg, TWL4030_REG_VIBRA_CTL);
76 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
77 (reg | TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
78
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030079 twl4030_audio_enable_resource(TWL4030_AUDIO_RES_APLL);
Jari Vanhala3dd1b392010-03-09 00:29:46 -080080
81 info->enabled = true;
82}
83
84static void vibra_disable(struct vibra_info *info)
85{
86 u8 reg;
87
88 /* Power down H-Bridge */
89 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
90 &reg, TWL4030_REG_VIBRA_CTL);
91 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
92 (reg & ~TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
93
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030094 twl4030_audio_disable_resource(TWL4030_AUDIO_RES_APLL);
95 twl4030_audio_disable_resource(TWL4030_AUDIO_RES_POWER);
Jari Vanhala3dd1b392010-03-09 00:29:46 -080096
97 info->enabled = false;
98}
99
100static void vibra_play_work(struct work_struct *work)
101{
102 struct vibra_info *info = container_of(work,
103 struct vibra_info, play_work);
104 int dir;
105 int pwm;
106 u8 reg;
107
108 dir = info->direction;
109 pwm = info->speed;
110
111 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
112 &reg, TWL4030_REG_VIBRA_CTL);
113 if (pwm && (!info->coexist || !(reg & TWL4030_VIBRA_SEL))) {
114
115 if (!info->enabled)
116 vibra_enable(info);
117
118 /* set vibra rotation direction */
119 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
120 &reg, 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);
125
126 /* set PWM, 1 = max, 255 = min */
127 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
128 256 - pwm, TWL4030_REG_VIBRA_SET);
129 } else {
130 if (info->enabled)
131 vibra_disable(info);
132 }
133}
134
135/*** Input/ForceFeedback ***/
136
137static int vibra_play(struct input_dev *input, void *data,
138 struct ff_effect *effect)
139{
140 struct vibra_info *info = input_get_drvdata(input);
141
142 info->speed = effect->u.rumble.strong_magnitude >> 8;
143 if (!info->speed)
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);
147 return 0;
148}
149
150static int twl4030_vibra_open(struct input_dev *input)
151{
152 struct vibra_info *info = input_get_drvdata(input);
153
154 info->workqueue = create_singlethread_workqueue("vibra");
155 if (info->workqueue == NULL) {
156 dev_err(&input->dev, "couldn't create workqueue\n");
157 return -ENOMEM;
158 }
159 return 0;
160}
161
162static void twl4030_vibra_close(struct input_dev *input)
163{
164 struct vibra_info *info = input_get_drvdata(input);
165
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;
170
171 if (info->enabled)
172 vibra_disable(info);
173}
174
175/*** Module ***/
Dmitry Torokhov7a0a1df2012-03-11 16:02:06 -0700176#ifdef CONFIG_PM_SLEEP
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800177static int twl4030_vibra_suspend(struct device *dev)
178{
179 struct platform_device *pdev = to_platform_device(dev);
180 struct vibra_info *info = platform_get_drvdata(pdev);
181
182 if (info->enabled)
183 vibra_disable(info);
184
185 return 0;
186}
187
188static int twl4030_vibra_resume(struct device *dev)
189{
190 vibra_disable_leds();
191 return 0;
192}
Dmitry Torokhovf3761c02012-02-24 00:51:40 -0800193#endif
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800194
195static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
196 twl4030_vibra_suspend, twl4030_vibra_resume);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800197
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300198static bool twl4030_vibra_check_coexist(struct twl4030_vibra_data *pdata,
199 struct device_node *node)
200{
201 if (pdata && pdata->coexist)
202 return true;
203
204 if (of_find_node_by_name(node, "codec"))
205 return true;
206
207 return false;
208}
209
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800210static int __devinit twl4030_vibra_probe(struct platform_device *pdev)
211{
Peter Ujfalusi4ae6df52011-05-31 15:21:13 +0300212 struct twl4030_vibra_data *pdata = pdev->dev.platform_data;
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300213 struct device_node *twl4030_core_node = pdev->dev.parent->of_node;
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800214 struct vibra_info *info;
215 int ret;
216
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300217 if (!pdata && !twl4030_core_node) {
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800218 dev_dbg(&pdev->dev, "platform_data not available\n");
219 return -EINVAL;
220 }
221
222 info = kzalloc(sizeof(*info), GFP_KERNEL);
223 if (!info)
224 return -ENOMEM;
225
226 info->dev = &pdev->dev;
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300227 info->coexist = twl4030_vibra_check_coexist(pdata, twl4030_core_node);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800228 INIT_WORK(&info->play_work, vibra_play_work);
229
230 info->input_dev = input_allocate_device();
231 if (info->input_dev == NULL) {
232 dev_err(&pdev->dev, "couldn't allocate input device\n");
233 ret = -ENOMEM;
234 goto err_kzalloc;
235 }
236
237 input_set_drvdata(info->input_dev, info);
238
239 info->input_dev->name = "twl4030:vibrator";
240 info->input_dev->id.version = 1;
241 info->input_dev->dev.parent = pdev->dev.parent;
242 info->input_dev->open = twl4030_vibra_open;
243 info->input_dev->close = twl4030_vibra_close;
244 __set_bit(FF_RUMBLE, info->input_dev->ffbit);
245
246 ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
247 if (ret < 0) {
248 dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
249 goto err_ialloc;
250 }
251
252 ret = input_register_device(info->input_dev);
253 if (ret < 0) {
254 dev_dbg(&pdev->dev, "couldn't register input device\n");
255 goto err_iff;
256 }
257
258 vibra_disable_leds();
259
260 platform_set_drvdata(pdev, info);
261 return 0;
262
263err_iff:
264 input_ff_destroy(info->input_dev);
265err_ialloc:
266 input_free_device(info->input_dev);
267err_kzalloc:
268 kfree(info);
269 return ret;
270}
271
272static int __devexit twl4030_vibra_remove(struct platform_device *pdev)
273{
274 struct vibra_info *info = platform_get_drvdata(pdev);
275
276 /* this also free ff-memless and calls close if needed */
277 input_unregister_device(info->input_dev);
278 kfree(info);
279 platform_set_drvdata(pdev, NULL);
280
281 return 0;
282}
283
284static struct platform_driver twl4030_vibra_driver = {
285 .probe = twl4030_vibra_probe,
286 .remove = __devexit_p(twl4030_vibra_remove),
287 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000288 .name = "twl4030-vibra",
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800289 .owner = THIS_MODULE,
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800290 .pm = &twl4030_vibra_pm_ops,
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800291 },
292};
JJ Ding840a7462011-11-29 11:08:40 -0800293module_platform_driver(twl4030_vibra_driver);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800294
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000295MODULE_ALIAS("platform:twl4030-vibra");
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800296MODULE_DESCRIPTION("TWL4030 Vibra driver");
297MODULE_LICENSE("GPL");
298MODULE_AUTHOR("Nokia Corporation");