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