ad7152.c 13.7 KB
Newer Older
1 2 3
/*
 * AD7152 capacitive sensor driver supporting AD7152/3
 *
4
 * Copyright 2010-2011a 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/delay.h>
17 18 19 20

#include "../iio.h"
#include "../sysfs.h"

21
/*
22
 * TODO: Check compliance of calibbias with abi (units)
23
 */
24 25 26 27
/*
 * AD7152 registers definition
 */

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
#define AD7152_REG_STATUS		0
#define AD7152_REG_CH1_DATA_HIGH	1
#define AD7152_REG_CH2_DATA_HIGH	3
#define AD7152_REG_CH1_OFFS_HIGH	5
#define AD7152_REG_CH2_OFFS_HIGH	7
#define AD7152_REG_CH1_GAIN_HIGH	9
#define AD7152_REG_CH1_SETUP		11
#define AD7152_REG_CH2_GAIN_HIGH	12
#define AD7152_REG_CH2_SETUP		14
#define AD7152_REG_CFG			15
#define AD7152_REG_RESEVERD		16
#define AD7152_REG_CAPDAC_POS		17
#define AD7152_REG_CAPDAC_NEG		18
#define AD7152_REG_CFG2			26

/* Status Register Bit Designations (AD7152_REG_STATUS) */
#define AD7152_STATUS_RDY1		(1 << 0)
#define AD7152_STATUS_RDY2		(1 << 1)
#define AD7152_STATUS_C1C2		(1 << 2)
#define AD7152_STATUS_PWDN		(1 << 7)

/* Setup Register Bit Designations (AD7152_REG_CHx_SETUP) */
#define AD7152_SETUP_CAPDIFF		(1 << 5)
#define AD7152_SETUP_RANGE_2pF		(0 << 6)
#define AD7152_SETUP_RANGE_0_5pF	(1 << 6)
#define AD7152_SETUP_RANGE_1pF		(2 << 6)
#define AD7152_SETUP_RANGE_4pF		(3 << 6)
55
#define AD7152_SETUP_RANGE(x)		((x) << 6)
56 57 58 59 60 61 62 63 64 65 66 67 68 69

/* Config Register Bit Designations (AD7152_REG_CFG) */
#define AD7152_CONF_CH2EN		(1 << 3)
#define AD7152_CONF_CH1EN		(1 << 4)
#define AD7152_CONF_MODE_IDLE		(0 << 0)
#define AD7152_CONF_MODE_CONT_CONV	(1 << 0)
#define AD7152_CONF_MODE_SINGLE_CONV	(2 << 0)
#define AD7152_CONF_MODE_OFFS_CAL	(5 << 0)
#define AD7152_CONF_MODE_GAIN_CAL	(6 << 0)

/* Capdac Register Bit Designations (AD7152_REG_CAPDAC_XXX) */
#define AD7152_CAPDAC_DACEN		(1 << 7)
#define AD7152_CAPDAC_DACP(x)		((x) & 0x1F)

70 71 72
/* CFG2 Register Bit Designations (AD7152_REG_CFG2) */
#define AD7152_CFG2_OSR(x)		(((x) & 0x3) << 4)

73 74 75 76 77 78
enum {
	AD7152_DATA,
	AD7152_OFFS,
	AD7152_GAIN,
	AD7152_SETUP
};
79 80 81 82 83 84 85

/*
 * struct ad7152_chip_info - chip specifc information
 */

struct ad7152_chip_info {
	struct i2c_client *client;
86 87 88 89
	/*
	 * Capacitive channel digital filter setup;
	 * conversion time/update rate setup per channel
	 */
90 91
	u8	filter_rate_setup;
	u8	setup[2];
92 93
};

94 95 96 97 98
static inline ssize_t ad7152_start_calib(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf,
					 size_t len,
					 u8 regval)
