]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/iio/magnetometer/ak8975.c
Merge tag 'powerpc-5.6-5' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux
[linux.git] / drivers / iio / magnetometer / ak8975.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * A sensor driver for the magnetometer AK8975.
4  *
5  * Magnetic compass sensor driver for monitoring magnetic flux information.
6  *
7  * Copyright (c) 2010, NVIDIA Corporation.
8  */
9
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/err.h>
16 #include <linux/mutex.h>
17 #include <linux/delay.h>
18 #include <linux/bitops.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/acpi.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/pm_runtime.h>
23
24 #include <linux/iio/iio.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/buffer.h>
27 #include <linux/iio/trigger.h>
28 #include <linux/iio/trigger_consumer.h>
29 #include <linux/iio/triggered_buffer.h>
30
31 /*
32  * Register definitions, as well as various shifts and masks to get at the
33  * individual fields of the registers.
34  */
35 #define AK8975_REG_WIA                  0x00
36 #define AK8975_DEVICE_ID                0x48
37
38 #define AK8975_REG_INFO                 0x01
39
40 #define AK8975_REG_ST1                  0x02
41 #define AK8975_REG_ST1_DRDY_SHIFT       0
42 #define AK8975_REG_ST1_DRDY_MASK        (1 << AK8975_REG_ST1_DRDY_SHIFT)
43
44 #define AK8975_REG_HXL                  0x03
45 #define AK8975_REG_HXH                  0x04
46 #define AK8975_REG_HYL                  0x05
47 #define AK8975_REG_HYH                  0x06
48 #define AK8975_REG_HZL                  0x07
49 #define AK8975_REG_HZH                  0x08
50 #define AK8975_REG_ST2                  0x09
51 #define AK8975_REG_ST2_DERR_SHIFT       2
52 #define AK8975_REG_ST2_DERR_MASK        (1 << AK8975_REG_ST2_DERR_SHIFT)
53
54 #define AK8975_REG_ST2_HOFL_SHIFT       3
55 #define AK8975_REG_ST2_HOFL_MASK        (1 << AK8975_REG_ST2_HOFL_SHIFT)
56
57 #define AK8975_REG_CNTL                 0x0A
58 #define AK8975_REG_CNTL_MODE_SHIFT      0
59 #define AK8975_REG_CNTL_MODE_MASK       (0xF << AK8975_REG_CNTL_MODE_SHIFT)
60 #define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
61 #define AK8975_REG_CNTL_MODE_ONCE       0x01
62 #define AK8975_REG_CNTL_MODE_SELF_TEST  0x08
63 #define AK8975_REG_CNTL_MODE_FUSE_ROM   0x0F
64
65 #define AK8975_REG_RSVC                 0x0B
66 #define AK8975_REG_ASTC                 0x0C
67 #define AK8975_REG_TS1                  0x0D
68 #define AK8975_REG_TS2                  0x0E
69 #define AK8975_REG_I2CDIS               0x0F
70 #define AK8975_REG_ASAX                 0x10
71 #define AK8975_REG_ASAY                 0x11
72 #define AK8975_REG_ASAZ                 0x12
73
74 #define AK8975_MAX_REGS                 AK8975_REG_ASAZ
75
76 /*
77  * AK09912 Register definitions
78  */
79 #define AK09912_REG_WIA1                0x00
80 #define AK09912_REG_WIA2                0x01
81 #define AK09912_DEVICE_ID               0x04
82 #define AK09911_DEVICE_ID               0x05
83
84 #define AK09911_REG_INFO1               0x02
85 #define AK09911_REG_INFO2               0x03
86
87 #define AK09912_REG_ST1                 0x10
88
89 #define AK09912_REG_ST1_DRDY_SHIFT      0
90 #define AK09912_REG_ST1_DRDY_MASK       (1 << AK09912_REG_ST1_DRDY_SHIFT)
91
92 #define AK09912_REG_HXL                 0x11
93 #define AK09912_REG_HXH                 0x12
94 #define AK09912_REG_HYL                 0x13
95 #define AK09912_REG_HYH                 0x14
96 #define AK09912_REG_HZL                 0x15
97 #define AK09912_REG_HZH                 0x16
98 #define AK09912_REG_TMPS                0x17
99
100 #define AK09912_REG_ST2                 0x18
101 #define AK09912_REG_ST2_HOFL_SHIFT      3
102 #define AK09912_REG_ST2_HOFL_MASK       (1 << AK09912_REG_ST2_HOFL_SHIFT)
103
104 #define AK09912_REG_CNTL1               0x30
105
106 #define AK09912_REG_CNTL2               0x31
107 #define AK09912_REG_CNTL_MODE_POWER_DOWN        0x00
108 #define AK09912_REG_CNTL_MODE_ONCE      0x01
109 #define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
110 #define AK09912_REG_CNTL_MODE_FUSE_ROM  0x1F
111 #define AK09912_REG_CNTL2_MODE_SHIFT    0
112 #define AK09912_REG_CNTL2_MODE_MASK     (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
113
114 #define AK09912_REG_CNTL3               0x32
115
116 #define AK09912_REG_TS1                 0x33
117 #define AK09912_REG_TS2                 0x34
118 #define AK09912_REG_TS3                 0x35
119 #define AK09912_REG_I2CDIS              0x36
120 #define AK09912_REG_TS4                 0x37
121
122 #define AK09912_REG_ASAX                0x60
123 #define AK09912_REG_ASAY                0x61
124 #define AK09912_REG_ASAZ                0x62
125
126 #define AK09912_MAX_REGS                AK09912_REG_ASAZ
127
128 /*
129  * Miscellaneous values.
130  */
131 #define AK8975_MAX_CONVERSION_TIMEOUT   500
132 #define AK8975_CONVERSION_DONE_POLL_TIME 10
133 #define AK8975_DATA_READY_TIMEOUT       ((100*HZ)/1000)
134
135 /*
136  * Precalculate scale factor (in Gauss units) for each axis and
137  * store in the device data.
138  *
139  * This scale factor is axis-dependent, and is derived from 3 calibration
140  * factors ASA(x), ASA(y), and ASA(z).
141  *
142  * These ASA values are read from the sensor device at start of day, and
143  * cached in the device context struct.
144  *
145  * Adjusting the flux value with the sensitivity adjustment value should be
146  * done via the following formula:
147  *
148  * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
149  * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
150  * is the resultant adjusted value.
151  *
152  * We reduce the formula to:
153  *
154  * Hadj = H * (ASA + 128) / 256
155  *
156  * H is in the range of -4096 to 4095.  The magnetometer has a range of
157  * +-1229uT.  To go from the raw value to uT is:
158  *
159  * HuT = H * 1229/4096, or roughly, 3/10.
160  *
161  * Since 1uT = 0.01 gauss, our final scale factor becomes:
162  *
163  * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
164  * Hadj = H * ((ASA + 128) * 0.003) / 256
165  *
166  * Since ASA doesn't change, we cache the resultant scale factor into the
167  * device context in ak8975_setup().
168  *
169  * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
170  * multiply the stored scale value by 1e6.
171  */
172 static long ak8975_raw_to_gauss(u16 data)
173 {
174         return (((long)data + 128) * 3000) / 256;
175 }
176
177 /*
178  * For AK8963 and AK09911, same calculation, but the device is less sensitive:
179  *
180  * H is in the range of +-8190.  The magnetometer has a range of
181  * +-4912uT.  To go from the raw value to uT is:
182  *
183  * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
184  */
185
186 static long ak8963_09911_raw_to_gauss(u16 data)
187 {
188         return (((long)data + 128) * 6000) / 256;
189 }
190
191 /*
192  * For AK09912, same calculation, except the device is more sensitive:
193  *
194  * H is in the range of -32752 to 32752.  The magnetometer has a range of
195  * +-4912uT.  To go from the raw value to uT is:
196  *
197  * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
198  */
199 static long ak09912_raw_to_gauss(u16 data)
200 {
201         return (((long)data + 128) * 1500) / 256;
202 }
203
204 /* Compatible Asahi Kasei Compass parts */
205 enum asahi_compass_chipset {
206         AKXXXX          = 0,
207         AK8975,
208         AK8963,
209         AK09911,
210         AK09912,
211 };
212
213 enum ak_ctrl_reg_addr {
214         ST1,
215         ST2,
216         CNTL,
217         ASA_BASE,
218         MAX_REGS,
219         REGS_END,
220 };
221
222 enum ak_ctrl_reg_mask {
223         ST1_DRDY,
224         ST2_HOFL,
225         ST2_DERR,
226         CNTL_MODE,
227         MASK_END,
228 };
229
230 enum ak_ctrl_mode {
231         POWER_DOWN,
232         MODE_ONCE,
233         SELF_TEST,
234         FUSE_ROM,
235         MODE_END,
236 };
237
238 struct ak_def {
239         enum asahi_compass_chipset type;
240         long (*raw_to_gauss)(u16 data);
241         u16 range;
242         u8 ctrl_regs[REGS_END];
243         u8 ctrl_masks[MASK_END];
244         u8 ctrl_modes[MODE_END];
245         u8 data_regs[3];
246 };
247
248 static const struct ak_def ak_def_array[] = {
249         {
250                 .type = AK8975,
251                 .raw_to_gauss = ak8975_raw_to_gauss,
252                 .range = 4096,
253                 .ctrl_regs = {
254                         AK8975_REG_ST1,
255                         AK8975_REG_ST2,
256                         AK8975_REG_CNTL,
257                         AK8975_REG_ASAX,
258                         AK8975_MAX_REGS},
259                 .ctrl_masks = {
260                         AK8975_REG_ST1_DRDY_MASK,
261                         AK8975_REG_ST2_HOFL_MASK,
262                         AK8975_REG_ST2_DERR_MASK,
263                         AK8975_REG_CNTL_MODE_MASK},
264                 .ctrl_modes = {
265                         AK8975_REG_CNTL_MODE_POWER_DOWN,
266                         AK8975_REG_CNTL_MODE_ONCE,
267                         AK8975_REG_CNTL_MODE_SELF_TEST,
268                         AK8975_REG_CNTL_MODE_FUSE_ROM},
269                 .data_regs = {
270                         AK8975_REG_HXL,
271                         AK8975_REG_HYL,
272                         AK8975_REG_HZL},
273         },
274         {
275                 .type = AK8963,
276                 .raw_to_gauss = ak8963_09911_raw_to_gauss,
277                 .range = 8190,
278                 .ctrl_regs = {
279                         AK8975_REG_ST1,
280                         AK8975_REG_ST2,
281                         AK8975_REG_CNTL,
282                         AK8975_REG_ASAX,
283                         AK8975_MAX_REGS},
284                 .ctrl_masks = {
285                         AK8975_REG_ST1_DRDY_MASK,
286                         AK8975_REG_ST2_HOFL_MASK,
287                         0,
288                         AK8975_REG_CNTL_MODE_MASK},
289                 .ctrl_modes = {
290                         AK8975_REG_CNTL_MODE_POWER_DOWN,
291                         AK8975_REG_CNTL_MODE_ONCE,
292                         AK8975_REG_CNTL_MODE_SELF_TEST,
293                         AK8975_REG_CNTL_MODE_FUSE_ROM},
294                 .data_regs = {
295                         AK8975_REG_HXL,
296                         AK8975_REG_HYL,
297                         AK8975_REG_HZL},
298         },
299         {
300                 .type = AK09911,
301                 .raw_to_gauss = ak8963_09911_raw_to_gauss,
302                 .range = 8192,
303                 .ctrl_regs = {
304                         AK09912_REG_ST1,
305                         AK09912_REG_ST2,
306                         AK09912_REG_CNTL2,
307                         AK09912_REG_ASAX,
308                         AK09912_MAX_REGS},
309                 .ctrl_masks = {
310                         AK09912_REG_ST1_DRDY_MASK,
311                         AK09912_REG_ST2_HOFL_MASK,
312                         0,
313                         AK09912_REG_CNTL2_MODE_MASK},
314                 .ctrl_modes = {
315                         AK09912_REG_CNTL_MODE_POWER_DOWN,
316                         AK09912_REG_CNTL_MODE_ONCE,
317                         AK09912_REG_CNTL_MODE_SELF_TEST,
318                         AK09912_REG_CNTL_MODE_FUSE_ROM},
319                 .data_regs = {
320                         AK09912_REG_HXL,
321                         AK09912_REG_HYL,
322                         AK09912_REG_HZL},
323         },
324         {
325                 .type = AK09912,
326                 .raw_to_gauss = ak09912_raw_to_gauss,
327                 .range = 32752,
328                 .ctrl_regs = {
329                         AK09912_REG_ST1,
330                         AK09912_REG_ST2,
331                         AK09912_REG_CNTL2,
332                         AK09912_REG_ASAX,
333                         AK09912_MAX_REGS},
334                 .ctrl_masks = {
335                         AK09912_REG_ST1_DRDY_MASK,
336                         AK09912_REG_ST2_HOFL_MASK,
337                         0,
338                         AK09912_REG_CNTL2_MODE_MASK},
339                 .ctrl_modes = {
340                         AK09912_REG_CNTL_MODE_POWER_DOWN,
341                         AK09912_REG_CNTL_MODE_ONCE,
342                         AK09912_REG_CNTL_MODE_SELF_TEST,
343                         AK09912_REG_CNTL_MODE_FUSE_ROM},
344                 .data_regs = {
345                         AK09912_REG_HXL,
346                         AK09912_REG_HYL,
347                         AK09912_REG_HZL},
348         }
349 };
350
351 /*
352  * Per-instance context data for the device.
353  */
354 struct ak8975_data {
355         struct i2c_client       *client;
356         const struct ak_def     *def;
357         struct mutex            lock;
358         u8                      asa[3];
359         long                    raw_to_gauss[3];
360         struct gpio_desc        *eoc_gpiod;
361         int                     eoc_irq;
362         wait_queue_head_t       data_ready_queue;
363         unsigned long           flags;
364         u8                      cntl_cache;
365         struct iio_mount_matrix orientation;
366         struct regulator        *vdd;
367         struct regulator        *vid;
368 };
369
370 /* Enable attached power regulator if any. */
371 static int ak8975_power_on(const struct ak8975_data *data)
372 {
373         int ret;
374
375         ret = regulator_enable(data->vdd);
376         if (ret) {
377                 dev_warn(&data->client->dev,
378                          "Failed to enable specified Vdd supply\n");
379                 return ret;
380         }
381         ret = regulator_enable(data->vid);
382         if (ret) {
383                 dev_warn(&data->client->dev,
384                          "Failed to enable specified Vid supply\n");
385                 return ret;
386         }
387         /*
388          * According to the datasheet the power supply rise time i 200us
389          * and the minimum wait time before mode setting is 100us, in
390          * total 300 us. Add some margin and say minimum 500us here.
391          */
392         usleep_range(500, 1000);
393         return 0;
394 }
395
396 /* Disable attached power regulator if any. */
397 static void ak8975_power_off(const struct ak8975_data *data)
398 {
399         regulator_disable(data->vid);
400         regulator_disable(data->vdd);
401 }
402
403 /*
404  * Return 0 if the i2c device is the one we expect.
405  * return a negative error number otherwise
406  */
407 static int ak8975_who_i_am(struct i2c_client *client,
408                            enum asahi_compass_chipset type)
409 {
410         u8 wia_val[2];
411         int ret;
412
413         /*
414          * Signature for each device:
415          * Device   |  WIA1      |  WIA2
416          * AK09912  |  DEVICE_ID |  AK09912_DEVICE_ID
417          * AK09911  |  DEVICE_ID |  AK09911_DEVICE_ID
418          * AK8975   |  DEVICE_ID |  NA
419          * AK8963   |  DEVICE_ID |  NA
420          */
421         ret = i2c_smbus_read_i2c_block_data_or_emulated(
422                         client, AK09912_REG_WIA1, 2, wia_val);
423         if (ret < 0) {
424                 dev_err(&client->dev, "Error reading WIA\n");
425                 return ret;
426         }
427
428         if (wia_val[0] != AK8975_DEVICE_ID)
429                 return -ENODEV;
430
431         switch (type) {
432         case AK8975:
433         case AK8963:
434                 return 0;
435         case AK09911:
436                 if (wia_val[1] == AK09911_DEVICE_ID)
437                         return 0;
438                 break;
439         case AK09912:
440                 if (wia_val[1] == AK09912_DEVICE_ID)
441                         return 0;
442                 break;
443         default:
444                 dev_err(&client->dev, "Type %d unknown\n", type);
445         }
446         return -ENODEV;
447 }
448
449 /*
450  * Helper function to write to CNTL register.
451  */
452 static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
453 {
454         u8 regval;
455         int ret;
456
457         regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
458                  data->def->ctrl_modes[mode];
459         ret = i2c_smbus_write_byte_data(data->client,
460                                         data->def->ctrl_regs[CNTL], regval);
461         if (ret < 0) {
462                 return ret;
463         }
464         data->cntl_cache = regval;
465         /* After mode change wait atleast 100us */
466         usleep_range(100, 500);
467
468         return 0;
469 }
470
471 /*
472  * Handle data ready irq
473  */
474 static irqreturn_t ak8975_irq_handler(int irq, void *data)
475 {
476         struct ak8975_data *ak8975 = data;
477
478         set_bit(0, &ak8975->flags);
479         wake_up(&ak8975->data_ready_queue);
480
481         return IRQ_HANDLED;
482 }
483
484 /*
485  * Install data ready interrupt handler
486  */
487 static int ak8975_setup_irq(struct ak8975_data *data)
488 {
489         struct i2c_client *client = data->client;
490         int rc;
491         int irq;
492
493         init_waitqueue_head(&data->data_ready_queue);
494         clear_bit(0, &data->flags);
495         if (client->irq)
496                 irq = client->irq;
497         else
498                 irq = gpiod_to_irq(data->eoc_gpiod);
499
500         rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
501                               IRQF_TRIGGER_RISING | IRQF_ONESHOT,
502                               dev_name(&client->dev), data);
503         if (rc < 0) {
504                 dev_err(&client->dev, "irq %d request failed: %d\n", irq, rc);
505                 return rc;
506         }
507
508         data->eoc_irq = irq;
509
510         return rc;
511 }
512
513
514 /*
515  * Perform some start-of-day setup, including reading the asa calibration
516  * values and caching them.
517  */
518 static int ak8975_setup(struct i2c_client *client)
519 {
520         struct iio_dev *indio_dev = i2c_get_clientdata(client);
521         struct ak8975_data *data = iio_priv(indio_dev);
522         int ret;
523
524         /* Write the fused rom access mode. */
525         ret = ak8975_set_mode(data, FUSE_ROM);
526         if (ret < 0) {
527                 dev_err(&client->dev, "Error in setting fuse access mode\n");
528                 return ret;
529         }
530
531         /* Get asa data and store in the device data. */
532         ret = i2c_smbus_read_i2c_block_data_or_emulated(
533                         client, data->def->ctrl_regs[ASA_BASE],
534                         3, data->asa);
535         if (ret < 0) {
536                 dev_err(&client->dev, "Not able to read asa data\n");
537                 return ret;
538         }
539
540         /* After reading fuse ROM data set power-down mode */
541         ret = ak8975_set_mode(data, POWER_DOWN);
542         if (ret < 0) {
543                 dev_err(&client->dev, "Error in setting power-down mode\n");
544                 return ret;
545         }
546
547         if (data->eoc_gpiod || client->irq > 0) {
548                 ret = ak8975_setup_irq(data);
549                 if (ret < 0) {
550                         dev_err(&client->dev,
551                                 "Error setting data ready interrupt\n");
552                         return ret;
553                 }
554         }
555
556         data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
557         data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
558         data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
559
560         return 0;
561 }
562
563 static int wait_conversion_complete_gpio(struct ak8975_data *data)
564 {
565         struct i2c_client *client = data->client;
566         u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
567         int ret;
568
569         /* Wait for the conversion to complete. */
570         while (timeout_ms) {
571                 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
572                 if (gpiod_get_value(data->eoc_gpiod))
573                         break;
574                 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
575         }
576         if (!timeout_ms) {
577                 dev_err(&client->dev, "Conversion timeout happened\n");
578                 return -EINVAL;
579         }
580
581         ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
582         if (ret < 0)
583                 dev_err(&client->dev, "Error in reading ST1\n");
584
585         return ret;
586 }
587
588 static int wait_conversion_complete_polled(struct ak8975_data *data)
589 {
590         struct i2c_client *client = data->client;
591         u8 read_status;
592         u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
593         int ret;
594
595         /* Wait for the conversion to complete. */
596         while (timeout_ms) {
597                 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
598                 ret = i2c_smbus_read_byte_data(client,
599                                                data->def->ctrl_regs[ST1]);
600                 if (ret < 0) {
601                         dev_err(&client->dev, "Error in reading ST1\n");
602                         return ret;
603                 }
604                 read_status = ret;
605                 if (read_status)
606                         break;
607                 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
608         }
609         if (!timeout_ms) {
610                 dev_err(&client->dev, "Conversion timeout happened\n");
611                 return -EINVAL;
612         }
613
614         return read_status;
615 }
616
617 /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
618 static int wait_conversion_complete_interrupt(struct ak8975_data *data)
619 {
620         int ret;
621
622         ret = wait_event_timeout(data->data_ready_queue,
623                                  test_bit(0, &data->flags),
624                                  AK8975_DATA_READY_TIMEOUT);
625         clear_bit(0, &data->flags);
626
627         return ret > 0 ? 0 : -ETIME;
628 }
629
630 static int ak8975_start_read_axis(struct ak8975_data *data,
631                                   const struct i2c_client *client)
632 {
633         /* Set up the device for taking a sample. */
634         int ret = ak8975_set_mode(data, MODE_ONCE);
635
636         if (ret < 0) {
637                 dev_err(&client->dev, "Error in setting operating mode\n");
638                 return ret;
639         }
640
641         /* Wait for the conversion to complete. */
642         if (data->eoc_irq)
643                 ret = wait_conversion_complete_interrupt(data);
644         else if (data->eoc_gpiod)
645                 ret = wait_conversion_complete_gpio(data);
646         else
647                 ret = wait_conversion_complete_polled(data);
648         if (ret < 0)
649                 return ret;
650
651         /* This will be executed only for non-interrupt based waiting case */
652         if (ret & data->def->ctrl_masks[ST1_DRDY]) {
653                 ret = i2c_smbus_read_byte_data(client,
654                                                data->def->ctrl_regs[ST2]);
655                 if (ret < 0) {
656                         dev_err(&client->dev, "Error in reading ST2\n");
657                         return ret;
658                 }
659                 if (ret & (data->def->ctrl_masks[ST2_DERR] |
660                            data->def->ctrl_masks[ST2_HOFL])) {
661                         dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
662                         return -EINVAL;
663                 }
664         }
665
666         return 0;
667 }
668
669 /* Retrieve raw flux value for one of the x, y, or z axis.  */
670 static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
671 {
672         struct ak8975_data *data = iio_priv(indio_dev);
673         const struct i2c_client *client = data->client;
674         const struct ak_def *def = data->def;
675         __le16 rval;
676         u16 buff;
677         int ret;
678
679         pm_runtime_get_sync(&data->client->dev);
680
681         mutex_lock(&data->lock);
682
683         ret = ak8975_start_read_axis(data, client);
684         if (ret)
685                 goto exit;
686
687         ret = i2c_smbus_read_i2c_block_data_or_emulated(
688                         client, def->data_regs[index],
689                         sizeof(rval), (u8*)&rval);
690         if (ret < 0)
691                 goto exit;
692
693         mutex_unlock(&data->lock);
694
695         pm_runtime_mark_last_busy(&data->client->dev);
696         pm_runtime_put_autosuspend(&data->client->dev);
697
698         /* Swap bytes and convert to valid range. */
699         buff = le16_to_cpu(rval);
700         *val = clamp_t(s16, buff, -def->range, def->range);
701         return IIO_VAL_INT;
702
703 exit:
704         mutex_unlock(&data->lock);
705         dev_err(&client->dev, "Error in reading axis\n");
706         return ret;
707 }
708
709 static int ak8975_read_raw(struct iio_dev *indio_dev,
710                            struct iio_chan_spec const *chan,
711                            int *val, int *val2,
712                            long mask)
713 {
714         struct ak8975_data *data = iio_priv(indio_dev);
715
716         switch (mask) {
717         case IIO_CHAN_INFO_RAW:
718                 return ak8975_read_axis(indio_dev, chan->address, val);
719         case IIO_CHAN_INFO_SCALE:
720                 *val = 0;
721                 *val2 = data->raw_to_gauss[chan->address];
722                 return IIO_VAL_INT_PLUS_MICRO;
723         }
724         return -EINVAL;
725 }
726
727 static const struct iio_mount_matrix *
728 ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
729                         const struct iio_chan_spec *chan)
730 {
731         struct ak8975_data *data = iio_priv(indio_dev);
732
733         return &data->orientation;
734 }
735
736 static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
737         IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
738         { }
739 };
740
741 #define AK8975_CHANNEL(axis, index)                                     \
742         {                                                               \
743                 .type = IIO_MAGN,                                       \
744                 .modified = 1,                                          \
745                 .channel2 = IIO_MOD_##axis,                             \
746                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |          \
747                              BIT(IIO_CHAN_INFO_SCALE),                  \
748                 .address = index,                                       \
749                 .scan_index = index,                                    \
750                 .scan_type = {                                          \
751                         .sign = 's',                                    \
752                         .realbits = 16,                                 \
753                         .storagebits = 16,                              \
754                         .endianness = IIO_CPU                           \
755                 },                                                      \
756                 .ext_info = ak8975_ext_info,                            \
757         }
758
759 static const struct iio_chan_spec ak8975_channels[] = {
760         AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
761         IIO_CHAN_SOFT_TIMESTAMP(3),
762 };
763
764 static const unsigned long ak8975_scan_masks[] = { 0x7, 0 };
765
766 static const struct iio_info ak8975_info = {
767         .read_raw = &ak8975_read_raw,
768 };
769
770 #ifdef CONFIG_ACPI
771 static const struct acpi_device_id ak_acpi_match[] = {
772         {"AK8975", AK8975},
773         {"AK8963", AK8963},
774         {"INVN6500", AK8963},
775         {"AK009911", AK09911},
776         {"AK09911", AK09911},
777         {"AKM9911", AK09911},
778         {"AK09912", AK09912},
779         { }
780 };
781 MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
782 #endif
783
784 static void ak8975_fill_buffer(struct iio_dev *indio_dev)
785 {
786         struct ak8975_data *data = iio_priv(indio_dev);
787         const struct i2c_client *client = data->client;
788         const struct ak_def *def = data->def;
789         int ret;
790         s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */
791         __le16 fval[3];
792
793         mutex_lock(&data->lock);
794
795         ret = ak8975_start_read_axis(data, client);
796         if (ret)
797                 goto unlock;
798
799         /*
800          * For each axis, read the flux value from the appropriate register
801          * (the register is specified in the iio device attributes).
802          */
803         ret = i2c_smbus_read_i2c_block_data_or_emulated(client,
804                                                         def->data_regs[0],
805                                                         3 * sizeof(fval[0]),
806                                                         (u8 *)fval);
807         if (ret < 0)
808                 goto unlock;
809
810         mutex_unlock(&data->lock);
811
812         /* Clamp to valid range. */
813         buff[0] = clamp_t(s16, le16_to_cpu(fval[0]), -def->range, def->range);
814         buff[1] = clamp_t(s16, le16_to_cpu(fval[1]), -def->range, def->range);
815         buff[2] = clamp_t(s16, le16_to_cpu(fval[2]), -def->range, def->range);
816
817         iio_push_to_buffers_with_timestamp(indio_dev, buff,
818                                            iio_get_time_ns(indio_dev));
819         return;
820
821 unlock:
822         mutex_unlock(&data->lock);
823         dev_err(&client->dev, "Error in reading axes block\n");
824 }
825
826 static irqreturn_t ak8975_handle_trigger(int irq, void *p)
827 {
828         const struct iio_poll_func *pf = p;
829         struct iio_dev *indio_dev = pf->indio_dev;
830
831         ak8975_fill_buffer(indio_dev);
832         iio_trigger_notify_done(indio_dev->trig);
833         return IRQ_HANDLED;
834 }
835
836 static int ak8975_probe(struct i2c_client *client,
837                         const struct i2c_device_id *id)
838 {
839         struct ak8975_data *data;
840         struct iio_dev *indio_dev;
841         struct gpio_desc *eoc_gpiod;
842         const void *match;
843         unsigned int i;
844         int err;
845         enum asahi_compass_chipset chipset;
846         const char *name = NULL;
847
848         /*
849          * Grab and set up the supplied GPIO.
850          * We may not have a GPIO based IRQ to scan, that is fine, we will
851          * poll if so.
852          */
853         eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN);
854         if (IS_ERR(eoc_gpiod))
855                 return PTR_ERR(eoc_gpiod);
856         if (eoc_gpiod)
857                 gpiod_set_consumer_name(eoc_gpiod, "ak_8975");
858
859         /* Register with IIO */
860         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
861         if (indio_dev == NULL)
862                 return -ENOMEM;
863
864         data = iio_priv(indio_dev);
865         i2c_set_clientdata(client, indio_dev);
866
867         data->client = client;
868         data->eoc_gpiod = eoc_gpiod;
869         data->eoc_irq = 0;
870
871         err = iio_read_mount_matrix(&client->dev, "mount-matrix", &data->orientation);
872         if (err)
873                 return err;
874
875         /* id will be NULL when enumerated via ACPI */
876         match = device_get_match_data(&client->dev);
877         if (match) {
878                 chipset = (enum asahi_compass_chipset)(match);
879                 name = dev_name(&client->dev);
880         } else if (id) {
881                 chipset = (enum asahi_compass_chipset)(id->driver_data);
882                 name = id->name;
883         } else
884                 return -ENOSYS;
885
886         for (i = 0; i < ARRAY_SIZE(ak_def_array); i++)
887                 if (ak_def_array[i].type == chipset)
888                         break;
889
890         if (i == ARRAY_SIZE(ak_def_array)) {
891                 dev_err(&client->dev, "AKM device type unsupported: %d\n",
892                         chipset);
893                 return -ENODEV;
894         }
895
896         data->def = &ak_def_array[i];
897
898         /* Fetch the regulators */
899         data->vdd = devm_regulator_get(&client->dev, "vdd");
900         if (IS_ERR(data->vdd))
901                 return PTR_ERR(data->vdd);
902         data->vid = devm_regulator_get(&client->dev, "vid");
903         if (IS_ERR(data->vid))
904                 return PTR_ERR(data->vid);
905
906         err = ak8975_power_on(data);
907         if (err)
908                 return err;
909
910         err = ak8975_who_i_am(client, data->def->type);
911         if (err < 0) {
912                 dev_err(&client->dev, "Unexpected device\n");
913                 goto power_off;
914         }
915         dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
916
917         /* Perform some basic start-of-day setup of the device. */
918         err = ak8975_setup(client);
919         if (err < 0) {
920                 dev_err(&client->dev, "%s initialization fails\n", name);
921                 goto power_off;
922         }
923
924         mutex_init(&data->lock);
925         indio_dev->dev.parent = &client->dev;
926         indio_dev->channels = ak8975_channels;
927         indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
928         indio_dev->info = &ak8975_info;
929         indio_dev->available_scan_masks = ak8975_scan_masks;
930         indio_dev->modes = INDIO_DIRECT_MODE;
931         indio_dev->name = name;
932
933         err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger,
934                                          NULL);
935         if (err) {
936                 dev_err(&client->dev, "triggered buffer setup failed\n");
937                 goto power_off;
938         }
939
940         err = iio_device_register(indio_dev);
941         if (err) {
942                 dev_err(&client->dev, "device register failed\n");
943                 goto cleanup_buffer;
944         }
945
946         /* Enable runtime PM */
947         pm_runtime_get_noresume(&client->dev);
948         pm_runtime_set_active(&client->dev);
949         pm_runtime_enable(&client->dev);
950         /*
951          * The device comes online in 500us, so add two orders of magnitude
952          * of delay before autosuspending: 50 ms.
953          */
954         pm_runtime_set_autosuspend_delay(&client->dev, 50);
955         pm_runtime_use_autosuspend(&client->dev);
956         pm_runtime_put(&client->dev);
957
958         return 0;
959
960 cleanup_buffer:
961         iio_triggered_buffer_cleanup(indio_dev);
962 power_off:
963         ak8975_power_off(data);
964         return err;
965 }
966
967 static int ak8975_remove(struct i2c_client *client)
968 {
969         struct iio_dev *indio_dev = i2c_get_clientdata(client);
970         struct ak8975_data *data = iio_priv(indio_dev);
971
972         pm_runtime_get_sync(&client->dev);
973         pm_runtime_put_noidle(&client->dev);
974         pm_runtime_disable(&client->dev);
975         iio_device_unregister(indio_dev);
976         iio_triggered_buffer_cleanup(indio_dev);
977         ak8975_set_mode(data, POWER_DOWN);
978         ak8975_power_off(data);
979
980         return 0;
981 }
982
983 #ifdef CONFIG_PM
984 static int ak8975_runtime_suspend(struct device *dev)
985 {
986         struct i2c_client *client = to_i2c_client(dev);
987         struct iio_dev *indio_dev = i2c_get_clientdata(client);
988         struct ak8975_data *data = iio_priv(indio_dev);
989         int ret;
990
991         /* Set the device in power down if it wasn't already */
992         ret = ak8975_set_mode(data, POWER_DOWN);
993         if (ret < 0) {
994                 dev_err(&client->dev, "Error in setting power-down mode\n");
995                 return ret;
996         }
997         /* Next cut the regulators */
998         ak8975_power_off(data);
999
1000         return 0;
1001 }
1002
1003 static int ak8975_runtime_resume(struct device *dev)
1004 {
1005         struct i2c_client *client = to_i2c_client(dev);
1006         struct iio_dev *indio_dev = i2c_get_clientdata(client);
1007         struct ak8975_data *data = iio_priv(indio_dev);
1008         int ret;
1009
1010         /* Take up the regulators */
1011         ak8975_power_on(data);
1012         /*
1013          * We come up in powered down mode, the reading routines will
1014          * put us in the mode to read values later.
1015          */
1016         ret = ak8975_set_mode(data, POWER_DOWN);
1017         if (ret < 0) {
1018                 dev_err(&client->dev, "Error in setting power-down mode\n");
1019                 return ret;
1020         }
1021
1022         return 0;
1023 }
1024 #endif /* CONFIG_PM */
1025
1026 static const struct dev_pm_ops ak8975_dev_pm_ops = {
1027         SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1028                                 pm_runtime_force_resume)
1029         SET_RUNTIME_PM_OPS(ak8975_runtime_suspend,
1030                            ak8975_runtime_resume, NULL)
1031 };
1032
1033 static const struct i2c_device_id ak8975_id[] = {
1034         {"ak8975", AK8975},
1035         {"ak8963", AK8963},
1036         {"AK8963", AK8963},
1037         {"ak09911", AK09911},
1038         {"ak09912", AK09912},
1039         {}
1040 };
1041
1042 MODULE_DEVICE_TABLE(i2c, ak8975_id);
1043
1044 static const struct of_device_id ak8975_of_match[] = {
1045         { .compatible = "asahi-kasei,ak8975", },
1046         { .compatible = "ak8975", },
1047         { .compatible = "asahi-kasei,ak8963", },
1048         { .compatible = "ak8963", },
1049         { .compatible = "asahi-kasei,ak09911", },
1050         { .compatible = "ak09911", },
1051         { .compatible = "asahi-kasei,ak09912", },
1052         { .compatible = "ak09912", },
1053         {}
1054 };
1055 MODULE_DEVICE_TABLE(of, ak8975_of_match);
1056
1057 static struct i2c_driver ak8975_driver = {
1058         .driver = {
1059                 .name   = "ak8975",
1060                 .pm = &ak8975_dev_pm_ops,
1061                 .of_match_table = of_match_ptr(ak8975_of_match),
1062                 .acpi_match_table = ACPI_PTR(ak_acpi_match),
1063         },
1064         .probe          = ak8975_probe,
1065         .remove         = ak8975_remove,
1066         .id_table       = ak8975_id,
1067 };
1068 module_i2c_driver(ak8975_driver);
1069
1070 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1071 MODULE_DESCRIPTION("AK8975 magnetometer driver");
1072 MODULE_LICENSE("GPL");