st_sensors.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/regulator/consumer.h>
  18. #include <linux/platform_data/st_sensors_pdata.h>
  19. #define ST_SENSORS_TX_MAX_LENGTH 2
  20. #define ST_SENSORS_RX_MAX_LENGTH 6
  21. #define ST_SENSORS_ODR_LIST_MAX 10
  22. #define ST_SENSORS_FULLSCALE_AVL_MAX 10
  23. #define ST_SENSORS_NUMBER_ALL_CHANNELS 4
  24. #define ST_SENSORS_ENABLE_ALL_AXIS 0x07
  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. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  43. .scan_index = index, \
  44. .channel2 = ch2, \
  45. .address = addr, \
  46. .scan_type = { \
  47. .sign = s, \
  48. .realbits = rbits, \
  49. .shift = sbits - rbits, \
  50. .storagebits = sbits, \
  51. .endianness = endian, \
  52. }, \
  53. }
  54. #define ST_SENSORS_DEV_ATTR_SAMP_FREQ_AVAIL() \
  55. IIO_DEV_ATTR_SAMP_FREQ_AVAIL( \
  56. st_sensors_sysfs_sampling_frequency_avail)
  57. #define ST_SENSORS_DEV_ATTR_SCALE_AVAIL(name) \
  58. IIO_DEVICE_ATTR(name, S_IRUGO, \
  59. st_sensors_sysfs_scale_avail, NULL , 0);
  60. struct st_sensor_odr_avl {
  61. unsigned int hz;
  62. u8 value;
  63. };
  64. struct st_sensor_odr {
  65. u8 addr;
  66. u8 mask;
  67. struct st_sensor_odr_avl odr_avl[ST_SENSORS_ODR_LIST_MAX];
  68. };
  69. struct st_sensor_power {
  70. u8 addr;
  71. u8 mask;
  72. u8 value_off;
  73. u8 value_on;
  74. };
  75. struct st_sensor_axis {
  76. u8 addr;
  77. u8 mask;
  78. };
  79. struct st_sensor_fullscale_avl {
  80. unsigned int num;
  81. u8 value;
  82. unsigned int gain;
  83. unsigned int gain2;
  84. };
  85. struct st_sensor_fullscale {
  86. u8 addr;
  87. u8 mask;
  88. struct st_sensor_fullscale_avl fs_avl[ST_SENSORS_FULLSCALE_AVL_MAX];
  89. };
  90. /**
  91. * struct st_sensor_bdu - ST sensor device block data update
  92. * @addr: address of the register.
  93. * @mask: mask to write the block data update flag.
  94. */
  95. struct st_sensor_bdu {
  96. u8 addr;
  97. u8 mask;
  98. };
  99. /**
  100. * struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
  101. * @addr: address of the register.
  102. * @mask_int1: mask to enable/disable IRQ on INT1 pin.
  103. * @mask_int2: mask to enable/disable IRQ on INT2 pin.
  104. * struct ig1 - represents the Interrupt Generator 1 of sensors.
  105. * @en_addr: address of the enable ig1 register.
  106. * @en_mask: mask to write the on/off value for enable.
  107. */
  108. struct st_sensor_data_ready_irq {
  109. u8 addr;
  110. u8 mask_int1;
  111. u8 mask_int2;
  112. struct {
  113. u8 en_addr;
  114. u8 en_mask;
  115. } ig1;
  116. };
  117. /**
  118. * struct st_sensor_transfer_buffer - ST sensor device I/O buffer
  119. * @buf_lock: Mutex to protect rx and tx buffers.
  120. * @tx_buf: Buffer used by SPI transfer function to send data to the sensors.
  121. * This buffer is used to avoid DMA not-aligned issue.
  122. * @rx_buf: Buffer used by SPI transfer to receive data from sensors.
  123. * This buffer is used to avoid DMA not-aligned issue.
  124. */
  125. struct st_sensor_transfer_buffer {
  126. struct mutex buf_lock;
  127. u8 rx_buf[ST_SENSORS_RX_MAX_LENGTH];
  128. u8 tx_buf[ST_SENSORS_TX_MAX_LENGTH] ____cacheline_aligned;
  129. };
  130. /**
  131. * struct st_sensor_transfer_function - ST sensor device I/O function
  132. * @read_byte: Function used to read one byte.
  133. * @write_byte: Function used to write one byte.
  134. * @read_multiple_byte: Function used to read multiple byte.
  135. */
  136. struct st_sensor_transfer_function {
  137. int (*read_byte) (struct st_sensor_transfer_buffer *tb,
  138. struct device *dev, u8 reg_addr, u8 *res_byte);
  139. int (*write_byte) (struct st_sensor_transfer_buffer *tb,
  140. struct device *dev, u8 reg_addr, u8 data);
  141. int (*read_multiple_byte) (struct st_sensor_transfer_buffer *tb,
  142. struct device *dev, u8 reg_addr, int len, u8 *data,
  143. bool multiread_bit);
  144. };
  145. /**
  146. * struct st_sensor_settings - ST specific sensor settings
  147. * @wai: Contents of WhoAmI register.
  148. * @wai_addr: The address 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_sensor_settings {
  161. u8 wai;
  162. u8 wai_addr;
  163. char sensors_supported[ST_SENSORS_MAX_4WAI][ST_SENSORS_MAX_NAME];
  164. struct iio_chan_spec *ch;
  165. int num_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_settings: Pointer to the specific sensor settings in use.
  180. * @current_fullscale: Maximum range of measure by the sensor.
  181. * @vdd: Pointer to sensor's Vdd power supply
  182. * @vdd_io: Pointer to sensor's Vdd-IO power supply
  183. * @enabled: Status of the sensor (false->off, true->on).
  184. * @multiread_bit: Use or not particular bit for [I2C/SPI] multiread.
  185. * @buffer_data: Data used by buffer part.
  186. * @odr: Output data rate of the sensor [Hz].
  187. * num_data_channels: Number of data channels used in buffer.
  188. * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2).
  189. * @get_irq_data_ready: Function to get the IRQ used for data ready signal.
  190. * @tf: Transfer function structure used by I/O operations.
  191. * @tb: Transfer buffers and mutex used by I/O operations.
  192. */
  193. struct st_sensor_data {
  194. struct device *dev;
  195. struct iio_trigger *trig;
  196. struct st_sensor_settings *sensor_settings;
  197. struct st_sensor_fullscale_avl *current_fullscale;
  198. struct regulator *vdd;
  199. struct regulator *vdd_io;
  200. bool enabled;
  201. bool multiread_bit;
  202. char *buffer_data;
  203. unsigned int odr;
  204. unsigned int num_data_channels;
  205. u8 drdy_int_pin;
  206. unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev);
  207. const struct st_sensor_transfer_function *tf;
  208. struct st_sensor_transfer_buffer tb;
  209. };
  210. #ifdef CONFIG_IIO_BUFFER
  211. irqreturn_t st_sensors_trigger_handler(int irq, void *p);
  212. int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf);
  213. #endif
  214. #ifdef CONFIG_IIO_TRIGGER
  215. int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
  216. const struct iio_trigger_ops *trigger_ops);
  217. void st_sensors_deallocate_trigger(struct iio_dev *indio_dev);
  218. #else
  219. static inline int st_sensors_allocate_trigger(struct iio_dev *indio_dev,
  220. const struct iio_trigger_ops *trigger_ops)
  221. {
  222. return 0;
  223. }
  224. static inline void st_sensors_deallocate_trigger(struct iio_dev *indio_dev)
  225. {
  226. return;
  227. }
  228. #endif
  229. int st_sensors_init_sensor(struct iio_dev *indio_dev,
  230. struct st_sensors_platform_data *pdata);
  231. int st_sensors_set_enable(struct iio_dev *indio_dev, bool enable);
  232. int st_sensors_set_axis_enable(struct iio_dev *indio_dev, u8 axis_enable);
  233. void st_sensors_power_enable(struct iio_dev *indio_dev);
  234. void st_sensors_power_disable(struct iio_dev *indio_dev);
  235. int st_sensors_debugfs_reg_access(struct iio_dev *indio_dev,
  236. unsigned reg, unsigned writeval,
  237. unsigned *readval);
  238. int st_sensors_set_odr(struct iio_dev *indio_dev, unsigned int odr);
  239. int st_sensors_set_dataready_irq(struct iio_dev *indio_dev, bool enable);
  240. int st_sensors_set_fullscale_by_gain(struct iio_dev *indio_dev, int scale);
  241. int st_sensors_read_info_raw(struct iio_dev *indio_dev,
  242. struct iio_chan_spec const *ch, int *val);
  243. int st_sensors_check_device_support(struct iio_dev *indio_dev,
  244. int num_sensors_list, const struct st_sensor_settings *sensor_settings);
  245. ssize_t st_sensors_sysfs_sampling_frequency_avail(struct device *dev,
  246. struct device_attribute *attr, char *buf);
  247. ssize_t st_sensors_sysfs_scale_avail(struct device *dev,
  248. struct device_attribute *attr, char *buf);
  249. #endif /* ST_SENSORS_H */