179e8a32dSPhil Reid // SPDX-License-Identifier: GPL-2.0+
279e8a32dSPhil Reid /*
379e8a32dSPhil Reid * Analog Devices AD5272 digital potentiometer driver
479e8a32dSPhil Reid * Copyright (C) 2018 Phil Reid <preid@electromag.com.au>
579e8a32dSPhil Reid *
63593cd53SAlexander A. Klimov * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/AD5272_5274.pdf
779e8a32dSPhil Reid *
879e8a32dSPhil Reid * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address
979e8a32dSPhil Reid * ad5272 1 1024 20, 50, 100 01011xx
1079e8a32dSPhil Reid * ad5274 1 256 20, 100 01011xx
1179e8a32dSPhil Reid */
1279e8a32dSPhil Reid
1379e8a32dSPhil Reid #include <linux/delay.h>
1479e8a32dSPhil Reid #include <linux/gpio/consumer.h>
1579e8a32dSPhil Reid #include <linux/i2c.h>
1679e8a32dSPhil Reid #include <linux/iio/iio.h>
1779e8a32dSPhil Reid #include <linux/module.h>
1819061b3cSJonathan Cameron #include <linux/mod_devicetable.h>
1979e8a32dSPhil Reid
2079e8a32dSPhil Reid #define AD5272_RDAC_WR 1
2179e8a32dSPhil Reid #define AD5272_RDAC_RD 2
2279e8a32dSPhil Reid #define AD5272_RESET 4
2379e8a32dSPhil Reid #define AD5272_CTL 7
2479e8a32dSPhil Reid
2579e8a32dSPhil Reid #define AD5272_RDAC_WR_EN BIT(1)
2679e8a32dSPhil Reid
2779e8a32dSPhil Reid struct ad5272_cfg {
2879e8a32dSPhil Reid int max_pos;
2979e8a32dSPhil Reid int kohms;
3079e8a32dSPhil Reid int shift;
3179e8a32dSPhil Reid };
3279e8a32dSPhil Reid
3379e8a32dSPhil Reid enum ad5272_type {
3479e8a32dSPhil Reid AD5272_020,
3579e8a32dSPhil Reid AD5272_050,
3679e8a32dSPhil Reid AD5272_100,
3779e8a32dSPhil Reid AD5274_020,
3879e8a32dSPhil Reid AD5274_100,
3979e8a32dSPhil Reid };
4079e8a32dSPhil Reid
4179e8a32dSPhil Reid static const struct ad5272_cfg ad5272_cfg[] = {
4279e8a32dSPhil Reid [AD5272_020] = { .max_pos = 1024, .kohms = 20 },
4379e8a32dSPhil Reid [AD5272_050] = { .max_pos = 1024, .kohms = 50 },
4479e8a32dSPhil Reid [AD5272_100] = { .max_pos = 1024, .kohms = 100 },
4579e8a32dSPhil Reid [AD5274_020] = { .max_pos = 256, .kohms = 20, .shift = 2 },
4679e8a32dSPhil Reid [AD5274_100] = { .max_pos = 256, .kohms = 100, .shift = 2 },
4779e8a32dSPhil Reid };
4879e8a32dSPhil Reid
4979e8a32dSPhil Reid struct ad5272_data {
5079e8a32dSPhil Reid struct i2c_client *client;
5179e8a32dSPhil Reid struct mutex lock;
5279e8a32dSPhil Reid const struct ad5272_cfg *cfg;
53da803652SJonathan Cameron u8 buf[2] __aligned(IIO_DMA_MINALIGN);
5479e8a32dSPhil Reid };
5579e8a32dSPhil Reid
5679e8a32dSPhil Reid static const struct iio_chan_spec ad5272_channel = {
5779e8a32dSPhil Reid .type = IIO_RESISTANCE,
5879e8a32dSPhil Reid .output = 1,
5979e8a32dSPhil Reid .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
6079e8a32dSPhil Reid .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
6179e8a32dSPhil Reid };
6279e8a32dSPhil Reid
ad5272_write(struct ad5272_data * data,int reg,int val)6379e8a32dSPhil Reid static int ad5272_write(struct ad5272_data *data, int reg, int val)
6479e8a32dSPhil Reid {
6579e8a32dSPhil Reid int ret;
6679e8a32dSPhil Reid
6779e8a32dSPhil Reid data->buf[0] = (reg << 2) | ((val >> 8) & 0x3);
6879e8a32dSPhil Reid data->buf[1] = (u8)val;
6979e8a32dSPhil Reid
7079e8a32dSPhil Reid mutex_lock(&data->lock);
7179e8a32dSPhil Reid ret = i2c_master_send(data->client, data->buf, sizeof(data->buf));
7279e8a32dSPhil Reid mutex_unlock(&data->lock);
7379e8a32dSPhil Reid return ret < 0 ? ret : 0;
7479e8a32dSPhil Reid }
7579e8a32dSPhil Reid
ad5272_read(struct ad5272_data * data,int reg,int * val)7679e8a32dSPhil Reid static int ad5272_read(struct ad5272_data *data, int reg, int *val)
7779e8a32dSPhil Reid {
7879e8a32dSPhil Reid int ret;
7979e8a32dSPhil Reid
8079e8a32dSPhil Reid data->buf[0] = reg << 2;
8179e8a32dSPhil Reid data->buf[1] = 0;
8279e8a32dSPhil Reid
8379e8a32dSPhil Reid mutex_lock(&data->lock);
8479e8a32dSPhil Reid ret = i2c_master_send(data->client, data->buf, sizeof(data->buf));
8579e8a32dSPhil Reid if (ret < 0)
8679e8a32dSPhil Reid goto error;
8779e8a32dSPhil Reid
8879e8a32dSPhil Reid ret = i2c_master_recv(data->client, data->buf, sizeof(data->buf));
8979e8a32dSPhil Reid if (ret < 0)
9079e8a32dSPhil Reid goto error;
9179e8a32dSPhil Reid
9279e8a32dSPhil Reid *val = ((data->buf[0] & 0x3) << 8) | data->buf[1];
9379e8a32dSPhil Reid ret = 0;
9479e8a32dSPhil Reid error:
9579e8a32dSPhil Reid mutex_unlock(&data->lock);
9679e8a32dSPhil Reid return ret;
9779e8a32dSPhil Reid }
9879e8a32dSPhil Reid
ad5272_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)9979e8a32dSPhil Reid static int ad5272_read_raw(struct iio_dev *indio_dev,
10079e8a32dSPhil Reid struct iio_chan_spec const *chan,
10179e8a32dSPhil Reid int *val, int *val2, long mask)
10279e8a32dSPhil Reid {
10379e8a32dSPhil Reid struct ad5272_data *data = iio_priv(indio_dev);
10479e8a32dSPhil Reid int ret;
10579e8a32dSPhil Reid
10679e8a32dSPhil Reid switch (mask) {
10779e8a32dSPhil Reid case IIO_CHAN_INFO_RAW: {
10879e8a32dSPhil Reid ret = ad5272_read(data, AD5272_RDAC_RD, val);
10979e8a32dSPhil Reid *val = *val >> data->cfg->shift;
11079e8a32dSPhil Reid return ret ? ret : IIO_VAL_INT;
11179e8a32dSPhil Reid }
11279e8a32dSPhil Reid case IIO_CHAN_INFO_SCALE:
11379e8a32dSPhil Reid *val = 1000 * data->cfg->kohms;
11479e8a32dSPhil Reid *val2 = data->cfg->max_pos;
11579e8a32dSPhil Reid return IIO_VAL_FRACTIONAL;
11679e8a32dSPhil Reid }
11779e8a32dSPhil Reid
11879e8a32dSPhil Reid return -EINVAL;
11979e8a32dSPhil Reid }
12079e8a32dSPhil Reid
ad5272_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)12179e8a32dSPhil Reid static int ad5272_write_raw(struct iio_dev *indio_dev,
12279e8a32dSPhil Reid struct iio_chan_spec const *chan,
12379e8a32dSPhil Reid int val, int val2, long mask)
12479e8a32dSPhil Reid {
12579e8a32dSPhil Reid struct ad5272_data *data = iio_priv(indio_dev);
12679e8a32dSPhil Reid
12779e8a32dSPhil Reid if (mask != IIO_CHAN_INFO_RAW)
12879e8a32dSPhil Reid return -EINVAL;
12979e8a32dSPhil Reid
13079e8a32dSPhil Reid if (val >= data->cfg->max_pos || val < 0 || val2)
13179e8a32dSPhil Reid return -EINVAL;
13279e8a32dSPhil Reid
13379e8a32dSPhil Reid return ad5272_write(data, AD5272_RDAC_WR, val << data->cfg->shift);
13479e8a32dSPhil Reid }
13579e8a32dSPhil Reid
13679e8a32dSPhil Reid static const struct iio_info ad5272_info = {
13779e8a32dSPhil Reid .read_raw = ad5272_read_raw,
13879e8a32dSPhil Reid .write_raw = ad5272_write_raw,
13979e8a32dSPhil Reid };
14079e8a32dSPhil Reid
ad5272_reset(struct ad5272_data * data)14179e8a32dSPhil Reid static int ad5272_reset(struct ad5272_data *data)
14279e8a32dSPhil Reid {
14379e8a32dSPhil Reid struct gpio_desc *reset_gpio;
14479e8a32dSPhil Reid
14579e8a32dSPhil Reid reset_gpio = devm_gpiod_get_optional(&data->client->dev, "reset",
1467dd94246SPhil Reid GPIOD_OUT_HIGH);
14779e8a32dSPhil Reid if (IS_ERR(reset_gpio))
14879e8a32dSPhil Reid return PTR_ERR(reset_gpio);
14979e8a32dSPhil Reid
15079e8a32dSPhil Reid if (reset_gpio) {
15179e8a32dSPhil Reid udelay(1);
1527dd94246SPhil Reid gpiod_set_value(reset_gpio, 0);
15379e8a32dSPhil Reid } else {
15479e8a32dSPhil Reid ad5272_write(data, AD5272_RESET, 0);
15579e8a32dSPhil Reid }
15679e8a32dSPhil Reid usleep_range(1000, 2000);
15779e8a32dSPhil Reid
15879e8a32dSPhil Reid return 0;
15979e8a32dSPhil Reid }
16079e8a32dSPhil Reid
ad5272_probe(struct i2c_client * client)161b11df837SUwe Kleine-König static int ad5272_probe(struct i2c_client *client)
16279e8a32dSPhil Reid {
163b11df837SUwe Kleine-König const struct i2c_device_id *id = i2c_client_get_device_id(client);
16479e8a32dSPhil Reid struct device *dev = &client->dev;
16579e8a32dSPhil Reid struct iio_dev *indio_dev;
16679e8a32dSPhil Reid struct ad5272_data *data;
16779e8a32dSPhil Reid int ret;
16879e8a32dSPhil Reid
16979e8a32dSPhil Reid indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
17079e8a32dSPhil Reid if (!indio_dev)
17179e8a32dSPhil Reid return -ENOMEM;
17279e8a32dSPhil Reid
17379e8a32dSPhil Reid i2c_set_clientdata(client, indio_dev);
17479e8a32dSPhil Reid
17579e8a32dSPhil Reid data = iio_priv(indio_dev);
17679e8a32dSPhil Reid data->client = client;
17779e8a32dSPhil Reid mutex_init(&data->lock);
17879e8a32dSPhil Reid data->cfg = &ad5272_cfg[id->driver_data];
17979e8a32dSPhil Reid
18079e8a32dSPhil Reid ret = ad5272_reset(data);
18179e8a32dSPhil Reid if (ret)
18279e8a32dSPhil Reid return ret;
18379e8a32dSPhil Reid
18479e8a32dSPhil Reid ret = ad5272_write(data, AD5272_CTL, AD5272_RDAC_WR_EN);
18579e8a32dSPhil Reid if (ret < 0)
18679e8a32dSPhil Reid return -ENODEV;
18779e8a32dSPhil Reid
18879e8a32dSPhil Reid indio_dev->info = &ad5272_info;
18979e8a32dSPhil Reid indio_dev->channels = &ad5272_channel;
19079e8a32dSPhil Reid indio_dev->num_channels = 1;
19179e8a32dSPhil Reid indio_dev->name = client->name;
19279e8a32dSPhil Reid
19379e8a32dSPhil Reid return devm_iio_device_register(dev, indio_dev);
19479e8a32dSPhil Reid }
19579e8a32dSPhil Reid
19679e8a32dSPhil Reid static const struct of_device_id ad5272_dt_ids[] = {
19779e8a32dSPhil Reid { .compatible = "adi,ad5272-020", .data = (void *)AD5272_020 },
19879e8a32dSPhil Reid { .compatible = "adi,ad5272-050", .data = (void *)AD5272_050 },
19979e8a32dSPhil Reid { .compatible = "adi,ad5272-100", .data = (void *)AD5272_100 },
20079e8a32dSPhil Reid { .compatible = "adi,ad5274-020", .data = (void *)AD5274_020 },
20179e8a32dSPhil Reid { .compatible = "adi,ad5274-100", .data = (void *)AD5274_100 },
20279e8a32dSPhil Reid {}
20379e8a32dSPhil Reid };
20479e8a32dSPhil Reid MODULE_DEVICE_TABLE(of, ad5272_dt_ids);
20579e8a32dSPhil Reid
20679e8a32dSPhil Reid static const struct i2c_device_id ad5272_id[] = {
20779e8a32dSPhil Reid { "ad5272-020", AD5272_020 },
20879e8a32dSPhil Reid { "ad5272-050", AD5272_050 },
20979e8a32dSPhil Reid { "ad5272-100", AD5272_100 },
21079e8a32dSPhil Reid { "ad5274-020", AD5274_020 },
21179e8a32dSPhil Reid { "ad5274-100", AD5274_100 },
21279e8a32dSPhil Reid {}
21379e8a32dSPhil Reid };
21479e8a32dSPhil Reid MODULE_DEVICE_TABLE(i2c, ad5272_id);
21579e8a32dSPhil Reid
21679e8a32dSPhil Reid static struct i2c_driver ad5272_driver = {
21779e8a32dSPhil Reid .driver = {
21879e8a32dSPhil Reid .name = "ad5272",
21919061b3cSJonathan Cameron .of_match_table = ad5272_dt_ids,
22079e8a32dSPhil Reid },
221*7cf15f42SUwe Kleine-König .probe = ad5272_probe,
22279e8a32dSPhil Reid .id_table = ad5272_id,
22379e8a32dSPhil Reid };
22479e8a32dSPhil Reid
22579e8a32dSPhil Reid module_i2c_driver(ad5272_driver);
22679e8a32dSPhil Reid
22779e8a32dSPhil Reid MODULE_AUTHOR("Phil Reid <preid@eletromag.com.au>");
22879e8a32dSPhil Reid MODULE_DESCRIPTION("AD5272 digital potentiometer");
22979e8a32dSPhil Reid MODULE_LICENSE("GPL v2");
230