blob: 89bca7647f549ca033d297c8de994df0bcabe307 [file] [log] [blame]
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -05001/*
2 * twl6040-vibra.c - TWL6040 Vibrator driver
3 *
4 * Author: Jorge Eduardo Candelaria <jorge.candelaria@ti.com>
5 * Author: Misael Lopez Cruz <misael.lopez@ti.com>
6 *
7 * Copyright: (C) 2011 Texas Instruments, Inc.
8 *
9 * Based on twl4030-vibra.c by Henrik Saari <henrik.saari@nokia.com>
10 * Felipe Balbi <felipe.balbi@nokia.com>
11 * Jari Vanhala <ext-javi.vanhala@nokia.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25 * 02110-1301 USA
26 *
27 */
28#include <linux/module.h>
29#include <linux/platform_device.h>
Peter Ujfalusi9ac7b1a2012-05-07 08:45:50 -070030#include <linux/of.h>
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -050031#include <linux/workqueue.h>
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030032#include <linux/input.h>
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -050033#include <linux/mfd/twl6040.h>
34#include <linux/slab.h>
35#include <linux/delay.h>
36#include <linux/regulator/consumer.h>
37
38#define EFFECT_DIR_180_DEG 0x8000
39
40/* Recommended modulation index 85% */
41#define TWL6040_VIBRA_MOD 85
42
43#define TWL6040_NUM_SUPPLIES 2
44
45struct vibra_info {
46 struct device *dev;
47 struct input_dev *input_dev;
48 struct workqueue_struct *workqueue;
49 struct work_struct play_work;
50 struct mutex mutex;
Peter Ujfalusi625cbe42011-07-04 19:50:02 +030051 int irq;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -050052
53 bool enabled;
54 int weak_speed;
55 int strong_speed;
56 int direction;
57
58 unsigned int vibldrv_res;
59 unsigned int vibrdrv_res;
60 unsigned int viblmotor_res;
61 unsigned int vibrmotor_res;
62
63 struct regulator_bulk_data supplies[TWL6040_NUM_SUPPLIES];
64
65 struct twl6040 *twl6040;
66};
67
68static irqreturn_t twl6040_vib_irq_handler(int irq, void *data)
69{
70 struct vibra_info *info = data;
71 struct twl6040 *twl6040 = info->twl6040;
72 u8 status;
73
74 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
75 if (status & TWL6040_VIBLOCDET) {
76 dev_warn(info->dev, "Left Vibrator overcurrent detected\n");
77 twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLL,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +030078 TWL6040_VIBENA);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -050079 }
80 if (status & TWL6040_VIBROCDET) {
81 dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
82 twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +030083 TWL6040_VIBENA);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -050084 }
85
86 return IRQ_HANDLED;
87}
88
89static void twl6040_vibra_enable(struct vibra_info *info)
90{
91 struct twl6040 *twl6040 = info->twl6040;
92 int ret;
93
94 ret = regulator_bulk_enable(ARRAY_SIZE(info->supplies), info->supplies);
95 if (ret) {
96 dev_err(info->dev, "failed to enable regulators %d\n", ret);
97 return;
98 }
99
100 twl6040_power(info->twl6040, 1);
Peter Ujfalusi7e968982011-09-15 15:39:25 +0300101 if (twl6040_get_revid(twl6040) <= TWL6040_REV_ES1_1) {
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500102 /*
103 * ERRATA: Disable overcurrent protection for at least
104 * 3ms when enabling vibrator drivers to avoid false
105 * overcurrent detection
106 */
107 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +0300108 TWL6040_VIBENA | TWL6040_VIBCTRL);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500109 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +0300110 TWL6040_VIBENA | TWL6040_VIBCTRL);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500111 usleep_range(3000, 3500);
112 }
113
114 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +0300115 TWL6040_VIBENA);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500116 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +0300117 TWL6040_VIBENA);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500118
119 info->enabled = true;
120}
121
122static void twl6040_vibra_disable(struct vibra_info *info)
123{
124 struct twl6040 *twl6040 = info->twl6040;
125
126 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, 0x00);
127 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, 0x00);
128 twl6040_power(info->twl6040, 0);
129
130 regulator_bulk_disable(ARRAY_SIZE(info->supplies), info->supplies);
131
132 info->enabled = false;
133}
134
135static u8 twl6040_vibra_code(int vddvib, int vibdrv_res, int motor_res,
136 int speed, int direction)
137{
138 int vpk, max_code;
139 u8 vibdat;
140
141 /* output swing */
142 vpk = (vddvib * motor_res * TWL6040_VIBRA_MOD) /
143 (100 * (vibdrv_res + motor_res));
144
145 /* 50mV per VIBDAT code step */
146 max_code = vpk / 50;
147 if (max_code > TWL6040_VIBDAT_MAX)
148 max_code = TWL6040_VIBDAT_MAX;
149
150 /* scale speed to max allowed code */
151 vibdat = (u8)((speed * max_code) / USHRT_MAX);
152
153 /* 2's complement for direction > 180 degrees */
154 vibdat *= direction;
155
156 return vibdat;
157}
158
159static void twl6040_vibra_set_effect(struct vibra_info *info)
160{
161 struct twl6040 *twl6040 = info->twl6040;
162 u8 vibdatl, vibdatr;
163 int volt;
164
165 /* weak motor */
166 volt = regulator_get_voltage(info->supplies[0].consumer) / 1000;
167 vibdatl = twl6040_vibra_code(volt, info->vibldrv_res,
168 info->viblmotor_res,
169 info->weak_speed, info->direction);
170
171 /* strong motor */
172 volt = regulator_get_voltage(info->supplies[1].consumer) / 1000;
173 vibdatr = twl6040_vibra_code(volt, info->vibrdrv_res,
174 info->vibrmotor_res,
175 info->strong_speed, info->direction);
176
177 twl6040_reg_write(twl6040, TWL6040_REG_VIBDATL, vibdatl);
178 twl6040_reg_write(twl6040, TWL6040_REG_VIBDATR, vibdatr);
179}
180
181static void vibra_play_work(struct work_struct *work)
182{
183 struct vibra_info *info = container_of(work,
184 struct vibra_info, play_work);
185
186 mutex_lock(&info->mutex);
187
188 if (info->weak_speed || info->strong_speed) {
189 if (!info->enabled)
190 twl6040_vibra_enable(info);
191
192 twl6040_vibra_set_effect(info);
193 } else if (info->enabled)
194 twl6040_vibra_disable(info);
195
196 mutex_unlock(&info->mutex);
197}
198
199static int vibra_play(struct input_dev *input, void *data,
200 struct ff_effect *effect)
201{
202 struct vibra_info *info = input_get_drvdata(input);
203 int ret;
204
Peter Ujfalusi5f07c322011-10-12 11:57:56 +0300205 /* Do not allow effect, while the routing is set to use audio */
206 ret = twl6040_get_vibralr_status(info->twl6040);
207 if (ret & TWL6040_VIBSEL) {
208 dev_info(&input->dev, "Vibra is configured for audio\n");
209 return -EBUSY;
210 }
211
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500212 info->weak_speed = effect->u.rumble.weak_magnitude;
213 info->strong_speed = effect->u.rumble.strong_magnitude;
214 info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
215
216 ret = queue_work(info->workqueue, &info->play_work);
217 if (!ret) {
218 dev_info(&input->dev, "work is already on queue\n");
219 return ret;
220 }
221
222 return 0;
223}
224
225static void twl6040_vibra_close(struct input_dev *input)
226{
227 struct vibra_info *info = input_get_drvdata(input);
228
229 cancel_work_sync(&info->play_work);
230
231 mutex_lock(&info->mutex);
232
233 if (info->enabled)
234 twl6040_vibra_disable(info);
235
236 mutex_unlock(&info->mutex);
237}
238
Randy Dunlapb6b1e922011-09-09 10:15:29 -0700239#ifdef CONFIG_PM_SLEEP
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500240static int twl6040_vibra_suspend(struct device *dev)
241{
242 struct platform_device *pdev = to_platform_device(dev);
243 struct vibra_info *info = platform_get_drvdata(pdev);
244
245 mutex_lock(&info->mutex);
246
247 if (info->enabled)
248 twl6040_vibra_disable(info);
249
250 mutex_unlock(&info->mutex);
251
252 return 0;
253}
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500254#endif
255
256static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL);
257
Bill Pemberton5298cc42012-11-23 21:38:25 -0800258static int twl6040_vibra_probe(struct platform_device *pdev)
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500259{
Peter Ujfalusie7ec0142012-06-12 01:10:02 -0700260 struct device *twl6040_core_dev = pdev->dev.parent;
261 struct device_node *twl6040_core_node = NULL;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500262 struct vibra_info *info;
Peter Ujfalusi9ac7b1a2012-05-07 08:45:50 -0700263 int vddvibl_uV = 0;
264 int vddvibr_uV = 0;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500265 int ret;
266
Peter Ujfalusie7ec0142012-06-12 01:10:02 -0700267#ifdef CONFIG_OF
268 twl6040_core_node = of_find_node_by_name(twl6040_core_dev->of_node,
269 "vibra");
270#endif
271
Peter Ujfalusi7bf2a982013-07-13 13:36:19 -0700272 if (!twl6040_core_node) {
273 dev_err(&pdev->dev, "parent of node is missing?\n");
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500274 return -EINVAL;
275 }
276
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800277 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500278 if (!info) {
Libo Chenf048dd12014-01-04 00:00:30 -0800279 of_node_put(twl6040_core_node);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500280 dev_err(&pdev->dev, "couldn't allocate memory\n");
281 return -ENOMEM;
282 }
283
284 info->dev = &pdev->dev;
Peter Ujfalusi9ac7b1a2012-05-07 08:45:50 -0700285
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500286 info->twl6040 = dev_get_drvdata(pdev->dev.parent);
Peter Ujfalusi7bf2a982013-07-13 13:36:19 -0700287
288 of_property_read_u32(twl6040_core_node, "ti,vibldrv-res",
289 &info->vibldrv_res);
290 of_property_read_u32(twl6040_core_node, "ti,vibrdrv-res",
291 &info->vibrdrv_res);
292 of_property_read_u32(twl6040_core_node, "ti,viblmotor-res",
293 &info->viblmotor_res);
294 of_property_read_u32(twl6040_core_node, "ti,vibrmotor-res",
295 &info->vibrmotor_res);
296 of_property_read_u32(twl6040_core_node, "ti,vddvibl-uV", &vddvibl_uV);
297 of_property_read_u32(twl6040_core_node, "ti,vddvibr-uV", &vddvibr_uV);
Peter Ujfalusi9ac7b1a2012-05-07 08:45:50 -0700298
Libo Chenf048dd12014-01-04 00:00:30 -0800299 of_node_put(twl6040_core_node);
300
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500301 if ((!info->vibldrv_res && !info->viblmotor_res) ||
302 (!info->vibrdrv_res && !info->vibrmotor_res)) {
303 dev_err(info->dev, "invalid vibra driver/motor resistance\n");
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800304 return -EINVAL;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500305 }
306
Peter Ujfalusi625cbe42011-07-04 19:50:02 +0300307 info->irq = platform_get_irq(pdev, 0);
308 if (info->irq < 0) {
309 dev_err(info->dev, "invalid irq\n");
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800310 return -EINVAL;
Peter Ujfalusi625cbe42011-07-04 19:50:02 +0300311 }
312
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500313 mutex_init(&info->mutex);
314
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800315 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
316 twl6040_vib_irq_handler, 0,
317 "twl6040_irq_vib", info);
318 if (ret) {
319 dev_err(info->dev, "VIB IRQ request failed: %d\n", ret);
320 return ret;
321 }
322
323 info->supplies[0].supply = "vddvibl";
324 info->supplies[1].supply = "vddvibr";
325 /*
326 * When booted with Device tree the regulators are attached to the
327 * parent device (twl6040 MFD core)
328 */
Peter Ujfalusi7bf2a982013-07-13 13:36:19 -0700329 ret = regulator_bulk_get(twl6040_core_dev, ARRAY_SIZE(info->supplies),
330 info->supplies);
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800331 if (ret) {
332 dev_err(info->dev, "couldn't get regulators %d\n", ret);
333 return ret;
334 }
335
336 if (vddvibl_uV) {
337 ret = regulator_set_voltage(info->supplies[0].consumer,
338 vddvibl_uV, vddvibl_uV);
339 if (ret) {
340 dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
341 ret);
342 goto err_regulator;
343 }
344 }
345
346 if (vddvibr_uV) {
347 ret = regulator_set_voltage(info->supplies[1].consumer,
348 vddvibr_uV, vddvibr_uV);
349 if (ret) {
350 dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
351 ret);
352 goto err_regulator;
353 }
354 }
355
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800356 INIT_WORK(&info->play_work, vibra_play_work);
357
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500358 info->input_dev = input_allocate_device();
359 if (info->input_dev == NULL) {
360 dev_err(info->dev, "couldn't allocate input device\n");
361 ret = -ENOMEM;
Peter Ujfalusi21fb9f02013-01-25 00:03:54 -0800362 goto err_regulator;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500363 }
364
365 input_set_drvdata(info->input_dev, info);
366
367 info->input_dev->name = "twl6040:vibrator";
368 info->input_dev->id.version = 1;
369 info->input_dev->dev.parent = pdev->dev.parent;
370 info->input_dev->close = twl6040_vibra_close;
371 __set_bit(FF_RUMBLE, info->input_dev->ffbit);
372
373 ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
374 if (ret < 0) {
375 dev_err(info->dev, "couldn't register vibrator to FF\n");
376 goto err_ialloc;
377 }
378
379 ret = input_register_device(info->input_dev);
380 if (ret < 0) {
381 dev_err(info->dev, "couldn't register input device\n");
382 goto err_iff;
383 }
384
385 platform_set_drvdata(pdev, info);
386
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500387 return 0;
388
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500389err_iff:
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800390 input_ff_destroy(info->input_dev);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500391err_ialloc:
392 input_free_device(info->input_dev);
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800393err_regulator:
394 regulator_bulk_free(ARRAY_SIZE(info->supplies), info->supplies);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500395 return ret;
396}
397
Bill Pembertone2619cf2012-11-23 21:50:47 -0800398static int twl6040_vibra_remove(struct platform_device *pdev)
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500399{
400 struct vibra_info *info = platform_get_drvdata(pdev);
401
402 input_unregister_device(info->input_dev);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500403 regulator_bulk_free(ARRAY_SIZE(info->supplies), info->supplies);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500404
405 return 0;
406}
407
408static struct platform_driver twl6040_vibra_driver = {
409 .probe = twl6040_vibra_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800410 .remove = twl6040_vibra_remove,
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500411 .driver = {
412 .name = "twl6040-vibra",
413 .owner = THIS_MODULE,
414 .pm = &twl6040_vibra_pm_ops,
415 },
416};
JJ Ding840a7462011-11-29 11:08:40 -0800417module_platform_driver(twl6040_vibra_driver);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500418
419MODULE_ALIAS("platform:twl6040-vibra");
420MODULE_DESCRIPTION("TWL6040 Vibra driver");
421MODULE_LICENSE("GPL");
422MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
423MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");