2 * Copyright (C) 2014 Angelo Compagnucci <angelo.compagnucci@gmail.com>
4 * Driver for Texas Instruments' ADC128S052 ADC chip.
5 * Datasheet can be found here:
6 * http://www.ti.com/lit/ds/symlink/adc128s052.pdf
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/err.h>
14 #include <linux/spi/spi.h>
15 #include <linux/module.h>
16 #include <linux/iio/iio.h>
17 #include <linux/regulator/consumer.h>
20 struct spi_device
*spi
;
22 struct regulator
*reg
;
25 u8 buffer
[2] ____cacheline_aligned
;
28 static int adc128_adc_conversion(struct adc128
*adc
, u8 channel
)
32 mutex_lock(&adc
->lock
);
34 adc
->buffer
[0] = channel
<< 3;
37 ret
= spi_write(adc
->spi
, &adc
->buffer
, 2);
39 mutex_unlock(&adc
->lock
);
43 ret
= spi_read(adc
->spi
, &adc
->buffer
, 2);
45 mutex_unlock(&adc
->lock
);
50 return ((adc
->buffer
[0] << 8 | adc
->buffer
[1]) & 0xFFF);
53 static int adc128_read_raw(struct iio_dev
*indio_dev
,
54 struct iio_chan_spec
const *channel
, int *val
,
57 struct adc128
*adc
= iio_priv(indio_dev
);
61 case IIO_CHAN_INFO_RAW
:
63 ret
= adc128_adc_conversion(adc
, channel
->channel
);
70 case IIO_CHAN_INFO_SCALE
:
72 ret
= regulator_get_voltage(adc
->reg
);
78 return IIO_VAL_FRACTIONAL_LOG2
;
86 #define ADC128_VOLTAGE_CHANNEL(num) \
88 .type = IIO_VOLTAGE, \
91 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
92 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
95 static const struct iio_chan_spec adc128_channels
[] = {
96 ADC128_VOLTAGE_CHANNEL(0),
97 ADC128_VOLTAGE_CHANNEL(1),
98 ADC128_VOLTAGE_CHANNEL(2),
99 ADC128_VOLTAGE_CHANNEL(3),
100 ADC128_VOLTAGE_CHANNEL(4),
101 ADC128_VOLTAGE_CHANNEL(5),
102 ADC128_VOLTAGE_CHANNEL(6),
103 ADC128_VOLTAGE_CHANNEL(7),
106 static const struct iio_info adc128_info
= {
107 .read_raw
= adc128_read_raw
,
108 .driver_module
= THIS_MODULE
,
111 static int adc128_probe(struct spi_device
*spi
)
113 struct iio_dev
*indio_dev
;
117 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*adc
));
121 adc
= iio_priv(indio_dev
);
124 spi_set_drvdata(spi
, indio_dev
);
126 indio_dev
->dev
.parent
= &spi
->dev
;
127 indio_dev
->name
= spi_get_device_id(spi
)->name
;
128 indio_dev
->modes
= INDIO_DIRECT_MODE
;
129 indio_dev
->info
= &adc128_info
;
131 indio_dev
->channels
= adc128_channels
;
132 indio_dev
->num_channels
= ARRAY_SIZE(adc128_channels
);
134 adc
->reg
= devm_regulator_get(&spi
->dev
, "vref");
135 if (IS_ERR(adc
->reg
))
136 return PTR_ERR(adc
->reg
);
138 ret
= regulator_enable(adc
->reg
);
142 mutex_init(&adc
->lock
);
144 ret
= iio_device_register(indio_dev
);
149 static int adc128_remove(struct spi_device
*spi
)
151 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
152 struct adc128
*adc
= iio_priv(indio_dev
);
154 iio_device_unregister(indio_dev
);
155 regulator_disable(adc
->reg
);
160 static const struct spi_device_id adc128_id
[] = {
164 MODULE_DEVICE_TABLE(spi
, adc128_id
);
166 static struct spi_driver adc128_driver
= {
168 .name
= "adc128s052",
169 .owner
= THIS_MODULE
,
171 .probe
= adc128_probe
,
172 .remove
= adc128_remove
,
173 .id_table
= adc128_id
,
175 module_spi_driver(adc128_driver
);
177 MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>");
178 MODULE_DESCRIPTION("Texas Instruments ADC128S052");
179 MODULE_LICENSE("GPL v2");