ad7298.c 9.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * AD7298 SPI ADC driver
 *
 * Copyright 2011 Analog Devices Inc.
 *
 * Licensed under the GPL-2.
 */

#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/spi/spi.h>
#include <linux/regulator/consumer.h>
#include <linux/err.h>
#include <linux/delay.h>
17
#include <linux/module.h>
18
#include <linux/interrupt.h>
19

20 21 22
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/buffer.h>
23 24
#include <linux/iio/trigger_consumer.h>
#include <linux/iio/triggered_buffer.h>
25

26
#include <linux/platform_data/ad7298.h>
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#define AD7298_WRITE	(1 << 15) /* write to the control register */
#define AD7298_REPEAT	(1 << 14) /* repeated conversion enable */
#define AD7298_CH(x)	(1 << (13 - (x))) /* channel select */
#define AD7298_TSENSE	(1 << 5) /* temperature conversion enable */
#define AD7298_EXTREF	(1 << 2) /* external reference enable */
#define AD7298_TAVG	(1 << 1) /* temperature sensor averaging enable */
#define AD7298_PDD	(1 << 0) /* partial power down enable */

#define AD7298_MAX_CHAN		8
#define AD7298_BITS		12
#define AD7298_STORAGE_BITS	16
#define AD7298_INTREF_mV	2500

#define AD7298_CH_TEMP		9

#define RES_MASK(bits)	((1 << (bits)) - 1)

struct ad7298_state {
	struct spi_device		*spi;
	struct regulator		*reg;
	unsigned			ext_ref;
	struct spi_transfer		ring_xfer[10];
	struct spi_transfer		scan_single_xfer[3];
	struct spi_message		ring_msg;
	struct spi_message		scan_single_msg;
	/*
	 * DMA (thus cache coherency maintenance) requires the
	 * transfer buffers to live in their own cache lines.
	 */
57 58
	__be16				rx_buf[12] ____cacheline_aligned;
	__be16				tx_buf[2];
59 60
};

61 62 63 64 65
#define AD7298_V_CHAN(index)						\
	{								\
		.type = IIO_VOLTAGE,					\
		.indexed = 1,						\
		.channel = index,					\
66 67
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
68 69 70 71 72 73
		.address = index,					\
		.scan_index = index,					\
		.scan_type = {						\
			.sign = 'u',					\
			.realbits = 12,					\
			.storagebits = 16,				\
74
			.endianness = IIO_BE,				\
75 76 77
		},							\
	}

78
static const struct iio_chan_spec ad7298_channels[] = {
79 80 81 82
	{
		.type = IIO_TEMP,
		.indexed = 1,
		.channel = 0,
83 84 85
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
			BIT(IIO_CHAN_INFO_SCALE) |
			BIT(IIO_CHAN_INFO_OFFSET),
86 87
		.address = AD7298_CH_TEMP,
		.scan_index = -1,
88 89 90 91 92 93 94 95 96 97 98 99 100 101
		.scan_type = {
			.sign = 's',
			.realbits = 32,
			.storagebits = 32,
		},
	},
	AD7298_V_CHAN(0),
	AD7298_V_CHAN(1),
	AD7298_V_CHAN(2),
	AD7298_V_CHAN(3),
	AD7298_V_CHAN(4),
	AD7298_V_CHAN(5),
	AD7298_V_CHAN(6),
	AD7298_V_CHAN(7),
102 103 104
	IIO_CHAN_SOFT_TIMESTAMP(8),
};

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
/**
 * ad7298_update_scan_mode() setup the spi transfer buffer for the new scan mask
 **/
static int ad7298_update_scan_mode(struct iio_dev *indio_dev,
	const unsigned long *active_scan_mask)
{
	struct ad7298_state *st = iio_priv(indio_dev);
	int i, m;
	unsigned short command;
	int scan_count;

	/* Now compute overall size */
	scan_count = bitmap_weight(active_scan_mask, indio_dev->masklength);

	command = AD7298_WRITE | st->ext_ref;

	for (i = 0, m = AD7298_CH(0); i < AD7298_MAX_CHAN; i++, m >>= 1)
		if (test_bit(i, active_scan_mask))
			command |= m;

	st->tx_buf[0] = cpu_to_be16(command);

	/* build spi ring message */
	st->ring_xfer[0].tx_buf = &st->tx_buf[0];
	st->ring_xfer[0].len = 2;
	st->ring_xfer[0].cs_change = 1;
	st->ring_xfer[1].tx_buf = &st->tx_buf[1];
	st->ring_xfer[1].len = 2;
	st->ring_xfer[1].cs_change = 1;

	spi_message_init(&st->ring_msg);
	spi_message_add_tail(&st->ring_xfer[0], &st->ring_msg);
	spi_message_add_tail(&st->ring_xfer[1], &st->ring_msg);

	for (i = 0; i < scan_count; i++) {
		st->ring_xfer[i + 2].rx_buf = &st->rx_buf[i];
		st->ring_xfer[i + 2].len = 2;
		st->ring_xfer[i + 2].cs_change = 1;
		spi_message_add_tail(&st->ring_xfer[i + 2], &st->ring_msg);
	}
	/* make sure last transfer cs_change is not set */
	st->ring_xfer[i + 1].cs_change = 0;

	return 0;
}

