bmp280-core.c 27 KB

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