Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * twl4030-vibra.c - TWL4030 Vibrator driver
  4 *
  5 * Copyright (C) 2008-2010 Nokia Corporation
  6 *
  7 * Written by Henrik Saari <henrik.saari@nokia.com>
  8 * Updates by Felipe Balbi <felipe.balbi@nokia.com>
  9 * Input by Jari Vanhala <ext-jari.vanhala@nokia.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 10 */
 11
 12#include <linux/module.h>
 13#include <linux/jiffies.h>
 14#include <linux/platform_device.h>
 15#include <linux/of.h>
 16#include <linux/workqueue.h>
 17#include <linux/mfd/twl.h>
 18#include <linux/mfd/twl4030-audio.h>
 19#include <linux/input.h>
 20#include <linux/slab.h>
 21
 22/* MODULE ID2 */
 23#define LEDEN		0x00
 24
 25/* ForceFeedback */
 26#define EFFECT_DIR_180_DEG	0x8000 /* range is 0 - 0xFFFF */
 27
 28struct vibra_info {
 29	struct device		*dev;
 30	struct input_dev	*input_dev;
 31
 
 32	struct work_struct	play_work;
 33
 34	bool			enabled;
 35	int			speed;
 36	int			direction;
 37
 38	bool			coexist;
 39};
 40
 41static void vibra_disable_leds(void)
 42{
 43	u8 reg;
 44
 45	/* Disable LEDA & LEDB, cannot be used with vibra (PWM) */
 46	twl_i2c_read_u8(TWL4030_MODULE_LED, &reg, LEDEN);
 47	reg &= ~0x03;
 48	twl_i2c_write_u8(TWL4030_MODULE_LED, LEDEN, reg);
 49}
 50
 51/* Powers H-Bridge and enables audio clk */
 52static void vibra_enable(struct vibra_info *info)
 53{
 54	u8 reg;
 55
 56	twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER);
 57
 58	/* turn H-Bridge on */
 59	twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
 60			&reg, TWL4030_REG_VIBRA_CTL);
 61	twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
 62			 (reg | TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
 63
 64	twl4030_audio_enable_resource(TWL4030_AUDIO_RES_APLL);
 65
 66	info->enabled = true;
 67}
 68
 69static void vibra_disable(struct vibra_info *info)
 70{
 71	u8 reg;
 72
 73	/* Power down H-Bridge */
 74	twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
 75			&reg, TWL4030_REG_VIBRA_CTL);
 76	twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
 77			 (reg & ~TWL4030_VIBRA_EN), TWL4030_REG_VIBRA_CTL);
 78
 79	twl4030_audio_disable_resource(TWL4030_AUDIO_RES_APLL);
 80	twl4030_audio_disable_resource(TWL4030_AUDIO_RES_POWER);
 81
 82	info->enabled = false;
 83}
 84
 85static void vibra_play_work(struct work_struct *work)
 86{
 87	struct vibra_info *info = container_of(work,
 88			struct vibra_info, play_work);
 89	int dir;
 90	int pwm;
 91	u8 reg;
 92
 93	dir = info->direction;
 94	pwm = info->speed;
 95
 96	twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
 97			&reg, TWL4030_REG_VIBRA_CTL);
 98	if (pwm && (!info->coexist || !(reg & TWL4030_VIBRA_SEL))) {
 99
100		if (!info->enabled)
101			vibra_enable(info);
102
103		/* set vibra rotation direction */
104		twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
105				&reg, TWL4030_REG_VIBRA_CTL);
106		reg = (dir) ? (reg | TWL4030_VIBRA_DIR) :
107			(reg & ~TWL4030_VIBRA_DIR);
108		twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
109				 reg, TWL4030_REG_VIBRA_CTL);
110
111		/* set PWM, 1 = max, 255 = min */
112		twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
113				 256 - pwm, TWL4030_REG_VIBRA_SET);
114	} else {
115		if (info->enabled)
116			vibra_disable(info);
117	}
118}
119
120/*** Input/ForceFeedback ***/
121
122static int vibra_play(struct input_dev *input, void *data,
123		      struct ff_effect *effect)
124{
125	struct vibra_info *info = input_get_drvdata(input);
126
127	info->speed = effect->u.rumble.strong_magnitude >> 8;
128	if (!info->speed)
129		info->speed = effect->u.rumble.weak_magnitude >> 9;
130	info->direction = effect->direction < EFFECT_DIR_180_DEG ? 0 : 1;
131	schedule_work(&info->play_work);
 
 
 
 
 
 
 
 
 
 
 
 
132	return 0;
133}
134
135static void twl4030_vibra_close(struct input_dev *input)
136{
137	struct vibra_info *info = input_get_drvdata(input);
138
139	cancel_work_sync(&info->play_work);
 
 
 
140
141	if (info->enabled)
142		vibra_disable(info);
143}
144
145/*** Module ***/
146static int __maybe_unused twl4030_vibra_suspend(struct device *dev)
 
