Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / potentiometer / max5432.c
blobaed3b6ab82a21f9d2e247bd35d57acba669be96a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Maxim Integrated MAX5432-MAX5435 digital potentiometer driver
4 * Copyright (C) 2019 Martin Kaiser <martin@kaiser.cx>
6 * Datasheet:
7 * https://datasheets.maximintegrated.com/en/ds/MAX5432-MAX5435.pdf
8 */
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
26 struct max5432_data {
27 struct i2c_client *client;
28 unsigned long ohm;
31 static const struct iio_chan_spec max5432_channels[] = {
33 .type = IIO_RESISTANCE,
34 .indexed = 1,
35 .output = 1,
36 .channel = 0,
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)
50 return -EINVAL;
52 if (unlikely(data->ohm > INT_MAX))
53 return -ERANGE;
55 *val = data->ohm;
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);
66 u8 data_byte;
68 if (mask != IIO_CHAN_INFO_RAW)
69 return -EINVAL;
71 if (val < 0 || val > MAX5432_MAX_POS)
72 return -EINVAL;
74 if (val2 != 0)
75 return -EINVAL;
77 /* Wiper position is in bits D7-D3. (D2-D0 are don't care bits.) */
78 data_byte = val << 3;
79 return i2c_smbus_write_byte_data(data->client, chan->address,
80 data_byte);
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));
96 if (!indio_dev)
97 return -ENOMEM;
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 },
118 { /* sentinel */ }
120 MODULE_DEVICE_TABLE(of, max5432_dt_ids);
122 static struct i2c_driver max5432_driver = {
123 .driver = {
124 .name = "max5432",
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");