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 }; 207 208 #define AD7780_CHANNEL(bits, wordsize) \ 209 AD_SD_CHANNEL(1, 0, 0, bits, 32, (wordsize) - (bits)) 210 #define AD7170_CHANNEL(bits, wordsize) \ 211 AD_SD_CHANNEL_NO_SAMP_FREQ(1, 0, 0, bits, 32, (wordsize) - (bits)) 212 213 static const struct ad7780_chip_info ad7780_chip_info_tbl[] = { 214 [ID_AD7170] = { 215 .channel = AD7170_CHANNEL(12, 24), 216 .pattern = AD7170_PATTERN_GOOD, 217 .pattern_mask = AD7170_PATTERN_MASK, 218 .is_ad778x = false, 219 }, 220 [ID_AD7171] = { 221 .channel = AD7170_CHANNEL(16, 24), 222 .pattern = AD7170_PATTERN_GOOD, 223 .pattern_mask = AD7170_PATTERN_MASK, 224 .is_ad778x = false, 225 }, 226 [ID_AD7780] = { 227 .channel = AD7780_CHANNEL(24, 32), 228 .pattern = AD7780_PATTERN_GOOD, 229 .pattern_mask = AD7780_PATTERN_MASK, 230 .is_ad778x = true, 231 }, 232 [ID_AD7781] = { 233 .channel = AD7780_CHANNEL(20, 32), 234 .pattern = AD7780_PATTERN_GOOD, 235 .pattern_mask = AD7780_PATTERN_MASK, 236 .is_ad778x = true, 237 }, 238 }; 239 240 static const struct iio_info ad7780_info = { 241 .read_raw = ad7780_read_raw, 242 .write_raw = ad7780_write_raw, 243 }; 244 245 static int ad7780_init_gpios(struct device *dev, struct ad7780_state *st) 246 { 247 int ret; 248 249 st->powerdown_gpio = devm_gpiod_get_optional(dev, 250 "powerdown", 251 GPIOD_OUT_LOW); 252 if (IS_ERR(st->powerdown_gpio)) { 253 ret = PTR_ERR(st->powerdown_gpio); 254 dev_err(dev, "Failed to request powerdown GPIO: %d\n", ret); 255 return ret; 256 } 257 258 if (!st->chip_info->is_ad778x) 259 return 0; 260 261 262 st->gain_gpio = devm_gpiod_get_optional(dev, 263 "adi,gain", 264 GPIOD_OUT_HIGH); 265 if (IS_ERR(st->gain_gpio)) { 266 ret = PTR_ERR(st->gain_gpio); 267 dev_err(dev, "Failed to request gain GPIO: %d\n", ret); 268 return ret; 269 } 270 271 st->filter_gpio = devm_gpiod_get_optional(dev, 272 "adi,filter", 273 GPIOD_OUT_HIGH); 274 if (IS_ERR(st->filter_gpio)) { 275 ret = PTR_ERR(st->filter_gpio); 276 dev_err(dev, "Failed to request filter GPIO: %d\n", ret); 277 return ret; 278 } 279 280 return 0; 281 } 282 283 static int ad7780_probe(struct spi_device *spi) 284 { 285 struct ad7780_state *st; 286 struct iio_dev *indio_dev; 287 int ret; 288 289 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 290 if (!indio_dev) 291 return -ENOMEM; 292 293 st = iio_priv(indio_dev); 294 st->gain = 1; 295 296 ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info); 297 298 st->chip_info = 299 &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data]; 300 301 spi_set_drvdata(spi, indio_dev); 302 303 indio_dev->dev.parent = &spi->dev; 304 indio_dev->name = spi_get_device_id(spi)->name; 305 indio_dev->modes = INDIO_DIRECT_MODE; 306 indio_dev->channels = &st->chip_info->channel; 307 indio_dev->num_channels = 1; 308 indio_dev->info = &ad7780_info; 309 310 ret = ad7780_init_gpios(&spi->dev, st); 311 if (ret) 312 goto error_cleanup_buffer_and_trigger; 313 314 st->reg = devm_regulator_get(&spi->dev, "avdd"); 315 if (IS_ERR(st->reg)) 316 return PTR_ERR(st->reg); 317 318 ret = regulator_enable(st->reg); 319 if (ret) { 320 dev_err(&spi->dev, "Failed to enable specified AVdd supply\n"); 321 return ret; 322 } 323 324 ret = ad_sd_setup_buffer_and_trigger(indio_dev); 325 if (ret) 326 goto error_disable_reg; 327 328 ret = iio_device_register(indio_dev); 329 if (ret) 330 goto error_cleanup_buffer_and_trigger; 331 332 return 0; 333 334 error_cleanup_buffer_and_trigger: 335 ad_sd_cleanup_buffer_and_trigger(indio_dev); 336 error_disable_reg: 337 regulator_disable(st->reg); 338 339 return ret; 340 } 341 342 static int ad7780_remove(struct spi_device *spi) 343 { 344 struct iio_dev *indio_dev = spi_get_drvdata(spi); 345 struct ad7780_state *st = iio_priv(indio_dev); 346 347 iio_device_unregister(indio_dev); 348 ad_sd_cleanup_buffer_and_trigger(indio_dev); 349 350 regulator_disable(st->reg); 351 352 return 0; 353 } 354 355 static const struct spi_device_id ad7780_id[] = { 356 {"ad7170", ID_AD7170}, 357 {"ad7171", ID_AD7171}, 358 {"ad7780", ID_AD7780}, 359 {"ad7781", ID_AD7781}, 360 {} 361 }; 362 MODULE_DEVICE_TABLE(spi, ad7780_id); 363 364 static struct spi_driver ad7780_driver = { 365 .driver = { 366 .name = "ad7780", 367 }, 368 .probe = ad7780_probe, 369 .remove = ad7780_remove, 370 .id_table = ad7780_id, 371 }; 372 module_spi_driver(ad7780_driver); 373 374 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 375 MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs"); 376 MODULE_LICENSE("GPL v2"); 377