X-Git-Url: http://git.cascardo.info/?a=blobdiff_plain;f=drivers%2Fmisc%2Fbmp085.c;h=62e418293b7e8a1ddccbf2c6330b63458608e76e;hb=07acfc2a9349a8ce45b236c2624dad452001966b;hp=76c3064629f1c802ef38c0f3ad4ae15f5a1142cb;hpb=5ff5c3a4ab0d638fa63e939e75727c233b681e8d;p=cascardo%2Flinux.git diff --git a/drivers/misc/bmp085.c b/drivers/misc/bmp085.c index 76c3064629f1..62e418293b7e 100644 --- a/drivers/misc/bmp085.c +++ b/drivers/misc/bmp085.c @@ -1,62 +1,62 @@ /* Copyright (c) 2010 Christoph Mair - - This driver supports the bmp085 digital barometric pressure - and temperature sensor from Bosch Sensortec. The datasheet - is available from their website: - http://www.bosch-sensortec.com/content/language1/downloads/BST-BMP085-DS000-05.pdf - - 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. -*/ - + * Copyright (c) 2012 Bosch Sensortec GmbH + * Copyright (c) 2012 Unixphere AB + * + * This driver supports the bmp085 and bmp18x digital barometric pressure + * and temperature sensors from Bosch Sensortec. The datasheets + * are available from their website: + * http://www.bosch-sensortec.com/content/language1/downloads/BST-BMP085-DS000-05.pdf + * http://www.bosch-sensortec.com/content/language1/downloads/BST-BMP180-DS000-07.pdf + * + * 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. + */ #include +#include #include -#include #include #include +#include +#include "bmp085.h" - -#define BMP085_I2C_ADDRESS 0x77 #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_VERSION_REG 0xD1 #define BMP085_CTRL_REG 0xF4 #define BMP085_TEMP_MEASUREMENT 0x2E #define BMP085_PRESSURE_MEASUREMENT 0x34 @@ -65,12 +65,6 @@ #define BMP085_CONVERSION_REGISTER_XLSB 0xF8 #define BMP085_TEMP_CONVERSION_TIME 5 -#define BMP085_CLIENT_NAME "bmp085" - - -static const unsigned short normal_i2c[] = { BMP085_I2C_ADDRESS, - I2C_CLIENT_END }; - struct bmp085_calibration_data { s16 AC1, AC2, AC3; u16 AC4, AC5, AC6; @@ -78,35 +72,30 @@ struct bmp085_calibration_data { s16 MB, MC, MD; }; - -/* Each client has this additional data */ struct bmp085_data { - struct i2c_client *client; - struct mutex lock; - struct bmp085_calibration_data calibration; - u32 raw_temperature; - u32 raw_pressure; - unsigned char oversampling_setting; + struct device *dev; + struct regmap *regmap; + struct mutex lock; + struct bmp085_calibration_data calibration; + u8 oversampling_setting; + u32 raw_temperature; + u32 raw_pressure; + u32 temp_measurement_period; unsigned long last_temp_measurement; - s32 b6; /* calculated temperature correction coefficient */ + u8 chip_id; + s32 b6; /* calculated temperature correction coefficient */ }; - -static s32 bmp085_read_calibration_data(struct i2c_client *client) +static s32 bmp085_read_calibration_data(struct bmp085_data *data) { u16 tmp[BMP085_CALIBRATION_DATA_LENGTH]; - struct bmp085_data *data = i2c_get_clientdata(client); struct bmp085_calibration_data *cali = &(data->calibration); - s32 status = i2c_smbus_read_i2c_block_data(client, - BMP085_CALIBRATION_DATA_START, - BMP085_CALIBRATION_DATA_LENGTH*sizeof(u16), - (u8 *)tmp); + s32 status = regmap_bulk_read(data->regmap, + BMP085_CALIBRATION_DATA_START, (u8 *)tmp, + (BMP085_CALIBRATION_DATA_LENGTH << 1)); if (status < 0) return status; - if (status != BMP085_CALIBRATION_DATA_LENGTH*sizeof(u16)) - return -EIO; - cali->AC1 = be16_to_cpu(tmp[0]); cali->AC2 = be16_to_cpu(tmp[1]); cali->AC3 = be16_to_cpu(tmp[2]); @@ -121,30 +110,26 @@ static s32 bmp085_read_calibration_data(struct i2c_client *client) return 0; } - static s32 bmp085_update_raw_temperature(struct bmp085_data *data) { u16 tmp; s32 status; mutex_lock(&data->lock); - status = i2c_smbus_write_byte_data(data->client, BMP085_CTRL_REG, - BMP085_TEMP_MEASUREMENT); - if (status != 0) { - dev_err(&data->client->dev, + status = regmap_write(data->regmap, BMP085_CTRL_REG, + BMP085_TEMP_MEASUREMENT); + if (status < 0) { + dev_err(data->dev, "Error while requesting temperature measurement.\n"); goto exit; } msleep(BMP085_TEMP_CONVERSION_TIME); - status = i2c_smbus_read_i2c_block_data(data->client, - BMP085_CONVERSION_REGISTER_MSB, sizeof(tmp), (u8 *)&tmp); - if (status < 0) - goto exit; - if (status != sizeof(tmp)) { - dev_err(&data->client->dev, + status = regmap_bulk_read(data->regmap, BMP085_CONVERSION_REGISTER_MSB, + &tmp, sizeof(tmp)); + if (status < 0) { + dev_err(data->dev, "Error while reading temperature measurement result\n"); - status = -EIO; goto exit; } data->raw_temperature = be16_to_cpu(tmp); @@ -162,10 +147,11 @@ static s32 bmp085_update_raw_pressure(struct bmp085_data *data) s32 status; mutex_lock(&data->lock); - status = i2c_smbus_write_byte_data(data->client, BMP085_CTRL_REG, - BMP085_PRESSURE_MEASUREMENT + (data->oversampling_setting<<6)); - if (status != 0) { - dev_err(&data->client->dev, + status = regmap_write(data->regmap, BMP085_CTRL_REG, + BMP085_PRESSURE_MEASUREMENT + + (data->oversampling_setting << 6)); + if (status < 0) { + dev_err(data->dev, "Error while requesting pressure measurement.\n"); goto exit; } @@ -174,14 +160,11 @@ static s32 bmp085_update_raw_pressure(struct bmp085_data *data) msleep(2+(3 << data->oversampling_setting)); /* copy data into a u32 (4 bytes), but skip the first byte. */ - status = i2c_smbus_read_i2c_block_data(data->client, - BMP085_CONVERSION_REGISTER_MSB, 3, ((u8 *)&tmp)+1); - if (status < 0) - goto exit; - if (status != 3) { - dev_err(&data->client->dev, + status = regmap_bulk_read(data->regmap, BMP085_CONVERSION_REGISTER_MSB, + ((u8 *)&tmp)+1, 3); + if (status < 0) { + dev_err(data->dev, "Error while reading pressure measurement results\n"); - status = -EIO; goto exit; } data->raw_pressure = be32_to_cpu((tmp)); @@ -193,7 +176,6 @@ exit: return status; } - /* * This function starts the temperature measurement and returns the value * in tenth of a degree celsius. @@ -205,7 +187,7 @@ static s32 bmp085_get_temperature(struct bmp085_data *data, int *temperature) int status; status = bmp085_update_raw_temperature(data); - if (status != 0) + if (status < 0) goto exit; x1 = ((data->raw_temperature - cali->AC6) * cali->AC5) >> 15; @@ -222,8 +204,10 @@ exit: /* * This function starts the pressure measurement and returns the value * in millibar. Since the pressure depends on the ambient temperature, - * a temperature measurement is executed if the last known value is older - * than one second. + * 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. */ static s32 bmp085_get_pressure(struct bmp085_data *data, int *pressure) { @@ -234,16 +218,16 @@ static s32 bmp085_get_pressure(struct bmp085_data *data, int *pressure) int status; /* alt least every second force an update of the ambient temperature */ - if (data->last_temp_measurement == 0 || - time_is_before_jiffies(data->last_temp_measurement + 1*HZ)) { + if ((data->last_temp_measurement == 0) || + time_is_before_jiffies(data->last_temp_measurement + 1*HZ)) { status = bmp085_get_temperature(data, NULL); - if (status != 0) - goto exit; + if (status < 0) + return status; } status = bmp085_update_raw_pressure(data); - if (status != 0) - goto exit; + if (status < 0) + return status; x1 = (data->b6 * data->b6) >> 12; x1 *= cali->B2; @@ -274,15 +258,14 @@ static s32 bmp085_get_pressure(struct bmp085_data *data, int *pressure) *pressure = p; -exit: - return status; + return 0; } /* * 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 - * increase both. The datasheet gives on overview on how measurement time, + * increase both. The datasheet gives an overview on how measurement time, * accuracy and noise correlate. */ static void bmp085_set_oversampling(struct bmp085_data *data, @@ -306,22 +289,25 @@ static ssize_t set_oversampling(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - struct i2c_client *client = to_i2c_client(dev); - struct bmp085_data *data = i2c_get_clientdata(client); + struct bmp085_data *data = dev_get_drvdata(dev); unsigned long oversampling; - int success = strict_strtoul(buf, 10, &oversampling); - if (success == 0) { + int err = kstrtoul(buf, 10, &oversampling); + + if (err == 0) { + mutex_lock(&data->lock); bmp085_set_oversampling(data, oversampling); + mutex_unlock(&data->lock); return count; } - return success; + + return err; } static ssize_t show_oversampling(struct device *dev, struct device_attribute *attr, char *buf) { - struct i2c_client *client = to_i2c_client(dev); - struct bmp085_data *data = i2c_get_clientdata(client); + struct bmp085_data *data = dev_get_drvdata(dev); + return sprintf(buf, "%u\n", bmp085_get_oversampling(data)); } static DEVICE_ATTR(oversampling, S_IWUSR | S_IRUGO, @@ -333,11 +319,10 @@ static ssize_t show_temperature(struct device *dev, { int temperature; int status; - struct i2c_client *client = to_i2c_client(dev); - struct bmp085_data *data = i2c_get_clientdata(client); + struct bmp085_data *data = dev_get_drvdata(dev); status = bmp085_get_temperature(data, &temperature); - if (status != 0) + if (status < 0) return status; else return sprintf(buf, "%d\n", temperature); @@ -350,11 +335,10 @@ static ssize_t show_pressure(struct device *dev, { int pressure; int status; - struct i2c_client *client = to_i2c_client(dev); - struct bmp085_data *data = i2c_get_clientdata(client); + struct bmp085_data *data = dev_get_drvdata(dev); status = bmp085_get_pressure(data, &pressure); - if (status != 0) + if (status < 0) return status; else return sprintf(buf, "%d\n", pressure); @@ -373,38 +357,70 @@ static const struct attribute_group bmp085_attr_group = { .attrs = bmp085_attributes, }; -static int bmp085_detect(struct i2c_client *client, struct i2c_board_info *info) +int bmp085_detect(struct device *dev) { - if (client->addr != BMP085_I2C_ADDRESS) - return -ENODEV; + struct bmp085_data *data = dev_get_drvdata(dev); + unsigned int id; + int ret; - if (i2c_smbus_read_byte_data(client, BMP085_CHIP_ID_REG) != BMP085_CHIP_ID) + ret = regmap_read(data->regmap, BMP085_CHIP_ID_REG, &id); + if (ret < 0) + return ret; + + if (id != data->chip_id) return -ENODEV; return 0; } +EXPORT_SYMBOL_GPL(bmp085_detect); -static int bmp085_init_client(struct i2c_client *client) +static void __init bmp085_get_of_properties(struct bmp085_data *data) { - unsigned char version; - int status; - struct bmp085_data *data = i2c_get_clientdata(client); - data->client = client; - status = bmp085_read_calibration_data(client); - if (status != 0) - goto exit; - version = i2c_smbus_read_byte_data(client, BMP085_VERSION_REG); +#ifdef CONFIG_OF + struct device_node *np = data->dev->of_node; + 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 +} + +static int bmp085_init_client(struct bmp085_data *data) +{ + int status = bmp085_read_calibration_data(data); + + if (status < 0) + return status; + + /* default settings */ + data->chip_id = BMP085_CHIP_ID; data->last_temp_measurement = 0; + data->temp_measurement_period = 1*HZ; data->oversampling_setting = 3; + + bmp085_get_of_properties(data); + mutex_init(&data->lock); - dev_info(&data->client->dev, "BMP085 ver. %d.%d found.\n", - (version & 0x0F), (version & 0xF0) >> 4); -exit: - return status; + + return 0; } -static int __devinit bmp085_probe(struct i2c_client *client, - const struct i2c_device_id *id) +struct regmap_config bmp085_regmap_config = { + .reg_bits = 8, + .val_bits = 8 +}; +EXPORT_SYMBOL_GPL(bmp085_regmap_config); + +__devinit int bmp085_probe(struct device *dev, struct regmap *regmap) { struct bmp085_data *data; int err = 0; @@ -415,58 +431,48 @@ static int __devinit bmp085_probe(struct i2c_client *client, goto exit; } - /* default settings after POR */ - data->oversampling_setting = 0x00; - - i2c_set_clientdata(client, data); + dev_set_drvdata(dev, data); + data->dev = dev; + data->regmap = regmap; /* Initialize the BMP085 chip */ - err = bmp085_init_client(client); - if (err != 0) + err = bmp085_init_client(data); + if (err < 0) goto exit_free; + err = bmp085_detect(dev); + if (err < 0) { + dev_err(dev, "%s: chip_id failed!\n", BMP085_NAME); + goto exit_free; + } + /* Register sysfs hooks */ - err = sysfs_create_group(&client->dev.kobj, &bmp085_attr_group); + err = sysfs_create_group(&dev->kobj, &bmp085_attr_group); if (err) goto exit_free; - dev_info(&data->client->dev, "Successfully initialized bmp085!\n"); - goto exit; + dev_info(dev, "Successfully initialized %s!\n", BMP085_NAME); + + return 0; exit_free: kfree(data); exit: return err; } +EXPORT_SYMBOL_GPL(bmp085_probe); -static int __devexit bmp085_remove(struct i2c_client *client) +int bmp085_remove(struct device *dev) { - sysfs_remove_group(&client->dev.kobj, &bmp085_attr_group); - kfree(i2c_get_clientdata(client)); - return 0; -} + struct bmp085_data *data = dev_get_drvdata(dev); -static const struct i2c_device_id bmp085_id[] = { - { "bmp085", 0 }, - { } -}; -MODULE_DEVICE_TABLE(i2c, bmp085_id); - -static struct i2c_driver bmp085_driver = { - .driver = { - .owner = THIS_MODULE, - .name = "bmp085" - }, - .id_table = bmp085_id, - .probe = bmp085_probe, - .remove = __devexit_p(bmp085_remove), - - .detect = bmp085_detect, - .address_list = normal_i2c -}; + sysfs_remove_group(&data->dev->kobj, &bmp085_attr_group); + kfree(data); -module_i2c_driver(bmp085_driver); + return 0; +} +EXPORT_SYMBOL_GPL(bmp085_remove); -MODULE_AUTHOR("Christoph Mair "); MODULE_DESCRIPTION("BMP085 driver"); MODULE_LICENSE("GPL");