aboutsummaryrefslogtreecommitdiff
path: root/drivers/misc/tspdrv/ImmVibeSPI.c
blob: 637d3da1d457c3f96580e50a0a70572464aa25b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * File: ImmVibeSPI.c
 *
 * Description:
 *     Device-dependent functions called by Immersion TSP API
 *     to control PWM duty cycle, amp enable/disable, save IVT file, etc...
 *
 * Portions Copyright (c) 2008-2010 Immersion Corporation. All Rights Reserved.
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the GNU Public License v2 -
 * (the 'License'). You may not use this file except in compliance with the
 * License. You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or contact
 * TouchSenseSales@immersion.com.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND IMMERSION HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see
 * the License for the specific language governing rights and limitations
 * under the License.
 */

#include <linux/io.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/regulator/msm-gpio-regulator.h>

#include <mach/irqs.h>
#include <mach/gpiomux.h>
#include <mach/msm_iomap.h>
#include <mach/msm_xo.h>

static bool g_bAmpEnabled = false;

/* gpio and clock control for vibrator */

#define PM8921_GPIO_BASE                NR_GPIO_IRQS
#define PM8921_GPIO_PM_TO_SYS(pm_gpio)  (pm_gpio - 1 + PM8921_GPIO_BASE)

#define REG_WRITEL(value, reg)          writel(value, (MSM_CLK_CTL_BASE + reg))
#define REG_READL(reg)                  readl((MSM_CLK_CTL_BASE + reg))

#define GPn_MD_REG(n)                   (0x2D00+32*(n))
#define GPn_NS_REG(n)                   (0x2D24+32*(n))

/*
** This SPI supports only one actuator.
*/
#define NUM_ACTUATORS                   1

#define PWM_DUTY_MAX                    579 /* 13MHz / (579 + 1) = 22.4kHz */

#define GPIO_LIN_MOTOR_EN               33
#define GPIO_LIN_MOTOR_PWR              47
#define GPIO_LIN_MOTOR_PWM              3

#define GP_CLK_ID                       0 /* gp clk 0 */
#define GP_CLK_M_DEFAULT                1
#define GP_CLK_N_DEFAULT                166
#define GP_CLK_D_MAX                    GP_CLK_N_DEFAULT
#define GP_CLK_D_HALF                   (GP_CLK_N_DEFAULT >> 1)


static struct gpiomux_setting vibrator_suspend_cfg = {
	.func = GPIOMUX_FUNC_GPIO,
	.drv = GPIOMUX_DRV_2MA,
	.pull = GPIOMUX_PULL_NONE,
};

static struct gpiomux_setting vibrator_active_cfg_gpio3 = {
	.func = GPIOMUX_FUNC_2, /*gp_mn:2 */
	.drv = GPIOMUX_DRV_2MA,
	.pull = GPIOMUX_PULL_NONE,
};

static struct msm_gpiomux_config gpio2_vibrator_configs[] = {
	{
		.gpio = 3,
		.settings = {
			[GPIOMUX_ACTIVE] = &vibrator_active_cfg_gpio3,
			[GPIOMUX_SUSPENDED] = &vibrator_suspend_cfg,
		},
	},
};

static struct msm_xo_voter *vib_clock;
static int vibrator_clock_init(void)
{
	int rc;
	/*Vote for XO clock*/
	vib_clock = msm_xo_get(MSM_XO_TCXO_D0, "vib_clock");

	if (IS_ERR(vib_clock)) {
		rc = PTR_ERR(vib_clock);
		printk(KERN_ERR "%s: Couldn't get TCXO_D0 vote for Vib(%d)\n",
							__func__, rc);
	}
	return rc;
}

static int vibrator_clock_on(void)
{
	int rc;
	rc = msm_xo_mode_vote(vib_clock, MSM_XO_MODE_ON);
	if (rc < 0) {
		printk(KERN_ERR "%s: Failed to vote for TCX0_D0 ON (%d)\n",
					__func__, rc);
	}
	return rc;
}

static int vibrator_clock_off(void)
{
	int rc;
	rc = msm_xo_mode_vote(vib_clock, MSM_XO_MODE_OFF);
	if (rc < 0) {
		printk(KERN_ERR "%s: Failed to vote for TCX0_D0 OFF (%d)\n",
					__func__, rc);
	}
	return rc;
}

