1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Maxim Integrated MAX5481-MAX5484 digital potentiometer driver 4 * Copyright 2016 Rockwell Collins 5 * 6 * Datasheet: 7 * https://datasheets.maximintegrated.com/en/ds/MAX5481-MAX5484.pdf 8 */ 9 10 #include <linux/iio/iio.h> 11 #include <linux/iio/sysfs.h> 12 #include <linux/module.h> 13 #include <linux/property.h> 14 #include <linux/spi/spi.h> 15 16 /* write wiper reg */ 17 #define MAX5481_WRITE_WIPER (0 << 4) 18 /* copy wiper reg to NV reg */ 19 #define MAX5481_COPY_AB_TO_NV (2 << 4) 20 /* copy NV reg to wiper reg */ 21 #define MAX5481_COPY_NV_TO_AB (3 << 4) 22 23 #define MAX5481_MAX_POS 1023 24 25 enum max5481_variant { 26 max5481, 27 max5482, 28 max5483, 29 max5484, 30 }; 31 32 struct max5481_cfg { 33 int kohms; 34 }; 35 36 static const struct max5481_cfg max5481_cfg[] = { 37 [max5481] = { .kohms = 10, }, 38 [max5482] = { .kohms = 50, }, 39 [max5483] = { .kohms = 10, }, 40 [max5484] = { .kohms = 50, }, 41 }; 42 43 struct max5481_data { 44 struct spi_device *spi; 45 const struct max5481_cfg *cfg; 46 u8 msg[3] __aligned(IIO_DMA_MINALIGN); 47 }; 48 49 #define MAX5481_CHANNEL { \ 50 .type = IIO_RESISTANCE, \ 51 .indexed = 1, \ 52 .output = 1, \ 53 .channel = 0, \ 54 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 55 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 56 } 57 58 static const struct iio_chan_spec max5481_channels[] = { 59 MAX5481_CHANNEL, 60 }; 61 62 static int max5481_write_cmd(struct max5481_data *data, u8 cmd, u16 val) 63 { 64 struct spi_device *spi = data->spi; 65 66 data->msg[0] = cmd; 67 68 switch (cmd) { 69 case MAX5481_WRITE_WIPER: 70 data->msg[1] = val >> 2; 71 data->msg[2] = (val & 0x3) << 6; 72 return spi_write(spi, data->msg, 3); 73 74 case MAX5481_COPY_AB_TO_NV: 75 case MAX5481_COPY_NV_TO_AB: 76 return spi_write(spi, data->msg, 1); 77 78 default: 79 return -EIO; 80 } 81 } 82 83 static int max5481_read_raw(struct iio_dev *indio_dev, 84 struct iio_chan_spec const *chan, 85 int *val, int *val2, long mask) 86 { 87 struct max5481_data *data = iio_priv(indio_dev); 88 89 if (mask != IIO_CHAN_INFO_SCALE) 90 return -EINVAL; 91 92 *val = 1000 * data->cfg->kohms; 93 *val2 = MAX5481_MAX_POS; 94 95 return IIO_VAL_FRACTIONAL; 96 } 97 98 static int max5481_write_raw(struct iio_dev *indio_dev, 99 struct iio_chan_spec const *chan, 100 int val, int val2, long mask) 101 { 102 struct max5481_data *data = iio_priv(indio_dev); 103 104 if (mask != IIO_CHAN_INFO_RAW) 105 return -EINVAL; 106 107 if (val < 0 || val > MAX5481_MAX_POS) 108 return -EINVAL; 109 110 return max5481_write_cmd(data, MAX5481_WRITE_WIPER, val); 111 } 112 113 static const struct iio_info max5481_info = { 114 .read_raw = max5481_read_raw, 115 .write_raw = max5481_write_raw, 116 }; 117 118 static const struct of_device_id max5481_match[] = { 119 { .compatible = "maxim,max5481", .data = &max5481_cfg[max5481] }, 120 { .compatible = "maxim,max5482", .data = &max5481_cfg[max5482] }, 121 { .compatible = "maxim,max5483", .data = &max5481_cfg[max5483] }, 122 { .compatible = "maxim,max5484", .data = &max5481_cfg[max5484] }, 123 { } 124 }; 125 MODULE_DEVICE_TABLE(of, max5481_match); 126 127 static void max5481_wiper_save(void *data) 128 { 129 max5481_write_cmd(data, MAX5481_COPY_AB_TO_NV, 0); 130 } 131 132 static int max5481_probe(struct spi_device *spi) 133 { 134 struct iio_dev *indio_dev; 135 struct max5481_data *data; 136 const struct spi_device_id *id = spi_get_device_id(spi); 137 int ret; 138 139 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data)); 140 if (!indio_dev) 141 return -ENOMEM; 142 143 data = iio_priv(indio_dev); 144 145 data->spi = spi; 146 147 data->cfg = device_get_match_data(&spi->dev); 148 if (!data->cfg) 149 data->cfg = &max5481_cfg[id->driver_data]; 150 151 indio_dev->name = id->name; 152 indio_dev->modes = INDIO_DIRECT_MODE; 153 154 /* variant specific configuration */ 155 indio_dev->info = &max5481_info; 156 indio_dev->channels = max5481_channels; 157 indio_dev->num_channels = ARRAY_SIZE(max5481_channels); 158 159 /* restore wiper from NV */ 160 ret = max5481_write_cmd(data, MAX5481_COPY_NV_TO_AB, 0); 161 if (ret < 0) 162 return ret; 163 164 ret = devm_add_action(&spi->dev, max5481_wiper_save, data); 165 if (ret < 0) 166 return ret; 167 168 return devm_iio_device_register(&spi->dev, indio_dev); 169 } 170 171 static const struct spi_device_id max5481_id_table[] = { 172 { .name = "max5481", .driver_data = max5481 }, 173 { .name = "max5482", .driver_data = max5482 }, 174 { .name = "max5483", .driver_data = max5483 }, 175 { .name = "max5484", .driver_data = max5484 }, 176 { } 177 }; 178 MODULE_DEVICE_TABLE(spi, max5481_id_table); 179 180 static struct spi_driver max5481_driver = { 181 .driver = { 182 .name = "max5481", 183 .of_match_table = max5481_match, 184 }, 185 .probe = max5481_probe, 186 .id_table = max5481_id_table, 187 }; 188 189 module_spi_driver(max5481_driver); 190 191 MODULE_AUTHOR("Maury Anderson <maury.anderson@rockwellcollins.com>"); 192 MODULE_DESCRIPTION("max5481 SPI driver"); 193 MODULE_LICENSE("GPL v2"); 194