bmp280-core.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
  3. * Copyright (c) 2012 Bosch Sensortec GmbH
  4. * Copyright (c) 2012 Unixphere AB
  5. * Copyright (c) 2014 Intel Corporation
  6. * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org>
  7. *
  8. * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * Datasheet:
  15. * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf
  16. * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf
  17. * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf
  18. */
  19. #define pr_fmt(fmt) "bmp280: " fmt
  20. #include <linux/device.h>
  21. #include <linux/module.h>
  22. #include <linux/regmap.h>
  23. #include <linux/delay.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/sysfs.h>
  26. #include <linux/gpio/consumer.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/irq.h> /* For irq_get_irq_data() */
  30. #include <linux/completion.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/random.h>
  33. #include "bmp280.h"
  34. /*
  35. * These enums are used for indexing into the array of calibration
  36. * coefficients for BMP180.
  37. */
  38. enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
  39. struct bmp180_calib {
  40. s16 AC1;
  41. s16 AC2;
  42. s16 AC3;
  43. u16 AC4;
  44. u16 AC5;
  45. u16 AC6;
  46. s16 B1;
  47. s16 B2;
  48. s16 MB;
  49. s16 MC;
  50. s16 MD;
  51. };
  52. struct bmp280_data {
  53. struct device *dev;
  54. struct mutex lock;
  55. struct regmap *regmap;
  56. struct completion done;
  57. bool use_eoc;
  58. const struct bmp280_chip_info *chip_info;
  59. struct bmp180_calib calib;
  60. struct regulator *vddd;
  61. struct regulator *vdda;
  62. unsigned int start_up_time; /* in microseconds */
  63. /* log of base 2 of oversampling rate */
  64. u8 oversampling_press;
  65. u8 oversampling_temp;
  66. u8 oversampling_humid;
  67. /*
  68. * Carryover value from temperature conversion, used in pressure
  69. * calculation.
  70. */
  71. s32 t_fine;
  72. };
  73. struct bmp280_chip_info {
  74. const int *oversampling_temp_avail;
  75. int num_oversampling_temp_avail;
  76. const int *oversampling_press_avail;
  77. int num_oversampling_press_avail;
  78. const int *oversampling_humid_avail;
  79. int num_oversampling_humid_avail;
  80. int (*chip_config)(struct bmp280_data *);
  81. int (*read_temp)(struct bmp280_data *, int *);
  82. int (*read_press)(struct bmp280_data *, int *, int *);
  83. int (*read_humid)(struct bmp280_data *, int *, int *);
  84. };
  85. /*
  86. * These enums are used for indexing into the array of compensation
  87. * parameters for BMP280.
  88. */
  89. enum { T1, T2, T3 };
  90. enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
  91. static const struct iio_chan_spec bmp280_channels[] = {
  92. {
  93. .type = IIO_PRESSURE,
  94. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  95. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  96. },
  97. {
  98. .type = IIO_TEMP,
  99. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  100. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  101. },
  102. {
  103. .type = IIO_HUMIDITYRELATIVE,
  104. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
  105. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
  106. },
  107. };
  108. /*
  109. * Returns humidity in percent, resolution is 0.01 percent. Output value of
  110. * "47445" represents 47445/1024 = 46.333 %RH.
  111. *
  112. * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
  113. */
  114. static u32 bmp280_compensate_humidity(struct bmp280_data *data,
  115. s32 adc_humidity)
  116. {
  117. struct device *dev = data->dev;
  118. unsigned int H1, H3, tmp;
  119. int H2, H4, H5, H6, ret, var;
  120. ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &H1);
  121. if (ret < 0) {
  122. dev_err(dev, "failed to read H1 comp value\n");
  123. return ret;
  124. }
  125. ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
  126. if (ret < 0) {
  127. dev_err(dev, "failed to read H2 comp value\n");
  128. return ret;
  129. }
  130. H2 = sign_extend32(le16_to_cpu(tmp), 15);
  131. ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &H3);
  132. if (ret < 0) {
  133. dev_err(dev, "failed to read H3 comp value\n");
  134. return ret;
  135. }
  136. ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
  137. if (ret < 0) {
  138. dev_err(dev, "failed to read H4 comp value\n");
  139. return ret;
  140. }
  141. H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
  142. (be16_to_cpu(tmp) & 0xf), 11);
  143. ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
  144. if (ret < 0) {
  145. dev_err(dev, "failed to read H5 comp value\n");
  146. return ret;
  147. }
  148. H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
  149. ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
  150. if (ret < 0) {
  151. dev_err(dev, "failed to read H6 comp value\n");
  152. return ret;
  153. }
  154. H6 = sign_extend32(tmp, 7);
  155. var = ((s32)data->t_fine) - (s32)76800;
  156. var = ((((adc_humidity << 14) - (H4 << 20) - (H5 * var))
  157. + (s32)16384) >> 15) * (((((((var * H6) >> 10)
  158. * (((var * (s32)H3) >> 11) + (s32)32768)) >> 10)
  159. + (s32)2097152) * H2 + 8192) >> 14);
  160. var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)H1) >> 4;
  161. return var >> 12;
  162. };
  163. /*
  164. * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
  165. * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
  166. * value.
  167. *
  168. * Taken from datasheet, Section 3.11.3, "Compensation formula".
  169. */
  170. static s32 bmp280_compensate_temp(struct bmp280_data *data,
  171. s32 adc_temp)
  172. {
  173. int ret;
  174. s32 var1, var2;
  175. __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
  176. ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
  177. buf, BMP280_COMP_TEMP_REG_COUNT);
  178. if (ret < 0) {
  179. dev_err(data->dev,
  180. "failed to read temperature calibration parameters\n");
  181. return ret;
  182. }
  183. /*
  184. * The double casts are necessary because le16_to_cpu returns an
  185. * unsigned 16-bit value. Casting that value directly to a
  186. * signed 32-bit will not do proper sign extension.
  187. *
  188. * Conversely, T1 and P1 are unsigned values, so they can be
  189. * cast straight to the larger type.
  190. */
  191. var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
  192. ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
  193. var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
  194. ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
  195. ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
  196. data->t_fine = var1 + var2;
  197. return (data->t_fine * 5 + 128) >> 8;
  198. }
  199. /*
  200. * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
  201. * integer bits and 8 fractional bits). Output value of "24674867"
  202. * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
  203. *
  204. * Taken from datasheet, Section 3.11.3, "Compensation formula".
  205. */
  206. static u32 bmp280_compensate_press(struct bmp280_data *data,
  207. s32 adc_press)
  208. {
  209. int ret;
  210. s64 var1, var2, p;
  211. __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
  212. ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
  213. buf, BMP280_COMP_PRESS_REG_COUNT);
  214. if (ret < 0) {
  215. dev_err(data->dev,
  216. "failed to read pressure calibration parameters\n");
  217. return ret;
  218. }
  219. var1 = ((s64)data->t_fine) - 128000;
  220. var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
  221. var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
  222. var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
  223. var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
  224. ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
  225. var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
  226. if (var1 == 0)
  227. return 0;
  228. p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
  229. p = div64_s64(p, var1);
  230. var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
  231. var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
  232. p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
  233. return (u32)p;
  234. }
  235. static int bmp280_read_temp(struct bmp280_data *data,
  236. int *val)
  237. {
  238. int ret;
  239. __be32 tmp = 0;
  240. s32 adc_temp, comp_temp;
  241. ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
  242. (u8 *) &tmp, 3);
  243. if (ret < 0) {
  244. dev_err(data->dev, "failed to read temperature\n");
  245. return ret;
  246. }
  247. adc_temp = be32_to_cpu(tmp) >> 12;
  248. comp_temp = bmp280_compensate_temp(data, adc_temp);
  249. /*
  250. * val might be NULL if we're called by the read_press routine,
  251. * who only cares about the carry over t_fine value.
  252. */
  253. if (val) {
  254. *val = comp_temp * 10;
  255. return IIO_VAL_INT;
  256. }
  257. return 0;
  258. }
  259. static int bmp280_read_press(struct bmp280_data *data,
  260. int *val, int *val2)
  261. {
  262. int ret;
  263. __be32 tmp = 0;
  264. s32 adc_press;
  265. u32 comp_press;
  266. /* Read and compensate temperature so we get a reading of t_fine. */
  267. ret = bmp280_read_temp(data, NULL);
  268. if (ret < 0)
  269. return ret;
  270. ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
  271. (u8 *) &tmp, 3);
  272. if (ret < 0) {
  273. dev_err(data->dev, "failed to read pressure\n");
  274. return ret;
  275. }
  276. adc_press = be32_to_cpu(tmp) >> 12;
  277. comp_press = bmp280_compensate_press(data, adc_press);
  278. *val = comp_press;
  279. *val2 = 256000;
  280. return IIO_VAL_FRACTIONAL;
  281. }
  282. static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
  283. {
  284. int ret;
  285. __be16 tmp = 0;
  286. s32 adc_humidity;
  287. u32 comp_humidity;
  288. /* Read and compensate temperature so we get a reading of t_fine. */
  289. ret = bmp280_read_temp(data, NULL);
  290. if (ret < 0)
  291. return ret;
  292. ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
  293. (u8 *) &tmp, 2);
  294. if (ret < 0) {
  295. dev_err(data->dev, "failed to read humidity\n");
  296. return ret;
  297. }
  298. adc_humidity = be16_to_cpu(tmp);
  299. comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
  300. *val = comp_humidity;
  301. *val2 = 1024;
  302. return IIO_VAL_FRACTIONAL;
  303. }
  304. static int bmp280_read_raw(struct iio_dev *indio_dev,
  305. struct iio_chan_spec const *chan,
  306. int *val, int *val2, long mask)
  307. {
  308. int ret;
  309. struct bmp280_data *data = iio_priv(indio_dev);
  310. pm_runtime_get_sync(data->dev);
  311. mutex_lock(&data->lock);
  312. switch (mask) {
  313. case IIO_CHAN_INFO_PROCESSED:
  314. switch (chan->type) {
  315. case IIO_HUMIDITYRELATIVE:
  316. ret = data->chip_info->read_humid(data, val, val2);
  317. break;
  318. case IIO_PRESSURE:
  319. ret = data->chip_info->read_press(data, val, val2);
  320. break;
  321. case IIO_TEMP:
  322. ret = data->chip_info->read_temp(data, val);
  323. break;
  324. default:
  325. ret = -EINVAL;
  326. break;
  327. }
  328. break;
  329. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  330. switch (chan->type) {
  331. case IIO_HUMIDITYRELATIVE:
  332. *val = 1 << data->oversampling_humid;
  333. ret = IIO_VAL_INT;
  334. break;
  335. case IIO_PRESSURE:
  336. *val = 1 << data->oversampling_press;
  337. ret = IIO_VAL_INT;
  338. break;
  339. case IIO_TEMP:
  340. *val = 1 << data->oversampling_temp;
  341. ret = IIO_VAL_INT;
  342. break;
  343. default:
  344. ret = -EINVAL;
  345. break;
  346. }
  347. break;
  348. default:
  349. ret = -EINVAL;
  350. break;
  351. }
  352. mutex_unlock(&data->lock);
  353. pm_runtime_mark_last_busy(data->dev);
  354. pm_runtime_put_autosuspend(data->dev);
  355. return ret;
  356. }
  357. static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
  358. int val)
  359. {
  360. int i;
  361. const int *avail = data->chip_info->oversampling_humid_avail;
  362. const int n = data->chip_info->num_oversampling_humid_avail;
  363. for (i = 0; i < n; i++) {
  364. if (avail[i] == val) {
  365. data->oversampling_humid = ilog2(val);
  366. return data->chip_info->chip_config(data);
  367. }
  368. }
  369. return -EINVAL;
  370. }
  371. static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
  372. int val)
  373. {
  374. int i;
  375. const int *avail = data->chip_info->oversampling_temp_avail;
  376. const int n = data->chip_info->num_oversampling_temp_avail;
  377. for (i = 0; i < n; i++) {
  378. if (avail[i] == val) {
  379. data->oversampling_temp = ilog2(val);
  380. return data->chip_info->chip_config(data);
  381. }
  382. }
  383. return -EINVAL;
  384. }
  385. static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
  386. int val)
  387. {
  388. int i;
  389. const int *avail = data->chip_info->oversampling_press_avail;
  390. const int n = data->chip_info->num_oversampling_press_avail;
  391. for (i = 0; i < n; i++) {
  392. if (avail[i] == val) {
  393. data->oversampling_press = ilog2(val);
  394. return data->chip_info->chip_config(data);
  395. }
  396. }
  397. return -EINVAL;
  398. }
  399. static int bmp280_write_raw(struct iio_dev *indio_dev,
  400. struct iio_chan_spec const *chan,
  401. int val, int val2, long mask)
  402. {
  403. int ret = 0;
  404. struct bmp280_data *data = iio_priv(indio_dev);
  405. switch (mask) {
  406. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  407. pm_runtime_get_sync(data->dev);
  408. mutex_lock(&data->lock);
  409. switch (chan->type) {
  410. case IIO_HUMIDITYRELATIVE:
  411. ret = bmp280_write_oversampling_ratio_humid(data, val);
  412. break;
  413. case IIO_PRESSURE:
  414. ret = bmp280_write_oversampling_ratio_press(data, val);
  415. break;
  416. case IIO_TEMP:
  417. ret = bmp280_write_oversampling_ratio_temp(data, val);
  418. break;
  419. default:
  420. ret = -EINVAL;
  421. break;
  422. }
  423. mutex_unlock(&data->lock);
  424. pm_runtime_mark_last_busy(data->dev);
  425. pm_runtime_put_autosuspend(data->dev);
  426. break;
  427. default:
  428. return -EINVAL;
  429. }
  430. return ret;
  431. }
  432. static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
  433. {
  434. size_t len = 0;
  435. int i;
  436. for (i = 0; i < n; i++)
  437. len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
  438. buf[len - 1] = '\n';
  439. return len;
  440. }
  441. static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
  442. struct device_attribute *attr, char *buf)
  443. {
  444. struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
  445. return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
  446. data->chip_info->num_oversampling_temp_avail);
  447. }
  448. static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
  449. struct device_attribute *attr, char *buf)
  450. {
  451. struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
  452. return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
  453. data->chip_info->num_oversampling_press_avail);
  454. }
  455. static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
  456. S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
  457. static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
  458. S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
  459. static struct attribute *bmp280_attributes[] = {
  460. &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
  461. &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
  462. NULL,
  463. };
  464. static const struct attribute_group bmp280_attrs_group = {
  465. .attrs = bmp280_attributes,
  466. };
  467. static const struct iio_info bmp280_info = {
  468. .driver_module = THIS_MODULE,
  469. .read_raw = &bmp280_read_raw,
  470. .write_raw = &bmp280_write_raw,
  471. .attrs = &bmp280_attrs_group,
  472. };
  473. static int bmp280_chip_config(struct bmp280_data *data)
  474. {
  475. int ret;
  476. u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
  477. BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
  478. ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
  479. BMP280_OSRS_TEMP_MASK |
  480. BMP280_OSRS_PRESS_MASK |
  481. BMP280_MODE_MASK,
  482. osrs | BMP280_MODE_NORMAL);
  483. if (ret < 0) {
  484. dev_err(data->dev,
  485. "failed to write ctrl_meas register\n");
  486. return ret;
  487. }
  488. ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
  489. BMP280_FILTER_MASK,
  490. BMP280_FILTER_4X);
  491. if (ret < 0) {
  492. dev_err(data->dev,
  493. "failed to write config register\n");
  494. return ret;
  495. }
  496. return ret;
  497. }
  498. static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
  499. static const struct bmp280_chip_info bmp280_chip_info = {
  500. .oversampling_temp_avail = bmp280_oversampling_avail,
  501. .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
  502. .oversampling_press_avail = bmp280_oversampling_avail,
  503. .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
  504. .chip_config = bmp280_chip_config,
  505. .read_temp = bmp280_read_temp,
  506. .read_press = bmp280_read_press,
  507. };
  508. static int bme280_chip_config(struct bmp280_data *data)
  509. {
  510. int ret = bmp280_chip_config(data);
  511. u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
  512. if (ret < 0)
  513. return ret;
  514. return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
  515. BMP280_OSRS_HUMIDITY_MASK, osrs);
  516. }
  517. static const struct bmp280_chip_info bme280_chip_info = {
  518. .oversampling_temp_avail = bmp280_oversampling_avail,
  519. .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
  520. .oversampling_press_avail = bmp280_oversampling_avail,
  521. .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
  522. .oversampling_humid_avail = bmp280_oversampling_avail,
  523. .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
  524. .chip_config = bme280_chip_config,
  525. .read_temp = bmp280_read_temp,
  526. .read_press = bmp280_read_press,
  527. .read_humid = bmp280_read_humid,
  528. };
  529. static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
  530. {
  531. int ret;
  532. const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
  533. unsigned int delay_us;
  534. unsigned int ctrl;
  535. if (data->use_eoc)
  536. init_completion(&data->done);
  537. ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
  538. if (ret)
  539. return ret;
  540. if (data->use_eoc) {
  541. /*
  542. * If we have a completion interrupt, use it, wait up to
  543. * 100ms. The longest conversion time listed is 76.5 ms for
  544. * advanced resolution mode.
  545. */
  546. ret = wait_for_completion_timeout(&data->done,
  547. 1 + msecs_to_jiffies(100));
  548. if (!ret)
  549. dev_err(data->dev, "timeout waiting for completion\n");
  550. } else {
  551. if (ctrl_meas == BMP180_MEAS_TEMP)
  552. delay_us = 4500;
  553. else
  554. delay_us =
  555. conversion_time_max[data->oversampling_press];
  556. usleep_range(delay_us, delay_us + 1000);
  557. }
  558. ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
  559. if (ret)
  560. return ret;
  561. /* The value of this bit reset to "0" after conversion is complete */
  562. if (ctrl & BMP180_MEAS_SCO)
  563. return -EIO;
  564. return 0;
  565. }
  566. static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
  567. {
  568. int ret;
  569. __be16 tmp = 0;
  570. ret = bmp180_measure(data, BMP180_MEAS_TEMP);
  571. if (ret)
  572. return ret;
  573. ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
  574. if (ret)
  575. return ret;
  576. *val = be16_to_cpu(tmp);
  577. return 0;
  578. }
  579. static int bmp180_read_calib(struct bmp280_data *data,
  580. struct bmp180_calib *calib)
  581. {
  582. int ret;
  583. int i;
  584. __be16 buf[BMP180_REG_CALIB_COUNT / 2];
  585. ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
  586. sizeof(buf));
  587. if (ret < 0)
  588. return ret;
  589. /* None of the words has the value 0 or 0xFFFF */
  590. for (i = 0; i < ARRAY_SIZE(buf); i++) {
  591. if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
  592. return -EIO;
  593. }
  594. /* Toss the calibration data into the entropy pool */
  595. add_device_randomness(buf, sizeof(buf));
  596. calib->AC1 = be16_to_cpu(buf[AC1]);
  597. calib->AC2 = be16_to_cpu(buf[AC2]);
  598. calib->AC3 = be16_to_cpu(buf[AC3]);
  599. calib->AC4 = be16_to_cpu(buf[AC4]);
  600. calib->AC5 = be16_to_cpu(buf[AC5]);
  601. calib->AC6 = be16_to_cpu(buf[AC6]);
  602. calib->B1 = be16_to_cpu(buf[B1]);
  603. calib->B2 = be16_to_cpu(buf[B2]);
  604. calib->MB = be16_to_cpu(buf[MB]);
  605. calib->MC = be16_to_cpu(buf[MC]);
  606. calib->MD = be16_to_cpu(buf[MD]);
  607. return 0;
  608. }
  609. /*
  610. * Returns temperature in DegC, resolution is 0.1 DegC.
  611. * t_fine carries fine temperature as global value.
  612. *
  613. * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
  614. */
  615. static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
  616. {
  617. s32 x1, x2;
  618. struct bmp180_calib *calib = &data->calib;
  619. x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
  620. x2 = (calib->MC << 11) / (x1 + calib->MD);
  621. data->t_fine = x1 + x2;
  622. return (data->t_fine + 8) >> 4;
  623. }
  624. static int bmp180_read_temp(struct bmp280_data *data, int *val)
  625. {
  626. int ret;
  627. s32 adc_temp, comp_temp;
  628. ret = bmp180_read_adc_temp(data, &adc_temp);
  629. if (ret)
  630. return ret;
  631. comp_temp = bmp180_compensate_temp(data, adc_temp);
  632. /*
  633. * val might be NULL if we're called by the read_press routine,
  634. * who only cares about the carry over t_fine value.
  635. */
  636. if (val) {
  637. *val = comp_temp * 100;
  638. return IIO_VAL_INT;
  639. }
  640. return 0;
  641. }
  642. static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
  643. {
  644. int ret;
  645. __be32 tmp = 0;
  646. u8 oss = data->oversampling_press;
  647. ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
  648. if (ret)
  649. return ret;
  650. ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
  651. if (ret)
  652. return ret;
  653. *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
  654. return 0;
  655. }
  656. /*
  657. * Returns pressure in Pa, resolution is 1 Pa.
  658. *
  659. * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
  660. */
  661. static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
  662. {
  663. s32 x1, x2, x3, p;
  664. s32 b3, b6;
  665. u32 b4, b7;
  666. s32 oss = data->oversampling_press;
  667. struct bmp180_calib *calib = &data->calib;
  668. b6 = data->t_fine - 4000;
  669. x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
  670. x2 = calib->AC2 * b6 >> 11;
  671. x3 = x1 + x2;
  672. b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
  673. x1 = calib->AC3 * b6 >> 13;
  674. x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
  675. x3 = (x1 + x2 + 2) >> 2;
  676. b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
  677. b7 = ((u32)adc_press - b3) * (50000 >> oss);
  678. if (b7 < 0x80000000)
  679. p = (b7 * 2) / b4;
  680. else
  681. p = (b7 / b4) * 2;
  682. x1 = (p >> 8) * (p >> 8);
  683. x1 = (x1 * 3038) >> 16;
  684. x2 = (-7357 * p) >> 16;
  685. return p + ((x1 + x2 + 3791) >> 4);
  686. }
  687. static int bmp180_read_press(struct bmp280_data *data,
  688. int *val, int *val2)
  689. {
  690. int ret;
  691. s32 adc_press;
  692. u32 comp_press;
  693. /* Read and compensate temperature so we get a reading of t_fine. */
  694. ret = bmp180_read_temp(data, NULL);
  695. if (ret)
  696. return ret;
  697. ret = bmp180_read_adc_press(data, &adc_press);
  698. if (ret)
  699. return ret;
  700. comp_press = bmp180_compensate_press(data, adc_press);
  701. *val = comp_press;
  702. *val2 = 1000;
  703. return IIO_VAL_FRACTIONAL;
  704. }
  705. static int bmp180_chip_config(struct bmp280_data *data)
  706. {
  707. return 0;
  708. }
  709. static const int bmp180_oversampling_temp_avail[] = { 1 };
  710. static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
  711. static const struct bmp280_chip_info bmp180_chip_info = {
  712. .oversampling_temp_avail = bmp180_oversampling_temp_avail,
  713. .num_oversampling_temp_avail =
  714. ARRAY_SIZE(bmp180_oversampling_temp_avail),
  715. .oversampling_press_avail = bmp180_oversampling_press_avail,
  716. .num_oversampling_press_avail =
  717. ARRAY_SIZE(bmp180_oversampling_press_avail),
  718. .chip_config = bmp180_chip_config,
  719. .read_temp = bmp180_read_temp,
  720. .read_press = bmp180_read_press,
  721. };
  722. static irqreturn_t bmp085_eoc_irq(int irq, void *d)
  723. {
  724. struct bmp280_data *data = d;
  725. complete(&data->done);
  726. return IRQ_HANDLED;
  727. }
  728. static int bmp085_fetch_eoc_irq(struct device *dev,
  729. const char *name,
  730. int irq,
  731. struct bmp280_data *data)
  732. {
  733. unsigned long irq_trig;
  734. int ret;
  735. irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
  736. if (irq_trig != IRQF_TRIGGER_RISING) {
  737. dev_err(dev, "non-rising trigger given for EOC interrupt, "
  738. "trying to enforce it\n");
  739. irq_trig = IRQF_TRIGGER_RISING;
  740. }
  741. ret = devm_request_threaded_irq(dev,
  742. irq,
  743. bmp085_eoc_irq,
  744. NULL,
  745. irq_trig,
  746. name,
  747. data);
  748. if (ret) {
  749. /* Bail out without IRQ but keep the driver in place */
  750. dev_err(dev, "unable to request DRDY IRQ\n");
  751. return 0;
  752. }
  753. data->use_eoc = true;
  754. return 0;
  755. }
  756. int bmp280_common_probe(struct device *dev,
  757. struct regmap *regmap,
  758. unsigned int chip,
  759. const char *name,
  760. int irq)
  761. {
  762. int ret;
  763. struct iio_dev *indio_dev;
  764. struct bmp280_data *data;
  765. unsigned int chip_id;
  766. struct gpio_desc *gpiod;
  767. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  768. if (!indio_dev)
  769. return -ENOMEM;
  770. data = iio_priv(indio_dev);
  771. mutex_init(&data->lock);
  772. data->dev = dev;
  773. indio_dev->dev.parent = dev;
  774. indio_dev->name = name;
  775. indio_dev->channels = bmp280_channels;
  776. indio_dev->info = &bmp280_info;
  777. indio_dev->modes = INDIO_DIRECT_MODE;
  778. switch (chip) {
  779. case BMP180_CHIP_ID:
  780. indio_dev->num_channels = 2;
  781. data->chip_info = &bmp180_chip_info;
  782. data->oversampling_press = ilog2(8);
  783. data->oversampling_temp = ilog2(1);
  784. data->start_up_time = 10000;
  785. break;
  786. case BMP280_CHIP_ID:
  787. indio_dev->num_channels = 2;
  788. data->chip_info = &bmp280_chip_info;
  789. data->oversampling_press = ilog2(16);
  790. data->oversampling_temp = ilog2(2);
  791. data->start_up_time = 2000;
  792. break;
  793. case BME280_CHIP_ID:
  794. indio_dev->num_channels = 3;
  795. data->chip_info = &bme280_chip_info;
  796. data->oversampling_press = ilog2(16);
  797. data->oversampling_humid = ilog2(16);
  798. data->oversampling_temp = ilog2(2);
  799. data->start_up_time = 2000;
  800. break;
  801. default:
  802. return -EINVAL;
  803. }
  804. /* Bring up regulators */
  805. data->vddd = devm_regulator_get(dev, "vddd");
  806. if (IS_ERR(data->vddd)) {
  807. dev_err(dev, "failed to get VDDD regulator\n");
  808. return PTR_ERR(data->vddd);
  809. }
  810. ret = regulator_enable(data->vddd);
  811. if (ret) {
  812. dev_err(dev, "failed to enable VDDD regulator\n");
  813. return ret;
  814. }
  815. data->vdda = devm_regulator_get(dev, "vdda");
  816. if (IS_ERR(data->vdda)) {
  817. dev_err(dev, "failed to get VDDA regulator\n");
  818. ret = PTR_ERR(data->vdda);
  819. goto out_disable_vddd;
  820. }
  821. ret = regulator_enable(data->vdda);
  822. if (ret) {
  823. dev_err(dev, "failed to enable VDDA regulator\n");
  824. goto out_disable_vddd;
  825. }
  826. /* Wait to make sure we started up properly */
  827. usleep_range(data->start_up_time, data->start_up_time + 100);
  828. /* Bring chip out of reset if there is an assigned GPIO line */
  829. gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  830. /* Deassert the signal */
  831. if (!IS_ERR(gpiod)) {
  832. dev_info(dev, "release reset\n");
  833. gpiod_set_value(gpiod, 0);
  834. }
  835. data->regmap = regmap;
  836. ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
  837. if (ret < 0)
  838. goto out_disable_vdda;
  839. if (chip_id != chip) {
  840. dev_err(dev, "bad chip id: expected %x got %x\n",
  841. chip, chip_id);
  842. ret = -EINVAL;
  843. goto out_disable_vdda;
  844. }
  845. ret = data->chip_info->chip_config(data);
  846. if (ret < 0)
  847. goto out_disable_vdda;
  848. dev_set_drvdata(dev, indio_dev);
  849. /*
  850. * The BMP085 and BMP180 has calibration in an E2PROM, read it out
  851. * at probe time. It will not change.
  852. */
  853. if (chip_id == BMP180_CHIP_ID) {
  854. ret = bmp180_read_calib(data, &data->calib);
  855. if (ret < 0) {
  856. dev_err(data->dev,
  857. "failed to read calibration coefficients\n");
  858. goto out_disable_vdda;
  859. }
  860. }
  861. /*
  862. * Attempt to grab an optional EOC IRQ - only the BMP085 has this
  863. * however as it happens, the BMP085 shares the chip ID of BMP180
  864. * so we look for an IRQ if we have that.
  865. */
  866. if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
  867. ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
  868. if (ret)
  869. goto out_disable_vdda;
  870. }
  871. /* Enable runtime PM */
  872. pm_runtime_get_noresume(dev);
  873. pm_runtime_set_active(dev);
  874. pm_runtime_enable(dev);
  875. /*
  876. * Set autosuspend to two orders of magnitude larger than the
  877. * start-up time.
  878. */
  879. pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
  880. pm_runtime_use_autosuspend(dev);
  881. pm_runtime_put(dev);
  882. ret = iio_device_register(indio_dev);
  883. if (ret)
  884. goto out_runtime_pm_disable;
  885. return 0;
  886. out_runtime_pm_disable:
  887. pm_runtime_get_sync(data->dev);
  888. pm_runtime_put_noidle(data->dev);
  889. pm_runtime_disable(data->dev);
  890. out_disable_vdda:
  891. regulator_disable(data->vdda);
  892. out_disable_vddd:
  893. regulator_disable(data->vddd);
  894. return ret;
  895. }
  896. EXPORT_SYMBOL(bmp280_common_probe);
  897. int bmp280_common_remove(struct device *dev)
  898. {
  899. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  900. struct bmp280_data *data = iio_priv(indio_dev);
  901. iio_device_unregister(indio_dev);
  902. pm_runtime_get_sync(data->dev);
  903. pm_runtime_put_noidle(data->dev);
  904. pm_runtime_disable(data->dev);
  905. regulator_disable(data->vdda);
  906. regulator_disable(data->vddd);
  907. return 0;
  908. }
  909. EXPORT_SYMBOL(bmp280_common_remove);
  910. #ifdef CONFIG_PM
  911. static int bmp280_runtime_suspend(struct device *dev)
  912. {
  913. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  914. struct bmp280_data *data = iio_priv(indio_dev);
  915. int ret;
  916. ret = regulator_disable(data->vdda);
  917. if (ret)
  918. return ret;
  919. return regulator_disable(data->vddd);
  920. }
  921. static int bmp280_runtime_resume(struct device *dev)
  922. {
  923. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  924. struct bmp280_data *data = iio_priv(indio_dev);
  925. int ret;
  926. ret = regulator_enable(data->vddd);
  927. if (ret)
  928. return ret;
  929. ret = regulator_enable(data->vdda);
  930. if (ret)
  931. return ret;
  932. usleep_range(data->start_up_time, data->start_up_time + 100);
  933. return data->chip_info->chip_config(data);
  934. }
  935. #endif /* CONFIG_PM */
  936. const struct dev_pm_ops bmp280_dev_pm_ops = {
  937. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  938. pm_runtime_force_resume)
  939. SET_RUNTIME_PM_OPS(bmp280_runtime_suspend,
  940. bmp280_runtime_resume, NULL)
  941. };
  942. EXPORT_SYMBOL(bmp280_dev_pm_ops);
  943. MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
  944. MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
  945. MODULE_LICENSE("GPL v2");