xref: /OK3568_Linux_fs/kernel/drivers/input/sensors/compass/ak8975.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 <linux/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 
sensor_active(struct i2c_client * client,int enable,int rate)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 
sensor_init(struct i2c_client * client)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 
sensor_report_value(struct i2c_client * client)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 	int i;
187 
188 	if(sensor->ops->read_len < 8)	//sensor->ops->read_len = 8
189 	{
190 		printk("%s:lenth is error,len=%d\n",__func__,sensor->ops->read_len);
191 		return -1;
192 	}
193 
194 	memset(buffer, 0, 8);
195 
196 	/* Data bytes from hardware xL, xH, yL, yH, zL, zH */
197 	do {
198 		*buffer = sensor->ops->read_reg;
199 		ret = sensor_rx_data(client, buffer, sensor->ops->read_len);
200 		if (ret < 0)
201 		return ret;
202 	} while (0);
203 
204 	stat = &buffer[0];
205 	stat2 = &buffer[7];
206 
207 	/*
208 	 * ST : data ready -
209 	 * Measurement has been completed and data is ready to be read.
210 	 */
211 	if ((*stat & 0x01) != 0x01) {
212 		DBG(KERN_ERR "%s:ST is not set\n",__func__);
213 		return -1;
214 	}
215 
216 	/*
217 	 * ST2 : data error -
218 	 * occurs when data read is started outside of a readable period;
219 	 * data read would not be correct.
220 	 * Valid in continuous measurement mode only.
221 	 * In single measurement mode this error should not occour but we
222 	 * stil account for it and return an error, since the data would be
223 	 * corrupted.
224 	 * DERR bit is self-clearing when ST2 register is read.
225 	 */
226 	if (*stat2 & 0x04)
227 	{
228 		DBG(KERN_ERR "%s:compass data error\n",__func__);
229 		return -2;
230 	}
231 
232 	/*
233 	 * ST2 : overflow -
234 	 * the sum of the absolute values of all axis |X|+|Y|+|Z| < 2400uT.
235 	 * This is likely to happen in presence of an external magnetic
236 	 * disturbance; it indicates, the sensor data is incorrect and should
237 	 * be ignored.
238 	 * An error is returned.
239 	 * HOFL bit clears when a new measurement starts.
240 	 */
241 	if (*stat2 & 0x08)
242 	{
243 		DBG(KERN_ERR "%s:compass data overflow\n",__func__);
244 		return -3;
245 	}
246 
247 	/* ����ػ�������. */
248 	mutex_lock(&sensor->data_mutex);
249 	memcpy(sensor->sensor_data, buffer, sensor->ops->read_len);
250 	mutex_unlock(&sensor->data_mutex);
251 	DBG("%s:",__func__);
252 	for(i=0; i<sensor->ops->read_len; i++)
253 		DBG("0x%x,",buffer[i]);
254 	DBG("\n");
255 
256 	if((sensor->pdata->irq_enable)&& (sensor->ops->int_status_reg >= 0))	//read sensor intterupt status register
257 	{
258 
259 		DBG("%s:sensor int status :0x%x\n", __func__,
260 			sensor_read_reg(client, sensor->ops->int_status_reg));
261 	}
262 
263 
264 	//trigger next measurement
265 	ret = sensor_write_reg(client, sensor->ops->ctrl_reg, sensor->ops->ctrl_data);
266 	if(ret)
267 	{
268 		printk(KERN_ERR "%s:fail to set ctrl_data:0x%x\n",__func__,sensor->ops->ctrl_data);
269 		return ret;
270 	}
271 
272 	return ret;
273 }
274 
compass_set_YPR(short * rbuf)275 static void compass_set_YPR(short *rbuf)
276 {
277 	struct sensor_private_data *sensor =
278 	    (struct sensor_private_data *) i2c_get_clientdata(this_client);
279 
280 	/* Report magnetic sensor information */
281 	if (atomic_read(&sensor->flags.m_flag)) {
282 		input_report_abs(sensor->input_dev, ABS_RX, rbuf[0]);
283 		input_report_abs(sensor->input_dev, ABS_RY, rbuf[1]);
284 		input_report_abs(sensor->input_dev, ABS_RZ, rbuf[2]);
285 		input_report_abs(sensor->input_dev, ABS_RUDDER, rbuf[4]);
286 		DBG("%s:m_flag:x=%d,y=%d,z=%d,RUDDER=%d\n",__func__,rbuf[0], rbuf[1], rbuf[2], rbuf[4]);
287 	}
288 
289 	/* Report acceleration sensor information */
290 	if (atomic_read(&sensor->flags.a_flag)) {
291 		input_report_abs(sensor->input_dev, ABS_X, rbuf[6]);
292 		input_report_abs(sensor->input_dev, ABS_Y, rbuf[7]);
293 		input_report_abs(sensor->input_dev, ABS_Z, rbuf[8]);
294 		input_report_abs(sensor->input_dev, ABS_WHEEL, rbuf[5]);
295 
296 		DBG("%s:a_flag:x=%d,y=%d,z=%d,WHEEL=%d\n",__func__,rbuf[6], rbuf[7], rbuf[8], rbuf[5]);
297 	}
298 
299 	/* Report magnetic vector information */
300 	if (atomic_read(&sensor->flags.mv_flag)) {
301 		input_report_abs(sensor->input_dev, ABS_HAT0X, rbuf[9]);
302 		input_report_abs(sensor->input_dev, ABS_HAT0Y, rbuf[10]);
303 		input_report_abs(sensor->input_dev, ABS_BRAKE, rbuf[11]);
304 
305 		DBG("%s:mv_flag:x=%d,y=%d,BRAKE=%d\n",__func__,rbuf[9], rbuf[10], rbuf[11]);
306 	}
307 
308 	input_sync(sensor->input_dev);
309 }
310 
311 
compass_dev_open(struct inode * inode,struct file * file)312 static int compass_dev_open(struct inode *inode, struct file *file)
313 {
314 	int result = 0;
315 	DBG("%s\n",__func__);
316 
317 	return result;
318 }
319 
320 
compass_dev_release(struct inode * inode,struct file * file)321 static int compass_dev_release(struct inode *inode, struct file *file)
322 {
323 	int result = 0;
324 	DBG("%s\n",__func__);
325 
326 	return result;
327 }
328 
compass_akm_set_mode(struct i2c_client * client,char mode)329 static int compass_akm_set_mode(struct i2c_client *client, char mode)
330 {
331 	struct sensor_private_data* sensor = (struct sensor_private_data *)i2c_get_clientdata(this_client);
332 	int result = 0;
333 
334 	switch(mode)
335 	{
336 		case AK8975_MODE_SNG_MEASURE:
337 		case AK8975_MODE_SELF_TEST:
338 		case AK8975_MODE_FUSE_ACCESS:
339 			if(sensor->status_cur == SENSOR_OFF)
340 			{
341 				sensor->stop_work = 0;
342 				sensor->status_cur = SENSOR_ON;
343 				if(sensor->pdata->irq_enable)
344 				{
345 					//DBG("%s:enable irq=%d\n",__func__,client->irq);
346 					//enable_irq(client->irq);
347 				}
348 				else
349 				{
350 					schedule_delayed_work(&sensor->delaywork, msecs_to_jiffies(sensor->pdata->poll_delay_ms));
351 				}
352 			}
353 
354 			break;
355 
356 		case AK8975_MODE_POWERDOWN:
357 			if(sensor->status_cur == SENSOR_ON)
358 			{
359 				sensor->stop_work = 1;
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 
compass_akm_get_openstatus(void)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 
compass_akm_get_closestatus(void)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 */
compass_dev_ioctl(struct file * file,unsigned int cmd,unsigned long arg)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 
433 	/* NOTE: In this function the size of "char" should be 1-byte. */
434 	char compass_data[SENSOR_DATA_SIZE];/* for GETDATA */
435 	char rwbuf[RWBUF_SIZE];		/* for READ/WRITE */
436 	char mode;					/* for SET_MODE*/
437 	short value[12];			/* for SET_YPR */
438 	short delay;				/* for GET_DELAY */
439 	int status;					/* for OPEN/CLOSE_STATUS */
440 	int ret = -1;				/* Return value. */
441 
442 	switch (cmd) {
443 		case ECS_IOCTL_WRITE:
444 		case ECS_IOCTL_READ:
445 			if (argp == NULL) {
446 				return -EINVAL;
447 			}
448 			if (copy_from_user(&rwbuf, argp, sizeof(rwbuf))) {
449 				return -EFAULT;
450 			}
451 			break;
452 		case ECS_IOCTL_SET_MODE:
453 			if (argp == NULL) {
454 				return -EINVAL;
455 			}
456 			if (copy_from_user(&mode, argp, sizeof(mode))) {
457 				return -EFAULT;
458 			}
459 			break;
460 		case ECS_IOCTL_SET_YPR:
461 			if (argp == NULL) {
462 				return -EINVAL;
463 			}
464 			if (copy_from_user(&value, argp, sizeof(value))) {
465 				return -EFAULT;
466 			}
467 			break;
468 		default:
469 			break;
470 	}
471 
472 	switch (cmd) {
473 		case ECS_IOCTL_WRITE:
474 			DBG("%s:ECS_IOCTL_WRITE start\n",__func__);
475 			mutex_lock(&sensor->operation_mutex);
476 			if ((rwbuf[0] < 2) || (rwbuf[0] > (RWBUF_SIZE-1))) {
477 				mutex_unlock(&sensor->operation_mutex);
478 				return -EINVAL;
479 			}
480 			ret = sensor_tx_data(client, &rwbuf[1], rwbuf[0]);
481 			if (ret < 0) {
482 				mutex_unlock(&sensor->operation_mutex);
483 				printk("%s:fait to tx data\n",__func__);
484 				return ret;
485 			}
486 			mutex_unlock(&sensor->operation_mutex);
487 			break;
488 		case ECS_IOCTL_READ:
489 			DBG("%s:ECS_IOCTL_READ start\n",__func__);
490 			mutex_lock(&sensor->operation_mutex);
491 			if ((rwbuf[0] < 1) || (rwbuf[0] > (RWBUF_SIZE-1))) {
492 				mutex_unlock(&sensor->operation_mutex);
493 				printk("%s:data is error\n",__func__);
494 				return -EINVAL;
495 			}
496 			ret = sensor_rx_data(client, &rwbuf[1], rwbuf[0]);
497 			if (ret < 0) {
498 				mutex_unlock(&sensor->operation_mutex);
499 				printk("%s:fait to rx data\n",__func__);
500 				return ret;
501 			}
502 			mutex_unlock(&sensor->operation_mutex);
503 			break;
504 		case ECS_IOCTL_SET_MODE:
505 			DBG("%s:ECS_IOCTL_SET_MODE start\n",__func__);
506 			mutex_lock(&sensor->operation_mutex);
507 			if(sensor->ops->ctrl_data != mode)
508 			{
509 				ret = compass_akm_set_mode(client, mode);
510 				if (ret < 0) {
511 					printk("%s:fait to set mode\n",__func__);
512 					mutex_unlock(&sensor->operation_mutex);
513 					return ret;
514 				}
515 
516 				sensor->ops->ctrl_data = mode;
517 			}
518 			mutex_unlock(&sensor->operation_mutex);
519 			break;
520 		case ECS_IOCTL_GETDATA:
521 			DBG("%s:ECS_IOCTL_GETDATA start\n",__func__);
522 			mutex_lock(&sensor->data_mutex);
523 			memcpy(compass_data, sensor->sensor_data, SENSOR_DATA_SIZE);	//get data from buffer
524 			mutex_unlock(&sensor->data_mutex);
525 			break;
526 		case ECS_IOCTL_SET_YPR:
527 			DBG("%s:ECS_IOCTL_SET_YPR start\n",__func__);
528 			mutex_lock(&sensor->data_mutex);
529 			compass_set_YPR(value);
530 			mutex_unlock(&sensor->data_mutex);
531 			break;
532 		case ECS_IOCTL_GET_OPEN_STATUS:
533 			status = compass_akm_get_openstatus();
534 			DBG("%s:openstatus=%d\n",__func__,status);
535 			break;
536 		case ECS_IOCTL_GET_CLOSE_STATUS:
537 			status = compass_akm_get_closestatus();
538 			DBG("%s:closestatus=%d\n",__func__,status);
539 			break;
540 		case ECS_IOCTL_GET_DELAY:
541 			delay = sensor->flags.delay;
542 			break;
543 		case ECS_IOCTL_GET_PLATFORM_DATA:
544 			break;
545 
546 		default:
547 			return -ENOTTY;
548 	}
549 
550 	switch (cmd) {
551 		case ECS_IOCTL_READ:
552 			if (copy_to_user(argp, &rwbuf, rwbuf[0]+1)) {
553 				return -EFAULT;
554 			}
555 			break;
556 		case ECS_IOCTL_GETDATA:
557 			if (copy_to_user(argp, &compass_data, sizeof(compass_data))) {
558 				return -EFAULT;
559 			}
560 			break;
561 		case ECS_IOCTL_GET_OPEN_STATUS:
562 		case ECS_IOCTL_GET_CLOSE_STATUS:
563 			if (copy_to_user(argp, &status, sizeof(status))) {
564 				return -EFAULT;
565 			}
566 			break;
567 		case ECS_IOCTL_GET_DELAY:
568 			if (copy_to_user(argp, &delay, sizeof(delay))) {
569 				return -EFAULT;
570 			}
571 			break;
572 		default:
573 			break;
574 	}
575 
576 	return result;
577 }
578 
579 static struct file_operations compass_dev_fops =
580 {
581 	.owner = THIS_MODULE,
582 	.open = compass_dev_open,
583 	.release = compass_dev_release,
584 	.unlocked_ioctl = compass_dev_ioctl,
585 };
586 
587 
588 static struct miscdevice compass_dev_device =
589 {
590 	.minor = MISC_DYNAMIC_MINOR,
591 	.name = "akm8975_dev",
592 	.fops = &compass_dev_fops,
593 };
594 
595 static struct sensor_operate compass_akm8975_ops = {
596 	.name				= "akm8975",
597 	.type				= SENSOR_TYPE_COMPASS,	//it is important
598 	.id_i2c				= COMPASS_ID_AK8975,
599 	.read_reg			= AK8975_REG_ST1,	//read data
600 	.read_len			= SENSOR_DATA_SIZE,	//data length
601 	.id_reg				= AK8975_REG_WIA,	//read id
602 	.id_data 			= AK8975_DEVICE_ID,
603 	.precision			= 8,			//12 bits
604 	.ctrl_reg 			= AK8975_REG_CNTL,	//enable or disable
605 	.int_status_reg			= SENSOR_UNKNOW_DATA,	//not exist
606 	.range				= {-0xffff,0xffff},
607 	.trig				= IRQF_TRIGGER_RISING,	//if LEVEL interrupt then IRQF_ONESHOT
608 	.active				= sensor_active,
609 	.init				= sensor_init,
610 	.report				= sensor_report_value,
611 	.misc_dev 			= NULL,			//private misc support
612 };
613 
614 /****************operate according to sensor chip:end************/
compass_akm8975_probe(struct i2c_client * client,const struct i2c_device_id * devid)615 static int compass_akm8975_probe(struct i2c_client *client,
616 				 const struct i2c_device_id *devid)
617 {
618 	return sensor_register_device(client, NULL, devid, &compass_akm8975_ops);
619 }
620 
compass_akm8975_remove(struct i2c_client * client)621 static int compass_akm8975_remove(struct i2c_client *client)
622 {
623 	return sensor_unregister_device(client, NULL, &compass_akm8975_ops);
624 }
625 
626 static const struct i2c_device_id compass_akm8975_id[] = {
627 	{"ak8975", COMPASS_ID_AK8975},
628 	{}
629 };
630 
631 static struct i2c_driver compass_akm8975_driver = {
632 	.probe = compass_akm8975_probe,
633 	.remove = compass_akm8975_remove,
634 	.shutdown = sensor_shutdown,
635 	.id_table = compass_akm8975_id,
636 	.driver = {
637 		.name = "compass_akm8975",
638 	#ifdef CONFIG_PM
639 		.pm = &sensor_pm_ops,
640 	#endif
641 	},
642 };
643 
644 module_i2c_driver(compass_akm8975_driver);
645 
646 MODULE_AUTHOR("luowei <lw@rock-chips.com>");
647 MODULE_DESCRIPTION("akm8975 3-Axis compasss driver");
648 MODULE_LICENSE("GPL");
649 
650 
651