bmp085.c 13 KB

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