Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  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>
 
 30#include <linux/workqueue.h>
 31#include <linux/i2c/twl.h>
 32#include <linux/mfd/twl6040.h>
 33#include <linux/slab.h>
 34#include <linux/delay.h>
 35#include <linux/regulator/consumer.h>
 36
 37#define EFFECT_DIR_180_DEG	0x8000
 38
 39/* Recommended modulation index 85% */
 40#define TWL6040_VIBRA_MOD	85
 41
 42#define TWL6040_NUM_SUPPLIES 2
 43
 44struct vibra_info {
 45	struct device *dev;
 46	struct input_dev *input_dev;
 47	struct workqueue_struct *workqueue;
 48	struct work_struct play_work;
 49	struct mutex mutex;
 50	int irq;
 51
 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,
 77				   TWL6040_VIBENAL);
 78	}
 79	if (status & TWL6040_VIBROCDET) {
 80		dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
 81		twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
 82				   TWL6040_VIBENAR);
 83	}
 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);
100	if (twl6040->rev <= TWL6040_REV_ES1_1) {
101		/*
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,
107				  TWL6040_VIBENAL | TWL6040_VIBCTRLL);
108		twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
109				  TWL6040_VIBENAR | TWL6040_VIBCTRLR);
110		usleep_range(3000, 3500);
111	}
112
113	twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
114			  TWL6040_VIBENAL);
115	twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
116			  TWL6040_VIBENAR);
117
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);
 
 
 
 
 
 
 
 
184
185	mutex_lock(&info->mutex);
186
187	if (info->weak_speed || info->strong_speed) {
188		if (!info->enabled)
189			twl6040_vibra_enable(info);
190
191		twl6040_vibra_set_effect(info);
192	} else if (info->enabled)
193		twl6040_vibra_disable(info);
194
195	mutex_unlock(&info->mutex);
196}
197
198static int vibra_play(struct input_dev *input, void *data,
199		      struct ff_effect *effect)
200{
201	struct vibra_info *info = input_get_drvdata(input);
202	int ret;
203
204	info->weak_speed = effect->u.rumble.weak_magnitude;
205	info->strong_speed = effect->u.rumble.strong_magnitude;
206	info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
207
208	ret = queue_work(info->workqueue, &info->play_work);
209	if (!ret) {
210		dev_info(&input->dev, "work is already on queue\n");
211		return ret;
212	}
213
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
223	mutex_lock(&info->mutex);
224
225	if (info->enabled)
226		twl6040_vibra_disable(info);
227
228	mutex_unlock(&info->mutex);
229}
230
231#if CONFIG_PM_SLEEP
232static int twl6040_vibra_suspend(struct device *dev)
233{
234	struct platform_device *pdev = to_platform_device(dev);
235	struct vibra_info *info = platform_get_drvdata(pdev);
236
237	mutex_lock(&info->mutex);
238
239	if (info->enabled)
240		twl6040_vibra_disable(info);
241
242	mutex_unlock(&info->mutex);
243
244	return 0;
245}
246
247#endif
248
249static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL);
250
251static int __devinit twl6040_vibra_probe(struct platform_device *pdev)
252{
253	struct twl4030_vibra_data *pdata = pdev->dev.platform_data;
 
254	struct vibra_info *info;
255	int ret;
256
257	if (!pdata) {
258		dev_err(&pdev->dev, "platform_data not available\n");
 
 
 
 
 
259		return -EINVAL;
260	}
261
262	info = kzalloc(sizeof(*info), GFP_KERNEL);
263	if (!info) {
 
264		dev_err(&pdev->dev, "couldn't allocate memory\n");
265		return -ENOMEM;
266	}
267
268	info->dev = &pdev->dev;
 
269	info->twl6040 = dev_get_drvdata(pdev->dev.parent);
270	info->vibldrv_res = pdata->vibldrv_res;
271	info->vibrdrv_res = pdata->vibrdrv_res;
272	info->viblmotor_res = pdata->viblmotor_res;
273	info->vibrmotor_res = pdata->vibrmotor_res;
 
 
 
 
 
 
 
 
 
 
274	if ((!info->vibldrv_res && !info->viblmotor_res) ||
275	    (!info->vibrdrv_res && !info->vibrmotor_res)) {
276		dev_err(info->dev, "invalid vibra driver/motor resistance\n");
277		ret = -EINVAL;
278		goto err_kzalloc;
279	}
280
281	info->irq = platform_get_irq(pdev, 0);
282	if (info->irq < 0) {
283		dev_err(info->dev, "invalid irq\n");
284		ret = -EINVAL;
285		goto err_kzalloc;
286	}
287
288	mutex_init(&info->mutex);
289
290	info->input_dev = input_allocate_device();
291	if (info->input_dev == NULL) {
292		dev_err(info->dev, "couldn't allocate input device\n");
293		ret = -ENOMEM;
294		goto err_kzalloc;
295	}
296
297	input_set_drvdata(info->input_dev, info);
298
299	info->input_dev->name = "twl6040:vibrator";
300	info->input_dev->id.version = 1;
301	info->input_dev->dev.parent = pdev->dev.parent;
302	info->input_dev->close = twl6040_vibra_close;
303	__set_bit(FF_RUMBLE, info->input_dev->ffbit);
304
305	ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
306	if (ret < 0) {
307		dev_err(info->dev, "couldn't register vibrator to FF\n");
308		goto err_ialloc;
309	}
310
311	ret = input_register_device(info->input_dev);
312	if (ret < 0) {
313		dev_err(info->dev, "couldn't register input device\n");
314		goto err_iff;
315	}
316
317	platform_set_drvdata(pdev, info);
318
319	ret = request_threaded_irq(info->irq, NULL, twl6040_vib_irq_handler, 0,
320				   "twl6040_irq_vib", info);
321	if (ret) {
322		dev_err(info->dev, "VIB IRQ request failed: %d\n", ret);
323		goto err_irq;
324	}
325
326	info->supplies[0].supply = "vddvibl";
327	info->supplies[1].supply = "vddvibr";
328	ret = regulator_bulk_get(info->dev, ARRAY_SIZE(info->supplies),
329				 info->supplies);
330	if (ret) {
331		dev_err(info->dev, "couldn't get regulators %d\n", ret);
332		goto err_regulator;
333	}
334
335	if (pdata->vddvibl_uV) {
336		ret = regulator_set_voltage(info->supplies[0].consumer,
337					    pdata->vddvibl_uV,
338					    pdata->vddvibl_uV);
339		if (ret) {
 
 
 
 
340			dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
341				ret);
342			goto err_voltage;
343		}
344	}
345
346	if (pdata->vddvibr_uV) {
347		ret = regulator_set_voltage(info->supplies[1].consumer,
348					    pdata->vddvibr_uV,
349					    pdata->vddvibr_uV);
350		if (ret) {
351			dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
352				ret);
353			goto err_voltage;
354		}
355	}
356
357	info->workqueue = alloc_workqueue("twl6040-vibra", 0, 0);
358	if (info->workqueue == NULL) {
359		dev_err(info->dev, "couldn't create workqueue\n");
360		ret = -ENOMEM;
361		goto err_voltage;
362	}
363	INIT_WORK(&info->play_work, vibra_play_work);
364
365	return 0;
 
 
 
 
366
367err_voltage:
368	regulator_bulk_free(ARRAY_SIZE(info->supplies), info->supplies);
369err_regulator:
370	free_irq(info->irq, info);
371err_irq:
372	input_unregister_device(info->input_dev);
373	info->input_dev = NULL;
374err_iff:
375	if (info->input_dev)
376		input_ff_destroy(info->input_dev);
377err_ialloc:
378	input_free_device(info->input_dev);
379err_kzalloc:
380	kfree(info);
381	return ret;
382}
383
384static int __devexit twl6040_vibra_remove(struct platform_device *pdev)
385{
386	struct vibra_info *info = platform_get_drvdata(pdev);
 
 
 
 
 
 
 
387
388	input_unregister_device(info->input_dev);
389	free_irq(info->irq, info);
390	regulator_bulk_free(ARRAY_SIZE(info->supplies), info->supplies);
391	destroy_workqueue(info->workqueue);
392	kfree(info);
 
 
393
394	return 0;
395}
396
397static struct platform_driver twl6040_vibra_driver = {
398	.probe		= twl6040_vibra_probe,
399	.remove		= __devexit_p(twl6040_vibra_remove),
400	.driver		= {
401		.name	= "twl6040-vibra",
402		.owner	= THIS_MODULE,
403		.pm	= &twl6040_vibra_pm_ops,
404	},
405};
406
407static int __init twl6040_vibra_init(void)
408{
409	return platform_driver_register(&twl6040_vibra_driver);
410}
411module_init(twl6040_vibra_init);
412
413static void __exit twl6040_vibra_exit(void)
414{
415	platform_driver_unregister(&twl6040_vibra_driver);
416}
417module_exit(twl6040_vibra_exit);
418
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>");
v4.6
  1/*
  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>
 30#include <linux/of.h>
 31#include <linux/workqueue.h>
 32#include <linux/input.h>
 33#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 work_struct play_work;
 49	struct mutex mutex;
 50	int irq;
 51
 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,
 77				   TWL6040_VIBENA);
 78	}
 79	if (status & TWL6040_VIBROCDET) {
 80		dev_warn(info->dev, "Right Vibrator overcurrent detected\n");
 81		twl6040_clear_bits(twl6040, TWL6040_REG_VIBCTLR,
 82				   TWL6040_VIBENA);
 83	}
 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);
100	if (twl6040_get_revid(twl6040) <= TWL6040_REV_ES1_1) {
101		/*
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,
107				  TWL6040_VIBENA | TWL6040_VIBCTRL);
108		twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
109				  TWL6040_VIBENA | TWL6040_VIBCTRL);
110		usleep_range(3000, 3500);
111	}
112
113	twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLL,
114			  TWL6040_VIBENA);
115	twl6040_reg_write(twl6040, TWL6040_REG_VIBCTLR,
116			  TWL6040_VIBENA);
117
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);
184	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	}
192
193	mutex_lock(&info->mutex);
194
195	if (info->weak_speed || info->strong_speed) {
196		if (!info->enabled)
197			twl6040_vibra_enable(info);
198
199		twl6040_vibra_set_effect(info);
200	} else if (info->enabled)
201		twl6040_vibra_disable(info);
202
203	mutex_unlock(&info->mutex);
204}
205
206static int vibra_play(struct input_dev *input, void *data,
207		      struct ff_effect *effect)
208{
209	struct vibra_info *info = input_get_drvdata(input);
 
210
211	info->weak_speed = effect->u.rumble.weak_magnitude;
212	info->strong_speed = effect->u.rumble.strong_magnitude;
213	info->direction = effect->direction < EFFECT_DIR_180_DEG ? 1 : -1;
214
215	schedule_work(&info->play_work);
 
 
 
 
216
217	return 0;
218}
219
220static void twl6040_vibra_close(struct input_dev *input)
221{
222	struct vibra_info *info = input_get_drvdata(input);
223
224	cancel_work_sync(&info->play_work);
225
226	mutex_lock(&info->mutex);
227
228	if (info->enabled)
229		twl6040_vibra_disable(info);
230
231	mutex_unlock(&info->mutex);
232}
233
234static int __maybe_unused twl6040_vibra_suspend(struct device *dev)
 
235{
236	struct platform_device *pdev = to_platform_device(dev);
237	struct vibra_info *info = platform_get_drvdata(pdev);
238
239	mutex_lock(&info->mutex);
240
241	if (info->enabled)
242		twl6040_vibra_disable(info);
243
244	mutex_unlock(&info->mutex);
245
246	return 0;
247}
248
 
 
249static SIMPLE_DEV_PM_OPS(twl6040_vibra_pm_ops, twl6040_vibra_suspend, NULL);
250
251static int twl6040_vibra_probe(struct platform_device *pdev)
252{
253	struct device *twl6040_core_dev = pdev->dev.parent;
254	struct device_node *twl6040_core_node;
255	struct vibra_info *info;
256	int vddvibl_uV = 0;
257	int vddvibr_uV = 0;
258	int error;
259
260	of_node_get(twl6040_core_dev->of_node);
261	twl6040_core_node = of_find_node_by_name(twl6040_core_dev->of_node,
262						 "vibra");
263	if (!twl6040_core_node) {
264		dev_err(&pdev->dev, "parent of node is missing?\n");
265		return -EINVAL;
266	}
267
268	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
269	if (!info) {
270		of_node_put(twl6040_core_node);
271		dev_err(&pdev->dev, "couldn't allocate memory\n");
272		return -ENOMEM;
273	}
274
275	info->dev = &pdev->dev;
276
277	info->twl6040 = dev_get_drvdata(pdev->dev.parent);
278
279	of_property_read_u32(twl6040_core_node, "ti,vibldrv-res",
280			     &info->vibldrv_res);
281	of_property_read_u32(twl6040_core_node, "ti,vibrdrv-res",
282			     &info->vibrdrv_res);
283	of_property_read_u32(twl6040_core_node, "ti,viblmotor-res",
284			     &info->viblmotor_res);
285	of_property_read_u32(twl6040_core_node, "ti,vibrmotor-res",
286			     &info->vibrmotor_res);
287	of_property_read_u32(twl6040_core_node, "ti,vddvibl-uV", &vddvibl_uV);
288	of_property_read_u32(twl6040_core_node, "ti,vddvibr-uV", &vddvibr_uV);
289
290	of_node_put(twl6040_core_node);
291
292	if ((!info->vibldrv_res && !info->viblmotor_res) ||
293	    (!info->vibrdrv_res && !info->vibrmotor_res)) {
294		dev_err(info->dev, "invalid vibra driver/motor resistance\n");
295		return -EINVAL;
 
296	}
297
298	info->irq = platform_get_irq(pdev, 0);
299	if (info->irq < 0) {
300		dev_err(info->dev, "invalid irq\n");
301		return -EINVAL;
 
302	}
303
304	mutex_init(&info->mutex);
305
306	error = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
307					  twl6040_vib_irq_handler,
308					  IRQF_ONESHOT,
309					  "twl6040_irq_vib", info);
310	if (error) {
311		dev_err(info->dev, "VIB IRQ request failed: %d\n", error);
312		return error;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313	}
314
315	info->supplies[0].supply = "vddvibl";
316	info->supplies[1].supply = "vddvibr";
317	/*
318	 * When booted with Device tree the regulators are attached to the
319	 * parent device (twl6040 MFD core)
320	 */
321	error = devm_regulator_bulk_get(twl6040_core_dev,
322					ARRAY_SIZE(info->supplies),
323					info->supplies);
324	if (error) {
325		dev_err(info->dev, "couldn't get regulators %d\n", error);
326		return error;
327	}
328
329	if (vddvibl_uV) {
330		error = regulator_set_voltage(info->supplies[0].consumer,
331					      vddvibl_uV, vddvibl_uV);
332		if (error) {
333			dev_err(info->dev, "failed to set VDDVIBL volt %d\n",
334				error);
335			return error;
336		}
337	}
338
339	if (vddvibr_uV) {
340		error = regulator_set_voltage(info->supplies[1].consumer,
341					      vddvibr_uV, vddvibr_uV);
342		if (error) {
 
343			dev_err(info->dev, "failed to set VDDVIBR volt %d\n",
344				error);
345			return error;
346		}
347	}
348
 
 
 
 
 
 
349	INIT_WORK(&info->play_work, vibra_play_work);
350
351	info->input_dev = devm_input_allocate_device(&pdev->dev);
352	if (!info->input_dev) {
353		dev_err(info->dev, "couldn't allocate input device\n");
354		return -ENOMEM;
355	}
356
357	input_set_drvdata(info->input_dev, info);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
358
359	info->input_dev->name = "twl6040:vibrator";
360	info->input_dev->id.version = 1;
361	info->input_dev->close = twl6040_vibra_close;
362	__set_bit(FF_RUMBLE, info->input_dev->ffbit);
363
364	error = input_ff_create_memless(info->input_dev, NULL, vibra_play);
365	if (error) {
366		dev_err(info->dev, "couldn't register vibrator to FF\n");
367		return error;
368	}
369
370	error = input_register_device(info->input_dev);
371	if (error) {
372		dev_err(info->dev, "couldn't register input device\n");
373		return error;
374	}
375
376	platform_set_drvdata(pdev, info);
377
378	return 0;
379}
380
381static struct platform_driver twl6040_vibra_driver = {
382	.probe		= twl6040_vibra_probe,
 
383	.driver		= {
384		.name	= "twl6040-vibra",
 
385		.pm	= &twl6040_vibra_pm_ops,
386	},
387};
388module_platform_driver(twl6040_vibra_driver);
 
 
 
 
 
 
 
 
 
 
 
389
390MODULE_ALIAS("platform:twl6040-vibra");
391MODULE_DESCRIPTION("TWL6040 Vibra driver");
392MODULE_LICENSE("GPL");
393MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
394MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");