12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 23e5b0851SSebastian Reichel /* 33e5b0851SSebastian Reichel * PWM vibrator driver 43e5b0851SSebastian Reichel * 53e5b0851SSebastian Reichel * Copyright (C) 2017 Collabora Ltd. 63e5b0851SSebastian Reichel * 73e5b0851SSebastian Reichel * Based on previous work from: 83e5b0851SSebastian Reichel * Copyright (C) 2012 Dmitry Torokhov <dmitry.torokhov@gmail.com> 93e5b0851SSebastian Reichel * 103e5b0851SSebastian Reichel * Based on PWM beeper driver: 113e5b0851SSebastian Reichel * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de> 123e5b0851SSebastian Reichel */ 133e5b0851SSebastian Reichel 143e5b0851SSebastian Reichel #include <linux/input.h> 153e5b0851SSebastian Reichel #include <linux/kernel.h> 163e5b0851SSebastian Reichel #include <linux/module.h> 173e5b0851SSebastian Reichel #include <linux/of_device.h> 183e5b0851SSebastian Reichel #include <linux/platform_device.h> 193e5b0851SSebastian Reichel #include <linux/property.h> 203e5b0851SSebastian Reichel #include <linux/pwm.h> 213e5b0851SSebastian Reichel #include <linux/regulator/consumer.h> 223e5b0851SSebastian Reichel #include <linux/slab.h> 233e5b0851SSebastian Reichel 243e5b0851SSebastian Reichel struct pwm_vibrator { 253e5b0851SSebastian Reichel struct input_dev *input; 263e5b0851SSebastian Reichel struct pwm_device *pwm; 273e5b0851SSebastian Reichel struct pwm_device *pwm_dir; 283e5b0851SSebastian Reichel struct regulator *vcc; 293e5b0851SSebastian Reichel 303e5b0851SSebastian Reichel struct work_struct play_work; 313e5b0851SSebastian Reichel u16 level; 323e5b0851SSebastian Reichel u32 direction_duty_cycle; 333ca232dfSJonathan Bakker bool vcc_on; 343e5b0851SSebastian Reichel }; 353e5b0851SSebastian Reichel 363e5b0851SSebastian Reichel static int pwm_vibrator_start(struct pwm_vibrator *vibrator) 373e5b0851SSebastian Reichel { 383e5b0851SSebastian Reichel struct device *pdev = vibrator->input->dev.parent; 393e5b0851SSebastian Reichel struct pwm_state state; 403e5b0851SSebastian Reichel int err; 413e5b0851SSebastian Reichel 423ca232dfSJonathan Bakker if (!vibrator->vcc_on) { 433e5b0851SSebastian Reichel err = regulator_enable(vibrator->vcc); 443e5b0851SSebastian Reichel if (err) { 45*29ebf697SLuca Weiss dev_err(pdev, "failed to enable regulator: %d\n", err); 463e5b0851SSebastian Reichel return err; 473e5b0851SSebastian Reichel } 483ca232dfSJonathan Bakker vibrator->vcc_on = true; 493ca232dfSJonathan Bakker } 503e5b0851SSebastian Reichel 513e5b0851SSebastian Reichel pwm_get_state(vibrator->pwm, &state); 523e5b0851SSebastian Reichel pwm_set_relative_duty_cycle(&state, vibrator->level, 0xffff); 533e5b0851SSebastian Reichel state.enabled = true; 543e5b0851SSebastian Reichel 553e5b0851SSebastian Reichel err = pwm_apply_state(vibrator->pwm, &state); 563e5b0851SSebastian Reichel if (err) { 57*29ebf697SLuca Weiss dev_err(pdev, "failed to apply pwm state: %d\n", err); 583e5b0851SSebastian Reichel return err; 593e5b0851SSebastian Reichel } 603e5b0851SSebastian Reichel 613e5b0851SSebastian Reichel if (vibrator->pwm_dir) { 623e5b0851SSebastian Reichel pwm_get_state(vibrator->pwm_dir, &state); 633e5b0851SSebastian Reichel state.duty_cycle = vibrator->direction_duty_cycle; 643e5b0851SSebastian Reichel state.enabled = true; 653e5b0851SSebastian Reichel 663e5b0851SSebastian Reichel err = pwm_apply_state(vibrator->pwm_dir, &state); 673e5b0851SSebastian Reichel if (err) { 68*29ebf697SLuca Weiss dev_err(pdev, "failed to apply dir-pwm state: %d\n", err); 693e5b0851SSebastian Reichel pwm_disable(vibrator->pwm); 703e5b0851SSebastian Reichel return err; 713e5b0851SSebastian Reichel } 723e5b0851SSebastian Reichel } 733e5b0851SSebastian Reichel 743e5b0851SSebastian Reichel return 0; 753e5b0851SSebastian Reichel } 763e5b0851SSebastian Reichel 773e5b0851SSebastian Reichel static void pwm_vibrator_stop(struct pwm_vibrator *vibrator) 783e5b0851SSebastian Reichel { 7994803aefSPaweł Chmiel if (vibrator->pwm_dir) 8094803aefSPaweł Chmiel pwm_disable(vibrator->pwm_dir); 8194803aefSPaweł Chmiel pwm_disable(vibrator->pwm); 8294803aefSPaweł Chmiel 833ca232dfSJonathan Bakker if (vibrator->vcc_on) { 843e5b0851SSebastian Reichel regulator_disable(vibrator->vcc); 853ca232dfSJonathan Bakker vibrator->vcc_on = false; 863ca232dfSJonathan Bakker } 873e5b0851SSebastian Reichel } 883e5b0851SSebastian Reichel 893e5b0851SSebastian Reichel static void pwm_vibrator_play_work(struct work_struct *work) 903e5b0851SSebastian Reichel { 913e5b0851SSebastian Reichel struct pwm_vibrator *vibrator = container_of(work, 923e5b0851SSebastian Reichel struct pwm_vibrator, play_work); 933e5b0851SSebastian Reichel 943e5b0851SSebastian Reichel if (vibrator->level) 953e5b0851SSebastian Reichel pwm_vibrator_start(vibrator); 963e5b0851SSebastian Reichel else 973e5b0851SSebastian Reichel pwm_vibrator_stop(vibrator); 983e5b0851SSebastian Reichel } 993e5b0851SSebastian Reichel 1003e5b0851SSebastian Reichel static int pwm_vibrator_play_effect(struct input_dev *dev, void *data, 1013e5b0851SSebastian Reichel struct ff_effect *effect) 1023e5b0851SSebastian Reichel { 1033e5b0851SSebastian Reichel struct pwm_vibrator *vibrator = input_get_drvdata(dev); 1043e5b0851SSebastian Reichel 1053e5b0851SSebastian Reichel vibrator->level = effect->u.rumble.strong_magnitude; 1063e5b0851SSebastian Reichel if (!vibrator->level) 1073e5b0851SSebastian Reichel vibrator->level = effect->u.rumble.weak_magnitude; 1083e5b0851SSebastian Reichel 1093e5b0851SSebastian Reichel schedule_work(&vibrator->play_work); 1103e5b0851SSebastian Reichel 1113e5b0851SSebastian Reichel return 0; 1123e5b0851SSebastian Reichel } 1133e5b0851SSebastian Reichel 1143e5b0851SSebastian Reichel static void pwm_vibrator_close(struct input_dev *input) 1153e5b0851SSebastian Reichel { 1163e5b0851SSebastian Reichel struct pwm_vibrator *vibrator = input_get_drvdata(input); 1173e5b0851SSebastian Reichel 1183e5b0851SSebastian Reichel cancel_work_sync(&vibrator->play_work); 1193e5b0851SSebastian Reichel pwm_vibrator_stop(vibrator); 1203e5b0851SSebastian Reichel } 1213e5b0851SSebastian Reichel 1223e5b0851SSebastian Reichel static int pwm_vibrator_probe(struct platform_device *pdev) 1233e5b0851SSebastian Reichel { 1243e5b0851SSebastian Reichel struct pwm_vibrator *vibrator; 1253e5b0851SSebastian Reichel struct pwm_state state; 1263e5b0851SSebastian Reichel int err; 1273e5b0851SSebastian Reichel 1283e5b0851SSebastian Reichel vibrator = devm_kzalloc(&pdev->dev, sizeof(*vibrator), GFP_KERNEL); 1293e5b0851SSebastian Reichel if (!vibrator) 1303e5b0851SSebastian Reichel return -ENOMEM; 1313e5b0851SSebastian Reichel 1323e5b0851SSebastian Reichel vibrator->input = devm_input_allocate_device(&pdev->dev); 1333e5b0851SSebastian Reichel if (!vibrator->input) 1343e5b0851SSebastian Reichel return -ENOMEM; 1353e5b0851SSebastian Reichel 1363e5b0851SSebastian Reichel vibrator->vcc = devm_regulator_get(&pdev->dev, "vcc"); 1373e5b0851SSebastian Reichel err = PTR_ERR_OR_ZERO(vibrator->vcc); 1383e5b0851SSebastian Reichel if (err) { 1393e5b0851SSebastian Reichel if (err != -EPROBE_DEFER) 140*29ebf697SLuca Weiss dev_err(&pdev->dev, "Failed to request regulator: %d\n", 1413e5b0851SSebastian Reichel err); 1423e5b0851SSebastian Reichel return err; 1433e5b0851SSebastian Reichel } 1443e5b0851SSebastian Reichel 1453e5b0851SSebastian Reichel vibrator->pwm = devm_pwm_get(&pdev->dev, "enable"); 1463e5b0851SSebastian Reichel err = PTR_ERR_OR_ZERO(vibrator->pwm); 1473e5b0851SSebastian Reichel if (err) { 1483e5b0851SSebastian Reichel if (err != -EPROBE_DEFER) 149*29ebf697SLuca Weiss dev_err(&pdev->dev, "Failed to request main pwm: %d\n", 1503e5b0851SSebastian Reichel err); 1513e5b0851SSebastian Reichel return err; 1523e5b0851SSebastian Reichel } 1533e5b0851SSebastian Reichel 1543e5b0851SSebastian Reichel INIT_WORK(&vibrator->play_work, pwm_vibrator_play_work); 1553e5b0851SSebastian Reichel 1563e5b0851SSebastian Reichel /* Sync up PWM state and ensure it is off. */ 1573e5b0851SSebastian Reichel pwm_init_state(vibrator->pwm, &state); 1583e5b0851SSebastian Reichel state.enabled = false; 1593e5b0851SSebastian Reichel err = pwm_apply_state(vibrator->pwm, &state); 1603e5b0851SSebastian Reichel if (err) { 161*29ebf697SLuca Weiss dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n", 1623e5b0851SSebastian Reichel err); 1633e5b0851SSebastian Reichel return err; 1643e5b0851SSebastian Reichel } 1653e5b0851SSebastian Reichel 1663e5b0851SSebastian Reichel vibrator->pwm_dir = devm_pwm_get(&pdev->dev, "direction"); 1673e5b0851SSebastian Reichel err = PTR_ERR_OR_ZERO(vibrator->pwm_dir); 1683e5b0851SSebastian Reichel switch (err) { 1693e5b0851SSebastian Reichel case 0: 1703e5b0851SSebastian Reichel /* Sync up PWM state and ensure it is off. */ 1713e5b0851SSebastian Reichel pwm_init_state(vibrator->pwm_dir, &state); 1723e5b0851SSebastian Reichel state.enabled = false; 1733e5b0851SSebastian Reichel err = pwm_apply_state(vibrator->pwm_dir, &state); 1743e5b0851SSebastian Reichel if (err) { 175*29ebf697SLuca Weiss dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n", 1763e5b0851SSebastian Reichel err); 1773e5b0851SSebastian Reichel return err; 1783e5b0851SSebastian Reichel } 1793e5b0851SSebastian Reichel 1803e5b0851SSebastian Reichel vibrator->direction_duty_cycle = 1813e5b0851SSebastian Reichel pwm_get_period(vibrator->pwm_dir) / 2; 1823e5b0851SSebastian Reichel device_property_read_u32(&pdev->dev, "direction-duty-cycle-ns", 1833e5b0851SSebastian Reichel &vibrator->direction_duty_cycle); 1843e5b0851SSebastian Reichel break; 1853e5b0851SSebastian Reichel 1863e5b0851SSebastian Reichel case -ENODATA: 1873e5b0851SSebastian Reichel /* Direction PWM is optional */ 1883e5b0851SSebastian Reichel vibrator->pwm_dir = NULL; 1893e5b0851SSebastian Reichel break; 1903e5b0851SSebastian Reichel 1913e5b0851SSebastian Reichel default: 192*29ebf697SLuca Weiss dev_err(&pdev->dev, "Failed to request direction pwm: %d\n", err); 1936f49c4f5SGustavo A. R. Silva fallthrough; 1943e5b0851SSebastian Reichel 1953e5b0851SSebastian Reichel case -EPROBE_DEFER: 1963e5b0851SSebastian Reichel return err; 1973e5b0851SSebastian Reichel } 1983e5b0851SSebastian Reichel 1993e5b0851SSebastian Reichel vibrator->input->name = "pwm-vibrator"; 2003e5b0851SSebastian Reichel vibrator->input->id.bustype = BUS_HOST; 2013e5b0851SSebastian Reichel vibrator->input->dev.parent = &pdev->dev; 2023e5b0851SSebastian Reichel vibrator->input->close = pwm_vibrator_close; 2033e5b0851SSebastian Reichel 2043e5b0851SSebastian Reichel input_set_drvdata(vibrator->input, vibrator); 2053e5b0851SSebastian Reichel input_set_capability(vibrator->input, EV_FF, FF_RUMBLE); 2063e5b0851SSebastian Reichel 2073e5b0851SSebastian Reichel err = input_ff_create_memless(vibrator->input, NULL, 2083e5b0851SSebastian Reichel pwm_vibrator_play_effect); 2093e5b0851SSebastian Reichel if (err) { 210*29ebf697SLuca Weiss dev_err(&pdev->dev, "Couldn't create FF dev: %d\n", err); 2113e5b0851SSebastian Reichel return err; 2123e5b0851SSebastian Reichel } 2133e5b0851SSebastian Reichel 2143e5b0851SSebastian Reichel err = input_register_device(vibrator->input); 2153e5b0851SSebastian Reichel if (err) { 216*29ebf697SLuca Weiss dev_err(&pdev->dev, "Couldn't register input dev: %d\n", err); 2173e5b0851SSebastian Reichel return err; 2183e5b0851SSebastian Reichel } 2193e5b0851SSebastian Reichel 2203e5b0851SSebastian Reichel platform_set_drvdata(pdev, vibrator); 2213e5b0851SSebastian Reichel 2223e5b0851SSebastian Reichel return 0; 2233e5b0851SSebastian Reichel } 2243e5b0851SSebastian Reichel 225e4b4592fSJonathan Cameron static int pwm_vibrator_suspend(struct device *dev) 2263e5b0851SSebastian Reichel { 2273e5b0851SSebastian Reichel struct pwm_vibrator *vibrator = dev_get_drvdata(dev); 2283e5b0851SSebastian Reichel 2293e5b0851SSebastian Reichel cancel_work_sync(&vibrator->play_work); 2303e5b0851SSebastian Reichel if (vibrator->level) 2313e5b0851SSebastian Reichel pwm_vibrator_stop(vibrator); 2323e5b0851SSebastian Reichel 2333e5b0851SSebastian Reichel return 0; 2343e5b0851SSebastian Reichel } 2353e5b0851SSebastian Reichel 236e4b4592fSJonathan Cameron static int pwm_vibrator_resume(struct device *dev) 2373e5b0851SSebastian Reichel { 2383e5b0851SSebastian Reichel struct pwm_vibrator *vibrator = dev_get_drvdata(dev); 2393e5b0851SSebastian Reichel 2403e5b0851SSebastian Reichel if (vibrator->level) 2413e5b0851SSebastian Reichel pwm_vibrator_start(vibrator); 2423e5b0851SSebastian Reichel 2433e5b0851SSebastian Reichel return 0; 2443e5b0851SSebastian Reichel } 2453e5b0851SSebastian Reichel 246e4b4592fSJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(pwm_vibrator_pm_ops, 2473e5b0851SSebastian Reichel pwm_vibrator_suspend, pwm_vibrator_resume); 2483e5b0851SSebastian Reichel 2493e5b0851SSebastian Reichel #ifdef CONFIG_OF 2503e5b0851SSebastian Reichel static const struct of_device_id pwm_vibra_dt_match_table[] = { 2513e5b0851SSebastian Reichel { .compatible = "pwm-vibrator" }, 2523e5b0851SSebastian Reichel {}, 2533e5b0851SSebastian Reichel }; 2543e5b0851SSebastian Reichel MODULE_DEVICE_TABLE(of, pwm_vibra_dt_match_table); 2553e5b0851SSebastian Reichel #endif 2563e5b0851SSebastian Reichel 2573e5b0851SSebastian Reichel static struct platform_driver pwm_vibrator_driver = { 2583e5b0851SSebastian Reichel .probe = pwm_vibrator_probe, 2593e5b0851SSebastian Reichel .driver = { 2603e5b0851SSebastian Reichel .name = "pwm-vibrator", 261e4b4592fSJonathan Cameron .pm = pm_sleep_ptr(&pwm_vibrator_pm_ops), 2623e5b0851SSebastian Reichel .of_match_table = of_match_ptr(pwm_vibra_dt_match_table), 2633e5b0851SSebastian Reichel }, 2643e5b0851SSebastian Reichel }; 2653e5b0851SSebastian Reichel module_platform_driver(pwm_vibrator_driver); 2663e5b0851SSebastian Reichel 2673e5b0851SSebastian Reichel MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>"); 2683e5b0851SSebastian Reichel MODULE_DESCRIPTION("PWM vibrator driver"); 2693e5b0851SSebastian Reichel MODULE_LICENSE("GPL"); 2703e5b0851SSebastian Reichel MODULE_ALIAS("platform:pwm-vibrator"); 271