st_pressure_core.c 7.9 KB

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