]>
Commit | Line | Data |
---|---|---|
2b27bdcc | 1 | // SPDX-License-Identifier: GPL-2.0-only |
cc697d38 MLC |
2 | /* |
3 | * twl6040-vibra.c - TWL6040 Vibrator driver | |
4 | * | |
5 | * Author: Jorge Eduardo Candelaria <jorge.candelaria@ti.com> | |
6 | * Author: Misael Lopez Cruz <misael.lopez@ti.com> | |
7 | * | |
8 | * Copyright: (C) 2011 Texas Instruments, Inc. | |
9 | * | |
10 | * Based on twl4030-vibra.c by Henrik Saari <henrik.saari@nokia.com> | |
11 | * Felipe Balbi <felipe.balbi@nokia.com> | |
12 | * Jari Vanhala <ext-javi.vanhala@nokia.com> | |
cc697d38 MLC |
13 | */ |
14 | #include <linux/module.h> | |
15 | #include <linux/platform_device.h> | |
9ac7b1a3 | 16 | #include <linux/of.h> |
cc697d38 | 17 | #include <linux/workqueue.h> |
8eaeb939 | 18 | #include <linux/input.h> |
cc697d38 MLC |
19 | #include <linux/mfd/twl6040.h> |
20 | #include <linux/slab.h> | |
21 | #include <linux/delay.h> | |
22 | #include <linux/regulator/consumer.h> | |
23 | ||
24 | #define EFFECT_DIR_180_DEG 0x8000 | |
25 | ||
26 | /* Recommended modulation index 85% */ | |
27 | #define TWL6040_VIBRA_MOD 85 | |
28 | ||
29 | #define TWL6040_NUM_SUPPLIES 2 | |
30 | ||
31 | struct vibra_info { | |
32 | struct device *dev; | |
33 | struct input_dev *input_dev; | |
cc697d38 | 34 | struct work_struct play_work; |
9e4cc255 | 35 | |
625cbe46 | 36 | int irq; |
cc697d38 MLC |
37 | |
38 | bool enabled; | |
39 | int weak_speed; | |
40 | int strong_speed; | |
41 | int direction; | |
42 | ||
43 | unsigned int vibldrv_res; | |
44 | unsigned int vibrdrv_res; | |
45 | unsigned int viblmotor_res; | |
46 | unsigned int vibrmotor_res; | |
47 | ||
48 | struct regulator_bulk_data supplies[TWL6040_NUM_SUPPLIES]; | |
49 | ||
50 | struct twl6040 *twl6040; | |
51 | }; | |
52 | ||
53 | static irqreturn_t twl6040_vib_irq_handler(int irq, void *data) | |
54 | { | |
55 | struct vibra_info *info = data; | |
56 | struct twl6040 *twl6040 = info->twl6040; | |
57 | u8 status; | |
58 | ||
59 | status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS); | |
60 | if (status & TWL6040_VIBLOCDET) { | |
61 | dev_warn(info->dev, "Left Vibrator overcurrent detected\n"); | |
62 | twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLL, | |
1e036f65 | 63 | TWL6040_VIBENA); |
cc697d38 MLC |
64 | } |
65 | if (status & TWL6040_VIBROCDET) { | |
66 | dev_warn(info->dev, "Right Vibrator overcurrent detected\n"); | |
67 | twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR, | |
1e036f65 | 68 | TWL6040_VIBENA); |
cc697d38 MLC |
69 | } |
70 | ||
71 | return IRQ_HANDLED; | |
72 | } | |
73 | ||
74 | static void twl6040_vibra_enable(struct vibra_info *info) | |
75 | { | |
76 | struct twl6040 *twl6040 = info->twl6040; | |
77 | int ret; | |
78 | ||
79 | ret = regulator_bulk_enable(ARRAY_SIZE(info->supplies), info->supplies); | |
80 | if (ret) { | |
81 | dev_err(info->dev, "failed to enable regulators %d\n", ret); | |
82 | return; | |
83 | } | |
84 | ||
85 | twl6040_power(info->twl6040, 1); | |
7e968985 | 86 | if (twl6040_get_revid(twl6040) <= TWL6040_REV_ES1_1) { |
cc697d38 MLC |
87 | /* |
88 | * ERRATA: Disable overcurrent protection for at least | |
89 | * 3ms when enabling vibrator drivers to avoid false | |
90 | * overcurrent detection | |
91 | */ | |
92 | twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, | |
1e036f65 | 93 | TWL6040_VIBENA | TWL6040_VIBCTRL); |
cc697d38 | 94 | twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, |
1e036f65 | 95 | TWL6040_VIBENA | TWL6040_VIBCTRL); |
cc697d38 MLC |
96 | usleep_range(3000, 3500); |
97 | } | |
98 | ||
99 | twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, | |
1e036f65 | 100 | TWL6040_VIBENA); |
cc697d38 | 101 | twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, |
1e036f65 | 102 | TWL6040_VIBENA); |
cc697d38 MLC |
103 | |
104 | info->enabled = true; | |
105 | } | |
106 | ||
107 | static void twl6040_vibra_disable(struct vibra_info *info) | |
108 | { | |
109 | struct twl6040 *twl6040 = info->twl6040; | |
110 | ||
111 | twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL, 0x00); | |
112 | twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR, 0x00); | |
113 | twl6040_power(info->twl6040, 0); | |
114 | ||
115 | regulator_bulk_disable(ARRAY_SIZE(info->supplies), info->supplies); | |
116 | ||
117 | info->enabled = false; | |
118 | } | |
119 | ||
120 | static u8 twl6040_vibra_code(int vddvib, int vibdrv_res, int motor_res, | |
121 | int speed, int direction) | |
122 | { | |
123 | int vpk, max_code; | |
124 | u8 vibdat; | |
125 | ||
126 | /* output swing */ | |
127 | vpk = (vddvib * motor_res * TWL6040_VIBRA_MOD) / | |
128 | (100 * (vibdrv_res + motor_res)); | |
129 | ||
130 | /* 50mV per VIBDAT code step */ | |
131 | max_code = vpk / 50; | |
132 | if (max_code > TWL6040_VIBDAT_MAX) | |
133 | max_code = TWL6040_VIBDAT_MAX; | |
134 | ||
135 | /* scale speed to max allowed code */ | |
136 | vibdat = (u8)((speed * max_code) / USHRT_MAX); | |
137 | ||
138 | /* 2's complement for direction > 180 degrees */ | |
139 | vibdat *= direction; | |
140 | ||
141 | return vibdat; | |
142 | } | |
143 | ||
144 | static void twl6040_vibra_set_effect(struct vibra_info *info) | |
145 | { | |
146 | struct twl6040 *twl6040 = info->twl6040; | |
147 | u8 vibdatl, vibdatr; | |
148 | int volt; | |
149 | ||
150 | /* weak motor */ | |
151 | volt = regulator_get_voltage(info->supplies[0].consumer) / 1000; | |
152 | vibdatl = twl6040_vibra_code(volt, info->vibldrv_res, | |
153 | info->viblmotor_res, | |
154 | info->weak_speed, info->direction); | |
155 | ||
156 | /* strong motor */ | |
157 | volt = regulator_get_voltage(info->supplies[1].consumer) / 1000; | |
158 | vibdatr = twl6040_vibra_code(volt, info->vibrdrv_res, | |
159 | info->vibrmotor_res, | |
160 | info->strong_speed, info->direction); | |
161 | ||
162 | twl6040_reg_write(twl6040, TWL6040_REG_VIBDATL, vibdatl); | |
163 | twl6040_reg_write(twl6040, TWL6040_REG_VIBDATR, vibdatr); | |
164 | } | |
165 | ||
166 | static void vibra_play_work(struct work_struct *work) | |
167 | { | |
168 | struct vibra_info *info = container_of(work, | |
169 | struct vibra_info, play_work); | |
28a994fa NS |
170 | int ret; |
171 | ||
172 | /* Do not allow effect, while the routing is set to use audio */ | |
173 | ret = twl6040_get_vibralr_status(info->twl6040); | |
174 | if (ret & TWL6040_VIBSEL) { | |
175 | dev_info(info->dev, "Vibra is configured for audio\n"); | |
176 | return; | |
177 | } | |
cc697d38 | 178 | |
cc697d38 MLC |
179 | if (info->weak_speed || info->strong_speed) { |
180 | if (!info->enabled) | |
181 | twl6040_vibra_enable(info); | |
182 | ||
183 | twl6040_vibra_set_effect(info); | |
184 | } else if (info->enabled) | |
185 | twl6040_vibra_disable(info); | |
186 | ||
cc697d38 MLC |
187 | } |
188 | ||
189 | static int vibra_play(struct input_dev *input, void *data, | |
190 | struct ff_effect *effect) | |
191 | { | |
192 | struct vibra_info *info = input_get_drvdata(input); | |
5f07c32e | 193 | |
cc697d38 MLC |
194 | info->weak_speed = effect->u.rumble.weak_magnitude; |
195 | info->strong_speed = effect->u.rumble.strong_magnitude; | |
196 | info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1; | |
197 | ||
5f7fb6f1 | 198 | schedule_work(&info->play_work); |
cc697d38 MLC |
199 | |
200 | return 0; | |
201 | } | |
202 | ||
203 | static void twl6040_vibra_close(struct input_dev *input) | |
204 | { | |
205 | struct vibra_info *info = input_get_drvdata(input); | |
206 | ||
207 | cancel_work_sync(&info->play_work); | |
208 | ||
cc697d38 MLC |
209 | if (info->enabled) |
210 | twl6040_vibra_disable(info); | |
cc697d38 MLC |
211 | } |
212 | ||
97a652a8 | 213 | static int __maybe_unused twl6040_vibra_suspend(struct device *dev) |
cc697d38 MLC |
214 | { |
215 | struct platform_device *pdev = to_platform_device(dev); | |
216 | struct vibra_info *info = platform_get_drvdata(pdev); | |
217 | ||
9e4cc255 | 218 | cancel_work_sync(&info->play_work); |
cc697d38 MLC |
219 | |
220 | if (info->enabled) | |
221 | twl6040_vibra_disable(info); | |
222 | ||
cc697d38 MLC |
223 | return 0; |
224 | } | |
cc697d38 MLC |
225 | |
226 | static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL); | |
227 | ||
5298cc4c | 228 | static int twl6040_vibra_probe(struct platform_device *pdev) |
cc697d38 | 229 | { |
e7ec014a | 230 | struct device *twl6040_core_dev = pdev->dev.parent; |
32e573c4 | 231 | struct device_node *twl6040_core_node; |
cc697d38 | 232 | struct vibra_info *info; |
9ac7b1a3 PU |
233 | int vddvibl_uV = 0; |
234 | int vddvibr_uV = 0; | |
1f9e1470 | 235 | int error; |
cc697d38 | 236 | |
dcaf12a8 | 237 | twl6040_core_node = of_get_child_by_name(twl6040_core_dev->of_node, |
e7ec014a | 238 | "vibra"); |
7bf2a98a PU |
239 | if (!twl6040_core_node) { |
240 | dev_err(&pdev->dev, "parent of node is missing?\n"); | |
cc697d38 MLC |
241 | return -EINVAL; |
242 | } | |
243 | ||
b2ebcc1b | 244 | info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); |
cc697d38 | 245 | if (!info) { |
f048dd17 | 246 | of_node_put(twl6040_core_node); |
cc697d38 MLC |
247 | dev_err(&pdev->dev, "couldn't allocate memory\n"); |
248 | return -ENOMEM; | |
249 | } | |
250 | ||
251 | info->dev = &pdev->dev; | |
9ac7b1a3 | 252 | |
cc697d38 | 253 | info->twl6040 = dev_get_drvdata(pdev->dev.parent); |
7bf2a98a PU |
254 | |
255 | of_property_read_u32(twl6040_core_node, "ti,vibldrv-res", | |
256 | &info->vibldrv_res); | |
257 | of_property_read_u32(twl6040_core_node, "ti,vibrdrv-res", | |
258 | &info->vibrdrv_res); | |
259 | of_property_read_u32(twl6040_core_node, "ti,viblmotor-res", | |
260 | &info->viblmotor_res); | |
261 | of_property_read_u32(twl6040_core_node, "ti,vibrmotor-res", | |
262 | &info->vibrmotor_res); | |
263 | of_property_read_u32(twl6040_core_node, "ti,vddvibl-uV", &vddvibl_uV); | |
264 | of_property_read_u32(twl6040_core_node, "ti,vddvibr-uV", &vddvibr_uV); | |
9ac7b1a3 | 265 | |
f048dd17 LC |
266 | of_node_put(twl6040_core_node); |
267 | ||
cc697d38 MLC |
268 | if ((!info->vibldrv_res && !info->viblmotor_res) || |
269 | (!info->vibrdrv_res && !info->vibrmotor_res)) { | |
270 | dev_err(info->dev, "invalid vibra driver/motor resistance\n"); | |
b2ebcc1b | 271 | return -EINVAL; |
cc697d38 MLC |
272 | } |
273 | ||
625cbe46 PU |
274 | info->irq = platform_get_irq(pdev, 0); |
275 | if (info->irq < 0) { | |
276 | dev_err(info->dev, "invalid irq\n"); | |
b2ebcc1b | 277 | return -EINVAL; |
625cbe46 PU |
278 | } |
279 | ||
1f9e1470 | 280 | error = devm_request_threaded_irq(&pdev->dev, info->irq, NULL, |
4be01a29 FE |
281 | twl6040_vib_irq_handler, |
282 | IRQF_ONESHOT, | |
1f9e1470 FE |
283 | "twl6040_irq_vib", info); |
284 | if (error) { | |
285 | dev_err(info->dev, "VIB IRQ request failed: %d\n", error); | |
286 | return error; | |
cc697d38 MLC |
287 | } |
288 | ||
289 | info->supplies[0].supply = "vddvibl"; | |
290 | info->supplies[1].supply = "vddvibr"; | |
e7ec014a PU |
291 | /* |
292 | * When booted with Device tree the regulators are attached to the | |
293 | * parent device (twl6040 MFD core) | |
294 | */ | |
1f9e1470 FE |
295 | error = devm_regulator_bulk_get(twl6040_core_dev, |
296 | ARRAY_SIZE(info->supplies), | |
297 | info->supplies); | |
298 | if (error) { | |
299 | dev_err(info->dev, "couldn't get regulators %d\n", error); | |
300 | return error; | |
cc697d38 MLC |
301 | } |
302 | ||
9ac7b1a3 | 303 | if (vddvibl_uV) { |
1f9e1470 FE |
304 | error = regulator_set_voltage(info->supplies[0].consumer, |
305 | vddvibl_uV, vddvibl_uV); | |
306 | if (error) { | |
cc697d38 | 307 | dev_err(info->dev, "failed to set VDDVIBL volt %d\n", |
1f9e1470 FE |
308 | error); |
309 | return error; | |
cc697d38 MLC |
310 | } |
311 | } | |
312 | ||
9ac7b1a3 | 313 | if (vddvibr_uV) { |
1f9e1470 FE |
314 | error = regulator_set_voltage(info->supplies[1].consumer, |
315 | vddvibr_uV, vddvibr_uV); | |
316 | if (error) { | |
cc697d38 | 317 | dev_err(info->dev, "failed to set VDDVIBR volt %d\n", |
1f9e1470 FE |
318 | error); |
319 | return error; | |
cc697d38 MLC |
320 | } |
321 | } | |
322 | ||
cc697d38 MLC |
323 | INIT_WORK(&info->play_work, vibra_play_work); |
324 | ||
1f9e1470 FE |
325 | info->input_dev = devm_input_allocate_device(&pdev->dev); |
326 | if (!info->input_dev) { | |
b2ebcc1b | 327 | dev_err(info->dev, "couldn't allocate input device\n"); |
1f9e1470 | 328 | return -ENOMEM; |
b2ebcc1b PU |
329 | } |
330 | ||
331 | input_set_drvdata(info->input_dev, info); | |
332 | ||
333 | info->input_dev->name = "twl6040:vibrator"; | |
334 | info->input_dev->id.version = 1; | |
b2ebcc1b PU |
335 | info->input_dev->close = twl6040_vibra_close; |
336 | __set_bit(FF_RUMBLE, info->input_dev->ffbit); | |
337 | ||
1f9e1470 FE |
338 | error = input_ff_create_memless(info->input_dev, NULL, vibra_play); |
339 | if (error) { | |
b2ebcc1b | 340 | dev_err(info->dev, "couldn't register vibrator to FF\n"); |
1f9e1470 | 341 | return error; |
b2ebcc1b PU |
342 | } |
343 | ||
1f9e1470 FE |
344 | error = input_register_device(info->input_dev); |
345 | if (error) { | |
b2ebcc1b | 346 | dev_err(info->dev, "couldn't register input device\n"); |
1f9e1470 | 347 | return error; |
b2ebcc1b PU |
348 | } |
349 | ||
350 | platform_set_drvdata(pdev, info); | |
351 | ||
cc697d38 MLC |
352 | return 0; |
353 | } | |
354 | ||
355 | static struct platform_driver twl6040_vibra_driver = { | |
356 | .probe = twl6040_vibra_probe, | |
cc697d38 MLC |
357 | .driver = { |
358 | .name = "twl6040-vibra", | |
cc697d38 MLC |
359 | .pm = &twl6040_vibra_pm_ops, |
360 | }, | |
361 | }; | |
840a746b | 362 | module_platform_driver(twl6040_vibra_driver); |
cc697d38 MLC |
363 | |
364 | MODULE_ALIAS("platform:twl6040-vibra"); | |
365 | MODULE_DESCRIPTION("TWL6040 Vibra driver"); | |
366 | MODULE_LICENSE("GPL"); | |
367 | MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>"); | |
368 | MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>"); |