/**
 * ad7298_trigger_handler() bh of trigger launched polling to ring buffer
 *
 * Currently there is no option in this driver to disable the saving of
 * timestamps within the ring.
 **/
static irqreturn_t ad7298_trigger_handler(int irq, void *p)
{
	struct iio_poll_func *pf = p;
	struct iio_dev *indio_dev = pf->indio_dev;
	struct ad7298_state *st = iio_priv(indio_dev);
	int b_sent;

	b_sent = spi_sync(st->spi, &st->ring_msg);
	if (b_sent)
		goto done;

168 169
	iio_push_to_buffers_with_timestamp(indio_dev, st->rx_buf,
		iio_get_time_ns());
170 171 172 173 174 175 176

done:
	iio_trigger_notify_done(indio_dev->trig);

	return IRQ_HANDLED;
}

177 178 179 180 181 182 183 184 185 186 187 188 189
static int ad7298_scan_direct(struct ad7298_state *st, unsigned ch)
{
	int ret;
	st->tx_buf[0] = cpu_to_be16(AD7298_WRITE | st->ext_ref |
				   (AD7298_CH(0) >> ch));

	ret = spi_sync(st->spi, &st->scan_single_msg);
	if (ret)
		return ret;

	return be16_to_cpu(st->rx_buf[0]);
}

190
static int ad7298_scan_temp(struct ad7298_state *st, int *val)
191
{
192
	int ret;
193
	__be16 buf;
194

195
	buf = cpu_to_be16(AD7298_WRITE | AD7298_TSENSE |
196
			  AD7298_TAVG | st->ext_ref);
197

198
	ret = spi_write(st->spi, (u8 *)&buf, 2);
199
	if (ret)
200 201
		return ret;

202
	buf = cpu_to_be16(0);
203

204
	ret = spi_write(st->spi, (u8 *)&buf, 2);
205 206
	if (ret)
		return ret;
207 208

	usleep_range(101, 1000); /* sleep > 100us */
209

210
	ret = spi_read(st->spi, (u8 *)&buf, 2);
211 212
	if (ret)
		return ret;
213

214
	*val = sign_extend32(be16_to_cpu(buf), 11);
215

216 217 218 219 220 221
	return 0;
}

static int ad7298_get_ref_voltage(struct ad7298_state *st)
{
	int vref;
222

223 224 225 226
	if (st->ext_ref) {
		vref = regulator_get_voltage(st->reg);
		if (vref < 0)
			return vref;
227

228
		return vref / 1000;
229
	} else {
230
		return AD7298_INTREF_mV;
231
	}
232
}
233

234
static int ad7298_read_raw(struct iio_dev *indio_dev,
235 236 237 238
			   struct iio_chan_spec const *chan,
			   int *val,
			   int *val2,
			   long m)
239
{
240
	int ret;
241
	struct ad7298_state *st = iio_priv(indio_dev);
242 243

	switch (m) {
244
	case IIO_CHAN_INFO_RAW:
245
		mutex_lock(&indio_dev->mlock);
246 247
		if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) {
			ret = -EBUSY;
248 249 250 251 252 253
		} else {
			if (chan->address == AD7298_CH_TEMP)
				ret = ad7298_scan_temp(st, val);
			else
				ret = ad7298_scan_direct(st, chan->address);
		}
254
		mutex_unlock(&indio_dev->mlock);
255 256 257 258 259 260 261 262

		if (ret < 0)
			return ret;

		if (chan->address != AD7298_CH_TEMP)
			*val = ret & RES_MASK(AD7298_BITS);

		return IIO_VAL_INT;
263 264 265
	case IIO_CHAN_INFO_SCALE:
		switch (chan->type) {
		case IIO_VOLTAGE:
266
			*val = ad7298_get_ref_voltage(st);
267 268
			*val2 = chan->scan_type.realbits;
			return IIO_VAL_FRACTIONAL_LOG2;
269
		case IIO_TEMP:
270 271 272
			*val = ad7298_get_ref_voltage(st);
			*val2 = 10;
			return IIO_VAL_FRACTIONAL;
273 274 275
		default:
			return -EINVAL;
		}
276 277 278
	case IIO_CHAN_INFO_OFFSET:
		*val = 1093 - 2732500 / ad7298_get_ref_voltage(st);
		return IIO_VAL_INT;
279 280
	}
	return -EINVAL;
