blob: 5690eb7ff954d23390d966d2f5c7137513a68b9f [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;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -050048 struct work_struct play_work;
H. Nikolaus Schaller9e4cc252016-04-25 14:02:16 -070049
Peter Ujfalusi625cbe42011-07-04 19:50:02 +030050 int irq;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -050051
52 bool enabled;
53 int weak_speed;
54 int strong_speed;
55 int direction;
56
57 unsigned int vibldrv_res;
58 unsigned int vibrdrv_res;
59 unsigned int viblmotor_res;
60 unsigned int vibrmotor_res;
61
62 struct regulator_bulk_data supplies[TWL6040_NUM_SUPPLIES];
63
64 struct twl6040 *twl6040;
65};
66
67static irqreturn_t twl6040_vib_irq_handler(int irq, void *data)
68{
69 struct vibra_info *info = data;
70 struct twl6040 *twl6040 = info->twl6040;
71 u8 status;
72
73 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
74 if (status & TWL6040_VIBLOCDET) {
75 dev_warn(info->dev, "Left Vibrator overcurrent detected\n");
76 twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLL,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +030077 TWL6040_VIBENA);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -050078 }
79 if (status & TWL6040_VIBROCDET) {
80 dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
81 twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +030082 TWL6040_VIBENA);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -050083 }
84
85 return IRQ_HANDLED;
86}
87
88static void twl6040_vibra_enable(struct vibra_info *info)
89{
90 struct twl6040 *twl6040 = info->twl6040;
91 int ret;
92
93 ret = regulator_bulk_enable(ARRAY_SIZE(info->supplies), info->supplies);
94 if (ret) {
95 dev_err(info->dev, "failed to enable regulators %d\n", ret);
96 return;
97 }
98
99 twl6040_power(info->twl6040, 1);
Peter Ujfalusi7e968982011-09-15 15:39:25 +0300100 if (twl6040_get_revid(twl6040) <= TWL6040_REV_ES1_1) {
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500101 /*
102 * ERRATA: Disable overcurrent protection for at least
103 * 3ms when enabling vibrator drivers to avoid false
104 * overcurrent detection
105 */
106 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +0300107 TWL6040_VIBENA | TWL6040_VIBCTRL);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500108 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +0300109 TWL6040_VIBENA | TWL6040_VIBCTRL);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500110 usleep_range(3000, 3500);
111 }
112
113 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +0300114 TWL6040_VIBENA);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500115 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
Peter Ujfalusi1e036f62011-10-12 11:57:53 +0300116 TWL6040_VIBENA);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500117
118 info->enabled = true;
119}
120
121static void twl6040_vibra_disable(struct vibra_info *info)
122{
123 struct twl6040 *twl6040 = info->twl6040;
124
125 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, 0x00);
126 twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, 0x00);
127 twl6040_power(info->twl6040, 0);
128
129 regulator_bulk_disable(ARRAY_SIZE(info->supplies), info->supplies);
130
131 info->enabled = false;
132}
133
134static u8 twl6040_vibra_code(int vddvib, int vibdrv_res, int motor_res,
135 int speed, int direction)
136{
137 int vpk, max_code;
138 u8 vibdat;
139
140 /* output swing */
141 vpk = (vddvib * motor_res * TWL6040_VIBRA_MOD) /
142 (100 * (vibdrv_res + motor_res));
143
144 /* 50mV per VIBDAT code step */
145 max_code = vpk / 50;
146 if (max_code > TWL6040_VIBDAT_MAX)
147 max_code = TWL6040_VIBDAT_MAX;
148
149 /* scale speed to max allowed code */
150 vibdat = (u8)((speed * max_code) / USHRT_MAX);
151
152 /* 2's complement for direction > 180 degrees */
153 vibdat *= direction;
154
155 return vibdat;
156}
157
158static void twl6040_vibra_set_effect(struct vibra_info *info)
159{
160 struct twl6040 *twl6040 = info->twl6040;
161 u8 vibdatl, vibdatr;
162 int volt;
163
164 /* weak motor */
165 volt = regulator_get_voltage(info->supplies[0].consumer) / 1000;
166 vibdatl = twl6040_vibra_code(volt, info->vibldrv_res,
167 info->viblmotor_res,
168 info->weak_speed, info->direction);
169
170 /* strong motor */
171 volt = regulator_get_voltage(info->supplies[1].consumer) / 1000;
172 vibdatr = twl6040_vibra_code(volt, info->vibrdrv_res,
173 info->vibrmotor_res,
174 info->strong_speed, info->direction);
175
176 twl6040_reg_write(twl6040, TWL6040_REG_VIBDATL, vibdatl);
177 twl6040_reg_write(twl6040, TWL6040_REG_VIBDATR, vibdatr);
178}
179
180static void vibra_play_work(struct work_struct *work)
181{
182 struct vibra_info *info = container_of(work,
183 struct vibra_info, play_work);
H. Nikolaus Schaller28a994f2016-04-25 14:02:36 -0700184 int ret;
185
186 /* Do not allow effect, while the routing is set to use audio */
187 ret = twl6040_get_vibralr_status(info->twl6040);
188 if (ret & TWL6040_VIBSEL) {
189 dev_info(info->dev, "Vibra is configured for audio\n");
190 return;
191 }
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500192
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500193 if (info->weak_speed || info->strong_speed) {
194 if (!info->enabled)
195 twl6040_vibra_enable(info);
196
197 twl6040_vibra_set_effect(info);
198 } else if (info->enabled)
199 twl6040_vibra_disable(info);
200
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500201}
202
203static int vibra_play(struct input_dev *input, void *data,
204 struct ff_effect *effect)
205{
206 struct vibra_info *info = input_get_drvdata(input);
Peter Ujfalusi5f07c322011-10-12 11:57:56 +0300207
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500208 info->weak_speed = effect->u.rumble.weak_magnitude;
209 info->strong_speed = effect->u.rumble.strong_magnitude;
210 info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
211
H. Nikolaus Schaller5f7fb6f2016-04-18 14:47:14 -0700212 schedule_work(&info->play_work);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500213
214 return 0;
215}
216
217static void twl6040_vibra_close(struct input_dev *input)
218{
219 struct vibra_info *info = input_get_drvdata(input);
220
221 cancel_work_sync(&info->play_work);
222
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500223 if (info->enabled)
224 twl6040_vibra_disable(info);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500225}
226
Jingoo Han97a652a2014-11-02 00:02:46 -0700227static int __maybe_unused twl6040_vibra_suspend(struct device *dev)
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500228{
229 struct platform_device *pdev = to_platform_device(dev);
230 struct vibra_info *info = platform_get_drvdata(pdev);
231
H. Nikolaus Schaller9e4cc252016-04-25 14:02:16 -0700232 cancel_work_sync(&info->play_work);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500233
234 if (info->enabled)
235 twl6040_vibra_disable(info);
236
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500237 return 0;
238}
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500239
240static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL);
241
Bill Pemberton5298cc42012-11-23 21:38:25 -0800242static int twl6040_vibra_probe(struct platform_device *pdev)
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500243{
Peter Ujfalusie7ec0142012-06-12 01:10:02 -0700244 struct device *twl6040_core_dev = pdev->dev.parent;
Dmitry Torokhov32e573c2014-01-04 00:08:49 -0800245 struct device_node *twl6040_core_node;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500246 struct vibra_info *info;
Peter Ujfalusi9ac7b1a2012-05-07 08:45:50 -0700247 int vddvibl_uV = 0;
248 int vddvibr_uV = 0;
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700249 int error;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500250
H. Nikolaus Schallerc52c5452016-05-09 17:01:01 -0700251 of_node_get(twl6040_core_dev->of_node);
Peter Ujfalusie7ec0142012-06-12 01:10:02 -0700252 twl6040_core_node = of_find_node_by_name(twl6040_core_dev->of_node,
253 "vibra");
Peter Ujfalusi7bf2a982013-07-13 13:36:19 -0700254 if (!twl6040_core_node) {
255 dev_err(&pdev->dev, "parent of node is missing?\n");
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500256 return -EINVAL;
257 }
258
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800259 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500260 if (!info) {
Libo Chenf048dd12014-01-04 00:00:30 -0800261 of_node_put(twl6040_core_node);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500262 dev_err(&pdev->dev, "couldn't allocate memory\n");
263 return -ENOMEM;
264 }
265
266 info->dev = &pdev->dev;
Peter Ujfalusi9ac7b1a2012-05-07 08:45:50 -0700267
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500268 info->twl6040 = dev_get_drvdata(pdev->dev.parent);
Peter Ujfalusi7bf2a982013-07-13 13:36:19 -0700269
270 of_property_read_u32(twl6040_core_node, "ti,vibldrv-res",
271 &info->vibldrv_res);
272 of_property_read_u32(twl6040_core_node, "ti,vibrdrv-res",
273 &info->vibrdrv_res);
274 of_property_read_u32(twl6040_core_node, "ti,viblmotor-res",
275 &info->viblmotor_res);
276 of_property_read_u32(twl6040_core_node, "ti,vibrmotor-res",
277 &info->vibrmotor_res);
278 of_property_read_u32(twl6040_core_node, "ti,vddvibl-uV", &vddvibl_uV);
279 of_property_read_u32(twl6040_core_node, "ti,vddvibr-uV", &vddvibr_uV);
Peter Ujfalusi9ac7b1a2012-05-07 08:45:50 -0700280
Libo Chenf048dd12014-01-04 00:00:30 -0800281 of_node_put(twl6040_core_node);
282
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500283 if ((!info->vibldrv_res && !info->viblmotor_res) ||
284 (!info->vibrdrv_res && !info->vibrmotor_res)) {
285 dev_err(info->dev, "invalid vibra driver/motor resistance\n");
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800286 return -EINVAL;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500287 }
288
Peter Ujfalusi625cbe42011-07-04 19:50:02 +0300289 info->irq = platform_get_irq(pdev, 0);
290 if (info->irq < 0) {
291 dev_err(info->dev, "invalid irq\n");
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800292 return -EINVAL;
Peter Ujfalusi625cbe42011-07-04 19:50:02 +0300293 }
294
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700295 error = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
Fabio Estevam4be01a22015-05-15 15:55:42 -0700296 twl6040_vib_irq_handler,
297 IRQF_ONESHOT,
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700298 "twl6040_irq_vib", info);
299 if (error) {
300 dev_err(info->dev, "VIB IRQ request failed: %d\n", error);
301 return error;
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800302 }
303
304 info->supplies[0].supply = "vddvibl";
305 info->supplies[1].supply = "vddvibr";
306 /*
307 * When booted with Device tree the regulators are attached to the
308 * parent device (twl6040 MFD core)
309 */
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700310 error = devm_regulator_bulk_get(twl6040_core_dev,
311 ARRAY_SIZE(info->supplies),
312 info->supplies);
313 if (error) {
314 dev_err(info->dev, "couldn't get regulators %d\n", error);
315 return error;
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800316 }
317
318 if (vddvibl_uV) {
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700319 error = regulator_set_voltage(info->supplies[0].consumer,
320 vddvibl_uV, vddvibl_uV);
321 if (error) {
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800322 dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700323 error);
324 return error;
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800325 }
326 }
327
328 if (vddvibr_uV) {
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700329 error = regulator_set_voltage(info->supplies[1].consumer,
330 vddvibr_uV, vddvibr_uV);
331 if (error) {
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800332 dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700333 error);
334 return error;
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800335 }
336 }
337
Peter Ujfalusib2ebcc12013-01-25 00:03:50 -0800338 INIT_WORK(&info->play_work, vibra_play_work);
339
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700340 info->input_dev = devm_input_allocate_device(&pdev->dev);
341 if (!info->input_dev) {
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500342 dev_err(info->dev, "couldn't allocate input device\n");
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700343 return -ENOMEM;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500344 }
345
346 input_set_drvdata(info->input_dev, info);
347
348 info->input_dev->name = "twl6040:vibrator";
349 info->input_dev->id.version = 1;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500350 info->input_dev->close = twl6040_vibra_close;
351 __set_bit(FF_RUMBLE, info->input_dev->ffbit);
352
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700353 error = input_ff_create_memless(info->input_dev, NULL, vibra_play);
354 if (error) {
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500355 dev_err(info->dev, "couldn't register vibrator to FF\n");
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700356 return error;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500357 }
358
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700359 error = input_register_device(info->input_dev);
360 if (error) {
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500361 dev_err(info->dev, "couldn't register input device\n");
Fabio Estevam1f9e1472014-04-25 09:21:12 -0700362 return error;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500363 }
364
365 platform_set_drvdata(pdev, info);
366
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500367 return 0;
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500368}
369
370static struct platform_driver twl6040_vibra_driver = {
371 .probe = twl6040_vibra_probe,
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500372 .driver = {
373 .name = "twl6040-vibra",
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500374 .pm = &twl6040_vibra_pm_ops,
375 },
376};
JJ Ding840a7462011-11-29 11:08:40 -0800377module_platform_driver(twl6040_vibra_driver);
Misael Lopez Cruzcc697d382011-05-01 03:51:24 -0500378
379MODULE_ALIAS("platform:twl6040-vibra");
380MODULE_DESCRIPTION("TWL6040 Vibra driver");
381MODULE_LICENSE("GPL");
382MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
383MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");