1 /* drivers/input/sensors/access/akm8975.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>
33 #include <linux/sensor-dev.h>
35 #define SENSOR_DATA_SIZE 8
38 /*! \name AK8975 operation mode
40 Defines an operation mode of the AK8975.*/
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
48 #define SENSOR_DATA_SIZE 8 /* Rx buffer size, i.e from ST1 to ST2 */
49 #define RWBUF_SIZE 16 /* Read/Write buffer size.*/
52 /*! \name AK8975 register address
54 Defines a register address of the AK8975.*/
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
74 /*! \name AK8975 fuse-rom address
76 Defines a read-only address of the fuse ROM of the AK8975.*/
78 #define AK8975_FUSE_ASAX 0x10
79 #define AK8975_FUSE_ASAY 0x11
80 #define AK8975_FUSE_ASAZ 0x12
83 #define AK8975_INFO_DATA (0x01<<3)
86 #define COMPASS_IOCTL_MAGIC 'c'
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)
106 #define AK8975_DEVICE_ID 0x48
107 static struct i2c_client *this_client;
108 static struct miscdevice compass_dev_device;
113 /****************operate according to sensor chip:start************/
115 static int sensor_active(struct i2c_client *client, int enable, int rate)
117 struct sensor_private_data *sensor =
118 (struct sensor_private_data *) i2c_get_clientdata(client);
121 //sensor->ops->ctrl_data = sensor_read_reg(client, sensor->ops->ctrl_reg);
123 //register setting according to chip datasheet
126 sensor->ops->ctrl_data = AK8975_MODE_SNG_MEASURE;
130 sensor->ops->ctrl_data = AK8975_MODE_POWERDOWN;
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);
136 printk("%s:fail to active sensor\n",__func__);
142 static int sensor_init(struct i2c_client *client)
144 struct sensor_private_data *sensor =
145 (struct sensor_private_data *) i2c_get_clientdata(client);
149 this_client = client;
151 result = sensor->ops->active(client,0,0);
154 printk("%s:line=%d,error\n",__func__,__LINE__);
158 sensor->status_cur = SENSOR_OFF;
160 info = sensor_read_reg(client, AK8975_REG_INFO);
161 if((info & (0x0f<<3)) != AK8975_INFO_DATA)
164 printk("%s:info=0x%x,it is not %s\n",__func__, info, sensor->ops->name);
168 result = misc_register(&compass_dev_device);
170 printk("%s:fail to register misc device %s\n", __func__, compass_dev_device.name);
174 DBG("%s:status_cur=%d\n",__func__, sensor->status_cur);
178 static int sensor_report_value(struct i2c_client *client)
180 struct sensor_private_data *sensor =
181 (struct sensor_private_data *) i2c_get_clientdata(client);
182 char buffer[8] = {0};
184 unsigned char *stat2;
189 if(sensor->ops->read_len < 8) //sensor->ops->read_len = 8
191 printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
195 memset(buffer, 0, 8);
197 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
199 *buffer = sensor->ops->read_reg;
200 ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
210 * Measurement has been completed and data is ready to be read.
212 if ((*stat & 0x01) != 0x01) {
213 DBG(KERN_ERR "%s:ST is not set\n",__func__);
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
225 * DERR bit is self-clearing when ST2 register is read.
229 DBG(KERN_ERR "%s:compass data error\n",__func__);
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
239 * An error is returned.
240 * HOFL bit clears when a new measurement starts.
244 DBG(KERN_ERR "%s:compass data overflow\n",__func__);
248 /* »¥³âµØ»º´æÊý¾Ý. */
249 mutex_lock(&sensor->data_mutex);
250 memcpy(sensor->sensor_data, buffer, sensor->ops->read_len);
251 mutex_unlock(&sensor->data_mutex);
253 for(i=0; i<sensor->ops->read_len; i++)
254 DBG("0x%x,",buffer[i]);
257 if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0)) //read sensor intterupt status register
260 value = sensor_read_reg(client, sensor->ops->int_status_reg);
261 DBG("%s:sensor int status :0x%x\n",__func__,value);
265 //trigger next measurement
266 ret = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
269 printk(KERN_ERR "%s:fail to set ctrl_data:0x%x\n",__func__,sensor->ops->ctrl_data);
276 static void compass_set_YPR(short *rbuf)
278 struct sensor_private_data *sensor =
279 (struct sensor_private_data *) i2c_get_clientdata(this_client);
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]);
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]);
297 DBG("%s:a_flag:x=%d,y=%d,z=%d,WHEEL=%d\n",__func__,rbuf[6], rbuf[7], rbuf[8], rbuf[5]);
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]);
306 DBG("%s:mv_flag:x=%d,y=%d,BRAKE=%d\n",__func__,rbuf[9], rbuf[10], rbuf[11]);
309 input_sync(sensor->input_dev);
313 static int compass_dev_open(struct inode *inode, struct file *file)
316 DBG("%s\n",__func__);
322 static int compass_dev_release(struct inode *inode, struct file *file)
325 DBG("%s\n",__func__);
330 static int compass_akm_set_mode(struct i2c_client *client, char mode)
332 struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
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)
342 if(sensor->pdata->irq_enable)
344 //DBG("%s:enable irq=%d\n",__func__,client->irq);
345 //enable_irq(client->irq);
349 schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
352 sensor->status_cur = SENSOR_ON;
357 case AK8975_MODE_POWERDOWN:
358 if(sensor->status_cur == SENSOR_ON)
360 if(sensor->pdata->irq_enable)
362 //DBG("%s:disable irq=%d\n",__func__,client->irq);
363 //disable_irq_nosync(client->irq);//disable irq
366 cancel_delayed_work_sync(&sensor->delaywork);
368 sensor->status_cur = SENSOR_OFF;
376 case AK8975_MODE_SNG_MEASURE:
377 result = sensor_write_reg(client, sensor->ops->ctrl_reg, AK8975_MODE_SNG_MEASURE);
379 printk("%s:i2c error,mode=%d\n",__func__,mode);
381 case AK8975_MODE_SELF_TEST:
382 result = sensor_write_reg(client, sensor->ops->ctrl_reg, AK8975_MODE_SELF_TEST);
384 printk("%s:i2c error,mode=%d\n",__func__,mode);
386 case AK8975_MODE_FUSE_ACCESS:
387 result = sensor_write_reg(client, sensor->ops->ctrl_reg, AK8975_MODE_FUSE_ACCESS);
389 printk("%s:i2c error,mode=%d\n",__func__,mode);
391 case AK8975_MODE_POWERDOWN:
392 /* Set powerdown mode */
393 result = sensor_write_reg(client, sensor->ops->ctrl_reg, AK8975_MODE_POWERDOWN);
395 printk("%s:i2c error,mode=%d\n",__func__,mode);
399 printk("%s: Unknown mode(%d)", __func__, mode);
403 DBG("%s:mode=%d\n",__func__,mode);
409 static int compass_akm_get_openstatus(void)
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);
416 static int compass_akm_get_closestatus(void)
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);
424 /* ioctl - I/O control */
425 static long compass_dev_ioctl(struct file *file,
426 unsigned int cmd, unsigned long arg)
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;
432 struct akm_platform_data compass;
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. */
444 case ECS_IOCTL_WRITE:
449 if (copy_from_user(&rwbuf, argp, sizeof(rwbuf))) {
453 case ECS_IOCTL_SET_MODE:
457 if (copy_from_user(&mode, argp, sizeof(mode))) {
461 case ECS_IOCTL_SET_YPR:
465 if (copy_from_user(&value, argp, sizeof(value))) {
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);
481 ret = sensor_tx_data(client, &rwbuf[1], rwbuf[0]);
483 mutex_unlock(&sensor->operation_mutex);
484 printk("%s:fait to tx data\n",__func__);
487 mutex_unlock(&sensor->operation_mutex);
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__);
497 ret = sensor_rx_data(client, &rwbuf[1], rwbuf[0]);
499 mutex_unlock(&sensor->operation_mutex);
500 printk("%s:fait to rx data\n",__func__);
503 mutex_unlock(&sensor->operation_mutex);
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)
510 ret = compass_akm_set_mode(client, mode);
512 printk("%s:fait to set mode\n",__func__);
513 mutex_unlock(&sensor->operation_mutex);
517 sensor->ops->ctrl_data = mode;
519 mutex_unlock(&sensor->operation_mutex);
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);
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);
533 case ECS_IOCTL_GET_OPEN_STATUS:
534 status = compass_akm_get_openstatus();
535 DBG("%s:openstatus=%d\n",__func__,status);
537 case ECS_IOCTL_GET_CLOSE_STATUS:
538 status = compass_akm_get_closestatus();
539 DBG("%s:closestatus=%d\n",__func__,status);
541 case ECS_IOCTL_GET_DELAY:
542 delay = sensor->flags.delay;
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));
551 printk("%s:error,ret=%d\n",__FUNCTION__, ret);
562 if (copy_to_user(argp, &rwbuf, rwbuf[0]+1)) {
566 case ECS_IOCTL_GETDATA:
567 if (copy_to_user(argp, &compass_data, sizeof(compass_data))) {
571 case ECS_IOCTL_GET_OPEN_STATUS:
572 case ECS_IOCTL_GET_CLOSE_STATUS:
573 if (copy_to_user(argp, &status, sizeof(status))) {
577 case ECS_IOCTL_GET_DELAY:
578 if (copy_to_user(argp, &delay, sizeof(delay))) {
589 static struct file_operations compass_dev_fops =
591 .owner = THIS_MODULE,
592 .open = compass_dev_open,
593 .release = compass_dev_release,
594 .unlocked_ioctl = compass_dev_ioctl,
598 static struct miscdevice compass_dev_device =
600 .minor = MISC_DYNAMIC_MINOR,
601 .name = "akm8975_dev",
602 .fops = &compass_dev_fops,
605 struct sensor_operate compass_akm8975_ops = {
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,
620 .report = sensor_report_value,
621 .misc_dev = NULL, //private misc support
624 /****************operate according to sensor chip:end************/
626 //function name should not be changed
627 static struct sensor_operate *compass_get_ops(void)
629 return &compass_akm8975_ops;
633 static int __init compass_akm8975_init(void)
635 struct sensor_operate *ops = compass_get_ops();
637 int type = ops->type;
638 result = sensor_register_slave(type, NULL, NULL, compass_get_ops);
643 static void __exit compass_akm8975_exit(void)
645 struct sensor_operate *ops = compass_get_ops();
646 int type = ops->type;
647 sensor_unregister_slave(type, NULL, NULL, compass_get_ops);
651 module_init(compass_akm8975_init);
652 module_exit(compass_akm8975_exit);