1 // SPDX-License-Identifier: GPL-2.0-only
3 * twl6040-vibra.c - TWL6040 Vibrator driver
8 * Copyright: (C) 2011 Texas Instruments, Inc.
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
17 #include <linux/workqueue.h>
18 #include <linux/input.h>
19 #include <linux/mfd/twl6040.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/regulator/consumer.h>
24 #define EFFECT_DIR_180_DEG 0x8000
26 /* Recommended modulation index 85% */
27 #define TWL6040_VIBRA_MOD 85
29 #define TWL6040_NUM_SUPPLIES 2
33 struct input_dev *input_dev;
34 struct work_struct play_work;
43 unsigned int vibldrv_res;
44 unsigned int vibrdrv_res;
45 unsigned int viblmotor_res;
46 unsigned int vibrmotor_res;
48 struct regulator_bulk_data supplies[TWL6040_NUM_SUPPLIES];
50 struct twl6040 *twl6040;
53 static irqreturn_t twl6040_vib_irq_handler(int irq, void *data)
55 struct vibra_info *info = data;
56 struct twl6040 *twl6040 = info->twl6040;
59 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
60 if (status & TWL6040_VIBLOCDET) {
61 dev_warn(info->dev, "Left Vibrator overcurrent detected\n");
62 twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLL,
65 if (status & TWL6040_VIBROCDET) {
66 dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
67 twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
74 static void twl6040_vibra_enable(struct vibra_info *info)
76 struct twl6040 *twl6040 = info->twl6040;
79 ret = regulator_bulk_enable(ARRAY_SIZE(info->supplies), info->supplies);
81 dev_err(info->dev, "failed to enable regulators %d\n", ret);
85 twl6040_power(info->twl6040, 1);
86 if (twl6040_get_revid(twl6040) <= TWL6040_REV_ES1_1) {
88 * ERRATA: Disable overcurrent protection for at least
89 * 3ms when enabling vibrator drivers to avoid false
90 * overcurrent detection
92 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
93 TWL6040_VIBENA | TWL6040_VIBCTRL);
94 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
95 TWL6040_VIBENA | TWL6040_VIBCTRL);
96 usleep_range(3000, 3500);
99 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
101 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
104 info->enabled = true;
107 static void twl6040_vibra_disable(struct vibra_info *info)
109 struct twl6040 *twl6040 = info->twl6040;
111 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, 0x00);
112 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, 0x00);
113 twl6040_power(info->twl6040, 0);
115 regulator_bulk_disable(ARRAY_SIZE(info->supplies), info->supplies);
117 info->enabled = false;
120 static u8 twl6040_vibra_code(int vddvib, int vibdrv_res, int motor_res,
121 int speed, int direction)
127 vpk = (vddvib * motor_res * TWL6040_VIBRA_MOD) /
128 (100 * (vibdrv_res + motor_res));
130 /* 50mV per VIBDAT code step */
132 if (max_code > TWL6040_VIBDAT_MAX)
133 max_code = TWL6040_VIBDAT_MAX;
135 /* scale speed to max allowed code */
136 vibdat = (u8)((speed * max_code) / USHRT_MAX);
138 /* 2's complement for direction > 180 degrees */
144 static void twl6040_vibra_set_effect(struct vibra_info *info)
146 struct twl6040 *twl6040 = info->twl6040;
151 volt = regulator_get_voltage(info->supplies[0].consumer) / 1000;
152 vibdatl = twl6040_vibra_code(volt, info->vibldrv_res,
154 info->weak_speed, info->direction);
157 volt = regulator_get_voltage(info->supplies[1].consumer) / 1000;
158 vibdatr = twl6040_vibra_code(volt, info->vibrdrv_res,
160 info->strong_speed, info->direction);
162 twl6040_reg_write(twl6040, TWL6040_REG_VIBDATL, vibdatl);
163 twl6040_reg_write(twl6040, TWL6040_REG_VIBDATR, vibdatr);
166 static void vibra_play_work(struct work_struct *work)
168 struct vibra_info *info = container_of(work,
169 struct vibra_info, play_work);
172 /* Do not allow effect, while the routing is set to use audio */
173 ret = twl6040_get_vibralr_status(info->twl6040);
174 if (ret & TWL6040_VIBSEL) {
175 dev_info(info->dev, "Vibra is configured for audio\n");
179 if (info->weak_speed || info->strong_speed) {
181 twl6040_vibra_enable(info);
183 twl6040_vibra_set_effect(info);
184 } else if (info->enabled)
185 twl6040_vibra_disable(info);
189 static int vibra_play(struct input_dev *input, void *data,
190 struct ff_effect *effect)
192 struct vibra_info *info = input_get_drvdata(input);
194 info->weak_speed = effect->u.rumble.weak_magnitude;
195 info->strong_speed = effect->u.rumble.strong_magnitude;
196 info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
198 schedule_work(&info->play_work);
203 static void twl6040_vibra_close(struct input_dev *input)
205 struct vibra_info *info = input_get_drvdata(input);
207 cancel_work_sync(&info->play_work);
210 twl6040_vibra_disable(info);
213 static int twl6040_vibra_suspend(struct device *dev)
215 struct platform_device *pdev = to_platform_device(dev);
216 struct vibra_info *info = platform_get_drvdata(pdev);
218 cancel_work_sync(&info->play_work);
221 twl6040_vibra_disable(info);
226 static DEFINE_SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops,
227 twl6040_vibra_suspend, NULL);
229 static int twl6040_vibra_probe(struct platform_device *pdev)
231 struct device *twl6040_core_dev = pdev->dev.parent;
232 struct vibra_info *info;
237 struct device_node *twl6040_core_node __free(device_node) =
238 of_get_child_by_name(twl6040_core_dev->of_node, "vibra");
239 if (!twl6040_core_node) {
240 dev_err(&pdev->dev, "parent of node is missing?\n");
244 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
246 dev_err(&pdev->dev, "couldn't allocate memory\n");
250 info->dev = &pdev->dev;
252 info->twl6040 = dev_get_drvdata(pdev->dev.parent);
254 of_property_read_u32(twl6040_core_node, "ti,vibldrv-res",
256 of_property_read_u32(twl6040_core_node, "ti,vibrdrv-res",
258 of_property_read_u32(twl6040_core_node, "ti,viblmotor-res",
259 &info->viblmotor_res);
260 of_property_read_u32(twl6040_core_node, "ti,vibrmotor-res",
261 &info->vibrmotor_res);
262 of_property_read_u32(twl6040_core_node, "ti,vddvibl-uV", &vddvibl_uV);
263 of_property_read_u32(twl6040_core_node, "ti,vddvibr-uV", &vddvibr_uV);
265 if ((!info->vibldrv_res && !info->viblmotor_res) ||
266 (!info->vibrdrv_res && !info->vibrmotor_res)) {
267 dev_err(info->dev, "invalid vibra driver/motor resistance\n");
271 info->irq = platform_get_irq(pdev, 0);
275 error = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
276 twl6040_vib_irq_handler,
278 "twl6040_irq_vib", info);
280 dev_err(info->dev, "VIB IRQ request failed: %d\n", error);
284 info->supplies[0].supply = "vddvibl";
285 info->supplies[1].supply = "vddvibr";
287 * When booted with Device tree the regulators are attached to the
288 * parent device (twl6040 MFD core)
290 error = devm_regulator_bulk_get(twl6040_core_dev,
291 ARRAY_SIZE(info->supplies),
294 dev_err(info->dev, "couldn't get regulators %d\n", error);
299 error = regulator_set_voltage(info->supplies[0].consumer,
300 vddvibl_uV, vddvibl_uV);
302 dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
309 error = regulator_set_voltage(info->supplies[1].consumer,
310 vddvibr_uV, vddvibr_uV);
312 dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
318 INIT_WORK(&info->play_work, vibra_play_work);
320 info->input_dev = devm_input_allocate_device(&pdev->dev);
321 if (!info->input_dev) {
322 dev_err(info->dev, "couldn't allocate input device\n");
326 input_set_drvdata(info->input_dev, info);
328 info->input_dev->name = "twl6040:vibrator";
329 info->input_dev->id.version = 1;
330 info->input_dev->close = twl6040_vibra_close;
331 __set_bit(FF_RUMBLE, info->input_dev->ffbit);
333 error = input_ff_create_memless(info->input_dev, NULL, vibra_play);
335 dev_err(info->dev, "couldn't register vibrator to FF\n");
339 error = input_register_device(info->input_dev);
341 dev_err(info->dev, "couldn't register input device\n");
345 platform_set_drvdata(pdev, info);
350 static struct platform_driver twl6040_vibra_driver = {
351 .probe = twl6040_vibra_probe,
353 .name = "twl6040-vibra",
354 .pm = pm_sleep_ptr(&twl6040_vibra_pm_ops),
357 module_platform_driver(twl6040_vibra_driver);
359 MODULE_ALIAS("platform:twl6040-vibra");
360 MODULE_DESCRIPTION("TWL6040 Vibra driver");
361 MODULE_LICENSE("GPL");