Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ad7780.c
Go to the documentation of this file.
1 /*
2  * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
3  *
4  * Copyright 2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8 
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/spi/spi.h>
16 #include <linux/err.h>
17 #include <linux/sched.h>
18 #include <linux/gpio.h>
19 #include <linux/module.h>
20 
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
24 
25 #include "ad7780.h"
26 
27 #define AD7780_RDY (1 << 7)
28 #define AD7780_FILTER (1 << 6)
29 #define AD7780_ERR (1 << 5)
30 #define AD7780_ID1 (1 << 4)
31 #define AD7780_ID0 (1 << 3)
32 #define AD7780_GAIN (1 << 2)
33 #define AD7780_PAT1 (1 << 1)
34 #define AD7780_PAT0 (1 << 0)
35 
38  unsigned int pattern_mask;
39  unsigned int pattern;
40 };
41 
42 struct ad7780_state {
43  const struct ad7780_chip_info *chip_info;
44  struct regulator *reg;
46  unsigned int gain;
48 
50 };
51 
57 };
58 
59 static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
60 {
61  return container_of(sd, struct ad7780_state, sd);
62 }
63 
64 static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
66 {
67  struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
68  unsigned val;
69 
70  switch (mode) {
71  case AD_SD_MODE_SINGLE:
73  val = 1;
74  break;
75  default:
76  val = 0;
77  break;
78  }
79 
80  if (gpio_is_valid(st->powerdown_gpio))
82 
83  return 0;
84 }
85 
86 static int ad7780_read_raw(struct iio_dev *indio_dev,
87  struct iio_chan_spec const *chan,
88  int *val,
89  int *val2,
90  long m)
91 {
92  struct ad7780_state *st = iio_priv(indio_dev);
93  unsigned long scale_uv;
94 
95  switch (m) {
96  case IIO_CHAN_INFO_RAW:
97  return ad_sigma_delta_single_conversion(indio_dev, chan, val);
99  scale_uv = (st->int_vref_mv * 100000 * st->gain)
100  >> (chan->scan_type.realbits - 1);
101  *val = scale_uv / 100000;
102  *val2 = (scale_uv % 100000) * 10;
103  return IIO_VAL_INT_PLUS_MICRO;
105  *val -= (1 << (chan->scan_type.realbits - 1));
106  return IIO_VAL_INT;
107  }
108 
109  return -EINVAL;
110 }
111 
112 static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
113  unsigned int raw_sample)
114 {
115  struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
116  const struct ad7780_chip_info *chip_info = st->chip_info;
117 
118  if ((raw_sample & AD7780_ERR) ||
119  ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
120  return -EIO;
121 
122  if (raw_sample & AD7780_GAIN)
123  st->gain = 1;
124  else
125  st->gain = 128;
126 
127  return 0;
128 }
129 
130 static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
131  .set_mode = ad7780_set_mode,
132  .postprocess_sample = ad7780_postprocess_sample,
133  .has_registers = false,
134 };
135 
136 #define AD7780_CHANNEL(bits, wordsize) \
137  AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
138 
139 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
140  [ID_AD7170] = {
141  .channel = AD7780_CHANNEL(12, 24),
142  .pattern = 0x5,
143  .pattern_mask = 0x7,
144  },
145  [ID_AD7171] = {
146  .channel = AD7780_CHANNEL(16, 24),
147  .pattern = 0x5,
148  .pattern_mask = 0x7,
149  },
150  [ID_AD7780] = {
151  .channel = AD7780_CHANNEL(24, 32),
152  .pattern = 0x1,
153  .pattern_mask = 0x3,
154  },
155  [ID_AD7781] = {
156  .channel = AD7780_CHANNEL(20, 32),
157  .pattern = 0x1,
158  .pattern_mask = 0x3,
159  },
160 };
161 
162 static const struct iio_info ad7780_info = {
163  .read_raw = &ad7780_read_raw,
164  .driver_module = THIS_MODULE,
165 };
166 
167 static int __devinit ad7780_probe(struct spi_device *spi)
168 {
169  struct ad7780_platform_data *pdata = spi->dev.platform_data;
170  struct ad7780_state *st;
171  struct iio_dev *indio_dev;
172  int ret, voltage_uv = 0;
173 
174  indio_dev = iio_device_alloc(sizeof(*st));
175  if (indio_dev == NULL)
176  return -ENOMEM;
177 
178  st = iio_priv(indio_dev);
179  st->gain = 1;
180 
181  ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
182 
183  st->reg = regulator_get(&spi->dev, "vcc");
184  if (!IS_ERR(st->reg)) {
185  ret = regulator_enable(st->reg);
186  if (ret)
187  goto error_put_reg;
188 
189  voltage_uv = regulator_get_voltage(st->reg);
190  }
191 
192  st->chip_info =
193  &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
194 
195  if (pdata && pdata->vref_mv)
196  st->int_vref_mv = pdata->vref_mv;
197  else if (voltage_uv)
198  st->int_vref_mv = voltage_uv / 1000;
199  else
200  dev_warn(&spi->dev, "reference voltage unspecified\n");
201 
202  spi_set_drvdata(spi, indio_dev);
203 
204  indio_dev->dev.parent = &spi->dev;
205  indio_dev->name = spi_get_device_id(spi)->name;
206  indio_dev->modes = INDIO_DIRECT_MODE;
207  indio_dev->channels = &st->chip_info->channel;
208  indio_dev->num_channels = 1;
209  indio_dev->info = &ad7780_info;
210 
211  if (pdata && gpio_is_valid(pdata->gpio_pdrst)) {
212 
214  "AD7780 /PDRST");
215  if (ret) {
216  dev_err(&spi->dev, "failed to request GPIO PDRST\n");
217  goto error_disable_reg;
218  }
219  st->powerdown_gpio = pdata->gpio_pdrst;
220  } else {
221  st->powerdown_gpio = -1;
222  }
223 
224  ret = ad_sd_setup_buffer_and_trigger(indio_dev);
225  if (ret)
226  goto error_free_gpio;
227 
228  ret = iio_device_register(indio_dev);
229  if (ret)
230  goto error_cleanup_buffer_and_trigger;
231 
232  return 0;
233 
234 error_cleanup_buffer_and_trigger:
236 error_free_gpio:
237  if (pdata && gpio_is_valid(pdata->gpio_pdrst))
238  gpio_free(pdata->gpio_pdrst);
239 error_disable_reg:
240  if (!IS_ERR(st->reg))
241  regulator_disable(st->reg);
242 error_put_reg:
243  if (!IS_ERR(st->reg))
244  regulator_put(st->reg);
245 
246  iio_device_free(indio_dev);
247 
248  return ret;
249 }
250 
251 static int __devexit ad7780_remove(struct spi_device *spi)
252 {
253  struct iio_dev *indio_dev = spi_get_drvdata(spi);
254  struct ad7780_state *st = iio_priv(indio_dev);
255 
256  iio_device_unregister(indio_dev);
258 
259  if (gpio_is_valid(st->powerdown_gpio))
261 
262  if (!IS_ERR(st->reg)) {
263  regulator_disable(st->reg);
264  regulator_put(st->reg);
265  }
266  iio_device_free(indio_dev);
267 
268  return 0;
269 }
270 
271 static const struct spi_device_id ad7780_id[] = {
272  {"ad7170", ID_AD7170},
273  {"ad7171", ID_AD7171},
274  {"ad7780", ID_AD7780},
275  {"ad7781", ID_AD7781},
276  {}
277 };
278 MODULE_DEVICE_TABLE(spi, ad7780_id);
279 
280 static struct spi_driver ad7780_driver = {
281  .driver = {
282  .name = "ad7780",
283  .owner = THIS_MODULE,
284  },
285  .probe = ad7780_probe,
286  .remove = __devexit_p(ad7780_remove),
287  .id_table = ad7780_id,
288 };
289 module_spi_driver(ad7780_driver);
290 
291 MODULE_AUTHOR("Michael Hennerich <[email protected]>");
292 MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
293 MODULE_LICENSE("GPL v2");