blob: d738491222f210c87d19783c737ff414902b1da6 [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>
13#include "ad7606.h"
14
15#define MAX_SPI_FREQ_HZ 23500000 /* VDRIVE above 4.75 V */
16
17static int ad7606_spi_read_block(struct device *dev,
18 int count, void *buf)
19{
20 struct spi_device *spi = to_spi_device(dev);
21 int i, ret;
22 unsigned short *data = buf;
23
24 ret = spi_read(spi, (u8 *)buf, count * 2);
25 if (ret < 0) {
26 dev_err(&spi->dev, "SPI read error\n");
27 return ret;
28 }
29
30 for (i = 0; i < count; i++)
31 data[i] = be16_to_cpu(data[i]);
32
33 return 0;
34}
35
36static const struct ad7606_bus_ops ad7606_spi_bops = {
37 .read_block = ad7606_spi_read_block,
38};
39
40static int __devinit ad7606_spi_probe(struct spi_device *spi)
41{
42 struct ad7606_state *st;
43
44 st = ad7606_probe(&spi->dev, spi->irq, NULL,
45 spi_get_device_id(spi)->driver_data,
46 &ad7606_spi_bops);
47
48 if (IS_ERR(st))
49 return PTR_ERR(st);
50
51 spi_set_drvdata(spi, st);
52
53 return 0;
54}
55
56static int __devexit ad7606_spi_remove(struct spi_device *spi)
57{
58 struct ad7606_state *st = dev_get_drvdata(&spi->dev);
59
60 return ad7606_remove(st);
61}
62
63#ifdef CONFIG_PM
64static int ad7606_spi_suspend(struct device *dev)
65{
66 struct ad7606_state *st = dev_get_drvdata(dev);
67
68 ad7606_suspend(st);
69
70 return 0;
71}
72
73static int ad7606_spi_resume(struct device *dev)
74{
75 struct ad7606_state *st = dev_get_drvdata(dev);
76
77 ad7606_resume(st);
78
79 return 0;
80}
81
82static const struct dev_pm_ops ad7606_pm_ops = {
83 .suspend = ad7606_spi_suspend,
84 .resume = ad7606_spi_resume,
85};
86#define AD7606_SPI_PM_OPS (&ad7606_pm_ops)
87
88#else
89#define AD7606_SPI_PM_OPS NULL
90#endif
91
92static const struct spi_device_id ad7606_id[] = {
93 {"ad7606-8", ID_AD7606_8},
94 {"ad7606-6", ID_AD7606_6},
95 {"ad7606-4", ID_AD7606_4},
96 {}
97};
98
99static struct spi_driver ad7606_driver = {
100 .driver = {
101 .name = "ad7606",
102 .bus = &spi_bus_type,
103 .owner = THIS_MODULE,
104 .pm = AD7606_SPI_PM_OPS,
105 },
106 .probe = ad7606_spi_probe,
107 .remove = __devexit_p(ad7606_spi_remove),
108 .id_table = ad7606_id,
109};
110
111static int __init ad7606_spi_init(void)
112{
113 return spi_register_driver(&ad7606_driver);
114}
115module_init(ad7606_spi_init);
116
117static void __exit ad7606_spi_exit(void)
118{
119 spi_unregister_driver(&ad7606_driver);
120}
121module_exit(ad7606_spi_exit);
122
123MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
124MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
125MODULE_LICENSE("GPL v2");
126MODULE_ALIAS("spi:ad7606_spi");