bmp280-core.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. if (adc_temp == BMP280_TEMP_SKIPPED) {
  249. /* reading was skipped */
  250. dev_err(data->dev, "reading temperature skipped\n");
  251. return -EIO;
  252. }
  253. comp_temp = bmp280_compensate_temp(data, adc_temp);
  254. /*
  255. * val might be NULL if we're called by the read_press routine,
  256. * who only cares about the carry over t_fine value.
  257. */
  258. if (val) {
  259. *val = comp_temp * 10;
  260. return IIO_VAL_INT;
  261. }
  262. return 0;
  263. }
  264. static int bmp280_read_press(struct bmp280_data *data,
  265. int *val, int *val2)
  266. {
  267. int ret;
  268. __be32 tmp = 0;
  269. s32 adc_press;
  270. u32 comp_press;
  271. /* Read and compensate temperature so we get a reading of t_fine. */
  272. ret = bmp280_read_temp(data, NULL);
  273. if (ret < 0)
  274. return ret;
  275. ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
  276. (u8 *) &tmp, 3);
  277. if (ret < 0) {
  278. dev_err(data->dev, "failed to read pressure\n");
  279. return ret;
  280. }
  281. adc_press = be32_to_cpu(tmp) >> 12;
  282. if (adc_press == BMP280_PRESS_SKIPPED) {
  283. /* reading was skipped */
  284. dev_err(data->dev, "reading pressure skipped\n");
  285. return -EIO;
  286. }
  287. comp_press = bmp280_compensate_press(data, adc_press);
  288. *val = comp_press;
  289. *val2 = 256000;
  290. return IIO_VAL_FRACTIONAL;
  291. }
  292. static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
  293. {
  294. int ret;
  295. __be16 tmp = 0;
  296. s32 adc_humidity;
  297. u32 comp_humidity;
  298. /* Read and compensate temperature so we get a reading of t_fine. */
  299. ret = bmp280_read_temp(data, NULL);
  300. if (ret < 0)
  301. return ret;
  302. ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
  303. (u8 *) &tmp, 2);
  304. if (ret < 0) {
  305. dev_err(data->dev, "failed to read humidity\n");
  306. return ret;
  307. }
  308. adc_humidity = be16_to_cpu(tmp);
  309. if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
  310. /* reading was skipped */
  311. dev_err(data->dev, "reading humidity skipped\n");
  312. return -EIO;
  313. }
  314. comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
  315. *val = comp_humidity;
  316. *val2 = 1024;
  317. return IIO_VAL_FRACTIONAL;
  318. }
  319. static int bmp280_read_raw(struct iio_dev *indio_dev,
  320. struct iio_chan_spec const *chan,
  321. int *val, int *val2, long mask)
  322. {
  323. int ret;
  324. struct bmp280_data *data = iio_priv(indio_dev);
  325. pm_runtime_get_sync(data->dev);
  326. mutex_lock(&data->lock);
  327. switch (mask) {
  328. case IIO_CHAN_INFO_PROCESSED:
  329. switch (chan->type) {
  330. case IIO_HUMIDITYRELATIVE:
  331. ret = data->chip_info->read_humid(data, val, val2);
  332. break;
  333. case IIO_PRESSURE:
  334. ret = data->chip_info->read_press(data, val, val2);
  335. break;
  336. case IIO_TEMP:
  337. ret = data->chip_info->read_temp(data, val);
  338. break;
  339. default:
  340. ret = -EINVAL;
  341. break;
  342. }
  343. break;
  344. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  345. switch (chan->type) {
  346. case IIO_HUMIDITYRELATIVE:
  347. *val = 1 << data->oversampling_humid;
  348. ret = IIO_VAL_INT;
  349. break;
  350. case IIO_PRESSURE:
  351. *val = 1 << data->oversampling_press;
  352. ret = IIO_VAL_INT;
  353. break;
  354. case IIO_TEMP:
  355. *val = 1 << data->oversampling_temp;
  356. ret = IIO_VAL_INT;
  357. break;
  358. default:
  359. ret = -EINVAL;
  360. break;
  361. }
  362. break;
  363. default:
  364. ret = -EINVAL;
  365. break;
  366. }
  367. mutex_unlock(&data->lock);
  368. pm_runtime_mark_last_busy(data->dev);
  369. pm_runtime_put_autosuspend(data->dev);
  370. return ret;
  371. }
  372. static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
  373. int val)
  374. {
  375. int i;
  376. const int *avail = data->chip_info->oversampling_humid_avail;
  377. const int n = data->chip_info->num_oversampling_humid_avail;
  378. for (i = 0; i < n; i++) {
  379. if (avail[i] == val) {
  380. data->oversampling_humid = ilog2(val);
  381. return data->chip_info->chip_config(data);
  382. }
  383. }
  384. return -EINVAL;
  385. }
  386. static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
  387. int val)
  388. {
  389. int i;
  390. const int *avail = data->chip_info->oversampling_temp_avail;
  391. const int n = data->chip_info->num_oversampling_temp_avail;
  392. for (i = 0; i < n; i++) {
  393. if (avail[i] == val) {
  394. data->oversampling_temp = ilog2(val);
  395. return data->chip_info->chip_config(data);
  396. }
  397. }
  398. return -EINVAL;
  399. }
  400. static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
  401. int val)
  402. {
  403. int i;
  404. const int *avail = data->chip_info->oversampling_press_avail;
  405. const int n = data->chip_info->num_oversampling_press_avail;
  406. for (i = 0; i < n; i++) {
  407. if (avail[i] == val) {
  408. data->oversampling_press = ilog2(val);
  409. return data->chip_info->chip_config(data);
  410. }
  411. }
  412. return -EINVAL;
  413. }
  414. static int bmp280_write_raw(struct iio_dev *indio_dev,
  415. struct iio_chan_spec const *chan,
  416. int val, int val2, long mask)
  417. {
  418. int ret = 0;
  419. struct bmp280_data *data = iio_priv(indio_dev);
  420. switch (mask) {
  421. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  422. pm_runtime_get_sync(data->dev);
  423. mutex_lock(&data->lock);
  424. switch (chan->type) {
  425. case IIO_HUMIDITYRELATIVE:
  426. ret = bmp280_write_oversampling_ratio_humid(data, val);
  427. break;
  428. case IIO_PRESSURE:
  429. ret = bmp280_write_oversampling_ratio_press(data, val);
  430. break;
  431. case IIO_TEMP:
  432. ret = bmp280_write_oversampling_ratio_temp(data, val);
  433. break;
  434. default:
  435. ret = -EINVAL;
  436. break;
  437. }
  438. mutex_unlock(&data->lock);
  439. pm_runtime_mark_last_busy(data->dev);
  440. pm_runtime_put_autosuspend(data->dev);
  441. break;
  442. default:
  443. return -EINVAL;
  444. }
  445. return ret;
  446. }
  447. static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
  448. {
  449. size_t len = 0;
  450. int i;
  451. for (i = 0; i < n; i++)
  452. len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
  453. buf[len - 1] = '\n';
  454. return len;
  455. }
  456. static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
  457. struct device_attribute *attr, char *buf)
  458. {
  459. struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
  460. return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
  461. data->chip_info->num_oversampling_temp_avail);
  462. }
  463. static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
  464. struct device_attribute *attr, char *buf)
  465. {
  466. struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
  467. return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
  468. data->chip_info->num_oversampling_press_avail);
  469. }
  470. static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
  471. S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
  472. static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
  473. S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
  474. static struct attribute *bmp280_attributes[] = {
  475. &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
  476. &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
  477. NULL,
  478. };
  479. static const struct attribute_group bmp280_attrs_group = {
  480. .attrs = bmp280_attributes,
  481. };
  482. static const struct iio_info bmp280_info = {
  483. .driver_module = THIS_MODULE,
  484. .read_raw = &bmp280_read_raw,
  485. .write_raw = &bmp280_write_raw,
  486. .attrs = &bmp280_attrs_group,
  487. };
  488. static int bmp280_chip_config(struct bmp280_data *data)
  489. {
  490. int ret;
  491. u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
  492. BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
  493. ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
  494. BMP280_OSRS_TEMP_MASK |
  495. BMP280_OSRS_PRESS_MASK |
  496. BMP280_MODE_MASK,
  497. osrs | BMP280_MODE_NORMAL);
  498. if (ret < 0) {
  499. dev_err(data->dev,
  500. "failed to write ctrl_meas register\n");
  501. return ret;
  502. }
  503. ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
  504. BMP280_FILTER_MASK,
  505. BMP280_FILTER_4X);
  506. if (ret < 0) {
  507. dev_err(data->dev,
  508. "failed to write config register\n");
  509. return ret;
  510. }
  511. return ret;
  512. }
  513. static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
  514. static const struct bmp280_chip_info bmp280_chip_info = {
  515. .oversampling_temp_avail = bmp280_oversampling_avail,
  516. .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
  517. .oversampling_press_avail = bmp280_oversampling_avail,
  518. .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
  519. .chip_config = bmp280_chip_config,
  520. .read_temp = bmp280_read_temp,
  521. .read_press = bmp280_read_press,
  522. };
  523. static int bme280_chip_config(struct bmp280_data *data)
  524. {
  525. int ret;
  526. u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
  527. /*
  528. * Oversampling of humidity must be set before oversampling of
  529. * temperature/pressure is set to become effective.
  530. */
  531. ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
  532. BMP280_OSRS_HUMIDITY_MASK, osrs);
  533. if (ret < 0)
  534. return ret;
  535. return bmp280_chip_config(data);
  536. }
  537. static const struct bmp280_chip_info bme280_chip_info = {
  538. .oversampling_temp_avail = bmp280_oversampling_avail,
  539. .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
  540. .oversampling_press_avail = bmp280_oversampling_avail,
  541. .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
  542. .oversampling_humid_avail = bmp280_oversampling_avail,
  543. .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
  544. .chip_config = bme280_chip_config,
  545. .read_temp = bmp280_read_temp,
  546. .read_press = bmp280_read_press,
  547. .read_humid = bmp280_read_humid,
  548. };
  549. static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
  550. {
  551. int ret;
  552. const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
  553. unsigned int delay_us;
  554. unsigned int ctrl;
  555. if (data->use_eoc)
  556. init_completion(&data->done);
  557. ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
  558. if (ret)
  559. return ret;
  560. if (data->use_eoc) {
  561. /*
  562. * If we have a completion interrupt, use it, wait up to
  563. * 100ms. The longest conversion time listed is 76.5 ms for
  564. * advanced resolution mode.
  565. */
  566. ret = wait_for_completion_timeout(&data->done,
  567. 1 + msecs_to_jiffies(100));
  568. if (!ret)
  569. dev_err(data->dev, "timeout waiting for completion\n");
  570. } else {
  571. if (ctrl_meas == BMP180_MEAS_TEMP)
  572. delay_us = 4500;
  573. else
  574. delay_us =
  575. conversion_time_max[data->oversampling_press];
  576. usleep_range(delay_us, delay_us + 1000);
  577. }
  578. ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
  579. if (ret)
  580. return ret;
  581. /* The value of this bit reset to "0" after conversion is complete */
  582. if (ctrl & BMP180_MEAS_SCO)
  583. return -EIO;
  584. return 0;
  585. }
  586. static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
  587. {
  588. int ret;
  589. __be16 tmp = 0;
  590. ret = bmp180_measure(data, BMP180_MEAS_TEMP);
  591. if (ret)
  592. return ret;
  593. ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
  594. if (ret)
  595. return ret;
  596. *val = be16_to_cpu(tmp);
  597. return 0;
  598. }
  599. static int bmp180_read_calib(struct bmp280_data *data,
  600. struct bmp180_calib *calib)
  601. {
  602. int ret;
  603. int i;
  604. __be16 buf[BMP180_REG_CALIB_COUNT / 2];
  605. ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
  606. sizeof(buf));
  607. if (ret < 0)
  608. return ret;
  609. /* None of the words has the value 0 or 0xFFFF */
  610. for (i = 0; i < ARRAY_SIZE(buf); i++) {
  611. if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
  612. return -EIO;
  613. }
  614. /* Toss the calibration data into the entropy pool */
  615. add_device_randomness(buf, sizeof(buf));
  616. calib->AC1 = be16_to_cpu(buf[AC1]);
  617. calib->AC2 = be16_to_cpu(buf[AC2]);
  618. calib->AC3 = be16_to_cpu(buf[AC3]);
  619. calib->AC4 = be16_to_cpu(buf[AC4]);
  620. calib->AC5 = be16_to_cpu(buf[AC5]);
  621. calib->AC6 = be16_to_cpu(buf[AC6]);
  622. calib->B1 = be16_to_cpu(buf[B1]);
  623. calib->B2 = be16_to_cpu(buf[B2]);
  624. calib->MB = be16_to_cpu(buf[MB]);
  625. calib->MC = be16_to_cpu(buf[MC]);
  626. calib->MD = be16_to_cpu(buf[MD]);
  627. return 0;
  628. }
  629. /*
  630. * Returns temperature in DegC, resolution is 0.1 DegC.
  631. * t_fine carries fine temperature as global value.
  632. *
  633. * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
  634. */
  635. static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
  636. {
  637. s32 x1, x2;
  638. struct bmp180_calib *calib = &data->calib;
  639. x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
  640. x2 = (calib->MC << 11) / (x1 + calib->MD);
  641. data->t_fine = x1 + x2;
  642. return (data->t_fine + 8) >> 4;
  643. }
  644. static int bmp180_read_temp(struct bmp280_data *data, int *val)
  645. {
  646. int ret;
  647. s32 adc_temp, comp_temp;
  648. ret = bmp180_read_adc_temp(data, &adc_temp);
  649. if (ret)
  650. return ret;
  651. comp_temp = bmp180_compensate_temp(data, adc_temp);
  652. /*
  653. * val might be NULL if we're called by the read_press routine,
  654. * who only cares about the carry over t_fine value.
  655. */
  656. if (val) {
  657. *val = comp_temp * 100;
  658. return IIO_VAL_INT;
  659. }
  660. return 0;
  661. }
  662. static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
  663. {
  664. int ret;
  665. __be32 tmp = 0;
  666. u8 oss = data->oversampling_press;
  667. ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
  668. if (ret)
  669. return ret;
  670. ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
  671. if (ret)
  672. return ret;
  673. *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
  674. return 0;
  675. }
  676. /*
  677. * Returns pressure in Pa, resolution is 1 Pa.
  678. *
  679. * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
  680. */
  681. static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
  682. {
  683. s32 x1, x2, x3, p;
  684. s32 b3, b6;
  685. u32 b4, b7;
  686. s32 oss = data->oversampling_press;
  687. struct bmp180_calib *calib = &data->calib;
  688. b6 = data->t_fine - 4000;
  689. x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
  690. x2 = calib->AC2 * b6 >> 11;
  691. x3 = x1 + x2;
  692. b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
  693. x1 = calib->AC3 * b6 >> 13;
  694. x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
  695. x3 = (x1 + x2 + 2) >> 2;
  696. b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
  697. b7 = ((u32)adc_press - b3) * (50000 >> oss);
  698. if (b7 < 0x80000000)
  699. p = (b7 * 2) / b4;
  700. else
  701. p = (b7 / b4) * 2;
  702. x1 = (p >> 8) * (p >> 8);
  703. x1 = (x1 * 3038) >> 16;
  704. x2 = (-7357 * p) >> 16;
  705. return p + ((x1 + x2 + 3791) >> 4);
  706. }
  707. static int bmp180_read_press(struct bmp280_data *data,
  708. int *val, int *val2)
  709. {
  710. int ret;
  711. s32 adc_press;
  712. u32 comp_press;
  713. /* Read and compensate temperature so we get a reading of t_fine. */
  714. ret = bmp180_read_temp(data, NULL);
  715. if (ret)
  716. return ret;
  717. ret = bmp180_read_adc_press(data, &adc_press);
  718. if (ret)
  719. return ret;
  720. comp_press = bmp180_compensate_press(data, adc_press);
  721. *val = comp_press;
  722. *val2 = 1000;
  723. return IIO_VAL_FRACTIONAL;
  724. }
  725. static int bmp180_chip_config(struct bmp280_data *data)
  726. {
  727. return 0;
  728. }
  729. static const int bmp180_oversampling_temp_avail[] = { 1 };
  730. static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
  731. static const struct bmp280_chip_info bmp180_chip_info = {
  732. .oversampling_temp_avail = bmp180_oversampling_temp_avail,
  733. .num_oversampling_temp_avail =
  734. ARRAY_SIZE(bmp180_oversampling_temp_avail),
  735. .oversampling_press_avail = bmp180_oversampling_press_avail,
  736. .num_oversampling_press_avail =
  737. ARRAY_SIZE(bmp180_oversampling_press_avail),
  738. .chip_config = bmp180_chip_config,
  739. .read_temp = bmp180_read_temp,
  740. .read_press = bmp180_read_press,
  741. };
  742. static irqreturn_t bmp085_eoc_irq(int irq, void *d)
  743. {
  744. struct bmp280_data *data = d;
  745. complete(&data->done);
  746. return IRQ_HANDLED;
  747. }
  748. static int bmp085_fetch_eoc_irq(struct device *dev,
  749. const char *name,
  750. int irq,
  751. struct bmp280_data *data)
  752. {
  753. unsigned long irq_trig;
  754. int ret;
  755. irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
  756. if (irq_trig != IRQF_TRIGGER_RISING) {
  757. dev_err(dev, "non-rising trigger given for EOC interrupt, "
  758. "trying to enforce it\n");
  759. irq_trig = IRQF_TRIGGER_RISING;
  760. }
  761. ret = devm_request_threaded_irq(dev,
  762. irq,
  763. bmp085_eoc_irq,
  764. NULL,
  765. irq_trig,
  766. name,
  767. data);
  768. if (ret) {
  769. /* Bail out without IRQ but keep the driver in place */
  770. dev_err(dev, "unable to request DRDY IRQ\n");
  771. return 0;
  772. }
  773. data->use_eoc = true;
  774. return 0;
  775. }
  776. int bmp280_common_probe(struct device *dev,
  777. struct regmap *regmap,
  778. unsigned int chip,
  779. const char *name,
  780. int irq)
  781. {
  782. int ret;
  783. struct iio_dev *indio_dev;
  784. struct bmp280_data *data;
  785. unsigned int chip_id;
  786. struct gpio_desc *gpiod;
  787. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  788. if (!indio_dev)
  789. return -ENOMEM;
  790. data = iio_priv(indio_dev);
  791. mutex_init(&data->lock);
  792. data->dev = dev;
  793. indio_dev->dev.parent = dev;
  794. indio_dev->name = name;
  795. indio_dev->channels = bmp280_channels;
  796. indio_dev->info = &bmp280_info;
  797. indio_dev->modes = INDIO_DIRECT_MODE;
  798. switch (chip) {
  799. case BMP180_CHIP_ID:
  800. indio_dev->num_channels = 2;
  801. data->chip_info = &bmp180_chip_info;
  802. data->oversampling_press = ilog2(8);
  803. data->oversampling_temp = ilog2(1);
  804. data->start_up_time = 10000;
  805. break;
  806. case BMP280_CHIP_ID:
  807. indio_dev->num_channels = 2;
  808. data->chip_info = &bmp280_chip_info;
  809. data->oversampling_press = ilog2(16);
  810. data->oversampling_temp = ilog2(2);
  811. data->start_up_time = 2000;
  812. break;
  813. case BME280_CHIP_ID:
  814. indio_dev->num_channels = 3;
  815. data->chip_info = &bme280_chip_info;
  816. data->oversampling_press = ilog2(16);
  817. data->oversampling_humid = ilog2(16);
  818. data->oversampling_temp = ilog2(2);
  819. data->start_up_time = 2000;
  820. break;
  821. default:
  822. return -EINVAL;
  823. }
  824. /* Bring up regulators */
  825. data->vddd = devm_regulator_get(dev, "vddd");
  826. if (IS_ERR(data->vddd)) {
  827. dev_err(dev, "failed to get VDDD regulator\n");
  828. return PTR_ERR(data->vddd);
  829. }
  830. ret = regulator_enable(data->vddd);
  831. if (ret) {
  832. dev_err(dev, "failed to enable VDDD regulator\n");
  833. return ret;
  834. }
  835. data->vdda = devm_regulator_get(dev, "vdda");
  836. if (IS_ERR(data->vdda)) {
  837. dev_err(dev, "failed to get VDDA regulator\n");
  838. ret = PTR_ERR(data->vdda);
  839. goto out_disable_vddd;
  840. }
  841. ret = regulator_enable(data->vdda);
  842. if (ret) {
  843. dev_err(dev, "failed to enable VDDA regulator\n");
  844. goto out_disable_vddd;
  845. }
  846. /* Wait to make sure we started up properly */
  847. usleep_range(data->start_up_time, data->start_up_time + 100);
  848. /* Bring chip out of reset if there is an assigned GPIO line */
  849. gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  850. /* Deassert the signal */
  851. if (!IS_ERR(gpiod)) {
  852. dev_info(dev, "release reset\n");
  853. gpiod_set_value(gpiod, 0);
  854. }
  855. data->regmap = regmap;
  856. ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
  857. if (ret < 0)
  858. goto out_disable_vdda;
  859. if (chip_id != chip) {
  860. dev_err(dev, "bad chip id: expected %x got %x\n",
  861. chip, chip_id);
  862. ret = -EINVAL;
  863. goto out_disable_vdda;
  864. }
  865. ret = data->chip_info->chip_config(data);
  866. if (ret < 0)
  867. goto out_disable_vdda;
  868. dev_set_drvdata(dev, indio_dev);
  869. /*
  870. * The BMP085 and BMP180 has calibration in an E2PROM, read it out
  871. * at probe time. It will not change.
  872. */
  873. if (chip_id == BMP180_CHIP_ID) {
  874. ret = bmp180_read_calib(data, &data->calib);
  875. if (ret < 0) {
  876. dev_err(data->dev,
  877. "failed to read calibration coefficients\n");
  878. goto out_disable_vdda;
  879. }
  880. }
  881. /*
  882. * Attempt to grab an optional EOC IRQ - only the BMP085 has this
  883. * however as it happens, the BMP085 shares the chip ID of BMP180
  884. * so we look for an IRQ if we have that.
  885. */
  886. if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
  887. ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
  888. if (ret)
  889. goto out_disable_vdda;
  890. }
  891. /* Enable runtime PM */
  892. pm_runtime_get_noresume(dev);
  893. pm_runtime_set_active(dev);
  894. pm_runtime_enable(dev);
  895. /*
  896. * Set autosuspend to two orders of magnitude larger than the
  897. * start-up time.
  898. */
  899. pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
  900. pm_runtime_use_autosuspend(dev);
  901. pm_runtime_put(dev);
  902. ret = iio_device_register(indio_dev);
  903. if (ret)
  904. goto out_runtime_pm_disable;
  905. return 0;
  906. out_runtime_pm_disable:
  907. pm_runtime_get_sync(data->dev);
  908. pm_runtime_put_noidle(data->dev);
  909. pm_runtime_disable(data->dev);
  910. out_disable_vdda:
  911. regulator_disable(data->vdda);
  912. out_disable_vddd:
  913. regulator_disable(data->vddd);
  914. return ret;
  915. }
  916. EXPORT_SYMBOL(bmp280_common_probe);
  917. int bmp280_common_remove(struct device *dev)
  918. {
  919. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  920. struct bmp280_data *data = iio_priv(indio_dev);
  921. iio_device_unregister(indio_dev);
  922. pm_runtime_get_sync(data->dev);
  923. pm_runtime_put_noidle(data->dev);
  924. pm_runtime_disable(data->dev);
  925. regulator_disable(data->vdda);
  926. regulator_disable(data->vddd);
  927. return 0;
  928. }
  929. EXPORT_SYMBOL(bmp280_common_remove);
  930. #ifdef CONFIG_PM
  931. static int bmp280_runtime_suspend(struct device *dev)
  932. {
  933. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  934. struct bmp280_data *data = iio_priv(indio_dev);
  935. int ret;
  936. ret = regulator_disable(data->vdda);
  937. if (ret)
  938. return ret;
  939. return regulator_disable(data->vddd);
  940. }
  941. static int bmp280_runtime_resume(struct device *dev)
  942. {
  943. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  944. struct bmp280_data *data = iio_priv(indio_dev);
  945. int ret;
  946. ret = regulator_enable(data->vddd);
  947. if (ret)
  948. return ret;
  949. ret = regulator_enable(data->vdda);
  950. if (ret)
  951. return ret;
  952. usleep_range(data->start_up_time, data->start_up_time + 100);
  953. return data->chip_info->chip_config(data);
  954. }
  955. #endif /* CONFIG_PM */
  956. const struct dev_pm_ops bmp280_dev_pm_ops = {
  957. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  958. pm_runtime_force_resume)
  959. SET_RUNTIME_PM_OPS(bmp280_runtime_suspend,
  960. bmp280_runtime_resume, NULL)
  961. };
  962. EXPORT_SYMBOL(bmp280_dev_pm_ops);
  963. MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
  964. MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
  965. MODULE_LICENSE("GPL v2");