input: misc: fix sysfs permissions
[linux-3.10.git] / drivers / input / misc / compass / ak8975_input.c
1 /* Copyright (C) 2012 Invensense, Inc.
2  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This software is licensed under the terms of the GNU General Public
5  * License version 2, as published by the Free Software Foundation, and
6  * may be copied, distributed, and modified under those terms.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/i2c.h>
15 #include <linux/input.h>
16 #include <linux/module.h>
17 #include <linux/miscdevice.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
20 #include <linux/workqueue.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/mpu.h>
23
24
25 #define AKM_NAME                        "akm89xx"
26 #define AKM_HW_DELAY_POR_MS             (50)
27 #define AKM_HW_DELAY_TSM_MS             (10)    /* Time Single Measurement */
28 #define AKM_HW_DELAY_US                 (100)
29 #define AKM_HW_DELAY_ROM_ACCESS_US      (200)
30 #define AKM_POLL_DELAY_MS_DFLT          (200)
31 #define AKM_MPU_RETRY_COUNT             (20)
32 #define AKM_MPU_RETRY_DELAY_MS          (100)
33 #define AKM_ERR_CNT_MAX                 (20)
34
35 #define AKM_INPUT_RESOLUTION            (1)
36 #define AKM_INPUT_DIVISOR               (1)
37 #define AKM_INPUT_DELAY_MS_MIN          (AKM_HW_DELAY_TSM_MS)
38 #define AKM_INPUT_POWER_UA              (10000)
39 #define AKM_INPUT_RANGE                 (4912)
40
41 #define AKM_REG_WIA                     (0x00)
42 #define AKM_WIA_ID                      (0x48)
43 #define AKM_REG_INFO                    (0x01)
44 #define AKM_REG_ST1                     (0x02)
45 #define AKM_ST1_DRDY                    (0x01)
46 #define AKM_ST1_DOR                     (0x02)
47 #define AKM_REG_HXL                     (0x03)
48 #define AKM_REG_HXH                     (0x04)
49 #define AKM_REG_HYL                     (0x05)
50 #define AKM_REG_HYH                     (0x06)
51 #define AKM_REG_HZL                     (0x07)
52 #define AKM_REG_HZH                     (0x08)
53 #define AKM_REG_ST2                     (0x09)
54 #define AKM_ST2_DERR                    (0x04)
55 #define AKM_ST2_HOFL                    (0x08)
56 #define AKM_ST2_BITM                    (0x10)
57 #define AKM_REG_CNTL1                   (0x0A)
58 #define AKM_CNTL1_MODE_MASK             (0x0F)
59 #define AKM_CNTL1_MODE_POWERDOWN        (0x00)
60 #define AKM_CNTL1_MODE_SINGLE           (0x01)
61 #define AKM_CNTL1_MODE_CONT1            (0x02)
62 #define AKM_CNTL1_MODE_CONT2            (0x06)
63 #define AKM_CNTL1_MODE_SELFTEST         (0x08)
64 #define AKM_CNTL1_MODE_ROM_ACCESS       (0x0F)
65 #define AKM_CNTL1_OUTPUT_BIT16          (0x10)
66 #define AKM_REG_CNTL2                   (0x0B)
67 #define AKM_CNTL2_SRST                  (0x01)
68 #define AKM_REG_ASTC                    (0x0C)
69 #define AKM_REG_ASTC_SELF               (0x40)
70 #define AKM_REG_TS1                     (0x0D)
71 #define AKM_REG_TS2                     (0x0E)
72 #define AKM_REG_I2CDIS                  (0x0F)
73 #define AKM_REG_ASAX                    (0x10)
74 #define AKM_REG_ASAY                    (0x11)
75 #define AKM_REG_ASAZ                    (0x12)
76
77 #define AKM8963_SCALE14                 (19661)
78 #define AKM8963_SCALE16                 (4915)
79 #define AKM8972_SCALE                   (19661)
80 #define AKM8975_SCALE                   (9830)
81 #define AKM8975_RANGE_X_LO              (-100)
82 #define AKM8975_RANGE_X_HI              (100)
83 #define AKM8975_RANGE_Y_LO              (-100)
84 #define AKM8975_RANGE_Y_HI              (100)
85 #define AKM8975_RANGE_Z_LO              (-1000)
86 #define AKM8975_RANGE_Z_HI              (-300)
87 #define AKM8972_RANGE_X_LO              (-50)
88 #define AKM8972_RANGE_X_HI              (50)
89 #define AKM8972_RANGE_Y_LO              (-50)
90 #define AKM8972_RANGE_Y_HI              (50)
91 #define AKM8972_RANGE_Z_LO              (-500)
92 #define AKM8972_RANGE_Z_HI              (-100)
93 #define AKM8963_RANGE14_X_LO            (-50)
94 #define AKM8963_RANGE14_X_HI            (50)
95 #define AKM8963_RANGE14_Y_LO            (-50)
96 #define AKM8963_RANGE14_Y_HI            (50)
97 #define AKM8963_RANGE14_Z_LO            (-800)
98 #define AKM8963_RANGE14_Z_HI            (-200)
99 #define AKM8963_RANGE16_X_LO            (-200)
100 #define AKM8963_RANGE16_X_HI            (200)
101 #define AKM8963_RANGE16_Y_LO            (-200)
102 #define AKM8963_RANGE16_Y_HI            (200)
103 #define AKM8963_RANGE16_Z_LO            (-3200)
104 #define AKM8963_RANGE16_Z_HI            (-800)
105
106 #define AXIS_X                          (0)
107 #define AXIS_Y                          (1)
108 #define AXIS_Z                          (2)
109 #define WR                              (0)
110 #define RD                              (1)
111
112 enum AKM_DATA_INFO {
113         AKM_DATA_INFO_MAGNETIC_FIELD = 0,
114         AKM_DATA_INFO_MAGNETIC_FIELD_RAW,
115         AKM_DATA_INFO_CALIBRATION,
116         AKM_DATA_INFO_SELF_TEST,
117         AKM_DATA_INFO_SELF_TEST_RAW,
118         AKM_DATA_INFO_RESET,
119         AKM_DATA_INFO_REGISTERS,
120         AKM_DATA_INFO_LIMIT_MAX,
121 };
122
123 /* regulator names in order of powering on */
124 static char *akm_vregs[] = {
125         "vdd",
126         "vid",
127 };
128
129 static char *akm_configs[] = {
130         "auto",
131         "mpu",
132         "host",
133 };
134
135 struct akm_asa {
136         u8 asa[3];                      /* axis sensitivity adjustment */
137         s16 range_lo[3];
138         s16 range_hi[3];
139 };
140
141 struct akm_inf {
142         struct i2c_client *i2c;
143         struct mutex mutex_data;
144         struct input_dev *idev;
145         struct workqueue_struct *wq;
146         struct delayed_work dw;
147         struct regulator_bulk_data vreg[ARRAY_SIZE(akm_vregs)];
148         struct mpu_platform_data pdata;
149         struct akm_asa asa;             /* data for calibration */
150         unsigned int poll_delay_us;     /* requested sampling delay (us) */
151         unsigned int range_i;           /* max_range index */
152         unsigned int resolution;        /* report when new data outside this */
153         unsigned int data_info;         /* data info to return */
154         unsigned int dev_id;            /* device id */
155         bool use_mpu;                   /* if device behind MPU */
156         bool initd;                     /* set if initialized */
157         bool enable;                    /* enable status */
158         bool fifo_enable;               /* MPU FIFO enable */
159         bool port_en[2];                /* enable status of MPU write port */
160         int port_id[2];                 /* MPU port ID */
161         u8 data_out;                    /* write value to trigger a sample */
162         s16 xyz_raw[3];                 /* raw sample data */
163         s16 xyz[3];                     /* sample data after calibration */
164 };
165
166
167 static int akm_i2c_rd(struct akm_inf *inf, u8 reg, u16 len, u8 *val)
168 {
169         struct i2c_msg msg[2];
170
171         msg[0].addr = inf->i2c->addr;
172         msg[0].flags = 0;
173         msg[0].len = 1;
174         msg[0].buf = &reg;
175         msg[1].addr = inf->i2c->addr;
176         msg[1].flags = I2C_M_RD;
177         msg[1].len = len;
178         msg[1].buf = val;
179         if (i2c_transfer(inf->i2c->adapter, msg, 2) != 2)
180                 return -EIO;
181
182         return 0;
183 }
184
185 static int akm_i2c_wr(struct akm_inf *inf, u8 reg, u8 val)
186 {
187         struct i2c_msg msg;
188         u8 buf[2];
189
190         buf[0] = reg;
191         buf[1] = val;
192         msg.addr = inf->i2c->addr;
193         msg.flags = 0;
194         msg.len = 2;
195         msg.buf = buf;
196         if (i2c_transfer(inf->i2c->adapter, &msg, 1) != 1)
197                 return -EIO;
198
199         return 0;
200 }
201
202 static int akm_vreg_dis(struct akm_inf *inf, int i)
203 {
204         int err = 0;
205
206         if (inf->vreg[i].ret && (inf->vreg[i].consumer != NULL)) {
207                 err = regulator_disable(inf->vreg[i].consumer);
208                 if (err)
209                         dev_err(&inf->i2c->dev, "%s %s ERR\n",
210                                 __func__, inf->vreg[i].supply);
211                 else
212                         dev_dbg(&inf->i2c->dev, "%s %s\n",
213                                 __func__, inf->vreg[i].supply);
214         }
215         inf->vreg[i].ret = 0;
216         return err;
217 }
218
219 static int akm_vreg_dis_all(struct akm_inf *inf)
220 {
221         unsigned int i;
222         int err = 0;
223
224         for (i = ARRAY_SIZE(akm_vregs); i > 0; i--)
225                 err |= akm_vreg_dis(inf, (i - 1));
226         return err;
227 }
228
229 static int akm_vreg_en(struct akm_inf *inf, int i)
230 {
231         int err = 0;
232
233         if ((!inf->vreg[i].ret) && (inf->vreg[i].consumer != NULL)) {
234                 err = regulator_enable(inf->vreg[i].consumer);
235                 if (!err) {
236                         inf->vreg[i].ret = 1;
237                         dev_dbg(&inf->i2c->dev, "%s %s\n",
238                                 __func__, inf->vreg[i].supply);
239                         err = 1; /* flag regulator state change */
240                 } else {
241                         dev_err(&inf->i2c->dev, "%s %s ERR\n",
242                                 __func__, inf->vreg[i].supply);
243                 }
244         }
245         return err;
246 }
247
248 static int akm_vreg_en_all(struct akm_inf *inf)
249 {
250         int i;
251         int err = 0;
252
253         for (i = 0; i < ARRAY_SIZE(akm_vregs); i++)
254                 err |= akm_vreg_en(inf, i);
255         return err;
256 }
257
258 static void akm_vreg_exit(struct akm_inf *inf)
259 {
260         int i;
261
262         for (i = 0; i < ARRAY_SIZE(akm_vregs); i++) {
263                 if (inf->vreg[i].consumer != NULL) {
264                         devm_regulator_put(inf->vreg[i].consumer);
265                         inf->vreg[i].consumer = NULL;
266                         dev_dbg(&inf->i2c->dev, "%s %s\n",
267                                 __func__, inf->vreg[i].supply);
268                 }
269         }
270 }
271
272 static int akm_vreg_init(struct akm_inf *inf)
273 {
274         unsigned int i;
275         int err = 0;
276
277         for (i = 0; i < ARRAY_SIZE(akm_vregs); i++) {
278                 inf->vreg[i].supply = akm_vregs[i];
279                 inf->vreg[i].ret = 0;
280                 inf->vreg[i].consumer = devm_regulator_get(&inf->i2c->dev,
281                                                           inf->vreg[i].supply);
282                 if (IS_ERR(inf->vreg[i].consumer)) {
283                         err |= PTR_ERR(inf->vreg[i].consumer);
284                         dev_err(&inf->i2c->dev, "%s err %d for %s\n",
285                                 __func__, err, inf->vreg[i].supply);
286                         inf->vreg[i].consumer = NULL;
287                 } else {
288                         dev_dbg(&inf->i2c->dev, "%s %s\n",
289                                 __func__, inf->vreg[i].supply);
290                 }
291         }
292         return err;
293 }
294
295 static int akm_pm(struct akm_inf *inf, bool enable)
296 {
297         int err;
298
299         if (enable) {
300                 err = akm_vreg_en_all(inf);
301                 if (err)
302                         mdelay(AKM_HW_DELAY_POR_MS);
303         } else {
304                 err = akm_vreg_dis_all(inf);
305         }
306         if (err > 0)
307                 err = 0;
308         if (err)
309                 dev_err(&inf->i2c->dev, "%s enable=%x ERR=%d\n",
310                         __func__, enable, err);
311         else
312                 dev_dbg(&inf->i2c->dev, "%s enable=%x\n",
313                         __func__, enable);
314         return err;
315 }
316
317 static int akm_port_free(struct akm_inf *inf, int port)
318 {
319         int err = 0;
320
321         if ((inf->use_mpu) && (inf->port_id[port] >= 0)) {
322                 err = nvi_mpu_port_free(inf->port_id[port]);
323                 if (!err)
324                         inf->port_id[port] = -1;
325         }
326         return err;
327 }
328
329 static int akm_ports_free(struct akm_inf *inf)
330 {
331         int err;
332
333         err = akm_port_free(inf, WR);
334         err |= akm_port_free(inf, RD);
335         return err;
336 }
337
338 static void akm_pm_exit(struct akm_inf *inf)
339 {
340         akm_ports_free(inf);
341         akm_pm(inf, false);
342         akm_vreg_exit(inf);
343 }
344
345 static int akm_pm_init(struct akm_inf *inf)
346 {
347         int err;
348
349         inf->initd = false;
350         inf->enable = false;
351         inf->fifo_enable = false; /* DON'T ENABLE: MPU FIFO HW BROKEN */
352         inf->port_en[WR] = false;
353         inf->port_en[RD] = false;
354         inf->port_id[WR] = -1;
355         inf->port_id[RD] = -1;
356         inf->poll_delay_us = (AKM_POLL_DELAY_MS_DFLT * 1000);
357         akm_vreg_init(inf);
358         err = akm_pm(inf, true);
359         return err;
360 }
361
362 static int akm_nvi_mpu_bypass_request(struct akm_inf *inf)
363 {
364         int i;
365         int err = 0;
366
367         if (inf->use_mpu) {
368                 for (i = 0; i < AKM_MPU_RETRY_COUNT; i++) {
369                         err = nvi_mpu_bypass_request(true);
370                         if ((!err) || (err == -EPERM))
371                                 break;
372
373                         mdelay(AKM_MPU_RETRY_DELAY_MS);
374                 }
375                 if (err == -EPERM)
376                         err = 0;
377         }
378         return err;
379 }
380
381 static int akm_nvi_mpu_bypass_release(struct akm_inf *inf)
382 {
383         int err = 0;
384
385         if (inf->use_mpu)
386                 err = nvi_mpu_bypass_release();
387         return err;
388 }
389
390 static int akm_wr(struct akm_inf *inf, u8 reg, u8 val)
391 {
392         int err = 0;
393
394         err = akm_nvi_mpu_bypass_request(inf);
395         if (!err) {
396                 err = akm_i2c_wr(inf, AKM_REG_CNTL1, AKM_CNTL1_MODE_POWERDOWN);
397                 udelay(AKM_HW_DELAY_US);
398                 err |= akm_i2c_wr(inf, reg, val);
399                 akm_nvi_mpu_bypass_release(inf);
400         }
401         return err;
402 }
403
404 static int akm_port_enable(struct akm_inf *inf, int port, bool enable)
405 {
406         int err = 0;
407
408         if (enable != inf->port_en[port]) {
409                 err = nvi_mpu_enable(inf->port_id[port],
410                                      enable, inf->fifo_enable);
411                 if (!err)
412                         inf->port_en[port] = enable;
413         }
414         return err;
415 }
416
417 static int akm_ports_enable(struct akm_inf *inf, bool enable)
418 {
419         int err;
420
421         err = akm_port_enable(inf, RD, enable);
422         err |= akm_port_enable(inf, WR, enable);
423         return err;
424 }
425
426 static int akm_mode_wr(struct akm_inf *inf, bool reset,
427                        unsigned int range_i, u8 mode)
428 {
429         u8 mode_old;
430         u8 mode_new;
431         u8 val;
432         int i;
433         int err = 0;
434
435         mode_new = mode;
436         if (range_i)
437                 mode |= AKM_CNTL1_OUTPUT_BIT16;
438         if ((mode == inf->data_out) && (!reset))
439                 return err;
440
441         mode_old = inf->data_out & AKM_CNTL1_MODE_MASK;
442         if (inf->use_mpu)
443                 err = akm_ports_enable(inf, false);
444         else
445                 cancel_delayed_work_sync(&inf->dw);
446         if (err)
447                 return err;
448
449         if (reset) {
450                 if (inf->dev_id == COMPASS_ID_AK8963) {
451                         err = akm_nvi_mpu_bypass_request(inf);
452                         if (!err) {
453                                 err = akm_wr(inf, AKM_REG_CNTL2,
454                                              AKM_CNTL2_SRST);
455                                 for (i = 0; i < AKM_HW_DELAY_POR_MS; i++) {
456                                         mdelay(1);
457                                         err = akm_i2c_rd(inf, AKM_REG_CNTL2,
458                                                          1, &val);
459                                         if (err)
460                                                 continue;
461
462                                         if (!(val & AKM_CNTL2_SRST))
463                                                 break;
464                                 }
465                                 akm_nvi_mpu_bypass_release(inf);
466                         }
467                 }
468         }
469         inf->range_i = range_i;
470         inf->data_out = mode;
471         if (inf->use_mpu) {
472                 if ((mode_old > AKM_CNTL1_MODE_SINGLE) ||
473                                             (mode_new > AKM_CNTL1_MODE_SINGLE))
474                         err = akm_wr(inf, AKM_REG_CNTL1, mode);
475                 if (mode_new <= AKM_CNTL1_MODE_SINGLE) {
476                         err |= nvi_mpu_data_out(inf->port_id[WR], mode);
477                         if (mode_new)
478                                 err |= akm_ports_enable(inf, true);
479                 } else {
480                         err |= akm_port_enable(inf, RD, true);
481                 }
482         } else {
483                 err = akm_wr(inf, AKM_REG_CNTL1, mode);
484                 if (mode_new)
485                         queue_delayed_work(inf->wq, &inf->dw,
486                                          usecs_to_jiffies(inf->poll_delay_us));
487         }
488         return err;
489 }
490
491 static int akm_delay(struct akm_inf *inf, unsigned int delay_us)
492 {
493         u8 mode;
494         int err = 0;
495
496         if (inf->use_mpu)
497                 err |= nvi_mpu_delay_us(inf->port_id[RD],
498                                         (unsigned int)delay_us);
499         if (!err) {
500                 if (inf->dev_id == COMPASS_ID_AK8963) {
501                         if (delay_us == (AKM_INPUT_DELAY_MS_MIN * 1000))
502                                 mode = AKM_CNTL1_MODE_CONT2;
503                         else
504                                 mode = AKM_CNTL1_MODE_SINGLE;
505                         err = akm_mode_wr(inf, false, inf->range_i, mode);
506                 }
507         }
508         return err;
509 }
510
511 static void akm_calc(struct akm_inf *inf, u8 *data)
512 {
513         s16 x;
514         s16 y;
515         s16 z;
516
517         /* data[1] = AKM_REG_HXL
518          * data[2] = AKM_REG_HXH
519          * data[3] = AKM_REG_HYL
520          * data[4] = AKM_REG_HYH
521          * data[5] = AKM_REG_HZL
522          * data[6] = AKM_REG_HZH
523          */
524         mutex_lock(&inf->mutex_data);
525         x = (s16)le16_to_cpup((__le16 *)(&data[1]));
526         y = (s16)le16_to_cpup((__le16 *)(&data[3]));
527         z = (s16)le16_to_cpup((__le16 *)(&data[5]));
528         inf->xyz_raw[AXIS_X] = x;
529         inf->xyz_raw[AXIS_Y] = y;
530         inf->xyz_raw[AXIS_Z] = z;
531         x = (((int)x * (inf->asa.asa[AXIS_X] + 128)) >> 8);
532         y = (((int)y * (inf->asa.asa[AXIS_Y] + 128)) >> 8);
533         z = (((int)z * (inf->asa.asa[AXIS_Z] + 128)) >> 8);
534         inf->xyz[AXIS_X] = x;
535         inf->xyz[AXIS_Y] = y;
536         inf->xyz[AXIS_Z] = z;
537         mutex_unlock(&inf->mutex_data);
538 }
539
540 static s64 akm_timestamp_ns(void)
541 {
542         struct timespec ts;
543         s64 ns;
544
545         ktime_get_ts(&ts);
546         ns = timespec_to_ns(&ts);
547         return ns;
548 }
549
550 static void akm_report(struct akm_inf *inf, u8 *data, s64 ts)
551 {
552         akm_calc(inf, data);
553         mutex_lock(&inf->mutex_data);
554         input_report_rel(inf->idev, REL_X, inf->xyz[AXIS_X]);
555         input_report_rel(inf->idev, REL_Y, inf->xyz[AXIS_Y]);
556         input_report_rel(inf->idev, REL_Z, inf->xyz[AXIS_Z]);
557         mutex_unlock(&inf->mutex_data);
558         input_report_rel(inf->idev, REL_MISC, (unsigned int)(ts >> 32));
559         input_report_rel(inf->idev, REL_WHEEL,
560                          (unsigned int)(ts & 0xffffffff));
561         input_sync(inf->idev);
562 }
563
564 static int akm_read_sts(struct akm_inf *inf, u8 *data)
565 {
566         int err = 0; /* assume still processing */
567         /* data[0] = AKM_REG_ST1
568          * data[7] = AKM_REG_ST2
569          * data[8] = AKM_REG_CNTL1
570          */
571         if ((data[0] == AKM_ST1_DRDY) && (!(data[7] &
572                                            (AKM_ST2_HOFL | AKM_ST2_DERR))))
573                 err = 1; /* data ready to be reported */
574         else if (!(data[8] & AKM_CNTL1_MODE_MASK))
575                 err = -1; /* something wrong */
576         return err;
577 }
578
579 static int akm_read(struct akm_inf *inf)
580 {
581         long long timestamp1;
582         long long timestamp2;
583         u8 data[9];
584         int err;
585
586         timestamp1 = akm_timestamp_ns();
587         err = akm_i2c_rd(inf, AKM_REG_ST1, 9, data);
588         timestamp2 = akm_timestamp_ns();
589         if (err)
590                 return err;
591
592         err = akm_read_sts(inf, data);
593         if (err > 0) {
594                 timestamp2 = (timestamp2 - timestamp1) / 2;
595                 timestamp1 += timestamp2;
596                 akm_report(inf, data, timestamp1);
597                 if ((inf->data_out & AKM_CNTL1_MODE_MASK) ==
598                                                          AKM_CNTL1_MODE_SINGLE)
599                         akm_i2c_wr(inf, AKM_REG_CNTL1, inf->data_out);
600         } else if (err < 0) {
601                         dev_err(&inf->i2c->dev, "%s ERR\n", __func__);
602                         akm_mode_wr(inf, true, inf->range_i,
603                                     inf->data_out & AKM_CNTL1_MODE_MASK);
604         }
605         return err;
606 }
607
608 static void akm_mpu_handler(u8 *data, unsigned int len, s64 ts, void *p_val)
609 {
610         struct akm_inf *inf;
611         int err;
612
613         inf = (struct akm_inf *)p_val;
614         if (inf->enable) {
615                 err = akm_read_sts(inf, data);
616                 if (err > 0)
617                         akm_report(inf, data, ts);
618         }
619 }
620
621 static void akm_work(struct work_struct *ws)
622 {
623         struct akm_inf *inf;
624
625         inf = container_of(ws, struct akm_inf, dw.work);
626         akm_read(inf);
627         queue_delayed_work(inf->wq, &inf->dw,
628                            usecs_to_jiffies(inf->poll_delay_us));
629 }
630
631 static void akm_range_limits(struct akm_inf *inf)
632 {
633         if (inf->dev_id == COMPASS_ID_AK8963) {
634                 if (inf->range_i) {
635                         inf->asa.range_lo[AXIS_X] = AKM8963_RANGE16_X_LO;
636                         inf->asa.range_hi[AXIS_X] = AKM8963_RANGE16_X_HI;
637                         inf->asa.range_lo[AXIS_Y] = AKM8963_RANGE16_Y_LO;
638                         inf->asa.range_hi[AXIS_Y] = AKM8963_RANGE16_Y_HI;
639                         inf->asa.range_lo[AXIS_Z] = AKM8963_RANGE16_Z_LO;
640                         inf->asa.range_hi[AXIS_Z] = AKM8963_RANGE16_Z_HI;
641                 } else {
642                         inf->asa.range_lo[AXIS_X] = AKM8963_RANGE14_X_LO;
643                         inf->asa.range_hi[AXIS_X] = AKM8963_RANGE14_X_HI;
644                         inf->asa.range_lo[AXIS_Y] = AKM8963_RANGE14_Y_LO;
645                         inf->asa.range_hi[AXIS_Y] = AKM8963_RANGE14_Y_HI;
646                         inf->asa.range_lo[AXIS_Z] = AKM8963_RANGE14_Z_LO;
647                         inf->asa.range_hi[AXIS_Z] = AKM8963_RANGE14_Z_HI;
648                 }
649         } else if (inf->dev_id == COMPASS_ID_AK8972) {
650                 inf->asa.range_lo[AXIS_X] = AKM8972_RANGE_X_LO;
651                 inf->asa.range_hi[AXIS_X] = AKM8972_RANGE_X_HI;
652                 inf->asa.range_lo[AXIS_Y] = AKM8972_RANGE_Y_LO;
653                 inf->asa.range_hi[AXIS_Y] = AKM8972_RANGE_Y_HI;
654                 inf->asa.range_lo[AXIS_Z] = AKM8972_RANGE_Z_LO;
655                 inf->asa.range_hi[AXIS_Z] = AKM8972_RANGE_Z_HI;
656         } else { /* default COMPASS_ID_AK8975 */
657                 inf->dev_id = COMPASS_ID_AK8975;
658                 inf->asa.range_lo[AXIS_X] = AKM8975_RANGE_X_LO;
659                 inf->asa.range_hi[AXIS_X] = AKM8975_RANGE_X_HI;
660                 inf->asa.range_lo[AXIS_Y] = AKM8975_RANGE_Y_LO;
661                 inf->asa.range_hi[AXIS_Y] = AKM8975_RANGE_Y_HI;
662                 inf->asa.range_lo[AXIS_Z] = AKM8975_RANGE_Z_LO;
663                 inf->asa.range_hi[AXIS_Z] = AKM8975_RANGE_Z_HI;
664         }
665 }
666
667 static int akm_self_test(struct akm_inf *inf)
668 {
669         u8 data[9];
670         u8 mode;
671         int i;
672         int err;
673         int err_t;
674
675         err_t = akm_i2c_wr(inf, AKM_REG_CNTL1, AKM_CNTL1_MODE_POWERDOWN);
676         udelay(AKM_HW_DELAY_US);
677         err_t |= akm_i2c_wr(inf, AKM_REG_ASTC, AKM_REG_ASTC_SELF);
678         udelay(AKM_HW_DELAY_US);
679         mode = AKM_CNTL1_MODE_SELFTEST;
680         if (inf->range_i)
681                 mode |= AKM_CNTL1_OUTPUT_BIT16;
682         err_t |= akm_i2c_wr(inf, AKM_REG_CNTL1, mode);
683         for (i = 0; i < AKM_HW_DELAY_TSM_MS; i++) {
684                 mdelay(AKM_HW_DELAY_TSM_MS);
685                 err = akm_i2c_rd(inf, AKM_REG_ST1, 9, data);
686                 if (!err) {
687                         err = akm_read_sts(inf, data);
688                         if (err > 0) {
689                                 akm_calc(inf, data);
690                                 err = 0;
691                                 break;
692                         }
693                         err = -EBUSY;
694                 }
695         }
696         err_t |= err;
697         akm_i2c_wr(inf, AKM_REG_ASTC, 0);
698         if (!err_t) {
699                 akm_range_limits(inf);
700                 if ((inf->xyz[AXIS_X] < inf->asa.range_lo[AXIS_X]) ||
701                                 (inf->xyz[AXIS_X] > inf->asa.range_hi[AXIS_X]))
702                         err_t |= 1 << AXIS_X;
703                 if ((inf->xyz[AXIS_Y] < inf->asa.range_lo[AXIS_Y]) ||
704                                 (inf->xyz[AXIS_Y] > inf->asa.range_hi[AXIS_Y]))
705                         err_t |= 1 << AXIS_Y;
706                 if ((inf->xyz[AXIS_Z] < inf->asa.range_lo[AXIS_Z]) ||
707                                 (inf->xyz[AXIS_Z] > inf->asa.range_hi[AXIS_Z]))
708                         err_t |= 1 << AXIS_Z;
709                 if (err_t) {
710                         dev_err(&inf->i2c->dev, "%s ERR: out_of_range %x\n",
711                                 __func__, err_t);
712
713                 }
714         }
715         return err_t;
716 }
717
718 static int akm_init_hw(struct akm_inf *inf)
719 {
720         u8 val[8];
721         int err;
722         int err_t;
723
724         err_t = akm_nvi_mpu_bypass_request(inf);
725         if (!err_t) {
726                 err_t = akm_wr(inf, AKM_REG_CNTL1, AKM_CNTL1_MODE_ROM_ACCESS);
727                 udelay(AKM_HW_DELAY_ROM_ACCESS_US);
728                 err_t |= akm_i2c_rd(inf, AKM_REG_ASAX, 3, inf->asa.asa);
729                 /* we can autodetect AK8963 with BITM */
730                 inf->dev_id = 0;
731                 err = akm_wr(inf, AKM_REG_CNTL1, (AKM_CNTL1_MODE_SINGLE |
732                                                   AKM_CNTL1_OUTPUT_BIT16));
733                 if (!err) {
734                         mdelay(AKM_HW_DELAY_TSM_MS);
735                         err = akm_i2c_rd(inf, AKM_REG_ST2, 1, val);
736                         if ((!err) && (val[0] & AKM_CNTL1_OUTPUT_BIT16)) {
737                                 inf->dev_id = COMPASS_ID_AK8963;
738                                 inf->range_i = 1;
739                         }
740                 }
741                 if (!inf->dev_id)
742                         inf->dev_id = inf->pdata.sec_slave_id;
743                 err = akm_self_test(inf);
744                 if (err < 0)
745                         err_t |= err;
746                 akm_nvi_mpu_bypass_release(inf);
747         }
748         if (!err_t)
749                 inf->initd = true;
750         else
751                 dev_err(&inf->i2c->dev, "%s ERR %d\n", __func__, err_t);
752         return err_t;
753 }
754
755 static int akm_enable(struct akm_inf *inf, bool enable)
756 {
757         int err = 0;
758
759         akm_pm(inf, true);
760         if (!inf->initd)
761                 err = akm_init_hw(inf);
762         if (!err) {
763                 if (enable) {
764                         inf->data_out = AKM_CNTL1_MODE_SINGLE;
765                         err |= akm_delay(inf, inf->poll_delay_us);
766                         err |= akm_mode_wr(inf, true, inf->range_i,
767                                           inf->data_out & AKM_CNTL1_MODE_MASK);
768                         if (!err)
769                                 inf->enable = true;
770                 } else {
771                         inf->enable = false;
772                         err = akm_mode_wr(inf, false, inf->range_i,
773                                           AKM_CNTL1_MODE_POWERDOWN);
774                         if (!err)
775                                 akm_pm(inf, false);
776                 }
777         } else {
778                 akm_pm(inf, false);
779         }
780         return err;
781 }
782
783 static ssize_t akm_enable_store(struct device *dev,
784                                 struct device_attribute *attr,
785                                 const char *buf, size_t count)
786 {
787         struct akm_inf *inf;
788         unsigned int enable;
789         bool en;
790         int err;
791
792         inf = dev_get_drvdata(dev);
793         err = kstrtouint(buf, 10, &enable);
794         if (err)
795                 return -EINVAL;
796
797         if (enable)
798                 en = true;
799         else
800                 en = false;
801         dev_dbg(&inf->i2c->dev, "%s: %x\n", __func__, en);
802         err = akm_enable(inf, en);
803         if (err) {
804                 dev_err(&inf->i2c->dev, "%s: %x ERR=%d\n", __func__, en, err);
805                 return err;
806         }
807
808         return count;
809 }
810
811 static ssize_t akm_enable_show(struct device *dev,
812                                struct device_attribute *attr, char *buf)
813 {
814         struct akm_inf *inf;
815         unsigned int enable;
816
817         inf = dev_get_drvdata(dev);
818         if (inf->enable)
819                 enable = 1;
820         else
821                 enable = 0;
822         return sprintf(buf, "%u\n", enable);
823 }
824
825 static ssize_t akm_delay_store(struct device *dev,
826                                struct device_attribute *attr,
827                                const char *buf, size_t count)
828 {
829         struct akm_inf *inf;
830         unsigned int delay_us;
831         int err;
832
833         inf = dev_get_drvdata(dev);
834         err = kstrtouint(buf, 10, &delay_us);
835         if (err)
836                 return -EINVAL;
837
838         dev_dbg(&inf->i2c->dev, "%s: %u\n", __func__, delay_us);
839         if (delay_us < (AKM_INPUT_DELAY_MS_MIN * 1000))
840                 delay_us = (AKM_INPUT_DELAY_MS_MIN * 1000);
841         if ((inf->enable) && (delay_us != inf->poll_delay_us))
842                 err = akm_delay(inf, delay_us);
843         if (!err) {
844                 inf->poll_delay_us = delay_us;
845         } else {
846                 dev_err(&inf->i2c->dev, "%s: %u ERR=%d\n",
847                         __func__, delay_us, err);
848                 return err;
849         }
850
851         return count;
852 }
853
854 static ssize_t akm_delay_show(struct device *dev,
855                               struct device_attribute *attr, char *buf)
856 {
857         struct akm_inf *inf;
858
859         inf = dev_get_drvdata(dev);
860         if (inf->enable)
861                 return sprintf(buf, "%u\n", inf->poll_delay_us);
862
863         return sprintf(buf, "%u\n", (AKM_INPUT_DELAY_MS_MIN * 1000));
864 }
865
866 static ssize_t akm_resolution_store(struct device *dev,
867                                     struct device_attribute *attr,
868                                     const char *buf, size_t count)
869 {
870         struct akm_inf *inf;
871         unsigned int resolution;
872
873         inf = dev_get_drvdata(dev);
874         if (kstrtouint(buf, 10, &resolution))
875                 return -EINVAL;
876
877         dev_dbg(&inf->i2c->dev, "%s %u\n", __func__, resolution);
878         inf->resolution = resolution;
879         return count;
880 }
881
882 static ssize_t akm_resolution_show(struct device *dev,
883                                    struct device_attribute *attr, char *buf)
884 {
885         struct akm_inf *inf;
886         unsigned int resolution;
887
888         inf = dev_get_drvdata(dev);
889         if (inf->enable)
890                 resolution = inf->resolution;
891         else
892                 resolution = AKM_INPUT_RESOLUTION;
893         return sprintf(buf, "%u\n", resolution);
894 }
895
896 static ssize_t akm_max_range_store(struct device *dev,
897                                    struct device_attribute *attr,
898                                    const char *buf, size_t count)
899 {
900         struct akm_inf *inf;
901         unsigned int range_i;
902         int err;
903
904         inf = dev_get_drvdata(dev);
905         err = kstrtouint(buf, 10, &range_i);
906         if (err)
907                 return -EINVAL;
908
909         dev_dbg(&inf->i2c->dev, "%s %u\n", __func__, range_i);
910         if (inf->dev_id == COMPASS_ID_AK8963) {
911                 if (range_i > 1)
912                         return -EINVAL;
913
914                 if (inf->enable) {
915                         err = akm_mode_wr(inf, false, range_i,
916                                           inf->data_out & AKM_CNTL1_MODE_MASK);
917                         if (err)
918                                 return err;
919                 } else {
920                         inf->range_i = range_i;
921                 }
922         } else {
923                 return -EINVAL;
924         }
925
926         return count;
927 }
928
929 static ssize_t akm_max_range_show(struct device *dev,
930                                   struct device_attribute *attr, char *buf)
931 {
932         struct akm_inf *inf;
933         unsigned int range;
934
935         inf = dev_get_drvdata(dev);
936         if (inf->enable) {
937                 range = inf->range_i;
938         } else {
939                 if (inf->dev_id == COMPASS_ID_AK8963) {
940                         if (inf->range_i)
941                                 range = AKM8963_SCALE16;
942                         else
943                                 range = AKM8963_SCALE14;
944                 } else if (inf->dev_id == COMPASS_ID_AK8972) {
945                         range = AKM8972_SCALE;
946                 } else {
947                         range = AKM8975_SCALE;
948                 }
949         }
950         return sprintf(buf, "%u\n", range);
951 }
952
953 static ssize_t akm_data_store(struct device *dev,
954                               struct device_attribute *attr,
955                               const char *buf, size_t count)
956 {
957         struct akm_inf *inf;
958         unsigned int data_info;
959         int err;
960
961         inf = dev_get_drvdata(dev);
962         err = kstrtouint(buf, 10, &data_info);
963         if (err)
964                 return -EINVAL;
965
966         if (data_info >= AKM_DATA_INFO_LIMIT_MAX)
967                 return -EINVAL;
968
969         dev_dbg(&inf->i2c->dev, "%s %u\n", __func__, data_info);
970         inf->data_info = data_info;
971         return count;
972 }
973
974 static ssize_t akm_data_show(struct device *dev,
975                              struct device_attribute *attr,
976                              char *buf)
977 {
978         struct akm_inf *inf;
979         u8 data[16];
980         s16 x = 0;
981         s16 y = 0;
982         s16 z = 0;
983         enum AKM_DATA_INFO data_info;
984         bool enable;
985         char const *info_str;
986         int err = 0;
987
988         inf = dev_get_drvdata(dev);
989         data_info = inf->data_info;
990         inf->data_info = AKM_DATA_INFO_MAGNETIC_FIELD;
991         enable = inf->enable;
992         switch (data_info) {
993         case AKM_DATA_INFO_MAGNETIC_FIELD:
994                 if (inf->enable) {
995                         mutex_lock(&inf->mutex_data);
996                         x = inf->xyz[AXIS_X];
997                         y = inf->xyz[AXIS_Y];
998                         z = inf->xyz[AXIS_Z];
999                         mutex_unlock(&inf->mutex_data);
1000                         info_str = "magnetic_field";
1001                 } else {
1002                         info_str = "ERR: DISABLED";
1003                 }
1004                 break;
1005
1006         case AKM_DATA_INFO_MAGNETIC_FIELD_RAW:
1007                 if (inf->enable) {
1008                         mutex_lock(&inf->mutex_data);
1009                         x = inf->xyz_raw[AXIS_X];
1010                         y = inf->xyz_raw[AXIS_Y];
1011                         z = inf->xyz_raw[AXIS_Z];
1012                         mutex_unlock(&inf->mutex_data);
1013                         info_str = "raw_data";
1014                 } else {
1015                         info_str = "ERR: DISABLED";
1016                 }
1017                 break;
1018
1019         case AKM_DATA_INFO_CALIBRATION:
1020                 err = 0;
1021                 if (!inf->initd)
1022                         err = akm_enable(inf, enable);
1023                 if (err) {
1024                         info_str = "calibration ERR";
1025                 } else {
1026                         x = (s16)inf->asa.asa[AXIS_X];
1027                         y = (s16)inf->asa.asa[AXIS_Y];
1028                         z = (s16)inf->asa.asa[AXIS_Z];
1029                         info_str = "calibration";
1030                 }
1031                 break;
1032
1033         case AKM_DATA_INFO_SELF_TEST:
1034                 akm_enable(inf, false);
1035                 akm_pm(inf, true);
1036                 if (!inf->initd) {
1037                         err = akm_init_hw(inf);
1038                 } else {
1039                         err = akm_nvi_mpu_bypass_request(inf);
1040                         if (!err) {
1041                                 err = akm_self_test(inf);
1042                                 akm_nvi_mpu_bypass_release(inf);
1043                         }
1044                 }
1045                 if (err < 0) {
1046                         info_str = "self_test ERR";
1047                 } else {
1048                         mutex_lock(&inf->mutex_data);
1049                         x = inf->xyz[AXIS_X];
1050                         y = inf->xyz[AXIS_Y];
1051                         z = inf->xyz[AXIS_Z];
1052                         mutex_unlock(&inf->mutex_data);
1053                         if (err > 0)
1054                                 info_str = "self_test FAIL";
1055                         else
1056                                 info_str = "self_test PASS";
1057                 }
1058                 akm_enable(inf, enable);
1059                 break;
1060
1061         case AKM_DATA_INFO_SELF_TEST_RAW:
1062                 akm_enable(inf, false);
1063                 akm_pm(inf, true);
1064                 if (!inf->initd) {
1065                         err = akm_init_hw(inf);
1066                 } else {
1067                         err = akm_nvi_mpu_bypass_request(inf);
1068                         if (!err) {
1069                                 err = akm_self_test(inf);
1070                                 akm_nvi_mpu_bypass_release(inf);
1071                         }
1072                 }
1073                 if (err < 0) {
1074                         info_str = "self_test ERR";
1075                 } else {
1076                         mutex_lock(&inf->mutex_data);
1077                         x = inf->xyz_raw[AXIS_X];
1078                         y = inf->xyz_raw[AXIS_Y];
1079                         z = inf->xyz_raw[AXIS_Z];
1080                         mutex_unlock(&inf->mutex_data);
1081                         info_str = "self_test raw";
1082                 }
1083                 akm_enable(inf, enable);
1084                 break;
1085
1086         case AKM_DATA_INFO_RESET:
1087                 akm_pm(inf, true);
1088                 err = akm_mode_wr(inf, true, inf->range_i,
1089                                   inf->data_out & AKM_CNTL1_MODE_MASK);
1090                 akm_enable(inf, enable);
1091                 if (err)
1092                         return sprintf(buf, "reset ERR\n");
1093                 else
1094                         return sprintf(buf, "reset done\n");
1095
1096         case AKM_DATA_INFO_REGISTERS:
1097                 err = akm_nvi_mpu_bypass_request(inf);
1098                 if (!err) {
1099                         err = akm_i2c_rd(inf, AKM_REG_WIA, AKM_REG_ASAX, data);
1100                         akm_nvi_mpu_bypass_release(inf);
1101                 }
1102                 if (err)
1103                         return sprintf(buf, "register read ERR\n");
1104
1105                 return sprintf(buf, "%x %x %x %x %x %x %x %x %x %x\n",
1106                                data[0], data[1], data[2],
1107                                le16_to_cpup((__le16 *)(&data[3])),
1108                                le16_to_cpup((__le16 *)(&data[5])),
1109                                le16_to_cpup((__le16 *)(&data[7])),
1110                                data[9], data[10], data[11], data[12]);
1111
1112         default:
1113                 return -EINVAL;
1114         }
1115
1116         return sprintf(buf, "%s: %hd, %hd, %hd\n", info_str, x, y, z);
1117 }
1118
1119 static ssize_t akm_mpu_fifo_enable_store(struct device *dev,
1120                                          struct device_attribute *attr,
1121                                          const char *buf, size_t count)
1122 {
1123         struct akm_inf *inf;
1124         unsigned int fifo_enable;
1125         int err;
1126
1127         inf = dev_get_drvdata(dev);
1128         err = kstrtouint(buf, 10, &fifo_enable);
1129         if (err)
1130                 return -EINVAL;
1131
1132         if (!inf->use_mpu)
1133                 return -EINVAL;
1134
1135         if (fifo_enable)
1136                 inf->fifo_enable = true;
1137         else
1138                 inf->fifo_enable = false;
1139         dev_dbg(&inf->i2c->dev, "%s %x\n", __func__, inf->fifo_enable);
1140         return count;
1141 }
1142
1143 static ssize_t akm_mpu_fifo_enable_show(struct device *dev,
1144                                         struct device_attribute *attr,
1145                                         char *buf)
1146 {
1147         struct akm_inf *inf = dev_get_drvdata(dev);
1148
1149         return sprintf(buf, "%x\n", inf->fifo_enable & inf->use_mpu);
1150 }
1151
1152 static ssize_t akm_divisor_show(struct device *dev,
1153                                 struct device_attribute *attr, char *buf)
1154 {
1155         struct akm_inf *inf;
1156
1157         inf = dev_get_drvdata(dev);
1158         return sprintf(buf, "%u\n", AKM_INPUT_DIVISOR);
1159 }
1160
1161 static ssize_t akm_microamp_show(struct device *dev,
1162                                  struct device_attribute *attr, char *buf)
1163 {
1164         struct akm_inf *inf;
1165
1166         inf = dev_get_drvdata(dev);
1167         return sprintf(buf, "%u\n", AKM_INPUT_POWER_UA);
1168 }
1169
1170 static ssize_t akm_orientation_show(struct device *dev,
1171                                     struct device_attribute *attr,
1172                                     char *buf)
1173 {
1174         struct akm_inf *inf;
1175         signed char *m;
1176
1177         inf = dev_get_drvdata(dev);
1178         m = inf->pdata.orientation;
1179         return sprintf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
1180                        m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
1181 }
1182
1183 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP,
1184                    akm_enable_show, akm_enable_store);
1185 static DEVICE_ATTR(delay, S_IRUGO | S_IWUSR | S_IWGRP,
1186                    akm_delay_show, akm_delay_store);
1187 static DEVICE_ATTR(resolution, S_IRUGO | S_IWUSR | S_IWGRP,
1188                    akm_resolution_show, akm_resolution_store);
1189 static DEVICE_ATTR(max_range, S_IRUGO | S_IWUSR | S_IWGRP,
1190                    akm_max_range_show, akm_max_range_store);
1191 static DEVICE_ATTR(data, S_IRUGO | S_IWUSR | S_IWGRP,
1192                    akm_data_show, akm_data_store);
1193 static DEVICE_ATTR(mpu_fifo_en, S_IRUGO | S_IWUSR | S_IWGRP,
1194                    akm_mpu_fifo_enable_show, akm_mpu_fifo_enable_store);
1195 static DEVICE_ATTR(divisor, S_IRUGO,
1196                    akm_divisor_show, NULL);
1197 static DEVICE_ATTR(microamp, S_IRUGO,
1198                    akm_microamp_show, NULL);
1199 static DEVICE_ATTR(orientation, S_IRUGO,
1200                    akm_orientation_show, NULL);
1201
1202 static struct attribute *akm_attrs[] = {
1203         &dev_attr_enable.attr,
1204         &dev_attr_delay.attr,
1205         &dev_attr_resolution.attr,
1206         &dev_attr_max_range.attr,
1207         &dev_attr_divisor.attr,
1208         &dev_attr_microamp.attr,
1209         &dev_attr_data.attr,
1210         &dev_attr_orientation.attr,
1211         &dev_attr_mpu_fifo_en.attr,
1212         NULL
1213 };
1214
1215 static struct attribute_group akm_attr_group = {
1216         .name = AKM_NAME,
1217         .attrs = akm_attrs
1218 };
1219
1220 static int akm_sysfs_create(struct akm_inf *inf)
1221 {
1222         int err;
1223
1224         err = sysfs_create_group(&inf->idev->dev.kobj, &akm_attr_group);
1225         return err;
1226 }
1227
1228 static void akm_input_close(struct input_dev *idev)
1229 {
1230         struct akm_inf *inf;
1231
1232         inf = input_get_drvdata(idev);
1233         if (inf != NULL)
1234                 akm_enable(inf, false);
1235 }
1236
1237 static int akm_input_create(struct akm_inf *inf)
1238 {
1239         int err;
1240
1241         inf->idev = input_allocate_device();
1242         if (!inf->idev) {
1243                 err = -ENOMEM;
1244                 dev_err(&inf->i2c->dev, "%s ERR %d\n", __func__, err);
1245                 return err;
1246         }
1247
1248         inf->idev->name = AKM_NAME;
1249         inf->idev->dev.parent = &inf->i2c->dev;
1250         inf->idev->close = akm_input_close;
1251         input_set_drvdata(inf->idev, inf);
1252         input_set_capability(inf->idev, EV_REL, REL_X);
1253         input_set_capability(inf->idev, EV_REL, REL_Y);
1254         input_set_capability(inf->idev, EV_REL, REL_Z);
1255         input_set_capability(inf->idev, EV_REL, REL_MISC);
1256         input_set_capability(inf->idev, EV_REL, REL_WHEEL);
1257         err = input_register_device(inf->idev);
1258         if (err) {
1259                 input_free_device(inf->idev);
1260                 inf->idev = NULL;
1261         }
1262         return err;
1263 }
1264
1265 static int akm_id(struct akm_inf *inf)
1266 {
1267         struct nvi_mpu_port nmp;
1268         u8 config_boot;
1269         u8 val = 0;
1270         int err;
1271
1272         config_boot = inf->pdata.config & NVI_CONFIG_BOOT_MASK;
1273         if (config_boot == NVI_CONFIG_BOOT_AUTO) {
1274                 nmp.addr = inf->i2c->addr | 0x80;
1275                 nmp.reg = AKM_REG_WIA;
1276                 nmp.ctrl = 1;
1277                 err = nvi_mpu_dev_valid(&nmp, &val);
1278                 /* see mpu.h for possible return values */
1279                 dev_dbg(&inf->i2c->dev, "%s AUTO ID=%x err=%d\n",
1280                         __func__, val, err);
1281                 if ((err == -EAGAIN) || (err == -EBUSY))
1282                         return -EAGAIN;
1283
1284                 if (((!err) && (val == AKM_WIA_ID)) || (err == -EIO))
1285                         config_boot = NVI_CONFIG_BOOT_MPU;
1286         }
1287         if (config_boot == NVI_CONFIG_BOOT_MPU) {
1288                 inf->use_mpu = true;
1289                 nmp.addr = inf->i2c->addr | 0x80;
1290                 nmp.reg = AKM_REG_ST1;
1291                 nmp.ctrl = 10; /* MPU FIFO can't handle odd size */
1292                 nmp.data_out = 0;
1293                 nmp.delay_ms = 0;
1294                 nmp.delay_us = inf->poll_delay_us;
1295                 nmp.shutdown_bypass = false;
1296                 nmp.handler = &akm_mpu_handler;
1297                 nmp.ext_driver = (void *)inf;
1298                 err = nvi_mpu_port_alloc(&nmp);
1299                 dev_dbg(&inf->i2c->dev, "%s MPU port/err=%d\n",
1300                         __func__, err);
1301                 if (err < 0)
1302                         return err;
1303
1304                 inf->port_id[RD] = err;
1305                 nmp.addr = inf->i2c->addr;
1306                 nmp.reg = AKM_REG_CNTL1;
1307                 nmp.ctrl = 1;
1308                 nmp.data_out = inf->data_out;
1309                 nmp.delay_ms = AKM_HW_DELAY_TSM_MS;
1310                 nmp.delay_us = 0;
1311                 nmp.shutdown_bypass = false;
1312                 nmp.handler = NULL;
1313                 nmp.ext_driver = NULL;
1314                 err = nvi_mpu_port_alloc(&nmp);
1315                 dev_dbg(&inf->i2c->dev, "%s MPU port/err=%d\n",
1316                         __func__, err);
1317                 if (err < 0) {
1318                         akm_ports_free(inf);
1319                 } else {
1320                         inf->port_id[WR] = err;
1321                         err = 0;
1322                 }
1323                 return err;
1324         }
1325
1326         /* NVI_CONFIG_BOOT_HOST */
1327         inf->use_mpu = false;
1328         err = akm_i2c_rd(inf, AKM_REG_WIA, 1, &val);
1329         dev_dbg(&inf->i2c->dev, "%s Host read ID=%x err=%d\n",
1330                 __func__, val, err);
1331         if ((!err) && (val == AKM_WIA_ID))
1332                 return 0;
1333
1334         return -ENODEV;
1335 }
1336
1337 static int akm_remove(struct i2c_client *client)
1338 {
1339         struct akm_inf *inf;
1340
1341         inf = i2c_get_clientdata(client);
1342         if (inf != NULL) {
1343                 if (inf->idev) {
1344                         input_unregister_device(inf->idev);
1345                         input_free_device(inf->idev);
1346                 }
1347                 if (inf->wq)
1348                         destroy_workqueue(inf->wq);
1349                 akm_pm_exit(inf);
1350                 if (&inf->mutex_data)
1351                         mutex_destroy(&inf->mutex_data);
1352         }
1353         dev_info(&client->dev, "%s\n", __func__);
1354         return 0;
1355 }
1356
1357 static void akm_shutdown(struct i2c_client *client)
1358 {
1359         akm_remove(client);
1360 }
1361
1362 static struct mpu_platform_data *akm_parse_dt(struct i2c_client *client)
1363 {
1364         struct mpu_platform_data *pdata;
1365         struct device_node *np = client->dev.of_node;
1366         char const *pchar;
1367         u8 config;
1368         int len;
1369
1370         pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1371         if (!pdata) {
1372                 dev_err(&client->dev, "Can't allocate platform data\n");
1373                 return ERR_PTR(-ENOMEM);
1374         }
1375
1376         pchar = of_get_property(np, "orientation", &len);
1377         if (!pchar || len != sizeof(pdata->orientation)) {
1378                 dev_err(&client->dev, "Cannot read orientation property\n");
1379                 return ERR_PTR(-EINVAL);
1380         }
1381
1382         memcpy(pdata->orientation, pchar, len);
1383         if (of_property_read_string(np, "config", &pchar)) {
1384                 dev_err(&client->dev, "Cannot read config property\n");
1385                 return ERR_PTR(-EINVAL);
1386         }
1387
1388         for (config = 0; config < ARRAY_SIZE(akm_configs); config++) {
1389                 if (!strcasecmp(pchar, akm_configs[config])) {
1390                         pdata->config = config;
1391                         break;
1392                 }
1393         }
1394         if (config == ARRAY_SIZE(akm_configs)) {
1395                 dev_err(&client->dev, "Invalid config value\n");
1396                 return ERR_PTR(-EINVAL);
1397         }
1398
1399         return pdata;
1400 }
1401
1402 static int akm_probe(struct i2c_client *client,
1403                      const struct i2c_device_id *devid)
1404 {
1405         struct akm_inf *inf;
1406         struct mpu_platform_data *pd;
1407         int err;
1408
1409         dev_info(&client->dev, "%s\n", __func__);
1410         inf = devm_kzalloc(&client->dev, sizeof(*inf), GFP_KERNEL);
1411         if (!inf) {
1412                 dev_err(&client->dev, "%s kzalloc ERR\n", __func__);
1413                 return -ENOMEM;
1414         }
1415
1416         inf->i2c = client;
1417         i2c_set_clientdata(client, inf);
1418         if (client->dev.of_node) {
1419                 pd = akm_parse_dt(client);
1420                 if (IS_ERR(pd))
1421                         return -EINVAL;
1422         } else {
1423                 pd = (struct mpu_platform_data *)dev_get_platdata(&client->dev);
1424                 if (!pd)
1425                         return -EINVAL;
1426         }
1427
1428         inf->pdata = *pd;
1429         akm_pm_init(inf);
1430         err = akm_id(inf);
1431         akm_pm(inf, false);
1432         if (err == -EAGAIN)
1433                 goto akm_probe_again;
1434         else if (err)
1435                 goto akm_probe_err;
1436
1437         mutex_init(&inf->mutex_data);
1438         err = akm_input_create(inf);
1439         if (err)
1440                 goto akm_probe_err;
1441
1442         inf->wq = create_singlethread_workqueue(AKM_NAME);
1443         if (!inf->wq) {
1444                 dev_err(&client->dev, "%s workqueue ERR\n", __func__);
1445                 err = -ENOMEM;
1446                 goto akm_probe_err;
1447         }
1448
1449         INIT_DELAYED_WORK(&inf->dw, akm_work);
1450         err = akm_sysfs_create(inf);
1451         if (err)
1452                 goto akm_probe_err;
1453
1454         return 0;
1455
1456 akm_probe_err:
1457         dev_err(&client->dev, "%s ERR %d\n", __func__, err);
1458 akm_probe_again:
1459         akm_remove(client);
1460         return err;
1461 }
1462
1463 static int akm_suspend(struct device *dev)
1464 {
1465         struct akm_inf *inf;
1466         int err;
1467
1468         inf = dev_get_drvdata(dev);
1469         err = akm_enable(inf, false);
1470         if (err)
1471                 dev_err(dev, "%s ERR\n", __func__);
1472         dev_info(dev, "%s done\n", __func__);
1473         return 0;
1474 }
1475
1476 static const struct dev_pm_ops akm_pm_ops = {
1477         .suspend        = akm_suspend,
1478 };
1479
1480 static const struct i2c_device_id akm_i2c_device_id[] = {
1481         {AKM_NAME, 0},
1482         {"ak8963", 0},
1483         {"ak8972", 0},
1484         {"ak8975", 0},
1485         {}
1486 };
1487
1488 MODULE_DEVICE_TABLE(i2c, akm_i2c_device_id);
1489
1490 static const struct of_device_id akm_of_match[] = {
1491         { .compatible = "ak,ak89xx", },
1492         { .compatible = "ak,ak8963", },
1493         { .compatible = "ak,ak8972", },
1494         { .compatible = "ak,ak8975", },
1495         { },
1496 };
1497
1498 MODULE_DEVICE_TABLE(of, akm_of_match);
1499
1500 static struct i2c_driver akm_driver = {
1501         .class          = I2C_CLASS_HWMON,
1502         .probe          = akm_probe,
1503         .remove         = akm_remove,
1504         .driver = {
1505                 .name           = AKM_NAME,
1506                 .owner          = THIS_MODULE,
1507                 .of_match_table = of_match_ptr(akm_of_match),
1508                 .pm             = &akm_pm_ops,
1509         },
1510         .id_table       = akm_i2c_device_id,
1511         .shutdown       = akm_shutdown,
1512 };
1513
1514 static int __init akm_init(void)
1515 {
1516         return i2c_add_driver(&akm_driver);
1517 }
1518
1519 static void __exit akm_exit(void)
1520 {
1521         i2c_del_driver(&akm_driver);
1522 }
1523
1524 module_init(akm_init);
1525 module_exit(akm_exit);
1526
1527 MODULE_LICENSE("GPL");
1528 MODULE_DESCRIPTION("AKM driver");
1529