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 /* akm8975_wait_for_data_ready() revice IRQ signal.
227 * But, akm8975 is not ready to send magnetic data.
231 ret = i2c_smbus_read_i2c_block_data(akm->this_client,
232 AK8975_REG_ST1, sizeof(data), data);
233 if (ret != sizeof(data)) {
234 pr_err("%s: failed to read %d bytes of mag data\n",
235 __func__, sizeof(data));
240 if (data[0] & 0x01) {
241 raw_data[0] = (data[2] << 8) + data[1];
242 raw_data[1] = (data[4] << 8) + data[3];
243 raw_data[2] = (data[6] << 8) + data[5];
245 for (i = 0; i < 3; i++) {
246 *x += (position_map[akm->position][0][i] * raw_data[i]);
247 *y += (position_map[akm->position][1][i] * raw_data[i]);
248 *z += (position_map[akm->position][2][i] * raw_data[i]);
251 pr_err("%s: invalid raw data(st1 = %d)\n",
252 __func__, data[0] & 0x01);
256 mutex_unlock(&akm->lock);
261 static ssize_t akmd_read(struct file *file, char __user *buf,
262 size_t count, loff_t *pos)
264 struct akm8975_data *akm = container_of(file->private_data,
265 struct akm8975_data, akmd_device);
266 short x = 0, y = 0, z = 0;
269 ret = akm8975_get_raw_data(akm, &x, &y, &z);
271 pr_err("%s: failed to get raw data %d\n", __func__, ret);
273 return sprintf(buf, "%d,%d,%d\n", x, y, z);
276 static long akmd_ioctl(struct file *file, unsigned int cmd,
279 void __user *argp = (void __user *)arg;
280 struct akm8975_data *akm = container_of(file->private_data,
281 struct akm8975_data, akmd_device);
284 short raw_data[3] = {0, };
285 short adjust_raw[3] = {0,};
286 #ifdef MAGNETIC_LOGGING
290 char raw[RWBUF_SIZE];
296 ret = akmd_copy_in(cmd, argp, rwbuf.raw, sizeof(rwbuf));
301 case ECS_IOCTL_WRITE:
302 if ((rwbuf.raw[0] < 2) || (rwbuf.raw[0] > (RWBUF_SIZE - 1)))
304 if (copy_from_user(&rwbuf.raw[2], argp+2, rwbuf.raw[0]-1))
307 ret = i2c_smbus_write_i2c_block_data(akm->this_client,
313 if ((rwbuf.raw[0] < 1) || (rwbuf.raw[0] > (RWBUF_SIZE - 1)))
316 ret = i2c_smbus_read_i2c_block_data(akm->this_client,
322 if (copy_to_user(argp+1, rwbuf.raw+1, rwbuf.raw[0]))
325 case ECS_IOCTL_SET_MODE:
326 mutex_lock(&akm->lock);
327 ret = akm8975_ecs_set_mode(akm, rwbuf.mode);
328 mutex_unlock(&akm->lock);
330 case ECS_IOCTL_GETDATA:
331 mutex_lock(&akm->lock);
332 ret = akm8975_wait_for_data_ready(akm);
334 mutex_unlock(&akm->lock);
337 ret = i2c_smbus_read_i2c_block_data(akm->this_client,
342 #ifdef MAGNETIC_LOGGING
343 x = (rwbuf.data[2] << 8) + rwbuf.data[1];
344 y = (rwbuf.data[4] << 8) + rwbuf.data[3];
345 z = (rwbuf.data[6] << 8) + rwbuf.data[5];
347 pr_info("%s:ST1=%d, x=%d, y=%d, z=%d, ST2=%d\n",
348 __func__, rwbuf.data[0], x, y, z, rwbuf.data[7]);
351 raw_data[0] = (rwbuf.data[2] << 8) + rwbuf.data[1];
352 raw_data[1] = (rwbuf.data[4] << 8) + rwbuf.data[3];
353 raw_data[2] = (rwbuf.data[6] << 8) + rwbuf.data[5];
355 for (i = 0; i < 3; i++) {
357 (position_map[akm->position][0][i]
360 (position_map[akm->position][1][i]
363 (position_map[akm->position][2][i]
366 #ifdef MAGNETIC_LOGGING
367 pr_info("%s:adjusted x=%d, y=%d, z=%d\n",
368 __func__, adjust_raw[0], adjust_raw[1], adjust_raw[2]);
371 rwbuf.data[1] = adjust_raw[0] & 0x00ff;
372 rwbuf.data[2] = (adjust_raw[0] & 0xff00) >> 8;
374 rwbuf.data[3] = adjust_raw[1] & 0x00ff;
375 rwbuf.data[4] = (adjust_raw[1] & 0xff00) >> 8;
377 rwbuf.data[5] = adjust_raw[2] & 0x00ff;
378 rwbuf.data[6] = (adjust_raw[2] & 0xff00) >> 8;
379 mutex_unlock(&akm->lock);
380 if (ret != sizeof(rwbuf.data)) {
381 pr_err("%s : failed to read %d bytes of mag data\n",
382 __func__, sizeof(rwbuf.data));
393 return akmd_copy_out(cmd, argp, rwbuf.raw, sizeof(rwbuf));
396 static const struct file_operations akmd_fops = {
397 .owner = THIS_MODULE,
398 .open = nonseekable_open,
400 .unlocked_ioctl = akmd_ioctl,
403 static int akm8975_setup_irq(struct akm8975_data *akm)
406 struct akm8975_platform_data *pdata = akm->pdata;
409 if (akm->this_client->irq)
410 irq = akm->this_client->irq;
412 rc = gpio_request(pdata->gpio_data_ready_int, "gpio_akm_int");
414 pr_err("%s: gpio %d request failed (%d)\n",
415 __func__, pdata->gpio_data_ready_int, rc);
419 rc = gpio_direction_input(pdata->gpio_data_ready_int);
421 pr_err("%s: failed to set gpio %d as input (%d)\n",
422 __func__, pdata->gpio_data_ready_int, rc);
423 goto err_request_irq;
426 irq = gpio_to_irq(pdata->gpio_data_ready_int);
428 /* trigger high so we don't miss initial interrupt if it
431 rc = request_irq(irq, akm8975_irq_handler,
432 IRQF_TRIGGER_RISING | IRQF_ONESHOT, "akm_int", akm);
434 pr_err("%s: request_irq(%d) failed for gpio %d (%d)\n",
436 pdata->gpio_data_ready_int, rc);
437 goto err_request_irq;
440 /* start with interrupt disabled until the driver is enabled */
442 akm8975_disable_irq(akm);
447 gpio_free(pdata->gpio_data_ready_int);
453 static void ak8975c_selftest(struct akm8975_data *ak_data)
458 mutex_lock(&ak_data->lock);
459 /* read device info */
460 i2c_smbus_read_i2c_block_data(ak_data->this_client,
461 AK8975_REG_WIA, 2, buf);
462 pr_info("%s: device id = 0x%x, info = 0x%x\n",
463 __func__, buf[0], buf[1]);
465 /* set ATSC self test bit to 1 */
466 i2c_smbus_write_byte_data(ak_data->this_client,
467 AK8975_REG_ASTC, 0x40);
469 /* start self test */
470 i2c_smbus_write_byte_data(ak_data->this_client,
472 REG_CNTL_MODE_SELF_TEST);
474 /* wait for data ready */
477 if (i2c_smbus_read_byte_data(ak_data->this_client,
478 AK8975_REG_ST1) == 1) {
483 i2c_smbus_read_i2c_block_data(ak_data->this_client,
484 AK8975_REG_HXL, sizeof(buf), buf);
486 /* set ATSC self test bit to 0 */
487 i2c_smbus_write_byte_data(ak_data->this_client,
488 AK8975_REG_ASTC, 0x00);
490 mutex_unlock(&ak_data->lock);
492 x = buf[0] | (buf[1] << 8);
493 y = buf[2] | (buf[3] << 8);
494 z = buf[4] | (buf[5] << 8);
496 /* Hadj = (H*(Asa+128))/256 */
497 x = (x*(ak_data->asa[0] + 128)) >> 8;
498 y = (y*(ak_data->asa[1] + 128)) >> 8;
499 z = (z*(ak_data->asa[2] + 128)) >> 8;
501 pr_info("%s: self test x = %d, y = %d, z = %d\n",
503 if ((x >= -100) && (x <= 100))
504 pr_info("%s: x passed self test, expect -100<=x<=100\n",
507 pr_info("%s: x failed self test, expect -100<=x<=100\n",
509 if ((y >= -100) && (y <= 100))
510 pr_info("%s: y passed self test, expect -100<=y<=100\n",
513 pr_info("%s: y failed self test, expect -100<=y<=100\n",
515 if ((z >= -1000) && (z <= -300))
516 pr_info("%s: z passed self test, expect -1000<=z<=-300\n",
519 pr_info("%s: z failed self test, expect -1000<=z<=-300\n",
522 if (((x >= -100) && (x <= 100)) && ((y >= -100) && (y <= 100)) &&
523 ((z >= -1000) && (z <= -300)))
524 ak8975_selftest_passed = 1;
531 static ssize_t ak8975c_get_asa(struct device *dev,
532 struct device_attribute *attr, char *buf)
534 struct akm8975_data *ak_data = dev_get_drvdata(dev);
536 return sprintf(buf, "%d, %d, %d\n", ak_data->asa[0],\
537 ak_data->asa[1], ak_data->asa[2]);
540 static ssize_t ak8975c_get_selftest(struct device *dev,
541 struct device_attribute *attr, char *buf)
543 ak8975c_selftest(dev_get_drvdata(dev));
544 return sprintf(buf, "%d, %d, %d, %d\n",\
545 ak8975_selftest_passed, sf_x, sf_y, sf_z);
548 static ssize_t ak8975c_check_registers(struct device *dev,
549 struct device_attribute *attr, char *strbuf)
551 struct akm8975_data *ak_data = dev_get_drvdata(dev);
555 i2c_smbus_write_byte_data(ak_data->this_client,\
556 AK8975_REG_CNTL, REG_CNTL_MODE_POWER_DOWN);
559 i2c_smbus_read_i2c_block_data(ak_data->this_client,
560 AK8975_REG_WIA, 11, buf);
562 buf[11] = i2c_smbus_read_byte_data(ak_data->this_client,
564 buf[12] = i2c_smbus_read_byte_data(ak_data->this_client,
568 return sprintf(strbuf, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
569 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
570 buf[6], buf[7], buf[8], buf[9], buf[10], buf[11],
574 static ssize_t ak8975c_check_cntl(struct device *dev,
575 struct device_attribute *attr, char *strbuf)
577 struct akm8975_data *ak_data = dev_get_drvdata(dev);
582 err = i2c_smbus_write_byte_data(ak_data->this_client,\
583 AK8975_REG_CNTL, REG_CNTL_MODE_POWER_DOWN);
585 buf = i2c_smbus_read_byte_data(ak_data->this_client,
589 return sprintf(strbuf, "%s\n", (!buf ? "OK" : "NG"));
592 static ssize_t ak8975c_get_status(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 struct akm8975_data *ak_data = dev_get_drvdata(dev);
598 if ((ak_data->asa[0] == 0) | (ak_data->asa[0] == 0xff)\
599 | (ak_data->asa[1] == 0) | (ak_data->asa[1] == 0xff)\
600 | (ak_data->asa[2] == 0) | (ak_data->asa[2] == 0xff))
605 return sprintf(buf, "%s\n", (success ? "OK" : "NG"));
608 static ssize_t ak8975_adc(struct device *dev,
609 struct device_attribute *attr, char *strbuf)
611 struct akm8975_data *ak_data = dev_get_drvdata(dev);
613 short raw_data[3] = {0,};
614 s16 x = 0, y = 0, z = 0;
618 /* start ADC conversion */
619 err = i2c_smbus_write_byte_data(ak_data->this_client,
620 AK8975_REG_CNTL, REG_CNTL_MODE_ONCE);
622 /* wait for ADC conversion to complete */
623 err = akm8975_wait_for_data_ready(ak_data);
625 pr_err("%s: wait for data ready failed\n", __func__);
630 /* get the value and report it */
631 err = i2c_smbus_read_i2c_block_data(ak_data->this_client,
632 AK8975_REG_ST1, sizeof(buf), buf);
633 if (err != sizeof(buf)) {
634 pr_err("%s: read data over i2c failed\n", __func__);
638 /* buf[0] is status1, buf[7] is status2 */
639 if ((buf[0] == 0) | (buf[7] == 1))
644 raw_data[0] = buf[1] | (buf[2] << 8);
645 raw_data[1] = buf[3] | (buf[4] << 8);
646 raw_data[2] = buf[5] | (buf[6] << 8);
648 for (i = 0; i < 3; i++) {
649 x += (position_map[ak_data->position][0][i]*raw_data[i]);
650 y += (position_map[ak_data->position][1][i]*raw_data[i]);
651 z += (position_map[ak_data->position][2][i]*raw_data[i]);
654 pr_info("%s: raw x = %d, y = %d, z = %d\n", __func__, x, y, z);
655 return sprintf(strbuf, "%s, %d, %d, %d\n", (success ? "OK" : "NG"),\
660 static ssize_t ak8975_show_raw_data(struct device *dev,
661 struct device_attribute *attr, char *buf)
663 struct akm8975_data *akm = dev_get_drvdata(dev);
664 short x = 0, y = 0, z = 0;
667 err = akm8975_get_raw_data(akm, &x, &y, &z);
669 pr_err("%s: failed to get raw data\n", __func__);
671 return sprintf(buf, "%d,%d,%d\n", x, y, z);
674 static ssize_t ak8975_show_vendor(struct device *dev,
675 struct device_attribute *attr, char *buf)
677 return sprintf(buf, "%s\n", VENDOR);
680 static ssize_t ak8975_show_name(struct device *dev,
681 struct device_attribute *attr, char *buf)
683 return sprintf(buf, "%s\n", CHIP_ID);
686 static ssize_t ak8975_show_delay(struct device *dev,
687 struct device_attribute *attr, char *buf)
689 struct akm8975_data *akm = dev_get_drvdata(dev);
691 return sprintf(buf, "%lld\n", ktime_to_ns(akm->poll_delay));
694 static ssize_t ak8975_store_delay(struct device *dev,
695 struct device_attribute *attr, const char *buf, size_t count)
697 struct akm8975_data *akm = dev_get_drvdata(dev);
701 err = strict_strtoll(buf, 10, &delay_ns);
703 pr_err("%s, kstrtoint failed.", __func__);
705 mutex_lock(&akm->lock);
708 hrtimer_cancel(&akm->timer);
710 akm->poll_delay = ns_to_ktime(delay_ns);
713 hrtimer_start(&akm->timer, akm->poll_delay,
715 mutex_unlock(&akm->lock);
720 static ssize_t ak8975_show_enable(struct device *dev,
721 struct device_attribute *attr, char *buf)
723 struct akm8975_data *akm = dev_get_drvdata(dev);
725 return sprintf(buf, "%d\n", akm->enabled);
728 static ssize_t ak8975_store_enable(struct device *dev,
729 struct device_attribute *attr, const char *buf, size_t count)
731 struct akm8975_data *akm = dev_get_drvdata(dev);
734 err = kstrtoint(buf, 10, &enable);
736 pr_err("%s, kstrtoint failed.", __func__);
738 mutex_lock(&akm->lock);
741 hrtimer_start(&akm->timer, akm->poll_delay, HRTIMER_MODE_REL);
744 hrtimer_cancel(&akm->timer);
745 akm->enabled = false;
748 mutex_unlock(&akm->lock);
753 static DEVICE_ATTR(poll_delay, 0644,
754 ak8975_show_delay, ak8975_store_delay);
755 static DEVICE_ATTR(enable, 0644,
756 ak8975_show_enable, ak8975_store_enable);
758 static struct attribute *ak8975_sysfs_attrs[] = {
759 &dev_attr_enable.attr,
760 &dev_attr_poll_delay.attr,
764 static struct attribute_group ak8975_attribute_group = {
765 .attrs = ak8975_sysfs_attrs,
768 static DEVICE_ATTR(raw_data, 0664,
769 ak8975_show_raw_data, NULL);
770 static DEVICE_ATTR(vendor, 0664,
771 ak8975_show_vendor, NULL);
772 static DEVICE_ATTR(name, 0664,
773 ak8975_show_name, NULL);
776 static DEVICE_ATTR(asa, 0664,
777 ak8975c_get_asa, NULL);
778 static DEVICE_ATTR(selftest, 0664,
779 ak8975c_get_selftest, NULL);
780 static DEVICE_ATTR(chk_registers, 0664,
781 ak8975c_check_registers, NULL);
782 static DEVICE_ATTR(chk_cntl, 0664,
783 ak8975c_check_cntl, NULL);
784 static DEVICE_ATTR(status, 0664,
785 ak8975c_get_status, NULL);
786 static DEVICE_ATTR(adc, 0664,
790 static void akm8975_work_func(struct work_struct *work)
792 struct akm8975_data *akm =
793 container_of(work, struct akm8975_data, work);
794 short x = 0, y = 0, z = 0;
797 err = akm8975_get_raw_data(akm, &x, &y, &z);
799 pr_err("%s: failed to get raw data\n", __func__);
803 input_report_rel(akm->input_dev, REL_RX, x);
804 input_report_rel(akm->input_dev, REL_RY, y);
805 input_report_rel(akm->input_dev, REL_RZ, z);
806 input_sync(akm->input_dev);
809 static enum hrtimer_restart ak8975_timer_func(struct hrtimer *timer)
811 struct akm8975_data *akm = container_of(timer,
812 struct akm8975_data, timer);
813 queue_work(akm->work_queue, &akm->work);
814 hrtimer_forward_now(&akm->timer, akm->poll_delay);
816 return HRTIMER_RESTART;
819 int akm8975_probe(struct i2c_client *client,
820 const struct i2c_device_id *devid)
822 struct akm8975_data *akm;
825 pr_info("%s is called.\n", __func__);
826 if (client->dev.platform_data == NULL && client->irq == 0) {
827 dev_err(&client->dev, "platform data & irq are NULL.\n");
829 goto exit_platform_data_null;
832 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
833 dev_err(&client->dev, "I2C check failed, exiting.\n");
835 goto exit_check_functionality_failed;
838 akm = kzalloc(sizeof(struct akm8975_data), GFP_KERNEL);
840 dev_err(&client->dev,
841 "failed to allocate memory for module data\n");
843 goto exit_alloc_data_failed;
846 akm->pdata = client->dev.platform_data;
847 mutex_init(&akm->lock);
848 init_completion(&akm->data_ready);
849 akm->enabled = false;
851 i2c_set_clientdata(client, akm);
852 akm->this_client = client;
854 if (akm->pdata->magnetic_get_position)
855 akm->position = akm->pdata->magnetic_get_position();
857 akm->position = 2; /*default position */
858 pr_info("%s: position info:%d\n", __func__, akm->position);
860 err = akm8975_ecs_set_mode_power_down(akm);
862 pr_err("%s: akm8975_ecs_set_mode_power_down fail(err=%d)\n",
864 goto exit_set_mode_power_down_failed;
867 err = akm8975_setup_irq(akm);
869 pr_err("%s: could not setup irq\n", __func__);
873 akm->akmd_device.minor = MISC_DYNAMIC_MINOR;
874 akm->akmd_device.name = "akm8975";
875 akm->akmd_device.fops = &akmd_fops;
877 err = misc_register(&akm->akmd_device);
879 pr_err("%s, misc_register failed.\n", __func__);
880 goto exit_akmd_device_register_failed;
883 init_waitqueue_head(&akm->state_wq);
885 /* initialize input device */
886 akm->input_dev = input_allocate_device();
887 if (!akm->input_dev) {
888 pr_err("%s: Failed to allocate input device\n", __func__);
890 goto exit_akmd_alloc_input;
893 akm->input_dev->name = "magnetic_sensor";
894 input_set_drvdata(akm->input_dev, akm);
895 input_set_capability(akm->input_dev, EV_REL, REL_RX);
896 input_set_capability(akm->input_dev, EV_REL, REL_RY);
897 input_set_capability(akm->input_dev, EV_REL, REL_RZ);
899 err = input_register_device(akm->input_dev);
901 input_free_device(akm->input_dev);
902 pr_err("%s: Failed to register input device\n", __func__);
903 goto exit_akmd_input_register;
906 err = sysfs_create_group(&akm->input_dev->dev.kobj,
907 &ak8975_attribute_group);
909 pr_err("%s: Failed to create sysfs group\n", __func__);
910 goto err_create_sysfs;
913 /* initialize poll delay */
914 hrtimer_init(&akm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
915 akm->poll_delay = ns_to_ktime(200 * NSEC_PER_MSEC);
916 akm->timer.function = ak8975_timer_func;
918 create_singlethread_workqueue("ak8975_workqueue");
919 if (!akm->work_queue) {
921 pr_err("%s: Failed to create workqueue\n", __func__);
922 goto err_create_workqueue;
925 INIT_WORK(&akm->work, akm8975_work_func);
927 /* put into fuse access mode to read asa data */
928 err = i2c_smbus_write_byte_data(client, AK8975_REG_CNTL,
929 REG_CNTL_MODE_FUSE_ROM);
931 pr_err("%s: unable to enter fuse rom mode\n", __func__);
932 goto exit_i2c_failed;
935 err = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
936 sizeof(akm->asa), akm->asa);
937 if (err != sizeof(akm->asa)) {
938 pr_err("%s: unable to load factory sensitivity adjust values\n",
940 goto exit_i2c_failed;
942 pr_info("%s: asa_x = %d, asa_y = %d, asa_z = %d\n", __func__,
943 akm->asa[0], akm->asa[1], akm->asa[2]);
945 err = i2c_smbus_write_byte_data(client, AK8975_REG_CNTL,
946 REG_CNTL_MODE_POWER_DOWN);
948 dev_err(&client->dev, "Error in setting power down mode\n");
949 goto exit_i2c_failed;
952 akm->dev = sensors_classdev_register("magnetic_sensor");
953 if (IS_ERR(akm->dev)) {
954 pr_err("Failed to create device!");
955 goto exit_class_create_failed;
958 if (device_create_file(akm->dev, &dev_attr_raw_data) < 0) {
959 pr_err("Failed to create device file(%s)!\n",
960 dev_attr_raw_data.attr.name);
961 goto exit_device_create_raw_data;
964 if (device_create_file(akm->dev, &dev_attr_vendor) < 0) {
965 pr_err("Failed to create device file(%s)!\n",
966 dev_attr_name.attr.name);
967 goto exit_device_create_vendor;
970 if (device_create_file(akm->dev, &dev_attr_name) < 0) {
971 pr_err("Failed to create device file(%s)!\n",
972 dev_attr_raw_data.attr.name);
973 goto exit_device_create_name;
977 if (device_create_file(akm->dev, &dev_attr_adc) < 0) {
978 pr_err("Failed to create device file(%s)!\n",
979 dev_attr_adc.attr.name);
980 goto exit_device_create_file1;
983 if (device_create_file(akm->dev, &dev_attr_status) < 0) {
984 pr_err("Failed to create device file(%s)!\n",
985 dev_attr_status.attr.name);
986 goto exit_device_create_file2;
989 if (device_create_file(akm->dev, &dev_attr_asa) < 0) {
990 pr_err("Failed to create device file(%s)!\n",
991 dev_attr_asa.attr.name);
992 goto exit_device_create_file3;
994 if (device_create_file(akm->dev, &dev_attr_selftest) < 0) {
995 pr_err("Failed to create device file(%s)!\n",
996 dev_attr_selftest.attr.name);
997 goto exit_device_create_file4;
999 if (device_create_file(akm->dev,\
1000 &dev_attr_chk_registers) < 0) {
1001 pr_err("Failed to create device file(%s)!\n",
1002 dev_attr_chk_registers.attr.name);
1003 goto exit_device_create_file5;
1005 if (device_create_file(akm->dev, &dev_attr_chk_cntl) < 0) {
1006 pr_err("Failed to create device file(%s)!\n",
1007 dev_attr_chk_cntl.attr.name);
1008 goto exit_device_create_file6;
1011 dev_set_drvdata(akm->dev, akm);
1013 pr_info("%s is successful.\n", __func__);
1017 exit_device_create_file6:
1018 device_remove_file(akm->dev, &dev_attr_chk_registers);
1019 exit_device_create_file5:
1020 device_remove_file(akm->dev, &dev_attr_selftest);
1021 exit_device_create_file4:
1022 device_remove_file(akm->dev, &dev_attr_asa);
1023 exit_device_create_file3:
1024 device_remove_file(akm->dev, &dev_attr_status);
1025 exit_device_create_file2:
1026 device_remove_file(akm->dev, &dev_attr_adc);
1027 exit_device_create_file1:
1028 device_remove_file(akm->dev, &dev_attr_name);
1030 exit_device_create_name:
1031 device_remove_file(akm->dev, &dev_attr_vendor);
1032 exit_device_create_vendor:
1033 device_remove_file(akm->dev, &dev_attr_raw_data);
1034 exit_device_create_raw_data:
1035 sensors_classdev_unregister(akm->dev);
1036 exit_class_create_failed:
1038 destroy_workqueue(akm->work_queue);
1039 err_create_workqueue:
1040 sysfs_remove_group(&akm->input_dev->dev.kobj,
1041 &ak8975_attribute_group);
1043 input_unregister_device(akm->input_dev);
1044 exit_akmd_input_register:
1045 input_free_device(akm->input_dev);
1046 exit_akmd_alloc_input:
1047 misc_deregister(&akm->akmd_device);
1048 exit_akmd_device_register_failed:
1049 free_irq(akm->irq, akm);
1050 gpio_free(akm->pdata->gpio_data_ready_int);
1052 exit_set_mode_power_down_failed:
1053 mutex_destroy(&akm->lock);
1055 exit_alloc_data_failed:
1056 exit_check_functionality_failed:
1057 exit_platform_data_null:
1061 static int __devexit akm8975_remove(struct i2c_client *client)
1063 struct akm8975_data *akm = i2c_get_clientdata(client);
1066 hrtimer_cancel(&akm->timer);
1067 cancel_work_sync(&akm->work);
1071 device_remove_file(akm->dev, &dev_attr_adc);
1072 device_remove_file(akm->dev, &dev_attr_status);
1073 device_remove_file(akm->dev, &dev_attr_asa);
1074 device_remove_file(akm->dev, &dev_attr_selftest);
1075 device_remove_file(akm->dev, &dev_attr_chk_registers);
1076 device_remove_file(akm->dev, &dev_attr_chk_cntl);
1078 device_remove_file(akm->dev, &dev_attr_name);
1079 device_remove_file(akm->dev, &dev_attr_vendor);
1080 device_remove_file(akm->dev, &dev_attr_raw_data);
1082 sysfs_remove_group(&akm->input_dev->dev.kobj,
1083 &ak8975_attribute_group);
1084 input_unregister_device(akm->input_dev);
1085 input_free_device(akm->input_dev);
1086 sensors_classdev_unregister(akm->dev);
1087 misc_deregister(&akm->akmd_device);
1088 free_irq(akm->irq, akm);
1089 gpio_free(akm->pdata->gpio_data_ready_int);
1090 mutex_destroy(&akm->lock);
1095 static const struct i2c_device_id akm8975_id[] = {
1096 {AKM8975_I2C_NAME, 0 },
1100 static struct i2c_driver akm8975_driver = {
1101 .probe = akm8975_probe,
1102 .remove = akm8975_remove,
1103 .id_table = akm8975_id,
1105 .name = AKM8975_I2C_NAME,
1109 static int __init akm8975_init(void)
1111 return i2c_add_driver(&akm8975_driver);
1114 static void __exit akm8975_exit(void)
1116 i2c_del_driver(&akm8975_driver);
1119 module_init(akm8975_init);
1120 module_exit(akm8975_exit);
1122 MODULE_DESCRIPTION("AKM8975 compass driver");
1123 MODULE_AUTHOR("Samsung Electronics");
1124 MODULE_LICENSE("GPL");