blob: b7c7cba2dc0921d97a81c0e2411c9294bc57cf29 [file] [log] [blame]
Angelo Compagnucci913b86462014-03-08 18:38:00 +00001/*
2 * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com>
3 *
Oliver Stäblerb41fa862015-12-09 10:24:04 +01004 * Driver for Texas Instruments' ADC128S052, ADC122S021 and ADC124S021 ADC chip.
Urs Fässler2a67dfb2015-05-18 15:22:44 +02005 * Datasheets can be found here:
Angelo Compagnucci913b86462014-03-08 18:38:00 +00006 * http://www.ti.com/lit/ds/symlink/adc128s052.pdf
Urs Fässler2a67dfb2015-05-18 15:22:44 +02007 * http://www.ti.com/lit/ds/symlink/adc122s021.pdf
Oliver Stäblerb41fa862015-12-09 10:24:04 +01008 * http://www.ti.com/lit/ds/symlink/adc124s021.pdf
Angelo Compagnucci913b86462014-03-08 18:38:00 +00009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/err.h>
16#include <linux/spi/spi.h>
17#include <linux/module.h>
18#include <linux/iio/iio.h>
19#include <linux/regulator/consumer.h>
20
Urs Fässler2a67dfb2015-05-18 15:22:44 +020021struct adc128_configuration {
22 const struct iio_chan_spec *channels;
23 u8 num_channels;
24};
25
Angelo Compagnucci913b86462014-03-08 18:38:00 +000026struct adc128 {
27 struct spi_device *spi;
28
29 struct regulator *reg;
30 struct mutex lock;
31
32 u8 buffer[2] ____cacheline_aligned;
33};
34
35static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
36{
37 int ret;
38
39 mutex_lock(&adc->lock);
40
41 adc->buffer[0] = channel << 3;
42 adc->buffer[1] = 0;
43
44 ret = spi_write(adc->spi, &adc->buffer, 2);
45 if (ret < 0) {
46 mutex_unlock(&adc->lock);
47 return ret;
48 }
49
50 ret = spi_read(adc->spi, &adc->buffer, 2);
51
52 mutex_unlock(&adc->lock);
53
54 if (ret < 0)
55 return ret;
56
57 return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF);
58}
59
60static int adc128_read_raw(struct iio_dev *indio_dev,
61 struct iio_chan_spec const *channel, int *val,
62 int *val2, long mask)
63{
64 struct adc128 *adc = iio_priv(indio_dev);
65 int ret;
66
67 switch (mask) {
68 case IIO_CHAN_INFO_RAW:
69
70 ret = adc128_adc_conversion(adc, channel->channel);
71 if (ret < 0)
72 return ret;
73
74 *val = ret;
75 return IIO_VAL_INT;
76
77 case IIO_CHAN_INFO_SCALE:
78
79 ret = regulator_get_voltage(adc->reg);
80 if (ret < 0)
81 return ret;
82
83 *val = ret / 1000;
84 *val2 = 12;
85 return IIO_VAL_FRACTIONAL_LOG2;
86
87 default:
88 return -EINVAL;
89 }
90
91}
92
93#define ADC128_VOLTAGE_CHANNEL(num) \
94 { \
95 .type = IIO_VOLTAGE, \
96 .indexed = 1, \
97 .channel = (num), \
98 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
99 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
100 }
101
Urs Fässler2a67dfb2015-05-18 15:22:44 +0200102static const struct iio_chan_spec adc128s052_channels[] = {
Angelo Compagnucci913b86462014-03-08 18:38:00 +0000103 ADC128_VOLTAGE_CHANNEL(0),
104 ADC128_VOLTAGE_CHANNEL(1),
105 ADC128_VOLTAGE_CHANNEL(2),
106 ADC128_VOLTAGE_CHANNEL(3),
107 ADC128_VOLTAGE_CHANNEL(4),
108 ADC128_VOLTAGE_CHANNEL(5),
109 ADC128_VOLTAGE_CHANNEL(6),
110 ADC128_VOLTAGE_CHANNEL(7),
111};
112
Urs Fässler2a67dfb2015-05-18 15:22:44 +0200113static const struct iio_chan_spec adc122s021_channels[] = {
114 ADC128_VOLTAGE_CHANNEL(0),
115 ADC128_VOLTAGE_CHANNEL(1),
116};
117
Oliver Stäblerb41fa862015-12-09 10:24:04 +0100118static const struct iio_chan_spec adc124s021_channels[] = {
119 ADC128_VOLTAGE_CHANNEL(0),
120 ADC128_VOLTAGE_CHANNEL(1),
121 ADC128_VOLTAGE_CHANNEL(2),
122 ADC128_VOLTAGE_CHANNEL(3),
123};
124
Urs Fässler2a67dfb2015-05-18 15:22:44 +0200125static const struct adc128_configuration adc128_config[] = {
126 { adc128s052_channels, ARRAY_SIZE(adc128s052_channels) },
127 { adc122s021_channels, ARRAY_SIZE(adc122s021_channels) },
Oliver Stäblerb41fa862015-12-09 10:24:04 +0100128 { adc124s021_channels, ARRAY_SIZE(adc124s021_channels) },
Urs Fässler2a67dfb2015-05-18 15:22:44 +0200129};
130
Angelo Compagnucci913b86462014-03-08 18:38:00 +0000131static const struct iio_info adc128_info = {
132 .read_raw = adc128_read_raw,
133 .driver_module = THIS_MODULE,
134};
135
136static int adc128_probe(struct spi_device *spi)
137{
138 struct iio_dev *indio_dev;
139 struct adc128 *adc;
Urs Fässler2a67dfb2015-05-18 15:22:44 +0200140 int config = spi_get_device_id(spi)->driver_data;
Angelo Compagnucci913b86462014-03-08 18:38:00 +0000141 int ret;
142
143 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
144 if (!indio_dev)
145 return -ENOMEM;
146
147 adc = iio_priv(indio_dev);
148 adc->spi = spi;
149
150 spi_set_drvdata(spi, indio_dev);
151
152 indio_dev->dev.parent = &spi->dev;
Matt Ranostayb541eaf2016-07-02 17:26:33 -0700153 indio_dev->dev.of_node = spi->dev.of_node;
Angelo Compagnucci913b86462014-03-08 18:38:00 +0000154 indio_dev->name = spi_get_device_id(spi)->name;
155 indio_dev->modes = INDIO_DIRECT_MODE;
156 indio_dev->info = &adc128_info;
157
Urs Fässler2a67dfb2015-05-18 15:22:44 +0200158 indio_dev->channels = adc128_config[config].channels;
159 indio_dev->num_channels = adc128_config[config].num_channels;
Angelo Compagnucci913b86462014-03-08 18:38:00 +0000160
161 adc->reg = devm_regulator_get(&spi->dev, "vref");
162 if (IS_ERR(adc->reg))
163 return PTR_ERR(adc->reg);
164
165 ret = regulator_enable(adc->reg);
166 if (ret < 0)
167 return ret;
168
169 mutex_init(&adc->lock);
170
171 ret = iio_device_register(indio_dev);
Christophe JAILLET42518692021-08-21 12:37:24 +0200172 if (ret)
173 goto err_disable_regulator;
Angelo Compagnucci913b86462014-03-08 18:38:00 +0000174
Christophe JAILLET42518692021-08-21 12:37:24 +0200175 return 0;
176
177err_disable_regulator:
178 regulator_disable(adc->reg);
Angelo Compagnucci913b86462014-03-08 18:38:00 +0000179 return ret;
180}
181
182static int adc128_remove(struct spi_device *spi)
183{
184 struct iio_dev *indio_dev = spi_get_drvdata(spi);
185 struct adc128 *adc = iio_priv(indio_dev);
186
187 iio_device_unregister(indio_dev);
188 regulator_disable(adc->reg);
189
190 return 0;
191}
192
Javier Martinez Canillas9e611c92015-08-20 09:07:28 +0200193static const struct of_device_id adc128_of_match[] = {
194 { .compatible = "ti,adc128s052", },
195 { .compatible = "ti,adc122s021", },
Oliver Stäblerb41fa862015-12-09 10:24:04 +0100196 { .compatible = "ti,adc124s021", },
Javier Martinez Canillas9e611c92015-08-20 09:07:28 +0200197 { /* sentinel */ },
198};
199MODULE_DEVICE_TABLE(of, adc128_of_match);
200
Angelo Compagnucci913b86462014-03-08 18:38:00 +0000201static const struct spi_device_id adc128_id[] = {
Urs Fässler2a67dfb2015-05-18 15:22:44 +0200202 { "adc128s052", 0}, /* index into adc128_config */
203 { "adc122s021", 1},
Oliver Stäblerb41fa862015-12-09 10:24:04 +0100204 { "adc124s021", 2},
Angelo Compagnucci913b86462014-03-08 18:38:00 +0000205 { }
206};
207MODULE_DEVICE_TABLE(spi, adc128_id);
208
209static struct spi_driver adc128_driver = {
210 .driver = {
211 .name = "adc128s052",
Javier Martinez Canillas9e611c92015-08-20 09:07:28 +0200212 .of_match_table = of_match_ptr(adc128_of_match),
Angelo Compagnucci913b86462014-03-08 18:38:00 +0000213 },
214 .probe = adc128_probe,
215 .remove = adc128_remove,
216 .id_table = adc128_id,
217};
218module_spi_driver(adc128_driver);
219
220MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
221MODULE_DESCRIPTION("Texas Instruments ADC128S052");
222MODULE_LICENSE("GPL v2");