^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Arizona haptics driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2012 Wolfson Microelectronics plc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <sound/soc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <sound/soc-dapm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/mfd/arizona/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/mfd/arizona/pdata.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/mfd/arizona/registers.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct arizona_haptics {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct arizona *arizona;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) u8 intensity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static void arizona_haptics_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct arizona_haptics *haptics = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct arizona_haptics,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct arizona *arizona = haptics->arizona;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct snd_soc_component *component =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) snd_soc_dapm_to_component(arizona->dapm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (!haptics->arizona->dapm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) dev_err(arizona->dev, "No DAPM context\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (haptics->intensity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ret = regmap_update_bits(arizona->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ARIZONA_HAPTICS_PHASE_2_INTENSITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ARIZONA_PHASE2_INTENSITY_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) haptics->intensity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) dev_err(arizona->dev, "Failed to set intensity: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* This enable sequence will be a noop if already enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) ret = regmap_update_bits(arizona->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ARIZONA_HAPTICS_CONTROL_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ARIZONA_HAP_CTRL_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) 1 << ARIZONA_HAP_CTRL_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) dev_err(arizona->dev, "Failed to start haptics: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ret = snd_soc_component_enable_pin(component, "HAPTICS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dev_err(arizona->dev, "Failed to start HAPTICS: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ret = snd_soc_dapm_sync(arizona->dapm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* This disable sequence will be a noop if already enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ret = snd_soc_component_disable_pin(component, "HAPTICS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) dev_err(arizona->dev, "Failed to disable HAPTICS: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ret = snd_soc_dapm_sync(arizona->dapm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = regmap_update_bits(arizona->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ARIZONA_HAPTICS_CONTROL_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ARIZONA_HAP_CTRL_MASK, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dev_err(arizona->dev, "Failed to stop haptics: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int arizona_haptics_play(struct input_dev *input, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct arizona_haptics *haptics = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct arizona *arizona = haptics->arizona;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (!arizona->dapm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) dev_err(arizona->dev, "No DAPM context\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (effect->u.rumble.strong_magnitude) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* Scale the magnitude into the range the device supports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (arizona->pdata.hap_act) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) haptics->intensity =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) effect->u.rumble.strong_magnitude >> 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (effect->direction < 0x8000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) haptics->intensity += 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) haptics->intensity =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) effect->u.rumble.strong_magnitude >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) haptics->intensity = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) schedule_work(&haptics->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static void arizona_haptics_close(struct input_dev *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct arizona_haptics *haptics = input_get_drvdata(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct snd_soc_component *component;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) cancel_work_sync(&haptics->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (haptics->arizona->dapm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) component = snd_soc_dapm_to_component(haptics->arizona->dapm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) snd_soc_component_disable_pin(component, "HAPTICS");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static int arizona_haptics_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct arizona_haptics *haptics;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) haptics = devm_kzalloc(&pdev->dev, sizeof(*haptics), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!haptics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) haptics->arizona = arizona;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = regmap_update_bits(arizona->regmap, ARIZONA_HAPTICS_CONTROL_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ARIZONA_HAP_ACT, arizona->pdata.hap_act);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) dev_err(arizona->dev, "Failed to set haptics actuator: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) INIT_WORK(&haptics->work, arizona_haptics_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) haptics->input_dev = devm_input_allocate_device(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (!haptics->input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) dev_err(arizona->dev, "Failed to allocate input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) input_set_drvdata(haptics->input_dev, haptics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) haptics->input_dev->name = "arizona:haptics";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) haptics->input_dev->close = arizona_haptics_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) __set_bit(FF_RUMBLE, haptics->input_dev->ffbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) ret = input_ff_create_memless(haptics->input_dev, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) arizona_haptics_play);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dev_err(arizona->dev, "input_ff_create_memless() failed: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ret = input_register_device(haptics->input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) dev_err(arizona->dev, "couldn't register input device: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static struct platform_driver arizona_haptics_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .probe = arizona_haptics_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) .name = "arizona-haptics",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) module_platform_driver(arizona_haptics_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) MODULE_ALIAS("platform:arizona-haptics");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) MODULE_DESCRIPTION("Arizona haptics driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");