1 // SPDX-License-Identifier: GPL-2.0
3 * ad2s90.c simple support for the ADI Resolver to Digital Converters: AD2S90
5 * Copyright (c) 2010-2010 Analog Devices Inc.
7 #include <linux/types.h>
8 #include <linux/mutex.h>
9 #include <linux/device.h>
10 #include <linux/spi/spi.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/module.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
19 * Although chip's max frequency is 2Mhz, it needs 600ns between CS and the
20 * first falling edge of SCLK, so frequency should be at most 1 / (2 * 6e-7)
22 #define AD2S90_MAX_SPI_FREQ_HZ 830000
25 struct mutex lock
; /* lock to protect rx buffer */
26 struct spi_device
*sdev
;
27 u8 rx
[2] ____cacheline_aligned
;
30 static int ad2s90_read_raw(struct iio_dev
*indio_dev
,
31 struct iio_chan_spec
const *chan
,
37 struct ad2s90_state
*st
= iio_priv(indio_dev
);
39 if (chan
->type
!= IIO_ANGL
)
43 case IIO_CHAN_INFO_SCALE
:
47 return IIO_VAL_FRACTIONAL_LOG2
;
48 case IIO_CHAN_INFO_RAW
:
49 mutex_lock(&st
->lock
);
50 ret
= spi_read(st
->sdev
, st
->rx
, 2);
52 mutex_unlock(&st
->lock
);
55 *val
= (((u16
)(st
->rx
[0])) << 4) | ((st
->rx
[1] & 0xF0) >> 4);
57 mutex_unlock(&st
->lock
);
67 static const struct iio_info ad2s90_info
= {
68 .read_raw
= ad2s90_read_raw
,
71 static const struct iio_chan_spec ad2s90_chan
= {
75 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
78 static int ad2s90_probe(struct spi_device
*spi
)
80 struct iio_dev
*indio_dev
;
81 struct ad2s90_state
*st
;
83 if (spi
->max_speed_hz
> AD2S90_MAX_SPI_FREQ_HZ
) {
84 dev_err(&spi
->dev
, "SPI CLK, %d Hz exceeds %d Hz\n",
85 spi
->max_speed_hz
, AD2S90_MAX_SPI_FREQ_HZ
);
89 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
92 st
= iio_priv(indio_dev
);
93 spi_set_drvdata(spi
, indio_dev
);
95 mutex_init(&st
->lock
);
97 indio_dev
->info
= &ad2s90_info
;
98 indio_dev
->modes
= INDIO_DIRECT_MODE
;
99 indio_dev
->channels
= &ad2s90_chan
;
100 indio_dev
->num_channels
= 1;
101 indio_dev
->name
= spi_get_device_id(spi
)->name
;
103 return devm_iio_device_register(indio_dev
->dev
.parent
, indio_dev
);
106 static const struct of_device_id ad2s90_of_match
[] = {
107 { .compatible
= "adi,ad2s90", },
110 MODULE_DEVICE_TABLE(of
, ad2s90_of_match
);
112 static const struct spi_device_id ad2s90_id
[] = {
116 MODULE_DEVICE_TABLE(spi
, ad2s90_id
);
118 static struct spi_driver ad2s90_driver
= {
121 .of_match_table
= ad2s90_of_match
,
123 .probe
= ad2s90_probe
,
124 .id_table
= ad2s90_id
,
126 module_spi_driver(ad2s90_driver
);
128 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
129 MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
130 MODULE_LICENSE("GPL v2");