st_pressure_core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. /*
  107. * CUSTOM VALUES FOR LPS331AP SENSOR
  108. * See LPS331AP datasheet:
  109. * http://www2.st.com/resource/en/datasheet/lps331ap.pdf
  110. */
  111. #define ST_PRESS_LPS331AP_WAI_EXP 0xbb
  112. #define ST_PRESS_LPS331AP_ODR_ADDR 0x20
  113. #define ST_PRESS_LPS331AP_ODR_MASK 0x70
  114. #define ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL 0x01
  115. #define ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL 0x05
  116. #define ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL 0x06
  117. #define ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL 0x07
  118. #define ST_PRESS_LPS331AP_PW_ADDR 0x20
  119. #define ST_PRESS_LPS331AP_PW_MASK 0x80
  120. #define ST_PRESS_LPS331AP_FS_ADDR 0x23
  121. #define ST_PRESS_LPS331AP_FS_MASK 0x30
  122. #define ST_PRESS_LPS331AP_BDU_ADDR 0x20
  123. #define ST_PRESS_LPS331AP_BDU_MASK 0x04
  124. #define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR 0x22
  125. #define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK 0x04
  126. #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20
  127. #define ST_PRESS_LPS331AP_IHL_IRQ_ADDR 0x22
  128. #define ST_PRESS_LPS331AP_IHL_IRQ_MASK 0x80
  129. #define ST_PRESS_LPS331AP_OD_IRQ_ADDR 0x22
  130. #define ST_PRESS_LPS331AP_OD_IRQ_MASK 0x40
  131. #define ST_PRESS_LPS331AP_MULTIREAD_BIT true
  132. /*
  133. * CUSTOM VALUES FOR THE OBSOLETE LPS001WP SENSOR
  134. */
  135. /* LPS001WP pressure resolution */
  136. #define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL
  137. /* LPS001WP temperature resolution */
  138. #define ST_PRESS_LPS001WP_LSB_PER_CELSIUS 64UL
  139. #define ST_PRESS_LPS001WP_WAI_EXP 0xba
  140. #define ST_PRESS_LPS001WP_ODR_ADDR 0x20
  141. #define ST_PRESS_LPS001WP_ODR_MASK 0x30
  142. #define ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL 0x01
  143. #define ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL 0x02
  144. #define ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL 0x03
  145. #define ST_PRESS_LPS001WP_PW_ADDR 0x20
  146. #define ST_PRESS_LPS001WP_PW_MASK 0x40
  147. #define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \
  148. (100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR)
  149. #define ST_PRESS_LPS001WP_BDU_ADDR 0x20
  150. #define ST_PRESS_LPS001WP_BDU_MASK 0x04
  151. #define ST_PRESS_LPS001WP_MULTIREAD_BIT true
  152. #define ST_PRESS_LPS001WP_OUT_L_ADDR 0x28
  153. #define ST_TEMP_LPS001WP_OUT_L_ADDR 0x2a
  154. /*
  155. * CUSTOM VALUES FOR LPS25H SENSOR
  156. * See LPS25H datasheet:
  157. * http://www2.st.com/resource/en/datasheet/lps25h.pdf
  158. */
  159. #define ST_PRESS_LPS25H_WAI_EXP 0xbd
  160. #define ST_PRESS_LPS25H_ODR_ADDR 0x20
  161. #define ST_PRESS_LPS25H_ODR_MASK 0x70
  162. #define ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL 0x01
  163. #define ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL 0x02
  164. #define ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL 0x03
  165. #define ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL 0x04
  166. #define ST_PRESS_LPS25H_PW_ADDR 0x20
  167. #define ST_PRESS_LPS25H_PW_MASK 0x80
  168. #define ST_PRESS_LPS25H_BDU_ADDR 0x20
  169. #define ST_PRESS_LPS25H_BDU_MASK 0x04
  170. #define ST_PRESS_LPS25H_DRDY_IRQ_ADDR 0x23
  171. #define ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK 0x01
  172. #define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK 0x10
  173. #define ST_PRESS_LPS25H_IHL_IRQ_ADDR 0x22
  174. #define ST_PRESS_LPS25H_IHL_IRQ_MASK 0x80
  175. #define ST_PRESS_LPS25H_OD_IRQ_ADDR 0x22
  176. #define ST_PRESS_LPS25H_OD_IRQ_MASK 0x40
  177. #define ST_PRESS_LPS25H_MULTIREAD_BIT true
  178. #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
  179. #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b
  180. /*
  181. * CUSTOM VALUES FOR LPS22HB SENSOR
  182. * See LPS22HB datasheet:
  183. * http://www2.st.com/resource/en/datasheet/lps22hb.pdf
  184. */
  185. /* LPS22HB temperature sensitivity */
  186. #define ST_PRESS_LPS22HB_LSB_PER_CELSIUS 100UL
  187. #define ST_PRESS_LPS22HB_WAI_EXP 0xb1
  188. #define ST_PRESS_LPS22HB_ODR_ADDR 0x10
  189. #define ST_PRESS_LPS22HB_ODR_MASK 0x70
  190. #define ST_PRESS_LPS22HB_ODR_AVL_1HZ_VAL 0x01
  191. #define ST_PRESS_LPS22HB_ODR_AVL_10HZ_VAL 0x02
  192. #define ST_PRESS_LPS22HB_ODR_AVL_25HZ_VAL 0x03
  193. #define ST_PRESS_LPS22HB_ODR_AVL_50HZ_VAL 0x04
  194. #define ST_PRESS_LPS22HB_ODR_AVL_75HZ_VAL 0x05
  195. #define ST_PRESS_LPS22HB_PW_ADDR 0x10
  196. #define ST_PRESS_LPS22HB_PW_MASK 0x70
  197. #define ST_PRESS_LPS22HB_BDU_ADDR 0x10
  198. #define ST_PRESS_LPS22HB_BDU_MASK 0x02
  199. #define ST_PRESS_LPS22HB_DRDY_IRQ_ADDR 0x12
  200. #define ST_PRESS_LPS22HB_DRDY_IRQ_INT1_MASK 0x04
  201. #define ST_PRESS_LPS22HB_DRDY_IRQ_INT2_MASK 0x08
  202. #define ST_PRESS_LPS22HB_IHL_IRQ_ADDR 0x12
  203. #define ST_PRESS_LPS22HB_IHL_IRQ_MASK 0x80
  204. #define ST_PRESS_LPS22HB_OD_IRQ_ADDR 0x12
  205. #define ST_PRESS_LPS22HB_OD_IRQ_MASK 0x40
  206. #define ST_PRESS_LPS22HB_MULTIREAD_BIT true
  207. static const struct iio_chan_spec st_press_1_channels[] = {
  208. {
  209. .type = IIO_PRESSURE,
  210. .address = ST_PRESS_1_OUT_XL_ADDR,
  211. .scan_index = 0,
  212. .scan_type = {
  213. .sign = 's',
  214. .realbits = 24,
  215. .storagebits = 32,
  216. .endianness = IIO_LE,
  217. },
  218. .info_mask_separate =
  219. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  220. },
  221. {
  222. .type = IIO_TEMP,
  223. .address = ST_TEMP_1_OUT_L_ADDR,
  224. .scan_index = 1,
  225. .scan_type = {
  226. .sign = 's',
  227. .realbits = 16,
  228. .storagebits = 16,
  229. .endianness = IIO_LE,
  230. },
  231. .info_mask_separate =
  232. BIT(IIO_CHAN_INFO_RAW) |
  233. BIT(IIO_CHAN_INFO_SCALE) |
  234. BIT(IIO_CHAN_INFO_OFFSET),
  235. },
  236. IIO_CHAN_SOFT_TIMESTAMP(2)
  237. };
  238. static const struct iio_chan_spec st_press_lps001wp_channels[] = {
  239. {
  240. .type = IIO_PRESSURE,
  241. .address = ST_PRESS_LPS001WP_OUT_L_ADDR,
  242. .scan_index = 0,
  243. .scan_type = {
  244. .sign = 's',
  245. .realbits = 16,
  246. .storagebits = 16,
  247. .endianness = IIO_LE,
  248. },
  249. .info_mask_separate =
  250. BIT(IIO_CHAN_INFO_RAW) |
  251. BIT(IIO_CHAN_INFO_SCALE),
  252. },
  253. {
  254. .type = IIO_TEMP,
  255. .address = ST_TEMP_LPS001WP_OUT_L_ADDR,
  256. .scan_index = 1,
  257. .scan_type = {
  258. .sign = 's',
  259. .realbits = 16,
  260. .storagebits = 16,
  261. .endianness = IIO_LE,
  262. },
  263. .info_mask_separate =
  264. BIT(IIO_CHAN_INFO_RAW) |
  265. BIT(IIO_CHAN_INFO_SCALE),
  266. },
  267. IIO_CHAN_SOFT_TIMESTAMP(2)
  268. };
  269. static const struct iio_chan_spec st_press_lps22hb_channels[] = {
  270. {
  271. .type = IIO_PRESSURE,
  272. .address = ST_PRESS_1_OUT_XL_ADDR,
  273. .scan_index = 0,
  274. .scan_type = {
  275. .sign = 's',
  276. .realbits = 24,
  277. .storagebits = 32,
  278. .endianness = IIO_LE,
  279. },
  280. .info_mask_separate =
  281. BIT(IIO_CHAN_INFO_RAW) |
  282. BIT(IIO_CHAN_INFO_SCALE),
  283. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  284. },
  285. {
  286. .type = IIO_TEMP,
  287. .address = ST_TEMP_1_OUT_L_ADDR,
  288. .scan_index = 1,
  289. .scan_type = {
  290. .sign = 's',
  291. .realbits = 16,
  292. .storagebits = 16,
  293. .endianness = IIO_LE,
  294. },
  295. .info_mask_separate =
  296. BIT(IIO_CHAN_INFO_RAW) |
  297. BIT(IIO_CHAN_INFO_SCALE),
  298. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  299. },
  300. IIO_CHAN_SOFT_TIMESTAMP(2)
  301. };
  302. static const struct st_sensor_settings st_press_sensors_settings[] = {
  303. {
  304. .wai = ST_PRESS_LPS331AP_WAI_EXP,
  305. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  306. .sensors_supported = {
  307. [0] = LPS331AP_PRESS_DEV_NAME,
  308. },
  309. .ch = (struct iio_chan_spec *)st_press_1_channels,
  310. .num_ch = ARRAY_SIZE(st_press_1_channels),
  311. .odr = {
  312. .addr = ST_PRESS_LPS331AP_ODR_ADDR,
  313. .mask = ST_PRESS_LPS331AP_ODR_MASK,
  314. .odr_avl = {
  315. { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL, },
  316. { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL, },
  317. { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL, },
  318. { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL, },
  319. },
  320. },
  321. .pw = {
  322. .addr = ST_PRESS_LPS331AP_PW_ADDR,
  323. .mask = ST_PRESS_LPS331AP_PW_MASK,
  324. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  325. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  326. },
  327. .fs = {
  328. .addr = ST_PRESS_LPS331AP_FS_ADDR,
  329. .mask = ST_PRESS_LPS331AP_FS_MASK,
  330. .fs_avl = {
  331. /*
  332. * Pressure and temperature sensitivity values
  333. * as defined in table 3 of LPS331AP datasheet.
  334. */
  335. [0] = {
  336. .num = ST_PRESS_FS_AVL_1260MB,
  337. .gain = ST_PRESS_KPASCAL_NANO_SCALE,
  338. .gain2 = ST_PRESS_LSB_PER_CELSIUS,
  339. },
  340. },
  341. },
  342. .bdu = {
  343. .addr = ST_PRESS_LPS331AP_BDU_ADDR,
  344. .mask = ST_PRESS_LPS331AP_BDU_MASK,
  345. },
  346. .drdy_irq = {
  347. .addr = ST_PRESS_LPS331AP_DRDY_IRQ_ADDR,
  348. .mask_int1 = ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK,
  349. .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK,
  350. .addr_ihl = ST_PRESS_LPS331AP_IHL_IRQ_ADDR,
  351. .mask_ihl = ST_PRESS_LPS331AP_IHL_IRQ_MASK,
  352. .addr_od = ST_PRESS_LPS331AP_OD_IRQ_ADDR,
  353. .mask_od = ST_PRESS_LPS331AP_OD_IRQ_MASK,
  354. .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
  355. },
  356. .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT,
  357. .bootime = 2,
  358. },
  359. {
  360. .wai = ST_PRESS_LPS001WP_WAI_EXP,
  361. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  362. .sensors_supported = {
  363. [0] = LPS001WP_PRESS_DEV_NAME,
  364. },
  365. .ch = (struct iio_chan_spec *)st_press_lps001wp_channels,
  366. .num_ch = ARRAY_SIZE(st_press_lps001wp_channels),
  367. .odr = {
  368. .addr = ST_PRESS_LPS001WP_ODR_ADDR,
  369. .mask = ST_PRESS_LPS001WP_ODR_MASK,
  370. .odr_avl = {
  371. { 1, ST_PRESS_LPS001WP_ODR_AVL_1HZ_VAL, },
  372. { 7, ST_PRESS_LPS001WP_ODR_AVL_7HZ_VAL, },
  373. { 13, ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL, },
  374. },
  375. },
  376. .pw = {
  377. .addr = ST_PRESS_LPS001WP_PW_ADDR,
  378. .mask = ST_PRESS_LPS001WP_PW_MASK,
  379. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  380. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  381. },
  382. .fs = {
  383. .fs_avl = {
  384. /*
  385. * Pressure and temperature resolution values
  386. * as defined in table 3 of LPS001WP datasheet.
  387. */
  388. [0] = {
  389. .num = ST_PRESS_FS_AVL_1100MB,
  390. .gain = ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN,
  391. .gain2 = ST_PRESS_LPS001WP_LSB_PER_CELSIUS,
  392. },
  393. },
  394. },
  395. .bdu = {
  396. .addr = ST_PRESS_LPS001WP_BDU_ADDR,
  397. .mask = ST_PRESS_LPS001WP_BDU_MASK,
  398. },
  399. .drdy_irq = {
  400. .addr = 0,
  401. },
  402. .multi_read_bit = ST_PRESS_LPS001WP_MULTIREAD_BIT,
  403. .bootime = 2,
  404. },
  405. {
  406. .wai = ST_PRESS_LPS25H_WAI_EXP,
  407. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  408. .sensors_supported = {
  409. [0] = LPS25H_PRESS_DEV_NAME,
  410. },
  411. .ch = (struct iio_chan_spec *)st_press_1_channels,
  412. .num_ch = ARRAY_SIZE(st_press_1_channels),
  413. .odr = {
  414. .addr = ST_PRESS_LPS25H_ODR_ADDR,
  415. .mask = ST_PRESS_LPS25H_ODR_MASK,
  416. .odr_avl = {
  417. { 1, ST_PRESS_LPS25H_ODR_AVL_1HZ_VAL, },
  418. { 7, ST_PRESS_LPS25H_ODR_AVL_7HZ_VAL, },
  419. { 13, ST_PRESS_LPS25H_ODR_AVL_13HZ_VAL, },
  420. { 25, ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL, },
  421. },
  422. },
  423. .pw = {
  424. .addr = ST_PRESS_LPS25H_PW_ADDR,
  425. .mask = ST_PRESS_LPS25H_PW_MASK,
  426. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  427. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  428. },
  429. .fs = {
  430. .fs_avl = {
  431. /*
  432. * Pressure and temperature sensitivity values
  433. * as defined in table 3 of LPS25H datasheet.
  434. */
  435. [0] = {
  436. .num = ST_PRESS_FS_AVL_1260MB,
  437. .gain = ST_PRESS_KPASCAL_NANO_SCALE,
  438. .gain2 = ST_PRESS_LSB_PER_CELSIUS,
  439. },
  440. },
  441. },
  442. .bdu = {
  443. .addr = ST_PRESS_LPS25H_BDU_ADDR,
  444. .mask = ST_PRESS_LPS25H_BDU_MASK,
  445. },
  446. .drdy_irq = {
  447. .addr = ST_PRESS_LPS25H_DRDY_IRQ_ADDR,
  448. .mask_int1 = ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK,
  449. .mask_int2 = ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK,
  450. .addr_ihl = ST_PRESS_LPS25H_IHL_IRQ_ADDR,
  451. .mask_ihl = ST_PRESS_LPS25H_IHL_IRQ_MASK,
  452. .addr_od = ST_PRESS_LPS25H_OD_IRQ_ADDR,
  453. .mask_od = ST_PRESS_LPS25H_OD_IRQ_MASK,
  454. .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
  455. },
  456. .multi_read_bit = ST_PRESS_LPS25H_MULTIREAD_BIT,
  457. .bootime = 2,
  458. },
  459. {
  460. .wai = ST_PRESS_LPS22HB_WAI_EXP,
  461. .wai_addr = ST_SENSORS_DEFAULT_WAI_ADDRESS,
  462. .sensors_supported = {
  463. [0] = LPS22HB_PRESS_DEV_NAME,
  464. },
  465. .ch = (struct iio_chan_spec *)st_press_lps22hb_channels,
  466. .num_ch = ARRAY_SIZE(st_press_lps22hb_channels),
  467. .odr = {
  468. .addr = ST_PRESS_LPS22HB_ODR_ADDR,
  469. .mask = ST_PRESS_LPS22HB_ODR_MASK,
  470. .odr_avl = {
  471. { 1, ST_PRESS_LPS22HB_ODR_AVL_1HZ_VAL, },
  472. { 10, ST_PRESS_LPS22HB_ODR_AVL_10HZ_VAL, },
  473. { 25, ST_PRESS_LPS22HB_ODR_AVL_25HZ_VAL, },
  474. { 50, ST_PRESS_LPS22HB_ODR_AVL_50HZ_VAL, },
  475. { 75, ST_PRESS_LPS22HB_ODR_AVL_75HZ_VAL, },
  476. },
  477. },
  478. .pw = {
  479. .addr = ST_PRESS_LPS22HB_PW_ADDR,
  480. .mask = ST_PRESS_LPS22HB_PW_MASK,
  481. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  482. },
  483. .fs = {
  484. .fs_avl = {
  485. /*
  486. * Pressure and temperature sensitivity values
  487. * as defined in table 3 of LPS22HB datasheet.
  488. */
  489. [0] = {
  490. .num = ST_PRESS_FS_AVL_1260MB,
  491. .gain = ST_PRESS_KPASCAL_NANO_SCALE,
  492. .gain2 = ST_PRESS_LPS22HB_LSB_PER_CELSIUS,
  493. },
  494. },
  495. },
  496. .bdu = {
  497. .addr = ST_PRESS_LPS22HB_BDU_ADDR,
  498. .mask = ST_PRESS_LPS22HB_BDU_MASK,
  499. },
  500. .drdy_irq = {
  501. .addr = ST_PRESS_LPS22HB_DRDY_IRQ_ADDR,
  502. .mask_int1 = ST_PRESS_LPS22HB_DRDY_IRQ_INT1_MASK,
  503. .mask_int2 = ST_PRESS_LPS22HB_DRDY_IRQ_INT2_MASK,
  504. .addr_ihl = ST_PRESS_LPS22HB_IHL_IRQ_ADDR,
  505. .mask_ihl = ST_PRESS_LPS22HB_IHL_IRQ_MASK,
  506. .addr_od = ST_PRESS_LPS22HB_OD_IRQ_ADDR,
  507. .mask_od = ST_PRESS_LPS22HB_OD_IRQ_MASK,
  508. .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR,
  509. },
  510. .multi_read_bit = ST_PRESS_LPS22HB_MULTIREAD_BIT,
  511. },
  512. };
  513. static int st_press_write_raw(struct iio_dev *indio_dev,
  514. struct iio_chan_spec const *ch,
  515. int val,
  516. int val2,
  517. long mask)
  518. {
  519. int err;
  520. switch (mask) {
  521. case IIO_CHAN_INFO_SAMP_FREQ:
  522. if (val2)
  523. return -EINVAL;
  524. mutex_lock(&indio_dev->mlock);
  525. err = st_sensors_set_odr(indio_dev, val);
  526. mutex_unlock(&indio_dev->mlock);
  527. return err;
  528. default:
  529. return -EINVAL;
  530. }
  531. }
  532. static int st_press_read_raw(struct iio_dev *indio_dev,
  533. struct iio_chan_spec const *ch, int *val,
  534. int *val2, long mask)
  535. {
  536. int err;
  537. struct st_sensor_data *press_data = iio_priv(indio_dev);
  538. switch (mask) {
  539. case IIO_CHAN_INFO_RAW:
  540. err = st_sensors_read_info_raw(indio_dev, ch, val);
  541. if (err < 0)
  542. goto read_error;
  543. return IIO_VAL_INT;
  544. case IIO_CHAN_INFO_SCALE:
  545. switch (ch->type) {
  546. case IIO_PRESSURE:
  547. *val = 0;
  548. *val2 = press_data->current_fullscale->gain;
  549. return IIO_VAL_INT_PLUS_NANO;
  550. case IIO_TEMP:
  551. *val = MCELSIUS_PER_CELSIUS;
  552. *val2 = press_data->current_fullscale->gain2;
  553. return IIO_VAL_FRACTIONAL;
  554. default:
  555. err = -EINVAL;
  556. goto read_error;
  557. }
  558. case IIO_CHAN_INFO_OFFSET:
  559. switch (ch->type) {
  560. case IIO_TEMP:
  561. *val = ST_PRESS_MILLI_CELSIUS_OFFSET *
  562. press_data->current_fullscale->gain2;
  563. *val2 = MCELSIUS_PER_CELSIUS;
  564. break;
  565. default:
  566. err = -EINVAL;
  567. goto read_error;
  568. }
  569. return IIO_VAL_FRACTIONAL;
  570. case IIO_CHAN_INFO_SAMP_FREQ:
  571. *val = press_data->odr;
  572. return IIO_VAL_INT;
  573. default:
  574. return -EINVAL;
  575. }
  576. read_error:
  577. return err;
  578. }
  579. static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
  580. static struct attribute *st_press_attributes[] = {
  581. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  582. NULL,
  583. };
  584. static const struct attribute_group st_press_attribute_group = {
  585. .attrs = st_press_attributes,
  586. };
  587. static const struct iio_info press_info = {
  588. .driver_module = THIS_MODULE,
  589. .attrs = &st_press_attribute_group,
  590. .read_raw = &st_press_read_raw,
  591. .write_raw = &st_press_write_raw,
  592. .debugfs_reg_access = &st_sensors_debugfs_reg_access,
  593. };
  594. #ifdef CONFIG_IIO_TRIGGER
  595. static const struct iio_trigger_ops st_press_trigger_ops = {
  596. .owner = THIS_MODULE,
  597. .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
  598. .validate_device = st_sensors_validate_device,
  599. };
  600. #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
  601. #else
  602. #define ST_PRESS_TRIGGER_OPS NULL
  603. #endif
  604. int st_press_common_probe(struct iio_dev *indio_dev)
  605. {
  606. struct st_sensor_data *press_data = iio_priv(indio_dev);
  607. struct st_sensors_platform_data *pdata =
  608. (struct st_sensors_platform_data *)press_data->dev->platform_data;
  609. int irq = press_data->get_irq_data_ready(indio_dev);
  610. int err;
  611. indio_dev->modes = INDIO_DIRECT_MODE;
  612. indio_dev->info = &press_info;
  613. mutex_init(&press_data->tb.buf_lock);
  614. err = st_sensors_power_enable(indio_dev);
  615. if (err)
  616. return err;
  617. err = st_sensors_check_device_support(indio_dev,
  618. ARRAY_SIZE(st_press_sensors_settings),
  619. st_press_sensors_settings);
  620. if (err < 0)
  621. goto st_press_power_off;
  622. /*
  623. * Skip timestamping channel while declaring available channels to
  624. * common st_sensor layer. Look at st_sensors_get_buffer_element() to
  625. * see how timestamps are explicitly pushed as last samples block
  626. * element.
  627. */
  628. press_data->num_data_channels = press_data->sensor_settings->num_ch - 1;
  629. press_data->multiread_bit = press_data->sensor_settings->multi_read_bit;
  630. indio_dev->channels = press_data->sensor_settings->ch;
  631. indio_dev->num_channels = press_data->sensor_settings->num_ch;
  632. press_data->current_fullscale =
  633. (struct st_sensor_fullscale_avl *)
  634. &press_data->sensor_settings->fs.fs_avl[0];
  635. press_data->odr = press_data->sensor_settings->odr.odr_avl[0].hz;
  636. /* Some devices don't support a data ready pin. */
  637. if (!pdata && press_data->sensor_settings->drdy_irq.addr)
  638. pdata = (struct st_sensors_platform_data *)&default_press_pdata;
  639. err = st_sensors_init_sensor(indio_dev, pdata);
  640. if (err < 0)
  641. goto st_press_power_off;
  642. err = st_press_allocate_ring(indio_dev);
  643. if (err < 0)
  644. goto st_press_power_off;
  645. if (irq > 0) {
  646. err = st_sensors_allocate_trigger(indio_dev,
  647. ST_PRESS_TRIGGER_OPS);
  648. if (err < 0)
  649. goto st_press_probe_trigger_error;
  650. }
  651. err = iio_device_register(indio_dev);
  652. if (err)
  653. goto st_press_device_register_error;
  654. dev_info(&indio_dev->dev, "registered pressure sensor %s\n",
  655. indio_dev->name);
  656. return err;
  657. st_press_device_register_error:
  658. if (irq > 0)
  659. st_sensors_deallocate_trigger(indio_dev);
  660. st_press_probe_trigger_error:
  661. st_press_deallocate_ring(indio_dev);
  662. st_press_power_off:
  663. st_sensors_power_disable(indio_dev);
  664. return err;
  665. }
  666. EXPORT_SYMBOL(st_press_common_probe);
  667. void st_press_common_remove(struct iio_dev *indio_dev)
  668. {
  669. struct st_sensor_data *press_data = iio_priv(indio_dev);
  670. st_sensors_power_disable(indio_dev);
  671. iio_device_unregister(indio_dev);
  672. if (press_data->get_irq_data_ready(indio_dev) > 0)
  673. st_sensors_deallocate_trigger(indio_dev);
  674. st_press_deallocate_ring(indio_dev);
  675. }
  676. EXPORT_SYMBOL(st_press_common_remove);
  677. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  678. MODULE_DESCRIPTION("STMicroelectronics pressures driver");
  679. MODULE_LICENSE("GPL v2");