Merge remote-tracking branch 'lsk/v3.10/topic/gator' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / input / misc / arizona-haptics.c
1 /*
2  * Arizona haptics driver
3  *
4  * Copyright 2012 Wolfson Microelectronics plc
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
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.
11  */
12
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/input.h>
16 #include <linux/slab.h>
17
18 #include <sound/soc.h>
19 #include <sound/soc-dapm.h>
20
21 #include <linux/mfd/arizona/core.h>
22 #include <linux/mfd/arizona/pdata.h>
23 #include <linux/mfd/arizona/registers.h>
24
25 struct arizona_haptics {
26         struct arizona *arizona;
27         struct input_dev *input_dev;
28         struct work_struct work;
29
30         struct mutex mutex;
31         u8 intensity;
32 };
33
34 static void arizona_haptics_work(struct work_struct *work)
35 {
36         struct arizona_haptics *haptics = container_of(work,
37                                                        struct arizona_haptics,
38                                                        work);
39         struct arizona *arizona = haptics->arizona;
40         struct mutex *dapm_mutex = &arizona->dapm->card->dapm_mutex;
41         int ret;
42
43         if (!haptics->arizona->dapm) {
44                 dev_err(arizona->dev, "No DAPM context\n");
45                 return;
46         }
47
48         if (haptics->intensity) {
49                 ret = regmap_update_bits(arizona->regmap,
50                                          ARIZONA_HAPTICS_PHASE_2_INTENSITY,
51                                          ARIZONA_PHASE2_INTENSITY_MASK,
52                                          haptics->intensity);
53                 if (ret != 0) {
54                         dev_err(arizona->dev, "Failed to set intensity: %d\n",
55                                 ret);
56                         return;
57                 }
58
59                 /* This enable sequence will be a noop if already enabled */
60                 ret = regmap_update_bits(arizona->regmap,
61                                          ARIZONA_HAPTICS_CONTROL_1,
62                                          ARIZONA_HAP_CTRL_MASK,
63                                          1 << ARIZONA_HAP_CTRL_SHIFT);
64                 if (ret != 0) {
65                         dev_err(arizona->dev, "Failed to start haptics: %d\n",
66                                 ret);
67                         return;
68                 }
69
70                 mutex_lock_nested(dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
71
72                 ret = snd_soc_dapm_enable_pin(arizona->dapm, "HAPTICS");
73                 if (ret != 0) {
74                         dev_err(arizona->dev, "Failed to start HAPTICS: %d\n",
75                                 ret);
76                         mutex_unlock(dapm_mutex);
77                         return;
78                 }
79
80                 mutex_unlock(dapm_mutex);
81
82                 ret = snd_soc_dapm_sync(arizona->dapm);
83                 if (ret != 0) {
84                         dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
85                                 ret);
86                         return;
87                 }
88         } else {
89                 /* This disable sequence will be a noop if already enabled */
90                 mutex_lock_nested(dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
91
92                 ret = snd_soc_dapm_disable_pin(arizona->dapm, "HAPTICS");
93                 if (ret != 0) {
94                         dev_err(arizona->dev, "Failed to disable HAPTICS: %d\n",
95                                 ret);
96                         mutex_unlock(dapm_mutex);
97                         return;
98                 }
99
100                 mutex_unlock(dapm_mutex);
101
102                 ret = snd_soc_dapm_sync(arizona->dapm);
103                 if (ret != 0) {
104                         dev_err(arizona->dev, "Failed to sync DAPM: %d\n",
105                                 ret);
106                         return;
107                 }
108
109                 ret = regmap_update_bits(arizona->regmap,
110                                          ARIZONA_HAPTICS_CONTROL_1,
111                                          ARIZONA_HAP_CTRL_MASK,
112                                          1 << ARIZONA_HAP_CTRL_SHIFT);
113                 if (ret != 0) {
114                         dev_err(arizona->dev, "Failed to stop haptics: %d\n",
115                                 ret);
116                         return;
117                 }
118         }
119 }
120
121 static int arizona_haptics_play(struct input_dev *input, void *data,
122                                 struct ff_effect *effect)
123 {
124         struct arizona_haptics *haptics = input_get_drvdata(input);
125         struct arizona *arizona = haptics->arizona;
126
127         if (!arizona->dapm) {
128                 dev_err(arizona->dev, "No DAPM context\n");
129                 return -EBUSY;
130         }
131
132         if (effect->u.rumble.strong_magnitude) {
133                 /* Scale the magnitude into the range the device supports */
134                 if (arizona->pdata.hap_act) {
135                         haptics->intensity =
136                                 effect->u.rumble.strong_magnitude >> 9;
137                         if (effect->direction < 0x8000)
138                                 haptics->intensity += 0x7f;
139                 } else {
140                         haptics->intensity =
141                                 effect->u.rumble.strong_magnitude >> 8;
142                 }
143         } else {
144                 haptics->intensity = 0;
145         }
146
147         schedule_work(&haptics->work);
148
149         return 0;
150 }
151
152 static void arizona_haptics_close(struct input_dev *input)
153 {
154         struct arizona_haptics *haptics = input_get_drvdata(input);
155         struct mutex *dapm_mutex = &haptics->arizona->dapm->card->dapm_mutex;
156
157         cancel_work_sync(&haptics->work);
158
159         mutex_lock_nested(dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
160
161         if (haptics->arizona->dapm)
162                 snd_soc_dapm_disable_pin(haptics->arizona->dapm, "HAPTICS");
163
164         mutex_unlock(dapm_mutex);
165 }
166
167 static int arizona_haptics_probe(struct platform_device *pdev)
168 {
169         struct arizona *arizona = dev_get_drvdata(pdev->dev.parent);
170         struct arizona_haptics *haptics;
171         int ret;
172
173         haptics = devm_kzalloc(&pdev->dev, sizeof(*haptics), GFP_KERNEL);
174         if (!haptics)
175                 return -ENOMEM;
176
177         haptics->arizona = arizona;
178
179         ret = regmap_update_bits(arizona->regmap, ARIZONA_HAPTICS_CONTROL_1,
180                                  ARIZONA_HAP_ACT, arizona->pdata.hap_act);
181         if (ret != 0) {
182                 dev_err(arizona->dev, "Failed to set haptics actuator: %d\n",
183                         ret);
184                 return ret;
185         }
186
187         INIT_WORK(&haptics->work, arizona_haptics_work);
188
189         haptics->input_dev = input_allocate_device();
190         if (haptics->input_dev == NULL) {
191                 dev_err(arizona->dev, "Failed to allocate input device\n");
192                 return -ENOMEM;
193         }
194
195         input_set_drvdata(haptics->input_dev, haptics);
196
197         haptics->input_dev->name = "arizona:haptics";
198         haptics->input_dev->dev.parent = pdev->dev.parent;
199         haptics->input_dev->close = arizona_haptics_close;
200         __set_bit(FF_RUMBLE, haptics->input_dev->ffbit);
201
202         ret = input_ff_create_memless(haptics->input_dev, NULL,
203                                       arizona_haptics_play);
204         if (ret < 0) {
205                 dev_err(arizona->dev, "input_ff_create_memless() failed: %d\n",
206                         ret);
207                 goto err_ialloc;
208         }
209
210         ret = input_register_device(haptics->input_dev);
211         if (ret < 0) {
212                 dev_err(arizona->dev, "couldn't register input device: %d\n",
213                         ret);
214                 goto err_iff;
215         }
216
217         platform_set_drvdata(pdev, haptics);
218
219         return 0;
220
221 err_iff:
222         if (haptics->input_dev)
223                 input_ff_destroy(haptics->input_dev);
224 err_ialloc:
225         input_free_device(haptics->input_dev);
226
227         return ret;
228 }
229
230 static int arizona_haptics_remove(struct platform_device *pdev)
231 {
232         struct arizona_haptics *haptics = platform_get_drvdata(pdev);
233
234         input_unregister_device(haptics->input_dev);
235
236         return 0;
237 }
238
239 static struct platform_driver arizona_haptics_driver = {
240         .probe          = arizona_haptics_probe,
241         .remove         = arizona_haptics_remove,
242         .driver         = {
243                 .name   = "arizona-haptics",
244                 .owner  = THIS_MODULE,
245         },
246 };
247 module_platform_driver(arizona_haptics_driver);
248
249 MODULE_ALIAS("platform:arizona-haptics");
250 MODULE_DESCRIPTION("Arizona haptics driver");
251 MODULE_LICENSE("GPL");
252 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");