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>
1879e8a32dSPhil Reid 
1979e8a32dSPhil Reid #define  AD5272_RDAC_WR  1
2079e8a32dSPhil Reid #define  AD5272_RDAC_RD  2
2179e8a32dSPhil Reid #define  AD5272_RESET    4
2279e8a32dSPhil Reid #define  AD5272_CTL      7
2379e8a32dSPhil Reid 
2479e8a32dSPhil Reid #define  AD5272_RDAC_WR_EN  BIT(1)
2579e8a32dSPhil Reid 
2679e8a32dSPhil Reid struct ad5272_cfg {
2779e8a32dSPhil Reid 	int max_pos;
2879e8a32dSPhil Reid 	int kohms;
2979e8a32dSPhil Reid 	int shift;
3079e8a32dSPhil Reid };
3179e8a32dSPhil Reid 
3279e8a32dSPhil Reid enum ad5272_type {
3379e8a32dSPhil Reid 	AD5272_020,
3479e8a32dSPhil Reid 	AD5272_050,
3579e8a32dSPhil Reid 	AD5272_100,
3679e8a32dSPhil Reid 	AD5274_020,
3779e8a32dSPhil Reid 	AD5274_100,
3879e8a32dSPhil Reid };
3979e8a32dSPhil Reid 
4079e8a32dSPhil Reid static const struct ad5272_cfg ad5272_cfg[] = {
4179e8a32dSPhil Reid 	[AD5272_020] = { .max_pos = 1024, .kohms = 20 },
4279e8a32dSPhil Reid 	[AD5272_050] = { .max_pos = 1024, .kohms = 50 },
4379e8a32dSPhil Reid 	[AD5272_100] = { .max_pos = 1024, .kohms = 100 },
4479e8a32dSPhil Reid 	[AD5274_020] = { .max_pos = 256,  .kohms = 20,  .shift = 2 },
4579e8a32dSPhil Reid 	[AD5274_100] = { .max_pos = 256,  .kohms = 100, .shift = 2 },
4679e8a32dSPhil Reid };
4779e8a32dSPhil Reid 
4879e8a32dSPhil Reid struct ad5272_data {
4979e8a32dSPhil Reid 	struct i2c_client       *client;
5079e8a32dSPhil Reid 	struct mutex            lock;
5179e8a32dSPhil Reid 	const struct ad5272_cfg *cfg;
5279e8a32dSPhil Reid 	u8                      buf[2] ____cacheline_aligned;
5379e8a32dSPhil Reid };
5479e8a32dSPhil Reid 
5579e8a32dSPhil Reid static const struct iio_chan_spec ad5272_channel = {
5679e8a32dSPhil Reid 	.type = IIO_RESISTANCE,
5779e8a32dSPhil Reid 	.output = 1,
5879e8a32dSPhil Reid 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
5979e8a32dSPhil Reid 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
6079e8a32dSPhil Reid };
6179e8a32dSPhil Reid 
6279e8a32dSPhil Reid static int ad5272_write(struct ad5272_data *data, int reg, int val)
6379e8a32dSPhil Reid {
6479e8a32dSPhil Reid 	int ret;
6579e8a32dSPhil Reid 
6679e8a32dSPhil Reid 	data->buf[0] = (reg << 2) | ((val >> 8) & 0x3);
6779e8a32dSPhil Reid 	data->buf[1] = (u8)val;
6879e8a32dSPhil Reid 
6979e8a32dSPhil Reid 	mutex_lock(&data->lock);
7079e8a32dSPhil Reid 	ret = i2c_master_send(data->client, data->buf, sizeof(data->buf));
7179e8a32dSPhil Reid 	mutex_unlock(&data->lock);
7279e8a32dSPhil Reid 	return ret < 0 ? ret : 0;
7379e8a32dSPhil Reid }
7479e8a32dSPhil Reid 
7579e8a32dSPhil Reid static int ad5272_read(struct ad5272_data *data, int reg, int *val)
7679e8a32dSPhil Reid {
7779e8a32dSPhil Reid 	int ret;
7879e8a32dSPhil Reid 
7979e8a32dSPhil Reid 	data->buf[0] = reg << 2;
8079e8a32dSPhil Reid 	data->buf[1] = 0;
8179e8a32dSPhil Reid 
8279e8a32dSPhil Reid 	mutex_lock(&data->lock);
8379e8a32dSPhil Reid 	ret = i2c_master_send(data->client, data->buf, sizeof(data->buf));
8479e8a32dSPhil Reid 	if (ret < 0)
8579e8a32dSPhil Reid 		goto error;
8679e8a32dSPhil Reid 
8779e8a32dSPhil Reid 	ret = i2c_master_recv(data->client, data->buf, sizeof(data->buf));
8879e8a32dSPhil Reid 	if (ret < 0)
8979e8a32dSPhil Reid 		goto error;
9079e8a32dSPhil Reid 
9179e8a32dSPhil Reid 	*val = ((data->buf[0] & 0x3) << 8) | data->buf[1];
9279e8a32dSPhil Reid 	ret = 0;
9379e8a32dSPhil Reid error:
9479e8a32dSPhil Reid 	mutex_unlock(&data->lock);
9579e8a32dSPhil Reid 	return ret;
9679e8a32dSPhil Reid }
9779e8a32dSPhil Reid 
9879e8a32dSPhil Reid static int ad5272_read_raw(struct iio_dev *indio_dev,
9979e8a32dSPhil Reid 			   struct iio_chan_spec const *chan,
10079e8a32dSPhil Reid 			   int *val, int *val2, long mask)
10179e8a32dSPhil Reid {
10279e8a32dSPhil Reid 	struct ad5272_data *data = iio_priv(indio_dev);
10379e8a32dSPhil Reid 	int ret;
10479e8a32dSPhil Reid 
10579e8a32dSPhil Reid 	switch (mask) {
10679e8a32dSPhil Reid 	case IIO_CHAN_INFO_RAW: {
10779e8a32dSPhil Reid 		ret = ad5272_read(data, AD5272_RDAC_RD, val);
10879e8a32dSPhil Reid 		*val = *val >> data->cfg->shift;
10979e8a32dSPhil Reid 		return ret ? ret : IIO_VAL_INT;
11079e8a32dSPhil Reid 	}
11179e8a32dSPhil Reid 	case IIO_CHAN_INFO_SCALE:
11279e8a32dSPhil Reid 		*val = 1000 * data->cfg->kohms;
11379e8a32dSPhil Reid 		*val2 = data->cfg->max_pos;
11479e8a32dSPhil Reid 		return IIO_VAL_FRACTIONAL;
11579e8a32dSPhil Reid 	}
11679e8a32dSPhil Reid 
11779e8a32dSPhil Reid 	return -EINVAL;
11879e8a32dSPhil Reid }
11979e8a32dSPhil Reid 
12079e8a32dSPhil Reid static int ad5272_write_raw(struct iio_dev *indio_dev,
12179e8a32dSPhil Reid 			    struct iio_chan_spec const *chan,
12279e8a32dSPhil Reid 			    int val, int val2, long mask)
12379e8a32dSPhil Reid {
12479e8a32dSPhil Reid 	struct ad5272_data *data = iio_priv(indio_dev);
12579e8a32dSPhil Reid 
12679e8a32dSPhil Reid 	if (mask != IIO_CHAN_INFO_RAW)
12779e8a32dSPhil Reid 		return -EINVAL;
12879e8a32dSPhil Reid 
12979e8a32dSPhil Reid 	if (val >= data->cfg->max_pos || val < 0 || val2)
13079e8a32dSPhil Reid 		return -EINVAL;
13179e8a32dSPhil Reid 
13279e8a32dSPhil Reid 	return ad5272_write(data, AD5272_RDAC_WR, val << data->cfg->shift);
13379e8a32dSPhil Reid }
13479e8a32dSPhil Reid 
13579e8a32dSPhil Reid static const struct iio_info ad5272_info = {
13679e8a32dSPhil Reid 	.read_raw = ad5272_read_raw,
13779e8a32dSPhil Reid 	.write_raw = ad5272_write_raw,
13879e8a32dSPhil Reid };
13979e8a32dSPhil Reid 
14079e8a32dSPhil Reid static int ad5272_reset(struct ad5272_data *data)
14179e8a32dSPhil Reid {
14279e8a32dSPhil Reid 	struct gpio_desc *reset_gpio;
14379e8a32dSPhil Reid 
14479e8a32dSPhil Reid 	reset_gpio = devm_gpiod_get_optional(&data->client->dev, "reset",
14579e8a32dSPhil Reid 		GPIOD_OUT_LOW);
14679e8a32dSPhil Reid 	if (IS_ERR(reset_gpio))
14779e8a32dSPhil Reid 		return PTR_ERR(reset_gpio);
14879e8a32dSPhil Reid 
14979e8a32dSPhil Reid 	if (reset_gpio) {
15079e8a32dSPhil Reid 		udelay(1);
15179e8a32dSPhil Reid 		gpiod_set_value(reset_gpio, 1);
15279e8a32dSPhil Reid 	} else {
15379e8a32dSPhil Reid 		ad5272_write(data, AD5272_RESET, 0);
15479e8a32dSPhil Reid 	}
15579e8a32dSPhil Reid 	usleep_range(1000, 2000);
15679e8a32dSPhil Reid 
15779e8a32dSPhil Reid 	return 0;
15879e8a32dSPhil Reid }
15979e8a32dSPhil Reid 
16079e8a32dSPhil Reid static int ad5272_probe(struct i2c_client *client,
16179e8a32dSPhil Reid 			const struct i2c_device_id *id)
16279e8a32dSPhil Reid {
16379e8a32dSPhil Reid 	struct device *dev = &client->dev;
16479e8a32dSPhil Reid 	struct iio_dev *indio_dev;
16579e8a32dSPhil Reid 	struct ad5272_data *data;
16679e8a32dSPhil Reid 	int ret;
16779e8a32dSPhil Reid 
16879e8a32dSPhil Reid 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
16979e8a32dSPhil Reid 	if (!indio_dev)
17079e8a32dSPhil Reid 		return -ENOMEM;
17179e8a32dSPhil Reid 
17279e8a32dSPhil Reid 	i2c_set_clientdata(client, indio_dev);
17379e8a32dSPhil Reid 
17479e8a32dSPhil Reid 	data = iio_priv(indio_dev);
17579e8a32dSPhil Reid 	data->client = client;
17679e8a32dSPhil Reid 	mutex_init(&data->lock);
17779e8a32dSPhil Reid 	data->cfg = &ad5272_cfg[id->driver_data];
17879e8a32dSPhil Reid 
17979e8a32dSPhil Reid 	ret = ad5272_reset(data);
18079e8a32dSPhil Reid 	if (ret)
18179e8a32dSPhil Reid 		return ret;
18279e8a32dSPhil Reid 
18379e8a32dSPhil Reid 	ret = ad5272_write(data, AD5272_CTL, AD5272_RDAC_WR_EN);
18479e8a32dSPhil Reid 	if (ret < 0)
18579e8a32dSPhil Reid 		return -ENODEV;
18679e8a32dSPhil Reid 
18779e8a32dSPhil Reid 	indio_dev->info = &ad5272_info;
18879e8a32dSPhil Reid 	indio_dev->channels = &ad5272_channel;
18979e8a32dSPhil Reid 	indio_dev->num_channels = 1;
19079e8a32dSPhil Reid 	indio_dev->name = client->name;
19179e8a32dSPhil Reid 
19279e8a32dSPhil Reid 	return devm_iio_device_register(dev, indio_dev);
19379e8a32dSPhil Reid }
19479e8a32dSPhil Reid 
19579e8a32dSPhil Reid #if defined(CONFIG_OF)
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 #endif /* CONFIG_OF */
20679e8a32dSPhil Reid 
20779e8a32dSPhil Reid static const struct i2c_device_id ad5272_id[] = {
20879e8a32dSPhil Reid 	{ "ad5272-020", AD5272_020 },
20979e8a32dSPhil Reid 	{ "ad5272-050", AD5272_050 },
21079e8a32dSPhil Reid 	{ "ad5272-100", AD5272_100 },
21179e8a32dSPhil Reid 	{ "ad5274-020", AD5274_020 },
21279e8a32dSPhil Reid 	{ "ad5274-100", AD5274_100 },
21379e8a32dSPhil Reid 	{}
21479e8a32dSPhil Reid };
21579e8a32dSPhil Reid MODULE_DEVICE_TABLE(i2c, ad5272_id);
21679e8a32dSPhil Reid 
21779e8a32dSPhil Reid static struct i2c_driver ad5272_driver = {
21879e8a32dSPhil Reid 	.driver = {
21979e8a32dSPhil Reid 		.name	= "ad5272",
22079e8a32dSPhil Reid 		.of_match_table = of_match_ptr(ad5272_dt_ids),
22179e8a32dSPhil Reid 	},
22279e8a32dSPhil Reid 	.probe		= ad5272_probe,
22379e8a32dSPhil Reid 	.id_table	= ad5272_id,
22479e8a32dSPhil Reid };
22579e8a32dSPhil Reid 
22679e8a32dSPhil Reid module_i2c_driver(ad5272_driver);
22779e8a32dSPhil Reid 
22879e8a32dSPhil Reid MODULE_AUTHOR("Phil Reid <preid@eletromag.com.au>");
22979e8a32dSPhil Reid MODULE_DESCRIPTION("AD5272 digital potentiometer");
23079e8a32dSPhil Reid MODULE_LICENSE("GPL v2");
231