1 // SPDX-License-Identifier: GPL-2.0
3 * AD7606 Parallel Interface ADC driver
5 * Copyright 2011 - 2024 Analog Devices Inc.
6 * Copyright 2024 BayLibre SAS.
10 #include <linux/gpio/consumer.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>
18 #include <linux/iio/backend.h>
19 #include <linux/iio/iio.h>
23 static const struct iio_chan_spec ad7606b_bi_channels
[] = {
34 static int ad7606_bi_update_scan_mode(struct iio_dev
*indio_dev
, const unsigned long *scan_mask
)
36 struct ad7606_state
*st
= iio_priv(indio_dev
);
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
);
43 ret
= iio_backend_chan_disable(st
->back
, c
);
51 static int ad7606_bi_setup_iio_backend(struct device
*dev
, struct iio_dev
*indio_dev
)
53 struct ad7606_state
*st
= iio_priv(indio_dev
);
55 struct iio_backend_data_fmt data
= {
60 st
->back
= devm_iio_backend_get(dev
, NULL
);
62 return PTR_ERR(st
->back
);
64 /* If the device is iio_backend powered the PWM is mandatory */
66 return dev_err_probe(st
->dev
, -EINVAL
,
67 "A PWM is mandatory when using backend.\n");
69 ret
= devm_iio_backend_request_buffer(dev
, st
->back
, indio_dev
);
73 ret
= devm_iio_backend_enable(dev
, st
->back
);
77 for (c
= 0; c
< indio_dev
->num_channels
; c
++) {
78 ret
= iio_backend_data_format_set(st
->back
, c
, &data
);
83 indio_dev
->channels
= ad7606b_bi_channels
;
84 indio_dev
->num_channels
= 8;
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
,
94 static int ad7606_par16_read_block(struct device
*dev
,
97 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
98 struct ad7606_state
*st
= iio_priv(indio_dev
);
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.
113 if (st
->gpio_frstdata
) {
114 insw((unsigned long)st
->base_address
, _buf
, 1);
115 if (!gpiod_get_value(st
->gpio_frstdata
)) {
122 insw((unsigned long)st
->base_address
, _buf
, num
);
126 static const struct ad7606_bus_ops ad7606_par16_bops
= {
127 .read_block
= ad7606_par16_read_block
,
130 static int ad7606_par8_read_block(struct device
*dev
,
131 int count
, void *buf
)
133 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
134 struct ad7606_state
*st
= iio_priv(indio_dev
);
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.
147 if (st
->gpio_frstdata
) {
148 insb((unsigned long)st
->base_address
, _buf
, 2);
149 if (!gpiod_get_value(st
->gpio_frstdata
)) {
156 insb((unsigned long)st
->base_address
, _buf
, num
* 2);
161 static const struct ad7606_bus_ops ad7606_par8_bops
= {
162 .read_block
= ad7606_par8_read_block
,
165 static int ad7606_par_probe(struct platform_device
*pdev
)
167 const struct ad7606_chip_info
*chip_info
;
168 const struct platform_device_id
*id
;
169 struct resource
*res
;
171 resource_size_t remap_size
;
175 * If a firmware node is available (ACPI or DT), platform_device_id is null
176 * and we must use get_match_data.
178 if (dev_fwnode(&pdev
->dev
)) {
179 chip_info
= device_get_match_data(&pdev
->dev
);
180 if (device_property_present(&pdev
->dev
, "io-backends"))
182 * If a backend is available ,call the core probe with backend
183 * bops, otherwise use the former bops.
185 return ad7606_probe(&pdev
->dev
, 0, NULL
,
189 id
= platform_get_device_id(pdev
);
190 chip_info
= (const struct ad7606_chip_info
*)id
->driver_data
;
193 irq
= platform_get_irq(pdev
, 0);
197 addr
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
199 return PTR_ERR(addr
);
201 remap_size
= resource_size(res
);
203 return ad7606_probe(&pdev
->dev
, irq
, addr
, chip_info
,
204 remap_size
> 1 ? &ad7606_par16_bops
:
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
, },
219 MODULE_DEVICE_TABLE(platform
, ad7606_driver_ids
);
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
},
232 MODULE_DEVICE_TABLE(of
, ad7606_of_match
);
234 static struct platform_driver ad7606_driver
= {
235 .probe
= ad7606_par_probe
,
236 .id_table
= ad7606_driver_ids
,
240 .of_match_table
= ad7606_of_match
,
243 module_platform_driver(ad7606_driver
);
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");