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