ad7291.c 13.5 KB
Newer Older
1
/*
2
 * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
3
 *
4
 * Copyright 2010-2011 Analog Devices Inc.
5 6 7 8 9 10 11 12 13 14
 *
 * Licensed under the GPL-2 or later.
 */

#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/i2c.h>
15
#include <linux/module.h>
16
#include <linux/mutex.h>
17 18
#include <linux/regulator/consumer.h>
#include <linux/err.h>
19

20 21 22
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/events.h>
23

24 25
#include "ad7291.h"

26 27 28 29 30 31 32 33 34 35
/*
 * Simplified handling
 *
 * If no events enabled - single polled channel read
 * If event enabled direct reads disable unless channel
 * is in the read mask.
 *
 * The noise-delayed bit as per datasheet suggestion is always enabled.
 *
 */
36

37 38 39
/*
 * AD7291 registers definition
 */
40 41 42 43
#define AD7291_COMMAND			0x00
#define AD7291_VOLTAGE			0x01
#define AD7291_T_SENSE			0x02
#define AD7291_T_AVERAGE		0x03
44 45 46
#define AD7291_DATA_HIGH(x)		((x) * 3 + 0x4)
#define AD7291_DATA_LOW(x)		((x) * 3 + 0x5)
#define AD7291_HYST(x)			((x) * 3 + 0x6)
47
#define AD7291_VOLTAGE_ALERT_STATUS	0x1F
48 49
#define AD7291_T_ALERT_STATUS		0x20

50 51 52
#define AD7291_VOLTAGE_LIMIT_COUNT	8


53 54 55
/*
 * AD7291 command
 */
56 57 58 59 60 61 62 63
#define AD7291_AUTOCYCLE		(1 << 0)
#define AD7291_RESET			(1 << 1)
#define AD7291_ALERT_CLEAR		(1 << 2)
#define AD7291_ALERT_POLARITY		(1 << 3)
#define AD7291_EXT_REF			(1 << 4)
#define AD7291_NOISE_DELAY		(1 << 5)
#define AD7291_T_SENSE_MASK		(1 << 7)
#define AD7291_VOLTAGE_MASK		0xFF00
64 65 66 67 68
#define AD7291_VOLTAGE_OFFSET		0x8

/*
 * AD7291 value masks
 */
69
#define AD7291_CHANNEL_MASK		0xF000
70
#define AD7291_BITS			12
71
#define AD7291_VALUE_MASK		0xFFF
72 73 74 75
#define AD7291_T_VALUE_SIGN		0x400
#define AD7291_T_VALUE_FLOAT_OFFSET	2
#define AD7291_T_VALUE_FLOAT_MASK	0x2

76 77
#define AD7291_BITS			12

78
struct ad7291_chip_info {
79 80 81
	struct i2c_client	*client;
	struct regulator	*reg;
	u16			command;
82
	u16			c_mask;	/* Active voltage channels for events */
83
	struct mutex		state_lock;
84 85 86 87 88 89 90
};

static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
{
	struct i2c_client *client = chip->client;
	int ret = 0;

91
	ret = i2c_smbus_read_word_swapped(client, reg);
92 93 94 95 96
	if (ret < 0) {
		dev_err(&client->dev, "I2C read error\n");
		return ret;
	}

97
	*data = ret;
98 99 100 101 102 103

	return 0;
}

static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
{
104
	return i2c_smbus_write_word_swapped(chip->client, reg, data);
105 106
}

107
static irqreturn_t ad7291_event_handler(int irq, void *private)
108
{
109
	struct iio_dev *indio_dev = private;
110
	struct ad7291_chip_info *chip = iio_priv(private);
111 112 113
	u16 t_status, v_status;
	u16 command;
	int i;
114
	s64 timestamp = iio_get_time_ns();
115 116

	if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
117
		return IRQ_HANDLED;
118 119

	if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
120
		return IRQ_HANDLED;
121 122

	if (!(t_status || v_status))
123
		return IRQ_HANDLED;
124

125
	command = chip->command | AD7291_ALERT_CLEAR;
126 127
	ad7291_i2c_write(chip, AD7291_COMMAND, command);

128
	command = chip->command & ~AD7291_ALERT_CLEAR;
129 130
	ad7291_i2c_write(chip, AD7291_COMMAND, command);

131 132
	/* For now treat t_sense and t_sense_average the same */
	if ((t_status & (1 << 0)) || (t_status & (1 << 2)))
133
		iio_push_event(indio_dev,
134 135 136 137 138
			       IIO_UNMOD_EVENT_CODE(IIO_TEMP,
						    0,
						    IIO_EV_TYPE_THRESH,
						    IIO_EV_DIR_FALLING),
			       timestamp);
139
	if ((t_status & (1 << 1)) || (t_status & (1 << 3)))
140
		iio_push_event(indio_dev,
141 142 143 144 145 146 147
			       IIO_UNMOD_EVENT_CODE(IIO_TEMP,
						    0,
						    IIO_EV_TYPE_THRESH,
						    IIO_EV_DIR_RISING),
			       timestamp);

	for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT*2; i += 2) {
148
		if (v_status & (1 << i))
149
			iio_push_event(indio_dev,
150
				       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
151 152 153 154 155
							    i/2,
							    IIO_EV_TYPE_THRESH,
							    IIO_EV_DIR_FALLING),
				       timestamp);
		if (v_status & (1 << (i + 1)))
