2 * Copyright (C) 2010, Samsung Electronics Co. Ltd. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/i2c.h>
17 #include <linux/input.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/slab.h>
21 #include <linux/gpio.h>
22 #include <linux/miscdevice.h>
23 #include <linux/uaccess.h>
24 #include <linux/delay.h>
26 #include <linux/sensor/ak8975.h>
27 #include <linux/completion.h>
28 #include "ak8975-reg.h"
29 #include <linux/sensor/sensors_core.h>
31 #if defined(CONFIG_SLP) || defined(CONFIG_MACH_GC1)
36 #undef MAGNETIC_LOGGING
39 #define CHIP_ID "AK8975C"
41 #define AK8975_REG_CNTL 0x0A
42 #define REG_CNTL_MODE_SHIFT 0
43 #define REG_CNTL_MODE_MASK (0xF << REG_CNTL_MODE_SHIFT)
44 #define REG_CNTL_MODE_POWER_DOWN 0
45 #define REG_CNTL_MODE_ONCE 0x01
46 #define REG_CNTL_MODE_SELF_TEST 0x08
47 #define REG_CNTL_MODE_FUSE_ROM 0x0F
49 #define AK8975_REG_RSVC 0x0B
50 #define AK8975_REG_ASTC 0x0C
51 #define AK8975_REG_TS1 0x0D
52 #define AK8975_REG_TS2 0x0E
53 #define AK8975_REG_I2CDIS 0x0F
54 #define AK8975_REG_ASAX 0x10
55 #define AK8975_REG_ASAY 0x11
56 #define AK8975_REG_ASAZ 0x12
58 static const int position_map[][3][3] = {
59 {{-1, 0, 0}, { 0, -1, 0}, { 0, 0, 1} }, /* 0 top/upper-left */
60 {{ 0, -1, 0}, { 1, 0, 0}, { 0, 0, 1} }, /* 1 top/upper-right */
61 {{ 1, 0, 0}, { 0, 1, 0}, { 0, 0, 1} }, /* 2 top/lower-right */
62 {{ 0, 1, 0}, {-1, 0, 0}, { 0, 0, 1} }, /* 3 top/lower-left */
63 {{ 1, 0, 0}, { 0, -1, 0}, { 0, 0, -1} }, /* 4 bottom/upper-left */
64 {{ 0, 1, 0}, { 1, 0, 0}, { 0, 0, -1} }, /* 5 bottom/upper-right */
65 {{-1, 0, 0}, { 0, 1, 0}, { 0, 0, -1} }, /* 6 bottom/lower-right */
66 {{ 0, -1, 0}, {-1, 0, 0}, { 0, 0, -1} }, /* 7 bottom/lower-left*/
70 struct i2c_client *this_client;
71 struct akm8975_platform_data *pdata;
73 struct miscdevice akmd_device;
74 struct completion data_ready;
76 struct input_dev *input_dev;
78 struct workqueue_struct *work_queue;
79 struct work_struct work;
81 wait_queue_head_t state_wq;
89 static bool ak8975_selftest_passed;
90 static s16 sf_x, sf_y, sf_z;
93 static s32 akm8975_ecs_set_mode_power_down(struct akm8975_data *akm)
96 ret = i2c_smbus_write_byte_data(akm->this_client,
97 AK8975_REG_CNTL, AK8975_MODE_POWER_DOWN);
101 static int akm8975_ecs_set_mode(struct akm8975_data *akm, char mode)
106 case AK8975_MODE_SNG_MEASURE:
107 ret = i2c_smbus_write_byte_data(akm->this_client,
108 AK8975_REG_CNTL, AK8975_MODE_SNG_MEASURE);
110 case AK8975_MODE_FUSE_ACCESS:
111 ret = i2c_smbus_write_byte_data(akm->this_client,
112 AK8975_REG_CNTL, AK8975_MODE_FUSE_ACCESS);
114 case AK8975_MODE_POWER_DOWN:
115 ret = akm8975_ecs_set_mode_power_down(akm);
117 case AK8975_MODE_SELF_TEST:
118 ret = i2c_smbus_write_byte_data(akm->this_client,
119 AK8975_REG_CNTL, AK8975_MODE_SELF_TEST);
128 /* Wait at least 300us after changing mode. */
131 /* Workaround: Some sensors (defective?) require more delay */
137 static int akmd_copy_in(unsigned int cmd, void __user *argp,
138 void *buf, size_t buf_size)
142 if (_IOC_SIZE(cmd) > buf_size)
144 if (copy_from_user(buf, argp, _IOC_SIZE(cmd)))
149 static int akmd_copy_out(unsigned int cmd, void __user *argp,
150 void *buf, size_t buf_size)
152 if (!(cmd & IOC_OUT))
154 if (_IOC_SIZE(cmd) > buf_size)
156 if (copy_to_user(argp, buf, _IOC_SIZE(cmd)))
161 static void akm8975_disable_irq(struct akm8975_data *akm)
163 disable_irq(akm->irq);
164 if (try_wait_for_completion(&akm->data_ready)) {
165 /* we actually got the interrupt before we could disable it
166 * so we need to enable again to undo our disable since the
167 * irq_handler already disabled it
169 enable_irq(akm->irq);
173 static irqreturn_t akm8975_irq_handler(int irq, void *data)
175 struct akm8975_data *akm = data;
176 disable_irq_nosync(irq);
177 complete(&akm->data_ready);
181 static int akm8975_wait_for_data_ready(struct akm8975_data *akm)
183 int data_ready = gpio_get_value(akm->pdata->gpio_data_ready_int);
189 enable_irq(akm->irq);
191 err = wait_for_completion_timeout(&akm->data_ready, 2*HZ);
195 akm8975_disable_irq(akm);
198 pr_err("akm: wait timed out\n");
202 pr_err("akm: wait restart\n");
206 static int akm8975_get_raw_data(struct akm8975_data *akm,
207 short *x, short *y, short *z)
209 short raw_data[3] = {0, };
213 mutex_lock(&akm->lock);
214 err = akm8975_ecs_set_mode(akm, AK8975_MODE_SNG_MEASURE);
216 pr_err("%s: failed to set ecs mode\n", __func__);
220 err = akm8975_wait_for_data_ready(akm);
222 pr_err("%s: failed to wait for data ready\n", __func__);
226 ret = i2c_smbus_read_i2c_block_data(akm->this_client,
227 AK8975_REG_ST1, sizeof(data), data);
228 if (ret != sizeof(data)) {
229 pr_err("%s: failed to read %d bytes of mag data\n",
230 __func__, sizeof(data));
235 if (data[0] & 0x01) {
236 raw_data[0] = (data[2] << 8) + data[1];
237 raw_data[1] = (data[4] << 8) + data[3];
238 raw_data[2] = (data[6] << 8) + data[5];
240 for (i = 0; i < 3; i++) {
241 *x += (position_map[akm->position][0][i] * raw_data[i]);
242 *y += (position_map[akm->position][1][i] * raw_data[i]);
243 *z += (position_map[akm->position][2][i] * raw_data[i]);
246 pr_err("%s: invalid raw data(st1 = %d)\n",
247 __func__, data[0] & 0x01);
251 mutex_unlock(&akm->lock);
256 static ssize_t akmd_read(struct file *file, char __user *buf,
257 size_t count, loff_t *pos)
259 struct akm8975_data *akm = container_of(file->private_data,
260 struct akm8975_data, akmd_device);
261 short x = 0, y = 0, z = 0;
264 ret = akm8975_get_raw_data(akm, &x, &y, &z);
266 pr_err("%s: failed to get raw data %d\n", __func__, ret);
268 return sprintf(buf, "%d,%d,%d\n", x, y, z);
271 static long akmd_ioctl(struct file *file, unsigned int cmd,
274 void __user *argp = (void __user *)arg;
275 struct akm8975_data *akm = container_of(file->private_data,
276 struct akm8975_data, akmd_device);
279 short raw_data[3] = {0, };
280 short adjust_raw[3] = {0,};
281 #ifdef MAGNETIC_LOGGING
285 char raw[RWBUF_SIZE];
291 ret = akmd_copy_in(cmd, argp, rwbuf.raw, sizeof(rwbuf));
296 case ECS_IOCTL_WRITE:
297 if ((rwbuf.raw[0] < 2) || (rwbuf.raw[0] > (RWBUF_SIZE - 1)))
299 if (copy_from_user(&rwbuf.raw[2], argp+2, rwbuf.raw[0]-1))
302 ret = i2c_smbus_write_i2c_block_data(akm->this_client,
308 if ((rwbuf.raw[0] < 1) || (rwbuf.raw[0] > (RWBUF_SIZE - 1)))
311 ret = i2c_smbus_read_i2c_block_data(akm->this_client,
317 if (copy_to_user(argp+1, rwbuf.raw+1, rwbuf.raw[0]))
320 case ECS_IOCTL_SET_MODE:
321 mutex_lock(&akm->lock);
322 ret = akm8975_ecs_set_mode(akm, rwbuf.mode);
323 mutex_unlock(&akm->lock);
325 case ECS_IOCTL_GETDATA:
326 mutex_lock(&akm->lock);
327 ret = akm8975_wait_for_data_ready(akm);
329 mutex_unlock(&akm->lock);
332 ret = i2c_smbus_read_i2c_block_data(akm->this_client,
337 #ifdef MAGNETIC_LOGGING
338 x = (rwbuf.data[2] << 8) + rwbuf.data[1];
339 y = (rwbuf.data[4] << 8) + rwbuf.data[3];
340 z = (rwbuf.data[6] << 8) + rwbuf.data[5];
342 pr_info("%s:ST1=%d, x=%d, y=%d, z=%d, ST2=%d\n",
343 __func__, rwbuf.data[0], x, y, z, rwbuf.data[7]);
346 raw_data[0] = (rwbuf.data[2] << 8) + rwbuf.data[1];
347 raw_data[1] = (rwbuf.data[4] << 8) + rwbuf.data[3];
348 raw_data[2] = (rwbuf.data[6] << 8) + rwbuf.data[5];
350 for (i = 0; i < 3; i++) {
352 (position_map[akm->position][0][i]
355 (position_map[akm->position][1][i]
358 (position_map[akm->position][2][i]
361 #ifdef MAGNETIC_LOGGING
362 pr_info("%s:adjusted x=%d, y=%d, z=%d\n",
363 __func__, adjust_raw[0], adjust_raw[1], adjust_raw[2]);
366 rwbuf.data[1] = adjust_raw[0] & 0x00ff;
367 rwbuf.data[2] = (adjust_raw[0] & 0xff00) >> 8;
369 rwbuf.data[3] = adjust_raw[1] & 0x00ff;
370 rwbuf.data[4] = (adjust_raw[1] & 0xff00) >> 8;
372 rwbuf.data[5] = adjust_raw[2] & 0x00ff;
373 rwbuf.data[6] = (adjust_raw[2] & 0xff00) >> 8;
374 mutex_unlock(&akm->lock);
375 if (ret != sizeof(rwbuf.data)) {
376 pr_err("%s : failed to read %d bytes of mag data\n",
377 __func__, sizeof(rwbuf.data));
388 return akmd_copy_out(cmd, argp, rwbuf.raw, sizeof(rwbuf));
391 static const struct file_operations akmd_fops = {
392 .owner = THIS_MODULE,
393 .open = nonseekable_open,
395 .unlocked_ioctl = akmd_ioctl,
398 static int akm8975_setup_irq(struct akm8975_data *akm)
401 struct akm8975_platform_data *pdata = akm->pdata;
404 if (akm->this_client->irq)
405 irq = akm->this_client->irq;
407 rc = gpio_request(pdata->gpio_data_ready_int, "gpio_akm_int");
409 pr_err("%s: gpio %d request failed (%d)\n",
410 __func__, pdata->gpio_data_ready_int, rc);
414 rc = gpio_direction_input(pdata->gpio_data_ready_int);
416 pr_err("%s: failed to set gpio %d as input (%d)\n",
417 __func__, pdata->gpio_data_ready_int, rc);
418 goto err_request_irq;
421 irq = gpio_to_irq(pdata->gpio_data_ready_int);
423 /* trigger high so we don't miss initial interrupt if it
426 rc = request_irq(irq, akm8975_irq_handler,
427 IRQF_TRIGGER_RISING | IRQF_ONESHOT, "akm_int", akm);
429 pr_err("%s: request_irq(%d) failed for gpio %d (%d)\n",
431 pdata->gpio_data_ready_int, rc);
432 goto err_request_irq;
435 /* start with interrupt disabled until the driver is enabled */
437 akm8975_disable_irq(akm);
442 gpio_free(pdata->gpio_data_ready_int);
448 static void ak8975c_selftest(struct akm8975_data *ak_data)
453 mutex_lock(&ak_data->lock);
454 /* read device info */
455 i2c_smbus_read_i2c_block_data(ak_data->this_client,
456 AK8975_REG_WIA, 2, buf);
457 pr_info("%s: device id = 0x%x, info = 0x%x\n",
458 __func__, buf[0], buf[1]);
460 /* set ATSC self test bit to 1 */
461 i2c_smbus_write_byte_data(ak_data->this_client,
462 AK8975_REG_ASTC, 0x40);
464 /* start self test */
465 i2c_smbus_write_byte_data(ak_data->this_client,
467 REG_CNTL_MODE_SELF_TEST);
469 /* wait for data ready */
472 if (i2c_smbus_read_byte_data(ak_data->this_client,
473 AK8975_REG_ST1) == 1) {
478 i2c_smbus_read_i2c_block_data(ak_data->this_client,
479 AK8975_REG_HXL, sizeof(buf), buf);
481 /* set ATSC self test bit to 0 */
482 i2c_smbus_write_byte_data(ak_data->this_client,
483 AK8975_REG_ASTC, 0x00);
485 mutex_unlock(&ak_data->lock);
487 x = buf[0] | (buf[1] << 8);
488 y = buf[2] | (buf[3] << 8);
489 z = buf[4] | (buf[5] << 8);
491 /* Hadj = (H*(Asa+128))/256 */
492 x = (x*(ak_data->asa[0] + 128)) >> 8;
493 y = (y*(ak_data->asa[1] + 128)) >> 8;
494 z = (z*(ak_data->asa[2] + 128)) >> 8;
496 pr_info("%s: self test x = %d, y = %d, z = %d\n",
498 if ((x >= -100) && (x <= 100))
499 pr_info("%s: x passed self test, expect -100<=x<=100\n",
502 pr_info("%s: x failed self test, expect -100<=x<=100\n",
504 if ((y >= -100) && (y <= 100))
505 pr_info("%s: y passed self test, expect -100<=y<=100\n",
508 pr_info("%s: y failed self test, expect -100<=y<=100\n",
510 if ((z >= -1000) && (z <= -300))
511 pr_info("%s: z passed self test, expect -1000<=z<=-300\n",
514 pr_info("%s: z failed self test, expect -1000<=z<=-300\n",
517 if (((x >= -100) && (x <= 100)) && ((y >= -100) && (y <= 100)) &&
518 ((z >= -1000) && (z <= -300)))
519 ak8975_selftest_passed = 1;
526 static ssize_t ak8975c_get_asa(struct device *dev,
527 struct device_attribute *attr, char *buf)
529 struct akm8975_data *ak_data = dev_get_drvdata(dev);
531 return sprintf(buf, "%d, %d, %d\n", ak_data->asa[0],\
532 ak_data->asa[1], ak_data->asa[2]);
535 static ssize_t ak8975c_get_selftest(struct device *dev,
536 struct device_attribute *attr, char *buf)
538 ak8975c_selftest(dev_get_drvdata(dev));
539 return sprintf(buf, "%d, %d, %d, %d\n",\
540 ak8975_selftest_passed, sf_x, sf_y, sf_z);
543 static ssize_t ak8975c_check_registers(struct device *dev,
544 struct device_attribute *attr, char *strbuf)
546 struct akm8975_data *ak_data = dev_get_drvdata(dev);
550 i2c_smbus_write_byte_data(ak_data->this_client,\
551 AK8975_REG_CNTL, REG_CNTL_MODE_POWER_DOWN);
554 i2c_smbus_read_i2c_block_data(ak_data->this_client,
555 AK8975_REG_WIA, 11, buf);
557 buf[11] = i2c_smbus_read_byte_data(ak_data->this_client,
559 buf[12] = i2c_smbus_read_byte_data(ak_data->this_client,
563 return sprintf(strbuf, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
564 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
565 buf[6], buf[7], buf[8], buf[9], buf[10], buf[11],
569 static ssize_t ak8975c_check_cntl(struct device *dev,
570 struct device_attribute *attr, char *strbuf)
572 struct akm8975_data *ak_data = dev_get_drvdata(dev);
577 err = i2c_smbus_write_byte_data(ak_data->this_client,\
578 AK8975_REG_CNTL, REG_CNTL_MODE_POWER_DOWN);
580 buf = i2c_smbus_read_byte_data(ak_data->this_client,
584 return sprintf(strbuf, "%s\n", (!buf ? "OK" : "NG"));
587 static ssize_t ak8975c_get_status(struct device *dev,
588 struct device_attribute *attr, char *buf)
590 struct akm8975_data *ak_data = dev_get_drvdata(dev);
593 if ((ak_data->asa[0] == 0) | (ak_data->asa[0] == 0xff)\
594 | (ak_data->asa[1] == 0) | (ak_data->asa[1] == 0xff)\
595 | (ak_data->asa[2] == 0) | (ak_data->asa[2] == 0xff))
600 return sprintf(buf, "%s\n", (success ? "OK" : "NG"));
603 static ssize_t ak8975_adc(struct device *dev,
604 struct device_attribute *attr, char *strbuf)
606 struct akm8975_data *ak_data = dev_get_drvdata(dev);
608 short raw_data[3] = {0,};
609 s16 x = 0, y = 0, z = 0;
613 /* start ADC conversion */
614 err = i2c_smbus_write_byte_data(ak_data->this_client,
615 AK8975_REG_CNTL, REG_CNTL_MODE_ONCE);
617 /* wait for ADC conversion to complete */
618 err = akm8975_wait_for_data_ready(ak_data);
620 pr_err("%s: wait for data ready failed\n", __func__);
624 /* get the value and report it */
625 err = i2c_smbus_read_i2c_block_data(ak_data->this_client,
626 AK8975_REG_ST1, sizeof(buf), buf);
627 if (err != sizeof(buf)) {
628 pr_err("%s: read data over i2c failed\n", __func__);
632 /* buf[0] is status1, buf[7] is status2 */
633 if ((buf[0] == 0) | (buf[7] == 1))
638 raw_data[0] = buf[1] | (buf[2] << 8);
639 raw_data[1] = buf[3] | (buf[4] << 8);
640 raw_data[2] = buf[5] | (buf[6] << 8);
642 for (i = 0; i < 3; i++) {
643 x += (position_map[ak_data->position][0][i]*raw_data[i]);
644 y += (position_map[ak_data->position][1][i]*raw_data[i]);
645 z += (position_map[ak_data->position][2][i]*raw_data[i]);
648 pr_info("%s: raw x = %d, y = %d, z = %d\n", __func__, x, y, z);
649 return sprintf(strbuf, "%s, %d, %d, %d\n", (success ? "OK" : "NG"),\
654 static ssize_t ak8975_show_raw_data(struct device *dev,
655 struct device_attribute *attr, char *buf)
657 struct akm8975_data *akm = dev_get_drvdata(dev);
658 short x = 0, y = 0, z = 0;
661 err = akm8975_get_raw_data(akm, &x, &y, &z);
663 pr_err("%s: failed to get raw data\n", __func__);
665 return sprintf(buf, "%d,%d,%d\n", x, y, z);
668 static ssize_t ak8975_show_vendor(struct device *dev,
669 struct device_attribute *attr, char *buf)
671 return sprintf(buf, "%s\n", VENDOR);
674 static ssize_t ak8975_show_name(struct device *dev,
675 struct device_attribute *attr, char *buf)
677 return sprintf(buf, "%s\n", CHIP_ID);
680 static ssize_t ak8975_show_delay(struct device *dev,
681 struct device_attribute *attr, char *buf)
683 struct akm8975_data *akm = dev_get_drvdata(dev);
685 return sprintf(buf, "%lld\n", ktime_to_ns(akm->poll_delay));
688 static ssize_t ak8975_store_delay(struct device *dev,
689 struct device_attribute *attr, const char *buf, size_t count)
691 struct akm8975_data *akm = dev_get_drvdata(dev);
695 err = strict_strtoll(buf, 10, &delay_ns);
697 pr_err("%s, kstrtoint failed.", __func__);
699 mutex_lock(&akm->lock);
702 hrtimer_cancel(&akm->timer);
704 akm->poll_delay = ns_to_ktime(delay_ns);
707 hrtimer_start(&akm->timer, akm->poll_delay,
709 mutex_unlock(&akm->lock);
714 static ssize_t ak8975_show_enable(struct device *dev,
715 struct device_attribute *attr, char *buf)
717 struct akm8975_data *akm = dev_get_drvdata(dev);
719 return sprintf(buf, "%d\n", akm->enabled);
722 static ssize_t ak8975_store_enable(struct device *dev,
723 struct device_attribute *attr, const char *buf, size_t count)
725 struct akm8975_data *akm = dev_get_drvdata(dev);
728 err = kstrtoint(buf, 10, &enable);
730 pr_err("%s, kstrtoint failed.", __func__);
732 mutex_lock(&akm->lock);
735 hrtimer_start(&akm->timer, akm->poll_delay, HRTIMER_MODE_REL);
738 hrtimer_cancel(&akm->timer);
739 akm->enabled = false;
742 mutex_unlock(&akm->lock);
747 static DEVICE_ATTR(poll_delay, 0644,
748 ak8975_show_delay, ak8975_store_delay);
749 static DEVICE_ATTR(enable, 0644,
750 ak8975_show_enable, ak8975_store_enable);
752 static struct attribute *ak8975_sysfs_attrs[] = {
753 &dev_attr_enable.attr,
754 &dev_attr_poll_delay.attr,
758 static struct attribute_group ak8975_attribute_group = {
759 .attrs = ak8975_sysfs_attrs,
762 static DEVICE_ATTR(raw_data, 0664,
763 ak8975_show_raw_data, NULL);
764 static DEVICE_ATTR(vendor, 0664,
765 ak8975_show_vendor, NULL);
766 static DEVICE_ATTR(name, 0664,
767 ak8975_show_name, NULL);
770 static DEVICE_ATTR(asa, 0664,
771 ak8975c_get_asa, NULL);
772 static DEVICE_ATTR(selftest, 0664,
773 ak8975c_get_selftest, NULL);
774 static DEVICE_ATTR(chk_registers, 0664,
775 ak8975c_check_registers, NULL);
776 static DEVICE_ATTR(chk_cntl, 0664,
777 ak8975c_check_cntl, NULL);
778 static DEVICE_ATTR(status, 0664,
779 ak8975c_get_status, NULL);
780 static DEVICE_ATTR(adc, 0664,
784 static void akm8975_work_func(struct work_struct *work)
786 struct akm8975_data *akm =
787 container_of(work, struct akm8975_data, work);
788 short x = 0, y = 0, z = 0;
791 err = akm8975_get_raw_data(akm, &x, &y, &z);
793 pr_err("%s: failed to get raw data\n", __func__);
797 input_report_rel(akm->input_dev, REL_RX, x);
798 input_report_rel(akm->input_dev, REL_RY, y);
799 input_report_rel(akm->input_dev, REL_RZ, z);
800 input_sync(akm->input_dev);
803 static enum hrtimer_restart ak8975_timer_func(struct hrtimer *timer)
805 struct akm8975_data *akm = container_of(timer,
806 struct akm8975_data, timer);
807 queue_work(akm->work_queue, &akm->work);
808 hrtimer_forward_now(&akm->timer, akm->poll_delay);
810 return HRTIMER_RESTART;
813 int akm8975_probe(struct i2c_client *client,
814 const struct i2c_device_id *devid)
816 struct akm8975_data *akm;
819 pr_info("%s is called.\n", __func__);
820 if (client->dev.platform_data == NULL && client->irq == 0) {
821 dev_err(&client->dev, "platform data & irq are NULL.\n");
823 goto exit_platform_data_null;
826 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
827 dev_err(&client->dev, "I2C check failed, exiting.\n");
829 goto exit_check_functionality_failed;
832 akm = kzalloc(sizeof(struct akm8975_data), GFP_KERNEL);
834 dev_err(&client->dev,
835 "failed to allocate memory for module data\n");
837 goto exit_alloc_data_failed;
840 akm->pdata = client->dev.platform_data;
841 mutex_init(&akm->lock);
842 init_completion(&akm->data_ready);
843 akm->enabled = false;
845 i2c_set_clientdata(client, akm);
846 akm->this_client = client;
848 if (akm->pdata->magnetic_get_position)
849 akm->position = akm->pdata->magnetic_get_position();
851 akm->position = 2; /*default position */
852 pr_info("%s: position info:%d\n", __func__, akm->position);
854 err = akm8975_ecs_set_mode_power_down(akm);
856 pr_err("%s: akm8975_ecs_set_mode_power_down fail(err=%d)\n",
858 goto exit_set_mode_power_down_failed;
861 err = akm8975_setup_irq(akm);
863 pr_err("%s: could not setup irq\n", __func__);
867 akm->akmd_device.minor = MISC_DYNAMIC_MINOR;
868 akm->akmd_device.name = "akm8975";
869 akm->akmd_device.fops = &akmd_fops;
871 err = misc_register(&akm->akmd_device);
873 pr_err("%s, misc_register failed.\n", __func__);
874 goto exit_akmd_device_register_failed;
877 init_waitqueue_head(&akm->state_wq);
879 /* initialize input device */
880 akm->input_dev = input_allocate_device();
881 if (!akm->input_dev) {
882 pr_err("%s: Failed to allocate input device\n", __func__);
884 goto exit_akmd_alloc_input;
887 akm->input_dev->name = "magnetic_sensor";
888 input_set_drvdata(akm->input_dev, akm);
889 input_set_capability(akm->input_dev, EV_REL, REL_RX);
890 input_set_capability(akm->input_dev, EV_REL, REL_RY);
891 input_set_capability(akm->input_dev, EV_REL, REL_RZ);
893 err = input_register_device(akm->input_dev);
895 input_free_device(akm->input_dev);
896 pr_err("%s: Failed to register input device\n", __func__);
897 goto exit_akmd_input_register;
900 err = sysfs_create_group(&akm->input_dev->dev.kobj,
901 &ak8975_attribute_group);
903 pr_err("%s: Failed to create sysfs group\n", __func__);
904 goto err_create_sysfs;
907 /* initialize poll delay */
908 hrtimer_init(&akm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
909 akm->poll_delay = ns_to_ktime(200 * NSEC_PER_MSEC);
910 akm->timer.function = ak8975_timer_func;
912 create_singlethread_workqueue("ak8975_workqueue");
913 if (!akm->work_queue) {
915 pr_err("%s: Failed to create workqueue\n", __func__);
916 goto err_create_workqueue;
919 INIT_WORK(&akm->work, akm8975_work_func);
921 /* put into fuse access mode to read asa data */
922 err = i2c_smbus_write_byte_data(client, AK8975_REG_CNTL,
923 REG_CNTL_MODE_FUSE_ROM);
925 pr_err("%s: unable to enter fuse rom mode\n", __func__);
926 goto exit_i2c_failed;
929 err = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
930 sizeof(akm->asa), akm->asa);
931 if (err != sizeof(akm->asa)) {
932 pr_err("%s: unable to load factory sensitivity adjust values\n",
934 goto exit_i2c_failed;
936 pr_info("%s: asa_x = %d, asa_y = %d, asa_z = %d\n", __func__,
937 akm->asa[0], akm->asa[1], akm->asa[2]);
939 err = i2c_smbus_write_byte_data(client, AK8975_REG_CNTL,
940 REG_CNTL_MODE_POWER_DOWN);
942 dev_err(&client->dev, "Error in setting power down mode\n");
943 goto exit_i2c_failed;
946 akm->dev = sensors_classdev_register("magnetic_sensor");
947 if (IS_ERR(akm->dev)) {
948 pr_err("Failed to create device!");
949 goto exit_class_create_failed;
952 if (device_create_file(akm->dev, &dev_attr_raw_data) < 0) {
953 pr_err("Failed to create device file(%s)!\n",
954 dev_attr_raw_data.attr.name);
955 goto exit_device_create_raw_data;
958 if (device_create_file(akm->dev, &dev_attr_vendor) < 0) {
959 pr_err("Failed to create device file(%s)!\n",
960 dev_attr_name.attr.name);
961 goto exit_device_create_vendor;
964 if (device_create_file(akm->dev, &dev_attr_name) < 0) {
965 pr_err("Failed to create device file(%s)!\n",
966 dev_attr_raw_data.attr.name);
967 goto exit_device_create_name;
971 if (device_create_file(akm->dev, &dev_attr_adc) < 0) {
972 pr_err("Failed to create device file(%s)!\n",
973 dev_attr_adc.attr.name);
974 goto exit_device_create_file1;
977 if (device_create_file(akm->dev, &dev_attr_status) < 0) {
978 pr_err("Failed to create device file(%s)!\n",
979 dev_attr_status.attr.name);
980 goto exit_device_create_file2;
983 if (device_create_file(akm->dev, &dev_attr_asa) < 0) {
984 pr_err("Failed to create device file(%s)!\n",
985 dev_attr_asa.attr.name);
986 goto exit_device_create_file3;
988 if (device_create_file(akm->dev, &dev_attr_selftest) < 0) {
989 pr_err("Failed to create device file(%s)!\n",
990 dev_attr_selftest.attr.name);
991 goto exit_device_create_file4;
993 if (device_create_file(akm->dev,\
994 &dev_attr_chk_registers) < 0) {
995 pr_err("Failed to create device file(%s)!\n",
996 dev_attr_chk_registers.attr.name);
997 goto exit_device_create_file5;
999 if (device_create_file(akm->dev, &dev_attr_chk_cntl) < 0) {
1000 pr_err("Failed to create device file(%s)!\n",
1001 dev_attr_chk_cntl.attr.name);
1002 goto exit_device_create_file6;
1005 dev_set_drvdata(akm->dev, akm);
1007 pr_info("%s is successful.\n", __func__);
1011 exit_device_create_file6:
1012 device_remove_file(akm->dev, &dev_attr_chk_registers);
1013 exit_device_create_file5:
1014 device_remove_file(akm->dev, &dev_attr_selftest);
1015 exit_device_create_file4:
1016 device_remove_file(akm->dev, &dev_attr_asa);
1017 exit_device_create_file3:
1018 device_remove_file(akm->dev, &dev_attr_status);
1019 exit_device_create_file2:
1020 device_remove_file(akm->dev, &dev_attr_adc);
1021 exit_device_create_file1:
1022 device_remove_file(akm->dev, &dev_attr_name);
1024 exit_device_create_name:
1025 device_remove_file(akm->dev, &dev_attr_vendor);
1026 exit_device_create_vendor:
1027 device_remove_file(akm->dev, &dev_attr_raw_data);
1028 exit_device_create_raw_data:
1029 sensors_classdev_unregister(akm->dev);
1030 exit_class_create_failed:
1032 destroy_workqueue(akm->work_queue);
1033 err_create_workqueue:
1034 sysfs_remove_group(&akm->input_dev->dev.kobj,
1035 &ak8975_attribute_group);
1037 input_unregister_device(akm->input_dev);
1038 exit_akmd_input_register:
1039 input_free_device(akm->input_dev);
1040 exit_akmd_alloc_input:
1041 misc_deregister(&akm->akmd_device);
1042 exit_akmd_device_register_failed:
1043 free_irq(akm->irq, akm);
1044 gpio_free(akm->pdata->gpio_data_ready_int);
1046 exit_set_mode_power_down_failed:
1047 mutex_destroy(&akm->lock);
1049 exit_alloc_data_failed:
1050 exit_check_functionality_failed:
1051 exit_platform_data_null:
1055 static int __devexit akm8975_remove(struct i2c_client *client)
1057 struct akm8975_data *akm = i2c_get_clientdata(client);
1060 hrtimer_cancel(&akm->timer);
1061 cancel_work_sync(&akm->work);
1065 device_remove_file(akm->dev, &dev_attr_adc);
1066 device_remove_file(akm->dev, &dev_attr_status);
1067 device_remove_file(akm->dev, &dev_attr_asa);
1068 device_remove_file(akm->dev, &dev_attr_selftest);
1069 device_remove_file(akm->dev, &dev_attr_chk_registers);
1070 device_remove_file(akm->dev, &dev_attr_chk_cntl);
1072 device_remove_file(akm->dev, &dev_attr_name);
1073 device_remove_file(akm->dev, &dev_attr_vendor);
1074 device_remove_file(akm->dev, &dev_attr_raw_data);
1076 sysfs_remove_group(&akm->input_dev->dev.kobj,
1077 &ak8975_attribute_group);
1078 input_unregister_device(akm->input_dev);
1079 input_free_device(akm->input_dev);
1080 sensors_classdev_unregister(akm->dev);
1081 misc_deregister(&akm->akmd_device);
1082 free_irq(akm->irq, akm);
1083 gpio_free(akm->pdata->gpio_data_ready_int);
1084 mutex_destroy(&akm->lock);
1089 static const struct i2c_device_id akm8975_id[] = {
1090 {AKM8975_I2C_NAME, 0 },
1094 static struct i2c_driver akm8975_driver = {
1095 .probe = akm8975_probe,
1096 .remove = akm8975_remove,
1097 .id_table = akm8975_id,
1099 .name = AKM8975_I2C_NAME,
1103 static int __init akm8975_init(void)
1105 return i2c_add_driver(&akm8975_driver);
1108 static void __exit akm8975_exit(void)
1110 i2c_del_driver(&akm8975_driver);
1113 module_init(akm8975_init);
1114 module_exit(akm8975_exit);
1116 MODULE_DESCRIPTION("AKM8975 compass driver");
1117 MODULE_AUTHOR("Samsung Electronics");
1118 MODULE_LICENSE("GPL");