st_pressure_core.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. .num_ch = ARRAY_SIZE(st_press_lps331ap_channels),
  104. .odr = {
  105. .addr = ST_PRESS_LPS331AP_ODR_ADDR,
  106. .mask = ST_PRESS_LPS331AP_ODR_MASK,
  107. .odr_avl = {
  108. { 1, ST_PRESS_LPS331AP_ODR_AVL_1HZ_VAL, },
  109. { 7, ST_PRESS_LPS331AP_ODR_AVL_7HZ_VAL, },
  110. { 13, ST_PRESS_LPS331AP_ODR_AVL_13HZ_VAL, },
  111. { 25, ST_PRESS_LPS331AP_ODR_AVL_25HZ_VAL, },
  112. },
  113. },
  114. .pw = {
  115. .addr = ST_PRESS_LPS331AP_PW_ADDR,
  116. .mask = ST_PRESS_LPS331AP_PW_MASK,
  117. .value_on = ST_SENSORS_DEFAULT_POWER_ON_VALUE,
  118. .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
  119. },
  120. .fs = {
  121. .addr = ST_PRESS_LPS331AP_FS_ADDR,
  122. .mask = ST_PRESS_LPS331AP_FS_MASK,
  123. .fs_avl = {
  124. [0] = {
  125. .num = ST_PRESS_FS_AVL_1260MB,
  126. .value = ST_PRESS_LPS331AP_FS_AVL_1260_VAL,
  127. .gain = ST_PRESS_LPS331AP_FS_AVL_1260_GAIN,
  128. .gain2 = ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN,
  129. },
  130. },
  131. },
  132. .bdu = {
  133. .addr = ST_PRESS_LPS331AP_BDU_ADDR,
  134. .mask = ST_PRESS_LPS331AP_BDU_MASK,
  135. },
  136. .drdy_irq = {
  137. .addr = ST_PRESS_LPS331AP_DRDY_IRQ_ADDR,
  138. .mask_int1 = ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK,
  139. .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK,
  140. },
  141. .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT,
  142. .bootime = 2,
  143. },
  144. };
  145. static int st_press_read_raw(struct iio_dev *indio_dev,
  146. struct iio_chan_spec const *ch, int *val,
  147. int *val2, long mask)
  148. {
  149. int err;
  150. struct st_sensor_data *pdata = iio_priv(indio_dev);
  151. switch (mask) {
  152. case IIO_CHAN_INFO_RAW:
  153. err = st_sensors_read_info_raw(indio_dev, ch, val);
  154. if (err < 0)
  155. goto read_error;
  156. return IIO_VAL_INT;
  157. case IIO_CHAN_INFO_SCALE:
  158. *val = 0;
  159. switch (ch->type) {
  160. case IIO_PRESSURE:
  161. *val2 = pdata->current_fullscale->gain;
  162. break;
  163. case IIO_TEMP:
  164. *val2 = pdata->current_fullscale->gain2;
  165. break;
  166. default:
  167. err = -EINVAL;
  168. goto read_error;
  169. }
  170. return IIO_VAL_INT_PLUS_NANO;
  171. case IIO_CHAN_INFO_OFFSET:
  172. switch (ch->type) {
  173. case IIO_TEMP:
  174. *val = 425;
  175. *val2 = 10;
  176. break;
  177. default:
  178. err = -EINVAL;
  179. goto read_error;
  180. }
  181. return IIO_VAL_FRACTIONAL;
  182. default:
  183. return -EINVAL;
  184. }
  185. read_error:
  186. return err;
  187. }
  188. static ST_SENSOR_DEV_ATTR_SAMP_FREQ();
  189. static ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL();
  190. static struct attribute *st_press_attributes[] = {
  191. &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
  192. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  193. NULL,
  194. };
  195. static const struct attribute_group st_press_attribute_group = {
  196. .attrs = st_press_attributes,
  197. };
  198. static const struct iio_info press_info = {
  199. .driver_module = THIS_MODULE,
  200. .attrs = &st_press_attribute_group,
  201. .read_raw = &st_press_read_raw,
  202. };
  203. #ifdef CONFIG_IIO_TRIGGER
  204. static const struct iio_trigger_ops st_press_trigger_ops = {
  205. .owner = THIS_MODULE,
  206. .set_trigger_state = ST_PRESS_TRIGGER_SET_STATE,
  207. };
  208. #define ST_PRESS_TRIGGER_OPS (&st_press_trigger_ops)
  209. #else
  210. #define ST_PRESS_TRIGGER_OPS NULL
  211. #endif
  212. int st_press_common_probe(struct iio_dev *indio_dev,
  213. struct st_sensors_platform_data *plat_data)
  214. {
  215. int err;
  216. struct st_sensor_data *pdata = iio_priv(indio_dev);
  217. indio_dev->modes = INDIO_DIRECT_MODE;
  218. indio_dev->info = &press_info;
  219. err = st_sensors_check_device_support(indio_dev,
  220. ARRAY_SIZE(st_press_sensors), st_press_sensors);
  221. if (err < 0)
  222. goto st_press_common_probe_error;
  223. pdata->num_data_channels = ST_PRESS_NUMBER_DATA_CHANNELS;
  224. pdata->multiread_bit = pdata->sensor->multi_read_bit;
  225. indio_dev->channels = pdata->sensor->ch;
  226. indio_dev->num_channels = pdata->sensor->num_ch;
  227. if (pdata->sensor->fs.addr != 0)
  228. pdata->current_fullscale = (struct st_sensor_fullscale_avl *)
  229. &pdata->sensor->fs.fs_avl[0];
  230. pdata->odr = pdata->sensor->odr.odr_avl[0].hz;
  231. if (!plat_data)
  232. plat_data =
  233. (struct st_sensors_platform_data *)&default_press_pdata;
  234. err = st_sensors_init_sensor(indio_dev, plat_data);
  235. if (err < 0)
  236. goto st_press_common_probe_error;
  237. if (pdata->get_irq_data_ready(indio_dev) > 0) {
  238. err = st_press_allocate_ring(indio_dev);
  239. if (err < 0)
  240. goto st_press_common_probe_error;
  241. err = st_sensors_allocate_trigger(indio_dev,
  242. ST_PRESS_TRIGGER_OPS);
  243. if (err < 0)
  244. goto st_press_probe_trigger_error;
  245. }
  246. err = iio_device_register(indio_dev);
  247. if (err)
  248. goto st_press_device_register_error;
  249. return err;
  250. st_press_device_register_error:
  251. if (pdata->get_irq_data_ready(indio_dev) > 0)
  252. st_sensors_deallocate_trigger(indio_dev);
  253. st_press_probe_trigger_error:
  254. if (pdata->get_irq_data_ready(indio_dev) > 0)
  255. st_press_deallocate_ring(indio_dev);
  256. st_press_common_probe_error:
  257. return err;
  258. }
  259. EXPORT_SYMBOL(st_press_common_probe);
  260. void st_press_common_remove(struct iio_dev *indio_dev)
  261. {
  262. struct st_sensor_data *pdata = iio_priv(indio_dev);
  263. iio_device_unregister(indio_dev);
  264. if (pdata->get_irq_data_ready(indio_dev) > 0) {
  265. st_sensors_deallocate_trigger(indio_dev);
  266. st_press_deallocate_ring(indio_dev);
  267. }
  268. }
  269. EXPORT_SYMBOL(st_press_common_remove);
  270. MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
  271. MODULE_DESCRIPTION("STMicroelectronics pressures driver");
  272. MODULE_LICENSE("GPL v2");