st_pressure_core.c 7.2 KB

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