1 /*
2 ** =============================================================================
3 ** Copyright (c) 2016 Texas Instruments Inc.
4 **
5 ** This program is free software; you can redistribute it and/or
6 ** modify it under the terms of the GNU General Public License
7 ** as published by the Free Software Foundation; either version 2
8 ** of the License, or (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **
19 ** File:
20 ** drv2624.c
21 **
22 ** Description:
23 ** DRV2624 chip driver
24 **
25 ** =============================================================================
26 */
27 #define DEBUG
29 #include <linux/init.h>
30 #include <linux/input.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/types.h>
35 #include <linux/fs.h>
36 #include <linux/i2c.h>
37 #include <linux/semaphore.h>
38 #include <linux/device.h>
39 #include <linux/syscalls.h>
40 #include <asm/uaccess.h>
41 #include <linux/gpio.h>
42 #include <linux/sched.h>
43 #include <linux/spinlock_types.h>
44 #include <linux/spinlock.h>
45 #include <linux/delay.h>
46 #include <linux/of.h>
47 #include <linux/of_gpio.h>
48 #include <linux/jiffies.h>
49 #include <linux/err.h>
50 #include <linux/clk.h>
51 #include <linux/miscdevice.h>
52 #include <linux/interrupt.h>
53 #include "drv2624.h"
55 static struct drv2624_data *g_DRV2624data;
56 static struct regmap_config drv2624_i2c_regmap = {
57 .reg_bits = 8,
58 .val_bits = 8,
59 .cache_type = REGCACHE_NONE,
60 };
62 static int dev_run_diagnostics(struct drv2624_data *pDRV2624);
63 static inline int drv2624_calculate_voltage(unsigned int voltage);
65 /**
66 * RW Functions for DRV2624 registers through I2C
67 * drv2624_reg_read, drv2624_reg_write,
68 * drv2624_bulk_read, drv2624_bulk_write
69 **/
71 static int drv2624_reg_read(struct drv2624_data *pDRV2624,
72 unsigned char reg)
73 {
74 unsigned int val;
75 int nResult;
77 mutex_lock(&pDRV2624->dev_lock);
78 nResult = regmap_read(pDRV2624->mpRegmap, reg, &val);
79 mutex_unlock(&pDRV2624->dev_lock);
81 if (nResult < 0) {
82 dev_err(pDRV2624->dev, "%s I2C error %d\n", __func__, nResult);
83 //return nResult;
84 } else {
85 dev_dbg(pDRV2624->dev,
86 "%s, Reg[0x%x]=0x%x\n", __func__, reg, val);
87 }
88 return val;
89 }
91 static int drv2624_reg_write(struct drv2624_data *pDRV2624,
92 unsigned char reg, unsigned char val)
93 {
94 int nResult;
96 dev_dbg(pDRV2624->dev, "%s, Reg[0x%x]=0x%x\n", __func__, reg, val);
97 mutex_lock(&pDRV2624->dev_lock);
98 nResult = regmap_write(pDRV2624->mpRegmap, reg, val);
99 mutex_unlock(&pDRV2624->dev_lock);
100 if (nResult < 0)
101 dev_err(pDRV2624->dev,
102 "%s reg=0x%x, value=0%x error %d\n",
103 __func__, reg, val, nResult);
105 dev_dbg(pDRV2624->dev, "%s, Reg[0x%x]=0x%x\n", __func__, reg, val);
107 return nResult;
108 }
110 static int drv2624_bulk_read(struct drv2624_data *pDRV2624,
111 unsigned char reg, unsigned char *buf, unsigned int count)
112 {
113 int nResult;
115 mutex_lock(&pDRV2624->dev_lock);
116 nResult = regmap_bulk_read(pDRV2624->mpRegmap, reg, buf, count);
117 mutex_unlock(&pDRV2624->dev_lock);
118 if (nResult < 0)
119 dev_err(pDRV2624->dev,
120 "%s reg=0%x, count=%d error %d\n",
121 __func__, reg, count, nResult);
123 return nResult;
124 }
126 static int drv2624_bulk_write(struct drv2624_data *pDRV2624,
127 unsigned char reg, const u8 *buf, unsigned int count)
128 {
129 int nResult, i;
131 mutex_lock(&pDRV2624->dev_lock);
132 nResult = regmap_bulk_write(pDRV2624->mpRegmap, reg, buf, count);
133 mutex_unlock(&pDRV2624->dev_lock);
134 if (nResult < 0)
135 dev_err(pDRV2624->dev,
136 "%s reg=0%x, count=%d error %d\n", __func__,
137 reg, count, nResult);
138 for (i = 0; i < count; i++)
139 dev_dbg(pDRV2624->dev,
140 "%s, Reg[0x%x]=0x%x\n", __func__, reg+i, buf[i]);
142 return nResult;
143 }
145 static int drv2624_set_bits(struct drv2624_data *pDRV2624,
146 unsigned char reg, unsigned char mask, unsigned char val)
147 {
148 int nResult;
150 mutex_lock(&pDRV2624->dev_lock);
151 nResult = regmap_update_bits(pDRV2624->mpRegmap, reg, mask, val);
152 mutex_unlock(&pDRV2624->dev_lock);
153 if (nResult < 0)
154 dev_err(pDRV2624->dev,
155 "%s reg=%x, mask=0x%x, value=0x%x error %d\n",
156 __func__, reg, mask, val, nResult);
157 dev_dbg(pDRV2624->dev,
158 "%s, Reg[0x%x]:M=0x%x, V=0x%x\n", __func__, reg, mask, val);
160 return nResult;
161 }
163 /**
164 *
165 * bRTP = NO == 0; Enable all interrupt of DRV2624
166 * bRTP = 1 == 1; Only Enable critical interrupt, PROCESS_DONE and PRG_ERROR
167 *
168 **/
169 static int drv2624_enableIRQ(struct drv2624_data *pDRV2624, unsigned char bRTP)
170 {
171 int nResult = 0;
172 unsigned char mask = INT_ENABLE_CRITICAL;
174 if (!pDRV2624->mbIRQUsed)
175 goto end;
177 if (pDRV2624->mbIRQEnabled)
178 goto end;
180 if (bRTP == 0)
181 mask = INT_ENABLE_ALL;
183 nResult = drv2624_reg_read(pDRV2624, DRV2624_REG_STATUS);
184 if (nResult < 0)
185 goto end;
186 nResult = drv2624_reg_write(pDRV2624, DRV2624_REG_INT_ENABLE, mask);
187 if (nResult < 0)
188 goto end;
190 enable_irq(pDRV2624->mnIRQ);
191 pDRV2624->mbIRQEnabled = true;
193 end:
194 return nResult;
195 }
197 static void drv2624_disableIRQ(struct drv2624_data *pDRV2624)
198 {
199 if (pDRV2624->mbIRQUsed) {
200 if (pDRV2624->mbIRQEnabled) {
201 disable_irq_nosync(pDRV2624->mnIRQ);
202 drv2624_reg_write(pDRV2624,
203 DRV2624_REG_INT_ENABLE, INT_MASK_ALL);
204 pDRV2624->mbIRQEnabled = false;
205 }
206 }
207 }
209 static int drv2624_set_go_bit(struct drv2624_data *pDRV2624, unsigned char val)
210 {
211 int nResult = 0, value = 0;
212 int retry = GO_BIT_MAX_RETRY_CNT;
214 val &= DRV2624_GO_BIT_MASK;
215 dev_dbg(pDRV2624->dev, "%s, go val = %d\n", __func__, val);
216 nResult = drv2624_reg_write(pDRV2624, DRV2624_REG_GO, val);
217 if (nResult < 0)
218 goto end;
220 mdelay(GO_BIT_CHECK_INTERVAL);
221 value = drv2624_reg_read(pDRV2624, DRV2624_REG_GO);
222 if (value < 0) {
223 nResult = value;
224 goto end;
225 }
227 dev_dbg(pDRV2624->dev, "%s, go value = %d\n", __func__, value);
228 while (((value & DRV2624_GO_BIT_MASK) != val) && (retry > 0)) {
229 value = drv2624_reg_read(pDRV2624, DRV2624_REG_GO);
231 dev_dbg(pDRV2624->dev, "%s, GO bit %d\n", __func__, value);
232 mdelay(GO_BIT_CHECK_INTERVAL);
233 retry--;
234 }
236 dev_dbg(pDRV2624->dev, "%s: pull go bit success!\n", __func__);
238 end:
239 return nResult;
240 }
242 static inline int drv2624_change_mode(struct drv2624_data *pDRV2624,
243 drv2624_mode_t work_mode)
244 {
245 pDRV2624->mnWorkMode = work_mode;
247 return drv2624_set_bits(pDRV2624,
248 DRV2624_REG_MODE, WORKMODE_MASK, work_mode);
249 }
251 static inline void drv2624_set_stopflag(struct drv2624_data *pDRV2624)
252 {
253 pDRV2624->mnVibratorPlaying = NO;
254 pDRV2624->mnEffectType = 0;
255 }
257 static int
258 drv2624_get_diag_result(struct drv2624_data *pDRV2624, unsigned char nStatus)
259 {
260 int Re = 0, nResult = 0;
262 pDRV2624->mDiagResult.mnResult = nStatus;
263 if ((nStatus & DIAG_MASK) != DIAG_SUCCESS)
264 dev_err(pDRV2624->dev, "%s: Diagnostic fail\n", __func__);
265 else {
266 nResult = drv2624_reg_read(pDRV2624, DRV2624_REG_DIAG_Z);
267 if (nResult < 0)
268 goto end;
269 pDRV2624->mDiagResult.mnDiagZ = nResult;
271 nResult = drv2624_reg_read(pDRV2624, DRV2624_REG_DIAG_K);
272 if (nResult < 0)
273 goto end;
274 pDRV2624->mDiagResult.mnDiagK = nResult;
276 Re = 478.43 * (pDRV2624->mDiagResult.mnDiagZ /
277 (4 * pDRV2624->mDiagResult.mnDiagK + 719));
279 dev_dbg(pDRV2624->dev,
280 "%s: ZResult=0x%x, CurrentK=0x%x, Re = %d ohm\n",
281 __func__, pDRV2624->mDiagResult.mnDiagZ,
282 pDRV2624->mDiagResult.mnDiagK, Re);
283 }
285 end:
286 return nResult;
287 }
289 /**
290 * No need to stop in Waveform Sequencer Mode.
291 * 1. Disable irq
292 * 2. Cancel hrimer
293 * 3. Set GO bit as STOP
294 * 4. Set stop flag in drv2624_data struct
295 *
296 **/
297 static int drv2624_stop(struct drv2624_data *pDRV2624)
298 {
299 int nResult = 0, mode = 0;
301 dev_dbg(pDRV2624->dev, "%s enter!\n", __func__);
302 nResult = drv2624_reg_read(pDRV2624, DRV2624_REG_MODE);
303 if (nResult < 0)
304 return nResult;
306 mode = nResult & WORKMODE_MASK;
307 if (mode == MODE_WAVEFORM_SEQUENCER) {
308 dev_dbg(pDRV2624->dev, "In sequence play, ignore stop\n");
309 return 0;
310 }
312 if (pDRV2624->mnVibratorPlaying == YES) {
313 if (pDRV2624->mbIRQUsed)
314 drv2624_disableIRQ(pDRV2624);
315 if (hrtimer_active(&pDRV2624->haptics_timer))
316 hrtimer_cancel(&pDRV2624->haptics_timer);
317 nResult = drv2624_set_go_bit(pDRV2624, STOP);
318 drv2624_set_stopflag(pDRV2624);
319 }
321 return nResult;
322 }
324 #ifdef ANDROID_TIMED_OUTPUT
325 static int vibrator_get_time(struct timed_output_dev *dev)
326 {
327 struct drv2624_data *pDRV2624 =
328 container_of(dev, struct drv2624_data, to_dev);
330 if (hrtimer_active(&pDRV2624->haptics_timer)) {
331 ktime_t r;
333 r = hrtimer_get_remaining(&pDRV2624->haptics_timer);
334 return ktime_to_ms(r);
335 }
337 return 0;
338 }
340 static void vibrator_enable(struct timed_output_dev *dev, int value)
341 {
342 int nResult = 0;
343 struct drv2624_data *pDRV2624 =
344 container_of(dev, struct drv2624_data, to_dev);
346 dev_dbg(pDRV2624->dev, "%s, value=%d\n", __func__, value);
348 mutex_lock(&pDRV2624->lock);
350 dev_dbg(pDRV2624->dev, "%s, afer mnWorkMode=0x%x\n",
351 __func__, pDRV2624->mnWorkMode);
353 drv2624_stop(pDRV2624);
355 if (value > 0) {
356 nResult = drv2624_change_mode(pDRV2624, DRV2624_RTP_MODE);
357 if (nResult < 0)
358 goto end;
360 nResult = drv2624_set_go_bit(pDRV2624, GO);
361 if (nResult >= 0) {
362 value = (value > MAX_TIMEOUT) ? MAX_TIMEOUT : value;
363 hrtimer_start(&pDRV2624->haptics_timer,
364 ns_to_ktime((u64)value * NSEC_PER_MSEC),
365 HRTIMER_MODE_REL);
366 pDRV2624->mnVibratorPlaying = YES;
368 if (pDRV2624->mbIRQUsed)
369 nResult = drv2624_enableIRQ(pDRV2624, YES);
370 }
371 }
373 end:
374 mutex_unlock(&pDRV2624->lock);
375 }
376 #endif /* ANDROID_TIMED_OUTPUT */
378 static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
379 {
380 struct drv2624_data *pDRV2624 =
381 container_of(timer, struct drv2624_data, haptics_timer);
383 dev_dbg(pDRV2624->dev, "%s\n", __func__);
384 schedule_work(&pDRV2624->vibrator_work);
386 return HRTIMER_NORESTART;
387 }
389 /**
390 * 1. Do work due to pDRV2624->mnWorkMode set before.
391 * 2. For WORK_EFFECTSEQUENCER, WORK_CALIBRATION and WORK_DIAGNOSTIC
392 * check the GO bit until the process in DRV2624 has completed.
393 * 3. For WORK_VIBRATOR, Stop DRV2624 directly.
394 **/
395 static void vibrator_work_routine(struct work_struct *work)
396 {
397 struct drv2624_data *pDRV2624 =
398 container_of(work, struct drv2624_data, vibrator_work);
399 unsigned char status;
400 int nResult = 0;
402 mutex_lock(&pDRV2624->lock);
404 dev_dbg(pDRV2624->dev, "%s, afer mnWorkMode=0x%x\n",
405 __func__, pDRV2624->mnWorkMode);
407 if (pDRV2624->mbIRQUsed) {
408 pDRV2624->mnIntStatus =
409 drv2624_reg_read(pDRV2624, DRV2624_REG_STATUS);
410 if (nResult < 0)
411 goto err;
413 drv2624_disableIRQ(pDRV2624);
415 status = pDRV2624->mnIntStatus;
416 dev_dbg(pDRV2624->dev, "%s, status=0x%x\n",
417 __func__, pDRV2624->mnIntStatus);
419 if (status & OVERCURRENT_MASK)
420 dev_err(pDRV2624->dev,
421 "ERROR, Over Current detected!!\n");
423 if (status & OVERTEMPRATURE_MASK)
424 dev_err(pDRV2624->dev,
425 "ERROR, Over Temperature detected!!\n");
427 if (status & ULVO_MASK)
428 dev_err(pDRV2624->dev, "ERROR, VDD drop observed!!\n");
430 if (status & PRG_ERR_MASK)
431 dev_err(pDRV2624->dev, "ERROR, PRG error!!\n");
432 }
434 if (pDRV2624->mnWorkMode == DRV2624_RTP_MODE) {
435 drv2624_stop(pDRV2624);
436 } else if (pDRV2624->mnWorkMode == DRV2624_RAM_MODE) {
437 status = drv2624_reg_read(pDRV2624, DRV2624_REG_GO);
438 if ((status < 0) || (status == STOP)
439 || !pDRV2624->mnVibratorPlaying) {
440 drv2624_stop(pDRV2624);
441 } else {
442 if (!hrtimer_active(&pDRV2624->haptics_timer)) {
443 dev_dbg(pDRV2624->dev,
444 "will check GO bit after %dms\n",
445 GO_BIT_CHECK_INTERVAL);
446 hrtimer_start(&pDRV2624->haptics_timer,
447 ns_to_ktime((u64)GO_BIT_CHECK_INTERVAL
448 * NSEC_PER_MSEC),
449 HRTIMER_MODE_REL);
450 }
451 }
452 }
454 err:
455 mutex_unlock(&pDRV2624->lock);
456 }
458 static int dev_auto_calibrate(struct drv2624_data *pDRV2624)
459 {
460 int nResult = 0;
462 dev_info(pDRV2624->dev, "%s enter!\n", __func__);
464 /**
465 * Set MODE register to Auto Level Calibration Routine
466 * and choose Trigger Function Internal
467 **/
468 nResult = drv2624_reg_write(pDRV2624,
469 DRV2624_REG_MODE, DRV2624_CALIBRATION_MODE_CFG);
470 if (nResult < 0)
471 goto end;
473 nResult = drv2624_change_mode(pDRV2624, MODE_CALIBRATION);
475 if (nResult < 0) {
476 dev_err(pDRV2624->dev,
477 "%s: change mode not Done nResult= %d\n",
478 __func__, nResult);
479 goto end;
480 }
482 nResult = drv2624_set_bits(pDRV2624,
483 AUTO_CAL_TIME_REG, AUTO_CAL_TIME_MASK,
484 AUTO_CAL_TIME_AUTO_TRIGGER);
485 if (nResult < 0) {
486 dev_err(pDRV2624->dev,
487 "%s: set bits not Done nResult = %d\n",
488 __func__, nResult);
489 goto end;
490 }
492 nResult = drv2624_set_go_bit(pDRV2624, GO);
493 if (nResult < 0) {
494 dev_err(pDRV2624->dev,
495 "%s: calibrate go bit not Done nResult = %d\n",
496 __func__, nResult);
497 goto end;
498 }
499 msleep(1000); /* waiting auto calibration finished */
500 pDRV2624->mnVibratorPlaying = YES;
502 return nResult;
503 end:
504 dev_err(pDRV2624->dev,
505 "%s: Calibtion Done nResult = %d\n", __func__, nResult);
506 return nResult;
507 }
509 static int drv2624_get_calibration_result(struct drv2624_data *pDRV2624)
510 {
511 int nResult, cal_bemf, cal_comp, cal_gain;
513 dev_dbg(pDRV2624->dev, "%s: enter!\n", __func__);
515 nResult = drv2624_reg_read(pDRV2624, DRV2624_REG_STATUS);
516 if (nResult < 0)
517 goto end;
518 //pDRV2624->mnIntStatus = nResult;
519 pDRV2624->mAutoCalResult.mnResult = nResult;
520 cal_comp = drv2624_reg_read(pDRV2624, DRV2624_REG_CAL_COMP);
521 if (cal_comp < 0)
522 goto end;
523 pDRV2624->mAutoCalResult.mnCalComp = cal_comp;
525 cal_bemf = drv2624_reg_read(pDRV2624, DRV2624_REG_CAL_BEMF);
526 if (cal_bemf < 0)
527 goto end;
528 pDRV2624->mAutoCalResult.mnCalBemf = cal_bemf;
530 cal_gain = drv2624_reg_read(pDRV2624,
531 DRV2624_REG_LOOP_CONTROL) & BEMFGAIN_MASK;
532 if (cal_gain < 0)
533 goto end;
534 pDRV2624->mAutoCalResult.mnCalGain = cal_gain;
536 end:
537 dev_dbg(pDRV2624->dev, "%s: nResult = %d\n", __func__, nResult);
538 return nResult;
539 }
541 /*show calibrtion*/
542 static ssize_t
543 drv2624_show(struct device *dev, struct device_attribute *attr, char *buf)
544 {
546 int nResult = 0;
547 struct drv2624_data *pDRV2624 = dev_get_drvdata(dev);
549 nResult = drv2624_get_calibration_result(pDRV2624);
550 if (nResult < 0)
551 goto end;
553 dev_dbg(pDRV2624->dev,
554 "%s: Cal_Result: 0x%x, CalComp: 0x%x, CalBemf: 0x%x, CalGain: 0x%x\n",
555 __func__, pDRV2624->mAutoCalResult.mnResult,
556 pDRV2624->mAutoCalResult.mnCalComp,
557 pDRV2624->mAutoCalResult.mnCalBemf,
558 pDRV2624->mAutoCalResult.mnCalGain);
559 return snprintf(buf, 100,
560 "Cal_Result: 0x%x, CalComp: 0x%x, CalBemf: 0x%x, CalGain: 0x%x\n",
561 pDRV2624->mAutoCalResult.mnResult,
562 pDRV2624->mAutoCalResult.mnCalComp,
563 pDRV2624->mAutoCalResult.mnCalBemf,
564 pDRV2624->mAutoCalResult.mnCalGain);
565 end:
566 return nResult;
567 }
569 /* store calibration*/
570 static ssize_t drv2624_store(
571 struct device *dev, struct device_attribute *attr,
572 const char *buf, size_t size)
573 {
574 int nResult = 0;
575 struct drv2624_data *tdev = dev_get_drvdata(dev);
577 if ((buf[0] == 'C') && (buf[1] == 'A') &&
578 (buf[2] == 'L') && (buf[3] == 'I') && (buf[4] == 'B')) {
579 nResult = dev_auto_calibrate(tdev);
580 } else if ((buf[0] == 'D') && (buf[1] == 'I')
581 && (buf[2] == 'A') && (buf[3] == 'G')) {
582 nResult = dev_run_diagnostics(tdev);
583 }
585 return size;
586 }
588 static DEVICE_ATTR(cali, 0664, drv2624_show, drv2624_store);
590 static int dev_run_diagnostics(struct drv2624_data *pDRV2624)
591 {
592 int nResult = 0, value = 0;
594 dev_info(pDRV2624->dev, "%s\n", __func__);
596 nResult = drv2624_change_mode(pDRV2624, DRV2624_DIAG_MODE);
597 if (nResult < 0)
598 goto end;
599 nResult = drv2624_set_go_bit(pDRV2624, GO);
600 if (nResult < 0)
601 goto end;
603 dev_dbg(pDRV2624->dev, "%s: Diag start\n", __func__);
604 pDRV2624->mnVibratorPlaying = YES;
606 value = drv2624_reg_read(pDRV2624, DRV2624_REG_STATUS);
607 if (value < 0)
608 return value;
610 dev_dbg(pDRV2624->dev, "%s:DRV2624_REG_STATUS=0x%x\n", __func__, value);
612 drv2624_get_diag_result(pDRV2624, value);
613 end:
614 return nResult;
615 }
617 /**
618 * Play Waveform sequence stored in DRV2624_REG_SEQUENCER_1
619 *
620 **/
621 static int drv2624_playEffect(struct drv2624_data *pDRV2624)
622 {
623 int nResult = 0;
625 dev_info(pDRV2624->dev, "%s\n", __func__);
627 nResult = drv2624_change_mode(pDRV2624, DRV2624_WAVE_SEQ_MODE);
628 if (nResult < 0)
629 goto end;
631 nResult = drv2624_set_go_bit(pDRV2624, GO);
632 if (nResult < 0)
633 goto end;
635 dev_dbg(pDRV2624->dev, "effects start\n");
636 pDRV2624->mnVibratorPlaying = YES;
638 if (!pDRV2624->mbIRQUsed)
639 schedule_work(&pDRV2624->vibrator_work);
641 end:
642 return nResult;
643 }
645 static int drv2624_set_waveform(struct drv2624_data *pDRV2624,
646 struct drv2624_waveform_sequencer *pSequencer)
647 {
648 int nResult = 0;
649 int i = 0;
650 unsigned char loop[2] = {0};
651 unsigned char effects[DRV2624_SEQUENCER_SIZE] = {0};
652 unsigned char len = 0;
654 for (i = 0; i < DRV2624_SEQUENCER_SIZE; i++) {
655 len++;
656 if (pSequencer->msWaveform[i].mnEffect != 0) {
657 if (i < 4)
658 loop[0] |=
659 (pSequencer->msWaveform[i].mnLoop << (2*i));
660 else
661 loop[1] |=
662 (pSequencer->msWaveform[i].mnLoop << (2*(i-4)));
664 effects[i] = pSequencer->msWaveform[i].mnEffect;
665 } else
666 break;
667 }
669 if (len == 1)
670 nResult = drv2624_reg_write(pDRV2624,
671 DRV2624_REG_SEQUENCER_1, 0);
672 else
673 nResult = drv2624_bulk_write(pDRV2624,
674 DRV2624_REG_SEQUENCER_1, effects, len);
676 if (nResult < 0) {
677 dev_err(pDRV2624->dev, "sequence error\n");
678 goto end;
679 }
681 if (len > 1) {
682 if ((len-1) <= 4)
683 drv2624_reg_write(pDRV2624,
684 DRV2624_REG_SEQ_LOOP_1, loop[0]);
685 else
686 drv2624_bulk_write(pDRV2624,
687 DRV2624_REG_SEQ_LOOP_1, loop, 2);
688 }
690 end:
691 return nResult;
692 }
694 static int fw_chksum(const struct firmware *fw)
695 {
696 int sum = 0;
697 int i = 0;
698 int size = fw->size;
699 const unsigned char *pBuf = fw->data;
701 for (i = 0; i < size; i++) {
702 if ((i <= 11) || (i >= 16))
703 sum += pBuf[i];
704 }
706 return sum;
707 }
709 static int
710 drv2624_get_effect_timems(struct drv2624_data *pDRV2624, unsigned char effect)
711 {
712 unsigned char *fw = &pDRV2624->mnFwRam[0];
713 u16 header_address, tmp;
714 u16 address = 0;
715 unsigned char effect_repeats = 0;
716 unsigned int effect_size = 0;
717 int i = 0;
718 unsigned int ticks = 0;
719 unsigned int playback_interval = 0;
721 effect++;
722 header_address = (effect - 1) * 3 + 1;
723 tmp = fw[header_address];
724 address = tmp << 8 | fw[header_address+1];
725 effect_repeats = (fw[header_address+2] & 0xe0) >> 5;
726 effect_size = fw[header_address+2] & 0x1f;
728 for (i = 0; i < effect_size/2; i++)
729 ticks += fw[address + (i * 2) + 1];
731 playback_interval =
732 (pDRV2624->msWaveformSetting.mnInterval == INTERVAL_5MS) ? 5 : 1;
734 return ticks * (effect_repeats + 1) * playback_interval;
735 }
737 /**
738 * drv2624_firmware_load:
739 * This function is called by the
740 * request_firmware_nowait function as soon
741 * as the firmware has been loaded from the file.
742 * The firmware structure contains the data and$
743 * the size of the firmware loaded.
744 *
745 * @fw: pointer to firmware file to be dowloaded
746 * @context: pointer variable to drv2624 data
747 **/
748 static void drv2624_firmware_load(const struct firmware *fw, void *context)
749 {
750 struct drv2624_data *pDRV2624 = context;
751 int size = 0, fwsize = 0, i = 0;
752 const unsigned char *pBuf = NULL;
754 mutex_lock(&pDRV2624->lock);
756 if (fw != NULL) {
757 pBuf = fw->data;
758 size = fw->size;
759 if (size > 1024) {
760 dev_err(pDRV2624->dev,
761 "%s, ERROR!! firmware size %d too big\n",
762 __func__, size);
763 } else {
764 memcpy(&(pDRV2624->fw_header), pBuf,
765 sizeof(struct drv2624_fw_header));
766 if ((pDRV2624->fw_header.fw_magic != DRV2624_MAGIC)
767 || (pDRV2624->fw_header.fw_size != size)
768 || (pDRV2624->fw_header.fw_chksum !=
769 fw_chksum(fw))) {
770 dev_err(pDRV2624->dev,
771 "%s, ERROR!! firmware not right: Magic=0x%x, Size=%d, chksum=0x%x\n",
772 __func__, pDRV2624->fw_header.fw_magic,
773 pDRV2624->fw_header.fw_size,
774 pDRV2624->fw_header.fw_chksum);
775 } else {
776 dev_info(pDRV2624->dev, "%s, firmware good\n",
777 __func__);
779 //pDRV2624->play.effect_count = pDRV2624->fw_header.fw_effCount;
781 pBuf += sizeof(struct drv2624_fw_header);
782 drv2624_reg_write(pDRV2624,
783 DRV2624_REG_RAM_ADDR_UPPER, 0);
784 drv2624_reg_write(pDRV2624,
785 DRV2624_REG_RAM_ADDR_LOWER, 0);
787 fwsize =
788 size - sizeof(struct drv2624_fw_header);
789 for (i = 0; i < fwsize; i++)
790 drv2624_reg_write(pDRV2624,
791 DRV2624_REG_RAM_DATA, pBuf[i]);
792 memset(&pDRV2624->mnFwRam[0],
793 0, DRV2624_RAM_SIZE);
794 memcpy(&pDRV2624->mnFwRam[0], pBuf, fwsize);
795 for (i = 0; i < pDRV2624->fw_header.fw_effCount; i++)
796 pDRV2624->mnEffectTimems[i] =
797 drv2624_get_effect_timems(pDRV2624, i);
798 }
799 }
800 } else
801 dev_err(pDRV2624->dev,
802 "%s, ERROR!! firmware not found\n", __func__);
804 release_firmware(fw);
806 mutex_unlock(&pDRV2624->lock);
807 }
809 static int drv2624_file_open(struct inode *inode, struct file *file)
810 {
811 if (!try_module_get(THIS_MODULE))
812 return -ENODEV;
814 file->private_data = (void *)g_DRV2624data;
815 return 0;
816 }
818 static int drv2624_file_release(struct inode *inode, struct file *file)
819 {
820 file->private_data = (void *)NULL;
821 module_put(THIS_MODULE);
822 return 0;
823 }
825 static long drv2624_file_unlocked_ioctl(struct file *file,
826 unsigned int cmd, unsigned long arg)
827 {
828 struct drv2624_data *pDRV2624 = file->private_data;
829 //void __user *user_arg = (void __user *)arg;
830 int nResult = 0;
832 mutex_lock(&pDRV2624->lock);
834 dev_dbg(pDRV2624->dev, "ioctl 0x%x\n", cmd);
836 switch (cmd) {
838 }
840 mutex_unlock(&pDRV2624->lock);
842 return nResult;
843 }
845 static ssize_t drv2624_file_read(struct file *filp,
846 char *buff, size_t length, loff_t *offset)
847 {
848 struct drv2624_data *pDRV2624 =
849 (struct drv2624_data *)filp->private_data;
850 int nResult = 0;
851 unsigned char value = 0;
852 unsigned char *p_kBuf = NULL;
854 mutex_lock(&pDRV2624->lock);
856 switch (pDRV2624->mnFileCmd) {
857 case HAPTIC_CMDID_REG_READ:
858 if (length == 1) {
859 nResult = drv2624_reg_read(pDRV2624,
860 pDRV2624->mnCurrentReg);
861 if (nResult >= 0) {
862 value = nResult;
863 nResult = copy_to_user(buff, &value, 1);
864 if (nResult != 0) {
865 /* Failed to copy all the data, exit */
866 dev_err(pDRV2624->dev,
867 "copy to user fail %d\n", nResult);
868 }
869 }
870 } else if (length > 1) {
871 p_kBuf = (unsigned char *)kzalloc(length, GFP_KERNEL);
872 if (p_kBuf != NULL) {
873 nResult = drv2624_bulk_read(pDRV2624,
874 pDRV2624->mnCurrentReg, p_kBuf, length);
875 if (nResult >= 0) {
876 nResult =
877 copy_to_user(buff, p_kBuf, length);
878 if (nResult != 0) {
879 /* Failed to copy all the data, exit */
880 dev_err(pDRV2624->dev,
881 "copy to user fail %d\n",
882 nResult);
883 }
884 }
886 kfree(p_kBuf);
887 } else {
888 dev_err(pDRV2624->dev, "read no mem\n");
889 nResult = -ENOMEM;
890 }
891 }
892 break;
894 case HAPTIC_CMDID_RUN_DIAG:
895 if (pDRV2624->mnVibratorPlaying)
896 length = 0;
897 else {
898 unsigned char buf[3];
900 buf[0] = pDRV2624->mDiagResult.mnResult;
901 buf[1] = pDRV2624->mDiagResult.mnDiagZ;
902 buf[2] = pDRV2624->mDiagResult.mnDiagK;
903 nResult = copy_to_user(buff, buf, 3);
904 if (nResult != 0) {
905 /* Failed to copy all the data, exit */
906 dev_err(pDRV2624->dev,
907 "copy to user fail %d\n", nResult);
908 }
909 }
910 break;
912 case HAPTIC_CMDID_RUN_CALIBRATION:
913 if (!pDRV2624->mnVibratorPlaying)
914 length = 0;
915 else {
916 unsigned char buf[4];
918 buf[0] = pDRV2624->mAutoCalResult.mnResult;
919 buf[1] = pDRV2624->mAutoCalResult.mnCalComp;
920 buf[2] = pDRV2624->mAutoCalResult.mnCalBemf;
921 buf[3] = pDRV2624->mAutoCalResult.mnCalGain;
922 nResult = copy_to_user(buff, buf, 4);
923 if (nResult != 0) {
924 /* Failed to copy all the data, exit */
925 dev_err(pDRV2624->dev,
926 "copy to user fail %d\n", nResult);
927 }
928 }
929 break;
930 default:
931 pDRV2624->mnFileCmd = 0;
932 break;
933 }
934 mutex_unlock(&pDRV2624->lock);
936 return length;
937 }
938 static ssize_t
939 drv2624_file_write(struct file *filp, const char *buff, size_t len, loff_t *off)
940 {
941 struct drv2624_data *pDRV2624 =
942 (struct drv2624_data *)filp->private_data;
943 unsigned char *p_kBuf = NULL;
944 int nResult = 0;
946 mutex_lock(&pDRV2624->lock);
948 p_kBuf = (unsigned char *)kzalloc(len, GFP_KERNEL);
949 if (p_kBuf == NULL) {
950 //dev_err(pDRV2624->dev, "write no mem\n");
951 goto err;
952 }
954 nResult = copy_from_user(p_kBuf, buff, len);
955 if (nResult != 0) {
956 dev_err(pDRV2624->dev, "copy_from_user failed.\n");
957 goto err;
958 }
960 pDRV2624->mnFileCmd = p_kBuf[0];
962 switch (pDRV2624->mnFileCmd) {
963 case HAPTIC_CMDID_REG_READ:
964 if (len == 2)
965 pDRV2624->mnCurrentReg = p_kBuf[1];
966 else
967 dev_err(pDRV2624->dev,
968 " read cmd len %lu err\n", (unsigned long)len);
969 break;
971 case HAPTIC_CMDID_REG_WRITE:
972 if ((len - 1) == 2)
973 nResult = drv2624_reg_write(pDRV2624,
974 p_kBuf[1], p_kBuf[2]);
975 else if ((len - 1) > 2)
976 nResult = drv2624_bulk_write(pDRV2624,
977 p_kBuf[1], &p_kBuf[2], len-2);
978 else
979 dev_err(pDRV2624->dev, "%s, reg_write len %lu error\n",
980 __func__, (unsigned long)len);
981 break;
983 case HAPTIC_CMDID_RUN_DIAG:
984 nResult = drv2624_stop(pDRV2624);
985 if (nResult < 0)
986 break;
987 nResult = dev_run_diagnostics(pDRV2624);
988 if ((nResult >= 0) && pDRV2624->mbIRQUsed)
989 drv2624_enableIRQ(pDRV2624, NO);
990 break;
992 case HAPTIC_CMDID_RUN_CALIBRATION:
993 nResult = drv2624_stop(pDRV2624);
994 if (nResult < 0)
995 break;
997 nResult = dev_auto_calibrate(pDRV2624);
998 nResult = drv2624_get_calibration_result(pDRV2624);
999 if ((nResult >= 0) && pDRV2624->mbIRQUsed)
1000 drv2624_enableIRQ(pDRV2624, NO);
1001 break;
1003 case HAPTIC_SET_CALIBRATION_RESULT:
1004 drv2624_reg_write(pDRV2624, DRV2624_REG_CAL_COMP, p_kBuf[1]);
1005 drv2624_reg_write(pDRV2624, DRV2624_REG_CAL_BEMF, p_kBuf[2]);
1006 drv2624_reg_write(pDRV2624, DRV2624_REG_LOOP_CONTROL, p_kBuf[3]);
1007 break;
1009 default:
1010 dev_err(pDRV2624->dev, "%s, unknown cmd\n", __func__);
1011 break;
1012 }
1014 err:
1015 if (p_kBuf != NULL)
1016 kfree(p_kBuf);
1018 mutex_unlock(&pDRV2624->lock);
1020 return len;
1021 }
1022 static void upload_periodic_work_routine(struct work_struct *work)
1023 {
1024 struct drv2624_data *pDRV2624 =
1025 container_of(work, struct drv2624_data, upload_periodic_work);
1026 int nResult = 0;
1028 mutex_lock(&pDRV2624->lock);
1029 nResult = drv2624_stop(pDRV2624);
1030 if (nResult < 0)
1031 return;
1033 nResult =
1034 drv2624_set_waveform(pDRV2624, &pDRV2624->msWaveformSequencer);
1035 if (nResult < 0)
1036 dev_err(pDRV2624->dev,
1037 "Configurate predefined effect %d failed, nResult=%d\n",
1038 pDRV2624->msWaveformSequencer.msWaveform[0].mnEffect, nResult);
1039 dev_dbg(pDRV2624->dev,
1040 "Configurate predefined effect success, effect=%d\n",
1041 pDRV2624->msWaveformSequencer.msWaveform[0].mnEffect);
1043 mutex_unlock(&pDRV2624->lock);
1044 }
1046 static int drv2624_haptics_upload_effect(struct input_dev *dev,
1047 struct ff_effect *effect, struct ff_effect *old)
1048 {
1049 struct drv2624_data *pDRV2624 = input_get_drvdata(dev);
1050 struct drv2624_constant_playinfo *play = &pDRV2624->play;
1051 u16 data[CUSTOM_DATA_LEN];
1052 int nResult = 0;
1053 ktime_t rem;
1054 s64 time_us;
1055 uint time_ms = 0;
1057 pr_info("%s enter function\n", __func__);
1058 mutex_lock(&pDRV2624->lock);
1060 /* waiting last vibration to end */
1061 if (hrtimer_active(&pDRV2624->haptics_timer)) {
1062 rem = hrtimer_get_remaining(&pDRV2624->haptics_timer);
1063 time_us = ktime_to_us(rem);
1064 dev_info(pDRV2624->dev,
1065 "%s: waiting for playing finished: %lld us\n",
1066 __func__, time_us);
1067 usleep_range(time_us, time_us + 100);
1069 }
1071 pDRV2624->mnEffectType = effect->type;
1072 dev_info(pDRV2624->dev, "%s: mnEffectType: %d\n",
1073 __func__, pDRV2624->mnEffectType);
1075 switch (pDRV2624->mnEffectType) {
1076 case FF_CONSTANT:
1077 play->length = effect->replay.length;
1078 play->rtp_input = effect->u.constant.level;
1079 pDRV2624->mnWorkMode = DRV2624_RTP_MODE;
1080 dev_info(pDRV2624->dev, "%s: length(%d), level(%d)\n",
1081 __func__, play->length, play->rtp_input);
1082 break;
1083 case FF_PERIODIC:
1084 #ifdef NEED_RELOAD_FIRMWARE
1085 if (pDRV2624->fw_header.fw_magic == 0) {
1086 nResult = request_firmware_nowait(THIS_MODULE,
1087 FW_ACTION_HOTPLUG,
1088 "drv2624.bin",
1089 pDRV2624->dev,
1090 GFP_KERNEL,
1091 pDRV2624,
1092 drv2624_firmware_load);
1093 dev_info(pDRV2624->dev,
1094 "%s:request_firmware_nowait nResult=%d\n",
1095 __func__, nResult);
1096 }
1097 #endif
1099 if (effect->u.periodic.waveform != FF_CUSTOM) {
1100 dev_err(pDRV2624->dev, "Only accept custom waveform\n");
1101 nResult = -EINVAL;
1102 break;
1103 }
1105 if (copy_from_user(data, effect->u.periodic.custom_data,
1106 sizeof(u16) * CUSTOM_DATA_LEN)) {
1107 nResult = -EFAULT;
1108 break;
1109 }
1111 play->effect_id = data[CUSTOM_DATA_EFFECT_IDX];
1112 play->magnitude = effect->u.periodic.magnitude;
1113 pDRV2624->mnWorkMode = DRV2624_RAM_MODE;
1114 dev_dbg(pDRV2624->dev, "%s: effect_id = %d, magnitude = %d\n",
1115 __func__, play->effect_id, play->magnitude);
1117 if ((play->effect_id < 0) ||
1118 (play->effect_id > pDRV2624->fw_header.fw_effCount)) {
1119 dev_err(pDRV2624->dev,
1120 "%s: overflow effect_id=%d, max_effect_id=%d\n",
1121 __func__, play->effect_id,
1122 pDRV2624->fw_header.fw_effCount);
1123 nResult = -EINVAL;
1124 break;
1125 }
1127 pr_info("%s next upload to use effect\n", __func__);
1128 memset(&pDRV2624->msWaveformSequencer, 0,
1129 sizeof(struct drv2624_waveform_sequencer));
1130 pDRV2624->msWaveformSequencer.msWaveform[0].mnEffect =
1131 data[CUSTOM_DATA_EFFECT_IDX];
1132 pDRV2624->msWaveformSequencer.msWaveform[0].mnLoop = 0;
1133 pDRV2624->msWaveformSequencer.msWaveform[1].mnEffect = 0;
1134 pDRV2624->msWaveformSequencer.msWaveform[1].mnLoop = 0;
1136 dev_dbg(pDRV2624->dev, "upload to use effect %d\n",
1137 data[CUSTOM_DATA_EFFECT_IDX]);
1139 time_ms =
1140 pDRV2624->mnEffectTimems[data[CUSTOM_DATA_EFFECT_IDX] - 1];
1141 dev_dbg(pDRV2624->dev, "effect playing time_ms %d\n", time_ms);
1143 data[CUSTOM_DATA_TIMEOUT_SEC_IDX] =
1144 time_ms / MSEC_PER_SEC;
1145 data[CUSTOM_DATA_TIMEOUT_MSEC_IDX] =
1146 time_ms % MSEC_PER_SEC;
1148 /*
1149 * Copy the custom data contains the play length back to
1150 * userspace so that the userspace client can wait and
1151 * send stop playing command after it's done.
1152 */
1153 if (copy_to_user(effect->u.periodic.custom_data, data,
1154 sizeof(u16) * CUSTOM_DATA_LEN)) {
1155 pr_info("%s copy to user failed\n", __func__);
1156 nResult = -EFAULT;
1157 break;
1158 }
1160 schedule_work(&pDRV2624->upload_periodic_work);
1161 break;
1162 default:
1163 dev_err(pDRV2624->dev, "Unsupported effect type: %d\n",
1164 effect->type);
1165 break;
1166 }
1168 mutex_unlock(&pDRV2624->lock);
1169 dev_info(pDRV2624->dev, "%s exit\n", __func__);
1171 return nResult;
1172 }
1174 static void haptics_playback_work_routine(struct work_struct *work)
1175 {
1176 int nResult = 0;
1177 struct drv2624_data *pDRV2624 =
1178 container_of(work, struct drv2624_data, haptics_playback_work);
1180 mutex_lock(&pDRV2624->lock);
1182 dev_dbg(pDRV2624->dev, "%s enter effect.lenth(%d)\n",
1183 __func__, pDRV2624->play.length);
1185 nResult = drv2624_stop(pDRV2624);
1186 if (nResult < 0) {
1187 dev_err(pDRV2624->dev, "%s: stop faild!\n", __func__);
1188 goto end;
1189 }
1191 switch (pDRV2624->mnEffectType) {
1192 case FF_PERIODIC:
1193 nResult = drv2624_stop(pDRV2624);
1194 if (nResult < 0)
1195 break;
1196 nResult = drv2624_playEffect(pDRV2624);
1197 if ((nResult >= 0) && pDRV2624->mbIRQUsed)
1198 drv2624_enableIRQ(pDRV2624, NO);
1199 break;
1201 case FF_CONSTANT:
1202 drv2624_stop(pDRV2624);
1203 nResult =
1204 drv2624_change_mode(pDRV2624, DRV2624_RTP_MODE);
1205 if (nResult < 0) {
1206 dev_dbg(pDRV2624->dev,
1207 "%s: change_mode nResult = %d\n",
1208 __func__, nResult);
1209 break;
1210 }
1212 nResult = drv2624_reg_write(pDRV2624,
1213 DRV2624_REG_RTP_INPUT, 0x7f);
1214 if (nResult < 0)
1215 break;
1217 nResult = drv2624_set_go_bit(pDRV2624, GO);
1218 if (nResult < 0)
1219 break;
1221 pDRV2624->mnVibratorPlaying = YES;
1223 if (pDRV2624->mbIRQUsed)
1224 nResult = drv2624_enableIRQ(pDRV2624, YES);
1225 break;
1227 default:
1228 dev_info(pDRV2624->dev, "Unsupported effect type: %d\n",
1229 pDRV2624->mnEffectType);
1230 break;
1231 }
1233 if (pDRV2624->play.length != 0) {
1234 hrtimer_start(&pDRV2624->haptics_timer,
1235 ns_to_ktime(pDRV2624->play.length * NSEC_PER_MSEC),
1236 HRTIMER_MODE_REL);
1237 }
1239 end:
1240 mutex_unlock(&pDRV2624->lock);
1241 }
1243 static int drv2624_haptics_playback(struct input_dev *dev,
1244 int effect_id, int val)
1245 {
1246 int nResult = 0;
1247 struct drv2624_data *pDRV2624 = input_get_drvdata(dev);
1249 dev_dbg(pDRV2624->dev, "%s: mnEffectType(%d) WorkMode(%d)\n",
1250 __func__, pDRV2624->mnEffectType, pDRV2624->mnWorkMode);
1252 if ((pDRV2624->mnEffectType == FF_CONSTANT) &&
1253 (pDRV2624->mnWorkMode == DRV2624_RTP_MODE)) {
1254 if (hrtimer_active(&pDRV2624->haptics_timer))
1255 hrtimer_cancel(&pDRV2624->haptics_timer);
1257 schedule_work(&pDRV2624->haptics_playback_work);
1258 } else if ((pDRV2624->mnEffectType == FF_PERIODIC) &&
1259 (pDRV2624->mnWorkMode == DRV2624_RAM_MODE)) {
1260 schedule_work(&pDRV2624->haptics_playback_work);
1261 } else {
1262 dev_err(pDRV2624->dev, "%s: effect_type(%d) not supported!\n",
1263 __func__, pDRV2624->mnEffectType);
1264 }
1266 return nResult;
1267 }
1269 static int drv2624_haptics_erase(struct input_dev *dev, int effect_id)
1270 {
1271 struct drv2624_data *pDRV2624 = input_get_drvdata(dev);
1272 int nResult = 0;
1274 mutex_lock(&pDRV2624->lock);
1275 nResult = drv2624_stop(pDRV2624);
1276 if (nResult < 0)
1277 dev_err(pDRV2624->dev, "failed to stop vibrator:%d\n", nResult);
1278 mutex_unlock(&pDRV2624->lock);
1280 return nResult;
1281 }
1282 static void haptics_set_gain_work_routine(struct work_struct *work)
1283 {
1284 int nResult;
1285 struct drv2624_data *pDRV2624 =
1286 container_of(work, struct drv2624_data, haptics_set_gain_work);
1287 struct drv2624_constant_playinfo *play = &pDRV2624->play;
1289 mutex_lock(&pDRV2624->lock);
1290 nResult = drv2624_reg_write(pDRV2624,
1291 DRV2624_REG_RTP_INPUT, play->rtp_input);
1292 if (nResult < 0)
1293 dev_err(pDRV2624->dev,
1294 "Configurate RTP_INPUT failed, nResult=%d\n", nResult);
1295 mutex_unlock(&pDRV2624->lock);
1296 }
1298 /* NOTE: gain range: 0 ~ 0x7f*/
1299 static void drv2624_haptics_set_gain(struct input_dev *dev, u16 gain)
1300 {
1301 struct drv2624_data *pDRV2624 = input_get_drvdata(dev);
1302 struct drv2624_constant_playinfo *play = &pDRV2624->play;
1304 if (gain == 0)
1305 return;
1306 else if (gain > 0x7f)
1307 gain = 0x7f;
1308 dev_dbg(pDRV2624->dev, "%s, gain=%x\n", __func__, gain);
1309 play->rtp_input =
1310 (gain - LIGHT_MAGNITUDE) *
1311 255 / (STRONG_MAGNITUDE - LIGHT_MAGNITUDE);
1312 //play->rtp_input = gain;
1313 dev_dbg(pDRV2624->dev, "upload constant effect, rtp_input=%d\n",
1314 play->rtp_input);
1315 dev_dbg(pDRV2624->dev, "enter drv264_reg_write rtp_input\n");
1316 schedule_work(&pDRV2624->haptics_set_gain_work);
1317 }
1320 static const struct file_operations fops = {
1321 .owner = THIS_MODULE,
1322 .read = drv2624_file_read,
1323 .write = drv2624_file_write,
1324 .unlocked_ioctl = drv2624_file_unlocked_ioctl,
1325 .open = drv2624_file_open,
1326 .release = drv2624_file_release,
1327 };
1329 static struct miscdevice drv2624_misc = {
1330 .minor = MISC_DYNAMIC_MINOR,
1331 .name = HAPTICS_DEVICE_NAME,
1332 .fops = &fops,
1333 };
1335 static int Haptics_init(struct drv2624_data *pDRV2624)
1336 {
1337 int nResult = 0;
1339 #ifdef ANDROID_TIMED_OUTPUT
1340 pDRV2624->to_dev.name = "vibrator";
1341 pDRV2624->to_dev.get_time = vibrator_get_time;
1342 pDRV2624->to_dev.enable = vibrator_enable;
1344 nResult = timed_output_dev_register(&(pDRV2624->to_dev));
1345 if (nResult < 0) {
1346 dev_err(pDRV2624->dev,
1347 "drv2624: fail to create timed output dev\n");
1348 return nResult;
1349 }
1350 #endif /* ANDROID_TIMED_OUTPUT */
1352 nResult = misc_register(&drv2624_misc);
1353 if (nResult) {
1354 dev_err(pDRV2624->dev, "drv2624 misc fail: %d\n", nResult);
1355 return nResult;
1356 }
1358 nResult = device_create_file(pDRV2624->dev, &dev_attr_cali);
1359 if (nResult < 0) {
1360 dev_info(pDRV2624->dev,
1361 "sys file creation failed nResult:%d\n", nResult);
1362 return nResult;
1363 }
1365 hrtimer_init(&pDRV2624->haptics_timer,
1366 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1367 pDRV2624->haptics_timer.function = vibrator_timer_func;
1369 INIT_WORK(&pDRV2624->vibrator_work, vibrator_work_routine);
1370 INIT_WORK(&pDRV2624->upload_periodic_work,
1371 upload_periodic_work_routine);
1372 INIT_WORK(&pDRV2624->haptics_playback_work,
1373 haptics_playback_work_routine);
1374 INIT_WORK(&pDRV2624->haptics_set_gain_work,
1375 haptics_set_gain_work_routine);
1376 mutex_init(&pDRV2624->lock);
1378 return 0;
1379 }
1381 static void drv2624_init(struct drv2624_data *pDRV2624)
1382 {
1383 struct drv2624_platform_data *pDrv2624Platdata = &pDRV2624->msPlatData;
1384 struct actuator_data actuator = pDrv2624Platdata->msActuator;
1385 unsigned char value_temp = 0;
1386 unsigned char mask_temp = 0;
1387 struct drv2624_wave_setting wavesetting;
1388 unsigned char value = 0;
1390 drv2624_set_bits(pDRV2624,
1391 DRV2624_REG_MODE, PINFUNC_MASK, (PINFUNC_INT<<PINFUNC_SHIFT));
1393 if ((actuator.mnActuatorType == ERM)
1394 || (actuator.mnActuatorType == LRA)) {
1395 mask_temp |= ACTUATOR_MASK;
1396 value_temp |= (actuator.mnActuatorType << ACTUATOR_SHIFT);
1397 }
1399 if ((pDrv2624Platdata->mnLoop == CLOSE_LOOP)
1400 || (pDrv2624Platdata->mnLoop == OPEN_LOOP)) {
1401 mask_temp |= LOOP_MASK;
1402 value_temp |= (pDrv2624Platdata->mnLoop << LOOP_SHIFT);
1403 }
1405 drv2624_set_bits(pDRV2624, DRV2624_REG_CONTROL1,
1406 mask_temp|AUTOBRK_OK_MASK, value_temp|AUTOBRK_OK_ENABLE);
1408 value_temp = 0;
1409 if (actuator.mnActuatorType == ERM)
1410 value_temp = LIB_ERM;
1411 else if (actuator.mnActuatorType == LRA)
1412 value_temp = LIB_LRA;
1413 if (value_temp != 0)
1414 drv2624_set_bits(pDRV2624,
1415 DRV2624_REG_CONTROL2, LIB_MASK, value_temp<<LIB_SHIFT);
1417 if (actuator.mnRatedVoltage != 0)
1418 drv2624_reg_write(pDRV2624,
1419 DRV2624_REG_RATED_VOLTAGE, actuator.mnRatedVoltage);
1420 else
1421 dev_err(pDRV2624->dev, "%s, ERROR Rated ZERO\n", __func__);
1422 if (actuator.mnOverDriveClampVoltage != 0)
1423 drv2624_reg_write(pDRV2624,
1424 DRV2624_REG_OVERDRIVE_CLAMP,
1425 actuator.mnOverDriveClampVoltage);
1426 else
1427 dev_err(pDRV2624->dev,
1428 "%s, ERROR OverDriveVol ZERO\n", __func__);
1430 if (actuator.mnActuatorType == LRA) {
1431 unsigned char DriveTime =
1432 5*(1000 - actuator.mnLRAFreq)/actuator.mnLRAFreq;
1433 unsigned short openLoopPeriod =
1434 (unsigned short)(1000000000 /
1435 (24619 * actuator.mnLRAFreq));
1437 if (actuator.mnLRAFreq < 125)
1438 DriveTime |= (MINFREQ_SEL_45HZ << MINFREQ_SEL_SHIFT);
1439 drv2624_set_bits(pDRV2624, DRV2624_REG_DRIVE_TIME,
1440 DRIVE_TIME_MASK | MINFREQ_SEL_MASK, DriveTime);
1441 drv2624_set_bits(pDRV2624,
1442 DRV2624_REG_OL_PERIOD_H, 0x03,
1443 (openLoopPeriod&0x0300)>>8);
1444 drv2624_reg_write(pDRV2624, DRV2624_REG_OL_PERIOD_L,
1445 (openLoopPeriod&0x00ff));
1447 dev_info(pDRV2624->dev, "%s, LRA = %d, DriveTime=0x%x\n",
1448 __func__, actuator.mnLRAFreq, DriveTime);
1449 }
1451 value = drv2624_reg_read(pDRV2624, DRV2624_REG_CONTROL2);
1452 wavesetting.mnLoop = drv2624_reg_read(pDRV2624,
1453 DRV2624_REG_MAIN_LOOP)&0x07;
1454 wavesetting.mnInterval = ((value&INTERVAL_MASK)>>INTERVAL_SHIFT);
1455 wavesetting.mnScale = (value&SCALE_MASK);
1456 memcpy(&pDRV2624->msWaveformSetting,
1457 &wavesetting,
1458 sizeof(struct drv2624_wave_setting));
1459 }
1461 static irqreturn_t drv2624_irq_handler(int irq, void *dev_id)
1462 {
1463 struct drv2624_data *pDRV2624 = (struct drv2624_data *)dev_id;
1465 pDRV2624->mnWorkMode |= WORK_IRQ;
1466 schedule_work(&pDRV2624->vibrator_work);
1468 return IRQ_HANDLED;
1469 }
1471 static int drv2624_parse_dt(struct device *dev, struct drv2624_data *pDRV2624)
1472 {
1473 struct device_node *np = dev->of_node;
1474 struct drv2624_platform_data *pPlatData = &pDRV2624->msPlatData;
1475 int rc = 0, nResult = 0;
1476 unsigned int value;
1478 pPlatData->mnGpioNRST = of_get_named_gpio(np, "ti,reset-gpio", 0);
1479 if (pPlatData->mnGpioNRST >= 0) {
1480 dev_dbg(pDRV2624->dev,
1481 "ti,reset-gpio=%d\n", pPlatData->mnGpioNRST);
1482 } else {
1483 dev_err(pDRV2624->dev,
1484 "Looking up %s property in node %s failed %d\n",
1485 "ti,reset-gpio", np->full_name, pPlatData->mnGpioNRST);
1486 return -EINVAL;
1487 }
1489 rc = of_property_read_u32(np, "ti,smart-loop", &value);
1490 if (rc == 0) {
1491 pPlatData->mnLoop = value & 0x01;
1492 dev_dbg(pDRV2624->dev, "ti,smart-loop=%d\n", pPlatData->mnLoop);
1493 } else {
1494 dev_err(pDRV2624->dev,
1495 "Looking up %s property in node %s failed %d\n",
1496 "ti,smart-loop", np->full_name, rc);
1497 return -EINVAL;
1498 }
1500 rc = of_property_read_u32(np, "ti,actuator", &value);
1501 if (rc == 0) {
1502 pPlatData->msActuator.mnActuatorType = value & 0x01;
1503 dev_dbg(pDRV2624->dev, "ti,actuator=%d\n",
1504 pPlatData->msActuator.mnActuatorType);
1505 } else {
1506 dev_err(pDRV2624->dev,
1507 "Looking up %s property in node %s failed %d\n",
1508 "ti,actuator", np->full_name, rc);
1509 return -EINVAL;
1510 }
1512 rc = of_property_read_u32(np, "ti,rated-voltage", &value);
1513 if (rc == 0) {
1514 pPlatData->msActuator.mnRatedVoltage =
1515 drv2624_calculate_voltage(value);
1516 dev_dbg(pDRV2624->dev, "ti,rated-voltage=0x%x\n",
1517 pPlatData->msActuator.mnRatedVoltage);
1518 } else {
1519 dev_err(pDRV2624->dev,
1520 "Looking up %s property in node %s failed %d\n",
1521 "ti,rated-voltage", np->full_name, rc);
1522 return -EINVAL;
1523 }
1525 rc = of_property_read_u32(np, "ti,odclamp-voltage", &value);
1526 if (rc == 0) {
1527 pPlatData->msActuator.mnOverDriveClampVoltage =
1528 drv2624_calculate_voltage(value);
1529 dev_dbg(pDRV2624->dev, "ti,odclamp-voltage=0x%x\n",
1530 pPlatData->msActuator.mnOverDriveClampVoltage);
1531 } else {
1532 dev_err(pDRV2624->dev,
1533 "Looking up %s property in node %s failed %d\n",
1534 "ti,odclamp-voltage", np->full_name, rc);
1535 return -EINVAL;
1537 }
1539 if (pPlatData->msActuator.mnActuatorType == LRA) {
1540 rc = of_property_read_u32(np, "ti,lra-frequency", &value);
1541 if (rc == 0) {
1542 if ((value >= 45) && (value <= 300)) {
1543 pPlatData->msActuator.mnLRAFreq = value;
1544 dev_dbg(pDRV2624->dev, "ti,lra-frequency=%d\n",
1545 pPlatData->msActuator.mnLRAFreq);
1546 } else {
1547 dev_err(pDRV2624->dev,
1548 "ERROR, ti,lra-frequency=%d, out of range\n",
1549 pPlatData->msActuator.mnLRAFreq);
1550 return -EINVAL;
1551 }
1552 } else {
1553 dev_err(pDRV2624->dev,
1554 "Looking up %s property in node %s failed %d\n",
1555 "ti,lra-frequency", np->full_name, rc);
1556 return -EINVAL;
1557 }
1558 }
1560 pPlatData->mnGpioINT = of_get_named_gpio(np, "ti,irq-gpio", 0);
1561 if (pPlatData->mnGpioINT >= 0) {
1562 dev_dbg(pDRV2624->dev,
1563 "ti,irq-gpio=%d\n", pPlatData->mnGpioINT);
1564 } else {
1565 dev_err(pDRV2624->dev,
1566 "Looking up %s property in node %s failed %d\n",
1567 "ti,irq-gpio", np->full_name, pPlatData->mnGpioINT);
1568 return -EINVAL;
1569 }
1570 return nResult;
1571 }
1573 static void drv2624_close(struct input_dev *input)
1574 {
1575 struct drv2624_data *pDRV2624 = input_get_drvdata(input);
1577 dev_dbg(pDRV2624->dev, "%s\n", __func__);
1578 mutex_lock(&pDRV2624->lock);
1579 if (hrtimer_active(&pDRV2624->haptics_timer)
1580 || pDRV2624->mnVibratorPlaying)
1581 drv2624_stop(pDRV2624);
1582 mutex_unlock(&pDRV2624->lock);
1584 }
1586 /**
1587 * Rated and Overdriver Voltages:
1588 * Calculated using the formula r = voltage(V) * 255 / 5.6
1589 * where r is what will be written to the register
1590 * and v is the rated or overdriver voltage of the actuator
1591 **/
1592 static inline int drv2624_calculate_voltage(unsigned int voltage)
1593 {
1594 return (voltage * 255 / 5600);
1595 }
1597 static int drv2624_hw_reset(struct drv2624_data *pDRV2624)
1598 {
1599 int nResult = 0;
1601 dev_dbg(pDRV2624->dev, "%s: enter!\n", __func__);
1603 gpio_direction_output(pDRV2624->msPlatData.mnGpioNRST, 0);
1604 mdelay(5);
1605 gpio_direction_output(pDRV2624->msPlatData.mnGpioNRST, 1);
1606 mdelay(2);
1608 return nResult;
1609 }
1611 static int drv2624_i2c_probe(struct i2c_client *client,
1612 const struct i2c_device_id *id)
1613 {
1614 int nResult = 0;
1615 struct drv2624_data *pDRV2624;
1616 struct ff_device *ff;
1618 dev_info(&client->dev, "%s enter\n", __func__);
1620 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1621 dev_err(&client->dev, "%s:I2C check failed\n", __func__);
1622 return -ENODEV;
1623 }
1625 pDRV2624 = devm_kzalloc(&client->dev,
1626 sizeof(struct drv2624_data), GFP_KERNEL);
1627 if (pDRV2624 == NULL) {
1628 //dev_err(&client->dev, "%s: error\n", __func__);
1629 return -ENOMEM;
1630 }
1632 pDRV2624->dev = &client->dev;
1633 pDRV2624->client = client;
1634 i2c_set_clientdata(client, pDRV2624);
1635 dev_set_drvdata(&client->dev, pDRV2624);
1637 pDRV2624->mpRegmap = devm_regmap_init_i2c(client, &drv2624_i2c_regmap);
1638 if (IS_ERR(pDRV2624->mpRegmap)) {
1639 nResult = PTR_ERR(pDRV2624->mpRegmap);
1640 dev_err(pDRV2624->dev, "%s:Fail to allocate register map:%d\n",
1641 __func__, nResult);
1642 goto free_mem;
1643 }
1645 if (client->dev.of_node) {
1646 dev_dbg(pDRV2624->dev, "of node parse\n");
1647 nResult = drv2624_parse_dt(&client->dev, pDRV2624);
1648 if (nResult < 0) {
1649 dev_err(pDRV2624->dev, "%s: parse_dt failed %d\n",
1650 __func__, nResult);
1651 goto free_gpio;
1652 }
1653 } else if (client->dev.platform_data) {
1654 dev_dbg(pDRV2624->dev, "platform data parse\n");
1655 memcpy(&pDRV2624->msPlatData, client->dev.platform_data,
1656 sizeof(struct drv2624_platform_data));
1657 } else {
1658 dev_err(pDRV2624->dev,
1659 "%s: ERROR no platform data\n", __func__);
1660 goto free_gpio;
1661 }
1663 if (gpio_is_valid(pDRV2624->msPlatData.mnGpioNRST)) {
1664 nResult = gpio_request(pDRV2624->msPlatData.mnGpioNRST,
1665 "DRV2624-NRST");
1666 if (nResult < 0) {
1667 dev_err(pDRV2624->dev,
1668 "%s: GPIO %d request NRST error\n", __func__,
1669 pDRV2624->msPlatData.mnGpioNRST);
1670 goto free_gpio;
1671 }
1672 drv2624_hw_reset(pDRV2624);
1673 }
1675 mutex_init(&pDRV2624->dev_lock);
1677 nResult = drv2624_reg_read(pDRV2624, DRV2624_REG_ID);
1678 if (nResult < 0)
1679 goto destroy_mutex;
1680 else {
1681 dev_info(pDRV2624->dev,
1682 "%s, ID status (0x%x)\n", __func__, nResult);
1683 pDRV2624->mnDeviceID = nResult;
1684 }
1686 if ((pDRV2624->mnDeviceID & 0xf0) != DRV2624_ID) {
1687 dev_err(pDRV2624->dev, "%s, device_id(0x%x) fail\n",
1688 __func__, pDRV2624->mnDeviceID);
1689 goto destroy_mutex;
1690 }
1692 drv2624_init(pDRV2624);
1694 if (gpio_is_valid(pDRV2624->msPlatData.mnGpioINT)) {
1695 nResult =
1696 gpio_request(pDRV2624->msPlatData.mnGpioINT, "DRV2624-IRQ");
1697 if (nResult < 0) {
1698 dev_err(pDRV2624->dev, "%s: GPIO %d request INT err\n",
1699 __func__, pDRV2624->msPlatData.mnGpioINT);
1700 goto destroy_mutex;
1701 }
1702 gpio_direction_input(pDRV2624->msPlatData.mnGpioINT);
1703 pDRV2624->mnIRQ = gpio_to_irq(pDRV2624->msPlatData.mnGpioINT);
1705 dev_dbg(pDRV2624->dev, "irq = %d\n", pDRV2624->mnIRQ);
1707 nResult = request_threaded_irq(pDRV2624->mnIRQ,
1708 drv2624_irq_handler, NULL,
1709 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1710 client->name, pDRV2624);
1711 if (nResult < 0) {
1712 dev_err(pDRV2624->dev,
1713 "request_irq failed, %d\n", nResult);
1714 goto destroy_mutex;
1715 }
1717 disable_irq_nosync(pDRV2624->mnIRQ);
1718 pDRV2624->mbIRQEnabled = false;
1719 pDRV2624->mbIRQUsed = true;
1720 } else
1721 pDRV2624->mbIRQUsed = false;
1723 /* input dev init */
1724 pDRV2624->input_dev = devm_input_allocate_device(&client->dev);
1725 if (!pDRV2624->input_dev) {
1726 dev_err(&client->dev, "Failed to allocate input device_node\n");
1727 goto destroy_mutex;
1728 }
1730 pDRV2624->input_dev->name = "drv2624:haptics";
1731 pDRV2624->input_dev->close = drv2624_close;
1732 input_set_drvdata(pDRV2624->input_dev, pDRV2624);
1733 input_set_capability(pDRV2624->input_dev, EV_FF, FF_RUMBLE);
1735 input_set_capability(pDRV2624->input_dev, EV_FF, FF_CONSTANT);
1736 input_set_capability(pDRV2624->input_dev, EV_FF, FF_GAIN);
1737 input_set_capability(pDRV2624->input_dev, EV_FF, FF_PERIODIC);
1738 input_set_capability(pDRV2624->input_dev, EV_FF, FF_CUSTOM);
1740 nResult = input_ff_create(pDRV2624->input_dev, EFFECT_MAX_NUM);
1741 if (nResult) {
1742 dev_err(&client->dev, "input_ff_create() failed: %d\n",
1743 nResult);
1744 goto destroy_mutex;
1745 }
1747 ff = pDRV2624->input_dev->ff;
1748 ff->upload = drv2624_haptics_upload_effect;
1749 ff->playback = drv2624_haptics_playback;
1750 ff->erase = drv2624_haptics_erase;
1751 ff->set_gain = drv2624_haptics_set_gain;
1753 nResult = input_register_device(pDRV2624->input_dev);
1754 if (nResult) {
1755 dev_err(&client->dev, "couldn't register input device: %d\n",
1756 nResult);
1757 goto destory_ff;
1758 }
1760 Haptics_init(pDRV2624);
1761 g_DRV2624data = pDRV2624;
1763 request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG, "drv2624.bin",
1764 &(client->dev), GFP_KERNEL, pDRV2624, drv2624_firmware_load);
1766 dev_info(pDRV2624->dev, "drv2624 probe succeeded\n");
1768 return 0;
1770 destory_ff:
1771 input_ff_destroy(pDRV2624->input_dev);
1772 destroy_mutex:
1773 mutex_destroy(&pDRV2624->dev_lock);
1774 free_gpio:
1775 if (gpio_is_valid(pDRV2624->msPlatData.mnGpioINT))
1776 gpio_free(pDRV2624->msPlatData.mnGpioINT);
1777 if (gpio_is_valid(pDRV2624->msPlatData.mnGpioNRST))
1778 gpio_free(pDRV2624->msPlatData.mnGpioNRST);
1779 free_mem:
1780 if (pDRV2624 != NULL)
1781 kfree(pDRV2624);
1783 return nResult;
1784 }
1786 static int drv2624_i2c_remove(struct i2c_client *client)
1787 {
1788 struct drv2624_data *pDRV2624 = i2c_get_clientdata(client);
1790 if (pDRV2624->msPlatData.mnGpioNRST)
1791 gpio_free(pDRV2624->msPlatData.mnGpioNRST);
1793 if (pDRV2624->msPlatData.mnGpioINT)
1794 gpio_free(pDRV2624->msPlatData.mnGpioINT);
1796 misc_deregister(&drv2624_misc);
1798 input_ff_destroy(pDRV2624->input_dev);
1799 mutex_destroy(&pDRV2624->lock);
1800 mutex_destroy(&pDRV2624->dev_lock);
1802 return 0;
1803 }
1805 static const struct i2c_device_id drv2624_i2c_id[] = {
1806 {"drv2624", 0},
1807 {}
1808 };
1810 #ifdef CONFIG_PM_SLEEP
1811 static int __maybe_unused drv2624_suspend(struct device *dev)
1812 {
1813 struct drv2624_data *pDRV2624 = dev_get_drvdata(dev);
1815 dev_dbg(pDRV2624->dev, "%s enter!\n", __func__);
1816 mutex_lock(&pDRV2624->lock);
1817 if (hrtimer_active(&pDRV2624->haptics_timer) ||
1818 pDRV2624->mnVibratorPlaying) {
1819 drv2624_stop(pDRV2624);
1820 }
1821 /*
1822 * set device to standby mode
1823 */
1824 drv2624_set_bits(pDRV2624,
1825 DRV2624_REG_CONTROL1,
1826 DRV2624_AUTO_BRK_INTO_STBY_MASK,
1827 DRV2624_STBY_MODE_WITH_AUTO_BRAKE);
1828 mutex_unlock(&pDRV2624->lock);
1830 return 0;
1831 }
1833 static int __maybe_unused drv2624_resume(struct device *dev)
1834 {
1835 struct drv2624_data *pDRV2624 = dev_get_drvdata(dev);
1837 dev_dbg(pDRV2624->dev, "%s enter!\n", __func__);
1839 mutex_lock(&pDRV2624->lock);
1840 /* set device to active mode */
1841 drv2624_reg_write(pDRV2624,
1842 DRV2624_REG_CONTROL1,
1843 DRV2624_REMOVE_STBY_MODE);
1844 mutex_unlock(&pDRV2624->lock);
1846 return 0;
1847 }
1849 static SIMPLE_DEV_PM_OPS(drv2624_pm_ops, drv2624_suspend, drv2624_resume);
1850 #endif
1852 MODULE_DEVICE_TABLE(i2c, drv2624_i2c_id);
1854 #if defined(CONFIG_OF)
1855 static const struct of_device_id drv2624_of_match[] = {
1856 {.compatible = "ti,drv2624"},
1857 {},
1858 };
1860 MODULE_DEVICE_TABLE(of, drv2624_of_match);
1861 #endif
1863 static struct i2c_driver drv2624_i2c_driver = {
1864 .driver = {
1865 .name = "drv2624",
1866 .owner = THIS_MODULE,
1867 #ifdef CONFIG_PM_SLEEP
1868 .pm = &drv2624_pm_ops,
1869 #endif
1870 #if defined(CONFIG_OF)
1871 .of_match_table = of_match_ptr(drv2624_of_match),
1872 #endif
1873 },
1874 .probe = drv2624_i2c_probe,
1875 .remove = drv2624_i2c_remove,
1876 .id_table = drv2624_i2c_id,
1877 };
1879 module_i2c_driver(drv2624_i2c_driver);
1881 MODULE_AUTHOR("Texas Instruments Inc.");
1882 MODULE_DESCRIPTION("DRV2624 I2C Smart Haptics driver");
1883 MODULE_LICENSE("GPL");