st_pressure_core.c 7.3 KB

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