1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * max5487.c - Support for MAX5487, MAX5488, MAX5489 digital potentiometers 4 * 5 * Copyright (C) 2016 Cristina-Gabriela Moraru <cristina.moraru09@gmail.com> 6 */ 7 #include <linux/module.h> 8 #include <linux/spi/spi.h> 9 10 #include <linux/iio/sysfs.h> 11 #include <linux/iio/iio.h> 12 13 #define MAX5487_WRITE_WIPER_A (0x01 << 8) 14 #define MAX5487_WRITE_WIPER_B (0x02 << 8) 15 16 /* copy both wiper regs to NV regs */ 17 #define MAX5487_COPY_AB_TO_NV (0x23 << 8) 18 /* copy both NV regs to wiper regs */ 19 #define MAX5487_COPY_NV_TO_AB (0x33 << 8) 20 21 #define MAX5487_MAX_POS 255 22 23 struct max5487_data { 24 struct spi_device *spi; 25 int kohms; 26 }; 27 28 #define MAX5487_CHANNEL(ch, addr) { \ 29 .type = IIO_RESISTANCE, \ 30 .indexed = 1, \ 31 .output = 1, \ 32 .channel = ch, \ 33 .address = addr, \ 34 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 35 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 36 } 37 38 static const struct iio_chan_spec max5487_channels[] = { 39 MAX5487_CHANNEL(0, MAX5487_WRITE_WIPER_A), 40 MAX5487_CHANNEL(1, MAX5487_WRITE_WIPER_B), 41 }; 42 43 static int max5487_write_cmd(struct spi_device *spi, u16 cmd) 44 { 45 return spi_write(spi, (const void *) &cmd, sizeof(u16)); 46 } 47 48 static int max5487_read_raw(struct iio_dev *indio_dev, 49 struct iio_chan_spec const *chan, 50 int *val, int *val2, long mask) 51 { 52 struct max5487_data *data = iio_priv(indio_dev); 53 54 if (mask != IIO_CHAN_INFO_SCALE) 55 return -EINVAL; 56 57 *val = 1000 * data->kohms; 58 *val2 = MAX5487_MAX_POS; 59 60 return IIO_VAL_FRACTIONAL; 61 } 62 63 static int max5487_write_raw(struct iio_dev *indio_dev, 64 struct iio_chan_spec const *chan, 65 int val, int val2, long mask) 66 { 67 struct max5487_data *data = iio_priv(indio_dev); 68 69 if (mask != IIO_CHAN_INFO_RAW) 70 return -EINVAL; 71 72 if (val < 0 || val > MAX5487_MAX_POS) 73 return -EINVAL; 74 75 return max5487_write_cmd(data->spi, chan->address | val); 76 } 77 78 static const struct iio_info max5487_info = { 79 .read_raw = max5487_read_raw, 80 .write_raw = max5487_write_raw, 81 }; 82 83 static int max5487_spi_probe(struct spi_device *spi) 84 { 85 struct iio_dev *indio_dev; 86 struct max5487_data *data; 87 const struct spi_device_id *id = spi_get_device_id(spi); 88 int ret; 89 90 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data)); 91 if (!indio_dev) 92 return -ENOMEM; 93 94 spi_set_drvdata(spi, indio_dev); 95 data = iio_priv(indio_dev); 96 97 data->spi = spi; 98 data->kohms = id->driver_data; 99 100 indio_dev->info = &max5487_info; 101 indio_dev->name = id->name; 102 indio_dev->modes = INDIO_DIRECT_MODE; 103 indio_dev->channels = max5487_channels; 104 indio_dev->num_channels = ARRAY_SIZE(max5487_channels); 105 106 /* restore both wiper regs from NV regs */ 107 ret = max5487_write_cmd(data->spi, MAX5487_COPY_NV_TO_AB); 108 if (ret < 0) 109 return ret; 110 111 return iio_device_register(indio_dev); 112 } 113 114 static void max5487_spi_remove(struct spi_device *spi) 115 { 116 struct iio_dev *indio_dev = spi_get_drvdata(spi); 117 int ret; 118 119 iio_device_unregister(indio_dev); 120 121 /* save both wiper regs to NV regs */ 122 ret = max5487_write_cmd(spi, MAX5487_COPY_AB_TO_NV); 123 if (ret) 124 dev_warn(&spi->dev, "Failed to save wiper regs to NV regs\n"); 125 } 126 127 static const struct spi_device_id max5487_id[] = { 128 { .name = "MAX5487", .driver_data = 10 }, 129 { .name = "MAX5488", .driver_data = 50 }, 130 { .name = "MAX5489", .driver_data = 100 }, 131 { } 132 }; 133 MODULE_DEVICE_TABLE(spi, max5487_id); 134 135 static const struct acpi_device_id max5487_acpi_match[] = { 136 { "MAX5487", 10 }, 137 { "MAX5488", 50 }, 138 { "MAX5489", 100 }, 139 { } 140 }; 141 MODULE_DEVICE_TABLE(acpi, max5487_acpi_match); 142 143 static struct spi_driver max5487_driver = { 144 .driver = { 145 .name = "max5487", 146 .acpi_match_table = max5487_acpi_match, 147 }, 148 .id_table = max5487_id, 149 .probe = max5487_spi_probe, 150 .remove = max5487_spi_remove 151 }; 152 module_spi_driver(max5487_driver); 153 154 MODULE_AUTHOR("Cristina-Gabriela Moraru <cristina.moraru09@gmail.com>"); 155 MODULE_DESCRIPTION("max5487 SPI driver"); 156 MODULE_LICENSE("GPL v2"); 157