Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-microblaze.git] / drivers / iio / adc / ti-ads8344.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ADS8344 16-bit 8-Channel ADC driver
4  *
5  * Author: Gregory CLEMENT <gregory.clement@bootlin.com>
6  *
7  * Datasheet: http://www.ti.com/lit/ds/symlink/ads8344.pdf
8  */
9
10 #include <linux/delay.h>
11 #include <linux/iio/buffer.h>
12 #include <linux/iio/iio.h>
13 #include <linux/module.h>
14 #include <linux/regulator/consumer.h>
15 #include <linux/spi/spi.h>
16
17 #define ADS8344_START BIT(7)
18 #define ADS8344_SINGLE_END BIT(2)
19 #define ADS8344_CHANNEL(channel) ((channel) << 4)
20 #define ADS8344_CLOCK_INTERNAL 0x2 /* PD1 = 1 and PD0 = 0 */
21
22 struct ads8344 {
23         struct spi_device *spi;
24         struct regulator *reg;
25         /*
26          * Lock protecting access to adc->tx_buff and rx_buff,
27          * especially from concurrent read on sysfs file.
28          */
29         struct mutex lock;
30
31         u8 tx_buf ____cacheline_aligned;
32         u16 rx_buf;
33 };
34
35 #define ADS8344_VOLTAGE_CHANNEL(chan, si)                               \
36         {                                                               \
37                 .type = IIO_VOLTAGE,                                    \
38                 .indexed = 1,                                           \
39                 .channel = chan,                                        \
40                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
41                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
42         }
43
44 #define ADS8344_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si)                  \
45         {                                                               \
46                 .type = IIO_VOLTAGE,                                    \
47                 .indexed = 1,                                           \
48                 .channel = (chan1),                                     \
49                 .channel2 = (chan2),                                    \
50                 .differential = 1,                                      \
51                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
52                 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
53         }
54
55 static const struct iio_chan_spec ads8344_channels[] = {
56         ADS8344_VOLTAGE_CHANNEL(0, 0),
57         ADS8344_VOLTAGE_CHANNEL(1, 4),
58         ADS8344_VOLTAGE_CHANNEL(2, 1),
59         ADS8344_VOLTAGE_CHANNEL(3, 5),
60         ADS8344_VOLTAGE_CHANNEL(4, 2),
61         ADS8344_VOLTAGE_CHANNEL(5, 6),
62         ADS8344_VOLTAGE_CHANNEL(6, 3),
63         ADS8344_VOLTAGE_CHANNEL(7, 7),
64         ADS8344_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
65         ADS8344_VOLTAGE_CHANNEL_DIFF(2, 3, 9),
66         ADS8344_VOLTAGE_CHANNEL_DIFF(4, 5, 10),
67         ADS8344_VOLTAGE_CHANNEL_DIFF(6, 7, 11),
68         ADS8344_VOLTAGE_CHANNEL_DIFF(1, 0, 12),
69         ADS8344_VOLTAGE_CHANNEL_DIFF(3, 2, 13),
70         ADS8344_VOLTAGE_CHANNEL_DIFF(5, 4, 14),
71         ADS8344_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
72 };
73
74 static int ads8344_adc_conversion(struct ads8344 *adc, int channel,
75                                   bool differential)
76 {
77         struct spi_device *spi = adc->spi;
78         int ret;
79
80         adc->tx_buf = ADS8344_START;
81         if (!differential)
82                 adc->tx_buf |= ADS8344_SINGLE_END;
83         adc->tx_buf |= ADS8344_CHANNEL(channel);
84         adc->tx_buf |= ADS8344_CLOCK_INTERNAL;
85
86         ret = spi_write(spi, &adc->tx_buf, 1);
87         if (ret)
88                 return ret;
89
90         udelay(9);
91
92         ret = spi_read(spi, &adc->rx_buf, 2);
93         if (ret)
94                 return ret;
95
96         return adc->rx_buf;
97 }
98
99 static int ads8344_read_raw(struct iio_dev *iio,
100                             struct iio_chan_spec const *channel, int *value,
101                             int *shift, long mask)
102 {
103         struct ads8344 *adc = iio_priv(iio);
104
105         switch (mask) {
106         case IIO_CHAN_INFO_RAW:
107                 mutex_lock(&adc->lock);
108                 *value = ads8344_adc_conversion(adc, channel->scan_index,
109                                                 channel->differential);
110                 mutex_unlock(&adc->lock);
111                 if (*value < 0)
112                         return *value;
113
114                 return IIO_VAL_INT;
115         case IIO_CHAN_INFO_SCALE:
116                 *value = regulator_get_voltage(adc->reg);
117                 if (*value < 0)
118                         return *value;
119
120                 /* convert regulator output voltage to mV */
121                 *value /= 1000;
122                 *shift = 16;
123
124                 return IIO_VAL_FRACTIONAL_LOG2;
125         default:
126                 return -EINVAL;
127         }
128 }
129
130 static const struct iio_info ads8344_info = {
131         .read_raw = ads8344_read_raw,
132 };
133
134 static int ads8344_probe(struct spi_device *spi)
135 {
136         struct iio_dev *indio_dev;
137         struct ads8344 *adc;
138         int ret;
139
140         indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
141         if (!indio_dev)
142                 return -ENOMEM;
143
144         adc = iio_priv(indio_dev);
145         adc->spi = spi;
146         mutex_init(&adc->lock);
147
148         indio_dev->name = dev_name(&spi->dev);
149         indio_dev->dev.parent = &spi->dev;
150         indio_dev->dev.of_node = spi->dev.of_node;
151         indio_dev->info = &ads8344_info;
152         indio_dev->modes = INDIO_DIRECT_MODE;
153         indio_dev->channels = ads8344_channels;
154         indio_dev->num_channels = ARRAY_SIZE(ads8344_channels);
155
156         adc->reg = devm_regulator_get(&spi->dev, "vref");
157         if (IS_ERR(adc->reg))
158                 return PTR_ERR(adc->reg);
159
160         ret = regulator_enable(adc->reg);
161         if (ret)
162                 return ret;
163
164         spi_set_drvdata(spi, indio_dev);
165
166         ret = iio_device_register(indio_dev);
167         if (ret) {
168                 regulator_disable(adc->reg);
169                 return ret;
170         }
171
172         return 0;
173 }
174
175 static int ads8344_remove(struct spi_device *spi)
176 {
177         struct iio_dev *indio_dev = spi_get_drvdata(spi);
178         struct ads8344 *adc = iio_priv(indio_dev);
179
180         iio_device_unregister(indio_dev);
181         regulator_disable(adc->reg);
182
183         return 0;
184 }
185
186 static const struct of_device_id ads8344_of_match[] = {
187         { .compatible = "ti,ads8344", },
188         {}
189 };
190 MODULE_DEVICE_TABLE(of, ads8344_of_match);
191
192 static struct spi_driver ads8344_driver = {
193         .driver = {
194                 .name = "ads8344",
195                 .of_match_table = ads8344_of_match,
196         },
197         .probe = ads8344_probe,
198         .remove = ads8344_remove,
199 };
200 module_spi_driver(ads8344_driver);
201
202 MODULE_AUTHOR("Gregory CLEMENT <gregory.clement@bootlin.com>");
203 MODULE_DESCRIPTION("ADS8344 driver");
204 MODULE_LICENSE("GPL");