156
			iio_push_event(indio_dev,
157
				       IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
158 159 160 161
							    i/2,
							    IIO_EV_TYPE_THRESH,
							    IIO_EV_DIR_RISING),
				       timestamp);
162 163
	}

164
	return IRQ_HANDLED;
165 166
}

167
static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
168
	enum iio_event_direction dir, enum iio_event_info info)
169 170 171
{
	unsigned int offset;

172
	switch (chan->type) {
173
	case IIO_VOLTAGE:
174
		offset = chan->channel;
175 176 177 178 179 180 181 182
		break;
	case IIO_TEMP:
		offset = 8;
		break;
	default:
	    return 0;
	}

183 184 185 186 187 188 189 190 191 192 193 194
	switch (info) {
	case IIO_EV_INFO_VALUE:
			if (dir == IIO_EV_DIR_FALLING)
					return AD7291_DATA_HIGH(offset);
			else
					return AD7291_DATA_LOW(offset);
	case IIO_EV_INFO_HYSTERESIS:
			return AD7291_HYST(offset);
	default:
			break;
	}
	return 0;
195
}
196

197
static int ad7291_read_event_value(struct iio_dev *indio_dev,
198 199 200 201 202
				   const struct iio_chan_spec *chan,
				   enum iio_event_type type,
				   enum iio_event_direction dir,
				   enum iio_event_info info,
				   int *val, int *val2)
203
{
204 205 206 207
	struct ad7291_chip_info *chip = iio_priv(indio_dev);
	int ret;
	u16 uval;

208 209
	ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
		&uval);
210 211 212
	if (ret < 0)
		return ret;

213
	if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
214
		*val = uval & AD7291_VALUE_MASK;
215 216

	else
217
		*val = sign_extend32(uval, 11);
218 219

	return IIO_VAL_INT;
220
}
221

222
static int ad7291_write_event_value(struct iio_dev *indio_dev,
223 224 225 226 227
				    const struct iio_chan_spec *chan,
				    enum iio_event_type type,
				    enum iio_event_direction dir,
				    enum iio_event_info info,
				    int val, int val2)
228 229 230
{
	struct ad7291_chip_info *chip = iio_priv(indio_dev);

231
	if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
232
		if (val > AD7291_VALUE_MASK || val < 0)
233
			return -EINVAL;
234
	} else {
235 236
		if (val > 2047 || val < -2048)
			return -EINVAL;
237 238
	}

239 240
	return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
		val);
241 242
}

243
static int ad7291_read_event_config(struct iio_dev *indio_dev,
244 245 246
				    const struct iio_chan_spec *chan,
				    enum iio_event_type type,
				    enum iio_event_direction dir)
247
{
248 249 250 251
	struct ad7291_chip_info *chip = iio_priv(indio_dev);
	/* To be enabled the channel must simply be on. If any are enabled
	   we are in continuous sampling mode */

252
	switch (chan->type) {
253
	case IIO_VOLTAGE:
254
		if (chip->c_mask & (1 << (15 - chan->channel)))
255 256 257 258 259 260 261
			return 1;
		else
			return 0;
	case IIO_TEMP:
		/* always on */
		return 1;
	default:
262
		return -EINVAL;
263
	}
264

265
}
266

267
static int ad7291_write_event_config(struct iio_dev *indio_dev,
268 269 270
				     const struct iio_chan_spec *chan,
				     enum iio_event_type type,
				     enum iio_event_direction dir,
271 272 273 274
				     int state)
{
	int ret = 0;
	struct ad7291_chip_info *chip = iio_priv(indio_dev);
275
	unsigned int mask;
276 277 278 279 280 281 282 283 284
	u16 regval;

	mutex_lock(&chip->state_lock);
	regval = chip->command;
	/*
	 * To be enabled the channel must simply be on. If any are enabled
	 * use continuous sampling mode.
	 * Possible to disable temp as well but that makes single read tricky.
	 */
285

286 287 288
	mask = BIT(15 - chan->channel);

	switch (chan->type) {
289
	case IIO_VOLTAGE:
290 291 292 293
		if ((!state) && (chip->c_mask & mask))
			chip->c_mask &= ~mask;
		else if (state && (!(chip->c_mask & mask)))
			chip->c_mask |= mask;
294 295 296
		else
			break;

297
		regval &= ~AD7291_AUTOCYCLE;
298
		regval |= chip->c_mask;
299 300 301 302 303 304 305 306 307 308 309 310
		if (chip->c_mask) /* Enable autocycle? */
			regval |= AD7291_AUTOCYCLE;

		ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
		if (ret < 0)
			goto error_ret;

		chip->command = regval;
		break;
	default:
		ret = -EINVAL;
	}
311

312 313
error_ret:
	mutex_unlock(&chip->state_lock);
314 315 316
	return ret;
}

