1 /* drivers/misc/akm8975.c - akm8975 compass driver
3 * Copyright (C) 2007-2008 HTC Corporation.
4 * Author: Hou-Kun Chen <houkun.chen@gmail.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.
18 * Revised by AKM 2009/04/02
19 * Revised by Motorola 2010/05/27
23 #include <linux/interrupt.h>
24 #include <linux/i2c.h>
25 #include <linux/slab.h>
26 #include <linux/irq.h>
27 #include <linux/miscdevice.h>
28 #include <linux/gpio.h>
29 #include <linux/uaccess.h>
30 #include <linux/delay.h>
31 #include <linux/input.h>
32 #include <linux/workqueue.h>
33 #include <linux/freezer.h>
34 #include <linux/akm8975.h>
35 #include <linux/earlysuspend.h>
37 #define AK8975DRV_CALL_DBG 0
38 #if AK8975DRV_CALL_DBG
39 #define FUNCDBG(msg) pr_err("%s:%s\n", __func__, msg);
44 #define AK8975DRV_DATA_DBG 0
45 #define MAX_FAILURE_COUNT 10
48 struct i2c_client
*this_client
;
49 struct akm8975_platform_data
*pdata
;
50 struct input_dev
*input_dev
;
51 struct work_struct work
;
52 struct mutex flags_lock
;
53 #ifdef CONFIG_HAS_EARLYSUSPEND
54 struct early_suspend early_suspend
;
59 * Because misc devices can not carry a pointer from driver register to
60 * open, we keep this global. This limits the driver to a single instance.
62 struct akm8975_data
*akmd_data
;
64 static DECLARE_WAIT_QUEUE_HEAD(open_wq
);
66 static atomic_t open_flag
;
73 static short akmd_delay
;
75 static ssize_t
akm8975_show(struct device
*dev
, struct device_attribute
*attr
,
78 struct i2c_client
*client
= to_i2c_client(dev
);
79 return sprintf(buf
, "%u\n", i2c_smbus_read_byte_data(client
,
82 static ssize_t
akm8975_store(struct device
*dev
, struct device_attribute
*attr
,
83 const char *buf
, size_t count
)
85 struct i2c_client
*client
= to_i2c_client(dev
);
87 strict_strtoul(buf
, 10, &val
);
90 i2c_smbus_write_byte_data(client
, AK8975_REG_CNTL
, val
);
93 static DEVICE_ATTR(akm_ms1
, S_IWUSR
| S_IRUGO
, akm8975_show
, akm8975_store
);
95 static int akm8975_i2c_rxdata(struct akm8975_data
*akm
, char *buf
, int length
)
97 struct i2c_msg msgs
[] = {
99 .addr
= akm
->this_client
->addr
,
105 .addr
= akm
->this_client
->addr
,
114 if (i2c_transfer(akm
->this_client
->adapter
, msgs
, 2) < 0) {
115 pr_err("akm8975_i2c_rxdata: transfer error\n");
121 static int akm8975_i2c_txdata(struct akm8975_data
*akm
, char *buf
, int length
)
123 struct i2c_msg msgs
[] = {
125 .addr
= akm
->this_client
->addr
,
134 if (i2c_transfer(akm
->this_client
->adapter
, msgs
, 1) < 0) {
135 pr_err("akm8975_i2c_txdata: transfer error\n");
141 static void akm8975_ecs_report_value(struct akm8975_data
*akm
, short *rbuf
)
143 struct akm8975_data
*data
= i2c_get_clientdata(akm
->this_client
);
147 #if AK8975DRV_DATA_DBG
148 pr_info("akm8975_ecs_report_value: yaw = %d, pitch = %d, roll = %d\n",
149 rbuf
[0], rbuf
[1], rbuf
[2]);
150 pr_info("tmp = %d, m_stat= %d, g_stat=%d\n", rbuf
[3], rbuf
[4], rbuf
[5]);
151 pr_info("Acceleration: x = %d LSB, y = %d LSB, z = %d LSB\n",
152 rbuf
[6], rbuf
[7], rbuf
[8]);
153 pr_info("Magnetic: x = %d LSB, y = %d LSB, z = %d LSB\n\n",
154 rbuf
[9], rbuf
[10], rbuf
[11]);
156 mutex_lock(&akm
->flags_lock
);
157 /* Report magnetic sensor information */
159 input_report_abs(data
->input_dev
, ABS_RX
, rbuf
[0]);
160 input_report_abs(data
->input_dev
, ABS_RY
, rbuf
[1]);
161 input_report_abs(data
->input_dev
, ABS_RZ
, rbuf
[2]);
162 input_report_abs(data
->input_dev
, ABS_RUDDER
, rbuf
[4]);
165 /* Report acceleration sensor information */
167 input_report_abs(data
->input_dev
, ABS_X
, rbuf
[6]);
168 input_report_abs(data
->input_dev
, ABS_Y
, rbuf
[7]);
169 input_report_abs(data
->input_dev
, ABS_Z
, rbuf
[8]);
170 input_report_abs(data
->input_dev
, ABS_WHEEL
, rbuf
[5]);
173 /* Report temperature information */
175 input_report_abs(data
->input_dev
, ABS_THROTTLE
, rbuf
[3]);
178 input_report_abs(data
->input_dev
, ABS_HAT0X
, rbuf
[9]);
179 input_report_abs(data
->input_dev
, ABS_HAT0Y
, rbuf
[10]);
180 input_report_abs(data
->input_dev
, ABS_BRAKE
, rbuf
[11]);
182 mutex_unlock(&akm
->flags_lock
);
184 input_sync(data
->input_dev
);
187 static void akm8975_ecs_close_done(struct akm8975_data
*akm
)
190 mutex_lock(&akm
->flags_lock
);
195 mutex_unlock(&akm
->flags_lock
);
198 static int akm_aot_open(struct inode
*inode
, struct file
*file
)
203 if (atomic_cmpxchg(&open_flag
, 0, 1) == 0) {
208 ret
= nonseekable_open(inode
, file
);
212 file
->private_data
= akmd_data
;
217 static int akm_aot_release(struct inode
*inode
, struct file
*file
)
220 atomic_set(&open_flag
, 0);
225 static int akm_aot_ioctl(struct inode
*inode
, struct file
*file
,
226 unsigned int cmd
, unsigned long arg
)
228 void __user
*argp
= (void __user
*) arg
;
230 struct akm8975_data
*akm
= file
->private_data
;
235 case ECS_IOCTL_APP_SET_MFLAG
:
236 case ECS_IOCTL_APP_SET_AFLAG
:
237 case ECS_IOCTL_APP_SET_MVFLAG
:
238 if (copy_from_user(&flag
, argp
, sizeof(flag
)))
240 if (flag
< 0 || flag
> 1)
243 case ECS_IOCTL_APP_SET_DELAY
:
244 if (copy_from_user(&flag
, argp
, sizeof(flag
)))
251 mutex_lock(&akm
->flags_lock
);
253 case ECS_IOCTL_APP_SET_MFLAG
:
256 case ECS_IOCTL_APP_GET_MFLAG
:
259 case ECS_IOCTL_APP_SET_AFLAG
:
262 case ECS_IOCTL_APP_GET_AFLAG
:
265 case ECS_IOCTL_APP_SET_MVFLAG
:
268 case ECS_IOCTL_APP_GET_MVFLAG
:
271 case ECS_IOCTL_APP_SET_DELAY
:
274 case ECS_IOCTL_APP_GET_DELAY
:
280 mutex_unlock(&akm
->flags_lock
);
283 case ECS_IOCTL_APP_GET_MFLAG
:
284 case ECS_IOCTL_APP_GET_AFLAG
:
285 case ECS_IOCTL_APP_GET_MVFLAG
:
286 case ECS_IOCTL_APP_GET_DELAY
:
287 if (copy_to_user(argp
, &flag
, sizeof(flag
)))
297 static int akmd_open(struct inode
*inode
, struct file
*file
)
302 err
= nonseekable_open(inode
, file
);
306 file
->private_data
= akmd_data
;
310 static int akmd_release(struct inode
*inode
, struct file
*file
)
312 struct akm8975_data
*akm
= file
->private_data
;
315 akm8975_ecs_close_done(akm
);
319 static int akmd_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
,
322 void __user
*argp
= (void __user
*) arg
;
329 struct akm8975_data
*akm
= file
->private_data
;
335 case ECS_IOCTL_WRITE
:
336 if (copy_from_user(&rwbuf
, argp
, sizeof(rwbuf
)))
340 case ECS_IOCTL_SET_YPR
:
341 if (copy_from_user(&value
, argp
, sizeof(value
)))
354 ret
= akm8975_i2c_rxdata(akm
, &rwbuf
[1], rwbuf
[0]);
359 case ECS_IOCTL_WRITE
:
363 ret
= akm8975_i2c_txdata(akm
, &rwbuf
[1], rwbuf
[0]);
367 case ECS_IOCTL_SET_YPR
:
368 akm8975_ecs_report_value(akm
, value
);
371 case ECS_IOCTL_GET_OPEN_STATUS
:
372 wait_event_interruptible(open_wq
,
373 (atomic_read(&open_flag
) != 0));
374 status
= atomic_read(&open_flag
);
376 case ECS_IOCTL_GET_CLOSE_STATUS
:
377 wait_event_interruptible(open_wq
,
378 (atomic_read(&open_flag
) == 0));
379 status
= atomic_read(&open_flag
);
382 case ECS_IOCTL_GET_DELAY
:
387 FUNCDBG("Unknown cmd\n");
393 if (copy_to_user(argp
, &rwbuf
, sizeof(rwbuf
)))
396 case ECS_IOCTL_GET_OPEN_STATUS
:
397 case ECS_IOCTL_GET_CLOSE_STATUS
:
398 if (copy_to_user(argp
, &status
, sizeof(status
)))
401 case ECS_IOCTL_GET_DELAY
:
402 if (copy_to_user(argp
, &delay
, sizeof(delay
)))
412 /* needed to clear the int. pin */
413 static void akm_work_func(struct work_struct
*work
)
415 struct akm8975_data
*akm
=
416 container_of(work
, struct akm8975_data
, work
);
419 enable_irq(akm
->this_client
->irq
);
422 static irqreturn_t
akm8975_interrupt(int irq
, void *dev_id
)
424 struct akm8975_data
*akm
= dev_id
;
427 disable_irq_nosync(akm
->this_client
->irq
);
428 schedule_work(&akm
->work
);
432 static int akm8975_power_off(struct akm8975_data
*akm
)
434 #if AK8975DRV_CALL_DBG
435 pr_info("%s\n", __func__
);
437 if (akm
->pdata
->power_off
)
438 akm
->pdata
->power_off();
443 static int akm8975_power_on(struct akm8975_data
*akm
)
447 #if AK8975DRV_CALL_DBG
448 pr_info("%s\n", __func__
);
450 if (akm
->pdata
->power_on
) {
451 err
= akm
->pdata
->power_on();
458 static int akm8975_suspend(struct i2c_client
*client
, pm_message_t mesg
)
460 struct akm8975_data
*akm
= i2c_get_clientdata(client
);
462 #if AK8975DRV_CALL_DBG
463 pr_info("%s\n", __func__
);
465 /* TO DO: might need more work after power mgmt
467 return akm8975_power_off(akm
);
470 static int akm8975_resume(struct i2c_client
*client
)
472 struct akm8975_data
*akm
= i2c_get_clientdata(client
);
474 #if AK8975DRV_CALL_DBG
475 pr_info("%s\n", __func__
);
477 /* TO DO: might need more work after power mgmt
479 return akm8975_power_on(akm
);
482 #ifdef CONFIG_HAS_EARLYSUSPEND
483 static void akm8975_early_suspend(struct early_suspend
*handler
)
485 struct akm8975_data
*akm
;
486 akm
= container_of(handler
, struct akm8975_data
, early_suspend
);
488 #if AK8975DRV_CALL_DBG
489 pr_info("%s\n", __func__
);
491 akm8975_suspend(akm
->this_client
, PMSG_SUSPEND
);
494 static void akm8975_early_resume(struct early_suspend
*handler
)
496 struct akm8975_data
*akm
;
497 akm
= container_of(handler
, struct akm8975_data
, early_suspend
);
499 #if AK8975DRV_CALL_DBG
500 pr_info("%s\n", __func__
);
502 akm8975_resume(akm
->this_client
);
507 static int akm8975_init_client(struct i2c_client
*client
)
509 struct akm8975_data
*data
;
512 data
= i2c_get_clientdata(client
);
514 ret
= request_irq(client
->irq
, akm8975_interrupt
, IRQF_TRIGGER_RISING
,
518 pr_err("akm8975_init_client: request irq failed\n");
522 init_waitqueue_head(&open_wq
);
524 mutex_lock(&data
->flags_lock
);
529 mutex_unlock(&data
->flags_lock
);
536 static const struct file_operations akmd_fops
= {
537 .owner
= THIS_MODULE
,
539 .release
= akmd_release
,
543 static const struct file_operations akm_aot_fops
= {
544 .owner
= THIS_MODULE
,
545 .open
= akm_aot_open
,
546 .release
= akm_aot_release
,
547 .ioctl
= akm_aot_ioctl
,
550 static struct miscdevice akm_aot_device
= {
551 .minor
= MISC_DYNAMIC_MINOR
,
552 .name
= "akm8975_aot",
553 .fops
= &akm_aot_fops
,
556 static struct miscdevice akmd_device
= {
557 .minor
= MISC_DYNAMIC_MINOR
,
558 .name
= "akm8975_dev",
562 int akm8975_probe(struct i2c_client
*client
,
563 const struct i2c_device_id
*devid
)
565 struct akm8975_data
*akm
;
569 if (client
->dev
.platform_data
== NULL
) {
570 dev_err(&client
->dev
, "platform data is NULL. exiting.\n");
572 goto exit_platform_data_null
;
575 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
576 dev_err(&client
->dev
, "platform data is NULL. exiting.\n");
578 goto exit_check_functionality_failed
;
581 akm
= kzalloc(sizeof(struct akm8975_data
), GFP_KERNEL
);
583 dev_err(&client
->dev
,
584 "failed to allocate memory for module data\n");
586 goto exit_alloc_data_failed
;
589 akm
->pdata
= client
->dev
.platform_data
;
591 mutex_init(&akm
->flags_lock
);
592 INIT_WORK(&akm
->work
, akm_work_func
);
593 i2c_set_clientdata(client
, akm
);
595 err
= akm8975_power_on(akm
);
597 goto exit_power_on_failed
;
599 akm8975_init_client(client
);
600 akm
->this_client
= client
;
603 akm
->input_dev
= input_allocate_device();
604 if (!akm
->input_dev
) {
606 dev_err(&akm
->this_client
->dev
,
607 "input device allocate failed\n");
608 goto exit_input_dev_alloc_failed
;
611 set_bit(EV_ABS
, akm
->input_dev
->evbit
);
614 input_set_abs_params(akm
->input_dev
, ABS_RX
, 0, 23040, 0, 0);
616 input_set_abs_params(akm
->input_dev
, ABS_RY
, -11520, 11520, 0, 0);
618 input_set_abs_params(akm
->input_dev
, ABS_RZ
, -5760, 5760, 0, 0);
619 /* x-axis acceleration */
620 input_set_abs_params(akm
->input_dev
, ABS_X
, -5760, 5760, 0, 0);
621 /* y-axis acceleration */
622 input_set_abs_params(akm
->input_dev
, ABS_Y
, -5760, 5760, 0, 0);
623 /* z-axis acceleration */
624 input_set_abs_params(akm
->input_dev
, ABS_Z
, -5760, 5760, 0, 0);
626 input_set_abs_params(akm
->input_dev
, ABS_THROTTLE
, -30, 85, 0, 0);
627 /* status of magnetic sensor */
628 input_set_abs_params(akm
->input_dev
, ABS_RUDDER
, 0, 3, 0, 0);
629 /* status of acceleration sensor */
630 input_set_abs_params(akm
->input_dev
, ABS_WHEEL
, 0, 3, 0, 0);
631 /* x-axis of raw magnetic vector */
632 input_set_abs_params(akm
->input_dev
, ABS_HAT0X
, -20480, 20479, 0, 0);
633 /* y-axis of raw magnetic vector */
634 input_set_abs_params(akm
->input_dev
, ABS_HAT0Y
, -20480, 20479, 0, 0);
635 /* z-axis of raw magnetic vector */
636 input_set_abs_params(akm
->input_dev
, ABS_BRAKE
, -20480, 20479, 0, 0);
638 akm
->input_dev
->name
= "compass";
640 err
= input_register_device(akm
->input_dev
);
642 pr_err("akm8975_probe: Unable to register input device: %s\n",
643 akm
->input_dev
->name
);
644 goto exit_input_register_device_failed
;
647 err
= misc_register(&akmd_device
);
649 pr_err("akm8975_probe: akmd_device register failed\n");
650 goto exit_misc_device_register_failed
;
653 err
= misc_register(&akm_aot_device
);
655 pr_err("akm8975_probe: akm_aot_device register failed\n");
656 goto exit_misc_device_register_failed
;
659 err
= device_create_file(&client
->dev
, &dev_attr_akm_ms1
);
661 #ifdef CONFIG_HAS_EARLYSUSPEND
662 akm
->early_suspend
.suspend
= akm8975_early_suspend
;
663 akm
->early_suspend
.resume
= akm8975_early_resume
;
664 register_early_suspend(&akm
->early_suspend
);
668 exit_misc_device_register_failed
:
669 exit_input_register_device_failed
:
670 input_free_device(akm
->input_dev
);
671 exit_input_dev_alloc_failed
:
672 akm8975_power_off(akm
);
673 exit_power_on_failed
:
675 exit_alloc_data_failed
:
676 exit_check_functionality_failed
:
677 exit_platform_data_null
:
681 static int __devexit
akm8975_remove(struct i2c_client
*client
)
683 struct akm8975_data
*akm
= i2c_get_clientdata(client
);
685 free_irq(client
->irq
, NULL
);
686 input_unregister_device(akm
->input_dev
);
687 misc_deregister(&akmd_device
);
688 misc_deregister(&akm_aot_device
);
689 akm8975_power_off(akm
);
694 static const struct i2c_device_id akm8975_id
[] = {
699 MODULE_DEVICE_TABLE(i2c
, akm8975_id
);
701 static struct i2c_driver akm8975_driver
= {
702 .probe
= akm8975_probe
,
703 .remove
= akm8975_remove
,
704 #ifndef CONFIG_HAS_EARLYSUSPEND
705 .resume
= akm8975_resume
,
706 .suspend
= akm8975_suspend
,
708 .id_table
= akm8975_id
,
714 static int __init
akm8975_init(void)
716 pr_info("AK8975 compass driver: init\n");
717 FUNCDBG("AK8975 compass driver: init\n");
718 return i2c_add_driver(&akm8975_driver
);
721 static void __exit
akm8975_exit(void)
723 FUNCDBG("AK8975 compass driver: exit\n");
724 i2c_del_driver(&akm8975_driver
);
727 module_init(akm8975_init
);
728 module_exit(akm8975_exit
);
730 MODULE_AUTHOR("Hou-Kun Chen <hk_chen@htc.com>");
731 MODULE_DESCRIPTION("AK8975 compass driver");
732 MODULE_LICENSE("GPL");