1 // SPDX-License-Identifier: GPL-2.0-only
4 * Dual, Ultra-Low-Power 10-Bit, Voltage-Output DACs
6 * Copyright 2022 Timesys Corp.
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>
18 #include <linux/iio/iio.h>
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
25 #define MAX5522_REG_DATA(x) ((x) + MAX5522_CTRL_LOAD_IN_A)
27 struct max5522_chip_info {
29 const struct iio_chan_spec *channels;
30 unsigned int num_channels;
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;
40 #define MAX5522_CHANNEL(chan) { \
41 .type = IIO_VOLTAGE, \
45 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
46 BIT(IIO_CHAN_INFO_SCALE), \
55 static const struct iio_chan_spec max5522_channels[] = {
64 static const struct max5522_chip_info max5522_chip_info_tbl[] = {
67 .channels = max5522_channels,
72 static inline int max5522_info_to_reg(struct iio_chan_spec const *chan)
74 return MAX5522_REG_DATA(chan->channel);
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)
81 struct max5522_state *state = iio_priv(indio_dev);
85 case IIO_CHAN_INFO_RAW:
86 *val = state->dac_cache[chan->channel];
88 case IIO_CHAN_INFO_SCALE:
89 ret = regulator_get_voltage(state->vrefin_reg);
94 return IIO_VAL_FRACTIONAL_LOG2;
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)
106 struct max5522_state *state = iio_priv(indio_dev);
109 if (val > 1023 || val < 0)
112 rval = regmap_write(state->regmap, max5522_info_to_reg(chan),
113 val << chan->scan_type.shift);
117 state->dac_cache[chan->channel] = val;
122 static const struct iio_info max5522_info = {
123 .read_raw = max5522_read_raw,
124 .write_raw = max5522_write_raw,
127 static const struct regmap_config max5522_regmap_config = {
130 .max_register = MAX5522_MAX_ADDR,
133 static int max5522_spi_probe(struct spi_device *spi)
135 const struct spi_device_id *id = spi_get_device_id(spi);
136 struct iio_dev *indio_dev;
137 struct max5522_state *state;
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");
146 state = iio_priv(indio_dev);
147 state->chip_info = device_get_match_data(&spi->dev);
148 if (!state->chip_info) {
150 (struct max5522_chip_info *)(id->driver_data);
151 if (!state->chip_info)
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");
160 ret = regulator_enable(state->vrefin_reg);
162 return dev_err_probe(&spi->dev, ret,
163 "Failed to enable vref regulators\n");
166 state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config);
168 if (IS_ERR(state->regmap))
169 return PTR_ERR(state->regmap);
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;
177 return devm_iio_device_register(&spi->dev, indio_dev);
180 static const struct spi_device_id max5522_ids[] = {
181 { "max5522", (kernel_ulong_t)&max5522_chip_info_tbl[ID_MAX5522] },
184 MODULE_DEVICE_TABLE(spi, max5522_ids);
186 static const struct of_device_id max5522_of_match[] = {
188 .compatible = "maxim,max5522",
189 .data = &max5522_chip_info_tbl[ID_MAX5522],
193 MODULE_DEVICE_TABLE(of, max5522_of_match);
195 static struct spi_driver max5522_spi_driver = {
198 .of_match_table = max5522_of_match,
200 .probe = max5522_spi_probe,
201 .id_table = max5522_ids,
203 module_spi_driver(max5522_spi_driver);
206 MODULE_DESCRIPTION("MAX5522 DAC driver");
207 MODULE_LICENSE("GPL");