Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: adc: add max11205 adc driver

Adding support for max11205 16-bit single-channel ultra-low power
delta-sigma adc.
The MAX11205 is compatible with the 2-wire interface and uses
SCLK and RDY/DOUT for serial communications. In this mode, all
controls are implemented by timing the high or low phase of the SCLK.
The 2-wire serial interface only allows for data to be read out through
the RDY/DOUT output.

Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX11205.pdf
Signed-off-by: Ramona Bolboaca <ramona.bolboaca@analog.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Link: https://lore.kernel.org/r/20220831133021.215625-2-ramona.bolboaca@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Ramona Bolboaca and committed by
Jonathan Cameron
0fea1007 5a80c257

+198
+14
drivers/iio/adc/Kconfig
··· 653 653 To compile this driver as a module, choose M here: the module will be 654 654 called max1118. 655 655 656 + config MAX11205 657 + tristate "Maxim max11205 ADC driver" 658 + depends on SPI 659 + select AD_SIGMA_DELTA 660 + select IIO_BUFFER 661 + select IIO_TRIGGERED_BUFFER 662 + 663 + help 664 + Say yes here to build support for Maxim max11205 16-bit, single-channel 665 + ultra-low power delta-sigma ADC. 666 + 667 + To compile this driver as a module, choose M here: the module will be 668 + called max11205. 669 + 656 670 config MAX1241 657 671 tristate "Maxim max1241 ADC driver" 658 672 depends on SPI_MASTER
+1
drivers/iio/adc/Makefile
··· 61 61 obj-$(CONFIG_MAX1027) += max1027.o 62 62 obj-$(CONFIG_MAX11100) += max11100.o 63 63 obj-$(CONFIG_MAX1118) += max1118.o 64 + obj-$(CONFIG_MAX11205) += max11205.o 64 65 obj-$(CONFIG_MAX1241) += max1241.o 65 66 obj-$(CONFIG_MAX1363) += max1363.o 66 67 obj-$(CONFIG_MAX9611) += max9611.o
+183
drivers/iio/adc/max11205.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Maxim MAX11205 16-Bit Delta-Sigma ADC 4 + * 5 + * Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX1240-max11205.pdf 6 + * Copyright (C) 2022 Analog Devices, Inc. 7 + * Author: Ramona Bolboaca <ramona.bolboaca@analog.com> 8 + */ 9 + 10 + #include <linux/device.h> 11 + #include <linux/module.h> 12 + #include <linux/regulator/consumer.h> 13 + #include <linux/spi/spi.h> 14 + 15 + #include <linux/iio/iio.h> 16 + #include <linux/iio/adc/ad_sigma_delta.h> 17 + 18 + #define MAX11205_BIT_SCALE 15 19 + #define MAX11205A_OUT_DATA_RATE 116 20 + #define MAX11205B_OUT_DATA_RATE 13 21 + 22 + enum max11205_chip_type { 23 + TYPE_MAX11205A, 24 + TYPE_MAX11205B, 25 + }; 26 + 27 + struct max11205_chip_info { 28 + unsigned int out_data_rate; 29 + const char *name; 30 + }; 31 + 32 + struct max11205_state { 33 + const struct max11205_chip_info *chip_info; 34 + struct regulator *vref; 35 + struct ad_sigma_delta sd; 36 + }; 37 + 38 + static const struct ad_sigma_delta_info max11205_sigma_delta_info = { 39 + .has_registers = false, 40 + }; 41 + 42 + static int max11205_read_raw(struct iio_dev *indio_dev, 43 + struct iio_chan_spec const *chan, 44 + int *val, int *val2, long mask) 45 + { 46 + struct max11205_state *st = iio_priv(indio_dev); 47 + int reg_mv; 48 + 49 + switch (mask) { 50 + case IIO_CHAN_INFO_RAW: 51 + return ad_sigma_delta_single_conversion(indio_dev, chan, val); 52 + case IIO_CHAN_INFO_SCALE: 53 + reg_mv = regulator_get_voltage(st->vref); 54 + if (reg_mv < 0) 55 + return reg_mv; 56 + reg_mv /= 1000; 57 + *val = reg_mv; 58 + *val2 = MAX11205_BIT_SCALE; 59 + return IIO_VAL_FRACTIONAL_LOG2; 60 + case IIO_CHAN_INFO_SAMP_FREQ: 61 + *val = st->chip_info->out_data_rate; 62 + return IIO_VAL_INT; 63 + default: 64 + return -EINVAL; 65 + } 66 + } 67 + 68 + static const struct iio_info max11205_iio_info = { 69 + .read_raw = max11205_read_raw, 70 + .validate_trigger = ad_sd_validate_trigger, 71 + }; 72 + 73 + static const struct iio_chan_spec max11205_channels[] = { 74 + { 75 + .type = IIO_VOLTAGE, 76 + .indexed = 1, 77 + .scan_type = { 78 + .sign = 's', 79 + .realbits = 16, 80 + .storagebits = 16, 81 + .endianness = IIO_BE, 82 + }, 83 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 84 + BIT(IIO_CHAN_INFO_SAMP_FREQ) | 85 + BIT(IIO_CHAN_INFO_SCALE), 86 + }, 87 + }; 88 + 89 + static const struct max11205_chip_info max11205_chip_info[] = { 90 + [TYPE_MAX11205A] = { 91 + .out_data_rate = MAX11205A_OUT_DATA_RATE, 92 + .name = "max11205a", 93 + }, 94 + [TYPE_MAX11205B] = { 95 + .out_data_rate = MAX11205B_OUT_DATA_RATE, 96 + .name = "max11205b", 97 + }, 98 + }; 99 + 100 + static void max11205_reg_disable(void *reg) 101 + { 102 + regulator_disable(reg); 103 + } 104 + 105 + static int max11205_probe(struct spi_device *spi) 106 + { 107 + struct max11205_state *st; 108 + struct iio_dev *indio_dev; 109 + int ret; 110 + 111 + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 112 + if (!indio_dev) 113 + return -ENOMEM; 114 + 115 + st = iio_priv(indio_dev); 116 + 117 + ad_sd_init(&st->sd, indio_dev, spi, &max11205_sigma_delta_info); 118 + 119 + st->chip_info = device_get_match_data(&spi->dev); 120 + if (!st->chip_info) 121 + st->chip_info = 122 + (const struct max11205_chip_info *)spi_get_device_id(spi)->driver_data; 123 + 124 + indio_dev->name = st->chip_info->name; 125 + indio_dev->modes = INDIO_DIRECT_MODE; 126 + indio_dev->channels = max11205_channels; 127 + indio_dev->num_channels = 1; 128 + indio_dev->info = &max11205_iio_info; 129 + 130 + st->vref = devm_regulator_get(&spi->dev, "vref"); 131 + if (IS_ERR(st->vref)) 132 + return dev_err_probe(&spi->dev, PTR_ERR(st->vref), 133 + "Failed to get vref regulator\n"); 134 + 135 + ret = regulator_enable(st->vref); 136 + if (ret) 137 + return ret; 138 + 139 + ret = devm_add_action_or_reset(&spi->dev, max11205_reg_disable, st->vref); 140 + if (ret) 141 + return ret; 142 + 143 + ret = devm_ad_sd_setup_buffer_and_trigger(&spi->dev, indio_dev); 144 + if (ret) 145 + return ret; 146 + 147 + return devm_iio_device_register(&spi->dev, indio_dev); 148 + } 149 + 150 + static const struct spi_device_id max11205_spi_ids[] = { 151 + { "max11205a", (kernel_ulong_t)&max11205_chip_info[TYPE_MAX11205A] }, 152 + { "max11205b", (kernel_ulong_t)&max11205_chip_info[TYPE_MAX11205B] }, 153 + { } 154 + }; 155 + MODULE_DEVICE_TABLE(spi, max11205_spi_ids); 156 + 157 + static const struct of_device_id max11205_dt_ids[] = { 158 + { 159 + .compatible = "maxim,max11205a", 160 + .data = &max11205_chip_info[TYPE_MAX11205A], 161 + }, 162 + { 163 + .compatible = "maxim,max11205b", 164 + .data = &max11205_chip_info[TYPE_MAX11205B], 165 + }, 166 + { } 167 + }; 168 + MODULE_DEVICE_TABLE(of, max11205_dt_ids); 169 + 170 + static struct spi_driver max11205_spi_driver = { 171 + .driver = { 172 + .name = "max11205", 173 + .of_match_table = max11205_dt_ids, 174 + }, 175 + .probe = max11205_probe, 176 + .id_table = max11205_spi_ids, 177 + }; 178 + module_spi_driver(max11205_spi_driver); 179 + 180 + MODULE_AUTHOR("Ramona Bolboaca <ramona.bolboaca@analog.com>"); 181 + MODULE_DESCRIPTION("MAX11205 ADC driver"); 182 + MODULE_LICENSE("GPL v2"); 183 + MODULE_IMPORT_NS(IIO_AD_SIGMA_DELTA);