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

Configure Feed

Select the types of activity you want to include in your feed.

iio: dac: add support for max5522

Add initial support for DAC max5522.

Tested writing DAC A and B with some values,
from 0 to 1023, measured output voltages, driver works properly.

Additions for max5523/24/25 will follow.

Signed-off-by: Angelo Dureghello <angelo.dureghello@timesys.com>
Link: https://lore.kernel.org/r/20221106165928.223318-1-angelo.dureghello@timesys.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Angelo Dureghello and committed by
Jonathan Cameron
ba56b46d f2edf0c8

+221
+13
drivers/iio/dac/Kconfig
··· 357 357 This driver can also be built as a module. If so, the module 358 358 will be called max517. 359 359 360 + config MAX5522 361 + tristate "Maxim MAX5522 DAC driver" 362 + depends on SPI_MASTER 363 + select REGMAP_SPI 364 + help 365 + Say Y here if you want to build a driver for the Maxim MAX5522. 366 + 367 + MAX5522 is a dual, ultra-low-power, 10-Bit, voltage-output 368 + digital to analog converter (DAC) offering rail-to-rail buffered 369 + voltage outputs. 370 + 371 + If compiled as a module, it will be called max5522. 372 + 360 373 config MAX5821 361 374 tristate "Maxim MAX5821 DAC driver" 362 375 depends on I2C
+1
drivers/iio/dac/Makefile
··· 38 38 obj-$(CONFIG_LTC2688) += ltc2688.o 39 39 obj-$(CONFIG_M62332) += m62332.o 40 40 obj-$(CONFIG_MAX517) += max517.o 41 + obj-$(CONFIG_MAX5522) += max5522.o 41 42 obj-$(CONFIG_MAX5821) += max5821.o 42 43 obj-$(CONFIG_MCP4725) += mcp4725.o 43 44 obj-$(CONFIG_MCP4922) += mcp4922.o
+207
drivers/iio/dac/max5522.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Maxim MAX5522 4 + * Dual, Ultra-Low-Power 10-Bit, Voltage-Output DACs 5 + * 6 + * Copyright 2022 Timesys Corp. 7 + */ 8 + 9 + #include <linux/device.h> 10 + #include <linux/kernel.h> 11 + #include <linux/module.h> 12 + #include <linux/mod_devicetable.h> 13 + #include <linux/regmap.h> 14 + #include <linux/regulator/consumer.h> 15 + #include <linux/slab.h> 16 + #include <linux/spi/spi.h> 17 + 18 + #include <linux/iio/iio.h> 19 + 20 + #define MAX5522_MAX_ADDR 15 21 + #define MAX5522_CTRL_NONE 0 22 + #define MAX5522_CTRL_LOAD_IN_A 9 23 + #define MAX5522_CTRL_LOAD_IN_B 10 24 + 25 + #define MAX5522_REG_DATA(x) ((x) + MAX5522_CTRL_LOAD_IN_A) 26 + 27 + struct max5522_chip_info { 28 + const char *name; 29 + const struct iio_chan_spec *channels; 30 + unsigned int num_channels; 31 + }; 32 + 33 + struct max5522_state { 34 + struct regmap *regmap; 35 + const struct max5522_chip_info *chip_info; 36 + unsigned short dac_cache[2]; 37 + struct regulator *vrefin_reg; 38 + }; 39 + 40 + #define MAX5522_CHANNEL(chan) { \ 41 + .type = IIO_VOLTAGE, \ 42 + .indexed = 1, \ 43 + .output = 1, \ 44 + .channel = chan, \ 45 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 46 + BIT(IIO_CHAN_INFO_SCALE), \ 47 + .scan_type = { \ 48 + .sign = 'u', \ 49 + .realbits = 10, \ 50 + .storagebits = 16, \ 51 + .shift = 2, \ 52 + } \ 53 + } 54 + 55 + const struct iio_chan_spec max5522_channels[] = { 56 + MAX5522_CHANNEL(0), 57 + MAX5522_CHANNEL(1), 58 + }; 59 + 60 + enum max5522_type { 61 + ID_MAX5522, 62 + }; 63 + 64 + static const struct max5522_chip_info max5522_chip_info_tbl[] = { 65 + [ID_MAX5522] = { 66 + .name = "max5522", 67 + .channels = max5522_channels, 68 + .num_channels = 2, 69 + }, 70 + }; 71 + 72 + static inline int max5522_info_to_reg(struct iio_chan_spec const *chan) 73 + { 74 + return MAX5522_REG_DATA(chan->channel); 75 + } 76 + 77 + static int max5522_read_raw(struct iio_dev *indio_dev, 78 + struct iio_chan_spec const *chan, 79 + int *val, int *val2, long info) 80 + { 81 + struct max5522_state *state = iio_priv(indio_dev); 82 + int ret; 83 + 84 + switch (info) { 85 + case IIO_CHAN_INFO_RAW: 86 + *val = state->dac_cache[chan->channel]; 87 + return IIO_VAL_INT; 88 + case IIO_CHAN_INFO_SCALE: 89 + ret = regulator_get_voltage(state->vrefin_reg); 90 + if (ret < 0) 91 + return -EINVAL; 92 + *val = ret / 1000; 93 + *val2 = 10; 94 + return IIO_VAL_FRACTIONAL_LOG2; 95 + default: 96 + return -EINVAL; 97 + } 98 + 99 + return -EINVAL; 100 + } 101 + 102 + static int max5522_write_raw(struct iio_dev *indio_dev, 103 + struct iio_chan_spec const *chan, 104 + int val, int val2, long info) 105 + { 106 + struct max5522_state *state = iio_priv(indio_dev); 107 + int rval; 108 + 109 + if (val > 1023 || val < 0) 110 + return -EINVAL; 111 + 112 + rval = regmap_write(state->regmap, max5522_info_to_reg(chan), 113 + val << chan->scan_type.shift); 114 + if (rval < 0) 115 + return rval; 116 + 117 + state->dac_cache[chan->channel] = val; 118 + 119 + return 0; 120 + } 121 + 122 + static const struct iio_info max5522_info = { 123 + .read_raw = max5522_read_raw, 124 + .write_raw = max5522_write_raw, 125 + }; 126 + 127 + static const struct regmap_config max5522_regmap_config = { 128 + .reg_bits = 4, 129 + .val_bits = 12, 130 + .max_register = MAX5522_MAX_ADDR, 131 + }; 132 + 133 + static int max5522_spi_probe(struct spi_device *spi) 134 + { 135 + const struct spi_device_id *id = spi_get_device_id(spi); 136 + struct iio_dev *indio_dev; 137 + struct max5522_state *state; 138 + int ret; 139 + 140 + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state)); 141 + if (indio_dev == NULL) { 142 + dev_err(&spi->dev, "failed to allocate iio device\n"); 143 + return -ENOMEM; 144 + } 145 + 146 + state = iio_priv(indio_dev); 147 + state->chip_info = device_get_match_data(&spi->dev); 148 + if (!state->chip_info) { 149 + state->chip_info = 150 + (struct max5522_chip_info *)(id->driver_data); 151 + if (!state->chip_info) 152 + return -EINVAL; 153 + } 154 + 155 + state->vrefin_reg = devm_regulator_get(&spi->dev, "vrefin"); 156 + if (IS_ERR(state->vrefin_reg)) 157 + return dev_err_probe(&spi->dev, PTR_ERR(state->vrefin_reg), 158 + "Vrefin regulator not specified\n"); 159 + 160 + ret = regulator_enable(state->vrefin_reg); 161 + if (ret) { 162 + return dev_err_probe(&spi->dev, ret, 163 + "Failed to enable vref regulators\n"); 164 + } 165 + 166 + state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config); 167 + 168 + if (IS_ERR(state->regmap)) 169 + return PTR_ERR(state->regmap); 170 + 171 + indio_dev->info = &max5522_info; 172 + indio_dev->modes = INDIO_DIRECT_MODE; 173 + indio_dev->channels = max5522_channels; 174 + indio_dev->num_channels = ARRAY_SIZE(max5522_channels); 175 + indio_dev->name = max5522_chip_info_tbl[ID_MAX5522].name; 176 + 177 + return devm_iio_device_register(&spi->dev, indio_dev); 178 + } 179 + 180 + static const struct spi_device_id max5522_ids[] = { 181 + { "max5522", (kernel_ulong_t)&max5522_chip_info_tbl[ID_MAX5522] }, 182 + {} 183 + }; 184 + MODULE_DEVICE_TABLE(spi, max5522_ids); 185 + 186 + static const struct of_device_id max5522_of_match[] = { 187 + { 188 + .compatible = "maxim,max5522", 189 + .data = &max5522_chip_info_tbl[ID_MAX5522], 190 + }, 191 + {} 192 + }; 193 + MODULE_DEVICE_TABLE(of, max5522_of_match); 194 + 195 + static struct spi_driver max5522_spi_driver = { 196 + .driver = { 197 + .name = "max5522", 198 + .of_match_table = max5522_of_match, 199 + }, 200 + .probe = max5522_spi_probe, 201 + .id_table = max5522_ids, 202 + }; 203 + module_spi_driver(max5522_spi_driver); 204 + 205 + MODULE_AUTHOR("Angelo Dureghello <angelo.dureghello@timesys.com"); 206 + MODULE_DESCRIPTION("MAX5522 DAC driver"); 207 + MODULE_LICENSE("GPL");