2 * Arizona haptics driver
4 * Copyright 2012 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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;
40 struct snd_soc_component *component =
41 snd_soc_dapm_to_component(arizona->dapm);
44 if (!haptics->arizona->dapm) {
45 dev_err(arizona->dev, "No DAPM context\n");
49 if (haptics->intensity) {
50 ret = regmap_update_bits(arizona->regmap,
51 ARIZONA_HAPTICS_PHASE_2_INTENSITY,
52 ARIZONA_PHASE2_INTENSITY_MASK,
55 dev_err(arizona->dev, "Failed to set intensity: %d\n",
60 /* This enable sequence will be a noop if already enabled */
61 ret = regmap_update_bits(arizona->regmap,
62 ARIZONA_HAPTICS_CONTROL_1,
63 ARIZONA_HAP_CTRL_MASK,
64 1 << ARIZONA_HAP_CTRL_SHIFT);
66 dev_err(arizona->dev, "Failed to start haptics: %d\n",
71 ret = snd_soc_component_enable_pin(component, "HAPTICS");
73 dev_err(arizona->dev, "Failed to start HAPTICS: %d\n",
78 ret = snd_soc_dapm_sync(arizona->dapm);
80 dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
85 /* This disable sequence will be a noop if already enabled */
86 ret = snd_soc_component_disable_pin(component, "HAPTICS");
88 dev_err(arizona->dev, "Failed to disable HAPTICS: %d\n",
93 ret = snd_soc_dapm_sync(arizona->dapm);
95 dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
100 ret = regmap_update_bits(arizona->regmap,
101 ARIZONA_HAPTICS_CONTROL_1,
102 ARIZONA_HAP_CTRL_MASK, 0);
104 dev_err(arizona->dev, "Failed to stop haptics: %d\n",
111 static int arizona_haptics_play(struct input_dev *input, void *data,
112 struct ff_effect *effect)
114 struct arizona_haptics *haptics = input_get_drvdata(input);
115 struct arizona *arizona = haptics->arizona;
117 if (!arizona->dapm) {
118 dev_err(arizona->dev, "No DAPM context\n");
122 if (effect->u.rumble.strong_magnitude) {
123 /* Scale the magnitude into the range the device supports */
124 if (arizona->pdata.hap_act) {
126 effect->u.rumble.strong_magnitude >> 9;
127 if (effect->direction < 0x8000)
128 haptics->intensity += 0x7f;
131 effect->u.rumble.strong_magnitude >> 8;
134 haptics->intensity = 0;
137 schedule_work(&haptics->work);
142 static void arizona_haptics_close(struct input_dev *input)
144 struct arizona_haptics *haptics = input_get_drvdata(input);
145 struct snd_soc_component *component;
147 cancel_work_sync(&haptics->work);
149 if (haptics->arizona->dapm) {
150 component = snd_soc_dapm_to_component(haptics->arizona->dapm);
151 snd_soc_component_disable_pin(component, "HAPTICS");
155 static int arizona_haptics_probe(struct platform_device *pdev)
157 struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
158 struct arizona_haptics *haptics;
161 haptics = devm_kzalloc(&pdev->dev, sizeof(*haptics), GFP_KERNEL);
165 haptics->arizona = arizona;
167 ret = regmap_update_bits(arizona->regmap, ARIZONA_HAPTICS_CONTROL_1,
168 ARIZONA_HAP_ACT, arizona->pdata.hap_act);
170 dev_err(arizona->dev, "Failed to set haptics actuator: %d\n",
175 INIT_WORK(&haptics->work, arizona_haptics_work);
177 haptics->input_dev = devm_input_allocate_device(&pdev->dev);
178 if (!haptics->input_dev) {
179 dev_err(arizona->dev, "Failed to allocate input device\n");
183 input_set_drvdata(haptics->input_dev, haptics);
185 haptics->input_dev->name = "arizona:haptics";
186 haptics->input_dev->close = arizona_haptics_close;
187 __set_bit(FF_RUMBLE, haptics->input_dev->ffbit);
189 ret = input_ff_create_memless(haptics->input_dev, NULL,
190 arizona_haptics_play);
192 dev_err(arizona->dev, "input_ff_create_memless() failed: %d\n",
197 ret = input_register_device(haptics->input_dev);
199 dev_err(arizona->dev, "couldn't register input device: %d\n",
207 static struct platform_driver arizona_haptics_driver = {
208 .probe = arizona_haptics_probe,
210 .name = "arizona-haptics",
213 module_platform_driver(arizona_haptics_driver);
215 MODULE_ALIAS("platform:arizona-haptics");
216 MODULE_DESCRIPTION("Arizona haptics driver");
217 MODULE_LICENSE("GPL");
218 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");