99
{
100 101
	struct iio_dev *indio_dev = dev_get_drvdata(dev);
	struct ad7152_chip_info *chip = iio_priv(indio_dev);
102 103
	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
	bool doit;
104
	int ret, timeout = 10;
105

106
	ret = strtobool(buf, &doit);
107 108
	if (ret < 0)
		return ret;
109

110 111
	if (!doit)
		return 0;
112

113
	if (this_attr->address == 0)
114
		regval |= AD7152_CONF_CH1EN;
115
	else
116
		regval |= AD7152_CONF_CH2EN;
117

118
	mutex_lock(&indio_dev->mlock);
119
	ret = i2c_smbus_write_byte_data(chip->client, AD7152_REG_CFG, regval);
120 121
	if (ret < 0) {
		mutex_unlock(&indio_dev->mlock);
122
		return ret;
123
	}
124 125 126 127

	do {
		mdelay(20);
		ret = i2c_smbus_read_byte_data(chip->client, AD7152_REG_CFG);
128 129
		if (ret < 0) {
			mutex_unlock(&indio_dev->mlock);
130
			return ret;
131
		}
132 133
	} while ((ret == regval) && timeout--);

134
	mutex_unlock(&indio_dev->mlock);
135
	return len;
136
}
137 138 139 140
static ssize_t ad7152_start_offset_calib(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf,
					 size_t len)
141
{
142 143
	return ad7152_start_calib(dev, attr, buf, len,
				  AD7152_CONF_MODE_OFFS_CAL);
144
}
145 146 147 148
static ssize_t ad7152_start_gain_calib(struct device *dev,
				       struct device_attribute *attr,
				       const char *buf,
				       size_t len)
149
{
150 151
	return ad7152_start_calib(dev, attr, buf, len,
				  AD7152_CONF_MODE_GAIN_CAL);
152 153
}

154 155 156 157 158 159 160 161 162
static IIO_DEVICE_ATTR(in_capacitance0_calibbias_calibration,
		       S_IWUSR, NULL, ad7152_start_offset_calib, 0);
static IIO_DEVICE_ATTR(in_capacitance1_calibbias_calibration,
		       S_IWUSR, NULL, ad7152_start_offset_calib, 1);
static IIO_DEVICE_ATTR(in_capacitance0_calibscale_calibration,
		       S_IWUSR, NULL, ad7152_start_gain_calib, 0);
static IIO_DEVICE_ATTR(in_capacitance1_calibscale_calibration,
		       S_IWUSR, NULL, ad7152_start_gain_calib, 1);

163
/* Values are Update Rate (Hz), Conversion Time (ms) + 1*/
164
static const unsigned char ad7152_filter_rate_table[][2] = {
165
	{200, 5 + 1}, {50, 20 + 1}, {20, 50 + 1}, {17, 60 + 1},
166
};
167 168 169 170 171

static ssize_t ad7152_show_filter_rate_setup(struct device *dev,
		struct device_attribute *attr,
		char *buf)
{
172 173
	struct iio_dev *indio_dev = dev_get_drvdata(dev);
	struct ad7152_chip_info *chip = iio_priv(indio_dev);
174

175 176
	return sprintf(buf, "%d\n",
		       ad7152_filter_rate_table[chip->filter_rate_setup][0]);
177 178 179 180 181 182 183
}

static ssize_t ad7152_store_filter_rate_setup(struct device *dev,
		struct device_attribute *attr,
		const char *buf,
		size_t len)
{
184 185
	struct iio_dev *indio_dev = dev_get_drvdata(dev);
	struct ad7152_chip_info *chip = iio_priv(indio_dev);
186
	u8 data;
187
	int ret, i;
188

189 190 191 192
	ret = kstrtou8(buf, 10, &data);
	if (ret < 0)
		return ret;

193 194 195 196 197 198 199
	for (i = 0; i < ARRAY_SIZE(ad7152_filter_rate_table); i++)
		if (data >= ad7152_filter_rate_table[i][0])
			break;

	if (i >= ARRAY_SIZE(ad7152_filter_rate_table))
		i = ARRAY_SIZE(ad7152_filter_rate_table) - 1;

200
	mutex_lock(&indio_dev->mlock);
201 202 203 204
	ret = i2c_smbus_write_byte_data(chip->client,
			AD7152_REG_CFG2, AD7152_CFG2_OSR(i));
	if (ret < 0) {
		mutex_unlock(&indio_dev->mlock);
205
		return ret;
206
	}
207

208
	chip->filter_rate_setup = i;
209
	mutex_unlock(&indio_dev->mlock);
210

211
	return len;
212 213
}

