bmp085.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /* Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
  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. */
  46. #include <linux/module.h>
  47. #include <linux/device.h>
  48. #include <linux/init.h>
  49. #include <linux/i2c.h>
  50. #include <linux/slab.h>
  51. #include <linux/delay.h>
  52. #include <linux/of.h>
  53. #define BMP085_NAME "bmp085"
  54. #define BMP085_I2C_ADDRESS 0x77
  55. #define BMP085_CHIP_ID 0x55
  56. #define BMP085_CALIBRATION_DATA_START 0xAA
  57. #define BMP085_CALIBRATION_DATA_LENGTH 11 /* 16 bit values */
  58. #define BMP085_CHIP_ID_REG 0xD0
  59. #define BMP085_CTRL_REG 0xF4
  60. #define BMP085_TEMP_MEASUREMENT 0x2E
  61. #define BMP085_PRESSURE_MEASUREMENT 0x34
  62. #define BMP085_CONVERSION_REGISTER_MSB 0xF6
  63. #define BMP085_CONVERSION_REGISTER_LSB 0xF7
  64. #define BMP085_CONVERSION_REGISTER_XLSB 0xF8
  65. #define BMP085_TEMP_CONVERSION_TIME 5
  66. static const unsigned short normal_i2c[] = { BMP085_I2C_ADDRESS,
  67. I2C_CLIENT_END };
  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. struct bmp085_data {
  75. struct i2c_client *client;
  76. struct mutex lock;
  77. struct bmp085_calibration_data calibration;
  78. u8 oversampling_setting;
  79. u32 raw_temperature;
  80. u32 raw_pressure;
  81. u32 temp_measurement_period;
  82. unsigned long last_temp_measurement;
  83. u8 chip_id;
  84. s32 b6; /* calculated temperature correction coefficient */
  85. };
  86. static s32 bmp085_read_calibration_data(struct i2c_client *client)
  87. {
  88. u16 tmp[BMP085_CALIBRATION_DATA_LENGTH];
  89. struct bmp085_data *data = i2c_get_clientdata(client);
  90. struct bmp085_calibration_data *cali = &(data->calibration);
  91. s32 status = i2c_smbus_read_i2c_block_data(client,
  92. BMP085_CALIBRATION_DATA_START,
  93. (BMP085_CALIBRATION_DATA_LENGTH << 1),
  94. (u8 *)tmp);
  95. if (status < 0)
  96. return status;
  97. if (status != (BMP085_CALIBRATION_DATA_LENGTH << 1))
  98. return -EIO;
  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. static s32 bmp085_update_raw_temperature(struct bmp085_data *data)
  113. {
  114. u16 tmp;
  115. s32 status;
  116. mutex_lock(&data->lock);
  117. status = i2c_smbus_write_byte_data(data->client, BMP085_CTRL_REG,
  118. BMP085_TEMP_MEASUREMENT);
  119. if (status < 0) {
  120. dev_err(&data->client->dev,
  121. "Error while requesting temperature measurement.\n");
  122. goto exit;
  123. }
  124. msleep(BMP085_TEMP_CONVERSION_TIME);
  125. status = i2c_smbus_read_i2c_block_data(data->client,
  126. BMP085_CONVERSION_REGISTER_MSB, sizeof(tmp), (u8 *)&tmp);
  127. if (status < 0)
  128. goto exit;
  129. if (status != sizeof(tmp)) {
  130. dev_err(&data->client->dev,
  131. "Error while reading temperature measurement result\n");
  132. status = -EIO;
  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. exit:
  139. mutex_unlock(&data->lock);
  140. return status;
  141. }
  142. static s32 bmp085_update_raw_pressure(struct bmp085_data *data)
  143. {
  144. u32 tmp = 0;
  145. s32 status;
  146. mutex_lock(&data->lock);
  147. status = i2c_smbus_write_byte_data(data->client, BMP085_CTRL_REG,
  148. BMP085_PRESSURE_MEASUREMENT +
  149. (data->oversampling_setting << 6));
  150. if (status < 0) {
  151. dev_err(&data->client->dev,
  152. "Error while requesting pressure measurement.\n");
  153. goto exit;
  154. }
  155. /* wait for the end of conversion */
  156. msleep(2+(3 << data->oversampling_setting));
  157. /* copy data into a u32 (4 bytes), but skip the first byte. */
  158. status = i2c_smbus_read_i2c_block_data(data->client,
  159. BMP085_CONVERSION_REGISTER_MSB, 3, ((u8 *)&tmp)+1);
  160. if (status < 0)
  161. goto exit;
  162. if (status != 3) {
  163. dev_err(&data->client->dev,
  164. "Error while reading pressure measurement results\n");
  165. status = -EIO;
  166. goto exit;
  167. }
  168. data->raw_pressure = be32_to_cpu((tmp));
  169. data->raw_pressure >>= (8-data->oversampling_setting);
  170. status = 0; /* everything ok, return 0 */
  171. exit:
  172. mutex_unlock(&data->lock);
  173. return status;
  174. }
  175. /*
  176. * This function starts the temperature measurement and returns the value
  177. * in tenth of a degree celsius.
  178. */
  179. static s32 bmp085_get_temperature(struct bmp085_data *data, int *temperature)
  180. {
  181. struct bmp085_calibration_data *cali = &data->calibration;
  182. long x1, x2;
  183. int status;
  184. status = bmp085_update_raw_temperature(data);
  185. if (status < 0)
  186. goto exit;
  187. x1 = ((data->raw_temperature - cali->AC6) * cali->AC5) >> 15;
  188. x2 = (cali->MC << 11) / (x1 + cali->MD);
  189. data->b6 = x1 + x2 - 4000;
  190. /* if NULL just update b6. Used for pressure only measurements */
  191. if (temperature != NULL)
  192. *temperature = (x1+x2+8) >> 4;
  193. exit:
  194. return status;
  195. }
  196. /*
  197. * This function starts the pressure measurement and returns the value
  198. * in millibar. Since the pressure depends on the ambient temperature,
  199. * a temperature measurement is executed according to the given temperature
  200. * measurement period (default is 1 sec boundary). This period could vary
  201. * and needs to be adjusted according to the sensor environment, i.e. if big
  202. * temperature variations then the temperature needs to be read out often.
  203. */
  204. static s32 bmp085_get_pressure(struct bmp085_data *data, int *pressure)
  205. {
  206. struct bmp085_calibration_data *cali = &data->calibration;
  207. s32 x1, x2, x3, b3;
  208. u32 b4, b7;
  209. s32 p;
  210. int status;
  211. /* alt least every second force an update of the ambient temperature */
  212. if ((data->last_temp_measurement == 0) ||
  213. time_is_before_jiffies(data->last_temp_measurement + 1*HZ)) {
  214. status = bmp085_get_temperature(data, NULL);
  215. if (status < 0)
  216. return status;
  217. }
  218. status = bmp085_update_raw_pressure(data);
  219. if (status < 0)
  220. return status;
  221. x1 = (data->b6 * data->b6) >> 12;
  222. x1 *= cali->B2;
  223. x1 >>= 11;
  224. x2 = cali->AC2 * data->b6;
  225. x2 >>= 11;
  226. x3 = x1 + x2;
  227. b3 = (((((s32)cali->AC1) * 4 + x3) << data->oversampling_setting) + 2);
  228. b3 >>= 2;
  229. x1 = (cali->AC3 * data->b6) >> 13;
  230. x2 = (cali->B1 * ((data->b6 * data->b6) >> 12)) >> 16;
  231. x3 = (x1 + x2 + 2) >> 2;
  232. b4 = (cali->AC4 * (u32)(x3 + 32768)) >> 15;
  233. b7 = ((u32)data->raw_pressure - b3) *
  234. (50000 >> data->oversampling_setting);
  235. p = ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2));
  236. x1 = p >> 8;
  237. x1 *= x1;
  238. x1 = (x1 * 3038) >> 16;
  239. x2 = (-7357 * p) >> 16;
  240. p += (x1 + x2 + 3791) >> 4;
  241. *pressure = p;
  242. return 0;
  243. }
  244. /*
  245. * This function sets the chip-internal oversampling. Valid values are 0..3.
  246. * The chip will use 2^oversampling samples for internal averaging.
  247. * This influences the measurement time and the accuracy; larger values
  248. * increase both. The datasheet gives an overview on how measurement time,
  249. * accuracy and noise correlate.
  250. */
  251. static void bmp085_set_oversampling(struct bmp085_data *data,
  252. unsigned char oversampling)
  253. {
  254. if (oversampling > 3)
  255. oversampling = 3;
  256. data->oversampling_setting = oversampling;
  257. }
  258. /*
  259. * Returns the currently selected oversampling. Range: 0..3
  260. */
  261. static unsigned char bmp085_get_oversampling(struct bmp085_data *data)
  262. {
  263. return data->oversampling_setting;
  264. }
  265. /* sysfs callbacks */
  266. static ssize_t set_oversampling(struct device *dev,
  267. struct device_attribute *attr,
  268. const char *buf, size_t count)
  269. {
  270. struct i2c_client *client = to_i2c_client(dev);
  271. struct bmp085_data *data = i2c_get_clientdata(client);
  272. unsigned long oversampling;
  273. int err = kstrtoul(buf, 10, &oversampling);
  274. if (err == 0) {
  275. mutex_lock(&data->lock);
  276. bmp085_set_oversampling(data, oversampling);
  277. mutex_unlock(&data->lock);
  278. return count;
  279. }
  280. return err;
  281. }
  282. static ssize_t show_oversampling(struct device *dev,
  283. struct device_attribute *attr, char *buf)
  284. {
  285. struct i2c_client *client = to_i2c_client(dev);
  286. struct bmp085_data *data = i2c_get_clientdata(client);
  287. return sprintf(buf, "%u\n", bmp085_get_oversampling(data));
  288. }
  289. static DEVICE_ATTR(oversampling, S_IWUSR | S_IRUGO,
  290. show_oversampling, set_oversampling);
  291. static ssize_t show_temperature(struct device *dev,
  292. struct device_attribute *attr, char *buf)
  293. {
  294. int temperature;
  295. int status;
  296. struct i2c_client *client = to_i2c_client(dev);
  297. struct bmp085_data *data = i2c_get_clientdata(client);
  298. status = bmp085_get_temperature(data, &temperature);
  299. if (status < 0)
  300. return status;
  301. else
  302. return sprintf(buf, "%d\n", temperature);
  303. }
  304. static DEVICE_ATTR(temp0_input, S_IRUGO, show_temperature, NULL);
  305. static ssize_t show_pressure(struct device *dev,
  306. struct device_attribute *attr, char *buf)
  307. {
  308. int pressure;
  309. int status;
  310. struct i2c_client *client = to_i2c_client(dev);
  311. struct bmp085_data *data = i2c_get_clientdata(client);
  312. status = bmp085_get_pressure(data, &pressure);
  313. if (status < 0)
  314. return status;
  315. else
  316. return sprintf(buf, "%d\n", pressure);
  317. }
  318. static DEVICE_ATTR(pressure0_input, S_IRUGO, show_pressure, NULL);
  319. static struct attribute *bmp085_attributes[] = {
  320. &dev_attr_temp0_input.attr,
  321. &dev_attr_pressure0_input.attr,
  322. &dev_attr_oversampling.attr,
  323. NULL
  324. };
  325. static const struct attribute_group bmp085_attr_group = {
  326. .attrs = bmp085_attributes,
  327. };
  328. static int bmp085_detect(struct i2c_client *client, struct i2c_board_info *info)
  329. {
  330. if (client->addr != BMP085_I2C_ADDRESS)
  331. return -ENODEV;
  332. if (i2c_smbus_read_byte_data(client, BMP085_CHIP_ID_REG) != BMP085_CHIP_ID)
  333. return -ENODEV;
  334. return 0;
  335. }
  336. static void __init bmp085_get_of_properties(struct i2c_client *client,
  337. struct bmp085_data *data)
  338. {
  339. #ifdef CONFIG_OF
  340. struct device_node *np = client->dev.of_node;
  341. u32 prop;
  342. if (!np)
  343. return;
  344. if (!of_property_read_u32(np, "chip-id", &prop))
  345. data->chip_id = prop & 0xff;
  346. if (!of_property_read_u32(np, "temp-measurement-period", &prop))
  347. data->temp_measurement_period = (prop/100)*HZ;
  348. if (!of_property_read_u32(np, "default-oversampling", &prop))
  349. data->oversampling_setting = prop & 0xff;
  350. #endif
  351. }
  352. static int bmp085_init_client(struct i2c_client *client)
  353. {
  354. struct bmp085_data *data = i2c_get_clientdata(client);
  355. int status = bmp085_read_calibration_data(client);
  356. if (status < 0)
  357. return status;
  358. /* default settings */
  359. data->client = client;
  360. data->chip_id = BMP085_CHIP_ID;
  361. data->last_temp_measurement = 0;
  362. data->temp_measurement_period = 1*HZ;
  363. data->oversampling_setting = 3;
  364. bmp085_get_of_properties(client, data);
  365. mutex_init(&data->lock);
  366. return 0;
  367. }
  368. static int __devinit bmp085_probe(struct i2c_client *client,
  369. const struct i2c_device_id *id)
  370. {
  371. struct bmp085_data *data;
  372. int err = 0;
  373. data = kzalloc(sizeof(struct bmp085_data), GFP_KERNEL);
  374. if (!data) {
  375. err = -ENOMEM;
  376. goto exit;
  377. }
  378. i2c_set_clientdata(client, data);
  379. /* Initialize the BMP085 chip */
  380. err = bmp085_init_client(client);
  381. if (err < 0)
  382. goto exit_free;
  383. /* Register sysfs hooks */
  384. err = sysfs_create_group(&client->dev.kobj, &bmp085_attr_group);
  385. if (err)
  386. goto exit_free;
  387. dev_info(&client->dev, "Successfully initialized %s!\n", BMP085_NAME);
  388. return 0;
  389. exit_free:
  390. kfree(data);
  391. exit:
  392. return err;
  393. }
  394. static int __devexit bmp085_remove(struct i2c_client *client)
  395. {
  396. struct bmp085_data *data = i2c_get_clientdata(client);
  397. sysfs_remove_group(&client->dev.kobj, &bmp085_attr_group);
  398. kfree(data);
  399. return 0;
  400. }
  401. static const struct of_device_id bmp085_of_match[] = {
  402. { .compatible = "bosch,bmp085", },
  403. { },
  404. };
  405. MODULE_DEVICE_TABLE(of, bmp085_of_match);
  406. static const struct i2c_device_id bmp085_id[] = {
  407. { BMP085_NAME, 0 },
  408. { }
  409. };
  410. MODULE_DEVICE_TABLE(i2c, bmp085_id);
  411. static struct i2c_driver bmp085_driver = {
  412. .driver = {
  413. .owner = THIS_MODULE,
  414. .name = BMP085_NAME,
  415. .of_match_table = bmp085_of_match
  416. },
  417. .id_table = bmp085_id,
  418. .probe = bmp085_probe,
  419. .remove = __devexit_p(bmp085_remove),
  420. .detect = bmp085_detect,
  421. .address_list = normal_i2c
  422. };
  423. module_i2c_driver(bmp085_driver);
  424. MODULE_AUTHOR("Christoph Mair <christoph.mair@gmail.com>");
  425. MODULE_DESCRIPTION("BMP085 driver");
  426. MODULE_LICENSE("GPL");