bmp085.c 13.3 KB
Newer Older
1
/*  Copyright (c) 2010  Christoph Mair <christoph.mair@gmail.com>
E
Eric Andersson 已提交
2 3 4
 *  Copyright (c) 2012  Bosch Sensortec GmbH
 *  Copyright (c) 2012  Unixphere AB
 *
5 6 7
 *  This driver supports the bmp085 and bmp18x digital barometric pressure
 *  and temperature sensors from Bosch Sensortec. The datasheets
 *  are available from their website:
E
Eric Andersson 已提交
8
 *  http://www.bosch-sensortec.com/content/language1/downloads/BST-BMP085-DS000-05.pdf
9
 *  http://www.bosch-sensortec.com/content/language1/downloads/BST-BMP180-DS000-07.pdf
E
Eric Andersson 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 *
 *  A pressure measurement is issued by reading from pressure0_input.
 *  The return value ranges from 30000 to 110000 pascal with a resulution
 *  of 1 pascal (0.01 millibar) which enables measurements from 9000m above
 *  to 500m below sea level.
 *
 *  The temperature can be read from temp0_input. Values range from
 *  -400 to 850 representing the ambient temperature in degree celsius
 *  multiplied by 10.The resolution is 0.1 celsius.
 *
 *  Because ambient pressure is temperature dependent, a temperature
 *  measurement will be executed automatically even if the user is reading
 *  from pressure0_input. This happens if the last temperature measurement
 *  has been executed more then one second ago.
 *
 *  To decrease RMS noise from pressure measurements, the bmp085 can
 *  autonomously calculate the average of up to eight samples. This is
 *  set up by writing to the oversampling sysfs file. Accepted values
 *  are 0, 1, 2 and 3. 2^x when x is the value written to this file
 *  specifies the number of samples used to calculate the ambient pressure.
 *  RMS noise is specified with six pascal (without averaging) and decreases
 *  down to 3 pascal when using an oversampling setting of 3.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
47 48

#include <linux/module.h>
E
Eric Andersson 已提交
49
#include <linux/device.h>
50
#include <linux/slab.h>
51
#include <linux/of.h>
52
#include "bmp085.h"
53 54 55
#include <linux/interrupt.h>
#include <linux/completion.h>
#include <linux/gpio.h>
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

#define BMP085_CHIP_ID			0x55
#define BMP085_CALIBRATION_DATA_START	0xAA
#define BMP085_CALIBRATION_DATA_LENGTH	11	/* 16 bit values */
#define BMP085_CHIP_ID_REG		0xD0
#define BMP085_CTRL_REG			0xF4
#define BMP085_TEMP_MEASUREMENT		0x2E
#define BMP085_PRESSURE_MEASUREMENT	0x34
#define BMP085_CONVERSION_REGISTER_MSB	0xF6
#define BMP085_CONVERSION_REGISTER_LSB	0xF7
#define BMP085_CONVERSION_REGISTER_XLSB	0xF8
#define BMP085_TEMP_CONVERSION_TIME	5

struct bmp085_calibration_data {
	s16 AC1, AC2, AC3;
	u16 AC4, AC5, AC6;
	s16 B1, B2;
	s16 MB, MC, MD;
};

struct bmp085_data {
77 78
	struct	device *dev;
	struct  regmap *regmap;
E
Eric Andersson 已提交
79 80 81 82 83 84
	struct	mutex lock;
	struct	bmp085_calibration_data calibration;
	u8	oversampling_setting;
	u32	raw_temperature;
	u32	raw_pressure;
	u32	temp_measurement_period;
85
	unsigned long last_temp_measurement;
86
	u8	chip_id;
E
Eric Andersson 已提交
87
	s32	b6; /* calculated temperature correction coefficient */
88 89
	int	irq;
	struct	completion done;
90 91
};

92 93 94 95 96 97 98 99 100
static irqreturn_t bmp085_eoc_isr(int irq, void *devid)
{
	struct bmp085_data *data = devid;

	complete(&data->done);

	return IRQ_HANDLED;
}