214
static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
215 216 217
		ad7152_show_filter_rate_setup,
		ad7152_store_filter_rate_setup);

218 219
static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("200 50 20 17");

220 221 222
static IIO_CONST_ATTR(in_capacitance_scale_available,
		      "0.000061050 0.000030525 0.000015263 0.000007631");

223
static struct attribute *ad7152_attributes[] = {
224
	&iio_dev_attr_sampling_frequency.dev_attr.attr,
225 226 227 228
	&iio_dev_attr_in_capacitance0_calibbias_calibration.dev_attr.attr,
	&iio_dev_attr_in_capacitance1_calibbias_calibration.dev_attr.attr,
	&iio_dev_attr_in_capacitance0_calibscale_calibration.dev_attr.attr,
	&iio_dev_attr_in_capacitance1_calibscale_calibration.dev_attr.attr,
229
	&iio_const_attr_in_capacitance_scale_available.dev_attr.attr,
230
	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
231 232 233 234 235 236 237
	NULL,
};

static const struct attribute_group ad7152_attribute_group = {
	.attrs = ad7152_attributes,
};

238
static const u8 ad7152_addresses[][4] = {
239 240 241 242
	{ AD7152_REG_CH1_DATA_HIGH, AD7152_REG_CH1_OFFS_HIGH,
	  AD7152_REG_CH1_GAIN_HIGH, AD7152_REG_CH1_SETUP },
	{ AD7152_REG_CH2_DATA_HIGH, AD7152_REG_CH2_OFFS_HIGH,
	  AD7152_REG_CH2_GAIN_HIGH, AD7152_REG_CH2_SETUP },
243 244
};

245
/* Values are nano relative to pf base. */
246
static const int ad7152_scale_table[] = {
247
	30525, 7631, 15263, 61050
248
};
249