147{
148	struct platform_device *pdev = to_platform_device(dev);
149	struct vibra_info *info = platform_get_drvdata(pdev);
150
151	if (info->enabled)
152		vibra_disable(info);
153
154	return 0;
155}
156
157static int __maybe_unused twl4030_vibra_resume(struct device *dev)
158{
159	vibra_disable_leds();
160	return 0;
161}
162
163static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
164			 twl4030_vibra_suspend, twl4030_vibra_resume);
 
165
166static bool twl4030_vibra_check_coexist(struct device_node *parent)
167{
168	struct device_node *node;
169
170	node = of_get_child_by_name(parent, "codec");
171	if (node) {
172		of_node_put(node);
173		return true;
174	}
175
176	return false;
177}
178
179static int twl4030_vibra_probe(struct platform_device *pdev)
180{
181	struct device_node *twl4030_core_node = pdev->dev.parent->of_node;
182	struct vibra_info *info;
183	int ret;
184
185	if (!twl4030_core_node) {
186		dev_dbg(&pdev->dev, "twl4030 OF node is missing\n");
187		return -EINVAL;
188	}
189
190	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
191	if (!info)
192		return -ENOMEM;
193
194	info->dev = &pdev->dev;
195	info->coexist = twl4030_vibra_check_coexist(twl4030_core_node);
196	INIT_WORK(&info->play_work, vibra_play_work);
197
198	info->input_dev = devm_input_allocate_device(&pdev->dev);
199	if (info->input_dev == NULL) {
200		dev_err(&pdev->dev, "couldn't allocate input device\n");
201		return -ENOMEM;
 
202	}
203
204	input_set_drvdata(info->input_dev, info);
205
206	info->input_dev->name = "twl4030:vibrator";
207	info->input_dev->id.version = 1;
 
 
208	info->input_dev->close = twl4030_vibra_close;
209	__set_bit(FF_RUMBLE, info->input_dev->ffbit);
210
211	ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
212	if (ret < 0) {
213		dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
214		return ret;
215	}
216
217	ret = input_register_device(info->input_dev);
218	if (ret < 0) {
219		dev_dbg(&pdev->dev, "couldn't register input device\n");
220		goto err_iff;
221	}
222
223	vibra_disable_leds();
224
225	platform_set_drvdata(pdev, info);
226	return 0;
227
228err_iff:
229	input_ff_destroy(info->input_dev);
 
 
 
 
230	return ret;
231}
232
 
 
 
 
 
 
 
 
 
 
 
 
233static struct platform_driver twl4030_vibra_driver = {
234	.probe		= twl4030_vibra_probe,
 
235	.driver		= {
236		.name	= "twl4030-vibra",
 
 
237		.pm	= &twl4030_vibra_pm_ops,
 
238	},
239};
240module_platform_driver(twl4030_vibra_driver);
 
 
 
 
 
 
 
 
 
 
 
241
242MODULE_ALIAS("platform:twl4030-vibra");
 
243MODULE_DESCRIPTION("TWL4030 Vibra driver");
244MODULE_LICENSE("GPL");
245MODULE_AUTHOR("Nokia Corporation");
v3.1
 
  1/*
  2 * twl4030-vibra.c - TWL4030 Vibrator driver
  3 *
  4 * Copyright (C) 2008-2010 Nokia Corporation
  5 *
  6 * Written by Henrik Saari <henrik.saari@nokia.com>
  7 * Updates by Felipe Balbi <felipe.balbi@nokia.com>
  8 * Input by Jari Vanhala <ext-jari.vanhala@nokia.com>
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License version 2 as
 12 * published by the Free Software Foundation.
 13 *
 14 * This program is distributed in the hope that it will be useful, but
 15 * WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with this program; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 22 * 02110-1301 USA
 23 *
 24 */
 25
 26#include <linux/module.h>
 27#include <linux/jiffies.h>
 28#include <linux/platform_device.h>
 
 29#include <linux/workqueue.h>
 30#include <linux/i2c/twl.h>
 31#include <linux/mfd/twl4030-audio.h>
 32#include <linux/input.h>
 33#include <linux/slab.h>
 34
 35/* MODULE ID2 */
 36#define LEDEN		0x00
 37
 38/* ForceFeedback */
 39#define EFFECT_DIR_180_DEG	0x8000 /* range is 0 - 0xFFFF */
 40
 41struct vibra_info {
 42	struct device		*dev;
 43	struct input_dev	*input_dev;
 44
 45	struct workqueue_struct *workqueue;
 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	/* Disable LEDA & LEDB, cannot be used with vibra (PWM) */
 60	twl_i2c_read_u8(TWL4030_MODULE_LED, &reg, LEDEN);
 61	reg &= ~0x03;
 62	twl_i2c_write_u8(TWL4030_MODULE_LED, LEDEN, reg);
 63}
 64
 65/* Powers H-Bridge and enables audio clk */
 66static void vibra_enable(struct vibra_info *info)
 67{
 68	u8 reg;
 69
 70	twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER);
 71
 72	/* turn H-Bridge on */
 73	twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
 74			&reg, 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	/* Power down H-Bridge */
 88	twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
 89			&reg, 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			&reg, TWL4030_REG_VIBRA_CTL);