101
static s32 bmp085_read_calibration_data(struct bmp085_data *data)
102 103 104
{
	u16 tmp[BMP085_CALIBRATION_DATA_LENGTH];
	struct bmp085_calibration_data *cali = &(data->calibration);
105 106 107
	s32 status = regmap_bulk_read(data->regmap,
				BMP085_CALIBRATION_DATA_START, (u8 *)tmp,
				(BMP085_CALIBRATION_DATA_LENGTH << 1));
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	if (status < 0)
		return status;

	cali->AC1 =  be16_to_cpu(tmp[0]);
	cali->AC2 =  be16_to_cpu(tmp[1]);
	cali->AC3 =  be16_to_cpu(tmp[2]);
	cali->AC4 =  be16_to_cpu(tmp[3]);
	cali->AC5 =  be16_to_cpu(tmp[4]);
	cali->AC6 = be16_to_cpu(tmp[5]);
	cali->B1 = be16_to_cpu(tmp[6]);
	cali->B2 = be16_to_cpu(tmp[7]);
	cali->MB = be16_to_cpu(tmp[8]);
	cali->MC = be16_to_cpu(tmp[9]);
	cali->MD = be16_to_cpu(tmp[10]);
	return 0;
}

static s32 bmp085_update_raw_temperature(struct bmp085_data *data)
{
	u16 tmp;
	s32 status;

	mutex_lock(&data->lock);
131 132 133

	init_completion(&data->done);

134 135
	status = regmap_write(data->regmap, BMP085_CTRL_REG,
			      BMP085_TEMP_MEASUREMENT);
E
Eric Andersson 已提交
136
	if (status < 0) {
137
		dev_err(data->dev,
138 139 140
			"Error while requesting temperature measurement.\n");
		goto exit;
	}
141 142
	wait_for_completion_timeout(&data->done, 1 + msecs_to_jiffies(
					    BMP085_TEMP_CONVERSION_TIME));
143

144 145 146 147
	status = regmap_bulk_read(data->regmap, BMP085_CONVERSION_REGISTER_MSB,
				 &tmp, sizeof(tmp));
	if (status < 0) {
		dev_err(data->dev,
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
			"Error while reading temperature measurement result\n");
		goto exit;
	}
	data->raw_temperature = be16_to_cpu(tmp);
	data->last_temp_measurement = jiffies;
	status = 0;	/* everything ok, return 0 */

exit:
	mutex_unlock(&data->lock);
	return status;
}

static s32 bmp085_update_raw_pressure(struct bmp085_data *data)
{
	u32 tmp = 0;
	s32 status;

	mutex_lock(&data->lock);
166 167 168

	init_completion(&data->done);

169
	status = regmap_write(data->regmap, BMP085_CTRL_REG,
E
Eric Andersson 已提交
170 171 172
			BMP085_PRESSURE_MEASUREMENT +
			(data->oversampling_setting << 6));
	if (status < 0) {
173
		dev_err(data->dev,
174 175 176 177 178
			"Error while requesting pressure measurement.\n");
		goto exit;
	}

	/* wait for the end of conversion */
179 180
	wait_for_completion_timeout(&data->done, 1 + msecs_to_jiffies(
					2+(3 << data->oversampling_setting)));
181
	/* copy data into a u32 (4 bytes), but skip the first byte. */
182 183 184 185
	status = regmap_bulk_read(data->regmap, BMP085_CONVERSION_REGISTER_MSB,
				 ((u8 *)&tmp)+1, 3);
	if (status < 0) {
		dev_err(data->dev,
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
			"Error while reading pressure measurement results\n");
		goto exit;
	}
	data->raw_pressure = be32_to_cpu((tmp));
	data->raw_pressure >>= (8-data->oversampling_setting);
	status = 0;	/* everything ok, return 0 */

exit:
	mutex_unlock(&data->lock);
	return status;
}

/*
 * This function starts the temperature measurement and returns the value
 * in tenth of a degree celsius.
 */
static s32 bmp085_get_temperature(struct bmp085_data *data, int *temperature)
{
	struct bmp085_calibration_data *cali = &data->calibration;
	long x1, x2;
	int status;

	status = bmp085_update_raw_temperature(data);
E
Eric Andersson 已提交
209
	if (status < 0)
210 211 212 213 214 215 216 217 218 219
		goto exit;

	x1 = ((data->raw_temperature - cali->AC6) * cali->AC5) >> 15;
	x2 = (cali->MC << 11) / (x1 + cali->MD);
	data->b6 = x1 + x2 - 4000;
	/* if NULL just update b6. Used for pressure only measurements */
	if (temperature != NULL)
		*temperature = (x1+x2+8) >> 4;

exit:
220
	return status;
221 222 223 224 225
}

/*
 * This function starts the pressure measurement and returns the value
 * in millibar. Since the pressure depends on the ambient temperature,
E
Eric Andersson 已提交
226 227 228 229
 * a temperature measurement is executed according to the given temperature
 * measurement period (default is 1 sec boundary). This period could vary
 * and needs to be adjusted according to the sensor environment, i.e. if big
 * temperature variations then the temperature needs to be read out often.
230 231 232 233 234 235 236 237 238 239
 */
