1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver 4 * 5 * Copyright 2011 Analog Devices Inc. 6 * Copyright 2019 Renato Lui Geh 7 */ 8 9 #include <linux/interrupt.h> 10 #include <linux/device.h> 11 #include <linux/kernel.h> 12 #include <linux/slab.h> 13 #include <linux/sysfs.h> 14 #include <linux/spi/spi.h> 15 #include <linux/regulator/consumer.h> 16 #include <linux/err.h> 17 #include <linux/sched.h> 18 #include <linux/gpio/consumer.h> 19 #include <linux/module.h> 20 #include <linux/bits.h> 21 22 #include <linux/iio/iio.h> 23 #include <linux/iio/sysfs.h> 24 #include <linux/iio/adc/ad_sigma_delta.h> 25 26 #define AD7780_RDY BIT(7) 27 #define AD7780_FILTER BIT(6) 28 #define AD7780_ERR BIT(5) 29 #define AD7780_ID1 BIT(4) 30 #define AD7780_ID0 BIT(3) 31 #define AD7780_GAIN BIT(2) 32 33 #define AD7170_ID 0 34 #define AD7171_ID 1 35 #define AD7780_ID 1 36 #define AD7781_ID 0 37 38 #define AD7780_ID_MASK (AD7780_ID0 | AD7780_ID1) 39 40 #define AD7780_PATTERN_GOOD 1 41 #define AD7780_PATTERN_MASK GENMASK(1, 0) 42 43 #define AD7170_PATTERN_GOOD 5 44 #define AD7170_PATTERN_MASK GENMASK(2, 0) 45 46 #define AD7780_GAIN_MIDPOINT 64 47 #define AD7780_FILTER_MIDPOINT 13350 48 49 static const unsigned int ad778x_gain[2] = { 1, 128 }; 50 static const unsigned int ad778x_odr_avail[2] = { 10000, 16700 }; 51 52 struct ad7780_chip_info { 53 struct iio_chan_spec channel; 54 unsigned int pattern_mask; 55 unsigned int pattern; 56 bool is_ad778x; 57 }; 58 59 struct ad7780_state { 60 const struct ad7780_chip_info *chip_info; 61 struct regulator *reg; 62 struct gpio_desc *powerdown_gpio; 63 struct gpio_desc *gain_gpio; 64 struct gpio_desc *filter_gpio; 65 unsigned int gain; 66 unsigned int odr; 67 unsigned int int_vref_mv; 68 69 struct ad_sigma_delta sd; 70 }; 71 72 enum ad7780_supported_device_ids { 73 ID_AD7170, 74 ID_AD7171, 75 ID_AD7780, 76 ID_AD7781, 77 }; 78 79 static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd) 80 { 81 return container_of(sd, struct ad7780_state, sd); 82 } 83 84 static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta, 85 enum ad_sigma_delta_mode mode) 86 { 87 struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta); 88 unsigned int val; 89 90 switch (mode) { 91 case AD_SD_MODE_SINGLE: 92 case AD_SD_MODE_CONTINUOUS: 93 val = 1; 94 break; 95 default: 96 val = 0; 97 break; 98 } 99 100 gpiod_set_value(st->powerdown_gpio, val); 101 102 return 0; 103 } 104 105 static int ad7780_read_raw(struct iio_dev *indio_dev, 106 struct iio_chan_spec const *chan, 107 int *val, 108 int *val2, 109 long m) 110 { 111 struct ad7780_state *st = iio_priv(indio_dev); 112 int voltage_uv; 113 114 switch (m) { 115 case IIO_CHAN_INFO_RAW: 116 return ad_sigma_delta_single_conversion(indio_dev, chan, val); 117 case IIO_CHAN_INFO_SCALE: 118 voltage_uv = regulator_get_voltage(st->reg); 119 if (voltage_uv < 0) 120 return voltage_uv; 121 voltage_uv /= 1000; 122 *val = voltage_uv * st->gain; 123 *val2 = chan->scan_type.realbits - 1; 124 st->int_vref_mv = voltage_uv; 125 return IIO_VAL_FRACTIONAL_LOG2; 126 case IIO_CHAN_INFO_OFFSET: 127 *val = -(1 << (chan->scan_type.realbits - 1)); 128 return IIO_VAL_INT; 129 case IIO_CHAN_INFO_SAMP_FREQ: 130 *val = st->odr; 131 return IIO_VAL_INT; 132 default: 133 break; 134 } 135 136 return -EINVAL; 137 } 138 139 static int ad7780_write_raw(struct iio_dev *indio_dev, 140 struct iio_chan_spec const *chan, 141 int val, 142 int val2, 143 long m) 144 { 145 struct ad7780_state *st = iio_priv(indio_dev); 146 const struct ad7780_chip_info *chip_info = st->chip_info; 147 unsigned long long vref; 148 unsigned int full_scale, gain; 149 150 if (!chip_info->is_ad778x) 151 return -EINVAL; 152 153 switch (m) { 154 case IIO_CHAN_INFO_SCALE: 155 if (val != 0) 156 return -EINVAL; 157 158 vref = st->int_vref_mv * 1000000LL; 159 full_scale = 1 << (chip_info->channel.scan_type.realbits - 1); 160 gain = DIV_ROUND_CLOSEST_ULL(vref, full_scale); 161 gain = DIV_ROUND_CLOSEST(gain, val2); 162 st->gain = gain; 163 if (gain < AD7780_GAIN_MIDPOINT) 164 gain = 0; 165 else 166 gain = 1; 167 gpiod_set_value(st->gain_gpio, gain); 168 break; 169 case IIO_CHAN_INFO_SAMP_FREQ: 170 if (1000*val + val2/1000 < AD7780_FILTER_MIDPOINT) 171 val = 0; 172 else 173 val = 1; 174 st->odr = ad778x_odr_avail[val]; 175 gpiod_set_value(st->filter_gpio, val); 176 break; 177 default: 178 break; 179 } 180 181 return 0; 182 } 183 184 static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta, 185 unsigned int raw_sample) 186 { 187 struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta); 188 const struct ad7780_chip_info *chip_info = st->chip_info; 189 190 if ((raw_sample & AD7780_ERR) || 191 ((raw_sample & chip_info->pattern_mask) != chip_info->pattern)) 192 return -EIO; 193 194 if (chip_info->is_ad778x) { 195 st->gain = ad778x_gain[raw_sample & AD7780_GAIN]; 196 st->odr = ad778x_odr_avail[raw_sample & AD7780_FILTER]; 197 } 198 199 return 0; 200 } 201 202 static const struct ad_sigma_delta_info ad7780_sigma_delta_info = { 203 .set_mode = ad7780_set_mode, 204 .postprocess_sample = ad7780_postprocess_sample, 205 .has_registers = false, 206 .irq_flags = IRQF_TRIGGER_LOW, 207 }; 208 209 #define AD7780_CHANNEL(bits, wordsize) \ 210 AD_SD_CHANNEL(1, 0, 0, bits, 32, (wordsize) - (bits)) 211 #define AD7170_CHANNEL(bits, wordsize) \ 212 AD_SD_CHANNEL_NO_SAMP_FREQ(1, 0, 0, bits, 32, (wordsize) - (bits)) 213 214 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = { 215 [ID_AD7170] = { 216 .channel = AD7170_CHANNEL(12, 24), 217 .pattern = AD7170_PATTERN_GOOD, 218 .pattern_mask = AD7170_PATTERN_MASK, 219 .is_ad778x = false, 220 }, 221 [ID_AD7171] = { 222 .channel = AD7170_CHANNEL(16, 24), 223 .pattern = AD7170_PATTERN_GOOD, 224 .pattern_mask = AD7170_PATTERN_MASK, 225 .is_ad778x = false, 226 }, 227 [ID_AD7780] = { 228 .channel = AD7780_CHANNEL(24, 32), 229 .pattern = AD7780_PATTERN_GOOD, 230 .pattern_mask = AD7780_PATTERN_MASK, 231 .is_ad778x = true, 232 }, 233 [ID_AD7781] = { 234 .channel = AD7780_CHANNEL(20, 32), 235 .pattern = AD7780_PATTERN_GOOD, 236 .pattern_mask = AD7780_PATTERN_MASK, 237 .is_ad778x = true, 238 }, 239 }; 240 241 static const struct iio_info ad7780_info = { 242 .read_raw = ad7780_read_raw, 243 .write_raw = ad7780_write_raw, 244 }; 245 246 static int ad7780_init_gpios(struct device *dev, struct ad7780_state *st) 247 { 248 int ret; 249 250 st->powerdown_gpio = devm_gpiod_get_optional(dev, 251 "powerdown", 252 GPIOD_OUT_LOW); 253 if (IS_ERR(st->powerdown_gpio)) { 254 ret = PTR_ERR(st->powerdown_gpio); 255 dev_err(dev, "Failed to request powerdown GPIO: %d\n", ret); 256 return ret; 257 } 258 259 if (!st->chip_info->is_ad778x) 260 return 0; 261 262 263 st->gain_gpio = devm_gpiod_get_optional(dev, 264 "adi,gain", 265 GPIOD_OUT_HIGH); 266 if (IS_ERR(st->gain_gpio)) { 267 ret = PTR_ERR(st->gain_gpio); 268 dev_err(dev, "Failed to request gain GPIO: %d\n", ret); 269 return ret; 270 } 271 272 st->filter_gpio = devm_gpiod_get_optional(dev, 273 "adi,filter", 274 GPIOD_OUT_HIGH); 275 if (IS_ERR(st->filter_gpio)) { 276 ret = PTR_ERR(st->filter_gpio); 277 dev_err(dev, "Failed to request filter GPIO: %d\n", ret); 278 return ret; 279 } 280 281 return 0; 282 } 283 284 static int ad7780_probe(struct spi_device *spi) 285 { 286 struct ad7780_state *st; 287 struct iio_dev *indio_dev; 288 int ret; 289 290 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 291 if (!indio_dev) 292 return -ENOMEM; 293 294 st = iio_priv(indio_dev); 295 st->gain = 1; 296 297 ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info); 298 299 st->chip_info = 300 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data]; 301 302 spi_set_drvdata(spi, indio_dev); 303 304 indio_dev->dev.parent = &spi->dev; 305 indio_dev->name = spi_get_device_id(spi)->name; 306 indio_dev->modes = INDIO_DIRECT_MODE; 307 indio_dev->channels = &st->chip_info->channel; 308 indio_dev->num_channels = 1; 309 indio_dev->info = &ad7780_info; 310 311 ret = ad7780_init_gpios(&spi->dev, st); 312 if (ret) 313 goto error_cleanup_buffer_and_trigger; 314 315 st->reg = devm_regulator_get(&spi->dev, "avdd"); 316 if (IS_ERR(st->reg)) 317 return PTR_ERR(st->reg); 318 319 ret = regulator_enable(st->reg); 320 if (ret) { 321 dev_err(&spi->dev, "Failed to enable specified AVdd supply\n"); 322 return ret; 323 } 324 325 ret = ad_sd_setup_buffer_and_trigger(indio_dev); 326 if (ret) 327 goto error_disable_reg; 328 329 ret = iio_device_register(indio_dev); 330 if (ret) 331 goto error_cleanup_buffer_and_trigger; 332 333 return 0; 334 335 error_cleanup_buffer_and_trigger: 336 ad_sd_cleanup_buffer_and_trigger(indio_dev); 337 error_disable_reg: 338 regulator_disable(st->reg); 339 340 return ret; 341 } 342 343 static int ad7780_remove(struct spi_device *spi) 344 { 345 struct iio_dev *indio_dev = spi_get_drvdata(spi); 346 struct ad7780_state *st = iio_priv(indio_dev); 347 348 iio_device_unregister(indio_dev); 349 ad_sd_cleanup_buffer_and_trigger(indio_dev); 350 351 regulator_disable(st->reg); 352 353 return 0; 354 } 355 356 static const struct spi_device_id ad7780_id[] = { 357 {"ad7170", ID_AD7170}, 358 {"ad7171", ID_AD7171}, 359 {"ad7780", ID_AD7780}, 360 {"ad7781", ID_AD7781}, 361 {} 362 }; 363 MODULE_DEVICE_TABLE(spi, ad7780_id); 364 365 static struct spi_driver ad7780_driver = { 366 .driver = { 367 .name = "ad7780", 368 }, 369 .probe = ad7780_probe, 370 .remove = ad7780_remove, 371 .id_table = ad7780_id, 372 }; 373 module_spi_driver(ad7780_driver); 374 375 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 376 MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs"); 377 MODULE_LICENSE("GPL v2"); 378