xref: /linux/drivers/iio/adc/ad7606_par.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * AD7606 Parallel Interface ADC driver
4  *
5  * Copyright 2011 - 2024 Analog Devices Inc.
6  * Copyright 2024 BayLibre SAS.
7  */
8 
9 #include <linux/err.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/io.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/types.h>
17 
18 #include <linux/iio/backend.h>
19 #include <linux/iio/iio.h>
20 
21 #include "ad7606.h"
22 
23 static const struct iio_chan_spec ad7606b_bi_channels[] = {
24 	AD7606_BI_CHANNEL(0),
25 	AD7606_BI_CHANNEL(1),
26 	AD7606_BI_CHANNEL(2),
27 	AD7606_BI_CHANNEL(3),
28 	AD7606_BI_CHANNEL(4),
29 	AD7606_BI_CHANNEL(5),
30 	AD7606_BI_CHANNEL(6),
31 	AD7606_BI_CHANNEL(7),
32 };
33 
34 static int ad7606_bi_update_scan_mode(struct iio_dev *indio_dev, const unsigned long *scan_mask)
35 {
36 	struct ad7606_state *st = iio_priv(indio_dev);
37 	unsigned int c, ret;
38 
39 	for (c = 0; c < indio_dev->num_channels; c++) {
40 		if (test_bit(c, scan_mask))
41 			ret = iio_backend_chan_enable(st->back, c);
42 		else
43 			ret = iio_backend_chan_disable(st->back, c);
44 		if (ret)
45 			return ret;
46 	}
47 
48 	return 0;
49 }
50 
51 static int ad7606_bi_setup_iio_backend(struct device *dev, struct iio_dev *indio_dev)
52 {
53 	struct ad7606_state *st = iio_priv(indio_dev);
54 	unsigned int ret, c;
55 	struct iio_backend_data_fmt data = {
56 		.sign_extend = true,
57 		.enable = true,
58 	};
59 
60 	st->back = devm_iio_backend_get(dev, NULL);
61 	if (IS_ERR(st->back))
62 		return PTR_ERR(st->back);
63 
64 	/* If the device is iio_backend powered the PWM is mandatory */
65 	if (!st->cnvst_pwm)
66 		return dev_err_probe(st->dev, -EINVAL,
67 				     "A PWM is mandatory when using backend.\n");
68 
69 	ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
70 	if (ret)
71 		return ret;
72 
73 	ret = devm_iio_backend_enable(dev, st->back);
74 	if (ret)
75 		return ret;
76 
77 	for (c = 0; c < indio_dev->num_channels; c++) {
78 		ret = iio_backend_data_format_set(st->back, c, &data);
79 		if (ret)
80 			return ret;
81 	}
82 
83 	indio_dev->channels = ad7606b_bi_channels;
84 	indio_dev->num_channels = 8;
85 
86 	return 0;
87 }
88 
89 static const struct ad7606_bus_ops ad7606_bi_bops = {
90 	.iio_backend_config = ad7606_bi_setup_iio_backend,
91 	.update_scan_mode = ad7606_bi_update_scan_mode,
92 };
93 
94 static int ad7606_par16_read_block(struct device *dev,
95 				   int count, void *buf)
96 {
97 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
98 	struct ad7606_state *st = iio_priv(indio_dev);
99 
100 
101 	/*
102 	 * On the parallel interface, the frstdata signal is set to high while
103 	 * and after reading the sample of the first channel and low for all
104 	 * other channels.  This can be used to check that the incoming data is
105 	 * correctly aligned.  During normal operation the data should never
106 	 * become unaligned, but some glitch or electrostatic discharge might
107 	 * cause an extra read or clock cycle.  Monitoring the frstdata signal
108 	 * allows to recover from such failure situations.
109 	 */
110 	int num = count;
111 	u16 *_buf = buf;
112 
113 	if (st->gpio_frstdata) {
114 		insw((unsigned long)st->base_address, _buf, 1);
115 		if (!gpiod_get_value(st->gpio_frstdata)) {
116 			ad7606_reset(st);
117 			return -EIO;
118 		}
119 		_buf++;
120 		num--;
121 	}
122 	insw((unsigned long)st->base_address, _buf, num);
123 	return 0;
124 }
125 
126 static const struct ad7606_bus_ops ad7606_par16_bops = {
127 	.read_block = ad7606_par16_read_block,
128 };
129 
130 static int ad7606_par8_read_block(struct device *dev,
131 				  int count, void *buf)
132 {
133 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
134 	struct ad7606_state *st = iio_priv(indio_dev);
135 	/*
136 	 * On the parallel interface, the frstdata signal is set to high while
137 	 * and after reading the sample of the first channel and low for all
138 	 * other channels.  This can be used to check that the incoming data is
139 	 * correctly aligned.  During normal operation the data should never
140 	 * become unaligned, but some glitch or electrostatic discharge might
141 	 * cause an extra read or clock cycle.  Monitoring the frstdata signal
142 	 * allows to recover from such failure situations.
143 	 */
144 	int num = count;
145 	u16 *_buf = buf;
146 
147 	if (st->gpio_frstdata) {
148 		insb((unsigned long)st->base_address, _buf, 2);
149 		if (!gpiod_get_value(st->gpio_frstdata)) {
150 			ad7606_reset(st);
151 			return -EIO;
152 		}
153 		_buf++;
154 		num--;
155 	}
156 	insb((unsigned long)st->base_address, _buf, num * 2);
157 
158 	return 0;
159 }
160 
161 static const struct ad7606_bus_ops ad7606_par8_bops = {
162 	.read_block = ad7606_par8_read_block,
163 };
164 
165 static int ad7606_par_probe(struct platform_device *pdev)
166 {
167 	const struct ad7606_chip_info *chip_info;
168 	const struct platform_device_id *id;
169 	struct resource *res;
170 	void __iomem *addr;
171 	resource_size_t remap_size;
172 	int irq;
173 
174 	/*
175 	 * If a firmware node is available (ACPI or DT), platform_device_id is null
176 	 * and we must use get_match_data.
177 	 */
178 	if (dev_fwnode(&pdev->dev)) {
179 		chip_info = device_get_match_data(&pdev->dev);
180 		if (device_property_present(&pdev->dev, "io-backends"))
181 			/*
182 			 * If a backend is available ,call the core probe with backend
183 			 * bops, otherwise use the former bops.
184 			 */
185 			return ad7606_probe(&pdev->dev, 0, NULL,
186 					    chip_info,
187 					    &ad7606_bi_bops);
188 	} else {
189 		id = platform_get_device_id(pdev);
190 		chip_info = (const struct ad7606_chip_info *)id->driver_data;
191 	}
192 
193 	irq = platform_get_irq(pdev, 0);
194 	if (irq < 0)
195 		return irq;
196 
197 	addr = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
198 	if (IS_ERR(addr))
199 		return PTR_ERR(addr);
200 
201 	remap_size = resource_size(res);
202 
203 	return ad7606_probe(&pdev->dev, irq, addr, chip_info,
204 			    remap_size > 1 ? &ad7606_par16_bops :
205 			    &ad7606_par8_bops);
206 }
207 
208 static const struct platform_device_id ad7606_driver_ids[] = {
209 	{ .name	= "ad7605-4", .driver_data = (kernel_ulong_t)&ad7605_4_info, },
210 	{ .name	= "ad7606-4", .driver_data = (kernel_ulong_t)&ad7606_4_info, },
211 	{ .name	= "ad7606-6", .driver_data = (kernel_ulong_t)&ad7606_6_info, },
212 	{ .name	= "ad7606-8", .driver_data = (kernel_ulong_t)&ad7606_8_info, },
213 	{ .name	= "ad7606b", .driver_data = (kernel_ulong_t)&ad7606b_info, },
214 	{ .name	= "ad7607", .driver_data = (kernel_ulong_t)&ad7607_info, },
215 	{ .name	= "ad7608", .driver_data = (kernel_ulong_t)&ad7608_info, },
216 	{ .name	= "ad7609", .driver_data = (kernel_ulong_t)&ad7609_info, },
217 	{ }
218 };
219 MODULE_DEVICE_TABLE(platform, ad7606_driver_ids);
220 
221 static const struct of_device_id ad7606_of_match[] = {
222 	{ .compatible = "adi,ad7605-4", .data = &ad7605_4_info },
223 	{ .compatible = "adi,ad7606-4", .data = &ad7606_4_info },
224 	{ .compatible = "adi,ad7606-6", .data = &ad7606_6_info },
225 	{ .compatible = "adi,ad7606-8", .data = &ad7606_8_info },
226 	{ .compatible = "adi,ad7606b", .data = &ad7606b_info },
227 	{ .compatible = "adi,ad7607", .data = &ad7607_info },
228 	{ .compatible = "adi,ad7608", .data = &ad7608_info },
229 	{ .compatible = "adi,ad7609", .data = &ad7609_info },
230 	{ }
231 };
232 MODULE_DEVICE_TABLE(of, ad7606_of_match);
233 
234 static struct platform_driver ad7606_driver = {
235 	.probe = ad7606_par_probe,
236 	.id_table = ad7606_driver_ids,
237 	.driver = {
238 		.name = "ad7606",
239 		.pm = AD7606_PM_OPS,
240 		.of_match_table = ad7606_of_match,
241 	},
242 };
243 module_platform_driver(ad7606_driver);
244 
245 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
246 MODULE_DESCRIPTION("Analog Devices AD7606 ADC");
247 MODULE_LICENSE("GPL v2");
248 MODULE_IMPORT_NS(IIO_AD7606);
249 MODULE_IMPORT_NS(IIO_BACKEND);
250