xref: /openbmc/linux/drivers/iio/adc/ti-adc081c.c (revision d40d48e1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver
4  *
5  * Copyright (C) 2012 Avionic Design GmbH
6  * Copyright (C) 2016 Intel
7  *
8  * Datasheets:
9  *	https://www.ti.com/lit/ds/symlink/adc081c021.pdf
10  *	https://www.ti.com/lit/ds/symlink/adc101c021.pdf
11  *	https://www.ti.com/lit/ds/symlink/adc121c021.pdf
12  *
13  * The devices have a very similar interface and differ mostly in the number of
14  * bits handled. For the 8-bit and 10-bit models the least-significant 4 or 2
15  * bits of value registers are reserved.
16  */
17 
18 #include <linux/err.h>
19 #include <linux/i2c.h>
20 #include <linux/module.h>
21 #include <linux/mod_devicetable.h>
22 
23 #include <linux/iio/iio.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/trigger_consumer.h>
26 #include <linux/iio/triggered_buffer.h>
27 #include <linux/regulator/consumer.h>
28 
29 struct adc081c {
30 	struct i2c_client *i2c;
31 	struct regulator *ref;
32 
33 	/* 8, 10 or 12 */
34 	int bits;
35 
36 	/* Ensure natural alignment of buffer elements */
37 	struct {
38 		u16 channel;
39 		s64 ts __aligned(8);
40 	} scan;
41 };
42 
43 #define REG_CONV_RES 0x00
44 
45 static int adc081c_read_raw(struct iio_dev *iio,
46 			    struct iio_chan_spec const *channel, int *value,
47 			    int *shift, long mask)
48 {
49 	struct adc081c *adc = iio_priv(iio);
50 	int err;
51 
52 	switch (mask) {
53 	case IIO_CHAN_INFO_RAW:
54 		err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
55 		if (err < 0)
56 			return err;
57 
58 		*value = (err & 0xFFF) >> (12 - adc->bits);
59 		return IIO_VAL_INT;
60 
61 	case IIO_CHAN_INFO_SCALE:
62 		err = regulator_get_voltage(adc->ref);
63 		if (err < 0)
64 			return err;
65 
66 		*value = err / 1000;
67 		*shift = adc->bits;
68 
69 		return IIO_VAL_FRACTIONAL_LOG2;
70 
71 	default:
72 		break;
73 	}
74 
75 	return -EINVAL;
76 }
77 
78 #define ADCxx1C_CHAN(_bits) {					\
79 	.type = IIO_VOLTAGE,					\
80 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
81 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
82 	.scan_type = {						\
83 		.sign = 'u',					\
84 		.realbits = (_bits),				\
85 		.storagebits = 16,				\
86 		.shift = 12 - (_bits),				\
87 		.endianness = IIO_CPU,				\
88 	},							\
89 }
90 
91 #define DEFINE_ADCxx1C_CHANNELS(_name, _bits)				\
92 	static const struct iio_chan_spec _name ## _channels[] = {	\
93 		ADCxx1C_CHAN((_bits)),					\
94 		IIO_CHAN_SOFT_TIMESTAMP(1),				\
95 	};								\
96 
97 #define ADC081C_NUM_CHANNELS 2
98 
99 struct adcxx1c_model {
100 	const struct iio_chan_spec* channels;
101 	int bits;
102 };
103 
104 #define ADCxx1C_MODEL(_name, _bits)					\
105 	{								\
106 		.channels = _name ## _channels,				\
107 		.bits = (_bits),					\
108 	}
109 
110 DEFINE_ADCxx1C_CHANNELS(adc081c,  8);
111 DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
112 DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
113 
114 /* Model ids are indexes in _models array */
115 enum adcxx1c_model_id {
116 	ADC081C = 0,
117 	ADC101C = 1,
118 	ADC121C = 2,
119 };
120 
121 static struct adcxx1c_model adcxx1c_models[] = {
122 	ADCxx1C_MODEL(adc081c,  8),
123 	ADCxx1C_MODEL(adc101c, 10),
124 	ADCxx1C_MODEL(adc121c, 12),
125 };
126 
127 static const struct iio_info adc081c_info = {
128 	.read_raw = adc081c_read_raw,
129 };
130 
131 static irqreturn_t adc081c_trigger_handler(int irq, void *p)
132 {
133 	struct iio_poll_func *pf = p;
134 	struct iio_dev *indio_dev = pf->indio_dev;
135 	struct adc081c *data = iio_priv(indio_dev);
136 	int ret;
137 
138 	ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
139 	if (ret < 0)
140 		goto out;
141 	data->scan.channel = ret;
142 	iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
143 					   iio_get_time_ns(indio_dev));
144 out:
145 	iio_trigger_notify_done(indio_dev->trig);
146 	return IRQ_HANDLED;
147 }
148 
149 static void adc081c_reg_disable(void *reg)
150 {
151 	regulator_disable(reg);
152 }
153 
154 static int adc081c_probe(struct i2c_client *client,
155 			 const struct i2c_device_id *id)
156 {
157 	struct iio_dev *iio;
158 	struct adc081c *adc;
159 	struct adcxx1c_model *model;
160 	int err;
161 
162 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
163 		return -EOPNOTSUPP;
164 
165 	model = &adcxx1c_models[id->driver_data];
166 
167 	iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
168 	if (!iio)
169 		return -ENOMEM;
170 
171 	adc = iio_priv(iio);
172 	adc->i2c = client;
173 	adc->bits = model->bits;
174 
175 	adc->ref = devm_regulator_get(&client->dev, "vref");
176 	if (IS_ERR(adc->ref))
177 		return PTR_ERR(adc->ref);
178 
179 	err = regulator_enable(adc->ref);
180 	if (err < 0)
181 		return err;
182 
183 	err = devm_add_action_or_reset(&client->dev, adc081c_reg_disable,
184 				       adc->ref);
185 	if (err)
186 		return err;
187 
188 	iio->name = dev_name(&client->dev);
189 	iio->modes = INDIO_DIRECT_MODE;
190 	iio->info = &adc081c_info;
191 
192 	iio->channels = model->channels;
193 	iio->num_channels = ADC081C_NUM_CHANNELS;
194 
195 	err = devm_iio_triggered_buffer_setup(&client->dev, iio, NULL,
196 					      adc081c_trigger_handler, NULL);
197 	if (err < 0) {
198 		dev_err(&client->dev, "iio triggered buffer setup failed\n");
199 		return err;
200 	}
201 
202 	return devm_iio_device_register(&client->dev, iio);
203 }
204 
205 static const struct i2c_device_id adc081c_id[] = {
206 	{ "adc081c", ADC081C },
207 	{ "adc101c", ADC101C },
208 	{ "adc121c", ADC121C },
209 	{ }
210 };
211 MODULE_DEVICE_TABLE(i2c, adc081c_id);
212 
213 static const struct of_device_id adc081c_of_match[] = {
214 	{ .compatible = "ti,adc081c" },
215 	{ .compatible = "ti,adc101c" },
216 	{ .compatible = "ti,adc121c" },
217 	{ }
218 };
219 MODULE_DEVICE_TABLE(of, adc081c_of_match);
220 
221 static struct i2c_driver adc081c_driver = {
222 	.driver = {
223 		.name = "adc081c",
224 		.of_match_table = adc081c_of_match,
225 	},
226 	.probe = adc081c_probe,
227 	.id_table = adc081c_id,
228 };
229 module_i2c_driver(adc081c_driver);
230 
231 MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
232 MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
233 MODULE_LICENSE("GPL v2");
234