Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ad7606_spi.c
Go to the documentation of this file.
1 /*
2  * AD7606 SPI ADC driver
3  *
4  * Copyright 2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2.
7  */
8 
9 #include <linux/module.h>
10 #include <linux/spi/spi.h>
11 #include <linux/types.h>
12 #include <linux/err.h>
13 
14 #include <linux/iio/iio.h>
15 #include "ad7606.h"
16 
17 #define MAX_SPI_FREQ_HZ 23500000 /* VDRIVE above 4.75 V */
18 
19 static int ad7606_spi_read_block(struct device *dev,
20  int count, void *buf)
21 {
22  struct spi_device *spi = to_spi_device(dev);
23  int i, ret;
24  unsigned short *data = buf;
25 
26  ret = spi_read(spi, (u8 *)buf, count * 2);
27  if (ret < 0) {
28  dev_err(&spi->dev, "SPI read error\n");
29  return ret;
30  }
31 
32  for (i = 0; i < count; i++)
33  data[i] = be16_to_cpu(data[i]);
34 
35  return 0;
36 }
37 
38 static const struct ad7606_bus_ops ad7606_spi_bops = {
39  .read_block = ad7606_spi_read_block,
40 };
41 
42 static int __devinit ad7606_spi_probe(struct spi_device *spi)
43 {
44  struct iio_dev *indio_dev;
45 
46  indio_dev = ad7606_probe(&spi->dev, spi->irq, NULL,
48  &ad7606_spi_bops);
49 
50  if (IS_ERR(indio_dev))
51  return PTR_ERR(indio_dev);
52 
53  spi_set_drvdata(spi, indio_dev);
54 
55  return 0;
56 }
57 
58 static int __devexit ad7606_spi_remove(struct spi_device *spi)
59 {
60  struct iio_dev *indio_dev = dev_get_drvdata(&spi->dev);
61 
62  return ad7606_remove(indio_dev, spi->irq);
63 }
64 
65 #ifdef CONFIG_PM
66 static int ad7606_spi_suspend(struct device *dev)
67 {
68  struct iio_dev *indio_dev = dev_get_drvdata(dev);
69 
70  ad7606_suspend(indio_dev);
71 
72  return 0;
73 }
74 
75 static int ad7606_spi_resume(struct device *dev)
76 {
77  struct iio_dev *indio_dev = dev_get_drvdata(dev);
78 
79  ad7606_resume(indio_dev);
80 
81  return 0;
82 }
83 
84 static const struct dev_pm_ops ad7606_pm_ops = {
85  .suspend = ad7606_spi_suspend,
86  .resume = ad7606_spi_resume,
87 };
88 #define AD7606_SPI_PM_OPS (&ad7606_pm_ops)
89 
90 #else
91 #define AD7606_SPI_PM_OPS NULL
92 #endif
93 
94 static const struct spi_device_id ad7606_id[] = {
95  {"ad7606-8", ID_AD7606_8},
96  {"ad7606-6", ID_AD7606_6},
97  {"ad7606-4", ID_AD7606_4},
98  {}
99 };
100 MODULE_DEVICE_TABLE(spi, ad7606_id);
101 
102 static struct spi_driver ad7606_driver = {
103  .driver = {
104  .name = "ad7606",
105  .owner = THIS_MODULE,
106  .pm = AD7606_SPI_PM_OPS,
107  },
108  .probe = ad7606_spi_probe,
109  .remove = __devexit_p(ad7606_spi_remove),
110  .id_table = ad7606_id,
111 };
112 module_spi_driver(ad7606_driver);
113 
114 MODULE_AUTHOR("Michael Hennerich <[email protected]>");
115 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
116 MODULE_LICENSE("GPL v2");