NFS: Don't clear desc->pg_moreio in nfs_do_recoalesce()
[linux/fpc-iii.git] / drivers / iio / adc / ti-adc128s052.c
blob655cb564ec54af8696229853bf5f418389e49be9
1 /*
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>
19 struct adc128 {
20 struct spi_device *spi;
22 struct regulator *reg;
23 struct mutex lock;
25 u8 buffer[2] ____cacheline_aligned;
28 static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
30 int ret;
32 mutex_lock(&adc->lock);
34 adc->buffer[0] = channel << 3;
35 adc->buffer[1] = 0;
37 ret = spi_write(adc->spi, &adc->buffer, 2);
38 if (ret < 0) {
39 mutex_unlock(&adc->lock);
40 return ret;
43 ret = spi_read(adc->spi, &adc->buffer, 2);
45 mutex_unlock(&adc->lock);
47 if (ret < 0)
48 return ret;
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,
55 int *val2, long mask)
57 struct adc128 *adc = iio_priv(indio_dev);
58 int ret;
60 switch (mask) {
61 case IIO_CHAN_INFO_RAW:
63 ret = adc128_adc_conversion(adc, channel->channel);
64 if (ret < 0)
65 return ret;
67 *val = ret;
68 return IIO_VAL_INT;
70 case IIO_CHAN_INFO_SCALE:
72 ret = regulator_get_voltage(adc->reg);
73 if (ret < 0)
74 return ret;
76 *val = ret / 1000;
77 *val2 = 12;
78 return IIO_VAL_FRACTIONAL_LOG2;
80 default:
81 return -EINVAL;
86 #define ADC128_VOLTAGE_CHANNEL(num) \
87 { \
88 .type = IIO_VOLTAGE, \
89 .indexed = 1, \
90 .channel = (num), \
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;
114 struct adc128 *adc;
115 int ret;
117 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
118 if (!indio_dev)
119 return -ENOMEM;
121 adc = iio_priv(indio_dev);
122 adc->spi = spi;
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);
139 if (ret < 0)
140 return ret;
142 mutex_init(&adc->lock);
144 ret = iio_device_register(indio_dev);
146 return ret;
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);
157 return 0;
160 static const struct spi_device_id adc128_id[] = {
161 { "adc128s052", 0},
164 MODULE_DEVICE_TABLE(spi, adc128_id);
166 static struct spi_driver adc128_driver = {
167 .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");