1 // SPDX-License-Identifier: GPL-2.0
3 * ad2s1210.c support for the ADI Resolver to Digital Converters: AD2S1210
5 * Copyright (c) 2010-2010 Analog Devices Inc.
6 * Copyright (c) 2023 BayLibre, SAS
8 * Device register to IIO ABI mapping:
10 * Register | Addr | IIO ABI (sysfs)
11 * ----------------------------|------|-------------------------------------------
12 * DOS Overrange Threshold | 0x89 | events/in_altvoltage0_thresh_rising_value
13 * DOS Mismatch Threshold | 0x8A | events/in_altvoltage0_mag_rising_value
14 * DOS Reset Maximum Threshold | 0x8B | events/in_altvoltage0_mag_rising_reset_max
15 * DOS Reset Minimum Threshold | 0x8C | events/in_altvoltage0_mag_rising_reset_min
16 * LOT High Threshold | 0x8D | events/in_angl1_thresh_rising_value
17 * LOT Low Threshold [1] | 0x8E | events/in_angl1_thresh_rising_hysteresis
18 * Excitation Frequency | 0x91 | out_altvoltage0_frequency
19 * Control | 0x92 | *as bit fields*
20 * Phase lock range | D5 | events/in_phase0_mag_rising_value
21 * Hysteresis | D4 | in_angl0_hysteresis
22 * Encoder resolution | D3:2 | *not implemented*
23 * Resolution | D1:0 | *device tree: assigned-resolution-bits*
24 * Soft Reset | 0xF0 | [2]
25 * Fault | 0xFF | *not implemented*
27 * [1]: The value written to the LOT low register is high value minus the
29 * [2]: Soft reset is performed when `out_altvoltage0_frequency` is written.
31 * Fault to event mapping:
33 * Fault | | Channel | Type | Direction
34 * ----------------------------------------|----|---------------------------------
35 * Sine/cosine inputs clipped [3] | D7 | altvoltage1 | mag | either
36 * Sine/cosine inputs below LOS | D6 | altvoltage0 | thresh | falling
37 * Sine/cosine inputs exceed DOS overrange | D5 | altvoltage0 | thresh | rising
38 * Sine/cosine inputs exceed DOS mismatch | D4 | altvoltage0 | mag | rising
39 * Tracking error exceeds LOT | D3 | angl1 | thresh | rising
40 * Velocity exceeds maximum tracking rate | D2 | anglvel0 | mag | rising
41 * Phase error exceeds phase lock range | D1 | phase0 | mag | rising
42 * Configuration parity error | D0 | *writes to kernel log*
44 * [3]: The chip does not differentiate between fault on sine vs. cosine so
45 * there will also be an event on the altvoltage2 channel.
48 #include <linux/bitfield.h>
49 #include <linux/bits.h>
50 #include <linux/cleanup.h>
51 #include <linux/clk.h>
52 #include <linux/delay.h>
53 #include <linux/device.h>
54 #include <linux/gpio/consumer.h>
55 #include <linux/module.h>
56 #include <linux/mutex.h>
57 #include <linux/regmap.h>
58 #include <linux/slab.h>
59 #include <linux/spi/spi.h>
60 #include <linux/sysfs.h>
61 #include <linux/types.h>
63 #include <linux/iio/buffer.h>
64 #include <linux/iio/events.h>
65 #include <linux/iio/iio.h>
66 #include <linux/iio/sysfs.h>
67 #include <linux/iio/trigger_consumer.h>
68 #include <linux/iio/triggered_buffer.h>
70 /* control register flags */
71 #define AD2S1210_ADDRESS_DATA BIT(7)
72 #define AD2S1210_PHASE_LOCK_RANGE_44 BIT(5)
73 #define AD2S1210_ENABLE_HYSTERESIS BIT(4)
74 #define AD2S1210_SET_ENRES GENMASK(3, 2)
75 #define AD2S1210_SET_RES GENMASK(1, 0)
77 /* fault register flags */
78 #define AD2S1210_FAULT_CLIP BIT(7)
79 #define AD2S1210_FAULT_LOS BIT(6)
80 #define AD2S1210_FAULT_DOS_OVR BIT(5)
81 #define AD2S1210_FAULT_DOS_MIS BIT(4)
82 #define AD2S1210_FAULT_LOT BIT(3)
83 #define AD2S1210_FAULT_VELOCITY BIT(2)
84 #define AD2S1210_FAULT_PHASE BIT(1)
85 #define AD2S1210_FAULT_CONFIG_PARITY BIT(0)
87 #define AD2S1210_REG_POSITION_MSB 0x80
88 #define AD2S1210_REG_POSITION_LSB 0x81
89 #define AD2S1210_REG_VELOCITY_MSB 0x82
90 #define AD2S1210_REG_VELOCITY_LSB 0x83
91 #define AD2S1210_REG_LOS_THRD 0x88
92 #define AD2S1210_REG_DOS_OVR_THRD 0x89
93 #define AD2S1210_REG_DOS_MIS_THRD 0x8A
94 #define AD2S1210_REG_DOS_RST_MAX_THRD 0x8B
95 #define AD2S1210_REG_DOS_RST_MIN_THRD 0x8C
96 #define AD2S1210_REG_LOT_HIGH_THRD 0x8D
97 #define AD2S1210_REG_LOT_LOW_THRD 0x8E
98 #define AD2S1210_REG_EXCIT_FREQ 0x91
99 #define AD2S1210_REG_CONTROL 0x92
100 #define AD2S1210_REG_SOFT_RESET 0xF0
101 #define AD2S1210_REG_FAULT 0xFF
103 #define AD2S1210_MIN_CLKIN 6144000
104 #define AD2S1210_MAX_CLKIN 10240000
105 #define AD2S1210_MIN_EXCIT 2000
106 #define AD2S1210_DEF_EXCIT 10000
107 #define AD2S1210_MAX_EXCIT 20000
108 #define AD2S1210_MIN_FCW 0x4
109 #define AD2S1210_MAX_FCW 0x50
111 /* 44 degrees ~= 0.767945 radians */
112 #define PHASE_44_DEG_TO_RAD_INT 0
113 #define PHASE_44_DEG_TO_RAD_MICRO 767945
114 /* 360 degrees ~= 6.283185 radians */
115 #define PHASE_360_DEG_TO_RAD_INT 6
116 #define PHASE_360_DEG_TO_RAD_MICRO 283185
118 /* Threshold voltage registers have 1 LSB == 38 mV */
119 #define THRESHOLD_MILLIVOLT_PER_LSB 38
120 /* max voltage for threshold registers is 0x7F * 38 mV */
121 #define THRESHOLD_RANGE_STR "[0 38 4826]"
123 #define FAULT_ONESHOT(bit, new, old) (new & bit && !(old & bit))
132 enum ad2s1210_resolution {
133 AD2S1210_RES_10 = 0b00,
134 AD2S1210_RES_12 = 0b01,
135 AD2S1210_RES_14 = 0b10,
136 AD2S1210_RES_16 = 0b11,
139 struct ad2s1210_state {
141 struct spi_device *sdev;
142 /** GPIO pin connected to SAMPLE line. */
143 struct gpio_desc *sample_gpio;
144 /** GPIO pins connected to A0 and A1 lines (optional). */
145 struct gpio_descs *mode_gpios;
146 /** Used to access config registers. */
147 struct regmap *regmap;
148 /** The external oscillator frequency in Hz. */
149 unsigned long clkin_hz;
150 /** Available raw hysteresis values based on resolution. */
151 int hysteresis_available[2];
152 /* adi,fixed-mode property - only valid when mode_gpios == NULL. */
153 enum ad2s1210_mode fixed_mode;
154 /** The selected resolution */
155 enum ad2s1210_resolution resolution;
156 /** Copy of fault register from the previous read. */
158 /** For reading raw sample value via SPI. */
162 } sample __aligned(IIO_DMA_MINALIGN);
166 /* Ensure timestamp is naturally aligned. */
167 s64 timestamp __aligned(8);
169 /** SPI transmit buffer. */
171 /** SPI receive buffer. */
175 static int ad2s1210_set_mode(struct ad2s1210_state *st, enum ad2s1210_mode mode)
177 struct gpio_descs *gpios = st->mode_gpios;
178 DECLARE_BITMAP(bitmap, 2);
181 return mode == st->fixed_mode ? 0 : -EOPNOTSUPP;
185 return gpiod_set_array_value(gpios->ndescs, gpios->desc, gpios->info,
190 * Writes the given data to the given register address.
192 * If the mode is configurable, the device will first be placed in
193 * configuration mode.
195 static int ad2s1210_regmap_reg_write(void *context, unsigned int reg,
198 struct ad2s1210_state *st = context;
199 struct spi_transfer xfers[] = {
202 .rx_buf = &st->rx[0],
203 .tx_buf = &st->tx[0],
207 .rx_buf = &st->rx[1],
208 .tx_buf = &st->tx[1],
213 /* values can only be 7 bits, the MSB indicates an address */
220 ret = ad2s1210_set_mode(st, MOD_CONFIG);
224 ret = spi_sync_transfer(st->sdev, xfers, ARRAY_SIZE(xfers));
228 /* soft reset also clears the fault register */
229 if (reg == AD2S1210_REG_SOFT_RESET)
230 st->prev_fault_flags = 0;
236 * Reads value from one of the registers.
238 * If the mode is configurable, the device will first be placed in
239 * configuration mode.
241 static int ad2s1210_regmap_reg_read(void *context, unsigned int reg,
244 struct ad2s1210_state *st = context;
245 struct spi_transfer xfers[] = {
248 .rx_buf = &st->rx[0],
249 .tx_buf = &st->tx[0],
253 .rx_buf = &st->rx[1],
254 .tx_buf = &st->tx[1],
259 ret = ad2s1210_set_mode(st, MOD_CONFIG);
265 * Must be valid register address here otherwise this could write data.
266 * It doesn't matter which one as long as reading doesn't have side-
269 st->tx[1] = AD2S1210_REG_CONTROL;
271 ret = spi_sync_transfer(st->sdev, xfers, ARRAY_SIZE(xfers));
275 /* reading the fault register also clears it */
276 if (reg == AD2S1210_REG_FAULT)
277 st->prev_fault_flags = 0;
280 * If the D7 bit is set on any read/write register, it indicates a
281 * parity error. The fault register is read-only and the D7 bit means
282 * something else there.
284 if ((reg > AD2S1210_REG_VELOCITY_LSB && reg != AD2S1210_REG_FAULT)
285 && st->rx[1] & AD2S1210_ADDRESS_DATA)
294 * Toggles the SAMPLE line on the AD2S1210 to latch in the current position,
295 * velocity, and faults.
297 * Must be called with lock held.
299 static void ad2s1210_toggle_sample_line(struct ad2s1210_state *st)
302 * Datasheet specifies minimum hold time t16 = 2 * tck + 20 ns. So the
303 * longest time needed is when CLKIN is 6.144 MHz, in which case t16
304 * ~= 350 ns. The same delay is also needed before re-asserting the
307 gpiod_set_value(st->sample_gpio, 1);
309 gpiod_set_value(st->sample_gpio, 0);
314 * Sets the excitation frequency and performs software reset.
316 * Must be called with lock held.
318 static int ad2s1210_reinit_excitation_frequency(struct ad2s1210_state *st,
321 /* Map resolution to settle time in milliseconds. */
322 static const int track_time_ms[] = { 10, 20, 25, 60 };
323 unsigned int ignored;
327 fcw = fexcit * (1 << 15) / st->clkin_hz;
328 if (fcw < AD2S1210_MIN_FCW || fcw > AD2S1210_MAX_FCW)
331 ret = regmap_write(st->regmap, AD2S1210_REG_EXCIT_FREQ, fcw);
336 * Software reset reinitializes the excitation frequency output.
337 * It does not reset any of the configuration registers.
339 ret = regmap_write(st->regmap, AD2S1210_REG_SOFT_RESET, 0);
344 * Soft reset always triggers some faults due the change in the output
345 * signal so clear the faults too. We need to delay for some time
346 * (what datasheet calls t[track]) to allow things to settle before
347 * clearing the faults.
349 msleep(track_time_ms[st->resolution] * 8192000 / st->clkin_hz);
351 /* Reading the fault register clears the faults. */
352 ret = regmap_read(st->regmap, AD2S1210_REG_FAULT, &ignored);
356 /* Have to toggle sample line to get fault output pins to reset. */
357 ad2s1210_toggle_sample_line(st);
362 static void ad2s1210_push_events(struct iio_dev *indio_dev,
363 u8 flags, s64 timestamp)
365 struct ad2s1210_state *st = iio_priv(indio_dev);
367 /* Sine/cosine inputs clipped */
368 if (FAULT_ONESHOT(AD2S1210_FAULT_CLIP, flags, st->prev_fault_flags)) {
370 * The chip does not differentiate between fault on sine vs.
371 * cosine channel so we just send an event on both channels.
373 iio_push_event(indio_dev,
374 IIO_UNMOD_EVENT_CODE(IIO_ALTVOLTAGE, 1,
378 iio_push_event(indio_dev,
379 IIO_UNMOD_EVENT_CODE(IIO_ALTVOLTAGE, 2,
385 /* Sine/cosine inputs below LOS threshold */
386 if (FAULT_ONESHOT(AD2S1210_FAULT_LOS, flags, st->prev_fault_flags))
387 iio_push_event(indio_dev,
388 IIO_UNMOD_EVENT_CODE(IIO_ALTVOLTAGE, 0,
393 /* Sine/cosine inputs exceed DOS overrange threshold */
394 if (FAULT_ONESHOT(AD2S1210_FAULT_DOS_OVR, flags, st->prev_fault_flags))
395 iio_push_event(indio_dev,
396 IIO_UNMOD_EVENT_CODE(IIO_ALTVOLTAGE, 0,
401 /* Sine/cosine inputs exceed DOS mismatch threshold */
402 if (FAULT_ONESHOT(AD2S1210_FAULT_DOS_MIS, flags, st->prev_fault_flags))
403 iio_push_event(indio_dev,
404 IIO_UNMOD_EVENT_CODE(IIO_ALTVOLTAGE, 0,
409 /* Tracking error exceeds LOT threshold */
410 if (FAULT_ONESHOT(AD2S1210_FAULT_LOT, flags, st->prev_fault_flags))
411 iio_push_event(indio_dev,
412 IIO_UNMOD_EVENT_CODE(IIO_ANGL, 1,
417 /* Velocity exceeds maximum tracking rate */
418 if (FAULT_ONESHOT(AD2S1210_FAULT_VELOCITY, flags, st->prev_fault_flags))
419 iio_push_event(indio_dev,
420 IIO_UNMOD_EVENT_CODE(IIO_ANGL_VEL, 0,
425 /* Phase error exceeds phase lock range */
426 if (FAULT_ONESHOT(AD2S1210_FAULT_PHASE, flags, st->prev_fault_flags))
427 iio_push_event(indio_dev,
428 IIO_UNMOD_EVENT_CODE(IIO_PHASE, 0,
433 /* Configuration parity error */
434 if (FAULT_ONESHOT(AD2S1210_FAULT_CONFIG_PARITY, flags,
435 st->prev_fault_flags))
437 * Userspace should also get notified of this via error return
438 * when trying to write to any attribute that writes a register.
440 dev_err_ratelimited(&indio_dev->dev,
441 "Configuration parity error\n");
443 st->prev_fault_flags = flags;
446 static int ad2s1210_single_conversion(struct iio_dev *indio_dev,
447 struct iio_chan_spec const *chan,
450 struct ad2s1210_state *st = iio_priv(indio_dev);
454 guard(mutex)(&st->lock);
456 ad2s1210_toggle_sample_line(st);
457 timestamp = iio_get_time_ns(indio_dev);
459 if (st->fixed_mode == MOD_CONFIG) {
460 unsigned int reg_val;
462 switch (chan->type) {
464 ret = regmap_bulk_read(st->regmap,
465 AD2S1210_REG_POSITION_MSB,
472 ret = regmap_bulk_read(st->regmap,
473 AD2S1210_REG_VELOCITY_MSB,
483 ret = regmap_read(st->regmap, AD2S1210_REG_FAULT, ®_val);
487 st->sample.fault = reg_val;
489 switch (chan->type) {
491 ret = ad2s1210_set_mode(st, MOD_POS);
494 ret = ad2s1210_set_mode(st, MOD_VEL);
502 ret = spi_read(st->sdev, &st->sample, 3);
507 switch (chan->type) {
509 *val = be16_to_cpu(st->sample.raw);
513 *val = (s16)be16_to_cpu(st->sample.raw);
520 ad2s1210_push_events(indio_dev, st->sample.fault, timestamp);
525 static int ad2s1210_get_hysteresis(struct ad2s1210_state *st, int *val)
529 guard(mutex)(&st->lock);
530 ret = regmap_test_bits(st->regmap, AD2S1210_REG_CONTROL,
531 AD2S1210_ENABLE_HYSTERESIS);
535 *val = ret << (2 * (AD2S1210_RES_16 - st->resolution));
539 static int ad2s1210_set_hysteresis(struct ad2s1210_state *st, int val)
541 guard(mutex)(&st->lock);
542 return regmap_update_bits(st->regmap, AD2S1210_REG_CONTROL,
543 AD2S1210_ENABLE_HYSTERESIS,
544 val ? AD2S1210_ENABLE_HYSTERESIS : 0);
547 static int ad2s1210_get_phase_lock_range(struct ad2s1210_state *st,
552 guard(mutex)(&st->lock);
553 ret = regmap_test_bits(st->regmap, AD2S1210_REG_CONTROL,
554 AD2S1210_PHASE_LOCK_RANGE_44);
559 /* 44 degrees as radians */
560 *val = PHASE_44_DEG_TO_RAD_INT;
561 *val2 = PHASE_44_DEG_TO_RAD_MICRO;
563 /* 360 degrees as radians */
564 *val = PHASE_360_DEG_TO_RAD_INT;
565 *val2 = PHASE_360_DEG_TO_RAD_MICRO;
568 return IIO_VAL_INT_PLUS_MICRO;
571 static int ad2s1210_set_phase_lock_range(struct ad2s1210_state *st,
576 /* convert radians to degrees - only two allowable values */
577 if (val == PHASE_44_DEG_TO_RAD_INT && val2 == PHASE_44_DEG_TO_RAD_MICRO)
579 else if (val == PHASE_360_DEG_TO_RAD_INT &&
580 val2 == PHASE_360_DEG_TO_RAD_MICRO)
585 guard(mutex)(&st->lock);
586 return regmap_update_bits(st->regmap, AD2S1210_REG_CONTROL,
587 AD2S1210_PHASE_LOCK_RANGE_44,
588 deg == 44 ? AD2S1210_PHASE_LOCK_RANGE_44 : 0);
591 /* map resolution to microradians/LSB for LOT registers */
592 static const int ad2s1210_lot_threshold_urad_per_lsb[] = {
593 6184, /* 10-bit: ~0.35 deg/LSB, 45 deg max */
594 2473, /* 12-bit: ~0.14 deg/LSB, 18 deg max */
595 1237, /* 14-bit: ~0.07 deg/LSB, 9 deg max */
596 1237, /* 16-bit: same as 14-bit */
599 static int ad2s1210_get_voltage_threshold(struct ad2s1210_state *st,
600 unsigned int reg, int *val)
602 unsigned int reg_val;
605 guard(mutex)(&st->lock);
606 ret = regmap_read(st->regmap, reg, ®_val);
610 *val = reg_val * THRESHOLD_MILLIVOLT_PER_LSB;
614 static int ad2s1210_set_voltage_threshold(struct ad2s1210_state *st,
615 unsigned int reg, int val)
617 unsigned int reg_val;
619 reg_val = val / THRESHOLD_MILLIVOLT_PER_LSB;
621 guard(mutex)(&st->lock);
622 return regmap_write(st->regmap, reg, reg_val);
625 static int ad2s1210_get_lot_high_threshold(struct ad2s1210_state *st,
628 unsigned int reg_val;
631 guard(mutex)(&st->lock);
632 ret = regmap_read(st->regmap, AD2S1210_REG_LOT_HIGH_THRD, ®_val);
637 *val2 = reg_val * ad2s1210_lot_threshold_urad_per_lsb[st->resolution];
638 return IIO_VAL_INT_PLUS_MICRO;
641 static int ad2s1210_set_lot_high_threshold(struct ad2s1210_state *st,
644 unsigned int high_reg_val, low_reg_val, hysteresis;
647 /* all valid values are between 0 and pi/4 radians */
651 guard(mutex)(&st->lock);
653 * We need to read both high and low registers first so we can preserve
656 ret = regmap_read(st->regmap, AD2S1210_REG_LOT_HIGH_THRD, &high_reg_val);
660 ret = regmap_read(st->regmap, AD2S1210_REG_LOT_LOW_THRD, &low_reg_val);
664 hysteresis = high_reg_val - low_reg_val;
665 high_reg_val = val2 / ad2s1210_lot_threshold_urad_per_lsb[st->resolution];
666 low_reg_val = high_reg_val - hysteresis;
668 ret = regmap_write(st->regmap, AD2S1210_REG_LOT_HIGH_THRD, high_reg_val);
672 return regmap_write(st->regmap, AD2S1210_REG_LOT_LOW_THRD, low_reg_val);
675 static int ad2s1210_get_lot_low_threshold(struct ad2s1210_state *st,
678 unsigned int high_reg_val, low_reg_val;
681 guard(mutex)(&st->lock);
683 ret = regmap_read(st->regmap, AD2S1210_REG_LOT_HIGH_THRD, &high_reg_val);
687 ret = regmap_read(st->regmap, AD2S1210_REG_LOT_LOW_THRD, &low_reg_val);
691 /* sysfs value is hysteresis rather than actual low value */
693 *val2 = (high_reg_val - low_reg_val) *
694 ad2s1210_lot_threshold_urad_per_lsb[st->resolution];
695 return IIO_VAL_INT_PLUS_MICRO;
698 static int ad2s1210_set_lot_low_threshold(struct ad2s1210_state *st,
701 unsigned int reg_val, hysteresis;
704 /* all valid values are between 0 and pi/4 radians */
708 hysteresis = val2 / ad2s1210_lot_threshold_urad_per_lsb[st->resolution];
710 guard(mutex)(&st->lock);
712 ret = regmap_read(st->regmap, AD2S1210_REG_LOT_HIGH_THRD, ®_val);
716 return regmap_write(st->regmap, AD2S1210_REG_LOT_LOW_THRD,
717 reg_val - hysteresis);
720 static int ad2s1210_get_excitation_frequency(struct ad2s1210_state *st, int *val)
722 unsigned int reg_val;
725 guard(mutex)(&st->lock);
727 ret = regmap_read(st->regmap, AD2S1210_REG_EXCIT_FREQ, ®_val);
731 *val = reg_val * st->clkin_hz / (1 << 15);
735 static int ad2s1210_set_excitation_frequency(struct ad2s1210_state *st, int val)
737 if (val < AD2S1210_MIN_EXCIT || val > AD2S1210_MAX_EXCIT)
740 guard(mutex)(&st->lock);
741 return ad2s1210_reinit_excitation_frequency(st, val);
744 static const int ad2s1210_velocity_scale[] = {
745 17089132, /* 8.192MHz / (2*pi * 2500 / 2^15) */
746 42722830, /* 8.192MHz / (2*pi * 1000 / 2^15) */
747 85445659, /* 8.192MHz / (2*pi * 500 / 2^15) */
748 341782638, /* 8.192MHz / (2*pi * 125 / 2^15) */
751 static int ad2s1210_read_raw(struct iio_dev *indio_dev,
752 struct iio_chan_spec const *chan,
757 struct ad2s1210_state *st = iio_priv(indio_dev);
760 case IIO_CHAN_INFO_RAW:
761 return ad2s1210_single_conversion(indio_dev, chan, val);
762 case IIO_CHAN_INFO_SCALE:
763 switch (chan->type) {
765 /* approx 0.3 arc min converted to radians */
768 return IIO_VAL_INT_PLUS_NANO;
771 *val2 = ad2s1210_velocity_scale[st->resolution];
772 return IIO_VAL_FRACTIONAL;
776 case IIO_CHAN_INFO_FREQUENCY:
777 switch (chan->type) {
779 return ad2s1210_get_excitation_frequency(st, val);
783 case IIO_CHAN_INFO_HYSTERESIS:
784 switch (chan->type) {
786 return ad2s1210_get_hysteresis(st, val);
795 static int ad2s1210_read_avail(struct iio_dev *indio_dev,
796 struct iio_chan_spec const *chan,
797 const int **vals, int *type,
798 int *length, long mask)
800 static const int excitation_frequency_available[] = {
806 struct ad2s1210_state *st = iio_priv(indio_dev);
809 case IIO_CHAN_INFO_FREQUENCY:
810 switch (chan->type) {
813 *vals = excitation_frequency_available;
814 return IIO_AVAIL_RANGE;
818 case IIO_CHAN_INFO_HYSTERESIS:
819 switch (chan->type) {
821 *vals = st->hysteresis_available;
823 *length = ARRAY_SIZE(st->hysteresis_available);
824 return IIO_AVAIL_LIST;
833 static int ad2s1210_write_raw(struct iio_dev *indio_dev,
834 struct iio_chan_spec const *chan,
835 int val, int val2, long mask)
837 struct ad2s1210_state *st = iio_priv(indio_dev);
840 case IIO_CHAN_INFO_FREQUENCY:
841 switch (chan->type) {
843 return ad2s1210_set_excitation_frequency(st, val);
847 case IIO_CHAN_INFO_HYSTERESIS:
848 switch (chan->type) {
850 return ad2s1210_set_hysteresis(st, val);
859 static const struct iio_event_spec ad2s1210_position_event_spec[] = {
861 /* Tracking error exceeds LOT threshold fault. */
862 .type = IIO_EV_TYPE_THRESH,
863 .dir = IIO_EV_DIR_RISING,
865 /* Loss of tracking high threshold. */
866 BIT(IIO_EV_INFO_VALUE) |
867 /* Loss of tracking low threshold. */
868 BIT(IIO_EV_INFO_HYSTERESIS),
872 static const struct iio_event_spec ad2s1210_velocity_event_spec[] = {
874 /* Velocity exceeds maximum tracking rate fault. */
875 .type = IIO_EV_TYPE_MAG,
876 .dir = IIO_EV_DIR_RISING,
880 static const struct iio_event_spec ad2s1210_phase_event_spec[] = {
882 /* Phase error fault. */
883 .type = IIO_EV_TYPE_MAG,
884 .dir = IIO_EV_DIR_RISING,
885 /* Phase lock range. */
886 .mask_separate = BIT(IIO_EV_INFO_VALUE),
890 static const struct iio_event_spec ad2s1210_monitor_signal_event_spec[] = {
892 /* Sine/cosine below LOS threshold fault. */
893 .type = IIO_EV_TYPE_THRESH,
894 .dir = IIO_EV_DIR_FALLING,
895 /* Loss of signal threshold. */
896 .mask_separate = BIT(IIO_EV_INFO_VALUE),
899 /* Sine/cosine DOS overrange fault.*/
900 .type = IIO_EV_TYPE_THRESH,
901 .dir = IIO_EV_DIR_RISING,
902 /* Degredation of signal overrange threshold. */
903 .mask_separate = BIT(IIO_EV_INFO_VALUE),
906 /* Sine/cosine DOS mismatch fault.*/
907 .type = IIO_EV_TYPE_MAG,
908 .dir = IIO_EV_DIR_RISING,
909 .mask_separate = BIT(IIO_EV_INFO_VALUE),
913 static const struct iio_event_spec ad2s1210_sin_cos_event_spec[] = {
915 /* Sine/cosine clipping fault. */
916 .type = IIO_EV_TYPE_MAG,
917 .dir = IIO_EV_DIR_EITHER,
921 static const struct iio_chan_spec ad2s1210_channels[] = {
931 .endianness = IIO_BE,
933 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
934 BIT(IIO_CHAN_INFO_SCALE) |
935 BIT(IIO_CHAN_INFO_HYSTERESIS),
936 .info_mask_separate_available =
937 BIT(IIO_CHAN_INFO_HYSTERESIS),
939 .type = IIO_ANGL_VEL,
947 .endianness = IIO_BE,
949 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
950 BIT(IIO_CHAN_INFO_SCALE),
951 .event_spec = ad2s1210_velocity_event_spec,
952 .num_event_specs = ARRAY_SIZE(ad2s1210_velocity_event_spec),
954 IIO_CHAN_SOFT_TIMESTAMP(2),
956 /* used to configure LOT thresholds and get tracking error */
961 .event_spec = ad2s1210_position_event_spec,
962 .num_event_specs = ARRAY_SIZE(ad2s1210_position_event_spec),
965 /* used to configure phase lock range and get phase lock error */
970 .event_spec = ad2s1210_phase_event_spec,
971 .num_event_specs = ARRAY_SIZE(ad2s1210_phase_event_spec),
973 /* excitation frequency output */
974 .type = IIO_ALTVOLTAGE,
979 .info_mask_separate = BIT(IIO_CHAN_INFO_FREQUENCY),
980 .info_mask_separate_available = BIT(IIO_CHAN_INFO_FREQUENCY),
983 .type = IIO_ALTVOLTAGE,
987 .event_spec = ad2s1210_monitor_signal_event_spec,
988 .num_event_specs = ARRAY_SIZE(ad2s1210_monitor_signal_event_spec),
991 .type = IIO_ALTVOLTAGE,
995 .event_spec = ad2s1210_sin_cos_event_spec,
996 .num_event_specs = ARRAY_SIZE(ad2s1210_sin_cos_event_spec),
999 .type = IIO_ALTVOLTAGE,
1003 .event_spec = ad2s1210_sin_cos_event_spec,
1004 .num_event_specs = ARRAY_SIZE(ad2s1210_sin_cos_event_spec),
1008 static ssize_t event_attr_voltage_reg_show(struct device *dev,
1009 struct device_attribute *attr,
1012 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
1013 struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
1017 guard(mutex)(&st->lock);
1018 ret = regmap_read(st->regmap, iattr->address, &value);
1022 return sprintf(buf, "%d\n", value * THRESHOLD_MILLIVOLT_PER_LSB);
1025 static ssize_t event_attr_voltage_reg_store(struct device *dev,
1026 struct device_attribute *attr,
1027 const char *buf, size_t len)
1029 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
1030 struct iio_dev_attr *iattr = to_iio_dev_attr(attr);
1034 ret = kstrtou16(buf, 10, &data);
1038 guard(mutex)(&st->lock);
1039 ret = regmap_write(st->regmap, iattr->address,
1040 data / THRESHOLD_MILLIVOLT_PER_LSB);
1048 in_angl1_thresh_rising_value_available_show(struct device *dev,
1049 struct device_attribute *attr,
1052 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
1053 int step = ad2s1210_lot_threshold_urad_per_lsb[st->resolution];
1055 return sysfs_emit(buf, "[0 0.%06d 0.%06d]\n", step, step * 0x7F);
1059 in_angl1_thresh_rising_hysteresis_available_show(struct device *dev,
1060 struct device_attribute *attr,
1063 struct ad2s1210_state *st = iio_priv(dev_to_iio_dev(dev));
1064 int step = ad2s1210_lot_threshold_urad_per_lsb[st->resolution];
1066 return sysfs_emit(buf, "[0 0.%06d 0.%06d]\n", step, step * 0x7F);
1069 static IIO_CONST_ATTR(in_phase0_mag_rising_value_available,
1070 __stringify(PHASE_44_DEG_TO_RAD_INT) "."
1071 __stringify(PHASE_44_DEG_TO_RAD_MICRO) " "
1072 __stringify(PHASE_360_DEG_TO_RAD_INT) "."
1073 __stringify(PHASE_360_DEG_TO_RAD_MICRO));
1074 static IIO_CONST_ATTR(in_altvoltage0_thresh_falling_value_available,
1075 THRESHOLD_RANGE_STR);
1076 static IIO_CONST_ATTR(in_altvoltage0_thresh_rising_value_available,
1077 THRESHOLD_RANGE_STR);
1078 static IIO_CONST_ATTR(in_altvoltage0_mag_rising_value_available,
1079 THRESHOLD_RANGE_STR);
1080 static IIO_DEVICE_ATTR(in_altvoltage0_mag_rising_reset_max, 0644,
1081 event_attr_voltage_reg_show, event_attr_voltage_reg_store,
1082 AD2S1210_REG_DOS_RST_MAX_THRD);
1083 static IIO_CONST_ATTR(in_altvoltage0_mag_rising_reset_max_available, THRESHOLD_RANGE_STR);
1084 static IIO_DEVICE_ATTR(in_altvoltage0_mag_rising_reset_min, 0644,
1085 event_attr_voltage_reg_show, event_attr_voltage_reg_store,
1086 AD2S1210_REG_DOS_RST_MIN_THRD);
1087 static IIO_CONST_ATTR(in_altvoltage0_mag_rising_reset_min_available, THRESHOLD_RANGE_STR);
1088 static IIO_DEVICE_ATTR_RO(in_angl1_thresh_rising_value_available, 0);
1089 static IIO_DEVICE_ATTR_RO(in_angl1_thresh_rising_hysteresis_available, 0);
1091 static struct attribute *ad2s1210_event_attributes[] = {
1092 &iio_const_attr_in_phase0_mag_rising_value_available.dev_attr.attr,
1093 &iio_const_attr_in_altvoltage0_thresh_falling_value_available.dev_attr.attr,
1094 &iio_const_attr_in_altvoltage0_thresh_rising_value_available.dev_attr.attr,
1095 &iio_const_attr_in_altvoltage0_mag_rising_value_available.dev_attr.attr,
1096 &iio_dev_attr_in_altvoltage0_mag_rising_reset_max.dev_attr.attr,
1097 &iio_const_attr_in_altvoltage0_mag_rising_reset_max_available.dev_attr.attr,
1098 &iio_dev_attr_in_altvoltage0_mag_rising_reset_min.dev_attr.attr,
1099 &iio_const_attr_in_altvoltage0_mag_rising_reset_min_available.dev_attr.attr,
1100 &iio_dev_attr_in_angl1_thresh_rising_value_available.dev_attr.attr,
1101 &iio_dev_attr_in_angl1_thresh_rising_hysteresis_available.dev_attr.attr,
1105 static const struct attribute_group ad2s1210_event_attribute_group = {
1106 .attrs = ad2s1210_event_attributes,
1109 static int ad2s1210_initial(struct ad2s1210_state *st)
1114 guard(mutex)(&st->lock);
1116 /* Use default config register value plus resolution from devicetree. */
1117 data = FIELD_PREP(AD2S1210_PHASE_LOCK_RANGE_44, 1);
1118 data |= FIELD_PREP(AD2S1210_ENABLE_HYSTERESIS, 1);
1119 data |= FIELD_PREP(AD2S1210_SET_ENRES, 0x3);
1120 data |= FIELD_PREP(AD2S1210_SET_RES, st->resolution);
1122 ret = regmap_write(st->regmap, AD2S1210_REG_CONTROL, data);
1126 return ad2s1210_reinit_excitation_frequency(st, AD2S1210_DEF_EXCIT);
1129 static int ad2s1210_read_label(struct iio_dev *indio_dev,
1130 struct iio_chan_spec const *chan,
1133 if (chan->type == IIO_ANGL) {
1134 if (chan->channel == 0)
1135 return sprintf(label, "position\n");
1136 if (chan->channel == 1)
1137 return sprintf(label, "tracking error\n");
1139 if (chan->type == IIO_ANGL_VEL)
1140 return sprintf(label, "velocity\n");
1141 if (chan->type == IIO_PHASE)
1142 return sprintf(label, "synthetic reference\n");
1143 if (chan->type == IIO_ALTVOLTAGE) {
1145 return sprintf(label, "excitation\n");
1146 if (chan->channel == 0)
1147 return sprintf(label, "monitor signal\n");
1148 if (chan->channel == 1)
1149 return sprintf(label, "cosine\n");
1150 if (chan->channel == 2)
1151 return sprintf(label, "sine\n");
1157 static int ad2s1210_read_event_value(struct iio_dev *indio_dev,
1158 const struct iio_chan_spec *chan,
1159 enum iio_event_type type,
1160 enum iio_event_direction dir,
1161 enum iio_event_info info,
1162 int *val, int *val2)
1164 struct ad2s1210_state *st = iio_priv(indio_dev);
1166 switch (chan->type) {
1169 case IIO_EV_INFO_VALUE:
1170 return ad2s1210_get_lot_high_threshold(st, val, val2);
1171 case IIO_EV_INFO_HYSTERESIS:
1172 return ad2s1210_get_lot_low_threshold(st, val, val2);
1176 case IIO_ALTVOLTAGE:
1179 if (type == IIO_EV_TYPE_THRESH && dir == IIO_EV_DIR_FALLING)
1180 return ad2s1210_get_voltage_threshold(st,
1181 AD2S1210_REG_LOS_THRD, val);
1182 if (type == IIO_EV_TYPE_THRESH && dir == IIO_EV_DIR_RISING)
1183 return ad2s1210_get_voltage_threshold(st,
1184 AD2S1210_REG_DOS_OVR_THRD, val);
1185 if (type == IIO_EV_TYPE_MAG)
1186 return ad2s1210_get_voltage_threshold(st,
1187 AD2S1210_REG_DOS_MIS_THRD, val);
1190 return ad2s1210_get_phase_lock_range(st, val, val2);
1196 static int ad2s1210_write_event_value(struct iio_dev *indio_dev,
1197 const struct iio_chan_spec *chan,
1198 enum iio_event_type type,
1199 enum iio_event_direction dir,
1200 enum iio_event_info info,
1203 struct ad2s1210_state *st = iio_priv(indio_dev);
1205 switch (chan->type) {
1208 case IIO_EV_INFO_VALUE:
1209 return ad2s1210_set_lot_high_threshold(st, val, val2);
1210 case IIO_EV_INFO_HYSTERESIS:
1211 return ad2s1210_set_lot_low_threshold(st, val, val2);
1215 case IIO_ALTVOLTAGE:
1218 if (type == IIO_EV_TYPE_THRESH && dir == IIO_EV_DIR_FALLING)
1219 return ad2s1210_set_voltage_threshold(st,
1220 AD2S1210_REG_LOS_THRD, val);
1221 if (type == IIO_EV_TYPE_THRESH && dir == IIO_EV_DIR_RISING)
1222 return ad2s1210_set_voltage_threshold(st,
1223 AD2S1210_REG_DOS_OVR_THRD, val);
1224 if (type == IIO_EV_TYPE_MAG)
1225 return ad2s1210_set_voltage_threshold(st,
1226 AD2S1210_REG_DOS_MIS_THRD, val);
1229 return ad2s1210_set_phase_lock_range(st, val, val2);
1235 static int ad2s1210_read_event_label(struct iio_dev *indio_dev,
1236 struct iio_chan_spec const *chan,
1237 enum iio_event_type type,
1238 enum iio_event_direction dir,
1241 if (chan->type == IIO_ANGL)
1242 return sprintf(label, "LOT\n");
1243 if (chan->type == IIO_ANGL_VEL)
1244 return sprintf(label, "max tracking rate\n");
1245 if (chan->type == IIO_PHASE)
1246 return sprintf(label, "phase lock\n");
1247 if (chan->type == IIO_ALTVOLTAGE) {
1248 if (chan->channel == 0) {
1249 if (type == IIO_EV_TYPE_THRESH &&
1250 dir == IIO_EV_DIR_FALLING)
1251 return sprintf(label, "LOS\n");
1252 if (type == IIO_EV_TYPE_THRESH &&
1253 dir == IIO_EV_DIR_RISING)
1254 return sprintf(label, "DOS overrange\n");
1255 if (type == IIO_EV_TYPE_MAG)
1256 return sprintf(label, "DOS mismatch\n");
1258 if (chan->channel == 1 || chan->channel == 2)
1259 return sprintf(label, "clipped\n");
1265 static int ad2s1210_debugfs_reg_access(struct iio_dev *indio_dev,
1266 unsigned int reg, unsigned int writeval,
1267 unsigned int *readval)
1269 struct ad2s1210_state *st = iio_priv(indio_dev);
1271 guard(mutex)(&st->lock);
1274 return regmap_read(st->regmap, reg, readval);
1276 return regmap_write(st->regmap, reg, writeval);
1279 static irqreturn_t ad2s1210_trigger_handler(int irq, void *p)
1281 struct iio_poll_func *pf = p;
1282 struct iio_dev *indio_dev = pf->indio_dev;
1283 struct ad2s1210_state *st = iio_priv(indio_dev);
1287 guard(mutex)(&st->lock);
1289 memset(&st->scan, 0, sizeof(st->scan));
1290 ad2s1210_toggle_sample_line(st);
1292 if (test_bit(0, indio_dev->active_scan_mask)) {
1293 if (st->fixed_mode == MOD_CONFIG) {
1294 ret = regmap_bulk_read(st->regmap,
1295 AD2S1210_REG_POSITION_MSB,
1296 &st->sample.raw, 2);
1300 ret = ad2s1210_set_mode(st, MOD_POS);
1304 ret = spi_read(st->sdev, &st->sample, 3);
1309 memcpy(&st->scan.chan[chan++], &st->sample.raw, 2);
1312 if (test_bit(1, indio_dev->active_scan_mask)) {
1313 if (st->fixed_mode == MOD_CONFIG) {
1314 ret = regmap_bulk_read(st->regmap,
1315 AD2S1210_REG_VELOCITY_MSB,
1316 &st->sample.raw, 2);
1320 ret = ad2s1210_set_mode(st, MOD_VEL);
1324 ret = spi_read(st->sdev, &st->sample, 3);
1329 memcpy(&st->scan.chan[chan++], &st->sample.raw, 2);
1332 if (st->fixed_mode == MOD_CONFIG) {
1333 unsigned int reg_val;
1335 ret = regmap_read(st->regmap, AD2S1210_REG_FAULT, ®_val);
1339 st->sample.fault = reg_val;
1342 ad2s1210_push_events(indio_dev, st->sample.fault, pf->timestamp);
1343 iio_push_to_buffers_with_timestamp(indio_dev, &st->scan, pf->timestamp);
1346 iio_trigger_notify_done(indio_dev->trig);
1351 static const struct iio_info ad2s1210_info = {
1352 .event_attrs = &ad2s1210_event_attribute_group,
1353 .read_raw = ad2s1210_read_raw,
1354 .read_avail = ad2s1210_read_avail,
1355 .write_raw = ad2s1210_write_raw,
1356 .read_label = ad2s1210_read_label,
1357 .read_event_value = ad2s1210_read_event_value,
1358 .write_event_value = ad2s1210_write_event_value,
1359 .read_event_label = ad2s1210_read_event_label,
1360 .debugfs_reg_access = &ad2s1210_debugfs_reg_access,
1363 static int ad2s1210_setup_properties(struct ad2s1210_state *st)
1365 struct device *dev = &st->sdev->dev;
1366 const char *str_val;
1370 ret = device_property_read_string(dev, "adi,fixed-mode", &str_val);
1372 st->fixed_mode = -1;
1374 return dev_err_probe(dev, ret,
1375 "failed to read adi,fixed-mode property\n");
1377 if (strcmp(str_val, "config"))
1378 return dev_err_probe(dev, -EINVAL,
1379 "only adi,fixed-mode=\"config\" is supported\n");
1381 st->fixed_mode = MOD_CONFIG;
1384 ret = device_property_read_u32(dev, "assigned-resolution-bits", &val);
1386 return dev_err_probe(dev, ret,
1387 "failed to read assigned-resolution-bits property\n");
1389 if (val < 10 || val > 16)
1390 return dev_err_probe(dev, -EINVAL,
1391 "resolution out of range: %u\n", val);
1393 st->resolution = (val - 10) >> 1;
1395 * These are values that correlate to the hysteresis bit in the Control
1396 * register. 0 = disabled, 1 = enabled. When enabled, the actual
1397 * hysteresis is +/- 1 LSB of the raw position value. Which bit is the
1398 * LSB depends on the specified resolution.
1400 st->hysteresis_available[0] = 0;
1401 st->hysteresis_available[1] = 1 << (2 * (AD2S1210_RES_16 -
1407 static int ad2s1210_setup_clocks(struct ad2s1210_state *st)
1409 struct device *dev = &st->sdev->dev;
1412 clk = devm_clk_get_enabled(dev, NULL);
1414 return dev_err_probe(dev, PTR_ERR(clk), "failed to get clock\n");
1416 st->clkin_hz = clk_get_rate(clk);
1417 if (st->clkin_hz < AD2S1210_MIN_CLKIN || st->clkin_hz > AD2S1210_MAX_CLKIN)
1418 return dev_err_probe(dev, -EINVAL,
1419 "clock frequency out of range: %lu\n",
1425 static int ad2s1210_setup_gpios(struct ad2s1210_state *st)
1427 struct device *dev = &st->sdev->dev;
1428 struct gpio_descs *resolution_gpios;
1429 struct gpio_desc *reset_gpio;
1430 DECLARE_BITMAP(bitmap, 2);
1433 /* should not be sampling on startup */
1434 st->sample_gpio = devm_gpiod_get(dev, "sample", GPIOD_OUT_LOW);
1435 if (IS_ERR(st->sample_gpio))
1436 return dev_err_probe(dev, PTR_ERR(st->sample_gpio),
1437 "failed to request sample GPIO\n");
1439 /* both pins high means that we start in config mode */
1440 st->mode_gpios = devm_gpiod_get_array_optional(dev, "mode",
1442 if (IS_ERR(st->mode_gpios))
1443 return dev_err_probe(dev, PTR_ERR(st->mode_gpios),
1444 "failed to request mode GPIOs\n");
1446 if (!st->mode_gpios && st->fixed_mode == -1)
1447 return dev_err_probe(dev, -EINVAL,
1448 "must specify either adi,fixed-mode or mode-gpios\n");
1450 if (st->mode_gpios && st->fixed_mode != -1)
1451 return dev_err_probe(dev, -EINVAL,
1452 "must specify only one of adi,fixed-mode or mode-gpios\n");
1454 if (st->mode_gpios && st->mode_gpios->ndescs != 2)
1455 return dev_err_probe(dev, -EINVAL,
1456 "requires exactly 2 mode-gpios\n");
1459 * If resolution gpios are provided, they get set to the required
1460 * resolution, otherwise it is assumed the RES0 and RES1 pins are
1461 * hard-wired to match the resolution indicated in the devicetree.
1463 resolution_gpios = devm_gpiod_get_array_optional(dev, "resolution",
1465 if (IS_ERR(resolution_gpios))
1466 return dev_err_probe(dev, PTR_ERR(resolution_gpios),
1467 "failed to request resolution GPIOs\n");
1469 if (resolution_gpios) {
1470 if (resolution_gpios->ndescs != 2)
1471 return dev_err_probe(dev, -EINVAL,
1472 "requires exactly 2 resolution-gpios\n");
1474 bitmap[0] = st->resolution;
1476 ret = gpiod_set_array_value(resolution_gpios->ndescs,
1477 resolution_gpios->desc,
1478 resolution_gpios->info,
1481 return dev_err_probe(dev, ret,
1482 "failed to set resolution gpios\n");
1485 /* If the optional reset GPIO is present, toggle it to do a hard reset. */
1486 reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1487 if (IS_ERR(reset_gpio))
1488 return dev_err_probe(dev, PTR_ERR(reset_gpio),
1489 "failed to request reset GPIO\n");
1493 gpiod_set_value(reset_gpio, 0);
1499 static const struct regmap_range ad2s1210_regmap_readable_ranges[] = {
1500 regmap_reg_range(AD2S1210_REG_POSITION_MSB, AD2S1210_REG_VELOCITY_LSB),
1501 regmap_reg_range(AD2S1210_REG_LOS_THRD, AD2S1210_REG_LOT_LOW_THRD),
1502 regmap_reg_range(AD2S1210_REG_EXCIT_FREQ, AD2S1210_REG_CONTROL),
1503 regmap_reg_range(AD2S1210_REG_FAULT, AD2S1210_REG_FAULT),
1506 static const struct regmap_access_table ad2s1210_regmap_rd_table = {
1507 .yes_ranges = ad2s1210_regmap_readable_ranges,
1508 .n_yes_ranges = ARRAY_SIZE(ad2s1210_regmap_readable_ranges),
1511 static const struct regmap_range ad2s1210_regmap_writeable_ranges[] = {
1512 regmap_reg_range(AD2S1210_REG_LOS_THRD, AD2S1210_REG_LOT_LOW_THRD),
1513 regmap_reg_range(AD2S1210_REG_EXCIT_FREQ, AD2S1210_REG_CONTROL),
1514 regmap_reg_range(AD2S1210_REG_SOFT_RESET, AD2S1210_REG_SOFT_RESET),
1515 regmap_reg_range(AD2S1210_REG_FAULT, AD2S1210_REG_FAULT),
1518 static const struct regmap_access_table ad2s1210_regmap_wr_table = {
1519 .yes_ranges = ad2s1210_regmap_writeable_ranges,
1520 .n_yes_ranges = ARRAY_SIZE(ad2s1210_regmap_writeable_ranges),
1523 static int ad2s1210_setup_regmap(struct ad2s1210_state *st)
1525 struct device *dev = &st->sdev->dev;
1526 const struct regmap_config config = {
1529 .disable_locking = true,
1530 .reg_read = ad2s1210_regmap_reg_read,
1531 .reg_write = ad2s1210_regmap_reg_write,
1532 .rd_table = &ad2s1210_regmap_rd_table,
1533 .wr_table = &ad2s1210_regmap_wr_table,
1537 st->regmap = devm_regmap_init(dev, NULL, st, &config);
1538 if (IS_ERR(st->regmap))
1539 return dev_err_probe(dev, PTR_ERR(st->regmap),
1540 "failed to allocate register map\n");
1545 static int ad2s1210_probe(struct spi_device *spi)
1547 struct iio_dev *indio_dev;
1548 struct ad2s1210_state *st;
1551 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1554 st = iio_priv(indio_dev);
1556 mutex_init(&st->lock);
1559 ret = ad2s1210_setup_properties(st);
1563 ret = ad2s1210_setup_clocks(st);
1567 ret = ad2s1210_setup_gpios(st);
1571 ret = ad2s1210_setup_regmap(st);
1575 ret = ad2s1210_initial(st);
1579 indio_dev->info = &ad2s1210_info;
1580 indio_dev->modes = INDIO_DIRECT_MODE;
1581 indio_dev->channels = ad2s1210_channels;
1582 indio_dev->num_channels = ARRAY_SIZE(ad2s1210_channels);
1583 indio_dev->name = spi_get_device_id(spi)->name;
1585 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev,
1586 &iio_pollfunc_store_time,
1587 &ad2s1210_trigger_handler, NULL);
1589 return dev_err_probe(&spi->dev, ret,
1590 "iio triggered buffer setup failed\n");
1592 return devm_iio_device_register(&spi->dev, indio_dev);
1595 static const struct of_device_id ad2s1210_of_match[] = {
1596 { .compatible = "adi,ad2s1210", },
1599 MODULE_DEVICE_TABLE(of, ad2s1210_of_match);
1601 static const struct spi_device_id ad2s1210_id[] = {
1605 MODULE_DEVICE_TABLE(spi, ad2s1210_id);
1607 static struct spi_driver ad2s1210_driver = {
1610 .of_match_table = ad2s1210_of_match,
1612 .probe = ad2s1210_probe,
1613 .id_table = ad2s1210_id,
1615 module_spi_driver(ad2s1210_driver);
1617 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
1618 MODULE_DESCRIPTION("Analog Devices AD2S1210 Resolver to Digital SPI driver");
1619 MODULE_LICENSE("GPL v2");