st_sensors.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * STMicroelectronics sensors library driver
  3. *
  4. * Copyright 2012-2013 STMicroelectronics Inc.
  5. *
  6. * Denis Ciocca <denis.ciocca@st.com>
  7. *
  8. * Licensed under the GPL-2.
  9. */
  10. #ifndef ST_SENSORS_H
  11. #define ST_SENSORS_H
  12. #include <linux/i2c.h>
  13. #include <linux/spi/spi.h>
  14. #include <linux/irqreturn.h>
  15. #include <linux/iio/trigger.h>
  16. #include <linux/bitops.h>
  17. #define ST_SENSORS_TX_MAX_LENGTH 2
  18. #define ST_SENSORS_RX_MAX_LENGTH 6
  19. #define ST_SENSORS_ODR_LIST_MAX 10
  20. #define ST_SENSORS_FULLSCALE_AVL_MAX 10
  21. #define ST_SENSORS_NUMBER_ALL_CHANNELS 4
  22. #define ST_SENSORS_NUMBER_DATA_CHANNELS 3
  23. #define ST_SENSORS_ENABLE_ALL_AXIS 0x07
  24. #define ST_SENSORS_BYTE_FOR_CHANNEL 2
  25. #define ST_SENSORS_SCAN_X 0
  26. #define ST_SENSORS_SCAN_Y 1
  27. #define ST_SENSORS_SCAN_Z 2
  28. #define ST_SENSORS_DEFAULT_POWER_ON_VALUE 0x01
  29. #define ST_SENSORS_DEFAULT_POWER_OFF_VALUE 0x00
  30. #define ST_SENSORS_DEFAULT_WAI_ADDRESS 0x0f
  31. #define ST_SENSORS_DEFAULT_AXIS_ADDR 0x20
  32. #define ST_SENSORS_DEFAULT_AXIS_MASK 0x07
  33. #define ST_SENSORS_DEFAULT_AXIS_N_BIT 3
  34. #define ST_SENSORS_MAX_NAME 17
  35. #define ST_SENSORS_MAX_4WAI 7
  36. #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
  37. ch2, s, endian, rbits, sbits, addr) \
  38. { \
  39. .type = device_type, \
  40. .modified = mod, \
  41. .info_mask_separate = mask, \
  42. .scan_index = index, \
  43. .channel2 = ch2, \
  44. .address = addr, \
  45. .scan_type = { \
  46. .sign = s, \
  47. .realbits = rbits, \
  48. .shift = sbits - rbits, \
  49. .storagebits = sbits, \
  50. .endianness = endian, \
  51. }, \
  52. }
  53. #define ST_SENSOR_DEV_ATTR_SAMP_FREQ() \
  54. IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, \
  55. st_sensors_sysfs_get_sampling_frequency, \
  56. st_sensors_sysfs_set_sampling_frequency)
  57. #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
  58. IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
  59. st_sensors_sysfs_sampling_frequency_avail)
  60. #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
  61. IIO_DEVICE_ATTR(name, S_IRUGO, \
  62. st_sensors_sysfs_scale_avail, NULL , 0);
  63. struct st_sensor_odr_avl {
  64. unsigned int hz;
  65. u8 value;
  66. };
  67. struct st_sensor_odr {
  68. u8 addr;
  69. u8 mask;
  70. struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
  71. };
  72. struct st_sensor_power {
  73. u8 addr;
  74. u8 mask;
  75. u8 value_off;
  76. u8 value_on;
  77. };
  78. struct st_sensor_axis {
  79. u8 addr;
  80. u8 mask;
  81. };
  82. struct st_sensor_fullscale_avl {
  83. unsigned int num;
  84. u8 value;
  85. unsigned int gain;
  86. unsigned int gain2;
  87. };
  88. struct st_sensor_fullscale {
  89. u8 addr;
  90. u8 mask;
  91. struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
  92. };
  93. /**
  94. * struct st_sensor_bdu - ST sensor device block data update
  95. * @addr: address of the register.
  96. * @mask: mask to write the block data update flag.
  97. */
  98. struct st_sensor_bdu {
  99. u8 addr;
  100. u8 mask;
  101. };
  102. /**
  103. * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
  104. * @addr: address of the register.
  105. * @mask: mask to write the on/off value.
  106. * struct ig1 - represents the Interrupt Generator 1 of sensors.
  107. * @en_addr: address of the enable ig1 register.
  108. * @en_mask: mask to write the on/off value for enable.
  109. */
  110. struct st_sensor_data_ready_irq {
  111. u8 addr;
  112. u8 mask;
  113. struct {
  114. u8 en_addr;
  115. u8 en_mask;
  116. } ig1;
  117. };
  118. /**
  119. * struct st_sensor_transfer_buffer - ST sensor device I/O buffer
  120. * @buf_lock: Mutex to protect rx and tx buffers.
  121. * @tx_buf: Buffer used by SPI transfer function to send data to the sensors.
  122. * This buffer is used to avoid DMA not-aligned issue.
  123. * @rx_buf: Buffer used by SPI transfer to receive data from sensors.
  124. * This buffer is used to avoid DMA not-aligned issue.
  125. */
  126. struct st_sensor_transfer_buffer {
  127. struct mutex buf_lock;
  128. u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH];
  129. u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned;
  130. };
  131. /**
  132. * struct st_sensor_transfer_function - ST sensor device I/O function
  133. * @read_byte: Function used to read one byte.
  134. * @write_byte: Function used to write one byte.
  135. * @read_multiple_byte: Function used to read multiple byte.
  136. */
  137. struct st_sensor_transfer_function {
  138. int (*read_byte) (struct st_sensor_transfer_buffer *tb,
  139. struct device *dev, u8 reg_addr, u8 *res_byte);
  140. int (*write_byte) (struct st_sensor_transfer_buffer *tb,
  141. struct device *dev, u8 reg_addr, u8 data);
  142. int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb,
  143. struct device *dev, u8 reg_addr, int len, u8 *data,
  144. bool multiread_bit);
  145. };
  146. /**
  147. * struct st_sensors - ST sensors list
  148. * @wai: Contents of WhoAmI register.
  149. * @sensors_supported: List of supported sensors by struct itself.
  150. * @ch: IIO channels for the sensor.
  151. * @odr: Output data rate register and ODR list available.
  152. * @pw: Power register of the sensor.
  153. * @enable_axis: Enable one or more axis of the sensor.
  154. * @fs: Full scale register and full scale list available.
  155. * @bdu: Block data update register.
  156. * @drdy_irq: Data ready register of the sensor.
  157. * @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
  158. * @bootime: samples to discard when sensor passing from power-down to power-up.
  159. */
  160. struct st_sensors {
  161. u8 wai;
  162. char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
  163. struct iio_chan_spec *ch;
  164. struct st_sensor_odr odr;
  165. struct st_sensor_power pw;
  166. struct st_sensor_axis enable_axis;
  167. struct st_sensor_fullscale fs;
  168. struct st_sensor_bdu bdu;
  169. struct st_sensor_data_ready_irq drdy_irq;
  170. bool multi_read_bit;
  171. unsigned int bootime;
  172. };
  173. /**
  174. * struct st_sensor_data - ST sensor device status
  175. * @dev: Pointer to instance of struct device (I2C or SPI).
  176. * @trig: The trigger in use by the core driver.
  177. * @sensor: Pointer to the current sensor struct in use.
  178. * @current_fullscale: Maximum range of measure by the sensor.
  179. * @enabled: Status of the sensor (false->off, true->on).
  180. * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread.
  181. * @buffer_data: Data used by buffer part.
  182. * @odr: Output data rate of the sensor [Hz].
  183. * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
  184. * @tf: Transfer function structure used by I/O operations.
  185. * @tb: Transfer buffers and mutex used by I/O operations.
  186. */
  187. struct st_sensor_data {
  188. struct device *dev;
  189. struct iio_trigger *trig;
  190. struct st_sensors *sensor;
  191. struct st_sensor_fullscale_avl *current_fullscale;
  192. bool enabled;
  193. bool multiread_bit;
  194. char *buffer_data;
  195. unsigned int odr;
  196. unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);
  197. const struct st_sensor_transfer_function *tf;
  198. struct st_sensor_transfer_buffer tb;
  199. };
  200. #ifdef CONFIG_IIO_BUFFER
  201. irqreturn_t st_sensors_trigger_handler(int irq, void *p);
  202. int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf);
  203. #endif
  204. #ifdef CONFIG_IIO_TRIGGER
  205. int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
  206. const struct iio_trigger_ops *trigger_ops);
  207. void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
  208. #else
  209. static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
  210. const struct iio_trigger_ops *trigger_ops)
  211. {
  212. return 0;
  213. }
  214. static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
  215. {
  216. return;
  217. }
  218. #endif
  219. int st_sensors_init_sensor(struct iio_dev *indio_dev);
  220. int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
  221. int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
  222. int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
  223. int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
  224. int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
  225. int st_sensors_read_info_raw(struct iio_dev *indio_dev,
  226. struct iio_chan_spec const *ch, int *val);
  227. int st_sensors_check_device_support(struct iio_dev *indio_dev,
  228. int num_sensors_list, const struct st_sensors *sensors);
  229. ssize_t st_sensors_sysfs_get_sampling_frequency(struct device *dev,
  230. struct device_attribute *attr, char *buf);
  231. ssize_t st_sensors_sysfs_set_sampling_frequency(struct device *dev,
  232. struct device_attribute *attr, const char *buf, size_t size);
  233. ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
  234. struct device_attribute *attr, char *buf);
  235. ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
  236. struct device_attribute *attr, char *buf);
  237. #endif /* ST_SENSORS_H */