xref: /linux/drivers/iio/potentiometer/ad5272.c (revision 79e8a32d2aa9e98b1560cbe107e750f07807d8c7)
1*79e8a32dSPhil Reid // SPDX-License-Identifier: GPL-2.0+
2*79e8a32dSPhil Reid /*
3*79e8a32dSPhil Reid  * Analog Devices AD5272 digital potentiometer driver
4*79e8a32dSPhil Reid  * Copyright (C) 2018 Phil Reid <preid@electromag.com.au>
5*79e8a32dSPhil Reid  *
6*79e8a32dSPhil Reid  * Datasheet: http://www.analog.com/media/en/technical-documentation/data-sheets/AD5272_5274.pdf
7*79e8a32dSPhil Reid  *
8*79e8a32dSPhil Reid  * DEVID	#Wipers	#Positions	Resistor Opts (kOhm)	i2c address
9*79e8a32dSPhil Reid  * ad5272	1	1024		20, 50, 100		01011xx
10*79e8a32dSPhil Reid  * ad5274	1	256		20, 100			01011xx
11*79e8a32dSPhil Reid  */
12*79e8a32dSPhil Reid 
13*79e8a32dSPhil Reid #include <linux/delay.h>
14*79e8a32dSPhil Reid #include <linux/gpio/consumer.h>
15*79e8a32dSPhil Reid #include <linux/i2c.h>
16*79e8a32dSPhil Reid #include <linux/iio/iio.h>
17*79e8a32dSPhil Reid #include <linux/module.h>
18*79e8a32dSPhil Reid 
19*79e8a32dSPhil Reid #define  AD5272_RDAC_WR  1
20*79e8a32dSPhil Reid #define  AD5272_RDAC_RD  2
21*79e8a32dSPhil Reid #define  AD5272_RESET    4
22*79e8a32dSPhil Reid #define  AD5272_CTL      7
23*79e8a32dSPhil Reid 
24*79e8a32dSPhil Reid #define  AD5272_RDAC_WR_EN  BIT(1)
25*79e8a32dSPhil Reid 
26*79e8a32dSPhil Reid struct ad5272_cfg {
27*79e8a32dSPhil Reid 	int max_pos;
28*79e8a32dSPhil Reid 	int kohms;
29*79e8a32dSPhil Reid 	int shift;
30*79e8a32dSPhil Reid };
31*79e8a32dSPhil Reid 
32*79e8a32dSPhil Reid enum ad5272_type {
33*79e8a32dSPhil Reid 	AD5272_020,
34*79e8a32dSPhil Reid 	AD5272_050,
35*79e8a32dSPhil Reid 	AD5272_100,
36*79e8a32dSPhil Reid 	AD5274_020,
37*79e8a32dSPhil Reid 	AD5274_100,
38*79e8a32dSPhil Reid };
39*79e8a32dSPhil Reid 
40*79e8a32dSPhil Reid static const struct ad5272_cfg ad5272_cfg[] = {
41*79e8a32dSPhil Reid 	[AD5272_020] = { .max_pos = 1024, .kohms = 20 },
42*79e8a32dSPhil Reid 	[AD5272_050] = { .max_pos = 1024, .kohms = 50 },
43*79e8a32dSPhil Reid 	[AD5272_100] = { .max_pos = 1024, .kohms = 100 },
44*79e8a32dSPhil Reid 	[AD5274_020] = { .max_pos = 256,  .kohms = 20,  .shift = 2 },
45*79e8a32dSPhil Reid 	[AD5274_100] = { .max_pos = 256,  .kohms = 100, .shift = 2 },
46*79e8a32dSPhil Reid };
47*79e8a32dSPhil Reid 
48*79e8a32dSPhil Reid struct ad5272_data {
49*79e8a32dSPhil Reid 	struct i2c_client       *client;
50*79e8a32dSPhil Reid 	struct mutex            lock;
51*79e8a32dSPhil Reid 	const struct ad5272_cfg *cfg;
52*79e8a32dSPhil Reid 	u8                      buf[2] ____cacheline_aligned;
53*79e8a32dSPhil Reid };
54*79e8a32dSPhil Reid 
55*79e8a32dSPhil Reid static const struct iio_chan_spec ad5272_channel = {
56*79e8a32dSPhil Reid 	.type = IIO_RESISTANCE,
57*79e8a32dSPhil Reid 	.output = 1,
58*79e8a32dSPhil Reid 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
59*79e8a32dSPhil Reid 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
60*79e8a32dSPhil Reid };
61*79e8a32dSPhil Reid 
62*79e8a32dSPhil Reid static int ad5272_write(struct ad5272_data *data, int reg, int val)
63*79e8a32dSPhil Reid {
64*79e8a32dSPhil Reid 	int ret;
65*79e8a32dSPhil Reid 
66*79e8a32dSPhil Reid 	data->buf[0] = (reg << 2) | ((val >> 8) & 0x3);
67*79e8a32dSPhil Reid 	data->buf[1] = (u8)val;
68*79e8a32dSPhil Reid 
69*79e8a32dSPhil Reid 	mutex_lock(&data->lock);
70*79e8a32dSPhil Reid 	ret = i2c_master_send(data->client, data->buf, sizeof(data->buf));
71*79e8a32dSPhil Reid 	mutex_unlock(&data->lock);
72*79e8a32dSPhil Reid 	return ret < 0 ? ret : 0;
73*79e8a32dSPhil Reid }
74*79e8a32dSPhil Reid 
75*79e8a32dSPhil Reid static int ad5272_read(struct ad5272_data *data, int reg, int *val)
76*79e8a32dSPhil Reid {
77*79e8a32dSPhil Reid 	int ret;
78*79e8a32dSPhil Reid 
79*79e8a32dSPhil Reid 	data->buf[0] = reg << 2;
80*79e8a32dSPhil Reid 	data->buf[1] = 0;
81*79e8a32dSPhil Reid 
82*79e8a32dSPhil Reid 	mutex_lock(&data->lock);
83*79e8a32dSPhil Reid 	ret = i2c_master_send(data->client, data->buf, sizeof(data->buf));
84*79e8a32dSPhil Reid 	if (ret < 0)
85*79e8a32dSPhil Reid 		goto error;
86*79e8a32dSPhil Reid 
87*79e8a32dSPhil Reid 	ret = i2c_master_recv(data->client, data->buf, sizeof(data->buf));
88*79e8a32dSPhil Reid 	if (ret < 0)
89*79e8a32dSPhil Reid 		goto error;
90*79e8a32dSPhil Reid 
91*79e8a32dSPhil Reid 	*val = ((data->buf[0] & 0x3) << 8) | data->buf[1];
92*79e8a32dSPhil Reid 	ret = 0;
93*79e8a32dSPhil Reid error:
94*79e8a32dSPhil Reid 	mutex_unlock(&data->lock);
95*79e8a32dSPhil Reid 	return ret;
96*79e8a32dSPhil Reid }
97*79e8a32dSPhil Reid 
98*79e8a32dSPhil Reid static int ad5272_read_raw(struct iio_dev *indio_dev,
99*79e8a32dSPhil Reid 			   struct iio_chan_spec const *chan,
100*79e8a32dSPhil Reid 			   int *val, int *val2, long mask)
101*79e8a32dSPhil Reid {
102*79e8a32dSPhil Reid 	struct ad5272_data *data = iio_priv(indio_dev);
103*79e8a32dSPhil Reid 	int ret;
104*79e8a32dSPhil Reid 
105*79e8a32dSPhil Reid 	switch (mask) {
106*79e8a32dSPhil Reid 	case IIO_CHAN_INFO_RAW: {
107*79e8a32dSPhil Reid 		ret = ad5272_read(data, AD5272_RDAC_RD, val);
108*79e8a32dSPhil Reid 		*val = *val >> data->cfg->shift;
109*79e8a32dSPhil Reid 		return ret ? ret : IIO_VAL_INT;
110*79e8a32dSPhil Reid 	}
111*79e8a32dSPhil Reid 	case IIO_CHAN_INFO_SCALE:
112*79e8a32dSPhil Reid 		*val = 1000 * data->cfg->kohms;
113*79e8a32dSPhil Reid 		*val2 = data->cfg->max_pos;
114*79e8a32dSPhil Reid 		return IIO_VAL_FRACTIONAL;
115*79e8a32dSPhil Reid 	}
116*79e8a32dSPhil Reid 
117*79e8a32dSPhil Reid 	return -EINVAL;
118*79e8a32dSPhil Reid }
119*79e8a32dSPhil Reid 
120*79e8a32dSPhil Reid static int ad5272_write_raw(struct iio_dev *indio_dev,
121*79e8a32dSPhil Reid 			    struct iio_chan_spec const *chan,
122*79e8a32dSPhil Reid 			    int val, int val2, long mask)
123*79e8a32dSPhil Reid {
124*79e8a32dSPhil Reid 	struct ad5272_data *data = iio_priv(indio_dev);
125*79e8a32dSPhil Reid 
126*79e8a32dSPhil Reid 	if (mask != IIO_CHAN_INFO_RAW)
127*79e8a32dSPhil Reid 		return -EINVAL;
128*79e8a32dSPhil Reid 
129*79e8a32dSPhil Reid 	if (val >= data->cfg->max_pos || val < 0 || val2)
130*79e8a32dSPhil Reid 		return -EINVAL;
131*79e8a32dSPhil Reid 
132*79e8a32dSPhil Reid 	return ad5272_write(data, AD5272_RDAC_WR, val << data->cfg->shift);
133*79e8a32dSPhil Reid }
134*79e8a32dSPhil Reid 
135*79e8a32dSPhil Reid static const struct iio_info ad5272_info = {
136*79e8a32dSPhil Reid 	.read_raw = ad5272_read_raw,
137*79e8a32dSPhil Reid 	.write_raw = ad5272_write_raw,
138*79e8a32dSPhil Reid };
139*79e8a32dSPhil Reid 
140*79e8a32dSPhil Reid static int ad5272_reset(struct ad5272_data *data)
141*79e8a32dSPhil Reid {
142*79e8a32dSPhil Reid 	struct gpio_desc *reset_gpio;
143*79e8a32dSPhil Reid 
144*79e8a32dSPhil Reid 	reset_gpio = devm_gpiod_get_optional(&data->client->dev, "reset",
145*79e8a32dSPhil Reid 		GPIOD_OUT_LOW);
146*79e8a32dSPhil Reid 	if (IS_ERR(reset_gpio))
147*79e8a32dSPhil Reid 		return PTR_ERR(reset_gpio);
148*79e8a32dSPhil Reid 
149*79e8a32dSPhil Reid 	if (reset_gpio) {
150*79e8a32dSPhil Reid 		udelay(1);
151*79e8a32dSPhil Reid 		gpiod_set_value(reset_gpio, 1);
152*79e8a32dSPhil Reid 	} else {
153*79e8a32dSPhil Reid 		ad5272_write(data, AD5272_RESET, 0);
154*79e8a32dSPhil Reid 	}
155*79e8a32dSPhil Reid 	usleep_range(1000, 2000);
156*79e8a32dSPhil Reid 
157*79e8a32dSPhil Reid 	return 0;
158*79e8a32dSPhil Reid }
159*79e8a32dSPhil Reid 
160*79e8a32dSPhil Reid static int ad5272_probe(struct i2c_client *client,
161*79e8a32dSPhil Reid 			const struct i2c_device_id *id)
162*79e8a32dSPhil Reid {
163*79e8a32dSPhil Reid 	struct device *dev = &client->dev;
164*79e8a32dSPhil Reid 	struct iio_dev *indio_dev;
165*79e8a32dSPhil Reid 	struct ad5272_data *data;
166*79e8a32dSPhil Reid 	int ret;
167*79e8a32dSPhil Reid 
168*79e8a32dSPhil Reid 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
169*79e8a32dSPhil Reid 	if (!indio_dev)
170*79e8a32dSPhil Reid 		return -ENOMEM;
171*79e8a32dSPhil Reid 
172*79e8a32dSPhil Reid 	i2c_set_clientdata(client, indio_dev);
173*79e8a32dSPhil Reid 
174*79e8a32dSPhil Reid 	data = iio_priv(indio_dev);
175*79e8a32dSPhil Reid 	data->client = client;
176*79e8a32dSPhil Reid 	mutex_init(&data->lock);
177*79e8a32dSPhil Reid 	data->cfg = &ad5272_cfg[id->driver_data];
178*79e8a32dSPhil Reid 
179*79e8a32dSPhil Reid 	ret = ad5272_reset(data);
180*79e8a32dSPhil Reid 	if (ret)
181*79e8a32dSPhil Reid 		return ret;
182*79e8a32dSPhil Reid 
183*79e8a32dSPhil Reid 	ret = ad5272_write(data, AD5272_CTL, AD5272_RDAC_WR_EN);
184*79e8a32dSPhil Reid 	if (ret < 0)
185*79e8a32dSPhil Reid 		return -ENODEV;
186*79e8a32dSPhil Reid 
187*79e8a32dSPhil Reid 	indio_dev->dev.parent = dev;
188*79e8a32dSPhil Reid 	indio_dev->info = &ad5272_info;
189*79e8a32dSPhil Reid 	indio_dev->channels = &ad5272_channel;
190*79e8a32dSPhil Reid 	indio_dev->num_channels = 1;
191*79e8a32dSPhil Reid 	indio_dev->name = client->name;
192*79e8a32dSPhil Reid 
193*79e8a32dSPhil Reid 	return devm_iio_device_register(dev, indio_dev);
194*79e8a32dSPhil Reid }
195*79e8a32dSPhil Reid 
196*79e8a32dSPhil Reid #if defined(CONFIG_OF)
197*79e8a32dSPhil Reid static const struct of_device_id ad5272_dt_ids[] = {
198*79e8a32dSPhil Reid 	{ .compatible = "adi,ad5272-020", .data = (void *)AD5272_020 },
199*79e8a32dSPhil Reid 	{ .compatible = "adi,ad5272-050", .data = (void *)AD5272_050 },
200*79e8a32dSPhil Reid 	{ .compatible = "adi,ad5272-100", .data = (void *)AD5272_100 },
201*79e8a32dSPhil Reid 	{ .compatible = "adi,ad5274-020", .data = (void *)AD5274_020 },
202*79e8a32dSPhil Reid 	{ .compatible = "adi,ad5274-100", .data = (void *)AD5274_100 },
203*79e8a32dSPhil Reid 	{}
204*79e8a32dSPhil Reid };
205*79e8a32dSPhil Reid MODULE_DEVICE_TABLE(of, ad5272_dt_ids);
206*79e8a32dSPhil Reid #endif /* CONFIG_OF */
207*79e8a32dSPhil Reid 
208*79e8a32dSPhil Reid static const struct i2c_device_id ad5272_id[] = {
209*79e8a32dSPhil Reid 	{ "ad5272-020", AD5272_020 },
210*79e8a32dSPhil Reid 	{ "ad5272-050", AD5272_050 },
211*79e8a32dSPhil Reid 	{ "ad5272-100", AD5272_100 },
212*79e8a32dSPhil Reid 	{ "ad5274-020", AD5274_020 },
213*79e8a32dSPhil Reid 	{ "ad5274-100", AD5274_100 },
214*79e8a32dSPhil Reid 	{}
215*79e8a32dSPhil Reid };
216*79e8a32dSPhil Reid MODULE_DEVICE_TABLE(i2c, ad5272_id);
217*79e8a32dSPhil Reid 
218*79e8a32dSPhil Reid static struct i2c_driver ad5272_driver = {
219*79e8a32dSPhil Reid 	.driver = {
220*79e8a32dSPhil Reid 		.name	= "ad5272",
221*79e8a32dSPhil Reid 		.of_match_table = of_match_ptr(ad5272_dt_ids),
222*79e8a32dSPhil Reid 	},
223*79e8a32dSPhil Reid 	.probe		= ad5272_probe,
224*79e8a32dSPhil Reid 	.id_table	= ad5272_id,
225*79e8a32dSPhil Reid };
226*79e8a32dSPhil Reid 
227*79e8a32dSPhil Reid module_i2c_driver(ad5272_driver);
228*79e8a32dSPhil Reid 
229*79e8a32dSPhil Reid MODULE_AUTHOR("Phil Reid <preid@eletromag.com.au>");
230*79e8a32dSPhil Reid MODULE_DESCRIPTION("AD5272 digital potentiometer");
231*79e8a32dSPhil Reid MODULE_LICENSE("GPL v2");
232