0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/delay.h>
0014 #include <linux/gpio/consumer.h>
0015 #include <linux/i2c.h>
0016 #include <linux/iio/iio.h>
0017 #include <linux/module.h>
0018 #include <linux/mod_devicetable.h>
0019
0020 #define AD5272_RDAC_WR 1
0021 #define AD5272_RDAC_RD 2
0022 #define AD5272_RESET 4
0023 #define AD5272_CTL 7
0024
0025 #define AD5272_RDAC_WR_EN BIT(1)
0026
0027 struct ad5272_cfg {
0028 int max_pos;
0029 int kohms;
0030 int shift;
0031 };
0032
0033 enum ad5272_type {
0034 AD5272_020,
0035 AD5272_050,
0036 AD5272_100,
0037 AD5274_020,
0038 AD5274_100,
0039 };
0040
0041 static const struct ad5272_cfg ad5272_cfg[] = {
0042 [AD5272_020] = { .max_pos = 1024, .kohms = 20 },
0043 [AD5272_050] = { .max_pos = 1024, .kohms = 50 },
0044 [AD5272_100] = { .max_pos = 1024, .kohms = 100 },
0045 [AD5274_020] = { .max_pos = 256, .kohms = 20, .shift = 2 },
0046 [AD5274_100] = { .max_pos = 256, .kohms = 100, .shift = 2 },
0047 };
0048
0049 struct ad5272_data {
0050 struct i2c_client *client;
0051 struct mutex lock;
0052 const struct ad5272_cfg *cfg;
0053 u8 buf[2] __aligned(IIO_DMA_MINALIGN);
0054 };
0055
0056 static const struct iio_chan_spec ad5272_channel = {
0057 .type = IIO_RESISTANCE,
0058 .output = 1,
0059 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
0060 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
0061 };
0062
0063 static int ad5272_write(struct ad5272_data *data, int reg, int val)
0064 {
0065 int ret;
0066
0067 data->buf[0] = (reg << 2) | ((val >> 8) & 0x3);
0068 data->buf[1] = (u8)val;
0069
0070 mutex_lock(&data->lock);
0071 ret = i2c_master_send(data->client, data->buf, sizeof(data->buf));
0072 mutex_unlock(&data->lock);
0073 return ret < 0 ? ret : 0;
0074 }
0075
0076 static int ad5272_read(struct ad5272_data *data, int reg, int *val)
0077 {
0078 int ret;
0079
0080 data->buf[0] = reg << 2;
0081 data->buf[1] = 0;
0082
0083 mutex_lock(&data->lock);
0084 ret = i2c_master_send(data->client, data->buf, sizeof(data->buf));
0085 if (ret < 0)
0086 goto error;
0087
0088 ret = i2c_master_recv(data->client, data->buf, sizeof(data->buf));
0089 if (ret < 0)
0090 goto error;
0091
0092 *val = ((data->buf[0] & 0x3) << 8) | data->buf[1];
0093 ret = 0;
0094 error:
0095 mutex_unlock(&data->lock);
0096 return ret;
0097 }
0098
0099 static int ad5272_read_raw(struct iio_dev *indio_dev,
0100 struct iio_chan_spec const *chan,
0101 int *val, int *val2, long mask)
0102 {
0103 struct ad5272_data *data = iio_priv(indio_dev);
0104 int ret;
0105
0106 switch (mask) {
0107 case IIO_CHAN_INFO_RAW: {
0108 ret = ad5272_read(data, AD5272_RDAC_RD, val);
0109 *val = *val >> data->cfg->shift;
0110 return ret ? ret : IIO_VAL_INT;
0111 }
0112 case IIO_CHAN_INFO_SCALE:
0113 *val = 1000 * data->cfg->kohms;
0114 *val2 = data->cfg->max_pos;
0115 return IIO_VAL_FRACTIONAL;
0116 }
0117
0118 return -EINVAL;
0119 }
0120
0121 static int ad5272_write_raw(struct iio_dev *indio_dev,
0122 struct iio_chan_spec const *chan,
0123 int val, int val2, long mask)
0124 {
0125 struct ad5272_data *data = iio_priv(indio_dev);
0126
0127 if (mask != IIO_CHAN_INFO_RAW)
0128 return -EINVAL;
0129
0130 if (val >= data->cfg->max_pos || val < 0 || val2)
0131 return -EINVAL;
0132
0133 return ad5272_write(data, AD5272_RDAC_WR, val << data->cfg->shift);
0134 }
0135
0136 static const struct iio_info ad5272_info = {
0137 .read_raw = ad5272_read_raw,
0138 .write_raw = ad5272_write_raw,
0139 };
0140
0141 static int ad5272_reset(struct ad5272_data *data)
0142 {
0143 struct gpio_desc *reset_gpio;
0144
0145 reset_gpio = devm_gpiod_get_optional(&data->client->dev, "reset",
0146 GPIOD_OUT_HIGH);
0147 if (IS_ERR(reset_gpio))
0148 return PTR_ERR(reset_gpio);
0149
0150 if (reset_gpio) {
0151 udelay(1);
0152 gpiod_set_value(reset_gpio, 0);
0153 } else {
0154 ad5272_write(data, AD5272_RESET, 0);
0155 }
0156 usleep_range(1000, 2000);
0157
0158 return 0;
0159 }
0160
0161 static int ad5272_probe(struct i2c_client *client,
0162 const struct i2c_device_id *id)
0163 {
0164 struct device *dev = &client->dev;
0165 struct iio_dev *indio_dev;
0166 struct ad5272_data *data;
0167 int ret;
0168
0169 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
0170 if (!indio_dev)
0171 return -ENOMEM;
0172
0173 i2c_set_clientdata(client, indio_dev);
0174
0175 data = iio_priv(indio_dev);
0176 data->client = client;
0177 mutex_init(&data->lock);
0178 data->cfg = &ad5272_cfg[id->driver_data];
0179
0180 ret = ad5272_reset(data);
0181 if (ret)
0182 return ret;
0183
0184 ret = ad5272_write(data, AD5272_CTL, AD5272_RDAC_WR_EN);
0185 if (ret < 0)
0186 return -ENODEV;
0187
0188 indio_dev->info = &ad5272_info;
0189 indio_dev->channels = &ad5272_channel;
0190 indio_dev->num_channels = 1;
0191 indio_dev->name = client->name;
0192
0193 return devm_iio_device_register(dev, indio_dev);
0194 }
0195
0196 static const struct of_device_id ad5272_dt_ids[] = {
0197 { .compatible = "adi,ad5272-020", .data = (void *)AD5272_020 },
0198 { .compatible = "adi,ad5272-050", .data = (void *)AD5272_050 },
0199 { .compatible = "adi,ad5272-100", .data = (void *)AD5272_100 },
0200 { .compatible = "adi,ad5274-020", .data = (void *)AD5274_020 },
0201 { .compatible = "adi,ad5274-100", .data = (void *)AD5274_100 },
0202 {}
0203 };
0204 MODULE_DEVICE_TABLE(of, ad5272_dt_ids);
0205
0206 static const struct i2c_device_id ad5272_id[] = {
0207 { "ad5272-020", AD5272_020 },
0208 { "ad5272-050", AD5272_050 },
0209 { "ad5272-100", AD5272_100 },
0210 { "ad5274-020", AD5274_020 },
0211 { "ad5274-100", AD5274_100 },
0212 {}
0213 };
0214 MODULE_DEVICE_TABLE(i2c, ad5272_id);
0215
0216 static struct i2c_driver ad5272_driver = {
0217 .driver = {
0218 .name = "ad5272",
0219 .of_match_table = ad5272_dt_ids,
0220 },
0221 .probe = ad5272_probe,
0222 .id_table = ad5272_id,
0223 };
0224
0225 module_i2c_driver(ad5272_driver);
0226
0227 MODULE_AUTHOR("Phil Reid <preid@eletromag.com.au>");
0228 MODULE_DESCRIPTION("AD5272 digital potentiometer");
0229 MODULE_LICENSE("GPL v2");