^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2018 Spreadtrum Communications Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define CUR_DRV_CAL_SEL GENMASK(13, 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define SLP_LDOVIBR_PD_EN BIT(9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define LDO_VIBR_PD BIT(8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct vibra_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct work_struct play_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) u32 base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) u32 strength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) bool enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static void sc27xx_vibra_set(struct vibra_info *info, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) regmap_update_bits(info->regmap, info->base, LDO_VIBR_PD, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) regmap_update_bits(info->regmap, info->base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) SLP_LDOVIBR_PD_EN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) info->enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) regmap_update_bits(info->regmap, info->base, LDO_VIBR_PD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) LDO_VIBR_PD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) regmap_update_bits(info->regmap, info->base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) SLP_LDOVIBR_PD_EN, SLP_LDOVIBR_PD_EN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) info->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int sc27xx_vibra_hw_init(struct vibra_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return regmap_update_bits(info->regmap, info->base, CUR_DRV_CAL_SEL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static void sc27xx_vibra_play_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct vibra_info *info = container_of(work, struct vibra_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) play_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (info->strength && !info->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) sc27xx_vibra_set(info, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) else if (info->strength == 0 && info->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) sc27xx_vibra_set(info, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int sc27xx_vibra_play(struct input_dev *input, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct vibra_info *info = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) info->strength = effect->u.rumble.weak_magnitude;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) schedule_work(&info->play_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static void sc27xx_vibra_close(struct input_dev *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct vibra_info *info = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) cancel_work_sync(&info->play_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (info->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) sc27xx_vibra_set(info, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static int sc27xx_vibra_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct vibra_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) info->regmap = dev_get_regmap(pdev->dev.parent, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!info->regmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dev_err(&pdev->dev, "failed to get vibrator regmap.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) error = device_property_read_u32(&pdev->dev, "reg", &info->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) dev_err(&pdev->dev, "failed to get vibrator base address.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) info->input_dev = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!info->input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dev_err(&pdev->dev, "failed to allocate input device.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) info->input_dev->name = "sc27xx:vibrator";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) info->input_dev->id.version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) info->input_dev->close = sc27xx_vibra_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) input_set_drvdata(info->input_dev, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) input_set_capability(info->input_dev, EV_FF, FF_RUMBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) INIT_WORK(&info->play_work, sc27xx_vibra_play_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) info->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) error = sc27xx_vibra_hw_init(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) dev_err(&pdev->dev, "failed to initialize the vibrator.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) error = input_ff_create_memless(info->input_dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) sc27xx_vibra_play);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) dev_err(&pdev->dev, "failed to register vibrator to FF.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) error = input_register_device(info->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) dev_err(&pdev->dev, "failed to register input device.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static const struct of_device_id sc27xx_vibra_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) { .compatible = "sprd,sc2731-vibrator", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) MODULE_DEVICE_TABLE(of, sc27xx_vibra_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static struct platform_driver sc27xx_vibra_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .name = "sc27xx-vibrator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .of_match_table = sc27xx_vibra_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .probe = sc27xx_vibra_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) module_platform_driver(sc27xx_vibra_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) MODULE_DESCRIPTION("Spreadtrum SC27xx Vibrator Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) MODULE_AUTHOR("Xiaotong Lu <xiaotong.lu@spreadtrum.com>");