tuntap: switch to use rtnl_dereference()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / misc / twl6040-vibra.c
blob71a28ee699f3ef52732e7cd601f26d1ed2eb72c0
1 /*
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
25 * 02110-1301 USA
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/of.h>
31 #include <linux/workqueue.h>
32 #include <linux/input.h>
33 #include <linux/mfd/twl6040.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/regulator/consumer.h>
38 #define EFFECT_DIR_180_DEG 0x8000
40 /* Recommended modulation index 85% */
41 #define TWL6040_VIBRA_MOD 85
43 #define TWL6040_NUM_SUPPLIES 2
45 struct vibra_info {
46 struct device *dev;
47 struct input_dev *input_dev;
48 struct workqueue_struct *workqueue;
49 struct work_struct play_work;
50 struct mutex mutex;
51 int irq;
53 bool enabled;
54 int weak_speed;
55 int strong_speed;
56 int direction;
58 unsigned int vibldrv_res;
59 unsigned int vibrdrv_res;
60 unsigned int viblmotor_res;
61 unsigned int vibrmotor_res;
63 struct regulator_bulk_data supplies[TWL6040_NUM_SUPPLIES];
65 struct twl6040 *twl6040;
68 static irqreturn_t twl6040_vib_irq_handler(int irq, void *data)
70 struct vibra_info *info = data;
71 struct twl6040 *twl6040 = info->twl6040;
72 u8 status;
74 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
75 if (status & TWL6040_VIBLOCDET) {
76 dev_warn(info->dev, "Left Vibrator overcurrent detected\n");
77 twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLL,
78 TWL6040_VIBENA);
80 if (status & TWL6040_VIBROCDET) {
81 dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
82 twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
83 TWL6040_VIBENA);
86 return IRQ_HANDLED;
89 static void twl6040_vibra_enable(struct vibra_info *info)
91 struct twl6040 *twl6040 = info->twl6040;
92 int ret;
94 ret = regulator_bulk_enable(ARRAY_SIZE(info->supplies), info->supplies);
95 if (ret) {
96 dev_err(info->dev, "failed to enable regulators %d\n", ret);
97 return;
100 twl6040_power(info->twl6040, 1);
101 if (twl6040_get_revid(twl6040) <= TWL6040_REV_ES1_1) {
103 * ERRATA: Disable overcurrent protection for at least
104 * 3ms when enabling vibrator drivers to avoid false
105 * overcurrent detection
107 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
108 TWL6040_VIBENA | TWL6040_VIBCTRL);
109 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
110 TWL6040_VIBENA | TWL6040_VIBCTRL);
111 usleep_range(3000, 3500);
114 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
115 TWL6040_VIBENA);
116 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
117 TWL6040_VIBENA);
119 info->enabled = true;
122 static void twl6040_vibra_disable(struct vibra_info *info)
124 struct twl6040 *twl6040 = info->twl6040;
126 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, 0x00);
127 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, 0x00);
128 twl6040_power(info->twl6040, 0);
130 regulator_bulk_disable(ARRAY_SIZE(info->supplies), info->supplies);
132 info->enabled = false;
135 static u8 twl6040_vibra_code(int vddvib, int vibdrv_res, int motor_res,
136 int speed, int direction)
138 int vpk, max_code;
139 u8 vibdat;
141 /* output swing */
142 vpk = (vddvib * motor_res * TWL6040_VIBRA_MOD) /
143 (100 * (vibdrv_res + motor_res));
145 /* 50mV per VIBDAT code step */
146 max_code = vpk / 50;
147 if (max_code > TWL6040_VIBDAT_MAX)
148 max_code = TWL6040_VIBDAT_MAX;
150 /* scale speed to max allowed code */
151 vibdat = (u8)((speed * max_code) / USHRT_MAX);
153 /* 2's complement for direction > 180 degrees */
154 vibdat *= direction;
156 return vibdat;
159 static void twl6040_vibra_set_effect(struct vibra_info *info)
161 struct twl6040 *twl6040 = info->twl6040;
162 u8 vibdatl, vibdatr;
163 int volt;
165 /* weak motor */
166 volt = regulator_get_voltage(info->supplies[0].consumer) / 1000;
167 vibdatl = twl6040_vibra_code(volt, info->vibldrv_res,
168 info->viblmotor_res,
169 info->weak_speed, info->direction);
171 /* strong motor */
172 volt = regulator_get_voltage(info->supplies[1].consumer) / 1000;
173 vibdatr = twl6040_vibra_code(volt, info->vibrdrv_res,
174 info->vibrmotor_res,
175 info->strong_speed, info->direction);
177 twl6040_reg_write(twl6040, TWL6040_REG_VIBDATL, vibdatl);
178 twl6040_reg_write(twl6040, TWL6040_REG_VIBDATR, vibdatr);
181 static void vibra_play_work(struct work_struct *work)
183 struct vibra_info *info = container_of(work,
184 struct vibra_info, play_work);
186 mutex_lock(&info->mutex);
188 if (info->weak_speed || info->strong_speed) {
189 if (!info->enabled)
190 twl6040_vibra_enable(info);
192 twl6040_vibra_set_effect(info);
193 } else if (info->enabled)
194 twl6040_vibra_disable(info);
196 mutex_unlock(&info->mutex);
199 static int vibra_play(struct input_dev *input, void *data,
200 struct ff_effect *effect)
202 struct vibra_info *info = input_get_drvdata(input);
203 int ret;
205 /* Do not allow effect, while the routing is set to use audio */
206 ret = twl6040_get_vibralr_status(info->twl6040);
207 if (ret & TWL6040_VIBSEL) {
208 dev_info(&input->dev, "Vibra is configured for audio\n");
209 return -EBUSY;
212 info->weak_speed = effect->u.rumble.weak_magnitude;
213 info->strong_speed = effect->u.rumble.strong_magnitude;
214 info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
216 ret = queue_work(info->workqueue, &info->play_work);
217 if (!ret) {
218 dev_info(&input->dev, "work is already on queue\n");
219 return ret;
222 return 0;
225 static void twl6040_vibra_close(struct input_dev *input)
227 struct vibra_info *info = input_get_drvdata(input);
229 cancel_work_sync(&info->play_work);
231 mutex_lock(&info->mutex);
233 if (info->enabled)
234 twl6040_vibra_disable(info);
236 mutex_unlock(&info->mutex);
239 #ifdef CONFIG_PM_SLEEP
240 static int twl6040_vibra_suspend(struct device *dev)
242 struct platform_device *pdev = to_platform_device(dev);
243 struct vibra_info *info = platform_get_drvdata(pdev);
245 mutex_lock(&info->mutex);
247 if (info->enabled)
248 twl6040_vibra_disable(info);
250 mutex_unlock(&info->mutex);
252 return 0;
254 #endif
256 static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL);
258 static int twl6040_vibra_probe(struct platform_device *pdev)
260 struct twl6040_vibra_data *pdata = pdev->dev.platform_data;
261 struct device *twl6040_core_dev = pdev->dev.parent;
262 struct device_node *twl6040_core_node = NULL;
263 struct vibra_info *info;
264 int vddvibl_uV = 0;
265 int vddvibr_uV = 0;
266 int ret;
268 #ifdef CONFIG_OF
269 twl6040_core_node = of_find_node_by_name(twl6040_core_dev->of_node,
270 "vibra");
271 #endif
273 if (!pdata && !twl6040_core_node) {
274 dev_err(&pdev->dev, "platform_data not available\n");
275 return -EINVAL;
278 info = kzalloc(sizeof(*info), GFP_KERNEL);
279 if (!info) {
280 dev_err(&pdev->dev, "couldn't allocate memory\n");
281 return -ENOMEM;
284 info->dev = &pdev->dev;
286 info->twl6040 = dev_get_drvdata(pdev->dev.parent);
287 if (pdata) {
288 info->vibldrv_res = pdata->vibldrv_res;
289 info->vibrdrv_res = pdata->vibrdrv_res;
290 info->viblmotor_res = pdata->viblmotor_res;
291 info->vibrmotor_res = pdata->vibrmotor_res;
292 vddvibl_uV = pdata->vddvibl_uV;
293 vddvibr_uV = pdata->vddvibr_uV;
294 } else {
295 of_property_read_u32(twl6040_core_node, "ti,vibldrv-res",
296 &info->vibldrv_res);
297 of_property_read_u32(twl6040_core_node, "ti,vibrdrv-res",
298 &info->vibrdrv_res);
299 of_property_read_u32(twl6040_core_node, "ti,viblmotor-res",
300 &info->viblmotor_res);
301 of_property_read_u32(twl6040_core_node, "ti,vibrmotor-res",
302 &info->vibrmotor_res);
303 of_property_read_u32(twl6040_core_node, "ti,vddvibl-uV",
304 &vddvibl_uV);
305 of_property_read_u32(twl6040_core_node, "ti,vddvibr-uV",
306 &vddvibr_uV);
309 if ((!info->vibldrv_res && !info->viblmotor_res) ||
310 (!info->vibrdrv_res && !info->vibrmotor_res)) {
311 dev_err(info->dev, "invalid vibra driver/motor resistance\n");
312 ret = -EINVAL;
313 goto err_kzalloc;
316 info->irq = platform_get_irq(pdev, 0);
317 if (info->irq < 0) {
318 dev_err(info->dev, "invalid irq\n");
319 ret = -EINVAL;
320 goto err_kzalloc;
323 mutex_init(&info->mutex);
325 info->input_dev = input_allocate_device();
326 if (info->input_dev == NULL) {
327 dev_err(info->dev, "couldn't allocate input device\n");
328 ret = -ENOMEM;
329 goto err_kzalloc;
332 input_set_drvdata(info->input_dev, info);
334 info->input_dev->name = "twl6040:vibrator";
335 info->input_dev->id.version = 1;
336 info->input_dev->dev.parent = pdev->dev.parent;
337 info->input_dev->close = twl6040_vibra_close;
338 __set_bit(FF_RUMBLE, info->input_dev->ffbit);
340 ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
341 if (ret < 0) {
342 dev_err(info->dev, "couldn't register vibrator to FF\n");
343 goto err_ialloc;
346 ret = input_register_device(info->input_dev);
347 if (ret < 0) {
348 dev_err(info->dev, "couldn't register input device\n");
349 goto err_iff;
352 platform_set_drvdata(pdev, info);
354 ret = request_threaded_irq(info->irq, NULL, twl6040_vib_irq_handler, 0,
355 "twl6040_irq_vib", info);
356 if (ret) {
357 dev_err(info->dev, "VIB IRQ request failed: %d\n", ret);
358 goto err_irq;
361 info->supplies[0].supply = "vddvibl";
362 info->supplies[1].supply = "vddvibr";
364 * When booted with Device tree the regulators are attached to the
365 * parent device (twl6040 MFD core)
367 ret = regulator_bulk_get(pdata ? info->dev : twl6040_core_dev,
368 ARRAY_SIZE(info->supplies), info->supplies);
369 if (ret) {
370 dev_err(info->dev, "couldn't get regulators %d\n", ret);
371 goto err_regulator;
374 if (vddvibl_uV) {
375 ret = regulator_set_voltage(info->supplies[0].consumer,
376 vddvibl_uV, vddvibl_uV);
377 if (ret) {
378 dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
379 ret);
380 goto err_voltage;
384 if (vddvibr_uV) {
385 ret = regulator_set_voltage(info->supplies[1].consumer,
386 vddvibr_uV, vddvibr_uV);
387 if (ret) {
388 dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
389 ret);
390 goto err_voltage;
394 info->workqueue = alloc_workqueue("twl6040-vibra", 0, 0);
395 if (info->workqueue == NULL) {
396 dev_err(info->dev, "couldn't create workqueue\n");
397 ret = -ENOMEM;
398 goto err_voltage;
400 INIT_WORK(&info->play_work, vibra_play_work);
402 return 0;
404 err_voltage:
405 regulator_bulk_free(ARRAY_SIZE(info->supplies), info->supplies);
406 err_regulator:
407 free_irq(info->irq, info);
408 err_irq:
409 input_unregister_device(info->input_dev);
410 info->input_dev = NULL;
411 err_iff:
412 if (info->input_dev)
413 input_ff_destroy(info->input_dev);
414 err_ialloc:
415 input_free_device(info->input_dev);
416 err_kzalloc:
417 kfree(info);
418 return ret;
421 static int twl6040_vibra_remove(struct platform_device *pdev)
423 struct vibra_info *info = platform_get_drvdata(pdev);
425 input_unregister_device(info->input_dev);
426 free_irq(info->irq, info);
427 regulator_bulk_free(ARRAY_SIZE(info->supplies), info->supplies);
428 destroy_workqueue(info->workqueue);
429 kfree(info);
431 return 0;
434 static struct platform_driver twl6040_vibra_driver = {
435 .probe = twl6040_vibra_probe,
436 .remove = twl6040_vibra_remove,
437 .driver = {
438 .name = "twl6040-vibra",
439 .owner = THIS_MODULE,
440 .pm = &twl6040_vibra_pm_ops,
443 module_platform_driver(twl6040_vibra_driver);
445 MODULE_ALIAS("platform:twl6040-vibra");
446 MODULE_DESCRIPTION("TWL6040 Vibra driver");
447 MODULE_LICENSE("GPL");
448 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
449 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");