ssp.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
  4. */
  5. #ifndef __SSP_SENSORHUB_H__
  6. #define __SSP_SENSORHUB_H__
  7. #include <linux/delay.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/iio/common/ssp_sensors.h>
  10. #include <linux/iio/iio.h>
  11. #include <linux/spi/spi.h>
  12. #define SSP_DEVICE_ID 0x55
  13. #ifdef SSP_DBG
  14. #define ssp_dbg(format, ...) pr_info("[SSP] "format, ##__VA_ARGS__)
  15. #else
  16. #define ssp_dbg(format, ...)
  17. #endif
  18. #define SSP_SW_RESET_TIME 3000
  19. /* Sensor polling in ms */
  20. #define SSP_DEFAULT_POLLING_DELAY 200
  21. #define SSP_DEFAULT_RETRIES 3
  22. #define SSP_DATA_PACKET_SIZE 960
  23. #define SSP_HEADER_BUFFER_SIZE 4
  24. enum {
  25. SSP_KERNEL_BINARY = 0,
  26. SSP_KERNEL_CRASHED_BINARY,
  27. };
  28. enum {
  29. SSP_INITIALIZATION_STATE = 0,
  30. SSP_NO_SENSOR_STATE,
  31. SSP_ADD_SENSOR_STATE,
  32. SSP_RUNNING_SENSOR_STATE,
  33. };
  34. /* Firmware download STATE */
  35. enum {
  36. SSP_FW_DL_STATE_FAIL = -1,
  37. SSP_FW_DL_STATE_NONE = 0,
  38. SSP_FW_DL_STATE_NEED_TO_SCHEDULE,
  39. SSP_FW_DL_STATE_SCHEDULED,
  40. SSP_FW_DL_STATE_DOWNLOADING,
  41. SSP_FW_DL_STATE_SYNC,
  42. SSP_FW_DL_STATE_DONE,
  43. };
  44. #define SSP_INVALID_REVISION 99999
  45. #define SSP_INVALID_REVISION2 0xffffff
  46. /* AP -> SSP Instruction */
  47. #define SSP_MSG2SSP_INST_BYPASS_SENSOR_ADD 0xa1
  48. #define SSP_MSG2SSP_INST_BYPASS_SENSOR_RM 0xa2
  49. #define SSP_MSG2SSP_INST_REMOVE_ALL 0xa3
  50. #define SSP_MSG2SSP_INST_CHANGE_DELAY 0xa4
  51. #define SSP_MSG2SSP_INST_LIBRARY_ADD 0xb1
  52. #define SSP_MSG2SSP_INST_LIBRARY_REMOVE 0xb2
  53. #define SSP_MSG2SSP_INST_LIB_NOTI 0xb4
  54. #define SSP_MSG2SSP_INST_LIB_DATA 0xc1
  55. #define SSP_MSG2SSP_AP_MCU_SET_GYRO_CAL 0xcd
  56. #define SSP_MSG2SSP_AP_MCU_SET_ACCEL_CAL 0xce
  57. #define SSP_MSG2SSP_AP_STATUS_SHUTDOWN 0xd0
  58. #define SSP_MSG2SSP_AP_STATUS_WAKEUP 0xd1
  59. #define SSP_MSG2SSP_AP_STATUS_SLEEP 0xd2
  60. #define SSP_MSG2SSP_AP_STATUS_RESUME 0xd3
  61. #define SSP_MSG2SSP_AP_STATUS_SUSPEND 0xd4
  62. #define SSP_MSG2SSP_AP_STATUS_RESET 0xd5
  63. #define SSP_MSG2SSP_AP_STATUS_POW_CONNECTED 0xd6
  64. #define SSP_MSG2SSP_AP_STATUS_POW_DISCONNECTED 0xd7
  65. #define SSP_MSG2SSP_AP_TEMPHUMIDITY_CAL_DONE 0xda
  66. #define SSP_MSG2SSP_AP_MCU_SET_DUMPMODE 0xdb
  67. #define SSP_MSG2SSP_AP_MCU_DUMP_CHECK 0xdc
  68. #define SSP_MSG2SSP_AP_MCU_BATCH_FLUSH 0xdd
  69. #define SSP_MSG2SSP_AP_MCU_BATCH_COUNT 0xdf
  70. #define SSP_MSG2SSP_AP_WHOAMI 0x0f
  71. #define SSP_MSG2SSP_AP_FIRMWARE_REV 0xf0
  72. #define SSP_MSG2SSP_AP_SENSOR_FORMATION 0xf1
  73. #define SSP_MSG2SSP_AP_SENSOR_PROXTHRESHOLD 0xf2
  74. #define SSP_MSG2SSP_AP_SENSOR_BARCODE_EMUL 0xf3
  75. #define SSP_MSG2SSP_AP_SENSOR_SCANNING 0xf4
  76. #define SSP_MSG2SSP_AP_SET_MAGNETIC_HWOFFSET 0xf5
  77. #define SSP_MSG2SSP_AP_GET_MAGNETIC_HWOFFSET 0xf6
  78. #define SSP_MSG2SSP_AP_SENSOR_GESTURE_CURRENT 0xf7
  79. #define SSP_MSG2SSP_AP_GET_THERM 0xf8
  80. #define SSP_MSG2SSP_AP_GET_BIG_DATA 0xf9
  81. #define SSP_MSG2SSP_AP_SET_BIG_DATA 0xfa
  82. #define SSP_MSG2SSP_AP_START_BIG_DATA 0xfb
  83. #define SSP_MSG2SSP_AP_SET_MAGNETIC_STATIC_MATRIX 0xfd
  84. #define SSP_MSG2SSP_AP_SENSOR_TILT 0xea
  85. #define SSP_MSG2SSP_AP_MCU_SET_TIME 0xfe
  86. #define SSP_MSG2SSP_AP_MCU_GET_TIME 0xff
  87. #define SSP_MSG2SSP_AP_FUSEROM 0x01
  88. /* voice data */
  89. #define SSP_TYPE_WAKE_UP_VOICE_SERVICE 0x01
  90. #define SSP_TYPE_WAKE_UP_VOICE_SOUND_SOURCE_AM 0x01
  91. #define SSP_TYPE_WAKE_UP_VOICE_SOUND_SOURCE_GRAMMER 0x02
  92. /* Factory Test */
  93. #define SSP_ACCELEROMETER_FACTORY 0x80
  94. #define SSP_GYROSCOPE_FACTORY 0x81
  95. #define SSP_GEOMAGNETIC_FACTORY 0x82
  96. #define SSP_PRESSURE_FACTORY 0x85
  97. #define SSP_GESTURE_FACTORY 0x86
  98. #define SSP_TEMPHUMIDITY_CRC_FACTORY 0x88
  99. #define SSP_GYROSCOPE_TEMP_FACTORY 0x8a
  100. #define SSP_GYROSCOPE_DPS_FACTORY 0x8b
  101. #define SSP_MCU_FACTORY 0x8c
  102. #define SSP_MCU_SLEEP_FACTORY 0x8d
  103. /* SSP -> AP ACK about write CMD */
  104. #define SSP_MSG_ACK 0x80 /* ACK from SSP to AP */
  105. #define SSP_MSG_NAK 0x70 /* NAK from SSP to AP */
  106. struct ssp_sensorhub_info {
  107. char *fw_name;
  108. char *fw_crashed_name;
  109. unsigned int fw_rev;
  110. const u8 * const mag_table;
  111. const unsigned int mag_length;
  112. };
  113. /* ssp_msg options bit */
  114. #define SSP_RW 0
  115. #define SSP_INDEX 3
  116. #define SSP_AP2HUB_READ 0
  117. #define SSP_AP2HUB_WRITE 1
  118. #define SSP_HUB2AP_WRITE 2
  119. #define SSP_AP2HUB_READY 3
  120. #define SSP_AP2HUB_RETURN 4
  121. /**
  122. * struct ssp_data - ssp platformdata structure
  123. * @spi: spi device
  124. * @sensorhub_info: info about sensorhub board specific features
  125. * @wdt_timer: watchdog timer
  126. * @work_wdt: watchdog work
  127. * @work_firmware: firmware upgrade work queue
  128. * @work_refresh: refresh work queue for reset request from MCU
  129. * @shut_down: shut down flag
  130. * @mcu_dump_mode: mcu dump mode for debug
  131. * @time_syncing: time syncing indication flag
  132. * @timestamp: previous time in ns calculated for time syncing
  133. * @check_status: status table for each sensor
  134. * @com_fail_cnt: communication fail count
  135. * @reset_cnt: reset count
  136. * @timeout_cnt: timeout count
  137. * @available_sensors: available sensors seen by sensorhub (bit array)
  138. * @cur_firm_rev: cached current firmware revision
  139. * @last_resume_state: last AP resume/suspend state used to handle the PM
  140. * state of ssp
  141. * @last_ap_state: (obsolete) sleep notification for MCU
  142. * @sensor_enable: sensor enable mask
  143. * @delay_buf: data acquisition intervals table
  144. * @batch_latency_buf: yet unknown but existing in communication protocol
  145. * @batch_opt_buf: yet unknown but existing in communication protocol
  146. * @accel_position: yet unknown but existing in communication protocol
  147. * @mag_position: yet unknown but existing in communication protocol
  148. * @fw_dl_state: firmware download state
  149. * @comm_lock: lock protecting the handshake
  150. * @pending_lock: lock protecting pending list and completion
  151. * @mcu_reset_gpiod: mcu reset line
  152. * @ap_mcu_gpiod: ap to mcu gpio line
  153. * @mcu_ap_gpiod: mcu to ap gpio line
  154. * @pending_list: pending list for messages queued to be sent/read
  155. * @sensor_devs: registered IIO devices table
  156. * @enable_refcount: enable reference count for wdt (watchdog timer)
  157. * @header_buffer: cache aligned buffer for packet header
  158. */
  159. struct ssp_data {
  160. struct spi_device *spi;
  161. const struct ssp_sensorhub_info *sensorhub_info;
  162. struct timer_list wdt_timer;
  163. struct work_struct work_wdt;
  164. struct delayed_work work_refresh;
  165. bool shut_down;
  166. bool mcu_dump_mode;
  167. bool time_syncing;
  168. int64_t timestamp;
  169. int check_status[SSP_SENSOR_MAX];
  170. unsigned int com_fail_cnt;
  171. unsigned int reset_cnt;
  172. unsigned int timeout_cnt;
  173. unsigned int available_sensors;
  174. unsigned int cur_firm_rev;
  175. char last_resume_state;
  176. char last_ap_state;
  177. unsigned int sensor_enable;
  178. u32 delay_buf[SSP_SENSOR_MAX];
  179. s32 batch_latency_buf[SSP_SENSOR_MAX];
  180. s8 batch_opt_buf[SSP_SENSOR_MAX];
  181. int accel_position;
  182. int mag_position;
  183. int fw_dl_state;
  184. struct mutex comm_lock;
  185. struct mutex pending_lock;
  186. struct gpio_desc *mcu_reset_gpiod;
  187. struct gpio_desc *ap_mcu_gpiod;
  188. struct gpio_desc *mcu_ap_gpiod;
  189. struct list_head pending_list;
  190. struct iio_dev *sensor_devs[SSP_SENSOR_MAX];
  191. atomic_t enable_refcount;
  192. __le16 header_buffer[SSP_HEADER_BUFFER_SIZE / sizeof(__le16)] __aligned(IIO_DMA_MINALIGN);
  193. };
  194. void ssp_clean_pending_list(struct ssp_data *data);
  195. int ssp_command(struct ssp_data *data, char command, int arg);
  196. int ssp_send_instruction(struct ssp_data *data, u8 inst, u8 sensor_type,
  197. u8 *send_buf, u8 length);
  198. int ssp_irq_msg(struct ssp_data *data);
  199. int ssp_get_chipid(struct ssp_data *data);
  200. int ssp_set_magnetic_matrix(struct ssp_data *data);
  201. unsigned int ssp_get_sensor_scanning_info(struct ssp_data *data);
  202. unsigned int ssp_get_firmware_rev(struct ssp_data *data);
  203. int ssp_queue_ssp_refresh_task(struct ssp_data *data, unsigned int delay);
  204. #endif /* __SSP_SENSORHUB_H__ */