317 318 319 320 321 322 323 324 325 326 327
static int ad7291_read_raw(struct iio_dev *indio_dev,
			   struct iio_chan_spec const *chan,
			   int *val,
			   int *val2,
			   long mask)
{
	int ret;
	struct ad7291_chip_info *chip = iio_priv(indio_dev);
	u16 regval;

	switch (mask) {
328
	case IIO_CHAN_INFO_RAW:
329 330 331 332
		switch (chan->type) {
		case IIO_VOLTAGE:
			mutex_lock(&chip->state_lock);
			/* If in autocycle mode drop through */
333
			if (chip->command & AD7291_AUTOCYCLE) {
334 335 336 337 338
				mutex_unlock(&chip->state_lock);
				return -EBUSY;
			}
			/* Enable this channel alone */
			regval = chip->command & (~AD7291_VOLTAGE_MASK);
339
			regval |= 1 << (15 - chan->channel);
340 341 342 343 344 345
			ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
			if (ret < 0) {
				mutex_unlock(&chip->state_lock);
				return ret;
			}
			/* Read voltage */
346
			ret = i2c_smbus_read_word_swapped(chip->client,
347 348 349 350 351
						       AD7291_VOLTAGE);
			if (ret < 0) {
				mutex_unlock(&chip->state_lock);
				return ret;
			}
352
			*val = ret & AD7291_VALUE_MASK;
353 354 355 356
			mutex_unlock(&chip->state_lock);
			return IIO_VAL_INT;
		case IIO_TEMP:
			/* Assumes tsense bit of command register always set */
357
			ret = i2c_smbus_read_word_swapped(chip->client,
358 359 360
						       AD7291_T_SENSE);
			if (ret < 0)
				return ret;
361
			*val = sign_extend32(ret, 11);
362 363 364 365
			return IIO_VAL_INT;
		default:
			return -EINVAL;
		}
366
	case IIO_CHAN_INFO_AVERAGE_RAW:
367
		ret = i2c_smbus_read_word_swapped(chip->client,
368 369 370
					       AD7291_T_AVERAGE);
			if (ret < 0)
				return ret;
371
			*val = sign_extend32(ret, 11);
372
			return IIO_VAL_INT;
373 374 375
	case IIO_CHAN_INFO_SCALE:
		switch (chan->type) {
		case IIO_VOLTAGE:
376 377 378 379 380 381 382 383 384
			if (chip->reg) {
				int vref;
				vref = regulator_get_voltage(chip->reg);
				if (vref < 0)
					return vref;
				*val = vref / 1000;
			} else {
				*val = 2500;
			}
385 386
			*val2 = AD7291_BITS;
			return IIO_VAL_FRACTIONAL_LOG2;
387 388 389 390 391 392 393 394 395 396 397
		case IIO_TEMP:
			/*
			 * One LSB of the ADC corresponds to 0.25 deg C.
			 * The temperature reading is in 12-bit twos
			 * complement format
			 */
			*val = 250;
			return IIO_VAL_INT;
		default:
			return -EINVAL;
		}
398 399 400 401
	default:
		return -EINVAL;
	}
}
402

403 404 405 406 407 408 409 410 411 412 413
static const struct iio_event_spec ad7291_events[] = {
	{
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_RISING,
		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
			BIT(IIO_EV_INFO_ENABLE),
	}, {
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_FALLING,
		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
			BIT(IIO_EV_INFO_ENABLE),
414 415 416 417
	}, {
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_EITHER,
		.mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
418 419 420
	},
};

421 422 423
#define AD7291_VOLTAGE_CHAN(_chan)					\
{									\
	.type = IIO_VOLTAGE,						\
424 425
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),		\
426 427
	.indexed = 1,							\
	.channel = _chan,						\
428 429
	.event_spec = ad7291_events,					\
	.num_event_specs = ARRAY_SIZE(ad7291_events),			\
430 431 432 433 434 435 436 437 438 439 440 441 442
}

