st_sensors.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #include <linux/platform_data/st_sensors_pdata.h>
  18. #define ST_SENSORS_TX_MAX_LENGTH 2
  19. #define ST_SENSORS_RX_MAX_LENGTH 6
  20. #define ST_SENSORS_ODR_LIST_MAX 10
  21. #define ST_SENSORS_FULLSCALE_AVL_MAX 10
  22. #define ST_SENSORS_NUMBER_ALL_CHANNELS 4
  23. #define ST_SENSORS_ENABLE_ALL_AXIS 0x07
  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_POWER_ON_VALUE 0x01
  28. #define ST_SENSORS_DEFAULT_POWER_OFF_VALUE 0x00
  29. #define ST_SENSORS_DEFAULT_WAI_ADDRESS 0x0f
  30. #define ST_SENSORS_DEFAULT_AXIS_ADDR 0x20
  31. #define ST_SENSORS_DEFAULT_AXIS_MASK 0x07
  32. #define ST_SENSORS_DEFAULT_AXIS_N_BIT 3
  33. #define ST_SENSORS_MAX_NAME 17
  34. #define ST_SENSORS_MAX_4WAI 7
  35. #define ST_SENSORS_LSM_CHANNELS(device_type, mask, index, mod, \
  36. ch2, s, endian, rbits, sbits, addr) \
  37. { \
  38. .type = device_type, \
  39. .modified = mod, \
  40. .info_mask_separate = mask, \
  41. .scan_index = index, \
  42. .channel2 = ch2, \
  43. .address = addr, \
  44. .scan_type = { \
  45. .sign = s, \
  46. .realbits = rbits, \
  47. .shift = sbits - rbits, \
  48. .storagebits = sbits, \
  49. .endianness = endian, \
  50. }, \
  51. }
  52. #define ST_SENSOR_DEV_ATTR_SAMP_FREQ() \
  53. IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, \
  54. st_sensors_sysfs_get_sampling_frequency, \
  55. st_sensors_sysfs_set_sampling_frequency)
  56. #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
  57. IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
  58. st_sensors_sysfs_sampling_frequency_avail)
  59. #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
  60. IIO_DEVICE_ATTR(name, S_IRUGO, \
  61. st_sensors_sysfs_scale_avail, NULL , 0);
  62. struct st_sensor_odr_avl {
  63. unsigned int hz;
  64. u8 value;
  65. };
  66. struct st_sensor_odr {
  67. u8 addr;
  68. u8 mask;
  69. struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
  70. };
  71. struct st_sensor_power {
  72. u8 addr;
  73. u8 mask;
  74. u8 value_off;
  75. u8 value_on;
  76. };
  77. struct st_sensor_axis {
  78. u8 addr;
  79. u8 mask;
  80. };
  81. struct st_sensor_fullscale_avl {
  82. unsigned int num;
  83. u8 value;
  84. unsigned int gain;
  85. unsigned int gain2;
  86. };
  87. struct st_sensor_fullscale {
  88. u8 addr;
  89. u8 mask;
  90. struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
  91. };
  92. /**
  93. * struct st_sensor_bdu - ST sensor device block data update
  94. * @addr: address of the register.
  95. * @mask: mask to write the block data update flag.
  96. */
  97. struct st_sensor_bdu {
  98. u8 addr;
  99. u8 mask;
  100. };
  101. /**
  102. * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
  103. * @addr: address of the register.
  104. * @mask_int1: mask to enable/disable IRQ on INT1 pin.
  105. * @mask_int2: mask to enable/disable IRQ on INT2 pin.
  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_int1;
  113. u8 mask_int2;
  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. * num_data_channels: Number of data channels used in buffer.
  185. * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
  186. * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
  187. * @tf: Transfer function structure used by I/O operations.
  188. * @tb: Transfer buffers and mutex used by I/O operations.
  189. */
  190. struct st_sensor_data {
  191. struct device *dev;
  192. struct iio_trigger *trig;
  193. struct st_sensors *sensor;
  194. struct st_sensor_fullscale_avl *current_fullscale;
  195. bool enabled;
  196. bool multiread_bit;
  197. char *buffer_data;
  198. unsigned int odr;
  199. unsigned int num_data_channels;
  200. u8 drdy_int_pin;
  201. unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);
  202. const struct st_sensor_transfer_function *tf;
  203. struct st_sensor_transfer_buffer tb;
  204. };
  205. #ifdef CONFIG_IIO_BUFFER
  206. irqreturn_t st_sensors_trigger_handler(int irq, void *p);
  207. int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf);
  208. #endif
  209. #ifdef CONFIG_IIO_TRIGGER
  210. int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
  211. const struct iio_trigger_ops *trigger_ops);
  212. void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
  213. #else
  214. static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
  215. const struct iio_trigger_ops *trigger_ops)
  216. {
  217. return 0;
  218. }
  219. static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
  220. {
  221. return;
  222. }
  223. #endif
  224. int st_sensors_init_sensor(struct iio_dev *indio_dev,
  225. struct st_sensors_platform_data *pdata);
  226. int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
  227. int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
  228. int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
  229. int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
  230. int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
  231. int st_sensors_read_info_raw(struct iio_dev *indio_dev,
  232. struct iio_chan_spec const *ch, int *val);
  233. int st_sensors_check_device_support(struct iio_dev *indio_dev,
  234. int num_sensors_list, const struct st_sensors *sensors);
  235. ssize_t st_sensors_sysfs_get_sampling_frequency(struct device *dev,
  236. struct device_attribute *attr, char *buf);
  237. ssize_t st_sensors_sysfs_set_sampling_frequency(struct device *dev,
  238. struct device_attribute *attr, const char *buf, size_t size);
  239. ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
  240. struct device_attribute *attr, char *buf);
  241. ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
  242. struct device_attribute *attr, char *buf);
  243. #endif /* ST_SENSORS_H */