cfa6115caa1c5ba46a111e91597d74872c4b8747
[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          (20)
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_TS1                     (0x0D)
70 #define AKM_REG_TS2                     (0x0E)
71 #define AKM_REG_I2CDIS                  (0x0F)
72 #define AKM_REG_ASAX                    (0x10)
73 #define AKM_REG_ASAY                    (0x11)
74 #define AKM_REG_ASAZ                    (0x12)
75
76 #define AKM8963_SCALE14                 (19661)
77 #define AKM8963_SCALE16                 (4915)
78 #define AKM8972_SCALE                   (19661)
79 #define AKM8975_SCALE                   (9830)
80 #define AKM8975_RANGE_X_HI              (100)
81 #define AKM8975_RANGE_X_LO              (-100)
82 #define AKM8975_RANGE_Y_HI              (100)
83 #define AKM8975_RANGE_Y_LO              (-100)
84 #define AKM8975_RANGE_Z_HI              (-300)
85 #define AKM8975_RANGE_Z_LO              (-1000)
86 #define AKM8972_RANGE_X_HI              (50)
87 #define AKM8972_RANGE_X_LO              (-50)
88 #define AKM8972_RANGE_Y_HI              (50)
89 #define AKM8972_RANGE_Y_LO              (-50)
90 #define AKM8972_RANGE_Z_HI              (-100)
91 #define AKM8972_RANGE_Z_LO              (-500)
92 #define AKM8963_RANGE_X_HI              (200)
93 #define AKM8963_RANGE_X_LO              (-200)
94 #define AKM8963_RANGE_Y_HI              (200)
95 #define AKM8963_RANGE_Y_LO              (-200)
96 #define AKM8963_RANGE_Z_HI              (-800)
97 #define AKM8963_RANGE_Z_LO              (-3200)
98 #define AXIS_X                          (0)
99 #define AXIS_Y                          (1)
100 #define AXIS_Z                          (2)
101 #define WR                              (0)
102 #define RD                              (1)
103
104
105 /* regulator names in order of powering on */
106 static char *akm_vregs[] = {
107         "vdd",
108         "vid",
109 };
110
111 static char *akm_configs[] = {
112         "auto",
113         "mpu",
114         "host",
115 };
116
117 struct akm_asa {
118         u8 asa[3];                      /* axis sensitivity adjustment */
119         s16 range_lo[3];
120         s16 range_hi[3];
121 };
122
123 struct akm_inf {
124         struct i2c_client *i2c;
125         struct mutex mutex_data;
126         struct input_dev *idev;
127         struct workqueue_struct *wq;
128         struct delayed_work dw;
129         struct regulator_bulk_data vreg[ARRAY_SIZE(akm_vregs)];
130         struct mpu_platform_data pdata;
131         struct akm_asa asa;             /* data for calibration */
132         unsigned int compass_id;        /* compass id */
133         unsigned long poll_delay_us;    /* requested sampling delay (us) */
134         unsigned int resolution;        /* report when new data outside this */
135         bool use_mpu;                   /* if device behind MPU */
136         bool initd;                     /* set if initialized */
137         bool enable;                    /* enable status */
138         bool port_en[2];                /* enable status of MPU write port */
139         int port_id[2];                 /* MPU port ID */
140         u8 data_out;                    /* write value to trigger a sample */
141         u8 range_index;                 /* max_range index */
142         short xyz[3];                   /* sample data */
143         bool cycle;                     /* cycle MPU en/dis for high speed */
144         unsigned long cycle_delay_us;   /* cycle off time (us) */
145         s64 cycle_ts;                   /* cycle MPU disable timestamp */
146 };
147
148
149 static int akm_i2c_rd(struct akm_inf *inf, u8 reg, u16 len, u8 *val)
150 {
151         struct i2c_msg msg[2];
152
153         msg[0].addr = inf->i2c->addr;
154         msg[0].flags = 0;
155         msg[0].len = 1;
156         msg[0].buf = &reg;
157         msg[1].addr = inf->i2c->addr;
158         msg[1].flags = I2C_M_RD;
159         msg[1].len = len;
160         msg[1].buf = val;
161         if (i2c_transfer(inf->i2c->adapter, msg, 2) != 2)
162                 return -EIO;
163
164         return 0;
165 }
166
167 static int akm_i2c_wr(struct akm_inf *inf, u8 reg, u8 val)
168 {
169         struct i2c_msg msg;
170         u8 buf[2];
171
172         buf[0] = reg;
173         buf[1] = val;
174         msg.addr = inf->i2c->addr;
175         msg.flags = 0;
176         msg.len = 2;
177         msg.buf = buf;
178         if (i2c_transfer(inf->i2c->adapter, &msg, 1) != 1)
179                 return -EIO;
180
181         return 0;
182 }
183
184 static int akm_vreg_dis(struct akm_inf *inf, int i)
185 {
186         int err = 0;
187
188         if (inf->vreg[i].ret && (inf->vreg[i].consumer != NULL)) {
189                 err = regulator_disable(inf->vreg[i].consumer);
190                 if (err)
191                         dev_err(&inf->i2c->dev, "%s %s ERR\n",
192                                 __func__, inf->vreg[i].supply);
193                 else
194                         dev_dbg(&inf->i2c->dev, "%s %s\n",
195                                 __func__, inf->vreg[i].supply);
196         }
197         inf->vreg[i].ret = 0;
198         return err;
199 }
200
201 static int akm_vreg_dis_all(struct akm_inf *inf)
202 {
203         unsigned int i;
204         int err = 0;
205
206         for (i = ARRAY_SIZE(akm_vregs); i > 0; i--)
207                 err |= akm_vreg_dis(inf, (i - 1));
208         return err;
209 }
210
211 static int akm_vreg_en(struct akm_inf *inf, int i)
212 {
213         int err = 0;
214
215         if ((!inf->vreg[i].ret) && (inf->vreg[i].consumer != NULL)) {
216                 err = regulator_enable(inf->vreg[i].consumer);
217                 if (!err) {
218                         inf->vreg[i].ret = 1;
219                         dev_dbg(&inf->i2c->dev, "%s %s\n",
220                                 __func__, inf->vreg[i].supply);
221                         err = 1; /* flag regulator state change */
222                 } else {
223                         dev_err(&inf->i2c->dev, "%s %s ERR\n",
224                                 __func__, inf->vreg[i].supply);
225                 }
226         }
227         return err;
228 }
229
230 static int akm_vreg_en_all(struct akm_inf *inf)
231 {
232         int i;
233         int err = 0;
234
235         for (i = 0; i < ARRAY_SIZE(akm_vregs); i++)
236                 err |= akm_vreg_en(inf, i);
237         return err;
238 }
239
240 static void akm_vreg_exit(struct akm_inf *inf)
241 {
242         int i;
243
244         for (i = 0; i < ARRAY_SIZE(akm_vregs); i++) {
245                 if (inf->vreg[i].consumer != NULL) {
246                         devm_regulator_put(inf->vreg[i].consumer);
247                         inf->vreg[i].consumer = NULL;
248                         dev_dbg(&inf->i2c->dev, "%s %s\n",
249                                 __func__, inf->vreg[i].supply);
250                 }
251         }
252 }
253
254 static int akm_vreg_init(struct akm_inf *inf)
255 {
256         unsigned int i;
257         int err = 0;
258
259         for (i = 0; i < ARRAY_SIZE(akm_vregs); i++) {
260                 inf->vreg[i].supply = akm_vregs[i];
261                 inf->vreg[i].ret = 0;
262                 inf->vreg[i].consumer = devm_regulator_get(&inf->i2c->dev,
263                                                           inf->vreg[i].supply);
264                 if (IS_ERR(inf->vreg[i].consumer)) {
265                         err |= PTR_ERR(inf->vreg[i].consumer);
266                         dev_err(&inf->i2c->dev, "%s err %d for %s\n",
267                                 __func__, err, inf->vreg[i].supply);
268                         inf->vreg[i].consumer = NULL;
269                 } else {
270                         dev_dbg(&inf->i2c->dev, "%s %s\n",
271                                 __func__, inf->vreg[i].supply);
272                 }
273         }
274         return err;
275 }
276
277 static int akm_pm(struct akm_inf *inf, bool enable)
278 {
279         int err;
280
281         if (enable) {
282                 err = akm_vreg_en_all(inf);
283                 if (err)
284                         mdelay(AKM_HW_DELAY_POR_MS);
285         } else {
286                 err = akm_vreg_dis_all(inf);
287         }
288         if (err > 0)
289                 err = 0;
290         if (err)
291                 dev_err(&inf->i2c->dev, "%s enable=%x ERR=%d\n",
292                         __func__, enable, err);
293         else
294                 dev_dbg(&inf->i2c->dev, "%s enable=%x\n",
295                         __func__, enable);
296         return err;
297 }
298
299 static int akm_port_free(struct akm_inf *inf, int port)
300 {
301         int err = 0;
302
303         if ((inf->use_mpu) && (inf->port_id[port] >= 0)) {
304                 err = nvi_mpu_port_free(inf->port_id[port]);
305                 if (!err)
306                         inf->port_id[port] = -1;
307         }
308         return err;
309 }
310
311 static int akm_ports_free(struct akm_inf *inf)
312 {
313         int err;
314
315         err = akm_port_free(inf, WR);
316         err |= akm_port_free(inf, RD);
317         return err;
318 }
319
320 static void akm_pm_exit(struct akm_inf *inf)
321 {
322         akm_ports_free(inf);
323         akm_pm(inf, false);
324         akm_vreg_exit(inf);
325 }
326
327 static int akm_pm_init(struct akm_inf *inf)
328 {
329         int err;
330
331         inf->initd = false;
332         inf->enable = false;
333         inf->port_en[WR] = false;
334         inf->port_en[RD] = false;
335         inf->port_id[WR] = -1;
336         inf->port_id[RD] = -1;
337         inf->poll_delay_us = (AKM_POLL_DELAY_MS_DFLT * 1000);
338         inf->cycle_delay_us = (AKM_INPUT_DELAY_MS_MIN * 1000);
339         akm_vreg_init(inf);
340         err = akm_pm(inf, true);
341         return err;
342 }
343
344 static int akm_nvi_mpu_bypass_request(struct akm_inf *inf)
345 {
346         int i;
347         int err = 0;
348
349         if (inf->use_mpu) {
350                 for (i = 0; i < AKM_MPU_RETRY_COUNT; i++) {
351                         err = nvi_mpu_bypass_request(true);
352                         if ((!err) || (err == -EPERM))
353                                 break;
354
355                         mdelay(AKM_MPU_RETRY_DELAY_MS);
356                 }
357                 if (err == -EPERM)
358                         err = 0;
359         }
360         return err;
361 }
362
363 static int akm_nvi_mpu_bypass_release(struct akm_inf *inf)
364 {
365         int err = 0;
366
367         if (inf->use_mpu)
368                 err = nvi_mpu_bypass_release();
369         return err;
370 }
371
372 static int akm_wr(struct akm_inf *inf, u8 reg, u8 val)
373 {
374         int err = 0;
375
376         err = akm_nvi_mpu_bypass_request(inf);
377         if (!err) {
378                 err = akm_i2c_wr(inf, AKM_REG_CNTL1, AKM_CNTL1_MODE_POWERDOWN);
379                 udelay(AKM_HW_DELAY_US);
380                 err |= akm_i2c_wr(inf, reg, val);
381                 akm_nvi_mpu_bypass_release(inf);
382         }
383         return err;
384 }
385
386 static int akm_port_enable(struct akm_inf *inf, int port, bool enable)
387 {
388         int err = 0;
389
390         if (enable != inf->port_en[port]) {
391                 err = nvi_mpu_enable(inf->port_id[port], enable, false);
392                 if (!err)
393                         inf->port_en[port] = enable;
394         }
395         return err;
396 }
397
398 static int akm_ports_enable(struct akm_inf *inf, bool enable)
399 {
400         int err;
401
402         err = akm_port_enable(inf, RD, enable);
403         err |= akm_port_enable(inf, WR, enable);
404         return err;
405 }
406
407 static int akm_mode_wr(struct akm_inf *inf, bool reset, u8 range, u8 mode)
408 {
409         u8 mode_old;
410         u8 mode_new;
411         u8 val;
412         int i;
413         int err = 0;
414
415         mode_new = mode;
416         if (range)
417                 mode |= AKM_CNTL1_OUTPUT_BIT16;
418         if ((mode == inf->data_out) && (!reset))
419                 return err;
420
421         mode_old = inf->data_out & AKM_CNTL1_MODE_MASK;
422         if (inf->use_mpu)
423                 err = akm_ports_enable(inf, false);
424         else
425                 cancel_delayed_work_sync(&inf->dw);
426         if (err)
427                 return err;
428
429         if (reset) {
430                 if (inf->compass_id == COMPASS_ID_AK8963) {
431                         err = akm_nvi_mpu_bypass_request(inf);
432                         if (!err) {
433                                 err = akm_wr(inf, AKM_REG_CNTL2,
434                                              AKM_CNTL2_SRST);
435                                 for (i = 0; i < AKM_HW_DELAY_POR_MS; i++) {
436                                         mdelay(1);
437                                         err = akm_i2c_rd(inf, AKM_REG_CNTL2,
438                                                          1, &val);
439                                         if (err)
440                                                 continue;
441
442                                         if (!(val & AKM_CNTL2_SRST))
443                                                 break;
444                                 }
445                                 akm_nvi_mpu_bypass_release(inf);
446                         }
447                 }
448         }
449         inf->range_index = range;
450         inf->data_out = mode;
451         if (inf->use_mpu) {
452                 if ((mode_old > AKM_CNTL1_MODE_SINGLE) ||
453                                             (mode_new > AKM_CNTL1_MODE_SINGLE))
454                         err = akm_wr(inf, AKM_REG_CNTL1, mode);
455                 if (mode_new <= AKM_CNTL1_MODE_SINGLE) {
456                         err |= nvi_mpu_data_out(inf->port_id[WR], mode);
457                         if (mode_new)
458                                 err |= akm_ports_enable(inf, true);
459                 } else {
460                         err |= akm_port_enable(inf, RD, true);
461                 }
462         } else {
463                 err = akm_wr(inf, AKM_REG_CNTL1, mode);
464                 if (mode_new)
465                         queue_delayed_work(inf->wq, &inf->dw,
466                                          usecs_to_jiffies(inf->poll_delay_us));
467         }
468         return err;
469 }
470
471 static int akm_delay(struct akm_inf *inf, unsigned long delay_us)
472 {
473         u8 mode;
474         int err = 0;
475
476         if (inf->use_mpu)
477                 err |= nvi_mpu_delay_us(inf->port_id[RD], delay_us);
478         if (!err) {
479                 if (inf->compass_id == COMPASS_ID_AK8963) {
480                         if (delay_us == (AKM_INPUT_DELAY_MS_MIN * 1000))
481                                 mode = AKM_CNTL1_MODE_CONT2;
482                         else
483                                 mode = AKM_CNTL1_MODE_SINGLE;
484                         err = akm_mode_wr(inf, false, inf->range_index, mode);
485                 } else {
486                         if ((delay_us == (AKM_INPUT_DELAY_MS_MIN * 1000)) &&
487                                                            inf->cycle_delay_us)
488                                 inf->cycle = true;
489                         else
490                                 inf->cycle = false;
491                 }
492         }
493         return err;
494 }
495
496 static int akm_init_hw(struct akm_inf *inf)
497 {
498         u8 val[8];
499         int err;
500         int err_t;
501
502         err_t = akm_nvi_mpu_bypass_request(inf);
503         if (!err_t) {
504                 err_t = akm_wr(inf, AKM_REG_CNTL1, AKM_CNTL1_MODE_ROM_ACCESS);
505                 udelay(AKM_HW_DELAY_ROM_ACCESS_US);
506                 err_t |= akm_i2c_rd(inf, AKM_REG_ASAX, 3, inf->asa.asa);
507                 /* we can autodetect AK8963 with BITM */
508                 inf->compass_id = 0;
509                 err = akm_wr(inf, AKM_REG_CNTL1, (AKM_CNTL1_MODE_SINGLE |
510                                                   AKM_CNTL1_OUTPUT_BIT16));
511                 if (!err) {
512                         mdelay(AKM_HW_DELAY_TSM_MS);
513                         err = akm_i2c_rd(inf, AKM_REG_ST2, 1, val);
514                         if ((!err) && (val[0] & AKM_CNTL1_OUTPUT_BIT16))
515                                 inf->compass_id = COMPASS_ID_AK8963;
516                 }
517                 akm_nvi_mpu_bypass_release(inf);
518                 if (!inf->compass_id)
519                         inf->compass_id = inf->pdata.sec_slave_id;
520                 if (inf->compass_id == COMPASS_ID_AK8963) {
521                         inf->asa.range_lo[AXIS_X] = AKM8963_RANGE_X_LO;
522                         inf->asa.range_hi[AXIS_X] = AKM8963_RANGE_X_HI;
523                         inf->asa.range_lo[AXIS_Y] = AKM8963_RANGE_Y_LO;
524                         inf->asa.range_hi[AXIS_Y] = AKM8963_RANGE_Y_HI;
525                         inf->asa.range_lo[AXIS_Z] = AKM8963_RANGE_Z_LO;
526                         inf->asa.range_hi[AXIS_Z] = AKM8963_RANGE_Z_HI;
527                         inf->range_index = 1;
528                 } else if (inf->compass_id == COMPASS_ID_AK8972) {
529                         inf->asa.range_lo[AXIS_X] = AKM8972_RANGE_X_LO;
530                         inf->asa.range_hi[AXIS_X] = AKM8972_RANGE_X_HI;
531                         inf->asa.range_lo[AXIS_Y] = AKM8972_RANGE_Y_LO;
532                         inf->asa.range_hi[AXIS_Y] = AKM8972_RANGE_Y_HI;
533                         inf->asa.range_lo[AXIS_Z] = AKM8972_RANGE_Z_LO;
534                         inf->asa.range_hi[AXIS_Z] = AKM8972_RANGE_Z_HI;
535                         inf->range_index = 0;
536                 } else { /* default COMPASS_ID_AK8975 */
537                         inf->compass_id = COMPASS_ID_AK8975;
538                         inf->asa.range_lo[AXIS_X] = AKM8975_RANGE_X_LO;
539                         inf->asa.range_hi[AXIS_X] = AKM8975_RANGE_X_HI;
540                         inf->asa.range_lo[AXIS_Y] = AKM8975_RANGE_Y_LO;
541                         inf->asa.range_hi[AXIS_Y] = AKM8975_RANGE_Y_HI;
542                         inf->asa.range_lo[AXIS_Z] = AKM8975_RANGE_Z_LO;
543                         inf->asa.range_hi[AXIS_Z] = AKM8975_RANGE_Z_HI;
544                         inf->range_index = 0;
545                 }
546         }
547         if (!err_t)
548                 inf->initd = true;
549         else
550                 dev_err(&inf->i2c->dev, "%s ERR %d", __func__, err_t);
551         return err_t;
552 }
553
554 static void akm_calc(struct akm_inf *inf, u8 *data)
555 {
556         short x;
557         short y;
558         short z;
559
560         /* data[1] = AKM_REG_HXL
561          * data[2] = AKM_REG_HXH
562          * data[3] = AKM_REG_HYL
563          * data[4] = AKM_REG_HYH
564          * data[5] = AKM_REG_HZL
565          * data[6] = AKM_REG_HZH
566          */
567         x = ((data[2] << 8) | data[1]);
568         y = ((data[4] << 8) | data[3]);
569         z = ((data[6] << 8) | data[5]);
570         x = ((x * (inf->asa.asa[AXIS_X] + 128)) >> 8);
571         y = ((y * (inf->asa.asa[AXIS_Y] + 128)) >> 8);
572         z = ((z * (inf->asa.asa[AXIS_Z] + 128)) >> 8);
573         mutex_lock(&inf->mutex_data);
574         inf->xyz[AXIS_X] = x;
575         inf->xyz[AXIS_Y] = y;
576         inf->xyz[AXIS_Z] = z;
577         mutex_unlock(&inf->mutex_data);
578 }
579
580 static void akm_report(struct akm_inf *inf, u8 *data, s64 ts)
581 {
582         akm_calc(inf, data);
583         input_report_rel(inf->idev, REL_X, inf->xyz[AXIS_X]);
584         input_report_rel(inf->idev, REL_Y, inf->xyz[AXIS_Y]);
585         input_report_rel(inf->idev, REL_Z, inf->xyz[AXIS_Z]);
586         input_report_rel(inf->idev, REL_MISC, (unsigned int)(ts >> 32));
587         input_report_rel(inf->idev, REL_WHEEL,
588                          (unsigned int)(ts & 0xffffffff));
589         input_sync(inf->idev);
590 }
591
592 static int akm_read_sts(struct akm_inf *inf, u8 *data)
593 {
594         int err = -1; /* assume something wrong */
595         /* data[0] = AKM_REG_ST1
596          * data[7] = AKM_REG_ST2
597          * data[8] = AKM_REG_CNTL1
598          */
599         if ((data[0] & AKM_ST1_DRDY) && (!(data[7] &
600                                            (AKM_ST2_HOFL | AKM_ST2_DERR))))
601                 err = 1; /* data ready to be reported */
602         else if ((data[8] & AKM_CNTL1_MODE_MASK) == (inf->data_out &
603                                                      AKM_CNTL1_MODE_MASK))
604                 err = 0; /* still processing */
605         return err;
606 }
607
608 static s64 akm_timestamp_ns(void)
609 {
610         struct timespec ts;
611         s64 ns;
612
613         ktime_get_ts(&ts);
614         ns = timespec_to_ns(&ts);
615         return ns;
616 }
617
618 static int akm_read(struct akm_inf *inf)
619 {
620         long long timestamp1;
621         long long timestamp2;
622         u8 data[9];
623         int err;
624
625         timestamp1 = akm_timestamp_ns();
626         err = akm_i2c_rd(inf, AKM_REG_ST1, 9, data);
627         timestamp2 = akm_timestamp_ns();
628         if (err)
629                 return err;
630
631         err = akm_read_sts(inf, data);
632         if (err > 0) {
633                 timestamp2 = (timestamp2 - timestamp1) / 2;
634                 timestamp1 += timestamp2;
635                 akm_report(inf, data, timestamp1);
636                 if ((inf->data_out & AKM_CNTL1_MODE_MASK) ==
637                                                          AKM_CNTL1_MODE_SINGLE)
638                         akm_i2c_wr(inf, AKM_REG_CNTL1, inf->data_out);
639         } else if (err < 0) {
640                         dev_err(&inf->i2c->dev, "%s ERR\n", __func__);
641                         akm_mode_wr(inf, true, inf->range_index,
642                                     inf->data_out & AKM_CNTL1_MODE_MASK);
643         }
644         return err;
645 }
646
647 static void akm_mpu_handler(u8 *data, unsigned int len, s64 ts, void *p_val)
648 {
649         struct akm_inf *inf;
650         int err;
651
652         inf = (struct akm_inf *)p_val;
653         if (inf->enable) {
654                 if (inf->cycle && (!inf->port_en[WR])) {
655                         ts -= inf->cycle_ts;
656                         if (ts > (inf->cycle_delay_us * 1000))
657                                 akm_port_enable(inf, WR, true);
658                         return;
659                 }
660                 err = akm_read_sts(inf, data);
661                 if (err > 0) {
662                         akm_report(inf, data, ts);
663                         if (inf->cycle) {
664                                 akm_port_enable(inf, WR, false);
665                                 inf->cycle_ts = ts;
666                         }
667                 }
668         }
669 }
670
671 static int akm_id(struct akm_inf *inf)
672 {
673         struct nvi_mpu_port nmp;
674         u8 config_boot;
675         u8 val = 0;
676         int err;
677
678         config_boot = inf->pdata.config & NVI_CONFIG_BOOT_MASK;
679         if (config_boot == NVI_CONFIG_BOOT_AUTO) {
680                 nmp.addr = inf->i2c->addr | 0x80;
681                 nmp.reg = AKM_REG_WIA;
682                 nmp.ctrl = 1;
683                 err = nvi_mpu_dev_valid(&nmp, &val);
684                 /* see mpu.h for possible return values */
685                 if ((err == -EAGAIN) || (err == -EBUSY))
686                         return -EAGAIN;
687
688                 if (((!err) && (val == AKM_WIA_ID)) || (err == -EIO))
689                         config_boot = NVI_CONFIG_BOOT_MPU;
690         }
691         if (config_boot == NVI_CONFIG_BOOT_MPU) {
692                 inf->use_mpu = true;
693                 nmp.addr = inf->i2c->addr | 0x80;
694                 nmp.reg = AKM_REG_ST1;
695                 nmp.ctrl = 9;
696                 nmp.data_out = 0;
697                 nmp.delay_ms = 0;
698                 nmp.delay_us = inf->poll_delay_us;
699                 nmp.shutdown_bypass = false;
700                 nmp.handler = &akm_mpu_handler;
701                 nmp.ext_driver = (void *)inf;
702                 err = nvi_mpu_port_alloc(&nmp);
703                 if (err < 0)
704                         return err;
705
706                 inf->port_id[RD] = err;
707                 nmp.addr = inf->i2c->addr;
708                 nmp.reg = AKM_REG_CNTL1;
709                 nmp.ctrl = 1;
710                 nmp.data_out = inf->data_out;
711                 nmp.delay_ms = AKM_HW_DELAY_TSM_MS;
712                 nmp.delay_us = 0;
713                 nmp.shutdown_bypass = false;
714                 nmp.handler = NULL;
715                 nmp.ext_driver = NULL;
716                 err = nvi_mpu_port_alloc(&nmp);
717                 if (err < 0) {
718                         akm_ports_free(inf);
719                 } else {
720                         inf->port_id[WR] = err;
721                         err = 0;
722                 }
723                 return err;
724         }
725
726         /* NVI_CONFIG_BOOT_EXTERNAL */
727         inf->use_mpu = false;
728         err = akm_i2c_rd(inf, AKM_REG_WIA, 1, &val);
729         if ((!err) && (val == AKM_WIA_ID))
730                 return 0;
731
732         return -ENODEV;
733 }
734
735 static void akm_work(struct work_struct *ws)
736 {
737         struct akm_inf *inf;
738
739         inf = container_of(ws, struct akm_inf, dw.work);
740         akm_read(inf);
741         queue_delayed_work(inf->wq, &inf->dw,
742                            usecs_to_jiffies(inf->poll_delay_us));
743 }
744
745 static int akm_enable(struct akm_inf *inf, bool enable)
746 {
747         int err = 0;
748
749         akm_pm(inf, true);
750         if (!inf->initd)
751                 err = akm_init_hw(inf);
752         if (!err) {
753                 if (enable) {
754                         inf->data_out = AKM_CNTL1_MODE_SINGLE;
755                         err = akm_delay(inf, inf->poll_delay_us);
756                         err |= akm_mode_wr(inf, true, inf->range_index,
757                                           inf->data_out & AKM_CNTL1_MODE_MASK);
758                         if (!err)
759                                 inf->enable = true;
760                 } else {
761                         inf->enable = false;
762                         err = akm_mode_wr(inf, false, inf->range_index,
763                                           AKM_CNTL1_MODE_POWERDOWN);
764                         if (!err)
765                                 akm_pm(inf, false);
766                 }
767         }
768         return err;
769 }
770
771 static ssize_t akm_enable_store(struct device *dev,
772                                 struct device_attribute *attr,
773                                 const char *buf, size_t count)
774 {
775         struct akm_inf *inf;
776         unsigned int enable;
777         bool en;
778         int err;
779
780         inf = dev_get_drvdata(dev);
781         err = kstrtouint(buf, 10, &enable);
782         if (err)
783                 return -EINVAL;
784
785         if (enable)
786                 en = true;
787         else
788                 en = false;
789         dev_dbg(&inf->i2c->dev, "%s: %x", __func__, en);
790         err = akm_enable(inf, en);
791         if (err) {
792                 dev_err(&inf->i2c->dev, "%s: %x ERR=%d", __func__, en, err);
793                 return err;
794         }
795
796         return count;
797 }
798
799 static ssize_t akm_enable_show(struct device *dev,
800                                struct device_attribute *attr, char *buf)
801 {
802         struct akm_inf *inf;
803         unsigned int enable = 0;
804
805         inf = dev_get_drvdata(dev);
806         if (inf->enable)
807                 enable = 1;
808         return sprintf(buf, "%u\n", enable);
809 }
810
811 static ssize_t akm_delay_store(struct device *dev,
812                                struct device_attribute *attr,
813                                const char *buf, size_t count)
814 {
815         struct akm_inf *inf;
816         unsigned long delay_us;
817         int err;
818
819         inf = dev_get_drvdata(dev);
820         err = kstrtoul(buf, 10, &delay_us);
821         if (err)
822                 return -EINVAL;
823
824         dev_dbg(&inf->i2c->dev, "%s: %lu\n", __func__, delay_us);
825         if (delay_us < (AKM_INPUT_DELAY_MS_MIN * 1000))
826                 delay_us = (AKM_INPUT_DELAY_MS_MIN * 1000);
827         if ((inf->enable) && (delay_us != inf->poll_delay_us))
828                 err = akm_delay(inf, delay_us);
829         if (!err) {
830                 inf->poll_delay_us = delay_us;
831         } else {
832                 dev_err(&inf->i2c->dev, "%s: %lu ERR=%d\n",
833                         __func__, delay_us, err);
834                 return err;
835         }
836
837         return count;
838 }
839
840 static ssize_t akm_delay_show(struct device *dev,
841                               struct device_attribute *attr, char *buf)
842 {
843         struct akm_inf *inf;
844
845         inf = dev_get_drvdata(dev);
846         if (inf->enable)
847                 return sprintf(buf, "%lu\n", inf->poll_delay_us);
848
849         return sprintf(buf, "%u\n", (AKM_INPUT_DELAY_MS_MIN * 1000));
850 }
851
852 static ssize_t akm_resolution_store(struct device *dev,
853                                     struct device_attribute *attr,
854                                     const char *buf, size_t count)
855 {
856         struct akm_inf *inf;
857         unsigned int resolution;
858
859         inf = dev_get_drvdata(dev);
860         if (kstrtouint(buf, 10, &resolution))
861                 return -EINVAL;
862
863         dev_dbg(&inf->i2c->dev, "%s %u", __func__, resolution);
864         inf->resolution = resolution;
865         return count;
866 }
867
868 static ssize_t akm_resolution_show(struct device *dev,
869                                    struct device_attribute *attr, char *buf)
870 {
871         struct akm_inf *inf;
872         unsigned int resolution;
873
874         inf = dev_get_drvdata(dev);
875         if (inf->enable)
876                 resolution = inf->resolution;
877         else
878                 resolution = AKM_INPUT_RESOLUTION;
879         return sprintf(buf, "%u\n", resolution);
880 }
881
882 static ssize_t akm_max_range_store(struct device *dev,
883                                    struct device_attribute *attr,
884                                    const char *buf, size_t count)
885 {
886         struct akm_inf *inf;
887         u8 range_index;
888         int err;
889
890         inf = dev_get_drvdata(dev);
891         err = kstrtou8(buf, 10, &range_index);
892         if (err)
893                 return -EINVAL;
894
895         dev_dbg(&inf->i2c->dev, "%s %u", __func__, range_index);
896         if (inf->compass_id == COMPASS_ID_AK8963) {
897                 if (range_index > 1)
898                         return -EINVAL;
899
900                 if (inf->enable) {
901                         err = akm_mode_wr(inf, false, range_index,
902                                           inf->data_out & AKM_CNTL1_MODE_MASK);
903                         if (err)
904                                 return err;
905                 } else {
906                         inf->range_index = range_index;
907                 }
908         } else {
909                 return -EINVAL;
910         }
911
912         return count;
913 }
914
915 static ssize_t akm_max_range_show(struct device *dev,
916                                   struct device_attribute *attr, char *buf)
917 {
918         struct akm_inf *inf;
919         unsigned int range;
920
921         inf = dev_get_drvdata(dev);
922         if (inf->enable) {
923                 range = inf->range_index;
924         } else {
925                 if (inf->compass_id == COMPASS_ID_AK8963) {
926                         if (inf->range_index)
927                                 range = AKM8963_SCALE16;
928                         else
929                                 range = AKM8963_SCALE14;
930                 } else if (inf->compass_id == COMPASS_ID_AK8972) {
931                         range = AKM8972_SCALE;
932                 } else {
933                         range = AKM8975_SCALE;
934                 }
935         }
936         return sprintf(buf, "%u\n", range);
937 }
938
939 static ssize_t akm_cycle_delay_us_store(struct device *dev,
940                                         struct device_attribute *attr,
941                                         const char *buf, size_t count)
942 {
943         struct akm_inf *inf;
944         unsigned long cycle_delay_us;
945         int err;
946
947         inf = dev_get_drvdata(dev);
948         err = kstrtoul(buf, 10, &cycle_delay_us);
949         if (err)
950                 return -EINVAL;
951
952         dev_dbg(&inf->i2c->dev, "%s %lu", __func__, cycle_delay_us);
953         inf->cycle_delay_us = cycle_delay_us;
954         return count;
955 }
956
957 static ssize_t akm_cycle_delay_us_show(struct device *dev,
958                                        struct device_attribute *attr,
959                                        char *buf)
960 {
961         struct akm_inf *inf = dev_get_drvdata(dev);
962
963         return sprintf(buf, "%lu\n", inf->cycle_delay_us);
964 }
965
966 static ssize_t akm_divisor_show(struct device *dev,
967                                 struct device_attribute *attr, char *buf)
968 {
969         struct akm_inf *inf;
970
971         inf = dev_get_drvdata(dev);
972         return sprintf(buf, "%d\n", AKM_INPUT_DIVISOR);
973 }
974
975 static ssize_t akm_microamp_show(struct device *dev,
976                                  struct device_attribute *attr, char *buf)
977 {
978         struct akm_inf *inf;
979
980         inf = dev_get_drvdata(dev);
981         return sprintf(buf, "%d\n", AKM_INPUT_POWER_UA);
982 }
983
984 static ssize_t akm_magnetic_field_show(struct device *dev,
985                                        struct device_attribute *attr,
986                                        char *buf)
987 {
988         struct akm_inf *inf;
989         short x;
990         short y;
991         short z;
992
993         inf = dev_get_drvdata(dev);
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                 return sprintf(buf, "%hd, %hd, %hd\n", x, y, z);
1001         }
1002
1003         return -EPERM;
1004 }
1005
1006 static ssize_t akm_orientation_show(struct device *dev,
1007                                     struct device_attribute *attr,
1008                                     char *buf)
1009 {
1010         struct akm_inf *inf;
1011         signed char *m;
1012
1013         inf = dev_get_drvdata(dev);
1014         m = inf->pdata.orientation;
1015         return sprintf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
1016                        m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
1017 }
1018
1019 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWOTH,
1020                    akm_enable_show, akm_enable_store);
1021 static DEVICE_ATTR(delay, S_IRUGO | S_IWUSR | S_IWOTH,
1022                    akm_delay_show, akm_delay_store);
1023 static DEVICE_ATTR(resolution, S_IRUGO | S_IWUSR | S_IWOTH,
1024                    akm_resolution_show, akm_resolution_store);
1025 static DEVICE_ATTR(max_range, S_IRUGO | S_IWUSR | S_IWOTH,
1026                    akm_max_range_show, akm_max_range_store);
1027 static DEVICE_ATTR(cycle_delay_us, S_IRUGO | S_IWUSR | S_IWOTH,
1028                    akm_cycle_delay_us_show, akm_cycle_delay_us_store);
1029 static DEVICE_ATTR(divisor, S_IRUGO,
1030                    akm_divisor_show, NULL);
1031 static DEVICE_ATTR(microamp, S_IRUGO,
1032                    akm_microamp_show, NULL);
1033 static DEVICE_ATTR(magnetic_field, S_IRUGO,
1034                    akm_magnetic_field_show, NULL);
1035 static DEVICE_ATTR(orientation, S_IRUGO,
1036                    akm_orientation_show, NULL);
1037
1038 static struct attribute *akm_attrs[] = {
1039         &dev_attr_enable.attr,
1040         &dev_attr_delay.attr,
1041         &dev_attr_resolution.attr,
1042         &dev_attr_max_range.attr,
1043         &dev_attr_divisor.attr,
1044         &dev_attr_microamp.attr,
1045         &dev_attr_magnetic_field.attr,
1046         &dev_attr_orientation.attr,
1047         &dev_attr_cycle_delay_us.attr,
1048         NULL
1049 };
1050
1051 static struct attribute_group akm_attr_group = {
1052         .name = AKM_NAME,
1053         .attrs = akm_attrs
1054 };
1055
1056 static int akm_sysfs_create(struct akm_inf *inf)
1057 {
1058         int err;
1059
1060         err = sysfs_create_group(&inf->idev->dev.kobj, &akm_attr_group);
1061         return err;
1062 }
1063
1064 static void akm_input_close(struct input_dev *idev)
1065 {
1066         struct akm_inf *inf;
1067
1068         inf = input_get_drvdata(idev);
1069         if (inf != NULL)
1070                 akm_enable(inf, false);
1071 }
1072
1073 static int akm_input_create(struct akm_inf *inf)
1074 {
1075         int err;
1076
1077         inf->idev = input_allocate_device();
1078         if (!inf->idev) {
1079                 err = -ENOMEM;
1080                 dev_err(&inf->i2c->dev, "%s ERR %d\n", __func__, err);
1081                 return err;
1082         }
1083
1084         inf->idev->name = AKM_NAME;
1085         inf->idev->dev.parent = &inf->i2c->dev;
1086         inf->idev->close = akm_input_close;
1087         input_set_drvdata(inf->idev, inf);
1088         input_set_capability(inf->idev, EV_REL, REL_X);
1089         input_set_capability(inf->idev, EV_REL, REL_Y);
1090         input_set_capability(inf->idev, EV_REL, REL_Z);
1091         input_set_capability(inf->idev, EV_REL, REL_MISC);
1092         input_set_capability(inf->idev, EV_REL, REL_WHEEL);
1093         err = input_register_device(inf->idev);
1094         if (err) {
1095                 input_free_device(inf->idev);
1096                 inf->idev = NULL;
1097         }
1098         return err;
1099 }
1100
1101 static int akm_remove(struct i2c_client *client)
1102 {
1103         struct akm_inf *inf;
1104
1105         inf = i2c_get_clientdata(client);
1106         if (inf != NULL) {
1107                 if (inf->idev) {
1108                         input_unregister_device(inf->idev);
1109                         input_free_device(inf->idev);
1110                 }
1111                 if (inf->wq)
1112                         destroy_workqueue(inf->wq);
1113                 akm_pm_exit(inf);
1114                 if (&inf->mutex_data)
1115                         mutex_destroy(&inf->mutex_data);
1116         }
1117         dev_info(&client->dev, "%s\n", __func__);
1118         return 0;
1119 }
1120
1121 static void akm_shutdown(struct i2c_client *client)
1122 {
1123         akm_remove(client);
1124 }
1125
1126 static struct mpu_platform_data *akm_parse_dt(struct i2c_client *client)
1127 {
1128         struct mpu_platform_data *pdata;
1129         struct device_node *np = client->dev.of_node;
1130         char *pchar;
1131         u8 config;
1132         int len;
1133
1134         pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
1135         if (!pdata) {
1136                 dev_err(&client->dev, "Can't allocate platform data\n");
1137                 return ERR_PTR(-ENOMEM);
1138         }
1139
1140         pchar = of_get_property(np, "orientation", &len);
1141         if (!pchar || len != sizeof(pdata->orientation)) {
1142                 dev_err(&client->dev, "Cannot read orientation property\n");
1143                 return ERR_PTR(-EINVAL);
1144         }
1145         memcpy(pdata->orientation, pchar, len);
1146
1147         if (of_property_read_string(np, "config", &pchar)) {
1148                 dev_err(&client->dev, "Cannot read config property\n");
1149                 return ERR_PTR(-EINVAL);
1150         }
1151
1152         for (config = 0; config < ARRAY_SIZE(akm_configs); config++) {
1153                 if (!strcasecmp(pchar, akm_configs[config])) {
1154                         pdata->config = config;
1155                         break;
1156                 }
1157         }
1158
1159         if (config == ARRAY_SIZE(akm_configs)) {
1160                 dev_err(&client->dev, "Invalid config value\n");
1161                 return ERR_PTR(-EINVAL);
1162         }
1163
1164         return pdata;
1165 }
1166
1167 static int akm_probe(struct i2c_client *client,
1168                      const struct i2c_device_id *devid)
1169 {
1170         struct akm_inf *inf;
1171         struct mpu_platform_data *pd;
1172         int err;
1173
1174         dev_info(&client->dev, "%s\n", __func__);
1175         inf = devm_kzalloc(&client->dev, sizeof(*inf), GFP_KERNEL);
1176         if (!inf) {
1177                 dev_err(&client->dev, "%s kzalloc ERR\n", __func__);
1178                 return -ENOMEM;
1179         }
1180
1181         inf->i2c = client;
1182         i2c_set_clientdata(client, inf);
1183
1184         if (client->dev.of_node)
1185                 pd = akm_parse_dt(client);
1186         else
1187                 pd = (struct mpu_platform_data *)dev_get_platdata(&client->dev);
1188
1189         if (!pd || IS_ERR(pd))
1190                 return -EINVAL;
1191
1192         inf->pdata = *pd;
1193
1194         akm_pm_init(inf);
1195         err = akm_id(inf);
1196         akm_pm(inf, false);
1197         if (err == -EAGAIN)
1198                 goto akm_probe_again;
1199         else if (err)
1200                 goto akm_probe_err;
1201
1202         mutex_init(&inf->mutex_data);
1203         err = akm_input_create(inf);
1204         if (err)
1205                 goto akm_probe_err;
1206
1207         inf->wq = create_singlethread_workqueue(AKM_NAME);
1208         if (!inf->wq) {
1209                 dev_err(&client->dev, "%s workqueue ERR\n", __func__);
1210                 err = -ENOMEM;
1211                 goto akm_probe_err;
1212         }
1213
1214         INIT_DELAYED_WORK(&inf->dw, akm_work);
1215         err = akm_sysfs_create(inf);
1216         if (err)
1217                 goto akm_probe_err;
1218
1219         return 0;
1220
1221 akm_probe_err:
1222         dev_err(&client->dev, "%s ERR %d\n", __func__, err);
1223 akm_probe_again:
1224         akm_remove(client);
1225         return err;
1226 }
1227
1228 static const struct i2c_device_id akm_i2c_device_id[] = {
1229         {AKM_NAME, 0},
1230         {"ak8963", 0},
1231         {"ak8972", 0},
1232         {"ak8975", 0},
1233         {}
1234 };
1235
1236 MODULE_DEVICE_TABLE(i2c, akm_i2c_device_id);
1237
1238 static const struct of_device_id akm_of_match[] = {
1239         { .compatible = "ak,ak8963", },
1240         { .compatible = "ak,ak8972", },
1241         { .compatible = "ak,ak8975", },
1242         { },
1243 };
1244
1245 MODULE_DEVICE_TABLE(of, akm_of_match);
1246
1247 static struct i2c_driver akm_driver = {
1248         .class          = I2C_CLASS_HWMON,
1249         .probe          = akm_probe,
1250         .remove         = akm_remove,
1251         .driver = {
1252                 .name   = AKM_NAME,
1253                 .owner  = THIS_MODULE,
1254                 .of_match_table = of_match_ptr(akm_of_match),
1255         },
1256         .id_table       = akm_i2c_device_id,
1257         .shutdown       = akm_shutdown,
1258 };
1259
1260 static int __init akm_init(void)
1261 {
1262         return i2c_add_driver(&akm_driver);
1263 }
1264
1265 static void __exit akm_exit(void)
1266 {
1267         i2c_del_driver(&akm_driver);
1268 }
1269
1270 module_init(akm_init);
1271 module_exit(akm_exit);
1272
1273 MODULE_LICENSE("GPL");
1274 MODULE_DESCRIPTION("AKM driver");
1275