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