6f56b569eade0073c370e630ebc81083690c6748
[kernel/linux-3.0.git] / drivers / sensor / ak8975.c
1 /*
2  *      Copyright (C) 2010, Samsung Electronics Co. Ltd. All Rights Reserved.
3  *
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.
8  *
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.
13  *
14 */
15
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>
25 #include <linux/fs.h>
26 #include <linux/sensor/ak8975.h>
27 #include <linux/completion.h>
28 #include "ak8975-reg.h"
29 #include <linux/sensor/sensors_core.h>
30
31 #if defined(CONFIG_SLP) || defined(CONFIG_MACH_GC1)
32 #define FACTORY_TEST
33 #else
34 #undef FACTORY_TEST
35 #endif
36 #undef MAGNETIC_LOGGING
37
38 #define VENDOR          "AKM"
39 #define CHIP_ID         "AK8975C"
40
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
48
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
57
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*/
67 };
68
69 struct akm8975_data {
70         struct i2c_client *this_client;
71         struct akm8975_platform_data *pdata;
72         struct mutex lock;
73         struct miscdevice akmd_device;
74         struct completion data_ready;
75         struct device *dev;
76         struct input_dev *input_dev;
77         struct hrtimer timer;
78         struct workqueue_struct *work_queue;
79         struct work_struct work;
80         ktime_t poll_delay;
81         wait_queue_head_t state_wq;
82         u8 asa[3];
83         int irq;
84         int position;
85         bool enabled;
86 };
87
88 #ifdef FACTORY_TEST
89 static bool ak8975_selftest_passed;
90 static s16 sf_x, sf_y, sf_z;
91 #endif
92
93 static s32 akm8975_ecs_set_mode_power_down(struct akm8975_data *akm)
94 {
95         s32 ret;
96         ret = i2c_smbus_write_byte_data(akm->this_client,
97                         AK8975_REG_CNTL, AK8975_MODE_POWER_DOWN);
98         return ret;
99 }
100
101 static int akm8975_ecs_set_mode(struct akm8975_data *akm, char mode)
102 {
103         s32 ret;
104
105         switch (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);
109                 break;
110         case AK8975_MODE_FUSE_ACCESS:
111                 ret = i2c_smbus_write_byte_data(akm->this_client,
112                                 AK8975_REG_CNTL, AK8975_MODE_FUSE_ACCESS);
113                 break;
114         case AK8975_MODE_POWER_DOWN:
115                 ret = akm8975_ecs_set_mode_power_down(akm);
116                 break;
117         case AK8975_MODE_SELF_TEST:
118                 ret = i2c_smbus_write_byte_data(akm->this_client,
119                                 AK8975_REG_CNTL, AK8975_MODE_SELF_TEST);
120                 break;
121         default:
122                 return -EINVAL;
123         }
124
125         if (ret < 0)
126                 return ret;
127
128         /* Wait at least 300us after changing mode. */
129         udelay(300);
130
131         /* Workaround: Some sensors (defective?) require more delay */
132         msleep(5);
133
134         return 0;
135 }
136
137 static int akmd_copy_in(unsigned int cmd, void __user *argp,
138                         void *buf, size_t buf_size)
139 {
140         if (!(cmd & IOC_IN))
141                 return 0;
142         if (_IOC_SIZE(cmd) > buf_size)
143                 return -EINVAL;
144         if (copy_from_user(buf, argp, _IOC_SIZE(cmd)))
145                 return -EFAULT;
146         return 0;
147 }
148
149 static int akmd_copy_out(unsigned int cmd, void __user *argp,
150                          void *buf, size_t buf_size)
151 {
152         if (!(cmd & IOC_OUT))
153                 return 0;
154         if (_IOC_SIZE(cmd) > buf_size)
155                 return -EINVAL;
156         if (copy_to_user(argp, buf, _IOC_SIZE(cmd)))
157                 return -EFAULT;
158         return 0;
159 }
160
161 static void akm8975_disable_irq(struct akm8975_data *akm)
162 {
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
168                  */
169                 enable_irq(akm->irq);
170         }
171 }
172
173 static irqreturn_t akm8975_irq_handler(int irq, void *data)
174 {
175         struct akm8975_data *akm = data;
176         disable_irq_nosync(irq);
177         complete(&akm->data_ready);
178         return IRQ_HANDLED;
179 }
180
181 static int akm8975_wait_for_data_ready(struct akm8975_data *akm)
182 {
183         int data_ready = gpio_get_value(akm->pdata->gpio_data_ready_int);
184         int err;
185
186         if (data_ready)
187                 return 0;
188
189         enable_irq(akm->irq);
190
191         err = wait_for_completion_timeout(&akm->data_ready, 2*HZ);
192         if (err > 0)
193                 return 0;
194
195         akm8975_disable_irq(akm);
196
197         if (err == 0) {
198                 pr_err("akm: wait timed out\n");
199                 return -ETIMEDOUT;
200         }
201
202         pr_err("akm: wait restart\n");
203         return err;
204 }
205
206 static int akm8975_get_raw_data(struct akm8975_data *akm,
207                                 short *x, short *y, short *z)
208 {
209         short raw_data[3] = {0, };
210         u8 data[8] = {0, };
211         int i, err, ret;
212
213         mutex_lock(&akm->lock);
214         err = akm8975_ecs_set_mode(akm, AK8975_MODE_SNG_MEASURE);
215         if (err) {
216                 pr_err("%s: failed to set ecs mode\n", __func__);
217                 goto done;
218         }
219
220         err = akm8975_wait_for_data_ready(akm);
221         if (err) {
222                 pr_err("%s: failed to wait for data ready\n", __func__);
223                 goto done;
224         }
225
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));
231                 err = ret;
232                 goto done;
233         }
234
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];
239
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]);
244                 }
245         } else {
246                 pr_err("%s: invalid raw data(st1 = %d)\n",
247                                 __func__, data[0] & 0x01);
248         }
249
250 done:
251         mutex_unlock(&akm->lock);
252
253         return err;
254 }
255
256 static ssize_t akmd_read(struct file *file, char __user *buf,
257                                         size_t count, loff_t *pos)
258 {
259         struct akm8975_data *akm = container_of(file->private_data,
260                         struct akm8975_data, akmd_device);
261         short x = 0, y = 0, z = 0;
262         int ret;
263
264         ret = akm8975_get_raw_data(akm, &x, &y, &z);
265         if (ret)
266                 pr_err("%s: failed to get raw data %d\n", __func__, ret);
267
268         return sprintf(buf, "%d,%d,%d\n", x, y, z);
269 }
270
271 static long akmd_ioctl(struct file *file, unsigned int cmd,
272                 unsigned long arg)
273 {
274         void __user *argp = (void __user *)arg;
275         struct akm8975_data *akm = container_of(file->private_data,
276                         struct akm8975_data, akmd_device);
277         int ret;
278         int i;
279         short raw_data[3] = {0, };
280         short adjust_raw[3] = {0,};
281         #ifdef MAGNETIC_LOGGING
282         short x, y, z;
283         #endif
284         union {
285                 char raw[RWBUF_SIZE];
286                 int status;
287                 char mode;
288                 u8 data[8];
289         } rwbuf;
290
291         ret = akmd_copy_in(cmd, argp, rwbuf.raw, sizeof(rwbuf));
292         if (ret)
293                 return ret;
294
295         switch (cmd) {
296         case ECS_IOCTL_WRITE:
297                 if ((rwbuf.raw[0] < 2) || (rwbuf.raw[0] > (RWBUF_SIZE - 1)))
298                         return -EINVAL;
299                 if (copy_from_user(&rwbuf.raw[2], argp+2, rwbuf.raw[0]-1))
300                         return -EFAULT;
301
302                 ret = i2c_smbus_write_i2c_block_data(akm->this_client,
303                                                      rwbuf.raw[1],
304                                                      rwbuf.raw[0] - 1,
305                                                      &rwbuf.raw[2]);
306                 break;
307         case ECS_IOCTL_READ:
308                 if ((rwbuf.raw[0] < 1) || (rwbuf.raw[0] > (RWBUF_SIZE - 1)))
309                         return -EINVAL;
310
311                 ret = i2c_smbus_read_i2c_block_data(akm->this_client,
312                                                     rwbuf.raw[1],
313                                                     rwbuf.raw[0],
314                                                     &rwbuf.raw[1]);
315                 if (ret < 0)
316                         return ret;
317                 if (copy_to_user(argp+1, rwbuf.raw+1, rwbuf.raw[0]))
318                         return -EFAULT;
319                 return 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);
324                 break;
325         case ECS_IOCTL_GETDATA:
326                 mutex_lock(&akm->lock);
327                 ret = akm8975_wait_for_data_ready(akm);
328                 if (ret) {
329                         mutex_unlock(&akm->lock);
330                         return ret;
331                 }
332                 ret = i2c_smbus_read_i2c_block_data(akm->this_client,
333                                                     AK8975_REG_ST1,
334                                                     sizeof(rwbuf.data),
335                                                     rwbuf.data);
336
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];
341
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]);
344                 #endif
345
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];
349
350                 for (i = 0; i < 3; i++) {
351                         adjust_raw[0] +=
352                                 (position_map[akm->position][0][i]
353                                 * raw_data[i]);
354                         adjust_raw[1] +=
355                                 (position_map[akm->position][1][i]
356                                 * raw_data[i]);
357                         adjust_raw[2] +=
358                                 (position_map[akm->position][2][i]
359                                 * raw_data[i]);
360                 }
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]);
364                 #endif
365
366                 rwbuf.data[1] = adjust_raw[0] & 0x00ff;
367                 rwbuf.data[2] = (adjust_raw[0] & 0xff00) >> 8;
368
369                 rwbuf.data[3] = adjust_raw[1] & 0x00ff;
370                 rwbuf.data[4] = (adjust_raw[1] & 0xff00) >> 8;
371
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));
378                         return -EIO;
379                 }
380                 break;
381         default:
382                 return -ENOTTY;
383         }
384
385         if (ret < 0)
386                 return ret;
387
388         return akmd_copy_out(cmd, argp, rwbuf.raw, sizeof(rwbuf));
389 }
390
391 static const struct file_operations akmd_fops = {
392         .owner = THIS_MODULE,
393         .open = nonseekable_open,
394         .read = akmd_read,
395         .unlocked_ioctl = akmd_ioctl,
396 };
397
398 static int akm8975_setup_irq(struct akm8975_data *akm)
399 {
400         int rc = -EIO;
401         struct akm8975_platform_data *pdata = akm->pdata;
402         int irq;
403
404         if (akm->this_client->irq)
405                 irq = akm->this_client->irq;
406         else {
407                 rc = gpio_request(pdata->gpio_data_ready_int, "gpio_akm_int");
408                 if (rc < 0) {
409                         pr_err("%s: gpio %d request failed (%d)\n",
410                                 __func__, pdata->gpio_data_ready_int, rc);
411                         return rc;
412                 }
413
414                 rc = gpio_direction_input(pdata->gpio_data_ready_int);
415                 if (rc < 0) {
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;
419                 }
420
421                 irq = gpio_to_irq(pdata->gpio_data_ready_int);
422         }
423         /* trigger high so we don't miss initial interrupt if it
424          * is already pending
425          */
426         rc = request_irq(irq, akm8975_irq_handler,
427                 IRQF_TRIGGER_RISING | IRQF_ONESHOT, "akm_int", akm);
428         if (rc < 0) {
429                 pr_err("%s: request_irq(%d) failed for gpio %d (%d)\n",
430                         __func__, irq,
431                         pdata->gpio_data_ready_int, rc);
432                 goto err_request_irq;
433         }
434
435         /* start with interrupt disabled until the driver is enabled */
436         akm->irq = irq;
437         akm8975_disable_irq(akm);
438
439         goto done;
440
441 err_request_irq:
442         gpio_free(pdata->gpio_data_ready_int);
443 done:
444         return rc;
445 }
446
447 #ifdef FACTORY_TEST
448 static void ak8975c_selftest(struct akm8975_data *ak_data)
449 {
450         u8 buf[6];
451         s16 x, y, z;
452
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]);
459
460         /* set ATSC self test bit to 1 */
461         i2c_smbus_write_byte_data(ak_data->this_client,
462                                         AK8975_REG_ASTC, 0x40);
463
464         /* start self test */
465         i2c_smbus_write_byte_data(ak_data->this_client,
466                                         AK8975_REG_CNTL,
467                                         REG_CNTL_MODE_SELF_TEST);
468
469         /* wait for data ready */
470         while (1) {
471                 msleep(20);
472                 if (i2c_smbus_read_byte_data(ak_data->this_client,
473                                                 AK8975_REG_ST1) == 1) {
474                         break;
475                 }
476         }
477
478         i2c_smbus_read_i2c_block_data(ak_data->this_client,
479                                         AK8975_REG_HXL, sizeof(buf), buf);
480
481         /* set ATSC self test bit to 0 */
482         i2c_smbus_write_byte_data(ak_data->this_client,
483                                         AK8975_REG_ASTC, 0x00);
484
485         mutex_unlock(&ak_data->lock);
486
487         x = buf[0] | (buf[1] << 8);
488         y = buf[2] | (buf[3] << 8);
489         z = buf[4] | (buf[5] << 8);
490
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;
495
496         pr_info("%s: self test x = %d, y = %d, z = %d\n",
497                 __func__, x, y, z);
498         if ((x >= -100) && (x <= 100))
499                 pr_info("%s: x passed self test, expect -100<=x<=100\n",
500                         __func__);
501         else
502                 pr_info("%s: x failed self test, expect -100<=x<=100\n",
503                         __func__);
504         if ((y >= -100) && (y <= 100))
505                 pr_info("%s: y passed self test, expect -100<=y<=100\n",
506                         __func__);
507         else
508                 pr_info("%s: y failed self test, expect -100<=y<=100\n",
509                         __func__);
510         if ((z >= -1000) && (z <= -300))
511                 pr_info("%s: z passed self test, expect -1000<=z<=-300\n",
512                         __func__);
513         else
514                 pr_info("%s: z failed self test, expect -1000<=z<=-300\n",
515                         __func__);
516
517         if (((x >= -100) && (x <= 100)) && ((y >= -100) && (y <= 100)) &&
518             ((z >= -1000) && (z <= -300)))
519                 ak8975_selftest_passed = 1;
520
521         sf_x = x;
522         sf_y = y;
523         sf_z = z;
524 }
525
526 static ssize_t ak8975c_get_asa(struct device *dev,
527                 struct device_attribute *attr, char *buf)
528 {
529         struct akm8975_data *ak_data  = dev_get_drvdata(dev);
530
531         return sprintf(buf, "%d, %d, %d\n", ak_data->asa[0],\
532                 ak_data->asa[1], ak_data->asa[2]);
533 }
534
535 static ssize_t ak8975c_get_selftest(struct device *dev,
536                 struct device_attribute *attr, char *buf)
537 {
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);
541 }
542
543 static ssize_t ak8975c_check_registers(struct device *dev,
544                 struct device_attribute *attr, char *strbuf)
545 {
546         struct akm8975_data *ak_data  = dev_get_drvdata(dev);
547         u8 buf[13];
548
549         /* power down */
550         i2c_smbus_write_byte_data(ak_data->this_client,\
551         AK8975_REG_CNTL, REG_CNTL_MODE_POWER_DOWN);
552
553         /* get the value */
554         i2c_smbus_read_i2c_block_data(ak_data->this_client,
555                                         AK8975_REG_WIA, 11, buf);
556
557         buf[11] = i2c_smbus_read_byte_data(ak_data->this_client,
558                                         AK8975_REG_ASTC);
559         buf[12] = i2c_smbus_read_byte_data(ak_data->this_client,
560                                         AK8975_REG_I2CDIS);
561
562
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],
566                         buf[12]);
567 }
568
569 static ssize_t ak8975c_check_cntl(struct device *dev,
570                 struct device_attribute *attr, char *strbuf)
571 {
572         struct akm8975_data *ak_data  = dev_get_drvdata(dev);
573         u8 buf;
574         int err;
575
576         /* power down */
577         err = i2c_smbus_write_byte_data(ak_data->this_client,\
578         AK8975_REG_CNTL, REG_CNTL_MODE_POWER_DOWN);
579
580         buf = i2c_smbus_read_byte_data(ak_data->this_client,
581                                         AK8975_REG_CNTL);
582
583
584         return sprintf(strbuf, "%s\n", (!buf ? "OK" : "NG"));
585 }
586
587 static ssize_t ak8975c_get_status(struct device *dev,
588                 struct device_attribute *attr, char *buf)
589 {
590         struct akm8975_data *ak_data  = dev_get_drvdata(dev);
591         int success;
592
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))
596                 success = 0;
597         else
598                 success = 1;
599
600         return sprintf(buf, "%s\n", (success ? "OK" : "NG"));
601 }
602
603 static ssize_t ak8975_adc(struct device *dev,
604                 struct device_attribute *attr, char *strbuf)
605 {
606         struct akm8975_data *ak_data  = dev_get_drvdata(dev);
607         u8 buf[8];
608         short raw_data[3] = {0,};
609         s16 x = 0, y = 0, z = 0;
610         int err, success;
611         int i;
612
613         /* start ADC conversion */
614         err = i2c_smbus_write_byte_data(ak_data->this_client,
615                                         AK8975_REG_CNTL, REG_CNTL_MODE_ONCE);
616
617         /* wait for ADC conversion to complete */
618         err = akm8975_wait_for_data_ready(ak_data);
619         if (err) {
620                 pr_err("%s: wait for data ready failed\n", __func__);
621                 return err;
622         }
623         msleep(20);
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__);
629                 return err;
630         }
631
632         /* buf[0] is status1, buf[7] is status2 */
633         if ((buf[0] == 0) | (buf[7] == 1))
634                 success = 0;
635         else
636                 success = 1;
637
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);
641
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]);
646         }
647
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"),\
650                 x, y, z);
651 }
652 #endif
653
654 static ssize_t ak8975_show_raw_data(struct device *dev,
655                 struct device_attribute *attr, char *buf)
656 {
657         struct akm8975_data *akm = dev_get_drvdata(dev);
658         short x = 0, y = 0, z = 0;
659         int err;
660
661         err = akm8975_get_raw_data(akm, &x, &y, &z);
662         if (err)
663                 pr_err("%s: failed to get raw data\n", __func__);
664
665         return sprintf(buf, "%d,%d,%d\n", x, y, z);
666 }
667
668 static ssize_t ak8975_show_vendor(struct device *dev,
669         struct device_attribute *attr, char *buf)
670 {
671         return sprintf(buf, "%s\n", VENDOR);
672 }
673
674 static ssize_t ak8975_show_name(struct device *dev,
675         struct device_attribute *attr, char *buf)
676 {
677         return sprintf(buf, "%s\n", CHIP_ID);
678 }
679
680 static ssize_t ak8975_show_delay(struct device *dev,
681                 struct device_attribute *attr, char *buf)
682 {
683         struct akm8975_data *akm = dev_get_drvdata(dev);
684
685         return sprintf(buf, "%lld\n", ktime_to_ns(akm->poll_delay));
686 }
687
688 static ssize_t ak8975_store_delay(struct device *dev,
689         struct device_attribute *attr, const char *buf, size_t count)
690 {
691         struct akm8975_data *akm = dev_get_drvdata(dev);
692         u64 delay_ns;
693         int err;
694
695         err = strict_strtoll(buf, 10, &delay_ns);
696         if (err < 0)
697                 pr_err("%s, kstrtoint failed.", __func__);
698
699         mutex_lock(&akm->lock);
700
701         if (akm->enabled)
702                 hrtimer_cancel(&akm->timer);
703
704         akm->poll_delay = ns_to_ktime(delay_ns);
705
706         if (akm->enabled)
707                 hrtimer_start(&akm->timer, akm->poll_delay,
708                                 HRTIMER_MODE_REL);
709         mutex_unlock(&akm->lock);
710
711         return count;
712 }
713
714 static ssize_t ak8975_show_enable(struct device *dev,
715         struct device_attribute *attr, char *buf)
716 {
717         struct akm8975_data *akm = dev_get_drvdata(dev);
718
719         return sprintf(buf, "%d\n", akm->enabled);
720 }
721
722 static ssize_t ak8975_store_enable(struct device *dev,
723         struct device_attribute *attr, const char *buf, size_t count)
724 {
725         struct akm8975_data *akm = dev_get_drvdata(dev);
726         int err, enable;
727
728         err = kstrtoint(buf, 10, &enable);
729         if (err < 0)
730                 pr_err("%s, kstrtoint failed.", __func__);
731
732         mutex_lock(&akm->lock);
733
734         if (enable) {
735                 hrtimer_start(&akm->timer, akm->poll_delay, HRTIMER_MODE_REL);
736                 akm->enabled = true;
737         } else {
738                 hrtimer_cancel(&akm->timer);
739                 akm->enabled = false;
740         }
741
742         mutex_unlock(&akm->lock);
743
744         return count;
745 }
746
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);
751
752 static struct attribute *ak8975_sysfs_attrs[] = {
753         &dev_attr_enable.attr,
754         &dev_attr_poll_delay.attr,
755         NULL
756 };
757
758 static struct attribute_group ak8975_attribute_group = {
759         .attrs = ak8975_sysfs_attrs,
760 };
761
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);
768
769 #ifdef FACTORY_TEST
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,
781                 ak8975_adc, NULL);
782 #endif
783
784 static void akm8975_work_func(struct work_struct *work)
785 {
786         struct akm8975_data *akm =
787                 container_of(work, struct akm8975_data, work);
788         short x = 0, y = 0, z = 0;
789         int err;
790
791         err = akm8975_get_raw_data(akm, &x, &y, &z);
792         if (err) {
793                 pr_err("%s: failed to get raw data\n", __func__);
794                 return;
795         }
796
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);
801 }
802
803 static enum hrtimer_restart ak8975_timer_func(struct hrtimer *timer)
804 {
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);
809
810         return HRTIMER_RESTART;
811 }
812
813 int akm8975_probe(struct i2c_client *client,
814                 const struct i2c_device_id *devid)
815 {
816         struct akm8975_data *akm;
817         int err;
818
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");
822                 err = -ENODEV;
823                 goto exit_platform_data_null;
824         }
825
826         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
827                 dev_err(&client->dev, "I2C check failed, exiting.\n");
828                 err = -ENODEV;
829                 goto exit_check_functionality_failed;
830         }
831
832         akm = kzalloc(sizeof(struct akm8975_data), GFP_KERNEL);
833         if (!akm) {
834                 dev_err(&client->dev,
835                         "failed to allocate memory for module data\n");
836                 err = -ENOMEM;
837                 goto exit_alloc_data_failed;
838         }
839
840         akm->pdata = client->dev.platform_data;
841         mutex_init(&akm->lock);
842         init_completion(&akm->data_ready);
843         akm->enabled = false;
844
845         i2c_set_clientdata(client, akm);
846         akm->this_client = client;
847
848         if (akm->pdata->magnetic_get_position)
849                 akm->position = akm->pdata->magnetic_get_position();
850         else
851                 akm->position = 2;      /*default position */
852         pr_info("%s: position info:%d\n", __func__, akm->position);
853
854         err = akm8975_ecs_set_mode_power_down(akm);
855         if (err < 0) {
856                 pr_err("%s: akm8975_ecs_set_mode_power_down fail(err=%d)\n",
857                         __func__, err);
858                 goto exit_set_mode_power_down_failed;
859         }
860
861         err = akm8975_setup_irq(akm);
862         if (err) {
863                 pr_err("%s: could not setup irq\n", __func__);
864                 goto exit_setup_irq;
865         }
866
867         akm->akmd_device.minor = MISC_DYNAMIC_MINOR;
868         akm->akmd_device.name = "akm8975";
869         akm->akmd_device.fops = &akmd_fops;
870
871         err = misc_register(&akm->akmd_device);
872         if (err) {
873                 pr_err("%s, misc_register failed.\n", __func__);
874                 goto exit_akmd_device_register_failed;
875         }
876
877         init_waitqueue_head(&akm->state_wq);
878
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__);
883                 err = -ENOMEM;
884                 goto exit_akmd_alloc_input;
885         }
886
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);
892
893         err = input_register_device(akm->input_dev);
894         if (err < 0) {
895                 input_free_device(akm->input_dev);
896                 pr_err("%s: Failed to register input device\n", __func__);
897                 goto exit_akmd_input_register;
898         }
899
900         err = sysfs_create_group(&akm->input_dev->dev.kobj,
901                         &ak8975_attribute_group);
902         if (err) {
903                 pr_err("%s: Failed to create sysfs group\n", __func__);
904                 goto err_create_sysfs;
905         }
906
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;
911         akm->work_queue =
912                 create_singlethread_workqueue("ak8975_workqueue");
913         if (!akm->work_queue) {
914                 err = -ENOMEM;
915                 pr_err("%s: Failed to create workqueue\n", __func__);
916                 goto err_create_workqueue;
917         }
918
919         INIT_WORK(&akm->work, akm8975_work_func);
920
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);
924         if (err) {
925                 pr_err("%s: unable to enter fuse rom mode\n", __func__);
926                 goto exit_i2c_failed;
927         }
928
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",
933                         __func__);
934                 goto exit_i2c_failed;
935         } else
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]);
938
939         err = i2c_smbus_write_byte_data(client, AK8975_REG_CNTL,
940                                         REG_CNTL_MODE_POWER_DOWN);
941         if (err) {
942                 dev_err(&client->dev, "Error in setting power down mode\n");
943                 goto exit_i2c_failed;
944         }
945
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;
950         }
951
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;
956         }
957
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;
962         }
963
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;
968         }
969
970 #ifdef FACTORY_TEST
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;
975         }
976
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;
981         }
982
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;
987         }
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;
992         }
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;
998         }
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;
1003         }
1004 #endif
1005         dev_set_drvdata(akm->dev, akm);
1006
1007         pr_info("%s is successful.\n", __func__);
1008         return 0;
1009
1010 #ifdef FACTORY_TEST
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);
1023 #endif
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:
1031 exit_i2c_failed:
1032         destroy_workqueue(akm->work_queue);
1033 err_create_workqueue:
1034         sysfs_remove_group(&akm->input_dev->dev.kobj,
1035                                 &ak8975_attribute_group);
1036 err_create_sysfs:
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);
1045 exit_setup_irq:
1046 exit_set_mode_power_down_failed:
1047         mutex_destroy(&akm->lock);
1048         kfree(akm);
1049 exit_alloc_data_failed:
1050 exit_check_functionality_failed:
1051 exit_platform_data_null:
1052         return err;
1053 }
1054
1055 static int __devexit akm8975_remove(struct i2c_client *client)
1056 {
1057         struct akm8975_data *akm = i2c_get_clientdata(client);
1058
1059         if (akm->enabled) {
1060                 hrtimer_cancel(&akm->timer);
1061                 cancel_work_sync(&akm->work);
1062         }
1063
1064         #ifdef FACTORY_TEST
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);
1071         #endif
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);
1075
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);
1085         kfree(akm);
1086         return 0;
1087 }
1088
1089 static const struct i2c_device_id akm8975_id[] = {
1090         {AKM8975_I2C_NAME, 0 },
1091         { }
1092 };
1093
1094 static struct i2c_driver akm8975_driver = {
1095         .probe          = akm8975_probe,
1096         .remove         = akm8975_remove,
1097         .id_table       = akm8975_id,
1098         .driver = {
1099                 .name = AKM8975_I2C_NAME,
1100         },
1101 };
1102
1103 static int __init akm8975_init(void)
1104 {
1105         return i2c_add_driver(&akm8975_driver);
1106 }
1107
1108 static void __exit akm8975_exit(void)
1109 {
1110         i2c_del_driver(&akm8975_driver);
1111 }
1112
1113 module_init(akm8975_init);
1114 module_exit(akm8975_exit);
1115
1116 MODULE_DESCRIPTION("AKM8975 compass driver");
1117 MODULE_AUTHOR("Samsung Electronics");
1118 MODULE_LICENSE("GPL");