2 * Arizona haptics driver
4 * Copyright 2012 Wolfson Microelectronics plc
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/input.h>
16 #include <linux/slab.h>
18 #include <sound/soc.h>
19 #include <sound/soc-dapm.h>
21 #include <linux/mfd/arizona/core.h>
22 #include <linux/mfd/arizona/pdata.h>
23 #include <linux/mfd/arizona/registers.h>
25 struct arizona_haptics {
26 struct arizona *arizona;
27 struct input_dev *input_dev;
28 struct work_struct work;
34 static void arizona_haptics_work(struct work_struct *work)
36 struct arizona_haptics *haptics = container_of(work,
37 struct arizona_haptics,
39 struct arizona *arizona = haptics->arizona;
42 if (!haptics->arizona->dapm) {
43 dev_err(arizona->dev, "No DAPM context\n");
47 if (haptics->intensity) {
48 ret = regmap_update_bits(arizona->regmap,
49 ARIZONA_HAPTICS_PHASE_2_INTENSITY,
50 ARIZONA_PHASE2_INTENSITY_MASK,
53 dev_err(arizona->dev, "Failed to set intensity: %d\n",
58 /* This enable sequence will be a noop if already enabled */
59 ret = regmap_update_bits(arizona->regmap,
60 ARIZONA_HAPTICS_CONTROL_1,
61 ARIZONA_HAP_CTRL_MASK,
62 1 << ARIZONA_HAP_CTRL_SHIFT);
64 dev_err(arizona->dev, "Failed to start haptics: %d\n",
69 ret = snd_soc_dapm_enable_pin(arizona->dapm, "HAPTICS");
71 dev_err(arizona->dev, "Failed to start HAPTICS: %d\n",
76 ret = snd_soc_dapm_sync(arizona->dapm);
78 dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
83 /* This disable sequence will be a noop if already enabled */
84 ret = snd_soc_dapm_disable_pin(arizona->dapm, "HAPTICS");
86 dev_err(arizona->dev, "Failed to disable HAPTICS: %d\n",
91 ret = snd_soc_dapm_sync(arizona->dapm);
93 dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
98 ret = regmap_update_bits(arizona->regmap,
99 ARIZONA_HAPTICS_CONTROL_1,
100 ARIZONA_HAP_CTRL_MASK, 0);
102 dev_err(arizona->dev, "Failed to stop haptics: %d\n",
109 static int arizona_haptics_play(struct input_dev *input, void *data,
110 struct ff_effect *effect)
112 struct arizona_haptics *haptics = input_get_drvdata(input);
113 struct arizona *arizona = haptics->arizona;
115 if (!arizona->dapm) {
116 dev_err(arizona->dev, "No DAPM context\n");
120 if (effect->u.rumble.strong_magnitude) {
121 /* Scale the magnitude into the range the device supports */
122 if (arizona->pdata.hap_act) {
124 effect->u.rumble.strong_magnitude >> 9;
125 if (effect->direction < 0x8000)
126 haptics->intensity += 0x7f;
129 effect->u.rumble.strong_magnitude >> 8;
132 haptics->intensity = 0;
135 schedule_work(&haptics->work);
140 static void arizona_haptics_close(struct input_dev *input)
142 struct arizona_haptics *haptics = input_get_drvdata(input);
144 cancel_work_sync(&haptics->work);
146 if (haptics->arizona->dapm)
147 snd_soc_dapm_disable_pin(haptics->arizona->dapm, "HAPTICS");
150 static int arizona_haptics_probe(struct platform_device *pdev)
152 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
153 struct arizona_haptics *haptics;
156 haptics = devm_kzalloc(&pdev->dev, sizeof(*haptics), GFP_KERNEL);
160 haptics->arizona = arizona;
162 ret = regmap_update_bits(arizona->regmap, ARIZONA_HAPTICS_CONTROL_1,
163 ARIZONA_HAP_ACT, arizona->pdata.hap_act);
165 dev_err(arizona->dev, "Failed to set haptics actuator: %d\n",
170 INIT_WORK(&haptics->work, arizona_haptics_work);
172 haptics->input_dev = devm_input_allocate_device(&pdev->dev);
173 if (!haptics->input_dev) {
174 dev_err(arizona->dev, "Failed to allocate input device\n");
178 input_set_drvdata(haptics->input_dev, haptics);
180 haptics->input_dev->name = "arizona:haptics";
181 haptics->input_dev->close = arizona_haptics_close;
182 __set_bit(FF_RUMBLE, haptics->input_dev->ffbit);
184 ret = input_ff_create_memless(haptics->input_dev, NULL,
185 arizona_haptics_play);
187 dev_err(arizona->dev, "input_ff_create_memless() failed: %d\n",
192 ret = input_register_device(haptics->input_dev);
194 dev_err(arizona->dev, "couldn't register input device: %d\n",
199 platform_set_drvdata(pdev, haptics);
204 static struct platform_driver arizona_haptics_driver = {
205 .probe = arizona_haptics_probe,
207 .name = "arizona-haptics",
210 module_platform_driver(arizona_haptics_driver);
212 MODULE_ALIAS("platform:arizona-haptics");
213 MODULE_DESCRIPTION("Arizona haptics driver");
214 MODULE_LICENSE("GPL");