blob: 15929d8624592b5e17a3d94d2f37704670bc226a [file] [log] [blame]
Jari Vanhala3dd1b392010-03-09 00:29:46 -08001/*
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>
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +030029#include <linux/of.h>
Jari Vanhala3dd1b392010-03-09 00:29:46 -080030#include <linux/workqueue.h>
31#include <linux/i2c/twl.h>
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030032#include <linux/mfd/twl4030-audio.h>
Jari Vanhala3dd1b392010-03-09 00:29:46 -080033#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Jari Vanhala3dd1b392010-03-09 00:29:46 -080035
36/* MODULE ID2 */
37#define LEDEN 0x00
38
39/* ForceFeedback */
40#define EFFECT_DIR_180_DEG 0x8000 /* range is 0 - 0xFFFF */
41
42struct vibra_info {
43 struct device *dev;
44 struct input_dev *input_dev;
45
Jari Vanhala3dd1b392010-03-09 00:29:46 -080046 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
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030070 twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER);
Jari Vanhala3dd1b392010-03-09 00:29:46 -080071
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
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030078 twl4030_audio_enable_resource(TWL4030_AUDIO_RES_APLL);
Jari Vanhala3dd1b392010-03-09 00:29:46 -080079
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
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030093 twl4030_audio_disable_resource(TWL4030_AUDIO_RES_APLL);
94 twl4030_audio_disable_resource(TWL4030_AUDIO_RES_POWER);
Jari Vanhala3dd1b392010-03-09 00:29:46 -080095
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;
Peter Ujfalusi4a31ba32013-01-11 09:05:47 -0800145 schedule_work(&info->play_work);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800146 return 0;
147}
148
149static void twl4030_vibra_close(struct input_dev *input)
150{
151 struct vibra_info *info = input_get_drvdata(input);
152
153 cancel_work_sync(&info->play_work);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800154
155 if (info->enabled)
156 vibra_disable(info);
157}
158
159/*** Module ***/
Jingoo Han97a652a2014-11-02 00:02:46 -0700160static int __maybe_unused twl4030_vibra_suspend(struct device *dev)
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800161{
162 struct platform_device *pdev = to_platform_device(dev);
163 struct vibra_info *info = platform_get_drvdata(pdev);
164
165 if (info->enabled)
166 vibra_disable(info);
167
168 return 0;
169}
170
Jingoo Han97a652a2014-11-02 00:02:46 -0700171static int __maybe_unused twl4030_vibra_resume(struct device *dev)
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800172{
173 vibra_disable_leds();
174 return 0;
175}
176
177static SIMPLE_DEV_PM_OPS(twl4030_vibra_pm_ops,
178 twl4030_vibra_suspend, twl4030_vibra_resume);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800179
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300180static bool twl4030_vibra_check_coexist(struct twl4030_vibra_data *pdata,
Johan Hovoldcb513d12018-01-08 17:15:06 -0800181 struct device_node *parent)
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300182{
Johan Hovoldcb513d12018-01-08 17:15:06 -0800183 struct device_node *node;
184
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300185 if (pdata && pdata->coexist)
186 return true;
187
Johan Hovoldcb513d12018-01-08 17:15:06 -0800188 node = of_get_child_by_name(parent, "codec");
Marek Beliskoe661d0a02015-07-29 14:02:19 -0700189 if (node) {
Libo Chena9e1d3c2014-01-03 23:58:32 -0800190 of_node_put(node);
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300191 return true;
Libo Chena9e1d3c2014-01-03 23:58:32 -0800192 }
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300193
194 return false;
195}
196
Bill Pemberton5298cc42012-11-23 21:38:25 -0800197static int twl4030_vibra_probe(struct platform_device *pdev)
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800198{
Jingoo Hanc838cb32013-12-05 19:21:10 -0800199 struct twl4030_vibra_data *pdata = dev_get_platdata(&pdev->dev);
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300200 struct device_node *twl4030_core_node = pdev->dev.parent->of_node;
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800201 struct vibra_info *info;
202 int ret;
203
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300204 if (!pdata && !twl4030_core_node) {
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800205 dev_dbg(&pdev->dev, "platform_data not available\n");
206 return -EINVAL;
207 }
208
Peter Ujfalusic3ead162013-01-11 09:05:42 -0800209 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800210 if (!info)
211 return -ENOMEM;
212
213 info->dev = &pdev->dev;
Peter Ujfalusi64b9e4d2012-09-10 13:46:26 +0300214 info->coexist = twl4030_vibra_check_coexist(pdata, twl4030_core_node);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800215 INIT_WORK(&info->play_work, vibra_play_work);
216
Peter Ujfalusic3ead162013-01-11 09:05:42 -0800217 info->input_dev = devm_input_allocate_device(&pdev->dev);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800218 if (info->input_dev == NULL) {
219 dev_err(&pdev->dev, "couldn't allocate input device\n");
Peter Ujfalusic3ead162013-01-11 09:05:42 -0800220 return -ENOMEM;
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800221 }
222
223 input_set_drvdata(info->input_dev, info);
224
225 info->input_dev->name = "twl4030:vibrator";
226 info->input_dev->id.version = 1;
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800227 info->input_dev->close = twl4030_vibra_close;
228 __set_bit(FF_RUMBLE, info->input_dev->ffbit);
229
230 ret = input_ff_create_memless(info->input_dev, NULL, vibra_play);
231 if (ret < 0) {
232 dev_dbg(&pdev->dev, "couldn't register vibrator to FF\n");
Peter Ujfalusic3ead162013-01-11 09:05:42 -0800233 return ret;
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800234 }
235
236 ret = input_register_device(info->input_dev);
237 if (ret < 0) {
238 dev_dbg(&pdev->dev, "couldn't register input device\n");
239 goto err_iff;
240 }
241
242 vibra_disable_leds();
243
244 platform_set_drvdata(pdev, info);
245 return 0;
246
247err_iff:
248 input_ff_destroy(info->input_dev);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800249 return ret;
250}
251
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800252static struct platform_driver twl4030_vibra_driver = {
253 .probe = twl4030_vibra_probe,
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800254 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000255 .name = "twl4030-vibra",
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800256 .pm = &twl4030_vibra_pm_ops,
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800257 },
258};
JJ Ding840a7462011-11-29 11:08:40 -0800259module_platform_driver(twl4030_vibra_driver);
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800260
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000261MODULE_ALIAS("platform:twl4030-vibra");
Jari Vanhala3dd1b392010-03-09 00:29:46 -0800262MODULE_DESCRIPTION("TWL4030 Vibra driver");
263MODULE_LICENSE("GPL");
264MODULE_AUTHOR("Nokia Corporation");