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