0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/module.h>
0011 #include <linux/mod_devicetable.h>
0012 #include <linux/spi/spi.h>
0013 #include <linux/iio/iio.h>
0014 #include <linux/regulator/consumer.h>
0015 #include <linux/iio/buffer.h>
0016 #include <linux/iio/trigger.h>
0017 #include <linux/iio/triggered_buffer.h>
0018 #include <linux/iio/trigger_consumer.h>
0019
0020 enum {
0021 adc0831,
0022 adc0832,
0023 adc0834,
0024 adc0838,
0025 };
0026
0027 struct adc0832 {
0028 struct spi_device *spi;
0029 struct regulator *reg;
0030 struct mutex lock;
0031 u8 mux_bits;
0032
0033
0034
0035
0036
0037 u8 data[24] __aligned(8);
0038
0039 u8 tx_buf[2] __aligned(IIO_DMA_MINALIGN);
0040 u8 rx_buf[2];
0041 };
0042
0043 #define ADC0832_VOLTAGE_CHANNEL(chan) \
0044 { \
0045 .type = IIO_VOLTAGE, \
0046 .indexed = 1, \
0047 .channel = chan, \
0048 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
0049 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
0050 .scan_index = chan, \
0051 .scan_type = { \
0052 .sign = 'u', \
0053 .realbits = 8, \
0054 .storagebits = 8, \
0055 }, \
0056 }
0057
0058 #define ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
0059 { \
0060 .type = IIO_VOLTAGE, \
0061 .indexed = 1, \
0062 .channel = (chan1), \
0063 .channel2 = (chan2), \
0064 .differential = 1, \
0065 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
0066 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
0067 .scan_index = si, \
0068 .scan_type = { \
0069 .sign = 'u', \
0070 .realbits = 8, \
0071 .storagebits = 8, \
0072 }, \
0073 }
0074
0075 static const struct iio_chan_spec adc0831_channels[] = {
0076 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 0),
0077 IIO_CHAN_SOFT_TIMESTAMP(1),
0078 };
0079
0080 static const struct iio_chan_spec adc0832_channels[] = {
0081 ADC0832_VOLTAGE_CHANNEL(0),
0082 ADC0832_VOLTAGE_CHANNEL(1),
0083 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
0084 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
0085 IIO_CHAN_SOFT_TIMESTAMP(4),
0086 };
0087
0088 static const struct iio_chan_spec adc0834_channels[] = {
0089 ADC0832_VOLTAGE_CHANNEL(0),
0090 ADC0832_VOLTAGE_CHANNEL(1),
0091 ADC0832_VOLTAGE_CHANNEL(2),
0092 ADC0832_VOLTAGE_CHANNEL(3),
0093 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 4),
0094 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 5),
0095 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 6),
0096 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 7),
0097 IIO_CHAN_SOFT_TIMESTAMP(8),
0098 };
0099
0100 static const struct iio_chan_spec adc0838_channels[] = {
0101 ADC0832_VOLTAGE_CHANNEL(0),
0102 ADC0832_VOLTAGE_CHANNEL(1),
0103 ADC0832_VOLTAGE_CHANNEL(2),
0104 ADC0832_VOLTAGE_CHANNEL(3),
0105 ADC0832_VOLTAGE_CHANNEL(4),
0106 ADC0832_VOLTAGE_CHANNEL(5),
0107 ADC0832_VOLTAGE_CHANNEL(6),
0108 ADC0832_VOLTAGE_CHANNEL(7),
0109 ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
0110 ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
0111 ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
0112 ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
0113 ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
0114 ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
0115 ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
0116 ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
0117 IIO_CHAN_SOFT_TIMESTAMP(16),
0118 };
0119
0120 static int adc0831_adc_conversion(struct adc0832 *adc)
0121 {
0122 struct spi_device *spi = adc->spi;
0123 int ret;
0124
0125 ret = spi_read(spi, &adc->rx_buf, 2);
0126 if (ret)
0127 return ret;
0128
0129
0130
0131
0132 return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
0133 }
0134
0135 static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
0136 bool differential)
0137 {
0138 struct spi_device *spi = adc->spi;
0139 struct spi_transfer xfer = {
0140 .tx_buf = adc->tx_buf,
0141 .rx_buf = adc->rx_buf,
0142 .len = 2,
0143 };
0144 int ret;
0145
0146 if (!adc->mux_bits)
0147 return adc0831_adc_conversion(adc);
0148
0149
0150 adc->tx_buf[0] = 1 << (adc->mux_bits + 1);
0151
0152 adc->tx_buf[0] |= differential ? 0 : (1 << adc->mux_bits);
0153
0154 adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
0155
0156 if (adc->mux_bits > 1)
0157 adc->tx_buf[0] |= channel / 2;
0158
0159
0160 adc->tx_buf[0] <<= 1;
0161
0162 ret = spi_sync_transfer(spi, &xfer, 1);
0163 if (ret)
0164 return ret;
0165
0166 return adc->rx_buf[1];
0167 }
0168
0169 static int adc0832_read_raw(struct iio_dev *iio,
0170 struct iio_chan_spec const *channel, int *value,
0171 int *shift, long mask)
0172 {
0173 struct adc0832 *adc = iio_priv(iio);
0174
0175 switch (mask) {
0176 case IIO_CHAN_INFO_RAW:
0177 mutex_lock(&adc->lock);
0178 *value = adc0832_adc_conversion(adc, channel->channel,
0179 channel->differential);
0180 mutex_unlock(&adc->lock);
0181 if (*value < 0)
0182 return *value;
0183
0184 return IIO_VAL_INT;
0185 case IIO_CHAN_INFO_SCALE:
0186 *value = regulator_get_voltage(adc->reg);
0187 if (*value < 0)
0188 return *value;
0189
0190
0191 *value /= 1000;
0192 *shift = 8;
0193
0194 return IIO_VAL_FRACTIONAL_LOG2;
0195 }
0196
0197 return -EINVAL;
0198 }
0199
0200 static const struct iio_info adc0832_info = {
0201 .read_raw = adc0832_read_raw,
0202 };
0203
0204 static irqreturn_t adc0832_trigger_handler(int irq, void *p)
0205 {
0206 struct iio_poll_func *pf = p;
0207 struct iio_dev *indio_dev = pf->indio_dev;
0208 struct adc0832 *adc = iio_priv(indio_dev);
0209 int scan_index;
0210 int i = 0;
0211
0212 mutex_lock(&adc->lock);
0213
0214 for_each_set_bit(scan_index, indio_dev->active_scan_mask,
0215 indio_dev->masklength) {
0216 const struct iio_chan_spec *scan_chan =
0217 &indio_dev->channels[scan_index];
0218 int ret = adc0832_adc_conversion(adc, scan_chan->channel,
0219 scan_chan->differential);
0220 if (ret < 0) {
0221 dev_warn(&adc->spi->dev,
0222 "failed to get conversion data\n");
0223 goto out;
0224 }
0225
0226 adc->data[i] = ret;
0227 i++;
0228 }
0229 iio_push_to_buffers_with_timestamp(indio_dev, adc->data,
0230 iio_get_time_ns(indio_dev));
0231 out:
0232 mutex_unlock(&adc->lock);
0233
0234 iio_trigger_notify_done(indio_dev->trig);
0235
0236 return IRQ_HANDLED;
0237 }
0238
0239 static void adc0832_reg_disable(void *reg)
0240 {
0241 regulator_disable(reg);
0242 }
0243
0244 static int adc0832_probe(struct spi_device *spi)
0245 {
0246 struct iio_dev *indio_dev;
0247 struct adc0832 *adc;
0248 int ret;
0249
0250 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
0251 if (!indio_dev)
0252 return -ENOMEM;
0253
0254 adc = iio_priv(indio_dev);
0255 adc->spi = spi;
0256 mutex_init(&adc->lock);
0257
0258 indio_dev->name = spi_get_device_id(spi)->name;
0259 indio_dev->info = &adc0832_info;
0260 indio_dev->modes = INDIO_DIRECT_MODE;
0261
0262 switch (spi_get_device_id(spi)->driver_data) {
0263 case adc0831:
0264 adc->mux_bits = 0;
0265 indio_dev->channels = adc0831_channels;
0266 indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
0267 break;
0268 case adc0832:
0269 adc->mux_bits = 1;
0270 indio_dev->channels = adc0832_channels;
0271 indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
0272 break;
0273 case adc0834:
0274 adc->mux_bits = 2;
0275 indio_dev->channels = adc0834_channels;
0276 indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
0277 break;
0278 case adc0838:
0279 adc->mux_bits = 3;
0280 indio_dev->channels = adc0838_channels;
0281 indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
0282 break;
0283 default:
0284 return -EINVAL;
0285 }
0286
0287 adc->reg = devm_regulator_get(&spi->dev, "vref");
0288 if (IS_ERR(adc->reg))
0289 return PTR_ERR(adc->reg);
0290
0291 ret = regulator_enable(adc->reg);
0292 if (ret)
0293 return ret;
0294
0295 ret = devm_add_action_or_reset(&spi->dev, adc0832_reg_disable,
0296 adc->reg);
0297 if (ret)
0298 return ret;
0299
0300 ret = devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, NULL,
0301 adc0832_trigger_handler, NULL);
0302 if (ret)
0303 return ret;
0304
0305 return devm_iio_device_register(&spi->dev, indio_dev);
0306 }
0307
0308 static const struct of_device_id adc0832_dt_ids[] = {
0309 { .compatible = "ti,adc0831", },
0310 { .compatible = "ti,adc0832", },
0311 { .compatible = "ti,adc0834", },
0312 { .compatible = "ti,adc0838", },
0313 {}
0314 };
0315 MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
0316
0317 static const struct spi_device_id adc0832_id[] = {
0318 { "adc0831", adc0831 },
0319 { "adc0832", adc0832 },
0320 { "adc0834", adc0834 },
0321 { "adc0838", adc0838 },
0322 {}
0323 };
0324 MODULE_DEVICE_TABLE(spi, adc0832_id);
0325
0326 static struct spi_driver adc0832_driver = {
0327 .driver = {
0328 .name = "adc0832",
0329 .of_match_table = adc0832_dt_ids,
0330 },
0331 .probe = adc0832_probe,
0332 .id_table = adc0832_id,
0333 };
0334 module_spi_driver(adc0832_driver);
0335
0336 MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
0337 MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
0338 MODULE_LICENSE("GPL v2");