input: sensors: fromdos and remove trailing whitespace
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / sensor-dev.c
1 /* drivers/input/sensors/sensor-dev.c - handle all gsensor in this file
2  *
3  * Copyright (C) 2012-2015 ROCKCHIP.
4  * Author: luowei <lw@rock-chips.com>
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
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  */
16
17 #include <linux/interrupt.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/irq.h>
21 #include <linux/miscdevice.h>
22 #include <linux/gpio.h>
23 #include <asm/uaccess.h>
24 #include <asm/atomic.h>
25 #include <linux/delay.h>
26 #include <linux/input.h>
27 #include <linux/workqueue.h>
28 #include <linux/freezer.h>
29 #include <linux/proc_fs.h>
30 #include <linux/gpio.h>
31 #include <linux/of_gpio.h>
32 #include <linux/of.h>
33 #ifdef CONFIG_HAS_EARLYSUSPEND
34 #include <linux/earlysuspend.h>
35 #endif
36 #include <linux/l3g4200d.h>
37 #include <linux/sensor-dev.h>
38 #include <linux/module.h>
39 #ifdef CONFIG_COMPAT
40 #include <linux/compat.h>
41 #endif
42
43
44 /*
45 sensor-dev.c v1.1 add pressure and temperature support 2013-2-27
46 sensor-dev.c v1.2 add akm8963 support 2013-3-10
47 sensor-dev.c v1.3 add sensor debug support 2013-3-15
48 sensor-dev.c v1.4 add angle calculation support between two gsensors 2013-09-01
49 */
50
51 #define SENSOR_VERSION_AND_TIME  "sensor-dev.c v1.4 add angle calculation support between two gsensors 2013-09-01"
52
53
54 struct sensor_private_data *g_sensor[SENSOR_NUM_TYPES];
55 static struct sensor_operate *sensor_ops[SENSOR_NUM_ID];
56 static struct class *g_sensor_class[SENSOR_NUM_TYPES];
57
58 static ssize_t sensor_proc_write(struct file *file, const char __user *buffer,
59                            size_t count, loff_t *data)
60 {
61         char c;
62         int rc;
63         int i = 0, num = 0;
64
65         rc = get_user(c, buffer);
66         if (rc)
67         {
68                 for(i=SENSOR_TYPE_NULL+1; i<SENSOR_NUM_TYPES; i++)
69                 atomic_set(&g_sensor[i]->flags.debug_flag, SENSOR_TYPE_NULL);
70                 return rc;
71         }
72
73
74         num = c - '0';
75
76         printk("%s command list:close:%d,angle:%d accel:%d, compass:%d, gyro:%d, light:%d, psensor:%d, temp:%d, pressure:%d,total:%d,num=%d\n",__func__,
77
78                 SENSOR_TYPE_NULL, SENSOR_TYPE_ANGLE, SENSOR_TYPE_ACCEL,SENSOR_TYPE_COMPASS,SENSOR_TYPE_GYROSCOPE,SENSOR_TYPE_LIGHT,SENSOR_TYPE_PROXIMITY,
79
80                 SENSOR_TYPE_TEMPERATURE,SENSOR_TYPE_PRESSURE,SENSOR_NUM_TYPES,num);
81
82         if((num > SENSOR_NUM_TYPES) || (num < SENSOR_TYPE_NULL))
83         {
84                 printk("%s:error! only support %d to %d\n",__func__, SENSOR_TYPE_NULL,SENSOR_NUM_TYPES);
85                 return -1;
86         }
87
88         for(i=SENSOR_TYPE_NULL+1; i<SENSOR_NUM_TYPES; i++)
89         {
90                 if(g_sensor[i])
91                 atomic_set(&g_sensor[i]->flags.debug_flag, num);
92         }
93
94         return count;
95 }
96
97 static const struct file_operations sensor_proc_fops = {
98         .owner          = THIS_MODULE,
99         .write          = sensor_proc_write,
100 };
101
102
103
104 static int sensor_get_id(struct i2c_client *client, int *value)
105 {
106         struct sensor_private_data *sensor =
107             (struct sensor_private_data *) i2c_get_clientdata(client);
108         int result = 0;
109         char temp = sensor->ops->id_reg;
110         int i = 0;
111
112         if(sensor->ops->id_reg >= 0)
113         {
114                 for(i=0; i<3; i++)
115                 {
116                         result = sensor_rx_data(client, &temp, 1);
117                         *value = temp;
118                         if(!result)
119                         break;
120                 }
121
122                 if(result)
123                         return result;
124
125                 if(*value != sensor->ops->id_data)
126                 {
127                         printk("%s:id=0x%x is not 0x%x\n",__func__,*value, sensor->ops->id_data);
128                         result = -1;
129                 }
130
131                 DBG("%s:devid=0x%x\n",__func__,*value);
132         }
133
134         return result;
135 }
136
137 static int sensor_initial(struct i2c_client *client)
138 {
139         struct sensor_private_data *sensor =
140             (struct sensor_private_data *) i2c_get_clientdata(client);
141         int result = 0;
142
143         //register setting according to chip datasheet
144         result = sensor->ops->init(client);
145         if(result < 0)
146         {
147                 printk("%s:fail to init sensor\n",__func__);
148                 return result;
149         }
150
151
152         DBG("%s:ctrl_data=0x%x\n",__func__,sensor->ops->ctrl_data);
153
154         return result;
155
156 }
157
158 static int sensor_chip_init(struct i2c_client *client)
159 {
160         struct sensor_private_data *sensor =
161             (struct sensor_private_data *) i2c_get_clientdata(client);
162         struct sensor_operate *ops = sensor_ops[(int)sensor->i2c_id->driver_data];
163         int result = 0;
164
165         if(ops)
166         {
167                 sensor->ops = ops;
168         }
169         else
170         {
171                 printk("%s:ops is null,sensor name is %s\n",__func__,sensor->i2c_id->name);
172                 result = -1;
173                 goto error;
174         }
175
176         if((sensor->type != ops->type) || ((int)sensor->i2c_id->driver_data != ops->id_i2c))
177         {
178                 printk("%s:type or id is different:type=%d,%d,id=%d,%d\n",__func__,sensor->type, ops->type, (int)sensor->i2c_id->driver_data, ops->id_i2c);
179                 result = -1;
180                 goto error;
181         }
182
183         if(!ops->init || !ops->active || !ops->report)
184         {
185                 printk("%s:error:some function is needed\n",__func__);
186                 result = -1;
187                 goto error;
188         }
189
190         result = sensor_get_id(sensor->client, &sensor->devid);//get id
191         if(result < 0)
192         {
193                 printk("%s:fail to read %s devid:0x%x\n",__func__, sensor->i2c_id->name, sensor->devid);
194                 goto error;
195         }
196
197         printk("%s:%s:devid=0x%x,ops=0x%p\n",__func__, sensor->i2c_id->name, sensor->devid,sensor->ops);
198
199         result = sensor_initial(sensor->client);        //init sensor
200         if(result < 0)
201         {
202                 printk("%s:fail to init sensor\n",__func__);
203                 goto error;
204         }
205
206         return 0;
207
208 error:
209
210         return result;
211 }
212
213 static int sensor_reset_rate(struct i2c_client *client, int rate)
214 {
215         struct sensor_private_data *sensor =
216             (struct sensor_private_data *) i2c_get_clientdata(client);
217         int result = 0;
218
219         result = sensor->ops->active(client,SENSOR_OFF,rate);
220         sensor->ops->init(client);
221         result = sensor->ops->active(client,SENSOR_ON,rate);
222
223         return result;
224 }
225
226 static int sensor_get_data(struct i2c_client *client)
227 {
228         struct sensor_private_data *sensor =
229             (struct sensor_private_data *) i2c_get_clientdata(client);
230         int result = 0;
231
232         result = sensor->ops->report(client);
233         if(result)
234                 goto error;
235
236         /* set data_ready */
237         atomic_set(&sensor->data_ready, 1);
238         /*wake up data_ready  work queue*/
239         wake_up(&sensor->data_ready_wq);
240
241 error:
242         return result;
243 }
244
245 #if 0
246 int sensor_get_cached_data(struct i2c_client* client, char *buffer, int length, struct sensor_axis *axis)
247 {
248     struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(client);
249     wait_event_interruptible_timeout(sensor->data_ready_wq,
250                                      atomic_read(&(sensor->data_ready) ),
251                                      msecs_to_jiffies(1000) );
252     if ( 0 == atomic_read(&(sensor->data_ready) ) ) {
253         printk("waiting 'data_ready_wq' timed out.");
254         goto error;
255     }
256
257
258         mutex_lock(&sensor->data_mutex);
259
260         switch(sensor->type)
261         {
262                 case SENSOR_TYPE_ACCEL:
263                 *axis = sensor->axis;
264                 break;
265
266                 case SENSOR_TYPE_COMPASS:
267                 memcpy(buffer, sensor->sensor_data, length);
268                 break;
269         }
270
271         mutex_unlock(&sensor->data_mutex);
272
273     return 0;
274
275 error:
276         return -1;
277 }
278 #endif
279
280 static void  sensor_delaywork_func(struct work_struct *work)
281 {
282         struct delayed_work *delaywork = container_of(work, struct delayed_work, work);
283         struct sensor_private_data *sensor = container_of(delaywork, struct sensor_private_data, delaywork);
284         struct i2c_client *client = sensor->client;
285
286         mutex_lock(&sensor->sensor_mutex);
287         if (sensor_get_data(client) < 0)
288                 DBG(KERN_ERR "%s: Get data failed\n",__func__);
289
290         if(!sensor->pdata->irq_enable)//restart work while polling
291         schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
292         //else
293         //{
294                 //if((sensor->ops->trig == IRQF_TRIGGER_LOW) || (sensor->ops->trig == IRQF_TRIGGER_HIGH))
295                 //enable_irq(sensor->client->irq);
296         //}
297         mutex_unlock(&sensor->sensor_mutex);
298
299         DBG("%s:%s\n",__func__,sensor->i2c_id->name);
300 }
301
302 /*
303  * This is a threaded IRQ handler so can access I2C/SPI.  Since all
304  * interrupts are clear on read the IRQ line will be reasserted and
305  * the physical IRQ will be handled again if another interrupt is
306  * asserted while we run - in the normal course of events this is a
307  * rare occurrence so we save I2C/SPI reads.  We're also assuming that
308  * it's rare to get lots of interrupts firing simultaneously so try to
309  * minimise I/O.
310  */
311 static irqreturn_t sensor_interrupt(int irq, void *dev_id)
312 {
313         struct sensor_private_data *sensor = (struct sensor_private_data *)dev_id;
314
315         //use threaded IRQ
316         if (sensor_get_data(sensor->client) < 0)
317                 DBG(KERN_ERR "%s: Get data failed\n",__func__);
318         msleep(sensor->pdata->poll_delay_ms);
319
320
321         //if((sensor->ops->trig == IRQF_TRIGGER_LOW) || (sensor->ops->trig == IRQF_TRIGGER_HIGH))
322         //disable_irq_nosync(irq);
323         //schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
324         DBG("%s:irq=%d\n",__func__,irq);
325         return IRQ_HANDLED;
326 }
327
328
329 static int sensor_irq_init(struct i2c_client *client)
330 {
331         struct sensor_private_data *sensor =
332             (struct sensor_private_data *) i2c_get_clientdata(client);
333         int result = 0;
334         int irq;
335         if((sensor->pdata->irq_enable)&&(sensor->pdata->irq_flags!= SENSOR_UNKNOW_DATA))
336         {
337                 //INIT_DELAYED_WORK(&sensor->delaywork, sensor_delaywork_func);
338                 if(sensor->pdata->poll_delay_ms < 0)
339                         sensor->pdata->poll_delay_ms = 30;
340                 result = gpio_request(client->irq, sensor->i2c_id->name);
341                 if (result)
342                 {
343                         printk("%s:fail to request gpio :%d\n",__func__,client->irq);
344                 }
345
346                 //gpio_pull_updown(client->irq, PullEnable);
347                 irq = gpio_to_irq(client->irq);
348                 //result = request_irq(irq, sensor_interrupt, sensor->ops->trig, sensor->ops->name, sensor);
349                 //result = request_threaded_irq(irq, NULL, sensor_interrupt, sensor->ops->trig, sensor->ops->name, sensor);
350                 result = devm_request_threaded_irq(&client->dev, irq, NULL, sensor_interrupt, sensor->pdata->irq_flags | IRQF_ONESHOT, sensor->ops->name, sensor);
351                 if (result) {
352                         printk(KERN_ERR "%s:fail to request irq = %d, ret = 0x%x\n",__func__, irq, result);
353                         goto error;
354                 }
355                 client->irq = irq;
356                 if((sensor->pdata->type == SENSOR_TYPE_GYROSCOPE) || (sensor->pdata->type == SENSOR_TYPE_ACCEL) || (sensor->pdata->type == SENSOR_TYPE_ANGLE))
357                 disable_irq_nosync(client->irq);//disable irq
358                 if(((sensor->pdata->type == SENSOR_TYPE_LIGHT) || (sensor->pdata->type == SENSOR_TYPE_PROXIMITY))&& (!(sensor->ops->trig & IRQF_SHARED)))
359                 disable_irq_nosync(client->irq);//disable irq
360                 if(((sensor->pdata->type == SENSOR_TYPE_TEMPERATURE) || (sensor->pdata->type == SENSOR_TYPE_PRESSURE))&& (!(sensor->ops->trig & IRQF_SHARED)))
361                 disable_irq_nosync(client->irq);//disable irq
362                 DBG("%s:use irq=%d\n",__func__,irq);
363         }
364         else if(!sensor->pdata->irq_enable)
365         {
366                 INIT_DELAYED_WORK(&sensor->delaywork, sensor_delaywork_func);
367                 if(sensor->pdata->poll_delay_ms < 0)
368                         sensor->pdata->poll_delay_ms = 30;
369
370                 DBG("%s:use polling,delay=%d ms\n",__func__,sensor->pdata->poll_delay_ms);
371         }
372
373 error:
374         return result;
375 }
376
377 #ifdef CONFIG_HAS_EARLYSUSPEND
378 static void sensor_suspend(struct early_suspend *h)
379 {
380         struct sensor_private_data *sensor =
381                         container_of(h, struct sensor_private_data, early_suspend);
382
383         if(sensor->ops->suspend)
384                 sensor->ops->suspend(sensor->client);
385
386 }
387
388 static void sensor_resume(struct early_suspend *h)
389 {
390         struct sensor_private_data *sensor =
391                         container_of(h, struct sensor_private_data, early_suspend);
392
393         if(sensor->ops->resume)
394                 sensor->ops->resume(sensor->client);
395 }
396 #endif
397
398 #ifdef CONFIG_PM
399 static int sensor_of_suspend(struct device *dev)
400 {
401         struct sensor_private_data *sensor = dev_get_drvdata(dev);
402
403         if (sensor->ops->suspend)
404                 sensor->ops->suspend(sensor->client);
405
406         return 0;
407 }
408
409 static int sensor_of_resume(struct device *dev)
410 {
411         struct sensor_private_data *sensor = dev_get_drvdata(dev);
412
413         if (sensor->ops->resume)
414                 sensor->ops->resume(sensor->client);
415         if (sensor->pdata->power_off_in_suspend)
416                 sensor_initial(sensor->client);
417
418         return 0;
419 }
420
421 static const struct dev_pm_ops sensor_pm_ops = {
422         SET_SYSTEM_SLEEP_PM_OPS(sensor_of_suspend, sensor_of_resume)
423 };
424
425 #define SENSOR_PM_OPS (&sensor_pm_ops)
426 #else
427 #define SENSOR_PM_OPS NULL
428 #endif
429
430 static int angle_dev_open(struct inode *inode, struct file *file)
431 {
432         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ACCEL];
433         //struct i2c_client *client = sensor->client;
434
435         int result = 0;
436
437
438         return result;
439 }
440
441
442 static int angle_dev_release(struct inode *inode, struct file *file)
443 {
444         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ANGLE];
445         //struct i2c_client *client = sensor->client;
446
447         int result = 0;
448
449
450         return result;
451 }
452
453 /* ioctl - I/O control */
454 static long angle_dev_ioctl(struct file *file,
455                           unsigned int cmd, unsigned long arg)
456 {
457         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ANGLE];
458         struct i2c_client *client = sensor->client;
459         void __user *argp = (void __user *)arg;
460         struct sensor_axis axis = {0};
461         char rate;
462         int result = 0;
463
464         switch (cmd) {
465         case GSENSOR_IOCTL_APP_SET_RATE:
466                 if (copy_from_user(&rate, argp, sizeof(rate)))
467                 {
468                         result = -EFAULT;
469                         goto error;
470                 }
471                 break;
472         default:
473                 break;
474         }
475
476         switch (cmd) {
477         case GSENSOR_IOCTL_START:
478                 DBG("%s:GSENSOR_IOCTL_START start,status=%d\n", __func__,sensor->status_cur);
479                 mutex_lock(&sensor->operation_mutex);
480                 if(++sensor->start_count == 1)
481                 {
482                         if(sensor->status_cur == SENSOR_OFF)
483                         {
484                                 atomic_set(&(sensor->data_ready), 0);
485                                 if ( (result = sensor->ops->active(client, 1, 0) ) < 0 ) {
486                                         mutex_unlock(&sensor->operation_mutex);
487                                         printk("%s:fail to active sensor,ret=%d\n",__func__,result);
488                                         goto error;
489                                 }
490                                 if(sensor->pdata->irq_enable)
491                                 {
492                                         DBG("%s:enable irq,irq=%d\n",__func__,client->irq);
493                                         enable_irq(client->irq);        //enable irq
494                                 }
495                                 else
496                                 {
497                                         schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
498                                 }
499                                 sensor->status_cur = SENSOR_ON;
500                         }
501                 }
502                 mutex_unlock(&sensor->operation_mutex);
503                 DBG("%s:GSENSOR_IOCTL_START OK\n", __func__);
504                 break;
505
506         case GSENSOR_IOCTL_CLOSE:
507                 DBG("%s:GSENSOR_IOCTL_CLOSE start,status=%d\n", __func__,sensor->status_cur);
508                 mutex_lock(&sensor->operation_mutex);
509                 if(--sensor->start_count == 0)
510                 {
511                         if(sensor->status_cur == SENSOR_ON)
512                         {
513                                 atomic_set(&(sensor->data_ready), 0);
514                                 if ( (result = sensor->ops->active(client, 0, 0) ) < 0 ) {
515                                         mutex_unlock(&sensor->operation_mutex);
516                                         goto error;
517                                 }
518
519                                 if(sensor->pdata->irq_enable)
520                                 {
521                                         DBG("%s:disable irq,irq=%d\n",__func__,client->irq);
522                                         disable_irq_nosync(client->irq);//disable irq
523                                 }
524                                 else
525                                 cancel_delayed_work_sync(&sensor->delaywork);
526                                 sensor->status_cur = SENSOR_OFF;
527                         }
528
529                         DBG("%s:GSENSOR_IOCTL_CLOSE OK\n", __func__);
530                 }
531
532                 mutex_unlock(&sensor->operation_mutex);
533                 break;
534
535         case GSENSOR_IOCTL_APP_SET_RATE:
536                 DBG("%s:GSENSOR_IOCTL_APP_SET_RATE start\n", __func__);
537                 mutex_lock(&sensor->operation_mutex);
538                 result = sensor_reset_rate(client, rate);
539                 if (result < 0){
540                         mutex_unlock(&sensor->operation_mutex);
541                         goto error;
542                 }
543
544                 sensor->status_cur = SENSOR_ON;
545                 mutex_unlock(&sensor->operation_mutex);
546                 DBG("%s:GSENSOR_IOCTL_APP_SET_RATE OK\n", __func__);
547                 break;
548
549         case GSENSOR_IOCTL_GETDATA:
550                 mutex_lock(&sensor->data_mutex);
551                 memcpy(&axis, &sensor->axis, sizeof(sensor->axis));     //get data from buffer
552                 mutex_unlock(&sensor->data_mutex);
553                 break;
554         default:
555                 result = -ENOTTY;
556         goto error;
557         }
558
559         switch (cmd) {
560         case GSENSOR_IOCTL_GETDATA:
561                 if ( copy_to_user(argp, &axis, sizeof(axis) ) ) {
562                     printk("failed to copy sense data to user space.");
563                                 result = -EFAULT;
564                                 goto error;
565                 }
566                 DBG("%s:GSENSOR_IOCTL_GETDATA OK\n", __func__);
567                 break;
568         default:
569                 break;
570         }
571
572 error:
573         return result;
574 }
575
576
577 static int gsensor_dev_open(struct inode *inode, struct file *file)
578 {
579         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ACCEL];
580         //struct i2c_client *client = sensor->client;
581
582         int result = 0;
583
584
585         return result;
586 }
587
588
589 static int gsensor_dev_release(struct inode *inode, struct file *file)
590 {
591         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ACCEL];
592         //struct i2c_client *client = sensor->client;
593
594         int result = 0;
595
596
597         return result;
598 }
599
600 /* ioctl - I/O control */
601 static long gsensor_dev_ioctl(struct file *file,
602                           unsigned int cmd, unsigned long arg)
603 {
604         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ACCEL];
605         struct i2c_client *client = sensor->client;
606         void __user *argp = (void __user *)arg;
607         struct sensor_axis axis = {0};
608         char rate;
609         int result = 0;
610
611         switch (cmd) {
612         case GSENSOR_IOCTL_APP_SET_RATE:
613                 if (copy_from_user(&rate, argp, sizeof(rate)))
614                 {
615                         result = -EFAULT;
616                         goto error;
617                 }
618                 break;
619         default:
620                 break;
621         }
622
623         switch (cmd) {
624         case GSENSOR_IOCTL_START:
625                 DBG("%s:GSENSOR_IOCTL_START start,status=%d\n", __func__,sensor->status_cur);
626                 mutex_lock(&sensor->operation_mutex);
627                 if(++sensor->start_count == 1)
628                 {
629                         if(sensor->status_cur == SENSOR_OFF)
630                         {
631                                 atomic_set(&(sensor->data_ready), 0);
632                                 if ( (result = sensor->ops->active(client, 1, 0) ) < 0 ) {
633                                         mutex_unlock(&sensor->operation_mutex);
634                                         printk("%s:fail to active sensor,ret=%d\n",__func__,result);
635                                         goto error;
636                                 }
637                                 if(sensor->pdata->irq_enable)
638                                 {
639                                         DBG("%s:enable irq,irq=%d\n",__func__,client->irq);
640                                         enable_irq(client->irq);        //enable irq
641                                 }
642                                 else
643                                 {
644                                         schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
645                                 }
646                                 sensor->status_cur = SENSOR_ON;
647                         }
648                 }
649                 mutex_unlock(&sensor->operation_mutex);
650                 DBG("%s:GSENSOR_IOCTL_START OK\n", __func__);
651                 break;
652
653         case GSENSOR_IOCTL_CLOSE:
654                 DBG("%s:GSENSOR_IOCTL_CLOSE start,status=%d\n", __func__,sensor->status_cur);
655                 mutex_lock(&sensor->operation_mutex);
656                 if(--sensor->start_count == 0)
657                 {
658                         if(sensor->status_cur == SENSOR_ON)
659                         {
660                                 atomic_set(&(sensor->data_ready), 0);
661                                 if ( (result = sensor->ops->active(client, 0, 0) ) < 0 ) {
662                                         mutex_unlock(&sensor->operation_mutex);
663                                         goto error;
664                                 }
665
666                                 if(sensor->pdata->irq_enable)
667                                 {
668                                         DBG("%s:disable irq,irq=%d\n",__func__,client->irq);
669                                         disable_irq_nosync(client->irq);//disable irq
670                                 }
671                                 else
672                                 cancel_delayed_work_sync(&sensor->delaywork);
673                                 sensor->status_cur = SENSOR_OFF;
674                         }
675
676                         DBG("%s:GSENSOR_IOCTL_CLOSE OK\n", __func__);
677                 }
678
679                 mutex_unlock(&sensor->operation_mutex);
680                 break;
681
682         case GSENSOR_IOCTL_APP_SET_RATE:
683                 DBG("%s:GSENSOR_IOCTL_APP_SET_RATE start\n", __func__);
684                 mutex_lock(&sensor->operation_mutex);
685                 result = sensor_reset_rate(client, rate);
686                 if (result < 0){
687                         mutex_unlock(&sensor->operation_mutex);
688                         goto error;
689                 }
690
691                 sensor->status_cur = SENSOR_ON;
692                 mutex_unlock(&sensor->operation_mutex);
693                 DBG("%s:GSENSOR_IOCTL_APP_SET_RATE OK\n", __func__);
694                 break;
695
696         case GSENSOR_IOCTL_GETDATA:
697                 mutex_lock(&sensor->data_mutex);
698                 memcpy(&axis, &sensor->axis, sizeof(sensor->axis));     //get data from buffer
699                 mutex_unlock(&sensor->data_mutex);
700                 break;
701         default:
702                 result = -ENOTTY;
703         goto error;
704         }
705
706         switch (cmd) {
707         case GSENSOR_IOCTL_GETDATA:
708                 if ( copy_to_user(argp, &axis, sizeof(axis) ) ) {
709                     printk("failed to copy sense data to user space.");
710                                 result = -EFAULT;
711                                 goto error;
712                 }
713                 DBG("%s:GSENSOR_IOCTL_GETDATA OK\n", __func__);
714                 break;
715         default:
716                 break;
717         }
718
719 error:
720         return result;
721 }
722
723 static ssize_t gsensor_set_orientation_online(struct class *class,
724                 struct class_attribute *attr, const char *buf, size_t count)
725 {
726         int i=0;
727         char orientation[20];
728         char *tmp;
729
730         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ACCEL];
731         struct sensor_platform_data *pdata = sensor->pdata;
732
733
734         char *p = strstr(buf,"gsensor_class");
735         int start = strcspn(p,"{");
736         int end = strcspn(p,"}");
737
738         strncpy(orientation,p+start,end-start+1);
739         tmp = orientation;
740
741
742         while(strncmp(tmp,"}",1)!=0)
743          {
744                 if((strncmp(tmp,",",1)==0)||(strncmp(tmp,"{",1)==0))
745                 {
746
747                          tmp++;
748                          continue;
749                 }
750                 else if(strncmp(tmp,"-",1)==0)
751                 {
752                         pdata->orientation[i++]=-1;
753                         DBG("i=%d,data=%d\n",i,pdata->orientation[i]);
754                          tmp++;
755                 }
756                 else
757                 {
758                         pdata->orientation[i++]=tmp[0]-48;
759                         DBG("----i=%d,data=%d\n",i,pdata->orientation[i]);
760                 }
761                 tmp++;
762
763
764          }
765
766         for(i=0;i<9;i++)
767                 DBG("i=%d gsensor_info=%d\n",i,pdata->orientation[i]);
768         return 0;
769
770 }
771
772 static CLASS_ATTR(orientation, 0660, NULL, gsensor_set_orientation_online);
773
774 static int  gsensor_class_init(void)
775 {
776         int ret ;
777         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_ACCEL];
778         g_sensor_class[SENSOR_TYPE_ACCEL] = class_create(THIS_MODULE, "gsensor_class");
779         ret =  class_create_file(g_sensor_class[SENSOR_TYPE_ACCEL], &class_attr_orientation);
780         if (ret)
781         {
782                 printk("%s:Fail to creat class\n",__func__);
783                 return ret;
784         }
785         printk("%s:%s\n",__func__,sensor->i2c_id->name);
786         return 0;
787 }
788
789
790
791 static int compass_dev_open(struct inode *inode, struct file *file)
792 {
793         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_COMPASS];
794         //struct i2c_client *client = sensor->client;
795
796         int result = 0;
797         int flag = 0;
798         flag = atomic_read(&sensor->flags.open_flag);
799         if(!flag)
800         {
801                 atomic_set(&sensor->flags.open_flag, 1);
802                 wake_up(&sensor->flags.open_wq);
803         }
804
805         DBG("%s\n", __func__);
806         return result;
807 }
808
809
810
811 static int compass_dev_release(struct inode *inode, struct file *file)
812 {
813         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_COMPASS];
814         //struct i2c_client *client = sensor->client;
815         //void __user *argp = (void __user *)arg;
816         int result = 0;
817         int flag = 0;
818         flag = atomic_read(&sensor->flags.open_flag);
819         if(flag)
820         {
821                 atomic_set(&sensor->flags.open_flag, 0);
822                 wake_up(&sensor->flags.open_wq);
823         }
824
825         DBG("%s\n", __func__);
826         return result;
827 }
828
829 #ifdef CONFIG_COMPAT
830 /* ioctl - I/O control */
831 static long compass_dev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
832 {
833         void __user *arg64 = compat_ptr(arg);
834         int result = 0;
835
836         if (!file->f_op || !file->f_op->unlocked_ioctl) {
837                 pr_err("file->f_op or file->f_op->unlocked_ioctl is null\n");
838                 return -ENOTTY;
839         }
840
841         switch (cmd) {
842         case COMPAT_ECS_IOCTL_APP_SET_MFLAG:
843                 if (file->f_op->unlocked_ioctl)
844                         result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_SET_MFLAG, (unsigned long)arg64);
845                 break;
846         case COMPAT_ECS_IOCTL_APP_GET_MFLAG:
847                 if (file->f_op->unlocked_ioctl)
848                         result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_GET_MFLAG, (unsigned long)arg64);
849                 break;
850         case COMPAT_ECS_IOCTL_APP_SET_AFLAG:
851                 if (file->f_op->unlocked_ioctl)
852                         result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_SET_AFLAG, (unsigned long)arg64);
853                 break;
854         case COMPAT_ECS_IOCTL_APP_GET_AFLAG:
855                 if (file->f_op->unlocked_ioctl)
856                         result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_GET_AFLAG, (unsigned long)arg64);
857                 break;
858         case COMPAT_ECS_IOCTL_APP_SET_MVFLAG:
859                 if (file->f_op->unlocked_ioctl)
860                         result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_SET_MVFLAG, (unsigned long)arg64);
861                 break;
862         case COMPAT_ECS_IOCTL_APP_GET_MVFLAG:
863                 if (file->f_op->unlocked_ioctl)
864                         result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_GET_MVFLAG, (unsigned long)arg64);
865                 break;
866         case COMPAT_ECS_IOCTL_APP_SET_DELAY:
867                 if (file->f_op->unlocked_ioctl)
868                         result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_SET_DELAY, (unsigned long)arg64);
869                 break;
870         case COMPAT_ECS_IOCTL_APP_GET_DELAY:
871                 if (file->f_op->unlocked_ioctl)
872                         result = file->f_op->unlocked_ioctl(file, ECS_IOCTL_APP_GET_DELAY, (unsigned long)arg64);
873                 break;
874         default:
875                 break;
876         }
877
878         return result;
879 }
880 #endif
881
882
883 /* ioctl - I/O control */
884 static long compass_dev_ioctl(struct file *file,
885                           unsigned int cmd, unsigned long arg)
886 {
887         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_COMPASS];
888         //struct i2c_client *client = sensor->client;
889         void __user *argp = (void __user *)arg;
890         int result = 0;
891         short flag;
892
893         switch (cmd) {
894                 case ECS_IOCTL_APP_SET_MFLAG:
895                 case ECS_IOCTL_APP_SET_AFLAG:
896                 case ECS_IOCTL_APP_SET_MVFLAG:
897                         if (copy_from_user(&flag, argp, sizeof(flag))) {
898                                 return -EFAULT;
899                         }
900                         if (flag < 0 || flag > 1) {
901                                 return -EINVAL;
902                         }
903                         break;
904                 case ECS_IOCTL_APP_SET_DELAY:
905                         if (copy_from_user(&flag, argp, sizeof(flag))) {
906                                 return -EFAULT;
907                         }
908                         break;
909                 default:
910                         break;
911         }
912
913         switch (cmd) {
914                 case ECS_IOCTL_APP_SET_MFLAG:
915                         atomic_set(&sensor->flags.m_flag, flag);
916                         DBG("%s:ECS_IOCTL_APP_SET_MFLAG,flag=%d\n", __func__,flag);
917                         break;
918                 case ECS_IOCTL_APP_GET_MFLAG:
919                         flag = atomic_read(&sensor->flags.m_flag);
920                         DBG("%s:ECS_IOCTL_APP_GET_MFLAG,flag=%d\n", __func__,flag);
921                         break;
922                 case ECS_IOCTL_APP_SET_AFLAG:
923                         atomic_set(&sensor->flags.a_flag, flag);
924                         DBG("%s:ECS_IOCTL_APP_SET_AFLAG,flag=%d\n", __func__,flag);
925                         break;
926                 case ECS_IOCTL_APP_GET_AFLAG:
927                         flag = atomic_read(&sensor->flags.a_flag);
928                         DBG("%s:ECS_IOCTL_APP_GET_AFLAG,flag=%d\n", __func__,flag);
929                         break;
930                 case ECS_IOCTL_APP_SET_MVFLAG:
931                         atomic_set(&sensor->flags.mv_flag, flag);
932                         DBG("%s:ECS_IOCTL_APP_SET_MVFLAG,flag=%d\n", __func__,flag);
933                         break;
934                 case ECS_IOCTL_APP_GET_MVFLAG:
935                         flag = atomic_read(&sensor->flags.mv_flag);
936                         DBG("%s:ECS_IOCTL_APP_GET_MVFLAG,flag=%d\n", __func__,flag);
937                         break;
938                 case ECS_IOCTL_APP_SET_DELAY:
939                         sensor->flags.delay = flag;
940                         break;
941                 case ECS_IOCTL_APP_GET_DELAY:
942                         flag = sensor->flags.delay;
943                         break;
944                 default:
945                         return -ENOTTY;
946         }
947
948         switch (cmd) {
949                 case ECS_IOCTL_APP_GET_MFLAG:
950                 case ECS_IOCTL_APP_GET_AFLAG:
951                 case ECS_IOCTL_APP_GET_MVFLAG:
952                 case ECS_IOCTL_APP_GET_DELAY:
953                         if (copy_to_user(argp, &flag, sizeof(flag))) {
954                                 return -EFAULT;
955                         }
956                         break;
957                 default:
958                         break;
959         }
960
961         return result;
962 }
963
964 static int gyro_dev_open(struct inode *inode, struct file *file)
965 {
966         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_GYROSCOPE];
967         //struct i2c_client *client = sensor->client;
968
969         int result = 0;
970
971
972         return result;
973 }
974
975
976 static int gyro_dev_release(struct inode *inode, struct file *file)
977 {
978         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_GYROSCOPE];
979         //struct i2c_client *client = sensor->client;
980
981         int result = 0;
982
983
984         return result;
985 }
986
987
988 /* ioctl - I/O control */
989 static long gyro_dev_ioctl(struct file *file,
990                           unsigned int cmd, unsigned long arg)
991 {
992         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_GYROSCOPE];
993         struct i2c_client *client = sensor->client;
994         void __user *argp = (void __user *)arg;
995         int result = 0;
996         char rate;
997         switch (cmd) {
998         case L3G4200D_IOCTL_GET_ENABLE:
999                 result = !sensor->status_cur;
1000                 if (copy_to_user(argp, &result, sizeof(result)))
1001                 {
1002                         printk("%s:failed to copy status to user space.\n",__FUNCTION__);
1003                         return -EFAULT;
1004                 }
1005
1006                 DBG("%s :L3G4200D_IOCTL_GET_ENABLE,status=%d\n",__FUNCTION__,result);
1007                 break;
1008         case L3G4200D_IOCTL_SET_ENABLE:
1009                 DBG("%s :L3G4200D_IOCTL_SET_ENABLE,flag=%d\n",__FUNCTION__,*(unsigned int *)argp);
1010                 mutex_lock(&sensor->operation_mutex);
1011                 if(*(unsigned int *)argp)
1012                 {
1013                         if(sensor->status_cur == SENSOR_OFF)
1014                         {
1015                                 if ( (result = sensor->ops->active(client, 1, ODR100_BW12_5) ) < 0 ) {
1016                                 mutex_unlock(&sensor->operation_mutex);
1017                                 printk("%s:fail to active sensor,ret=%d\n",__func__,result);
1018                                 goto error;
1019                                 }
1020                                 if(sensor->pdata->irq_enable)
1021                                 {
1022                                         DBG("%s:enable irq,irq=%d\n",__func__,client->irq);
1023                                         enable_irq(client->irq);        //enable irq
1024                                 }
1025                                 else
1026                                 {
1027                                         schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
1028                                 }
1029                                 sensor->status_cur = SENSOR_ON;
1030                         }
1031                 }
1032                 else
1033                 {
1034                         if(sensor->status_cur == SENSOR_ON)
1035                         {
1036                                 if ( (result = sensor->ops->active(client, 0, 0) ) < 0 ) {
1037                                 mutex_unlock(&sensor->operation_mutex);
1038                                 goto error;
1039                                 }
1040
1041                                 if(sensor->pdata->irq_enable)
1042                                 {
1043                                         DBG("%s:disable irq,irq=%d\n",__func__,client->irq);
1044                                         disable_irq_nosync(client->irq);//disable irq
1045                                 }
1046                                 else
1047                                 cancel_delayed_work_sync(&sensor->delaywork);
1048                                 sensor->status_cur = SENSOR_OFF;
1049                         }
1050                 }
1051
1052                 result = sensor->status_cur;
1053                 if (copy_to_user(argp, &result, sizeof(result)))
1054                 {
1055                         mutex_unlock(&sensor->operation_mutex);
1056                         printk("%s:failed to copy sense data to user space.\n",__FUNCTION__);
1057                         return -EFAULT;
1058                 }
1059
1060                 mutex_unlock(&sensor->operation_mutex);
1061                 DBG("%s:L3G4200D_IOCTL_SET_ENABLE OK\n", __func__);
1062                 break;
1063         case L3G4200D_IOCTL_SET_DELAY:
1064                 if (copy_from_user(&rate, argp, sizeof(rate)))
1065                 return -EFAULT;
1066                 mutex_lock(&sensor->operation_mutex);
1067                 if(sensor->status_cur == SENSOR_OFF)
1068                 {
1069                         if ( (result = sensor->ops->active(client, 1, rate) ) < 0 ) {
1070                         mutex_unlock(&sensor->operation_mutex);
1071                         printk("%s:fail to active sensor,ret=%d\n",__func__,result);
1072                         goto error;
1073                         }
1074
1075                         if(sensor->pdata->irq_enable)
1076                         {
1077                                 DBG("%s:enable irq,irq=%d\n",__func__,client->irq);
1078                                 enable_irq(client->irq);        //enable irq
1079                         }
1080                         else
1081                         {
1082                                 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
1083                         }
1084                         sensor->status_cur = SENSOR_ON;
1085                 }
1086
1087                 mutex_unlock(&sensor->operation_mutex);
1088                 DBG("%s :L3G4200D_IOCTL_SET_DELAY,rate=%d\n",__FUNCTION__,rate);
1089                 break;
1090
1091         default:
1092                 printk("%s:error,cmd=0x%x\n",__func__,cmd);
1093                 return -ENOTTY;
1094         }
1095
1096         DBG("%s:line=%d,cmd=0x%x\n",__func__,__LINE__,cmd);
1097
1098 error:
1099         return result;
1100 }
1101
1102 static int light_dev_open(struct inode *inode, struct file *file)
1103 {
1104         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_LIGHT];
1105         //struct i2c_client *client = sensor->client;
1106         int result = 0;
1107
1108
1109         return result;
1110 }
1111
1112
1113
1114
1115 static int light_dev_release(struct inode *inode, struct file *file)
1116 {
1117         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_LIGHT];
1118         //struct i2c_client *client = sensor->client;
1119         int result = 0;
1120
1121
1122         return result;
1123 }
1124
1125 #ifdef CONFIG_COMPAT
1126 static long light_dev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1127 {
1128         long ret = 0;
1129         void __user *arg64 = compat_ptr(arg);
1130
1131         if (!file->f_op || !file->f_op->unlocked_ioctl) {
1132                 pr_err("[DEBUG] file->f_op or file->f_op->unlocked_ioctl is null\n");
1133                 return -ENOTTY;
1134         }
1135
1136         switch (cmd) {
1137         case COMPAT_LIGHTSENSOR_IOCTL_GET_ENABLED:
1138                 if (file->f_op->unlocked_ioctl)
1139                         ret = file->f_op->unlocked_ioctl(file, LIGHTSENSOR_IOCTL_GET_ENABLED, (unsigned long)arg64);
1140                 break;
1141         case COMPAT_LIGHTSENSOR_IOCTL_ENABLE:
1142                 if (file->f_op->unlocked_ioctl)
1143                         ret = file->f_op->unlocked_ioctl(file, LIGHTSENSOR_IOCTL_ENABLE, (unsigned long)arg64);
1144                 break;
1145         default:
1146                 break;
1147         }
1148
1149         return ret;
1150 }
1151 #endif
1152
1153 /* ioctl - I/O control */
1154 static long light_dev_ioctl(struct file *file,
1155                           unsigned int cmd, unsigned long arg)
1156 {
1157         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_LIGHT];
1158         struct i2c_client *client = sensor->client;
1159         unsigned int *argp = (unsigned int *)arg;
1160         int result = 0;
1161
1162         switch(cmd)
1163         {
1164                 case LIGHTSENSOR_IOCTL_GET_ENABLED:
1165                         *argp = sensor->status_cur;
1166                         break;
1167                 case LIGHTSENSOR_IOCTL_ENABLE:
1168                         DBG("%s:LIGHTSENSOR_IOCTL_ENABLE start\n", __func__);
1169                         mutex_lock(&sensor->operation_mutex);
1170                         if(*(unsigned int *)argp)
1171                         {
1172                                 if(sensor->status_cur == SENSOR_OFF)
1173                                 {
1174                                         if ( (result = sensor->ops->active(client, SENSOR_ON, 0) ) < 0 ) {
1175                                         mutex_unlock(&sensor->operation_mutex);
1176                                         printk("%s:fail to active sensor,ret=%d\n",__func__,result);
1177                                         goto error;
1178                                         }
1179                                         if(sensor->pdata->irq_enable)
1180                                         {
1181                                                 if(!(sensor->ops->trig & IRQF_SHARED))
1182                                                 {
1183                                                         DBG("%s:enable irq,irq=%d\n",__func__,client->irq);
1184                                                         enable_irq(client->irq);        //enable irq
1185                                                 }
1186                                         }
1187                                         else
1188                                         {
1189                                                 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
1190                                         }
1191
1192                                         sensor->status_cur = SENSOR_ON;
1193                                 }
1194                         }
1195                         else
1196                         {
1197                                 if(sensor->status_cur == SENSOR_ON)
1198                                 {
1199                                         if ( (result = sensor->ops->active(client, SENSOR_OFF, 0) ) < 0 ) {
1200                                         mutex_unlock(&sensor->operation_mutex);
1201                                         goto error;
1202                                         }
1203
1204                                         if(sensor->pdata->irq_enable)
1205                                         {
1206                                                 if(!(sensor->ops->trig & IRQF_SHARED))
1207                                                 {
1208                                                         DBG("%s:disable irq,irq=%d\n",__func__,client->irq);
1209                                                         disable_irq_nosync(client->irq);//disable irq
1210                                                 }
1211                                         }
1212                                         else
1213                                         cancel_delayed_work_sync(&sensor->delaywork);
1214
1215                                         sensor->status_cur = SENSOR_OFF;
1216                                 }
1217                         }
1218                         mutex_unlock(&sensor->operation_mutex);
1219                         DBG("%s:LIGHTSENSOR_IOCTL_ENABLE OK\n", __func__);
1220                         break;
1221
1222                 default:
1223                         break;
1224         }
1225
1226 error:
1227         return result;
1228 }
1229
1230
1231 static int proximity_dev_open(struct inode *inode, struct file *file)
1232 {
1233         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_PROXIMITY];
1234         //struct i2c_client *client = sensor->client;
1235         int result = 0;
1236
1237
1238         return result;
1239 }
1240
1241
1242 static int proximity_dev_release(struct inode *inode, struct file *file)
1243 {
1244         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_PROXIMITY];
1245         //struct i2c_client *client = sensor->client;
1246         int result = 0;
1247
1248
1249         return result;
1250 }
1251
1252 #ifdef CONFIG_COMPAT
1253 static long proximity_dev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1254 {
1255         long ret = 0;
1256         void __user *arg64 = compat_ptr(arg);
1257
1258         if (!file->f_op || !file->f_op->unlocked_ioctl) {
1259                 pr_err("file->f_op or file->f_op->unlocked_ioctl is null\n");
1260                 return -ENOTTY;
1261         }
1262
1263         switch (cmd) {
1264         case COMPAT_PSENSOR_IOCTL_GET_ENABLED:
1265                 if (file->f_op->unlocked_ioctl)
1266                         ret = file->f_op->unlocked_ioctl(file, PSENSOR_IOCTL_GET_ENABLED, (unsigned long)arg64);
1267                 break;
1268         case COMPAT_PSENSOR_IOCTL_ENABLE:
1269                 if (file->f_op->unlocked_ioctl)
1270                         ret = file->f_op->unlocked_ioctl(file, PSENSOR_IOCTL_ENABLE, (unsigned long)arg64);
1271                 break;
1272         default:
1273                 break;
1274         }
1275
1276         return ret;
1277 }
1278 #endif
1279
1280 /* ioctl - I/O control */
1281 static long proximity_dev_ioctl(struct file *file,
1282                           unsigned int cmd, unsigned long arg)
1283 {
1284         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_PROXIMITY];
1285         struct i2c_client *client = sensor->client;
1286         unsigned int *argp = (unsigned int *)arg;
1287         int result = 0;
1288         switch(cmd)
1289         {
1290                 case PSENSOR_IOCTL_GET_ENABLED:
1291                         *argp = sensor->status_cur;
1292                         break;
1293                 case PSENSOR_IOCTL_ENABLE:
1294                         DBG("%s:PSENSOR_IOCTL_ENABLE start\n", __func__);
1295                         mutex_lock(&sensor->operation_mutex);
1296                         if(*(unsigned int *)argp)
1297                         {
1298                                 if(sensor->status_cur == SENSOR_OFF)
1299                                 {
1300                                         if ( (result = sensor->ops->active(client, SENSOR_ON, 0) ) < 0 ) {
1301                                         mutex_unlock(&sensor->operation_mutex);
1302                                         printk("%s:fail to active sensor,ret=%d\n",__func__,result);
1303                                         goto error;
1304                                         }
1305
1306                                         if(sensor->pdata->irq_enable)
1307                                         {
1308                                                 if(!(sensor->ops->trig & IRQF_SHARED))
1309                                                 {
1310                                                         DBG("%s:enable irq,irq=%d\n",__func__,client->irq);
1311                                                         enable_irq(client->irq);        //enable irq
1312                                                 }
1313                                         }
1314                                         else
1315                                         {
1316                                                 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
1317                                         }
1318
1319                                         sensor->status_cur = SENSOR_ON;
1320                                 }
1321                         }
1322                         else
1323                         {
1324                                 if(sensor->status_cur == SENSOR_ON)
1325                                 {
1326                                         if ( (result = sensor->ops->active(client, SENSOR_OFF, 0) ) < 0 ) {
1327                                         mutex_unlock(&sensor->operation_mutex);
1328                                         goto error;
1329                                         }
1330                                         if(sensor->pdata->irq_enable)
1331                                         {
1332                                                 if(!(sensor->ops->trig & IRQF_SHARED))
1333                                                 {
1334                                                         DBG("%s:disable irq,irq=%d\n",__func__,client->irq);
1335                                                         disable_irq_nosync(client->irq);//disable irq
1336                                                 }
1337                                         }
1338                                         else
1339                                         cancel_delayed_work_sync(&sensor->delaywork);
1340                                         sensor->status_cur = SENSOR_OFF;
1341                                 }
1342                         }
1343                         mutex_unlock(&sensor->operation_mutex);
1344                         DBG("%s:PSENSOR_IOCTL_ENABLE OK\n", __func__);
1345                         break;
1346
1347                 default:
1348                         break;
1349         }
1350
1351 error:
1352         return result;
1353 }
1354
1355 static int temperature_dev_open(struct inode *inode, struct file *file)
1356 {
1357         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_TEMPERATURE];
1358         //struct i2c_client *client = sensor->client;
1359
1360         int result = 0;
1361
1362
1363         return result;
1364 }
1365
1366
1367 static int temperature_dev_release(struct inode *inode, struct file *file)
1368 {
1369         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_TEMPERATURE];
1370         //struct i2c_client *client = sensor->client;
1371
1372         int result = 0;
1373
1374
1375         return result;
1376 }
1377
1378
1379 /* ioctl - I/O control */
1380 static long temperature_dev_ioctl(struct file *file,
1381                           unsigned int cmd, unsigned long arg)
1382 {
1383         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_TEMPERATURE];
1384         struct i2c_client *client = sensor->client;
1385         unsigned int *argp = (unsigned int *)arg;
1386         int result = 0;
1387
1388         switch(cmd)
1389         {
1390                 case TEMPERATURE_IOCTL_GET_ENABLED:
1391                         *argp = sensor->status_cur;
1392                         break;
1393                 case TEMPERATURE_IOCTL_ENABLE:
1394                         DBG("%s:LIGHTSENSOR_IOCTL_ENABLE start\n", __func__);
1395                         mutex_lock(&sensor->operation_mutex);
1396                         if(*(unsigned int *)argp)
1397                         {
1398                                 if(sensor->status_cur == SENSOR_OFF)
1399                                 {
1400                                         if ( (result = sensor->ops->active(client, SENSOR_ON, 0) ) < 0 ) {
1401                                         mutex_unlock(&sensor->operation_mutex);
1402                                         printk("%s:fail to active sensor,ret=%d\n",__func__,result);
1403                                         goto error;
1404                                         }
1405                                         if(sensor->pdata->irq_enable)
1406                                         {
1407                                                 if(!(sensor->ops->trig & IRQF_SHARED))
1408                                                 {
1409                                                         DBG("%s:enable irq,irq=%d\n",__func__,client->irq);
1410                                                         enable_irq(client->irq);        //enable irq
1411                                                 }
1412                                         }
1413                                         else
1414                                         {
1415                                                 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
1416                                         }
1417
1418                                         sensor->status_cur = SENSOR_ON;
1419                                 }
1420                         }
1421                         else
1422                         {
1423                                 if(sensor->status_cur == SENSOR_ON)
1424                                 {
1425                                         if ( (result = sensor->ops->active(client, SENSOR_OFF, 0) ) < 0 ) {
1426                                         mutex_unlock(&sensor->operation_mutex);
1427                                         goto error;
1428                                         }
1429
1430                                         if(sensor->pdata->irq_enable)
1431                                         {
1432                                                 if(!(sensor->ops->trig & IRQF_SHARED))
1433                                                 {
1434                                                         DBG("%s:disable irq,irq=%d\n",__func__,client->irq);
1435                                                         disable_irq_nosync(client->irq);//disable irq
1436                                                 }
1437                                         }
1438                                         else
1439                                         cancel_delayed_work_sync(&sensor->delaywork);
1440
1441                                         sensor->status_cur = SENSOR_OFF;
1442                                 }
1443                         }
1444                         mutex_unlock(&sensor->operation_mutex);
1445                         DBG("%s:LIGHTSENSOR_IOCTL_ENABLE OK\n", __func__);
1446                         break;
1447
1448                 default:
1449                         break;
1450         }
1451
1452 error:
1453         return result;
1454 }
1455
1456
1457 static int pressure_dev_open(struct inode *inode, struct file *file)
1458 {
1459         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_PRESSURE];
1460         //struct i2c_client *client = sensor->client;
1461
1462         int result = 0;
1463
1464
1465         return result;
1466 }
1467
1468
1469 static int pressure_dev_release(struct inode *inode, struct file *file)
1470 {
1471         //struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_PRESSURE];
1472         //struct i2c_client *client = sensor->client;
1473
1474         int result = 0;
1475
1476
1477         return result;
1478 }
1479
1480
1481 /* ioctl - I/O control */
1482 static long pressure_dev_ioctl(struct file *file,
1483                           unsigned int cmd, unsigned long arg)
1484 {
1485         struct sensor_private_data *sensor = g_sensor[SENSOR_TYPE_PRESSURE];
1486         struct i2c_client *client = sensor->client;
1487         unsigned int *argp = (unsigned int *)arg;
1488         int result = 0;
1489
1490         switch(cmd)
1491         {
1492                 case PRESSURE_IOCTL_GET_ENABLED:
1493                         *argp = sensor->status_cur;
1494                         break;
1495                 case PRESSURE_IOCTL_ENABLE:
1496                         DBG("%s:LIGHTSENSOR_IOCTL_ENABLE start\n", __func__);
1497                         mutex_lock(&sensor->operation_mutex);
1498                         if(*(unsigned int *)argp)
1499                         {
1500                                 if(sensor->status_cur == SENSOR_OFF)
1501                                 {
1502                                         if ( (result = sensor->ops->active(client, SENSOR_ON, 0) ) < 0 ) {
1503                                         mutex_unlock(&sensor->operation_mutex);
1504                                         printk("%s:fail to active sensor,ret=%d\n",__func__,result);
1505                                         goto error;
1506                                         }
1507                                         if(sensor->pdata->irq_enable)
1508                                         {
1509                                                 if(!(sensor->ops->trig & IRQF_SHARED))
1510                                                 {
1511                                                         DBG("%s:enable irq,irq=%d\n",__func__,client->irq);
1512                                                         enable_irq(client->irq);        //enable irq
1513                                                 }
1514                                         }
1515                                         else
1516                                         {
1517                                                 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
1518                                         }
1519
1520                                         sensor->status_cur = SENSOR_ON;
1521                                 }
1522                         }
1523                         else
1524                         {
1525                                 if(sensor->status_cur == SENSOR_ON)
1526                                 {
1527                                         if ( (result = sensor->ops->active(client, SENSOR_OFF, 0) ) < 0 ) {
1528                                         mutex_unlock(&sensor->operation_mutex);
1529                                         goto error;
1530                                         }
1531
1532                                         if(sensor->pdata->irq_enable)
1533                                         {
1534                                                 if(!(sensor->ops->trig & IRQF_SHARED))
1535                                                 {
1536                                                         DBG("%s:disable irq,irq=%d\n",__func__,client->irq);
1537                                                         disable_irq_nosync(client->irq);//disable irq
1538                                                 }
1539                                         }
1540                                         else
1541                                         cancel_delayed_work_sync(&sensor->delaywork);
1542
1543                                         sensor->status_cur = SENSOR_OFF;
1544                                 }
1545                         }
1546                         mutex_unlock(&sensor->operation_mutex);
1547                         DBG("%s:LIGHTSENSOR_IOCTL_ENABLE OK\n", __func__);
1548                         break;
1549
1550                 default:
1551                         break;
1552         }
1553
1554 error:
1555         return result;
1556 }
1557
1558
1559
1560
1561 static int sensor_misc_device_register(struct sensor_private_data *sensor, int type)
1562 {
1563         int result = 0;
1564
1565         switch(type)
1566         {
1567                 case SENSOR_TYPE_ANGLE:
1568                         if(!sensor->ops->misc_dev)
1569                         {
1570                                 sensor->fops.owner = THIS_MODULE;
1571                                 sensor->fops.unlocked_ioctl = angle_dev_ioctl;
1572                                 sensor->fops.open = angle_dev_open;
1573                                 sensor->fops.release = angle_dev_release;
1574
1575                                 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1576                                 sensor->miscdev.name = "angle";
1577                                 sensor->miscdev.fops = &sensor->fops;
1578                         }
1579                         else
1580                         {
1581                                 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1582
1583                         }
1584
1585                         break;
1586
1587                 case SENSOR_TYPE_ACCEL:
1588                         if(!sensor->ops->misc_dev)
1589                         {
1590                                 sensor->fops.owner = THIS_MODULE;
1591                                 sensor->fops.unlocked_ioctl = gsensor_dev_ioctl;
1592                 #ifdef CONFIG_COMPAT
1593                                 sensor->fops.compat_ioctl = gsensor_dev_ioctl;
1594                                 #endif
1595                                 sensor->fops.open = gsensor_dev_open;
1596                                 sensor->fops.release = gsensor_dev_release;
1597
1598                                 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1599                                 sensor->miscdev.name = "mma8452_daemon";
1600                                 sensor->miscdev.fops = &sensor->fops;
1601                         }
1602                         else
1603                         {
1604                                 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1605
1606                         }
1607
1608                         break;
1609
1610                 case SENSOR_TYPE_COMPASS:
1611                         if(!sensor->ops->misc_dev)
1612                         {
1613                                 sensor->fops.owner = THIS_MODULE;
1614                                 sensor->fops.unlocked_ioctl = compass_dev_ioctl;
1615 #ifdef CONFIG_COMPAT
1616                                 sensor->fops.compat_ioctl = compass_dev_compat_ioctl;
1617 #endif
1618                                 sensor->fops.open = compass_dev_open;
1619                                 sensor->fops.release = compass_dev_release;
1620
1621                                 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1622                                 sensor->miscdev.name = "compass";
1623                                 sensor->miscdev.fops = &sensor->fops;
1624                         }
1625                         else
1626                         {
1627                                 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1628
1629                         }
1630
1631                         break;
1632
1633                 case SENSOR_TYPE_GYROSCOPE:
1634                         if(!sensor->ops->misc_dev)
1635                         {
1636                                 sensor->fops.owner = THIS_MODULE;
1637                                 sensor->fops.unlocked_ioctl = gyro_dev_ioctl;
1638                                 sensor->fops.open = gyro_dev_open;
1639                                 sensor->fops.release = gyro_dev_release;
1640
1641                                 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1642                                 sensor->miscdev.name = "gyrosensor";
1643                                 sensor->miscdev.fops = &sensor->fops;
1644                         }
1645                         else
1646                         {
1647                                 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1648
1649                         }
1650
1651                         break;
1652
1653                 case SENSOR_TYPE_LIGHT:
1654                         if(!sensor->ops->misc_dev)
1655                         {
1656                                 sensor->fops.owner = THIS_MODULE;
1657                                 sensor->fops.unlocked_ioctl = light_dev_ioctl;
1658 #ifdef CONFIG_COMPAT
1659                                 sensor->fops.compat_ioctl = light_dev_compat_ioctl;
1660 #endif
1661                                 sensor->fops.open = light_dev_open;
1662                                 sensor->fops.release = light_dev_release;
1663
1664                                 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1665                                 sensor->miscdev.name = "lightsensor";
1666                                 sensor->miscdev.fops = &sensor->fops;
1667                         }
1668                         else
1669                         {
1670                                 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1671
1672                         }
1673                         break;
1674
1675                 case SENSOR_TYPE_PROXIMITY:
1676                         if(!sensor->ops->misc_dev)
1677                         {
1678                                 sensor->fops.owner = THIS_MODULE;
1679                                 sensor->fops.unlocked_ioctl = proximity_dev_ioctl;
1680 #ifdef CONFIG_COMPAT
1681                                 sensor->fops.compat_ioctl = proximity_dev_compat_ioctl;
1682 #endif
1683                                 sensor->fops.open = proximity_dev_open;
1684                                 sensor->fops.release = proximity_dev_release;
1685
1686                                 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1687                                 sensor->miscdev.name = "psensor";
1688                                 sensor->miscdev.fops = &sensor->fops;
1689                         }
1690                         else
1691                         {
1692                                 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1693
1694                         }
1695                         break;
1696
1697                 case SENSOR_TYPE_TEMPERATURE:
1698                         if(!sensor->ops->misc_dev)
1699                         {
1700                                 sensor->fops.owner = THIS_MODULE;
1701                                 sensor->fops.unlocked_ioctl = temperature_dev_ioctl;
1702                                 sensor->fops.open = temperature_dev_open;
1703                                 sensor->fops.release = temperature_dev_release;
1704
1705                                 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1706                                 sensor->miscdev.name = "temperature";
1707                                 sensor->miscdev.fops = &sensor->fops;
1708                         }
1709                         else
1710                         {
1711                                 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1712
1713                         }
1714
1715                         break;
1716
1717                 case SENSOR_TYPE_PRESSURE:
1718                         if(!sensor->ops->misc_dev)
1719                         {
1720                                 sensor->fops.owner = THIS_MODULE;
1721                                 sensor->fops.unlocked_ioctl = pressure_dev_ioctl;
1722                                 sensor->fops.open = pressure_dev_open;
1723                                 sensor->fops.release = pressure_dev_release;
1724
1725                                 sensor->miscdev.minor = MISC_DYNAMIC_MINOR;
1726                                 sensor->miscdev.name = "pressure";
1727                                 sensor->miscdev.fops = &sensor->fops;
1728                         }
1729                         else
1730                         {
1731                                 memcpy(&sensor->miscdev, sensor->ops->misc_dev, sizeof(*sensor->ops->misc_dev));
1732
1733                         }
1734
1735                         break;
1736
1737                 default:
1738                         printk("%s:unknow sensor type=%d\n",__func__,type);
1739                         result = -1;
1740                         goto error;
1741         }
1742
1743         sensor->miscdev.parent = &sensor->client->dev;
1744         result = misc_register(&sensor->miscdev);
1745         if (result < 0) {
1746                 dev_err(&sensor->client->dev,
1747                         "fail to register misc device %s\n", sensor->miscdev.name);
1748                 goto error;
1749         }
1750
1751         printk("%s:miscdevice: %s\n",__func__,sensor->miscdev.name);
1752
1753 error:
1754
1755         return result;
1756
1757 }
1758
1759 int sensor_register_slave(int type,struct i2c_client *client,
1760                         struct sensor_platform_data *slave_pdata,
1761                         struct sensor_operate *(*get_sensor_ops)(void))
1762 {
1763         int result = 0;
1764         struct sensor_operate *ops = get_sensor_ops();
1765         if((ops->id_i2c >= SENSOR_NUM_ID) || (ops->id_i2c <= ID_INVALID))
1766         {
1767                 printk("%s:%s id is error %d\n", __func__, ops->name, ops->id_i2c);
1768                 return -1;
1769         }
1770         sensor_ops[ops->id_i2c] = ops;
1771         printk("%s:%s,id=%d\n",__func__,sensor_ops[ops->id_i2c]->name, ops->id_i2c);
1772         return result;
1773 }
1774
1775
1776 int sensor_unregister_slave(int type,struct i2c_client *client,
1777                         struct sensor_platform_data *slave_pdata,
1778                         struct sensor_operate *(*get_sensor_ops)(void))
1779 {
1780         int result = 0;
1781         struct sensor_operate *ops = get_sensor_ops();
1782         if((ops->id_i2c >= SENSOR_NUM_ID) || (ops->id_i2c <= ID_INVALID))
1783         {
1784                 printk("%s:%s id is error %d\n", __func__, ops->name, ops->id_i2c);
1785                 return -1;
1786         }
1787         printk("%s:%s,id=%d\n",__func__,sensor_ops[ops->id_i2c]->name, ops->id_i2c);
1788         sensor_ops[ops->id_i2c] = NULL;
1789         return result;
1790 }
1791
1792
1793 int sensor_probe(struct i2c_client *client, const struct i2c_device_id *devid)
1794 {
1795         struct sensor_private_data *sensor =
1796             (struct sensor_private_data *) i2c_get_clientdata(client);
1797         struct sensor_platform_data *pdata;
1798         struct device_node *np = client->dev.of_node;
1799         enum of_gpio_flags rst_flags, pwr_flags;
1800         unsigned long irq_flags;
1801         int result = 0;
1802         int type = 0;
1803
1804         dev_info(&client->adapter->dev, "%s: %s,%p\n", __func__, devid->name, client);
1805
1806         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1807                 result = -ENODEV;
1808                 goto out_no_free;
1809         }
1810         if (!np) {
1811                 dev_err(&client->dev, "no device tree\n");
1812                 return -EINVAL;
1813         }
1814     pdata = devm_kzalloc(&client->dev,sizeof(*pdata), GFP_KERNEL);
1815         if (!pdata) {
1816                 result = -ENOMEM;
1817                 goto out_no_free;
1818         }
1819         sensor = devm_kzalloc(&client->dev,sizeof(*sensor), GFP_KERNEL);
1820         if (!sensor) {
1821                 result = -ENOMEM;
1822                 goto out_no_free;
1823         }
1824
1825         of_property_read_u32(np,"type",&(pdata->type));
1826
1827         pdata->irq_pin = of_get_named_gpio_flags(np, "irq-gpio", 0,(enum of_gpio_flags *)&irq_flags);
1828         pdata->reset_pin = of_get_named_gpio_flags(np, "reset-gpio",0,&rst_flags);
1829         pdata->power_pin = of_get_named_gpio_flags(np, "power-gpio",0,&pwr_flags);
1830
1831         of_property_read_u32(np,"irq_enable",&(pdata->irq_enable));
1832         of_property_read_u32(np,"poll_delay_ms",&(pdata->poll_delay_ms));
1833
1834         of_property_read_u32(np,"x_min",&(pdata->x_min));
1835         of_property_read_u32(np,"y_min",&(pdata->y_min));
1836         of_property_read_u32(np,"z_min",&(pdata->z_min));
1837         of_property_read_u32(np,"factory",&(pdata->factory));
1838         of_property_read_u32(np,"layout",&(pdata->layout));
1839
1840         of_property_read_u8(np,"address",&(pdata->address));
1841         of_get_property(np, "project_name", pdata->project_name);
1842
1843         of_property_read_u32(np, "power-off-in-suspend",
1844                              &pdata->power_off_in_suspend);
1845
1846         switch(pdata->layout)
1847         {
1848                 case 1:
1849                         pdata->orientation[0] = 1;
1850                         pdata->orientation[1] = 0;
1851                         pdata->orientation[2] = 0;
1852
1853                         pdata->orientation[3] = 0;
1854                         pdata->orientation[4] = 1;
1855                         pdata->orientation[5] = 0;
1856
1857                         pdata->orientation[6] = 0;
1858                         pdata->orientation[7] = 0;
1859                         pdata->orientation[8] = 1;
1860                         break;
1861
1862                 case 2:
1863                         pdata->orientation[0] = 0;
1864                         pdata->orientation[1] = -1;
1865                         pdata->orientation[2] = 0;
1866
1867                         pdata->orientation[3] = 1;
1868                         pdata->orientation[4] = 0;
1869                         pdata->orientation[5] = 0;
1870
1871                         pdata->orientation[6] = 0;
1872                         pdata->orientation[7] = 0;
1873                         pdata->orientation[8] = 1;
1874                         break;
1875
1876                 case 3:
1877                         pdata->orientation[0] = -1;
1878                         pdata->orientation[1] = 0;
1879                         pdata->orientation[2] = 0;
1880
1881                         pdata->orientation[3] = 0;
1882                         pdata->orientation[4] = -1;
1883                         pdata->orientation[5] = 0;
1884
1885                         pdata->orientation[6] = 0;
1886                         pdata->orientation[7] = 0;
1887                         pdata->orientation[8] = 1;
1888                         break;
1889
1890                 case 4:
1891                         pdata->orientation[0] = 0;
1892                         pdata->orientation[1] = 1;
1893                         pdata->orientation[2] = 0;
1894
1895                         pdata->orientation[3] = -1;
1896                         pdata->orientation[4] = 0;
1897                         pdata->orientation[5] = 0;
1898
1899                         pdata->orientation[6] = 0;
1900                         pdata->orientation[7] = 0;
1901                         pdata->orientation[8] = 1;
1902                         break;
1903
1904                 case 5:
1905                         pdata->orientation[0] = 1;
1906                         pdata->orientation[1] = 0;
1907                         pdata->orientation[2] = 0;
1908
1909                         pdata->orientation[3] = 0;
1910                         pdata->orientation[4] = -1;
1911                         pdata->orientation[5] = 0;
1912
1913                         pdata->orientation[6] = 0;
1914                         pdata->orientation[7] = 0;
1915                         pdata->orientation[8] = -1;
1916                         break;
1917
1918                 case 6:
1919                         pdata->orientation[0] = 0;
1920                         pdata->orientation[1] = -1;
1921                         pdata->orientation[2] = 0;
1922
1923                         pdata->orientation[3] = -1;
1924                         pdata->orientation[4] = 0;
1925                         pdata->orientation[5] = 0;
1926
1927                         pdata->orientation[6] = 0;
1928                         pdata->orientation[7] = 0;
1929                         pdata->orientation[8] = -1;
1930                         break;
1931
1932                 case 7:
1933                         pdata->orientation[0] = -1;
1934                         pdata->orientation[1] = 0;
1935                         pdata->orientation[2] = 0;
1936
1937                         pdata->orientation[3] = 0;
1938                         pdata->orientation[4] = 1;
1939                         pdata->orientation[5] = 0;
1940
1941                         pdata->orientation[6] = 0;
1942                         pdata->orientation[7] = 0;
1943                         pdata->orientation[8] = -1;
1944                         break;
1945
1946                 case 8:
1947                         pdata->orientation[0] = 0;
1948                         pdata->orientation[1] = 1;
1949                         pdata->orientation[2] = 0;
1950
1951                         pdata->orientation[3] = 1;
1952                         pdata->orientation[4] = 0;
1953                         pdata->orientation[5] = 0;
1954
1955                         pdata->orientation[6] = 0;
1956                         pdata->orientation[7] = 0;
1957                         pdata->orientation[8] = -1;
1958                         break;
1959
1960                 default:
1961                         pdata->orientation[0] = 1;
1962                         pdata->orientation[1] = 0;
1963                         pdata->orientation[2] = 0;
1964
1965                         pdata->orientation[3] = 0;
1966                         pdata->orientation[4] = 1;
1967                         pdata->orientation[5] = 0;
1968
1969                         pdata->orientation[6] = 0;
1970                         pdata->orientation[7] = 0;
1971                         pdata->orientation[8] = 1;
1972                         break;
1973         }
1974
1975         client->irq = pdata->irq_pin;
1976         type = pdata->type;
1977         pdata->irq_flags = irq_flags;
1978         DBG("irq_flags = %lu  padta->irq_flags = %lu\n",irq_flags, pdata->irq_flags);
1979         DBG("type = %d \n",pdata->type);
1980         DBG("irq = %d \n",pdata->irq);
1981         DBG("irq_pin = %d \n",pdata->irq_pin);
1982         DBG("pwer_pin = %d \n",pdata->power_pin);
1983         DBG("reset_pin = %d \n",pdata->reset_pin);
1984         DBG("irq_enable = %d \n",pdata->irq_enable);
1985
1986         DBG("poll_delay_ms = %d \n",pdata->poll_delay_ms);
1987         DBG("x_min = %d \n",pdata->x_min);
1988         DBG("y_min = %d \n",pdata->y_min);
1989         DBG("z_min = %d \n",pdata->z_min);
1990         DBG("factory = %d \n",pdata->factory);
1991         DBG("layout = %d \n",pdata->layout);
1992         DBG("address = 0x%x \n",pdata->address);
1993         DBG("project_name = [%s] \n",pdata->project_name);
1994
1995         DBG(" == %d,%d ,%d \t ,%d ,%d ,%d , \t ,%d, %d, %d ,==%d\n",pdata->orientation[0],pdata->orientation[1],pdata->orientation[2]
1996                                 ,pdata->orientation[3],pdata->orientation[4],pdata->orientation[5]
1997                                 ,pdata->orientation[6],pdata->orientation[7],pdata->orientation[8],ARRAY_SIZE(pdata->orientation));
1998
1999
2000         if((type >= SENSOR_NUM_TYPES) || (type <= SENSOR_TYPE_NULL))
2001         {
2002                 dev_err(&client->adapter->dev, "sensor type is error %d\n", type);
2003                 result = -EFAULT;
2004                 goto out_no_free;
2005         }
2006         if(((int)devid->driver_data >= SENSOR_NUM_ID) || ((int)devid->driver_data <= ID_INVALID))
2007         {
2008                 dev_err(&client->adapter->dev, "sensor id is error %d\n", (int)devid->driver_data);
2009                 result = -EFAULT;
2010                 goto out_no_free;
2011         }
2012         i2c_set_clientdata(client, sensor);
2013         sensor->client = client;
2014         sensor->pdata = pdata;
2015         sensor->type = type;
2016         sensor->i2c_id = (struct i2c_device_id *)devid;
2017
2018
2019         memset(&(sensor->axis), 0, sizeof(struct sensor_axis) );
2020         atomic_set(&(sensor->data_ready), 0);
2021         init_waitqueue_head(&(sensor->data_ready_wq));
2022         mutex_init(&sensor->data_mutex);
2023         mutex_init(&sensor->operation_mutex);
2024         mutex_init(&sensor->sensor_mutex);
2025         mutex_init(&sensor->i2c_mutex);
2026
2027         /* As default, report all information */
2028         atomic_set(&sensor->flags.m_flag, 1);
2029         atomic_set(&sensor->flags.a_flag, 1);
2030         atomic_set(&sensor->flags.mv_flag, 1);
2031         atomic_set(&sensor->flags.open_flag, 0);
2032         atomic_set(&sensor->flags.debug_flag, 1);
2033         init_waitqueue_head(&sensor->flags.open_wq);
2034         sensor->flags.delay = 100;
2035
2036         sensor->status_cur = SENSOR_OFF;
2037         sensor->axis.x = 0;
2038         sensor->axis.y = 0;
2039         sensor->axis.z = 0;
2040
2041         result = sensor_chip_init(sensor->client);
2042         if(result < 0)
2043                 goto out_free_memory;
2044
2045         sensor->input_dev = devm_input_allocate_device(&client->dev);
2046         if (!sensor->input_dev) {
2047                 result = -ENOMEM;
2048                 dev_err(&client->dev,
2049                         "Failed to allocate input device\n");
2050                 goto out_free_memory;
2051         }
2052
2053         switch(type)
2054         {
2055                 case SENSOR_TYPE_ANGLE:
2056                         sensor->input_dev->name = "angle";
2057                         set_bit(EV_ABS, sensor->input_dev->evbit);
2058                         /* x-axis acceleration */
2059                         input_set_abs_params(sensor->input_dev, ABS_X, sensor->ops->range[0], sensor->ops->range[1], 0, 0); //2g full scale range
2060                         /* y-axis acceleration */
2061                         input_set_abs_params(sensor->input_dev, ABS_Y, sensor->ops->range[0], sensor->ops->range[1], 0, 0); //2g full scale range
2062                         /* z-axis acceleration */
2063                         input_set_abs_params(sensor->input_dev, ABS_Z, sensor->ops->range[0], sensor->ops->range[1], 0, 0); //2g full scale range
2064                         break;
2065
2066                 case SENSOR_TYPE_ACCEL:
2067                         sensor->input_dev->name = "gsensor";
2068                         set_bit(EV_ABS, sensor->input_dev->evbit);
2069                         /* x-axis acceleration */
2070                         input_set_abs_params(sensor->input_dev, ABS_X, sensor->ops->range[0], sensor->ops->range[1], 0, 0); //2g full scale range
2071                         /* y-axis acceleration */
2072                         input_set_abs_params(sensor->input_dev, ABS_Y, sensor->ops->range[0], sensor->ops->range[1], 0, 0); //2g full scale range
2073                         /* z-axis acceleration */
2074                         input_set_abs_params(sensor->input_dev, ABS_Z, sensor->ops->range[0], sensor->ops->range[1], 0, 0); //2g full scale range
2075                         break;
2076                 case SENSOR_TYPE_COMPASS:
2077                         sensor->input_dev->name = "compass";
2078                         /* Setup input device */
2079                         set_bit(EV_ABS, sensor->input_dev->evbit);
2080                         /* yaw (0, 360) */
2081                         input_set_abs_params(sensor->input_dev, ABS_RX, 0, 23040, 0, 0);
2082                         /* pitch (-180, 180) */
2083                         input_set_abs_params(sensor->input_dev, ABS_RY, -11520, 11520, 0, 0);
2084                         /* roll (-90, 90) */
2085                         input_set_abs_params(sensor->input_dev, ABS_RZ, -5760, 5760, 0, 0);
2086                         /* x-axis acceleration (720 x 8G) */
2087                         input_set_abs_params(sensor->input_dev, ABS_X, -5760, 5760, 0, 0);
2088                         /* y-axis acceleration (720 x 8G) */
2089                         input_set_abs_params(sensor->input_dev, ABS_Y, -5760, 5760, 0, 0);
2090                         /* z-axis acceleration (720 x 8G) */
2091                         input_set_abs_params(sensor->input_dev, ABS_Z, -5760, 5760, 0, 0);
2092                         /* status of magnetic sensor */
2093                         input_set_abs_params(sensor->input_dev, ABS_RUDDER, -32768, 3, 0, 0);
2094                         /* status of acceleration sensor */
2095                         input_set_abs_params(sensor->input_dev, ABS_WHEEL, -32768, 3, 0, 0);
2096                         /* x-axis of raw magnetic vector (-4096, 4095) */
2097                         input_set_abs_params(sensor->input_dev, ABS_HAT0X, -20480, 20479, 0, 0);
2098                         /* y-axis of raw magnetic vector (-4096, 4095) */
2099                         input_set_abs_params(sensor->input_dev, ABS_HAT0Y, -20480, 20479, 0, 0);
2100                         /* z-axis of raw magnetic vector (-4096, 4095) */
2101                         input_set_abs_params(sensor->input_dev, ABS_BRAKE, -20480, 20479, 0, 0);
2102                         break;
2103                 case SENSOR_TYPE_GYROSCOPE:
2104                         sensor->input_dev->name = "gyro";
2105                         /* x-axis acceleration */
2106                         input_set_capability(sensor->input_dev, EV_REL, REL_RX);
2107                         input_set_abs_params(sensor->input_dev, ABS_RX, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
2108                         /* y-axis acceleration */
2109                         input_set_capability(sensor->input_dev, EV_REL, REL_RY);
2110                         input_set_abs_params(sensor->input_dev, ABS_RY, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
2111                         /* z-axis acceleration */
2112                         input_set_capability(sensor->input_dev, EV_REL, REL_RZ);
2113                         input_set_abs_params(sensor->input_dev, ABS_RZ, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
2114                         break;
2115                 case SENSOR_TYPE_LIGHT:
2116                         sensor->input_dev->name = "lightsensor-level";
2117                         set_bit(EV_ABS, sensor->input_dev->evbit);
2118                         input_set_abs_params(sensor->input_dev, ABS_MISC, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
2119                         input_set_abs_params(sensor->input_dev, ABS_TOOL_WIDTH ,  sensor->ops->brightness[0],sensor->ops->brightness[1], 0, 0);
2120                         break;
2121                 case SENSOR_TYPE_PROXIMITY:
2122                         sensor->input_dev->name = "proximity";
2123                         set_bit(EV_ABS, sensor->input_dev->evbit);
2124                         input_set_abs_params(sensor->input_dev, ABS_DISTANCE, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
2125                         break;
2126                 case SENSOR_TYPE_TEMPERATURE:
2127                         sensor->input_dev->name = "temperature";
2128                         set_bit(EV_ABS, sensor->input_dev->evbit);
2129                         input_set_abs_params(sensor->input_dev, ABS_THROTTLE, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
2130                         break;
2131                 case SENSOR_TYPE_PRESSURE:
2132                         sensor->input_dev->name = "pressure";
2133                         set_bit(EV_ABS, sensor->input_dev->evbit);
2134                         input_set_abs_params(sensor->input_dev, ABS_PRESSURE, sensor->ops->range[0], sensor->ops->range[1], 0, 0);
2135                         break;
2136                 default:
2137                         printk("%s:unknow sensor type=%d\n",__func__,type);
2138                         break;
2139
2140         }
2141         sensor->input_dev->dev.parent = &client->dev;
2142
2143         result = input_register_device(sensor->input_dev);
2144         if (result) {
2145                 dev_err(&client->dev,
2146                         "Unable to register input device %s\n", sensor->input_dev->name);
2147                 goto out_input_register_device_failed;
2148         }
2149
2150         result = sensor_irq_init(sensor->client);
2151         if (result) {
2152                 dev_err(&client->dev,
2153                         "fail to init sensor irq,ret=%d\n",result);
2154                 goto out_input_register_device_failed;
2155         }
2156
2157
2158         sensor->miscdev.parent = &client->dev;
2159         result = sensor_misc_device_register(sensor, type);
2160         if (result) {
2161                 dev_err(&client->dev,
2162                         "fail to register misc device %s\n", sensor->miscdev.name);
2163                 goto out_misc_device_register_device_failed;
2164         }
2165
2166         g_sensor[type] = sensor;
2167
2168         if((type == SENSOR_TYPE_ACCEL) && (sensor->pdata->factory))     //only support  setting gsensor orientation online now
2169         {
2170                 result = gsensor_class_init();
2171                 if (result) {
2172                         dev_err(&client->dev,
2173                                 "fail to register misc device %s\n", sensor->i2c_id->name);
2174                         goto out_misc_device_register_device_failed;
2175                 }
2176         }
2177
2178 #ifdef CONFIG_HAS_EARLYSUSPEND
2179         if((sensor->ops->suspend) && (sensor->ops->resume))
2180         {
2181                 sensor->early_suspend.suspend = sensor_suspend;
2182                 sensor->early_suspend.resume = sensor_resume;
2183                 sensor->early_suspend.level = 0x02;
2184                 register_early_suspend(&sensor->early_suspend);
2185         }
2186 #endif
2187
2188         printk("%s:initialized ok,sensor name:%s,type:%d,id=%d\n\n",__func__,sensor->ops->name,type,(int)sensor->i2c_id->driver_data);
2189
2190         return result;
2191
2192 out_misc_device_register_device_failed:
2193 out_input_register_device_failed:
2194 out_free_memory:
2195 out_no_free:
2196         dev_err(&client->adapter->dev, "%s failed %d\n\n", __func__, result);
2197         return result;
2198
2199 }
2200
2201 static void sensor_shut_down(struct i2c_client *client)
2202 {
2203 #ifdef CONFIG_HAS_EARLYSUSPEND
2204         struct sensor_private_data *sensor =
2205             (struct sensor_private_data *) i2c_get_clientdata(client);
2206         if((sensor->ops->suspend) && (sensor->ops->resume))
2207                 unregister_early_suspend(&sensor->early_suspend);
2208         DBG("%s:%s\n",__func__,sensor->i2c_id->name);
2209 #endif
2210 }
2211
2212 static int sensor_remove(struct i2c_client *client)
2213 {
2214         struct sensor_private_data *sensor =
2215             (struct sensor_private_data *) i2c_get_clientdata(client);
2216         int result = 0;
2217
2218         cancel_delayed_work_sync(&sensor->delaywork);
2219         misc_deregister(&sensor->miscdev);
2220 #ifdef CONFIG_HAS_EARLYSUSPEND
2221         if((sensor->ops->suspend) && (sensor->ops->resume))
2222                 unregister_early_suspend(&sensor->early_suspend);
2223 #endif
2224         return result;
2225 }
2226
2227 static const struct i2c_device_id sensor_id[] = {
2228         /*angle*/
2229         {"angle_kxtik", ANGLE_ID_KXTIK},
2230         {"angle_lis3dh", ANGLE_ID_LIS3DH},
2231         /*gsensor*/
2232         {"gsensor", ACCEL_ID_ALL},
2233         {"gs_mma8452", ACCEL_ID_MMA845X},
2234         {"gs_kxtik", ACCEL_ID_KXTIK},
2235         {"gs_kxtj9", ACCEL_ID_KXTJ9},
2236         {"gs_lis3dh", ACCEL_ID_LIS3DH},
2237         {"gs_mma7660", ACCEL_ID_MMA7660},
2238         {"gs_mxc6225", ACCEL_ID_MXC6225},
2239         {"gs_dmard10", ACCEL_ID_DMARD10},
2240         {"gs_lsm303d", ACCEL_ID_LSM303D},
2241         {"gs_mc3230",ACCEL_ID_MC3230},
2242         {"mpu6880_acc",ACCEL_ID_MPU6880},
2243         {"mpu6500_acc",ACCEL_ID_MPU6500},
2244         {"lsm330_acc", ACCEL_ID_LSM330},
2245         /*compass*/
2246         {"compass", COMPASS_ID_ALL},
2247         {"ak8975", COMPASS_ID_AK8975},
2248         {"ak8963", COMPASS_ID_AK8963},
2249         {"ak09911", COMPASS_ID_AK09911},
2250         {"mmc314x", COMPASS_ID_MMC314X},
2251         /*gyroscope*/
2252         {"gyro", GYRO_ID_ALL},
2253         {"l3g4200d_gyro", GYRO_ID_L3G4200D},
2254         {"l3g20d_gyro", GYRO_ID_L3G20D},
2255         {"ewtsa_gyro", GYRO_ID_EWTSA},
2256         {"k3g", GYRO_ID_K3G},
2257         {"mpu6880_gyro",GYRO_ID_MPU6880},
2258         {"lsm330_gyro", GYRO_ID_LSM330},
2259         /*light sensor*/
2260         {"lightsensor", LIGHT_ID_ALL},
2261         {"light_cm3217", LIGHT_ID_CM3217},
2262         {"light_cm3218", LIGHT_ID_CM3218},
2263         {"light_cm3232", LIGHT_ID_CM3232},
2264         {"light_al3006", LIGHT_ID_AL3006},
2265         {"ls_stk3171", LIGHT_ID_STK3171},
2266         {"ls_isl29023", LIGHT_ID_ISL29023},
2267         {"ls_ap321xx", LIGHT_ID_AP321XX},
2268         {"ls_photoresistor", LIGHT_ID_PHOTORESISTOR},
2269         {"ls_us5152", LIGHT_ID_US5152},
2270         /*proximity sensor*/
2271         {"psensor", PROXIMITY_ID_ALL},
2272         {"proximity_al3006", PROXIMITY_ID_AL3006},
2273         {"ps_stk3171", PROXIMITY_ID_STK3171},
2274         {"ps_ap321xx", PROXIMITY_ID_AP321XX},
2275
2276         /*temperature*/
2277         {"temperature", TEMPERATURE_ID_ALL},
2278         {"tmp_ms5607", TEMPERATURE_ID_MS5607},
2279
2280         /*pressure*/
2281         {"pressure", PRESSURE_ID_ALL},
2282         {"pr_ms5607", PRESSURE_ID_MS5607},
2283
2284         {},
2285 };
2286
2287 static struct of_device_id sensor_dt_ids[] = {
2288         /*gsensor*/
2289         { .compatible = "gs_mma8452" },
2290         { .compatible = "gs_lis3dh" },
2291         { .compatible = "gs_lsm303d" },
2292         { .compatible = "gs_mma7660" },
2293         { .compatible = "gs_mxc6225" },
2294         { .compatible = "gs_mc3230" },
2295         { .compatible = "lsm330_acc" },
2296         /*compass*/
2297         { .compatible = "ak8975" },
2298         { .compatible = "ak8963" },
2299         { .compatible = "ak09911" },
2300         { .compatible = "mmc314x" },
2301
2302         /* gyroscop*/
2303         { .compatible = "l3g4200d_gyro" },
2304         { .compatible = "l3g20d_gyro" },
2305         { .compatible = "ewtsa_gyro" },
2306         { .compatible = "k3g" },
2307         { .compatible = "lsm330_gyro" },
2308
2309         /*light sensor*/
2310         { .compatible = "light_cm3217" },
2311         { .compatible = "light_cm3232" },
2312         { .compatible = "light_al3006" },
2313         { .compatible = "ls_stk3171" },
2314         { .compatible = "ls_ap321xx" },
2315
2316         { .compatible = "ls_photoresistor" },
2317         { .compatible = "ls_us5152" },
2318
2319         /*temperature sensor*/
2320         { .compatible = "tmp_ms5607" },
2321
2322         /*pressure sensor*/
2323         { .compatible = "pr_ms5607" },
2324
2325         /*hall sensor*/
2326         { .compatible = "hall_och165t" },
2327         { }
2328 };
2329
2330
2331 static struct i2c_driver sensor_driver = {
2332         .probe = sensor_probe,
2333         .remove = sensor_remove,
2334         .shutdown = sensor_shut_down,
2335         .id_table = sensor_id,
2336         .driver = {
2337                 .owner = THIS_MODULE,
2338                 .name = "sensors",
2339                 .of_match_table = of_match_ptr(sensor_dt_ids),
2340                 .pm = SENSOR_PM_OPS,
2341         },
2342 };
2343
2344 static int __init sensor_init(void)
2345 {
2346         int res = i2c_add_driver(&sensor_driver);
2347         struct proc_dir_entry *sensor_proc_entry;
2348         pr_info("%s: Probe name %s\n", __func__, sensor_driver.driver.name);
2349         if (res)
2350                 pr_err("%s failed\n", __func__);
2351
2352         sensor_proc_entry = proc_create("driver/sensor_dbg", 0660, NULL, &sensor_proc_fops);
2353         printk("%s\n", SENSOR_VERSION_AND_TIME);
2354         return res;
2355 }
2356
2357 static void __exit sensor_exit(void)
2358 {
2359         pr_info("%s\n", __func__);
2360         i2c_del_driver(&sensor_driver);
2361 }
2362
2363 late_initcall(sensor_init);
2364 module_exit(sensor_exit);
2365
2366 MODULE_AUTHOR("ROCKCHIP Corporation:lw@rock-chips.com");
2367 MODULE_DESCRIPTION("User space character device interface for sensors");
2368 MODULE_LICENSE("GPL");
2369