Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  *  Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
0004  */
0005 
0006 #ifndef __SSP_SENSORHUB_H__
0007 #define __SSP_SENSORHUB_H__
0008 
0009 #include <linux/delay.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/iio/common/ssp_sensors.h>
0012 #include <linux/iio/iio.h>
0013 #include <linux/spi/spi.h>
0014 
0015 #define SSP_DEVICE_ID       0x55
0016 
0017 #ifdef SSP_DBG
0018 #define ssp_dbg(format, ...) pr_info("[SSP] "format, ##__VA_ARGS__)
0019 #else
0020 #define ssp_dbg(format, ...)
0021 #endif
0022 
0023 #define SSP_SW_RESET_TIME       3000
0024 /* Sensor polling in ms */
0025 #define SSP_DEFAULT_POLLING_DELAY   200
0026 #define SSP_DEFAULT_RETRIES     3
0027 #define SSP_DATA_PACKET_SIZE        960
0028 #define SSP_HEADER_BUFFER_SIZE      4
0029 
0030 enum {
0031     SSP_KERNEL_BINARY = 0,
0032     SSP_KERNEL_CRASHED_BINARY,
0033 };
0034 
0035 enum {
0036     SSP_INITIALIZATION_STATE = 0,
0037     SSP_NO_SENSOR_STATE,
0038     SSP_ADD_SENSOR_STATE,
0039     SSP_RUNNING_SENSOR_STATE,
0040 };
0041 
0042 /* Firmware download STATE */
0043 enum {
0044     SSP_FW_DL_STATE_FAIL = -1,
0045     SSP_FW_DL_STATE_NONE = 0,
0046     SSP_FW_DL_STATE_NEED_TO_SCHEDULE,
0047     SSP_FW_DL_STATE_SCHEDULED,
0048     SSP_FW_DL_STATE_DOWNLOADING,
0049     SSP_FW_DL_STATE_SYNC,
0050     SSP_FW_DL_STATE_DONE,
0051 };
0052 
0053 #define SSP_INVALID_REVISION            99999
0054 #define SSP_INVALID_REVISION2           0xffffff
0055 
0056 /* AP -> SSP Instruction */
0057 #define SSP_MSG2SSP_INST_BYPASS_SENSOR_ADD  0xa1
0058 #define SSP_MSG2SSP_INST_BYPASS_SENSOR_RM   0xa2
0059 #define SSP_MSG2SSP_INST_REMOVE_ALL     0xa3
0060 #define SSP_MSG2SSP_INST_CHANGE_DELAY       0xa4
0061 #define SSP_MSG2SSP_INST_LIBRARY_ADD        0xb1
0062 #define SSP_MSG2SSP_INST_LIBRARY_REMOVE     0xb2
0063 #define SSP_MSG2SSP_INST_LIB_NOTI       0xb4
0064 #define SSP_MSG2SSP_INST_LIB_DATA       0xc1
0065 
0066 #define SSP_MSG2SSP_AP_MCU_SET_GYRO_CAL     0xcd
0067 #define SSP_MSG2SSP_AP_MCU_SET_ACCEL_CAL    0xce
0068 #define SSP_MSG2SSP_AP_STATUS_SHUTDOWN      0xd0
0069 #define SSP_MSG2SSP_AP_STATUS_WAKEUP        0xd1
0070 #define SSP_MSG2SSP_AP_STATUS_SLEEP     0xd2
0071 #define SSP_MSG2SSP_AP_STATUS_RESUME        0xd3
0072 #define SSP_MSG2SSP_AP_STATUS_SUSPEND       0xd4
0073 #define SSP_MSG2SSP_AP_STATUS_RESET     0xd5
0074 #define SSP_MSG2SSP_AP_STATUS_POW_CONNECTED 0xd6
0075 #define SSP_MSG2SSP_AP_STATUS_POW_DISCONNECTED  0xd7
0076 #define SSP_MSG2SSP_AP_TEMPHUMIDITY_CAL_DONE    0xda
0077 #define SSP_MSG2SSP_AP_MCU_SET_DUMPMODE     0xdb
0078 #define SSP_MSG2SSP_AP_MCU_DUMP_CHECK       0xdc
0079 #define SSP_MSG2SSP_AP_MCU_BATCH_FLUSH      0xdd
0080 #define SSP_MSG2SSP_AP_MCU_BATCH_COUNT      0xdf
0081 
0082 #define SSP_MSG2SSP_AP_WHOAMI               0x0f
0083 #define SSP_MSG2SSP_AP_FIRMWARE_REV         0xf0
0084 #define SSP_MSG2SSP_AP_SENSOR_FORMATION         0xf1
0085 #define SSP_MSG2SSP_AP_SENSOR_PROXTHRESHOLD     0xf2
0086 #define SSP_MSG2SSP_AP_SENSOR_BARCODE_EMUL      0xf3
0087 #define SSP_MSG2SSP_AP_SENSOR_SCANNING          0xf4
0088 #define SSP_MSG2SSP_AP_SET_MAGNETIC_HWOFFSET        0xf5
0089 #define SSP_MSG2SSP_AP_GET_MAGNETIC_HWOFFSET        0xf6
0090 #define SSP_MSG2SSP_AP_SENSOR_GESTURE_CURRENT       0xf7
0091 #define SSP_MSG2SSP_AP_GET_THERM            0xf8
0092 #define SSP_MSG2SSP_AP_GET_BIG_DATA         0xf9
0093 #define SSP_MSG2SSP_AP_SET_BIG_DATA         0xfa
0094 #define SSP_MSG2SSP_AP_START_BIG_DATA           0xfb
0095 #define SSP_MSG2SSP_AP_SET_MAGNETIC_STATIC_MATRIX   0xfd
0096 #define SSP_MSG2SSP_AP_SENSOR_TILT          0xea
0097 #define SSP_MSG2SSP_AP_MCU_SET_TIME         0xfe
0098 #define SSP_MSG2SSP_AP_MCU_GET_TIME         0xff
0099 
0100 #define SSP_MSG2SSP_AP_FUSEROM              0x01
0101 
0102 /* voice data */
0103 #define SSP_TYPE_WAKE_UP_VOICE_SERVICE          0x01
0104 #define SSP_TYPE_WAKE_UP_VOICE_SOUND_SOURCE_AM      0x01
0105 #define SSP_TYPE_WAKE_UP_VOICE_SOUND_SOURCE_GRAMMER 0x02
0106 
0107 /* Factory Test */
0108 #define SSP_ACCELEROMETER_FACTORY           0x80
0109 #define SSP_GYROSCOPE_FACTORY               0x81
0110 #define SSP_GEOMAGNETIC_FACTORY             0x82
0111 #define SSP_PRESSURE_FACTORY                0x85
0112 #define SSP_GESTURE_FACTORY             0x86
0113 #define SSP_TEMPHUMIDITY_CRC_FACTORY            0x88
0114 #define SSP_GYROSCOPE_TEMP_FACTORY          0x8a
0115 #define SSP_GYROSCOPE_DPS_FACTORY           0x8b
0116 #define SSP_MCU_FACTORY                 0x8c
0117 #define SSP_MCU_SLEEP_FACTORY               0x8d
0118 
0119 /* SSP -> AP ACK about write CMD */
0120 #define SSP_MSG_ACK     0x80    /* ACK from SSP to AP */
0121 #define SSP_MSG_NAK     0x70    /* NAK from SSP to AP */
0122 
0123 struct ssp_sensorhub_info {
0124     char *fw_name;
0125     char *fw_crashed_name;
0126     unsigned int fw_rev;
0127     const u8 * const mag_table;
0128     const unsigned int mag_length;
0129 };
0130 
0131 /* ssp_msg options bit */
0132 #define SSP_RW      0
0133 #define SSP_INDEX   3
0134 
0135 #define SSP_AP2HUB_READ     0
0136 #define SSP_AP2HUB_WRITE    1
0137 #define SSP_HUB2AP_WRITE    2
0138 #define SSP_AP2HUB_READY    3
0139 #define SSP_AP2HUB_RETURN   4
0140 
0141 /**
0142  * struct ssp_data - ssp platformdata structure
0143  * @spi:        spi device
0144  * @sensorhub_info: info about sensorhub board specific features
0145  * @wdt_timer:      watchdog timer
0146  * @work_wdt:       watchdog work
0147  * @work_firmware:  firmware upgrade work queue
0148  * @work_refresh:   refresh work queue for reset request from MCU
0149  * @shut_down:      shut down flag
0150  * @mcu_dump_mode:  mcu dump mode for debug
0151  * @time_syncing:   time syncing indication flag
0152  * @timestamp:      previous time in ns calculated for time syncing
0153  * @check_status:   status table for each sensor
0154  * @com_fail_cnt:   communication fail count
0155  * @reset_cnt:      reset count
0156  * @timeout_cnt:    timeout count
0157  * @available_sensors:  available sensors seen by sensorhub (bit array)
0158  * @cur_firm_rev:   cached current firmware revision
0159  * @last_resume_state:  last AP resume/suspend state used to handle the PM
0160  *                      state of ssp
0161  * @last_ap_state:  (obsolete) sleep notification for MCU
0162  * @sensor_enable:  sensor enable mask
0163  * @delay_buf:      data acquisition intervals table
0164  * @batch_latency_buf:  yet unknown but existing in communication protocol
0165  * @batch_opt_buf:  yet unknown but existing in communication protocol
0166  * @accel_position: yet unknown but existing in communication protocol
0167  * @mag_position:   yet unknown but existing in communication protocol
0168  * @fw_dl_state:    firmware download state
0169  * @comm_lock:      lock protecting the handshake
0170  * @pending_lock:   lock protecting pending list and completion
0171  * @mcu_reset_gpiod:    mcu reset line
0172  * @ap_mcu_gpiod:   ap to mcu gpio line
0173  * @mcu_ap_gpiod:   mcu to ap gpio line
0174  * @pending_list:   pending list for messages queued to be sent/read
0175  * @sensor_devs:    registered IIO devices table
0176  * @enable_refcount:    enable reference count for wdt (watchdog timer)
0177  * @header_buffer:  cache aligned buffer for packet header
0178  */
0179 struct ssp_data {
0180     struct spi_device *spi;
0181     const struct ssp_sensorhub_info *sensorhub_info;
0182     struct timer_list wdt_timer;
0183     struct work_struct work_wdt;
0184     struct delayed_work work_refresh;
0185 
0186     bool shut_down;
0187     bool mcu_dump_mode;
0188     bool time_syncing;
0189     int64_t timestamp;
0190 
0191     int check_status[SSP_SENSOR_MAX];
0192 
0193     unsigned int com_fail_cnt;
0194     unsigned int reset_cnt;
0195     unsigned int timeout_cnt;
0196 
0197     unsigned int available_sensors;
0198     unsigned int cur_firm_rev;
0199 
0200     char last_resume_state;
0201     char last_ap_state;
0202 
0203     unsigned int sensor_enable;
0204     u32 delay_buf[SSP_SENSOR_MAX];
0205     s32 batch_latency_buf[SSP_SENSOR_MAX];
0206     s8 batch_opt_buf[SSP_SENSOR_MAX];
0207 
0208     int accel_position;
0209     int mag_position;
0210     int fw_dl_state;
0211 
0212     struct mutex comm_lock;
0213     struct mutex pending_lock;
0214 
0215     struct gpio_desc *mcu_reset_gpiod;
0216     struct gpio_desc *ap_mcu_gpiod;
0217     struct gpio_desc *mcu_ap_gpiod;
0218 
0219     struct list_head pending_list;
0220 
0221     struct iio_dev *sensor_devs[SSP_SENSOR_MAX];
0222     atomic_t enable_refcount;
0223 
0224     __le16 header_buffer[SSP_HEADER_BUFFER_SIZE / sizeof(__le16)] __aligned(IIO_DMA_MINALIGN);
0225 };
0226 
0227 void ssp_clean_pending_list(struct ssp_data *data);
0228 
0229 int ssp_command(struct ssp_data *data, char command, int arg);
0230 
0231 int ssp_send_instruction(struct ssp_data *data, u8 inst, u8 sensor_type,
0232              u8 *send_buf, u8 length);
0233 
0234 int ssp_irq_msg(struct ssp_data *data);
0235 
0236 int ssp_get_chipid(struct ssp_data *data);
0237 
0238 int ssp_set_magnetic_matrix(struct ssp_data *data);
0239 
0240 unsigned int ssp_get_sensor_scanning_info(struct ssp_data *data);
0241 
0242 unsigned int ssp_get_firmware_rev(struct ssp_data *data);
0243 
0244 int ssp_queue_ssp_refresh_task(struct ssp_data *data, unsigned int delay);
0245 
0246 #endif /* __SSP_SENSORHUB_H__ */