1 /* drivers/misc/akm8963.c - akm8963 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 /*#define VERBOSE_DEBUG*/
20 #include <linux/device.h>
21 #include <linux/interrupt.h>
22 #include <linux/i2c.h>
23 #include <linux/slab.h>
24 #include <linux/irq.h>
25 #include <linux/miscdevice.h>
26 #include <linux/gpio.h>
27 #include <linux/uaccess.h>
28 #include <linux/delay.h>
29 #include <linux/input.h>
30 #include <linux/workqueue.h>
31 #include <linux/freezer.h>
32 #include <linux/akm8963.h>
34 #define AKM8963_DEBUG_IF 0
35 #define AKM8963_DEBUG_DATA 0
37 #define AKM_ACCEL_ITEMS 3
38 /* Wait timeout in millisecond */
39 #define AKM8963_DRDY_TIMEOUT 100
42 struct i2c_client *i2c;
43 struct input_dev *input;
44 struct device *class_dev;
45 struct class *compass;
46 struct delayed_work work;
48 wait_queue_head_t drdy_wq;
49 wait_queue_head_t open_wq;
51 struct mutex sensor_mutex;
52 int8_t sense_data[SENSOR_DATA_SIZE];
53 struct mutex accel_mutex;
54 int16_t accel_data[AKM_ACCEL_ITEMS];
56 struct mutex val_mutex;
58 int64_t delay[AKM_NUM_SENSORS];
71 static struct akm8963_data *s_akm;
75 /***** I2C I/O function ***********************************************/
76 static int akm8963_i2c_rxdata(
77 struct i2c_client *i2c,
78 unsigned char *rxData,
81 struct i2c_msg msgs[] = {
94 unsigned char addr = rxData[0];
96 if (i2c_transfer(i2c->adapter, msgs, 2) < 0) {
97 dev_err(&i2c->dev, "%s: transfer failed.", __func__);
101 dev_vdbg(&i2c->dev, "RxData: len=%02x, addr=%02x, data=%02x",
102 length, addr, rxData[0]);
106 static int akm8963_i2c_txdata(
107 struct i2c_client *i2c,
108 unsigned char *txData,
111 struct i2c_msg msg[] = {
119 if (i2c_transfer(i2c->adapter, msg, 1) < 0) {
120 dev_err(&i2c->dev, "%s: transfer failed.", __func__);
124 dev_vdbg(&i2c->dev, "TxData: len=%02x, addr=%02x data=%02x",
125 length, txData[0], txData[1]);
129 static int akm8963_i2c_check_device(
130 struct i2c_client *client)
132 unsigned char buffer[2];
135 /* Set measure mode */
136 buffer[0] = AK8963_REG_WIA;
137 err = akm8963_i2c_rxdata(client, buffer, 1);
139 dev_err(&client->dev,
140 "%s: Can not read WIA.", __func__);
144 /* Check read data */
145 if (buffer[0] != 0x48) {
146 dev_err(&client->dev,
147 "%s: The device is not AK8963.", __func__);
154 /***** wlg add inv bypass functions *************************************/
155 #ifdef CONFIG_AK8963_BYPASS_MODE
156 static int inv_bypass_2nd_i2c(struct i2c_client *i2c, uint8_t *txData, int length)
159 struct i2c_msg msg[] = {
167 pr_info("AKM compass driver: inv_bypass_2nd_i2c.");
168 ret = i2c_transfer(i2c->adapter, msg, ARRAY_SIZE(msg));
170 dev_err(&i2c->dev, "%s: transfer failed.", __func__);
172 } else if (ret != ARRAY_SIZE(msg)) {
173 dev_err(&i2c->dev, "%s: transfer failed(size error).", __func__);
176 dev_vdbg(&i2c->dev, "TxData: len=%02x, addr=%02x data=%02x",length, txData[0], txData[1]);
179 static void set_inv_bypass_init(struct i2c_client *client)
185 err = inv_bypass_2nd_i2c(client, buffer, 2);
188 pr_info("AKM compass driver: inv_bypass_2nd_i2c error.");
194 /***** akm miscdevice functions *************************************/
195 static int AKECS_Open(struct inode *inode, struct file *file);
196 static int AKECS_Release(struct inode *inode, struct file *file);
197 static long AKECS_ioctl(struct file *file,
198 unsigned int cmd, unsigned long arg);
200 static struct file_operations AKECS_fops = {
201 .owner = THIS_MODULE,
203 .release = AKECS_Release,
204 .unlocked_ioctl = AKECS_ioctl,
207 static struct miscdevice akm8963_dev = {
208 .minor = MISC_DYNAMIC_MINOR,
209 .name = "akm8963_dev",
213 static int AKECS_Set_CNTL1(
214 struct akm8963_data *akm,
217 unsigned char buffer[2];
221 if (atomic_cmpxchg(&akm->is_busy, 0, 1) != 0) {
222 dev_err(&akm->i2c->dev, "%s: device is busy.", __func__);
227 atomic_set(&akm->drdy, 0);
229 /* Set measure mode */
230 buffer[0] = AK8963_REG_CNTL1;
232 err = akm8963_i2c_txdata(akm->i2c, buffer, 2);
234 dev_err(&akm->i2c->dev, "%s: Can not set CNTL.", __func__);
235 atomic_set(&akm->is_busy, 0);
237 dev_dbg(&akm->i2c->dev, "Mode is set to (%d).", mode);
243 static int AKECS_Set_PowerDown(
244 struct akm8963_data *akm)
246 unsigned char buffer[2];
249 /* Set measure mode */
250 buffer[0] = AK8963_REG_CNTL1;
251 buffer[1] = AK8963_MODE_POWERDOWN;
252 err = akm8963_i2c_txdata(akm->i2c, buffer, 2);
254 dev_err(&akm->i2c->dev,
255 "%s: Can not set to measurement mode.", __func__);
256 atomic_set(&akm->is_busy, 0);
258 dev_dbg(&akm->i2c->dev, "Powerdown mode is set.");
261 /* Set to initial status. */
262 atomic_set(&akm->is_busy, 0);
263 atomic_set(&akm->drdy, 0);
268 static int AKECS_Reset(
269 struct akm8963_data *akm,
272 unsigned char buffer[2];
276 gpio_set_value(akm->rstn, 0);
278 gpio_set_value(akm->rstn, 1);
280 /* Set measure mode */
281 buffer[0] = AK8963_REG_CNTL2;
283 err = akm8963_i2c_txdata(akm->i2c, buffer, 2);
285 dev_err(&akm->i2c->dev,
286 "%s: Can not set SRST bit.", __func__);
288 dev_dbg(&akm->i2c->dev, "Soft reset is done.");
292 /* Device will be accessible 100 us after */
298 static int AKECS_SetMode(
299 struct akm8963_data *akm,
304 switch (mode & 0x0F) {
305 case AK8963_MODE_SNG_MEASURE:
306 case AK8963_MODE_SELF_TEST:
307 case AK8963_MODE_FUSE_ACCESS:
308 err = AKECS_Set_CNTL1(akm, mode);
309 if ((err >= 0) && (akm->irq == 0)) {
310 schedule_delayed_work(
312 usecs_to_jiffies(AK8963_MEASUREMENT_TIME_US));
315 case AK8963_MODE_POWERDOWN:
316 err = AKECS_Set_PowerDown(akm);
319 dev_err(&akm->i2c->dev,
320 "%s: Unknown mode(%d).", __func__, mode);
324 /* wait at least 100us after changing mode */
330 /* This function will block a process until the latest measurement
333 static int AKECS_GetData(
334 struct akm8963_data *akm,
339 err = wait_event_interruptible_timeout(
341 atomic_read(&akm->drdy),
342 AKM8963_DRDY_TIMEOUT);
345 dev_err(&akm->i2c->dev,
346 "%s: wait_event failed (%d).", __func__, err);
349 if (!atomic_read(&akm->drdy)) {
350 dev_err(&akm->i2c->dev,
351 "%s: DRDY is not set.", __func__);
355 mutex_lock(&akm->sensor_mutex);
356 memcpy(rbuf, akm->sense_data, size);
357 atomic_set(&akm->drdy, 0);
358 mutex_unlock(&akm->sensor_mutex);
363 static void AKECS_SetYPR(
364 struct akm8963_data *akm,
368 dev_vdbg(&akm->i2c->dev, "AKM8963 %s: flag =0x%X", __func__,
370 dev_vdbg(&akm->input->dev, " Acceleration[LSB]: %6d,%6d,%6d stat=%d",
371 rbuf[1], rbuf[2], rbuf[3], rbuf[4]);
372 dev_vdbg(&akm->input->dev, " Geomagnetism[LSB]: %6d,%6d,%6d stat=%d",
373 rbuf[5], rbuf[6], rbuf[7], rbuf[8]);
374 dev_vdbg(&akm->input->dev, " Orientation[YPR] : %6d,%6d,%6d",
375 rbuf[9], rbuf[10], rbuf[11]);
377 /* No events are reported */
379 dev_dbg(&akm->i2c->dev, "Don't waste a time.");
383 mutex_lock(&akm->val_mutex);
384 ready = (akm->enable_flag & (uint32_t)rbuf[0]);
385 mutex_unlock(&akm->val_mutex);
387 /* Report acceleration sensor information */
388 if (ready & ACC_DATA_READY) {
389 input_report_abs(akm->input, ABS_X, rbuf[1]);
390 input_report_abs(akm->input, ABS_Y, rbuf[2]);
391 input_report_abs(akm->input, ABS_Z, rbuf[3]);
392 input_report_abs(akm->input, ABS_THROTTLE, rbuf[4]);
394 /* Report magnetic vector information */
395 if (ready & MAG_DATA_READY) {
396 input_report_abs(akm->input, ABS_RX, rbuf[5]);
397 input_report_abs(akm->input, ABS_RY, rbuf[6]);
398 input_report_abs(akm->input, ABS_RZ, rbuf[7]);
399 input_report_abs(akm->input, ABS_RUDDER, rbuf[8]);
401 /* Report orientation sensor information */
402 if (ready & ORI_DATA_READY) {
403 input_report_abs(akm->input, ABS_HAT0X, rbuf[9]);
404 input_report_abs(akm->input, ABS_HAT0Y, rbuf[10]);
405 input_report_abs(akm->input, ABS_HAT1X, rbuf[11]);
406 input_report_abs(akm->input, ABS_HAT1Y, rbuf[4]);
409 input_sync(akm->input);
412 static int AKECS_GetOpenStatus(
413 struct akm8963_data *akm)
415 return wait_event_interruptible(
416 akm->open_wq, (atomic_read(&akm->active) != 0));
419 static int AKECS_GetCloseStatus(
420 struct akm8963_data *akm)
422 return wait_event_interruptible(
423 akm->open_wq, (atomic_read(&akm->active) <= 0));
426 static int AKECS_Open(struct inode *inode, struct file *file)
428 file->private_data = s_akm;
429 return nonseekable_open(inode, file);
432 static int AKECS_Release(struct inode *inode, struct file *file)
438 AKECS_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
440 void __user *argp = (void __user *)arg;
441 struct akm8963_data *akm = file->private_data;
443 /* NOTE: In this function the size of "char" should be 1-byte. */
444 char i2c_buf[RWBUF_SIZE]; /* for READ/WRITE */
445 int8_t sensor_buf[SENSOR_DATA_SIZE];/* for GETDATA */
446 int32_t ypr_buf[YPR_DATA_SIZE]; /* for SET_YPR */
447 int16_t acc_buf[3]; /* for GET_ACCEL */
448 int64_t delay[AKM_NUM_SENSORS]; /* for GET_DELAY */
449 char mode; /* for SET_MODE*/
450 char layout; /* for GET_LAYOUT */
451 char outbit; /* for GET_OUTBIT */
452 int status; /* for OPEN/CLOSE_STATUS */
453 int ret = -1; /* Return value. */
457 case ECS_IOCTL_WRITE:
459 dev_err(&akm->i2c->dev, "invalid argument.");
462 if (copy_from_user(&i2c_buf, argp, sizeof(i2c_buf))) {
463 dev_err(&akm->i2c->dev, "copy_from_user failed.");
467 case ECS_IOCTL_SET_MODE:
469 dev_err(&akm->i2c->dev, "invalid argument.");
472 if (copy_from_user(&mode, argp, sizeof(mode))) {
473 dev_err(&akm->i2c->dev, "copy_from_user failed.");
477 case ECS_IOCTL_SET_YPR:
479 dev_err(&akm->i2c->dev, "invalid argument.");
482 if (copy_from_user(&ypr_buf, argp, sizeof(ypr_buf))) {
483 dev_err(&akm->i2c->dev, "copy_from_user failed.");
486 case ECS_IOCTL_GETDATA:
487 case ECS_IOCTL_GET_OPEN_STATUS:
488 case ECS_IOCTL_GET_CLOSE_STATUS:
489 case ECS_IOCTL_GET_DELAY:
490 case ECS_IOCTL_GET_LAYOUT:
491 case ECS_IOCTL_GET_OUTBIT:
492 case ECS_IOCTL_GET_ACCEL:
493 /* Just check buffer pointer */
495 dev_err(&akm->i2c->dev, "invalid argument.");
506 dev_vdbg(&akm->i2c->dev, "IOCTL_READ called.");
507 if ((i2c_buf[0] < 1) || (i2c_buf[0] > (RWBUF_SIZE-1))) {
508 dev_err(&akm->i2c->dev, "invalid argument.");
511 ret = akm8963_i2c_rxdata(akm->i2c, &i2c_buf[1], i2c_buf[0]);
515 case ECS_IOCTL_WRITE:
516 dev_vdbg(&akm->i2c->dev, "IOCTL_WRITE called.");
517 if ((i2c_buf[0] < 2) || (i2c_buf[0] > (RWBUF_SIZE-1))) {
518 dev_err(&akm->i2c->dev, "invalid argument.");
521 ret = akm8963_i2c_txdata(akm->i2c, &i2c_buf[1], i2c_buf[0]);
525 case ECS_IOCTL_SET_MODE:
526 dev_vdbg(&akm->i2c->dev, "IOCTL_SET_MODE called.");
527 ret = AKECS_SetMode(akm, mode);
531 case ECS_IOCTL_GETDATA:
532 dev_vdbg(&akm->i2c->dev, "IOCTL_GETDATA called.");
533 ret = AKECS_GetData(akm, sensor_buf, SENSOR_DATA_SIZE);
537 case ECS_IOCTL_SET_YPR:
538 dev_vdbg(&akm->i2c->dev, "IOCTL_SET_YPR called.");
539 AKECS_SetYPR(akm, ypr_buf);
541 case ECS_IOCTL_GET_OPEN_STATUS:
542 dev_vdbg(&akm->i2c->dev, "IOCTL_GET_OPEN_STATUS called.");
543 ret = AKECS_GetOpenStatus(akm);
545 dev_err(&akm->i2c->dev,
546 "Get Open returns error (%d).", ret);
549 case ECS_IOCTL_GET_CLOSE_STATUS:
550 dev_vdbg(&akm->i2c->dev, "IOCTL_GET_CLOSE_STATUS called.");
551 ret = AKECS_GetCloseStatus(akm);
553 dev_err(&akm->i2c->dev,
554 "Get Close returns error (%d).", ret);
557 case ECS_IOCTL_GET_DELAY:
558 dev_vdbg(&akm->i2c->dev, "IOCTL_GET_DELAY called.");
559 mutex_lock(&akm->val_mutex);
560 delay[0] = akm->delay[0];
561 delay[1] = akm->delay[1];
562 delay[2] = akm->delay[2];
563 mutex_unlock(&akm->val_mutex);
565 case ECS_IOCTL_GET_LAYOUT:
566 dev_vdbg(&akm->i2c->dev, "IOCTL_GET_LAYOUT called.");
567 layout = akm->layout;
569 case ECS_IOCTL_GET_OUTBIT:
570 dev_vdbg(&akm->i2c->dev, "IOCTL_GET_OUTBIT called.");
571 outbit = akm->outbit;
573 case ECS_IOCTL_RESET:
574 ret = AKECS_Reset(akm, akm->rstn);
578 case ECS_IOCTL_GET_ACCEL:
579 dev_vdbg(&akm->i2c->dev, "IOCTL_GET_ACCEL called.");
580 mutex_lock(&akm->accel_mutex);
581 acc_buf[0] = akm->accel_data[0];
582 acc_buf[1] = akm->accel_data[1];
583 acc_buf[2] = akm->accel_data[2];
584 mutex_unlock(&akm->accel_mutex);
592 if (copy_to_user(argp, &i2c_buf, i2c_buf[0]+1)) {
593 dev_err(&akm->i2c->dev, "copy_to_user failed.");
597 case ECS_IOCTL_GETDATA:
598 if (copy_to_user(argp, &sensor_buf, sizeof(sensor_buf))) {
599 dev_err(&akm->i2c->dev, "copy_to_user failed.");
603 case ECS_IOCTL_GET_OPEN_STATUS:
604 case ECS_IOCTL_GET_CLOSE_STATUS:
605 status = atomic_read(&akm->active);
606 if (copy_to_user(argp, &status, sizeof(status))) {
607 dev_err(&akm->i2c->dev, "copy_to_user failed.");
611 case ECS_IOCTL_GET_DELAY:
612 if (copy_to_user(argp, &delay, sizeof(delay))) {
613 dev_err(&akm->i2c->dev, "copy_to_user failed.");
617 case ECS_IOCTL_GET_LAYOUT:
618 if (copy_to_user(argp, &layout, sizeof(layout))) {
619 dev_err(&akm->i2c->dev, "copy_to_user failed.");
623 case ECS_IOCTL_GET_OUTBIT:
624 if (copy_to_user(argp, &outbit, sizeof(outbit))) {
625 dev_err(&akm->i2c->dev, "copy_to_user failed.");
629 case ECS_IOCTL_GET_ACCEL:
630 if (copy_to_user(argp, &acc_buf, sizeof(acc_buf))) {
631 dev_err(&akm->i2c->dev, "copy_to_user failed.");
642 /***** akm sysfs functions ******************************************/
643 static int create_device_attributes(
645 struct device_attribute *attrs)
650 for (i = 0 ; NULL != attrs[i].attr.name ; ++i) {
651 err = device_create_file(dev, &attrs[i]);
658 device_remove_file(dev, &attrs[i]);
664 static void remove_device_attributes(
666 struct device_attribute *attrs)
670 for (i = 0 ; NULL != attrs[i].attr.name ; ++i)
671 device_remove_file(dev, &attrs[i]);
674 static int create_device_binary_attributes(
675 struct kobject *kobj,
676 struct bin_attribute *attrs)
683 for (i = 0 ; NULL != attrs[i].attr.name ; ++i) {
684 err = sysfs_create_bin_file(kobj, &attrs[i]);
691 sysfs_remove_bin_file(kobj, &attrs[i]);
697 static void remove_device_binary_attributes(
698 struct kobject *kobj,
699 struct bin_attribute *attrs)
703 for (i = 0 ; NULL != attrs[i].attr.name ; ++i)
704 sysfs_remove_bin_file(kobj, &attrs[i]);
707 static bool get_value_as_int(char const *buf, size_t size, int *value)
714 /* maybe text format value */
715 if ((buf[0] == '0') && (size > 1)) {
716 if ((buf[1] == 'x') || (buf[1] == 'X')) {
717 /* hexadecimal format */
718 if (0 != strict_strtol(buf, 16, &tmp))
722 if (0 != strict_strtol(buf, 8, &tmp))
727 if (0 != strict_strtol(buf, 10, &tmp))
739 static bool get_value_as_int64(char const *buf, size_t size, long long *value)
746 /* maybe text format value */
747 if ((buf[0] == '0') && (size > 1)) {
748 if ((buf[1] == 'x') || (buf[1] == 'X')) {
749 /* hexadecimal format */
750 if (0 != strict_strtoll(buf, 16, &tmp))
754 if (0 != strict_strtoll(buf, 8, &tmp))
759 if (0 != strict_strtoll(buf, 10, &tmp))
771 /*********************************************************************
773 * SysFS attribute functions
775 * directory : /sys/class/compass/akm8963/
777 * - enable_acc [rw] [t] : enable flag for accelerometer
778 * - enable_mag [rw] [t] : enable flag for magnetometer
779 * - enable_ori [rw] [t] : enable flag for orientation
780 * - delay_acc [rw] [t] : delay in nanosecond for accelerometer
781 * - delay_mag [rw] [t] : delay in nanosecond for magnetometer
782 * - delay_ori [rw] [t] : delay in nanosecond for orientation
783 * - accel [w] [b] : accelerometer data
786 * - mode [w] [t] : AK8963's mode
787 * - bdata [r] [t] : raw data
788 * - asa [r] [t] : FUSEROM data
790 * [b] = binary format
794 /***** sysfs enable *************************************************/
795 static void akm8963_sysfs_update_active_status(
796 struct akm8963_data *akm)
799 mutex_lock(&akm->val_mutex);
800 en = akm->enable_flag;
801 mutex_unlock(&akm->val_mutex);
804 if (atomic_cmpxchg(&akm->active, 1, 0) == 1) {
805 wake_up(&akm->open_wq);
806 dev_dbg(akm->class_dev, "Deactivated");
809 if (atomic_cmpxchg(&akm->active, 0, 1) == 0) {
810 wake_up(&akm->open_wq);
811 dev_dbg(akm->class_dev, "Activated");
814 dev_dbg(&akm->i2c->dev,
815 "Status updated: enable=0x%X, active=%d",
816 en, atomic_read(&akm->active));
819 static ssize_t akm8963_sysfs_enable_show(
820 struct akm8963_data *akm, char *buf, int pos)
824 mutex_lock(&akm->val_mutex);
825 flag = ((akm->enable_flag >> pos) & 1);
826 mutex_unlock(&akm->val_mutex);
828 return sprintf(buf, "%d\n", flag);
831 static ssize_t akm8963_sysfs_enable_store(
832 struct akm8963_data *akm, char const *buf, size_t count, int pos)
842 if (false == get_value_as_int(buf, count, &en))
847 mutex_lock(&akm->val_mutex);
848 akm->enable_flag &= ~(1<<pos);
849 akm->enable_flag |= ((uint32_t)(en))<<pos;
850 mutex_unlock(&akm->val_mutex);
852 akm8963_sysfs_update_active_status(akm);
857 /***** Acceleration ***/
858 static ssize_t akm8963_enable_acc_show(
859 struct device *dev, struct device_attribute *attr, char *buf)
861 return akm8963_sysfs_enable_show(
862 dev_get_drvdata(dev), buf, ACC_DATA_FLAG);
864 static ssize_t akm8963_enable_acc_store(
865 struct device *dev, struct device_attribute *attr,
866 char const *buf, size_t count)
868 return akm8963_sysfs_enable_store(
869 dev_get_drvdata(dev), buf, count, ACC_DATA_FLAG);
872 /***** Magnetic field ***/
873 static ssize_t akm8963_enable_mag_show(
874 struct device *dev, struct device_attribute *attr, char *buf)
876 return akm8963_sysfs_enable_show(
877 dev_get_drvdata(dev), buf, MAG_DATA_FLAG);
879 static ssize_t akm8963_enable_mag_store(
880 struct device *dev, struct device_attribute *attr,
881 char const *buf, size_t count)
883 return akm8963_sysfs_enable_store(
884 dev_get_drvdata(dev), buf, count, MAG_DATA_FLAG);
887 /***** Orientation ***/
888 static ssize_t akm8963_enable_ori_show(
889 struct device *dev, struct device_attribute *attr, char *buf)
891 return akm8963_sysfs_enable_show(
892 dev_get_drvdata(dev), buf, ORI_DATA_FLAG);
894 static ssize_t akm8963_enable_ori_store(
895 struct device *dev, struct device_attribute *attr,
896 char const *buf, size_t count)
898 return akm8963_sysfs_enable_store(
899 dev_get_drvdata(dev), buf, count, ORI_DATA_FLAG);
902 /***** sysfs delay **************************************************/
903 static ssize_t akm8963_sysfs_delay_show(
904 struct akm8963_data *akm, char *buf, int pos)
908 mutex_lock(&akm->val_mutex);
909 val = akm->delay[pos];
910 mutex_unlock(&akm->val_mutex);
912 return sprintf(buf, "%lld\n", val);
915 static ssize_t akm8963_sysfs_delay_store(
916 struct akm8963_data *akm, char const *buf, size_t count, int pos)
926 if (false == get_value_as_int64(buf, count, &val))
929 mutex_lock(&akm->val_mutex);
930 akm->delay[pos] = val;
931 mutex_unlock(&akm->val_mutex);
936 /***** Accelerometer ***/
937 static ssize_t akm8963_delay_acc_show(
938 struct device *dev, struct device_attribute *attr, char *buf)
940 return akm8963_sysfs_delay_show(
941 dev_get_drvdata(dev), buf, ACC_DATA_FLAG);
943 static ssize_t akm8963_delay_acc_store(
944 struct device *dev, struct device_attribute *attr,
945 char const *buf, size_t count)
947 return akm8963_sysfs_delay_store(
948 dev_get_drvdata(dev), buf, count, ACC_DATA_FLAG);
951 /***** Magnetic field ***/
952 static ssize_t akm8963_delay_mag_show(
953 struct device *dev, struct device_attribute *attr, char *buf)
955 return akm8963_sysfs_delay_show(
956 dev_get_drvdata(dev), buf, MAG_DATA_FLAG);
958 static ssize_t akm8963_delay_mag_store(
959 struct device *dev, struct device_attribute *attr,
960 char const *buf, size_t count)
962 return akm8963_sysfs_delay_store(
963 dev_get_drvdata(dev), buf, count, MAG_DATA_FLAG);
966 /***** Orientation ***/
967 static ssize_t akm8963_delay_ori_show(
968 struct device *dev, struct device_attribute *attr, char *buf)
970 return akm8963_sysfs_delay_show(
971 dev_get_drvdata(dev), buf, ORI_DATA_FLAG);
973 static ssize_t akm8963_delay_ori_store(
974 struct device *dev, struct device_attribute *attr,
975 char const *buf, size_t count)
977 return akm8963_sysfs_delay_store(
978 dev_get_drvdata(dev), buf, count, ORI_DATA_FLAG);
982 /***** accel (binary) ***/
983 static ssize_t akm8963_bin_accel_write(
985 struct kobject *kobj,
986 struct bin_attribute *attr,
991 struct device *dev = container_of(kobj, struct device, kobj);
992 struct akm8963_data *akm = dev_get_drvdata(dev);
998 accel_data = (int16_t*)buf;
1000 mutex_lock(&akm->accel_mutex);
1001 akm->accel_data[0] = accel_data[0];
1002 akm->accel_data[1] = accel_data[1];
1003 akm->accel_data[2] = accel_data[2];
1004 mutex_unlock(&akm->accel_mutex);
1006 dev_vdbg(&akm->i2c->dev, "accel:%d,%d,%d\n",
1007 accel_data[0], accel_data[1], accel_data[2]);
1013 #ifdef AKM8963_DEBUG_IF
1014 static ssize_t akm8963_mode_store(
1015 struct device *dev, struct device_attribute *attr,
1016 char const *buf, size_t count)
1018 struct akm8963_data *akm = dev_get_drvdata(dev);
1027 if (false == get_value_as_int(buf, count, &mode))
1030 if (AKECS_SetMode(akm, mode) < 0)
1036 static ssize_t akm8963_bdata_show(
1037 struct device *dev, struct device_attribute *attr, char *buf)
1039 struct akm8963_data *akm = dev_get_drvdata(dev);
1040 char rbuf[SENSOR_DATA_SIZE];
1042 mutex_lock(&akm->sensor_mutex);
1043 memcpy(&rbuf, akm->sense_data, sizeof(rbuf));
1044 mutex_unlock(&akm->sensor_mutex);
1047 "0x%02X,0x%02X,0x%02X,0x%02X,"
1048 "0x%02X,0x%02X,0x%02X,0x%02X\n",
1049 rbuf[0],rbuf[1],rbuf[2],rbuf[3],
1050 rbuf[4],rbuf[5],rbuf[6],rbuf[7]);
1053 static ssize_t akm8963_asa_show(
1054 struct device *dev, struct device_attribute *attr, char *buf)
1056 struct akm8963_data *akm = dev_get_drvdata(dev);
1058 unsigned char asa[3];
1060 err = AKECS_SetMode(akm, AK8963_MODE_FUSE_ACCESS);
1064 asa[0] = AK8963_FUSE_ASAX;
1065 err = akm8963_i2c_rxdata(akm->i2c, asa, 3);
1069 err = AKECS_SetMode(akm, AK8963_MODE_POWERDOWN);
1073 return sprintf(buf, "0x%02X,0x%02X,0x%02X\n",
1074 asa[0], asa[1], asa[2]);
1078 static struct device_attribute akm8963_attributes[] = {
1079 __ATTR(enable_acc, 0660, akm8963_enable_acc_show, akm8963_enable_acc_store),
1080 __ATTR(enable_mag, 0660, akm8963_enable_mag_show, akm8963_enable_mag_store),
1081 __ATTR(enable_ori, 0660, akm8963_enable_ori_show, akm8963_enable_ori_store),
1082 __ATTR(delay_acc, 0660, akm8963_delay_acc_show, akm8963_delay_acc_store),
1083 __ATTR(delay_mag, 0660, akm8963_delay_mag_show, akm8963_delay_mag_store),
1084 __ATTR(delay_ori, 0660, akm8963_delay_ori_show, akm8963_delay_ori_store),
1085 #ifdef AKM8963_DEBUG_IF
1086 __ATTR(mode, 0220, NULL, akm8963_mode_store),
1087 __ATTR(bdata, 0440, akm8963_bdata_show, NULL),
1088 __ATTR(asa, 0440, akm8963_asa_show, NULL),
1093 #define __BIN_ATTR(name_, mode_, size_, private_, read_, write_) \
1095 .attr = { .name = __stringify(name_), .mode = mode_ }, \
1097 .private = private_, \
1102 #define __BIN_ATTR_NULL \
1104 .attr = { .name = NULL }, \
1107 static struct bin_attribute akm8963_bin_attributes[] = {
1108 __BIN_ATTR(accel, 0220, 6, NULL,
1109 NULL, akm8963_bin_accel_write),
1113 static char const *const compass_class_name = "compass";
1114 static char const *const akm8963_device_name = "akm8963";
1115 static char const *const device_link_name = "i2c";
1116 static dev_t const akm8963_device_dev_t = MKDEV(MISC_MAJOR, 240);
1118 static int create_sysfs_interfaces(struct akm8963_data *akm)
1127 akm->compass = class_create(THIS_MODULE, compass_class_name);
1128 if (IS_ERR(akm->compass)) {
1129 err = PTR_ERR(akm->compass);
1130 goto exit_class_create_failed;
1133 akm->class_dev = device_create(
1136 akm8963_device_dev_t,
1138 akm8963_device_name);
1139 if (IS_ERR(akm->class_dev)) {
1140 err = PTR_ERR(akm->class_dev);
1141 goto exit_class_device_create_failed;
1144 err = sysfs_create_link(
1145 &akm->class_dev->kobj,
1146 &akm->i2c->dev.kobj,
1149 goto exit_sysfs_create_link_failed;
1151 err = create_device_attributes(
1153 akm8963_attributes);
1155 goto exit_device_attributes_create_failed;
1157 err = create_device_binary_attributes(
1158 &akm->class_dev->kobj,
1159 akm8963_bin_attributes);
1161 goto exit_device_binary_attributes_create_failed;
1165 exit_device_binary_attributes_create_failed:
1166 remove_device_attributes(akm->class_dev, akm8963_attributes);
1167 exit_device_attributes_create_failed:
1168 sysfs_remove_link(&akm->class_dev->kobj, device_link_name);
1169 exit_sysfs_create_link_failed:
1170 device_destroy(akm->compass, akm8963_device_dev_t);
1171 exit_class_device_create_failed:
1172 akm->class_dev = NULL;
1173 class_destroy(akm->compass);
1174 exit_class_create_failed:
1175 akm->compass = NULL;
1179 static void remove_sysfs_interfaces(struct akm8963_data *akm)
1184 if (NULL != akm->class_dev) {
1185 remove_device_binary_attributes(
1186 &akm->class_dev->kobj,
1187 akm8963_bin_attributes);
1188 remove_device_attributes(
1190 akm8963_attributes);
1192 &akm->class_dev->kobj,
1194 akm->class_dev = NULL;
1196 if (NULL != akm->compass) {
1199 akm8963_device_dev_t);
1200 class_destroy(akm->compass);
1201 akm->compass = NULL;
1206 /***** akm input device functions ***********************************/
1207 static int akm8963_input_init(
1208 struct input_dev **input)
1212 /* Declare input device */
1213 *input = input_allocate_device();
1217 /* Setup input device */
1218 set_bit(EV_ABS, (*input)->evbit);
1219 /* Accelerometer (720 x 16G)*/
1220 input_set_abs_params(*input, ABS_X,
1221 -11520, 11520, 0, 0);
1222 input_set_abs_params(*input, ABS_Y,
1223 -11520, 11520, 0, 0);
1224 input_set_abs_params(*input, ABS_Z,
1225 -11520, 11520, 0, 0);
1226 input_set_abs_params(*input, ABS_THROTTLE,
1228 /* Magnetic field (-81900, 81900) -> limited to 16bit*/
1229 input_set_abs_params(*input, ABS_RX,
1230 -32768, 32767, 0, 0);
1231 input_set_abs_params(*input, ABS_RY,
1232 -32768, 32767, 0, 0);
1233 input_set_abs_params(*input, ABS_RZ,
1234 -32768, 32767, 0, 0);
1235 input_set_abs_params(*input, ABS_RUDDER,
1237 /* Orientation (yaw:0,360 pitch:-180,180 roll:-90,90) */
1238 input_set_abs_params(*input, ABS_HAT0X,
1240 input_set_abs_params(*input, ABS_HAT0Y,
1241 -11520, 11520, 0, 0);
1242 input_set_abs_params(*input, ABS_HAT1X,
1244 input_set_abs_params(*input, ABS_HAT1Y,
1248 (*input)->name = "compass";
1251 err = input_register_device(*input);
1253 input_free_device(*input);
1260 /***** akm functions ************************************************/
1262 static irqreturn_t akm8963_irq(int irq, void *handle)
1264 struct akm8963_data *akm = handle;
1265 char buffer[SENSOR_DATA_SIZE];
1268 memset(buffer, 0, sizeof(buffer));
1269 buffer[0] = AK8963_REG_ST1;
1270 err = akm8963_i2c_rxdata(akm->i2c, buffer, SENSOR_DATA_SIZE);
1272 dev_err(&akm->i2c->dev, "%s failed.", __func__);
1276 if ((buffer[0] & 0x01) != 0x01) {
1277 dev_err(&akm->i2c->dev, "%s ST is not set.", __func__);
1281 mutex_lock(&akm->sensor_mutex);
1282 memcpy(akm->sense_data, buffer, SENSOR_DATA_SIZE);
1283 mutex_unlock(&akm->sensor_mutex);
1285 atomic_set(&akm->drdy, 1);
1286 atomic_set(&akm->is_busy, 0);
1287 wake_up(&akm->drdy_wq);
1293 static void akm8963_delayed_work(struct work_struct *work)
1295 struct akm8963_data *akm = container_of(
1296 work, struct akm8963_data, work.work);
1298 akm8963_irq(akm->irq, akm);
1301 static int akm8963_suspend(struct device *dev)
1303 struct akm8963_data *akm = dev_get_drvdata(dev);
1304 dev_dbg(&akm->i2c->dev, "suspended\n");
1309 static int akm8963_resume(struct device *dev)
1311 struct akm8963_data *akm = dev_get_drvdata(dev);
1312 dev_dbg(&akm->i2c->dev, "resumed\n");
1317 int akm8963_probe(struct i2c_client *client, const struct i2c_device_id *id)
1319 struct akm8963_platform_data *pdata;
1323 dev_dbg(&client->dev, "start probing.");
1325 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1326 dev_err(&client->dev, "%s: check_functionality failed.", __func__);
1331 /* Allocate memory for driver data */
1332 s_akm = kzalloc(sizeof(struct akm8963_data), GFP_KERNEL);
1334 dev_err(&client->dev, "%s: memory allocation failed.", __func__);
1339 /***** Set layout information *****/
1340 pdata = client->dev.platform_data;
1342 /* Platform data is available. copy its value to local. */
1343 s_akm->layout = pdata->layout;
1344 s_akm->outbit = pdata->outbit;
1345 s_akm->rstn = pdata->gpio_RST;
1347 /* Platform data is not available.
1348 Layout and Outbit information should be set by each application. */
1349 dev_dbg(&client->dev, "%s: No platform data.", __func__);
1355 /***** GPIO init & Hard reset*****/
1356 if(pdata->gpio_DRDY != 0) {
1357 gpio_request(pdata->gpio_DRDY, "akm8963_drdy");
1358 gpio_direction_input(pdata->gpio_DRDY);
1359 client->irq = gpio_to_irq(pdata->gpio_DRDY);
1361 if(pdata->gpio_RST != 0) {
1362 gpio_request(pdata->gpio_RST, "akm8963_rstn");
1363 gpio_direction_output( pdata->gpio_RST, 0);
1365 gpio_direction_output( pdata->gpio_RST, 1);
1366 /* Device will be accessible 50-100 us after */
1370 /***** I2C initialization *****/
1371 s_akm->i2c = client;
1372 /* set inv bypass */
1373 #ifdef CONFIG_AK8963_BYPASS_MODE
1374 set_inv_bypass_init(client);
1376 /* check connection */
1377 err = akm8963_i2c_check_device(client);
1380 /* set client data */
1381 i2c_set_clientdata(client, s_akm);
1385 err = akm8963_input_init(&s_akm->input);
1387 dev_err(&client->dev,
1388 "%s: input_dev register failed", __func__);
1391 input_set_drvdata(s_akm->input, s_akm);
1394 /**** initialize variables in akm8963_data *****/
1395 init_waitqueue_head(&s_akm->drdy_wq);
1396 init_waitqueue_head(&s_akm->open_wq);
1398 mutex_init(&s_akm->sensor_mutex);
1399 mutex_init(&s_akm->accel_mutex);
1400 mutex_init(&s_akm->val_mutex);
1402 atomic_set(&s_akm->active, 0);
1403 atomic_set(&s_akm->is_busy, 0);
1404 atomic_set(&s_akm->drdy, 0);
1405 atomic_set(&s_akm->suspend, 0);
1407 s_akm->enable_flag = 0;
1408 for (i=0; i<AKM_NUM_SENSORS; i++)
1409 s_akm->delay[i] = -1;
1412 /***** IRQ setup *****/
1413 s_akm->irq = client->irq;
1415 if (s_akm->irq == 0) {
1416 dev_dbg(&client->dev, "%s: IRQ is not set.", __func__);
1417 /* Use timer to notify measurement end */
1418 INIT_DELAYED_WORK(&s_akm->work, akm8963_delayed_work);
1420 err = request_threaded_irq(
1424 IRQF_TRIGGER_HIGH|IRQF_ONESHOT,
1425 dev_name(&client->dev),
1428 dev_err(&client->dev,
1429 "%s: request irq failed.", __func__);
1435 err = misc_register(&akm8963_dev);
1437 dev_err(&client->dev,
1438 "%s: akm8963_dev register failed", __func__);
1443 err = create_sysfs_interfaces(s_akm);
1445 dev_err(&client->dev,
1446 "%s: create sysfs failed.", __func__);
1450 dev_info(&client->dev, "successfully probed.");
1454 misc_deregister(&akm8963_dev);
1457 free_irq(s_akm->irq, s_akm);
1459 input_unregister_device(s_akm->input);
1462 if(pdata->gpio_RST != 0)
1463 gpio_free(pdata->gpio_RST);
1464 if(pdata->gpio_DRDY != 0)
1465 gpio_free(pdata->gpio_DRDY);
1472 static int akm8963_remove(struct i2c_client *client)
1474 struct akm8963_data *akm = i2c_get_clientdata(client);
1476 remove_sysfs_interfaces(akm);
1477 if (misc_deregister(&akm8963_dev) < 0)
1478 dev_err(&client->dev, "misc deregister failed.");
1480 free_irq(akm->irq, akm);
1481 input_unregister_device(akm->input);
1483 dev_info(&client->dev, "successfully removed.");
1487 static const struct i2c_device_id akm8963_id[] = {
1488 {AKM8963_I2C_NAME, 0 },
1492 static const struct dev_pm_ops akm8963_pm_ops = {
1493 .suspend = akm8963_suspend,
1494 .resume = akm8963_resume,
1497 static struct i2c_driver akm8963_driver = {
1498 .probe = akm8963_probe,
1499 .remove = akm8963_remove,
1500 .id_table = akm8963_id,
1502 .name = AKM8963_I2C_NAME,
1503 .pm = &akm8963_pm_ops,
1507 static int __init akm8963_init(void)
1509 printk(KERN_INFO "AKM8963 compass driver: initialize.");
1510 return i2c_add_driver(&akm8963_driver);
1513 static void __exit akm8963_exit(void)
1515 printk(KERN_INFO "AKM8963 compass driver: release.");
1516 i2c_del_driver(&akm8963_driver);
1518 #ifdef CONFIG_AK8963_BYPASS_MODE
1519 late_initcall(akm8963_init);
1521 module_init(akm8963_init);
1523 module_exit(akm8963_exit);