Merge remote-tracking branches 'asoc/topic/tegra', 'asoc/topic/tlv320aic23', 'asoc...
[sfrench/cifs-2.6.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 };
157
158 static const char ak8974_reg_avdd[] = "avdd";
159 static const char ak8974_reg_dvdd[] = "dvdd";
160
161 static int ak8974_set_power(struct ak8974 *ak8974, bool mode)
162 {
163         int ret;
164         u8 val;
165
166         val = mode ? AK8974_CTRL1_POWER : 0;
167         val |= AK8974_CTRL1_FORCE_EN;
168         ret = regmap_write(ak8974->map, AK8974_CTRL1, val);
169         if (ret < 0)
170                 return ret;
171
172         if (mode)
173                 msleep(AK8974_ACTIVATE_DELAY);
174
175         return 0;
176 }
177
178 static int ak8974_reset(struct ak8974 *ak8974)
179 {
180         int ret;
181
182         /* Power on to get register access. Sets CTRL1 reg to reset state */
183         ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
184         if (ret)
185                 return ret;
186         ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_RESDEF);
187         if (ret)
188                 return ret;
189         ret = regmap_write(ak8974->map, AK8974_CTRL3, AK8974_CTRL3_RESDEF);
190         if (ret)
191                 return ret;
192         ret = regmap_write(ak8974->map, AK8974_INT_CTRL,
193                            AK8974_INT_CTRL_RESDEF);
194         if (ret)
195                 return ret;
196
197         /* After reset, power off is default state */
198         return ak8974_set_power(ak8974, AK8974_PWR_OFF);
199 }
200
201 static int ak8974_configure(struct ak8974 *ak8974)
202 {
203         int ret;
204
205         ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_DRDY_EN |
206                            AK8974_CTRL2_INT_EN);
207         if (ret)
208                 return ret;
209         ret = regmap_write(ak8974->map, AK8974_CTRL3, 0);
210         if (ret)
211                 return ret;
212         ret = regmap_write(ak8974->map, AK8974_INT_CTRL, AK8974_INT_CTRL_POL);
213         if (ret)
214                 return ret;
215
216         return regmap_write(ak8974->map, AK8974_PRESET, 0);
217 }
218
219 static int ak8974_trigmeas(struct ak8974 *ak8974)
220 {
221         unsigned int clear;
222         u8 mask;
223         u8 val;
224         int ret;
225
226         /* Clear any previous measurement overflow status */
227         ret = regmap_read(ak8974->map, AK8974_INT_CLEAR, &clear);
228         if (ret)
229                 return ret;
230
231         /* If we have a DRDY IRQ line, use it */
232         if (ak8974->drdy_irq) {
233                 mask = AK8974_CTRL2_INT_EN |
234                         AK8974_CTRL2_DRDY_EN |
235                         AK8974_CTRL2_DRDY_POL;
236                 val = AK8974_CTRL2_DRDY_EN;
237
238                 if (!ak8974->drdy_active_low)
239                         val |= AK8974_CTRL2_DRDY_POL;
240
241                 init_completion(&ak8974->drdy_complete);
242                 ret = regmap_update_bits(ak8974->map, AK8974_CTRL2,
243                                          mask, val);
244                 if (ret)
245                         return ret;
246         }
247
248         /* Force a measurement */
249         return regmap_update_bits(ak8974->map,
250                                   AK8974_CTRL3,
251                                   AK8974_CTRL3_FORCE,
252                                   AK8974_CTRL3_FORCE);
253 }
254
255 static int ak8974_await_drdy(struct ak8974 *ak8974)
256 {
257         int timeout = 2;
258         unsigned int val;
259         int ret;
260
261         if (ak8974->drdy_irq) {
262                 ret = wait_for_completion_timeout(&ak8974->drdy_complete,
263                                         1 + msecs_to_jiffies(1000));
264                 if (!ret) {
265                         dev_err(&ak8974->i2c->dev,
266                                 "timeout waiting for DRDY IRQ\n");
267                         return -ETIMEDOUT;
268                 }
269                 return 0;
270         }
271
272         /* Default delay-based poll loop */
273         do {
274                 msleep(AK8974_MEASTIME);
275                 ret = regmap_read(ak8974->map, AK8974_STATUS, &val);
276                 if (ret < 0)
277                         return ret;
278                 if (val & AK8974_STATUS_DRDY)
279                         return 0;
280         } while (--timeout);
281
282         dev_err(&ak8974->i2c->dev, "timeout waiting for DRDY\n");
283         return -ETIMEDOUT;
284 }
285
286 static int ak8974_getresult(struct ak8974 *ak8974, __le16 *result)
287 {
288         unsigned int src;
289         int ret;
290
291         ret = ak8974_await_drdy(ak8974);
292         if (ret)
293                 return ret;
294         ret = regmap_read(ak8974->map, AK8974_INT_SRC, &src);
295         if (ret < 0)
296                 return ret;
297
298         /* Out of range overflow! Strong magnet close? */
299         if (src & AK8974_INT_RANGE) {
300                 dev_err(&ak8974->i2c->dev,
301                         "range overflow in sensor\n");
302                 return -ERANGE;
303         }
304
305         ret = regmap_bulk_read(ak8974->map, AK8974_DATA_X, result, 6);
306         if (ret)
307                 return ret;
308
309         return ret;
310 }
311
312 static irqreturn_t ak8974_drdy_irq(int irq, void *d)
313 {
314         struct ak8974 *ak8974 = d;
315
316         if (!ak8974->drdy_irq)
317                 return IRQ_NONE;
318
319         /* TODO: timestamp here to get good measurement stamps */
320         return IRQ_WAKE_THREAD;
321 }
322
323 static irqreturn_t ak8974_drdy_irq_thread(int irq, void *d)
324 {
325         struct ak8974 *ak8974 = d;
326         unsigned int val;
327         int ret;
328
329         /* Check if this was a DRDY from us */
330         ret = regmap_read(ak8974->map, AK8974_STATUS, &val);
331         if (ret < 0) {
332                 dev_err(&ak8974->i2c->dev, "error reading DRDY status\n");
333                 return IRQ_HANDLED;
334         }
335         if (val & AK8974_STATUS_DRDY) {
336                 /* Yes this was our IRQ */
337                 complete(&ak8974->drdy_complete);
338                 return IRQ_HANDLED;
339         }
340
341         /* We may be on a shared IRQ, let the next client check */
342         return IRQ_NONE;
343 }
344
345 static int ak8974_selftest(struct ak8974 *ak8974)
346 {
347         struct device *dev = &ak8974->i2c->dev;
348         unsigned int val;
349         int ret;
350
351         ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
352         if (ret)
353                 return ret;
354         if (val != AK8974_SELFTEST_IDLE) {
355                 dev_err(dev, "selftest not idle before test\n");
356                 return -EIO;
357         }
358
359         /* Trigger self-test */
360         ret = regmap_update_bits(ak8974->map,
361                         AK8974_CTRL3,
362                         AK8974_CTRL3_SELFTEST,
363                         AK8974_CTRL3_SELFTEST);
364         if (ret) {
365                 dev_err(dev, "could not write CTRL3\n");
366                 return ret;
367         }
368
369         msleep(AK8974_SELFTEST_DELAY);
370
371         ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
372         if (ret)
373                 return ret;
374         if (val != AK8974_SELFTEST_OK) {
375                 dev_err(dev, "selftest result NOT OK (%02x)\n", val);
376                 return -EIO;
377         }
378
379         ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
380         if (ret)
381                 return ret;
382         if (val != AK8974_SELFTEST_IDLE) {
383                 dev_err(dev, "selftest not idle after test (%02x)\n", val);
384                 return -EIO;
385         }
386         dev_dbg(dev, "passed self-test\n");
387
388         return 0;
389 }
390
391 static int ak8974_get_u16_val(struct ak8974 *ak8974, u8 reg, u16 *val)
392 {
393         int ret;
394         __le16 bulk;
395
396         ret = regmap_bulk_read(ak8974->map, reg, &bulk, 2);
397         if (ret)
398                 return ret;
399         *val = le16_to_cpu(bulk);
400
401         return 0;
402 }
403
404 static int ak8974_detect(struct ak8974 *ak8974)
405 {
406         unsigned int whoami;
407         const char *name;
408         int ret;
409         unsigned int fw;
410         u16 sn;
411
412         ret = regmap_read(ak8974->map, AK8974_WHOAMI, &whoami);
413         if (ret)
414                 return ret;
415
416         switch (whoami) {
417         case AK8974_WHOAMI_VALUE_AMI305:
418                 name = "ami305";
419                 ret = regmap_read(ak8974->map, AMI305_VER, &fw);
420                 if (ret)
421                         return ret;
422                 fw &= 0x7f; /* only bits 0 thru 6 valid */
423                 ret = ak8974_get_u16_val(ak8974, AMI305_SN, &sn);
424                 if (ret)
425                         return ret;
426                 dev_info(&ak8974->i2c->dev,
427                          "detected %s, FW ver %02x, S/N: %04x\n",
428                          name, fw, sn);
429                 break;
430         case AK8974_WHOAMI_VALUE_AK8974:
431                 name = "ak8974";
432                 dev_info(&ak8974->i2c->dev, "detected AK8974\n");
433                 break;
434         default:
435                 dev_err(&ak8974->i2c->dev, "unsupported device (%02x) ",
436                         whoami);
437                 return -ENODEV;
438         }
439
440         ak8974->name = name;
441         ak8974->variant = whoami;
442
443         return 0;
444 }
445
446 static int ak8974_read_raw(struct iio_dev *indio_dev,
447                            struct iio_chan_spec const *chan,
448                            int *val, int *val2,
449                            long mask)
450 {
451         struct ak8974 *ak8974 = iio_priv(indio_dev);
452         __le16 hw_values[3];
453         int ret = -EINVAL;
454
455         pm_runtime_get_sync(&ak8974->i2c->dev);
456         mutex_lock(&ak8974->lock);
457
458         switch (mask) {
459         case IIO_CHAN_INFO_RAW:
460                 if (chan->address > 2) {
461                         dev_err(&ak8974->i2c->dev, "faulty channel address\n");
462                         ret = -EIO;
463                         goto out_unlock;
464                 }
465                 ret = ak8974_trigmeas(ak8974);
466                 if (ret)
467                         goto out_unlock;
468                 ret = ak8974_getresult(ak8974, hw_values);
469                 if (ret)
470                         goto out_unlock;
471
472                 /*
473                  * We read all axes and discard all but one, for optimized
474                  * reading, use the triggered buffer.
475                  */
476                 *val = le16_to_cpu(hw_values[chan->address]);
477
478                 ret = IIO_VAL_INT;
479         }
480
481  out_unlock:
482         mutex_unlock(&ak8974->lock);
483         pm_runtime_mark_last_busy(&ak8974->i2c->dev);
484         pm_runtime_put_autosuspend(&ak8974->i2c->dev);
485
486         return ret;
487 }
488
489 static void ak8974_fill_buffer(struct iio_dev *indio_dev)
490 {
491         struct ak8974 *ak8974 = iio_priv(indio_dev);
492         int ret;
493         __le16 hw_values[8]; /* Three axes + 64bit padding */
494
495         pm_runtime_get_sync(&ak8974->i2c->dev);
496         mutex_lock(&ak8974->lock);
497
498         ret = ak8974_trigmeas(ak8974);
499         if (ret) {
500                 dev_err(&ak8974->i2c->dev, "error triggering measure\n");
501                 goto out_unlock;
502         }
503         ret = ak8974_getresult(ak8974, hw_values);
504         if (ret) {
505                 dev_err(&ak8974->i2c->dev, "error getting measures\n");
506                 goto out_unlock;
507         }
508
509         iio_push_to_buffers_with_timestamp(indio_dev, hw_values,
510                                            iio_get_time_ns(indio_dev));
511
512  out_unlock:
513         mutex_unlock(&ak8974->lock);
514         pm_runtime_mark_last_busy(&ak8974->i2c->dev);
515         pm_runtime_put_autosuspend(&ak8974->i2c->dev);
516 }
517
518 static irqreturn_t ak8974_handle_trigger(int irq, void *p)
519 {
520         const struct iio_poll_func *pf = p;
521         struct iio_dev *indio_dev = pf->indio_dev;
522
523         ak8974_fill_buffer(indio_dev);
524         iio_trigger_notify_done(indio_dev->trig);
525
526         return IRQ_HANDLED;
527 }
528
529 static const struct iio_mount_matrix *
530 ak8974_get_mount_matrix(const struct iio_dev *indio_dev,
531                         const struct iio_chan_spec *chan)
532 {
533         struct ak8974 *ak8974 = iio_priv(indio_dev);
534
535         return &ak8974->orientation;
536 }
537
538 static const struct iio_chan_spec_ext_info ak8974_ext_info[] = {
539         IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8974_get_mount_matrix),
540         { },
541 };
542
543 #define AK8974_AXIS_CHANNEL(axis, index)                                \
544         {                                                               \
545                 .type = IIO_MAGN,                                       \
546                 .modified = 1,                                          \
547                 .channel2 = IIO_MOD_##axis,                             \
548                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
549                 .ext_info = ak8974_ext_info,                            \
550                 .address = index,                                       \
551                 .scan_index = index,                                    \
552                 .scan_type = {                                          \
553                         .sign = 's',                                    \
554                         .realbits = 16,                                 \
555                         .storagebits = 16,                              \
556                         .endianness = IIO_LE                            \
557                 },                                                      \
558         }
559
560 static const struct iio_chan_spec ak8974_channels[] = {
561         AK8974_AXIS_CHANNEL(X, 0),
562         AK8974_AXIS_CHANNEL(Y, 1),
563         AK8974_AXIS_CHANNEL(Z, 2),
564         IIO_CHAN_SOFT_TIMESTAMP(3),
565 };
566
567 static const unsigned long ak8974_scan_masks[] = { 0x7, 0 };
568
569 static const struct iio_info ak8974_info = {
570         .read_raw = &ak8974_read_raw,
571         .driver_module = THIS_MODULE,
572 };
573
574 static bool ak8974_writeable_reg(struct device *dev, unsigned int reg)
575 {
576         struct i2c_client *i2c = to_i2c_client(dev);
577         struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
578         struct ak8974 *ak8974 = iio_priv(indio_dev);
579
580         switch (reg) {
581         case AK8974_CTRL1:
582         case AK8974_CTRL2:
583         case AK8974_CTRL3:
584         case AK8974_INT_CTRL:
585         case AK8974_INT_THRES:
586         case AK8974_INT_THRES + 1:
587         case AK8974_PRESET:
588         case AK8974_PRESET + 1:
589                 return true;
590         case AK8974_OFFSET_X:
591         case AK8974_OFFSET_X + 1:
592         case AK8974_OFFSET_Y:
593         case AK8974_OFFSET_Y + 1:
594         case AK8974_OFFSET_Z:
595         case AK8974_OFFSET_Z + 1:
596                 if (ak8974->variant == AK8974_WHOAMI_VALUE_AK8974)
597                         return true;
598                 return false;
599         case AMI305_OFFSET_X:
600         case AMI305_OFFSET_X + 1:
601         case AMI305_OFFSET_Y:
602         case AMI305_OFFSET_Y + 1:
603         case AMI305_OFFSET_Z:
604         case AMI305_OFFSET_Z + 1:
605                 if (ak8974->variant == AK8974_WHOAMI_VALUE_AMI305)
606                         return true;
607                 return false;
608         default:
609                 return false;
610         }
611 }
612
613 static const struct regmap_config ak8974_regmap_config = {
614         .reg_bits = 8,
615         .val_bits = 8,
616         .max_register = 0xff,
617         .writeable_reg = ak8974_writeable_reg,
618 };
619
620 static int ak8974_probe(struct i2c_client *i2c,
621                         const struct i2c_device_id *id)
622 {
623         struct iio_dev *indio_dev;
624         struct ak8974 *ak8974;
625         unsigned long irq_trig;
626         int irq = i2c->irq;
627         int ret;
628
629         /* Register with IIO */
630         indio_dev = devm_iio_device_alloc(&i2c->dev, sizeof(*ak8974));
631         if (indio_dev == NULL)
632                 return -ENOMEM;
633
634         ak8974 = iio_priv(indio_dev);
635         i2c_set_clientdata(i2c, indio_dev);
636         ak8974->i2c = i2c;
637         mutex_init(&ak8974->lock);
638
639         ret = of_iio_read_mount_matrix(&i2c->dev,
640                                        "mount-matrix",
641                                        &ak8974->orientation);
642         if (ret)
643                 return ret;
644
645         ak8974->regs[0].supply = ak8974_reg_avdd;
646         ak8974->regs[1].supply = ak8974_reg_dvdd;
647
648         ret = devm_regulator_bulk_get(&i2c->dev,
649                                       ARRAY_SIZE(ak8974->regs),
650                                       ak8974->regs);
651         if (ret < 0) {
652                 dev_err(&i2c->dev, "cannot get regulators\n");
653                 return ret;
654         }
655
656         ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
657         if (ret < 0) {
658                 dev_err(&i2c->dev, "cannot enable regulators\n");
659                 return ret;
660         }
661
662         /* Take runtime PM online */
663         pm_runtime_get_noresume(&i2c->dev);
664         pm_runtime_set_active(&i2c->dev);
665         pm_runtime_enable(&i2c->dev);
666
667         ak8974->map = devm_regmap_init_i2c(i2c, &ak8974_regmap_config);
668         if (IS_ERR(ak8974->map)) {
669                 dev_err(&i2c->dev, "failed to allocate register map\n");
670                 return PTR_ERR(ak8974->map);
671         }
672
673         ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
674         if (ret) {
675                 dev_err(&i2c->dev, "could not power on\n");
676                 goto power_off;
677         }
678
679         ret = ak8974_detect(ak8974);
680         if (ret) {
681                 dev_err(&i2c->dev, "neither AK8974 nor AMI305 found\n");
682                 goto power_off;
683         }
684
685         ret = ak8974_selftest(ak8974);
686         if (ret)
687                 dev_err(&i2c->dev, "selftest failed (continuing anyway)\n");
688
689         ret = ak8974_reset(ak8974);
690         if (ret) {
691                 dev_err(&i2c->dev, "AK8974 reset failed\n");
692                 goto power_off;
693         }
694
695         pm_runtime_set_autosuspend_delay(&i2c->dev,
696                                          AK8974_AUTOSUSPEND_DELAY);
697         pm_runtime_use_autosuspend(&i2c->dev);
698         pm_runtime_put(&i2c->dev);
699
700         indio_dev->dev.parent = &i2c->dev;
701         indio_dev->channels = ak8974_channels;
702         indio_dev->num_channels = ARRAY_SIZE(ak8974_channels);
703         indio_dev->info = &ak8974_info;
704         indio_dev->available_scan_masks = ak8974_scan_masks;
705         indio_dev->modes = INDIO_DIRECT_MODE;
706         indio_dev->name = ak8974->name;
707
708         ret = iio_triggered_buffer_setup(indio_dev, NULL,
709                                          ak8974_handle_trigger,
710                                          NULL);
711         if (ret) {
712                 dev_err(&i2c->dev, "triggered buffer setup failed\n");
713                 goto disable_pm;
714         }
715
716         /* If we have a valid DRDY IRQ, make use of it */
717         if (irq > 0) {
718                 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
719                 if (irq_trig == IRQF_TRIGGER_RISING) {
720                         dev_info(&i2c->dev, "enable rising edge DRDY IRQ\n");
721                 } else if (irq_trig == IRQF_TRIGGER_FALLING) {
722                         ak8974->drdy_active_low = true;
723                         dev_info(&i2c->dev, "enable falling edge DRDY IRQ\n");
724                 } else {
725                         irq_trig = IRQF_TRIGGER_RISING;
726                 }
727                 irq_trig |= IRQF_ONESHOT;
728                 irq_trig |= IRQF_SHARED;
729
730                 ret = devm_request_threaded_irq(&i2c->dev,
731                                                 irq,
732                                                 ak8974_drdy_irq,
733                                                 ak8974_drdy_irq_thread,
734                                                 irq_trig,
735                                                 ak8974->name,
736                                                 ak8974);
737                 if (ret) {
738                         dev_err(&i2c->dev, "unable to request DRDY IRQ "
739                                 "- proceeding without IRQ\n");
740                         goto no_irq;
741                 }
742                 ak8974->drdy_irq = true;
743         }
744
745 no_irq:
746         ret = iio_device_register(indio_dev);
747         if (ret) {
748                 dev_err(&i2c->dev, "device register failed\n");
749                 goto cleanup_buffer;
750         }
751
752         return 0;
753
754 cleanup_buffer:
755         iio_triggered_buffer_cleanup(indio_dev);
756 disable_pm:
757         pm_runtime_put_noidle(&i2c->dev);
758         pm_runtime_disable(&i2c->dev);
759         ak8974_set_power(ak8974, AK8974_PWR_OFF);
760 power_off:
761         regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
762
763         return ret;
764 }
765
766 static int ak8974_remove(struct i2c_client *i2c)
767 {
768         struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
769         struct ak8974 *ak8974 = iio_priv(indio_dev);
770
771         iio_device_unregister(indio_dev);
772         iio_triggered_buffer_cleanup(indio_dev);
773         pm_runtime_get_sync(&i2c->dev);
774         pm_runtime_put_noidle(&i2c->dev);
775         pm_runtime_disable(&i2c->dev);
776         ak8974_set_power(ak8974, AK8974_PWR_OFF);
777         regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
778
779         return 0;
780 }
781
782 static int __maybe_unused ak8974_runtime_suspend(struct device *dev)
783 {
784         struct ak8974 *ak8974 =
785                 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
786
787         ak8974_set_power(ak8974, AK8974_PWR_OFF);
788         regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
789
790         return 0;
791 }
792
793 static int __maybe_unused ak8974_runtime_resume(struct device *dev)
794 {
795         struct ak8974 *ak8974 =
796                 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
797         int ret;
798
799         ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
800         if (ret)
801                 return ret;
802         msleep(AK8974_POWERON_DELAY);
803         ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
804         if (ret)
805                 goto out_regulator_disable;
806
807         ret = ak8974_configure(ak8974);
808         if (ret)
809                 goto out_disable_power;
810
811         return 0;
812
813 out_disable_power:
814         ak8974_set_power(ak8974, AK8974_PWR_OFF);
815 out_regulator_disable:
816         regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
817
818         return ret;
819 }
820
821 static const struct dev_pm_ops ak8974_dev_pm_ops = {
822         SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
823                                 pm_runtime_force_resume)
824         SET_RUNTIME_PM_OPS(ak8974_runtime_suspend,
825                            ak8974_runtime_resume, NULL)
826 };
827
828 static const struct i2c_device_id ak8974_id[] = {
829         {"ami305", 0 },
830         {"ak8974", 0 },
831         {}
832 };
833 MODULE_DEVICE_TABLE(i2c, ak8974_id);
834
835 static const struct of_device_id ak8974_of_match[] = {
836         { .compatible = "asahi-kasei,ak8974", },
837         {}
838 };
839 MODULE_DEVICE_TABLE(of, ak8974_of_match);
840
841 static struct i2c_driver ak8974_driver = {
842         .driver  = {
843                 .name   = "ak8974",
844                 .pm = &ak8974_dev_pm_ops,
845                 .of_match_table = of_match_ptr(ak8974_of_match),
846         },
847         .probe    = ak8974_probe,
848         .remove   = ak8974_remove,
849         .id_table = ak8974_id,
850 };
851 module_i2c_driver(ak8974_driver);
852
853 MODULE_DESCRIPTION("AK8974 and AMI305 3-axis magnetometer driver");
854 MODULE_AUTHOR("Samu Onkalo");
855 MODULE_AUTHOR("Linus Walleij");
856 MODULE_LICENSE("GPL v2");