static s32 bmp085_get_pressure(struct bmp085_data *data, int *pressure)
{
	struct bmp085_calibration_data *cali = &data->calibration;
	s32 x1, x2, x3, b3;
	u32 b4, b7;
	s32 p;
	int status;

	/* alt least every second force an update of the ambient temperature */
E
Eric Andersson 已提交
240 241
	if ((data->last_temp_measurement == 0) ||
	    time_is_before_jiffies(data->last_temp_measurement + 1*HZ)) {
242
		status = bmp085_get_temperature(data, NULL);
E
Eric Andersson 已提交
243 244
		if (status < 0)
			return status;
245 246 247
	}

	status = bmp085_update_raw_pressure(data);
E
Eric Andersson 已提交
248 249
	if (status < 0)
		return status;
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

	x1 = (data->b6 * data->b6) >> 12;
	x1 *= cali->B2;
	x1 >>= 11;

	x2 = cali->AC2 * data->b6;
	x2 >>= 11;

	x3 = x1 + x2;

	b3 = (((((s32)cali->AC1) * 4 + x3) << data->oversampling_setting) + 2);
	b3 >>= 2;

	x1 = (cali->AC3 * data->b6) >> 13;
	x2 = (cali->B1 * ((data->b6 * data->b6) >> 12)) >> 16;
	x3 = (x1 + x2 + 2) >> 2;
	b4 = (cali->AC4 * (u32)(x3 + 32768)) >> 15;

	b7 = ((u32)data->raw_pressure - b3) *
					(50000 >> data->oversampling_setting);
	p = ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2));

	x1 = p >> 8;
	x1 *= x1;
	x1 = (x1 * 3038) >> 16;
	x2 = (-7357 * p) >> 16;
	p += (x1 + x2 + 3791) >> 4;

	*pressure = p;

E
Eric Andersson 已提交
280
	return 0;
281 282 283 284 285 286
}

/*
 * This function sets the chip-internal oversampling. Valid values are 0..3.
 * The chip will use 2^oversampling samples for internal averaging.
 * This influences the measurement time and the accuracy; larger values
E
Eric Andersson 已提交
287
 * increase both. The datasheet gives an overview on how measurement time,
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
 * accuracy and noise correlate.
 */
static void bmp085_set_oversampling(struct bmp085_data *data,
						unsigned char oversampling)
{
	if (oversampling > 3)
		oversampling = 3;
	data->oversampling_setting = oversampling;
}

/*
 * Returns the currently selected oversampling. Range: 0..3
 */
static unsigned char bmp085_get_oversampling(struct bmp085_data *data)
{
	return data->oversampling_setting;
}

/* sysfs callbacks */
static ssize_t set_oversampling(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
311
	struct bmp085_data *data = dev_get_drvdata(dev);
312
	unsigned long oversampling;
E
Eric Andersson 已提交
313 314 315 316
	int err = kstrtoul(buf, 10, &oversampling);

	if (err == 0) {
		mutex_lock(&data->lock);
317
		bmp085_set_oversampling(data, oversampling);
E
Eric Andersson 已提交
318
		mutex_unlock(&data->lock);
319 320
		return count;
	}
E
Eric Andersson 已提交
321 322

	return err;
323 324 325 326 327
}

static ssize_t show_oversampling(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
328
	struct bmp085_data *data = dev_get_drvdata(dev);
E
Eric Andersson 已提交
329

330 331 332 333 334 335 336 337 338 339 340
	return sprintf(buf, "%u\n", bmp085_get_oversampling(data));
}
static DEVICE_ATTR(oversampling, S_IWUSR | S_IRUGO,
					show_oversampling, set_oversampling);


static ssize_t show_temperature(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	int temperature;
	int status;
341
	struct bmp085_data *data = dev_get_drvdata(dev);
342 343

	status = bmp085_get_temperature(data, &temperature);
E
Eric Andersson 已提交
344
	if (status < 0)
345 346 347 348 349 350 351 352 353 354 355 356
		return status;
	else
		return sprintf(buf, "%d\n", temperature);
}
static DEVICE_ATTR(temp0_input, S_IRUGO, show_temperature, NULL);


static ssize_t show_pressure(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	int pressure;
	int status;
357
	struct bmp085_data *data = dev_get_drvdata(dev);
358 359

	status = bmp085_get_pressure(data, &pressure);
E
Eric Andersson 已提交
360
	if (status < 0)
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
		return status;
	else
		return sprintf(buf, "%d\n", pressure);
}
static DEVICE_ATTR(pressure0_input, S_IRUGO, show_pressure, NULL);


