Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com>
0004  *
0005  * Driver for Texas Instruments' ADC128S052, ADC122S021 and ADC124S021 ADC chip.
0006  * Datasheets can be found here:
0007  * https://www.ti.com/lit/ds/symlink/adc128s052.pdf
0008  * https://www.ti.com/lit/ds/symlink/adc122s021.pdf
0009  * https://www.ti.com/lit/ds/symlink/adc124s021.pdf
0010  */
0011 
0012 #include <linux/acpi.h>
0013 #include <linux/err.h>
0014 #include <linux/spi/spi.h>
0015 #include <linux/module.h>
0016 #include <linux/mod_devicetable.h>
0017 #include <linux/iio/iio.h>
0018 #include <linux/property.h>
0019 #include <linux/regulator/consumer.h>
0020 
0021 struct adc128_configuration {
0022     const struct iio_chan_spec  *channels;
0023     u8              num_channels;
0024 };
0025 
0026 struct adc128 {
0027     struct spi_device *spi;
0028 
0029     struct regulator *reg;
0030     struct mutex lock;
0031 
0032     u8 buffer[2] __aligned(IIO_DMA_MINALIGN);
0033 };
0034 
0035 static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
0036 {
0037     int ret;
0038 
0039     mutex_lock(&adc->lock);
0040 
0041     adc->buffer[0] = channel << 3;
0042     adc->buffer[1] = 0;
0043 
0044     ret = spi_write(adc->spi, &adc->buffer, 2);
0045     if (ret < 0) {
0046         mutex_unlock(&adc->lock);
0047         return ret;
0048     }
0049 
0050     ret = spi_read(adc->spi, &adc->buffer, 2);
0051 
0052     mutex_unlock(&adc->lock);
0053 
0054     if (ret < 0)
0055         return ret;
0056 
0057     return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF);
0058 }
0059 
0060 static int adc128_read_raw(struct iio_dev *indio_dev,
0061                struct iio_chan_spec const *channel, int *val,
0062                int *val2, long mask)
0063 {
0064     struct adc128 *adc = iio_priv(indio_dev);
0065     int ret;
0066 
0067     switch (mask) {
0068     case IIO_CHAN_INFO_RAW:
0069 
0070         ret = adc128_adc_conversion(adc, channel->channel);
0071         if (ret < 0)
0072             return ret;
0073 
0074         *val = ret;
0075         return IIO_VAL_INT;
0076 
0077     case IIO_CHAN_INFO_SCALE:
0078 
0079         ret = regulator_get_voltage(adc->reg);
0080         if (ret < 0)
0081             return ret;
0082 
0083         *val = ret / 1000;
0084         *val2 = 12;
0085         return IIO_VAL_FRACTIONAL_LOG2;
0086 
0087     default:
0088         return -EINVAL;
0089     }
0090 
0091 }
0092 
0093 #define ADC128_VOLTAGE_CHANNEL(num) \
0094     { \
0095         .type = IIO_VOLTAGE, \
0096         .indexed = 1, \
0097         .channel = (num), \
0098         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
0099         .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
0100     }
0101 
0102 static const struct iio_chan_spec adc128s052_channels[] = {
0103     ADC128_VOLTAGE_CHANNEL(0),
0104     ADC128_VOLTAGE_CHANNEL(1),
0105     ADC128_VOLTAGE_CHANNEL(2),
0106     ADC128_VOLTAGE_CHANNEL(3),
0107     ADC128_VOLTAGE_CHANNEL(4),
0108     ADC128_VOLTAGE_CHANNEL(5),
0109     ADC128_VOLTAGE_CHANNEL(6),
0110     ADC128_VOLTAGE_CHANNEL(7),
0111 };
0112 
0113 static const struct iio_chan_spec adc122s021_channels[] = {
0114     ADC128_VOLTAGE_CHANNEL(0),
0115     ADC128_VOLTAGE_CHANNEL(1),
0116 };
0117 
0118 static const struct iio_chan_spec adc124s021_channels[] = {
0119     ADC128_VOLTAGE_CHANNEL(0),
0120     ADC128_VOLTAGE_CHANNEL(1),
0121     ADC128_VOLTAGE_CHANNEL(2),
0122     ADC128_VOLTAGE_CHANNEL(3),
0123 };
0124 
0125 static const struct adc128_configuration adc128_config[] = {
0126     { adc128s052_channels, ARRAY_SIZE(adc128s052_channels) },
0127     { adc122s021_channels, ARRAY_SIZE(adc122s021_channels) },
0128     { adc124s021_channels, ARRAY_SIZE(adc124s021_channels) },
0129 };
0130 
0131 static const struct iio_info adc128_info = {
0132     .read_raw = adc128_read_raw,
0133 };
0134 
0135 static void adc128_disable_regulator(void *reg)
0136 {
0137     regulator_disable(reg);
0138 }
0139 
0140 static int adc128_probe(struct spi_device *spi)
0141 {
0142     struct iio_dev *indio_dev;
0143     unsigned int config;
0144     struct adc128 *adc;
0145     int ret;
0146 
0147     if (dev_fwnode(&spi->dev))
0148         config = (unsigned long) device_get_match_data(&spi->dev);
0149     else
0150         config = spi_get_device_id(spi)->driver_data;
0151 
0152     indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
0153     if (!indio_dev)
0154         return -ENOMEM;
0155 
0156     adc = iio_priv(indio_dev);
0157     adc->spi = spi;
0158 
0159     indio_dev->name = spi_get_device_id(spi)->name;
0160     indio_dev->modes = INDIO_DIRECT_MODE;
0161     indio_dev->info = &adc128_info;
0162 
0163     indio_dev->channels = adc128_config[config].channels;
0164     indio_dev->num_channels = adc128_config[config].num_channels;
0165 
0166     adc->reg = devm_regulator_get(&spi->dev, "vref");
0167     if (IS_ERR(adc->reg))
0168         return PTR_ERR(adc->reg);
0169 
0170     ret = regulator_enable(adc->reg);
0171     if (ret < 0)
0172         return ret;
0173     ret = devm_add_action_or_reset(&spi->dev, adc128_disable_regulator,
0174                        adc->reg);
0175     if (ret)
0176         return ret;
0177 
0178     mutex_init(&adc->lock);
0179 
0180     return devm_iio_device_register(&spi->dev, indio_dev);
0181 }
0182 
0183 static const struct of_device_id adc128_of_match[] = {
0184     { .compatible = "ti,adc128s052", },
0185     { .compatible = "ti,adc122s021", },
0186     { .compatible = "ti,adc122s051", },
0187     { .compatible = "ti,adc122s101", },
0188     { .compatible = "ti,adc124s021", },
0189     { .compatible = "ti,adc124s051", },
0190     { .compatible = "ti,adc124s101", },
0191     { /* sentinel */ },
0192 };
0193 MODULE_DEVICE_TABLE(of, adc128_of_match);
0194 
0195 static const struct spi_device_id adc128_id[] = {
0196     { "adc128s052", 0 },    /* index into adc128_config */
0197     { "adc122s021", 1 },
0198     { "adc122s051", 1 },
0199     { "adc122s101", 1 },
0200     { "adc124s021", 2 },
0201     { "adc124s051", 2 },
0202     { "adc124s101", 2 },
0203     { }
0204 };
0205 MODULE_DEVICE_TABLE(spi, adc128_id);
0206 
0207 #ifdef CONFIG_ACPI
0208 static const struct acpi_device_id adc128_acpi_match[] = {
0209     { "AANT1280", 2 }, /* ADC124S021 compatible ACPI ID */
0210     { }
0211 };
0212 MODULE_DEVICE_TABLE(acpi, adc128_acpi_match);
0213 #endif
0214 
0215 static struct spi_driver adc128_driver = {
0216     .driver = {
0217         .name = "adc128s052",
0218         .of_match_table = adc128_of_match,
0219         .acpi_match_table = ACPI_PTR(adc128_acpi_match),
0220     },
0221     .probe = adc128_probe,
0222     .id_table = adc128_id,
0223 };
0224 module_spi_driver(adc128_driver);
0225 
0226 MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
0227 MODULE_DESCRIPTION("Texas Instruments ADC128S052");
0228 MODULE_LICENSE("GPL v2");