281 282
}

283 284
static const struct iio_info ad7298_info = {
	.read_raw = &ad7298_read_raw,
285
	.update_scan_mode = ad7298_update_scan_mode,
286 287 288
	.driver_module = THIS_MODULE,
};

289
static int ad7298_probe(struct spi_device *spi)
290 291 292
{
	struct ad7298_platform_data *pdata = spi->dev.platform_data;
	struct ad7298_state *st;
S
Sachin Kamat 已提交
293
	struct iio_dev *indio_dev;
294
	int ret;
295

S
Sachin Kamat 已提交
296
	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
297 298 299 300
	if (indio_dev == NULL)
		return -ENOMEM;

	st = iio_priv(indio_dev);
301

302 303 304 305
	if (pdata && pdata->ext_ref)
		st->ext_ref = AD7298_EXTREF;

	if (st->ext_ref) {
S
Sachin Kamat 已提交
306 307 308 309
		st->reg = devm_regulator_get(&spi->dev, "vref");
		if (IS_ERR(st->reg))
			return PTR_ERR(st->reg);

310 311
		ret = regulator_enable(st->reg);
		if (ret)
S
Sachin Kamat 已提交
312
			return ret;
313 314
	}

315
	spi_set_drvdata(spi, indio_dev);
316 317 318

	st->spi = spi;

319 320 321 322 323
	indio_dev->name = spi_get_device_id(spi)->name;
	indio_dev->dev.parent = &spi->dev;
	indio_dev->modes = INDIO_DIRECT_MODE;
	indio_dev->channels = ad7298_channels;
	indio_dev->num_channels = ARRAY_SIZE(ad7298_channels);
324
	indio_dev->info = &ad7298_info;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

	/* Setup default message */

	st->scan_single_xfer[0].tx_buf = &st->tx_buf[0];
	st->scan_single_xfer[0].len = 2;
	st->scan_single_xfer[0].cs_change = 1;
	st->scan_single_xfer[1].tx_buf = &st->tx_buf[1];
	st->scan_single_xfer[1].len = 2;
	st->scan_single_xfer[1].cs_change = 1;
	st->scan_single_xfer[2].rx_buf = &st->rx_buf[0];
	st->scan_single_xfer[2].len = 2;

	spi_message_init(&st->scan_single_msg);
	spi_message_add_tail(&st->scan_single_xfer[0], &st->scan_single_msg);
	spi_message_add_tail(&st->scan_single_xfer[1], &st->scan_single_msg);
	spi_message_add_tail(&st->scan_single_xfer[2], &st->scan_single_msg);

342 343
	ret = iio_triggered_buffer_setup(indio_dev, NULL,
			&ad7298_trigger_handler, NULL);
344
	if (ret)
345
		goto error_disable_reg;
346

347 348
	ret = iio_device_register(indio_dev);
	if (ret)
349
		goto error_cleanup_ring;
350

351 352 353
	return 0;

error_cleanup_ring:
354
	iio_triggered_buffer_cleanup(indio_dev);
355
error_disable_reg:
356
	if (st->ext_ref)
357
		regulator_disable(st->reg);
358

359 360 361
	return ret;
}

362
static int ad7298_remove(struct spi_device *spi)
363
{
364 365
	struct iio_dev *indio_dev = spi_get_drvdata(spi);
	struct ad7298_state *st = iio_priv(indio_dev);
366

367
	iio_device_unregister(indio_dev);
368
	iio_triggered_buffer_cleanup(indio_dev);
S
Sachin Kamat 已提交
369
	if (st->ext_ref)
370
		regulator_disable(st->reg);
371

372 373 374 375 376 377 378
	return 0;
}

static const struct spi_device_id ad7298_id[] = {
	{"ad7298", 0},
	{}
};
379
MODULE_DEVICE_TABLE(spi, ad7298_id);
380 381 382 383 384 385 386

static struct spi_driver ad7298_driver = {
	.driver = {
		.name	= "ad7298",
		.owner	= THIS_MODULE,
	},
	.probe		= ad7298_probe,
387
	.remove		= ad7298_remove,
388 389
	.id_table	= ad7298_id,
};
390
module_spi_driver(ad7298_driver);
391 392 393 394

MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
MODULE_DESCRIPTION("Analog Devices AD7298 ADC");
MODULE_LICENSE("GPL v2");