GNU Linux-libre 4.9.333-gnu1
[releases.git] / drivers / iio / magnetometer / ak8974.c
1 /*
2  * Driver for the Asahi Kasei EMD Corporation AK8974
3  * and Aichi Steel AMI305 magnetometer chips.
4  * Based on a patch from Samu Onkalo and the AK8975 IIO driver.
5  *
6  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7  * Copyright (c) 2010 NVIDIA Corporation.
8  * Copyright (C) 2016 Linaro Ltd.
9  *
10  * Author: Samu Onkalo <samu.p.onkalo@nokia.com>
11  * Author: Linus Walleij <linus.walleij@linaro.org>
12  */
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h> /* For irq_get_irq_data() */
18 #include <linux/completion.h>
19 #include <linux/err.h>
20 #include <linux/mutex.h>
21 #include <linux/delay.h>
22 #include <linux/bitops.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/pm_runtime.h>
26
27 #include <linux/iio/iio.h>
28 #include <linux/iio/sysfs.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/trigger.h>
31 #include <linux/iio/trigger_consumer.h>
32 #include <linux/iio/triggered_buffer.h>
33
34 /*
35  * 16-bit registers are little-endian. LSB is at the address defined below
36  * and MSB is at the next higher address.
37  */
38
39 /* These registers are common for AK8974 and AMI305 */
40 #define AK8974_SELFTEST         0x0C
41 #define AK8974_SELFTEST_IDLE    0x55
42 #define AK8974_SELFTEST_OK      0xAA
43
44 #define AK8974_INFO             0x0D
45
46 #define AK8974_WHOAMI           0x0F
47 #define AK8974_WHOAMI_VALUE_AMI305 0x47
48 #define AK8974_WHOAMI_VALUE_AK8974 0x48
49
50 #define AK8974_DATA_X           0x10
51 #define AK8974_DATA_Y           0x12
52 #define AK8974_DATA_Z           0x14
53 #define AK8974_INT_SRC          0x16
54 #define AK8974_STATUS           0x18
55 #define AK8974_INT_CLEAR        0x1A
56 #define AK8974_CTRL1            0x1B
57 #define AK8974_CTRL2            0x1C
58 #define AK8974_CTRL3            0x1D
59 #define AK8974_INT_CTRL         0x1E
60 #define AK8974_INT_THRES        0x26  /* Absolute any axis value threshold */
61 #define AK8974_PRESET           0x30
62
63 /* AK8974-specific offsets */
64 #define AK8974_OFFSET_X         0x20
65 #define AK8974_OFFSET_Y         0x22
66 #define AK8974_OFFSET_Z         0x24
67 /* AMI305-specific offsets */
68 #define AMI305_OFFSET_X         0x6C
69 #define AMI305_OFFSET_Y         0x72
70 #define AMI305_OFFSET_Z         0x78
71
72 /* Different temperature registers */
73 #define AK8974_TEMP             0x31
74 #define AMI305_TEMP             0x60
75
76 #define AK8974_INT_X_HIGH       BIT(7) /* Axis over +threshold  */
77 #define AK8974_INT_Y_HIGH       BIT(6)
78 #define AK8974_INT_Z_HIGH       BIT(5)
79 #define AK8974_INT_X_LOW        BIT(4) /* Axis below -threshold */
80 #define AK8974_INT_Y_LOW        BIT(3)
81 #define AK8974_INT_Z_LOW        BIT(2)
82 #define AK8974_INT_RANGE        BIT(1) /* Range overflow (any axis) */
83
84 #define AK8974_STATUS_DRDY      BIT(6) /* Data ready */
85 #define AK8974_STATUS_OVERRUN   BIT(5) /* Data overrun */
86 #define AK8974_STATUS_INT       BIT(4) /* Interrupt occurred */
87
88 #define AK8974_CTRL1_POWER      BIT(7) /* 0 = standby; 1 = active */
89 #define AK8974_CTRL1_RATE       BIT(4) /* 0 = 10 Hz; 1 = 20 Hz   */
90 #define AK8974_CTRL1_FORCE_EN   BIT(1) /* 0 = normal; 1 = force  */
91 #define AK8974_CTRL1_MODE2      BIT(0) /* 0 */
92
93 #define AK8974_CTRL2_INT_EN     BIT(4)  /* 1 = enable interrupts              */
94 #define AK8974_CTRL2_DRDY_EN    BIT(3)  /* 1 = enable data ready signal */
95 #define AK8974_CTRL2_DRDY_POL   BIT(2)  /* 1 = data ready active high   */
96 #define AK8974_CTRL2_RESDEF     (AK8974_CTRL2_DRDY_POL)
97
98 #define AK8974_CTRL3_RESET      BIT(7) /* Software reset                  */
99 #define AK8974_CTRL3_FORCE      BIT(6) /* Start forced measurement */
100 #define AK8974_CTRL3_SELFTEST   BIT(4) /* Set selftest register   */
101 #define AK8974_CTRL3_RESDEF     0x00
102
103 #define AK8974_INT_CTRL_XEN     BIT(7) /* Enable interrupt for this axis */
104 #define AK8974_INT_CTRL_YEN     BIT(6)
105 #define AK8974_INT_CTRL_ZEN     BIT(5)
106 #define AK8974_INT_CTRL_XYZEN   (BIT(7)|BIT(6)|BIT(5))
107 #define AK8974_INT_CTRL_POL     BIT(3) /* 0 = active low; 1 = active high */
108 #define AK8974_INT_CTRL_PULSE   BIT(1) /* 0 = latched; 1 = pulse (50 usec) */
109 #define AK8974_INT_CTRL_RESDEF  (AK8974_INT_CTRL_XYZEN | AK8974_INT_CTRL_POL)
110
111 /* The AMI305 has elaborate FW version and serial number registers */
112 #define AMI305_VER              0xE8
113 #define AMI305_SN               0xEA
114
115 #define AK8974_MAX_RANGE        2048
116
117 #define AK8974_POWERON_DELAY    50
118 #define AK8974_ACTIVATE_DELAY   1
119 #define AK8974_SELFTEST_DELAY   1
120 /*
121  * Set the autosuspend to two orders of magnitude larger than the poweron
122  * delay to make sane reasonable power tradeoff savings (5 seconds in
123  * this case).
124  */
125 #define AK8974_AUTOSUSPEND_DELAY 5000
126
127 #define AK8974_MEASTIME         3
128
129 #define AK8974_PWR_ON           1
130 #define AK8974_PWR_OFF          0
131
132 /**
133  * struct ak8974 - state container for the AK8974 driver
134  * @i2c: parent I2C client
135  * @orientation: mounting matrix, flipped axis etc
136  * @map: regmap to access the AK8974 registers over I2C
137  * @regs: the avdd and dvdd power regulators
138  * @name: the name of the part
139  * @variant: the whoami ID value (for selecting code paths)
140  * @lock: locks the magnetometer for exclusive use during a measurement
141  * @drdy_irq: uses the DRDY IRQ line
142  * @drdy_complete: completion for DRDY
143  * @drdy_active_low: the DRDY IRQ is active low
144  */
145 struct ak8974 {
146         struct i2c_client *i2c;
147         struct iio_mount_matrix orientation;
148         struct regmap *map;
149         struct regulator_bulk_data regs[2];
150         const char *name;
151         u8 variant;
152         struct mutex lock;
153         bool drdy_irq;
154         struct completion drdy_complete;
155         bool drdy_active_low;
156         /* Ensure timestamp is naturally aligned */
157         struct {
158                 __le16 channels[3];
159                 s64 ts __aligned(8);
160         } scan;
161 };
162
163 static const char ak8974_reg_avdd[] = "avdd";
164 static const char ak8974_reg_dvdd[] = "dvdd";
165
166 static int ak8974_set_power(struct ak8974 *ak8974, bool mode)
167 {
168         int ret;
169         u8 val;
170
171         val = mode ? AK8974_CTRL1_POWER : 0;
172         val |= AK8974_CTRL1_FORCE_EN;
173         ret = regmap_write(ak8974->map, AK8974_CTRL1, val);
174         if (ret < 0)
175                 return ret;
176
177         if (mode)
178                 msleep(AK8974_ACTIVATE_DELAY);
179
180         return 0;
181 }
182
183 static int ak8974_reset(struct ak8974 *ak8974)
184 {
185         int ret;
186
187         /* Power on to get register access. Sets CTRL1 reg to reset state */
188         ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
189         if (ret)
190                 return ret;
191         ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_RESDEF);
192         if (ret)
193                 return ret;
194         ret = regmap_write(ak8974->map, AK8974_CTRL3, AK8974_CTRL3_RESDEF);
195         if (ret)
196                 return ret;
197         ret = regmap_write(ak8974->map, AK8974_INT_CTRL,
198                            AK8974_INT_CTRL_RESDEF);
199         if (ret)
200                 return ret;
201
202         /* After reset, power off is default state */
203         return ak8974_set_power(ak8974, AK8974_PWR_OFF);
204 }
205
206 static int ak8974_configure(struct ak8974 *ak8974)
207 {
208         int ret;
209
210         ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_DRDY_EN |
211                            AK8974_CTRL2_INT_EN);
212         if (ret)
213                 return ret;
214         ret = regmap_write(ak8974->map, AK8974_CTRL3, 0);
215         if (ret)
216                 return ret;
217         ret = regmap_write(ak8974->map, AK8974_INT_CTRL, AK8974_INT_CTRL_POL);
218         if (ret)
219                 return ret;
220
221         return regmap_write(ak8974->map, AK8974_PRESET, 0);
222 }
223
224 static int ak8974_trigmeas(struct ak8974 *ak8974)
225 {
226         unsigned int clear;
227         u8 mask;
228         u8 val;
229         int ret;
230
231         /* Clear any previous measurement overflow status */
232         ret = regmap_read(ak8974->map, AK8974_INT_CLEAR, &clear);
233         if (ret)
234                 return ret;
235
236         /* If we have a DRDY IRQ line, use it */
237         if (ak8974->drdy_irq) {
238                 mask = AK8974_CTRL2_INT_EN |
239                         AK8974_CTRL2_DRDY_EN |
240                         AK8974_CTRL2_DRDY_POL;
241                 val = AK8974_CTRL2_DRDY_EN;
242
243                 if (!ak8974->drdy_active_low)
244                         val |= AK8974_CTRL2_DRDY_POL;
245
246                 init_completion(&ak8974->drdy_complete);
247                 ret = regmap_update_bits(ak8974->map, AK8974_CTRL2,
248                                          mask, val);
249                 if (ret)
250                         return ret;
251         }
252
253         /* Force a measurement */
254         return regmap_update_bits(ak8974->map,
255                                   AK8974_CTRL3,
256                                   AK8974_CTRL3_FORCE,
257                                   AK8974_CTRL3_FORCE);
258 }
259
260 static int ak8974_await_drdy(struct ak8974 *ak8974)
261 {
262         int timeout = 2;
263         unsigned int val;
264         int ret;
265
266         if (ak8974->drdy_irq) {
267                 ret = wait_for_completion_timeout(&ak8974->drdy_complete,
268                                         1 + msecs_to_jiffies(1000));
269                 if (!ret) {
270                         dev_err(&ak8974->i2c->dev,
271                                 "timeout waiting for DRDY IRQ\n");
272                         return -ETIMEDOUT;
273                 }
274                 return 0;
275         }
276
277         /* Default delay-based poll loop */
278         do {
279                 msleep(AK8974_MEASTIME);
280                 ret = regmap_read(ak8974->map, AK8974_STATUS, &val);
281                 if (ret < 0)
282                         return ret;
283                 if (val & AK8974_STATUS_DRDY)
284                         return 0;
285         } while (--timeout);
286         if (!timeout) {
287                 dev_err(&ak8974->i2c->dev,
288                         "timeout waiting for DRDY\n");
289                 return -ETIMEDOUT;
290         }
291
292         return 0;
293 }
294
295 static int ak8974_getresult(struct ak8974 *ak8974, s16 *result)
296 {
297         unsigned int src;
298         int ret;
299
300         ret = ak8974_await_drdy(ak8974);
301         if (ret)
302                 return ret;
303         ret = regmap_read(ak8974->map, AK8974_INT_SRC, &src);
304         if (ret < 0)
305                 return ret;
306
307         /* Out of range overflow! Strong magnet close? */
308         if (src & AK8974_INT_RANGE) {
309                 dev_err(&ak8974->i2c->dev,
310                         "range overflow in sensor\n");
311                 return -ERANGE;
312         }
313
314         ret = regmap_bulk_read(ak8974->map, AK8974_DATA_X, result, 6);
315         if (ret)
316                 return ret;
317
318         return ret;
319 }
320
321 static irqreturn_t ak8974_drdy_irq(int irq, void *d)
322 {
323         struct ak8974 *ak8974 = d;
324
325         if (!ak8974->drdy_irq)
326                 return IRQ_NONE;
327
328         /* TODO: timestamp here to get good measurement stamps */
329         return IRQ_WAKE_THREAD;
330 }
331
332 static irqreturn_t ak8974_drdy_irq_thread(int irq, void *d)
333 {
334         struct ak8974 *ak8974 = d;
335         unsigned int val;
336         int ret;
337
338         /* Check if this was a DRDY from us */
339         ret = regmap_read(ak8974->map, AK8974_STATUS, &val);
340         if (ret < 0) {
341                 dev_err(&ak8974->i2c->dev, "error reading DRDY status\n");
342                 return IRQ_HANDLED;
343         }
344         if (val & AK8974_STATUS_DRDY) {
345                 /* Yes this was our IRQ */
346                 complete(&ak8974->drdy_complete);
347                 return IRQ_HANDLED;
348         }
349
350         /* We may be on a shared IRQ, let the next client check */
351         return IRQ_NONE;
352 }
353
354 static int ak8974_selftest(struct ak8974 *ak8974)
355 {
356         struct device *dev = &ak8974->i2c->dev;
357         unsigned int val;
358         int ret;
359
360         ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
361         if (ret)
362                 return ret;
363         if (val != AK8974_SELFTEST_IDLE) {
364                 dev_err(dev, "selftest not idle before test\n");
365                 return -EIO;
366         }
367
368         /* Trigger self-test */
369         ret = regmap_update_bits(ak8974->map,
370                         AK8974_CTRL3,
371                         AK8974_CTRL3_SELFTEST,
372                         AK8974_CTRL3_SELFTEST);
373         if (ret) {
374                 dev_err(dev, "could not write CTRL3\n");
375                 return ret;
376         }
377
378         msleep(AK8974_SELFTEST_DELAY);
379
380         ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
381         if (ret)
382                 return ret;
383         if (val != AK8974_SELFTEST_OK) {
384                 dev_err(dev, "selftest result NOT OK (%02x)\n", val);
385                 return -EIO;
386         }
387
388         ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
389         if (ret)
390                 return ret;
391         if (val != AK8974_SELFTEST_IDLE) {
392                 dev_err(dev, "selftest not idle after test (%02x)\n", val);
393                 return -EIO;
394         }
395         dev_dbg(dev, "passed self-test\n");
396
397         return 0;
398 }
399
400 static int ak8974_get_u16_val(struct ak8974 *ak8974, u8 reg, u16 *val)
401 {
402         int ret;
403         u16 bulk;
404
405         ret = regmap_bulk_read(ak8974->map, reg, &bulk, 2);
406         if (ret)
407                 return ret;
408         *val = le16_to_cpu(bulk);
409
410         return 0;
411 }
412
413 static int ak8974_detect(struct ak8974 *ak8974)
414 {
415         unsigned int whoami;
416         const char *name;
417         int ret;
418         unsigned int fw;
419         u16 sn;
420
421         ret = regmap_read(ak8974->map, AK8974_WHOAMI, &whoami);
422         if (ret)
423                 return ret;
424
425         switch (whoami) {
426         case AK8974_WHOAMI_VALUE_AMI305:
427                 name = "ami305";
428                 ret = regmap_read(ak8974->map, AMI305_VER, &fw);
429                 if (ret)
430                         return ret;
431                 fw &= 0x7f; /* only bits 0 thru 6 valid */
432                 ret = ak8974_get_u16_val(ak8974, AMI305_SN, &sn);
433                 if (ret)
434                         return ret;
435                 dev_info(&ak8974->i2c->dev,
436                          "detected %s, FW ver %02x, S/N: %04x\n",
437                          name, fw, sn);
438                 break;
439         case AK8974_WHOAMI_VALUE_AK8974:
440                 name = "ak8974";
441                 dev_info(&ak8974->i2c->dev, "detected AK8974\n");
442                 break;
443         default:
444                 dev_err(&ak8974->i2c->dev, "unsupported device (%02x) ",
445                         whoami);
446                 return -ENODEV;
447         }
448
449         ak8974->name = name;
450         ak8974->variant = whoami;
451
452         return 0;
453 }
454
455 static int ak8974_read_raw(struct iio_dev *indio_dev,
456                            struct iio_chan_spec const *chan,
457                            int *val, int *val2,
458                            long mask)
459 {
460         struct ak8974 *ak8974 = iio_priv(indio_dev);
461         s16 hw_values[3];
462         int ret = -EINVAL;
463
464         pm_runtime_get_sync(&ak8974->i2c->dev);
465         mutex_lock(&ak8974->lock);
466
467         switch (mask) {
468         case IIO_CHAN_INFO_RAW:
469                 if (chan->address > 2) {
470                         dev_err(&ak8974->i2c->dev, "faulty channel address\n");
471                         ret = -EIO;
472                         goto out_unlock;
473                 }
474                 ret = ak8974_trigmeas(ak8974);
475                 if (ret)
476                         goto out_unlock;
477                 ret = ak8974_getresult(ak8974, hw_values);
478                 if (ret)
479                         goto out_unlock;
480
481                 /*
482                  * We read all axes and discard all but one, for optimized
483                  * reading, use the triggered buffer.
484                  */
485                 *val = (s16)le16_to_cpu(hw_values[chan->address]);
486
487                 ret = IIO_VAL_INT;
488         }
489
490  out_unlock:
491         mutex_unlock(&ak8974->lock);
492         pm_runtime_mark_last_busy(&ak8974->i2c->dev);
493         pm_runtime_put_autosuspend(&ak8974->i2c->dev);
494
495         return ret;
496 }
497
498 static void ak8974_fill_buffer(struct iio_dev *indio_dev)
499 {
500         struct ak8974 *ak8974 = iio_priv(indio_dev);
501         int ret;
502
503         pm_runtime_get_sync(&ak8974->i2c->dev);
504         mutex_lock(&ak8974->lock);
505
506         ret = ak8974_trigmeas(ak8974);
507         if (ret) {
508                 dev_err(&ak8974->i2c->dev, "error triggering measure\n");
509                 goto out_unlock;
510         }
511         ret = ak8974_getresult(ak8974, ak8974->scan.channels);
512         if (ret) {
513                 dev_err(&ak8974->i2c->dev, "error getting measures\n");
514                 goto out_unlock;
515         }
516
517         iio_push_to_buffers_with_timestamp(indio_dev, &ak8974->scan,
518                                            iio_get_time_ns(indio_dev));
519
520  out_unlock:
521         mutex_unlock(&ak8974->lock);
522         pm_runtime_mark_last_busy(&ak8974->i2c->dev);
523         pm_runtime_put_autosuspend(&ak8974->i2c->dev);
524 }
525
526 static irqreturn_t ak8974_handle_trigger(int irq, void *p)
527 {
528         const struct iio_poll_func *pf = p;
529         struct iio_dev *indio_dev = pf->indio_dev;
530
531         ak8974_fill_buffer(indio_dev);
532         iio_trigger_notify_done(indio_dev->trig);
533
534         return IRQ_HANDLED;
535 }
536
537 static const struct iio_mount_matrix *
538 ak8974_get_mount_matrix(const struct iio_dev *indio_dev,
539                         const struct iio_chan_spec *chan)
540 {
541         struct ak8974 *ak8974 = iio_priv(indio_dev);
542
543         return &ak8974->orientation;
544 }
545
546 static const struct iio_chan_spec_ext_info ak8974_ext_info[] = {
547         IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8974_get_mount_matrix),
548         { },
549 };
550
551 #define AK8974_AXIS_CHANNEL(axis, index)                                \
552         {                                                               \
553                 .type = IIO_MAGN,                                       \
554                 .modified = 1,                                          \
555                 .channel2 = IIO_MOD_##axis,                             \
556                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
557                 .ext_info = ak8974_ext_info,                            \
558                 .address = index,                                       \
559                 .scan_index = index,                                    \
560                 .scan_type = {                                          \
561                         .sign = 's',                                    \
562                         .realbits = 16,                                 \
563                         .storagebits = 16,                              \
564                         .endianness = IIO_LE                            \
565                 },                                                      \
566         }
567
568 static const struct iio_chan_spec ak8974_channels[] = {
569         AK8974_AXIS_CHANNEL(X, 0),
570         AK8974_AXIS_CHANNEL(Y, 1),
571         AK8974_AXIS_CHANNEL(Z, 2),
572         IIO_CHAN_SOFT_TIMESTAMP(3),
573 };
574
575 static const unsigned long ak8974_scan_masks[] = { 0x7, 0 };
576
577 static const struct iio_info ak8974_info = {
578         .read_raw = &ak8974_read_raw,
579         .driver_module = THIS_MODULE,
580 };
581
582 static bool ak8974_writeable_reg(struct device *dev, unsigned int reg)
583 {
584         struct i2c_client *i2c = to_i2c_client(dev);
585         struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
586         struct ak8974 *ak8974 = iio_priv(indio_dev);
587
588         switch (reg) {
589         case AK8974_CTRL1:
590         case AK8974_CTRL2:
591         case AK8974_CTRL3:
592         case AK8974_INT_CTRL:
593         case AK8974_INT_THRES:
594         case AK8974_INT_THRES + 1:
595         case AK8974_PRESET:
596         case AK8974_PRESET + 1:
597                 return true;
598         case AK8974_OFFSET_X:
599         case AK8974_OFFSET_X + 1:
600         case AK8974_OFFSET_Y:
601         case AK8974_OFFSET_Y + 1:
602         case AK8974_OFFSET_Z:
603         case AK8974_OFFSET_Z + 1:
604                 if (ak8974->variant == AK8974_WHOAMI_VALUE_AK8974)
605                         return true;
606                 return false;
607         case AMI305_OFFSET_X:
608         case AMI305_OFFSET_X + 1:
609         case AMI305_OFFSET_Y:
610         case AMI305_OFFSET_Y + 1:
611         case AMI305_OFFSET_Z:
612         case AMI305_OFFSET_Z + 1:
613                 if (ak8974->variant == AK8974_WHOAMI_VALUE_AMI305)
614                         return true;
615                 return false;
616         default:
617                 return false;
618         }
619 }
620
621 static const struct regmap_config ak8974_regmap_config = {
622         .reg_bits = 8,
623         .val_bits = 8,
624         .max_register = 0xff,
625         .writeable_reg = ak8974_writeable_reg,
626 };
627
628 static int ak8974_probe(struct i2c_client *i2c,
629                         const struct i2c_device_id *id)
630 {
631         struct iio_dev *indio_dev;
632         struct ak8974 *ak8974;
633         unsigned long irq_trig;
634         int irq = i2c->irq;
635         int ret;
636
637         /* Register with IIO */
638         indio_dev = devm_iio_device_alloc(&i2c->dev, sizeof(*ak8974));
639         if (indio_dev == NULL)
640                 return -ENOMEM;
641
642         ak8974 = iio_priv(indio_dev);
643         i2c_set_clientdata(i2c, indio_dev);
644         ak8974->i2c = i2c;
645         mutex_init(&ak8974->lock);
646
647         ret = of_iio_read_mount_matrix(&i2c->dev,
648                                        "mount-matrix",
649                                        &ak8974->orientation);
650         if (ret)
651                 return ret;
652
653         ak8974->regs[0].supply = ak8974_reg_avdd;
654         ak8974->regs[1].supply = ak8974_reg_dvdd;
655
656         ret = devm_regulator_bulk_get(&i2c->dev,
657                                       ARRAY_SIZE(ak8974->regs),
658                                       ak8974->regs);
659         if (ret < 0) {
660                 dev_err(&i2c->dev, "cannot get regulators\n");
661                 return ret;
662         }
663
664         ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
665         if (ret < 0) {
666                 dev_err(&i2c->dev, "cannot enable regulators\n");
667                 return ret;
668         }
669
670         /* Take runtime PM online */
671         pm_runtime_get_noresume(&i2c->dev);
672         pm_runtime_set_active(&i2c->dev);
673         pm_runtime_enable(&i2c->dev);
674
675         ak8974->map = devm_regmap_init_i2c(i2c, &ak8974_regmap_config);
676         if (IS_ERR(ak8974->map)) {
677                 dev_err(&i2c->dev, "failed to allocate register map\n");
678                 pm_runtime_put_noidle(&i2c->dev);
679                 pm_runtime_disable(&i2c->dev);
680                 return PTR_ERR(ak8974->map);
681         }
682
683         ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
684         if (ret) {
685                 dev_err(&i2c->dev, "could not power on\n");
686                 goto disable_pm;
687         }
688
689         ret = ak8974_detect(ak8974);
690         if (ret) {
691                 dev_err(&i2c->dev, "neither AK8974 nor AMI305 found\n");
692                 goto disable_pm;
693         }
694
695         ret = ak8974_selftest(ak8974);
696         if (ret)
697                 dev_err(&i2c->dev, "selftest failed (continuing anyway)\n");
698
699         ret = ak8974_reset(ak8974);
700         if (ret) {
701                 dev_err(&i2c->dev, "AK8974 reset failed\n");
702                 goto disable_pm;
703         }
704
705         indio_dev->dev.parent = &i2c->dev;
706         indio_dev->channels = ak8974_channels;
707         indio_dev->num_channels = ARRAY_SIZE(ak8974_channels);
708         indio_dev->info = &ak8974_info;
709         indio_dev->available_scan_masks = ak8974_scan_masks;
710         indio_dev->modes = INDIO_DIRECT_MODE;
711         indio_dev->name = ak8974->name;
712
713         ret = iio_triggered_buffer_setup(indio_dev, NULL,
714                                          ak8974_handle_trigger,
715                                          NULL);
716         if (ret) {
717                 dev_err(&i2c->dev, "triggered buffer setup failed\n");
718                 goto disable_pm;
719         }
720
721         /* If we have a valid DRDY IRQ, make use of it */
722         if (irq > 0) {
723                 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
724                 if (irq_trig == IRQF_TRIGGER_RISING) {
725                         dev_info(&i2c->dev, "enable rising edge DRDY IRQ\n");
726                 } else if (irq_trig == IRQF_TRIGGER_FALLING) {
727                         ak8974->drdy_active_low = true;
728                         dev_info(&i2c->dev, "enable falling edge DRDY IRQ\n");
729                 } else {
730                         irq_trig = IRQF_TRIGGER_RISING;
731                 }
732                 irq_trig |= IRQF_ONESHOT;
733                 irq_trig |= IRQF_SHARED;
734
735                 ret = devm_request_threaded_irq(&i2c->dev,
736                                                 irq,
737                                                 ak8974_drdy_irq,
738                                                 ak8974_drdy_irq_thread,
739                                                 irq_trig,
740                                                 ak8974->name,
741                                                 ak8974);
742                 if (ret) {
743                         dev_err(&i2c->dev, "unable to request DRDY IRQ "
744                                 "- proceeding without IRQ\n");
745                         goto no_irq;
746                 }
747                 ak8974->drdy_irq = true;
748         }
749
750 no_irq:
751         ret = iio_device_register(indio_dev);
752         if (ret) {
753                 dev_err(&i2c->dev, "device register failed\n");
754                 goto cleanup_buffer;
755         }
756
757         pm_runtime_set_autosuspend_delay(&i2c->dev,
758                                          AK8974_AUTOSUSPEND_DELAY);
759         pm_runtime_use_autosuspend(&i2c->dev);
760         pm_runtime_put(&i2c->dev);
761
762         return 0;
763
764 cleanup_buffer:
765         iio_triggered_buffer_cleanup(indio_dev);
766 disable_pm:
767         pm_runtime_put_noidle(&i2c->dev);
768         pm_runtime_disable(&i2c->dev);
769         ak8974_set_power(ak8974, AK8974_PWR_OFF);
770         regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
771
772         return ret;
773 }
774
775 static int ak8974_remove(struct i2c_client *i2c)
776 {
777         struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
778         struct ak8974 *ak8974 = iio_priv(indio_dev);
779
780         iio_device_unregister(indio_dev);
781         iio_triggered_buffer_cleanup(indio_dev);
782         pm_runtime_get_sync(&i2c->dev);
783         pm_runtime_put_noidle(&i2c->dev);
784         pm_runtime_disable(&i2c->dev);
785         ak8974_set_power(ak8974, AK8974_PWR_OFF);
786         regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
787
788         return 0;
789 }
790
791 static int __maybe_unused ak8974_runtime_suspend(struct device *dev)
792 {
793         struct ak8974 *ak8974 =
794                 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
795
796         ak8974_set_power(ak8974, AK8974_PWR_OFF);
797         regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
798
799         return 0;
800 }
801
802 static int __maybe_unused ak8974_runtime_resume(struct device *dev)
803 {
804         struct ak8974 *ak8974 =
805                 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
806         int ret;
807
808         ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
809         if (ret)
810                 return ret;
811         msleep(AK8974_POWERON_DELAY);
812         ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
813         if (ret)
814                 goto out_regulator_disable;
815
816         ret = ak8974_configure(ak8974);
817         if (ret)
818                 goto out_disable_power;
819
820         return 0;
821
822 out_disable_power:
823         ak8974_set_power(ak8974, AK8974_PWR_OFF);
824 out_regulator_disable:
825         regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
826
827         return ret;
828 }
829
830 static const struct dev_pm_ops ak8974_dev_pm_ops = {
831         SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
832                                 pm_runtime_force_resume)
833         SET_RUNTIME_PM_OPS(ak8974_runtime_suspend,
834                            ak8974_runtime_resume, NULL)
835 };
836
837 static const struct i2c_device_id ak8974_id[] = {
838         {"ami305", 0 },
839         {"ak8974", 0 },
840         {}
841 };
842 MODULE_DEVICE_TABLE(i2c, ak8974_id);
843
844 static const struct of_device_id ak8974_of_match[] = {
845         { .compatible = "asahi-kasei,ak8974", },
846         {}
847 };
848 MODULE_DEVICE_TABLE(of, ak8974_of_match);
849
850 static struct i2c_driver ak8974_driver = {
851         .driver  = {
852                 .name   = "ak8974",
853                 .pm = &ak8974_dev_pm_ops,
854                 .of_match_table = of_match_ptr(ak8974_of_match),
855         },
856         .probe    = ak8974_probe,
857         .remove   = ak8974_remove,
858         .id_table = ak8974_id,
859 };
860 module_i2c_driver(ak8974_driver);
861
862 MODULE_DESCRIPTION("AK8974 and AMI305 3-axis magnetometer driver");
863 MODULE_AUTHOR("Samu Onkalo");
864 MODULE_AUTHOR("Linus Walleij");
865 MODULE_LICENSE("GPL v2");