xref: /openbmc/linux/drivers/iio/adc/ad7606_spi.c (revision 03638e62)
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 	{ "ad7616",   ID_AD7616 },
57 	{}
58 };
59 MODULE_DEVICE_TABLE(spi, ad7606_id_table);
60 
61 static const struct of_device_id ad7606_of_match[] = {
62 	{ .compatible = "adi,ad7605-4" },
63 	{ .compatible = "adi,ad7606-4" },
64 	{ .compatible = "adi,ad7606-6" },
65 	{ .compatible = "adi,ad7606-8" },
66 	{ .compatible = "adi,ad7616" },
67 	{ },
68 };
69 MODULE_DEVICE_TABLE(of, ad7606_of_match);
70 
71 static struct spi_driver ad7606_driver = {
72 	.driver = {
73 		.name = "ad7606",
74 		.of_match_table = ad7606_of_match,
75 		.pm = AD7606_PM_OPS,
76 	},
77 	.probe = ad7606_spi_probe,
78 	.id_table = ad7606_id_table,
79 };
80 module_spi_driver(ad7606_driver);
81 
82 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
83 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
84 MODULE_LICENSE("GPL v2");
85