File Index
Files and directories
Sign in
FILE

ti-adc081c.c

File preview with syntax highlighting.

linux-6.19.14/drivers/iio/adc/ti-adc081c.c C
  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#include <linux/property.h>
 23
 24#include <linux/iio/iio.h>
 25#include <linux/iio/buffer.h>
 26#include <linux/iio/trigger_consumer.h>
 27#include <linux/iio/triggered_buffer.h>
 28#include <linux/regulator/consumer.h>
 29
 30struct adc081c {
 31	struct i2c_client *i2c;
 32	struct regulator *ref;
 33
 34	/* 8, 10 or 12 */
 35	int bits;
 36
 37	/* Ensure natural alignment of buffer elements */
 38	struct {
 39		u16 channel;
 40		aligned_s64 ts;
 41	} scan;
 42};
 43
 44#define REG_CONV_RES 0x00
 45
 46static int adc081c_read_raw(struct iio_dev *iio,
 47			    struct iio_chan_spec const *channel, int *value,
 48			    int *shift, long mask)
 49{
 50	struct adc081c *adc = iio_priv(iio);
 51	int err;
 52
 53	switch (mask) {
 54	case IIO_CHAN_INFO_RAW:
 55		err = i2c_smbus_read_word_swapped(adc->i2c, REG_CONV_RES);
 56		if (err < 0)
 57			return err;
 58
 59		*value = (err & 0xFFF) >> (12 - adc->bits);
 60		return IIO_VAL_INT;
 61
 62	case IIO_CHAN_INFO_SCALE:
 63		err = regulator_get_voltage(adc->ref);
 64		if (err < 0)
 65			return err;
 66
 67		*value = err / 1000;
 68		*shift = adc->bits;
 69
 70		return IIO_VAL_FRACTIONAL_LOG2;
 71
 72	default:
 73		break;
 74	}
 75
 76	return -EINVAL;
 77}
 78
 79#define ADCxx1C_CHAN(_bits) {					\
 80	.type = IIO_VOLTAGE,					\
 81	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
 82	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
 83	.scan_type = {						\
 84		.sign = 'u',					\
 85		.realbits = (_bits),				\
 86		.storagebits = 16,				\
 87		.shift = 12 - (_bits),				\
 88		.endianness = IIO_CPU,				\
 89	},							\
 90}
 91
 92#define DEFINE_ADCxx1C_CHANNELS(_name, _bits)				\
 93	static const struct iio_chan_spec _name ## _channels[] = {	\
 94		ADCxx1C_CHAN((_bits)),					\
 95		IIO_CHAN_SOFT_TIMESTAMP(1),				\
 96	};								\
 97
 98#define ADC081C_NUM_CHANNELS 2
 99
100struct adcxx1c_model {
101	const struct iio_chan_spec* channels;
102	int bits;
103};
104
105DEFINE_ADCxx1C_CHANNELS(adc081c,  8);
106DEFINE_ADCxx1C_CHANNELS(adc101c, 10);
107DEFINE_ADCxx1C_CHANNELS(adc121c, 12);
108
109static const struct adcxx1c_model adc081c_model = {
110	.channels = adc081c_channels,
111	.bits = 8,
112};
113
114static const struct adcxx1c_model adc101c_model = {
115	.channels = adc101c_channels,
116	.bits = 10,
117};
118
119static const struct adcxx1c_model adc121c_model = {
120	.channels = adc121c_channels,
121	.bits = 12,
122};
123
124static const struct iio_info adc081c_info = {
125	.read_raw = adc081c_read_raw,
126};
127
128static irqreturn_t adc081c_trigger_handler(int irq, void *p)
129{
130	struct iio_poll_func *pf = p;
131	struct iio_dev *indio_dev = pf->indio_dev;
132	struct adc081c *data = iio_priv(indio_dev);
133	int ret;
134
135	ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES);
136	if (ret < 0)
137		goto out;
138	data->scan.channel = ret;
139	iio_push_to_buffers_with_ts(indio_dev, &data->scan, sizeof(data->scan),
140				    iio_get_time_ns(indio_dev));
141out:
142	iio_trigger_notify_done(indio_dev->trig);
143	return IRQ_HANDLED;
144}
145
146static void adc081c_reg_disable(void *reg)
147{
148	regulator_disable(reg);
149}
150
151static int adc081c_probe(struct i2c_client *client)
152{
153	struct iio_dev *iio;
154	struct adc081c *adc;
155	const struct adcxx1c_model *model;
156	int err;
157
158	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
159		return -EOPNOTSUPP;
160
161	model = i2c_get_match_data(client);
162
163	iio = devm_iio_device_alloc(&client->dev, sizeof(*adc));
164	if (!iio)
165		return -ENOMEM;
166
167	adc = iio_priv(iio);
168	adc->i2c = client;
169	adc->bits = model->bits;
170
171	adc->ref = devm_regulator_get(&client->dev, "vref");
172	if (IS_ERR(adc->ref))
173		return PTR_ERR(adc->ref);
174
175	err = regulator_enable(adc->ref);
176	if (err < 0)
177		return err;
178
179	err = devm_add_action_or_reset(&client->dev, adc081c_reg_disable,
180				       adc->ref);
181	if (err)
182		return err;
183
184	iio->name = dev_name(&client->dev);
185	iio->modes = INDIO_DIRECT_MODE;
186	iio->info = &adc081c_info;
187
188	iio->channels = model->channels;
189	iio->num_channels = ADC081C_NUM_CHANNELS;
190
191	err = devm_iio_triggered_buffer_setup(&client->dev, iio, NULL,
192					      adc081c_trigger_handler, NULL);
193	if (err < 0) {
194		dev_err(&client->dev, "iio triggered buffer setup failed\n");
195		return err;
196	}
197
198	return devm_iio_device_register(&client->dev, iio);
199}
200
201static const struct i2c_device_id adc081c_id[] = {
202	{ "adc081c", (kernel_ulong_t)&adc081c_model },
203	{ "adc101c", (kernel_ulong_t)&adc101c_model },
204	{ "adc121c", (kernel_ulong_t)&adc121c_model },
205	{ }
206};
207MODULE_DEVICE_TABLE(i2c, adc081c_id);
208
209static const struct acpi_device_id adc081c_acpi_match[] = {
210	/* Used on some AAEON boards */
211	{ "ADC081C", (kernel_ulong_t)&adc081c_model },
212	{ }
213};
214MODULE_DEVICE_TABLE(acpi, adc081c_acpi_match);
215
216static const struct of_device_id adc081c_of_match[] = {
217	{ .compatible = "ti,adc081c", .data = &adc081c_model },
218	{ .compatible = "ti,adc101c", .data = &adc101c_model },
219	{ .compatible = "ti,adc121c", .data = &adc121c_model },
220	{ }
221};
222MODULE_DEVICE_TABLE(of, adc081c_of_match);
223
224static struct i2c_driver adc081c_driver = {
225	.driver = {
226		.name = "adc081c",
227		.of_match_table = adc081c_of_match,
228		.acpi_match_table = adc081c_acpi_match,
229	},
230	.probe = adc081c_probe,
231	.id_table = adc081c_id,
232};
233module_i2c_driver(adc081c_driver);
234
235MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>");
236MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver");
237MODULE_LICENSE("GPL v2");
~/ Read-only file preview.