1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
4 */
5
6 #include "ssp.h"
7
8 #define SSP_DEV (&data->spi->dev)
9 #define SSP_GET_MESSAGE_TYPE(data) (data & (3 << SSP_RW))
10
11 /*
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.
16 */
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
24
25 #define SSP_UNIMPLEMENTED -1
26
27 struct ssp_msg_header {
28 u8 cmd;
29 __le16 length;
30 __le16 options;
31 __le32 data;
32 } __attribute__((__packed__));
33
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;
41 };
42
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,
70 };
71
72 #define SSP_HEADER_SIZE (sizeof(struct ssp_msg_header))
73 #define SSP_HEADER_SIZE_ALIGNED (ALIGN(SSP_HEADER_SIZE, 4))
74
ssp_create_msg(u8 cmd,u16 len,u16 opt,u32 data)75 static struct ssp_msg *ssp_create_msg(u8 cmd, u16 len, u16 opt, u32 data)
76 {
77 struct ssp_msg_header h;
78 struct ssp_msg *msg;
79
80 msg = kzalloc(sizeof(*msg), GFP_KERNEL);
81 if (!msg)
82 return NULL;
83
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);
88
89 msg->buffer = kzalloc(SSP_HEADER_SIZE_ALIGNED + len,
90 GFP_KERNEL | GFP_DMA);
91 if (!msg->buffer) {
92 kfree(msg);
93 return NULL;
94 }
95
96 msg->length = len;
97 msg->options = opt;
98
99 memcpy(msg->buffer, &h, SSP_HEADER_SIZE);
100
101 return msg;
102 }
103
104 /*
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.
108 */
ssp_fill_buffer(struct ssp_msg * m,unsigned int offset,const void * src,unsigned int len)109 static inline void ssp_fill_buffer(struct ssp_msg *m, unsigned int offset,
110 const void *src, unsigned int len)
111 {
112 memcpy(&m->buffer[SSP_HEADER_SIZE_ALIGNED + offset], src, len);
113 }
114
ssp_get_buffer(struct ssp_msg * m,unsigned int offset,void * dest,unsigned int len)115 static inline void ssp_get_buffer(struct ssp_msg *m, unsigned int offset,
116 void *dest, unsigned int len)
117 {
118 memcpy(dest, &m->buffer[SSP_HEADER_SIZE_ALIGNED + offset], len);
119 }
120
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)
125
ssp_clean_msg(struct ssp_msg * m)126 static void ssp_clean_msg(struct ssp_msg *m)
127 {
128 kfree(m->buffer);
129 kfree(m);
130 }
131
ssp_print_mcu_debug(char * data_frame,int * data_index,int received_len)132 static int ssp_print_mcu_debug(char *data_frame, int *data_index,
133 int received_len)
134 {
135 int length = data_frame[(*data_index)++];
136
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 -EPROTO;
141 }
142
143 ssp_dbg("[SSP]: MSG From MCU - %s\n", &data_frame[*data_index]);
144
145 *data_index += length;
146
147 return 0;
148 }
149
150 /*
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.
153 */
ssp_check_lines(struct ssp_data * data,bool state)154 static int ssp_check_lines(struct ssp_data *data, bool state)
155 {
156 int delay_cnt = 0;
157
158 gpiod_set_value_cansleep(data->ap_mcu_gpiod, state);
159
160 while (gpiod_get_value_cansleep(data->mcu_ap_gpiod) != state) {
161 usleep_range(3000, 3500);
162
163 if (data->shut_down || delay_cnt++ > 500) {
164 dev_err(SSP_DEV, "%s:timeout, hw ack wait fail %d\n",
165 __func__, state);
166
167 if (!state)
168 gpiod_set_value_cansleep(data->ap_mcu_gpiod, 1);
169
170 return -ETIMEDOUT;
171 }
172 }
173
174 return 0;
175 }
176
ssp_do_transfer(struct ssp_data * data,struct ssp_msg * msg,struct completion * done,int timeout)177 static int ssp_do_transfer(struct ssp_data *data, struct ssp_msg *msg,
178 struct completion *done, int timeout)
179 {
180 int status;
181 /*
182 * check if this is a short one way message or the whole transfer has
183 * second part after an interrupt
184 */
185 const bool use_no_irq = msg->length == 0;
186
187 if (data->shut_down)
188 return -EPERM;
189
190 msg->done = done;
191
192 mutex_lock(&data->comm_lock);
193
194 status = ssp_check_lines(data, false);
195 if (status < 0)
196 goto _error_locked;
197
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;
203 }
204
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);
209 }
210
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);
217 }
218 goto _error_locked;
219 }
220
221 mutex_unlock(&data->comm_lock);
222
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);
230
231 data->timeout_cnt++;
232 return -ETIMEDOUT;
233 }
234
235 return 0;
236
237 _error_locked:
238 mutex_unlock(&data->comm_lock);
239 data->timeout_cnt++;
240 return status;
241 }
242
ssp_spi_sync_command(struct ssp_data * data,struct ssp_msg * msg)243 static inline int ssp_spi_sync_command(struct ssp_data *data,
244 struct ssp_msg *msg)
245 {
246 return ssp_do_transfer(data, msg, NULL, 0);
247 }
248
ssp_spi_sync(struct ssp_data * data,struct ssp_msg * msg,int timeout)249 static int ssp_spi_sync(struct ssp_data *data, struct ssp_msg *msg,
250 int timeout)
251 {
252 DECLARE_COMPLETION_ONSTACK(done);
253
254 if (WARN_ON(!msg->length))
255 return -EPERM;
256
257 return ssp_do_transfer(data, msg, &done, timeout);
258 }
259
ssp_handle_big_data(struct ssp_data * data,char * dataframe,int * idx)260 static int ssp_handle_big_data(struct ssp_data *data, char *dataframe, int *idx)
261 {
262 /* mock-up, it will be changed with adding another sensor types */
263 *idx += 8;
264 return 0;
265 }
266
ssp_parse_dataframe(struct ssp_data * data,char * dataframe,int len)267 static int ssp_parse_dataframe(struct ssp_data *data, char *dataframe, int len)
268 {
269 int idx, sd;
270 struct ssp_sensor_data *spd;
271 struct iio_dev **indio_devs = data->sensor_devs;
272
273 for (idx = 0; idx < len;) {
274 switch (dataframe[idx++]) {
275 case SSP_MSG2AP_INST_BYPASS_DATA:
276 if (idx >= len)
277 return -EPROTO;
278 sd = dataframe[idx++];
279 if (sd < 0 || sd >= SSP_SENSOR_MAX) {
280 dev_err(SSP_DEV,
281 "Mcu data frame1 error %d\n", sd);
282 return -EPROTO;
283 }
284
285 if (indio_devs[sd]) {
286 spd = iio_priv(indio_devs[sd]);
287 if (spd->process_data) {
288 if (idx >= len)
289 return -EPROTO;
290 spd->process_data(indio_devs[sd],
291 &dataframe[idx],
292 data->timestamp);
293 }
294 } else {
295 dev_err(SSP_DEV, "no client for frame\n");
296 }
297
298 idx += ssp_offset_map[sd];
299 break;
300 case SSP_MSG2AP_INST_DEBUG_DATA:
301 if (idx >= len)
302 return -EPROTO;
303 sd = ssp_print_mcu_debug(dataframe, &idx, len);
304 if (sd) {
305 dev_err(SSP_DEV,
306 "Mcu data frame3 error %d\n", sd);
307 return sd;
308 }
309 break;
310 case SSP_MSG2AP_INST_LIBRARY_DATA:
311 idx += len;
312 break;
313 case SSP_MSG2AP_INST_BIG_DATA:
314 ssp_handle_big_data(data, dataframe, &idx);
315 break;
316 case SSP_MSG2AP_INST_TIME_SYNC:
317 data->time_syncing = true;
318 break;
319 case SSP_MSG2AP_INST_RESET:
320 ssp_queue_ssp_refresh_task(data, 0);
321 break;
322 }
323 }
324
325 if (data->time_syncing)
326 data->timestamp = ktime_get_real_ns();
327
328 return 0;
329 }
330
331 /* threaded irq */
ssp_irq_msg(struct ssp_data * data)332 int ssp_irq_msg(struct ssp_data *data)
333 {
334 bool found = false;
335 char *buffer;
336 u8 msg_type;
337 int ret;
338 u16 length, msg_options;
339 struct ssp_msg *msg, *n;
340
341 ret = spi_read(data->spi, data->header_buffer, SSP_HEADER_BUFFER_SIZE);
342 if (ret < 0) {
343 dev_err(SSP_DEV, "header read fail\n");
344 return ret;
345 }
346
347 length = le16_to_cpu(data->header_buffer[1]);
348 msg_options = le16_to_cpu(data->header_buffer[0]);
349
350 if (length == 0) {
351 dev_err(SSP_DEV, "length received from mcu is 0\n");
352 return -EINVAL;
353 }
354
355 msg_type = SSP_GET_MESSAGE_TYPE(msg_options);
356
357 switch (msg_type) {
358 case SSP_AP2HUB_READ:
359 case SSP_AP2HUB_WRITE:
360 /*
361 * this is a small list, a few elements - the packets can be
362 * received with no order
363 */
364 mutex_lock(&data->pending_lock);
365 list_for_each_entry_safe(msg, n, &data->pending_list, list) {
366 if (msg->options == msg_options) {
367 list_del(&msg->list);
368 found = true;
369 break;
370 }
371 }
372
373 if (!found) {
374 /*
375 * here can be implemented dead messages handling
376 * but the slave should not send such ones - it is to
377 * check but let's handle this
378 */
379 buffer = kmalloc(length, GFP_KERNEL | GFP_DMA);
380 if (!buffer) {
381 ret = -ENOMEM;
382 goto _unlock;
383 }
384
385 /* got dead packet so it is always an error */
386 ret = spi_read(data->spi, buffer, length);
387 if (ret >= 0)
388 ret = -EPROTO;
389
390 kfree(buffer);
391
392 dev_err(SSP_DEV, "No match error %x\n",
393 msg_options);
394
395 goto _unlock;
396 }
397
398 if (msg_type == SSP_AP2HUB_READ)
399 ret = spi_read(data->spi,
400 &msg->buffer[SSP_HEADER_SIZE_ALIGNED],
401 msg->length);
402
403 if (msg_type == SSP_AP2HUB_WRITE) {
404 ret = spi_write(data->spi,
405 &msg->buffer[SSP_HEADER_SIZE_ALIGNED],
406 msg->length);
407 if (msg_options & SSP_AP2HUB_RETURN) {
408 msg->options =
409 SSP_AP2HUB_READ | SSP_AP2HUB_RETURN;
410 msg->length = 1;
411
412 list_add_tail(&msg->list, &data->pending_list);
413 goto _unlock;
414 }
415 }
416
417 if (msg->done)
418 if (!completion_done(msg->done))
419 complete(msg->done);
420 _unlock:
421 mutex_unlock(&data->pending_lock);
422 break;
423 case SSP_HUB2AP_WRITE:
424 buffer = kzalloc(length, GFP_KERNEL | GFP_DMA);
425 if (!buffer)
426 return -ENOMEM;
427
428 ret = spi_read(data->spi, buffer, length);
429 if (ret < 0) {
430 dev_err(SSP_DEV, "spi read fail\n");
431 kfree(buffer);
432 break;
433 }
434
435 ret = ssp_parse_dataframe(data, buffer, length);
436
437 kfree(buffer);
438 break;
439
440 default:
441 dev_err(SSP_DEV, "unknown msg type\n");
442 return -EPROTO;
443 }
444
445 return ret;
446 }
447
ssp_clean_pending_list(struct ssp_data * data)448 void ssp_clean_pending_list(struct ssp_data *data)
449 {
450 struct ssp_msg *msg, *n;
451
452 mutex_lock(&data->pending_lock);
453 list_for_each_entry_safe(msg, n, &data->pending_list, list) {
454 list_del(&msg->list);
455
456 if (msg->done)
457 if (!completion_done(msg->done))
458 complete(msg->done);
459 }
460 mutex_unlock(&data->pending_lock);
461 }
462
ssp_command(struct ssp_data * data,char command,int arg)463 int ssp_command(struct ssp_data *data, char command, int arg)
464 {
465 int ret;
466 struct ssp_msg *msg;
467
468 msg = ssp_create_msg(command, 0, SSP_AP2HUB_WRITE, arg);
469 if (!msg)
470 return -ENOMEM;
471
472 ssp_dbg("%s - command 0x%x %d\n", __func__, command, arg);
473
474 ret = ssp_spi_sync_command(data, msg);
475 ssp_clean_msg(msg);
476
477 return ret;
478 }
479
ssp_send_instruction(struct ssp_data * data,u8 inst,u8 sensor_type,u8 * send_buf,u8 length)480 int ssp_send_instruction(struct ssp_data *data, u8 inst, u8 sensor_type,
481 u8 *send_buf, u8 length)
482 {
483 int ret;
484 struct ssp_msg *msg;
485
486 if (data->fw_dl_state == SSP_FW_DL_STATE_DOWNLOADING) {
487 dev_err(SSP_DEV, "%s - Skip Inst! DL state = %d\n",
488 __func__, data->fw_dl_state);
489 return -EBUSY;
490 } else if (!(data->available_sensors & BIT(sensor_type)) &&
491 (inst <= SSP_MSG2SSP_INST_CHANGE_DELAY)) {
492 dev_err(SSP_DEV, "%s - Bypass Inst Skip! - %u\n",
493 __func__, sensor_type);
494 return -EIO; /* just fail */
495 }
496
497 msg = ssp_create_msg(inst, length + 2, SSP_AP2HUB_WRITE, 0);
498 if (!msg)
499 return -ENOMEM;
500
501 ssp_fill_buffer(msg, 0, &sensor_type, 1);
502 ssp_fill_buffer(msg, 1, send_buf, length);
503
504 ssp_dbg("%s - Inst = 0x%x, Sensor Type = 0x%x, data = %u\n",
505 __func__, inst, sensor_type, send_buf[1]);
506
507 ret = ssp_spi_sync(data, msg, 1000);
508 ssp_clean_msg(msg);
509
510 return ret;
511 }
512
ssp_get_chipid(struct ssp_data * data)513 int ssp_get_chipid(struct ssp_data *data)
514 {
515 int ret;
516 char buffer;
517 struct ssp_msg *msg;
518
519 msg = ssp_create_msg(SSP_MSG2SSP_AP_WHOAMI, 1, SSP_AP2HUB_READ, 0);
520 if (!msg)
521 return -ENOMEM;
522
523 ret = ssp_spi_sync(data, msg, 1000);
524
525 buffer = SSP_GET_BUFFER_AT_INDEX(msg, 0);
526
527 ssp_clean_msg(msg);
528
529 return ret < 0 ? ret : buffer;
530 }
531
ssp_set_magnetic_matrix(struct ssp_data * data)532 int ssp_set_magnetic_matrix(struct ssp_data *data)
533 {
534 int ret;
535 struct ssp_msg *msg;
536
537 msg = ssp_create_msg(SSP_MSG2SSP_AP_SET_MAGNETIC_STATIC_MATRIX,
538 data->sensorhub_info->mag_length, SSP_AP2HUB_WRITE,
539 0);
540 if (!msg)
541 return -ENOMEM;
542
543 ssp_fill_buffer(msg, 0, data->sensorhub_info->mag_table,
544 data->sensorhub_info->mag_length);
545
546 ret = ssp_spi_sync(data, msg, 1000);
547 ssp_clean_msg(msg);
548
549 return ret;
550 }
551
ssp_get_sensor_scanning_info(struct ssp_data * data)552 unsigned int ssp_get_sensor_scanning_info(struct ssp_data *data)
553 {
554 int ret;
555 __le32 result;
556 u32 cpu_result = 0;
557
558 struct ssp_msg *msg = ssp_create_msg(SSP_MSG2SSP_AP_SENSOR_SCANNING, 4,
559 SSP_AP2HUB_READ, 0);
560 if (!msg)
561 return 0;
562
563 ret = ssp_spi_sync(data, msg, 1000);
564 if (ret < 0) {
565 dev_err(SSP_DEV, "%s - spi read fail %d\n", __func__, ret);
566 goto _exit;
567 }
568
569 ssp_get_buffer(msg, 0, &result, 4);
570 cpu_result = le32_to_cpu(result);
571
572 dev_info(SSP_DEV, "%s state: 0x%08x\n", __func__, cpu_result);
573
574 _exit:
575 ssp_clean_msg(msg);
576 return cpu_result;
577 }
578
ssp_get_firmware_rev(struct ssp_data * data)579 unsigned int ssp_get_firmware_rev(struct ssp_data *data)
580 {
581 int ret;
582 __le32 result;
583
584 struct ssp_msg *msg = ssp_create_msg(SSP_MSG2SSP_AP_FIRMWARE_REV, 4,
585 SSP_AP2HUB_READ, 0);
586 if (!msg)
587 return SSP_INVALID_REVISION;
588
589 ret = ssp_spi_sync(data, msg, 1000);
590 if (ret < 0) {
591 dev_err(SSP_DEV, "%s - transfer fail %d\n", __func__, ret);
592 ret = SSP_INVALID_REVISION;
593 goto _exit;
594 }
595
596 ssp_get_buffer(msg, 0, &result, 4);
597 ret = le32_to_cpu(result);
598
599 _exit:
600 ssp_clean_msg(msg);
601 return ret;
602 }
603