| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* SPI ADC driver for Analog Devices Inc. AD4695 and similar chips |
| 4 |
* |
| 5 |
* https://www.analog.com/en/products/ad4695.html |
| 6 |
* https://www.analog.com/en/products/ad4696.html |
| 7 |
* https://www.analog.com/en/products/ad4697.html |
| 8 |
* https://www.analog.com/en/products/ad4698.html |
| 9 |
* |
| 10 |
* Copyright 2024 Analog Devices Inc. |
| 11 |
* Copyright 2024 BayLibre, SAS |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <linux/align.h> |
| 15 |
#include <linux/bitfield.h> |
| 16 |
#include <linux/bits.h> |
| 17 |
#include <linux/compiler.h> |
| 18 |
#include <linux/delay.h> |
| 19 |
#include <linux/device.h> |
| 20 |
#include <linux/err.h> |
| 21 |
#include <linux/gpio/consumer.h> |
| 22 |
#include <linux/iio/buffer-dmaengine.h> |
| 23 |
#include <linux/iio/buffer.h> |
| 24 |
#include <linux/iio/iio.h> |
| 25 |
#include <linux/iio/triggered_buffer.h> |
| 26 |
#include <linux/iio/trigger_consumer.h> |
| 27 |
#include <linux/minmax.h> |
| 28 |
#include <linux/mutex.h> |
| 29 |
#include <linux/property.h> |
| 30 |
#include <linux/pwm.h> |
| 31 |
#include <linux/regmap.h> |
| 32 |
#include <linux/regulator/consumer.h> |
| 33 |
#include <linux/spi/offload/consumer.h> |
| 34 |
#include <linux/spi/offload/provider.h> |
| 35 |
#include <linux/spi/spi.h> |
| 36 |
#include <linux/units.h> |
| 37 |
|
| 38 |
#include <dt-bindings/iio/adc/adi,ad4695.h> |
| 39 |
|
| 40 |
/* AD4695 registers */ |
| 41 |
#define AD4695_REG_SPI_CONFIG_A 0x0000 |
| 42 |
#define AD4695_REG_SPI_CONFIG_A_SW_RST (BIT(7) | BIT(0)) |
| 43 |
#define AD4695_REG_SPI_CONFIG_A_ADDR_DIR BIT(5) |
| 44 |
#define AD4695_REG_SPI_CONFIG_B 0x0001 |
| 45 |
#define AD4695_REG_SPI_CONFIG_B_INST_MODE BIT(7) |
| 46 |
#define AD4695_REG_DEVICE_TYPE 0x0003 |
| 47 |
#define AD4695_REG_SCRATCH_PAD 0x000A |
| 48 |
#define AD4695_REG_VENDOR_L 0x000C |
| 49 |
#define AD4695_REG_VENDOR_H 0x000D |
| 50 |
#define AD4695_REG_LOOP_MODE 0x000E |
| 51 |
#define AD4695_REG_SPI_CONFIG_C 0x0010 |
| 52 |
#define AD4695_REG_SPI_CONFIG_C_MB_STRICT BIT(7) |
| 53 |
#define AD4695_REG_SPI_STATUS 0x0011 |
| 54 |
#define AD4695_REG_STATUS 0x0014 |
| 55 |
#define AD4695_REG_ALERT_STATUS1 0x0015 |
| 56 |
#define AD4695_REG_ALERT_STATUS2 0x0016 |
| 57 |
#define AD4695_REG_CLAMP_STATUS 0x001A |
| 58 |
#define AD4695_REG_SETUP 0x0020 |
| 59 |
#define AD4695_REG_SETUP_LDO_EN BIT(4) |
| 60 |
#define AD4695_REG_SETUP_SPI_MODE BIT(2) |
| 61 |
#define AD4695_REG_SETUP_SPI_CYC_CTRL BIT(1) |
| 62 |
#define AD4695_REG_REF_CTRL 0x0021 |
| 63 |
#define AD4695_REG_REF_CTRL_OV_MODE BIT(7) |
| 64 |
#define AD4695_REG_REF_CTRL_VREF_SET GENMASK(4, 2) |
| 65 |
#define AD4695_REG_REF_CTRL_REFHIZ_EN BIT(1) |
| 66 |
#define AD4695_REG_REF_CTRL_REFBUF_EN BIT(0) |
| 67 |
#define AD4695_REG_SEQ_CTRL 0x0022 |
| 68 |
#define AD4695_REG_SEQ_CTRL_STD_SEQ_EN BIT(7) |
| 69 |
#define AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS GENMASK(6, 0) |
| 70 |
#define AD4695_REG_AC_CTRL 0x0023 |
| 71 |
#define AD4695_REG_STD_SEQ_CONFIG 0x0024 |
| 72 |
#define AD4695_REG_GPIO_CTRL 0x0026 |
| 73 |
#define AD4695_REG_GP_MODE 0x0027 |
| 74 |
#define AD4695_REG_GP_MODE_BUSY_GP_SEL BIT(5) |
| 75 |
#define AD4695_REG_GP_MODE_BUSY_GP_EN BIT(1) |
| 76 |
#define AD4695_REG_TEMP_CTRL 0x0029 |
| 77 |
#define AD4695_REG_TEMP_CTRL_TEMP_EN BIT(0) |
| 78 |
#define AD4695_REG_CONFIG_IN(n) (0x0030 | (n)) |
| 79 |
#define AD4695_REG_CONFIG_IN_MODE BIT(6) |
| 80 |
#define AD4695_REG_CONFIG_IN_PAIR GENMASK(5, 4) |
| 81 |
#define AD4695_REG_CONFIG_IN_AINHIGHZ_EN BIT(3) |
| 82 |
#define AD4695_REG_CONFIG_IN_OSR_SET GENMASK(1, 0) |
| 83 |
#define AD4695_REG_UPPER_IN(n) (0x0040 | (2 * (n))) |
| 84 |
#define AD4695_REG_LOWER_IN(n) (0x0060 | (2 * (n))) |
| 85 |
#define AD4695_REG_HYST_IN(n) (0x0080 | (2 * (n))) |
| 86 |
#define AD4695_REG_OFFSET_IN(n) (0x00A0 | (2 * (n))) |
| 87 |
#define AD4695_REG_GAIN_IN(n) (0x00C0 | (2 * (n))) |
| 88 |
#define AD4695_REG_AS_SLOT(n) (0x0100 | (n)) |
| 89 |
#define AD4695_REG_AS_SLOT_INX GENMASK(3, 0) |
| 90 |
|
| 91 |
/* Conversion mode commands */ |
| 92 |
#define AD4695_CMD_EXIT_CNV_MODE 0x0A |
| 93 |
#define AD4695_CMD_TEMP_CHAN 0x0F |
| 94 |
#define AD4695_CMD_VOLTAGE_CHAN(n) (0x10 | (n)) |
| 95 |
|
| 96 |
/* timing specs */ |
| 97 |
#define AD4695_T_CONVERT_NS 415 |
| 98 |
#define AD4695_T_WAKEUP_HW_MS 3 |
| 99 |
#define AD4695_T_WAKEUP_SW_MS 3 |
| 100 |
#define AD4695_T_REFBUF_MS 100 |
| 101 |
#define AD4695_T_REGCONFIG_NS 20 |
| 102 |
#define AD4695_T_SCK_CNV_DELAY_NS 80 |
| 103 |
#define AD4695_T_CNVL_NS 80 |
| 104 |
#define AD4695_T_CNVH_NS 10 |
| 105 |
#define AD4695_REG_ACCESS_SCLK_HZ (10 * MEGA) |
| 106 |
|
| 107 |
/* Max number of voltage input channels. */ |
| 108 |
#define AD4695_MAX_VIN_CHANNELS 16 |
| 109 |
|
| 110 |
enum ad4695_in_pair { |
| 111 |
AD4695_IN_PAIR_REFGND, |
| 112 |
AD4695_IN_PAIR_COM, |
| 113 |
AD4695_IN_PAIR_EVEN_ODD, |
| 114 |
}; |
| 115 |
|
| 116 |
struct ad4695_chip_info { |
| 117 |
const char *name; |
| 118 |
int max_sample_rate; |
| 119 |
u32 t_acq_ns; |
| 120 |
u8 num_voltage_inputs; |
| 121 |
}; |
| 122 |
|
| 123 |
struct ad4695_channel_config { |
| 124 |
unsigned int channel; |
| 125 |
bool highz_en; |
| 126 |
bool bipolar; |
| 127 |
enum ad4695_in_pair pin_pairing; |
| 128 |
unsigned int common_mode_mv; |
| 129 |
unsigned int oversampling_ratio; |
| 130 |
}; |
| 131 |
|
| 132 |
struct ad4695_state { |
| 133 |
struct spi_device *spi; |
| 134 |
struct spi_offload *offload; |
| 135 |
struct spi_offload_trigger *offload_trigger; |
| 136 |
struct regmap *regmap; |
| 137 |
struct regmap *regmap16; |
| 138 |
struct gpio_desc *reset_gpio; |
| 139 |
/* currently PWM CNV only supported with SPI offload use */ |
| 140 |
struct pwm_device *cnv_pwm; |
| 141 |
/* protects against concurrent use of cnv_pwm */ |
| 142 |
struct mutex cnv_pwm_lock; |
| 143 |
/* offload also requires separate gpio to manually control CNV */ |
| 144 |
struct gpio_desc *cnv_gpio; |
| 145 |
/* voltages channels plus temperature and timestamp */ |
| 146 |
struct iio_chan_spec iio_chan[AD4695_MAX_VIN_CHANNELS + 2]; |
| 147 |
struct ad4695_channel_config channels_cfg[AD4695_MAX_VIN_CHANNELS]; |
| 148 |
const struct ad4695_chip_info *chip_info; |
| 149 |
int sample_freq_range[3]; |
| 150 |
/* Reference voltage. */ |
| 151 |
unsigned int vref_mv; |
| 152 |
/* Common mode input pin voltage. */ |
| 153 |
unsigned int com_mv; |
| 154 |
/* |
| 155 |
* 2 per voltage and temperature chan plus 1 xfer to trigger 1st |
| 156 |
* CNV. Excluding the trigger xfer, every 2nd xfer only serves |
| 157 |
* to control CS and add a delay between the last SCLK and next |
| 158 |
* CNV rising edges. |
| 159 |
*/ |
| 160 |
struct spi_transfer buf_read_xfer[AD4695_MAX_VIN_CHANNELS * 2 + 3]; |
| 161 |
struct spi_message buf_read_msg; |
| 162 |
/* Raw conversion data received. */ |
| 163 |
IIO_DECLARE_DMA_BUFFER_WITH_TS(u16, buf, AD4695_MAX_VIN_CHANNELS + 1); |
| 164 |
u16 raw_data; |
| 165 |
/* Commands to send for single conversion. */ |
| 166 |
u16 cnv_cmd; |
| 167 |
u8 cnv_cmd2; |
| 168 |
/* Buffer for storing data from regmap bus reads/writes */ |
| 169 |
u8 regmap_bus_data[4]; |
| 170 |
}; |
| 171 |
|
| 172 |
static const struct regmap_range ad4695_regmap_rd_ranges[] = { |
| 173 |
regmap_reg_range(AD4695_REG_SPI_CONFIG_A, AD4695_REG_SPI_CONFIG_B), |
| 174 |
regmap_reg_range(AD4695_REG_DEVICE_TYPE, AD4695_REG_DEVICE_TYPE), |
| 175 |
regmap_reg_range(AD4695_REG_SCRATCH_PAD, AD4695_REG_SCRATCH_PAD), |
| 176 |
regmap_reg_range(AD4695_REG_VENDOR_L, AD4695_REG_LOOP_MODE), |
| 177 |
regmap_reg_range(AD4695_REG_SPI_CONFIG_C, AD4695_REG_SPI_STATUS), |
| 178 |
regmap_reg_range(AD4695_REG_STATUS, AD4695_REG_ALERT_STATUS2), |
| 179 |
regmap_reg_range(AD4695_REG_CLAMP_STATUS, AD4695_REG_CLAMP_STATUS), |
| 180 |
regmap_reg_range(AD4695_REG_SETUP, AD4695_REG_AC_CTRL), |
| 181 |
regmap_reg_range(AD4695_REG_GPIO_CTRL, AD4695_REG_TEMP_CTRL), |
| 182 |
regmap_reg_range(AD4695_REG_CONFIG_IN(0), AD4695_REG_CONFIG_IN(15)), |
| 183 |
regmap_reg_range(AD4695_REG_AS_SLOT(0), AD4695_REG_AS_SLOT(127)), |
| 184 |
}; |
| 185 |
|
| 186 |
static const struct regmap_access_table ad4695_regmap_rd_table = { |
| 187 |
.yes_ranges = ad4695_regmap_rd_ranges, |
| 188 |
.n_yes_ranges = ARRAY_SIZE(ad4695_regmap_rd_ranges), |
| 189 |
}; |
| 190 |
|
| 191 |
static const struct regmap_range ad4695_regmap_wr_ranges[] = { |
| 192 |
regmap_reg_range(AD4695_REG_SPI_CONFIG_A, AD4695_REG_SPI_CONFIG_B), |
| 193 |
regmap_reg_range(AD4695_REG_SCRATCH_PAD, AD4695_REG_SCRATCH_PAD), |
| 194 |
regmap_reg_range(AD4695_REG_LOOP_MODE, AD4695_REG_LOOP_MODE), |
| 195 |
regmap_reg_range(AD4695_REG_SPI_CONFIG_C, AD4695_REG_SPI_STATUS), |
| 196 |
regmap_reg_range(AD4695_REG_SETUP, AD4695_REG_AC_CTRL), |
| 197 |
regmap_reg_range(AD4695_REG_GPIO_CTRL, AD4695_REG_TEMP_CTRL), |
| 198 |
regmap_reg_range(AD4695_REG_CONFIG_IN(0), AD4695_REG_CONFIG_IN(15)), |
| 199 |
regmap_reg_range(AD4695_REG_AS_SLOT(0), AD4695_REG_AS_SLOT(127)), |
| 200 |
}; |
| 201 |
|
| 202 |
static const struct regmap_access_table ad4695_regmap_wr_table = { |
| 203 |
.yes_ranges = ad4695_regmap_wr_ranges, |
| 204 |
.n_yes_ranges = ARRAY_SIZE(ad4695_regmap_wr_ranges), |
| 205 |
}; |
| 206 |
|
| 207 |
static const struct regmap_config ad4695_regmap_config = { |
| 208 |
.name = "ad4695-8", |
| 209 |
.reg_bits = 16, |
| 210 |
.val_bits = 8, |
| 211 |
.max_register = AD4695_REG_AS_SLOT(127), |
| 212 |
.rd_table = &ad4695_regmap_rd_table, |
| 213 |
.wr_table = &ad4695_regmap_wr_table, |
| 214 |
}; |
| 215 |
|
| 216 |
static const struct regmap_range ad4695_regmap16_rd_ranges[] = { |
| 217 |
regmap_reg_range(AD4695_REG_STD_SEQ_CONFIG, AD4695_REG_STD_SEQ_CONFIG), |
| 218 |
regmap_reg_range(AD4695_REG_UPPER_IN(0), AD4695_REG_GAIN_IN(15)), |
| 219 |
}; |
| 220 |
|
| 221 |
static const struct regmap_access_table ad4695_regmap16_rd_table = { |
| 222 |
.yes_ranges = ad4695_regmap16_rd_ranges, |
| 223 |
.n_yes_ranges = ARRAY_SIZE(ad4695_regmap16_rd_ranges), |
| 224 |
}; |
| 225 |
|
| 226 |
static const struct regmap_range ad4695_regmap16_wr_ranges[] = { |
| 227 |
regmap_reg_range(AD4695_REG_STD_SEQ_CONFIG, AD4695_REG_STD_SEQ_CONFIG), |
| 228 |
regmap_reg_range(AD4695_REG_UPPER_IN(0), AD4695_REG_GAIN_IN(15)), |
| 229 |
}; |
| 230 |
|
| 231 |
static const struct regmap_access_table ad4695_regmap16_wr_table = { |
| 232 |
.yes_ranges = ad4695_regmap16_wr_ranges, |
| 233 |
.n_yes_ranges = ARRAY_SIZE(ad4695_regmap16_wr_ranges), |
| 234 |
}; |
| 235 |
|
| 236 |
static const struct regmap_config ad4695_regmap16_config = { |
| 237 |
.name = "ad4695-16", |
| 238 |
.reg_bits = 16, |
| 239 |
.reg_stride = 2, |
| 240 |
.val_bits = 16, |
| 241 |
.val_format_endian = REGMAP_ENDIAN_LITTLE, |
| 242 |
.max_register = AD4695_REG_GAIN_IN(15), |
| 243 |
.rd_table = &ad4695_regmap16_rd_table, |
| 244 |
.wr_table = &ad4695_regmap16_wr_table, |
| 245 |
}; |
| 246 |
|
| 247 |
static int ad4695_regmap_bus_reg_write(void *context, const void *data, |
| 248 |
size_t count) |
| 249 |
{ |
| 250 |
struct ad4695_state *st = context; |
| 251 |
struct spi_transfer xfer = { |
| 252 |
.speed_hz = AD4695_REG_ACCESS_SCLK_HZ, |
| 253 |
.len = count, |
| 254 |
.tx_buf = st->regmap_bus_data, |
| 255 |
}; |
| 256 |
|
| 257 |
if (count > ARRAY_SIZE(st->regmap_bus_data)) |
| 258 |
return -EINVAL; |
| 259 |
|
| 260 |
memcpy(st->regmap_bus_data, data, count); |
| 261 |
|
| 262 |
return spi_sync_transfer(st->spi, &xfer, 1); |
| 263 |
} |
| 264 |
|
| 265 |
static int ad4695_regmap_bus_reg_read(void *context, const void *reg, |
| 266 |
size_t reg_size, void *val, |
| 267 |
size_t val_size) |
| 268 |
{ |
| 269 |
struct ad4695_state *st = context; |
| 270 |
struct spi_transfer xfers[] = { |
| 271 |
{ |
| 272 |
.speed_hz = AD4695_REG_ACCESS_SCLK_HZ, |
| 273 |
.len = reg_size, |
| 274 |
.tx_buf = &st->regmap_bus_data[0], |
| 275 |
}, { |
| 276 |
.speed_hz = AD4695_REG_ACCESS_SCLK_HZ, |
| 277 |
.len = val_size, |
| 278 |
.rx_buf = &st->regmap_bus_data[2], |
| 279 |
}, |
| 280 |
}; |
| 281 |
int ret; |
| 282 |
|
| 283 |
if (reg_size > 2) |
| 284 |
return -EINVAL; |
| 285 |
|
| 286 |
if (val_size > 2) |
| 287 |
return -EINVAL; |
| 288 |
|
| 289 |
memcpy(&st->regmap_bus_data[0], reg, reg_size); |
| 290 |
|
| 291 |
ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); |
| 292 |
if (ret) |
| 293 |
return ret; |
| 294 |
|
| 295 |
memcpy(val, &st->regmap_bus_data[2], val_size); |
| 296 |
|
| 297 |
return 0; |
| 298 |
} |
| 299 |
|
| 300 |
static const struct regmap_bus ad4695_regmap_bus = { |
| 301 |
.write = ad4695_regmap_bus_reg_write, |
| 302 |
.read = ad4695_regmap_bus_reg_read, |
| 303 |
.read_flag_mask = 0x80, |
| 304 |
.reg_format_endian_default = REGMAP_ENDIAN_BIG, |
| 305 |
.val_format_endian_default = REGMAP_ENDIAN_BIG, |
| 306 |
}; |
| 307 |
|
| 308 |
enum { |
| 309 |
AD4695_SCAN_TYPE_OSR_1, |
| 310 |
AD4695_SCAN_TYPE_OSR_4, |
| 311 |
AD4695_SCAN_TYPE_OSR_16, |
| 312 |
AD4695_SCAN_TYPE_OSR_64, |
| 313 |
}; |
| 314 |
|
| 315 |
static const struct iio_scan_type ad4695_scan_type_offload_u[] = { |
| 316 |
[AD4695_SCAN_TYPE_OSR_1] = { |
| 317 |
.sign = 'u', |
| 318 |
.realbits = 16, |
| 319 |
.shift = 3, |
| 320 |
.storagebits = 32, |
| 321 |
}, |
| 322 |
[AD4695_SCAN_TYPE_OSR_4] = { |
| 323 |
.sign = 'u', |
| 324 |
.realbits = 17, |
| 325 |
.shift = 2, |
| 326 |
.storagebits = 32, |
| 327 |
}, |
| 328 |
[AD4695_SCAN_TYPE_OSR_16] = { |
| 329 |
.sign = 'u', |
| 330 |
.realbits = 18, |
| 331 |
.shift = 1, |
| 332 |
.storagebits = 32, |
| 333 |
}, |
| 334 |
[AD4695_SCAN_TYPE_OSR_64] = { |
| 335 |
.sign = 'u', |
| 336 |
.realbits = 19, |
| 337 |
.storagebits = 32, |
| 338 |
}, |
| 339 |
}; |
| 340 |
|
| 341 |
static const struct iio_scan_type ad4695_scan_type_offload_s[] = { |
| 342 |
[AD4695_SCAN_TYPE_OSR_1] = { |
| 343 |
.sign = 's', |
| 344 |
.realbits = 16, |
| 345 |
.shift = 3, |
| 346 |
.storagebits = 32, |
| 347 |
}, |
| 348 |
[AD4695_SCAN_TYPE_OSR_4] = { |
| 349 |
.sign = 's', |
| 350 |
.realbits = 17, |
| 351 |
.shift = 2, |
| 352 |
.storagebits = 32, |
| 353 |
}, |
| 354 |
[AD4695_SCAN_TYPE_OSR_16] = { |
| 355 |
.sign = 's', |
| 356 |
.realbits = 18, |
| 357 |
.shift = 1, |
| 358 |
.storagebits = 32, |
| 359 |
}, |
| 360 |
[AD4695_SCAN_TYPE_OSR_64] = { |
| 361 |
.sign = 's', |
| 362 |
.realbits = 19, |
| 363 |
.storagebits = 32, |
| 364 |
}, |
| 365 |
}; |
| 366 |
|
| 367 |
static const struct iio_chan_spec ad4695_channel_template = { |
| 368 |
.type = IIO_VOLTAGE, |
| 369 |
.indexed = 1, |
| 370 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 371 |
BIT(IIO_CHAN_INFO_SCALE) | |
| 372 |
BIT(IIO_CHAN_INFO_OFFSET) | |
| 373 |
BIT(IIO_CHAN_INFO_CALIBSCALE) | |
| 374 |
BIT(IIO_CHAN_INFO_CALIBBIAS), |
| 375 |
.info_mask_separate_available = BIT(IIO_CHAN_INFO_CALIBSCALE) | |
| 376 |
BIT(IIO_CHAN_INFO_CALIBBIAS), |
| 377 |
.scan_type = { |
| 378 |
.sign = 'u', |
| 379 |
.realbits = 16, |
| 380 |
.storagebits = 16, |
| 381 |
}, |
| 382 |
}; |
| 383 |
|
| 384 |
static const struct iio_chan_spec ad4695_temp_channel_template = { |
| 385 |
.address = AD4695_CMD_TEMP_CHAN, |
| 386 |
.type = IIO_TEMP, |
| 387 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 388 |
BIT(IIO_CHAN_INFO_SCALE) | |
| 389 |
BIT(IIO_CHAN_INFO_OFFSET), |
| 390 |
.scan_type = { |
| 391 |
.sign = 's', |
| 392 |
.realbits = 16, |
| 393 |
.storagebits = 16, |
| 394 |
}, |
| 395 |
}; |
| 396 |
|
| 397 |
static const struct iio_chan_spec ad4695_soft_timestamp_channel_template = |
| 398 |
IIO_CHAN_SOFT_TIMESTAMP(0); |
| 399 |
|
| 400 |
static const char * const ad4695_power_supplies[] = { |
| 401 |
"avdd", "vio" |
| 402 |
}; |
| 403 |
|
| 404 |
static const int ad4695_oversampling_ratios[] = { |
| 405 |
1, 4, 16, 64, |
| 406 |
}; |
| 407 |
|
| 408 |
static const struct ad4695_chip_info ad4695_chip_info = { |
| 409 |
.name = "ad4695", |
| 410 |
.max_sample_rate = 500 * KILO, |
| 411 |
.t_acq_ns = 1715, |
| 412 |
.num_voltage_inputs = 16, |
| 413 |
}; |
| 414 |
|
| 415 |
static const struct ad4695_chip_info ad4696_chip_info = { |
| 416 |
.name = "ad4696", |
| 417 |
.max_sample_rate = 1 * MEGA, |
| 418 |
.t_acq_ns = 715, |
| 419 |
.num_voltage_inputs = 16, |
| 420 |
}; |
| 421 |
|
| 422 |
static const struct ad4695_chip_info ad4697_chip_info = { |
| 423 |
.name = "ad4697", |
| 424 |
.max_sample_rate = 500 * KILO, |
| 425 |
.t_acq_ns = 1715, |
| 426 |
.num_voltage_inputs = 8, |
| 427 |
}; |
| 428 |
|
| 429 |
static const struct ad4695_chip_info ad4698_chip_info = { |
| 430 |
.name = "ad4698", |
| 431 |
.max_sample_rate = 1 * MEGA, |
| 432 |
.t_acq_ns = 715, |
| 433 |
.num_voltage_inputs = 8, |
| 434 |
}; |
| 435 |
|
| 436 |
static void ad4695_cnv_manual_trigger(struct ad4695_state *st) |
| 437 |
{ |
| 438 |
gpiod_set_value_cansleep(st->cnv_gpio, 1); |
| 439 |
ndelay(10); |
| 440 |
gpiod_set_value_cansleep(st->cnv_gpio, 0); |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* ad4695_set_single_cycle_mode - Set the device in single cycle mode |
| 445 |
* @st: The AD4695 state |
| 446 |
* @channel: The first channel to read |
| 447 |
* |
| 448 |
* As per the datasheet, to enable single cycle mode, we need to set |
| 449 |
* STD_SEQ_EN=0, NUM_SLOTS_AS=0 and CYC_CTRL=1 (Table 15). Setting SPI_MODE=1 |
| 450 |
* triggers the first conversion using the channel in AS_SLOT0. |
| 451 |
* |
| 452 |
* Context: can sleep, must be called with iio_device_claim_direct held |
| 453 |
* Return: 0 on success, a negative error code on failure |
| 454 |
*/ |
| 455 |
static int ad4695_set_single_cycle_mode(struct ad4695_state *st, |
| 456 |
unsigned int channel) |
| 457 |
{ |
| 458 |
int ret; |
| 459 |
|
| 460 |
ret = regmap_clear_bits(st->regmap, AD4695_REG_SEQ_CTRL, |
| 461 |
AD4695_REG_SEQ_CTRL_STD_SEQ_EN | |
| 462 |
AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS); |
| 463 |
if (ret) |
| 464 |
return ret; |
| 465 |
|
| 466 |
ret = regmap_write(st->regmap, AD4695_REG_AS_SLOT(0), |
| 467 |
FIELD_PREP(AD4695_REG_AS_SLOT_INX, channel)); |
| 468 |
if (ret) |
| 469 |
return ret; |
| 470 |
|
| 471 |
return regmap_set_bits(st->regmap, AD4695_REG_SETUP, |
| 472 |
AD4695_REG_SETUP_SPI_MODE | |
| 473 |
AD4695_REG_SETUP_SPI_CYC_CTRL); |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* ad4695_enter_advanced_sequencer_mode - Put the ADC in advanced sequencer mode |
| 478 |
* @st: The driver state |
| 479 |
* @n: The number of slots to use - must be >= 2, <= 128 |
| 480 |
* |
| 481 |
* As per the datasheet, to enable advanced sequencer, we need to set |
| 482 |
* STD_SEQ_EN=0, NUM_SLOTS_AS=n-1 and CYC_CTRL=0 (Table 15). Setting SPI_MODE=1 |
| 483 |
* triggers the first conversion using the channel in AS_SLOT0. |
| 484 |
* |
| 485 |
* Return: 0 on success, a negative error code on failure |
| 486 |
*/ |
| 487 |
static int ad4695_enter_advanced_sequencer_mode(struct ad4695_state *st, u32 n) |
| 488 |
{ |
| 489 |
int ret; |
| 490 |
|
| 491 |
ret = regmap_update_bits(st->regmap, AD4695_REG_SEQ_CTRL, |
| 492 |
AD4695_REG_SEQ_CTRL_STD_SEQ_EN | |
| 493 |
AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS, |
| 494 |
FIELD_PREP(AD4695_REG_SEQ_CTRL_STD_SEQ_EN, 0) | |
| 495 |
FIELD_PREP(AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS, n - 1)); |
| 496 |
if (ret) |
| 497 |
return ret; |
| 498 |
|
| 499 |
return regmap_update_bits(st->regmap, AD4695_REG_SETUP, |
| 500 |
AD4695_REG_SETUP_SPI_MODE | AD4695_REG_SETUP_SPI_CYC_CTRL, |
| 501 |
FIELD_PREP(AD4695_REG_SETUP_SPI_MODE, 1) | |
| 502 |
FIELD_PREP(AD4695_REG_SETUP_SPI_CYC_CTRL, 0)); |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
* ad4695_exit_conversion_mode - Exit conversion mode |
| 507 |
* @st: The AD4695 state |
| 508 |
* |
| 509 |
* Sends SPI command to exit conversion mode. |
| 510 |
* |
| 511 |
* Return: 0 on success, a negative error code on failure |
| 512 |
*/ |
| 513 |
static int ad4695_exit_conversion_mode(struct ad4695_state *st) |
| 514 |
{ |
| 515 |
/* |
| 516 |
* An extra transfer is needed to trigger a conversion here so |
| 517 |
* that we can be 100% sure the command will be processed by the |
| 518 |
* ADC, rather than relying on it to be in the correct state |
| 519 |
* when this function is called (this chip has a quirk where the |
| 520 |
* command only works when reading a conversion, and if the |
| 521 |
* previous conversion was already read then it won't work). The |
| 522 |
* actual conversion command is then run at the slower |
| 523 |
* AD4695_REG_ACCESS_SCLK_HZ speed to guarantee this works. |
| 524 |
*/ |
| 525 |
struct spi_transfer xfers[] = { |
| 526 |
{ |
| 527 |
.delay.value = AD4695_T_CNVL_NS, |
| 528 |
.delay.unit = SPI_DELAY_UNIT_NSECS, |
| 529 |
.cs_change = 1, |
| 530 |
.cs_change_delay.value = AD4695_T_CNVH_NS, |
| 531 |
.cs_change_delay.unit = SPI_DELAY_UNIT_NSECS, |
| 532 |
}, |
| 533 |
{ |
| 534 |
.speed_hz = AD4695_REG_ACCESS_SCLK_HZ, |
| 535 |
.tx_buf = &st->cnv_cmd2, |
| 536 |
.len = 1, |
| 537 |
.delay.value = AD4695_T_REGCONFIG_NS, |
| 538 |
.delay.unit = SPI_DELAY_UNIT_NSECS, |
| 539 |
}, |
| 540 |
}; |
| 541 |
|
| 542 |
/* |
| 543 |
* Technically, could do a 5-bit transfer, but shifting to start of |
| 544 |
* 8 bits instead for better SPI controller support. |
| 545 |
*/ |
| 546 |
st->cnv_cmd2 = AD4695_CMD_EXIT_CNV_MODE << 3; |
| 547 |
|
| 548 |
if (st->cnv_gpio) { |
| 549 |
ad4695_cnv_manual_trigger(st); |
| 550 |
|
| 551 |
/* |
| 552 |
* In this case, CNV is not connected to CS, so we don't need |
| 553 |
* the extra CS toggle to trigger the conversion and toggling |
| 554 |
* CS would have no effect. |
| 555 |
*/ |
| 556 |
return spi_sync_transfer(st->spi, &xfers[1], 1); |
| 557 |
} |
| 558 |
|
| 559 |
return spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); |
| 560 |
} |
| 561 |
|
| 562 |
static int ad4695_set_ref_voltage(struct ad4695_state *st, int vref_mv) |
| 563 |
{ |
| 564 |
u8 val; |
| 565 |
|
| 566 |
if (vref_mv >= 2400 && vref_mv <= 2750) |
| 567 |
val = 0; |
| 568 |
else if (vref_mv > 2750 && vref_mv <= 3250) |
| 569 |
val = 1; |
| 570 |
else if (vref_mv > 3250 && vref_mv <= 3750) |
| 571 |
val = 2; |
| 572 |
else if (vref_mv > 3750 && vref_mv <= 4500) |
| 573 |
val = 3; |
| 574 |
else if (vref_mv > 4500 && vref_mv <= 5100) |
| 575 |
val = 4; |
| 576 |
else |
| 577 |
return -EINVAL; |
| 578 |
|
| 579 |
return regmap_update_bits(st->regmap, AD4695_REG_REF_CTRL, |
| 580 |
AD4695_REG_REF_CTRL_VREF_SET, |
| 581 |
FIELD_PREP(AD4695_REG_REF_CTRL_VREF_SET, val)); |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* ad4695_osr_to_regval - convert ratio to OSR register value |
| 586 |
* @ratio: ratio to check |
| 587 |
* |
| 588 |
* Check if ratio is present in the list of available ratios and return |
| 589 |
* the corresponding value that needs to be written to the register to |
| 590 |
* select that ratio. |
| 591 |
* |
| 592 |
* Returns: register value (0 to 3) or -EINVAL if there is not an exact |
| 593 |
* match |
| 594 |
*/ |
| 595 |
static int ad4695_osr_to_regval(int ratio) |
| 596 |
{ |
| 597 |
int i; |
| 598 |
|
| 599 |
for (i = 0; i < ARRAY_SIZE(ad4695_oversampling_ratios); i++) { |
| 600 |
if (ratio == ad4695_oversampling_ratios[i]) |
| 601 |
return i; |
| 602 |
} |
| 603 |
|
| 604 |
return -EINVAL; |
| 605 |
} |
| 606 |
|
| 607 |
static int ad4695_write_chn_cfg(struct ad4695_state *st, |
| 608 |
struct ad4695_channel_config *cfg) |
| 609 |
{ |
| 610 |
u32 mask, val; |
| 611 |
|
| 612 |
mask = AD4695_REG_CONFIG_IN_MODE; |
| 613 |
val = FIELD_PREP(AD4695_REG_CONFIG_IN_MODE, cfg->bipolar ? 1 : 0); |
| 614 |
|
| 615 |
mask |= AD4695_REG_CONFIG_IN_PAIR; |
| 616 |
val |= FIELD_PREP(AD4695_REG_CONFIG_IN_PAIR, cfg->pin_pairing); |
| 617 |
|
| 618 |
mask |= AD4695_REG_CONFIG_IN_AINHIGHZ_EN; |
| 619 |
val |= FIELD_PREP(AD4695_REG_CONFIG_IN_AINHIGHZ_EN, |
| 620 |
cfg->highz_en ? 1 : 0); |
| 621 |
|
| 622 |
return regmap_update_bits(st->regmap, |
| 623 |
AD4695_REG_CONFIG_IN(cfg->channel), |
| 624 |
mask, val); |
| 625 |
} |
| 626 |
|
| 627 |
static int ad4695_buffer_preenable(struct iio_dev *indio_dev) |
| 628 |
{ |
| 629 |
struct ad4695_state *st = iio_priv(indio_dev); |
| 630 |
struct spi_transfer *xfer; |
| 631 |
u8 temp_chan_bit = st->chip_info->num_voltage_inputs; |
| 632 |
u32 bit, num_xfer, num_slots; |
| 633 |
u32 temp_en = 0; |
| 634 |
int ret, rx_buf_offset = 0; |
| 635 |
|
| 636 |
/* |
| 637 |
* We are using the advanced sequencer since it is the only way to read |
| 638 |
* multiple channels that allows individual configuration of each |
| 639 |
* voltage input channel. Slot 0 in the advanced sequencer is used to |
| 640 |
* account for the gap between trigger polls - we don't read data from |
| 641 |
* this slot. Each enabled voltage channel is assigned a slot starting |
| 642 |
* with slot 1. |
| 643 |
*/ |
| 644 |
num_slots = 1; |
| 645 |
|
| 646 |
memset(st->buf_read_xfer, 0, sizeof(st->buf_read_xfer)); |
| 647 |
|
| 648 |
/* First xfer is only to trigger conversion of slot 1, so no rx. */ |
| 649 |
xfer = &st->buf_read_xfer[0]; |
| 650 |
xfer->cs_change = 1; |
| 651 |
xfer->delay.value = st->chip_info->t_acq_ns; |
| 652 |
xfer->delay.unit = SPI_DELAY_UNIT_NSECS; |
| 653 |
xfer->cs_change_delay.value = AD4695_T_CONVERT_NS; |
| 654 |
xfer->cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; |
| 655 |
num_xfer = 1; |
| 656 |
|
| 657 |
iio_for_each_active_channel(indio_dev, bit) { |
| 658 |
xfer = &st->buf_read_xfer[num_xfer]; |
| 659 |
xfer->bits_per_word = 16; |
| 660 |
xfer->rx_buf = &st->buf[rx_buf_offset++]; |
| 661 |
xfer->len = 2; |
| 662 |
|
| 663 |
if (bit == temp_chan_bit) { |
| 664 |
temp_en = 1; |
| 665 |
} else { |
| 666 |
ret = regmap_write(st->regmap, |
| 667 |
AD4695_REG_AS_SLOT(num_slots), |
| 668 |
FIELD_PREP(AD4695_REG_AS_SLOT_INX, bit)); |
| 669 |
if (ret) |
| 670 |
return ret; |
| 671 |
|
| 672 |
num_slots++; |
| 673 |
} |
| 674 |
|
| 675 |
num_xfer++; |
| 676 |
|
| 677 |
/* |
| 678 |
* We need to add a blank xfer in data reads, to meet the timing |
| 679 |
* requirement of a minimum delay between the last SCLK rising |
| 680 |
* edge and the CS deassert. |
| 681 |
*/ |
| 682 |
xfer = &st->buf_read_xfer[num_xfer]; |
| 683 |
xfer->delay.value = AD4695_T_SCK_CNV_DELAY_NS; |
| 684 |
xfer->delay.unit = SPI_DELAY_UNIT_NSECS; |
| 685 |
xfer->cs_change = 1; |
| 686 |
xfer->cs_change_delay.value = AD4695_T_CONVERT_NS; |
| 687 |
xfer->cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; |
| 688 |
|
| 689 |
num_xfer++; |
| 690 |
} |
| 691 |
|
| 692 |
/* |
| 693 |
* The advanced sequencer requires that at least 2 slots are enabled. |
| 694 |
* Since slot 0 is always used for other purposes, we need only 1 |
| 695 |
* enabled voltage channel to meet this requirement. If the temperature |
| 696 |
* channel is the only enabled channel, we need to add one more slot in |
| 697 |
* the sequence but not read from it. This is because the temperature |
| 698 |
* sensor is sampled at the end of the channel sequence in advanced |
| 699 |
* sequencer mode (see datasheet page 38). |
| 700 |
* |
| 701 |
* From the iio_for_each_active_channel() block above, we now have an |
| 702 |
* xfer with data followed by a blank xfer to allow us to meet the |
| 703 |
* timing spec, so move both of those up before adding an extra to |
| 704 |
* handle the temperature-only case. |
| 705 |
*/ |
| 706 |
if (num_slots < 2) { |
| 707 |
/* Move last two xfers */ |
| 708 |
st->buf_read_xfer[num_xfer] = st->buf_read_xfer[num_xfer - 1]; |
| 709 |
st->buf_read_xfer[num_xfer - 1] = st->buf_read_xfer[num_xfer - 2]; |
| 710 |
num_xfer++; |
| 711 |
|
| 712 |
/* Modify inserted xfer for extra slot. */ |
| 713 |
xfer = &st->buf_read_xfer[num_xfer - 3]; |
| 714 |
memset(xfer, 0, sizeof(*xfer)); |
| 715 |
xfer->cs_change = 1; |
| 716 |
xfer->delay.value = st->chip_info->t_acq_ns; |
| 717 |
xfer->delay.unit = SPI_DELAY_UNIT_NSECS; |
| 718 |
xfer->cs_change_delay.value = AD4695_T_CONVERT_NS; |
| 719 |
xfer->cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; |
| 720 |
xfer++; |
| 721 |
|
| 722 |
/* and add the extra slot in the sequencer */ |
| 723 |
ret = regmap_write(st->regmap, |
| 724 |
AD4695_REG_AS_SLOT(num_slots), |
| 725 |
FIELD_PREP(AD4695_REG_AS_SLOT_INX, 0)); |
| 726 |
if (ret) |
| 727 |
return ret; |
| 728 |
|
| 729 |
num_slots++; |
| 730 |
|
| 731 |
/* |
| 732 |
* We still want to point at the last xfer when finished, so |
| 733 |
* update the pointer. |
| 734 |
*/ |
| 735 |
xfer = &st->buf_read_xfer[num_xfer - 1]; |
| 736 |
} |
| 737 |
|
| 738 |
/* |
| 739 |
* Don't keep CS asserted after last xfer. Also triggers conversion of |
| 740 |
* slot 0. |
| 741 |
*/ |
| 742 |
xfer->cs_change = 0; |
| 743 |
|
| 744 |
/* |
| 745 |
* Temperature channel isn't included in the sequence, but rather |
| 746 |
* controlled by setting a bit in the TEMP_CTRL register. |
| 747 |
*/ |
| 748 |
|
| 749 |
ret = regmap_update_bits(st->regmap, AD4695_REG_TEMP_CTRL, |
| 750 |
AD4695_REG_TEMP_CTRL_TEMP_EN, |
| 751 |
FIELD_PREP(AD4695_REG_TEMP_CTRL_TEMP_EN, temp_en)); |
| 752 |
if (ret) |
| 753 |
return ret; |
| 754 |
|
| 755 |
spi_message_init_with_transfers(&st->buf_read_msg, st->buf_read_xfer, |
| 756 |
num_xfer); |
| 757 |
|
| 758 |
ret = spi_optimize_message(st->spi, &st->buf_read_msg); |
| 759 |
if (ret) |
| 760 |
return ret; |
| 761 |
|
| 762 |
/* This triggers conversion of slot 0. */ |
| 763 |
ret = ad4695_enter_advanced_sequencer_mode(st, num_slots); |
| 764 |
if (ret) |
| 765 |
spi_unoptimize_message(&st->buf_read_msg); |
| 766 |
|
| 767 |
return ret; |
| 768 |
} |
| 769 |
|
| 770 |
static int ad4695_buffer_postdisable(struct iio_dev *indio_dev) |
| 771 |
{ |
| 772 |
struct ad4695_state *st = iio_priv(indio_dev); |
| 773 |
int ret; |
| 774 |
|
| 775 |
ret = ad4695_exit_conversion_mode(st); |
| 776 |
if (ret) |
| 777 |
return ret; |
| 778 |
|
| 779 |
spi_unoptimize_message(&st->buf_read_msg); |
| 780 |
|
| 781 |
return 0; |
| 782 |
} |
| 783 |
|
| 784 |
static const struct iio_buffer_setup_ops ad4695_buffer_setup_ops = { |
| 785 |
.preenable = ad4695_buffer_preenable, |
| 786 |
.postdisable = ad4695_buffer_postdisable, |
| 787 |
}; |
| 788 |
|
| 789 |
static irqreturn_t ad4695_trigger_handler(int irq, void *p) |
| 790 |
{ |
| 791 |
struct iio_poll_func *pf = p; |
| 792 |
struct iio_dev *indio_dev = pf->indio_dev; |
| 793 |
struct ad4695_state *st = iio_priv(indio_dev); |
| 794 |
int ret; |
| 795 |
|
| 796 |
ret = spi_sync(st->spi, &st->buf_read_msg); |
| 797 |
if (ret) |
| 798 |
goto out; |
| 799 |
|
| 800 |
iio_push_to_buffers_with_ts(indio_dev, st->buf, sizeof(st->buf), |
| 801 |
pf->timestamp); |
| 802 |
|
| 803 |
out: |
| 804 |
iio_trigger_notify_done(indio_dev->trig); |
| 805 |
|
| 806 |
return IRQ_HANDLED; |
| 807 |
} |
| 808 |
|
| 809 |
static int ad4695_offload_buffer_postenable(struct iio_dev *indio_dev) |
| 810 |
{ |
| 811 |
struct ad4695_state *st = iio_priv(indio_dev); |
| 812 |
struct spi_offload_trigger_config config = { |
| 813 |
.type = SPI_OFFLOAD_TRIGGER_DATA_READY, |
| 814 |
}; |
| 815 |
struct spi_transfer *xfer = &st->buf_read_xfer[0]; |
| 816 |
struct pwm_state state; |
| 817 |
u8 temp_chan_bit = st->chip_info->num_voltage_inputs; |
| 818 |
u8 num_slots = 0; |
| 819 |
u8 temp_en = 0; |
| 820 |
unsigned int bit; |
| 821 |
int ret; |
| 822 |
|
| 823 |
iio_for_each_active_channel(indio_dev, bit) { |
| 824 |
if (bit == temp_chan_bit) { |
| 825 |
temp_en = 1; |
| 826 |
continue; |
| 827 |
} |
| 828 |
|
| 829 |
ret = regmap_write(st->regmap, AD4695_REG_AS_SLOT(num_slots), |
| 830 |
FIELD_PREP(AD4695_REG_AS_SLOT_INX, bit)); |
| 831 |
if (ret) |
| 832 |
return ret; |
| 833 |
|
| 834 |
num_slots++; |
| 835 |
} |
| 836 |
|
| 837 |
/* |
| 838 |
* For non-offload, we could discard data to work around this |
| 839 |
* restriction, but with offload, that is not possible. |
| 840 |
*/ |
| 841 |
if (num_slots < 2) { |
| 842 |
dev_err(&st->spi->dev, |
| 843 |
"At least two voltage channels must be enabled.\n"); |
| 844 |
return -EINVAL; |
| 845 |
} |
| 846 |
|
| 847 |
ret = regmap_update_bits(st->regmap, AD4695_REG_TEMP_CTRL, |
| 848 |
AD4695_REG_TEMP_CTRL_TEMP_EN, |
| 849 |
FIELD_PREP(AD4695_REG_TEMP_CTRL_TEMP_EN, |
| 850 |
temp_en)); |
| 851 |
if (ret) |
| 852 |
return ret; |
| 853 |
|
| 854 |
/* Each BUSY event means just one sample for one channel is ready. */ |
| 855 |
memset(xfer, 0, sizeof(*xfer)); |
| 856 |
xfer->offload_flags = SPI_OFFLOAD_XFER_RX_STREAM; |
| 857 |
/* Using 19 bits per word to allow for possible oversampling */ |
| 858 |
xfer->bits_per_word = 19; |
| 859 |
xfer->len = 4; |
| 860 |
|
| 861 |
spi_message_init_with_transfers(&st->buf_read_msg, xfer, 1); |
| 862 |
st->buf_read_msg.offload = st->offload; |
| 863 |
|
| 864 |
ret = spi_optimize_message(st->spi, &st->buf_read_msg); |
| 865 |
if (ret) |
| 866 |
return ret; |
| 867 |
|
| 868 |
/* |
| 869 |
* NB: technically, this is part the SPI offload trigger enable, but it |
| 870 |
* doesn't work to call it from the offload trigger enable callback |
| 871 |
* because it requires accessing the SPI bus. Calling it from the |
| 872 |
* trigger enable callback could cause a deadlock. |
| 873 |
*/ |
| 874 |
ret = regmap_set_bits(st->regmap, AD4695_REG_GP_MODE, |
| 875 |
AD4695_REG_GP_MODE_BUSY_GP_EN); |
| 876 |
if (ret) |
| 877 |
goto err_unoptimize_message; |
| 878 |
|
| 879 |
ret = ad4695_enter_advanced_sequencer_mode(st, num_slots); |
| 880 |
if (ret) |
| 881 |
goto err_disable_busy_output; |
| 882 |
|
| 883 |
ret = spi_offload_trigger_enable(st->offload, st->offload_trigger, |
| 884 |
&config); |
| 885 |
if (ret) |
| 886 |
goto err_exit_conversion_mode; |
| 887 |
|
| 888 |
mutex_lock(&st->cnv_pwm_lock); |
| 889 |
pwm_get_state(st->cnv_pwm, &state); |
| 890 |
/* |
| 891 |
* PWM subsystem generally rounds down, so requesting 2x minimum high |
| 892 |
* time ensures that we meet the minimum high time in any case. |
| 893 |
*/ |
| 894 |
state.duty_cycle = AD4695_T_CNVH_NS * 2; |
| 895 |
ret = pwm_apply_might_sleep(st->cnv_pwm, &state); |
| 896 |
mutex_unlock(&st->cnv_pwm_lock); |
| 897 |
if (ret) |
| 898 |
goto err_offload_trigger_disable; |
| 899 |
|
| 900 |
return 0; |
| 901 |
|
| 902 |
err_offload_trigger_disable: |
| 903 |
spi_offload_trigger_disable(st->offload, st->offload_trigger); |
| 904 |
|
| 905 |
err_exit_conversion_mode: |
| 906 |
ad4695_exit_conversion_mode(st); |
| 907 |
|
| 908 |
err_disable_busy_output: |
| 909 |
regmap_clear_bits(st->regmap, AD4695_REG_GP_MODE, |
| 910 |
AD4695_REG_GP_MODE_BUSY_GP_EN); |
| 911 |
|
| 912 |
err_unoptimize_message: |
| 913 |
spi_unoptimize_message(&st->buf_read_msg); |
| 914 |
|
| 915 |
return ret; |
| 916 |
} |
| 917 |
|
| 918 |
static int ad4695_offload_buffer_predisable(struct iio_dev *indio_dev) |
| 919 |
{ |
| 920 |
struct ad4695_state *st = iio_priv(indio_dev); |
| 921 |
struct pwm_state state; |
| 922 |
int ret; |
| 923 |
|
| 924 |
scoped_guard(mutex, &st->cnv_pwm_lock) { |
| 925 |
pwm_get_state(st->cnv_pwm, &state); |
| 926 |
state.duty_cycle = 0; |
| 927 |
ret = pwm_apply_might_sleep(st->cnv_pwm, &state); |
| 928 |
if (ret) |
| 929 |
return ret; |
| 930 |
} |
| 931 |
|
| 932 |
spi_offload_trigger_disable(st->offload, st->offload_trigger); |
| 933 |
|
| 934 |
/* |
| 935 |
* ad4695_exit_conversion_mode() triggers a conversion, so it has to be |
| 936 |
* done after spi_offload_trigger_disable(). |
| 937 |
*/ |
| 938 |
ret = ad4695_exit_conversion_mode(st); |
| 939 |
if (ret) |
| 940 |
return ret; |
| 941 |
|
| 942 |
ret = regmap_clear_bits(st->regmap, AD4695_REG_GP_MODE, |
| 943 |
AD4695_REG_GP_MODE_BUSY_GP_EN); |
| 944 |
if (ret) |
| 945 |
return ret; |
| 946 |
|
| 947 |
spi_unoptimize_message(&st->buf_read_msg); |
| 948 |
|
| 949 |
return 0; |
| 950 |
} |
| 951 |
|
| 952 |
static const struct iio_buffer_setup_ops ad4695_offload_buffer_setup_ops = { |
| 953 |
.postenable = ad4695_offload_buffer_postenable, |
| 954 |
.predisable = ad4695_offload_buffer_predisable, |
| 955 |
}; |
| 956 |
|
| 957 |
/** |
| 958 |
* ad4695_read_one_sample - Read a single sample using single-cycle mode |
| 959 |
* @st: The AD4695 state |
| 960 |
* @address: The address of the channel to read |
| 961 |
* |
| 962 |
* Upon successful return, the sample will be stored in `st->raw_data`. |
| 963 |
* |
| 964 |
* Context: can sleep, must be called with iio_device_claim_direct held |
| 965 |
* Return: 0 on success, a negative error code on failure |
| 966 |
*/ |
| 967 |
static int ad4695_read_one_sample(struct ad4695_state *st, unsigned int address) |
| 968 |
{ |
| 969 |
struct spi_transfer xfers[2] = { |
| 970 |
{ |
| 971 |
.speed_hz = AD4695_REG_ACCESS_SCLK_HZ, |
| 972 |
.bits_per_word = 16, |
| 973 |
.tx_buf = &st->cnv_cmd, |
| 974 |
.len = 2, |
| 975 |
}, |
| 976 |
{ |
| 977 |
/* Required delay between last SCLK and CNV/CS */ |
| 978 |
.delay.value = AD4695_T_SCK_CNV_DELAY_NS, |
| 979 |
.delay.unit = SPI_DELAY_UNIT_NSECS, |
| 980 |
} |
| 981 |
}; |
| 982 |
int ret; |
| 983 |
|
| 984 |
ret = ad4695_set_single_cycle_mode(st, address); |
| 985 |
if (ret) |
| 986 |
return ret; |
| 987 |
|
| 988 |
/* |
| 989 |
* If CNV is connected to CS, the previous function will have triggered |
| 990 |
* the conversion, otherwise, we do it manually. |
| 991 |
*/ |
| 992 |
if (st->cnv_gpio) |
| 993 |
ad4695_cnv_manual_trigger(st); |
| 994 |
|
| 995 |
/* |
| 996 |
* Setting the first channel to the temperature channel isn't supported |
| 997 |
* in single-cycle mode, so we have to do an extra conversion to read |
| 998 |
* the temperature. |
| 999 |
*/ |
| 1000 |
if (address == AD4695_CMD_TEMP_CHAN) { |
| 1001 |
st->cnv_cmd = AD4695_CMD_TEMP_CHAN << 11; |
| 1002 |
|
| 1003 |
ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); |
| 1004 |
if (ret) |
| 1005 |
return ret; |
| 1006 |
|
| 1007 |
/* |
| 1008 |
* If CNV is connected to CS, the previous function will have |
| 1009 |
* triggered the conversion, otherwise, we do it manually. |
| 1010 |
*/ |
| 1011 |
if (st->cnv_gpio) |
| 1012 |
ad4695_cnv_manual_trigger(st); |
| 1013 |
} |
| 1014 |
|
| 1015 |
/* Then read the result and exit conversion mode. */ |
| 1016 |
st->cnv_cmd = AD4695_CMD_EXIT_CNV_MODE << 11; |
| 1017 |
xfers[0].rx_buf = &st->raw_data; |
| 1018 |
|
| 1019 |
return spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers)); |
| 1020 |
} |
| 1021 |
|
| 1022 |
static int __ad4695_read_info_raw(struct ad4695_state *st, |
| 1023 |
struct iio_chan_spec const *chan, |
| 1024 |
int *val) |
| 1025 |
{ |
| 1026 |
u8 realbits = chan->scan_type.realbits; |
| 1027 |
int ret; |
| 1028 |
|
| 1029 |
ret = ad4695_read_one_sample(st, chan->address); |
| 1030 |
if (ret) |
| 1031 |
return ret; |
| 1032 |
|
| 1033 |
if (chan->scan_type.sign == 's') |
| 1034 |
*val = sign_extend32(st->raw_data, realbits - 1); |
| 1035 |
else |
| 1036 |
*val = st->raw_data; |
| 1037 |
|
| 1038 |
return IIO_VAL_INT; |
| 1039 |
} |
| 1040 |
|
| 1041 |
static int ad4695_read_raw(struct iio_dev *indio_dev, |
| 1042 |
struct iio_chan_spec const *chan, |
| 1043 |
int *val, int *val2, long mask) |
| 1044 |
{ |
| 1045 |
struct ad4695_state *st = iio_priv(indio_dev); |
| 1046 |
const struct iio_scan_type *scan_type; |
| 1047 |
struct ad4695_channel_config *cfg; |
| 1048 |
unsigned int reg_val; |
| 1049 |
int ret, tmp; |
| 1050 |
u8 realbits; |
| 1051 |
|
| 1052 |
if (chan->type == IIO_VOLTAGE) |
| 1053 |
cfg = &st->channels_cfg[chan->scan_index]; |
| 1054 |
|
| 1055 |
scan_type = iio_get_current_scan_type(indio_dev, chan); |
| 1056 |
if (IS_ERR(scan_type)) |
| 1057 |
return PTR_ERR(scan_type); |
| 1058 |
|
| 1059 |
realbits = scan_type->realbits; |
| 1060 |
|
| 1061 |
switch (mask) { |
| 1062 |
case IIO_CHAN_INFO_RAW: |
| 1063 |
if (!iio_device_claim_direct(indio_dev)) |
| 1064 |
return -EBUSY; |
| 1065 |
|
| 1066 |
ret = __ad4695_read_info_raw(st, chan, val); |
| 1067 |
iio_device_release_direct(indio_dev); |
| 1068 |
return ret; |
| 1069 |
case IIO_CHAN_INFO_SCALE: |
| 1070 |
switch (chan->type) { |
| 1071 |
case IIO_VOLTAGE: |
| 1072 |
*val = st->vref_mv; |
| 1073 |
*val2 = realbits; |
| 1074 |
return IIO_VAL_FRACTIONAL_LOG2; |
| 1075 |
case IIO_TEMP: |
| 1076 |
/* T_scale (°C) = raw * V_REF (mV) / (-1.8 mV/°C * 2^16) */ |
| 1077 |
*val = st->vref_mv * -556; |
| 1078 |
*val2 = 16; |
| 1079 |
return IIO_VAL_FRACTIONAL_LOG2; |
| 1080 |
default: |
| 1081 |
return -EINVAL; |
| 1082 |
} |
| 1083 |
case IIO_CHAN_INFO_OFFSET: |
| 1084 |
switch (chan->type) { |
| 1085 |
case IIO_VOLTAGE: |
| 1086 |
if (cfg->pin_pairing == AD4695_IN_PAIR_COM) |
| 1087 |
*val = st->com_mv * (1 << realbits) / st->vref_mv; |
| 1088 |
else if (cfg->pin_pairing == AD4695_IN_PAIR_EVEN_ODD) |
| 1089 |
*val = cfg->common_mode_mv * (1 << realbits) / st->vref_mv; |
| 1090 |
else |
| 1091 |
*val = 0; |
| 1092 |
|
| 1093 |
return IIO_VAL_INT; |
| 1094 |
case IIO_TEMP: |
| 1095 |
/* T_offset (°C) = -725 mV / (-1.8 mV/°C) */ |
| 1096 |
/* T_offset (raw) = T_offset (°C) * (-1.8 mV/°C) * 2^16 / V_REF (mV) */ |
| 1097 |
*val = -47513600; |
| 1098 |
*val2 = st->vref_mv; |
| 1099 |
return IIO_VAL_FRACTIONAL; |
| 1100 |
default: |
| 1101 |
return -EINVAL; |
| 1102 |
} |
| 1103 |
case IIO_CHAN_INFO_CALIBSCALE: |
| 1104 |
switch (chan->type) { |
| 1105 |
case IIO_VOLTAGE: |
| 1106 |
if (!iio_device_claim_direct(indio_dev)) |
| 1107 |
return -EBUSY; |
| 1108 |
ret = regmap_read(st->regmap16, |
| 1109 |
AD4695_REG_GAIN_IN(chan->scan_index), |
| 1110 |
®_val); |
| 1111 |
iio_device_release_direct(indio_dev); |
| 1112 |
if (ret) |
| 1113 |
return ret; |
| 1114 |
*val = reg_val; |
| 1115 |
*val2 = 15; |
| 1116 |
|
| 1117 |
return IIO_VAL_FRACTIONAL_LOG2; |
| 1118 |
default: |
| 1119 |
return -EINVAL; |
| 1120 |
} |
| 1121 |
case IIO_CHAN_INFO_CALIBBIAS: |
| 1122 |
switch (chan->type) |
| 1123 |
case IIO_VOLTAGE: { |
| 1124 |
if (!iio_device_claim_direct(indio_dev)) |
| 1125 |
return -EBUSY; |
| 1126 |
ret = regmap_read(st->regmap16, |
| 1127 |
AD4695_REG_OFFSET_IN(chan->scan_index), |
| 1128 |
®_val); |
| 1129 |
iio_device_release_direct(indio_dev); |
| 1130 |
if (ret) |
| 1131 |
return ret; |
| 1132 |
|
| 1133 |
tmp = sign_extend32(reg_val, 15); |
| 1134 |
|
| 1135 |
switch (cfg->oversampling_ratio) { |
| 1136 |
case 1: |
| 1137 |
*val = tmp / 4; |
| 1138 |
*val2 = abs(tmp) % 4 * MICRO / 4; |
| 1139 |
break; |
| 1140 |
case 4: |
| 1141 |
*val = tmp / 2; |
| 1142 |
*val2 = abs(tmp) % 2 * MICRO / 2; |
| 1143 |
break; |
| 1144 |
case 16: |
| 1145 |
*val = tmp; |
| 1146 |
*val2 = 0; |
| 1147 |
break; |
| 1148 |
case 64: |
| 1149 |
*val = tmp * 2; |
| 1150 |
*val2 = 0; |
| 1151 |
break; |
| 1152 |
default: |
| 1153 |
return -EINVAL; |
| 1154 |
} |
| 1155 |
|
| 1156 |
if (tmp < 0 && *val2) { |
| 1157 |
*val *= -1; |
| 1158 |
*val2 *= -1; |
| 1159 |
} |
| 1160 |
|
| 1161 |
return IIO_VAL_INT_PLUS_MICRO; |
| 1162 |
default: |
| 1163 |
return -EINVAL; |
| 1164 |
} |
| 1165 |
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 1166 |
switch (chan->type) { |
| 1167 |
case IIO_VOLTAGE: |
| 1168 |
*val = cfg->oversampling_ratio; |
| 1169 |
return IIO_VAL_INT; |
| 1170 |
default: |
| 1171 |
return -EINVAL; |
| 1172 |
} |
| 1173 |
case IIO_CHAN_INFO_SAMP_FREQ: { |
| 1174 |
struct pwm_state state; |
| 1175 |
unsigned int osr = 1; |
| 1176 |
|
| 1177 |
if (chan->type == IIO_VOLTAGE) |
| 1178 |
osr = cfg->oversampling_ratio; |
| 1179 |
|
| 1180 |
ret = pwm_get_state_hw(st->cnv_pwm, &state); |
| 1181 |
if (ret) |
| 1182 |
return ret; |
| 1183 |
|
| 1184 |
/* |
| 1185 |
* The effective sampling frequency for a channel is the input |
| 1186 |
* frequency divided by the channel's OSR value. |
| 1187 |
*/ |
| 1188 |
*val = DIV_ROUND_UP_ULL(NSEC_PER_SEC, state.period * osr); |
| 1189 |
|
| 1190 |
return IIO_VAL_INT; |
| 1191 |
} |
| 1192 |
default: |
| 1193 |
return -EINVAL; |
| 1194 |
} |
| 1195 |
} |
| 1196 |
|
| 1197 |
static int ad4695_write_raw_get_fmt(struct iio_dev *indio_dev, |
| 1198 |
struct iio_chan_spec const *chan, |
| 1199 |
long mask) |
| 1200 |
{ |
| 1201 |
switch (mask) { |
| 1202 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 1203 |
return IIO_VAL_INT; |
| 1204 |
default: |
| 1205 |
return IIO_VAL_INT_PLUS_MICRO; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
static int ad4695_set_osr_val(struct ad4695_state *st, |
| 1210 |
struct iio_chan_spec const *chan, |
| 1211 |
int val) |
| 1212 |
{ |
| 1213 |
int osr = ad4695_osr_to_regval(val); |
| 1214 |
|
| 1215 |
if (osr < 0) |
| 1216 |
return osr; |
| 1217 |
|
| 1218 |
switch (chan->type) { |
| 1219 |
case IIO_VOLTAGE: |
| 1220 |
st->channels_cfg[chan->scan_index].oversampling_ratio = val; |
| 1221 |
return regmap_update_bits(st->regmap, |
| 1222 |
AD4695_REG_CONFIG_IN(chan->scan_index), |
| 1223 |
AD4695_REG_CONFIG_IN_OSR_SET, |
| 1224 |
FIELD_PREP(AD4695_REG_CONFIG_IN_OSR_SET, osr)); |
| 1225 |
default: |
| 1226 |
return -EINVAL; |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
static unsigned int ad4695_get_calibbias(int val, int val2, int osr) |
| 1231 |
{ |
| 1232 |
int val_calc, scale; |
| 1233 |
|
| 1234 |
switch (osr) { |
| 1235 |
case 4: |
| 1236 |
scale = 4; |
| 1237 |
break; |
| 1238 |
case 16: |
| 1239 |
scale = 2; |
| 1240 |
break; |
| 1241 |
case 64: |
| 1242 |
scale = 1; |
| 1243 |
break; |
| 1244 |
default: |
| 1245 |
scale = 8; |
| 1246 |
break; |
| 1247 |
} |
| 1248 |
|
| 1249 |
val = clamp_t(int, val, S32_MIN / 8, S32_MAX / 8); |
| 1250 |
|
| 1251 |
/* val2 range is (-MICRO, MICRO) if val == 0, otherwise [0, MICRO) */ |
| 1252 |
if (val < 0) |
| 1253 |
val_calc = val * scale - val2 * scale / MICRO; |
| 1254 |
else if (val2 < 0) |
| 1255 |
/* if val2 < 0 then val == 0 */ |
| 1256 |
val_calc = val2 * scale / (int)MICRO; |
| 1257 |
else |
| 1258 |
val_calc = val * scale + val2 * scale / MICRO; |
| 1259 |
|
| 1260 |
val_calc /= 2; |
| 1261 |
|
| 1262 |
return clamp_t(int, val_calc, S16_MIN, S16_MAX); |
| 1263 |
} |
| 1264 |
|
| 1265 |
static int __ad4695_write_raw(struct iio_dev *indio_dev, |
| 1266 |
struct iio_chan_spec const *chan, |
| 1267 |
int val, int val2, long mask) |
| 1268 |
{ |
| 1269 |
struct ad4695_state *st = iio_priv(indio_dev); |
| 1270 |
unsigned int reg_val; |
| 1271 |
unsigned int osr = 1; |
| 1272 |
|
| 1273 |
if (chan->type == IIO_VOLTAGE) |
| 1274 |
osr = st->channels_cfg[chan->scan_index].oversampling_ratio; |
| 1275 |
|
| 1276 |
switch (mask) { |
| 1277 |
case IIO_CHAN_INFO_CALIBSCALE: |
| 1278 |
switch (chan->type) { |
| 1279 |
case IIO_VOLTAGE: |
| 1280 |
if (val < 0 || val2 < 0) |
| 1281 |
reg_val = 0; |
| 1282 |
else if (val > 1) |
| 1283 |
reg_val = U16_MAX; |
| 1284 |
else |
| 1285 |
reg_val = (val * (1 << 16) + |
| 1286 |
mul_u64_u32_div(val2, 1 << 16, |
| 1287 |
MICRO)) / 2; |
| 1288 |
|
| 1289 |
return regmap_write(st->regmap16, |
| 1290 |
AD4695_REG_GAIN_IN(chan->scan_index), |
| 1291 |
reg_val); |
| 1292 |
default: |
| 1293 |
return -EINVAL; |
| 1294 |
} |
| 1295 |
case IIO_CHAN_INFO_CALIBBIAS: |
| 1296 |
switch (chan->type) { |
| 1297 |
case IIO_VOLTAGE: |
| 1298 |
reg_val = ad4695_get_calibbias(val, val2, osr); |
| 1299 |
return regmap_write(st->regmap16, |
| 1300 |
AD4695_REG_OFFSET_IN(chan->scan_index), |
| 1301 |
reg_val); |
| 1302 |
default: |
| 1303 |
return -EINVAL; |
| 1304 |
} |
| 1305 |
case IIO_CHAN_INFO_SAMP_FREQ: { |
| 1306 |
struct pwm_state state; |
| 1307 |
/* |
| 1308 |
* Limit the maximum acceptable sample rate according to |
| 1309 |
* the channel's oversampling ratio. |
| 1310 |
*/ |
| 1311 |
u64 max_osr_rate = DIV_ROUND_UP_ULL(st->chip_info->max_sample_rate, |
| 1312 |
osr); |
| 1313 |
|
| 1314 |
if (val <= 0 || val > max_osr_rate) |
| 1315 |
return -EINVAL; |
| 1316 |
|
| 1317 |
guard(mutex)(&st->cnv_pwm_lock); |
| 1318 |
pwm_get_state(st->cnv_pwm, &state); |
| 1319 |
/* |
| 1320 |
* The required sample frequency for a given OSR is the |
| 1321 |
* input frequency multiplied by it. |
| 1322 |
*/ |
| 1323 |
state.period = DIV_ROUND_UP_ULL(NSEC_PER_SEC, val * osr); |
| 1324 |
return pwm_apply_might_sleep(st->cnv_pwm, &state); |
| 1325 |
} |
| 1326 |
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 1327 |
return ad4695_set_osr_val(st, chan, val); |
| 1328 |
default: |
| 1329 |
return -EINVAL; |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
static int ad4695_write_raw(struct iio_dev *indio_dev, |
| 1334 |
struct iio_chan_spec const *chan, |
| 1335 |
int val, int val2, long mask) |
| 1336 |
{ |
| 1337 |
int ret; |
| 1338 |
|
| 1339 |
if (!iio_device_claim_direct(indio_dev)) |
| 1340 |
return -EBUSY; |
| 1341 |
ret = __ad4695_write_raw(indio_dev, chan, val, val2, mask); |
| 1342 |
iio_device_release_direct(indio_dev); |
| 1343 |
|
| 1344 |
return ret; |
| 1345 |
} |
| 1346 |
|
| 1347 |
static int ad4695_read_avail(struct iio_dev *indio_dev, |
| 1348 |
struct iio_chan_spec const *chan, |
| 1349 |
const int **vals, int *type, int *length, |
| 1350 |
long mask) |
| 1351 |
{ |
| 1352 |
int ret; |
| 1353 |
static const int ad4695_calibscale_available[6] = { |
| 1354 |
/* Range of 0 (inclusive) to 2 (exclusive) */ |
| 1355 |
0, 15, 1, 15, U16_MAX, 15 |
| 1356 |
}; |
| 1357 |
static const int ad4695_calibbias_available[4][6] = { |
| 1358 |
/* |
| 1359 |
* Datasheet says FSR/8 which translates to signed/4. The step |
| 1360 |
* depends on oversampling ratio, so we need four different |
| 1361 |
* ranges to select from. |
| 1362 |
*/ |
| 1363 |
{ |
| 1364 |
S16_MIN / 4, 0, |
| 1365 |
0, MICRO / 4, |
| 1366 |
S16_MAX / 4, S16_MAX % 4 * MICRO / 4 |
| 1367 |
}, |
| 1368 |
{ |
| 1369 |
S16_MIN / 2, 0, |
| 1370 |
0, MICRO / 2, |
| 1371 |
S16_MAX / 2, S16_MAX % 2 * MICRO / 2, |
| 1372 |
}, |
| 1373 |
{ |
| 1374 |
S16_MIN, 0, |
| 1375 |
1, 0, |
| 1376 |
S16_MAX, 0, |
| 1377 |
}, |
| 1378 |
{ |
| 1379 |
S16_MIN * 2, 0, |
| 1380 |
2, 0, |
| 1381 |
S16_MAX * 2, 0, |
| 1382 |
}, |
| 1383 |
}; |
| 1384 |
struct ad4695_state *st = iio_priv(indio_dev); |
| 1385 |
unsigned int osr = 1; |
| 1386 |
|
| 1387 |
if (chan->type == IIO_VOLTAGE) |
| 1388 |
osr = st->channels_cfg[chan->scan_index].oversampling_ratio; |
| 1389 |
|
| 1390 |
switch (mask) { |
| 1391 |
case IIO_CHAN_INFO_CALIBSCALE: |
| 1392 |
switch (chan->type) { |
| 1393 |
case IIO_VOLTAGE: |
| 1394 |
*vals = ad4695_calibscale_available; |
| 1395 |
*type = IIO_VAL_FRACTIONAL_LOG2; |
| 1396 |
return IIO_AVAIL_RANGE; |
| 1397 |
default: |
| 1398 |
return -EINVAL; |
| 1399 |
} |
| 1400 |
case IIO_CHAN_INFO_CALIBBIAS: |
| 1401 |
switch (chan->type) { |
| 1402 |
case IIO_VOLTAGE: |
| 1403 |
ret = ad4695_osr_to_regval(osr); |
| 1404 |
if (ret < 0) |
| 1405 |
return ret; |
| 1406 |
/* |
| 1407 |
* Select the appropriate calibbias array based on the |
| 1408 |
* OSR value in the register. |
| 1409 |
*/ |
| 1410 |
*vals = ad4695_calibbias_available[ret]; |
| 1411 |
*type = IIO_VAL_INT_PLUS_MICRO; |
| 1412 |
return IIO_AVAIL_RANGE; |
| 1413 |
default: |
| 1414 |
return -EINVAL; |
| 1415 |
} |
| 1416 |
case IIO_CHAN_INFO_SAMP_FREQ: |
| 1417 |
/* Max sample rate for the channel depends on OSR */ |
| 1418 |
st->sample_freq_range[2] = |
| 1419 |
DIV_ROUND_UP_ULL(st->chip_info->max_sample_rate, osr); |
| 1420 |
*vals = st->sample_freq_range; |
| 1421 |
*type = IIO_VAL_INT; |
| 1422 |
return IIO_AVAIL_RANGE; |
| 1423 |
case IIO_CHAN_INFO_OVERSAMPLING_RATIO: |
| 1424 |
switch (chan->type) { |
| 1425 |
case IIO_VOLTAGE: |
| 1426 |
*vals = ad4695_oversampling_ratios; |
| 1427 |
*length = ARRAY_SIZE(ad4695_oversampling_ratios); |
| 1428 |
*type = IIO_VAL_INT; |
| 1429 |
return IIO_AVAIL_LIST; |
| 1430 |
default: |
| 1431 |
return -EINVAL; |
| 1432 |
} |
| 1433 |
default: |
| 1434 |
return -EINVAL; |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
static int ad4695_debugfs_reg_access(struct iio_dev *indio_dev, |
| 1439 |
unsigned int reg, |
| 1440 |
unsigned int writeval, |
| 1441 |
unsigned int *readval) |
| 1442 |
{ |
| 1443 |
struct ad4695_state *st = iio_priv(indio_dev); |
| 1444 |
int ret = -EINVAL; |
| 1445 |
|
| 1446 |
if (!iio_device_claim_direct(indio_dev)) |
| 1447 |
return -EBUSY; |
| 1448 |
|
| 1449 |
if (readval) { |
| 1450 |
if (regmap_check_range_table(st->regmap, reg, |
| 1451 |
&ad4695_regmap_rd_table)) |
| 1452 |
ret = regmap_read(st->regmap, reg, readval); |
| 1453 |
if (regmap_check_range_table(st->regmap16, reg, |
| 1454 |
&ad4695_regmap16_rd_table)) |
| 1455 |
ret = regmap_read(st->regmap16, reg, readval); |
| 1456 |
} else { |
| 1457 |
if (regmap_check_range_table(st->regmap, reg, |
| 1458 |
&ad4695_regmap_wr_table)) |
| 1459 |
ret = regmap_write(st->regmap, reg, writeval); |
| 1460 |
if (regmap_check_range_table(st->regmap16, reg, |
| 1461 |
&ad4695_regmap16_wr_table)) |
| 1462 |
ret = regmap_write(st->regmap16, reg, writeval); |
| 1463 |
} |
| 1464 |
iio_device_release_direct(indio_dev); |
| 1465 |
|
| 1466 |
return ret; |
| 1467 |
} |
| 1468 |
|
| 1469 |
static int ad4695_get_current_scan_type(const struct iio_dev *indio_dev, |
| 1470 |
const struct iio_chan_spec *chan) |
| 1471 |
{ |
| 1472 |
struct ad4695_state *st = iio_priv(indio_dev); |
| 1473 |
unsigned int osr = st->channels_cfg[chan->scan_index].oversampling_ratio; |
| 1474 |
|
| 1475 |
switch (osr) { |
| 1476 |
case 1: |
| 1477 |
return AD4695_SCAN_TYPE_OSR_1; |
| 1478 |
case 4: |
| 1479 |
return AD4695_SCAN_TYPE_OSR_4; |
| 1480 |
case 16: |
| 1481 |
return AD4695_SCAN_TYPE_OSR_16; |
| 1482 |
case 64: |
| 1483 |
return AD4695_SCAN_TYPE_OSR_64; |
| 1484 |
default: |
| 1485 |
return -EINVAL; |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
static const struct iio_info ad4695_info = { |
| 1490 |
.read_raw = &ad4695_read_raw, |
| 1491 |
.write_raw_get_fmt = &ad4695_write_raw_get_fmt, |
| 1492 |
.write_raw = &ad4695_write_raw, |
| 1493 |
.read_avail = &ad4695_read_avail, |
| 1494 |
.debugfs_reg_access = &ad4695_debugfs_reg_access, |
| 1495 |
}; |
| 1496 |
|
| 1497 |
static const struct iio_info ad4695_offload_info = { |
| 1498 |
.read_raw = &ad4695_read_raw, |
| 1499 |
.write_raw_get_fmt = &ad4695_write_raw_get_fmt, |
| 1500 |
.write_raw = &ad4695_write_raw, |
| 1501 |
.get_current_scan_type = &ad4695_get_current_scan_type, |
| 1502 |
.read_avail = &ad4695_read_avail, |
| 1503 |
.debugfs_reg_access = &ad4695_debugfs_reg_access, |
| 1504 |
}; |
| 1505 |
|
| 1506 |
static int ad4695_parse_channel_cfg(struct ad4695_state *st) |
| 1507 |
{ |
| 1508 |
struct device *dev = &st->spi->dev; |
| 1509 |
struct ad4695_channel_config *chan_cfg; |
| 1510 |
struct iio_chan_spec *iio_chan; |
| 1511 |
int ret, i; |
| 1512 |
|
| 1513 |
/* populate defaults */ |
| 1514 |
for (i = 0; i < st->chip_info->num_voltage_inputs; i++) { |
| 1515 |
chan_cfg = &st->channels_cfg[i]; |
| 1516 |
iio_chan = &st->iio_chan[i]; |
| 1517 |
|
| 1518 |
chan_cfg->highz_en = true; |
| 1519 |
chan_cfg->channel = i; |
| 1520 |
|
| 1521 |
/* This is the default OSR after reset */ |
| 1522 |
chan_cfg->oversampling_ratio = 1; |
| 1523 |
|
| 1524 |
*iio_chan = ad4695_channel_template; |
| 1525 |
iio_chan->channel = i; |
| 1526 |
iio_chan->scan_index = i; |
| 1527 |
iio_chan->address = AD4695_CMD_VOLTAGE_CHAN(i); |
| 1528 |
} |
| 1529 |
|
| 1530 |
/* modify based on firmware description */ |
| 1531 |
device_for_each_child_node_scoped(dev, child) { |
| 1532 |
u32 reg, val; |
| 1533 |
|
| 1534 |
ret = fwnode_property_read_u32(child, "reg", ®); |
| 1535 |
if (ret) |
| 1536 |
return dev_err_probe(dev, ret, |
| 1537 |
"failed to read reg property (%s)\n", |
| 1538 |
fwnode_get_name(child)); |
| 1539 |
|
| 1540 |
if (reg >= st->chip_info->num_voltage_inputs) |
| 1541 |
return dev_err_probe(dev, -EINVAL, |
| 1542 |
"reg out of range (%s)\n", |
| 1543 |
fwnode_get_name(child)); |
| 1544 |
|
| 1545 |
iio_chan = &st->iio_chan[reg]; |
| 1546 |
chan_cfg = &st->channels_cfg[reg]; |
| 1547 |
|
| 1548 |
chan_cfg->highz_en = |
| 1549 |
!fwnode_property_read_bool(child, "adi,no-high-z"); |
| 1550 |
chan_cfg->bipolar = fwnode_property_read_bool(child, "bipolar"); |
| 1551 |
|
| 1552 |
ret = fwnode_property_read_u32(child, "common-mode-channel", |
| 1553 |
&val); |
| 1554 |
if (ret && ret != -EINVAL) |
| 1555 |
return dev_err_probe(dev, ret, |
| 1556 |
"failed to read common-mode-channel (%s)\n", |
| 1557 |
fwnode_get_name(child)); |
| 1558 |
|
| 1559 |
if (ret == -EINVAL || val == AD4695_COMMON_MODE_REFGND) |
| 1560 |
chan_cfg->pin_pairing = AD4695_IN_PAIR_REFGND; |
| 1561 |
else if (val == AD4695_COMMON_MODE_COM) |
| 1562 |
chan_cfg->pin_pairing = AD4695_IN_PAIR_COM; |
| 1563 |
else |
| 1564 |
chan_cfg->pin_pairing = AD4695_IN_PAIR_EVEN_ODD; |
| 1565 |
|
| 1566 |
if (chan_cfg->pin_pairing == AD4695_IN_PAIR_EVEN_ODD && |
| 1567 |
val % 2 == 0) |
| 1568 |
return dev_err_probe(dev, -EINVAL, |
| 1569 |
"common-mode-channel must be odd number (%s)\n", |
| 1570 |
fwnode_get_name(child)); |
| 1571 |
|
| 1572 |
if (chan_cfg->pin_pairing == AD4695_IN_PAIR_EVEN_ODD && |
| 1573 |
val != reg + 1) |
| 1574 |
return dev_err_probe(dev, -EINVAL, |
| 1575 |
"common-mode-channel must be next consecutive channel (%s)\n", |
| 1576 |
fwnode_get_name(child)); |
| 1577 |
|
| 1578 |
if (chan_cfg->pin_pairing == AD4695_IN_PAIR_EVEN_ODD) { |
| 1579 |
char name[5]; |
| 1580 |
|
| 1581 |
snprintf(name, sizeof(name), "in%d", reg + 1); |
| 1582 |
|
| 1583 |
ret = devm_regulator_get_enable_read_voltage(dev, name); |
| 1584 |
if (ret < 0) |
| 1585 |
return dev_err_probe(dev, ret, |
| 1586 |
"failed to get %s voltage (%s)\n", |
| 1587 |
name, fwnode_get_name(child)); |
| 1588 |
|
| 1589 |
chan_cfg->common_mode_mv = ret / 1000; |
| 1590 |
} |
| 1591 |
|
| 1592 |
if (chan_cfg->bipolar && |
| 1593 |
chan_cfg->pin_pairing == AD4695_IN_PAIR_REFGND) |
| 1594 |
return dev_err_probe(dev, -EINVAL, |
| 1595 |
"bipolar mode is not available for inputs paired with REFGND (%s).\n", |
| 1596 |
fwnode_get_name(child)); |
| 1597 |
|
| 1598 |
if (chan_cfg->bipolar) |
| 1599 |
iio_chan->scan_type.sign = 's'; |
| 1600 |
|
| 1601 |
ret = ad4695_write_chn_cfg(st, chan_cfg); |
| 1602 |
if (ret) |
| 1603 |
return ret; |
| 1604 |
} |
| 1605 |
|
| 1606 |
/* Temperature channel must be next scan index after voltage channels. */ |
| 1607 |
st->iio_chan[i] = ad4695_temp_channel_template; |
| 1608 |
st->iio_chan[i].scan_index = i; |
| 1609 |
i++; |
| 1610 |
|
| 1611 |
st->iio_chan[i] = ad4695_soft_timestamp_channel_template; |
| 1612 |
st->iio_chan[i].scan_index = i; |
| 1613 |
|
| 1614 |
return 0; |
| 1615 |
} |
| 1616 |
|
| 1617 |
static bool ad4695_offload_trigger_match(struct spi_offload_trigger *trigger, |
| 1618 |
enum spi_offload_trigger_type type, |
| 1619 |
u64 *args, u32 nargs) |
| 1620 |
{ |
| 1621 |
if (type != SPI_OFFLOAD_TRIGGER_DATA_READY) |
| 1622 |
return false; |
| 1623 |
|
| 1624 |
/* |
| 1625 |
* Requires 2 args: |
| 1626 |
* args[0] is the trigger event. |
| 1627 |
* args[1] is the GPIO pin number. |
| 1628 |
*/ |
| 1629 |
if (nargs != 2 || args[0] != AD4695_TRIGGER_EVENT_BUSY) |
| 1630 |
return false; |
| 1631 |
|
| 1632 |
return true; |
| 1633 |
} |
| 1634 |
|
| 1635 |
static int ad4695_offload_trigger_request(struct spi_offload_trigger *trigger, |
| 1636 |
enum spi_offload_trigger_type type, |
| 1637 |
u64 *args, u32 nargs) |
| 1638 |
{ |
| 1639 |
struct ad4695_state *st = spi_offload_trigger_get_priv(trigger); |
| 1640 |
|
| 1641 |
/* Should already be validated by match, but just in case. */ |
| 1642 |
if (nargs != 2) |
| 1643 |
return -EINVAL; |
| 1644 |
|
| 1645 |
/* DT tells us if BUSY event uses GP0 or GP3. */ |
| 1646 |
if (args[1] == AD4695_TRIGGER_PIN_GP3) |
| 1647 |
return regmap_set_bits(st->regmap, AD4695_REG_GP_MODE, |
| 1648 |
AD4695_REG_GP_MODE_BUSY_GP_SEL); |
| 1649 |
|
| 1650 |
return regmap_clear_bits(st->regmap, AD4695_REG_GP_MODE, |
| 1651 |
AD4695_REG_GP_MODE_BUSY_GP_SEL); |
| 1652 |
} |
| 1653 |
|
| 1654 |
static int |
| 1655 |
ad4695_offload_trigger_validate(struct spi_offload_trigger *trigger, |
| 1656 |
struct spi_offload_trigger_config *config) |
| 1657 |
{ |
| 1658 |
if (config->type != SPI_OFFLOAD_TRIGGER_DATA_READY) |
| 1659 |
return -EINVAL; |
| 1660 |
|
| 1661 |
return 0; |
| 1662 |
} |
| 1663 |
|
| 1664 |
/* |
| 1665 |
* NB: There are no enable/disable callbacks here due to requiring a SPI |
| 1666 |
* message to enable or disable the BUSY output on the ADC. |
| 1667 |
*/ |
| 1668 |
static const struct spi_offload_trigger_ops ad4695_offload_trigger_ops = { |
| 1669 |
.match = ad4695_offload_trigger_match, |
| 1670 |
.request = ad4695_offload_trigger_request, |
| 1671 |
.validate = ad4695_offload_trigger_validate, |
| 1672 |
}; |
| 1673 |
|
| 1674 |
static void ad4695_pwm_disable(void *pwm) |
| 1675 |
{ |
| 1676 |
pwm_disable(pwm); |
| 1677 |
} |
| 1678 |
|
| 1679 |
static int ad4695_probe_spi_offload(struct iio_dev *indio_dev, |
| 1680 |
struct ad4695_state *st) |
| 1681 |
{ |
| 1682 |
struct device *dev = &st->spi->dev; |
| 1683 |
struct spi_offload_trigger_info trigger_info = { |
| 1684 |
.fwnode = dev_fwnode(dev), |
| 1685 |
.ops = &ad4695_offload_trigger_ops, |
| 1686 |
.priv = st, |
| 1687 |
}; |
| 1688 |
struct pwm_state pwm_state; |
| 1689 |
struct dma_chan *rx_dma; |
| 1690 |
int ret, i; |
| 1691 |
|
| 1692 |
indio_dev->info = &ad4695_offload_info; |
| 1693 |
indio_dev->num_channels = st->chip_info->num_voltage_inputs + 1; |
| 1694 |
indio_dev->setup_ops = &ad4695_offload_buffer_setup_ops; |
| 1695 |
|
| 1696 |
if (!st->cnv_gpio) |
| 1697 |
return dev_err_probe(dev, -ENODEV, |
| 1698 |
"CNV GPIO is required for SPI offload\n"); |
| 1699 |
|
| 1700 |
ret = devm_spi_offload_trigger_register(dev, &trigger_info); |
| 1701 |
if (ret) |
| 1702 |
return dev_err_probe(dev, ret, |
| 1703 |
"failed to register offload trigger\n"); |
| 1704 |
|
| 1705 |
st->offload_trigger = devm_spi_offload_trigger_get(dev, st->offload, |
| 1706 |
SPI_OFFLOAD_TRIGGER_DATA_READY); |
| 1707 |
if (IS_ERR(st->offload_trigger)) |
| 1708 |
return dev_err_probe(dev, PTR_ERR(st->offload_trigger), |
| 1709 |
"failed to get offload trigger\n"); |
| 1710 |
|
| 1711 |
ret = devm_mutex_init(dev, &st->cnv_pwm_lock); |
| 1712 |
if (ret) |
| 1713 |
return ret; |
| 1714 |
|
| 1715 |
st->cnv_pwm = devm_pwm_get(dev, NULL); |
| 1716 |
if (IS_ERR(st->cnv_pwm)) |
| 1717 |
return dev_err_probe(dev, PTR_ERR(st->cnv_pwm), |
| 1718 |
"failed to get CNV PWM\n"); |
| 1719 |
|
| 1720 |
pwm_init_state(st->cnv_pwm, &pwm_state); |
| 1721 |
|
| 1722 |
/* If firmware didn't provide default rate, use 10kHz (arbitrary). */ |
| 1723 |
if (pwm_state.period == 0) |
| 1724 |
pwm_state.period = 100 * MILLI; |
| 1725 |
|
| 1726 |
pwm_state.enabled = true; |
| 1727 |
|
| 1728 |
ret = pwm_apply_might_sleep(st->cnv_pwm, &pwm_state); |
| 1729 |
if (ret) |
| 1730 |
return dev_err_probe(dev, ret, "failed to apply CNV PWM\n"); |
| 1731 |
|
| 1732 |
ret = devm_add_action_or_reset(dev, ad4695_pwm_disable, st->cnv_pwm); |
| 1733 |
if (ret) |
| 1734 |
return ret; |
| 1735 |
|
| 1736 |
rx_dma = devm_spi_offload_rx_stream_request_dma_chan(dev, st->offload); |
| 1737 |
if (IS_ERR(rx_dma)) |
| 1738 |
return dev_err_probe(dev, PTR_ERR(rx_dma), |
| 1739 |
"failed to get offload RX DMA\n"); |
| 1740 |
|
| 1741 |
for (i = 0; i < indio_dev->num_channels; i++) { |
| 1742 |
struct iio_chan_spec *chan = &st->iio_chan[i]; |
| 1743 |
struct ad4695_channel_config *cfg; |
| 1744 |
|
| 1745 |
/* |
| 1746 |
* NB: When using offload support, all channels need to have the |
| 1747 |
* same bits_per_word because they all use the same SPI message |
| 1748 |
* for reading one sample. In order to prevent breaking |
| 1749 |
* userspace in the future when oversampling support is added, |
| 1750 |
* all channels are set read 19 bits with a shift of 3 to mask |
| 1751 |
* out the extra bits even though we currently only support 16 |
| 1752 |
* bit samples (oversampling ratio == 1). |
| 1753 |
*/ |
| 1754 |
chan->scan_type.shift = 3; |
| 1755 |
chan->scan_type.storagebits = 32; |
| 1756 |
/* add sample frequency for PWM CNV trigger */ |
| 1757 |
chan->info_mask_separate |= BIT(IIO_CHAN_INFO_SAMP_FREQ); |
| 1758 |
chan->info_mask_separate_available |= BIT(IIO_CHAN_INFO_SAMP_FREQ); |
| 1759 |
|
| 1760 |
/* Add the oversampling properties only for voltage channels */ |
| 1761 |
if (chan->type != IIO_VOLTAGE) |
| 1762 |
continue; |
| 1763 |
|
| 1764 |
cfg = &st->channels_cfg[i]; |
| 1765 |
|
| 1766 |
chan->info_mask_separate |= BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO); |
| 1767 |
chan->info_mask_separate_available |= |
| 1768 |
BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO); |
| 1769 |
chan->has_ext_scan_type = 1; |
| 1770 |
if (cfg->bipolar) { |
| 1771 |
chan->ext_scan_type = ad4695_scan_type_offload_s; |
| 1772 |
chan->num_ext_scan_type = |
| 1773 |
ARRAY_SIZE(ad4695_scan_type_offload_s); |
| 1774 |
} else { |
| 1775 |
chan->ext_scan_type = ad4695_scan_type_offload_u; |
| 1776 |
chan->num_ext_scan_type = |
| 1777 |
ARRAY_SIZE(ad4695_scan_type_offload_u); |
| 1778 |
} |
| 1779 |
} |
| 1780 |
|
| 1781 |
return devm_iio_dmaengine_buffer_setup_with_handle(dev, indio_dev, |
| 1782 |
rx_dma, IIO_BUFFER_DIRECTION_IN); |
| 1783 |
} |
| 1784 |
|
| 1785 |
static const struct spi_offload_config ad4695_spi_offload_config = { |
| 1786 |
.capability_flags = SPI_OFFLOAD_CAP_TRIGGER | |
| 1787 |
SPI_OFFLOAD_CAP_RX_STREAM_DMA, |
| 1788 |
}; |
| 1789 |
|
| 1790 |
static int ad4695_probe(struct spi_device *spi) |
| 1791 |
{ |
| 1792 |
struct device *dev = &spi->dev; |
| 1793 |
struct ad4695_state *st; |
| 1794 |
struct iio_dev *indio_dev; |
| 1795 |
bool use_internal_ldo_supply; |
| 1796 |
bool use_internal_ref_buffer; |
| 1797 |
int ret; |
| 1798 |
|
| 1799 |
indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); |
| 1800 |
if (!indio_dev) |
| 1801 |
return -ENOMEM; |
| 1802 |
|
| 1803 |
st = iio_priv(indio_dev); |
| 1804 |
st->spi = spi; |
| 1805 |
|
| 1806 |
st->chip_info = spi_get_device_match_data(spi); |
| 1807 |
if (!st->chip_info) |
| 1808 |
return -EINVAL; |
| 1809 |
|
| 1810 |
st->sample_freq_range[0] = 1; /* min */ |
| 1811 |
st->sample_freq_range[1] = 1; /* step */ |
| 1812 |
st->sample_freq_range[2] = st->chip_info->max_sample_rate; /* max */ |
| 1813 |
|
| 1814 |
st->regmap = devm_regmap_init(dev, &ad4695_regmap_bus, st, |
| 1815 |
&ad4695_regmap_config); |
| 1816 |
if (IS_ERR(st->regmap)) |
| 1817 |
return dev_err_probe(dev, PTR_ERR(st->regmap), |
| 1818 |
"Failed to initialize regmap\n"); |
| 1819 |
|
| 1820 |
st->regmap16 = devm_regmap_init(dev, &ad4695_regmap_bus, st, |
| 1821 |
&ad4695_regmap16_config); |
| 1822 |
if (IS_ERR(st->regmap16)) |
| 1823 |
return dev_err_probe(dev, PTR_ERR(st->regmap16), |
| 1824 |
"Failed to initialize regmap16\n"); |
| 1825 |
|
| 1826 |
st->cnv_gpio = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_LOW); |
| 1827 |
if (IS_ERR(st->cnv_gpio)) |
| 1828 |
return dev_err_probe(dev, PTR_ERR(st->cnv_gpio), |
| 1829 |
"Failed to get CNV GPIO\n"); |
| 1830 |
|
| 1831 |
ret = devm_regulator_bulk_get_enable(dev, |
| 1832 |
ARRAY_SIZE(ad4695_power_supplies), |
| 1833 |
ad4695_power_supplies); |
| 1834 |
if (ret) |
| 1835 |
return dev_err_probe(dev, ret, |
| 1836 |
"Failed to enable power supplies\n"); |
| 1837 |
|
| 1838 |
/* If LDO_IN supply is present, then we are using internal LDO. */ |
| 1839 |
ret = devm_regulator_get_enable_optional(dev, "ldo-in"); |
| 1840 |
if (ret < 0 && ret != -ENODEV) |
| 1841 |
return dev_err_probe(dev, ret, |
| 1842 |
"Failed to enable LDO_IN supply\n"); |
| 1843 |
|
| 1844 |
use_internal_ldo_supply = ret == 0; |
| 1845 |
|
| 1846 |
if (!use_internal_ldo_supply) { |
| 1847 |
/* Otherwise we need an external VDD supply. */ |
| 1848 |
ret = devm_regulator_get_enable(dev, "vdd"); |
| 1849 |
if (ret < 0) |
| 1850 |
return dev_err_probe(dev, ret, |
| 1851 |
"Failed to enable VDD supply\n"); |
| 1852 |
} |
| 1853 |
|
| 1854 |
/* If REFIN supply is given, then we are using internal buffer */ |
| 1855 |
ret = devm_regulator_get_enable_read_voltage(dev, "refin"); |
| 1856 |
if (ret < 0 && ret != -ENODEV) |
| 1857 |
return dev_err_probe(dev, ret, "Failed to get REFIN voltage\n"); |
| 1858 |
|
| 1859 |
if (ret != -ENODEV) { |
| 1860 |
st->vref_mv = ret / 1000; |
| 1861 |
use_internal_ref_buffer = true; |
| 1862 |
} else { |
| 1863 |
/* Otherwise, we need an external reference. */ |
| 1864 |
ret = devm_regulator_get_enable_read_voltage(dev, "ref"); |
| 1865 |
if (ret < 0) |
| 1866 |
return dev_err_probe(dev, ret, |
| 1867 |
"Failed to get REF voltage\n"); |
| 1868 |
|
| 1869 |
st->vref_mv = ret / 1000; |
| 1870 |
use_internal_ref_buffer = false; |
| 1871 |
} |
| 1872 |
|
| 1873 |
ret = devm_regulator_get_enable_read_voltage(dev, "com"); |
| 1874 |
if (ret < 0 && ret != -ENODEV) |
| 1875 |
return dev_err_probe(dev, ret, "Failed to get COM voltage\n"); |
| 1876 |
|
| 1877 |
st->com_mv = ret == -ENODEV ? 0 : ret / 1000; |
| 1878 |
|
| 1879 |
/* |
| 1880 |
* Reset the device using hardware reset if available or fall back to |
| 1881 |
* software reset. |
| 1882 |
*/ |
| 1883 |
|
| 1884 |
st->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); |
| 1885 |
if (IS_ERR(st->reset_gpio)) |
| 1886 |
return PTR_ERR(st->reset_gpio); |
| 1887 |
|
| 1888 |
if (st->reset_gpio) { |
| 1889 |
gpiod_set_value(st->reset_gpio, 0); |
| 1890 |
msleep(AD4695_T_WAKEUP_HW_MS); |
| 1891 |
} else { |
| 1892 |
ret = regmap_write(st->regmap, AD4695_REG_SPI_CONFIG_A, |
| 1893 |
AD4695_REG_SPI_CONFIG_A_SW_RST); |
| 1894 |
if (ret) |
| 1895 |
return ret; |
| 1896 |
|
| 1897 |
msleep(AD4695_T_WAKEUP_SW_MS); |
| 1898 |
} |
| 1899 |
|
| 1900 |
/* Needed for regmap16 to be able to work correctly. */ |
| 1901 |
ret = regmap_set_bits(st->regmap, AD4695_REG_SPI_CONFIG_A, |
| 1902 |
AD4695_REG_SPI_CONFIG_A_ADDR_DIR); |
| 1903 |
if (ret) |
| 1904 |
return ret; |
| 1905 |
|
| 1906 |
/* Disable internal LDO if it isn't needed. */ |
| 1907 |
ret = regmap_update_bits(st->regmap, AD4695_REG_SETUP, |
| 1908 |
AD4695_REG_SETUP_LDO_EN, |
| 1909 |
FIELD_PREP(AD4695_REG_SETUP_LDO_EN, |
| 1910 |
use_internal_ldo_supply ? 1 : 0)); |
| 1911 |
if (ret) |
| 1912 |
return ret; |
| 1913 |
|
| 1914 |
/* configure reference supply */ |
| 1915 |
|
| 1916 |
if (device_property_present(dev, "adi,no-ref-current-limit")) { |
| 1917 |
ret = regmap_set_bits(st->regmap, AD4695_REG_REF_CTRL, |
| 1918 |
AD4695_REG_REF_CTRL_OV_MODE); |
| 1919 |
if (ret) |
| 1920 |
return ret; |
| 1921 |
} |
| 1922 |
|
| 1923 |
if (device_property_present(dev, "adi,no-ref-high-z")) { |
| 1924 |
if (use_internal_ref_buffer) |
| 1925 |
return dev_err_probe(dev, -EINVAL, |
| 1926 |
"Cannot disable high-Z mode for internal reference buffer\n"); |
| 1927 |
|
| 1928 |
ret = regmap_clear_bits(st->regmap, AD4695_REG_REF_CTRL, |
| 1929 |
AD4695_REG_REF_CTRL_REFHIZ_EN); |
| 1930 |
if (ret) |
| 1931 |
return ret; |
| 1932 |
} |
| 1933 |
|
| 1934 |
ret = ad4695_set_ref_voltage(st, st->vref_mv); |
| 1935 |
if (ret) |
| 1936 |
return ret; |
| 1937 |
|
| 1938 |
if (use_internal_ref_buffer) { |
| 1939 |
ret = regmap_set_bits(st->regmap, AD4695_REG_REF_CTRL, |
| 1940 |
AD4695_REG_REF_CTRL_REFBUF_EN); |
| 1941 |
if (ret) |
| 1942 |
return ret; |
| 1943 |
|
| 1944 |
/* Give the capacitor some time to charge up. */ |
| 1945 |
msleep(AD4695_T_REFBUF_MS); |
| 1946 |
} |
| 1947 |
|
| 1948 |
ret = ad4695_parse_channel_cfg(st); |
| 1949 |
if (ret) |
| 1950 |
return ret; |
| 1951 |
|
| 1952 |
indio_dev->name = st->chip_info->name; |
| 1953 |
indio_dev->info = &ad4695_info; |
| 1954 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 1955 |
indio_dev->channels = st->iio_chan; |
| 1956 |
indio_dev->num_channels = st->chip_info->num_voltage_inputs + 2; |
| 1957 |
|
| 1958 |
st->offload = devm_spi_offload_get(dev, spi, &ad4695_spi_offload_config); |
| 1959 |
ret = PTR_ERR_OR_ZERO(st->offload); |
| 1960 |
if (ret && ret != -ENODEV) |
| 1961 |
return dev_err_probe(dev, ret, "failed to get SPI offload\n"); |
| 1962 |
|
| 1963 |
/* If no SPI offload, fall back to low speed usage. */ |
| 1964 |
if (ret == -ENODEV) { |
| 1965 |
/* Driver currently requires CNV pin to be connected to SPI CS */ |
| 1966 |
if (st->cnv_gpio) |
| 1967 |
return dev_err_probe(dev, -EINVAL, |
| 1968 |
"CNV GPIO is not supported\n"); |
| 1969 |
|
| 1970 |
indio_dev->num_channels = st->chip_info->num_voltage_inputs + 2; |
| 1971 |
|
| 1972 |
ret = devm_iio_triggered_buffer_setup(dev, indio_dev, |
| 1973 |
iio_pollfunc_store_time, |
| 1974 |
ad4695_trigger_handler, |
| 1975 |
&ad4695_buffer_setup_ops); |
| 1976 |
if (ret) |
| 1977 |
return ret; |
| 1978 |
} else { |
| 1979 |
ret = ad4695_probe_spi_offload(indio_dev, st); |
| 1980 |
if (ret) |
| 1981 |
return ret; |
| 1982 |
} |
| 1983 |
|
| 1984 |
return devm_iio_device_register(dev, indio_dev); |
| 1985 |
} |
| 1986 |
|
| 1987 |
static const struct spi_device_id ad4695_spi_id_table[] = { |
| 1988 |
{ .name = "ad4695", .driver_data = (kernel_ulong_t)&ad4695_chip_info }, |
| 1989 |
{ .name = "ad4696", .driver_data = (kernel_ulong_t)&ad4696_chip_info }, |
| 1990 |
{ .name = "ad4697", .driver_data = (kernel_ulong_t)&ad4697_chip_info }, |
| 1991 |
{ .name = "ad4698", .driver_data = (kernel_ulong_t)&ad4698_chip_info }, |
| 1992 |
{ } |
| 1993 |
}; |
| 1994 |
MODULE_DEVICE_TABLE(spi, ad4695_spi_id_table); |
| 1995 |
|
| 1996 |
static const struct of_device_id ad4695_of_match_table[] = { |
| 1997 |
{ .compatible = "adi,ad4695", .data = &ad4695_chip_info, }, |
| 1998 |
{ .compatible = "adi,ad4696", .data = &ad4696_chip_info, }, |
| 1999 |
{ .compatible = "adi,ad4697", .data = &ad4697_chip_info, }, |
| 2000 |
{ .compatible = "adi,ad4698", .data = &ad4698_chip_info, }, |
| 2001 |
{ } |
| 2002 |
}; |
| 2003 |
MODULE_DEVICE_TABLE(of, ad4695_of_match_table); |
| 2004 |
|
| 2005 |
static struct spi_driver ad4695_driver = { |
| 2006 |
.driver = { |
| 2007 |
.name = "ad4695", |
| 2008 |
.of_match_table = ad4695_of_match_table, |
| 2009 |
}, |
| 2010 |
.probe = ad4695_probe, |
| 2011 |
.id_table = ad4695_spi_id_table, |
| 2012 |
}; |
| 2013 |
module_spi_driver(ad4695_driver); |
| 2014 |
|
| 2015 |
MODULE_AUTHOR("Ramona Gradinariu <[email protected]>"); |
| 2016 |
MODULE_AUTHOR("David Lechner <[email protected]>"); |
| 2017 |
MODULE_DESCRIPTION("Analog Devices AD4695 ADC driver"); |
| 2018 |
MODULE_LICENSE("GPL"); |
| 2019 |
MODULE_IMPORT_NS("IIO_DMAENGINE_BUFFER"); |