Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * MAX1241 low-power, 12-bit serial ADC
0004  *
0005  * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1240-MAX1241.pdf
0006  */
0007 
0008 #include <linux/delay.h>
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/iio/iio.h>
0011 #include <linux/module.h>
0012 #include <linux/regulator/consumer.h>
0013 #include <linux/spi/spi.h>
0014 
0015 #define MAX1241_VAL_MASK GENMASK(11, 0)
0016 #define MAX1241_SHUTDOWN_DELAY_USEC 4
0017 
0018 enum max1241_id {
0019     max1241,
0020 };
0021 
0022 struct max1241 {
0023     struct spi_device *spi;
0024     struct mutex lock;
0025     struct regulator *vdd;
0026     struct regulator *vref;
0027     struct gpio_desc *shutdown;
0028 
0029     __be16 data __aligned(IIO_DMA_MINALIGN);
0030 };
0031 
0032 static const struct iio_chan_spec max1241_channels[] = {
0033     {
0034         .type = IIO_VOLTAGE,
0035         .indexed = 1,
0036         .channel = 0,
0037         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0038                 BIT(IIO_CHAN_INFO_SCALE),
0039     },
0040 };
0041 
0042 static int max1241_read(struct max1241 *adc)
0043 {
0044     struct spi_transfer xfers[] = {
0045         /*
0046          * Begin conversion by bringing /CS low for at least
0047          * tconv us.
0048          */
0049         {
0050             .len = 0,
0051             .delay.value = 8,
0052             .delay.unit = SPI_DELAY_UNIT_USECS,
0053         },
0054         /*
0055          * Then read two bytes of data in our RX buffer.
0056          */
0057         {
0058             .rx_buf = &adc->data,
0059             .len = 2,
0060         },
0061     };
0062 
0063     return spi_sync_transfer(adc->spi, xfers, ARRAY_SIZE(xfers));
0064 }
0065 
0066 static int max1241_read_raw(struct iio_dev *indio_dev,
0067             struct iio_chan_spec const *chan,
0068             int *val, int *val2, long mask)
0069 {
0070     int ret, vref_uV;
0071     struct max1241 *adc = iio_priv(indio_dev);
0072 
0073     switch (mask) {
0074     case IIO_CHAN_INFO_RAW:
0075         mutex_lock(&adc->lock);
0076 
0077         if (adc->shutdown) {
0078             gpiod_set_value(adc->shutdown, 0);
0079             udelay(MAX1241_SHUTDOWN_DELAY_USEC);
0080             ret = max1241_read(adc);
0081             gpiod_set_value(adc->shutdown, 1);
0082         } else
0083             ret = max1241_read(adc);
0084 
0085         if (ret) {
0086             mutex_unlock(&adc->lock);
0087             return ret;
0088         }
0089 
0090         *val = (be16_to_cpu(adc->data) >> 3) & MAX1241_VAL_MASK;
0091 
0092         mutex_unlock(&adc->lock);
0093         return IIO_VAL_INT;
0094     case IIO_CHAN_INFO_SCALE:
0095         vref_uV = regulator_get_voltage(adc->vref);
0096 
0097         if (vref_uV < 0)
0098             return vref_uV;
0099 
0100         *val = vref_uV / 1000;
0101         *val2 = 12;
0102 
0103         return IIO_VAL_FRACTIONAL_LOG2;
0104     default:
0105         return -EINVAL;
0106     }
0107 }
0108 
0109 static const struct iio_info max1241_info = {
0110     .read_raw = max1241_read_raw,
0111 };
0112 
0113 static void max1241_disable_vdd_action(void *data)
0114 {
0115     struct max1241 *adc = data;
0116     struct device *dev = &adc->spi->dev;
0117     int err;
0118 
0119     err = regulator_disable(adc->vdd);
0120     if (err)
0121         dev_err(dev, "could not disable vdd regulator.\n");
0122 }
0123 
0124 static void max1241_disable_vref_action(void *data)
0125 {
0126     struct max1241 *adc = data;
0127     struct device *dev = &adc->spi->dev;
0128     int err;
0129 
0130     err = regulator_disable(adc->vref);
0131     if (err)
0132         dev_err(dev, "could not disable vref regulator.\n");
0133 }
0134 
0135 static int max1241_probe(struct spi_device *spi)
0136 {
0137     struct device *dev = &spi->dev;
0138     struct iio_dev *indio_dev;
0139     struct max1241 *adc;
0140     int ret;
0141 
0142     indio_dev = devm_iio_device_alloc(dev, sizeof(*adc));
0143     if (!indio_dev)
0144         return -ENOMEM;
0145 
0146     adc = iio_priv(indio_dev);
0147     adc->spi = spi;
0148     mutex_init(&adc->lock);
0149 
0150     adc->vdd = devm_regulator_get(dev, "vdd");
0151     if (IS_ERR(adc->vdd))
0152         return dev_err_probe(dev, PTR_ERR(adc->vdd),
0153                      "failed to get vdd regulator\n");
0154 
0155     ret = regulator_enable(adc->vdd);
0156     if (ret)
0157         return ret;
0158 
0159     ret = devm_add_action_or_reset(dev, max1241_disable_vdd_action, adc);
0160     if (ret) {
0161         dev_err(dev, "could not set up vdd regulator cleanup action\n");
0162         return ret;
0163     }
0164 
0165     adc->vref = devm_regulator_get(dev, "vref");
0166     if (IS_ERR(adc->vref))
0167         return dev_err_probe(dev, PTR_ERR(adc->vref),
0168                      "failed to get vref regulator\n");
0169 
0170     ret = regulator_enable(adc->vref);
0171     if (ret)
0172         return ret;
0173 
0174     ret = devm_add_action_or_reset(dev, max1241_disable_vref_action, adc);
0175     if (ret) {
0176         dev_err(dev, "could not set up vref regulator cleanup action\n");
0177         return ret;
0178     }
0179 
0180     adc->shutdown = devm_gpiod_get_optional(dev, "shutdown",
0181                         GPIOD_OUT_HIGH);
0182     if (IS_ERR(adc->shutdown))
0183         return dev_err_probe(dev, PTR_ERR(adc->shutdown),
0184                      "cannot get shutdown gpio\n");
0185 
0186     if (adc->shutdown)
0187         dev_dbg(dev, "shutdown pin passed, low-power mode enabled");
0188     else
0189         dev_dbg(dev, "no shutdown pin passed, low-power mode disabled");
0190 
0191     indio_dev->name = spi_get_device_id(spi)->name;
0192     indio_dev->info = &max1241_info;
0193     indio_dev->modes = INDIO_DIRECT_MODE;
0194     indio_dev->channels = max1241_channels;
0195     indio_dev->num_channels = ARRAY_SIZE(max1241_channels);
0196 
0197     return devm_iio_device_register(dev, indio_dev);
0198 }
0199 
0200 static const struct spi_device_id max1241_id[] = {
0201     { "max1241", max1241 },
0202     {}
0203 };
0204 
0205 static const struct of_device_id max1241_dt_ids[] = {
0206     { .compatible = "maxim,max1241" },
0207     {}
0208 };
0209 MODULE_DEVICE_TABLE(of, max1241_dt_ids);
0210 
0211 static struct spi_driver max1241_spi_driver = {
0212     .driver = {
0213         .name = "max1241",
0214         .of_match_table = max1241_dt_ids,
0215     },
0216     .probe = max1241_probe,
0217     .id_table = max1241_id,
0218 };
0219 module_spi_driver(max1241_spi_driver);
0220 
0221 MODULE_AUTHOR("Alexandru Lazar <alazar@startmail.com>");
0222 MODULE_DESCRIPTION("MAX1241 ADC driver");
0223 MODULE_LICENSE("GPL v2");