GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / staging / iio / resolver / ad2s90.c
1 /*
2  * ad2s90.c simple support for the ADI Resolver to Digital Converters: AD2S90
3  *
4  * Copyright (c) 2010-2010 Analog Devices Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11 #include <linux/types.h>
12 #include <linux/mutex.h>
13 #include <linux/device.h>
14 #include <linux/spi/spi.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/module.h>
18
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21
22 struct ad2s90_state {
23         struct mutex lock;
24         struct spi_device *sdev;
25         u8 rx[2] ____cacheline_aligned;
26 };
27
28 static int ad2s90_read_raw(struct iio_dev *indio_dev,
29                            struct iio_chan_spec const *chan,
30                            int *val,
31                            int *val2,
32                            long m)
33 {
34         int ret;
35         struct ad2s90_state *st = iio_priv(indio_dev);
36
37         mutex_lock(&st->lock);
38         ret = spi_read(st->sdev, st->rx, 2);
39         if (ret)
40                 goto error_ret;
41         *val = (((u16)(st->rx[0])) << 4) | ((st->rx[1] & 0xF0) >> 4);
42
43 error_ret:
44         mutex_unlock(&st->lock);
45
46         return IIO_VAL_INT;
47 }
48
49 static const struct iio_info ad2s90_info = {
50         .read_raw = ad2s90_read_raw,
51 };
52
53 static const struct iio_chan_spec ad2s90_chan = {
54         .type = IIO_ANGL,
55         .indexed = 1,
56         .channel = 0,
57         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
58 };
59
60 static int ad2s90_probe(struct spi_device *spi)
61 {
62         struct iio_dev *indio_dev;
63         struct ad2s90_state *st;
64         int ret = 0;
65
66         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
67         if (!indio_dev)
68                 return -ENOMEM;
69         st = iio_priv(indio_dev);
70         spi_set_drvdata(spi, indio_dev);
71
72         mutex_init(&st->lock);
73         st->sdev = spi;
74         indio_dev->dev.parent = &spi->dev;
75         indio_dev->info = &ad2s90_info;
76         indio_dev->modes = INDIO_DIRECT_MODE;
77         indio_dev->channels = &ad2s90_chan;
78         indio_dev->num_channels = 1;
79         indio_dev->name = spi_get_device_id(spi)->name;
80
81         ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
82         if (ret)
83                 return ret;
84
85         /* need 600ns between CS and the first falling edge of SCLK */
86         spi->max_speed_hz = 830000;
87         spi->mode = SPI_MODE_3;
88         ret = spi_setup(spi);
89
90         if (ret < 0) {
91                 dev_err(&spi->dev, "spi_setup failed!\n");
92                 return ret;
93         }
94
95         return 0;
96 }
97
98 static const struct spi_device_id ad2s90_id[] = {
99         { "ad2s90" },
100         {}
101 };
102 MODULE_DEVICE_TABLE(spi, ad2s90_id);
103
104 static struct spi_driver ad2s90_driver = {
105         .driver = {
106                 .name = "ad2s90",
107         },
108         .probe = ad2s90_probe,
109         .id_table = ad2s90_id,
110 };
111 module_spi_driver(ad2s90_driver);
112
113 MODULE_AUTHOR("Graff Yang <graff.yang@gmail.com>");
114 MODULE_DESCRIPTION("Analog Devices AD2S90 Resolver to Digital SPI driver");
115 MODULE_LICENSE("GPL v2");