1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Maxim Integrated MAX5432-MAX5435 digital potentiometer driver
4 * Copyright (C) 2019 Martin Kaiser <martin@kaiser.cx>
7 * https://datasheets.maximintegrated.com/en/ds/MAX5432-MAX5435.pdf
10 #include <linux/i2c.h>
11 #include <linux/iio/iio.h>
12 #include <linux/limits.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/property.h>
17 /* All chip variants have 32 wiper positions. */
18 #define MAX5432_MAX_POS 31
20 #define MAX5432_OHM_50K (50 * 1000)
21 #define MAX5432_OHM_100K (100 * 1000)
23 /* Update the volatile (currently active) setting. */
24 #define MAX5432_CMD_VREG 0x11
27 struct i2c_client
*client
;
31 static const struct iio_chan_spec max5432_channels
[] = {
33 .type
= IIO_RESISTANCE
,
37 .address
= MAX5432_CMD_VREG
,
38 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
39 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
43 static int max5432_read_raw(struct iio_dev
*indio_dev
,
44 struct iio_chan_spec
const *chan
,
45 int *val
, int *val2
, long mask
)
47 struct max5432_data
*data
= iio_priv(indio_dev
);
49 if (mask
!= IIO_CHAN_INFO_SCALE
)
52 if (unlikely(data
->ohm
> INT_MAX
))
56 *val2
= MAX5432_MAX_POS
;
58 return IIO_VAL_FRACTIONAL
;
61 static int max5432_write_raw(struct iio_dev
*indio_dev
,
62 struct iio_chan_spec
const *chan
,
63 int val
, int val2
, long mask
)
65 struct max5432_data
*data
= iio_priv(indio_dev
);
68 if (mask
!= IIO_CHAN_INFO_RAW
)
71 if (val
< 0 || val
> MAX5432_MAX_POS
)
77 /* Wiper position is in bits D7-D3. (D2-D0 are don't care bits.) */
79 return i2c_smbus_write_byte_data(data
->client
, chan
->address
,
83 static const struct iio_info max5432_info
= {
84 .read_raw
= max5432_read_raw
,
85 .write_raw
= max5432_write_raw
,
88 static int max5432_probe(struct i2c_client
*client
,
89 const struct i2c_device_id
*id
)
91 struct device
*dev
= &client
->dev
;
92 struct iio_dev
*indio_dev
;
93 struct max5432_data
*data
;
95 indio_dev
= devm_iio_device_alloc(dev
, sizeof(struct max5432_data
));
99 i2c_set_clientdata(client
, indio_dev
);
101 data
= iio_priv(indio_dev
);
102 data
->client
= client
;
103 data
->ohm
= (unsigned long)device_get_match_data(dev
);
105 indio_dev
->info
= &max5432_info
;
106 indio_dev
->channels
= max5432_channels
;
107 indio_dev
->num_channels
= ARRAY_SIZE(max5432_channels
);
108 indio_dev
->name
= client
->name
;
110 return devm_iio_device_register(dev
, indio_dev
);
113 static const struct of_device_id max5432_dt_ids
[] = {
114 { .compatible
= "maxim,max5432", .data
= (void *)MAX5432_OHM_50K
},
115 { .compatible
= "maxim,max5433", .data
= (void *)MAX5432_OHM_100K
},
116 { .compatible
= "maxim,max5434", .data
= (void *)MAX5432_OHM_50K
},
117 { .compatible
= "maxim,max5435", .data
= (void *)MAX5432_OHM_100K
},
120 MODULE_DEVICE_TABLE(of
, max5432_dt_ids
);
122 static struct i2c_driver max5432_driver
= {
125 .of_match_table
= max5432_dt_ids
,
127 .probe
= max5432_probe
,
130 module_i2c_driver(max5432_driver
);
132 MODULE_AUTHOR("Martin Kaiser <martin@kaiser.cx>");
133 MODULE_DESCRIPTION("max5432-max5435 digital potentiometers");
134 MODULE_LICENSE("GPL v2");