Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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#include <linux/units.h>
18
19#include <linux/iio/iio.h>
20
21#define MAX5522_MAX_ADDR 15
22#define MAX5522_CTRL_NONE 0
23#define MAX5522_CTRL_LOAD_IN_A 9
24#define MAX5522_CTRL_LOAD_IN_B 10
25
26#define MAX5522_REG_DATA(x) ((x) + MAX5522_CTRL_LOAD_IN_A)
27
28struct max5522_chip_info {
29 const char *name;
30 const struct iio_chan_spec *channels;
31 unsigned int num_channels;
32};
33
34struct max5522_state {
35 struct regmap *regmap;
36 const struct max5522_chip_info *chip_info;
37 unsigned short dac_cache[2];
38 int vref_mV;
39};
40
41#define MAX5522_CHANNEL(chan) { \
42 .type = IIO_VOLTAGE, \
43 .indexed = 1, \
44 .output = 1, \
45 .channel = chan, \
46 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
47 BIT(IIO_CHAN_INFO_SCALE), \
48 .scan_type = { \
49 .sign = 'u', \
50 .realbits = 10, \
51 .storagebits = 16, \
52 .shift = 2, \
53 } \
54}
55
56static const struct iio_chan_spec max5522_channels[] = {
57 MAX5522_CHANNEL(0),
58 MAX5522_CHANNEL(1),
59};
60
61enum max5522_type {
62 ID_MAX5522,
63};
64
65static const struct max5522_chip_info max5522_chip_info_tbl[] = {
66 [ID_MAX5522] = {
67 .name = "max5522",
68 .channels = max5522_channels,
69 .num_channels = 2,
70 },
71};
72
73static inline int max5522_info_to_reg(struct iio_chan_spec const *chan)
74{
75 return MAX5522_REG_DATA(chan->channel);
76}
77
78static int max5522_read_raw(struct iio_dev *indio_dev,
79 struct iio_chan_spec const *chan,
80 int *val, int *val2, long info)
81{
82 struct max5522_state *state = iio_priv(indio_dev);
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 *val = state->vref_mV;
90 *val2 = 10;
91 return IIO_VAL_FRACTIONAL_LOG2;
92 default:
93 return -EINVAL;
94 }
95
96 return -EINVAL;
97}
98
99static int max5522_write_raw(struct iio_dev *indio_dev,
100 struct iio_chan_spec const *chan,
101 int val, int val2, long info)
102{
103 struct max5522_state *state = iio_priv(indio_dev);
104 int rval;
105
106 if (val > 1023 || val < 0)
107 return -EINVAL;
108
109 rval = regmap_write(state->regmap, max5522_info_to_reg(chan),
110 val << chan->scan_type.shift);
111 if (rval < 0)
112 return rval;
113
114 state->dac_cache[chan->channel] = val;
115
116 return 0;
117}
118
119static const struct iio_info max5522_info = {
120 .read_raw = max5522_read_raw,
121 .write_raw = max5522_write_raw,
122};
123
124static const struct regmap_config max5522_regmap_config = {
125 .reg_bits = 4,
126 .val_bits = 12,
127 .max_register = MAX5522_MAX_ADDR,
128};
129
130static int max5522_spi_probe(struct spi_device *spi)
131{
132 struct iio_dev *indio_dev;
133 struct max5522_state *state;
134 int ret;
135
136 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
137 if (indio_dev == NULL) {
138 dev_err(&spi->dev, "failed to allocate iio device\n");
139 return -ENOMEM;
140 }
141
142 state = iio_priv(indio_dev);
143 state->chip_info = spi_get_device_match_data(spi);
144 if (!state->chip_info)
145 return -EINVAL;
146
147 ret = devm_regulator_get_enable_read_voltage(&spi->dev, "vrefin");
148 if (ret < 0)
149 return dev_err_probe(&spi->dev, ret,
150 "Failed to get vrefin regulator\n");
151 state->vref_mV = ret / (MICRO / MILLI);
152
153 state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config);
154
155 if (IS_ERR(state->regmap))
156 return PTR_ERR(state->regmap);
157
158 indio_dev->info = &max5522_info;
159 indio_dev->modes = INDIO_DIRECT_MODE;
160 indio_dev->channels = max5522_channels;
161 indio_dev->num_channels = ARRAY_SIZE(max5522_channels);
162 indio_dev->name = max5522_chip_info_tbl[ID_MAX5522].name;
163
164 return devm_iio_device_register(&spi->dev, indio_dev);
165}
166
167static const struct spi_device_id max5522_ids[] = {
168 { "max5522", (kernel_ulong_t)&max5522_chip_info_tbl[ID_MAX5522] },
169 { }
170};
171MODULE_DEVICE_TABLE(spi, max5522_ids);
172
173static const struct of_device_id max5522_of_match[] = {
174 {
175 .compatible = "maxim,max5522",
176 .data = &max5522_chip_info_tbl[ID_MAX5522],
177 },
178 { }
179};
180MODULE_DEVICE_TABLE(of, max5522_of_match);
181
182static struct spi_driver max5522_spi_driver = {
183 .driver = {
184 .name = "max5522",
185 .of_match_table = max5522_of_match,
186 },
187 .probe = max5522_spi_probe,
188 .id_table = max5522_ids,
189};
190module_spi_driver(max5522_spi_driver);
191
192MODULE_AUTHOR("Angelo Dureghello <angelo.dureghello@timesys.com");
193MODULE_DESCRIPTION("MAX5522 DAC driver");
194MODULE_LICENSE("GPL");