ssp.h 7.8 KB

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