first commit
[drv262xsw-android/drv2624-android-driver.git] / drv2624.c
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/module.h>
31 #include <linux/kernel.h>
32 #include <linux/slab.h>
33 #include <linux/types.h>
34 #include <linux/fs.h>
35 #include <linux/i2c.h>
36 #include <linux/semaphore.h>
37 #include <linux/device.h>
38 #include <linux/syscalls.h>
39 #include <asm/uaccess.h>
40 #include <linux/gpio.h>
41 #include <linux/sched.h>
42 #include <linux/spinlock_types.h>
43 #include <linux/spinlock.h>
44 #include <linux/delay.h>
45 #include <linux/of.h>
46 #include <linux/of_gpio.h>
47 #include <linux/jiffies.h>
48 #include <linux/err.h>
49 #include <linux/clk.h>
50 #include <linux/miscdevice.h>
51 #include "drv2624.h"
53 //#define       AUTOCALIBRATION_ENABLE
55 static struct drv2624_data *g_DRV2624data = NULL;
57 static int drv2624_reg_read(struct drv2624_data *pDRV2624, unsigned char reg)
58 {
59         unsigned int val;
60         int ret;
62         mutex_lock(&pDRV2624->dev_lock);        
63         ret = regmap_read(pDRV2624->mpRegmap, reg, &val);
64     mutex_unlock(&pDRV2624->dev_lock);  
66         if (ret < 0){
67                 dev_err(pDRV2624->dev, 
68                         "%s reg=0x%x error %d\n", __FUNCTION__, reg, ret);
69                 return ret;
70         }else{
71                 dev_dbg(pDRV2624->dev, "%s, Reg[0x%x]=0x%x\n", 
72                         __FUNCTION__, reg, val);
73                 return val;
74         }
75 }
77 static int drv2624_reg_write(struct drv2624_data *pDRV2624, 
78         unsigned char reg, unsigned char val)
79 {
80         int ret;
82         mutex_lock(&pDRV2624->dev_lock);                
83         ret = regmap_write(pDRV2624->mpRegmap, reg, val);
84     mutex_unlock(&pDRV2624->dev_lock);          
85         if (ret < 0){
86                 dev_err(pDRV2624->dev, 
87                         "%s reg=0x%x, value=0%x error %d\n", 
88                         __FUNCTION__, reg, val, ret);
89         }else{
90                 dev_dbg(pDRV2624->dev, "%s, Reg[0x%x]=0x%x\n", 
91                         __FUNCTION__, reg, val);
92         }
93         
94         return ret;
95 }
97 static int drv2624_bulk_read(struct drv2624_data *pDRV2624, 
98         unsigned char reg, u8 *buf, unsigned int count)
99 {
100         int ret;
101         mutex_lock(&pDRV2624->dev_lock);        
102         ret = regmap_bulk_read(pDRV2624->mpRegmap, reg, buf, count);
103     mutex_unlock(&pDRV2624->dev_lock);          
104         if (ret < 0){
105                 dev_err(pDRV2624->dev, 
106                         "%s reg=0%x, count=%d error %d\n", 
107                         __FUNCTION__, reg, count, ret);
108         }
109         
110         return ret;
113 static int drv2624_bulk_write(struct drv2624_data *pDRV2624, 
114         unsigned char reg, const u8 *buf, unsigned int count)
116         int ret, i;
117         mutex_lock(&pDRV2624->dev_lock);        
118         ret = regmap_bulk_write(pDRV2624->mpRegmap, reg, buf, count);
119     mutex_unlock(&pDRV2624->dev_lock);          
120         if (ret < 0){
121                 dev_err(pDRV2624->dev, 
122                         "%s reg=0%x, count=%d error %d\n", 
123                         __FUNCTION__, reg, count, ret);
124         }else{
125                 for(i = 0; i < count; i++)
126                         dev_dbg(pDRV2624->dev, "%s, Reg[0x%x]=0x%x\n", 
127                                 __FUNCTION__,reg+i, buf[i]);
128         }
129         return ret;     
132 static int drv2624_set_bits(struct drv2624_data *pDRV2624, 
133         unsigned char reg, unsigned char mask, unsigned char val)
135         int ret;
136     mutex_lock(&pDRV2624->dev_lock);            
137         ret = regmap_update_bits(pDRV2624->mpRegmap, reg, mask, val);
138         mutex_unlock(&pDRV2624->dev_lock);      
139         if (ret < 0){
140                 dev_err(pDRV2624->dev, 
141                         "%s reg=%x, mask=0x%x, value=0x%x error %d\n", 
142                         __FUNCTION__, reg, mask, val, ret);
143         }else{
144                 dev_dbg(pDRV2624->dev, "%s, Reg[0x%x]:M=0x%x, V=0x%x\n", 
145                         __FUNCTION__, reg, mask, val);
146         }
147         
148         return ret;     
151 static void drv2624_enableIRQ(struct drv2624_data *pDRV2624, unsigned char bRTP)
153         unsigned char mask = INT_ENABLE_CRITICAL;
154         
155         if(bRTP == 0){
156                 mask = INT_ENABLE_ALL;
157         }
158         
159         drv2624_reg_read(pDRV2624,DRV2624_REG_STATUS);  
160         drv2624_reg_write(pDRV2624, DRV2624_REG_INT_ENABLE, mask);
161         enable_irq(pDRV2624->mnIRQ);
164 static void drv2624_disableIRQ(struct drv2624_data *pDRV2624)
166         disable_irq(pDRV2624->mnIRQ);
167         drv2624_reg_write(pDRV2624, DRV2624_REG_INT_ENABLE, INT_MASK_ALL);
170 static int drv2624_set_go_bit(struct drv2624_data *pDRV2624, unsigned char val)
172         int ret = 0, value =0;
173         int retry = 10; // to finish auto-brake
174         
175         val &= 0x01;    
176         ret = drv2624_reg_write(pDRV2624, DRV2624_REG_GO, val);
177         if(ret >= 0 ){          
178                 if(val == 1){
179                         mdelay(1);
180                         value = drv2624_reg_read(pDRV2624, DRV2624_REG_GO);
181                         if(value < 0){
182                                 ret = value;
183                         }else if(value != 1){
184                                 ret = -1;
185                                 dev_warn(pDRV2624->dev, 
186                                         "%s, GO fail, stop action\n", __FUNCTION__);
187                         }
188                 }else{
189                         while(retry > 0){
190                                 value = drv2624_reg_read(pDRV2624, DRV2624_REG_GO);
191                                 if(value < 0){
192                                         ret = value;
193                                         break;
194                                 }
195                                 
196                                 if(value==0) break;
197                                 mdelay(10);
198                                 retry--;                                
199                         }       
200                         
201                         if(retry == 0){
202                                 dev_err(pDRV2624->dev, 
203                                         "%s, ERROR: clear GO fail\n", __FUNCTION__);
204                         }
205                 }
206         }
207                 
208         return ret;
211 static void drv2624_change_mode(struct drv2624_data *pDRV2624, unsigned char work_mode)
213         drv2624_set_bits(pDRV2624, DRV2624_REG_MODE, WORKMODE_MASK , work_mode);
216 static int vibrator_get_time(struct timed_output_dev *dev)
218         struct drv2624_data *pDRV2624 = container_of(dev, struct drv2624_data, to_dev);
220     if (hrtimer_active(&pDRV2624->timer)) {
221         ktime_t r = hrtimer_get_remaining(&pDRV2624->timer);
222         return ktime_to_ms(r);
223     }
225     return 0;
228 static void drv2624_stop(struct drv2624_data *pDRV2624)
230         if(pDRV2624->mnVibratorPlaying == YES){
231                 dev_dbg(pDRV2624->dev, "%s\n", __FUNCTION__);
232                 drv2624_disableIRQ(pDRV2624);
233                 hrtimer_cancel(&pDRV2624->timer);       
234                 drv2624_set_go_bit(pDRV2624, STOP);
235                 pDRV2624->mnVibratorPlaying = NO;
236                 wake_unlock(&pDRV2624->wklock);
237         }
240 static void vibrator_enable( struct timed_output_dev *dev, int value)
242         int ret = 0;
243         struct drv2624_data *pDRV2624 = 
244                 container_of(dev, struct drv2624_data, to_dev);
245         //int status;
246         
247         dev_dbg(pDRV2624->dev, 
248                 "%s, value=%d\n", __FUNCTION__, value);
249                 
250     mutex_lock(&pDRV2624->lock);
251         
252         pDRV2624->mnWorkMode = WORK_IDLE;
253         dev_dbg(pDRV2624->dev, 
254                 "%s, afer mnWorkMode=0x%x\n", 
255                 __FUNCTION__, pDRV2624->mnWorkMode);
256                 
257         drv2624_stop(pDRV2624);
259     if (value > 0) {
260                 wake_lock(&pDRV2624->wklock);
261         
262                 drv2624_change_mode(pDRV2624, MODE_RTP);
263                 pDRV2624->mnVibratorPlaying = YES;
264                 drv2624_enableIRQ(pDRV2624, YES);       
265                 ret = drv2624_set_go_bit(pDRV2624, GO);
266                 if(ret <0){
267                         dev_warn(pDRV2624->dev, "Start RTP failed\n");
268                         wake_unlock(&pDRV2624->wklock);
269                         pDRV2624->mnVibratorPlaying = NO;
270                         drv2624_disableIRQ(pDRV2624);
271                 }else{
272                         value = (value>MAX_TIMEOUT)?MAX_TIMEOUT:value;
273                         hrtimer_start(&pDRV2624->timer, 
274                                 ns_to_ktime((u64)value * NSEC_PER_MSEC), HRTIMER_MODE_REL);
275                 }
276     }
277         
278         mutex_unlock(&pDRV2624->lock);
281 static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
283         struct drv2624_data *pDRV2624 = 
284                 container_of(timer, struct drv2624_data, timer);
286         dev_dbg(pDRV2624->dev, "%s\n", __FUNCTION__);
287         pDRV2624->mnWorkMode |= WORK_VIBRATOR;
288         schedule_work(&pDRV2624->vibrator_work);
289     return HRTIMER_NORESTART;
292 static void vibrator_work_routine(struct work_struct *work)
294         struct drv2624_data *pDRV2624 = 
295                 container_of(work, struct drv2624_data, vibrator_work);
296         unsigned char mode = MODE_RTP;
297         unsigned char status; 
298         int ret = 0;
299                 
300         mutex_lock(&pDRV2624->lock);
302         dev_dbg(pDRV2624->dev, 
303                 "%s, afer mnWorkMode=0x%x\n", 
304                 __FUNCTION__, pDRV2624->mnWorkMode);
305                 
306         if(pDRV2624->mnWorkMode & WORK_IRQ){    
307                 ret = drv2624_reg_read(pDRV2624,DRV2624_REG_STATUS);
308                 if(ret >= 0) pDRV2624->mnIntStatus = ret;
309                 drv2624_disableIRQ(pDRV2624);   
310                 if(ret < 0){
311                         dev_err(pDRV2624->dev, 
312                                 "%s, reg read error\n",
313                                 __FUNCTION__);
314                         goto err;               
315                 }
316                 
317                 status = pDRV2624->mnIntStatus;
318                 dev_dbg(pDRV2624->dev, 
319                         "%s, status=0x%x\n", 
320                         __FUNCTION__, pDRV2624->mnIntStatus);
321                 
322                 if(status & OVERCURRENT_MASK){
323                         dev_err(pDRV2624->dev, 
324                                 "ERROR, Over Current detected!!\n");
325                 }
326                 
327                 if(status & OVERTEMPRATURE_MASK){
328                         dev_err(pDRV2624->dev, 
329                                 "ERROR, Over Temperature detected!!\n");
330                 }
331                 
332                 if(status & ULVO_MASK){
333                         dev_err(pDRV2624->dev, 
334                                 "ERROR, VDD drop observed!!\n");
335                 }
337                 if(status & PRG_ERR_MASK){
338                         dev_err(pDRV2624->dev, 
339                                 "ERROR, PRG error!!\n");                        
340                 }
342                 if(status & PROCESS_DONE_MASK){
343                         ret = drv2624_reg_read(pDRV2624, DRV2624_REG_MODE);
344                         if(ret < 0)     {
345                                 dev_err(pDRV2624->dev, 
346                                         "%s, reg read error\n",
347                                         __FUNCTION__);
348                                 goto err;               
349                         }
350                 
351                         mode =  ret& WORKMODE_MASK;
352                         if(mode == MODE_CALIBRATION){
353                                 pDRV2624->mAutoCalResult.mnResult = status;
354                                 if((status&DIAG_MASK) != DIAG_SUCCESS){
355                                         dev_err(pDRV2624->dev, "Calibration fail\n");
356                                 }else{
357                                         pDRV2624->mAutoCalResult.mnCalComp = 
358                                                 drv2624_reg_read(pDRV2624, DRV2624_REG_CAL_COMP);
359                                         pDRV2624->mAutoCalResult.mnCalBemf = 
360                                                 drv2624_reg_read(pDRV2624, DRV2624_REG_CAL_BEMF);
361                                         pDRV2624->mAutoCalResult.mnCalGain = 
362                                                 drv2624_reg_read(pDRV2624, DRV2624_REG_CAL_COMP) & BEMFGAIN_MASK;
363                                         dev_dbg(pDRV2624->dev, 
364                                                 "AutoCal : Comp=0x%x, Bemf=0x%x, Gain=0x%x\n",
365                                                 pDRV2624->mAutoCalResult.mnCalComp, 
366                                                 pDRV2624->mAutoCalResult.mnCalBemf,
367                                                 pDRV2624->mAutoCalResult.mnCalGain);
368                                 }       
369                         }else if(mode == MODE_DIAGNOSTIC){
370                                 pDRV2624->mDiagResult.mnResult = status;
371                                 if((status&DIAG_MASK) != DIAG_SUCCESS){
372                                         dev_err(pDRV2624->dev, "Diagnostic fail\n");
373                                 }else{
374                                         pDRV2624->mDiagResult.mnDiagZ = 
375                                                 drv2624_reg_read(pDRV2624, DRV2624_REG_DIAG_Z);
376                                         pDRV2624->mDiagResult.mnDiagK = 
377                                                 drv2624_reg_read(pDRV2624, DRV2624_REG_DIAG_K);
378                                         dev_dbg(pDRV2624->dev, 
379                                                 "Diag : ZResult=0x%x, CurrentK=0x%x\n",
380                                                 pDRV2624->mDiagResult.mnDiagZ,
381                                                 pDRV2624->mDiagResult.mnDiagK);
382                                 }                                               
383                         }else if(mode == MODE_WAVEFORM_SEQUENCER){
384                                 dev_dbg(pDRV2624->dev, 
385                                         "Waveform Sequencer Playback finished\n");
386                         }else if(mode == MODE_RTP){
387                                 dev_dbg(pDRV2624->dev, 
388                                         "RTP IRQ\n");
389                         }
390                 }
391                 
392                 if((mode != MODE_RTP)&&
393                         (pDRV2624->mnVibratorPlaying == YES)){
394                         dev_info(pDRV2624->dev, "release wklock\n");
395                         pDRV2624->mnVibratorPlaying = NO;
396                         wake_unlock(&pDRV2624->wklock);
397                 }
398                         
399                 pDRV2624->mnWorkMode &= ~WORK_IRQ;
400         }
401         
402         if(pDRV2624->mnWorkMode & WORK_VIBRATOR){
403                 drv2624_stop(pDRV2624);
404                 pDRV2624->mnWorkMode &= ~WORK_VIBRATOR;
405         }
406 err:
407         
408         mutex_unlock(&pDRV2624->lock);
411 static int dev_auto_calibrate(struct drv2624_data *pDRV2624)
413         int ret = 0;
414         
415         dev_info(pDRV2624->dev, "%s\n", __FUNCTION__);
416         wake_lock(&pDRV2624->wklock);
417         pDRV2624->mnVibratorPlaying = YES;      
418         drv2624_change_mode(pDRV2624, MODE_CALIBRATION);
419         ret = drv2624_set_go_bit(pDRV2624, GO);
420         if(ret < 0){
421                 dev_warn(pDRV2624->dev, "calibration start fail\n");
422                 wake_unlock(&pDRV2624->wklock);
423                 pDRV2624->mnVibratorPlaying = NO;                       
424         }else{
425                 dev_dbg(pDRV2624->dev, "calibration start\n");
426         }
427         return ret;
430 static int dev_run_diagnostics(struct drv2624_data *pDRV2624)
432         int ret = 0;
433         
434         dev_info(pDRV2624->dev, "%s\n", __FUNCTION__);
435         wake_lock(&pDRV2624->wklock);
436         pDRV2624->mnVibratorPlaying = YES;      
437         drv2624_change_mode(pDRV2624, MODE_DIAGNOSTIC);
438         ret = drv2624_set_go_bit(pDRV2624, GO);
439         if(ret < 0){
440                 dev_warn(pDRV2624->dev, "Diag start fail\n");
441                 wake_unlock(&pDRV2624->wklock);
442                 pDRV2624->mnVibratorPlaying = NO;                       
443         }else{
444                 dev_dbg(pDRV2624->dev, "Diag start\n");
445         }
446         
447         return ret;
450 static int drv2624_playEffect(struct drv2624_data *pDRV2624)
452         int ret = 0;
453         dev_info(pDRV2624->dev, "%s\n", __FUNCTION__);
454         wake_lock(&pDRV2624->wklock);
455         pDRV2624->mnVibratorPlaying = YES;      
456         drv2624_change_mode(pDRV2624, MODE_WAVEFORM_SEQUENCER);
457         ret = drv2624_set_go_bit(pDRV2624, GO);
458         if(ret < 0){
459                 dev_warn(pDRV2624->dev, "effects start fail\n");
460                 wake_unlock(&pDRV2624->wklock);
461                 pDRV2624->mnVibratorPlaying = NO;                       
462         }else{
463                 dev_dbg(pDRV2624->dev, "effects start\n");
464         }
465         
466         return ret;
468         
469 static int drv2624_config_waveform(struct drv2624_data *pDRV2624,
470         struct drv2624_wave_setting *psetting)
472         int ret = 0;
473         int value = 0;
474         
475         ret = drv2624_reg_write(pDRV2624, 
476                         DRV2624_REG_MAIN_LOOP, psetting->mnLoop&0x07);
477         if(ret >= 0){
478                 value |= ((psetting->mnInterval&0x01)<<INTERVAL_SHIFT);
479                 value |= (psetting->mnScale&0x03);
480                 drv2624_set_bits(pDRV2624,
481                         DRV2624_REG_CONTROL2, INTERVAL_MASK|SCALE_MASK, value);
482         }
483         return ret;
486 static int drv2624_set_waveform(struct drv2624_data *pDRV2624,
487         struct drv2624_waveform_sequencer *pSequencer)
489         int ret = 0;
490         int i = 0;
491         unsigned char loop[2] = {0};
492         unsigned char effects[DRV2624_SEQUENCER_SIZE] = {0};
493         unsigned char len = 0;
494         
495         for(i = 0; i < DRV2624_SEQUENCER_SIZE; i++){
496                 len++;
497                 if(pSequencer->msWaveform[i].mnEffect != 0){                    
498                         if(i < 4) 
499                                 loop[0] |= 
500                                                 (pSequencer->msWaveform[i].mnLoop << (2*i));
501                         else
502                                 loop[1] |= 
503                                                 (pSequencer->msWaveform[i].mnLoop << (2*(i-4)));
504                         
505                         effects[i] = pSequencer->msWaveform[i].mnEffect;
506                 }else{                  
507                         break;
508                 }               
509         }
510         
511         if(len == 1)
512                 ret = drv2624_reg_write(pDRV2624, DRV2624_REG_SEQUENCER_1, 0);
513         else 
514                 ret = drv2624_bulk_write(pDRV2624, DRV2624_REG_SEQUENCER_1, effects, len);
516         if(ret < 0){ 
517                 dev_err(pDRV2624->dev, "sequence error\n");
518         }
519         
520         if(ret >= 0){
521                 if(len > 1){
522                         if((len-1) <= 4)
523                                 drv2624_reg_write(pDRV2624, DRV2624_REG_SEQ_LOOP_1, loop[0]);
524                         else
525                                 drv2624_bulk_write(pDRV2624, DRV2624_REG_SEQ_LOOP_1, loop, 2);  
526                 }
527         }
528         
529         return ret;
532 static int fw_chksum(const struct firmware *fw){
533         int sum = 0;
534         int i=0;
535         int size = fw->size;
536         const unsigned char *pBuf = fw->data;
537         
538         for (i=0; i< size; i++){
539                 if((i>11) && (i<16)){
540                 
541                 }else{
542                         sum += pBuf[i];
543                 }
544         }
546         return sum;
549 /* drv2624_firmware_load:   This function is called by the
550  *              request_firmware_nowait function as soon
551  *              as the firmware has been loaded from the file.
552  *              The firmware structure contains the data and$
553  *              the size of the firmware loaded.
554  * @fw: pointer to firmware file to be dowloaded
555  * @context: pointer variable to drv2624 data
556  *
557  * 
558  */
559 static void drv2624_firmware_load(const struct firmware *fw, void *context)
561         struct drv2624_data *pDRV2624 = context;
562         int size = 0, fwsize = 0, i=0;
563         const unsigned char *pBuf = NULL;
565     mutex_lock(&pDRV2624->lock);
566         
567         if(fw != NULL){
568                 pBuf = fw->data;
569                 size = fw->size;
570                 if(size > 1024){
571                         dev_err(pDRV2624->dev,"%s, ERROR!! firmware size %d too big\n", 
572                                 __FUNCTION__, size);
573                 }else{
574                         memcpy(&(pDRV2624->fw_header), pBuf, sizeof(struct drv2624_fw_header));
575                         if((pDRV2624->fw_header.fw_magic != DRV2624_MAGIC) 
576                                 ||(pDRV2624->fw_header.fw_size != size)
577                                 ||(pDRV2624->fw_header.fw_chksum != fw_chksum(fw))){
578                                 dev_err(pDRV2624->dev,"%s, ERROR!! firmware not right:Magic=0x%x,Size=%d,chksum=0x%x\n", 
579                                         __FUNCTION__, pDRV2624->fw_header.fw_magic, 
580                                         pDRV2624->fw_header.fw_size, pDRV2624->fw_header.fw_chksum);
581                         }else{
582                                 dev_info(pDRV2624->dev,"%s, firmware good\n", __FUNCTION__);
583                 
584                                 pBuf += sizeof(struct drv2624_fw_header);
585                                 
586                                 drv2624_reg_write(pDRV2624, DRV2624_REG_RAM_ADDR_UPPER, 0);
587                                 drv2624_reg_write(pDRV2624, DRV2624_REG_RAM_ADDR_LOWER, 0);
588                                 
589                                 fwsize = size - sizeof(struct drv2624_fw_header);
590                                 for(i = 0; i < fwsize; i++){
591                                         drv2624_reg_write(pDRV2624, DRV2624_REG_RAM_DATA, pBuf[i]);
592                                 }       
593                         }                               
594                 }
595         }else{
596                 dev_err(pDRV2624->dev, "%s, ERROR!! firmware not found\n", __FUNCTION__);
597         }
598         
599         release_firmware(fw);
600         
601     mutex_unlock(&pDRV2624->lock);
604 static int drv2624_file_open(struct inode *inode, struct file *file)
606         if (!try_module_get(THIS_MODULE)) return -ENODEV;
608         file->private_data = (void*)g_DRV2624data;
609         return 0;
612 static int drv2624_file_release(struct inode *inode, struct file *file)
614         file->private_data = (void*)NULL;
615         module_put(THIS_MODULE);
616         return 0;
619 static long drv2624_file_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
621         struct drv2624_data *pDRV2624 = file->private_data;
622         //void __user *user_arg = (void __user *)arg;
623         int ret = 0;
624         
625         mutex_lock(&pDRV2624->lock);
626         
627         dev_dbg(pDRV2624->dev, "ioctl 0x%x\n", cmd);
628         
629         switch (cmd) {
630                 
631         }
632         
633         mutex_unlock(&pDRV2624->lock);
634         
635         return ret;
638 static ssize_t drv2624_file_read(struct file* filp, char* buff, size_t length, loff_t* offset)
640         struct drv2624_data *pDRV2624 = (struct drv2624_data *)filp->private_data;
641         int ret = 0;
642         unsigned char value = 0;
643         unsigned char *p_kBuf = NULL;
645         mutex_lock(&pDRV2624->lock);
647         switch(pDRV2624->mnFileCmd)
648         {
649                 case HAPTIC_CMDID_REG_READ:
650                 {
651                         if(length == 1){
652                                 ret = drv2624_reg_read(pDRV2624, pDRV2624->mnCurrentReg);
653                                 if( 0 > ret) {
654                                         dev_err(pDRV2624->dev, "dev read fail %d\n", ret);
655                                 }else{
656                                         value = ret;
657                                         ret = copy_to_user(buff, &value, 1);
658                                         if (0 != ret) {
659                                                 /* Failed to copy all the data, exit */
660                                                 dev_err(pDRV2624->dev, "copy to user fail %d\n", ret);
661                                         }       
662                                 }
663                         }else if(length > 1){
664                                 p_kBuf = (unsigned char *)kzalloc(length, GFP_KERNEL);
665                                 if(p_kBuf != NULL){
666                                         ret = drv2624_bulk_read(pDRV2624, 
667                                                 pDRV2624->mnCurrentReg, p_kBuf, length);
668                                         if( 0 > ret) {
669                                                 dev_err(pDRV2624->dev, "dev bulk read fail %d\n", ret);
670                                         }else{
671                                                 ret = copy_to_user(buff, p_kBuf, length);
672                                                 if (0 != ret) {
673                                                         /* Failed to copy all the data, exit */
674                                                         dev_err(pDRV2624->dev, "copy to user fail %d\n", ret);
675                                                 }
676                                         }
677                                         
678                                         kfree(p_kBuf);
679                                 }else{
680                                         dev_err(pDRV2624->dev, "read no mem\n");
681                                         ret = -ENOMEM;
682                                 }
683                         }
684                         break;
685                 }
686                 
687                 case HAPTIC_CMDID_RUN_DIAG:
688                 {
689                         if(pDRV2624->mnVibratorPlaying){
690                                 length = 0;
691                         }else{
692                                 unsigned char buf[3] ;
693                                 buf[0] = pDRV2624->mDiagResult.mnResult;
694                                 buf[1] = pDRV2624->mDiagResult.mnDiagZ;
695                                 buf[2] = pDRV2624->mDiagResult.mnDiagK;
696                                 ret = copy_to_user(buff, buf, 3);
697                                 if (0 != ret) {
698                                         /* Failed to copy all the data, exit */
699                                         dev_err(pDRV2624->dev, "copy to user fail %d\n", ret);
700                                 }
701                         }
702                         break;
703                 }
704                 
705                 case HAPTIC_CMDID_RUN_CALIBRATION:
706                 {
707                         if(pDRV2624->mnVibratorPlaying){
708                                 length = 0;
709                         }else{
710                                 unsigned char buf[4] ;
711                                 buf[0] = pDRV2624->mAutoCalResult.mnResult;
712                                 buf[1] = pDRV2624->mAutoCalResult.mnCalComp;
713                                 buf[2] = pDRV2624->mAutoCalResult.mnCalBemf;
714                                 buf[3] = pDRV2624->mAutoCalResult.mnCalGain;
715                                 ret = copy_to_user(buff, buf, 4);
716                                 if (0 != ret) {
717                                         /* Failed to copy all the data, exit */
718                                         dev_err(pDRV2624->dev, "copy to user fail %d\n", ret);
719                                 }
720                         }
721                         break;
722                 }
723                 
724                 case HAPTIC_CMDID_CONFIG_WAVEFORM:
725                 {
726                         if(length == sizeof(struct drv2624_wave_setting)){
727                                 struct drv2624_wave_setting wavesetting;                
728                                 value = drv2624_reg_read(pDRV2624, DRV2624_REG_CONTROL2);
729                                 wavesetting.mnLoop = 
730                                         drv2624_reg_read(pDRV2624, DRV2624_REG_MAIN_LOOP)&0x07;
731                                 wavesetting.mnInterval = ((value&INTERVAL_MASK)>>INTERVAL_SHIFT);
732                                 wavesetting.mnScale = (value&SCALE_MASK);
733                                 ret = copy_to_user(buff, &wavesetting, length);
734                                 if (0 != ret) {
735                                         /* Failed to copy all the data, exit */
736                                         dev_err(pDRV2624->dev, "copy to user fail %d\n", ret);
737                                 }       
738                         }
739                         
740                         break;
741                 }
742                 
743                 case HAPTIC_CMDID_SET_SEQUENCER:
744                 {
745                         if(length == sizeof(struct drv2624_waveform_sequencer)){
746                                 struct drv2624_waveform_sequencer sequencer;
747                                 unsigned char effects[DRV2624_SEQUENCER_SIZE] = {0};
748                                 unsigned char loop[2] = {0};
749                                 int i = 0;
750                                 ret = drv2624_bulk_read(pDRV2624, 
751                                                 DRV2624_REG_SEQUENCER_1, 
752                                                 effects, DRV2624_SEQUENCER_SIZE);
753                                 if(ret < 0){
754                                         dev_err(pDRV2624->dev, "bulk read error %d\n", ret);
755                                         break;
756                                 }
757                                         
758                                 ret = drv2624_bulk_read(pDRV2624, 
759                                                 DRV2624_REG_SEQ_LOOP_1,
760                                                 loop, 2);
761                                                 
762                                 for(i=0; i < DRV2624_SEQUENCER_SIZE; i++){
763                                         sequencer.msWaveform[i].mnEffect = effects[i];
764                                         if(i < 4)
765                                                 sequencer.msWaveform[i].mnLoop = ((loop[0]>>(2*i))&0x03);
766                                         else
767                                                 sequencer.msWaveform[i].mnLoop = ((loop[1]>>(2*(i-4)))&0x03);
768                                 }
769                                 
770                                 ret = copy_to_user(buff, &sequencer, length);
771                                 if (0 != ret) {
772                                         /* Failed to copy all the data, exit */
773                                         dev_err(pDRV2624->dev, "copy to user fail %d\n", ret);
774                                 }       
775                         }
776                         
777                         break;
778                 }
779                 
780                 case HAPTIC_CMDID_READ_FIRMWARE:
781                 {
782                         if(length > 0){
783                                 int i = 0;
784                                 p_kBuf = (unsigned char *)kzalloc(length, GFP_KERNEL);
785                                 if(p_kBuf != NULL){
786                                         
787                                         drv2624_reg_write(pDRV2624, DRV2624_REG_RAM_ADDR_UPPER, pDRV2624->mRAMMSB);
788                                         drv2624_reg_write(pDRV2624, DRV2624_REG_RAM_ADDR_LOWER, pDRV2624->mRAMLSB);
789                                 
790                                         for(i=0; i < length; i++)
791                                                 p_kBuf[i] = drv2624_reg_read(pDRV2624,DRV2624_REG_RAM_DATA);
792                                         
793                                         if( 0 > ret) {
794                                                 dev_err(pDRV2624->dev, "dev read fail %d\n", ret);
795                                         }else{
796                                                 ret = copy_to_user(buff, p_kBuf, length);
797                                                 if (0 != ret) {
798                                                         /* Failed to copy all the data, exit */
799                                                         dev_err(pDRV2624->dev, "copy to user fail %d\n", ret);
800                                                 }
801                                         }
802                                         
803                                         kfree(p_kBuf);
804                                 }else{
805                                         dev_err(pDRV2624->dev, "read no mem\n");
806                                         ret = -ENOMEM;
807                                 }
808                         }
809                         break;                  
810                 }
812                 default:
813                         pDRV2624->mnFileCmd = 0;
814                         break;
815         }
816         
817         mutex_unlock(&pDRV2624->lock);
818         
819     return length;
822 static ssize_t drv2624_file_write(struct file* filp, const char* buff, size_t len, loff_t* off)
824         struct drv2624_data *pDRV2624 = 
825                 (struct drv2624_data *)filp->private_data;
826         unsigned char *p_kBuf = NULL;
827         int ret = 0;
828         
829     mutex_lock(&pDRV2624->lock);
830         
831         p_kBuf = (unsigned char *)kzalloc(len, GFP_KERNEL);
832         if(p_kBuf == NULL) {
833                 dev_err(pDRV2624->dev, "write no mem\n");
834                 goto err;
835         }
836         
837         ret = copy_from_user(p_kBuf, buff, len);
838         if (0 != ret) {
839                 dev_err(pDRV2624->dev,"copy_from_user failed.\n");
840                 goto err;
841         }
843         pDRV2624->mnFileCmd = p_kBuf[0];
844         
845     switch(pDRV2624->mnFileCmd)
846     {
847                 case HAPTIC_CMDID_REG_READ:
848                 {
849                         if(len == 2){
850                                 pDRV2624->mnCurrentReg = p_kBuf[1];
851                         }else{
852                                 dev_err(pDRV2624->dev, 
853                                         " read cmd len %d err\n", len);
854                         }
855                         break;
856                 }
857                 
858                 case HAPTIC_CMDID_REG_WRITE:
859                 {
860                         if((len-1) == 2){
861                                 drv2624_reg_write(pDRV2624, p_kBuf[1], p_kBuf[2]);      
862                         }else if((len-1)>2){
863                                 drv2624_bulk_write(pDRV2624, p_kBuf[1], &p_kBuf[2], len-2);
864                         }else{
865                                 dev_err(pDRV2624->dev, 
866                                         "%s, reg_write len %d error\n", __FUNCTION__, len);
867                         }
868                         break;
869                 }
870                 
871                 case HAPTIC_CMDID_REG_SETBIT:
872                 {
873                         if(len == 4){
874                                 drv2624_set_bits(pDRV2624, p_kBuf[1], p_kBuf[2], p_kBuf[3]);
875                         }else{
876                                 dev_err(pDRV2624->dev, 
877                                         "setbit len %d error\n", len);
878                         }
879                         break;
880                 }       
881                 
882                 case HAPTIC_CMDID_RUN_DIAG:
883                 {
884                         drv2624_stop(pDRV2624); 
885                         drv2624_enableIRQ(pDRV2624, NO);        
886                         ret = dev_run_diagnostics(pDRV2624);
887                         if(ret < 0) drv2624_disableIRQ(pDRV2624);
888                         break;
889                 }
890                 
891                 case HAPTIC_CMDID_UPDATE_FIRMWARE:
892                 {
893                         ret = request_firmware_nowait(THIS_MODULE, 
894                                                         FW_ACTION_HOTPLUG,
895                                                         "drv2624.bin",
896                                                         pDRV2624->dev, 
897                                                         GFP_KERNEL, 
898                                                         pDRV2624, 
899                                                         drv2624_firmware_load); 
900                         break;
901                 }
902                 
903                 case HAPTIC_CMDID_READ_FIRMWARE:
904                 {
905                         if(len == 3){
906                                 pDRV2624->mRAMMSB = buff[1];
907                                 pDRV2624->mRAMLSB = buff[2];
908                         }else{
909                                 dev_err(pDRV2624->dev, "%s, read fw len error\n", __FUNCTION__);
910                         }
911                         break;
912                 }
913                 
914                 case HAPTIC_CMDID_RUN_CALIBRATION:
915                 {
916                         drv2624_stop(pDRV2624); 
917                         drv2624_enableIRQ(pDRV2624, NO);        
918                         ret = dev_auto_calibrate(pDRV2624);
919                         if(ret < 0) drv2624_disableIRQ(pDRV2624);
920                         break;
921                 }
922                 
923                 case HAPTIC_CMDID_CONFIG_WAVEFORM:
924                 {
925                         if(len == (1+ sizeof(struct drv2624_wave_setting))){
926                                 struct drv2624_wave_setting wavesetting;
927                                 memcpy(&wavesetting, &p_kBuf[1], 
928                                         sizeof(struct drv2624_wave_setting));
929                                 ret = drv2624_config_waveform(pDRV2624, &wavesetting);
930                         }else{
931                                 dev_dbg(pDRV2624->dev, "pass cmd, prepare for read\n");
932                         }
933                 }
934                 break;  
936                 case HAPTIC_CMDID_SET_SEQUENCER:
937                 {
938                         if(len == (1+ sizeof(struct drv2624_waveform_sequencer))){
939                                 struct drv2624_waveform_sequencer sequencer;
940                                 memcpy(&sequencer, &p_kBuf[1], 
941                                         sizeof(struct drv2624_waveform_sequencer));
942                                 ret = drv2624_set_waveform(pDRV2624, &sequencer);
943                         }else{
944                                 dev_dbg(pDRV2624->dev, "pass cmd, prepare for read\n");
945                         }
946                 }
947                 break;  
948                 
949                 case HAPTIC_CMDID_PLAY_EFFECT_SEQUENCE:
950                 {
951                         drv2624_stop(pDRV2624); 
952                         drv2624_enableIRQ(pDRV2624, NO);        
953                         ret = drv2624_playEffect(pDRV2624);
954                         if(ret < 0) drv2624_disableIRQ(pDRV2624);
955                         break;
956                 }
957                                 
958                 default:
959                         dev_err(pDRV2624->dev, 
960                         "%s, unknown cmd\n", __FUNCTION__);
961                         break;
962     }
963         
964 err:
965         if(p_kBuf != NULL)
966                 kfree(p_kBuf);
967         
968     mutex_unlock(&pDRV2624->lock);
970     return len;
973 static struct file_operations fops =
975         .owner = THIS_MODULE,
976         .read = drv2624_file_read,
977         .write = drv2624_file_write,
978         .unlocked_ioctl = drv2624_file_unlocked_ioctl,
979         .open = drv2624_file_open,
980         .release = drv2624_file_release,
981 };
983 static struct miscdevice drv2624_misc =
985         .minor = MISC_DYNAMIC_MINOR,
986         .name = HAPTICS_DEVICE_NAME,
987         .fops = &fops,
988 };
989  
990 static int Haptics_init(struct drv2624_data *pDRV2624)
992     int ret = 0;
993         
994         pDRV2624->to_dev.name = "vibrator";
995         pDRV2624->to_dev.get_time = vibrator_get_time;
996         pDRV2624->to_dev.enable = vibrator_enable;
998         ret = timed_output_dev_register(&(pDRV2624->to_dev));
999     if ( ret < 0){
1000         dev_err(pDRV2624->dev, 
1001                         "drv2624: fail to create timed output dev\n");
1002         return ret;
1003     }
1004         
1005         ret = misc_register(&drv2624_misc);
1006         if (ret) {
1007                 dev_err(pDRV2624->dev, 
1008                         "drv2624 misc fail: %d\n", ret);
1009                 return ret;
1010         }
1011         
1012     hrtimer_init(&pDRV2624->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1013     pDRV2624->timer.function = vibrator_timer_func;
1014     INIT_WORK(&pDRV2624->vibrator_work, vibrator_work_routine);
1015     
1016     wake_lock_init(&pDRV2624->wklock, WAKE_LOCK_SUSPEND, "vibrator");
1017     mutex_init(&pDRV2624->lock);
1018         
1019     return 0;
1022 static void dev_init_platform_data(struct drv2624_data *pDRV2624)
1024         struct drv2624_platform_data *pDrv2624Platdata = &pDRV2624->msPlatData;
1025         struct actuator_data actuator = pDrv2624Platdata->msActuator;
1026         unsigned char value_temp = 0;
1027         unsigned char mask_temp = 0;
1028         
1029         drv2624_set_bits(pDRV2624, 
1030                 DRV2624_REG_MODE, PINFUNC_MASK, (PINFUNC_INT<<PINFUNC_SHIFT));
1031         
1032         if((actuator.mnActuatorType == ERM)||
1033                 (actuator.mnActuatorType == LRA)){
1034                 mask_temp |= ACTUATOR_MASK;
1035                 value_temp |= (actuator.mnActuatorType << ACTUATOR_SHIFT);
1036         }
1037         
1038         if((pDrv2624Platdata->mnLoop == CLOSE_LOOP)||
1039                 (pDrv2624Platdata->mnLoop == OPEN_LOOP)){
1040                 mask_temp |= LOOP_MASK;
1041                 value_temp |= (pDrv2624Platdata->mnLoop << LOOP_SHIFT);
1042         }
1043         
1044         if(value_temp != 0){
1045                 drv2624_set_bits(pDRV2624, 
1046                         DRV2624_REG_CONTROL1, 
1047                         mask_temp|AUTOBRK_OK_MASK, value_temp|AUTOBRK_OK_ENABLE);
1048         }
1049         
1050         value_temp = 0;
1051         if(actuator.mnActuatorType == ERM)
1052                 value_temp = LIB_ERM;
1053         else if(actuator.mnActuatorType == LRA)
1054                 value_temp = LIB_LRA;
1055         if(value_temp != 0){
1056                 drv2624_set_bits(pDRV2624, 
1057                         DRV2624_REG_CONTROL2, LIB_MASK, value_temp<<LIB_SHIFT);
1058         }
1059                 
1060         if(actuator.mnRatedVoltage != 0){
1061                 drv2624_reg_write(pDRV2624, 
1062                         DRV2624_REG_RATED_VOLTAGE, actuator.mnRatedVoltage);
1063         }else{
1064                 dev_err(pDRV2624->dev, 
1065                         "%s, ERROR Rated ZERO\n", __FUNCTION__);
1066         }
1068         if(actuator.mnOverDriveClampVoltage != 0){
1069                 drv2624_reg_write(pDRV2624, 
1070                         DRV2624_REG_OVERDRIVE_CLAMP, actuator.mnOverDriveClampVoltage);
1071         }else{
1072                 dev_err(pDRV2624->dev,
1073                         "%s, ERROR OverDriveVol ZERO\n", __FUNCTION__);
1074         }
1075                 
1076         if(actuator.mnActuatorType == LRA){
1077                 unsigned char DriveTime = 5*(1000 - actuator.mnLRAFreq)/actuator.mnLRAFreq;
1078                 unsigned short openLoopPeriod = 
1079                         (unsigned short)((unsigned int)1000000000 / (24619 * actuator.mnLRAFreq)); 
1080                         
1081                 if(actuator.mnLRAFreq < 125) 
1082                         DriveTime |= (MINFREQ_SEL_45HZ << MINFREQ_SEL_SHIFT);
1083                 drv2624_set_bits(pDRV2624, 
1084                         DRV2624_REG_DRIVE_TIME, 
1085                         DRIVE_TIME_MASK | MINFREQ_SEL_MASK, DriveTime); 
1086                 drv2624_set_bits(pDRV2624, 
1087                         DRV2624_REG_OL_PERIOD_H, 0x03, (openLoopPeriod&0x0300)>>8);                     
1088                 drv2624_reg_write(pDRV2624, 
1089                         DRV2624_REG_OL_PERIOD_L, (openLoopPeriod&0x00ff));
1090                 
1091                 dev_info(pDRV2624->dev,
1092                         "%s, LRA = %d, DriveTime=0x%x\n", 
1093                         __FUNCTION__, actuator.mnLRAFreq, DriveTime);
1094         }       
1097 static irqreturn_t drv2624_irq_handler(int irq, void *dev_id)
1099         struct drv2624_data *pDRV2624 = (struct drv2624_data *)dev_id;
1100         
1101         pDRV2624->mnWorkMode |= WORK_IRQ;
1102         schedule_work(&pDRV2624->vibrator_work);
1103         return IRQ_HANDLED;
1106 static int drv2624_parse_dt(struct device *dev,
1107                         struct drv2624_data *pDRV2624)
1109         struct device_node *np = dev->of_node;
1110         struct drv2624_platform_data *pPlatData = &pDRV2624->msPlatData;
1111         int rc= 0, ret = 0;
1112         unsigned int value;
1113         
1114         pPlatData->mnGpioNRST = of_get_named_gpio(np, "ti,reset-gpio", 0);
1115         if (pPlatData->mnGpioNRST < 0) {
1116                 dev_err(pDRV2624->dev, "Looking up %s property in node %s failed %d\n",
1117                         "ti,reset-gpio", np->full_name,
1118                         pPlatData->mnGpioNRST);
1119                 ret = -1;
1120         }else{
1121                 dev_dbg(pDRV2624->dev, "ti,reset-gpio=%d\n", pPlatData->mnGpioNRST);
1122         }
1123         
1124         if(ret >=0){
1125                 pPlatData->mnGpioINT = of_get_named_gpio(np, "ti,irq-gpio", 0);
1126                 if (pPlatData->mnGpioINT < 0) {
1127                         dev_err(pDRV2624->dev, "Looking up %s property in node %s failed %d\n",
1128                                 "ti,irq-gpio", np->full_name,
1129                                 pPlatData->mnGpioINT);
1130                         ret = -1;
1131                 }else{
1132                         dev_dbg(pDRV2624->dev, "ti,irq-gpio=%d\n", pPlatData->mnGpioINT);
1133                 }
1134         }
1135         
1136         if(ret >=0){
1137                 rc = of_property_read_u32(np, "ti,smart-loop", &value);
1138                 if (rc) {
1139                         dev_err(pDRV2624->dev, "Looking up %s property in node %s failed %d\n",
1140                                 "ti,smart-loop", np->full_name, rc);
1141                         ret = -2;
1142                 }else{
1143                         pPlatData->mnLoop = value&0x01;
1144                         dev_dbg(pDRV2624->dev, 
1145                                 "ti,smart-loop=%d\n", pPlatData->mnLoop);
1146                 }
1147         }       
1148         
1149         if(ret >=0){
1150                 rc = of_property_read_u32(np, "ti,actuator", &value);
1151                 if (rc) {
1152                         dev_err(pDRV2624->dev, "Looking up %s property in node %s failed %d\n",
1153                                 "ti,actuator", np->full_name, rc);
1154                         ret = -2;
1155                 }else{
1156                         pPlatData->msActuator.mnActuatorType = value&0x01;
1157                         dev_dbg(pDRV2624->dev, 
1158                                 "ti,actuator=%d\n", pPlatData->msActuator.mnActuatorType);
1159                 }
1160         }
1161                 
1162         if(ret >=0){
1163                 rc = of_property_read_u32(np, "ti,rated-voltage", &value);
1164                 if (rc) {
1165                         dev_err(pDRV2624->dev, "Looking up %s property in node %s failed %d\n",
1166                                 "ti,rated-voltage", np->full_name, rc);
1167                         ret = -2;
1168                 }else{
1169                         pPlatData->msActuator.mnRatedVoltage = value;
1170                         dev_dbg(pDRV2624->dev, 
1171                                 "ti,rated-voltage=0x%x\n", pPlatData->msActuator.mnRatedVoltage);
1172                 }
1173         }
1174         
1175         if(ret >=0){
1176                 rc = of_property_read_u32(np, "ti,odclamp-voltage", &value);
1177                 if (rc) {
1178                         dev_err(pDRV2624->dev, "Looking up %s property in node %s failed %d\n",
1179                                 "ti,odclamp-voltage", np->full_name, rc);
1180                         ret = -2;
1181                 }else{
1182                         pPlatData->msActuator.mnOverDriveClampVoltage = value;
1183                         dev_dbg(pDRV2624->dev, 
1184                                 "ti,odclamp-voltage=0x%x\n", 
1185                                 pPlatData->msActuator.mnOverDriveClampVoltage);
1186                 }
1187         }
1188         
1189         if((ret >=0)&&(pPlatData->msActuator.mnActuatorType == LRA)){
1190                 rc = of_property_read_u32(np, "ti,lra-frequency", &value);
1191                 if (rc) {
1192                         dev_err(pDRV2624->dev, "Looking up %s property in node %s failed %d\n",
1193                                 "ti,lra-frequency", np->full_name, rc);
1194                         ret = -3;
1195                 }else{
1196                         if((value >= 45) &&(value <= 300)){
1197                                 pPlatData->msActuator.mnLRAFreq = value;
1198                                 dev_dbg(pDRV2624->dev, 
1199                                         "ti,lra-frequency=%d\n", 
1200                                         pPlatData->msActuator.mnLRAFreq);
1201                         }else{
1202                                 ret = -1;
1203                                 dev_err(pDRV2624->dev, 
1204                                         "ERROR, ti,lra-frequency=%d, out of range\n", 
1205                                         pPlatData->msActuator.mnLRAFreq);                               
1206                         }
1207                 }
1208         }
1209         
1210         return ret;
1213 static struct regmap_config drv2624_i2c_regmap = {
1214         .reg_bits = 8,
1215         .val_bits = 8,
1216         .cache_type = REGCACHE_NONE,
1217 };
1219 static int drv2624_i2c_probe(struct i2c_client* client, const struct i2c_device_id* id)
1221         struct drv2624_data *pDRV2624;
1222         int err = 0;
1224         dev_info(&client->dev, "%s enter\n", __FUNCTION__);
1225         
1226         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
1227         {
1228                 dev_err(&client->dev, "%s:I2C check failed\n", __FUNCTION__);
1229                 return -ENODEV;
1230         }
1232         pDRV2624 = devm_kzalloc(&client->dev, sizeof(struct drv2624_data), GFP_KERNEL);
1233         if (pDRV2624 == NULL){
1234                 dev_err(&client->dev, "%s:no memory\n", __FUNCTION__);
1235                 return -ENOMEM;
1236         }
1238         pDRV2624->dev = &client->dev;
1239         i2c_set_clientdata(client,pDRV2624);
1240         dev_set_drvdata(&client->dev, pDRV2624);
1241         
1242         pDRV2624->mpRegmap = devm_regmap_init_i2c(client, &drv2624_i2c_regmap);
1243         if (IS_ERR(pDRV2624->mpRegmap)) {
1244                 err = PTR_ERR(pDRV2624->mpRegmap);
1245                 dev_err(pDRV2624->dev, 
1246                         "%s:Failed to allocate register map: %d\n",__FUNCTION__,err);
1247                 return err;
1248         }
1250         if (client->dev.of_node){
1251                 dev_dbg(pDRV2624->dev, "of node parse\n");
1252                 err = drv2624_parse_dt(&client->dev, pDRV2624);
1253         }else if(client->dev.platform_data){
1254                 dev_dbg(pDRV2624->dev, "platform data parse\n");
1255                 memcpy(&pDRV2624->msPlatData, 
1256                         client->dev.platform_data, sizeof(struct drv2624_platform_data));
1257         }else{
1258                 dev_err(pDRV2624->dev, 
1259                         "%s: ERROR no platform data\n",__FUNCTION__);
1260                 return -1;
1261         }
1262         
1263         if((err < 0)
1264                 ||(pDRV2624->msPlatData.mnGpioNRST <= 0)
1265                 ||(pDRV2624->msPlatData.mnGpioINT <= 0)){
1266                 dev_err(pDRV2624->dev, 
1267                         "%s: platform data error\n",__FUNCTION__);
1268                 return -1;              
1269         }
1270                 
1271         if(pDRV2624->msPlatData.mnGpioNRST){
1272                 err = gpio_request(pDRV2624->msPlatData.mnGpioNRST,"DRV2624-NRST");
1273                 if(err < 0){
1274                         dev_err(pDRV2624->dev, 
1275                                 "%s: GPIO %d request NRST error\n", 
1276                                 __FUNCTION__, pDRV2624->msPlatData.mnGpioNRST);                         
1277                         return err;
1278                 }
1279                 
1280                 gpio_direction_output(pDRV2624->msPlatData.mnGpioNRST, 0);
1281                 mdelay(5);
1282                 gpio_direction_output(pDRV2624->msPlatData.mnGpioNRST, 1);
1283                 mdelay(2);
1284         }
1286         mutex_init(&pDRV2624->dev_lock);
1287         err = drv2624_reg_read(pDRV2624, DRV2624_REG_ID);
1288         if(err < 0){
1289                 dev_err(pDRV2624->dev, 
1290                         "%s, i2c bus fail (%d)\n", __FUNCTION__, err);
1291                 goto exit_gpio_request_failed1;
1292         }else{
1293                 dev_info(pDRV2624->dev, 
1294                         "%s, ID status (0x%x)\n", __FUNCTION__, err);
1295                 pDRV2624->mnDeviceID = err;
1296         }
1297         
1298         if((pDRV2624->mnDeviceID&0xf0) != DRV2624_ID){
1299                 dev_err(pDRV2624->dev, 
1300                         "%s, device_id(0x%x) fail\n",
1301                         __FUNCTION__, pDRV2624->mnDeviceID);
1302                 goto exit_gpio_request_failed1;
1303         }
1304         
1305         dev_init_platform_data(pDRV2624);
1307         if(pDRV2624->msPlatData.mnGpioINT){
1308                 err = gpio_request(pDRV2624->msPlatData.mnGpioINT, "DRV2624-IRQ");
1309                 if(err < 0){
1310                         dev_err(pDRV2624->dev, 
1311                                 "%s: GPIO %d request INT error\n", 
1312                                 __FUNCTION__, pDRV2624->msPlatData.mnGpioINT);                                  
1313                         goto exit_gpio_request_failed1;
1314                 }
1316                 gpio_direction_input(pDRV2624->msPlatData.mnGpioINT);
1318                 pDRV2624->mnIRQ = gpio_to_irq(pDRV2624->msPlatData.mnGpioINT);
1319                 dev_dbg(pDRV2624->dev, "irq = %d \n", pDRV2624->mnIRQ);
1320                 
1321                 err = request_threaded_irq(pDRV2624->mnIRQ, drv2624_irq_handler,
1322                                 NULL, IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1323                                 client->name, pDRV2624);
1325                 if (err < 0) {
1326                         dev_err(pDRV2624->dev, 
1327                                 "request_irq failed, %d\n", err);                                                       
1328                         goto exit_gpio_request_failed2;
1329                 }
1330                 drv2624_disableIRQ(pDRV2624);
1331         }
1333         g_DRV2624data = pDRV2624;
1334         
1335     Haptics_init(pDRV2624);
1337         request_firmware_nowait(THIS_MODULE, 
1338                                                 FW_ACTION_HOTPLUG,
1339                                                 "drv2624.bin",
1340                                                 &(client->dev), 
1341                                                 GFP_KERNEL, 
1342                                                 pDRV2624, 
1343                                                 drv2624_firmware_load); 
1344                 
1345     dev_info(pDRV2624->dev, 
1346                 "drv2624 probe succeeded\n");
1348     return 0;
1350 exit_gpio_request_failed2:
1351         if(pDRV2624->msPlatData.mnGpioINT > 0){
1352                 gpio_free(pDRV2624->msPlatData.mnGpioINT);
1353         }
1354         
1355 exit_gpio_request_failed1:      
1356         if(pDRV2624->msPlatData.mnGpioNRST > 0){
1357                 gpio_free(pDRV2624->msPlatData.mnGpioNRST);
1358         }
1360         mutex_destroy(&pDRV2624->dev_lock);
1361         
1362     dev_err(pDRV2624->dev, 
1363                 "%s failed, err=%d\n",
1364                 __FUNCTION__, err);
1365         return err;
1368 static int drv2624_i2c_remove(struct i2c_client* client)
1370         struct drv2624_data *pDRV2624 = i2c_get_clientdata(client);
1372         if(pDRV2624->msPlatData.mnGpioNRST)
1373                 gpio_free(pDRV2624->msPlatData.mnGpioNRST);
1375         if(pDRV2624->msPlatData.mnGpioINT)
1376                 gpio_free(pDRV2624->msPlatData.mnGpioINT);
1378         misc_deregister(&drv2624_misc);
1380         mutex_destroy(&pDRV2624->lock); 
1381         mutex_destroy(&pDRV2624->dev_lock);
1382         
1383     return 0;
1386 static const struct i2c_device_id drv2624_i2c_id[] = {
1387         {"drv2624", 0},
1388         {}
1389 };
1391 MODULE_DEVICE_TABLE(i2c, drv2624_i2c_id);
1393 #if defined(CONFIG_OF)
1394 static const struct of_device_id drv2624_of_match[] = {
1395         {.compatible = "ti,drv2624"},
1396         {},
1397 };
1399 MODULE_DEVICE_TABLE(of, drv2624_of_match);
1400 #endif
1402 static struct i2c_driver drv2624_i2c_driver = {
1403         .driver = {
1404                         .name = "drv2624",
1405                         .owner = THIS_MODULE,
1406 #if defined(CONFIG_OF)
1407                         .of_match_table = of_match_ptr(drv2624_of_match),
1408 #endif
1409                 },
1410         .probe = drv2624_i2c_probe,
1411         .remove = drv2624_i2c_remove,
1412         .id_table = drv2624_i2c_id,
1413 };
1415 module_i2c_driver(drv2624_i2c_driver);
1417 MODULE_AUTHOR("Texas Instruments Inc.");
1418 MODULE_DESCRIPTION("DRV2624 I2C Smart Haptics driver");
1419 MODULE_LICENSE("GPLv2");