linux/drivers/iio/adc/max1118.c
<<
>>
Prefs
   1/*
   2 * MAX1117/MAX1118/MAX1119 8-bit, dual-channel ADCs driver
   3 *
   4 * Copyright (c) 2017 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: https://datasheets.maximintegrated.com/en/ds/MAX1117-MAX1119.pdf
  11 *
  12 * SPI interface connections
  13 *
  14 * SPI                MAXIM
  15 * Master  Direction  MAX1117/8/9
  16 * ------  ---------  -----------
  17 * nCS        -->     CNVST
  18 * SCK        -->     SCLK
  19 * MISO       <--     DOUT
  20 * ------  ---------  -----------
  21 */
  22
  23#include <linux/module.h>
  24#include <linux/spi/spi.h>
  25#include <linux/iio/iio.h>
  26#include <linux/iio/buffer.h>
  27#include <linux/iio/triggered_buffer.h>
  28#include <linux/iio/trigger_consumer.h>
  29#include <linux/regulator/consumer.h>
  30
  31enum max1118_id {
  32        max1117,
  33        max1118,
  34        max1119,
  35};
  36
  37struct max1118 {
  38        struct spi_device *spi;
  39        struct mutex lock;
  40        struct regulator *reg;
  41
  42        u8 data ____cacheline_aligned;
  43};
  44
  45#define MAX1118_CHANNEL(ch)                                             \
  46        {                                                               \
  47                .type = IIO_VOLTAGE,                                    \
  48                .indexed = 1,                                           \
  49                .channel = (ch),                                        \
  50                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
  51                .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),   \
  52                .scan_index = ch,                                       \
  53                .scan_type = {                                          \
  54                        .sign = 'u',                                    \
  55                        .realbits = 8,                                  \
  56                        .storagebits = 8,                               \
  57                },                                                      \
  58        }
  59
  60static const struct iio_chan_spec max1118_channels[] = {
  61        MAX1118_CHANNEL(0),
  62        MAX1118_CHANNEL(1),
  63        IIO_CHAN_SOFT_TIMESTAMP(2),
  64};
  65
  66static int max1118_read(struct spi_device *spi, int channel)
  67{
  68        struct iio_dev *indio_dev = spi_get_drvdata(spi);
  69        struct max1118 *adc = iio_priv(indio_dev);
  70        struct spi_transfer xfers[] = {
  71                /*
  72                 * To select CH1 for conversion, CNVST pin must be brought high
  73                 * and low for a second time.
  74                 */
  75                {
  76                        .len = 0,
  77                        .delay_usecs = 1,       /* > CNVST Low Time 100 ns */
  78                        .cs_change = 1,
  79                },
  80                /*
  81                 * The acquisition interval begins with the falling edge of
  82                 * CNVST.  The total acquisition and conversion process takes
  83                 * <7.5us.
  84                 */
  85                {
  86                        .len = 0,
  87                        .delay_usecs = 8,
  88                },
  89                {
  90                        .rx_buf = &adc->data,
  91                        .len = 1,
  92                },
  93        };
  94        int ret;
  95
  96        if (channel == 0)
  97                ret = spi_sync_transfer(spi, xfers + 1, 2);
  98        else
  99                ret = spi_sync_transfer(spi, xfers, 3);
 100
 101        if (ret)
 102                return ret;
 103
 104        return adc->data;
 105}
 106
 107static int max1118_get_vref_mV(struct spi_device *spi)
 108{
 109        struct iio_dev *indio_dev = spi_get_drvdata(spi);
 110        struct max1118 *adc = iio_priv(indio_dev);
 111        const struct spi_device_id *id = spi_get_device_id(spi);
 112        int vref_uV;
 113
 114        switch (id->driver_data) {
 115        case max1117:
 116                return 2048;
 117        case max1119:
 118                return 4096;
 119        case max1118:
 120                vref_uV = regulator_get_voltage(adc->reg);
 121                if (vref_uV < 0)
 122                        return vref_uV;
 123                return vref_uV / 1000;
 124        }
 125
 126        return -ENODEV;
 127}
 128
 129static int max1118_read_raw(struct iio_dev *indio_dev,
 130                        struct iio_chan_spec const *chan,
 131                        int *val, int *val2, long mask)
 132{
 133        struct max1118 *adc = iio_priv(indio_dev);
 134
 135        switch (mask) {
 136        case IIO_CHAN_INFO_RAW:
 137                mutex_lock(&adc->lock);
 138                *val = max1118_read(adc->spi, chan->channel);
 139                mutex_unlock(&adc->lock);
 140                if (*val < 0)
 141                        return *val;
 142
 143                return IIO_VAL_INT;
 144        case IIO_CHAN_INFO_SCALE:
 145                *val = max1118_get_vref_mV(adc->spi);
 146                if (*val < 0)
 147                        return *val;
 148                *val2 = 8;
 149
 150                return IIO_VAL_FRACTIONAL_LOG2;
 151        }
 152
 153        return -EINVAL;
 154}
 155
 156static const struct iio_info max1118_info = {
 157        .read_raw = max1118_read_raw,
 158};
 159
 160static irqreturn_t max1118_trigger_handler(int irq, void *p)
 161{
 162        struct iio_poll_func *pf = p;
 163        struct iio_dev *indio_dev = pf->indio_dev;
 164        struct max1118 *adc = iio_priv(indio_dev);
 165        u8 data[16] = { }; /* 2x 8-bit ADC data + padding + 8 bytes timestamp */
 166        int scan_index;
 167        int i = 0;
 168
 169        mutex_lock(&adc->lock);
 170
 171        for_each_set_bit(scan_index, indio_dev->active_scan_mask,
 172                        indio_dev->masklength) {
 173                const struct iio_chan_spec *scan_chan =
 174                                &indio_dev->channels[scan_index];
 175                int ret = max1118_read(adc->spi, scan_chan->channel);
 176
 177                if (ret < 0) {
 178                        dev_warn(&adc->spi->dev,
 179                                "failed to get conversion data\n");
 180                        goto out;
 181                }
 182
 183                data[i] = ret;
 184                i++;
 185        }
 186        iio_push_to_buffers_with_timestamp(indio_dev, data,
 187                                           iio_get_time_ns(indio_dev));
 188out:
 189        mutex_unlock(&adc->lock);
 190
 191        iio_trigger_notify_done(indio_dev->trig);
 192
 193        return IRQ_HANDLED;
 194}
 195
 196static int max1118_probe(struct spi_device *spi)
 197{
 198        struct iio_dev *indio_dev;
 199        struct max1118 *adc;
 200        const struct spi_device_id *id = spi_get_device_id(spi);
 201        int ret;
 202
 203        indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
 204        if (!indio_dev)
 205                return -ENOMEM;
 206
 207        adc = iio_priv(indio_dev);
 208        adc->spi = spi;
 209        mutex_init(&adc->lock);
 210
 211        if (id->driver_data == max1118) {
 212                adc->reg = devm_regulator_get(&spi->dev, "vref");
 213                if (IS_ERR(adc->reg)) {
 214                        dev_err(&spi->dev, "failed to get vref regulator\n");
 215                        return PTR_ERR(adc->reg);
 216                }
 217                ret = regulator_enable(adc->reg);
 218                if (ret)
 219                        return ret;
 220        }
 221
 222        spi_set_drvdata(spi, indio_dev);
 223
 224        indio_dev->name = spi_get_device_id(spi)->name;
 225        indio_dev->dev.parent = &spi->dev;
 226        indio_dev->info = &max1118_info;
 227        indio_dev->modes = INDIO_DIRECT_MODE;
 228        indio_dev->channels = max1118_channels;
 229        indio_dev->num_channels = ARRAY_SIZE(max1118_channels);
 230
 231        /*
 232         * To reinitiate a conversion on CH0, it is necessary to allow for a
 233         * conversion to be complete and all of the data to be read out.  Once
 234         * a conversion has been completed, the MAX1117/MAX1118/MAX1119 will go
 235         * into AutoShutdown mode until the next conversion is initiated.
 236         */
 237        max1118_read(spi, 0);
 238
 239        ret = iio_triggered_buffer_setup(indio_dev, NULL,
 240                                        max1118_trigger_handler, NULL);
 241        if (ret)
 242                goto err_reg_disable;
 243
 244        ret = iio_device_register(indio_dev);
 245        if (ret)
 246                goto err_buffer_cleanup;
 247
 248        return 0;
 249
 250err_buffer_cleanup:
 251        iio_triggered_buffer_cleanup(indio_dev);
 252err_reg_disable:
 253        if (id->driver_data == max1118)
 254                regulator_disable(adc->reg);
 255
 256        return ret;
 257}
 258
 259static int max1118_remove(struct spi_device *spi)
 260{
 261        struct iio_dev *indio_dev = spi_get_drvdata(spi);
 262        struct max1118 *adc = iio_priv(indio_dev);
 263        const struct spi_device_id *id = spi_get_device_id(spi);
 264
 265        iio_device_unregister(indio_dev);
 266        iio_triggered_buffer_cleanup(indio_dev);
 267        if (id->driver_data == max1118)
 268                return regulator_disable(adc->reg);
 269
 270        return 0;
 271}
 272
 273static const struct spi_device_id max1118_id[] = {
 274        { "max1117", max1117 },
 275        { "max1118", max1118 },
 276        { "max1119", max1119 },
 277        {}
 278};
 279MODULE_DEVICE_TABLE(spi, max1118_id);
 280
 281#ifdef CONFIG_OF
 282
 283static const struct of_device_id max1118_dt_ids[] = {
 284        { .compatible = "maxim,max1117" },
 285        { .compatible = "maxim,max1118" },
 286        { .compatible = "maxim,max1119" },
 287        {},
 288};
 289MODULE_DEVICE_TABLE(of, max1118_dt_ids);
 290
 291#endif
 292
 293static struct spi_driver max1118_spi_driver = {
 294        .driver = {
 295                .name = "max1118",
 296                .of_match_table = of_match_ptr(max1118_dt_ids),
 297        },
 298        .probe = max1118_probe,
 299        .remove = max1118_remove,
 300        .id_table = max1118_id,
 301};
 302module_spi_driver(max1118_spi_driver);
 303
 304MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
 305MODULE_DESCRIPTION("MAXIM MAX1117/MAX1118/MAX1119 ADCs driver");
 306MODULE_LICENSE("GPL v2");
 307