Dan O'Donovan | 7d1d308 | 2018-10-25 16:35:42 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com> |
| 4 | * |
Oliver Stäbler | b41fa86 | 2015-12-09 10:24:04 +0100 | [diff] [blame] | 5 | * Driver for Texas Instruments' ADC128S052, ADC122S021 and ADC124S021 ADC chip. |
Urs Fässler | 2a67dfb | 2015-05-18 15:22:44 +0200 | [diff] [blame] | 6 | * Datasheets can be found here: |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 7 | * http://www.ti.com/lit/ds/symlink/adc128s052.pdf |
Urs Fässler | 2a67dfb | 2015-05-18 15:22:44 +0200 | [diff] [blame] | 8 | * http://www.ti.com/lit/ds/symlink/adc122s021.pdf |
Oliver Stäbler | b41fa86 | 2015-12-09 10:24:04 +0100 | [diff] [blame] | 9 | * http://www.ti.com/lit/ds/symlink/adc124s021.pdf |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Nicola Lunghi | bd5d54e | 2018-10-25 16:35:41 +0100 | [diff] [blame] | 12 | #include <linux/acpi.h> |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 13 | #include <linux/err.h> |
| 14 | #include <linux/spi/spi.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/iio/iio.h> |
Nicola Lunghi | bd5d54e | 2018-10-25 16:35:41 +0100 | [diff] [blame] | 17 | #include <linux/property.h> |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 18 | #include <linux/regulator/consumer.h> |
| 19 | |
Urs Fässler | 2a67dfb | 2015-05-18 15:22:44 +0200 | [diff] [blame] | 20 | struct adc128_configuration { |
| 21 | const struct iio_chan_spec *channels; |
| 22 | u8 num_channels; |
| 23 | }; |
| 24 | |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 25 | struct adc128 { |
| 26 | struct spi_device *spi; |
| 27 | |
| 28 | struct regulator *reg; |
| 29 | struct mutex lock; |
| 30 | |
| 31 | u8 buffer[2] ____cacheline_aligned; |
| 32 | }; |
| 33 | |
| 34 | static int adc128_adc_conversion(struct adc128 *adc, u8 channel) |
| 35 | { |
| 36 | int ret; |
| 37 | |
| 38 | mutex_lock(&adc->lock); |
| 39 | |
| 40 | adc->buffer[0] = channel << 3; |
| 41 | adc->buffer[1] = 0; |
| 42 | |
| 43 | ret = spi_write(adc->spi, &adc->buffer, 2); |
| 44 | if (ret < 0) { |
| 45 | mutex_unlock(&adc->lock); |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | ret = spi_read(adc->spi, &adc->buffer, 2); |
| 50 | |
| 51 | mutex_unlock(&adc->lock); |
| 52 | |
| 53 | if (ret < 0) |
| 54 | return ret; |
| 55 | |
| 56 | return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF); |
| 57 | } |
| 58 | |
| 59 | static int adc128_read_raw(struct iio_dev *indio_dev, |
| 60 | struct iio_chan_spec const *channel, int *val, |
| 61 | int *val2, long mask) |
| 62 | { |
| 63 | struct adc128 *adc = iio_priv(indio_dev); |
| 64 | int ret; |
| 65 | |
| 66 | switch (mask) { |
| 67 | case IIO_CHAN_INFO_RAW: |
| 68 | |
| 69 | ret = adc128_adc_conversion(adc, channel->channel); |
| 70 | if (ret < 0) |
| 71 | return ret; |
| 72 | |
| 73 | *val = ret; |
| 74 | return IIO_VAL_INT; |
| 75 | |
| 76 | case IIO_CHAN_INFO_SCALE: |
| 77 | |
| 78 | ret = regulator_get_voltage(adc->reg); |
| 79 | if (ret < 0) |
| 80 | return ret; |
| 81 | |
| 82 | *val = ret / 1000; |
| 83 | *val2 = 12; |
| 84 | return IIO_VAL_FRACTIONAL_LOG2; |
| 85 | |
| 86 | default: |
| 87 | return -EINVAL; |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | #define ADC128_VOLTAGE_CHANNEL(num) \ |
| 93 | { \ |
| 94 | .type = IIO_VOLTAGE, \ |
| 95 | .indexed = 1, \ |
| 96 | .channel = (num), \ |
| 97 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
| 98 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \ |
| 99 | } |
| 100 | |
Urs Fässler | 2a67dfb | 2015-05-18 15:22:44 +0200 | [diff] [blame] | 101 | static const struct iio_chan_spec adc128s052_channels[] = { |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 102 | ADC128_VOLTAGE_CHANNEL(0), |
| 103 | ADC128_VOLTAGE_CHANNEL(1), |
| 104 | ADC128_VOLTAGE_CHANNEL(2), |
| 105 | ADC128_VOLTAGE_CHANNEL(3), |
| 106 | ADC128_VOLTAGE_CHANNEL(4), |
| 107 | ADC128_VOLTAGE_CHANNEL(5), |
| 108 | ADC128_VOLTAGE_CHANNEL(6), |
| 109 | ADC128_VOLTAGE_CHANNEL(7), |
| 110 | }; |
| 111 | |
Urs Fässler | 2a67dfb | 2015-05-18 15:22:44 +0200 | [diff] [blame] | 112 | static const struct iio_chan_spec adc122s021_channels[] = { |
| 113 | ADC128_VOLTAGE_CHANNEL(0), |
| 114 | ADC128_VOLTAGE_CHANNEL(1), |
| 115 | }; |
| 116 | |
Oliver Stäbler | b41fa86 | 2015-12-09 10:24:04 +0100 | [diff] [blame] | 117 | static const struct iio_chan_spec adc124s021_channels[] = { |
| 118 | ADC128_VOLTAGE_CHANNEL(0), |
| 119 | ADC128_VOLTAGE_CHANNEL(1), |
| 120 | ADC128_VOLTAGE_CHANNEL(2), |
| 121 | ADC128_VOLTAGE_CHANNEL(3), |
| 122 | }; |
| 123 | |
Urs Fässler | 2a67dfb | 2015-05-18 15:22:44 +0200 | [diff] [blame] | 124 | static const struct adc128_configuration adc128_config[] = { |
| 125 | { adc128s052_channels, ARRAY_SIZE(adc128s052_channels) }, |
| 126 | { adc122s021_channels, ARRAY_SIZE(adc122s021_channels) }, |
Oliver Stäbler | b41fa86 | 2015-12-09 10:24:04 +0100 | [diff] [blame] | 127 | { adc124s021_channels, ARRAY_SIZE(adc124s021_channels) }, |
Urs Fässler | 2a67dfb | 2015-05-18 15:22:44 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 130 | static const struct iio_info adc128_info = { |
| 131 | .read_raw = adc128_read_raw, |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | static int adc128_probe(struct spi_device *spi) |
| 135 | { |
| 136 | struct iio_dev *indio_dev; |
Nicola Lunghi | bd5d54e | 2018-10-25 16:35:41 +0100 | [diff] [blame] | 137 | unsigned int config; |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 138 | struct adc128 *adc; |
| 139 | int ret; |
| 140 | |
Nicola Lunghi | bd5d54e | 2018-10-25 16:35:41 +0100 | [diff] [blame] | 141 | if (dev_fwnode(&spi->dev)) |
| 142 | config = (unsigned long) device_get_match_data(&spi->dev); |
| 143 | else |
| 144 | config = spi_get_device_id(spi)->driver_data; |
| 145 | |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 146 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); |
| 147 | if (!indio_dev) |
| 148 | return -ENOMEM; |
| 149 | |
| 150 | adc = iio_priv(indio_dev); |
| 151 | adc->spi = spi; |
| 152 | |
| 153 | spi_set_drvdata(spi, indio_dev); |
| 154 | |
| 155 | indio_dev->dev.parent = &spi->dev; |
Matt Ranostay | b541eaf | 2016-07-02 17:26:33 -0700 | [diff] [blame] | 156 | indio_dev->dev.of_node = spi->dev.of_node; |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 157 | indio_dev->name = spi_get_device_id(spi)->name; |
| 158 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 159 | indio_dev->info = &adc128_info; |
| 160 | |
Urs Fässler | 2a67dfb | 2015-05-18 15:22:44 +0200 | [diff] [blame] | 161 | indio_dev->channels = adc128_config[config].channels; |
| 162 | indio_dev->num_channels = adc128_config[config].num_channels; |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 163 | |
| 164 | adc->reg = devm_regulator_get(&spi->dev, "vref"); |
| 165 | if (IS_ERR(adc->reg)) |
| 166 | return PTR_ERR(adc->reg); |
| 167 | |
| 168 | ret = regulator_enable(adc->reg); |
| 169 | if (ret < 0) |
| 170 | return ret; |
| 171 | |
| 172 | mutex_init(&adc->lock); |
| 173 | |
| 174 | ret = iio_device_register(indio_dev); |
| 175 | |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | static int adc128_remove(struct spi_device *spi) |
| 180 | { |
| 181 | struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 182 | struct adc128 *adc = iio_priv(indio_dev); |
| 183 | |
| 184 | iio_device_unregister(indio_dev); |
| 185 | regulator_disable(adc->reg); |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Javier Martinez Canillas | 9e611c9 | 2015-08-20 09:07:28 +0200 | [diff] [blame] | 190 | static const struct of_device_id adc128_of_match[] = { |
| 191 | { .compatible = "ti,adc128s052", }, |
| 192 | { .compatible = "ti,adc122s021", }, |
Javier Arteaga | 37cd3c8 | 2018-10-25 16:35:40 +0100 | [diff] [blame] | 193 | { .compatible = "ti,adc122s051", }, |
| 194 | { .compatible = "ti,adc122s101", }, |
Oliver Stäbler | b41fa86 | 2015-12-09 10:24:04 +0100 | [diff] [blame] | 195 | { .compatible = "ti,adc124s021", }, |
Javier Arteaga | 37cd3c8 | 2018-10-25 16:35:40 +0100 | [diff] [blame] | 196 | { .compatible = "ti,adc124s051", }, |
| 197 | { .compatible = "ti,adc124s101", }, |
Javier Martinez Canillas | 9e611c9 | 2015-08-20 09:07:28 +0200 | [diff] [blame] | 198 | { /* sentinel */ }, |
| 199 | }; |
| 200 | MODULE_DEVICE_TABLE(of, adc128_of_match); |
| 201 | |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 202 | static const struct spi_device_id adc128_id[] = { |
Javier Arteaga | 37cd3c8 | 2018-10-25 16:35:40 +0100 | [diff] [blame] | 203 | { "adc128s052", 0 }, /* index into adc128_config */ |
| 204 | { "adc122s021", 1 }, |
| 205 | { "adc122s051", 1 }, |
| 206 | { "adc122s101", 1 }, |
| 207 | { "adc124s021", 2 }, |
| 208 | { "adc124s051", 2 }, |
| 209 | { "adc124s101", 2 }, |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 210 | { } |
| 211 | }; |
| 212 | MODULE_DEVICE_TABLE(spi, adc128_id); |
| 213 | |
Nicola Lunghi | bd5d54e | 2018-10-25 16:35:41 +0100 | [diff] [blame] | 214 | #ifdef CONFIG_ACPI |
| 215 | static const struct acpi_device_id adc128_acpi_match[] = { |
| 216 | { "AANT1280", 2 }, /* ADC124S021 compatible ACPI ID */ |
| 217 | { } |
| 218 | }; |
| 219 | MODULE_DEVICE_TABLE(acpi, adc128_acpi_match); |
| 220 | #endif |
| 221 | |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 222 | static struct spi_driver adc128_driver = { |
| 223 | .driver = { |
| 224 | .name = "adc128s052", |
Javier Martinez Canillas | 9e611c9 | 2015-08-20 09:07:28 +0200 | [diff] [blame] | 225 | .of_match_table = of_match_ptr(adc128_of_match), |
Nicola Lunghi | bd5d54e | 2018-10-25 16:35:41 +0100 | [diff] [blame] | 226 | .acpi_match_table = ACPI_PTR(adc128_acpi_match), |
Angelo Compagnucci | 913b8646 | 2014-03-08 18:38:00 +0000 | [diff] [blame] | 227 | }, |
| 228 | .probe = adc128_probe, |
| 229 | .remove = adc128_remove, |
| 230 | .id_table = adc128_id, |
| 231 | }; |
| 232 | module_spi_driver(adc128_driver); |
| 233 | |
| 234 | MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>"); |
| 235 | MODULE_DESCRIPTION("Texas Instruments ADC128S052"); |
| 236 | MODULE_LICENSE("GPL v2"); |