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