250
static int ad7152_write_raw(struct iio_dev *indio_dev,
251 252 253 254 255
			    struct iio_chan_spec const *chan,
			    int val,
			    int val2,
			    long mask)
{
256
	struct ad7152_chip_info *chip = iio_priv(indio_dev);
257
	int ret, i;
258

259 260
	mutex_lock(&indio_dev->mlock);

261 262
	switch (mask) {
	case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
263 264 265 266
		if (val != 1) {
			ret = -EINVAL;
			goto out;
		}
267 268 269

		val = (val2 * 1024) / 15625;

270
		ret = i2c_smbus_write_word_data(chip->client,
271
				ad7152_addresses[chan->channel][AD7152_GAIN],
272
				swab16(val));
273
		if (ret < 0)
274
			goto out;
275

276 277
		ret = 0;
		break;
278 279

	case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
280 281 282 283
		if ((val < 0) | (val > 0xFFFF)) {
			ret = -EINVAL;
			goto out;
		}
284
		ret = i2c_smbus_write_word_data(chip->client,
285
				ad7152_addresses[chan->channel][AD7152_OFFS],
286
				swab16(val));
287
		if (ret < 0)
288
			goto out;
289

290 291
		ret = 0;
		break;
292
	case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
293 294 295 296
		if (val != 0) {
			ret = -EINVAL;
			goto out;
		}
297
		for (i = 0; i < ARRAY_SIZE(ad7152_scale_table); i++)
298
			if (val2 == ad7152_scale_table[i])
299
				break;
300 301 302 303 304

		chip->setup[chan->channel] &= ~AD7152_SETUP_RANGE_4pF;
		chip->setup[chan->channel] |= AD7152_SETUP_RANGE(i);

		ret = i2c_smbus_write_byte_data(chip->client,
305
				ad7152_addresses[chan->channel][AD7152_SETUP],
306
				chip->setup[chan->channel]);
307
		if (ret < 0)
308 309 310 311
			goto out;

		ret = 0;
		break;
312
	default:
313
		ret = -EINVAL;
314
	}
315 316 317 318

out:
	mutex_unlock(&indio_dev->mlock);
	return ret;
319
}
320
static int ad7152_read_raw(struct iio_dev *indio_dev,
321 322 323 324
			   struct iio_chan_spec const *chan,
			   int *val, int *val2,
			   long mask)
{
325
	struct ad7152_chip_info *chip = iio_priv(indio_dev);
326
	int ret;
327
	u8 regval = 0;
328 329 330

	mutex_lock(&indio_dev->mlock);

331 332
	switch (mask) {
	case 0:
333
		/* First set whether in differential mode */
334 335 336

		regval = chip->setup[chan->channel];

337
		if (chan->differential)
338 339 340
			chip->setup[chan->channel] |= AD7152_SETUP_CAPDIFF;
		else
			chip->setup[chan->channel] &= ~AD7152_SETUP_CAPDIFF;
341

342 343 344 345 346
		if (regval != chip->setup[chan->channel]) {
			ret = i2c_smbus_write_byte_data(chip->client,
				ad7152_addresses[chan->channel][AD7152_SETUP],
				chip->setup[chan->channel]);
			if (ret < 0)
347
				goto out;
348
		}
349
		/* Make sure the channel is enabled */
350 351
		if (chan->channel == 0)
			regval = AD7152_CONF_CH1EN;
352
		else
353 354
			regval = AD7152_CONF_CH2EN;

355
		/* Trigger a single read */
356
		regval |= AD7152_CONF_MODE_SINGLE_CONV;
357
		ret = i2c_smbus_write_byte_data(chip->client, AD7152_REG_CFG,
358
				regval);
359
		if (ret < 0)
360
			goto out;
361

362
		msleep(ad7152_filter_rate_table[chip->filter_rate_setup][1]);
363
		/* Now read the actual register */
364
		ret = i2c_smbus_read_word_data(chip->client,
365
				ad7152_addresses[chan->channel][AD7152_DATA]);
366
		if (ret < 0)
367
			goto out;
368
		*val = swab16(ret);
369

370 371 372
		if (chan->differential)
			*val -= 0x8000;

373 374
		ret = IIO_VAL_INT;
		break;
375
	case (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE):
376

377
		ret = i2c_smbus_read_word_data(chip->client,
378
				ad7152_addresses[chan->channel][AD7152_GAIN]);
379
		if (ret < 0)
380
			goto out;
381 382 383
		/* 1 + gain_val / 2^16 */
		*val = 1;
		*val2 = (15625 * swab16(ret)) / 1024;
384

385 386
		ret = IIO_VAL_INT_PLUS_MICRO;
		break;
387 388
	case (1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE):
		ret = i2c_smbus_read_word_data(chip->client,
389
				ad7152_addresses[chan->channel][AD7152_OFFS]);
390
		if (ret < 0)
391
			goto out;
392
		*val = swab16(ret);
393

394 395
		ret = IIO_VAL_INT;
		break;
396 397
	case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
		ret = i2c_smbus_read_byte_data(chip->client,
398
				ad7152_addresses[chan->channel][AD7152_SETUP]);
399
		if (ret < 0)
400
			goto out;
401 402 403
		*val = 0;
		*val2 = ad7152_scale_table[ret >> 6];

404 405
		ret = IIO_VAL_INT_PLUS_NANO;
		break;
406
	default:
407
		ret = -EINVAL;
408
	};
409 410 411
out:
	mutex_unlock(&indio_dev->mlock);
	return ret;
412
}
413 414 415 416 417 418 419 420 421 422 423 424 425

static int ad7152_write_raw_get_fmt(struct iio_dev *indio_dev,
			       struct iio_chan_spec const *chan,
			       long mask)
{
	switch (mask) {
	case (1 << IIO_CHAN_INFO_SCALE_SEPARATE):
		return IIO_VAL_INT_PLUS_NANO;
	default:
		return IIO_VAL_INT_PLUS_MICRO;
	}
}

426 427
static const struct iio_info ad7152_info = {
	.attrs = &ad7152_attribute_group,
428 429
	.read_raw = &ad7152_read_raw,
	.write_raw = &ad7152_write_raw,
430
	.write_raw_get_fmt = &ad7152_write_raw_get_fmt,
431 432
	.driver_module = THIS_MODULE,
};
433 434 435 436 437 438 439

