blob: 7303983e64a7262653ba95611ff8a30ce8d743c8 [file] [log] [blame]
Michael Hennerichb9618c02011-02-22 21:46:18 +01001/*
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>
Michael Henneriche61181d2011-05-18 14:42:01 +010013
Jonathan Cameron06458e22012-04-25 15:54:58 +010014#include <linux/iio/iio.h>
Michael Hennerichb9618c02011-02-22 21:46:18 +010015#include "ad7606.h"
16
17#define MAX_SPI_FREQ_HZ 23500000 /* VDRIVE above 4.75 V */
18
19static 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
Tapasweni Pathak31f8f062014-10-30 17:02:25 +053026 ret = spi_read(spi, buf, count * 2);
Michael Hennerichb9618c02011-02-22 21:46:18 +010027 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
38static const struct ad7606_bus_ops ad7606_spi_bops = {
39 .read_block = ad7606_spi_read_block,
40};
41
Bill Pemberton4ae1c612012-11-19 13:21:57 -050042static int ad7606_spi_probe(struct spi_device *spi)
Michael Hennerichb9618c02011-02-22 21:46:18 +010043{
Michael Henneriche61181d2011-05-18 14:42:01 +010044 struct iio_dev *indio_dev;
Michael Hennerichb9618c02011-02-22 21:46:18 +010045
Michael Henneriche61181d2011-05-18 14:42:01 +010046 indio_dev = ad7606_probe(&spi->dev, spi->irq, NULL,
Michael Hennerichb9618c02011-02-22 21:46:18 +010047 spi_get_device_id(spi)->driver_data,
48 &ad7606_spi_bops);
49
Michael Henneriche61181d2011-05-18 14:42:01 +010050 if (IS_ERR(indio_dev))
51 return PTR_ERR(indio_dev);
Michael Hennerichb9618c02011-02-22 21:46:18 +010052
Michael Henneriche61181d2011-05-18 14:42:01 +010053 spi_set_drvdata(spi, indio_dev);
Michael Hennerichb9618c02011-02-22 21:46:18 +010054
55 return 0;
56}
57
Bill Pemberton447d4f22012-11-19 13:26:37 -050058static int ad7606_spi_remove(struct spi_device *spi)
Michael Hennerichb9618c02011-02-22 21:46:18 +010059{
Michael Henneriche61181d2011-05-18 14:42:01 +010060 struct iio_dev *indio_dev = dev_get_drvdata(&spi->dev);
Michael Hennerichb9618c02011-02-22 21:46:18 +010061
Jonathan Cameron8cbb36a2011-09-30 10:05:42 +010062 return ad7606_remove(indio_dev, spi->irq);
Michael Hennerichb9618c02011-02-22 21:46:18 +010063}
64
65#ifdef CONFIG_PM
66static int ad7606_spi_suspend(struct device *dev)
67{
Michael Henneriche61181d2011-05-18 14:42:01 +010068 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Michael Hennerichb9618c02011-02-22 21:46:18 +010069
Michael Henneriche61181d2011-05-18 14:42:01 +010070 ad7606_suspend(indio_dev);
Michael Hennerichb9618c02011-02-22 21:46:18 +010071
72 return 0;
73}
74
75static int ad7606_spi_resume(struct device *dev)
76{
Michael Henneriche61181d2011-05-18 14:42:01 +010077 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Michael Hennerichb9618c02011-02-22 21:46:18 +010078
Michael Henneriche61181d2011-05-18 14:42:01 +010079 ad7606_resume(indio_dev);
Michael Hennerichb9618c02011-02-22 21:46:18 +010080
81 return 0;
82}
83
84static 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
94static 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};
Lars-Peter Clausen55e43902011-11-16 08:53:31 +0100100MODULE_DEVICE_TABLE(spi, ad7606_id);
Michael Hennerichb9618c02011-02-22 21:46:18 +0100101
102static struct spi_driver ad7606_driver = {
103 .driver = {
104 .name = "ad7606",
Michael Hennerichb9618c02011-02-22 21:46:18 +0100105 .owner = THIS_MODULE,
106 .pm = AD7606_SPI_PM_OPS,
107 },
108 .probe = ad7606_spi_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500109 .remove = ad7606_spi_remove,
Michael Hennerichb9618c02011-02-22 21:46:18 +0100110 .id_table = ad7606_id,
111};
Lars-Peter Clausenae6ae6f2011-11-16 10:13:39 +0100112module_spi_driver(ad7606_driver);
Michael Hennerichb9618c02011-02-22 21:46:18 +0100113
114MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
115MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
116MODULE_LICENSE("GPL v2");