static int vibrator_power_set(int enable)
{
	int rc = 0;
	static struct regulator *vreg_l16 = NULL;
	int enabled = 0;

	if (unlikely(!vreg_l16)) {
		vreg_l16 = regulator_get(NULL, "8921_l16"); /* 2.6 ~ 3V */

		if (IS_ERR(vreg_l16)) {
			pr_err("%s: regulator get of 8921_lvs6 failed (%ld)\n",
						__func__, PTR_ERR(vreg_l16));
			rc = PTR_ERR(vreg_l16);
			return rc;
		}
	}

	/* fix the unbalanced disables */
	enabled = regulator_is_enabled(vreg_l16);
	if (enabled > 0) {
		if (enable) { /* already enabled */
			printk("vibrator already enabled\n");
			return 0;
		}
	} else if (enabled == 0) {
		if (enable == 0) { /* already disabled */
			printk("vibrator already disabled\n");
			return 0;
		}
	} else { /*  (enabled < 0) */
		pr_warn("%s: regulator_is_enabled failed\n", __func__);
	}

	rc = regulator_set_voltage(vreg_l16, 2800000, 2800000);

	if(enable) {
		printk("vibrator_power_set() : vibrator enable\n");
		rc = regulator_enable(vreg_l16);
	}
	else {
		printk("vibrator_power_set() : vibrator disable\n");
		rc = regulator_disable(vreg_l16);
	}

	return rc;
}

static void vibrator_pwm_set(int enable, int amp, int n_value)
{
	uint M_VAL	= GP_CLK_M_DEFAULT;
	uint D_VAL	= GP_CLK_D_MAX;
	uint D_INV	= 0; /* QCT support invert bit for msm8960 */

	if (enable) {
		vibrator_clock_on();

		D_VAL = (((GP_CLK_D_MAX -1) * amp) >> 8) + GP_CLK_D_HALF;

		if (D_VAL > GP_CLK_D_HALF) {
			if (D_VAL == GP_CLK_D_MAX)      /* Max duty is 99% */
				D_VAL = 2;
			else
				D_VAL = GP_CLK_D_MAX - D_VAL;

			D_INV = 1;
		}

		REG_WRITEL(
			(((M_VAL & 0xffU) << 16U) +     /* M_VAL[23:16] */
			((~(D_VAL << 1)) & 0xffU)),     /* D_VAL[7:0] */
			GPn_MD_REG(GP_CLK_ID));

		REG_WRITEL(
			((((~(n_value-M_VAL)) & 0xffU) << 16U) + /* N_VAL[23:16] */
			(1U << 11U) +  	/* CLK_ROOT_ENA[11]      : Enable(1) */
			((D_INV & 0x01U) << 10U) +/* CLK_INV[10] : Disable(0) */
			(1U << 9U) +    /* CLK_BRANCH_ENA[9]     : Enable(1) */
			(1U << 8U) +    /* NMCNTR_EN[8]          : Enable(1) */
			(0U << 7U) +    /* MNCNTR_RST[7]         : Not Active(0) */
			(2U << 5U) +    /* MNCNTR_MODE[6:5]      : Dual-edge mode(2) */
			(3U << 3U) +    /* PRE_DIV_SEL[4:3]      : Div-4 (3) */
			(5U << 0U)),    /* SRC_SEL[2:0]          : CXO (5) */
			GPn_NS_REG(GP_CLK_ID));
	}
	else {
		vibrator_clock_off();

		REG_WRITEL(
			((((~(n_value-M_VAL)) & 0xffU) << 16U) + /* N_VAL[23:16] */
			(0U << 11U) +	/* CLK_ROOT_ENA[11]	: Disable(0) */
			(0U << 10U) +	/* CLK_INV[10]		: Disable(0) */
			(0U << 9U) +	/* CLK_BRANCH_ENA[9]	: Disable(0) */
			(0U << 8U) +	/* NMCNTR_EN[8]		: Disable(0) */
			(0U << 7U) +	/* MNCNTR_RST[7]	: Not Active(0) */
			(2U << 5U) +	/* MNCNTR_MODE[6:5]	: Dual-edge mode(2) */
			(3U << 3U) +	/* PRE_DIV_SEL[4:3]	: Div-4 (3) */
			(5U << 0U)),	/* SRC_SEL[2:0]		: CXO (5) */
			GPn_NS_REG(GP_CLK_ID));
	}
}

static void vibrator_ic_enable_set(int enable)
{
	int gpio_lin_motor_en = 0;
	gpio_lin_motor_en = PM8921_GPIO_PM_TO_SYS(GPIO_LIN_MOTOR_EN);

	if (enable)
		gpio_direction_output(gpio_lin_motor_en, 1);
	else
		gpio_direction_output(gpio_lin_motor_en, 0);
}

/*
** Called to disable amp (disable output force)
*/
static VibeStatus ImmVibeSPI_ForceOut_AmpDisable(VibeUInt8 nActuatorIndex)
{

	if (g_bAmpEnabled) {
		DbgOut((KERN_DEBUG "ImmVibeSPI_ForceOut_AmpDisable.\n"));

		g_bAmpEnabled = false;

		vibrator_ic_enable_set(0);
		vibrator_pwm_set(0, 0, GP_CLK_N_DEFAULT);
		vibrator_power_set(0);
	}

	return VIBE_S_SUCCESS;
}

