xref: /linux/drivers/iio/adc/ad7606_spi.c (revision d003d772e64df08af04ee63609d47169ee82ae0e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * AD7606 SPI ADC driver
4  *
5  * Copyright 2011 Analog Devices Inc.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/spi/spi.h>
10 #include <linux/types.h>
11 #include <linux/err.h>
12 
13 #include <linux/iio/iio.h>
14 #include "ad7606.h"
15 
16 #define MAX_SPI_FREQ_HZ		23500000	/* VDRIVE above 4.75 V */
17 
18 static int ad7606_spi_read_block(struct device *dev,
19 				 int count, void *buf)
20 {
21 	struct spi_device *spi = to_spi_device(dev);
22 	int i, ret;
23 	unsigned short *data = buf;
24 	__be16 *bdata = 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(bdata[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 ad7606_spi_probe(struct spi_device *spi)
43 {
44 	const struct spi_device_id *id = spi_get_device_id(spi);
45 
46 	return ad7606_probe(&spi->dev, spi->irq, NULL,
47 			    id->name, id->driver_data,
48 			    &ad7606_spi_bops);
49 }
50 
51 static const struct spi_device_id ad7606_id_table[] = {
52 	{ "ad7605-4", ID_AD7605_4 },
53 	{ "ad7606-4", ID_AD7606_4 },
54 	{ "ad7606-6", ID_AD7606_6 },
55 	{ "ad7606-8", ID_AD7606_8 },
56 	{}
57 };
58 MODULE_DEVICE_TABLE(spi, ad7606_id_table);
59 
60 static const struct of_device_id ad7606_of_match[] = {
61 	{ .compatible = "adi,ad7605-4" },
62 	{ .compatible = "adi,ad7606-4" },
63 	{ .compatible = "adi,ad7606-6" },
64 	{ .compatible = "adi,ad7606-8" },
65 	{ },
66 };
67 MODULE_DEVICE_TABLE(of, ad7606_of_match);
68 
69 static struct spi_driver ad7606_driver = {
70 	.driver = {
71 		.name = "ad7606",
72 		.of_match_table = ad7606_of_match,
73 		.pm = AD7606_PM_OPS,
74 	},
75 	.probe = ad7606_spi_probe,
76 	.id_table = ad7606_id_table,
77 };
78 module_spi_driver(ad7606_driver);
79 
80 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
81 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
82 MODULE_LICENSE("GPL v2");
83