112	if (pwm && (!info->coexist || !(reg & TWL4030_VIBRA_SEL))) {
113
114		if (!info->enabled)
115			vibra_enable(info);
116
117		/* set vibra rotation direction */
118		twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE,
119				&reg, 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		/* set PWM, 1 = max, 255 = min */
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/*** Input/ForceFeedback ***/
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	queue_work(info->workqueue, &info->play_work);
146	return 0;
147}
148
149static int twl4030_vibra_open(struct input_dev *input)
150{
151	struct vibra_info *info = input_get_drvdata(input);
152
153	info->workqueue = create_singlethread_workqueue("vibra");
154	if (info->workqueue == NULL) {
155		dev_err(&input->dev, "couldn't create workqueue\n");
156		return -ENOMEM;
157	}
158	return 0;
159}
160
161static void twl4030_vibra_close(struct input_dev *input)
162{
163	struct vibra_info *info = input_get_drvdata(input);
164
165	cancel_work_sync(&info->play_work);
166	INIT_WORK(&info->play_work, vibra_play_work); /* cleanup */
167	destroy_workqueue(info->workqueue);
168	info->workqueue = NULL;
169
170	if (info->enabled)
171		vibra_disable(info);
172}
173
174/*** Module ***/
175#if CONFIG_PM
176static int twl4030_vibra_suspend(struct device *dev)
177{
178	struct platform_device *pdev = to_platform_device(dev);
179	struct vibra_info *info = platform_get_drvdata(pdev);
180
181	if (info->enabled)
182		vibra_disable(info);
183
184	return 0;
185}
186
187static int twl4030_vibra_resume(struct device *dev)
188{
189	vibra_disable_leds();
190	return 0;
191}
192
193static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
194			 twl4030_vibra_suspend, twl4030_vibra_resume);
195#endif
196
197static int __devinit twl4030_vibra_probe(struct platform_device *pdev)
198{
199	struct twl4030_vibra_data *pdata = pdev->dev.platform_data;
 
 
 
 
 
 
 
 
 
 
 
 
 
200	struct vibra_info *info;
201	int ret;
202
203	if (!pdata) {
204		dev_dbg(&pdev->dev, "platform_data not available\n");
205		return -EINVAL;
206	}
207
208	info = kzalloc(sizeof(*info), GFP_KERNEL);
209	if (!info)
210		return -ENOMEM;
211
212	info->dev = &pdev->dev;
213	info->coexist = pdata->coexist;
214	INIT_WORK(&info->play_work, vibra_play_work);
215
216	info->input_dev = input_allocate_device();
217	if (info->input_dev == NULL) {
218		dev_err(&pdev->dev, "couldn't allocate input device\n");
219		ret = -ENOMEM;
220		goto err_kzalloc;
221	}
222
223	input_set_drvdata(info->input_dev, info);
224
225	info->input_dev->name = "twl4030:vibrator";
226	info->input_dev->id.version = 1;
227	info->input_dev->dev.parent = pdev->dev.parent;
228	info->input_dev->open = twl4030_vibra_open;
229	info->input_dev->close = twl4030_vibra_close;
230	__set_bit(FF_RUMBLE, info->input_dev->ffbit);
231
232	ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
233	if (ret < 0) {
234		dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
235		goto err_ialloc;
236	}
237
238	ret = input_register_device(info->input_dev);
239	if (ret < 0) {
240		dev_dbg(&pdev->dev, "couldn't register input device\n");
241		goto err_iff;
242	}
243
244	vibra_disable_leds();
245
246	platform_set_drvdata(pdev, info);
247	return 0;
248
249err_iff:
250	input_ff_destroy(info->input_dev);
251err_ialloc:
252	input_free_device(info->input_dev);
253err_kzalloc:
254	kfree(info);
255	return ret;
256}
257
258static int __devexit twl4030_vibra_remove(struct platform_device *pdev)
259{
260	struct vibra_info *info = platform_get_drvdata(pdev);
261
262	/* this also free ff-memless and calls close if needed */
263	input_unregister_device(info->input_dev);
264	kfree(info);
265	platform_set_drvdata(pdev, NULL);
266
267	return 0;
268}
269
270static struct platform_driver twl4030_vibra_driver = {
271	.probe		= twl4030_vibra_probe,
272	.remove		= __devexit_p(twl4030_vibra_remove),
273	.driver		= {
274		.name	= "twl4030-vibra",
275		.owner	= THIS_MODULE,
276#ifdef CONFIG_PM
277		.pm	= &twl4030_vibra_pm_ops,
278#endif
279	},
280};
281
282static int __init twl4030_vibra_init(void)
283{
284	return platform_driver_register(&twl4030_vibra_driver);
285}
286module_init(twl4030_vibra_init);
287
288static void __exit twl4030_vibra_exit(void)
289{
290	platform_driver_unregister(&twl4030_vibra_driver);
291}
292module_exit(twl4030_vibra_exit);
293
294MODULE_ALIAS("platform:twl4030-vibra");
295
296MODULE_DESCRIPTION("TWL4030 Vibra driver");
297MODULE_LICENSE("GPL");
298MODULE_AUTHOR("Nokia Corporation");