/*
** Called to enable amp (enable output force)
*/
static VibeStatus ImmVibeSPI_ForceOut_AmpEnable(VibeUInt8 nActuatorIndex)
{

	if (!g_bAmpEnabled) {
		DbgOut((KERN_DEBUG "ImmVibeSPI_ForceOut_AmpEnable.\n"));

		g_bAmpEnabled = true;

		vibrator_power_set(1);
		vibrator_pwm_set(1, 0, GP_CLK_N_DEFAULT);
		vibrator_ic_enable_set(1);
	}
	else {
		DbgOut((KERN_DEBUG "[ImmVibeSPI] : ImmVibeSPI_ForceOut_AmpEnable [%d]\n", g_bAmpEnabled ));
	}

	return VIBE_S_SUCCESS;
}

/*
** Called at initialization time to set PWM freq, disable amp, etc...
*/
static VibeStatus ImmVibeSPI_ForceOut_Initialize(void)
{
	int rc;
	int gpio_motor_en = 0;
	int gpio_motor_pwm = 0;

	DbgOut((KERN_DEBUG "ImmVibeSPI_ForceOut_Initialize.\n"));

	/* to force ImmVibeSPI_ForceOut_AmpDisable disabling the amp */
	g_bAmpEnabled = true;

	/*
	** Disable amp.
	** If multiple actuators are supported, please make sure to call
	** ImmVibeSPI_ForceOut_AmpDisable for each actuator (provide the actuator index as
	** input argument).
	*/

	gpio_motor_en = GPIO_LIN_MOTOR_EN;
	gpio_motor_pwm = GPIO_LIN_MOTOR_PWM;

	/* GPIO function setting */
	msm_gpiomux_install(gpio2_vibrator_configs, ARRAY_SIZE(gpio2_vibrator_configs));

	/* GPIO setting for Motor EN in pmic8921 */
	gpio_motor_en = PM8921_GPIO_PM_TO_SYS(GPIO_LIN_MOTOR_EN);
	rc = gpio_request(gpio_motor_en, "lin_motor_en");

	if (rc) {
		DbgOut(("GPIO_LIN_MOTOR_EN %d request failed\n", gpio_motor_en));
		return -1;
	}

	/* gpio init */
	rc = gpio_request(gpio_motor_pwm, "lin_motor_pwm");

	if (unlikely(rc < 0))
		DbgOut(("not able to get gpio\n"));

	vibrator_clock_init();

	ImmVibeSPI_ForceOut_AmpDisable(0);

	return VIBE_S_SUCCESS;
}

/*
** Called at termination time to set PWM freq, disable amp, etc...
*/
static VibeStatus ImmVibeSPI_ForceOut_Terminate(void)
{

	DbgOut((KERN_DEBUG "ImmVibeSPI_ForceOut_Terminate.\n"));

	/*
	** Disable amp.
	** If multiple actuators are supported, please make sure to call
	** ImmVibeSPI_ForceOut_AmpDisable for each actuator (provide the actuator index as
	** input argument).
	*/
	ImmVibeSPI_ForceOut_AmpDisable(0);

	return VIBE_S_SUCCESS;
}

/*
** Called by the real-time loop to set PWM duty cycle
*/
static VibeStatus ImmVibeSPI_ForceOut_SetSamples(VibeUInt8 nActuatorIndex, VibeUInt16 nOutputSignalBitDepth, VibeUInt16 nBufferSizeInBytes, VibeInt8* pForceOutputBuffer)
{
	VibeInt8 nForce;

	switch (nOutputSignalBitDepth) {
	case 8:
		/* pForceOutputBuffer is expected to contain 1 byte */
		if (nBufferSizeInBytes != 1)
			return VIBE_E_FAIL;

		nForce = pForceOutputBuffer[0];
		break;
	case 16:
		/* pForceOutputBuffer is expected to contain 2 byte */
		if (nBufferSizeInBytes != 2)
			return VIBE_E_FAIL;

		/* Map 16-bit value to 8-bit */
		nForce = ((VibeInt16*)pForceOutputBuffer)[0] >> 8;
		break;
	default:
		/* Unexpected bit depth */
		return VIBE_E_FAIL;
	}
	/* Check the Force value with Max and Min force value */

	if (nForce > 127)
		nForce = 127;
	if (nForce < -127)
		nForce = -127;

	vibrator_pwm_set(1, nForce, GP_CLK_N_DEFAULT);

	return VIBE_S_SUCCESS;
}

/*
** Called to get the device name (device name must be returned as ANSI char)
*/
static VibeStatus ImmVibeSPI_Device_GetName(VibeUInt8 nActuatorIndex, char *szDevName, int nSize)
{
	return VIBE_S_SUCCESS;
}