1 // SPDX-License-Identifier: GPL-2.0-only
3 * Maxim Integrated MAX5481-MAX5484 digital potentiometer driver
4 * Copyright 2016 Rockwell Collins
7 * https://datasheets.maximintegrated.com/en/ds/MAX5481-MAX5484.pdf
10 #include <linux/iio/iio.h>
11 #include <linux/iio/sysfs.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/property.h>
15 #include <linux/spi/spi.h>
18 #define MAX5481_WRITE_WIPER (0 << 4)
19 /* copy wiper reg to NV reg */
20 #define MAX5481_COPY_AB_TO_NV (2 << 4)
21 /* copy NV reg to wiper reg */
22 #define MAX5481_COPY_NV_TO_AB (3 << 4)
24 #define MAX5481_MAX_POS 1023
26 enum max5481_variant
{
37 static const struct max5481_cfg max5481_cfg
[] = {
38 [max5481
] = { .kohms
= 10, },
39 [max5482
] = { .kohms
= 50, },
40 [max5483
] = { .kohms
= 10, },
41 [max5484
] = { .kohms
= 50, },
45 struct spi_device
*spi
;
46 const struct max5481_cfg
*cfg
;
47 u8 msg
[3] ____cacheline_aligned
;
50 #define MAX5481_CHANNEL { \
51 .type = IIO_RESISTANCE, \
55 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
56 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
59 static const struct iio_chan_spec max5481_channels
[] = {
63 static int max5481_write_cmd(struct max5481_data
*data
, u8 cmd
, u16 val
)
65 struct spi_device
*spi
= data
->spi
;
70 case MAX5481_WRITE_WIPER
:
71 data
->msg
[1] = val
>> 2;
72 data
->msg
[2] = (val
& 0x3) << 6;
73 return spi_write(spi
, data
->msg
, 3);
75 case MAX5481_COPY_AB_TO_NV
:
76 case MAX5481_COPY_NV_TO_AB
:
77 return spi_write(spi
, data
->msg
, 1);
84 static int max5481_read_raw(struct iio_dev
*indio_dev
,
85 struct iio_chan_spec
const *chan
,
86 int *val
, int *val2
, long mask
)
88 struct max5481_data
*data
= iio_priv(indio_dev
);
90 if (mask
!= IIO_CHAN_INFO_SCALE
)
93 *val
= 1000 * data
->cfg
->kohms
;
94 *val2
= MAX5481_MAX_POS
;
96 return IIO_VAL_FRACTIONAL
;
99 static int max5481_write_raw(struct iio_dev
*indio_dev
,
100 struct iio_chan_spec
const *chan
,
101 int val
, int val2
, long mask
)
103 struct max5481_data
*data
= iio_priv(indio_dev
);
105 if (mask
!= IIO_CHAN_INFO_RAW
)
108 if (val
< 0 || val
> MAX5481_MAX_POS
)
111 return max5481_write_cmd(data
, MAX5481_WRITE_WIPER
, val
);
114 static const struct iio_info max5481_info
= {
115 .read_raw
= max5481_read_raw
,
116 .write_raw
= max5481_write_raw
,
119 static const struct of_device_id max5481_match
[] = {
120 { .compatible
= "maxim,max5481", .data
= &max5481_cfg
[max5481
] },
121 { .compatible
= "maxim,max5482", .data
= &max5481_cfg
[max5482
] },
122 { .compatible
= "maxim,max5483", .data
= &max5481_cfg
[max5483
] },
123 { .compatible
= "maxim,max5484", .data
= &max5481_cfg
[max5484
] },
126 MODULE_DEVICE_TABLE(of
, max5481_match
);
128 static int max5481_probe(struct spi_device
*spi
)
130 struct iio_dev
*indio_dev
;
131 struct max5481_data
*data
;
132 const struct spi_device_id
*id
= spi_get_device_id(spi
);
135 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*data
));
139 dev_set_drvdata(&spi
->dev
, indio_dev
);
140 data
= iio_priv(indio_dev
);
144 data
->cfg
= device_get_match_data(&spi
->dev
);
146 data
->cfg
= &max5481_cfg
[id
->driver_data
];
148 indio_dev
->name
= id
->name
;
149 indio_dev
->modes
= INDIO_DIRECT_MODE
;
151 /* variant specific configuration */
152 indio_dev
->info
= &max5481_info
;
153 indio_dev
->channels
= max5481_channels
;
154 indio_dev
->num_channels
= ARRAY_SIZE(max5481_channels
);
156 /* restore wiper from NV */
157 ret
= max5481_write_cmd(data
, MAX5481_COPY_NV_TO_AB
, 0);
161 return iio_device_register(indio_dev
);
164 static int max5481_remove(struct spi_device
*spi
)
166 struct iio_dev
*indio_dev
= dev_get_drvdata(&spi
->dev
);
167 struct max5481_data
*data
= iio_priv(indio_dev
);
169 iio_device_unregister(indio_dev
);
171 /* save wiper reg to NV reg */
172 return max5481_write_cmd(data
, MAX5481_COPY_AB_TO_NV
, 0);
175 static const struct spi_device_id max5481_id_table
[] = {
176 { "max5481", max5481
},
177 { "max5482", max5482
},
178 { "max5483", max5483
},
179 { "max5484", max5484
},
182 MODULE_DEVICE_TABLE(spi
, max5481_id_table
);
184 static struct spi_driver max5481_driver
= {
187 .of_match_table
= max5481_match
,
189 .probe
= max5481_probe
,
190 .remove
= max5481_remove
,
191 .id_table
= max5481_id_table
,
194 module_spi_driver(max5481_driver
);
196 MODULE_AUTHOR("Maury Anderson <maury.anderson@rockwellcollins.com>");
197 MODULE_DESCRIPTION("max5481 SPI driver");
198 MODULE_LICENSE("GPL v2");