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 struct iio_dev
*indio_dev
;
136 struct max5522_state
*state
;
139 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*state
));
140 if (indio_dev
== NULL
) {
141 dev_err(&spi
->dev
, "failed to allocate iio device\n");
145 state
= iio_priv(indio_dev
);
146 state
->chip_info
= spi_get_device_match_data(spi
);
147 if (!state
->chip_info
)
150 state
->vrefin_reg
= devm_regulator_get(&spi
->dev
, "vrefin");
151 if (IS_ERR(state
->vrefin_reg
))
152 return dev_err_probe(&spi
->dev
, PTR_ERR(state
->vrefin_reg
),
153 "Vrefin regulator not specified\n");
155 ret
= regulator_enable(state
->vrefin_reg
);
157 return dev_err_probe(&spi
->dev
, ret
,
158 "Failed to enable vref regulators\n");
161 state
->regmap
= devm_regmap_init_spi(spi
, &max5522_regmap_config
);
163 if (IS_ERR(state
->regmap
))
164 return PTR_ERR(state
->regmap
);
166 indio_dev
->info
= &max5522_info
;
167 indio_dev
->modes
= INDIO_DIRECT_MODE
;
168 indio_dev
->channels
= max5522_channels
;
169 indio_dev
->num_channels
= ARRAY_SIZE(max5522_channels
);
170 indio_dev
->name
= max5522_chip_info_tbl
[ID_MAX5522
].name
;
172 return devm_iio_device_register(&spi
->dev
, indio_dev
);
175 static const struct spi_device_id max5522_ids
[] = {
176 { "max5522", (kernel_ulong_t
)&max5522_chip_info_tbl
[ID_MAX5522
] },
179 MODULE_DEVICE_TABLE(spi
, max5522_ids
);
181 static const struct of_device_id max5522_of_match
[] = {
183 .compatible
= "maxim,max5522",
184 .data
= &max5522_chip_info_tbl
[ID_MAX5522
],
188 MODULE_DEVICE_TABLE(of
, max5522_of_match
);
190 static struct spi_driver max5522_spi_driver
= {
193 .of_match_table
= max5522_of_match
,
195 .probe
= max5522_spi_probe
,
196 .id_table
= max5522_ids
,
198 module_spi_driver(max5522_spi_driver
);
200 MODULE_AUTHOR("Angelo Dureghello <angelo.dureghello@timesys.com");
201 MODULE_DESCRIPTION("MAX5522 DAC driver");
202 MODULE_LICENSE("GPL");