static const struct iio_chan_spec ad7152_channels[] = {
	{
		.type = IIO_CAPACITANCE,
		.indexed = 1,
		.channel = 0,
		.info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) |
440 441 442 443
		(1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
		(1 << IIO_CHAN_INFO_SCALE_SEPARATE),
	}, {
		.type = IIO_CAPACITANCE,
444
		.differential = 1,
445
		.indexed = 1,
446 447
		.channel = 0,
		.channel2 = 2,
448 449 450 451 452 453
		.info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) |
		(1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
		(1 << IIO_CHAN_INFO_SCALE_SEPARATE),
	}, {
		.type = IIO_CAPACITANCE,
		.indexed = 1,
454
		.channel = 1,
455 456 457
		.info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) |
		(1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
		(1 << IIO_CHAN_INFO_SCALE_SEPARATE),
458 459
	}, {
		.type = IIO_CAPACITANCE,
460
		.differential = 1,
461 462
		.indexed = 1,
		.channel = 1,
463
		.channel2 = 3,
464
		.info_mask = (1 << IIO_CHAN_INFO_CALIBSCALE_SEPARATE) |
465 466
		(1 << IIO_CHAN_INFO_CALIBBIAS_SEPARATE) |
		(1 << IIO_CHAN_INFO_SCALE_SEPARATE),
467 468
	}
};
469 470 471 472 473 474 475 476
/*
 * device probe and remove
 */

static int __devinit ad7152_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	int ret = 0;
477 478 479 480 481
	struct ad7152_chip_info *chip;
	struct iio_dev *indio_dev;

	indio_dev = iio_allocate_device(sizeof(*chip));
	if (indio_dev == NULL) {
482 483 484
		ret = -ENOMEM;
		goto error_ret;
	}
485
	chip = iio_priv(indio_dev);
486
	/* this is only used for device removal purposes */
487
	i2c_set_clientdata(client, indio_dev);
488 489 490

	chip->client = client;

491
	/* Establish that the iio_dev is a child of the i2c device */
492 493 494
	indio_dev->name = id->name;
	indio_dev->dev.parent = &client->dev;
	indio_dev->info = &ad7152_info;
495
	indio_dev->channels = ad7152_channels;
496 497 498
	if (id->driver_data == 0)
		indio_dev->num_channels = ARRAY_SIZE(ad7152_channels);
	else
499
		indio_dev->num_channels = 2;
500
	indio_dev->num_channels = ARRAY_SIZE(ad7152_channels);
501
	indio_dev->modes = INDIO_DIRECT_MODE;
502

503
	ret = iio_device_register(indio_dev);
504 505 506 507 508 509 510 511
	if (ret)
		goto error_free_dev;

	dev_err(&client->dev, "%s capacitive sensor registered\n", id->name);

	return 0;

error_free_dev:
512
	iio_free_device(indio_dev);
513 514 515 516 517 518
error_ret:
	return ret;
}

static int __devexit ad7152_remove(struct i2c_client *client)
{
519
	struct iio_dev *indio_dev = i2c_get_clientdata(client);
520 521

	iio_device_unregister(indio_dev);
522
	iio_free_device(indio_dev);
523 524 525 526 527 528

	return 0;
}

static const struct i2c_device_id ad7152_id[] = {
	{ "ad7152", 0 },
529
	{ "ad7153", 1 },
530 531 532 533 534 535 536
	{}
};

MODULE_DEVICE_TABLE(i2c, ad7152_id);

static struct i2c_driver ad7152_driver = {
	.driver = {
537
		.name = KBUILD_MODNAME,
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
	},
	.probe = ad7152_probe,
	.remove = __devexit_p(ad7152_remove),
	.id_table = ad7152_id,
};

static __init int ad7152_init(void)
{
	return i2c_add_driver(&ad7152_driver);
}

static __exit void ad7152_exit(void)
{
	i2c_del_driver(&ad7152_driver);
}

MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
555
MODULE_DESCRIPTION("Analog Devices AD7152/3 capacitive sensor driver");
556 557 558 559
MODULE_LICENSE("GPL v2");

module_init(ad7152_init);
module_exit(ad7152_exit);