1 // SPDX-License-Identifier: GPL-2.0-only
3 * twl4030-vibra.c - TWL4030 Vibrator driver
5 * Copyright (C) 2008-2010 Nokia Corporation
7 * Written by Henrik Saari <henrik.saari@nokia.com>
8 * Updates by Felipe Balbi <felipe.balbi@nokia.com>
9 * Input by Jari Vanhala <ext-jari.vanhala@nokia.com>
12 #include <linux/module.h>
13 #include <linux/jiffies.h>
14 #include <linux/platform_device.h>
16 #include <linux/workqueue.h>
17 #include <linux/mfd/twl.h>
18 #include <linux/mfd/twl4030-audio.h>
19 #include <linux/input.h>
20 #include <linux/slab.h>
26 #define EFFECT_DIR_180_DEG 0x8000 /* range is 0 - 0xFFFF */
30 struct input_dev *input_dev;
32 struct work_struct play_work;
41 static void vibra_disable_leds(void)
45 /* Disable LEDA & LEDB, cannot be used with vibra (PWM) */
46 twl_i2c_read_u8(TWL4030_MODULE_LED, ®, LEDEN);
48 twl_i2c_write_u8(TWL4030_MODULE_LED, LEDEN, reg);
51 /* Powers H-Bridge and enables audio clk */
52 static void vibra_enable(struct vibra_info *info)
56 twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER);
58 /* turn H-Bridge on */
59 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
60 ®, TWL4030_REG_VIBRA_CTL);
61 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
62 (reg | TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
64 twl4030_audio_enable_resource(TWL4030_AUDIO_RES_APLL);
69 static void vibra_disable(struct vibra_info *info)
73 /* Power down H-Bridge */
74 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
75 ®, TWL4030_REG_VIBRA_CTL);
76 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
77 (reg & ~TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
79 twl4030_audio_disable_resource(TWL4030_AUDIO_RES_APLL);
80 twl4030_audio_disable_resource(TWL4030_AUDIO_RES_POWER);
82 info->enabled = false;
85 static void vibra_play_work(struct work_struct *work)
87 struct vibra_info *info = container_of(work,
88 struct vibra_info, play_work);
93 dir = info->direction;
96 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
97 ®, TWL4030_REG_VIBRA_CTL);
98 if (pwm && (!info->coexist || !(reg & TWL4030_VIBRA_SEL))) {
103 /* set vibra rotation direction */
104 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
105 ®, TWL4030_REG_VIBRA_CTL);
106 reg = (dir) ? (reg | TWL4030_VIBRA_DIR) :
107 (reg & ~TWL4030_VIBRA_DIR);
108 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
109 reg, TWL4030_REG_VIBRA_CTL);
111 /* set PWM, 1 = max, 255 = min */
112 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
113 256 - pwm, TWL4030_REG_VIBRA_SET);
120 /*** Input/ForceFeedback ***/
122 static int vibra_play(struct input_dev *input, void *data,
123 struct ff_effect *effect)
125 struct vibra_info *info = input_get_drvdata(input);
127 info->speed = effect->u.rumble.strong_magnitude >> 8;
129 info->speed = effect->u.rumble.weak_magnitude >> 9;
130 info->direction = effect->direction < EFFECT_DIR_180_DEG ? 0 : 1;
131 schedule_work(&info->play_work);
135 static void twl4030_vibra_close(struct input_dev *input)
137 struct vibra_info *info = input_get_drvdata(input);
139 cancel_work_sync(&info->play_work);
146 static int twl4030_vibra_suspend(struct device *dev)
148 struct platform_device *pdev = to_platform_device(dev);
149 struct vibra_info *info = platform_get_drvdata(pdev);
157 static int twl4030_vibra_resume(struct device *dev)
159 vibra_disable_leds();
163 static DEFINE_SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
164 twl4030_vibra_suspend, twl4030_vibra_resume);
166 static bool twl4030_vibra_check_coexist(struct device_node *parent)
168 struct device_node *node;
170 node = of_get_child_by_name(parent, "codec");
179 static int twl4030_vibra_probe(struct platform_device *pdev)
181 struct device_node *twl4030_core_node = pdev->dev.parent->of_node;
182 struct vibra_info *info;
185 if (!twl4030_core_node) {
186 dev_dbg(&pdev->dev, "twl4030 OF node is missing\n");
190 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
194 info->dev = &pdev->dev;
195 info->coexist = twl4030_vibra_check_coexist(twl4030_core_node);
196 INIT_WORK(&info->play_work, vibra_play_work);
198 info->input_dev = devm_input_allocate_device(&pdev->dev);
199 if (info->input_dev == NULL) {
200 dev_err(&pdev->dev, "couldn't allocate input device\n");
204 input_set_drvdata(info->input_dev, info);
206 info->input_dev->name = "twl4030:vibrator";
207 info->input_dev->id.version = 1;
208 info->input_dev->close = twl4030_vibra_close;
209 __set_bit(FF_RUMBLE, info->input_dev->ffbit);
211 ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
213 dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
217 ret = input_register_device(info->input_dev);
219 dev_dbg(&pdev->dev, "couldn't register input device\n");
223 vibra_disable_leds();
225 platform_set_drvdata(pdev, info);
229 input_ff_destroy(info->input_dev);
233 static struct platform_driver twl4030_vibra_driver = {
234 .probe = twl4030_vibra_probe,
236 .name = "twl4030-vibra",
237 .pm = pm_sleep_ptr(&twl4030_vibra_pm_ops),
240 module_platform_driver(twl4030_vibra_driver);
242 MODULE_ALIAS("platform:twl4030-vibra");
243 MODULE_DESCRIPTION("TWL4030 Vibra driver");
244 MODULE_LICENSE("GPL");
245 MODULE_AUTHOR("Nokia Corporation");