drm/client: Fix drm client endless Kconfig loop
[drm/drm-misc.git] / drivers / iio / adc / ad7606_par.c
blob64733b607aa87a3ff101d317c48fbd41b47f279e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * AD7606 Parallel Interface ADC driver
5 * Copyright 2011 - 2024 Analog Devices Inc.
6 * Copyright 2024 BayLibre SAS.
7 */
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>
18 #include <linux/iio/backend.h>
19 #include <linux/iio/iio.h>
21 #include "ad7606.h"
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),
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);
37 unsigned int c, ret;
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;
48 return 0;
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);
54 unsigned int ret, c;
55 struct iio_backend_data_fmt data = {
56 .sign_extend = true,
57 .enable = true,
60 st->back = devm_iio_backend_get(dev, NULL);
61 if (IS_ERR(st->back))
62 return PTR_ERR(st->back);
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");
69 ret = devm_iio_backend_request_buffer(dev, st->back, indio_dev);
70 if (ret)
71 return ret;
73 ret = devm_iio_backend_enable(dev, st->back);
74 if (ret)
75 return ret;
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;
83 indio_dev->channels = ad7606b_bi_channels;
84 indio_dev->num_channels = 8;
86 return 0;
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,
95 int count, void *buf)
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.
110 int num = count;
111 u16 *_buf = buf;
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;
119 _buf++;
120 num--;
122 insw((unsigned long)st->base_address, _buf, num);
123 return 0;
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.
144 int num = count;
145 u16 *_buf = buf;
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;
153 _buf++;
154 num--;
156 insb((unsigned long)st->base_address, _buf, num * 2);
158 return 0;
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;
170 void __iomem *addr;
171 resource_size_t remap_size;
172 int irq;
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,
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;
193 irq = platform_get_irq(pdev, 0);
194 if (irq < 0)
195 return irq;
197 addr = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
198 if (IS_ERR(addr))
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 :
205 &ad7606_par8_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,
237 .driver = {
238 .name = "ad7606",
239 .pm = AD7606_PM_OPS,
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");