1// SPDX-License-Identifier: GPL-2.0
2/*
3 * ad2s90.c simple support for the ADI Resolver to Digital Converters: AD2S90
4 *
5 * Copyright (c) 2010-2010 Analog Devices Inc.
6 */
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>
14
15#include <linux/iio/iio.h>
16#include <linux/iio/sysfs.h>
17
18/*
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)
21 */
22#define AD2S90_MAX_SPI_FREQ_HZ  830000
23
24struct ad2s90_state {
25	struct mutex lock; /* lock to protect rx buffer */
26	struct spi_device *sdev;
27	u8 rx[2] __aligned(IIO_DMA_MINALIGN);
28};
29
30static int ad2s90_read_raw(struct iio_dev *indio_dev,
31			   struct iio_chan_spec const *chan,
32			   int *val,
33			   int *val2,
34			   long m)
35{
36	int ret;
37	struct ad2s90_state *st = iio_priv(indio_dev);
38
39	if (chan->type != IIO_ANGL)
40		return -EINVAL;
41
42	switch (m) {
43	case IIO_CHAN_INFO_SCALE:
44		/* 2 * Pi / 2^12 */
45		*val = 6283; /* mV */
46		*val2 = 12;
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);
51		if (ret < 0) {
52			mutex_unlock(&st->lock);
53			return ret;
54		}
55		*val = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
56
57		mutex_unlock(&st->lock);
58
59		return IIO_VAL_INT;
60	default:
61		break;
62	}
63
64	return -EINVAL;
65}
66
67static const struct iio_info ad2s90_info = {
68	.read_raw = ad2s90_read_raw,
69};
70
71static const struct iio_chan_spec ad2s90_chan = {
72	.type = IIO_ANGL,
73	.indexed = 1,
74	.channel = 0,
75	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
76};
77
78static int ad2s90_probe(struct spi_device *spi)
79{
80	struct iio_dev *indio_dev;
81	struct ad2s90_state *st;
82
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);
86		return -EINVAL;
87	}
88
89	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
90	if (!indio_dev)
91		return -ENOMEM;
92	st = iio_priv(indio_dev);
93	spi_set_drvdata(spi, indio_dev);
94
95	mutex_init(&st->lock);
96	st->sdev = spi;
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;
102
103	return devm_iio_device_register(indio_dev->dev.parent, indio_dev);
104}
105
106static const struct of_device_id ad2s90_of_match[] = {
107	{ .compatible = "adi,ad2s90", },
108	{}
109};
110MODULE_DEVICE_TABLE(of, ad2s90_of_match);
111
112static const struct spi_device_id ad2s90_id[] = {
113	{ "ad2s90" },
114	{}
115};
116MODULE_DEVICE_TABLE(spi, ad2s90_id);
117
118static struct spi_driver ad2s90_driver = {
119	.driver = {
120		.name = "ad2s90",
121		.of_match_table = ad2s90_of_match,
122	},
123	.probe = ad2s90_probe,
124	.id_table = ad2s90_id,
125};
126module_spi_driver(ad2s90_driver);
127
128MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
129MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
130MODULE_LICENSE("GPL v2");
131