Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * iio/adc/max11100.c
0004  * Maxim max11100 ADC Driver with IIO interface
0005  *
0006  * Copyright (C) 2016-17 Renesas Electronics Corporation
0007  * Copyright (C) 2016-17 Jacopo Mondi
0008  */
0009 #include <linux/delay.h>
0010 #include <linux/kernel.h>
0011 #include <linux/mod_devicetable.h>
0012 #include <linux/module.h>
0013 #include <linux/regulator/consumer.h>
0014 #include <linux/spi/spi.h>
0015 #include <asm/unaligned.h>
0016 
0017 #include <linux/iio/iio.h>
0018 #include <linux/iio/driver.h>
0019 
0020 /*
0021  * LSB is the ADC single digital step
0022  * 1 LSB = (vref_mv / 2 ^ 16)
0023  *
0024  * LSB is used to calculate analog voltage value
0025  * from the number of ADC steps count
0026  *
0027  * Ain = (count * LSB)
0028  */
0029 #define MAX11100_LSB_DIV        (1 << 16)
0030 
0031 struct max11100_state {
0032     struct regulator *vref_reg;
0033     struct spi_device *spi;
0034 
0035     /*
0036      * DMA (thus cache coherency maintenance) may require the
0037      * transfer buffers to live in their own cache lines.
0038      */
0039     u8 buffer[3] __aligned(IIO_DMA_MINALIGN);
0040 };
0041 
0042 static const struct iio_chan_spec max11100_channels[] = {
0043     { /* [0] */
0044         .type = IIO_VOLTAGE,
0045         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0046                       BIT(IIO_CHAN_INFO_SCALE),
0047     },
0048 };
0049 
0050 static int max11100_read_single(struct iio_dev *indio_dev, int *val)
0051 {
0052     int ret;
0053     struct max11100_state *state = iio_priv(indio_dev);
0054 
0055     ret = spi_read(state->spi, state->buffer, sizeof(state->buffer));
0056     if (ret) {
0057         dev_err(&indio_dev->dev, "SPI transfer failed\n");
0058         return ret;
0059     }
0060 
0061     /* the first 8 bits sent out from ADC must be 0s */
0062     if (state->buffer[0]) {
0063         dev_err(&indio_dev->dev, "Invalid value: buffer[0] != 0\n");
0064         return -EINVAL;
0065     }
0066 
0067     *val = get_unaligned_be16(&state->buffer[1]);
0068 
0069     return 0;
0070 }
0071 
0072 static int max11100_read_raw(struct iio_dev *indio_dev,
0073                  struct iio_chan_spec const *chan,
0074                  int *val, int *val2, long info)
0075 {
0076     int ret, vref_uv;
0077     struct max11100_state *state = iio_priv(indio_dev);
0078 
0079     switch (info) {
0080     case IIO_CHAN_INFO_RAW:
0081         ret = max11100_read_single(indio_dev, val);
0082         if (ret)
0083             return ret;
0084 
0085         return IIO_VAL_INT;
0086 
0087     case IIO_CHAN_INFO_SCALE:
0088         vref_uv = regulator_get_voltage(state->vref_reg);
0089         if (vref_uv < 0)
0090             /* dummy regulator "get_voltage" returns -EINVAL */
0091             return -EINVAL;
0092 
0093         *val =  vref_uv / 1000;
0094         *val2 = MAX11100_LSB_DIV;
0095         return IIO_VAL_FRACTIONAL;
0096     }
0097 
0098     return -EINVAL;
0099 }
0100 
0101 static const struct iio_info max11100_info = {
0102     .read_raw = max11100_read_raw,
0103 };
0104 
0105 static void max11100_regulator_disable(void *reg)
0106 {
0107     regulator_disable(reg);
0108 }
0109 
0110 static int max11100_probe(struct spi_device *spi)
0111 {
0112     int ret;
0113     struct iio_dev *indio_dev;
0114     struct max11100_state *state;
0115 
0116     indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
0117     if (!indio_dev)
0118         return -ENOMEM;
0119 
0120     state = iio_priv(indio_dev);
0121     state->spi = spi;
0122 
0123     indio_dev->name = "max11100";
0124     indio_dev->info = &max11100_info;
0125     indio_dev->modes = INDIO_DIRECT_MODE;
0126     indio_dev->channels = max11100_channels;
0127     indio_dev->num_channels = ARRAY_SIZE(max11100_channels);
0128 
0129     state->vref_reg = devm_regulator_get(&spi->dev, "vref");
0130     if (IS_ERR(state->vref_reg))
0131         return PTR_ERR(state->vref_reg);
0132 
0133     ret = regulator_enable(state->vref_reg);
0134     if (ret)
0135         return ret;
0136 
0137     ret = devm_add_action_or_reset(&spi->dev, max11100_regulator_disable,
0138                        state->vref_reg);
0139     if (ret)
0140         return ret;
0141 
0142     return devm_iio_device_register(&spi->dev, indio_dev);
0143 }
0144 
0145 static const struct of_device_id max11100_ids[] = {
0146     {.compatible = "maxim,max11100"},
0147     { },
0148 };
0149 MODULE_DEVICE_TABLE(of, max11100_ids);
0150 
0151 static struct spi_driver max11100_driver = {
0152     .driver = {
0153         .name   = "max11100",
0154         .of_match_table = max11100_ids,
0155     },
0156     .probe      = max11100_probe,
0157 };
0158 
0159 module_spi_driver(max11100_driver);
0160 
0161 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
0162 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
0163 MODULE_LICENSE("GPL v2");