ARM64: DTS: Add rk3399-firefly uart4 device, node as /dev/ttyS1
[firefly-linux-kernel-4.4.55.git] / drivers / input / sensors / compass / ak8975.c
1 /* drivers/input/sensors/access/akm8975.c
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 #include <linux/interrupt.h>
17 #include <linux/i2c.h>
18 #include <linux/slab.h>
19 #include <linux/irq.h>
20 #include <linux/miscdevice.h>
21 #include <linux/gpio.h>
22 #include <asm/uaccess.h>
23 #include <asm/atomic.h>
24 #include <linux/delay.h>
25 #include <linux/input.h>
26 #include <linux/workqueue.h>
27 #include <linux/freezer.h>
28 #include <linux/of_gpio.h>
29 #ifdef CONFIG_HAS_EARLYSUSPEND
30 #include <linux/earlysuspend.h>
31 #endif
32
33 #include <linux/sensor-dev.h>
34
35 #define SENSOR_DATA_SIZE                8
36
37
38 /*! \name AK8975 operation mode
39  \anchor AK8975_Mode
40  Defines an operation mode of the AK8975.*/
41 /*! @{*/
42 #define AK8975_MODE_SNG_MEASURE 0x01
43 #define AK8975_MODE_SELF_TEST   0x08
44 #define AK8975_MODE_FUSE_ACCESS 0x0F
45 #define AK8975_MODE_POWERDOWN   0x00
46 /*! @}*/
47
48 #define SENSOR_DATA_SIZE                8       /* Rx buffer size, i.e from ST1 to ST2 */
49 #define RWBUF_SIZE                      16      /* Read/Write buffer size.*/
50
51
52 /*! \name AK8975 register address
53 \anchor AK8975_REG
54 Defines a register address of the AK8975.*/
55 /*! @{*/
56 #define AK8975_REG_WIA          0x00
57 #define AK8975_REG_INFO         0x01
58 #define AK8975_REG_ST1          0x02
59 #define AK8975_REG_HXL          0x03
60 #define AK8975_REG_HXH          0x04
61 #define AK8975_REG_HYL          0x05
62 #define AK8975_REG_HYH          0x06
63 #define AK8975_REG_HZL          0x07
64 #define AK8975_REG_HZH          0x08
65 #define AK8975_REG_ST2          0x09
66 #define AK8975_REG_CNTL         0x0A
67 #define AK8975_REG_RSV          0x0B
68 #define AK8975_REG_ASTC         0x0C
69 #define AK8975_REG_TS1          0x0D
70 #define AK8975_REG_TS2          0x0E
71 #define AK8975_REG_I2CDIS       0x0F
72 /*! @}*/
73
74 /*! \name AK8975 fuse-rom address
75 \anchor AK8975_FUSE
76 Defines a read-only address of the fuse ROM of the AK8975.*/
77 /*! @{*/
78 #define AK8975_FUSE_ASAX        0x10
79 #define AK8975_FUSE_ASAY        0x11
80 #define AK8975_FUSE_ASAZ        0x12
81 /*! @}*/
82
83 #define AK8975_INFO_DATA        (0x01<<3)
84
85
86 #define COMPASS_IOCTL_MAGIC                   'c'
87
88 /* IOCTLs for AKM library */
89 #define ECS_IOCTL_WRITE                 _IOW(COMPASS_IOCTL_MAGIC, 0x01, char*)
90 #define ECS_IOCTL_READ                  _IOWR(COMPASS_IOCTL_MAGIC, 0x02, char*)
91 #define ECS_IOCTL_RESET                 _IO(COMPASS_IOCTL_MAGIC, 0x03) /* NOT used in AK8975 */
92 #define ECS_IOCTL_SET_MODE              _IOW(COMPASS_IOCTL_MAGIC, 0x04, short)
93 #define ECS_IOCTL_GETDATA               _IOR(COMPASS_IOCTL_MAGIC, 0x05, char[SENSOR_DATA_SIZE])
94 #define ECS_IOCTL_SET_YPR               _IOW(COMPASS_IOCTL_MAGIC, 0x06, short[12])
95 #define ECS_IOCTL_GET_OPEN_STATUS       _IOR(COMPASS_IOCTL_MAGIC, 0x07, int)
96 #define ECS_IOCTL_GET_CLOSE_STATUS      _IOR(COMPASS_IOCTL_MAGIC, 0x08, int)
97 #define ECS_IOCTL_GET_LAYOUT            _IOR(COMPASS_IOCTL_MAGIC, 0x09, char)
98 #define ECS_IOCTL_GET_ACCEL             _IOR(COMPASS_IOCTL_MAGIC, 0x0A, short[3])
99 #define ECS_IOCTL_GET_OUTBIT            _IOR(COMPASS_IOCTL_MAGIC, 0x0B, char)
100 #define ECS_IOCTL_GET_DELAY             _IOR(COMPASS_IOCTL_MAGIC, 0x30, short)
101 #define ECS_IOCTL_GET_PROJECT_NAME      _IOR(COMPASS_IOCTL_MAGIC, 0x0D, char[64])
102 #define ECS_IOCTL_GET_MATRIX            _IOR(COMPASS_IOCTL_MAGIC, 0x0E, short [4][3][3])
103 #define ECS_IOCTL_GET_PLATFORM_DATA     _IOR(COMPASS_IOCTL_MAGIC, 0x0E, struct akm_platform_data)
104
105
106 #define AK8975_DEVICE_ID                0x48
107 static struct i2c_client *this_client;
108 static struct miscdevice compass_dev_device;
109
110
111
112
113 /****************operate according to sensor chip:start************/
114
115 static int sensor_active(struct i2c_client *client, int enable, int rate)
116 {
117         struct sensor_private_data *sensor =
118             (struct sensor_private_data *) i2c_get_clientdata(client);
119         int result = 0;
120
121         //sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
122
123         //register setting according to chip datasheet
124         if(enable)
125         {
126                 sensor->ops->ctrl_data = AK8975_MODE_SNG_MEASURE;
127         }
128         else
129         {
130                 sensor->ops->ctrl_data = AK8975_MODE_POWERDOWN;
131         }
132
133         DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
134         result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
135         if(result)
136                 printk("%s:fail to active sensor\n",__func__);
137
138         return result;
139
140 }
141
142 static int sensor_init(struct i2c_client *client)
143 {
144         struct sensor_private_data *sensor =
145             (struct sensor_private_data *) i2c_get_clientdata(client);
146         int result = 0;
147         int info = 0;
148
149         this_client = client;
150
151         result = sensor->ops->active(client,0,0);
152         if(result)
153         {
154                 printk("%s:line=%d,error\n",__func__,__LINE__);
155                 return result;
156         }
157
158         sensor->status_cur = SENSOR_OFF;
159
160         info = sensor_read_reg(client, AK8975_REG_INFO);
161         if((info & (0x0f<<3)) != AK8975_INFO_DATA)
162
163         {
164                 printk("%s:info=0x%x,it is not %s\n",__func__, info, sensor->ops->name);
165                 return -1;
166         }
167
168         result = misc_register(&compass_dev_device);
169         if (result < 0) {
170                 printk("%s:fail to register misc device %s\n", __func__, compass_dev_device.name);
171                 result = -1;
172         }
173
174         DBG("%s:status_cur=%d\n",__func__, sensor->status_cur);
175         return result;
176 }
177
178 static int sensor_report_value(struct i2c_client *client)
179 {
180         struct sensor_private_data *sensor =
181                 (struct sensor_private_data *) i2c_get_clientdata(client);
182         char buffer[8] = {0};
183         unsigned char *stat;
184         unsigned char *stat2;
185         int ret = 0;
186         char value = 0;
187         int i;
188
189         if(sensor->ops->read_len < 8)   //sensor->ops->read_len = 8
190         {
191                 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
192                 return -1;
193         }
194
195         memset(buffer, 0, 8);
196
197         /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
198         do {
199                 *buffer = sensor->ops->read_reg;
200                 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
201                 if (ret < 0)
202                 return ret;
203         } while (0);
204
205         stat = &buffer[0];
206         stat2 = &buffer[7];
207
208         /*
209          * ST : data ready -
210          * Measurement has been completed and data is ready to be read.
211          */
212         if ((*stat & 0x01) != 0x01) {
213                 DBG(KERN_ERR "%s:ST is not set\n",__func__);
214                 return -1;
215         }
216
217         /*
218          * ST2 : data error -
219          * occurs when data read is started outside of a readable period;
220          * data read would not be correct.
221          * Valid in continuous measurement mode only.
222          * In single measurement mode this error should not occour but we
223          * stil account for it and return an error, since the data would be
224          * corrupted.
225          * DERR bit is self-clearing when ST2 register is read.
226          */
227         if (*stat2 & 0x04)
228         {
229                 DBG(KERN_ERR "%s:compass data error\n",__func__);
230                 return -2;
231         }
232
233         /*
234          * ST2 : overflow -
235          * the sum of the absolute values of all axis |X|+|Y|+|Z| < 2400uT.
236          * This is likely to happen in presence of an external magnetic
237          * disturbance; it indicates, the sensor data is incorrect and should
238          * be ignored.
239          * An error is returned.
240          * HOFL bit clears when a new measurement starts.
241          */
242         if (*stat2 & 0x08)
243         {
244                 DBG(KERN_ERR "%s:compass data overflow\n",__func__);
245                 return -3;
246         }
247
248         /* »¥³âµØ»º´æÊý¾Ý. */
249         mutex_lock(&sensor->data_mutex);
250         memcpy(sensor->sensor_data, buffer, sensor->ops->read_len);
251         mutex_unlock(&sensor->data_mutex);
252         DBG("%s:",__func__);
253         for(i=0; i<sensor->ops->read_len; i++)
254                 DBG("0x%x,",buffer[i]);
255         DBG("\n");
256
257         if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))    //read sensor intterupt status register
258         {
259
260                 value = sensor_read_reg(client, sensor->ops->int_status_reg);
261                 DBG("%s:sensor int status :0x%x\n",__func__,value);
262         }
263
264
265         //trigger next measurement
266         ret = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
267         if(ret)
268         {
269                 printk(KERN_ERR "%s:fail to set ctrl_data:0x%x\n",__func__,sensor->ops->ctrl_data);
270                 return ret;
271         }
272
273         return ret;
274 }
275
276 static void compass_set_YPR(short *rbuf)
277 {
278         struct sensor_private_data *sensor =
279             (struct sensor_private_data *) i2c_get_clientdata(this_client);
280
281         /* Report magnetic sensor information */
282         if (atomic_read(&sensor->flags.m_flag)) {
283                 input_report_abs(sensor->input_dev, ABS_RX, rbuf[0]);
284                 input_report_abs(sensor->input_dev, ABS_RY, rbuf[1]);
285                 input_report_abs(sensor->input_dev, ABS_RZ, rbuf[2]);
286                 input_report_abs(sensor->input_dev, ABS_RUDDER, rbuf[4]);
287                 DBG("%s:m_flag:x=%d,y=%d,z=%d,RUDDER=%d\n",__func__,rbuf[0], rbuf[1], rbuf[2], rbuf[4]);
288         }
289
290         /* Report acceleration sensor information */
291         if (atomic_read(&sensor->flags.a_flag)) {
292                 input_report_abs(sensor->input_dev, ABS_X, rbuf[6]);
293                 input_report_abs(sensor->input_dev, ABS_Y, rbuf[7]);
294                 input_report_abs(sensor->input_dev, ABS_Z, rbuf[8]);
295                 input_report_abs(sensor->input_dev, ABS_WHEEL, rbuf[5]);
296
297                 DBG("%s:a_flag:x=%d,y=%d,z=%d,WHEEL=%d\n",__func__,rbuf[6], rbuf[7], rbuf[8], rbuf[5]);
298         }
299
300         /* Report magnetic vector information */
301         if (atomic_read(&sensor->flags.mv_flag)) {
302                 input_report_abs(sensor->input_dev, ABS_HAT0X, rbuf[9]);
303                 input_report_abs(sensor->input_dev, ABS_HAT0Y, rbuf[10]);
304                 input_report_abs(sensor->input_dev, ABS_BRAKE, rbuf[11]);
305
306                 DBG("%s:mv_flag:x=%d,y=%d,BRAKE=%d\n",__func__,rbuf[9], rbuf[10], rbuf[11]);
307         }
308
309         input_sync(sensor->input_dev);
310 }
311
312
313 static int compass_dev_open(struct inode *inode, struct file *file)
314 {
315         int result = 0;
316         DBG("%s\n",__func__);
317
318         return result;
319 }
320
321
322 static int compass_dev_release(struct inode *inode, struct file *file)
323 {
324         int result = 0;
325         DBG("%s\n",__func__);
326
327         return result;
328 }
329
330 static int compass_akm_set_mode(struct i2c_client *client, char mode)
331 {
332         struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
333         int result = 0;
334
335         switch(mode)
336         {
337                 case AK8975_MODE_SNG_MEASURE:
338                 case AK8975_MODE_SELF_TEST:
339                 case AK8975_MODE_FUSE_ACCESS:
340                         if(sensor->status_cur == SENSOR_OFF)
341                         {
342                                 if(sensor->pdata->irq_enable)
343                                 {
344                                         //DBG("%s:enable irq=%d\n",__func__,client->irq);
345                                         //enable_irq(client->irq);
346                                 }
347                                 else
348                                 {
349                                         schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
350                                 }
351
352                                 sensor->status_cur = SENSOR_ON;
353                         }
354
355                         break;
356
357                 case AK8975_MODE_POWERDOWN:
358                         if(sensor->status_cur == SENSOR_ON)
359                         {
360                                 if(sensor->pdata->irq_enable)
361                                 {
362                                         //DBG("%s:disable irq=%d\n",__func__,client->irq);
363                                         //disable_irq_nosync(client->irq);//disable irq
364                                 }
365                                 else
366                                 cancel_delayed_work_sync(&sensor->delaywork);
367
368                                 sensor->status_cur = SENSOR_OFF;
369                         }
370                         break;
371
372         }
373
374         switch(mode)
375         {
376                 case AK8975_MODE_SNG_MEASURE:
377                         result = sensor_write_reg(client, sensor->ops->ctrl_reg, AK8975_MODE_SNG_MEASURE);
378                         if(result)
379                         printk("%s:i2c error,mode=%d\n",__func__,mode);
380                         break;
381                 case AK8975_MODE_SELF_TEST:
382                         result = sensor_write_reg(client, sensor->ops->ctrl_reg, AK8975_MODE_SELF_TEST);
383                         if(result)
384                         printk("%s:i2c error,mode=%d\n",__func__,mode);
385                         break;
386                 case AK8975_MODE_FUSE_ACCESS:
387                         result = sensor_write_reg(client, sensor->ops->ctrl_reg, AK8975_MODE_FUSE_ACCESS);
388                         if(result)
389                         printk("%s:i2c error,mode=%d\n",__func__,mode);
390                         break;
391                 case AK8975_MODE_POWERDOWN:
392                         /* Set powerdown mode */
393                         result = sensor_write_reg(client, sensor->ops->ctrl_reg, AK8975_MODE_POWERDOWN);
394                         if(result)
395                         printk("%s:i2c error,mode=%d\n",__func__,mode);
396                         udelay(100);
397                         break;
398                 default:
399                         printk("%s: Unknown mode(%d)", __func__, mode);
400                         result = -EINVAL;
401                         break;
402         }
403         DBG("%s:mode=%d\n",__func__,mode);
404         return result;
405
406 }
407
408
409 static int compass_akm_get_openstatus(void)
410 {
411         struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
412         wait_event_interruptible(sensor->flags.open_wq, (atomic_read(&sensor->flags.open_flag) != 0));
413         return atomic_read(&sensor->flags.open_flag);
414 }
415
416 static int compass_akm_get_closestatus(void)
417 {
418         struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
419         wait_event_interruptible(sensor->flags.open_wq, (atomic_read(&sensor->flags.open_flag) <= 0));
420         return atomic_read(&sensor->flags.open_flag);
421 }
422
423
424 /* ioctl - I/O control */
425 static long compass_dev_ioctl(struct file *file,
426                           unsigned int cmd, unsigned long arg)
427 {
428         struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
429         struct i2c_client *client = this_client;
430         void __user *argp = (void __user *)arg;
431         int result = 0;
432         struct akm_platform_data compass;
433
434         /* NOTE: In this function the size of "char" should be 1-byte. */
435         char compass_data[SENSOR_DATA_SIZE];/* for GETDATA */
436         char rwbuf[RWBUF_SIZE];         /* for READ/WRITE */
437         char mode;                                      /* for SET_MODE*/
438         short value[12];                        /* for SET_YPR */
439         short delay;                            /* for GET_DELAY */
440         int status;                                     /* for OPEN/CLOSE_STATUS */
441         int ret = -1;                           /* Return value. */
442
443         switch (cmd) {
444                 case ECS_IOCTL_WRITE:
445                 case ECS_IOCTL_READ:
446                         if (argp == NULL) {
447                                 return -EINVAL;
448                         }
449                         if (copy_from_user(&rwbuf, argp, sizeof(rwbuf))) {
450                                 return -EFAULT;
451                         }
452                         break;
453                 case ECS_IOCTL_SET_MODE:
454                         if (argp == NULL) {
455                                 return -EINVAL;
456                         }
457                         if (copy_from_user(&mode, argp, sizeof(mode))) {
458                                 return -EFAULT;
459                         }
460                         break;
461                 case ECS_IOCTL_SET_YPR:
462                         if (argp == NULL) {
463                                 return -EINVAL;
464                         }
465                         if (copy_from_user(&value, argp, sizeof(value))) {
466                                 return -EFAULT;
467                         }
468                         break;
469                 default:
470                         break;
471         }
472
473         switch (cmd) {
474                 case ECS_IOCTL_WRITE:
475                         DBG("%s:ECS_IOCTL_WRITE start\n",__func__);
476                         mutex_lock(&sensor->operation_mutex);
477                         if ((rwbuf[0] < 2) || (rwbuf[0] > (RWBUF_SIZE-1))) {
478                                 mutex_unlock(&sensor->operation_mutex);
479                                 return -EINVAL;
480                         }
481                         ret = sensor_tx_data(client, &rwbuf[1], rwbuf[0]);
482                         if (ret < 0) {
483                                 mutex_unlock(&sensor->operation_mutex);
484                                 printk("%s:fait to tx data\n",__func__);
485                                 return ret;
486                         }
487                         mutex_unlock(&sensor->operation_mutex);
488                         break;
489                 case ECS_IOCTL_READ:
490                         DBG("%s:ECS_IOCTL_READ start\n",__func__);
491                         mutex_lock(&sensor->operation_mutex);
492                         if ((rwbuf[0] < 1) || (rwbuf[0] > (RWBUF_SIZE-1))) {
493                                 mutex_unlock(&sensor->operation_mutex);
494                                 printk("%s:data is error\n",__func__);
495                                 return -EINVAL;
496                         }
497                         ret = sensor_rx_data(client, &rwbuf[1], rwbuf[0]);
498                         if (ret < 0) {
499                                 mutex_unlock(&sensor->operation_mutex);
500                                 printk("%s:fait to rx data\n",__func__);
501                                 return ret;
502                         }
503                         mutex_unlock(&sensor->operation_mutex);
504                         break;
505                 case ECS_IOCTL_SET_MODE:
506                         DBG("%s:ECS_IOCTL_SET_MODE start\n",__func__);
507                         mutex_lock(&sensor->operation_mutex);
508                         if(sensor->ops->ctrl_data != mode)
509                         {
510                                 ret = compass_akm_set_mode(client, mode);
511                                 if (ret < 0) {
512                                         printk("%s:fait to set mode\n",__func__);
513                                         mutex_unlock(&sensor->operation_mutex);
514                                         return ret;
515                                 }
516
517                                 sensor->ops->ctrl_data = mode;
518                         }
519                         mutex_unlock(&sensor->operation_mutex);
520                         break;
521                 case ECS_IOCTL_GETDATA:
522                         DBG("%s:ECS_IOCTL_GETDATA start\n",__func__);
523                         mutex_lock(&sensor->data_mutex);
524                         memcpy(compass_data, sensor->sensor_data, SENSOR_DATA_SIZE);    //get data from buffer
525                         mutex_unlock(&sensor->data_mutex);
526                         break;
527                 case ECS_IOCTL_SET_YPR:
528                         DBG("%s:ECS_IOCTL_SET_YPR start\n",__func__);
529                         mutex_lock(&sensor->data_mutex);
530                         compass_set_YPR(value);
531                         mutex_unlock(&sensor->data_mutex);
532                         break;
533                 case ECS_IOCTL_GET_OPEN_STATUS:
534                         status = compass_akm_get_openstatus();
535                         DBG("%s:openstatus=%d\n",__func__,status);
536                         break;
537                 case ECS_IOCTL_GET_CLOSE_STATUS:
538                         status = compass_akm_get_closestatus();
539                         DBG("%s:closestatus=%d\n",__func__,status);
540                         break;
541                 case ECS_IOCTL_GET_DELAY:
542                         delay = sensor->flags.delay;
543                         break;
544                 case ECS_IOCTL_GET_PLATFORM_DATA:
545                         DBG("%s:ECS_IOCTL_GET_PLATFORM_DATA start\n",__func__);
546                         //memcpy(compass.m_layout, sensor->pdata->m_layout, sizeof(sensor->pdata->m_layout));
547                         //memcpy(compass.project_name, sensor->pdata->project_name, sizeof(sensor->pdata->project_name));
548                         ret = copy_to_user(argp, &compass, sizeof(compass));
549                         if(ret < 0)
550                         {
551                                 printk("%s:error,ret=%d\n",__FUNCTION__, ret);
552                                 return ret;
553                         }
554                         break;
555
556                 default:
557                         return -ENOTTY;
558         }
559
560         switch (cmd) {
561                 case ECS_IOCTL_READ:
562                         if (copy_to_user(argp, &rwbuf, rwbuf[0]+1)) {
563                                 return -EFAULT;
564                         }
565                         break;
566                 case ECS_IOCTL_GETDATA:
567                         if (copy_to_user(argp, &compass_data, sizeof(compass_data))) {
568                                 return -EFAULT;
569                         }
570                         break;
571                 case ECS_IOCTL_GET_OPEN_STATUS:
572                 case ECS_IOCTL_GET_CLOSE_STATUS:
573                         if (copy_to_user(argp, &status, sizeof(status))) {
574                                 return -EFAULT;
575                         }
576                         break;
577                 case ECS_IOCTL_GET_DELAY:
578                         if (copy_to_user(argp, &delay, sizeof(delay))) {
579                                 return -EFAULT;
580                         }
581                         break;
582                 default:
583                         break;
584         }
585
586         return result;
587 }
588
589 static struct file_operations compass_dev_fops =
590 {
591         .owner = THIS_MODULE,
592         .open = compass_dev_open,
593         .release = compass_dev_release,
594         .unlocked_ioctl = compass_dev_ioctl,
595 };
596
597
598 static struct miscdevice compass_dev_device =
599 {
600         .minor = MISC_DYNAMIC_MINOR,
601         .name = "akm8975_dev",
602         .fops = &compass_dev_fops,
603 };
604
605 struct sensor_operate compass_akm8975_ops = {
606         .name                           = "akm8975",
607         .type                           = SENSOR_TYPE_COMPASS,  //it is important
608         .id_i2c                         = COMPASS_ID_AK8975,
609         .read_reg                       = AK8975_REG_ST1,       //read data
610         .read_len                       = SENSOR_DATA_SIZE,     //data length
611         .id_reg                         = AK8975_REG_WIA,       //read id
612         .id_data                        = AK8975_DEVICE_ID,
613         .precision                      = 8,                    //12 bits
614         .ctrl_reg                       = AK8975_REG_CNTL,      //enable or disable
615         .int_status_reg                 = SENSOR_UNKNOW_DATA,   //not exist
616         .range                          = {-0xffff,0xffff},
617         .trig                           = IRQF_TRIGGER_RISING,  //if LEVEL interrupt then IRQF_ONESHOT
618         .active                         = sensor_active,
619         .init                           = sensor_init,
620         .report                         = sensor_report_value,
621         .misc_dev                       = NULL,                 //private misc support
622 };
623
624 /****************operate according to sensor chip:end************/
625
626 //function name should not be changed
627 static struct sensor_operate *compass_get_ops(void)
628 {
629         return &compass_akm8975_ops;
630 }
631
632
633 static int __init compass_akm8975_init(void)
634 {
635         struct sensor_operate *ops = compass_get_ops();
636         int result = 0;
637         int type = ops->type;
638         result = sensor_register_slave(type, NULL, NULL, compass_get_ops);
639
640         return result;
641 }
642
643 static void __exit compass_akm8975_exit(void)
644 {
645         struct sensor_operate *ops = compass_get_ops();
646         int type = ops->type;
647         sensor_unregister_slave(type, NULL, NULL, compass_get_ops);
648 }
649
650
651 module_init(compass_akm8975_init);
652 module_exit(compass_akm8975_exit);
653
654