st_pressure_core.c 6.9 KB

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