WIP FPC-III support
[linux/fpc-iii.git] / drivers / iio / common / ssp_sensors / ssp_spi.c
blob4864c38b8d1c2d6f66c89fbe7a1407ebf9f4073f
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
4 */
6 #include "ssp.h"
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 {
28 u8 cmd;
29 __le16 length;
30 __le16 options;
31 __le32 data;
32 } __attribute__((__packed__));
34 struct ssp_msg {
35 u16 length;
36 u16 options;
37 struct list_head list;
38 struct completion *done;
39 struct ssp_msg_header *h;
40 char *buffer;
43 static const int ssp_offset_map[SSP_SENSOR_MAX] = {
44 [SSP_ACCELEROMETER_SENSOR] = SSP_ACCELEROMETER_SIZE +
45 SSP_TIME_SIZE,
46 [SSP_GYROSCOPE_SENSOR] = SSP_GYROSCOPE_SIZE +
47 SSP_TIME_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 +
65 SSP_TIME_SIZE,
66 [SSP_BIO_HRM_RAW_FAC] = SSP_BIO_HRM_RAW_FAC_SIZE +
67 SSP_TIME_SIZE,
68 [SSP_BIO_HRM_LIB] = SSP_BIO_HRM_LIB_SIZE +
69 SSP_TIME_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;
78 struct ssp_msg *msg;
80 msg = kzalloc(sizeof(*msg), GFP_KERNEL);
81 if (!msg)
82 return NULL;
84 h.cmd = cmd;
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);
91 if (!msg->buffer) {
92 kfree(msg);
93 return NULL;
96 msg->length = len;
97 msg->options = opt;
99 memcpy(msg->buffer, &h, SSP_HEADER_SIZE);
101 return msg;
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)
128 kfree(m->buffer);
129 kfree(m);
132 static int ssp_print_mcu_debug(char *data_frame, int *data_index,
133 int received_len)
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;
147 return 0;
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)
156 int delay_cnt = 0;
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",
165 __func__, state);
167 if (!state)
168 gpiod_set_value_cansleep(data->ap_mcu_gpiod, 1);
170 return -ETIMEDOUT;
174 return 0;
177 static int ssp_do_transfer(struct ssp_data *data, struct ssp_msg *msg,
178 struct completion *done, int timeout)
180 int status;
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;
187 if (data->shut_down)
188 return -EPERM;
190 msg->done = done;
192 mutex_lock(&data->comm_lock);
194 status = ssp_check_lines(data, false);
195 if (status < 0)
196 goto _error_locked;
198 status = spi_write(data->spi, msg->buffer, SSP_HEADER_SIZE);
199 if (status < 0) {
200 gpiod_set_value_cansleep(data->ap_mcu_gpiod, 1);
201 dev_err(SSP_DEV, "%s spi_write fail\n", __func__);
202 goto _error_locked;
205 if (!use_no_irq) {
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);
212 if (status < 0) {
213 if (!use_no_irq) {
214 mutex_lock(&data->pending_lock);
215 list_del(&msg->list);
216 mutex_unlock(&data->pending_lock);
218 goto _error_locked;
221 mutex_unlock(&data->comm_lock);
223 if (!use_no_irq && done)
224 if (wait_for_completion_timeout(done,
225 msecs_to_jiffies(timeout)) ==
226 0) {
227 mutex_lock(&data->pending_lock);
228 list_del(&msg->list);
229 mutex_unlock(&data->pending_lock);
231 data->timeout_cnt++;
232 return -ETIMEDOUT;
235 return 0;
237 _error_locked:
238 mutex_unlock(&data->comm_lock);
239 data->timeout_cnt++;
240 return status;
243 static inline int ssp_spi_sync_command(struct ssp_data *data,
244 struct ssp_msg *msg)
246 return ssp_do_transfer(data, msg, NULL, 0);
249 static int ssp_spi_sync(struct ssp_data *data, struct ssp_msg *msg,
250 int timeout)
252 DECLARE_COMPLETION_ONSTACK(done);
254 if (WARN_ON(!msg->length))
255 return -EPERM;
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 */
263 *idx += 8;
264 return 0;
267 static int ssp_parse_dataframe(struct ssp_data *data, char *dataframe, int len)
269 int idx, sd;
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) {
278 dev_err(SSP_DEV,
279 "Mcu data frame1 error %d\n", sd);
280 return -EPROTO;
283 if (indio_devs[sd]) {
284 spd = iio_priv(indio_devs[sd]);
285 if (spd->process_data)
286 spd->process_data(indio_devs[sd],
287 &dataframe[idx],
288 data->timestamp);
289 } else {
290 dev_err(SSP_DEV, "no client for frame\n");
293 idx += ssp_offset_map[sd];
294 break;
295 case SSP_MSG2AP_INST_DEBUG_DATA:
296 sd = ssp_print_mcu_debug(dataframe, &idx, len);
297 if (sd) {
298 dev_err(SSP_DEV,
299 "Mcu data frame3 error %d\n", sd);
300 return sd;
302 break;
303 case SSP_MSG2AP_INST_LIBRARY_DATA:
304 idx += len;
305 break;
306 case SSP_MSG2AP_INST_BIG_DATA:
307 ssp_handle_big_data(data, dataframe, &idx);
308 break;
309 case SSP_MSG2AP_INST_TIME_SYNC:
310 data->time_syncing = true;
311 break;
312 case SSP_MSG2AP_INST_RESET:
313 ssp_queue_ssp_refresh_task(data, 0);
314 break;
318 if (data->time_syncing)
319 data->timestamp = ktime_get_real_ns();
321 return 0;
324 /* threaded irq */
325 int ssp_irq_msg(struct ssp_data *data)
327 bool found = false;
328 char *buffer;
329 u8 msg_type;
330 int ret;
331 u16 length, msg_options;
332 struct ssp_msg *msg, *n;
334 ret = spi_read(data->spi, data->header_buffer, SSP_HEADER_BUFFER_SIZE);
335 if (ret < 0) {
336 dev_err(SSP_DEV, "header read fail\n");
337 return ret;
340 length = le16_to_cpu(data->header_buffer[1]);
341 msg_options = le16_to_cpu(data->header_buffer[0]);
343 if (length == 0) {
344 dev_err(SSP_DEV, "length received from mcu is 0\n");
345 return -EINVAL;
348 msg_type = SSP_GET_MESSAGE_TYPE(msg_options);
350 switch (msg_type) {
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);
361 found = true;
362 break;
366 if (!found) {
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);
373 if (!buffer) {
374 ret = -ENOMEM;
375 goto _unlock;
378 /* got dead packet so it is always an error */
379 ret = spi_read(data->spi, buffer, length);
380 if (ret >= 0)
381 ret = -EPROTO;
383 kfree(buffer);
385 dev_err(SSP_DEV, "No match error %x\n",
386 msg_options);
388 goto _unlock;
391 if (msg_type == SSP_AP2HUB_READ)
392 ret = spi_read(data->spi,
393 &msg->buffer[SSP_HEADER_SIZE_ALIGNED],
394 msg->length);
396 if (msg_type == SSP_AP2HUB_WRITE) {
397 ret = spi_write(data->spi,
398 &msg->buffer[SSP_HEADER_SIZE_ALIGNED],
399 msg->length);
400 if (msg_options & SSP_AP2HUB_RETURN) {
401 msg->options =
402 SSP_AP2HUB_READ | SSP_AP2HUB_RETURN;
403 msg->length = 1;
405 list_add_tail(&msg->list, &data->pending_list);
406 goto _unlock;
410 if (msg->done)
411 if (!completion_done(msg->done))
412 complete(msg->done);
413 _unlock:
414 mutex_unlock(&data->pending_lock);
415 break;
416 case SSP_HUB2AP_WRITE:
417 buffer = kzalloc(length, GFP_KERNEL | GFP_DMA);
418 if (!buffer)
419 return -ENOMEM;
421 ret = spi_read(data->spi, buffer, length);
422 if (ret < 0) {
423 dev_err(SSP_DEV, "spi read fail\n");
424 kfree(buffer);
425 break;
428 ret = ssp_parse_dataframe(data, buffer, length);
430 kfree(buffer);
431 break;
433 default:
434 dev_err(SSP_DEV, "unknown msg type\n");
435 return -EPROTO;
438 return ret;
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);
449 if (msg->done)
450 if (!completion_done(msg->done))
451 complete(msg->done);
453 mutex_unlock(&data->pending_lock);
456 int ssp_command(struct ssp_data *data, char command, int arg)
458 int ret;
459 struct ssp_msg *msg;
461 msg = ssp_create_msg(command, 0, SSP_AP2HUB_WRITE, arg);
462 if (!msg)
463 return -ENOMEM;
465 ssp_dbg("%s - command 0x%x %d\n", __func__, command, arg);
467 ret = ssp_spi_sync_command(data, msg);
468 ssp_clean_msg(msg);
470 return ret;
473 int ssp_send_instruction(struct ssp_data *data, u8 inst, u8 sensor_type,
474 u8 *send_buf, u8 length)
476 int ret;
477 struct ssp_msg *msg;
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);
482 return -EBUSY;
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);
491 if (!msg)
492 return -ENOMEM;
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);
501 ssp_clean_msg(msg);
503 return ret;
506 int ssp_get_chipid(struct ssp_data *data)
508 int ret;
509 char buffer;
510 struct ssp_msg *msg;
512 msg = ssp_create_msg(SSP_MSG2SSP_AP_WHOAMI, 1, SSP_AP2HUB_READ, 0);
513 if (!msg)
514 return -ENOMEM;
516 ret = ssp_spi_sync(data, msg, 1000);
518 buffer = SSP_GET_BUFFER_AT_INDEX(msg, 0);
520 ssp_clean_msg(msg);
522 return ret < 0 ? ret : buffer;
525 int ssp_set_magnetic_matrix(struct ssp_data *data)
527 int ret;
528 struct ssp_msg *msg;
530 msg = ssp_create_msg(SSP_MSG2SSP_AP_SET_MAGNETIC_STATIC_MATRIX,
531 data->sensorhub_info->mag_length, SSP_AP2HUB_WRITE,
533 if (!msg)
534 return -ENOMEM;
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);
540 ssp_clean_msg(msg);
542 return ret;
545 unsigned int ssp_get_sensor_scanning_info(struct ssp_data *data)
547 int ret;
548 __le32 result;
549 u32 cpu_result = 0;
551 struct ssp_msg *msg = ssp_create_msg(SSP_MSG2SSP_AP_SENSOR_SCANNING, 4,
552 SSP_AP2HUB_READ, 0);
553 if (!msg)
554 return 0;
556 ret = ssp_spi_sync(data, msg, 1000);
557 if (ret < 0) {
558 dev_err(SSP_DEV, "%s - spi read fail %d\n", __func__, ret);
559 goto _exit;
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);
567 _exit:
568 ssp_clean_msg(msg);
569 return cpu_result;
572 unsigned int ssp_get_firmware_rev(struct ssp_data *data)
574 int ret;
575 __le32 result;
577 struct ssp_msg *msg = ssp_create_msg(SSP_MSG2SSP_AP_FIRMWARE_REV, 4,
578 SSP_AP2HUB_READ, 0);
579 if (!msg)
580 return SSP_INVALID_REVISION;
582 ret = ssp_spi_sync(data, msg, 1000);
583 if (ret < 0) {
584 dev_err(SSP_DEV, "%s - transfer fail %d\n", __func__, ret);
585 ret = SSP_INVALID_REVISION;
586 goto _exit;
589 ssp_get_buffer(msg, 0, &result, 4);
590 ret = le32_to_cpu(result);
592 _exit:
593 ssp_clean_msg(msg);
594 return ret;