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