st_pressure_core.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #define ST_PRESS_MBAR_TO_KPASCAL(x) (x * 10)
  29. #define ST_PRESS_NUMBER_DATA_CHANNELS 1
  30. /* DEFAULT VALUE FOR SENSORS */
  31. #define ST_PRESS_DEFAULT_OUT_XL_ADDR 0x28
  32. #define ST_TEMP_DEFAULT_OUT_L_ADDR 0x2b
  33. /* FULLSCALE */
  34. #define ST_PRESS_FS_AVL_1260MB 1260
  35. /* CUSTOM VALUES FOR SENSOR 1 */
  36. #define ST_PRESS_1_WAI_EXP 0xbb
  37. #define ST_PRESS_1_ODR_ADDR 0x20
  38. #define ST_PRESS_1_ODR_MASK 0x70
  39. #define ST_PRESS_1_ODR_AVL_1HZ_VAL 0x01
  40. #define ST_PRESS_1_ODR_AVL_7HZ_VAL 0x05
  41. #define ST_PRESS_1_ODR_AVL_13HZ_VAL 0x06
  42. #define ST_PRESS_1_ODR_AVL_25HZ_VAL 0x07
  43. #define ST_PRESS_1_PW_ADDR 0x20
  44. #define ST_PRESS_1_PW_MASK 0x80
  45. #define ST_PRESS_1_FS_ADDR 0x23
  46. #define ST_PRESS_1_FS_MASK 0x30
  47. #define ST_PRESS_1_FS_AVL_1260_VAL 0x00
  48. #define ST_PRESS_1_FS_AVL_1260_GAIN ST_PRESS_MBAR_TO_KPASCAL(244141)
  49. #define ST_PRESS_1_FS_AVL_TEMP_GAIN 2083000
  50. #define ST_PRESS_1_BDU_ADDR 0x20
  51. #define ST_PRESS_1_BDU_MASK 0x04
  52. #define ST_PRESS_1_DRDY_IRQ_ADDR 0x22
  53. #define ST_PRESS_1_DRDY_IRQ_MASK 0x04
  54. #define ST_PRESS_1_MULTIREAD_BIT true
  55. #define ST_PRESS_1_TEMP_OFFSET 42500
  56. static const struct iio_chan_spec st_press_channels[] = {
  57. ST_SENSORS_LSM_CHANNELS(IIO_PRESSURE,
  58. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  59. ST_SENSORS_SCAN_X, 0, IIO_NO_MOD, 'u', IIO_LE, 24, 24,
  60. ST_PRESS_DEFAULT_OUT_XL_ADDR),
  61. ST_SENSORS_LSM_CHANNELS(IIO_TEMP,
  62. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
  63. BIT(IIO_CHAN_INFO_OFFSET),
  64. -1, 0, IIO_NO_MOD, 's', IIO_LE, 16, 16,
  65. ST_TEMP_DEFAULT_OUT_L_ADDR),
  66. IIO_CHAN_SOFT_TIMESTAMP(1)
  67. };
  68. static const struct st_sensors st_press_sensors[] = {
  69. {
  70. .wai = ST_PRESS_1_WAI_EXP,
  71. .sensors_supported = {
  72. [0] = LPS331AP_PRESS_DEV_NAME,
  73. },
  74. .ch = (struct iio_chan_spec *)st_press_channels,
  75. .odr = {
  76. .addr = ST_PRESS_1_ODR_ADDR,
  77. .mask = ST_PRESS_1_ODR_MASK,
  78. .odr_avl = {
  79. { 1, ST_PRESS_1_ODR_AVL_1HZ_VAL, },
  80. { 7, ST_PRESS_1_ODR_AVL_7HZ_VAL, },
  81. { 13, ST_PRESS_1_ODR_AVL_13HZ_VAL, },
  82. { 25, ST_PRESS_1_ODR_AVL_25HZ_VAL, },
  83. },
  84. },
  85. .pw = {
  86. .addr = ST_PRESS_1_PW_ADDR,
  87. .mask = ST_PRESS_1_PW_MASK,
  88. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  89. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  90. },
  91. .fs = {
  92. .addr = ST_PRESS_1_FS_ADDR,
  93. .mask = ST_PRESS_1_FS_MASK,
  94. .fs_avl = {
  95. [0] = {
  96. .num = ST_PRESS_FS_AVL_1260MB,
  97. .value = ST_PRESS_1_FS_AVL_1260_VAL,
  98. .gain = ST_PRESS_1_FS_AVL_1260_GAIN,
  99. .gain2 = ST_PRESS_1_FS_AVL_TEMP_GAIN,
  100. },
  101. },
  102. },
  103. .bdu = {
  104. .addr = ST_PRESS_1_BDU_ADDR,
  105. .mask = ST_PRESS_1_BDU_MASK,
  106. },
  107. .drdy_irq = {
  108. .addr = ST_PRESS_1_DRDY_IRQ_ADDR,
  109. .mask = ST_PRESS_1_DRDY_IRQ_MASK,
  110. },
  111. .multi_read_bit = ST_PRESS_1_MULTIREAD_BIT,
  112. .bootime = 2,
  113. },
  114. };
  115. static int st_press_read_raw(struct iio_dev *indio_dev,
  116. struct iio_chan_spec const *ch, int *val,
  117. int *val2, long mask)
  118. {
  119. int err;
  120. struct st_sensor_data *pdata = iio_priv(indio_dev);
  121. switch (mask) {
  122. case IIO_CHAN_INFO_RAW:
  123. err = st_sensors_read_info_raw(indio_dev, ch, val);
  124. if (err < 0)
  125. goto read_error;
  126. return IIO_VAL_INT;
  127. case IIO_CHAN_INFO_SCALE:
  128. *val = 0;
  129. switch (ch->type) {
  130. case IIO_PRESSURE:
  131. *val2 = pdata->current_fullscale->gain;
  132. break;
  133. case IIO_TEMP:
  134. *val2 = pdata->current_fullscale->gain2;
  135. break;
  136. default:
  137. err = -EINVAL;
  138. goto read_error;
  139. }
  140. return IIO_VAL_INT_PLUS_NANO;
  141. case IIO_CHAN_INFO_OFFSET:
  142. switch (ch->type) {
  143. case IIO_TEMP:
  144. *val = 425;
  145. *val2 = 10;
  146. break;
  147. default:
  148. err = -EINVAL;
  149. goto read_error;
  150. }
  151. return IIO_VAL_FRACTIONAL;
  152. default:
  153. return -EINVAL;
  154. }
  155. read_error:
  156. return err;
  157. }
  158. static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
  159. static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
  160. static struct attribute *st_press_attributes[] = {
  161. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  162. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  163. NULL,
  164. };
  165. static const struct attribute_group st_press_attribute_group = {
  166. .attrs = st_press_attributes,
  167. };
  168. static const struct iio_info press_info = {
  169. .driver_module = THIS_MODULE,
  170. .attrs = &st_press_attribute_group,
  171. .read_raw = &st_press_read_raw,
  172. };
  173. #ifdef CONFIG_IIO_TRIGGER
  174. static const struct iio_trigger_ops st_press_trigger_ops = {
  175. .owner = THIS_MODULE,
  176. .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
  177. };
  178. #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
  179. #else
  180. #define ST_PRESS_TRIGGER_OPS NULL
  181. #endif
  182. int st_press_common_probe(struct iio_dev *indio_dev)
  183. {
  184. int err;
  185. struct st_sensor_data *pdata = iio_priv(indio_dev);
  186. indio_dev->modes = INDIO_DIRECT_MODE;
  187. indio_dev->info = &press_info;
  188. err = st_sensors_check_device_support(indio_dev,
  189. ARRAY_SIZE(st_press_sensors), st_press_sensors);
  190. if (err < 0)
  191. goto st_press_common_probe_error;
  192. pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS;
  193. pdata->multiread_bit = pdata->sensor->multi_read_bit;
  194. indio_dev->channels = pdata->sensor->ch;
  195. indio_dev->num_channels = ARRAY_SIZE(st_press_channels);
  196. pdata->current_fullscale = (struct st_sensor_fullscale_avl *)
  197. &pdata->sensor->fs.fs_avl[0];
  198. pdata->odr = pdata->sensor->odr.odr_avl[0].hz;
  199. err = st_sensors_init_sensor(indio_dev);
  200. if (err < 0)
  201. goto st_press_common_probe_error;
  202. if (pdata->get_irq_data_ready(indio_dev) > 0) {
  203. err = st_press_allocate_ring(indio_dev);
  204. if (err < 0)
  205. goto st_press_common_probe_error;
  206. err = st_sensors_allocate_trigger(indio_dev,
  207. ST_PRESS_TRIGGER_OPS);
  208. if (err < 0)
  209. goto st_press_probe_trigger_error;
  210. }
  211. err = iio_device_register(indio_dev);
  212. if (err)
  213. goto st_press_device_register_error;
  214. return err;
  215. st_press_device_register_error:
  216. if (pdata->get_irq_data_ready(indio_dev) > 0)
  217. st_sensors_deallocate_trigger(indio_dev);
  218. st_press_probe_trigger_error:
  219. if (pdata->get_irq_data_ready(indio_dev) > 0)
  220. st_press_deallocate_ring(indio_dev);
  221. st_press_common_probe_error:
  222. return err;
  223. }
  224. EXPORT_SYMBOL(st_press_common_probe);
  225. void st_press_common_remove(struct iio_dev *indio_dev)
  226. {
  227. struct st_sensor_data *pdata = iio_priv(indio_dev);
  228. iio_device_unregister(indio_dev);
  229. if (pdata->get_irq_data_ready(indio_dev) > 0) {
  230. st_sensors_deallocate_trigger(indio_dev);
  231. st_press_deallocate_ring(indio_dev);
  232. }
  233. iio_device_free(indio_dev);
  234. }
  235. EXPORT_SYMBOL(st_press_common_remove);
  236. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  237. MODULE_DESCRIPTION("STMicroelectronics pressures driver");
  238. MODULE_LICENSE("GPL v2");