st_sensors.h 8.0 KB

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