bmp280-core.c 29 KB

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