blob: 637d3da1d457c3f96580e50a0a70572464aa25b6 [file] [log] [blame]
Devin Kimba0e9452012-06-21 14:09:34 -07001/*
Devin Kimc68f4162012-09-18 11:48:20 -07002 * File: ImmVibeSPI.c
3 *
4 * Description:
5 * Device-dependent functions called by Immersion TSP API
6 * to control PWM duty cycle, amp enable/disable, save IVT file, etc...
7 *
8 * Portions Copyright (c) 2008-2010 Immersion Corporation. All Rights Reserved.
9 *
10 * This file contains Original Code and/or Modifications of Original Code
11 * as defined in and that are subject to the GNU Public License v2 -
12 * (the 'License'). You may not use this file except in compliance with the
13 * License. You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or contact
16 * TouchSenseSales@immersion.com.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND IMMERSION HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
22 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see
23 * the License for the specific language governing rights and limitations
24 * under the License.
25 */
Devin Kimba0e9452012-06-21 14:09:34 -070026
27#include <linux/io.h>
28#include <linux/err.h>
29#include <linux/gpio.h>
30#include <linux/regulator/msm-gpio-regulator.h>
31
32#include <mach/irqs.h>
33#include <mach/gpiomux.h>
34#include <mach/msm_iomap.h>
35#include <mach/msm_xo.h>
36
Devin Kimba0e9452012-06-21 14:09:34 -070037static bool g_bAmpEnabled = false;
38
39/* gpio and clock control for vibrator */
40
41#define PM8921_GPIO_BASE NR_GPIO_IRQS
42#define PM8921_GPIO_PM_TO_SYS(pm_gpio) (pm_gpio - 1 + PM8921_GPIO_BASE)
43
44#define REG_WRITEL(value, reg) writel(value, (MSM_CLK_CTL_BASE + reg))
45#define REG_READL(reg) readl((MSM_CLK_CTL_BASE + reg))
46
47#define GPn_MD_REG(n) (0x2D00+32*(n))
48#define GPn_NS_REG(n) (0x2D24+32*(n))
49
50/*
51** This SPI supports only one actuator.
52*/
53#define NUM_ACTUATORS 1
54
55#define PWM_DUTY_MAX 579 /* 13MHz / (579 + 1) = 22.4kHz */
56
57#define GPIO_LIN_MOTOR_EN 33
58#define GPIO_LIN_MOTOR_PWR 47
59#define GPIO_LIN_MOTOR_PWM 3
60
61#define GP_CLK_ID 0 /* gp clk 0 */
62#define GP_CLK_M_DEFAULT 1
63#define GP_CLK_N_DEFAULT 166
64#define GP_CLK_D_MAX GP_CLK_N_DEFAULT
65#define GP_CLK_D_HALF (GP_CLK_N_DEFAULT >> 1)
66
Devin Kimba0e9452012-06-21 14:09:34 -070067
68static struct gpiomux_setting vibrator_suspend_cfg = {
69 .func = GPIOMUX_FUNC_GPIO,
70 .drv = GPIOMUX_DRV_2MA,
71 .pull = GPIOMUX_PULL_NONE,
72};
73
74static struct gpiomux_setting vibrator_active_cfg_gpio3 = {
75 .func = GPIOMUX_FUNC_2, /*gp_mn:2 */
76 .drv = GPIOMUX_DRV_2MA,
77 .pull = GPIOMUX_PULL_NONE,
78};
79
80static struct msm_gpiomux_config gpio2_vibrator_configs[] = {
81 {
82 .gpio = 3,
83 .settings = {
84 [GPIOMUX_ACTIVE] = &vibrator_active_cfg_gpio3,
85 [GPIOMUX_SUSPENDED] = &vibrator_suspend_cfg,
86 },
87 },
88};
89
90static struct msm_xo_voter *vib_clock;
91static int vibrator_clock_init(void)
92{
93 int rc;
94 /*Vote for XO clock*/
95 vib_clock = msm_xo_get(MSM_XO_TCXO_D0, "vib_clock");
96
97 if (IS_ERR(vib_clock)) {
98 rc = PTR_ERR(vib_clock);
99 printk(KERN_ERR "%s: Couldn't get TCXO_D0 vote for Vib(%d)\n",
100 __func__, rc);
101 }
102 return rc;
103}
104
105static int vibrator_clock_on(void)
106{
107 int rc;
108 rc = msm_xo_mode_vote(vib_clock, MSM_XO_MODE_ON);
109 if (rc < 0) {
110 printk(KERN_ERR "%s: Failed to vote for TCX0_D0 ON (%d)\n",
111 __func__, rc);
112 }
113 return rc;
114}
115
116static int vibrator_clock_off(void)
117{
118 int rc;
119 rc = msm_xo_mode_vote(vib_clock, MSM_XO_MODE_OFF);
120 if (rc < 0) {
121 printk(KERN_ERR "%s: Failed to vote for TCX0_D0 OFF (%d)\n",
122 __func__, rc);
123 }
124 return rc;
125}
126
127static int vibrator_power_set(int enable)
128{
129 int rc = 0;
130 static struct regulator *vreg_l16 = NULL;
131 int enabled = 0;
132
133 if (unlikely(!vreg_l16)) {
134 vreg_l16 = regulator_get(NULL, "8921_l16"); /* 2.6 ~ 3V */
135
136 if (IS_ERR(vreg_l16)) {
137 pr_err("%s: regulator get of 8921_lvs6 failed (%ld)\n",
138 __func__, PTR_ERR(vreg_l16));
139 rc = PTR_ERR(vreg_l16);
140 return rc;
141 }
142 }
143
144 /* fix the unbalanced disables */
145 enabled = regulator_is_enabled(vreg_l16);
146 if (enabled > 0) {
147 if (enable) { /* already enabled */
148 printk("vibrator already enabled\n");
149 return 0;
150 }
151 } else if (enabled == 0) {
152 if (enable == 0) { /* already disabled */
153 printk("vibrator already disabled\n");
154 return 0;
155 }
156 } else { /* (enabled < 0) */
157 pr_warn("%s: regulator_is_enabled failed\n", __func__);
158 }
159
Devin Kimba0e9452012-06-21 14:09:34 -0700160 rc = regulator_set_voltage(vreg_l16, 2800000, 2800000);
161
162 if(enable) {
163 printk("vibrator_power_set() : vibrator enable\n");
164 rc = regulator_enable(vreg_l16);
165 }
166 else {
167 printk("vibrator_power_set() : vibrator disable\n");
168 rc = regulator_disable(vreg_l16);
169 }
170
171 return rc;
172}
173
174static void vibrator_pwm_set(int enable, int amp, int n_value)
175{
176 uint M_VAL = GP_CLK_M_DEFAULT;
177 uint D_VAL = GP_CLK_D_MAX;
178 uint D_INV = 0; /* QCT support invert bit for msm8960 */
179
180 if (enable) {
181 vibrator_clock_on();
182
183 D_VAL = (((GP_CLK_D_MAX -1) * amp) >> 8) + GP_CLK_D_HALF;
184
185 if (D_VAL > GP_CLK_D_HALF) {
186 if (D_VAL == GP_CLK_D_MAX) /* Max duty is 99% */
187 D_VAL = 2;
188 else
189 D_VAL = GP_CLK_D_MAX - D_VAL;
190
191 D_INV = 1;
192 }
193
194 REG_WRITEL(
195 (((M_VAL & 0xffU) << 16U) + /* M_VAL[23:16] */
196 ((~(D_VAL << 1)) & 0xffU)), /* D_VAL[7:0] */
197 GPn_MD_REG(GP_CLK_ID));
198
199 REG_WRITEL(
200 ((((~(n_value-M_VAL)) & 0xffU) << 16U) + /* N_VAL[23:16] */
201 (1U << 11U) + /* CLK_ROOT_ENA[11] : Enable(1) */
202 ((D_INV & 0x01U) << 10U) +/* CLK_INV[10] : Disable(0) */
203 (1U << 9U) + /* CLK_BRANCH_ENA[9] : Enable(1) */
204 (1U << 8U) + /* NMCNTR_EN[8] : Enable(1) */
205 (0U << 7U) + /* MNCNTR_RST[7] : Not Active(0) */
206 (2U << 5U) + /* MNCNTR_MODE[6:5] : Dual-edge mode(2) */
207 (3U << 3U) + /* PRE_DIV_SEL[4:3] : Div-4 (3) */
208 (5U << 0U)), /* SRC_SEL[2:0] : CXO (5) */
209 GPn_NS_REG(GP_CLK_ID));
210 }
211 else {
212 vibrator_clock_off();
213
214 REG_WRITEL(
215 ((((~(n_value-M_VAL)) & 0xffU) << 16U) + /* N_VAL[23:16] */
216 (0U << 11U) + /* CLK_ROOT_ENA[11] : Disable(0) */
217 (0U << 10U) + /* CLK_INV[10] : Disable(0) */
218 (0U << 9U) + /* CLK_BRANCH_ENA[9] : Disable(0) */
219 (0U << 8U) + /* NMCNTR_EN[8] : Disable(0) */
220 (0U << 7U) + /* MNCNTR_RST[7] : Not Active(0) */
221 (2U << 5U) + /* MNCNTR_MODE[6:5] : Dual-edge mode(2) */
222 (3U << 3U) + /* PRE_DIV_SEL[4:3] : Div-4 (3) */
223 (5U << 0U)), /* SRC_SEL[2:0] : CXO (5) */
224 GPn_NS_REG(GP_CLK_ID));
225 }
226}
227
228static void vibrator_ic_enable_set(int enable)
229{
230 int gpio_lin_motor_en = 0;
231 gpio_lin_motor_en = PM8921_GPIO_PM_TO_SYS(GPIO_LIN_MOTOR_EN);
232
233 if (enable)
234 gpio_direction_output(gpio_lin_motor_en, 1);
235 else
236 gpio_direction_output(gpio_lin_motor_en, 0);
237}
238
239/*
240** Called to disable amp (disable output force)
241*/
Devin Kimc68f4162012-09-18 11:48:20 -0700242static VibeStatus ImmVibeSPI_ForceOut_AmpDisable(VibeUInt8 nActuatorIndex)
Devin Kimba0e9452012-06-21 14:09:34 -0700243{
244
245 if (g_bAmpEnabled) {
246 DbgOut((KERN_DEBUG "ImmVibeSPI_ForceOut_AmpDisable.\n"));
247
248 g_bAmpEnabled = false;
249
250 vibrator_ic_enable_set(0);
251 vibrator_pwm_set(0, 0, GP_CLK_N_DEFAULT);
252 vibrator_power_set(0);
253 }
254
255 return VIBE_S_SUCCESS;
256}
257
258/*
259** Called to enable amp (enable output force)
260*/
Devin Kimc68f4162012-09-18 11:48:20 -0700261static VibeStatus ImmVibeSPI_ForceOut_AmpEnable(VibeUInt8 nActuatorIndex)
Devin Kimba0e9452012-06-21 14:09:34 -0700262{
263
264 if (!g_bAmpEnabled) {
265 DbgOut((KERN_DEBUG "ImmVibeSPI_ForceOut_AmpEnable.\n"));
266
267 g_bAmpEnabled = true;
268
269 vibrator_power_set(1);
270 vibrator_pwm_set(1, 0, GP_CLK_N_DEFAULT);
271 vibrator_ic_enable_set(1);
272 }
273 else {
274 DbgOut((KERN_DEBUG "[ImmVibeSPI] : ImmVibeSPI_ForceOut_AmpEnable [%d]\n", g_bAmpEnabled ));
275 }
276
277 return VIBE_S_SUCCESS;
278}
279
280/*
281** Called at initialization time to set PWM freq, disable amp, etc...
282*/
Devin Kimc68f4162012-09-18 11:48:20 -0700283static VibeStatus ImmVibeSPI_ForceOut_Initialize(void)
Devin Kimba0e9452012-06-21 14:09:34 -0700284{
285 int rc;
286 int gpio_motor_en = 0;
287 int gpio_motor_pwm = 0;
288
289 DbgOut((KERN_DEBUG "ImmVibeSPI_ForceOut_Initialize.\n"));
290
291 /* to force ImmVibeSPI_ForceOut_AmpDisable disabling the amp */
292 g_bAmpEnabled = true;
293
294 /*
295 ** Disable amp.
296 ** If multiple actuators are supported, please make sure to call
297 ** ImmVibeSPI_ForceOut_AmpDisable for each actuator (provide the actuator index as
298 ** input argument).
299 */
300
301 gpio_motor_en = GPIO_LIN_MOTOR_EN;
302 gpio_motor_pwm = GPIO_LIN_MOTOR_PWM;
303
304 /* GPIO function setting */
305 msm_gpiomux_install(gpio2_vibrator_configs, ARRAY_SIZE(gpio2_vibrator_configs));
306
307 /* GPIO setting for Motor EN in pmic8921 */
308 gpio_motor_en = PM8921_GPIO_PM_TO_SYS(GPIO_LIN_MOTOR_EN);
309 rc = gpio_request(gpio_motor_en, "lin_motor_en");
310
311 if (rc) {
312 DbgOut(("GPIO_LIN_MOTOR_EN %d request failed\n", gpio_motor_en));
313 return -1;
314 }
315
316 /* gpio init */
317 rc = gpio_request(gpio_motor_pwm, "lin_motor_pwm");
318
319 if (unlikely(rc < 0))
320 DbgOut(("not able to get gpio\n"));
321
322 vibrator_clock_init();
323
324 ImmVibeSPI_ForceOut_AmpDisable(0);
325
326 return VIBE_S_SUCCESS;
327}
328
329/*
330** Called at termination time to set PWM freq, disable amp, etc...
331*/
Devin Kimc68f4162012-09-18 11:48:20 -0700332static VibeStatus ImmVibeSPI_ForceOut_Terminate(void)
Devin Kimba0e9452012-06-21 14:09:34 -0700333{
334
335 DbgOut((KERN_DEBUG "ImmVibeSPI_ForceOut_Terminate.\n"));
336
337 /*
338 ** Disable amp.
339 ** If multiple actuators are supported, please make sure to call
340 ** ImmVibeSPI_ForceOut_AmpDisable for each actuator (provide the actuator index as
341 ** input argument).
342 */
343 ImmVibeSPI_ForceOut_AmpDisable(0);
344
345 return VIBE_S_SUCCESS;
346}
347
348/*
349** Called by the real-time loop to set PWM duty cycle
350*/
Devin Kimc68f4162012-09-18 11:48:20 -0700351static VibeStatus ImmVibeSPI_ForceOut_SetSamples(VibeUInt8 nActuatorIndex, VibeUInt16 nOutputSignalBitDepth, VibeUInt16 nBufferSizeInBytes, VibeInt8* pForceOutputBuffer)
Devin Kimba0e9452012-06-21 14:09:34 -0700352{
353 VibeInt8 nForce;
354
355 switch (nOutputSignalBitDepth) {
356 case 8:
357 /* pForceOutputBuffer is expected to contain 1 byte */
358 if (nBufferSizeInBytes != 1)
359 return VIBE_E_FAIL;
360
361 nForce = pForceOutputBuffer[0];
362 break;
363 case 16:
364 /* pForceOutputBuffer is expected to contain 2 byte */
365 if (nBufferSizeInBytes != 2)
366 return VIBE_E_FAIL;
367
368 /* Map 16-bit value to 8-bit */
369 nForce = ((VibeInt16*)pForceOutputBuffer)[0] >> 8;
370 break;
371 default:
372 /* Unexpected bit depth */
373 return VIBE_E_FAIL;
374 }
375 /* Check the Force value with Max and Min force value */
376
377 if (nForce > 127)
378 nForce = 127;
379 if (nForce < -127)
380 nForce = -127;
381
382 vibrator_pwm_set(1, nForce, GP_CLK_N_DEFAULT);
383
384 return VIBE_S_SUCCESS;
385}
386
387/*
388** Called to get the device name (device name must be returned as ANSI char)
389*/
Devin Kimc68f4162012-09-18 11:48:20 -0700390static VibeStatus ImmVibeSPI_Device_GetName(VibeUInt8 nActuatorIndex, char *szDevName, int nSize)
Devin Kimba0e9452012-06-21 14:09:34 -0700391{
Devin Kimba0e9452012-06-21 14:09:34 -0700392 return VIBE_S_SUCCESS;
393}