1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/module.h>
27#include <linux/jiffies.h>
28#include <linux/platform_device.h>
29#include <linux/of.h>
30#include <linux/workqueue.h>
31#include <linux/i2c/twl.h>
32#include <linux/mfd/twl4030-audio.h>
33#include <linux/input.h>
34#include <linux/slab.h>
35
36
37#define LEDEN 0x00
38
39
40#define EFFECT_DIR_180_DEG 0x8000
41
42struct vibra_info {
43 struct device *dev;
44 struct input_dev *input_dev;
45
46 struct work_struct play_work;
47
48 bool enabled;
49 int speed;
50 int direction;
51
52 bool coexist;
53};
54
55static void vibra_disable_leds(void)
56{
57 u8 reg;
58
59
60 twl_i2c_read_u8(TWL4030_MODULE_LED, ®, LEDEN);
61 reg &= ~0x03;
62 twl_i2c_write_u8(TWL4030_MODULE_LED, LEDEN, reg);
63}
64
65
66static void vibra_enable(struct vibra_info *info)
67{
68 u8 reg;
69
70 twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER);
71
72
73 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
74 ®, TWL4030_REG_VIBRA_CTL);
75 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
76 (reg | TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
77
78 twl4030_audio_enable_resource(TWL4030_AUDIO_RES_APLL);
79
80 info->enabled = true;
81}
82
83static void vibra_disable(struct vibra_info *info)
84{
85 u8 reg;
86
87
88 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
89 ®, TWL4030_REG_VIBRA_CTL);
90 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
91 (reg & ~TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
92
93 twl4030_audio_disable_resource(TWL4030_AUDIO_RES_APLL);
94 twl4030_audio_disable_resource(TWL4030_AUDIO_RES_POWER);
95
96 info->enabled = false;
97}
98
99static void vibra_play_work(struct work_struct *work)
100{
101 struct vibra_info *info = container_of(work,
102 struct vibra_info, play_work);
103 int dir;
104 int pwm;
105 u8 reg;
106
107 dir = info->direction;
108 pwm = info->speed;
109
110 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
111 ®, TWL4030_REG_VIBRA_CTL);
112 if (pwm && (!info->coexist || !(reg & TWL4030_VIBRA_SEL))) {
113
114 if (!info->enabled)
115 vibra_enable(info);
116
117
118 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
119 ®, TWL4030_REG_VIBRA_CTL);
120 reg = (dir) ? (reg | TWL4030_VIBRA_DIR) :
121 (reg & ~TWL4030_VIBRA_DIR);
122 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
123 reg, TWL4030_REG_VIBRA_CTL);
124
125
126 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
127 256 - pwm, TWL4030_REG_VIBRA_SET);
128 } else {
129 if (info->enabled)
130 vibra_disable(info);
131 }
132}
133
134
135
136static int vibra_play(struct input_dev *input, void *data,
137 struct ff_effect *effect)
138{
139 struct vibra_info *info = input_get_drvdata(input);
140
141 info->speed = effect->u.rumble.strong_magnitude >> 8;
142 if (!info->speed)
143 info->speed = effect->u.rumble.weak_magnitude >> 9;
144 info->direction = effect->direction < EFFECT_DIR_180_DEG ? 0 : 1;
145 schedule_work(&info->play_work);
146 return 0;
147}
148
149static void twl4030_vibra_close(struct input_dev *input)
150{
151 struct vibra_info *info = input_get_drvdata(input);
152
153 cancel_work_sync(&info->play_work);
154
155 if (info->enabled)
156 vibra_disable(info);
157}
158
159
160static int __maybe_unused twl4030_vibra_suspend(struct device *dev)
161{
162 struct platform_device *pdev = to_platform_device(dev);
163 struct vibra_info *info = platform_get_drvdata(pdev);
164
165 if (info->enabled)
166 vibra_disable(info);
167
168 return 0;
169}
170
171static int __maybe_unused twl4030_vibra_resume(struct device *dev)
172{
173 vibra_disable_leds();
174 return 0;
175}
176
177static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
178 twl4030_vibra_suspend, twl4030_vibra_resume);
179
180static bool twl4030_vibra_check_coexist(struct twl4030_vibra_data *pdata,
181 struct device_node *node)
182{
183 if (pdata && pdata->coexist)
184 return true;
185
186 node = of_find_node_by_name(node, "codec");
187 if (node) {
188 of_node_put(node);
189 return true;
190 }
191
192 return false;
193}
194
195static int twl4030_vibra_probe(struct platform_device *pdev)
196{
197 struct twl4030_vibra_data *pdata = dev_get_platdata(&pdev->dev);
198 struct device_node *twl4030_core_node = pdev->dev.parent->of_node;
199 struct vibra_info *info;
200 int ret;
201
202 if (!pdata && !twl4030_core_node) {
203 dev_dbg(&pdev->dev, "platform_data not available\n");
204 return -EINVAL;
205 }
206
207 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
208 if (!info)
209 return -ENOMEM;
210
211 info->dev = &pdev->dev;
212 info->coexist = twl4030_vibra_check_coexist(pdata, twl4030_core_node);
213 INIT_WORK(&info->play_work, vibra_play_work);
214
215 info->input_dev = devm_input_allocate_device(&pdev->dev);
216 if (info->input_dev == NULL) {
217 dev_err(&pdev->dev, "couldn't allocate input device\n");
218 return -ENOMEM;
219 }
220
221 input_set_drvdata(info->input_dev, info);
222
223 info->input_dev->name = "twl4030:vibrator";
224 info->input_dev->id.version = 1;
225 info->input_dev->close = twl4030_vibra_close;
226 __set_bit(FF_RUMBLE, info->input_dev->ffbit);
227
228 ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
229 if (ret < 0) {
230 dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
231 return ret;
232 }
233
234 ret = input_register_device(info->input_dev);
235 if (ret < 0) {
236 dev_dbg(&pdev->dev, "couldn't register input device\n");
237 goto err_iff;
238 }
239
240 vibra_disable_leds();
241
242 platform_set_drvdata(pdev, info);
243 return 0;
244
245err_iff:
246 input_ff_destroy(info->input_dev);
247 return ret;
248}
249
250static struct platform_driver twl4030_vibra_driver = {
251 .probe = twl4030_vibra_probe,
252 .driver = {
253 .name = "twl4030-vibra",
254 .pm = &twl4030_vibra_pm_ops,
255 },
256};
257module_platform_driver(twl4030_vibra_driver);
258
259MODULE_ALIAS("platform:twl4030-vibra");
260MODULE_DESCRIPTION("TWL4030 Vibra driver");
261MODULE_LICENSE("GPL");
262MODULE_AUTHOR("Nokia Corporation");
263