static const struct iio_chan_spec ad7291_channels[] = {
	AD7291_VOLTAGE_CHAN(0),
	AD7291_VOLTAGE_CHAN(1),
	AD7291_VOLTAGE_CHAN(2),
	AD7291_VOLTAGE_CHAN(3),
	AD7291_VOLTAGE_CHAN(4),
	AD7291_VOLTAGE_CHAN(5),
	AD7291_VOLTAGE_CHAN(6),
	AD7291_VOLTAGE_CHAN(7),
	{
		.type = IIO_TEMP,
443 444 445
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
				BIT(IIO_CHAN_INFO_SCALE),
446 447
		.indexed = 1,
		.channel = 0,
448 449
		.event_spec = ad7291_events,
		.num_event_specs = ARRAY_SIZE(ad7291_events),
450
	}
451 452
};

453
static const struct iio_info ad7291_info = {
454
	.read_raw = &ad7291_read_raw,
455 456 457 458
	.read_event_config = &ad7291_read_event_config,
	.write_event_config = &ad7291_write_event_config,
	.read_event_value = &ad7291_read_event_value,
	.write_event_value = &ad7291_write_event_value,
459
	.driver_module = THIS_MODULE,
460 461
};

462
static int ad7291_probe(struct i2c_client *client,
463 464
		const struct i2c_device_id *id)
{
465
	struct ad7291_platform_data *pdata = client->dev.platform_data;
466
	struct ad7291_chip_info *chip;
467
	struct iio_dev *indio_dev;
468
	int ret;
469

470 471 472
	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
	if (!indio_dev)
		return -ENOMEM;
473
	chip = iio_priv(indio_dev);
474

475
	if (pdata && pdata->use_external_ref) {
476
		chip->reg = devm_regulator_get(&client->dev, "vref");
477
		if (IS_ERR(chip->reg))
478
			return PTR_ERR(chip->reg);
479

480 481
		ret = regulator_enable(chip->reg);
		if (ret)
482
			return ret;
483 484
	}

485
	mutex_init(&chip->state_lock);
486
	/* this is only used for device removal purposes */
487
	i2c_set_clientdata(client, indio_dev);
488 489

	chip->client = client;
490 491 492 493

	chip->command = AD7291_NOISE_DELAY |
			AD7291_T_SENSE_MASK | /* Tsense always enabled */
			AD7291_ALERT_POLARITY; /* set irq polarity low level */
494

495
	if (pdata && pdata->use_external_ref)
496 497
		chip->command |= AD7291_EXT_REF;

498
	indio_dev->name = id->name;
499 500 501
	indio_dev->channels = ad7291_channels;
	indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);

502 503 504
	indio_dev->dev.parent = &client->dev;
	indio_dev->info = &ad7291_info;
	indio_dev->modes = INDIO_DIRECT_MODE;
505

506 507 508 509 510 511 512 513 514 515 516 517
	ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
	if (ret) {
		ret = -EIO;
		goto error_disable_reg;
	}

	ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
	if (ret) {
		ret = -EIO;
		goto error_disable_reg;
	}

518
	if (client->irq > 0) {
519 520 521 522
		ret = request_threaded_irq(client->irq,
					   NULL,
					   &ad7291_event_handler,
					   IRQF_TRIGGER_LOW | IRQF_ONESHOT,
523
					   id->name,
524
					   indio_dev);
525
		if (ret)
526
			goto error_disable_reg;
527 528
	}

529 530 531 532
	ret = iio_device_register(indio_dev);
	if (ret)
		goto error_unreg_irq;

533 534 535
	return 0;

error_unreg_irq:
536 537
	if (client->irq)
		free_irq(client->irq, indio_dev);
538
error_disable_reg:
539
	if (chip->reg)
540
		regulator_disable(chip->reg);
541

542 543 544
	return ret;
}

545
static int ad7291_remove(struct i2c_client *client)
546
{
547
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
548
	struct ad7291_chip_info *chip = iio_priv(indio_dev);
549

550 551
	iio_device_unregister(indio_dev);

552
	if (client->irq)
553
		free_irq(client->irq, indio_dev);
554

555
	if (chip->reg)
556
		regulator_disable(chip->reg);
557 558 559 560 561 562 563 564 565 566 567 568 569

	return 0;
}

static const struct i2c_device_id ad7291_id[] = {
	{ "ad7291", 0 },
	{}
};

MODULE_DEVICE_TABLE(i2c, ad7291_id);

static struct i2c_driver ad7291_driver = {
	.driver = {
570
		.name = KBUILD_MODNAME,
571 572
	},
	.probe = ad7291_probe,
573
	.remove = ad7291_remove,
574 575
	.id_table = ad7291_id,
};
576
module_i2c_driver(ad7291_driver);
577 578

MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
579
MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
580
MODULE_LICENSE("GPL v2");