2 * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com>
4 * Driver for Texas Instruments' ADC128S052, ADC122S021 and ADC124S021 ADC chip.
5 * Datasheets can be found here:
6 * http://www.ti.com/lit/ds/symlink/adc128s052.pdf
7 * http://www.ti.com/lit/ds/symlink/adc122s021.pdf
8 * http://www.ti.com/lit/ds/symlink/adc124s021.pdf
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.
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>
21 struct adc128_configuration
{
22 const struct iio_chan_spec
*channels
;
27 struct spi_device
*spi
;
29 struct regulator
*reg
;
32 u8 buffer
[2] ____cacheline_aligned
;
35 static int adc128_adc_conversion(struct adc128
*adc
, u8 channel
)
39 mutex_lock(&adc
->lock
);
41 adc
->buffer
[0] = channel
<< 3;
44 ret
= spi_write(adc
->spi
, &adc
->buffer
, 2);
46 mutex_unlock(&adc
->lock
);
50 ret
= spi_read(adc
->spi
, &adc
->buffer
, 2);
52 mutex_unlock(&adc
->lock
);
57 return ((adc
->buffer
[0] << 8 | adc
->buffer
[1]) & 0xFFF);
60 static int adc128_read_raw(struct iio_dev
*indio_dev
,
61 struct iio_chan_spec
const *channel
, int *val
,
64 struct adc128
*adc
= iio_priv(indio_dev
);
68 case IIO_CHAN_INFO_RAW
:
70 ret
= adc128_adc_conversion(adc
, channel
->channel
);
77 case IIO_CHAN_INFO_SCALE
:
79 ret
= regulator_get_voltage(adc
->reg
);
85 return IIO_VAL_FRACTIONAL_LOG2
;
93 #define ADC128_VOLTAGE_CHANNEL(num) \
95 .type = IIO_VOLTAGE, \
98 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
99 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
102 static const struct iio_chan_spec adc128s052_channels
[] = {
103 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),
113 static const struct iio_chan_spec adc122s021_channels
[] = {
114 ADC128_VOLTAGE_CHANNEL(0),
115 ADC128_VOLTAGE_CHANNEL(1),
118 static 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),
125 static const struct adc128_configuration adc128_config
[] = {
126 { adc128s052_channels
, ARRAY_SIZE(adc128s052_channels
) },
127 { adc122s021_channels
, ARRAY_SIZE(adc122s021_channels
) },
128 { adc124s021_channels
, ARRAY_SIZE(adc124s021_channels
) },
131 static const struct iio_info adc128_info
= {
132 .read_raw
= adc128_read_raw
,
133 .driver_module
= THIS_MODULE
,
136 static int adc128_probe(struct spi_device
*spi
)
138 struct iio_dev
*indio_dev
;
140 int config
= spi_get_device_id(spi
)->driver_data
;
143 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
147 adc
= iio_priv(indio_dev
);
150 spi_set_drvdata(spi
, indio_dev
);
152 indio_dev
->dev
.parent
= &spi
->dev
;
153 indio_dev
->dev
.of_node
= spi
->dev
.of_node
;
154 indio_dev
->name
= spi_get_device_id(spi
)->name
;
155 indio_dev
->modes
= INDIO_DIRECT_MODE
;
156 indio_dev
->info
= &adc128_info
;
158 indio_dev
->channels
= adc128_config
[config
].channels
;
159 indio_dev
->num_channels
= adc128_config
[config
].num_channels
;
161 adc
->reg
= devm_regulator_get(&spi
->dev
, "vref");
162 if (IS_ERR(adc
->reg
))
163 return PTR_ERR(adc
->reg
);
165 ret
= regulator_enable(adc
->reg
);
169 mutex_init(&adc
->lock
);
171 ret
= iio_device_register(indio_dev
);
176 static int adc128_remove(struct spi_device
*spi
)
178 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
179 struct adc128
*adc
= iio_priv(indio_dev
);
181 iio_device_unregister(indio_dev
);
182 regulator_disable(adc
->reg
);
187 static const struct of_device_id adc128_of_match
[] = {
188 { .compatible
= "ti,adc128s052", },
189 { .compatible
= "ti,adc122s021", },
190 { .compatible
= "ti,adc124s021", },
193 MODULE_DEVICE_TABLE(of
, adc128_of_match
);
195 static const struct spi_device_id adc128_id
[] = {
196 { "adc128s052", 0}, /* index into adc128_config */
201 MODULE_DEVICE_TABLE(spi
, adc128_id
);
203 static struct spi_driver adc128_driver
= {
205 .name
= "adc128s052",
206 .of_match_table
= of_match_ptr(adc128_of_match
),
208 .probe
= adc128_probe
,
209 .remove
= adc128_remove
,
210 .id_table
= adc128_id
,
212 module_spi_driver(adc128_driver
);
214 MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
215 MODULE_DESCRIPTION("Texas Instruments ADC128S052");
216 MODULE_LICENSE("GPL v2");