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_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 int vref_mV; 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 static 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 83 switch (info) { 84 case IIO_CHAN_INFO_RAW: 85 *val = state->dac_cache[chan->channel]; 86 return IIO_VAL_INT; 87 case IIO_CHAN_INFO_SCALE: 88 *val = state->vref_mV; 89 *val2 = 10; 90 return IIO_VAL_FRACTIONAL_LOG2; 91 default: 92 return -EINVAL; 93 } 94 95 return -EINVAL; 96 } 97 98 static int max5522_write_raw(struct iio_dev *indio_dev, 99 struct iio_chan_spec const *chan, 100 int val, int val2, long info) 101 { 102 struct max5522_state *state = iio_priv(indio_dev); 103 int rval; 104 105 if (val > 1023 || val < 0) 106 return -EINVAL; 107 108 rval = regmap_write(state->regmap, max5522_info_to_reg(chan), 109 val << chan->scan_type.shift); 110 if (rval < 0) 111 return rval; 112 113 state->dac_cache[chan->channel] = val; 114 115 return 0; 116 } 117 118 static const struct iio_info max5522_info = { 119 .read_raw = max5522_read_raw, 120 .write_raw = max5522_write_raw, 121 }; 122 123 static const struct regmap_config max5522_regmap_config = { 124 .reg_bits = 4, 125 .val_bits = 12, 126 .max_register = MAX5522_MAX_ADDR, 127 }; 128 129 static int max5522_spi_probe(struct spi_device *spi) 130 { 131 struct iio_dev *indio_dev; 132 struct max5522_state *state; 133 int ret; 134 135 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state)); 136 if (indio_dev == NULL) { 137 dev_err(&spi->dev, "failed to allocate iio device\n"); 138 return -ENOMEM; 139 } 140 141 state = iio_priv(indio_dev); 142 state->chip_info = spi_get_device_match_data(spi); 143 if (!state->chip_info) 144 return -EINVAL; 145 146 ret = devm_regulator_get_enable_read_voltage(&spi->dev, "vrefin"); 147 if (ret < 0) 148 return dev_err_probe(&spi->dev, ret, 149 "Failed to get vrefin regulator\n"); 150 state->vref_mV = ret / (MICRO / MILLI); 151 152 state->regmap = devm_regmap_init_spi(spi, &max5522_regmap_config); 153 154 if (IS_ERR(state->regmap)) 155 return PTR_ERR(state->regmap); 156 157 indio_dev->info = &max5522_info; 158 indio_dev->modes = INDIO_DIRECT_MODE; 159 indio_dev->channels = max5522_channels; 160 indio_dev->num_channels = ARRAY_SIZE(max5522_channels); 161 indio_dev->name = max5522_chip_info_tbl[ID_MAX5522].name; 162 163 return devm_iio_device_register(&spi->dev, indio_dev); 164 } 165 166 static const struct spi_device_id max5522_ids[] = { 167 { "max5522", (kernel_ulong_t)&max5522_chip_info_tbl[ID_MAX5522] }, 168 { } 169 }; 170 MODULE_DEVICE_TABLE(spi, max5522_ids); 171 172 static const struct of_device_id max5522_of_match[] = { 173 { 174 .compatible = "maxim,max5522", 175 .data = &max5522_chip_info_tbl[ID_MAX5522], 176 }, 177 { } 178 }; 179 MODULE_DEVICE_TABLE(of, max5522_of_match); 180 181 static struct spi_driver max5522_spi_driver = { 182 .driver = { 183 .name = "max5522", 184 .of_match_table = max5522_of_match, 185 }, 186 .probe = max5522_spi_probe, 187 .id_table = max5522_ids, 188 }; 189 module_spi_driver(max5522_spi_driver); 190 191 MODULE_AUTHOR("Angelo Dureghello <angelo.dureghello@timesys.com"); 192 MODULE_DESCRIPTION("MAX5522 DAC driver"); 193 MODULE_LICENSE("GPL"); 194