Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ad7298_core.c
Go to the documentation of this file.
1 /*
2  * AD7298 SPI ADC driver
3  *
4  * Copyright 2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8 
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/sysfs.h>
13 #include <linux/spi/spi.h>
15 #include <linux/err.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 #include <linux/iio/buffer.h>
22 
23 #include "ad7298.h"
24 
25 #define AD7298_V_CHAN(index) \
26  { \
27  .type = IIO_VOLTAGE, \
28  .indexed = 1, \
29  .channel = index, \
30  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
31  IIO_CHAN_INFO_SCALE_SHARED_BIT, \
32  .address = index, \
33  .scan_index = index, \
34  .scan_type = { \
35  .sign = 'u', \
36  .realbits = 12, \
37  .storagebits = 16, \
38  }, \
39  }
40 
41 static const struct iio_chan_spec ad7298_channels[] = {
42  {
43  .type = IIO_TEMP,
44  .indexed = 1,
45  .channel = 0,
46  .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
48  .address = AD7298_CH_TEMP,
49  .scan_index = -1,
50  .scan_type = {
51  .sign = 's',
52  .realbits = 32,
53  .storagebits = 32,
54  },
55  },
56  AD7298_V_CHAN(0),
57  AD7298_V_CHAN(1),
58  AD7298_V_CHAN(2),
59  AD7298_V_CHAN(3),
60  AD7298_V_CHAN(4),
61  AD7298_V_CHAN(5),
62  AD7298_V_CHAN(6),
63  AD7298_V_CHAN(7),
65 };
66 
67 static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch)
68 {
69  int ret;
70  st->tx_buf[0] = cpu_to_be16(AD7298_WRITE | st->ext_ref |
71  (AD7298_CH(0) >> ch));
72 
73  ret = spi_sync(st->spi, &st->scan_single_msg);
74  if (ret)
75  return ret;
76 
77  return be16_to_cpu(st->rx_buf[0]);
78 }
79 
80 static int ad7298_scan_temp(struct ad7298_state *st, int *val)
81 {
82  int tmp, ret;
83  __be16 buf;
84 
86  AD7298_TAVG | st->ext_ref);
87 
88  ret = spi_write(st->spi, (u8 *)&buf, 2);
89  if (ret)
90  return ret;
91 
92  buf = cpu_to_be16(0);
93 
94  ret = spi_write(st->spi, (u8 *)&buf, 2);
95  if (ret)
96  return ret;
97 
98  usleep_range(101, 1000); /* sleep > 100us */
99 
100  ret = spi_read(st->spi, (u8 *)&buf, 2);
101  if (ret)
102  return ret;
103 
104  tmp = be16_to_cpu(buf) & RES_MASK(AD7298_BITS);
105 
106  /*
107  * One LSB of the ADC corresponds to 0.25 deg C.
108  * The temperature reading is in 12-bit twos complement format
109  */
110 
111  if (tmp & (1 << (AD7298_BITS - 1))) {
112  tmp = (4096 - tmp) * 250;
113  tmp -= (2 * tmp);
114 
115  } else {
116  tmp *= 250; /* temperature in milli degrees Celsius */
117  }
118 
119  *val = tmp;
120 
121  return 0;
122 }
123 
124 static int ad7298_read_raw(struct iio_dev *indio_dev,
125  struct iio_chan_spec const *chan,
126  int *val,
127  int *val2,
128  long m)
129 {
130  int ret;
131  struct ad7298_state *st = iio_priv(indio_dev);
132  unsigned int scale_uv;
133 
134  switch (m) {
135  case IIO_CHAN_INFO_RAW:
136  mutex_lock(&indio_dev->mlock);
137  if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
138  ret = -EBUSY;
139  } else {
140  if (chan->address == AD7298_CH_TEMP)
141  ret = ad7298_scan_temp(st, val);
142  else
143  ret = ad7298_scan_direct(st, chan->address);
144  }
145  mutex_unlock(&indio_dev->mlock);
146 
147  if (ret < 0)
148  return ret;
149 
150  if (chan->address != AD7298_CH_TEMP)
151  *val = ret & RES_MASK(AD7298_BITS);
152 
153  return IIO_VAL_INT;
154  case IIO_CHAN_INFO_SCALE:
155  switch (chan->type) {
156  case IIO_VOLTAGE:
157  scale_uv = (st->int_vref_mv * 1000) >> AD7298_BITS;
158  *val = scale_uv / 1000;
159  *val2 = (scale_uv % 1000) * 1000;
160  return IIO_VAL_INT_PLUS_MICRO;
161  case IIO_TEMP:
162  *val = 1;
163  *val2 = 0;
164  return IIO_VAL_INT_PLUS_MICRO;
165  default:
166  return -EINVAL;
167  }
168  }
169  return -EINVAL;
170 }
171 
172 static const struct iio_info ad7298_info = {
173  .read_raw = &ad7298_read_raw,
174  .update_scan_mode = ad7298_update_scan_mode,
175  .driver_module = THIS_MODULE,
176 };
177 
178 static int __devinit ad7298_probe(struct spi_device *spi)
179 {
180  struct ad7298_platform_data *pdata = spi->dev.platform_data;
181  struct ad7298_state *st;
182  int ret;
183  struct iio_dev *indio_dev = iio_device_alloc(sizeof(*st));
184 
185  if (indio_dev == NULL)
186  return -ENOMEM;
187 
188  st = iio_priv(indio_dev);
189 
190  st->reg = regulator_get(&spi->dev, "vcc");
191  if (!IS_ERR(st->reg)) {
192  ret = regulator_enable(st->reg);
193  if (ret)
194  goto error_put_reg;
195  }
196 
197  spi_set_drvdata(spi, indio_dev);
198 
199  st->spi = spi;
200 
201  indio_dev->name = spi_get_device_id(spi)->name;
202  indio_dev->dev.parent = &spi->dev;
203  indio_dev->modes = INDIO_DIRECT_MODE;
204  indio_dev->channels = ad7298_channels;
205  indio_dev->num_channels = ARRAY_SIZE(ad7298_channels);
206  indio_dev->info = &ad7298_info;
207 
208  /* Setup default message */
209 
210  st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
211  st->scan_single_xfer[0].len = 2;
212  st->scan_single_xfer[0].cs_change = 1;
213  st->scan_single_xfer[1].tx_buf = &st->tx_buf[1];
214  st->scan_single_xfer[1].len = 2;
215  st->scan_single_xfer[1].cs_change = 1;
216  st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
217  st->scan_single_xfer[2].len = 2;
218 
219  spi_message_init(&st->scan_single_msg);
220  spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
221  spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
222  spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);
223 
224  if (pdata && pdata->vref_mv) {
225  st->int_vref_mv = pdata->vref_mv;
226  st->ext_ref = AD7298_EXTREF;
227  } else {
229  }
230 
231  ret = ad7298_register_ring_funcs_and_init(indio_dev);
232  if (ret)
233  goto error_disable_reg;
234 
235  ret = iio_device_register(indio_dev);
236  if (ret)
237  goto error_cleanup_ring;
238 
239  return 0;
240 
241 error_cleanup_ring:
242  ad7298_ring_cleanup(indio_dev);
243 error_disable_reg:
244  if (!IS_ERR(st->reg))
245  regulator_disable(st->reg);
246 error_put_reg:
247  if (!IS_ERR(st->reg))
248  regulator_put(st->reg);
249  iio_device_free(indio_dev);
250 
251  return ret;
252 }
253 
254 static int __devexit ad7298_remove(struct spi_device *spi)
255 {
256  struct iio_dev *indio_dev = spi_get_drvdata(spi);
257  struct ad7298_state *st = iio_priv(indio_dev);
258 
259  iio_device_unregister(indio_dev);
260  ad7298_ring_cleanup(indio_dev);
261  if (!IS_ERR(st->reg)) {
262  regulator_disable(st->reg);
263  regulator_put(st->reg);
264  }
265  iio_device_free(indio_dev);
266 
267  return 0;
268 }
269 
270 static const struct spi_device_id ad7298_id[] = {
271  {"ad7298", 0},
272  {}
273 };
274 MODULE_DEVICE_TABLE(spi, ad7298_id);
275 
276 static struct spi_driver ad7298_driver = {
277  .driver = {
278  .name = "ad7298",
279  .owner = THIS_MODULE,
280  },
281  .probe = ad7298_probe,
282  .remove = __devexit_p(ad7298_remove),
283  .id_table = ad7298_id,
284 };
285 module_spi_driver(ad7298_driver);
286 
287 MODULE_AUTHOR("Michael Hennerich <[email protected]>");
288 MODULE_DESCRIPTION("Analog Devices AD7298 ADC");
289 MODULE_LICENSE("GPL v2");