1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Industrial I/O driver for Microchip digital potentiometers 4 * 5 * Copyright (c) 2018 Chris Coffey <cmc@babblebit.net> 6 * Based on: Slawomir Stepien's code from mcp4131.c 7 * 8 * Datasheet: https://ww1.microchip.com/downloads/en/devicedoc/11195c.pdf 9 * 10 * DEVID #Wipers #Positions Resistance (kOhm) 11 * mcp41010 1 256 10 12 * mcp41050 1 256 50 13 * mcp41100 1 256 100 14 * mcp42010 2 256 10 15 * mcp42050 2 256 50 16 * mcp42100 2 256 100 17 */ 18 19 #include <linux/cache.h> 20 #include <linux/err.h> 21 #include <linux/iio/iio.h> 22 #include <linux/iio/types.h> 23 #include <linux/module.h> 24 #include <linux/mutex.h> 25 #include <linux/property.h> 26 #include <linux/spi/spi.h> 27 28 #define MCP41010_MAX_WIPERS 2 29 #define MCP41010_WRITE BIT(4) 30 #define MCP41010_WIPER_MAX 255 31 #define MCP41010_WIPER_CHANNEL BIT(0) 32 33 struct mcp41010_cfg { 34 char name[16]; 35 int wipers; 36 int kohms; 37 }; 38 39 enum mcp41010_type { 40 MCP41010, 41 MCP41050, 42 MCP41100, 43 MCP42010, 44 MCP42050, 45 MCP42100, 46 }; 47 48 static const struct mcp41010_cfg mcp41010_cfg[] = { 49 [MCP41010] = { .name = "mcp41010", .wipers = 1, .kohms = 10, }, 50 [MCP41050] = { .name = "mcp41050", .wipers = 1, .kohms = 50, }, 51 [MCP41100] = { .name = "mcp41100", .wipers = 1, .kohms = 100, }, 52 [MCP42010] = { .name = "mcp42010", .wipers = 2, .kohms = 10, }, 53 [MCP42050] = { .name = "mcp42050", .wipers = 2, .kohms = 50, }, 54 [MCP42100] = { .name = "mcp42100", .wipers = 2, .kohms = 100, }, 55 }; 56 57 struct mcp41010_data { 58 struct spi_device *spi; 59 const struct mcp41010_cfg *cfg; 60 struct mutex lock; /* Protect write sequences */ 61 unsigned int value[MCP41010_MAX_WIPERS]; /* Cache wiper values */ 62 u8 buf[2] __aligned(IIO_DMA_MINALIGN); 63 }; 64 65 #define MCP41010_CHANNEL(ch) { \ 66 .type = IIO_RESISTANCE, \ 67 .indexed = 1, \ 68 .output = 1, \ 69 .channel = (ch), \ 70 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 71 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 72 } 73 74 static const struct iio_chan_spec mcp41010_channels[] = { 75 MCP41010_CHANNEL(0), 76 MCP41010_CHANNEL(1), 77 }; 78 79 static int mcp41010_read_raw(struct iio_dev *indio_dev, 80 struct iio_chan_spec const *chan, 81 int *val, int *val2, long mask) 82 { 83 struct mcp41010_data *data = iio_priv(indio_dev); 84 int channel = chan->channel; 85 86 switch (mask) { 87 case IIO_CHAN_INFO_RAW: 88 *val = data->value[channel]; 89 return IIO_VAL_INT; 90 91 case IIO_CHAN_INFO_SCALE: 92 *val = 1000 * data->cfg->kohms; 93 *val2 = MCP41010_WIPER_MAX; 94 return IIO_VAL_FRACTIONAL; 95 } 96 97 return -EINVAL; 98 } 99 100 static int mcp41010_write_raw(struct iio_dev *indio_dev, 101 struct iio_chan_spec const *chan, 102 int val, int val2, long mask) 103 { 104 int err; 105 struct mcp41010_data *data = iio_priv(indio_dev); 106 int channel = chan->channel; 107 108 if (mask != IIO_CHAN_INFO_RAW) 109 return -EINVAL; 110 111 if (val > MCP41010_WIPER_MAX || val < 0) 112 return -EINVAL; 113 114 mutex_lock(&data->lock); 115 116 data->buf[0] = MCP41010_WIPER_CHANNEL << channel; 117 data->buf[0] |= MCP41010_WRITE; 118 data->buf[1] = val & 0xff; 119 120 err = spi_write(data->spi, data->buf, sizeof(data->buf)); 121 if (!err) 122 data->value[channel] = val; 123 124 mutex_unlock(&data->lock); 125 126 return err; 127 } 128 129 static const struct iio_info mcp41010_info = { 130 .read_raw = mcp41010_read_raw, 131 .write_raw = mcp41010_write_raw, 132 }; 133 134 static int mcp41010_probe(struct spi_device *spi) 135 { 136 int err; 137 struct device *dev = &spi->dev; 138 struct mcp41010_data *data; 139 struct iio_dev *indio_dev; 140 141 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 142 if (!indio_dev) 143 return -ENOMEM; 144 145 data = iio_priv(indio_dev); 146 spi_set_drvdata(spi, indio_dev); 147 data->spi = spi; 148 data->cfg = device_get_match_data(&spi->dev); 149 if (!data->cfg) 150 data->cfg = &mcp41010_cfg[spi_get_device_id(spi)->driver_data]; 151 152 mutex_init(&data->lock); 153 154 indio_dev->info = &mcp41010_info; 155 indio_dev->channels = mcp41010_channels; 156 indio_dev->num_channels = data->cfg->wipers; 157 indio_dev->name = data->cfg->name; 158 159 err = devm_iio_device_register(dev, indio_dev); 160 if (err) 161 dev_info(&spi->dev, "Unable to register %s\n", indio_dev->name); 162 163 return err; 164 } 165 166 static const struct of_device_id mcp41010_match[] = { 167 { .compatible = "microchip,mcp41010", .data = &mcp41010_cfg[MCP41010] }, 168 { .compatible = "microchip,mcp41050", .data = &mcp41010_cfg[MCP41050] }, 169 { .compatible = "microchip,mcp41100", .data = &mcp41010_cfg[MCP41100] }, 170 { .compatible = "microchip,mcp42010", .data = &mcp41010_cfg[MCP42010] }, 171 { .compatible = "microchip,mcp42050", .data = &mcp41010_cfg[MCP42050] }, 172 { .compatible = "microchip,mcp42100", .data = &mcp41010_cfg[MCP42100] }, 173 { } 174 }; 175 MODULE_DEVICE_TABLE(of, mcp41010_match); 176 177 static const struct spi_device_id mcp41010_id[] = { 178 { .name = "mcp41010", .driver_data = MCP41010 }, 179 { .name = "mcp41050", .driver_data = MCP41050 }, 180 { .name = "mcp41100", .driver_data = MCP41100 }, 181 { .name = "mcp42010", .driver_data = MCP42010 }, 182 { .name = "mcp42050", .driver_data = MCP42050 }, 183 { .name = "mcp42100", .driver_data = MCP42100 }, 184 { } 185 }; 186 MODULE_DEVICE_TABLE(spi, mcp41010_id); 187 188 static struct spi_driver mcp41010_driver = { 189 .driver = { 190 .name = "mcp41010", 191 .of_match_table = mcp41010_match, 192 }, 193 .probe = mcp41010_probe, 194 .id_table = mcp41010_id, 195 }; 196 197 module_spi_driver(mcp41010_driver); 198 199 MODULE_AUTHOR("Chris Coffey <cmc@babblebit.net>"); 200 MODULE_DESCRIPTION("MCP41010 digital potentiometer"); 201 MODULE_LICENSE("GPL v2"); 202