1*a44ef7c4SIbrahim Tilki // SPDX-License-Identifier: GPL-2.0-only 2*a44ef7c4SIbrahim Tilki /* 3*a44ef7c4SIbrahim Tilki * MAX11410 SPI ADC driver 4*a44ef7c4SIbrahim Tilki * 5*a44ef7c4SIbrahim Tilki * Copyright 2022 Analog Devices Inc. 6*a44ef7c4SIbrahim Tilki */ 7*a44ef7c4SIbrahim Tilki #include <asm-generic/unaligned.h> 8*a44ef7c4SIbrahim Tilki #include <linux/bitfield.h> 9*a44ef7c4SIbrahim Tilki #include <linux/delay.h> 10*a44ef7c4SIbrahim Tilki #include <linux/device.h> 11*a44ef7c4SIbrahim Tilki #include <linux/err.h> 12*a44ef7c4SIbrahim Tilki #include <linux/interrupt.h> 13*a44ef7c4SIbrahim Tilki #include <linux/kernel.h> 14*a44ef7c4SIbrahim Tilki #include <linux/module.h> 15*a44ef7c4SIbrahim Tilki #include <linux/regmap.h> 16*a44ef7c4SIbrahim Tilki #include <linux/regulator/consumer.h> 17*a44ef7c4SIbrahim Tilki #include <linux/spi/spi.h> 18*a44ef7c4SIbrahim Tilki 19*a44ef7c4SIbrahim Tilki #include <linux/iio/buffer.h> 20*a44ef7c4SIbrahim Tilki #include <linux/iio/sysfs.h> 21*a44ef7c4SIbrahim Tilki #include <linux/iio/trigger.h> 22*a44ef7c4SIbrahim Tilki #include <linux/iio/trigger_consumer.h> 23*a44ef7c4SIbrahim Tilki #include <linux/iio/triggered_buffer.h> 24*a44ef7c4SIbrahim Tilki 25*a44ef7c4SIbrahim Tilki #define MAX11410_REG_CONV_START 0x01 26*a44ef7c4SIbrahim Tilki #define MAX11410_CONV_TYPE_SINGLE 0x00 27*a44ef7c4SIbrahim Tilki #define MAX11410_CONV_TYPE_CONTINUOUS 0x01 28*a44ef7c4SIbrahim Tilki #define MAX11410_REG_CAL_START 0x03 29*a44ef7c4SIbrahim Tilki #define MAX11410_CAL_START_SELF 0x00 30*a44ef7c4SIbrahim Tilki #define MAX11410_CAL_START_PGA 0x01 31*a44ef7c4SIbrahim Tilki #define MAX11410_REG_GPIO_CTRL(ch) ((ch) ? 0x05 : 0x04) 32*a44ef7c4SIbrahim Tilki #define MAX11410_GPIO_INTRB 0xC1 33*a44ef7c4SIbrahim Tilki #define MAX11410_REG_FILTER 0x08 34*a44ef7c4SIbrahim Tilki #define MAX11410_FILTER_RATE_MASK GENMASK(3, 0) 35*a44ef7c4SIbrahim Tilki #define MAX11410_FILTER_RATE_MAX 0x0F 36*a44ef7c4SIbrahim Tilki #define MAX11410_FILTER_LINEF_MASK GENMASK(5, 4) 37*a44ef7c4SIbrahim Tilki #define MAX11410_FILTER_50HZ BIT(5) 38*a44ef7c4SIbrahim Tilki #define MAX11410_FILTER_60HZ BIT(4) 39*a44ef7c4SIbrahim Tilki #define MAX11410_REG_CTRL 0x09 40*a44ef7c4SIbrahim Tilki #define MAX11410_CTRL_REFSEL_MASK GENMASK(2, 0) 41*a44ef7c4SIbrahim Tilki #define MAX11410_CTRL_VREFN_BUF_BIT BIT(3) 42*a44ef7c4SIbrahim Tilki #define MAX11410_CTRL_VREFP_BUF_BIT BIT(4) 43*a44ef7c4SIbrahim Tilki #define MAX11410_CTRL_FORMAT_BIT BIT(5) 44*a44ef7c4SIbrahim Tilki #define MAX11410_CTRL_UNIPOLAR_BIT BIT(6) 45*a44ef7c4SIbrahim Tilki #define MAX11410_REG_MUX_CTRL0 0x0B 46*a44ef7c4SIbrahim Tilki #define MAX11410_REG_PGA 0x0E 47*a44ef7c4SIbrahim Tilki #define MAX11410_PGA_GAIN_MASK GENMASK(2, 0) 48*a44ef7c4SIbrahim Tilki #define MAX11410_PGA_SIG_PATH_MASK GENMASK(5, 4) 49*a44ef7c4SIbrahim Tilki #define MAX11410_PGA_SIG_PATH_BUFFERED 0x00 50*a44ef7c4SIbrahim Tilki #define MAX11410_PGA_SIG_PATH_BYPASS 0x01 51*a44ef7c4SIbrahim Tilki #define MAX11410_PGA_SIG_PATH_PGA 0x02 52*a44ef7c4SIbrahim Tilki #define MAX11410_REG_DATA0 0x30 53*a44ef7c4SIbrahim Tilki #define MAX11410_REG_STATUS 0x38 54*a44ef7c4SIbrahim Tilki #define MAX11410_STATUS_CONV_READY_BIT BIT(0) 55*a44ef7c4SIbrahim Tilki #define MAX11410_STATUS_CAL_READY_BIT BIT(2) 56*a44ef7c4SIbrahim Tilki 57*a44ef7c4SIbrahim Tilki #define MAX11410_REFSEL_AVDD_AGND 0x03 58*a44ef7c4SIbrahim Tilki #define MAX11410_REFSEL_MAX 0x06 59*a44ef7c4SIbrahim Tilki #define MAX11410_SIG_PATH_MAX 0x02 60*a44ef7c4SIbrahim Tilki #define MAX11410_CHANNEL_INDEX_MAX 0x0A 61*a44ef7c4SIbrahim Tilki #define MAX11410_AINP_AVDD 0x0A 62*a44ef7c4SIbrahim Tilki #define MAX11410_AINN_GND 0x0A 63*a44ef7c4SIbrahim Tilki 64*a44ef7c4SIbrahim Tilki #define MAX11410_CONVERSION_TIMEOUT_MS 2000 65*a44ef7c4SIbrahim Tilki #define MAX11410_CALIB_TIMEOUT_MS 2000 66*a44ef7c4SIbrahim Tilki 67*a44ef7c4SIbrahim Tilki #define MAX11410_SCALE_AVAIL_SIZE 8 68*a44ef7c4SIbrahim Tilki 69*a44ef7c4SIbrahim Tilki enum max11410_filter { 70*a44ef7c4SIbrahim Tilki MAX11410_FILTER_FIR5060, 71*a44ef7c4SIbrahim Tilki MAX11410_FILTER_FIR50, 72*a44ef7c4SIbrahim Tilki MAX11410_FILTER_FIR60, 73*a44ef7c4SIbrahim Tilki MAX11410_FILTER_SINC4, 74*a44ef7c4SIbrahim Tilki }; 75*a44ef7c4SIbrahim Tilki 76*a44ef7c4SIbrahim Tilki static const u8 max11410_sampling_len[] = { 77*a44ef7c4SIbrahim Tilki [MAX11410_FILTER_FIR5060] = 5, 78*a44ef7c4SIbrahim Tilki [MAX11410_FILTER_FIR50] = 6, 79*a44ef7c4SIbrahim Tilki [MAX11410_FILTER_FIR60] = 6, 80*a44ef7c4SIbrahim Tilki [MAX11410_FILTER_SINC4] = 10, 81*a44ef7c4SIbrahim Tilki }; 82*a44ef7c4SIbrahim Tilki 83*a44ef7c4SIbrahim Tilki static const int max11410_sampling_rates[4][10][2] = { 84*a44ef7c4SIbrahim Tilki [MAX11410_FILTER_FIR5060] = { 85*a44ef7c4SIbrahim Tilki { 1, 100000 }, 86*a44ef7c4SIbrahim Tilki { 2, 100000 }, 87*a44ef7c4SIbrahim Tilki { 4, 200000 }, 88*a44ef7c4SIbrahim Tilki { 8, 400000 }, 89*a44ef7c4SIbrahim Tilki { 16, 800000 } 90*a44ef7c4SIbrahim Tilki }, 91*a44ef7c4SIbrahim Tilki [MAX11410_FILTER_FIR50] = { 92*a44ef7c4SIbrahim Tilki { 1, 300000 }, 93*a44ef7c4SIbrahim Tilki { 2, 700000 }, 94*a44ef7c4SIbrahim Tilki { 5, 300000 }, 95*a44ef7c4SIbrahim Tilki { 10, 700000 }, 96*a44ef7c4SIbrahim Tilki { 21, 300000 }, 97*a44ef7c4SIbrahim Tilki { 40 } 98*a44ef7c4SIbrahim Tilki }, 99*a44ef7c4SIbrahim Tilki [MAX11410_FILTER_FIR60] = { 100*a44ef7c4SIbrahim Tilki { 1, 300000 }, 101*a44ef7c4SIbrahim Tilki { 2, 700000 }, 102*a44ef7c4SIbrahim Tilki { 5, 300000 }, 103*a44ef7c4SIbrahim Tilki { 10, 700000 }, 104*a44ef7c4SIbrahim Tilki { 21, 300000 }, 105*a44ef7c4SIbrahim Tilki { 40 } 106*a44ef7c4SIbrahim Tilki }, 107*a44ef7c4SIbrahim Tilki [MAX11410_FILTER_SINC4] = { 108*a44ef7c4SIbrahim Tilki { 4 }, 109*a44ef7c4SIbrahim Tilki { 10 }, 110*a44ef7c4SIbrahim Tilki { 20 }, 111*a44ef7c4SIbrahim Tilki { 40 }, 112*a44ef7c4SIbrahim Tilki { 60 }, 113*a44ef7c4SIbrahim Tilki { 120 }, 114*a44ef7c4SIbrahim Tilki { 240 }, 115*a44ef7c4SIbrahim Tilki { 480 }, 116*a44ef7c4SIbrahim Tilki { 960 }, 117*a44ef7c4SIbrahim Tilki { 1920 } 118*a44ef7c4SIbrahim Tilki } 119*a44ef7c4SIbrahim Tilki }; 120*a44ef7c4SIbrahim Tilki 121*a44ef7c4SIbrahim Tilki struct max11410_channel_config { 122*a44ef7c4SIbrahim Tilki u32 settling_time_us; 123*a44ef7c4SIbrahim Tilki u32 *scale_avail; 124*a44ef7c4SIbrahim Tilki u8 refsel; 125*a44ef7c4SIbrahim Tilki u8 sig_path; 126*a44ef7c4SIbrahim Tilki u8 gain; 127*a44ef7c4SIbrahim Tilki bool bipolar; 128*a44ef7c4SIbrahim Tilki bool buffered_vrefp; 129*a44ef7c4SIbrahim Tilki bool buffered_vrefn; 130*a44ef7c4SIbrahim Tilki }; 131*a44ef7c4SIbrahim Tilki 132*a44ef7c4SIbrahim Tilki struct max11410_state { 133*a44ef7c4SIbrahim Tilki struct spi_device *spi_dev; 134*a44ef7c4SIbrahim Tilki struct iio_trigger *trig; 135*a44ef7c4SIbrahim Tilki struct completion completion; 136*a44ef7c4SIbrahim Tilki struct mutex lock; /* Prevent changing channel config during sampling */ 137*a44ef7c4SIbrahim Tilki struct regmap *regmap; 138*a44ef7c4SIbrahim Tilki struct regulator *avdd; 139*a44ef7c4SIbrahim Tilki struct regulator *vrefp[3]; 140*a44ef7c4SIbrahim Tilki struct regulator *vrefn[3]; 141*a44ef7c4SIbrahim Tilki struct max11410_channel_config *channels; 142*a44ef7c4SIbrahim Tilki int irq; 143*a44ef7c4SIbrahim Tilki struct { 144*a44ef7c4SIbrahim Tilki u32 data __aligned(IIO_DMA_MINALIGN); 145*a44ef7c4SIbrahim Tilki s64 ts __aligned(8); 146*a44ef7c4SIbrahim Tilki } scan; 147*a44ef7c4SIbrahim Tilki }; 148*a44ef7c4SIbrahim Tilki 149*a44ef7c4SIbrahim Tilki static const struct iio_chan_spec chanspec_template = { 150*a44ef7c4SIbrahim Tilki .type = IIO_VOLTAGE, 151*a44ef7c4SIbrahim Tilki .indexed = 1, 152*a44ef7c4SIbrahim Tilki .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 153*a44ef7c4SIbrahim Tilki BIT(IIO_CHAN_INFO_SCALE) | 154*a44ef7c4SIbrahim Tilki BIT(IIO_CHAN_INFO_OFFSET), 155*a44ef7c4SIbrahim Tilki .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), 156*a44ef7c4SIbrahim Tilki .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), 157*a44ef7c4SIbrahim Tilki .scan_type = { 158*a44ef7c4SIbrahim Tilki .sign = 's', 159*a44ef7c4SIbrahim Tilki .realbits = 24, 160*a44ef7c4SIbrahim Tilki .storagebits = 32, 161*a44ef7c4SIbrahim Tilki .endianness = IIO_LE, 162*a44ef7c4SIbrahim Tilki }, 163*a44ef7c4SIbrahim Tilki }; 164*a44ef7c4SIbrahim Tilki 165*a44ef7c4SIbrahim Tilki static unsigned int max11410_reg_size(unsigned int reg) 166*a44ef7c4SIbrahim Tilki { 167*a44ef7c4SIbrahim Tilki /* Registers from 0x00 to 0x10 are 1 byte, the rest are 3 bytes long. */ 168*a44ef7c4SIbrahim Tilki return reg <= 0x10 ? 1 : 3; 169*a44ef7c4SIbrahim Tilki } 170*a44ef7c4SIbrahim Tilki 171*a44ef7c4SIbrahim Tilki static int max11410_write_reg(struct max11410_state *st, unsigned int reg, 172*a44ef7c4SIbrahim Tilki unsigned int val) 173*a44ef7c4SIbrahim Tilki { 174*a44ef7c4SIbrahim Tilki /* This driver only needs to write 8-bit registers */ 175*a44ef7c4SIbrahim Tilki if (max11410_reg_size(reg) != 1) 176*a44ef7c4SIbrahim Tilki return -EINVAL; 177*a44ef7c4SIbrahim Tilki 178*a44ef7c4SIbrahim Tilki return regmap_write(st->regmap, reg, val); 179*a44ef7c4SIbrahim Tilki } 180*a44ef7c4SIbrahim Tilki 181*a44ef7c4SIbrahim Tilki static int max11410_read_reg(struct max11410_state *st, unsigned int reg, 182*a44ef7c4SIbrahim Tilki int *val) 183*a44ef7c4SIbrahim Tilki { 184*a44ef7c4SIbrahim Tilki int ret; 185*a44ef7c4SIbrahim Tilki 186*a44ef7c4SIbrahim Tilki if (max11410_reg_size(reg) == 3) { 187*a44ef7c4SIbrahim Tilki ret = regmap_bulk_read(st->regmap, reg, &st->scan.data, 3); 188*a44ef7c4SIbrahim Tilki if (ret) 189*a44ef7c4SIbrahim Tilki return ret; 190*a44ef7c4SIbrahim Tilki 191*a44ef7c4SIbrahim Tilki *val = get_unaligned_be24(&st->scan.data); 192*a44ef7c4SIbrahim Tilki return 0; 193*a44ef7c4SIbrahim Tilki } 194*a44ef7c4SIbrahim Tilki 195*a44ef7c4SIbrahim Tilki return regmap_read(st->regmap, reg, val); 196*a44ef7c4SIbrahim Tilki } 197*a44ef7c4SIbrahim Tilki 198*a44ef7c4SIbrahim Tilki static struct regulator *max11410_get_vrefp(struct max11410_state *st, 199*a44ef7c4SIbrahim Tilki u8 refsel) 200*a44ef7c4SIbrahim Tilki { 201*a44ef7c4SIbrahim Tilki refsel = refsel % 4; 202*a44ef7c4SIbrahim Tilki if (refsel == 3) 203*a44ef7c4SIbrahim Tilki return st->avdd; 204*a44ef7c4SIbrahim Tilki 205*a44ef7c4SIbrahim Tilki return st->vrefp[refsel]; 206*a44ef7c4SIbrahim Tilki } 207*a44ef7c4SIbrahim Tilki 208*a44ef7c4SIbrahim Tilki static struct regulator *max11410_get_vrefn(struct max11410_state *st, 209*a44ef7c4SIbrahim Tilki u8 refsel) 210*a44ef7c4SIbrahim Tilki { 211*a44ef7c4SIbrahim Tilki if (refsel > 2) 212*a44ef7c4SIbrahim Tilki return NULL; 213*a44ef7c4SIbrahim Tilki 214*a44ef7c4SIbrahim Tilki return st->vrefn[refsel]; 215*a44ef7c4SIbrahim Tilki } 216*a44ef7c4SIbrahim Tilki 217*a44ef7c4SIbrahim Tilki static const struct regmap_config regmap_config = { 218*a44ef7c4SIbrahim Tilki .reg_bits = 8, 219*a44ef7c4SIbrahim Tilki .val_bits = 8, 220*a44ef7c4SIbrahim Tilki .max_register = 0x39, 221*a44ef7c4SIbrahim Tilki }; 222*a44ef7c4SIbrahim Tilki 223*a44ef7c4SIbrahim Tilki static ssize_t max11410_notch_en_show(struct device *dev, 224*a44ef7c4SIbrahim Tilki struct device_attribute *devattr, 225*a44ef7c4SIbrahim Tilki char *buf) 226*a44ef7c4SIbrahim Tilki { 227*a44ef7c4SIbrahim Tilki struct iio_dev *indio_dev = dev_get_drvdata(dev); 228*a44ef7c4SIbrahim Tilki struct max11410_state *state = iio_priv(indio_dev); 229*a44ef7c4SIbrahim Tilki struct iio_dev_attr *iio_attr = to_iio_dev_attr(devattr); 230*a44ef7c4SIbrahim Tilki unsigned int val; 231*a44ef7c4SIbrahim Tilki int ret; 232*a44ef7c4SIbrahim Tilki 233*a44ef7c4SIbrahim Tilki ret = max11410_read_reg(state, MAX11410_REG_FILTER, &val); 234*a44ef7c4SIbrahim Tilki if (ret) 235*a44ef7c4SIbrahim Tilki return ret; 236*a44ef7c4SIbrahim Tilki 237*a44ef7c4SIbrahim Tilki switch (iio_attr->address) { 238*a44ef7c4SIbrahim Tilki case 0: 239*a44ef7c4SIbrahim Tilki val = !FIELD_GET(MAX11410_FILTER_50HZ, val); 240*a44ef7c4SIbrahim Tilki break; 241*a44ef7c4SIbrahim Tilki case 1: 242*a44ef7c4SIbrahim Tilki val = !FIELD_GET(MAX11410_FILTER_60HZ, val); 243*a44ef7c4SIbrahim Tilki break; 244*a44ef7c4SIbrahim Tilki case 2: 245*a44ef7c4SIbrahim Tilki val = FIELD_GET(MAX11410_FILTER_LINEF_MASK, val) == 3; 246*a44ef7c4SIbrahim Tilki break; 247*a44ef7c4SIbrahim Tilki default: 248*a44ef7c4SIbrahim Tilki return -EINVAL; 249*a44ef7c4SIbrahim Tilki } 250*a44ef7c4SIbrahim Tilki 251*a44ef7c4SIbrahim Tilki return sysfs_emit(buf, "%d\n", val); 252*a44ef7c4SIbrahim Tilki } 253*a44ef7c4SIbrahim Tilki 254*a44ef7c4SIbrahim Tilki static ssize_t max11410_notch_en_store(struct device *dev, 255*a44ef7c4SIbrahim Tilki struct device_attribute *devattr, 256*a44ef7c4SIbrahim Tilki const char *buf, size_t count) 257*a44ef7c4SIbrahim Tilki { 258*a44ef7c4SIbrahim Tilki struct iio_dev_attr *iio_attr = to_iio_dev_attr(devattr); 259*a44ef7c4SIbrahim Tilki struct iio_dev *indio_dev = dev_get_drvdata(dev); 260*a44ef7c4SIbrahim Tilki struct max11410_state *state = iio_priv(indio_dev); 261*a44ef7c4SIbrahim Tilki unsigned int filter_bits; 262*a44ef7c4SIbrahim Tilki bool enable; 263*a44ef7c4SIbrahim Tilki int ret; 264*a44ef7c4SIbrahim Tilki 265*a44ef7c4SIbrahim Tilki ret = kstrtobool(buf, &enable); 266*a44ef7c4SIbrahim Tilki if (ret) 267*a44ef7c4SIbrahim Tilki return ret; 268*a44ef7c4SIbrahim Tilki 269*a44ef7c4SIbrahim Tilki switch (iio_attr->address) { 270*a44ef7c4SIbrahim Tilki case 0: 271*a44ef7c4SIbrahim Tilki filter_bits = MAX11410_FILTER_50HZ; 272*a44ef7c4SIbrahim Tilki break; 273*a44ef7c4SIbrahim Tilki case 1: 274*a44ef7c4SIbrahim Tilki filter_bits = MAX11410_FILTER_60HZ; 275*a44ef7c4SIbrahim Tilki break; 276*a44ef7c4SIbrahim Tilki case 2: 277*a44ef7c4SIbrahim Tilki default: 278*a44ef7c4SIbrahim Tilki filter_bits = MAX11410_FILTER_50HZ | MAX11410_FILTER_60HZ; 279*a44ef7c4SIbrahim Tilki enable = !enable; 280*a44ef7c4SIbrahim Tilki break; 281*a44ef7c4SIbrahim Tilki } 282*a44ef7c4SIbrahim Tilki 283*a44ef7c4SIbrahim Tilki if (enable) 284*a44ef7c4SIbrahim Tilki ret = regmap_clear_bits(state->regmap, MAX11410_REG_FILTER, 285*a44ef7c4SIbrahim Tilki filter_bits); 286*a44ef7c4SIbrahim Tilki else 287*a44ef7c4SIbrahim Tilki ret = regmap_set_bits(state->regmap, MAX11410_REG_FILTER, 288*a44ef7c4SIbrahim Tilki filter_bits); 289*a44ef7c4SIbrahim Tilki 290*a44ef7c4SIbrahim Tilki if (ret) 291*a44ef7c4SIbrahim Tilki return ret; 292*a44ef7c4SIbrahim Tilki 293*a44ef7c4SIbrahim Tilki return count; 294*a44ef7c4SIbrahim Tilki } 295*a44ef7c4SIbrahim Tilki 296*a44ef7c4SIbrahim Tilki static ssize_t in_voltage_filter2_notch_center_show(struct device *dev, 297*a44ef7c4SIbrahim Tilki struct device_attribute *devattr, 298*a44ef7c4SIbrahim Tilki char *buf) 299*a44ef7c4SIbrahim Tilki { 300*a44ef7c4SIbrahim Tilki struct iio_dev *indio_dev = dev_get_drvdata(dev); 301*a44ef7c4SIbrahim Tilki struct max11410_state *state = iio_priv(indio_dev); 302*a44ef7c4SIbrahim Tilki int ret, reg, rate, filter; 303*a44ef7c4SIbrahim Tilki 304*a44ef7c4SIbrahim Tilki ret = regmap_read(state->regmap, MAX11410_REG_FILTER, ®); 305*a44ef7c4SIbrahim Tilki if (ret) 306*a44ef7c4SIbrahim Tilki return ret; 307*a44ef7c4SIbrahim Tilki 308*a44ef7c4SIbrahim Tilki rate = FIELD_GET(MAX11410_FILTER_RATE_MASK, reg); 309*a44ef7c4SIbrahim Tilki rate = clamp_val(rate, 0, 310*a44ef7c4SIbrahim Tilki max11410_sampling_len[MAX11410_FILTER_SINC4] - 1); 311*a44ef7c4SIbrahim Tilki filter = max11410_sampling_rates[MAX11410_FILTER_SINC4][rate][0]; 312*a44ef7c4SIbrahim Tilki 313*a44ef7c4SIbrahim Tilki return sysfs_emit(buf, "%d\n", filter); 314*a44ef7c4SIbrahim Tilki } 315*a44ef7c4SIbrahim Tilki 316*a44ef7c4SIbrahim Tilki static IIO_CONST_ATTR(in_voltage_filter0_notch_center, "50"); 317*a44ef7c4SIbrahim Tilki static IIO_CONST_ATTR(in_voltage_filter1_notch_center, "60"); 318*a44ef7c4SIbrahim Tilki static IIO_DEVICE_ATTR_RO(in_voltage_filter2_notch_center, 2); 319*a44ef7c4SIbrahim Tilki 320*a44ef7c4SIbrahim Tilki static IIO_DEVICE_ATTR(in_voltage_filter0_notch_en, 0644, 321*a44ef7c4SIbrahim Tilki max11410_notch_en_show, max11410_notch_en_store, 0); 322*a44ef7c4SIbrahim Tilki static IIO_DEVICE_ATTR(in_voltage_filter1_notch_en, 0644, 323*a44ef7c4SIbrahim Tilki max11410_notch_en_show, max11410_notch_en_store, 1); 324*a44ef7c4SIbrahim Tilki static IIO_DEVICE_ATTR(in_voltage_filter2_notch_en, 0644, 325*a44ef7c4SIbrahim Tilki max11410_notch_en_show, max11410_notch_en_store, 2); 326*a44ef7c4SIbrahim Tilki 327*a44ef7c4SIbrahim Tilki static struct attribute *max11410_attributes[] = { 328*a44ef7c4SIbrahim Tilki &iio_const_attr_in_voltage_filter0_notch_center.dev_attr.attr, 329*a44ef7c4SIbrahim Tilki &iio_const_attr_in_voltage_filter1_notch_center.dev_attr.attr, 330*a44ef7c4SIbrahim Tilki &iio_dev_attr_in_voltage_filter2_notch_center.dev_attr.attr, 331*a44ef7c4SIbrahim Tilki &iio_dev_attr_in_voltage_filter0_notch_en.dev_attr.attr, 332*a44ef7c4SIbrahim Tilki &iio_dev_attr_in_voltage_filter1_notch_en.dev_attr.attr, 333*a44ef7c4SIbrahim Tilki &iio_dev_attr_in_voltage_filter2_notch_en.dev_attr.attr, 334*a44ef7c4SIbrahim Tilki NULL 335*a44ef7c4SIbrahim Tilki }; 336*a44ef7c4SIbrahim Tilki 337*a44ef7c4SIbrahim Tilki static const struct attribute_group max11410_attribute_group = { 338*a44ef7c4SIbrahim Tilki .attrs = max11410_attributes, 339*a44ef7c4SIbrahim Tilki }; 340*a44ef7c4SIbrahim Tilki 341*a44ef7c4SIbrahim Tilki static int max11410_set_input_mux(struct max11410_state *st, u8 ainp, u8 ainn) 342*a44ef7c4SIbrahim Tilki { 343*a44ef7c4SIbrahim Tilki if (ainp > MAX11410_CHANNEL_INDEX_MAX || 344*a44ef7c4SIbrahim Tilki ainn > MAX11410_CHANNEL_INDEX_MAX) 345*a44ef7c4SIbrahim Tilki return -EINVAL; 346*a44ef7c4SIbrahim Tilki 347*a44ef7c4SIbrahim Tilki return max11410_write_reg(st, MAX11410_REG_MUX_CTRL0, 348*a44ef7c4SIbrahim Tilki (ainp << 4) | ainn); 349*a44ef7c4SIbrahim Tilki } 350*a44ef7c4SIbrahim Tilki 351*a44ef7c4SIbrahim Tilki static int max11410_configure_channel(struct max11410_state *st, 352*a44ef7c4SIbrahim Tilki struct iio_chan_spec const *chan) 353*a44ef7c4SIbrahim Tilki { 354*a44ef7c4SIbrahim Tilki struct max11410_channel_config cfg = st->channels[chan->address]; 355*a44ef7c4SIbrahim Tilki unsigned int regval; 356*a44ef7c4SIbrahim Tilki int ret; 357*a44ef7c4SIbrahim Tilki 358*a44ef7c4SIbrahim Tilki if (chan->differential) 359*a44ef7c4SIbrahim Tilki ret = max11410_set_input_mux(st, chan->channel, chan->channel2); 360*a44ef7c4SIbrahim Tilki else 361*a44ef7c4SIbrahim Tilki ret = max11410_set_input_mux(st, chan->channel, 362*a44ef7c4SIbrahim Tilki MAX11410_AINN_GND); 363*a44ef7c4SIbrahim Tilki 364*a44ef7c4SIbrahim Tilki if (ret) 365*a44ef7c4SIbrahim Tilki return ret; 366*a44ef7c4SIbrahim Tilki 367*a44ef7c4SIbrahim Tilki regval = FIELD_PREP(MAX11410_CTRL_VREFP_BUF_BIT, cfg.buffered_vrefp) | 368*a44ef7c4SIbrahim Tilki FIELD_PREP(MAX11410_CTRL_VREFN_BUF_BIT, cfg.buffered_vrefn) | 369*a44ef7c4SIbrahim Tilki FIELD_PREP(MAX11410_CTRL_REFSEL_MASK, cfg.refsel) | 370*a44ef7c4SIbrahim Tilki FIELD_PREP(MAX11410_CTRL_UNIPOLAR_BIT, cfg.bipolar ? 0 : 1); 371*a44ef7c4SIbrahim Tilki ret = regmap_update_bits(st->regmap, MAX11410_REG_CTRL, 372*a44ef7c4SIbrahim Tilki MAX11410_CTRL_REFSEL_MASK | 373*a44ef7c4SIbrahim Tilki MAX11410_CTRL_VREFN_BUF_BIT | 374*a44ef7c4SIbrahim Tilki MAX11410_CTRL_VREFN_BUF_BIT | 375*a44ef7c4SIbrahim Tilki MAX11410_CTRL_UNIPOLAR_BIT, regval); 376*a44ef7c4SIbrahim Tilki if (ret) 377*a44ef7c4SIbrahim Tilki return ret; 378*a44ef7c4SIbrahim Tilki 379*a44ef7c4SIbrahim Tilki regval = FIELD_PREP(MAX11410_PGA_SIG_PATH_MASK, cfg.sig_path) | 380*a44ef7c4SIbrahim Tilki FIELD_PREP(MAX11410_PGA_GAIN_MASK, cfg.gain); 381*a44ef7c4SIbrahim Tilki ret = regmap_write(st->regmap, MAX11410_REG_PGA, regval); 382*a44ef7c4SIbrahim Tilki if (ret) 383*a44ef7c4SIbrahim Tilki return ret; 384*a44ef7c4SIbrahim Tilki 385*a44ef7c4SIbrahim Tilki if (cfg.settling_time_us) 386*a44ef7c4SIbrahim Tilki fsleep(cfg.settling_time_us); 387*a44ef7c4SIbrahim Tilki 388*a44ef7c4SIbrahim Tilki return 0; 389*a44ef7c4SIbrahim Tilki } 390*a44ef7c4SIbrahim Tilki 391*a44ef7c4SIbrahim Tilki static int max11410_sample(struct max11410_state *st, int *sample_raw, 392*a44ef7c4SIbrahim Tilki struct iio_chan_spec const *chan) 393*a44ef7c4SIbrahim Tilki { 394*a44ef7c4SIbrahim Tilki int val, ret; 395*a44ef7c4SIbrahim Tilki 396*a44ef7c4SIbrahim Tilki ret = max11410_configure_channel(st, chan); 397*a44ef7c4SIbrahim Tilki if (ret) 398*a44ef7c4SIbrahim Tilki return ret; 399*a44ef7c4SIbrahim Tilki 400*a44ef7c4SIbrahim Tilki if (st->irq > 0) 401*a44ef7c4SIbrahim Tilki reinit_completion(&st->completion); 402*a44ef7c4SIbrahim Tilki 403*a44ef7c4SIbrahim Tilki /* Start Conversion */ 404*a44ef7c4SIbrahim Tilki ret = max11410_write_reg(st, MAX11410_REG_CONV_START, 405*a44ef7c4SIbrahim Tilki MAX11410_CONV_TYPE_SINGLE); 406*a44ef7c4SIbrahim Tilki if (ret) 407*a44ef7c4SIbrahim Tilki return ret; 408*a44ef7c4SIbrahim Tilki 409*a44ef7c4SIbrahim Tilki if (st->irq > 0) { 410*a44ef7c4SIbrahim Tilki /* Wait for an interrupt. */ 411*a44ef7c4SIbrahim Tilki ret = wait_for_completion_timeout(&st->completion, 412*a44ef7c4SIbrahim Tilki msecs_to_jiffies(MAX11410_CONVERSION_TIMEOUT_MS)); 413*a44ef7c4SIbrahim Tilki if (!ret) 414*a44ef7c4SIbrahim Tilki return -ETIMEDOUT; 415*a44ef7c4SIbrahim Tilki } else { 416*a44ef7c4SIbrahim Tilki /* Wait for status register Conversion Ready flag */ 417*a44ef7c4SIbrahim Tilki ret = read_poll_timeout(max11410_read_reg, ret, 418*a44ef7c4SIbrahim Tilki ret || (val & MAX11410_STATUS_CONV_READY_BIT), 419*a44ef7c4SIbrahim Tilki 5000, MAX11410_CONVERSION_TIMEOUT_MS * 1000, 420*a44ef7c4SIbrahim Tilki true, st, MAX11410_REG_STATUS, &val); 421*a44ef7c4SIbrahim Tilki if (ret) 422*a44ef7c4SIbrahim Tilki return ret; 423*a44ef7c4SIbrahim Tilki } 424*a44ef7c4SIbrahim Tilki 425*a44ef7c4SIbrahim Tilki /* Read ADC Data */ 426*a44ef7c4SIbrahim Tilki return max11410_read_reg(st, MAX11410_REG_DATA0, sample_raw); 427*a44ef7c4SIbrahim Tilki } 428*a44ef7c4SIbrahim Tilki 429*a44ef7c4SIbrahim Tilki static int max11410_get_scale(struct max11410_state *state, 430*a44ef7c4SIbrahim Tilki struct max11410_channel_config cfg) 431*a44ef7c4SIbrahim Tilki { 432*a44ef7c4SIbrahim Tilki struct regulator *vrefp, *vrefn; 433*a44ef7c4SIbrahim Tilki int scale; 434*a44ef7c4SIbrahim Tilki 435*a44ef7c4SIbrahim Tilki vrefp = max11410_get_vrefp(state, cfg.refsel); 436*a44ef7c4SIbrahim Tilki 437*a44ef7c4SIbrahim Tilki scale = regulator_get_voltage(vrefp) / 1000; 438*a44ef7c4SIbrahim Tilki vrefn = max11410_get_vrefn(state, cfg.refsel); 439*a44ef7c4SIbrahim Tilki if (vrefn) 440*a44ef7c4SIbrahim Tilki scale -= regulator_get_voltage(vrefn) / 1000; 441*a44ef7c4SIbrahim Tilki 442*a44ef7c4SIbrahim Tilki if (cfg.bipolar) 443*a44ef7c4SIbrahim Tilki scale *= 2; 444*a44ef7c4SIbrahim Tilki 445*a44ef7c4SIbrahim Tilki return scale >> cfg.gain; 446*a44ef7c4SIbrahim Tilki } 447*a44ef7c4SIbrahim Tilki 448*a44ef7c4SIbrahim Tilki static int max11410_read_raw(struct iio_dev *indio_dev, 449*a44ef7c4SIbrahim Tilki struct iio_chan_spec const *chan, 450*a44ef7c4SIbrahim Tilki int *val, int *val2, long info) 451*a44ef7c4SIbrahim Tilki { 452*a44ef7c4SIbrahim Tilki struct max11410_state *state = iio_priv(indio_dev); 453*a44ef7c4SIbrahim Tilki struct max11410_channel_config cfg = state->channels[chan->address]; 454*a44ef7c4SIbrahim Tilki int ret, reg_val, filter, rate; 455*a44ef7c4SIbrahim Tilki 456*a44ef7c4SIbrahim Tilki switch (info) { 457*a44ef7c4SIbrahim Tilki case IIO_CHAN_INFO_SCALE: 458*a44ef7c4SIbrahim Tilki *val = max11410_get_scale(state, cfg); 459*a44ef7c4SIbrahim Tilki *val2 = chan->scan_type.realbits; 460*a44ef7c4SIbrahim Tilki return IIO_VAL_FRACTIONAL_LOG2; 461*a44ef7c4SIbrahim Tilki case IIO_CHAN_INFO_OFFSET: 462*a44ef7c4SIbrahim Tilki if (cfg.bipolar) 463*a44ef7c4SIbrahim Tilki *val = -BIT(chan->scan_type.realbits - 1); 464*a44ef7c4SIbrahim Tilki else 465*a44ef7c4SIbrahim Tilki *val = 0; 466*a44ef7c4SIbrahim Tilki 467*a44ef7c4SIbrahim Tilki return IIO_VAL_INT; 468*a44ef7c4SIbrahim Tilki case IIO_CHAN_INFO_RAW: 469*a44ef7c4SIbrahim Tilki ret = iio_device_claim_direct_mode(indio_dev); 470*a44ef7c4SIbrahim Tilki if (ret) 471*a44ef7c4SIbrahim Tilki return ret; 472*a44ef7c4SIbrahim Tilki 473*a44ef7c4SIbrahim Tilki mutex_lock(&state->lock); 474*a44ef7c4SIbrahim Tilki 475*a44ef7c4SIbrahim Tilki ret = max11410_sample(state, ®_val, chan); 476*a44ef7c4SIbrahim Tilki 477*a44ef7c4SIbrahim Tilki mutex_unlock(&state->lock); 478*a44ef7c4SIbrahim Tilki 479*a44ef7c4SIbrahim Tilki iio_device_release_direct_mode(indio_dev); 480*a44ef7c4SIbrahim Tilki 481*a44ef7c4SIbrahim Tilki if (ret) 482*a44ef7c4SIbrahim Tilki return ret; 483*a44ef7c4SIbrahim Tilki 484*a44ef7c4SIbrahim Tilki *val = reg_val; 485*a44ef7c4SIbrahim Tilki 486*a44ef7c4SIbrahim Tilki return IIO_VAL_INT; 487*a44ef7c4SIbrahim Tilki case IIO_CHAN_INFO_SAMP_FREQ: 488*a44ef7c4SIbrahim Tilki ret = regmap_read(state->regmap, MAX11410_REG_FILTER, ®_val); 489*a44ef7c4SIbrahim Tilki if (ret) 490*a44ef7c4SIbrahim Tilki return ret; 491*a44ef7c4SIbrahim Tilki 492*a44ef7c4SIbrahim Tilki filter = FIELD_GET(MAX11410_FILTER_LINEF_MASK, reg_val); 493*a44ef7c4SIbrahim Tilki rate = reg_val & MAX11410_FILTER_RATE_MASK; 494*a44ef7c4SIbrahim Tilki if (rate >= max11410_sampling_len[filter]) 495*a44ef7c4SIbrahim Tilki rate = max11410_sampling_len[filter] - 1; 496*a44ef7c4SIbrahim Tilki 497*a44ef7c4SIbrahim Tilki *val = max11410_sampling_rates[filter][rate][0]; 498*a44ef7c4SIbrahim Tilki *val2 = max11410_sampling_rates[filter][rate][1]; 499*a44ef7c4SIbrahim Tilki 500*a44ef7c4SIbrahim Tilki return IIO_VAL_INT_PLUS_MICRO; 501*a44ef7c4SIbrahim Tilki } 502*a44ef7c4SIbrahim Tilki return -EINVAL; 503*a44ef7c4SIbrahim Tilki } 504*a44ef7c4SIbrahim Tilki 505*a44ef7c4SIbrahim Tilki static int max11410_write_raw(struct iio_dev *indio_dev, 506*a44ef7c4SIbrahim Tilki struct iio_chan_spec const *chan, 507*a44ef7c4SIbrahim Tilki int val, int val2, long mask) 508*a44ef7c4SIbrahim Tilki { 509*a44ef7c4SIbrahim Tilki struct max11410_state *st = iio_priv(indio_dev); 510*a44ef7c4SIbrahim Tilki int i, ret, reg_val, filter, gain; 511*a44ef7c4SIbrahim Tilki u32 *scale_avail; 512*a44ef7c4SIbrahim Tilki 513*a44ef7c4SIbrahim Tilki switch (mask) { 514*a44ef7c4SIbrahim Tilki case IIO_CHAN_INFO_SCALE: 515*a44ef7c4SIbrahim Tilki scale_avail = st->channels[chan->address].scale_avail; 516*a44ef7c4SIbrahim Tilki if (!scale_avail) 517*a44ef7c4SIbrahim Tilki return -EOPNOTSUPP; 518*a44ef7c4SIbrahim Tilki 519*a44ef7c4SIbrahim Tilki /* Accept values in range 0.000001 <= scale < 1.000000 */ 520*a44ef7c4SIbrahim Tilki if (val != 0 || val2 == 0) 521*a44ef7c4SIbrahim Tilki return -EINVAL; 522*a44ef7c4SIbrahim Tilki 523*a44ef7c4SIbrahim Tilki ret = iio_device_claim_direct_mode(indio_dev); 524*a44ef7c4SIbrahim Tilki if (ret) 525*a44ef7c4SIbrahim Tilki return ret; 526*a44ef7c4SIbrahim Tilki 527*a44ef7c4SIbrahim Tilki /* Convert from INT_PLUS_MICRO to FRACTIONAL_LOG2 */ 528*a44ef7c4SIbrahim Tilki val2 = val2 * DIV_ROUND_CLOSEST(BIT(24), 1000000); 529*a44ef7c4SIbrahim Tilki val2 = DIV_ROUND_CLOSEST(scale_avail[0], val2); 530*a44ef7c4SIbrahim Tilki gain = order_base_2(val2); 531*a44ef7c4SIbrahim Tilki 532*a44ef7c4SIbrahim Tilki st->channels[chan->address].gain = clamp_val(gain, 0, 7); 533*a44ef7c4SIbrahim Tilki 534*a44ef7c4SIbrahim Tilki iio_device_release_direct_mode(indio_dev); 535*a44ef7c4SIbrahim Tilki 536*a44ef7c4SIbrahim Tilki return 0; 537*a44ef7c4SIbrahim Tilki case IIO_CHAN_INFO_SAMP_FREQ: 538*a44ef7c4SIbrahim Tilki ret = iio_device_claim_direct_mode(indio_dev); 539*a44ef7c4SIbrahim Tilki if (ret) 540*a44ef7c4SIbrahim Tilki return ret; 541*a44ef7c4SIbrahim Tilki 542*a44ef7c4SIbrahim Tilki mutex_lock(&st->lock); 543*a44ef7c4SIbrahim Tilki 544*a44ef7c4SIbrahim Tilki ret = regmap_read(st->regmap, MAX11410_REG_FILTER, ®_val); 545*a44ef7c4SIbrahim Tilki if (ret) 546*a44ef7c4SIbrahim Tilki goto out; 547*a44ef7c4SIbrahim Tilki 548*a44ef7c4SIbrahim Tilki filter = FIELD_GET(MAX11410_FILTER_LINEF_MASK, reg_val); 549*a44ef7c4SIbrahim Tilki 550*a44ef7c4SIbrahim Tilki for (i = 0; i < max11410_sampling_len[filter]; ++i) { 551*a44ef7c4SIbrahim Tilki if (val == max11410_sampling_rates[filter][i][0] && 552*a44ef7c4SIbrahim Tilki val2 == max11410_sampling_rates[filter][i][1]) 553*a44ef7c4SIbrahim Tilki break; 554*a44ef7c4SIbrahim Tilki } 555*a44ef7c4SIbrahim Tilki if (i == max11410_sampling_len[filter]) { 556*a44ef7c4SIbrahim Tilki ret = -EINVAL; 557*a44ef7c4SIbrahim Tilki goto out; 558*a44ef7c4SIbrahim Tilki } 559*a44ef7c4SIbrahim Tilki 560*a44ef7c4SIbrahim Tilki ret = regmap_write_bits(st->regmap, MAX11410_REG_FILTER, 561*a44ef7c4SIbrahim Tilki MAX11410_FILTER_RATE_MASK, i); 562*a44ef7c4SIbrahim Tilki 563*a44ef7c4SIbrahim Tilki out: 564*a44ef7c4SIbrahim Tilki mutex_unlock(&st->lock); 565*a44ef7c4SIbrahim Tilki iio_device_release_direct_mode(indio_dev); 566*a44ef7c4SIbrahim Tilki 567*a44ef7c4SIbrahim Tilki return ret; 568*a44ef7c4SIbrahim Tilki default: 569*a44ef7c4SIbrahim Tilki return -EINVAL; 570*a44ef7c4SIbrahim Tilki } 571*a44ef7c4SIbrahim Tilki } 572*a44ef7c4SIbrahim Tilki 573*a44ef7c4SIbrahim Tilki static int max11410_read_avail(struct iio_dev *indio_dev, 574*a44ef7c4SIbrahim Tilki struct iio_chan_spec const *chan, 575*a44ef7c4SIbrahim Tilki const int **vals, int *type, int *length, 576*a44ef7c4SIbrahim Tilki long info) 577*a44ef7c4SIbrahim Tilki { 578*a44ef7c4SIbrahim Tilki struct max11410_state *st = iio_priv(indio_dev); 579*a44ef7c4SIbrahim Tilki struct max11410_channel_config cfg; 580*a44ef7c4SIbrahim Tilki int ret, reg_val, filter; 581*a44ef7c4SIbrahim Tilki 582*a44ef7c4SIbrahim Tilki switch (info) { 583*a44ef7c4SIbrahim Tilki case IIO_CHAN_INFO_SAMP_FREQ: 584*a44ef7c4SIbrahim Tilki ret = regmap_read(st->regmap, MAX11410_REG_FILTER, ®_val); 585*a44ef7c4SIbrahim Tilki if (ret) 586*a44ef7c4SIbrahim Tilki return ret; 587*a44ef7c4SIbrahim Tilki 588*a44ef7c4SIbrahim Tilki filter = FIELD_GET(MAX11410_FILTER_LINEF_MASK, reg_val); 589*a44ef7c4SIbrahim Tilki 590*a44ef7c4SIbrahim Tilki *vals = (const int *)max11410_sampling_rates[filter]; 591*a44ef7c4SIbrahim Tilki *length = max11410_sampling_len[filter] * 2; 592*a44ef7c4SIbrahim Tilki *type = IIO_VAL_INT_PLUS_MICRO; 593*a44ef7c4SIbrahim Tilki 594*a44ef7c4SIbrahim Tilki return IIO_AVAIL_LIST; 595*a44ef7c4SIbrahim Tilki case IIO_CHAN_INFO_SCALE: 596*a44ef7c4SIbrahim Tilki cfg = st->channels[chan->address]; 597*a44ef7c4SIbrahim Tilki 598*a44ef7c4SIbrahim Tilki if (!cfg.scale_avail) 599*a44ef7c4SIbrahim Tilki return -EINVAL; 600*a44ef7c4SIbrahim Tilki 601*a44ef7c4SIbrahim Tilki *vals = cfg.scale_avail; 602*a44ef7c4SIbrahim Tilki *length = MAX11410_SCALE_AVAIL_SIZE * 2; 603*a44ef7c4SIbrahim Tilki *type = IIO_VAL_FRACTIONAL_LOG2; 604*a44ef7c4SIbrahim Tilki 605*a44ef7c4SIbrahim Tilki return IIO_AVAIL_LIST; 606*a44ef7c4SIbrahim Tilki } 607*a44ef7c4SIbrahim Tilki return -EINVAL; 608*a44ef7c4SIbrahim Tilki } 609*a44ef7c4SIbrahim Tilki 610*a44ef7c4SIbrahim Tilki static const struct iio_info max11410_info = { 611*a44ef7c4SIbrahim Tilki .read_raw = max11410_read_raw, 612*a44ef7c4SIbrahim Tilki .write_raw = max11410_write_raw, 613*a44ef7c4SIbrahim Tilki .read_avail = max11410_read_avail, 614*a44ef7c4SIbrahim Tilki .attrs = &max11410_attribute_group, 615*a44ef7c4SIbrahim Tilki }; 616*a44ef7c4SIbrahim Tilki 617*a44ef7c4SIbrahim Tilki static irqreturn_t max11410_trigger_handler(int irq, void *p) 618*a44ef7c4SIbrahim Tilki { 619*a44ef7c4SIbrahim Tilki struct iio_poll_func *pf = p; 620*a44ef7c4SIbrahim Tilki struct iio_dev *indio_dev = pf->indio_dev; 621*a44ef7c4SIbrahim Tilki struct max11410_state *st = iio_priv(indio_dev); 622*a44ef7c4SIbrahim Tilki int ret; 623*a44ef7c4SIbrahim Tilki 624*a44ef7c4SIbrahim Tilki ret = max11410_read_reg(st, MAX11410_REG_DATA0, &st->scan.data); 625*a44ef7c4SIbrahim Tilki if (ret) { 626*a44ef7c4SIbrahim Tilki dev_err(&indio_dev->dev, "cannot read data\n"); 627*a44ef7c4SIbrahim Tilki goto out; 628*a44ef7c4SIbrahim Tilki } 629*a44ef7c4SIbrahim Tilki 630*a44ef7c4SIbrahim Tilki iio_push_to_buffers_with_timestamp(indio_dev, &st->scan, 631*a44ef7c4SIbrahim Tilki iio_get_time_ns(indio_dev)); 632*a44ef7c4SIbrahim Tilki 633*a44ef7c4SIbrahim Tilki out: 634*a44ef7c4SIbrahim Tilki iio_trigger_notify_done(indio_dev->trig); 635*a44ef7c4SIbrahim Tilki 636*a44ef7c4SIbrahim Tilki return IRQ_HANDLED; 637*a44ef7c4SIbrahim Tilki } 638*a44ef7c4SIbrahim Tilki 639*a44ef7c4SIbrahim Tilki static int max11410_buffer_postenable(struct iio_dev *indio_dev) 640*a44ef7c4SIbrahim Tilki { 641*a44ef7c4SIbrahim Tilki struct max11410_state *st = iio_priv(indio_dev); 642*a44ef7c4SIbrahim Tilki int scan_ch, ret; 643*a44ef7c4SIbrahim Tilki 644*a44ef7c4SIbrahim Tilki scan_ch = ffs(*indio_dev->active_scan_mask) - 1; 645*a44ef7c4SIbrahim Tilki 646*a44ef7c4SIbrahim Tilki ret = max11410_configure_channel(st, &indio_dev->channels[scan_ch]); 647*a44ef7c4SIbrahim Tilki if (ret) 648*a44ef7c4SIbrahim Tilki return ret; 649*a44ef7c4SIbrahim Tilki 650*a44ef7c4SIbrahim Tilki /* Start continuous conversion. */ 651*a44ef7c4SIbrahim Tilki return max11410_write_reg(st, MAX11410_REG_CONV_START, 652*a44ef7c4SIbrahim Tilki MAX11410_CONV_TYPE_CONTINUOUS); 653*a44ef7c4SIbrahim Tilki } 654*a44ef7c4SIbrahim Tilki 655*a44ef7c4SIbrahim Tilki static int max11410_buffer_predisable(struct iio_dev *indio_dev) 656*a44ef7c4SIbrahim Tilki { 657*a44ef7c4SIbrahim Tilki struct max11410_state *st = iio_priv(indio_dev); 658*a44ef7c4SIbrahim Tilki 659*a44ef7c4SIbrahim Tilki /* Stop continuous conversion. */ 660*a44ef7c4SIbrahim Tilki return max11410_write_reg(st, MAX11410_REG_CONV_START, 661*a44ef7c4SIbrahim Tilki MAX11410_CONV_TYPE_SINGLE); 662*a44ef7c4SIbrahim Tilki } 663*a44ef7c4SIbrahim Tilki 664*a44ef7c4SIbrahim Tilki static const struct iio_buffer_setup_ops max11410_buffer_ops = { 665*a44ef7c4SIbrahim Tilki .postenable = &max11410_buffer_postenable, 666*a44ef7c4SIbrahim Tilki .predisable = &max11410_buffer_predisable, 667*a44ef7c4SIbrahim Tilki .validate_scan_mask = &iio_validate_scan_mask_onehot, 668*a44ef7c4SIbrahim Tilki }; 669*a44ef7c4SIbrahim Tilki 670*a44ef7c4SIbrahim Tilki static const struct iio_trigger_ops max11410_trigger_ops = { 671*a44ef7c4SIbrahim Tilki .validate_device = iio_trigger_validate_own_device, 672*a44ef7c4SIbrahim Tilki }; 673*a44ef7c4SIbrahim Tilki 674*a44ef7c4SIbrahim Tilki static irqreturn_t max11410_interrupt(int irq, void *dev_id) 675*a44ef7c4SIbrahim Tilki { 676*a44ef7c4SIbrahim Tilki struct iio_dev *indio_dev = dev_id; 677*a44ef7c4SIbrahim Tilki struct max11410_state *st = iio_priv(indio_dev); 678*a44ef7c4SIbrahim Tilki 679*a44ef7c4SIbrahim Tilki if (iio_buffer_enabled(indio_dev)) 680*a44ef7c4SIbrahim Tilki iio_trigger_poll_chained(st->trig); 681*a44ef7c4SIbrahim Tilki else 682*a44ef7c4SIbrahim Tilki complete(&st->completion); 683*a44ef7c4SIbrahim Tilki 684*a44ef7c4SIbrahim Tilki return IRQ_HANDLED; 685*a44ef7c4SIbrahim Tilki }; 686*a44ef7c4SIbrahim Tilki 687*a44ef7c4SIbrahim Tilki static int max11410_parse_channels(struct max11410_state *st, 688*a44ef7c4SIbrahim Tilki struct iio_dev *indio_dev) 689*a44ef7c4SIbrahim Tilki { 690*a44ef7c4SIbrahim Tilki struct iio_chan_spec chanspec = chanspec_template; 691*a44ef7c4SIbrahim Tilki struct device *dev = &st->spi_dev->dev; 692*a44ef7c4SIbrahim Tilki struct max11410_channel_config *cfg; 693*a44ef7c4SIbrahim Tilki struct iio_chan_spec *channels; 694*a44ef7c4SIbrahim Tilki struct fwnode_handle *child; 695*a44ef7c4SIbrahim Tilki u32 reference, sig_path; 696*a44ef7c4SIbrahim Tilki const char *node_name; 697*a44ef7c4SIbrahim Tilki u32 inputs[2], scale; 698*a44ef7c4SIbrahim Tilki unsigned int num_ch; 699*a44ef7c4SIbrahim Tilki int chan_idx = 0; 700*a44ef7c4SIbrahim Tilki int ret, i; 701*a44ef7c4SIbrahim Tilki 702*a44ef7c4SIbrahim Tilki num_ch = device_get_child_node_count(dev); 703*a44ef7c4SIbrahim Tilki if (num_ch == 0) 704*a44ef7c4SIbrahim Tilki return dev_err_probe(&indio_dev->dev, -ENODEV, 705*a44ef7c4SIbrahim Tilki "FW has no channels defined\n"); 706*a44ef7c4SIbrahim Tilki 707*a44ef7c4SIbrahim Tilki /* Reserve space for soft timestamp channel */ 708*a44ef7c4SIbrahim Tilki num_ch++; 709*a44ef7c4SIbrahim Tilki channels = devm_kcalloc(dev, num_ch, sizeof(*channels), GFP_KERNEL); 710*a44ef7c4SIbrahim Tilki if (!channels) 711*a44ef7c4SIbrahim Tilki return -ENOMEM; 712*a44ef7c4SIbrahim Tilki 713*a44ef7c4SIbrahim Tilki st->channels = devm_kcalloc(dev, num_ch, sizeof(*st->channels), 714*a44ef7c4SIbrahim Tilki GFP_KERNEL); 715*a44ef7c4SIbrahim Tilki if (!st->channels) 716*a44ef7c4SIbrahim Tilki return -ENOMEM; 717*a44ef7c4SIbrahim Tilki 718*a44ef7c4SIbrahim Tilki device_for_each_child_node(dev, child) { 719*a44ef7c4SIbrahim Tilki node_name = fwnode_get_name(child); 720*a44ef7c4SIbrahim Tilki if (fwnode_property_present(child, "diff-channels")) { 721*a44ef7c4SIbrahim Tilki ret = fwnode_property_read_u32_array(child, 722*a44ef7c4SIbrahim Tilki "diff-channels", 723*a44ef7c4SIbrahim Tilki inputs, 724*a44ef7c4SIbrahim Tilki ARRAY_SIZE(inputs)); 725*a44ef7c4SIbrahim Tilki 726*a44ef7c4SIbrahim Tilki chanspec.differential = 1; 727*a44ef7c4SIbrahim Tilki } else { 728*a44ef7c4SIbrahim Tilki ret = fwnode_property_read_u32(child, "reg", &inputs[0]); 729*a44ef7c4SIbrahim Tilki 730*a44ef7c4SIbrahim Tilki inputs[1] = 0; 731*a44ef7c4SIbrahim Tilki chanspec.differential = 0; 732*a44ef7c4SIbrahim Tilki } 733*a44ef7c4SIbrahim Tilki if (ret) { 734*a44ef7c4SIbrahim Tilki fwnode_handle_put(child); 735*a44ef7c4SIbrahim Tilki return ret; 736*a44ef7c4SIbrahim Tilki } 737*a44ef7c4SIbrahim Tilki 738*a44ef7c4SIbrahim Tilki if (inputs[0] > MAX11410_CHANNEL_INDEX_MAX || 739*a44ef7c4SIbrahim Tilki inputs[1] > MAX11410_CHANNEL_INDEX_MAX) { 740*a44ef7c4SIbrahim Tilki fwnode_handle_put(child); 741*a44ef7c4SIbrahim Tilki return dev_err_probe(&indio_dev->dev, -EINVAL, 742*a44ef7c4SIbrahim Tilki "Invalid channel index for %s, should be less than %d\n", 743*a44ef7c4SIbrahim Tilki node_name, 744*a44ef7c4SIbrahim Tilki MAX11410_CHANNEL_INDEX_MAX + 1); 745*a44ef7c4SIbrahim Tilki } 746*a44ef7c4SIbrahim Tilki 747*a44ef7c4SIbrahim Tilki cfg = &st->channels[chan_idx]; 748*a44ef7c4SIbrahim Tilki 749*a44ef7c4SIbrahim Tilki reference = MAX11410_REFSEL_AVDD_AGND; 750*a44ef7c4SIbrahim Tilki fwnode_property_read_u32(child, "adi,reference", &reference); 751*a44ef7c4SIbrahim Tilki if (reference > MAX11410_REFSEL_MAX) { 752*a44ef7c4SIbrahim Tilki fwnode_handle_put(child); 753*a44ef7c4SIbrahim Tilki return dev_err_probe(&indio_dev->dev, -EINVAL, 754*a44ef7c4SIbrahim Tilki "Invalid adi,reference value for %s, should be less than %d.\n", 755*a44ef7c4SIbrahim Tilki node_name, MAX11410_REFSEL_MAX + 1); 756*a44ef7c4SIbrahim Tilki } 757*a44ef7c4SIbrahim Tilki 758*a44ef7c4SIbrahim Tilki if (!max11410_get_vrefp(st, reference) || 759*a44ef7c4SIbrahim Tilki (!max11410_get_vrefn(st, reference) && reference <= 2)) { 760*a44ef7c4SIbrahim Tilki fwnode_handle_put(child); 761*a44ef7c4SIbrahim Tilki return dev_err_probe(&indio_dev->dev, -EINVAL, 762*a44ef7c4SIbrahim Tilki "Invalid VREF configuration for %s, either specify corresponding VREF regulators or change adi,reference property.\n", 763*a44ef7c4SIbrahim Tilki node_name); 764*a44ef7c4SIbrahim Tilki } 765*a44ef7c4SIbrahim Tilki 766*a44ef7c4SIbrahim Tilki sig_path = MAX11410_PGA_SIG_PATH_BUFFERED; 767*a44ef7c4SIbrahim Tilki fwnode_property_read_u32(child, "adi,input-mode", &sig_path); 768*a44ef7c4SIbrahim Tilki if (sig_path > MAX11410_SIG_PATH_MAX) { 769*a44ef7c4SIbrahim Tilki fwnode_handle_put(child); 770*a44ef7c4SIbrahim Tilki return dev_err_probe(&indio_dev->dev, -EINVAL, 771*a44ef7c4SIbrahim Tilki "Invalid adi,input-mode value for %s, should be less than %d.\n", 772*a44ef7c4SIbrahim Tilki node_name, MAX11410_SIG_PATH_MAX + 1); 773*a44ef7c4SIbrahim Tilki } 774*a44ef7c4SIbrahim Tilki 775*a44ef7c4SIbrahim Tilki fwnode_property_read_u32(child, "settling-time-us", 776*a44ef7c4SIbrahim Tilki &cfg->settling_time_us); 777*a44ef7c4SIbrahim Tilki cfg->bipolar = fwnode_property_read_bool(child, "bipolar"); 778*a44ef7c4SIbrahim Tilki cfg->buffered_vrefp = fwnode_property_read_bool(child, "adi,buffered-vrefp"); 779*a44ef7c4SIbrahim Tilki cfg->buffered_vrefn = fwnode_property_read_bool(child, "adi,buffered-vrefn"); 780*a44ef7c4SIbrahim Tilki cfg->refsel = reference; 781*a44ef7c4SIbrahim Tilki cfg->sig_path = sig_path; 782*a44ef7c4SIbrahim Tilki cfg->gain = 0; 783*a44ef7c4SIbrahim Tilki 784*a44ef7c4SIbrahim Tilki /* Enable scale_available property if input mode is PGA */ 785*a44ef7c4SIbrahim Tilki if (sig_path == MAX11410_PGA_SIG_PATH_PGA) { 786*a44ef7c4SIbrahim Tilki __set_bit(IIO_CHAN_INFO_SCALE, 787*a44ef7c4SIbrahim Tilki &chanspec.info_mask_separate_available); 788*a44ef7c4SIbrahim Tilki cfg->scale_avail = devm_kcalloc(dev, MAX11410_SCALE_AVAIL_SIZE * 2, 789*a44ef7c4SIbrahim Tilki sizeof(*cfg->scale_avail), 790*a44ef7c4SIbrahim Tilki GFP_KERNEL); 791*a44ef7c4SIbrahim Tilki if (!cfg->scale_avail) { 792*a44ef7c4SIbrahim Tilki fwnode_handle_put(child); 793*a44ef7c4SIbrahim Tilki return -ENOMEM; 794*a44ef7c4SIbrahim Tilki } 795*a44ef7c4SIbrahim Tilki 796*a44ef7c4SIbrahim Tilki scale = max11410_get_scale(st, *cfg); 797*a44ef7c4SIbrahim Tilki for (i = 0; i < MAX11410_SCALE_AVAIL_SIZE; i++) { 798*a44ef7c4SIbrahim Tilki cfg->scale_avail[2 * i] = scale >> i; 799*a44ef7c4SIbrahim Tilki cfg->scale_avail[2 * i + 1] = chanspec.scan_type.realbits; 800*a44ef7c4SIbrahim Tilki } 801*a44ef7c4SIbrahim Tilki } else { 802*a44ef7c4SIbrahim Tilki __clear_bit(IIO_CHAN_INFO_SCALE, 803*a44ef7c4SIbrahim Tilki &chanspec.info_mask_separate_available); 804*a44ef7c4SIbrahim Tilki } 805*a44ef7c4SIbrahim Tilki 806*a44ef7c4SIbrahim Tilki chanspec.address = chan_idx; 807*a44ef7c4SIbrahim Tilki chanspec.scan_index = chan_idx; 808*a44ef7c4SIbrahim Tilki chanspec.channel = inputs[0]; 809*a44ef7c4SIbrahim Tilki chanspec.channel2 = inputs[1]; 810*a44ef7c4SIbrahim Tilki 811*a44ef7c4SIbrahim Tilki channels[chan_idx] = chanspec; 812*a44ef7c4SIbrahim Tilki chan_idx++; 813*a44ef7c4SIbrahim Tilki } 814*a44ef7c4SIbrahim Tilki 815*a44ef7c4SIbrahim Tilki channels[chan_idx] = (struct iio_chan_spec)IIO_CHAN_SOFT_TIMESTAMP(chan_idx); 816*a44ef7c4SIbrahim Tilki 817*a44ef7c4SIbrahim Tilki indio_dev->num_channels = chan_idx + 1; 818*a44ef7c4SIbrahim Tilki indio_dev->channels = channels; 819*a44ef7c4SIbrahim Tilki 820*a44ef7c4SIbrahim Tilki return 0; 821*a44ef7c4SIbrahim Tilki } 822*a44ef7c4SIbrahim Tilki 823*a44ef7c4SIbrahim Tilki static void max11410_disable_reg(void *reg) 824*a44ef7c4SIbrahim Tilki { 825*a44ef7c4SIbrahim Tilki regulator_disable(reg); 826*a44ef7c4SIbrahim Tilki } 827*a44ef7c4SIbrahim Tilki 828*a44ef7c4SIbrahim Tilki static int max11410_init_vref(struct device *dev, 829*a44ef7c4SIbrahim Tilki struct regulator **vref, 830*a44ef7c4SIbrahim Tilki const char *id) 831*a44ef7c4SIbrahim Tilki { 832*a44ef7c4SIbrahim Tilki struct regulator *reg; 833*a44ef7c4SIbrahim Tilki int ret; 834*a44ef7c4SIbrahim Tilki 835*a44ef7c4SIbrahim Tilki reg = devm_regulator_get_optional(dev, id); 836*a44ef7c4SIbrahim Tilki if (PTR_ERR(reg) == -ENODEV) { 837*a44ef7c4SIbrahim Tilki *vref = NULL; 838*a44ef7c4SIbrahim Tilki return 0; 839*a44ef7c4SIbrahim Tilki } else if (IS_ERR(reg)) { 840*a44ef7c4SIbrahim Tilki return PTR_ERR(reg); 841*a44ef7c4SIbrahim Tilki } 842*a44ef7c4SIbrahim Tilki ret = regulator_enable(reg); 843*a44ef7c4SIbrahim Tilki if (ret) 844*a44ef7c4SIbrahim Tilki return dev_err_probe(dev, ret, 845*a44ef7c4SIbrahim Tilki "Failed to enable regulator %s\n", id); 846*a44ef7c4SIbrahim Tilki 847*a44ef7c4SIbrahim Tilki *vref = reg; 848*a44ef7c4SIbrahim Tilki return devm_add_action_or_reset(dev, max11410_disable_reg, reg); 849*a44ef7c4SIbrahim Tilki } 850*a44ef7c4SIbrahim Tilki 851*a44ef7c4SIbrahim Tilki static int max11410_calibrate(struct max11410_state *st, u32 cal_type) 852*a44ef7c4SIbrahim Tilki { 853*a44ef7c4SIbrahim Tilki int ret, val; 854*a44ef7c4SIbrahim Tilki 855*a44ef7c4SIbrahim Tilki ret = max11410_write_reg(st, MAX11410_REG_CAL_START, cal_type); 856*a44ef7c4SIbrahim Tilki if (ret) 857*a44ef7c4SIbrahim Tilki return ret; 858*a44ef7c4SIbrahim Tilki 859*a44ef7c4SIbrahim Tilki /* Wait for status register Calibration Ready flag */ 860*a44ef7c4SIbrahim Tilki return read_poll_timeout(max11410_read_reg, ret, 861*a44ef7c4SIbrahim Tilki ret || (val & MAX11410_STATUS_CAL_READY_BIT), 862*a44ef7c4SIbrahim Tilki 50000, MAX11410_CALIB_TIMEOUT_MS * 1000, true, 863*a44ef7c4SIbrahim Tilki st, MAX11410_REG_STATUS, &val); 864*a44ef7c4SIbrahim Tilki } 865*a44ef7c4SIbrahim Tilki 866*a44ef7c4SIbrahim Tilki static int max11410_self_calibrate(struct max11410_state *st) 867*a44ef7c4SIbrahim Tilki { 868*a44ef7c4SIbrahim Tilki int ret, i; 869*a44ef7c4SIbrahim Tilki 870*a44ef7c4SIbrahim Tilki ret = regmap_write_bits(st->regmap, MAX11410_REG_FILTER, 871*a44ef7c4SIbrahim Tilki MAX11410_FILTER_RATE_MASK, 872*a44ef7c4SIbrahim Tilki FIELD_PREP(MAX11410_FILTER_RATE_MASK, 873*a44ef7c4SIbrahim Tilki MAX11410_FILTER_RATE_MAX)); 874*a44ef7c4SIbrahim Tilki if (ret) 875*a44ef7c4SIbrahim Tilki return ret; 876*a44ef7c4SIbrahim Tilki 877*a44ef7c4SIbrahim Tilki ret = max11410_calibrate(st, MAX11410_CAL_START_SELF); 878*a44ef7c4SIbrahim Tilki if (ret) 879*a44ef7c4SIbrahim Tilki return ret; 880*a44ef7c4SIbrahim Tilki 881*a44ef7c4SIbrahim Tilki ret = regmap_write_bits(st->regmap, MAX11410_REG_PGA, 882*a44ef7c4SIbrahim Tilki MAX11410_PGA_SIG_PATH_MASK, 883*a44ef7c4SIbrahim Tilki FIELD_PREP(MAX11410_PGA_SIG_PATH_MASK, 884*a44ef7c4SIbrahim Tilki MAX11410_PGA_SIG_PATH_PGA)); 885*a44ef7c4SIbrahim Tilki if (ret) 886*a44ef7c4SIbrahim Tilki return ret; 887*a44ef7c4SIbrahim Tilki 888*a44ef7c4SIbrahim Tilki /* PGA calibrations */ 889*a44ef7c4SIbrahim Tilki for (i = 1; i < 8; ++i) { 890*a44ef7c4SIbrahim Tilki ret = regmap_write_bits(st->regmap, MAX11410_REG_PGA, 891*a44ef7c4SIbrahim Tilki MAX11410_PGA_GAIN_MASK, i); 892*a44ef7c4SIbrahim Tilki if (ret) 893*a44ef7c4SIbrahim Tilki return ret; 894*a44ef7c4SIbrahim Tilki 895*a44ef7c4SIbrahim Tilki ret = max11410_calibrate(st, MAX11410_CAL_START_PGA); 896*a44ef7c4SIbrahim Tilki if (ret) 897*a44ef7c4SIbrahim Tilki return ret; 898*a44ef7c4SIbrahim Tilki } 899*a44ef7c4SIbrahim Tilki 900*a44ef7c4SIbrahim Tilki /* Cleanup */ 901*a44ef7c4SIbrahim Tilki ret = regmap_write_bits(st->regmap, MAX11410_REG_PGA, 902*a44ef7c4SIbrahim Tilki MAX11410_PGA_GAIN_MASK, 0); 903*a44ef7c4SIbrahim Tilki if (ret) 904*a44ef7c4SIbrahim Tilki return ret; 905*a44ef7c4SIbrahim Tilki 906*a44ef7c4SIbrahim Tilki ret = regmap_write_bits(st->regmap, MAX11410_REG_FILTER, 907*a44ef7c4SIbrahim Tilki MAX11410_FILTER_RATE_MASK, 0); 908*a44ef7c4SIbrahim Tilki if (ret) 909*a44ef7c4SIbrahim Tilki return ret; 910*a44ef7c4SIbrahim Tilki 911*a44ef7c4SIbrahim Tilki return regmap_write_bits(st->regmap, MAX11410_REG_PGA, 912*a44ef7c4SIbrahim Tilki MAX11410_PGA_SIG_PATH_MASK, 913*a44ef7c4SIbrahim Tilki FIELD_PREP(MAX11410_PGA_SIG_PATH_MASK, 914*a44ef7c4SIbrahim Tilki MAX11410_PGA_SIG_PATH_BUFFERED)); 915*a44ef7c4SIbrahim Tilki } 916*a44ef7c4SIbrahim Tilki 917*a44ef7c4SIbrahim Tilki static int max11410_probe(struct spi_device *spi) 918*a44ef7c4SIbrahim Tilki { 919*a44ef7c4SIbrahim Tilki const char *vrefp_regs[] = { "vref0p", "vref1p", "vref2p" }; 920*a44ef7c4SIbrahim Tilki const char *vrefn_regs[] = { "vref0n", "vref1n", "vref2n" }; 921*a44ef7c4SIbrahim Tilki struct device *dev = &spi->dev; 922*a44ef7c4SIbrahim Tilki struct max11410_state *st; 923*a44ef7c4SIbrahim Tilki struct iio_dev *indio_dev; 924*a44ef7c4SIbrahim Tilki int ret, irqs[2]; 925*a44ef7c4SIbrahim Tilki int i; 926*a44ef7c4SIbrahim Tilki 927*a44ef7c4SIbrahim Tilki indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 928*a44ef7c4SIbrahim Tilki if (!indio_dev) 929*a44ef7c4SIbrahim Tilki return -ENOMEM; 930*a44ef7c4SIbrahim Tilki 931*a44ef7c4SIbrahim Tilki st = iio_priv(indio_dev); 932*a44ef7c4SIbrahim Tilki st->spi_dev = spi; 933*a44ef7c4SIbrahim Tilki init_completion(&st->completion); 934*a44ef7c4SIbrahim Tilki mutex_init(&st->lock); 935*a44ef7c4SIbrahim Tilki 936*a44ef7c4SIbrahim Tilki indio_dev->name = "max11410"; 937*a44ef7c4SIbrahim Tilki indio_dev->modes = INDIO_DIRECT_MODE; 938*a44ef7c4SIbrahim Tilki indio_dev->info = &max11410_info; 939*a44ef7c4SIbrahim Tilki 940*a44ef7c4SIbrahim Tilki st->regmap = devm_regmap_init_spi(spi, ®map_config); 941*a44ef7c4SIbrahim Tilki if (IS_ERR(st->regmap)) 942*a44ef7c4SIbrahim Tilki return dev_err_probe(dev, PTR_ERR(st->regmap), 943*a44ef7c4SIbrahim Tilki "regmap initialization failed\n"); 944*a44ef7c4SIbrahim Tilki 945*a44ef7c4SIbrahim Tilki ret = max11410_init_vref(dev, &st->avdd, "avdd"); 946*a44ef7c4SIbrahim Tilki if (ret) 947*a44ef7c4SIbrahim Tilki return ret; 948*a44ef7c4SIbrahim Tilki 949*a44ef7c4SIbrahim Tilki for (i = 0; i < ARRAY_SIZE(vrefp_regs); i++) { 950*a44ef7c4SIbrahim Tilki ret = max11410_init_vref(dev, &st->vrefp[i], vrefp_regs[i]); 951*a44ef7c4SIbrahim Tilki if (ret) 952*a44ef7c4SIbrahim Tilki return ret; 953*a44ef7c4SIbrahim Tilki 954*a44ef7c4SIbrahim Tilki ret = max11410_init_vref(dev, &st->vrefn[i], vrefn_regs[i]); 955*a44ef7c4SIbrahim Tilki if (ret) 956*a44ef7c4SIbrahim Tilki return ret; 957*a44ef7c4SIbrahim Tilki } 958*a44ef7c4SIbrahim Tilki 959*a44ef7c4SIbrahim Tilki /* 960*a44ef7c4SIbrahim Tilki * Regulators must be configured before parsing channels for 961*a44ef7c4SIbrahim Tilki * validating "adi,reference" property of each channel. 962*a44ef7c4SIbrahim Tilki */ 963*a44ef7c4SIbrahim Tilki ret = max11410_parse_channels(st, indio_dev); 964*a44ef7c4SIbrahim Tilki if (ret) 965*a44ef7c4SIbrahim Tilki return ret; 966*a44ef7c4SIbrahim Tilki 967*a44ef7c4SIbrahim Tilki irqs[0] = fwnode_irq_get_byname(dev_fwnode(dev), "gpio0"); 968*a44ef7c4SIbrahim Tilki irqs[1] = fwnode_irq_get_byname(dev_fwnode(dev), "gpio1"); 969*a44ef7c4SIbrahim Tilki 970*a44ef7c4SIbrahim Tilki if (irqs[0] > 0) { 971*a44ef7c4SIbrahim Tilki st->irq = irqs[0]; 972*a44ef7c4SIbrahim Tilki ret = regmap_write(st->regmap, MAX11410_REG_GPIO_CTRL(0), 973*a44ef7c4SIbrahim Tilki MAX11410_GPIO_INTRB); 974*a44ef7c4SIbrahim Tilki } else if (irqs[1] > 0) { 975*a44ef7c4SIbrahim Tilki st->irq = irqs[1]; 976*a44ef7c4SIbrahim Tilki ret = regmap_write(st->regmap, MAX11410_REG_GPIO_CTRL(1), 977*a44ef7c4SIbrahim Tilki MAX11410_GPIO_INTRB); 978*a44ef7c4SIbrahim Tilki } else if (spi->irq > 0) { 979*a44ef7c4SIbrahim Tilki return dev_err_probe(dev, -ENODEV, 980*a44ef7c4SIbrahim Tilki "no interrupt name specified"); 981*a44ef7c4SIbrahim Tilki } 982*a44ef7c4SIbrahim Tilki 983*a44ef7c4SIbrahim Tilki if (ret) 984*a44ef7c4SIbrahim Tilki return ret; 985*a44ef7c4SIbrahim Tilki 986*a44ef7c4SIbrahim Tilki ret = regmap_set_bits(st->regmap, MAX11410_REG_CTRL, 987*a44ef7c4SIbrahim Tilki MAX11410_CTRL_FORMAT_BIT); 988*a44ef7c4SIbrahim Tilki if (ret) 989*a44ef7c4SIbrahim Tilki return ret; 990*a44ef7c4SIbrahim Tilki 991*a44ef7c4SIbrahim Tilki ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, 992*a44ef7c4SIbrahim Tilki &max11410_trigger_handler, 993*a44ef7c4SIbrahim Tilki &max11410_buffer_ops); 994*a44ef7c4SIbrahim Tilki if (ret) 995*a44ef7c4SIbrahim Tilki return ret; 996*a44ef7c4SIbrahim Tilki 997*a44ef7c4SIbrahim Tilki if (st->irq > 0) { 998*a44ef7c4SIbrahim Tilki st->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", 999*a44ef7c4SIbrahim Tilki indio_dev->name, 1000*a44ef7c4SIbrahim Tilki iio_device_id(indio_dev)); 1001*a44ef7c4SIbrahim Tilki if (!st->trig) 1002*a44ef7c4SIbrahim Tilki return -ENOMEM; 1003*a44ef7c4SIbrahim Tilki 1004*a44ef7c4SIbrahim Tilki st->trig->ops = &max11410_trigger_ops; 1005*a44ef7c4SIbrahim Tilki ret = devm_iio_trigger_register(dev, st->trig); 1006*a44ef7c4SIbrahim Tilki if (ret) 1007*a44ef7c4SIbrahim Tilki return ret; 1008*a44ef7c4SIbrahim Tilki 1009*a44ef7c4SIbrahim Tilki ret = devm_request_threaded_irq(dev, st->irq, NULL, 1010*a44ef7c4SIbrahim Tilki &max11410_interrupt, 1011*a44ef7c4SIbrahim Tilki IRQF_ONESHOT, "max11410", 1012*a44ef7c4SIbrahim Tilki indio_dev); 1013*a44ef7c4SIbrahim Tilki if (ret) 1014*a44ef7c4SIbrahim Tilki return ret; 1015*a44ef7c4SIbrahim Tilki } 1016*a44ef7c4SIbrahim Tilki 1017*a44ef7c4SIbrahim Tilki ret = max11410_self_calibrate(st); 1018*a44ef7c4SIbrahim Tilki if (ret) 1019*a44ef7c4SIbrahim Tilki return dev_err_probe(dev, ret, 1020*a44ef7c4SIbrahim Tilki "cannot perform device self calibration\n"); 1021*a44ef7c4SIbrahim Tilki 1022*a44ef7c4SIbrahim Tilki return devm_iio_device_register(dev, indio_dev); 1023*a44ef7c4SIbrahim Tilki } 1024*a44ef7c4SIbrahim Tilki 1025*a44ef7c4SIbrahim Tilki static const struct of_device_id max11410_spi_of_id[] = { 1026*a44ef7c4SIbrahim Tilki { .compatible = "adi,max11410" }, 1027*a44ef7c4SIbrahim Tilki { } 1028*a44ef7c4SIbrahim Tilki }; 1029*a44ef7c4SIbrahim Tilki MODULE_DEVICE_TABLE(of, max11410_spi_of_id); 1030*a44ef7c4SIbrahim Tilki 1031*a44ef7c4SIbrahim Tilki static const struct spi_device_id max11410_id[] = { 1032*a44ef7c4SIbrahim Tilki { "max11410" }, 1033*a44ef7c4SIbrahim Tilki { } 1034*a44ef7c4SIbrahim Tilki }; 1035*a44ef7c4SIbrahim Tilki MODULE_DEVICE_TABLE(spi, max11410_id); 1036*a44ef7c4SIbrahim Tilki 1037*a44ef7c4SIbrahim Tilki static struct spi_driver max11410_driver = { 1038*a44ef7c4SIbrahim Tilki .driver = { 1039*a44ef7c4SIbrahim Tilki .name = "max11410", 1040*a44ef7c4SIbrahim Tilki .of_match_table = max11410_spi_of_id, 1041*a44ef7c4SIbrahim Tilki }, 1042*a44ef7c4SIbrahim Tilki .probe = max11410_probe, 1043*a44ef7c4SIbrahim Tilki .id_table = max11410_id, 1044*a44ef7c4SIbrahim Tilki }; 1045*a44ef7c4SIbrahim Tilki module_spi_driver(max11410_driver); 1046*a44ef7c4SIbrahim Tilki 1047*a44ef7c4SIbrahim Tilki MODULE_AUTHOR("David Jung <David.Jung@analog.com>"); 1048*a44ef7c4SIbrahim Tilki MODULE_AUTHOR("Ibrahim Tilki <Ibrahim.Tilki@analog.com>"); 1049*a44ef7c4SIbrahim Tilki MODULE_DESCRIPTION("Analog Devices MAX11410 ADC"); 1050*a44ef7c4SIbrahim Tilki MODULE_LICENSE("GPL"); 1051