tizen 2.4 release
[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         mdelay(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         /* akm8975_wait_for_data_ready() revice IRQ signal.
227          * But, akm8975 is not ready to send magnetic data.
228          */
229         mdelay(5);
230
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));
236                 err = ret;
237                 goto done;
238         }
239
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];
244
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]);
249                 }
250         } else {
251                 pr_err("%s: invalid raw data(st1 = %d)\n",
252                                 __func__, data[0] & 0x01);
253         }
254
255 done:
256         mutex_unlock(&akm->lock);
257
258         return err;
259 }
260
261 static ssize_t akmd_read(struct file *file, char __user *buf,
262                                         size_t count, loff_t *pos)
263 {
264         struct akm8975_data *akm = container_of(file->private_data,
265                         struct akm8975_data, akmd_device);
266         short x = 0, y = 0, z = 0;
267         int ret;
268
269         ret = akm8975_get_raw_data(akm, &x, &y, &z);
270         if (ret)
271                 pr_err("%s: failed to get raw data %d\n", __func__, ret);
272
273         return sprintf(buf, "%d,%d,%d\n", x, y, z);
274 }
275
276 static long akmd_ioctl(struct file *file, unsigned int cmd,
277                 unsigned long arg)
278 {
279         void __user *argp = (void __user *)arg;
280         struct akm8975_data *akm = container_of(file->private_data,
281                         struct akm8975_data, akmd_device);
282         int ret;
283         int i;
284         short raw_data[3] = {0, };
285         short adjust_raw[3] = {0,};
286         #ifdef MAGNETIC_LOGGING
287         short x, y, z;
288         #endif
289         union {
290                 char raw[RWBUF_SIZE];
291                 int status;
292                 char mode;
293                 u8 data[8];
294         } rwbuf;
295
296         ret = akmd_copy_in(cmd, argp, rwbuf.raw, sizeof(rwbuf));
297         if (ret)
298                 return ret;
299
300         switch (cmd) {
301         case ECS_IOCTL_WRITE:
302                 if ((rwbuf.raw[0] < 2) || (rwbuf.raw[0] > (RWBUF_SIZE - 1)))
303                         return -EINVAL;
304                 if (copy_from_user(&rwbuf.raw[2], argp+2, rwbuf.raw[0]-1))
305                         return -EFAULT;
306
307                 ret = i2c_smbus_write_i2c_block_data(akm->this_client,
308                                                      rwbuf.raw[1],
309                                                      rwbuf.raw[0] - 1,
310                                                      &rwbuf.raw[2]);
311                 break;
312         case ECS_IOCTL_READ:
313                 if ((rwbuf.raw[0] < 1) || (rwbuf.raw[0] > (RWBUF_SIZE - 1)))
314                         return -EINVAL;
315
316                 ret = i2c_smbus_read_i2c_block_data(akm->this_client,
317                                                     rwbuf.raw[1],
318                                                     rwbuf.raw[0],
319                                                     &rwbuf.raw[1]);
320                 if (ret < 0)
321                         return ret;
322                 if (copy_to_user(argp+1, rwbuf.raw+1, rwbuf.raw[0]))
323                         return -EFAULT;
324                 return 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);
329                 break;
330         case ECS_IOCTL_GETDATA:
331                 mutex_lock(&akm->lock);
332                 ret = akm8975_wait_for_data_ready(akm);
333                 if (ret) {
334                         mutex_unlock(&akm->lock);
335                         return ret;
336                 }
337                 ret = i2c_smbus_read_i2c_block_data(akm->this_client,
338                                                     AK8975_REG_ST1,
339                                                     sizeof(rwbuf.data),
340                                                     rwbuf.data);
341
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];
346
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]);
349                 #endif
350
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];
354
355                 for (i = 0; i < 3; i++) {
356                         adjust_raw[0] +=
357                                 (position_map[akm->position][0][i]
358                                 * raw_data[i]);
359                         adjust_raw[1] +=
360                                 (position_map[akm->position][1][i]
361                                 * raw_data[i]);
362                         adjust_raw[2] +=
363                                 (position_map[akm->position][2][i]
364                                 * raw_data[i]);
365                 }
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]);
369                 #endif
370
371                 rwbuf.data[1] = adjust_raw[0] & 0x00ff;
372                 rwbuf.data[2] = (adjust_raw[0] & 0xff00) >> 8;
373
374                 rwbuf.data[3] = adjust_raw[1] & 0x00ff;
375                 rwbuf.data[4] = (adjust_raw[1] & 0xff00) >> 8;
376
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));
383                         return -EIO;
384                 }
385                 break;
386         default:
387                 return -ENOTTY;
388         }
389
390         if (ret < 0)
391                 return ret;
392
393         return akmd_copy_out(cmd, argp, rwbuf.raw, sizeof(rwbuf));
394 }
395
396 static const struct file_operations akmd_fops = {
397         .owner = THIS_MODULE,
398         .open = nonseekable_open,
399         .read = akmd_read,
400         .unlocked_ioctl = akmd_ioctl,
401 };
402
403 static int akm8975_setup_irq(struct akm8975_data *akm)
404 {
405         int rc = -EIO;
406         struct akm8975_platform_data *pdata = akm->pdata;
407         int irq;
408
409         if (akm->this_client->irq)
410                 irq = akm->this_client->irq;
411         else {
412                 rc = gpio_request(pdata->gpio_data_ready_int, "gpio_akm_int");
413                 if (rc < 0) {
414                         pr_err("%s: gpio %d request failed (%d)\n",
415                                 __func__, pdata->gpio_data_ready_int, rc);
416                         return rc;
417                 }
418
419                 rc = gpio_direction_input(pdata->gpio_data_ready_int);
420                 if (rc < 0) {
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;
424                 }
425
426                 irq = gpio_to_irq(pdata->gpio_data_ready_int);
427         }
428         /* trigger high so we don't miss initial interrupt if it
429          * is already pending
430          */
431         rc = request_irq(irq, akm8975_irq_handler,
432                 IRQF_TRIGGER_RISING | IRQF_ONESHOT, "akm_int", akm);
433         if (rc < 0) {
434                 pr_err("%s: request_irq(%d) failed for gpio %d (%d)\n",
435                         __func__, irq,
436                         pdata->gpio_data_ready_int, rc);
437                 goto err_request_irq;
438         }
439
440         /* start with interrupt disabled until the driver is enabled */
441         akm->irq = irq;
442         akm8975_disable_irq(akm);
443
444         goto done;
445
446 err_request_irq:
447         gpio_free(pdata->gpio_data_ready_int);
448 done:
449         return rc;
450 }
451
452 #ifdef FACTORY_TEST
453 static void ak8975c_selftest(struct akm8975_data *ak_data)
454 {
455         u8 buf[6];
456         s16 x, y, z;
457
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]);
464
465         /* set ATSC self test bit to 1 */
466         i2c_smbus_write_byte_data(ak_data->this_client,
467                                         AK8975_REG_ASTC, 0x40);
468
469         /* start self test */
470         i2c_smbus_write_byte_data(ak_data->this_client,
471                                         AK8975_REG_CNTL,
472                                         REG_CNTL_MODE_SELF_TEST);
473
474         /* wait for data ready */
475         while (1) {
476                 mdelay(5);
477                 if (i2c_smbus_read_byte_data(ak_data->this_client,
478                                                 AK8975_REG_ST1) == 1) {
479                         break;
480                 }
481         }
482
483         i2c_smbus_read_i2c_block_data(ak_data->this_client,
484                                         AK8975_REG_HXL, sizeof(buf), buf);
485
486         /* set ATSC self test bit to 0 */
487         i2c_smbus_write_byte_data(ak_data->this_client,
488                                         AK8975_REG_ASTC, 0x00);
489
490         mutex_unlock(&ak_data->lock);
491
492         x = buf[0] | (buf[1] << 8);
493         y = buf[2] | (buf[3] << 8);
494         z = buf[4] | (buf[5] << 8);
495
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;
500
501         pr_info("%s: self test x = %d, y = %d, z = %d\n",
502                 __func__, x, y, z);
503         if ((x >= -100) && (x <= 100))
504                 pr_info("%s: x passed self test, expect -100<=x<=100\n",
505                         __func__);
506         else
507                 pr_info("%s: x failed self test, expect -100<=x<=100\n",
508                         __func__);
509         if ((y >= -100) && (y <= 100))
510                 pr_info("%s: y passed self test, expect -100<=y<=100\n",
511                         __func__);
512         else
513                 pr_info("%s: y failed self test, expect -100<=y<=100\n",
514                         __func__);
515         if ((z >= -1000) && (z <= -300))
516                 pr_info("%s: z passed self test, expect -1000<=z<=-300\n",
517                         __func__);
518         else
519                 pr_info("%s: z failed self test, expect -1000<=z<=-300\n",
520                         __func__);
521
522         if (((x >= -100) && (x <= 100)) && ((y >= -100) && (y <= 100)) &&
523             ((z >= -1000) && (z <= -300)))
524                 ak8975_selftest_passed = 1;
525
526         sf_x = x;
527         sf_y = y;
528         sf_z = z;
529 }
530
531 static ssize_t ak8975c_get_asa(struct device *dev,
532                 struct device_attribute *attr, char *buf)
533 {
534         struct akm8975_data *ak_data  = dev_get_drvdata(dev);
535
536         return sprintf(buf, "%d, %d, %d\n", ak_data->asa[0],\
537                 ak_data->asa[1], ak_data->asa[2]);
538 }
539
540 static ssize_t ak8975c_get_selftest(struct device *dev,
541                 struct device_attribute *attr, char *buf)
542 {
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);
546 }
547
548 static ssize_t ak8975c_check_registers(struct device *dev,
549                 struct device_attribute *attr, char *strbuf)
550 {
551         struct akm8975_data *ak_data  = dev_get_drvdata(dev);
552         u8 buf[13];
553
554         /* power down */
555         i2c_smbus_write_byte_data(ak_data->this_client,\
556         AK8975_REG_CNTL, REG_CNTL_MODE_POWER_DOWN);
557
558         /* get the value */
559         i2c_smbus_read_i2c_block_data(ak_data->this_client,
560                                         AK8975_REG_WIA, 11, buf);
561
562         buf[11] = i2c_smbus_read_byte_data(ak_data->this_client,
563                                         AK8975_REG_ASTC);
564         buf[12] = i2c_smbus_read_byte_data(ak_data->this_client,
565                                         AK8975_REG_I2CDIS);
566
567
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],
571                         buf[12]);
572 }
573
574 static ssize_t ak8975c_check_cntl(struct device *dev,
575                 struct device_attribute *attr, char *strbuf)
576 {
577         struct akm8975_data *ak_data  = dev_get_drvdata(dev);
578         u8 buf;
579         int err;
580
581         /* power down */
582         err = i2c_smbus_write_byte_data(ak_data->this_client,\
583         AK8975_REG_CNTL, REG_CNTL_MODE_POWER_DOWN);
584
585         buf = i2c_smbus_read_byte_data(ak_data->this_client,
586                                         AK8975_REG_CNTL);
587
588
589         return sprintf(strbuf, "%s\n", (!buf ? "OK" : "NG"));
590 }
591
592 static ssize_t ak8975c_get_status(struct device *dev,
593                 struct device_attribute *attr, char *buf)
594 {
595         struct akm8975_data *ak_data  = dev_get_drvdata(dev);
596         int success;
597
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))
601                 success = 0;
602         else
603                 success = 1;
604
605         return sprintf(buf, "%s\n", (success ? "OK" : "NG"));
606 }
607
608 static ssize_t ak8975_adc(struct device *dev,
609                 struct device_attribute *attr, char *strbuf)
610 {
611         struct akm8975_data *ak_data  = dev_get_drvdata(dev);
612         u8 buf[8];
613         short raw_data[3] = {0,};
614         s16 x = 0, y = 0, z = 0;
615         int err, success;
616         int i;
617
618         /* start ADC conversion */
619         err = i2c_smbus_write_byte_data(ak_data->this_client,
620                                         AK8975_REG_CNTL, REG_CNTL_MODE_ONCE);
621
622         /* wait for ADC conversion to complete */
623         err = akm8975_wait_for_data_ready(ak_data);
624         if (err) {
625                 pr_err("%s: wait for data ready failed\n", __func__);
626                 return err;
627         }
628
629         mdelay(5);
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__);
635                 return err;
636         }
637
638         /* buf[0] is status1, buf[7] is status2 */
639         if ((buf[0] == 0) | (buf[7] == 1))
640                 success = 0;
641         else
642                 success = 1;
643
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);
647
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]);
652         }
653
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"),\
656                 x, y, z);
657 }
658 #endif
659
660 static ssize_t ak8975_show_raw_data(struct device *dev,
661                 struct device_attribute *attr, char *buf)
662 {
663         struct akm8975_data *akm = dev_get_drvdata(dev);
664         short x = 0, y = 0, z = 0;
665         int err;
666
667         err = akm8975_get_raw_data(akm, &x, &y, &z);
668         if (err)
669                 pr_err("%s: failed to get raw data\n", __func__);
670
671         return sprintf(buf, "%d,%d,%d\n", x, y, z);
672 }
673
674 static ssize_t ak8975_show_vendor(struct device *dev,
675         struct device_attribute *attr, char *buf)
676 {
677         return sprintf(buf, "%s\n", VENDOR);
678 }
679
680 static ssize_t ak8975_show_name(struct device *dev,
681         struct device_attribute *attr, char *buf)
682 {
683         return sprintf(buf, "%s\n", CHIP_ID);
684 }
685
686 static ssize_t ak8975_show_delay(struct device *dev,
687                 struct device_attribute *attr, char *buf)
688 {
689         struct akm8975_data *akm = dev_get_drvdata(dev);
690
691         return sprintf(buf, "%lld\n", ktime_to_ns(akm->poll_delay));
692 }
693
694 static ssize_t ak8975_store_delay(struct device *dev,
695         struct device_attribute *attr, const char *buf, size_t count)
696 {
697         struct akm8975_data *akm = dev_get_drvdata(dev);
698         u64 delay_ns;
699         int err;
700
701         err = strict_strtoll(buf, 10, &delay_ns);
702         if (err < 0)
703                 pr_err("%s, kstrtoint failed.", __func__);
704
705         mutex_lock(&akm->lock);
706
707         if (akm->enabled)
708                 hrtimer_cancel(&akm->timer);
709
710         akm->poll_delay = ns_to_ktime(delay_ns);
711
712         if (akm->enabled)
713                 hrtimer_start(&akm->timer, akm->poll_delay,
714                                 HRTIMER_MODE_REL);
715         mutex_unlock(&akm->lock);
716
717         return count;
718 }
719
720 static ssize_t ak8975_show_enable(struct device *dev,
721         struct device_attribute *attr, char *buf)
722 {
723         struct akm8975_data *akm = dev_get_drvdata(dev);
724
725         return sprintf(buf, "%d\n", akm->enabled);
726 }
727
728 static ssize_t ak8975_store_enable(struct device *dev,
729         struct device_attribute *attr, const char *buf, size_t count)
730 {
731         struct akm8975_data *akm = dev_get_drvdata(dev);
732         int err, enable;
733
734         err = kstrtoint(buf, 10, &enable);
735         if (err < 0)
736                 pr_err("%s, kstrtoint failed.", __func__);
737
738         mutex_lock(&akm->lock);
739
740         if (enable) {
741                 hrtimer_start(&akm->timer, akm->poll_delay, HRTIMER_MODE_REL);
742                 akm->enabled = true;
743         } else {
744                 hrtimer_cancel(&akm->timer);
745                 akm->enabled = false;
746         }
747
748         mutex_unlock(&akm->lock);
749
750         return count;
751 }
752
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);
757
758 static struct attribute *ak8975_sysfs_attrs[] = {
759         &dev_attr_enable.attr,
760         &dev_attr_poll_delay.attr,
761         NULL
762 };
763
764 static struct attribute_group ak8975_attribute_group = {
765         .attrs = ak8975_sysfs_attrs,
766 };
767
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);
774
775 #ifdef FACTORY_TEST
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,
787                 ak8975_adc, NULL);
788 #endif
789
790 static void akm8975_work_func(struct work_struct *work)
791 {
792         struct akm8975_data *akm =
793                 container_of(work, struct akm8975_data, work);
794         short x = 0, y = 0, z = 0;
795         int err;
796
797         err = akm8975_get_raw_data(akm, &x, &y, &z);
798         if (err) {
799                 pr_err("%s: failed to get raw data\n", __func__);
800                 return;
801         }
802
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);
807 }
808
809 static enum hrtimer_restart ak8975_timer_func(struct hrtimer *timer)
810 {
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);
815
816         return HRTIMER_RESTART;
817 }
818
819 int akm8975_probe(struct i2c_client *client,
820                 const struct i2c_device_id *devid)
821 {
822         struct akm8975_data *akm;
823         int err;
824
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");
828                 err = -ENODEV;
829                 goto exit_platform_data_null;
830         }
831
832         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
833                 dev_err(&client->dev, "I2C check failed, exiting.\n");
834                 err = -ENODEV;
835                 goto exit_check_functionality_failed;
836         }
837
838         akm = kzalloc(sizeof(struct akm8975_data), GFP_KERNEL);
839         if (!akm) {
840                 dev_err(&client->dev,
841                         "failed to allocate memory for module data\n");
842                 err = -ENOMEM;
843                 goto exit_alloc_data_failed;
844         }
845
846         akm->pdata = client->dev.platform_data;
847         mutex_init(&akm->lock);
848         init_completion(&akm->data_ready);
849         akm->enabled = false;
850
851         i2c_set_clientdata(client, akm);
852         akm->this_client = client;
853
854         if (akm->pdata->magnetic_get_position)
855                 akm->position = akm->pdata->magnetic_get_position();
856         else
857                 akm->position = 2;      /*default position */
858         pr_info("%s: position info:%d\n", __func__, akm->position);
859
860         err = akm8975_ecs_set_mode_power_down(akm);
861         if (err < 0) {
862                 pr_err("%s: akm8975_ecs_set_mode_power_down fail(err=%d)\n",
863                         __func__, err);
864                 goto exit_set_mode_power_down_failed;
865         }
866
867         err = akm8975_setup_irq(akm);
868         if (err) {
869                 pr_err("%s: could not setup irq\n", __func__);
870                 goto exit_setup_irq;
871         }
872
873         akm->akmd_device.minor = MISC_DYNAMIC_MINOR;
874         akm->akmd_device.name = "akm8975";
875         akm->akmd_device.fops = &akmd_fops;
876
877         err = misc_register(&akm->akmd_device);
878         if (err) {
879                 pr_err("%s, misc_register failed.\n", __func__);
880                 goto exit_akmd_device_register_failed;
881         }
882
883         init_waitqueue_head(&akm->state_wq);
884
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__);
889                 err = -ENOMEM;
890                 goto exit_akmd_alloc_input;
891         }
892
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);
898
899         err = input_register_device(akm->input_dev);
900         if (err < 0) {
901                 input_free_device(akm->input_dev);
902                 pr_err("%s: Failed to register input device\n", __func__);
903                 goto exit_akmd_input_register;
904         }
905
906         err = sysfs_create_group(&akm->input_dev->dev.kobj,
907                         &ak8975_attribute_group);
908         if (err) {
909                 pr_err("%s: Failed to create sysfs group\n", __func__);
910                 goto err_create_sysfs;
911         }
912
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;
917         akm->work_queue =
918                 create_singlethread_workqueue("ak8975_workqueue");
919         if (!akm->work_queue) {
920                 err = -ENOMEM;
921                 pr_err("%s: Failed to create workqueue\n", __func__);
922                 goto err_create_workqueue;
923         }
924
925         INIT_WORK(&akm->work, akm8975_work_func);
926
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);
930         if (err) {
931                 pr_err("%s: unable to enter fuse rom mode\n", __func__);
932                 goto exit_i2c_failed;
933         }
934
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",
939                         __func__);
940                 goto exit_i2c_failed;
941         } else
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]);
944
945         err = i2c_smbus_write_byte_data(client, AK8975_REG_CNTL,
946                                         REG_CNTL_MODE_POWER_DOWN);
947         if (err) {
948                 dev_err(&client->dev, "Error in setting power down mode\n");
949                 goto exit_i2c_failed;
950         }
951
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;
956         }
957
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;
962         }
963
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;
968         }
969
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;
974         }
975
976 #ifdef FACTORY_TEST
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;
981         }
982
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;
987         }
988
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;
993         }
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;
998         }
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;
1004         }
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;
1009         }
1010 #endif
1011         dev_set_drvdata(akm->dev, akm);
1012
1013         pr_info("%s is successful.\n", __func__);
1014         return 0;
1015
1016 #ifdef FACTORY_TEST
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);
1029 #endif
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:
1037 exit_i2c_failed:
1038         destroy_workqueue(akm->work_queue);
1039 err_create_workqueue:
1040         sysfs_remove_group(&akm->input_dev->dev.kobj,
1041                                 &ak8975_attribute_group);
1042 err_create_sysfs:
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);
1051 exit_setup_irq:
1052 exit_set_mode_power_down_failed:
1053         mutex_destroy(&akm->lock);
1054         kfree(akm);
1055 exit_alloc_data_failed:
1056 exit_check_functionality_failed:
1057 exit_platform_data_null:
1058         return err;
1059 }
1060
1061 static int __devexit akm8975_remove(struct i2c_client *client)
1062 {
1063         struct akm8975_data *akm = i2c_get_clientdata(client);
1064
1065         if (akm->enabled) {
1066                 hrtimer_cancel(&akm->timer);
1067                 cancel_work_sync(&akm->work);
1068         }
1069
1070         #ifdef FACTORY_TEST
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);
1077         #endif
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);
1081
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);
1091         kfree(akm);
1092         return 0;
1093 }
1094
1095 static const struct i2c_device_id akm8975_id[] = {
1096         {AKM8975_I2C_NAME, 0 },
1097         { }
1098 };
1099
1100 static struct i2c_driver akm8975_driver = {
1101         .probe          = akm8975_probe,
1102         .remove         = akm8975_remove,
1103         .id_table       = akm8975_id,
1104         .driver = {
1105                 .name = AKM8975_I2C_NAME,
1106         },
1107 };
1108
1109 static int __init akm8975_init(void)
1110 {
1111         return i2c_add_driver(&akm8975_driver);
1112 }
1113
1114 static void __exit akm8975_exit(void)
1115 {
1116         i2c_del_driver(&akm8975_driver);
1117 }
1118
1119 module_init(akm8975_init);
1120 module_exit(akm8975_exit);
1121
1122 MODULE_DESCRIPTION("AKM8975 compass driver");
1123 MODULE_AUTHOR("Samsung Electronics");
1124 MODULE_LICENSE("GPL");