1 /* drivers/input/sensors/access/akm8963.c
3 * Copyright (C) 2012-2015 ROCKCHIP.
4 * Author: luowei <lw@rock-chips.com>
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.
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.
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>
32 #include <linux/sensor-dev.h>
34 #define AKM_SENSOR_INFO_SIZE 2
35 #define AKM_SENSOR_CONF_SIZE 3
36 #define SENSOR_DATA_SIZE 8
37 #define YPR_DATA_SIZE 12
40 #define ACC_DATA_FLAG 0
41 #define MAG_DATA_FLAG 1
42 #define ORI_DATA_FLAG 2
43 #define AKM_NUM_SENSORS 3
45 #define ACC_DATA_READY (1<<(ACC_DATA_FLAG))
46 #define MAG_DATA_READY (1<<(MAG_DATA_FLAG))
47 #define ORI_DATA_READY (1<<(ORI_DATA_FLAG))
49 /*! \name AK8963 constant definition
51 Constant definitions of the AK8963.*/
52 #define AK8963_MEASUREMENT_TIME_US 10000
54 /*! \name AK8963 operation mode
56 Defines an operation mode of the AK8963.*/
58 #define AK8963_MODE_SNG_MEASURE 0x01
59 #define AK8963_MODE_SELF_TEST 0x08
60 #define AK8963_MODE_FUSE_ACCESS 0x0F
61 #define AK8963_MODE_POWERDOWN 0x00
65 /*! \name AK8963 register address
67 Defines a register address of the AK8963.*/
69 #define AK8963_REG_WIA 0x00
70 #define AK8963_REG_INFO 0x01
71 #define AK8963_REG_ST1 0x02
72 #define AK8963_REG_HXL 0x03
73 #define AK8963_REG_HXH 0x04
74 #define AK8963_REG_HYL 0x05
75 #define AK8963_REG_HYH 0x06
76 #define AK8963_REG_HZL 0x07
77 #define AK8963_REG_HZH 0x08
78 #define AK8963_REG_ST2 0x09
79 #define AK8963_REG_CNTL1 0x0A
80 #define AK8963_REG_CNTL2 0x0B
81 #define AK8963_REG_ASTC 0x0C
82 #define AK8963_REG_TS1 0x0D
83 #define AK8963_REG_TS2 0x0E
84 #define AK8963_REG_I2CDIS 0x0F
86 #define AK8963_WIA_VALUE 0x48
90 /*! \name AK8963 fuse-rom address
92 Defines a read-only address of the fuse ROM of the AK8963.*/
94 #define AK8963_FUSE_ASAX 0x10
95 #define AK8963_FUSE_ASAY 0x11
96 #define AK8963_FUSE_ASAZ 0x12
99 #define AK8963_INFO_DATA (0x03<<3)
102 #define COMPASS_IOCTL_MAGIC 'c'
104 /* IOCTLs for AKM library */
105 #define ECS_IOCTL_WRITE _IOW(COMPASS_IOCTL_MAGIC, 0x01, char*)
106 #define ECS_IOCTL_READ _IOWR(COMPASS_IOCTL_MAGIC, 0x02, char*)
107 #define ECS_IOCTL_RESET _IO(COMPASS_IOCTL_MAGIC, 0x03) /* NOT used in AK8975 */
108 #define ECS_IOCTL_SET_MODE _IOW(COMPASS_IOCTL_MAGIC, 0x04, short)
109 #define ECS_IOCTL_GETDATA _IOR(COMPASS_IOCTL_MAGIC, 0x05, char[SENSOR_DATA_SIZE])
110 #define ECS_IOCTL_SET_YPR _IOW(COMPASS_IOCTL_MAGIC, 0x06, short[12])
111 #define ECS_IOCTL_GET_OPEN_STATUS _IOR(COMPASS_IOCTL_MAGIC, 0x07, int)
112 #define ECS_IOCTL_GET_CLOSE_STATUS _IOR(COMPASS_IOCTL_MAGIC, 0x08, int)
113 #define ECS_IOCTL_GET_LAYOUT _IOR(COMPASS_IOCTL_MAGIC, 0x09, char)
114 #define ECS_IOCTL_GET_ACCEL _IOR(COMPASS_IOCTL_MAGIC, 0x0A, short[3])
115 #define ECS_IOCTL_GET_OUTBIT _IOR(COMPASS_IOCTL_MAGIC, 0x0B, char)
116 #define ECS_IOCTL_GET_DELAY _IOR(COMPASS_IOCTL_MAGIC, 0x30, short)
117 #define ECS_IOCTL_GET_PROJECT_NAME _IOR(COMPASS_IOCTL_MAGIC, 0x0D, char[64])
118 #define ECS_IOCTL_GET_MATRIX _IOR(COMPASS_IOCTL_MAGIC, 0x0E, short [4][3][3])
119 #define ECS_IOCTL_GET_PLATFORM_DATA _IOR(COMPASS_IOCTL_MAGIC, 0x0E, struct akm_platform_data)
120 #define ECS_IOCTL_GET_INFO _IOR(COMPASS_IOCTL_MAGIC, 0x27, unsigned char[AKM_SENSOR_INFO_SIZE])
121 #define ECS_IOCTL_GET_CONF _IOR(COMPASS_IOCTL_MAGIC, 0x28, unsigned char[AKM_SENSOR_CONF_SIZE])
123 #define AK8963_DEVICE_ID 0x48
124 static struct i2c_client *this_client;
125 static struct miscdevice compass_dev_device;
127 static short g_akm_rbuf[12];
130 /****************operate according to sensor chip:start************/
132 static int sensor_active(struct i2c_client *client, int enable, int rate)
134 struct sensor_private_data *sensor =
135 (struct sensor_private_data *) i2c_get_clientdata(client);
138 //sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
140 //register setting according to chip datasheet
143 sensor->ops->ctrl_data = AK8963_MODE_SNG_MEASURE;
147 sensor->ops->ctrl_data = AK8963_MODE_POWERDOWN;
150 DBG("%s:reg=0x%x,reg_ctrl=0x%x,enable=%d\n",__func__,sensor->ops->ctrl_reg, sensor->ops->ctrl_data, enable);
151 result = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
153 printk("%s:fail to active sensor\n",__func__);
159 static int sensor_init(struct i2c_client *client)
161 struct sensor_private_data *sensor =
162 (struct sensor_private_data *) i2c_get_clientdata(client);
166 this_client = client;
168 result = sensor->ops->active(client,0,0);
171 printk("%s:line=%d,error\n",__func__,__LINE__);
175 sensor->status_cur = SENSOR_OFF;
177 info = sensor_read_reg(client, AK8963_REG_INFO);
178 if((info & (0x0f<<3)) != AK8963_INFO_DATA)
180 printk("%s:info=0x%x,it is not %s\n",__func__, info, sensor->ops->name);
184 result = misc_register(&compass_dev_device);
186 printk("%s:fail to register misc device %s\n", __func__, compass_dev_device.name);
190 DBG("%s:status_cur=%d\n",__func__, sensor->status_cur);
194 static int sensor_report_value(struct i2c_client *client)
196 struct sensor_private_data *sensor =
197 (struct sensor_private_data *) i2c_get_clientdata(client);
198 char buffer[8] = {0};
200 unsigned char *stat2;
205 if(sensor->ops->read_len < 8) //sensor->ops->read_len = 8
207 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
211 memset(buffer, 0, 8);
213 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
215 *buffer = sensor->ops->read_reg;
216 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
226 * Measurement has been completed and data is ready to be read.
228 if ((*stat & 0x01) != 0x01) {
229 DBG(KERN_ERR "%s:ST is not set\n",__func__);
235 * occurs when data read is started outside of a readable period;
236 * data read would not be correct.
237 * Valid in continuous measurement mode only.
238 * In single measurement mode this error should not occour but we
239 * stil account for it and return an error, since the data would be
241 * DERR bit is self-clearing when ST2 register is read.
245 DBG(KERN_ERR "%s:compass data error\n",__func__);
251 * the sum of the absolute values of all axis |X|+|Y|+|Z| < 2400uT.
252 * This is likely to happen in presence of an external magnetic
253 * disturbance; it indicates, the sensor data is incorrect and should
255 * An error is returned.
256 * HOFL bit clears when a new measurement starts.
260 DBG(KERN_ERR "%s:compass data overflow\n",__func__);
264 /* »¥³âµØ»º´æÊý¾Ý. */
265 mutex_lock(&sensor->data_mutex);
266 memcpy(sensor->sensor_data, buffer, sensor->ops->read_len);
267 mutex_unlock(&sensor->data_mutex);
269 for(i=0; i<sensor->ops->read_len; i++)
270 DBG("0x%x,",buffer[i]);
273 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
276 value = sensor_read_reg(client, sensor->ops->int_status_reg);
277 DBG("%s:sensor int status :0x%x\n",__func__,value);
281 //trigger next measurement
282 ret = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
285 printk(KERN_ERR "%s:fail to set ctrl_data:0x%x\n",__func__,sensor->ops->ctrl_data);
292 static void compass_set_YPR(int *rbuf)
294 struct sensor_private_data *sensor =
295 (struct sensor_private_data *) i2c_get_clientdata(this_client);
297 /* No events are reported */
299 printk("%s:Don't waste a time.",__func__);
303 DBG("%s:buf[0]=0x%x\n",__func__, rbuf[0]);
305 /* Report magnetic sensor information */
306 if (atomic_read(&sensor->flags.m_flag) && (rbuf[0] & ORI_DATA_READY)) {
307 input_report_abs(sensor->input_dev, ABS_RX, rbuf[9]);
308 input_report_abs(sensor->input_dev, ABS_RY, rbuf[10]);
309 input_report_abs(sensor->input_dev, ABS_RZ, rbuf[11]);
310 input_report_abs(sensor->input_dev, ABS_RUDDER, rbuf[4]);
311 DBG("%s:m_flag:x=%d,y=%d,z=%d,RUDDER=%d\n", __func__, rbuf[9], rbuf[10], rbuf[11], rbuf[4]);
314 /* Report acceleration sensor information */
315 if (atomic_read(&sensor->flags.a_flag) && (rbuf[0] & ACC_DATA_READY)) {
316 input_report_abs(sensor->input_dev, ABS_X, rbuf[1]);
317 input_report_abs(sensor->input_dev, ABS_Y, rbuf[2]);
318 input_report_abs(sensor->input_dev, ABS_Z, rbuf[3]);
319 input_report_abs(sensor->input_dev, ABS_WHEEL, rbuf[4]);
321 DBG("%s:a_flag:x=%d,y=%d,z=%d,WHEEL=%d\n",__func__,rbuf[1], rbuf[2], rbuf[3], rbuf[4]);
324 /* Report magnetic vector information */
325 if (atomic_read(&sensor->flags.mv_flag) && (rbuf[0] & MAG_DATA_READY)) {
326 input_report_abs(sensor->input_dev, ABS_HAT0X, rbuf[5]);
327 input_report_abs(sensor->input_dev, ABS_HAT0Y, rbuf[6]);
328 input_report_abs(sensor->input_dev, ABS_BRAKE, rbuf[7]);
329 input_report_abs(sensor->input_dev, ABS_HAT1X, rbuf[8]);
331 DBG("%s:mv_flag:x=%d,y=%d,z=%d,status=%d\n", __func__, rbuf[5], rbuf[6], rbuf[7], rbuf[8]);
334 input_sync(sensor->input_dev);
336 memcpy(g_akm_rbuf, rbuf, 12); //used for ECS_IOCTL_GET_ACCEL
341 static int compass_dev_open(struct inode *inode, struct file *file)
344 DBG("%s\n",__func__);
350 static int compass_dev_release(struct inode *inode, struct file *file)
353 DBG("%s\n",__func__);
358 static int compass_akm_set_mode(struct i2c_client *client, char mode)
360 struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
365 case AK8963_MODE_SNG_MEASURE:
366 case AK8963_MODE_SELF_TEST:
367 case AK8963_MODE_FUSE_ACCESS:
368 if(sensor->status_cur == SENSOR_OFF)
370 if(sensor->pdata->irq_enable)
372 //DBG("%s:enable irq=%d\n",__func__,client->irq);
373 //enable_irq(client->irq);
377 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
380 sensor->status_cur = SENSOR_ON;
385 case AK8963_MODE_POWERDOWN:
386 if(sensor->status_cur == SENSOR_ON)
388 if(sensor->pdata->irq_enable)
390 //DBG("%s:disable irq=%d\n",__func__,client->irq);
391 //disable_irq_nosync(client->irq);//disable irq
394 cancel_delayed_work_sync(&sensor->delaywork);
396 sensor->status_cur = SENSOR_OFF;
404 case AK8963_MODE_SNG_MEASURE:
405 result = sensor_write_reg(client, sensor->ops->ctrl_reg, mode);
407 printk("%s:i2c error,mode=%d\n",__func__,mode);
409 case AK8963_MODE_SELF_TEST:
410 result = sensor_write_reg(client, sensor->ops->ctrl_reg, mode);
412 printk("%s:i2c error,mode=%d\n",__func__,mode);
414 case AK8963_MODE_FUSE_ACCESS:
415 result = sensor_write_reg(client, sensor->ops->ctrl_reg, mode);
417 printk("%s:i2c error,mode=%d\n",__func__,mode);
419 case AK8963_MODE_POWERDOWN:
420 /* Set powerdown mode */
421 result = sensor_write_reg(client, sensor->ops->ctrl_reg, AK8963_MODE_POWERDOWN);
423 printk("%s:i2c error,mode=%d\n",__func__,mode);
427 printk("%s: Unknown mode(%d)", __func__, mode);
431 DBG("%s:mode=0x%x\n",__func__,mode);
436 static int compass_akm_reset(struct i2c_client *client)
438 struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
441 if(sensor->pdata->reset_pin > 0)
443 gpio_direction_output(sensor->pdata->reset_pin, GPIO_LOW);
445 gpio_direction_output(sensor->pdata->reset_pin, GPIO_HIGH);
449 /* Set measure mode */
450 result = sensor_write_reg(client, AK8963_REG_CNTL2, AK8963_MODE_SNG_MEASURE);
452 printk("%s:fail to Set measure mode\n",__func__);
463 static int compass_akm_get_openstatus(void)
465 struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
466 wait_event_interruptible(sensor->flags.open_wq, (atomic_read(&sensor->flags.open_flag) != 0));
467 return atomic_read(&sensor->flags.open_flag);
470 static int compass_akm_get_closestatus(void)
472 struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
473 wait_event_interruptible(sensor->flags.open_wq, (atomic_read(&sensor->flags.open_flag) <= 0));
474 return atomic_read(&sensor->flags.open_flag);
478 /* ioctl - I/O control */
479 static long compass_dev_ioctl(struct file *file,
480 unsigned int cmd, unsigned long arg)
482 struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
483 struct i2c_client *client = this_client;
484 void __user *argp = (void __user *)arg;
486 struct akm_platform_data compass;
487 unsigned char sense_info[AKM_SENSOR_INFO_SIZE];
488 unsigned char sense_conf[AKM_SENSOR_CONF_SIZE];
490 /* NOTE: In this function the size of "char" should be 1-byte. */
491 char compass_data[SENSOR_DATA_SIZE]; /* for GETDATA */
492 char rwbuf[RWBUF_SIZE]; /* for READ/WRITE */
493 char mode; /* for SET_MODE*/
494 int value[12]; /* for SET_YPR */
495 int status; /* for OPEN/CLOSE_STATUS */
496 int ret = -1; /* Return value. */
498 //int8_t sensor_buf[SENSOR_DATA_SIZE]; /* for GETDATA */
499 //int32_t ypr_buf[YPR_DATA_SIZE]; /* for SET_YPR */
500 int16_t acc_buf[3]; /* for GET_ACCEL */
501 int64_t delay[AKM_NUM_SENSORS]; /* for GET_DELAY */
503 char layout; /* for GET_LAYOUT */
504 char outbit; /* for GET_OUTBIT */
507 case ECS_IOCTL_WRITE:
512 if (copy_from_user(&rwbuf, argp, sizeof(rwbuf))) {
516 case ECS_IOCTL_SET_MODE:
520 if (copy_from_user(&mode, argp, sizeof(mode))) {
524 case ECS_IOCTL_SET_YPR:
528 if (copy_from_user(&value, argp, sizeof(value))) {
532 case ECS_IOCTL_GET_INFO:
533 case ECS_IOCTL_GET_CONF:
534 case ECS_IOCTL_GETDATA:
535 case ECS_IOCTL_GET_OPEN_STATUS:
536 case ECS_IOCTL_GET_CLOSE_STATUS:
537 case ECS_IOCTL_GET_DELAY:
538 case ECS_IOCTL_GET_LAYOUT:
539 case ECS_IOCTL_GET_OUTBIT:
540 case ECS_IOCTL_GET_ACCEL:
541 /* Just check buffer pointer */
543 printk("%s:invalid argument\n",__func__);
552 case ECS_IOCTL_GET_INFO:
553 sense_info[0] = AK8963_REG_WIA;
554 mutex_lock(&sensor->operation_mutex);
555 ret = sensor_rx_data(client, &sense_info[0], AKM_SENSOR_INFO_SIZE);
556 mutex_unlock(&sensor->operation_mutex);
558 printk("%s:fait to get sense_info\n", __func__);
561 /* Check read data */
562 if (sense_info[0] != AK8963_WIA_VALUE) {
563 dev_err(&client->dev,
564 "%s: The device is not AKM Compass.", __func__);
568 case ECS_IOCTL_GET_CONF:
569 sense_conf[0] = AK8963_FUSE_ASAX;
570 mutex_lock(&sensor->operation_mutex);
571 ret = sensor_rx_data(client, &sense_conf[0], AKM_SENSOR_CONF_SIZE);
572 mutex_unlock(&sensor->operation_mutex);
574 printk("%s:fait to get sense_conf\n", __func__);
578 case ECS_IOCTL_WRITE:
579 DBG("%s:ECS_IOCTL_WRITE start\n",__func__);
580 mutex_lock(&sensor->operation_mutex);
581 if ((rwbuf[0] < 2) || (rwbuf[0] > (RWBUF_SIZE-1))) {
582 mutex_unlock(&sensor->operation_mutex);
585 ret = sensor_tx_data(client, &rwbuf[1], rwbuf[0]);
587 mutex_unlock(&sensor->operation_mutex);
588 printk("%s:fait to tx data\n",__func__);
591 mutex_unlock(&sensor->operation_mutex);
594 DBG("%s:ECS_IOCTL_READ start\n",__func__);
595 mutex_lock(&sensor->operation_mutex);
596 if ((rwbuf[0] < 1) || (rwbuf[0] > (RWBUF_SIZE-1))) {
597 mutex_unlock(&sensor->operation_mutex);
598 printk("%s:data is error\n",__func__);
601 ret = sensor_rx_data(client, &rwbuf[1], rwbuf[0]);
603 mutex_unlock(&sensor->operation_mutex);
604 printk("%s:fait to rx data\n",__func__);
607 mutex_unlock(&sensor->operation_mutex);
609 case ECS_IOCTL_SET_MODE:
610 DBG("%s:ECS_IOCTL_SET_MODE start\n",__func__);
611 mutex_lock(&sensor->operation_mutex);
612 if(sensor->ops->ctrl_data != mode)
614 ret = compass_akm_set_mode(client, mode);
616 printk("%s:fait to set mode\n",__func__);
617 mutex_unlock(&sensor->operation_mutex);
621 sensor->ops->ctrl_data = mode;
623 mutex_unlock(&sensor->operation_mutex);
625 case ECS_IOCTL_GETDATA:
626 DBG("%s:ECS_IOCTL_GETDATA start\n",__func__);
627 mutex_lock(&sensor->data_mutex);
628 memcpy(compass_data, sensor->sensor_data, SENSOR_DATA_SIZE); //get data from buffer
629 mutex_unlock(&sensor->data_mutex);
631 case ECS_IOCTL_SET_YPR:
632 DBG("%s:ECS_IOCTL_SET_YPR start\n",__func__);
633 mutex_lock(&sensor->data_mutex);
634 compass_set_YPR(value);
635 mutex_unlock(&sensor->data_mutex);
637 case ECS_IOCTL_GET_OPEN_STATUS:
638 status = compass_akm_get_openstatus();
639 DBG("%s:openstatus=%d\n",__func__,status);
641 case ECS_IOCTL_GET_CLOSE_STATUS:
642 status = compass_akm_get_closestatus();
643 DBG("%s:closestatus=%d\n",__func__,status);
645 case ECS_IOCTL_GET_DELAY:
646 DBG("%s:ECS_IOCTL_GET_DELAY start\n",__func__);
647 mutex_lock(&sensor->operation_mutex);
648 delay[0] = sensor->flags.delay;
649 delay[1] = sensor->flags.delay;
650 delay[2] = sensor->flags.delay;
651 mutex_unlock(&sensor->operation_mutex);
654 case ECS_IOCTL_GET_PLATFORM_DATA:
655 DBG("%s:ECS_IOCTL_GET_PLATFORM_DATA start\n",__func__);
656 //memcpy(compass.m_layout, sensor->pdata->m_layout, sizeof(sensor->pdata->m_layout));
657 //memcpy(compass.project_name, sensor->pdata->project_name, sizeof(sensor->pdata->project_name));
658 ret = copy_to_user(argp, &compass, sizeof(compass));
661 printk("%s:error,ret=%d\n",__FUNCTION__, ret);
665 case ECS_IOCTL_GET_LAYOUT:
666 DBG("%s:ECS_IOCTL_GET_LAYOUT start\n",__func__);
667 layout = sensor->pdata->layout;
669 case ECS_IOCTL_GET_OUTBIT:
670 DBG("%s:ECS_IOCTL_GET_OUTBIT start\n",__func__);
671 outbit = 1; //sensor->pdata->outbit;
673 case ECS_IOCTL_RESET:
674 DBG("%s:ECS_IOCTL_RESET start\n",__func__);
675 ret = compass_akm_reset(client);
679 case ECS_IOCTL_GET_ACCEL:
680 DBG("%s:ECS_IOCTL_GET_ACCEL start,no accel data\n",__func__);
681 mutex_lock(&sensor->operation_mutex);
682 acc_buf[0] = g_akm_rbuf[6];
683 acc_buf[1] = g_akm_rbuf[7];
684 acc_buf[2] = g_akm_rbuf[8];
685 mutex_unlock(&sensor->operation_mutex);
694 if (copy_to_user(argp, &rwbuf, rwbuf[0]+1)) {
698 case ECS_IOCTL_GETDATA:
699 if (copy_to_user(argp, &compass_data, sizeof(compass_data))) {
703 case ECS_IOCTL_GET_OPEN_STATUS:
704 case ECS_IOCTL_GET_CLOSE_STATUS:
705 if (copy_to_user(argp, &status, sizeof(status))) {
709 case ECS_IOCTL_GET_DELAY:
710 if (copy_to_user(argp, &delay, sizeof(delay))) {
714 case ECS_IOCTL_GET_LAYOUT:
715 if (copy_to_user(argp, &layout, sizeof(layout))) {
716 printk("%s:error:%d\n",__FUNCTION__,__LINE__);
720 case ECS_IOCTL_GET_OUTBIT:
721 if (copy_to_user(argp, &outbit, sizeof(outbit))) {
722 printk("%s:error:%d\n",__FUNCTION__,__LINE__);
726 case ECS_IOCTL_GET_ACCEL:
727 if (copy_to_user(argp, &acc_buf, sizeof(acc_buf))) {
728 printk("%s:error:%d\n",__FUNCTION__,__LINE__);
732 case ECS_IOCTL_GET_INFO:
733 if (copy_to_user(argp, &sense_info, sizeof(sense_info))) {
734 printk("%s:error:%d\n", __FUNCTION__, __LINE__);
738 case ECS_IOCTL_GET_CONF:
739 if (copy_to_user(argp, &sense_conf, sizeof(sense_conf))) {
740 printk("%s:error:%d\n", __FUNCTION__, __LINE__);
751 static struct file_operations compass_dev_fops =
753 .owner = THIS_MODULE,
754 .open = compass_dev_open,
755 .release = compass_dev_release,
756 .unlocked_ioctl = compass_dev_ioctl,
760 static struct miscdevice compass_dev_device =
762 .minor = MISC_DYNAMIC_MINOR,
763 .name = "akm8963_dev",
764 .fops = &compass_dev_fops,
767 struct sensor_operate compass_akm8963_ops = {
769 .type = SENSOR_TYPE_COMPASS, //it is important
770 .id_i2c = COMPASS_ID_AK8963,
771 .read_reg = AK8963_REG_ST1, //read data
772 .read_len = SENSOR_DATA_SIZE, //data length
773 .id_reg = AK8963_REG_WIA, //read id
774 .id_data = AK8963_DEVICE_ID,
775 .precision = 8, //12 bits
776 .ctrl_reg = AK8963_REG_CNTL1, //enable or disable
777 .int_status_reg = SENSOR_UNKNOW_DATA, //not exist
778 .range = {-0xffff,0xffff},
779 .trig = IRQF_TRIGGER_RISING, //if LEVEL interrupt then IRQF_ONESHOT
780 .active = sensor_active,
782 .report = sensor_report_value,
783 .misc_dev = NULL, //private misc support
786 /****************operate according to sensor chip:end************/
788 //function name should not be changed
789 static struct sensor_operate *compass_get_ops(void)
791 return &compass_akm8963_ops;
795 static int __init compass_akm8963_init(void)
797 struct sensor_operate *ops = compass_get_ops();
799 int type = ops->type;
800 result = sensor_register_slave(type, NULL, NULL, compass_get_ops);
805 static void __exit compass_akm8963_exit(void)
807 struct sensor_operate *ops = compass_get_ops();
808 int type = ops->type;
809 sensor_unregister_slave(type, NULL, NULL, compass_get_ops);
813 module_init(compass_akm8963_init);
814 module_exit(compass_akm8963_exit);