1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
8 #define SSP_DEV (&data->spi->dev)
9 #define SSP_GET_MESSAGE_TYPE(data) (data & (3 << SSP_RW))
12 * SSP -> AP Instruction
13 * They tell what packet type can be expected. In the future there will
14 * be less of them. BYPASS means common sensor packets with accel, gyro,
15 * hrm etc. data. LIBRARY and META are mock-up's for now.
17 #define SSP_MSG2AP_INST_BYPASS_DATA 0x37
18 #define SSP_MSG2AP_INST_LIBRARY_DATA 0x01
19 #define SSP_MSG2AP_INST_DEBUG_DATA 0x03
20 #define SSP_MSG2AP_INST_BIG_DATA 0x04
21 #define SSP_MSG2AP_INST_META_DATA 0x05
22 #define SSP_MSG2AP_INST_TIME_SYNC 0x06
23 #define SSP_MSG2AP_INST_RESET 0x07
25 #define SSP_UNIMPLEMENTED -1
27 struct ssp_msg_header
{
32 } __attribute__((__packed__
));
37 struct list_head list
;
38 struct completion
*done
;
39 struct ssp_msg_header
*h
;
43 static const int ssp_offset_map
[SSP_SENSOR_MAX
] = {
44 [SSP_ACCELEROMETER_SENSOR
] = SSP_ACCELEROMETER_SIZE
+
46 [SSP_GYROSCOPE_SENSOR
] = SSP_GYROSCOPE_SIZE
+
48 [SSP_GEOMAGNETIC_UNCALIB_SENSOR
] = SSP_UNIMPLEMENTED
,
49 [SSP_GEOMAGNETIC_RAW
] = SSP_UNIMPLEMENTED
,
50 [SSP_GEOMAGNETIC_SENSOR
] = SSP_UNIMPLEMENTED
,
51 [SSP_PRESSURE_SENSOR
] = SSP_UNIMPLEMENTED
,
52 [SSP_GESTURE_SENSOR
] = SSP_UNIMPLEMENTED
,
53 [SSP_PROXIMITY_SENSOR
] = SSP_UNIMPLEMENTED
,
54 [SSP_TEMPERATURE_HUMIDITY_SENSOR
] = SSP_UNIMPLEMENTED
,
55 [SSP_LIGHT_SENSOR
] = SSP_UNIMPLEMENTED
,
56 [SSP_PROXIMITY_RAW
] = SSP_UNIMPLEMENTED
,
57 [SSP_ORIENTATION_SENSOR
] = SSP_UNIMPLEMENTED
,
58 [SSP_STEP_DETECTOR
] = SSP_UNIMPLEMENTED
,
59 [SSP_SIG_MOTION_SENSOR
] = SSP_UNIMPLEMENTED
,
60 [SSP_GYRO_UNCALIB_SENSOR
] = SSP_UNIMPLEMENTED
,
61 [SSP_GAME_ROTATION_VECTOR
] = SSP_UNIMPLEMENTED
,
62 [SSP_ROTATION_VECTOR
] = SSP_UNIMPLEMENTED
,
63 [SSP_STEP_COUNTER
] = SSP_UNIMPLEMENTED
,
64 [SSP_BIO_HRM_RAW
] = SSP_BIO_HRM_RAW_SIZE
+
66 [SSP_BIO_HRM_RAW_FAC
] = SSP_BIO_HRM_RAW_FAC_SIZE
+
68 [SSP_BIO_HRM_LIB
] = SSP_BIO_HRM_LIB_SIZE
+
72 #define SSP_HEADER_SIZE (sizeof(struct ssp_msg_header))
73 #define SSP_HEADER_SIZE_ALIGNED (ALIGN(SSP_HEADER_SIZE, 4))
75 static struct ssp_msg
*ssp_create_msg(u8 cmd
, u16 len
, u16 opt
, u32 data
)
77 struct ssp_msg_header h
;
80 msg
= kzalloc(sizeof(*msg
), GFP_KERNEL
);
85 h
.length
= cpu_to_le16(len
);
86 h
.options
= cpu_to_le16(opt
);
87 h
.data
= cpu_to_le32(data
);
89 msg
->buffer
= kzalloc(SSP_HEADER_SIZE_ALIGNED
+ len
,
90 GFP_KERNEL
| GFP_DMA
);
99 memcpy(msg
->buffer
, &h
, SSP_HEADER_SIZE
);
105 * It is a bit heavy to do it this way but often the function is used to compose
106 * the message from smaller chunks which are placed on the stack. Often the
107 * chunks are small so memcpy should be optimalized.
109 static inline void ssp_fill_buffer(struct ssp_msg
*m
, unsigned int offset
,
110 const void *src
, unsigned int len
)
112 memcpy(&m
->buffer
[SSP_HEADER_SIZE_ALIGNED
+ offset
], src
, len
);
115 static inline void ssp_get_buffer(struct ssp_msg
*m
, unsigned int offset
,
116 void *dest
, unsigned int len
)
118 memcpy(dest
, &m
->buffer
[SSP_HEADER_SIZE_ALIGNED
+ offset
], len
);
121 #define SSP_GET_BUFFER_AT_INDEX(m, index) \
122 (m->buffer[SSP_HEADER_SIZE_ALIGNED + index])
123 #define SSP_SET_BUFFER_AT_INDEX(m, index, val) \
124 (m->buffer[SSP_HEADER_SIZE_ALIGNED + index] = val)
126 static void ssp_clean_msg(struct ssp_msg
*m
)
132 static int ssp_print_mcu_debug(char *data_frame
, int *data_index
,
135 int length
= data_frame
[(*data_index
)++];
137 if (length
> received_len
- *data_index
|| length
<= 0) {
138 ssp_dbg("[SSP]: MSG From MCU-invalid debug length(%d/%d)\n",
139 length
, received_len
);
140 return length
? length
: -EPROTO
;
143 ssp_dbg("[SSP]: MSG From MCU - %s\n", &data_frame
[*data_index
]);
145 *data_index
+= length
;
151 * It was designed that way - additional lines to some kind of handshake,
152 * please do not ask why - only the firmware guy can know it.
154 static int ssp_check_lines(struct ssp_data
*data
, bool state
)
158 gpiod_set_value_cansleep(data
->ap_mcu_gpiod
, state
);
160 while (gpiod_get_value_cansleep(data
->mcu_ap_gpiod
) != state
) {
161 usleep_range(3000, 3500);
163 if (data
->shut_down
|| delay_cnt
++ > 500) {
164 dev_err(SSP_DEV
, "%s:timeout, hw ack wait fail %d\n",
168 gpiod_set_value_cansleep(data
->ap_mcu_gpiod
, 1);
177 static int ssp_do_transfer(struct ssp_data
*data
, struct ssp_msg
*msg
,
178 struct completion
*done
, int timeout
)
182 * check if this is a short one way message or the whole transfer has
183 * second part after an interrupt
185 const bool use_no_irq
= msg
->length
== 0;
192 mutex_lock(&data
->comm_lock
);
194 status
= ssp_check_lines(data
, false);
198 status
= spi_write(data
->spi
, msg
->buffer
, SSP_HEADER_SIZE
);
200 gpiod_set_value_cansleep(data
->ap_mcu_gpiod
, 1);
201 dev_err(SSP_DEV
, "%s spi_write fail\n", __func__
);
206 mutex_lock(&data
->pending_lock
);
207 list_add_tail(&msg
->list
, &data
->pending_list
);
208 mutex_unlock(&data
->pending_lock
);
211 status
= ssp_check_lines(data
, true);
214 mutex_lock(&data
->pending_lock
);
215 list_del(&msg
->list
);
216 mutex_unlock(&data
->pending_lock
);
221 mutex_unlock(&data
->comm_lock
);
223 if (!use_no_irq
&& done
)
224 if (wait_for_completion_timeout(done
,
225 msecs_to_jiffies(timeout
)) ==
227 mutex_lock(&data
->pending_lock
);
228 list_del(&msg
->list
);
229 mutex_unlock(&data
->pending_lock
);
238 mutex_unlock(&data
->comm_lock
);
243 static inline int ssp_spi_sync_command(struct ssp_data
*data
,
246 return ssp_do_transfer(data
, msg
, NULL
, 0);
249 static int ssp_spi_sync(struct ssp_data
*data
, struct ssp_msg
*msg
,
252 DECLARE_COMPLETION_ONSTACK(done
);
254 if (WARN_ON(!msg
->length
))
257 return ssp_do_transfer(data
, msg
, &done
, timeout
);
260 static int ssp_handle_big_data(struct ssp_data
*data
, char *dataframe
, int *idx
)
262 /* mock-up, it will be changed with adding another sensor types */
267 static int ssp_parse_dataframe(struct ssp_data
*data
, char *dataframe
, int len
)
270 struct ssp_sensor_data
*spd
;
271 struct iio_dev
**indio_devs
= data
->sensor_devs
;
273 for (idx
= 0; idx
< len
;) {
274 switch (dataframe
[idx
++]) {
275 case SSP_MSG2AP_INST_BYPASS_DATA
:
276 sd
= dataframe
[idx
++];
277 if (sd
< 0 || sd
>= SSP_SENSOR_MAX
) {
279 "Mcu data frame1 error %d\n", sd
);
283 if (indio_devs
[sd
]) {
284 spd
= iio_priv(indio_devs
[sd
]);
285 if (spd
->process_data
)
286 spd
->process_data(indio_devs
[sd
],
290 dev_err(SSP_DEV
, "no client for frame\n");
293 idx
+= ssp_offset_map
[sd
];
295 case SSP_MSG2AP_INST_DEBUG_DATA
:
296 sd
= ssp_print_mcu_debug(dataframe
, &idx
, len
);
299 "Mcu data frame3 error %d\n", sd
);
303 case SSP_MSG2AP_INST_LIBRARY_DATA
:
306 case SSP_MSG2AP_INST_BIG_DATA
:
307 ssp_handle_big_data(data
, dataframe
, &idx
);
309 case SSP_MSG2AP_INST_TIME_SYNC
:
310 data
->time_syncing
= true;
312 case SSP_MSG2AP_INST_RESET
:
313 ssp_queue_ssp_refresh_task(data
, 0);
318 if (data
->time_syncing
)
319 data
->timestamp
= ktime_get_real_ns();
325 int ssp_irq_msg(struct ssp_data
*data
)
331 u16 length
, msg_options
;
332 struct ssp_msg
*msg
, *n
;
334 ret
= spi_read(data
->spi
, data
->header_buffer
, SSP_HEADER_BUFFER_SIZE
);
336 dev_err(SSP_DEV
, "header read fail\n");
340 length
= le16_to_cpu(data
->header_buffer
[1]);
341 msg_options
= le16_to_cpu(data
->header_buffer
[0]);
344 dev_err(SSP_DEV
, "length received from mcu is 0\n");
348 msg_type
= SSP_GET_MESSAGE_TYPE(msg_options
);
351 case SSP_AP2HUB_READ
:
352 case SSP_AP2HUB_WRITE
:
354 * this is a small list, a few elements - the packets can be
355 * received with no order
357 mutex_lock(&data
->pending_lock
);
358 list_for_each_entry_safe(msg
, n
, &data
->pending_list
, list
) {
359 if (msg
->options
== msg_options
) {
360 list_del(&msg
->list
);
368 * here can be implemented dead messages handling
369 * but the slave should not send such ones - it is to
370 * check but let's handle this
372 buffer
= kmalloc(length
, GFP_KERNEL
| GFP_DMA
);
378 /* got dead packet so it is always an error */
379 ret
= spi_read(data
->spi
, buffer
, length
);
385 dev_err(SSP_DEV
, "No match error %x\n",
391 if (msg_type
== SSP_AP2HUB_READ
)
392 ret
= spi_read(data
->spi
,
393 &msg
->buffer
[SSP_HEADER_SIZE_ALIGNED
],
396 if (msg_type
== SSP_AP2HUB_WRITE
) {
397 ret
= spi_write(data
->spi
,
398 &msg
->buffer
[SSP_HEADER_SIZE_ALIGNED
],
400 if (msg_options
& SSP_AP2HUB_RETURN
) {
402 SSP_AP2HUB_READ
| SSP_AP2HUB_RETURN
;
405 list_add_tail(&msg
->list
, &data
->pending_list
);
411 if (!completion_done(msg
->done
))
414 mutex_unlock(&data
->pending_lock
);
416 case SSP_HUB2AP_WRITE
:
417 buffer
= kzalloc(length
, GFP_KERNEL
| GFP_DMA
);
421 ret
= spi_read(data
->spi
, buffer
, length
);
423 dev_err(SSP_DEV
, "spi read fail\n");
428 ret
= ssp_parse_dataframe(data
, buffer
, length
);
434 dev_err(SSP_DEV
, "unknown msg type\n");
441 void ssp_clean_pending_list(struct ssp_data
*data
)
443 struct ssp_msg
*msg
, *n
;
445 mutex_lock(&data
->pending_lock
);
446 list_for_each_entry_safe(msg
, n
, &data
->pending_list
, list
) {
447 list_del(&msg
->list
);
450 if (!completion_done(msg
->done
))
453 mutex_unlock(&data
->pending_lock
);
456 int ssp_command(struct ssp_data
*data
, char command
, int arg
)
461 msg
= ssp_create_msg(command
, 0, SSP_AP2HUB_WRITE
, arg
);
465 ssp_dbg("%s - command 0x%x %d\n", __func__
, command
, arg
);
467 ret
= ssp_spi_sync_command(data
, msg
);
473 int ssp_send_instruction(struct ssp_data
*data
, u8 inst
, u8 sensor_type
,
474 u8
*send_buf
, u8 length
)
479 if (data
->fw_dl_state
== SSP_FW_DL_STATE_DOWNLOADING
) {
480 dev_err(SSP_DEV
, "%s - Skip Inst! DL state = %d\n",
481 __func__
, data
->fw_dl_state
);
483 } else if (!(data
->available_sensors
& BIT(sensor_type
)) &&
484 (inst
<= SSP_MSG2SSP_INST_CHANGE_DELAY
)) {
485 dev_err(SSP_DEV
, "%s - Bypass Inst Skip! - %u\n",
486 __func__
, sensor_type
);
487 return -EIO
; /* just fail */
490 msg
= ssp_create_msg(inst
, length
+ 2, SSP_AP2HUB_WRITE
, 0);
494 ssp_fill_buffer(msg
, 0, &sensor_type
, 1);
495 ssp_fill_buffer(msg
, 1, send_buf
, length
);
497 ssp_dbg("%s - Inst = 0x%x, Sensor Type = 0x%x, data = %u\n",
498 __func__
, inst
, sensor_type
, send_buf
[1]);
500 ret
= ssp_spi_sync(data
, msg
, 1000);
506 int ssp_get_chipid(struct ssp_data
*data
)
512 msg
= ssp_create_msg(SSP_MSG2SSP_AP_WHOAMI
, 1, SSP_AP2HUB_READ
, 0);
516 ret
= ssp_spi_sync(data
, msg
, 1000);
518 buffer
= SSP_GET_BUFFER_AT_INDEX(msg
, 0);
522 return ret
< 0 ? ret
: buffer
;
525 int ssp_set_magnetic_matrix(struct ssp_data
*data
)
530 msg
= ssp_create_msg(SSP_MSG2SSP_AP_SET_MAGNETIC_STATIC_MATRIX
,
531 data
->sensorhub_info
->mag_length
, SSP_AP2HUB_WRITE
,
536 ssp_fill_buffer(msg
, 0, data
->sensorhub_info
->mag_table
,
537 data
->sensorhub_info
->mag_length
);
539 ret
= ssp_spi_sync(data
, msg
, 1000);
545 unsigned int ssp_get_sensor_scanning_info(struct ssp_data
*data
)
551 struct ssp_msg
*msg
= ssp_create_msg(SSP_MSG2SSP_AP_SENSOR_SCANNING
, 4,
556 ret
= ssp_spi_sync(data
, msg
, 1000);
558 dev_err(SSP_DEV
, "%s - spi read fail %d\n", __func__
, ret
);
562 ssp_get_buffer(msg
, 0, &result
, 4);
563 cpu_result
= le32_to_cpu(result
);
565 dev_info(SSP_DEV
, "%s state: 0x%08x\n", __func__
, cpu_result
);
572 unsigned int ssp_get_firmware_rev(struct ssp_data
*data
)
577 struct ssp_msg
*msg
= ssp_create_msg(SSP_MSG2SSP_AP_FIRMWARE_REV
, 4,
580 return SSP_INVALID_REVISION
;
582 ret
= ssp_spi_sync(data
, msg
, 1000);
584 dev_err(SSP_DEV
, "%s - transfer fail %d\n", __func__
, ret
);
585 ret
= SSP_INVALID_REVISION
;
589 ssp_get_buffer(msg
, 0, &result
, 4);
590 ret
= le32_to_cpu(result
);