bmp085.c 13 KB

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