Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com> |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 2 | * Copyright (c) 2012 Bosch Sensortec GmbH |
| 3 | * Copyright (c) 2012 Unixphere AB |
| 4 | * |
| 5 | * This driver supports the bmp085 digital barometric pressure |
| 6 | * and temperature sensor from Bosch Sensortec. The datasheet |
| 7 | * is available from their website: |
| 8 | * http://www.bosch-sensortec.com/content/language1/downloads/BST-BMP085-DS000-05.pdf |
| 9 | * |
| 10 | * A pressure measurement is issued by reading from pressure0_input. |
| 11 | * The return value ranges from 30000 to 110000 pascal with a resulution |
| 12 | * of 1 pascal (0.01 millibar) which enables measurements from 9000m above |
| 13 | * to 500m below sea level. |
| 14 | * |
| 15 | * The temperature can be read from temp0_input. Values range from |
| 16 | * -400 to 850 representing the ambient temperature in degree celsius |
| 17 | * multiplied by 10.The resolution is 0.1 celsius. |
| 18 | * |
| 19 | * Because ambient pressure is temperature dependent, a temperature |
| 20 | * measurement will be executed automatically even if the user is reading |
| 21 | * from pressure0_input. This happens if the last temperature measurement |
| 22 | * has been executed more then one second ago. |
| 23 | * |
| 24 | * To decrease RMS noise from pressure measurements, the bmp085 can |
| 25 | * autonomously calculate the average of up to eight samples. This is |
| 26 | * set up by writing to the oversampling sysfs file. Accepted values |
| 27 | * are 0, 1, 2 and 3. 2^x when x is the value written to this file |
| 28 | * specifies the number of samples used to calculate the ambient pressure. |
| 29 | * RMS noise is specified with six pascal (without averaging) and decreases |
| 30 | * down to 3 pascal when using an oversampling setting of 3. |
| 31 | * |
| 32 | * This program is free software; you can redistribute it and/or modify |
| 33 | * it under the terms of the GNU General Public License as published by |
| 34 | * the Free Software Foundation; either version 2 of the License, or |
| 35 | * (at your option) any later version. |
| 36 | * |
| 37 | * This program is distributed in the hope that it will be useful, |
| 38 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 39 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 40 | * GNU General Public License for more details. |
| 41 | * |
| 42 | * You should have received a copy of the GNU General Public License |
| 43 | * along with this program; if not, write to the Free Software |
| 44 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 45 | */ |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 46 | |
| 47 | #include <linux/module.h> |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 48 | #include <linux/device.h> |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 49 | #include <linux/init.h> |
| 50 | #include <linux/i2c.h> |
| 51 | #include <linux/slab.h> |
| 52 | #include <linux/delay.h> |
| 53 | |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 54 | #define BMP085_NAME "bmp085" |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 55 | #define BMP085_I2C_ADDRESS 0x77 |
| 56 | #define BMP085_CHIP_ID 0x55 |
| 57 | |
| 58 | #define BMP085_CALIBRATION_DATA_START 0xAA |
| 59 | #define BMP085_CALIBRATION_DATA_LENGTH 11 /* 16 bit values */ |
| 60 | #define BMP085_CHIP_ID_REG 0xD0 |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 61 | #define BMP085_CTRL_REG 0xF4 |
| 62 | #define BMP085_TEMP_MEASUREMENT 0x2E |
| 63 | #define BMP085_PRESSURE_MEASUREMENT 0x34 |
| 64 | #define BMP085_CONVERSION_REGISTER_MSB 0xF6 |
| 65 | #define BMP085_CONVERSION_REGISTER_LSB 0xF7 |
| 66 | #define BMP085_CONVERSION_REGISTER_XLSB 0xF8 |
| 67 | #define BMP085_TEMP_CONVERSION_TIME 5 |
| 68 | |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 69 | static const unsigned short normal_i2c[] = { BMP085_I2C_ADDRESS, |
| 70 | I2C_CLIENT_END }; |
| 71 | |
| 72 | struct bmp085_calibration_data { |
| 73 | s16 AC1, AC2, AC3; |
| 74 | u16 AC4, AC5, AC6; |
| 75 | s16 B1, B2; |
| 76 | s16 MB, MC, MD; |
| 77 | }; |
| 78 | |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 79 | struct bmp085_data { |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 80 | struct i2c_client *client; |
| 81 | struct mutex lock; |
| 82 | struct bmp085_calibration_data calibration; |
| 83 | u8 oversampling_setting; |
| 84 | u32 raw_temperature; |
| 85 | u32 raw_pressure; |
| 86 | u32 temp_measurement_period; |
Bernhard Walle | b222258 | 2012-02-25 10:28:12 +0100 | [diff] [blame] | 87 | unsigned long last_temp_measurement; |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 88 | s32 b6; /* calculated temperature correction coefficient */ |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 91 | static s32 bmp085_read_calibration_data(struct i2c_client *client) |
| 92 | { |
| 93 | u16 tmp[BMP085_CALIBRATION_DATA_LENGTH]; |
| 94 | struct bmp085_data *data = i2c_get_clientdata(client); |
| 95 | struct bmp085_calibration_data *cali = &(data->calibration); |
| 96 | s32 status = i2c_smbus_read_i2c_block_data(client, |
| 97 | BMP085_CALIBRATION_DATA_START, |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 98 | (BMP085_CALIBRATION_DATA_LENGTH << 1), |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 99 | (u8 *)tmp); |
| 100 | if (status < 0) |
| 101 | return status; |
| 102 | |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 103 | if (status != (BMP085_CALIBRATION_DATA_LENGTH << 1)) |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 104 | return -EIO; |
| 105 | |
| 106 | cali->AC1 = be16_to_cpu(tmp[0]); |
| 107 | cali->AC2 = be16_to_cpu(tmp[1]); |
| 108 | cali->AC3 = be16_to_cpu(tmp[2]); |
| 109 | cali->AC4 = be16_to_cpu(tmp[3]); |
| 110 | cali->AC5 = be16_to_cpu(tmp[4]); |
| 111 | cali->AC6 = be16_to_cpu(tmp[5]); |
| 112 | cali->B1 = be16_to_cpu(tmp[6]); |
| 113 | cali->B2 = be16_to_cpu(tmp[7]); |
| 114 | cali->MB = be16_to_cpu(tmp[8]); |
| 115 | cali->MC = be16_to_cpu(tmp[9]); |
| 116 | cali->MD = be16_to_cpu(tmp[10]); |
| 117 | return 0; |
| 118 | } |
| 119 | |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 120 | static s32 bmp085_update_raw_temperature(struct bmp085_data *data) |
| 121 | { |
| 122 | u16 tmp; |
| 123 | s32 status; |
| 124 | |
| 125 | mutex_lock(&data->lock); |
| 126 | status = i2c_smbus_write_byte_data(data->client, BMP085_CTRL_REG, |
| 127 | BMP085_TEMP_MEASUREMENT); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 128 | if (status < 0) { |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 129 | dev_err(&data->client->dev, |
| 130 | "Error while requesting temperature measurement.\n"); |
| 131 | goto exit; |
| 132 | } |
| 133 | msleep(BMP085_TEMP_CONVERSION_TIME); |
| 134 | |
| 135 | status = i2c_smbus_read_i2c_block_data(data->client, |
| 136 | BMP085_CONVERSION_REGISTER_MSB, sizeof(tmp), (u8 *)&tmp); |
| 137 | if (status < 0) |
| 138 | goto exit; |
| 139 | if (status != sizeof(tmp)) { |
| 140 | dev_err(&data->client->dev, |
| 141 | "Error while reading temperature measurement result\n"); |
| 142 | status = -EIO; |
| 143 | goto exit; |
| 144 | } |
| 145 | data->raw_temperature = be16_to_cpu(tmp); |
| 146 | data->last_temp_measurement = jiffies; |
| 147 | status = 0; /* everything ok, return 0 */ |
| 148 | |
| 149 | exit: |
| 150 | mutex_unlock(&data->lock); |
| 151 | return status; |
| 152 | } |
| 153 | |
| 154 | static s32 bmp085_update_raw_pressure(struct bmp085_data *data) |
| 155 | { |
| 156 | u32 tmp = 0; |
| 157 | s32 status; |
| 158 | |
| 159 | mutex_lock(&data->lock); |
| 160 | status = i2c_smbus_write_byte_data(data->client, BMP085_CTRL_REG, |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 161 | BMP085_PRESSURE_MEASUREMENT + |
| 162 | (data->oversampling_setting << 6)); |
| 163 | if (status < 0) { |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 164 | dev_err(&data->client->dev, |
| 165 | "Error while requesting pressure measurement.\n"); |
| 166 | goto exit; |
| 167 | } |
| 168 | |
| 169 | /* wait for the end of conversion */ |
| 170 | msleep(2+(3 << data->oversampling_setting)); |
| 171 | |
| 172 | /* copy data into a u32 (4 bytes), but skip the first byte. */ |
| 173 | status = i2c_smbus_read_i2c_block_data(data->client, |
| 174 | BMP085_CONVERSION_REGISTER_MSB, 3, ((u8 *)&tmp)+1); |
| 175 | if (status < 0) |
| 176 | goto exit; |
| 177 | if (status != 3) { |
| 178 | dev_err(&data->client->dev, |
| 179 | "Error while reading pressure measurement results\n"); |
| 180 | status = -EIO; |
| 181 | goto exit; |
| 182 | } |
| 183 | data->raw_pressure = be32_to_cpu((tmp)); |
| 184 | data->raw_pressure >>= (8-data->oversampling_setting); |
| 185 | status = 0; /* everything ok, return 0 */ |
| 186 | |
| 187 | exit: |
| 188 | mutex_unlock(&data->lock); |
| 189 | return status; |
| 190 | } |
| 191 | |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 192 | /* |
| 193 | * This function starts the temperature measurement and returns the value |
| 194 | * in tenth of a degree celsius. |
| 195 | */ |
| 196 | static s32 bmp085_get_temperature(struct bmp085_data *data, int *temperature) |
| 197 | { |
| 198 | struct bmp085_calibration_data *cali = &data->calibration; |
| 199 | long x1, x2; |
| 200 | int status; |
| 201 | |
| 202 | status = bmp085_update_raw_temperature(data); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 203 | if (status < 0) |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 204 | goto exit; |
| 205 | |
| 206 | x1 = ((data->raw_temperature - cali->AC6) * cali->AC5) >> 15; |
| 207 | x2 = (cali->MC << 11) / (x1 + cali->MD); |
| 208 | data->b6 = x1 + x2 - 4000; |
| 209 | /* if NULL just update b6. Used for pressure only measurements */ |
| 210 | if (temperature != NULL) |
| 211 | *temperature = (x1+x2+8) >> 4; |
| 212 | |
| 213 | exit: |
Jesper Juhl | f80ea66 | 2011-12-17 23:52:27 +0100 | [diff] [blame] | 214 | return status; |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* |
| 218 | * This function starts the pressure measurement and returns the value |
| 219 | * in millibar. Since the pressure depends on the ambient temperature, |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 220 | * a temperature measurement is executed according to the given temperature |
| 221 | * measurement period (default is 1 sec boundary). This period could vary |
| 222 | * and needs to be adjusted according to the sensor environment, i.e. if big |
| 223 | * temperature variations then the temperature needs to be read out often. |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 224 | */ |
| 225 | static s32 bmp085_get_pressure(struct bmp085_data *data, int *pressure) |
| 226 | { |
| 227 | struct bmp085_calibration_data *cali = &data->calibration; |
| 228 | s32 x1, x2, x3, b3; |
| 229 | u32 b4, b7; |
| 230 | s32 p; |
| 231 | int status; |
| 232 | |
| 233 | /* alt least every second force an update of the ambient temperature */ |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 234 | if ((data->last_temp_measurement == 0) || |
| 235 | time_is_before_jiffies(data->last_temp_measurement + 1*HZ)) { |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 236 | status = bmp085_get_temperature(data, NULL); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 237 | if (status < 0) |
| 238 | return status; |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | status = bmp085_update_raw_pressure(data); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 242 | if (status < 0) |
| 243 | return status; |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 244 | |
| 245 | x1 = (data->b6 * data->b6) >> 12; |
| 246 | x1 *= cali->B2; |
| 247 | x1 >>= 11; |
| 248 | |
| 249 | x2 = cali->AC2 * data->b6; |
| 250 | x2 >>= 11; |
| 251 | |
| 252 | x3 = x1 + x2; |
| 253 | |
| 254 | b3 = (((((s32)cali->AC1) * 4 + x3) << data->oversampling_setting) + 2); |
| 255 | b3 >>= 2; |
| 256 | |
| 257 | x1 = (cali->AC3 * data->b6) >> 13; |
| 258 | x2 = (cali->B1 * ((data->b6 * data->b6) >> 12)) >> 16; |
| 259 | x3 = (x1 + x2 + 2) >> 2; |
| 260 | b4 = (cali->AC4 * (u32)(x3 + 32768)) >> 15; |
| 261 | |
| 262 | b7 = ((u32)data->raw_pressure - b3) * |
| 263 | (50000 >> data->oversampling_setting); |
| 264 | p = ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2)); |
| 265 | |
| 266 | x1 = p >> 8; |
| 267 | x1 *= x1; |
| 268 | x1 = (x1 * 3038) >> 16; |
| 269 | x2 = (-7357 * p) >> 16; |
| 270 | p += (x1 + x2 + 3791) >> 4; |
| 271 | |
| 272 | *pressure = p; |
| 273 | |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 274 | return 0; |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* |
| 278 | * This function sets the chip-internal oversampling. Valid values are 0..3. |
| 279 | * The chip will use 2^oversampling samples for internal averaging. |
| 280 | * This influences the measurement time and the accuracy; larger values |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 281 | * increase both. The datasheet gives an overview on how measurement time, |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 282 | * accuracy and noise correlate. |
| 283 | */ |
| 284 | static void bmp085_set_oversampling(struct bmp085_data *data, |
| 285 | unsigned char oversampling) |
| 286 | { |
| 287 | if (oversampling > 3) |
| 288 | oversampling = 3; |
| 289 | data->oversampling_setting = oversampling; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Returns the currently selected oversampling. Range: 0..3 |
| 294 | */ |
| 295 | static unsigned char bmp085_get_oversampling(struct bmp085_data *data) |
| 296 | { |
| 297 | return data->oversampling_setting; |
| 298 | } |
| 299 | |
| 300 | /* sysfs callbacks */ |
| 301 | static ssize_t set_oversampling(struct device *dev, |
| 302 | struct device_attribute *attr, |
| 303 | const char *buf, size_t count) |
| 304 | { |
| 305 | struct i2c_client *client = to_i2c_client(dev); |
| 306 | struct bmp085_data *data = i2c_get_clientdata(client); |
| 307 | unsigned long oversampling; |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 308 | int err = kstrtoul(buf, 10, &oversampling); |
| 309 | |
| 310 | if (err == 0) { |
| 311 | mutex_lock(&data->lock); |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 312 | bmp085_set_oversampling(data, oversampling); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 313 | mutex_unlock(&data->lock); |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 314 | return count; |
| 315 | } |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 316 | |
| 317 | return err; |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static ssize_t show_oversampling(struct device *dev, |
| 321 | struct device_attribute *attr, char *buf) |
| 322 | { |
| 323 | struct i2c_client *client = to_i2c_client(dev); |
| 324 | struct bmp085_data *data = i2c_get_clientdata(client); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 325 | |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 326 | return sprintf(buf, "%u\n", bmp085_get_oversampling(data)); |
| 327 | } |
| 328 | static DEVICE_ATTR(oversampling, S_IWUSR | S_IRUGO, |
| 329 | show_oversampling, set_oversampling); |
| 330 | |
| 331 | |
| 332 | static ssize_t show_temperature(struct device *dev, |
| 333 | struct device_attribute *attr, char *buf) |
| 334 | { |
| 335 | int temperature; |
| 336 | int status; |
| 337 | struct i2c_client *client = to_i2c_client(dev); |
| 338 | struct bmp085_data *data = i2c_get_clientdata(client); |
| 339 | |
| 340 | status = bmp085_get_temperature(data, &temperature); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 341 | if (status < 0) |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 342 | return status; |
| 343 | else |
| 344 | return sprintf(buf, "%d\n", temperature); |
| 345 | } |
| 346 | static DEVICE_ATTR(temp0_input, S_IRUGO, show_temperature, NULL); |
| 347 | |
| 348 | |
| 349 | static ssize_t show_pressure(struct device *dev, |
| 350 | struct device_attribute *attr, char *buf) |
| 351 | { |
| 352 | int pressure; |
| 353 | int status; |
| 354 | struct i2c_client *client = to_i2c_client(dev); |
| 355 | struct bmp085_data *data = i2c_get_clientdata(client); |
| 356 | |
| 357 | status = bmp085_get_pressure(data, &pressure); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 358 | if (status < 0) |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 359 | return status; |
| 360 | else |
| 361 | return sprintf(buf, "%d\n", pressure); |
| 362 | } |
| 363 | static DEVICE_ATTR(pressure0_input, S_IRUGO, show_pressure, NULL); |
| 364 | |
| 365 | |
| 366 | static struct attribute *bmp085_attributes[] = { |
| 367 | &dev_attr_temp0_input.attr, |
| 368 | &dev_attr_pressure0_input.attr, |
| 369 | &dev_attr_oversampling.attr, |
| 370 | NULL |
| 371 | }; |
| 372 | |
| 373 | static const struct attribute_group bmp085_attr_group = { |
| 374 | .attrs = bmp085_attributes, |
| 375 | }; |
| 376 | |
| 377 | static int bmp085_detect(struct i2c_client *client, struct i2c_board_info *info) |
| 378 | { |
| 379 | if (client->addr != BMP085_I2C_ADDRESS) |
| 380 | return -ENODEV; |
| 381 | |
| 382 | if (i2c_smbus_read_byte_data(client, BMP085_CHIP_ID_REG) != BMP085_CHIP_ID) |
| 383 | return -ENODEV; |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int bmp085_init_client(struct i2c_client *client) |
| 389 | { |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 390 | struct bmp085_data *data = i2c_get_clientdata(client); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 391 | int status = bmp085_read_calibration_data(client); |
| 392 | |
| 393 | if (status < 0) |
| 394 | return status; |
| 395 | |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 396 | data->client = client; |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 397 | data->last_temp_measurement = 0; |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 398 | data->temp_measurement_period = 1*HZ; |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 399 | data->oversampling_setting = 3; |
| 400 | mutex_init(&data->lock); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 401 | |
| 402 | return 0; |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Shubhrajyoti Datta | 45bff2e | 2011-03-22 16:33:57 -0700 | [diff] [blame] | 405 | static int __devinit bmp085_probe(struct i2c_client *client, |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 406 | const struct i2c_device_id *id) |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 407 | { |
| 408 | struct bmp085_data *data; |
| 409 | int err = 0; |
| 410 | |
| 411 | data = kzalloc(sizeof(struct bmp085_data), GFP_KERNEL); |
| 412 | if (!data) { |
| 413 | err = -ENOMEM; |
| 414 | goto exit; |
| 415 | } |
| 416 | |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 417 | i2c_set_clientdata(client, data); |
| 418 | |
| 419 | /* Initialize the BMP085 chip */ |
| 420 | err = bmp085_init_client(client); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 421 | if (err < 0) |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 422 | goto exit_free; |
| 423 | |
| 424 | /* Register sysfs hooks */ |
| 425 | err = sysfs_create_group(&client->dev.kobj, &bmp085_attr_group); |
| 426 | if (err) |
| 427 | goto exit_free; |
| 428 | |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 429 | dev_info(&client->dev, "Successfully initialized %s!\n", BMP085_NAME); |
| 430 | |
| 431 | return 0; |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 432 | |
| 433 | exit_free: |
| 434 | kfree(data); |
| 435 | exit: |
| 436 | return err; |
| 437 | } |
| 438 | |
Shubhrajyoti Datta | 45bff2e | 2011-03-22 16:33:57 -0700 | [diff] [blame] | 439 | static int __devexit bmp085_remove(struct i2c_client *client) |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 440 | { |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 441 | struct bmp085_data *data = i2c_get_clientdata(client); |
| 442 | |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 443 | sysfs_remove_group(&client->dev.kobj, &bmp085_attr_group); |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 444 | kfree(data); |
| 445 | |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static const struct i2c_device_id bmp085_id[] = { |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 450 | { BMP085_NAME, 0 }, |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 451 | { } |
| 452 | }; |
Axel Lin | 97e419a | 2011-03-04 17:36:22 -0800 | [diff] [blame] | 453 | MODULE_DEVICE_TABLE(i2c, bmp085_id); |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 454 | |
| 455 | static struct i2c_driver bmp085_driver = { |
| 456 | .driver = { |
| 457 | .owner = THIS_MODULE, |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 458 | .name = BMP085_NAME |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 459 | }, |
| 460 | .id_table = bmp085_id, |
| 461 | .probe = bmp085_probe, |
Shubhrajyoti Datta | 45bff2e | 2011-03-22 16:33:57 -0700 | [diff] [blame] | 462 | .remove = __devexit_p(bmp085_remove), |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 463 | |
| 464 | .detect = bmp085_detect, |
| 465 | .address_list = normal_i2c |
| 466 | }; |
| 467 | |
Axel Lin | a64fe2e | 2012-01-22 15:36:45 +0800 | [diff] [blame] | 468 | module_i2c_driver(bmp085_driver); |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 469 | |
Eric Andersson | e939ca0 | 2012-04-09 22:16:15 +0200 | [diff] [blame^] | 470 | MODULE_AUTHOR("Christoph Mair <christoph.mair@gmail.com>"); |
Christoph Mair | 5bf1d29 | 2010-08-09 17:20:28 -0700 | [diff] [blame] | 471 | MODULE_DESCRIPTION("BMP085 driver"); |
| 472 | MODULE_LICENSE("GPL"); |