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/regmap.h>
13 #include <linux/regulator/consumer.h>
14 #include <linux/slab.h>
15 #include <linux/spi/spi.h>
16 #include <linux/units.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_state {
28 struct regmap *regmap;
29 unsigned short dac_cache[2];
30 int vref_mV;
31 };
32
33 #define MAX5522_CHANNEL(chan) { \
34 .type = IIO_VOLTAGE, \
35 .indexed = 1, \
36 .output = 1, \
37 .channel = chan, \
38 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
39 BIT(IIO_CHAN_INFO_SCALE), \
40 .scan_type = { \
41 .sign = 'u', \
42 .realbits = 10, \
43 .storagebits = 16, \
44 .shift = 2, \
45 } \
46 }
47
48 static const struct iio_chan_spec max5522_channels[] = {
49 MAX5522_CHANNEL(0),
50 MAX5522_CHANNEL(1),
51 };
52
max5522_info_to_reg(struct iio_chan_spec const * chan)53 static inline int max5522_info_to_reg(struct iio_chan_spec const *chan)
54 {
55 return MAX5522_REG_DATA(chan->channel);
56 }
57
max5522_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long info)58 static int max5522_read_raw(struct iio_dev *indio_dev,
59 struct iio_chan_spec const *chan,
60 int *val, int *val2, long info)
61 {
62 struct max5522_state *state = iio_priv(indio_dev);
63
64 switch (info) {
65 case IIO_CHAN_INFO_RAW:
66 *val = state->dac_cache[chan->channel];
67 return IIO_VAL_INT;
68 case IIO_CHAN_INFO_SCALE:
69 *val = state->vref_mV;
70 *val2 = 10;
71 return IIO_VAL_FRACTIONAL_LOG2;
72 default:
73 return -EINVAL;
74 }
75
76 return -EINVAL;
77 }
78
max5522_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long info)79 static int max5522_write_raw(struct iio_dev *indio_dev,
80 struct iio_chan_spec const *chan,
81 int val, int val2, long info)
82 {
83 struct max5522_state *state = iio_priv(indio_dev);
84 int rval;
85
86 if (val > 1023 || val < 0)
87 return -EINVAL;
88
89 rval = regmap_write(state->regmap, max5522_info_to_reg(chan),
90 val << chan->scan_type.shift);
91 if (rval < 0)
92 return rval;
93
94 state->dac_cache[chan->channel] = val;
95
96 return 0;
97 }
98
99 static const struct iio_info max5522_info = {
100 .read_raw = max5522_read_raw,
101 .write_raw = max5522_write_raw,
102 };
103
104 static const struct regmap_config max5522_regmap_config = {
105 .reg_bits = 4,
106 .val_bits = 12,
107 .max_register = MAX5522_MAX_ADDR,
108 };
109
max5522_spi_probe(struct spi_device * spi)110 static int max5522_spi_probe(struct spi_device *spi)
111 {
112 struct iio_dev *indio_dev;
113 struct max5522_state *state;
114 int ret;
115
116 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
117 if (indio_dev == NULL) {
118 dev_err(&spi->dev, "failed to allocate iio device\n");
119 return -ENOMEM;
120 }
121
122 state = iio_priv(indio_dev);
123
124 ret = devm_regulator_get_enable_read_voltage(&spi->dev, "vrefin");
125 if (ret < 0)
126 return dev_err_probe(&spi->dev, ret,
127 "Failed to get vrefin regulator\n");
128 state->vref_mV = ret / (MICRO / MILLI);
129
130 state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config);
131
132 if (IS_ERR(state->regmap))
133 return PTR_ERR(state->regmap);
134
135 indio_dev->info = &max5522_info;
136 indio_dev->modes = INDIO_DIRECT_MODE;
137 indio_dev->channels = max5522_channels;
138 indio_dev->num_channels = ARRAY_SIZE(max5522_channels);
139 indio_dev->name = "max5522";
140
141 return devm_iio_device_register(&spi->dev, indio_dev);
142 }
143
144 static const struct spi_device_id max5522_ids[] = {
145 { .name = "max5522" },
146 { }
147 };
148 MODULE_DEVICE_TABLE(spi, max5522_ids);
149
150 static const struct of_device_id max5522_of_match[] = {
151 { .compatible = "maxim,max5522" },
152 { }
153 };
154 MODULE_DEVICE_TABLE(of, max5522_of_match);
155
156 static struct spi_driver max5522_spi_driver = {
157 .driver = {
158 .name = "max5522",
159 .of_match_table = max5522_of_match,
160 },
161 .probe = max5522_spi_probe,
162 .id_table = max5522_ids,
163 };
164 module_spi_driver(max5522_spi_driver);
165
166 MODULE_AUTHOR("Angelo Dureghello <angelo.dureghello@timesys.com");
167 MODULE_DESCRIPTION("MAX5522 DAC driver");
168 MODULE_LICENSE("GPL");
169