st_pressure_core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * STMicroelectronics pressures driver
  3. *
  4. * Copyright 2013 STMicroelectronics Inc.
  5. *
  6. * Denis Ciocca <denis.ciocca@st.com>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/errno.h>
  14. #include <linux/types.h>
  15. #include <linux/mutex.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/i2c.h>
  18. #include <linux/gpio.h>
  19. #include <linux/irq.h>
  20. #include <linux/delay.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #include <linux/iio/trigger.h>
  24. #include <linux/iio/buffer.h>
  25. #include <asm/unaligned.h>
  26. #include <linux/iio/common/st_sensors.h>
  27. #include "st_pressure.h"
  28. /*
  29. * About determining pressure scaling factors
  30. * ------------------------------------------
  31. *
  32. * Datasheets specify typical pressure sensitivity so that pressure is computed
  33. * according to the following equation :
  34. * pressure[mBar] = raw / sensitivity
  35. * where :
  36. * raw the 24 bits long raw sampled pressure
  37. * sensitivity a scaling factor specified by the datasheet in LSB/mBar
  38. *
  39. * IIO ABI expects pressure to be expressed as kPascal, hence pressure should be
  40. * computed according to :
  41. * pressure[kPascal] = pressure[mBar] / 10
  42. * = raw / (sensitivity * 10) (1)
  43. *
  44. * Finally, st_press_read_raw() returns pressure scaling factor as an
  45. * IIO_VAL_INT_PLUS_NANO with a zero integral part and "gain" as decimal part.
  46. * Therefore, from (1), "gain" becomes :
  47. * gain = 10^9 / (sensitivity * 10)
  48. * = 10^8 / sensitivity
  49. *
  50. * About determining temperature scaling factors and offsets
  51. * ---------------------------------------------------------
  52. *
  53. * Datasheets specify typical temperature sensitivity and offset so that
  54. * temperature is computed according to the following equation :
  55. * temp[Celsius] = offset[Celsius] + (raw / sensitivity)
  56. * where :
  57. * raw the 16 bits long raw sampled temperature
  58. * offset a constant specified by the datasheet in degree Celsius
  59. * (sometimes zero)
  60. * sensitivity a scaling factor specified by the datasheet in LSB/Celsius
  61. *
  62. * IIO ABI expects temperature to be expressed as milli degree Celsius such as
  63. * user space should compute temperature according to :
  64. * temp[mCelsius] = temp[Celsius] * 10^3
  65. * = (offset[Celsius] + (raw / sensitivity)) * 10^3
  66. * = ((offset[Celsius] * sensitivity) + raw) *
  67. * (10^3 / sensitivity) (2)
  68. *
  69. * IIO ABI expects user space to apply offset and scaling factors to raw samples
  70. * according to :
  71. * temp[mCelsius] = (OFFSET + raw) * SCALE
  72. * where :
  73. * OFFSET an arbitrary constant exposed by device
  74. * SCALE an arbitrary scaling factor exposed by device
  75. *
  76. * Matching OFFSET and SCALE with members of (2) gives :
  77. * OFFSET = offset[Celsius] * sensitivity (3)
  78. * SCALE = 10^3 / sensitivity (4)
  79. *
  80. * st_press_read_raw() returns temperature scaling factor as an
  81. * IIO_VAL_FRACTIONAL with a 10^3 numerator and "gain2" as denominator.
  82. * Therefore, from (3), "gain2" becomes :
  83. * gain2 = sensitivity
  84. *
  85. * When declared within channel, i.e. for a non zero specified offset,
  86. * st_press_read_raw() will return the latter as an IIO_VAL_FRACTIONAL such as :
  87. * numerator = OFFSET * 10^3
  88. * denominator = 10^3
  89. * giving from (4):
  90. * numerator = offset[Celsius] * 10^3 * sensitivity
  91. * = offset[mCelsius] * gain2
  92. */
  93. #define MCELSIUS_PER_CELSIUS 1000
  94. /* Default pressure sensitivity */
  95. #define ST_PRESS_LSB_PER_MBAR 4096UL
  96. #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
  97. ST_PRESS_LSB_PER_MBAR)
  98. /* Default temperature sensitivity */
  99. #define ST_PRESS_LSB_PER_CELSIUS 480UL
  100. #define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL
  101. /* FULLSCALE */
  102. #define ST_PRESS_FS_AVL_1100MB 1100
  103. #define ST_PRESS_FS_AVL_1260MB 1260
  104. #define ST_PRESS_1_OUT_XL_ADDR 0x28
  105. #define ST_TEMP_1_OUT_L_ADDR 0x2b
  106. /* LPS001WP pressure resolution */
  107. #define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL
  108. /* LPS001WP temperature resolution */
  109. #define ST_PRESS_LPS001WP_LSB_PER_CELSIUS 64UL
  110. /* LPS001WP pressure gain */
  111. #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \
  112. (100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR)
  113. /* LPS001WP pressure and temp L addresses */
  114. #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
  115. #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
  116. /* LPS25H pressure and temp L addresses */
  117. #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
  118. #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b
  119. /* LPS22HB temperature sensitivity */
  120. #define ST_PRESS_LPS22HB_LSB_PER_CELSIUS 100UL
  121. static const struct iio_chan_spec st_press_1_channels[] = {
  122. {
  123. .type = IIO_PRESSURE,
  124. .address = ST_PRESS_1_OUT_XL_ADDR,
  125. .scan_index = 0,
  126. .scan_type = {
  127. .sign = 's',
  128. .realbits = 24,
  129. .storagebits = 32,
  130. .endianness = IIO_LE,
  131. },
  132. .info_mask_separate =
  133. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  134. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  135. },
  136. {
  137. .type = IIO_TEMP,
  138. .address = ST_TEMP_1_OUT_L_ADDR,
  139. .scan_index = 1,
  140. .scan_type = {
  141. .sign = 's',
  142. .realbits = 16,
  143. .storagebits = 16,
  144. .endianness = IIO_LE,
  145. },
  146. .info_mask_separate =
  147. BIT(IIO_CHAN_INFO_RAW) |
  148. BIT(IIO_CHAN_INFO_SCALE) |
  149. BIT(IIO_CHAN_INFO_OFFSET),
  150. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  151. },
  152. IIO_CHAN_SOFT_TIMESTAMP(2)
  153. };
  154. static const struct iio_chan_spec st_press_lps001wp_channels[] = {
  155. {
  156. .type = IIO_PRESSURE,
  157. .address = ST_PRESS_LPS001WP_OUT_L_ADDR,
  158. .scan_index = 0,
  159. .scan_type = {
  160. .sign = 's',
  161. .realbits = 16,
  162. .storagebits = 16,
  163. .endianness = IIO_LE,
  164. },
  165. .info_mask_separate =
  166. BIT(IIO_CHAN_INFO_RAW) |
  167. BIT(IIO_CHAN_INFO_SCALE),
  168. },
  169. {
  170. .type = IIO_TEMP,
  171. .address = ST_TEMP_LPS001WP_OUT_L_ADDR,
  172. .scan_index = 1,
  173. .scan_type = {
  174. .sign = 's',
  175. .realbits = 16,
  176. .storagebits = 16,
  177. .endianness = IIO_LE,
  178. },
  179. .info_mask_separate =
  180. BIT(IIO_CHAN_INFO_RAW) |
  181. BIT(IIO_CHAN_INFO_SCALE),
  182. },
  183. IIO_CHAN_SOFT_TIMESTAMP(2)
  184. };
  185. static const struct iio_chan_spec st_press_lps22hb_channels[] = {
  186. {
  187. .type = IIO_PRESSURE,
  188. .address = ST_PRESS_1_OUT_XL_ADDR,
  189. .scan_index = 0,
  190. .scan_type = {
  191. .sign = 's',
  192. .realbits = 24,
  193. .storagebits = 32,
  194. .endianness = IIO_LE,
  195. },
  196. .info_mask_separate =
  197. BIT(IIO_CHAN_INFO_RAW) |
  198. BIT(IIO_CHAN_INFO_SCALE),
  199. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  200. },
  201. {
  202. .type = IIO_TEMP,
  203. .address = ST_TEMP_1_OUT_L_ADDR,
  204. .scan_index = 1,
  205. .scan_type = {
  206. .sign = 's',
  207. .realbits = 16,
  208. .storagebits = 16,
  209. .endianness = IIO_LE,
  210. },
  211. .info_mask_separate =
  212. BIT(IIO_CHAN_INFO_RAW) |
  213. BIT(IIO_CHAN_INFO_SCALE),
  214. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  215. },
  216. IIO_CHAN_SOFT_TIMESTAMP(2)
  217. };
  218. static const struct st_sensor_settings st_press_sensors_settings[] = {
  219. {
  220. /*
  221. * CUSTOM VALUES FOR LPS331AP SENSOR
  222. * See LPS331AP datasheet:
  223. * http://www2.st.com/resource/en/datasheet/lps331ap.pdf
  224. */
  225. .wai = 0xbb,
  226. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  227. .sensors_supported = {
  228. [0] = LPS331AP_PRESS_DEV_NAME,
  229. },
  230. .ch = (struct iio_chan_spec *)st_press_1_channels,
  231. .num_ch = ARRAY_SIZE(st_press_1_channels),
  232. .odr = {
  233. .addr = 0x20,
  234. .mask = 0x70,
  235. .odr_avl = {
  236. { .hz = 1, .value = 0x01 },
  237. { .hz = 7, .value = 0x05 },
  238. { .hz = 13, .value = 0x06 },
  239. { .hz = 25, .value = 0x07 },
  240. },
  241. },
  242. .pw = {
  243. .addr = 0x20,
  244. .mask = 0x80,
  245. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  246. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  247. },
  248. .fs = {
  249. .addr = 0x23,
  250. .mask = 0x30,
  251. .fs_avl = {
  252. /*
  253. * Pressure and temperature sensitivity values
  254. * as defined in table 3 of LPS331AP datasheet.
  255. */
  256. [0] = {
  257. .num = ST_PRESS_FS_AVL_1260MB,
  258. .gain = ST_PRESS_KPASCAL_NANO_SCALE,
  259. .gain2 = ST_PRESS_LSB_PER_CELSIUS,
  260. },
  261. },
  262. },
  263. .bdu = {
  264. .addr = 0x20,
  265. .mask = 0x04,
  266. },
  267. .drdy_irq = {
  268. .int1 = {
  269. .addr = 0x22,
  270. .mask = 0x04,
  271. .addr_od = 0x22,
  272. .mask_od = 0x40,
  273. },
  274. .int2 = {
  275. .addr = 0x22,
  276. .mask = 0x20,
  277. .addr_od = 0x22,
  278. .mask_od = 0x40,
  279. },
  280. .addr_ihl = 0x22,
  281. .mask_ihl = 0x80,
  282. .stat_drdy = {
  283. .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
  284. .mask = 0x03,
  285. },
  286. },
  287. .sim = {
  288. .addr = 0x20,
  289. .value = BIT(0),
  290. },
  291. .multi_read_bit = true,
  292. .bootime = 2,
  293. },
  294. {
  295. /*
  296. * CUSTOM VALUES FOR LPS001WP SENSOR
  297. */
  298. .wai = 0xba,
  299. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  300. .sensors_supported = {
  301. [0] = LPS001WP_PRESS_DEV_NAME,
  302. },
  303. .ch = (struct iio_chan_spec *)st_press_lps001wp_channels,
  304. .num_ch = ARRAY_SIZE(st_press_lps001wp_channels),
  305. .odr = {
  306. .addr = 0x20,
  307. .mask = 0x30,
  308. .odr_avl = {
  309. { .hz = 1, .value = 0x01 },
  310. { .hz = 7, .value = 0x02 },
  311. { .hz = 13, .value = 0x03 },
  312. },
  313. },
  314. .pw = {
  315. .addr = 0x20,
  316. .mask = 0x40,
  317. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  318. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  319. },
  320. .fs = {
  321. .fs_avl = {
  322. /*
  323. * Pressure and temperature resolution values
  324. * as defined in table 3 of LPS001WP datasheet.
  325. */
  326. [0] = {
  327. .num = ST_PRESS_FS_AVL_1100MB,
  328. .gain = ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN,
  329. .gain2 = ST_PRESS_LPS001WP_LSB_PER_CELSIUS,
  330. },
  331. },
  332. },
  333. .bdu = {
  334. .addr = 0x20,
  335. .mask = 0x04,
  336. },
  337. .sim = {
  338. .addr = 0x20,
  339. .value = BIT(0),
  340. },
  341. .multi_read_bit = true,
  342. .bootime = 2,
  343. },
  344. {
  345. /*
  346. * CUSTOM VALUES FOR LPS25H SENSOR
  347. * See LPS25H datasheet:
  348. * http://www2.st.com/resource/en/datasheet/lps25h.pdf
  349. */
  350. .wai = 0xbd,
  351. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  352. .sensors_supported = {
  353. [0] = LPS25H_PRESS_DEV_NAME,
  354. },
  355. .ch = (struct iio_chan_spec *)st_press_1_channels,
  356. .num_ch = ARRAY_SIZE(st_press_1_channels),
  357. .odr = {
  358. .addr = 0x20,
  359. .mask = 0x70,
  360. .odr_avl = {
  361. { .hz = 1, .value = 0x01 },
  362. { .hz = 7, .value = 0x02 },
  363. { .hz = 13, .value = 0x03 },
  364. { .hz = 25, .value = 0x04 },
  365. },
  366. },
  367. .pw = {
  368. .addr = 0x20,
  369. .mask = 0x80,
  370. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  371. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  372. },
  373. .fs = {
  374. .fs_avl = {
  375. /*
  376. * Pressure and temperature sensitivity values
  377. * as defined in table 3 of LPS25H datasheet.
  378. */
  379. [0] = {
  380. .num = ST_PRESS_FS_AVL_1260MB,
  381. .gain = ST_PRESS_KPASCAL_NANO_SCALE,
  382. .gain2 = ST_PRESS_LSB_PER_CELSIUS,
  383. },
  384. },
  385. },
  386. .bdu = {
  387. .addr = 0x20,
  388. .mask = 0x04,
  389. },
  390. .drdy_irq = {
  391. .int1 = {
  392. .addr = 0x23,
  393. .mask = 0x01,
  394. .addr_od = 0x22,
  395. .mask_od = 0x40,
  396. },
  397. .addr_ihl = 0x22,
  398. .mask_ihl = 0x80,
  399. .stat_drdy = {
  400. .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
  401. .mask = 0x03,
  402. },
  403. },
  404. .sim = {
  405. .addr = 0x20,
  406. .value = BIT(0),
  407. },
  408. .multi_read_bit = true,
  409. .bootime = 2,
  410. },
  411. {
  412. /*
  413. * CUSTOM VALUES FOR LPS22HB SENSOR
  414. * See LPS22HB datasheet:
  415. * http://www2.st.com/resource/en/datasheet/lps22hb.pdf
  416. */
  417. .wai = 0xb1,
  418. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  419. .sensors_supported = {
  420. [0] = LPS22HB_PRESS_DEV_NAME,
  421. [1] = LPS33HW_PRESS_DEV_NAME,
  422. [2] = LPS35HW_PRESS_DEV_NAME,
  423. },
  424. .ch = (struct iio_chan_spec *)st_press_lps22hb_channels,
  425. .num_ch = ARRAY_SIZE(st_press_lps22hb_channels),
  426. .odr = {
  427. .addr = 0x10,
  428. .mask = 0x70,
  429. .odr_avl = {
  430. { .hz = 1, .value = 0x01 },
  431. { .hz = 10, .value = 0x02 },
  432. { .hz = 25, .value = 0x03 },
  433. { .hz = 50, .value = 0x04 },
  434. { .hz = 75, .value = 0x05 },
  435. },
  436. },
  437. .pw = {
  438. .addr = 0x10,
  439. .mask = 0x70,
  440. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  441. },
  442. .fs = {
  443. .fs_avl = {
  444. /*
  445. * Pressure and temperature sensitivity values
  446. * as defined in table 3 of LPS22HB datasheet.
  447. */
  448. [0] = {
  449. .num = ST_PRESS_FS_AVL_1260MB,
  450. .gain = ST_PRESS_KPASCAL_NANO_SCALE,
  451. .gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS,
  452. },
  453. },
  454. },
  455. .bdu = {
  456. .addr = 0x10,
  457. .mask = 0x02,
  458. },
  459. .drdy_irq = {
  460. .int1 = {
  461. .addr = 0x12,
  462. .mask = 0x04,
  463. .addr_od = 0x12,
  464. .mask_od = 0x40,
  465. },
  466. .addr_ihl = 0x12,
  467. .mask_ihl = 0x80,
  468. .stat_drdy = {
  469. .addr = ST_SENSORS_DEFAULT_STAT_ADDR,
  470. .mask = 0x03,
  471. },
  472. },
  473. .sim = {
  474. .addr = 0x10,
  475. .value = BIT(0),
  476. },
  477. .multi_read_bit = false,
  478. .bootime = 2,
  479. },
  480. };
  481. static int st_press_write_raw(struct iio_dev *indio_dev,
  482. struct iio_chan_spec const *ch,
  483. int val,
  484. int val2,
  485. long mask)
  486. {
  487. int err;
  488. switch (mask) {
  489. case IIO_CHAN_INFO_SAMP_FREQ:
  490. if (val2)
  491. return -EINVAL;
  492. mutex_lock(&indio_dev->mlock);
  493. err = st_sensors_set_odr(indio_dev, val);
  494. mutex_unlock(&indio_dev->mlock);
  495. return err;
  496. default:
  497. return -EINVAL;
  498. }
  499. }
  500. static int st_press_read_raw(struct iio_dev *indio_dev,
  501. struct iio_chan_spec const *ch, int *val,
  502. int *val2, long mask)
  503. {
  504. int err;
  505. struct st_sensor_data *press_data = iio_priv(indio_dev);
  506. switch (mask) {
  507. case IIO_CHAN_INFO_RAW:
  508. err = st_sensors_read_info_raw(indio_dev, ch, val);
  509. if (err < 0)
  510. goto read_error;
  511. return IIO_VAL_INT;
  512. case IIO_CHAN_INFO_SCALE:
  513. switch (ch->type) {
  514. case IIO_PRESSURE:
  515. *val = 0;
  516. *val2 = press_data->current_fullscale->gain;
  517. return IIO_VAL_INT_PLUS_NANO;
  518. case IIO_TEMP:
  519. *val = MCELSIUS_PER_CELSIUS;
  520. *val2 = press_data->current_fullscale->gain2;
  521. return IIO_VAL_FRACTIONAL;
  522. default:
  523. err = -EINVAL;
  524. goto read_error;
  525. }
  526. case IIO_CHAN_INFO_OFFSET:
  527. switch (ch->type) {
  528. case IIO_TEMP:
  529. *val = ST_PRESS_MILLI_CELSIUS_OFFSET *
  530. press_data->current_fullscale->gain2;
  531. *val2 = MCELSIUS_PER_CELSIUS;
  532. break;
  533. default:
  534. err = -EINVAL;
  535. goto read_error;
  536. }
  537. return IIO_VAL_FRACTIONAL;
  538. case IIO_CHAN_INFO_SAMP_FREQ:
  539. *val = press_data->odr;
  540. return IIO_VAL_INT;
  541. default:
  542. return -EINVAL;
  543. }
  544. read_error:
  545. return err;
  546. }
  547. static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
  548. static struct attribute *st_press_attributes[] = {
  549. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  550. NULL,
  551. };
  552. static const struct attribute_group st_press_attribute_group = {
  553. .attrs = st_press_attributes,
  554. };
  555. static const struct iio_info press_info = {
  556. .attrs = &st_press_attribute_group,
  557. .read_raw = &st_press_read_raw,
  558. .write_raw = &st_press_write_raw,
  559. .debugfs_reg_access = &st_sensors_debugfs_reg_access,
  560. };
  561. #ifdef CONFIG_IIO_TRIGGER
  562. static const struct iio_trigger_ops st_press_trigger_ops = {
  563. .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
  564. .validate_device = st_sensors_validate_device,
  565. };
  566. #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
  567. #else
  568. #define ST_PRESS_TRIGGER_OPS NULL
  569. #endif
  570. int st_press_common_probe(struct iio_dev *indio_dev)
  571. {
  572. struct st_sensor_data *press_data = iio_priv(indio_dev);
  573. struct st_sensors_platform_data *pdata =
  574. (struct st_sensors_platform_data *)press_data->dev->platform_data;
  575. int irq = press_data->get_irq_data_ready(indio_dev);
  576. int err;
  577. indio_dev->modes = INDIO_DIRECT_MODE;
  578. indio_dev->info = &press_info;
  579. mutex_init(&press_data->tb.buf_lock);
  580. err = st_sensors_power_enable(indio_dev);
  581. if (err)
  582. return err;
  583. err = st_sensors_check_device_support(indio_dev,
  584. ARRAY_SIZE(st_press_sensors_settings),
  585. st_press_sensors_settings);
  586. if (err < 0)
  587. goto st_press_power_off;
  588. /*
  589. * Skip timestamping channel while declaring available channels to
  590. * common st_sensor layer. Look at st_sensors_get_buffer_element() to
  591. * see how timestamps are explicitly pushed as last samples block
  592. * element.
  593. */
  594. press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
  595. press_data->multiread_bit = press_data->sensor_settings->multi_read_bit;
  596. indio_dev->channels = press_data->sensor_settings->ch;
  597. indio_dev->num_channels = press_data->sensor_settings->num_ch;
  598. press_data->current_fullscale =
  599. (struct st_sensor_fullscale_avl *)
  600. &press_data->sensor_settings->fs.fs_avl[0];
  601. press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz;
  602. /* Some devices don't support a data ready pin. */
  603. if (!pdata && (press_data->sensor_settings->drdy_irq.int1.addr ||
  604. press_data->sensor_settings->drdy_irq.int2.addr))
  605. pdata = (struct st_sensors_platform_data *)&default_press_pdata;
  606. err = st_sensors_init_sensor(indio_dev, pdata);
  607. if (err < 0)
  608. goto st_press_power_off;
  609. err = st_press_allocate_ring(indio_dev);
  610. if (err < 0)
  611. goto st_press_power_off;
  612. if (irq > 0) {
  613. err = st_sensors_allocate_trigger(indio_dev,
  614. ST_PRESS_TRIGGER_OPS);
  615. if (err < 0)
  616. goto st_press_probe_trigger_error;
  617. }
  618. err = iio_device_register(indio_dev);
  619. if (err)
  620. goto st_press_device_register_error;
  621. dev_info(&indio_dev->dev, "registered pressure sensor %s\n",
  622. indio_dev->name);
  623. return err;
  624. st_press_device_register_error:
  625. if (irq > 0)
  626. st_sensors_deallocate_trigger(indio_dev);
  627. st_press_probe_trigger_error:
  628. st_press_deallocate_ring(indio_dev);
  629. st_press_power_off:
  630. st_sensors_power_disable(indio_dev);
  631. return err;
  632. }
  633. EXPORT_SYMBOL(st_press_common_probe);
  634. void st_press_common_remove(struct iio_dev *indio_dev)
  635. {
  636. struct st_sensor_data *press_data = iio_priv(indio_dev);
  637. st_sensors_power_disable(indio_dev);
  638. iio_device_unregister(indio_dev);
  639. if (press_data->get_irq_data_ready(indio_dev) > 0)
  640. st_sensors_deallocate_trigger(indio_dev);
  641. st_press_deallocate_ring(indio_dev);
  642. }
  643. EXPORT_SYMBOL(st_press_common_remove);
  644. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  645. MODULE_DESCRIPTION("STMicroelectronics pressures driver");
  646. MODULE_LICENSE("GPL v2");