iio: common: NVS v209 common timestamps
[linux-3.10.git] / drivers / iio / magnetometer / nvi_ak89xx.c
1 /* Copyright (c) 2014-2015, NVIDIA CORPORATION.  All rights reserved.
2  *
3  * This software is licensed under the terms of the GNU General Public
4  * License version 2, as published by the Free Software Foundation, and
5  * may be copied, distributed, and modified under those terms.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 /* Device mapping is done via three parameters:
14  * 1. If AKM_NVI_MPU_SUPPORT (defined below) is set, the code is included to
15  *    support the device behind an Invensense MPU running an NVI (NVidia/
16  *    Invensense) driver.
17  *    If AKM_NVI_MPU_SUPPORT is 0 then this driver is only for the device in a
18  *    stand-alone configuration without any dependencies on an Invensense MPU.
19  * 2. Device tree platform configuration nvi_config:
20  *    - auto = automatically detect if connected to host or MPU
21  *    - mpu = connected to MPU
22  *    - host = connected to host
23  *    This is only available if AKM_NVI_MPU_SUPPORT is set.
24  * 3. device in board file:
25  *    - ak89xx = automatically detect the device
26  *    - force the device for:
27  *      - ak8963
28  *      - ak8975
29  *      - ak09911
30  * If you have no clue what the device is and don't know how it is
31  * connected then use auto and akm89xx.  The auto-detect mechanisms are for
32  * platforms that have multiple possible configurations but takes longer to
33  * initialize.  No device identification and connect testing is done for
34  * specific configurations.
35  *
36  * An interrupt can be used to configure the device.  When an interrupt is
37  * defined in struct i2c_client.irq, the driver is configured to only use the
38  * device's continuous mode if the device supports it.  If the device does not
39  * support continuous mode, then the interrupt is not used.
40  * If the device is connected to the MPU, the interrupt from the board file is
41  * used as a SW flag.  The interrupt itself is never touched so any value can
42  * be used.  If the struct i2c_client.irq is > 0, then the driver will only use
43  * the continuous modes of the device if supported.  This frees the MPU
44  * auxiliary port used for writes.  This configuration would be used if another
45  * MPU auxiliary port was needed for another device connected to the MPU.
46  * If the device is connected to the host, the delay timing used in continuous
47  * mode is the one closest to the device's supported modes.  Example: A 70ms
48  * request will use the 125ms from the possible 10ms and 125ms on the AK8963.
49  * If the device is connected to the MPU, the delay timing used in continuous
50  * mode is equal to or the next fastest supported speed.
51  */
52 /* The NVS = NVidia Sensor framework */
53 /* NVI = NVidia/Invensense */
54 /* See Nvs.cpp in the HAL for the NVS implementation of batch/flush. */
55 /* See NvsIio.cpp in the HAL for the IIO enable/disable extension mechanism. */
56
57
58 #include <linux/i2c.h>
59 #include <linux/module.h>
60 #include <linux/slab.h>
61 #include <linux/err.h>
62 #include <linux/delay.h>
63 #include <linux/regulator/consumer.h>
64 #include <linux/workqueue.h>
65 #include <linux/interrupt.h>
66 #include <linux/of.h>
67 #include <linux/nvs.h>
68 #if AKM_NVI_MPU_SUPPORT
69 #include <linux/mpu_iio.h>
70 #endif /* AKM_NVI_MPU_SUPPORT */
71
72 #define AKM_VENDOR                      "AsahiKASEI"
73 #define AKM_NAME                        "ak89xx"
74 #define AKM_NAME_AK8963                 "ak8963"
75 #define AKM_NAME_AK8975                 "ak8975"
76 #define AKM_NAME_AK09911                "ak09911"
77 #define AKM_KBUF_SIZE                   (32)
78 #define AKM_OFFSET_IVAL                 (0)
79 #define AKM_OFFSET_MICRO                (0)
80 #define AKM_DELAY_US_MAX                (255000)
81 #define AKM_HW_DELAY_POR_MS             (50)
82 #define AKM_HW_DELAY_TSM_MS             (10) /* Time Single Measurement */
83 #define AKM_HW_DELAY_US                 (100)
84 #define AKM_HW_DELAY_ROM_ACCESS_US      (200)
85 #define AKM_POLL_DELAY_MS_DFLT          (200)
86 #define AKM_MPU_RETRY_COUNT             (50)
87 #define AKM_MPU_RETRY_DELAY_MS          (20)
88 #define AKM_ERR_CNT_MAX                 (20)
89 /* HW registers */
90 #define AKM_WIA_ID                      (0x48)
91 #define AKM_DEVID_AK8963                (0x01)
92 #define AKM_DEVID_AK8975                (0x03)
93 #define AKM_DEVID_AK09911               (0x05)
94 #define AKM_REG_WIA                     (0x00)
95 #define AKM_REG_WIA2                    (0x01)
96 #define AKM_BIT_DRDY                    (0x01)
97 #define AKM_BIT_DOR                     (0x02)
98 #define AKM_BIT_DERR                    (0x04)
99 #define AKM_BIT_HOFL                    (0x08)
100 #define AKM_BIT_BITM                    (0x10)
101 #define AKM_BIT_SRST                    (0x01)
102 #define AKM_BIT_SELF                    (0x40)
103 #define AKM_MODE_POWERDOWN              (0x00)
104 #define AKM_MODE_SINGLE                 (0x01)
105
106 #define WR                              (0)
107 #define RD                              (1)
108 #define AXIS_X                          (0)
109 #define AXIS_Y                          (1)
110 #define AXIS_Z                          (2)
111 #define AXIS_N                          (3)
112
113
114 /* regulator names in order of powering on */
115 static char *akm_vregs[] = {
116         "vdd",
117         "vid",
118 };
119
120 static char *akm_configs[] = {
121         "auto",
122         "mpu",
123         "host",
124 };
125
126 static unsigned short akm_i2c_addrs[] = {
127         0x0C,
128         0x0D,
129         0x0E,
130         0x0F,
131 };
132
133 struct akm_scale {
134         struct nvs_float max_range;
135         struct nvs_float resolution;
136         struct nvs_float scale;
137         s16 range_lo[3];
138         s16 range_hi[3];
139 };
140
141 struct akm_cmode {
142         unsigned int t_us;
143         u8 mode;
144 };
145
146 struct akm_state {
147         struct i2c_client *i2c;
148         struct nvs_fn_if *nvs;
149         void *nvs_st;
150         struct sensor_cfg cfg;
151         struct regulator_bulk_data vreg[ARRAY_SIZE(akm_vregs)];
152         struct delayed_work dw;
153         struct akm_hal *hal;            /* Hardware Abstraction Layer */
154         u8 asa[3];                      /* axis sensitivity adjustment */
155         unsigned int sts;               /* status flags */
156         unsigned int errs;              /* error count */
157         unsigned int enabled;           /* enable status */
158         unsigned int poll_delay_us;     /* requested sampling delay (us) */
159         unsigned int scale_i;           /* scale index */
160         u16 i2c_addr;                   /* I2C address */
161         u8 dev_id;                      /* device ID */
162         bool irq_dis;                   /* interrupt host disable flag */
163         bool initd;                     /* set if initialized */
164         bool mpu_en;                    /* if device behind MPU */
165         bool port_en[2];                /* enable status of MPU write port */
166         int port_id[2];                 /* MPU port ID */
167         u8 data_out;                    /* write value to trigger a sample */
168         s16 magn_uc[AXIS_N];            /* uncalibrated sample data */
169         s16 magn[AXIS_N];               /* sample data after calibration */
170         u8 nvi_config;                  /* NVI configuration */
171 };
172
173 struct akm_hal {
174         const char *part;
175         int version;
176         struct akm_scale *scale;
177         u8 scale_i_max;
178         struct nvs_float milliamp;
179         unsigned int delay_us_min;
180         u8 reg_start_rd;
181         u8 reg_st1;
182         u8 reg_st2;
183         u8 reg_cntl1;
184         u8 reg_mode;
185         u8 reg_reset;
186         u8 reg_astc;
187         u8 reg_asa;
188         u8 mode_mask;
189         u8 mode_self_test;
190         u8 mode_rom_read;
191         struct akm_cmode *cmode_tbl;
192         bool irq;
193         unsigned int mpu_id;
194 };
195
196
197 static void akm_err(struct akm_state *st)
198 {
199         st->errs++;
200         if (!st->errs)
201                 st->errs--;
202 }
203
204 static int akm_i2c_rd(struct akm_state *st, u8 reg, u16 len, u8 *val)
205 {
206         struct i2c_msg msg[2];
207
208         msg[0].addr = st->i2c_addr;
209         msg[0].flags = 0;
210         msg[0].len = 1;
211         msg[0].buf = &reg;
212         msg[1].addr = st->i2c_addr;
213         msg[1].flags = I2C_M_RD;
214         msg[1].len = len;
215         msg[1].buf = val;
216         if (i2c_transfer(st->i2c->adapter, msg, 2) != 2) {
217                 akm_err(st);
218                 return -EIO;
219         }
220
221         return 0;
222 }
223
224 static int akm_i2c_wr(struct akm_state *st, u8 reg, u8 val)
225 {
226         struct i2c_msg msg;
227         u8 buf[2];
228
229         if (st->i2c_addr) {
230                 buf[0] = reg;
231                 buf[1] = val;
232                 msg.addr = st->i2c_addr;
233                 msg.flags = 0;
234                 msg.len = 2;
235                 msg.buf = buf;
236                 if (i2c_transfer(st->i2c->adapter, &msg, 1) != 1) {
237                         akm_err(st);
238                         return -EIO;
239                 }
240         }
241
242         return 0;
243 }
244
245 static int akm_nvi_mpu_bypass_request(struct akm_state *st)
246 {
247         int ret = 0;
248 #if AKM_NVI_MPU_SUPPORT
249         int i;
250
251         if (st->mpu_en) {
252                 for (i = 0; i < AKM_MPU_RETRY_COUNT; i++) {
253                         ret = nvi_mpu_bypass_request(true);
254                         if ((!ret) || (ret == -EPERM))
255                                 break;
256
257                         msleep(AKM_MPU_RETRY_DELAY_MS);
258                 }
259                 if (ret == -EPERM)
260                         ret = 0;
261         }
262 #endif /* AKM_NVI_MPU_SUPPORT */
263         return ret;
264 }
265
266 static int akm_nvi_mpu_bypass_release(struct akm_state *st)
267 {
268         int ret = 0;
269
270 #if AKM_NVI_MPU_SUPPORT
271         if (st->mpu_en)
272                 ret = nvi_mpu_bypass_release();
273 #endif /* AKM_NVI_MPU_SUPPORT */
274         return ret;
275 }
276
277 static int akm_mode_wr(struct akm_state *st, u8 mode)
278 {
279         int ret = 0;
280
281 #if AKM_NVI_MPU_SUPPORT
282         if (st->mpu_en && !st->i2c->irq) {
283                 ret = nvi_mpu_data_out(st->port_id[WR], mode);
284         } else {
285                 ret = akm_nvi_mpu_bypass_request(st);
286                 if (!ret) {
287                         if (st->i2c->irq) {
288                                 ret = akm_i2c_wr(st, st->hal->reg_mode,
289                                                  AKM_MODE_POWERDOWN);
290                                 if (mode & st->hal->mode_mask) {
291                                         udelay(AKM_HW_DELAY_US);
292                                         ret |= akm_i2c_wr(st,
293                                                           st->hal->reg_mode,
294                                                           mode);
295                                 }
296                         } else {
297                                 ret = akm_i2c_wr(st, st->hal->reg_mode, mode);
298                         }
299                         akm_nvi_mpu_bypass_release(st);
300                 }
301         }
302 #else /* AKM_NVI_MPU_SUPPORT */
303         if (st->i2c->irq) {
304                 ret = akm_i2c_wr(st, st->hal->reg_mode,
305                                  AKM_MODE_POWERDOWN);
306                 if (mode & st->hal->mode_mask) {
307                         udelay(AKM_HW_DELAY_US);
308                         ret |= akm_i2c_wr(st,
309                                           st->hal->reg_mode,
310                                           mode);
311                 }
312         } else {
313                 ret = akm_i2c_wr(st, st->hal->reg_mode, mode);
314         }
315 #endif /* AKM_NVI_MPU_SUPPORT */
316         if (!ret)
317                 st->data_out = mode;
318         return ret;
319 }
320
321 static int akm_pm(struct akm_state *st, bool enable)
322 {
323         int ret = 0;
324
325         if (enable) {
326                 ret = nvs_vregs_enable(&st->i2c->dev, st->vreg,
327                                        ARRAY_SIZE(akm_vregs));
328                 if (ret > 0)
329                         mdelay(AKM_HW_DELAY_POR_MS);
330         } else {
331                 if (st->i2c->irq) {
332                         ret = nvs_vregs_sts(st->vreg, ARRAY_SIZE(akm_vregs));
333                         if ((ret < 0) || (ret == ARRAY_SIZE(akm_vregs))) {
334                                 ret = akm_mode_wr(st, AKM_MODE_POWERDOWN);
335                         } else if (ret > 0) {
336                                 ret = nvs_vregs_enable(&st->i2c->dev, st->vreg,
337                                                        ARRAY_SIZE(akm_vregs));
338                                 mdelay(AKM_HW_DELAY_POR_MS);
339                                 ret = akm_mode_wr(st, AKM_MODE_POWERDOWN);
340                         }
341                 }
342                 ret |= nvs_vregs_disable(&st->i2c->dev, st->vreg,
343                                          ARRAY_SIZE(akm_vregs));
344         }
345         if (ret > 0)
346                 ret = 0;
347         if (ret) {
348                 dev_err(&st->i2c->dev, "%s pwr=%x ERR=%d\n",
349                         __func__, enable, ret);
350         } else {
351                 if (st->sts & NVS_STS_SPEW_MSG)
352                         dev_info(&st->i2c->dev, "%s pwr=%x\n",
353                                  __func__, enable);
354         }
355         return ret;
356 }
357
358 static int akm_port_free(struct akm_state *st, int port)
359 {
360         int ret = 0;
361
362 #if AKM_NVI_MPU_SUPPORT
363         if (st->port_id[port] >= 0) {
364                 ret = nvi_mpu_port_free(st->port_id[port]);
365                 if (!ret)
366                         st->port_id[port] = -1;
367         }
368 #endif /* AKM_NVI_MPU_SUPPORT */
369         return ret;
370 }
371
372 static int akm_ports_free(struct akm_state *st)
373 {
374         int ret;
375
376         ret = akm_port_free(st, WR);
377         ret |= akm_port_free(st, RD);
378         return ret;
379 }
380
381 static void akm_pm_exit(struct akm_state *st)
382 {
383         akm_ports_free(st);
384         akm_pm(st, false);
385         nvs_vregs_exit(&st->i2c->dev, st->vreg, ARRAY_SIZE(akm_vregs));
386 }
387
388 static int akm_pm_init(struct akm_state *st)
389 {
390         int ret;
391
392         st->enabled = 0;
393         st->poll_delay_us = (AKM_POLL_DELAY_MS_DFLT * 1000);
394         st->initd = false;
395         st->mpu_en = false;
396         st->port_en[WR] = false;
397         st->port_en[RD] = false;
398         st->port_id[WR] = -1;
399         st->port_id[RD] = -1;
400         nvs_vregs_init(&st->i2c->dev,
401                        st->vreg, ARRAY_SIZE(akm_vregs), akm_vregs);
402         ret = akm_pm(st, true);
403         return ret;
404 }
405
406 static int akm_port_enable(struct akm_state *st, int port, bool enable)
407 {
408         int ret = 0;
409
410 #if AKM_NVI_MPU_SUPPORT
411         if ((enable != st->port_en[port]) && (st->port_id[port] >= 0)) {
412                 ret = nvi_mpu_enable(st->port_id[port], enable);
413                 if (!ret)
414                         st->port_en[port] = enable;
415         }
416 #endif /* AKM_NVI_MPU_SUPPORT */
417         return ret;
418 }
419
420 static int akm_ports_enable(struct akm_state *st, bool enable)
421 {
422         int ret;
423
424         ret = akm_port_enable(st, RD, enable);
425         ret |= akm_port_enable(st, WR, enable);
426         return ret;
427 }
428
429 static int akm_reset_dev(struct akm_state *st)
430 {
431         u8 val;
432         unsigned int i;
433         int ret = 0;
434
435         if (st->hal->reg_reset) {
436                 ret = akm_nvi_mpu_bypass_request(st);
437                 if (!ret) {
438                         ret = akm_i2c_wr(st, st->hal->reg_reset,
439                                          AKM_BIT_SRST);
440                         for (i = 0; i < AKM_HW_DELAY_POR_MS; i++) {
441                                 mdelay(1);
442                                 ret = akm_i2c_rd(st, st->hal->reg_reset,
443                                                  1, &val);
444                                 if (ret)
445                                         continue;
446
447                                 if (!(val & AKM_BIT_SRST))
448                                         break;
449                         }
450                         akm_nvi_mpu_bypass_release(st);
451                 }
452         }
453
454         return 0;
455 }
456
457 static int akm_mode(struct akm_state *st)
458 {
459         u8 mode;
460         unsigned int t_us;
461         unsigned int i;
462         int ret;
463
464         mode = AKM_MODE_SINGLE;
465         if (st->i2c->irq) {
466                 i = 0;
467                 while (st->hal->cmode_tbl[i].t_us) {
468                         mode = st->hal->cmode_tbl[i].mode;
469                         t_us = st->hal->cmode_tbl[i].t_us;
470                         if (st->poll_delay_us >= st->hal->cmode_tbl[i].t_us)
471                                 break;
472
473                         i++;
474                         if (!st->mpu_en) {
475                                 t_us -= st->hal->cmode_tbl[i].t_us;
476                                 t_us >>= 1;
477                                 t_us += st->hal->cmode_tbl[i].t_us;
478                                 if (st->poll_delay_us > t_us)
479                                         break;
480                         }
481                 }
482         }
483         if (st->scale_i)
484                 mode |= AKM_BIT_BITM;
485         ret = akm_mode_wr(st, mode);
486         return ret;
487 }
488
489 static void akm_calc(struct akm_state *st, u8 *data)
490 {
491         s16 x;
492         s16 y;
493         s16 z;
494         u8 st1;
495
496         st1 = st->hal->reg_st1 - st->hal->reg_start_rd;
497         /* data[st1 + 1] = register HXL
498          * data[st1 + 2] = register HXH
499          * data[st1 + 3] = register HYL
500          * data[st1 + 4] = register HYH
501          * data[st1 + 5] = register HZL
502          * data[st1 + 6] = register HZH
503          */
504         x = (s16)le16_to_cpup((__le16 *)(&data[st1 + 1]));
505         y = (s16)le16_to_cpup((__le16 *)(&data[st1 + 3]));
506         z = (s16)le16_to_cpup((__le16 *)(&data[st1 + 5]));
507         st->magn_uc[AXIS_X] = x;
508         st->magn_uc[AXIS_Y] = y;
509         st->magn_uc[AXIS_Z] = z;
510         st->magn[AXIS_X] = (((int)x * (st->asa[AXIS_X] + 128)) >> 8);
511         st->magn[AXIS_Y] = (((int)y * (st->asa[AXIS_Y] + 128)) >> 8);
512         st->magn[AXIS_Z] = (((int)z * (st->asa[AXIS_Z] + 128)) >> 8);
513 }
514
515 static int akm_read_sts(struct akm_state *st, u8 *data)
516 {
517         u8 st1;
518         u8 st2;
519         int ret = 0; /* assume still processing */
520
521         st1 = st->hal->reg_st1 - st->hal->reg_start_rd;
522         st2 = st->hal->reg_st2 - st->hal->reg_start_rd;
523         if (data[st2] & (AKM_BIT_HOFL | AKM_BIT_DERR)) {
524                 if (st->sts & NVS_STS_SPEW_MSG)
525                         dev_info(&st->i2c->dev, "%s ERR\n", __func__);
526                 akm_err(st);
527                 ret = -1; /* error */
528         } else if (data[st1] & AKM_BIT_DRDY) {
529                 ret = 1; /* data ready to be reported */
530         }
531         return ret;
532 }
533
534 static int akm_read(struct akm_state *st)
535 {
536         s64 ts;
537         u8 data[10];
538         int ret;
539
540         ret = akm_i2c_rd(st, st->hal->reg_start_rd, 10, data);
541         if (ret)
542                 return ret;
543
544         ts = nvs_timestamp();
545         ret = akm_read_sts(st, data);
546         if (ret > 0) {
547                 akm_calc(st, data);
548                 st->nvs->handler(st->nvs_st, &st->magn, ts);
549         }
550         return ret;
551 }
552
553 #if AKM_NVI_MPU_SUPPORT
554 static void akm_mpu_handler(u8 *data, unsigned int len, s64 ts, void *p_val)
555 {
556         struct akm_state *st = (struct akm_state *)p_val;
557         int ret;
558
559         if (!ts) {
560                 /* no timestamp means flush done */
561                 st->nvs->handler(st->nvs_st, NULL, 0);
562                 return;
563         }
564
565         if (st->enabled) {
566                 ret = akm_read_sts(st, data);
567                 if (ret > 0) {
568                         akm_calc(st, data);
569                         st->nvs->handler(st->nvs_st, &st->magn, ts);
570                 }
571         }
572 }
573 #endif /* AKM_NVI_MPU_SUPPORT */
574
575 static void akm_work(struct work_struct *ws)
576 {
577         struct akm_state *st = container_of((struct delayed_work *)ws,
578                                             struct akm_state, dw);
579         int ret;
580
581         st->nvs->nvs_mutex_lock(st->nvs_st);
582         if (st->enabled) {
583                 ret = akm_read(st);
584                 if (ret > 0) {
585                         akm_i2c_wr(st, st->hal->reg_mode, st->data_out);
586                 } else if (ret < 0) {
587                         akm_reset_dev(st);
588                         akm_mode(st);
589                 }
590                 schedule_delayed_work(&st->dw,
591                                       usecs_to_jiffies(st->poll_delay_us));
592         }
593         st->nvs->nvs_mutex_unlock(st->nvs_st);
594 }
595
596 static irqreturn_t akm_irq_thread(int irq, void *dev_id)
597 {
598         struct akm_state *st = (struct akm_state *)dev_id;
599         int ret;
600
601         ret = akm_read(st);
602         if (ret < 0) {
603                 akm_reset_dev(st);
604                 akm_mode(st);
605         }
606         return IRQ_HANDLED;
607 }
608
609 static int akm_self_test(struct akm_state *st)
610 {
611         u8 data[10];
612         u8 mode;
613         int ret;
614         int ret_t;
615
616         ret_t = akm_i2c_wr(st, st->hal->reg_mode, AKM_MODE_POWERDOWN);
617         udelay(AKM_HW_DELAY_US);
618         if (st->hal->reg_astc) {
619                 ret_t |= akm_i2c_wr(st, st->hal->reg_astc, AKM_BIT_SELF);
620                 udelay(AKM_HW_DELAY_US);
621         }
622         mode = st->hal->mode_self_test;
623         if (st->scale_i)
624                 mode |= AKM_BIT_BITM;
625         ret_t |= akm_i2c_wr(st, st->hal->reg_mode, mode);
626         mdelay(AKM_HW_DELAY_TSM_MS);
627         ret = akm_i2c_rd(st, st->hal->reg_start_rd, 10, data);
628         if (!ret) {
629                 ret = akm_read_sts(st, data);
630                 if (ret > 0) {
631                         akm_calc(st, data);
632                         ret = 0;
633                 } else {
634                         ret = -EBUSY;
635                 }
636         }
637         ret_t |= ret;
638         if (st->hal->reg_astc)
639                 akm_i2c_wr(st, st->hal->reg_astc, 0);
640         if (!ret_t) {
641                 if ((st->magn[AXIS_X] <
642                                st->hal->scale[st->scale_i].range_lo[AXIS_X]) ||
643                                 (st->magn[AXIS_X] >
644                                  st->hal->scale[st->scale_i].range_hi[AXIS_X]))
645                         ret_t |= 1 << AXIS_X;
646                 if ((st->magn[AXIS_Y] <
647                                st->hal->scale[st->scale_i].range_lo[AXIS_Y]) ||
648                                 (st->magn[AXIS_Y] >
649                                  st->hal->scale[st->scale_i].range_hi[AXIS_Y]))
650                         ret_t |= 1 << AXIS_Y;
651                 if ((st->magn[AXIS_Z] <
652                                st->hal->scale[st->scale_i].range_lo[AXIS_Z]) ||
653                                 (st->magn[AXIS_Z] >
654                                  st->hal->scale[st->scale_i].range_hi[AXIS_Z]))
655                         ret_t |= 1 << AXIS_Z;
656                 if (ret_t) {
657                         dev_err(&st->i2c->dev, "%s ERR: out_of_range %x\n",
658                                 __func__, ret_t);
659
660                 }
661         } else {
662                 dev_err(&st->i2c->dev, "%s ERR: %d\n",
663                         __func__, ret_t);
664         }
665         return ret_t;
666 }
667
668 static int akm_init_hw(struct akm_state *st)
669 {
670         int ret;
671
672         ret = akm_nvi_mpu_bypass_request(st);
673         if (!ret) {
674                 ret = akm_i2c_wr(st, st->hal->reg_mode,
675                                  st->hal->mode_rom_read);
676                 udelay(AKM_HW_DELAY_ROM_ACCESS_US);
677                 ret |= akm_i2c_rd(st, st->hal->reg_asa, 3, st->asa);
678                 ret |= akm_i2c_wr(st, st->hal->reg_mode, AKM_MODE_POWERDOWN);
679                 akm_self_test(st);
680                 akm_nvi_mpu_bypass_release(st);
681         }
682         if (!ret)
683                 st->initd = true;
684         else
685                 dev_err(&st->i2c->dev, "%s ERR %d\n", __func__, ret);
686         return ret;
687 }
688
689 static void nvi_disable_irq(struct akm_state *st)
690 {
691         if (!st->irq_dis) {
692                 disable_irq_nosync(st->i2c->irq);
693                 st->irq_dis = true;
694         }
695 }
696
697 static void nvi_enable_irq(struct akm_state *st)
698 {
699         if (st->irq_dis) {
700                 enable_irq(st->i2c->irq);
701                 st->irq_dis = false;
702         }
703 }
704
705 static int akm_dis(struct akm_state *st)
706 {
707         int ret = 0;
708
709         if (st->mpu_en) {
710                 ret = akm_ports_enable(st, false);
711         } else {
712                 if (st->i2c->irq)
713                         nvi_disable_irq(st);
714                 else
715                         cancel_delayed_work(&st->dw);
716         }
717         if (!ret)
718                 st->enabled = 0;
719         return ret;
720 }
721
722 static int akm_disable(struct akm_state *st)
723 {
724         int ret;
725
726         ret = akm_dis(st);
727         if (!ret)
728                 akm_pm(st, false);
729         return ret;
730 }
731
732 static int akm_en(struct akm_state *st)
733 {
734         int ret = 0;
735
736         akm_pm(st, true);
737         if (!st->initd)
738                 ret = akm_init_hw(st);
739         return ret;
740 }
741
742 static int akm_enable(void *client, int snsr_id, int enable)
743 {
744         struct akm_state *st = (struct akm_state *)client;
745         int ret;
746
747         if (enable < 0)
748                 return st->enabled;
749
750         if (enable) {
751                 ret = akm_en(st);
752                 if (!ret) {
753                         ret = akm_mode(st);
754                         if (st->mpu_en)
755                                 ret |= akm_ports_enable(st, true);
756                         if (ret) {
757                                 akm_disable(st);
758                         } else {
759                                 st->enabled = 1;
760                                 if (!st->mpu_en) {
761                                         if (st->i2c->irq)
762                                                 nvi_enable_irq(st);
763                                         else
764                                                 schedule_delayed_work(&st->dw,
765                                                               usecs_to_jiffies(
766                                                            st->poll_delay_us));
767                                 }
768                         }
769                 }
770         } else {
771                 ret = akm_disable(st);
772         }
773         return ret;
774 }
775
776 static int akm_batch(void *client, int snsr_id, int flags,
777                      unsigned int period, unsigned int timeout)
778 {
779         struct akm_state *st = (struct akm_state *)client;
780         int ret = 0;
781
782         if (period < st->hal->delay_us_min)
783                 period = st->hal->delay_us_min;
784         if (period != st->poll_delay_us) {
785 #if AKM_NVI_MPU_SUPPORT
786                 if (st->port_id[RD] >= 0)
787                         ret = nvi_mpu_batch(st->port_id[RD], flags,
788                                             period, timeout);
789                 if (!ret)
790 #endif /* AKM_NVI_MPU_SUPPORT */
791                         st->poll_delay_us = period;
792                 if (st->enabled && st->i2c->irq && !ret)
793                         ret = akm_mode(st);
794         }
795         return ret;
796 }
797
798 static int akm_flush(void *client, int snsr_id)
799 {
800         struct akm_state *st = (struct akm_state *)client;
801         int ret = -EINVAL;
802
803 #if AKM_NVI_MPU_SUPPORT
804         if (st->mpu_en)
805                 ret = nvi_mpu_flush(st->port_id[RD]);
806 #endif /* AKM_NVI_MPU_SUPPORT */
807         return ret;
808 }
809
810 static int akm_resolution(void *client, int snsr_id, int resolution)
811 {
812         struct akm_state *st = (struct akm_state *)client;
813         unsigned int scale_i = st->scale_i;
814         int ret = 0;
815
816         if (resolution < 0 || resolution > st->hal->scale_i_max)
817                 return -EINVAL;
818
819         st->scale_i = resolution;
820         if (st->enabled && (resolution != scale_i)) {
821                 ret = akm_mode(st);
822                 if (ret < 0)
823                         st->scale_i = scale_i;
824         }
825
826         st->cfg.max_range.ival = st->hal->scale[st->scale_i].max_range.ival;
827         st->cfg.max_range.fval = st->hal->scale[st->scale_i].max_range.fval;
828         st->cfg.resolution.ival = st->hal->scale[st->scale_i].resolution.ival;
829         st->cfg.resolution.fval = st->hal->scale[st->scale_i].resolution.fval;
830         st->cfg.scale.ival = st->hal->scale[st->scale_i].scale.ival;
831         st->cfg.scale.fval = st->hal->scale[st->scale_i].scale.fval;
832         return ret;
833 }
834
835 static int akm_reset(void *client, int snsr_id)
836 {
837         struct akm_state *st = (struct akm_state *)client;
838         unsigned int enabled = st->enabled;
839         int ret;
840
841         akm_dis(st);
842         akm_pm(st, true);
843         ret = akm_reset_dev(st);
844         akm_enable(st, snsr_id, enabled);
845         return ret;
846 }
847
848 static int akm_selftest(void *client, int snsr_id, char *buf)
849 {
850         struct akm_state *st = (struct akm_state *)client;
851         unsigned int enabled = st->enabled;
852         ssize_t t;
853         int ret;
854
855         akm_dis(st);
856         akm_en(st);
857         ret = akm_nvi_mpu_bypass_request(st);
858         if (!ret) {
859                 ret = akm_self_test(st);
860                 akm_nvi_mpu_bypass_release(st);
861         }
862         if (buf) {
863                 if (ret < 0) {
864                         t = sprintf(buf, "ERR: %d\n", ret);
865                 } else {
866                         if (ret > 0)
867                                 t = sprintf(buf, "%d FAIL", ret);
868                         else
869                                 t = sprintf(buf, "%d PASS", ret);
870                         t += sprintf(buf + t, "   xyz: %hd %hd %hd   ",
871                                      st->magn[AXIS_X],
872                                      st->magn[AXIS_Y],
873                                      st->magn[AXIS_Z]);
874                         t += sprintf(buf + t, "uncalibrated: %hd %hd %hd   ",
875                                     st->magn_uc[AXIS_X],
876                                     st->magn_uc[AXIS_Y],
877                                     st->magn_uc[AXIS_Z]);
878                         if (ret > 0) {
879                                 if (ret & (1 << AXIS_X))
880                                         t += sprintf(buf + t, "X ");
881                                 if (ret & (1 << AXIS_Y))
882                                         t += sprintf(buf + t, "Y ");
883                                 if (ret & (1 << AXIS_Z))
884                                         t += sprintf(buf + t, "Z ");
885                         }
886                         t += sprintf(buf + t, "\n");
887                 }
888         }
889         akm_enable(st, 0, enabled);
890         if (buf)
891                 return t;
892
893         return ret;
894 }
895
896 static int akm_regs(void *client, int snsr_id, char *buf)
897 {
898         struct akm_state *st = (struct akm_state *)client;
899         ssize_t t;
900         u8 data1[25];
901         u8 data2[3];
902         unsigned int i;
903         int ret;
904
905         if (!st->initd)
906                 t = sprintf(buf, "calibration: NEED ENABLE\n");
907         else
908                 t = sprintf(buf, "calibration: x=%#2x y=%#2x z=%#2x\n",
909                             st->asa[AXIS_X],
910                             st->asa[AXIS_Y],
911                             st->asa[AXIS_Z]);
912         ret = akm_nvi_mpu_bypass_request(st);
913         if (!ret) {
914                 ret = akm_i2c_rd(st, AKM_REG_WIA,
915                                  st->hal->reg_st2 + 1, data1);
916                 ret |= akm_i2c_rd(st, st->hal->reg_cntl1, 3, data2);
917                 akm_nvi_mpu_bypass_release(st);
918         }
919         if (ret) {
920                 t += sprintf(buf + t, "registers: ERR %d\n", ret);
921         } else {
922                 t += sprintf(buf + t, "registers:\n");
923                 for (i = 0; i <= st->hal->reg_st2; i++)
924                         t += sprintf(buf + t, "%#2x=%#2x\n",
925                                      AKM_REG_WIA + i, data1[i]);
926                 for (i = 0; i < 3; i++)
927                         t += sprintf(buf + t, "%#2x=%#2x\n",
928                                      st->hal->reg_cntl1 + i,
929                                      data2[i]);
930         }
931         return t;
932 }
933
934 static struct nvs_fn_dev akm_fn_dev = {
935         .enable                         = akm_enable,
936         .batch                          = akm_batch,
937         .flush                          = akm_flush,
938         .resolution                     = akm_resolution,
939         .reset                          = akm_reset,
940         .self_test                      = akm_selftest,
941         .regs                           = akm_regs,
942 };
943
944 static int akm_suspend(struct device *dev)
945 {
946         struct i2c_client *client = to_i2c_client(dev);
947         struct akm_state *st = i2c_get_clientdata(client);
948         int ret = 0;
949
950         st->sts |= NVS_STS_SUSPEND;
951         if (st->nvs && st->nvs_st)
952                 ret = st->nvs->suspend(st->nvs_st);
953         if (st->sts & NVS_STS_SPEW_MSG)
954                 dev_info(&client->dev, "%s\n", __func__);
955         return ret;
956 }
957
958 static int akm_resume(struct device *dev)
959 {
960         struct i2c_client *client = to_i2c_client(dev);
961         struct akm_state *st = i2c_get_clientdata(client);
962         int ret = 0;
963
964         if (st->nvs && st->nvs_st)
965                 ret = st->nvs->resume(st->nvs_st);
966         st->sts &= ~NVS_STS_SUSPEND;
967         if (st->sts & NVS_STS_SPEW_MSG)
968                 dev_info(&client->dev, "%s\n", __func__);
969         return ret;
970 }
971
972 static SIMPLE_DEV_PM_OPS(akm_pm_ops, akm_suspend, akm_resume);
973
974 static void akm_shutdown(struct i2c_client *client)
975 {
976         struct akm_state *st = i2c_get_clientdata(client);
977
978         st->sts |= NVS_STS_SHUTDOWN;
979         if (st->nvs && st->nvs_st)
980                 st->nvs->shutdown(st->nvs_st);
981         if (st->sts & NVS_STS_SPEW_MSG)
982                 dev_info(&client->dev, "%s\n", __func__);
983 }
984
985 static int akm_remove(struct i2c_client *client)
986 {
987         struct akm_state *st = i2c_get_clientdata(client);
988
989         if (st != NULL) {
990                 akm_shutdown(client);
991                 if (st->nvs) {
992                         if (st->nvs_st)
993                                 st->nvs->remove(st->nvs_st);
994                 }
995                 if (st->dw.wq)
996                         destroy_workqueue(st->dw.wq);
997                 akm_pm_exit(st);
998         }
999         dev_info(&client->dev, "%s\n", __func__);
1000         return 0;
1001 }
1002
1003 static struct akm_scale akm_scale_09911[] = {
1004         {
1005                 .max_range              = {
1006                         .ival           = 9825,
1007                         .fval           = 0,
1008                 },
1009                 .resolution             = {
1010                         .ival           = 0,
1011                         .fval           = 600000,
1012                 },
1013                 .scale                  = {
1014                         .ival           = 0,
1015                         .fval           = 600000,
1016                 },
1017                 .range_lo[AXIS_X]       = -30,
1018                 .range_hi[AXIS_X]       = 30,
1019                 .range_lo[AXIS_Y]       = -30,
1020                 .range_hi[AXIS_Y]       = 30,
1021                 .range_lo[AXIS_Z]       = -400,
1022                 .range_hi[AXIS_Z]       = -50,
1023         },
1024 };
1025
1026 static struct akm_cmode akm_cmode_09911[] = {
1027         {
1028                 .t_us                   = 100000,
1029                 .mode                   = 0x02,
1030         },
1031         {
1032                 .t_us                   = 50000,
1033                 .mode                   = 0x04,
1034         },
1035         {
1036                 .t_us                   = 20000,
1037                 .mode                   = 0x06,
1038         },
1039         {
1040                 .t_us                   = 10000,
1041                 .mode                   = 0x08,
1042         },
1043         {},
1044 };
1045
1046 static struct akm_hal akm_hal_09911 = {
1047         .part                           = AKM_NAME_AK09911,
1048         .version                        = 2,
1049         .scale                          = akm_scale_09911,
1050         .scale_i_max                    = ARRAY_SIZE(akm_scale_09911) - 1,
1051         .milliamp                       = {
1052                 .ival                   = 2,
1053                 .fval                   = 400000,
1054         },
1055         .delay_us_min                   = 10000,
1056         .reg_start_rd                   = 0x10,
1057         .reg_st1                        = 0x10,
1058         .reg_st2                        = 0x18,
1059         .reg_cntl1                      = 0x30,
1060         .reg_mode                       = 0x31,
1061         .reg_reset                      = 0x32,
1062         .reg_astc                       = 0, /* N/A */
1063         .reg_asa                        = 0x60,
1064         .mode_mask                      = 0x1F,
1065         .mode_self_test                 = 0x10,
1066         .mode_rom_read                  = 0x1F,
1067         .cmode_tbl                      = akm_cmode_09911,
1068         .irq                            = false,
1069 #if AKM_NVI_MPU_SUPPORT
1070         .mpu_id                         = COMPASS_ID_AK09911,
1071 #endif /* AKM_NVI_MPU_SUPPORT */
1072 };
1073
1074 static struct akm_scale akm_scale_8975[] = {
1075         {
1076                 .max_range              = {
1077                         .ival           = 2459,
1078                         .fval           = 0,
1079                 },
1080                 .resolution             = {
1081                         .ival           = 0,
1082                         .fval           = 300000,
1083                 },
1084                 .scale                  = {
1085                         .ival           = 0,
1086                         .fval           = 300000,
1087                 },
1088                 .range_lo[AXIS_X]       = -100,
1089                 .range_hi[AXIS_X]       = 100,
1090                 .range_lo[AXIS_Y]       = -100,
1091                 .range_hi[AXIS_Y]       = 100,
1092                 .range_lo[AXIS_Z]       = -1000,
1093                 .range_hi[AXIS_Z]       = -300,
1094         },
1095 };
1096
1097 static struct akm_hal akm_hal_8975 = {
1098         .part                           = AKM_NAME_AK8975,
1099         .version                        = 2,
1100         .scale                          = akm_scale_8975,
1101         .scale_i_max                    = ARRAY_SIZE(akm_scale_8975) - 1,
1102         .milliamp                       = {
1103                 .ival                   = 3,
1104                 .fval                   = 0,
1105         },
1106         .delay_us_min                   = 10000,
1107         .reg_start_rd                   = 0x01,
1108         .reg_st1                        = 0x02,
1109         .reg_st2                        = 0x09,
1110         .reg_cntl1                      = 0x0A,
1111         .reg_mode                       = 0x0A,
1112         .reg_reset                      = 0, /* N/A */
1113         .reg_astc                       = 0x0C,
1114         .reg_asa                        = 0x10,
1115         .mode_mask                      = 0x0F,
1116         .mode_self_test                 = 0x08,
1117         .mode_rom_read                  = 0x0F,
1118         .cmode_tbl                      = NULL,
1119         .irq                            = true,
1120 #if AKM_NVI_MPU_SUPPORT
1121         .mpu_id                         = COMPASS_ID_AK8975,
1122 #endif /* AKM_NVI_MPU_SUPPORT */
1123 };
1124
1125 static struct akm_scale akm_scale_8963[] = {
1126         {
1127                 .max_range              = {
1128                         .ival           = 9825,
1129                         .fval           = 0,
1130                 },
1131                 .resolution             = {
1132                         .ival           = 0,
1133                         .fval           = 600000,
1134                 },
1135                 .scale                  = {
1136                         .ival           = 0,
1137                         .fval           = 600000,
1138                 },
1139                 .range_lo[AXIS_X]       = -50,
1140                 .range_hi[AXIS_X]       = 50,
1141                 .range_lo[AXIS_Y]       = -50,
1142                 .range_hi[AXIS_Y]       = 50,
1143                 .range_lo[AXIS_Z]       = -800,
1144                 .range_hi[AXIS_Z]       = -200,
1145         },
1146         {
1147                 .max_range              = {
1148                         .ival           = 9825,
1149                         .fval           = 0,
1150                 },
1151                 .resolution             = {
1152                         .ival           = 0,
1153                         .fval           = 150000,
1154                 },
1155                 .scale                  = {
1156                         .ival           = 0,
1157                         .fval           = 150000,
1158                 },
1159                 .range_lo[AXIS_X]       = -200,
1160                 .range_hi[AXIS_X]       = 200,
1161                 .range_lo[AXIS_Y]       = -200,
1162                 .range_hi[AXIS_Y]       = 200,
1163                 .range_lo[AXIS_Z]       = -3200,
1164                 .range_hi[AXIS_Z]       = -800,
1165         },
1166 };
1167
1168 static struct akm_cmode akm_cmode_8963[] = {
1169         {
1170                 .t_us                   = 125000,
1171                 .mode                   = 0x02,
1172         },
1173         {
1174                 .t_us                   = 10000,
1175                 .mode                   = 0x04,
1176         },
1177         {},
1178 };
1179
1180 static struct akm_hal akm_hal_8963 = {
1181         .part                           = AKM_NAME_AK8963,
1182         .version                        = 2,
1183         .scale                          = akm_scale_8963,
1184         .scale_i_max                    = ARRAY_SIZE(akm_scale_8963) - 1,
1185         .milliamp                       = {
1186                 .ival                   = 2,
1187                 .fval                   = 800000,
1188         },
1189         .delay_us_min                   = 10000,
1190         .reg_start_rd                   = 0x01,
1191         .reg_st1                        = 0x02,
1192         .reg_st2                        = 0x09,
1193         .reg_cntl1                      = 0x0A,
1194         .reg_mode                       = 0x0A,
1195         .reg_reset                      = 0x0B,
1196         .reg_astc                       = 0x0C,
1197         .reg_asa                        = 0x10,
1198         .mode_mask                      = 0x0F,
1199         .mode_self_test                 = 0x08,
1200         .mode_rom_read                  = 0x0F,
1201         .cmode_tbl                      = akm_cmode_8963,
1202         .irq                            = true,
1203 #if AKM_NVI_MPU_SUPPORT
1204         .mpu_id                         = COMPASS_ID_AK8963,
1205 #endif /* AKM_NVI_MPU_SUPPORT */
1206 };
1207
1208 static int akm_id_hal(struct akm_state *st, u8 dev_id)
1209 {
1210         int ret = 0;
1211
1212         switch (dev_id) {
1213         case AKM_DEVID_AK09911:
1214                 st->hal = &akm_hal_09911;
1215                 break;
1216
1217         case AKM_DEVID_AK8975:
1218                 st->hal = &akm_hal_8975;
1219                 break;
1220
1221         case AKM_DEVID_AK8963:
1222                 st->hal = &akm_hal_8963;
1223                 break;
1224
1225         default:
1226                 st->hal = &akm_hal_8975;
1227                 ret = -ENODEV;
1228         }
1229         st->scale_i = st->hal->scale_i_max;
1230         st->cfg.name = "magnetic_field";
1231         st->cfg.kbuf_sz = AKM_KBUF_SIZE;
1232         st->cfg.ch_n = 3;
1233         st->cfg.ch_sz = -2;
1234         st->cfg.part = st->hal->part;
1235         st->cfg.vendor = AKM_VENDOR;
1236         st->cfg.version = st->hal->version;
1237         st->cfg.max_range.ival = st->hal->scale[st->scale_i].max_range.ival;
1238         st->cfg.max_range.fval = st->hal->scale[st->scale_i].max_range.fval;
1239         st->cfg.resolution.ival = st->hal->scale[st->scale_i].resolution.ival;
1240         st->cfg.resolution.fval = st->hal->scale[st->scale_i].resolution.fval;
1241         st->cfg.milliamp.ival = st->hal->milliamp.ival;
1242         st->cfg.milliamp.fval = st->hal->milliamp.fval;
1243         st->cfg.delay_us_min = st->hal->delay_us_min;
1244         st->cfg.delay_us_max = AKM_DELAY_US_MAX;
1245         st->cfg.scale.ival = st->hal->scale[st->scale_i].scale.ival;
1246         st->cfg.scale.fval = st->hal->scale[st->scale_i].scale.fval;
1247         st->cfg.offset.ival = AKM_OFFSET_IVAL;
1248         st->cfg.offset.fval = AKM_OFFSET_MICRO;
1249         return ret;
1250 }
1251
1252 static int akm_id_compare(struct akm_state *st, const char *name)
1253 {
1254         u8 wia;
1255         u8 val;
1256         int ret;
1257         int ret_t;
1258
1259         ret_t = akm_nvi_mpu_bypass_request(st);
1260         if (!ret_t) {
1261                 ret_t = akm_i2c_rd(st, AKM_REG_WIA2, 1, &wia);
1262                 if (ret_t)
1263                         wia = 0;
1264                 akm_id_hal(st, wia);
1265                 if (wia != AKM_DEVID_AK09911) {
1266                         /* we can autodetect AK8963 with BITM */
1267                         ret = akm_i2c_wr(st, st->hal->reg_mode,
1268                                          AKM_BIT_BITM);
1269                         if (!ret) {
1270                                 ret = akm_i2c_rd(st, st->hal->reg_st2,
1271                                                  1, &val);
1272                                 if (!ret) {
1273                                         if (val & AKM_BIT_BITM)
1274                                                 wia = AKM_DEVID_AK8963;
1275                                         else
1276                                                 wia = AKM_DEVID_AK8975;
1277                                         akm_id_hal(st, wia);
1278                                 } else {
1279                                         ret_t |= ret;
1280                                         wia = 0;
1281                                 }
1282                         } else {
1283                                 ret_t |= ret;
1284                         }
1285                 }
1286                 akm_nvi_mpu_bypass_release(st);
1287                 if ((!st->dev_id) && (!wia)) {
1288                         dev_err(&st->i2c->dev, "%s ERR: %s HW ID FAIL\n",
1289                                 __func__, name);
1290                         ret = -ENODEV;
1291                 } else if ((!st->dev_id) && wia) {
1292                         st->dev_id = wia;
1293                         dev_dbg(&st->i2c->dev, "%s %s using ID %x\n",
1294                                 __func__, name, st->dev_id);
1295                 } else if (st->dev_id && (!wia)) {
1296                         dev_err(&st->i2c->dev, "%s WARN: %s HW ID FAIL\n",
1297                                 __func__, name);
1298                 } else if (st->dev_id != wia) {
1299                         dev_err(&st->i2c->dev, "%s WARN: %s != HW ID %x\n",
1300                                 __func__, name, wia);
1301                         st->dev_id = wia;
1302                 } else {
1303                         dev_dbg(&st->i2c->dev, "%s %s == HW ID %x\n",
1304                                 __func__, name, wia);
1305                 }
1306         }
1307         return ret_t;
1308 }
1309
1310 static int akm_id_dev(struct akm_state *st, const char *name)
1311 {
1312 #if AKM_NVI_MPU_SUPPORT
1313         struct nvi_mpu_port nmp;
1314         u8 config_boot;
1315 #endif /* AKM_NVI_MPU_SUPPORT */
1316         u8 val = 0;
1317         int ret;
1318
1319         if (st->i2c->irq < 0)
1320                 st->i2c->irq = 0;
1321         if (!strcmp(name, AKM_NAME_AK8963))
1322                 st->dev_id = AKM_DEVID_AK8963;
1323         else if (!strcmp(name, AKM_NAME_AK8975))
1324                 st->dev_id = AKM_DEVID_AK8975;
1325         else if (!strcmp(name, AKM_NAME_AK09911))
1326                 st->dev_id = AKM_DEVID_AK09911;
1327 #if AKM_NVI_MPU_SUPPORT
1328         config_boot = st->nvi_config & NVI_CONFIG_BOOT_MASK;
1329         if (config_boot == NVI_CONFIG_BOOT_AUTO) {
1330                 nmp.addr = st->i2c_addr | 0x80;
1331                 nmp.reg = AKM_REG_WIA;
1332                 nmp.ctrl = 1;
1333                 ret = nvi_mpu_dev_valid(&nmp, &val);
1334                 dev_info(&st->i2c->dev, "%s AUTO ID=%x ret=%d\n",
1335                          __func__, val, ret);
1336                 /* see mpu_iio.h for possible return values */
1337                 if ((ret == -EAGAIN) || (ret == -EBUSY))
1338                         return -EAGAIN;
1339
1340                 if ((val == AKM_WIA_ID) || ((ret == -EIO) && st->dev_id))
1341                         config_boot = NVI_CONFIG_BOOT_MPU;
1342         }
1343         if (config_boot == NVI_CONFIG_BOOT_MPU) {
1344                 st->mpu_en = true;
1345                 if (st->dev_id)
1346                         ret = akm_id_hal(st, st->dev_id);
1347                 else
1348                         ret = akm_id_compare(st, name);
1349                 if (!ret) {
1350                         nmp.addr = st->i2c_addr | 0x80;
1351                         nmp.reg = st->hal->reg_start_rd;
1352                         nmp.ctrl = 10; /* MPU FIFO can't handle odd size */
1353                         nmp.data_out = 0;
1354                         nmp.delay_ms = 0;
1355                         nmp.delay_us = st->poll_delay_us;
1356                         if ((st->hal->cmode_tbl != NULL) && st->i2c->irq)
1357                                 nmp.shutdown_bypass = true;
1358                         else
1359                                 nmp.shutdown_bypass = false;
1360                         nmp.handler = &akm_mpu_handler;
1361                         nmp.ext_driver = (void *)st;
1362                         nmp.type = SECONDARY_SLAVE_TYPE_COMPASS;
1363                         nmp.id = st->hal->mpu_id;
1364                         nmp.rate_scale = 10;
1365                         ret = nvi_mpu_port_alloc(&nmp);
1366                         dev_dbg(&st->i2c->dev, "%s MPU port/ret=%d\n",
1367                                 __func__, ret);
1368                         if (ret < 0)
1369                                 return ret;
1370
1371                         st->port_id[RD] = ret;
1372                         ret = 0;
1373                         if ((st->hal->cmode_tbl == NULL) || !st->i2c->irq) {
1374                                 st->i2c->irq = 0;
1375                                 nmp.addr = st->i2c_addr;
1376                                 nmp.reg = st->hal->reg_mode;
1377                                 nmp.ctrl = 1;
1378                                 nmp.data_out = AKM_MODE_SINGLE;
1379                                 nmp.delay_ms = AKM_HW_DELAY_TSM_MS;
1380                                 nmp.delay_us = 0;
1381                                 nmp.shutdown_bypass = false;
1382                                 nmp.handler = NULL;
1383                                 nmp.ext_driver = NULL;
1384                                 nmp.type = SECONDARY_SLAVE_TYPE_COMPASS;
1385                                 ret = nvi_mpu_port_alloc(&nmp);
1386                                 dev_dbg(&st->i2c->dev, "%s MPU port/ret=%d\n",
1387                                         __func__, ret);
1388                                 if (ret < 0) {
1389                                         akm_ports_free(st);
1390                                 } else {
1391                                         st->port_id[WR] = ret;
1392                                         ret = 0;
1393                                 }
1394                         }
1395                 }
1396                 nvi_mpu_fifo(st->port_id[RD],
1397                              &st->cfg.fifo_rsrv_evnt_cnt,
1398                              &st->cfg.fifo_max_evnt_cnt);
1399                 return ret;
1400         }
1401 #endif /* AKM_NVI_MPU_SUPPORT */
1402         /* NVI_CONFIG_BOOT_HOST */
1403         st->mpu_en = false;
1404         if (st->dev_id) {
1405                 ret = akm_id_hal(st, st->dev_id);
1406         } else {
1407                 ret = akm_i2c_rd(st, AKM_REG_WIA, 1, &val);
1408                 dev_info(&st->i2c->dev, "%s Host read ID=%x ret=%d\n",
1409                         __func__, val, ret);
1410                 if ((!ret) && (val == AKM_WIA_ID))
1411                         ret = akm_id_compare(st, name);
1412         }
1413         if (st->i2c->irq && !ret) {
1414                 if ((st->hal->cmode_tbl == NULL) || !st->hal->irq) {
1415                         nvi_disable_irq(st);
1416                         st->i2c->irq = 0;
1417                 }
1418         }
1419         return ret;
1420 }
1421
1422 static int akm_id_i2c(struct akm_state *st,
1423                       const struct i2c_device_id *id)
1424 {
1425         int i;
1426         int ret;
1427
1428         for (i = 0; i < ARRAY_SIZE(akm_i2c_addrs); i++) {
1429                 if (st->i2c->addr == akm_i2c_addrs[i])
1430                         break;
1431         }
1432
1433         if (i < ARRAY_SIZE(akm_i2c_addrs)) {
1434                 st->i2c_addr = st->i2c->addr;
1435                 ret = akm_id_dev(st, id->name);
1436         } else {
1437                 for (i = 0; i < ARRAY_SIZE(akm_i2c_addrs); i++) {
1438                         st->i2c_addr = akm_i2c_addrs[i];
1439                         ret = akm_id_dev(st, AKM_NAME);
1440                         if ((ret == -EAGAIN) || (!ret))
1441                                 break;
1442                 }
1443         }
1444         if (ret)
1445                 st->i2c_addr = 0;
1446         return ret;
1447 }
1448
1449 static int akm_of_dt(struct akm_state *st, struct device_node *dn)
1450 {
1451         char const *pchar;
1452         u8 cfg;
1453         int ret;
1454
1455         /* just test if global disable */
1456         ret = nvs_of_dt(dn, NULL, NULL);
1457         if (ret == -ENODEV)
1458                 return -ENODEV;
1459
1460         /* this device supports these programmable parameters */
1461         if (!(of_property_read_string(dn, "nvi_config", &pchar))) {
1462                 for (cfg = 0; cfg < ARRAY_SIZE(akm_configs); cfg++) {
1463                         if (!strcasecmp(pchar, akm_configs[cfg])) {
1464                                 st->nvi_config = cfg;
1465                                 break;
1466                         }
1467                 }
1468         }
1469
1470         return 0;
1471 }
1472
1473 static int akm_probe(struct i2c_client *client,
1474                      const struct i2c_device_id *id)
1475 {
1476         struct akm_state *st;
1477 #if AKM_NVI_MPU_SUPPORT
1478         struct mpu_platform_data *pd;
1479 #endif /* AKM_NVI_MPU_SUPPORT */
1480         int ret;
1481
1482         st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
1483         if (st == NULL) {
1484                 dev_err(&client->dev, "%s devm_kzalloc ERR\n", __func__);
1485                 return -ENOMEM;
1486         }
1487
1488         i2c_set_clientdata(client, st);
1489         st->i2c = client;
1490         /* default matrix to get the attribute */
1491         st->cfg.matrix[0] = 1;
1492         st->cfg.matrix[4] = 1;
1493         st->cfg.matrix[8] = 1;
1494         if (client->dev.of_node) {
1495                 ret = akm_of_dt(st, client->dev.of_node);
1496                 if (ret) {
1497                         if (ret == -ENODEV) {
1498                                 dev_info(&client->dev, "%s DT disabled\n",
1499                                          __func__);
1500                         } else {
1501                                 dev_err(&client->dev, "%s _of_dt ERR\n",
1502                                         __func__);
1503                                 ret = -ENODEV;
1504                         }
1505                         goto akm_probe_err;
1506                 }
1507 #if AKM_NVI_MPU_SUPPORT
1508         } else {
1509                 pd = (struct mpu_platform_data *)
1510                                                 dev_get_platdata(&client->dev);
1511                 memcpy(st->cfg.matrix, &pd->orientation,
1512                        sizeof(st->cfg.matrix));
1513 #endif /* AKM_NVI_MPU_SUPPORT */
1514         }
1515
1516         akm_pm_init(st);
1517         ret = akm_id_i2c(st, id);
1518         if (ret == -EAGAIN)
1519                 goto akm_probe_again;
1520         else if (ret)
1521                 goto akm_probe_err;
1522
1523         akm_init_hw(st);
1524         akm_pm(st, false);
1525         akm_fn_dev.errs = &st->errs;
1526         akm_fn_dev.sts = &st->sts;
1527         st->nvs = nvs_iio();
1528         if (st->nvs == NULL) {
1529                 ret = -ENODEV;
1530                 goto akm_probe_err;
1531         }
1532
1533         nvs_of_dt(client->dev.of_node, &st->cfg, NULL);
1534         ret = st->nvs->probe(&st->nvs_st, st, &client->dev,
1535                              &akm_fn_dev, &st->cfg);
1536         if (ret) {
1537                 dev_err(&client->dev, "%s nvs_probe ERR\n", __func__);
1538                 ret = -ENODEV;
1539                 goto akm_probe_err;
1540         }
1541
1542         if (!st->mpu_en)
1543                 INIT_DELAYED_WORK(&st->dw, akm_work);
1544         if ((st->i2c->irq > 0) && !st->mpu_en) {
1545                 ret = request_threaded_irq(st->i2c->irq, NULL, akm_irq_thread,
1546                                            IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1547                                            AKM_NAME, st);
1548                 if (ret) {
1549                         dev_err(&client->dev, "%s req_threaded_irq ERR %d\n",
1550                                 __func__, ret);
1551                         ret = -ENOMEM;
1552                         goto akm_probe_err;
1553                 }
1554         }
1555
1556         dev_info(&client->dev, "%s done\n", __func__);
1557         return 0;
1558
1559 akm_probe_err:
1560         dev_err(&client->dev, "%s ERR %d\n", __func__, ret);
1561 akm_probe_again:
1562         akm_remove(client);
1563         return ret;
1564 }
1565
1566 static const struct i2c_device_id akm_i2c_device_id[] = {
1567         { AKM_NAME, 0 },
1568         { AKM_NAME_AK8963, 0 },
1569         { AKM_NAME_AK8975, 0 },
1570         { AKM_NAME_AK09911, 0 },
1571         {}
1572 };
1573
1574 MODULE_DEVICE_TABLE(i2c, akm_i2c_device_id);
1575
1576 static const struct of_device_id akm_of_match[] = {
1577         { .compatible = "ak,ak89xx", },
1578         { .compatible = "ak,ak8963", },
1579         { .compatible = "ak,ak8975", },
1580         { .compatible = "ak,ak09911", },
1581         {}
1582 };
1583
1584 MODULE_DEVICE_TABLE(of, akm_of_match);
1585
1586 static struct i2c_driver akm_driver = {
1587         .class                          = I2C_CLASS_HWMON,
1588         .probe                          = akm_probe,
1589         .remove                         = akm_remove,
1590         .shutdown                       = akm_shutdown,
1591         .driver                         = {
1592                 .name                   = AKM_NAME,
1593                 .owner                  = THIS_MODULE,
1594                 .of_match_table         = of_match_ptr(akm_of_match),
1595                 .pm                     = &akm_pm_ops,
1596         },
1597         .id_table                       = akm_i2c_device_id,
1598 };
1599
1600 static int __init akm_init(void)
1601 {
1602         return i2c_add_driver(&akm_driver);
1603 }
1604
1605 static void __exit akm_exit(void)
1606 {
1607         i2c_del_driver(&akm_driver);
1608 }
1609
1610 late_initcall(akm_init);
1611 module_exit(akm_exit);
1612
1613 MODULE_LICENSE("GPL");
1614 MODULE_DESCRIPTION("AKM driver");
1615 MODULE_AUTHOR("NVIDIA Corporation");
1616