• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ad7606_spi_read_block(struct device * dev,int count,void * buf)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, 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 
ad7606_spi_probe(struct spi_device * spi)42 static int 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,
47 				 spi_get_device_id(spi)->driver_data,
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 
ad7606_spi_remove(struct spi_device * spi)58 static int 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
ad7606_spi_suspend(struct device * dev)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 
ad7606_spi_resume(struct device * dev)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 
89 #define AD7606_SPI_PM_OPS (&ad7606_pm_ops)
90 
91 #else
92 #define AD7606_SPI_PM_OPS NULL
93 #endif
94 
95 static const struct spi_device_id ad7606_id[] = {
96 	{"ad7606-8", ID_AD7606_8},
97 	{"ad7606-6", ID_AD7606_6},
98 	{"ad7606-4", ID_AD7606_4},
99 	{}
100 };
101 MODULE_DEVICE_TABLE(spi, ad7606_id);
102 
103 static struct spi_driver ad7606_driver = {
104 	.driver = {
105 		.name = "ad7606",
106 		.pm    = AD7606_SPI_PM_OPS,
107 	},
108 	.probe = ad7606_spi_probe,
109 	.remove = ad7606_spi_remove,
110 	.id_table = ad7606_id,
111 };
112 module_spi_driver(ad7606_driver);
113 
114 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
115 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
116 MODULE_LICENSE("GPL v2");
117