2 * twl6040-vibra.c - TWL6040 Vibrator driver
4 * Author: Jorge Eduardo Candelaria <jorge.candelaria@ti.com>
5 * Author: Misael Lopez Cruz <misael.lopez@ti.com>
7 * Copyright: (C) 2011 Texas Instruments, Inc.
9 * Based on twl4030-vibra.c by Henrik Saari <henrik.saari@nokia.com>
10 * Felipe Balbi <felipe.balbi@nokia.com>
11 * Jari Vanhala <ext-javi.vanhala@nokia.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/workqueue.h>
31 #include <linux/input.h>
32 #include <linux/mfd/twl6040.h>
33 #include <linux/slab.h>
34 #include <linux/delay.h>
35 #include <linux/regulator/consumer.h>
37 #define EFFECT_DIR_180_DEG 0x8000
39 /* Recommended modulation index 85% */
40 #define TWL6040_VIBRA_MOD 85
42 #define TWL6040_NUM_SUPPLIES 2
46 struct input_dev
*input_dev
;
47 struct workqueue_struct
*workqueue
;
48 struct work_struct play_work
;
57 unsigned int vibldrv_res
;
58 unsigned int vibrdrv_res
;
59 unsigned int viblmotor_res
;
60 unsigned int vibrmotor_res
;
62 struct regulator_bulk_data supplies
[TWL6040_NUM_SUPPLIES
];
64 struct twl6040
*twl6040
;
67 static irqreturn_t
twl6040_vib_irq_handler(int irq
, void *data
)
69 struct vibra_info
*info
= data
;
70 struct twl6040
*twl6040
= info
->twl6040
;
73 status
= twl6040_reg_read(twl6040
, TWL6040_REG_STATUS
);
74 if (status
& TWL6040_VIBLOCDET
) {
75 dev_warn(info
->dev
, "Left Vibrator overcurrent detected\n");
76 twl6040_clear_bits(twl6040
, TWL6040_REG_VIBCTLL
,
79 if (status
& TWL6040_VIBROCDET
) {
80 dev_warn(info
->dev
, "Right Vibrator overcurrent detected\n");
81 twl6040_clear_bits(twl6040
, TWL6040_REG_VIBCTLR
,
88 static void twl6040_vibra_enable(struct vibra_info
*info
)
90 struct twl6040
*twl6040
= info
->twl6040
;
93 ret
= regulator_bulk_enable(ARRAY_SIZE(info
->supplies
), info
->supplies
);
95 dev_err(info
->dev
, "failed to enable regulators %d\n", ret
);
99 twl6040_power(info
->twl6040
, 1);
100 if (twl6040_get_revid(twl6040
) <= TWL6040_REV_ES1_1
) {
102 * ERRATA: Disable overcurrent protection for at least
103 * 3ms when enabling vibrator drivers to avoid false
104 * overcurrent detection
106 twl6040_reg_write(twl6040
, TWL6040_REG_VIBCTLL
,
107 TWL6040_VIBENA
| TWL6040_VIBCTRL
);
108 twl6040_reg_write(twl6040
, TWL6040_REG_VIBCTLR
,
109 TWL6040_VIBENA
| TWL6040_VIBCTRL
);
110 usleep_range(3000, 3500);
113 twl6040_reg_write(twl6040
, TWL6040_REG_VIBCTLL
,
115 twl6040_reg_write(twl6040
, TWL6040_REG_VIBCTLR
,
118 info
->enabled
= true;
121 static void twl6040_vibra_disable(struct vibra_info
*info
)
123 struct twl6040
*twl6040
= info
->twl6040
;
125 twl6040_reg_write(twl6040
, TWL6040_REG_VIBCTLL
, 0x00);
126 twl6040_reg_write(twl6040
, TWL6040_REG_VIBCTLR
, 0x00);
127 twl6040_power(info
->twl6040
, 0);
129 regulator_bulk_disable(ARRAY_SIZE(info
->supplies
), info
->supplies
);
131 info
->enabled
= false;
134 static u8
twl6040_vibra_code(int vddvib
, int vibdrv_res
, int motor_res
,
135 int speed
, int direction
)
141 vpk
= (vddvib
* motor_res
* TWL6040_VIBRA_MOD
) /
142 (100 * (vibdrv_res
+ motor_res
));
144 /* 50mV per VIBDAT code step */
146 if (max_code
> TWL6040_VIBDAT_MAX
)
147 max_code
= TWL6040_VIBDAT_MAX
;
149 /* scale speed to max allowed code */
150 vibdat
= (u8
)((speed
* max_code
) / USHRT_MAX
);
152 /* 2's complement for direction > 180 degrees */
158 static void twl6040_vibra_set_effect(struct vibra_info
*info
)
160 struct twl6040
*twl6040
= info
->twl6040
;
165 volt
= regulator_get_voltage(info
->supplies
[0].consumer
) / 1000;
166 vibdatl
= twl6040_vibra_code(volt
, info
->vibldrv_res
,
168 info
->weak_speed
, info
->direction
);
171 volt
= regulator_get_voltage(info
->supplies
[1].consumer
) / 1000;
172 vibdatr
= twl6040_vibra_code(volt
, info
->vibrdrv_res
,
174 info
->strong_speed
, info
->direction
);
176 twl6040_reg_write(twl6040
, TWL6040_REG_VIBDATL
, vibdatl
);
177 twl6040_reg_write(twl6040
, TWL6040_REG_VIBDATR
, vibdatr
);
180 static void vibra_play_work(struct work_struct
*work
)
182 struct vibra_info
*info
= container_of(work
,
183 struct vibra_info
, play_work
);
185 mutex_lock(&info
->mutex
);
187 if (info
->weak_speed
|| info
->strong_speed
) {
189 twl6040_vibra_enable(info
);
191 twl6040_vibra_set_effect(info
);
192 } else if (info
->enabled
)
193 twl6040_vibra_disable(info
);
195 mutex_unlock(&info
->mutex
);
198 static int vibra_play(struct input_dev
*input
, void *data
,
199 struct ff_effect
*effect
)
201 struct vibra_info
*info
= input_get_drvdata(input
);
204 /* Do not allow effect, while the routing is set to use audio */
205 ret
= twl6040_get_vibralr_status(info
->twl6040
);
206 if (ret
& TWL6040_VIBSEL
) {
207 dev_info(&input
->dev
, "Vibra is configured for audio\n");
211 info
->weak_speed
= effect
->u
.rumble
.weak_magnitude
;
212 info
->strong_speed
= effect
->u
.rumble
.strong_magnitude
;
213 info
->direction
= effect
->direction
< EFFECT_DIR_180_DEG
? 1 : -1;
215 ret
= queue_work(info
->workqueue
, &info
->play_work
);
217 dev_info(&input
->dev
, "work is already on queue\n");
224 static void twl6040_vibra_close(struct input_dev
*input
)
226 struct vibra_info
*info
= input_get_drvdata(input
);
228 cancel_work_sync(&info
->play_work
);
230 mutex_lock(&info
->mutex
);
233 twl6040_vibra_disable(info
);
235 mutex_unlock(&info
->mutex
);
238 #ifdef CONFIG_PM_SLEEP
239 static int twl6040_vibra_suspend(struct device
*dev
)
241 struct platform_device
*pdev
= to_platform_device(dev
);
242 struct vibra_info
*info
= platform_get_drvdata(pdev
);
244 mutex_lock(&info
->mutex
);
247 twl6040_vibra_disable(info
);
249 mutex_unlock(&info
->mutex
);
256 static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops
, twl6040_vibra_suspend
, NULL
);
258 static int __devinit
twl6040_vibra_probe(struct platform_device
*pdev
)
260 struct twl6040_vibra_data
*pdata
= pdev
->dev
.platform_data
;
261 struct vibra_info
*info
;
265 dev_err(&pdev
->dev
, "platform_data not available\n");
269 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
271 dev_err(&pdev
->dev
, "couldn't allocate memory\n");
275 info
->dev
= &pdev
->dev
;
276 info
->twl6040
= dev_get_drvdata(pdev
->dev
.parent
);
277 info
->vibldrv_res
= pdata
->vibldrv_res
;
278 info
->vibrdrv_res
= pdata
->vibrdrv_res
;
279 info
->viblmotor_res
= pdata
->viblmotor_res
;
280 info
->vibrmotor_res
= pdata
->vibrmotor_res
;
281 if ((!info
->vibldrv_res
&& !info
->viblmotor_res
) ||
282 (!info
->vibrdrv_res
&& !info
->vibrmotor_res
)) {
283 dev_err(info
->dev
, "invalid vibra driver/motor resistance\n");
288 info
->irq
= platform_get_irq(pdev
, 0);
290 dev_err(info
->dev
, "invalid irq\n");
295 mutex_init(&info
->mutex
);
297 info
->input_dev
= input_allocate_device();
298 if (info
->input_dev
== NULL
) {
299 dev_err(info
->dev
, "couldn't allocate input device\n");
304 input_set_drvdata(info
->input_dev
, info
);
306 info
->input_dev
->name
= "twl6040:vibrator";
307 info
->input_dev
->id
.version
= 1;
308 info
->input_dev
->dev
.parent
= pdev
->dev
.parent
;
309 info
->input_dev
->close
= twl6040_vibra_close
;
310 __set_bit(FF_RUMBLE
, info
->input_dev
->ffbit
);
312 ret
= input_ff_create_memless(info
->input_dev
, NULL
, vibra_play
);
314 dev_err(info
->dev
, "couldn't register vibrator to FF\n");
318 ret
= input_register_device(info
->input_dev
);
320 dev_err(info
->dev
, "couldn't register input device\n");
324 platform_set_drvdata(pdev
, info
);
326 ret
= request_threaded_irq(info
->irq
, NULL
, twl6040_vib_irq_handler
, 0,
327 "twl6040_irq_vib", info
);
329 dev_err(info
->dev
, "VIB IRQ request failed: %d\n", ret
);
333 info
->supplies
[0].supply
= "vddvibl";
334 info
->supplies
[1].supply
= "vddvibr";
335 ret
= regulator_bulk_get(info
->dev
, ARRAY_SIZE(info
->supplies
),
338 dev_err(info
->dev
, "couldn't get regulators %d\n", ret
);
342 if (pdata
->vddvibl_uV
) {
343 ret
= regulator_set_voltage(info
->supplies
[0].consumer
,
347 dev_err(info
->dev
, "failed to set VDDVIBL volt %d\n",
353 if (pdata
->vddvibr_uV
) {
354 ret
= regulator_set_voltage(info
->supplies
[1].consumer
,
358 dev_err(info
->dev
, "failed to set VDDVIBR volt %d\n",
364 info
->workqueue
= alloc_workqueue("twl6040-vibra", 0, 0);
365 if (info
->workqueue
== NULL
) {
366 dev_err(info
->dev
, "couldn't create workqueue\n");
370 INIT_WORK(&info
->play_work
, vibra_play_work
);
375 regulator_bulk_free(ARRAY_SIZE(info
->supplies
), info
->supplies
);
377 free_irq(info
->irq
, info
);
379 input_unregister_device(info
->input_dev
);
380 info
->input_dev
= NULL
;
383 input_ff_destroy(info
->input_dev
);
385 input_free_device(info
->input_dev
);
391 static int __devexit
twl6040_vibra_remove(struct platform_device
*pdev
)
393 struct vibra_info
*info
= platform_get_drvdata(pdev
);
395 input_unregister_device(info
->input_dev
);
396 free_irq(info
->irq
, info
);
397 regulator_bulk_free(ARRAY_SIZE(info
->supplies
), info
->supplies
);
398 destroy_workqueue(info
->workqueue
);
404 static struct platform_driver twl6040_vibra_driver
= {
405 .probe
= twl6040_vibra_probe
,
406 .remove
= __devexit_p(twl6040_vibra_remove
),
408 .name
= "twl6040-vibra",
409 .owner
= THIS_MODULE
,
410 .pm
= &twl6040_vibra_pm_ops
,
413 module_platform_driver(twl6040_vibra_driver
);
415 MODULE_ALIAS("platform:twl6040-vibra");
416 MODULE_DESCRIPTION("TWL6040 Vibra driver");
417 MODULE_LICENSE("GPL");
418 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
419 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");