st_sensors.h 8.0 KB

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