bmp085.c 13 KB

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