bmp280-core.c 29 KB

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