static struct attribute *bmp085_attributes[] = {
	&dev_attr_temp0_input.attr,
	&dev_attr_pressure0_input.attr,
	&dev_attr_oversampling.attr,
	NULL
};

static const struct attribute_group bmp085_attr_group = {
	.attrs = bmp085_attributes,
};

379
int bmp085_detect(struct device *dev)
380
{
381 382 383 384 385 386 387
	struct bmp085_data *data = dev_get_drvdata(dev);
	unsigned int id;
	int ret;

	ret = regmap_read(data->regmap, BMP085_CHIP_ID_REG, &id);
	if (ret < 0)
		return ret;
388

389
	if (id != data->chip_id)
390 391 392 393
		return -ENODEV;

	return 0;
}
394
EXPORT_SYMBOL_GPL(bmp085_detect);
395

396
static void bmp085_get_of_properties(struct bmp085_data *data)
397 398
{
#ifdef CONFIG_OF
399
	struct device_node *np = data->dev->of_node;
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	u32 prop;

	if (!np)
		return;

	if (!of_property_read_u32(np, "chip-id", &prop))
		data->chip_id = prop & 0xff;

	if (!of_property_read_u32(np, "temp-measurement-period", &prop))
		data->temp_measurement_period = (prop/100)*HZ;

	if (!of_property_read_u32(np, "default-oversampling", &prop))
		data->oversampling_setting = prop & 0xff;
#endif
}

416
static int bmp085_init_client(struct bmp085_data *data)
417
{
418
	int status = bmp085_read_calibration_data(data);
E
Eric Andersson 已提交
419 420 421 422

	if (status < 0)
		return status;

423 424
	/* default settings */
	data->chip_id = BMP085_CHIP_ID;
425
	data->last_temp_measurement = 0;
E
Eric Andersson 已提交
426
	data->temp_measurement_period = 1*HZ;
427
	data->oversampling_setting = 3;
428

429
	bmp085_get_of_properties(data);
430

431
	mutex_init(&data->lock);
E
Eric Andersson 已提交
432 433

	return 0;
434 435
}

436 437 438 439 440 441
struct regmap_config bmp085_regmap_config = {
	.reg_bits = 8,
	.val_bits = 8
};
EXPORT_SYMBOL_GPL(bmp085_regmap_config);

442
int bmp085_probe(struct device *dev, struct regmap *regmap, int irq)
443 444 445 446 447 448 449 450 451 452
{
	struct bmp085_data *data;
	int err = 0;

	data = kzalloc(sizeof(struct bmp085_data), GFP_KERNEL);
	if (!data) {
		err = -ENOMEM;
		goto exit;
	}

453 454 455
	dev_set_drvdata(dev, data);
	data->dev = dev;
	data->regmap = regmap;
456 457 458 459 460 461 462 463 464
	data->irq = irq;

	if (data->irq > 0) {
		err = devm_request_irq(dev, data->irq, bmp085_eoc_isr,
					      IRQF_TRIGGER_RISING, "bmp085",
					      data);
		if (err < 0)
			goto exit_free;
	}
465 466

	/* Initialize the BMP085 chip */
467
	err = bmp085_init_client(data);
E
Eric Andersson 已提交
468
	if (err < 0)
469 470
		goto exit_free;

471 472 473 474 475 476
	err = bmp085_detect(dev);
	if (err < 0) {
		dev_err(dev, "%s: chip_id failed!\n", BMP085_NAME);
		goto exit_free;
	}

477
	/* Register sysfs hooks */
478
	err = sysfs_create_group(&dev->kobj, &bmp085_attr_group);
479 480 481
	if (err)
		goto exit_free;

482
	dev_info(dev, "Successfully initialized %s!\n", BMP085_NAME);
E
Eric Andersson 已提交
483 484

	return 0;
485 486 487 488 489 490

exit_free:
	kfree(data);
exit:
	return err;
}
491
EXPORT_SYMBOL_GPL(bmp085_probe);
492

493
int bmp085_remove(struct device *dev)
494
{
495
	struct bmp085_data *data = dev_get_drvdata(dev);
E
Eric Andersson 已提交
496

497
	sysfs_remove_group(&data->dev->kobj, &bmp085_attr_group);
E
Eric Andersson 已提交
498 499
	kfree(data);

500 501
	return 0;
}
502
EXPORT_SYMBOL_GPL(bmp085_remove);
503

E
Eric Andersson 已提交
504
MODULE_AUTHOR("Christoph Mair <christoph.mair@gmail.com>");
505 506
MODULE_DESCRIPTION("BMP085 driver");
MODULE_LICENSE("GPL");