linux/drivers/iio/adc/ti-adc0832.c
<<
>>
Prefs
   1/*
   2 * ADC0831/ADC0832/ADC0834/ADC0838 8-bit ADC driver
   3 *
   4 * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
   5 *
   6 * This file is subject to the terms and conditions of version 2 of
   7 * the GNU General Public License.  See the file COPYING in the main
   8 * directory of this archive for more details.
   9 *
  10 * Datasheet: http://www.ti.com/lit/ds/symlink/adc0832-n.pdf
  11 */
  12
  13#include <linux/module.h>
  14#include <linux/spi/spi.h>
  15#include <linux/iio/iio.h>
  16#include <linux/regulator/consumer.h>
  17
  18enum {
  19        adc0831,
  20        adc0832,
  21        adc0834,
  22        adc0838,
  23};
  24
  25struct adc0832 {
  26        struct spi_device *spi;
  27        struct regulator *reg;
  28        struct mutex lock;
  29        u8 mux_bits;
  30
  31        u8 tx_buf[2] ____cacheline_aligned;
  32        u8 rx_buf[2];
  33};
  34
  35#define ADC0832_VOLTAGE_CHANNEL(chan)                                   \
  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 ADC0832_VOLTAGE_CHANNEL_DIFF(chan1, chan2)                      \
  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
  55static const struct iio_chan_spec adc0831_channels[] = {
  56        ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
  57};
  58
  59static const struct iio_chan_spec adc0832_channels[] = {
  60        ADC0832_VOLTAGE_CHANNEL(0),
  61        ADC0832_VOLTAGE_CHANNEL(1),
  62        ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
  63        ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0),
  64};
  65
  66static const struct iio_chan_spec adc0834_channels[] = {
  67        ADC0832_VOLTAGE_CHANNEL(0),
  68        ADC0832_VOLTAGE_CHANNEL(1),
  69        ADC0832_VOLTAGE_CHANNEL(2),
  70        ADC0832_VOLTAGE_CHANNEL(3),
  71        ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
  72        ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0),
  73        ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3),
  74        ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2),
  75};
  76
  77static const struct iio_chan_spec adc0838_channels[] = {
  78        ADC0832_VOLTAGE_CHANNEL(0),
  79        ADC0832_VOLTAGE_CHANNEL(1),
  80        ADC0832_VOLTAGE_CHANNEL(2),
  81        ADC0832_VOLTAGE_CHANNEL(3),
  82        ADC0832_VOLTAGE_CHANNEL(4),
  83        ADC0832_VOLTAGE_CHANNEL(5),
  84        ADC0832_VOLTAGE_CHANNEL(6),
  85        ADC0832_VOLTAGE_CHANNEL(7),
  86        ADC0832_VOLTAGE_CHANNEL_DIFF(0, 1),
  87        ADC0832_VOLTAGE_CHANNEL_DIFF(1, 0),
  88        ADC0832_VOLTAGE_CHANNEL_DIFF(2, 3),
  89        ADC0832_VOLTAGE_CHANNEL_DIFF(3, 2),
  90        ADC0832_VOLTAGE_CHANNEL_DIFF(4, 5),
  91        ADC0832_VOLTAGE_CHANNEL_DIFF(5, 4),
  92        ADC0832_VOLTAGE_CHANNEL_DIFF(6, 7),
  93        ADC0832_VOLTAGE_CHANNEL_DIFF(7, 6),
  94};
  95
  96static int adc0831_adc_conversion(struct adc0832 *adc)
  97{
  98        struct spi_device *spi = adc->spi;
  99        int ret;
 100
 101        ret = spi_read(spi, &adc->rx_buf, 2);
 102        if (ret)
 103                return ret;
 104
 105        /*
 106         * Skip TRI-STATE and a leading zero
 107         */
 108        return (adc->rx_buf[0] << 2 & 0xff) | (adc->rx_buf[1] >> 6);
 109}
 110
 111static int adc0832_adc_conversion(struct adc0832 *adc, int channel,
 112                                bool differential)
 113{
 114        struct spi_device *spi = adc->spi;
 115        struct spi_transfer xfer = {
 116                .tx_buf = adc->tx_buf,
 117                .rx_buf = adc->rx_buf,
 118                .len = 2,
 119        };
 120        int ret;
 121
 122        if (!adc->mux_bits)
 123                return adc0831_adc_conversion(adc);
 124
 125        /* start bit */
 126        adc->tx_buf[0] = 1 << (adc->mux_bits + 1);
 127        /* single-ended or differential */
 128        adc->tx_buf[0] |= differential ? 0 : (1 << adc->mux_bits);
 129        /* odd / sign */
 130        adc->tx_buf[0] |= (channel % 2) << (adc->mux_bits - 1);
 131        /* select */
 132        if (adc->mux_bits > 1)
 133                adc->tx_buf[0] |= channel / 2;
 134
 135        /* align Data output BIT7 (MSB) to 8-bit boundary */
 136        adc->tx_buf[0] <<= 1;
 137
 138        ret = spi_sync_transfer(spi, &xfer, 1);
 139        if (ret)
 140                return ret;
 141
 142        return adc->rx_buf[1];
 143}
 144
 145static int adc0832_read_raw(struct iio_dev *iio,
 146                        struct iio_chan_spec const *channel, int *value,
 147                        int *shift, long mask)
 148{
 149        struct adc0832 *adc = iio_priv(iio);
 150
 151        switch (mask) {
 152        case IIO_CHAN_INFO_RAW:
 153                mutex_lock(&adc->lock);
 154                *value = adc0832_adc_conversion(adc, channel->channel,
 155                                                channel->differential);
 156                mutex_unlock(&adc->lock);
 157                if (*value < 0)
 158                        return *value;
 159
 160                return IIO_VAL_INT;
 161        case IIO_CHAN_INFO_SCALE:
 162                *value = regulator_get_voltage(adc->reg);
 163                if (*value < 0)
 164                        return *value;
 165
 166                /* convert regulator output voltage to mV */
 167                *value /= 1000;
 168                *shift = 8;
 169
 170                return IIO_VAL_FRACTIONAL_LOG2;
 171        }
 172
 173        return -EINVAL;
 174}
 175
 176static const struct iio_info adc0832_info = {
 177        .read_raw = adc0832_read_raw,
 178        .driver_module = THIS_MODULE,
 179};
 180
 181static int adc0832_probe(struct spi_device *spi)
 182{
 183        struct iio_dev *indio_dev;
 184        struct adc0832 *adc;
 185        int ret;
 186
 187        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
 188        if (!indio_dev)
 189                return -ENOMEM;
 190
 191        adc = iio_priv(indio_dev);
 192        adc->spi = spi;
 193        mutex_init(&adc->lock);
 194
 195        indio_dev->name = spi_get_device_id(spi)->name;
 196        indio_dev->dev.parent = &spi->dev;
 197        indio_dev->info = &adc0832_info;
 198        indio_dev->modes = INDIO_DIRECT_MODE;
 199
 200        switch (spi_get_device_id(spi)->driver_data) {
 201        case adc0831:
 202                adc->mux_bits = 0;
 203                indio_dev->channels = adc0831_channels;
 204                indio_dev->num_channels = ARRAY_SIZE(adc0831_channels);
 205                break;
 206        case adc0832:
 207                adc->mux_bits = 1;
 208                indio_dev->channels = adc0832_channels;
 209                indio_dev->num_channels = ARRAY_SIZE(adc0832_channels);
 210                break;
 211        case adc0834:
 212                adc->mux_bits = 2;
 213                indio_dev->channels = adc0834_channels;
 214                indio_dev->num_channels = ARRAY_SIZE(adc0834_channels);
 215                break;
 216        case adc0838:
 217                adc->mux_bits = 3;
 218                indio_dev->channels = adc0838_channels;
 219                indio_dev->num_channels = ARRAY_SIZE(adc0838_channels);
 220                break;
 221        default:
 222                return -EINVAL;
 223        }
 224
 225        adc->reg = devm_regulator_get(&spi->dev, "vref");
 226        if (IS_ERR(adc->reg))
 227                return PTR_ERR(adc->reg);
 228
 229        ret = regulator_enable(adc->reg);
 230        if (ret)
 231                return ret;
 232
 233        spi_set_drvdata(spi, indio_dev);
 234
 235        ret = iio_device_register(indio_dev);
 236        if (ret)
 237                regulator_disable(adc->reg);
 238
 239        return ret;
 240}
 241
 242static int adc0832_remove(struct spi_device *spi)
 243{
 244        struct iio_dev *indio_dev = spi_get_drvdata(spi);
 245        struct adc0832 *adc = iio_priv(indio_dev);
 246
 247        iio_device_unregister(indio_dev);
 248        regulator_disable(adc->reg);
 249
 250        return 0;
 251}
 252
 253#ifdef CONFIG_OF
 254
 255static const struct of_device_id adc0832_dt_ids[] = {
 256        { .compatible = "ti,adc0831", },
 257        { .compatible = "ti,adc0832", },
 258        { .compatible = "ti,adc0834", },
 259        { .compatible = "ti,adc0838", },
 260        {}
 261};
 262MODULE_DEVICE_TABLE(of, adc0832_dt_ids);
 263
 264#endif
 265
 266static const struct spi_device_id adc0832_id[] = {
 267        { "adc0831", adc0831 },
 268        { "adc0832", adc0832 },
 269        { "adc0834", adc0834 },
 270        { "adc0838", adc0838 },
 271        {}
 272};
 273MODULE_DEVICE_TABLE(spi, adc0832_id);
 274
 275static struct spi_driver adc0832_driver = {
 276        .driver = {
 277                .name = "adc0832",
 278                .of_match_table = of_match_ptr(adc0832_dt_ids),
 279        },
 280        .probe = adc0832_probe,
 281        .remove = adc0832_remove,
 282        .id_table = adc0832_id,
 283};
 284module_spi_driver(adc0832_driver);
 285
 286MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
 287MODULE_DESCRIPTION("ADC0831/ADC0832/ADC0834/ADC0838 driver");
 288MODULE_LICENSE("GPL v2");
 289